blob: a95b538b8fe76642661ce2c315e442e2a50fcea3 [file] [log] [blame]
Arun KSc1f27192008-10-02 14:45:49 +05301/*
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
Arun KSc1f27192008-10-02 14:45:49 +053038#define AIC23_VERSION "0.1"
39
Arun KSc1f27192008-10-02 14:45:49 +053040/*
41 * AIC23 register cache
42 */
43static 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 */
53static 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 */
65static 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 */
77static int tlv320aic23_write(struct snd_soc_codec *codec, unsigned int reg,
78 unsigned int value)
79{
80
Arun KS4aa02392008-10-13 15:47:25 +053081 u8 data[2];
Arun KSc1f27192008-10-02 14:45:49 +053082
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
Arun KS4aa02392008-10-13 15:47:25 +053093 data[0] = (reg << 1) | (value >> 8 & 0x01);
94 data[1] = value & 0xff;
Arun KSc1f27192008-10-02 14:45:49 +053095
96 tlv320aic23_write_reg_cache(codec, reg, value);
97
Arun KS4aa02392008-10-13 15:47:25 +053098 if (codec->hw_write(codec->control_data, data, 2) == 2)
Arun KSc1f27192008-10-02 14:45:49 +053099 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
107static const char *rec_src_text[] = { "Line", "Mic" };
108static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
Arun KSc1f27192008-10-02 14:45:49 +0530109
110static const struct soc_enum rec_src_enum =
111 SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
112
113static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
114SOC_DAPM_ENUM("Input Select", rec_src_enum);
115
116static const struct soc_enum tlv320aic23_rec_src =
117 SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
118static const struct soc_enum tlv320aic23_deemph =
119 SOC_ENUM_SINGLE(TLV320AIC23_DIGT, 1, 4, deemph_text);
Arun KSc1f27192008-10-02 14:45:49 +0530120
121static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
122static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
Arun KSdf91ddf2008-10-03 17:07:30 +0530123static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
124
125static 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
148static 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) }
Arun KSc1f27192008-10-02 14:45:49 +0530170
171static 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),
Arun KSdf91ddf2008-10-03 17:07:30 +0530181 SOC_TLV320AIC23_SINGLE_TLV("Sidetone Volume", TLV320AIC23_ANLG,
182 6, 4, 0, sidetone_vol_tlv),
Arun KSc1f27192008-10-02 14:45:49 +0530183 SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
184};
185
186/* add non dapm controls */
187static int tlv320aic23_add_controls(struct snd_soc_codec *codec)
188{
189
190 int err, i;
191
192 for (i = 0; i < ARRAY_SIZE(tlv320aic23_snd_controls); i++) {
193 err = snd_ctl_add(codec->card,
194 snd_soc_cnew(&tlv320aic23_snd_controls[i],
195 codec, NULL));
196 if (err < 0)
197 return err;
198 }
199
200 return 0;
201
202}
203
204/* PGA Mixer controls for Line and Mic switch */
205static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
206 SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
207 SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
208 SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
209};
210
211static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
212 SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
213 SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
214 SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
215 &tlv320aic23_rec_src_mux_controls),
216 SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
217 &tlv320aic23_output_mixer_controls[0],
218 ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
219 SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
220 SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
221
222 SND_SOC_DAPM_OUTPUT("LHPOUT"),
223 SND_SOC_DAPM_OUTPUT("RHPOUT"),
224 SND_SOC_DAPM_OUTPUT("LOUT"),
225 SND_SOC_DAPM_OUTPUT("ROUT"),
226
227 SND_SOC_DAPM_INPUT("LLINEIN"),
228 SND_SOC_DAPM_INPUT("RLINEIN"),
229
230 SND_SOC_DAPM_INPUT("MICIN"),
231};
232
233static const struct snd_soc_dapm_route intercon[] = {
234 /* Output Mixer */
235 {"Output Mixer", "Line Bypass Switch", "Line Input"},
236 {"Output Mixer", "Playback Switch", "DAC"},
237 {"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
238
239 /* Outputs */
240 {"RHPOUT", NULL, "Output Mixer"},
241 {"LHPOUT", NULL, "Output Mixer"},
242 {"LOUT", NULL, "Output Mixer"},
243 {"ROUT", NULL, "Output Mixer"},
244
245 /* Inputs */
246 {"Line Input", "NULL", "LLINEIN"},
247 {"Line Input", "NULL", "RLINEIN"},
248
249 {"Mic Input", "NULL", "MICIN"},
250
251 /* input mux */
252 {"Capture Source", "Line", "Line Input"},
253 {"Capture Source", "Mic", "Mic Input"},
254 {"ADC", NULL, "Capture Source"},
255
256};
257
Troy Kisky26df91c2008-11-05 18:53:28 +0000258/* AIC23 driver data */
259struct aic23 {
260 struct snd_soc_codec codec;
261 int mclk;
262 int requested_adc;
263 int requested_dac;
Arun KSc1f27192008-10-02 14:45:49 +0530264};
265
Troy Kisky26df91c2008-11-05 18:53:28 +0000266/*
267 * Common Crystals used
268 * 11.2896 Mhz /128 = *88.2k /192 = 58.8k
269 * 12.0000 Mhz /125 = *96k /136 = 88.235K
270 * 12.2880 Mhz /128 = *96k /192 = 64k
271 * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
272 * 18.4320 Mhz /128 = 144k /192 = *96k
273 */
274
275/*
276 * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
277 * USB BOSR 0-250/2 = 125, 1-272/2 = 136
278 */
279static const int bosr_usb_divisor_table[] = {
280 128, 125, 192, 136
281};
282#define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
283#define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<15))
284static const unsigned short sr_valid_mask[] = {
285 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 0*/
286 LOWER_GROUP|UPPER_GROUP, /* Normal, bosr - 1*/
287 LOWER_GROUP, /* Usb, bosr - 0*/
288 UPPER_GROUP, /* Usb, bosr - 1*/
289};
290/*
291 * Every divisor is a factor of 11*12
292 */
293#define SR_MULT (11*12)
294#define A(x) (x) ? (SR_MULT/x) : 0
295static const unsigned char sr_adc_mult_table[] = {
296 A(2), A(2), A(12), A(12), A(0), A(0), A(3), A(1),
297 A(2), A(2), A(11), A(11), A(0), A(0), A(0), A(1)
298};
299static const unsigned char sr_dac_mult_table[] = {
300 A(2), A(12), A(2), A(12), A(0), A(0), A(3), A(1),
301 A(2), A(11), A(2), A(11), A(0), A(0), A(0), A(1)
302};
303
304static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
305 int dac, int dac_l, int dac_h, int need_dac)
306{
307 if ((adc >= adc_l) && (adc <= adc_h) &&
308 (dac >= dac_l) && (dac <= dac_h)) {
309 int diff_adc = need_adc - adc;
310 int diff_dac = need_dac - dac;
311 return abs(diff_adc) + abs(diff_dac);
312 }
313 return UINT_MAX;
314}
315
316static int find_rate(int mclk, u32 need_adc, u32 need_dac)
317{
318 int i, j;
319 int best_i = -1;
320 int best_j = -1;
321 int best_div = 0;
322 unsigned best_score = UINT_MAX;
323 int adc_l, adc_h, dac_l, dac_h;
324
325 need_adc *= SR_MULT;
326 need_dac *= SR_MULT;
327 /*
328 * rates given are +/- 1/32
329 */
330 adc_l = need_adc - (need_adc >> 5);
331 adc_h = need_adc + (need_adc >> 5);
332 dac_l = need_dac - (need_dac >> 5);
333 dac_h = need_dac + (need_dac >> 5);
334 for (i = 0; i < 4; i++) {
335 int base = mclk / bosr_usb_divisor_table[i];
336 int mask = sr_valid_mask[i];
337 for (j = 0; j < 16; j++, mask >>= 1) {
338 int adc;
339 int dac;
340 int score;
341 if ((mask & 1) == 0)
342 continue;
343 adc = base * sr_adc_mult_table[j];
344 dac = base * sr_dac_mult_table[j];
345 score = get_score(adc, adc_l, adc_h, need_adc,
346 dac, dac_l, dac_h, need_dac);
347 if (best_score > score) {
348 best_score = score;
349 best_i = i;
350 best_j = j;
351 best_div = 0;
352 }
353 score = get_score((adc >> 1), adc_l, adc_h, need_adc,
354 (dac >> 1), dac_l, dac_h, need_dac);
355 /* prefer to have a /2 */
356 if ((score != UINT_MAX) && (best_score >= score)) {
357 best_score = score;
358 best_i = i;
359 best_j = j;
360 best_div = 1;
361 }
362 }
363 }
364 return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
365}
366
367static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk,
368 u32 *sample_rate_adc, u32 *sample_rate_dac)
369{
370 int src = tlv320aic23_read_reg_cache(codec, TLV320AIC23_SRATE);
371 int sr = (src >> 2) & 0x0f;
372 int val = (mclk / bosr_usb_divisor_table[src & 3]);
373 int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
374 int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
375 if (src & TLV320AIC23_CLKIN_HALF) {
376 adc >>= 1;
377 dac >>= 1;
378 }
379 *sample_rate_adc = adc;
380 *sample_rate_dac = dac;
381}
382
383static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk,
384 u32 sample_rate_adc, u32 sample_rate_dac)
385{
386 /* Search for the right sample rate */
387 int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
388 if (data < 0) {
389 printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
390 __func__, sample_rate_adc, sample_rate_dac);
391 return -EINVAL;
392 }
393 tlv320aic23_write(codec, TLV320AIC23_SRATE, data);
394 if (1) {
395 int adc, dac;
396 get_current_sample_rates(codec, mclk, &adc, &dac);
397 printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
398 adc, dac, data);
399 }
400 return 0;
401}
402
Arun KSc1f27192008-10-02 14:45:49 +0530403static int tlv320aic23_add_widgets(struct snd_soc_codec *codec)
404{
405 snd_soc_dapm_new_controls(codec, tlv320aic23_dapm_widgets,
406 ARRAY_SIZE(tlv320aic23_dapm_widgets));
407
408 /* set up audio path interconnects */
409 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
410
411 snd_soc_dapm_new_widgets(codec);
412 return 0;
413}
414
415static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
416 struct snd_pcm_hw_params *params)
417{
418 struct snd_soc_pcm_runtime *rtd = substream->private_data;
419 struct snd_soc_device *socdev = rtd->socdev;
420 struct snd_soc_codec *codec = socdev->codec;
Troy Kisky26df91c2008-11-05 18:53:28 +0000421 u16 iface_reg;
422 int ret;
423 struct aic23 *aic23 = container_of(codec, struct aic23, codec);
424 u32 sample_rate_adc = aic23->requested_adc;
425 u32 sample_rate_dac = aic23->requested_dac;
426 u32 sample_rate = params_rate(params);
427
428 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
429 aic23->requested_dac = sample_rate_dac = sample_rate;
430 if (!sample_rate_adc)
431 sample_rate_adc = sample_rate;
432 } else {
433 aic23->requested_adc = sample_rate_adc = sample_rate;
434 if (!sample_rate_dac)
435 sample_rate_dac = sample_rate;
436 }
437 ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc,
438 sample_rate_dac);
439 if (ret < 0)
440 return ret;
Arun KSc1f27192008-10-02 14:45:49 +0530441
442 iface_reg =
443 tlv320aic23_read_reg_cache(codec,
444 TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
Arun KSc1f27192008-10-02 14:45:49 +0530445 switch (params_format(params)) {
446 case SNDRV_PCM_FORMAT_S16_LE:
447 break;
448 case SNDRV_PCM_FORMAT_S20_3LE:
449 iface_reg |= (0x01 << 2);
450 break;
451 case SNDRV_PCM_FORMAT_S24_LE:
452 iface_reg |= (0x02 << 2);
453 break;
454 case SNDRV_PCM_FORMAT_S32_LE:
455 iface_reg |= (0x03 << 2);
456 break;
457 }
458 tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
459
460 return 0;
461}
462
463static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream)
464{
465 struct snd_soc_pcm_runtime *rtd = substream->private_data;
466 struct snd_soc_device *socdev = rtd->socdev;
467 struct snd_soc_codec *codec = socdev->codec;
468
469 /* set active */
470 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0001);
471
472 return 0;
473}
474
475static void tlv320aic23_shutdown(struct snd_pcm_substream *substream)
476{
477 struct snd_soc_pcm_runtime *rtd = substream->private_data;
478 struct snd_soc_device *socdev = rtd->socdev;
479 struct snd_soc_codec *codec = socdev->codec;
Troy Kisky26df91c2008-11-05 18:53:28 +0000480 struct aic23 *aic23 = container_of(codec, struct aic23, codec);
Arun KSc1f27192008-10-02 14:45:49 +0530481
482 /* deactivate */
483 if (!codec->active) {
484 udelay(50);
485 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
486 }
Troy Kisky26df91c2008-11-05 18:53:28 +0000487 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
488 aic23->requested_dac = 0;
489 else
490 aic23->requested_adc = 0;
Arun KSc1f27192008-10-02 14:45:49 +0530491}
492
493static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
494{
495 struct snd_soc_codec *codec = dai->codec;
496 u16 reg;
497
498 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT);
499 if (mute)
500 reg |= TLV320AIC23_DACM_MUTE;
501
502 else
503 reg &= ~TLV320AIC23_DACM_MUTE;
504
505 tlv320aic23_write(codec, TLV320AIC23_DIGT, reg);
506
507 return 0;
508}
509
510static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
511 unsigned int fmt)
512{
513 struct snd_soc_codec *codec = codec_dai->codec;
514 u16 iface_reg;
515
516 iface_reg =
517 tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
518
519 /* set master/slave audio interface */
520 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
521 case SND_SOC_DAIFMT_CBM_CFM:
522 iface_reg |= TLV320AIC23_MS_MASTER;
523 break;
524 case SND_SOC_DAIFMT_CBS_CFS:
525 break;
526 default:
527 return -EINVAL;
528
529 }
530
531 /* interface format */
532 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
533 case SND_SOC_DAIFMT_I2S:
534 iface_reg |= TLV320AIC23_FOR_I2S;
535 break;
536 case SND_SOC_DAIFMT_DSP_A:
537 iface_reg |= TLV320AIC23_FOR_DSP;
538 break;
539 case SND_SOC_DAIFMT_RIGHT_J:
540 break;
541 case SND_SOC_DAIFMT_LEFT_J:
542 iface_reg |= TLV320AIC23_FOR_LJUST;
543 break;
544 default:
545 return -EINVAL;
546
547 }
548
549 tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
550
551 return 0;
552}
553
554static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
555 int clk_id, unsigned int freq, int dir)
556{
557 struct snd_soc_codec *codec = codec_dai->codec;
Troy Kisky26df91c2008-11-05 18:53:28 +0000558 struct aic23 *aic23 = container_of(codec, struct aic23, codec);
559 aic23->mclk = freq;
560 return 0;
Arun KSc1f27192008-10-02 14:45:49 +0530561}
562
563static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
564 enum snd_soc_bias_level level)
565{
566 u16 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_PWR) & 0xff7f;
567
568 switch (level) {
569 case SND_SOC_BIAS_ON:
570 /* vref/mid, osc on, dac unmute */
571 tlv320aic23_write(codec, TLV320AIC23_PWR, reg);
572 break;
573 case SND_SOC_BIAS_PREPARE:
574 break;
575 case SND_SOC_BIAS_STANDBY:
576 /* everything off except vref/vmid, */
577 tlv320aic23_write(codec, TLV320AIC23_PWR, reg | 0x0040);
578 break;
579 case SND_SOC_BIAS_OFF:
580 /* everything off, dac mute, inactive */
581 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
582 tlv320aic23_write(codec, TLV320AIC23_PWR, 0xffff);
583 break;
584 }
585 codec->bias_level = level;
586 return 0;
587}
588
589#define AIC23_RATES SNDRV_PCM_RATE_8000_96000
590#define AIC23_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
591 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
592
593struct snd_soc_dai tlv320aic23_dai = {
594 .name = "tlv320aic23",
595 .playback = {
596 .stream_name = "Playback",
597 .channels_min = 2,
598 .channels_max = 2,
599 .rates = AIC23_RATES,
600 .formats = AIC23_FORMATS,},
601 .capture = {
602 .stream_name = "Capture",
603 .channels_min = 2,
604 .channels_max = 2,
605 .rates = AIC23_RATES,
606 .formats = AIC23_FORMATS,},
607 .ops = {
608 .prepare = tlv320aic23_pcm_prepare,
609 .hw_params = tlv320aic23_hw_params,
610 .shutdown = tlv320aic23_shutdown,
611 },
612 .dai_ops = {
613 .digital_mute = tlv320aic23_mute,
614 .set_fmt = tlv320aic23_set_dai_fmt,
615 .set_sysclk = tlv320aic23_set_dai_sysclk,
616 }
617};
618EXPORT_SYMBOL_GPL(tlv320aic23_dai);
619
620static int tlv320aic23_suspend(struct platform_device *pdev,
621 pm_message_t state)
622{
623 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
624 struct snd_soc_codec *codec = socdev->codec;
625
626 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
627 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
628
629 return 0;
630}
631
632static int tlv320aic23_resume(struct platform_device *pdev)
633{
634 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
635 struct snd_soc_codec *codec = socdev->codec;
636 int i;
637 u16 reg;
638
639 /* Sync reg_cache with the hardware */
640 for (reg = 0; reg < ARRAY_SIZE(tlv320aic23_reg); i++) {
641 u16 val = tlv320aic23_read_reg_cache(codec, reg);
642 tlv320aic23_write(codec, reg, val);
643 }
644
645 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
646 tlv320aic23_set_bias_level(codec, codec->suspend_bias_level);
647
648 return 0;
649}
650
651/*
652 * initialise the AIC23 driver
653 * register the mixer and dsp interfaces with the kernel
654 */
655static int tlv320aic23_init(struct snd_soc_device *socdev)
656{
657 struct snd_soc_codec *codec = socdev->codec;
658 int ret = 0;
659 u16 reg;
660
661 codec->name = "tlv320aic23";
662 codec->owner = THIS_MODULE;
663 codec->read = tlv320aic23_read_reg_cache;
664 codec->write = tlv320aic23_write;
665 codec->set_bias_level = tlv320aic23_set_bias_level;
666 codec->dai = &tlv320aic23_dai;
667 codec->num_dai = 1;
668 codec->reg_cache_size = ARRAY_SIZE(tlv320aic23_reg);
669 codec->reg_cache =
670 kmemdup(tlv320aic23_reg, sizeof(tlv320aic23_reg), GFP_KERNEL);
671 if (codec->reg_cache == NULL)
672 return -ENOMEM;
673
674 /* Reset codec */
675 tlv320aic23_write(codec, TLV320AIC23_RESET, 0);
676
677 /* register pcms */
678 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
679 if (ret < 0) {
680 printk(KERN_ERR "tlv320aic23: failed to create pcms\n");
681 goto pcm_err;
682 }
683
684 /* power on device */
685 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
686
687 tlv320aic23_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
688
689 /* Unmute input */
690 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_LINVOL);
691 tlv320aic23_write(codec, TLV320AIC23_LINVOL,
692 (reg & (~TLV320AIC23_LIM_MUTED)) |
693 (TLV320AIC23_LRS_ENABLED));
694
695 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_RINVOL);
696 tlv320aic23_write(codec, TLV320AIC23_RINVOL,
697 (reg & (~TLV320AIC23_LIM_MUTED)) |
698 TLV320AIC23_LRS_ENABLED);
699
700 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG);
701 tlv320aic23_write(codec, TLV320AIC23_ANLG,
702 (reg) & (~TLV320AIC23_BYPASS_ON) &
703 (~TLV320AIC23_MICM_MUTED));
704
705 /* Default output volume */
706 tlv320aic23_write(codec, TLV320AIC23_LCHNVOL,
707 TLV320AIC23_DEFAULT_OUT_VOL &
708 TLV320AIC23_OUT_VOL_MASK);
709 tlv320aic23_write(codec, TLV320AIC23_RCHNVOL,
710 TLV320AIC23_DEFAULT_OUT_VOL &
711 TLV320AIC23_OUT_VOL_MASK);
712
713 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x1);
714
715 tlv320aic23_add_controls(codec);
716 tlv320aic23_add_widgets(codec);
717 ret = snd_soc_register_card(socdev);
718 if (ret < 0) {
719 printk(KERN_ERR "tlv320aic23: failed to register card\n");
720 goto card_err;
721 }
722
723 return ret;
724
725card_err:
726 snd_soc_free_pcms(socdev);
727 snd_soc_dapm_free(socdev);
728pcm_err:
729 kfree(codec->reg_cache);
730 return ret;
731}
732static struct snd_soc_device *tlv320aic23_socdev;
733
734#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
735/*
736 * If the i2c layer weren't so broken, we could pass this kind of data
737 * around
738 */
739static int tlv320aic23_codec_probe(struct i2c_client *i2c,
740 const struct i2c_device_id *i2c_id)
741{
742 struct snd_soc_device *socdev = tlv320aic23_socdev;
743 struct snd_soc_codec *codec = socdev->codec;
744 int ret;
745
746 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
747 return -EINVAL;
748
749 i2c_set_clientdata(i2c, codec);
750 codec->control_data = i2c;
751
752 ret = tlv320aic23_init(socdev);
753 if (ret < 0) {
754 printk(KERN_ERR "tlv320aic23: failed to initialise AIC23\n");
755 goto err;
756 }
757 return ret;
758
759err:
760 kfree(codec);
761 kfree(i2c);
762 return ret;
763}
764static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c)
765{
766 put_device(&i2c->dev);
767 return 0;
768}
769
770static const struct i2c_device_id tlv320aic23_id[] = {
771 {"tlv320aic23", 0},
772 {}
773};
774
775MODULE_DEVICE_TABLE(i2c, tlv320aic23_id);
776
777static struct i2c_driver tlv320aic23_i2c_driver = {
778 .driver = {
779 .name = "tlv320aic23",
780 },
781 .probe = tlv320aic23_codec_probe,
782 .remove = __exit_p(tlv320aic23_i2c_remove),
783 .id_table = tlv320aic23_id,
784};
785
786#endif
787
788static int tlv320aic23_probe(struct platform_device *pdev)
789{
790 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
791 struct snd_soc_codec *codec;
Troy Kisky26df91c2008-11-05 18:53:28 +0000792 struct aic23 *aic23;
Arun KSc1f27192008-10-02 14:45:49 +0530793 int ret = 0;
794
795 printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION);
796
Troy Kisky26df91c2008-11-05 18:53:28 +0000797 aic23 = kzalloc(sizeof(struct aic23), GFP_KERNEL);
798 if (aic23 == NULL)
Arun KSc1f27192008-10-02 14:45:49 +0530799 return -ENOMEM;
Troy Kisky26df91c2008-11-05 18:53:28 +0000800 codec = &aic23->codec;
Arun KSc1f27192008-10-02 14:45:49 +0530801 socdev->codec = codec;
802 mutex_init(&codec->mutex);
803 INIT_LIST_HEAD(&codec->dapm_widgets);
804 INIT_LIST_HEAD(&codec->dapm_paths);
805
806 tlv320aic23_socdev = socdev;
807#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
Arun KS4aa02392008-10-13 15:47:25 +0530808 codec->hw_write = (hw_write_t) i2c_master_send;
Arun KSc1f27192008-10-02 14:45:49 +0530809 codec->hw_read = NULL;
810 ret = i2c_add_driver(&tlv320aic23_i2c_driver);
811 if (ret != 0)
812 printk(KERN_ERR "can't add i2c driver");
813#endif
814 return ret;
815}
816
817static int tlv320aic23_remove(struct platform_device *pdev)
818{
819 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
820 struct snd_soc_codec *codec = socdev->codec;
Troy Kisky26df91c2008-11-05 18:53:28 +0000821 struct aic23 *aic23 = container_of(codec, struct aic23, codec);
Arun KSc1f27192008-10-02 14:45:49 +0530822
823 if (codec->control_data)
824 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
825
826 snd_soc_free_pcms(socdev);
827 snd_soc_dapm_free(socdev);
828#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
829 i2c_del_driver(&tlv320aic23_i2c_driver);
830#endif
831 kfree(codec->reg_cache);
Troy Kisky26df91c2008-11-05 18:53:28 +0000832 kfree(aic23);
Arun KSc1f27192008-10-02 14:45:49 +0530833
834 return 0;
835}
836struct snd_soc_codec_device soc_codec_dev_tlv320aic23 = {
837 .probe = tlv320aic23_probe,
838 .remove = tlv320aic23_remove,
839 .suspend = tlv320aic23_suspend,
840 .resume = tlv320aic23_resume,
841};
842EXPORT_SYMBOL_GPL(soc_codec_dev_tlv320aic23);
843
844MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
845MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
846MODULE_LICENSE("GPL");