blob: af8cb6995a1f5d053ffda2e348c6cf489270d5f2 [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 Brown17a52fd2009-07-05 17:24:50 +010061#define wm8510_reset(c) snd_soc_write(c, WM8510_RESET, 0)
Mark Brown5d421512008-06-05 13:49:32 +010062
63static const char *wm8510_companding[] = { "Off", "NC", "u-law", "A-law" };
64static const char *wm8510_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
65static const char *wm8510_alc[] = { "ALC", "Limiter" };
66
67static 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),
72};
73
74static const struct snd_kcontrol_new wm8510_snd_controls[] = {
75
76SOC_SINGLE("Digital Loopback Switch", WM8510_COMP, 0, 1, 0),
77
78SOC_ENUM("DAC Companding", wm8510_enum[1]),
79SOC_ENUM("ADC Companding", wm8510_enum[0]),
80
81SOC_ENUM("Playback De-emphasis", wm8510_enum[2]),
82SOC_SINGLE("DAC Inversion Switch", WM8510_DAC, 0, 1, 0),
83
84SOC_SINGLE("Master Playback Volume", WM8510_DACVOL, 0, 127, 0),
85
86SOC_SINGLE("High Pass Filter Switch", WM8510_ADC, 8, 1, 0),
87SOC_SINGLE("High Pass Cut Off", WM8510_ADC, 4, 7, 0),
88SOC_SINGLE("ADC Inversion Switch", WM8510_COMP, 0, 1, 0),
89
90SOC_SINGLE("Capture Volume", WM8510_ADCVOL, 0, 127, 0),
91
92SOC_SINGLE("DAC Playback Limiter Switch", WM8510_DACLIM1, 8, 1, 0),
93SOC_SINGLE("DAC Playback Limiter Decay", WM8510_DACLIM1, 4, 15, 0),
94SOC_SINGLE("DAC Playback Limiter Attack", WM8510_DACLIM1, 0, 15, 0),
95
96SOC_SINGLE("DAC Playback Limiter Threshold", WM8510_DACLIM2, 4, 7, 0),
97SOC_SINGLE("DAC Playback Limiter Boost", WM8510_DACLIM2, 0, 15, 0),
98
99SOC_SINGLE("ALC Enable Switch", WM8510_ALC1, 8, 1, 0),
100SOC_SINGLE("ALC Capture Max Gain", WM8510_ALC1, 3, 7, 0),
101SOC_SINGLE("ALC Capture Min Gain", WM8510_ALC1, 0, 7, 0),
102
103SOC_SINGLE("ALC Capture ZC Switch", WM8510_ALC2, 8, 1, 0),
104SOC_SINGLE("ALC Capture Hold", WM8510_ALC2, 4, 7, 0),
105SOC_SINGLE("ALC Capture Target", WM8510_ALC2, 0, 15, 0),
106
107SOC_ENUM("ALC Capture Mode", wm8510_enum[3]),
108SOC_SINGLE("ALC Capture Decay", WM8510_ALC3, 4, 15, 0),
109SOC_SINGLE("ALC Capture Attack", WM8510_ALC3, 0, 15, 0),
110
111SOC_SINGLE("ALC Capture Noise Gate Switch", WM8510_NGATE, 3, 1, 0),
112SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8510_NGATE, 0, 7, 0),
113
114SOC_SINGLE("Capture PGA ZC Switch", WM8510_INPPGA, 7, 1, 0),
115SOC_SINGLE("Capture PGA Volume", WM8510_INPPGA, 0, 63, 0),
116
117SOC_SINGLE("Speaker Playback ZC Switch", WM8510_SPKVOL, 7, 1, 0),
118SOC_SINGLE("Speaker Playback Switch", WM8510_SPKVOL, 6, 1, 1),
119SOC_SINGLE("Speaker Playback Volume", WM8510_SPKVOL, 0, 63, 0),
120SOC_SINGLE("Speaker Boost", WM8510_OUTPUT, 2, 1, 0),
121
122SOC_SINGLE("Capture Boost(+20dB)", WM8510_ADCBOOST, 8, 1, 0),
123SOC_SINGLE("Mono Playback Switch", WM8510_MONOMIX, 6, 1, 1),
124};
125
Mark Brown5d421512008-06-05 13:49:32 +0100126/* Speaker Output Mixer */
127static const struct snd_kcontrol_new wm8510_speaker_mixer_controls[] = {
128SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_SPKMIX, 1, 1, 0),
129SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_SPKMIX, 5, 1, 0),
130SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_SPKMIX, 0, 1, 0),
131};
132
133/* Mono Output Mixer */
134static const struct snd_kcontrol_new wm8510_mono_mixer_controls[] = {
135SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX, 1, 1, 0),
136SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX, 2, 1, 0),
137SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX, 0, 1, 0),
138};
139
140static const struct snd_kcontrol_new wm8510_boost_controls[] = {
Mark Brown8ae23ec2008-10-06 11:33:21 +0100141SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA, 6, 1, 1),
Mark Brown5d421512008-06-05 13:49:32 +0100142SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST, 0, 7, 0),
143SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST, 4, 7, 0),
144};
145
146static const struct snd_kcontrol_new wm8510_micpga_controls[] = {
147SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT, 0, 1, 0),
148SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT, 1, 1, 0),
149SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT, 2, 1, 0),
150};
151
152static const struct snd_soc_dapm_widget wm8510_dapm_widgets[] = {
153SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3, 2, 0,
154 &wm8510_speaker_mixer_controls[0],
155 ARRAY_SIZE(wm8510_speaker_mixer_controls)),
156SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3, 3, 0,
157 &wm8510_mono_mixer_controls[0],
158 ARRAY_SIZE(wm8510_mono_mixer_controls)),
159SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3, 0, 0),
160SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2, 0, 0),
161SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1, 6, 0, NULL, 0),
162SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3, 5, 0, NULL, 0),
163SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3, 6, 0, NULL, 0),
164SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3, 7, 0, NULL, 0),
165
Mark Brown2b5f34c2008-10-07 16:13:50 +0100166SND_SOC_DAPM_MIXER("Mic PGA", WM8510_POWER2, 2, 0,
167 &wm8510_micpga_controls[0],
168 ARRAY_SIZE(wm8510_micpga_controls)),
Mark Brown5d421512008-06-05 13:49:32 +0100169SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2, 4, 0,
170 &wm8510_boost_controls[0],
171 ARRAY_SIZE(wm8510_boost_controls)),
172
173SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_POWER1, 4, 0),
174
175SND_SOC_DAPM_INPUT("MICN"),
176SND_SOC_DAPM_INPUT("MICP"),
177SND_SOC_DAPM_INPUT("AUX"),
178SND_SOC_DAPM_OUTPUT("MONOOUT"),
179SND_SOC_DAPM_OUTPUT("SPKOUTP"),
180SND_SOC_DAPM_OUTPUT("SPKOUTN"),
181};
182
183static 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"},
188
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"},
193
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"},
201
202 /* Microphone PGA */
203 {"Mic PGA", "MICN Switch", "MICN"},
204 {"Mic PGA", "MICP Switch", "MICP"},
205 { "Mic PGA", "AUX Switch", "Aux Input" },
206
207 /* Boost Mixer */
208 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
209 {"Boost Mixer", "Mic Volume", "MICP"},
210 {"Boost Mixer", "Aux Volume", "Aux Input"},
211
212 {"ADC", NULL, "Boost Mixer"},
213};
214
215static int wm8510_add_widgets(struct snd_soc_codec *codec)
216{
217 snd_soc_dapm_new_controls(codec, wm8510_dapm_widgets,
218 ARRAY_SIZE(wm8510_dapm_widgets));
219
220 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
221
Mark Brown5d421512008-06-05 13:49:32 +0100222 return 0;
223}
224
225struct pll_ {
226 unsigned int pre_div:4; /* prescale - 1 */
227 unsigned int n:4;
228 unsigned int k;
229};
230
231static struct pll_ pll_div;
232
233/* The size in bits of the pll divide multiplied by 10
234 * to allow rounding later */
235#define FIXED_PLL_SIZE ((1 << 24) * 10)
236
237static void pll_factors(unsigned int target, unsigned int source)
238{
239 unsigned long long Kpart;
240 unsigned int K, Ndiv, Nmod;
241
242 Ndiv = target / source;
243 if (Ndiv < 6) {
244 source >>= 1;
245 pll_div.pre_div = 1;
246 Ndiv = target / source;
247 } else
248 pll_div.pre_div = 0;
249
250 if ((Ndiv < 6) || (Ndiv > 12))
251 printk(KERN_WARNING
Roel Kluin449bd542009-05-27 17:08:39 -0700252 "WM8510 N value %u outwith recommended range!d\n",
Mark Brown5d421512008-06-05 13:49:32 +0100253 Ndiv);
254
255 pll_div.n = Ndiv;
256 Nmod = target % source;
257 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
258
259 do_div(Kpart, source);
260
261 K = Kpart & 0xFFFFFFFF;
262
263 /* Check if we need to round */
264 if ((K % 10) >= 5)
265 K += 5;
266
267 /* Move down to proper range now rounding is done */
268 K /= 10;
269
270 pll_div.k = K;
271}
272
Mark Brown85488032009-09-05 18:52:16 +0100273static int wm8510_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
274 int source, unsigned int freq_in, unsigned int freq_out)
Mark Brown5d421512008-06-05 13:49:32 +0100275{
276 struct snd_soc_codec *codec = codec_dai->codec;
277 u16 reg;
278
279 if (freq_in == 0 || freq_out == 0) {
280 /* Clock CODEC directly from MCLK */
Mark Brown17a52fd2009-07-05 17:24:50 +0100281 reg = snd_soc_read(codec, WM8510_CLOCK);
282 snd_soc_write(codec, WM8510_CLOCK, reg & 0x0ff);
Mark Brown5d421512008-06-05 13:49:32 +0100283
284 /* Turn off PLL */
Mark Brown17a52fd2009-07-05 17:24:50 +0100285 reg = snd_soc_read(codec, WM8510_POWER1);
286 snd_soc_write(codec, WM8510_POWER1, reg & 0x1df);
Mark Brown5d421512008-06-05 13:49:32 +0100287 return 0;
288 }
289
Jonas Andersson86027ae2009-03-04 08:24:26 +0100290 pll_factors(freq_out*4, freq_in);
Mark Brown5d421512008-06-05 13:49:32 +0100291
Mark Brown17a52fd2009-07-05 17:24:50 +0100292 snd_soc_write(codec, WM8510_PLLN, (pll_div.pre_div << 4) | pll_div.n);
293 snd_soc_write(codec, WM8510_PLLK1, pll_div.k >> 18);
294 snd_soc_write(codec, WM8510_PLLK2, (pll_div.k >> 9) & 0x1ff);
295 snd_soc_write(codec, WM8510_PLLK3, pll_div.k & 0x1ff);
296 reg = snd_soc_read(codec, WM8510_POWER1);
297 snd_soc_write(codec, WM8510_POWER1, reg | 0x020);
Mark Brown5d421512008-06-05 13:49:32 +0100298
299 /* Run CODEC from PLL instead of MCLK */
Mark Brown17a52fd2009-07-05 17:24:50 +0100300 reg = snd_soc_read(codec, WM8510_CLOCK);
301 snd_soc_write(codec, WM8510_CLOCK, reg | 0x100);
Mark Brown5d421512008-06-05 13:49:32 +0100302
303 return 0;
304}
305
306/*
307 * Configure WM8510 clock dividers.
308 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100309static int wm8510_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
Mark Brown5d421512008-06-05 13:49:32 +0100310 int div_id, int div)
311{
312 struct snd_soc_codec *codec = codec_dai->codec;
313 u16 reg;
314
315 switch (div_id) {
316 case WM8510_OPCLKDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100317 reg = snd_soc_read(codec, WM8510_GPIO) & 0x1cf;
318 snd_soc_write(codec, WM8510_GPIO, reg | div);
Mark Brown5d421512008-06-05 13:49:32 +0100319 break;
320 case WM8510_MCLKDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100321 reg = snd_soc_read(codec, WM8510_CLOCK) & 0x11f;
322 snd_soc_write(codec, WM8510_CLOCK, reg | div);
Mark Brown5d421512008-06-05 13:49:32 +0100323 break;
324 case WM8510_ADCCLK:
Mark Brown17a52fd2009-07-05 17:24:50 +0100325 reg = snd_soc_read(codec, WM8510_ADC) & 0x1f7;
326 snd_soc_write(codec, WM8510_ADC, reg | div);
Mark Brown5d421512008-06-05 13:49:32 +0100327 break;
328 case WM8510_DACCLK:
Mark Brown17a52fd2009-07-05 17:24:50 +0100329 reg = snd_soc_read(codec, WM8510_DAC) & 0x1f7;
330 snd_soc_write(codec, WM8510_DAC, reg | div);
Mark Brown5d421512008-06-05 13:49:32 +0100331 break;
332 case WM8510_BCLKDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100333 reg = snd_soc_read(codec, WM8510_CLOCK) & 0x1e3;
334 snd_soc_write(codec, WM8510_CLOCK, reg | div);
Mark Brown5d421512008-06-05 13:49:32 +0100335 break;
336 default:
337 return -EINVAL;
338 }
339
340 return 0;
341}
342
Liam Girdwoode550e172008-07-07 16:07:52 +0100343static int wm8510_set_dai_fmt(struct snd_soc_dai *codec_dai,
Mark Brown5d421512008-06-05 13:49:32 +0100344 unsigned int fmt)
345{
346 struct snd_soc_codec *codec = codec_dai->codec;
347 u16 iface = 0;
Mark Brown17a52fd2009-07-05 17:24:50 +0100348 u16 clk = snd_soc_read(codec, WM8510_CLOCK) & 0x1fe;
Mark Brown5d421512008-06-05 13:49:32 +0100349
350 /* set master/slave audio interface */
351 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
352 case SND_SOC_DAIFMT_CBM_CFM:
353 clk |= 0x0001;
354 break;
355 case SND_SOC_DAIFMT_CBS_CFS:
356 break;
357 default:
358 return -EINVAL;
359 }
360
361 /* interface format */
362 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
363 case SND_SOC_DAIFMT_I2S:
364 iface |= 0x0010;
365 break;
366 case SND_SOC_DAIFMT_RIGHT_J:
367 break;
368 case SND_SOC_DAIFMT_LEFT_J:
369 iface |= 0x0008;
370 break;
371 case SND_SOC_DAIFMT_DSP_A:
372 iface |= 0x00018;
373 break;
374 default:
375 return -EINVAL;
376 }
377
378 /* clock inversion */
379 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
380 case SND_SOC_DAIFMT_NB_NF:
381 break;
382 case SND_SOC_DAIFMT_IB_IF:
383 iface |= 0x0180;
384 break;
385 case SND_SOC_DAIFMT_IB_NF:
386 iface |= 0x0100;
387 break;
388 case SND_SOC_DAIFMT_NB_IF:
389 iface |= 0x0080;
390 break;
391 default:
392 return -EINVAL;
393 }
394
Mark Brown17a52fd2009-07-05 17:24:50 +0100395 snd_soc_write(codec, WM8510_IFACE, iface);
396 snd_soc_write(codec, WM8510_CLOCK, clk);
Mark Brown5d421512008-06-05 13:49:32 +0100397 return 0;
398}
399
400static int wm8510_pcm_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000401 struct snd_pcm_hw_params *params,
402 struct snd_soc_dai *dai)
Mark Brown5d421512008-06-05 13:49:32 +0100403{
404 struct snd_soc_pcm_runtime *rtd = substream->private_data;
405 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000406 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown17a52fd2009-07-05 17:24:50 +0100407 u16 iface = snd_soc_read(codec, WM8510_IFACE) & 0x19f;
408 u16 adn = snd_soc_read(codec, WM8510_ADD) & 0x1f1;
Mark Brown5d421512008-06-05 13:49:32 +0100409
410 /* bit size */
411 switch (params_format(params)) {
412 case SNDRV_PCM_FORMAT_S16_LE:
413 break;
414 case SNDRV_PCM_FORMAT_S20_3LE:
415 iface |= 0x0020;
416 break;
417 case SNDRV_PCM_FORMAT_S24_LE:
418 iface |= 0x0040;
419 break;
420 case SNDRV_PCM_FORMAT_S32_LE:
421 iface |= 0x0060;
422 break;
423 }
424
425 /* filter coefficient */
426 switch (params_rate(params)) {
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100427 case 8000:
Mark Brown5d421512008-06-05 13:49:32 +0100428 adn |= 0x5 << 1;
429 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100430 case 11025:
Mark Brown5d421512008-06-05 13:49:32 +0100431 adn |= 0x4 << 1;
432 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100433 case 16000:
Mark Brown5d421512008-06-05 13:49:32 +0100434 adn |= 0x3 << 1;
435 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100436 case 22050:
Mark Brown5d421512008-06-05 13:49:32 +0100437 adn |= 0x2 << 1;
438 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100439 case 32000:
Mark Brown5d421512008-06-05 13:49:32 +0100440 adn |= 0x1 << 1;
441 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100442 case 44100:
443 case 48000:
Mark Brown5d421512008-06-05 13:49:32 +0100444 break;
445 }
446
Mark Brown17a52fd2009-07-05 17:24:50 +0100447 snd_soc_write(codec, WM8510_IFACE, iface);
448 snd_soc_write(codec, WM8510_ADD, adn);
Mark Brown5d421512008-06-05 13:49:32 +0100449 return 0;
450}
451
Liam Girdwoode550e172008-07-07 16:07:52 +0100452static int wm8510_mute(struct snd_soc_dai *dai, int mute)
Mark Brown5d421512008-06-05 13:49:32 +0100453{
454 struct snd_soc_codec *codec = dai->codec;
Mark Brown17a52fd2009-07-05 17:24:50 +0100455 u16 mute_reg = snd_soc_read(codec, WM8510_DAC) & 0xffbf;
Mark Brown5d421512008-06-05 13:49:32 +0100456
457 if (mute)
Mark Brown17a52fd2009-07-05 17:24:50 +0100458 snd_soc_write(codec, WM8510_DAC, mute_reg | 0x40);
Mark Brown5d421512008-06-05 13:49:32 +0100459 else
Mark Brown17a52fd2009-07-05 17:24:50 +0100460 snd_soc_write(codec, WM8510_DAC, mute_reg);
Mark Brown5d421512008-06-05 13:49:32 +0100461 return 0;
462}
463
464/* liam need to make this lower power with dapm */
465static int wm8510_set_bias_level(struct snd_soc_codec *codec,
466 enum snd_soc_bias_level level)
467{
Mark Brown17a52fd2009-07-05 17:24:50 +0100468 u16 power1 = snd_soc_read(codec, WM8510_POWER1) & ~0x3;
Mark Brown5d421512008-06-05 13:49:32 +0100469
470 switch (level) {
471 case SND_SOC_BIAS_ON:
Mark Brown5d421512008-06-05 13:49:32 +0100472 case SND_SOC_BIAS_PREPARE:
Mark Brown09af98b2008-10-07 13:04:58 +0100473 power1 |= 0x1; /* VMID 50k */
Mark Brown17a52fd2009-07-05 17:24:50 +0100474 snd_soc_write(codec, WM8510_POWER1, power1);
Mark Brown5d421512008-06-05 13:49:32 +0100475 break;
Mark Brown09af98b2008-10-07 13:04:58 +0100476
477 case SND_SOC_BIAS_STANDBY:
478 power1 |= WM8510_POWER1_BIASEN | WM8510_POWER1_BUFIOEN;
479
480 if (codec->bias_level == SND_SOC_BIAS_OFF) {
481 /* Initial cap charge at VMID 5k */
Mark Brown17a52fd2009-07-05 17:24:50 +0100482 snd_soc_write(codec, WM8510_POWER1, power1 | 0x3);
Mark Brown09af98b2008-10-07 13:04:58 +0100483 mdelay(100);
484 }
485
486 power1 |= 0x2; /* VMID 500k */
Mark Brown17a52fd2009-07-05 17:24:50 +0100487 snd_soc_write(codec, WM8510_POWER1, power1);
Mark Brown09af98b2008-10-07 13:04:58 +0100488 break;
489
Mark Brown5d421512008-06-05 13:49:32 +0100490 case SND_SOC_BIAS_OFF:
Mark Brown17a52fd2009-07-05 17:24:50 +0100491 snd_soc_write(codec, WM8510_POWER1, 0);
492 snd_soc_write(codec, WM8510_POWER2, 0);
493 snd_soc_write(codec, WM8510_POWER3, 0);
Mark Brown5d421512008-06-05 13:49:32 +0100494 break;
495 }
Mark Brown09af98b2008-10-07 13:04:58 +0100496
Mark Brown5d421512008-06-05 13:49:32 +0100497 codec->bias_level = level;
498 return 0;
499}
500
501#define WM8510_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
502 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
503 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
504
505#define WM8510_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
506 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
507
Eric Miao6335d052009-03-03 09:41:00 +0800508static struct snd_soc_dai_ops wm8510_dai_ops = {
509 .hw_params = wm8510_pcm_hw_params,
510 .digital_mute = wm8510_mute,
511 .set_fmt = wm8510_set_dai_fmt,
512 .set_clkdiv = wm8510_set_dai_clkdiv,
513 .set_pll = wm8510_set_dai_pll,
514};
515
Liam Girdwoode550e172008-07-07 16:07:52 +0100516struct snd_soc_dai wm8510_dai = {
Mark Brown5d421512008-06-05 13:49:32 +0100517 .name = "WM8510 HiFi",
518 .playback = {
519 .stream_name = "Playback",
520 .channels_min = 2,
521 .channels_max = 2,
522 .rates = WM8510_RATES,
523 .formats = WM8510_FORMATS,},
524 .capture = {
525 .stream_name = "Capture",
526 .channels_min = 2,
527 .channels_max = 2,
528 .rates = WM8510_RATES,
529 .formats = WM8510_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800530 .ops = &wm8510_dai_ops,
Mark Browncc369cf2009-07-09 11:28:07 +0100531 .symmetric_rates = 1,
Mark Brown5d421512008-06-05 13:49:32 +0100532};
533EXPORT_SYMBOL_GPL(wm8510_dai);
534
535static int wm8510_suspend(struct platform_device *pdev, pm_message_t state)
536{
537 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000538 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100539
540 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
541 return 0;
542}
543
544static int wm8510_resume(struct platform_device *pdev)
545{
546 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000547 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100548 int i;
549 u8 data[2];
550 u16 *cache = codec->reg_cache;
551
552 /* Sync reg_cache with the hardware */
553 for (i = 0; i < ARRAY_SIZE(wm8510_reg); i++) {
554 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
555 data[1] = cache[i] & 0x00ff;
556 codec->hw_write(codec->control_data, data, 2);
557 }
558 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
559 wm8510_set_bias_level(codec, codec->suspend_bias_level);
560 return 0;
561}
562
563/*
564 * initialise the WM8510 driver
565 * register the mixer and dsp interfaces with the kernel
566 */
Mark Brown7084a422009-07-10 22:24:27 +0100567static int wm8510_init(struct snd_soc_device *socdev,
568 enum snd_soc_control_type control)
Mark Brown5d421512008-06-05 13:49:32 +0100569{
Mark Brown6627a652009-01-23 22:55:23 +0000570 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100571 int ret = 0;
572
573 codec->name = "WM8510";
574 codec->owner = THIS_MODULE;
Mark Brown5d421512008-06-05 13:49:32 +0100575 codec->set_bias_level = wm8510_set_bias_level;
576 codec->dai = &wm8510_dai;
577 codec->num_dai = 1;
578 codec->reg_cache_size = ARRAY_SIZE(wm8510_reg);
579 codec->reg_cache = kmemdup(wm8510_reg, sizeof(wm8510_reg), GFP_KERNEL);
580
581 if (codec->reg_cache == NULL)
582 return -ENOMEM;
583
Mark Brown7084a422009-07-10 22:24:27 +0100584 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
Mark Brown17a52fd2009-07-05 17:24:50 +0100585 if (ret < 0) {
586 printk(KERN_ERR "wm8510: failed to set cache I/O: %d\n",
587 ret);
588 goto err;
589 }
590
Mark Brown5d421512008-06-05 13:49:32 +0100591 wm8510_reset(codec);
592
593 /* register pcms */
594 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
595 if (ret < 0) {
596 printk(KERN_ERR "wm8510: failed to create pcms\n");
Mark Brown17a52fd2009-07-05 17:24:50 +0100597 goto err;
Mark Brown5d421512008-06-05 13:49:32 +0100598 }
599
600 /* power on device */
Mark Brown09af98b2008-10-07 13:04:58 +0100601 codec->bias_level = SND_SOC_BIAS_OFF;
Mark Brown5d421512008-06-05 13:49:32 +0100602 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Ian Molton3e8e1952009-01-09 00:23:21 +0000603 snd_soc_add_controls(codec, wm8510_snd_controls,
604 ARRAY_SIZE(wm8510_snd_controls));
Mark Brown5d421512008-06-05 13:49:32 +0100605 wm8510_add_widgets(codec);
Mark Brownfe3e78e2009-11-03 22:13:13 +0000606
Mark Brown5d421512008-06-05 13:49:32 +0100607 return ret;
608
Mark Brown17a52fd2009-07-05 17:24:50 +0100609err:
Mark Brown5d421512008-06-05 13:49:32 +0100610 kfree(codec->reg_cache);
611 return ret;
612}
613
614static struct snd_soc_device *wm8510_socdev;
615
616#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
617
618/*
619 * WM8510 2 wire address is 0x1a
620 */
Mark Brown5d421512008-06-05 13:49:32 +0100621
Jean Delvare41759c22008-09-02 17:07:30 +0200622static int wm8510_i2c_probe(struct i2c_client *i2c,
623 const struct i2c_device_id *id)
Mark Brown5d421512008-06-05 13:49:32 +0100624{
625 struct snd_soc_device *socdev = wm8510_socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000626 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100627 int ret;
628
Mark Brown5d421512008-06-05 13:49:32 +0100629 i2c_set_clientdata(i2c, codec);
630 codec->control_data = i2c;
631
Mark Brown7084a422009-07-10 22:24:27 +0100632 ret = wm8510_init(socdev, SND_SOC_I2C);
Jean Delvare41759c22008-09-02 17:07:30 +0200633 if (ret < 0)
Mark Browna5c95e92008-06-23 14:51:29 +0100634 pr_err("failed to initialise WM8510\n");
Mark Brown5d421512008-06-05 13:49:32 +0100635
Mark Brown5d421512008-06-05 13:49:32 +0100636 return ret;
637}
638
Jean Delvare41759c22008-09-02 17:07:30 +0200639static int wm8510_i2c_remove(struct i2c_client *client)
Mark Brown5d421512008-06-05 13:49:32 +0100640{
641 struct snd_soc_codec *codec = i2c_get_clientdata(client);
Mark Brown5d421512008-06-05 13:49:32 +0100642 kfree(codec->reg_cache);
Mark Brown5d421512008-06-05 13:49:32 +0100643 return 0;
644}
645
Jean Delvare41759c22008-09-02 17:07:30 +0200646static const struct i2c_device_id wm8510_i2c_id[] = {
647 { "wm8510", 0 },
648 { }
649};
650MODULE_DEVICE_TABLE(i2c, wm8510_i2c_id);
Mark Brown5d421512008-06-05 13:49:32 +0100651
Mark Brown5d421512008-06-05 13:49:32 +0100652static struct i2c_driver wm8510_i2c_driver = {
653 .driver = {
654 .name = "WM8510 I2C Codec",
655 .owner = THIS_MODULE,
656 },
Jean Delvare41759c22008-09-02 17:07:30 +0200657 .probe = wm8510_i2c_probe,
658 .remove = wm8510_i2c_remove,
659 .id_table = wm8510_i2c_id,
Mark Brown5d421512008-06-05 13:49:32 +0100660};
661
Jean Delvare41759c22008-09-02 17:07:30 +0200662static int wm8510_add_i2c_device(struct platform_device *pdev,
663 const struct wm8510_setup_data *setup)
664{
665 struct i2c_board_info info;
666 struct i2c_adapter *adapter;
667 struct i2c_client *client;
668 int ret;
669
670 ret = i2c_add_driver(&wm8510_i2c_driver);
671 if (ret != 0) {
672 dev_err(&pdev->dev, "can't add i2c driver\n");
673 return ret;
674 }
675
676 memset(&info, 0, sizeof(struct i2c_board_info));
677 info.addr = setup->i2c_address;
678 strlcpy(info.type, "wm8510", I2C_NAME_SIZE);
679
680 adapter = i2c_get_adapter(setup->i2c_bus);
681 if (!adapter) {
682 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
683 setup->i2c_bus);
684 goto err_driver;
685 }
686
687 client = i2c_new_device(adapter, &info);
688 i2c_put_adapter(adapter);
689 if (!client) {
690 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
691 (unsigned int)info.addr);
692 goto err_driver;
693 }
694
695 return 0;
696
697err_driver:
698 i2c_del_driver(&wm8510_i2c_driver);
699 return -ENODEV;
700}
Mark Brown5d421512008-06-05 13:49:32 +0100701#endif
702
Mark Brown5e357952008-10-07 11:56:20 +0100703#if defined(CONFIG_SPI_MASTER)
704static int __devinit wm8510_spi_probe(struct spi_device *spi)
705{
706 struct snd_soc_device *socdev = wm8510_socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000707 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5e357952008-10-07 11:56:20 +0100708 int ret;
709
710 codec->control_data = spi;
711
Mark Brown7084a422009-07-10 22:24:27 +0100712 ret = wm8510_init(socdev, SND_SOC_SPI);
Mark Brown5e357952008-10-07 11:56:20 +0100713 if (ret < 0)
714 dev_err(&spi->dev, "failed to initialise WM8510\n");
715
716 return ret;
717}
718
719static int __devexit wm8510_spi_remove(struct spi_device *spi)
720{
721 return 0;
722}
723
724static struct spi_driver wm8510_spi_driver = {
725 .driver = {
726 .name = "wm8510",
727 .bus = &spi_bus_type,
728 .owner = THIS_MODULE,
729 },
730 .probe = wm8510_spi_probe,
731 .remove = __devexit_p(wm8510_spi_remove),
732};
Mark Brown5e357952008-10-07 11:56:20 +0100733#endif /* CONFIG_SPI_MASTER */
734
Mark Brown5d421512008-06-05 13:49:32 +0100735static int wm8510_probe(struct platform_device *pdev)
736{
737 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
738 struct wm8510_setup_data *setup;
739 struct snd_soc_codec *codec;
740 int ret = 0;
741
Mark Browna5c95e92008-06-23 14:51:29 +0100742 pr_info("WM8510 Audio Codec %s", WM8510_VERSION);
Mark Brown5d421512008-06-05 13:49:32 +0100743
744 setup = socdev->codec_data;
745 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
746 if (codec == NULL)
747 return -ENOMEM;
748
Mark Brown6627a652009-01-23 22:55:23 +0000749 socdev->card->codec = codec;
Mark Brown5d421512008-06-05 13:49:32 +0100750 mutex_init(&codec->mutex);
751 INIT_LIST_HEAD(&codec->dapm_widgets);
752 INIT_LIST_HEAD(&codec->dapm_paths);
753
754 wm8510_socdev = socdev;
755#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
756 if (setup->i2c_address) {
Jean Delvare41759c22008-09-02 17:07:30 +0200757 ret = wm8510_add_i2c_device(pdev, setup);
Mark Brown5d421512008-06-05 13:49:32 +0100758 }
Mark Brown5e357952008-10-07 11:56:20 +0100759#endif
760#if defined(CONFIG_SPI_MASTER)
761 if (setup->spi) {
Mark Brown5e357952008-10-07 11:56:20 +0100762 ret = spi_register_driver(&wm8510_spi_driver);
763 if (ret != 0)
764 printk(KERN_ERR "can't add spi driver");
765 }
Mark Brown5d421512008-06-05 13:49:32 +0100766#endif
Jean Delvare3051e412008-08-25 11:49:20 +0100767
768 if (ret != 0)
769 kfree(codec);
Mark Brown5d421512008-06-05 13:49:32 +0100770 return ret;
771}
772
773/* power down chip */
774static int wm8510_remove(struct platform_device *pdev)
775{
776 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000777 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100778
779 if (codec->control_data)
780 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
781
782 snd_soc_free_pcms(socdev);
783 snd_soc_dapm_free(socdev);
784#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jean Delvare41759c22008-09-02 17:07:30 +0200785 i2c_unregister_device(codec->control_data);
Mark Brown5d421512008-06-05 13:49:32 +0100786 i2c_del_driver(&wm8510_i2c_driver);
787#endif
Mark Brown5e357952008-10-07 11:56:20 +0100788#if defined(CONFIG_SPI_MASTER)
789 spi_unregister_driver(&wm8510_spi_driver);
790#endif
Mark Brown5d421512008-06-05 13:49:32 +0100791 kfree(codec);
792
793 return 0;
794}
795
796struct snd_soc_codec_device soc_codec_dev_wm8510 = {
797 .probe = wm8510_probe,
798 .remove = wm8510_remove,
799 .suspend = wm8510_suspend,
800 .resume = wm8510_resume,
801};
802EXPORT_SYMBOL_GPL(soc_codec_dev_wm8510);
803
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100804static int __init wm8510_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000805{
806 return snd_soc_register_dai(&wm8510_dai);
807}
808module_init(wm8510_modinit);
809
810static void __exit wm8510_exit(void)
811{
812 snd_soc_unregister_dai(&wm8510_dai);
813}
814module_exit(wm8510_exit);
815
Mark Brown5d421512008-06-05 13:49:32 +0100816MODULE_DESCRIPTION("ASoC WM8510 driver");
817MODULE_AUTHOR("Liam Girdwood");
818MODULE_LICENSE("GPL");