2 * wm8523.c -- WM8523 ALSA SoC Audio driver
4 * Copyright 2009 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <linux/of_device.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/initval.h>
29 #include <sound/tlv.h>
33 #define WM8523_NUM_SUPPLIES 2
34 static const char *wm8523_supply_names
[WM8523_NUM_SUPPLIES
] = {
39 #define WM8523_NUM_RATES 7
41 /* codec private data */
43 struct regmap
*regmap
;
44 struct regulator_bulk_data supplies
[WM8523_NUM_SUPPLIES
];
46 unsigned int rate_constraint_list
[WM8523_NUM_RATES
];
47 struct snd_pcm_hw_constraint_list rate_constraint
;
50 static const struct reg_default wm8523_reg_defaults
[] = {
51 { 2, 0x0000 }, /* R2 - PSCTRL1 */
52 { 3, 0x1812 }, /* R3 - AIF_CTRL1 */
53 { 4, 0x0000 }, /* R4 - AIF_CTRL2 */
54 { 5, 0x0001 }, /* R5 - DAC_CTRL3 */
55 { 6, 0x0190 }, /* R6 - DAC_GAINL */
56 { 7, 0x0190 }, /* R7 - DAC_GAINR */
57 { 8, 0x0000 }, /* R8 - ZERO_DETECT */
60 static bool wm8523_volatile_register(struct device
*dev
, unsigned int reg
)
63 case WM8523_DEVICE_ID
:
71 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -10000, 25, 0);
73 static const char *wm8523_zd_count_text
[] = {
78 static const struct soc_enum wm8523_zc_count
=
79 SOC_ENUM_SINGLE(WM8523_ZERO_DETECT
, 0, 2, wm8523_zd_count_text
);
81 static const struct snd_kcontrol_new wm8523_controls
[] = {
82 SOC_DOUBLE_R_TLV("Playback Volume", WM8523_DAC_GAINL
, WM8523_DAC_GAINR
,
84 SOC_SINGLE("ZC Switch", WM8523_DAC_CTRL3
, 4, 1, 0),
85 SOC_SINGLE("Playback Deemphasis Switch", WM8523_AIF_CTRL1
, 8, 1, 0),
86 SOC_DOUBLE("Playback Switch", WM8523_DAC_CTRL3
, 2, 3, 1, 1),
87 SOC_SINGLE("Volume Ramp Up Switch", WM8523_DAC_CTRL3
, 1, 1, 0),
88 SOC_SINGLE("Volume Ramp Down Switch", WM8523_DAC_CTRL3
, 0, 1, 0),
89 SOC_ENUM("Zero Detect Count", wm8523_zc_count
),
92 static const struct snd_soc_dapm_widget wm8523_dapm_widgets
[] = {
93 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM
, 0, 0),
94 SND_SOC_DAPM_OUTPUT("LINEVOUTL"),
95 SND_SOC_DAPM_OUTPUT("LINEVOUTR"),
98 static const struct snd_soc_dapm_route wm8523_dapm_routes
[] = {
99 { "LINEVOUTL", NULL
, "DAC" },
100 { "LINEVOUTR", NULL
, "DAC" },
106 } lrclk_ratios
[WM8523_NUM_RATES
] = {
116 static int wm8523_startup(struct snd_pcm_substream
*substream
,
117 struct snd_soc_dai
*dai
)
119 struct snd_soc_codec
*codec
= dai
->codec
;
120 struct wm8523_priv
*wm8523
= snd_soc_codec_get_drvdata(codec
);
122 /* The set of sample rates that can be supported depends on the
123 * MCLK supplied to the CODEC - enforce this.
125 if (!wm8523
->sysclk
) {
127 "No MCLK configured, call set_sysclk() on init\n");
131 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
132 SNDRV_PCM_HW_PARAM_RATE
,
133 &wm8523
->rate_constraint
);
138 static int wm8523_hw_params(struct snd_pcm_substream
*substream
,
139 struct snd_pcm_hw_params
*params
,
140 struct snd_soc_dai
*dai
)
142 struct snd_soc_codec
*codec
= dai
->codec
;
143 struct wm8523_priv
*wm8523
= snd_soc_codec_get_drvdata(codec
);
145 u16 aifctrl1
= snd_soc_read(codec
, WM8523_AIF_CTRL1
);
146 u16 aifctrl2
= snd_soc_read(codec
, WM8523_AIF_CTRL2
);
148 /* Find a supported LRCLK ratio */
149 for (i
= 0; i
< ARRAY_SIZE(lrclk_ratios
); i
++) {
150 if (wm8523
->sysclk
/ params_rate(params
) ==
151 lrclk_ratios
[i
].ratio
)
155 /* Should never happen, should be handled by constraints */
156 if (i
== ARRAY_SIZE(lrclk_ratios
)) {
157 dev_err(codec
->dev
, "MCLK/fs ratio %d unsupported\n",
158 wm8523
->sysclk
/ params_rate(params
));
162 aifctrl2
&= ~WM8523_SR_MASK
;
163 aifctrl2
|= lrclk_ratios
[i
].value
;
165 aifctrl1
&= ~WM8523_WL_MASK
;
166 switch (params_format(params
)) {
167 case SNDRV_PCM_FORMAT_S16_LE
:
169 case SNDRV_PCM_FORMAT_S20_3LE
:
172 case SNDRV_PCM_FORMAT_S24_LE
:
175 case SNDRV_PCM_FORMAT_S32_LE
:
180 snd_soc_write(codec
, WM8523_AIF_CTRL1
, aifctrl1
);
181 snd_soc_write(codec
, WM8523_AIF_CTRL2
, aifctrl2
);
186 static int wm8523_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
187 int clk_id
, unsigned int freq
, int dir
)
189 struct snd_soc_codec
*codec
= codec_dai
->codec
;
190 struct wm8523_priv
*wm8523
= snd_soc_codec_get_drvdata(codec
);
194 wm8523
->sysclk
= freq
;
196 wm8523
->rate_constraint
.count
= 0;
197 for (i
= 0; i
< ARRAY_SIZE(lrclk_ratios
); i
++) {
198 val
= freq
/ lrclk_ratios
[i
].ratio
;
199 /* Check that it's a standard rate since core can't
200 * cope with others and having the odd rates confuses
201 * constraint matching.
216 dev_dbg(codec
->dev
, "Supported sample rate: %dHz\n",
218 wm8523
->rate_constraint_list
[i
] = val
;
219 wm8523
->rate_constraint
.count
++;
222 dev_dbg(codec
->dev
, "Skipping sample rate: %dHz\n",
227 /* Need at least one supported rate... */
228 if (wm8523
->rate_constraint
.count
== 0)
235 static int wm8523_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
238 struct snd_soc_codec
*codec
= codec_dai
->codec
;
239 u16 aifctrl1
= snd_soc_read(codec
, WM8523_AIF_CTRL1
);
241 aifctrl1
&= ~(WM8523_BCLK_INV_MASK
| WM8523_LRCLK_INV_MASK
|
242 WM8523_FMT_MASK
| WM8523_AIF_MSTR_MASK
);
244 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
245 case SND_SOC_DAIFMT_CBM_CFM
:
246 aifctrl1
|= WM8523_AIF_MSTR
;
248 case SND_SOC_DAIFMT_CBS_CFS
:
254 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
255 case SND_SOC_DAIFMT_I2S
:
258 case SND_SOC_DAIFMT_RIGHT_J
:
260 case SND_SOC_DAIFMT_LEFT_J
:
263 case SND_SOC_DAIFMT_DSP_A
:
266 case SND_SOC_DAIFMT_DSP_B
:
273 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
274 case SND_SOC_DAIFMT_NB_NF
:
276 case SND_SOC_DAIFMT_IB_IF
:
277 aifctrl1
|= WM8523_BCLK_INV
| WM8523_LRCLK_INV
;
279 case SND_SOC_DAIFMT_IB_NF
:
280 aifctrl1
|= WM8523_BCLK_INV
;
282 case SND_SOC_DAIFMT_NB_IF
:
283 aifctrl1
|= WM8523_LRCLK_INV
;
289 snd_soc_write(codec
, WM8523_AIF_CTRL1
, aifctrl1
);
294 static int wm8523_set_bias_level(struct snd_soc_codec
*codec
,
295 enum snd_soc_bias_level level
)
297 struct wm8523_priv
*wm8523
= snd_soc_codec_get_drvdata(codec
);
301 case SND_SOC_BIAS_ON
:
304 case SND_SOC_BIAS_PREPARE
:
306 snd_soc_update_bits(codec
, WM8523_PSCTRL1
,
307 WM8523_SYS_ENA_MASK
, 3);
310 case SND_SOC_BIAS_STANDBY
:
311 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_OFF
) {
312 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8523
->supplies
),
316 "Failed to enable supplies: %d\n",
321 /* Sync back default/cached values */
322 regcache_sync(wm8523
->regmap
);
324 /* Initial power up */
325 snd_soc_update_bits(codec
, WM8523_PSCTRL1
,
326 WM8523_SYS_ENA_MASK
, 1);
331 /* Power up to mute */
332 snd_soc_update_bits(codec
, WM8523_PSCTRL1
,
333 WM8523_SYS_ENA_MASK
, 2);
337 case SND_SOC_BIAS_OFF
:
338 /* The chip runs through the power down sequence for us. */
339 snd_soc_update_bits(codec
, WM8523_PSCTRL1
,
340 WM8523_SYS_ENA_MASK
, 0);
343 regulator_bulk_disable(ARRAY_SIZE(wm8523
->supplies
),
347 codec
->dapm
.bias_level
= level
;
351 #define WM8523_RATES SNDRV_PCM_RATE_8000_192000
353 #define WM8523_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
354 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
356 static const struct snd_soc_dai_ops wm8523_dai_ops
= {
357 .startup
= wm8523_startup
,
358 .hw_params
= wm8523_hw_params
,
359 .set_sysclk
= wm8523_set_dai_sysclk
,
360 .set_fmt
= wm8523_set_dai_fmt
,
363 static struct snd_soc_dai_driver wm8523_dai
= {
364 .name
= "wm8523-hifi",
366 .stream_name
= "Playback",
367 .channels_min
= 2, /* Mono modes not yet supported */
369 .rates
= WM8523_RATES
,
370 .formats
= WM8523_FORMATS
,
372 .ops
= &wm8523_dai_ops
,
376 static int wm8523_suspend(struct snd_soc_codec
*codec
)
378 wm8523_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
382 static int wm8523_resume(struct snd_soc_codec
*codec
)
384 wm8523_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
388 #define wm8523_suspend NULL
389 #define wm8523_resume NULL
392 static int wm8523_probe(struct snd_soc_codec
*codec
)
394 struct wm8523_priv
*wm8523
= snd_soc_codec_get_drvdata(codec
);
397 wm8523
->rate_constraint
.list
= &wm8523
->rate_constraint_list
[0];
398 wm8523
->rate_constraint
.count
=
399 ARRAY_SIZE(wm8523
->rate_constraint_list
);
401 ret
= snd_soc_codec_set_cache_io(codec
, 8, 16, SND_SOC_REGMAP
);
403 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
407 /* Change some default settings - latch VU and enable ZC */
408 snd_soc_update_bits(codec
, WM8523_DAC_GAINR
,
409 WM8523_DACR_VU
, WM8523_DACR_VU
);
410 snd_soc_update_bits(codec
, WM8523_DAC_CTRL3
, WM8523_ZC
, WM8523_ZC
);
412 wm8523_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
417 static int wm8523_remove(struct snd_soc_codec
*codec
)
419 wm8523_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
423 static struct snd_soc_codec_driver soc_codec_dev_wm8523
= {
424 .probe
= wm8523_probe
,
425 .remove
= wm8523_remove
,
426 .suspend
= wm8523_suspend
,
427 .resume
= wm8523_resume
,
428 .set_bias_level
= wm8523_set_bias_level
,
430 .controls
= wm8523_controls
,
431 .num_controls
= ARRAY_SIZE(wm8523_controls
),
432 .dapm_widgets
= wm8523_dapm_widgets
,
433 .num_dapm_widgets
= ARRAY_SIZE(wm8523_dapm_widgets
),
434 .dapm_routes
= wm8523_dapm_routes
,
435 .num_dapm_routes
= ARRAY_SIZE(wm8523_dapm_routes
),
438 static const struct of_device_id wm8523_of_match
[] = {
439 { .compatible
= "wlf,wm8523" },
443 static const struct regmap_config wm8523_regmap
= {
446 .max_register
= WM8523_ZERO_DETECT
,
448 .reg_defaults
= wm8523_reg_defaults
,
449 .num_reg_defaults
= ARRAY_SIZE(wm8523_reg_defaults
),
450 .cache_type
= REGCACHE_RBTREE
,
452 .volatile_reg
= wm8523_volatile_register
,
455 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
456 static int wm8523_i2c_probe(struct i2c_client
*i2c
,
457 const struct i2c_device_id
*id
)
459 struct wm8523_priv
*wm8523
;
463 wm8523
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm8523_priv
),
468 wm8523
->regmap
= devm_regmap_init_i2c(i2c
, &wm8523_regmap
);
469 if (IS_ERR(wm8523
->regmap
)) {
470 ret
= PTR_ERR(wm8523
->regmap
);
471 dev_err(&i2c
->dev
, "Failed to create regmap: %d\n", ret
);
475 for (i
= 0; i
< ARRAY_SIZE(wm8523
->supplies
); i
++)
476 wm8523
->supplies
[i
].supply
= wm8523_supply_names
[i
];
478 ret
= devm_regulator_bulk_get(&i2c
->dev
, ARRAY_SIZE(wm8523
->supplies
),
481 dev_err(&i2c
->dev
, "Failed to request supplies: %d\n", ret
);
485 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8523
->supplies
),
488 dev_err(&i2c
->dev
, "Failed to enable supplies: %d\n", ret
);
492 ret
= regmap_read(wm8523
->regmap
, WM8523_DEVICE_ID
, &val
);
494 dev_err(&i2c
->dev
, "Failed to read ID register\n");
498 dev_err(&i2c
->dev
, "Device is not a WM8523, ID is %x\n", ret
);
503 ret
= regmap_read(wm8523
->regmap
, WM8523_REVISION
, &val
);
505 dev_err(&i2c
->dev
, "Failed to read revision register\n");
508 dev_info(&i2c
->dev
, "revision %c\n",
509 (val
& WM8523_CHIP_REV_MASK
) + 'A');
511 ret
= regmap_write(wm8523
->regmap
, WM8523_DEVICE_ID
, 0x8523);
513 dev_err(&i2c
->dev
, "Failed to reset device: %d\n", ret
);
517 regulator_bulk_disable(ARRAY_SIZE(wm8523
->supplies
), wm8523
->supplies
);
519 i2c_set_clientdata(i2c
, wm8523
);
521 ret
= snd_soc_register_codec(&i2c
->dev
,
522 &soc_codec_dev_wm8523
, &wm8523_dai
, 1);
527 regulator_bulk_disable(ARRAY_SIZE(wm8523
->supplies
), wm8523
->supplies
);
531 static int wm8523_i2c_remove(struct i2c_client
*client
)
533 snd_soc_unregister_codec(&client
->dev
);
537 static const struct i2c_device_id wm8523_i2c_id
[] = {
541 MODULE_DEVICE_TABLE(i2c
, wm8523_i2c_id
);
543 static struct i2c_driver wm8523_i2c_driver
= {
546 .owner
= THIS_MODULE
,
547 .of_match_table
= wm8523_of_match
,
549 .probe
= wm8523_i2c_probe
,
550 .remove
= wm8523_i2c_remove
,
551 .id_table
= wm8523_i2c_id
,
555 static int __init
wm8523_modinit(void)
558 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
559 ret
= i2c_add_driver(&wm8523_i2c_driver
);
561 printk(KERN_ERR
"Failed to register WM8523 I2C driver: %d\n",
567 module_init(wm8523_modinit
);
569 static void __exit
wm8523_exit(void)
571 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
572 i2c_del_driver(&wm8523_i2c_driver
);
575 module_exit(wm8523_exit
);
577 MODULE_DESCRIPTION("ASoC WM8523 driver");
578 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
579 MODULE_LICENSE("GPL");