]>
Commit | Line | Data |
---|---|---|
c1f27190 AK |
1 | /* |
2 | * ALSA SoC TLV320AIC23 codec driver | |
3 | * | |
4 | * Author: Arun KS, <arunks@mistralsolutions.com> | |
5 | * Copyright: (C) 2008 Mistral Solutions Pvt Ltd., | |
6 | * | |
7 | * Based on sound/soc/codecs/wm8731.c by Richard Purdie | |
8 | * | |
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. | |
12 | * | |
13 | * Notes: | |
14 | * The AIC23 is a driver for a low power stereo audio | |
15 | * codec tlv320aic23 | |
16 | * | |
17 | * The machine layer should disable unsupported inputs/outputs by | |
18 | * snd_soc_dapm_disable_pin(codec, "LHPOUT"), etc. | |
19 | */ | |
20 | ||
21 | #include <linux/module.h> | |
22 | #include <linux/moduleparam.h> | |
23 | #include <linux/init.h> | |
24 | #include <linux/delay.h> | |
25 | #include <linux/pm.h> | |
4aa11d67 | 26 | #include <linux/regmap.h> |
5a0e3ad6 | 27 | #include <linux/slab.h> |
c1f27190 AK |
28 | #include <sound/core.h> |
29 | #include <sound/pcm.h> | |
30 | #include <sound/pcm_params.h> | |
31 | #include <sound/soc.h> | |
c1f27190 AK |
32 | #include <sound/tlv.h> |
33 | #include <sound/initval.h> | |
34 | ||
35 | #include "tlv320aic23.h" | |
36 | ||
c1f27190 AK |
37 | /* |
38 | * AIC23 register cache | |
39 | */ | |
4aa11d67 MB |
40 | static const struct reg_default tlv320aic23_reg[] = { |
41 | { 0, 0x0097 }, | |
42 | { 1, 0x0097 }, | |
43 | { 2, 0x00F9 }, | |
44 | { 3, 0x00F9 }, | |
45 | { 4, 0x001A }, | |
46 | { 5, 0x0004 }, | |
47 | { 6, 0x0007 }, | |
48 | { 7, 0x0001 }, | |
49 | { 8, 0x0020 }, | |
50 | { 9, 0x0000 }, | |
51 | }; | |
52 | ||
b3fc5725 | 53 | const struct regmap_config tlv320aic23_regmap = { |
4aa11d67 MB |
54 | .reg_bits = 7, |
55 | .val_bits = 9, | |
56 | ||
57 | .max_register = TLV320AIC23_RESET, | |
58 | .reg_defaults = tlv320aic23_reg, | |
59 | .num_reg_defaults = ARRAY_SIZE(tlv320aic23_reg), | |
60 | .cache_type = REGCACHE_RBTREE, | |
c1f27190 | 61 | }; |
40423285 | 62 | EXPORT_SYMBOL(tlv320aic23_regmap); |
c1f27190 | 63 | |
c1f27190 AK |
64 | static const char *rec_src_text[] = { "Line", "Mic" }; |
65 | static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"}; | |
c1f27190 | 66 | |
806057cc TI |
67 | static SOC_ENUM_SINGLE_DECL(rec_src_enum, |
68 | TLV320AIC23_ANLG, 2, rec_src_text); | |
c1f27190 AK |
69 | |
70 | static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls = | |
71 | SOC_DAPM_ENUM("Input Select", rec_src_enum); | |
72 | ||
806057cc TI |
73 | static SOC_ENUM_SINGLE_DECL(tlv320aic23_rec_src, |
74 | TLV320AIC23_ANLG, 2, rec_src_text); | |
75 | static SOC_ENUM_SINGLE_DECL(tlv320aic23_deemph, | |
76 | TLV320AIC23_DIGT, 1, deemph_text); | |
c1f27190 AK |
77 | |
78 | static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0); | |
79 | static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0); | |
df91ddf1 AK |
80 | static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0); |
81 | ||
82 | static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol, | |
83 | struct snd_ctl_elem_value *ucontrol) | |
84 | { | |
ea53bf77 | 85 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
df91ddf1 AK |
86 | u16 val, reg; |
87 | ||
88 | val = (ucontrol->value.integer.value[0] & 0x07); | |
89 | ||
90 | /* linear conversion to userspace | |
91 | * 000 = -6db | |
92 | * 001 = -9db | |
93 | * 010 = -12db | |
94 | * 011 = -18db (Min) | |
95 | * 100 = 0db (Max) | |
96 | */ | |
97 | val = (val >= 4) ? 4 : (3 - val); | |
98 | ||
f9dfbf91 AL |
99 | reg = snd_soc_read(codec, TLV320AIC23_ANLG) & (~0x1C0); |
100 | snd_soc_write(codec, TLV320AIC23_ANLG, reg | (val << 6)); | |
df91ddf1 AK |
101 | |
102 | return 0; | |
103 | } | |
104 | ||
105 | static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol, | |
106 | struct snd_ctl_elem_value *ucontrol) | |
107 | { | |
ea53bf77 | 108 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
df91ddf1 AK |
109 | u16 val; |
110 | ||
f9dfbf91 | 111 | val = snd_soc_read(codec, TLV320AIC23_ANLG) & (0x1C0); |
df91ddf1 AK |
112 | val = val >> 6; |
113 | val = (val >= 4) ? 4 : (3 - val); | |
114 | ucontrol->value.integer.value[0] = val; | |
115 | return 0; | |
116 | ||
117 | } | |
118 | ||
c1f27190 AK |
119 | static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = { |
120 | SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL, | |
121 | TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv), | |
122 | SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1), | |
123 | SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL, | |
124 | TLV320AIC23_RINVOL, 7, 1, 0), | |
125 | SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL, | |
126 | TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv), | |
127 | SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1), | |
128 | SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0), | |
0f9887d1 PU |
129 | SOC_SINGLE_EXT_TLV("Sidetone Volume", TLV320AIC23_ANLG, 6, 4, 0, |
130 | snd_soc_tlv320aic23_get_volsw, | |
131 | snd_soc_tlv320aic23_put_volsw, sidetone_vol_tlv), | |
c1f27190 AK |
132 | SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph), |
133 | }; | |
134 | ||
c1f27190 AK |
135 | /* PGA Mixer controls for Line and Mic switch */ |
136 | static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = { | |
137 | SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0), | |
138 | SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0), | |
139 | SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0), | |
140 | }; | |
141 | ||
142 | static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = { | |
143 | SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1), | |
144 | SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1), | |
145 | SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0, | |
146 | &tlv320aic23_rec_src_mux_controls), | |
147 | SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1, | |
148 | &tlv320aic23_output_mixer_controls[0], | |
149 | ARRAY_SIZE(tlv320aic23_output_mixer_controls)), | |
150 | SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0), | |
151 | SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0), | |
152 | ||
153 | SND_SOC_DAPM_OUTPUT("LHPOUT"), | |
154 | SND_SOC_DAPM_OUTPUT("RHPOUT"), | |
155 | SND_SOC_DAPM_OUTPUT("LOUT"), | |
156 | SND_SOC_DAPM_OUTPUT("ROUT"), | |
157 | ||
158 | SND_SOC_DAPM_INPUT("LLINEIN"), | |
159 | SND_SOC_DAPM_INPUT("RLINEIN"), | |
160 | ||
161 | SND_SOC_DAPM_INPUT("MICIN"), | |
162 | }; | |
163 | ||
a7dca707 | 164 | static const struct snd_soc_dapm_route tlv320aic23_intercon[] = { |
c1f27190 AK |
165 | /* Output Mixer */ |
166 | {"Output Mixer", "Line Bypass Switch", "Line Input"}, | |
167 | {"Output Mixer", "Playback Switch", "DAC"}, | |
168 | {"Output Mixer", "Mic Sidetone Switch", "Mic Input"}, | |
169 | ||
170 | /* Outputs */ | |
171 | {"RHPOUT", NULL, "Output Mixer"}, | |
172 | {"LHPOUT", NULL, "Output Mixer"}, | |
173 | {"LOUT", NULL, "Output Mixer"}, | |
174 | {"ROUT", NULL, "Output Mixer"}, | |
175 | ||
176 | /* Inputs */ | |
a03faba9 LD |
177 | {"Line Input", NULL, "LLINEIN"}, |
178 | {"Line Input", NULL, "RLINEIN"}, | |
179 | {"Mic Input", NULL, "MICIN"}, | |
c1f27190 AK |
180 | |
181 | /* input mux */ | |
182 | {"Capture Source", "Line", "Line Input"}, | |
183 | {"Capture Source", "Mic", "Mic Input"}, | |
184 | {"ADC", NULL, "Capture Source"}, | |
185 | ||
186 | }; | |
187 | ||
26df91c3 TK |
188 | /* AIC23 driver data */ |
189 | struct aic23 { | |
4aa11d67 | 190 | struct regmap *regmap; |
26df91c3 TK |
191 | int mclk; |
192 | int requested_adc; | |
193 | int requested_dac; | |
194 | }; | |
195 | ||
196 | /* | |
197 | * Common Crystals used | |
198 | * 11.2896 Mhz /128 = *88.2k /192 = 58.8k | |
199 | * 12.0000 Mhz /125 = *96k /136 = 88.235K | |
200 | * 12.2880 Mhz /128 = *96k /192 = 64k | |
201 | * 16.9344 Mhz /128 = 132.3k /192 = *88.2k | |
202 | * 18.4320 Mhz /128 = 144k /192 = *96k | |
203 | */ | |
204 | ||
205 | /* | |
206 | * Normal BOSR 0-256/2 = 128, 1-384/2 = 192 | |
207 | * USB BOSR 0-250/2 = 125, 1-272/2 = 136 | |
208 | */ | |
209 | static const int bosr_usb_divisor_table[] = { | |
210 | 128, 125, 192, 136 | |
211 | }; | |
212 | #define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7)) | |
213 | #define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15)) | |
214 | static const unsigned short sr_valid_mask[] = { | |
215 | LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/ | |
26df91c3 | 216 | LOWER_GROUP, /* Usb, bosr - 0*/ |
bab02124 | 217 | LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/ |
26df91c3 TK |
218 | UPPER_GROUP, /* Usb, bosr - 1*/ |
219 | }; | |
220 | /* | |
221 | * Every divisor is a factor of 11*12 | |
222 | */ | |
223 | #define SR_MULT (11*12) | |
ccff4b15 | 224 | #define A(x) (SR_MULT/x) |
26df91c3 | 225 | static const unsigned char sr_adc_mult_table[] = { |
ccff4b15 TK |
226 | A(2), A(2), A(12), A(12), 0, 0, A(3), A(1), |
227 | A(2), A(2), A(11), A(11), 0, 0, 0, A(1) | |
26df91c3 TK |
228 | }; |
229 | static const unsigned char sr_dac_mult_table[] = { | |
ccff4b15 TK |
230 | A(2), A(12), A(2), A(12), 0, 0, A(3), A(1), |
231 | A(2), A(11), A(2), A(11), 0, 0, 0, A(1) | |
c1f27190 AK |
232 | }; |
233 | ||
26df91c3 TK |
234 | static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc, |
235 | int dac, int dac_l, int dac_h, int need_dac) | |
236 | { | |
237 | if ((adc >= adc_l) && (adc <= adc_h) && | |
238 | (dac >= dac_l) && (dac <= dac_h)) { | |
239 | int diff_adc = need_adc - adc; | |
240 | int diff_dac = need_dac - dac; | |
241 | return abs(diff_adc) + abs(diff_dac); | |
242 | } | |
243 | return UINT_MAX; | |
244 | } | |
245 | ||
246 | static int find_rate(int mclk, u32 need_adc, u32 need_dac) | |
247 | { | |
248 | int i, j; | |
249 | int best_i = -1; | |
250 | int best_j = -1; | |
251 | int best_div = 0; | |
252 | unsigned best_score = UINT_MAX; | |
253 | int adc_l, adc_h, dac_l, dac_h; | |
254 | ||
255 | need_adc *= SR_MULT; | |
256 | need_dac *= SR_MULT; | |
257 | /* | |
258 | * rates given are +/- 1/32 | |
259 | */ | |
260 | adc_l = need_adc - (need_adc >> 5); | |
261 | adc_h = need_adc + (need_adc >> 5); | |
262 | dac_l = need_dac - (need_dac >> 5); | |
263 | dac_h = need_dac + (need_dac >> 5); | |
8d702f23 | 264 | for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) { |
26df91c3 TK |
265 | int base = mclk / bosr_usb_divisor_table[i]; |
266 | int mask = sr_valid_mask[i]; | |
8d702f23 MB |
267 | for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table); |
268 | j++, mask >>= 1) { | |
26df91c3 TK |
269 | int adc; |
270 | int dac; | |
271 | int score; | |
272 | if ((mask & 1) == 0) | |
273 | continue; | |
274 | adc = base * sr_adc_mult_table[j]; | |
275 | dac = base * sr_dac_mult_table[j]; | |
276 | score = get_score(adc, adc_l, adc_h, need_adc, | |
277 | dac, dac_l, dac_h, need_dac); | |
278 | if (best_score > score) { | |
279 | best_score = score; | |
280 | best_i = i; | |
281 | best_j = j; | |
282 | best_div = 0; | |
283 | } | |
284 | score = get_score((adc >> 1), adc_l, adc_h, need_adc, | |
285 | (dac >> 1), dac_l, dac_h, need_dac); | |
286 | /* prefer to have a /2 */ | |
287 | if ((score != UINT_MAX) && (best_score >= score)) { | |
288 | best_score = score; | |
289 | best_i = i; | |
290 | best_j = j; | |
291 | best_div = 1; | |
292 | } | |
293 | } | |
294 | } | |
295 | return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT); | |
296 | } | |
297 | ||
8d702f23 | 298 | #ifdef DEBUG |
26df91c3 TK |
299 | static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk, |
300 | u32 *sample_rate_adc, u32 *sample_rate_dac) | |
301 | { | |
f9dfbf91 | 302 | int src = snd_soc_read(codec, TLV320AIC23_SRATE); |
26df91c3 TK |
303 | int sr = (src >> 2) & 0x0f; |
304 | int val = (mclk / bosr_usb_divisor_table[src & 3]); | |
305 | int adc = (val * sr_adc_mult_table[sr]) / SR_MULT; | |
306 | int dac = (val * sr_dac_mult_table[sr]) / SR_MULT; | |
307 | if (src & TLV320AIC23_CLKIN_HALF) { | |
308 | adc >>= 1; | |
309 | dac >>= 1; | |
310 | } | |
311 | *sample_rate_adc = adc; | |
312 | *sample_rate_dac = dac; | |
313 | } | |
8d702f23 | 314 | #endif |
26df91c3 TK |
315 | |
316 | static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk, | |
317 | u32 sample_rate_adc, u32 sample_rate_dac) | |
318 | { | |
319 | /* Search for the right sample rate */ | |
320 | int data = find_rate(mclk, sample_rate_adc, sample_rate_dac); | |
321 | if (data < 0) { | |
322 | printk(KERN_ERR "%s:Invalid rate %u,%u requested\n", | |
323 | __func__, sample_rate_adc, sample_rate_dac); | |
324 | return -EINVAL; | |
325 | } | |
f9dfbf91 | 326 | snd_soc_write(codec, TLV320AIC23_SRATE, data); |
8d702f23 MB |
327 | #ifdef DEBUG |
328 | { | |
329 | u32 adc, dac; | |
26df91c3 TK |
330 | get_current_sample_rates(codec, mclk, &adc, &dac); |
331 | printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n", | |
332 | adc, dac, data); | |
333 | } | |
8d702f23 | 334 | #endif |
26df91c3 TK |
335 | return 0; |
336 | } | |
337 | ||
c1f27190 | 338 | static int tlv320aic23_hw_params(struct snd_pcm_substream *substream, |
dee89c4d MB |
339 | struct snd_pcm_hw_params *params, |
340 | struct snd_soc_dai *dai) | |
c1f27190 | 341 | { |
e6968a17 | 342 | struct snd_soc_codec *codec = dai->codec; |
26df91c3 TK |
343 | u16 iface_reg; |
344 | int ret; | |
f0fba2ad | 345 | struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec); |
26df91c3 TK |
346 | u32 sample_rate_adc = aic23->requested_adc; |
347 | u32 sample_rate_dac = aic23->requested_dac; | |
348 | u32 sample_rate = params_rate(params); | |
349 | ||
350 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
351 | aic23->requested_dac = sample_rate_dac = sample_rate; | |
352 | if (!sample_rate_adc) | |
353 | sample_rate_adc = sample_rate; | |
354 | } else { | |
355 | aic23->requested_adc = sample_rate_adc = sample_rate; | |
356 | if (!sample_rate_dac) | |
357 | sample_rate_dac = sample_rate; | |
358 | } | |
359 | ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc, | |
360 | sample_rate_dac); | |
361 | if (ret < 0) | |
362 | return ret; | |
c1f27190 | 363 | |
f9dfbf91 AL |
364 | iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & ~(0x03 << 2); |
365 | ||
08074dc1 MB |
366 | switch (params_width(params)) { |
367 | case 16: | |
c1f27190 | 368 | break; |
08074dc1 | 369 | case 20: |
c1f27190 AK |
370 | iface_reg |= (0x01 << 2); |
371 | break; | |
08074dc1 | 372 | case 24: |
c1f27190 AK |
373 | iface_reg |= (0x02 << 2); |
374 | break; | |
08074dc1 | 375 | case 32: |
c1f27190 AK |
376 | iface_reg |= (0x03 << 2); |
377 | break; | |
378 | } | |
f9dfbf91 | 379 | snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg); |
c1f27190 AK |
380 | |
381 | return 0; | |
382 | } | |
383 | ||
dee89c4d MB |
384 | static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream, |
385 | struct snd_soc_dai *dai) | |
c1f27190 | 386 | { |
e6968a17 | 387 | struct snd_soc_codec *codec = dai->codec; |
c1f27190 AK |
388 | |
389 | /* set active */ | |
f9dfbf91 | 390 | snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0001); |
c1f27190 AK |
391 | |
392 | return 0; | |
393 | } | |
394 | ||
dee89c4d MB |
395 | static void tlv320aic23_shutdown(struct snd_pcm_substream *substream, |
396 | struct snd_soc_dai *dai) | |
c1f27190 | 397 | { |
e6968a17 | 398 | struct snd_soc_codec *codec = dai->codec; |
f0fba2ad | 399 | struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec); |
c1f27190 AK |
400 | |
401 | /* deactivate */ | |
5c898e74 | 402 | if (!snd_soc_codec_is_active(codec)) { |
c1f27190 | 403 | udelay(50); |
f9dfbf91 | 404 | snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0); |
c1f27190 | 405 | } |
26df91c3 TK |
406 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
407 | aic23->requested_dac = 0; | |
408 | else | |
409 | aic23->requested_adc = 0; | |
c1f27190 AK |
410 | } |
411 | ||
412 | static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute) | |
413 | { | |
414 | struct snd_soc_codec *codec = dai->codec; | |
415 | u16 reg; | |
416 | ||
f9dfbf91 | 417 | reg = snd_soc_read(codec, TLV320AIC23_DIGT); |
c1f27190 AK |
418 | if (mute) |
419 | reg |= TLV320AIC23_DACM_MUTE; | |
420 | ||
421 | else | |
422 | reg &= ~TLV320AIC23_DACM_MUTE; | |
423 | ||
f9dfbf91 | 424 | snd_soc_write(codec, TLV320AIC23_DIGT, reg); |
c1f27190 AK |
425 | |
426 | return 0; | |
427 | } | |
428 | ||
429 | static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai, | |
430 | unsigned int fmt) | |
431 | { | |
432 | struct snd_soc_codec *codec = codec_dai->codec; | |
433 | u16 iface_reg; | |
434 | ||
f9dfbf91 | 435 | iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & (~0x03); |
c1f27190 AK |
436 | |
437 | /* set master/slave audio interface */ | |
438 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
439 | case SND_SOC_DAIFMT_CBM_CFM: | |
440 | iface_reg |= TLV320AIC23_MS_MASTER; | |
441 | break; | |
442 | case SND_SOC_DAIFMT_CBS_CFS: | |
b01a3d69 | 443 | iface_reg &= ~TLV320AIC23_MS_MASTER; |
c1f27190 AK |
444 | break; |
445 | default: | |
446 | return -EINVAL; | |
447 | ||
448 | } | |
449 | ||
450 | /* interface format */ | |
451 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
452 | case SND_SOC_DAIFMT_I2S: | |
453 | iface_reg |= TLV320AIC23_FOR_I2S; | |
454 | break; | |
894bf92f PU |
455 | case SND_SOC_DAIFMT_DSP_A: |
456 | iface_reg |= TLV320AIC23_LRP_ON; | |
bd25867a | 457 | case SND_SOC_DAIFMT_DSP_B: |
c1f27190 AK |
458 | iface_reg |= TLV320AIC23_FOR_DSP; |
459 | break; | |
460 | case SND_SOC_DAIFMT_RIGHT_J: | |
461 | break; | |
462 | case SND_SOC_DAIFMT_LEFT_J: | |
463 | iface_reg |= TLV320AIC23_FOR_LJUST; | |
464 | break; | |
465 | default: | |
466 | return -EINVAL; | |
467 | ||
468 | } | |
469 | ||
f9dfbf91 | 470 | snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg); |
c1f27190 AK |
471 | |
472 | return 0; | |
473 | } | |
474 | ||
475 | static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai, | |
476 | int clk_id, unsigned int freq, int dir) | |
477 | { | |
f0fba2ad | 478 | struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai); |
26df91c3 TK |
479 | aic23->mclk = freq; |
480 | return 0; | |
c1f27190 AK |
481 | } |
482 | ||
483 | static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec, | |
484 | enum snd_soc_bias_level level) | |
485 | { | |
e875c1e3 | 486 | u16 reg = snd_soc_read(codec, TLV320AIC23_PWR) & 0x17f; |
c1f27190 AK |
487 | |
488 | switch (level) { | |
489 | case SND_SOC_BIAS_ON: | |
490 | /* vref/mid, osc on, dac unmute */ | |
3d5a4516 EB |
491 | reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \ |
492 | TLV320AIC23_DAC_OFF); | |
f9dfbf91 | 493 | snd_soc_write(codec, TLV320AIC23_PWR, reg); |
c1f27190 AK |
494 | break; |
495 | case SND_SOC_BIAS_PREPARE: | |
496 | break; | |
497 | case SND_SOC_BIAS_STANDBY: | |
498 | /* everything off except vref/vmid, */ | |
f9dfbf91 AL |
499 | snd_soc_write(codec, TLV320AIC23_PWR, |
500 | reg | TLV320AIC23_CLK_OFF); | |
c1f27190 AK |
501 | break; |
502 | case SND_SOC_BIAS_OFF: | |
503 | /* everything off, dac mute, inactive */ | |
f9dfbf91 | 504 | snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0); |
e875c1e3 | 505 | snd_soc_write(codec, TLV320AIC23_PWR, 0x1ff); |
c1f27190 AK |
506 | break; |
507 | } | |
c1f27190 AK |
508 | return 0; |
509 | } | |
510 | ||
511 | #define AIC23_RATES SNDRV_PCM_RATE_8000_96000 | |
512 | #define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ | |
513 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE) | |
514 | ||
85e7652d | 515 | static const struct snd_soc_dai_ops tlv320aic23_dai_ops = { |
6335d055 EM |
516 | .prepare = tlv320aic23_pcm_prepare, |
517 | .hw_params = tlv320aic23_hw_params, | |
518 | .shutdown = tlv320aic23_shutdown, | |
519 | .digital_mute = tlv320aic23_mute, | |
520 | .set_fmt = tlv320aic23_set_dai_fmt, | |
521 | .set_sysclk = tlv320aic23_set_dai_sysclk, | |
522 | }; | |
523 | ||
f0fba2ad LG |
524 | static struct snd_soc_dai_driver tlv320aic23_dai = { |
525 | .name = "tlv320aic23-hifi", | |
c1f27190 AK |
526 | .playback = { |
527 | .stream_name = "Playback", | |
528 | .channels_min = 2, | |
529 | .channels_max = 2, | |
530 | .rates = AIC23_RATES, | |
531 | .formats = AIC23_FORMATS,}, | |
532 | .capture = { | |
533 | .stream_name = "Capture", | |
534 | .channels_min = 2, | |
535 | .channels_max = 2, | |
536 | .rates = AIC23_RATES, | |
537 | .formats = AIC23_FORMATS,}, | |
6335d055 | 538 | .ops = &tlv320aic23_dai_ops, |
c1f27190 | 539 | }; |
c1f27190 | 540 | |
f0fba2ad | 541 | static int tlv320aic23_resume(struct snd_soc_codec *codec) |
c1f27190 | 542 | { |
4aa11d67 MB |
543 | struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec); |
544 | regcache_mark_dirty(aic23->regmap); | |
545 | regcache_sync(aic23->regmap); | |
c1f27190 AK |
546 | |
547 | return 0; | |
548 | } | |
549 | ||
b3fc5725 | 550 | static int tlv320aic23_codec_probe(struct snd_soc_codec *codec) |
c1f27190 | 551 | { |
c1f27190 | 552 | /* Reset codec */ |
f9dfbf91 AL |
553 | snd_soc_write(codec, TLV320AIC23_RESET, 0); |
554 | ||
f9dfbf91 | 555 | snd_soc_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K); |
c1f27190 AK |
556 | |
557 | /* Unmute input */ | |
f9dfbf91 AL |
558 | snd_soc_update_bits(codec, TLV320AIC23_LINVOL, |
559 | TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED); | |
c1f27190 | 560 | |
f9dfbf91 AL |
561 | snd_soc_update_bits(codec, TLV320AIC23_RINVOL, |
562 | TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED); | |
c1f27190 | 563 | |
f9dfbf91 AL |
564 | snd_soc_update_bits(codec, TLV320AIC23_ANLG, |
565 | TLV320AIC23_BYPASS_ON | TLV320AIC23_MICM_MUTED, | |
566 | 0); | |
c1f27190 AK |
567 | |
568 | /* Default output volume */ | |
f9dfbf91 AL |
569 | snd_soc_write(codec, TLV320AIC23_LCHNVOL, |
570 | TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK); | |
571 | snd_soc_write(codec, TLV320AIC23_RCHNVOL, | |
572 | TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK); | |
c1f27190 | 573 | |
f9dfbf91 | 574 | snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x1); |
c1f27190 | 575 | |
f0fba2ad LG |
576 | return 0; |
577 | } | |
c1f27190 | 578 | |
f0fba2ad | 579 | static struct snd_soc_codec_driver soc_codec_dev_tlv320aic23 = { |
b3fc5725 | 580 | .probe = tlv320aic23_codec_probe, |
f0fba2ad | 581 | .resume = tlv320aic23_resume, |
f0fba2ad | 582 | .set_bias_level = tlv320aic23_set_bias_level, |
aabb87f0 LPC |
583 | .suspend_bias_off = true, |
584 | ||
e88e4950 KM |
585 | .component_driver = { |
586 | .controls = tlv320aic23_snd_controls, | |
587 | .num_controls = ARRAY_SIZE(tlv320aic23_snd_controls), | |
588 | .dapm_widgets = tlv320aic23_dapm_widgets, | |
589 | .num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets), | |
590 | .dapm_routes = tlv320aic23_intercon, | |
591 | .num_dapm_routes = ARRAY_SIZE(tlv320aic23_intercon), | |
592 | }, | |
f0fba2ad | 593 | }; |
c1f27190 | 594 | |
b3fc5725 | 595 | int tlv320aic23_probe(struct device *dev, struct regmap *regmap) |
c1f27190 | 596 | { |
f0fba2ad | 597 | struct aic23 *aic23; |
c1f27190 | 598 | |
b3fc5725 MF |
599 | if (IS_ERR(regmap)) |
600 | return PTR_ERR(regmap); | |
c1f27190 | 601 | |
b3fc5725 | 602 | aic23 = devm_kzalloc(dev, sizeof(struct aic23), GFP_KERNEL); |
f0fba2ad LG |
603 | if (aic23 == NULL) |
604 | return -ENOMEM; | |
c1f27190 | 605 | |
b3fc5725 | 606 | aic23->regmap = regmap; |
4aa11d67 | 607 | |
b3fc5725 | 608 | dev_set_drvdata(dev, aic23); |
c1f27190 | 609 | |
b3fc5725 MF |
610 | return snd_soc_register_codec(dev, &soc_codec_dev_tlv320aic23, |
611 | &tlv320aic23_dai, 1); | |
c1f27190 | 612 | } |
40423285 | 613 | EXPORT_SYMBOL(tlv320aic23_probe); |
c1f27190 | 614 | |
c1f27190 AK |
615 | MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver"); |
616 | MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>"); | |
617 | MODULE_LICENSE("GPL"); |