Revert "microblaze_mmu_v2: Update signal returning address"
[linux/fpc-iii.git] / sound / soc / codecs / wm2000.c
bloba3acb7a85f6ab26f62e92489e75e0f6b4822f831
1 /*
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 callibration 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/delay.h>
30 #include <linux/pm.h>
31 #include <linux/i2c.h>
32 #include <linux/regmap.h>
33 #include <linux/debugfs.h>
34 #include <linux/slab.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/initval.h>
40 #include <sound/tlv.h>
42 #include <sound/wm2000.h>
44 #include "wm2000.h"
46 enum wm2000_anc_mode {
47 ANC_ACTIVE = 0,
48 ANC_BYPASS = 1,
49 ANC_STANDBY = 2,
50 ANC_OFF = 3,
53 struct wm2000_priv {
54 struct i2c_client *i2c;
55 struct regmap *regmap;
57 enum wm2000_anc_mode anc_mode;
59 unsigned int anc_active:1;
60 unsigned int anc_eng_ena:1;
61 unsigned int spk_ena:1;
63 unsigned int mclk_div:1;
64 unsigned int speech_clarity:1;
66 int anc_download_size;
67 char *anc_download;
70 static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
71 unsigned int value)
73 struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
74 return regmap_write(wm2000->regmap, reg, value);
77 static unsigned int wm2000_read(struct i2c_client *i2c, unsigned int r)
79 struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
80 unsigned int val;
81 int ret;
83 ret = regmap_read(wm2000->regmap, r, &val);
84 if (ret < 0)
85 return -1;
87 return val;
90 static void wm2000_reset(struct wm2000_priv *wm2000)
92 struct i2c_client *i2c = wm2000->i2c;
94 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
95 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
96 wm2000_write(i2c, WM2000_REG_ID1, 0);
98 wm2000->anc_mode = ANC_OFF;
101 static int wm2000_poll_bit(struct i2c_client *i2c,
102 unsigned int reg, u8 mask)
104 int timeout = 4000;
105 int val;
107 val = wm2000_read(i2c, reg);
109 while (!(val & mask) && --timeout) {
110 msleep(1);
111 val = wm2000_read(i2c, reg);
114 if (timeout == 0)
115 return 0;
116 else
117 return 1;
120 static int wm2000_power_up(struct i2c_client *i2c, int analogue)
122 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
123 int ret;
125 BUG_ON(wm2000->anc_mode != ANC_OFF);
127 dev_dbg(&i2c->dev, "Beginning power up\n");
129 if (!wm2000->mclk_div) {
130 dev_dbg(&i2c->dev, "Disabling MCLK divider\n");
131 wm2000_write(i2c, WM2000_REG_SYS_CTL2,
132 WM2000_MCLK_DIV2_ENA_CLR);
133 } else {
134 dev_dbg(&i2c->dev, "Enabling MCLK divider\n");
135 wm2000_write(i2c, WM2000_REG_SYS_CTL2,
136 WM2000_MCLK_DIV2_ENA_SET);
139 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
140 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET);
142 /* Wait for ANC engine to become ready */
143 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
144 WM2000_ANC_ENG_IDLE)) {
145 dev_err(&i2c->dev, "ANC engine failed to reset\n");
146 return -ETIMEDOUT;
149 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
150 WM2000_STATUS_BOOT_COMPLETE)) {
151 dev_err(&i2c->dev, "ANC engine failed to initialise\n");
152 return -ETIMEDOUT;
155 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
157 /* Open code download of the data since it is the only bulk
158 * write we do. */
159 dev_dbg(&i2c->dev, "Downloading %d bytes\n",
160 wm2000->anc_download_size - 2);
162 ret = i2c_master_send(i2c, wm2000->anc_download,
163 wm2000->anc_download_size);
164 if (ret < 0) {
165 dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret);
166 return ret;
168 if (ret != wm2000->anc_download_size) {
169 dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n",
170 ret, wm2000->anc_download_size);
171 return -EIO;
174 dev_dbg(&i2c->dev, "Download complete\n");
176 if (analogue) {
177 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
179 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
180 WM2000_MODE_ANA_SEQ_INCLUDE |
181 WM2000_MODE_MOUSE_ENABLE |
182 WM2000_MODE_THERMAL_ENABLE);
183 } else {
184 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
185 WM2000_MODE_MOUSE_ENABLE |
186 WM2000_MODE_THERMAL_ENABLE);
189 ret = wm2000_read(i2c, WM2000_REG_SPEECH_CLARITY);
190 if (wm2000->speech_clarity)
191 ret &= ~WM2000_SPEECH_CLARITY;
192 else
193 ret |= WM2000_SPEECH_CLARITY;
194 wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, ret);
196 wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33);
197 wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02);
199 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
201 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
202 WM2000_STATUS_MOUSE_ACTIVE)) {
203 dev_err(&i2c->dev, "Timed out waiting for device\n");
204 return -ETIMEDOUT;
207 dev_dbg(&i2c->dev, "ANC active\n");
208 if (analogue)
209 dev_dbg(&i2c->dev, "Analogue active\n");
210 wm2000->anc_mode = ANC_ACTIVE;
212 return 0;
215 static int wm2000_power_down(struct i2c_client *i2c, int analogue)
217 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
219 if (analogue) {
220 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
221 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
222 WM2000_MODE_ANA_SEQ_INCLUDE |
223 WM2000_MODE_POWER_DOWN);
224 } else {
225 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
226 WM2000_MODE_POWER_DOWN);
229 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
230 WM2000_STATUS_POWER_DOWN_COMPLETE)) {
231 dev_err(&i2c->dev, "Timeout waiting for ANC power down\n");
232 return -ETIMEDOUT;
235 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
236 WM2000_ANC_ENG_IDLE)) {
237 dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
238 return -ETIMEDOUT;
241 dev_dbg(&i2c->dev, "powered off\n");
242 wm2000->anc_mode = ANC_OFF;
244 return 0;
247 static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue)
249 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
251 BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
253 if (analogue) {
254 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
255 WM2000_MODE_ANA_SEQ_INCLUDE |
256 WM2000_MODE_THERMAL_ENABLE |
257 WM2000_MODE_BYPASS_ENTRY);
258 } else {
259 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
260 WM2000_MODE_THERMAL_ENABLE |
261 WM2000_MODE_BYPASS_ENTRY);
264 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
265 WM2000_STATUS_ANC_DISABLED)) {
266 dev_err(&i2c->dev, "Timeout waiting for ANC disable\n");
267 return -ETIMEDOUT;
270 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
271 WM2000_ANC_ENG_IDLE)) {
272 dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
273 return -ETIMEDOUT;
276 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
277 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
279 wm2000->anc_mode = ANC_BYPASS;
280 dev_dbg(&i2c->dev, "bypass enabled\n");
282 return 0;
285 static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue)
287 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
289 BUG_ON(wm2000->anc_mode != ANC_BYPASS);
291 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
293 if (analogue) {
294 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
295 WM2000_MODE_ANA_SEQ_INCLUDE |
296 WM2000_MODE_MOUSE_ENABLE |
297 WM2000_MODE_THERMAL_ENABLE);
298 } else {
299 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
300 WM2000_MODE_MOUSE_ENABLE |
301 WM2000_MODE_THERMAL_ENABLE);
304 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
305 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
307 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
308 WM2000_STATUS_MOUSE_ACTIVE)) {
309 dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
310 return -ETIMEDOUT;
313 wm2000->anc_mode = ANC_ACTIVE;
314 dev_dbg(&i2c->dev, "MOUSE active\n");
316 return 0;
319 static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
321 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
323 BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
325 if (analogue) {
326 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
328 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
329 WM2000_MODE_ANA_SEQ_INCLUDE |
330 WM2000_MODE_THERMAL_ENABLE |
331 WM2000_MODE_STANDBY_ENTRY);
332 } else {
333 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
334 WM2000_MODE_THERMAL_ENABLE |
335 WM2000_MODE_STANDBY_ENTRY);
338 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
339 WM2000_STATUS_ANC_DISABLED)) {
340 dev_err(&i2c->dev,
341 "Timed out waiting for ANC disable after 1ms\n");
342 return -ETIMEDOUT;
345 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE)) {
346 dev_err(&i2c->dev,
347 "Timed out waiting for standby\n");
348 return -ETIMEDOUT;
351 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
352 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
354 wm2000->anc_mode = ANC_STANDBY;
355 dev_dbg(&i2c->dev, "standby\n");
356 if (analogue)
357 dev_dbg(&i2c->dev, "Analogue disabled\n");
359 return 0;
362 static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
364 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
366 BUG_ON(wm2000->anc_mode != ANC_STANDBY);
368 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
370 if (analogue) {
371 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
373 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
374 WM2000_MODE_ANA_SEQ_INCLUDE |
375 WM2000_MODE_THERMAL_ENABLE |
376 WM2000_MODE_MOUSE_ENABLE);
377 } else {
378 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
379 WM2000_MODE_THERMAL_ENABLE |
380 WM2000_MODE_MOUSE_ENABLE);
383 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
384 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
386 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
387 WM2000_STATUS_MOUSE_ACTIVE)) {
388 dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
389 return -ETIMEDOUT;
392 wm2000->anc_mode = ANC_ACTIVE;
393 dev_dbg(&i2c->dev, "MOUSE active\n");
394 if (analogue)
395 dev_dbg(&i2c->dev, "Analogue enabled\n");
397 return 0;
400 typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
402 static struct {
403 enum wm2000_anc_mode source;
404 enum wm2000_anc_mode dest;
405 int analogue;
406 wm2000_mode_fn step[2];
407 } anc_transitions[] = {
409 .source = ANC_OFF,
410 .dest = ANC_ACTIVE,
411 .analogue = 1,
412 .step = {
413 wm2000_power_up,
417 .source = ANC_OFF,
418 .dest = ANC_STANDBY,
419 .step = {
420 wm2000_power_up,
421 wm2000_enter_standby,
425 .source = ANC_OFF,
426 .dest = ANC_BYPASS,
427 .analogue = 1,
428 .step = {
429 wm2000_power_up,
430 wm2000_enter_bypass,
434 .source = ANC_ACTIVE,
435 .dest = ANC_BYPASS,
436 .analogue = 1,
437 .step = {
438 wm2000_enter_bypass,
442 .source = ANC_ACTIVE,
443 .dest = ANC_STANDBY,
444 .analogue = 1,
445 .step = {
446 wm2000_enter_standby,
450 .source = ANC_ACTIVE,
451 .dest = ANC_OFF,
452 .analogue = 1,
453 .step = {
454 wm2000_power_down,
458 .source = ANC_BYPASS,
459 .dest = ANC_ACTIVE,
460 .analogue = 1,
461 .step = {
462 wm2000_exit_bypass,
466 .source = ANC_BYPASS,
467 .dest = ANC_STANDBY,
468 .analogue = 1,
469 .step = {
470 wm2000_exit_bypass,
471 wm2000_enter_standby,
475 .source = ANC_BYPASS,
476 .dest = ANC_OFF,
477 .step = {
478 wm2000_exit_bypass,
479 wm2000_power_down,
483 .source = ANC_STANDBY,
484 .dest = ANC_ACTIVE,
485 .analogue = 1,
486 .step = {
487 wm2000_exit_standby,
491 .source = ANC_STANDBY,
492 .dest = ANC_BYPASS,
493 .analogue = 1,
494 .step = {
495 wm2000_exit_standby,
496 wm2000_enter_bypass,
500 .source = ANC_STANDBY,
501 .dest = ANC_OFF,
502 .step = {
503 wm2000_exit_standby,
504 wm2000_power_down,
509 static int wm2000_anc_transition(struct wm2000_priv *wm2000,
510 enum wm2000_anc_mode mode)
512 struct i2c_client *i2c = wm2000->i2c;
513 int i, j;
514 int ret;
516 if (wm2000->anc_mode == mode)
517 return 0;
519 for (i = 0; i < ARRAY_SIZE(anc_transitions); i++)
520 if (anc_transitions[i].source == wm2000->anc_mode &&
521 anc_transitions[i].dest == mode)
522 break;
523 if (i == ARRAY_SIZE(anc_transitions)) {
524 dev_err(&i2c->dev, "No transition for %d->%d\n",
525 wm2000->anc_mode, mode);
526 return -EINVAL;
529 for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) {
530 if (!anc_transitions[i].step[j])
531 break;
532 ret = anc_transitions[i].step[j](i2c,
533 anc_transitions[i].analogue);
534 if (ret != 0)
535 return ret;
538 return 0;
541 static int wm2000_anc_set_mode(struct wm2000_priv *wm2000)
543 struct i2c_client *i2c = wm2000->i2c;
544 enum wm2000_anc_mode mode;
546 if (wm2000->anc_eng_ena && wm2000->spk_ena)
547 if (wm2000->anc_active)
548 mode = ANC_ACTIVE;
549 else
550 mode = ANC_BYPASS;
551 else
552 mode = ANC_STANDBY;
554 dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n",
555 mode, wm2000->anc_eng_ena, !wm2000->spk_ena,
556 wm2000->anc_active);
558 return wm2000_anc_transition(wm2000, mode);
561 static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
562 struct snd_ctl_elem_value *ucontrol)
564 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
565 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
567 ucontrol->value.enumerated.item[0] = wm2000->anc_active;
569 return 0;
572 static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
573 struct snd_ctl_elem_value *ucontrol)
575 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
576 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
577 int anc_active = ucontrol->value.enumerated.item[0];
579 if (anc_active > 1)
580 return -EINVAL;
582 wm2000->anc_active = anc_active;
584 return wm2000_anc_set_mode(wm2000);
587 static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
588 struct snd_ctl_elem_value *ucontrol)
590 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
591 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
593 ucontrol->value.enumerated.item[0] = wm2000->spk_ena;
595 return 0;
598 static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
599 struct snd_ctl_elem_value *ucontrol)
601 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
602 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
603 int val = ucontrol->value.enumerated.item[0];
605 if (val > 1)
606 return -EINVAL;
608 wm2000->spk_ena = val;
610 return wm2000_anc_set_mode(wm2000);
613 static const struct snd_kcontrol_new wm2000_controls[] = {
614 SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0,
615 wm2000_anc_mode_get,
616 wm2000_anc_mode_put),
617 SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0,
618 wm2000_speaker_get,
619 wm2000_speaker_put),
622 static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
623 struct snd_kcontrol *kcontrol, int event)
625 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
626 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
628 if (SND_SOC_DAPM_EVENT_ON(event))
629 wm2000->anc_eng_ena = 1;
631 if (SND_SOC_DAPM_EVENT_OFF(event))
632 wm2000->anc_eng_ena = 0;
634 return wm2000_anc_set_mode(wm2000);
637 static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
638 /* Externally visible pins */
639 SND_SOC_DAPM_OUTPUT("SPKN"),
640 SND_SOC_DAPM_OUTPUT("SPKP"),
642 SND_SOC_DAPM_INPUT("LINN"),
643 SND_SOC_DAPM_INPUT("LINP"),
645 SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0,
646 wm2000_anc_power_event,
647 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
650 /* Target, Path, Source */
651 static const struct snd_soc_dapm_route wm2000_audio_map[] = {
652 { "SPKN", NULL, "ANC Engine" },
653 { "SPKP", NULL, "ANC Engine" },
654 { "ANC Engine", NULL, "LINN" },
655 { "ANC Engine", NULL, "LINP" },
658 #ifdef CONFIG_PM
659 static int wm2000_suspend(struct snd_soc_codec *codec)
661 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
663 return wm2000_anc_transition(wm2000, ANC_OFF);
666 static int wm2000_resume(struct snd_soc_codec *codec)
668 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
670 return wm2000_anc_set_mode(wm2000);
672 #else
673 #define wm2000_suspend NULL
674 #define wm2000_resume NULL
675 #endif
677 static bool wm2000_readable_reg(struct device *dev, unsigned int reg)
679 switch (reg) {
680 case WM2000_REG_SYS_START:
681 case WM2000_REG_SPEECH_CLARITY:
682 case WM2000_REG_SYS_WATCHDOG:
683 case WM2000_REG_ANA_VMID_PD_TIME:
684 case WM2000_REG_ANA_VMID_PU_TIME:
685 case WM2000_REG_CAT_FLTR_INDX:
686 case WM2000_REG_CAT_GAIN_0:
687 case WM2000_REG_SYS_STATUS:
688 case WM2000_REG_SYS_MODE_CNTRL:
689 case WM2000_REG_SYS_START0:
690 case WM2000_REG_SYS_START1:
691 case WM2000_REG_ID1:
692 case WM2000_REG_ID2:
693 case WM2000_REG_REVISON:
694 case WM2000_REG_SYS_CTL1:
695 case WM2000_REG_SYS_CTL2:
696 case WM2000_REG_ANC_STAT:
697 case WM2000_REG_IF_CTL:
698 return true;
699 default:
700 return false;
704 static const struct regmap_config wm2000_regmap = {
705 .reg_bits = 16,
706 .val_bits = 8,
708 .max_register = WM2000_REG_IF_CTL,
709 .readable_reg = wm2000_readable_reg,
712 static int wm2000_probe(struct snd_soc_codec *codec)
714 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
716 /* This will trigger a transition to standby mode by default */
717 wm2000_anc_set_mode(wm2000);
719 return 0;
722 static int wm2000_remove(struct snd_soc_codec *codec)
724 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
726 return wm2000_anc_transition(wm2000, ANC_OFF);
729 static struct snd_soc_codec_driver soc_codec_dev_wm2000 = {
730 .probe = wm2000_probe,
731 .remove = wm2000_remove,
732 .suspend = wm2000_suspend,
733 .resume = wm2000_resume,
735 .dapm_widgets = wm2000_dapm_widgets,
736 .num_dapm_widgets = ARRAY_SIZE(wm2000_dapm_widgets),
737 .dapm_routes = wm2000_audio_map,
738 .num_dapm_routes = ARRAY_SIZE(wm2000_audio_map),
739 .controls = wm2000_controls,
740 .num_controls = ARRAY_SIZE(wm2000_controls),
743 static int __devinit wm2000_i2c_probe(struct i2c_client *i2c,
744 const struct i2c_device_id *i2c_id)
746 struct wm2000_priv *wm2000;
747 struct wm2000_platform_data *pdata;
748 const char *filename;
749 const struct firmware *fw = NULL;
750 int ret;
751 int reg;
752 u16 id;
754 wm2000 = devm_kzalloc(&i2c->dev, sizeof(struct wm2000_priv),
755 GFP_KERNEL);
756 if (wm2000 == NULL) {
757 dev_err(&i2c->dev, "Unable to allocate private data\n");
758 return -ENOMEM;
761 dev_set_drvdata(&i2c->dev, wm2000);
763 wm2000->regmap = regmap_init_i2c(i2c, &wm2000_regmap);
764 if (IS_ERR(wm2000->regmap)) {
765 ret = PTR_ERR(wm2000->regmap);
766 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
767 ret);
768 goto out;
771 /* Verify that this is a WM2000 */
772 reg = wm2000_read(i2c, WM2000_REG_ID1);
773 id = reg << 8;
774 reg = wm2000_read(i2c, WM2000_REG_ID2);
775 id |= reg & 0xff;
777 if (id != 0x2000) {
778 dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id);
779 ret = -ENODEV;
780 goto out_regmap_exit;
783 reg = wm2000_read(i2c, WM2000_REG_REVISON);
784 dev_info(&i2c->dev, "revision %c\n", reg + 'A');
786 filename = "wm2000_anc.bin";
787 pdata = dev_get_platdata(&i2c->dev);
788 if (pdata) {
789 wm2000->mclk_div = pdata->mclkdiv2;
790 wm2000->speech_clarity = !pdata->speech_enh_disable;
792 if (pdata->download_file)
793 filename = pdata->download_file;
796 ret = request_firmware(&fw, filename, &i2c->dev);
797 if (ret != 0) {
798 dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret);
799 goto out_regmap_exit;
802 /* Pre-cook the concatenation of the register address onto the image */
803 wm2000->anc_download_size = fw->size + 2;
804 wm2000->anc_download = devm_kzalloc(&i2c->dev,
805 wm2000->anc_download_size,
806 GFP_KERNEL);
807 if (wm2000->anc_download == NULL) {
808 dev_err(&i2c->dev, "Out of memory\n");
809 ret = -ENOMEM;
810 goto out_regmap_exit;
813 wm2000->anc_download[0] = 0x80;
814 wm2000->anc_download[1] = 0x00;
815 memcpy(wm2000->anc_download + 2, fw->data, fw->size);
817 wm2000->anc_eng_ena = 1;
818 wm2000->anc_active = 1;
819 wm2000->spk_ena = 1;
820 wm2000->i2c = i2c;
822 wm2000_reset(wm2000);
824 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_wm2000, NULL, 0);
825 if (!ret)
826 goto out;
828 out_regmap_exit:
829 regmap_exit(wm2000->regmap);
830 out:
831 release_firmware(fw);
832 return ret;
835 static __devexit int wm2000_i2c_remove(struct i2c_client *i2c)
837 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
839 snd_soc_unregister_codec(&i2c->dev);
840 regmap_exit(wm2000->regmap);
842 return 0;
845 static const struct i2c_device_id wm2000_i2c_id[] = {
846 { "wm2000", 0 },
849 MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id);
851 static struct i2c_driver wm2000_i2c_driver = {
852 .driver = {
853 .name = "wm2000",
854 .owner = THIS_MODULE,
856 .probe = wm2000_i2c_probe,
857 .remove = __devexit_p(wm2000_i2c_remove),
858 .id_table = wm2000_i2c_id,
861 static int __init wm2000_init(void)
863 return i2c_add_driver(&wm2000_i2c_driver);
865 module_init(wm2000_init);
867 static void __exit wm2000_exit(void)
869 i2c_del_driver(&wm2000_i2c_driver);
871 module_exit(wm2000_exit);
873 MODULE_DESCRIPTION("ASoC WM2000 driver");
874 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
875 MODULE_LICENSE("GPL");