xref: /openbmc/linux/sound/soc/codecs/tlv320aic23.c (revision ff06ac2a)
1c1f27190SArun KS /*
2c1f27190SArun KS  * ALSA SoC TLV320AIC23 codec driver
3c1f27190SArun KS  *
4c1f27190SArun KS  * Author:      Arun KS, <arunks@mistralsolutions.com>
5c1f27190SArun KS  * Copyright:   (C) 2008 Mistral Solutions Pvt Ltd.,
6c1f27190SArun KS  *
7c1f27190SArun KS  * Based on sound/soc/codecs/wm8731.c by Richard Purdie
8c1f27190SArun KS  *
9c1f27190SArun KS  * This program is free software; you can redistribute it and/or modify
10c1f27190SArun KS  * it under the terms of the GNU General Public License version 2 as
11c1f27190SArun KS  * published by the Free Software Foundation.
12c1f27190SArun KS  *
13c1f27190SArun KS  * Notes:
14c1f27190SArun KS  *  The AIC23 is a driver for a low power stereo audio
15c1f27190SArun KS  *  codec tlv320aic23
16c1f27190SArun KS  *
17c1f27190SArun KS  *  The machine layer should disable unsupported inputs/outputs by
18c1f27190SArun KS  *  snd_soc_dapm_disable_pin(codec, "LHPOUT"), etc.
19c1f27190SArun KS  */
20c1f27190SArun KS 
21c1f27190SArun KS #include <linux/module.h>
22c1f27190SArun KS #include <linux/moduleparam.h>
23c1f27190SArun KS #include <linux/init.h>
24c1f27190SArun KS #include <linux/delay.h>
25c1f27190SArun KS #include <linux/pm.h>
264aa11d67SMark Brown #include <linux/regmap.h>
275a0e3ad6STejun Heo #include <linux/slab.h>
28c1f27190SArun KS #include <sound/core.h>
29c1f27190SArun KS #include <sound/pcm.h>
30c1f27190SArun KS #include <sound/pcm_params.h>
31c1f27190SArun KS #include <sound/soc.h>
32c1f27190SArun KS #include <sound/tlv.h>
33c1f27190SArun KS #include <sound/initval.h>
34c1f27190SArun KS 
35c1f27190SArun KS #include "tlv320aic23.h"
36c1f27190SArun KS 
37c1f27190SArun KS /*
38c1f27190SArun KS  * AIC23 register cache
39c1f27190SArun KS  */
404aa11d67SMark Brown static const struct reg_default tlv320aic23_reg[] = {
414aa11d67SMark Brown 	{  0, 0x0097 },
424aa11d67SMark Brown 	{  1, 0x0097 },
434aa11d67SMark Brown 	{  2, 0x00F9 },
444aa11d67SMark Brown 	{  3, 0x00F9 },
454aa11d67SMark Brown 	{  4, 0x001A },
464aa11d67SMark Brown 	{  5, 0x0004 },
474aa11d67SMark Brown 	{  6, 0x0007 },
484aa11d67SMark Brown 	{  7, 0x0001 },
494aa11d67SMark Brown 	{  8, 0x0020 },
504aa11d67SMark Brown 	{  9, 0x0000 },
514aa11d67SMark Brown };
524aa11d67SMark Brown 
53b3fc5725SMax Filippov const struct regmap_config tlv320aic23_regmap = {
544aa11d67SMark Brown 	.reg_bits = 7,
554aa11d67SMark Brown 	.val_bits = 9,
564aa11d67SMark Brown 
574aa11d67SMark Brown 	.max_register = TLV320AIC23_RESET,
584aa11d67SMark Brown 	.reg_defaults = tlv320aic23_reg,
594aa11d67SMark Brown 	.num_reg_defaults = ARRAY_SIZE(tlv320aic23_reg),
604aa11d67SMark Brown 	.cache_type = REGCACHE_RBTREE,
61c1f27190SArun KS };
6240423285SMax Filippov EXPORT_SYMBOL(tlv320aic23_regmap);
63c1f27190SArun KS 
64c1f27190SArun KS static const char *rec_src_text[] = { "Line", "Mic" };
65c1f27190SArun KS static const char *deemph_text[] = {"None", "32Khz", "44.1Khz", "48Khz"};
66c1f27190SArun KS 
67806057ccSTakashi Iwai static SOC_ENUM_SINGLE_DECL(rec_src_enum,
68806057ccSTakashi Iwai 			    TLV320AIC23_ANLG, 2, rec_src_text);
69c1f27190SArun KS 
70c1f27190SArun KS static const struct snd_kcontrol_new tlv320aic23_rec_src_mux_controls =
71c1f27190SArun KS SOC_DAPM_ENUM("Input Select", rec_src_enum);
72c1f27190SArun KS 
73806057ccSTakashi Iwai static SOC_ENUM_SINGLE_DECL(tlv320aic23_rec_src,
74806057ccSTakashi Iwai 			    TLV320AIC23_ANLG, 2, rec_src_text);
75806057ccSTakashi Iwai static SOC_ENUM_SINGLE_DECL(tlv320aic23_deemph,
76806057ccSTakashi Iwai 			    TLV320AIC23_DIGT, 1, deemph_text);
77c1f27190SArun KS 
78c1f27190SArun KS static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
79c1f27190SArun KS static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
80df91ddf1SArun KS static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
81df91ddf1SArun KS 
82df91ddf1SArun KS static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol,
83df91ddf1SArun KS 	struct snd_ctl_elem_value *ucontrol)
84df91ddf1SArun KS {
85ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
86df91ddf1SArun KS 	u16 val, reg;
87df91ddf1SArun KS 
88df91ddf1SArun KS 	val = (ucontrol->value.integer.value[0] & 0x07);
89df91ddf1SArun KS 
90df91ddf1SArun KS 	/* linear conversion to userspace
91df91ddf1SArun KS 	* 000	=	-6db
92df91ddf1SArun KS 	* 001	=	-9db
93df91ddf1SArun KS 	* 010	=	-12db
94df91ddf1SArun KS 	* 011	=	-18db (Min)
95df91ddf1SArun KS 	* 100	=	0db (Max)
96df91ddf1SArun KS 	*/
97df91ddf1SArun KS 	val = (val >= 4) ? 4  : (3 - val);
98df91ddf1SArun KS 
99ff06ac2aSKuninori Morimoto 	reg = snd_soc_component_read32(component, TLV320AIC23_ANLG) & (~0x1C0);
100ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_ANLG, reg | (val << 6));
101df91ddf1SArun KS 
102df91ddf1SArun KS 	return 0;
103df91ddf1SArun KS }
104df91ddf1SArun KS 
105df91ddf1SArun KS static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol,
106df91ddf1SArun KS 	struct snd_ctl_elem_value *ucontrol)
107df91ddf1SArun KS {
108ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
109df91ddf1SArun KS 	u16 val;
110df91ddf1SArun KS 
111ff06ac2aSKuninori Morimoto 	val = snd_soc_component_read32(component, TLV320AIC23_ANLG) & (0x1C0);
112df91ddf1SArun KS 	val = val >> 6;
113df91ddf1SArun KS 	val = (val >= 4) ? 4  : (3 -  val);
114df91ddf1SArun KS 	ucontrol->value.integer.value[0] = val;
115df91ddf1SArun KS 	return 0;
116df91ddf1SArun KS 
117df91ddf1SArun KS }
118df91ddf1SArun KS 
119c1f27190SArun KS static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
120c1f27190SArun KS 	SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
121c1f27190SArun KS 			 TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
122c1f27190SArun KS 	SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
123c1f27190SArun KS 	SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
124c1f27190SArun KS 		     TLV320AIC23_RINVOL, 7, 1, 0),
125c1f27190SArun KS 	SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
126c1f27190SArun KS 			 TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
127c1f27190SArun KS 	SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
128c1f27190SArun KS 	SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
1290f9887d1SPeter Ujfalusi 	SOC_SINGLE_EXT_TLV("Sidetone Volume", TLV320AIC23_ANLG, 6, 4, 0,
1300f9887d1SPeter Ujfalusi 			   snd_soc_tlv320aic23_get_volsw,
1310f9887d1SPeter Ujfalusi 			   snd_soc_tlv320aic23_put_volsw, sidetone_vol_tlv),
132c1f27190SArun KS 	SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
133c1f27190SArun KS };
134c1f27190SArun KS 
135c1f27190SArun KS /* PGA Mixer controls for Line and Mic switch */
136c1f27190SArun KS static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
137c1f27190SArun KS 	SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
138c1f27190SArun KS 	SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
139c1f27190SArun KS 	SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
140c1f27190SArun KS };
141c1f27190SArun KS 
142c1f27190SArun KS static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
143c1f27190SArun KS 	SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
144c1f27190SArun KS 	SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
145c1f27190SArun KS 	SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
146c1f27190SArun KS 			 &tlv320aic23_rec_src_mux_controls),
147c1f27190SArun KS 	SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
148c1f27190SArun KS 			   &tlv320aic23_output_mixer_controls[0],
149c1f27190SArun KS 			   ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
150c1f27190SArun KS 	SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
151c1f27190SArun KS 	SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
152c1f27190SArun KS 
153c1f27190SArun KS 	SND_SOC_DAPM_OUTPUT("LHPOUT"),
154c1f27190SArun KS 	SND_SOC_DAPM_OUTPUT("RHPOUT"),
155c1f27190SArun KS 	SND_SOC_DAPM_OUTPUT("LOUT"),
156c1f27190SArun KS 	SND_SOC_DAPM_OUTPUT("ROUT"),
157c1f27190SArun KS 
158c1f27190SArun KS 	SND_SOC_DAPM_INPUT("LLINEIN"),
159c1f27190SArun KS 	SND_SOC_DAPM_INPUT("RLINEIN"),
160c1f27190SArun KS 
161c1f27190SArun KS 	SND_SOC_DAPM_INPUT("MICIN"),
162c1f27190SArun KS };
163c1f27190SArun KS 
164a7dca707SLu Guanqun static const struct snd_soc_dapm_route tlv320aic23_intercon[] = {
165c1f27190SArun KS 	/* Output Mixer */
166c1f27190SArun KS 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
167c1f27190SArun KS 	{"Output Mixer", "Playback Switch", "DAC"},
168c1f27190SArun KS 	{"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
169c1f27190SArun KS 
170c1f27190SArun KS 	/* Outputs */
171c1f27190SArun KS 	{"RHPOUT", NULL, "Output Mixer"},
172c1f27190SArun KS 	{"LHPOUT", NULL, "Output Mixer"},
173c1f27190SArun KS 	{"LOUT", NULL, "Output Mixer"},
174c1f27190SArun KS 	{"ROUT", NULL, "Output Mixer"},
175c1f27190SArun KS 
176c1f27190SArun KS 	/* Inputs */
177a03faba9SLiviu Dudau 	{"Line Input", NULL, "LLINEIN"},
178a03faba9SLiviu Dudau 	{"Line Input", NULL, "RLINEIN"},
179a03faba9SLiviu Dudau 	{"Mic Input", NULL, "MICIN"},
180c1f27190SArun KS 
181c1f27190SArun KS 	/* input mux */
182c1f27190SArun KS 	{"Capture Source", "Line", "Line Input"},
183c1f27190SArun KS 	{"Capture Source", "Mic", "Mic Input"},
184c1f27190SArun KS 	{"ADC", NULL, "Capture Source"},
185c1f27190SArun KS 
186c1f27190SArun KS };
187c1f27190SArun KS 
18826df91c3STroy Kisky /* AIC23 driver data */
18926df91c3STroy Kisky struct aic23 {
1904aa11d67SMark Brown 	struct regmap *regmap;
19126df91c3STroy Kisky 	int mclk;
19226df91c3STroy Kisky 	int requested_adc;
19326df91c3STroy Kisky 	int requested_dac;
194c1f27190SArun KS };
195c1f27190SArun KS 
19626df91c3STroy Kisky /*
19726df91c3STroy Kisky  * Common Crystals used
19826df91c3STroy Kisky  * 11.2896 Mhz /128 = *88.2k  /192 = 58.8k
19926df91c3STroy Kisky  * 12.0000 Mhz /125 = *96k    /136 = 88.235K
20026df91c3STroy Kisky  * 12.2880 Mhz /128 = *96k    /192 = 64k
20126df91c3STroy Kisky  * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
20226df91c3STroy Kisky  * 18.4320 Mhz /128 = 144k   /192 = *96k
20326df91c3STroy Kisky  */
20426df91c3STroy Kisky 
20526df91c3STroy Kisky /*
20626df91c3STroy Kisky  * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
20726df91c3STroy Kisky  * USB BOSR 0-250/2 = 125, 1-272/2 = 136
20826df91c3STroy Kisky  */
20926df91c3STroy Kisky static const int bosr_usb_divisor_table[] = {
21026df91c3STroy Kisky 	128, 125, 192, 136
21126df91c3STroy Kisky };
21226df91c3STroy Kisky #define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
21326df91c3STroy Kisky #define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11)        | (1<<15))
21426df91c3STroy Kisky static const unsigned short sr_valid_mask[] = {
21526df91c3STroy Kisky 	LOWER_GROUP|UPPER_GROUP,	/* Normal, bosr - 0*/
21626df91c3STroy Kisky 	LOWER_GROUP,			/* Usb, bosr - 0*/
217bab02124STroy Kisky 	LOWER_GROUP|UPPER_GROUP,	/* Normal, bosr - 1*/
21826df91c3STroy Kisky 	UPPER_GROUP,			/* Usb, bosr - 1*/
21926df91c3STroy Kisky };
22026df91c3STroy Kisky /*
22126df91c3STroy Kisky  * Every divisor is a factor of 11*12
22226df91c3STroy Kisky  */
22326df91c3STroy Kisky #define SR_MULT (11*12)
224ccff4b15STroy Kisky #define A(x) (SR_MULT/x)
22526df91c3STroy Kisky static const unsigned char sr_adc_mult_table[] = {
226ccff4b15STroy Kisky 	A(2), A(2), A(12), A(12),  0, 0, A(3), A(1),
227ccff4b15STroy Kisky 	A(2), A(2), A(11), A(11),  0, 0, 0, A(1)
22826df91c3STroy Kisky };
22926df91c3STroy Kisky static const unsigned char sr_dac_mult_table[] = {
230ccff4b15STroy Kisky 	A(2), A(12), A(2), A(12),  0, 0, A(3), A(1),
231ccff4b15STroy Kisky 	A(2), A(11), A(2), A(11),  0, 0, 0, A(1)
23226df91c3STroy Kisky };
23326df91c3STroy Kisky 
23426df91c3STroy Kisky static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
23526df91c3STroy Kisky 		int dac, int dac_l, int dac_h, int need_dac)
23626df91c3STroy Kisky {
23726df91c3STroy Kisky 	if ((adc >= adc_l) && (adc <= adc_h) &&
23826df91c3STroy Kisky 			(dac >= dac_l) && (dac <= dac_h)) {
23926df91c3STroy Kisky 		int diff_adc = need_adc - adc;
24026df91c3STroy Kisky 		int diff_dac = need_dac - dac;
24126df91c3STroy Kisky 		return abs(diff_adc) + abs(diff_dac);
24226df91c3STroy Kisky 	}
24326df91c3STroy Kisky 	return UINT_MAX;
24426df91c3STroy Kisky }
24526df91c3STroy Kisky 
24626df91c3STroy Kisky static int find_rate(int mclk, u32 need_adc, u32 need_dac)
24726df91c3STroy Kisky {
24826df91c3STroy Kisky 	int i, j;
24926df91c3STroy Kisky 	int best_i = -1;
25026df91c3STroy Kisky 	int best_j = -1;
25126df91c3STroy Kisky 	int best_div = 0;
25226df91c3STroy Kisky 	unsigned best_score = UINT_MAX;
25326df91c3STroy Kisky 	int adc_l, adc_h, dac_l, dac_h;
25426df91c3STroy Kisky 
25526df91c3STroy Kisky 	need_adc *= SR_MULT;
25626df91c3STroy Kisky 	need_dac *= SR_MULT;
25726df91c3STroy Kisky 	/*
25826df91c3STroy Kisky 	 * rates given are +/- 1/32
25926df91c3STroy Kisky 	 */
26026df91c3STroy Kisky 	adc_l = need_adc - (need_adc >> 5);
26126df91c3STroy Kisky 	adc_h = need_adc + (need_adc >> 5);
26226df91c3STroy Kisky 	dac_l = need_dac - (need_dac >> 5);
26326df91c3STroy Kisky 	dac_h = need_dac + (need_dac >> 5);
2648d702f23SMark Brown 	for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) {
26526df91c3STroy Kisky 		int base = mclk / bosr_usb_divisor_table[i];
26626df91c3STroy Kisky 		int mask = sr_valid_mask[i];
2678d702f23SMark Brown 		for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table);
2688d702f23SMark Brown 				j++, mask >>= 1) {
26926df91c3STroy Kisky 			int adc;
27026df91c3STroy Kisky 			int dac;
27126df91c3STroy Kisky 			int score;
27226df91c3STroy Kisky 			if ((mask & 1) == 0)
27326df91c3STroy Kisky 				continue;
27426df91c3STroy Kisky 			adc = base * sr_adc_mult_table[j];
27526df91c3STroy Kisky 			dac = base * sr_dac_mult_table[j];
27626df91c3STroy Kisky 			score = get_score(adc, adc_l, adc_h, need_adc,
27726df91c3STroy Kisky 					dac, dac_l, dac_h, need_dac);
27826df91c3STroy Kisky 			if (best_score > score) {
27926df91c3STroy Kisky 				best_score = score;
28026df91c3STroy Kisky 				best_i = i;
28126df91c3STroy Kisky 				best_j = j;
28226df91c3STroy Kisky 				best_div = 0;
28326df91c3STroy Kisky 			}
28426df91c3STroy Kisky 			score = get_score((adc >> 1), adc_l, adc_h, need_adc,
28526df91c3STroy Kisky 					(dac >> 1), dac_l, dac_h, need_dac);
28626df91c3STroy Kisky 			/* prefer to have a /2 */
28726df91c3STroy Kisky 			if ((score != UINT_MAX) && (best_score >= score)) {
28826df91c3STroy Kisky 				best_score = score;
28926df91c3STroy Kisky 				best_i = i;
29026df91c3STroy Kisky 				best_j = j;
29126df91c3STroy Kisky 				best_div = 1;
29226df91c3STroy Kisky 			}
29326df91c3STroy Kisky 		}
29426df91c3STroy Kisky 	}
29526df91c3STroy Kisky 	return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
29626df91c3STroy Kisky }
29726df91c3STroy Kisky 
2988d702f23SMark Brown #ifdef DEBUG
299ff06ac2aSKuninori Morimoto static void get_current_sample_rates(struct snd_soc_component *component, int mclk,
30026df91c3STroy Kisky 		u32 *sample_rate_adc, u32 *sample_rate_dac)
30126df91c3STroy Kisky {
302ff06ac2aSKuninori Morimoto 	int src = snd_soc_component_read32(component, TLV320AIC23_SRATE);
30326df91c3STroy Kisky 	int sr = (src >> 2) & 0x0f;
30426df91c3STroy Kisky 	int val = (mclk / bosr_usb_divisor_table[src & 3]);
30526df91c3STroy Kisky 	int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
30626df91c3STroy Kisky 	int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
30726df91c3STroy Kisky 	if (src & TLV320AIC23_CLKIN_HALF) {
30826df91c3STroy Kisky 		adc >>= 1;
30926df91c3STroy Kisky 		dac >>= 1;
31026df91c3STroy Kisky 	}
31126df91c3STroy Kisky 	*sample_rate_adc = adc;
31226df91c3STroy Kisky 	*sample_rate_dac = dac;
31326df91c3STroy Kisky }
3148d702f23SMark Brown #endif
31526df91c3STroy Kisky 
316ff06ac2aSKuninori Morimoto static int set_sample_rate_control(struct snd_soc_component *component, int mclk,
31726df91c3STroy Kisky 		u32 sample_rate_adc, u32 sample_rate_dac)
31826df91c3STroy Kisky {
31926df91c3STroy Kisky 	/* Search for the right sample rate */
32026df91c3STroy Kisky 	int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
32126df91c3STroy Kisky 	if (data < 0) {
32226df91c3STroy Kisky 		printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
32326df91c3STroy Kisky 				__func__, sample_rate_adc, sample_rate_dac);
32426df91c3STroy Kisky 		return -EINVAL;
32526df91c3STroy Kisky 	}
326ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_SRATE, data);
3278d702f23SMark Brown #ifdef DEBUG
3288d702f23SMark Brown 	{
3298d702f23SMark Brown 		u32 adc, dac;
330ff06ac2aSKuninori Morimoto 		get_current_sample_rates(component, mclk, &adc, &dac);
33126df91c3STroy Kisky 		printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
33226df91c3STroy Kisky 			adc, dac, data);
33326df91c3STroy Kisky 	}
3348d702f23SMark Brown #endif
33526df91c3STroy Kisky 	return 0;
33626df91c3STroy Kisky }
33726df91c3STroy Kisky 
338c1f27190SArun KS static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
339dee89c4dSMark Brown 				 struct snd_pcm_hw_params *params,
340dee89c4dSMark Brown 				 struct snd_soc_dai *dai)
341c1f27190SArun KS {
342ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
34326df91c3STroy Kisky 	u16 iface_reg;
34426df91c3STroy Kisky 	int ret;
345ff06ac2aSKuninori Morimoto 	struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
34626df91c3STroy Kisky 	u32 sample_rate_adc = aic23->requested_adc;
34726df91c3STroy Kisky 	u32 sample_rate_dac = aic23->requested_dac;
34826df91c3STroy Kisky 	u32 sample_rate = params_rate(params);
34926df91c3STroy Kisky 
35026df91c3STroy Kisky 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
35126df91c3STroy Kisky 		aic23->requested_dac = sample_rate_dac = sample_rate;
35226df91c3STroy Kisky 		if (!sample_rate_adc)
35326df91c3STroy Kisky 			sample_rate_adc = sample_rate;
35426df91c3STroy Kisky 	} else {
35526df91c3STroy Kisky 		aic23->requested_adc = sample_rate_adc = sample_rate;
35626df91c3STroy Kisky 		if (!sample_rate_dac)
35726df91c3STroy Kisky 			sample_rate_dac = sample_rate;
35826df91c3STroy Kisky 	}
359ff06ac2aSKuninori Morimoto 	ret = set_sample_rate_control(component, aic23->mclk, sample_rate_adc,
36026df91c3STroy Kisky 			sample_rate_dac);
36126df91c3STroy Kisky 	if (ret < 0)
36226df91c3STroy Kisky 		return ret;
363c1f27190SArun KS 
364ff06ac2aSKuninori Morimoto 	iface_reg = snd_soc_component_read32(component, TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
365f9dfbf91SAxel Lin 
36608074dc1SMark Brown 	switch (params_width(params)) {
36708074dc1SMark Brown 	case 16:
368c1f27190SArun KS 		break;
36908074dc1SMark Brown 	case 20:
370c1f27190SArun KS 		iface_reg |= (0x01 << 2);
371c1f27190SArun KS 		break;
37208074dc1SMark Brown 	case 24:
373c1f27190SArun KS 		iface_reg |= (0x02 << 2);
374c1f27190SArun KS 		break;
37508074dc1SMark Brown 	case 32:
376c1f27190SArun KS 		iface_reg |= (0x03 << 2);
377c1f27190SArun KS 		break;
378c1f27190SArun KS 	}
379ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_DIGT_FMT, iface_reg);
380c1f27190SArun KS 
381c1f27190SArun KS 	return 0;
382c1f27190SArun KS }
383c1f27190SArun KS 
384dee89c4dSMark Brown static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
385dee89c4dSMark Brown 				   struct snd_soc_dai *dai)
386c1f27190SArun KS {
387ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
388c1f27190SArun KS 
389c1f27190SArun KS 	/* set active */
390ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0001);
391c1f27190SArun KS 
392c1f27190SArun KS 	return 0;
393c1f27190SArun KS }
394c1f27190SArun KS 
395dee89c4dSMark Brown static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
396dee89c4dSMark Brown 				 struct snd_soc_dai *dai)
397c1f27190SArun KS {
398ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
399ff06ac2aSKuninori Morimoto 	struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
400c1f27190SArun KS 
401c1f27190SArun KS 	/* deactivate */
402ff06ac2aSKuninori Morimoto 	if (!snd_soc_component_is_active(component)) {
403c1f27190SArun KS 		udelay(50);
404ff06ac2aSKuninori Morimoto 		snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0);
405c1f27190SArun KS 	}
40626df91c3STroy Kisky 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
40726df91c3STroy Kisky 		aic23->requested_dac = 0;
40826df91c3STroy Kisky 	else
40926df91c3STroy Kisky 		aic23->requested_adc = 0;
410c1f27190SArun KS }
411c1f27190SArun KS 
412c1f27190SArun KS static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute)
413c1f27190SArun KS {
414ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
415c1f27190SArun KS 	u16 reg;
416c1f27190SArun KS 
417ff06ac2aSKuninori Morimoto 	reg = snd_soc_component_read32(component, TLV320AIC23_DIGT);
418c1f27190SArun KS 	if (mute)
419c1f27190SArun KS 		reg |= TLV320AIC23_DACM_MUTE;
420c1f27190SArun KS 
421c1f27190SArun KS 	else
422c1f27190SArun KS 		reg &= ~TLV320AIC23_DACM_MUTE;
423c1f27190SArun KS 
424ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_DIGT, reg);
425c1f27190SArun KS 
426c1f27190SArun KS 	return 0;
427c1f27190SArun KS }
428c1f27190SArun KS 
429c1f27190SArun KS static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
430c1f27190SArun KS 				   unsigned int fmt)
431c1f27190SArun KS {
432ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
433c1f27190SArun KS 	u16 iface_reg;
434c1f27190SArun KS 
435ff06ac2aSKuninori Morimoto 	iface_reg = snd_soc_component_read32(component, TLV320AIC23_DIGT_FMT) & (~0x03);
436c1f27190SArun KS 
437c1f27190SArun KS 	/* set master/slave audio interface */
438c1f27190SArun KS 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
439c1f27190SArun KS 	case SND_SOC_DAIFMT_CBM_CFM:
440c1f27190SArun KS 		iface_reg |= TLV320AIC23_MS_MASTER;
441c1f27190SArun KS 		break;
442c1f27190SArun KS 	case SND_SOC_DAIFMT_CBS_CFS:
443b01a3d69SAxel Lin 		iface_reg &= ~TLV320AIC23_MS_MASTER;
444c1f27190SArun KS 		break;
445c1f27190SArun KS 	default:
446c1f27190SArun KS 		return -EINVAL;
447c1f27190SArun KS 
448c1f27190SArun KS 	}
449c1f27190SArun KS 
450c1f27190SArun KS 	/* interface format */
451c1f27190SArun KS 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
452c1f27190SArun KS 	case SND_SOC_DAIFMT_I2S:
453c1f27190SArun KS 		iface_reg |= TLV320AIC23_FOR_I2S;
454c1f27190SArun KS 		break;
455894bf92fSPeter Ujfalusi 	case SND_SOC_DAIFMT_DSP_A:
456894bf92fSPeter Ujfalusi 		iface_reg |= TLV320AIC23_LRP_ON;
457a47043ecSGustavo A. R. Silva 		/* fall through */
458bd25867aSJarkko Nikula 	case SND_SOC_DAIFMT_DSP_B:
459c1f27190SArun KS 		iface_reg |= TLV320AIC23_FOR_DSP;
460c1f27190SArun KS 		break;
461c1f27190SArun KS 	case SND_SOC_DAIFMT_RIGHT_J:
462c1f27190SArun KS 		break;
463c1f27190SArun KS 	case SND_SOC_DAIFMT_LEFT_J:
464c1f27190SArun KS 		iface_reg |= TLV320AIC23_FOR_LJUST;
465c1f27190SArun KS 		break;
466c1f27190SArun KS 	default:
467c1f27190SArun KS 		return -EINVAL;
468c1f27190SArun KS 
469c1f27190SArun KS 	}
470c1f27190SArun KS 
471ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_DIGT_FMT, iface_reg);
472c1f27190SArun KS 
473c1f27190SArun KS 	return 0;
474c1f27190SArun KS }
475c1f27190SArun KS 
476c1f27190SArun KS static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
477c1f27190SArun KS 				      int clk_id, unsigned int freq, int dir)
478c1f27190SArun KS {
479f0fba2adSLiam Girdwood 	struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
48026df91c3STroy Kisky 	aic23->mclk = freq;
481c1f27190SArun KS 	return 0;
482c1f27190SArun KS }
483c1f27190SArun KS 
484ff06ac2aSKuninori Morimoto static int tlv320aic23_set_bias_level(struct snd_soc_component *component,
485c1f27190SArun KS 				      enum snd_soc_bias_level level)
486c1f27190SArun KS {
487ff06ac2aSKuninori Morimoto 	u16 reg = snd_soc_component_read32(component, TLV320AIC23_PWR) & 0x17f;
488c1f27190SArun KS 
489c1f27190SArun KS 	switch (level) {
490c1f27190SArun KS 	case SND_SOC_BIAS_ON:
491c1f27190SArun KS 		/* vref/mid, osc on, dac unmute */
4923d5a4516SEric Bénard 		reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
4933d5a4516SEric Bénard 			TLV320AIC23_DAC_OFF);
494ff06ac2aSKuninori Morimoto 		snd_soc_component_write(component, TLV320AIC23_PWR, reg);
495c1f27190SArun KS 		break;
496c1f27190SArun KS 	case SND_SOC_BIAS_PREPARE:
497c1f27190SArun KS 		break;
498c1f27190SArun KS 	case SND_SOC_BIAS_STANDBY:
499c1f27190SArun KS 		/* everything off except vref/vmid, */
500ff06ac2aSKuninori Morimoto 		snd_soc_component_write(component, TLV320AIC23_PWR,
501f9dfbf91SAxel Lin 			      reg | TLV320AIC23_CLK_OFF);
502c1f27190SArun KS 		break;
503c1f27190SArun KS 	case SND_SOC_BIAS_OFF:
504c1f27190SArun KS 		/* everything off, dac mute, inactive */
505ff06ac2aSKuninori Morimoto 		snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0);
506ff06ac2aSKuninori Morimoto 		snd_soc_component_write(component, TLV320AIC23_PWR, 0x1ff);
507c1f27190SArun KS 		break;
508c1f27190SArun KS 	}
509c1f27190SArun KS 	return 0;
510c1f27190SArun KS }
511c1f27190SArun KS 
512c1f27190SArun KS #define AIC23_RATES	SNDRV_PCM_RATE_8000_96000
513c1f27190SArun KS #define AIC23_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
514c1f27190SArun KS 			 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
515c1f27190SArun KS 
51685e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops tlv320aic23_dai_ops = {
5176335d055SEric Miao 	.prepare	= tlv320aic23_pcm_prepare,
5186335d055SEric Miao 	.hw_params	= tlv320aic23_hw_params,
5196335d055SEric Miao 	.shutdown	= tlv320aic23_shutdown,
5206335d055SEric Miao 	.digital_mute	= tlv320aic23_mute,
5216335d055SEric Miao 	.set_fmt	= tlv320aic23_set_dai_fmt,
5226335d055SEric Miao 	.set_sysclk	= tlv320aic23_set_dai_sysclk,
5236335d055SEric Miao };
5246335d055SEric Miao 
525f0fba2adSLiam Girdwood static struct snd_soc_dai_driver tlv320aic23_dai = {
526f0fba2adSLiam Girdwood 	.name = "tlv320aic23-hifi",
527c1f27190SArun KS 	.playback = {
528c1f27190SArun KS 		     .stream_name = "Playback",
529c1f27190SArun KS 		     .channels_min = 2,
530c1f27190SArun KS 		     .channels_max = 2,
531c1f27190SArun KS 		     .rates = AIC23_RATES,
532c1f27190SArun KS 		     .formats = AIC23_FORMATS,},
533c1f27190SArun KS 	.capture = {
534c1f27190SArun KS 		    .stream_name = "Capture",
535c1f27190SArun KS 		    .channels_min = 2,
536c1f27190SArun KS 		    .channels_max = 2,
537c1f27190SArun KS 		    .rates = AIC23_RATES,
538c1f27190SArun KS 		    .formats = AIC23_FORMATS,},
5396335d055SEric Miao 	.ops = &tlv320aic23_dai_ops,
540c1f27190SArun KS };
541c1f27190SArun KS 
542ff06ac2aSKuninori Morimoto static int tlv320aic23_resume(struct snd_soc_component *component)
543c1f27190SArun KS {
544ff06ac2aSKuninori Morimoto 	struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
5454aa11d67SMark Brown 	regcache_mark_dirty(aic23->regmap);
5464aa11d67SMark Brown 	regcache_sync(aic23->regmap);
547c1f27190SArun KS 
548c1f27190SArun KS 	return 0;
549c1f27190SArun KS }
550c1f27190SArun KS 
551ff06ac2aSKuninori Morimoto static int tlv320aic23_component_probe(struct snd_soc_component *component)
552c1f27190SArun KS {
553c1f27190SArun KS 	/* Reset codec */
554ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_RESET, 0);
555f9dfbf91SAxel Lin 
556ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
557c1f27190SArun KS 
558c1f27190SArun KS 	/* Unmute input */
559ff06ac2aSKuninori Morimoto 	snd_soc_component_update_bits(component, TLV320AIC23_LINVOL,
560f9dfbf91SAxel Lin 			    TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
561c1f27190SArun KS 
562ff06ac2aSKuninori Morimoto 	snd_soc_component_update_bits(component, TLV320AIC23_RINVOL,
563f9dfbf91SAxel Lin 			    TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
564c1f27190SArun KS 
565ff06ac2aSKuninori Morimoto 	snd_soc_component_update_bits(component, TLV320AIC23_ANLG,
566f9dfbf91SAxel Lin 			    TLV320AIC23_BYPASS_ON | TLV320AIC23_MICM_MUTED,
567f9dfbf91SAxel Lin 			    0);
568c1f27190SArun KS 
569c1f27190SArun KS 	/* Default output volume */
570ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_LCHNVOL,
571f9dfbf91SAxel Lin 		      TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
572ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_RCHNVOL,
573f9dfbf91SAxel Lin 		      TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
574c1f27190SArun KS 
575ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x1);
576c1f27190SArun KS 
577f0fba2adSLiam Girdwood 	return 0;
578c1f27190SArun KS }
579f0fba2adSLiam Girdwood 
580ff06ac2aSKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_tlv320aic23 = {
581ff06ac2aSKuninori Morimoto 	.probe			= tlv320aic23_component_probe,
582f0fba2adSLiam Girdwood 	.resume			= tlv320aic23_resume,
583f0fba2adSLiam Girdwood 	.set_bias_level		= tlv320aic23_set_bias_level,
584b07c443fSMark Brown 	.controls		= tlv320aic23_snd_controls,
585b07c443fSMark Brown 	.num_controls		= ARRAY_SIZE(tlv320aic23_snd_controls),
586a7dca707SLu Guanqun 	.dapm_widgets		= tlv320aic23_dapm_widgets,
587a7dca707SLu Guanqun 	.num_dapm_widgets	= ARRAY_SIZE(tlv320aic23_dapm_widgets),
588a7dca707SLu Guanqun 	.dapm_routes		= tlv320aic23_intercon,
589a7dca707SLu Guanqun 	.num_dapm_routes	= ARRAY_SIZE(tlv320aic23_intercon),
590ff06ac2aSKuninori Morimoto 	.suspend_bias_off	= 1,
591ff06ac2aSKuninori Morimoto 	.idle_bias_on		= 1,
592ff06ac2aSKuninori Morimoto 	.use_pmdown_time	= 1,
593ff06ac2aSKuninori Morimoto 	.endianness		= 1,
594ff06ac2aSKuninori Morimoto 	.non_legacy_dai_naming	= 1,
595f0fba2adSLiam Girdwood };
596c1f27190SArun KS 
597b3fc5725SMax Filippov int tlv320aic23_probe(struct device *dev, struct regmap *regmap)
598c1f27190SArun KS {
599f0fba2adSLiam Girdwood 	struct aic23 *aic23;
600c1f27190SArun KS 
601b3fc5725SMax Filippov 	if (IS_ERR(regmap))
602b3fc5725SMax Filippov 		return PTR_ERR(regmap);
603c1f27190SArun KS 
604b3fc5725SMax Filippov 	aic23 = devm_kzalloc(dev, sizeof(struct aic23), GFP_KERNEL);
605f0fba2adSLiam Girdwood 	if (aic23 == NULL)
606f0fba2adSLiam Girdwood 		return -ENOMEM;
607c1f27190SArun KS 
608b3fc5725SMax Filippov 	aic23->regmap = regmap;
6094aa11d67SMark Brown 
610b3fc5725SMax Filippov 	dev_set_drvdata(dev, aic23);
611c1f27190SArun KS 
612ff06ac2aSKuninori Morimoto 	return devm_snd_soc_register_component(dev,
613ff06ac2aSKuninori Morimoto 				      &soc_component_dev_tlv320aic23,
614b3fc5725SMax Filippov 				      &tlv320aic23_dai, 1);
615c1f27190SArun KS }
61640423285SMax Filippov EXPORT_SYMBOL(tlv320aic23_probe);
61764089b84SMark Brown 
618c1f27190SArun KS MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
619c1f27190SArun KS MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
620c1f27190SArun KS MODULE_LICENSE("GPL");
621