2 * wm8804.c -- WM8804 S/PDIF transceiver driver
4 * Copyright 2010 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/delay.h>
18 #include <linux/i2c.h>
19 #include <linux/spi/spi.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/slab.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/initval.h>
27 #include <sound/tlv.h>
31 #define WM8804_NUM_SUPPLIES 2
32 static const char *wm8804_supply_names
[WM8804_NUM_SUPPLIES
] = {
37 static const u8 wm8804_reg_defs
[] = {
38 0x05, /* R0 - RST/DEVID1 */
39 0x88, /* R1 - DEVID2 */
40 0x04, /* R2 - DEVREV */
47 0xFF, /* R9 - SPDMODE */
48 0x00, /* R10 - INTMASK */
49 0x00, /* R11 - INTSTAT */
50 0x00, /* R12 - SPDSTAT */
51 0x00, /* R13 - RXCHAN1 */
52 0x00, /* R14 - RXCHAN2 */
53 0x00, /* R15 - RXCHAN3 */
54 0x00, /* R16 - RXCHAN4 */
55 0x00, /* R17 - RXCHAN5 */
56 0x00, /* R18 - SPDTX1 */
57 0x00, /* R19 - SPDTX2 */
58 0x00, /* R20 - SPDTX3 */
59 0x71, /* R21 - SPDTX4 */
60 0x0B, /* R22 - SPDTX5 */
61 0x70, /* R23 - GPO0 */
62 0x57, /* R24 - GPO1 */
64 0x42, /* R26 - GPO2 */
65 0x06, /* R27 - AIFTX */
66 0x06, /* R28 - AIFRX */
67 0x80, /* R29 - SPDRX1 */
68 0x07, /* R30 - PWRDN */
72 enum snd_soc_control_type control_type
;
73 struct regulator_bulk_data supplies
[WM8804_NUM_SUPPLIES
];
74 struct notifier_block disable_nb
[WM8804_NUM_SUPPLIES
];
75 struct snd_soc_codec
*codec
;
78 static int txsrc_get(struct snd_kcontrol
*kcontrol
,
79 struct snd_ctl_elem_value
*ucontrol
);
81 static int txsrc_put(struct snd_kcontrol
*kcontrol
,
82 struct snd_ctl_elem_value
*ucontrol
);
85 * We can't use the same notifier block for more than one supply and
86 * there's no way I can see to get from a callback to the caller
87 * except container_of().
89 #define WM8804_REGULATOR_EVENT(n) \
90 static int wm8804_regulator_event_##n(struct notifier_block *nb, \
91 unsigned long event, void *data) \
93 struct wm8804_priv *wm8804 = container_of(nb, struct wm8804_priv, \
95 if (event & REGULATOR_EVENT_DISABLE) { \
96 wm8804->codec->cache_sync = 1; \
101 WM8804_REGULATOR_EVENT(0)
102 WM8804_REGULATOR_EVENT(1)
104 static const char *txsrc_text
[] = { "S/PDIF RX", "AIF" };
105 static const SOC_ENUM_SINGLE_EXT_DECL(txsrc
, txsrc_text
);
107 static const struct snd_kcontrol_new wm8804_snd_controls
[] = {
108 SOC_ENUM_EXT("Input Source", txsrc
, txsrc_get
, txsrc_put
),
109 SOC_SINGLE("TX Playback Switch", WM8804_PWRDN
, 2, 1, 1),
110 SOC_SINGLE("AIF Playback Switch", WM8804_PWRDN
, 4, 1, 1)
113 static int txsrc_get(struct snd_kcontrol
*kcontrol
,
114 struct snd_ctl_elem_value
*ucontrol
)
116 struct snd_soc_codec
*codec
;
119 codec
= snd_kcontrol_chip(kcontrol
);
120 src
= snd_soc_read(codec
, WM8804_SPDTX4
);
122 ucontrol
->value
.integer
.value
[0] = 1;
124 ucontrol
->value
.integer
.value
[0] = 0;
129 static int txsrc_put(struct snd_kcontrol
*kcontrol
,
130 struct snd_ctl_elem_value
*ucontrol
)
132 struct snd_soc_codec
*codec
;
133 unsigned int src
, txpwr
;
135 codec
= snd_kcontrol_chip(kcontrol
);
137 if (ucontrol
->value
.integer
.value
[0] != 0
138 && ucontrol
->value
.integer
.value
[0] != 1)
141 src
= snd_soc_read(codec
, WM8804_SPDTX4
);
142 switch ((src
& 0x40) >> 6) {
144 if (!ucontrol
->value
.integer
.value
[0])
148 if (ucontrol
->value
.integer
.value
[1])
153 /* save the current power state of the transmitter */
154 txpwr
= snd_soc_read(codec
, WM8804_PWRDN
) & 0x4;
155 /* power down the transmitter */
156 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x4, 0x4);
157 /* set the tx source */
158 snd_soc_update_bits(codec
, WM8804_SPDTX4
, 0x40,
159 ucontrol
->value
.integer
.value
[0] << 6);
161 if (ucontrol
->value
.integer
.value
[0]) {
162 /* power down the receiver */
163 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x2, 0x2);
164 /* power up the AIF */
165 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x10, 0);
167 /* don't power down the AIF -- may be used as an output */
168 /* power up the receiver */
169 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x2, 0);
172 /* restore the transmitter's configuration */
173 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x4, txpwr
);
178 static int wm8804_volatile(struct snd_soc_codec
*codec
, unsigned int reg
)
181 case WM8804_RST_DEVID1
:
199 static int wm8804_reset(struct snd_soc_codec
*codec
)
201 return snd_soc_write(codec
, WM8804_RST_DEVID1
, 0x0);
204 static int wm8804_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
206 struct snd_soc_codec
*codec
;
207 u16 format
, master
, bcp
, lrp
;
211 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
212 case SND_SOC_DAIFMT_I2S
:
215 case SND_SOC_DAIFMT_RIGHT_J
:
218 case SND_SOC_DAIFMT_LEFT_J
:
221 case SND_SOC_DAIFMT_DSP_A
:
222 case SND_SOC_DAIFMT_DSP_B
:
226 dev_err(dai
->dev
, "Unknown dai format\n");
230 /* set data format */
231 snd_soc_update_bits(codec
, WM8804_AIFTX
, 0x3, format
);
232 snd_soc_update_bits(codec
, WM8804_AIFRX
, 0x3, format
);
234 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
235 case SND_SOC_DAIFMT_CBM_CFM
:
238 case SND_SOC_DAIFMT_CBS_CFS
:
242 dev_err(dai
->dev
, "Unknown master/slave configuration\n");
246 /* set master/slave mode */
247 snd_soc_update_bits(codec
, WM8804_AIFRX
, 0x40, master
<< 6);
250 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
251 case SND_SOC_DAIFMT_NB_NF
:
253 case SND_SOC_DAIFMT_IB_IF
:
256 case SND_SOC_DAIFMT_IB_NF
:
259 case SND_SOC_DAIFMT_NB_IF
:
263 dev_err(dai
->dev
, "Unknown polarity configuration\n");
267 /* set frame inversion */
268 snd_soc_update_bits(codec
, WM8804_AIFTX
, 0x10 | 0x20,
269 (bcp
<< 4) | (lrp
<< 5));
270 snd_soc_update_bits(codec
, WM8804_AIFRX
, 0x10 | 0x20,
271 (bcp
<< 4) | (lrp
<< 5));
275 static int wm8804_hw_params(struct snd_pcm_substream
*substream
,
276 struct snd_pcm_hw_params
*params
,
277 struct snd_soc_dai
*dai
)
279 struct snd_soc_codec
*codec
;
284 switch (params_format(params
)) {
285 case SNDRV_PCM_FORMAT_S16_LE
:
288 case SNDRV_PCM_FORMAT_S20_3LE
:
291 case SNDRV_PCM_FORMAT_S24_LE
:
295 dev_err(dai
->dev
, "Unsupported word length: %u\n",
296 params_format(params
));
300 /* set word length */
301 snd_soc_update_bits(codec
, WM8804_AIFTX
, 0xc, blen
<< 2);
302 snd_soc_update_bits(codec
, WM8804_AIFRX
, 0xc, blen
<< 2);
315 /* PLL rate to output rate divisions */
318 unsigned int freqmode
;
319 unsigned int mclkdiv
;
331 #define FIXED_PLL_SIZE ((1ULL << 22) * 10)
332 static int pll_factors(struct pll_div
*pll_div
, unsigned int target
,
336 unsigned long int K
, Ndiv
, Nmod
, tmp
;
340 * Scale the output frequency up; the PLL should run in the
341 * region of 90-100MHz.
343 for (i
= 0; i
< ARRAY_SIZE(post_table
); i
++) {
344 tmp
= target
* post_table
[i
].div
;
345 if (tmp
>= 90000000 && tmp
<= 100000000) {
346 pll_div
->freqmode
= post_table
[i
].freqmode
;
347 pll_div
->mclkdiv
= post_table
[i
].mclkdiv
;
348 target
*= post_table
[i
].div
;
353 if (i
== ARRAY_SIZE(post_table
)) {
354 pr_err("%s: Unable to scale output frequency: %uHz\n",
359 pll_div
->prescale
= 0;
360 Ndiv
= target
/ source
;
363 pll_div
->prescale
= 1;
364 Ndiv
= target
/ source
;
367 if (Ndiv
< 5 || Ndiv
> 13) {
368 pr_err("%s: WM8804 N value is not within the recommended range: %lu\n",
374 Nmod
= target
% source
;
375 Kpart
= FIXED_PLL_SIZE
* (u64
)Nmod
;
377 do_div(Kpart
, source
);
379 K
= Kpart
& 0xffffffff;
388 static int wm8804_set_pll(struct snd_soc_dai
*dai
, int pll_id
,
389 int source
, unsigned int freq_in
,
390 unsigned int freq_out
)
392 struct snd_soc_codec
*codec
;
395 if (!freq_in
|| !freq_out
) {
396 /* disable the PLL */
397 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x1, 0x1);
401 struct pll_div pll_div
;
403 ret
= pll_factors(&pll_div
, freq_out
, freq_in
);
407 /* power down the PLL before reprogramming it */
408 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x1, 0x1);
410 if (!freq_in
|| !freq_out
)
413 /* set PLLN and PRESCALE */
414 snd_soc_update_bits(codec
, WM8804_PLL4
, 0xf | 0x10,
415 pll_div
.n
| (pll_div
.prescale
<< 4));
416 /* set mclkdiv and freqmode */
417 snd_soc_update_bits(codec
, WM8804_PLL5
, 0x3 | 0x8,
418 pll_div
.freqmode
| (pll_div
.mclkdiv
<< 3));
420 snd_soc_write(codec
, WM8804_PLL1
, pll_div
.k
& 0xff);
421 snd_soc_write(codec
, WM8804_PLL2
, (pll_div
.k
>> 8) & 0xff);
422 snd_soc_write(codec
, WM8804_PLL3
, pll_div
.k
>> 16);
424 /* power up the PLL */
425 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x1, 0);
431 static int wm8804_set_sysclk(struct snd_soc_dai
*dai
,
432 int clk_id
, unsigned int freq
, int dir
)
434 struct snd_soc_codec
*codec
;
439 case WM8804_TX_CLKSRC_MCLK
:
440 if ((freq
>= 10000000 && freq
<= 14400000)
441 || (freq
>= 16280000 && freq
<= 27000000))
442 snd_soc_update_bits(codec
, WM8804_PLL6
, 0x80, 0x80);
444 dev_err(dai
->dev
, "OSCCLOCK is not within the "
445 "recommended range: %uHz\n", freq
);
449 case WM8804_TX_CLKSRC_PLL
:
450 snd_soc_update_bits(codec
, WM8804_PLL6
, 0x80, 0);
452 case WM8804_CLKOUT_SRC_CLK1
:
453 snd_soc_update_bits(codec
, WM8804_PLL6
, 0x8, 0);
455 case WM8804_CLKOUT_SRC_OSCCLK
:
456 snd_soc_update_bits(codec
, WM8804_PLL6
, 0x8, 0x8);
459 dev_err(dai
->dev
, "Unknown clock source: %d\n", clk_id
);
466 static int wm8804_set_clkdiv(struct snd_soc_dai
*dai
,
469 struct snd_soc_codec
*codec
;
473 case WM8804_CLKOUT_DIV
:
474 snd_soc_update_bits(codec
, WM8804_PLL5
, 0x30,
478 dev_err(dai
->dev
, "Unknown clock divider: %d\n", div_id
);
484 static void wm8804_sync_cache(struct snd_soc_codec
*codec
)
489 if (!codec
->cache_sync
)
492 codec
->cache_only
= 0;
493 cache
= codec
->reg_cache
;
494 for (i
= 0; i
< codec
->driver
->reg_cache_size
; i
++) {
495 if (i
== WM8804_RST_DEVID1
|| cache
[i
] == wm8804_reg_defs
[i
])
497 snd_soc_write(codec
, i
, cache
[i
]);
499 codec
->cache_sync
= 0;
502 static int wm8804_set_bias_level(struct snd_soc_codec
*codec
,
503 enum snd_soc_bias_level level
)
506 struct wm8804_priv
*wm8804
;
508 wm8804
= snd_soc_codec_get_drvdata(codec
);
510 case SND_SOC_BIAS_ON
:
512 case SND_SOC_BIAS_PREPARE
:
513 /* power up the OSC and the PLL */
514 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x9, 0);
516 case SND_SOC_BIAS_STANDBY
:
517 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_OFF
) {
518 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8804
->supplies
),
522 "Failed to enable supplies: %d\n",
526 wm8804_sync_cache(codec
);
528 /* power down the OSC and the PLL */
529 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x9, 0x9);
531 case SND_SOC_BIAS_OFF
:
532 /* power down the OSC and the PLL */
533 snd_soc_update_bits(codec
, WM8804_PWRDN
, 0x9, 0x9);
534 regulator_bulk_disable(ARRAY_SIZE(wm8804
->supplies
),
539 codec
->dapm
.bias_level
= level
;
544 static int wm8804_suspend(struct snd_soc_codec
*codec
, pm_message_t state
)
546 wm8804_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
550 static int wm8804_resume(struct snd_soc_codec
*codec
)
552 wm8804_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
556 #define wm8804_suspend NULL
557 #define wm8804_resume NULL
560 static int wm8804_remove(struct snd_soc_codec
*codec
)
562 struct wm8804_priv
*wm8804
;
565 wm8804
= snd_soc_codec_get_drvdata(codec
);
566 wm8804_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
568 for (i
= 0; i
< ARRAY_SIZE(wm8804
->supplies
); ++i
)
569 regulator_unregister_notifier(wm8804
->supplies
[i
].consumer
,
570 &wm8804
->disable_nb
[i
]);
571 regulator_bulk_free(ARRAY_SIZE(wm8804
->supplies
), wm8804
->supplies
);
575 static int wm8804_probe(struct snd_soc_codec
*codec
)
577 struct wm8804_priv
*wm8804
;
578 int i
, id1
, id2
, ret
;
580 wm8804
= snd_soc_codec_get_drvdata(codec
);
581 wm8804
->codec
= codec
;
583 codec
->dapm
.idle_bias_off
= 1;
585 ret
= snd_soc_codec_set_cache_io(codec
, 8, 8, wm8804
->control_type
);
587 dev_err(codec
->dev
, "Failed to set cache i/o: %d\n", ret
);
591 for (i
= 0; i
< ARRAY_SIZE(wm8804
->supplies
); i
++)
592 wm8804
->supplies
[i
].supply
= wm8804_supply_names
[i
];
594 ret
= regulator_bulk_get(codec
->dev
, ARRAY_SIZE(wm8804
->supplies
),
597 dev_err(codec
->dev
, "Failed to request supplies: %d\n", ret
);
601 wm8804
->disable_nb
[0].notifier_call
= wm8804_regulator_event_0
;
602 wm8804
->disable_nb
[1].notifier_call
= wm8804_regulator_event_1
;
604 /* This should really be moved into the regulator core */
605 for (i
= 0; i
< ARRAY_SIZE(wm8804
->supplies
); i
++) {
606 ret
= regulator_register_notifier(wm8804
->supplies
[i
].consumer
,
607 &wm8804
->disable_nb
[i
]);
610 "Failed to register regulator notifier: %d\n",
615 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8804
->supplies
),
618 dev_err(codec
->dev
, "Failed to enable supplies: %d\n", ret
);
622 id1
= snd_soc_read(codec
, WM8804_RST_DEVID1
);
624 dev_err(codec
->dev
, "Failed to read device ID: %d\n", id1
);
629 id2
= snd_soc_read(codec
, WM8804_DEVID2
);
631 dev_err(codec
->dev
, "Failed to read device ID: %d\n", id2
);
636 id2
= (id2
<< 8) | id1
;
638 if (id2
!= ((wm8804_reg_defs
[WM8804_DEVID2
] << 8)
639 | wm8804_reg_defs
[WM8804_RST_DEVID1
])) {
640 dev_err(codec
->dev
, "Invalid device ID: %#x\n", id2
);
645 ret
= snd_soc_read(codec
, WM8804_DEVREV
);
647 dev_err(codec
->dev
, "Failed to read device revision: %d\n",
651 dev_info(codec
->dev
, "revision %c\n", ret
+ 'A');
653 ret
= wm8804_reset(codec
);
655 dev_err(codec
->dev
, "Failed to issue reset: %d\n", ret
);
659 wm8804_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
661 snd_soc_add_controls(codec
, wm8804_snd_controls
,
662 ARRAY_SIZE(wm8804_snd_controls
));
666 regulator_bulk_disable(ARRAY_SIZE(wm8804
->supplies
), wm8804
->supplies
);
668 regulator_bulk_free(ARRAY_SIZE(wm8804
->supplies
), wm8804
->supplies
);
672 static struct snd_soc_dai_ops wm8804_dai_ops
= {
673 .hw_params
= wm8804_hw_params
,
674 .set_fmt
= wm8804_set_fmt
,
675 .set_sysclk
= wm8804_set_sysclk
,
676 .set_clkdiv
= wm8804_set_clkdiv
,
677 .set_pll
= wm8804_set_pll
680 #define WM8804_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
681 SNDRV_PCM_FMTBIT_S24_LE)
683 static struct snd_soc_dai_driver wm8804_dai
= {
684 .name
= "wm8804-spdif",
686 .stream_name
= "Playback",
689 .rates
= SNDRV_PCM_RATE_8000_192000
,
690 .formats
= WM8804_FORMATS
,
693 .stream_name
= "Capture",
696 .rates
= SNDRV_PCM_RATE_8000_192000
,
697 .formats
= WM8804_FORMATS
,
699 .ops
= &wm8804_dai_ops
,
703 static struct snd_soc_codec_driver soc_codec_dev_wm8804
= {
704 .probe
= wm8804_probe
,
705 .remove
= wm8804_remove
,
706 .suspend
= wm8804_suspend
,
707 .resume
= wm8804_resume
,
708 .set_bias_level
= wm8804_set_bias_level
,
709 .reg_cache_size
= ARRAY_SIZE(wm8804_reg_defs
),
710 .reg_word_size
= sizeof(u8
),
711 .reg_cache_default
= wm8804_reg_defs
,
712 .volatile_register
= wm8804_volatile
715 #if defined(CONFIG_SPI_MASTER)
716 static int __devinit
wm8804_spi_probe(struct spi_device
*spi
)
718 struct wm8804_priv
*wm8804
;
721 wm8804
= kzalloc(sizeof *wm8804
, GFP_KERNEL
);
725 wm8804
->control_type
= SND_SOC_SPI
;
726 spi_set_drvdata(spi
, wm8804
);
728 ret
= snd_soc_register_codec(&spi
->dev
,
729 &soc_codec_dev_wm8804
, &wm8804_dai
, 1);
735 static int __devexit
wm8804_spi_remove(struct spi_device
*spi
)
737 snd_soc_unregister_codec(&spi
->dev
);
738 kfree(spi_get_drvdata(spi
));
742 static struct spi_driver wm8804_spi_driver
= {
745 .owner
= THIS_MODULE
,
747 .probe
= wm8804_spi_probe
,
748 .remove
= __devexit_p(wm8804_spi_remove
)
752 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
753 static __devinit
int wm8804_i2c_probe(struct i2c_client
*i2c
,
754 const struct i2c_device_id
*id
)
756 struct wm8804_priv
*wm8804
;
759 wm8804
= kzalloc(sizeof *wm8804
, GFP_KERNEL
);
763 wm8804
->control_type
= SND_SOC_I2C
;
764 i2c_set_clientdata(i2c
, wm8804
);
766 ret
= snd_soc_register_codec(&i2c
->dev
,
767 &soc_codec_dev_wm8804
, &wm8804_dai
, 1);
773 static __devexit
int wm8804_i2c_remove(struct i2c_client
*client
)
775 snd_soc_unregister_codec(&client
->dev
);
776 kfree(i2c_get_clientdata(client
));
780 static const struct i2c_device_id wm8804_i2c_id
[] = {
784 MODULE_DEVICE_TABLE(i2c
, wm8804_i2c_id
);
786 static struct i2c_driver wm8804_i2c_driver
= {
789 .owner
= THIS_MODULE
,
791 .probe
= wm8804_i2c_probe
,
792 .remove
= __devexit_p(wm8804_i2c_remove
),
793 .id_table
= wm8804_i2c_id
797 static int __init
wm8804_modinit(void)
801 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
802 ret
= i2c_add_driver(&wm8804_i2c_driver
);
804 printk(KERN_ERR
"Failed to register wm8804 I2C driver: %d\n",
808 #if defined(CONFIG_SPI_MASTER)
809 ret
= spi_register_driver(&wm8804_spi_driver
);
811 printk(KERN_ERR
"Failed to register wm8804 SPI driver: %d\n",
817 module_init(wm8804_modinit
);
819 static void __exit
wm8804_exit(void)
821 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
822 i2c_del_driver(&wm8804_i2c_driver
);
824 #if defined(CONFIG_SPI_MASTER)
825 spi_unregister_driver(&wm8804_spi_driver
);
828 module_exit(wm8804_exit
);
830 MODULE_DESCRIPTION("ASoC WM8804 driver");
831 MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
832 MODULE_LICENSE("GPL");