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/platform_device.h>
32 #include <linux/spi/spi.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/soc-dapm.h>
38 #include <sound/initval.h>
39 #include <sound/tlv.h>
45 u16 reg_cache
[WM8940_CACHEREGNUM
];
46 struct snd_soc_codec codec
;
49 static u16 wm8940_reg_defaults
[] = {
50 0x8940, /* Soft Reset */
54 0x0010, /* Interface Control */
55 0x0000, /* Companding Control */
56 0x0140, /* Clock Control */
57 0x0000, /* Additional Controls */
58 0x0000, /* GPIO Control */
59 0x0002, /* Auto Increment Control */
60 0x0000, /* DAC Control */
61 0x00FF, /* DAC Volume */
64 0x0100, /* ADC Control */
65 0x00FF, /* ADC Volume */
66 0x0000, /* Notch Filter 1 Control 1 */
67 0x0000, /* Notch Filter 1 Control 2 */
68 0x0000, /* Notch Filter 2 Control 1 */
69 0x0000, /* Notch Filter 2 Control 2 */
70 0x0000, /* Notch Filter 3 Control 1 */
71 0x0000, /* Notch Filter 3 Control 2 */
72 0x0000, /* Notch Filter 4 Control 1 */
73 0x0000, /* Notch Filter 4 Control 2 */
74 0x0032, /* DAC Limit Control 1 */
75 0x0000, /* DAC Limit Control 2 */
82 0x0038, /* ALC Control 1 */
83 0x000B, /* ALC Control 2 */
84 0x0032, /* ALC Control 3 */
85 0x0000, /* Noise Gate */
92 0x0030, /* ALC Control 4 */
94 0x0002, /* Input Control */
95 0x0050, /* PGA Gain */
97 0x0002, /* ADC Boost Control */
99 0x0002, /* Output Control */
100 0x0000, /* Speaker Mixer Control */
104 0x0079, /* Speaker Volume */
106 0x0000, /* Mono Mixer Control */
109 static const char *wm8940_companding
[] = { "Off", "NC", "u-law", "A-law" };
110 static const struct soc_enum wm8940_adc_companding_enum
111 = SOC_ENUM_SINGLE(WM8940_COMPANDINGCTL
, 1, 4, wm8940_companding
);
112 static const struct soc_enum wm8940_dac_companding_enum
113 = SOC_ENUM_SINGLE(WM8940_COMPANDINGCTL
, 3, 4, wm8940_companding
);
115 static const char *wm8940_alc_mode_text
[] = {"ALC", "Limiter"};
116 static const struct soc_enum wm8940_alc_mode_enum
117 = SOC_ENUM_SINGLE(WM8940_ALC3
, 8, 2, wm8940_alc_mode_text
);
119 static const char *wm8940_mic_bias_level_text
[] = {"0.9", "0.65"};
120 static const struct soc_enum wm8940_mic_bias_level_enum
121 = SOC_ENUM_SINGLE(WM8940_INPUTCTL
, 8, 2, wm8940_mic_bias_level_text
);
123 static const char *wm8940_filter_mode_text
[] = {"Audio", "Application"};
124 static const struct soc_enum wm8940_filter_mode_enum
125 = SOC_ENUM_SINGLE(WM8940_ADC
, 7, 2, wm8940_filter_mode_text
);
127 static DECLARE_TLV_DB_SCALE(wm8940_spk_vol_tlv
, -5700, 100, 1);
128 static DECLARE_TLV_DB_SCALE(wm8940_att_tlv
, -1000, 1000, 0);
129 static DECLARE_TLV_DB_SCALE(wm8940_pga_vol_tlv
, -1200, 75, 0);
130 static DECLARE_TLV_DB_SCALE(wm8940_alc_min_tlv
, -1200, 600, 0);
131 static DECLARE_TLV_DB_SCALE(wm8940_alc_max_tlv
, 675, 600, 0);
132 static DECLARE_TLV_DB_SCALE(wm8940_alc_tar_tlv
, -2250, 50, 0);
133 static DECLARE_TLV_DB_SCALE(wm8940_lim_boost_tlv
, 0, 100, 0);
134 static DECLARE_TLV_DB_SCALE(wm8940_lim_thresh_tlv
, -600, 100, 0);
135 static DECLARE_TLV_DB_SCALE(wm8940_adc_tlv
, -12750, 50, 1);
136 static DECLARE_TLV_DB_SCALE(wm8940_capture_boost_vol_tlv
, 0, 2000, 0);
138 static const struct snd_kcontrol_new wm8940_snd_controls
[] = {
139 SOC_SINGLE("Digital Loopback Switch", WM8940_COMPANDINGCTL
,
141 SOC_ENUM("DAC Companding", wm8940_dac_companding_enum
),
142 SOC_ENUM("ADC Companding", wm8940_adc_companding_enum
),
144 SOC_ENUM("ALC Mode", wm8940_alc_mode_enum
),
145 SOC_SINGLE("ALC Switch", WM8940_ALC1
, 8, 1, 0),
146 SOC_SINGLE_TLV("ALC Capture Max Gain", WM8940_ALC1
,
147 3, 7, 1, wm8940_alc_max_tlv
),
148 SOC_SINGLE_TLV("ALC Capture Min Gain", WM8940_ALC1
,
149 0, 7, 0, wm8940_alc_min_tlv
),
150 SOC_SINGLE_TLV("ALC Capture Target", WM8940_ALC2
,
151 0, 14, 0, wm8940_alc_tar_tlv
),
152 SOC_SINGLE("ALC Capture Hold", WM8940_ALC2
, 4, 10, 0),
153 SOC_SINGLE("ALC Capture Decay", WM8940_ALC3
, 4, 10, 0),
154 SOC_SINGLE("ALC Capture Attach", WM8940_ALC3
, 0, 10, 0),
155 SOC_SINGLE("ALC ZC Switch", WM8940_ALC4
, 1, 1, 0),
156 SOC_SINGLE("ALC Capture Noise Gate Switch", WM8940_NOISEGATE
,
158 SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8940_NOISEGATE
,
161 SOC_SINGLE("DAC Playback Limiter Switch", WM8940_DACLIM1
, 8, 1, 0),
162 SOC_SINGLE("DAC Playback Limiter Attack", WM8940_DACLIM1
, 0, 9, 0),
163 SOC_SINGLE("DAC Playback Limiter Decay", WM8940_DACLIM1
, 4, 11, 0),
164 SOC_SINGLE_TLV("DAC Playback Limiter Threshold", WM8940_DACLIM2
,
165 4, 9, 1, wm8940_lim_thresh_tlv
),
166 SOC_SINGLE_TLV("DAC Playback Limiter Boost", WM8940_DACLIM2
,
167 0, 12, 0, wm8940_lim_boost_tlv
),
169 SOC_SINGLE("Capture PGA ZC Switch", WM8940_PGAGAIN
, 7, 1, 0),
170 SOC_SINGLE_TLV("Capture PGA Volume", WM8940_PGAGAIN
,
171 0, 63, 0, wm8940_pga_vol_tlv
),
172 SOC_SINGLE_TLV("Digital Playback Volume", WM8940_DACVOL
,
173 0, 255, 0, wm8940_adc_tlv
),
174 SOC_SINGLE_TLV("Digital Capture Volume", WM8940_ADCVOL
,
175 0, 255, 0, wm8940_adc_tlv
),
176 SOC_ENUM("Mic Bias Level", wm8940_mic_bias_level_enum
),
177 SOC_SINGLE_TLV("Capture Boost Volue", WM8940_ADCBOOST
,
178 8, 1, 0, wm8940_capture_boost_vol_tlv
),
179 SOC_SINGLE_TLV("Speaker Playback Volume", WM8940_SPKVOL
,
180 0, 63, 0, wm8940_spk_vol_tlv
),
181 SOC_SINGLE("Speaker Playback Switch", WM8940_SPKVOL
, 6, 1, 1),
183 SOC_SINGLE_TLV("Speaker Mixer Line Bypass Volume", WM8940_SPKVOL
,
184 8, 1, 1, wm8940_att_tlv
),
185 SOC_SINGLE("Speaker Playback ZC Switch", WM8940_SPKVOL
, 7, 1, 0),
187 SOC_SINGLE("Mono Out Switch", WM8940_MONOMIX
, 6, 1, 1),
188 SOC_SINGLE_TLV("Mono Mixer Line Bypass Volume", WM8940_MONOMIX
,
189 7, 1, 1, wm8940_att_tlv
),
191 SOC_SINGLE("High Pass Filter Switch", WM8940_ADC
, 8, 1, 0),
192 SOC_ENUM("High Pass Filter Mode", wm8940_filter_mode_enum
),
193 SOC_SINGLE("High Pass Filter Cut Off", WM8940_ADC
, 4, 7, 0),
194 SOC_SINGLE("ADC Inversion Switch", WM8940_ADC
, 0, 1, 0),
195 SOC_SINGLE("DAC Inversion Switch", WM8940_DAC
, 0, 1, 0),
196 SOC_SINGLE("DAC Auto Mute Switch", WM8940_DAC
, 2, 1, 0),
197 SOC_SINGLE("ZC Timeout Clock Switch", WM8940_ADDCNTRL
, 0, 1, 0),
200 static const struct snd_kcontrol_new wm8940_speaker_mixer_controls
[] = {
201 SOC_DAPM_SINGLE("Line Bypass Switch", WM8940_SPKMIX
, 1, 1, 0),
202 SOC_DAPM_SINGLE("Aux Playback Switch", WM8940_SPKMIX
, 5, 1, 0),
203 SOC_DAPM_SINGLE("PCM Playback Switch", WM8940_SPKMIX
, 0, 1, 0),
206 static const struct snd_kcontrol_new wm8940_mono_mixer_controls
[] = {
207 SOC_DAPM_SINGLE("Line Bypass Switch", WM8940_MONOMIX
, 1, 1, 0),
208 SOC_DAPM_SINGLE("Aux Playback Switch", WM8940_MONOMIX
, 2, 1, 0),
209 SOC_DAPM_SINGLE("PCM Playback Switch", WM8940_MONOMIX
, 0, 1, 0),
212 static DECLARE_TLV_DB_SCALE(wm8940_boost_vol_tlv
, -1500, 300, 1);
213 static const struct snd_kcontrol_new wm8940_input_boost_controls
[] = {
214 SOC_DAPM_SINGLE("Mic PGA Switch", WM8940_PGAGAIN
, 6, 1, 1),
215 SOC_DAPM_SINGLE_TLV("Aux Volume", WM8940_ADCBOOST
,
216 0, 7, 0, wm8940_boost_vol_tlv
),
217 SOC_DAPM_SINGLE_TLV("Mic Volume", WM8940_ADCBOOST
,
218 4, 7, 0, wm8940_boost_vol_tlv
),
221 static const struct snd_kcontrol_new wm8940_micpga_controls
[] = {
222 SOC_DAPM_SINGLE("AUX Switch", WM8940_INPUTCTL
, 2, 1, 0),
223 SOC_DAPM_SINGLE("MICP Switch", WM8940_INPUTCTL
, 0, 1, 0),
224 SOC_DAPM_SINGLE("MICN Switch", WM8940_INPUTCTL
, 1, 1, 0),
227 static const struct snd_soc_dapm_widget wm8940_dapm_widgets
[] = {
228 SND_SOC_DAPM_MIXER("Speaker Mixer", WM8940_POWER3
, 2, 0,
229 &wm8940_speaker_mixer_controls
[0],
230 ARRAY_SIZE(wm8940_speaker_mixer_controls
)),
231 SND_SOC_DAPM_MIXER("Mono Mixer", WM8940_POWER3
, 3, 0,
232 &wm8940_mono_mixer_controls
[0],
233 ARRAY_SIZE(wm8940_mono_mixer_controls
)),
234 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8940_POWER3
, 0, 0),
236 SND_SOC_DAPM_PGA("SpkN Out", WM8940_POWER3
, 5, 0, NULL
, 0),
237 SND_SOC_DAPM_PGA("SpkP Out", WM8940_POWER3
, 6, 0, NULL
, 0),
238 SND_SOC_DAPM_PGA("Mono Out", WM8940_POWER3
, 7, 0, NULL
, 0),
239 SND_SOC_DAPM_OUTPUT("MONOOUT"),
240 SND_SOC_DAPM_OUTPUT("SPKOUTP"),
241 SND_SOC_DAPM_OUTPUT("SPKOUTN"),
243 SND_SOC_DAPM_PGA("Aux Input", WM8940_POWER1
, 6, 0, NULL
, 0),
244 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8940_POWER2
, 0, 0),
245 SND_SOC_DAPM_MIXER("Mic PGA", WM8940_POWER2
, 2, 0,
246 &wm8940_micpga_controls
[0],
247 ARRAY_SIZE(wm8940_micpga_controls
)),
248 SND_SOC_DAPM_MIXER("Boost Mixer", WM8940_POWER2
, 4, 0,
249 &wm8940_input_boost_controls
[0],
250 ARRAY_SIZE(wm8940_input_boost_controls
)),
251 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8940_POWER1
, 4, 0),
253 SND_SOC_DAPM_INPUT("MICN"),
254 SND_SOC_DAPM_INPUT("MICP"),
255 SND_SOC_DAPM_INPUT("AUX"),
258 static const struct snd_soc_dapm_route audio_map
[] = {
259 /* Mono output mixer */
260 {"Mono Mixer", "PCM Playback Switch", "DAC"},
261 {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
262 {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
264 /* Speaker output mixer */
265 {"Speaker Mixer", "PCM Playback Switch", "DAC"},
266 {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
267 {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
270 {"Mono Out", NULL
, "Mono Mixer"},
271 {"MONOOUT", NULL
, "Mono Out"},
272 {"SpkN Out", NULL
, "Speaker Mixer"},
273 {"SpkP Out", NULL
, "Speaker Mixer"},
274 {"SPKOUTN", NULL
, "SpkN Out"},
275 {"SPKOUTP", NULL
, "SpkP Out"},
278 {"Mic PGA", "MICN Switch", "MICN"},
279 {"Mic PGA", "MICP Switch", "MICP"},
280 {"Mic PGA", "AUX Switch", "AUX"},
283 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
284 {"Boost Mixer", "Mic Volume", "MICP"},
285 {"Boost Mixer", "Aux Volume", "Aux Input"},
287 {"ADC", NULL
, "Boost Mixer"},
290 static int wm8940_add_widgets(struct snd_soc_codec
*codec
)
294 ret
= snd_soc_dapm_new_controls(codec
, wm8940_dapm_widgets
,
295 ARRAY_SIZE(wm8940_dapm_widgets
));
298 ret
= snd_soc_dapm_add_routes(codec
, audio_map
, ARRAY_SIZE(audio_map
));
301 ret
= snd_soc_dapm_new_widgets(codec
);
307 #define wm8940_reset(c) snd_soc_write(c, WM8940_SOFTRESET, 0);
309 static int wm8940_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
312 struct snd_soc_codec
*codec
= codec_dai
->codec
;
313 u16 iface
= snd_soc_read(codec
, WM8940_IFACE
) & 0xFE67;
314 u16 clk
= snd_soc_read(codec
, WM8940_CLOCK
) & 0x1fe;
316 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
317 case SND_SOC_DAIFMT_CBM_CFM
:
320 case SND_SOC_DAIFMT_CBS_CFS
:
325 snd_soc_write(codec
, WM8940_CLOCK
, clk
);
327 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
328 case SND_SOC_DAIFMT_I2S
:
331 case SND_SOC_DAIFMT_LEFT_J
:
334 case SND_SOC_DAIFMT_RIGHT_J
:
336 case SND_SOC_DAIFMT_DSP_A
:
339 case SND_SOC_DAIFMT_DSP_B
:
340 iface
|= (3 << 3) | (1 << 7);
344 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
345 case SND_SOC_DAIFMT_NB_NF
:
347 case SND_SOC_DAIFMT_NB_IF
:
350 case SND_SOC_DAIFMT_IB_NF
:
353 case SND_SOC_DAIFMT_IB_IF
:
354 iface
|= (1 << 8) | (1 << 7);
358 snd_soc_write(codec
, WM8940_IFACE
, iface
);
363 static int wm8940_i2s_hw_params(struct snd_pcm_substream
*substream
,
364 struct snd_pcm_hw_params
*params
,
365 struct snd_soc_dai
*dai
)
367 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
368 struct snd_soc_device
*socdev
= rtd
->socdev
;
369 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
370 u16 iface
= snd_soc_read(codec
, WM8940_IFACE
) & 0xFD9F;
371 u16 addcntrl
= snd_soc_read(codec
, WM8940_ADDCNTRL
) & 0xFFF1;
372 u16 companding
= snd_soc_read(codec
,
373 WM8940_COMPANDINGCTL
) & 0xFFDF;
377 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
378 && params_channels(params
) == 2)
381 switch (params_rate(params
)) {
383 addcntrl
|= (0x5 << 1);
386 addcntrl
|= (0x4 << 1);
389 addcntrl
|= (0x3 << 1);
392 addcntrl
|= (0x2 << 1);
395 addcntrl
|= (0x1 << 1);
401 ret
= snd_soc_write(codec
, WM8940_ADDCNTRL
, addcntrl
);
405 switch (params_format(params
)) {
406 case SNDRV_PCM_FORMAT_S8
:
407 companding
= companding
| (1 << 5);
409 case SNDRV_PCM_FORMAT_S16_LE
:
411 case SNDRV_PCM_FORMAT_S20_3LE
:
414 case SNDRV_PCM_FORMAT_S24_LE
:
417 case SNDRV_PCM_FORMAT_S32_LE
:
421 ret
= snd_soc_write(codec
, WM8940_COMPANDINGCTL
, companding
);
424 ret
= snd_soc_write(codec
, WM8940_IFACE
, iface
);
430 static int wm8940_mute(struct snd_soc_dai
*dai
, int mute
)
432 struct snd_soc_codec
*codec
= dai
->codec
;
433 u16 mute_reg
= snd_soc_read(codec
, WM8940_DAC
) & 0xffbf;
438 return snd_soc_write(codec
, WM8940_DAC
, mute_reg
);
441 static int wm8940_set_bias_level(struct snd_soc_codec
*codec
,
442 enum snd_soc_bias_level level
)
445 u16 pwr_reg
= snd_soc_read(codec
, WM8940_POWER1
) & 0x1F0;
449 case SND_SOC_BIAS_ON
:
450 /* ensure bufioen and biasen */
451 pwr_reg
|= (1 << 2) | (1 << 3);
452 /* Enable thermal shutdown */
453 val
= snd_soc_read(codec
, WM8940_OUTPUTCTL
);
454 ret
= snd_soc_write(codec
, WM8940_OUTPUTCTL
, val
| 0x2);
457 /* set vmid to 75k */
458 ret
= snd_soc_write(codec
, WM8940_POWER1
, pwr_reg
| 0x1);
460 case SND_SOC_BIAS_PREPARE
:
461 /* ensure bufioen and biasen */
462 pwr_reg
|= (1 << 2) | (1 << 3);
463 ret
= snd_soc_write(codec
, WM8940_POWER1
, pwr_reg
| 0x1);
465 case SND_SOC_BIAS_STANDBY
:
466 /* ensure bufioen and biasen */
467 pwr_reg
|= (1 << 2) | (1 << 3);
468 /* set vmid to 300k for standby */
469 ret
= snd_soc_write(codec
, WM8940_POWER1
, pwr_reg
| 0x2);
471 case SND_SOC_BIAS_OFF
:
472 ret
= snd_soc_write(codec
, WM8940_POWER1
, pwr_reg
);
475 codec
->bias_level
= level
;
481 unsigned int pre_scale
:2;
486 static struct pll_ pll_div
;
488 /* The size in bits of the pll divide multiplied by 10
489 * to allow rounding later */
490 #define FIXED_PLL_SIZE ((1 << 24) * 10)
491 static void pll_factors(unsigned int target
, unsigned int source
)
493 unsigned long long Kpart
;
494 unsigned int K
, Ndiv
, Nmod
;
495 /* The left shift ist to avoid accuracy loss when right shifting */
496 Ndiv
= target
/ source
;
501 pll_div
.pre_scale
= 0;
502 Ndiv
= target
/ source
;
503 } else if (Ndiv
< 3) {
506 pll_div
.pre_scale
= 3;
507 Ndiv
= target
/ source
;
508 } else if (Ndiv
< 6) {
511 pll_div
.pre_scale
= 2;
512 Ndiv
= target
/ source
;
514 pll_div
.pre_scale
= 1;
516 if ((Ndiv
< 6) || (Ndiv
> 12))
518 "WM8940 N value %d outwith recommended range!d\n",
522 Nmod
= target
% source
;
523 Kpart
= FIXED_PLL_SIZE
* (long long)Nmod
;
525 do_div(Kpart
, source
);
527 K
= Kpart
& 0xFFFFFFFF;
529 /* Check if we need to round */
533 /* Move down to proper range now rounding is done */
539 /* Untested at the moment */
540 static int wm8940_set_dai_pll(struct snd_soc_dai
*codec_dai
,
541 int pll_id
, unsigned int freq_in
, unsigned int freq_out
)
543 struct snd_soc_codec
*codec
= codec_dai
->codec
;
547 reg
= snd_soc_read(codec
, WM8940_POWER1
);
548 snd_soc_write(codec
, WM8940_POWER1
, reg
& 0x1df);
550 if (freq_in
== 0 || freq_out
== 0) {
551 /* Clock CODEC directly from MCLK */
552 reg
= snd_soc_read(codec
, WM8940_CLOCK
);
553 snd_soc_write(codec
, WM8940_CLOCK
, reg
& 0x0ff);
555 snd_soc_write(codec
, WM8940_PLLN
, (1 << 7));
559 /* Pll is followed by a frequency divide by 4 */
560 pll_factors(freq_out
*4, freq_in
);
562 snd_soc_write(codec
, WM8940_PLLN
,
563 (pll_div
.pre_scale
<< 4) | pll_div
.n
| (1 << 6));
564 else /* No factional component */
565 snd_soc_write(codec
, WM8940_PLLN
,
566 (pll_div
.pre_scale
<< 4) | pll_div
.n
);
567 snd_soc_write(codec
, WM8940_PLLK1
, pll_div
.k
>> 18);
568 snd_soc_write(codec
, WM8940_PLLK2
, (pll_div
.k
>> 9) & 0x1ff);
569 snd_soc_write(codec
, WM8940_PLLK3
, pll_div
.k
& 0x1ff);
571 reg
= snd_soc_read(codec
, WM8940_POWER1
);
572 snd_soc_write(codec
, WM8940_POWER1
, reg
| 0x020);
574 /* Run CODEC from PLL instead of MCLK */
575 reg
= snd_soc_read(codec
, WM8940_CLOCK
);
576 snd_soc_write(codec
, WM8940_CLOCK
, reg
| 0x100);
581 static int wm8940_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
582 int clk_id
, unsigned int freq
, int dir
)
584 struct snd_soc_codec
*codec
= codec_dai
->codec
;
585 struct wm8940_priv
*wm8940
= codec
->private_data
;
593 wm8940
->sysclk
= freq
;
599 static int wm8940_set_dai_clkdiv(struct snd_soc_dai
*codec_dai
,
602 struct snd_soc_codec
*codec
= codec_dai
->codec
;
608 reg
= snd_soc_read(codec
, WM8940_CLOCK
) & 0xFFEF3;
609 ret
= snd_soc_write(codec
, WM8940_CLOCK
, reg
| (div
<< 2));
612 reg
= snd_soc_read(codec
, WM8940_CLOCK
) & 0xFF1F;
613 ret
= snd_soc_write(codec
, WM8940_CLOCK
, reg
| (div
<< 5));
615 case WM8940_OPCLKDIV
:
616 reg
= snd_soc_read(codec
, WM8940_ADDCNTRL
) & 0xFFCF;
617 ret
= snd_soc_write(codec
, WM8940_ADDCNTRL
, reg
| (div
<< 4));
623 #define WM8940_RATES SNDRV_PCM_RATE_8000_48000
625 #define WM8940_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
626 SNDRV_PCM_FMTBIT_S16_LE | \
627 SNDRV_PCM_FMTBIT_S20_3LE | \
628 SNDRV_PCM_FMTBIT_S24_LE | \
629 SNDRV_PCM_FMTBIT_S32_LE)
631 static struct snd_soc_dai_ops wm8940_dai_ops
= {
632 .hw_params
= wm8940_i2s_hw_params
,
633 .set_sysclk
= wm8940_set_dai_sysclk
,
634 .digital_mute
= wm8940_mute
,
635 .set_fmt
= wm8940_set_dai_fmt
,
636 .set_clkdiv
= wm8940_set_dai_clkdiv
,
637 .set_pll
= wm8940_set_dai_pll
,
640 struct snd_soc_dai wm8940_dai
= {
643 .stream_name
= "Playback",
646 .rates
= WM8940_RATES
,
647 .formats
= WM8940_FORMATS
,
650 .stream_name
= "Capture",
653 .rates
= WM8940_RATES
,
654 .formats
= WM8940_FORMATS
,
656 .ops
= &wm8940_dai_ops
,
657 .symmetric_rates
= 1,
659 EXPORT_SYMBOL_GPL(wm8940_dai
);
661 static int wm8940_suspend(struct platform_device
*pdev
, pm_message_t state
)
663 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
664 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
666 return wm8940_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
669 static int wm8940_resume(struct platform_device
*pdev
)
671 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
672 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
676 u16
*cache
= codec
->reg_cache
;
678 /* Sync reg_cache with the hardware
679 * Could use auto incremented writes to speed this up
681 for (i
= 0; i
< ARRAY_SIZE(wm8940_reg_defaults
); i
++) {
683 data
[1] = (cache
[i
] & 0xFF00) >> 8;
684 data
[2] = cache
[i
] & 0x00FF;
685 ret
= codec
->hw_write(codec
->control_data
, data
, 3);
693 ret
= wm8940_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
696 ret
= wm8940_set_bias_level(codec
, codec
->suspend_bias_level
);
702 static struct snd_soc_codec
*wm8940_codec
;
704 static int wm8940_probe(struct platform_device
*pdev
)
706 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
707 struct snd_soc_codec
*codec
;
711 if (wm8940_codec
== NULL
) {
712 dev_err(&pdev
->dev
, "Codec device not registered\n");
716 socdev
->card
->codec
= wm8940_codec
;
717 codec
= wm8940_codec
;
719 mutex_init(&codec
->mutex
);
721 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
723 dev_err(codec
->dev
, "failed to create pcms: %d\n", ret
);
727 ret
= snd_soc_add_controls(codec
, wm8940_snd_controls
,
728 ARRAY_SIZE(wm8940_snd_controls
));
730 goto error_free_pcms
;
731 ret
= wm8940_add_widgets(codec
);
733 goto error_free_pcms
;
735 ret
= snd_soc_init_card(socdev
);
737 dev_err(codec
->dev
, "failed to register card: %d\n", ret
);
738 goto error_free_pcms
;
744 snd_soc_free_pcms(socdev
);
745 snd_soc_dapm_free(socdev
);
750 static int wm8940_remove(struct platform_device
*pdev
)
752 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
754 snd_soc_free_pcms(socdev
);
755 snd_soc_dapm_free(socdev
);
760 struct snd_soc_codec_device soc_codec_dev_wm8940
= {
761 .probe
= wm8940_probe
,
762 .remove
= wm8940_remove
,
763 .suspend
= wm8940_suspend
,
764 .resume
= wm8940_resume
,
766 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8940
);
768 static int wm8940_register(struct wm8940_priv
*wm8940
,
769 enum snd_soc_control_type control
)
771 struct wm8940_setup_data
*pdata
= wm8940
->codec
.dev
->platform_data
;
772 struct snd_soc_codec
*codec
= &wm8940
->codec
;
776 dev_err(codec
->dev
, "Another WM8940 is registered\n");
780 INIT_LIST_HEAD(&codec
->dapm_widgets
);
781 INIT_LIST_HEAD(&codec
->dapm_paths
);
783 codec
->private_data
= wm8940
;
784 codec
->name
= "WM8940";
785 codec
->owner
= THIS_MODULE
;
786 codec
->bias_level
= SND_SOC_BIAS_OFF
;
787 codec
->set_bias_level
= wm8940_set_bias_level
;
788 codec
->dai
= &wm8940_dai
;
790 codec
->reg_cache_size
= ARRAY_SIZE(wm8940_reg_defaults
);
791 codec
->reg_cache
= &wm8940
->reg_cache
;
793 ret
= snd_soc_codec_set_cache_io(codec
, 8, 16, control
);
795 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
799 memcpy(codec
->reg_cache
, wm8940_reg_defaults
,
800 sizeof(wm8940_reg_defaults
));
802 ret
= wm8940_reset(codec
);
804 dev_err(codec
->dev
, "Failed to issue reset\n");
808 wm8940_dai
.dev
= codec
->dev
;
810 wm8940_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
812 ret
= snd_soc_write(codec
, WM8940_POWER1
, 0x180);
817 dev_warn(codec
->dev
, "No platform data supplied\n");
819 reg
= snd_soc_read(codec
, WM8940_OUTPUTCTL
);
820 ret
= snd_soc_write(codec
, WM8940_OUTPUTCTL
, reg
| pdata
->vroi
);
826 wm8940_codec
= codec
;
828 ret
= snd_soc_register_codec(codec
);
830 dev_err(codec
->dev
, "Failed to register codec: %d\n", ret
);
834 ret
= snd_soc_register_dai(&wm8940_dai
);
836 dev_err(codec
->dev
, "Failed to register DAI: %d\n", ret
);
837 snd_soc_unregister_codec(codec
);
844 static void wm8940_unregister(struct wm8940_priv
*wm8940
)
846 wm8940_set_bias_level(&wm8940
->codec
, SND_SOC_BIAS_OFF
);
847 snd_soc_unregister_dai(&wm8940_dai
);
848 snd_soc_unregister_codec(&wm8940
->codec
);
853 static int wm8940_i2c_probe(struct i2c_client
*i2c
,
854 const struct i2c_device_id
*id
)
856 struct wm8940_priv
*wm8940
;
857 struct snd_soc_codec
*codec
;
859 wm8940
= kzalloc(sizeof *wm8940
, GFP_KERNEL
);
863 codec
= &wm8940
->codec
;
864 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
865 i2c_set_clientdata(i2c
, wm8940
);
866 codec
->control_data
= i2c
;
867 codec
->dev
= &i2c
->dev
;
869 return wm8940_register(wm8940
, SND_SOC_I2C
);
872 static int __devexit
wm8940_i2c_remove(struct i2c_client
*client
)
874 struct wm8940_priv
*wm8940
= i2c_get_clientdata(client
);
876 wm8940_unregister(wm8940
);
882 static int wm8940_i2c_suspend(struct i2c_client
*client
, pm_message_t msg
)
884 return snd_soc_suspend_device(&client
->dev
);
887 static int wm8940_i2c_resume(struct i2c_client
*client
)
889 return snd_soc_resume_device(&client
->dev
);
892 #define wm8940_i2c_suspend NULL
893 #define wm8940_i2c_resume NULL
896 static const struct i2c_device_id wm8940_i2c_id
[] = {
900 MODULE_DEVICE_TABLE(i2c
, wm8940_i2c_id
);
902 static struct i2c_driver wm8940_i2c_driver
= {
904 .name
= "WM8940 I2C Codec",
905 .owner
= THIS_MODULE
,
907 .probe
= wm8940_i2c_probe
,
908 .remove
= __devexit_p(wm8940_i2c_remove
),
909 .suspend
= wm8940_i2c_suspend
,
910 .resume
= wm8940_i2c_resume
,
911 .id_table
= wm8940_i2c_id
,
914 static int __init
wm8940_modinit(void)
918 ret
= i2c_add_driver(&wm8940_i2c_driver
);
920 printk(KERN_ERR
"Failed to register WM8940 I2C driver: %d\n",
924 module_init(wm8940_modinit
);
926 static void __exit
wm8940_exit(void)
928 i2c_del_driver(&wm8940_i2c_driver
);
930 module_exit(wm8940_exit
);
932 MODULE_DESCRIPTION("ASoC WM8940 driver");
933 MODULE_AUTHOR("Jonathan Cameron");
934 MODULE_LICENSE("GPL");