2 * wm8510.c -- WM8510 ALSA Soc Audio driver
4 * Copyright 2006 Wolfson Microelectronics PLC.
6 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/platform_device.h>
21 #include <linux/spi/spi.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27 #include <sound/initval.h>
31 #define WM8510_VERSION "0.6"
33 struct snd_soc_codec_device soc_codec_dev_wm8510
;
36 * wm8510 register cache
37 * We can't read the WM8510 register space when we are
38 * using 2 wire for device control, so we cache them instead.
40 static const u16 wm8510_reg
[WM8510_CACHEREGNUM
] = {
41 0x0000, 0x0000, 0x0000, 0x0000,
42 0x0050, 0x0000, 0x0140, 0x0000,
43 0x0000, 0x0000, 0x0000, 0x00ff,
44 0x0000, 0x0000, 0x0100, 0x00ff,
45 0x0000, 0x0000, 0x012c, 0x002c,
46 0x002c, 0x002c, 0x002c, 0x0000,
47 0x0032, 0x0000, 0x0000, 0x0000,
48 0x0000, 0x0000, 0x0000, 0x0000,
49 0x0038, 0x000b, 0x0032, 0x0000,
50 0x0008, 0x000c, 0x0093, 0x00e9,
51 0x0000, 0x0000, 0x0000, 0x0000,
52 0x0003, 0x0010, 0x0000, 0x0000,
53 0x0000, 0x0002, 0x0001, 0x0000,
54 0x0000, 0x0000, 0x0039, 0x0000,
58 #define WM8510_POWER1_BIASEN 0x08
59 #define WM8510_POWER1_BUFIOEN 0x10
62 * read wm8510 register cache
64 static inline unsigned int wm8510_read_reg_cache(struct snd_soc_codec
*codec
,
67 u16
*cache
= codec
->reg_cache
;
68 if (reg
== WM8510_RESET
)
70 if (reg
>= WM8510_CACHEREGNUM
)
76 * write wm8510 register cache
78 static inline void wm8510_write_reg_cache(struct snd_soc_codec
*codec
,
79 u16 reg
, unsigned int value
)
81 u16
*cache
= codec
->reg_cache
;
82 if (reg
>= WM8510_CACHEREGNUM
)
88 * write to the WM8510 register space
90 static int wm8510_write(struct snd_soc_codec
*codec
, unsigned int reg
,
96 * D15..D9 WM8510 register offset
97 * D8...D0 register data
99 data
[0] = (reg
<< 1) | ((value
>> 8) & 0x0001);
100 data
[1] = value
& 0x00ff;
102 wm8510_write_reg_cache(codec
, reg
, value
);
103 if (codec
->hw_write(codec
->control_data
, data
, 2) == 2)
109 #define wm8510_reset(c) wm8510_write(c, WM8510_RESET, 0)
111 static const char *wm8510_companding
[] = { "Off", "NC", "u-law", "A-law" };
112 static const char *wm8510_deemp
[] = { "None", "32kHz", "44.1kHz", "48kHz" };
113 static const char *wm8510_alc
[] = { "ALC", "Limiter" };
115 static const struct soc_enum wm8510_enum
[] = {
116 SOC_ENUM_SINGLE(WM8510_COMP
, 1, 4, wm8510_companding
), /* adc */
117 SOC_ENUM_SINGLE(WM8510_COMP
, 3, 4, wm8510_companding
), /* dac */
118 SOC_ENUM_SINGLE(WM8510_DAC
, 4, 4, wm8510_deemp
),
119 SOC_ENUM_SINGLE(WM8510_ALC3
, 8, 2, wm8510_alc
),
122 static const struct snd_kcontrol_new wm8510_snd_controls
[] = {
124 SOC_SINGLE("Digital Loopback Switch", WM8510_COMP
, 0, 1, 0),
126 SOC_ENUM("DAC Companding", wm8510_enum
[1]),
127 SOC_ENUM("ADC Companding", wm8510_enum
[0]),
129 SOC_ENUM("Playback De-emphasis", wm8510_enum
[2]),
130 SOC_SINGLE("DAC Inversion Switch", WM8510_DAC
, 0, 1, 0),
132 SOC_SINGLE("Master Playback Volume", WM8510_DACVOL
, 0, 127, 0),
134 SOC_SINGLE("High Pass Filter Switch", WM8510_ADC
, 8, 1, 0),
135 SOC_SINGLE("High Pass Cut Off", WM8510_ADC
, 4, 7, 0),
136 SOC_SINGLE("ADC Inversion Switch", WM8510_COMP
, 0, 1, 0),
138 SOC_SINGLE("Capture Volume", WM8510_ADCVOL
, 0, 127, 0),
140 SOC_SINGLE("DAC Playback Limiter Switch", WM8510_DACLIM1
, 8, 1, 0),
141 SOC_SINGLE("DAC Playback Limiter Decay", WM8510_DACLIM1
, 4, 15, 0),
142 SOC_SINGLE("DAC Playback Limiter Attack", WM8510_DACLIM1
, 0, 15, 0),
144 SOC_SINGLE("DAC Playback Limiter Threshold", WM8510_DACLIM2
, 4, 7, 0),
145 SOC_SINGLE("DAC Playback Limiter Boost", WM8510_DACLIM2
, 0, 15, 0),
147 SOC_SINGLE("ALC Enable Switch", WM8510_ALC1
, 8, 1, 0),
148 SOC_SINGLE("ALC Capture Max Gain", WM8510_ALC1
, 3, 7, 0),
149 SOC_SINGLE("ALC Capture Min Gain", WM8510_ALC1
, 0, 7, 0),
151 SOC_SINGLE("ALC Capture ZC Switch", WM8510_ALC2
, 8, 1, 0),
152 SOC_SINGLE("ALC Capture Hold", WM8510_ALC2
, 4, 7, 0),
153 SOC_SINGLE("ALC Capture Target", WM8510_ALC2
, 0, 15, 0),
155 SOC_ENUM("ALC Capture Mode", wm8510_enum
[3]),
156 SOC_SINGLE("ALC Capture Decay", WM8510_ALC3
, 4, 15, 0),
157 SOC_SINGLE("ALC Capture Attack", WM8510_ALC3
, 0, 15, 0),
159 SOC_SINGLE("ALC Capture Noise Gate Switch", WM8510_NGATE
, 3, 1, 0),
160 SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8510_NGATE
, 0, 7, 0),
162 SOC_SINGLE("Capture PGA ZC Switch", WM8510_INPPGA
, 7, 1, 0),
163 SOC_SINGLE("Capture PGA Volume", WM8510_INPPGA
, 0, 63, 0),
165 SOC_SINGLE("Speaker Playback ZC Switch", WM8510_SPKVOL
, 7, 1, 0),
166 SOC_SINGLE("Speaker Playback Switch", WM8510_SPKVOL
, 6, 1, 1),
167 SOC_SINGLE("Speaker Playback Volume", WM8510_SPKVOL
, 0, 63, 0),
168 SOC_SINGLE("Speaker Boost", WM8510_OUTPUT
, 2, 1, 0),
170 SOC_SINGLE("Capture Boost(+20dB)", WM8510_ADCBOOST
, 8, 1, 0),
171 SOC_SINGLE("Mono Playback Switch", WM8510_MONOMIX
, 6, 1, 1),
174 /* add non dapm controls */
175 static int wm8510_add_controls(struct snd_soc_codec
*codec
)
179 for (i
= 0; i
< ARRAY_SIZE(wm8510_snd_controls
); i
++) {
180 err
= snd_ctl_add(codec
->card
,
181 snd_soc_cnew(&wm8510_snd_controls
[i
], codec
,
190 /* Speaker Output Mixer */
191 static const struct snd_kcontrol_new wm8510_speaker_mixer_controls
[] = {
192 SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_SPKMIX
, 1, 1, 0),
193 SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_SPKMIX
, 5, 1, 0),
194 SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_SPKMIX
, 0, 1, 0),
197 /* Mono Output Mixer */
198 static const struct snd_kcontrol_new wm8510_mono_mixer_controls
[] = {
199 SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX
, 1, 1, 0),
200 SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX
, 2, 1, 0),
201 SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX
, 0, 1, 0),
204 static const struct snd_kcontrol_new wm8510_boost_controls
[] = {
205 SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA
, 6, 1, 1),
206 SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST
, 0, 7, 0),
207 SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST
, 4, 7, 0),
210 static const struct snd_kcontrol_new wm8510_micpga_controls
[] = {
211 SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT
, 0, 1, 0),
212 SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT
, 1, 1, 0),
213 SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT
, 2, 1, 0),
216 static const struct snd_soc_dapm_widget wm8510_dapm_widgets
[] = {
217 SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3
, 2, 0,
218 &wm8510_speaker_mixer_controls
[0],
219 ARRAY_SIZE(wm8510_speaker_mixer_controls
)),
220 SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3
, 3, 0,
221 &wm8510_mono_mixer_controls
[0],
222 ARRAY_SIZE(wm8510_mono_mixer_controls
)),
223 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3
, 0, 0),
224 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2
, 0, 0),
225 SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1
, 6, 0, NULL
, 0),
226 SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3
, 5, 0, NULL
, 0),
227 SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3
, 6, 0, NULL
, 0),
228 SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3
, 7, 0, NULL
, 0),
230 SND_SOC_DAPM_MIXER("Mic PGA", WM8510_POWER2
, 2, 0,
231 &wm8510_micpga_controls
[0],
232 ARRAY_SIZE(wm8510_micpga_controls
)),
233 SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2
, 4, 0,
234 &wm8510_boost_controls
[0],
235 ARRAY_SIZE(wm8510_boost_controls
)),
237 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_POWER1
, 4, 0),
239 SND_SOC_DAPM_INPUT("MICN"),
240 SND_SOC_DAPM_INPUT("MICP"),
241 SND_SOC_DAPM_INPUT("AUX"),
242 SND_SOC_DAPM_OUTPUT("MONOOUT"),
243 SND_SOC_DAPM_OUTPUT("SPKOUTP"),
244 SND_SOC_DAPM_OUTPUT("SPKOUTN"),
247 static const struct snd_soc_dapm_route audio_map
[] = {
248 /* Mono output mixer */
249 {"Mono Mixer", "PCM Playback Switch", "DAC"},
250 {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
251 {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
253 /* Speaker output mixer */
254 {"Speaker Mixer", "PCM Playback Switch", "DAC"},
255 {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
256 {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
259 {"Mono Out", NULL
, "Mono Mixer"},
260 {"MONOOUT", NULL
, "Mono Out"},
261 {"SpkN Out", NULL
, "Speaker Mixer"},
262 {"SpkP Out", NULL
, "Speaker Mixer"},
263 {"SPKOUTN", NULL
, "SpkN Out"},
264 {"SPKOUTP", NULL
, "SpkP Out"},
267 {"Mic PGA", "MICN Switch", "MICN"},
268 {"Mic PGA", "MICP Switch", "MICP"},
269 { "Mic PGA", "AUX Switch", "Aux Input" },
272 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
273 {"Boost Mixer", "Mic Volume", "MICP"},
274 {"Boost Mixer", "Aux Volume", "Aux Input"},
276 {"ADC", NULL
, "Boost Mixer"},
279 static int wm8510_add_widgets(struct snd_soc_codec
*codec
)
281 snd_soc_dapm_new_controls(codec
, wm8510_dapm_widgets
,
282 ARRAY_SIZE(wm8510_dapm_widgets
));
284 snd_soc_dapm_add_routes(codec
, audio_map
, ARRAY_SIZE(audio_map
));
286 snd_soc_dapm_new_widgets(codec
);
291 unsigned int pre_div
:4; /* prescale - 1 */
296 static struct pll_ pll_div
;
298 /* The size in bits of the pll divide multiplied by 10
299 * to allow rounding later */
300 #define FIXED_PLL_SIZE ((1 << 24) * 10)
302 static void pll_factors(unsigned int target
, unsigned int source
)
304 unsigned long long Kpart
;
305 unsigned int K
, Ndiv
, Nmod
;
307 Ndiv
= target
/ source
;
311 Ndiv
= target
/ source
;
315 if ((Ndiv
< 6) || (Ndiv
> 12))
317 "WM8510 N value %d outwith recommended range!d\n",
321 Nmod
= target
% source
;
322 Kpart
= FIXED_PLL_SIZE
* (long long)Nmod
;
324 do_div(Kpart
, source
);
326 K
= Kpart
& 0xFFFFFFFF;
328 /* Check if we need to round */
332 /* Move down to proper range now rounding is done */
338 static int wm8510_set_dai_pll(struct snd_soc_dai
*codec_dai
,
339 int pll_id
, unsigned int freq_in
, unsigned int freq_out
)
341 struct snd_soc_codec
*codec
= codec_dai
->codec
;
344 if (freq_in
== 0 || freq_out
== 0) {
345 /* Clock CODEC directly from MCLK */
346 reg
= wm8510_read_reg_cache(codec
, WM8510_CLOCK
);
347 wm8510_write(codec
, WM8510_CLOCK
, reg
& 0x0ff);
350 reg
= wm8510_read_reg_cache(codec
, WM8510_POWER1
);
351 wm8510_write(codec
, WM8510_POWER1
, reg
& 0x1df);
355 pll_factors(freq_out
*8, freq_in
);
357 wm8510_write(codec
, WM8510_PLLN
, (pll_div
.pre_div
<< 4) | pll_div
.n
);
358 wm8510_write(codec
, WM8510_PLLK1
, pll_div
.k
>> 18);
359 wm8510_write(codec
, WM8510_PLLK2
, (pll_div
.k
>> 9) & 0x1ff);
360 wm8510_write(codec
, WM8510_PLLK3
, pll_div
.k
& 0x1ff);
361 reg
= wm8510_read_reg_cache(codec
, WM8510_POWER1
);
362 wm8510_write(codec
, WM8510_POWER1
, reg
| 0x020);
364 /* Run CODEC from PLL instead of MCLK */
365 reg
= wm8510_read_reg_cache(codec
, WM8510_CLOCK
);
366 wm8510_write(codec
, WM8510_CLOCK
, reg
| 0x100);
372 * Configure WM8510 clock dividers.
374 static int wm8510_set_dai_clkdiv(struct snd_soc_dai
*codec_dai
,
377 struct snd_soc_codec
*codec
= codec_dai
->codec
;
381 case WM8510_OPCLKDIV
:
382 reg
= wm8510_read_reg_cache(codec
, WM8510_GPIO
) & 0x1cf;
383 wm8510_write(codec
, WM8510_GPIO
, reg
| div
);
386 reg
= wm8510_read_reg_cache(codec
, WM8510_CLOCK
) & 0x1f;
387 wm8510_write(codec
, WM8510_CLOCK
, reg
| div
);
390 reg
= wm8510_read_reg_cache(codec
, WM8510_ADC
) & 0x1f7;
391 wm8510_write(codec
, WM8510_ADC
, reg
| div
);
394 reg
= wm8510_read_reg_cache(codec
, WM8510_DAC
) & 0x1f7;
395 wm8510_write(codec
, WM8510_DAC
, reg
| div
);
398 reg
= wm8510_read_reg_cache(codec
, WM8510_CLOCK
) & 0x1e3;
399 wm8510_write(codec
, WM8510_CLOCK
, reg
| div
);
408 static int wm8510_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
411 struct snd_soc_codec
*codec
= codec_dai
->codec
;
413 u16 clk
= wm8510_read_reg_cache(codec
, WM8510_CLOCK
) & 0x1fe;
415 /* set master/slave audio interface */
416 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
417 case SND_SOC_DAIFMT_CBM_CFM
:
420 case SND_SOC_DAIFMT_CBS_CFS
:
426 /* interface format */
427 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
428 case SND_SOC_DAIFMT_I2S
:
431 case SND_SOC_DAIFMT_RIGHT_J
:
433 case SND_SOC_DAIFMT_LEFT_J
:
436 case SND_SOC_DAIFMT_DSP_A
:
443 /* clock inversion */
444 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
445 case SND_SOC_DAIFMT_NB_NF
:
447 case SND_SOC_DAIFMT_IB_IF
:
450 case SND_SOC_DAIFMT_IB_NF
:
453 case SND_SOC_DAIFMT_NB_IF
:
460 wm8510_write(codec
, WM8510_IFACE
, iface
);
461 wm8510_write(codec
, WM8510_CLOCK
, clk
);
465 static int wm8510_pcm_hw_params(struct snd_pcm_substream
*substream
,
466 struct snd_pcm_hw_params
*params
)
468 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
469 struct snd_soc_device
*socdev
= rtd
->socdev
;
470 struct snd_soc_codec
*codec
= socdev
->codec
;
471 u16 iface
= wm8510_read_reg_cache(codec
, WM8510_IFACE
) & 0x19f;
472 u16 adn
= wm8510_read_reg_cache(codec
, WM8510_ADD
) & 0x1f1;
475 switch (params_format(params
)) {
476 case SNDRV_PCM_FORMAT_S16_LE
:
478 case SNDRV_PCM_FORMAT_S20_3LE
:
481 case SNDRV_PCM_FORMAT_S24_LE
:
484 case SNDRV_PCM_FORMAT_S32_LE
:
489 /* filter coefficient */
490 switch (params_rate(params
)) {
491 case SNDRV_PCM_RATE_8000
:
494 case SNDRV_PCM_RATE_11025
:
497 case SNDRV_PCM_RATE_16000
:
500 case SNDRV_PCM_RATE_22050
:
503 case SNDRV_PCM_RATE_32000
:
506 case SNDRV_PCM_RATE_44100
:
507 case SNDRV_PCM_RATE_48000
:
511 wm8510_write(codec
, WM8510_IFACE
, iface
);
512 wm8510_write(codec
, WM8510_ADD
, adn
);
516 static int wm8510_mute(struct snd_soc_dai
*dai
, int mute
)
518 struct snd_soc_codec
*codec
= dai
->codec
;
519 u16 mute_reg
= wm8510_read_reg_cache(codec
, WM8510_DAC
) & 0xffbf;
522 wm8510_write(codec
, WM8510_DAC
, mute_reg
| 0x40);
524 wm8510_write(codec
, WM8510_DAC
, mute_reg
);
528 /* liam need to make this lower power with dapm */
529 static int wm8510_set_bias_level(struct snd_soc_codec
*codec
,
530 enum snd_soc_bias_level level
)
532 u16 power1
= wm8510_read_reg_cache(codec
, WM8510_POWER1
) & ~0x3;
535 case SND_SOC_BIAS_ON
:
536 case SND_SOC_BIAS_PREPARE
:
537 power1
|= 0x1; /* VMID 50k */
538 wm8510_write(codec
, WM8510_POWER1
, power1
);
541 case SND_SOC_BIAS_STANDBY
:
542 power1
|= WM8510_POWER1_BIASEN
| WM8510_POWER1_BUFIOEN
;
544 if (codec
->bias_level
== SND_SOC_BIAS_OFF
) {
545 /* Initial cap charge at VMID 5k */
546 wm8510_write(codec
, WM8510_POWER1
, power1
| 0x3);
550 power1
|= 0x2; /* VMID 500k */
551 wm8510_write(codec
, WM8510_POWER1
, power1
);
554 case SND_SOC_BIAS_OFF
:
555 wm8510_write(codec
, WM8510_POWER1
, 0);
556 wm8510_write(codec
, WM8510_POWER2
, 0);
557 wm8510_write(codec
, WM8510_POWER3
, 0);
561 codec
->bias_level
= level
;
565 #define WM8510_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
566 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
567 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
569 #define WM8510_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
570 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
572 struct snd_soc_dai wm8510_dai
= {
573 .name
= "WM8510 HiFi",
575 .stream_name
= "Playback",
578 .rates
= WM8510_RATES
,
579 .formats
= WM8510_FORMATS
,},
581 .stream_name
= "Capture",
584 .rates
= WM8510_RATES
,
585 .formats
= WM8510_FORMATS
,},
587 .hw_params
= wm8510_pcm_hw_params
,
590 .digital_mute
= wm8510_mute
,
591 .set_fmt
= wm8510_set_dai_fmt
,
592 .set_clkdiv
= wm8510_set_dai_clkdiv
,
593 .set_pll
= wm8510_set_dai_pll
,
596 EXPORT_SYMBOL_GPL(wm8510_dai
);
598 static int wm8510_suspend(struct platform_device
*pdev
, pm_message_t state
)
600 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
601 struct snd_soc_codec
*codec
= socdev
->codec
;
603 wm8510_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
607 static int wm8510_resume(struct platform_device
*pdev
)
609 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
610 struct snd_soc_codec
*codec
= socdev
->codec
;
613 u16
*cache
= codec
->reg_cache
;
615 /* Sync reg_cache with the hardware */
616 for (i
= 0; i
< ARRAY_SIZE(wm8510_reg
); i
++) {
617 data
[0] = (i
<< 1) | ((cache
[i
] >> 8) & 0x0001);
618 data
[1] = cache
[i
] & 0x00ff;
619 codec
->hw_write(codec
->control_data
, data
, 2);
621 wm8510_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
622 wm8510_set_bias_level(codec
, codec
->suspend_bias_level
);
627 * initialise the WM8510 driver
628 * register the mixer and dsp interfaces with the kernel
630 static int wm8510_init(struct snd_soc_device
*socdev
)
632 struct snd_soc_codec
*codec
= socdev
->codec
;
635 codec
->name
= "WM8510";
636 codec
->owner
= THIS_MODULE
;
637 codec
->read
= wm8510_read_reg_cache
;
638 codec
->write
= wm8510_write
;
639 codec
->set_bias_level
= wm8510_set_bias_level
;
640 codec
->dai
= &wm8510_dai
;
642 codec
->reg_cache_size
= ARRAY_SIZE(wm8510_reg
);
643 codec
->reg_cache
= kmemdup(wm8510_reg
, sizeof(wm8510_reg
), GFP_KERNEL
);
645 if (codec
->reg_cache
== NULL
)
651 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
653 printk(KERN_ERR
"wm8510: failed to create pcms\n");
657 /* power on device */
658 codec
->bias_level
= SND_SOC_BIAS_OFF
;
659 wm8510_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
660 wm8510_add_controls(codec
);
661 wm8510_add_widgets(codec
);
662 ret
= snd_soc_register_card(socdev
);
664 printk(KERN_ERR
"wm8510: failed to register card\n");
670 snd_soc_free_pcms(socdev
);
671 snd_soc_dapm_free(socdev
);
673 kfree(codec
->reg_cache
);
677 static struct snd_soc_device
*wm8510_socdev
;
679 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
682 * WM8510 2 wire address is 0x1a
685 static int wm8510_i2c_probe(struct i2c_client
*i2c
,
686 const struct i2c_device_id
*id
)
688 struct snd_soc_device
*socdev
= wm8510_socdev
;
689 struct snd_soc_codec
*codec
= socdev
->codec
;
692 i2c_set_clientdata(i2c
, codec
);
693 codec
->control_data
= i2c
;
695 ret
= wm8510_init(socdev
);
697 pr_err("failed to initialise WM8510\n");
702 static int wm8510_i2c_remove(struct i2c_client
*client
)
704 struct snd_soc_codec
*codec
= i2c_get_clientdata(client
);
705 kfree(codec
->reg_cache
);
709 static const struct i2c_device_id wm8510_i2c_id
[] = {
713 MODULE_DEVICE_TABLE(i2c
, wm8510_i2c_id
);
715 static struct i2c_driver wm8510_i2c_driver
= {
717 .name
= "WM8510 I2C Codec",
718 .owner
= THIS_MODULE
,
720 .probe
= wm8510_i2c_probe
,
721 .remove
= wm8510_i2c_remove
,
722 .id_table
= wm8510_i2c_id
,
725 static int wm8510_add_i2c_device(struct platform_device
*pdev
,
726 const struct wm8510_setup_data
*setup
)
728 struct i2c_board_info info
;
729 struct i2c_adapter
*adapter
;
730 struct i2c_client
*client
;
733 ret
= i2c_add_driver(&wm8510_i2c_driver
);
735 dev_err(&pdev
->dev
, "can't add i2c driver\n");
739 memset(&info
, 0, sizeof(struct i2c_board_info
));
740 info
.addr
= setup
->i2c_address
;
741 strlcpy(info
.type
, "wm8510", I2C_NAME_SIZE
);
743 adapter
= i2c_get_adapter(setup
->i2c_bus
);
745 dev_err(&pdev
->dev
, "can't get i2c adapter %d\n",
750 client
= i2c_new_device(adapter
, &info
);
751 i2c_put_adapter(adapter
);
753 dev_err(&pdev
->dev
, "can't add i2c device at 0x%x\n",
754 (unsigned int)info
.addr
);
761 i2c_del_driver(&wm8510_i2c_driver
);
766 #if defined(CONFIG_SPI_MASTER)
767 static int __devinit
wm8510_spi_probe(struct spi_device
*spi
)
769 struct snd_soc_device
*socdev
= wm8510_socdev
;
770 struct snd_soc_codec
*codec
= socdev
->codec
;
773 codec
->control_data
= spi
;
775 ret
= wm8510_init(socdev
);
777 dev_err(&spi
->dev
, "failed to initialise WM8510\n");
782 static int __devexit
wm8510_spi_remove(struct spi_device
*spi
)
787 static struct spi_driver wm8510_spi_driver
= {
790 .bus
= &spi_bus_type
,
791 .owner
= THIS_MODULE
,
793 .probe
= wm8510_spi_probe
,
794 .remove
= __devexit_p(wm8510_spi_remove
),
797 static int wm8510_spi_write(struct spi_device
*spi
, const char *data
, int len
)
799 struct spi_transfer t
;
800 struct spi_message m
;
809 spi_message_init(&m
);
810 memset(&t
, 0, (sizeof t
));
815 spi_message_add_tail(&t
, &m
);
820 #endif /* CONFIG_SPI_MASTER */
822 static int wm8510_probe(struct platform_device
*pdev
)
824 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
825 struct wm8510_setup_data
*setup
;
826 struct snd_soc_codec
*codec
;
829 pr_info("WM8510 Audio Codec %s", WM8510_VERSION
);
831 setup
= socdev
->codec_data
;
832 codec
= kzalloc(sizeof(struct snd_soc_codec
), GFP_KERNEL
);
836 socdev
->codec
= codec
;
837 mutex_init(&codec
->mutex
);
838 INIT_LIST_HEAD(&codec
->dapm_widgets
);
839 INIT_LIST_HEAD(&codec
->dapm_paths
);
841 wm8510_socdev
= socdev
;
842 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
843 if (setup
->i2c_address
) {
844 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
845 ret
= wm8510_add_i2c_device(pdev
, setup
);
848 #if defined(CONFIG_SPI_MASTER)
850 codec
->hw_write
= (hw_write_t
)wm8510_spi_write
;
851 ret
= spi_register_driver(&wm8510_spi_driver
);
853 printk(KERN_ERR
"can't add spi driver");
862 /* power down chip */
863 static int wm8510_remove(struct platform_device
*pdev
)
865 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
866 struct snd_soc_codec
*codec
= socdev
->codec
;
868 if (codec
->control_data
)
869 wm8510_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
871 snd_soc_free_pcms(socdev
);
872 snd_soc_dapm_free(socdev
);
873 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
874 i2c_unregister_device(codec
->control_data
);
875 i2c_del_driver(&wm8510_i2c_driver
);
877 #if defined(CONFIG_SPI_MASTER)
878 spi_unregister_driver(&wm8510_spi_driver
);
885 struct snd_soc_codec_device soc_codec_dev_wm8510
= {
886 .probe
= wm8510_probe
,
887 .remove
= wm8510_remove
,
888 .suspend
= wm8510_suspend
,
889 .resume
= wm8510_resume
,
891 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8510
);
893 MODULE_DESCRIPTION("ASoC WM8510 driver");
894 MODULE_AUTHOR("Liam Girdwood");
895 MODULE_LICENSE("GPL");