Linux 2.6.31.8
[linux/fpc-iii.git] / sound / soc / codecs / wm8580.c
blob86c4b24db8172aa610d283120476440587035c3d
1 /*
2 * wm8580.c -- WM8580 ALSA Soc Audio driver
4 * Copyright 2008, 2009 Wolfson Microelectronics PLC.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
11 * Notes:
12 * The WM8580 is a multichannel codec with S/PDIF support, featuring six
13 * DAC channels and two ADC channels.
15 * Currently only the primary audio interface is supported - S/PDIF and
16 * the secondary audio interfaces are not.
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/pm.h>
25 #include <linux/i2c.h>
26 #include <linux/platform_device.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/soc-dapm.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <asm/div64.h>
36 #include "wm8580.h"
38 /* WM8580 register space */
39 #define WM8580_PLLA1 0x00
40 #define WM8580_PLLA2 0x01
41 #define WM8580_PLLA3 0x02
42 #define WM8580_PLLA4 0x03
43 #define WM8580_PLLB1 0x04
44 #define WM8580_PLLB2 0x05
45 #define WM8580_PLLB3 0x06
46 #define WM8580_PLLB4 0x07
47 #define WM8580_CLKSEL 0x08
48 #define WM8580_PAIF1 0x09
49 #define WM8580_PAIF2 0x0A
50 #define WM8580_SAIF1 0x0B
51 #define WM8580_PAIF3 0x0C
52 #define WM8580_PAIF4 0x0D
53 #define WM8580_SAIF2 0x0E
54 #define WM8580_DAC_CONTROL1 0x0F
55 #define WM8580_DAC_CONTROL2 0x10
56 #define WM8580_DAC_CONTROL3 0x11
57 #define WM8580_DAC_CONTROL4 0x12
58 #define WM8580_DAC_CONTROL5 0x13
59 #define WM8580_DIGITAL_ATTENUATION_DACL1 0x14
60 #define WM8580_DIGITAL_ATTENUATION_DACR1 0x15
61 #define WM8580_DIGITAL_ATTENUATION_DACL2 0x16
62 #define WM8580_DIGITAL_ATTENUATION_DACR2 0x17
63 #define WM8580_DIGITAL_ATTENUATION_DACL3 0x18
64 #define WM8580_DIGITAL_ATTENUATION_DACR3 0x19
65 #define WM8580_MASTER_DIGITAL_ATTENUATION 0x1C
66 #define WM8580_ADC_CONTROL1 0x1D
67 #define WM8580_SPDTXCHAN0 0x1E
68 #define WM8580_SPDTXCHAN1 0x1F
69 #define WM8580_SPDTXCHAN2 0x20
70 #define WM8580_SPDTXCHAN3 0x21
71 #define WM8580_SPDTXCHAN4 0x22
72 #define WM8580_SPDTXCHAN5 0x23
73 #define WM8580_SPDMODE 0x24
74 #define WM8580_INTMASK 0x25
75 #define WM8580_GPO1 0x26
76 #define WM8580_GPO2 0x27
77 #define WM8580_GPO3 0x28
78 #define WM8580_GPO4 0x29
79 #define WM8580_GPO5 0x2A
80 #define WM8580_INTSTAT 0x2B
81 #define WM8580_SPDRXCHAN1 0x2C
82 #define WM8580_SPDRXCHAN2 0x2D
83 #define WM8580_SPDRXCHAN3 0x2E
84 #define WM8580_SPDRXCHAN4 0x2F
85 #define WM8580_SPDRXCHAN5 0x30
86 #define WM8580_SPDSTAT 0x31
87 #define WM8580_PWRDN1 0x32
88 #define WM8580_PWRDN2 0x33
89 #define WM8580_READBACK 0x34
90 #define WM8580_RESET 0x35
92 #define WM8580_MAX_REGISTER 0x35
94 /* PLLB4 (register 7h) */
95 #define WM8580_PLLB4_MCLKOUTSRC_MASK 0x60
96 #define WM8580_PLLB4_MCLKOUTSRC_PLLA 0x20
97 #define WM8580_PLLB4_MCLKOUTSRC_PLLB 0x40
98 #define WM8580_PLLB4_MCLKOUTSRC_OSC 0x60
100 #define WM8580_PLLB4_CLKOUTSRC_MASK 0x180
101 #define WM8580_PLLB4_CLKOUTSRC_PLLACLK 0x080
102 #define WM8580_PLLB4_CLKOUTSRC_PLLBCLK 0x100
103 #define WM8580_PLLB4_CLKOUTSRC_OSCCLK 0x180
105 /* CLKSEL (register 8h) */
106 #define WM8580_CLKSEL_DAC_CLKSEL_MASK 0x03
107 #define WM8580_CLKSEL_DAC_CLKSEL_PLLA 0x01
108 #define WM8580_CLKSEL_DAC_CLKSEL_PLLB 0x02
110 /* AIF control 1 (registers 9h-bh) */
111 #define WM8580_AIF_RATE_MASK 0x7
112 #define WM8580_AIF_RATE_128 0x0
113 #define WM8580_AIF_RATE_192 0x1
114 #define WM8580_AIF_RATE_256 0x2
115 #define WM8580_AIF_RATE_384 0x3
116 #define WM8580_AIF_RATE_512 0x4
117 #define WM8580_AIF_RATE_768 0x5
118 #define WM8580_AIF_RATE_1152 0x6
120 #define WM8580_AIF_BCLKSEL_MASK 0x18
121 #define WM8580_AIF_BCLKSEL_64 0x00
122 #define WM8580_AIF_BCLKSEL_128 0x08
123 #define WM8580_AIF_BCLKSEL_256 0x10
124 #define WM8580_AIF_BCLKSEL_SYSCLK 0x18
126 #define WM8580_AIF_MS 0x20
128 #define WM8580_AIF_CLKSRC_MASK 0xc0
129 #define WM8580_AIF_CLKSRC_PLLA 0x40
130 #define WM8580_AIF_CLKSRC_PLLB 0x40
131 #define WM8580_AIF_CLKSRC_MCLK 0xc0
133 /* AIF control 2 (registers ch-eh) */
134 #define WM8580_AIF_FMT_MASK 0x03
135 #define WM8580_AIF_FMT_RIGHTJ 0x00
136 #define WM8580_AIF_FMT_LEFTJ 0x01
137 #define WM8580_AIF_FMT_I2S 0x02
138 #define WM8580_AIF_FMT_DSP 0x03
140 #define WM8580_AIF_LENGTH_MASK 0x0c
141 #define WM8580_AIF_LENGTH_16 0x00
142 #define WM8580_AIF_LENGTH_20 0x04
143 #define WM8580_AIF_LENGTH_24 0x08
144 #define WM8580_AIF_LENGTH_32 0x0c
146 #define WM8580_AIF_LRP 0x10
147 #define WM8580_AIF_BCP 0x20
149 /* Powerdown Register 1 (register 32h) */
150 #define WM8580_PWRDN1_PWDN 0x001
151 #define WM8580_PWRDN1_ALLDACPD 0x040
153 /* Powerdown Register 2 (register 33h) */
154 #define WM8580_PWRDN2_OSSCPD 0x001
155 #define WM8580_PWRDN2_PLLAPD 0x002
156 #define WM8580_PWRDN2_PLLBPD 0x004
157 #define WM8580_PWRDN2_SPDIFPD 0x008
158 #define WM8580_PWRDN2_SPDIFTXD 0x010
159 #define WM8580_PWRDN2_SPDIFRXD 0x020
161 #define WM8580_DAC_CONTROL5_MUTEALL 0x10
164 * wm8580 register cache
165 * We can't read the WM8580 register space when we
166 * are using 2 wire for device control, so we cache them instead.
168 static const u16 wm8580_reg[] = {
169 0x0121, 0x017e, 0x007d, 0x0014, /*R3*/
170 0x0121, 0x017e, 0x007d, 0x0194, /*R7*/
171 0x001c, 0x0002, 0x0002, 0x00c2, /*R11*/
172 0x0182, 0x0082, 0x000a, 0x0024, /*R15*/
173 0x0009, 0x0000, 0x00ff, 0x0000, /*R19*/
174 0x00ff, 0x00ff, 0x00ff, 0x00ff, /*R23*/
175 0x00ff, 0x00ff, 0x00ff, 0x00ff, /*R27*/
176 0x01f0, 0x0040, 0x0000, 0x0000, /*R31(0x1F)*/
177 0x0000, 0x0000, 0x0031, 0x000b, /*R35*/
178 0x0039, 0x0000, 0x0010, 0x0032, /*R39*/
179 0x0054, 0x0076, 0x0098, 0x0000, /*R43(0x2B)*/
180 0x0000, 0x0000, 0x0000, 0x0000, /*R47*/
181 0x0000, 0x0000, 0x005e, 0x003e, /*R51(0x33)*/
182 0x0000, 0x0000 /*R53*/
185 struct pll_state {
186 unsigned int in;
187 unsigned int out;
190 /* codec private data */
191 struct wm8580_priv {
192 struct snd_soc_codec codec;
193 u16 reg_cache[WM8580_MAX_REGISTER + 1];
194 struct pll_state a;
195 struct pll_state b;
200 * read wm8580 register cache
202 static inline unsigned int wm8580_read_reg_cache(struct snd_soc_codec *codec,
203 unsigned int reg)
205 u16 *cache = codec->reg_cache;
206 BUG_ON(reg >= ARRAY_SIZE(wm8580_reg));
207 return cache[reg];
211 * write wm8580 register cache
213 static inline void wm8580_write_reg_cache(struct snd_soc_codec *codec,
214 unsigned int reg, unsigned int value)
216 u16 *cache = codec->reg_cache;
218 cache[reg] = value;
222 * write to the WM8580 register space
224 static int wm8580_write(struct snd_soc_codec *codec, unsigned int reg,
225 unsigned int value)
227 u8 data[2];
229 BUG_ON(reg >= ARRAY_SIZE(wm8580_reg));
231 /* Registers are 9 bits wide */
232 value &= 0x1ff;
234 switch (reg) {
235 case WM8580_RESET:
236 /* Uncached */
237 break;
238 default:
239 if (value == wm8580_read_reg_cache(codec, reg))
240 return 0;
243 /* data is
244 * D15..D9 WM8580 register offset
245 * D8...D0 register data
247 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
248 data[1] = value & 0x00ff;
250 wm8580_write_reg_cache(codec, reg, value);
251 if (codec->hw_write(codec->control_data, data, 2) == 2)
252 return 0;
253 else
254 return -EIO;
257 static inline unsigned int wm8580_read(struct snd_soc_codec *codec,
258 unsigned int reg)
260 switch (reg) {
261 default:
262 return wm8580_read_reg_cache(codec, reg);
266 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
268 static int wm8580_out_vu(struct snd_kcontrol *kcontrol,
269 struct snd_ctl_elem_value *ucontrol)
271 struct soc_mixer_control *mc =
272 (struct soc_mixer_control *)kcontrol->private_value;
273 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
274 unsigned int reg = mc->reg;
275 unsigned int reg2 = mc->rreg;
276 int ret;
277 u16 val;
279 /* Clear the register cache so we write without VU set */
280 wm8580_write_reg_cache(codec, reg, 0);
281 wm8580_write_reg_cache(codec, reg2, 0);
283 ret = snd_soc_put_volsw_2r(kcontrol, ucontrol);
284 if (ret < 0)
285 return ret;
287 /* Now write again with the volume update bit set */
288 val = wm8580_read_reg_cache(codec, reg);
289 wm8580_write(codec, reg, val | 0x0100);
291 val = wm8580_read_reg_cache(codec, reg2);
292 wm8580_write(codec, reg2, val | 0x0100);
294 return 0;
297 #define SOC_WM8580_OUT_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax, \
298 xinvert, tlv_array) \
299 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
300 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
301 SNDRV_CTL_ELEM_ACCESS_READWRITE, \
302 .tlv.p = (tlv_array), \
303 .info = snd_soc_info_volsw_2r, \
304 .get = snd_soc_get_volsw_2r, .put = wm8580_out_vu, \
305 .private_value = (unsigned long)&(struct soc_mixer_control) \
306 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
307 .max = xmax, .invert = xinvert} }
309 static const struct snd_kcontrol_new wm8580_snd_controls[] = {
310 SOC_WM8580_OUT_DOUBLE_R_TLV("DAC1 Playback Volume",
311 WM8580_DIGITAL_ATTENUATION_DACL1,
312 WM8580_DIGITAL_ATTENUATION_DACR1,
313 0, 0xff, 0, dac_tlv),
314 SOC_WM8580_OUT_DOUBLE_R_TLV("DAC2 Playback Volume",
315 WM8580_DIGITAL_ATTENUATION_DACL2,
316 WM8580_DIGITAL_ATTENUATION_DACR2,
317 0, 0xff, 0, dac_tlv),
318 SOC_WM8580_OUT_DOUBLE_R_TLV("DAC3 Playback Volume",
319 WM8580_DIGITAL_ATTENUATION_DACL3,
320 WM8580_DIGITAL_ATTENUATION_DACR3,
321 0, 0xff, 0, dac_tlv),
323 SOC_SINGLE("DAC1 Deemphasis Switch", WM8580_DAC_CONTROL3, 0, 1, 0),
324 SOC_SINGLE("DAC2 Deemphasis Switch", WM8580_DAC_CONTROL3, 1, 1, 0),
325 SOC_SINGLE("DAC3 Deemphasis Switch", WM8580_DAC_CONTROL3, 2, 1, 0),
327 SOC_DOUBLE("DAC1 Invert Switch", WM8580_DAC_CONTROL4, 0, 1, 1, 0),
328 SOC_DOUBLE("DAC2 Invert Switch", WM8580_DAC_CONTROL4, 2, 3, 1, 0),
329 SOC_DOUBLE("DAC3 Invert Switch", WM8580_DAC_CONTROL4, 4, 5, 1, 0),
331 SOC_SINGLE("DAC ZC Switch", WM8580_DAC_CONTROL5, 5, 1, 0),
332 SOC_SINGLE("DAC1 Switch", WM8580_DAC_CONTROL5, 0, 1, 0),
333 SOC_SINGLE("DAC2 Switch", WM8580_DAC_CONTROL5, 1, 1, 0),
334 SOC_SINGLE("DAC3 Switch", WM8580_DAC_CONTROL5, 2, 1, 0),
336 SOC_DOUBLE("ADC Mute Switch", WM8580_ADC_CONTROL1, 0, 1, 1, 0),
337 SOC_SINGLE("ADC High-Pass Filter Switch", WM8580_ADC_CONTROL1, 4, 1, 0),
340 static const struct snd_soc_dapm_widget wm8580_dapm_widgets[] = {
341 SND_SOC_DAPM_DAC("DAC1", "Playback", WM8580_PWRDN1, 2, 1),
342 SND_SOC_DAPM_DAC("DAC2", "Playback", WM8580_PWRDN1, 3, 1),
343 SND_SOC_DAPM_DAC("DAC3", "Playback", WM8580_PWRDN1, 4, 1),
345 SND_SOC_DAPM_OUTPUT("VOUT1L"),
346 SND_SOC_DAPM_OUTPUT("VOUT1R"),
347 SND_SOC_DAPM_OUTPUT("VOUT2L"),
348 SND_SOC_DAPM_OUTPUT("VOUT2R"),
349 SND_SOC_DAPM_OUTPUT("VOUT3L"),
350 SND_SOC_DAPM_OUTPUT("VOUT3R"),
352 SND_SOC_DAPM_ADC("ADC", "Capture", WM8580_PWRDN1, 1, 1),
354 SND_SOC_DAPM_INPUT("AINL"),
355 SND_SOC_DAPM_INPUT("AINR"),
358 static const struct snd_soc_dapm_route audio_map[] = {
359 { "VOUT1L", NULL, "DAC1" },
360 { "VOUT1R", NULL, "DAC1" },
362 { "VOUT2L", NULL, "DAC2" },
363 { "VOUT2R", NULL, "DAC2" },
365 { "VOUT3L", NULL, "DAC3" },
366 { "VOUT3R", NULL, "DAC3" },
368 { "ADC", NULL, "AINL" },
369 { "ADC", NULL, "AINR" },
372 static int wm8580_add_widgets(struct snd_soc_codec *codec)
374 snd_soc_dapm_new_controls(codec, wm8580_dapm_widgets,
375 ARRAY_SIZE(wm8580_dapm_widgets));
377 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
379 snd_soc_dapm_new_widgets(codec);
380 return 0;
383 /* PLL divisors */
384 struct _pll_div {
385 u32 prescale:1;
386 u32 postscale:1;
387 u32 freqmode:2;
388 u32 n:4;
389 u32 k:24;
392 /* The size in bits of the pll divide */
393 #define FIXED_PLL_SIZE (1 << 22)
395 /* PLL rate to output rate divisions */
396 static struct {
397 unsigned int div;
398 unsigned int freqmode;
399 unsigned int postscale;
400 } post_table[] = {
401 { 2, 0, 0 },
402 { 4, 0, 1 },
403 { 4, 1, 0 },
404 { 8, 1, 1 },
405 { 8, 2, 0 },
406 { 16, 2, 1 },
407 { 12, 3, 0 },
408 { 24, 3, 1 }
411 static int pll_factors(struct _pll_div *pll_div, unsigned int target,
412 unsigned int source)
414 u64 Kpart;
415 unsigned int K, Ndiv, Nmod;
416 int i;
418 pr_debug("wm8580: PLL %uHz->%uHz\n", source, target);
420 /* Scale the output frequency up; the PLL should run in the
421 * region of 90-100MHz.
423 for (i = 0; i < ARRAY_SIZE(post_table); i++) {
424 if (target * post_table[i].div >= 90000000 &&
425 target * post_table[i].div <= 100000000) {
426 pll_div->freqmode = post_table[i].freqmode;
427 pll_div->postscale = post_table[i].postscale;
428 target *= post_table[i].div;
429 break;
433 if (i == ARRAY_SIZE(post_table)) {
434 printk(KERN_ERR "wm8580: Unable to scale output frequency "
435 "%u\n", target);
436 return -EINVAL;
439 Ndiv = target / source;
441 if (Ndiv < 5) {
442 source /= 2;
443 pll_div->prescale = 1;
444 Ndiv = target / source;
445 } else
446 pll_div->prescale = 0;
448 if ((Ndiv < 5) || (Ndiv > 13)) {
449 printk(KERN_ERR
450 "WM8580 N=%u outside supported range\n", Ndiv);
451 return -EINVAL;
454 pll_div->n = Ndiv;
455 Nmod = target % source;
456 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
458 do_div(Kpart, source);
460 K = Kpart & 0xFFFFFFFF;
462 pll_div->k = K;
464 pr_debug("PLL %x.%x prescale %d freqmode %d postscale %d\n",
465 pll_div->n, pll_div->k, pll_div->prescale, pll_div->freqmode,
466 pll_div->postscale);
468 return 0;
471 static int wm8580_set_dai_pll(struct snd_soc_dai *codec_dai,
472 int pll_id, unsigned int freq_in, unsigned int freq_out)
474 int offset;
475 struct snd_soc_codec *codec = codec_dai->codec;
476 struct wm8580_priv *wm8580 = codec->private_data;
477 struct pll_state *state;
478 struct _pll_div pll_div;
479 unsigned int reg;
480 unsigned int pwr_mask;
481 int ret;
483 /* GCC isn't able to work out the ifs below for initialising/using
484 * pll_div so suppress warnings.
486 memset(&pll_div, 0, sizeof(pll_div));
488 switch (pll_id) {
489 case WM8580_PLLA:
490 state = &wm8580->a;
491 offset = 0;
492 pwr_mask = WM8580_PWRDN2_PLLAPD;
493 break;
494 case WM8580_PLLB:
495 state = &wm8580->b;
496 offset = 4;
497 pwr_mask = WM8580_PWRDN2_PLLBPD;
498 break;
499 default:
500 return -ENODEV;
503 if (freq_in && freq_out) {
504 ret = pll_factors(&pll_div, freq_out, freq_in);
505 if (ret != 0)
506 return ret;
509 state->in = freq_in;
510 state->out = freq_out;
512 /* Always disable the PLL - it is not safe to leave it running
513 * while reprogramming it.
515 reg = wm8580_read(codec, WM8580_PWRDN2);
516 wm8580_write(codec, WM8580_PWRDN2, reg | pwr_mask);
518 if (!freq_in || !freq_out)
519 return 0;
521 wm8580_write(codec, WM8580_PLLA1 + offset, pll_div.k & 0x1ff);
522 wm8580_write(codec, WM8580_PLLA2 + offset, (pll_div.k >> 9) & 0xff);
523 wm8580_write(codec, WM8580_PLLA3 + offset,
524 (pll_div.k >> 18 & 0xf) | (pll_div.n << 4));
526 reg = wm8580_read(codec, WM8580_PLLA4 + offset);
527 reg &= ~0x3f;
528 reg |= pll_div.prescale | pll_div.postscale << 1 |
529 pll_div.freqmode << 3;
531 wm8580_write(codec, WM8580_PLLA4 + offset, reg);
533 /* All done, turn it on */
534 reg = wm8580_read(codec, WM8580_PWRDN2);
535 wm8580_write(codec, WM8580_PWRDN2, reg & ~pwr_mask);
537 return 0;
541 * Set PCM DAI bit size and sample rate.
543 static int wm8580_paif_hw_params(struct snd_pcm_substream *substream,
544 struct snd_pcm_hw_params *params,
545 struct snd_soc_dai *dai)
547 struct snd_soc_pcm_runtime *rtd = substream->private_data;
548 struct snd_soc_device *socdev = rtd->socdev;
549 struct snd_soc_codec *codec = socdev->card->codec;
550 u16 paifb = wm8580_read(codec, WM8580_PAIF3 + dai->id);
552 paifb &= ~WM8580_AIF_LENGTH_MASK;
553 /* bit size */
554 switch (params_format(params)) {
555 case SNDRV_PCM_FORMAT_S16_LE:
556 break;
557 case SNDRV_PCM_FORMAT_S20_3LE:
558 paifb |= WM8580_AIF_LENGTH_20;
559 break;
560 case SNDRV_PCM_FORMAT_S24_LE:
561 paifb |= WM8580_AIF_LENGTH_24;
562 break;
563 case SNDRV_PCM_FORMAT_S32_LE:
564 paifb |= WM8580_AIF_LENGTH_24;
565 break;
566 default:
567 return -EINVAL;
570 wm8580_write(codec, WM8580_PAIF3 + dai->id, paifb);
571 return 0;
574 static int wm8580_set_paif_dai_fmt(struct snd_soc_dai *codec_dai,
575 unsigned int fmt)
577 struct snd_soc_codec *codec = codec_dai->codec;
578 unsigned int aifa;
579 unsigned int aifb;
580 int can_invert_lrclk;
582 aifa = wm8580_read(codec, WM8580_PAIF1 + codec_dai->id);
583 aifb = wm8580_read(codec, WM8580_PAIF3 + codec_dai->id);
585 aifb &= ~(WM8580_AIF_FMT_MASK | WM8580_AIF_LRP | WM8580_AIF_BCP);
587 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
588 case SND_SOC_DAIFMT_CBS_CFS:
589 aifa &= ~WM8580_AIF_MS;
590 break;
591 case SND_SOC_DAIFMT_CBM_CFM:
592 aifa |= WM8580_AIF_MS;
593 break;
594 default:
595 return -EINVAL;
598 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
599 case SND_SOC_DAIFMT_I2S:
600 can_invert_lrclk = 1;
601 aifb |= WM8580_AIF_FMT_I2S;
602 break;
603 case SND_SOC_DAIFMT_RIGHT_J:
604 can_invert_lrclk = 1;
605 aifb |= WM8580_AIF_FMT_RIGHTJ;
606 break;
607 case SND_SOC_DAIFMT_LEFT_J:
608 can_invert_lrclk = 1;
609 aifb |= WM8580_AIF_FMT_LEFTJ;
610 break;
611 case SND_SOC_DAIFMT_DSP_A:
612 can_invert_lrclk = 0;
613 aifb |= WM8580_AIF_FMT_DSP;
614 break;
615 case SND_SOC_DAIFMT_DSP_B:
616 can_invert_lrclk = 0;
617 aifb |= WM8580_AIF_FMT_DSP;
618 aifb |= WM8580_AIF_LRP;
619 break;
620 default:
621 return -EINVAL;
624 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
625 case SND_SOC_DAIFMT_NB_NF:
626 break;
628 case SND_SOC_DAIFMT_IB_IF:
629 if (!can_invert_lrclk)
630 return -EINVAL;
631 aifb |= WM8580_AIF_BCP;
632 aifb |= WM8580_AIF_LRP;
633 break;
635 case SND_SOC_DAIFMT_IB_NF:
636 aifb |= WM8580_AIF_BCP;
637 break;
639 case SND_SOC_DAIFMT_NB_IF:
640 if (!can_invert_lrclk)
641 return -EINVAL;
642 aifb |= WM8580_AIF_LRP;
643 break;
645 default:
646 return -EINVAL;
649 wm8580_write(codec, WM8580_PAIF1 + codec_dai->id, aifa);
650 wm8580_write(codec, WM8580_PAIF3 + codec_dai->id, aifb);
652 return 0;
655 static int wm8580_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
656 int div_id, int div)
658 struct snd_soc_codec *codec = codec_dai->codec;
659 unsigned int reg;
661 switch (div_id) {
662 case WM8580_MCLK:
663 reg = wm8580_read(codec, WM8580_PLLB4);
664 reg &= ~WM8580_PLLB4_MCLKOUTSRC_MASK;
666 switch (div) {
667 case WM8580_CLKSRC_MCLK:
668 /* Input */
669 break;
671 case WM8580_CLKSRC_PLLA:
672 reg |= WM8580_PLLB4_MCLKOUTSRC_PLLA;
673 break;
674 case WM8580_CLKSRC_PLLB:
675 reg |= WM8580_PLLB4_MCLKOUTSRC_PLLB;
676 break;
678 case WM8580_CLKSRC_OSC:
679 reg |= WM8580_PLLB4_MCLKOUTSRC_OSC;
680 break;
682 default:
683 return -EINVAL;
685 wm8580_write(codec, WM8580_PLLB4, reg);
686 break;
688 case WM8580_DAC_CLKSEL:
689 reg = wm8580_read(codec, WM8580_CLKSEL);
690 reg &= ~WM8580_CLKSEL_DAC_CLKSEL_MASK;
692 switch (div) {
693 case WM8580_CLKSRC_MCLK:
694 break;
696 case WM8580_CLKSRC_PLLA:
697 reg |= WM8580_CLKSEL_DAC_CLKSEL_PLLA;
698 break;
700 case WM8580_CLKSRC_PLLB:
701 reg |= WM8580_CLKSEL_DAC_CLKSEL_PLLB;
702 break;
704 default:
705 return -EINVAL;
707 wm8580_write(codec, WM8580_CLKSEL, reg);
708 break;
710 case WM8580_CLKOUTSRC:
711 reg = wm8580_read(codec, WM8580_PLLB4);
712 reg &= ~WM8580_PLLB4_CLKOUTSRC_MASK;
714 switch (div) {
715 case WM8580_CLKSRC_NONE:
716 break;
718 case WM8580_CLKSRC_PLLA:
719 reg |= WM8580_PLLB4_CLKOUTSRC_PLLACLK;
720 break;
722 case WM8580_CLKSRC_PLLB:
723 reg |= WM8580_PLLB4_CLKOUTSRC_PLLBCLK;
724 break;
726 case WM8580_CLKSRC_OSC:
727 reg |= WM8580_PLLB4_CLKOUTSRC_OSCCLK;
728 break;
730 default:
731 return -EINVAL;
733 wm8580_write(codec, WM8580_PLLB4, reg);
734 break;
736 default:
737 return -EINVAL;
740 return 0;
743 static int wm8580_digital_mute(struct snd_soc_dai *codec_dai, int mute)
745 struct snd_soc_codec *codec = codec_dai->codec;
746 unsigned int reg;
748 reg = wm8580_read(codec, WM8580_DAC_CONTROL5);
750 if (mute)
751 reg |= WM8580_DAC_CONTROL5_MUTEALL;
752 else
753 reg &= ~WM8580_DAC_CONTROL5_MUTEALL;
755 wm8580_write(codec, WM8580_DAC_CONTROL5, reg);
757 return 0;
760 static int wm8580_set_bias_level(struct snd_soc_codec *codec,
761 enum snd_soc_bias_level level)
763 u16 reg;
764 switch (level) {
765 case SND_SOC_BIAS_ON:
766 case SND_SOC_BIAS_PREPARE:
767 break;
769 case SND_SOC_BIAS_STANDBY:
770 if (codec->bias_level == SND_SOC_BIAS_OFF) {
771 /* Power up and get individual control of the DACs */
772 reg = wm8580_read(codec, WM8580_PWRDN1);
773 reg &= ~(WM8580_PWRDN1_PWDN | WM8580_PWRDN1_ALLDACPD);
774 wm8580_write(codec, WM8580_PWRDN1, reg);
776 /* Make VMID high impedence */
777 reg = wm8580_read(codec, WM8580_ADC_CONTROL1);
778 reg &= ~0x100;
779 wm8580_write(codec, WM8580_ADC_CONTROL1, reg);
781 break;
783 case SND_SOC_BIAS_OFF:
784 reg = wm8580_read(codec, WM8580_PWRDN1);
785 wm8580_write(codec, WM8580_PWRDN1, reg | WM8580_PWRDN1_PWDN);
786 break;
788 codec->bias_level = level;
789 return 0;
792 #define WM8580_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
793 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
795 static struct snd_soc_dai_ops wm8580_dai_ops_playback = {
796 .hw_params = wm8580_paif_hw_params,
797 .set_fmt = wm8580_set_paif_dai_fmt,
798 .set_clkdiv = wm8580_set_dai_clkdiv,
799 .set_pll = wm8580_set_dai_pll,
800 .digital_mute = wm8580_digital_mute,
803 static struct snd_soc_dai_ops wm8580_dai_ops_capture = {
804 .hw_params = wm8580_paif_hw_params,
805 .set_fmt = wm8580_set_paif_dai_fmt,
806 .set_clkdiv = wm8580_set_dai_clkdiv,
807 .set_pll = wm8580_set_dai_pll,
810 struct snd_soc_dai wm8580_dai[] = {
812 .name = "WM8580 PAIFRX",
813 .id = 0,
814 .playback = {
815 .stream_name = "Playback",
816 .channels_min = 1,
817 .channels_max = 6,
818 .rates = SNDRV_PCM_RATE_8000_192000,
819 .formats = WM8580_FORMATS,
821 .ops = &wm8580_dai_ops_playback,
824 .name = "WM8580 PAIFTX",
825 .id = 1,
826 .capture = {
827 .stream_name = "Capture",
828 .channels_min = 2,
829 .channels_max = 2,
830 .rates = SNDRV_PCM_RATE_8000_192000,
831 .formats = WM8580_FORMATS,
833 .ops = &wm8580_dai_ops_capture,
836 EXPORT_SYMBOL_GPL(wm8580_dai);
838 static struct snd_soc_codec *wm8580_codec;
840 static int wm8580_probe(struct platform_device *pdev)
842 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
843 struct snd_soc_codec *codec;
844 int ret = 0;
846 if (wm8580_codec == NULL) {
847 dev_err(&pdev->dev, "Codec device not registered\n");
848 return -ENODEV;
851 socdev->card->codec = wm8580_codec;
852 codec = wm8580_codec;
854 /* register pcms */
855 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
856 if (ret < 0) {
857 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
858 goto pcm_err;
861 snd_soc_add_controls(codec, wm8580_snd_controls,
862 ARRAY_SIZE(wm8580_snd_controls));
863 wm8580_add_widgets(codec);
864 ret = snd_soc_init_card(socdev);
865 if (ret < 0) {
866 dev_err(codec->dev, "failed to register card: %d\n", ret);
867 goto card_err;
870 return ret;
872 card_err:
873 snd_soc_free_pcms(socdev);
874 snd_soc_dapm_free(socdev);
875 pcm_err:
876 return ret;
879 /* power down chip */
880 static int wm8580_remove(struct platform_device *pdev)
882 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
884 snd_soc_free_pcms(socdev);
885 snd_soc_dapm_free(socdev);
887 return 0;
890 struct snd_soc_codec_device soc_codec_dev_wm8580 = {
891 .probe = wm8580_probe,
892 .remove = wm8580_remove,
894 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8580);
896 static int wm8580_register(struct wm8580_priv *wm8580)
898 int ret, i;
899 struct snd_soc_codec *codec = &wm8580->codec;
901 if (wm8580_codec) {
902 dev_err(codec->dev, "Another WM8580 is registered\n");
903 ret = -EINVAL;
904 goto err;
907 mutex_init(&codec->mutex);
908 INIT_LIST_HEAD(&codec->dapm_widgets);
909 INIT_LIST_HEAD(&codec->dapm_paths);
911 codec->private_data = wm8580;
912 codec->name = "WM8580";
913 codec->owner = THIS_MODULE;
914 codec->read = wm8580_read_reg_cache;
915 codec->write = wm8580_write;
916 codec->bias_level = SND_SOC_BIAS_OFF;
917 codec->set_bias_level = wm8580_set_bias_level;
918 codec->dai = wm8580_dai;
919 codec->num_dai = ARRAY_SIZE(wm8580_dai);
920 codec->reg_cache_size = ARRAY_SIZE(wm8580->reg_cache);
921 codec->reg_cache = &wm8580->reg_cache;
923 memcpy(codec->reg_cache, wm8580_reg, sizeof(wm8580_reg));
925 /* Get the codec into a known state */
926 ret = wm8580_write(codec, WM8580_RESET, 0);
927 if (ret != 0) {
928 dev_err(codec->dev, "Failed to reset codec: %d\n", ret);
929 goto err;
932 for (i = 0; i < ARRAY_SIZE(wm8580_dai); i++)
933 wm8580_dai[i].dev = codec->dev;
935 wm8580_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
937 wm8580_codec = codec;
939 ret = snd_soc_register_codec(codec);
940 if (ret != 0) {
941 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
942 goto err;
945 ret = snd_soc_register_dais(wm8580_dai, ARRAY_SIZE(wm8580_dai));
946 if (ret != 0) {
947 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
948 goto err_codec;
951 return 0;
953 err_codec:
954 snd_soc_unregister_codec(codec);
955 err:
956 kfree(wm8580);
957 return ret;
960 static void wm8580_unregister(struct wm8580_priv *wm8580)
962 wm8580_set_bias_level(&wm8580->codec, SND_SOC_BIAS_OFF);
963 snd_soc_unregister_dais(wm8580_dai, ARRAY_SIZE(wm8580_dai));
964 snd_soc_unregister_codec(&wm8580->codec);
965 kfree(wm8580);
966 wm8580_codec = NULL;
969 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
970 static int wm8580_i2c_probe(struct i2c_client *i2c,
971 const struct i2c_device_id *id)
973 struct wm8580_priv *wm8580;
974 struct snd_soc_codec *codec;
976 wm8580 = kzalloc(sizeof(struct wm8580_priv), GFP_KERNEL);
977 if (wm8580 == NULL)
978 return -ENOMEM;
980 codec = &wm8580->codec;
981 codec->hw_write = (hw_write_t)i2c_master_send;
983 i2c_set_clientdata(i2c, wm8580);
984 codec->control_data = i2c;
986 codec->dev = &i2c->dev;
988 return wm8580_register(wm8580);
991 static int wm8580_i2c_remove(struct i2c_client *client)
993 struct wm8580_priv *wm8580 = i2c_get_clientdata(client);
994 wm8580_unregister(wm8580);
995 return 0;
998 static const struct i2c_device_id wm8580_i2c_id[] = {
999 { "wm8580", 0 },
1002 MODULE_DEVICE_TABLE(i2c, wm8580_i2c_id);
1004 static struct i2c_driver wm8580_i2c_driver = {
1005 .driver = {
1006 .name = "wm8580",
1007 .owner = THIS_MODULE,
1009 .probe = wm8580_i2c_probe,
1010 .remove = wm8580_i2c_remove,
1011 .id_table = wm8580_i2c_id,
1013 #endif
1015 static int __init wm8580_modinit(void)
1017 int ret;
1019 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1020 ret = i2c_add_driver(&wm8580_i2c_driver);
1021 if (ret != 0) {
1022 pr_err("Failed to register WM8580 I2C driver: %d\n", ret);
1024 #endif
1026 return 0;
1028 module_init(wm8580_modinit);
1030 static void __exit wm8580_exit(void)
1032 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1033 i2c_del_driver(&wm8580_i2c_driver);
1034 #endif
1036 module_exit(wm8580_exit);
1038 MODULE_DESCRIPTION("ASoC WM8580 driver");
1039 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1040 MODULE_LICENSE("GPL");