blob: 40f8238df7174d65b6c306fbd23e51d505a85d1c [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
174/* add non dapm controls */
175static int wm8510_add_controls(struct snd_soc_codec *codec)
176{
177 int err, i;
178
179 for (i = 0; i < ARRAY_SIZE(wm8510_snd_controls); i++) {
180 err = snd_ctl_add(codec->card,
181 snd_soc_cnew(&wm8510_snd_controls[i], codec,
182 NULL));
183 if (err < 0)
184 return err;
185 }
186
187 return 0;
188}
189
190/* Speaker Output Mixer */
191static const struct snd_kcontrol_new wm8510_speaker_mixer_controls[] = {
192SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_SPKMIX, 1, 1, 0),
193SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_SPKMIX, 5, 1, 0),
194SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_SPKMIX, 0, 1, 0),
195};
196
197/* Mono Output Mixer */
198static const struct snd_kcontrol_new wm8510_mono_mixer_controls[] = {
199SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX, 1, 1, 0),
200SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX, 2, 1, 0),
201SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX, 0, 1, 0),
202};
203
204static const struct snd_kcontrol_new wm8510_boost_controls[] = {
Mark Brown8ae23ec2008-10-06 11:33:21 +0100205SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA, 6, 1, 1),
Mark Brown5d421512008-06-05 13:49:32 +0100206SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST, 0, 7, 0),
207SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST, 4, 7, 0),
208};
209
210static const struct snd_kcontrol_new wm8510_micpga_controls[] = {
211SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT, 0, 1, 0),
212SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT, 1, 1, 0),
213SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT, 2, 1, 0),
214};
215
216static const struct snd_soc_dapm_widget wm8510_dapm_widgets[] = {
217SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3, 2, 0,
218 &wm8510_speaker_mixer_controls[0],
219 ARRAY_SIZE(wm8510_speaker_mixer_controls)),
220SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3, 3, 0,
221 &wm8510_mono_mixer_controls[0],
222 ARRAY_SIZE(wm8510_mono_mixer_controls)),
223SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3, 0, 0),
224SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2, 0, 0),
225SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1, 6, 0, NULL, 0),
226SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3, 5, 0, NULL, 0),
227SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3, 6, 0, NULL, 0),
228SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3, 7, 0, NULL, 0),
229
Mark Brown2b5f34c2008-10-07 16:13:50 +0100230SND_SOC_DAPM_MIXER("Mic PGA", WM8510_POWER2, 2, 0,
231 &wm8510_micpga_controls[0],
232 ARRAY_SIZE(wm8510_micpga_controls)),
Mark Brown5d421512008-06-05 13:49:32 +0100233SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2, 4, 0,
234 &wm8510_boost_controls[0],
235 ARRAY_SIZE(wm8510_boost_controls)),
236
237SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_POWER1, 4, 0),
238
239SND_SOC_DAPM_INPUT("MICN"),
240SND_SOC_DAPM_INPUT("MICP"),
241SND_SOC_DAPM_INPUT("AUX"),
242SND_SOC_DAPM_OUTPUT("MONOOUT"),
243SND_SOC_DAPM_OUTPUT("SPKOUTP"),
244SND_SOC_DAPM_OUTPUT("SPKOUTN"),
245};
246
247static const struct snd_soc_dapm_route audio_map[] = {
248 /* Mono output mixer */
249 {"Mono Mixer", "PCM Playback Switch", "DAC"},
250 {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
251 {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
252
253 /* Speaker output mixer */
254 {"Speaker Mixer", "PCM Playback Switch", "DAC"},
255 {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
256 {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
257
258 /* Outputs */
259 {"Mono Out", NULL, "Mono Mixer"},
260 {"MONOOUT", NULL, "Mono Out"},
261 {"SpkN Out", NULL, "Speaker Mixer"},
262 {"SpkP Out", NULL, "Speaker Mixer"},
263 {"SPKOUTN", NULL, "SpkN Out"},
264 {"SPKOUTP", NULL, "SpkP Out"},
265
266 /* Microphone PGA */
267 {"Mic PGA", "MICN Switch", "MICN"},
268 {"Mic PGA", "MICP Switch", "MICP"},
269 { "Mic PGA", "AUX Switch", "Aux Input" },
270
271 /* Boost Mixer */
272 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
273 {"Boost Mixer", "Mic Volume", "MICP"},
274 {"Boost Mixer", "Aux Volume", "Aux Input"},
275
276 {"ADC", NULL, "Boost Mixer"},
277};
278
279static int wm8510_add_widgets(struct snd_soc_codec *codec)
280{
281 snd_soc_dapm_new_controls(codec, wm8510_dapm_widgets,
282 ARRAY_SIZE(wm8510_dapm_widgets));
283
284 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
285
286 snd_soc_dapm_new_widgets(codec);
287 return 0;
288}
289
290struct pll_ {
291 unsigned int pre_div:4; /* prescale - 1 */
292 unsigned int n:4;
293 unsigned int k;
294};
295
296static struct pll_ pll_div;
297
298/* The size in bits of the pll divide multiplied by 10
299 * to allow rounding later */
300#define FIXED_PLL_SIZE ((1 << 24) * 10)
301
302static void pll_factors(unsigned int target, unsigned int source)
303{
304 unsigned long long Kpart;
305 unsigned int K, Ndiv, Nmod;
306
307 Ndiv = target / source;
308 if (Ndiv < 6) {
309 source >>= 1;
310 pll_div.pre_div = 1;
311 Ndiv = target / source;
312 } else
313 pll_div.pre_div = 0;
314
315 if ((Ndiv < 6) || (Ndiv > 12))
316 printk(KERN_WARNING
317 "WM8510 N value %d outwith recommended range!d\n",
318 Ndiv);
319
320 pll_div.n = Ndiv;
321 Nmod = target % source;
322 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
323
324 do_div(Kpart, source);
325
326 K = Kpart & 0xFFFFFFFF;
327
328 /* Check if we need to round */
329 if ((K % 10) >= 5)
330 K += 5;
331
332 /* Move down to proper range now rounding is done */
333 K /= 10;
334
335 pll_div.k = K;
336}
337
Liam Girdwoode550e172008-07-07 16:07:52 +0100338static int wm8510_set_dai_pll(struct snd_soc_dai *codec_dai,
Mark Brown5d421512008-06-05 13:49:32 +0100339 int pll_id, unsigned int freq_in, unsigned int freq_out)
340{
341 struct snd_soc_codec *codec = codec_dai->codec;
342 u16 reg;
343
344 if (freq_in == 0 || freq_out == 0) {
345 /* Clock CODEC directly from MCLK */
346 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK);
347 wm8510_write(codec, WM8510_CLOCK, reg & 0x0ff);
348
349 /* Turn off PLL */
350 reg = wm8510_read_reg_cache(codec, WM8510_POWER1);
351 wm8510_write(codec, WM8510_POWER1, reg & 0x1df);
352 return 0;
353 }
354
355 pll_factors(freq_out*8, freq_in);
356
357 wm8510_write(codec, WM8510_PLLN, (pll_div.pre_div << 4) | pll_div.n);
358 wm8510_write(codec, WM8510_PLLK1, pll_div.k >> 18);
359 wm8510_write(codec, WM8510_PLLK2, (pll_div.k >> 9) & 0x1ff);
360 wm8510_write(codec, WM8510_PLLK3, pll_div.k & 0x1ff);
361 reg = wm8510_read_reg_cache(codec, WM8510_POWER1);
362 wm8510_write(codec, WM8510_POWER1, reg | 0x020);
363
364 /* Run CODEC from PLL instead of MCLK */
365 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK);
366 wm8510_write(codec, WM8510_CLOCK, reg | 0x100);
367
368 return 0;
369}
370
371/*
372 * Configure WM8510 clock dividers.
373 */
Liam Girdwoode550e172008-07-07 16:07:52 +0100374static int wm8510_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
Mark Brown5d421512008-06-05 13:49:32 +0100375 int div_id, int div)
376{
377 struct snd_soc_codec *codec = codec_dai->codec;
378 u16 reg;
379
380 switch (div_id) {
381 case WM8510_OPCLKDIV:
382 reg = wm8510_read_reg_cache(codec, WM8510_GPIO) & 0x1cf;
383 wm8510_write(codec, WM8510_GPIO, reg | div);
384 break;
385 case WM8510_MCLKDIV:
386 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1f;
387 wm8510_write(codec, WM8510_CLOCK, reg | div);
388 break;
389 case WM8510_ADCCLK:
390 reg = wm8510_read_reg_cache(codec, WM8510_ADC) & 0x1f7;
391 wm8510_write(codec, WM8510_ADC, reg | div);
392 break;
393 case WM8510_DACCLK:
394 reg = wm8510_read_reg_cache(codec, WM8510_DAC) & 0x1f7;
395 wm8510_write(codec, WM8510_DAC, reg | div);
396 break;
397 case WM8510_BCLKDIV:
398 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1e3;
399 wm8510_write(codec, WM8510_CLOCK, reg | div);
400 break;
401 default:
402 return -EINVAL;
403 }
404
405 return 0;
406}
407
Liam Girdwoode550e172008-07-07 16:07:52 +0100408static int wm8510_set_dai_fmt(struct snd_soc_dai *codec_dai,
Mark Brown5d421512008-06-05 13:49:32 +0100409 unsigned int fmt)
410{
411 struct snd_soc_codec *codec = codec_dai->codec;
412 u16 iface = 0;
413 u16 clk = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1fe;
414
415 /* set master/slave audio interface */
416 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
417 case SND_SOC_DAIFMT_CBM_CFM:
418 clk |= 0x0001;
419 break;
420 case SND_SOC_DAIFMT_CBS_CFS:
421 break;
422 default:
423 return -EINVAL;
424 }
425
426 /* interface format */
427 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
428 case SND_SOC_DAIFMT_I2S:
429 iface |= 0x0010;
430 break;
431 case SND_SOC_DAIFMT_RIGHT_J:
432 break;
433 case SND_SOC_DAIFMT_LEFT_J:
434 iface |= 0x0008;
435 break;
436 case SND_SOC_DAIFMT_DSP_A:
437 iface |= 0x00018;
438 break;
439 default:
440 return -EINVAL;
441 }
442
443 /* clock inversion */
444 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
445 case SND_SOC_DAIFMT_NB_NF:
446 break;
447 case SND_SOC_DAIFMT_IB_IF:
448 iface |= 0x0180;
449 break;
450 case SND_SOC_DAIFMT_IB_NF:
451 iface |= 0x0100;
452 break;
453 case SND_SOC_DAIFMT_NB_IF:
454 iface |= 0x0080;
455 break;
456 default:
457 return -EINVAL;
458 }
459
460 wm8510_write(codec, WM8510_IFACE, iface);
461 wm8510_write(codec, WM8510_CLOCK, clk);
462 return 0;
463}
464
465static int wm8510_pcm_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000466 struct snd_pcm_hw_params *params,
467 struct snd_soc_dai *dai)
Mark Brown5d421512008-06-05 13:49:32 +0100468{
469 struct snd_soc_pcm_runtime *rtd = substream->private_data;
470 struct snd_soc_device *socdev = rtd->socdev;
471 struct snd_soc_codec *codec = socdev->codec;
472 u16 iface = wm8510_read_reg_cache(codec, WM8510_IFACE) & 0x19f;
473 u16 adn = wm8510_read_reg_cache(codec, WM8510_ADD) & 0x1f1;
474
475 /* bit size */
476 switch (params_format(params)) {
477 case SNDRV_PCM_FORMAT_S16_LE:
478 break;
479 case SNDRV_PCM_FORMAT_S20_3LE:
480 iface |= 0x0020;
481 break;
482 case SNDRV_PCM_FORMAT_S24_LE:
483 iface |= 0x0040;
484 break;
485 case SNDRV_PCM_FORMAT_S32_LE:
486 iface |= 0x0060;
487 break;
488 }
489
490 /* filter coefficient */
491 switch (params_rate(params)) {
492 case SNDRV_PCM_RATE_8000:
493 adn |= 0x5 << 1;
494 break;
495 case SNDRV_PCM_RATE_11025:
496 adn |= 0x4 << 1;
497 break;
498 case SNDRV_PCM_RATE_16000:
499 adn |= 0x3 << 1;
500 break;
501 case SNDRV_PCM_RATE_22050:
502 adn |= 0x2 << 1;
503 break;
504 case SNDRV_PCM_RATE_32000:
505 adn |= 0x1 << 1;
506 break;
507 case SNDRV_PCM_RATE_44100:
508 case SNDRV_PCM_RATE_48000:
509 break;
510 }
511
512 wm8510_write(codec, WM8510_IFACE, iface);
513 wm8510_write(codec, WM8510_ADD, adn);
514 return 0;
515}
516
Liam Girdwoode550e172008-07-07 16:07:52 +0100517static int wm8510_mute(struct snd_soc_dai *dai, int mute)
Mark Brown5d421512008-06-05 13:49:32 +0100518{
519 struct snd_soc_codec *codec = dai->codec;
520 u16 mute_reg = wm8510_read_reg_cache(codec, WM8510_DAC) & 0xffbf;
521
522 if (mute)
523 wm8510_write(codec, WM8510_DAC, mute_reg | 0x40);
524 else
525 wm8510_write(codec, WM8510_DAC, mute_reg);
526 return 0;
527}
528
529/* liam need to make this lower power with dapm */
530static int wm8510_set_bias_level(struct snd_soc_codec *codec,
531 enum snd_soc_bias_level level)
532{
Mark Brown09af98b2008-10-07 13:04:58 +0100533 u16 power1 = wm8510_read_reg_cache(codec, WM8510_POWER1) & ~0x3;
Mark Brown5d421512008-06-05 13:49:32 +0100534
535 switch (level) {
536 case SND_SOC_BIAS_ON:
Mark Brown5d421512008-06-05 13:49:32 +0100537 case SND_SOC_BIAS_PREPARE:
Mark Brown09af98b2008-10-07 13:04:58 +0100538 power1 |= 0x1; /* VMID 50k */
539 wm8510_write(codec, WM8510_POWER1, power1);
Mark Brown5d421512008-06-05 13:49:32 +0100540 break;
Mark Brown09af98b2008-10-07 13:04:58 +0100541
542 case SND_SOC_BIAS_STANDBY:
543 power1 |= WM8510_POWER1_BIASEN | WM8510_POWER1_BUFIOEN;
544
545 if (codec->bias_level == SND_SOC_BIAS_OFF) {
546 /* Initial cap charge at VMID 5k */
547 wm8510_write(codec, WM8510_POWER1, power1 | 0x3);
548 mdelay(100);
549 }
550
551 power1 |= 0x2; /* VMID 500k */
552 wm8510_write(codec, WM8510_POWER1, power1);
553 break;
554
Mark Brown5d421512008-06-05 13:49:32 +0100555 case SND_SOC_BIAS_OFF:
Mark Brown09af98b2008-10-07 13:04:58 +0100556 wm8510_write(codec, WM8510_POWER1, 0);
557 wm8510_write(codec, WM8510_POWER2, 0);
558 wm8510_write(codec, WM8510_POWER3, 0);
Mark Brown5d421512008-06-05 13:49:32 +0100559 break;
560 }
Mark Brown09af98b2008-10-07 13:04:58 +0100561
Mark Brown5d421512008-06-05 13:49:32 +0100562 codec->bias_level = level;
563 return 0;
564}
565
566#define WM8510_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
567 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
568 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
569
570#define WM8510_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
571 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
572
Liam Girdwoode550e172008-07-07 16:07:52 +0100573struct snd_soc_dai wm8510_dai = {
Mark Brown5d421512008-06-05 13:49:32 +0100574 .name = "WM8510 HiFi",
575 .playback = {
576 .stream_name = "Playback",
577 .channels_min = 2,
578 .channels_max = 2,
579 .rates = WM8510_RATES,
580 .formats = WM8510_FORMATS,},
581 .capture = {
582 .stream_name = "Capture",
583 .channels_min = 2,
584 .channels_max = 2,
585 .rates = WM8510_RATES,
586 .formats = WM8510_FORMATS,},
587 .ops = {
588 .hw_params = wm8510_pcm_hw_params,
Mark Brown5d421512008-06-05 13:49:32 +0100589 .digital_mute = wm8510_mute,
590 .set_fmt = wm8510_set_dai_fmt,
591 .set_clkdiv = wm8510_set_dai_clkdiv,
592 .set_pll = wm8510_set_dai_pll,
593 },
594};
595EXPORT_SYMBOL_GPL(wm8510_dai);
596
597static int wm8510_suspend(struct platform_device *pdev, pm_message_t state)
598{
599 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
600 struct snd_soc_codec *codec = socdev->codec;
601
602 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
603 return 0;
604}
605
606static int wm8510_resume(struct platform_device *pdev)
607{
608 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
609 struct snd_soc_codec *codec = socdev->codec;
610 int i;
611 u8 data[2];
612 u16 *cache = codec->reg_cache;
613
614 /* Sync reg_cache with the hardware */
615 for (i = 0; i < ARRAY_SIZE(wm8510_reg); i++) {
616 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
617 data[1] = cache[i] & 0x00ff;
618 codec->hw_write(codec->control_data, data, 2);
619 }
620 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
621 wm8510_set_bias_level(codec, codec->suspend_bias_level);
622 return 0;
623}
624
625/*
626 * initialise the WM8510 driver
627 * register the mixer and dsp interfaces with the kernel
628 */
629static int wm8510_init(struct snd_soc_device *socdev)
630{
631 struct snd_soc_codec *codec = socdev->codec;
632 int ret = 0;
633
634 codec->name = "WM8510";
635 codec->owner = THIS_MODULE;
636 codec->read = wm8510_read_reg_cache;
637 codec->write = wm8510_write;
638 codec->set_bias_level = wm8510_set_bias_level;
639 codec->dai = &wm8510_dai;
640 codec->num_dai = 1;
641 codec->reg_cache_size = ARRAY_SIZE(wm8510_reg);
642 codec->reg_cache = kmemdup(wm8510_reg, sizeof(wm8510_reg), GFP_KERNEL);
643
644 if (codec->reg_cache == NULL)
645 return -ENOMEM;
646
647 wm8510_reset(codec);
648
649 /* register pcms */
650 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
651 if (ret < 0) {
652 printk(KERN_ERR "wm8510: failed to create pcms\n");
653 goto pcm_err;
654 }
655
656 /* power on device */
Mark Brown09af98b2008-10-07 13:04:58 +0100657 codec->bias_level = SND_SOC_BIAS_OFF;
Mark Brown5d421512008-06-05 13:49:32 +0100658 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
659 wm8510_add_controls(codec);
660 wm8510_add_widgets(codec);
Mark Brown968a6022008-11-28 11:49:07 +0000661 ret = snd_soc_init_card(socdev);
Mark Brown5d421512008-06-05 13:49:32 +0100662 if (ret < 0) {
663 printk(KERN_ERR "wm8510: failed to register card\n");
664 goto card_err;
665 }
666 return ret;
667
668card_err:
669 snd_soc_free_pcms(socdev);
670 snd_soc_dapm_free(socdev);
671pcm_err:
672 kfree(codec->reg_cache);
673 return ret;
674}
675
676static struct snd_soc_device *wm8510_socdev;
677
678#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
679
680/*
681 * WM8510 2 wire address is 0x1a
682 */
Mark Brown5d421512008-06-05 13:49:32 +0100683
Jean Delvare41759c22008-09-02 17:07:30 +0200684static int wm8510_i2c_probe(struct i2c_client *i2c,
685 const struct i2c_device_id *id)
Mark Brown5d421512008-06-05 13:49:32 +0100686{
687 struct snd_soc_device *socdev = wm8510_socdev;
Mark Brown5d421512008-06-05 13:49:32 +0100688 struct snd_soc_codec *codec = socdev->codec;
Mark Brown5d421512008-06-05 13:49:32 +0100689 int ret;
690
Mark Brown5d421512008-06-05 13:49:32 +0100691 i2c_set_clientdata(i2c, codec);
692 codec->control_data = i2c;
693
Mark Brown5d421512008-06-05 13:49:32 +0100694 ret = wm8510_init(socdev);
Jean Delvare41759c22008-09-02 17:07:30 +0200695 if (ret < 0)
Mark Browna5c95e92008-06-23 14:51:29 +0100696 pr_err("failed to initialise WM8510\n");
Mark Brown5d421512008-06-05 13:49:32 +0100697
Mark Brown5d421512008-06-05 13:49:32 +0100698 return ret;
699}
700
Jean Delvare41759c22008-09-02 17:07:30 +0200701static int wm8510_i2c_remove(struct i2c_client *client)
Mark Brown5d421512008-06-05 13:49:32 +0100702{
703 struct snd_soc_codec *codec = i2c_get_clientdata(client);
Mark Brown5d421512008-06-05 13:49:32 +0100704 kfree(codec->reg_cache);
Mark Brown5d421512008-06-05 13:49:32 +0100705 return 0;
706}
707
Jean Delvare41759c22008-09-02 17:07:30 +0200708static const struct i2c_device_id wm8510_i2c_id[] = {
709 { "wm8510", 0 },
710 { }
711};
712MODULE_DEVICE_TABLE(i2c, wm8510_i2c_id);
Mark Brown5d421512008-06-05 13:49:32 +0100713
Mark Brown5d421512008-06-05 13:49:32 +0100714static struct i2c_driver wm8510_i2c_driver = {
715 .driver = {
716 .name = "WM8510 I2C Codec",
717 .owner = THIS_MODULE,
718 },
Jean Delvare41759c22008-09-02 17:07:30 +0200719 .probe = wm8510_i2c_probe,
720 .remove = wm8510_i2c_remove,
721 .id_table = wm8510_i2c_id,
Mark Brown5d421512008-06-05 13:49:32 +0100722};
723
Jean Delvare41759c22008-09-02 17:07:30 +0200724static int wm8510_add_i2c_device(struct platform_device *pdev,
725 const struct wm8510_setup_data *setup)
726{
727 struct i2c_board_info info;
728 struct i2c_adapter *adapter;
729 struct i2c_client *client;
730 int ret;
731
732 ret = i2c_add_driver(&wm8510_i2c_driver);
733 if (ret != 0) {
734 dev_err(&pdev->dev, "can't add i2c driver\n");
735 return ret;
736 }
737
738 memset(&info, 0, sizeof(struct i2c_board_info));
739 info.addr = setup->i2c_address;
740 strlcpy(info.type, "wm8510", I2C_NAME_SIZE);
741
742 adapter = i2c_get_adapter(setup->i2c_bus);
743 if (!adapter) {
744 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
745 setup->i2c_bus);
746 goto err_driver;
747 }
748
749 client = i2c_new_device(adapter, &info);
750 i2c_put_adapter(adapter);
751 if (!client) {
752 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
753 (unsigned int)info.addr);
754 goto err_driver;
755 }
756
757 return 0;
758
759err_driver:
760 i2c_del_driver(&wm8510_i2c_driver);
761 return -ENODEV;
762}
Mark Brown5d421512008-06-05 13:49:32 +0100763#endif
764
Mark Brown5e357952008-10-07 11:56:20 +0100765#if defined(CONFIG_SPI_MASTER)
766static int __devinit wm8510_spi_probe(struct spi_device *spi)
767{
768 struct snd_soc_device *socdev = wm8510_socdev;
769 struct snd_soc_codec *codec = socdev->codec;
770 int ret;
771
772 codec->control_data = spi;
773
774 ret = wm8510_init(socdev);
775 if (ret < 0)
776 dev_err(&spi->dev, "failed to initialise WM8510\n");
777
778 return ret;
779}
780
781static int __devexit wm8510_spi_remove(struct spi_device *spi)
782{
783 return 0;
784}
785
786static struct spi_driver wm8510_spi_driver = {
787 .driver = {
788 .name = "wm8510",
789 .bus = &spi_bus_type,
790 .owner = THIS_MODULE,
791 },
792 .probe = wm8510_spi_probe,
793 .remove = __devexit_p(wm8510_spi_remove),
794};
795
796static int wm8510_spi_write(struct spi_device *spi, const char *data, int len)
797{
798 struct spi_transfer t;
799 struct spi_message m;
800 u8 msg[2];
801
802 if (len <= 0)
803 return 0;
804
805 msg[0] = data[0];
806 msg[1] = data[1];
807
808 spi_message_init(&m);
809 memset(&t, 0, (sizeof t));
810
811 t.tx_buf = &msg[0];
812 t.len = len;
813
814 spi_message_add_tail(&t, &m);
815 spi_sync(spi, &m);
816
817 return len;
818}
819#endif /* CONFIG_SPI_MASTER */
820
Mark Brown5d421512008-06-05 13:49:32 +0100821static int wm8510_probe(struct platform_device *pdev)
822{
823 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
824 struct wm8510_setup_data *setup;
825 struct snd_soc_codec *codec;
826 int ret = 0;
827
Mark Browna5c95e92008-06-23 14:51:29 +0100828 pr_info("WM8510 Audio Codec %s", WM8510_VERSION);
Mark Brown5d421512008-06-05 13:49:32 +0100829
830 setup = socdev->codec_data;
831 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
832 if (codec == NULL)
833 return -ENOMEM;
834
835 socdev->codec = codec;
836 mutex_init(&codec->mutex);
837 INIT_LIST_HEAD(&codec->dapm_widgets);
838 INIT_LIST_HEAD(&codec->dapm_paths);
839
840 wm8510_socdev = socdev;
841#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
842 if (setup->i2c_address) {
Mark Brown5d421512008-06-05 13:49:32 +0100843 codec->hw_write = (hw_write_t)i2c_master_send;
Jean Delvare41759c22008-09-02 17:07:30 +0200844 ret = wm8510_add_i2c_device(pdev, setup);
Mark Brown5d421512008-06-05 13:49:32 +0100845 }
Mark Brown5e357952008-10-07 11:56:20 +0100846#endif
847#if defined(CONFIG_SPI_MASTER)
848 if (setup->spi) {
849 codec->hw_write = (hw_write_t)wm8510_spi_write;
850 ret = spi_register_driver(&wm8510_spi_driver);
851 if (ret != 0)
852 printk(KERN_ERR "can't add spi driver");
853 }
Mark Brown5d421512008-06-05 13:49:32 +0100854#endif
Jean Delvare3051e412008-08-25 11:49:20 +0100855
856 if (ret != 0)
857 kfree(codec);
Mark Brown5d421512008-06-05 13:49:32 +0100858 return ret;
859}
860
861/* power down chip */
862static int wm8510_remove(struct platform_device *pdev)
863{
864 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
865 struct snd_soc_codec *codec = socdev->codec;
866
867 if (codec->control_data)
868 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF);
869
870 snd_soc_free_pcms(socdev);
871 snd_soc_dapm_free(socdev);
872#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Jean Delvare41759c22008-09-02 17:07:30 +0200873 i2c_unregister_device(codec->control_data);
Mark Brown5d421512008-06-05 13:49:32 +0100874 i2c_del_driver(&wm8510_i2c_driver);
875#endif
Mark Brown5e357952008-10-07 11:56:20 +0100876#if defined(CONFIG_SPI_MASTER)
877 spi_unregister_driver(&wm8510_spi_driver);
878#endif
Mark Brown5d421512008-06-05 13:49:32 +0100879 kfree(codec);
880
881 return 0;
882}
883
884struct snd_soc_codec_device soc_codec_dev_wm8510 = {
885 .probe = wm8510_probe,
886 .remove = wm8510_remove,
887 .suspend = wm8510_suspend,
888 .resume = wm8510_resume,
889};
890EXPORT_SYMBOL_GPL(soc_codec_dev_wm8510);
891
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100892static int __init wm8510_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000893{
894 return snd_soc_register_dai(&wm8510_dai);
895}
896module_init(wm8510_modinit);
897
898static void __exit wm8510_exit(void)
899{
900 snd_soc_unregister_dai(&wm8510_dai);
901}
902module_exit(wm8510_exit);
903
Mark Brown5d421512008-06-05 13:49:32 +0100904MODULE_DESCRIPTION("ASoC WM8510 driver");
905MODULE_AUTHOR("Liam Girdwood");
906MODULE_LICENSE("GPL");