2 * wm8731.c -- WM8731 ALSA SoC Audio driver
4 * Copyright 2005 Openedhand Ltd.
6 * Author: Richard Purdie <richard@openedhand.com>
8 * Based on wm8753.c by Liam Girdwood
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
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/slab.h>
22 #include <linux/platform_device.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/spi/spi.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/initval.h>
31 #include <sound/tlv.h>
35 #define WM8731_NUM_SUPPLIES 4
36 static const char *wm8731_supply_names
[WM8731_NUM_SUPPLIES
] = {
43 /* codec private data */
45 enum snd_soc_control_type control_type
;
46 struct regulator_bulk_data supplies
[WM8731_NUM_SUPPLIES
];
47 u16 reg_cache
[WM8731_CACHEREGNUM
];
54 * wm8731 register cache
55 * We can't read the WM8731 register space when we are
56 * using 2 wire for device control, so we cache them instead.
57 * There is no point in caching the reset register
59 static const u16 wm8731_reg
[WM8731_CACHEREGNUM
] = {
60 0x0097, 0x0097, 0x0079, 0x0079,
61 0x000a, 0x0008, 0x009f, 0x000a,
65 #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0)
67 static const char *wm8731_input_select
[] = {"Line In", "Mic"};
68 static const char *wm8731_deemph
[] = {"None", "32Khz", "44.1Khz", "48Khz"};
70 static const struct soc_enum wm8731_enum
[] = {
71 SOC_ENUM_SINGLE(WM8731_APANA
, 2, 2, wm8731_input_select
),
72 SOC_ENUM_SINGLE(WM8731_APDIGI
, 1, 4, wm8731_deemph
),
75 static const DECLARE_TLV_DB_SCALE(in_tlv
, -3450, 150, 0);
76 static const DECLARE_TLV_DB_SCALE(sidetone_tlv
, -1500, 300, 0);
77 static const DECLARE_TLV_DB_SCALE(out_tlv
, -12100, 100, 1);
79 static const struct snd_kcontrol_new wm8731_snd_controls
[] = {
81 SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V
, WM8731_ROUT1V
,
83 SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V
, WM8731_ROUT1V
,
86 SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL
, WM8731_RINVOL
, 0, 31, 0,
88 SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL
, WM8731_RINVOL
, 7, 1, 1),
90 SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA
, 0, 1, 0),
91 SOC_SINGLE("Mic Capture Switch", WM8731_APANA
, 1, 1, 1),
93 SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA
, 6, 3, 1,
96 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI
, 0, 1, 1),
97 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI
, 4, 1, 0),
99 SOC_ENUM("Playback De-emphasis", wm8731_enum
[1]),
103 static const struct snd_kcontrol_new wm8731_output_mixer_controls
[] = {
104 SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA
, 3, 1, 0),
105 SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA
, 5, 1, 0),
106 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA
, 4, 1, 0),
110 static const struct snd_kcontrol_new wm8731_input_mux_controls
=
111 SOC_DAPM_ENUM("Input Select", wm8731_enum
[0]);
113 static const struct snd_soc_dapm_widget wm8731_dapm_widgets
[] = {
114 SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR
, 5, 1, NULL
, 0),
115 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR
, 4, 1,
116 &wm8731_output_mixer_controls
[0],
117 ARRAY_SIZE(wm8731_output_mixer_controls
)),
118 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR
, 3, 1),
119 SND_SOC_DAPM_OUTPUT("LOUT"),
120 SND_SOC_DAPM_OUTPUT("LHPOUT"),
121 SND_SOC_DAPM_OUTPUT("ROUT"),
122 SND_SOC_DAPM_OUTPUT("RHPOUT"),
123 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR
, 2, 1),
124 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM
, 0, 0, &wm8731_input_mux_controls
),
125 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR
, 0, 1, NULL
, 0),
126 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR
, 1, 1),
127 SND_SOC_DAPM_INPUT("MICIN"),
128 SND_SOC_DAPM_INPUT("RLINEIN"),
129 SND_SOC_DAPM_INPUT("LLINEIN"),
132 static int wm8731_check_osc(struct snd_soc_dapm_widget
*source
,
133 struct snd_soc_dapm_widget
*sink
)
135 struct wm8731_priv
*wm8731
= snd_soc_codec_get_drvdata(source
->codec
);
137 return wm8731
->sysclk_type
== WM8731_SYSCLK_MCLK
;
140 static const struct snd_soc_dapm_route intercon
[] = {
141 {"DAC", NULL
, "OSC", wm8731_check_osc
},
142 {"ADC", NULL
, "OSC", wm8731_check_osc
},
145 {"Output Mixer", "Line Bypass Switch", "Line Input"},
146 {"Output Mixer", "HiFi Playback Switch", "DAC"},
147 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
150 {"RHPOUT", NULL
, "Output Mixer"},
151 {"ROUT", NULL
, "Output Mixer"},
152 {"LHPOUT", NULL
, "Output Mixer"},
153 {"LOUT", NULL
, "Output Mixer"},
156 {"Input Mux", "Line In", "Line Input"},
157 {"Input Mux", "Mic", "Mic Bias"},
158 {"ADC", NULL
, "Input Mux"},
161 {"Line Input", NULL
, "LLINEIN"},
162 {"Line Input", NULL
, "RLINEIN"},
163 {"Mic Bias", NULL
, "MICIN"},
166 static int wm8731_add_widgets(struct snd_soc_codec
*codec
)
168 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
170 snd_soc_dapm_new_controls(dapm
, wm8731_dapm_widgets
,
171 ARRAY_SIZE(wm8731_dapm_widgets
));
172 snd_soc_dapm_add_routes(dapm
, intercon
, ARRAY_SIZE(intercon
));
186 /* codec mclk clock divider coefficients */
187 static const struct _coeff_div coeff_div
[] = {
189 {12288000, 48000, 256, 0x0, 0x0, 0x0},
190 {18432000, 48000, 384, 0x0, 0x1, 0x0},
191 {12000000, 48000, 250, 0x0, 0x0, 0x1},
194 {12288000, 32000, 384, 0x6, 0x0, 0x0},
195 {18432000, 32000, 576, 0x6, 0x1, 0x0},
196 {12000000, 32000, 375, 0x6, 0x0, 0x1},
199 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
200 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
201 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
202 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
203 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
206 {12288000, 96000, 128, 0x7, 0x0, 0x0},
207 {18432000, 96000, 192, 0x7, 0x1, 0x0},
208 {12000000, 96000, 125, 0x7, 0x0, 0x1},
211 {11289600, 44100, 256, 0x8, 0x0, 0x0},
212 {16934400, 44100, 384, 0x8, 0x1, 0x0},
213 {12000000, 44100, 272, 0x8, 0x1, 0x1},
216 {11289600, 88200, 128, 0xf, 0x0, 0x0},
217 {16934400, 88200, 192, 0xf, 0x1, 0x0},
218 {12000000, 88200, 136, 0xf, 0x1, 0x1},
221 static inline int get_coeff(int mclk
, int rate
)
225 for (i
= 0; i
< ARRAY_SIZE(coeff_div
); i
++) {
226 if (coeff_div
[i
].rate
== rate
&& coeff_div
[i
].mclk
== mclk
)
232 static int wm8731_hw_params(struct snd_pcm_substream
*substream
,
233 struct snd_pcm_hw_params
*params
,
234 struct snd_soc_dai
*dai
)
236 struct snd_soc_codec
*codec
= dai
->codec
;
237 struct wm8731_priv
*wm8731
= snd_soc_codec_get_drvdata(codec
);
238 u16 iface
= snd_soc_read(codec
, WM8731_IFACE
) & 0xfff3;
239 int i
= get_coeff(wm8731
->sysclk
, params_rate(params
));
240 u16 srate
= (coeff_div
[i
].sr
<< 2) |
241 (coeff_div
[i
].bosr
<< 1) | coeff_div
[i
].usb
;
243 snd_soc_write(codec
, WM8731_SRATE
, srate
);
246 switch (params_format(params
)) {
247 case SNDRV_PCM_FORMAT_S16_LE
:
249 case SNDRV_PCM_FORMAT_S20_3LE
:
252 case SNDRV_PCM_FORMAT_S24_LE
:
257 snd_soc_write(codec
, WM8731_IFACE
, iface
);
261 static int wm8731_pcm_prepare(struct snd_pcm_substream
*substream
,
262 struct snd_soc_dai
*dai
)
264 struct snd_soc_codec
*codec
= dai
->codec
;
267 snd_soc_write(codec
, WM8731_ACTIVE
, 0x0001);
272 static void wm8731_shutdown(struct snd_pcm_substream
*substream
,
273 struct snd_soc_dai
*dai
)
275 struct snd_soc_codec
*codec
= dai
->codec
;
278 if (!codec
->active
) {
280 snd_soc_write(codec
, WM8731_ACTIVE
, 0x0);
284 static int wm8731_mute(struct snd_soc_dai
*dai
, int mute
)
286 struct snd_soc_codec
*codec
= dai
->codec
;
287 u16 mute_reg
= snd_soc_read(codec
, WM8731_APDIGI
) & 0xfff7;
290 snd_soc_write(codec
, WM8731_APDIGI
, mute_reg
| 0x8);
292 snd_soc_write(codec
, WM8731_APDIGI
, mute_reg
);
296 static int wm8731_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
297 int clk_id
, unsigned int freq
, int dir
)
299 struct snd_soc_codec
*codec
= codec_dai
->codec
;
300 struct wm8731_priv
*wm8731
= snd_soc_codec_get_drvdata(codec
);
303 case WM8731_SYSCLK_XTAL
:
304 case WM8731_SYSCLK_MCLK
:
305 wm8731
->sysclk_type
= clk_id
;
317 wm8731
->sysclk
= freq
;
323 snd_soc_dapm_sync(&codec
->dapm
);
329 static int wm8731_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
332 struct snd_soc_codec
*codec
= codec_dai
->codec
;
335 /* set master/slave audio interface */
336 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
337 case SND_SOC_DAIFMT_CBM_CFM
:
340 case SND_SOC_DAIFMT_CBS_CFS
:
346 /* interface format */
347 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
348 case SND_SOC_DAIFMT_I2S
:
351 case SND_SOC_DAIFMT_RIGHT_J
:
353 case SND_SOC_DAIFMT_LEFT_J
:
356 case SND_SOC_DAIFMT_DSP_A
:
359 case SND_SOC_DAIFMT_DSP_B
:
366 /* clock inversion */
367 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
368 case SND_SOC_DAIFMT_NB_NF
:
370 case SND_SOC_DAIFMT_IB_IF
:
373 case SND_SOC_DAIFMT_IB_NF
:
376 case SND_SOC_DAIFMT_NB_IF
:
384 snd_soc_write(codec
, WM8731_IFACE
, iface
);
388 static int wm8731_set_bias_level(struct snd_soc_codec
*codec
,
389 enum snd_soc_bias_level level
)
391 struct wm8731_priv
*wm8731
= snd_soc_codec_get_drvdata(codec
);
394 u16
*cache
= codec
->reg_cache
;
398 case SND_SOC_BIAS_ON
:
400 case SND_SOC_BIAS_PREPARE
:
402 case SND_SOC_BIAS_STANDBY
:
403 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_OFF
) {
404 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8731
->supplies
),
409 /* Sync reg_cache with the hardware */
410 for (i
= 0; i
< ARRAY_SIZE(wm8731_reg
); i
++) {
411 if (cache
[i
] == wm8731_reg
[i
])
414 data
[0] = (i
<< 1) | ((cache
[i
] >> 8)
416 data
[1] = cache
[i
] & 0x00ff;
417 codec
->hw_write(codec
->control_data
, data
, 2);
421 /* Clear PWROFF, gate CLKOUT, everything else as-is */
422 reg
= snd_soc_read(codec
, WM8731_PWR
) & 0xff7f;
423 snd_soc_write(codec
, WM8731_PWR
, reg
| 0x0040);
425 case SND_SOC_BIAS_OFF
:
426 snd_soc_write(codec
, WM8731_ACTIVE
, 0x0);
427 snd_soc_write(codec
, WM8731_PWR
, 0xffff);
428 regulator_bulk_disable(ARRAY_SIZE(wm8731
->supplies
),
432 codec
->dapm
.bias_level
= level
;
436 #define WM8731_RATES SNDRV_PCM_RATE_8000_96000
438 #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
439 SNDRV_PCM_FMTBIT_S24_LE)
441 static struct snd_soc_dai_ops wm8731_dai_ops
= {
442 .prepare
= wm8731_pcm_prepare
,
443 .hw_params
= wm8731_hw_params
,
444 .shutdown
= wm8731_shutdown
,
445 .digital_mute
= wm8731_mute
,
446 .set_sysclk
= wm8731_set_dai_sysclk
,
447 .set_fmt
= wm8731_set_dai_fmt
,
450 static struct snd_soc_dai_driver wm8731_dai
= {
451 .name
= "wm8731-hifi",
453 .stream_name
= "Playback",
456 .rates
= WM8731_RATES
,
457 .formats
= WM8731_FORMATS
,},
459 .stream_name
= "Capture",
462 .rates
= WM8731_RATES
,
463 .formats
= WM8731_FORMATS
,},
464 .ops
= &wm8731_dai_ops
,
465 .symmetric_rates
= 1,
469 static int wm8731_suspend(struct snd_soc_codec
*codec
, pm_message_t state
)
471 wm8731_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
476 static int wm8731_resume(struct snd_soc_codec
*codec
)
478 wm8731_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
483 #define wm8731_suspend NULL
484 #define wm8731_resume NULL
487 static int wm8731_probe(struct snd_soc_codec
*codec
)
489 struct wm8731_priv
*wm8731
= snd_soc_codec_get_drvdata(codec
);
492 ret
= snd_soc_codec_set_cache_io(codec
, 7, 9, wm8731
->control_type
);
494 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
498 for (i
= 0; i
< ARRAY_SIZE(wm8731
->supplies
); i
++)
499 wm8731
->supplies
[i
].supply
= wm8731_supply_names
[i
];
501 ret
= regulator_bulk_get(codec
->dev
, ARRAY_SIZE(wm8731
->supplies
),
504 dev_err(codec
->dev
, "Failed to request supplies: %d\n", ret
);
508 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8731
->supplies
),
511 dev_err(codec
->dev
, "Failed to enable supplies: %d\n", ret
);
512 goto err_regulator_get
;
515 ret
= wm8731_reset(codec
);
517 dev_err(codec
->dev
, "Failed to issue reset: %d\n", ret
);
518 goto err_regulator_enable
;
521 wm8731_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
523 /* Latch the update bits */
524 snd_soc_update_bits(codec
, WM8731_LOUT1V
, 0x100, 0);
525 snd_soc_update_bits(codec
, WM8731_ROUT1V
, 0x100, 0);
526 snd_soc_update_bits(codec
, WM8731_LINVOL
, 0x100, 0);
527 snd_soc_update_bits(codec
, WM8731_RINVOL
, 0x100, 0);
529 /* Disable bypass path by default */
530 snd_soc_update_bits(codec
, WM8731_APANA
, 0x4, 0);
532 snd_soc_add_controls(codec
, wm8731_snd_controls
,
533 ARRAY_SIZE(wm8731_snd_controls
));
534 wm8731_add_widgets(codec
);
536 /* Regulators will have been enabled by bias management */
537 regulator_bulk_disable(ARRAY_SIZE(wm8731
->supplies
), wm8731
->supplies
);
541 err_regulator_enable
:
542 regulator_bulk_disable(ARRAY_SIZE(wm8731
->supplies
), wm8731
->supplies
);
544 regulator_bulk_free(ARRAY_SIZE(wm8731
->supplies
), wm8731
->supplies
);
550 /* power down chip */
551 static int wm8731_remove(struct snd_soc_codec
*codec
)
553 struct wm8731_priv
*wm8731
= snd_soc_codec_get_drvdata(codec
);
555 wm8731_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
557 regulator_bulk_disable(ARRAY_SIZE(wm8731
->supplies
), wm8731
->supplies
);
558 regulator_bulk_free(ARRAY_SIZE(wm8731
->supplies
), wm8731
->supplies
);
563 static struct snd_soc_codec_driver soc_codec_dev_wm8731
= {
564 .probe
= wm8731_probe
,
565 .remove
= wm8731_remove
,
566 .suspend
= wm8731_suspend
,
567 .resume
= wm8731_resume
,
568 .set_bias_level
= wm8731_set_bias_level
,
569 .reg_cache_size
= ARRAY_SIZE(wm8731_reg
),
570 .reg_word_size
= sizeof(u16
),
571 .reg_cache_default
= wm8731_reg
,
574 #if defined(CONFIG_SPI_MASTER)
575 static int __devinit
wm8731_spi_probe(struct spi_device
*spi
)
577 struct wm8731_priv
*wm8731
;
580 wm8731
= kzalloc(sizeof(struct wm8731_priv
), GFP_KERNEL
);
584 wm8731
->control_type
= SND_SOC_SPI
;
585 spi_set_drvdata(spi
, wm8731
);
587 ret
= snd_soc_register_codec(&spi
->dev
,
588 &soc_codec_dev_wm8731
, &wm8731_dai
, 1);
594 static int __devexit
wm8731_spi_remove(struct spi_device
*spi
)
596 snd_soc_unregister_codec(&spi
->dev
);
597 kfree(spi_get_drvdata(spi
));
601 static struct spi_driver wm8731_spi_driver
= {
603 .name
= "wm8731-codec",
604 .owner
= THIS_MODULE
,
606 .probe
= wm8731_spi_probe
,
607 .remove
= __devexit_p(wm8731_spi_remove
),
609 #endif /* CONFIG_SPI_MASTER */
611 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
612 static __devinit
int wm8731_i2c_probe(struct i2c_client
*i2c
,
613 const struct i2c_device_id
*id
)
615 struct wm8731_priv
*wm8731
;
618 wm8731
= kzalloc(sizeof(struct wm8731_priv
), GFP_KERNEL
);
622 i2c_set_clientdata(i2c
, wm8731
);
623 wm8731
->control_type
= SND_SOC_I2C
;
625 ret
= snd_soc_register_codec(&i2c
->dev
,
626 &soc_codec_dev_wm8731
, &wm8731_dai
, 1);
632 static __devexit
int wm8731_i2c_remove(struct i2c_client
*client
)
634 snd_soc_unregister_codec(&client
->dev
);
635 kfree(i2c_get_clientdata(client
));
639 static const struct i2c_device_id wm8731_i2c_id
[] = {
643 MODULE_DEVICE_TABLE(i2c
, wm8731_i2c_id
);
645 static struct i2c_driver wm8731_i2c_driver
= {
647 .name
= "wm8731-codec",
648 .owner
= THIS_MODULE
,
650 .probe
= wm8731_i2c_probe
,
651 .remove
= __devexit_p(wm8731_i2c_remove
),
652 .id_table
= wm8731_i2c_id
,
656 static int __init
wm8731_modinit(void)
659 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
660 ret
= i2c_add_driver(&wm8731_i2c_driver
);
662 printk(KERN_ERR
"Failed to register WM8731 I2C driver: %d\n",
666 #if defined(CONFIG_SPI_MASTER)
667 ret
= spi_register_driver(&wm8731_spi_driver
);
669 printk(KERN_ERR
"Failed to register WM8731 SPI driver: %d\n",
675 module_init(wm8731_modinit
);
677 static void __exit
wm8731_exit(void)
679 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
680 i2c_del_driver(&wm8731_i2c_driver
);
682 #if defined(CONFIG_SPI_MASTER)
683 spi_unregister_driver(&wm8731_spi_driver
);
686 module_exit(wm8731_exit
);
688 MODULE_DESCRIPTION("ASoC WM8731 driver");
689 MODULE_AUTHOR("Richard Purdie");
690 MODULE_LICENSE("GPL");