1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/pm.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>
31
32#include "tlv320aic23.h"
33
34
35
36
37static const struct reg_default tlv320aic23_reg[] = {
38 { 0, 0x0097 },
39 { 1, 0x0097 },
40 { 2, 0x00F9 },
41 { 3, 0x00F9 },
42 { 4, 0x001A },
43 { 5, 0x0004 },
44 { 6, 0x0007 },
45 { 7, 0x0001 },
46 { 8, 0x0020 },
47 { 9, 0x0000 },
48};
49
50const struct regmap_config tlv320aic23_regmap = {
51 .reg_bits = 7,
52 .val_bits = 9,
53
54 .max_register = TLV320AIC23_RESET,
55 .reg_defaults = tlv320aic23_reg,
56 .num_reg_defaults = ARRAY_SIZE(tlv320aic23_reg),
57 .cache_type = REGCACHE_RBTREE,
58};
59EXPORT_SYMBOL(tlv320aic23_regmap);
60
61static const char *rec_src_text[] = { "Line", "Mic" };
62static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
63
64static SOC_ENUM_SINGLE_DECL(rec_src_enum,
65 TLV320AIC23_ANLG, 2, rec_src_text);
66
67static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
68SOC_DAPM_ENUM("Input Select", rec_src_enum);
69
70static SOC_ENUM_SINGLE_DECL(tlv320aic23_rec_src,
71 TLV320AIC23_ANLG, 2, rec_src_text);
72static SOC_ENUM_SINGLE_DECL(tlv320aic23_deemph,
73 TLV320AIC23_DIGT, 1, deemph_text);
74
75static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
76static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
77static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
78
79static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol,
80 struct snd_ctl_elem_value *ucontrol)
81{
82 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
83 u16 val, reg;
84
85 val = (ucontrol->value.integer.value[0] & 0x07);
86
87
88
89
90
91
92
93
94 val = (val >= 4) ? 4 : (3 - val);
95
96 reg = snd_soc_component_read32(component, TLV320AIC23_ANLG) & (~0x1C0);
97 snd_soc_component_write(component, TLV320AIC23_ANLG, reg | (val << 6));
98
99 return 0;
100}
101
102static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol,
103 struct snd_ctl_elem_value *ucontrol)
104{
105 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
106 u16 val;
107
108 val = snd_soc_component_read32(component, TLV320AIC23_ANLG) & (0x1C0);
109 val = val >> 6;
110 val = (val >= 4) ? 4 : (3 - val);
111 ucontrol->value.integer.value[0] = val;
112 return 0;
113
114}
115
116static 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),
130};
131
132
133static 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),
137};
138
139static 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),
149
150 SND_SOC_DAPM_OUTPUT("LHPOUT"),
151 SND_SOC_DAPM_OUTPUT("RHPOUT"),
152 SND_SOC_DAPM_OUTPUT("LOUT"),
153 SND_SOC_DAPM_OUTPUT("ROUT"),
154
155 SND_SOC_DAPM_INPUT("LLINEIN"),
156 SND_SOC_DAPM_INPUT("RLINEIN"),
157
158 SND_SOC_DAPM_INPUT("MICIN"),
159};
160
161static const struct snd_soc_dapm_route tlv320aic23_intercon[] = {
162
163 {"Output Mixer", "Line Bypass Switch", "Line Input"},
164 {"Output Mixer", "Playback Switch", "DAC"},
165 {"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
166
167
168 {"RHPOUT", NULL, "Output Mixer"},
169 {"LHPOUT", NULL, "Output Mixer"},
170 {"LOUT", NULL, "Output Mixer"},
171 {"ROUT", NULL, "Output Mixer"},
172
173
174 {"Line Input", NULL, "LLINEIN"},
175 {"Line Input", NULL, "RLINEIN"},
176 {"Mic Input", NULL, "MICIN"},
177
178
179 {"Capture Source", "Line", "Line Input"},
180 {"Capture Source", "Mic", "Mic Input"},
181 {"ADC", NULL, "Capture Source"},
182
183};
184
185
186struct aic23 {
187 struct regmap *regmap;
188 int mclk;
189 int requested_adc;
190 int requested_dac;
191};
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206static const int bosr_usb_divisor_table[] = {
207 128, 125, 192, 136
208};
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))
211static const unsigned short sr_valid_mask[] = {
212 LOWER_GROUP|UPPER_GROUP,
213 LOWER_GROUP,
214 LOWER_GROUP|UPPER_GROUP,
215 UPPER_GROUP,
216};
217
218
219
220#define SR_MULT (11*12)
221#define A(x) (SR_MULT/x)
222static 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)
225};
226static 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)
229};
230
231static 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)
233{
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);
239 }
240 return UINT_MAX;
241}
242
243static int find_rate(int mclk, u32 need_adc, u32 need_dac)
244{
245 int i, j;
246 int best_i = -1;
247 int best_j = -1;
248 int best_div = 0;
249 unsigned best_score = UINT_MAX;
250 int adc_l, adc_h, dac_l, dac_h;
251
252 need_adc *= SR_MULT;
253 need_dac *= SR_MULT;
254
255
256
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);
265 j++, mask >>= 1) {
266 int adc;
267 int dac;
268 int score;
269 if ((mask & 1) == 0)
270 continue;
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) {
276 best_score = score;
277 best_i = i;
278 best_j = j;
279 best_div = 0;
280 }
281 score = get_score((adc >> 1), adc_l, adc_h, need_adc,
282 (dac >> 1), dac_l, dac_h, need_dac);
283
284 if ((score != UINT_MAX) && (best_score >= score)) {
285 best_score = score;
286 best_i = i;
287 best_j = j;
288 best_div = 1;
289 }
290 }
291 }
292 return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
293}
294
295#ifdef DEBUG
296static void get_current_sample_rates(struct snd_soc_component *component, int mclk,
297 u32 *sample_rate_adc, u32 *sample_rate_dac)
298{
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) {
305 adc >>= 1;
306 dac >>= 1;
307 }
308 *sample_rate_adc = adc;
309 *sample_rate_dac = dac;
310}
311#endif
312
313static int set_sample_rate_control(struct snd_soc_component *component, int mclk,
314 u32 sample_rate_adc, u32 sample_rate_dac)
315{
316
317 int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
318 if (data < 0) {
319 printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
320 __func__, sample_rate_adc, sample_rate_dac);
321 return -EINVAL;
322 }
323 snd_soc_component_write(component, TLV320AIC23_SRATE, data);
324#ifdef DEBUG
325 {
326 u32 adc, dac;
327 get_current_sample_rates(component, mclk, &adc, &dac);
328 printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
329 adc, dac, data);
330 }
331#endif
332 return 0;
333}
334
335static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
336 struct snd_pcm_hw_params *params,
337 struct snd_soc_dai *dai)
338{
339 struct snd_soc_component *component = dai->component;
340 u16 iface_reg;
341 int ret;
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);
346
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;
351 } else {
352 aic23->requested_adc = sample_rate_adc = sample_rate;
353 if (!sample_rate_dac)
354 sample_rate_dac = sample_rate;
355 }
356 ret = set_sample_rate_control(component, aic23->mclk, sample_rate_adc,
357 sample_rate_dac);
358 if (ret < 0)
359 return ret;
360
361 iface_reg = snd_soc_component_read32(component, TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
362
363 switch (params_width(params)) {
364 case 16:
365 break;
366 case 20:
367 iface_reg |= (0x01 << 2);
368 break;
369 case 24:
370 iface_reg |= (0x02 << 2);
371 break;
372 case 32:
373 iface_reg |= (0x03 << 2);
374 break;
375 }
376 snd_soc_component_write(component, TLV320AIC23_DIGT_FMT, iface_reg);
377
378 return 0;
379}
380
381static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
382 struct snd_soc_dai *dai)
383{
384 struct snd_soc_component *component = dai->component;
385
386
387 snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0001);
388
389 return 0;
390}
391
392static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
393 struct snd_soc_dai *dai)
394{
395 struct snd_soc_component *component = dai->component;
396 struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
397
398
399 if (!snd_soc_component_is_active(component)) {
400 udelay(50);
401 snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0);
402 }
403 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
404 aic23->requested_dac = 0;
405 else
406 aic23->requested_adc = 0;
407}
408
409static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
410{
411 struct snd_soc_component *component = dai->component;
412 u16 reg;
413
414 reg = snd_soc_component_read32(component, TLV320AIC23_DIGT);
415 if (mute)
416 reg |= TLV320AIC23_DACM_MUTE;
417
418 else
419 reg &= ~TLV320AIC23_DACM_MUTE;
420
421 snd_soc_component_write(component, TLV320AIC23_DIGT, reg);
422
423 return 0;
424}
425
426static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
427 unsigned int fmt)
428{
429 struct snd_soc_component *component = codec_dai->component;
430 u16 iface_reg;
431
432 iface_reg = snd_soc_component_read32(component, TLV320AIC23_DIGT_FMT) & (~0x03);
433
434
435 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
436 case SND_SOC_DAIFMT_CBM_CFM:
437 iface_reg |= TLV320AIC23_MS_MASTER;
438 break;
439 case SND_SOC_DAIFMT_CBS_CFS:
440 iface_reg &= ~TLV320AIC23_MS_MASTER;
441 break;
442 default:
443 return -EINVAL;
444
445 }
446
447
448 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
449 case SND_SOC_DAIFMT_I2S:
450 iface_reg |= TLV320AIC23_FOR_I2S;
451 break;
452 case SND_SOC_DAIFMT_DSP_A:
453 iface_reg |= TLV320AIC23_LRP_ON;
454
455 case SND_SOC_DAIFMT_DSP_B:
456 iface_reg |= TLV320AIC23_FOR_DSP;
457 break;
458 case SND_SOC_DAIFMT_RIGHT_J:
459 break;
460 case SND_SOC_DAIFMT_LEFT_J:
461 iface_reg |= TLV320AIC23_FOR_LJUST;
462 break;
463 default:
464 return -EINVAL;
465
466 }
467
468 snd_soc_component_write(component, TLV320AIC23_DIGT_FMT, iface_reg);
469
470 return 0;
471}
472
473static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
474 int clk_id, unsigned int freq, int dir)
475{
476 struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
477 aic23->mclk = freq;
478 return 0;
479}
480
481static int tlv320aic23_set_bias_level(struct snd_soc_component *component,
482 enum snd_soc_bias_level level)
483{
484 u16 reg = snd_soc_component_read32(component, TLV320AIC23_PWR) & 0x17f;
485
486 switch (level) {
487 case SND_SOC_BIAS_ON:
488
489 reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
490 TLV320AIC23_DAC_OFF);
491 snd_soc_component_write(component, TLV320AIC23_PWR, reg);
492 break;
493 case SND_SOC_BIAS_PREPARE:
494 break;
495 case SND_SOC_BIAS_STANDBY:
496
497 snd_soc_component_write(component, TLV320AIC23_PWR,
498 reg | TLV320AIC23_CLK_OFF);
499 break;
500 case SND_SOC_BIAS_OFF:
501
502 snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0);
503 snd_soc_component_write(component, TLV320AIC23_PWR, 0x1ff);
504 break;
505 }
506 return 0;
507}
508
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)
512
513static 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,
520};
521
522static struct snd_soc_dai_driver tlv320aic23_dai = {
523 .name = "tlv320aic23-hifi",
524 .playback = {
525 .stream_name = "Playback",
526 .channels_min = 2,
527 .channels_max = 2,
528 .rates = AIC23_RATES,
529 .formats = AIC23_FORMATS,},
530 .capture = {
531 .stream_name = "Capture",
532 .channels_min = 2,
533 .channels_max = 2,
534 .rates = AIC23_RATES,
535 .formats = AIC23_FORMATS,},
536 .ops = &tlv320aic23_dai_ops,
537};
538
539static int tlv320aic23_resume(struct snd_soc_component *component)
540{
541 struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
542 regcache_mark_dirty(aic23->regmap);
543 regcache_sync(aic23->regmap);
544
545 return 0;
546}
547
548static int tlv320aic23_component_probe(struct snd_soc_component *component)
549{
550
551 snd_soc_component_write(component, TLV320AIC23_RESET, 0);
552
553 snd_soc_component_write(component, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
554
555
556 snd_soc_component_update_bits(component, TLV320AIC23_LINVOL,
557 TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
558
559 snd_soc_component_update_bits(component, TLV320AIC23_RINVOL,
560 TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
561
562 snd_soc_component_update_bits(component, TLV320AIC23_ANLG,
563 TLV320AIC23_BYPASS_ON | TLV320AIC23_MICM_MUTED,
564 0);
565
566
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);
571
572 snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x1);
573
574 return 0;
575}
576
577static 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,
588 .idle_bias_on = 1,
589 .use_pmdown_time = 1,
590 .endianness = 1,
591 .non_legacy_dai_naming = 1,
592};
593
594int tlv320aic23_probe(struct device *dev, struct regmap *regmap)
595{
596 struct aic23 *aic23;
597
598 if (IS_ERR(regmap))
599 return PTR_ERR(regmap);
600
601 aic23 = devm_kzalloc(dev, sizeof(struct aic23), GFP_KERNEL);
602 if (aic23 == NULL)
603 return -ENOMEM;
604
605 aic23->regmap = regmap;
606
607 dev_set_drvdata(dev, aic23);
608
609 return devm_snd_soc_register_component(dev,
610 &soc_component_dev_tlv320aic23,
611 &tlv320aic23_dai, 1);
612}
613EXPORT_SYMBOL(tlv320aic23_probe);
614
615MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
616MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
617MODULE_LICENSE("GPL");
618