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