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