2 * wm8804.c -- WM8804 S/PDIF transceiver driver
4 * Copyright 2010-11 Wolfson Microelectronics plc
6 * Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/delay.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/of_device.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
29 #include <sound/soc-dapm.h>
33 #define WM8804_NUM_SUPPLIES 2
34 static const char *wm8804_supply_names
[WM8804_NUM_SUPPLIES
] = {
39 static const struct reg_default wm8804_reg_defaults
[] = {
40 { 3, 0x21 }, /* R3 - PLL1 */
41 { 4, 0xFD }, /* R4 - PLL2 */
42 { 5, 0x36 }, /* R5 - PLL3 */
43 { 6, 0x07 }, /* R6 - PLL4 */
44 { 7, 0x16 }, /* R7 - PLL5 */
45 { 8, 0x18 }, /* R8 - PLL6 */
46 { 9, 0xFF }, /* R9 - SPDMODE */
47 { 10, 0x00 }, /* R10 - INTMASK */
48 { 18, 0x00 }, /* R18 - SPDTX1 */
49 { 19, 0x00 }, /* R19 - SPDTX2 */
50 { 20, 0x00 }, /* R20 - SPDTX3 */
51 { 21, 0x71 }, /* R21 - SPDTX4 */
52 { 22, 0x0B }, /* R22 - SPDTX5 */
53 { 23, 0x70 }, /* R23 - GPO0 */
54 { 24, 0x57 }, /* R24 - GPO1 */
55 { 26, 0x42 }, /* R26 - GPO2 */
56 { 27, 0x06 }, /* R27 - AIFTX */
57 { 28, 0x06 }, /* R28 - AIFRX */
58 { 29, 0x80 }, /* R29 - SPDRX1 */
59 { 30, 0x07 }, /* R30 - PWRDN */
64 struct regmap
*regmap
;
65 struct regulator_bulk_data supplies
[WM8804_NUM_SUPPLIES
];
66 struct notifier_block disable_nb
[WM8804_NUM_SUPPLIES
];
69 struct gpio_desc
*reset
;
74 static int txsrc_put(struct snd_kcontrol
*kcontrol
,
75 struct snd_ctl_elem_value
*ucontrol
);
77 static int wm8804_aif_event(struct snd_soc_dapm_widget
*w
,
78 struct snd_kcontrol
*kcontrol
, int event
);
81 * We can't use the same notifier block for more than one supply and
82 * there's no way I can see to get from a callback to the caller
83 * except container_of().
85 #define WM8804_REGULATOR_EVENT(n) \
86 static int wm8804_regulator_event_##n(struct notifier_block *nb, \
87 unsigned long event, void *data) \
89 struct wm8804_priv *wm8804 = container_of(nb, struct wm8804_priv, \
91 if (event & REGULATOR_EVENT_DISABLE) { \
92 regcache_mark_dirty(wm8804->regmap); \
97 WM8804_REGULATOR_EVENT(0)
98 WM8804_REGULATOR_EVENT(1)
100 static const char *txsrc_text
[] = { "S/PDIF RX", "AIF" };
101 static SOC_ENUM_SINGLE_DECL(txsrc
, WM8804_SPDTX4
, 6, txsrc_text
);
103 static const struct snd_kcontrol_new wm8804_tx_source_mux
[] = {
104 SOC_DAPM_ENUM_EXT("Input Source", txsrc
,
105 snd_soc_dapm_get_enum_double
, txsrc_put
),
108 static const struct snd_soc_dapm_widget wm8804_dapm_widgets
[] = {
109 SND_SOC_DAPM_OUTPUT("SPDIF Out"),
110 SND_SOC_DAPM_INPUT("SPDIF In"),
112 SND_SOC_DAPM_PGA("SPDIFTX", WM8804_PWRDN
, 2, 1, NULL
, 0),
113 SND_SOC_DAPM_PGA("SPDIFRX", WM8804_PWRDN
, 1, 1, NULL
, 0),
115 SND_SOC_DAPM_MUX("Tx Source", SND_SOC_NOPM
, 6, 0, wm8804_tx_source_mux
),
117 SND_SOC_DAPM_AIF_OUT_E("AIFTX", NULL
, 0, SND_SOC_NOPM
, 0, 0, wm8804_aif_event
,
118 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_POST_PMD
),
119 SND_SOC_DAPM_AIF_IN_E("AIFRX", NULL
, 0, SND_SOC_NOPM
, 0, 0, wm8804_aif_event
,
120 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_POST_PMD
),
123 static const struct snd_soc_dapm_route wm8804_dapm_routes
[] = {
124 { "AIFRX", NULL
, "Playback" },
125 { "Tx Source", "AIF", "AIFRX" },
127 { "SPDIFRX", NULL
, "SPDIF In" },
128 { "Tx Source", "S/PDIF RX", "SPDIFRX" },
130 { "SPDIFTX", NULL
, "Tx Source" },
131 { "SPDIF Out", NULL
, "SPDIFTX" },
133 { "AIFTX", NULL
, "SPDIFRX" },
134 { "Capture", NULL
, "AIFTX" },
137 static int wm8804_aif_event(struct snd_soc_dapm_widget
*w
,
138 struct snd_kcontrol
*kcontrol
, int event
)
140 struct snd_soc_codec
*codec
= snd_soc_dapm_to_codec(w
->dapm
);
141 struct wm8804_priv
*wm8804
= snd_soc_codec_get_drvdata(codec
);
144 case SND_SOC_DAPM_POST_PMU
:
145 /* power up the aif */
146 if (!wm8804
->aif_pwr
)
147 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x10, 0x0);
150 case SND_SOC_DAPM_POST_PMD
:
151 /* power down only both paths are disabled */
153 if (!wm8804
->aif_pwr
)
154 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x10, 0x10);
161 static int txsrc_put(struct snd_kcontrol
*kcontrol
,
162 struct snd_ctl_elem_value
*ucontrol
)
164 struct snd_soc_codec
*codec
= snd_soc_dapm_kcontrol_codec(kcontrol
);
165 struct snd_soc_dapm_context
*dapm
= snd_soc_codec_get_dapm(codec
);
166 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
167 unsigned int val
= ucontrol
->value
.enumerated
.item
[0] << e
->shift_l
;
168 unsigned int mask
= 1 << e
->shift_l
;
171 if (val
!= 0 && val
!= mask
)
174 snd_soc_dapm_mutex_lock(dapm
);
176 if (snd_soc_test_bits(codec
, e
->reg
, mask
, val
)) {
177 /* save the current power state of the transmitter */
178 txpwr
= snd_soc_read(codec
, WM8804_PWRDN
) & 0x4;
180 /* power down the transmitter */
181 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x4, 0x4);
183 /* set the tx source */
184 snd_soc_update_bits(codec
, e
->reg
, mask
, val
);
186 /* restore the transmitter's configuration */
187 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x4, txpwr
);
190 snd_soc_dapm_mutex_unlock(dapm
);
195 static bool wm8804_volatile(struct device
*dev
, unsigned int reg
)
198 case WM8804_RST_DEVID1
:
214 static int wm8804_soft_reset(struct wm8804_priv
*wm8804
)
216 return regmap_write(wm8804
->regmap
, WM8804_RST_DEVID1
, 0x0);
219 static int wm8804_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
221 struct snd_soc_codec
*codec
;
222 u16 format
, master
, bcp
, lrp
;
226 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
227 case SND_SOC_DAIFMT_I2S
:
230 case SND_SOC_DAIFMT_RIGHT_J
:
233 case SND_SOC_DAIFMT_LEFT_J
:
236 case SND_SOC_DAIFMT_DSP_A
:
237 case SND_SOC_DAIFMT_DSP_B
:
241 dev_err(dai
->dev
, "Unknown dai format\n");
245 /* set data format */
246 snd_soc_update_bits(codec
, WM8804_AIFTX
, 0x3, format
);
247 snd_soc_update_bits(codec
, WM8804_AIFRX
, 0x3, format
);
249 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
250 case SND_SOC_DAIFMT_CBM_CFM
:
253 case SND_SOC_DAIFMT_CBS_CFS
:
257 dev_err(dai
->dev
, "Unknown master/slave configuration\n");
261 /* set master/slave mode */
262 snd_soc_update_bits(codec
, WM8804_AIFRX
, 0x40, master
<< 6);
265 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
266 case SND_SOC_DAIFMT_NB_NF
:
268 case SND_SOC_DAIFMT_IB_IF
:
271 case SND_SOC_DAIFMT_IB_NF
:
274 case SND_SOC_DAIFMT_NB_IF
:
278 dev_err(dai
->dev
, "Unknown polarity configuration\n");
282 /* set frame inversion */
283 snd_soc_update_bits(codec
, WM8804_AIFTX
, 0x10 | 0x20,
284 (bcp
<< 4) | (lrp
<< 5));
285 snd_soc_update_bits(codec
, WM8804_AIFRX
, 0x10 | 0x20,
286 (bcp
<< 4) | (lrp
<< 5));
290 static int wm8804_hw_params(struct snd_pcm_substream
*substream
,
291 struct snd_pcm_hw_params
*params
,
292 struct snd_soc_dai
*dai
)
294 struct snd_soc_codec
*codec
;
299 switch (params_width(params
)) {
310 dev_err(dai
->dev
, "Unsupported word length: %u\n",
311 params_width(params
));
315 /* set word length */
316 snd_soc_update_bits(codec
, WM8804_AIFTX
, 0xc, blen
<< 2);
317 snd_soc_update_bits(codec
, WM8804_AIFRX
, 0xc, blen
<< 2);
330 /* PLL rate to output rate divisions */
333 unsigned int freqmode
;
334 unsigned int mclkdiv
;
346 #define FIXED_PLL_SIZE ((1ULL << 22) * 10)
347 static int pll_factors(struct pll_div
*pll_div
, unsigned int target
,
348 unsigned int source
, unsigned int mclk_div
)
351 unsigned long int K
, Ndiv
, Nmod
, tmp
;
355 * Scale the output frequency up; the PLL should run in the
356 * region of 90-100MHz.
358 for (i
= 0; i
< ARRAY_SIZE(post_table
); i
++) {
359 tmp
= target
* post_table
[i
].div
;
360 if ((tmp
>= 90000000 && tmp
<= 100000000) &&
361 (mclk_div
== post_table
[i
].mclkdiv
)) {
362 pll_div
->freqmode
= post_table
[i
].freqmode
;
363 pll_div
->mclkdiv
= post_table
[i
].mclkdiv
;
364 target
*= post_table
[i
].div
;
369 if (i
== ARRAY_SIZE(post_table
)) {
370 pr_err("%s: Unable to scale output frequency: %uHz\n",
375 pll_div
->prescale
= 0;
376 Ndiv
= target
/ source
;
379 pll_div
->prescale
= 1;
380 Ndiv
= target
/ source
;
383 if (Ndiv
< 5 || Ndiv
> 13) {
384 pr_err("%s: WM8804 N value is not within the recommended range: %lu\n",
390 Nmod
= target
% source
;
391 Kpart
= FIXED_PLL_SIZE
* (u64
)Nmod
;
393 do_div(Kpart
, source
);
395 K
= Kpart
& 0xffffffff;
404 static int wm8804_set_pll(struct snd_soc_dai
*dai
, int pll_id
,
405 int source
, unsigned int freq_in
,
406 unsigned int freq_out
)
408 struct snd_soc_codec
*codec
= dai
->codec
;
409 struct wm8804_priv
*wm8804
= snd_soc_codec_get_drvdata(codec
);
412 if (!freq_in
|| !freq_out
) {
413 /* disable the PLL */
414 regmap_update_bits_check(wm8804
->regmap
, WM8804_PWRDN
,
417 pm_runtime_put(wm8804
->dev
);
420 struct pll_div pll_div
;
422 ret
= pll_factors(&pll_div
, freq_out
, freq_in
,
427 /* power down the PLL before reprogramming it */
428 regmap_update_bits_check(wm8804
->regmap
, WM8804_PWRDN
,
431 pm_runtime_get_sync(wm8804
->dev
);
433 /* set PLLN and PRESCALE */
434 snd_soc_update_bits(codec
, WM8804_PLL4
, 0xf | 0x10,
435 pll_div
.n
| (pll_div
.prescale
<< 4));
436 /* set mclkdiv and freqmode */
437 snd_soc_update_bits(codec
, WM8804_PLL5
, 0x3 | 0x8,
438 pll_div
.freqmode
| (pll_div
.mclkdiv
<< 3));
440 snd_soc_write(codec
, WM8804_PLL1
, pll_div
.k
& 0xff);
441 snd_soc_write(codec
, WM8804_PLL2
, (pll_div
.k
>> 8) & 0xff);
442 snd_soc_write(codec
, WM8804_PLL3
, pll_div
.k
>> 16);
444 /* power up the PLL */
445 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x1, 0);
451 static int wm8804_set_sysclk(struct snd_soc_dai
*dai
,
452 int clk_id
, unsigned int freq
, int dir
)
454 struct snd_soc_codec
*codec
;
459 case WM8804_TX_CLKSRC_MCLK
:
460 if ((freq
>= 10000000 && freq
<= 14400000)
461 || (freq
>= 16280000 && freq
<= 27000000))
462 snd_soc_update_bits(codec
, WM8804_PLL6
, 0x80, 0x80);
464 dev_err(dai
->dev
, "OSCCLOCK is not within the "
465 "recommended range: %uHz\n", freq
);
469 case WM8804_TX_CLKSRC_PLL
:
470 snd_soc_update_bits(codec
, WM8804_PLL6
, 0x80, 0);
472 case WM8804_CLKOUT_SRC_CLK1
:
473 snd_soc_update_bits(codec
, WM8804_PLL6
, 0x8, 0);
475 case WM8804_CLKOUT_SRC_OSCCLK
:
476 snd_soc_update_bits(codec
, WM8804_PLL6
, 0x8, 0x8);
479 dev_err(dai
->dev
, "Unknown clock source: %d\n", clk_id
);
486 static int wm8804_set_clkdiv(struct snd_soc_dai
*dai
,
489 struct snd_soc_codec
*codec
;
490 struct wm8804_priv
*wm8804
;
494 case WM8804_CLKOUT_DIV
:
495 snd_soc_update_bits(codec
, WM8804_PLL5
, 0x30,
498 case WM8804_MCLK_DIV
:
499 wm8804
= snd_soc_codec_get_drvdata(codec
);
500 wm8804
->mclk_div
= div
;
503 dev_err(dai
->dev
, "Unknown clock divider: %d\n", div_id
);
509 static const struct snd_soc_dai_ops wm8804_dai_ops
= {
510 .hw_params
= wm8804_hw_params
,
511 .set_fmt
= wm8804_set_fmt
,
512 .set_sysclk
= wm8804_set_sysclk
,
513 .set_clkdiv
= wm8804_set_clkdiv
,
514 .set_pll
= wm8804_set_pll
517 #define WM8804_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
518 SNDRV_PCM_FMTBIT_S24_LE)
520 #define WM8804_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
521 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
522 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | \
523 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000)
525 static struct snd_soc_dai_driver wm8804_dai
= {
526 .name
= "wm8804-spdif",
528 .stream_name
= "Playback",
531 .rates
= WM8804_RATES
,
532 .formats
= WM8804_FORMATS
,
535 .stream_name
= "Capture",
538 .rates
= WM8804_RATES
,
539 .formats
= WM8804_FORMATS
,
541 .ops
= &wm8804_dai_ops
,
545 static const struct snd_soc_codec_driver soc_codec_dev_wm8804
= {
546 .idle_bias_off
= true,
548 .component_driver
= {
549 .dapm_widgets
= wm8804_dapm_widgets
,
550 .num_dapm_widgets
= ARRAY_SIZE(wm8804_dapm_widgets
),
551 .dapm_routes
= wm8804_dapm_routes
,
552 .num_dapm_routes
= ARRAY_SIZE(wm8804_dapm_routes
),
556 const struct regmap_config wm8804_regmap_config
= {
560 .max_register
= WM8804_MAX_REGISTER
,
561 .volatile_reg
= wm8804_volatile
,
563 .cache_type
= REGCACHE_RBTREE
,
564 .reg_defaults
= wm8804_reg_defaults
,
565 .num_reg_defaults
= ARRAY_SIZE(wm8804_reg_defaults
),
567 EXPORT_SYMBOL_GPL(wm8804_regmap_config
);
569 int wm8804_probe(struct device
*dev
, struct regmap
*regmap
)
571 struct wm8804_priv
*wm8804
;
572 unsigned int id1
, id2
;
575 wm8804
= devm_kzalloc(dev
, sizeof(*wm8804
), GFP_KERNEL
);
579 dev_set_drvdata(dev
, wm8804
);
582 wm8804
->regmap
= regmap
;
584 wm8804
->reset
= devm_gpiod_get_optional(dev
, "wlf,reset",
586 if (IS_ERR(wm8804
->reset
)) {
587 ret
= PTR_ERR(wm8804
->reset
);
588 dev_err(dev
, "Failed to get reset line: %d\n", ret
);
592 for (i
= 0; i
< ARRAY_SIZE(wm8804
->supplies
); i
++)
593 wm8804
->supplies
[i
].supply
= wm8804_supply_names
[i
];
595 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(wm8804
->supplies
),
598 dev_err(dev
, "Failed to request supplies: %d\n", ret
);
602 wm8804
->disable_nb
[0].notifier_call
= wm8804_regulator_event_0
;
603 wm8804
->disable_nb
[1].notifier_call
= wm8804_regulator_event_1
;
605 /* This should really be moved into the regulator core */
606 for (i
= 0; i
< ARRAY_SIZE(wm8804
->supplies
); i
++) {
607 struct regulator
*regulator
= wm8804
->supplies
[i
].consumer
;
609 ret
= devm_regulator_register_notifier(regulator
,
610 &wm8804
->disable_nb
[i
]);
613 "Failed to register regulator notifier: %d\n",
619 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8804
->supplies
),
622 dev_err(dev
, "Failed to enable supplies: %d\n", ret
);
627 gpiod_set_value_cansleep(wm8804
->reset
, 1);
629 ret
= regmap_read(regmap
, WM8804_RST_DEVID1
, &id1
);
631 dev_err(dev
, "Failed to read device ID: %d\n", ret
);
635 ret
= regmap_read(regmap
, WM8804_DEVID2
, &id2
);
637 dev_err(dev
, "Failed to read device ID: %d\n", ret
);
641 id2
= (id2
<< 8) | id1
;
644 dev_err(dev
, "Invalid device ID: %#x\n", id2
);
649 ret
= regmap_read(regmap
, WM8804_DEVREV
, &id1
);
651 dev_err(dev
, "Failed to read device revision: %d\n",
655 dev_info(dev
, "revision %c\n", id1
+ 'A');
657 if (!wm8804
->reset
) {
658 ret
= wm8804_soft_reset(wm8804
);
660 dev_err(dev
, "Failed to issue reset: %d\n", ret
);
665 ret
= snd_soc_register_codec(dev
, &soc_codec_dev_wm8804
,
668 dev_err(dev
, "Failed to register CODEC: %d\n", ret
);
672 pm_runtime_set_active(dev
);
673 pm_runtime_enable(dev
);
674 pm_runtime_idle(dev
);
679 regulator_bulk_disable(ARRAY_SIZE(wm8804
->supplies
), wm8804
->supplies
);
682 EXPORT_SYMBOL_GPL(wm8804_probe
);
684 void wm8804_remove(struct device
*dev
)
686 pm_runtime_disable(dev
);
687 snd_soc_unregister_codec(dev
);
689 EXPORT_SYMBOL_GPL(wm8804_remove
);
691 #if IS_ENABLED(CONFIG_PM)
692 static int wm8804_runtime_resume(struct device
*dev
)
694 struct wm8804_priv
*wm8804
= dev_get_drvdata(dev
);
697 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8804
->supplies
),
700 dev_err(wm8804
->dev
, "Failed to enable supplies: %d\n", ret
);
704 regcache_sync(wm8804
->regmap
);
706 /* Power up OSCCLK */
707 regmap_update_bits(wm8804
->regmap
, WM8804_PWRDN
, 0x8, 0x0);
712 static int wm8804_runtime_suspend(struct device
*dev
)
714 struct wm8804_priv
*wm8804
= dev_get_drvdata(dev
);
716 /* Power down OSCCLK */
717 regmap_update_bits(wm8804
->regmap
, WM8804_PWRDN
, 0x8, 0x8);
719 regulator_bulk_disable(ARRAY_SIZE(wm8804
->supplies
),
726 const struct dev_pm_ops wm8804_pm
= {
727 SET_RUNTIME_PM_OPS(wm8804_runtime_suspend
, wm8804_runtime_resume
, NULL
)
729 EXPORT_SYMBOL_GPL(wm8804_pm
);
731 MODULE_DESCRIPTION("ASoC WM8804 driver");
732 MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
733 MODULE_LICENSE("GPL");