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.
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>
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>
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*/
190 /* codec private data */
192 struct snd_soc_codec codec;
193 u16 reg_cache[WM8580_MAX_REGISTER + 1];
200 * read wm8580 register cache
202 static inline unsigned int wm8580_read_reg_cache(struct snd_soc_codec *codec,
205 u16 *cache = codec->reg_cache;
206 BUG_ON(reg >= ARRAY_SIZE(wm8580_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;
222 * write to the WM8580 register space
224 static int wm8580_write(struct snd_soc_codec *codec, unsigned int reg,
229 BUG_ON(reg >= ARRAY_SIZE(wm8580_reg));
231 /* Registers are 9 bits wide */
239 if (value == wm8580_read_reg_cache(codec, reg))
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)
257 static inline unsigned int wm8580_read(struct snd_soc_codec *codec,
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 snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
272 int reg = kcontrol->private_value & 0xff;
273 int reg2 = (kcontrol->private_value >> 24) & 0xff;
277 /* Clear the register cache so we write without VU set */
278 wm8580_write_reg_cache(codec, reg, 0);
279 wm8580_write_reg_cache(codec, reg2, 0);
281 ret = snd_soc_put_volsw_2r(kcontrol, ucontrol);
285 /* Now write again with the volume update bit set */
286 val = wm8580_read_reg_cache(codec, reg);
287 wm8580_write(codec, reg, val | 0x0100);
289 val = wm8580_read_reg_cache(codec, reg2);
290 wm8580_write(codec, reg2, val | 0x0100);
295 #define SOC_WM8580_OUT_DOUBLE_R_TLV(xname, reg_left, reg_right, shift, max, invert, tlv_array) \
296 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
297 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
298 SNDRV_CTL_ELEM_ACCESS_READWRITE, \
299 .tlv.p = (tlv_array), \
300 .info = snd_soc_info_volsw_2r, \
301 .get = snd_soc_get_volsw_2r, .put = wm8580_out_vu, \
302 .private_value = (reg_left) | ((shift) << 8) | \
303 ((max) << 12) | ((invert) << 20) | ((reg_right) << 24) }
305 static const struct snd_kcontrol_new wm8580_snd_controls[] = {
306 SOC_WM8580_OUT_DOUBLE_R_TLV("DAC1 Playback Volume",
307 WM8580_DIGITAL_ATTENUATION_DACL1,
308 WM8580_DIGITAL_ATTENUATION_DACR1,
309 0, 0xff, 0, dac_tlv),
310 SOC_WM8580_OUT_DOUBLE_R_TLV("DAC2 Playback Volume",
311 WM8580_DIGITAL_ATTENUATION_DACL2,
312 WM8580_DIGITAL_ATTENUATION_DACR2,
313 0, 0xff, 0, dac_tlv),
314 SOC_WM8580_OUT_DOUBLE_R_TLV("DAC3 Playback Volume",
315 WM8580_DIGITAL_ATTENUATION_DACL3,
316 WM8580_DIGITAL_ATTENUATION_DACR3,
317 0, 0xff, 0, dac_tlv),
319 SOC_SINGLE("DAC1 Deemphasis Switch", WM8580_DAC_CONTROL3, 0, 1, 0),
320 SOC_SINGLE("DAC2 Deemphasis Switch", WM8580_DAC_CONTROL3, 1, 1, 0),
321 SOC_SINGLE("DAC3 Deemphasis Switch", WM8580_DAC_CONTROL3, 2, 1, 0),
323 SOC_DOUBLE("DAC1 Invert Switch", WM8580_DAC_CONTROL4, 0, 1, 1, 0),
324 SOC_DOUBLE("DAC2 Invert Switch", WM8580_DAC_CONTROL4, 2, 3, 1, 0),
325 SOC_DOUBLE("DAC3 Invert Switch", WM8580_DAC_CONTROL4, 4, 5, 1, 0),
327 SOC_SINGLE("DAC ZC Switch", WM8580_DAC_CONTROL5, 5, 1, 0),
328 SOC_SINGLE("DAC1 Switch", WM8580_DAC_CONTROL5, 0, 1, 0),
329 SOC_SINGLE("DAC2 Switch", WM8580_DAC_CONTROL5, 1, 1, 0),
330 SOC_SINGLE("DAC3 Switch", WM8580_DAC_CONTROL5, 2, 1, 0),
332 SOC_DOUBLE("ADC Mute Switch", WM8580_ADC_CONTROL1, 0, 1, 1, 0),
333 SOC_SINGLE("ADC High-Pass Filter Switch", WM8580_ADC_CONTROL1, 4, 1, 0),
336 static const struct snd_soc_dapm_widget wm8580_dapm_widgets[] = {
337 SND_SOC_DAPM_DAC("DAC1", "Playback", WM8580_PWRDN1, 2, 1),
338 SND_SOC_DAPM_DAC("DAC2", "Playback", WM8580_PWRDN1, 3, 1),
339 SND_SOC_DAPM_DAC("DAC3", "Playback", WM8580_PWRDN1, 4, 1),
341 SND_SOC_DAPM_OUTPUT("VOUT1L"),
342 SND_SOC_DAPM_OUTPUT("VOUT1R"),
343 SND_SOC_DAPM_OUTPUT("VOUT2L"),
344 SND_SOC_DAPM_OUTPUT("VOUT2R"),
345 SND_SOC_DAPM_OUTPUT("VOUT3L"),
346 SND_SOC_DAPM_OUTPUT("VOUT3R"),
348 SND_SOC_DAPM_ADC("ADC", "Capture", WM8580_PWRDN1, 1, 1),
350 SND_SOC_DAPM_INPUT("AINL"),
351 SND_SOC_DAPM_INPUT("AINR"),
354 static const struct snd_soc_dapm_route audio_map[] = {
355 { "VOUT1L", NULL, "DAC1" },
356 { "VOUT1R", NULL, "DAC1" },
358 { "VOUT2L", NULL, "DAC2" },
359 { "VOUT2R", NULL, "DAC2" },
361 { "VOUT3L", NULL, "DAC3" },
362 { "VOUT3R", NULL, "DAC3" },
364 { "ADC", NULL, "AINL" },
365 { "ADC", NULL, "AINR" },
368 static int wm8580_add_widgets(struct snd_soc_codec *codec)
370 snd_soc_dapm_new_controls(codec, wm8580_dapm_widgets,
371 ARRAY_SIZE(wm8580_dapm_widgets));
373 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
375 snd_soc_dapm_new_widgets(codec);
388 /* The size in bits of the pll divide */
389 #define FIXED_PLL_SIZE (1 << 22)
391 /* PLL rate to output rate divisions */
394 unsigned int freqmode;
395 unsigned int postscale;
407 static int pll_factors(struct _pll_div *pll_div, unsigned int target,
411 unsigned int K, Ndiv, Nmod;
414 pr_debug("wm8580: PLL %dHz->%dHz\n", source, target);
416 /* Scale the output frequency up; the PLL should run in the
417 * region of 90-100MHz.
419 for (i = 0; i < ARRAY_SIZE(post_table); i++) {
420 if (target * post_table[i].div >= 90000000 &&
421 target * post_table[i].div <= 100000000) {
422 pll_div->freqmode = post_table[i].freqmode;
423 pll_div->postscale = post_table[i].postscale;
424 target *= post_table[i].div;
429 if (i == ARRAY_SIZE(post_table)) {
430 printk(KERN_ERR "wm8580: Unable to scale output frequency "
435 Ndiv = target / source;
439 pll_div->prescale = 1;
440 Ndiv = target / source;
442 pll_div->prescale = 0;
444 if ((Ndiv < 5) || (Ndiv > 13)) {
446 "WM8580 N=%d outside supported range\n", Ndiv);
451 Nmod = target % source;
452 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
454 do_div(Kpart, source);
456 K = Kpart & 0xFFFFFFFF;
460 pr_debug("PLL %x.%x prescale %d freqmode %d postscale %d\n",
461 pll_div->n, pll_div->k, pll_div->prescale, pll_div->freqmode,
467 static int wm8580_set_dai_pll(struct snd_soc_dai *codec_dai,
468 int pll_id, unsigned int freq_in, unsigned int freq_out)
471 struct snd_soc_codec *codec = codec_dai->codec;
472 struct wm8580_priv *wm8580 = codec->private_data;
473 struct pll_state *state;
474 struct _pll_div pll_div;
476 unsigned int pwr_mask;
479 /* GCC isn't able to work out the ifs below for initialising/using
480 * pll_div so suppress warnings.
482 memset(&pll_div, 0, sizeof(pll_div));
488 pwr_mask = WM8580_PWRDN2_PLLAPD;
493 pwr_mask = WM8580_PWRDN2_PLLBPD;
499 if (freq_in && freq_out) {
500 ret = pll_factors(&pll_div, freq_out, freq_in);
506 state->out = freq_out;
508 /* Always disable the PLL - it is not safe to leave it running
509 * while reprogramming it.
511 reg = wm8580_read(codec, WM8580_PWRDN2);
512 wm8580_write(codec, WM8580_PWRDN2, reg | pwr_mask);
514 if (!freq_in || !freq_out)
517 wm8580_write(codec, WM8580_PLLA1 + offset, pll_div.k & 0x1ff);
518 wm8580_write(codec, WM8580_PLLA2 + offset, (pll_div.k >> 9) & 0xff);
519 wm8580_write(codec, WM8580_PLLA3 + offset,
520 (pll_div.k >> 18 & 0xf) | (pll_div.n << 4));
522 reg = wm8580_read(codec, WM8580_PLLA4 + offset);
524 reg |= pll_div.prescale | pll_div.postscale << 1 |
525 pll_div.freqmode << 4;
527 wm8580_write(codec, WM8580_PLLA4 + offset, reg);
529 /* All done, turn it on */
530 reg = wm8580_read(codec, WM8580_PWRDN2);
531 wm8580_write(codec, WM8580_PWRDN2, reg & ~pwr_mask);
537 * Set PCM DAI bit size and sample rate.
539 static int wm8580_paif_hw_params(struct snd_pcm_substream *substream,
540 struct snd_pcm_hw_params *params,
541 struct snd_soc_dai *dai)
543 struct snd_soc_pcm_runtime *rtd = substream->private_data;
544 struct snd_soc_device *socdev = rtd->socdev;
545 struct snd_soc_codec *codec = socdev->card->codec;
546 u16 paifb = wm8580_read(codec, WM8580_PAIF3 + dai->id);
548 paifb &= ~WM8580_AIF_LENGTH_MASK;
550 switch (params_format(params)) {
551 case SNDRV_PCM_FORMAT_S16_LE:
553 case SNDRV_PCM_FORMAT_S20_3LE:
554 paifb |= WM8580_AIF_LENGTH_20;
556 case SNDRV_PCM_FORMAT_S24_LE:
557 paifb |= WM8580_AIF_LENGTH_24;
559 case SNDRV_PCM_FORMAT_S32_LE:
560 paifb |= WM8580_AIF_LENGTH_24;
566 wm8580_write(codec, WM8580_PAIF3 + dai->id, paifb);
570 static int wm8580_set_paif_dai_fmt(struct snd_soc_dai *codec_dai,
573 struct snd_soc_codec *codec = codec_dai->codec;
576 int can_invert_lrclk;
578 aifa = wm8580_read(codec, WM8580_PAIF1 + codec_dai->id);
579 aifb = wm8580_read(codec, WM8580_PAIF3 + codec_dai->id);
581 aifb &= ~(WM8580_AIF_FMT_MASK | WM8580_AIF_LRP | WM8580_AIF_BCP);
583 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
584 case SND_SOC_DAIFMT_CBS_CFS:
585 aifa &= ~WM8580_AIF_MS;
587 case SND_SOC_DAIFMT_CBM_CFM:
588 aifa |= WM8580_AIF_MS;
594 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
595 case SND_SOC_DAIFMT_I2S:
596 can_invert_lrclk = 1;
597 aifb |= WM8580_AIF_FMT_I2S;
599 case SND_SOC_DAIFMT_RIGHT_J:
600 can_invert_lrclk = 1;
601 aifb |= WM8580_AIF_FMT_RIGHTJ;
603 case SND_SOC_DAIFMT_LEFT_J:
604 can_invert_lrclk = 1;
605 aifb |= WM8580_AIF_FMT_LEFTJ;
607 case SND_SOC_DAIFMT_DSP_A:
608 can_invert_lrclk = 0;
609 aifb |= WM8580_AIF_FMT_DSP;
611 case SND_SOC_DAIFMT_DSP_B:
612 can_invert_lrclk = 0;
613 aifb |= WM8580_AIF_FMT_DSP;
614 aifb |= WM8580_AIF_LRP;
620 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
621 case SND_SOC_DAIFMT_NB_NF:
624 case SND_SOC_DAIFMT_IB_IF:
625 if (!can_invert_lrclk)
627 aifb |= WM8580_AIF_BCP;
628 aifb |= WM8580_AIF_LRP;
631 case SND_SOC_DAIFMT_IB_NF:
632 aifb |= WM8580_AIF_BCP;
635 case SND_SOC_DAIFMT_NB_IF:
636 if (!can_invert_lrclk)
638 aifb |= WM8580_AIF_LRP;
645 wm8580_write(codec, WM8580_PAIF1 + codec_dai->id, aifa);
646 wm8580_write(codec, WM8580_PAIF3 + codec_dai->id, aifb);
651 static int wm8580_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
654 struct snd_soc_codec *codec = codec_dai->codec;
659 reg = wm8580_read(codec, WM8580_PLLB4);
660 reg &= ~WM8580_PLLB4_MCLKOUTSRC_MASK;
663 case WM8580_CLKSRC_MCLK:
667 case WM8580_CLKSRC_PLLA:
668 reg |= WM8580_PLLB4_MCLKOUTSRC_PLLA;
670 case WM8580_CLKSRC_PLLB:
671 reg |= WM8580_PLLB4_MCLKOUTSRC_PLLB;
674 case WM8580_CLKSRC_OSC:
675 reg |= WM8580_PLLB4_MCLKOUTSRC_OSC;
681 wm8580_write(codec, WM8580_PLLB4, reg);
684 case WM8580_DAC_CLKSEL:
685 reg = wm8580_read(codec, WM8580_CLKSEL);
686 reg &= ~WM8580_CLKSEL_DAC_CLKSEL_MASK;
689 case WM8580_CLKSRC_MCLK:
692 case WM8580_CLKSRC_PLLA:
693 reg |= WM8580_CLKSEL_DAC_CLKSEL_PLLA;
696 case WM8580_CLKSRC_PLLB:
697 reg |= WM8580_CLKSEL_DAC_CLKSEL_PLLB;
703 wm8580_write(codec, WM8580_CLKSEL, reg);
706 case WM8580_CLKOUTSRC:
707 reg = wm8580_read(codec, WM8580_PLLB4);
708 reg &= ~WM8580_PLLB4_CLKOUTSRC_MASK;
711 case WM8580_CLKSRC_NONE:
714 case WM8580_CLKSRC_PLLA:
715 reg |= WM8580_PLLB4_CLKOUTSRC_PLLACLK;
718 case WM8580_CLKSRC_PLLB:
719 reg |= WM8580_PLLB4_CLKOUTSRC_PLLBCLK;
722 case WM8580_CLKSRC_OSC:
723 reg |= WM8580_PLLB4_CLKOUTSRC_OSCCLK;
729 wm8580_write(codec, WM8580_PLLB4, reg);
739 static int wm8580_digital_mute(struct snd_soc_dai *codec_dai, int mute)
741 struct snd_soc_codec *codec = codec_dai->codec;
744 reg = wm8580_read(codec, WM8580_DAC_CONTROL5);
747 reg |= WM8580_DAC_CONTROL5_MUTEALL;
749 reg &= ~WM8580_DAC_CONTROL5_MUTEALL;
751 wm8580_write(codec, WM8580_DAC_CONTROL5, reg);
756 static int wm8580_set_bias_level(struct snd_soc_codec *codec,
757 enum snd_soc_bias_level level)
761 case SND_SOC_BIAS_ON:
762 case SND_SOC_BIAS_PREPARE:
765 case SND_SOC_BIAS_STANDBY:
766 if (codec->bias_level == SND_SOC_BIAS_OFF) {
767 /* Power up and get individual control of the DACs */
768 reg = wm8580_read(codec, WM8580_PWRDN1);
769 reg &= ~(WM8580_PWRDN1_PWDN | WM8580_PWRDN1_ALLDACPD);
770 wm8580_write(codec, WM8580_PWRDN1, reg);
772 /* Make VMID high impedence */
773 reg = wm8580_read(codec, WM8580_ADC_CONTROL1);
775 wm8580_write(codec, WM8580_ADC_CONTROL1, reg);
779 case SND_SOC_BIAS_OFF:
780 reg = wm8580_read(codec, WM8580_PWRDN1);
781 wm8580_write(codec, WM8580_PWRDN1, reg | WM8580_PWRDN1_PWDN);
784 codec->bias_level = level;
788 #define WM8580_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
789 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
791 static struct snd_soc_dai_ops wm8580_dai_ops_playback = {
792 .hw_params = wm8580_paif_hw_params,
793 .set_fmt = wm8580_set_paif_dai_fmt,
794 .set_clkdiv = wm8580_set_dai_clkdiv,
795 .set_pll = wm8580_set_dai_pll,
796 .digital_mute = wm8580_digital_mute,
799 static struct snd_soc_dai_ops wm8580_dai_ops_capture = {
800 .hw_params = wm8580_paif_hw_params,
801 .set_fmt = wm8580_set_paif_dai_fmt,
802 .set_clkdiv = wm8580_set_dai_clkdiv,
803 .set_pll = wm8580_set_dai_pll,
806 struct snd_soc_dai wm8580_dai[] = {
808 .name = "WM8580 PAIFRX",
811 .stream_name = "Playback",
814 .rates = SNDRV_PCM_RATE_8000_192000,
815 .formats = WM8580_FORMATS,
817 .ops = &wm8580_dai_ops_playback,
820 .name = "WM8580 PAIFTX",
823 .stream_name = "Capture",
826 .rates = SNDRV_PCM_RATE_8000_192000,
827 .formats = WM8580_FORMATS,
829 .ops = &wm8580_dai_ops_capture,
832 EXPORT_SYMBOL_GPL(wm8580_dai);
834 static struct snd_soc_codec *wm8580_codec;
836 static int wm8580_probe(struct platform_device *pdev)
838 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
839 struct snd_soc_codec *codec;
842 if (wm8580_codec == NULL) {
843 dev_err(&pdev->dev, "Codec device not registered\n");
847 socdev->card->codec = wm8580_codec;
848 codec = wm8580_codec;
851 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
853 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
857 snd_soc_add_controls(codec, wm8580_snd_controls,
858 ARRAY_SIZE(wm8580_snd_controls));
859 wm8580_add_widgets(codec);
860 ret = snd_soc_init_card(socdev);
862 dev_err(codec->dev, "failed to register card: %d\n", ret);
869 snd_soc_free_pcms(socdev);
870 snd_soc_dapm_free(socdev);
875 /* power down chip */
876 static int wm8580_remove(struct platform_device *pdev)
878 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
880 snd_soc_free_pcms(socdev);
881 snd_soc_dapm_free(socdev);
886 struct snd_soc_codec_device soc_codec_dev_wm8580 = {
887 .probe = wm8580_probe,
888 .remove = wm8580_remove,
890 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8580);
892 static int wm8580_register(struct wm8580_priv *wm8580)
895 struct snd_soc_codec *codec = &wm8580->codec;
898 dev_err(codec->dev, "Another WM8580 is registered\n");
903 mutex_init(&codec->mutex);
904 INIT_LIST_HEAD(&codec->dapm_widgets);
905 INIT_LIST_HEAD(&codec->dapm_paths);
907 codec->private_data = wm8580;
908 codec->name = "WM8580";
909 codec->owner = THIS_MODULE;
910 codec->read = wm8580_read_reg_cache;
911 codec->write = wm8580_write;
912 codec->bias_level = SND_SOC_BIAS_OFF;
913 codec->set_bias_level = wm8580_set_bias_level;
914 codec->dai = wm8580_dai;
915 codec->num_dai = ARRAY_SIZE(wm8580_dai);
916 codec->reg_cache_size = ARRAY_SIZE(wm8580->reg_cache);
917 codec->reg_cache = &wm8580->reg_cache;
919 memcpy(codec->reg_cache, wm8580_reg, sizeof(wm8580_reg));
921 /* Get the codec into a known state */
922 ret = wm8580_write(codec, WM8580_RESET, 0);
924 dev_err(codec->dev, "Failed to reset codec: %d\n", ret);
928 for (i = 0; i < ARRAY_SIZE(wm8580_dai); i++)
929 wm8580_dai[i].dev = codec->dev;
931 wm8580_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
933 wm8580_codec = codec;
935 ret = snd_soc_register_codec(codec);
937 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
941 ret = snd_soc_register_dais(wm8580_dai, ARRAY_SIZE(wm8580_dai));
943 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
950 snd_soc_unregister_codec(codec);
956 static void wm8580_unregister(struct wm8580_priv *wm8580)
958 wm8580_set_bias_level(&wm8580->codec, SND_SOC_BIAS_OFF);
959 snd_soc_unregister_dais(wm8580_dai, ARRAY_SIZE(wm8580_dai));
960 snd_soc_unregister_codec(&wm8580->codec);
965 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
966 static int wm8580_i2c_probe(struct i2c_client *i2c,
967 const struct i2c_device_id *id)
969 struct wm8580_priv *wm8580;
970 struct snd_soc_codec *codec;
972 wm8580 = kzalloc(sizeof(struct wm8580_priv), GFP_KERNEL);
976 codec = &wm8580->codec;
977 codec->hw_write = (hw_write_t)i2c_master_send;
979 i2c_set_clientdata(i2c, wm8580);
980 codec->control_data = i2c;
982 codec->dev = &i2c->dev;
984 return wm8580_register(wm8580);
987 static int wm8580_i2c_remove(struct i2c_client *client)
989 struct wm8580_priv *wm8580 = i2c_get_clientdata(client);
990 wm8580_unregister(wm8580);
994 static const struct i2c_device_id wm8580_i2c_id[] = {
998 MODULE_DEVICE_TABLE(i2c, wm8580_i2c_id);
1000 static struct i2c_driver wm8580_i2c_driver = {
1003 .owner = THIS_MODULE,
1005 .probe = wm8580_i2c_probe,
1006 .remove = wm8580_i2c_remove,
1007 .id_table = wm8580_i2c_id,
1011 static int __init wm8580_modinit(void)
1015 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1016 ret = i2c_add_driver(&wm8580_i2c_driver);
1018 pr_err("Failed to register WM8580 I2C driver: %d\n", ret);
1024 module_init(wm8580_modinit);
1026 static void __exit wm8580_exit(void)
1028 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1029 i2c_del_driver(&wm8580_i2c_driver);
1032 module_exit(wm8580_exit);
1034 MODULE_DESCRIPTION("ASoC WM8580 driver");
1035 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1036 MODULE_LICENSE("GPL");