blob: cc975a62fa5c0633d50a507af457f814655fe095 [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 *
Liam Girdwoodd3311242008-10-12 13:17:36 +01006 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Mark Brown5d421512008-06-05 13:49:32 +01007 *
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>
Mark Brown5e357952008-10-07 11:56:20 +010021#include <linux/spi/spi.h>
Mark Brown5d421512008-06-05 13:49:32 +010022#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>
28
29#include "wm8510.h"
30
Mark Brown5d421512008-06-05 13:49:32 +010031#define WM8510_VERSION "0.6"
32
Mark Brown5d421512008-06-05 13:49:32 +010033struct snd_soc_codec_device soc_codec_dev_wm8510;
34
35/*
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.
39 */
40static 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,
Mark Brown05a076d2008-06-06 17:13:53 +010055 0x0001,
Mark Brown5d421512008-06-05 13:49:32 +010056};
57
Mark Brown09af98b2008-10-07 13:04:58 +010058#define WM8510_POWER1_BIASEN 0x08
59#define WM8510_POWER1_BUFIOEN 0x10
60
Mark Brown5d421512008-06-05 13:49:32 +010061/*
62 * read wm8510 register cache
63 */
64static inline unsigned int wm8510_read_reg_cache(struct snd_soc_codec *codec,
65 unsigned int reg)
66{
67 u16 *cache = codec->reg_cache;
68 if (reg == WM8510_RESET)
69 return 0;
70 if (reg >= WM8510_CACHEREGNUM)
71 return -1;
72 return cache[reg];
73}
74
75/*
76 * write wm8510 register cache
77 */
78static inline void wm8510_write_reg_cache(struct snd_soc_codec *codec,
79 u16 reg, unsigned int value)
80{
81 u16 *cache = codec->reg_cache;
82 if (reg >= WM8510_CACHEREGNUM)
83 return;
84 cache[reg] = value;
85}
86
87/*
88 * write to the WM8510 register space
89 */
90static int wm8510_write(struct snd_soc_codec *codec, unsigned int reg,
91 unsigned int value)
92{
93 u8 data[2];
94
95 /* data is
96 * D15..D9 WM8510 register offset
97 * D8...D0 register data
98 */
99 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
100 data[1] = value & 0x00ff;
101
102 wm8510_write_reg_cache(codec, reg, value);
103 if (codec->hw_write(codec->control_data, data, 2) == 2)
104 return 0;
105 else
106 return -EIO;
107}
108
109#define wm8510_reset(c) wm8510_write(c, WM8510_RESET, 0)
110
111static const char *wm8510_companding[] = { "Off", "NC", "u-law", "A-law" };
112static const char *wm8510_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
113static const char *wm8510_alc[] = { "ALC", "Limiter" };
114
115static 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),
120};
121
122static const struct snd_kcontrol_new wm8510_snd_controls[] = {
123
124SOC_SINGLE("Digital Loopback Switch", WM8510_COMP, 0, 1, 0),
125
126SOC_ENUM("DAC Companding", wm8510_enum[1]),
127SOC_ENUM("ADC Companding", wm8510_enum[0]),
128
129SOC_ENUM("Playback De-emphasis", wm8510_enum[2]),
130SOC_SINGLE("DAC Inversion Switch", WM8510_DAC, 0, 1, 0),
131
132SOC_SINGLE("Master Playback Volume", WM8510_DACVOL, 0, 127, 0),
133
134SOC_SINGLE("High Pass Filter Switch", WM8510_ADC, 8, 1, 0),
135SOC_SINGLE("High Pass Cut Off", WM8510_ADC, 4, 7, 0),
136SOC_SINGLE("ADC Inversion Switch", WM8510_COMP, 0, 1, 0),
137
138SOC_SINGLE("Capture Volume", WM8510_ADCVOL, 0, 127, 0),
139
140SOC_SINGLE("DAC Playback Limiter Switch", WM8510_DACLIM1, 8, 1, 0),
141SOC_SINGLE("DAC Playback Limiter Decay", WM8510_DACLIM1, 4, 15, 0),
142SOC_SINGLE("DAC Playback Limiter Attack", WM8510_DACLIM1, 0, 15, 0),
143
144SOC_SINGLE("DAC Playback Limiter Threshold", WM8510_DACLIM2, 4, 7, 0),
145SOC_SINGLE("DAC Playback Limiter Boost", WM8510_DACLIM2, 0, 15, 0),
146
147SOC_SINGLE("ALC Enable Switch", WM8510_ALC1, 8, 1, 0),
148SOC_SINGLE("ALC Capture Max Gain", WM8510_ALC1, 3, 7, 0),
149SOC_SINGLE("ALC Capture Min Gain", WM8510_ALC1, 0, 7, 0),
150
151SOC_SINGLE("ALC Capture ZC Switch", WM8510_ALC2, 8, 1, 0),
152SOC_SINGLE("ALC Capture Hold", WM8510_ALC2, 4, 7, 0),
153SOC_SINGLE("ALC Capture Target", WM8510_ALC2, 0, 15, 0),
154
155SOC_ENUM("ALC Capture Mode", wm8510_enum[3]),
156SOC_SINGLE("ALC Capture Decay", WM8510_ALC3, 4, 15, 0),
157SOC_SINGLE("ALC Capture Attack", WM8510_ALC3, 0, 15, 0),
158
159SOC_SINGLE("ALC Capture Noise Gate Switch", WM8510_NGATE, 3, 1, 0),
160SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8510_NGATE, 0, 7, 0),
161
162SOC_SINGLE("Capture PGA ZC Switch", WM8510_INPPGA, 7, 1, 0),
163SOC_SINGLE("Capture PGA Volume", WM8510_INPPGA, 0, 63, 0),
164
165SOC_SINGLE("Speaker Playback ZC Switch", WM8510_SPKVOL, 7, 1, 0),
166SOC_SINGLE("Speaker Playback Switch", WM8510_SPKVOL, 6, 1, 1),
167SOC_SINGLE("Speaker Playback Volume", WM8510_SPKVOL, 0, 63, 0),
168SOC_SINGLE("Speaker Boost", WM8510_OUTPUT, 2, 1, 0),
169
170SOC_SINGLE("Capture Boost(+20dB)", WM8510_ADCBOOST, 8, 1, 0),
171SOC_SINGLE("Mono Playback Switch", WM8510_MONOMIX, 6, 1, 1),
172};
173
Mark Brown5d421512008-06-05 13:49:32 +0100174/* Speaker Output Mixer */
175static const struct snd_kcontrol_new wm8510_speaker_mixer_controls[] = {
176SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_SPKMIX, 1, 1, 0),
177SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_SPKMIX, 5, 1, 0),
178SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_SPKMIX, 0, 1, 0),
179};
180
181/* Mono Output Mixer */
182static const struct snd_kcontrol_new wm8510_mono_mixer_controls[] = {
183SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX, 1, 1, 0),
184SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX, 2, 1, 0),
185SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX, 0, 1, 0),
186};
187
188static const struct snd_kcontrol_new wm8510_boost_controls[] = {
Mark Brown8ae23ec2008-10-06 11:33:21 +0100189SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA, 6, 1, 1),
Mark Brown5d421512008-06-05 13:49:32 +0100190SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST, 0, 7, 0),
191SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST, 4, 7, 0),
192};
193
194static const struct snd_kcontrol_new wm8510_micpga_controls[] = {
195SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT, 0, 1, 0),
196SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT, 1, 1, 0),
197SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT, 2, 1, 0),
198};
199
200static const struct snd_soc_dapm_widget wm8510_dapm_widgets[] = {
201SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3, 2, 0,
202 &wm8510_speaker_mixer_controls[0],
203 ARRAY_SIZE(wm8510_speaker_mixer_controls)),
204SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3, 3, 0,
205 &wm8510_mono_mixer_controls[0],
206 ARRAY_SIZE(wm8510_mono_mixer_controls)),
207SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3, 0, 0),
208SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2, 0, 0),
209SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1, 6, 0, NULL, 0),
210SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3, 5, 0, NULL, 0),
211SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3, 6, 0, NULL, 0),
212SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3, 7, 0, NULL, 0),
213
Mark Brown2b5f34c2008-10-07 16:13:50 +0100214SND_SOC_DAPM_MIXER("Mic PGA", WM8510_POWER2, 2, 0,
215 &wm8510_micpga_controls[0],
216 ARRAY_SIZE(wm8510_micpga_controls)),
Mark Brown5d421512008-06-05 13:49:32 +0100217SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2, 4, 0,
218 &wm8510_boost_controls[0],
219 ARRAY_SIZE(wm8510_boost_controls)),
220
221SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_POWER1, 4, 0),
222
223SND_SOC_DAPM_INPUT("MICN"),
224SND_SOC_DAPM_INPUT("MICP"),
225SND_SOC_DAPM_INPUT("AUX"),
226SND_SOC_DAPM_OUTPUT("MONOOUT"),
227SND_SOC_DAPM_OUTPUT("SPKOUTP"),
228SND_SOC_DAPM_OUTPUT("SPKOUTN"),
229};
230
231static const struct snd_soc_dapm_route audio_map[] = {
232 /* Mono output mixer */
233 {"Mono Mixer", "PCM Playback Switch", "DAC"},
234 {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
235 {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
236
237 /* Speaker output mixer */
238 {"Speaker Mixer", "PCM Playback Switch", "DAC"},
239 {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
240 {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
241
242 /* Outputs */
243 {"Mono Out", NULL, "Mono Mixer"},
244 {"MONOOUT", NULL, "Mono Out"},
245 {"SpkN Out", NULL, "Speaker Mixer"},
246 {"SpkP Out", NULL, "Speaker Mixer"},
247 {"SPKOUTN", NULL, "SpkN Out"},
248 {"SPKOUTP", NULL, "SpkP Out"},
249
250 /* Microphone PGA */
251 {"Mic PGA", "MICN Switch", "MICN"},
252 {"Mic PGA", "MICP Switch", "MICP"},
253 { "Mic PGA", "AUX Switch", "Aux Input" },
254
255 /* Boost Mixer */
256 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
257 {"Boost Mixer", "Mic Volume", "MICP"},
258 {"Boost Mixer", "Aux Volume", "Aux Input"},
259
260 {"ADC", NULL, "Boost Mixer"},
261};
262
263static int wm8510_add_widgets(struct snd_soc_codec *codec)
264{
265 snd_soc_dapm_new_controls(codec, wm8510_dapm_widgets,
266 ARRAY_SIZE(wm8510_dapm_widgets));
267
268 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
269
270 snd_soc_dapm_new_widgets(codec);
271 return 0;
272}
273
274struct pll_ {
275 unsigned int pre_div:4; /* prescale - 1 */
276 unsigned int n:4;
277 unsigned int k;
278};
279
280static struct pll_ pll_div;
281
282/* The size in bits of the pll divide multiplied by 10
283 * to allow rounding later */
284#define FIXED_PLL_SIZE ((1 << 24) * 10)
285
286static void pll_factors(unsigned int target, unsigned int source)
287{
288 unsigned long long Kpart;
289 unsigned int K, Ndiv, Nmod;
290
291 Ndiv = target / source;
292 if (Ndiv < 6) {
293 source >>= 1;
294 pll_div.pre_div = 1;
295 Ndiv = target / source;
296 } else
297 pll_div.pre_div = 0;
298
299 if ((Ndiv < 6) || (Ndiv > 12))
300 printk(KERN_WARNING
301 "WM8510 N value %d outwith recommended range!d\n",
302 Ndiv);
303
304 pll_div.n = Ndiv;
305 Nmod = target % source;
306 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
307
308 do_div(Kpart, source);
309
310 K = Kpart & 0xFFFFFFFF;
311
312 /* Check if we need to round */
313 if ((K % 10) >= 5)
314 K += 5;
315
316 /* Move down to proper range now rounding is done */
317 K /= 10;
318
319 pll_div.k = K;
320}
321
Liam Girdwoode550e172008-07-07 16:07:52 +0100322static int wm8510_set_dai_pll(struct snd_soc_dai *codec_dai,
Mark Brown5d421512008-06-05 13:49:32 +0100323 int pll_id, unsigned int freq_in, unsigned int freq_out)
324{
325 struct snd_soc_codec *codec = codec_dai->codec;
326 u16 reg;
327
328 if (freq_in == 0 || freq_out == 0) {
329 /* Clock CODEC directly from MCLK */
330 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK);
331 wm8510_write(codec, WM8510_CLOCK, reg & 0x0ff);
332
333 /* Turn off PLL */
334 reg = wm8510_read_reg_cache(codec, WM8510_POWER1);
335 wm8510_write(codec, WM8510_POWER1, reg & 0x1df);
336 return 0;
337 }
338
339 pll_factors(freq_out*8, freq_in);
340
341 wm8510_write(codec, WM8510_PLLN, (pll_div.pre_div << 4) | pll_div.n);
342 wm8510_write(codec, WM8510_PLLK1, pll_div.k >> 18);
343 wm8510_write(codec, WM8510_PLLK2, (pll_div.k >> 9) & 0x1ff);
344 wm8510_write(codec, WM8510_PLLK3, pll_div.k & 0x1ff);
345 reg = wm8510_read_reg_cache(codec, WM8510_POWER1);
346 wm8510_write(codec, WM8510_POWER1, reg | 0x020);
347
348 /* Run CODEC from PLL instead of MCLK */
349 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK);
350 wm8510_write(codec, WM8510_CLOCK, reg | 0x100);
351
352 return 0;
353}
354
355/*
356 * Configure WM8510 clock dividers.
357 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100358static int wm8510_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
Mark Brown5d421512008-06-05 13:49:32 +0100359 int div_id, int div)
360{
361 struct snd_soc_codec *codec = codec_dai->codec;
362 u16 reg;
363
364 switch (div_id) {
365 case WM8510_OPCLKDIV:
366 reg = wm8510_read_reg_cache(codec, WM8510_GPIO) & 0x1cf;
367 wm8510_write(codec, WM8510_GPIO, reg | div);
368 break;
369 case WM8510_MCLKDIV:
370 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1f;
371 wm8510_write(codec, WM8510_CLOCK, reg | div);
372 break;
373 case WM8510_ADCCLK:
374 reg = wm8510_read_reg_cache(codec, WM8510_ADC) & 0x1f7;
375 wm8510_write(codec, WM8510_ADC, reg | div);
376 break;
377 case WM8510_DACCLK:
378 reg = wm8510_read_reg_cache(codec, WM8510_DAC) & 0x1f7;
379 wm8510_write(codec, WM8510_DAC, reg | div);
380 break;
381 case WM8510_BCLKDIV:
382 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1e3;
383 wm8510_write(codec, WM8510_CLOCK, reg | div);
384 break;
385 default:
386 return -EINVAL;
387 }
388
389 return 0;
390}
391
Liam Girdwoode550e172008-07-07 16:07:52 +0100392static int wm8510_set_dai_fmt(struct snd_soc_dai *codec_dai,
Mark Brown5d421512008-06-05 13:49:32 +0100393 unsigned int fmt)
394{
395 struct snd_soc_codec *codec = codec_dai->codec;
396 u16 iface = 0;
397 u16 clk = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1fe;
398
399 /* set master/slave audio interface */
400 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
401 case SND_SOC_DAIFMT_CBM_CFM:
402 clk |= 0x0001;
403 break;
404 case SND_SOC_DAIFMT_CBS_CFS:
405 break;
406 default:
407 return -EINVAL;
408 }
409
410 /* interface format */
411 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
412 case SND_SOC_DAIFMT_I2S:
413 iface |= 0x0010;
414 break;
415 case SND_SOC_DAIFMT_RIGHT_J:
416 break;
417 case SND_SOC_DAIFMT_LEFT_J:
418 iface |= 0x0008;
419 break;
420 case SND_SOC_DAIFMT_DSP_A:
421 iface |= 0x00018;
422 break;
423 default:
424 return -EINVAL;
425 }
426
427 /* clock inversion */
428 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
429 case SND_SOC_DAIFMT_NB_NF:
430 break;
431 case SND_SOC_DAIFMT_IB_IF:
432 iface |= 0x0180;
433 break;
434 case SND_SOC_DAIFMT_IB_NF:
435 iface |= 0x0100;
436 break;
437 case SND_SOC_DAIFMT_NB_IF:
438 iface |= 0x0080;
439 break;
440 default:
441 return -EINVAL;
442 }
443
444 wm8510_write(codec, WM8510_IFACE, iface);
445 wm8510_write(codec, WM8510_CLOCK, clk);
446 return 0;
447}
448
449static int wm8510_pcm_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000450 struct snd_pcm_hw_params *params,
451 struct snd_soc_dai *dai)
Mark Brown5d421512008-06-05 13:49:32 +0100452{
453 struct snd_soc_pcm_runtime *rtd = substream->private_data;
454 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000455 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100456 u16 iface = wm8510_read_reg_cache(codec, WM8510_IFACE) & 0x19f;
457 u16 adn = wm8510_read_reg_cache(codec, WM8510_ADD) & 0x1f1;
458
459 /* bit size */
460 switch (params_format(params)) {
461 case SNDRV_PCM_FORMAT_S16_LE:
462 break;
463 case SNDRV_PCM_FORMAT_S20_3LE:
464 iface |= 0x0020;
465 break;
466 case SNDRV_PCM_FORMAT_S24_LE:
467 iface |= 0x0040;
468 break;
469 case SNDRV_PCM_FORMAT_S32_LE:
470 iface |= 0x0060;
471 break;
472 }
473
474 /* filter coefficient */
475 switch (params_rate(params)) {
476 case SNDRV_PCM_RATE_8000:
477 adn |= 0x5 << 1;
478 break;
479 case SNDRV_PCM_RATE_11025:
480 adn |= 0x4 << 1;
481 break;
482 case SNDRV_PCM_RATE_16000:
483 adn |= 0x3 << 1;
484 break;
485 case SNDRV_PCM_RATE_22050:
486 adn |= 0x2 << 1;
487 break;
488 case SNDRV_PCM_RATE_32000:
489 adn |= 0x1 << 1;
490 break;
491 case SNDRV_PCM_RATE_44100:
492 case SNDRV_PCM_RATE_48000:
493 break;
494 }
495
496 wm8510_write(codec, WM8510_IFACE, iface);
497 wm8510_write(codec, WM8510_ADD, adn);
498 return 0;
499}
500
Liam Girdwoode550e172008-07-07 16:07:52 +0100501static int wm8510_mute(struct snd_soc_dai *dai, int mute)
Mark Brown5d421512008-06-05 13:49:32 +0100502{
503 struct snd_soc_codec *codec = dai->codec;
504 u16 mute_reg = wm8510_read_reg_cache(codec, WM8510_DAC) & 0xffbf;
505
506 if (mute)
507 wm8510_write(codec, WM8510_DAC, mute_reg | 0x40);
508 else
509 wm8510_write(codec, WM8510_DAC, mute_reg);
510 return 0;
511}
512
513/* liam need to make this lower power with dapm */
514static int wm8510_set_bias_level(struct snd_soc_codec *codec,
515 enum snd_soc_bias_level level)
516{
Mark Brown09af98b2008-10-07 13:04:58 +0100517 u16 power1 = wm8510_read_reg_cache(codec, WM8510_POWER1) & ~0x3;
Mark Brown5d421512008-06-05 13:49:32 +0100518
519 switch (level) {
520 case SND_SOC_BIAS_ON:
Mark Brown5d421512008-06-05 13:49:32 +0100521 case SND_SOC_BIAS_PREPARE:
Mark Brown09af98b2008-10-07 13:04:58 +0100522 power1 |= 0x1; /* VMID 50k */
523 wm8510_write(codec, WM8510_POWER1, power1);
Mark Brown5d421512008-06-05 13:49:32 +0100524 break;
Mark Brown09af98b2008-10-07 13:04:58 +0100525
526 case SND_SOC_BIAS_STANDBY:
527 power1 |= WM8510_POWER1_BIASEN | WM8510_POWER1_BUFIOEN;
528
529 if (codec->bias_level == SND_SOC_BIAS_OFF) {
530 /* Initial cap charge at VMID 5k */
531 wm8510_write(codec, WM8510_POWER1, power1 | 0x3);
532 mdelay(100);
533 }
534
535 power1 |= 0x2; /* VMID 500k */
536 wm8510_write(codec, WM8510_POWER1, power1);
537 break;
538
Mark Brown5d421512008-06-05 13:49:32 +0100539 case SND_SOC_BIAS_OFF:
Mark Brown09af98b2008-10-07 13:04:58 +0100540 wm8510_write(codec, WM8510_POWER1, 0);
541 wm8510_write(codec, WM8510_POWER2, 0);
542 wm8510_write(codec, WM8510_POWER3, 0);
Mark Brown5d421512008-06-05 13:49:32 +0100543 break;
544 }
Mark Brown09af98b2008-10-07 13:04:58 +0100545
Mark Brown5d421512008-06-05 13:49:32 +0100546 codec->bias_level = level;
547 return 0;
548}
549
550#define WM8510_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
551 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
552 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
553
554#define WM8510_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
555 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
556
Eric Miao6335d052009-03-03 09:41:00 +0800557static struct snd_soc_dai_ops wm8510_dai_ops = {
558 .hw_params = wm8510_pcm_hw_params,
559 .digital_mute = wm8510_mute,
560 .set_fmt = wm8510_set_dai_fmt,
561 .set_clkdiv = wm8510_set_dai_clkdiv,
562 .set_pll = wm8510_set_dai_pll,
563};
564
Liam Girdwoode550e172008-07-07 16:07:52 +0100565struct snd_soc_dai wm8510_dai = {
Mark Brown5d421512008-06-05 13:49:32 +0100566 .name = "WM8510 HiFi",
567 .playback = {
568 .stream_name = "Playback",
569 .channels_min = 2,
570 .channels_max = 2,
571 .rates = WM8510_RATES,
572 .formats = WM8510_FORMATS,},
573 .capture = {
574 .stream_name = "Capture",
575 .channels_min = 2,
576 .channels_max = 2,
577 .rates = WM8510_RATES,
578 .formats = WM8510_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800579 .ops = &wm8510_dai_ops,
Mark Brown5d421512008-06-05 13:49:32 +0100580};
581EXPORT_SYMBOL_GPL(wm8510_dai);
582
583static int wm8510_suspend(struct platform_device *pdev, pm_message_t state)
584{
585 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000586 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100587
588 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
589 return 0;
590}
591
592static int wm8510_resume(struct platform_device *pdev)
593{
594 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000595 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100596 int i;
597 u8 data[2];
598 u16 *cache = codec->reg_cache;
599
600 /* Sync reg_cache with the hardware */
601 for (i = 0; i < ARRAY_SIZE(wm8510_reg); i++) {
602 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
603 data[1] = cache[i] & 0x00ff;
604 codec->hw_write(codec->control_data, data, 2);
605 }
606 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
607 wm8510_set_bias_level(codec, codec->suspend_bias_level);
608 return 0;
609}
610
611/*
612 * initialise the WM8510 driver
613 * register the mixer and dsp interfaces with the kernel
614 */
615static int wm8510_init(struct snd_soc_device *socdev)
616{
Mark Brown6627a652009-01-23 22:55:23 +0000617 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100618 int ret = 0;
619
620 codec->name = "WM8510";
621 codec->owner = THIS_MODULE;
622 codec->read = wm8510_read_reg_cache;
623 codec->write = wm8510_write;
624 codec->set_bias_level = wm8510_set_bias_level;
625 codec->dai = &wm8510_dai;
626 codec->num_dai = 1;
627 codec->reg_cache_size = ARRAY_SIZE(wm8510_reg);
628 codec->reg_cache = kmemdup(wm8510_reg, sizeof(wm8510_reg), GFP_KERNEL);
629
630 if (codec->reg_cache == NULL)
631 return -ENOMEM;
632
633 wm8510_reset(codec);
634
635 /* register pcms */
636 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
637 if (ret < 0) {
638 printk(KERN_ERR "wm8510: failed to create pcms\n");
639 goto pcm_err;
640 }
641
642 /* power on device */
Mark Brown09af98b2008-10-07 13:04:58 +0100643 codec->bias_level = SND_SOC_BIAS_OFF;
Mark Brown5d421512008-06-05 13:49:32 +0100644 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Ian Molton3e8e1952009-01-09 00:23:21 +0000645 snd_soc_add_controls(codec, wm8510_snd_controls,
646 ARRAY_SIZE(wm8510_snd_controls));
Mark Brown5d421512008-06-05 13:49:32 +0100647 wm8510_add_widgets(codec);
Mark Brown968a6022008-11-28 11:49:07 +0000648 ret = snd_soc_init_card(socdev);
Mark Brown5d421512008-06-05 13:49:32 +0100649 if (ret < 0) {
650 printk(KERN_ERR "wm8510: failed to register card\n");
651 goto card_err;
652 }
653 return ret;
654
655card_err:
656 snd_soc_free_pcms(socdev);
657 snd_soc_dapm_free(socdev);
658pcm_err:
659 kfree(codec->reg_cache);
660 return ret;
661}
662
663static struct snd_soc_device *wm8510_socdev;
664
665#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
666
667/*
668 * WM8510 2 wire address is 0x1a
669 */
Mark Brown5d421512008-06-05 13:49:32 +0100670
Jean Delvare41759c22008-09-02 17:07:30 +0200671static int wm8510_i2c_probe(struct i2c_client *i2c,
672 const struct i2c_device_id *id)
Mark Brown5d421512008-06-05 13:49:32 +0100673{
674 struct snd_soc_device *socdev = wm8510_socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000675 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100676 int ret;
677
Mark Brown5d421512008-06-05 13:49:32 +0100678 i2c_set_clientdata(i2c, codec);
679 codec->control_data = i2c;
680
Mark Brown5d421512008-06-05 13:49:32 +0100681 ret = wm8510_init(socdev);
Jean Delvare41759c22008-09-02 17:07:30 +0200682 if (ret < 0)
Mark Browna5c95e92008-06-23 14:51:29 +0100683 pr_err("failed to initialise WM8510\n");
Mark Brown5d421512008-06-05 13:49:32 +0100684
Mark Brown5d421512008-06-05 13:49:32 +0100685 return ret;
686}
687
Jean Delvare41759c22008-09-02 17:07:30 +0200688static int wm8510_i2c_remove(struct i2c_client *client)
Mark Brown5d421512008-06-05 13:49:32 +0100689{
690 struct snd_soc_codec *codec = i2c_get_clientdata(client);
Mark Brown5d421512008-06-05 13:49:32 +0100691 kfree(codec->reg_cache);
Mark Brown5d421512008-06-05 13:49:32 +0100692 return 0;
693}
694
Jean Delvare41759c22008-09-02 17:07:30 +0200695static const struct i2c_device_id wm8510_i2c_id[] = {
696 { "wm8510", 0 },
697 { }
698};
699MODULE_DEVICE_TABLE(i2c, wm8510_i2c_id);
Mark Brown5d421512008-06-05 13:49:32 +0100700
Mark Brown5d421512008-06-05 13:49:32 +0100701static struct i2c_driver wm8510_i2c_driver = {
702 .driver = {
703 .name = "WM8510 I2C Codec",
704 .owner = THIS_MODULE,
705 },
Jean Delvare41759c22008-09-02 17:07:30 +0200706 .probe = wm8510_i2c_probe,
707 .remove = wm8510_i2c_remove,
708 .id_table = wm8510_i2c_id,
Mark Brown5d421512008-06-05 13:49:32 +0100709};
710
Jean Delvare41759c22008-09-02 17:07:30 +0200711static int wm8510_add_i2c_device(struct platform_device *pdev,
712 const struct wm8510_setup_data *setup)
713{
714 struct i2c_board_info info;
715 struct i2c_adapter *adapter;
716 struct i2c_client *client;
717 int ret;
718
719 ret = i2c_add_driver(&wm8510_i2c_driver);
720 if (ret != 0) {
721 dev_err(&pdev->dev, "can't add i2c driver\n");
722 return ret;
723 }
724
725 memset(&info, 0, sizeof(struct i2c_board_info));
726 info.addr = setup->i2c_address;
727 strlcpy(info.type, "wm8510", I2C_NAME_SIZE);
728
729 adapter = i2c_get_adapter(setup->i2c_bus);
730 if (!adapter) {
731 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
732 setup->i2c_bus);
733 goto err_driver;
734 }
735
736 client = i2c_new_device(adapter, &info);
737 i2c_put_adapter(adapter);
738 if (!client) {
739 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
740 (unsigned int)info.addr);
741 goto err_driver;
742 }
743
744 return 0;
745
746err_driver:
747 i2c_del_driver(&wm8510_i2c_driver);
748 return -ENODEV;
749}
Mark Brown5d421512008-06-05 13:49:32 +0100750#endif
751
Mark Brown5e357952008-10-07 11:56:20 +0100752#if defined(CONFIG_SPI_MASTER)
753static int __devinit wm8510_spi_probe(struct spi_device *spi)
754{
755 struct snd_soc_device *socdev = wm8510_socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000756 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5e357952008-10-07 11:56:20 +0100757 int ret;
758
759 codec->control_data = spi;
760
761 ret = wm8510_init(socdev);
762 if (ret < 0)
763 dev_err(&spi->dev, "failed to initialise WM8510\n");
764
765 return ret;
766}
767
768static int __devexit wm8510_spi_remove(struct spi_device *spi)
769{
770 return 0;
771}
772
773static struct spi_driver wm8510_spi_driver = {
774 .driver = {
775 .name = "wm8510",
776 .bus = &spi_bus_type,
777 .owner = THIS_MODULE,
778 },
779 .probe = wm8510_spi_probe,
780 .remove = __devexit_p(wm8510_spi_remove),
781};
782
783static int wm8510_spi_write(struct spi_device *spi, const char *data, int len)
784{
785 struct spi_transfer t;
786 struct spi_message m;
787 u8 msg[2];
788
789 if (len <= 0)
790 return 0;
791
792 msg[0] = data[0];
793 msg[1] = data[1];
794
795 spi_message_init(&m);
796 memset(&t, 0, (sizeof t));
797
798 t.tx_buf = &msg[0];
799 t.len = len;
800
801 spi_message_add_tail(&t, &m);
802 spi_sync(spi, &m);
803
804 return len;
805}
806#endif /* CONFIG_SPI_MASTER */
807
Mark Brown5d421512008-06-05 13:49:32 +0100808static int wm8510_probe(struct platform_device *pdev)
809{
810 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
811 struct wm8510_setup_data *setup;
812 struct snd_soc_codec *codec;
813 int ret = 0;
814
Mark Browna5c95e92008-06-23 14:51:29 +0100815 pr_info("WM8510 Audio Codec %s", WM8510_VERSION);
Mark Brown5d421512008-06-05 13:49:32 +0100816
817 setup = socdev->codec_data;
818 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
819 if (codec == NULL)
820 return -ENOMEM;
821
Mark Brown6627a652009-01-23 22:55:23 +0000822 socdev->card->codec = codec;
Mark Brown5d421512008-06-05 13:49:32 +0100823 mutex_init(&codec->mutex);
824 INIT_LIST_HEAD(&codec->dapm_widgets);
825 INIT_LIST_HEAD(&codec->dapm_paths);
826
827 wm8510_socdev = socdev;
828#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
829 if (setup->i2c_address) {
Mark Brown5d421512008-06-05 13:49:32 +0100830 codec->hw_write = (hw_write_t)i2c_master_send;
Jean Delvare41759c22008-09-02 17:07:30 +0200831 ret = wm8510_add_i2c_device(pdev, setup);
Mark Brown5d421512008-06-05 13:49:32 +0100832 }
Mark Brown5e357952008-10-07 11:56:20 +0100833#endif
834#if defined(CONFIG_SPI_MASTER)
835 if (setup->spi) {
836 codec->hw_write = (hw_write_t)wm8510_spi_write;
837 ret = spi_register_driver(&wm8510_spi_driver);
838 if (ret != 0)
839 printk(KERN_ERR "can't add spi driver");
840 }
Mark Brown5d421512008-06-05 13:49:32 +0100841#endif
Jean Delvare3051e412008-08-25 11:49:20 +0100842
843 if (ret != 0)
844 kfree(codec);
Mark Brown5d421512008-06-05 13:49:32 +0100845 return ret;
846}
847
848/* power down chip */
849static int wm8510_remove(struct platform_device *pdev)
850{
851 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000852 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100853
854 if (codec->control_data)
855 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
856
857 snd_soc_free_pcms(socdev);
858 snd_soc_dapm_free(socdev);
859#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jean Delvare41759c22008-09-02 17:07:30 +0200860 i2c_unregister_device(codec->control_data);
Mark Brown5d421512008-06-05 13:49:32 +0100861 i2c_del_driver(&wm8510_i2c_driver);
862#endif
Mark Brown5e357952008-10-07 11:56:20 +0100863#if defined(CONFIG_SPI_MASTER)
864 spi_unregister_driver(&wm8510_spi_driver);
865#endif
Mark Brown5d421512008-06-05 13:49:32 +0100866 kfree(codec);
867
868 return 0;
869}
870
871struct snd_soc_codec_device soc_codec_dev_wm8510 = {
872 .probe = wm8510_probe,
873 .remove = wm8510_remove,
874 .suspend = wm8510_suspend,
875 .resume = wm8510_resume,
876};
877EXPORT_SYMBOL_GPL(soc_codec_dev_wm8510);
878
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100879static int __init wm8510_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000880{
881 return snd_soc_register_dai(&wm8510_dai);
882}
883module_init(wm8510_modinit);
884
885static void __exit wm8510_exit(void)
886{
887 snd_soc_unregister_dai(&wm8510_dai);
888}
889module_exit(wm8510_exit);
890
Mark Brown5d421512008-06-05 13:49:32 +0100891MODULE_DESCRIPTION("ASoC WM8510 driver");
892MODULE_AUTHOR("Liam Girdwood");
893MODULE_LICENSE("GPL");