blob: 0aba8ce424caf880fd28391cc4854df86e1b28b6 [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>
Mark Brown514cfd62012-12-13 17:29:00 +090029#include <linux/clk.h>
Mark Brown3a66d382010-02-11 13:27:19 +000030#include <linux/delay.h>
31#include <linux/pm.h>
32#include <linux/i2c.h>
Mark Brown8aa1fe82011-12-02 21:57:19 +000033#include <linux/regmap.h>
Mark Brown3a66d382010-02-11 13:27:19 +000034#include <linux/debugfs.h>
Mark Browna89be932012-09-22 18:33:23 -040035#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Mark Brown3a66d382010-02-11 13:27:19 +000037#include <sound/core.h>
38#include <sound/pcm.h>
39#include <sound/pcm_params.h>
40#include <sound/soc.h>
Mark Brown3a66d382010-02-11 13:27:19 +000041#include <sound/initval.h>
42#include <sound/tlv.h>
43
44#include <sound/wm2000.h>
45
46#include "wm2000.h"
47
Mark Browna89be932012-09-22 18:33:23 -040048#define WM2000_NUM_SUPPLIES 3
49
50static const char *wm2000_supplies[WM2000_NUM_SUPPLIES] = {
51 "SPKVDD",
52 "DBVDD",
53 "DCVDD",
54};
55
Mark Brown3a66d382010-02-11 13:27:19 +000056enum 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;
Mark Brown8aa1fe82011-12-02 21:57:19 +000065 struct regmap *regmap;
Mark Brown514cfd62012-12-13 17:29:00 +090066 struct clk *mclk;
Mark Brown3a66d382010-02-11 13:27:19 +000067
Mark Browna89be932012-09-22 18:33:23 -040068 struct regulator_bulk_data supplies[WM2000_NUM_SUPPLIES];
69
Mark Brown3a66d382010-02-11 13:27:19 +000070 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
Mark Brown3a66d382010-02-11 13:27:19 +000083static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
84 unsigned int value)
85{
Mark Brown8aa1fe82011-12-02 21:57:19 +000086 struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
87 return regmap_write(wm2000->regmap, reg, value);
Mark Brown3a66d382010-02-11 13:27:19 +000088}
89
90static unsigned int wm2000_read(struct i2c_client *i2c, unsigned int r)
91{
Mark Brown8aa1fe82011-12-02 21:57:19 +000092 struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
93 unsigned int val;
Mark Brown3a66d382010-02-11 13:27:19 +000094 int ret;
95
Mark Brown8aa1fe82011-12-02 21:57:19 +000096 ret = regmap_read(wm2000->regmap, r, &val);
97 if (ret < 0)
98 return -1;
Mark Brown3a66d382010-02-11 13:27:19 +000099
Mark Brown8aa1fe82011-12-02 21:57:19 +0000100 return val;
Mark Brown3a66d382010-02-11 13:27:19 +0000101}
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,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800115 unsigned int reg, u8 mask)
Mark Brown3a66d382010-02-11 13:27:19 +0000116{
Mark Brown3ff1ec22012-06-08 06:53:48 +0800117 int timeout = 4000;
Mark Brown3a66d382010-02-11 13:27:19 +0000118 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);
Mark Brown3ff1ec22012-06-08 06:53:48 +0800136 int ret;
Mark Brown3a66d382010-02-11 13:27:19 +0000137
138 BUG_ON(wm2000->anc_mode != ANC_OFF);
139
140 dev_dbg(&i2c->dev, "Beginning power up\n");
141
Mark Browna89be932012-09-22 18:33:23 -0400142 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
Mark Brown3a66d382010-02-11 13:27:19 +0000148 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,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800163 WM2000_ANC_ENG_IDLE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000164 dev_err(&i2c->dev, "ANC engine failed to reset\n");
Mark Browna89be932012-09-22 18:33:23 -0400165 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
Mark Brown3a66d382010-02-11 13:27:19 +0000166 return -ETIMEDOUT;
167 }
168
169 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800170 WM2000_STATUS_BOOT_COMPLETE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000171 dev_err(&i2c->dev, "ANC engine failed to initialise\n");
Mark Browna89be932012-09-22 18:33:23 -0400172 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
Mark Brown3a66d382010-02-11 13:27:19 +0000173 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);
Mark Browna89be932012-09-22 18:33:23 -0400187 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
Mark Brown3a66d382010-02-11 13:27:19 +0000188 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);
Mark Browna89be932012-09-22 18:33:23 -0400193 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
Mark Brown3a66d382010-02-11 13:27:19 +0000194 return -EIO;
195 }
196
197 dev_dbg(&i2c->dev, "Download complete\n");
198
199 if (analogue) {
Mark Brown3ff1ec22012-06-08 06:53:48 +0800200 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
Mark Brown3a66d382010-02-11 13:27:19 +0000201
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 {
Mark Brown3a66d382010-02-11 13:27:19 +0000207 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,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800225 WM2000_STATUS_MOUSE_ACTIVE)) {
226 dev_err(&i2c->dev, "Timed out waiting for device\n");
Mark Browna89be932012-09-22 18:33:23 -0400227 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
Mark Brown3a66d382010-02-11 13:27:19 +0000228 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);
Mark Brown3a66d382010-02-11 13:27:19 +0000242
243 if (analogue) {
Mark Brown3ff1ec22012-06-08 06:53:48 +0800244 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
Mark Brown3a66d382010-02-11 13:27:19 +0000245 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
246 WM2000_MODE_ANA_SEQ_INCLUDE |
247 WM2000_MODE_POWER_DOWN);
248 } else {
Mark Brown3a66d382010-02-11 13:27:19 +0000249 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,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800254 WM2000_STATUS_POWER_DOWN_COMPLETE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000255 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,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800260 WM2000_ANC_ENG_IDLE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000261 dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
262 return -ETIMEDOUT;
263 }
264
Mark Browna89be932012-09-22 18:33:23 -0400265 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
266
Mark Brown3a66d382010-02-11 13:27:19 +0000267 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,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800291 WM2000_STATUS_ANC_DISABLED)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000292 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,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800297 WM2000_ANC_ENG_IDLE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000298 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,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800334 WM2000_STATUS_MOUSE_ACTIVE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000335 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);
Mark Brown3a66d382010-02-11 13:27:19 +0000348
349 BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
350
351 if (analogue) {
Mark Brown3ff1ec22012-06-08 06:53:48 +0800352 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
Mark Brown3a66d382010-02-11 13:27:19 +0000353
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 {
Mark Brown3a66d382010-02-11 13:27:19 +0000359 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,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800365 WM2000_STATUS_ANC_DISABLED)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000366 dev_err(&i2c->dev,
367 "Timed out waiting for ANC disable after 1ms\n");
368 return -ETIMEDOUT;
369 }
370
Mark Brown3ff1ec22012-06-08 06:53:48 +0800371 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000372 dev_err(&i2c->dev,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800373 "Timed out waiting for standby\n");
Mark Brown3a66d382010-02-11 13:27:19 +0000374 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);
Mark Brown3a66d382010-02-11 13:27:19 +0000391
392 BUG_ON(wm2000->anc_mode != ANC_STANDBY);
393
394 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
395
396 if (analogue) {
Mark Brown3ff1ec22012-06-08 06:53:48 +0800397 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
Mark Brown3a66d382010-02-11 13:27:19 +0000398
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 {
Mark Brown3a66d382010-02-11 13:27:19 +0000404 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,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800413 WM2000_STATUS_MOUSE_ACTIVE)) {
414 dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
Mark Brown3a66d382010-02-11 13:27:19 +0000415 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
Mark Brown514cfd62012-12-13 17:29:00 +0900555 /* 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
Mark Brown3a66d382010-02-11 13:27:19 +0000564 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
Mark Brown514cfd62012-12-13 17:29:00 +0900573 if (anc_transitions[i].dest == ANC_OFF)
574 clk_disable_unprepare(wm2000->mclk);
575
576 return ret;
Mark Brown3a66d382010-02-11 13:27:19 +0000577}
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{
Mark Brown4911ccd2011-12-02 21:59:18 +0000602 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
603 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000604
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{
Mark Brown4911ccd2011-12-02 21:59:18 +0000613 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
614 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000615 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{
Mark Brown4911ccd2011-12-02 21:59:18 +0000628 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
629 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000630
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{
Mark Brown4911ccd2011-12-02 21:59:18 +0000639 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
640 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000641 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{
Mark Browna5ccea22012-11-26 14:18:36 +0000663 struct snd_soc_codec *codec = w->codec;
Mark Brown4911ccd2011-12-02 21:59:18 +0000664 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000665
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 */
Mark Brown4911ccd2011-12-02 21:59:18 +0000677SND_SOC_DAPM_OUTPUT("SPKN"),
678SND_SOC_DAPM_OUTPUT("SPKP"),
Mark Brown3a66d382010-02-11 13:27:19 +0000679
Mark Brown4911ccd2011-12-02 21:59:18 +0000680SND_SOC_DAPM_INPUT("LINN"),
681SND_SOC_DAPM_INPUT("LINP"),
Mark Brown3a66d382010-02-11 13:27:19 +0000682
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 */
Mark Brown4911ccd2011-12-02 21:59:18 +0000689static 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" },
Mark Brown3a66d382010-02-11 13:27:19 +0000694};
695
Mark Brown4911ccd2011-12-02 21:59:18 +0000696#ifdef CONFIG_PM
697static int wm2000_suspend(struct snd_soc_codec *codec)
Mark Brown3a66d382010-02-11 13:27:19 +0000698{
Mark Brown4911ccd2011-12-02 21:59:18 +0000699 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000700
Mark Brown4911ccd2011-12-02 21:59:18 +0000701 return wm2000_anc_transition(wm2000, ANC_OFF);
Mark Brown3a66d382010-02-11 13:27:19 +0000702}
Mark Brown4911ccd2011-12-02 21:59:18 +0000703
704static int wm2000_resume(struct snd_soc_codec *codec)
705{
706 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
707
708 return wm2000_anc_set_mode(wm2000);
709}
710#else
711#define wm2000_suspend NULL
712#define wm2000_resume NULL
713#endif
Mark Brown3a66d382010-02-11 13:27:19 +0000714
Mark Brown51cc7ed2012-05-31 14:48:07 +0100715static 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
Mark Brown8aa1fe82011-12-02 21:57:19 +0000742static const struct regmap_config wm2000_regmap = {
Mark Brownd0e12f32012-09-26 11:57:30 +0100743 .reg_bits = 16,
Mark Brown8aa1fe82011-12-02 21:57:19 +0000744 .val_bits = 8,
Mark Brown51cc7ed2012-05-31 14:48:07 +0100745
746 .max_register = WM2000_REG_IF_CTL,
747 .readable_reg = wm2000_readable_reg,
Mark Brown8aa1fe82011-12-02 21:57:19 +0000748};
749
Mark Brown4911ccd2011-12-02 21:59:18 +0000750static 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
Bill Pemberton7a79e942012-12-07 09:26:37 -0500781static int wm2000_i2c_probe(struct i2c_client *i2c,
782 const struct i2c_device_id *i2c_id)
Mark Brown3a66d382010-02-11 13:27:19 +0000783{
784 struct wm2000_priv *wm2000;
785 struct wm2000_platform_data *pdata;
786 const char *filename;
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100787 const struct firmware *fw = NULL;
Mark Browna89be932012-09-22 18:33:23 -0400788 int ret, i;
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100789 int reg;
Mark Brown3a66d382010-02-11 13:27:19 +0000790 u16 id;
791
Mark Brownb03e96e2011-12-02 21:28:31 +0000792 wm2000 = devm_kzalloc(&i2c->dev, sizeof(struct wm2000_priv),
793 GFP_KERNEL);
Mark Brown3a66d382010-02-11 13:27:19 +0000794 if (wm2000 == NULL) {
795 dev_err(&i2c->dev, "Unable to allocate private data\n");
796 return -ENOMEM;
797 }
798
Mark Brown8aa1fe82011-12-02 21:57:19 +0000799 dev_set_drvdata(&i2c->dev, wm2000);
800
Mark Brown8fed54a2012-09-22 18:32:08 -0400801 wm2000->regmap = devm_regmap_init_i2c(i2c, &wm2000_regmap);
Mark Brown8aa1fe82011-12-02 21:57:19 +0000802 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);
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100806 goto out;
Mark Brown8aa1fe82011-12-02 21:57:19 +0000807 }
808
Mark Browna89be932012-09-22 18:33:23 -0400809 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
Mark Brown3a66d382010-02-11 13:27:19 +0000825 /* 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;
Mark Browna89be932012-09-22 18:33:23 -0400834 goto err_supplies;
Mark Brown3a66d382010-02-11 13:27:19 +0000835 }
836
837 reg = wm2000_read(i2c, WM2000_REG_REVISON);
838 dev_info(&i2c->dev, "revision %c\n", reg + 'A');
839
Mark Brown514cfd62012-12-13 17:29:00 +0900840 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
Mark Brown3a66d382010-02-11 13:27:19 +0000847 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);
Mark Browna89be932012-09-22 18:33:23 -0400860 goto err_supplies;
Mark Brown3a66d382010-02-11 13:27:19 +0000861 }
862
863 /* Pre-cook the concatenation of the register address onto the image */
864 wm2000->anc_download_size = fw->size + 2;
Mark Brownb03e96e2011-12-02 21:28:31 +0000865 wm2000->anc_download = devm_kzalloc(&i2c->dev,
866 wm2000->anc_download_size,
867 GFP_KERNEL);
Mark Brown3a66d382010-02-11 13:27:19 +0000868 if (wm2000->anc_download == NULL) {
869 dev_err(&i2c->dev, "Out of memory\n");
870 ret = -ENOMEM;
Mark Browna89be932012-09-22 18:33:23 -0400871 goto err_supplies;
Mark Brown3a66d382010-02-11 13:27:19 +0000872 }
873
874 wm2000->anc_download[0] = 0x80;
875 wm2000->anc_download[1] = 0x00;
876 memcpy(wm2000->anc_download + 2, fw->data, fw->size);
877
Mark Brown3a66d382010-02-11 13:27:19 +0000878 wm2000->anc_eng_ena = 1;
Mark Browne71fa372010-06-15 15:14:00 +0100879 wm2000->anc_active = 1;
880 wm2000->spk_ena = 1;
Mark Brown3a66d382010-02-11 13:27:19 +0000881 wm2000->i2c = i2c;
882
883 wm2000_reset(wm2000);
884
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100885 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_wm2000, NULL, 0);
Mark Brown3a66d382010-02-11 13:27:19 +0000886
Mark Browna89be932012-09-22 18:33:23 -0400887err_supplies:
888 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
Mark Brown3a66d382010-02-11 13:27:19 +0000889
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100890out:
891 release_firmware(fw);
Mark Brown3a66d382010-02-11 13:27:19 +0000892 return ret;
893}
894
Bill Pemberton7a79e942012-12-07 09:26:37 -0500895static int wm2000_i2c_remove(struct i2c_client *i2c)
Mark Brown3a66d382010-02-11 13:27:19 +0000896{
Mark Brown4911ccd2011-12-02 21:59:18 +0000897 snd_soc_unregister_codec(&i2c->dev);
Mark Brown8aa1fe82011-12-02 21:57:19 +0000898
Mark Brown3a66d382010-02-11 13:27:19 +0000899 return 0;
900}
901
Mark Brown3a66d382010-02-11 13:27:19 +0000902static 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,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500914 .remove = wm2000_i2c_remove,
Mark Brown3a66d382010-02-11 13:27:19 +0000915 .id_table = wm2000_i2c_id,
916};
917
Sachin Kamat3a4bfd82012-08-06 17:25:54 +0530918module_i2c_driver(wm2000_i2c_driver);
Mark Brown3a66d382010-02-11 13:27:19 +0000919
920MODULE_DESCRIPTION("ASoC WM2000 driver");
921MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
922MODULE_LICENSE("GPL");