xref: /linux/sound/soc/codecs/wm2000.c (revision f3caa0b02455409eec4673ddd8df72d8bcad4e98)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * wm2000.c  --  WM2000 ALSA Soc Audio driver
4  *
5  * Copyright 2008-2011 Wolfson Microelectronics PLC.
6  *
7  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8  *
9  * The download image for the WM2000 will be requested as
10  * 'wm2000_anc.bin' by default (overridable via platform data) at
11  * runtime and is expected to be in flat binary format.  This is
12  * generated by Wolfson configuration tools and includes
13  * system-specific calibration information.  If supplied as a
14  * sequence of ASCII-encoded hexidecimal bytes this can be converted
15  * into a flat binary with a command such as this on the command line:
16  *
17  * perl -e 'while (<>) { s/[\r\n]+// ; printf("%c", hex($_)); }'
18  *                 < file  > wm2000_anc.bin
19  */
20 
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/firmware.h>
26 #include <linux/cleanup.h>
27 #include <linux/clk.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/i2c.h>
31 #include <linux/regmap.h>
32 #include <linux/debugfs.h>
33 #include <linux/regulator/consumer.h>
34 #include <linux/slab.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/initval.h>
40 #include <sound/tlv.h>
41 
42 #include <sound/wm2000.h>
43 
44 #include "wm2000.h"
45 
46 #define WM2000_NUM_SUPPLIES 3
47 
48 static const char *wm2000_supplies[WM2000_NUM_SUPPLIES] = {
49 	"SPKVDD",
50 	"DBVDD",
51 	"DCVDD",
52 };
53 
54 enum wm2000_anc_mode {
55 	ANC_ACTIVE = 0,
56 	ANC_BYPASS = 1,
57 	ANC_STANDBY = 2,
58 	ANC_OFF = 3,
59 };
60 
61 struct wm2000_priv {
62 	struct i2c_client *i2c;
63 	struct regmap *regmap;
64 	struct clk *mclk;
65 
66 	struct regulator_bulk_data supplies[WM2000_NUM_SUPPLIES];
67 
68 	enum wm2000_anc_mode anc_mode;
69 
70 	unsigned int anc_active:1;
71 	unsigned int anc_eng_ena:1;
72 	unsigned int spk_ena:1;
73 
74 	unsigned int speech_clarity:1;
75 
76 	int anc_download_size;
77 	char *anc_download;
78 
79 	struct mutex lock;
80 };
81 
82 static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
83 			unsigned int value)
84 {
85 	struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
86 	return regmap_write(wm2000->regmap, reg, value);
87 }
88 
89 static void wm2000_reset(struct wm2000_priv *wm2000)
90 {
91 	struct i2c_client *i2c = wm2000->i2c;
92 
93 	wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
94 	wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
95 	wm2000_write(i2c, WM2000_REG_ID1, 0);
96 
97 	wm2000->anc_mode = ANC_OFF;
98 }
99 
100 static int wm2000_poll_bit(struct i2c_client *i2c,
101 			   unsigned int reg, u8 mask)
102 {
103 	struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
104 	int timeout = 4000;
105 	unsigned int val;
106 
107 	regmap_read(wm2000->regmap, reg, &val);
108 
109 	while (!(val & mask) && --timeout) {
110 		msleep(1);
111 		regmap_read(wm2000->regmap, reg, &val);
112 	}
113 
114 	if (timeout == 0)
115 		return 0;
116 	else
117 		return 1;
118 }
119 
120 static int wm2000_power_up(struct i2c_client *i2c, int analogue)
121 {
122 	struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
123 	unsigned long rate;
124 	unsigned int val;
125 	int ret;
126 
127 	if (WARN_ON(wm2000->anc_mode != ANC_OFF))
128 		return -EINVAL;
129 
130 	dev_dbg(&i2c->dev, "Beginning power up\n");
131 
132 	ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies);
133 	if (ret != 0) {
134 		dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
135 		return ret;
136 	}
137 
138 	rate = clk_get_rate(wm2000->mclk);
139 	if (rate <= 13500000) {
140 		dev_dbg(&i2c->dev, "Disabling MCLK divider\n");
141 		wm2000_write(i2c, WM2000_REG_SYS_CTL2,
142 			     WM2000_MCLK_DIV2_ENA_CLR);
143 	} else {
144 		dev_dbg(&i2c->dev, "Enabling MCLK divider\n");
145 		wm2000_write(i2c, WM2000_REG_SYS_CTL2,
146 			     WM2000_MCLK_DIV2_ENA_SET);
147 	}
148 
149 	wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
150 	wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET);
151 
152 	/* Wait for ANC engine to become ready */
153 	if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
154 			     WM2000_ANC_ENG_IDLE)) {
155 		dev_err(&i2c->dev, "ANC engine failed to reset\n");
156 		regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
157 		return -ETIMEDOUT;
158 	}
159 
160 	if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
161 			     WM2000_STATUS_BOOT_COMPLETE)) {
162 		dev_err(&i2c->dev, "ANC engine failed to initialise\n");
163 		regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
164 		return -ETIMEDOUT;
165 	}
166 
167 	wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
168 
169 	/* Open code download of the data since it is the only bulk
170 	 * write we do. */
171 	dev_dbg(&i2c->dev, "Downloading %d bytes\n",
172 		wm2000->anc_download_size - 2);
173 
174 	ret = i2c_master_send(i2c, wm2000->anc_download,
175 			      wm2000->anc_download_size);
176 	if (ret < 0) {
177 		dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret);
178 		regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
179 		return ret;
180 	}
181 	if (ret != wm2000->anc_download_size) {
182 		dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n",
183 			ret, wm2000->anc_download_size);
184 		regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
185 		return -EIO;
186 	}
187 
188 	dev_dbg(&i2c->dev, "Download complete\n");
189 
190 	if (analogue) {
191 		wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
192 
193 		wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
194 			     WM2000_MODE_ANA_SEQ_INCLUDE |
195 			     WM2000_MODE_MOUSE_ENABLE |
196 			     WM2000_MODE_THERMAL_ENABLE);
197 	} else {
198 		wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
199 			     WM2000_MODE_MOUSE_ENABLE |
200 			     WM2000_MODE_THERMAL_ENABLE);
201 	}
202 
203 	ret = regmap_read(wm2000->regmap, WM2000_REG_SPEECH_CLARITY, &val);
204 	if (ret != 0) {
205 		dev_err(&i2c->dev, "Unable to read Speech Clarity: %d\n", ret);
206 		regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
207 		return ret;
208 	}
209 	if (wm2000->speech_clarity)
210 		val |= WM2000_SPEECH_CLARITY;
211 	else
212 		val &= ~WM2000_SPEECH_CLARITY;
213 	wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, val);
214 
215 	wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33);
216 	wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02);
217 
218 	wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
219 
220 	if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
221 			     WM2000_STATUS_MOUSE_ACTIVE)) {
222 		dev_err(&i2c->dev, "Timed out waiting for device\n");
223 		regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
224 		return -ETIMEDOUT;
225 	}
226 
227 	dev_dbg(&i2c->dev, "ANC active\n");
228 	if (analogue)
229 		dev_dbg(&i2c->dev, "Analogue active\n");
230 	wm2000->anc_mode = ANC_ACTIVE;
231 
232 	return 0;
233 }
234 
235 static int wm2000_power_down(struct i2c_client *i2c, int analogue)
236 {
237 	struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
238 
239 	if (analogue) {
240 		wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
241 		wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
242 			     WM2000_MODE_ANA_SEQ_INCLUDE |
243 			     WM2000_MODE_POWER_DOWN);
244 	} else {
245 		wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
246 			     WM2000_MODE_POWER_DOWN);
247 	}
248 
249 	if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
250 			     WM2000_STATUS_POWER_DOWN_COMPLETE)) {
251 		dev_err(&i2c->dev, "Timeout waiting for ANC power down\n");
252 		return -ETIMEDOUT;
253 	}
254 
255 	if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
256 			     WM2000_ANC_ENG_IDLE)) {
257 		dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
258 		return -ETIMEDOUT;
259 	}
260 
261 	regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
262 
263 	dev_dbg(&i2c->dev, "powered off\n");
264 	wm2000->anc_mode = ANC_OFF;
265 
266 	return 0;
267 }
268 
269 static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue)
270 {
271 	struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
272 
273 	if (WARN_ON(wm2000->anc_mode != ANC_ACTIVE))
274 		return -EINVAL;
275 
276 	if (analogue) {
277 		wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
278 			     WM2000_MODE_ANA_SEQ_INCLUDE |
279 			     WM2000_MODE_THERMAL_ENABLE |
280 			     WM2000_MODE_BYPASS_ENTRY);
281 	} else {
282 		wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
283 			     WM2000_MODE_THERMAL_ENABLE |
284 			     WM2000_MODE_BYPASS_ENTRY);
285 	}
286 
287 	if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
288 			     WM2000_STATUS_ANC_DISABLED)) {
289 		dev_err(&i2c->dev, "Timeout waiting for ANC disable\n");
290 		return -ETIMEDOUT;
291 	}
292 
293 	if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
294 			     WM2000_ANC_ENG_IDLE)) {
295 		dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
296 		return -ETIMEDOUT;
297 	}
298 
299 	wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
300 	wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
301 
302 	wm2000->anc_mode = ANC_BYPASS;
303 	dev_dbg(&i2c->dev, "bypass enabled\n");
304 
305 	return 0;
306 }
307 
308 static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue)
309 {
310 	struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
311 
312 	if (WARN_ON(wm2000->anc_mode != ANC_BYPASS))
313 		return -EINVAL;
314 
315 	wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
316 
317 	if (analogue) {
318 		wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
319 			     WM2000_MODE_ANA_SEQ_INCLUDE |
320 			     WM2000_MODE_MOUSE_ENABLE |
321 			     WM2000_MODE_THERMAL_ENABLE);
322 	} else {
323 		wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
324 			     WM2000_MODE_MOUSE_ENABLE |
325 			     WM2000_MODE_THERMAL_ENABLE);
326 	}
327 
328 	wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
329 	wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
330 
331 	if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
332 			     WM2000_STATUS_MOUSE_ACTIVE)) {
333 		dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
334 		return -ETIMEDOUT;
335 	}
336 
337 	wm2000->anc_mode = ANC_ACTIVE;
338 	dev_dbg(&i2c->dev, "MOUSE active\n");
339 
340 	return 0;
341 }
342 
343 static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
344 {
345 	struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
346 
347 	if (WARN_ON(wm2000->anc_mode != ANC_ACTIVE))
348 		return -EINVAL;
349 
350 	if (analogue) {
351 		wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4);
352 
353 		wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
354 			     WM2000_MODE_ANA_SEQ_INCLUDE |
355 			     WM2000_MODE_THERMAL_ENABLE |
356 			     WM2000_MODE_STANDBY_ENTRY);
357 	} else {
358 		wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
359 			     WM2000_MODE_THERMAL_ENABLE |
360 			     WM2000_MODE_STANDBY_ENTRY);
361 	}
362 
363 	if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
364 			     WM2000_STATUS_ANC_DISABLED)) {
365 		dev_err(&i2c->dev,
366 			"Timed out waiting for ANC disable after 1ms\n");
367 		return -ETIMEDOUT;
368 	}
369 
370 	if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE)) {
371 		dev_err(&i2c->dev,
372 			"Timed out waiting for standby\n");
373 		return -ETIMEDOUT;
374 	}
375 
376 	wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
377 	wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
378 
379 	wm2000->anc_mode = ANC_STANDBY;
380 	dev_dbg(&i2c->dev, "standby\n");
381 	if (analogue)
382 		dev_dbg(&i2c->dev, "Analogue disabled\n");
383 
384 	return 0;
385 }
386 
387 static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
388 {
389 	struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
390 
391 	if (WARN_ON(wm2000->anc_mode != ANC_STANDBY))
392 		return -EINVAL;
393 
394 	wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
395 
396 	if (analogue) {
397 		wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4);
398 
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 {
404 		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,
413 			     WM2000_STATUS_MOUSE_ACTIVE)) {
414 		dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
415 		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 
426 typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
427 
428 static 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 
535 static 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 = 0;
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 
555 	/* 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 
564 	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 			break;
571 	}
572 
573 	if (anc_transitions[i].dest == ANC_OFF)
574 		clk_disable_unprepare(wm2000->mclk);
575 
576 	return ret;
577 }
578 
579 static 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 
599 static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
600 			       struct snd_ctl_elem_value *ucontrol)
601 {
602 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
603 	struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
604 
605 	ucontrol->value.integer.value[0] = wm2000->anc_active;
606 
607 	return 0;
608 }
609 
610 static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
611 			       struct snd_ctl_elem_value *ucontrol)
612 {
613 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
614 	struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
615 	unsigned int anc_active = ucontrol->value.integer.value[0];
616 
617 	if (anc_active > 1)
618 		return -EINVAL;
619 
620 	guard(mutex)(&wm2000->lock);
621 
622 	wm2000->anc_active = anc_active;
623 
624 	return wm2000_anc_set_mode(wm2000);
625 }
626 
627 static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
628 			      struct snd_ctl_elem_value *ucontrol)
629 {
630 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
631 	struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
632 
633 	ucontrol->value.integer.value[0] = wm2000->spk_ena;
634 
635 	return 0;
636 }
637 
638 static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
639 			      struct snd_ctl_elem_value *ucontrol)
640 {
641 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
642 	struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
643 	unsigned int val = ucontrol->value.integer.value[0];
644 
645 	if (val > 1)
646 		return -EINVAL;
647 
648 	guard(mutex)(&wm2000->lock);
649 
650 	wm2000->spk_ena = val;
651 
652 	return wm2000_anc_set_mode(wm2000);
653 }
654 
655 static const struct snd_kcontrol_new wm2000_controls[] = {
656 	SOC_SINGLE("ANC Volume", WM2000_REG_ANC_GAIN_CTRL, 0, 255, 0),
657 	SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0,
658 			    wm2000_anc_mode_get,
659 			    wm2000_anc_mode_put),
660 	SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0,
661 			    wm2000_speaker_get,
662 			    wm2000_speaker_put),
663 };
664 
665 static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
666 				  struct snd_kcontrol *kcontrol, int event)
667 {
668 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
669 	struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
670 
671 	guard(mutex)(&wm2000->lock);
672 
673 	if (SND_SOC_DAPM_EVENT_ON(event))
674 		wm2000->anc_eng_ena = 1;
675 
676 	if (SND_SOC_DAPM_EVENT_OFF(event))
677 		wm2000->anc_eng_ena = 0;
678 
679 	return wm2000_anc_set_mode(wm2000);
680 }
681 
682 static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
683 /* Externally visible pins */
684 SND_SOC_DAPM_OUTPUT("SPKN"),
685 SND_SOC_DAPM_OUTPUT("SPKP"),
686 
687 SND_SOC_DAPM_INPUT("LINN"),
688 SND_SOC_DAPM_INPUT("LINP"),
689 
690 SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0,
691 		   wm2000_anc_power_event,
692 		   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
693 };
694 
695 /* Target, Path, Source */
696 static const struct snd_soc_dapm_route wm2000_audio_map[] = {
697 	{ "SPKN", NULL, "ANC Engine" },
698 	{ "SPKP", NULL, "ANC Engine" },
699 	{ "ANC Engine", NULL, "LINN" },
700 	{ "ANC Engine", NULL, "LINP" },
701 };
702 
703 #ifdef CONFIG_PM
704 static int wm2000_suspend(struct snd_soc_component *component)
705 {
706 	struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
707 
708 	return wm2000_anc_transition(wm2000, ANC_OFF);
709 }
710 
711 static int wm2000_resume(struct snd_soc_component *component)
712 {
713 	struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
714 
715 	return wm2000_anc_set_mode(wm2000);
716 }
717 #else
718 #define wm2000_suspend NULL
719 #define wm2000_resume NULL
720 #endif
721 
722 static bool wm2000_readable_reg(struct device *dev, unsigned int reg)
723 {
724 	switch (reg) {
725 	case WM2000_REG_SYS_START:
726 	case WM2000_REG_ANC_GAIN_CTRL:
727 	case WM2000_REG_MSE_TH1:
728 	case WM2000_REG_MSE_TH2:
729 	case WM2000_REG_SPEECH_CLARITY:
730 	case WM2000_REG_SYS_WATCHDOG:
731 	case WM2000_REG_ANA_VMID_PD_TIME:
732 	case WM2000_REG_ANA_VMID_PU_TIME:
733 	case WM2000_REG_CAT_FLTR_INDX:
734 	case WM2000_REG_CAT_GAIN_0:
735 	case WM2000_REG_SYS_STATUS:
736 	case WM2000_REG_SYS_MODE_CNTRL:
737 	case WM2000_REG_SYS_START0:
738 	case WM2000_REG_SYS_START1:
739 	case WM2000_REG_ID1:
740 	case WM2000_REG_ID2:
741 	case WM2000_REG_REVISON:
742 	case WM2000_REG_SYS_CTL1:
743 	case WM2000_REG_SYS_CTL2:
744 	case WM2000_REG_ANC_STAT:
745 	case WM2000_REG_IF_CTL:
746 	case WM2000_REG_ANA_MIC_CTL:
747 	case WM2000_REG_SPK_CTL:
748 		return true;
749 	default:
750 		return false;
751 	}
752 }
753 
754 static const struct regmap_config wm2000_regmap = {
755 	.reg_bits = 16,
756 	.val_bits = 8,
757 
758 	.max_register = WM2000_REG_SPK_CTL,
759 	.readable_reg = wm2000_readable_reg,
760 };
761 
762 static int wm2000_probe(struct snd_soc_component *component)
763 {
764 	struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
765 
766 	/* This will trigger a transition to standby mode by default */
767 	wm2000_anc_set_mode(wm2000);
768 
769 	return 0;
770 }
771 
772 static void wm2000_remove(struct snd_soc_component *component)
773 {
774 	struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev);
775 
776 	wm2000_anc_transition(wm2000, ANC_OFF);
777 }
778 
779 static const struct snd_soc_component_driver soc_component_dev_wm2000 = {
780 	.probe			= wm2000_probe,
781 	.remove			= wm2000_remove,
782 	.suspend		= wm2000_suspend,
783 	.resume			= wm2000_resume,
784 	.controls		= wm2000_controls,
785 	.num_controls		= ARRAY_SIZE(wm2000_controls),
786 	.dapm_widgets		= wm2000_dapm_widgets,
787 	.num_dapm_widgets	= ARRAY_SIZE(wm2000_dapm_widgets),
788 	.dapm_routes		= wm2000_audio_map,
789 	.num_dapm_routes	= ARRAY_SIZE(wm2000_audio_map),
790 	.idle_bias_on		= 1,
791 	.use_pmdown_time	= 1,
792 };
793 
794 static int wm2000_i2c_probe(struct i2c_client *i2c)
795 {
796 	struct wm2000_priv *wm2000;
797 	struct wm2000_platform_data *pdata;
798 	const char *filename;
799 	const struct firmware *fw = NULL;
800 	int ret, i;
801 	unsigned int reg;
802 	u16 id;
803 
804 	wm2000 = devm_kzalloc(&i2c->dev, sizeof(*wm2000), GFP_KERNEL);
805 	if (!wm2000)
806 		return -ENOMEM;
807 
808 	mutex_init(&wm2000->lock);
809 
810 	dev_set_drvdata(&i2c->dev, wm2000);
811 
812 	wm2000->regmap = devm_regmap_init_i2c(i2c, &wm2000_regmap);
813 	if (IS_ERR(wm2000->regmap)) {
814 		ret = PTR_ERR(wm2000->regmap);
815 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
816 			ret);
817 		goto out;
818 	}
819 
820 	for (i = 0; i < WM2000_NUM_SUPPLIES; i++)
821 		wm2000->supplies[i].supply = wm2000_supplies[i];
822 
823 	ret = devm_regulator_bulk_get(&i2c->dev, WM2000_NUM_SUPPLIES,
824 				      wm2000->supplies);
825 	if (ret != 0) {
826 		dev_err(&i2c->dev, "Failed to get supplies: %d\n", ret);
827 		return ret;
828 	}
829 
830 	ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies);
831 	if (ret != 0) {
832 		dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
833 		return ret;
834 	}
835 
836 	/* Verify that this is a WM2000 */
837 	ret = regmap_read(wm2000->regmap, WM2000_REG_ID1, &reg);
838 	if (ret != 0) {
839 		dev_err(&i2c->dev, "Unable to read ID1: %d\n", ret);
840 		return ret;
841 	}
842 	id = reg << 8;
843 	ret = regmap_read(wm2000->regmap, WM2000_REG_ID2, &reg);
844 	if (ret != 0) {
845 		dev_err(&i2c->dev, "Unable to read ID2: %d\n", ret);
846 		return ret;
847 	}
848 	id |= reg & 0xff;
849 
850 	if (id != 0x2000) {
851 		dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id);
852 		ret = -ENODEV;
853 		goto err_supplies;
854 	}
855 
856 	ret = regmap_read(wm2000->regmap, WM2000_REG_REVISON, &reg);
857 	if (ret != 0) {
858 		dev_err(&i2c->dev, "Unable to read Revision: %d\n", ret);
859 		return ret;
860 	}
861 	dev_info(&i2c->dev, "revision %c\n", reg + 'A');
862 
863 	wm2000->mclk = devm_clk_get(&i2c->dev, "MCLK");
864 	if (IS_ERR(wm2000->mclk)) {
865 		ret = PTR_ERR(wm2000->mclk);
866 		dev_err(&i2c->dev, "Failed to get MCLK: %d\n", ret);
867 		goto err_supplies;
868 	}
869 
870 	filename = "wm2000_anc.bin";
871 	pdata = dev_get_platdata(&i2c->dev);
872 	if (pdata) {
873 		wm2000->speech_clarity = !pdata->speech_enh_disable;
874 
875 		if (pdata->download_file)
876 			filename = pdata->download_file;
877 	}
878 
879 	ret = request_firmware(&fw, filename, &i2c->dev);
880 	if (ret != 0) {
881 		dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret);
882 		goto err_supplies;
883 	}
884 
885 	/* Pre-cook the concatenation of the register address onto the image */
886 	wm2000->anc_download_size = fw->size + 2;
887 	wm2000->anc_download = devm_kzalloc(&i2c->dev,
888 					    wm2000->anc_download_size,
889 					    GFP_KERNEL);
890 	if (wm2000->anc_download == NULL) {
891 		ret = -ENOMEM;
892 		goto err_supplies;
893 	}
894 
895 	wm2000->anc_download[0] = 0x80;
896 	wm2000->anc_download[1] = 0x00;
897 	memcpy(wm2000->anc_download + 2, fw->data, fw->size);
898 
899 	wm2000->anc_eng_ena = 1;
900 	wm2000->anc_active = 1;
901 	wm2000->spk_ena = 1;
902 	wm2000->i2c = i2c;
903 
904 	wm2000_reset(wm2000);
905 
906 	ret = devm_snd_soc_register_component(&i2c->dev,
907 					&soc_component_dev_wm2000, NULL, 0);
908 
909 err_supplies:
910 	regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies);
911 
912 out:
913 	release_firmware(fw);
914 	return ret;
915 }
916 
917 static const struct i2c_device_id wm2000_i2c_id[] = {
918 	{ .name = "wm2000" },
919 	{ }
920 };
921 MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id);
922 
923 static struct i2c_driver wm2000_i2c_driver = {
924 	.driver = {
925 		.name = "wm2000",
926 	},
927 	.probe = wm2000_i2c_probe,
928 	.id_table = wm2000_i2c_id,
929 };
930 
931 module_i2c_driver(wm2000_i2c_driver);
932 
933 MODULE_DESCRIPTION("ASoC WM2000 driver");
934 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
935 MODULE_LICENSE("GPL");
936