Linux Audio

Check our new training course

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