21 #include <linux/module.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
40 static const u16 tlv320aic23_reg[] = {
41 0x0097, 0x0097, 0x00F9, 0x00F9,
42 0x001A, 0x0004, 0x0007, 0x0001,
43 0x0020, 0x0000, 0x0000, 0x0000,
44 0x0000, 0x0000, 0x0000, 0x0000,
47 static const char *rec_src_text[] = {
"Line",
"Mic" };
48 static const char *deemph_text[] = {
"None",
"32Khz",
"44.1Khz",
"48Khz"};
50 static const struct soc_enum rec_src_enum =
56 static const struct soc_enum tlv320aic23_rec_src =
58 static const struct soc_enum tlv320aic23_deemph =
65 static int snd_soc_tlv320aic23_put_volsw(
struct snd_kcontrol *kcontrol,
71 val = (ucontrol->
value.integer.value[0] & 0x07);
80 val = (val >= 4) ? 4 : (3 - val);
88 static int snd_soc_tlv320aic23_get_volsw(
struct snd_kcontrol *kcontrol,
96 val = (val >= 4) ? 4 : (3 - val);
97 ucontrol->
value.integer.value[0] =
val;
113 snd_soc_tlv320aic23_get_volsw,
114 snd_soc_tlv320aic23_put_volsw, sidetone_vol_tlv),
115 SOC_ENUM(
"Playback De-emphasis", tlv320aic23_deemph),
119 static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
129 &tlv320aic23_rec_src_mux_controls),
131 &tlv320aic23_output_mixer_controls[0],
132 ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
149 {
"Output Mixer",
"Line Bypass Switch",
"Line Input"},
150 {
"Output Mixer",
"Playback Switch",
"DAC"},
151 {
"Output Mixer",
"Mic Sidetone Switch",
"Mic Input"},
154 {
"RHPOUT",
NULL,
"Output Mixer"},
155 {
"LHPOUT",
NULL,
"Output Mixer"},
156 {
"LOUT",
NULL,
"Output Mixer"},
157 {
"ROUT",
NULL,
"Output Mixer"},
160 {
"Line Input",
"NULL",
"LLINEIN"},
161 {
"Line Input",
"NULL",
"RLINEIN"},
163 {
"Mic Input",
"NULL",
"MICIN"},
166 {
"Capture Source",
"Line",
"Line Input"},
167 {
"Capture Source",
"Mic",
"Mic Input"},
168 {
"ADC",
NULL,
"Capture Source"},
193 static const int bosr_usb_divisor_table[] = {
196 #define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
197 #define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15))
198 static const unsigned short sr_valid_mask[] = {
207 #define SR_MULT (11*12)
208 #define A(x) (SR_MULT/x)
209 static const unsigned char sr_adc_mult_table[] = {
210 A(2),
A(2),
A(12),
A(12), 0, 0,
A(3),
A(1),
211 A(2),
A(2),
A(11),
A(11), 0, 0, 0,
A(1)
213 static const unsigned char sr_dac_mult_table[] = {
214 A(2),
A(12),
A(2),
A(12), 0, 0,
A(3),
A(1),
215 A(2),
A(11),
A(2),
A(11), 0, 0, 0,
A(1)
218 static unsigned get_score(
int adc,
int adc_l,
int adc_h,
int need_adc,
219 int dac,
int dac_l,
int dac_h,
int need_dac)
221 if ((adc >= adc_l) && (adc <= adc_h) &&
222 (dac >= dac_l) && (dac <= dac_h)) {
223 int diff_adc = need_adc -
adc;
224 int diff_dac = need_dac - dac;
225 return abs(diff_adc) +
abs(diff_dac);
230 static int find_rate(
int mclk,
u32 need_adc,
u32 need_dac)
237 int adc_l, adc_h, dac_l, dac_h;
244 adc_l = need_adc - (need_adc >> 5);
245 adc_h = need_adc + (need_adc >> 5);
246 dac_l = need_dac - (need_dac >> 5);
247 dac_h = need_dac + (need_dac >> 5);
248 for (i = 0; i <
ARRAY_SIZE(bosr_usb_divisor_table); i++) {
249 int base = mclk / bosr_usb_divisor_table[
i];
250 int mask = sr_valid_mask[
i];
251 for (j = 0; j <
ARRAY_SIZE(sr_adc_mult_table);
258 adc = base * sr_adc_mult_table[
j];
259 dac = base * sr_dac_mult_table[
j];
260 score = get_score(adc, adc_l, adc_h, need_adc,
261 dac, dac_l, dac_h, need_dac);
262 if (best_score > score) {
268 score = get_score((adc >> 1), adc_l, adc_h, need_adc,
269 (dac >> 1), dac_l, dac_h, need_dac);
271 if ((score !=
UINT_MAX) && (best_score >= score)) {
283 static void get_current_sample_rates(
struct snd_soc_codec *codec,
int mclk,
284 u32 *sample_rate_adc,
u32 *sample_rate_dac)
287 int sr = (src >> 2) & 0x0f;
288 int val = (mclk / bosr_usb_divisor_table[src & 3]);
289 int adc = (val * sr_adc_mult_table[
sr]) /
SR_MULT;
290 int dac = (val * sr_dac_mult_table[
sr]) /
SR_MULT;
295 *sample_rate_adc =
adc;
296 *sample_rate_dac = dac;
300 static int set_sample_rate_control(
struct snd_soc_codec *codec,
int mclk,
301 u32 sample_rate_adc,
u32 sample_rate_dac)
304 int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
307 __func__, sample_rate_adc, sample_rate_dac);
314 get_current_sample_rates(codec, mclk, &adc, &dac);
329 struct aic23 *
aic23 = snd_soc_codec_get_drvdata(codec);
336 if (!sample_rate_adc)
337 sample_rate_adc = sample_rate;
340 if (!sample_rate_dac)
341 sample_rate_dac = sample_rate;
343 ret = set_sample_rate_control(codec, aic23->
mclk, sample_rate_adc,
354 iface_reg |= (0x01 << 2);
357 iface_reg |= (0x02 << 2);
360 iface_reg |= (0x03 << 2);
383 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
413 static int tlv320aic23_set_dai_fmt(
struct snd_soc_dai *codec_dai,
459 static int tlv320aic23_set_dai_sysclk(
struct snd_soc_dai *codec_dai,
460 int clk_id,
unsigned int freq,
int dir)
462 struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
467 static int tlv320aic23_set_bias_level(
struct snd_soc_codec *codec,
476 TLV320AIC23_DAC_OFF);
496 #define AIC23_RATES SNDRV_PCM_RATE_8000_96000
497 #define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
498 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
501 .prepare = tlv320aic23_pcm_prepare,
502 .hw_params = tlv320aic23_hw_params,
503 .shutdown = tlv320aic23_shutdown,
504 .digital_mute = tlv320aic23_mute,
505 .set_fmt = tlv320aic23_set_dai_fmt,
506 .set_sysclk = tlv320aic23_set_dai_sysclk,
510 .name =
"tlv320aic23-hifi",
512 .stream_name =
"Playback",
518 .stream_name =
"Capture",
523 .ops = &tlv320aic23_dai_ops,
543 struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
548 dev_err(codec->
dev,
"Failed to set cache I/O: %d\n", ret);
602 .reg_cache_size =
ARRAY_SIZE(tlv320aic23_reg),
603 .reg_word_size =
sizeof(
u16),
605 .
probe = tlv320aic23_probe,
606 .
remove = tlv320aic23_remove,
607 .
suspend = tlv320aic23_suspend,
608 .
resume = tlv320aic23_resume,
612 .dapm_routes = tlv320aic23_intercon,
613 .num_dapm_routes =
ARRAY_SIZE(tlv320aic23_intercon),
616 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
621 static int tlv320aic23_codec_probe(
struct i2c_client *i2c,
634 i2c_set_clientdata(i2c, aic23);
638 &soc_codec_dev_tlv320aic23, &tlv320aic23_dai, 1);
654 static struct i2c_driver tlv320aic23_i2c_driver = {
656 .name =
"tlv320aic23-codec",
658 .probe = tlv320aic23_codec_probe,
659 .remove =
__exit_p(tlv320aic23_i2c_remove),
660 .id_table = tlv320aic23_id,
665 static int __init tlv320aic23_modinit(
void)
668 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
669 ret = i2c_add_driver(&tlv320aic23_i2c_driver);
671 printk(
KERN_ERR "Failed to register TLV320AIC23 I2C driver: %d\n",
679 static void __exit tlv320aic23_exit(
void)
681 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)