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