ASoC: Move WM2000 to dev_pm_ops
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / codecs / wm2000.c
bloba3b9cbb20ee9bc07ca849fbe4f7af593048da9c8
1 /*
2 * wm2000.c -- WM2000 ALSA Soc Audio driver
4 * Copyright 2008-2010 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/platform_device.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;
56 enum wm2000_anc_mode anc_mode;
58 unsigned int anc_active:1;
59 unsigned int anc_eng_ena:1;
60 unsigned int spk_ena:1;
62 unsigned int mclk_div:1;
63 unsigned int speech_clarity:1;
65 int anc_download_size;
66 char *anc_download;
69 static struct i2c_client *wm2000_i2c;
71 static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
72 unsigned int value)
74 u8 data[3];
75 int ret;
77 data[0] = (reg >> 8) & 0xff;
78 data[1] = reg & 0xff;
79 data[2] = value & 0xff;
81 dev_vdbg(&i2c->dev, "write %x = %x\n", reg, value);
83 ret = i2c_master_send(i2c, data, 3);
84 if (ret == 3)
85 return 0;
86 if (ret < 0)
87 return ret;
88 else
89 return -EIO;
92 static unsigned int wm2000_read(struct i2c_client *i2c, unsigned int r)
94 struct i2c_msg xfer[2];
95 u8 reg[2];
96 u8 data;
97 int ret;
99 /* Write register */
100 reg[0] = (r >> 8) & 0xff;
101 reg[1] = r & 0xff;
102 xfer[0].addr = i2c->addr;
103 xfer[0].flags = 0;
104 xfer[0].len = sizeof(reg);
105 xfer[0].buf = &reg[0];
107 /* Read data */
108 xfer[1].addr = i2c->addr;
109 xfer[1].flags = I2C_M_RD;
110 xfer[1].len = 1;
111 xfer[1].buf = &data;
113 ret = i2c_transfer(i2c->adapter, xfer, 2);
114 if (ret != 2) {
115 dev_err(&i2c->dev, "i2c_transfer() returned %d\n", ret);
116 return 0;
119 dev_vdbg(&i2c->dev, "read %x from %x\n", data, r);
121 return data;
124 static void wm2000_reset(struct wm2000_priv *wm2000)
126 struct i2c_client *i2c = wm2000->i2c;
128 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
129 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
130 wm2000_write(i2c, WM2000_REG_ID1, 0);
132 wm2000->anc_mode = ANC_OFF;
135 static int wm2000_poll_bit(struct i2c_client *i2c,
136 unsigned int reg, u8 mask, int timeout)
138 int val;
140 val = wm2000_read(i2c, reg);
142 while (!(val & mask) && --timeout) {
143 msleep(1);
144 val = wm2000_read(i2c, reg);
147 if (timeout == 0)
148 return 0;
149 else
150 return 1;
153 static int wm2000_power_up(struct i2c_client *i2c, int analogue)
155 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
156 int ret, timeout;
158 BUG_ON(wm2000->anc_mode != ANC_OFF);
160 dev_dbg(&i2c->dev, "Beginning power up\n");
162 if (!wm2000->mclk_div) {
163 dev_dbg(&i2c->dev, "Disabling MCLK divider\n");
164 wm2000_write(i2c, WM2000_REG_SYS_CTL2,
165 WM2000_MCLK_DIV2_ENA_CLR);
166 } else {
167 dev_dbg(&i2c->dev, "Enabling MCLK divider\n");
168 wm2000_write(i2c, WM2000_REG_SYS_CTL2,
169 WM2000_MCLK_DIV2_ENA_SET);
172 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
173 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET);
175 /* Wait for ANC engine to become ready */
176 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
177 WM2000_ANC_ENG_IDLE, 1)) {
178 dev_err(&i2c->dev, "ANC engine failed to reset\n");
179 return -ETIMEDOUT;
182 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
183 WM2000_STATUS_BOOT_COMPLETE, 1)) {
184 dev_err(&i2c->dev, "ANC engine failed to initialise\n");
185 return -ETIMEDOUT;
188 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
190 /* Open code download of the data since it is the only bulk
191 * write we do. */
192 dev_dbg(&i2c->dev, "Downloading %d bytes\n",
193 wm2000->anc_download_size - 2);
195 ret = i2c_master_send(i2c, wm2000->anc_download,
196 wm2000->anc_download_size);
197 if (ret < 0) {
198 dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret);
199 return ret;
201 if (ret != wm2000->anc_download_size) {
202 dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n",
203 ret, wm2000->anc_download_size);
204 return -EIO;
207 dev_dbg(&i2c->dev, "Download complete\n");
209 if (analogue) {
210 timeout = 248;
211 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, timeout / 4);
213 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
214 WM2000_MODE_ANA_SEQ_INCLUDE |
215 WM2000_MODE_MOUSE_ENABLE |
216 WM2000_MODE_THERMAL_ENABLE);
217 } else {
218 timeout = 10;
220 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
221 WM2000_MODE_MOUSE_ENABLE |
222 WM2000_MODE_THERMAL_ENABLE);
225 ret = wm2000_read(i2c, WM2000_REG_SPEECH_CLARITY);
226 if (wm2000->speech_clarity)
227 ret &= ~WM2000_SPEECH_CLARITY;
228 else
229 ret |= WM2000_SPEECH_CLARITY;
230 wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, ret);
232 wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33);
233 wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02);
235 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
237 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
238 WM2000_STATUS_MOUSE_ACTIVE, timeout)) {
239 dev_err(&i2c->dev, "Timed out waiting for device after %dms\n",
240 timeout * 10);
241 return -ETIMEDOUT;
244 dev_dbg(&i2c->dev, "ANC active\n");
245 if (analogue)
246 dev_dbg(&i2c->dev, "Analogue active\n");
247 wm2000->anc_mode = ANC_ACTIVE;
249 return 0;
252 static int wm2000_power_down(struct i2c_client *i2c, int analogue)
254 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
255 int timeout;
257 if (analogue) {
258 timeout = 248;
259 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, timeout / 4);
260 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
261 WM2000_MODE_ANA_SEQ_INCLUDE |
262 WM2000_MODE_POWER_DOWN);
263 } else {
264 timeout = 10;
265 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
266 WM2000_MODE_POWER_DOWN);
269 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
270 WM2000_STATUS_POWER_DOWN_COMPLETE, timeout)) {
271 dev_err(&i2c->dev, "Timeout waiting for ANC power down\n");
272 return -ETIMEDOUT;
275 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
276 WM2000_ANC_ENG_IDLE, 1)) {
277 dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
278 return -ETIMEDOUT;
281 dev_dbg(&i2c->dev, "powered off\n");
282 wm2000->anc_mode = ANC_OFF;
284 return 0;
287 static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue)
289 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
291 BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
293 if (analogue) {
294 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
295 WM2000_MODE_ANA_SEQ_INCLUDE |
296 WM2000_MODE_THERMAL_ENABLE |
297 WM2000_MODE_BYPASS_ENTRY);
298 } else {
299 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
300 WM2000_MODE_THERMAL_ENABLE |
301 WM2000_MODE_BYPASS_ENTRY);
304 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
305 WM2000_STATUS_ANC_DISABLED, 10)) {
306 dev_err(&i2c->dev, "Timeout waiting for ANC disable\n");
307 return -ETIMEDOUT;
310 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
311 WM2000_ANC_ENG_IDLE, 1)) {
312 dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
313 return -ETIMEDOUT;
316 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
317 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
319 wm2000->anc_mode = ANC_BYPASS;
320 dev_dbg(&i2c->dev, "bypass enabled\n");
322 return 0;
325 static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue)
327 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
329 BUG_ON(wm2000->anc_mode != ANC_BYPASS);
331 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
333 if (analogue) {
334 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
335 WM2000_MODE_ANA_SEQ_INCLUDE |
336 WM2000_MODE_MOUSE_ENABLE |
337 WM2000_MODE_THERMAL_ENABLE);
338 } else {
339 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
340 WM2000_MODE_MOUSE_ENABLE |
341 WM2000_MODE_THERMAL_ENABLE);
344 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
345 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
347 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
348 WM2000_STATUS_MOUSE_ACTIVE, 10)) {
349 dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
350 return -ETIMEDOUT;
353 wm2000->anc_mode = ANC_ACTIVE;
354 dev_dbg(&i2c->dev, "MOUSE active\n");
356 return 0;
359 static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
361 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
362 int timeout;
364 BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
366 if (analogue) {
367 timeout = 248;
368 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, timeout / 4);
370 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
371 WM2000_MODE_ANA_SEQ_INCLUDE |
372 WM2000_MODE_THERMAL_ENABLE |
373 WM2000_MODE_STANDBY_ENTRY);
374 } else {
375 timeout = 10;
377 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
378 WM2000_MODE_THERMAL_ENABLE |
379 WM2000_MODE_STANDBY_ENTRY);
382 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
383 WM2000_STATUS_ANC_DISABLED, timeout)) {
384 dev_err(&i2c->dev,
385 "Timed out waiting for ANC disable after 1ms\n");
386 return -ETIMEDOUT;
389 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE,
390 1)) {
391 dev_err(&i2c->dev,
392 "Timed out waiting for standby after %dms\n",
393 timeout * 10);
394 return -ETIMEDOUT;
397 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
398 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
400 wm2000->anc_mode = ANC_STANDBY;
401 dev_dbg(&i2c->dev, "standby\n");
402 if (analogue)
403 dev_dbg(&i2c->dev, "Analogue disabled\n");
405 return 0;
408 static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
410 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
411 int timeout;
413 BUG_ON(wm2000->anc_mode != ANC_STANDBY);
415 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
417 if (analogue) {
418 timeout = 248;
419 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, timeout / 4);
421 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
422 WM2000_MODE_ANA_SEQ_INCLUDE |
423 WM2000_MODE_THERMAL_ENABLE |
424 WM2000_MODE_MOUSE_ENABLE);
425 } else {
426 timeout = 10;
428 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
429 WM2000_MODE_THERMAL_ENABLE |
430 WM2000_MODE_MOUSE_ENABLE);
433 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
434 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
436 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
437 WM2000_STATUS_MOUSE_ACTIVE, timeout)) {
438 dev_err(&i2c->dev, "Timed out waiting for MOUSE after %dms\n",
439 timeout * 10);
440 return -ETIMEDOUT;
443 wm2000->anc_mode = ANC_ACTIVE;
444 dev_dbg(&i2c->dev, "MOUSE active\n");
445 if (analogue)
446 dev_dbg(&i2c->dev, "Analogue enabled\n");
448 return 0;
451 typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
453 static struct {
454 enum wm2000_anc_mode source;
455 enum wm2000_anc_mode dest;
456 int analogue;
457 wm2000_mode_fn step[2];
458 } anc_transitions[] = {
460 .source = ANC_OFF,
461 .dest = ANC_ACTIVE,
462 .analogue = 1,
463 .step = {
464 wm2000_power_up,
468 .source = ANC_OFF,
469 .dest = ANC_STANDBY,
470 .step = {
471 wm2000_power_up,
472 wm2000_enter_standby,
476 .source = ANC_OFF,
477 .dest = ANC_BYPASS,
478 .analogue = 1,
479 .step = {
480 wm2000_power_up,
481 wm2000_enter_bypass,
485 .source = ANC_ACTIVE,
486 .dest = ANC_BYPASS,
487 .analogue = 1,
488 .step = {
489 wm2000_enter_bypass,
493 .source = ANC_ACTIVE,
494 .dest = ANC_STANDBY,
495 .analogue = 1,
496 .step = {
497 wm2000_enter_standby,
501 .source = ANC_ACTIVE,
502 .dest = ANC_OFF,
503 .analogue = 1,
504 .step = {
505 wm2000_power_down,
509 .source = ANC_BYPASS,
510 .dest = ANC_ACTIVE,
511 .analogue = 1,
512 .step = {
513 wm2000_exit_bypass,
517 .source = ANC_BYPASS,
518 .dest = ANC_STANDBY,
519 .analogue = 1,
520 .step = {
521 wm2000_exit_bypass,
522 wm2000_enter_standby,
526 .source = ANC_BYPASS,
527 .dest = ANC_OFF,
528 .step = {
529 wm2000_exit_bypass,
530 wm2000_power_down,
534 .source = ANC_STANDBY,
535 .dest = ANC_ACTIVE,
536 .analogue = 1,
537 .step = {
538 wm2000_exit_standby,
542 .source = ANC_STANDBY,
543 .dest = ANC_BYPASS,
544 .analogue = 1,
545 .step = {
546 wm2000_exit_standby,
547 wm2000_enter_bypass,
551 .source = ANC_STANDBY,
552 .dest = ANC_OFF,
553 .step = {
554 wm2000_exit_standby,
555 wm2000_power_down,
560 static int wm2000_anc_transition(struct wm2000_priv *wm2000,
561 enum wm2000_anc_mode mode)
563 struct i2c_client *i2c = wm2000->i2c;
564 int i, j;
565 int ret;
567 if (wm2000->anc_mode == mode)
568 return 0;
570 for (i = 0; i < ARRAY_SIZE(anc_transitions); i++)
571 if (anc_transitions[i].source == wm2000->anc_mode &&
572 anc_transitions[i].dest == mode)
573 break;
574 if (i == ARRAY_SIZE(anc_transitions)) {
575 dev_err(&i2c->dev, "No transition for %d->%d\n",
576 wm2000->anc_mode, mode);
577 return -EINVAL;
580 for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) {
581 if (!anc_transitions[i].step[j])
582 break;
583 ret = anc_transitions[i].step[j](i2c,
584 anc_transitions[i].analogue);
585 if (ret != 0)
586 return ret;
589 return 0;
592 static int wm2000_anc_set_mode(struct wm2000_priv *wm2000)
594 struct i2c_client *i2c = wm2000->i2c;
595 enum wm2000_anc_mode mode;
597 if (wm2000->anc_eng_ena && wm2000->spk_ena)
598 if (wm2000->anc_active)
599 mode = ANC_ACTIVE;
600 else
601 mode = ANC_BYPASS;
602 else
603 mode = ANC_STANDBY;
605 dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n",
606 mode, wm2000->anc_eng_ena, !wm2000->spk_ena,
607 wm2000->anc_active);
609 return wm2000_anc_transition(wm2000, mode);
612 static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
613 struct snd_ctl_elem_value *ucontrol)
615 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
617 ucontrol->value.enumerated.item[0] = wm2000->anc_active;
619 return 0;
622 static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
623 struct snd_ctl_elem_value *ucontrol)
625 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
626 int anc_active = ucontrol->value.enumerated.item[0];
628 if (anc_active > 1)
629 return -EINVAL;
631 wm2000->anc_active = anc_active;
633 return wm2000_anc_set_mode(wm2000);
636 static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
637 struct snd_ctl_elem_value *ucontrol)
639 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
641 ucontrol->value.enumerated.item[0] = wm2000->spk_ena;
643 return 0;
646 static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
647 struct snd_ctl_elem_value *ucontrol)
649 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
650 int val = ucontrol->value.enumerated.item[0];
652 if (val > 1)
653 return -EINVAL;
655 wm2000->spk_ena = val;
657 return wm2000_anc_set_mode(wm2000);
660 static const struct snd_kcontrol_new wm2000_controls[] = {
661 SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0,
662 wm2000_anc_mode_get,
663 wm2000_anc_mode_put),
664 SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0,
665 wm2000_speaker_get,
666 wm2000_speaker_put),
669 static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
670 struct snd_kcontrol *kcontrol, int event)
672 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
674 if (SND_SOC_DAPM_EVENT_ON(event))
675 wm2000->anc_eng_ena = 1;
677 if (SND_SOC_DAPM_EVENT_OFF(event))
678 wm2000->anc_eng_ena = 0;
680 return wm2000_anc_set_mode(wm2000);
683 static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
684 /* Externally visible pins */
685 SND_SOC_DAPM_OUTPUT("WM2000 SPKN"),
686 SND_SOC_DAPM_OUTPUT("WM2000 SPKP"),
688 SND_SOC_DAPM_INPUT("WM2000 LINN"),
689 SND_SOC_DAPM_INPUT("WM2000 LINP"),
691 SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0,
692 wm2000_anc_power_event,
693 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
696 /* Target, Path, Source */
697 static const struct snd_soc_dapm_route audio_map[] = {
698 { "WM2000 SPKN", NULL, "ANC Engine" },
699 { "WM2000 SPKP", NULL, "ANC Engine" },
700 { "ANC Engine", NULL, "WM2000 LINN" },
701 { "ANC Engine", NULL, "WM2000 LINP" },
704 /* Called from the machine driver */
705 int wm2000_add_controls(struct snd_soc_codec *codec)
707 struct snd_soc_dapm_context *dapm = &codec->dapm;
708 int ret;
710 if (!wm2000_i2c) {
711 pr_err("WM2000 not yet probed\n");
712 return -ENODEV;
715 ret = snd_soc_dapm_new_controls(dapm, wm2000_dapm_widgets,
716 ARRAY_SIZE(wm2000_dapm_widgets));
717 if (ret < 0)
718 return ret;
720 ret = snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
721 if (ret < 0)
722 return ret;
724 return snd_soc_add_controls(codec, wm2000_controls,
725 ARRAY_SIZE(wm2000_controls));
727 EXPORT_SYMBOL_GPL(wm2000_add_controls);
729 static int __devinit wm2000_i2c_probe(struct i2c_client *i2c,
730 const struct i2c_device_id *i2c_id)
732 struct wm2000_priv *wm2000;
733 struct wm2000_platform_data *pdata;
734 const char *filename;
735 const struct firmware *fw;
736 int reg, ret;
737 u16 id;
739 if (wm2000_i2c) {
740 dev_err(&i2c->dev, "Another WM2000 is already registered\n");
741 return -EINVAL;
744 wm2000 = kzalloc(sizeof(struct wm2000_priv), GFP_KERNEL);
745 if (wm2000 == NULL) {
746 dev_err(&i2c->dev, "Unable to allocate private data\n");
747 return -ENOMEM;
750 /* Verify that this is a WM2000 */
751 reg = wm2000_read(i2c, WM2000_REG_ID1);
752 id = reg << 8;
753 reg = wm2000_read(i2c, WM2000_REG_ID2);
754 id |= reg & 0xff;
756 if (id != 0x2000) {
757 dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id);
758 ret = -ENODEV;
759 goto err;
762 reg = wm2000_read(i2c, WM2000_REG_REVISON);
763 dev_info(&i2c->dev, "revision %c\n", reg + 'A');
765 filename = "wm2000_anc.bin";
766 pdata = dev_get_platdata(&i2c->dev);
767 if (pdata) {
768 wm2000->mclk_div = pdata->mclkdiv2;
769 wm2000->speech_clarity = !pdata->speech_enh_disable;
771 if (pdata->download_file)
772 filename = pdata->download_file;
775 ret = request_firmware(&fw, filename, &i2c->dev);
776 if (ret != 0) {
777 dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret);
778 goto err;
781 /* Pre-cook the concatenation of the register address onto the image */
782 wm2000->anc_download_size = fw->size + 2;
783 wm2000->anc_download = kmalloc(wm2000->anc_download_size, GFP_KERNEL);
784 if (wm2000->anc_download == NULL) {
785 dev_err(&i2c->dev, "Out of memory\n");
786 ret = -ENOMEM;
787 goto err_fw;
790 wm2000->anc_download[0] = 0x80;
791 wm2000->anc_download[1] = 0x00;
792 memcpy(wm2000->anc_download + 2, fw->data, fw->size);
794 release_firmware(fw);
796 dev_set_drvdata(&i2c->dev, wm2000);
797 wm2000->anc_eng_ena = 1;
798 wm2000->anc_active = 1;
799 wm2000->spk_ena = 1;
800 wm2000->i2c = i2c;
802 wm2000_reset(wm2000);
804 /* This will trigger a transition to standby mode by default */
805 wm2000_anc_set_mode(wm2000);
807 wm2000_i2c = i2c;
809 return 0;
811 err_fw:
812 release_firmware(fw);
813 err:
814 kfree(wm2000);
815 return ret;
818 static __devexit int wm2000_i2c_remove(struct i2c_client *i2c)
820 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
822 wm2000_anc_transition(wm2000, ANC_OFF);
824 wm2000_i2c = NULL;
825 kfree(wm2000->anc_download);
826 kfree(wm2000);
828 return 0;
831 static void wm2000_i2c_shutdown(struct i2c_client *i2c)
833 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
835 wm2000_anc_transition(wm2000, ANC_OFF);
838 #ifdef CONFIG_PM
839 static int wm2000_i2c_suspend(struct device *dev)
841 struct i2c_client *i2c = to_i2c_client(dev);
842 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
844 return wm2000_anc_transition(wm2000, ANC_OFF);
847 static int wm2000_i2c_resume(struct device *dev)
849 struct i2c_client *i2c = to_i2c_client(dev);
850 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
852 return wm2000_anc_set_mode(wm2000);
854 #endif
856 static SIMPLE_DEV_PM_OPS(wm2000_pm, wm2000_i2c_suspend, wm2000_i2c_resume);
858 static const struct i2c_device_id wm2000_i2c_id[] = {
859 { "wm2000", 0 },
862 MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id);
864 static struct i2c_driver wm2000_i2c_driver = {
865 .driver = {
866 .name = "wm2000",
867 .owner = THIS_MODULE,
868 .pm = &wm2000_pm,
870 .probe = wm2000_i2c_probe,
871 .remove = __devexit_p(wm2000_i2c_remove),
872 .shutdown = wm2000_i2c_shutdown,
873 .id_table = wm2000_i2c_id,
876 static int __init wm2000_init(void)
878 return i2c_add_driver(&wm2000_i2c_driver);
880 module_init(wm2000_init);
882 static void __exit wm2000_exit(void)
884 i2c_del_driver(&wm2000_i2c_driver);
886 module_exit(wm2000_exit);
888 MODULE_DESCRIPTION("ASoC WM2000 driver");
889 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
890 MODULE_LICENSE("GPL");