xref: /linux/sound/soc/codecs/wm8731.c (revision a51ff30f45473a80f78b2572666473887e010d91)
1 /*
2  * wm8731.c  --  WM8731 ALSA SoC Audio driver
3  *
4  * Copyright 2005 Openedhand Ltd.
5  * Copyright 2006-12 Wolfson Microelectronics, plc
6  *
7  * Author: Richard Purdie <richard@openedhand.com>
8  *
9  * Based on wm8753.c by Liam Girdwood
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  */
15 
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
20 #include <linux/pm.h>
21 #include <linux/i2c.h>
22 #include <linux/slab.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/spi/spi.h>
26 #include <linux/of_device.h>
27 #include <linux/mutex.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/initval.h>
33 #include <sound/tlv.h>
34 
35 #include "wm8731.h"
36 
37 #define WM8731_NUM_SUPPLIES 4
38 static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = {
39 	"AVDD",
40 	"HPVDD",
41 	"DCVDD",
42 	"DBVDD",
43 };
44 
45 /* codec private data */
46 struct wm8731_priv {
47 	struct regmap *regmap;
48 	struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES];
49 	const struct snd_pcm_hw_constraint_list *constraints;
50 	unsigned int sysclk;
51 	int sysclk_type;
52 	int playback_fs;
53 	bool deemph;
54 
55 	struct mutex lock;
56 };
57 
58 
59 /*
60  * wm8731 register cache
61  */
62 static const struct reg_default wm8731_reg_defaults[] = {
63 	{ 0, 0x0097 },
64 	{ 1, 0x0097 },
65 	{ 2, 0x0079 },
66 	{ 3, 0x0079 },
67 	{ 4, 0x000a },
68 	{ 5, 0x0008 },
69 	{ 6, 0x009f },
70 	{ 7, 0x000a },
71 	{ 8, 0x0000 },
72 	{ 9, 0x0000 },
73 };
74 
75 static bool wm8731_volatile(struct device *dev, unsigned int reg)
76 {
77 	return reg == WM8731_RESET;
78 }
79 
80 static bool wm8731_writeable(struct device *dev, unsigned int reg)
81 {
82 	return reg <= WM8731_RESET;
83 }
84 
85 #define wm8731_reset(c)	snd_soc_write(c, WM8731_RESET, 0)
86 
87 static const char *wm8731_input_select[] = {"Line In", "Mic"};
88 
89 static SOC_ENUM_SINGLE_DECL(wm8731_insel_enum,
90 			    WM8731_APANA, 2, wm8731_input_select);
91 
92 static int wm8731_deemph[] = { 0, 32000, 44100, 48000 };
93 
94 static int wm8731_set_deemph(struct snd_soc_codec *codec)
95 {
96 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
97 	int val, i, best;
98 
99 	/* If we're using deemphasis select the nearest available sample
100 	 * rate.
101 	 */
102 	if (wm8731->deemph) {
103 		best = 1;
104 		for (i = 2; i < ARRAY_SIZE(wm8731_deemph); i++) {
105 			if (abs(wm8731_deemph[i] - wm8731->playback_fs) <
106 			    abs(wm8731_deemph[best] - wm8731->playback_fs))
107 				best = i;
108 		}
109 
110 		val = best << 1;
111 	} else {
112 		best = 0;
113 		val = 0;
114 	}
115 
116 	dev_dbg(codec->dev, "Set deemphasis %d (%dHz)\n",
117 		best, wm8731_deemph[best]);
118 
119 	return snd_soc_update_bits(codec, WM8731_APDIGI, 0x6, val);
120 }
121 
122 static int wm8731_get_deemph(struct snd_kcontrol *kcontrol,
123 			     struct snd_ctl_elem_value *ucontrol)
124 {
125 	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
126 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
127 
128 	ucontrol->value.enumerated.item[0] = wm8731->deemph;
129 
130 	return 0;
131 }
132 
133 static int wm8731_put_deemph(struct snd_kcontrol *kcontrol,
134 			     struct snd_ctl_elem_value *ucontrol)
135 {
136 	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
137 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
138 	int deemph = ucontrol->value.enumerated.item[0];
139 	int ret = 0;
140 
141 	if (deemph > 1)
142 		return -EINVAL;
143 
144 	mutex_lock(&wm8731->lock);
145 	if (wm8731->deemph != deemph) {
146 		wm8731->deemph = deemph;
147 
148 		wm8731_set_deemph(codec);
149 
150 		ret = 1;
151 	}
152 	mutex_unlock(&wm8731->lock);
153 
154 	return ret;
155 }
156 
157 static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
158 static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
159 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
160 static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 2000, 0);
161 
162 static const struct snd_kcontrol_new wm8731_snd_controls[] = {
163 
164 SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
165 		 0, 127, 0, out_tlv),
166 SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
167 	7, 1, 0),
168 
169 SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
170 		 in_tlv),
171 SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
172 
173 SOC_SINGLE_TLV("Mic Boost Volume", WM8731_APANA, 0, 1, 0, mic_tlv),
174 SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
175 
176 SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
177 	       sidetone_tlv),
178 
179 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
180 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
181 
182 SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
183 		    wm8731_get_deemph, wm8731_put_deemph),
184 };
185 
186 /* Output Mixer */
187 static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
188 SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
189 SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
190 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
191 };
192 
193 /* Input mux */
194 static const struct snd_kcontrol_new wm8731_input_mux_controls =
195 SOC_DAPM_ENUM("Input Select", wm8731_insel_enum);
196 
197 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
198 SND_SOC_DAPM_SUPPLY("ACTIVE",WM8731_ACTIVE, 0, 0, NULL, 0),
199 SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0),
200 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
201 	&wm8731_output_mixer_controls[0],
202 	ARRAY_SIZE(wm8731_output_mixer_controls)),
203 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
204 SND_SOC_DAPM_OUTPUT("LOUT"),
205 SND_SOC_DAPM_OUTPUT("LHPOUT"),
206 SND_SOC_DAPM_OUTPUT("ROUT"),
207 SND_SOC_DAPM_OUTPUT("RHPOUT"),
208 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
209 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
210 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
211 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
212 SND_SOC_DAPM_INPUT("MICIN"),
213 SND_SOC_DAPM_INPUT("RLINEIN"),
214 SND_SOC_DAPM_INPUT("LLINEIN"),
215 };
216 
217 static int wm8731_check_osc(struct snd_soc_dapm_widget *source,
218 			    struct snd_soc_dapm_widget *sink)
219 {
220 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(source->codec);
221 
222 	return wm8731->sysclk_type == WM8731_SYSCLK_XTAL;
223 }
224 
225 static const struct snd_soc_dapm_route wm8731_intercon[] = {
226 	{"DAC", NULL, "OSC", wm8731_check_osc},
227 	{"ADC", NULL, "OSC", wm8731_check_osc},
228 	{"DAC", NULL, "ACTIVE"},
229 	{"ADC", NULL, "ACTIVE"},
230 
231 	/* output mixer */
232 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
233 	{"Output Mixer", "HiFi Playback Switch", "DAC"},
234 	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
235 
236 	/* outputs */
237 	{"RHPOUT", NULL, "Output Mixer"},
238 	{"ROUT", NULL, "Output Mixer"},
239 	{"LHPOUT", NULL, "Output Mixer"},
240 	{"LOUT", NULL, "Output Mixer"},
241 
242 	/* input mux */
243 	{"Input Mux", "Line In", "Line Input"},
244 	{"Input Mux", "Mic", "Mic Bias"},
245 	{"ADC", NULL, "Input Mux"},
246 
247 	/* inputs */
248 	{"Line Input", NULL, "LLINEIN"},
249 	{"Line Input", NULL, "RLINEIN"},
250 	{"Mic Bias", NULL, "MICIN"},
251 };
252 
253 struct _coeff_div {
254 	u32 mclk;
255 	u32 rate;
256 	u16 fs;
257 	u8 sr:4;
258 	u8 bosr:1;
259 	u8 usb:1;
260 };
261 
262 /* codec mclk clock divider coefficients */
263 static const struct _coeff_div coeff_div[] = {
264 	/* 48k */
265 	{12288000, 48000, 256, 0x0, 0x0, 0x0},
266 	{18432000, 48000, 384, 0x0, 0x1, 0x0},
267 	{12000000, 48000, 250, 0x0, 0x0, 0x1},
268 
269 	/* 32k */
270 	{12288000, 32000, 384, 0x6, 0x0, 0x0},
271 	{18432000, 32000, 576, 0x6, 0x1, 0x0},
272 	{12000000, 32000, 375, 0x6, 0x0, 0x1},
273 
274 	/* 8k */
275 	{12288000, 8000, 1536, 0x3, 0x0, 0x0},
276 	{18432000, 8000, 2304, 0x3, 0x1, 0x0},
277 	{11289600, 8000, 1408, 0xb, 0x0, 0x0},
278 	{16934400, 8000, 2112, 0xb, 0x1, 0x0},
279 	{12000000, 8000, 1500, 0x3, 0x0, 0x1},
280 
281 	/* 96k */
282 	{12288000, 96000, 128, 0x7, 0x0, 0x0},
283 	{18432000, 96000, 192, 0x7, 0x1, 0x0},
284 	{12000000, 96000, 125, 0x7, 0x0, 0x1},
285 
286 	/* 44.1k */
287 	{11289600, 44100, 256, 0x8, 0x0, 0x0},
288 	{16934400, 44100, 384, 0x8, 0x1, 0x0},
289 	{12000000, 44100, 272, 0x8, 0x1, 0x1},
290 
291 	/* 88.2k */
292 	{11289600, 88200, 128, 0xf, 0x0, 0x0},
293 	{16934400, 88200, 192, 0xf, 0x1, 0x0},
294 	{12000000, 88200, 136, 0xf, 0x1, 0x1},
295 };
296 
297 /* rates constraints */
298 static const unsigned int wm8731_rates_12000000[] = {
299 	8000, 32000, 44100, 48000, 96000, 88200,
300 };
301 
302 static const unsigned int wm8731_rates_12288000_18432000[] = {
303 	8000, 32000, 48000, 96000,
304 };
305 
306 static const unsigned int wm8731_rates_11289600_16934400[] = {
307 	8000, 44100, 88200,
308 };
309 
310 static const struct snd_pcm_hw_constraint_list wm8731_constraints_12000000 = {
311 	.list = wm8731_rates_12000000,
312 	.count = ARRAY_SIZE(wm8731_rates_12000000),
313 };
314 
315 static const
316 struct snd_pcm_hw_constraint_list wm8731_constraints_12288000_18432000 = {
317 	.list = wm8731_rates_12288000_18432000,
318 	.count = ARRAY_SIZE(wm8731_rates_12288000_18432000),
319 };
320 
321 static const
322 struct snd_pcm_hw_constraint_list wm8731_constraints_11289600_16934400 = {
323 	.list = wm8731_rates_11289600_16934400,
324 	.count = ARRAY_SIZE(wm8731_rates_11289600_16934400),
325 };
326 
327 static inline int get_coeff(int mclk, int rate)
328 {
329 	int i;
330 
331 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
332 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
333 			return i;
334 	}
335 	return 0;
336 }
337 
338 static int wm8731_hw_params(struct snd_pcm_substream *substream,
339 			    struct snd_pcm_hw_params *params,
340 			    struct snd_soc_dai *dai)
341 {
342 	struct snd_soc_codec *codec = dai->codec;
343 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
344 	u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
345 	int i = get_coeff(wm8731->sysclk, params_rate(params));
346 	u16 srate = (coeff_div[i].sr << 2) |
347 		(coeff_div[i].bosr << 1) | coeff_div[i].usb;
348 
349 	wm8731->playback_fs = params_rate(params);
350 
351 	snd_soc_write(codec, WM8731_SRATE, srate);
352 
353 	/* bit size */
354 	switch (params_width(params)) {
355 	case 16:
356 		break;
357 	case 20:
358 		iface |= 0x0004;
359 		break;
360 	case 24:
361 		iface |= 0x0008;
362 		break;
363 	}
364 
365 	wm8731_set_deemph(codec);
366 
367 	snd_soc_write(codec, WM8731_IFACE, iface);
368 	return 0;
369 }
370 
371 static int wm8731_mute(struct snd_soc_dai *dai, int mute)
372 {
373 	struct snd_soc_codec *codec = dai->codec;
374 	u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
375 
376 	if (mute)
377 		snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
378 	else
379 		snd_soc_write(codec, WM8731_APDIGI, mute_reg);
380 	return 0;
381 }
382 
383 static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
384 		int clk_id, unsigned int freq, int dir)
385 {
386 	struct snd_soc_codec *codec = codec_dai->codec;
387 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
388 
389 	switch (clk_id) {
390 	case WM8731_SYSCLK_XTAL:
391 	case WM8731_SYSCLK_MCLK:
392 		wm8731->sysclk_type = clk_id;
393 		break;
394 	default:
395 		return -EINVAL;
396 	}
397 
398 	switch (freq) {
399 	case 0:
400 		wm8731->constraints = NULL;
401 		break;
402 	case 12000000:
403 		wm8731->constraints = &wm8731_constraints_12000000;
404 		break;
405 	case 12288000:
406 	case 18432000:
407 		wm8731->constraints = &wm8731_constraints_12288000_18432000;
408 		break;
409 	case 16934400:
410 	case 11289600:
411 		wm8731->constraints = &wm8731_constraints_11289600_16934400;
412 		break;
413 	default:
414 		return -EINVAL;
415 	}
416 
417 	wm8731->sysclk = freq;
418 
419 	snd_soc_dapm_sync(&codec->dapm);
420 
421 	return 0;
422 }
423 
424 
425 static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
426 		unsigned int fmt)
427 {
428 	struct snd_soc_codec *codec = codec_dai->codec;
429 	u16 iface = 0;
430 
431 	/* set master/slave audio interface */
432 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
433 	case SND_SOC_DAIFMT_CBM_CFM:
434 		iface |= 0x0040;
435 		break;
436 	case SND_SOC_DAIFMT_CBS_CFS:
437 		break;
438 	default:
439 		return -EINVAL;
440 	}
441 
442 	/* interface format */
443 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
444 	case SND_SOC_DAIFMT_I2S:
445 		iface |= 0x0002;
446 		break;
447 	case SND_SOC_DAIFMT_RIGHT_J:
448 		break;
449 	case SND_SOC_DAIFMT_LEFT_J:
450 		iface |= 0x0001;
451 		break;
452 	case SND_SOC_DAIFMT_DSP_A:
453 		iface |= 0x0013;
454 		break;
455 	case SND_SOC_DAIFMT_DSP_B:
456 		iface |= 0x0003;
457 		break;
458 	default:
459 		return -EINVAL;
460 	}
461 
462 	/* clock inversion */
463 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
464 	case SND_SOC_DAIFMT_NB_NF:
465 		break;
466 	case SND_SOC_DAIFMT_IB_IF:
467 		iface |= 0x0090;
468 		break;
469 	case SND_SOC_DAIFMT_IB_NF:
470 		iface |= 0x0080;
471 		break;
472 	case SND_SOC_DAIFMT_NB_IF:
473 		iface |= 0x0010;
474 		break;
475 	default:
476 		return -EINVAL;
477 	}
478 
479 	/* set iface */
480 	snd_soc_write(codec, WM8731_IFACE, iface);
481 	return 0;
482 }
483 
484 static int wm8731_set_bias_level(struct snd_soc_codec *codec,
485 				 enum snd_soc_bias_level level)
486 {
487 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
488 	int ret;
489 	u16 reg;
490 
491 	switch (level) {
492 	case SND_SOC_BIAS_ON:
493 		break;
494 	case SND_SOC_BIAS_PREPARE:
495 		break;
496 	case SND_SOC_BIAS_STANDBY:
497 		if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
498 			ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
499 						    wm8731->supplies);
500 			if (ret != 0)
501 				return ret;
502 
503 			regcache_sync(wm8731->regmap);
504 		}
505 
506 		/* Clear PWROFF, gate CLKOUT, everything else as-is */
507 		reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
508 		snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
509 		break;
510 	case SND_SOC_BIAS_OFF:
511 		snd_soc_write(codec, WM8731_PWR, 0xffff);
512 		regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
513 				       wm8731->supplies);
514 		regcache_mark_dirty(wm8731->regmap);
515 		break;
516 	}
517 	codec->dapm.bias_level = level;
518 	return 0;
519 }
520 
521 static int wm8731_startup(struct snd_pcm_substream *substream,
522 	struct snd_soc_dai *dai)
523 {
524 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(dai->codec);
525 
526 	if (wm8731->constraints)
527 		snd_pcm_hw_constraint_list(substream->runtime, 0,
528 					   SNDRV_PCM_HW_PARAM_RATE,
529 					   wm8731->constraints);
530 
531 	return 0;
532 }
533 
534 #define WM8731_RATES SNDRV_PCM_RATE_8000_96000
535 
536 #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
537 	SNDRV_PCM_FMTBIT_S24_LE)
538 
539 static const struct snd_soc_dai_ops wm8731_dai_ops = {
540 	.startup	= wm8731_startup,
541 	.hw_params	= wm8731_hw_params,
542 	.digital_mute	= wm8731_mute,
543 	.set_sysclk	= wm8731_set_dai_sysclk,
544 	.set_fmt	= wm8731_set_dai_fmt,
545 };
546 
547 static struct snd_soc_dai_driver wm8731_dai = {
548 	.name = "wm8731-hifi",
549 	.playback = {
550 		.stream_name = "Playback",
551 		.channels_min = 1,
552 		.channels_max = 2,
553 		.rates = WM8731_RATES,
554 		.formats = WM8731_FORMATS,},
555 	.capture = {
556 		.stream_name = "Capture",
557 		.channels_min = 1,
558 		.channels_max = 2,
559 		.rates = WM8731_RATES,
560 		.formats = WM8731_FORMATS,},
561 	.ops = &wm8731_dai_ops,
562 	.symmetric_rates = 1,
563 };
564 
565 #ifdef CONFIG_PM
566 static int wm8731_suspend(struct snd_soc_codec *codec)
567 {
568 	wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
569 
570 	return 0;
571 }
572 
573 static int wm8731_resume(struct snd_soc_codec *codec)
574 {
575 	wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
576 
577 	return 0;
578 }
579 #else
580 #define wm8731_suspend NULL
581 #define wm8731_resume NULL
582 #endif
583 
584 static int wm8731_probe(struct snd_soc_codec *codec)
585 {
586 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
587 	int ret = 0, i;
588 
589 	for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
590 		wm8731->supplies[i].supply = wm8731_supply_names[i];
591 
592 	ret = devm_regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies),
593 				 wm8731->supplies);
594 	if (ret != 0) {
595 		dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
596 		return ret;
597 	}
598 
599 	ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
600 				    wm8731->supplies);
601 	if (ret != 0) {
602 		dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
603 		return ret;
604 	}
605 
606 	ret = wm8731_reset(codec);
607 	if (ret < 0) {
608 		dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
609 		goto err_regulator_enable;
610 	}
611 
612 	wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
613 
614 	/* Latch the update bits */
615 	snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
616 	snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
617 	snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
618 	snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
619 
620 	/* Disable bypass path by default */
621 	snd_soc_update_bits(codec, WM8731_APANA, 0x8, 0);
622 
623 	/* Regulators will have been enabled by bias management */
624 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
625 
626 	return 0;
627 
628 err_regulator_enable:
629 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
630 
631 	return ret;
632 }
633 
634 /* power down chip */
635 static int wm8731_remove(struct snd_soc_codec *codec)
636 {
637 	struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
638 
639 	wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
640 
641 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
642 
643 	return 0;
644 }
645 
646 static struct snd_soc_codec_driver soc_codec_dev_wm8731 = {
647 	.probe =	wm8731_probe,
648 	.remove =	wm8731_remove,
649 	.suspend =	wm8731_suspend,
650 	.resume =	wm8731_resume,
651 	.set_bias_level = wm8731_set_bias_level,
652 	.dapm_widgets = wm8731_dapm_widgets,
653 	.num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
654 	.dapm_routes = wm8731_intercon,
655 	.num_dapm_routes = ARRAY_SIZE(wm8731_intercon),
656 	.controls =	wm8731_snd_controls,
657 	.num_controls = ARRAY_SIZE(wm8731_snd_controls),
658 };
659 
660 static const struct of_device_id wm8731_of_match[] = {
661 	{ .compatible = "wlf,wm8731", },
662 	{ }
663 };
664 
665 MODULE_DEVICE_TABLE(of, wm8731_of_match);
666 
667 static const struct regmap_config wm8731_regmap = {
668 	.reg_bits = 7,
669 	.val_bits = 9,
670 
671 	.max_register = WM8731_RESET,
672 	.volatile_reg = wm8731_volatile,
673 	.writeable_reg = wm8731_writeable,
674 
675 	.cache_type = REGCACHE_RBTREE,
676 	.reg_defaults = wm8731_reg_defaults,
677 	.num_reg_defaults = ARRAY_SIZE(wm8731_reg_defaults),
678 };
679 
680 #if defined(CONFIG_SPI_MASTER)
681 static int wm8731_spi_probe(struct spi_device *spi)
682 {
683 	struct wm8731_priv *wm8731;
684 	int ret;
685 
686 	wm8731 = devm_kzalloc(&spi->dev, sizeof(struct wm8731_priv),
687 			      GFP_KERNEL);
688 	if (wm8731 == NULL)
689 		return -ENOMEM;
690 
691 	mutex_init(&wm8731->lock);
692 
693 	wm8731->regmap = devm_regmap_init_spi(spi, &wm8731_regmap);
694 	if (IS_ERR(wm8731->regmap)) {
695 		ret = PTR_ERR(wm8731->regmap);
696 		dev_err(&spi->dev, "Failed to allocate register map: %d\n",
697 			ret);
698 		return ret;
699 	}
700 
701 	spi_set_drvdata(spi, wm8731);
702 
703 	ret = snd_soc_register_codec(&spi->dev,
704 			&soc_codec_dev_wm8731, &wm8731_dai, 1);
705 	if (ret != 0) {
706 		dev_err(&spi->dev, "Failed to register CODEC: %d\n", ret);
707 		return ret;
708 	}
709 
710 	return 0;
711 }
712 
713 static int wm8731_spi_remove(struct spi_device *spi)
714 {
715 	snd_soc_unregister_codec(&spi->dev);
716 	return 0;
717 }
718 
719 static struct spi_driver wm8731_spi_driver = {
720 	.driver = {
721 		.name	= "wm8731",
722 		.owner	= THIS_MODULE,
723 		.of_match_table = wm8731_of_match,
724 	},
725 	.probe		= wm8731_spi_probe,
726 	.remove		= wm8731_spi_remove,
727 };
728 #endif /* CONFIG_SPI_MASTER */
729 
730 #if IS_ENABLED(CONFIG_I2C)
731 static int wm8731_i2c_probe(struct i2c_client *i2c,
732 			    const struct i2c_device_id *id)
733 {
734 	struct wm8731_priv *wm8731;
735 	int ret;
736 
737 	wm8731 = devm_kzalloc(&i2c->dev, sizeof(struct wm8731_priv),
738 			      GFP_KERNEL);
739 	if (wm8731 == NULL)
740 		return -ENOMEM;
741 
742 	wm8731->regmap = devm_regmap_init_i2c(i2c, &wm8731_regmap);
743 	if (IS_ERR(wm8731->regmap)) {
744 		ret = PTR_ERR(wm8731->regmap);
745 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
746 			ret);
747 		return ret;
748 	}
749 
750 	i2c_set_clientdata(i2c, wm8731);
751 
752 	ret = snd_soc_register_codec(&i2c->dev,
753 			&soc_codec_dev_wm8731, &wm8731_dai, 1);
754 	if (ret != 0) {
755 		dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
756 		return ret;
757 	}
758 
759 	return 0;
760 }
761 
762 static int wm8731_i2c_remove(struct i2c_client *client)
763 {
764 	snd_soc_unregister_codec(&client->dev);
765 	return 0;
766 }
767 
768 static const struct i2c_device_id wm8731_i2c_id[] = {
769 	{ "wm8731", 0 },
770 	{ }
771 };
772 MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
773 
774 static struct i2c_driver wm8731_i2c_driver = {
775 	.driver = {
776 		.name = "wm8731",
777 		.owner = THIS_MODULE,
778 		.of_match_table = wm8731_of_match,
779 	},
780 	.probe =    wm8731_i2c_probe,
781 	.remove =   wm8731_i2c_remove,
782 	.id_table = wm8731_i2c_id,
783 };
784 #endif
785 
786 static int __init wm8731_modinit(void)
787 {
788 	int ret = 0;
789 #if IS_ENABLED(CONFIG_I2C)
790 	ret = i2c_add_driver(&wm8731_i2c_driver);
791 	if (ret != 0) {
792 		printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
793 		       ret);
794 	}
795 #endif
796 #if defined(CONFIG_SPI_MASTER)
797 	ret = spi_register_driver(&wm8731_spi_driver);
798 	if (ret != 0) {
799 		printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
800 		       ret);
801 	}
802 #endif
803 	return ret;
804 }
805 module_init(wm8731_modinit);
806 
807 static void __exit wm8731_exit(void)
808 {
809 #if IS_ENABLED(CONFIG_I2C)
810 	i2c_del_driver(&wm8731_i2c_driver);
811 #endif
812 #if defined(CONFIG_SPI_MASTER)
813 	spi_unregister_driver(&wm8731_spi_driver);
814 #endif
815 }
816 module_exit(wm8731_exit);
817 
818 MODULE_DESCRIPTION("ASoC WM8731 driver");
819 MODULE_AUTHOR("Richard Purdie");
820 MODULE_LICENSE("GPL");
821