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