2 * wm8776.c -- WM8776 ALSA SoC Audio driver
4 * Copyright 2009 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@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.
12 * TODO: Input ALC/limiter support
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
20 #include <linux/i2c.h>
21 #include <linux/platform_device.h>
22 #include <linux/spi/spi.h>
23 #include <linux/slab.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
29 #include <sound/initval.h>
30 #include <sound/tlv.h>
34 static struct snd_soc_codec
*wm8776_codec
;
35 struct snd_soc_codec_device soc_codec_dev_wm8776
;
37 /* codec private data */
39 struct snd_soc_codec codec
;
40 u16 reg_cache
[WM8776_CACHEREGNUM
];
44 #ifdef CONFIG_SPI_MASTER
45 static int wm8776_spi_write(struct spi_device
*spi
, const char *data
, int len
);
48 static const u16 wm8776_reg
[WM8776_CACHEREGNUM
] = {
49 0x79, 0x79, 0x79, 0xff, 0xff, /* 4 */
50 0xff, 0x00, 0x90, 0x00, 0x00, /* 9 */
51 0x22, 0x22, 0x22, 0x08, 0xcf, /* 14 */
52 0xcf, 0x7b, 0x00, 0x32, 0x00, /* 19 */
56 static int wm8776_reset(struct snd_soc_codec
*codec
)
58 return snd_soc_write(codec
, WM8776_RESET
, 0);
61 static const DECLARE_TLV_DB_SCALE(hp_tlv
, -12100, 100, 1);
62 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12750, 50, 1);
63 static const DECLARE_TLV_DB_SCALE(adc_tlv
, -10350, 50, 1);
65 static const struct snd_kcontrol_new wm8776_snd_controls
[] = {
66 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL
, WM8776_HPRVOL
,
68 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL
, WM8776_DACRVOL
,
70 SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1
, 0, 1, 0),
72 SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2
, 0, 1, 0),
74 SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL
, WM8776_ADCRVOL
,
76 SOC_DOUBLE("Capture Switch", WM8776_ADCMUX
, 7, 6, 1, 1),
77 SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL
, WM8776_ADCRVOL
, 8, 1, 0),
78 SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL
, 8, 1, 1),
81 static const struct snd_kcontrol_new inmix_controls
[] = {
82 SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX
, 0, 1, 0),
83 SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX
, 1, 1, 0),
84 SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX
, 2, 1, 0),
85 SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX
, 3, 1, 0),
86 SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX
, 4, 1, 0),
89 static const struct snd_kcontrol_new outmix_controls
[] = {
90 SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX
, 0, 1, 0),
91 SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX
, 1, 1, 0),
92 SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX
, 2, 1, 0),
95 static const struct snd_soc_dapm_widget wm8776_dapm_widgets
[] = {
96 SND_SOC_DAPM_INPUT("AUX"),
97 SND_SOC_DAPM_INPUT("AUX"),
99 SND_SOC_DAPM_INPUT("AIN1"),
100 SND_SOC_DAPM_INPUT("AIN2"),
101 SND_SOC_DAPM_INPUT("AIN3"),
102 SND_SOC_DAPM_INPUT("AIN4"),
103 SND_SOC_DAPM_INPUT("AIN5"),
105 SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN
, 6, 1,
106 inmix_controls
, ARRAY_SIZE(inmix_controls
)),
108 SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN
, 1, 1),
109 SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN
, 2, 1),
111 SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM
, 0, 0,
112 outmix_controls
, ARRAY_SIZE(outmix_controls
)),
114 SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN
, 3, 1, NULL
, 0),
116 SND_SOC_DAPM_OUTPUT("VOUT"),
118 SND_SOC_DAPM_OUTPUT("HPOUTL"),
119 SND_SOC_DAPM_OUTPUT("HPOUTR"),
122 static const struct snd_soc_dapm_route routes
[] = {
123 { "Input Mixer", "AIN1 Switch", "AIN1" },
124 { "Input Mixer", "AIN2 Switch", "AIN2" },
125 { "Input Mixer", "AIN3 Switch", "AIN3" },
126 { "Input Mixer", "AIN4 Switch", "AIN4" },
127 { "Input Mixer", "AIN5 Switch", "AIN5" },
129 { "ADC", NULL
, "Input Mixer" },
131 { "Output Mixer", "DAC Switch", "DAC" },
132 { "Output Mixer", "AUX Switch", "AUX" },
133 { "Output Mixer", "Bypass Switch", "Input Mixer" },
135 { "VOUT", NULL
, "Output Mixer" },
137 { "Headphone PGA", NULL
, "Output Mixer" },
139 { "HPOUTL", NULL
, "Headphone PGA" },
140 { "HPOUTR", NULL
, "Headphone PGA" },
143 static int wm8776_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
145 struct snd_soc_codec
*codec
= dai
->codec
;
146 int reg
, iface
, master
;
150 reg
= WM8776_DACIFCTRL
;
154 reg
= WM8776_ADCIFCTRL
;
163 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
164 case SND_SOC_DAIFMT_CBM_CFM
:
166 case SND_SOC_DAIFMT_CBS_CFS
:
173 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
174 case SND_SOC_DAIFMT_I2S
:
177 case SND_SOC_DAIFMT_RIGHT_J
:
179 case SND_SOC_DAIFMT_LEFT_J
:
182 /* FIXME: CHECK A/B */
183 case SND_SOC_DAIFMT_DSP_A
:
186 case SND_SOC_DAIFMT_DSP_B
:
193 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
194 case SND_SOC_DAIFMT_NB_NF
:
196 case SND_SOC_DAIFMT_IB_IF
:
199 case SND_SOC_DAIFMT_IB_NF
:
202 case SND_SOC_DAIFMT_NB_IF
:
209 /* Finally, write out the values */
210 snd_soc_update_bits(codec
, reg
, 0xf, iface
);
211 snd_soc_update_bits(codec
, WM8776_MSTRCTRL
, 0x180, master
);
216 static int mclk_ratios
[] = {
225 static int wm8776_hw_params(struct snd_pcm_substream
*substream
,
226 struct snd_pcm_hw_params
*params
,
227 struct snd_soc_dai
*dai
)
229 struct snd_soc_codec
*codec
= dai
->codec
;
230 struct wm8776_priv
*wm8776
= snd_soc_codec_get_drvdata(codec
);
231 int iface_reg
, iface
;
232 int ratio_shift
, master
;
239 iface_reg
= WM8776_DACIFCTRL
;
244 iface_reg
= WM8776_ADCIFCTRL
;
253 /* Set word length */
254 switch (params_format(params
)) {
255 case SNDRV_PCM_FORMAT_S16_LE
:
257 case SNDRV_PCM_FORMAT_S20_3LE
:
260 case SNDRV_PCM_FORMAT_S24_LE
:
263 case SNDRV_PCM_FORMAT_S32_LE
:
268 /* Only need to set MCLK/LRCLK ratio if we're master */
269 if (snd_soc_read(codec
, WM8776_MSTRCTRL
) & master
) {
270 for (i
= 0; i
< ARRAY_SIZE(mclk_ratios
); i
++) {
271 if (wm8776
->sysclk
[dai
->id
] / params_rate(params
)
276 if (i
== ARRAY_SIZE(mclk_ratios
)) {
278 "Unable to configure MCLK ratio %d/%d\n",
279 wm8776
->sysclk
[dai
->id
], params_rate(params
));
283 dev_dbg(codec
->dev
, "MCLK is %dfs\n", mclk_ratios
[i
]);
285 snd_soc_update_bits(codec
, WM8776_MSTRCTRL
,
286 0x7 << ratio_shift
, i
<< ratio_shift
);
288 dev_dbg(codec
->dev
, "DAI in slave mode\n");
291 snd_soc_update_bits(codec
, iface_reg
, 0x30, iface
);
296 static int wm8776_mute(struct snd_soc_dai
*dai
, int mute
)
298 struct snd_soc_codec
*codec
= dai
->codec
;
300 return snd_soc_write(codec
, WM8776_DACMUTE
, !!mute
);
303 static int wm8776_set_sysclk(struct snd_soc_dai
*dai
,
304 int clk_id
, unsigned int freq
, int dir
)
306 struct snd_soc_codec
*codec
= dai
->codec
;
307 struct wm8776_priv
*wm8776
= snd_soc_codec_get_drvdata(codec
);
309 BUG_ON(dai
->id
>= ARRAY_SIZE(wm8776
->sysclk
));
311 wm8776
->sysclk
[dai
->id
] = freq
;
316 static int wm8776_set_bias_level(struct snd_soc_codec
*codec
,
317 enum snd_soc_bias_level level
)
320 case SND_SOC_BIAS_ON
:
322 case SND_SOC_BIAS_PREPARE
:
324 case SND_SOC_BIAS_STANDBY
:
325 if (codec
->bias_level
== SND_SOC_BIAS_OFF
) {
326 /* Disable the global powerdown; DAPM does the rest */
327 snd_soc_update_bits(codec
, WM8776_PWRDOWN
, 1, 0);
331 case SND_SOC_BIAS_OFF
:
332 snd_soc_update_bits(codec
, WM8776_PWRDOWN
, 1, 1);
336 codec
->bias_level
= level
;
340 #define WM8776_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
341 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
342 SNDRV_PCM_RATE_96000)
345 #define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
346 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
348 static struct snd_soc_dai_ops wm8776_dac_ops
= {
349 .digital_mute
= wm8776_mute
,
350 .hw_params
= wm8776_hw_params
,
351 .set_fmt
= wm8776_set_fmt
,
352 .set_sysclk
= wm8776_set_sysclk
,
355 static struct snd_soc_dai_ops wm8776_adc_ops
= {
356 .hw_params
= wm8776_hw_params
,
357 .set_fmt
= wm8776_set_fmt
,
358 .set_sysclk
= wm8776_set_sysclk
,
361 struct snd_soc_dai wm8776_dai
[] = {
363 .name
= "WM8776 Playback",
364 .id
= WM8776_DAI_DAC
,
366 .stream_name
= "Playback",
369 .rates
= WM8776_RATES
,
370 .formats
= WM8776_FORMATS
,
372 .ops
= &wm8776_dac_ops
,
375 .name
= "WM8776 Capture",
376 .id
= WM8776_DAI_ADC
,
378 .stream_name
= "Capture",
381 .rates
= WM8776_RATES
,
382 .formats
= WM8776_FORMATS
,
384 .ops
= &wm8776_adc_ops
,
387 EXPORT_SYMBOL_GPL(wm8776_dai
);
390 static int wm8776_suspend(struct platform_device
*pdev
, pm_message_t state
)
392 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
393 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
395 wm8776_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
400 static int wm8776_resume(struct platform_device
*pdev
)
402 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
403 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
406 u16
*cache
= codec
->reg_cache
;
408 /* Sync reg_cache with the hardware */
409 for (i
= 0; i
< ARRAY_SIZE(wm8776_reg
); i
++) {
410 if (cache
[i
] == wm8776_reg
[i
])
412 data
[0] = (i
<< 1) | ((cache
[i
] >> 8) & 0x0001);
413 data
[1] = cache
[i
] & 0x00ff;
414 codec
->hw_write(codec
->control_data
, data
, 2);
417 wm8776_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
422 #define wm8776_suspend NULL
423 #define wm8776_resume NULL
426 static int wm8776_probe(struct platform_device
*pdev
)
428 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
429 struct snd_soc_codec
*codec
;
432 if (wm8776_codec
== NULL
) {
433 dev_err(&pdev
->dev
, "Codec device not registered\n");
437 socdev
->card
->codec
= wm8776_codec
;
438 codec
= wm8776_codec
;
441 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
443 dev_err(codec
->dev
, "failed to create pcms: %d\n", ret
);
447 snd_soc_add_controls(codec
, wm8776_snd_controls
,
448 ARRAY_SIZE(wm8776_snd_controls
));
449 snd_soc_dapm_new_controls(codec
, wm8776_dapm_widgets
,
450 ARRAY_SIZE(wm8776_dapm_widgets
));
451 snd_soc_dapm_add_routes(codec
, routes
, ARRAY_SIZE(routes
));
459 /* power down chip */
460 static int wm8776_remove(struct platform_device
*pdev
)
462 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
464 snd_soc_free_pcms(socdev
);
465 snd_soc_dapm_free(socdev
);
470 struct snd_soc_codec_device soc_codec_dev_wm8776
= {
471 .probe
= wm8776_probe
,
472 .remove
= wm8776_remove
,
473 .suspend
= wm8776_suspend
,
474 .resume
= wm8776_resume
,
476 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8776
);
478 static int wm8776_register(struct wm8776_priv
*wm8776
,
479 enum snd_soc_control_type control
)
482 struct snd_soc_codec
*codec
= &wm8776
->codec
;
485 dev_err(codec
->dev
, "Another WM8776 is registered\n");
490 mutex_init(&codec
->mutex
);
491 INIT_LIST_HEAD(&codec
->dapm_widgets
);
492 INIT_LIST_HEAD(&codec
->dapm_paths
);
494 snd_soc_codec_set_drvdata(codec
, wm8776
);
495 codec
->name
= "WM8776";
496 codec
->owner
= THIS_MODULE
;
497 codec
->bias_level
= SND_SOC_BIAS_OFF
;
498 codec
->set_bias_level
= wm8776_set_bias_level
;
499 codec
->dai
= wm8776_dai
;
500 codec
->num_dai
= ARRAY_SIZE(wm8776_dai
);
501 codec
->reg_cache_size
= WM8776_CACHEREGNUM
;
502 codec
->reg_cache
= &wm8776
->reg_cache
;
504 memcpy(codec
->reg_cache
, wm8776_reg
, sizeof(wm8776_reg
));
506 ret
= snd_soc_codec_set_cache_io(codec
, 7, 9, control
);
508 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
512 for (i
= 0; i
< ARRAY_SIZE(wm8776_dai
); i
++)
513 wm8776_dai
[i
].dev
= codec
->dev
;
515 ret
= wm8776_reset(codec
);
517 dev_err(codec
->dev
, "Failed to issue reset: %d\n", ret
);
521 wm8776_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
523 /* Latch the update bits; right channel only since we always
525 snd_soc_update_bits(codec
, WM8776_HPRVOL
, 0x100, 0x100);
526 snd_soc_update_bits(codec
, WM8776_DACRVOL
, 0x100, 0x100);
528 wm8776_codec
= codec
;
530 ret
= snd_soc_register_codec(codec
);
532 dev_err(codec
->dev
, "Failed to register codec: %d\n", ret
);
536 ret
= snd_soc_register_dais(wm8776_dai
, ARRAY_SIZE(wm8776_dai
));
538 dev_err(codec
->dev
, "Failed to register DAIs: %d\n", ret
);
545 snd_soc_unregister_codec(codec
);
551 static void wm8776_unregister(struct wm8776_priv
*wm8776
)
553 wm8776_set_bias_level(&wm8776
->codec
, SND_SOC_BIAS_OFF
);
554 snd_soc_unregister_dais(wm8776_dai
, ARRAY_SIZE(wm8776_dai
));
555 snd_soc_unregister_codec(&wm8776
->codec
);
560 #if defined(CONFIG_SPI_MASTER)
561 static int wm8776_spi_write(struct spi_device
*spi
, const char *data
, int len
)
563 struct spi_transfer t
;
564 struct spi_message m
;
573 spi_message_init(&m
);
574 memset(&t
, 0, (sizeof t
));
579 spi_message_add_tail(&t
, &m
);
585 static int __devinit
wm8776_spi_probe(struct spi_device
*spi
)
587 struct snd_soc_codec
*codec
;
588 struct wm8776_priv
*wm8776
;
590 wm8776
= kzalloc(sizeof(struct wm8776_priv
), GFP_KERNEL
);
594 codec
= &wm8776
->codec
;
595 codec
->control_data
= spi
;
596 codec
->hw_write
= (hw_write_t
)wm8776_spi_write
;
597 codec
->dev
= &spi
->dev
;
599 dev_set_drvdata(&spi
->dev
, wm8776
);
601 return wm8776_register(wm8776
, SND_SOC_SPI
);
604 static int __devexit
wm8776_spi_remove(struct spi_device
*spi
)
606 struct wm8776_priv
*wm8776
= dev_get_drvdata(&spi
->dev
);
608 wm8776_unregister(wm8776
);
613 static struct spi_driver wm8776_spi_driver
= {
616 .bus
= &spi_bus_type
,
617 .owner
= THIS_MODULE
,
619 .probe
= wm8776_spi_probe
,
620 .remove
= __devexit_p(wm8776_spi_remove
),
622 #endif /* CONFIG_SPI_MASTER */
624 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
625 static __devinit
int wm8776_i2c_probe(struct i2c_client
*i2c
,
626 const struct i2c_device_id
*id
)
628 struct wm8776_priv
*wm8776
;
629 struct snd_soc_codec
*codec
;
631 wm8776
= kzalloc(sizeof(struct wm8776_priv
), GFP_KERNEL
);
635 codec
= &wm8776
->codec
;
636 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
638 i2c_set_clientdata(i2c
, wm8776
);
639 codec
->control_data
= i2c
;
641 codec
->dev
= &i2c
->dev
;
643 return wm8776_register(wm8776
, SND_SOC_I2C
);
646 static __devexit
int wm8776_i2c_remove(struct i2c_client
*client
)
648 struct wm8776_priv
*wm8776
= i2c_get_clientdata(client
);
649 wm8776_unregister(wm8776
);
653 static const struct i2c_device_id wm8776_i2c_id
[] = {
657 MODULE_DEVICE_TABLE(i2c
, wm8776_i2c_id
);
659 static struct i2c_driver wm8776_i2c_driver
= {
662 .owner
= THIS_MODULE
,
664 .probe
= wm8776_i2c_probe
,
665 .remove
= __devexit_p(wm8776_i2c_remove
),
666 .id_table
= wm8776_i2c_id
,
670 static int __init
wm8776_modinit(void)
673 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
674 ret
= i2c_add_driver(&wm8776_i2c_driver
);
676 printk(KERN_ERR
"Failed to register WM8776 I2C driver: %d\n",
680 #if defined(CONFIG_SPI_MASTER)
681 ret
= spi_register_driver(&wm8776_spi_driver
);
683 printk(KERN_ERR
"Failed to register WM8776 SPI driver: %d\n",
689 module_init(wm8776_modinit
);
691 static void __exit
wm8776_exit(void)
693 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
694 i2c_del_driver(&wm8776_i2c_driver
);
696 #if defined(CONFIG_SPI_MASTER)
697 spi_unregister_driver(&wm8776_spi_driver
);
700 module_exit(wm8776_exit
);
702 MODULE_DESCRIPTION("ASoC WM8776 driver");
703 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
704 MODULE_LICENSE("GPL");