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/platform_device.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/soc-dapm.h>
28 #include <sound/initval.h>
29 #include <sound/tlv.h>
33 static struct snd_soc_codec
*wm8523_codec
;
34 struct snd_soc_codec_device soc_codec_dev_wm8523
;
36 #define WM8523_NUM_SUPPLIES 2
37 static const char *wm8523_supply_names
[WM8523_NUM_SUPPLIES
] = {
42 #define WM8523_NUM_RATES 7
44 /* codec private data */
46 struct snd_soc_codec codec
;
47 u16 reg_cache
[WM8523_REGISTER_COUNT
];
48 struct regulator_bulk_data supplies
[WM8523_NUM_SUPPLIES
];
50 unsigned int rate_constraint_list
[WM8523_NUM_RATES
];
51 struct snd_pcm_hw_constraint_list rate_constraint
;
54 static const u16 wm8523_reg
[WM8523_REGISTER_COUNT
] = {
55 0x8523, /* R0 - DEVICE_ID */
56 0x0001, /* R1 - REVISION */
57 0x0000, /* R2 - PSCTRL1 */
58 0x1812, /* R3 - AIF_CTRL1 */
59 0x0000, /* R4 - AIF_CTRL2 */
60 0x0001, /* R5 - DAC_CTRL3 */
61 0x0190, /* R6 - DAC_GAINL */
62 0x0190, /* R7 - DAC_GAINR */
63 0x0000, /* R8 - ZERO_DETECT */
66 static int wm8523_volatile_register(unsigned int reg
)
69 case WM8523_DEVICE_ID
:
77 static int wm8523_reset(struct snd_soc_codec
*codec
)
79 return snd_soc_write(codec
, WM8523_DEVICE_ID
, 0);
82 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -10000, 25, 0);
84 static const char *wm8523_zd_count_text
[] = {
89 static const struct soc_enum wm8523_zc_count
=
90 SOC_ENUM_SINGLE(WM8523_ZERO_DETECT
, 0, 2, wm8523_zd_count_text
);
92 static const struct snd_kcontrol_new wm8523_snd_controls
[] = {
93 SOC_DOUBLE_R_TLV("Playback Volume", WM8523_DAC_GAINL
, WM8523_DAC_GAINR
,
95 SOC_SINGLE("ZC Switch", WM8523_DAC_CTRL3
, 4, 1, 0),
96 SOC_SINGLE("Playback Deemphasis Switch", WM8523_AIF_CTRL1
, 8, 1, 0),
97 SOC_DOUBLE("Playback Switch", WM8523_DAC_CTRL3
, 2, 3, 1, 1),
98 SOC_SINGLE("Volume Ramp Up Switch", WM8523_DAC_CTRL3
, 1, 1, 0),
99 SOC_SINGLE("Volume Ramp Down Switch", WM8523_DAC_CTRL3
, 0, 1, 0),
100 SOC_ENUM("Zero Detect Count", wm8523_zc_count
),
103 static const struct snd_soc_dapm_widget wm8523_dapm_widgets
[] = {
104 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM
, 0, 0),
105 SND_SOC_DAPM_OUTPUT("LINEVOUTL"),
106 SND_SOC_DAPM_OUTPUT("LINEVOUTR"),
109 static const struct snd_soc_dapm_route intercon
[] = {
110 { "LINEVOUTL", NULL
, "DAC" },
111 { "LINEVOUTR", NULL
, "DAC" },
114 static int wm8523_add_widgets(struct snd_soc_codec
*codec
)
116 snd_soc_dapm_new_controls(codec
, wm8523_dapm_widgets
,
117 ARRAY_SIZE(wm8523_dapm_widgets
));
119 snd_soc_dapm_add_routes(codec
, intercon
, ARRAY_SIZE(intercon
));
127 } lrclk_ratios
[WM8523_NUM_RATES
] = {
137 static int wm8523_startup(struct snd_pcm_substream
*substream
,
138 struct snd_soc_dai
*dai
)
140 struct snd_soc_codec
*codec
= dai
->codec
;
141 struct wm8523_priv
*wm8523
= snd_soc_codec_get_drvdata(codec
);
143 /* The set of sample rates that can be supported depends on the
144 * MCLK supplied to the CODEC - enforce this.
146 if (!wm8523
->sysclk
) {
148 "No MCLK configured, call set_sysclk() on init\n");
153 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
154 SNDRV_PCM_HW_PARAM_RATE
,
155 &wm8523
->rate_constraint
);
160 static int wm8523_hw_params(struct snd_pcm_substream
*substream
,
161 struct snd_pcm_hw_params
*params
,
162 struct snd_soc_dai
*dai
)
164 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
165 struct snd_soc_device
*socdev
= rtd
->socdev
;
166 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
167 struct wm8523_priv
*wm8523
= snd_soc_codec_get_drvdata(codec
);
169 u16 aifctrl1
= snd_soc_read(codec
, WM8523_AIF_CTRL1
);
170 u16 aifctrl2
= snd_soc_read(codec
, WM8523_AIF_CTRL2
);
172 /* Find a supported LRCLK ratio */
173 for (i
= 0; i
< ARRAY_SIZE(lrclk_ratios
); i
++) {
174 if (wm8523
->sysclk
/ params_rate(params
) ==
175 lrclk_ratios
[i
].ratio
)
179 /* Should never happen, should be handled by constraints */
180 if (i
== ARRAY_SIZE(lrclk_ratios
)) {
181 dev_err(codec
->dev
, "MCLK/fs ratio %d unsupported\n",
182 wm8523
->sysclk
/ params_rate(params
));
186 aifctrl2
&= ~WM8523_SR_MASK
;
187 aifctrl2
|= lrclk_ratios
[i
].value
;
189 aifctrl1
&= ~WM8523_WL_MASK
;
190 switch (params_format(params
)) {
191 case SNDRV_PCM_FORMAT_S16_LE
:
193 case SNDRV_PCM_FORMAT_S20_3LE
:
196 case SNDRV_PCM_FORMAT_S24_LE
:
199 case SNDRV_PCM_FORMAT_S32_LE
:
204 snd_soc_write(codec
, WM8523_AIF_CTRL1
, aifctrl1
);
205 snd_soc_write(codec
, WM8523_AIF_CTRL2
, aifctrl2
);
210 static int wm8523_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
211 int clk_id
, unsigned int freq
, int dir
)
213 struct snd_soc_codec
*codec
= codec_dai
->codec
;
214 struct wm8523_priv
*wm8523
= snd_soc_codec_get_drvdata(codec
);
218 wm8523
->sysclk
= freq
;
220 wm8523
->rate_constraint
.count
= 0;
221 for (i
= 0; i
< ARRAY_SIZE(lrclk_ratios
); i
++) {
222 val
= freq
/ lrclk_ratios
[i
].ratio
;
223 /* Check that it's a standard rate since core can't
224 * cope with others and having the odd rates confuses
225 * constraint matching.
240 dev_dbg(codec
->dev
, "Supported sample rate: %dHz\n",
242 wm8523
->rate_constraint_list
[i
] = val
;
243 wm8523
->rate_constraint
.count
++;
246 dev_dbg(codec
->dev
, "Skipping sample rate: %dHz\n",
251 /* Need at least one supported rate... */
252 if (wm8523
->rate_constraint
.count
== 0)
259 static int wm8523_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
262 struct snd_soc_codec
*codec
= codec_dai
->codec
;
263 u16 aifctrl1
= snd_soc_read(codec
, WM8523_AIF_CTRL1
);
265 aifctrl1
&= ~(WM8523_BCLK_INV_MASK
| WM8523_LRCLK_INV_MASK
|
266 WM8523_FMT_MASK
| WM8523_AIF_MSTR_MASK
);
268 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
269 case SND_SOC_DAIFMT_CBM_CFM
:
270 aifctrl1
|= WM8523_AIF_MSTR
;
272 case SND_SOC_DAIFMT_CBS_CFS
:
278 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
279 case SND_SOC_DAIFMT_I2S
:
282 case SND_SOC_DAIFMT_RIGHT_J
:
284 case SND_SOC_DAIFMT_LEFT_J
:
287 case SND_SOC_DAIFMT_DSP_A
:
290 case SND_SOC_DAIFMT_DSP_B
:
297 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
298 case SND_SOC_DAIFMT_NB_NF
:
300 case SND_SOC_DAIFMT_IB_IF
:
301 aifctrl1
|= WM8523_BCLK_INV
| WM8523_LRCLK_INV
;
303 case SND_SOC_DAIFMT_IB_NF
:
304 aifctrl1
|= WM8523_BCLK_INV
;
306 case SND_SOC_DAIFMT_NB_IF
:
307 aifctrl1
|= WM8523_LRCLK_INV
;
313 snd_soc_write(codec
, WM8523_AIF_CTRL1
, aifctrl1
);
318 static int wm8523_set_bias_level(struct snd_soc_codec
*codec
,
319 enum snd_soc_bias_level level
)
321 struct wm8523_priv
*wm8523
= snd_soc_codec_get_drvdata(codec
);
325 case SND_SOC_BIAS_ON
:
328 case SND_SOC_BIAS_PREPARE
:
330 snd_soc_update_bits(codec
, WM8523_PSCTRL1
,
331 WM8523_SYS_ENA_MASK
, 3);
334 case SND_SOC_BIAS_STANDBY
:
335 if (codec
->bias_level
== SND_SOC_BIAS_OFF
) {
336 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8523
->supplies
),
340 "Failed to enable supplies: %d\n",
345 /* Initial power up */
346 snd_soc_update_bits(codec
, WM8523_PSCTRL1
,
347 WM8523_SYS_ENA_MASK
, 1);
349 /* Sync back default/cached values */
350 for (i
= WM8523_AIF_CTRL1
;
351 i
< WM8523_MAX_REGISTER
; i
++)
352 snd_soc_write(codec
, i
, wm8523
->reg_cache
[i
]);
358 /* Power up to mute */
359 snd_soc_update_bits(codec
, WM8523_PSCTRL1
,
360 WM8523_SYS_ENA_MASK
, 2);
364 case SND_SOC_BIAS_OFF
:
365 /* The chip runs through the power down sequence for us. */
366 snd_soc_update_bits(codec
, WM8523_PSCTRL1
,
367 WM8523_SYS_ENA_MASK
, 0);
370 regulator_bulk_disable(ARRAY_SIZE(wm8523
->supplies
),
374 codec
->bias_level
= level
;
378 #define WM8523_RATES SNDRV_PCM_RATE_8000_192000
380 #define WM8523_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
381 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
383 static struct snd_soc_dai_ops wm8523_dai_ops
= {
384 .startup
= wm8523_startup
,
385 .hw_params
= wm8523_hw_params
,
386 .set_sysclk
= wm8523_set_dai_sysclk
,
387 .set_fmt
= wm8523_set_dai_fmt
,
390 struct snd_soc_dai wm8523_dai
= {
393 .stream_name
= "Playback",
394 .channels_min
= 2, /* Mono modes not yet supported */
396 .rates
= WM8523_RATES
,
397 .formats
= WM8523_FORMATS
,
399 .ops
= &wm8523_dai_ops
,
401 EXPORT_SYMBOL_GPL(wm8523_dai
);
404 static int wm8523_suspend(struct platform_device
*pdev
, pm_message_t state
)
406 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
407 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
409 wm8523_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
413 static int wm8523_resume(struct platform_device
*pdev
)
415 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
416 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
418 wm8523_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
423 #define wm8523_suspend NULL
424 #define wm8523_resume NULL
427 static int wm8523_probe(struct platform_device
*pdev
)
429 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
430 struct snd_soc_codec
*codec
;
433 if (wm8523_codec
== NULL
) {
434 dev_err(&pdev
->dev
, "Codec device not registered\n");
438 socdev
->card
->codec
= wm8523_codec
;
439 codec
= wm8523_codec
;
442 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
444 dev_err(codec
->dev
, "failed to create pcms: %d\n", ret
);
448 snd_soc_add_controls(codec
, wm8523_snd_controls
,
449 ARRAY_SIZE(wm8523_snd_controls
));
450 wm8523_add_widgets(codec
);
458 static int wm8523_remove(struct platform_device
*pdev
)
460 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
462 snd_soc_free_pcms(socdev
);
463 snd_soc_dapm_free(socdev
);
468 struct snd_soc_codec_device soc_codec_dev_wm8523
= {
469 .probe
= wm8523_probe
,
470 .remove
= wm8523_remove
,
471 .suspend
= wm8523_suspend
,
472 .resume
= wm8523_resume
,
474 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8523
);
476 static int wm8523_register(struct wm8523_priv
*wm8523
,
477 enum snd_soc_control_type control
)
480 struct snd_soc_codec
*codec
= &wm8523
->codec
;
484 dev_err(codec
->dev
, "Another WM8523 is registered\n");
488 mutex_init(&codec
->mutex
);
489 INIT_LIST_HEAD(&codec
->dapm_widgets
);
490 INIT_LIST_HEAD(&codec
->dapm_paths
);
492 snd_soc_codec_set_drvdata(codec
, wm8523
);
493 codec
->name
= "WM8523";
494 codec
->owner
= THIS_MODULE
;
495 codec
->bias_level
= SND_SOC_BIAS_OFF
;
496 codec
->set_bias_level
= wm8523_set_bias_level
;
497 codec
->dai
= &wm8523_dai
;
499 codec
->reg_cache_size
= WM8523_REGISTER_COUNT
;
500 codec
->reg_cache
= &wm8523
->reg_cache
;
501 codec
->volatile_register
= wm8523_volatile_register
;
503 wm8523
->rate_constraint
.list
= &wm8523
->rate_constraint_list
[0];
504 wm8523
->rate_constraint
.count
=
505 ARRAY_SIZE(wm8523
->rate_constraint_list
);
507 memcpy(codec
->reg_cache
, wm8523_reg
, sizeof(wm8523_reg
));
509 ret
= snd_soc_codec_set_cache_io(codec
, 8, 16, control
);
511 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
515 for (i
= 0; i
< ARRAY_SIZE(wm8523
->supplies
); i
++)
516 wm8523
->supplies
[i
].supply
= wm8523_supply_names
[i
];
518 ret
= regulator_bulk_get(codec
->dev
, ARRAY_SIZE(wm8523
->supplies
),
521 dev_err(codec
->dev
, "Failed to request supplies: %d\n", ret
);
525 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8523
->supplies
),
528 dev_err(codec
->dev
, "Failed to enable supplies: %d\n", ret
);
532 ret
= snd_soc_read(codec
, WM8523_DEVICE_ID
);
534 dev_err(codec
->dev
, "Failed to read ID register\n");
537 if (ret
!= wm8523_reg
[WM8523_DEVICE_ID
]) {
538 dev_err(codec
->dev
, "Device is not a WM8523, ID is %x\n", ret
);
543 ret
= snd_soc_read(codec
, WM8523_REVISION
);
545 dev_err(codec
->dev
, "Failed to read revision register\n");
548 dev_info(codec
->dev
, "revision %c\n",
549 (ret
& WM8523_CHIP_REV_MASK
) + 'A');
551 ret
= wm8523_reset(codec
);
553 dev_err(codec
->dev
, "Failed to issue reset\n");
557 wm8523_dai
.dev
= codec
->dev
;
559 /* Change some default settings - latch VU and enable ZC */
560 wm8523
->reg_cache
[WM8523_DAC_GAINR
] |= WM8523_DACR_VU
;
561 wm8523
->reg_cache
[WM8523_DAC_CTRL3
] |= WM8523_ZC
;
563 wm8523_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
565 /* Bias level configuration will have done an extra enable */
566 regulator_bulk_disable(ARRAY_SIZE(wm8523
->supplies
), wm8523
->supplies
);
568 wm8523_codec
= codec
;
570 ret
= snd_soc_register_codec(codec
);
572 dev_err(codec
->dev
, "Failed to register codec: %d\n", ret
);
576 ret
= snd_soc_register_dai(&wm8523_dai
);
578 dev_err(codec
->dev
, "Failed to register DAI: %d\n", ret
);
579 snd_soc_unregister_codec(codec
);
586 regulator_bulk_disable(ARRAY_SIZE(wm8523
->supplies
), wm8523
->supplies
);
588 regulator_bulk_free(ARRAY_SIZE(wm8523
->supplies
), wm8523
->supplies
);
594 static void wm8523_unregister(struct wm8523_priv
*wm8523
)
596 wm8523_set_bias_level(&wm8523
->codec
, SND_SOC_BIAS_OFF
);
597 regulator_bulk_free(ARRAY_SIZE(wm8523
->supplies
), wm8523
->supplies
);
598 snd_soc_unregister_dai(&wm8523_dai
);
599 snd_soc_unregister_codec(&wm8523
->codec
);
604 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
605 static __devinit
int wm8523_i2c_probe(struct i2c_client
*i2c
,
606 const struct i2c_device_id
*id
)
608 struct wm8523_priv
*wm8523
;
609 struct snd_soc_codec
*codec
;
611 wm8523
= kzalloc(sizeof(struct wm8523_priv
), GFP_KERNEL
);
615 codec
= &wm8523
->codec
;
616 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
618 i2c_set_clientdata(i2c
, wm8523
);
619 codec
->control_data
= i2c
;
621 codec
->dev
= &i2c
->dev
;
623 return wm8523_register(wm8523
, SND_SOC_I2C
);
626 static __devexit
int wm8523_i2c_remove(struct i2c_client
*client
)
628 struct wm8523_priv
*wm8523
= i2c_get_clientdata(client
);
629 wm8523_unregister(wm8523
);
633 static const struct i2c_device_id wm8523_i2c_id
[] = {
637 MODULE_DEVICE_TABLE(i2c
, wm8523_i2c_id
);
639 static struct i2c_driver wm8523_i2c_driver
= {
642 .owner
= THIS_MODULE
,
644 .probe
= wm8523_i2c_probe
,
645 .remove
= __devexit_p(wm8523_i2c_remove
),
646 .id_table
= wm8523_i2c_id
,
650 static int __init
wm8523_modinit(void)
653 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
654 ret
= i2c_add_driver(&wm8523_i2c_driver
);
656 printk(KERN_ERR
"Failed to register WM8523 I2C driver: %d\n",
662 module_init(wm8523_modinit
);
664 static void __exit
wm8523_exit(void)
666 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
667 i2c_del_driver(&wm8523_i2c_driver
);
670 module_exit(wm8523_exit
);
672 MODULE_DESCRIPTION("ASoC WM8523 driver");
673 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
674 MODULE_LICENSE("GPL");