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/of_device.h>
20
#include <linux/spi/spi.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>
32
#define WM8804_NUM_SUPPLIES 2
33
static const char *wm8804_supply_names[WM8804_NUM_SUPPLIES] = {
38
static const u8 wm8804_reg_defs[] = {
39
0x05, /* R0 - RST/DEVID1 */
40
0x88, /* R1 - DEVID2 */
41
0x04, /* R2 - DEVREV */
48
0xFF, /* R9 - SPDMODE */
49
0x00, /* R10 - INTMASK */
50
0x00, /* R11 - INTSTAT */
51
0x00, /* R12 - SPDSTAT */
52
0x00, /* R13 - RXCHAN1 */
53
0x00, /* R14 - RXCHAN2 */
54
0x00, /* R15 - RXCHAN3 */
55
0x00, /* R16 - RXCHAN4 */
56
0x00, /* R17 - RXCHAN5 */
57
0x00, /* R18 - SPDTX1 */
58
0x00, /* R19 - SPDTX2 */
59
0x00, /* R20 - SPDTX3 */
60
0x71, /* R21 - SPDTX4 */
61
0x0B, /* R22 - SPDTX5 */
62
0x70, /* R23 - GPO0 */
63
0x57, /* R24 - GPO1 */
65
0x42, /* R26 - GPO2 */
66
0x06, /* R27 - AIFTX */
67
0x06, /* R28 - AIFRX */
68
0x80, /* R29 - SPDRX1 */
69
0x07, /* R30 - PWRDN */
73
enum snd_soc_control_type control_type;
74
struct regulator_bulk_data supplies[WM8804_NUM_SUPPLIES];
75
struct notifier_block disable_nb[WM8804_NUM_SUPPLIES];
76
struct snd_soc_codec *codec;
79
static int txsrc_get(struct snd_kcontrol *kcontrol,
80
struct snd_ctl_elem_value *ucontrol);
82
static int txsrc_put(struct snd_kcontrol *kcontrol,
83
struct snd_ctl_elem_value *ucontrol);
86
* We can't use the same notifier block for more than one supply and
87
* there's no way I can see to get from a callback to the caller
88
* except container_of().
90
#define WM8804_REGULATOR_EVENT(n) \
91
static int wm8804_regulator_event_##n(struct notifier_block *nb, \
92
unsigned long event, void *data) \
94
struct wm8804_priv *wm8804 = container_of(nb, struct wm8804_priv, \
96
if (event & REGULATOR_EVENT_DISABLE) { \
97
wm8804->codec->cache_sync = 1; \
102
WM8804_REGULATOR_EVENT(0)
103
WM8804_REGULATOR_EVENT(1)
105
static const char *txsrc_text[] = { "S/PDIF RX", "AIF" };
106
static const SOC_ENUM_SINGLE_EXT_DECL(txsrc, txsrc_text);
108
static const struct snd_kcontrol_new wm8804_snd_controls[] = {
109
SOC_ENUM_EXT("Input Source", txsrc, txsrc_get, txsrc_put),
110
SOC_SINGLE("TX Playback Switch", WM8804_PWRDN, 2, 1, 1),
111
SOC_SINGLE("AIF Playback Switch", WM8804_PWRDN, 4, 1, 1)
114
static int txsrc_get(struct snd_kcontrol *kcontrol,
115
struct snd_ctl_elem_value *ucontrol)
117
struct snd_soc_codec *codec;
120
codec = snd_kcontrol_chip(kcontrol);
121
src = snd_soc_read(codec, WM8804_SPDTX4);
123
ucontrol->value.integer.value[0] = 1;
125
ucontrol->value.integer.value[0] = 0;
130
static int txsrc_put(struct snd_kcontrol *kcontrol,
131
struct snd_ctl_elem_value *ucontrol)
133
struct snd_soc_codec *codec;
134
unsigned int src, txpwr;
136
codec = snd_kcontrol_chip(kcontrol);
138
if (ucontrol->value.integer.value[0] != 0
139
&& ucontrol->value.integer.value[0] != 1)
142
src = snd_soc_read(codec, WM8804_SPDTX4);
143
switch ((src & 0x40) >> 6) {
145
if (!ucontrol->value.integer.value[0])
149
if (ucontrol->value.integer.value[1])
154
/* save the current power state of the transmitter */
155
txpwr = snd_soc_read(codec, WM8804_PWRDN) & 0x4;
156
/* power down the transmitter */
157
snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x4);
158
/* set the tx source */
159
snd_soc_update_bits(codec, WM8804_SPDTX4, 0x40,
160
ucontrol->value.integer.value[0] << 6);
162
if (ucontrol->value.integer.value[0]) {
163
/* power down the receiver */
164
snd_soc_update_bits(codec, WM8804_PWRDN, 0x2, 0x2);
165
/* power up the AIF */
166
snd_soc_update_bits(codec, WM8804_PWRDN, 0x10, 0);
168
/* don't power down the AIF -- may be used as an output */
169
/* power up the receiver */
170
snd_soc_update_bits(codec, WM8804_PWRDN, 0x2, 0);
173
/* restore the transmitter's configuration */
174
snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, txpwr);
179
static int wm8804_volatile(struct snd_soc_codec *codec, unsigned int reg)
182
case WM8804_RST_DEVID1:
200
static int wm8804_reset(struct snd_soc_codec *codec)
202
return snd_soc_write(codec, WM8804_RST_DEVID1, 0x0);
205
static int wm8804_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
207
struct snd_soc_codec *codec;
208
u16 format, master, bcp, lrp;
212
switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
213
case SND_SOC_DAIFMT_I2S:
216
case SND_SOC_DAIFMT_RIGHT_J:
219
case SND_SOC_DAIFMT_LEFT_J:
222
case SND_SOC_DAIFMT_DSP_A:
223
case SND_SOC_DAIFMT_DSP_B:
227
dev_err(dai->dev, "Unknown dai format\n");
231
/* set data format */
232
snd_soc_update_bits(codec, WM8804_AIFTX, 0x3, format);
233
snd_soc_update_bits(codec, WM8804_AIFRX, 0x3, format);
235
switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
236
case SND_SOC_DAIFMT_CBM_CFM:
239
case SND_SOC_DAIFMT_CBS_CFS:
243
dev_err(dai->dev, "Unknown master/slave configuration\n");
247
/* set master/slave mode */
248
snd_soc_update_bits(codec, WM8804_AIFRX, 0x40, master << 6);
251
switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
252
case SND_SOC_DAIFMT_NB_NF:
254
case SND_SOC_DAIFMT_IB_IF:
257
case SND_SOC_DAIFMT_IB_NF:
260
case SND_SOC_DAIFMT_NB_IF:
264
dev_err(dai->dev, "Unknown polarity configuration\n");
268
/* set frame inversion */
269
snd_soc_update_bits(codec, WM8804_AIFTX, 0x10 | 0x20,
270
(bcp << 4) | (lrp << 5));
271
snd_soc_update_bits(codec, WM8804_AIFRX, 0x10 | 0x20,
272
(bcp << 4) | (lrp << 5));
276
static int wm8804_hw_params(struct snd_pcm_substream *substream,
277
struct snd_pcm_hw_params *params,
278
struct snd_soc_dai *dai)
280
struct snd_soc_codec *codec;
285
switch (params_format(params)) {
286
case SNDRV_PCM_FORMAT_S16_LE:
289
case SNDRV_PCM_FORMAT_S20_3LE:
292
case SNDRV_PCM_FORMAT_S24_LE:
296
dev_err(dai->dev, "Unsupported word length: %u\n",
297
params_format(params));
301
/* set word length */
302
snd_soc_update_bits(codec, WM8804_AIFTX, 0xc, blen << 2);
303
snd_soc_update_bits(codec, WM8804_AIFRX, 0xc, blen << 2);
316
/* PLL rate to output rate divisions */
319
unsigned int freqmode;
320
unsigned int mclkdiv;
332
#define FIXED_PLL_SIZE ((1ULL << 22) * 10)
333
static int pll_factors(struct pll_div *pll_div, unsigned int target,
337
unsigned long int K, Ndiv, Nmod, tmp;
341
* Scale the output frequency up; the PLL should run in the
342
* region of 90-100MHz.
344
for (i = 0; i < ARRAY_SIZE(post_table); i++) {
345
tmp = target * post_table[i].div;
346
if (tmp >= 90000000 && tmp <= 100000000) {
347
pll_div->freqmode = post_table[i].freqmode;
348
pll_div->mclkdiv = post_table[i].mclkdiv;
349
target *= post_table[i].div;
354
if (i == ARRAY_SIZE(post_table)) {
355
pr_err("%s: Unable to scale output frequency: %uHz\n",
360
pll_div->prescale = 0;
361
Ndiv = target / source;
364
pll_div->prescale = 1;
365
Ndiv = target / source;
368
if (Ndiv < 5 || Ndiv > 13) {
369
pr_err("%s: WM8804 N value is not within the recommended range: %lu\n",
375
Nmod = target % source;
376
Kpart = FIXED_PLL_SIZE * (u64)Nmod;
378
do_div(Kpart, source);
380
K = Kpart & 0xffffffff;
389
static int wm8804_set_pll(struct snd_soc_dai *dai, int pll_id,
390
int source, unsigned int freq_in,
391
unsigned int freq_out)
393
struct snd_soc_codec *codec;
396
if (!freq_in || !freq_out) {
397
/* disable the PLL */
398
snd_soc_update_bits(codec, WM8804_PWRDN, 0x1, 0x1);
402
struct pll_div pll_div;
404
ret = pll_factors(&pll_div, freq_out, freq_in);
408
/* power down the PLL before reprogramming it */
409
snd_soc_update_bits(codec, WM8804_PWRDN, 0x1, 0x1);
411
if (!freq_in || !freq_out)
414
/* set PLLN and PRESCALE */
415
snd_soc_update_bits(codec, WM8804_PLL4, 0xf | 0x10,
416
pll_div.n | (pll_div.prescale << 4));
417
/* set mclkdiv and freqmode */
418
snd_soc_update_bits(codec, WM8804_PLL5, 0x3 | 0x8,
419
pll_div.freqmode | (pll_div.mclkdiv << 3));
421
snd_soc_write(codec, WM8804_PLL1, pll_div.k & 0xff);
422
snd_soc_write(codec, WM8804_PLL2, (pll_div.k >> 8) & 0xff);
423
snd_soc_write(codec, WM8804_PLL3, pll_div.k >> 16);
425
/* power up the PLL */
426
snd_soc_update_bits(codec, WM8804_PWRDN, 0x1, 0);
432
static int wm8804_set_sysclk(struct snd_soc_dai *dai,
433
int clk_id, unsigned int freq, int dir)
435
struct snd_soc_codec *codec;
440
case WM8804_TX_CLKSRC_MCLK:
441
if ((freq >= 10000000 && freq <= 14400000)
442
|| (freq >= 16280000 && freq <= 27000000))
443
snd_soc_update_bits(codec, WM8804_PLL6, 0x80, 0x80);
445
dev_err(dai->dev, "OSCCLOCK is not within the "
446
"recommended range: %uHz\n", freq);
450
case WM8804_TX_CLKSRC_PLL:
451
snd_soc_update_bits(codec, WM8804_PLL6, 0x80, 0);
453
case WM8804_CLKOUT_SRC_CLK1:
454
snd_soc_update_bits(codec, WM8804_PLL6, 0x8, 0);
456
case WM8804_CLKOUT_SRC_OSCCLK:
457
snd_soc_update_bits(codec, WM8804_PLL6, 0x8, 0x8);
460
dev_err(dai->dev, "Unknown clock source: %d\n", clk_id);
467
static int wm8804_set_clkdiv(struct snd_soc_dai *dai,
470
struct snd_soc_codec *codec;
474
case WM8804_CLKOUT_DIV:
475
snd_soc_update_bits(codec, WM8804_PLL5, 0x30,
479
dev_err(dai->dev, "Unknown clock divider: %d\n", div_id);
485
static void wm8804_sync_cache(struct snd_soc_codec *codec)
490
if (!codec->cache_sync)
493
codec->cache_only = 0;
494
cache = codec->reg_cache;
495
for (i = 0; i < codec->driver->reg_cache_size; i++) {
496
if (i == WM8804_RST_DEVID1 || cache[i] == wm8804_reg_defs[i])
498
snd_soc_write(codec, i, cache[i]);
500
codec->cache_sync = 0;
503
static int wm8804_set_bias_level(struct snd_soc_codec *codec,
504
enum snd_soc_bias_level level)
507
struct wm8804_priv *wm8804;
509
wm8804 = snd_soc_codec_get_drvdata(codec);
511
case SND_SOC_BIAS_ON:
513
case SND_SOC_BIAS_PREPARE:
514
/* power up the OSC and the PLL */
515
snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
517
case SND_SOC_BIAS_STANDBY:
518
if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
519
ret = regulator_bulk_enable(ARRAY_SIZE(wm8804->supplies),
523
"Failed to enable supplies: %d\n",
527
wm8804_sync_cache(codec);
529
/* power down the OSC and the PLL */
530
snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0x9);
532
case SND_SOC_BIAS_OFF:
533
/* power down the OSC and the PLL */
534
snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0x9);
535
regulator_bulk_disable(ARRAY_SIZE(wm8804->supplies),
540
codec->dapm.bias_level = level;
545
static int wm8804_suspend(struct snd_soc_codec *codec, pm_message_t state)
547
wm8804_set_bias_level(codec, SND_SOC_BIAS_OFF);
551
static int wm8804_resume(struct snd_soc_codec *codec)
553
wm8804_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
557
#define wm8804_suspend NULL
558
#define wm8804_resume NULL
561
static int wm8804_remove(struct snd_soc_codec *codec)
563
struct wm8804_priv *wm8804;
566
wm8804 = snd_soc_codec_get_drvdata(codec);
567
wm8804_set_bias_level(codec, SND_SOC_BIAS_OFF);
569
for (i = 0; i < ARRAY_SIZE(wm8804->supplies); ++i)
570
regulator_unregister_notifier(wm8804->supplies[i].consumer,
571
&wm8804->disable_nb[i]);
572
regulator_bulk_free(ARRAY_SIZE(wm8804->supplies), wm8804->supplies);
576
static int wm8804_probe(struct snd_soc_codec *codec)
578
struct wm8804_priv *wm8804;
579
int i, id1, id2, ret;
581
wm8804 = snd_soc_codec_get_drvdata(codec);
582
wm8804->codec = codec;
584
codec->dapm.idle_bias_off = 1;
586
ret = snd_soc_codec_set_cache_io(codec, 8, 8, wm8804->control_type);
588
dev_err(codec->dev, "Failed to set cache i/o: %d\n", ret);
592
for (i = 0; i < ARRAY_SIZE(wm8804->supplies); i++)
593
wm8804->supplies[i].supply = wm8804_supply_names[i];
595
ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8804->supplies),
598
dev_err(codec->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
ret = regulator_register_notifier(wm8804->supplies[i].consumer,
608
&wm8804->disable_nb[i]);
611
"Failed to register regulator notifier: %d\n",
616
ret = regulator_bulk_enable(ARRAY_SIZE(wm8804->supplies),
619
dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
623
id1 = snd_soc_read(codec, WM8804_RST_DEVID1);
625
dev_err(codec->dev, "Failed to read device ID: %d\n", id1);
630
id2 = snd_soc_read(codec, WM8804_DEVID2);
632
dev_err(codec->dev, "Failed to read device ID: %d\n", id2);
637
id2 = (id2 << 8) | id1;
639
if (id2 != ((wm8804_reg_defs[WM8804_DEVID2] << 8)
640
| wm8804_reg_defs[WM8804_RST_DEVID1])) {
641
dev_err(codec->dev, "Invalid device ID: %#x\n", id2);
646
ret = snd_soc_read(codec, WM8804_DEVREV);
648
dev_err(codec->dev, "Failed to read device revision: %d\n",
652
dev_info(codec->dev, "revision %c\n", ret + 'A');
654
ret = wm8804_reset(codec);
656
dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
660
wm8804_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
662
snd_soc_add_controls(codec, wm8804_snd_controls,
663
ARRAY_SIZE(wm8804_snd_controls));
667
regulator_bulk_disable(ARRAY_SIZE(wm8804->supplies), wm8804->supplies);
669
regulator_bulk_free(ARRAY_SIZE(wm8804->supplies), wm8804->supplies);
673
static struct snd_soc_dai_ops wm8804_dai_ops = {
674
.hw_params = wm8804_hw_params,
675
.set_fmt = wm8804_set_fmt,
676
.set_sysclk = wm8804_set_sysclk,
677
.set_clkdiv = wm8804_set_clkdiv,
678
.set_pll = wm8804_set_pll
681
#define WM8804_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
682
SNDRV_PCM_FMTBIT_S24_LE)
684
#define WM8804_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
685
SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
686
SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | \
687
SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000)
689
static struct snd_soc_dai_driver wm8804_dai = {
690
.name = "wm8804-spdif",
692
.stream_name = "Playback",
695
.rates = WM8804_RATES,
696
.formats = WM8804_FORMATS,
699
.stream_name = "Capture",
702
.rates = WM8804_RATES,
703
.formats = WM8804_FORMATS,
705
.ops = &wm8804_dai_ops,
709
static struct snd_soc_codec_driver soc_codec_dev_wm8804 = {
710
.probe = wm8804_probe,
711
.remove = wm8804_remove,
712
.suspend = wm8804_suspend,
713
.resume = wm8804_resume,
714
.set_bias_level = wm8804_set_bias_level,
715
.reg_cache_size = ARRAY_SIZE(wm8804_reg_defs),
716
.reg_word_size = sizeof(u8),
717
.reg_cache_default = wm8804_reg_defs,
718
.volatile_register = wm8804_volatile
721
static const struct of_device_id wm8804_of_match[] = {
722
{ .compatible = "wlf,wm8804", },
725
MODULE_DEVICE_TABLE(of, wm8804_of_match);
727
#if defined(CONFIG_SPI_MASTER)
728
static int __devinit wm8804_spi_probe(struct spi_device *spi)
730
struct wm8804_priv *wm8804;
733
wm8804 = kzalloc(sizeof *wm8804, GFP_KERNEL);
737
wm8804->control_type = SND_SOC_SPI;
738
spi_set_drvdata(spi, wm8804);
740
ret = snd_soc_register_codec(&spi->dev,
741
&soc_codec_dev_wm8804, &wm8804_dai, 1);
747
static int __devexit wm8804_spi_remove(struct spi_device *spi)
749
snd_soc_unregister_codec(&spi->dev);
750
kfree(spi_get_drvdata(spi));
754
static struct spi_driver wm8804_spi_driver = {
757
.owner = THIS_MODULE,
758
.of_match_table = wm8804_of_match,
760
.probe = wm8804_spi_probe,
761
.remove = __devexit_p(wm8804_spi_remove)
765
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
766
static __devinit int wm8804_i2c_probe(struct i2c_client *i2c,
767
const struct i2c_device_id *id)
769
struct wm8804_priv *wm8804;
772
wm8804 = kzalloc(sizeof *wm8804, GFP_KERNEL);
776
wm8804->control_type = SND_SOC_I2C;
777
i2c_set_clientdata(i2c, wm8804);
779
ret = snd_soc_register_codec(&i2c->dev,
780
&soc_codec_dev_wm8804, &wm8804_dai, 1);
786
static __devexit int wm8804_i2c_remove(struct i2c_client *client)
788
snd_soc_unregister_codec(&client->dev);
789
kfree(i2c_get_clientdata(client));
793
static const struct i2c_device_id wm8804_i2c_id[] = {
797
MODULE_DEVICE_TABLE(i2c, wm8804_i2c_id);
799
static struct i2c_driver wm8804_i2c_driver = {
802
.owner = THIS_MODULE,
803
.of_match_table = wm8804_of_match,
805
.probe = wm8804_i2c_probe,
806
.remove = __devexit_p(wm8804_i2c_remove),
807
.id_table = wm8804_i2c_id
811
static int __init wm8804_modinit(void)
815
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
816
ret = i2c_add_driver(&wm8804_i2c_driver);
818
printk(KERN_ERR "Failed to register wm8804 I2C driver: %d\n",
822
#if defined(CONFIG_SPI_MASTER)
823
ret = spi_register_driver(&wm8804_spi_driver);
825
printk(KERN_ERR "Failed to register wm8804 SPI driver: %d\n",
831
module_init(wm8804_modinit);
833
static void __exit wm8804_exit(void)
835
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
836
i2c_del_driver(&wm8804_i2c_driver);
838
#if defined(CONFIG_SPI_MASTER)
839
spi_unregister_driver(&wm8804_spi_driver);
842
module_exit(wm8804_exit);
844
MODULE_DESCRIPTION("ASoC WM8804 driver");
845
MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>");
846
MODULE_LICENSE("GPL");