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> | |
26 | #include <linux/i2c.h> | |
27 | #include <linux/platform_device.h> | |
5a0e3ad6 | 28 | #include <linux/slab.h> |
c1f27190 AK |
29 | #include <sound/core.h> |
30 | #include <sound/pcm.h> | |
31 | #include <sound/pcm_params.h> | |
32 | #include <sound/soc.h> | |
c1f27190 AK |
33 | #include <sound/tlv.h> |
34 | #include <sound/initval.h> | |
35 | ||
36 | #include "tlv320aic23.h" | |
37 | ||
c1f27190 AK |
38 | #define AIC23_VERSION "0.1" |
39 | ||
c1f27190 AK |
40 | /* |
41 | * AIC23 register cache | |
42 | */ | |
43 | static const u16 tlv320aic23_reg[] = { | |
44 | 0x0097, 0x0097, 0x00F9, 0x00F9, /* 0 */ | |
45 | 0x001A, 0x0004, 0x0007, 0x0001, /* 4 */ | |
46 | 0x0020, 0x0000, 0x0000, 0x0000, /* 8 */ | |
47 | 0x0000, 0x0000, 0x0000, 0x0000, /* 12 */ | |
48 | }; | |
49 | ||
50 | /* | |
51 | * read tlv320aic23 register cache | |
52 | */ | |
53 | static inline unsigned int tlv320aic23_read_reg_cache(struct snd_soc_codec | |
54 | *codec, unsigned int reg) | |
55 | { | |
56 | u16 *cache = codec->reg_cache; | |
57 | if (reg >= ARRAY_SIZE(tlv320aic23_reg)) | |
58 | return -1; | |
59 | return cache[reg]; | |
60 | } | |
61 | ||
62 | /* | |
63 | * write tlv320aic23 register cache | |
64 | */ | |
65 | static inline void tlv320aic23_write_reg_cache(struct snd_soc_codec *codec, | |
66 | u8 reg, u16 value) | |
67 | { | |
68 | u16 *cache = codec->reg_cache; | |
69 | if (reg >= ARRAY_SIZE(tlv320aic23_reg)) | |
70 | return; | |
71 | cache[reg] = value; | |
72 | } | |
73 | ||
74 | /* | |
75 | * write to the tlv320aic23 register space | |
76 | */ | |
77 | static int tlv320aic23_write(struct snd_soc_codec *codec, unsigned int reg, | |
78 | unsigned int value) | |
79 | { | |
80 | ||
4aa02396 | 81 | u8 data[2]; |
c1f27190 AK |
82 | |
83 | /* TLV320AIC23 has 7 bit address and 9 bits of data | |
84 | * so we need to switch one data bit into reg and rest | |
85 | * of data into val | |
86 | */ | |
87 | ||
84ed1a19 | 88 | if (reg > 9 && reg != 15) { |
449bd54d | 89 | printk(KERN_WARNING "%s Invalid register R%u\n", __func__, reg); |
c1f27190 AK |
90 | return -1; |
91 | } | |
92 | ||
4aa02396 AK |
93 | data[0] = (reg << 1) | (value >> 8 & 0x01); |
94 | data[1] = value & 0xff; | |
c1f27190 AK |
95 | |
96 | tlv320aic23_write_reg_cache(codec, reg, value); | |
97 | ||
4aa02396 | 98 | if (codec->hw_write(codec->control_data, data, 2) == 2) |
c1f27190 AK |
99 | return 0; |
100 | ||
449bd54d | 101 | printk(KERN_ERR "%s cannot write %03x to register R%u\n", __func__, |
c1f27190 AK |
102 | value, reg); |
103 | ||
104 | return -EIO; | |
105 | } | |
106 | ||
107 | static const char *rec_src_text[] = { "Line", "Mic" }; | |
108 | static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"}; | |
c1f27190 AK |
109 | |
110 | static const struct soc_enum rec_src_enum = | |
111 | SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text); | |
112 | ||
113 | static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls = | |
114 | SOC_DAPM_ENUM("Input Select", rec_src_enum); | |
115 | ||
116 | static const struct soc_enum tlv320aic23_rec_src = | |
117 | SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text); | |
118 | static const struct soc_enum tlv320aic23_deemph = | |
119 | SOC_ENUM_SINGLE(TLV320AIC23_DIGT, 1, 4, deemph_text); | |
c1f27190 AK |
120 | |
121 | static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0); | |
122 | static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0); | |
df91ddf1 AK |
123 | static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0); |
124 | ||
125 | static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol, | |
126 | struct snd_ctl_elem_value *ucontrol) | |
127 | { | |
128 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
129 | u16 val, reg; | |
130 | ||
131 | val = (ucontrol->value.integer.value[0] & 0x07); | |
132 | ||
133 | /* linear conversion to userspace | |
134 | * 000 = -6db | |
135 | * 001 = -9db | |
136 | * 010 = -12db | |
137 | * 011 = -18db (Min) | |
138 | * 100 = 0db (Max) | |
139 | */ | |
140 | val = (val >= 4) ? 4 : (3 - val); | |
141 | ||
142 | reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG) & (~0x1C0); | |
143 | tlv320aic23_write(codec, TLV320AIC23_ANLG, reg | (val << 6)); | |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
148 | static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol, | |
149 | struct snd_ctl_elem_value *ucontrol) | |
150 | { | |
151 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
152 | u16 val; | |
153 | ||
154 | val = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG) & (0x1C0); | |
155 | val = val >> 6; | |
156 | val = (val >= 4) ? 4 : (3 - val); | |
157 | ucontrol->value.integer.value[0] = val; | |
158 | return 0; | |
159 | ||
160 | } | |
161 | ||
162 | #define SOC_TLV320AIC23_SINGLE_TLV(xname, reg, shift, max, invert, tlv_array) \ | |
163 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \ | |
164 | .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\ | |
165 | SNDRV_CTL_ELEM_ACCESS_READWRITE,\ | |
166 | .tlv.p = (tlv_array), \ | |
167 | .info = snd_soc_info_volsw, .get = snd_soc_tlv320aic23_get_volsw,\ | |
168 | .put = snd_soc_tlv320aic23_put_volsw, \ | |
169 | .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) } | |
c1f27190 AK |
170 | |
171 | static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = { | |
172 | SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL, | |
173 | TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv), | |
174 | SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1), | |
175 | SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL, | |
176 | TLV320AIC23_RINVOL, 7, 1, 0), | |
177 | SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL, | |
178 | TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv), | |
179 | SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1), | |
180 | SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0), | |
df91ddf1 AK |
181 | SOC_TLV320AIC23_SINGLE_TLV("Sidetone Volume", TLV320AIC23_ANLG, |
182 | 6, 4, 0, sidetone_vol_tlv), | |
c1f27190 AK |
183 | SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph), |
184 | }; | |
185 | ||
c1f27190 AK |
186 | /* PGA Mixer controls for Line and Mic switch */ |
187 | static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = { | |
188 | SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0), | |
189 | SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0), | |
190 | SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0), | |
191 | }; | |
192 | ||
193 | static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = { | |
194 | SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1), | |
195 | SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1), | |
196 | SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0, | |
197 | &tlv320aic23_rec_src_mux_controls), | |
198 | SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1, | |
199 | &tlv320aic23_output_mixer_controls[0], | |
200 | ARRAY_SIZE(tlv320aic23_output_mixer_controls)), | |
201 | SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0), | |
202 | SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0), | |
203 | ||
204 | SND_SOC_DAPM_OUTPUT("LHPOUT"), | |
205 | SND_SOC_DAPM_OUTPUT("RHPOUT"), | |
206 | SND_SOC_DAPM_OUTPUT("LOUT"), | |
207 | SND_SOC_DAPM_OUTPUT("ROUT"), | |
208 | ||
209 | SND_SOC_DAPM_INPUT("LLINEIN"), | |
210 | SND_SOC_DAPM_INPUT("RLINEIN"), | |
211 | ||
212 | SND_SOC_DAPM_INPUT("MICIN"), | |
213 | }; | |
214 | ||
215 | static const struct snd_soc_dapm_route intercon[] = { | |
216 | /* Output Mixer */ | |
217 | {"Output Mixer", "Line Bypass Switch", "Line Input"}, | |
218 | {"Output Mixer", "Playback Switch", "DAC"}, | |
219 | {"Output Mixer", "Mic Sidetone Switch", "Mic Input"}, | |
220 | ||
221 | /* Outputs */ | |
222 | {"RHPOUT", NULL, "Output Mixer"}, | |
223 | {"LHPOUT", NULL, "Output Mixer"}, | |
224 | {"LOUT", NULL, "Output Mixer"}, | |
225 | {"ROUT", NULL, "Output Mixer"}, | |
226 | ||
227 | /* Inputs */ | |
228 | {"Line Input", "NULL", "LLINEIN"}, | |
229 | {"Line Input", "NULL", "RLINEIN"}, | |
230 | ||
231 | {"Mic Input", "NULL", "MICIN"}, | |
232 | ||
233 | /* input mux */ | |
234 | {"Capture Source", "Line", "Line Input"}, | |
235 | {"Capture Source", "Mic", "Mic Input"}, | |
236 | {"ADC", NULL, "Capture Source"}, | |
237 | ||
238 | }; | |
239 | ||
26df91c3 TK |
240 | /* AIC23 driver data */ |
241 | struct aic23 { | |
f0fba2ad LG |
242 | enum snd_soc_control_type control_type; |
243 | void *control_data; | |
26df91c3 TK |
244 | int mclk; |
245 | int requested_adc; | |
246 | int requested_dac; | |
247 | }; | |
248 | ||
249 | /* | |
250 | * Common Crystals used | |
251 | * 11.2896 Mhz /128 = *88.2k /192 = 58.8k | |
252 | * 12.0000 Mhz /125 = *96k /136 = 88.235K | |
253 | * 12.2880 Mhz /128 = *96k /192 = 64k | |
254 | * 16.9344 Mhz /128 = 132.3k /192 = *88.2k | |
255 | * 18.4320 Mhz /128 = 144k /192 = *96k | |
256 | */ | |
257 | ||
258 | /* | |
259 | * Normal BOSR 0-256/2 = 128, 1-384/2 = 192 | |
260 | * USB BOSR 0-250/2 = 125, 1-272/2 = 136 | |
261 | */ | |
262 | static const int bosr_usb_divisor_table[] = { | |
263 | 128, 125, 192, 136 | |
264 | }; | |
265 | #define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7)) | |
266 | #define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15)) | |
267 | static const unsigned short sr_valid_mask[] = { | |
268 | LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/ | |
26df91c3 | 269 | LOWER_GROUP, /* Usb, bosr - 0*/ |
bab02124 | 270 | LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/ |
26df91c3 TK |
271 | UPPER_GROUP, /* Usb, bosr - 1*/ |
272 | }; | |
273 | /* | |
274 | * Every divisor is a factor of 11*12 | |
275 | */ | |
276 | #define SR_MULT (11*12) | |
ccff4b15 | 277 | #define A(x) (SR_MULT/x) |
26df91c3 | 278 | static const unsigned char sr_adc_mult_table[] = { |
ccff4b15 TK |
279 | A(2), A(2), A(12), A(12), 0, 0, A(3), A(1), |
280 | A(2), A(2), A(11), A(11), 0, 0, 0, A(1) | |
26df91c3 TK |
281 | }; |
282 | static const unsigned char sr_dac_mult_table[] = { | |
ccff4b15 TK |
283 | A(2), A(12), A(2), A(12), 0, 0, A(3), A(1), |
284 | A(2), A(11), A(2), A(11), 0, 0, 0, A(1) | |
c1f27190 AK |
285 | }; |
286 | ||
26df91c3 TK |
287 | static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc, |
288 | int dac, int dac_l, int dac_h, int need_dac) | |
289 | { | |
290 | if ((adc >= adc_l) && (adc <= adc_h) && | |
291 | (dac >= dac_l) && (dac <= dac_h)) { | |
292 | int diff_adc = need_adc - adc; | |
293 | int diff_dac = need_dac - dac; | |
294 | return abs(diff_adc) + abs(diff_dac); | |
295 | } | |
296 | return UINT_MAX; | |
297 | } | |
298 | ||
299 | static int find_rate(int mclk, u32 need_adc, u32 need_dac) | |
300 | { | |
301 | int i, j; | |
302 | int best_i = -1; | |
303 | int best_j = -1; | |
304 | int best_div = 0; | |
305 | unsigned best_score = UINT_MAX; | |
306 | int adc_l, adc_h, dac_l, dac_h; | |
307 | ||
308 | need_adc *= SR_MULT; | |
309 | need_dac *= SR_MULT; | |
310 | /* | |
311 | * rates given are +/- 1/32 | |
312 | */ | |
313 | adc_l = need_adc - (need_adc >> 5); | |
314 | adc_h = need_adc + (need_adc >> 5); | |
315 | dac_l = need_dac - (need_dac >> 5); | |
316 | dac_h = need_dac + (need_dac >> 5); | |
8d702f23 | 317 | for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) { |
26df91c3 TK |
318 | int base = mclk / bosr_usb_divisor_table[i]; |
319 | int mask = sr_valid_mask[i]; | |
8d702f23 MB |
320 | for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table); |
321 | j++, mask >>= 1) { | |
26df91c3 TK |
322 | int adc; |
323 | int dac; | |
324 | int score; | |
325 | if ((mask & 1) == 0) | |
326 | continue; | |
327 | adc = base * sr_adc_mult_table[j]; | |
328 | dac = base * sr_dac_mult_table[j]; | |
329 | score = get_score(adc, adc_l, adc_h, need_adc, | |
330 | dac, dac_l, dac_h, need_dac); | |
331 | if (best_score > score) { | |
332 | best_score = score; | |
333 | best_i = i; | |
334 | best_j = j; | |
335 | best_div = 0; | |
336 | } | |
337 | score = get_score((adc >> 1), adc_l, adc_h, need_adc, | |
338 | (dac >> 1), dac_l, dac_h, need_dac); | |
339 | /* prefer to have a /2 */ | |
340 | if ((score != UINT_MAX) && (best_score >= score)) { | |
341 | best_score = score; | |
342 | best_i = i; | |
343 | best_j = j; | |
344 | best_div = 1; | |
345 | } | |
346 | } | |
347 | } | |
348 | return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT); | |
349 | } | |
350 | ||
8d702f23 | 351 | #ifdef DEBUG |
26df91c3 TK |
352 | static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk, |
353 | u32 *sample_rate_adc, u32 *sample_rate_dac) | |
354 | { | |
355 | int src = tlv320aic23_read_reg_cache(codec, TLV320AIC23_SRATE); | |
356 | int sr = (src >> 2) & 0x0f; | |
357 | int val = (mclk / bosr_usb_divisor_table[src & 3]); | |
358 | int adc = (val * sr_adc_mult_table[sr]) / SR_MULT; | |
359 | int dac = (val * sr_dac_mult_table[sr]) / SR_MULT; | |
360 | if (src & TLV320AIC23_CLKIN_HALF) { | |
361 | adc >>= 1; | |
362 | dac >>= 1; | |
363 | } | |
364 | *sample_rate_adc = adc; | |
365 | *sample_rate_dac = dac; | |
366 | } | |
8d702f23 | 367 | #endif |
26df91c3 TK |
368 | |
369 | static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk, | |
370 | u32 sample_rate_adc, u32 sample_rate_dac) | |
371 | { | |
372 | /* Search for the right sample rate */ | |
373 | int data = find_rate(mclk, sample_rate_adc, sample_rate_dac); | |
374 | if (data < 0) { | |
375 | printk(KERN_ERR "%s:Invalid rate %u,%u requested\n", | |
376 | __func__, sample_rate_adc, sample_rate_dac); | |
377 | return -EINVAL; | |
378 | } | |
379 | tlv320aic23_write(codec, TLV320AIC23_SRATE, data); | |
8d702f23 MB |
380 | #ifdef DEBUG |
381 | { | |
382 | u32 adc, dac; | |
26df91c3 TK |
383 | get_current_sample_rates(codec, mclk, &adc, &dac); |
384 | printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n", | |
385 | adc, dac, data); | |
386 | } | |
8d702f23 | 387 | #endif |
26df91c3 TK |
388 | return 0; |
389 | } | |
390 | ||
c1f27190 AK |
391 | static int tlv320aic23_add_widgets(struct snd_soc_codec *codec) |
392 | { | |
ce6120cc | 393 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
c1f27190 | 394 | |
ce6120cc LG |
395 | snd_soc_dapm_new_controls(dapm, tlv320aic23_dapm_widgets, |
396 | ARRAY_SIZE(tlv320aic23_dapm_widgets)); | |
c1f27190 | 397 | /* set up audio path interconnects */ |
ce6120cc | 398 | snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon)); |
c1f27190 | 399 | |
c1f27190 AK |
400 | return 0; |
401 | } | |
402 | ||
403 | static int tlv320aic23_hw_params(struct snd_pcm_substream *substream, | |
dee89c4d MB |
404 | struct snd_pcm_hw_params *params, |
405 | struct snd_soc_dai *dai) | |
c1f27190 AK |
406 | { |
407 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad | 408 | struct snd_soc_codec *codec = rtd->codec; |
26df91c3 TK |
409 | u16 iface_reg; |
410 | int ret; | |
f0fba2ad | 411 | struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec); |
26df91c3 TK |
412 | u32 sample_rate_adc = aic23->requested_adc; |
413 | u32 sample_rate_dac = aic23->requested_dac; | |
414 | u32 sample_rate = params_rate(params); | |
415 | ||
416 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { | |
417 | aic23->requested_dac = sample_rate_dac = sample_rate; | |
418 | if (!sample_rate_adc) | |
419 | sample_rate_adc = sample_rate; | |
420 | } else { | |
421 | aic23->requested_adc = sample_rate_adc = sample_rate; | |
422 | if (!sample_rate_dac) | |
423 | sample_rate_dac = sample_rate; | |
424 | } | |
425 | ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc, | |
426 | sample_rate_dac); | |
427 | if (ret < 0) | |
428 | return ret; | |
c1f27190 AK |
429 | |
430 | iface_reg = | |
431 | tlv320aic23_read_reg_cache(codec, | |
432 | TLV320AIC23_DIGT_FMT) & ~(0x03 << 2); | |
c1f27190 AK |
433 | switch (params_format(params)) { |
434 | case SNDRV_PCM_FORMAT_S16_LE: | |
435 | break; | |
436 | case SNDRV_PCM_FORMAT_S20_3LE: | |
437 | iface_reg |= (0x01 << 2); | |
438 | break; | |
439 | case SNDRV_PCM_FORMAT_S24_LE: | |
440 | iface_reg |= (0x02 << 2); | |
441 | break; | |
442 | case SNDRV_PCM_FORMAT_S32_LE: | |
443 | iface_reg |= (0x03 << 2); | |
444 | break; | |
445 | } | |
446 | tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg); | |
447 | ||
448 | return 0; | |
449 | } | |
450 | ||
dee89c4d MB |
451 | static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream, |
452 | struct snd_soc_dai *dai) | |
c1f27190 AK |
453 | { |
454 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad | 455 | struct snd_soc_codec *codec = rtd->codec; |
c1f27190 AK |
456 | |
457 | /* set active */ | |
458 | tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0001); | |
459 | ||
460 | return 0; | |
461 | } | |
462 | ||
dee89c4d MB |
463 | static void tlv320aic23_shutdown(struct snd_pcm_substream *substream, |
464 | struct snd_soc_dai *dai) | |
c1f27190 AK |
465 | { |
466 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
f0fba2ad LG |
467 | struct snd_soc_codec *codec = rtd->codec; |
468 | struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec); | |
c1f27190 AK |
469 | |
470 | /* deactivate */ | |
471 | if (!codec->active) { | |
472 | udelay(50); | |
473 | tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0); | |
474 | } | |
26df91c3 TK |
475 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
476 | aic23->requested_dac = 0; | |
477 | else | |
478 | aic23->requested_adc = 0; | |
c1f27190 AK |
479 | } |
480 | ||
481 | static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute) | |
482 | { | |
483 | struct snd_soc_codec *codec = dai->codec; | |
484 | u16 reg; | |
485 | ||
486 | reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT); | |
487 | if (mute) | |
488 | reg |= TLV320AIC23_DACM_MUTE; | |
489 | ||
490 | else | |
491 | reg &= ~TLV320AIC23_DACM_MUTE; | |
492 | ||
493 | tlv320aic23_write(codec, TLV320AIC23_DIGT, reg); | |
494 | ||
495 | return 0; | |
496 | } | |
497 | ||
498 | static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai, | |
499 | unsigned int fmt) | |
500 | { | |
501 | struct snd_soc_codec *codec = codec_dai->codec; | |
502 | u16 iface_reg; | |
503 | ||
504 | iface_reg = | |
505 | tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT_FMT) & (~0x03); | |
506 | ||
507 | /* set master/slave audio interface */ | |
508 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
509 | case SND_SOC_DAIFMT_CBM_CFM: | |
510 | iface_reg |= TLV320AIC23_MS_MASTER; | |
511 | break; | |
512 | case SND_SOC_DAIFMT_CBS_CFS: | |
513 | break; | |
514 | default: | |
515 | return -EINVAL; | |
516 | ||
517 | } | |
518 | ||
519 | /* interface format */ | |
520 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
521 | case SND_SOC_DAIFMT_I2S: | |
522 | iface_reg |= TLV320AIC23_FOR_I2S; | |
523 | break; | |
894bf92f PU |
524 | case SND_SOC_DAIFMT_DSP_A: |
525 | iface_reg |= TLV320AIC23_LRP_ON; | |
bd25867a | 526 | case SND_SOC_DAIFMT_DSP_B: |
c1f27190 AK |
527 | iface_reg |= TLV320AIC23_FOR_DSP; |
528 | break; | |
529 | case SND_SOC_DAIFMT_RIGHT_J: | |
530 | break; | |
531 | case SND_SOC_DAIFMT_LEFT_J: | |
532 | iface_reg |= TLV320AIC23_FOR_LJUST; | |
533 | break; | |
534 | default: | |
535 | return -EINVAL; | |
536 | ||
537 | } | |
538 | ||
539 | tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg); | |
540 | ||
541 | return 0; | |
542 | } | |
543 | ||
544 | static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai, | |
545 | int clk_id, unsigned int freq, int dir) | |
546 | { | |
f0fba2ad | 547 | struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai); |
26df91c3 TK |
548 | aic23->mclk = freq; |
549 | return 0; | |
c1f27190 AK |
550 | } |
551 | ||
552 | static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec, | |
553 | enum snd_soc_bias_level level) | |
554 | { | |
555 | u16 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_PWR) & 0xff7f; | |
556 | ||
557 | switch (level) { | |
558 | case SND_SOC_BIAS_ON: | |
559 | /* vref/mid, osc on, dac unmute */ | |
3d5a4516 EB |
560 | reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \ |
561 | TLV320AIC23_DAC_OFF); | |
c1f27190 AK |
562 | tlv320aic23_write(codec, TLV320AIC23_PWR, reg); |
563 | break; | |
564 | case SND_SOC_BIAS_PREPARE: | |
565 | break; | |
566 | case SND_SOC_BIAS_STANDBY: | |
567 | /* everything off except vref/vmid, */ | |
3d5a4516 EB |
568 | tlv320aic23_write(codec, TLV320AIC23_PWR, reg | \ |
569 | TLV320AIC23_CLK_OFF); | |
c1f27190 AK |
570 | break; |
571 | case SND_SOC_BIAS_OFF: | |
572 | /* everything off, dac mute, inactive */ | |
573 | tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0); | |
574 | tlv320aic23_write(codec, TLV320AIC23_PWR, 0xffff); | |
575 | break; | |
576 | } | |
ce6120cc | 577 | codec->dapm.bias_level = level; |
c1f27190 AK |
578 | return 0; |
579 | } | |
580 | ||
581 | #define AIC23_RATES SNDRV_PCM_RATE_8000_96000 | |
582 | #define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ | |
583 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE) | |
584 | ||
6335d055 EM |
585 | static struct snd_soc_dai_ops tlv320aic23_dai_ops = { |
586 | .prepare = tlv320aic23_pcm_prepare, | |
587 | .hw_params = tlv320aic23_hw_params, | |
588 | .shutdown = tlv320aic23_shutdown, | |
589 | .digital_mute = tlv320aic23_mute, | |
590 | .set_fmt = tlv320aic23_set_dai_fmt, | |
591 | .set_sysclk = tlv320aic23_set_dai_sysclk, | |
592 | }; | |
593 | ||
f0fba2ad LG |
594 | static struct snd_soc_dai_driver tlv320aic23_dai = { |
595 | .name = "tlv320aic23-hifi", | |
c1f27190 AK |
596 | .playback = { |
597 | .stream_name = "Playback", | |
598 | .channels_min = 2, | |
599 | .channels_max = 2, | |
600 | .rates = AIC23_RATES, | |
601 | .formats = AIC23_FORMATS,}, | |
602 | .capture = { | |
603 | .stream_name = "Capture", | |
604 | .channels_min = 2, | |
605 | .channels_max = 2, | |
606 | .rates = AIC23_RATES, | |
607 | .formats = AIC23_FORMATS,}, | |
6335d055 | 608 | .ops = &tlv320aic23_dai_ops, |
c1f27190 | 609 | }; |
c1f27190 | 610 | |
f0fba2ad | 611 | static int tlv320aic23_suspend(struct snd_soc_codec *codec, |
c1f27190 AK |
612 | pm_message_t state) |
613 | { | |
c1f27190 AK |
614 | tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF); |
615 | ||
616 | return 0; | |
617 | } | |
618 | ||
f0fba2ad | 619 | static int tlv320aic23_resume(struct snd_soc_codec *codec) |
c1f27190 | 620 | { |
c1f27190 AK |
621 | u16 reg; |
622 | ||
623 | /* Sync reg_cache with the hardware */ | |
3e59aaa7 | 624 | for (reg = 0; reg <= TLV320AIC23_ACTIVE; reg++) { |
c1f27190 AK |
625 | u16 val = tlv320aic23_read_reg_cache(codec, reg); |
626 | tlv320aic23_write(codec, reg, val); | |
627 | } | |
c1f27190 | 628 | tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
c1f27190 AK |
629 | |
630 | return 0; | |
631 | } | |
632 | ||
f0fba2ad | 633 | static int tlv320aic23_probe(struct snd_soc_codec *codec) |
c1f27190 | 634 | { |
f0fba2ad LG |
635 | struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec); |
636 | int reg; | |
c1f27190 | 637 | |
f0fba2ad LG |
638 | printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION); |
639 | codec->control_data = aic23->control_data; | |
640 | codec->hw_write = (hw_write_t)i2c_master_send; | |
641 | codec->hw_read = NULL; | |
c1f27190 AK |
642 | |
643 | /* Reset codec */ | |
644 | tlv320aic23_write(codec, TLV320AIC23_RESET, 0); | |
645 | ||
c1f27190 AK |
646 | /* power on device */ |
647 | tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY); | |
648 | ||
649 | tlv320aic23_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K); | |
650 | ||
651 | /* Unmute input */ | |
652 | reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_LINVOL); | |
653 | tlv320aic23_write(codec, TLV320AIC23_LINVOL, | |
654 | (reg & (~TLV320AIC23_LIM_MUTED)) | | |
655 | (TLV320AIC23_LRS_ENABLED)); | |
656 | ||
657 | reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_RINVOL); | |
658 | tlv320aic23_write(codec, TLV320AIC23_RINVOL, | |
659 | (reg & (~TLV320AIC23_LIM_MUTED)) | | |
660 | TLV320AIC23_LRS_ENABLED); | |
661 | ||
662 | reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG); | |
663 | tlv320aic23_write(codec, TLV320AIC23_ANLG, | |
664 | (reg) & (~TLV320AIC23_BYPASS_ON) & | |
665 | (~TLV320AIC23_MICM_MUTED)); | |
666 | ||
667 | /* Default output volume */ | |
668 | tlv320aic23_write(codec, TLV320AIC23_LCHNVOL, | |
669 | TLV320AIC23_DEFAULT_OUT_VOL & | |
670 | TLV320AIC23_OUT_VOL_MASK); | |
671 | tlv320aic23_write(codec, TLV320AIC23_RCHNVOL, | |
672 | TLV320AIC23_DEFAULT_OUT_VOL & | |
673 | TLV320AIC23_OUT_VOL_MASK); | |
674 | ||
675 | tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x1); | |
676 | ||
3e8e1952 IM |
677 | snd_soc_add_controls(codec, tlv320aic23_snd_controls, |
678 | ARRAY_SIZE(tlv320aic23_snd_controls)); | |
c1f27190 | 679 | tlv320aic23_add_widgets(codec); |
c1f27190 | 680 | |
f0fba2ad LG |
681 | return 0; |
682 | } | |
c1f27190 | 683 | |
f0fba2ad LG |
684 | static int tlv320aic23_remove(struct snd_soc_codec *codec) |
685 | { | |
686 | tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF); | |
687 | return 0; | |
c1f27190 | 688 | } |
f0fba2ad LG |
689 | |
690 | static struct snd_soc_codec_driver soc_codec_dev_tlv320aic23 = { | |
691 | .reg_cache_size = ARRAY_SIZE(tlv320aic23_reg), | |
692 | .reg_word_size = sizeof(u16), | |
693 | .reg_cache_default = tlv320aic23_reg, | |
694 | .probe = tlv320aic23_probe, | |
695 | .remove = tlv320aic23_remove, | |
696 | .suspend = tlv320aic23_suspend, | |
697 | .resume = tlv320aic23_resume, | |
698 | .read = tlv320aic23_read_reg_cache, | |
699 | .write = tlv320aic23_write, | |
700 | .set_bias_level = tlv320aic23_set_bias_level, | |
701 | }; | |
c1f27190 AK |
702 | |
703 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) | |
704 | /* | |
705 | * If the i2c layer weren't so broken, we could pass this kind of data | |
706 | * around | |
707 | */ | |
708 | static int tlv320aic23_codec_probe(struct i2c_client *i2c, | |
709 | const struct i2c_device_id *i2c_id) | |
710 | { | |
f0fba2ad | 711 | struct aic23 *aic23; |
c1f27190 AK |
712 | int ret; |
713 | ||
714 | if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
715 | return -EINVAL; | |
716 | ||
f0fba2ad LG |
717 | aic23 = kzalloc(sizeof(struct aic23), GFP_KERNEL); |
718 | if (aic23 == NULL) | |
719 | return -ENOMEM; | |
c1f27190 | 720 | |
f0fba2ad LG |
721 | i2c_set_clientdata(i2c, aic23); |
722 | aic23->control_data = i2c; | |
723 | aic23->control_type = SND_SOC_I2C; | |
c1f27190 | 724 | |
f0fba2ad LG |
725 | ret = snd_soc_register_codec(&i2c->dev, |
726 | &soc_codec_dev_tlv320aic23, &tlv320aic23_dai, 1); | |
727 | if (ret < 0) | |
728 | kfree(aic23); | |
c1f27190 AK |
729 | return ret; |
730 | } | |
731 | static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c) | |
732 | { | |
f0fba2ad LG |
733 | snd_soc_unregister_codec(&i2c->dev); |
734 | kfree(i2c_get_clientdata(i2c)); | |
c1f27190 AK |
735 | return 0; |
736 | } | |
737 | ||
738 | static const struct i2c_device_id tlv320aic23_id[] = { | |
739 | {"tlv320aic23", 0}, | |
740 | {} | |
741 | }; | |
742 | ||
743 | MODULE_DEVICE_TABLE(i2c, tlv320aic23_id); | |
744 | ||
745 | static struct i2c_driver tlv320aic23_i2c_driver = { | |
746 | .driver = { | |
f0fba2ad | 747 | .name = "tlv320aic23-codec", |
c1f27190 AK |
748 | }, |
749 | .probe = tlv320aic23_codec_probe, | |
750 | .remove = __exit_p(tlv320aic23_i2c_remove), | |
751 | .id_table = tlv320aic23_id, | |
752 | }; | |
753 | ||
754 | #endif | |
755 | ||
f0fba2ad | 756 | static int __init tlv320aic23_modinit(void) |
c1f27190 | 757 | { |
f0fba2ad | 758 | int ret; |
c1f27190 | 759 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
c1f27190 | 760 | ret = i2c_add_driver(&tlv320aic23_i2c_driver); |
f0fba2ad LG |
761 | if (ret != 0) { |
762 | printk(KERN_ERR "Failed to register TLV320AIC23 I2C driver: %d\n", | |
763 | ret); | |
764 | } | |
c1f27190 AK |
765 | #endif |
766 | return ret; | |
767 | } | |
f0fba2ad | 768 | module_init(tlv320aic23_modinit); |
c1f27190 | 769 | |
f0fba2ad | 770 | static void __exit tlv320aic23_exit(void) |
c1f27190 | 771 | { |
c1f27190 AK |
772 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
773 | i2c_del_driver(&tlv320aic23_i2c_driver); | |
774 | #endif | |
64089b84 MB |
775 | } |
776 | module_exit(tlv320aic23_exit); | |
777 | ||
c1f27190 AK |
778 | MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver"); |
779 | MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>"); | |
780 | MODULE_LICENSE("GPL"); |