1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * wm8580.c -- WM8580 and WM8581 ALSA Soc Audio driver
5 * Copyright 2008-12 Wolfson Microelectronics PLC.
8 * The WM8580 is a multichannel codec with S/PDIF support, featuring six
9 * DAC channels and two ADC channels.
11 * The WM8581 is a multichannel codec with S/PDIF support, featuring eight
12 * DAC channels and two ADC channels.
14 * Currently only the primary audio interface is supported - S/PDIF and
15 * the secondary audio interfaces are not.
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
24 #include <linux/i2c.h>
25 #include <linux/regmap.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/slab.h>
28 #include <linux/of_device.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/soc.h>
34 #include <sound/tlv.h>
35 #include <sound/initval.h>
36 #include <asm/div64.h>
40 /* WM8580 register space */
41 #define WM8580_PLLA1 0x00
42 #define WM8580_PLLA2 0x01
43 #define WM8580_PLLA3 0x02
44 #define WM8580_PLLA4 0x03
45 #define WM8580_PLLB1 0x04
46 #define WM8580_PLLB2 0x05
47 #define WM8580_PLLB3 0x06
48 #define WM8580_PLLB4 0x07
49 #define WM8580_CLKSEL 0x08
50 #define WM8580_PAIF1 0x09
51 #define WM8580_PAIF2 0x0A
52 #define WM8580_SAIF1 0x0B
53 #define WM8580_PAIF3 0x0C
54 #define WM8580_PAIF4 0x0D
55 #define WM8580_SAIF2 0x0E
56 #define WM8580_DAC_CONTROL1 0x0F
57 #define WM8580_DAC_CONTROL2 0x10
58 #define WM8580_DAC_CONTROL3 0x11
59 #define WM8580_DAC_CONTROL4 0x12
60 #define WM8580_DAC_CONTROL5 0x13
61 #define WM8580_DIGITAL_ATTENUATION_DACL1 0x14
62 #define WM8580_DIGITAL_ATTENUATION_DACR1 0x15
63 #define WM8580_DIGITAL_ATTENUATION_DACL2 0x16
64 #define WM8580_DIGITAL_ATTENUATION_DACR2 0x17
65 #define WM8580_DIGITAL_ATTENUATION_DACL3 0x18
66 #define WM8580_DIGITAL_ATTENUATION_DACR3 0x19
67 #define WM8581_DIGITAL_ATTENUATION_DACL4 0x1A
68 #define WM8581_DIGITAL_ATTENUATION_DACR4 0x1B
69 #define WM8580_MASTER_DIGITAL_ATTENUATION 0x1C
70 #define WM8580_ADC_CONTROL1 0x1D
71 #define WM8580_SPDTXCHAN0 0x1E
72 #define WM8580_SPDTXCHAN1 0x1F
73 #define WM8580_SPDTXCHAN2 0x20
74 #define WM8580_SPDTXCHAN3 0x21
75 #define WM8580_SPDTXCHAN4 0x22
76 #define WM8580_SPDTXCHAN5 0x23
77 #define WM8580_SPDMODE 0x24
78 #define WM8580_INTMASK 0x25
79 #define WM8580_GPO1 0x26
80 #define WM8580_GPO2 0x27
81 #define WM8580_GPO3 0x28
82 #define WM8580_GPO4 0x29
83 #define WM8580_GPO5 0x2A
84 #define WM8580_INTSTAT 0x2B
85 #define WM8580_SPDRXCHAN1 0x2C
86 #define WM8580_SPDRXCHAN2 0x2D
87 #define WM8580_SPDRXCHAN3 0x2E
88 #define WM8580_SPDRXCHAN4 0x2F
89 #define WM8580_SPDRXCHAN5 0x30
90 #define WM8580_SPDSTAT 0x31
91 #define WM8580_PWRDN1 0x32
92 #define WM8580_PWRDN2 0x33
93 #define WM8580_READBACK 0x34
94 #define WM8580_RESET 0x35
96 #define WM8580_MAX_REGISTER 0x35
98 #define WM8580_DACOSR 0x40
100 /* PLLB4 (register 7h) */
101 #define WM8580_PLLB4_MCLKOUTSRC_MASK 0x60
102 #define WM8580_PLLB4_MCLKOUTSRC_PLLA 0x20
103 #define WM8580_PLLB4_MCLKOUTSRC_PLLB 0x40
104 #define WM8580_PLLB4_MCLKOUTSRC_OSC 0x60
106 #define WM8580_PLLB4_CLKOUTSRC_MASK 0x180
107 #define WM8580_PLLB4_CLKOUTSRC_PLLACLK 0x080
108 #define WM8580_PLLB4_CLKOUTSRC_PLLBCLK 0x100
109 #define WM8580_PLLB4_CLKOUTSRC_OSCCLK 0x180
111 /* CLKSEL (register 8h) */
112 #define WM8580_CLKSEL_DAC_CLKSEL_MASK 0x03
113 #define WM8580_CLKSEL_DAC_CLKSEL_PLLA 0x01
114 #define WM8580_CLKSEL_DAC_CLKSEL_PLLB 0x02
116 /* AIF control 1 (registers 9h-bh) */
117 #define WM8580_AIF_RATE_MASK 0x7
118 #define WM8580_AIF_BCLKSEL_MASK 0x18
120 #define WM8580_AIF_MS 0x20
122 #define WM8580_AIF_CLKSRC_MASK 0xc0
123 #define WM8580_AIF_CLKSRC_PLLA 0x40
124 #define WM8580_AIF_CLKSRC_PLLB 0x40
125 #define WM8580_AIF_CLKSRC_MCLK 0xc0
127 /* AIF control 2 (registers ch-eh) */
128 #define WM8580_AIF_FMT_MASK 0x03
129 #define WM8580_AIF_FMT_RIGHTJ 0x00
130 #define WM8580_AIF_FMT_LEFTJ 0x01
131 #define WM8580_AIF_FMT_I2S 0x02
132 #define WM8580_AIF_FMT_DSP 0x03
134 #define WM8580_AIF_LENGTH_MASK 0x0c
135 #define WM8580_AIF_LENGTH_16 0x00
136 #define WM8580_AIF_LENGTH_20 0x04
137 #define WM8580_AIF_LENGTH_24 0x08
138 #define WM8580_AIF_LENGTH_32 0x0c
140 #define WM8580_AIF_LRP 0x10
141 #define WM8580_AIF_BCP 0x20
143 /* Powerdown Register 1 (register 32h) */
144 #define WM8580_PWRDN1_PWDN 0x001
145 #define WM8580_PWRDN1_ALLDACPD 0x040
147 /* Powerdown Register 2 (register 33h) */
148 #define WM8580_PWRDN2_OSSCPD 0x001
149 #define WM8580_PWRDN2_PLLAPD 0x002
150 #define WM8580_PWRDN2_PLLBPD 0x004
151 #define WM8580_PWRDN2_SPDIFPD 0x008
152 #define WM8580_PWRDN2_SPDIFTXD 0x010
153 #define WM8580_PWRDN2_SPDIFRXD 0x020
155 #define WM8580_DAC_CONTROL5_MUTEALL 0x10
158 * wm8580 register cache
159 * We can't read the WM8580 register space when we
160 * are using 2 wire for device control, so we cache them instead.
162 static const struct reg_default wm8580_reg_defaults
[] = {
218 static bool wm8580_volatile(struct device
*dev
, unsigned int reg
)
233 #define WM8580_NUM_SUPPLIES 3
234 static const char *wm8580_supply_names
[WM8580_NUM_SUPPLIES
] = {
240 struct wm8580_driver_data
{
244 /* codec private data */
246 struct regmap
*regmap
;
247 struct regulator_bulk_data supplies
[WM8580_NUM_SUPPLIES
];
250 const struct wm8580_driver_data
*drvdata
;
254 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12750, 50, 1);
256 static int wm8580_out_vu(struct snd_kcontrol
*kcontrol
,
257 struct snd_ctl_elem_value
*ucontrol
)
259 struct soc_mixer_control
*mc
=
260 (struct soc_mixer_control
*)kcontrol
->private_value
;
261 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
262 struct wm8580_priv
*wm8580
= snd_soc_component_get_drvdata(component
);
263 unsigned int reg
= mc
->reg
;
264 unsigned int reg2
= mc
->rreg
;
267 /* Clear the register cache VU so we write without VU set */
268 regcache_cache_only(wm8580
->regmap
, true);
269 regmap_update_bits(wm8580
->regmap
, reg
, 0x100, 0x000);
270 regmap_update_bits(wm8580
->regmap
, reg2
, 0x100, 0x000);
271 regcache_cache_only(wm8580
->regmap
, false);
273 ret
= snd_soc_put_volsw(kcontrol
, ucontrol
);
277 /* Now write again with the volume update bit set */
278 snd_soc_component_update_bits(component
, reg
, 0x100, 0x100);
279 snd_soc_component_update_bits(component
, reg2
, 0x100, 0x100);
284 static const struct snd_kcontrol_new wm8580_snd_controls
[] = {
285 SOC_DOUBLE_R_EXT_TLV("DAC1 Playback Volume",
286 WM8580_DIGITAL_ATTENUATION_DACL1
,
287 WM8580_DIGITAL_ATTENUATION_DACR1
,
288 0, 0xff, 0, snd_soc_get_volsw
, wm8580_out_vu
, dac_tlv
),
289 SOC_DOUBLE_R_EXT_TLV("DAC2 Playback Volume",
290 WM8580_DIGITAL_ATTENUATION_DACL2
,
291 WM8580_DIGITAL_ATTENUATION_DACR2
,
292 0, 0xff, 0, snd_soc_get_volsw
, wm8580_out_vu
, dac_tlv
),
293 SOC_DOUBLE_R_EXT_TLV("DAC3 Playback Volume",
294 WM8580_DIGITAL_ATTENUATION_DACL3
,
295 WM8580_DIGITAL_ATTENUATION_DACR3
,
296 0, 0xff, 0, snd_soc_get_volsw
, wm8580_out_vu
, dac_tlv
),
298 SOC_SINGLE("DAC1 Deemphasis Switch", WM8580_DAC_CONTROL3
, 0, 1, 0),
299 SOC_SINGLE("DAC2 Deemphasis Switch", WM8580_DAC_CONTROL3
, 1, 1, 0),
300 SOC_SINGLE("DAC3 Deemphasis Switch", WM8580_DAC_CONTROL3
, 2, 1, 0),
302 SOC_DOUBLE("DAC1 Invert Switch", WM8580_DAC_CONTROL4
, 0, 1, 1, 0),
303 SOC_DOUBLE("DAC2 Invert Switch", WM8580_DAC_CONTROL4
, 2, 3, 1, 0),
304 SOC_DOUBLE("DAC3 Invert Switch", WM8580_DAC_CONTROL4
, 4, 5, 1, 0),
306 SOC_SINGLE("DAC ZC Switch", WM8580_DAC_CONTROL5
, 5, 1, 0),
307 SOC_SINGLE("DAC1 Switch", WM8580_DAC_CONTROL5
, 0, 1, 1),
308 SOC_SINGLE("DAC2 Switch", WM8580_DAC_CONTROL5
, 1, 1, 1),
309 SOC_SINGLE("DAC3 Switch", WM8580_DAC_CONTROL5
, 2, 1, 1),
311 SOC_DOUBLE("Capture Switch", WM8580_ADC_CONTROL1
, 0, 1, 1, 1),
312 SOC_SINGLE("Capture High-Pass Filter Switch", WM8580_ADC_CONTROL1
, 4, 1, 0),
315 static const struct snd_kcontrol_new wm8581_snd_controls
[] = {
316 SOC_DOUBLE_R_EXT_TLV("DAC4 Playback Volume",
317 WM8581_DIGITAL_ATTENUATION_DACL4
,
318 WM8581_DIGITAL_ATTENUATION_DACR4
,
319 0, 0xff, 0, snd_soc_get_volsw
, wm8580_out_vu
, dac_tlv
),
321 SOC_SINGLE("DAC4 Deemphasis Switch", WM8580_DAC_CONTROL3
, 3, 1, 0),
323 SOC_DOUBLE("DAC4 Invert Switch", WM8580_DAC_CONTROL4
, 8, 7, 1, 0),
325 SOC_SINGLE("DAC4 Switch", WM8580_DAC_CONTROL5
, 3, 1, 1),
328 static const struct snd_soc_dapm_widget wm8580_dapm_widgets
[] = {
329 SND_SOC_DAPM_DAC("DAC1", "Playback", WM8580_PWRDN1
, 2, 1),
330 SND_SOC_DAPM_DAC("DAC2", "Playback", WM8580_PWRDN1
, 3, 1),
331 SND_SOC_DAPM_DAC("DAC3", "Playback", WM8580_PWRDN1
, 4, 1),
333 SND_SOC_DAPM_OUTPUT("VOUT1L"),
334 SND_SOC_DAPM_OUTPUT("VOUT1R"),
335 SND_SOC_DAPM_OUTPUT("VOUT2L"),
336 SND_SOC_DAPM_OUTPUT("VOUT2R"),
337 SND_SOC_DAPM_OUTPUT("VOUT3L"),
338 SND_SOC_DAPM_OUTPUT("VOUT3R"),
340 SND_SOC_DAPM_ADC("ADC", "Capture", WM8580_PWRDN1
, 1, 1),
342 SND_SOC_DAPM_INPUT("AINL"),
343 SND_SOC_DAPM_INPUT("AINR"),
346 static const struct snd_soc_dapm_widget wm8581_dapm_widgets
[] = {
347 SND_SOC_DAPM_DAC("DAC4", "Playback", WM8580_PWRDN1
, 5, 1),
349 SND_SOC_DAPM_OUTPUT("VOUT4L"),
350 SND_SOC_DAPM_OUTPUT("VOUT4R"),
353 static const struct snd_soc_dapm_route wm8580_dapm_routes
[] = {
354 { "VOUT1L", NULL
, "DAC1" },
355 { "VOUT1R", NULL
, "DAC1" },
357 { "VOUT2L", NULL
, "DAC2" },
358 { "VOUT2R", NULL
, "DAC2" },
360 { "VOUT3L", NULL
, "DAC3" },
361 { "VOUT3R", NULL
, "DAC3" },
363 { "ADC", NULL
, "AINL" },
364 { "ADC", NULL
, "AINR" },
367 static const struct snd_soc_dapm_route wm8581_dapm_routes
[] = {
368 { "VOUT4L", NULL
, "DAC4" },
369 { "VOUT4R", NULL
, "DAC4" },
381 /* The size in bits of the pll divide */
382 #define FIXED_PLL_SIZE (1 << 22)
384 /* PLL rate to output rate divisions */
387 unsigned int freqmode
;
388 unsigned int postscale
;
400 static int pll_factors(struct _pll_div
*pll_div
, unsigned int target
,
404 unsigned int K
, Ndiv
, Nmod
;
407 pr_debug("wm8580: PLL %uHz->%uHz\n", source
, target
);
409 /* Scale the output frequency up; the PLL should run in the
410 * region of 90-100MHz.
412 for (i
= 0; i
< ARRAY_SIZE(post_table
); i
++) {
413 if (target
* post_table
[i
].div
>= 90000000 &&
414 target
* post_table
[i
].div
<= 100000000) {
415 pll_div
->freqmode
= post_table
[i
].freqmode
;
416 pll_div
->postscale
= post_table
[i
].postscale
;
417 target
*= post_table
[i
].div
;
422 if (i
== ARRAY_SIZE(post_table
)) {
423 printk(KERN_ERR
"wm8580: Unable to scale output frequency "
428 Ndiv
= target
/ source
;
432 pll_div
->prescale
= 1;
433 Ndiv
= target
/ source
;
435 pll_div
->prescale
= 0;
437 if ((Ndiv
< 5) || (Ndiv
> 13)) {
439 "WM8580 N=%u outside supported range\n", Ndiv
);
444 Nmod
= target
% source
;
445 Kpart
= FIXED_PLL_SIZE
* (long long)Nmod
;
447 do_div(Kpart
, source
);
449 K
= Kpart
& 0xFFFFFFFF;
453 pr_debug("PLL %x.%x prescale %d freqmode %d postscale %d\n",
454 pll_div
->n
, pll_div
->k
, pll_div
->prescale
, pll_div
->freqmode
,
460 static int wm8580_set_dai_pll(struct snd_soc_dai
*codec_dai
, int pll_id
,
461 int source
, unsigned int freq_in
, unsigned int freq_out
)
464 struct snd_soc_component
*component
= codec_dai
->component
;
465 struct wm8580_priv
*wm8580
= snd_soc_component_get_drvdata(component
);
466 struct pll_state
*state
;
467 struct _pll_div pll_div
;
469 unsigned int pwr_mask
;
472 /* GCC isn't able to work out the ifs below for initialising/using
473 * pll_div so suppress warnings.
475 memset(&pll_div
, 0, sizeof(pll_div
));
481 pwr_mask
= WM8580_PWRDN2_PLLAPD
;
486 pwr_mask
= WM8580_PWRDN2_PLLBPD
;
492 if (freq_in
&& freq_out
) {
493 ret
= pll_factors(&pll_div
, freq_out
, freq_in
);
499 state
->out
= freq_out
;
501 /* Always disable the PLL - it is not safe to leave it running
502 * while reprogramming it.
504 snd_soc_component_update_bits(component
, WM8580_PWRDN2
, pwr_mask
, pwr_mask
);
506 if (!freq_in
|| !freq_out
)
509 snd_soc_component_write(component
, WM8580_PLLA1
+ offset
, pll_div
.k
& 0x1ff);
510 snd_soc_component_write(component
, WM8580_PLLA2
+ offset
, (pll_div
.k
>> 9) & 0x1ff);
511 snd_soc_component_write(component
, WM8580_PLLA3
+ offset
,
512 (pll_div
.k
>> 18 & 0xf) | (pll_div
.n
<< 4));
514 reg
= snd_soc_component_read32(component
, WM8580_PLLA4
+ offset
);
516 reg
|= pll_div
.prescale
| pll_div
.postscale
<< 1 |
517 pll_div
.freqmode
<< 3;
519 snd_soc_component_write(component
, WM8580_PLLA4
+ offset
, reg
);
521 /* All done, turn it on */
522 snd_soc_component_update_bits(component
, WM8580_PWRDN2
, pwr_mask
, 0);
527 static const int wm8580_sysclk_ratios
[] = {
528 128, 192, 256, 384, 512, 768, 1152,
532 * Set PCM DAI bit size and sample rate.
534 static int wm8580_paif_hw_params(struct snd_pcm_substream
*substream
,
535 struct snd_pcm_hw_params
*params
,
536 struct snd_soc_dai
*dai
)
538 struct snd_soc_component
*component
= dai
->component
;
539 struct wm8580_priv
*wm8580
= snd_soc_component_get_drvdata(component
);
545 switch (params_width(params
)) {
551 paifb
|= WM8580_AIF_LENGTH_20
;
555 paifb
|= WM8580_AIF_LENGTH_24
;
559 paifb
|= WM8580_AIF_LENGTH_32
;
565 /* Look up the SYSCLK ratio; accept only exact matches */
566 ratio
= wm8580
->sysclk
[dai
->driver
->id
] / params_rate(params
);
567 for (i
= 0; i
< ARRAY_SIZE(wm8580_sysclk_ratios
); i
++)
568 if (ratio
== wm8580_sysclk_ratios
[i
])
570 if (i
== ARRAY_SIZE(wm8580_sysclk_ratios
)) {
571 dev_err(component
->dev
, "Invalid clock ratio %d/%d\n",
572 wm8580
->sysclk
[dai
->driver
->id
], params_rate(params
));
576 dev_dbg(component
->dev
, "Running at %dfs with %dHz clock\n",
577 wm8580_sysclk_ratios
[i
], wm8580
->sysclk
[dai
->driver
->id
]);
579 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
584 dev_dbg(component
->dev
, "Selecting 64x OSR\n");
588 dev_dbg(component
->dev
, "Selecting 128x OSR\n");
592 snd_soc_component_update_bits(component
, WM8580_PAIF3
, WM8580_DACOSR
, osr
);
595 snd_soc_component_update_bits(component
, WM8580_PAIF1
+ dai
->driver
->id
,
596 WM8580_AIF_RATE_MASK
| WM8580_AIF_BCLKSEL_MASK
,
598 snd_soc_component_update_bits(component
, WM8580_PAIF3
+ dai
->driver
->id
,
599 WM8580_AIF_LENGTH_MASK
, paifb
);
603 static int wm8580_set_paif_dai_fmt(struct snd_soc_dai
*codec_dai
,
606 struct snd_soc_component
*component
= codec_dai
->component
;
609 int can_invert_lrclk
;
611 aifa
= snd_soc_component_read32(component
, WM8580_PAIF1
+ codec_dai
->driver
->id
);
612 aifb
= snd_soc_component_read32(component
, WM8580_PAIF3
+ codec_dai
->driver
->id
);
614 aifb
&= ~(WM8580_AIF_FMT_MASK
| WM8580_AIF_LRP
| WM8580_AIF_BCP
);
616 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
617 case SND_SOC_DAIFMT_CBS_CFS
:
618 aifa
&= ~WM8580_AIF_MS
;
620 case SND_SOC_DAIFMT_CBM_CFM
:
621 aifa
|= WM8580_AIF_MS
;
627 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
628 case SND_SOC_DAIFMT_I2S
:
629 can_invert_lrclk
= 1;
630 aifb
|= WM8580_AIF_FMT_I2S
;
632 case SND_SOC_DAIFMT_RIGHT_J
:
633 can_invert_lrclk
= 1;
634 aifb
|= WM8580_AIF_FMT_RIGHTJ
;
636 case SND_SOC_DAIFMT_LEFT_J
:
637 can_invert_lrclk
= 1;
638 aifb
|= WM8580_AIF_FMT_LEFTJ
;
640 case SND_SOC_DAIFMT_DSP_A
:
641 can_invert_lrclk
= 0;
642 aifb
|= WM8580_AIF_FMT_DSP
;
644 case SND_SOC_DAIFMT_DSP_B
:
645 can_invert_lrclk
= 0;
646 aifb
|= WM8580_AIF_FMT_DSP
;
647 aifb
|= WM8580_AIF_LRP
;
653 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
654 case SND_SOC_DAIFMT_NB_NF
:
657 case SND_SOC_DAIFMT_IB_IF
:
658 if (!can_invert_lrclk
)
660 aifb
|= WM8580_AIF_BCP
;
661 aifb
|= WM8580_AIF_LRP
;
664 case SND_SOC_DAIFMT_IB_NF
:
665 aifb
|= WM8580_AIF_BCP
;
668 case SND_SOC_DAIFMT_NB_IF
:
669 if (!can_invert_lrclk
)
671 aifb
|= WM8580_AIF_LRP
;
678 snd_soc_component_write(component
, WM8580_PAIF1
+ codec_dai
->driver
->id
, aifa
);
679 snd_soc_component_write(component
, WM8580_PAIF3
+ codec_dai
->driver
->id
, aifb
);
684 static int wm8580_set_dai_clkdiv(struct snd_soc_dai
*codec_dai
,
687 struct snd_soc_component
*component
= codec_dai
->component
;
692 reg
= snd_soc_component_read32(component
, WM8580_PLLB4
);
693 reg
&= ~WM8580_PLLB4_MCLKOUTSRC_MASK
;
696 case WM8580_CLKSRC_MCLK
:
700 case WM8580_CLKSRC_PLLA
:
701 reg
|= WM8580_PLLB4_MCLKOUTSRC_PLLA
;
703 case WM8580_CLKSRC_PLLB
:
704 reg
|= WM8580_PLLB4_MCLKOUTSRC_PLLB
;
707 case WM8580_CLKSRC_OSC
:
708 reg
|= WM8580_PLLB4_MCLKOUTSRC_OSC
;
714 snd_soc_component_write(component
, WM8580_PLLB4
, reg
);
717 case WM8580_CLKOUTSRC
:
718 reg
= snd_soc_component_read32(component
, WM8580_PLLB4
);
719 reg
&= ~WM8580_PLLB4_CLKOUTSRC_MASK
;
722 case WM8580_CLKSRC_NONE
:
725 case WM8580_CLKSRC_PLLA
:
726 reg
|= WM8580_PLLB4_CLKOUTSRC_PLLACLK
;
729 case WM8580_CLKSRC_PLLB
:
730 reg
|= WM8580_PLLB4_CLKOUTSRC_PLLBCLK
;
733 case WM8580_CLKSRC_OSC
:
734 reg
|= WM8580_PLLB4_CLKOUTSRC_OSCCLK
;
740 snd_soc_component_write(component
, WM8580_PLLB4
, reg
);
750 static int wm8580_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
751 unsigned int freq
, int dir
)
753 struct snd_soc_component
*component
= dai
->component
;
754 struct wm8580_priv
*wm8580
= snd_soc_component_get_drvdata(component
);
755 int ret
, sel
, sel_mask
, sel_shift
;
757 switch (dai
->driver
->id
) {
758 case WM8580_DAI_PAIFRX
:
763 case WM8580_DAI_PAIFTX
:
769 WARN(1, "Unknown DAI driver ID\n");
774 case WM8580_CLKSRC_ADCMCLK
:
775 if (dai
->driver
->id
!= WM8580_DAI_PAIFTX
)
777 sel
= 0 << sel_shift
;
779 case WM8580_CLKSRC_PLLA
:
780 sel
= 1 << sel_shift
;
782 case WM8580_CLKSRC_PLLB
:
783 sel
= 2 << sel_shift
;
785 case WM8580_CLKSRC_MCLK
:
786 sel
= 3 << sel_shift
;
789 dev_err(component
->dev
, "Unknown clock %d\n", clk_id
);
793 /* We really should validate PLL settings but not yet */
794 wm8580
->sysclk
[dai
->driver
->id
] = freq
;
796 ret
= snd_soc_component_update_bits(component
, WM8580_CLKSEL
, sel_mask
, sel
);
803 static int wm8580_digital_mute(struct snd_soc_dai
*codec_dai
, int mute
)
805 struct snd_soc_component
*component
= codec_dai
->component
;
808 reg
= snd_soc_component_read32(component
, WM8580_DAC_CONTROL5
);
811 reg
|= WM8580_DAC_CONTROL5_MUTEALL
;
813 reg
&= ~WM8580_DAC_CONTROL5_MUTEALL
;
815 snd_soc_component_write(component
, WM8580_DAC_CONTROL5
, reg
);
820 static int wm8580_set_bias_level(struct snd_soc_component
*component
,
821 enum snd_soc_bias_level level
)
824 case SND_SOC_BIAS_ON
:
825 case SND_SOC_BIAS_PREPARE
:
828 case SND_SOC_BIAS_STANDBY
:
829 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_OFF
) {
830 /* Power up and get individual control of the DACs */
831 snd_soc_component_update_bits(component
, WM8580_PWRDN1
,
833 WM8580_PWRDN1_ALLDACPD
, 0);
835 /* Make VMID high impedance */
836 snd_soc_component_update_bits(component
, WM8580_ADC_CONTROL1
,
841 case SND_SOC_BIAS_OFF
:
842 snd_soc_component_update_bits(component
, WM8580_PWRDN1
,
843 WM8580_PWRDN1_PWDN
, WM8580_PWRDN1_PWDN
);
849 static int wm8580_playback_startup(struct snd_pcm_substream
*substream
,
850 struct snd_soc_dai
*dai
)
852 struct snd_soc_component
*component
= dai
->component
;
853 struct wm8580_priv
*wm8580
= snd_soc_component_get_drvdata(component
);
855 return snd_pcm_hw_constraint_minmax(substream
->runtime
,
856 SNDRV_PCM_HW_PARAM_CHANNELS
, 1, wm8580
->drvdata
->num_dacs
* 2);
859 #define WM8580_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
860 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
862 static const struct snd_soc_dai_ops wm8580_dai_ops_playback
= {
863 .startup
= wm8580_playback_startup
,
864 .set_sysclk
= wm8580_set_sysclk
,
865 .hw_params
= wm8580_paif_hw_params
,
866 .set_fmt
= wm8580_set_paif_dai_fmt
,
867 .set_clkdiv
= wm8580_set_dai_clkdiv
,
868 .set_pll
= wm8580_set_dai_pll
,
869 .digital_mute
= wm8580_digital_mute
,
872 static const struct snd_soc_dai_ops wm8580_dai_ops_capture
= {
873 .set_sysclk
= wm8580_set_sysclk
,
874 .hw_params
= wm8580_paif_hw_params
,
875 .set_fmt
= wm8580_set_paif_dai_fmt
,
876 .set_clkdiv
= wm8580_set_dai_clkdiv
,
877 .set_pll
= wm8580_set_dai_pll
,
880 static struct snd_soc_dai_driver wm8580_dai
[] = {
882 .name
= "wm8580-hifi-playback",
883 .id
= WM8580_DAI_PAIFRX
,
885 .stream_name
= "Playback",
887 .rates
= SNDRV_PCM_RATE_8000_192000
,
888 .formats
= WM8580_FORMATS
,
890 .ops
= &wm8580_dai_ops_playback
,
893 .name
= "wm8580-hifi-capture",
894 .id
= WM8580_DAI_PAIFTX
,
896 .stream_name
= "Capture",
899 .rates
= SNDRV_PCM_RATE_8000_192000
,
900 .formats
= WM8580_FORMATS
,
902 .ops
= &wm8580_dai_ops_capture
,
906 static int wm8580_probe(struct snd_soc_component
*component
)
908 struct wm8580_priv
*wm8580
= snd_soc_component_get_drvdata(component
);
909 struct snd_soc_dapm_context
*dapm
= snd_soc_component_get_dapm(component
);
912 switch (wm8580
->drvdata
->num_dacs
) {
914 snd_soc_add_component_controls(component
, wm8581_snd_controls
,
915 ARRAY_SIZE(wm8581_snd_controls
));
916 snd_soc_dapm_new_controls(dapm
, wm8581_dapm_widgets
,
917 ARRAY_SIZE(wm8581_dapm_widgets
));
918 snd_soc_dapm_add_routes(dapm
, wm8581_dapm_routes
,
919 ARRAY_SIZE(wm8581_dapm_routes
));
925 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8580
->supplies
),
928 dev_err(component
->dev
, "Failed to enable supplies: %d\n", ret
);
929 goto err_regulator_get
;
932 /* Get the codec into a known state */
933 ret
= snd_soc_component_write(component
, WM8580_RESET
, 0);
935 dev_err(component
->dev
, "Failed to reset component: %d\n", ret
);
936 goto err_regulator_enable
;
941 err_regulator_enable
:
942 regulator_bulk_disable(ARRAY_SIZE(wm8580
->supplies
), wm8580
->supplies
);
947 /* power down chip */
948 static void wm8580_remove(struct snd_soc_component
*component
)
950 struct wm8580_priv
*wm8580
= snd_soc_component_get_drvdata(component
);
952 regulator_bulk_disable(ARRAY_SIZE(wm8580
->supplies
), wm8580
->supplies
);
955 static const struct snd_soc_component_driver soc_component_dev_wm8580
= {
956 .probe
= wm8580_probe
,
957 .remove
= wm8580_remove
,
958 .set_bias_level
= wm8580_set_bias_level
,
959 .controls
= wm8580_snd_controls
,
960 .num_controls
= ARRAY_SIZE(wm8580_snd_controls
),
961 .dapm_widgets
= wm8580_dapm_widgets
,
962 .num_dapm_widgets
= ARRAY_SIZE(wm8580_dapm_widgets
),
963 .dapm_routes
= wm8580_dapm_routes
,
964 .num_dapm_routes
= ARRAY_SIZE(wm8580_dapm_routes
),
966 .use_pmdown_time
= 1,
968 .non_legacy_dai_naming
= 1,
971 static const struct regmap_config wm8580_regmap
= {
974 .max_register
= WM8580_MAX_REGISTER
,
976 .reg_defaults
= wm8580_reg_defaults
,
977 .num_reg_defaults
= ARRAY_SIZE(wm8580_reg_defaults
),
978 .cache_type
= REGCACHE_RBTREE
,
980 .volatile_reg
= wm8580_volatile
,
983 static const struct wm8580_driver_data wm8580_data
= {
987 static const struct wm8580_driver_data wm8581_data
= {
991 static const struct of_device_id wm8580_of_match
[] = {
992 { .compatible
= "wlf,wm8580", .data
= &wm8580_data
},
993 { .compatible
= "wlf,wm8581", .data
= &wm8581_data
},
996 MODULE_DEVICE_TABLE(of
, wm8580_of_match
);
998 static int wm8580_i2c_probe(struct i2c_client
*i2c
,
999 const struct i2c_device_id
*id
)
1001 const struct of_device_id
*of_id
;
1002 struct wm8580_priv
*wm8580
;
1005 wm8580
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm8580_priv
),
1010 wm8580
->regmap
= devm_regmap_init_i2c(i2c
, &wm8580_regmap
);
1011 if (IS_ERR(wm8580
->regmap
))
1012 return PTR_ERR(wm8580
->regmap
);
1014 for (i
= 0; i
< ARRAY_SIZE(wm8580
->supplies
); i
++)
1015 wm8580
->supplies
[i
].supply
= wm8580_supply_names
[i
];
1017 ret
= devm_regulator_bulk_get(&i2c
->dev
, ARRAY_SIZE(wm8580
->supplies
),
1020 dev_err(&i2c
->dev
, "Failed to request supplies: %d\n", ret
);
1024 i2c_set_clientdata(i2c
, wm8580
);
1026 of_id
= of_match_device(wm8580_of_match
, &i2c
->dev
);
1028 wm8580
->drvdata
= of_id
->data
;
1030 if (!wm8580
->drvdata
) {
1031 dev_err(&i2c
->dev
, "failed to find driver data\n");
1035 ret
= devm_snd_soc_register_component(&i2c
->dev
,
1036 &soc_component_dev_wm8580
, wm8580_dai
, ARRAY_SIZE(wm8580_dai
));
1041 static const struct i2c_device_id wm8580_i2c_id
[] = {
1042 { "wm8580", (kernel_ulong_t
)&wm8580_data
},
1043 { "wm8581", (kernel_ulong_t
)&wm8581_data
},
1046 MODULE_DEVICE_TABLE(i2c
, wm8580_i2c_id
);
1048 static struct i2c_driver wm8580_i2c_driver
= {
1051 .of_match_table
= wm8580_of_match
,
1053 .probe
= wm8580_i2c_probe
,
1054 .id_table
= wm8580_i2c_id
,
1057 module_i2c_driver(wm8580_i2c_driver
);
1059 MODULE_DESCRIPTION("ASoC WM8580 driver");
1060 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1061 MODULE_AUTHOR("Matt Flax <flatmax@flatmax.org>");
1062 MODULE_LICENSE("GPL");