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
52 * read wm8711 register cache
54 static inline unsigned int wm8711_read_reg_cache(struct snd_soc_codec *codec,
57 u16 *cache = codec->reg_cache;
58 if (reg == WM8711_RESET)
60 if (reg >= WM8711_CACHEREGNUM)
66 * write wm8711 register cache
68 static inline void wm8711_write_reg_cache(struct snd_soc_codec *codec,
69 u16 reg, unsigned int value)
71 u16 *cache = codec->reg_cache;
72 if (reg >= WM8711_CACHEREGNUM)
78 * write to the WM8711 register space
80 static int wm8711_write(struct snd_soc_codec *codec, unsigned int reg,
86 * D15..D9 WM8753 register offset
87 * D8...D0 register data
89 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
90 data[1] = value & 0x00ff;
92 wm8711_write_reg_cache(codec, reg, value);
93 if (codec->hw_write(codec->control_data, data, 2) == 2)
99 #define wm8711_reset(c) wm8711_write(c, WM8711_RESET, 0)
101 static const struct snd_kcontrol_new wm8711_snd_controls[] = {
103 SOC_DOUBLE_R("Master Playback Volume", WM8711_LOUT1V, WM8711_ROUT1V,
105 SOC_DOUBLE_R("Master Playback ZC Switch", WM8711_LOUT1V, WM8711_ROUT1V,
110 /* add non dapm controls */
111 static int wm8711_add_controls(struct snd_soc_codec *codec)
115 for (i = 0; i < ARRAY_SIZE(wm8711_snd_controls); i++) {
116 err = snd_ctl_add(codec->card,
117 snd_soc_cnew(&wm8711_snd_controls[i], codec,
127 static const struct snd_kcontrol_new wm8711_output_mixer_controls[] = {
128 SOC_DAPM_SINGLE("Line Bypass Switch", WM8711_APANA, 3, 1, 0),
129 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8711_APANA, 4, 1, 0),
132 static const struct snd_soc_dapm_widget wm8711_dapm_widgets[] = {
133 SND_SOC_DAPM_MIXER("Output Mixer", WM8711_PWR, 4, 1,
134 &wm8711_output_mixer_controls[0],
135 ARRAY_SIZE(wm8711_output_mixer_controls)),
136 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8711_PWR, 3, 1),
137 SND_SOC_DAPM_OUTPUT("LOUT"),
138 SND_SOC_DAPM_OUTPUT("LHPOUT"),
139 SND_SOC_DAPM_OUTPUT("ROUT"),
140 SND_SOC_DAPM_OUTPUT("RHPOUT"),
143 static const struct snd_soc_dapm_route intercon[] = {
145 {"Output Mixer", "Line Bypass Switch", "Line Input"},
146 {"Output Mixer", "HiFi Playback Switch", "DAC"},
149 {"RHPOUT", NULL, "Output Mixer"},
150 {"ROUT", NULL, "Output Mixer"},
151 {"LHPOUT", NULL, "Output Mixer"},
152 {"LOUT", NULL, "Output Mixer"},
155 static int wm8711_add_widgets(struct snd_soc_codec *codec)
157 snd_soc_dapm_new_controls(codec, wm8711_dapm_widgets,
158 ARRAY_SIZE(wm8711_dapm_widgets));
160 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
162 snd_soc_dapm_new_widgets(codec);
175 /* codec mclk clock divider coefficients */
176 static const struct _coeff_div coeff_div[] = {
178 {12288000, 48000, 256, 0x0, 0x0, 0x0},
179 {18432000, 48000, 384, 0x0, 0x1, 0x0},
180 {12000000, 48000, 250, 0x0, 0x0, 0x1},
183 {12288000, 32000, 384, 0x6, 0x0, 0x0},
184 {18432000, 32000, 576, 0x6, 0x1, 0x0},
185 {12000000, 32000, 375, 0x6, 0x0, 0x1},
188 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
189 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
190 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
191 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
192 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
195 {12288000, 96000, 128, 0x7, 0x0, 0x0},
196 {18432000, 96000, 192, 0x7, 0x1, 0x0},
197 {12000000, 96000, 125, 0x7, 0x0, 0x1},
200 {11289600, 44100, 256, 0x8, 0x0, 0x0},
201 {16934400, 44100, 384, 0x8, 0x1, 0x0},
202 {12000000, 44100, 272, 0x8, 0x1, 0x1},
205 {11289600, 88200, 128, 0xf, 0x0, 0x0},
206 {16934400, 88200, 192, 0xf, 0x1, 0x0},
207 {12000000, 88200, 136, 0xf, 0x1, 0x1},
210 static inline int get_coeff(int mclk, int rate)
214 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
215 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
221 static int wm8711_hw_params(struct snd_pcm_substream *substream,
222 struct snd_pcm_hw_params *params,
223 struct snd_soc_dai *dai)
225 struct snd_soc_codec *codec = dai->codec;
226 struct wm8711_priv *wm8711 = codec->private_data;
227 u16 iface = wm8711_read_reg_cache(codec, WM8711_IFACE) & 0xfffc;
228 int i = get_coeff(wm8711->sysclk, params_rate(params));
229 u16 srate = (coeff_div[i].sr << 2) |
230 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
232 wm8711_write(codec, WM8711_SRATE, srate);
235 switch (params_format(params)) {
236 case SNDRV_PCM_FORMAT_S16_LE:
238 case SNDRV_PCM_FORMAT_S20_3LE:
241 case SNDRV_PCM_FORMAT_S24_LE:
246 wm8711_write(codec, WM8711_IFACE, iface);
250 static int wm8711_pcm_prepare(struct snd_pcm_substream *substream,
251 struct snd_soc_dai *dai)
253 struct snd_soc_codec *codec = dai->codec;
256 wm8711_write(codec, WM8711_ACTIVE, 0x0001);
261 static void wm8711_shutdown(struct snd_pcm_substream *substream,
262 struct snd_soc_dai *dai)
264 struct snd_soc_codec *codec = dai->codec;
267 if (!codec->active) {
269 wm8711_write(codec, WM8711_ACTIVE, 0x0);
273 static int wm8711_mute(struct snd_soc_dai *dai, int mute)
275 struct snd_soc_codec *codec = dai->codec;
276 u16 mute_reg = wm8711_read_reg_cache(codec, WM8711_APDIGI) & 0xfff7;
279 wm8711_write(codec, WM8711_APDIGI, mute_reg | 0x8);
281 wm8711_write(codec, WM8711_APDIGI, mute_reg);
286 static int wm8711_set_dai_sysclk(struct snd_soc_dai *codec_dai,
287 int clk_id, unsigned int freq, int dir)
289 struct snd_soc_codec *codec = codec_dai->codec;
290 struct wm8711_priv *wm8711 = codec->private_data;
298 wm8711->sysclk = freq;
304 static int wm8711_set_dai_fmt(struct snd_soc_dai *codec_dai,
307 struct snd_soc_codec *codec = codec_dai->codec;
310 /* set master/slave audio interface */
311 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
312 case SND_SOC_DAIFMT_CBM_CFM:
315 case SND_SOC_DAIFMT_CBS_CFS:
321 /* interface format */
322 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
323 case SND_SOC_DAIFMT_I2S:
326 case SND_SOC_DAIFMT_RIGHT_J:
328 case SND_SOC_DAIFMT_LEFT_J:
331 case SND_SOC_DAIFMT_DSP_A:
334 case SND_SOC_DAIFMT_DSP_B:
341 /* clock inversion */
342 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
343 case SND_SOC_DAIFMT_NB_NF:
345 case SND_SOC_DAIFMT_IB_IF:
348 case SND_SOC_DAIFMT_IB_NF:
351 case SND_SOC_DAIFMT_NB_IF:
359 wm8711_write(codec, WM8711_IFACE, iface);
364 static int wm8711_set_bias_level(struct snd_soc_codec *codec,
365 enum snd_soc_bias_level level)
367 u16 reg = wm8711_read_reg_cache(codec, WM8711_PWR) & 0xff7f;
370 case SND_SOC_BIAS_ON:
371 wm8711_write(codec, WM8711_PWR, reg);
373 case SND_SOC_BIAS_PREPARE:
375 case SND_SOC_BIAS_STANDBY:
376 wm8711_write(codec, WM8711_PWR, reg | 0x0040);
378 case SND_SOC_BIAS_OFF:
379 wm8711_write(codec, WM8711_ACTIVE, 0x0);
380 wm8711_write(codec, WM8711_PWR, 0xffff);
383 codec->bias_level = level;
387 #define WM8711_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
388 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
389 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
390 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
391 SNDRV_PCM_RATE_96000)
393 #define WM8711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
394 SNDRV_PCM_FMTBIT_S24_LE)
396 static struct snd_soc_dai_ops wm8711_ops = {
397 .prepare = wm8711_pcm_prepare,
398 .hw_params = wm8711_hw_params,
399 .shutdown = wm8711_shutdown,
400 .digital_mute = wm8711_mute,
401 .set_sysclk = wm8711_set_dai_sysclk,
402 .set_fmt = wm8711_set_dai_fmt,
405 struct snd_soc_dai wm8711_dai = {
408 .stream_name = "Playback",
411 .rates = WM8711_RATES,
412 .formats = WM8711_FORMATS,},
415 EXPORT_SYMBOL_GPL(wm8711_dai);
417 static int wm8711_suspend(struct platform_device *pdev, pm_message_t state)
419 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
420 struct snd_soc_codec *codec = socdev->card->codec;
422 wm8711_write(codec, WM8711_ACTIVE, 0x0);
423 wm8711_set_bias_level(codec, SND_SOC_BIAS_OFF);
427 static int wm8711_resume(struct platform_device *pdev)
429 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
430 struct snd_soc_codec *codec = socdev->card->codec;
433 u16 *cache = codec->reg_cache;
435 /* Sync reg_cache with the hardware */
436 for (i = 0; i < ARRAY_SIZE(wm8711_reg); i++) {
437 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
438 data[1] = cache[i] & 0x00ff;
439 codec->hw_write(codec->control_data, data, 2);
441 wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
442 wm8711_set_bias_level(codec, codec->suspend_bias_level);
446 static int wm8711_probe(struct platform_device *pdev)
448 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
449 struct snd_soc_codec *codec;
452 if (wm8711_codec == NULL) {
453 dev_err(&pdev->dev, "Codec device not registered\n");
457 socdev->card->codec = wm8711_codec;
458 codec = wm8711_codec;
461 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
463 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
467 snd_soc_add_controls(codec, wm8711_snd_controls,
468 ARRAY_SIZE(wm8711_snd_controls));
469 wm8711_add_widgets(codec);
470 ret = snd_soc_init_card(socdev);
472 dev_err(codec->dev, "failed to register card: %d\n", ret);
479 snd_soc_free_pcms(socdev);
480 snd_soc_dapm_free(socdev);
485 /* power down chip */
486 static int wm8711_remove(struct platform_device *pdev)
488 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
490 snd_soc_free_pcms(socdev);
491 snd_soc_dapm_free(socdev);
496 struct snd_soc_codec_device soc_codec_dev_wm8711 = {
497 .probe = wm8711_probe,
498 .remove = wm8711_remove,
499 .suspend = wm8711_suspend,
500 .resume = wm8711_resume,
502 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8711);
504 static int wm8711_register(struct wm8711_priv *wm8711)
507 struct snd_soc_codec *codec = &wm8711->codec;
511 dev_err(codec->dev, "Another WM8711 is registered\n");
515 mutex_init(&codec->mutex);
516 INIT_LIST_HEAD(&codec->dapm_widgets);
517 INIT_LIST_HEAD(&codec->dapm_paths);
519 codec->private_data = wm8711;
520 codec->name = "WM8711";
521 codec->owner = THIS_MODULE;
522 codec->read = wm8711_read_reg_cache;
523 codec->write = wm8711_write;
524 codec->bias_level = SND_SOC_BIAS_OFF;
525 codec->set_bias_level = wm8711_set_bias_level;
526 codec->dai = &wm8711_dai;
528 codec->reg_cache_size = WM8711_CACHEREGNUM;
529 codec->reg_cache = &wm8711->reg_cache;
531 memcpy(codec->reg_cache, wm8711_reg, sizeof(wm8711_reg));
533 ret = wm8711_reset(codec);
535 dev_err(codec->dev, "Failed to issue reset\n");
539 wm8711_dai.dev = codec->dev;
541 wm8711_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
543 /* Latch the update bits */
544 reg = wm8711_read_reg_cache(codec, WM8711_LOUT1V);
545 wm8711_write(codec, WM8711_LOUT1V, reg | 0x0100);
546 reg = wm8711_read_reg_cache(codec, WM8711_ROUT1V);
547 wm8711_write(codec, WM8711_ROUT1V, reg | 0x0100);
549 wm8711_codec = codec;
551 ret = snd_soc_register_codec(codec);
553 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
557 ret = snd_soc_register_dai(&wm8711_dai);
559 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
560 snd_soc_unregister_codec(codec);
567 static void wm8711_unregister(struct wm8711_priv *wm8711)
569 wm8711_set_bias_level(&wm8711->codec, SND_SOC_BIAS_OFF);
570 snd_soc_unregister_dai(&wm8711_dai);
571 snd_soc_unregister_codec(&wm8711->codec);
576 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
577 static __devinit int wm8711_i2c_probe(struct i2c_client *i2c,
578 const struct i2c_device_id *id)
580 struct wm8711_priv *wm8711;
581 struct snd_soc_codec *codec;
583 wm8711 = kzalloc(sizeof(struct wm8711_priv), GFP_KERNEL);
587 codec = &wm8711->codec;
588 codec->hw_write = (hw_write_t)i2c_master_send;
590 i2c_set_clientdata(i2c, wm8711);
591 codec->control_data = i2c;
593 codec->dev = &i2c->dev;
595 return wm8711_register(wm8711);
598 static __devexit int wm8711_i2c_remove(struct i2c_client *client)
600 struct wm8711_priv *wm8711 = i2c_get_clientdata(client);
601 wm8711_unregister(wm8711);
605 static const struct i2c_device_id wm8711_i2c_id[] = {
609 MODULE_DEVICE_TABLE(i2c, wm8711_i2c_id);
611 static struct i2c_driver wm8711_i2c_driver = {
613 .name = "WM8711 I2C Codec",
614 .owner = THIS_MODULE,
616 .probe = wm8711_i2c_probe,
617 .remove = __devexit_p(wm8711_i2c_remove),
618 .id_table = wm8711_i2c_id,
622 static int __init wm8711_modinit(void)
625 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
626 ret = i2c_add_driver(&wm8711_i2c_driver);
628 printk(KERN_ERR "Failed to register WM8711 I2C driver: %d\n",
634 module_init(wm8711_modinit);
636 static void __exit wm8711_exit(void)
638 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
639 i2c_del_driver(&wm8711_i2c_driver);
642 module_exit(wm8711_exit);
644 MODULE_DESCRIPTION("ASoC WM8711 driver");
645 MODULE_AUTHOR("Mike Arthur");
646 MODULE_LICENSE("GPL");