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