Merge branches 'fixes', 'pgt-next' and 'versatile' into devel
[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>
32#include <linux/platform_device.h>
33#include <linux/debugfs.h>
5a0e3ad6 34#include <linux/slab.h>
3a66d387
MB
35#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
3a66d387
MB
39#include <sound/initval.h>
40#include <sound/tlv.h>
41
42#include <sound/wm2000.h>
43
44#include "wm2000.h"
45
46enum wm2000_anc_mode {
47 ANC_ACTIVE = 0,
48 ANC_BYPASS = 1,
49 ANC_STANDBY = 2,
50 ANC_OFF = 3,
51};
52
53struct wm2000_priv {
54 struct i2c_client *i2c;
55
56 enum wm2000_anc_mode anc_mode;
57
58 unsigned int anc_active:1;
59 unsigned int anc_eng_ena:1;
60 unsigned int spk_ena:1;
61
62 unsigned int mclk_div:1;
63 unsigned int speech_clarity:1;
64
65 int anc_download_size;
66 char *anc_download;
67};
68
69static struct i2c_client *wm2000_i2c;
70
71static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
72 unsigned int value)
73{
74 u8 data[3];
75 int ret;
76
77 data[0] = (reg >> 8) & 0xff;
78 data[1] = reg & 0xff;
79 data[2] = value & 0xff;
80
81 dev_vdbg(&i2c->dev, "write %x = %x\n", reg, value);
82
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;
90}
91
92static unsigned int wm2000_read(struct i2c_client *i2c, unsigned int r)
93{
94 struct i2c_msg xfer[2];
95 u8 reg[2];
96 u8 data;
97 int ret;
98
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];
106
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;
112
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;
117 }
118
119 dev_vdbg(&i2c->dev, "read %x from %x\n", data, r);
120
121 return data;
122}
123
124static void wm2000_reset(struct wm2000_priv *wm2000)
125{
126 struct i2c_client *i2c = wm2000->i2c;
127
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);
131
132 wm2000->anc_mode = ANC_OFF;
133}
134
135static int wm2000_poll_bit(struct i2c_client *i2c,
136 unsigned int reg, u8 mask, int timeout)
137{
138 int val;
139
140 val = wm2000_read(i2c, reg);
141
142 while (!(val & mask) && --timeout) {
143 msleep(1);
144 val = wm2000_read(i2c, reg);
145 }
146
147 if (timeout == 0)
148 return 0;
149 else
150 return 1;
151}
152
153static int wm2000_power_up(struct i2c_client *i2c, int analogue)
154{
155 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
156 int ret, timeout;
157
158 BUG_ON(wm2000->anc_mode != ANC_OFF);
159
160 dev_dbg(&i2c->dev, "Beginning power up\n");
161
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);
170 }
171
172 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
173 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET);
174
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;
180 }
181
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;
186 }
187
188 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
189
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);
194
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;
200 }
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;
205 }
206
207 dev_dbg(&i2c->dev, "Download complete\n");
208
209 if (analogue) {
210 timeout = 248;
211 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, timeout / 4);
212
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;
219
220 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
221 WM2000_MODE_MOUSE_ENABLE |
222 WM2000_MODE_THERMAL_ENABLE);
223 }
224
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);
231
232 wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33);
233 wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02);
234
235 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
236
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;
242 }
243
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;
248
249 return 0;
250}
251
252static int wm2000_power_down(struct i2c_client *i2c, int analogue)
253{
254 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
255 int timeout;
256
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);
267 }
268
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;
273 }
274
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;
279 }
280
281 dev_dbg(&i2c->dev, "powered off\n");
282 wm2000->anc_mode = ANC_OFF;
283
284 return 0;
285}
286
287static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue)
288{
289 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
290
291 BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
292
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);
302 }
303
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;
308 }
309
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;
314 }
315
316 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
317 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
318
319 wm2000->anc_mode = ANC_BYPASS;
320 dev_dbg(&i2c->dev, "bypass enabled\n");
321
322 return 0;
323}
324
325static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue)
326{
327 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
328
329 BUG_ON(wm2000->anc_mode != ANC_BYPASS);
330
331 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
332
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);
342 }
343
344 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
345 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
346
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;
351 }
352
353 wm2000->anc_mode = ANC_ACTIVE;
354 dev_dbg(&i2c->dev, "MOUSE active\n");
355
356 return 0;
357}
358
359static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
360{
361 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
362 int timeout;
363
364 BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
365
366 if (analogue) {
367 timeout = 248;
368 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, timeout / 4);
369
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;
376
377 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
378 WM2000_MODE_THERMAL_ENABLE |
379 WM2000_MODE_STANDBY_ENTRY);
380 }
381
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;
387 }
388
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;
395 }
396
397 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
398 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
399
400 wm2000->anc_mode = ANC_STANDBY;
401 dev_dbg(&i2c->dev, "standby\n");
402 if (analogue)
403 dev_dbg(&i2c->dev, "Analogue disabled\n");
404
405 return 0;
406}
407
408static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
409{
410 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
411 int timeout;
412
413 BUG_ON(wm2000->anc_mode != ANC_STANDBY);
414
415 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
416
417 if (analogue) {
418 timeout = 248;
419 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, timeout / 4);
420
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;
427
428 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
429 WM2000_MODE_THERMAL_ENABLE |
430 WM2000_MODE_MOUSE_ENABLE);
431 }
432
433 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
434 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
435
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;
441 }
442
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");
447
448 return 0;
449}
450
451typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
452
453static 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[] = {
459 {
460 .source = ANC_OFF,
461 .dest = ANC_ACTIVE,
462 .analogue = 1,
463 .step = {
464 wm2000_power_up,
465 },
466 },
467 {
468 .source = ANC_OFF,
469 .dest = ANC_STANDBY,
470 .step = {
471 wm2000_power_up,
472 wm2000_enter_standby,
473 },
474 },
475 {
476 .source = ANC_OFF,
477 .dest = ANC_BYPASS,
478 .analogue = 1,
479 .step = {
480 wm2000_power_up,
481 wm2000_enter_bypass,
482 },
483 },
484 {
485 .source = ANC_ACTIVE,
486 .dest = ANC_BYPASS,
487 .analogue = 1,
488 .step = {
489 wm2000_enter_bypass,
490 },
491 },
492 {
493 .source = ANC_ACTIVE,
494 .dest = ANC_STANDBY,
495 .analogue = 1,
496 .step = {
497 wm2000_enter_standby,
498 },
499 },
500 {
501 .source = ANC_ACTIVE,
502 .dest = ANC_OFF,
503 .analogue = 1,
504 .step = {
505 wm2000_power_down,
506 },
507 },
508 {
509 .source = ANC_BYPASS,
510 .dest = ANC_ACTIVE,
511 .analogue = 1,
512 .step = {
513 wm2000_exit_bypass,
514 },
515 },
516 {
517 .source = ANC_BYPASS,
518 .dest = ANC_STANDBY,
519 .analogue = 1,
520 .step = {
521 wm2000_exit_bypass,
522 wm2000_enter_standby,
523 },
524 },
525 {
526 .source = ANC_BYPASS,
527 .dest = ANC_OFF,
528 .step = {
529 wm2000_exit_bypass,
530 wm2000_power_down,
531 },
532 },
533 {
534 .source = ANC_STANDBY,
535 .dest = ANC_ACTIVE,
536 .analogue = 1,
537 .step = {
538 wm2000_exit_standby,
539 },
540 },
541 {
542 .source = ANC_STANDBY,
543 .dest = ANC_BYPASS,
544 .analogue = 1,
545 .step = {
546 wm2000_exit_standby,
547 wm2000_enter_bypass,
548 },
549 },
550 {
551 .source = ANC_STANDBY,
552 .dest = ANC_OFF,
553 .step = {
554 wm2000_exit_standby,
555 wm2000_power_down,
556 },
557 },
558};
559
560static int wm2000_anc_transition(struct wm2000_priv *wm2000,
561 enum wm2000_anc_mode mode)
562{
563 struct i2c_client *i2c = wm2000->i2c;
564 int i, j;
565 int ret;
566
567 if (wm2000->anc_mode == mode)
568 return 0;
569
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;
578 }
579
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;
587 }
588
589 return 0;
590}
591
592static int wm2000_anc_set_mode(struct wm2000_priv *wm2000)
593{
594 struct i2c_client *i2c = wm2000->i2c;
595 enum wm2000_anc_mode mode;
596
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;
604
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);
608
609 return wm2000_anc_transition(wm2000, mode);
610}
611
612static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
613 struct snd_ctl_elem_value *ucontrol)
614{
615 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
616
617 ucontrol->value.enumerated.item[0] = wm2000->anc_active;
618
619 return 0;
620}
621
622static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
623 struct snd_ctl_elem_value *ucontrol)
624{
625 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
626 int anc_active = ucontrol->value.enumerated.item[0];
627
628 if (anc_active > 1)
629 return -EINVAL;
630
631 wm2000->anc_active = anc_active;
632
633 return wm2000_anc_set_mode(wm2000);
634}
635
636static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
637 struct snd_ctl_elem_value *ucontrol)
638{
639 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
640
641 ucontrol->value.enumerated.item[0] = wm2000->spk_ena;
642
643 return 0;
644}
645
646static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
647 struct snd_ctl_elem_value *ucontrol)
648{
649 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
650 int val = ucontrol->value.enumerated.item[0];
651
652 if (val > 1)
653 return -EINVAL;
654
655 wm2000->spk_ena = val;
656
657 return wm2000_anc_set_mode(wm2000);
658}
659
660static 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),
667};
668
669static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
670 struct snd_kcontrol *kcontrol, int event)
671{
672 struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
673
674 if (SND_SOC_DAPM_EVENT_ON(event))
675 wm2000->anc_eng_ena = 1;
676
677 if (SND_SOC_DAPM_EVENT_OFF(event))
678 wm2000->anc_eng_ena = 0;
679
680 return wm2000_anc_set_mode(wm2000);
681}
682
683static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
684/* Externally visible pins */
685SND_SOC_DAPM_OUTPUT("WM2000 SPKN"),
686SND_SOC_DAPM_OUTPUT("WM2000 SPKP"),
687
688SND_SOC_DAPM_INPUT("WM2000 LINN"),
689SND_SOC_DAPM_INPUT("WM2000 LINP"),
690
691SND_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),
694};
695
696/* Target, Path, Source */
697static 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" },
702};
703
704/* Called from the machine driver */
705int wm2000_add_controls(struct snd_soc_codec *codec)
706{
ce6120cc 707 struct snd_soc_dapm_context *dapm = &codec->dapm;
3a66d387
MB
708 int ret;
709
710 if (!wm2000_i2c) {
711 pr_err("WM2000 not yet probed\n");
712 return -ENODEV;
713 }
714
ce6120cc 715 ret = snd_soc_dapm_new_controls(dapm, wm2000_dapm_widgets,
3a66d387
MB
716 ARRAY_SIZE(wm2000_dapm_widgets));
717 if (ret < 0)
718 return ret;
719
ce6120cc 720 ret = snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
3a66d387
MB
721 if (ret < 0)
722 return ret;
723
724 return snd_soc_add_controls(codec, wm2000_controls,
725 ARRAY_SIZE(wm2000_controls));
726}
727EXPORT_SYMBOL_GPL(wm2000_add_controls);
728
729static int __devinit wm2000_i2c_probe(struct i2c_client *i2c,
730 const struct i2c_device_id *i2c_id)
731{
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;
738
739 if (wm2000_i2c) {
740 dev_err(&i2c->dev, "Another WM2000 is already registered\n");
741 return -EINVAL;
742 }
743
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;
748 }
749
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;
755
756 if (id != 0x2000) {
757 dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id);
758 ret = -ENODEV;
759 goto err;
760 }
761
762 reg = wm2000_read(i2c, WM2000_REG_REVISON);
763 dev_info(&i2c->dev, "revision %c\n", reg + 'A');
764
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;
770
771 if (pdata->download_file)
772 filename = pdata->download_file;
773 }
774
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;
779 }
780
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;
788 }
789
790 wm2000->anc_download[0] = 0x80;
791 wm2000->anc_download[1] = 0x00;
792 memcpy(wm2000->anc_download + 2, fw->data, fw->size);
793
794 release_firmware(fw);
795
796 dev_set_drvdata(&i2c->dev, wm2000);
797 wm2000->anc_eng_ena = 1;
e71fa370
MB
798 wm2000->anc_active = 1;
799 wm2000->spk_ena = 1;
3a66d387
MB
800 wm2000->i2c = i2c;
801
802 wm2000_reset(wm2000);
803
804 /* This will trigger a transition to standby mode by default */
805 wm2000_anc_set_mode(wm2000);
806
807 wm2000_i2c = i2c;
808
809 return 0;
810
811err_fw:
812 release_firmware(fw);
813err:
814 kfree(wm2000);
815 return ret;
816}
817
818static __devexit int wm2000_i2c_remove(struct i2c_client *i2c)
819{
820 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
821
822 wm2000_anc_transition(wm2000, ANC_OFF);
823
824 wm2000_i2c = NULL;
825 kfree(wm2000->anc_download);
826 kfree(wm2000);
827
828 return 0;
829}
830
831static void wm2000_i2c_shutdown(struct i2c_client *i2c)
832{
833 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
834
835 wm2000_anc_transition(wm2000, ANC_OFF);
836}
837
838#ifdef CONFIG_PM
839static int wm2000_i2c_suspend(struct i2c_client *i2c, pm_message_t mesg)
840{
841 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
842
843 return wm2000_anc_transition(wm2000, ANC_OFF);
844}
845
846static int wm2000_i2c_resume(struct i2c_client *i2c)
847{
848 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
849
850 return wm2000_anc_set_mode(wm2000);
851}
852#else
853#define wm2000_i2c_suspend NULL
854#define wm2000_i2c_resume NULL
855#endif
856
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,
867 },
868 .probe = wm2000_i2c_probe,
869 .remove = __devexit_p(wm2000_i2c_remove),
870 .suspend = wm2000_i2c_suspend,
871 .resume = wm2000_i2c_resume,
872 .shutdown = wm2000_i2c_shutdown,
873 .id_table = wm2000_i2c_id,
874};
875
876static int __init wm2000_init(void)
877{
878 return i2c_add_driver(&wm2000_i2c_driver);
879}
880module_init(wm2000_init);
881
882static void __exit wm2000_exit(void)
883{
884 i2c_del_driver(&wm2000_i2c_driver);
885}
886module_exit(wm2000_exit);
887
888MODULE_DESCRIPTION("ASoC WM2000 driver");
889MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
890MODULE_LICENSE("GPL");
This page took 0.187808 seconds and 5 git commands to generate.