blob: 55a4c830e111372f7e67768076debf56275fc3cd [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mark Brown0a422e12011-08-02 13:03:04 +090023#include <linux/of_device.h>
Mark Brown5d421512008-06-05 13:49:32 +010024#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
Mark Brown5d421512008-06-05 13:49:32 +010028#include <sound/initval.h>
29
30#include "wm8510.h"
31
Mark Brown5d421512008-06-05 13:49:32 +010032/*
33 * wm8510 register cache
34 * We can't read the WM8510 register space when we are
35 * using 2 wire for device control, so we cache them instead.
36 */
37static const u16 wm8510_reg[WM8510_CACHEREGNUM] = {
38 0x0000, 0x0000, 0x0000, 0x0000,
39 0x0050, 0x0000, 0x0140, 0x0000,
40 0x0000, 0x0000, 0x0000, 0x00ff,
41 0x0000, 0x0000, 0x0100, 0x00ff,
42 0x0000, 0x0000, 0x012c, 0x002c,
43 0x002c, 0x002c, 0x002c, 0x0000,
44 0x0032, 0x0000, 0x0000, 0x0000,
45 0x0000, 0x0000, 0x0000, 0x0000,
46 0x0038, 0x000b, 0x0032, 0x0000,
47 0x0008, 0x000c, 0x0093, 0x00e9,
48 0x0000, 0x0000, 0x0000, 0x0000,
49 0x0003, 0x0010, 0x0000, 0x0000,
50 0x0000, 0x0002, 0x0001, 0x0000,
51 0x0000, 0x0000, 0x0039, 0x0000,
Mark Brown05a076d2008-06-06 17:13:53 +010052 0x0001,
Mark Brown5d421512008-06-05 13:49:32 +010053};
54
Mark Brown09af98b2008-10-07 13:04:58 +010055#define WM8510_POWER1_BIASEN 0x08
56#define WM8510_POWER1_BUFIOEN 0x10
57
Mark Brown17a52fd2009-07-05 17:24:50 +010058#define wm8510_reset(c) snd_soc_write(c, WM8510_RESET, 0)
Mark Brown5d421512008-06-05 13:49:32 +010059
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000060/* codec private data */
61struct wm8510_priv {
62 enum snd_soc_control_type control_type;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000063};
64
Mark Brown5d421512008-06-05 13:49:32 +010065static const char *wm8510_companding[] = { "Off", "NC", "u-law", "A-law" };
66static const char *wm8510_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
67static const char *wm8510_alc[] = { "ALC", "Limiter" };
68
69static const struct soc_enum wm8510_enum[] = {
70 SOC_ENUM_SINGLE(WM8510_COMP, 1, 4, wm8510_companding), /* adc */
71 SOC_ENUM_SINGLE(WM8510_COMP, 3, 4, wm8510_companding), /* dac */
72 SOC_ENUM_SINGLE(WM8510_DAC, 4, 4, wm8510_deemp),
73 SOC_ENUM_SINGLE(WM8510_ALC3, 8, 2, wm8510_alc),
74};
75
76static const struct snd_kcontrol_new wm8510_snd_controls[] = {
77
78SOC_SINGLE("Digital Loopback Switch", WM8510_COMP, 0, 1, 0),
79
80SOC_ENUM("DAC Companding", wm8510_enum[1]),
81SOC_ENUM("ADC Companding", wm8510_enum[0]),
82
83SOC_ENUM("Playback De-emphasis", wm8510_enum[2]),
84SOC_SINGLE("DAC Inversion Switch", WM8510_DAC, 0, 1, 0),
85
86SOC_SINGLE("Master Playback Volume", WM8510_DACVOL, 0, 127, 0),
87
88SOC_SINGLE("High Pass Filter Switch", WM8510_ADC, 8, 1, 0),
89SOC_SINGLE("High Pass Cut Off", WM8510_ADC, 4, 7, 0),
90SOC_SINGLE("ADC Inversion Switch", WM8510_COMP, 0, 1, 0),
91
92SOC_SINGLE("Capture Volume", WM8510_ADCVOL, 0, 127, 0),
93
94SOC_SINGLE("DAC Playback Limiter Switch", WM8510_DACLIM1, 8, 1, 0),
95SOC_SINGLE("DAC Playback Limiter Decay", WM8510_DACLIM1, 4, 15, 0),
96SOC_SINGLE("DAC Playback Limiter Attack", WM8510_DACLIM1, 0, 15, 0),
97
98SOC_SINGLE("DAC Playback Limiter Threshold", WM8510_DACLIM2, 4, 7, 0),
99SOC_SINGLE("DAC Playback Limiter Boost", WM8510_DACLIM2, 0, 15, 0),
100
101SOC_SINGLE("ALC Enable Switch", WM8510_ALC1, 8, 1, 0),
102SOC_SINGLE("ALC Capture Max Gain", WM8510_ALC1, 3, 7, 0),
103SOC_SINGLE("ALC Capture Min Gain", WM8510_ALC1, 0, 7, 0),
104
105SOC_SINGLE("ALC Capture ZC Switch", WM8510_ALC2, 8, 1, 0),
106SOC_SINGLE("ALC Capture Hold", WM8510_ALC2, 4, 7, 0),
107SOC_SINGLE("ALC Capture Target", WM8510_ALC2, 0, 15, 0),
108
109SOC_ENUM("ALC Capture Mode", wm8510_enum[3]),
110SOC_SINGLE("ALC Capture Decay", WM8510_ALC3, 4, 15, 0),
111SOC_SINGLE("ALC Capture Attack", WM8510_ALC3, 0, 15, 0),
112
113SOC_SINGLE("ALC Capture Noise Gate Switch", WM8510_NGATE, 3, 1, 0),
114SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8510_NGATE, 0, 7, 0),
115
116SOC_SINGLE("Capture PGA ZC Switch", WM8510_INPPGA, 7, 1, 0),
117SOC_SINGLE("Capture PGA Volume", WM8510_INPPGA, 0, 63, 0),
118
119SOC_SINGLE("Speaker Playback ZC Switch", WM8510_SPKVOL, 7, 1, 0),
120SOC_SINGLE("Speaker Playback Switch", WM8510_SPKVOL, 6, 1, 1),
121SOC_SINGLE("Speaker Playback Volume", WM8510_SPKVOL, 0, 63, 0),
122SOC_SINGLE("Speaker Boost", WM8510_OUTPUT, 2, 1, 0),
123
124SOC_SINGLE("Capture Boost(+20dB)", WM8510_ADCBOOST, 8, 1, 0),
125SOC_SINGLE("Mono Playback Switch", WM8510_MONOMIX, 6, 1, 1),
126};
127
Mark Brown5d421512008-06-05 13:49:32 +0100128/* Speaker Output Mixer */
129static const struct snd_kcontrol_new wm8510_speaker_mixer_controls[] = {
130SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_SPKMIX, 1, 1, 0),
131SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_SPKMIX, 5, 1, 0),
132SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_SPKMIX, 0, 1, 0),
133};
134
135/* Mono Output Mixer */
136static const struct snd_kcontrol_new wm8510_mono_mixer_controls[] = {
137SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX, 1, 1, 0),
138SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX, 2, 1, 0),
139SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX, 0, 1, 0),
140};
141
142static const struct snd_kcontrol_new wm8510_boost_controls[] = {
Mark Brown8ae23ec2008-10-06 11:33:21 +0100143SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA, 6, 1, 1),
Mark Brown5d421512008-06-05 13:49:32 +0100144SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST, 0, 7, 0),
145SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST, 4, 7, 0),
146};
147
148static const struct snd_kcontrol_new wm8510_micpga_controls[] = {
149SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT, 0, 1, 0),
150SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT, 1, 1, 0),
151SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT, 2, 1, 0),
152};
153
154static const struct snd_soc_dapm_widget wm8510_dapm_widgets[] = {
155SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3, 2, 0,
156 &wm8510_speaker_mixer_controls[0],
157 ARRAY_SIZE(wm8510_speaker_mixer_controls)),
158SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3, 3, 0,
159 &wm8510_mono_mixer_controls[0],
160 ARRAY_SIZE(wm8510_mono_mixer_controls)),
161SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3, 0, 0),
162SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2, 0, 0),
163SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1, 6, 0, NULL, 0),
164SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3, 5, 0, NULL, 0),
165SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3, 6, 0, NULL, 0),
166SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3, 7, 0, NULL, 0),
167
Mark Brown2b5f34c2008-10-07 16:13:50 +0100168SND_SOC_DAPM_MIXER("Mic PGA", WM8510_POWER2, 2, 0,
169 &wm8510_micpga_controls[0],
170 ARRAY_SIZE(wm8510_micpga_controls)),
Mark Brown5d421512008-06-05 13:49:32 +0100171SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2, 4, 0,
172 &wm8510_boost_controls[0],
173 ARRAY_SIZE(wm8510_boost_controls)),
174
175SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_POWER1, 4, 0),
176
177SND_SOC_DAPM_INPUT("MICN"),
178SND_SOC_DAPM_INPUT("MICP"),
179SND_SOC_DAPM_INPUT("AUX"),
180SND_SOC_DAPM_OUTPUT("MONOOUT"),
181SND_SOC_DAPM_OUTPUT("SPKOUTP"),
182SND_SOC_DAPM_OUTPUT("SPKOUTN"),
183};
184
185static const struct snd_soc_dapm_route audio_map[] = {
186 /* Mono output mixer */
187 {"Mono Mixer", "PCM Playback Switch", "DAC"},
188 {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
189 {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
190
191 /* Speaker output mixer */
192 {"Speaker Mixer", "PCM Playback Switch", "DAC"},
193 {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
194 {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
195
196 /* Outputs */
197 {"Mono Out", NULL, "Mono Mixer"},
198 {"MONOOUT", NULL, "Mono Out"},
199 {"SpkN Out", NULL, "Speaker Mixer"},
200 {"SpkP Out", NULL, "Speaker Mixer"},
201 {"SPKOUTN", NULL, "SpkN Out"},
202 {"SPKOUTP", NULL, "SpkP Out"},
203
204 /* Microphone PGA */
205 {"Mic PGA", "MICN Switch", "MICN"},
206 {"Mic PGA", "MICP Switch", "MICP"},
207 { "Mic PGA", "AUX Switch", "Aux Input" },
208
209 /* Boost Mixer */
210 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
211 {"Boost Mixer", "Mic Volume", "MICP"},
212 {"Boost Mixer", "Aux Volume", "Aux Input"},
213
214 {"ADC", NULL, "Boost Mixer"},
215};
216
217static int wm8510_add_widgets(struct snd_soc_codec *codec)
218{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200219 struct snd_soc_dapm_context *dapm = &codec->dapm;
Mark Brown5d421512008-06-05 13:49:32 +0100220
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200221 snd_soc_dapm_new_controls(dapm, wm8510_dapm_widgets,
222 ARRAY_SIZE(wm8510_dapm_widgets));
223 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Mark Brown5d421512008-06-05 13:49:32 +0100224
Mark Brown5d421512008-06-05 13:49:32 +0100225 return 0;
226}
227
228struct pll_ {
229 unsigned int pre_div:4; /* prescale - 1 */
230 unsigned int n:4;
231 unsigned int k;
232};
233
234static struct pll_ pll_div;
235
236/* The size in bits of the pll divide multiplied by 10
237 * to allow rounding later */
238#define FIXED_PLL_SIZE ((1 << 24) * 10)
239
240static void pll_factors(unsigned int target, unsigned int source)
241{
242 unsigned long long Kpart;
243 unsigned int K, Ndiv, Nmod;
244
245 Ndiv = target / source;
246 if (Ndiv < 6) {
247 source >>= 1;
248 pll_div.pre_div = 1;
249 Ndiv = target / source;
250 } else
251 pll_div.pre_div = 0;
252
253 if ((Ndiv < 6) || (Ndiv > 12))
254 printk(KERN_WARNING
Roel Kluin449bd542009-05-27 17:08:39 -0700255 "WM8510 N value %u outwith recommended range!d\n",
Mark Brown5d421512008-06-05 13:49:32 +0100256 Ndiv);
257
258 pll_div.n = Ndiv;
259 Nmod = target % source;
260 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
261
262 do_div(Kpart, source);
263
264 K = Kpart & 0xFFFFFFFF;
265
266 /* Check if we need to round */
267 if ((K % 10) >= 5)
268 K += 5;
269
270 /* Move down to proper range now rounding is done */
271 K /= 10;
272
273 pll_div.k = K;
274}
275
Mark Brown85488032009-09-05 18:52:16 +0100276static int wm8510_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
277 int source, unsigned int freq_in, unsigned int freq_out)
Mark Brown5d421512008-06-05 13:49:32 +0100278{
279 struct snd_soc_codec *codec = codec_dai->codec;
280 u16 reg;
281
282 if (freq_in == 0 || freq_out == 0) {
283 /* Clock CODEC directly from MCLK */
Mark Brown17a52fd2009-07-05 17:24:50 +0100284 reg = snd_soc_read(codec, WM8510_CLOCK);
285 snd_soc_write(codec, WM8510_CLOCK, reg & 0x0ff);
Mark Brown5d421512008-06-05 13:49:32 +0100286
287 /* Turn off PLL */
Mark Brown17a52fd2009-07-05 17:24:50 +0100288 reg = snd_soc_read(codec, WM8510_POWER1);
289 snd_soc_write(codec, WM8510_POWER1, reg & 0x1df);
Mark Brown5d421512008-06-05 13:49:32 +0100290 return 0;
291 }
292
Jonas Andersson86027ae2009-03-04 08:24:26 +0100293 pll_factors(freq_out*4, freq_in);
Mark Brown5d421512008-06-05 13:49:32 +0100294
Mark Brown17a52fd2009-07-05 17:24:50 +0100295 snd_soc_write(codec, WM8510_PLLN, (pll_div.pre_div << 4) | pll_div.n);
296 snd_soc_write(codec, WM8510_PLLK1, pll_div.k >> 18);
297 snd_soc_write(codec, WM8510_PLLK2, (pll_div.k >> 9) & 0x1ff);
298 snd_soc_write(codec, WM8510_PLLK3, pll_div.k & 0x1ff);
299 reg = snd_soc_read(codec, WM8510_POWER1);
300 snd_soc_write(codec, WM8510_POWER1, reg | 0x020);
Mark Brown5d421512008-06-05 13:49:32 +0100301
302 /* Run CODEC from PLL instead of MCLK */
Mark Brown17a52fd2009-07-05 17:24:50 +0100303 reg = snd_soc_read(codec, WM8510_CLOCK);
304 snd_soc_write(codec, WM8510_CLOCK, reg | 0x100);
Mark Brown5d421512008-06-05 13:49:32 +0100305
306 return 0;
307}
308
309/*
310 * Configure WM8510 clock dividers.
311 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100312static int wm8510_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
Mark Brown5d421512008-06-05 13:49:32 +0100313 int div_id, int div)
314{
315 struct snd_soc_codec *codec = codec_dai->codec;
316 u16 reg;
317
318 switch (div_id) {
319 case WM8510_OPCLKDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100320 reg = snd_soc_read(codec, WM8510_GPIO) & 0x1cf;
321 snd_soc_write(codec, WM8510_GPIO, reg | div);
Mark Brown5d421512008-06-05 13:49:32 +0100322 break;
323 case WM8510_MCLKDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100324 reg = snd_soc_read(codec, WM8510_CLOCK) & 0x11f;
325 snd_soc_write(codec, WM8510_CLOCK, reg | div);
Mark Brown5d421512008-06-05 13:49:32 +0100326 break;
327 case WM8510_ADCCLK:
Mark Brown17a52fd2009-07-05 17:24:50 +0100328 reg = snd_soc_read(codec, WM8510_ADC) & 0x1f7;
329 snd_soc_write(codec, WM8510_ADC, reg | div);
Mark Brown5d421512008-06-05 13:49:32 +0100330 break;
331 case WM8510_DACCLK:
Mark Brown17a52fd2009-07-05 17:24:50 +0100332 reg = snd_soc_read(codec, WM8510_DAC) & 0x1f7;
333 snd_soc_write(codec, WM8510_DAC, reg | div);
Mark Brown5d421512008-06-05 13:49:32 +0100334 break;
335 case WM8510_BCLKDIV:
Mark Brown17a52fd2009-07-05 17:24:50 +0100336 reg = snd_soc_read(codec, WM8510_CLOCK) & 0x1e3;
337 snd_soc_write(codec, WM8510_CLOCK, reg | div);
Mark Brown5d421512008-06-05 13:49:32 +0100338 break;
339 default:
340 return -EINVAL;
341 }
342
343 return 0;
344}
345
Liam Girdwoode550e172008-07-07 16:07:52 +0100346static int wm8510_set_dai_fmt(struct snd_soc_dai *codec_dai,
Mark Brown5d421512008-06-05 13:49:32 +0100347 unsigned int fmt)
348{
349 struct snd_soc_codec *codec = codec_dai->codec;
350 u16 iface = 0;
Mark Brown17a52fd2009-07-05 17:24:50 +0100351 u16 clk = snd_soc_read(codec, WM8510_CLOCK) & 0x1fe;
Mark Brown5d421512008-06-05 13:49:32 +0100352
353 /* set master/slave audio interface */
354 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
355 case SND_SOC_DAIFMT_CBM_CFM:
356 clk |= 0x0001;
357 break;
358 case SND_SOC_DAIFMT_CBS_CFS:
359 break;
360 default:
361 return -EINVAL;
362 }
363
364 /* interface format */
365 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
366 case SND_SOC_DAIFMT_I2S:
367 iface |= 0x0010;
368 break;
369 case SND_SOC_DAIFMT_RIGHT_J:
370 break;
371 case SND_SOC_DAIFMT_LEFT_J:
372 iface |= 0x0008;
373 break;
374 case SND_SOC_DAIFMT_DSP_A:
375 iface |= 0x00018;
376 break;
377 default:
378 return -EINVAL;
379 }
380
381 /* clock inversion */
382 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
383 case SND_SOC_DAIFMT_NB_NF:
384 break;
385 case SND_SOC_DAIFMT_IB_IF:
386 iface |= 0x0180;
387 break;
388 case SND_SOC_DAIFMT_IB_NF:
389 iface |= 0x0100;
390 break;
391 case SND_SOC_DAIFMT_NB_IF:
392 iface |= 0x0080;
393 break;
394 default:
395 return -EINVAL;
396 }
397
Mark Brown17a52fd2009-07-05 17:24:50 +0100398 snd_soc_write(codec, WM8510_IFACE, iface);
399 snd_soc_write(codec, WM8510_CLOCK, clk);
Mark Brown5d421512008-06-05 13:49:32 +0100400 return 0;
401}
402
403static int wm8510_pcm_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000404 struct snd_pcm_hw_params *params,
405 struct snd_soc_dai *dai)
Mark Brown5d421512008-06-05 13:49:32 +0100406{
407 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000408 struct snd_soc_codec *codec = rtd->codec;
Mark Brown17a52fd2009-07-05 17:24:50 +0100409 u16 iface = snd_soc_read(codec, WM8510_IFACE) & 0x19f;
410 u16 adn = snd_soc_read(codec, WM8510_ADD) & 0x1f1;
Mark Brown5d421512008-06-05 13:49:32 +0100411
412 /* bit size */
413 switch (params_format(params)) {
414 case SNDRV_PCM_FORMAT_S16_LE:
415 break;
416 case SNDRV_PCM_FORMAT_S20_3LE:
417 iface |= 0x0020;
418 break;
419 case SNDRV_PCM_FORMAT_S24_LE:
420 iface |= 0x0040;
421 break;
422 case SNDRV_PCM_FORMAT_S32_LE:
423 iface |= 0x0060;
424 break;
425 }
426
427 /* filter coefficient */
428 switch (params_rate(params)) {
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100429 case 8000:
Mark Brown5d421512008-06-05 13:49:32 +0100430 adn |= 0x5 << 1;
431 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100432 case 11025:
Mark Brown5d421512008-06-05 13:49:32 +0100433 adn |= 0x4 << 1;
434 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100435 case 16000:
Mark Brown5d421512008-06-05 13:49:32 +0100436 adn |= 0x3 << 1;
437 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100438 case 22050:
Mark Brown5d421512008-06-05 13:49:32 +0100439 adn |= 0x2 << 1;
440 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100441 case 32000:
Mark Brown5d421512008-06-05 13:49:32 +0100442 adn |= 0x1 << 1;
443 break;
Guennadi Liakhovetskib3172f22009-12-24 01:13:51 +0100444 case 44100:
445 case 48000:
Mark Brown5d421512008-06-05 13:49:32 +0100446 break;
447 }
448
Mark Brown17a52fd2009-07-05 17:24:50 +0100449 snd_soc_write(codec, WM8510_IFACE, iface);
450 snd_soc_write(codec, WM8510_ADD, adn);
Mark Brown5d421512008-06-05 13:49:32 +0100451 return 0;
452}
453
Liam Girdwoode550e172008-07-07 16:07:52 +0100454static int wm8510_mute(struct snd_soc_dai *dai, int mute)
Mark Brown5d421512008-06-05 13:49:32 +0100455{
456 struct snd_soc_codec *codec = dai->codec;
Mark Brown17a52fd2009-07-05 17:24:50 +0100457 u16 mute_reg = snd_soc_read(codec, WM8510_DAC) & 0xffbf;
Mark Brown5d421512008-06-05 13:49:32 +0100458
459 if (mute)
Mark Brown17a52fd2009-07-05 17:24:50 +0100460 snd_soc_write(codec, WM8510_DAC, mute_reg | 0x40);
Mark Brown5d421512008-06-05 13:49:32 +0100461 else
Mark Brown17a52fd2009-07-05 17:24:50 +0100462 snd_soc_write(codec, WM8510_DAC, mute_reg);
Mark Brown5d421512008-06-05 13:49:32 +0100463 return 0;
464}
465
466/* liam need to make this lower power with dapm */
467static int wm8510_set_bias_level(struct snd_soc_codec *codec,
468 enum snd_soc_bias_level level)
469{
Mark Brown17a52fd2009-07-05 17:24:50 +0100470 u16 power1 = snd_soc_read(codec, WM8510_POWER1) & ~0x3;
Mark Brown5d421512008-06-05 13:49:32 +0100471
472 switch (level) {
473 case SND_SOC_BIAS_ON:
Mark Brown5d421512008-06-05 13:49:32 +0100474 case SND_SOC_BIAS_PREPARE:
Mark Brown09af98b2008-10-07 13:04:58 +0100475 power1 |= 0x1; /* VMID 50k */
Mark Brown17a52fd2009-07-05 17:24:50 +0100476 snd_soc_write(codec, WM8510_POWER1, power1);
Mark Brown5d421512008-06-05 13:49:32 +0100477 break;
Mark Brown09af98b2008-10-07 13:04:58 +0100478
479 case SND_SOC_BIAS_STANDBY:
480 power1 |= WM8510_POWER1_BIASEN | WM8510_POWER1_BUFIOEN;
481
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200482 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Brown09af98b2008-10-07 13:04:58 +0100483 /* Initial cap charge at VMID 5k */
Mark Brown17a52fd2009-07-05 17:24:50 +0100484 snd_soc_write(codec, WM8510_POWER1, power1 | 0x3);
Mark Brown09af98b2008-10-07 13:04:58 +0100485 mdelay(100);
486 }
487
488 power1 |= 0x2; /* VMID 500k */
Mark Brown17a52fd2009-07-05 17:24:50 +0100489 snd_soc_write(codec, WM8510_POWER1, power1);
Mark Brown09af98b2008-10-07 13:04:58 +0100490 break;
491
Mark Brown5d421512008-06-05 13:49:32 +0100492 case SND_SOC_BIAS_OFF:
Mark Brown17a52fd2009-07-05 17:24:50 +0100493 snd_soc_write(codec, WM8510_POWER1, 0);
494 snd_soc_write(codec, WM8510_POWER2, 0);
495 snd_soc_write(codec, WM8510_POWER3, 0);
Mark Brown5d421512008-06-05 13:49:32 +0100496 break;
497 }
Mark Brown09af98b2008-10-07 13:04:58 +0100498
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200499 codec->dapm.bias_level = level;
Mark Brown5d421512008-06-05 13:49:32 +0100500 return 0;
501}
502
503#define WM8510_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
504 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
505 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
506
507#define WM8510_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
508 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
509
Eric Miao6335d052009-03-03 09:41:00 +0800510static struct snd_soc_dai_ops wm8510_dai_ops = {
511 .hw_params = wm8510_pcm_hw_params,
512 .digital_mute = wm8510_mute,
513 .set_fmt = wm8510_set_dai_fmt,
514 .set_clkdiv = wm8510_set_dai_clkdiv,
515 .set_pll = wm8510_set_dai_pll,
516};
517
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518static struct snd_soc_dai_driver wm8510_dai = {
519 .name = "wm8510-hifi",
Mark Brown5d421512008-06-05 13:49:32 +0100520 .playback = {
521 .stream_name = "Playback",
522 .channels_min = 2,
523 .channels_max = 2,
524 .rates = WM8510_RATES,
525 .formats = WM8510_FORMATS,},
526 .capture = {
527 .stream_name = "Capture",
528 .channels_min = 2,
529 .channels_max = 2,
530 .rates = WM8510_RATES,
531 .formats = WM8510_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800532 .ops = &wm8510_dai_ops,
Mark Browncc369cf2009-07-09 11:28:07 +0100533 .symmetric_rates = 1,
Mark Brown5d421512008-06-05 13:49:32 +0100534};
Mark Brown5d421512008-06-05 13:49:32 +0100535
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000536static int wm8510_suspend(struct snd_soc_codec *codec, pm_message_t state)
Mark Brown5d421512008-06-05 13:49:32 +0100537{
Mark Brown5d421512008-06-05 13:49:32 +0100538 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
539 return 0;
540}
541
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000542static int wm8510_resume(struct snd_soc_codec *codec)
Mark Brown5d421512008-06-05 13:49:32 +0100543{
Mark Brown5d421512008-06-05 13:49:32 +0100544 int i;
545 u8 data[2];
546 u16 *cache = codec->reg_cache;
547
548 /* Sync reg_cache with the hardware */
549 for (i = 0; i < ARRAY_SIZE(wm8510_reg); i++) {
550 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
551 data[1] = cache[i] & 0x00ff;
552 codec->hw_write(codec->control_data, data, 2);
553 }
554 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Mark Brown29e189c2010-05-07 20:30:00 +0100555
Mark Brown5d421512008-06-05 13:49:32 +0100556 return 0;
557}
558
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000559static int wm8510_probe(struct snd_soc_codec *codec)
Mark Brown5d421512008-06-05 13:49:32 +0100560{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 struct wm8510_priv *wm8510 = snd_soc_codec_get_drvdata(codec);
562 int ret;
Mark Brown5d421512008-06-05 13:49:32 +0100563
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000564 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8510->control_type);
Mark Brown17a52fd2009-07-05 17:24:50 +0100565 if (ret < 0) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000566 printk(KERN_ERR "wm8510: failed to set cache I/O: %d\n", ret);
567 return ret;
Mark Brown17a52fd2009-07-05 17:24:50 +0100568 }
569
Mark Brown5d421512008-06-05 13:49:32 +0100570 wm8510_reset(codec);
571
Mark Brown5d421512008-06-05 13:49:32 +0100572 /* power on device */
573 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Ian Molton3e8e1952009-01-09 00:23:21 +0000574 snd_soc_add_controls(codec, wm8510_snd_controls,
575 ARRAY_SIZE(wm8510_snd_controls));
Mark Brown5d421512008-06-05 13:49:32 +0100576 wm8510_add_widgets(codec);
Mark Brownfe3e78e2009-11-03 22:13:13 +0000577
Mark Brown5d421512008-06-05 13:49:32 +0100578 return ret;
Mark Brown5d421512008-06-05 13:49:32 +0100579}
580
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000581/* power down chip */
582static int wm8510_remove(struct snd_soc_codec *codec)
Mark Brown5d421512008-06-05 13:49:32 +0100583{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000584 struct wm8510_priv *wm8510 = snd_soc_codec_get_drvdata(codec);
Mark Brown5d421512008-06-05 13:49:32 +0100585
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000586 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
587 kfree(wm8510);
Mark Brown5d421512008-06-05 13:49:32 +0100588 return 0;
589}
590
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000591static struct snd_soc_codec_driver soc_codec_dev_wm8510 = {
592 .probe = wm8510_probe,
593 .remove = wm8510_remove,
594 .suspend = wm8510_suspend,
595 .resume = wm8510_resume,
596 .set_bias_level = wm8510_set_bias_level,
597 .reg_cache_size = ARRAY_SIZE(wm8510_reg),
598 .reg_word_size = sizeof(u16),
599 .reg_cache_default =wm8510_reg,
Jean Delvare41759c22008-09-02 17:07:30 +0200600};
Mark Brown5d421512008-06-05 13:49:32 +0100601
Mark Brown0a422e12011-08-02 13:03:04 +0900602static const struct of_device_id wm8510_of_match[] = {
603 { .compatible = "wlf,wm8510" },
604 { },
605};
606
Mark Brown5e357952008-10-07 11:56:20 +0100607#if defined(CONFIG_SPI_MASTER)
608static int __devinit wm8510_spi_probe(struct spi_device *spi)
609{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 struct wm8510_priv *wm8510;
Mark Brown5e357952008-10-07 11:56:20 +0100611 int ret;
612
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 wm8510 = kzalloc(sizeof(struct wm8510_priv), GFP_KERNEL);
614 if (wm8510 == NULL)
615 return -ENOMEM;
Mark Brown5e357952008-10-07 11:56:20 +0100616
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 wm8510->control_type = SND_SOC_SPI;
618 spi_set_drvdata(spi, wm8510);
619
620 ret = snd_soc_register_codec(&spi->dev,
621 &soc_codec_dev_wm8510, &wm8510_dai, 1);
Mark Brown5e357952008-10-07 11:56:20 +0100622 if (ret < 0)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000623 kfree(wm8510);
Mark Brown5e357952008-10-07 11:56:20 +0100624 return ret;
625}
626
627static int __devexit wm8510_spi_remove(struct spi_device *spi)
628{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000629 snd_soc_unregister_codec(&spi->dev);
Mark Brown5e357952008-10-07 11:56:20 +0100630 return 0;
631}
632
633static struct spi_driver wm8510_spi_driver = {
634 .driver = {
635 .name = "wm8510",
Mark Brown5e357952008-10-07 11:56:20 +0100636 .owner = THIS_MODULE,
Mark Brown0a422e12011-08-02 13:03:04 +0900637 .of_match_table = wm8510_of_match,
Mark Brown5e357952008-10-07 11:56:20 +0100638 },
639 .probe = wm8510_spi_probe,
640 .remove = __devexit_p(wm8510_spi_remove),
641};
Mark Brown5e357952008-10-07 11:56:20 +0100642#endif /* CONFIG_SPI_MASTER */
643
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
645static __devinit int wm8510_i2c_probe(struct i2c_client *i2c,
646 const struct i2c_device_id *id)
Mark Brown5d421512008-06-05 13:49:32 +0100647{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000648 struct wm8510_priv *wm8510;
649 int ret;
Mark Brown5d421512008-06-05 13:49:32 +0100650
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000651 wm8510 = kzalloc(sizeof(struct wm8510_priv), GFP_KERNEL);
652 if (wm8510 == NULL)
Mark Brown5d421512008-06-05 13:49:32 +0100653 return -ENOMEM;
654
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000655 i2c_set_clientdata(i2c, wm8510);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656 wm8510->control_type = SND_SOC_I2C;
Mark Brown5d421512008-06-05 13:49:32 +0100657
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000658 ret = snd_soc_register_codec(&i2c->dev,
659 &soc_codec_dev_wm8510, &wm8510_dai, 1);
660 if (ret < 0)
661 kfree(wm8510);
Mark Brown5d421512008-06-05 13:49:32 +0100662 return ret;
663}
664
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665static __devexit int wm8510_i2c_remove(struct i2c_client *client)
Mark Brown5d421512008-06-05 13:49:32 +0100666{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000667 snd_soc_unregister_codec(&client->dev);
Mark Brown5d421512008-06-05 13:49:32 +0100668 return 0;
669}
670
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000671static const struct i2c_device_id wm8510_i2c_id[] = {
672 { "wm8510", 0 },
673 { }
Mark Brown5d421512008-06-05 13:49:32 +0100674};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000675MODULE_DEVICE_TABLE(i2c, wm8510_i2c_id);
676
677static struct i2c_driver wm8510_i2c_driver = {
678 .driver = {
679 .name = "wm8510-codec",
680 .owner = THIS_MODULE,
Mark Brown0a422e12011-08-02 13:03:04 +0900681 .of_match_table = wm8510_of_match,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000682 },
683 .probe = wm8510_i2c_probe,
684 .remove = __devexit_p(wm8510_i2c_remove),
685 .id_table = wm8510_i2c_id,
686};
687#endif
Mark Brown5d421512008-06-05 13:49:32 +0100688
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100689static int __init wm8510_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000690{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000691 int ret = 0;
692#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
693 ret = i2c_add_driver(&wm8510_i2c_driver);
694 if (ret != 0) {
695 printk(KERN_ERR "Failed to register WM8510 I2C driver: %d\n",
696 ret);
697 }
698#endif
699#if defined(CONFIG_SPI_MASTER)
700 ret = spi_register_driver(&wm8510_spi_driver);
701 if (ret != 0) {
702 printk(KERN_ERR "Failed to register WM8510 SPI driver: %d\n",
703 ret);
704 }
705#endif
706 return ret;
Mark Brown64089b82008-12-08 19:17:58 +0000707}
708module_init(wm8510_modinit);
709
710static void __exit wm8510_exit(void)
711{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000712#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
713 i2c_del_driver(&wm8510_i2c_driver);
714#endif
715#if defined(CONFIG_SPI_MASTER)
716 spi_unregister_driver(&wm8510_spi_driver);
717#endif
Mark Brown64089b82008-12-08 19:17:58 +0000718}
719module_exit(wm8510_exit);
720
Mark Brown5d421512008-06-05 13:49:32 +0100721MODULE_DESCRIPTION("ASoC WM8510 driver");
722MODULE_AUTHOR("Liam Girdwood");
723MODULE_LICENSE("GPL");