xref: /openbmc/linux/sound/soc/codecs/tlv320aic23.c (revision fc9a30e8)
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/slab.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/tlv.h>
33 #include <sound/initval.h>
34 
35 #include "tlv320aic23.h"
36 
37 /*
38  * AIC23 register cache
39  */
40 static const u16 tlv320aic23_reg[] = {
41 	0x0097, 0x0097, 0x00F9, 0x00F9,	/* 0 */
42 	0x001A, 0x0004, 0x0007, 0x0001,	/* 4 */
43 	0x0020, 0x0000, 0x0000, 0x0000,	/* 8 */
44 	0x0000, 0x0000, 0x0000, 0x0000,	/* 12 */
45 };
46 
47 static const char *rec_src_text[] = { "Line", "Mic" };
48 static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
49 
50 static const struct soc_enum rec_src_enum =
51 	SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
52 
53 static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
54 SOC_DAPM_ENUM("Input Select", rec_src_enum);
55 
56 static const struct soc_enum tlv320aic23_rec_src =
57 	SOC_ENUM_SINGLE(TLV320AIC23_ANLG, 2, 2, rec_src_text);
58 static const struct soc_enum tlv320aic23_deemph =
59 	SOC_ENUM_SINGLE(TLV320AIC23_DIGT, 1, 4, deemph_text);
60 
61 static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
62 static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
63 static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
64 
65 static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol,
66 	struct snd_ctl_elem_value *ucontrol)
67 {
68 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
69 	u16 val, reg;
70 
71 	val = (ucontrol->value.integer.value[0] & 0x07);
72 
73 	/* linear conversion to userspace
74 	* 000	=	-6db
75 	* 001	=	-9db
76 	* 010	=	-12db
77 	* 011	=	-18db (Min)
78 	* 100	=	0db (Max)
79 	*/
80 	val = (val >= 4) ? 4  : (3 - val);
81 
82 	reg = snd_soc_read(codec, TLV320AIC23_ANLG) & (~0x1C0);
83 	snd_soc_write(codec, TLV320AIC23_ANLG, reg | (val << 6));
84 
85 	return 0;
86 }
87 
88 static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol,
89 	struct snd_ctl_elem_value *ucontrol)
90 {
91 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
92 	u16 val;
93 
94 	val = snd_soc_read(codec, TLV320AIC23_ANLG) & (0x1C0);
95 	val = val >> 6;
96 	val = (val >= 4) ? 4  : (3 -  val);
97 	ucontrol->value.integer.value[0] = val;
98 	return 0;
99 
100 }
101 
102 static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
103 	SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
104 			 TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
105 	SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
106 	SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
107 		     TLV320AIC23_RINVOL, 7, 1, 0),
108 	SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
109 			 TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
110 	SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
111 	SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
112 	SOC_SINGLE_EXT_TLV("Sidetone Volume", TLV320AIC23_ANLG, 6, 4, 0,
113 			   snd_soc_tlv320aic23_get_volsw,
114 			   snd_soc_tlv320aic23_put_volsw, sidetone_vol_tlv),
115 	SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
116 };
117 
118 /* PGA Mixer controls for Line and Mic switch */
119 static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
120 	SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
121 	SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
122 	SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
123 };
124 
125 static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
126 	SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
127 	SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
128 	SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
129 			 &tlv320aic23_rec_src_mux_controls),
130 	SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
131 			   &tlv320aic23_output_mixer_controls[0],
132 			   ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
133 	SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
134 	SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
135 
136 	SND_SOC_DAPM_OUTPUT("LHPOUT"),
137 	SND_SOC_DAPM_OUTPUT("RHPOUT"),
138 	SND_SOC_DAPM_OUTPUT("LOUT"),
139 	SND_SOC_DAPM_OUTPUT("ROUT"),
140 
141 	SND_SOC_DAPM_INPUT("LLINEIN"),
142 	SND_SOC_DAPM_INPUT("RLINEIN"),
143 
144 	SND_SOC_DAPM_INPUT("MICIN"),
145 };
146 
147 static const struct snd_soc_dapm_route tlv320aic23_intercon[] = {
148 	/* Output Mixer */
149 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
150 	{"Output Mixer", "Playback Switch", "DAC"},
151 	{"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
152 
153 	/* Outputs */
154 	{"RHPOUT", NULL, "Output Mixer"},
155 	{"LHPOUT", NULL, "Output Mixer"},
156 	{"LOUT", NULL, "Output Mixer"},
157 	{"ROUT", NULL, "Output Mixer"},
158 
159 	/* Inputs */
160 	{"Line Input", "NULL", "LLINEIN"},
161 	{"Line Input", "NULL", "RLINEIN"},
162 
163 	{"Mic Input", "NULL", "MICIN"},
164 
165 	/* input mux */
166 	{"Capture Source", "Line", "Line Input"},
167 	{"Capture Source", "Mic", "Mic Input"},
168 	{"ADC", NULL, "Capture Source"},
169 
170 };
171 
172 /* AIC23 driver data */
173 struct aic23 {
174 	enum snd_soc_control_type control_type;
175 	int mclk;
176 	int requested_adc;
177 	int requested_dac;
178 };
179 
180 /*
181  * Common Crystals used
182  * 11.2896 Mhz /128 = *88.2k  /192 = 58.8k
183  * 12.0000 Mhz /125 = *96k    /136 = 88.235K
184  * 12.2880 Mhz /128 = *96k    /192 = 64k
185  * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
186  * 18.4320 Mhz /128 = 144k   /192 = *96k
187  */
188 
189 /*
190  * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
191  * USB BOSR 0-250/2 = 125, 1-272/2 = 136
192  */
193 static const int bosr_usb_divisor_table[] = {
194 	128, 125, 192, 136
195 };
196 #define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
197 #define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11)        | (1<<15))
198 static const unsigned short sr_valid_mask[] = {
199 	LOWER_GROUP|UPPER_GROUP,	/* Normal, bosr - 0*/
200 	LOWER_GROUP,			/* Usb, bosr - 0*/
201 	LOWER_GROUP|UPPER_GROUP,	/* Normal, bosr - 1*/
202 	UPPER_GROUP,			/* Usb, bosr - 1*/
203 };
204 /*
205  * Every divisor is a factor of 11*12
206  */
207 #define SR_MULT (11*12)
208 #define A(x) (SR_MULT/x)
209 static const unsigned char sr_adc_mult_table[] = {
210 	A(2), A(2), A(12), A(12),  0, 0, A(3), A(1),
211 	A(2), A(2), A(11), A(11),  0, 0, 0, A(1)
212 };
213 static const unsigned char sr_dac_mult_table[] = {
214 	A(2), A(12), A(2), A(12),  0, 0, A(3), A(1),
215 	A(2), A(11), A(2), A(11),  0, 0, 0, A(1)
216 };
217 
218 static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
219 		int dac, int dac_l, int dac_h, int need_dac)
220 {
221 	if ((adc >= adc_l) && (adc <= adc_h) &&
222 			(dac >= dac_l) && (dac <= dac_h)) {
223 		int diff_adc = need_adc - adc;
224 		int diff_dac = need_dac - dac;
225 		return abs(diff_adc) + abs(diff_dac);
226 	}
227 	return UINT_MAX;
228 }
229 
230 static int find_rate(int mclk, u32 need_adc, u32 need_dac)
231 {
232 	int i, j;
233 	int best_i = -1;
234 	int best_j = -1;
235 	int best_div = 0;
236 	unsigned best_score = UINT_MAX;
237 	int adc_l, adc_h, dac_l, dac_h;
238 
239 	need_adc *= SR_MULT;
240 	need_dac *= SR_MULT;
241 	/*
242 	 * rates given are +/- 1/32
243 	 */
244 	adc_l = need_adc - (need_adc >> 5);
245 	adc_h = need_adc + (need_adc >> 5);
246 	dac_l = need_dac - (need_dac >> 5);
247 	dac_h = need_dac + (need_dac >> 5);
248 	for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) {
249 		int base = mclk / bosr_usb_divisor_table[i];
250 		int mask = sr_valid_mask[i];
251 		for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table);
252 				j++, mask >>= 1) {
253 			int adc;
254 			int dac;
255 			int score;
256 			if ((mask & 1) == 0)
257 				continue;
258 			adc = base * sr_adc_mult_table[j];
259 			dac = base * sr_dac_mult_table[j];
260 			score = get_score(adc, adc_l, adc_h, need_adc,
261 					dac, dac_l, dac_h, need_dac);
262 			if (best_score > score) {
263 				best_score = score;
264 				best_i = i;
265 				best_j = j;
266 				best_div = 0;
267 			}
268 			score = get_score((adc >> 1), adc_l, adc_h, need_adc,
269 					(dac >> 1), dac_l, dac_h, need_dac);
270 			/* prefer to have a /2 */
271 			if ((score != UINT_MAX) && (best_score >= score)) {
272 				best_score = score;
273 				best_i = i;
274 				best_j = j;
275 				best_div = 1;
276 			}
277 		}
278 	}
279 	return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
280 }
281 
282 #ifdef DEBUG
283 static void get_current_sample_rates(struct snd_soc_codec *codec, int mclk,
284 		u32 *sample_rate_adc, u32 *sample_rate_dac)
285 {
286 	int src = snd_soc_read(codec, TLV320AIC23_SRATE);
287 	int sr = (src >> 2) & 0x0f;
288 	int val = (mclk / bosr_usb_divisor_table[src & 3]);
289 	int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
290 	int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
291 	if (src & TLV320AIC23_CLKIN_HALF) {
292 		adc >>= 1;
293 		dac >>= 1;
294 	}
295 	*sample_rate_adc = adc;
296 	*sample_rate_dac = dac;
297 }
298 #endif
299 
300 static int set_sample_rate_control(struct snd_soc_codec *codec, int mclk,
301 		u32 sample_rate_adc, u32 sample_rate_dac)
302 {
303 	/* Search for the right sample rate */
304 	int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
305 	if (data < 0) {
306 		printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
307 				__func__, sample_rate_adc, sample_rate_dac);
308 		return -EINVAL;
309 	}
310 	snd_soc_write(codec, TLV320AIC23_SRATE, data);
311 #ifdef DEBUG
312 	{
313 		u32 adc, dac;
314 		get_current_sample_rates(codec, mclk, &adc, &dac);
315 		printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
316 			adc, dac, data);
317 	}
318 #endif
319 	return 0;
320 }
321 
322 static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
323 				 struct snd_pcm_hw_params *params,
324 				 struct snd_soc_dai *dai)
325 {
326 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
327 	struct snd_soc_codec *codec = rtd->codec;
328 	u16 iface_reg;
329 	int ret;
330 	struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
331 	u32 sample_rate_adc = aic23->requested_adc;
332 	u32 sample_rate_dac = aic23->requested_dac;
333 	u32 sample_rate = params_rate(params);
334 
335 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
336 		aic23->requested_dac = sample_rate_dac = sample_rate;
337 		if (!sample_rate_adc)
338 			sample_rate_adc = sample_rate;
339 	} else {
340 		aic23->requested_adc = sample_rate_adc = sample_rate;
341 		if (!sample_rate_dac)
342 			sample_rate_dac = sample_rate;
343 	}
344 	ret = set_sample_rate_control(codec, aic23->mclk, sample_rate_adc,
345 			sample_rate_dac);
346 	if (ret < 0)
347 		return ret;
348 
349 	iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
350 
351 	switch (params_format(params)) {
352 	case SNDRV_PCM_FORMAT_S16_LE:
353 		break;
354 	case SNDRV_PCM_FORMAT_S20_3LE:
355 		iface_reg |= (0x01 << 2);
356 		break;
357 	case SNDRV_PCM_FORMAT_S24_LE:
358 		iface_reg |= (0x02 << 2);
359 		break;
360 	case SNDRV_PCM_FORMAT_S32_LE:
361 		iface_reg |= (0x03 << 2);
362 		break;
363 	}
364 	snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
365 
366 	return 0;
367 }
368 
369 static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
370 				   struct snd_soc_dai *dai)
371 {
372 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
373 	struct snd_soc_codec *codec = rtd->codec;
374 
375 	/* set active */
376 	snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0001);
377 
378 	return 0;
379 }
380 
381 static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
382 				 struct snd_soc_dai *dai)
383 {
384 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
385 	struct snd_soc_codec *codec = rtd->codec;
386 	struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
387 
388 	/* deactivate */
389 	if (!codec->active) {
390 		udelay(50);
391 		snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0);
392 	}
393 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
394 		aic23->requested_dac = 0;
395 	else
396 		aic23->requested_adc = 0;
397 }
398 
399 static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
400 {
401 	struct snd_soc_codec *codec = dai->codec;
402 	u16 reg;
403 
404 	reg = snd_soc_read(codec, TLV320AIC23_DIGT);
405 	if (mute)
406 		reg |= TLV320AIC23_DACM_MUTE;
407 
408 	else
409 		reg &= ~TLV320AIC23_DACM_MUTE;
410 
411 	snd_soc_write(codec, TLV320AIC23_DIGT, reg);
412 
413 	return 0;
414 }
415 
416 static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
417 				   unsigned int fmt)
418 {
419 	struct snd_soc_codec *codec = codec_dai->codec;
420 	u16 iface_reg;
421 
422 	iface_reg = snd_soc_read(codec, TLV320AIC23_DIGT_FMT) & (~0x03);
423 
424 	/* set master/slave audio interface */
425 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
426 	case SND_SOC_DAIFMT_CBM_CFM:
427 		iface_reg |= TLV320AIC23_MS_MASTER;
428 		break;
429 	case SND_SOC_DAIFMT_CBS_CFS:
430 		iface_reg &= ~TLV320AIC23_MS_MASTER;
431 		break;
432 	default:
433 		return -EINVAL;
434 
435 	}
436 
437 	/* interface format */
438 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
439 	case SND_SOC_DAIFMT_I2S:
440 		iface_reg |= TLV320AIC23_FOR_I2S;
441 		break;
442 	case SND_SOC_DAIFMT_DSP_A:
443 		iface_reg |= TLV320AIC23_LRP_ON;
444 	case SND_SOC_DAIFMT_DSP_B:
445 		iface_reg |= TLV320AIC23_FOR_DSP;
446 		break;
447 	case SND_SOC_DAIFMT_RIGHT_J:
448 		break;
449 	case SND_SOC_DAIFMT_LEFT_J:
450 		iface_reg |= TLV320AIC23_FOR_LJUST;
451 		break;
452 	default:
453 		return -EINVAL;
454 
455 	}
456 
457 	snd_soc_write(codec, TLV320AIC23_DIGT_FMT, iface_reg);
458 
459 	return 0;
460 }
461 
462 static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
463 				      int clk_id, unsigned int freq, int dir)
464 {
465 	struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
466 	aic23->mclk = freq;
467 	return 0;
468 }
469 
470 static int tlv320aic23_set_bias_level(struct snd_soc_codec *codec,
471 				      enum snd_soc_bias_level level)
472 {
473 	u16 reg = snd_soc_read(codec, TLV320AIC23_PWR) & 0xff7f;
474 
475 	switch (level) {
476 	case SND_SOC_BIAS_ON:
477 		/* vref/mid, osc on, dac unmute */
478 		reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
479 			TLV320AIC23_DAC_OFF);
480 		snd_soc_write(codec, TLV320AIC23_PWR, reg);
481 		break;
482 	case SND_SOC_BIAS_PREPARE:
483 		break;
484 	case SND_SOC_BIAS_STANDBY:
485 		/* everything off except vref/vmid, */
486 		snd_soc_write(codec, TLV320AIC23_PWR,
487 			      reg | TLV320AIC23_CLK_OFF);
488 		break;
489 	case SND_SOC_BIAS_OFF:
490 		/* everything off, dac mute, inactive */
491 		snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x0);
492 		snd_soc_write(codec, TLV320AIC23_PWR, 0xffff);
493 		break;
494 	}
495 	codec->dapm.bias_level = level;
496 	return 0;
497 }
498 
499 #define AIC23_RATES	SNDRV_PCM_RATE_8000_96000
500 #define AIC23_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
501 			 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
502 
503 static const struct snd_soc_dai_ops tlv320aic23_dai_ops = {
504 	.prepare	= tlv320aic23_pcm_prepare,
505 	.hw_params	= tlv320aic23_hw_params,
506 	.shutdown	= tlv320aic23_shutdown,
507 	.digital_mute	= tlv320aic23_mute,
508 	.set_fmt	= tlv320aic23_set_dai_fmt,
509 	.set_sysclk	= tlv320aic23_set_dai_sysclk,
510 };
511 
512 static struct snd_soc_dai_driver tlv320aic23_dai = {
513 	.name = "tlv320aic23-hifi",
514 	.playback = {
515 		     .stream_name = "Playback",
516 		     .channels_min = 2,
517 		     .channels_max = 2,
518 		     .rates = AIC23_RATES,
519 		     .formats = AIC23_FORMATS,},
520 	.capture = {
521 		    .stream_name = "Capture",
522 		    .channels_min = 2,
523 		    .channels_max = 2,
524 		    .rates = AIC23_RATES,
525 		    .formats = AIC23_FORMATS,},
526 	.ops = &tlv320aic23_dai_ops,
527 };
528 
529 static int tlv320aic23_suspend(struct snd_soc_codec *codec)
530 {
531 	tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
532 
533 	return 0;
534 }
535 
536 static int tlv320aic23_resume(struct snd_soc_codec *codec)
537 {
538 	snd_soc_cache_sync(codec);
539 	tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
540 
541 	return 0;
542 }
543 
544 static int tlv320aic23_probe(struct snd_soc_codec *codec)
545 {
546 	struct aic23 *aic23 = snd_soc_codec_get_drvdata(codec);
547 	int ret;
548 
549 	ret = snd_soc_codec_set_cache_io(codec, 7, 9, aic23->control_type);
550 	if (ret < 0) {
551 		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
552 		return ret;
553 	}
554 
555 	/* Reset codec */
556 	snd_soc_write(codec, TLV320AIC23_RESET, 0);
557 
558 	/* Write the register default value to cache for reserved registers,
559 	 * so the write to the these registers are suppressed by the cache
560 	 * restore code when it skips writes of default registers.
561 	 */
562 	snd_soc_cache_write(codec, 0x0A, 0);
563 	snd_soc_cache_write(codec, 0x0B, 0);
564 	snd_soc_cache_write(codec, 0x0C, 0);
565 	snd_soc_cache_write(codec, 0x0D, 0);
566 	snd_soc_cache_write(codec, 0x0E, 0);
567 
568 	/* power on device */
569 	tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
570 
571 	snd_soc_write(codec, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
572 
573 	/* Unmute input */
574 	snd_soc_update_bits(codec, TLV320AIC23_LINVOL,
575 			    TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
576 
577 	snd_soc_update_bits(codec, TLV320AIC23_RINVOL,
578 			    TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
579 
580 	snd_soc_update_bits(codec, TLV320AIC23_ANLG,
581 			    TLV320AIC23_BYPASS_ON | TLV320AIC23_MICM_MUTED,
582 			    0);
583 
584 	/* Default output volume */
585 	snd_soc_write(codec, TLV320AIC23_LCHNVOL,
586 		      TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
587 	snd_soc_write(codec, TLV320AIC23_RCHNVOL,
588 		      TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
589 
590 	snd_soc_write(codec, TLV320AIC23_ACTIVE, 0x1);
591 
592 	snd_soc_add_codec_controls(codec, tlv320aic23_snd_controls,
593 				ARRAY_SIZE(tlv320aic23_snd_controls));
594 
595 	return 0;
596 }
597 
598 static int tlv320aic23_remove(struct snd_soc_codec *codec)
599 {
600 	tlv320aic23_set_bias_level(codec, SND_SOC_BIAS_OFF);
601 	return 0;
602 }
603 
604 static struct snd_soc_codec_driver soc_codec_dev_tlv320aic23 = {
605 	.reg_cache_size = ARRAY_SIZE(tlv320aic23_reg),
606 	.reg_word_size = sizeof(u16),
607 	.reg_cache_default = tlv320aic23_reg,
608 	.probe = tlv320aic23_probe,
609 	.remove = tlv320aic23_remove,
610 	.suspend = tlv320aic23_suspend,
611 	.resume = tlv320aic23_resume,
612 	.set_bias_level = tlv320aic23_set_bias_level,
613 	.dapm_widgets = tlv320aic23_dapm_widgets,
614 	.num_dapm_widgets = ARRAY_SIZE(tlv320aic23_dapm_widgets),
615 	.dapm_routes = tlv320aic23_intercon,
616 	.num_dapm_routes = ARRAY_SIZE(tlv320aic23_intercon),
617 };
618 
619 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
620 /*
621  * If the i2c layer weren't so broken, we could pass this kind of data
622  * around
623  */
624 static int tlv320aic23_codec_probe(struct i2c_client *i2c,
625 				   const struct i2c_device_id *i2c_id)
626 {
627 	struct aic23 *aic23;
628 	int ret;
629 
630 	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
631 		return -EINVAL;
632 
633 	aic23 = devm_kzalloc(&i2c->dev, sizeof(struct aic23), GFP_KERNEL);
634 	if (aic23 == NULL)
635 		return -ENOMEM;
636 
637 	i2c_set_clientdata(i2c, aic23);
638 	aic23->control_type = SND_SOC_I2C;
639 
640 	ret =  snd_soc_register_codec(&i2c->dev,
641 			&soc_codec_dev_tlv320aic23, &tlv320aic23_dai, 1);
642 	return ret;
643 }
644 static int __exit tlv320aic23_i2c_remove(struct i2c_client *i2c)
645 {
646 	snd_soc_unregister_codec(&i2c->dev);
647 	return 0;
648 }
649 
650 static const struct i2c_device_id tlv320aic23_id[] = {
651 	{"tlv320aic23", 0},
652 	{}
653 };
654 
655 MODULE_DEVICE_TABLE(i2c, tlv320aic23_id);
656 
657 static struct i2c_driver tlv320aic23_i2c_driver = {
658 	.driver = {
659 		   .name = "tlv320aic23-codec",
660 		   },
661 	.probe = tlv320aic23_codec_probe,
662 	.remove = __exit_p(tlv320aic23_i2c_remove),
663 	.id_table = tlv320aic23_id,
664 };
665 
666 #endif
667 
668 static int __init tlv320aic23_modinit(void)
669 {
670 	int ret;
671 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
672 	ret = i2c_add_driver(&tlv320aic23_i2c_driver);
673 	if (ret != 0) {
674 		printk(KERN_ERR "Failed to register TLV320AIC23 I2C driver: %d\n",
675 		       ret);
676 	}
677 #endif
678 	return ret;
679 }
680 module_init(tlv320aic23_modinit);
681 
682 static void __exit tlv320aic23_exit(void)
683 {
684 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
685 	i2c_del_driver(&tlv320aic23_i2c_driver);
686 #endif
687 }
688 module_exit(tlv320aic23_exit);
689 
690 MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
691 MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
692 MODULE_LICENSE("GPL");
693