ASoC: Don't resync WM8903 register cache on reset
[deliverable/linux.git] / sound / soc / codecs / wm2000.c
CommitLineData
3a66d387
MB
1/*
2 * wm2000.c -- WM2000 ALSA Soc Audio driver
3 *
4 * Copyright 2008-2010 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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.
11 *
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:
19 *
20 * perl -e 'while (<>) { s/[\r\n]+// ; printf("%c", hex($_)); }'
21 * < file > wm2000_anc.bin
22 */
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
3a66d387
MB
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>
3a66d387 32#include <linux/debugfs.h>
5a0e3ad6 33#include <linux/slab.h>
3a66d387
MB
34#include <sound/core.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/soc.h>
3a66d387
MB
38#include <sound/initval.h>
39#include <sound/tlv.h>
40
41#include <sound/wm2000.h>
42
43#include "wm2000.h"
44
45enum wm2000_anc_mode {
46 ANC_ACTIVE = 0,
47 ANC_BYPASS = 1,
48 ANC_STANDBY = 2,
49 ANC_OFF = 3,
50};
51
52struct wm2000_priv {
53 struct i2c_client *i2c;
54
55 enum wm2000_anc_mode anc_mode;
56
57 unsigned int anc_active:1;
58 unsigned int anc_eng_ena:1;
59 unsigned int spk_ena:1;
60
61 unsigned int mclk_div:1;
62 unsigned int speech_clarity:1;
63
64 int anc_download_size;
65 char *anc_download;
66};
67
68static struct i2c_client *wm2000_i2c;
69
70static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
71 unsigned int value)
72{
73 u8 data[3];
74 int ret;
75
76 data[0] = (reg >> 8) & 0xff;
77 data[1] = reg & 0xff;
78 data[2] = value & 0xff;
79
80 dev_vdbg(&i2c->dev, "write %x = %x\n", reg, value);
81
82 ret = i2c_master_send(i2c, data, 3);
83 if (ret == 3)
84 return 0;
85 if (ret < 0)
86 return ret;
87 else
88 return -EIO;
89}
90
91static unsigned int wm2000_read(struct i2c_client *i2c, unsigned int r)
92{
93 struct i2c_msg xfer[2];
94 u8 reg[2];
95 u8 data;
96 int ret;
97
98 /* Write register */
99 reg[0] = (r >> 8) & 0xff;
100 reg[1] = r & 0xff;
101 xfer[0].addr = i2c->addr;
102 xfer[0].flags = 0;
103 xfer[0].len = sizeof(reg);
104 xfer[0].buf = &reg[0];
105
106 /* Read data */
107 xfer[1].addr = i2c->addr;
108 xfer[1].flags = I2C_M_RD;
109 xfer[1].len = 1;
110 xfer[1].buf = &data;
111
112 ret = i2c_transfer(i2c->adapter, xfer, 2);
113 if (ret != 2) {
114 dev_err(&i2c->dev, "i2c_transfer() returned %d\n", ret);
115 return 0;
116 }
117
118 dev_vdbg(&i2c->dev, "read %x from %x\n", data, r);
119
120 return data;
121}
122
123static void wm2000_reset(struct wm2000_priv *wm2000)
124{
125 struct i2c_client *i2c = wm2000->i2c;
126
127 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
128 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
129 wm2000_write(i2c, WM2000_REG_ID1, 0);
130
131 wm2000->anc_mode = ANC_OFF;
132}
133
134static int wm2000_poll_bit(struct i2c_client *i2c,
135 unsigned int reg, u8 mask, int timeout)
136{
137 int val;
138
139 val = wm2000_read(i2c, reg);
140
141 while (!(val & mask) && --timeout) {
142 msleep(1);
143 val = wm2000_read(i2c, reg);
144 }
145
146 if (timeout == 0)
147 return 0;
148 else
149 return 1;
150}
151
152static int wm2000_power_up(struct i2c_client *i2c, int analogue)
153{
154 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
155 int ret, timeout;
156
157 BUG_ON(wm2000->anc_mode != ANC_OFF);
158
159 dev_dbg(&i2c->dev, "Beginning power up\n");
160
161 if (!wm2000->mclk_div) {
162 dev_dbg(&i2c->dev, "Disabling MCLK divider\n");
163 wm2000_write(i2c, WM2000_REG_SYS_CTL2,
164 WM2000_MCLK_DIV2_ENA_CLR);
165 } else {
166 dev_dbg(&i2c->dev, "Enabling MCLK divider\n");
167 wm2000_write(i2c, WM2000_REG_SYS_CTL2,
168 WM2000_MCLK_DIV2_ENA_SET);
169 }
170
171 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
172 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET);
173
174 /* Wait for ANC engine to become ready */
175 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
176 WM2000_ANC_ENG_IDLE, 1)) {
177 dev_err(&i2c->dev, "ANC engine failed to reset\n");
178 return -ETIMEDOUT;
179 }
180
181 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
182 WM2000_STATUS_BOOT_COMPLETE, 1)) {
183 dev_err(&i2c->dev, "ANC engine failed to initialise\n");
184 return -ETIMEDOUT;
185 }
186
187 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
188
189 /* Open code download of the data since it is the only bulk
190 * write we do. */
191 dev_dbg(&i2c->dev, "Downloading %d bytes\n",
192 wm2000->anc_download_size - 2);
193
194 ret = i2c_master_send(i2c, wm2000->anc_download,
195 wm2000->anc_download_size);
196 if (ret < 0) {
197 dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret);
198 return ret;
199 }
200 if (ret != wm2000->anc_download_size) {
201 dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n",
202 ret, wm2000->anc_download_size);
203 return -EIO;
204 }
205
206 dev_dbg(&i2c->dev, "Download complete\n");
207
208 if (analogue) {
209 timeout = 248;
210 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, timeout / 4);
211
212 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
213 WM2000_MODE_ANA_SEQ_INCLUDE |
214 WM2000_MODE_MOUSE_ENABLE |
215 WM2000_MODE_THERMAL_ENABLE);
216 } else {
217 timeout = 10;
218
219 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
220 WM2000_MODE_MOUSE_ENABLE |
221 WM2000_MODE_THERMAL_ENABLE);
222 }
223
224 ret = wm2000_read(i2c, WM2000_REG_SPEECH_CLARITY);
225 if (wm2000->speech_clarity)
226 ret &= ~WM2000_SPEECH_CLARITY;
227 else
228 ret |= WM2000_SPEECH_CLARITY;
229 wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, ret);
230
231 wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33);
232 wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02);
233
234 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
235
236 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
237 WM2000_STATUS_MOUSE_ACTIVE, timeout)) {
238 dev_err(&i2c->dev, "Timed out waiting for device after %dms\n",
239 timeout * 10);
240 return -ETIMEDOUT;
241 }
242
243 dev_dbg(&i2c->dev, "ANC active\n");
244 if (analogue)
245 dev_dbg(&i2c->dev, "Analogue active\n");
246 wm2000->anc_mode = ANC_ACTIVE;
247
248 return 0;
249}
250
251static int wm2000_power_down(struct i2c_client *i2c, int analogue)
252{
253 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
254 int timeout;
255
256 if (analogue) {
257 timeout = 248;
258 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, timeout / 4);
259 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
260 WM2000_MODE_ANA_SEQ_INCLUDE |
261 WM2000_MODE_POWER_DOWN);
262 } else {
263 timeout = 10;
264 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
265 WM2000_MODE_POWER_DOWN);
266 }
267
268 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
269 WM2000_STATUS_POWER_DOWN_COMPLETE, timeout)) {
270 dev_err(&i2c->dev, "Timeout waiting for ANC power down\n");
271 return -ETIMEDOUT;
272 }
273
274 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
275 WM2000_ANC_ENG_IDLE, 1)) {
276 dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
277 return -ETIMEDOUT;
278 }
279
280 dev_dbg(&i2c->dev, "powered off\n");
281 wm2000->anc_mode = ANC_OFF;
282
283 return 0;
284}
285
286static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue)
287{
288 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
289
290 BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
291
292 if (analogue) {
293 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
294 WM2000_MODE_ANA_SEQ_INCLUDE |
295 WM2000_MODE_THERMAL_ENABLE |
296 WM2000_MODE_BYPASS_ENTRY);
297 } else {
298 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
299 WM2000_MODE_THERMAL_ENABLE |
300 WM2000_MODE_BYPASS_ENTRY);
301 }
302
303 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
304 WM2000_STATUS_ANC_DISABLED, 10)) {
305 dev_err(&i2c->dev, "Timeout waiting for ANC disable\n");
306 return -ETIMEDOUT;
307 }
308
309 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
310 WM2000_ANC_ENG_IDLE, 1)) {
311 dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
312 return -ETIMEDOUT;
313 }
314
315 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
316 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
317
318 wm2000->anc_mode = ANC_BYPASS;
319 dev_dbg(&i2c->dev, "bypass enabled\n");
320
321 return 0;
322}
323
324static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue)
325{
326 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
327
328 BUG_ON(wm2000->anc_mode != ANC_BYPASS);
329
330 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
331
332 if (analogue) {
333 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
334 WM2000_MODE_ANA_SEQ_INCLUDE |
335 WM2000_MODE_MOUSE_ENABLE |
336 WM2000_MODE_THERMAL_ENABLE);
337 } else {
338 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
339 WM2000_MODE_MOUSE_ENABLE |
340 WM2000_MODE_THERMAL_ENABLE);
341 }
342
343 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
344 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
345
346 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
347 WM2000_STATUS_MOUSE_ACTIVE, 10)) {
348 dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
349 return -ETIMEDOUT;
350 }
351
352 wm2000->anc_mode = ANC_ACTIVE;
353 dev_dbg(&i2c->dev, "MOUSE active\n");
354
355 return 0;
356}
357
358static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
359{
360 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
361 int timeout;
362
363 BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
364
365 if (analogue) {
366 timeout = 248;
367 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, timeout / 4);
368
369 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
370 WM2000_MODE_ANA_SEQ_INCLUDE |
371 WM2000_MODE_THERMAL_ENABLE |
372 WM2000_MODE_STANDBY_ENTRY);
373 } else {
374 timeout = 10;
375
376 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
377 WM2000_MODE_THERMAL_ENABLE |
378 WM2000_MODE_STANDBY_ENTRY);
379 }
380
381 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
382 WM2000_STATUS_ANC_DISABLED, timeout)) {
383 dev_err(&i2c->dev,
384 "Timed out waiting for ANC disable after 1ms\n");
385 return -ETIMEDOUT;
386 }
387
388 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE,
389 1)) {
390 dev_err(&i2c->dev,
391 "Timed out waiting for standby after %dms\n",
392 timeout * 10);
393 return -ETIMEDOUT;
394 }
395
396 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
397 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
398
399 wm2000->anc_mode = ANC_STANDBY;
400 dev_dbg(&i2c->dev, "standby\n");
401 if (analogue)
402 dev_dbg(&i2c->dev, "Analogue disabled\n");
403
404 return 0;
405}
406
407static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
408{
409 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
410 int timeout;
411
412 BUG_ON(wm2000->anc_mode != ANC_STANDBY);
413
414 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
415
416 if (analogue) {
417 timeout = 248;
418 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, timeout / 4);
419
420 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
421 WM2000_MODE_ANA_SEQ_INCLUDE |
422 WM2000_MODE_THERMAL_ENABLE |
423 WM2000_MODE_MOUSE_ENABLE);
424 } else {
425 timeout = 10;
426
427 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
428 WM2000_MODE_THERMAL_ENABLE |
429 WM2000_MODE_MOUSE_ENABLE);
430 }
431
432 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
433 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
434
435 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
436 WM2000_STATUS_MOUSE_ACTIVE, timeout)) {
437 dev_err(&i2c->dev, "Timed out waiting for MOUSE after %dms\n",
438 timeout * 10);
439 return -ETIMEDOUT;
440 }
441
442 wm2000->anc_mode = ANC_ACTIVE;
443 dev_dbg(&i2c->dev, "MOUSE active\n");
444 if (analogue)
445 dev_dbg(&i2c->dev, "Analogue enabled\n");
446
447 return 0;
448}
449
450typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
451
452static struct {
453 enum wm2000_anc_mode source;
454 enum wm2000_anc_mode dest;
455 int analogue;
456 wm2000_mode_fn step[2];
457} anc_transitions[] = {
458 {
459 .source = ANC_OFF,
460 .dest = ANC_ACTIVE,
461 .analogue = 1,
462 .step = {
463 wm2000_power_up,
464 },
465 },
466 {
467 .source = ANC_OFF,
468 .dest = ANC_STANDBY,
469 .step = {
470 wm2000_power_up,
471 wm2000_enter_standby,
472 },
473 },
474 {
475 .source = ANC_OFF,
476 .dest = ANC_BYPASS,
477 .analogue = 1,
478 .step = {
479 wm2000_power_up,
480 wm2000_enter_bypass,
481 },
482 },
483 {
484 .source = ANC_ACTIVE,
485 .dest = ANC_BYPASS,
486 .analogue = 1,
487 .step = {
488 wm2000_enter_bypass,
489 },
490 },
491 {
492 .source = ANC_ACTIVE,
493 .dest = ANC_STANDBY,
494 .analogue = 1,
495 .step = {
496 wm2000_enter_standby,
497 },
498 },
499 {
500 .source = ANC_ACTIVE,
501 .dest = ANC_OFF,
502 .analogue = 1,
503 .step = {
504 wm2000_power_down,
505 },
506 },
507 {
508 .source = ANC_BYPASS,
509 .dest = ANC_ACTIVE,
510 .analogue = 1,
511 .step = {
512 wm2000_exit_bypass,
513 },
514 },
515 {
516 .source = ANC_BYPASS,
517 .dest = ANC_STANDBY,
518 .analogue = 1,
519 .step = {
520 wm2000_exit_bypass,
521 wm2000_enter_standby,
522 },
523 },
524 {
525 .source = ANC_BYPASS,
526 .dest = ANC_OFF,
527 .step = {
528 wm2000_exit_bypass,
529 wm2000_power_down,
530 },
531 },
532 {
533 .source = ANC_STANDBY,
534 .dest = ANC_ACTIVE,
535 .analogue = 1,
536 .step = {
537 wm2000_exit_standby,
538 },
539 },
540 {
541 .source = ANC_STANDBY,
542 .dest = ANC_BYPASS,
543 .analogue = 1,
544 .step = {
545 wm2000_exit_standby,
546 wm2000_enter_bypass,
547 },
548 },
549 {
550 .source = ANC_STANDBY,
551 .dest = ANC_OFF,
552 .step = {
553 wm2000_exit_standby,
554 wm2000_power_down,
555 },
556 },
557};
558
559static int wm2000_anc_transition(struct wm2000_priv *wm2000,
560 enum wm2000_anc_mode mode)
561{
562 struct i2c_client *i2c = wm2000->i2c;
563 int i, j;
564 int ret;
565
566 if (wm2000->anc_mode == mode)
567 return 0;
568
569 for (i = 0; i < ARRAY_SIZE(anc_transitions); i++)
570 if (anc_transitions[i].source == wm2000->anc_mode &&
571 anc_transitions[i].dest == mode)
572 break;
573 if (i == ARRAY_SIZE(anc_transitions)) {
574 dev_err(&i2c->dev, "No transition for %d->%d\n",
575 wm2000->anc_mode, mode);
576 return -EINVAL;
577 }
578
579 for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) {
580 if (!anc_transitions[i].step[j])
581 break;
582 ret = anc_transitions[i].step[j](i2c,
583 anc_transitions[i].analogue);
584 if (ret != 0)
585 return ret;
586 }
587
588 return 0;
589}
590
591static int wm2000_anc_set_mode(struct wm2000_priv *wm2000)
592{
593 struct i2c_client *i2c = wm2000->i2c;
594 enum wm2000_anc_mode mode;
595
596 if (wm2000->anc_eng_ena && wm2000->spk_ena)
597 if (wm2000->anc_active)
598 mode = ANC_ACTIVE;
599 else
600 mode = ANC_BYPASS;
601 else
602 mode = ANC_STANDBY;
603
604 dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n",
605 mode, wm2000->anc_eng_ena, !wm2000->spk_ena,
606 wm2000->anc_active);
607
608 return wm2000_anc_transition(wm2000, mode);
609}
610
611static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
612 struct snd_ctl_elem_value *ucontrol)
613{
614 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
615
616 ucontrol->value.enumerated.item[0] = wm2000->anc_active;
617
618 return 0;
619}
620
621static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
622 struct snd_ctl_elem_value *ucontrol)
623{
624 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
625 int anc_active = ucontrol->value.enumerated.item[0];
626
627 if (anc_active > 1)
628 return -EINVAL;
629
630 wm2000->anc_active = anc_active;
631
632 return wm2000_anc_set_mode(wm2000);
633}
634
635static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
636 struct snd_ctl_elem_value *ucontrol)
637{
638 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
639
640 ucontrol->value.enumerated.item[0] = wm2000->spk_ena;
641
642 return 0;
643}
644
645static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
646 struct snd_ctl_elem_value *ucontrol)
647{
648 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
649 int val = ucontrol->value.enumerated.item[0];
650
651 if (val > 1)
652 return -EINVAL;
653
654 wm2000->spk_ena = val;
655
656 return wm2000_anc_set_mode(wm2000);
657}
658
659static const struct snd_kcontrol_new wm2000_controls[] = {
660 SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0,
661 wm2000_anc_mode_get,
662 wm2000_anc_mode_put),
663 SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0,
664 wm2000_speaker_get,
665 wm2000_speaker_put),
666};
667
668static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
669 struct snd_kcontrol *kcontrol, int event)
670{
671 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
672
673 if (SND_SOC_DAPM_EVENT_ON(event))
674 wm2000->anc_eng_ena = 1;
675
676 if (SND_SOC_DAPM_EVENT_OFF(event))
677 wm2000->anc_eng_ena = 0;
678
679 return wm2000_anc_set_mode(wm2000);
680}
681
682static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
683/* Externally visible pins */
684SND_SOC_DAPM_OUTPUT("WM2000 SPKN"),
685SND_SOC_DAPM_OUTPUT("WM2000 SPKP"),
686
687SND_SOC_DAPM_INPUT("WM2000 LINN"),
688SND_SOC_DAPM_INPUT("WM2000 LINP"),
689
690SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0,
691 wm2000_anc_power_event,
692 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
693};
694
695/* Target, Path, Source */
696static const struct snd_soc_dapm_route audio_map[] = {
697 { "WM2000 SPKN", NULL, "ANC Engine" },
698 { "WM2000 SPKP", NULL, "ANC Engine" },
699 { "ANC Engine", NULL, "WM2000 LINN" },
700 { "ANC Engine", NULL, "WM2000 LINP" },
701};
702
703/* Called from the machine driver */
704int wm2000_add_controls(struct snd_soc_codec *codec)
705{
ce6120cc 706 struct snd_soc_dapm_context *dapm = &codec->dapm;
3a66d387
MB
707 int ret;
708
709 if (!wm2000_i2c) {
710 pr_err("WM2000 not yet probed\n");
711 return -ENODEV;
712 }
713
ce6120cc 714 ret = snd_soc_dapm_new_controls(dapm, wm2000_dapm_widgets,
3a66d387
MB
715 ARRAY_SIZE(wm2000_dapm_widgets));
716 if (ret < 0)
717 return ret;
718
ce6120cc 719 ret = snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
3a66d387
MB
720 if (ret < 0)
721 return ret;
722
723 return snd_soc_add_controls(codec, wm2000_controls,
724 ARRAY_SIZE(wm2000_controls));
725}
726EXPORT_SYMBOL_GPL(wm2000_add_controls);
727
728static int __devinit wm2000_i2c_probe(struct i2c_client *i2c,
729 const struct i2c_device_id *i2c_id)
730{
731 struct wm2000_priv *wm2000;
732 struct wm2000_platform_data *pdata;
733 const char *filename;
734 const struct firmware *fw;
735 int reg, ret;
736 u16 id;
737
738 if (wm2000_i2c) {
739 dev_err(&i2c->dev, "Another WM2000 is already registered\n");
740 return -EINVAL;
741 }
742
743 wm2000 = kzalloc(sizeof(struct wm2000_priv), GFP_KERNEL);
744 if (wm2000 == NULL) {
745 dev_err(&i2c->dev, "Unable to allocate private data\n");
746 return -ENOMEM;
747 }
748
749 /* Verify that this is a WM2000 */
750 reg = wm2000_read(i2c, WM2000_REG_ID1);
751 id = reg << 8;
752 reg = wm2000_read(i2c, WM2000_REG_ID2);
753 id |= reg & 0xff;
754
755 if (id != 0x2000) {
756 dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id);
757 ret = -ENODEV;
758 goto err;
759 }
760
761 reg = wm2000_read(i2c, WM2000_REG_REVISON);
762 dev_info(&i2c->dev, "revision %c\n", reg + 'A');
763
764 filename = "wm2000_anc.bin";
765 pdata = dev_get_platdata(&i2c->dev);
766 if (pdata) {
767 wm2000->mclk_div = pdata->mclkdiv2;
768 wm2000->speech_clarity = !pdata->speech_enh_disable;
769
770 if (pdata->download_file)
771 filename = pdata->download_file;
772 }
773
774 ret = request_firmware(&fw, filename, &i2c->dev);
775 if (ret != 0) {
776 dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret);
777 goto err;
778 }
779
780 /* Pre-cook the concatenation of the register address onto the image */
781 wm2000->anc_download_size = fw->size + 2;
782 wm2000->anc_download = kmalloc(wm2000->anc_download_size, GFP_KERNEL);
783 if (wm2000->anc_download == NULL) {
784 dev_err(&i2c->dev, "Out of memory\n");
785 ret = -ENOMEM;
786 goto err_fw;
787 }
788
789 wm2000->anc_download[0] = 0x80;
790 wm2000->anc_download[1] = 0x00;
791 memcpy(wm2000->anc_download + 2, fw->data, fw->size);
792
793 release_firmware(fw);
794
795 dev_set_drvdata(&i2c->dev, wm2000);
796 wm2000->anc_eng_ena = 1;
e71fa370
MB
797 wm2000->anc_active = 1;
798 wm2000->spk_ena = 1;
3a66d387
MB
799 wm2000->i2c = i2c;
800
801 wm2000_reset(wm2000);
802
803 /* This will trigger a transition to standby mode by default */
804 wm2000_anc_set_mode(wm2000);
805
806 wm2000_i2c = i2c;
807
808 return 0;
809
810err_fw:
811 release_firmware(fw);
812err:
813 kfree(wm2000);
814 return ret;
815}
816
817static __devexit int wm2000_i2c_remove(struct i2c_client *i2c)
818{
819 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
820
821 wm2000_anc_transition(wm2000, ANC_OFF);
822
823 wm2000_i2c = NULL;
824 kfree(wm2000->anc_download);
825 kfree(wm2000);
826
827 return 0;
828}
829
830static void wm2000_i2c_shutdown(struct i2c_client *i2c)
831{
832 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
833
834 wm2000_anc_transition(wm2000, ANC_OFF);
835}
836
837#ifdef CONFIG_PM
b3111a9a 838static int wm2000_i2c_suspend(struct device *dev)
3a66d387 839{
b3111a9a 840 struct i2c_client *i2c = to_i2c_client(dev);
3a66d387
MB
841 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
842
843 return wm2000_anc_transition(wm2000, ANC_OFF);
844}
845
b3111a9a 846static int wm2000_i2c_resume(struct device *dev)
3a66d387 847{
b3111a9a 848 struct i2c_client *i2c = to_i2c_client(dev);
3a66d387
MB
849 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
850
851 return wm2000_anc_set_mode(wm2000);
852}
3a66d387
MB
853#endif
854
b3111a9a
MB
855static SIMPLE_DEV_PM_OPS(wm2000_pm, wm2000_i2c_suspend, wm2000_i2c_resume);
856
3a66d387
MB
857static const struct i2c_device_id wm2000_i2c_id[] = {
858 { "wm2000", 0 },
859 { }
860};
861MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id);
862
863static struct i2c_driver wm2000_i2c_driver = {
864 .driver = {
865 .name = "wm2000",
866 .owner = THIS_MODULE,
b3111a9a 867 .pm = &wm2000_pm,
3a66d387
MB
868 },
869 .probe = wm2000_i2c_probe,
870 .remove = __devexit_p(wm2000_i2c_remove),
3a66d387
MB
871 .shutdown = wm2000_i2c_shutdown,
872 .id_table = wm2000_i2c_id,
873};
874
875static int __init wm2000_init(void)
876{
877 return i2c_add_driver(&wm2000_i2c_driver);
878}
879module_init(wm2000_init);
880
881static void __exit wm2000_exit(void)
882{
883 i2c_del_driver(&wm2000_i2c_driver);
884}
885module_exit(wm2000_exit);
886
887MODULE_DESCRIPTION("ASoC WM2000 driver");
888MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
889MODULE_LICENSE("GPL");
This page took 0.203232 seconds and 5 git commands to generate.