2 * wm8728.c -- WM8728 ALSA SoC Audio driver
4 * Copyright 2008 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.
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
18 #include <linux/i2c.h>
19 #include <linux/platform_device.h>
20 #include <linux/spi/spi.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/soc-dapm.h>
26 #include <sound/initval.h>
27 #include <sound/tlv.h>
31 struct snd_soc_codec_device soc_codec_dev_wm8728;
34 * We can't read the WM8728 register space so we cache them instead.
35 * Note that the defaults here aren't the physical defaults, we latch
36 * the volume update bits, mute the output and enable infinite zero
39 static const u16 wm8728_reg_defaults[] = {
46 static inline unsigned int wm8728_read_reg_cache(struct snd_soc_codec *codec,
49 u16 *cache = codec->reg_cache;
50 BUG_ON(reg > ARRAY_SIZE(wm8728_reg_defaults));
54 static inline void wm8728_write_reg_cache(struct snd_soc_codec *codec,
55 u16 reg, unsigned int value)
57 u16 *cache = codec->reg_cache;
58 BUG_ON(reg > ARRAY_SIZE(wm8728_reg_defaults));
63 * write to the WM8728 register space
65 static int wm8728_write(struct snd_soc_codec *codec, unsigned int reg,
71 * D15..D9 WM8728 register offset
72 * D8...D0 register data
74 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
75 data[1] = value & 0x00ff;
77 wm8728_write_reg_cache(codec, reg, value);
79 if (codec->hw_write(codec->control_data, data, 2) == 2)
85 static const DECLARE_TLV_DB_SCALE(wm8728_tlv, -12750, 50, 1);
87 static const struct snd_kcontrol_new wm8728_snd_controls[] = {
89 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8728_DACLVOL, WM8728_DACRVOL,
90 0, 255, 0, wm8728_tlv),
92 SOC_SINGLE("Deemphasis", WM8728_DACCTL, 1, 1, 0),
95 static int wm8728_add_controls(struct snd_soc_codec *codec)
99 for (i = 0; i < ARRAY_SIZE(wm8728_snd_controls); i++) {
100 err = snd_ctl_add(codec->card,
101 snd_soc_cnew(&wm8728_snd_controls[i],
113 static const struct snd_soc_dapm_widget wm8728_dapm_widgets[] = {
114 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0),
115 SND_SOC_DAPM_OUTPUT("VOUTL"),
116 SND_SOC_DAPM_OUTPUT("VOUTR"),
119 static const struct snd_soc_dapm_route intercon[] = {
120 {"VOUTL", NULL, "DAC"},
121 {"VOUTR", NULL, "DAC"},
124 static int wm8728_add_widgets(struct snd_soc_codec *codec)
126 snd_soc_dapm_new_controls(codec, wm8728_dapm_widgets,
127 ARRAY_SIZE(wm8728_dapm_widgets));
129 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
131 snd_soc_dapm_new_widgets(codec);
136 static int wm8728_mute(struct snd_soc_dai *dai, int mute)
138 struct snd_soc_codec *codec = dai->codec;
139 u16 mute_reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
142 wm8728_write(codec, WM8728_DACCTL, mute_reg | 1);
144 wm8728_write(codec, WM8728_DACCTL, mute_reg & ~1);
149 static int wm8728_hw_params(struct snd_pcm_substream *substream,
150 struct snd_pcm_hw_params *params)
152 struct snd_soc_pcm_runtime *rtd = substream->private_data;
153 struct snd_soc_device *socdev = rtd->socdev;
154 struct snd_soc_codec *codec = socdev->codec;
155 u16 dac = wm8728_read_reg_cache(codec, WM8728_DACCTL);
159 switch (params_format(params)) {
160 case SNDRV_PCM_FORMAT_S16_LE:
162 case SNDRV_PCM_FORMAT_S20_3LE:
165 case SNDRV_PCM_FORMAT_S24_LE:
172 wm8728_write(codec, WM8728_DACCTL, dac);
177 static int wm8728_set_dai_fmt(struct snd_soc_dai *codec_dai,
180 struct snd_soc_codec *codec = codec_dai->codec;
181 u16 iface = wm8728_read_reg_cache(codec, WM8728_IFCTL);
183 /* Currently only I2S is supported by the driver, though the
184 * hardware is more flexible.
186 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
187 case SND_SOC_DAIFMT_I2S:
194 /* The hardware only support full slave mode */
195 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
196 case SND_SOC_DAIFMT_CBS_CFS:
202 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
203 case SND_SOC_DAIFMT_NB_NF:
206 case SND_SOC_DAIFMT_IB_NF:
210 case SND_SOC_DAIFMT_NB_IF:
214 case SND_SOC_DAIFMT_IB_IF:
221 wm8728_write(codec, WM8728_IFCTL, iface);
225 static int wm8728_set_bias_level(struct snd_soc_codec *codec,
226 enum snd_soc_bias_level level)
232 case SND_SOC_BIAS_ON:
233 case SND_SOC_BIAS_PREPARE:
234 case SND_SOC_BIAS_STANDBY:
235 if (codec->bias_level == SND_SOC_BIAS_OFF) {
236 /* Power everything up... */
237 reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
238 wm8728_write(codec, WM8728_DACCTL, reg & ~0x4);
240 /* ..then sync in the register cache. */
241 for (i = 0; i < ARRAY_SIZE(wm8728_reg_defaults); i++)
242 wm8728_write(codec, i,
243 wm8728_read_reg_cache(codec, i));
247 case SND_SOC_BIAS_OFF:
248 reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
249 wm8728_write(codec, WM8728_DACCTL, reg | 0x4);
252 codec->bias_level = level;
256 #define WM8728_RATES (SNDRV_PCM_RATE_8000_192000)
258 #define WM8728_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
259 SNDRV_PCM_FMTBIT_S24_LE)
261 struct snd_soc_dai wm8728_dai = {
264 .stream_name = "Playback",
267 .rates = WM8728_RATES,
268 .formats = WM8728_FORMATS,
271 .hw_params = wm8728_hw_params,
274 .digital_mute = wm8728_mute,
275 .set_fmt = wm8728_set_dai_fmt,
278 EXPORT_SYMBOL_GPL(wm8728_dai);
280 static int wm8728_suspend(struct platform_device *pdev, pm_message_t state)
282 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
283 struct snd_soc_codec *codec = socdev->codec;
285 wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
290 static int wm8728_resume(struct platform_device *pdev)
292 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
293 struct snd_soc_codec *codec = socdev->codec;
295 wm8728_set_bias_level(codec, codec->suspend_bias_level);
301 * initialise the WM8728 driver
302 * register the mixer and dsp interfaces with the kernel
304 static int wm8728_init(struct snd_soc_device *socdev)
306 struct snd_soc_codec *codec = socdev->codec;
309 codec->name = "WM8728";
310 codec->owner = THIS_MODULE;
311 codec->read = wm8728_read_reg_cache;
312 codec->write = wm8728_write;
313 codec->set_bias_level = wm8728_set_bias_level;
314 codec->dai = &wm8728_dai;
316 codec->bias_level = SND_SOC_BIAS_OFF;
317 codec->reg_cache_size = ARRAY_SIZE(wm8728_reg_defaults);
318 codec->reg_cache = kmemdup(wm8728_reg_defaults,
319 sizeof(wm8728_reg_defaults),
321 if (codec->reg_cache == NULL)
325 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
327 printk(KERN_ERR "wm8728: failed to create pcms\n");
331 /* power on device */
332 wm8728_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
334 wm8728_add_controls(codec);
335 wm8728_add_widgets(codec);
336 ret = snd_soc_register_card(socdev);
338 printk(KERN_ERR "wm8728: failed to register card\n");
345 snd_soc_free_pcms(socdev);
346 snd_soc_dapm_free(socdev);
348 kfree(codec->reg_cache);
352 static struct snd_soc_device *wm8728_socdev;
354 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
357 * WM8728 2 wire address is determined by GPIO5
358 * state during powerup.
363 static int wm8728_i2c_probe(struct i2c_client *i2c,
364 const struct i2c_device_id *id)
366 struct snd_soc_device *socdev = wm8728_socdev;
367 struct snd_soc_codec *codec = socdev->codec;
370 i2c_set_clientdata(i2c, codec);
371 codec->control_data = i2c;
373 ret = wm8728_init(socdev);
375 pr_err("failed to initialise WM8728\n");
380 static int wm8728_i2c_remove(struct i2c_client *client)
382 struct snd_soc_codec *codec = i2c_get_clientdata(client);
383 kfree(codec->reg_cache);
387 static const struct i2c_device_id wm8728_i2c_id[] = {
391 MODULE_DEVICE_TABLE(i2c, wm8728_i2c_id);
393 static struct i2c_driver wm8728_i2c_driver = {
395 .name = "WM8728 I2C Codec",
396 .owner = THIS_MODULE,
398 .probe = wm8728_i2c_probe,
399 .remove = wm8728_i2c_remove,
400 .id_table = wm8728_i2c_id,
403 static int wm8728_add_i2c_device(struct platform_device *pdev,
404 const struct wm8728_setup_data *setup)
406 struct i2c_board_info info;
407 struct i2c_adapter *adapter;
408 struct i2c_client *client;
411 ret = i2c_add_driver(&wm8728_i2c_driver);
413 dev_err(&pdev->dev, "can't add i2c driver\n");
417 memset(&info, 0, sizeof(struct i2c_board_info));
418 info.addr = setup->i2c_address;
419 strlcpy(info.type, "wm8728", I2C_NAME_SIZE);
421 adapter = i2c_get_adapter(setup->i2c_bus);
423 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
428 client = i2c_new_device(adapter, &info);
429 i2c_put_adapter(adapter);
431 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
432 (unsigned int)info.addr);
439 i2c_del_driver(&wm8728_i2c_driver);
444 #if defined(CONFIG_SPI_MASTER)
445 static int __devinit wm8728_spi_probe(struct spi_device *spi)
447 struct snd_soc_device *socdev = wm8728_socdev;
448 struct snd_soc_codec *codec = socdev->codec;
451 codec->control_data = spi;
453 ret = wm8728_init(socdev);
455 dev_err(&spi->dev, "failed to initialise WM8728\n");
460 static int __devexit wm8728_spi_remove(struct spi_device *spi)
465 static struct spi_driver wm8728_spi_driver = {
468 .bus = &spi_bus_type,
469 .owner = THIS_MODULE,
471 .probe = wm8728_spi_probe,
472 .remove = __devexit_p(wm8728_spi_remove),
475 static int wm8728_spi_write(struct spi_device *spi, const char *data, int len)
477 struct spi_transfer t;
478 struct spi_message m;
487 spi_message_init(&m);
488 memset(&t, 0, (sizeof t));
493 spi_message_add_tail(&t, &m);
498 #endif /* CONFIG_SPI_MASTER */
500 static int wm8728_probe(struct platform_device *pdev)
502 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
503 struct wm8728_setup_data *setup;
504 struct snd_soc_codec *codec;
507 setup = socdev->codec_data;
508 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
512 socdev->codec = codec;
513 mutex_init(&codec->mutex);
514 INIT_LIST_HEAD(&codec->dapm_widgets);
515 INIT_LIST_HEAD(&codec->dapm_paths);
517 wm8728_socdev = socdev;
520 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
521 if (setup->i2c_address) {
522 codec->hw_write = (hw_write_t)i2c_master_send;
523 ret = wm8728_add_i2c_device(pdev, setup);
526 #if defined(CONFIG_SPI_MASTER)
528 codec->hw_write = (hw_write_t)wm8728_spi_write;
529 ret = spi_register_driver(&wm8728_spi_driver);
531 printk(KERN_ERR "can't add spi driver");
541 /* power down chip */
542 static int wm8728_remove(struct platform_device *pdev)
544 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
545 struct snd_soc_codec *codec = socdev->codec;
547 if (codec->control_data)
548 wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
550 snd_soc_free_pcms(socdev);
551 snd_soc_dapm_free(socdev);
552 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
553 i2c_unregister_device(codec->control_data);
554 i2c_del_driver(&wm8728_i2c_driver);
556 #if defined(CONFIG_SPI_MASTER)
557 spi_unregister_driver(&wm8728_spi_driver);
564 struct snd_soc_codec_device soc_codec_dev_wm8728 = {
565 .probe = wm8728_probe,
566 .remove = wm8728_remove,
567 .suspend = wm8728_suspend,
568 .resume = wm8728_resume,
570 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8728);
572 MODULE_DESCRIPTION("ASoC WM8728 driver");
573 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
574 MODULE_LICENSE("GPL");