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 <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/soc-dapm.h>
28 #include <sound/initval.h>
29 #include <sound/tlv.h>
33 static struct snd_soc_codec
*wm8776_codec
;
34 struct snd_soc_codec_device soc_codec_dev_wm8776
;
36 /* codec private data */
38 struct snd_soc_codec codec
;
39 u16 reg_cache
[WM8776_CACHEREGNUM
];
43 #ifdef CONFIG_SPI_MASTER
44 static int wm8776_spi_write(struct spi_device
*spi
, const char *data
, int len
);
47 static const u16 wm8776_reg
[WM8776_CACHEREGNUM
] = {
48 0x79, 0x79, 0x79, 0xff, 0xff, /* 4 */
49 0xff, 0x00, 0x90, 0x00, 0x00, /* 9 */
50 0x22, 0x22, 0x22, 0x08, 0xcf, /* 14 */
51 0xcf, 0x7b, 0x00, 0x32, 0x00, /* 19 */
55 static int wm8776_reset(struct snd_soc_codec
*codec
)
57 return snd_soc_write(codec
, WM8776_RESET
, 0);
60 static const DECLARE_TLV_DB_SCALE(hp_tlv
, -12100, 100, 1);
61 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12750, 50, 1);
62 static const DECLARE_TLV_DB_SCALE(adc_tlv
, -10350, 50, 1);
64 static const struct snd_kcontrol_new wm8776_snd_controls
[] = {
65 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL
, WM8776_HPRVOL
,
67 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL
, WM8776_DACRVOL
,
69 SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1
, 0, 1, 0),
71 SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2
, 0, 1, 0),
73 SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL
, WM8776_ADCRVOL
,
75 SOC_DOUBLE("Capture Switch", WM8776_ADCMUX
, 7, 6, 1, 1),
76 SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL
, WM8776_ADCRVOL
, 8, 1, 0),
77 SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL
, 8, 1, 1),
80 static const struct snd_kcontrol_new inmix_controls
[] = {
81 SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX
, 0, 1, 0),
82 SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX
, 1, 1, 0),
83 SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX
, 2, 1, 0),
84 SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX
, 3, 1, 0),
85 SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX
, 4, 1, 0),
88 static const struct snd_kcontrol_new outmix_controls
[] = {
89 SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX
, 0, 1, 0),
90 SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX
, 1, 1, 0),
91 SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX
, 2, 1, 0),
94 static const struct snd_soc_dapm_widget wm8776_dapm_widgets
[] = {
95 SND_SOC_DAPM_INPUT("AUX"),
96 SND_SOC_DAPM_INPUT("AUX"),
98 SND_SOC_DAPM_INPUT("AIN1"),
99 SND_SOC_DAPM_INPUT("AIN2"),
100 SND_SOC_DAPM_INPUT("AIN3"),
101 SND_SOC_DAPM_INPUT("AIN4"),
102 SND_SOC_DAPM_INPUT("AIN5"),
104 SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN
, 6, 1,
105 inmix_controls
, ARRAY_SIZE(inmix_controls
)),
107 SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN
, 1, 1),
108 SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN
, 2, 1),
110 SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM
, 0, 0,
111 outmix_controls
, ARRAY_SIZE(outmix_controls
)),
113 SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN
, 3, 1, NULL
, 0),
115 SND_SOC_DAPM_OUTPUT("VOUT"),
117 SND_SOC_DAPM_OUTPUT("HPOUTL"),
118 SND_SOC_DAPM_OUTPUT("HPOUTR"),
121 static const struct snd_soc_dapm_route routes
[] = {
122 { "Input Mixer", "AIN1 Switch", "AIN1" },
123 { "Input Mixer", "AIN2 Switch", "AIN2" },
124 { "Input Mixer", "AIN3 Switch", "AIN3" },
125 { "Input Mixer", "AIN4 Switch", "AIN4" },
126 { "Input Mixer", "AIN5 Switch", "AIN5" },
128 { "ADC", NULL
, "Input Mixer" },
130 { "Output Mixer", "DAC Switch", "DAC" },
131 { "Output Mixer", "AUX Switch", "AUX" },
132 { "Output Mixer", "Bypass Switch", "Input Mixer" },
134 { "VOUT", NULL
, "Output Mixer" },
136 { "Headphone PGA", NULL
, "Output Mixer" },
138 { "HPOUTL", NULL
, "Headphone PGA" },
139 { "HPOUTR", NULL
, "Headphone PGA" },
142 static int wm8776_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
144 struct snd_soc_codec
*codec
= dai
->codec
;
145 int reg
, iface
, master
;
149 reg
= WM8776_DACIFCTRL
;
153 reg
= WM8776_ADCIFCTRL
;
162 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
163 case SND_SOC_DAIFMT_CBM_CFM
:
165 case SND_SOC_DAIFMT_CBS_CFS
:
172 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
173 case SND_SOC_DAIFMT_I2S
:
176 case SND_SOC_DAIFMT_RIGHT_J
:
178 case SND_SOC_DAIFMT_LEFT_J
:
181 /* FIXME: CHECK A/B */
182 case SND_SOC_DAIFMT_DSP_A
:
185 case SND_SOC_DAIFMT_DSP_B
:
192 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
193 case SND_SOC_DAIFMT_NB_NF
:
195 case SND_SOC_DAIFMT_IB_IF
:
198 case SND_SOC_DAIFMT_IB_NF
:
201 case SND_SOC_DAIFMT_NB_IF
:
208 /* Finally, write out the values */
209 snd_soc_update_bits(codec
, reg
, 0xf, iface
);
210 snd_soc_update_bits(codec
, WM8776_MSTRCTRL
, 0x180, master
);
215 static int mclk_ratios
[] = {
224 static int wm8776_hw_params(struct snd_pcm_substream
*substream
,
225 struct snd_pcm_hw_params
*params
,
226 struct snd_soc_dai
*dai
)
228 struct snd_soc_codec
*codec
= dai
->codec
;
229 struct wm8776_priv
*wm8776
= codec
->private_data
;
230 int iface_reg
, iface
;
231 int ratio_shift
, master
;
238 iface_reg
= WM8776_DACIFCTRL
;
243 iface_reg
= WM8776_ADCIFCTRL
;
252 /* Set word length */
253 switch (params_format(params
)) {
254 case SNDRV_PCM_FORMAT_S16_LE
:
256 case SNDRV_PCM_FORMAT_S20_3LE
:
259 case SNDRV_PCM_FORMAT_S24_LE
:
262 case SNDRV_PCM_FORMAT_S32_LE
:
267 /* Only need to set MCLK/LRCLK ratio if we're master */
268 if (snd_soc_read(codec
, WM8776_MSTRCTRL
) & master
) {
269 for (i
= 0; i
< ARRAY_SIZE(mclk_ratios
); i
++) {
270 if (wm8776
->sysclk
[dai
->id
] / params_rate(params
)
275 if (i
== ARRAY_SIZE(mclk_ratios
)) {
277 "Unable to configure MCLK ratio %d/%d\n",
278 wm8776
->sysclk
[dai
->id
], params_rate(params
));
282 dev_dbg(codec
->dev
, "MCLK is %dfs\n", mclk_ratios
[i
]);
284 snd_soc_update_bits(codec
, WM8776_MSTRCTRL
,
285 0x7 << ratio_shift
, i
<< ratio_shift
);
287 dev_dbg(codec
->dev
, "DAI in slave mode\n");
290 snd_soc_update_bits(codec
, iface_reg
, 0x30, iface
);
295 static int wm8776_mute(struct snd_soc_dai
*dai
, int mute
)
297 struct snd_soc_codec
*codec
= dai
->codec
;
299 return snd_soc_write(codec
, WM8776_DACMUTE
, !!mute
);
302 static int wm8776_set_sysclk(struct snd_soc_dai
*dai
,
303 int clk_id
, unsigned int freq
, int dir
)
305 struct snd_soc_codec
*codec
= dai
->codec
;
306 struct wm8776_priv
*wm8776
= codec
->private_data
;
308 BUG_ON(dai
->id
>= ARRAY_SIZE(wm8776
->sysclk
));
310 wm8776
->sysclk
[dai
->id
] = freq
;
315 static int wm8776_set_bias_level(struct snd_soc_codec
*codec
,
316 enum snd_soc_bias_level level
)
319 case SND_SOC_BIAS_ON
:
321 case SND_SOC_BIAS_PREPARE
:
323 case SND_SOC_BIAS_STANDBY
:
324 if (codec
->bias_level
== SND_SOC_BIAS_OFF
) {
325 /* Disable the global powerdown; DAPM does the rest */
326 snd_soc_update_bits(codec
, WM8776_PWRDOWN
, 1, 0);
330 case SND_SOC_BIAS_OFF
:
331 snd_soc_update_bits(codec
, WM8776_PWRDOWN
, 1, 1);
335 codec
->bias_level
= level
;
339 #define WM8776_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
340 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
341 SNDRV_PCM_RATE_96000)
344 #define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
345 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
347 static struct snd_soc_dai_ops wm8776_dac_ops
= {
348 .digital_mute
= wm8776_mute
,
349 .hw_params
= wm8776_hw_params
,
350 .set_fmt
= wm8776_set_fmt
,
351 .set_sysclk
= wm8776_set_sysclk
,
354 static struct snd_soc_dai_ops wm8776_adc_ops
= {
355 .hw_params
= wm8776_hw_params
,
356 .set_fmt
= wm8776_set_fmt
,
357 .set_sysclk
= wm8776_set_sysclk
,
360 struct snd_soc_dai wm8776_dai
[] = {
362 .name
= "WM8776 Playback",
363 .id
= WM8776_DAI_DAC
,
365 .stream_name
= "Playback",
368 .rates
= WM8776_RATES
,
369 .formats
= WM8776_FORMATS
,
371 .ops
= &wm8776_dac_ops
,
374 .name
= "WM8776 Capture",
375 .id
= WM8776_DAI_ADC
,
377 .stream_name
= "Capture",
380 .rates
= WM8776_RATES
,
381 .formats
= WM8776_FORMATS
,
383 .ops
= &wm8776_adc_ops
,
386 EXPORT_SYMBOL_GPL(wm8776_dai
);
389 static int wm8776_suspend(struct platform_device
*pdev
, pm_message_t state
)
391 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
392 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
394 wm8776_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
399 static int wm8776_resume(struct platform_device
*pdev
)
401 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
402 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
405 u16
*cache
= codec
->reg_cache
;
407 /* Sync reg_cache with the hardware */
408 for (i
= 0; i
< ARRAY_SIZE(wm8776_reg
); i
++) {
409 data
[0] = (i
<< 1) | ((cache
[i
] >> 8) & 0x0001);
410 data
[1] = cache
[i
] & 0x00ff;
411 codec
->hw_write(codec
->control_data
, data
, 2);
414 wm8776_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
419 #define wm8776_suspend NULL
420 #define wm8776_resume NULL
423 static int wm8776_probe(struct platform_device
*pdev
)
425 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
426 struct snd_soc_codec
*codec
;
429 if (wm8776_codec
== NULL
) {
430 dev_err(&pdev
->dev
, "Codec device not registered\n");
434 socdev
->card
->codec
= wm8776_codec
;
435 codec
= wm8776_codec
;
438 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
440 dev_err(codec
->dev
, "failed to create pcms: %d\n", ret
);
444 snd_soc_add_controls(codec
, wm8776_snd_controls
,
445 ARRAY_SIZE(wm8776_snd_controls
));
446 snd_soc_dapm_new_controls(codec
, wm8776_dapm_widgets
,
447 ARRAY_SIZE(wm8776_dapm_widgets
));
448 snd_soc_dapm_add_routes(codec
, routes
, ARRAY_SIZE(routes
));
450 ret
= snd_soc_init_card(socdev
);
452 dev_err(codec
->dev
, "failed to register card: %d\n", ret
);
459 snd_soc_free_pcms(socdev
);
460 snd_soc_dapm_free(socdev
);
465 /* power down chip */
466 static int wm8776_remove(struct platform_device
*pdev
)
468 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
470 snd_soc_free_pcms(socdev
);
471 snd_soc_dapm_free(socdev
);
476 struct snd_soc_codec_device soc_codec_dev_wm8776
= {
477 .probe
= wm8776_probe
,
478 .remove
= wm8776_remove
,
479 .suspend
= wm8776_suspend
,
480 .resume
= wm8776_resume
,
482 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8776
);
484 static int wm8776_register(struct wm8776_priv
*wm8776
,
485 enum snd_soc_control_type control
)
488 struct snd_soc_codec
*codec
= &wm8776
->codec
;
491 dev_err(codec
->dev
, "Another WM8776 is registered\n");
496 mutex_init(&codec
->mutex
);
497 INIT_LIST_HEAD(&codec
->dapm_widgets
);
498 INIT_LIST_HEAD(&codec
->dapm_paths
);
500 codec
->private_data
= wm8776
;
501 codec
->name
= "WM8776";
502 codec
->owner
= THIS_MODULE
;
503 codec
->bias_level
= SND_SOC_BIAS_OFF
;
504 codec
->set_bias_level
= wm8776_set_bias_level
;
505 codec
->dai
= wm8776_dai
;
506 codec
->num_dai
= ARRAY_SIZE(wm8776_dai
);
507 codec
->reg_cache_size
= WM8776_CACHEREGNUM
;
508 codec
->reg_cache
= &wm8776
->reg_cache
;
510 memcpy(codec
->reg_cache
, wm8776_reg
, sizeof(wm8776_reg
));
512 ret
= snd_soc_codec_set_cache_io(codec
, 7, 9, control
);
514 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
518 for (i
= 0; i
< ARRAY_SIZE(wm8776_dai
); i
++)
519 wm8776_dai
[i
].dev
= codec
->dev
;
521 ret
= wm8776_reset(codec
);
523 dev_err(codec
->dev
, "Failed to issue reset: %d\n", ret
);
527 wm8776_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
529 /* Latch the update bits; right channel only since we always
531 snd_soc_update_bits(codec
, WM8776_HPRVOL
, 0x100, 0x100);
532 snd_soc_update_bits(codec
, WM8776_DACRVOL
, 0x100, 0x100);
534 wm8776_codec
= codec
;
536 ret
= snd_soc_register_codec(codec
);
538 dev_err(codec
->dev
, "Failed to register codec: %d\n", ret
);
542 ret
= snd_soc_register_dais(wm8776_dai
, ARRAY_SIZE(wm8776_dai
));
544 dev_err(codec
->dev
, "Failed to register DAIs: %d\n", ret
);
551 snd_soc_unregister_codec(codec
);
557 static void wm8776_unregister(struct wm8776_priv
*wm8776
)
559 wm8776_set_bias_level(&wm8776
->codec
, SND_SOC_BIAS_OFF
);
560 snd_soc_unregister_dais(wm8776_dai
, ARRAY_SIZE(wm8776_dai
));
561 snd_soc_unregister_codec(&wm8776
->codec
);
566 #if defined(CONFIG_SPI_MASTER)
567 static int wm8776_spi_write(struct spi_device
*spi
, const char *data
, int len
)
569 struct spi_transfer t
;
570 struct spi_message m
;
579 spi_message_init(&m
);
580 memset(&t
, 0, (sizeof t
));
585 spi_message_add_tail(&t
, &m
);
591 static int __devinit
wm8776_spi_probe(struct spi_device
*spi
)
593 struct snd_soc_codec
*codec
;
594 struct wm8776_priv
*wm8776
;
596 wm8776
= kzalloc(sizeof(struct wm8776_priv
), GFP_KERNEL
);
600 codec
= &wm8776
->codec
;
601 codec
->control_data
= spi
;
602 codec
->hw_write
= (hw_write_t
)wm8776_spi_write
;
603 codec
->dev
= &spi
->dev
;
605 dev_set_drvdata(&spi
->dev
, wm8776
);
607 return wm8776_register(wm8776
, SND_SOC_SPI
);
610 static int __devexit
wm8776_spi_remove(struct spi_device
*spi
)
612 struct wm8776_priv
*wm8776
= dev_get_drvdata(&spi
->dev
);
614 wm8776_unregister(wm8776
);
620 static int wm8776_spi_suspend(struct spi_device
*spi
, pm_message_t msg
)
622 return snd_soc_suspend_device(&spi
->dev
);
625 static int wm8776_spi_resume(struct spi_device
*spi
)
627 return snd_soc_resume_device(&spi
->dev
);
630 #define wm8776_spi_suspend NULL
631 #define wm8776_spi_resume NULL
634 static struct spi_driver wm8776_spi_driver
= {
637 .bus
= &spi_bus_type
,
638 .owner
= THIS_MODULE
,
640 .probe
= wm8776_spi_probe
,
641 .suspend
= wm8776_spi_suspend
,
642 .resume
= wm8776_spi_resume
,
643 .remove
= __devexit_p(wm8776_spi_remove
),
645 #endif /* CONFIG_SPI_MASTER */
647 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
648 static __devinit
int wm8776_i2c_probe(struct i2c_client
*i2c
,
649 const struct i2c_device_id
*id
)
651 struct wm8776_priv
*wm8776
;
652 struct snd_soc_codec
*codec
;
654 wm8776
= kzalloc(sizeof(struct wm8776_priv
), GFP_KERNEL
);
658 codec
= &wm8776
->codec
;
659 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
661 i2c_set_clientdata(i2c
, wm8776
);
662 codec
->control_data
= i2c
;
664 codec
->dev
= &i2c
->dev
;
666 return wm8776_register(wm8776
, SND_SOC_I2C
);
669 static __devexit
int wm8776_i2c_remove(struct i2c_client
*client
)
671 struct wm8776_priv
*wm8776
= i2c_get_clientdata(client
);
672 wm8776_unregister(wm8776
);
677 static int wm8776_i2c_suspend(struct i2c_client
*i2c
, pm_message_t msg
)
679 return snd_soc_suspend_device(&i2c
->dev
);
682 static int wm8776_i2c_resume(struct i2c_client
*i2c
)
684 return snd_soc_resume_device(&i2c
->dev
);
687 #define wm8776_i2c_suspend NULL
688 #define wm8776_i2c_resume NULL
691 static const struct i2c_device_id wm8776_i2c_id
[] = {
695 MODULE_DEVICE_TABLE(i2c
, wm8776_i2c_id
);
697 static struct i2c_driver wm8776_i2c_driver
= {
700 .owner
= THIS_MODULE
,
702 .probe
= wm8776_i2c_probe
,
703 .remove
= __devexit_p(wm8776_i2c_remove
),
704 .suspend
= wm8776_i2c_suspend
,
705 .resume
= wm8776_i2c_resume
,
706 .id_table
= wm8776_i2c_id
,
710 static int __init
wm8776_modinit(void)
713 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
714 ret
= i2c_add_driver(&wm8776_i2c_driver
);
716 printk(KERN_ERR
"Failed to register WM8776 I2C driver: %d\n",
720 #if defined(CONFIG_SPI_MASTER)
721 ret
= spi_register_driver(&wm8776_spi_driver
);
723 printk(KERN_ERR
"Failed to register WM8776 SPI driver: %d\n",
729 module_init(wm8776_modinit
);
731 static void __exit
wm8776_exit(void)
733 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
734 i2c_del_driver(&wm8776_i2c_driver
);
736 #if defined(CONFIG_SPI_MASTER)
737 spi_unregister_driver(&wm8776_spi_driver
);
740 module_exit(wm8776_exit
);
742 MODULE_DESCRIPTION("ASoC WM8776 driver");
743 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
744 MODULE_LICENSE("GPL");