blob: 261d4cb7596440fa37ef7b1dcaf35a195f259b3b [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
Roel Kluin449bd542009-05-27 17:08:39 -0700301 "WM8510 N value %u outwith recommended range!d\n",
Mark Brown5d421512008-06-05 13:49:32 +0100302 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
Jonas Andersson86027ae2009-03-04 08:24:26 +0100339 pll_factors(freq_out*4, freq_in);
Mark Brown5d421512008-06-05 13:49:32 +0100340
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:
Jonas Andersson86027ae2009-03-04 08:24:26 +0100370 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x11f;
Mark Brown5d421512008-06-05 13:49:32 +0100371 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 Browncc369cf2009-07-09 11:28:07 +0100580 .symmetric_rates = 1,
Mark Brown5d421512008-06-05 13:49:32 +0100581};
582EXPORT_SYMBOL_GPL(wm8510_dai);
583
584static int wm8510_suspend(struct platform_device *pdev, pm_message_t state)
585{
586 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000587 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100588
589 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
590 return 0;
591}
592
593static int wm8510_resume(struct platform_device *pdev)
594{
595 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000596 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100597 int i;
598 u8 data[2];
599 u16 *cache = codec->reg_cache;
600
601 /* Sync reg_cache with the hardware */
602 for (i = 0; i < ARRAY_SIZE(wm8510_reg); i++) {
603 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
604 data[1] = cache[i] & 0x00ff;
605 codec->hw_write(codec->control_data, data, 2);
606 }
607 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
608 wm8510_set_bias_level(codec, codec->suspend_bias_level);
609 return 0;
610}
611
612/*
613 * initialise the WM8510 driver
614 * register the mixer and dsp interfaces with the kernel
615 */
616static int wm8510_init(struct snd_soc_device *socdev)
617{
Mark Brown6627a652009-01-23 22:55:23 +0000618 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100619 int ret = 0;
620
621 codec->name = "WM8510";
622 codec->owner = THIS_MODULE;
623 codec->read = wm8510_read_reg_cache;
624 codec->write = wm8510_write;
625 codec->set_bias_level = wm8510_set_bias_level;
626 codec->dai = &wm8510_dai;
627 codec->num_dai = 1;
628 codec->reg_cache_size = ARRAY_SIZE(wm8510_reg);
629 codec->reg_cache = kmemdup(wm8510_reg, sizeof(wm8510_reg), GFP_KERNEL);
630
631 if (codec->reg_cache == NULL)
632 return -ENOMEM;
633
634 wm8510_reset(codec);
635
636 /* register pcms */
637 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
638 if (ret < 0) {
639 printk(KERN_ERR "wm8510: failed to create pcms\n");
640 goto pcm_err;
641 }
642
643 /* power on device */
Mark Brown09af98b2008-10-07 13:04:58 +0100644 codec->bias_level = SND_SOC_BIAS_OFF;
Mark Brown5d421512008-06-05 13:49:32 +0100645 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Ian Molton3e8e1952009-01-09 00:23:21 +0000646 snd_soc_add_controls(codec, wm8510_snd_controls,
647 ARRAY_SIZE(wm8510_snd_controls));
Mark Brown5d421512008-06-05 13:49:32 +0100648 wm8510_add_widgets(codec);
Mark Brown968a6022008-11-28 11:49:07 +0000649 ret = snd_soc_init_card(socdev);
Mark Brown5d421512008-06-05 13:49:32 +0100650 if (ret < 0) {
651 printk(KERN_ERR "wm8510: failed to register card\n");
652 goto card_err;
653 }
654 return ret;
655
656card_err:
657 snd_soc_free_pcms(socdev);
658 snd_soc_dapm_free(socdev);
659pcm_err:
660 kfree(codec->reg_cache);
661 return ret;
662}
663
664static struct snd_soc_device *wm8510_socdev;
665
666#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
667
668/*
669 * WM8510 2 wire address is 0x1a
670 */
Mark Brown5d421512008-06-05 13:49:32 +0100671
Jean Delvare41759c22008-09-02 17:07:30 +0200672static int wm8510_i2c_probe(struct i2c_client *i2c,
673 const struct i2c_device_id *id)
Mark Brown5d421512008-06-05 13:49:32 +0100674{
675 struct snd_soc_device *socdev = wm8510_socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000676 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100677 int ret;
678
Mark Brown5d421512008-06-05 13:49:32 +0100679 i2c_set_clientdata(i2c, codec);
680 codec->control_data = i2c;
681
Mark Brown5d421512008-06-05 13:49:32 +0100682 ret = wm8510_init(socdev);
Jean Delvare41759c22008-09-02 17:07:30 +0200683 if (ret < 0)
Mark Browna5c95e92008-06-23 14:51:29 +0100684 pr_err("failed to initialise WM8510\n");
Mark Brown5d421512008-06-05 13:49:32 +0100685
Mark Brown5d421512008-06-05 13:49:32 +0100686 return ret;
687}
688
Jean Delvare41759c22008-09-02 17:07:30 +0200689static int wm8510_i2c_remove(struct i2c_client *client)
Mark Brown5d421512008-06-05 13:49:32 +0100690{
691 struct snd_soc_codec *codec = i2c_get_clientdata(client);
Mark Brown5d421512008-06-05 13:49:32 +0100692 kfree(codec->reg_cache);
Mark Brown5d421512008-06-05 13:49:32 +0100693 return 0;
694}
695
Jean Delvare41759c22008-09-02 17:07:30 +0200696static const struct i2c_device_id wm8510_i2c_id[] = {
697 { "wm8510", 0 },
698 { }
699};
700MODULE_DEVICE_TABLE(i2c, wm8510_i2c_id);
Mark Brown5d421512008-06-05 13:49:32 +0100701
Mark Brown5d421512008-06-05 13:49:32 +0100702static struct i2c_driver wm8510_i2c_driver = {
703 .driver = {
704 .name = "WM8510 I2C Codec",
705 .owner = THIS_MODULE,
706 },
Jean Delvare41759c22008-09-02 17:07:30 +0200707 .probe = wm8510_i2c_probe,
708 .remove = wm8510_i2c_remove,
709 .id_table = wm8510_i2c_id,
Mark Brown5d421512008-06-05 13:49:32 +0100710};
711
Jean Delvare41759c22008-09-02 17:07:30 +0200712static int wm8510_add_i2c_device(struct platform_device *pdev,
713 const struct wm8510_setup_data *setup)
714{
715 struct i2c_board_info info;
716 struct i2c_adapter *adapter;
717 struct i2c_client *client;
718 int ret;
719
720 ret = i2c_add_driver(&wm8510_i2c_driver);
721 if (ret != 0) {
722 dev_err(&pdev->dev, "can't add i2c driver\n");
723 return ret;
724 }
725
726 memset(&info, 0, sizeof(struct i2c_board_info));
727 info.addr = setup->i2c_address;
728 strlcpy(info.type, "wm8510", I2C_NAME_SIZE);
729
730 adapter = i2c_get_adapter(setup->i2c_bus);
731 if (!adapter) {
732 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
733 setup->i2c_bus);
734 goto err_driver;
735 }
736
737 client = i2c_new_device(adapter, &info);
738 i2c_put_adapter(adapter);
739 if (!client) {
740 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
741 (unsigned int)info.addr);
742 goto err_driver;
743 }
744
745 return 0;
746
747err_driver:
748 i2c_del_driver(&wm8510_i2c_driver);
749 return -ENODEV;
750}
Mark Brown5d421512008-06-05 13:49:32 +0100751#endif
752
Mark Brown5e357952008-10-07 11:56:20 +0100753#if defined(CONFIG_SPI_MASTER)
754static int __devinit wm8510_spi_probe(struct spi_device *spi)
755{
756 struct snd_soc_device *socdev = wm8510_socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000757 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5e357952008-10-07 11:56:20 +0100758 int ret;
759
760 codec->control_data = spi;
761
762 ret = wm8510_init(socdev);
763 if (ret < 0)
764 dev_err(&spi->dev, "failed to initialise WM8510\n");
765
766 return ret;
767}
768
769static int __devexit wm8510_spi_remove(struct spi_device *spi)
770{
771 return 0;
772}
773
774static struct spi_driver wm8510_spi_driver = {
775 .driver = {
776 .name = "wm8510",
777 .bus = &spi_bus_type,
778 .owner = THIS_MODULE,
779 },
780 .probe = wm8510_spi_probe,
781 .remove = __devexit_p(wm8510_spi_remove),
782};
783
784static int wm8510_spi_write(struct spi_device *spi, const char *data, int len)
785{
786 struct spi_transfer t;
787 struct spi_message m;
788 u8 msg[2];
789
790 if (len <= 0)
791 return 0;
792
793 msg[0] = data[0];
794 msg[1] = data[1];
795
796 spi_message_init(&m);
797 memset(&t, 0, (sizeof t));
798
799 t.tx_buf = &msg[0];
800 t.len = len;
801
802 spi_message_add_tail(&t, &m);
803 spi_sync(spi, &m);
804
805 return len;
806}
807#endif /* CONFIG_SPI_MASTER */
808
Mark Brown5d421512008-06-05 13:49:32 +0100809static int wm8510_probe(struct platform_device *pdev)
810{
811 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
812 struct wm8510_setup_data *setup;
813 struct snd_soc_codec *codec;
814 int ret = 0;
815
Mark Browna5c95e92008-06-23 14:51:29 +0100816 pr_info("WM8510 Audio Codec %s", WM8510_VERSION);
Mark Brown5d421512008-06-05 13:49:32 +0100817
818 setup = socdev->codec_data;
819 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
820 if (codec == NULL)
821 return -ENOMEM;
822
Mark Brown6627a652009-01-23 22:55:23 +0000823 socdev->card->codec = codec;
Mark Brown5d421512008-06-05 13:49:32 +0100824 mutex_init(&codec->mutex);
825 INIT_LIST_HEAD(&codec->dapm_widgets);
826 INIT_LIST_HEAD(&codec->dapm_paths);
827
828 wm8510_socdev = socdev;
829#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
830 if (setup->i2c_address) {
Mark Brown5d421512008-06-05 13:49:32 +0100831 codec->hw_write = (hw_write_t)i2c_master_send;
Jean Delvare41759c22008-09-02 17:07:30 +0200832 ret = wm8510_add_i2c_device(pdev, setup);
Mark Brown5d421512008-06-05 13:49:32 +0100833 }
Mark Brown5e357952008-10-07 11:56:20 +0100834#endif
835#if defined(CONFIG_SPI_MASTER)
836 if (setup->spi) {
837 codec->hw_write = (hw_write_t)wm8510_spi_write;
838 ret = spi_register_driver(&wm8510_spi_driver);
839 if (ret != 0)
840 printk(KERN_ERR "can't add spi driver");
841 }
Mark Brown5d421512008-06-05 13:49:32 +0100842#endif
Jean Delvare3051e412008-08-25 11:49:20 +0100843
844 if (ret != 0)
845 kfree(codec);
Mark Brown5d421512008-06-05 13:49:32 +0100846 return ret;
847}
848
849/* power down chip */
850static int wm8510_remove(struct platform_device *pdev)
851{
852 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000853 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100854
855 if (codec->control_data)
856 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
857
858 snd_soc_free_pcms(socdev);
859 snd_soc_dapm_free(socdev);
860#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jean Delvare41759c22008-09-02 17:07:30 +0200861 i2c_unregister_device(codec->control_data);
Mark Brown5d421512008-06-05 13:49:32 +0100862 i2c_del_driver(&wm8510_i2c_driver);
863#endif
Mark Brown5e357952008-10-07 11:56:20 +0100864#if defined(CONFIG_SPI_MASTER)
865 spi_unregister_driver(&wm8510_spi_driver);
866#endif
Mark Brown5d421512008-06-05 13:49:32 +0100867 kfree(codec);
868
869 return 0;
870}
871
872struct snd_soc_codec_device soc_codec_dev_wm8510 = {
873 .probe = wm8510_probe,
874 .remove = wm8510_remove,
875 .suspend = wm8510_suspend,
876 .resume = wm8510_resume,
877};
878EXPORT_SYMBOL_GPL(soc_codec_dev_wm8510);
879
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100880static int __init wm8510_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000881{
882 return snd_soc_register_dai(&wm8510_dai);
883}
884module_init(wm8510_modinit);
885
886static void __exit wm8510_exit(void)
887{
888 snd_soc_unregister_dai(&wm8510_dai);
889}
890module_exit(wm8510_exit);
891
Mark Brown5d421512008-06-05 13:49:32 +0100892MODULE_DESCRIPTION("ASoC WM8510 driver");
893MODULE_AUTHOR("Liam Girdwood");
894MODULE_LICENSE("GPL");