1 // SPDX-License-Identifier: GPL-2.0-only
3 * ALSA SoC TLV320AIC23 codec driver
5 * Author: Arun KS, <arunks@mistralsolutions.com>
6 * Copyright: (C) 2008 Mistral Solutions Pvt Ltd.,
8 * Based on sound/soc/codecs/wm8731.c by Richard Purdie
11 * The AIC23 is a driver for a low power stereo audio
14 * The machine layer should disable unsupported inputs/outputs by
15 * snd_soc_dapm_disable_pin(codec, "LHPOUT"), etc.
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/tlv.h>
30 #include <sound/initval.h>
32 #include "tlv320aic23.h"
35 * AIC23 register cache
37 static const struct reg_default tlv320aic23_reg[] = {
50 const struct regmap_config tlv320aic23_regmap = {
54 .max_register = TLV320AIC23_RESET,
55 .reg_defaults = tlv320aic23_reg,
56 .num_reg_defaults = ARRAY_SIZE(tlv320aic23_reg),
57 .cache_type = REGCACHE_RBTREE,
59 EXPORT_SYMBOL(tlv320aic23_regmap);
61 static const char *rec_src_text[] = { "Line", "Mic" };
62 static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
64 static SOC_ENUM_SINGLE_DECL(rec_src_enum,
65 TLV320AIC23_ANLG, 2, rec_src_text);
67 static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
68 SOC_DAPM_ENUM("Input Select", rec_src_enum);
70 static SOC_ENUM_SINGLE_DECL(tlv320aic23_rec_src,
71 TLV320AIC23_ANLG, 2, rec_src_text);
72 static SOC_ENUM_SINGLE_DECL(tlv320aic23_deemph,
73 TLV320AIC23_DIGT, 1, deemph_text);
75 static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
76 static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
77 static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
79 static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol,
80 struct snd_ctl_elem_value *ucontrol)
82 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
85 val = (ucontrol->value.integer.value[0] & 0x07);
87 /* linear conversion to userspace
94 val = (val >= 4) ? 4 : (3 - val);
96 reg = snd_soc_component_read32(component, TLV320AIC23_ANLG) & (~0x1C0);
97 snd_soc_component_write(component, TLV320AIC23_ANLG, reg | (val << 6));
102 static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol,
103 struct snd_ctl_elem_value *ucontrol)
105 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
108 val = snd_soc_component_read32(component, TLV320AIC23_ANLG) & (0x1C0);
110 val = (val >= 4) ? 4 : (3 - val);
111 ucontrol->value.integer.value[0] = val;
116 static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
117 SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
118 TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
119 SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
120 SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
121 TLV320AIC23_RINVOL, 7, 1, 0),
122 SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
123 TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
124 SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
125 SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
126 SOC_SINGLE_EXT_TLV("Sidetone Volume", TLV320AIC23_ANLG, 6, 4, 0,
127 snd_soc_tlv320aic23_get_volsw,
128 snd_soc_tlv320aic23_put_volsw, sidetone_vol_tlv),
129 SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
132 /* PGA Mixer controls for Line and Mic switch */
133 static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
134 SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
135 SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
136 SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
139 static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
140 SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
141 SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
142 SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
143 &tlv320aic23_rec_src_mux_controls),
144 SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
145 &tlv320aic23_output_mixer_controls[0],
146 ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
147 SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
148 SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
150 SND_SOC_DAPM_OUTPUT("LHPOUT"),
151 SND_SOC_DAPM_OUTPUT("RHPOUT"),
152 SND_SOC_DAPM_OUTPUT("LOUT"),
153 SND_SOC_DAPM_OUTPUT("ROUT"),
155 SND_SOC_DAPM_INPUT("LLINEIN"),
156 SND_SOC_DAPM_INPUT("RLINEIN"),
158 SND_SOC_DAPM_INPUT("MICIN"),
161 static const struct snd_soc_dapm_route tlv320aic23_intercon[] = {
163 {"Output Mixer", "Line Bypass Switch", "Line Input"},
164 {"Output Mixer", "Playback Switch", "DAC"},
165 {"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
168 {"RHPOUT", NULL, "Output Mixer"},
169 {"LHPOUT", NULL, "Output Mixer"},
170 {"LOUT", NULL, "Output Mixer"},
171 {"ROUT", NULL, "Output Mixer"},
174 {"Line Input", NULL, "LLINEIN"},
175 {"Line Input", NULL, "RLINEIN"},
176 {"Mic Input", NULL, "MICIN"},
179 {"Capture Source", "Line", "Line Input"},
180 {"Capture Source", "Mic", "Mic Input"},
181 {"ADC", NULL, "Capture Source"},
185 /* AIC23 driver data */
187 struct regmap *regmap;
194 * Common Crystals used
195 * 11.2896 Mhz /128 = *88.2k /192 = 58.8k
196 * 12.0000 Mhz /125 = *96k /136 = 88.235K
197 * 12.2880 Mhz /128 = *96k /192 = 64k
198 * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
199 * 18.4320 Mhz /128 = 144k /192 = *96k
203 * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
204 * USB BOSR 0-250/2 = 125, 1-272/2 = 136
206 static const int bosr_usb_divisor_table[] = {
209 #define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
210 #define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15))
211 static const unsigned short sr_valid_mask[] = {
212 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/
213 LOWER_GROUP, /* Usb, bosr - 0*/
214 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/
215 UPPER_GROUP, /* Usb, bosr - 1*/
218 * Every divisor is a factor of 11*12
220 #define SR_MULT (11*12)
221 #define A(x) (SR_MULT/x)
222 static const unsigned char sr_adc_mult_table[] = {
223 A(2), A(2), A(12), A(12), 0, 0, A(3), A(1),
224 A(2), A(2), A(11), A(11), 0, 0, 0, A(1)
226 static const unsigned char sr_dac_mult_table[] = {
227 A(2), A(12), A(2), A(12), 0, 0, A(3), A(1),
228 A(2), A(11), A(2), A(11), 0, 0, 0, A(1)
231 static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
232 int dac, int dac_l, int dac_h, int need_dac)
234 if ((adc >= adc_l) && (adc <= adc_h) &&
235 (dac >= dac_l) && (dac <= dac_h)) {
236 int diff_adc = need_adc - adc;
237 int diff_dac = need_dac - dac;
238 return abs(diff_adc) + abs(diff_dac);
243 static int find_rate(int mclk, u32 need_adc, u32 need_dac)
249 unsigned best_score = UINT_MAX;
250 int adc_l, adc_h, dac_l, dac_h;
255 * rates given are +/- 1/32
257 adc_l = need_adc - (need_adc >> 5);
258 adc_h = need_adc + (need_adc >> 5);
259 dac_l = need_dac - (need_dac >> 5);
260 dac_h = need_dac + (need_dac >> 5);
261 for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) {
262 int base = mclk / bosr_usb_divisor_table[i];
263 int mask = sr_valid_mask[i];
264 for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table);
271 adc = base * sr_adc_mult_table[j];
272 dac = base * sr_dac_mult_table[j];
273 score = get_score(adc, adc_l, adc_h, need_adc,
274 dac, dac_l, dac_h, need_dac);
275 if (best_score > score) {
281 score = get_score((adc >> 1), adc_l, adc_h, need_adc,
282 (dac >> 1), dac_l, dac_h, need_dac);
283 /* prefer to have a /2 */
284 if ((score != UINT_MAX) && (best_score >= score)) {
292 return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
296 static void get_current_sample_rates(struct snd_soc_component *component, int mclk,
297 u32 *sample_rate_adc, u32 *sample_rate_dac)
299 int src = snd_soc_component_read32(component, TLV320AIC23_SRATE);
300 int sr = (src >> 2) & 0x0f;
301 int val = (mclk / bosr_usb_divisor_table[src & 3]);
302 int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
303 int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
304 if (src & TLV320AIC23_CLKIN_HALF) {
308 *sample_rate_adc = adc;
309 *sample_rate_dac = dac;
313 static int set_sample_rate_control(struct snd_soc_component *component, int mclk,
314 u32 sample_rate_adc, u32 sample_rate_dac)
316 /* Search for the right sample rate */
317 int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
319 printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
320 __func__, sample_rate_adc, sample_rate_dac);
323 snd_soc_component_write(component, TLV320AIC23_SRATE, data);
327 get_current_sample_rates(component, mclk, &adc, &dac);
328 printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
335 static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
336 struct snd_pcm_hw_params *params,
337 struct snd_soc_dai *dai)
339 struct snd_soc_component *component = dai->component;
342 struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
343 u32 sample_rate_adc = aic23->requested_adc;
344 u32 sample_rate_dac = aic23->requested_dac;
345 u32 sample_rate = params_rate(params);
347 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
348 aic23->requested_dac = sample_rate_dac = sample_rate;
349 if (!sample_rate_adc)
350 sample_rate_adc = sample_rate;
352 aic23->requested_adc = sample_rate_adc = sample_rate;
353 if (!sample_rate_dac)
354 sample_rate_dac = sample_rate;
356 ret = set_sample_rate_control(component, aic23->mclk, sample_rate_adc,
361 iface_reg = snd_soc_component_read32(component, TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
363 switch (params_width(params)) {
367 iface_reg |= (0x01 << 2);
370 iface_reg |= (0x02 << 2);
373 iface_reg |= (0x03 << 2);
376 snd_soc_component_write(component, TLV320AIC23_DIGT_FMT, iface_reg);
381 static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
382 struct snd_soc_dai *dai)
384 struct snd_soc_component *component = dai->component;
387 snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0001);
392 static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
393 struct snd_soc_dai *dai)
395 struct snd_soc_component *component = dai->component;
396 struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
399 if (!snd_soc_component_is_active(component)) {
401 snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0);
403 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
404 aic23->requested_dac = 0;
406 aic23->requested_adc = 0;
409 static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
411 struct snd_soc_component *component = dai->component;
414 reg = snd_soc_component_read32(component, TLV320AIC23_DIGT);
416 reg |= TLV320AIC23_DACM_MUTE;
419 reg &= ~TLV320AIC23_DACM_MUTE;
421 snd_soc_component_write(component, TLV320AIC23_DIGT, reg);
426 static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
429 struct snd_soc_component *component = codec_dai->component;
432 iface_reg = snd_soc_component_read32(component, TLV320AIC23_DIGT_FMT) & (~0x03);
434 /* set master/slave audio interface */
435 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
436 case SND_SOC_DAIFMT_CBM_CFM:
437 iface_reg |= TLV320AIC23_MS_MASTER;
439 case SND_SOC_DAIFMT_CBS_CFS:
440 iface_reg &= ~TLV320AIC23_MS_MASTER;
447 /* interface format */
448 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
449 case SND_SOC_DAIFMT_I2S:
450 iface_reg |= TLV320AIC23_FOR_I2S;
452 case SND_SOC_DAIFMT_DSP_A:
453 iface_reg |= TLV320AIC23_LRP_ON;
455 case SND_SOC_DAIFMT_DSP_B:
456 iface_reg |= TLV320AIC23_FOR_DSP;
458 case SND_SOC_DAIFMT_RIGHT_J:
460 case SND_SOC_DAIFMT_LEFT_J:
461 iface_reg |= TLV320AIC23_FOR_LJUST;
468 snd_soc_component_write(component, TLV320AIC23_DIGT_FMT, iface_reg);
473 static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
474 int clk_id, unsigned int freq, int dir)
476 struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
481 static int tlv320aic23_set_bias_level(struct snd_soc_component *component,
482 enum snd_soc_bias_level level)
484 u16 reg = snd_soc_component_read32(component, TLV320AIC23_PWR) & 0x17f;
487 case SND_SOC_BIAS_ON:
488 /* vref/mid, osc on, dac unmute */
489 reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
490 TLV320AIC23_DAC_OFF);
491 snd_soc_component_write(component, TLV320AIC23_PWR, reg);
493 case SND_SOC_BIAS_PREPARE:
495 case SND_SOC_BIAS_STANDBY:
496 /* everything off except vref/vmid, */
497 snd_soc_component_write(component, TLV320AIC23_PWR,
498 reg | TLV320AIC23_CLK_OFF);
500 case SND_SOC_BIAS_OFF:
501 /* everything off, dac mute, inactive */
502 snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0);
503 snd_soc_component_write(component, TLV320AIC23_PWR, 0x1ff);
509 #define AIC23_RATES SNDRV_PCM_RATE_8000_96000
510 #define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
511 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
513 static const struct snd_soc_dai_ops tlv320aic23_dai_ops = {
514 .prepare = tlv320aic23_pcm_prepare,
515 .hw_params = tlv320aic23_hw_params,
516 .shutdown = tlv320aic23_shutdown,
517 .digital_mute = tlv320aic23_mute,
518 .set_fmt = tlv320aic23_set_dai_fmt,
519 .set_sysclk = tlv320aic23_set_dai_sysclk,
522 static struct snd_soc_dai_driver tlv320aic23_dai = {
523 .name = "tlv320aic23-hifi",
525 .stream_name = "Playback",
528 .rates = AIC23_RATES,
529 .formats = AIC23_FORMATS,},
531 .stream_name = "Capture",
534 .rates = AIC23_RATES,
535 .formats = AIC23_FORMATS,},
536 .ops = &tlv320aic23_dai_ops,
539 static int tlv320aic23_resume(struct snd_soc_component *component)
541 struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
542 regcache_mark_dirty(aic23->regmap);
543 regcache_sync(aic23->regmap);
548 static int tlv320aic23_component_probe(struct snd_soc_component *component)
551 snd_soc_component_write(component, TLV320AIC23_RESET, 0);
553 snd_soc_component_write(component, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
556 snd_soc_component_update_bits(component, TLV320AIC23_LINVOL,
557 TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
559 snd_soc_component_update_bits(component, TLV320AIC23_RINVOL,
560 TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
562 snd_soc_component_update_bits(component, TLV320AIC23_ANLG,
563 TLV320AIC23_BYPASS_ON | TLV320AIC23_MICM_MUTED,
566 /* Default output volume */
567 snd_soc_component_write(component, TLV320AIC23_LCHNVOL,
568 TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
569 snd_soc_component_write(component, TLV320AIC23_RCHNVOL,
570 TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
572 snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x1);
577 static const struct snd_soc_component_driver soc_component_dev_tlv320aic23 = {
578 .probe = tlv320aic23_component_probe,
579 .resume = tlv320aic23_resume,
580 .set_bias_level = tlv320aic23_set_bias_level,
581 .controls = tlv320aic23_snd_controls,
582 .num_controls = ARRAY_SIZE(tlv320aic23_snd_controls),
583 .dapm_widgets = tlv320aic23_dapm_widgets,
584 .num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets),
585 .dapm_routes = tlv320aic23_intercon,
586 .num_dapm_routes = ARRAY_SIZE(tlv320aic23_intercon),
587 .suspend_bias_off = 1,
589 .use_pmdown_time = 1,
591 .non_legacy_dai_naming = 1,
594 int tlv320aic23_probe(struct device *dev, struct regmap *regmap)
599 return PTR_ERR(regmap);
601 aic23 = devm_kzalloc(dev, sizeof(struct aic23), GFP_KERNEL);
605 aic23->regmap = regmap;
607 dev_set_drvdata(dev, aic23);
609 return devm_snd_soc_register_component(dev,
610 &soc_component_dev_tlv320aic23,
611 &tlv320aic23_dai, 1);
613 EXPORT_SYMBOL(tlv320aic23_probe);
615 MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
616 MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
617 MODULE_LICENSE("GPL");