Full support for Ginger Console
[linux-ginger.git] / sound / soc / codecs / wm8510.c
blob060d5d06ba955019baf1209595a72734d6866657
1 /*
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>
18 #include <linux/pm.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>
29 #include "wm8510.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,
55 0x0001,
58 #define WM8510_POWER1_BIASEN 0x08
59 #define WM8510_POWER1_BUFIOEN 0x10
61 #define wm8510_reset(c) snd_soc_write(c, WM8510_RESET, 0)
63 static const char *wm8510_companding[] = { "Off", "NC", "u-law", "A-law" };
64 static const char *wm8510_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
65 static const char *wm8510_alc[] = { "ALC", "Limiter" };
67 static const struct soc_enum wm8510_enum[] = {
68 SOC_ENUM_SINGLE(WM8510_COMP, 1, 4, wm8510_companding), /* adc */
69 SOC_ENUM_SINGLE(WM8510_COMP, 3, 4, wm8510_companding), /* dac */
70 SOC_ENUM_SINGLE(WM8510_DAC, 4, 4, wm8510_deemp),
71 SOC_ENUM_SINGLE(WM8510_ALC3, 8, 2, wm8510_alc),
74 static const struct snd_kcontrol_new wm8510_snd_controls[] = {
76 SOC_SINGLE("Digital Loopback Switch", WM8510_COMP, 0, 1, 0),
78 SOC_ENUM("DAC Companding", wm8510_enum[1]),
79 SOC_ENUM("ADC Companding", wm8510_enum[0]),
81 SOC_ENUM("Playback De-emphasis", wm8510_enum[2]),
82 SOC_SINGLE("DAC Inversion Switch", WM8510_DAC, 0, 1, 0),
84 SOC_SINGLE("Master Playback Volume", WM8510_DACVOL, 0, 127, 0),
86 SOC_SINGLE("High Pass Filter Switch", WM8510_ADC, 8, 1, 0),
87 SOC_SINGLE("High Pass Cut Off", WM8510_ADC, 4, 7, 0),
88 SOC_SINGLE("ADC Inversion Switch", WM8510_COMP, 0, 1, 0),
90 SOC_SINGLE("Capture Volume", WM8510_ADCVOL, 0, 127, 0),
92 SOC_SINGLE("DAC Playback Limiter Switch", WM8510_DACLIM1, 8, 1, 0),
93 SOC_SINGLE("DAC Playback Limiter Decay", WM8510_DACLIM1, 4, 15, 0),
94 SOC_SINGLE("DAC Playback Limiter Attack", WM8510_DACLIM1, 0, 15, 0),
96 SOC_SINGLE("DAC Playback Limiter Threshold", WM8510_DACLIM2, 4, 7, 0),
97 SOC_SINGLE("DAC Playback Limiter Boost", WM8510_DACLIM2, 0, 15, 0),
99 SOC_SINGLE("ALC Enable Switch", WM8510_ALC1, 8, 1, 0),
100 SOC_SINGLE("ALC Capture Max Gain", WM8510_ALC1, 3, 7, 0),
101 SOC_SINGLE("ALC Capture Min Gain", WM8510_ALC1, 0, 7, 0),
103 SOC_SINGLE("ALC Capture ZC Switch", WM8510_ALC2, 8, 1, 0),
104 SOC_SINGLE("ALC Capture Hold", WM8510_ALC2, 4, 7, 0),
105 SOC_SINGLE("ALC Capture Target", WM8510_ALC2, 0, 15, 0),
107 SOC_ENUM("ALC Capture Mode", wm8510_enum[3]),
108 SOC_SINGLE("ALC Capture Decay", WM8510_ALC3, 4, 15, 0),
109 SOC_SINGLE("ALC Capture Attack", WM8510_ALC3, 0, 15, 0),
111 SOC_SINGLE("ALC Capture Noise Gate Switch", WM8510_NGATE, 3, 1, 0),
112 SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8510_NGATE, 0, 7, 0),
114 SOC_SINGLE("Capture PGA ZC Switch", WM8510_INPPGA, 7, 1, 0),
115 SOC_SINGLE("Capture PGA Volume", WM8510_INPPGA, 0, 63, 0),
117 SOC_SINGLE("Speaker Playback ZC Switch", WM8510_SPKVOL, 7, 1, 0),
118 SOC_SINGLE("Speaker Playback Switch", WM8510_SPKVOL, 6, 1, 1),
119 SOC_SINGLE("Speaker Playback Volume", WM8510_SPKVOL, 0, 63, 0),
120 SOC_SINGLE("Speaker Boost", WM8510_OUTPUT, 2, 1, 0),
122 SOC_SINGLE("Capture Boost(+20dB)", WM8510_ADCBOOST, 8, 1, 0),
123 SOC_SINGLE("Mono Playback Switch", WM8510_MONOMIX, 6, 1, 1),
126 /* Speaker Output Mixer */
127 static const struct snd_kcontrol_new wm8510_speaker_mixer_controls[] = {
128 SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_SPKMIX, 1, 1, 0),
129 SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_SPKMIX, 5, 1, 0),
130 SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_SPKMIX, 0, 1, 0),
133 /* Mono Output Mixer */
134 static const struct snd_kcontrol_new wm8510_mono_mixer_controls[] = {
135 SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX, 1, 1, 0),
136 SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX, 2, 1, 0),
137 SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX, 0, 1, 0),
140 static const struct snd_kcontrol_new wm8510_boost_controls[] = {
141 SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA, 6, 1, 1),
142 SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST, 0, 7, 0),
143 SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST, 4, 7, 0),
146 static const struct snd_kcontrol_new wm8510_micpga_controls[] = {
147 SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT, 0, 1, 0),
148 SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT, 1, 1, 0),
149 SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT, 2, 1, 0),
152 static const struct snd_soc_dapm_widget wm8510_dapm_widgets[] = {
153 SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3, 2, 0,
154 &wm8510_speaker_mixer_controls[0],
155 ARRAY_SIZE(wm8510_speaker_mixer_controls)),
156 SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3, 3, 0,
157 &wm8510_mono_mixer_controls[0],
158 ARRAY_SIZE(wm8510_mono_mixer_controls)),
159 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3, 0, 0),
160 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2, 0, 0),
161 SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1, 6, 0, NULL, 0),
162 SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3, 5, 0, NULL, 0),
163 SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3, 6, 0, NULL, 0),
164 SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3, 7, 0, NULL, 0),
166 SND_SOC_DAPM_MIXER("Mic PGA", WM8510_POWER2, 2, 0,
167 &wm8510_micpga_controls[0],
168 ARRAY_SIZE(wm8510_micpga_controls)),
169 SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2, 4, 0,
170 &wm8510_boost_controls[0],
171 ARRAY_SIZE(wm8510_boost_controls)),
173 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_POWER1, 4, 0),
175 SND_SOC_DAPM_INPUT("MICN"),
176 SND_SOC_DAPM_INPUT("MICP"),
177 SND_SOC_DAPM_INPUT("AUX"),
178 SND_SOC_DAPM_OUTPUT("MONOOUT"),
179 SND_SOC_DAPM_OUTPUT("SPKOUTP"),
180 SND_SOC_DAPM_OUTPUT("SPKOUTN"),
183 static const struct snd_soc_dapm_route audio_map[] = {
184 /* Mono output mixer */
185 {"Mono Mixer", "PCM Playback Switch", "DAC"},
186 {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
187 {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
189 /* Speaker output mixer */
190 {"Speaker Mixer", "PCM Playback Switch", "DAC"},
191 {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
192 {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
194 /* Outputs */
195 {"Mono Out", NULL, "Mono Mixer"},
196 {"MONOOUT", NULL, "Mono Out"},
197 {"SpkN Out", NULL, "Speaker Mixer"},
198 {"SpkP Out", NULL, "Speaker Mixer"},
199 {"SPKOUTN", NULL, "SpkN Out"},
200 {"SPKOUTP", NULL, "SpkP Out"},
202 /* Microphone PGA */
203 {"Mic PGA", "MICN Switch", "MICN"},
204 {"Mic PGA", "MICP Switch", "MICP"},
205 { "Mic PGA", "AUX Switch", "Aux Input" },
207 /* Boost Mixer */
208 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
209 {"Boost Mixer", "Mic Volume", "MICP"},
210 {"Boost Mixer", "Aux Volume", "Aux Input"},
212 {"ADC", NULL, "Boost Mixer"},
215 static int wm8510_add_widgets(struct snd_soc_codec *codec)
217 snd_soc_dapm_new_controls(codec, wm8510_dapm_widgets,
218 ARRAY_SIZE(wm8510_dapm_widgets));
220 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
222 snd_soc_dapm_new_widgets(codec);
223 return 0;
226 struct pll_ {
227 unsigned int pre_div:4; /* prescale - 1 */
228 unsigned int n:4;
229 unsigned int k;
232 static struct pll_ pll_div;
234 /* The size in bits of the pll divide multiplied by 10
235 * to allow rounding later */
236 #define FIXED_PLL_SIZE ((1 << 24) * 10)
238 static void pll_factors(unsigned int target, unsigned int source)
240 unsigned long long Kpart;
241 unsigned int K, Ndiv, Nmod;
243 Ndiv = target / source;
244 if (Ndiv < 6) {
245 source >>= 1;
246 pll_div.pre_div = 1;
247 Ndiv = target / source;
248 } else
249 pll_div.pre_div = 0;
251 if ((Ndiv < 6) || (Ndiv > 12))
252 printk(KERN_WARNING
253 "WM8510 N value %u outwith recommended range!d\n",
254 Ndiv);
256 pll_div.n = Ndiv;
257 Nmod = target % source;
258 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
260 do_div(Kpart, source);
262 K = Kpart & 0xFFFFFFFF;
264 /* Check if we need to round */
265 if ((K % 10) >= 5)
266 K += 5;
268 /* Move down to proper range now rounding is done */
269 K /= 10;
271 pll_div.k = K;
274 static int wm8510_set_dai_pll(struct snd_soc_dai *codec_dai,
275 int pll_id, unsigned int freq_in, unsigned int freq_out)
277 struct snd_soc_codec *codec = codec_dai->codec;
278 u16 reg;
280 if (freq_in == 0 || freq_out == 0) {
281 /* Clock CODEC directly from MCLK */
282 reg = snd_soc_read(codec, WM8510_CLOCK);
283 snd_soc_write(codec, WM8510_CLOCK, reg & 0x0ff);
285 /* Turn off PLL */
286 reg = snd_soc_read(codec, WM8510_POWER1);
287 snd_soc_write(codec, WM8510_POWER1, reg & 0x1df);
288 return 0;
291 pll_factors(freq_out*4, freq_in);
293 snd_soc_write(codec, WM8510_PLLN, (pll_div.pre_div << 4) | pll_div.n);
294 snd_soc_write(codec, WM8510_PLLK1, pll_div.k >> 18);
295 snd_soc_write(codec, WM8510_PLLK2, (pll_div.k >> 9) & 0x1ff);
296 snd_soc_write(codec, WM8510_PLLK3, pll_div.k & 0x1ff);
297 reg = snd_soc_read(codec, WM8510_POWER1);
298 snd_soc_write(codec, WM8510_POWER1, reg | 0x020);
300 /* Run CODEC from PLL instead of MCLK */
301 reg = snd_soc_read(codec, WM8510_CLOCK);
302 snd_soc_write(codec, WM8510_CLOCK, reg | 0x100);
304 return 0;
308 * Configure WM8510 clock dividers.
310 static int wm8510_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
311 int div_id, int div)
313 struct snd_soc_codec *codec = codec_dai->codec;
314 u16 reg;
316 switch (div_id) {
317 case WM8510_OPCLKDIV:
318 reg = snd_soc_read(codec, WM8510_GPIO) & 0x1cf;
319 snd_soc_write(codec, WM8510_GPIO, reg | div);
320 break;
321 case WM8510_MCLKDIV:
322 reg = snd_soc_read(codec, WM8510_CLOCK) & 0x11f;
323 snd_soc_write(codec, WM8510_CLOCK, reg | div);
324 break;
325 case WM8510_ADCCLK:
326 reg = snd_soc_read(codec, WM8510_ADC) & 0x1f7;
327 snd_soc_write(codec, WM8510_ADC, reg | div);
328 break;
329 case WM8510_DACCLK:
330 reg = snd_soc_read(codec, WM8510_DAC) & 0x1f7;
331 snd_soc_write(codec, WM8510_DAC, reg | div);
332 break;
333 case WM8510_BCLKDIV:
334 reg = snd_soc_read(codec, WM8510_CLOCK) & 0x1e3;
335 snd_soc_write(codec, WM8510_CLOCK, reg | div);
336 break;
337 default:
338 return -EINVAL;
341 return 0;
344 static int wm8510_set_dai_fmt(struct snd_soc_dai *codec_dai,
345 unsigned int fmt)
347 struct snd_soc_codec *codec = codec_dai->codec;
348 u16 iface = 0;
349 u16 clk = snd_soc_read(codec, WM8510_CLOCK) & 0x1fe;
351 /* set master/slave audio interface */
352 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
353 case SND_SOC_DAIFMT_CBM_CFM:
354 clk |= 0x0001;
355 break;
356 case SND_SOC_DAIFMT_CBS_CFS:
357 break;
358 default:
359 return -EINVAL;
362 /* interface format */
363 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
364 case SND_SOC_DAIFMT_I2S:
365 iface |= 0x0010;
366 break;
367 case SND_SOC_DAIFMT_RIGHT_J:
368 break;
369 case SND_SOC_DAIFMT_LEFT_J:
370 iface |= 0x0008;
371 break;
372 case SND_SOC_DAIFMT_DSP_A:
373 iface |= 0x00018;
374 break;
375 default:
376 return -EINVAL;
379 /* clock inversion */
380 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
381 case SND_SOC_DAIFMT_NB_NF:
382 break;
383 case SND_SOC_DAIFMT_IB_IF:
384 iface |= 0x0180;
385 break;
386 case SND_SOC_DAIFMT_IB_NF:
387 iface |= 0x0100;
388 break;
389 case SND_SOC_DAIFMT_NB_IF:
390 iface |= 0x0080;
391 break;
392 default:
393 return -EINVAL;
396 snd_soc_write(codec, WM8510_IFACE, iface);
397 snd_soc_write(codec, WM8510_CLOCK, clk);
398 return 0;
401 static int wm8510_pcm_hw_params(struct snd_pcm_substream *substream,
402 struct snd_pcm_hw_params *params,
403 struct snd_soc_dai *dai)
405 struct snd_soc_pcm_runtime *rtd = substream->private_data;
406 struct snd_soc_device *socdev = rtd->socdev;
407 struct snd_soc_codec *codec = socdev->card->codec;
408 u16 iface = snd_soc_read(codec, WM8510_IFACE) & 0x19f;
409 u16 adn = snd_soc_read(codec, WM8510_ADD) & 0x1f1;
411 /* bit size */
412 switch (params_format(params)) {
413 case SNDRV_PCM_FORMAT_S16_LE:
414 break;
415 case SNDRV_PCM_FORMAT_S20_3LE:
416 iface |= 0x0020;
417 break;
418 case SNDRV_PCM_FORMAT_S24_LE:
419 iface |= 0x0040;
420 break;
421 case SNDRV_PCM_FORMAT_S32_LE:
422 iface |= 0x0060;
423 break;
426 /* filter coefficient */
427 switch (params_rate(params)) {
428 case SNDRV_PCM_RATE_8000:
429 adn |= 0x5 << 1;
430 break;
431 case SNDRV_PCM_RATE_11025:
432 adn |= 0x4 << 1;
433 break;
434 case SNDRV_PCM_RATE_16000:
435 adn |= 0x3 << 1;
436 break;
437 case SNDRV_PCM_RATE_22050:
438 adn |= 0x2 << 1;
439 break;
440 case SNDRV_PCM_RATE_32000:
441 adn |= 0x1 << 1;
442 break;
443 case SNDRV_PCM_RATE_44100:
444 case SNDRV_PCM_RATE_48000:
445 break;
448 snd_soc_write(codec, WM8510_IFACE, iface);
449 snd_soc_write(codec, WM8510_ADD, adn);
450 return 0;
453 static int wm8510_mute(struct snd_soc_dai *dai, int mute)
455 struct snd_soc_codec *codec = dai->codec;
456 u16 mute_reg = snd_soc_read(codec, WM8510_DAC) & 0xffbf;
458 if (mute)
459 snd_soc_write(codec, WM8510_DAC, mute_reg | 0x40);
460 else
461 snd_soc_write(codec, WM8510_DAC, mute_reg);
462 return 0;
465 /* liam need to make this lower power with dapm */
466 static int wm8510_set_bias_level(struct snd_soc_codec *codec,
467 enum snd_soc_bias_level level)
469 u16 power1 = snd_soc_read(codec, WM8510_POWER1) & ~0x3;
471 switch (level) {
472 case SND_SOC_BIAS_ON:
473 case SND_SOC_BIAS_PREPARE:
474 power1 |= 0x1; /* VMID 50k */
475 snd_soc_write(codec, WM8510_POWER1, power1);
476 break;
478 case SND_SOC_BIAS_STANDBY:
479 power1 |= WM8510_POWER1_BIASEN | WM8510_POWER1_BUFIOEN;
481 if (codec->bias_level == SND_SOC_BIAS_OFF) {
482 /* Initial cap charge at VMID 5k */
483 snd_soc_write(codec, WM8510_POWER1, power1 | 0x3);
484 mdelay(100);
487 power1 |= 0x2; /* VMID 500k */
488 snd_soc_write(codec, WM8510_POWER1, power1);
489 break;
491 case SND_SOC_BIAS_OFF:
492 snd_soc_write(codec, WM8510_POWER1, 0);
493 snd_soc_write(codec, WM8510_POWER2, 0);
494 snd_soc_write(codec, WM8510_POWER3, 0);
495 break;
498 codec->bias_level = level;
499 return 0;
502 #define WM8510_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
503 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
504 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
506 #define WM8510_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
507 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
509 static struct snd_soc_dai_ops wm8510_dai_ops = {
510 .hw_params = wm8510_pcm_hw_params,
511 .digital_mute = wm8510_mute,
512 .set_fmt = wm8510_set_dai_fmt,
513 .set_clkdiv = wm8510_set_dai_clkdiv,
514 .set_pll = wm8510_set_dai_pll,
517 struct snd_soc_dai wm8510_dai = {
518 .name = "WM8510 HiFi",
519 .playback = {
520 .stream_name = "Playback",
521 .channels_min = 2,
522 .channels_max = 2,
523 .rates = WM8510_RATES,
524 .formats = WM8510_FORMATS,},
525 .capture = {
526 .stream_name = "Capture",
527 .channels_min = 2,
528 .channels_max = 2,
529 .rates = WM8510_RATES,
530 .formats = WM8510_FORMATS,},
531 .ops = &wm8510_dai_ops,
532 .symmetric_rates = 1,
534 EXPORT_SYMBOL_GPL(wm8510_dai);
536 static int wm8510_suspend(struct platform_device *pdev, pm_message_t state)
538 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
539 struct snd_soc_codec *codec = socdev->card->codec;
541 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
542 return 0;
545 static int wm8510_resume(struct platform_device *pdev)
547 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
548 struct snd_soc_codec *codec = socdev->card->codec;
549 int i;
550 u8 data[2];
551 u16 *cache = codec->reg_cache;
553 /* Sync reg_cache with the hardware */
554 for (i = 0; i < ARRAY_SIZE(wm8510_reg); i++) {
555 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
556 data[1] = cache[i] & 0x00ff;
557 codec->hw_write(codec->control_data, data, 2);
559 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
560 wm8510_set_bias_level(codec, codec->suspend_bias_level);
561 return 0;
565 * initialise the WM8510 driver
566 * register the mixer and dsp interfaces with the kernel
568 static int wm8510_init(struct snd_soc_device *socdev,
569 enum snd_soc_control_type control)
571 struct snd_soc_codec *codec = socdev->card->codec;
572 int ret = 0;
574 codec->name = "WM8510";
575 codec->owner = THIS_MODULE;
576 codec->set_bias_level = wm8510_set_bias_level;
577 codec->dai = &wm8510_dai;
578 codec->num_dai = 1;
579 codec->reg_cache_size = ARRAY_SIZE(wm8510_reg);
580 codec->reg_cache = kmemdup(wm8510_reg, sizeof(wm8510_reg), GFP_KERNEL);
582 if (codec->reg_cache == NULL)
583 return -ENOMEM;
585 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
586 if (ret < 0) {
587 printk(KERN_ERR "wm8510: failed to set cache I/O: %d\n",
588 ret);
589 goto err;
592 wm8510_reset(codec);
594 /* register pcms */
595 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
596 if (ret < 0) {
597 printk(KERN_ERR "wm8510: failed to create pcms\n");
598 goto err;
601 /* power on device */
602 codec->bias_level = SND_SOC_BIAS_OFF;
603 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
604 snd_soc_add_controls(codec, wm8510_snd_controls,
605 ARRAY_SIZE(wm8510_snd_controls));
606 wm8510_add_widgets(codec);
607 ret = snd_soc_init_card(socdev);
608 if (ret < 0) {
609 printk(KERN_ERR "wm8510: failed to register card\n");
610 goto card_err;
612 return ret;
614 card_err:
615 snd_soc_free_pcms(socdev);
616 snd_soc_dapm_free(socdev);
617 err:
618 kfree(codec->reg_cache);
619 return ret;
622 static struct snd_soc_device *wm8510_socdev;
624 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
627 * WM8510 2 wire address is 0x1a
630 static int wm8510_i2c_probe(struct i2c_client *i2c,
631 const struct i2c_device_id *id)
633 struct snd_soc_device *socdev = wm8510_socdev;
634 struct snd_soc_codec *codec = socdev->card->codec;
635 int ret;
637 i2c_set_clientdata(i2c, codec);
638 codec->control_data = i2c;
640 ret = wm8510_init(socdev, SND_SOC_I2C);
641 if (ret < 0)
642 pr_err("failed to initialise WM8510\n");
644 return ret;
647 static int wm8510_i2c_remove(struct i2c_client *client)
649 struct snd_soc_codec *codec = i2c_get_clientdata(client);
650 kfree(codec->reg_cache);
651 return 0;
654 static const struct i2c_device_id wm8510_i2c_id[] = {
655 { "wm8510", 0 },
658 MODULE_DEVICE_TABLE(i2c, wm8510_i2c_id);
660 static struct i2c_driver wm8510_i2c_driver = {
661 .driver = {
662 .name = "WM8510 I2C Codec",
663 .owner = THIS_MODULE,
665 .probe = wm8510_i2c_probe,
666 .remove = wm8510_i2c_remove,
667 .id_table = wm8510_i2c_id,
670 static int wm8510_add_i2c_device(struct platform_device *pdev,
671 const struct wm8510_setup_data *setup)
673 struct i2c_board_info info;
674 struct i2c_adapter *adapter;
675 struct i2c_client *client;
676 int ret;
678 ret = i2c_add_driver(&wm8510_i2c_driver);
679 if (ret != 0) {
680 dev_err(&pdev->dev, "can't add i2c driver\n");
681 return ret;
684 memset(&info, 0, sizeof(struct i2c_board_info));
685 info.addr = setup->i2c_address;
686 strlcpy(info.type, "wm8510", I2C_NAME_SIZE);
688 adapter = i2c_get_adapter(setup->i2c_bus);
689 if (!adapter) {
690 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
691 setup->i2c_bus);
692 goto err_driver;
695 client = i2c_new_device(adapter, &info);
696 i2c_put_adapter(adapter);
697 if (!client) {
698 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
699 (unsigned int)info.addr);
700 goto err_driver;
703 return 0;
705 err_driver:
706 i2c_del_driver(&wm8510_i2c_driver);
707 return -ENODEV;
709 #endif
711 #if defined(CONFIG_SPI_MASTER)
712 static int __devinit wm8510_spi_probe(struct spi_device *spi)
714 struct snd_soc_device *socdev = wm8510_socdev;
715 struct snd_soc_codec *codec = socdev->card->codec;
716 int ret;
718 codec->control_data = spi;
720 ret = wm8510_init(socdev, SND_SOC_SPI);
721 if (ret < 0)
722 dev_err(&spi->dev, "failed to initialise WM8510\n");
724 return ret;
727 static int __devexit wm8510_spi_remove(struct spi_device *spi)
729 return 0;
732 static struct spi_driver wm8510_spi_driver = {
733 .driver = {
734 .name = "wm8510",
735 .bus = &spi_bus_type,
736 .owner = THIS_MODULE,
738 .probe = wm8510_spi_probe,
739 .remove = __devexit_p(wm8510_spi_remove),
741 #endif /* CONFIG_SPI_MASTER */
743 static int wm8510_probe(struct platform_device *pdev)
745 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
746 struct wm8510_setup_data *setup;
747 struct snd_soc_codec *codec;
748 int ret = 0;
750 pr_info("WM8510 Audio Codec %s", WM8510_VERSION);
752 setup = socdev->codec_data;
753 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
754 if (codec == NULL)
755 return -ENOMEM;
757 socdev->card->codec = codec;
758 mutex_init(&codec->mutex);
759 INIT_LIST_HEAD(&codec->dapm_widgets);
760 INIT_LIST_HEAD(&codec->dapm_paths);
762 wm8510_socdev = socdev;
763 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
764 if (setup->i2c_address) {
765 ret = wm8510_add_i2c_device(pdev, setup);
767 #endif
768 #if defined(CONFIG_SPI_MASTER)
769 if (setup->spi) {
770 ret = spi_register_driver(&wm8510_spi_driver);
771 if (ret != 0)
772 printk(KERN_ERR "can't add spi driver");
774 #endif
776 if (ret != 0)
777 kfree(codec);
778 return ret;
781 /* power down chip */
782 static int wm8510_remove(struct platform_device *pdev)
784 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
785 struct snd_soc_codec *codec = socdev->card->codec;
787 if (codec->control_data)
788 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
790 snd_soc_free_pcms(socdev);
791 snd_soc_dapm_free(socdev);
792 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
793 i2c_unregister_device(codec->control_data);
794 i2c_del_driver(&wm8510_i2c_driver);
795 #endif
796 #if defined(CONFIG_SPI_MASTER)
797 spi_unregister_driver(&wm8510_spi_driver);
798 #endif
799 kfree(codec);
801 return 0;
804 struct snd_soc_codec_device soc_codec_dev_wm8510 = {
805 .probe = wm8510_probe,
806 .remove = wm8510_remove,
807 .suspend = wm8510_suspend,
808 .resume = wm8510_resume,
810 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8510);
812 static int __init wm8510_modinit(void)
814 return snd_soc_register_dai(&wm8510_dai);
816 module_init(wm8510_modinit);
818 static void __exit wm8510_exit(void)
820 snd_soc_unregister_dai(&wm8510_dai);
822 module_exit(wm8510_exit);
824 MODULE_DESCRIPTION("ASoC WM8510 driver");
825 MODULE_AUTHOR("Liam Girdwood");
826 MODULE_LICENSE("GPL");