blob: f2ac38b61a1bc15b8b583915069c6a09fe870108 [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
Mark Brown3a66d382010-02-11 13:27:19 +000076 unsigned int speech_clarity:1;
77
78 int anc_download_size;
79 char *anc_download;
Mark Brown8e9bb422013-01-23 18:38:54 +080080
81 struct mutex lock;
Mark Brown3a66d382010-02-11 13:27:19 +000082};
83
Mark Brown3a66d382010-02-11 13:27:19 +000084static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
85 unsigned int value)
86{
Mark Brown8aa1fe82011-12-02 21:57:19 +000087 struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
88 return regmap_write(wm2000->regmap, reg, value);
Mark Brown3a66d382010-02-11 13:27:19 +000089}
90
91static unsigned int wm2000_read(struct i2c_client *i2c, unsigned int r)
92{
Mark Brown8aa1fe82011-12-02 21:57:19 +000093 struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
94 unsigned int val;
Mark Brown3a66d382010-02-11 13:27:19 +000095 int ret;
96
Mark Brown8aa1fe82011-12-02 21:57:19 +000097 ret = regmap_read(wm2000->regmap, r, &val);
98 if (ret < 0)
99 return -1;
Mark Brown3a66d382010-02-11 13:27:19 +0000100
Mark Brown8aa1fe82011-12-02 21:57:19 +0000101 return val;
Mark Brown3a66d382010-02-11 13:27:19 +0000102}
103
104static void wm2000_reset(struct wm2000_priv *wm2000)
105{
106 struct i2c_client *i2c = wm2000->i2c;
107
108 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
109 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
110 wm2000_write(i2c, WM2000_REG_ID1, 0);
111
112 wm2000->anc_mode = ANC_OFF;
113}
114
115static int wm2000_poll_bit(struct i2c_client *i2c,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800116 unsigned int reg, u8 mask)
Mark Brown3a66d382010-02-11 13:27:19 +0000117{
Mark Brown3ff1ec22012-06-08 06:53:48 +0800118 int timeout = 4000;
Mark Brown3a66d382010-02-11 13:27:19 +0000119 int val;
120
121 val = wm2000_read(i2c, reg);
122
123 while (!(val & mask) && --timeout) {
124 msleep(1);
125 val = wm2000_read(i2c, reg);
126 }
127
128 if (timeout == 0)
129 return 0;
130 else
131 return 1;
132}
133
134static int wm2000_power_up(struct i2c_client *i2c, int analogue)
135{
136 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
Mark Brownd61100b2012-12-14 15:16:58 +0900137 unsigned long rate;
Mark Brown3ff1ec22012-06-08 06:53:48 +0800138 int ret;
Mark Brown3a66d382010-02-11 13:27:19 +0000139
140 BUG_ON(wm2000->anc_mode != ANC_OFF);
141
142 dev_dbg(&i2c->dev, "Beginning power up\n");
143
Mark Browna89be932012-09-22 18:33:23 -0400144 ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies);
145 if (ret != 0) {
146 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
147 return ret;
148 }
149
Mark Brownd61100b2012-12-14 15:16:58 +0900150 rate = clk_get_rate(wm2000->mclk);
151 if (rate <= 13500000) {
Mark Brown3a66d382010-02-11 13:27:19 +0000152 dev_dbg(&i2c->dev, "Disabling MCLK divider\n");
153 wm2000_write(i2c, WM2000_REG_SYS_CTL2,
154 WM2000_MCLK_DIV2_ENA_CLR);
155 } else {
156 dev_dbg(&i2c->dev, "Enabling MCLK divider\n");
157 wm2000_write(i2c, WM2000_REG_SYS_CTL2,
158 WM2000_MCLK_DIV2_ENA_SET);
159 }
160
161 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
162 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET);
163
164 /* Wait for ANC engine to become ready */
165 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800166 WM2000_ANC_ENG_IDLE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000167 dev_err(&i2c->dev, "ANC engine failed to reset\n");
Mark Browna89be932012-09-22 18:33:23 -0400168 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
Mark Brown3a66d382010-02-11 13:27:19 +0000169 return -ETIMEDOUT;
170 }
171
172 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800173 WM2000_STATUS_BOOT_COMPLETE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000174 dev_err(&i2c->dev, "ANC engine failed to initialise\n");
Mark Browna89be932012-09-22 18:33:23 -0400175 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
Mark Brown3a66d382010-02-11 13:27:19 +0000176 return -ETIMEDOUT;
177 }
178
179 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
180
181 /* Open code download of the data since it is the only bulk
182 * write we do. */
183 dev_dbg(&i2c->dev, "Downloading %d bytes\n",
184 wm2000->anc_download_size - 2);
185
186 ret = i2c_master_send(i2c, wm2000->anc_download,
187 wm2000->anc_download_size);
188 if (ret < 0) {
189 dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret);
Mark Browna89be932012-09-22 18:33:23 -0400190 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
Mark Brown3a66d382010-02-11 13:27:19 +0000191 return ret;
192 }
193 if (ret != wm2000->anc_download_size) {
194 dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n",
195 ret, wm2000->anc_download_size);
Mark Browna89be932012-09-22 18:33:23 -0400196 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
Mark Brown3a66d382010-02-11 13:27:19 +0000197 return -EIO;
198 }
199
200 dev_dbg(&i2c->dev, "Download complete\n");
201
202 if (analogue) {
Mark Brown3ff1ec22012-06-08 06:53:48 +0800203 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
Mark Brown3a66d382010-02-11 13:27:19 +0000204
205 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
206 WM2000_MODE_ANA_SEQ_INCLUDE |
207 WM2000_MODE_MOUSE_ENABLE |
208 WM2000_MODE_THERMAL_ENABLE);
209 } else {
Mark Brown3a66d382010-02-11 13:27:19 +0000210 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
211 WM2000_MODE_MOUSE_ENABLE |
212 WM2000_MODE_THERMAL_ENABLE);
213 }
214
215 ret = wm2000_read(i2c, WM2000_REG_SPEECH_CLARITY);
216 if (wm2000->speech_clarity)
Mark Brown3a66d382010-02-11 13:27:19 +0000217 ret |= WM2000_SPEECH_CLARITY;
Mark Brown267f8fa2013-01-04 21:18:12 +0000218 else
219 ret &= ~WM2000_SPEECH_CLARITY;
Mark Brown3a66d382010-02-11 13:27:19 +0000220 wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, ret);
221
222 wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33);
223 wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02);
224
225 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
226
227 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800228 WM2000_STATUS_MOUSE_ACTIVE)) {
229 dev_err(&i2c->dev, "Timed out waiting for device\n");
Mark Browna89be932012-09-22 18:33:23 -0400230 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
Mark Brown3a66d382010-02-11 13:27:19 +0000231 return -ETIMEDOUT;
232 }
233
234 dev_dbg(&i2c->dev, "ANC active\n");
235 if (analogue)
236 dev_dbg(&i2c->dev, "Analogue active\n");
237 wm2000->anc_mode = ANC_ACTIVE;
238
239 return 0;
240}
241
242static int wm2000_power_down(struct i2c_client *i2c, int analogue)
243{
244 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000245
246 if (analogue) {
Mark Brown3ff1ec22012-06-08 06:53:48 +0800247 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
Mark Brown3a66d382010-02-11 13:27:19 +0000248 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
249 WM2000_MODE_ANA_SEQ_INCLUDE |
250 WM2000_MODE_POWER_DOWN);
251 } else {
Mark Brown3a66d382010-02-11 13:27:19 +0000252 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
253 WM2000_MODE_POWER_DOWN);
254 }
255
256 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800257 WM2000_STATUS_POWER_DOWN_COMPLETE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000258 dev_err(&i2c->dev, "Timeout waiting for ANC power down\n");
259 return -ETIMEDOUT;
260 }
261
262 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800263 WM2000_ANC_ENG_IDLE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000264 dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
265 return -ETIMEDOUT;
266 }
267
Mark Browna89be932012-09-22 18:33:23 -0400268 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
269
Mark Brown3a66d382010-02-11 13:27:19 +0000270 dev_dbg(&i2c->dev, "powered off\n");
271 wm2000->anc_mode = ANC_OFF;
272
273 return 0;
274}
275
276static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue)
277{
278 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
279
280 BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
281
282 if (analogue) {
283 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
284 WM2000_MODE_ANA_SEQ_INCLUDE |
285 WM2000_MODE_THERMAL_ENABLE |
286 WM2000_MODE_BYPASS_ENTRY);
287 } else {
288 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
289 WM2000_MODE_THERMAL_ENABLE |
290 WM2000_MODE_BYPASS_ENTRY);
291 }
292
293 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800294 WM2000_STATUS_ANC_DISABLED)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000295 dev_err(&i2c->dev, "Timeout waiting for ANC disable\n");
296 return -ETIMEDOUT;
297 }
298
299 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800300 WM2000_ANC_ENG_IDLE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000301 dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
302 return -ETIMEDOUT;
303 }
304
305 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
306 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
307
308 wm2000->anc_mode = ANC_BYPASS;
309 dev_dbg(&i2c->dev, "bypass enabled\n");
310
311 return 0;
312}
313
314static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue)
315{
316 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
317
318 BUG_ON(wm2000->anc_mode != ANC_BYPASS);
319
320 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
321
322 if (analogue) {
323 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
324 WM2000_MODE_ANA_SEQ_INCLUDE |
325 WM2000_MODE_MOUSE_ENABLE |
326 WM2000_MODE_THERMAL_ENABLE);
327 } else {
328 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
329 WM2000_MODE_MOUSE_ENABLE |
330 WM2000_MODE_THERMAL_ENABLE);
331 }
332
333 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
334 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
335
336 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800337 WM2000_STATUS_MOUSE_ACTIVE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000338 dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
339 return -ETIMEDOUT;
340 }
341
342 wm2000->anc_mode = ANC_ACTIVE;
343 dev_dbg(&i2c->dev, "MOUSE active\n");
344
345 return 0;
346}
347
348static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
349{
350 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000351
352 BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
353
354 if (analogue) {
Mark Brown3ff1ec22012-06-08 06:53:48 +0800355 wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
Mark Brown3a66d382010-02-11 13:27:19 +0000356
357 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
358 WM2000_MODE_ANA_SEQ_INCLUDE |
359 WM2000_MODE_THERMAL_ENABLE |
360 WM2000_MODE_STANDBY_ENTRY);
361 } else {
Mark Brown3a66d382010-02-11 13:27:19 +0000362 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
363 WM2000_MODE_THERMAL_ENABLE |
364 WM2000_MODE_STANDBY_ENTRY);
365 }
366
367 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800368 WM2000_STATUS_ANC_DISABLED)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000369 dev_err(&i2c->dev,
370 "Timed out waiting for ANC disable after 1ms\n");
371 return -ETIMEDOUT;
372 }
373
Mark Brown3ff1ec22012-06-08 06:53:48 +0800374 if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE)) {
Mark Brown3a66d382010-02-11 13:27:19 +0000375 dev_err(&i2c->dev,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800376 "Timed out waiting for standby\n");
Mark Brown3a66d382010-02-11 13:27:19 +0000377 return -ETIMEDOUT;
378 }
379
380 wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
381 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
382
383 wm2000->anc_mode = ANC_STANDBY;
384 dev_dbg(&i2c->dev, "standby\n");
385 if (analogue)
386 dev_dbg(&i2c->dev, "Analogue disabled\n");
387
388 return 0;
389}
390
391static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
392{
393 struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000394
395 BUG_ON(wm2000->anc_mode != ANC_STANDBY);
396
397 wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
398
399 if (analogue) {
Mark Brown3ff1ec22012-06-08 06:53:48 +0800400 wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
Mark Brown3a66d382010-02-11 13:27:19 +0000401
402 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
403 WM2000_MODE_ANA_SEQ_INCLUDE |
404 WM2000_MODE_THERMAL_ENABLE |
405 WM2000_MODE_MOUSE_ENABLE);
406 } else {
Mark Brown3a66d382010-02-11 13:27:19 +0000407 wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
408 WM2000_MODE_THERMAL_ENABLE |
409 WM2000_MODE_MOUSE_ENABLE);
410 }
411
412 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
413 wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
414
415 if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
Mark Brown3ff1ec22012-06-08 06:53:48 +0800416 WM2000_STATUS_MOUSE_ACTIVE)) {
417 dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
Mark Brown3a66d382010-02-11 13:27:19 +0000418 return -ETIMEDOUT;
419 }
420
421 wm2000->anc_mode = ANC_ACTIVE;
422 dev_dbg(&i2c->dev, "MOUSE active\n");
423 if (analogue)
424 dev_dbg(&i2c->dev, "Analogue enabled\n");
425
426 return 0;
427}
428
429typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
430
431static struct {
432 enum wm2000_anc_mode source;
433 enum wm2000_anc_mode dest;
434 int analogue;
435 wm2000_mode_fn step[2];
436} anc_transitions[] = {
437 {
438 .source = ANC_OFF,
439 .dest = ANC_ACTIVE,
440 .analogue = 1,
441 .step = {
442 wm2000_power_up,
443 },
444 },
445 {
446 .source = ANC_OFF,
447 .dest = ANC_STANDBY,
448 .step = {
449 wm2000_power_up,
450 wm2000_enter_standby,
451 },
452 },
453 {
454 .source = ANC_OFF,
455 .dest = ANC_BYPASS,
456 .analogue = 1,
457 .step = {
458 wm2000_power_up,
459 wm2000_enter_bypass,
460 },
461 },
462 {
463 .source = ANC_ACTIVE,
464 .dest = ANC_BYPASS,
465 .analogue = 1,
466 .step = {
467 wm2000_enter_bypass,
468 },
469 },
470 {
471 .source = ANC_ACTIVE,
472 .dest = ANC_STANDBY,
473 .analogue = 1,
474 .step = {
475 wm2000_enter_standby,
476 },
477 },
478 {
479 .source = ANC_ACTIVE,
480 .dest = ANC_OFF,
481 .analogue = 1,
482 .step = {
483 wm2000_power_down,
484 },
485 },
486 {
487 .source = ANC_BYPASS,
488 .dest = ANC_ACTIVE,
489 .analogue = 1,
490 .step = {
491 wm2000_exit_bypass,
492 },
493 },
494 {
495 .source = ANC_BYPASS,
496 .dest = ANC_STANDBY,
497 .analogue = 1,
498 .step = {
499 wm2000_exit_bypass,
500 wm2000_enter_standby,
501 },
502 },
503 {
504 .source = ANC_BYPASS,
505 .dest = ANC_OFF,
506 .step = {
507 wm2000_exit_bypass,
508 wm2000_power_down,
509 },
510 },
511 {
512 .source = ANC_STANDBY,
513 .dest = ANC_ACTIVE,
514 .analogue = 1,
515 .step = {
516 wm2000_exit_standby,
517 },
518 },
519 {
520 .source = ANC_STANDBY,
521 .dest = ANC_BYPASS,
522 .analogue = 1,
523 .step = {
524 wm2000_exit_standby,
525 wm2000_enter_bypass,
526 },
527 },
528 {
529 .source = ANC_STANDBY,
530 .dest = ANC_OFF,
531 .step = {
532 wm2000_exit_standby,
533 wm2000_power_down,
534 },
535 },
536};
537
538static int wm2000_anc_transition(struct wm2000_priv *wm2000,
539 enum wm2000_anc_mode mode)
540{
541 struct i2c_client *i2c = wm2000->i2c;
542 int i, j;
543 int ret;
544
545 if (wm2000->anc_mode == mode)
546 return 0;
547
548 for (i = 0; i < ARRAY_SIZE(anc_transitions); i++)
549 if (anc_transitions[i].source == wm2000->anc_mode &&
550 anc_transitions[i].dest == mode)
551 break;
552 if (i == ARRAY_SIZE(anc_transitions)) {
553 dev_err(&i2c->dev, "No transition for %d->%d\n",
554 wm2000->anc_mode, mode);
555 return -EINVAL;
556 }
557
Mark Brown514cfd62012-12-13 17:29:00 +0900558 /* Maintain clock while active */
559 if (anc_transitions[i].source == ANC_OFF) {
560 ret = clk_prepare_enable(wm2000->mclk);
561 if (ret != 0) {
562 dev_err(&i2c->dev, "Failed to enable MCLK: %d\n", ret);
563 return ret;
564 }
565 }
566
Mark Brown3a66d382010-02-11 13:27:19 +0000567 for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) {
568 if (!anc_transitions[i].step[j])
569 break;
570 ret = anc_transitions[i].step[j](i2c,
571 anc_transitions[i].analogue);
572 if (ret != 0)
573 return ret;
574 }
575
Mark Brown514cfd62012-12-13 17:29:00 +0900576 if (anc_transitions[i].dest == ANC_OFF)
577 clk_disable_unprepare(wm2000->mclk);
578
579 return ret;
Mark Brown3a66d382010-02-11 13:27:19 +0000580}
581
582static int wm2000_anc_set_mode(struct wm2000_priv *wm2000)
583{
584 struct i2c_client *i2c = wm2000->i2c;
585 enum wm2000_anc_mode mode;
586
587 if (wm2000->anc_eng_ena && wm2000->spk_ena)
588 if (wm2000->anc_active)
589 mode = ANC_ACTIVE;
590 else
591 mode = ANC_BYPASS;
592 else
593 mode = ANC_STANDBY;
594
595 dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n",
596 mode, wm2000->anc_eng_ena, !wm2000->spk_ena,
597 wm2000->anc_active);
598
599 return wm2000_anc_transition(wm2000, mode);
600}
601
602static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
603 struct snd_ctl_elem_value *ucontrol)
604{
Mark Brown4911ccd2011-12-02 21:59:18 +0000605 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
606 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000607
608 ucontrol->value.enumerated.item[0] = wm2000->anc_active;
609
610 return 0;
611}
612
613static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
614 struct snd_ctl_elem_value *ucontrol)
615{
Mark Brown4911ccd2011-12-02 21:59:18 +0000616 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
617 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000618 int anc_active = ucontrol->value.enumerated.item[0];
Mark Brown8e9bb422013-01-23 18:38:54 +0800619 int ret;
Mark Brown3a66d382010-02-11 13:27:19 +0000620
621 if (anc_active > 1)
622 return -EINVAL;
623
Mark Brown8e9bb422013-01-23 18:38:54 +0800624 mutex_lock(&wm2000->lock);
625
Mark Brown3a66d382010-02-11 13:27:19 +0000626 wm2000->anc_active = anc_active;
627
Mark Brown8e9bb422013-01-23 18:38:54 +0800628 ret = wm2000_anc_set_mode(wm2000);
629
630 mutex_unlock(&wm2000->lock);
631
632 return ret;
Mark Brown3a66d382010-02-11 13:27:19 +0000633}
634
635static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
636 struct snd_ctl_elem_value *ucontrol)
637{
Mark Brown4911ccd2011-12-02 21:59:18 +0000638 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
639 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000640
641 ucontrol->value.enumerated.item[0] = wm2000->spk_ena;
642
643 return 0;
644}
645
646static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
647 struct snd_ctl_elem_value *ucontrol)
648{
Mark Brown4911ccd2011-12-02 21:59:18 +0000649 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
650 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000651 int val = ucontrol->value.enumerated.item[0];
Mark Brown8e9bb422013-01-23 18:38:54 +0800652 int ret;
Mark Brown3a66d382010-02-11 13:27:19 +0000653
654 if (val > 1)
655 return -EINVAL;
656
Mark Brown8e9bb422013-01-23 18:38:54 +0800657 mutex_lock(&wm2000->lock);
658
Mark Brown3a66d382010-02-11 13:27:19 +0000659 wm2000->spk_ena = val;
660
Mark Brown8e9bb422013-01-23 18:38:54 +0800661 ret = wm2000_anc_set_mode(wm2000);
662
663 mutex_unlock(&wm2000->lock);
664
665 return ret;
Mark Brown3a66d382010-02-11 13:27:19 +0000666}
667
668static const struct snd_kcontrol_new wm2000_controls[] = {
Mark Brown3f3af6e2013-01-30 21:35:44 +0800669 SOC_SINGLE("ANC Volume", WM2000_REG_ANC_GAIN_CTRL, 0, 255, 0),
Mark Brown3a66d382010-02-11 13:27:19 +0000670 SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0,
671 wm2000_anc_mode_get,
672 wm2000_anc_mode_put),
673 SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0,
674 wm2000_speaker_get,
675 wm2000_speaker_put),
676};
677
678static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
679 struct snd_kcontrol *kcontrol, int event)
680{
Mark Browna5ccea22012-11-26 14:18:36 +0000681 struct snd_soc_codec *codec = w->codec;
Mark Brown4911ccd2011-12-02 21:59:18 +0000682 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown8e9bb422013-01-23 18:38:54 +0800683 int ret;
684
685 mutex_lock(&wm2000->lock);
Mark Brown3a66d382010-02-11 13:27:19 +0000686
687 if (SND_SOC_DAPM_EVENT_ON(event))
688 wm2000->anc_eng_ena = 1;
689
690 if (SND_SOC_DAPM_EVENT_OFF(event))
691 wm2000->anc_eng_ena = 0;
692
Mark Brown8e9bb422013-01-23 18:38:54 +0800693 ret = wm2000_anc_set_mode(wm2000);
694
695 mutex_unlock(&wm2000->lock);
696
697 return ret;
Mark Brown3a66d382010-02-11 13:27:19 +0000698}
699
700static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
701/* Externally visible pins */
Mark Brown4911ccd2011-12-02 21:59:18 +0000702SND_SOC_DAPM_OUTPUT("SPKN"),
703SND_SOC_DAPM_OUTPUT("SPKP"),
Mark Brown3a66d382010-02-11 13:27:19 +0000704
Mark Brown4911ccd2011-12-02 21:59:18 +0000705SND_SOC_DAPM_INPUT("LINN"),
706SND_SOC_DAPM_INPUT("LINP"),
Mark Brown3a66d382010-02-11 13:27:19 +0000707
708SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0,
709 wm2000_anc_power_event,
710 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
711};
712
713/* Target, Path, Source */
Mark Brown4911ccd2011-12-02 21:59:18 +0000714static const struct snd_soc_dapm_route wm2000_audio_map[] = {
715 { "SPKN", NULL, "ANC Engine" },
716 { "SPKP", NULL, "ANC Engine" },
717 { "ANC Engine", NULL, "LINN" },
718 { "ANC Engine", NULL, "LINP" },
Mark Brown3a66d382010-02-11 13:27:19 +0000719};
720
Mark Brown4911ccd2011-12-02 21:59:18 +0000721#ifdef CONFIG_PM
722static int wm2000_suspend(struct snd_soc_codec *codec)
Mark Brown3a66d382010-02-11 13:27:19 +0000723{
Mark Brown4911ccd2011-12-02 21:59:18 +0000724 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
Mark Brown3a66d382010-02-11 13:27:19 +0000725
Mark Brown4911ccd2011-12-02 21:59:18 +0000726 return wm2000_anc_transition(wm2000, ANC_OFF);
Mark Brown3a66d382010-02-11 13:27:19 +0000727}
Mark Brown4911ccd2011-12-02 21:59:18 +0000728
729static int wm2000_resume(struct snd_soc_codec *codec)
730{
731 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
732
733 return wm2000_anc_set_mode(wm2000);
734}
735#else
736#define wm2000_suspend NULL
737#define wm2000_resume NULL
738#endif
Mark Brown3a66d382010-02-11 13:27:19 +0000739
Mark Brown51cc7ed2012-05-31 14:48:07 +0100740static bool wm2000_readable_reg(struct device *dev, unsigned int reg)
741{
742 switch (reg) {
743 case WM2000_REG_SYS_START:
Mark Brown33e75462013-01-22 15:51:08 +0900744 case WM2000_REG_ANC_GAIN_CTRL:
745 case WM2000_REG_MSE_TH1:
746 case WM2000_REG_MSE_TH2:
Mark Brown51cc7ed2012-05-31 14:48:07 +0100747 case WM2000_REG_SPEECH_CLARITY:
748 case WM2000_REG_SYS_WATCHDOG:
749 case WM2000_REG_ANA_VMID_PD_TIME:
750 case WM2000_REG_ANA_VMID_PU_TIME:
751 case WM2000_REG_CAT_FLTR_INDX:
752 case WM2000_REG_CAT_GAIN_0:
753 case WM2000_REG_SYS_STATUS:
754 case WM2000_REG_SYS_MODE_CNTRL:
755 case WM2000_REG_SYS_START0:
756 case WM2000_REG_SYS_START1:
757 case WM2000_REG_ID1:
758 case WM2000_REG_ID2:
759 case WM2000_REG_REVISON:
760 case WM2000_REG_SYS_CTL1:
761 case WM2000_REG_SYS_CTL2:
762 case WM2000_REG_ANC_STAT:
763 case WM2000_REG_IF_CTL:
764 return true;
765 default:
766 return false;
767 }
768}
769
Mark Brown8aa1fe82011-12-02 21:57:19 +0000770static const struct regmap_config wm2000_regmap = {
Mark Brownd0e12f32012-09-26 11:57:30 +0100771 .reg_bits = 16,
Mark Brown8aa1fe82011-12-02 21:57:19 +0000772 .val_bits = 8,
Mark Brown51cc7ed2012-05-31 14:48:07 +0100773
774 .max_register = WM2000_REG_IF_CTL,
775 .readable_reg = wm2000_readable_reg,
Mark Brown8aa1fe82011-12-02 21:57:19 +0000776};
777
Mark Brown4911ccd2011-12-02 21:59:18 +0000778static int wm2000_probe(struct snd_soc_codec *codec)
779{
780 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
781
Mark Brown3f3af6e2013-01-30 21:35:44 +0800782 snd_soc_codec_set_cache_io(codec, 16, 8, SND_SOC_REGMAP);
783
Mark Brown4911ccd2011-12-02 21:59:18 +0000784 /* This will trigger a transition to standby mode by default */
785 wm2000_anc_set_mode(wm2000);
786
787 return 0;
788}
789
790static int wm2000_remove(struct snd_soc_codec *codec)
791{
792 struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
793
794 return wm2000_anc_transition(wm2000, ANC_OFF);
795}
796
797static struct snd_soc_codec_driver soc_codec_dev_wm2000 = {
798 .probe = wm2000_probe,
799 .remove = wm2000_remove,
800 .suspend = wm2000_suspend,
801 .resume = wm2000_resume,
802
803 .dapm_widgets = wm2000_dapm_widgets,
804 .num_dapm_widgets = ARRAY_SIZE(wm2000_dapm_widgets),
805 .dapm_routes = wm2000_audio_map,
806 .num_dapm_routes = ARRAY_SIZE(wm2000_audio_map),
807 .controls = wm2000_controls,
808 .num_controls = ARRAY_SIZE(wm2000_controls),
809};
810
Bill Pemberton7a79e942012-12-07 09:26:37 -0500811static int wm2000_i2c_probe(struct i2c_client *i2c,
812 const struct i2c_device_id *i2c_id)
Mark Brown3a66d382010-02-11 13:27:19 +0000813{
814 struct wm2000_priv *wm2000;
815 struct wm2000_platform_data *pdata;
816 const char *filename;
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100817 const struct firmware *fw = NULL;
Mark Browna89be932012-09-22 18:33:23 -0400818 int ret, i;
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100819 int reg;
Mark Brown3a66d382010-02-11 13:27:19 +0000820 u16 id;
821
Mark Brownb03e96e2011-12-02 21:28:31 +0000822 wm2000 = devm_kzalloc(&i2c->dev, sizeof(struct wm2000_priv),
823 GFP_KERNEL);
Mark Brown3a66d382010-02-11 13:27:19 +0000824 if (wm2000 == NULL) {
825 dev_err(&i2c->dev, "Unable to allocate private data\n");
826 return -ENOMEM;
827 }
828
Mark Brown8e9bb422013-01-23 18:38:54 +0800829 mutex_init(&wm2000->lock);
830
Mark Brown8aa1fe82011-12-02 21:57:19 +0000831 dev_set_drvdata(&i2c->dev, wm2000);
832
Mark Brown8fed54a2012-09-22 18:32:08 -0400833 wm2000->regmap = devm_regmap_init_i2c(i2c, &wm2000_regmap);
Mark Brown8aa1fe82011-12-02 21:57:19 +0000834 if (IS_ERR(wm2000->regmap)) {
835 ret = PTR_ERR(wm2000->regmap);
836 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
837 ret);
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100838 goto out;
Mark Brown8aa1fe82011-12-02 21:57:19 +0000839 }
840
Mark Browna89be932012-09-22 18:33:23 -0400841 for (i = 0; i < WM2000_NUM_SUPPLIES; i++)
842 wm2000->supplies[i].supply = wm2000_supplies[i];
843
844 ret = devm_regulator_bulk_get(&i2c->dev, WM2000_NUM_SUPPLIES,
845 wm2000->supplies);
846 if (ret != 0) {
847 dev_err(&i2c->dev, "Failed to get supplies: %d\n", ret);
848 return ret;
849 }
850
851 ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies);
852 if (ret != 0) {
853 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
854 return ret;
855 }
856
Mark Brown3a66d382010-02-11 13:27:19 +0000857 /* Verify that this is a WM2000 */
858 reg = wm2000_read(i2c, WM2000_REG_ID1);
859 id = reg << 8;
860 reg = wm2000_read(i2c, WM2000_REG_ID2);
861 id |= reg & 0xff;
862
863 if (id != 0x2000) {
864 dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id);
865 ret = -ENODEV;
Mark Browna89be932012-09-22 18:33:23 -0400866 goto err_supplies;
Mark Brown3a66d382010-02-11 13:27:19 +0000867 }
868
869 reg = wm2000_read(i2c, WM2000_REG_REVISON);
870 dev_info(&i2c->dev, "revision %c\n", reg + 'A');
871
Mark Brown514cfd62012-12-13 17:29:00 +0900872 wm2000->mclk = devm_clk_get(&i2c->dev, "MCLK");
873 if (IS_ERR(wm2000->mclk)) {
874 ret = PTR_ERR(wm2000->mclk);
875 dev_err(&i2c->dev, "Failed to get MCLK: %d\n", ret);
876 goto err_supplies;
877 }
878
Mark Brown3a66d382010-02-11 13:27:19 +0000879 filename = "wm2000_anc.bin";
880 pdata = dev_get_platdata(&i2c->dev);
881 if (pdata) {
Mark Brown3a66d382010-02-11 13:27:19 +0000882 wm2000->speech_clarity = !pdata->speech_enh_disable;
883
884 if (pdata->download_file)
885 filename = pdata->download_file;
886 }
887
888 ret = request_firmware(&fw, filename, &i2c->dev);
889 if (ret != 0) {
890 dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret);
Mark Browna89be932012-09-22 18:33:23 -0400891 goto err_supplies;
Mark Brown3a66d382010-02-11 13:27:19 +0000892 }
893
894 /* Pre-cook the concatenation of the register address onto the image */
895 wm2000->anc_download_size = fw->size + 2;
Mark Brownb03e96e2011-12-02 21:28:31 +0000896 wm2000->anc_download = devm_kzalloc(&i2c->dev,
897 wm2000->anc_download_size,
898 GFP_KERNEL);
Mark Brown3a66d382010-02-11 13:27:19 +0000899 if (wm2000->anc_download == NULL) {
900 dev_err(&i2c->dev, "Out of memory\n");
901 ret = -ENOMEM;
Mark Browna89be932012-09-22 18:33:23 -0400902 goto err_supplies;
Mark Brown3a66d382010-02-11 13:27:19 +0000903 }
904
905 wm2000->anc_download[0] = 0x80;
906 wm2000->anc_download[1] = 0x00;
907 memcpy(wm2000->anc_download + 2, fw->data, fw->size);
908
Mark Brown3a66d382010-02-11 13:27:19 +0000909 wm2000->anc_eng_ena = 1;
Mark Browne71fa372010-06-15 15:14:00 +0100910 wm2000->anc_active = 1;
911 wm2000->spk_ena = 1;
Mark Brown3a66d382010-02-11 13:27:19 +0000912 wm2000->i2c = i2c;
913
914 wm2000_reset(wm2000);
915
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100916 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_wm2000, NULL, 0);
Mark Brown3a66d382010-02-11 13:27:19 +0000917
Mark Browna89be932012-09-22 18:33:23 -0400918err_supplies:
919 regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
Mark Brown3a66d382010-02-11 13:27:19 +0000920
Jesper Juhlc83f1d72012-01-23 22:28:44 +0100921out:
922 release_firmware(fw);
Mark Brown3a66d382010-02-11 13:27:19 +0000923 return ret;
924}
925
Bill Pemberton7a79e942012-12-07 09:26:37 -0500926static int wm2000_i2c_remove(struct i2c_client *i2c)
Mark Brown3a66d382010-02-11 13:27:19 +0000927{
Mark Brown4911ccd2011-12-02 21:59:18 +0000928 snd_soc_unregister_codec(&i2c->dev);
Mark Brown8aa1fe82011-12-02 21:57:19 +0000929
Mark Brown3a66d382010-02-11 13:27:19 +0000930 return 0;
931}
932
Mark Brown3a66d382010-02-11 13:27:19 +0000933static const struct i2c_device_id wm2000_i2c_id[] = {
934 { "wm2000", 0 },
935 { }
936};
937MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id);
938
939static struct i2c_driver wm2000_i2c_driver = {
940 .driver = {
941 .name = "wm2000",
942 .owner = THIS_MODULE,
943 },
944 .probe = wm2000_i2c_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500945 .remove = wm2000_i2c_remove,
Mark Brown3a66d382010-02-11 13:27:19 +0000946 .id_table = wm2000_i2c_id,
947};
948
Sachin Kamat3a4bfd82012-08-06 17:25:54 +0530949module_i2c_driver(wm2000_i2c_driver);
Mark Brown3a66d382010-02-11 13:27:19 +0000950
951MODULE_DESCRIPTION("ASoC WM2000 driver");
952MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
953MODULE_LICENSE("GPL");