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