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
)) {
311 dev_err(dai
->dev
, "Unsupported word length: %u\n",
312 params_width(params
));
316 /* set word length */
317 snd_soc_update_bits(codec
, WM8804_AIFTX
, 0xc, blen
<< 2);
318 snd_soc_update_bits(codec
, WM8804_AIFRX
, 0xc, blen
<< 2);
331 /* PLL rate to output rate divisions */
334 unsigned int freqmode
;
335 unsigned int mclkdiv
;
347 #define FIXED_PLL_SIZE ((1ULL << 22) * 10)
348 static int pll_factors(struct pll_div
*pll_div
, unsigned int target
,
349 unsigned int source
, unsigned int mclk_div
)
352 unsigned long int K
, Ndiv
, Nmod
, tmp
;
356 * Scale the output frequency up; the PLL should run in the
357 * region of 90-100MHz.
359 for (i
= 0; i
< ARRAY_SIZE(post_table
); i
++) {
360 tmp
= target
* post_table
[i
].div
;
361 if ((tmp
>= 90000000 && tmp
<= 100000000) &&
362 (mclk_div
== post_table
[i
].mclkdiv
)) {
363 pll_div
->freqmode
= post_table
[i
].freqmode
;
364 pll_div
->mclkdiv
= post_table
[i
].mclkdiv
;
365 target
*= post_table
[i
].div
;
370 if (i
== ARRAY_SIZE(post_table
)) {
371 pr_err("%s: Unable to scale output frequency: %uHz\n",
376 pll_div
->prescale
= 0;
377 Ndiv
= target
/ source
;
380 pll_div
->prescale
= 1;
381 Ndiv
= target
/ source
;
384 if (Ndiv
< 5 || Ndiv
> 13) {
385 pr_err("%s: WM8804 N value is not within the recommended range: %lu\n",
391 Nmod
= target
% source
;
392 Kpart
= FIXED_PLL_SIZE
* (u64
)Nmod
;
394 do_div(Kpart
, source
);
396 K
= Kpart
& 0xffffffff;
405 static int wm8804_set_pll(struct snd_soc_dai
*dai
, int pll_id
,
406 int source
, unsigned int freq_in
,
407 unsigned int freq_out
)
409 struct snd_soc_codec
*codec
= dai
->codec
;
410 struct wm8804_priv
*wm8804
= snd_soc_codec_get_drvdata(codec
);
413 if (!freq_in
|| !freq_out
) {
414 /* disable the PLL */
415 regmap_update_bits_check(wm8804
->regmap
, WM8804_PWRDN
,
418 pm_runtime_put(wm8804
->dev
);
421 struct pll_div pll_div
;
423 ret
= pll_factors(&pll_div
, freq_out
, freq_in
,
428 /* power down the PLL before reprogramming it */
429 regmap_update_bits_check(wm8804
->regmap
, WM8804_PWRDN
,
432 pm_runtime_get_sync(wm8804
->dev
);
434 /* set PLLN and PRESCALE */
435 snd_soc_update_bits(codec
, WM8804_PLL4
, 0xf | 0x10,
436 pll_div
.n
| (pll_div
.prescale
<< 4));
437 /* set mclkdiv and freqmode */
438 snd_soc_update_bits(codec
, WM8804_PLL5
, 0x3 | 0x8,
439 pll_div
.freqmode
| (pll_div
.mclkdiv
<< 3));
441 snd_soc_write(codec
, WM8804_PLL1
, pll_div
.k
& 0xff);
442 snd_soc_write(codec
, WM8804_PLL2
, (pll_div
.k
>> 8) & 0xff);
443 snd_soc_write(codec
, WM8804_PLL3
, pll_div
.k
>> 16);
445 /* power up the PLL */
446 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x1, 0);
452 static int wm8804_set_sysclk(struct snd_soc_dai
*dai
,
453 int clk_id
, unsigned int freq
, int dir
)
455 struct snd_soc_codec
*codec
;
460 case WM8804_TX_CLKSRC_MCLK
:
461 if ((freq
>= 10000000 && freq
<= 14400000)
462 || (freq
>= 16280000 && freq
<= 27000000))
463 snd_soc_update_bits(codec
, WM8804_PLL6
, 0x80, 0x80);
465 dev_err(dai
->dev
, "OSCCLOCK is not within the "
466 "recommended range: %uHz\n", freq
);
470 case WM8804_TX_CLKSRC_PLL
:
471 snd_soc_update_bits(codec
, WM8804_PLL6
, 0x80, 0);
473 case WM8804_CLKOUT_SRC_CLK1
:
474 snd_soc_update_bits(codec
, WM8804_PLL6
, 0x8, 0);
476 case WM8804_CLKOUT_SRC_OSCCLK
:
477 snd_soc_update_bits(codec
, WM8804_PLL6
, 0x8, 0x8);
480 dev_err(dai
->dev
, "Unknown clock source: %d\n", clk_id
);
487 static int wm8804_set_clkdiv(struct snd_soc_dai
*dai
,
490 struct snd_soc_codec
*codec
;
491 struct wm8804_priv
*wm8804
;
495 case WM8804_CLKOUT_DIV
:
496 snd_soc_update_bits(codec
, WM8804_PLL5
, 0x30,
499 case WM8804_MCLK_DIV
:
500 wm8804
= snd_soc_codec_get_drvdata(codec
);
501 wm8804
->mclk_div
= div
;
504 dev_err(dai
->dev
, "Unknown clock divider: %d\n", div_id
);
510 static const struct snd_soc_dai_ops wm8804_dai_ops
= {
511 .hw_params
= wm8804_hw_params
,
512 .set_fmt
= wm8804_set_fmt
,
513 .set_sysclk
= wm8804_set_sysclk
,
514 .set_clkdiv
= wm8804_set_clkdiv
,
515 .set_pll
= wm8804_set_pll
518 #define WM8804_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
519 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
521 #define WM8804_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
522 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
523 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | \
524 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000)
526 static struct snd_soc_dai_driver wm8804_dai
= {
527 .name
= "wm8804-spdif",
529 .stream_name
= "Playback",
532 .rates
= WM8804_RATES
,
533 .formats
= WM8804_FORMATS
,
536 .stream_name
= "Capture",
539 .rates
= WM8804_RATES
,
540 .formats
= WM8804_FORMATS
,
542 .ops
= &wm8804_dai_ops
,
546 static const struct snd_soc_codec_driver soc_codec_dev_wm8804
= {
547 .idle_bias_off
= false,
549 .component_driver
= {
550 .dapm_widgets
= wm8804_dapm_widgets
,
551 .num_dapm_widgets
= ARRAY_SIZE(wm8804_dapm_widgets
),
552 .dapm_routes
= wm8804_dapm_routes
,
553 .num_dapm_routes
= ARRAY_SIZE(wm8804_dapm_routes
),
557 const struct regmap_config wm8804_regmap_config
= {
561 .max_register
= WM8804_MAX_REGISTER
,
562 .volatile_reg
= wm8804_volatile
,
564 .cache_type
= REGCACHE_RBTREE
,
565 .reg_defaults
= wm8804_reg_defaults
,
566 .num_reg_defaults
= ARRAY_SIZE(wm8804_reg_defaults
),
568 EXPORT_SYMBOL_GPL(wm8804_regmap_config
);
570 int wm8804_probe(struct device
*dev
, struct regmap
*regmap
)
572 struct wm8804_priv
*wm8804
;
573 unsigned int id1
, id2
;
576 wm8804
= devm_kzalloc(dev
, sizeof(*wm8804
), GFP_KERNEL
);
580 dev_set_drvdata(dev
, wm8804
);
583 wm8804
->regmap
= regmap
;
585 wm8804
->reset
= devm_gpiod_get_optional(dev
, "wlf,reset",
587 if (IS_ERR(wm8804
->reset
)) {
588 ret
= PTR_ERR(wm8804
->reset
);
589 dev_err(dev
, "Failed to get reset line: %d\n", ret
);
593 for (i
= 0; i
< ARRAY_SIZE(wm8804
->supplies
); i
++)
594 wm8804
->supplies
[i
].supply
= wm8804_supply_names
[i
];
596 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(wm8804
->supplies
),
599 dev_err(dev
, "Failed to request supplies: %d\n", ret
);
603 wm8804
->disable_nb
[0].notifier_call
= wm8804_regulator_event_0
;
604 wm8804
->disable_nb
[1].notifier_call
= wm8804_regulator_event_1
;
606 /* This should really be moved into the regulator core */
607 for (i
= 0; i
< ARRAY_SIZE(wm8804
->supplies
); i
++) {
608 struct regulator
*regulator
= wm8804
->supplies
[i
].consumer
;
610 ret
= devm_regulator_register_notifier(regulator
,
611 &wm8804
->disable_nb
[i
]);
614 "Failed to register regulator notifier: %d\n",
620 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8804
->supplies
),
623 dev_err(dev
, "Failed to enable supplies: %d\n", ret
);
628 gpiod_set_value_cansleep(wm8804
->reset
, 1);
630 ret
= regmap_read(regmap
, WM8804_RST_DEVID1
, &id1
);
632 dev_err(dev
, "Failed to read device ID: %d\n", ret
);
636 ret
= regmap_read(regmap
, WM8804_DEVID2
, &id2
);
638 dev_err(dev
, "Failed to read device ID: %d\n", ret
);
642 id2
= (id2
<< 8) | id1
;
645 dev_err(dev
, "Invalid device ID: %#x\n", id2
);
650 ret
= regmap_read(regmap
, WM8804_DEVREV
, &id1
);
652 dev_err(dev
, "Failed to read device revision: %d\n",
656 dev_info(dev
, "revision %c\n", id1
+ 'A');
658 if (!wm8804
->reset
) {
659 ret
= wm8804_soft_reset(wm8804
);
661 dev_err(dev
, "Failed to issue reset: %d\n", ret
);
666 ret
= snd_soc_register_codec(dev
, &soc_codec_dev_wm8804
,
669 dev_err(dev
, "Failed to register CODEC: %d\n", ret
);
673 pm_runtime_set_active(dev
);
674 pm_runtime_enable(dev
);
675 pm_runtime_idle(dev
);
680 regulator_bulk_disable(ARRAY_SIZE(wm8804
->supplies
), wm8804
->supplies
);
683 EXPORT_SYMBOL_GPL(wm8804_probe
);
685 void wm8804_remove(struct device
*dev
)
687 pm_runtime_disable(dev
);
688 snd_soc_unregister_codec(dev
);
690 EXPORT_SYMBOL_GPL(wm8804_remove
);
692 #if IS_ENABLED(CONFIG_PM)
693 static int wm8804_runtime_resume(struct device
*dev
)
695 struct wm8804_priv
*wm8804
= dev_get_drvdata(dev
);
698 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8804
->supplies
),
701 dev_err(wm8804
->dev
, "Failed to enable supplies: %d\n", ret
);
705 regcache_sync(wm8804
->regmap
);
707 /* Power up OSCCLK */
708 regmap_update_bits(wm8804
->regmap
, WM8804_PWRDN
, 0x8, 0x0);
713 static int wm8804_runtime_suspend(struct device
*dev
)
715 struct wm8804_priv
*wm8804
= dev_get_drvdata(dev
);
717 /* Power down OSCCLK */
718 regmap_update_bits(wm8804
->regmap
, WM8804_PWRDN
, 0x8, 0x8);
720 regulator_bulk_disable(ARRAY_SIZE(wm8804
->supplies
),
727 const struct dev_pm_ops wm8804_pm
= {
728 SET_RUNTIME_PM_OPS(wm8804_runtime_suspend
, wm8804_runtime_resume
, NULL
)
730 EXPORT_SYMBOL_GPL(wm8804_pm
);
732 MODULE_DESCRIPTION("ASoC WM8804 driver");
733 MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
734 MODULE_LICENSE("GPL");