1 // SPDX-License-Identifier: GPL-2.0-only
3 * wm8961.c -- WM8961 ALSA SoC Audio driver
5 * Copyright 2009-10 Wolfson Microelectronics, plc
9 * Currently unimplemented features:
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
18 #include <linux/i2c.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/initval.h>
26 #include <sound/tlv.h>
30 #define WM8961_MAX_REGISTER 0xFC
32 static const struct reg_default wm8961_reg_defaults[] = {
33 { 0, 0x009F }, /* R0 - Left Input volume */
34 { 1, 0x009F }, /* R1 - Right Input volume */
35 { 2, 0x0000 }, /* R2 - LOUT1 volume */
36 { 3, 0x0000 }, /* R3 - ROUT1 volume */
37 { 4, 0x0020 }, /* R4 - Clocking1 */
38 { 5, 0x0008 }, /* R5 - ADC & DAC Control 1 */
39 { 6, 0x0000 }, /* R6 - ADC & DAC Control 2 */
40 { 7, 0x000A }, /* R7 - Audio Interface 0 */
41 { 8, 0x01F4 }, /* R8 - Clocking2 */
42 { 9, 0x0000 }, /* R9 - Audio Interface 1 */
43 { 10, 0x00FF }, /* R10 - Left DAC volume */
44 { 11, 0x00FF }, /* R11 - Right DAC volume */
46 { 14, 0x0040 }, /* R14 - Audio Interface 2 */
48 { 17, 0x007B }, /* R17 - ALC1 */
49 { 18, 0x0000 }, /* R18 - ALC2 */
50 { 19, 0x0032 }, /* R19 - ALC3 */
51 { 20, 0x0000 }, /* R20 - Noise Gate */
52 { 21, 0x00C0 }, /* R21 - Left ADC volume */
53 { 22, 0x00C0 }, /* R22 - Right ADC volume */
54 { 23, 0x0120 }, /* R23 - Additional control(1) */
55 { 24, 0x0000 }, /* R24 - Additional control(2) */
56 { 25, 0x0000 }, /* R25 - Pwr Mgmt (1) */
57 { 26, 0x0000 }, /* R26 - Pwr Mgmt (2) */
58 { 27, 0x0000 }, /* R27 - Additional Control (3) */
59 { 28, 0x0000 }, /* R28 - Anti-pop */
61 { 30, 0x005F }, /* R30 - Clocking 3 */
63 { 32, 0x0000 }, /* R32 - ADCL signal path */
64 { 33, 0x0000 }, /* R33 - ADCR signal path */
66 { 40, 0x0000 }, /* R40 - LOUT2 volume */
67 { 41, 0x0000 }, /* R41 - ROUT2 volume */
69 { 47, 0x0000 }, /* R47 - Pwr Mgmt (3) */
70 { 48, 0x0023 }, /* R48 - Additional Control (4) */
71 { 49, 0x0000 }, /* R49 - Class D Control 1 */
73 { 51, 0x0003 }, /* R51 - Class D Control 2 */
75 { 56, 0x0106 }, /* R56 - Clocking 4 */
76 { 57, 0x0000 }, /* R57 - DSP Sidetone 0 */
77 { 58, 0x0000 }, /* R58 - DSP Sidetone 1 */
79 { 60, 0x0000 }, /* R60 - DC Servo 0 */
80 { 61, 0x0000 }, /* R61 - DC Servo 1 */
82 { 63, 0x015E }, /* R63 - DC Servo 3 */
84 { 65, 0x0010 }, /* R65 - DC Servo 5 */
86 { 68, 0x0003 }, /* R68 - Analogue PGA Bias */
87 { 69, 0x0000 }, /* R69 - Analogue HP 0 */
89 { 71, 0x01FB }, /* R71 - Analogue HP 2 */
90 { 72, 0x0000 }, /* R72 - Charge Pump 1 */
92 { 82, 0x0000 }, /* R82 - Charge Pump B */
94 { 87, 0x0000 }, /* R87 - Write Sequencer 1 */
95 { 88, 0x0000 }, /* R88 - Write Sequencer 2 */
96 { 89, 0x0000 }, /* R89 - Write Sequencer 3 */
97 { 90, 0x0000 }, /* R90 - Write Sequencer 4 */
98 { 91, 0x0000 }, /* R91 - Write Sequencer 5 */
99 { 92, 0x0000 }, /* R92 - Write Sequencer 6 */
100 { 93, 0x0000 }, /* R93 - Write Sequencer 7 */
102 { 252, 0x0001 }, /* R252 - General test 1 */
106 struct regmap *regmap;
110 static bool wm8961_volatile(struct device *dev, unsigned int reg)
113 case WM8961_SOFTWARE_RESET:
114 case WM8961_WRITE_SEQUENCER_7:
115 case WM8961_DC_SERVO_1:
123 static bool wm8961_readable(struct device *dev, unsigned int reg)
126 case WM8961_LEFT_INPUT_VOLUME:
127 case WM8961_RIGHT_INPUT_VOLUME:
128 case WM8961_LOUT1_VOLUME:
129 case WM8961_ROUT1_VOLUME:
130 case WM8961_CLOCKING1:
131 case WM8961_ADC_DAC_CONTROL_1:
132 case WM8961_ADC_DAC_CONTROL_2:
133 case WM8961_AUDIO_INTERFACE_0:
134 case WM8961_CLOCKING2:
135 case WM8961_AUDIO_INTERFACE_1:
136 case WM8961_LEFT_DAC_VOLUME:
137 case WM8961_RIGHT_DAC_VOLUME:
138 case WM8961_AUDIO_INTERFACE_2:
139 case WM8961_SOFTWARE_RESET:
143 case WM8961_NOISE_GATE:
144 case WM8961_LEFT_ADC_VOLUME:
145 case WM8961_RIGHT_ADC_VOLUME:
146 case WM8961_ADDITIONAL_CONTROL_1:
147 case WM8961_ADDITIONAL_CONTROL_2:
148 case WM8961_PWR_MGMT_1:
149 case WM8961_PWR_MGMT_2:
150 case WM8961_ADDITIONAL_CONTROL_3:
151 case WM8961_ANTI_POP:
152 case WM8961_CLOCKING_3:
153 case WM8961_ADCL_SIGNAL_PATH:
154 case WM8961_ADCR_SIGNAL_PATH:
155 case WM8961_LOUT2_VOLUME:
156 case WM8961_ROUT2_VOLUME:
157 case WM8961_PWR_MGMT_3:
158 case WM8961_ADDITIONAL_CONTROL_4:
159 case WM8961_CLASS_D_CONTROL_1:
160 case WM8961_CLASS_D_CONTROL_2:
161 case WM8961_CLOCKING_4:
162 case WM8961_DSP_SIDETONE_0:
163 case WM8961_DSP_SIDETONE_1:
164 case WM8961_DC_SERVO_0:
165 case WM8961_DC_SERVO_1:
166 case WM8961_DC_SERVO_3:
167 case WM8961_DC_SERVO_5:
168 case WM8961_ANALOGUE_PGA_BIAS:
169 case WM8961_ANALOGUE_HP_0:
170 case WM8961_ANALOGUE_HP_2:
171 case WM8961_CHARGE_PUMP_1:
172 case WM8961_CHARGE_PUMP_B:
173 case WM8961_WRITE_SEQUENCER_1:
174 case WM8961_WRITE_SEQUENCER_2:
175 case WM8961_WRITE_SEQUENCER_3:
176 case WM8961_WRITE_SEQUENCER_4:
177 case WM8961_WRITE_SEQUENCER_5:
178 case WM8961_WRITE_SEQUENCER_6:
179 case WM8961_WRITE_SEQUENCER_7:
180 case WM8961_GENERAL_TEST_1:
188 * The headphone output supports special anti-pop sequences giving
189 * silent power up and power down.
191 static int wm8961_hp_event(struct snd_soc_dapm_widget *w,
192 struct snd_kcontrol *kcontrol, int event)
194 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
195 u16 hp_reg = snd_soc_component_read(component, WM8961_ANALOGUE_HP_0);
196 u16 cp_reg = snd_soc_component_read(component, WM8961_CHARGE_PUMP_1);
197 u16 pwr_reg = snd_soc_component_read(component, WM8961_PWR_MGMT_2);
198 u16 dcs_reg = snd_soc_component_read(component, WM8961_DC_SERVO_1);
201 if (event & SND_SOC_DAPM_POST_PMU) {
202 /* Make sure the output is shorted */
203 hp_reg &= ~(WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT);
204 snd_soc_component_write(component, WM8961_ANALOGUE_HP_0, hp_reg);
206 /* Enable the charge pump */
207 cp_reg |= WM8961_CP_ENA;
208 snd_soc_component_write(component, WM8961_CHARGE_PUMP_1, cp_reg);
212 pwr_reg |= WM8961_LOUT1_PGA | WM8961_ROUT1_PGA;
213 snd_soc_component_write(component, WM8961_PWR_MGMT_2, pwr_reg);
215 /* Enable the amplifier */
216 hp_reg |= WM8961_HPR_ENA | WM8961_HPL_ENA;
217 snd_soc_component_write(component, WM8961_ANALOGUE_HP_0, hp_reg);
219 /* Second stage enable */
220 hp_reg |= WM8961_HPR_ENA_DLY | WM8961_HPL_ENA_DLY;
221 snd_soc_component_write(component, WM8961_ANALOGUE_HP_0, hp_reg);
223 /* Enable the DC servo & trigger startup */
225 WM8961_DCS_ENA_CHAN_HPR | WM8961_DCS_TRIG_STARTUP_HPR |
226 WM8961_DCS_ENA_CHAN_HPL | WM8961_DCS_TRIG_STARTUP_HPL;
227 dev_dbg(component->dev, "Enabling DC servo\n");
229 snd_soc_component_write(component, WM8961_DC_SERVO_1, dcs_reg);
232 dcs_reg = snd_soc_component_read(component, WM8961_DC_SERVO_1);
233 } while (--timeout &&
234 dcs_reg & (WM8961_DCS_TRIG_STARTUP_HPR |
235 WM8961_DCS_TRIG_STARTUP_HPL));
236 if (dcs_reg & (WM8961_DCS_TRIG_STARTUP_HPR |
237 WM8961_DCS_TRIG_STARTUP_HPL))
238 dev_err(component->dev, "DC servo timed out\n");
240 dev_dbg(component->dev, "DC servo startup complete\n");
242 /* Enable the output stage */
243 hp_reg |= WM8961_HPR_ENA_OUTP | WM8961_HPL_ENA_OUTP;
244 snd_soc_component_write(component, WM8961_ANALOGUE_HP_0, hp_reg);
246 /* Remove the short on the output stage */
247 hp_reg |= WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT;
248 snd_soc_component_write(component, WM8961_ANALOGUE_HP_0, hp_reg);
251 if (event & SND_SOC_DAPM_PRE_PMD) {
252 /* Short the output */
253 hp_reg &= ~(WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT);
254 snd_soc_component_write(component, WM8961_ANALOGUE_HP_0, hp_reg);
256 /* Disable the output stage */
257 hp_reg &= ~(WM8961_HPR_ENA_OUTP | WM8961_HPL_ENA_OUTP);
258 snd_soc_component_write(component, WM8961_ANALOGUE_HP_0, hp_reg);
260 /* Disable DC offset cancellation */
261 dcs_reg &= ~(WM8961_DCS_ENA_CHAN_HPR |
262 WM8961_DCS_ENA_CHAN_HPL);
263 snd_soc_component_write(component, WM8961_DC_SERVO_1, dcs_reg);
266 hp_reg &= ~(WM8961_HPR_ENA_DLY | WM8961_HPR_ENA |
267 WM8961_HPL_ENA_DLY | WM8961_HPL_ENA);
268 snd_soc_component_write(component, WM8961_ANALOGUE_HP_0, hp_reg);
270 /* Disable the PGA */
271 pwr_reg &= ~(WM8961_LOUT1_PGA | WM8961_ROUT1_PGA);
272 snd_soc_component_write(component, WM8961_PWR_MGMT_2, pwr_reg);
274 /* Disable the charge pump */
275 dev_dbg(component->dev, "Disabling charge pump\n");
276 snd_soc_component_write(component, WM8961_CHARGE_PUMP_1,
277 cp_reg & ~WM8961_CP_ENA);
283 static int wm8961_spk_event(struct snd_soc_dapm_widget *w,
284 struct snd_kcontrol *kcontrol, int event)
286 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
287 u16 pwr_reg = snd_soc_component_read(component, WM8961_PWR_MGMT_2);
288 u16 spk_reg = snd_soc_component_read(component, WM8961_CLASS_D_CONTROL_1);
290 if (event & SND_SOC_DAPM_POST_PMU) {
292 pwr_reg |= WM8961_SPKL_PGA | WM8961_SPKR_PGA;
293 snd_soc_component_write(component, WM8961_PWR_MGMT_2, pwr_reg);
295 /* Enable the amplifier */
296 spk_reg |= WM8961_SPKL_ENA | WM8961_SPKR_ENA;
297 snd_soc_component_write(component, WM8961_CLASS_D_CONTROL_1, spk_reg);
300 if (event & SND_SOC_DAPM_PRE_PMD) {
301 /* Disable the amplifier */
302 spk_reg &= ~(WM8961_SPKL_ENA | WM8961_SPKR_ENA);
303 snd_soc_component_write(component, WM8961_CLASS_D_CONTROL_1, spk_reg);
305 /* Disable the PGA */
306 pwr_reg &= ~(WM8961_SPKL_PGA | WM8961_SPKR_PGA);
307 snd_soc_component_write(component, WM8961_PWR_MGMT_2, pwr_reg);
313 static const char *adc_hpf_text[] = {
314 "Hi-fi", "Voice 1", "Voice 2", "Voice 3",
317 static SOC_ENUM_SINGLE_DECL(adc_hpf,
318 WM8961_ADC_DAC_CONTROL_2, 7, adc_hpf_text);
320 static const char *dac_deemph_text[] = {
321 "None", "32kHz", "44.1kHz", "48kHz",
324 static SOC_ENUM_SINGLE_DECL(dac_deemph,
325 WM8961_ADC_DAC_CONTROL_1, 1, dac_deemph_text);
327 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
328 static const DECLARE_TLV_DB_SCALE(hp_sec_tlv, -700, 100, 0);
329 static const DECLARE_TLV_DB_SCALE(adc_tlv, -7200, 75, 1);
330 static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
331 static const DECLARE_TLV_DB_RANGE(boost_tlv,
332 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
333 1, 1, TLV_DB_SCALE_ITEM(13, 0, 0),
334 2, 2, TLV_DB_SCALE_ITEM(20, 0, 0),
335 3, 3, TLV_DB_SCALE_ITEM(29, 0, 0)
337 static const DECLARE_TLV_DB_SCALE(pga_tlv, -2325, 75, 0);
339 static const struct snd_kcontrol_new wm8961_snd_controls[] = {
340 SOC_DOUBLE_R_TLV("Headphone Volume", WM8961_LOUT1_VOLUME, WM8961_ROUT1_VOLUME,
342 SOC_DOUBLE_TLV("Headphone Secondary Volume", WM8961_ANALOGUE_HP_2,
343 6, 3, 7, 0, hp_sec_tlv),
344 SOC_DOUBLE_R("Headphone ZC Switch", WM8961_LOUT1_VOLUME, WM8961_ROUT1_VOLUME,
347 SOC_DOUBLE_R_TLV("Speaker Volume", WM8961_LOUT2_VOLUME, WM8961_ROUT2_VOLUME,
349 SOC_DOUBLE_R("Speaker ZC Switch", WM8961_LOUT2_VOLUME, WM8961_ROUT2_VOLUME,
351 SOC_SINGLE("Speaker AC Gain", WM8961_CLASS_D_CONTROL_2, 0, 7, 0),
353 SOC_SINGLE("DAC x128 OSR Switch", WM8961_ADC_DAC_CONTROL_2, 0, 1, 0),
354 SOC_ENUM("DAC Deemphasis", dac_deemph),
355 SOC_SINGLE("DAC Soft Mute Switch", WM8961_ADC_DAC_CONTROL_2, 3, 1, 0),
357 SOC_DOUBLE_R_TLV("Sidetone Volume", WM8961_DSP_SIDETONE_0,
358 WM8961_DSP_SIDETONE_1, 4, 12, 0, sidetone_tlv),
360 SOC_SINGLE("ADC High Pass Filter Switch", WM8961_ADC_DAC_CONTROL_1, 0, 1, 0),
361 SOC_ENUM("ADC High Pass Filter Mode", adc_hpf),
363 SOC_DOUBLE_R_TLV("Capture Volume",
364 WM8961_LEFT_ADC_VOLUME, WM8961_RIGHT_ADC_VOLUME,
366 SOC_DOUBLE_R_TLV("Capture Boost Volume",
367 WM8961_ADCL_SIGNAL_PATH, WM8961_ADCR_SIGNAL_PATH,
369 SOC_DOUBLE_R_TLV("Capture PGA Volume",
370 WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
372 SOC_DOUBLE_R("Capture PGA ZC Switch",
373 WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
375 SOC_DOUBLE_R("Capture PGA Switch",
376 WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
380 static const char *sidetone_text[] = {
381 "None", "Left", "Right"
384 static SOC_ENUM_SINGLE_DECL(dacl_sidetone,
385 WM8961_DSP_SIDETONE_0, 2, sidetone_text);
387 static SOC_ENUM_SINGLE_DECL(dacr_sidetone,
388 WM8961_DSP_SIDETONE_1, 2, sidetone_text);
390 static const struct snd_kcontrol_new dacl_mux =
391 SOC_DAPM_ENUM("DACL Sidetone", dacl_sidetone);
393 static const struct snd_kcontrol_new dacr_mux =
394 SOC_DAPM_ENUM("DACR Sidetone", dacr_sidetone);
396 static const struct snd_soc_dapm_widget wm8961_dapm_widgets[] = {
397 SND_SOC_DAPM_INPUT("LINPUT"),
398 SND_SOC_DAPM_INPUT("RINPUT"),
400 SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8961_CLOCKING2, 4, 0, NULL, 0),
402 SND_SOC_DAPM_PGA("Left Input", WM8961_PWR_MGMT_1, 5, 0, NULL, 0),
403 SND_SOC_DAPM_PGA("Right Input", WM8961_PWR_MGMT_1, 4, 0, NULL, 0),
405 SND_SOC_DAPM_ADC("ADCL", "HiFi Capture", WM8961_PWR_MGMT_1, 3, 0),
406 SND_SOC_DAPM_ADC("ADCR", "HiFi Capture", WM8961_PWR_MGMT_1, 2, 0),
408 SND_SOC_DAPM_SUPPLY("MICBIAS", WM8961_PWR_MGMT_1, 1, 0, NULL, 0),
410 SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &dacl_mux),
411 SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &dacr_mux),
413 SND_SOC_DAPM_DAC("DACL", "HiFi Playback", WM8961_PWR_MGMT_2, 8, 0),
414 SND_SOC_DAPM_DAC("DACR", "HiFi Playback", WM8961_PWR_MGMT_2, 7, 0),
416 /* Handle as a mono path for DCS */
417 SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM,
418 4, 0, NULL, 0, wm8961_hp_event,
419 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
420 SND_SOC_DAPM_PGA_E("Speaker Output", SND_SOC_NOPM,
421 4, 0, NULL, 0, wm8961_spk_event,
422 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
424 SND_SOC_DAPM_OUTPUT("HP_L"),
425 SND_SOC_DAPM_OUTPUT("HP_R"),
426 SND_SOC_DAPM_OUTPUT("SPK_LN"),
427 SND_SOC_DAPM_OUTPUT("SPK_LP"),
428 SND_SOC_DAPM_OUTPUT("SPK_RN"),
429 SND_SOC_DAPM_OUTPUT("SPK_RP"),
433 static const struct snd_soc_dapm_route audio_paths[] = {
434 { "DACL", NULL, "CLK_DSP" },
435 { "DACL", NULL, "DACL Sidetone" },
436 { "DACR", NULL, "CLK_DSP" },
437 { "DACR", NULL, "DACR Sidetone" },
439 { "DACL Sidetone", "Left", "ADCL" },
440 { "DACL Sidetone", "Right", "ADCR" },
442 { "DACR Sidetone", "Left", "ADCL" },
443 { "DACR Sidetone", "Right", "ADCR" },
445 { "HP_L", NULL, "Headphone Output" },
446 { "HP_R", NULL, "Headphone Output" },
447 { "Headphone Output", NULL, "DACL" },
448 { "Headphone Output", NULL, "DACR" },
450 { "SPK_LN", NULL, "Speaker Output" },
451 { "SPK_LP", NULL, "Speaker Output" },
452 { "SPK_RN", NULL, "Speaker Output" },
453 { "SPK_RP", NULL, "Speaker Output" },
455 { "Speaker Output", NULL, "DACL" },
456 { "Speaker Output", NULL, "DACR" },
458 { "ADCL", NULL, "Left Input" },
459 { "ADCL", NULL, "CLK_DSP" },
460 { "ADCR", NULL, "Right Input" },
461 { "ADCR", NULL, "CLK_DSP" },
463 { "Left Input", NULL, "LINPUT" },
464 { "Right Input", NULL, "RINPUT" },
468 /* Values for CLK_SYS_RATE */
472 } wm8961_clk_sys_ratio[] = {
485 /* Values for SAMPLE_RATE */
501 static int wm8961_hw_params(struct snd_pcm_substream *substream,
502 struct snd_pcm_hw_params *params,
503 struct snd_soc_dai *dai)
505 struct snd_soc_component *component = dai->component;
506 struct wm8961_priv *wm8961 = snd_soc_component_get_drvdata(component);
507 int i, best, target, fs;
510 fs = params_rate(params);
512 if (!wm8961->sysclk) {
513 dev_err(component->dev, "MCLK has not been specified\n");
517 /* Find the closest sample rate for the filters */
519 for (i = 0; i < ARRAY_SIZE(wm8961_srate); i++) {
520 if (abs(wm8961_srate[i].rate - fs) <
521 abs(wm8961_srate[best].rate - fs))
524 reg = snd_soc_component_read(component, WM8961_ADDITIONAL_CONTROL_3);
525 reg &= ~WM8961_SAMPLE_RATE_MASK;
526 reg |= wm8961_srate[best].val;
527 snd_soc_component_write(component, WM8961_ADDITIONAL_CONTROL_3, reg);
528 dev_dbg(component->dev, "Selected SRATE %dHz for %dHz\n",
529 wm8961_srate[best].rate, fs);
531 /* Select a CLK_SYS/fs ratio equal to or higher than required */
532 target = wm8961->sysclk / fs;
534 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && target < 64) {
535 dev_err(component->dev,
536 "SYSCLK must be at least 64*fs for DAC\n");
539 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && target < 256) {
540 dev_err(component->dev,
541 "SYSCLK must be at least 256*fs for ADC\n");
545 for (i = 0; i < ARRAY_SIZE(wm8961_clk_sys_ratio); i++) {
546 if (wm8961_clk_sys_ratio[i].ratio >= target)
549 if (i == ARRAY_SIZE(wm8961_clk_sys_ratio)) {
550 dev_err(component->dev, "Unable to generate CLK_SYS_RATE\n");
553 dev_dbg(component->dev, "Selected CLK_SYS_RATE of %d for %d/%d=%d\n",
554 wm8961_clk_sys_ratio[i].ratio, wm8961->sysclk, fs,
555 wm8961->sysclk / fs);
557 reg = snd_soc_component_read(component, WM8961_CLOCKING_4);
558 reg &= ~WM8961_CLK_SYS_RATE_MASK;
559 reg |= wm8961_clk_sys_ratio[i].val << WM8961_CLK_SYS_RATE_SHIFT;
560 snd_soc_component_write(component, WM8961_CLOCKING_4, reg);
562 reg = snd_soc_component_read(component, WM8961_AUDIO_INTERFACE_0);
563 reg &= ~WM8961_WL_MASK;
564 switch (params_width(params)) {
568 reg |= 1 << WM8961_WL_SHIFT;
571 reg |= 2 << WM8961_WL_SHIFT;
574 reg |= 3 << WM8961_WL_SHIFT;
579 snd_soc_component_write(component, WM8961_AUDIO_INTERFACE_0, reg);
581 /* Sloping stop-band filter is recommended for <= 24kHz */
582 reg = snd_soc_component_read(component, WM8961_ADC_DAC_CONTROL_2);
584 reg |= WM8961_DACSLOPE;
586 reg &= ~WM8961_DACSLOPE;
587 snd_soc_component_write(component, WM8961_ADC_DAC_CONTROL_2, reg);
592 static int wm8961_set_sysclk(struct snd_soc_dai *dai, int clk_id,
596 struct snd_soc_component *component = dai->component;
597 struct wm8961_priv *wm8961 = snd_soc_component_get_drvdata(component);
598 u16 reg = snd_soc_component_read(component, WM8961_CLOCKING1);
600 if (freq > 33000000) {
601 dev_err(component->dev, "MCLK must be <33MHz\n");
605 if (freq > 16500000) {
606 dev_dbg(component->dev, "Using MCLK/2 for %dHz MCLK\n", freq);
607 reg |= WM8961_MCLKDIV;
610 dev_dbg(component->dev, "Using MCLK/1 for %dHz MCLK\n", freq);
611 reg &= ~WM8961_MCLKDIV;
614 snd_soc_component_write(component, WM8961_CLOCKING1, reg);
616 wm8961->sysclk = freq;
621 static int wm8961_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
623 struct snd_soc_component *component = dai->component;
624 u16 aif = snd_soc_component_read(component, WM8961_AUDIO_INTERFACE_0);
626 aif &= ~(WM8961_BCLKINV | WM8961_LRP |
627 WM8961_MS | WM8961_FORMAT_MASK);
629 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
630 case SND_SOC_DAIFMT_CBM_CFM:
633 case SND_SOC_DAIFMT_CBS_CFS:
639 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
640 case SND_SOC_DAIFMT_RIGHT_J:
643 case SND_SOC_DAIFMT_LEFT_J:
647 case SND_SOC_DAIFMT_I2S:
651 case SND_SOC_DAIFMT_DSP_B:
654 case SND_SOC_DAIFMT_DSP_A:
656 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
657 case SND_SOC_DAIFMT_NB_NF:
658 case SND_SOC_DAIFMT_IB_NF:
669 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
670 case SND_SOC_DAIFMT_NB_NF:
672 case SND_SOC_DAIFMT_NB_IF:
675 case SND_SOC_DAIFMT_IB_NF:
676 aif |= WM8961_BCLKINV;
678 case SND_SOC_DAIFMT_IB_IF:
679 aif |= WM8961_BCLKINV | WM8961_LRP;
685 return snd_soc_component_write(component, WM8961_AUDIO_INTERFACE_0, aif);
688 static int wm8961_set_tristate(struct snd_soc_dai *dai, int tristate)
690 struct snd_soc_component *component = dai->component;
691 u16 reg = snd_soc_component_read(component, WM8961_ADDITIONAL_CONTROL_2);
698 return snd_soc_component_write(component, WM8961_ADDITIONAL_CONTROL_2, reg);
701 static int wm8961_mute(struct snd_soc_dai *dai, int mute, int direction)
703 struct snd_soc_component *component = dai->component;
704 u16 reg = snd_soc_component_read(component, WM8961_ADC_DAC_CONTROL_1);
709 reg &= ~WM8961_DACMU;
713 return snd_soc_component_write(component, WM8961_ADC_DAC_CONTROL_1, reg);
716 static int wm8961_set_clkdiv(struct snd_soc_dai *dai, int div_id, int div)
718 struct snd_soc_component *component = dai->component;
723 reg = snd_soc_component_read(component, WM8961_CLOCKING2);
724 reg &= ~WM8961_BCLKDIV_MASK;
726 snd_soc_component_write(component, WM8961_CLOCKING2, reg);
730 reg = snd_soc_component_read(component, WM8961_AUDIO_INTERFACE_2);
731 reg &= ~WM8961_LRCLK_RATE_MASK;
733 snd_soc_component_write(component, WM8961_AUDIO_INTERFACE_2, reg);
743 static int wm8961_set_bias_level(struct snd_soc_component *component,
744 enum snd_soc_bias_level level)
748 /* This is all slightly unusual since we have no bypass paths
749 * and the output amplifier structure means we can just slam
750 * the biases straight up rather than having to ramp them
754 case SND_SOC_BIAS_ON:
757 case SND_SOC_BIAS_PREPARE:
758 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_STANDBY) {
759 /* Enable bias generation */
760 reg = snd_soc_component_read(component, WM8961_ANTI_POP);
761 reg |= WM8961_BUFIOEN | WM8961_BUFDCOPEN;
762 snd_soc_component_write(component, WM8961_ANTI_POP, reg);
764 /* VMID=2*50k, VREF */
765 reg = snd_soc_component_read(component, WM8961_PWR_MGMT_1);
766 reg &= ~WM8961_VMIDSEL_MASK;
767 reg |= (1 << WM8961_VMIDSEL_SHIFT) | WM8961_VREF;
768 snd_soc_component_write(component, WM8961_PWR_MGMT_1, reg);
772 case SND_SOC_BIAS_STANDBY:
773 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_PREPARE) {
775 reg = snd_soc_component_read(component, WM8961_PWR_MGMT_1);
777 snd_soc_component_write(component, WM8961_PWR_MGMT_1, reg);
779 /* Bias generation off */
780 reg = snd_soc_component_read(component, WM8961_ANTI_POP);
781 reg &= ~(WM8961_BUFIOEN | WM8961_BUFDCOPEN);
782 snd_soc_component_write(component, WM8961_ANTI_POP, reg);
785 reg = snd_soc_component_read(component, WM8961_PWR_MGMT_1);
786 reg &= ~WM8961_VMIDSEL_MASK;
787 snd_soc_component_write(component, WM8961_PWR_MGMT_1, reg);
791 case SND_SOC_BIAS_OFF:
799 #define WM8961_RATES SNDRV_PCM_RATE_8000_48000
801 #define WM8961_FORMATS \
802 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
803 SNDRV_PCM_FMTBIT_S24_LE)
805 static const struct snd_soc_dai_ops wm8961_dai_ops = {
806 .hw_params = wm8961_hw_params,
807 .set_sysclk = wm8961_set_sysclk,
808 .set_fmt = wm8961_set_fmt,
809 .mute_stream = wm8961_mute,
810 .set_tristate = wm8961_set_tristate,
811 .set_clkdiv = wm8961_set_clkdiv,
812 .no_capture_mute = 1,
815 static struct snd_soc_dai_driver wm8961_dai = {
816 .name = "wm8961-hifi",
818 .stream_name = "HiFi Playback",
821 .rates = WM8961_RATES,
822 .formats = WM8961_FORMATS,},
824 .stream_name = "HiFi Capture",
827 .rates = WM8961_RATES,
828 .formats = WM8961_FORMATS,},
829 .ops = &wm8961_dai_ops,
832 static int wm8961_probe(struct snd_soc_component *component)
837 reg = snd_soc_component_read(component, WM8961_CHARGE_PUMP_B);
838 reg |= WM8961_CP_DYN_PWR_MASK;
839 snd_soc_component_write(component, WM8961_CHARGE_PUMP_B, reg);
841 /* Latch volume update bits (right channel only, we always
842 * write both out) and default ZC on. */
843 reg = snd_soc_component_read(component, WM8961_ROUT1_VOLUME);
844 snd_soc_component_write(component, WM8961_ROUT1_VOLUME,
845 reg | WM8961_LO1ZC | WM8961_OUT1VU);
846 snd_soc_component_write(component, WM8961_LOUT1_VOLUME, reg | WM8961_LO1ZC);
847 reg = snd_soc_component_read(component, WM8961_ROUT2_VOLUME);
848 snd_soc_component_write(component, WM8961_ROUT2_VOLUME,
849 reg | WM8961_SPKRZC | WM8961_SPKVU);
850 snd_soc_component_write(component, WM8961_LOUT2_VOLUME, reg | WM8961_SPKLZC);
852 reg = snd_soc_component_read(component, WM8961_RIGHT_ADC_VOLUME);
853 snd_soc_component_write(component, WM8961_RIGHT_ADC_VOLUME, reg | WM8961_ADCVU);
854 reg = snd_soc_component_read(component, WM8961_RIGHT_INPUT_VOLUME);
855 snd_soc_component_write(component, WM8961_RIGHT_INPUT_VOLUME, reg | WM8961_IPVU);
857 /* Use soft mute by default */
858 reg = snd_soc_component_read(component, WM8961_ADC_DAC_CONTROL_2);
859 reg |= WM8961_DACSMM;
860 snd_soc_component_write(component, WM8961_ADC_DAC_CONTROL_2, reg);
862 /* Use automatic clocking mode by default; for now this is all
865 reg = snd_soc_component_read(component, WM8961_CLOCKING_3);
866 reg &= ~WM8961_MANUAL_MODE;
867 snd_soc_component_write(component, WM8961_CLOCKING_3, reg);
874 static int wm8961_resume(struct snd_soc_component *component)
876 snd_soc_component_cache_sync(component);
881 #define wm8961_resume NULL
884 static const struct snd_soc_component_driver soc_component_dev_wm8961 = {
885 .probe = wm8961_probe,
886 .resume = wm8961_resume,
887 .set_bias_level = wm8961_set_bias_level,
888 .controls = wm8961_snd_controls,
889 .num_controls = ARRAY_SIZE(wm8961_snd_controls),
890 .dapm_widgets = wm8961_dapm_widgets,
891 .num_dapm_widgets = ARRAY_SIZE(wm8961_dapm_widgets),
892 .dapm_routes = audio_paths,
893 .num_dapm_routes = ARRAY_SIZE(audio_paths),
894 .suspend_bias_off = 1,
896 .use_pmdown_time = 1,
900 static const struct regmap_config wm8961_regmap = {
903 .max_register = WM8961_MAX_REGISTER,
905 .reg_defaults = wm8961_reg_defaults,
906 .num_reg_defaults = ARRAY_SIZE(wm8961_reg_defaults),
907 .cache_type = REGCACHE_RBTREE,
909 .volatile_reg = wm8961_volatile,
910 .readable_reg = wm8961_readable,
913 static int wm8961_i2c_probe(struct i2c_client *i2c)
915 struct wm8961_priv *wm8961;
919 wm8961 = devm_kzalloc(&i2c->dev, sizeof(struct wm8961_priv),
924 wm8961->regmap = devm_regmap_init_i2c(i2c, &wm8961_regmap);
925 if (IS_ERR(wm8961->regmap))
926 return PTR_ERR(wm8961->regmap);
928 ret = regmap_read(wm8961->regmap, WM8961_SOFTWARE_RESET, &val);
930 dev_err(&i2c->dev, "Failed to read chip ID: %d\n", ret);
935 dev_err(&i2c->dev, "Device is not a WM8961: ID=0x%x\n", val);
939 /* This isn't volatile - readback doesn't correspond to write */
940 regcache_cache_bypass(wm8961->regmap, true);
941 ret = regmap_read(wm8961->regmap, WM8961_RIGHT_INPUT_VOLUME, &val);
942 regcache_cache_bypass(wm8961->regmap, false);
945 dev_err(&i2c->dev, "Failed to read chip revision: %d\n", ret);
949 dev_info(&i2c->dev, "WM8961 family %d revision %c\n",
950 (val & WM8961_DEVICE_ID_MASK) >> WM8961_DEVICE_ID_SHIFT,
951 ((val & WM8961_CHIP_REV_MASK) >> WM8961_CHIP_REV_SHIFT)
954 ret = regmap_write(wm8961->regmap, WM8961_SOFTWARE_RESET, 0x1801);
956 dev_err(&i2c->dev, "Failed to issue reset: %d\n", ret);
960 i2c_set_clientdata(i2c, wm8961);
962 ret = devm_snd_soc_register_component(&i2c->dev,
963 &soc_component_dev_wm8961, &wm8961_dai, 1);
968 static const struct i2c_device_id wm8961_i2c_id[] = {
972 MODULE_DEVICE_TABLE(i2c, wm8961_i2c_id);
974 static const struct of_device_id wm8961_of_match[] __maybe_unused = {
975 { .compatible = "wlf,wm8961", },
978 MODULE_DEVICE_TABLE(of, wm8961_of_match);
980 static struct i2c_driver wm8961_i2c_driver = {
983 .of_match_table = of_match_ptr(wm8961_of_match),
985 .probe = wm8961_i2c_probe,
986 .id_table = wm8961_i2c_id,
989 module_i2c_driver(wm8961_i2c_driver);
991 MODULE_DESCRIPTION("ASoC WM8961 driver");
992 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
993 MODULE_LICENSE("GPL");