2 * wm8711.c -- WM8711 ALSA SoC Audio driver
4 * Copyright 2006 Wolfson Microelectronics
6 * Author: Mike Arthur <linux@wolfsonmicro.com>
8 * Based on wm8731.c by Richard Purdie
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/platform_device.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27 #include <sound/initval.h>
31 static struct snd_soc_codec *wm8711_codec;
33 /* codec private data */
35 struct snd_soc_codec codec;
36 u16 reg_cache[WM8711_CACHEREGNUM];
41 * wm8711 register cache
42 * We can't read the WM8711 register space when we are
43 * using 2 wire for device control, so we cache them instead.
44 * There is no point in caching the reset register
46 static const u16 wm8711_reg[WM8711_CACHEREGNUM] = {
47 0x0079, 0x0079, 0x000a, 0x0008,
48 0x009f, 0x000a, 0x0000, 0x0000
51 #define wm8711_reset(c) snd_soc_write(c, WM8711_RESET, 0)
53 static const struct snd_kcontrol_new wm8711_snd_controls[] = {
55 SOC_DOUBLE_R("Master Playback Volume", WM8711_LOUT1V, WM8711_ROUT1V,
57 SOC_DOUBLE_R("Master Playback ZC Switch", WM8711_LOUT1V, WM8711_ROUT1V,
62 /* add non dapm controls */
63 static int wm8711_add_controls(struct snd_soc_codec *codec)
67 for (i = 0; i < ARRAY_SIZE(wm8711_snd_controls); i++) {
68 err = snd_ctl_add(codec->card,
69 snd_soc_cnew(&wm8711_snd_controls[i], codec,
79 static const struct snd_kcontrol_new wm8711_output_mixer_controls[] = {
80 SOC_DAPM_SINGLE("Line Bypass Switch", WM8711_APANA, 3, 1, 0),
81 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8711_APANA, 4, 1, 0),
84 static const struct snd_soc_dapm_widget wm8711_dapm_widgets[] = {
85 SND_SOC_DAPM_MIXER("Output Mixer", WM8711_PWR, 4, 1,
86 &wm8711_output_mixer_controls[0],
87 ARRAY_SIZE(wm8711_output_mixer_controls)),
88 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8711_PWR, 3, 1),
89 SND_SOC_DAPM_OUTPUT("LOUT"),
90 SND_SOC_DAPM_OUTPUT("LHPOUT"),
91 SND_SOC_DAPM_OUTPUT("ROUT"),
92 SND_SOC_DAPM_OUTPUT("RHPOUT"),
95 static const struct snd_soc_dapm_route intercon[] = {
97 {"Output Mixer", "Line Bypass Switch", "Line Input"},
98 {"Output Mixer", "HiFi Playback Switch", "DAC"},
101 {"RHPOUT", NULL, "Output Mixer"},
102 {"ROUT", NULL, "Output Mixer"},
103 {"LHPOUT", NULL, "Output Mixer"},
104 {"LOUT", NULL, "Output Mixer"},
107 static int wm8711_add_widgets(struct snd_soc_codec *codec)
109 snd_soc_dapm_new_controls(codec, wm8711_dapm_widgets,
110 ARRAY_SIZE(wm8711_dapm_widgets));
112 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
114 snd_soc_dapm_new_widgets(codec);
127 /* codec mclk clock divider coefficients */
128 static const struct _coeff_div coeff_div[] = {
130 {12288000, 48000, 256, 0x0, 0x0, 0x0},
131 {18432000, 48000, 384, 0x0, 0x1, 0x0},
132 {12000000, 48000, 250, 0x0, 0x0, 0x1},
135 {12288000, 32000, 384, 0x6, 0x0, 0x0},
136 {18432000, 32000, 576, 0x6, 0x1, 0x0},
137 {12000000, 32000, 375, 0x6, 0x0, 0x1},
140 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
141 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
142 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
143 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
144 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
147 {12288000, 96000, 128, 0x7, 0x0, 0x0},
148 {18432000, 96000, 192, 0x7, 0x1, 0x0},
149 {12000000, 96000, 125, 0x7, 0x0, 0x1},
152 {11289600, 44100, 256, 0x8, 0x0, 0x0},
153 {16934400, 44100, 384, 0x8, 0x1, 0x0},
154 {12000000, 44100, 272, 0x8, 0x1, 0x1},
157 {11289600, 88200, 128, 0xf, 0x0, 0x0},
158 {16934400, 88200, 192, 0xf, 0x1, 0x0},
159 {12000000, 88200, 136, 0xf, 0x1, 0x1},
162 static inline int get_coeff(int mclk, int rate)
166 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
167 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
173 static int wm8711_hw_params(struct snd_pcm_substream *substream,
174 struct snd_pcm_hw_params *params,
175 struct snd_soc_dai *dai)
177 struct snd_soc_codec *codec = dai->codec;
178 struct wm8711_priv *wm8711 = codec->private_data;
179 u16 iface = snd_soc_read(codec, WM8711_IFACE) & 0xfffc;
180 int i = get_coeff(wm8711->sysclk, params_rate(params));
181 u16 srate = (coeff_div[i].sr << 2) |
182 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
184 snd_soc_write(codec, WM8711_SRATE, srate);
187 switch (params_format(params)) {
188 case SNDRV_PCM_FORMAT_S16_LE:
190 case SNDRV_PCM_FORMAT_S20_3LE:
193 case SNDRV_PCM_FORMAT_S24_LE:
198 snd_soc_write(codec, WM8711_IFACE, iface);
202 static int wm8711_pcm_prepare(struct snd_pcm_substream *substream,
203 struct snd_soc_dai *dai)
205 struct snd_soc_codec *codec = dai->codec;
208 snd_soc_write(codec, WM8711_ACTIVE, 0x0001);
213 static void wm8711_shutdown(struct snd_pcm_substream *substream,
214 struct snd_soc_dai *dai)
216 struct snd_soc_codec *codec = dai->codec;
219 if (!codec->active) {
221 snd_soc_write(codec, WM8711_ACTIVE, 0x0);
225 static int wm8711_mute(struct snd_soc_dai *dai, int mute)
227 struct snd_soc_codec *codec = dai->codec;
228 u16 mute_reg = snd_soc_read(codec, WM8711_APDIGI) & 0xfff7;
231 snd_soc_write(codec, WM8711_APDIGI, mute_reg | 0x8);
233 snd_soc_write(codec, WM8711_APDIGI, mute_reg);
238 static int wm8711_set_dai_sysclk(struct snd_soc_dai *codec_dai,
239 int clk_id, unsigned int freq, int dir)
241 struct snd_soc_codec *codec = codec_dai->codec;
242 struct wm8711_priv *wm8711 = codec->private_data;
250 wm8711->sysclk = freq;
256 static int wm8711_set_dai_fmt(struct snd_soc_dai *codec_dai,
259 struct snd_soc_codec *codec = codec_dai->codec;
262 /* set master/slave audio interface */
263 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
264 case SND_SOC_DAIFMT_CBM_CFM:
267 case SND_SOC_DAIFMT_CBS_CFS:
273 /* interface format */
274 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
275 case SND_SOC_DAIFMT_I2S:
278 case SND_SOC_DAIFMT_RIGHT_J:
280 case SND_SOC_DAIFMT_LEFT_J:
283 case SND_SOC_DAIFMT_DSP_A:
286 case SND_SOC_DAIFMT_DSP_B:
293 /* clock inversion */
294 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
295 case SND_SOC_DAIFMT_NB_NF:
297 case SND_SOC_DAIFMT_IB_IF:
300 case SND_SOC_DAIFMT_IB_NF:
303 case SND_SOC_DAIFMT_NB_IF:
311 snd_soc_write(codec, WM8711_IFACE, iface);
316 static int wm8711_set_bias_level(struct snd_soc_codec *codec,
317 enum snd_soc_bias_level level)
319 u16 reg = snd_soc_read(codec, WM8711_PWR) & 0xff7f;
322 case SND_SOC_BIAS_ON:
323 snd_soc_write(codec, WM8711_PWR, reg);
325 case SND_SOC_BIAS_PREPARE:
327 case SND_SOC_BIAS_STANDBY:
328 snd_soc_write(codec, WM8711_PWR, reg | 0x0040);
330 case SND_SOC_BIAS_OFF:
331 snd_soc_write(codec, WM8711_ACTIVE, 0x0);
332 snd_soc_write(codec, WM8711_PWR, 0xffff);
335 codec->bias_level = level;
339 #define WM8711_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
340 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
341 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
342 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
343 SNDRV_PCM_RATE_96000)
345 #define WM8711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
346 SNDRV_PCM_FMTBIT_S24_LE)
348 static struct snd_soc_dai_ops wm8711_ops = {
349 .prepare = wm8711_pcm_prepare,
350 .hw_params = wm8711_hw_params,
351 .shutdown = wm8711_shutdown,
352 .digital_mute = wm8711_mute,
353 .set_sysclk = wm8711_set_dai_sysclk,
354 .set_fmt = wm8711_set_dai_fmt,
357 struct snd_soc_dai wm8711_dai = {
360 .stream_name = "Playback",
363 .rates = WM8711_RATES,
364 .formats = WM8711_FORMATS,},
367 EXPORT_SYMBOL_GPL(wm8711_dai);
369 static int wm8711_suspend(struct platform_device *pdev, pm_message_t state)
371 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
372 struct snd_soc_codec *codec = socdev->card->codec;
374 snd_soc_write(codec, WM8711_ACTIVE, 0x0);
375 wm8711_set_bias_level(codec, SND_SOC_BIAS_OFF);
379 static int wm8711_resume(struct platform_device *pdev)
381 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
382 struct snd_soc_codec *codec = socdev->card->codec;
385 u16 *cache = codec->reg_cache;
387 /* Sync reg_cache with the hardware */
388 for (i = 0; i < ARRAY_SIZE(wm8711_reg); i++) {
389 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
390 data[1] = cache[i] & 0x00ff;
391 codec->hw_write(codec->control_data, data, 2);
393 wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
394 wm8711_set_bias_level(codec, codec->suspend_bias_level);
398 static int wm8711_probe(struct platform_device *pdev)
400 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
401 struct snd_soc_codec *codec;
404 if (wm8711_codec == NULL) {
405 dev_err(&pdev->dev, "Codec device not registered\n");
409 socdev->card->codec = wm8711_codec;
410 codec = wm8711_codec;
413 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
415 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
419 snd_soc_add_controls(codec, wm8711_snd_controls,
420 ARRAY_SIZE(wm8711_snd_controls));
421 wm8711_add_widgets(codec);
422 ret = snd_soc_init_card(socdev);
424 dev_err(codec->dev, "failed to register card: %d\n", ret);
431 snd_soc_free_pcms(socdev);
432 snd_soc_dapm_free(socdev);
437 /* power down chip */
438 static int wm8711_remove(struct platform_device *pdev)
440 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
442 snd_soc_free_pcms(socdev);
443 snd_soc_dapm_free(socdev);
448 struct snd_soc_codec_device soc_codec_dev_wm8711 = {
449 .probe = wm8711_probe,
450 .remove = wm8711_remove,
451 .suspend = wm8711_suspend,
452 .resume = wm8711_resume,
454 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8711);
456 static int wm8711_register(struct wm8711_priv *wm8711,
457 enum snd_soc_control_type control)
460 struct snd_soc_codec *codec = &wm8711->codec;
464 dev_err(codec->dev, "Another WM8711 is registered\n");
468 mutex_init(&codec->mutex);
469 INIT_LIST_HEAD(&codec->dapm_widgets);
470 INIT_LIST_HEAD(&codec->dapm_paths);
472 codec->private_data = wm8711;
473 codec->name = "WM8711";
474 codec->owner = THIS_MODULE;
475 codec->bias_level = SND_SOC_BIAS_OFF;
476 codec->set_bias_level = wm8711_set_bias_level;
477 codec->dai = &wm8711_dai;
479 codec->reg_cache_size = WM8711_CACHEREGNUM;
480 codec->reg_cache = &wm8711->reg_cache;
482 memcpy(codec->reg_cache, wm8711_reg, sizeof(wm8711_reg));
484 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
486 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
490 ret = wm8711_reset(codec);
492 dev_err(codec->dev, "Failed to issue reset\n");
496 wm8711_dai.dev = codec->dev;
498 wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
500 /* Latch the update bits */
501 reg = snd_soc_read(codec, WM8711_LOUT1V);
502 snd_soc_write(codec, WM8711_LOUT1V, reg | 0x0100);
503 reg = snd_soc_read(codec, WM8711_ROUT1V);
504 snd_soc_write(codec, WM8711_ROUT1V, reg | 0x0100);
506 wm8711_codec = codec;
508 ret = snd_soc_register_codec(codec);
510 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
514 ret = snd_soc_register_dai(&wm8711_dai);
516 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
523 snd_soc_unregister_codec(codec);
529 static void wm8711_unregister(struct wm8711_priv *wm8711)
531 wm8711_set_bias_level(&wm8711->codec, SND_SOC_BIAS_OFF);
532 snd_soc_unregister_dai(&wm8711_dai);
533 snd_soc_unregister_codec(&wm8711->codec);
538 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
539 static __devinit int wm8711_i2c_probe(struct i2c_client *i2c,
540 const struct i2c_device_id *id)
542 struct wm8711_priv *wm8711;
543 struct snd_soc_codec *codec;
545 wm8711 = kzalloc(sizeof(struct wm8711_priv), GFP_KERNEL);
549 codec = &wm8711->codec;
550 codec->hw_write = (hw_write_t)i2c_master_send;
552 i2c_set_clientdata(i2c, wm8711);
553 codec->control_data = i2c;
555 codec->dev = &i2c->dev;
557 return wm8711_register(wm8711, SND_SOC_I2C);
560 static __devexit int wm8711_i2c_remove(struct i2c_client *client)
562 struct wm8711_priv *wm8711 = i2c_get_clientdata(client);
563 wm8711_unregister(wm8711);
567 static const struct i2c_device_id wm8711_i2c_id[] = {
571 MODULE_DEVICE_TABLE(i2c, wm8711_i2c_id);
573 static struct i2c_driver wm8711_i2c_driver = {
575 .name = "WM8711 I2C Codec",
576 .owner = THIS_MODULE,
578 .probe = wm8711_i2c_probe,
579 .remove = __devexit_p(wm8711_i2c_remove),
580 .id_table = wm8711_i2c_id,
584 static int __init wm8711_modinit(void)
587 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
588 ret = i2c_add_driver(&wm8711_i2c_driver);
590 printk(KERN_ERR "Failed to register WM8711 I2C driver: %d\n",
596 module_init(wm8711_modinit);
598 static void __exit wm8711_exit(void)
600 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
601 i2c_del_driver(&wm8711_i2c_driver);
604 module_exit(wm8711_exit);
606 MODULE_DESCRIPTION("ASoC WM8711 driver");
607 MODULE_AUTHOR("Mike Arthur");
608 MODULE_LICENSE("GPL");