blob: a3acb7a85f6ab26f62e92489e75e0f6b4822f831 [file] [log] [blame]
Mark Brown3a66d382010-02-11 13:27:19 +00001/*
2 * wm2000.c -- WM2000 ALSA Soc Audio driver
3 *
Mark Brown656baae2012-05-23 12:39:07 +01004 * Copyright 2008-2011 Wolfson Microelectronics PLC.
Mark Brown3a66d382010-02-11 13:27:19 +00005 *
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>
Mark Brown3a66d382010-02-11 13:27:19 +000026#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>
Mark Brown8aa1fe82011-12-02 21:57:19 +000032#include <linux/regmap.h>
Mark Brown3a66d382010-02-11 13:27:19 +000033#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Mark Brown3a66d382010-02-11 13:27:19 +000035#include <sound/core.h>
36#include <sound/pcm.h>
37#include <sound/pcm_params.h>
38#include <sound/soc.h>
Mark Brown3a66d382010-02-11 13:27:19 +000039#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;
Mark Brown8aa1fe82011-12-02 21:57:19 +000055 struct regmap *regmap;
Mark Brown3a66d382010-02-11 13:27:19 +000056
57 enum wm2000_anc_mode anc_mode;
58
59 unsigned int anc_active:1;
60 unsigned int anc_eng_ena:1;
61 unsigned int spk_ena:1;
62
63 unsigned int mclk_div:1;
64 unsigned int speech_clarity:1;
65
66 int anc_download_size;
67 char *anc_download;
68};
69
Mark Brown3a66d382010-02-11 13:27:19 +000070static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
71 unsigned int value)
72{
Mark Brown8aa1fe82011-12-02 21:57:19 +000073 struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
74 return regmap_write(wm2000->regmap, reg, value);
Mark Brown3a66d382010-02-11 13:27:19 +000075}
76
77static unsigned int wm2000_read(struct i2c_client *i2c, unsigned int r)
78{
Mark Brown8aa1fe82011-12-02 21:57:19 +000079 struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
80 unsigned int val;
Mark Brown3a66d382010-02-11 13:27:19 +000081 int ret;
82
Mark Brown8aa1fe82011-12-02 21:57:19 +000083 ret = regmap_read(wm2000->regmap, r, &val);
84 if (ret < 0)
85 return -1;
Mark Brown3a66d382010-02-11 13:27:19 +000086
Mark Brown8aa1fe82011-12-02 21:57:19 +000087 return val;
Mark Brown3a66d382010-02-11 13:27:19 +000088}
89
90static void wm2000_reset(struct wm2000_priv *wm2000)
91{
92 struct i2c_client *i2c = wm2000->i2c;
93
94 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
95 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
96 wm2000_write(i2c, WM2000_REG_ID1, 0);
97
98 wm2000->anc_mode = ANC_OFF;
99}
100
101static int wm2000_poll_bit(struct i2c_client *i2c,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800102 unsigned int reg, u8 mask)
Mark Brown3a66d382010-02-11 13:27:19 +0000103{
Mark Brown3ff1ec22012-06-08 06:53:48 +0800104 int timeout = 4000;
Mark Brown3a66d382010-02-11 13:27:19 +0000105 int val;
106
107 val = wm2000_read(i2c, reg);
108
109 while (!(val & mask) && --timeout) {
110 msleep(1);
111 val = wm2000_read(i2c, reg);
112 }
113
114 if (timeout == 0)
115 return 0;
116 else
117 return 1;
118}
119
120static int wm2000_power_up(struct i2c_client *i2c, int analogue)
121{
122 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
Mark Brown3ff1ec22012-06-08 06:53:48 +0800123 int ret;
Mark Brown3a66d382010-02-11 13:27:19 +0000124
125 BUG_ON(wm2000->anc_mode != ANC_OFF);
126
127 dev_dbg(&i2c->dev, "Beginning power up\n");
128
129 if (!wm2000->mclk_div) {
130 dev_dbg(&i2c->dev, "Disabling MCLK divider\n");
131 wm2000_write(i2c, WM2000_REG_SYS_CTL2,
132 WM2000_MCLK_DIV2_ENA_CLR);
133 } else {
134 dev_dbg(&i2c->dev, "Enabling MCLK divider\n");
135 wm2000_write(i2c, WM2000_REG_SYS_CTL2,
136 WM2000_MCLK_DIV2_ENA_SET);
137 }
138
139 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
140 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET);
141
142 /* Wait for ANC engine to become ready */
143 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800144 WM2000_ANC_ENG_IDLE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000145 dev_err(&i2c->dev, "ANC engine failed to reset\n");
146 return -ETIMEDOUT;
147 }
148
149 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800150 WM2000_STATUS_BOOT_COMPLETE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000151 dev_err(&i2c->dev, "ANC engine failed to initialise\n");
152 return -ETIMEDOUT;
153 }
154
155 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
156
157 /* Open code download of the data since it is the only bulk
158 * write we do. */
159 dev_dbg(&i2c->dev, "Downloading %d bytes\n",
160 wm2000->anc_download_size - 2);
161
162 ret = i2c_master_send(i2c, wm2000->anc_download,
163 wm2000->anc_download_size);
164 if (ret < 0) {
165 dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret);
166 return ret;
167 }
168 if (ret != wm2000->anc_download_size) {
169 dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n",
170 ret, wm2000->anc_download_size);
171 return -EIO;
172 }
173
174 dev_dbg(&i2c->dev, "Download complete\n");
175
176 if (analogue) {
Mark Brown3ff1ec22012-06-08 06:53:48 +0800177 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
Mark Brown3a66d382010-02-11 13:27:19 +0000178
179 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
180 WM2000_MODE_ANA_SEQ_INCLUDE |
181 WM2000_MODE_MOUSE_ENABLE |
182 WM2000_MODE_THERMAL_ENABLE);
183 } else {
Mark Brown3a66d382010-02-11 13:27:19 +0000184 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
185 WM2000_MODE_MOUSE_ENABLE |
186 WM2000_MODE_THERMAL_ENABLE);
187 }
188
189 ret = wm2000_read(i2c, WM2000_REG_SPEECH_CLARITY);
190 if (wm2000->speech_clarity)
191 ret &= ~WM2000_SPEECH_CLARITY;
192 else
193 ret |= WM2000_SPEECH_CLARITY;
194 wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, ret);
195
196 wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33);
197 wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02);
198
199 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
200
201 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800202 WM2000_STATUS_MOUSE_ACTIVE)) {
203 dev_err(&i2c->dev, "Timed out waiting for device\n");
Mark Brown3a66d382010-02-11 13:27:19 +0000204 return -ETIMEDOUT;
205 }
206
207 dev_dbg(&i2c->dev, "ANC active\n");
208 if (analogue)
209 dev_dbg(&i2c->dev, "Analogue active\n");
210 wm2000->anc_mode = ANC_ACTIVE;
211
212 return 0;
213}
214
215static int wm2000_power_down(struct i2c_client *i2c, int analogue)
216{
217 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000218
219 if (analogue) {
Mark Brown3ff1ec22012-06-08 06:53:48 +0800220 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
Mark Brown3a66d382010-02-11 13:27:19 +0000221 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
222 WM2000_MODE_ANA_SEQ_INCLUDE |
223 WM2000_MODE_POWER_DOWN);
224 } else {
Mark Brown3a66d382010-02-11 13:27:19 +0000225 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
226 WM2000_MODE_POWER_DOWN);
227 }
228
229 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800230 WM2000_STATUS_POWER_DOWN_COMPLETE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000231 dev_err(&i2c->dev, "Timeout waiting for ANC power down\n");
232 return -ETIMEDOUT;
233 }
234
235 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800236 WM2000_ANC_ENG_IDLE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000237 dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
238 return -ETIMEDOUT;
239 }
240
241 dev_dbg(&i2c->dev, "powered off\n");
242 wm2000->anc_mode = ANC_OFF;
243
244 return 0;
245}
246
247static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue)
248{
249 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
250
251 BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
252
253 if (analogue) {
254 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
255 WM2000_MODE_ANA_SEQ_INCLUDE |
256 WM2000_MODE_THERMAL_ENABLE |
257 WM2000_MODE_BYPASS_ENTRY);
258 } else {
259 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
260 WM2000_MODE_THERMAL_ENABLE |
261 WM2000_MODE_BYPASS_ENTRY);
262 }
263
264 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800265 WM2000_STATUS_ANC_DISABLED)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000266 dev_err(&i2c->dev, "Timeout waiting for ANC disable\n");
267 return -ETIMEDOUT;
268 }
269
270 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800271 WM2000_ANC_ENG_IDLE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000272 dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
273 return -ETIMEDOUT;
274 }
275
276 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
277 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
278
279 wm2000->anc_mode = ANC_BYPASS;
280 dev_dbg(&i2c->dev, "bypass enabled\n");
281
282 return 0;
283}
284
285static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue)
286{
287 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
288
289 BUG_ON(wm2000->anc_mode != ANC_BYPASS);
290
291 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
292
293 if (analogue) {
294 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
295 WM2000_MODE_ANA_SEQ_INCLUDE |
296 WM2000_MODE_MOUSE_ENABLE |
297 WM2000_MODE_THERMAL_ENABLE);
298 } else {
299 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
300 WM2000_MODE_MOUSE_ENABLE |
301 WM2000_MODE_THERMAL_ENABLE);
302 }
303
304 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
305 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
306
307 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800308 WM2000_STATUS_MOUSE_ACTIVE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000309 dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
310 return -ETIMEDOUT;
311 }
312
313 wm2000->anc_mode = ANC_ACTIVE;
314 dev_dbg(&i2c->dev, "MOUSE active\n");
315
316 return 0;
317}
318
319static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
320{
321 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000322
323 BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
324
325 if (analogue) {
Mark Brown3ff1ec22012-06-08 06:53:48 +0800326 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
Mark Brown3a66d382010-02-11 13:27:19 +0000327
328 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
329 WM2000_MODE_ANA_SEQ_INCLUDE |
330 WM2000_MODE_THERMAL_ENABLE |
331 WM2000_MODE_STANDBY_ENTRY);
332 } else {
Mark Brown3a66d382010-02-11 13:27:19 +0000333 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
334 WM2000_MODE_THERMAL_ENABLE |
335 WM2000_MODE_STANDBY_ENTRY);
336 }
337
338 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800339 WM2000_STATUS_ANC_DISABLED)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000340 dev_err(&i2c->dev,
341 "Timed out waiting for ANC disable after 1ms\n");
342 return -ETIMEDOUT;
343 }
344
Mark Brown3ff1ec22012-06-08 06:53:48 +0800345 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000346 dev_err(&i2c->dev,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800347 "Timed out waiting for standby\n");
Mark Brown3a66d382010-02-11 13:27:19 +0000348 return -ETIMEDOUT;
349 }
350
351 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
352 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
353
354 wm2000->anc_mode = ANC_STANDBY;
355 dev_dbg(&i2c->dev, "standby\n");
356 if (analogue)
357 dev_dbg(&i2c->dev, "Analogue disabled\n");
358
359 return 0;
360}
361
362static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
363{
364 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000365
366 BUG_ON(wm2000->anc_mode != ANC_STANDBY);
367
368 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
369
370 if (analogue) {
Mark Brown3ff1ec22012-06-08 06:53:48 +0800371 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
Mark Brown3a66d382010-02-11 13:27:19 +0000372
373 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
374 WM2000_MODE_ANA_SEQ_INCLUDE |
375 WM2000_MODE_THERMAL_ENABLE |
376 WM2000_MODE_MOUSE_ENABLE);
377 } else {
Mark Brown3a66d382010-02-11 13:27:19 +0000378 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
379 WM2000_MODE_THERMAL_ENABLE |
380 WM2000_MODE_MOUSE_ENABLE);
381 }
382
383 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
384 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
385
386 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800387 WM2000_STATUS_MOUSE_ACTIVE)) {
388 dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
Mark Brown3a66d382010-02-11 13:27:19 +0000389 return -ETIMEDOUT;
390 }
391
392 wm2000->anc_mode = ANC_ACTIVE;
393 dev_dbg(&i2c->dev, "MOUSE active\n");
394 if (analogue)
395 dev_dbg(&i2c->dev, "Analogue enabled\n");
396
397 return 0;
398}
399
400typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
401
402static struct {
403 enum wm2000_anc_mode source;
404 enum wm2000_anc_mode dest;
405 int analogue;
406 wm2000_mode_fn step[2];
407} anc_transitions[] = {
408 {
409 .source = ANC_OFF,
410 .dest = ANC_ACTIVE,
411 .analogue = 1,
412 .step = {
413 wm2000_power_up,
414 },
415 },
416 {
417 .source = ANC_OFF,
418 .dest = ANC_STANDBY,
419 .step = {
420 wm2000_power_up,
421 wm2000_enter_standby,
422 },
423 },
424 {
425 .source = ANC_OFF,
426 .dest = ANC_BYPASS,
427 .analogue = 1,
428 .step = {
429 wm2000_power_up,
430 wm2000_enter_bypass,
431 },
432 },
433 {
434 .source = ANC_ACTIVE,
435 .dest = ANC_BYPASS,
436 .analogue = 1,
437 .step = {
438 wm2000_enter_bypass,
439 },
440 },
441 {
442 .source = ANC_ACTIVE,
443 .dest = ANC_STANDBY,
444 .analogue = 1,
445 .step = {
446 wm2000_enter_standby,
447 },
448 },
449 {
450 .source = ANC_ACTIVE,
451 .dest = ANC_OFF,
452 .analogue = 1,
453 .step = {
454 wm2000_power_down,
455 },
456 },
457 {
458 .source = ANC_BYPASS,
459 .dest = ANC_ACTIVE,
460 .analogue = 1,
461 .step = {
462 wm2000_exit_bypass,
463 },
464 },
465 {
466 .source = ANC_BYPASS,
467 .dest = ANC_STANDBY,
468 .analogue = 1,
469 .step = {
470 wm2000_exit_bypass,
471 wm2000_enter_standby,
472 },
473 },
474 {
475 .source = ANC_BYPASS,
476 .dest = ANC_OFF,
477 .step = {
478 wm2000_exit_bypass,
479 wm2000_power_down,
480 },
481 },
482 {
483 .source = ANC_STANDBY,
484 .dest = ANC_ACTIVE,
485 .analogue = 1,
486 .step = {
487 wm2000_exit_standby,
488 },
489 },
490 {
491 .source = ANC_STANDBY,
492 .dest = ANC_BYPASS,
493 .analogue = 1,
494 .step = {
495 wm2000_exit_standby,
496 wm2000_enter_bypass,
497 },
498 },
499 {
500 .source = ANC_STANDBY,
501 .dest = ANC_OFF,
502 .step = {
503 wm2000_exit_standby,
504 wm2000_power_down,
505 },
506 },
507};
508
509static int wm2000_anc_transition(struct wm2000_priv *wm2000,
510 enum wm2000_anc_mode mode)
511{
512 struct i2c_client *i2c = wm2000->i2c;
513 int i, j;
514 int ret;
515
516 if (wm2000->anc_mode == mode)
517 return 0;
518
519 for (i = 0; i < ARRAY_SIZE(anc_transitions); i++)
520 if (anc_transitions[i].source == wm2000->anc_mode &&
521 anc_transitions[i].dest == mode)
522 break;
523 if (i == ARRAY_SIZE(anc_transitions)) {
524 dev_err(&i2c->dev, "No transition for %d->%d\n",
525 wm2000->anc_mode, mode);
526 return -EINVAL;
527 }
528
529 for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) {
530 if (!anc_transitions[i].step[j])
531 break;
532 ret = anc_transitions[i].step[j](i2c,
533 anc_transitions[i].analogue);
534 if (ret != 0)
535 return ret;
536 }
537
538 return 0;
539}
540
541static int wm2000_anc_set_mode(struct wm2000_priv *wm2000)
542{
543 struct i2c_client *i2c = wm2000->i2c;
544 enum wm2000_anc_mode mode;
545
546 if (wm2000->anc_eng_ena && wm2000->spk_ena)
547 if (wm2000->anc_active)
548 mode = ANC_ACTIVE;
549 else
550 mode = ANC_BYPASS;
551 else
552 mode = ANC_STANDBY;
553
554 dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n",
555 mode, wm2000->anc_eng_ena, !wm2000->spk_ena,
556 wm2000->anc_active);
557
558 return wm2000_anc_transition(wm2000, mode);
559}
560
561static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
562 struct snd_ctl_elem_value *ucontrol)
563{
Mark Brown4911ccd2011-12-02 21:59:18 +0000564 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
565 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000566
567 ucontrol->value.enumerated.item[0] = wm2000->anc_active;
568
569 return 0;
570}
571
572static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
573 struct snd_ctl_elem_value *ucontrol)
574{
Mark Brown4911ccd2011-12-02 21:59:18 +0000575 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
576 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000577 int anc_active = ucontrol->value.enumerated.item[0];
578
579 if (anc_active > 1)
580 return -EINVAL;
581
582 wm2000->anc_active = anc_active;
583
584 return wm2000_anc_set_mode(wm2000);
585}
586
587static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
588 struct snd_ctl_elem_value *ucontrol)
589{
Mark Brown4911ccd2011-12-02 21:59:18 +0000590 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
591 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000592
593 ucontrol->value.enumerated.item[0] = wm2000->spk_ena;
594
595 return 0;
596}
597
598static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
599 struct snd_ctl_elem_value *ucontrol)
600{
Mark Brown4911ccd2011-12-02 21:59:18 +0000601 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
602 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000603 int val = ucontrol->value.enumerated.item[0];
604
605 if (val > 1)
606 return -EINVAL;
607
608 wm2000->spk_ena = val;
609
610 return wm2000_anc_set_mode(wm2000);
611}
612
613static const struct snd_kcontrol_new wm2000_controls[] = {
614 SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0,
615 wm2000_anc_mode_get,
616 wm2000_anc_mode_put),
617 SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0,
618 wm2000_speaker_get,
619 wm2000_speaker_put),
620};
621
622static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
623 struct snd_kcontrol *kcontrol, int event)
624{
Mark Brown4911ccd2011-12-02 21:59:18 +0000625 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
626 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000627
628 if (SND_SOC_DAPM_EVENT_ON(event))
629 wm2000->anc_eng_ena = 1;
630
631 if (SND_SOC_DAPM_EVENT_OFF(event))
632 wm2000->anc_eng_ena = 0;
633
634 return wm2000_anc_set_mode(wm2000);
635}
636
637static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
638/* Externally visible pins */
Mark Brown4911ccd2011-12-02 21:59:18 +0000639SND_SOC_DAPM_OUTPUT("SPKN"),
640SND_SOC_DAPM_OUTPUT("SPKP"),
Mark Brown3a66d382010-02-11 13:27:19 +0000641
Mark Brown4911ccd2011-12-02 21:59:18 +0000642SND_SOC_DAPM_INPUT("LINN"),
643SND_SOC_DAPM_INPUT("LINP"),
Mark Brown3a66d382010-02-11 13:27:19 +0000644
645SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0,
646 wm2000_anc_power_event,
647 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
648};
649
650/* Target, Path, Source */
Mark Brown4911ccd2011-12-02 21:59:18 +0000651static const struct snd_soc_dapm_route wm2000_audio_map[] = {
652 { "SPKN", NULL, "ANC Engine" },
653 { "SPKP", NULL, "ANC Engine" },
654 { "ANC Engine", NULL, "LINN" },
655 { "ANC Engine", NULL, "LINP" },
Mark Brown3a66d382010-02-11 13:27:19 +0000656};
657
Mark Brown4911ccd2011-12-02 21:59:18 +0000658#ifdef CONFIG_PM
659static int wm2000_suspend(struct snd_soc_codec *codec)
Mark Brown3a66d382010-02-11 13:27:19 +0000660{
Mark Brown4911ccd2011-12-02 21:59:18 +0000661 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000662
Mark Brown4911ccd2011-12-02 21:59:18 +0000663 return wm2000_anc_transition(wm2000, ANC_OFF);
Mark Brown3a66d382010-02-11 13:27:19 +0000664}
Mark Brown4911ccd2011-12-02 21:59:18 +0000665
666static int wm2000_resume(struct snd_soc_codec *codec)
667{
668 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
669
670 return wm2000_anc_set_mode(wm2000);
671}
672#else
673#define wm2000_suspend NULL
674#define wm2000_resume NULL
675#endif
Mark Brown3a66d382010-02-11 13:27:19 +0000676
Mark Brown51cc7ed2012-05-31 14:48:07 +0100677static bool wm2000_readable_reg(struct device *dev, unsigned int reg)
678{
679 switch (reg) {
680 case WM2000_REG_SYS_START:
681 case WM2000_REG_SPEECH_CLARITY:
682 case WM2000_REG_SYS_WATCHDOG:
683 case WM2000_REG_ANA_VMID_PD_TIME:
684 case WM2000_REG_ANA_VMID_PU_TIME:
685 case WM2000_REG_CAT_FLTR_INDX:
686 case WM2000_REG_CAT_GAIN_0:
687 case WM2000_REG_SYS_STATUS:
688 case WM2000_REG_SYS_MODE_CNTRL:
689 case WM2000_REG_SYS_START0:
690 case WM2000_REG_SYS_START1:
691 case WM2000_REG_ID1:
692 case WM2000_REG_ID2:
693 case WM2000_REG_REVISON:
694 case WM2000_REG_SYS_CTL1:
695 case WM2000_REG_SYS_CTL2:
696 case WM2000_REG_ANC_STAT:
697 case WM2000_REG_IF_CTL:
698 return true;
699 default:
700 return false;
701 }
702}
703
Mark Brown8aa1fe82011-12-02 21:57:19 +0000704static const struct regmap_config wm2000_regmap = {
Mark Brownd0e12f32012-09-26 11:57:30 +0100705 .reg_bits = 16,
Mark Brown8aa1fe82011-12-02 21:57:19 +0000706 .val_bits = 8,
Mark Brown51cc7ed2012-05-31 14:48:07 +0100707
708 .max_register = WM2000_REG_IF_CTL,
709 .readable_reg = wm2000_readable_reg,
Mark Brown8aa1fe82011-12-02 21:57:19 +0000710};
711
Mark Brown4911ccd2011-12-02 21:59:18 +0000712static int wm2000_probe(struct snd_soc_codec *codec)
713{
714 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
715
716 /* This will trigger a transition to standby mode by default */
717 wm2000_anc_set_mode(wm2000);
718
719 return 0;
720}
721
722static int wm2000_remove(struct snd_soc_codec *codec)
723{
724 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
725
726 return wm2000_anc_transition(wm2000, ANC_OFF);
727}
728
729static struct snd_soc_codec_driver soc_codec_dev_wm2000 = {
730 .probe = wm2000_probe,
731 .remove = wm2000_remove,
732 .suspend = wm2000_suspend,
733 .resume = wm2000_resume,
734
735 .dapm_widgets = wm2000_dapm_widgets,
736 .num_dapm_widgets = ARRAY_SIZE(wm2000_dapm_widgets),
737 .dapm_routes = wm2000_audio_map,
738 .num_dapm_routes = ARRAY_SIZE(wm2000_audio_map),
739 .controls = wm2000_controls,
740 .num_controls = ARRAY_SIZE(wm2000_controls),
741};
742
Mark Brown3a66d382010-02-11 13:27:19 +0000743static int __devinit wm2000_i2c_probe(struct i2c_client *i2c,
744 const struct i2c_device_id *i2c_id)
745{
746 struct wm2000_priv *wm2000;
747 struct wm2000_platform_data *pdata;
748 const char *filename;
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100749 const struct firmware *fw = NULL;
750 int ret;
751 int reg;
Mark Brown3a66d382010-02-11 13:27:19 +0000752 u16 id;
753
Mark Brownb03e96e2011-12-02 21:28:31 +0000754 wm2000 = devm_kzalloc(&i2c->dev, sizeof(struct wm2000_priv),
755 GFP_KERNEL);
Mark Brown3a66d382010-02-11 13:27:19 +0000756 if (wm2000 == NULL) {
757 dev_err(&i2c->dev, "Unable to allocate private data\n");
758 return -ENOMEM;
759 }
760
Mark Brown8aa1fe82011-12-02 21:57:19 +0000761 dev_set_drvdata(&i2c->dev, wm2000);
762
763 wm2000->regmap = regmap_init_i2c(i2c, &wm2000_regmap);
764 if (IS_ERR(wm2000->regmap)) {
765 ret = PTR_ERR(wm2000->regmap);
766 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
767 ret);
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100768 goto out;
Mark Brown8aa1fe82011-12-02 21:57:19 +0000769 }
770
Mark Brown3a66d382010-02-11 13:27:19 +0000771 /* Verify that this is a WM2000 */
772 reg = wm2000_read(i2c, WM2000_REG_ID1);
773 id = reg << 8;
774 reg = wm2000_read(i2c, WM2000_REG_ID2);
775 id |= reg & 0xff;
776
777 if (id != 0x2000) {
778 dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id);
779 ret = -ENODEV;
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100780 goto out_regmap_exit;
Mark Brown3a66d382010-02-11 13:27:19 +0000781 }
782
783 reg = wm2000_read(i2c, WM2000_REG_REVISON);
784 dev_info(&i2c->dev, "revision %c\n", reg + 'A');
785
786 filename = "wm2000_anc.bin";
787 pdata = dev_get_platdata(&i2c->dev);
788 if (pdata) {
789 wm2000->mclk_div = pdata->mclkdiv2;
790 wm2000->speech_clarity = !pdata->speech_enh_disable;
791
792 if (pdata->download_file)
793 filename = pdata->download_file;
794 }
795
796 ret = request_firmware(&fw, filename, &i2c->dev);
797 if (ret != 0) {
798 dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret);
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100799 goto out_regmap_exit;
Mark Brown3a66d382010-02-11 13:27:19 +0000800 }
801
802 /* Pre-cook the concatenation of the register address onto the image */
803 wm2000->anc_download_size = fw->size + 2;
Mark Brownb03e96e2011-12-02 21:28:31 +0000804 wm2000->anc_download = devm_kzalloc(&i2c->dev,
805 wm2000->anc_download_size,
806 GFP_KERNEL);
Mark Brown3a66d382010-02-11 13:27:19 +0000807 if (wm2000->anc_download == NULL) {
808 dev_err(&i2c->dev, "Out of memory\n");
809 ret = -ENOMEM;
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100810 goto out_regmap_exit;
Mark Brown3a66d382010-02-11 13:27:19 +0000811 }
812
813 wm2000->anc_download[0] = 0x80;
814 wm2000->anc_download[1] = 0x00;
815 memcpy(wm2000->anc_download + 2, fw->data, fw->size);
816
Mark Brown3a66d382010-02-11 13:27:19 +0000817 wm2000->anc_eng_ena = 1;
Mark Browne71fa372010-06-15 15:14:00 +0100818 wm2000->anc_active = 1;
819 wm2000->spk_ena = 1;
Mark Brown3a66d382010-02-11 13:27:19 +0000820 wm2000->i2c = i2c;
821
822 wm2000_reset(wm2000);
823
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100824 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_wm2000, NULL, 0);
825 if (!ret)
826 goto out;
Mark Brown3a66d382010-02-11 13:27:19 +0000827
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100828out_regmap_exit:
Mark Brown8aa1fe82011-12-02 21:57:19 +0000829 regmap_exit(wm2000->regmap);
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100830out:
831 release_firmware(fw);
Mark Brown3a66d382010-02-11 13:27:19 +0000832 return ret;
833}
834
835static __devexit int wm2000_i2c_remove(struct i2c_client *i2c)
836{
837 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
838
Mark Brown4911ccd2011-12-02 21:59:18 +0000839 snd_soc_unregister_codec(&i2c->dev);
Mark Brown8aa1fe82011-12-02 21:57:19 +0000840 regmap_exit(wm2000->regmap);
841
Mark Brown3a66d382010-02-11 13:27:19 +0000842 return 0;
843}
844
Mark Brown3a66d382010-02-11 13:27:19 +0000845static const struct i2c_device_id wm2000_i2c_id[] = {
846 { "wm2000", 0 },
847 { }
848};
849MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id);
850
851static struct i2c_driver wm2000_i2c_driver = {
852 .driver = {
853 .name = "wm2000",
854 .owner = THIS_MODULE,
855 },
856 .probe = wm2000_i2c_probe,
857 .remove = __devexit_p(wm2000_i2c_remove),
Mark Brown3a66d382010-02-11 13:27:19 +0000858 .id_table = wm2000_i2c_id,
859};
860
861static int __init wm2000_init(void)
862{
863 return i2c_add_driver(&wm2000_i2c_driver);
864}
865module_init(wm2000_init);
866
867static void __exit wm2000_exit(void)
868{
869 i2c_del_driver(&wm2000_i2c_driver);
870}
871module_exit(wm2000_exit);
872
873MODULE_DESCRIPTION("ASoC WM2000 driver");
874MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
875MODULE_LICENSE("GPL");