]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - sound/soc/codecs/tlv320aic23.c
ALSA: ASoC: Add custom SOC_SINGLE_TLV for tlv320aic23 codec
[linux-2.6-omap-h63xx.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 <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
38 #define AUDIO_NAME "tlv320aic23"
39 #define AIC23_VERSION "0.1"
40
41 struct tlv320aic23_srate_reg_info {
42         u32 sample_rate;
43         u8 control;             /* SR3, SR2, SR1, SR0 and BOSR */
44         u8 divider;             /* if 0 CLKIN = MCLK, if 1 CLKIN = MCLK/2 */
45 };
46
47 /*
48  * AIC23 register cache
49  */
50 static const u16 tlv320aic23_reg[] = {
51         0x0097, 0x0097, 0x00F9, 0x00F9, /* 0 */
52         0x001A, 0x0004, 0x0007, 0x0001, /* 4 */
53         0x0020, 0x0000, 0x0000, 0x0000, /* 8 */
54         0x0000, 0x0000, 0x0000, 0x0000, /* 12 */
55 };
56
57 /*
58  * read tlv320aic23 register cache
59  */
60 static inline unsigned int tlv320aic23_read_reg_cache(struct snd_soc_codec
61                                                       *codec, unsigned int reg)
62 {
63         u16 *cache = codec->reg_cache;
64         if (reg >= ARRAY_SIZE(tlv320aic23_reg))
65                 return -1;
66         return cache[reg];
67 }
68
69 /*
70  * write tlv320aic23 register cache
71  */
72 static inline void tlv320aic23_write_reg_cache(struct snd_soc_codec *codec,
73                                                u8 reg, u16 value)
74 {
75         u16 *cache = codec->reg_cache;
76         if (reg >= ARRAY_SIZE(tlv320aic23_reg))
77                 return;
78         cache[reg] = value;
79 }
80
81 /*
82  * write to the tlv320aic23 register space
83  */
84 static int tlv320aic23_write(struct snd_soc_codec *codec, unsigned int reg,
85                              unsigned int value)
86 {
87
88         u8 data;
89
90         /* TLV320AIC23 has 7 bit address and 9 bits of data
91          * so we need to switch one data bit into reg and rest
92          * of data into val
93          */
94
95         if ((reg < 0 || reg > 9) && (reg != 15)) {
96                 printk(KERN_WARNING "%s Invalid register R%d\n", __func__, reg);
97                 return -1;
98         }
99
100         data = (reg << 1) | (value >> 8 & 0x01);
101
102         tlv320aic23_write_reg_cache(codec, reg, value);
103
104         if (codec->hw_write(codec->control_data, data,
105                             (value & 0xff)) == 0)
106                 return 0;
107
108         printk(KERN_ERR "%s cannot write %03x to register R%d\n", __func__,
109                value, reg);
110
111         return -EIO;
112 }
113
114 static const char *rec_src_text[] = { "Line", "Mic" };
115 static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
116
117 static const struct soc_enum rec_src_enum =
118         SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
119
120 static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
121 SOC_DAPM_ENUM("Input Select", rec_src_enum);
122
123 static const struct soc_enum tlv320aic23_rec_src =
124         SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
125 static const struct soc_enum tlv320aic23_deemph =
126         SOC_ENUM_SINGLE(TLV320AIC23_DIGT, 1, 4, deemph_text);
127
128 static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
129 static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
130 static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
131
132 static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol,
133         struct snd_ctl_elem_value *ucontrol)
134 {
135         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
136         u16 val, reg;
137
138         val = (ucontrol->value.integer.value[0] & 0x07);
139
140         /* linear conversion to userspace
141         * 000   =       -6db
142         * 001   =       -9db
143         * 010   =       -12db
144         * 011   =       -18db (Min)
145         * 100   =       0db (Max)
146         */
147         val = (val >= 4) ? 4  : (3 - val);
148
149         reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG) & (~0x1C0);
150         tlv320aic23_write(codec, TLV320AIC23_ANLG, reg | (val << 6));
151
152         return 0;
153 }
154
155 static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol,
156         struct snd_ctl_elem_value *ucontrol)
157 {
158         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
159         u16 val;
160
161         val = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG) & (0x1C0);
162         val = val >> 6;
163         val = (val >= 4) ? 4  : (3 -  val);
164         ucontrol->value.integer.value[0] = val;
165         return 0;
166
167 }
168
169 #define SOC_TLV320AIC23_SINGLE_TLV(xname, reg, shift, max, invert, tlv_array) \
170 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
171         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
172                  SNDRV_CTL_ELEM_ACCESS_READWRITE,\
173         .tlv.p = (tlv_array), \
174         .info = snd_soc_info_volsw, .get = snd_soc_tlv320aic23_get_volsw,\
175         .put = snd_soc_tlv320aic23_put_volsw, \
176         .private_value =  SOC_SINGLE_VALUE(reg, shift, max, invert) }
177
178 static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
179         SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
180                          TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
181         SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
182         SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
183                      TLV320AIC23_RINVOL, 7, 1, 0),
184         SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
185                          TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
186         SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
187         SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
188         SOC_TLV320AIC23_SINGLE_TLV("Sidetone Volume", TLV320AIC23_ANLG,
189                                   6, 4, 0, sidetone_vol_tlv),
190         SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
191 };
192
193 /* add non dapm controls */
194 static int tlv320aic23_add_controls(struct snd_soc_codec *codec)
195 {
196
197         int err, i;
198
199         for (i = 0; i < ARRAY_SIZE(tlv320aic23_snd_controls); i++) {
200                 err = snd_ctl_add(codec->card,
201                                   snd_soc_cnew(&tlv320aic23_snd_controls[i],
202                                                codec, NULL));
203                 if (err < 0)
204                         return err;
205         }
206
207         return 0;
208
209 }
210
211 /* PGA Mixer controls for Line and Mic switch */
212 static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
213         SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
214         SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
215         SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
216 };
217
218 static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
219         SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
220         SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
221         SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
222                          &tlv320aic23_rec_src_mux_controls),
223         SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
224                            &tlv320aic23_output_mixer_controls[0],
225                            ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
226         SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
227         SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
228
229         SND_SOC_DAPM_OUTPUT("LHPOUT"),
230         SND_SOC_DAPM_OUTPUT("RHPOUT"),
231         SND_SOC_DAPM_OUTPUT("LOUT"),
232         SND_SOC_DAPM_OUTPUT("ROUT"),
233
234         SND_SOC_DAPM_INPUT("LLINEIN"),
235         SND_SOC_DAPM_INPUT("RLINEIN"),
236
237         SND_SOC_DAPM_INPUT("MICIN"),
238 };
239
240 static const struct snd_soc_dapm_route intercon[] = {
241         /* Output Mixer */
242         {"Output Mixer", "Line Bypass Switch", "Line Input"},
243         {"Output Mixer", "Playback Switch", "DAC"},
244         {"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
245
246         /* Outputs */
247         {"RHPOUT", NULL, "Output Mixer"},
248         {"LHPOUT", NULL, "Output Mixer"},
249         {"LOUT", NULL, "Output Mixer"},
250         {"ROUT", NULL, "Output Mixer"},
251
252         /* Inputs */
253         {"Line Input", "NULL", "LLINEIN"},
254         {"Line Input", "NULL", "RLINEIN"},
255
256         {"Mic Input", "NULL", "MICIN"},
257
258         /* input mux */
259         {"Capture Source", "Line", "Line Input"},
260         {"Capture Source", "Mic", "Mic Input"},
261         {"ADC", NULL, "Capture Source"},
262
263 };
264
265 /* tlv320aic23 related */
266 static const struct tlv320aic23_srate_reg_info srate_reg_info[] = {
267         {4000, 0x06, 1},        /*  4000 */
268         {8000, 0x06, 0},        /*  8000 */
269         {16000, 0x0C, 1},       /* 16000 */
270         {22050, 0x11, 1},       /* 22050 */
271         {24000, 0x00, 1},       /* 24000 */
272         {32000, 0x0C, 0},       /* 32000 */
273         {44100, 0x11, 0},       /* 44100 */
274         {48000, 0x00, 0},       /* 48000 */
275         {88200, 0x1F, 0},       /* 88200 */
276         {96000, 0x0E, 0},       /* 96000 */
277 };
278
279 static int tlv320aic23_add_widgets(struct snd_soc_codec *codec)
280 {
281         snd_soc_dapm_new_controls(codec, tlv320aic23_dapm_widgets,
282                                   ARRAY_SIZE(tlv320aic23_dapm_widgets));
283
284         /* set up audio path interconnects */
285         snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
286
287         snd_soc_dapm_new_widgets(codec);
288         return 0;
289 }
290
291 static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
292                                  struct snd_pcm_hw_params *params)
293 {
294         struct snd_soc_pcm_runtime *rtd = substream->private_data;
295         struct snd_soc_device *socdev = rtd->socdev;
296         struct snd_soc_codec *codec = socdev->codec;
297         u16 iface_reg, data;
298         u8 count = 0;
299
300         iface_reg =
301             tlv320aic23_read_reg_cache(codec,
302                                        TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
303
304         /* Search for the right sample rate */
305         /* Verify what happens if the rate is not supported
306          * now it goes to 96Khz */
307         while ((srate_reg_info[count].sample_rate != params_rate(params)) &&
308                (count < ARRAY_SIZE(srate_reg_info))) {
309                 count++;
310         }
311
312         data =  (srate_reg_info[count].divider << TLV320AIC23_CLKIN_SHIFT) |
313                 (srate_reg_info[count]. control << TLV320AIC23_BOSR_SHIFT) |
314                 TLV320AIC23_USB_CLK_ON;
315
316         tlv320aic23_write(codec, TLV320AIC23_SRATE, data);
317
318         switch (params_format(params)) {
319         case SNDRV_PCM_FORMAT_S16_LE:
320                 break;
321         case SNDRV_PCM_FORMAT_S20_3LE:
322                 iface_reg |= (0x01 << 2);
323                 break;
324         case SNDRV_PCM_FORMAT_S24_LE:
325                 iface_reg |= (0x02 << 2);
326                 break;
327         case SNDRV_PCM_FORMAT_S32_LE:
328                 iface_reg |= (0x03 << 2);
329                 break;
330         }
331         tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
332
333         return 0;
334 }
335
336 static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream)
337 {
338         struct snd_soc_pcm_runtime *rtd = substream->private_data;
339         struct snd_soc_device *socdev = rtd->socdev;
340         struct snd_soc_codec *codec = socdev->codec;
341
342         /* set active */
343         tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0001);
344
345         return 0;
346 }
347
348 static void tlv320aic23_shutdown(struct snd_pcm_substream *substream)
349 {
350         struct snd_soc_pcm_runtime *rtd = substream->private_data;
351         struct snd_soc_device *socdev = rtd->socdev;
352         struct snd_soc_codec *codec = socdev->codec;
353
354         /* deactivate */
355         if (!codec->active) {
356                 udelay(50);
357                 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
358         }
359 }
360
361 static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
362 {
363         struct snd_soc_codec *codec = dai->codec;
364         u16 reg;
365
366         reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT);
367         if (mute)
368                 reg |= TLV320AIC23_DACM_MUTE;
369
370         else
371                 reg &= ~TLV320AIC23_DACM_MUTE;
372
373         tlv320aic23_write(codec, TLV320AIC23_DIGT, reg);
374
375         return 0;
376 }
377
378 static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
379                                    unsigned int fmt)
380 {
381         struct snd_soc_codec *codec = codec_dai->codec;
382         u16 iface_reg;
383
384         iface_reg =
385             tlv320aic23_read_reg_cache(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
386
387         /* set master/slave audio interface */
388         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
389         case SND_SOC_DAIFMT_CBM_CFM:
390                 iface_reg |= TLV320AIC23_MS_MASTER;
391                 break;
392         case SND_SOC_DAIFMT_CBS_CFS:
393                 break;
394         default:
395                 return -EINVAL;
396
397         }
398
399         /* interface format */
400         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
401         case SND_SOC_DAIFMT_I2S:
402                 iface_reg |= TLV320AIC23_FOR_I2S;
403                 break;
404         case SND_SOC_DAIFMT_DSP_A:
405                 iface_reg |= TLV320AIC23_FOR_DSP;
406                 break;
407         case SND_SOC_DAIFMT_RIGHT_J:
408                 break;
409         case SND_SOC_DAIFMT_LEFT_J:
410                 iface_reg |= TLV320AIC23_FOR_LJUST;
411                 break;
412         default:
413                 return -EINVAL;
414
415         }
416
417         tlv320aic23_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
418
419         return 0;
420 }
421
422 static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
423                                       int clk_id, unsigned int freq, int dir)
424 {
425         struct snd_soc_codec *codec = codec_dai->codec;
426
427         switch (freq) {
428         case 12000000:
429                 return 0;
430         }
431         return -EINVAL;
432 }
433
434 static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
435                                       enum snd_soc_bias_level level)
436 {
437         u16 reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_PWR) & 0xff7f;
438
439         switch (level) {
440         case SND_SOC_BIAS_ON:
441                 /* vref/mid, osc on, dac unmute */
442                 tlv320aic23_write(codec, TLV320AIC23_PWR, reg);
443                 break;
444         case SND_SOC_BIAS_PREPARE:
445                 break;
446         case SND_SOC_BIAS_STANDBY:
447                 /* everything off except vref/vmid, */
448                 tlv320aic23_write(codec, TLV320AIC23_PWR, reg | 0x0040);
449                 break;
450         case SND_SOC_BIAS_OFF:
451                 /* everything off, dac mute, inactive */
452                 tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
453                 tlv320aic23_write(codec, TLV320AIC23_PWR, 0xffff);
454                 break;
455         }
456         codec->bias_level = level;
457         return 0;
458 }
459
460 #define AIC23_RATES     SNDRV_PCM_RATE_8000_96000
461 #define AIC23_FORMATS   (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
462                          SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
463
464 struct snd_soc_dai tlv320aic23_dai = {
465         .name = "tlv320aic23",
466         .playback = {
467                      .stream_name = "Playback",
468                      .channels_min = 2,
469                      .channels_max = 2,
470                      .rates = AIC23_RATES,
471                      .formats = AIC23_FORMATS,},
472         .capture = {
473                     .stream_name = "Capture",
474                     .channels_min = 2,
475                     .channels_max = 2,
476                     .rates = AIC23_RATES,
477                     .formats = AIC23_FORMATS,},
478         .ops = {
479                 .prepare = tlv320aic23_pcm_prepare,
480                 .hw_params = tlv320aic23_hw_params,
481                 .shutdown = tlv320aic23_shutdown,
482                 },
483         .dai_ops = {
484                     .digital_mute = tlv320aic23_mute,
485                     .set_fmt = tlv320aic23_set_dai_fmt,
486                     .set_sysclk = tlv320aic23_set_dai_sysclk,
487                     }
488 };
489 EXPORT_SYMBOL_GPL(tlv320aic23_dai);
490
491 static int tlv320aic23_suspend(struct platform_device *pdev,
492                                pm_message_t state)
493 {
494         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
495         struct snd_soc_codec *codec = socdev->codec;
496
497         tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x0);
498         tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
499
500         return 0;
501 }
502
503 static int tlv320aic23_resume(struct platform_device *pdev)
504 {
505         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
506         struct snd_soc_codec *codec = socdev->codec;
507         int i;
508         u16 reg;
509
510         /* Sync reg_cache with the hardware */
511         for (reg = 0; reg < ARRAY_SIZE(tlv320aic23_reg); i++) {
512                 u16 val = tlv320aic23_read_reg_cache(codec, reg);
513                 tlv320aic23_write(codec, reg, val);
514         }
515
516         tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
517         tlv320aic23_set_bias_level(codec, codec->suspend_bias_level);
518
519         return 0;
520 }
521
522 /*
523  * initialise the AIC23 driver
524  * register the mixer and dsp interfaces with the kernel
525  */
526 static int tlv320aic23_init(struct snd_soc_device *socdev)
527 {
528         struct snd_soc_codec *codec = socdev->codec;
529         int ret = 0;
530         u16 reg;
531
532         codec->name = "tlv320aic23";
533         codec->owner = THIS_MODULE;
534         codec->read = tlv320aic23_read_reg_cache;
535         codec->write = tlv320aic23_write;
536         codec->set_bias_level = tlv320aic23_set_bias_level;
537         codec->dai = &tlv320aic23_dai;
538         codec->num_dai = 1;
539         codec->reg_cache_size = ARRAY_SIZE(tlv320aic23_reg);
540         codec->reg_cache =
541             kmemdup(tlv320aic23_reg, sizeof(tlv320aic23_reg), GFP_KERNEL);
542         if (codec->reg_cache == NULL)
543                 return -ENOMEM;
544
545         /* Reset codec */
546         tlv320aic23_write(codec, TLV320AIC23_RESET, 0);
547
548         /* register pcms */
549         ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
550         if (ret < 0) {
551                 printk(KERN_ERR "tlv320aic23: failed to create pcms\n");
552                 goto pcm_err;
553         }
554
555         /* power on device */
556         tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
557
558         tlv320aic23_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
559
560         /* Unmute input */
561         reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_LINVOL);
562         tlv320aic23_write(codec, TLV320AIC23_LINVOL,
563                           (reg & (~TLV320AIC23_LIM_MUTED)) |
564                           (TLV320AIC23_LRS_ENABLED));
565
566         reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_RINVOL);
567         tlv320aic23_write(codec, TLV320AIC23_RINVOL,
568                           (reg & (~TLV320AIC23_LIM_MUTED)) |
569                           TLV320AIC23_LRS_ENABLED);
570
571         reg = tlv320aic23_read_reg_cache(codec, TLV320AIC23_ANLG);
572         tlv320aic23_write(codec, TLV320AIC23_ANLG,
573                          (reg) & (~TLV320AIC23_BYPASS_ON) &
574                          (~TLV320AIC23_MICM_MUTED));
575
576         /* Default output volume */
577         tlv320aic23_write(codec, TLV320AIC23_LCHNVOL,
578                           TLV320AIC23_DEFAULT_OUT_VOL &
579                           TLV320AIC23_OUT_VOL_MASK);
580         tlv320aic23_write(codec, TLV320AIC23_RCHNVOL,
581                           TLV320AIC23_DEFAULT_OUT_VOL &
582                           TLV320AIC23_OUT_VOL_MASK);
583
584         tlv320aic23_write(codec, TLV320AIC23_ACTIVE, 0x1);
585
586         tlv320aic23_add_controls(codec);
587         tlv320aic23_add_widgets(codec);
588         ret = snd_soc_register_card(socdev);
589         if (ret < 0) {
590                 printk(KERN_ERR "tlv320aic23: failed to register card\n");
591                 goto card_err;
592         }
593
594         return ret;
595
596 card_err:
597         snd_soc_free_pcms(socdev);
598         snd_soc_dapm_free(socdev);
599 pcm_err:
600         kfree(codec->reg_cache);
601         return ret;
602 }
603 static struct snd_soc_device *tlv320aic23_socdev;
604
605 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
606 /*
607  * If the i2c layer weren't so broken, we could pass this kind of data
608  * around
609  */
610 static int tlv320aic23_codec_probe(struct i2c_client *i2c,
611                                    const struct i2c_device_id *i2c_id)
612 {
613         struct snd_soc_device *socdev = tlv320aic23_socdev;
614         struct snd_soc_codec *codec = socdev->codec;
615         int ret;
616
617         if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
618                 return -EINVAL;
619
620         i2c_set_clientdata(i2c, codec);
621         codec->control_data = i2c;
622
623         ret = tlv320aic23_init(socdev);
624         if (ret < 0) {
625                 printk(KERN_ERR "tlv320aic23: failed to initialise AIC23\n");
626                 goto err;
627         }
628         return ret;
629
630 err:
631         kfree(codec);
632         kfree(i2c);
633         return ret;
634 }
635 static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c)
636 {
637         put_device(&i2c->dev);
638         return 0;
639 }
640
641 static const struct i2c_device_id tlv320aic23_id[] = {
642         {"tlv320aic23", 0},
643         {}
644 };
645
646 MODULE_DEVICE_TABLE(i2c, tlv320aic23_id);
647
648 static struct i2c_driver tlv320aic23_i2c_driver = {
649         .driver = {
650                    .name = "tlv320aic23",
651                    },
652         .probe = tlv320aic23_codec_probe,
653         .remove = __exit_p(tlv320aic23_i2c_remove),
654         .id_table = tlv320aic23_id,
655 };
656
657 #endif
658
659 static int tlv320aic23_probe(struct platform_device *pdev)
660 {
661         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
662         struct snd_soc_codec *codec;
663         int ret = 0;
664
665         printk(KERN_INFO "AIC23 Audio Codec %s\n", AIC23_VERSION);
666
667         codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
668         if (codec == NULL)
669                 return -ENOMEM;
670
671         socdev->codec = codec;
672         mutex_init(&codec->mutex);
673         INIT_LIST_HEAD(&codec->dapm_widgets);
674         INIT_LIST_HEAD(&codec->dapm_paths);
675
676         tlv320aic23_socdev = socdev;
677 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
678         codec->hw_write = (hw_write_t) i2c_smbus_write_byte_data;
679         codec->hw_read = NULL;
680         ret = i2c_add_driver(&tlv320aic23_i2c_driver);
681         if (ret != 0)
682                 printk(KERN_ERR "can't add i2c driver");
683 #endif
684         return ret;
685 }
686
687 static int tlv320aic23_remove(struct platform_device *pdev)
688 {
689         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
690         struct snd_soc_codec *codec = socdev->codec;
691
692         if (codec->control_data)
693                 tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
694
695         snd_soc_free_pcms(socdev);
696         snd_soc_dapm_free(socdev);
697 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
698         i2c_del_driver(&tlv320aic23_i2c_driver);
699 #endif
700         kfree(codec->reg_cache);
701         kfree(codec);
702
703         return 0;
704 }
705 struct snd_soc_codec_device soc_codec_dev_tlv320aic23 = {
706         .probe = tlv320aic23_probe,
707         .remove = tlv320aic23_remove,
708         .suspend = tlv320aic23_suspend,
709         .resume = tlv320aic23_resume,
710 };
711 EXPORT_SYMBOL_GPL(soc_codec_dev_tlv320aic23);
712
713 MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
714 MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
715 MODULE_LICENSE("GPL");