1 // SPDX-License-Identifier: GPL-2.0-only
3 * wm8940.c -- WM8940 ALSA Soc Audio driver
5 * Author: Jonathan Cameron <jic23@cam.ac.uk>
8 * Copyright 2006 Wolfson Microelectronics PLC.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
11 * Not currently handled:
12 * Notch filter control
13 * AUXMode (inverting vs mixer)
14 * No means to obtain current gain if alc enabled.
16 * Fast VMID discharge for power down
18 * DLR and ALR Swaps not enabled
19 * Digital Sidetone not supported
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
27 #include <linux/i2c.h>
28 #include <linux/regmap.h>
29 #include <linux/slab.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/soc.h>
34 #include <sound/initval.h>
35 #include <sound/tlv.h>
43 struct regmap
*regmap
;
46 static bool wm8940_volatile_register(struct device
*dev
, unsigned int reg
)
49 case WM8940_SOFTRESET
:
56 static bool wm8940_readable_register(struct device
*dev
, unsigned int reg
)
59 case WM8940_SOFTRESET
:
64 case WM8940_COMPANDINGCTL
:
86 case WM8940_NOISEGATE
:
95 case WM8940_OUTPUTCTL
:
105 static const struct reg_default wm8940_reg_defaults
[] = {
106 { 0x1, 0x0000 }, /* Power 1 */
107 { 0x2, 0x0000 }, /* Power 2 */
108 { 0x3, 0x0000 }, /* Power 3 */
109 { 0x4, 0x0010 }, /* Interface Control */
110 { 0x5, 0x0000 }, /* Companding Control */
111 { 0x6, 0x0140 }, /* Clock Control */
112 { 0x7, 0x0000 }, /* Additional Controls */
113 { 0x8, 0x0000 }, /* GPIO Control */
114 { 0x9, 0x0002 }, /* Auto Increment Control */
115 { 0xa, 0x0000 }, /* DAC Control */
116 { 0xb, 0x00FF }, /* DAC Volume */
118 { 0xe, 0x0100 }, /* ADC Control */
119 { 0xf, 0x00FF }, /* ADC Volume */
120 { 0x10, 0x0000 }, /* Notch Filter 1 Control 1 */
121 { 0x11, 0x0000 }, /* Notch Filter 1 Control 2 */
122 { 0x12, 0x0000 }, /* Notch Filter 2 Control 1 */
123 { 0x13, 0x0000 }, /* Notch Filter 2 Control 2 */
124 { 0x14, 0x0000 }, /* Notch Filter 3 Control 1 */
125 { 0x15, 0x0000 }, /* Notch Filter 3 Control 2 */
126 { 0x16, 0x0000 }, /* Notch Filter 4 Control 1 */
127 { 0x17, 0x0000 }, /* Notch Filter 4 Control 2 */
128 { 0x18, 0x0032 }, /* DAC Limit Control 1 */
129 { 0x19, 0x0000 }, /* DAC Limit Control 2 */
131 { 0x20, 0x0038 }, /* ALC Control 1 */
132 { 0x21, 0x000B }, /* ALC Control 2 */
133 { 0x22, 0x0032 }, /* ALC Control 3 */
134 { 0x23, 0x0000 }, /* Noise Gate */
135 { 0x24, 0x0041 }, /* PLLN */
136 { 0x25, 0x000C }, /* PLLK1 */
137 { 0x26, 0x0093 }, /* PLLK2 */
138 { 0x27, 0x00E9 }, /* PLLK3 */
140 { 0x2a, 0x0030 }, /* ALC Control 4 */
142 { 0x2c, 0x0002 }, /* Input Control */
143 { 0x2d, 0x0050 }, /* PGA Gain */
145 { 0x2f, 0x0002 }, /* ADC Boost Control */
147 { 0x31, 0x0002 }, /* Output Control */
148 { 0x32, 0x0000 }, /* Speaker Mixer Control */
150 { 0x36, 0x0079 }, /* Speaker Volume */
152 { 0x38, 0x0000 }, /* Mono Mixer Control */
155 static const char *wm8940_companding
[] = { "Off", "NC", "u-law", "A-law" };
156 static SOC_ENUM_SINGLE_DECL(wm8940_adc_companding_enum
,
157 WM8940_COMPANDINGCTL
, 1, wm8940_companding
);
158 static SOC_ENUM_SINGLE_DECL(wm8940_dac_companding_enum
,
159 WM8940_COMPANDINGCTL
, 3, wm8940_companding
);
161 static const char *wm8940_alc_mode_text
[] = {"ALC", "Limiter"};
162 static SOC_ENUM_SINGLE_DECL(wm8940_alc_mode_enum
,
163 WM8940_ALC3
, 8, wm8940_alc_mode_text
);
165 static const char *wm8940_mic_bias_level_text
[] = {"0.9", "0.65"};
166 static SOC_ENUM_SINGLE_DECL(wm8940_mic_bias_level_enum
,
167 WM8940_INPUTCTL
, 8, wm8940_mic_bias_level_text
);
169 static const char *wm8940_filter_mode_text
[] = {"Audio", "Application"};
170 static SOC_ENUM_SINGLE_DECL(wm8940_filter_mode_enum
,
171 WM8940_ADC
, 7, wm8940_filter_mode_text
);
173 static DECLARE_TLV_DB_SCALE(wm8940_spk_vol_tlv
, -5700, 100, 1);
174 static DECLARE_TLV_DB_SCALE(wm8940_att_tlv
, -1000, 1000, 0);
175 static DECLARE_TLV_DB_SCALE(wm8940_pga_vol_tlv
, -1200, 75, 0);
176 static DECLARE_TLV_DB_SCALE(wm8940_alc_min_tlv
, -1200, 600, 0);
177 static DECLARE_TLV_DB_SCALE(wm8940_alc_max_tlv
, 675, 600, 0);
178 static DECLARE_TLV_DB_SCALE(wm8940_alc_tar_tlv
, -2250, 50, 0);
179 static DECLARE_TLV_DB_SCALE(wm8940_lim_boost_tlv
, 0, 100, 0);
180 static DECLARE_TLV_DB_SCALE(wm8940_lim_thresh_tlv
, -600, 100, 0);
181 static DECLARE_TLV_DB_SCALE(wm8940_adc_tlv
, -12750, 50, 1);
182 static DECLARE_TLV_DB_SCALE(wm8940_capture_boost_vol_tlv
, 0, 2000, 0);
184 static const struct snd_kcontrol_new wm8940_snd_controls
[] = {
185 SOC_SINGLE("Digital Loopback Switch", WM8940_COMPANDINGCTL
,
187 SOC_ENUM("DAC Companding", wm8940_dac_companding_enum
),
188 SOC_ENUM("ADC Companding", wm8940_adc_companding_enum
),
190 SOC_ENUM("ALC Mode", wm8940_alc_mode_enum
),
191 SOC_SINGLE("ALC Switch", WM8940_ALC1
, 8, 1, 0),
192 SOC_SINGLE_TLV("ALC Capture Max Gain", WM8940_ALC1
,
193 3, 7, 1, wm8940_alc_max_tlv
),
194 SOC_SINGLE_TLV("ALC Capture Min Gain", WM8940_ALC1
,
195 0, 7, 0, wm8940_alc_min_tlv
),
196 SOC_SINGLE_TLV("ALC Capture Target", WM8940_ALC2
,
197 0, 14, 0, wm8940_alc_tar_tlv
),
198 SOC_SINGLE("ALC Capture Hold", WM8940_ALC2
, 4, 10, 0),
199 SOC_SINGLE("ALC Capture Decay", WM8940_ALC3
, 4, 10, 0),
200 SOC_SINGLE("ALC Capture Attach", WM8940_ALC3
, 0, 10, 0),
201 SOC_SINGLE("ALC ZC Switch", WM8940_ALC4
, 1, 1, 0),
202 SOC_SINGLE("ALC Capture Noise Gate Switch", WM8940_NOISEGATE
,
204 SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8940_NOISEGATE
,
207 SOC_SINGLE("DAC Playback Limiter Switch", WM8940_DACLIM1
, 8, 1, 0),
208 SOC_SINGLE("DAC Playback Limiter Attack", WM8940_DACLIM1
, 0, 9, 0),
209 SOC_SINGLE("DAC Playback Limiter Decay", WM8940_DACLIM1
, 4, 11, 0),
210 SOC_SINGLE_TLV("DAC Playback Limiter Threshold", WM8940_DACLIM2
,
211 4, 9, 1, wm8940_lim_thresh_tlv
),
212 SOC_SINGLE_TLV("DAC Playback Limiter Boost", WM8940_DACLIM2
,
213 0, 12, 0, wm8940_lim_boost_tlv
),
215 SOC_SINGLE("Capture PGA ZC Switch", WM8940_PGAGAIN
, 7, 1, 0),
216 SOC_SINGLE_TLV("Capture PGA Volume", WM8940_PGAGAIN
,
217 0, 63, 0, wm8940_pga_vol_tlv
),
218 SOC_SINGLE_TLV("Digital Playback Volume", WM8940_DACVOL
,
219 0, 255, 0, wm8940_adc_tlv
),
220 SOC_SINGLE_TLV("Digital Capture Volume", WM8940_ADCVOL
,
221 0, 255, 0, wm8940_adc_tlv
),
222 SOC_ENUM("Mic Bias Level", wm8940_mic_bias_level_enum
),
223 SOC_SINGLE_TLV("Capture Boost Volue", WM8940_ADCBOOST
,
224 8, 1, 0, wm8940_capture_boost_vol_tlv
),
225 SOC_SINGLE_TLV("Speaker Playback Volume", WM8940_SPKVOL
,
226 0, 63, 0, wm8940_spk_vol_tlv
),
227 SOC_SINGLE("Speaker Playback Switch", WM8940_SPKVOL
, 6, 1, 1),
229 SOC_SINGLE_TLV("Speaker Mixer Line Bypass Volume", WM8940_SPKVOL
,
230 8, 1, 1, wm8940_att_tlv
),
231 SOC_SINGLE("Speaker Playback ZC Switch", WM8940_SPKVOL
, 7, 1, 0),
233 SOC_SINGLE("Mono Out Switch", WM8940_MONOMIX
, 6, 1, 1),
234 SOC_SINGLE_TLV("Mono Mixer Line Bypass Volume", WM8940_MONOMIX
,
235 7, 1, 1, wm8940_att_tlv
),
237 SOC_SINGLE("High Pass Filter Switch", WM8940_ADC
, 8, 1, 0),
238 SOC_ENUM("High Pass Filter Mode", wm8940_filter_mode_enum
),
239 SOC_SINGLE("High Pass Filter Cut Off", WM8940_ADC
, 4, 7, 0),
240 SOC_SINGLE("ADC Inversion Switch", WM8940_ADC
, 0, 1, 0),
241 SOC_SINGLE("DAC Inversion Switch", WM8940_DAC
, 0, 1, 0),
242 SOC_SINGLE("DAC Auto Mute Switch", WM8940_DAC
, 2, 1, 0),
243 SOC_SINGLE("ZC Timeout Clock Switch", WM8940_ADDCNTRL
, 0, 1, 0),
246 static const struct snd_kcontrol_new wm8940_speaker_mixer_controls
[] = {
247 SOC_DAPM_SINGLE("Line Bypass Switch", WM8940_SPKMIX
, 1, 1, 0),
248 SOC_DAPM_SINGLE("Aux Playback Switch", WM8940_SPKMIX
, 5, 1, 0),
249 SOC_DAPM_SINGLE("PCM Playback Switch", WM8940_SPKMIX
, 0, 1, 0),
252 static const struct snd_kcontrol_new wm8940_mono_mixer_controls
[] = {
253 SOC_DAPM_SINGLE("Line Bypass Switch", WM8940_MONOMIX
, 1, 1, 0),
254 SOC_DAPM_SINGLE("Aux Playback Switch", WM8940_MONOMIX
, 2, 1, 0),
255 SOC_DAPM_SINGLE("PCM Playback Switch", WM8940_MONOMIX
, 0, 1, 0),
258 static DECLARE_TLV_DB_SCALE(wm8940_boost_vol_tlv
, -1500, 300, 1);
259 static const struct snd_kcontrol_new wm8940_input_boost_controls
[] = {
260 SOC_DAPM_SINGLE("Mic PGA Switch", WM8940_PGAGAIN
, 6, 1, 1),
261 SOC_DAPM_SINGLE_TLV("Aux Volume", WM8940_ADCBOOST
,
262 0, 7, 0, wm8940_boost_vol_tlv
),
263 SOC_DAPM_SINGLE_TLV("Mic Volume", WM8940_ADCBOOST
,
264 4, 7, 0, wm8940_boost_vol_tlv
),
267 static const struct snd_kcontrol_new wm8940_micpga_controls
[] = {
268 SOC_DAPM_SINGLE("AUX Switch", WM8940_INPUTCTL
, 2, 1, 0),
269 SOC_DAPM_SINGLE("MICP Switch", WM8940_INPUTCTL
, 0, 1, 0),
270 SOC_DAPM_SINGLE("MICN Switch", WM8940_INPUTCTL
, 1, 1, 0),
273 static const struct snd_soc_dapm_widget wm8940_dapm_widgets
[] = {
274 SND_SOC_DAPM_MIXER("Speaker Mixer", WM8940_POWER3
, 2, 0,
275 &wm8940_speaker_mixer_controls
[0],
276 ARRAY_SIZE(wm8940_speaker_mixer_controls
)),
277 SND_SOC_DAPM_MIXER("Mono Mixer", WM8940_POWER3
, 3, 0,
278 &wm8940_mono_mixer_controls
[0],
279 ARRAY_SIZE(wm8940_mono_mixer_controls
)),
280 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8940_POWER3
, 0, 0),
282 SND_SOC_DAPM_PGA("SpkN Out", WM8940_POWER3
, 5, 0, NULL
, 0),
283 SND_SOC_DAPM_PGA("SpkP Out", WM8940_POWER3
, 6, 0, NULL
, 0),
284 SND_SOC_DAPM_PGA("Mono Out", WM8940_POWER3
, 7, 0, NULL
, 0),
285 SND_SOC_DAPM_OUTPUT("MONOOUT"),
286 SND_SOC_DAPM_OUTPUT("SPKOUTP"),
287 SND_SOC_DAPM_OUTPUT("SPKOUTN"),
289 SND_SOC_DAPM_PGA("Aux Input", WM8940_POWER1
, 6, 0, NULL
, 0),
290 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8940_POWER2
, 0, 0),
291 SND_SOC_DAPM_MIXER("Mic PGA", WM8940_POWER2
, 2, 0,
292 &wm8940_micpga_controls
[0],
293 ARRAY_SIZE(wm8940_micpga_controls
)),
294 SND_SOC_DAPM_MIXER("Boost Mixer", WM8940_POWER2
, 4, 0,
295 &wm8940_input_boost_controls
[0],
296 ARRAY_SIZE(wm8940_input_boost_controls
)),
297 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8940_POWER1
, 4, 0),
299 SND_SOC_DAPM_INPUT("MICN"),
300 SND_SOC_DAPM_INPUT("MICP"),
301 SND_SOC_DAPM_INPUT("AUX"),
304 static const struct snd_soc_dapm_route wm8940_dapm_routes
[] = {
305 /* Mono output mixer */
306 {"Mono Mixer", "PCM Playback Switch", "DAC"},
307 {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
308 {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
310 /* Speaker output mixer */
311 {"Speaker Mixer", "PCM Playback Switch", "DAC"},
312 {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
313 {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
316 {"Mono Out", NULL
, "Mono Mixer"},
317 {"MONOOUT", NULL
, "Mono Out"},
318 {"SpkN Out", NULL
, "Speaker Mixer"},
319 {"SpkP Out", NULL
, "Speaker Mixer"},
320 {"SPKOUTN", NULL
, "SpkN Out"},
321 {"SPKOUTP", NULL
, "SpkP Out"},
324 {"Mic PGA", "MICN Switch", "MICN"},
325 {"Mic PGA", "MICP Switch", "MICP"},
326 {"Mic PGA", "AUX Switch", "AUX"},
329 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
330 {"Boost Mixer", "Mic Volume", "MICP"},
331 {"Boost Mixer", "Aux Volume", "Aux Input"},
333 {"ADC", NULL
, "Boost Mixer"},
336 #define wm8940_reset(c) snd_soc_component_write(c, WM8940_SOFTRESET, 0);
338 static int wm8940_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
341 struct snd_soc_component
*component
= codec_dai
->component
;
342 u16 iface
= snd_soc_component_read(component
, WM8940_IFACE
) & 0xFE67;
343 u16 clk
= snd_soc_component_read(component
, WM8940_CLOCK
) & 0x1fe;
345 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
346 case SND_SOC_DAIFMT_CBM_CFM
:
349 case SND_SOC_DAIFMT_CBS_CFS
:
354 snd_soc_component_write(component
, WM8940_CLOCK
, clk
);
356 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
357 case SND_SOC_DAIFMT_I2S
:
360 case SND_SOC_DAIFMT_LEFT_J
:
363 case SND_SOC_DAIFMT_RIGHT_J
:
365 case SND_SOC_DAIFMT_DSP_A
:
368 case SND_SOC_DAIFMT_DSP_B
:
369 iface
|= (3 << 3) | (1 << 7);
373 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
374 case SND_SOC_DAIFMT_NB_NF
:
376 case SND_SOC_DAIFMT_NB_IF
:
379 case SND_SOC_DAIFMT_IB_NF
:
382 case SND_SOC_DAIFMT_IB_IF
:
383 iface
|= (1 << 8) | (1 << 7);
387 snd_soc_component_write(component
, WM8940_IFACE
, iface
);
392 static int wm8940_update_clocks(struct snd_soc_dai
*dai
);
393 static int wm8940_i2s_hw_params(struct snd_pcm_substream
*substream
,
394 struct snd_pcm_hw_params
*params
,
395 struct snd_soc_dai
*dai
)
397 struct snd_soc_component
*component
= dai
->component
;
398 struct wm8940_priv
*priv
= snd_soc_component_get_drvdata(component
);
399 u16 iface
= snd_soc_component_read(component
, WM8940_IFACE
) & 0xFD9F;
400 u16 addcntrl
= snd_soc_component_read(component
, WM8940_ADDCNTRL
) & 0xFFF1;
401 u16 companding
= snd_soc_component_read(component
,
402 WM8940_COMPANDINGCTL
) & 0xFFDF;
405 priv
->fs
= params_rate(params
);
406 ret
= wm8940_update_clocks(dai
);
411 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
412 && params_channels(params
) == 2)
415 switch (params_rate(params
)) {
417 addcntrl
|= (0x5 << 1);
420 addcntrl
|= (0x4 << 1);
423 addcntrl
|= (0x3 << 1);
426 addcntrl
|= (0x2 << 1);
429 addcntrl
|= (0x1 << 1);
435 ret
= snd_soc_component_write(component
, WM8940_ADDCNTRL
, addcntrl
);
439 switch (params_width(params
)) {
441 companding
= companding
| (1 << 5);
455 ret
= snd_soc_component_write(component
, WM8940_COMPANDINGCTL
, companding
);
458 ret
= snd_soc_component_write(component
, WM8940_IFACE
, iface
);
464 static int wm8940_mute(struct snd_soc_dai
*dai
, int mute
, int direction
)
466 struct snd_soc_component
*component
= dai
->component
;
467 u16 mute_reg
= snd_soc_component_read(component
, WM8940_DAC
) & 0xffbf;
472 return snd_soc_component_write(component
, WM8940_DAC
, mute_reg
);
475 static int wm8940_set_bias_level(struct snd_soc_component
*component
,
476 enum snd_soc_bias_level level
)
478 struct wm8940_priv
*wm8940
= snd_soc_component_get_drvdata(component
);
480 u16 pwr_reg
= snd_soc_component_read(component
, WM8940_POWER1
) & 0x1F0;
484 case SND_SOC_BIAS_ON
:
485 /* ensure bufioen and biasen */
486 pwr_reg
|= (1 << 2) | (1 << 3);
487 /* Enable thermal shutdown */
488 val
= snd_soc_component_read(component
, WM8940_OUTPUTCTL
);
489 ret
= snd_soc_component_write(component
, WM8940_OUTPUTCTL
, val
| 0x2);
492 /* set vmid to 75k */
493 ret
= snd_soc_component_write(component
, WM8940_POWER1
, pwr_reg
| 0x1);
495 case SND_SOC_BIAS_PREPARE
:
496 /* ensure bufioen and biasen */
497 pwr_reg
|= (1 << 2) | (1 << 3);
498 ret
= snd_soc_component_write(component
, WM8940_POWER1
, pwr_reg
| 0x1);
500 case SND_SOC_BIAS_STANDBY
:
501 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_OFF
) {
502 ret
= regcache_sync(wm8940
->regmap
);
504 dev_err(component
->dev
, "Failed to sync cache: %d\n", ret
);
509 /* ensure bufioen and biasen */
510 pwr_reg
|= (1 << 2) | (1 << 3);
511 /* set vmid to 300k for standby */
512 ret
= snd_soc_component_write(component
, WM8940_POWER1
, pwr_reg
| 0x2);
514 case SND_SOC_BIAS_OFF
:
515 ret
= snd_soc_component_write(component
, WM8940_POWER1
, pwr_reg
);
523 unsigned int pre_scale
:2;
528 static struct pll_ pll_div
;
530 /* The size in bits of the pll divide multiplied by 10
531 * to allow rounding later */
532 #define FIXED_PLL_SIZE ((1 << 24) * 10)
533 static void pll_factors(unsigned int target
, unsigned int source
)
535 unsigned long long Kpart
;
536 unsigned int K
, Ndiv
, Nmod
;
537 /* The left shift ist to avoid accuracy loss when right shifting */
538 Ndiv
= target
/ source
;
543 pll_div
.pre_scale
= 0;
544 Ndiv
= target
/ source
;
545 } else if (Ndiv
< 3) {
548 pll_div
.pre_scale
= 3;
549 Ndiv
= target
/ source
;
550 } else if (Ndiv
< 6) {
553 pll_div
.pre_scale
= 2;
554 Ndiv
= target
/ source
;
556 pll_div
.pre_scale
= 1;
558 if ((Ndiv
< 6) || (Ndiv
> 12))
560 "WM8940 N value %d outwith recommended range!d\n",
564 Nmod
= target
% source
;
565 Kpart
= FIXED_PLL_SIZE
* (long long)Nmod
;
567 do_div(Kpart
, source
);
569 K
= Kpart
& 0xFFFFFFFF;
571 /* Check if we need to round */
575 /* Move down to proper range now rounding is done */
581 /* Untested at the moment */
582 static int wm8940_set_dai_pll(struct snd_soc_dai
*codec_dai
, int pll_id
,
583 int source
, unsigned int freq_in
, unsigned int freq_out
)
585 struct snd_soc_component
*component
= codec_dai
->component
;
589 reg
= snd_soc_component_read(component
, WM8940_POWER1
);
590 snd_soc_component_write(component
, WM8940_POWER1
, reg
& 0x1df);
592 if (freq_in
== 0 || freq_out
== 0) {
593 /* Clock CODEC directly from MCLK */
594 reg
= snd_soc_component_read(component
, WM8940_CLOCK
);
595 snd_soc_component_write(component
, WM8940_CLOCK
, reg
& 0x0ff);
597 snd_soc_component_write(component
, WM8940_PLLN
, (1 << 7));
601 /* Pll is followed by a frequency divide by 4 */
602 pll_factors(freq_out
*4, freq_in
);
604 snd_soc_component_write(component
, WM8940_PLLN
,
605 (pll_div
.pre_scale
<< 4) | pll_div
.n
| (1 << 6));
606 else /* No factional component */
607 snd_soc_component_write(component
, WM8940_PLLN
,
608 (pll_div
.pre_scale
<< 4) | pll_div
.n
);
609 snd_soc_component_write(component
, WM8940_PLLK1
, pll_div
.k
>> 18);
610 snd_soc_component_write(component
, WM8940_PLLK2
, (pll_div
.k
>> 9) & 0x1ff);
611 snd_soc_component_write(component
, WM8940_PLLK3
, pll_div
.k
& 0x1ff);
613 reg
= snd_soc_component_read(component
, WM8940_POWER1
);
614 snd_soc_component_write(component
, WM8940_POWER1
, reg
| 0x020);
616 /* Run CODEC from PLL instead of MCLK */
617 reg
= snd_soc_component_read(component
, WM8940_CLOCK
);
618 snd_soc_component_write(component
, WM8940_CLOCK
, reg
| 0x100);
623 static int wm8940_set_dai_clkdiv(struct snd_soc_dai
*codec_dai
,
626 struct snd_soc_component
*component
= codec_dai
->component
;
632 reg
= snd_soc_component_read(component
, WM8940_CLOCK
) & 0xFFE3;
633 ret
= snd_soc_component_write(component
, WM8940_CLOCK
, reg
| (div
<< 2));
636 reg
= snd_soc_component_read(component
, WM8940_CLOCK
) & 0xFF1F;
637 ret
= snd_soc_component_write(component
, WM8940_CLOCK
, reg
| (div
<< 5));
639 case WM8940_OPCLKDIV
:
640 reg
= snd_soc_component_read(component
, WM8940_GPIO
) & 0xFFCF;
641 ret
= snd_soc_component_write(component
, WM8940_GPIO
, reg
| (div
<< 4));
647 static unsigned int wm8940_get_mclkdiv(unsigned int f_in
, unsigned int f_out
,
650 unsigned int ratio
= 2 * f_in
/ f_out
;
653 *mclkdiv
= WM8940_MCLKDIV_1
;
655 } else if (ratio
== 3) {
656 *mclkdiv
= WM8940_MCLKDIV_1_5
;
657 } else if (ratio
== 4) {
658 *mclkdiv
= WM8940_MCLKDIV_2
;
659 } else if (ratio
<= 6) {
660 *mclkdiv
= WM8940_MCLKDIV_3
;
662 } else if (ratio
<= 8) {
663 *mclkdiv
= WM8940_MCLKDIV_4
;
665 } else if (ratio
<= 12) {
666 *mclkdiv
= WM8940_MCLKDIV_6
;
668 } else if (ratio
<= 16) {
669 *mclkdiv
= WM8940_MCLKDIV_8
;
672 *mclkdiv
= WM8940_MCLKDIV_12
;
676 return f_out
* ratio
/ 2;
679 static int wm8940_update_clocks(struct snd_soc_dai
*dai
)
681 struct snd_soc_component
*codec
= dai
->component
;
682 struct wm8940_priv
*priv
= snd_soc_component_get_drvdata(codec
);
684 unsigned int fpll
= 0;
688 if (!priv
->mclk
|| !priv
->fs
)
691 fs256
= 256 * priv
->fs
;
693 f
= wm8940_get_mclkdiv(priv
->mclk
, fs256
, &mclkdiv
);
694 if (f
!= priv
->mclk
) {
695 /* The PLL performs best around 90MHz */
696 fpll
= wm8940_get_mclkdiv(22500000, fs256
, &mclkdiv
);
699 wm8940_set_dai_pll(dai
, 0, 0, priv
->mclk
, fpll
);
700 wm8940_set_dai_clkdiv(dai
, WM8940_MCLKDIV
, mclkdiv
);
705 static int wm8940_set_dai_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
706 unsigned int freq
, int dir
)
708 struct snd_soc_component
*codec
= dai
->component
;
709 struct wm8940_priv
*priv
= snd_soc_component_get_drvdata(codec
);
711 if (dir
!= SND_SOC_CLOCK_IN
)
716 return wm8940_update_clocks(dai
);
719 #define WM8940_RATES SNDRV_PCM_RATE_8000_48000
721 #define WM8940_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
722 SNDRV_PCM_FMTBIT_S16_LE | \
723 SNDRV_PCM_FMTBIT_S20_3LE | \
724 SNDRV_PCM_FMTBIT_S24_LE | \
725 SNDRV_PCM_FMTBIT_S32_LE)
727 static const struct snd_soc_dai_ops wm8940_dai_ops
= {
728 .hw_params
= wm8940_i2s_hw_params
,
729 .set_sysclk
= wm8940_set_dai_sysclk
,
730 .mute_stream
= wm8940_mute
,
731 .set_fmt
= wm8940_set_dai_fmt
,
732 .set_clkdiv
= wm8940_set_dai_clkdiv
,
733 .set_pll
= wm8940_set_dai_pll
,
734 .no_capture_mute
= 1,
737 static struct snd_soc_dai_driver wm8940_dai
= {
738 .name
= "wm8940-hifi",
740 .stream_name
= "Playback",
743 .rates
= WM8940_RATES
,
744 .formats
= WM8940_FORMATS
,
747 .stream_name
= "Capture",
750 .rates
= WM8940_RATES
,
751 .formats
= WM8940_FORMATS
,
753 .ops
= &wm8940_dai_ops
,
757 static int wm8940_probe(struct snd_soc_component
*component
)
759 struct wm8940_setup_data
*pdata
= component
->dev
->platform_data
;
764 * Check chip ID for wm8940 - value of 0x00 offset
765 * SOFTWARE_RESET on write
768 reg
= snd_soc_component_read(component
, WM8940_SOFTRESET
);
769 if (reg
!= WM8940_CHIP_ID
) {
770 dev_err(component
->dev
, "Wrong wm8940 chip ID: 0x%x\n", reg
);
774 ret
= wm8940_reset(component
);
776 dev_err(component
->dev
, "Failed to issue reset\n");
780 snd_soc_component_force_bias_level(component
, SND_SOC_BIAS_STANDBY
);
782 ret
= snd_soc_component_write(component
, WM8940_POWER1
, 0x180);
787 reg
= snd_soc_component_read(component
, WM8940_OUTPUTCTL
);
788 ret
= snd_soc_component_write(component
, WM8940_OUTPUTCTL
, reg
| pdata
->vroi
);
796 static const struct snd_soc_component_driver soc_component_dev_wm8940
= {
797 .probe
= wm8940_probe
,
798 .set_bias_level
= wm8940_set_bias_level
,
799 .controls
= wm8940_snd_controls
,
800 .num_controls
= ARRAY_SIZE(wm8940_snd_controls
),
801 .dapm_widgets
= wm8940_dapm_widgets
,
802 .num_dapm_widgets
= ARRAY_SIZE(wm8940_dapm_widgets
),
803 .dapm_routes
= wm8940_dapm_routes
,
804 .num_dapm_routes
= ARRAY_SIZE(wm8940_dapm_routes
),
805 .suspend_bias_off
= 1,
807 .use_pmdown_time
= 1,
811 static const struct regmap_config wm8940_regmap
= {
815 .max_register
= WM8940_MONOMIX
,
816 .reg_defaults
= wm8940_reg_defaults
,
817 .num_reg_defaults
= ARRAY_SIZE(wm8940_reg_defaults
),
818 .cache_type
= REGCACHE_MAPLE
,
820 .readable_reg
= wm8940_readable_register
,
821 .volatile_reg
= wm8940_volatile_register
,
824 static int wm8940_i2c_probe(struct i2c_client
*i2c
)
826 struct wm8940_priv
*wm8940
;
829 wm8940
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm8940_priv
),
834 wm8940
->regmap
= devm_regmap_init_i2c(i2c
, &wm8940_regmap
);
835 if (IS_ERR(wm8940
->regmap
))
836 return PTR_ERR(wm8940
->regmap
);
838 i2c_set_clientdata(i2c
, wm8940
);
840 ret
= devm_snd_soc_register_component(&i2c
->dev
,
841 &soc_component_dev_wm8940
, &wm8940_dai
, 1);
846 static const struct i2c_device_id wm8940_i2c_id
[] = {
850 MODULE_DEVICE_TABLE(i2c
, wm8940_i2c_id
);
852 static const struct of_device_id wm8940_of_match
[] = {
853 { .compatible
= "wlf,wm8940", },
856 MODULE_DEVICE_TABLE(of
, wm8940_of_match
);
858 static struct i2c_driver wm8940_i2c_driver
= {
861 .of_match_table
= wm8940_of_match
,
863 .probe
= wm8940_i2c_probe
,
864 .id_table
= wm8940_i2c_id
,
867 module_i2c_driver(wm8940_i2c_driver
);
869 MODULE_DESCRIPTION("ASoC WM8940 driver");
870 MODULE_AUTHOR("Jonathan Cameron");
871 MODULE_LICENSE("GPL");