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