blob: 152e6f21154f08f3b5ba1ef49ff4e3effe96ab78 [file] [log] [blame]
Mark Brown5d421512008-06-05 13:49:32 +01001/*
2 * wm8510.c -- WM8510 ALSA Soc Audio driver
3 *
4 * Copyright 2006 Wolfson Microelectronics PLC.
5 *
6 * Author: Liam Girdwood <liam.girdwood@wolfsonmicro.com>
7 *
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.
11 */
12
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 <sound/core.h>
22#include <sound/pcm.h>
23#include <sound/pcm_params.h>
24#include <sound/soc.h>
25#include <sound/soc-dapm.h>
26#include <sound/initval.h>
27
28#include "wm8510.h"
29
30#define AUDIO_NAME "wm8510"
31#define WM8510_VERSION "0.6"
32
33/*
34 * Debug
35 */
36
37#define WM8510_DEBUG 0
38
39#ifdef WM8510_DEBUG
40#define dbg(format, arg...) \
41 printk(KERN_DEBUG AUDIO_NAME ": " format "\n" , ## arg)
42#else
43#define dbg(format, arg...) do {} while (0)
44#endif
45#define err(format, arg...) \
46 printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg)
47#define info(format, arg...) \
48 printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg)
49#define warn(format, arg...) \
50 printk(KERN_WARNING AUDIO_NAME ": " format "\n" , ## arg)
51
52struct snd_soc_codec_device soc_codec_dev_wm8510;
53
54/*
55 * wm8510 register cache
56 * We can't read the WM8510 register space when we are
57 * using 2 wire for device control, so we cache them instead.
58 */
59static const u16 wm8510_reg[WM8510_CACHEREGNUM] = {
60 0x0000, 0x0000, 0x0000, 0x0000,
61 0x0050, 0x0000, 0x0140, 0x0000,
62 0x0000, 0x0000, 0x0000, 0x00ff,
63 0x0000, 0x0000, 0x0100, 0x00ff,
64 0x0000, 0x0000, 0x012c, 0x002c,
65 0x002c, 0x002c, 0x002c, 0x0000,
66 0x0032, 0x0000, 0x0000, 0x0000,
67 0x0000, 0x0000, 0x0000, 0x0000,
68 0x0038, 0x000b, 0x0032, 0x0000,
69 0x0008, 0x000c, 0x0093, 0x00e9,
70 0x0000, 0x0000, 0x0000, 0x0000,
71 0x0003, 0x0010, 0x0000, 0x0000,
72 0x0000, 0x0002, 0x0001, 0x0000,
73 0x0000, 0x0000, 0x0039, 0x0000,
Mark Brown05a076d2008-06-06 17:13:53 +010074 0x0001,
Mark Brown5d421512008-06-05 13:49:32 +010075};
76
77/*
78 * read wm8510 register cache
79 */
80static inline unsigned int wm8510_read_reg_cache(struct snd_soc_codec *codec,
81 unsigned int reg)
82{
83 u16 *cache = codec->reg_cache;
84 if (reg == WM8510_RESET)
85 return 0;
86 if (reg >= WM8510_CACHEREGNUM)
87 return -1;
88 return cache[reg];
89}
90
91/*
92 * write wm8510 register cache
93 */
94static inline void wm8510_write_reg_cache(struct snd_soc_codec *codec,
95 u16 reg, unsigned int value)
96{
97 u16 *cache = codec->reg_cache;
98 if (reg >= WM8510_CACHEREGNUM)
99 return;
100 cache[reg] = value;
101}
102
103/*
104 * write to the WM8510 register space
105 */
106static int wm8510_write(struct snd_soc_codec *codec, unsigned int reg,
107 unsigned int value)
108{
109 u8 data[2];
110
111 /* data is
112 * D15..D9 WM8510 register offset
113 * D8...D0 register data
114 */
115 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
116 data[1] = value & 0x00ff;
117
118 wm8510_write_reg_cache(codec, reg, value);
119 if (codec->hw_write(codec->control_data, data, 2) == 2)
120 return 0;
121 else
122 return -EIO;
123}
124
125#define wm8510_reset(c) wm8510_write(c, WM8510_RESET, 0)
126
127static const char *wm8510_companding[] = { "Off", "NC", "u-law", "A-law" };
128static const char *wm8510_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
129static const char *wm8510_alc[] = { "ALC", "Limiter" };
130
131static const struct soc_enum wm8510_enum[] = {
132 SOC_ENUM_SINGLE(WM8510_COMP, 1, 4, wm8510_companding), /* adc */
133 SOC_ENUM_SINGLE(WM8510_COMP, 3, 4, wm8510_companding), /* dac */
134 SOC_ENUM_SINGLE(WM8510_DAC, 4, 4, wm8510_deemp),
135 SOC_ENUM_SINGLE(WM8510_ALC3, 8, 2, wm8510_alc),
136};
137
138static const struct snd_kcontrol_new wm8510_snd_controls[] = {
139
140SOC_SINGLE("Digital Loopback Switch", WM8510_COMP, 0, 1, 0),
141
142SOC_ENUM("DAC Companding", wm8510_enum[1]),
143SOC_ENUM("ADC Companding", wm8510_enum[0]),
144
145SOC_ENUM("Playback De-emphasis", wm8510_enum[2]),
146SOC_SINGLE("DAC Inversion Switch", WM8510_DAC, 0, 1, 0),
147
148SOC_SINGLE("Master Playback Volume", WM8510_DACVOL, 0, 127, 0),
149
150SOC_SINGLE("High Pass Filter Switch", WM8510_ADC, 8, 1, 0),
151SOC_SINGLE("High Pass Cut Off", WM8510_ADC, 4, 7, 0),
152SOC_SINGLE("ADC Inversion Switch", WM8510_COMP, 0, 1, 0),
153
154SOC_SINGLE("Capture Volume", WM8510_ADCVOL, 0, 127, 0),
155
156SOC_SINGLE("DAC Playback Limiter Switch", WM8510_DACLIM1, 8, 1, 0),
157SOC_SINGLE("DAC Playback Limiter Decay", WM8510_DACLIM1, 4, 15, 0),
158SOC_SINGLE("DAC Playback Limiter Attack", WM8510_DACLIM1, 0, 15, 0),
159
160SOC_SINGLE("DAC Playback Limiter Threshold", WM8510_DACLIM2, 4, 7, 0),
161SOC_SINGLE("DAC Playback Limiter Boost", WM8510_DACLIM2, 0, 15, 0),
162
163SOC_SINGLE("ALC Enable Switch", WM8510_ALC1, 8, 1, 0),
164SOC_SINGLE("ALC Capture Max Gain", WM8510_ALC1, 3, 7, 0),
165SOC_SINGLE("ALC Capture Min Gain", WM8510_ALC1, 0, 7, 0),
166
167SOC_SINGLE("ALC Capture ZC Switch", WM8510_ALC2, 8, 1, 0),
168SOC_SINGLE("ALC Capture Hold", WM8510_ALC2, 4, 7, 0),
169SOC_SINGLE("ALC Capture Target", WM8510_ALC2, 0, 15, 0),
170
171SOC_ENUM("ALC Capture Mode", wm8510_enum[3]),
172SOC_SINGLE("ALC Capture Decay", WM8510_ALC3, 4, 15, 0),
173SOC_SINGLE("ALC Capture Attack", WM8510_ALC3, 0, 15, 0),
174
175SOC_SINGLE("ALC Capture Noise Gate Switch", WM8510_NGATE, 3, 1, 0),
176SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8510_NGATE, 0, 7, 0),
177
178SOC_SINGLE("Capture PGA ZC Switch", WM8510_INPPGA, 7, 1, 0),
179SOC_SINGLE("Capture PGA Volume", WM8510_INPPGA, 0, 63, 0),
180
181SOC_SINGLE("Speaker Playback ZC Switch", WM8510_SPKVOL, 7, 1, 0),
182SOC_SINGLE("Speaker Playback Switch", WM8510_SPKVOL, 6, 1, 1),
183SOC_SINGLE("Speaker Playback Volume", WM8510_SPKVOL, 0, 63, 0),
184SOC_SINGLE("Speaker Boost", WM8510_OUTPUT, 2, 1, 0),
185
186SOC_SINGLE("Capture Boost(+20dB)", WM8510_ADCBOOST, 8, 1, 0),
187SOC_SINGLE("Mono Playback Switch", WM8510_MONOMIX, 6, 1, 1),
188};
189
190/* add non dapm controls */
191static int wm8510_add_controls(struct snd_soc_codec *codec)
192{
193 int err, i;
194
195 for (i = 0; i < ARRAY_SIZE(wm8510_snd_controls); i++) {
196 err = snd_ctl_add(codec->card,
197 snd_soc_cnew(&wm8510_snd_controls[i], codec,
198 NULL));
199 if (err < 0)
200 return err;
201 }
202
203 return 0;
204}
205
206/* Speaker Output Mixer */
207static const struct snd_kcontrol_new wm8510_speaker_mixer_controls[] = {
208SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_SPKMIX, 1, 1, 0),
209SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_SPKMIX, 5, 1, 0),
210SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_SPKMIX, 0, 1, 0),
211};
212
213/* Mono Output Mixer */
214static const struct snd_kcontrol_new wm8510_mono_mixer_controls[] = {
215SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX, 1, 1, 0),
216SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX, 2, 1, 0),
217SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX, 0, 1, 0),
218};
219
220static const struct snd_kcontrol_new wm8510_boost_controls[] = {
221SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA, 6, 1, 0),
222SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST, 0, 7, 0),
223SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST, 4, 7, 0),
224};
225
226static const struct snd_kcontrol_new wm8510_micpga_controls[] = {
227SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT, 0, 1, 0),
228SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT, 1, 1, 0),
229SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT, 2, 1, 0),
230};
231
232static const struct snd_soc_dapm_widget wm8510_dapm_widgets[] = {
233SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3, 2, 0,
234 &wm8510_speaker_mixer_controls[0],
235 ARRAY_SIZE(wm8510_speaker_mixer_controls)),
236SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3, 3, 0,
237 &wm8510_mono_mixer_controls[0],
238 ARRAY_SIZE(wm8510_mono_mixer_controls)),
239SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3, 0, 0),
240SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2, 0, 0),
241SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1, 6, 0, NULL, 0),
242SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3, 5, 0, NULL, 0),
243SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3, 6, 0, NULL, 0),
244SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3, 7, 0, NULL, 0),
245
246SND_SOC_DAPM_PGA("Mic PGA", WM8510_POWER2, 2, 0,
247 &wm8510_micpga_controls[0],
248 ARRAY_SIZE(wm8510_micpga_controls)),
249SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2, 4, 0,
250 &wm8510_boost_controls[0],
251 ARRAY_SIZE(wm8510_boost_controls)),
252
253SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_POWER1, 4, 0),
254
255SND_SOC_DAPM_INPUT("MICN"),
256SND_SOC_DAPM_INPUT("MICP"),
257SND_SOC_DAPM_INPUT("AUX"),
258SND_SOC_DAPM_OUTPUT("MONOOUT"),
259SND_SOC_DAPM_OUTPUT("SPKOUTP"),
260SND_SOC_DAPM_OUTPUT("SPKOUTN"),
261};
262
263static const struct snd_soc_dapm_route audio_map[] = {
264 /* Mono output mixer */
265 {"Mono Mixer", "PCM Playback Switch", "DAC"},
266 {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
267 {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
268
269 /* Speaker output mixer */
270 {"Speaker Mixer", "PCM Playback Switch", "DAC"},
271 {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
272 {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
273
274 /* Outputs */
275 {"Mono Out", NULL, "Mono Mixer"},
276 {"MONOOUT", NULL, "Mono Out"},
277 {"SpkN Out", NULL, "Speaker Mixer"},
278 {"SpkP Out", NULL, "Speaker Mixer"},
279 {"SPKOUTN", NULL, "SpkN Out"},
280 {"SPKOUTP", NULL, "SpkP Out"},
281
282 /* Microphone PGA */
283 {"Mic PGA", "MICN Switch", "MICN"},
284 {"Mic PGA", "MICP Switch", "MICP"},
285 { "Mic PGA", "AUX Switch", "Aux Input" },
286
287 /* Boost Mixer */
288 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
289 {"Boost Mixer", "Mic Volume", "MICP"},
290 {"Boost Mixer", "Aux Volume", "Aux Input"},
291
292 {"ADC", NULL, "Boost Mixer"},
293};
294
295static int wm8510_add_widgets(struct snd_soc_codec *codec)
296{
297 snd_soc_dapm_new_controls(codec, wm8510_dapm_widgets,
298 ARRAY_SIZE(wm8510_dapm_widgets));
299
300 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
301
302 snd_soc_dapm_new_widgets(codec);
303 return 0;
304}
305
306struct pll_ {
307 unsigned int pre_div:4; /* prescale - 1 */
308 unsigned int n:4;
309 unsigned int k;
310};
311
312static struct pll_ pll_div;
313
314/* The size in bits of the pll divide multiplied by 10
315 * to allow rounding later */
316#define FIXED_PLL_SIZE ((1 << 24) * 10)
317
318static void pll_factors(unsigned int target, unsigned int source)
319{
320 unsigned long long Kpart;
321 unsigned int K, Ndiv, Nmod;
322
323 Ndiv = target / source;
324 if (Ndiv < 6) {
325 source >>= 1;
326 pll_div.pre_div = 1;
327 Ndiv = target / source;
328 } else
329 pll_div.pre_div = 0;
330
331 if ((Ndiv < 6) || (Ndiv > 12))
332 printk(KERN_WARNING
333 "WM8510 N value %d outwith recommended range!d\n",
334 Ndiv);
335
336 pll_div.n = Ndiv;
337 Nmod = target % source;
338 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
339
340 do_div(Kpart, source);
341
342 K = Kpart & 0xFFFFFFFF;
343
344 /* Check if we need to round */
345 if ((K % 10) >= 5)
346 K += 5;
347
348 /* Move down to proper range now rounding is done */
349 K /= 10;
350
351 pll_div.k = K;
352}
353
354static int wm8510_set_dai_pll(struct snd_soc_codec_dai *codec_dai,
355 int pll_id, unsigned int freq_in, unsigned int freq_out)
356{
357 struct snd_soc_codec *codec = codec_dai->codec;
358 u16 reg;
359
360 if (freq_in == 0 || freq_out == 0) {
361 /* Clock CODEC directly from MCLK */
362 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK);
363 wm8510_write(codec, WM8510_CLOCK, reg & 0x0ff);
364
365 /* Turn off PLL */
366 reg = wm8510_read_reg_cache(codec, WM8510_POWER1);
367 wm8510_write(codec, WM8510_POWER1, reg & 0x1df);
368 return 0;
369 }
370
371 pll_factors(freq_out*8, freq_in);
372
373 wm8510_write(codec, WM8510_PLLN, (pll_div.pre_div << 4) | pll_div.n);
374 wm8510_write(codec, WM8510_PLLK1, pll_div.k >> 18);
375 wm8510_write(codec, WM8510_PLLK2, (pll_div.k >> 9) & 0x1ff);
376 wm8510_write(codec, WM8510_PLLK3, pll_div.k & 0x1ff);
377 reg = wm8510_read_reg_cache(codec, WM8510_POWER1);
378 wm8510_write(codec, WM8510_POWER1, reg | 0x020);
379
380 /* Run CODEC from PLL instead of MCLK */
381 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK);
382 wm8510_write(codec, WM8510_CLOCK, reg | 0x100);
383
384 return 0;
385}
386
387/*
388 * Configure WM8510 clock dividers.
389 */
390static int wm8510_set_dai_clkdiv(struct snd_soc_codec_dai *codec_dai,
391 int div_id, int div)
392{
393 struct snd_soc_codec *codec = codec_dai->codec;
394 u16 reg;
395
396 switch (div_id) {
397 case WM8510_OPCLKDIV:
398 reg = wm8510_read_reg_cache(codec, WM8510_GPIO) & 0x1cf;
399 wm8510_write(codec, WM8510_GPIO, reg | div);
400 break;
401 case WM8510_MCLKDIV:
402 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1f;
403 wm8510_write(codec, WM8510_CLOCK, reg | div);
404 break;
405 case WM8510_ADCCLK:
406 reg = wm8510_read_reg_cache(codec, WM8510_ADC) & 0x1f7;
407 wm8510_write(codec, WM8510_ADC, reg | div);
408 break;
409 case WM8510_DACCLK:
410 reg = wm8510_read_reg_cache(codec, WM8510_DAC) & 0x1f7;
411 wm8510_write(codec, WM8510_DAC, reg | div);
412 break;
413 case WM8510_BCLKDIV:
414 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1e3;
415 wm8510_write(codec, WM8510_CLOCK, reg | div);
416 break;
417 default:
418 return -EINVAL;
419 }
420
421 return 0;
422}
423
424static int wm8510_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
425 unsigned int fmt)
426{
427 struct snd_soc_codec *codec = codec_dai->codec;
428 u16 iface = 0;
429 u16 clk = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1fe;
430
431 /* set master/slave audio interface */
432 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
433 case SND_SOC_DAIFMT_CBM_CFM:
434 clk |= 0x0001;
435 break;
436 case SND_SOC_DAIFMT_CBS_CFS:
437 break;
438 default:
439 return -EINVAL;
440 }
441
442 /* interface format */
443 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
444 case SND_SOC_DAIFMT_I2S:
445 iface |= 0x0010;
446 break;
447 case SND_SOC_DAIFMT_RIGHT_J:
448 break;
449 case SND_SOC_DAIFMT_LEFT_J:
450 iface |= 0x0008;
451 break;
452 case SND_SOC_DAIFMT_DSP_A:
453 iface |= 0x00018;
454 break;
455 default:
456 return -EINVAL;
457 }
458
459 /* clock inversion */
460 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
461 case SND_SOC_DAIFMT_NB_NF:
462 break;
463 case SND_SOC_DAIFMT_IB_IF:
464 iface |= 0x0180;
465 break;
466 case SND_SOC_DAIFMT_IB_NF:
467 iface |= 0x0100;
468 break;
469 case SND_SOC_DAIFMT_NB_IF:
470 iface |= 0x0080;
471 break;
472 default:
473 return -EINVAL;
474 }
475
476 wm8510_write(codec, WM8510_IFACE, iface);
477 wm8510_write(codec, WM8510_CLOCK, clk);
478 return 0;
479}
480
481static int wm8510_pcm_hw_params(struct snd_pcm_substream *substream,
482 struct snd_pcm_hw_params *params)
483{
484 struct snd_soc_pcm_runtime *rtd = substream->private_data;
485 struct snd_soc_device *socdev = rtd->socdev;
486 struct snd_soc_codec *codec = socdev->codec;
487 u16 iface = wm8510_read_reg_cache(codec, WM8510_IFACE) & 0x19f;
488 u16 adn = wm8510_read_reg_cache(codec, WM8510_ADD) & 0x1f1;
489
490 /* bit size */
491 switch (params_format(params)) {
492 case SNDRV_PCM_FORMAT_S16_LE:
493 break;
494 case SNDRV_PCM_FORMAT_S20_3LE:
495 iface |= 0x0020;
496 break;
497 case SNDRV_PCM_FORMAT_S24_LE:
498 iface |= 0x0040;
499 break;
500 case SNDRV_PCM_FORMAT_S32_LE:
501 iface |= 0x0060;
502 break;
503 }
504
505 /* filter coefficient */
506 switch (params_rate(params)) {
507 case SNDRV_PCM_RATE_8000:
508 adn |= 0x5 << 1;
509 break;
510 case SNDRV_PCM_RATE_11025:
511 adn |= 0x4 << 1;
512 break;
513 case SNDRV_PCM_RATE_16000:
514 adn |= 0x3 << 1;
515 break;
516 case SNDRV_PCM_RATE_22050:
517 adn |= 0x2 << 1;
518 break;
519 case SNDRV_PCM_RATE_32000:
520 adn |= 0x1 << 1;
521 break;
522 case SNDRV_PCM_RATE_44100:
523 case SNDRV_PCM_RATE_48000:
524 break;
525 }
526
527 wm8510_write(codec, WM8510_IFACE, iface);
528 wm8510_write(codec, WM8510_ADD, adn);
529 return 0;
530}
531
532static int wm8510_mute(struct snd_soc_codec_dai *dai, int mute)
533{
534 struct snd_soc_codec *codec = dai->codec;
535 u16 mute_reg = wm8510_read_reg_cache(codec, WM8510_DAC) & 0xffbf;
536
537 if (mute)
538 wm8510_write(codec, WM8510_DAC, mute_reg | 0x40);
539 else
540 wm8510_write(codec, WM8510_DAC, mute_reg);
541 return 0;
542}
543
544/* liam need to make this lower power with dapm */
545static int wm8510_set_bias_level(struct snd_soc_codec *codec,
546 enum snd_soc_bias_level level)
547{
548
549 switch (level) {
550 case SND_SOC_BIAS_ON:
551 wm8510_write(codec, WM8510_POWER1, 0x1ff);
552 wm8510_write(codec, WM8510_POWER2, 0x1ff);
553 wm8510_write(codec, WM8510_POWER3, 0x1ff);
554 break;
555 case SND_SOC_BIAS_PREPARE:
556 case SND_SOC_BIAS_STANDBY:
557 break;
558 case SND_SOC_BIAS_OFF:
559 /* everything off, dac mute, inactive */
560 wm8510_write(codec, WM8510_POWER1, 0x0);
561 wm8510_write(codec, WM8510_POWER2, 0x0);
562 wm8510_write(codec, WM8510_POWER3, 0x0);
563 break;
564 }
565 codec->bias_level = level;
566 return 0;
567}
568
569#define WM8510_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
570 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
571 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
572
573#define WM8510_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
574 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
575
576struct snd_soc_codec_dai wm8510_dai = {
577 .name = "WM8510 HiFi",
578 .playback = {
579 .stream_name = "Playback",
580 .channels_min = 2,
581 .channels_max = 2,
582 .rates = WM8510_RATES,
583 .formats = WM8510_FORMATS,},
584 .capture = {
585 .stream_name = "Capture",
586 .channels_min = 2,
587 .channels_max = 2,
588 .rates = WM8510_RATES,
589 .formats = WM8510_FORMATS,},
590 .ops = {
591 .hw_params = wm8510_pcm_hw_params,
592 },
593 .dai_ops = {
594 .digital_mute = wm8510_mute,
595 .set_fmt = wm8510_set_dai_fmt,
596 .set_clkdiv = wm8510_set_dai_clkdiv,
597 .set_pll = wm8510_set_dai_pll,
598 },
599};
600EXPORT_SYMBOL_GPL(wm8510_dai);
601
602static int wm8510_suspend(struct platform_device *pdev, pm_message_t state)
603{
604 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
605 struct snd_soc_codec *codec = socdev->codec;
606
607 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
608 return 0;
609}
610
611static int wm8510_resume(struct platform_device *pdev)
612{
613 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
614 struct snd_soc_codec *codec = socdev->codec;
615 int i;
616 u8 data[2];
617 u16 *cache = codec->reg_cache;
618
619 /* Sync reg_cache with the hardware */
620 for (i = 0; i < ARRAY_SIZE(wm8510_reg); i++) {
621 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
622 data[1] = cache[i] & 0x00ff;
623 codec->hw_write(codec->control_data, data, 2);
624 }
625 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
626 wm8510_set_bias_level(codec, codec->suspend_bias_level);
627 return 0;
628}
629
630/*
631 * initialise the WM8510 driver
632 * register the mixer and dsp interfaces with the kernel
633 */
634static int wm8510_init(struct snd_soc_device *socdev)
635{
636 struct snd_soc_codec *codec = socdev->codec;
637 int ret = 0;
638
639 codec->name = "WM8510";
640 codec->owner = THIS_MODULE;
641 codec->read = wm8510_read_reg_cache;
642 codec->write = wm8510_write;
643 codec->set_bias_level = wm8510_set_bias_level;
644 codec->dai = &wm8510_dai;
645 codec->num_dai = 1;
646 codec->reg_cache_size = ARRAY_SIZE(wm8510_reg);
647 codec->reg_cache = kmemdup(wm8510_reg, sizeof(wm8510_reg), GFP_KERNEL);
648
649 if (codec->reg_cache == NULL)
650 return -ENOMEM;
651
652 wm8510_reset(codec);
653
654 /* register pcms */
655 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
656 if (ret < 0) {
657 printk(KERN_ERR "wm8510: failed to create pcms\n");
658 goto pcm_err;
659 }
660
661 /* power on device */
662 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
663 wm8510_add_controls(codec);
664 wm8510_add_widgets(codec);
665 ret = snd_soc_register_card(socdev);
666 if (ret < 0) {
667 printk(KERN_ERR "wm8510: failed to register card\n");
668 goto card_err;
669 }
670 return ret;
671
672card_err:
673 snd_soc_free_pcms(socdev);
674 snd_soc_dapm_free(socdev);
675pcm_err:
676 kfree(codec->reg_cache);
677 return ret;
678}
679
680static struct snd_soc_device *wm8510_socdev;
681
682#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
683
684/*
685 * WM8510 2 wire address is 0x1a
686 */
687#define I2C_DRIVERID_WM8510 0xfefe /* liam - need a proper id */
688
689static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
690
691/* Magic definition of all other variables and things */
692I2C_CLIENT_INSMOD;
693
694static struct i2c_driver wm8510_i2c_driver;
695static struct i2c_client client_template;
696
697/* If the i2c layer weren't so broken, we could pass this kind of data
698 around */
699
700static int wm8510_codec_probe(struct i2c_adapter *adap, int addr, int kind)
701{
702 struct snd_soc_device *socdev = wm8510_socdev;
703 struct wm8510_setup_data *setup = socdev->codec_data;
704 struct snd_soc_codec *codec = socdev->codec;
705 struct i2c_client *i2c;
706 int ret;
707
708 if (addr != setup->i2c_address)
709 return -ENODEV;
710
711 client_template.adapter = adap;
712 client_template.addr = addr;
713
714 i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
715 if (i2c == NULL) {
716 kfree(codec);
717 return -ENOMEM;
718 }
719 i2c_set_clientdata(i2c, codec);
720 codec->control_data = i2c;
721
722 ret = i2c_attach_client(i2c);
723 if (ret < 0) {
724 err("failed to attach codec at addr %x\n", addr);
725 goto err;
726 }
727
728 ret = wm8510_init(socdev);
729 if (ret < 0) {
730 err("failed to initialise WM8510\n");
731 goto err;
732 }
733 return ret;
734
735err:
736 kfree(codec);
737 kfree(i2c);
738 return ret;
739}
740
741static int wm8510_i2c_detach(struct i2c_client *client)
742{
743 struct snd_soc_codec *codec = i2c_get_clientdata(client);
744 i2c_detach_client(client);
745 kfree(codec->reg_cache);
746 kfree(client);
747 return 0;
748}
749
750static int wm8510_i2c_attach(struct i2c_adapter *adap)
751{
752 return i2c_probe(adap, &addr_data, wm8510_codec_probe);
753}
754
755/* corgi i2c codec control layer */
756static struct i2c_driver wm8510_i2c_driver = {
757 .driver = {
758 .name = "WM8510 I2C Codec",
759 .owner = THIS_MODULE,
760 },
761 .id = I2C_DRIVERID_WM8510,
762 .attach_adapter = wm8510_i2c_attach,
763 .detach_client = wm8510_i2c_detach,
764 .command = NULL,
765};
766
767static struct i2c_client client_template = {
768 .name = "WM8510",
769 .driver = &wm8510_i2c_driver,
770};
771#endif
772
773static int wm8510_probe(struct platform_device *pdev)
774{
775 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
776 struct wm8510_setup_data *setup;
777 struct snd_soc_codec *codec;
778 int ret = 0;
779
780 info("WM8510 Audio Codec %s", WM8510_VERSION);
781
782 setup = socdev->codec_data;
783 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
784 if (codec == NULL)
785 return -ENOMEM;
786
787 socdev->codec = codec;
788 mutex_init(&codec->mutex);
789 INIT_LIST_HEAD(&codec->dapm_widgets);
790 INIT_LIST_HEAD(&codec->dapm_paths);
791
792 wm8510_socdev = socdev;
793#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
794 if (setup->i2c_address) {
795 normal_i2c[0] = setup->i2c_address;
796 codec->hw_write = (hw_write_t)i2c_master_send;
797 ret = i2c_add_driver(&wm8510_i2c_driver);
798 if (ret != 0)
799 printk(KERN_ERR "can't add i2c driver");
800 }
801#else
802 /* Add other interfaces here */
803#endif
804 return ret;
805}
806
807/* power down chip */
808static int wm8510_remove(struct platform_device *pdev)
809{
810 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
811 struct snd_soc_codec *codec = socdev->codec;
812
813 if (codec->control_data)
814 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
815
816 snd_soc_free_pcms(socdev);
817 snd_soc_dapm_free(socdev);
818#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
819 i2c_del_driver(&wm8510_i2c_driver);
820#endif
821 kfree(codec);
822
823 return 0;
824}
825
826struct snd_soc_codec_device soc_codec_dev_wm8510 = {
827 .probe = wm8510_probe,
828 .remove = wm8510_remove,
829 .suspend = wm8510_suspend,
830 .resume = wm8510_resume,
831};
832EXPORT_SYMBOL_GPL(soc_codec_dev_wm8510);
833
834MODULE_DESCRIPTION("ASoC WM8510 driver");
835MODULE_AUTHOR("Liam Girdwood");
836MODULE_LICENSE("GPL");