Initial commit
[kernel/linux-3.0.git] / sound / soc / codecs / tlv320aic23.c
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 <linux/slab.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/soc.h>
33 #include <sound/tlv.h>
34 #include <sound/initval.h>
35
36 #include "tlv320aic23.h"
37
38 #define AIC23_VERSION "0.1"
39
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
81         u8 data[2];
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 > 9 && reg != 15) {
89                 printk(KERN_WARNING "%s Invalid register R%u\n", __func__, reg);
90                 return -1;
91         }
92
93         data[0] = (reg << 1) | (value >> 8 & 0x01);
94         data[1] = value & 0xff;
95
96         tlv320aic23_write_reg_cache(codec, reg, value);
97
98         if (codec->hw_write(codec->control_data, data, 2) == 2)
99                 return 0;
100
101         printk(KERN_ERR "%s cannot write %03x to register R%u\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"};
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);
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);
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) }
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),
181         SOC_TLV320AIC23_SINGLE_TLV("Sidetone Volume", TLV320AIC23_ANLG,
182                                   6, 4, 0, sidetone_vol_tlv),
183         SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
184 };
185
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 tlv320aic23_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
240 /* AIC23 driver data */
241 struct aic23 {
242         enum snd_soc_control_type control_type;
243         void *control_data;
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*/
269         LOWER_GROUP,                    /* Usb, bosr - 0*/
270         LOWER_GROUP|UPPER_GROUP,        /* Normal, bosr - 1*/
271         UPPER_GROUP,                    /* Usb, bosr - 1*/
272 };
273 /*
274  * Every divisor is a factor of 11*12
275  */
276 #define SR_MULT (11*12)
277 #define A(x) (SR_MULT/x)
278 static const unsigned char sr_adc_mult_table[] = {
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)
281 };
282 static const unsigned char sr_dac_mult_table[] = {
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)
285 };
286
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);
317         for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) {
318                 int base = mclk / bosr_usb_divisor_table[i];
319                 int mask = sr_valid_mask[i];
320                 for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table);
321                                 j++, mask >>= 1) {
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
351 #ifdef DEBUG
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 }
367 #endif
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);
380 #ifdef DEBUG
381         {
382                 u32 adc, dac;
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         }
387 #endif
388         return 0;
389 }
390
391 static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
392                                  struct snd_pcm_hw_params *params,
393                                  struct snd_soc_dai *dai)
394 {
395         struct snd_soc_pcm_runtime *rtd = substream->private_data;
396         struct snd_soc_codec *codec = rtd->codec;
397         u16 iface_reg;
398         int ret;
399         struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
400         u32 sample_rate_adc = aic23->requested_adc;
401         u32 sample_rate_dac = aic23->requested_dac;
402         u32 sample_rate = params_rate(params);
403
404         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
405                 aic23->requested_dac = sample_rate_dac = sample_rate;
406                 if (!sample_rate_adc)
407                         sample_rate_adc = sample_rate;
408         } else {
409                 aic23->requested_adc = sample_rate_adc = sample_rate;
410                 if (!sample_rate_dac)
411                         sample_rate_dac = sample_rate;
412         }
413         ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc,
414                         sample_rate_dac);
415         if (ret < 0)
416                 return ret;
417
418         iface_reg =
419             tlv320aic23_read_reg_cache(codec,
420                                        TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
421         switch (params_format(params)) {
422         case SNDRV_PCM_FORMAT_S16_LE:
423                 break;
424         case SNDRV_PCM_FORMAT_S20_3LE:
425                 iface_reg |= (0x01 << 2);
426                 break;
427         case SNDRV_PCM_FORMAT_S24_LE:
428                 iface_reg |= (0x02 << 2);
429                 break;
430         case SNDRV_PCM_FORMAT_S32_LE:
431                 iface_reg |= (0x03 << 2);
432                 break;
433         }
434         tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
435
436         return 0;
437 }
438
439 static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
440                                    struct snd_soc_dai *dai)
441 {
442         struct snd_soc_pcm_runtime *rtd = substream->private_data;
443         struct snd_soc_codec *codec = rtd->codec;
444
445         /* set active */
446         tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0001);
447
448         return 0;
449 }
450
451 static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
452                                  struct snd_soc_dai *dai)
453 {
454         struct snd_soc_pcm_runtime *rtd = substream->private_data;
455         struct snd_soc_codec *codec = rtd->codec;
456         struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
457
458         /* deactivate */
459         if (!codec->active) {
460                 udelay(50);
461                 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
462         }
463         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
464                 aic23->requested_dac = 0;
465         else
466                 aic23->requested_adc = 0;
467 }
468
469 static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
470 {
471         struct snd_soc_codec *codec = dai->codec;
472         u16 reg;
473
474         reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT);
475         if (mute)
476                 reg |= TLV320AIC23_DACM_MUTE;
477
478         else
479                 reg &= ~TLV320AIC23_DACM_MUTE;
480
481         tlv320aic23_write(codec, TLV320AIC23_DIGT, reg);
482
483         return 0;
484 }
485
486 static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
487                                    unsigned int fmt)
488 {
489         struct snd_soc_codec *codec = codec_dai->codec;
490         u16 iface_reg;
491
492         iface_reg =
493             tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
494
495         /* set master/slave audio interface */
496         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
497         case SND_SOC_DAIFMT_CBM_CFM:
498                 iface_reg |= TLV320AIC23_MS_MASTER;
499                 break;
500         case SND_SOC_DAIFMT_CBS_CFS:
501                 break;
502         default:
503                 return -EINVAL;
504
505         }
506
507         /* interface format */
508         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
509         case SND_SOC_DAIFMT_I2S:
510                 iface_reg |= TLV320AIC23_FOR_I2S;
511                 break;
512         case SND_SOC_DAIFMT_DSP_A:
513                 iface_reg |= TLV320AIC23_LRP_ON;
514         case SND_SOC_DAIFMT_DSP_B:
515                 iface_reg |= TLV320AIC23_FOR_DSP;
516                 break;
517         case SND_SOC_DAIFMT_RIGHT_J:
518                 break;
519         case SND_SOC_DAIFMT_LEFT_J:
520                 iface_reg |= TLV320AIC23_FOR_LJUST;
521                 break;
522         default:
523                 return -EINVAL;
524
525         }
526
527         tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
528
529         return 0;
530 }
531
532 static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
533                                       int clk_id, unsigned int freq, int dir)
534 {
535         struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
536         aic23->mclk = freq;
537         return 0;
538 }
539
540 static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
541                                       enum snd_soc_bias_level level)
542 {
543         u16 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_PWR) & 0xff7f;
544
545         switch (level) {
546         case SND_SOC_BIAS_ON:
547                 /* vref/mid, osc on, dac unmute */
548                 reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
549                         TLV320AIC23_DAC_OFF);
550                 tlv320aic23_write(codec, TLV320AIC23_PWR, reg);
551                 break;
552         case SND_SOC_BIAS_PREPARE:
553                 break;
554         case SND_SOC_BIAS_STANDBY:
555                 /* everything off except vref/vmid, */
556                 tlv320aic23_write(codec, TLV320AIC23_PWR, reg | \
557                         TLV320AIC23_CLK_OFF);
558                 break;
559         case SND_SOC_BIAS_OFF:
560                 /* everything off, dac mute, inactive */
561                 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
562                 tlv320aic23_write(codec, TLV320AIC23_PWR, 0xffff);
563                 break;
564         }
565         codec->dapm.bias_level = level;
566         return 0;
567 }
568
569 #define AIC23_RATES     SNDRV_PCM_RATE_8000_96000
570 #define AIC23_FORMATS   (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
571                          SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
572
573 static struct snd_soc_dai_ops tlv320aic23_dai_ops = {
574         .prepare        = tlv320aic23_pcm_prepare,
575         .hw_params      = tlv320aic23_hw_params,
576         .shutdown       = tlv320aic23_shutdown,
577         .digital_mute   = tlv320aic23_mute,
578         .set_fmt        = tlv320aic23_set_dai_fmt,
579         .set_sysclk     = tlv320aic23_set_dai_sysclk,
580 };
581
582 static struct snd_soc_dai_driver tlv320aic23_dai = {
583         .name = "tlv320aic23-hifi",
584         .playback = {
585                      .stream_name = "Playback",
586                      .channels_min = 2,
587                      .channels_max = 2,
588                      .rates = AIC23_RATES,
589                      .formats = AIC23_FORMATS,},
590         .capture = {
591                     .stream_name = "Capture",
592                     .channels_min = 2,
593                     .channels_max = 2,
594                     .rates = AIC23_RATES,
595                     .formats = AIC23_FORMATS,},
596         .ops = &tlv320aic23_dai_ops,
597 };
598
599 static int tlv320aic23_suspend(struct snd_soc_codec *codec,
600                                pm_message_t state)
601 {
602         tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
603
604         return 0;
605 }
606
607 static int tlv320aic23_resume(struct snd_soc_codec *codec)
608 {
609         u16 reg;
610
611         /* Sync reg_cache with the hardware */
612         for (reg = 0; reg <= TLV320AIC23_ACTIVE; reg++) {
613                 u16 val = tlv320aic23_read_reg_cache(codec, reg);
614                 tlv320aic23_write(codec, reg, val);
615         }
616         tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
617
618         return 0;
619 }
620
621 static int tlv320aic23_probe(struct snd_soc_codec *codec)
622 {
623         struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
624         int reg;
625
626         printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION);
627         codec->control_data = aic23->control_data;
628         codec->hw_write = (hw_write_t)i2c_master_send;
629         codec->hw_read = NULL;
630
631         /* Reset codec */
632         tlv320aic23_write(codec, TLV320AIC23_RESET, 0);
633
634         /* power on device */
635         tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
636
637         tlv320aic23_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
638
639         /* Unmute input */
640         reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_LINVOL);
641         tlv320aic23_write(codec, TLV320AIC23_LINVOL,
642                           (reg & (~TLV320AIC23_LIM_MUTED)) |
643                           (TLV320AIC23_LRS_ENABLED));
644
645         reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_RINVOL);
646         tlv320aic23_write(codec, TLV320AIC23_RINVOL,
647                           (reg & (~TLV320AIC23_LIM_MUTED)) |
648                           TLV320AIC23_LRS_ENABLED);
649
650         reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG);
651         tlv320aic23_write(codec, TLV320AIC23_ANLG,
652                          (reg) & (~TLV320AIC23_BYPASS_ON) &
653                          (~TLV320AIC23_MICM_MUTED));
654
655         /* Default output volume */
656         tlv320aic23_write(codec, TLV320AIC23_LCHNVOL,
657                           TLV320AIC23_DEFAULT_OUT_VOL &
658                           TLV320AIC23_OUT_VOL_MASK);
659         tlv320aic23_write(codec, TLV320AIC23_RCHNVOL,
660                           TLV320AIC23_DEFAULT_OUT_VOL &
661                           TLV320AIC23_OUT_VOL_MASK);
662
663         tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x1);
664
665         snd_soc_add_controls(codec, tlv320aic23_snd_controls,
666                                 ARRAY_SIZE(tlv320aic23_snd_controls));
667
668         return 0;
669 }
670
671 static int tlv320aic23_remove(struct snd_soc_codec *codec)
672 {
673         tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
674         return 0;
675 }
676
677 static struct snd_soc_codec_driver soc_codec_dev_tlv320aic23 = {
678         .reg_cache_size = ARRAY_SIZE(tlv320aic23_reg),
679         .reg_word_size = sizeof(u16),
680         .reg_cache_default = tlv320aic23_reg,
681         .probe = tlv320aic23_probe,
682         .remove = tlv320aic23_remove,
683         .suspend = tlv320aic23_suspend,
684         .resume = tlv320aic23_resume,
685         .read = tlv320aic23_read_reg_cache,
686         .write = tlv320aic23_write,
687         .set_bias_level = tlv320aic23_set_bias_level,
688         .dapm_widgets = tlv320aic23_dapm_widgets,
689         .num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets),
690         .dapm_routes = tlv320aic23_intercon,
691         .num_dapm_routes = ARRAY_SIZE(tlv320aic23_intercon),
692 };
693
694 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
695 /*
696  * If the i2c layer weren't so broken, we could pass this kind of data
697  * around
698  */
699 static int tlv320aic23_codec_probe(struct i2c_client *i2c,
700                                    const struct i2c_device_id *i2c_id)
701 {
702         struct aic23 *aic23;
703         int ret;
704
705         if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
706                 return -EINVAL;
707
708         aic23 = kzalloc(sizeof(struct aic23), GFP_KERNEL);
709         if (aic23 == NULL)
710                 return -ENOMEM;
711
712         i2c_set_clientdata(i2c, aic23);
713         aic23->control_data = i2c;
714         aic23->control_type = SND_SOC_I2C;
715
716         ret =  snd_soc_register_codec(&i2c->dev,
717                         &soc_codec_dev_tlv320aic23, &tlv320aic23_dai, 1);
718         if (ret < 0)
719                 kfree(aic23);
720         return ret;
721 }
722 static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c)
723 {
724         snd_soc_unregister_codec(&i2c->dev);
725         kfree(i2c_get_clientdata(i2c));
726         return 0;
727 }
728
729 static const struct i2c_device_id tlv320aic23_id[] = {
730         {"tlv320aic23", 0},
731         {}
732 };
733
734 MODULE_DEVICE_TABLE(i2c, tlv320aic23_id);
735
736 static struct i2c_driver tlv320aic23_i2c_driver = {
737         .driver = {
738                    .name = "tlv320aic23-codec",
739                    },
740         .probe = tlv320aic23_codec_probe,
741         .remove = __exit_p(tlv320aic23_i2c_remove),
742         .id_table = tlv320aic23_id,
743 };
744
745 #endif
746
747 static int __init tlv320aic23_modinit(void)
748 {
749         int ret;
750 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
751         ret = i2c_add_driver(&tlv320aic23_i2c_driver);
752         if (ret != 0) {
753                 printk(KERN_ERR "Failed to register TLV320AIC23 I2C driver: %d\n",
754                        ret);
755         }
756 #endif
757         return ret;
758 }
759 module_init(tlv320aic23_modinit);
760
761 static void __exit tlv320aic23_exit(void)
762 {
763 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
764         i2c_del_driver(&tlv320aic23_i2c_driver);
765 #endif
766 }
767 module_exit(tlv320aic23_exit);
768
769 MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
770 MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
771 MODULE_LICENSE("GPL");