xref: /openbmc/linux/sound/soc/codecs/wm8770.c (revision e23feb16)
1 /*
2  * wm8770.c  --  WM8770 ALSA SoC Audio driver
3  *
4  * Copyright 2010 Wolfson Microelectronics plc
5  *
6  * Author: Dimitris Papastamos <dp@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 
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/of_device.h>
18 #include <linux/pm.h>
19 #include <linux/spi/spi.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
29 
30 #include "wm8770.h"
31 
32 #define WM8770_NUM_SUPPLIES 3
33 static const char *wm8770_supply_names[WM8770_NUM_SUPPLIES] = {
34 	"AVDD1",
35 	"AVDD2",
36 	"DVDD"
37 };
38 
39 static const struct reg_default wm8770_reg_defaults[] = {
40 	{  0, 0x7f },
41 	{  1, 0x7f },
42 	{  2, 0x7f },
43 	{  3, 0x7f },
44 	{  4, 0x7f },
45 	{  5, 0x7f },
46 	{  6, 0x7f },
47 	{  7, 0x7f },
48 	{  8, 0x7f },
49 	{  9, 0xff },
50 	{ 10, 0xff },
51 	{ 11, 0xff },
52 	{ 12, 0xff },
53 	{ 13, 0xff },
54 	{ 14, 0xff },
55 	{ 15, 0xff },
56 	{ 16, 0xff },
57 	{ 17, 0xff },
58 	{ 18, 0    },
59 	{ 19, 0x90 },
60 	{ 20, 0    },
61 	{ 21, 0    },
62 	{ 22, 0x22 },
63 	{ 23, 0x22 },
64 	{ 24, 0x3e },
65 	{ 25, 0xc  },
66 	{ 26, 0xc  },
67 	{ 27, 0x100 },
68 	{ 28, 0x189 },
69 	{ 29, 0x189 },
70 	{ 30, 0x8770 },
71 };
72 
73 static bool wm8770_volatile_reg(struct device *dev, unsigned int reg)
74 {
75 	switch (reg) {
76 	case WM8770_RESET:
77 		return true;
78 	default:
79 		return false;
80 	}
81 }
82 
83 struct wm8770_priv {
84 	struct regmap *regmap;
85 	struct regulator_bulk_data supplies[WM8770_NUM_SUPPLIES];
86 	struct notifier_block disable_nb[WM8770_NUM_SUPPLIES];
87 	struct snd_soc_codec *codec;
88 	int sysclk;
89 };
90 
91 static int vout12supply_event(struct snd_soc_dapm_widget *w,
92 	struct snd_kcontrol *kcontrol, int event);
93 static int vout34supply_event(struct snd_soc_dapm_widget *w,
94 	struct snd_kcontrol *kcontrol, int event);
95 
96 /*
97  * We can't use the same notifier block for more than one supply and
98  * there's no way I can see to get from a callback to the caller
99  * except container_of().
100  */
101 #define WM8770_REGULATOR_EVENT(n) \
102 static int wm8770_regulator_event_##n(struct notifier_block *nb, \
103 				      unsigned long event, void *data)    \
104 { \
105 	struct wm8770_priv *wm8770 = container_of(nb, struct wm8770_priv, \
106 				     disable_nb[n]); \
107 	if (event & REGULATOR_EVENT_DISABLE) { \
108 		regcache_mark_dirty(wm8770->regmap);	\
109 	} \
110 	return 0; \
111 }
112 
113 WM8770_REGULATOR_EVENT(0)
114 WM8770_REGULATOR_EVENT(1)
115 WM8770_REGULATOR_EVENT(2)
116 
117 static const DECLARE_TLV_DB_SCALE(adc_tlv, -1200, 100, 0);
118 static const DECLARE_TLV_DB_SCALE(dac_dig_tlv, -12750, 50, 1);
119 static const DECLARE_TLV_DB_SCALE(dac_alg_tlv, -12700, 100, 1);
120 
121 static const char *dac_phase_text[][2] = {
122 	{ "DAC1 Normal", "DAC1 Inverted" },
123 	{ "DAC2 Normal", "DAC2 Inverted" },
124 	{ "DAC3 Normal", "DAC3 Inverted" },
125 	{ "DAC4 Normal", "DAC4 Inverted" },
126 };
127 
128 static const struct soc_enum dac_phase[] = {
129 	SOC_ENUM_DOUBLE(WM8770_DACPHASE, 0, 1, 2, dac_phase_text[0]),
130 	SOC_ENUM_DOUBLE(WM8770_DACPHASE, 2, 3, 2, dac_phase_text[1]),
131 	SOC_ENUM_DOUBLE(WM8770_DACPHASE, 4, 5, 2, dac_phase_text[2]),
132 	SOC_ENUM_DOUBLE(WM8770_DACPHASE, 6, 7, 2, dac_phase_text[3]),
133 };
134 
135 static const struct snd_kcontrol_new wm8770_snd_controls[] = {
136 	/* global DAC playback controls */
137 	SOC_SINGLE_TLV("DAC Playback Volume", WM8770_MSDIGVOL, 0, 255, 0,
138 		dac_dig_tlv),
139 	SOC_SINGLE("DAC Playback Switch", WM8770_DACMUTE, 4, 1, 1),
140 	SOC_SINGLE("DAC Playback ZC Switch", WM8770_DACCTRL1, 0, 1, 0),
141 
142 	/* global VOUT playback controls */
143 	SOC_SINGLE_TLV("VOUT Playback Volume", WM8770_MSALGVOL, 0, 127, 0,
144 		dac_alg_tlv),
145 	SOC_SINGLE("VOUT Playback ZC Switch", WM8770_MSALGVOL, 7, 1, 0),
146 
147 	/* VOUT1/2/3/4 specific controls */
148 	SOC_DOUBLE_R_TLV("VOUT1 Playback Volume", WM8770_VOUT1LVOL,
149 		WM8770_VOUT1RVOL, 0, 127, 0, dac_alg_tlv),
150 	SOC_DOUBLE_R("VOUT1 Playback ZC Switch", WM8770_VOUT1LVOL,
151 		WM8770_VOUT1RVOL, 7, 1, 0),
152 	SOC_DOUBLE_R_TLV("VOUT2 Playback Volume", WM8770_VOUT2LVOL,
153 		WM8770_VOUT2RVOL, 0, 127, 0, dac_alg_tlv),
154 	SOC_DOUBLE_R("VOUT2 Playback ZC Switch", WM8770_VOUT2LVOL,
155 		WM8770_VOUT2RVOL, 7, 1, 0),
156 	SOC_DOUBLE_R_TLV("VOUT3 Playback Volume", WM8770_VOUT3LVOL,
157 		WM8770_VOUT3RVOL, 0, 127, 0, dac_alg_tlv),
158 	SOC_DOUBLE_R("VOUT3 Playback ZC Switch", WM8770_VOUT3LVOL,
159 		WM8770_VOUT3RVOL, 7, 1, 0),
160 	SOC_DOUBLE_R_TLV("VOUT4 Playback Volume", WM8770_VOUT4LVOL,
161 		WM8770_VOUT4RVOL, 0, 127, 0, dac_alg_tlv),
162 	SOC_DOUBLE_R("VOUT4 Playback ZC Switch", WM8770_VOUT4LVOL,
163 		WM8770_VOUT4RVOL, 7, 1, 0),
164 
165 	/* DAC1/2/3/4 specific controls */
166 	SOC_DOUBLE_R_TLV("DAC1 Playback Volume", WM8770_DAC1LVOL,
167 		WM8770_DAC1RVOL, 0, 255, 0, dac_dig_tlv),
168 	SOC_SINGLE("DAC1 Deemphasis Switch", WM8770_DACCTRL2, 0, 1, 0),
169 	SOC_ENUM("DAC1 Phase", dac_phase[0]),
170 	SOC_DOUBLE_R_TLV("DAC2 Playback Volume", WM8770_DAC2LVOL,
171 		WM8770_DAC2RVOL, 0, 255, 0, dac_dig_tlv),
172 	SOC_SINGLE("DAC2 Deemphasis Switch", WM8770_DACCTRL2, 1, 1, 0),
173 	SOC_ENUM("DAC2 Phase", dac_phase[1]),
174 	SOC_DOUBLE_R_TLV("DAC3 Playback Volume", WM8770_DAC3LVOL,
175 		WM8770_DAC3RVOL, 0, 255, 0, dac_dig_tlv),
176 	SOC_SINGLE("DAC3 Deemphasis Switch", WM8770_DACCTRL2, 2, 1, 0),
177 	SOC_ENUM("DAC3 Phase", dac_phase[2]),
178 	SOC_DOUBLE_R_TLV("DAC4 Playback Volume", WM8770_DAC4LVOL,
179 		WM8770_DAC4RVOL, 0, 255, 0, dac_dig_tlv),
180 	SOC_SINGLE("DAC4 Deemphasis Switch", WM8770_DACCTRL2, 3, 1, 0),
181 	SOC_ENUM("DAC4 Phase", dac_phase[3]),
182 
183 	/* ADC specific controls */
184 	SOC_DOUBLE_R_TLV("Capture Volume", WM8770_ADCLCTRL, WM8770_ADCRCTRL,
185 		0, 31, 0, adc_tlv),
186 	SOC_DOUBLE_R("Capture Switch", WM8770_ADCLCTRL, WM8770_ADCRCTRL,
187 		5, 1, 1),
188 
189 	/* other controls */
190 	SOC_SINGLE("ADC 128x Oversampling Switch", WM8770_MSTRCTRL, 3, 1, 0),
191 	SOC_SINGLE("ADC Highpass Filter Switch", WM8770_IFACECTRL, 8, 1, 1)
192 };
193 
194 static const char *ain_text[] = {
195 	"AIN1", "AIN2", "AIN3", "AIN4",
196 	"AIN5", "AIN6", "AIN7", "AIN8"
197 };
198 
199 static const struct soc_enum ain_enum =
200 	SOC_ENUM_DOUBLE(WM8770_ADCMUX, 0, 4, 8, ain_text);
201 
202 static const struct snd_kcontrol_new ain_mux =
203 	SOC_DAPM_ENUM("Capture Mux", ain_enum);
204 
205 static const struct snd_kcontrol_new vout1_mix_controls[] = {
206 	SOC_DAPM_SINGLE("DAC1 Switch", WM8770_OUTMUX1, 0, 1, 0),
207 	SOC_DAPM_SINGLE("AUX1 Switch", WM8770_OUTMUX1, 1, 1, 0),
208 	SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX1, 2, 1, 0)
209 };
210 
211 static const struct snd_kcontrol_new vout2_mix_controls[] = {
212 	SOC_DAPM_SINGLE("DAC2 Switch", WM8770_OUTMUX1, 3, 1, 0),
213 	SOC_DAPM_SINGLE("AUX2 Switch", WM8770_OUTMUX1, 4, 1, 0),
214 	SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX1, 5, 1, 0)
215 };
216 
217 static const struct snd_kcontrol_new vout3_mix_controls[] = {
218 	SOC_DAPM_SINGLE("DAC3 Switch", WM8770_OUTMUX2, 0, 1, 0),
219 	SOC_DAPM_SINGLE("AUX3 Switch", WM8770_OUTMUX2, 1, 1, 0),
220 	SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX2, 2, 1, 0)
221 };
222 
223 static const struct snd_kcontrol_new vout4_mix_controls[] = {
224 	SOC_DAPM_SINGLE("DAC4 Switch", WM8770_OUTMUX2, 3, 1, 0),
225 	SOC_DAPM_SINGLE("Bypass Switch", WM8770_OUTMUX2, 4, 1, 0)
226 };
227 
228 static const struct snd_soc_dapm_widget wm8770_dapm_widgets[] = {
229 	SND_SOC_DAPM_INPUT("AUX1"),
230 	SND_SOC_DAPM_INPUT("AUX2"),
231 	SND_SOC_DAPM_INPUT("AUX3"),
232 
233 	SND_SOC_DAPM_INPUT("AIN1"),
234 	SND_SOC_DAPM_INPUT("AIN2"),
235 	SND_SOC_DAPM_INPUT("AIN3"),
236 	SND_SOC_DAPM_INPUT("AIN4"),
237 	SND_SOC_DAPM_INPUT("AIN5"),
238 	SND_SOC_DAPM_INPUT("AIN6"),
239 	SND_SOC_DAPM_INPUT("AIN7"),
240 	SND_SOC_DAPM_INPUT("AIN8"),
241 
242 	SND_SOC_DAPM_MUX("Capture Mux", WM8770_ADCMUX, 8, 1, &ain_mux),
243 
244 	SND_SOC_DAPM_ADC("ADC", "Capture", WM8770_PWDNCTRL, 1, 1),
245 
246 	SND_SOC_DAPM_DAC("DAC1", "Playback", WM8770_PWDNCTRL, 2, 1),
247 	SND_SOC_DAPM_DAC("DAC2", "Playback", WM8770_PWDNCTRL, 3, 1),
248 	SND_SOC_DAPM_DAC("DAC3", "Playback", WM8770_PWDNCTRL, 4, 1),
249 	SND_SOC_DAPM_DAC("DAC4", "Playback", WM8770_PWDNCTRL, 5, 1),
250 
251 	SND_SOC_DAPM_SUPPLY("VOUT12 Supply", SND_SOC_NOPM, 0, 0,
252 		vout12supply_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
253 	SND_SOC_DAPM_SUPPLY("VOUT34 Supply", SND_SOC_NOPM, 0, 0,
254 		vout34supply_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
255 
256 	SND_SOC_DAPM_MIXER("VOUT1 Mixer", SND_SOC_NOPM, 0, 0,
257 		vout1_mix_controls, ARRAY_SIZE(vout1_mix_controls)),
258 	SND_SOC_DAPM_MIXER("VOUT2 Mixer", SND_SOC_NOPM, 0, 0,
259 		vout2_mix_controls, ARRAY_SIZE(vout2_mix_controls)),
260 	SND_SOC_DAPM_MIXER("VOUT3 Mixer", SND_SOC_NOPM, 0, 0,
261 		vout3_mix_controls, ARRAY_SIZE(vout3_mix_controls)),
262 	SND_SOC_DAPM_MIXER("VOUT4 Mixer", SND_SOC_NOPM, 0, 0,
263 		vout4_mix_controls, ARRAY_SIZE(vout4_mix_controls)),
264 
265 	SND_SOC_DAPM_OUTPUT("VOUT1"),
266 	SND_SOC_DAPM_OUTPUT("VOUT2"),
267 	SND_SOC_DAPM_OUTPUT("VOUT3"),
268 	SND_SOC_DAPM_OUTPUT("VOUT4")
269 };
270 
271 static const struct snd_soc_dapm_route wm8770_intercon[] = {
272 	{ "Capture Mux", "AIN1", "AIN1" },
273 	{ "Capture Mux", "AIN2", "AIN2" },
274 	{ "Capture Mux", "AIN3", "AIN3" },
275 	{ "Capture Mux", "AIN4", "AIN4" },
276 	{ "Capture Mux", "AIN5", "AIN5" },
277 	{ "Capture Mux", "AIN6", "AIN6" },
278 	{ "Capture Mux", "AIN7", "AIN7" },
279 	{ "Capture Mux", "AIN8", "AIN8" },
280 
281 	{ "ADC", NULL, "Capture Mux" },
282 
283 	{ "VOUT1 Mixer", NULL, "VOUT12 Supply" },
284 	{ "VOUT1 Mixer", "DAC1 Switch", "DAC1" },
285 	{ "VOUT1 Mixer", "AUX1 Switch", "AUX1" },
286 	{ "VOUT1 Mixer", "Bypass Switch", "Capture Mux" },
287 
288 	{ "VOUT2 Mixer", NULL, "VOUT12 Supply" },
289 	{ "VOUT2 Mixer", "DAC2 Switch", "DAC2" },
290 	{ "VOUT2 Mixer", "AUX2 Switch", "AUX2" },
291 	{ "VOUT2 Mixer", "Bypass Switch", "Capture Mux" },
292 
293 	{ "VOUT3 Mixer", NULL, "VOUT34 Supply" },
294 	{ "VOUT3 Mixer", "DAC3 Switch", "DAC3" },
295 	{ "VOUT3 Mixer", "AUX3 Switch", "AUX3" },
296 	{ "VOUT3 Mixer", "Bypass Switch", "Capture Mux" },
297 
298 	{ "VOUT4 Mixer", NULL, "VOUT34 Supply" },
299 	{ "VOUT4 Mixer", "DAC4 Switch", "DAC4" },
300 	{ "VOUT4 Mixer", "Bypass Switch", "Capture Mux" },
301 
302 	{ "VOUT1", NULL, "VOUT1 Mixer" },
303 	{ "VOUT2", NULL, "VOUT2 Mixer" },
304 	{ "VOUT3", NULL, "VOUT3 Mixer" },
305 	{ "VOUT4", NULL, "VOUT4 Mixer" }
306 };
307 
308 static int vout12supply_event(struct snd_soc_dapm_widget *w,
309 	struct snd_kcontrol *kcontrol, int event)
310 {
311 	struct snd_soc_codec *codec;
312 
313 	codec = w->codec;
314 
315 	switch (event) {
316 	case SND_SOC_DAPM_PRE_PMU:
317 		snd_soc_update_bits(codec, WM8770_OUTMUX1, 0x180, 0);
318 		break;
319 	case SND_SOC_DAPM_POST_PMD:
320 		snd_soc_update_bits(codec, WM8770_OUTMUX1, 0x180, 0x180);
321 		break;
322 	}
323 
324 	return 0;
325 }
326 
327 static int vout34supply_event(struct snd_soc_dapm_widget *w,
328 	struct snd_kcontrol *kcontrol, int event)
329 {
330 	struct snd_soc_codec *codec;
331 
332 	codec = w->codec;
333 
334 	switch (event) {
335 	case SND_SOC_DAPM_PRE_PMU:
336 		snd_soc_update_bits(codec, WM8770_OUTMUX2, 0x180, 0);
337 		break;
338 	case SND_SOC_DAPM_POST_PMD:
339 		snd_soc_update_bits(codec, WM8770_OUTMUX2, 0x180, 0x180);
340 		break;
341 	}
342 
343 	return 0;
344 }
345 
346 static int wm8770_reset(struct snd_soc_codec *codec)
347 {
348 	return snd_soc_write(codec, WM8770_RESET, 0);
349 }
350 
351 static int wm8770_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
352 {
353 	struct snd_soc_codec *codec;
354 	int iface, master;
355 
356 	codec = dai->codec;
357 
358 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
359 	case SND_SOC_DAIFMT_CBM_CFM:
360 		master = 0x100;
361 		break;
362 	case SND_SOC_DAIFMT_CBS_CFS:
363 		master = 0;
364 		break;
365 	default:
366 		return -EINVAL;
367 	}
368 
369 	iface = 0;
370 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
371 	case SND_SOC_DAIFMT_I2S:
372 		iface |= 0x2;
373 		break;
374 	case SND_SOC_DAIFMT_RIGHT_J:
375 		break;
376 	case SND_SOC_DAIFMT_LEFT_J:
377 		iface |= 0x1;
378 		break;
379 	default:
380 		return -EINVAL;
381 	}
382 
383 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
384 	case SND_SOC_DAIFMT_NB_NF:
385 		break;
386 	case SND_SOC_DAIFMT_IB_IF:
387 		iface |= 0xc;
388 		break;
389 	case SND_SOC_DAIFMT_IB_NF:
390 		iface |= 0x8;
391 		break;
392 	case SND_SOC_DAIFMT_NB_IF:
393 		iface |= 0x4;
394 		break;
395 	default:
396 		return -EINVAL;
397 	}
398 
399 	snd_soc_update_bits(codec, WM8770_IFACECTRL, 0xf, iface);
400 	snd_soc_update_bits(codec, WM8770_MSTRCTRL, 0x100, master);
401 
402 	return 0;
403 }
404 
405 static const int mclk_ratios[] = {
406 	128,
407 	192,
408 	256,
409 	384,
410 	512,
411 	768
412 };
413 
414 static int wm8770_hw_params(struct snd_pcm_substream *substream,
415 			    struct snd_pcm_hw_params *params,
416 			    struct snd_soc_dai *dai)
417 {
418 	struct snd_soc_codec *codec;
419 	struct wm8770_priv *wm8770;
420 	int i;
421 	int iface;
422 	int shift;
423 	int ratio;
424 
425 	codec = dai->codec;
426 	wm8770 = snd_soc_codec_get_drvdata(codec);
427 
428 	iface = 0;
429 	switch (params_format(params)) {
430 	case SNDRV_PCM_FORMAT_S16_LE:
431 		break;
432 	case SNDRV_PCM_FORMAT_S20_3LE:
433 		iface |= 0x10;
434 		break;
435 	case SNDRV_PCM_FORMAT_S24_LE:
436 		iface |= 0x20;
437 		break;
438 	case SNDRV_PCM_FORMAT_S32_LE:
439 		iface |= 0x30;
440 		break;
441 	}
442 
443 	switch (substream->stream) {
444 	case SNDRV_PCM_STREAM_PLAYBACK:
445 		i = 0;
446 		shift = 4;
447 		break;
448 	case SNDRV_PCM_STREAM_CAPTURE:
449 		i = 2;
450 		shift = 0;
451 		break;
452 	default:
453 		return -EINVAL;
454 	}
455 
456 	/* Only need to set MCLK/LRCLK ratio if we're master */
457 	if (snd_soc_read(codec, WM8770_MSTRCTRL) & 0x100) {
458 		for (; i < ARRAY_SIZE(mclk_ratios); ++i) {
459 			ratio = wm8770->sysclk / params_rate(params);
460 			if (ratio == mclk_ratios[i])
461 				break;
462 		}
463 
464 		if (i == ARRAY_SIZE(mclk_ratios)) {
465 			dev_err(codec->dev,
466 				"Unable to configure MCLK ratio %d/%d\n",
467 				wm8770->sysclk, params_rate(params));
468 			return -EINVAL;
469 		}
470 
471 		dev_dbg(codec->dev, "MCLK is %dfs\n", mclk_ratios[i]);
472 
473 		snd_soc_update_bits(codec, WM8770_MSTRCTRL, 0x7 << shift,
474 				    i << shift);
475 	}
476 
477 	snd_soc_update_bits(codec, WM8770_IFACECTRL, 0x30, iface);
478 
479 	return 0;
480 }
481 
482 static int wm8770_mute(struct snd_soc_dai *dai, int mute)
483 {
484 	struct snd_soc_codec *codec;
485 
486 	codec = dai->codec;
487 	return snd_soc_update_bits(codec, WM8770_DACMUTE, 0x10,
488 				   !!mute << 4);
489 }
490 
491 static int wm8770_set_sysclk(struct snd_soc_dai *dai,
492 			     int clk_id, unsigned int freq, int dir)
493 {
494 	struct snd_soc_codec *codec;
495 	struct wm8770_priv *wm8770;
496 
497 	codec = dai->codec;
498 	wm8770 = snd_soc_codec_get_drvdata(codec);
499 	wm8770->sysclk = freq;
500 	return 0;
501 }
502 
503 static int wm8770_set_bias_level(struct snd_soc_codec *codec,
504 				 enum snd_soc_bias_level level)
505 {
506 	int ret;
507 	struct wm8770_priv *wm8770;
508 
509 	wm8770 = snd_soc_codec_get_drvdata(codec);
510 
511 	switch (level) {
512 	case SND_SOC_BIAS_ON:
513 		break;
514 	case SND_SOC_BIAS_PREPARE:
515 		break;
516 	case SND_SOC_BIAS_STANDBY:
517 		if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
518 			ret = regulator_bulk_enable(ARRAY_SIZE(wm8770->supplies),
519 						    wm8770->supplies);
520 			if (ret) {
521 				dev_err(codec->dev,
522 					"Failed to enable supplies: %d\n",
523 					ret);
524 				return ret;
525 			}
526 
527 			regcache_sync(wm8770->regmap);
528 
529 			/* global powerup */
530 			snd_soc_write(codec, WM8770_PWDNCTRL, 0);
531 		}
532 		break;
533 	case SND_SOC_BIAS_OFF:
534 		/* global powerdown */
535 		snd_soc_write(codec, WM8770_PWDNCTRL, 1);
536 		regulator_bulk_disable(ARRAY_SIZE(wm8770->supplies),
537 				       wm8770->supplies);
538 		break;
539 	}
540 
541 	codec->dapm.bias_level = level;
542 	return 0;
543 }
544 
545 #define WM8770_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
546 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
547 
548 static const struct snd_soc_dai_ops wm8770_dai_ops = {
549 	.digital_mute = wm8770_mute,
550 	.hw_params = wm8770_hw_params,
551 	.set_fmt = wm8770_set_fmt,
552 	.set_sysclk = wm8770_set_sysclk,
553 };
554 
555 static struct snd_soc_dai_driver wm8770_dai = {
556 	.name = "wm8770-hifi",
557 	.playback = {
558 		.stream_name = "Playback",
559 		.channels_min = 2,
560 		.channels_max = 2,
561 		.rates = SNDRV_PCM_RATE_8000_192000,
562 		.formats = WM8770_FORMATS
563 	},
564 	.capture = {
565 		.stream_name = "Capture",
566 		.channels_min = 2,
567 		.channels_max = 2,
568 		.rates = SNDRV_PCM_RATE_8000_96000,
569 		.formats = WM8770_FORMATS
570 	},
571 	.ops = &wm8770_dai_ops,
572 	.symmetric_rates = 1
573 };
574 
575 static int wm8770_probe(struct snd_soc_codec *codec)
576 {
577 	struct wm8770_priv *wm8770;
578 	int ret;
579 
580 	wm8770 = snd_soc_codec_get_drvdata(codec);
581 	wm8770->codec = codec;
582 
583 	ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_REGMAP);
584 	if (ret < 0) {
585 		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
586 		return ret;
587 	}
588 
589 	ret = regulator_bulk_enable(ARRAY_SIZE(wm8770->supplies),
590 				    wm8770->supplies);
591 	if (ret) {
592 		dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
593 		return ret;
594 	}
595 
596 	ret = wm8770_reset(codec);
597 	if (ret < 0) {
598 		dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
599 		goto err_reg_enable;
600 	}
601 
602 	/* latch the volume update bits */
603 	snd_soc_update_bits(codec, WM8770_MSDIGVOL, 0x100, 0x100);
604 	snd_soc_update_bits(codec, WM8770_MSALGVOL, 0x100, 0x100);
605 	snd_soc_update_bits(codec, WM8770_VOUT1RVOL, 0x100, 0x100);
606 	snd_soc_update_bits(codec, WM8770_VOUT2RVOL, 0x100, 0x100);
607 	snd_soc_update_bits(codec, WM8770_VOUT3RVOL, 0x100, 0x100);
608 	snd_soc_update_bits(codec, WM8770_VOUT4RVOL, 0x100, 0x100);
609 	snd_soc_update_bits(codec, WM8770_DAC1RVOL, 0x100, 0x100);
610 	snd_soc_update_bits(codec, WM8770_DAC2RVOL, 0x100, 0x100);
611 	snd_soc_update_bits(codec, WM8770_DAC3RVOL, 0x100, 0x100);
612 	snd_soc_update_bits(codec, WM8770_DAC4RVOL, 0x100, 0x100);
613 
614 	/* mute all DACs */
615 	snd_soc_update_bits(codec, WM8770_DACMUTE, 0x10, 0x10);
616 
617 err_reg_enable:
618 	regulator_bulk_disable(ARRAY_SIZE(wm8770->supplies), wm8770->supplies);
619 	return ret;
620 }
621 
622 static struct snd_soc_codec_driver soc_codec_dev_wm8770 = {
623 	.probe = wm8770_probe,
624 	.set_bias_level = wm8770_set_bias_level,
625 	.idle_bias_off = true,
626 
627 	.controls = wm8770_snd_controls,
628 	.num_controls = ARRAY_SIZE(wm8770_snd_controls),
629 	.dapm_widgets = wm8770_dapm_widgets,
630 	.num_dapm_widgets = ARRAY_SIZE(wm8770_dapm_widgets),
631 	.dapm_routes = wm8770_intercon,
632 	.num_dapm_routes = ARRAY_SIZE(wm8770_intercon),
633 };
634 
635 static const struct of_device_id wm8770_of_match[] = {
636 	{ .compatible = "wlf,wm8770", },
637 	{ }
638 };
639 MODULE_DEVICE_TABLE(of, wm8770_of_match);
640 
641 static const struct regmap_config wm8770_regmap = {
642 	.reg_bits = 7,
643 	.val_bits = 9,
644 	.max_register = WM8770_RESET,
645 
646 	.reg_defaults = wm8770_reg_defaults,
647 	.num_reg_defaults = ARRAY_SIZE(wm8770_reg_defaults),
648 	.cache_type = REGCACHE_RBTREE,
649 
650 	.volatile_reg = wm8770_volatile_reg,
651 };
652 
653 static int wm8770_spi_probe(struct spi_device *spi)
654 {
655 	struct wm8770_priv *wm8770;
656 	int ret, i;
657 
658 	wm8770 = devm_kzalloc(&spi->dev, sizeof(struct wm8770_priv),
659 			      GFP_KERNEL);
660 	if (!wm8770)
661 		return -ENOMEM;
662 
663 	for (i = 0; i < ARRAY_SIZE(wm8770->supplies); i++)
664 		wm8770->supplies[i].supply = wm8770_supply_names[i];
665 
666 	ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(wm8770->supplies),
667 				      wm8770->supplies);
668 	if (ret) {
669 		dev_err(&spi->dev, "Failed to request supplies: %d\n", ret);
670 		return ret;
671 	}
672 
673 	wm8770->disable_nb[0].notifier_call = wm8770_regulator_event_0;
674 	wm8770->disable_nb[1].notifier_call = wm8770_regulator_event_1;
675 	wm8770->disable_nb[2].notifier_call = wm8770_regulator_event_2;
676 
677 	/* This should really be moved into the regulator core */
678 	for (i = 0; i < ARRAY_SIZE(wm8770->supplies); i++) {
679 		ret = regulator_register_notifier(wm8770->supplies[i].consumer,
680 						  &wm8770->disable_nb[i]);
681 		if (ret) {
682 			dev_err(&spi->dev,
683 				"Failed to register regulator notifier: %d\n",
684 				ret);
685 		}
686 	}
687 
688 	wm8770->regmap = devm_regmap_init_spi(spi, &wm8770_regmap);
689 	if (IS_ERR(wm8770->regmap))
690 		return PTR_ERR(wm8770->regmap);
691 
692 	spi_set_drvdata(spi, wm8770);
693 
694 	ret = snd_soc_register_codec(&spi->dev,
695 				     &soc_codec_dev_wm8770, &wm8770_dai, 1);
696 
697 	return ret;
698 }
699 
700 static int wm8770_spi_remove(struct spi_device *spi)
701 {
702 	struct wm8770_priv *wm8770 = spi_get_drvdata(spi);
703 	int i;
704 
705 	for (i = 0; i < ARRAY_SIZE(wm8770->supplies); ++i)
706 		regulator_unregister_notifier(wm8770->supplies[i].consumer,
707 					      &wm8770->disable_nb[i]);
708 
709 	snd_soc_unregister_codec(&spi->dev);
710 
711 	return 0;
712 }
713 
714 static struct spi_driver wm8770_spi_driver = {
715 	.driver = {
716 		.name = "wm8770",
717 		.owner = THIS_MODULE,
718 		.of_match_table = wm8770_of_match,
719 	},
720 	.probe = wm8770_spi_probe,
721 	.remove = wm8770_spi_remove
722 };
723 
724 module_spi_driver(wm8770_spi_driver);
725 
726 MODULE_DESCRIPTION("ASoC WM8770 driver");
727 MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
728 MODULE_LICENSE("GPL");
729