xref: /openbmc/linux/sound/soc/codecs/wm2000.c (revision 9344dade)
1 /*
2  * wm2000.c  --  WM2000 ALSA Soc Audio driver
3  *
4  * Copyright 2008-2011 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * 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>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/firmware.h>
29 #include <linux/clk.h>
30 #include <linux/delay.h>
31 #include <linux/pm.h>
32 #include <linux/i2c.h>
33 #include <linux/regmap.h>
34 #include <linux/debugfs.h>
35 #include <linux/regulator/consumer.h>
36 #include <linux/slab.h>
37 #include <sound/core.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include <sound/soc.h>
41 #include <sound/initval.h>
42 #include <sound/tlv.h>
43 
44 #include <sound/wm2000.h>
45 
46 #include "wm2000.h"
47 
48 #define WM2000_NUM_SUPPLIES 3
49 
50 static const char *wm2000_supplies[WM2000_NUM_SUPPLIES] = {
51 	"SPKVDD",
52 	"DBVDD",
53 	"DCVDD",
54 };
55 
56 enum wm2000_anc_mode {
57 	ANC_ACTIVE = 0,
58 	ANC_BYPASS = 1,
59 	ANC_STANDBY = 2,
60 	ANC_OFF = 3,
61 };
62 
63 struct wm2000_priv {
64 	struct i2c_client *i2c;
65 	struct regmap *regmap;
66 	struct clk *mclk;
67 
68 	struct regulator_bulk_data supplies[WM2000_NUM_SUPPLIES];
69 
70 	enum wm2000_anc_mode anc_mode;
71 
72 	unsigned int anc_active:1;
73 	unsigned int anc_eng_ena:1;
74 	unsigned int spk_ena:1;
75 
76 	unsigned int speech_clarity:1;
77 
78 	int anc_download_size;
79 	char *anc_download;
80 
81 	struct mutex lock;
82 };
83 
84 static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
85 			unsigned int value)
86 {
87 	struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
88 	return regmap_write(wm2000->regmap, reg, value);
89 }
90 
91 static unsigned int wm2000_read(struct i2c_client *i2c, unsigned int r)
92 {
93 	struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
94 	unsigned int val;
95 	int ret;
96 
97 	ret = regmap_read(wm2000->regmap, r, &val);
98 	if (ret < 0)
99 		return -1;
100 
101 	return val;
102 }
103 
104 static 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 
115 static int wm2000_poll_bit(struct i2c_client *i2c,
116 			   unsigned int reg, u8 mask)
117 {
118 	int timeout = 4000;
119 	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 
134 static int wm2000_power_up(struct i2c_client *i2c, int analogue)
135 {
136 	struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
137 	unsigned long rate;
138 	int ret;
139 
140 	BUG_ON(wm2000->anc_mode != ANC_OFF);
141 
142 	dev_dbg(&i2c->dev, "Beginning power up\n");
143 
144 	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 
150 	rate = clk_get_rate(wm2000->mclk);
151 	if (rate <= 13500000) {
152 		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,
166 			     WM2000_ANC_ENG_IDLE)) {
167 		dev_err(&i2c->dev, "ANC engine failed to reset\n");
168 		regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
169 		return -ETIMEDOUT;
170 	}
171 
172 	if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
173 			     WM2000_STATUS_BOOT_COMPLETE)) {
174 		dev_err(&i2c->dev, "ANC engine failed to initialise\n");
175 		regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
176 		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);
190 		regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
191 		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);
196 		regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
197 		return -EIO;
198 	}
199 
200 	dev_dbg(&i2c->dev, "Download complete\n");
201 
202 	if (analogue) {
203 		wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
204 
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 {
210 		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)
217 		ret |= WM2000_SPEECH_CLARITY;
218 	else
219 		ret &= ~WM2000_SPEECH_CLARITY;
220 	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,
228 			     WM2000_STATUS_MOUSE_ACTIVE)) {
229 		dev_err(&i2c->dev, "Timed out waiting for device\n");
230 		regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
231 		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 
242 static int wm2000_power_down(struct i2c_client *i2c, int analogue)
243 {
244 	struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
245 
246 	if (analogue) {
247 		wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
248 		wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
249 			     WM2000_MODE_ANA_SEQ_INCLUDE |
250 			     WM2000_MODE_POWER_DOWN);
251 	} else {
252 		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,
257 			     WM2000_STATUS_POWER_DOWN_COMPLETE)) {
258 		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,
263 			     WM2000_ANC_ENG_IDLE)) {
264 		dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
265 		return -ETIMEDOUT;
266 	}
267 
268 	regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
269 
270 	dev_dbg(&i2c->dev, "powered off\n");
271 	wm2000->anc_mode = ANC_OFF;
272 
273 	return 0;
274 }
275 
276 static 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,
294 			     WM2000_STATUS_ANC_DISABLED)) {
295 		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,
300 			     WM2000_ANC_ENG_IDLE)) {
301 		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 
314 static 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,
337 			     WM2000_STATUS_MOUSE_ACTIVE)) {
338 		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 
348 static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
349 {
350 	struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
351 
352 	BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
353 
354 	if (analogue) {
355 		wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
356 
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 {
362 		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,
368 			     WM2000_STATUS_ANC_DISABLED)) {
369 		dev_err(&i2c->dev,
370 			"Timed out waiting for ANC disable after 1ms\n");
371 		return -ETIMEDOUT;
372 	}
373 
374 	if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE)) {
375 		dev_err(&i2c->dev,
376 			"Timed out waiting for standby\n");
377 		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 
391 static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
392 {
393 	struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
394 
395 	BUG_ON(wm2000->anc_mode != ANC_STANDBY);
396 
397 	wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
398 
399 	if (analogue) {
400 		wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
401 
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 {
407 		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,
416 			     WM2000_STATUS_MOUSE_ACTIVE)) {
417 		dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
418 		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 
429 typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
430 
431 static 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 
538 static 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 
558 	/* 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 
567 	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 
576 	if (anc_transitions[i].dest == ANC_OFF)
577 		clk_disable_unprepare(wm2000->mclk);
578 
579 	return ret;
580 }
581 
582 static 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 
602 static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
603 			       struct snd_ctl_elem_value *ucontrol)
604 {
605 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
606 	struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
607 
608 	ucontrol->value.enumerated.item[0] = wm2000->anc_active;
609 
610 	return 0;
611 }
612 
613 static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
614 			       struct snd_ctl_elem_value *ucontrol)
615 {
616 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
617 	struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
618 	int anc_active = ucontrol->value.enumerated.item[0];
619 	int ret;
620 
621 	if (anc_active > 1)
622 		return -EINVAL;
623 
624 	mutex_lock(&wm2000->lock);
625 
626 	wm2000->anc_active = anc_active;
627 
628 	ret = wm2000_anc_set_mode(wm2000);
629 
630 	mutex_unlock(&wm2000->lock);
631 
632 	return ret;
633 }
634 
635 static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
636 			      struct snd_ctl_elem_value *ucontrol)
637 {
638 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
639 	struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
640 
641 	ucontrol->value.enumerated.item[0] = wm2000->spk_ena;
642 
643 	return 0;
644 }
645 
646 static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
647 			      struct snd_ctl_elem_value *ucontrol)
648 {
649 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
650 	struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
651 	int val = ucontrol->value.enumerated.item[0];
652 	int ret;
653 
654 	if (val > 1)
655 		return -EINVAL;
656 
657 	mutex_lock(&wm2000->lock);
658 
659 	wm2000->spk_ena = val;
660 
661 	ret = wm2000_anc_set_mode(wm2000);
662 
663 	mutex_unlock(&wm2000->lock);
664 
665 	return ret;
666 }
667 
668 static const struct snd_kcontrol_new wm2000_controls[] = {
669 	SOC_SINGLE("ANC Volume", WM2000_REG_ANC_GAIN_CTRL, 0, 255, 0),
670 	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 
678 static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
679 				  struct snd_kcontrol *kcontrol, int event)
680 {
681 	struct snd_soc_codec *codec = w->codec;
682 	struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
683 	int ret;
684 
685 	mutex_lock(&wm2000->lock);
686 
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 
693 	ret = wm2000_anc_set_mode(wm2000);
694 
695 	mutex_unlock(&wm2000->lock);
696 
697 	return ret;
698 }
699 
700 static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
701 /* Externally visible pins */
702 SND_SOC_DAPM_OUTPUT("SPKN"),
703 SND_SOC_DAPM_OUTPUT("SPKP"),
704 
705 SND_SOC_DAPM_INPUT("LINN"),
706 SND_SOC_DAPM_INPUT("LINP"),
707 
708 SND_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 */
714 static 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" },
719 };
720 
721 #ifdef CONFIG_PM
722 static int wm2000_suspend(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 
729 static 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
739 
740 static bool wm2000_readable_reg(struct device *dev, unsigned int reg)
741 {
742 	switch (reg) {
743 	case WM2000_REG_SYS_START:
744 	case WM2000_REG_ANC_GAIN_CTRL:
745 	case WM2000_REG_MSE_TH1:
746 	case WM2000_REG_MSE_TH2:
747 	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 	case WM2000_REG_ANA_MIC_CTL:
765 	case WM2000_REG_SPK_CTL:
766 		return true;
767 	default:
768 		return false;
769 	}
770 }
771 
772 static const struct regmap_config wm2000_regmap = {
773 	.reg_bits = 16,
774 	.val_bits = 8,
775 
776 	.max_register = WM2000_REG_SPK_CTL,
777 	.readable_reg = wm2000_readable_reg,
778 };
779 
780 static int wm2000_probe(struct snd_soc_codec *codec)
781 {
782 	struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
783 
784 	snd_soc_codec_set_cache_io(codec, 16, 8, SND_SOC_REGMAP);
785 
786 	/* This will trigger a transition to standby mode by default */
787 	wm2000_anc_set_mode(wm2000);
788 
789 	return 0;
790 }
791 
792 static int wm2000_remove(struct snd_soc_codec *codec)
793 {
794 	struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
795 
796 	return wm2000_anc_transition(wm2000, ANC_OFF);
797 }
798 
799 static struct snd_soc_codec_driver soc_codec_dev_wm2000 = {
800 	.probe = wm2000_probe,
801 	.remove = wm2000_remove,
802 	.suspend = wm2000_suspend,
803 	.resume = wm2000_resume,
804 
805 	.dapm_widgets = wm2000_dapm_widgets,
806 	.num_dapm_widgets = ARRAY_SIZE(wm2000_dapm_widgets),
807 	.dapm_routes = wm2000_audio_map,
808 	.num_dapm_routes = ARRAY_SIZE(wm2000_audio_map),
809 	.controls = wm2000_controls,
810 	.num_controls = ARRAY_SIZE(wm2000_controls),
811 };
812 
813 static int wm2000_i2c_probe(struct i2c_client *i2c,
814 			    const struct i2c_device_id *i2c_id)
815 {
816 	struct wm2000_priv *wm2000;
817 	struct wm2000_platform_data *pdata;
818 	const char *filename;
819 	const struct firmware *fw = NULL;
820 	int ret, i;
821 	int reg;
822 	u16 id;
823 
824 	wm2000 = devm_kzalloc(&i2c->dev, sizeof(struct wm2000_priv),
825 			      GFP_KERNEL);
826 	if (wm2000 == NULL) {
827 		dev_err(&i2c->dev, "Unable to allocate private data\n");
828 		return -ENOMEM;
829 	}
830 
831 	mutex_init(&wm2000->lock);
832 
833 	dev_set_drvdata(&i2c->dev, wm2000);
834 
835 	wm2000->regmap = devm_regmap_init_i2c(i2c, &wm2000_regmap);
836 	if (IS_ERR(wm2000->regmap)) {
837 		ret = PTR_ERR(wm2000->regmap);
838 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
839 			ret);
840 		goto out;
841 	}
842 
843 	for (i = 0; i < WM2000_NUM_SUPPLIES; i++)
844 		wm2000->supplies[i].supply = wm2000_supplies[i];
845 
846 	ret = devm_regulator_bulk_get(&i2c->dev, WM2000_NUM_SUPPLIES,
847 				      wm2000->supplies);
848 	if (ret != 0) {
849 		dev_err(&i2c->dev, "Failed to get supplies: %d\n", ret);
850 		return ret;
851 	}
852 
853 	ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies);
854 	if (ret != 0) {
855 		dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
856 		return ret;
857 	}
858 
859 	/* Verify that this is a WM2000 */
860 	reg = wm2000_read(i2c, WM2000_REG_ID1);
861 	id = reg << 8;
862 	reg = wm2000_read(i2c, WM2000_REG_ID2);
863 	id |= reg & 0xff;
864 
865 	if (id != 0x2000) {
866 		dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id);
867 		ret = -ENODEV;
868 		goto err_supplies;
869 	}
870 
871 	reg = wm2000_read(i2c, WM2000_REG_REVISON);
872 	dev_info(&i2c->dev, "revision %c\n", reg + 'A');
873 
874 	wm2000->mclk = devm_clk_get(&i2c->dev, "MCLK");
875 	if (IS_ERR(wm2000->mclk)) {
876 		ret = PTR_ERR(wm2000->mclk);
877 		dev_err(&i2c->dev, "Failed to get MCLK: %d\n", ret);
878 		goto err_supplies;
879 	}
880 
881 	filename = "wm2000_anc.bin";
882 	pdata = dev_get_platdata(&i2c->dev);
883 	if (pdata) {
884 		wm2000->speech_clarity = !pdata->speech_enh_disable;
885 
886 		if (pdata->download_file)
887 			filename = pdata->download_file;
888 	}
889 
890 	ret = request_firmware(&fw, filename, &i2c->dev);
891 	if (ret != 0) {
892 		dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret);
893 		goto err_supplies;
894 	}
895 
896 	/* Pre-cook the concatenation of the register address onto the image */
897 	wm2000->anc_download_size = fw->size + 2;
898 	wm2000->anc_download = devm_kzalloc(&i2c->dev,
899 					    wm2000->anc_download_size,
900 					    GFP_KERNEL);
901 	if (wm2000->anc_download == NULL) {
902 		dev_err(&i2c->dev, "Out of memory\n");
903 		ret = -ENOMEM;
904 		goto err_supplies;
905 	}
906 
907 	wm2000->anc_download[0] = 0x80;
908 	wm2000->anc_download[1] = 0x00;
909 	memcpy(wm2000->anc_download + 2, fw->data, fw->size);
910 
911 	wm2000->anc_eng_ena = 1;
912 	wm2000->anc_active = 1;
913 	wm2000->spk_ena = 1;
914 	wm2000->i2c = i2c;
915 
916 	wm2000_reset(wm2000);
917 
918 	ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_wm2000, NULL, 0);
919 
920 err_supplies:
921 	regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
922 
923 out:
924 	release_firmware(fw);
925 	return ret;
926 }
927 
928 static int wm2000_i2c_remove(struct i2c_client *i2c)
929 {
930 	snd_soc_unregister_codec(&i2c->dev);
931 
932 	return 0;
933 }
934 
935 static const struct i2c_device_id wm2000_i2c_id[] = {
936 	{ "wm2000", 0 },
937 	{ }
938 };
939 MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id);
940 
941 static struct i2c_driver wm2000_i2c_driver = {
942 	.driver = {
943 		.name = "wm2000",
944 		.owner = THIS_MODULE,
945 	},
946 	.probe = wm2000_i2c_probe,
947 	.remove = wm2000_i2c_remove,
948 	.id_table = wm2000_i2c_id,
949 };
950 
951 module_i2c_driver(wm2000_i2c_driver);
952 
953 MODULE_DESCRIPTION("ASoC WM2000 driver");
954 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
955 MODULE_LICENSE("GPL");
956