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> | |
28 | #include <sound/core.h> | |
29 | #include <sound/pcm.h> | |
30 | #include <sound/pcm_params.h> | |
31 | #include <sound/soc.h> | |
32 | #include <sound/soc-dapm.h> | |
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 | ||
88 | if ((reg < 0 || reg > 9) && (reg != 15)) { | |
89 | printk(KERN_WARNING "%s Invalid register R%d\n", __func__, reg); | |
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 | ||
101 | printk(KERN_ERR "%s cannot write %03x to register R%d\n", __func__, | |
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 { | |
242 | struct snd_soc_codec codec; | |
243 | int mclk; | |
244 | int requested_adc; | |
245 | int requested_dac; | |
246 | }; | |
247 | ||
248 | /* | |
249 | * Common Crystals used | |
250 | * 11.2896 Mhz /128 = *88.2k /192 = 58.8k | |
251 | * 12.0000 Mhz /125 = *96k /136 = 88.235K | |
252 | * 12.2880 Mhz /128 = *96k /192 = 64k | |
253 | * 16.9344 Mhz /128 = 132.3k /192 = *88.2k | |
254 | * 18.4320 Mhz /128 = 144k /192 = *96k | |
255 | */ | |
256 | ||
257 | /* | |
258 | * Normal BOSR 0-256/2 = 128, 1-384/2 = 192 | |
259 | * USB BOSR 0-250/2 = 125, 1-272/2 = 136 | |
260 | */ | |
261 | static const int bosr_usb_divisor_table[] = { | |
262 | 128, 125, 192, 136 | |
263 | }; | |
264 | #define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7)) | |
265 | #define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15)) | |
266 | static const unsigned short sr_valid_mask[] = { | |
267 | LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/ | |
268 | LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/ | |
269 | LOWER_GROUP, /* Usb, bosr - 0*/ | |
270 | UPPER_GROUP, /* Usb, bosr - 1*/ | |
271 | }; | |
272 | /* | |
273 | * Every divisor is a factor of 11*12 | |
274 | */ | |
275 | #define SR_MULT (11*12) | |
276 | #define A(x) (x) ? (SR_MULT/x) : 0 | |
277 | static const unsigned char sr_adc_mult_table[] = { | |
278 | A(2), A(2), A(12), A(12), A(0), A(0), A(3), A(1), | |
279 | A(2), A(2), A(11), A(11), A(0), A(0), A(0), A(1) | |
280 | }; | |
281 | static const unsigned char sr_dac_mult_table[] = { | |
282 | A(2), A(12), A(2), A(12), A(0), A(0), A(3), A(1), | |
283 | A(2), A(11), A(2), A(11), A(0), A(0), A(0), A(1) | |
c1f27190 AK |
284 | }; |
285 | ||
26df91c3 TK |
286 | static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc, |
287 | int dac, int dac_l, int dac_h, int need_dac) | |
288 | { | |
289 | if ((adc >= adc_l) && (adc <= adc_h) && | |
290 | (dac >= dac_l) && (dac <= dac_h)) { | |
291 | int diff_adc = need_adc - adc; | |
292 | int diff_dac = need_dac - dac; | |
293 | return abs(diff_adc) + abs(diff_dac); | |
294 | } | |
295 | return UINT_MAX; | |
296 | } | |
297 | ||
298 | static int find_rate(int mclk, u32 need_adc, u32 need_dac) | |
299 | { | |
300 | int i, j; | |
301 | int best_i = -1; | |
302 | int best_j = -1; | |
303 | int best_div = 0; | |
304 | unsigned best_score = UINT_MAX; | |
305 | int adc_l, adc_h, dac_l, dac_h; | |
306 | ||
307 | need_adc *= SR_MULT; | |
308 | need_dac *= SR_MULT; | |
309 | /* | |
310 | * rates given are +/- 1/32 | |
311 | */ | |
312 | adc_l = need_adc - (need_adc >> 5); | |
313 | adc_h = need_adc + (need_adc >> 5); | |
314 | dac_l = need_dac - (need_dac >> 5); | |
315 | dac_h = need_dac + (need_dac >> 5); | |
8d702f23 | 316 | for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) { |
26df91c3 TK |
317 | int base = mclk / bosr_usb_divisor_table[i]; |
318 | int mask = sr_valid_mask[i]; | |
8d702f23 MB |
319 | for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table); |
320 | j++, mask >>= 1) { | |
26df91c3 TK |
321 | int adc; |
322 | int dac; | |
323 | int score; | |
324 | if ((mask & 1) == 0) | |
325 | continue; | |
326 | adc = base * sr_adc_mult_table[j]; | |
327 | dac = base * sr_dac_mult_table[j]; | |
328 | score = get_score(adc, adc_l, adc_h, need_adc, | |
329 | dac, dac_l, dac_h, need_dac); | |
330 | if (best_score > score) { | |
331 | best_score = score; | |
332 | best_i = i; | |
333 | best_j = j; | |
334 | best_div = 0; | |
335 | } | |
336 | score = get_score((adc >> 1), adc_l, adc_h, need_adc, | |
337 | (dac >> 1), dac_l, dac_h, need_dac); | |
338 | /* prefer to have a /2 */ | |
339 | if ((score != UINT_MAX) && (best_score >= score)) { | |
340 | best_score = score; | |
341 | best_i = i; | |
342 | best_j = j; | |
343 | best_div = 1; | |
344 | } | |
345 | } | |
346 | } | |
347 | return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT); | |
348 | } | |
349 | ||
8d702f23 | 350 | #ifdef DEBUG |
26df91c3 TK |
351 | static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk, |
352 | u32 *sample_rate_adc, u32 *sample_rate_dac) | |
353 | { | |
354 | int src = tlv320aic23_read_reg_cache(codec, TLV320AIC23_SRATE); | |
355 | int sr = (src >> 2) & 0x0f; | |
356 | int val = (mclk / bosr_usb_divisor_table[src & 3]); | |
357 | int adc = (val * sr_adc_mult_table[sr]) / SR_MULT; | |
358 | int dac = (val * sr_dac_mult_table[sr]) / SR_MULT; | |
359 | if (src & TLV320AIC23_CLKIN_HALF) { | |
360 | adc >>= 1; | |
361 | dac >>= 1; | |
362 | } | |
363 | *sample_rate_adc = adc; | |
364 | *sample_rate_dac = dac; | |
365 | } | |
8d702f23 | 366 | #endif |
26df91c3 TK |
367 | |
368 | static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk, | |
369 | u32 sample_rate_adc, u32 sample_rate_dac) | |
370 | { | |
371 | /* Search for the right sample rate */ | |
372 | int data = find_rate(mclk, sample_rate_adc, sample_rate_dac); | |
373 | if (data < 0) { | |
374 | printk(KERN_ERR "%s:Invalid rate %u,%u requested\n", | |
375 | __func__, sample_rate_adc, sample_rate_dac); | |
376 | return -EINVAL; | |
377 | } | |
378 | tlv320aic23_write(codec, TLV320AIC23_SRATE, data); | |
8d702f23 MB |
379 | #ifdef DEBUG |
380 | { | |
381 | u32 adc, dac; | |
26df91c3 TK |
382 | get_current_sample_rates(codec, mclk, &adc, &dac); |
383 | printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n", | |
384 | adc, dac, data); | |
385 | } | |
8d702f23 | 386 | #endif |
26df91c3 TK |
387 | return 0; |
388 | } | |
389 | ||
c1f27190 AK |
390 | static int tlv320aic23_add_widgets(struct snd_soc_codec *codec) |
391 | { | |
392 | snd_soc_dapm_new_controls(codec, tlv320aic23_dapm_widgets, | |
393 | ARRAY_SIZE(tlv320aic23_dapm_widgets)); | |
394 | ||
395 | /* set up audio path interconnects */ | |
396 | snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon)); | |
397 | ||
398 | snd_soc_dapm_new_widgets(codec); | |
399 | return 0; | |
400 | } | |
401 | ||
402 | static int tlv320aic23_hw_params(struct snd_pcm_substream *substream, | |
dee89c4d MB |
403 | struct snd_pcm_hw_params *params, |
404 | struct snd_soc_dai *dai) | |
c1f27190 AK |
405 | { |
406 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
407 | struct snd_soc_device *socdev = rtd->socdev; | |
6627a653 | 408 | struct snd_soc_codec *codec = socdev->card->codec; |
26df91c3 TK |
409 | u16 iface_reg; |
410 | int ret; | |
411 | struct aic23 *aic23 = container_of(codec, struct aic23, codec); | |
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; | |
455 | struct snd_soc_device *socdev = rtd->socdev; | |
6627a653 | 456 | struct snd_soc_codec *codec = socdev->card->codec; |
c1f27190 AK |
457 | |
458 | /* set active */ | |
459 | tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0001); | |
460 | ||
461 | return 0; | |
462 | } | |
463 | ||
dee89c4d MB |
464 | static void tlv320aic23_shutdown(struct snd_pcm_substream *substream, |
465 | struct snd_soc_dai *dai) | |
c1f27190 AK |
466 | { |
467 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
468 | struct snd_soc_device *socdev = rtd->socdev; | |
6627a653 | 469 | struct snd_soc_codec *codec = socdev->card->codec; |
26df91c3 | 470 | struct aic23 *aic23 = container_of(codec, struct aic23, codec); |
c1f27190 AK |
471 | |
472 | /* deactivate */ | |
473 | if (!codec->active) { | |
474 | udelay(50); | |
475 | tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0); | |
476 | } | |
26df91c3 TK |
477 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
478 | aic23->requested_dac = 0; | |
479 | else | |
480 | aic23->requested_adc = 0; | |
c1f27190 AK |
481 | } |
482 | ||
483 | static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute) | |
484 | { | |
485 | struct snd_soc_codec *codec = dai->codec; | |
486 | u16 reg; | |
487 | ||
488 | reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT); | |
489 | if (mute) | |
490 | reg |= TLV320AIC23_DACM_MUTE; | |
491 | ||
492 | else | |
493 | reg &= ~TLV320AIC23_DACM_MUTE; | |
494 | ||
495 | tlv320aic23_write(codec, TLV320AIC23_DIGT, reg); | |
496 | ||
497 | return 0; | |
498 | } | |
499 | ||
500 | static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai, | |
501 | unsigned int fmt) | |
502 | { | |
503 | struct snd_soc_codec *codec = codec_dai->codec; | |
504 | u16 iface_reg; | |
505 | ||
506 | iface_reg = | |
507 | tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT_FMT) & (~0x03); | |
508 | ||
509 | /* set master/slave audio interface */ | |
510 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
511 | case SND_SOC_DAIFMT_CBM_CFM: | |
512 | iface_reg |= TLV320AIC23_MS_MASTER; | |
513 | break; | |
514 | case SND_SOC_DAIFMT_CBS_CFS: | |
515 | break; | |
516 | default: | |
517 | return -EINVAL; | |
518 | ||
519 | } | |
520 | ||
521 | /* interface format */ | |
522 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
523 | case SND_SOC_DAIFMT_I2S: | |
524 | iface_reg |= TLV320AIC23_FOR_I2S; | |
525 | break; | |
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 | { | |
547 | struct snd_soc_codec *codec = codec_dai->codec; | |
26df91c3 TK |
548 | struct aic23 *aic23 = container_of(codec, struct aic23, codec); |
549 | aic23->mclk = freq; | |
550 | return 0; | |
c1f27190 AK |
551 | } |
552 | ||
553 | static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec, | |
554 | enum snd_soc_bias_level level) | |
555 | { | |
556 | u16 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_PWR) & 0xff7f; | |
557 | ||
558 | switch (level) { | |
559 | case SND_SOC_BIAS_ON: | |
560 | /* vref/mid, osc on, dac unmute */ | |
561 | tlv320aic23_write(codec, TLV320AIC23_PWR, reg); | |
562 | break; | |
563 | case SND_SOC_BIAS_PREPARE: | |
564 | break; | |
565 | case SND_SOC_BIAS_STANDBY: | |
566 | /* everything off except vref/vmid, */ | |
567 | tlv320aic23_write(codec, TLV320AIC23_PWR, reg | 0x0040); | |
568 | break; | |
569 | case SND_SOC_BIAS_OFF: | |
570 | /* everything off, dac mute, inactive */ | |
571 | tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0); | |
572 | tlv320aic23_write(codec, TLV320AIC23_PWR, 0xffff); | |
573 | break; | |
574 | } | |
575 | codec->bias_level = level; | |
576 | return 0; | |
577 | } | |
578 | ||
579 | #define AIC23_RATES SNDRV_PCM_RATE_8000_96000 | |
580 | #define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ | |
581 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE) | |
582 | ||
583 | struct snd_soc_dai tlv320aic23_dai = { | |
584 | .name = "tlv320aic23", | |
585 | .playback = { | |
586 | .stream_name = "Playback", | |
587 | .channels_min = 2, | |
588 | .channels_max = 2, | |
589 | .rates = AIC23_RATES, | |
590 | .formats = AIC23_FORMATS,}, | |
591 | .capture = { | |
592 | .stream_name = "Capture", | |
593 | .channels_min = 2, | |
594 | .channels_max = 2, | |
595 | .rates = AIC23_RATES, | |
596 | .formats = AIC23_FORMATS,}, | |
597 | .ops = { | |
598 | .prepare = tlv320aic23_pcm_prepare, | |
599 | .hw_params = tlv320aic23_hw_params, | |
600 | .shutdown = tlv320aic23_shutdown, | |
dee89c4d MB |
601 | .digital_mute = tlv320aic23_mute, |
602 | .set_fmt = tlv320aic23_set_dai_fmt, | |
603 | .set_sysclk = tlv320aic23_set_dai_sysclk, | |
604 | } | |
c1f27190 AK |
605 | }; |
606 | EXPORT_SYMBOL_GPL(tlv320aic23_dai); | |
607 | ||
608 | static int tlv320aic23_suspend(struct platform_device *pdev, | |
609 | pm_message_t state) | |
610 | { | |
611 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
6627a653 | 612 | struct snd_soc_codec *codec = socdev->card->codec; |
c1f27190 AK |
613 | |
614 | tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0); | |
615 | tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF); | |
616 | ||
617 | return 0; | |
618 | } | |
619 | ||
620 | static int tlv320aic23_resume(struct platform_device *pdev) | |
621 | { | |
622 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
6627a653 | 623 | struct snd_soc_codec *codec = socdev->card->codec; |
c1f27190 AK |
624 | int i; |
625 | u16 reg; | |
626 | ||
627 | /* Sync reg_cache with the hardware */ | |
628 | for (reg = 0; reg < ARRAY_SIZE(tlv320aic23_reg); i++) { | |
629 | u16 val = tlv320aic23_read_reg_cache(codec, reg); | |
630 | tlv320aic23_write(codec, reg, val); | |
631 | } | |
632 | ||
633 | tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY); | |
634 | tlv320aic23_set_bias_level(codec, codec->suspend_bias_level); | |
635 | ||
636 | return 0; | |
637 | } | |
638 | ||
639 | /* | |
640 | * initialise the AIC23 driver | |
641 | * register the mixer and dsp interfaces with the kernel | |
642 | */ | |
643 | static int tlv320aic23_init(struct snd_soc_device *socdev) | |
644 | { | |
6627a653 | 645 | struct snd_soc_codec *codec = socdev->card->codec; |
c1f27190 AK |
646 | int ret = 0; |
647 | u16 reg; | |
648 | ||
649 | codec->name = "tlv320aic23"; | |
650 | codec->owner = THIS_MODULE; | |
651 | codec->read = tlv320aic23_read_reg_cache; | |
652 | codec->write = tlv320aic23_write; | |
653 | codec->set_bias_level = tlv320aic23_set_bias_level; | |
654 | codec->dai = &tlv320aic23_dai; | |
655 | codec->num_dai = 1; | |
656 | codec->reg_cache_size = ARRAY_SIZE(tlv320aic23_reg); | |
657 | codec->reg_cache = | |
658 | kmemdup(tlv320aic23_reg, sizeof(tlv320aic23_reg), GFP_KERNEL); | |
659 | if (codec->reg_cache == NULL) | |
660 | return -ENOMEM; | |
661 | ||
662 | /* Reset codec */ | |
663 | tlv320aic23_write(codec, TLV320AIC23_RESET, 0); | |
664 | ||
665 | /* register pcms */ | |
666 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); | |
667 | if (ret < 0) { | |
668 | printk(KERN_ERR "tlv320aic23: failed to create pcms\n"); | |
669 | goto pcm_err; | |
670 | } | |
671 | ||
672 | /* power on device */ | |
673 | tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY); | |
674 | ||
675 | tlv320aic23_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K); | |
676 | ||
677 | /* Unmute input */ | |
678 | reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_LINVOL); | |
679 | tlv320aic23_write(codec, TLV320AIC23_LINVOL, | |
680 | (reg & (~TLV320AIC23_LIM_MUTED)) | | |
681 | (TLV320AIC23_LRS_ENABLED)); | |
682 | ||
683 | reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_RINVOL); | |
684 | tlv320aic23_write(codec, TLV320AIC23_RINVOL, | |
685 | (reg & (~TLV320AIC23_LIM_MUTED)) | | |
686 | TLV320AIC23_LRS_ENABLED); | |
687 | ||
688 | reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG); | |
689 | tlv320aic23_write(codec, TLV320AIC23_ANLG, | |
690 | (reg) & (~TLV320AIC23_BYPASS_ON) & | |
691 | (~TLV320AIC23_MICM_MUTED)); | |
692 | ||
693 | /* Default output volume */ | |
694 | tlv320aic23_write(codec, TLV320AIC23_LCHNVOL, | |
695 | TLV320AIC23_DEFAULT_OUT_VOL & | |
696 | TLV320AIC23_OUT_VOL_MASK); | |
697 | tlv320aic23_write(codec, TLV320AIC23_RCHNVOL, | |
698 | TLV320AIC23_DEFAULT_OUT_VOL & | |
699 | TLV320AIC23_OUT_VOL_MASK); | |
700 | ||
701 | tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x1); | |
702 | ||
3e8e1952 IM |
703 | snd_soc_add_controls(codec, tlv320aic23_snd_controls, |
704 | ARRAY_SIZE(tlv320aic23_snd_controls)); | |
c1f27190 | 705 | tlv320aic23_add_widgets(codec); |
968a6025 | 706 | ret = snd_soc_init_card(socdev); |
c1f27190 AK |
707 | if (ret < 0) { |
708 | printk(KERN_ERR "tlv320aic23: failed to register card\n"); | |
709 | goto card_err; | |
710 | } | |
711 | ||
712 | return ret; | |
713 | ||
714 | card_err: | |
715 | snd_soc_free_pcms(socdev); | |
716 | snd_soc_dapm_free(socdev); | |
717 | pcm_err: | |
718 | kfree(codec->reg_cache); | |
719 | return ret; | |
720 | } | |
721 | static struct snd_soc_device *tlv320aic23_socdev; | |
722 | ||
723 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) | |
724 | /* | |
725 | * If the i2c layer weren't so broken, we could pass this kind of data | |
726 | * around | |
727 | */ | |
728 | static int tlv320aic23_codec_probe(struct i2c_client *i2c, | |
729 | const struct i2c_device_id *i2c_id) | |
730 | { | |
731 | struct snd_soc_device *socdev = tlv320aic23_socdev; | |
6627a653 | 732 | struct snd_soc_codec *codec = socdev->card->codec; |
c1f27190 AK |
733 | int ret; |
734 | ||
735 | if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
736 | return -EINVAL; | |
737 | ||
738 | i2c_set_clientdata(i2c, codec); | |
739 | codec->control_data = i2c; | |
740 | ||
741 | ret = tlv320aic23_init(socdev); | |
742 | if (ret < 0) { | |
743 | printk(KERN_ERR "tlv320aic23: failed to initialise AIC23\n"); | |
744 | goto err; | |
745 | } | |
746 | return ret; | |
747 | ||
748 | err: | |
749 | kfree(codec); | |
750 | kfree(i2c); | |
751 | return ret; | |
752 | } | |
753 | static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c) | |
754 | { | |
755 | put_device(&i2c->dev); | |
756 | return 0; | |
757 | } | |
758 | ||
759 | static const struct i2c_device_id tlv320aic23_id[] = { | |
760 | {"tlv320aic23", 0}, | |
761 | {} | |
762 | }; | |
763 | ||
764 | MODULE_DEVICE_TABLE(i2c, tlv320aic23_id); | |
765 | ||
766 | static struct i2c_driver tlv320aic23_i2c_driver = { | |
767 | .driver = { | |
768 | .name = "tlv320aic23", | |
769 | }, | |
770 | .probe = tlv320aic23_codec_probe, | |
771 | .remove = __exit_p(tlv320aic23_i2c_remove), | |
772 | .id_table = tlv320aic23_id, | |
773 | }; | |
774 | ||
775 | #endif | |
776 | ||
777 | static int tlv320aic23_probe(struct platform_device *pdev) | |
778 | { | |
779 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
780 | struct snd_soc_codec *codec; | |
26df91c3 | 781 | struct aic23 *aic23; |
c1f27190 AK |
782 | int ret = 0; |
783 | ||
784 | printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION); | |
785 | ||
26df91c3 TK |
786 | aic23 = kzalloc(sizeof(struct aic23), GFP_KERNEL); |
787 | if (aic23 == NULL) | |
c1f27190 | 788 | return -ENOMEM; |
26df91c3 | 789 | codec = &aic23->codec; |
6627a653 | 790 | socdev->card->codec = codec; |
c1f27190 AK |
791 | mutex_init(&codec->mutex); |
792 | INIT_LIST_HEAD(&codec->dapm_widgets); | |
793 | INIT_LIST_HEAD(&codec->dapm_paths); | |
794 | ||
795 | tlv320aic23_socdev = socdev; | |
796 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) | |
4aa02396 | 797 | codec->hw_write = (hw_write_t) i2c_master_send; |
c1f27190 AK |
798 | codec->hw_read = NULL; |
799 | ret = i2c_add_driver(&tlv320aic23_i2c_driver); | |
800 | if (ret != 0) | |
801 | printk(KERN_ERR "can't add i2c driver"); | |
802 | #endif | |
803 | return ret; | |
804 | } | |
805 | ||
806 | static int tlv320aic23_remove(struct platform_device *pdev) | |
807 | { | |
808 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
6627a653 | 809 | struct snd_soc_codec *codec = socdev->card->codec; |
26df91c3 | 810 | struct aic23 *aic23 = container_of(codec, struct aic23, codec); |
c1f27190 AK |
811 | |
812 | if (codec->control_data) | |
813 | tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF); | |
814 | ||
815 | snd_soc_free_pcms(socdev); | |
816 | snd_soc_dapm_free(socdev); | |
817 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) | |
818 | i2c_del_driver(&tlv320aic23_i2c_driver); | |
819 | #endif | |
820 | kfree(codec->reg_cache); | |
26df91c3 | 821 | kfree(aic23); |
c1f27190 AK |
822 | |
823 | return 0; | |
824 | } | |
825 | struct snd_soc_codec_device soc_codec_dev_tlv320aic23 = { | |
826 | .probe = tlv320aic23_probe, | |
827 | .remove = tlv320aic23_remove, | |
828 | .suspend = tlv320aic23_suspend, | |
829 | .resume = tlv320aic23_resume, | |
830 | }; | |
831 | EXPORT_SYMBOL_GPL(soc_codec_dev_tlv320aic23); | |
832 | ||
c9b3a40f | 833 | static int __init tlv320aic23_modinit(void) |
64089b84 MB |
834 | { |
835 | return snd_soc_register_dai(&tlv320aic23_dai); | |
836 | } | |
837 | module_init(tlv320aic23_modinit); | |
838 | ||
839 | static void __exit tlv320aic23_exit(void) | |
840 | { | |
841 | snd_soc_unregister_dai(&tlv320aic23_dai); | |
842 | } | |
843 | module_exit(tlv320aic23_exit); | |
844 | ||
c1f27190 AK |
845 | MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver"); |
846 | MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>"); | |
847 | MODULE_LICENSE("GPL"); |