2 * wm2000.c -- WM2000 ALSA Soc Audio driver
4 * Copyright 2008-2011 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * The download image for the WM2000 will be requested as
13 * 'wm2000_anc.bin' by default (overridable via platform data) at
14 * runtime and is expected to be in flat binary format. This is
15 * generated by Wolfson configuration tools and includes
16 * system-specific calibration information. If supplied as a
17 * sequence of ASCII-encoded hexidecimal bytes this can be converted
18 * into a flat binary with a command such as this on the command line:
20 * perl -e 'while (<>) { s/[\r\n]+// ; printf("%c", hex($_)); }'
21 * < file > wm2000_anc.bin
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/firmware.h>
29 #include <linux/clk.h>
30 #include <linux/delay.h>
32 #include <linux/i2c.h>
33 #include <linux/regmap.h>
34 #include <linux/debugfs.h>
35 #include <linux/regulator/consumer.h>
36 #include <linux/slab.h>
37 #include <sound/core.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include <sound/soc.h>
41 #include <sound/initval.h>
42 #include <sound/tlv.h>
44 #include <sound/wm2000.h>
48 #define WM2000_NUM_SUPPLIES 3
50 static const char *wm2000_supplies[WM2000_NUM_SUPPLIES] = {
56 enum wm2000_anc_mode {
64 struct i2c_client *i2c;
65 struct regmap *regmap;
68 struct regulator_bulk_data supplies[WM2000_NUM_SUPPLIES];
70 enum wm2000_anc_mode anc_mode;
72 unsigned int anc_active:1;
73 unsigned int anc_eng_ena:1;
74 unsigned int spk_ena:1;
76 unsigned int speech_clarity:1;
78 int anc_download_size;
84 static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
87 struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
88 return regmap_write(wm2000->regmap, reg, value);
91 static void wm2000_reset(struct wm2000_priv *wm2000)
93 struct i2c_client *i2c = wm2000->i2c;
95 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
96 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
97 wm2000_write(i2c, WM2000_REG_ID1, 0);
99 wm2000->anc_mode = ANC_OFF;
102 static int wm2000_poll_bit(struct i2c_client *i2c,
103 unsigned int reg, u8 mask)
105 struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
109 regmap_read(wm2000->regmap, reg, &val);
111 while (!(val & mask) && --timeout) {
113 regmap_read(wm2000->regmap, reg, &val);
122 static int wm2000_power_up(struct i2c_client *i2c, int analogue)
124 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
129 if (WARN_ON(wm2000->anc_mode != ANC_OFF))
132 dev_dbg(&i2c->dev, "Beginning power up\n");
134 ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies);
136 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
140 rate = clk_get_rate(wm2000->mclk);
141 if (rate <= 13500000) {
142 dev_dbg(&i2c->dev, "Disabling MCLK divider\n");
143 wm2000_write(i2c, WM2000_REG_SYS_CTL2,
144 WM2000_MCLK_DIV2_ENA_CLR);
146 dev_dbg(&i2c->dev, "Enabling MCLK divider\n");
147 wm2000_write(i2c, WM2000_REG_SYS_CTL2,
148 WM2000_MCLK_DIV2_ENA_SET);
151 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
152 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET);
154 /* Wait for ANC engine to become ready */
155 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
156 WM2000_ANC_ENG_IDLE)) {
157 dev_err(&i2c->dev, "ANC engine failed to reset\n");
158 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
162 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
163 WM2000_STATUS_BOOT_COMPLETE)) {
164 dev_err(&i2c->dev, "ANC engine failed to initialise\n");
165 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
169 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
171 /* Open code download of the data since it is the only bulk
173 dev_dbg(&i2c->dev, "Downloading %d bytes\n",
174 wm2000->anc_download_size - 2);
176 ret = i2c_master_send(i2c, wm2000->anc_download,
177 wm2000->anc_download_size);
179 dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret);
180 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
183 if (ret != wm2000->anc_download_size) {
184 dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n",
185 ret, wm2000->anc_download_size);
186 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
190 dev_dbg(&i2c->dev, "Download complete\n");
193 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
195 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
196 WM2000_MODE_ANA_SEQ_INCLUDE |
197 WM2000_MODE_MOUSE_ENABLE |
198 WM2000_MODE_THERMAL_ENABLE);
200 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
201 WM2000_MODE_MOUSE_ENABLE |
202 WM2000_MODE_THERMAL_ENABLE);
205 ret = regmap_read(wm2000->regmap, WM2000_REG_SPEECH_CLARITY, &val);
207 dev_err(&i2c->dev, "Unable to read Speech Clarity: %d\n", ret);
208 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
211 if (wm2000->speech_clarity)
212 val |= WM2000_SPEECH_CLARITY;
214 val &= ~WM2000_SPEECH_CLARITY;
215 wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, val);
217 wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33);
218 wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02);
220 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
222 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
223 WM2000_STATUS_MOUSE_ACTIVE)) {
224 dev_err(&i2c->dev, "Timed out waiting for device\n");
225 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
229 dev_dbg(&i2c->dev, "ANC active\n");
231 dev_dbg(&i2c->dev, "Analogue active\n");
232 wm2000->anc_mode = ANC_ACTIVE;
237 static int wm2000_power_down(struct i2c_client *i2c, int analogue)
239 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
242 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
243 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
244 WM2000_MODE_ANA_SEQ_INCLUDE |
245 WM2000_MODE_POWER_DOWN);
247 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
248 WM2000_MODE_POWER_DOWN);
251 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
252 WM2000_STATUS_POWER_DOWN_COMPLETE)) {
253 dev_err(&i2c->dev, "Timeout waiting for ANC power down\n");
257 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
258 WM2000_ANC_ENG_IDLE)) {
259 dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
263 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
265 dev_dbg(&i2c->dev, "powered off\n");
266 wm2000->anc_mode = ANC_OFF;
271 static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue)
273 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
275 if (WARN_ON(wm2000->anc_mode != ANC_ACTIVE))
279 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
280 WM2000_MODE_ANA_SEQ_INCLUDE |
281 WM2000_MODE_THERMAL_ENABLE |
282 WM2000_MODE_BYPASS_ENTRY);
284 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
285 WM2000_MODE_THERMAL_ENABLE |
286 WM2000_MODE_BYPASS_ENTRY);
289 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
290 WM2000_STATUS_ANC_DISABLED)) {
291 dev_err(&i2c->dev, "Timeout waiting for ANC disable\n");
295 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
296 WM2000_ANC_ENG_IDLE)) {
297 dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
301 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
302 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
304 wm2000->anc_mode = ANC_BYPASS;
305 dev_dbg(&i2c->dev, "bypass enabled\n");
310 static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue)
312 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
314 if (WARN_ON(wm2000->anc_mode != ANC_BYPASS))
317 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
320 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
321 WM2000_MODE_ANA_SEQ_INCLUDE |
322 WM2000_MODE_MOUSE_ENABLE |
323 WM2000_MODE_THERMAL_ENABLE);
325 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
326 WM2000_MODE_MOUSE_ENABLE |
327 WM2000_MODE_THERMAL_ENABLE);
330 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
331 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
333 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
334 WM2000_STATUS_MOUSE_ACTIVE)) {
335 dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
339 wm2000->anc_mode = ANC_ACTIVE;
340 dev_dbg(&i2c->dev, "MOUSE active\n");
345 static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
347 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
349 if (WARN_ON(wm2000->anc_mode != ANC_ACTIVE))
353 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
355 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
356 WM2000_MODE_ANA_SEQ_INCLUDE |
357 WM2000_MODE_THERMAL_ENABLE |
358 WM2000_MODE_STANDBY_ENTRY);
360 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
361 WM2000_MODE_THERMAL_ENABLE |
362 WM2000_MODE_STANDBY_ENTRY);
365 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
366 WM2000_STATUS_ANC_DISABLED)) {
368 "Timed out waiting for ANC disable after 1ms\n");
372 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE)) {
374 "Timed out waiting for standby\n");
378 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
379 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
381 wm2000->anc_mode = ANC_STANDBY;
382 dev_dbg(&i2c->dev, "standby\n");
384 dev_dbg(&i2c->dev, "Analogue disabled\n");
389 static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
391 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
393 if (WARN_ON(wm2000->anc_mode != ANC_STANDBY))
396 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
399 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
401 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
402 WM2000_MODE_ANA_SEQ_INCLUDE |
403 WM2000_MODE_THERMAL_ENABLE |
404 WM2000_MODE_MOUSE_ENABLE);
406 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
407 WM2000_MODE_THERMAL_ENABLE |
408 WM2000_MODE_MOUSE_ENABLE);
411 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
412 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
414 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
415 WM2000_STATUS_MOUSE_ACTIVE)) {
416 dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
420 wm2000->anc_mode = ANC_ACTIVE;
421 dev_dbg(&i2c->dev, "MOUSE active\n");
423 dev_dbg(&i2c->dev, "Analogue enabled\n");
428 typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
431 enum wm2000_anc_mode source;
432 enum wm2000_anc_mode dest;
434 wm2000_mode_fn step[2];
435 } anc_transitions[] = {
449 wm2000_enter_standby,
462 .source = ANC_ACTIVE,
470 .source = ANC_ACTIVE,
474 wm2000_enter_standby,
478 .source = ANC_ACTIVE,
486 .source = ANC_BYPASS,
494 .source = ANC_BYPASS,
499 wm2000_enter_standby,
503 .source = ANC_BYPASS,
511 .source = ANC_STANDBY,
519 .source = ANC_STANDBY,
528 .source = ANC_STANDBY,
537 static int wm2000_anc_transition(struct wm2000_priv *wm2000,
538 enum wm2000_anc_mode mode)
540 struct i2c_client *i2c = wm2000->i2c;
544 if (wm2000->anc_mode == mode)
547 for (i = 0; i < ARRAY_SIZE(anc_transitions); i++)
548 if (anc_transitions[i].source == wm2000->anc_mode &&
549 anc_transitions[i].dest == mode)
551 if (i == ARRAY_SIZE(anc_transitions)) {
552 dev_err(&i2c->dev, "No transition for %d->%d\n",
553 wm2000->anc_mode, mode);
557 /* Maintain clock while active */
558 if (anc_transitions[i].source == ANC_OFF) {
559 ret = clk_prepare_enable(wm2000->mclk);
561 dev_err(&i2c->dev, "Failed to enable MCLK: %d\n", ret);
566 for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) {
567 if (!anc_transitions[i].step[j])
569 ret = anc_transitions[i].step[j](i2c,
570 anc_transitions[i].analogue);
575 if (anc_transitions[i].dest == ANC_OFF)
576 clk_disable_unprepare(wm2000->mclk);
581 static int wm2000_anc_set_mode(struct wm2000_priv *wm2000)
583 struct i2c_client *i2c = wm2000->i2c;
584 enum wm2000_anc_mode mode;
586 if (wm2000->anc_eng_ena && wm2000->spk_ena)
587 if (wm2000->anc_active)
594 dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n",
595 mode, wm2000->anc_eng_ena, !wm2000->spk_ena,
598 return wm2000_anc_transition(wm2000, mode);
601 static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
602 struct snd_ctl_elem_value *ucontrol)
604 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
605 struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
607 ucontrol->value.integer.value[0] = wm2000->anc_active;
612 static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
613 struct snd_ctl_elem_value *ucontrol)
615 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
616 struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
617 unsigned int anc_active = ucontrol->value.integer.value[0];
623 mutex_lock(&wm2000->lock);
625 wm2000->anc_active = anc_active;
627 ret = wm2000_anc_set_mode(wm2000);
629 mutex_unlock(&wm2000->lock);
634 static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
635 struct snd_ctl_elem_value *ucontrol)
637 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
638 struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
640 ucontrol->value.integer.value[0] = wm2000->spk_ena;
645 static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
646 struct snd_ctl_elem_value *ucontrol)
648 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
649 struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
650 unsigned int val = ucontrol->value.integer.value[0];
656 mutex_lock(&wm2000->lock);
658 wm2000->spk_ena = val;
660 ret = wm2000_anc_set_mode(wm2000);
662 mutex_unlock(&wm2000->lock);
667 static const struct snd_kcontrol_new wm2000_controls[] = {
668 SOC_SINGLE("ANC Volume", WM2000_REG_ANC_GAIN_CTRL, 0, 255, 0),
669 SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0,
671 wm2000_anc_mode_put),
672 SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0,
677 static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
678 struct snd_kcontrol *kcontrol, int event)
680 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
681 struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
684 mutex_lock(&wm2000->lock);
686 if (SND_SOC_DAPM_EVENT_ON(event))
687 wm2000->anc_eng_ena = 1;
689 if (SND_SOC_DAPM_EVENT_OFF(event))
690 wm2000->anc_eng_ena = 0;
692 ret = wm2000_anc_set_mode(wm2000);
694 mutex_unlock(&wm2000->lock);
699 static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
700 /* Externally visible pins */
701 SND_SOC_DAPM_OUTPUT("SPKN"),
702 SND_SOC_DAPM_OUTPUT("SPKP"),
704 SND_SOC_DAPM_INPUT("LINN"),
705 SND_SOC_DAPM_INPUT("LINP"),
707 SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0,
708 wm2000_anc_power_event,
709 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
712 /* Target, Path, Source */
713 static const struct snd_soc_dapm_route wm2000_audio_map[] = {
714 { "SPKN", NULL, "ANC Engine" },
715 { "SPKP", NULL, "ANC Engine" },
716 { "ANC Engine", NULL, "LINN" },
717 { "ANC Engine", NULL, "LINP" },
721 static int wm2000_suspend(struct snd_soc_component *component)
723 struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
725 return wm2000_anc_transition(wm2000, ANC_OFF);
728 static int wm2000_resume(struct snd_soc_component *component)
730 struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
732 return wm2000_anc_set_mode(wm2000);
735 #define wm2000_suspend NULL
736 #define wm2000_resume NULL
739 static bool wm2000_readable_reg(struct device *dev, unsigned int reg)
742 case WM2000_REG_SYS_START:
743 case WM2000_REG_ANC_GAIN_CTRL:
744 case WM2000_REG_MSE_TH1:
745 case WM2000_REG_MSE_TH2:
746 case WM2000_REG_SPEECH_CLARITY:
747 case WM2000_REG_SYS_WATCHDOG:
748 case WM2000_REG_ANA_VMID_PD_TIME:
749 case WM2000_REG_ANA_VMID_PU_TIME:
750 case WM2000_REG_CAT_FLTR_INDX:
751 case WM2000_REG_CAT_GAIN_0:
752 case WM2000_REG_SYS_STATUS:
753 case WM2000_REG_SYS_MODE_CNTRL:
754 case WM2000_REG_SYS_START0:
755 case WM2000_REG_SYS_START1:
758 case WM2000_REG_REVISON:
759 case WM2000_REG_SYS_CTL1:
760 case WM2000_REG_SYS_CTL2:
761 case WM2000_REG_ANC_STAT:
762 case WM2000_REG_IF_CTL:
763 case WM2000_REG_ANA_MIC_CTL:
764 case WM2000_REG_SPK_CTL:
771 static const struct regmap_config wm2000_regmap = {
775 .max_register = WM2000_REG_SPK_CTL,
776 .readable_reg = wm2000_readable_reg,
779 static int wm2000_probe(struct snd_soc_component *component)
781 struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
783 /* This will trigger a transition to standby mode by default */
784 wm2000_anc_set_mode(wm2000);
789 static void wm2000_remove(struct snd_soc_component *component)
791 struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
793 wm2000_anc_transition(wm2000, ANC_OFF);
796 static const struct snd_soc_component_driver soc_component_dev_wm2000 = {
797 .probe = wm2000_probe,
798 .remove = wm2000_remove,
799 .suspend = wm2000_suspend,
800 .resume = wm2000_resume,
801 .controls = wm2000_controls,
802 .num_controls = ARRAY_SIZE(wm2000_controls),
803 .dapm_widgets = wm2000_dapm_widgets,
804 .num_dapm_widgets = ARRAY_SIZE(wm2000_dapm_widgets),
805 .dapm_routes = wm2000_audio_map,
806 .num_dapm_routes = ARRAY_SIZE(wm2000_audio_map),
808 .use_pmdown_time = 1,
810 .non_legacy_dai_naming = 1,
813 static int wm2000_i2c_probe(struct i2c_client *i2c,
814 const struct i2c_device_id *i2c_id)
816 struct wm2000_priv *wm2000;
817 struct wm2000_platform_data *pdata;
818 const char *filename;
819 const struct firmware *fw = NULL;
824 wm2000 = devm_kzalloc(&i2c->dev, sizeof(*wm2000), GFP_KERNEL);
828 mutex_init(&wm2000->lock);
830 dev_set_drvdata(&i2c->dev, wm2000);
832 wm2000->regmap = devm_regmap_init_i2c(i2c, &wm2000_regmap);
833 if (IS_ERR(wm2000->regmap)) {
834 ret = PTR_ERR(wm2000->regmap);
835 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
840 for (i = 0; i < WM2000_NUM_SUPPLIES; i++)
841 wm2000->supplies[i].supply = wm2000_supplies[i];
843 ret = devm_regulator_bulk_get(&i2c->dev, WM2000_NUM_SUPPLIES,
846 dev_err(&i2c->dev, "Failed to get supplies: %d\n", ret);
850 ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies);
852 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
856 /* Verify that this is a WM2000 */
857 ret = regmap_read(wm2000->regmap, WM2000_REG_ID1, ®);
859 dev_err(&i2c->dev, "Unable to read ID1: %d\n", ret);
863 ret = regmap_read(wm2000->regmap, WM2000_REG_ID2, ®);
865 dev_err(&i2c->dev, "Unable to read ID2: %d\n", ret);
871 dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id);
876 ret = regmap_read(wm2000->regmap, WM2000_REG_REVISON, ®);
878 dev_err(&i2c->dev, "Unable to read Revision: %d\n", ret);
881 dev_info(&i2c->dev, "revision %c\n", reg + 'A');
883 wm2000->mclk = devm_clk_get(&i2c->dev, "MCLK");
884 if (IS_ERR(wm2000->mclk)) {
885 ret = PTR_ERR(wm2000->mclk);
886 dev_err(&i2c->dev, "Failed to get MCLK: %d\n", ret);
890 filename = "wm2000_anc.bin";
891 pdata = dev_get_platdata(&i2c->dev);
893 wm2000->speech_clarity = !pdata->speech_enh_disable;
895 if (pdata->download_file)
896 filename = pdata->download_file;
899 ret = request_firmware(&fw, filename, &i2c->dev);
901 dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret);
905 /* Pre-cook the concatenation of the register address onto the image */
906 wm2000->anc_download_size = fw->size + 2;
907 wm2000->anc_download = devm_kzalloc(&i2c->dev,
908 wm2000->anc_download_size,
910 if (wm2000->anc_download == NULL) {
915 wm2000->anc_download[0] = 0x80;
916 wm2000->anc_download[1] = 0x00;
917 memcpy(wm2000->anc_download + 2, fw->data, fw->size);
919 wm2000->anc_eng_ena = 1;
920 wm2000->anc_active = 1;
924 wm2000_reset(wm2000);
926 ret = devm_snd_soc_register_component(&i2c->dev,
927 &soc_component_dev_wm2000, NULL, 0);
930 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
933 release_firmware(fw);
937 static const struct i2c_device_id wm2000_i2c_id[] = {
941 MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id);
943 static struct i2c_driver wm2000_i2c_driver = {
947 .probe = wm2000_i2c_probe,
948 .id_table = wm2000_i2c_id,
951 module_i2c_driver(wm2000_i2c_driver);
953 MODULE_DESCRIPTION("ASoC WM2000 driver");
954 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
955 MODULE_LICENSE("GPL");