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 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
;
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
);
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);
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
);
392 /* The size in bits of the pll divide */
393 #define FIXED_PLL_SIZE (1 << 22)
395 /* PLL rate to output rate divisions */
398 unsigned int freqmode
;
399 unsigned int postscale
;
411 static int pll_factors(struct _pll_div
*pll_div
, unsigned int target
,
415 unsigned int K
, Ndiv
, Nmod
;
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
;
433 if (i
== ARRAY_SIZE(post_table
)) {
434 printk(KERN_ERR
"wm8580: Unable to scale output frequency "
439 Ndiv
= target
/ source
;
443 pll_div
->prescale
= 1;
444 Ndiv
= target
/ source
;
446 pll_div
->prescale
= 0;
448 if ((Ndiv
< 5) || (Ndiv
> 13)) {
450 "WM8580 N=%u outside supported range\n", Ndiv
);
455 Nmod
= target
% source
;
456 Kpart
= FIXED_PLL_SIZE
* (long long)Nmod
;
458 do_div(Kpart
, source
);
460 K
= Kpart
& 0xFFFFFFFF;
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
,
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
)
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
;
480 unsigned int pwr_mask
;
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
));
492 pwr_mask
= WM8580_PWRDN2_PLLAPD
;
497 pwr_mask
= WM8580_PWRDN2_PLLBPD
;
503 if (freq_in
&& freq_out
) {
504 ret
= pll_factors(&pll_div
, freq_out
, 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
)
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
);
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
);
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
;
554 switch (params_format(params
)) {
555 case SNDRV_PCM_FORMAT_S16_LE
:
557 case SNDRV_PCM_FORMAT_S20_3LE
:
558 paifb
|= WM8580_AIF_LENGTH_20
;
560 case SNDRV_PCM_FORMAT_S24_LE
:
561 paifb
|= WM8580_AIF_LENGTH_24
;
563 case SNDRV_PCM_FORMAT_S32_LE
:
564 paifb
|= WM8580_AIF_LENGTH_24
;
570 wm8580_write(codec
, WM8580_PAIF3
+ dai
->id
, paifb
);
574 static int wm8580_set_paif_dai_fmt(struct snd_soc_dai
*codec_dai
,
577 struct snd_soc_codec
*codec
= codec_dai
->codec
;
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
;
591 case SND_SOC_DAIFMT_CBM_CFM
:
592 aifa
|= WM8580_AIF_MS
;
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
;
603 case SND_SOC_DAIFMT_RIGHT_J
:
604 can_invert_lrclk
= 1;
605 aifb
|= WM8580_AIF_FMT_RIGHTJ
;
607 case SND_SOC_DAIFMT_LEFT_J
:
608 can_invert_lrclk
= 1;
609 aifb
|= WM8580_AIF_FMT_LEFTJ
;
611 case SND_SOC_DAIFMT_DSP_A
:
612 can_invert_lrclk
= 0;
613 aifb
|= WM8580_AIF_FMT_DSP
;
615 case SND_SOC_DAIFMT_DSP_B
:
616 can_invert_lrclk
= 0;
617 aifb
|= WM8580_AIF_FMT_DSP
;
618 aifb
|= WM8580_AIF_LRP
;
624 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
625 case SND_SOC_DAIFMT_NB_NF
:
628 case SND_SOC_DAIFMT_IB_IF
:
629 if (!can_invert_lrclk
)
631 aifb
|= WM8580_AIF_BCP
;
632 aifb
|= WM8580_AIF_LRP
;
635 case SND_SOC_DAIFMT_IB_NF
:
636 aifb
|= WM8580_AIF_BCP
;
639 case SND_SOC_DAIFMT_NB_IF
:
640 if (!can_invert_lrclk
)
642 aifb
|= WM8580_AIF_LRP
;
649 wm8580_write(codec
, WM8580_PAIF1
+ codec_dai
->id
, aifa
);
650 wm8580_write(codec
, WM8580_PAIF3
+ codec_dai
->id
, aifb
);
655 static int wm8580_set_dai_clkdiv(struct snd_soc_dai
*codec_dai
,
658 struct snd_soc_codec
*codec
= codec_dai
->codec
;
663 reg
= wm8580_read(codec
, WM8580_PLLB4
);
664 reg
&= ~WM8580_PLLB4_MCLKOUTSRC_MASK
;
667 case WM8580_CLKSRC_MCLK
:
671 case WM8580_CLKSRC_PLLA
:
672 reg
|= WM8580_PLLB4_MCLKOUTSRC_PLLA
;
674 case WM8580_CLKSRC_PLLB
:
675 reg
|= WM8580_PLLB4_MCLKOUTSRC_PLLB
;
678 case WM8580_CLKSRC_OSC
:
679 reg
|= WM8580_PLLB4_MCLKOUTSRC_OSC
;
685 wm8580_write(codec
, WM8580_PLLB4
, reg
);
688 case WM8580_DAC_CLKSEL
:
689 reg
= wm8580_read(codec
, WM8580_CLKSEL
);
690 reg
&= ~WM8580_CLKSEL_DAC_CLKSEL_MASK
;
693 case WM8580_CLKSRC_MCLK
:
696 case WM8580_CLKSRC_PLLA
:
697 reg
|= WM8580_CLKSEL_DAC_CLKSEL_PLLA
;
700 case WM8580_CLKSRC_PLLB
:
701 reg
|= WM8580_CLKSEL_DAC_CLKSEL_PLLB
;
707 wm8580_write(codec
, WM8580_CLKSEL
, reg
);
710 case WM8580_CLKOUTSRC
:
711 reg
= wm8580_read(codec
, WM8580_PLLB4
);
712 reg
&= ~WM8580_PLLB4_CLKOUTSRC_MASK
;
715 case WM8580_CLKSRC_NONE
:
718 case WM8580_CLKSRC_PLLA
:
719 reg
|= WM8580_PLLB4_CLKOUTSRC_PLLACLK
;
722 case WM8580_CLKSRC_PLLB
:
723 reg
|= WM8580_PLLB4_CLKOUTSRC_PLLBCLK
;
726 case WM8580_CLKSRC_OSC
:
727 reg
|= WM8580_PLLB4_CLKOUTSRC_OSCCLK
;
733 wm8580_write(codec
, WM8580_PLLB4
, reg
);
743 static int wm8580_digital_mute(struct snd_soc_dai
*codec_dai
, int mute
)
745 struct snd_soc_codec
*codec
= codec_dai
->codec
;
748 reg
= wm8580_read(codec
, WM8580_DAC_CONTROL5
);
751 reg
|= WM8580_DAC_CONTROL5_MUTEALL
;
753 reg
&= ~WM8580_DAC_CONTROL5_MUTEALL
;
755 wm8580_write(codec
, WM8580_DAC_CONTROL5
, reg
);
760 static int wm8580_set_bias_level(struct snd_soc_codec
*codec
,
761 enum snd_soc_bias_level level
)
765 case SND_SOC_BIAS_ON
:
766 case SND_SOC_BIAS_PREPARE
:
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
);
779 wm8580_write(codec
, WM8580_ADC_CONTROL1
, reg
);
783 case SND_SOC_BIAS_OFF
:
784 reg
= wm8580_read(codec
, WM8580_PWRDN1
);
785 wm8580_write(codec
, WM8580_PWRDN1
, reg
| WM8580_PWRDN1_PWDN
);
788 codec
->bias_level
= level
;
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",
815 .stream_name
= "Playback",
818 .rates
= SNDRV_PCM_RATE_8000_192000
,
819 .formats
= WM8580_FORMATS
,
821 .ops
= &wm8580_dai_ops_playback
,
824 .name
= "WM8580 PAIFTX",
827 .stream_name
= "Capture",
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
;
846 if (wm8580_codec
== NULL
) {
847 dev_err(&pdev
->dev
, "Codec device not registered\n");
851 socdev
->card
->codec
= wm8580_codec
;
852 codec
= wm8580_codec
;
855 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
857 dev_err(codec
->dev
, "failed to create pcms: %d\n", ret
);
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
);
866 dev_err(codec
->dev
, "failed to register card: %d\n", ret
);
873 snd_soc_free_pcms(socdev
);
874 snd_soc_dapm_free(socdev
);
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
);
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
)
899 struct snd_soc_codec
*codec
= &wm8580
->codec
;
902 dev_err(codec
->dev
, "Another WM8580 is registered\n");
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);
928 dev_err(codec
->dev
, "Failed to reset codec: %d\n", ret
);
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
);
941 dev_err(codec
->dev
, "Failed to register codec: %d\n", ret
);
945 ret
= snd_soc_register_dais(wm8580_dai
, ARRAY_SIZE(wm8580_dai
));
947 dev_err(codec
->dev
, "Failed to register DAI: %d\n", ret
);
954 snd_soc_unregister_codec(codec
);
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
);
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
);
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
);
998 static const struct i2c_device_id wm8580_i2c_id
[] = {
1002 MODULE_DEVICE_TABLE(i2c
, wm8580_i2c_id
);
1004 static struct i2c_driver wm8580_i2c_driver
= {
1007 .owner
= THIS_MODULE
,
1009 .probe
= wm8580_i2c_probe
,
1010 .remove
= wm8580_i2c_remove
,
1011 .id_table
= wm8580_i2c_id
,
1015 static int __init
wm8580_modinit(void)
1019 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1020 ret
= i2c_add_driver(&wm8580_i2c_driver
);
1022 pr_err("Failed to register WM8580 I2C driver: %d\n", ret
);
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
);
1036 module_exit(wm8580_exit
);
1038 MODULE_DESCRIPTION("ASoC WM8580 driver");
1039 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1040 MODULE_LICENSE("GPL");