2 * wm8940.c -- WM8940 ALSA Soc Audio driver
4 * Author: Jonathan Cameron <jic23@cam.ac.uk>
7 * Copyright 2006 Wolfson Microelectronics PLC.
8 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * Not currently handled:
15 * Notch filter control
16 * AUXMode (inverting vs mixer)
17 * No means to obtain current gain if alc enabled.
19 * Fast VMID discharge for power down
21 * DLR and ALR Swaps not enabled
22 * Digital Sidetone not supported
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <linux/i2c.h>
31 #include <linux/regmap.h>
32 #include <linux/slab.h>
33 #include <sound/core.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/soc.h>
37 #include <sound/initval.h>
38 #include <sound/tlv.h>
44 struct regmap *regmap;
47 static bool wm8940_volatile_register(struct device *dev, unsigned int reg)
50 case WM8940_SOFTRESET:
57 static bool wm8940_readable_register(struct device *dev, unsigned int reg)
60 case WM8940_SOFTRESET:
65 case WM8940_COMPANDINGCTL:
87 case WM8940_NOISEGATE:
96 case WM8940_OUTPUTCTL:
106 static const struct reg_default wm8940_reg_defaults[] = {
107 { 0x1, 0x0000 }, /* Power 1 */
108 { 0x2, 0x0000 }, /* Power 2 */
109 { 0x3, 0x0000 }, /* Power 3 */
110 { 0x4, 0x0010 }, /* Interface Control */
111 { 0x5, 0x0000 }, /* Companding Control */
112 { 0x6, 0x0140 }, /* Clock Control */
113 { 0x7, 0x0000 }, /* Additional Controls */
114 { 0x8, 0x0000 }, /* GPIO Control */
115 { 0x9, 0x0002 }, /* Auto Increment Control */
116 { 0xa, 0x0000 }, /* DAC Control */
117 { 0xb, 0x00FF }, /* DAC Volume */
119 { 0xe, 0x0100 }, /* ADC Control */
120 { 0xf, 0x00FF }, /* ADC Volume */
121 { 0x10, 0x0000 }, /* Notch Filter 1 Control 1 */
122 { 0x11, 0x0000 }, /* Notch Filter 1 Control 2 */
123 { 0x12, 0x0000 }, /* Notch Filter 2 Control 1 */
124 { 0x13, 0x0000 }, /* Notch Filter 2 Control 2 */
125 { 0x14, 0x0000 }, /* Notch Filter 3 Control 1 */
126 { 0x15, 0x0000 }, /* Notch Filter 3 Control 2 */
127 { 0x16, 0x0000 }, /* Notch Filter 4 Control 1 */
128 { 0x17, 0x0000 }, /* Notch Filter 4 Control 2 */
129 { 0x18, 0x0032 }, /* DAC Limit Control 1 */
130 { 0x19, 0x0000 }, /* DAC Limit Control 2 */
132 { 0x20, 0x0038 }, /* ALC Control 1 */
133 { 0x21, 0x000B }, /* ALC Control 2 */
134 { 0x22, 0x0032 }, /* ALC Control 3 */
135 { 0x23, 0x0000 }, /* Noise Gate */
136 { 0x24, 0x0041 }, /* PLLN */
137 { 0x25, 0x000C }, /* PLLK1 */
138 { 0x26, 0x0093 }, /* PLLK2 */
139 { 0x27, 0x00E9 }, /* PLLK3 */
141 { 0x2a, 0x0030 }, /* ALC Control 4 */
143 { 0x2c, 0x0002 }, /* Input Control */
144 { 0x2d, 0x0050 }, /* PGA Gain */
146 { 0x2f, 0x0002 }, /* ADC Boost Control */
148 { 0x31, 0x0002 }, /* Output Control */
149 { 0x32, 0x0000 }, /* Speaker Mixer Control */
151 { 0x36, 0x0079 }, /* Speaker Volume */
153 { 0x38, 0x0000 }, /* Mono Mixer Control */
156 static const char *wm8940_companding[] = { "Off", "NC", "u-law", "A-law" };
157 static const struct soc_enum wm8940_adc_companding_enum
158 = SOC_ENUM_SINGLE(WM8940_COMPANDINGCTL, 1, 4, wm8940_companding);
159 static const struct soc_enum wm8940_dac_companding_enum
160 = SOC_ENUM_SINGLE(WM8940_COMPANDINGCTL, 3, 4, wm8940_companding);
162 static const char *wm8940_alc_mode_text[] = {"ALC", "Limiter"};
163 static const struct soc_enum wm8940_alc_mode_enum
164 = SOC_ENUM_SINGLE(WM8940_ALC3, 8, 2, wm8940_alc_mode_text);
166 static const char *wm8940_mic_bias_level_text[] = {"0.9", "0.65"};
167 static const struct soc_enum wm8940_mic_bias_level_enum
168 = SOC_ENUM_SINGLE(WM8940_INPUTCTL, 8, 2, wm8940_mic_bias_level_text);
170 static const char *wm8940_filter_mode_text[] = {"Audio", "Application"};
171 static const struct soc_enum wm8940_filter_mode_enum
172 = SOC_ENUM_SINGLE(WM8940_ADC, 7, 2, wm8940_filter_mode_text);
174 static DECLARE_TLV_DB_SCALE(wm8940_spk_vol_tlv, -5700, 100, 1);
175 static DECLARE_TLV_DB_SCALE(wm8940_att_tlv, -1000, 1000, 0);
176 static DECLARE_TLV_DB_SCALE(wm8940_pga_vol_tlv, -1200, 75, 0);
177 static DECLARE_TLV_DB_SCALE(wm8940_alc_min_tlv, -1200, 600, 0);
178 static DECLARE_TLV_DB_SCALE(wm8940_alc_max_tlv, 675, 600, 0);
179 static DECLARE_TLV_DB_SCALE(wm8940_alc_tar_tlv, -2250, 50, 0);
180 static DECLARE_TLV_DB_SCALE(wm8940_lim_boost_tlv, 0, 100, 0);
181 static DECLARE_TLV_DB_SCALE(wm8940_lim_thresh_tlv, -600, 100, 0);
182 static DECLARE_TLV_DB_SCALE(wm8940_adc_tlv, -12750, 50, 1);
183 static DECLARE_TLV_DB_SCALE(wm8940_capture_boost_vol_tlv, 0, 2000, 0);
185 static const struct snd_kcontrol_new wm8940_snd_controls[] = {
186 SOC_SINGLE("Digital Loopback Switch", WM8940_COMPANDINGCTL,
188 SOC_ENUM("DAC Companding", wm8940_dac_companding_enum),
189 SOC_ENUM("ADC Companding", wm8940_adc_companding_enum),
191 SOC_ENUM("ALC Mode", wm8940_alc_mode_enum),
192 SOC_SINGLE("ALC Switch", WM8940_ALC1, 8, 1, 0),
193 SOC_SINGLE_TLV("ALC Capture Max Gain", WM8940_ALC1,
194 3, 7, 1, wm8940_alc_max_tlv),
195 SOC_SINGLE_TLV("ALC Capture Min Gain", WM8940_ALC1,
196 0, 7, 0, wm8940_alc_min_tlv),
197 SOC_SINGLE_TLV("ALC Capture Target", WM8940_ALC2,
198 0, 14, 0, wm8940_alc_tar_tlv),
199 SOC_SINGLE("ALC Capture Hold", WM8940_ALC2, 4, 10, 0),
200 SOC_SINGLE("ALC Capture Decay", WM8940_ALC3, 4, 10, 0),
201 SOC_SINGLE("ALC Capture Attach", WM8940_ALC3, 0, 10, 0),
202 SOC_SINGLE("ALC ZC Switch", WM8940_ALC4, 1, 1, 0),
203 SOC_SINGLE("ALC Capture Noise Gate Switch", WM8940_NOISEGATE,
205 SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8940_NOISEGATE,
208 SOC_SINGLE("DAC Playback Limiter Switch", WM8940_DACLIM1, 8, 1, 0),
209 SOC_SINGLE("DAC Playback Limiter Attack", WM8940_DACLIM1, 0, 9, 0),
210 SOC_SINGLE("DAC Playback Limiter Decay", WM8940_DACLIM1, 4, 11, 0),
211 SOC_SINGLE_TLV("DAC Playback Limiter Threshold", WM8940_DACLIM2,
212 4, 9, 1, wm8940_lim_thresh_tlv),
213 SOC_SINGLE_TLV("DAC Playback Limiter Boost", WM8940_DACLIM2,
214 0, 12, 0, wm8940_lim_boost_tlv),
216 SOC_SINGLE("Capture PGA ZC Switch", WM8940_PGAGAIN, 7, 1, 0),
217 SOC_SINGLE_TLV("Capture PGA Volume", WM8940_PGAGAIN,
218 0, 63, 0, wm8940_pga_vol_tlv),
219 SOC_SINGLE_TLV("Digital Playback Volume", WM8940_DACVOL,
220 0, 255, 0, wm8940_adc_tlv),
221 SOC_SINGLE_TLV("Digital Capture Volume", WM8940_ADCVOL,
222 0, 255, 0, wm8940_adc_tlv),
223 SOC_ENUM("Mic Bias Level", wm8940_mic_bias_level_enum),
224 SOC_SINGLE_TLV("Capture Boost Volue", WM8940_ADCBOOST,
225 8, 1, 0, wm8940_capture_boost_vol_tlv),
226 SOC_SINGLE_TLV("Speaker Playback Volume", WM8940_SPKVOL,
227 0, 63, 0, wm8940_spk_vol_tlv),
228 SOC_SINGLE("Speaker Playback Switch", WM8940_SPKVOL, 6, 1, 1),
230 SOC_SINGLE_TLV("Speaker Mixer Line Bypass Volume", WM8940_SPKVOL,
231 8, 1, 1, wm8940_att_tlv),
232 SOC_SINGLE("Speaker Playback ZC Switch", WM8940_SPKVOL, 7, 1, 0),
234 SOC_SINGLE("Mono Out Switch", WM8940_MONOMIX, 6, 1, 1),
235 SOC_SINGLE_TLV("Mono Mixer Line Bypass Volume", WM8940_MONOMIX,
236 7, 1, 1, wm8940_att_tlv),
238 SOC_SINGLE("High Pass Filter Switch", WM8940_ADC, 8, 1, 0),
239 SOC_ENUM("High Pass Filter Mode", wm8940_filter_mode_enum),
240 SOC_SINGLE("High Pass Filter Cut Off", WM8940_ADC, 4, 7, 0),
241 SOC_SINGLE("ADC Inversion Switch", WM8940_ADC, 0, 1, 0),
242 SOC_SINGLE("DAC Inversion Switch", WM8940_DAC, 0, 1, 0),
243 SOC_SINGLE("DAC Auto Mute Switch", WM8940_DAC, 2, 1, 0),
244 SOC_SINGLE("ZC Timeout Clock Switch", WM8940_ADDCNTRL, 0, 1, 0),
247 static const struct snd_kcontrol_new wm8940_speaker_mixer_controls[] = {
248 SOC_DAPM_SINGLE("Line Bypass Switch", WM8940_SPKMIX, 1, 1, 0),
249 SOC_DAPM_SINGLE("Aux Playback Switch", WM8940_SPKMIX, 5, 1, 0),
250 SOC_DAPM_SINGLE("PCM Playback Switch", WM8940_SPKMIX, 0, 1, 0),
253 static const struct snd_kcontrol_new wm8940_mono_mixer_controls[] = {
254 SOC_DAPM_SINGLE("Line Bypass Switch", WM8940_MONOMIX, 1, 1, 0),
255 SOC_DAPM_SINGLE("Aux Playback Switch", WM8940_MONOMIX, 2, 1, 0),
256 SOC_DAPM_SINGLE("PCM Playback Switch", WM8940_MONOMIX, 0, 1, 0),
259 static DECLARE_TLV_DB_SCALE(wm8940_boost_vol_tlv, -1500, 300, 1);
260 static const struct snd_kcontrol_new wm8940_input_boost_controls[] = {
261 SOC_DAPM_SINGLE("Mic PGA Switch", WM8940_PGAGAIN, 6, 1, 1),
262 SOC_DAPM_SINGLE_TLV("Aux Volume", WM8940_ADCBOOST,
263 0, 7, 0, wm8940_boost_vol_tlv),
264 SOC_DAPM_SINGLE_TLV("Mic Volume", WM8940_ADCBOOST,
265 4, 7, 0, wm8940_boost_vol_tlv),
268 static const struct snd_kcontrol_new wm8940_micpga_controls[] = {
269 SOC_DAPM_SINGLE("AUX Switch", WM8940_INPUTCTL, 2, 1, 0),
270 SOC_DAPM_SINGLE("MICP Switch", WM8940_INPUTCTL, 0, 1, 0),
271 SOC_DAPM_SINGLE("MICN Switch", WM8940_INPUTCTL, 1, 1, 0),
274 static const struct snd_soc_dapm_widget wm8940_dapm_widgets[] = {
275 SND_SOC_DAPM_MIXER("Speaker Mixer", WM8940_POWER3, 2, 0,
276 &wm8940_speaker_mixer_controls[0],
277 ARRAY_SIZE(wm8940_speaker_mixer_controls)),
278 SND_SOC_DAPM_MIXER("Mono Mixer", WM8940_POWER3, 3, 0,
279 &wm8940_mono_mixer_controls[0],
280 ARRAY_SIZE(wm8940_mono_mixer_controls)),
281 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8940_POWER3, 0, 0),
283 SND_SOC_DAPM_PGA("SpkN Out", WM8940_POWER3, 5, 0, NULL, 0),
284 SND_SOC_DAPM_PGA("SpkP Out", WM8940_POWER3, 6, 0, NULL, 0),
285 SND_SOC_DAPM_PGA("Mono Out", WM8940_POWER3, 7, 0, NULL, 0),
286 SND_SOC_DAPM_OUTPUT("MONOOUT"),
287 SND_SOC_DAPM_OUTPUT("SPKOUTP"),
288 SND_SOC_DAPM_OUTPUT("SPKOUTN"),
290 SND_SOC_DAPM_PGA("Aux Input", WM8940_POWER1, 6, 0, NULL, 0),
291 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8940_POWER2, 0, 0),
292 SND_SOC_DAPM_MIXER("Mic PGA", WM8940_POWER2, 2, 0,
293 &wm8940_micpga_controls[0],
294 ARRAY_SIZE(wm8940_micpga_controls)),
295 SND_SOC_DAPM_MIXER("Boost Mixer", WM8940_POWER2, 4, 0,
296 &wm8940_input_boost_controls[0],
297 ARRAY_SIZE(wm8940_input_boost_controls)),
298 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8940_POWER1, 4, 0),
300 SND_SOC_DAPM_INPUT("MICN"),
301 SND_SOC_DAPM_INPUT("MICP"),
302 SND_SOC_DAPM_INPUT("AUX"),
305 static const struct snd_soc_dapm_route wm8940_dapm_routes[] = {
306 /* Mono output mixer */
307 {"Mono Mixer", "PCM Playback Switch", "DAC"},
308 {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
309 {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
311 /* Speaker output mixer */
312 {"Speaker Mixer", "PCM Playback Switch", "DAC"},
313 {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
314 {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
317 {"Mono Out", NULL, "Mono Mixer"},
318 {"MONOOUT", NULL, "Mono Out"},
319 {"SpkN Out", NULL, "Speaker Mixer"},
320 {"SpkP Out", NULL, "Speaker Mixer"},
321 {"SPKOUTN", NULL, "SpkN Out"},
322 {"SPKOUTP", NULL, "SpkP Out"},
325 {"Mic PGA", "MICN Switch", "MICN"},
326 {"Mic PGA", "MICP Switch", "MICP"},
327 {"Mic PGA", "AUX Switch", "AUX"},
330 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
331 {"Boost Mixer", "Mic Volume", "MICP"},
332 {"Boost Mixer", "Aux Volume", "Aux Input"},
334 {"ADC", NULL, "Boost Mixer"},
337 #define wm8940_reset(c) snd_soc_write(c, WM8940_SOFTRESET, 0);
339 static int wm8940_set_dai_fmt(struct snd_soc_dai *codec_dai,
342 struct snd_soc_codec *codec = codec_dai->codec;
343 u16 iface = snd_soc_read(codec, WM8940_IFACE) & 0xFE67;
344 u16 clk = snd_soc_read(codec, WM8940_CLOCK) & 0x1fe;
346 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
347 case SND_SOC_DAIFMT_CBM_CFM:
350 case SND_SOC_DAIFMT_CBS_CFS:
355 snd_soc_write(codec, WM8940_CLOCK, clk);
357 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
358 case SND_SOC_DAIFMT_I2S:
361 case SND_SOC_DAIFMT_LEFT_J:
364 case SND_SOC_DAIFMT_RIGHT_J:
366 case SND_SOC_DAIFMT_DSP_A:
369 case SND_SOC_DAIFMT_DSP_B:
370 iface |= (3 << 3) | (1 << 7);
374 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
375 case SND_SOC_DAIFMT_NB_NF:
377 case SND_SOC_DAIFMT_NB_IF:
380 case SND_SOC_DAIFMT_IB_NF:
383 case SND_SOC_DAIFMT_IB_IF:
384 iface |= (1 << 8) | (1 << 7);
388 snd_soc_write(codec, WM8940_IFACE, iface);
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_codec *codec = dai->codec;
398 u16 iface = snd_soc_read(codec, WM8940_IFACE) & 0xFD9F;
399 u16 addcntrl = snd_soc_read(codec, WM8940_ADDCNTRL) & 0xFFF1;
400 u16 companding = snd_soc_read(codec,
401 WM8940_COMPANDINGCTL) & 0xFFDF;
405 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE
406 && params_channels(params) == 2)
409 switch (params_rate(params)) {
411 addcntrl |= (0x5 << 1);
414 addcntrl |= (0x4 << 1);
417 addcntrl |= (0x3 << 1);
420 addcntrl |= (0x2 << 1);
423 addcntrl |= (0x1 << 1);
429 ret = snd_soc_write(codec, WM8940_ADDCNTRL, addcntrl);
433 switch (params_format(params)) {
434 case SNDRV_PCM_FORMAT_S8:
435 companding = companding | (1 << 5);
437 case SNDRV_PCM_FORMAT_S16_LE:
439 case SNDRV_PCM_FORMAT_S20_3LE:
442 case SNDRV_PCM_FORMAT_S24_LE:
445 case SNDRV_PCM_FORMAT_S32_LE:
449 ret = snd_soc_write(codec, WM8940_COMPANDINGCTL, companding);
452 ret = snd_soc_write(codec, WM8940_IFACE, iface);
458 static int wm8940_mute(struct snd_soc_dai *dai, int mute)
460 struct snd_soc_codec *codec = dai->codec;
461 u16 mute_reg = snd_soc_read(codec, WM8940_DAC) & 0xffbf;
466 return snd_soc_write(codec, WM8940_DAC, mute_reg);
469 static int wm8940_set_bias_level(struct snd_soc_codec *codec,
470 enum snd_soc_bias_level level)
472 struct wm8940_priv *wm8940 = snd_soc_codec_get_drvdata(codec);
474 u16 pwr_reg = snd_soc_read(codec, WM8940_POWER1) & 0x1F0;
478 case SND_SOC_BIAS_ON:
479 /* ensure bufioen and biasen */
480 pwr_reg |= (1 << 2) | (1 << 3);
481 /* Enable thermal shutdown */
482 val = snd_soc_read(codec, WM8940_OUTPUTCTL);
483 ret = snd_soc_write(codec, WM8940_OUTPUTCTL, val | 0x2);
486 /* set vmid to 75k */
487 ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x1);
489 case SND_SOC_BIAS_PREPARE:
490 /* ensure bufioen and biasen */
491 pwr_reg |= (1 << 2) | (1 << 3);
492 ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x1);
494 case SND_SOC_BIAS_STANDBY:
495 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
496 ret = regcache_sync(wm8940->regmap);
498 dev_err(codec->dev, "Failed to sync cache: %d\n", ret);
503 /* ensure bufioen and biasen */
504 pwr_reg |= (1 << 2) | (1 << 3);
505 /* set vmid to 300k for standby */
506 ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x2);
508 case SND_SOC_BIAS_OFF:
509 ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg);
513 codec->dapm.bias_level = level;
519 unsigned int pre_scale:2;
524 static struct pll_ pll_div;
526 /* The size in bits of the pll divide multiplied by 10
527 * to allow rounding later */
528 #define FIXED_PLL_SIZE ((1 << 24) * 10)
529 static void pll_factors(unsigned int target, unsigned int source)
531 unsigned long long Kpart;
532 unsigned int K, Ndiv, Nmod;
533 /* The left shift ist to avoid accuracy loss when right shifting */
534 Ndiv = target / source;
539 pll_div.pre_scale = 0;
540 Ndiv = target / source;
541 } else if (Ndiv < 3) {
544 pll_div.pre_scale = 3;
545 Ndiv = target / source;
546 } else if (Ndiv < 6) {
549 pll_div.pre_scale = 2;
550 Ndiv = target / source;
552 pll_div.pre_scale = 1;
554 if ((Ndiv < 6) || (Ndiv > 12))
556 "WM8940 N value %d outwith recommended range!d\n",
560 Nmod = target % source;
561 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
563 do_div(Kpart, source);
565 K = Kpart & 0xFFFFFFFF;
567 /* Check if we need to round */
571 /* Move down to proper range now rounding is done */
577 /* Untested at the moment */
578 static int wm8940_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
579 int source, unsigned int freq_in, unsigned int freq_out)
581 struct snd_soc_codec *codec = codec_dai->codec;
585 reg = snd_soc_read(codec, WM8940_POWER1);
586 snd_soc_write(codec, WM8940_POWER1, reg & 0x1df);
588 if (freq_in == 0 || freq_out == 0) {
589 /* Clock CODEC directly from MCLK */
590 reg = snd_soc_read(codec, WM8940_CLOCK);
591 snd_soc_write(codec, WM8940_CLOCK, reg & 0x0ff);
593 snd_soc_write(codec, WM8940_PLLN, (1 << 7));
597 /* Pll is followed by a frequency divide by 4 */
598 pll_factors(freq_out*4, freq_in);
600 snd_soc_write(codec, WM8940_PLLN,
601 (pll_div.pre_scale << 4) | pll_div.n | (1 << 6));
602 else /* No factional component */
603 snd_soc_write(codec, WM8940_PLLN,
604 (pll_div.pre_scale << 4) | pll_div.n);
605 snd_soc_write(codec, WM8940_PLLK1, pll_div.k >> 18);
606 snd_soc_write(codec, WM8940_PLLK2, (pll_div.k >> 9) & 0x1ff);
607 snd_soc_write(codec, WM8940_PLLK3, pll_div.k & 0x1ff);
609 reg = snd_soc_read(codec, WM8940_POWER1);
610 snd_soc_write(codec, WM8940_POWER1, reg | 0x020);
612 /* Run CODEC from PLL instead of MCLK */
613 reg = snd_soc_read(codec, WM8940_CLOCK);
614 snd_soc_write(codec, WM8940_CLOCK, reg | 0x100);
619 static int wm8940_set_dai_sysclk(struct snd_soc_dai *codec_dai,
620 int clk_id, unsigned int freq, int dir)
622 struct snd_soc_codec *codec = codec_dai->codec;
623 struct wm8940_priv *wm8940 = snd_soc_codec_get_drvdata(codec);
631 wm8940->sysclk = freq;
637 static int wm8940_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
640 struct snd_soc_codec *codec = codec_dai->codec;
646 reg = snd_soc_read(codec, WM8940_CLOCK) & 0xFFE3;
647 ret = snd_soc_write(codec, WM8940_CLOCK, reg | (div << 2));
650 reg = snd_soc_read(codec, WM8940_CLOCK) & 0xFF1F;
651 ret = snd_soc_write(codec, WM8940_CLOCK, reg | (div << 5));
653 case WM8940_OPCLKDIV:
654 reg = snd_soc_read(codec, WM8940_GPIO) & 0xFFCF;
655 ret = snd_soc_write(codec, WM8940_GPIO, reg | (div << 4));
661 #define WM8940_RATES SNDRV_PCM_RATE_8000_48000
663 #define WM8940_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
664 SNDRV_PCM_FMTBIT_S16_LE | \
665 SNDRV_PCM_FMTBIT_S20_3LE | \
666 SNDRV_PCM_FMTBIT_S24_LE | \
667 SNDRV_PCM_FMTBIT_S32_LE)
669 static const struct snd_soc_dai_ops wm8940_dai_ops = {
670 .hw_params = wm8940_i2s_hw_params,
671 .set_sysclk = wm8940_set_dai_sysclk,
672 .digital_mute = wm8940_mute,
673 .set_fmt = wm8940_set_dai_fmt,
674 .set_clkdiv = wm8940_set_dai_clkdiv,
675 .set_pll = wm8940_set_dai_pll,
678 static struct snd_soc_dai_driver wm8940_dai = {
679 .name = "wm8940-hifi",
681 .stream_name = "Playback",
684 .rates = WM8940_RATES,
685 .formats = WM8940_FORMATS,
688 .stream_name = "Capture",
691 .rates = WM8940_RATES,
692 .formats = WM8940_FORMATS,
694 .ops = &wm8940_dai_ops,
695 .symmetric_rates = 1,
698 static int wm8940_suspend(struct snd_soc_codec *codec)
700 return wm8940_set_bias_level(codec, SND_SOC_BIAS_OFF);
703 static int wm8940_resume(struct snd_soc_codec *codec)
705 wm8940_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
709 static int wm8940_probe(struct snd_soc_codec *codec)
711 struct wm8940_setup_data *pdata = codec->dev->platform_data;
715 ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_REGMAP);
717 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
721 ret = wm8940_reset(codec);
723 dev_err(codec->dev, "Failed to issue reset\n");
727 wm8940_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
729 ret = snd_soc_write(codec, WM8940_POWER1, 0x180);
734 dev_warn(codec->dev, "No platform data supplied\n");
736 reg = snd_soc_read(codec, WM8940_OUTPUTCTL);
737 ret = snd_soc_write(codec, WM8940_OUTPUTCTL, reg | pdata->vroi);
745 static int wm8940_remove(struct snd_soc_codec *codec)
747 wm8940_set_bias_level(codec, SND_SOC_BIAS_OFF);
751 static struct snd_soc_codec_driver soc_codec_dev_wm8940 = {
752 .probe = wm8940_probe,
753 .remove = wm8940_remove,
754 .suspend = wm8940_suspend,
755 .resume = wm8940_resume,
756 .set_bias_level = wm8940_set_bias_level,
757 .controls = wm8940_snd_controls,
758 .num_controls = ARRAY_SIZE(wm8940_snd_controls),
759 .dapm_widgets = wm8940_dapm_widgets,
760 .num_dapm_widgets = ARRAY_SIZE(wm8940_dapm_widgets),
761 .dapm_routes = wm8940_dapm_routes,
762 .num_dapm_routes = ARRAY_SIZE(wm8940_dapm_routes),
765 static const struct regmap_config wm8940_regmap = {
769 .max_register = WM8940_MONOMIX,
770 .reg_defaults = wm8940_reg_defaults,
771 .num_reg_defaults = ARRAY_SIZE(wm8940_reg_defaults),
773 .readable_reg = wm8940_readable_register,
774 .volatile_reg = wm8940_volatile_register,
777 static int wm8940_i2c_probe(struct i2c_client *i2c,
778 const struct i2c_device_id *id)
780 struct wm8940_priv *wm8940;
783 wm8940 = devm_kzalloc(&i2c->dev, sizeof(struct wm8940_priv),
788 wm8940->regmap = devm_regmap_init_i2c(i2c, &wm8940_regmap);
789 if (IS_ERR(wm8940->regmap))
790 return PTR_ERR(wm8940->regmap);
792 i2c_set_clientdata(i2c, wm8940);
794 ret = snd_soc_register_codec(&i2c->dev,
795 &soc_codec_dev_wm8940, &wm8940_dai, 1);
800 static int wm8940_i2c_remove(struct i2c_client *client)
802 snd_soc_unregister_codec(&client->dev);
807 static const struct i2c_device_id wm8940_i2c_id[] = {
811 MODULE_DEVICE_TABLE(i2c, wm8940_i2c_id);
813 static struct i2c_driver wm8940_i2c_driver = {
816 .owner = THIS_MODULE,
818 .probe = wm8940_i2c_probe,
819 .remove = wm8940_i2c_remove,
820 .id_table = wm8940_i2c_id,
823 module_i2c_driver(wm8940_i2c_driver);
825 MODULE_DESCRIPTION("ASoC WM8940 driver");
826 MODULE_AUTHOR("Jonathan Cameron");
827 MODULE_LICENSE("GPL");