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