2 * ALSA SoC TLV320AIC23 codec driver
4 * Author: Arun KS, <arunks@mistralsolutions.com>
5 * Copyright: (C) 2008 Mistral Solutions Pvt Ltd.,
7 * Based on sound/soc/codecs/wm8731.c by Richard Purdie
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 * The AIC23 is a driver for a low power stereo audio
17 * The machine layer should disable unsupported inputs/outputs by
18 * snd_soc_dapm_disable_pin(codec, "LHPOUT"), etc.
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
26 #include <linux/i2c.h>
27 #include <linux/platform_device.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dapm.h>
33 #include <sound/tlv.h>
34 #include <sound/initval.h>
36 #include "tlv320aic23.h"
38 #define AUDIO_NAME "tlv320aic23"
39 #define AIC23_VERSION "0.1"
41 struct tlv320aic23_srate_reg_info {
43 u8 control; /* SR3, SR2, SR1, SR0 and BOSR */
44 u8 divider; /* if 0 CLKIN = MCLK, if 1 CLKIN = MCLK/2 */
48 * AIC23 register cache
50 static const u16 tlv320aic23_reg[] = {
51 0x0097, 0x0097, 0x00F9, 0x00F9, /* 0 */
52 0x001A, 0x0004, 0x0007, 0x0001, /* 4 */
53 0x0020, 0x0000, 0x0000, 0x0000, /* 8 */
54 0x0000, 0x0000, 0x0000, 0x0000, /* 12 */
58 * read tlv320aic23 register cache
60 static inline unsigned int tlv320aic23_read_reg_cache(struct snd_soc_codec
61 *codec, unsigned int reg)
63 u16 *cache = codec->reg_cache;
64 if (reg >= ARRAY_SIZE(tlv320aic23_reg))
70 * write tlv320aic23 register cache
72 static inline void tlv320aic23_write_reg_cache(struct snd_soc_codec *codec,
75 u16 *cache = codec->reg_cache;
76 if (reg >= ARRAY_SIZE(tlv320aic23_reg))
82 * write to the tlv320aic23 register space
84 static int tlv320aic23_write(struct snd_soc_codec *codec, unsigned int reg,
90 /* TLV320AIC23 has 7 bit address and 9 bits of data
91 * so we need to switch one data bit into reg and rest
95 if ((reg < 0 || reg > 9) && (reg != 15)) {
96 printk(KERN_WARNING "%s Invalid register R%d\n", __func__, reg);
100 data = (reg << 1) | (value >> 8 & 0x01);
102 tlv320aic23_write_reg_cache(codec, reg, value);
104 if (codec->hw_write(codec->control_data, data,
105 (value & 0xff)) == 0)
108 printk(KERN_ERR "%s cannot write %03x to register R%d\n", __func__,
114 static const char *rec_src_text[] = { "Line", "Mic" };
115 static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
116 static const char *sidetone_text[] = {"-6db", "-9db", "-12db", "-18db", "0db"};
118 static const struct soc_enum rec_src_enum =
119 SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
121 static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
122 SOC_DAPM_ENUM("Input Select", rec_src_enum);
124 static const struct soc_enum tlv320aic23_rec_src =
125 SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
126 static const struct soc_enum tlv320aic23_deemph =
127 SOC_ENUM_SINGLE(TLV320AIC23_DIGT, 1, 4, deemph_text);
128 static const struct soc_enum tlv320aic23_sidetone =
129 SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 6, 5, sidetone_text);
131 static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
132 static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
134 static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
135 SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
136 TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
137 SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
138 SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
139 TLV320AIC23_RINVOL, 7, 1, 0),
140 SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
141 TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
142 SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
143 SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
144 SOC_ENUM("Sidetone Gain", tlv320aic23_sidetone),
145 SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
148 /* add non dapm controls */
149 static int tlv320aic23_add_controls(struct snd_soc_codec *codec)
154 for (i = 0; i < ARRAY_SIZE(tlv320aic23_snd_controls); i++) {
155 err = snd_ctl_add(codec->card,
156 snd_soc_cnew(&tlv320aic23_snd_controls[i],
166 /* PGA Mixer controls for Line and Mic switch */
167 static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
168 SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
169 SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
170 SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
173 static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
174 SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
175 SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
176 SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
177 &tlv320aic23_rec_src_mux_controls),
178 SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
179 &tlv320aic23_output_mixer_controls[0],
180 ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
181 SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
182 SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
184 SND_SOC_DAPM_OUTPUT("LHPOUT"),
185 SND_SOC_DAPM_OUTPUT("RHPOUT"),
186 SND_SOC_DAPM_OUTPUT("LOUT"),
187 SND_SOC_DAPM_OUTPUT("ROUT"),
189 SND_SOC_DAPM_INPUT("LLINEIN"),
190 SND_SOC_DAPM_INPUT("RLINEIN"),
192 SND_SOC_DAPM_INPUT("MICIN"),
195 static const struct snd_soc_dapm_route intercon[] = {
197 {"Output Mixer", "Line Bypass Switch", "Line Input"},
198 {"Output Mixer", "Playback Switch", "DAC"},
199 {"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
202 {"RHPOUT", NULL, "Output Mixer"},
203 {"LHPOUT", NULL, "Output Mixer"},
204 {"LOUT", NULL, "Output Mixer"},
205 {"ROUT", NULL, "Output Mixer"},
208 {"Line Input", "NULL", "LLINEIN"},
209 {"Line Input", "NULL", "RLINEIN"},
211 {"Mic Input", "NULL", "MICIN"},
214 {"Capture Source", "Line", "Line Input"},
215 {"Capture Source", "Mic", "Mic Input"},
216 {"ADC", NULL, "Capture Source"},
220 /* tlv320aic23 related */
221 static const struct tlv320aic23_srate_reg_info srate_reg_info[] = {
222 {4000, 0x06, 1}, /* 4000 */
223 {8000, 0x06, 0}, /* 8000 */
224 {16000, 0x0C, 1}, /* 16000 */
225 {22050, 0x11, 1}, /* 22050 */
226 {24000, 0x00, 1}, /* 24000 */
227 {32000, 0x0C, 0}, /* 32000 */
228 {44100, 0x11, 0}, /* 44100 */
229 {48000, 0x00, 0}, /* 48000 */
230 {88200, 0x1F, 0}, /* 88200 */
231 {96000, 0x0E, 0}, /* 96000 */
234 static int tlv320aic23_add_widgets(struct snd_soc_codec *codec)
236 snd_soc_dapm_new_controls(codec, tlv320aic23_dapm_widgets,
237 ARRAY_SIZE(tlv320aic23_dapm_widgets));
239 /* set up audio path interconnects */
240 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
242 snd_soc_dapm_new_widgets(codec);
246 static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
247 struct snd_pcm_hw_params *params)
249 struct snd_soc_pcm_runtime *rtd = substream->private_data;
250 struct snd_soc_device *socdev = rtd->socdev;
251 struct snd_soc_codec *codec = socdev->codec;
256 tlv320aic23_read_reg_cache(codec,
257 TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
259 /* Search for the right sample rate */
260 /* Verify what happens if the rate is not supported
261 * now it goes to 96Khz */
262 while ((srate_reg_info[count].sample_rate != params_rate(params)) &&
263 (count < ARRAY_SIZE(srate_reg_info))) {
267 data = (srate_reg_info[count].divider << TLV320AIC23_CLKIN_SHIFT) |
268 (srate_reg_info[count]. control << TLV320AIC23_BOSR_SHIFT) |
269 TLV320AIC23_USB_CLK_ON;
271 tlv320aic23_write(codec, TLV320AIC23_SRATE, data);
273 switch (params_format(params)) {
274 case SNDRV_PCM_FORMAT_S16_LE:
276 case SNDRV_PCM_FORMAT_S20_3LE:
277 iface_reg |= (0x01 << 2);
279 case SNDRV_PCM_FORMAT_S24_LE:
280 iface_reg |= (0x02 << 2);
282 case SNDRV_PCM_FORMAT_S32_LE:
283 iface_reg |= (0x03 << 2);
286 tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
291 static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream)
293 struct snd_soc_pcm_runtime *rtd = substream->private_data;
294 struct snd_soc_device *socdev = rtd->socdev;
295 struct snd_soc_codec *codec = socdev->codec;
298 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0001);
303 static void tlv320aic23_shutdown(struct snd_pcm_substream *substream)
305 struct snd_soc_pcm_runtime *rtd = substream->private_data;
306 struct snd_soc_device *socdev = rtd->socdev;
307 struct snd_soc_codec *codec = socdev->codec;
310 if (!codec->active) {
312 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
316 static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
318 struct snd_soc_codec *codec = dai->codec;
321 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT);
323 reg |= TLV320AIC23_DACM_MUTE;
326 reg &= ~TLV320AIC23_DACM_MUTE;
328 tlv320aic23_write(codec, TLV320AIC23_DIGT, reg);
333 static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
336 struct snd_soc_codec *codec = codec_dai->codec;
340 tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
342 /* set master/slave audio interface */
343 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
344 case SND_SOC_DAIFMT_CBM_CFM:
345 iface_reg |= TLV320AIC23_MS_MASTER;
347 case SND_SOC_DAIFMT_CBS_CFS:
354 /* interface format */
355 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
356 case SND_SOC_DAIFMT_I2S:
357 iface_reg |= TLV320AIC23_FOR_I2S;
359 case SND_SOC_DAIFMT_DSP_A:
360 iface_reg |= TLV320AIC23_FOR_DSP;
362 case SND_SOC_DAIFMT_RIGHT_J:
364 case SND_SOC_DAIFMT_LEFT_J:
365 iface_reg |= TLV320AIC23_FOR_LJUST;
372 tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
377 static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
378 int clk_id, unsigned int freq, int dir)
380 struct snd_soc_codec *codec = codec_dai->codec;
389 static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
390 enum snd_soc_bias_level level)
392 u16 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_PWR) & 0xff7f;
395 case SND_SOC_BIAS_ON:
396 /* vref/mid, osc on, dac unmute */
397 tlv320aic23_write(codec, TLV320AIC23_PWR, reg);
399 case SND_SOC_BIAS_PREPARE:
401 case SND_SOC_BIAS_STANDBY:
402 /* everything off except vref/vmid, */
403 tlv320aic23_write(codec, TLV320AIC23_PWR, reg | 0x0040);
405 case SND_SOC_BIAS_OFF:
406 /* everything off, dac mute, inactive */
407 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
408 tlv320aic23_write(codec, TLV320AIC23_PWR, 0xffff);
411 codec->bias_level = level;
415 #define AIC23_RATES SNDRV_PCM_RATE_8000_96000
416 #define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
417 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
419 struct snd_soc_dai tlv320aic23_dai = {
420 .name = "tlv320aic23",
422 .stream_name = "Playback",
425 .rates = AIC23_RATES,
426 .formats = AIC23_FORMATS,},
428 .stream_name = "Capture",
431 .rates = AIC23_RATES,
432 .formats = AIC23_FORMATS,},
434 .prepare = tlv320aic23_pcm_prepare,
435 .hw_params = tlv320aic23_hw_params,
436 .shutdown = tlv320aic23_shutdown,
439 .digital_mute = tlv320aic23_mute,
440 .set_fmt = tlv320aic23_set_dai_fmt,
441 .set_sysclk = tlv320aic23_set_dai_sysclk,
444 EXPORT_SYMBOL_GPL(tlv320aic23_dai);
446 static int tlv320aic23_suspend(struct platform_device *pdev,
449 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
450 struct snd_soc_codec *codec = socdev->codec;
452 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
453 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
458 static int tlv320aic23_resume(struct platform_device *pdev)
460 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
461 struct snd_soc_codec *codec = socdev->codec;
465 /* Sync reg_cache with the hardware */
466 for (reg = 0; reg < ARRAY_SIZE(tlv320aic23_reg); i++) {
467 u16 val = tlv320aic23_read_reg_cache(codec, reg);
468 tlv320aic23_write(codec, reg, val);
471 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
472 tlv320aic23_set_bias_level(codec, codec->suspend_bias_level);
478 * initialise the AIC23 driver
479 * register the mixer and dsp interfaces with the kernel
481 static int tlv320aic23_init(struct snd_soc_device *socdev)
483 struct snd_soc_codec *codec = socdev->codec;
487 codec->name = "tlv320aic23";
488 codec->owner = THIS_MODULE;
489 codec->read = tlv320aic23_read_reg_cache;
490 codec->write = tlv320aic23_write;
491 codec->set_bias_level = tlv320aic23_set_bias_level;
492 codec->dai = &tlv320aic23_dai;
494 codec->reg_cache_size = ARRAY_SIZE(tlv320aic23_reg);
496 kmemdup(tlv320aic23_reg, sizeof(tlv320aic23_reg), GFP_KERNEL);
497 if (codec->reg_cache == NULL)
501 tlv320aic23_write(codec, TLV320AIC23_RESET, 0);
504 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
506 printk(KERN_ERR "tlv320aic23: failed to create pcms\n");
510 /* power on device */
511 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
513 tlv320aic23_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
516 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_LINVOL);
517 tlv320aic23_write(codec, TLV320AIC23_LINVOL,
518 (reg & (~TLV320AIC23_LIM_MUTED)) |
519 (TLV320AIC23_LRS_ENABLED));
521 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_RINVOL);
522 tlv320aic23_write(codec, TLV320AIC23_RINVOL,
523 (reg & (~TLV320AIC23_LIM_MUTED)) |
524 TLV320AIC23_LRS_ENABLED);
526 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG);
527 tlv320aic23_write(codec, TLV320AIC23_ANLG,
528 (reg) & (~TLV320AIC23_BYPASS_ON) &
529 (~TLV320AIC23_MICM_MUTED));
531 /* Default output volume */
532 tlv320aic23_write(codec, TLV320AIC23_LCHNVOL,
533 TLV320AIC23_DEFAULT_OUT_VOL &
534 TLV320AIC23_OUT_VOL_MASK);
535 tlv320aic23_write(codec, TLV320AIC23_RCHNVOL,
536 TLV320AIC23_DEFAULT_OUT_VOL &
537 TLV320AIC23_OUT_VOL_MASK);
539 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x1);
541 tlv320aic23_add_controls(codec);
542 tlv320aic23_add_widgets(codec);
543 ret = snd_soc_register_card(socdev);
545 printk(KERN_ERR "tlv320aic23: failed to register card\n");
552 snd_soc_free_pcms(socdev);
553 snd_soc_dapm_free(socdev);
555 kfree(codec->reg_cache);
558 static struct snd_soc_device *tlv320aic23_socdev;
560 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
562 * If the i2c layer weren't so broken, we could pass this kind of data
565 static int tlv320aic23_codec_probe(struct i2c_client *i2c,
566 const struct i2c_device_id *i2c_id)
568 struct snd_soc_device *socdev = tlv320aic23_socdev;
569 struct snd_soc_codec *codec = socdev->codec;
572 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
575 i2c_set_clientdata(i2c, codec);
576 codec->control_data = i2c;
578 ret = tlv320aic23_init(socdev);
580 printk(KERN_ERR "tlv320aic23: failed to initialise AIC23\n");
590 static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c)
592 put_device(&i2c->dev);
596 static const struct i2c_device_id tlv320aic23_id[] = {
601 MODULE_DEVICE_TABLE(i2c, tlv320aic23_id);
603 static struct i2c_driver tlv320aic23_i2c_driver = {
605 .name = "tlv320aic23",
607 .probe = tlv320aic23_codec_probe,
608 .remove = __exit_p(tlv320aic23_i2c_remove),
609 .id_table = tlv320aic23_id,
614 static int tlv320aic23_probe(struct platform_device *pdev)
616 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
617 struct snd_soc_codec *codec;
620 printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION);
622 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
626 socdev->codec = codec;
627 mutex_init(&codec->mutex);
628 INIT_LIST_HEAD(&codec->dapm_widgets);
629 INIT_LIST_HEAD(&codec->dapm_paths);
631 tlv320aic23_socdev = socdev;
632 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
633 codec->hw_write = (hw_write_t) i2c_smbus_write_byte_data;
634 codec->hw_read = NULL;
635 ret = i2c_add_driver(&tlv320aic23_i2c_driver);
637 printk(KERN_ERR "can't add i2c driver");
642 static int tlv320aic23_remove(struct platform_device *pdev)
644 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
645 struct snd_soc_codec *codec = socdev->codec;
647 if (codec->control_data)
648 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
650 snd_soc_free_pcms(socdev);
651 snd_soc_dapm_free(socdev);
652 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
653 i2c_del_driver(&tlv320aic23_i2c_driver);
655 kfree(codec->reg_cache);
660 struct snd_soc_codec_device soc_codec_dev_tlv320aic23 = {
661 .probe = tlv320aic23_probe,
662 .remove = tlv320aic23_remove,
663 .suspend = tlv320aic23_suspend,
664 .resume = tlv320aic23_resume,
666 EXPORT_SYMBOL_GPL(soc_codec_dev_tlv320aic23);
668 MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
669 MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
670 MODULE_LICENSE("GPL");