xref: /openbmc/linux/sound/soc/codecs/tlv320aic23.c (revision 960af79d)
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_deemph,
71806057ccSTakashi Iwai 			    TLV320AIC23_DIGT, 1, deemph_text);
72c1f27190SArun KS 
73c1f27190SArun KS static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -12100, 100, 0);
74c1f27190SArun KS static const DECLARE_TLV_DB_SCALE(input_gain_tlv, -1725, 75, 0);
75df91ddf1SArun KS static const DECLARE_TLV_DB_SCALE(sidetone_vol_tlv, -1800, 300, 0);
76df91ddf1SArun KS 
77df91ddf1SArun KS static int snd_soc_tlv320aic23_put_volsw(struct snd_kcontrol *kcontrol,
78df91ddf1SArun KS 	struct snd_ctl_elem_value *ucontrol)
79df91ddf1SArun KS {
80ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
81df91ddf1SArun KS 	u16 val, reg;
82df91ddf1SArun KS 
83df91ddf1SArun KS 	val = (ucontrol->value.integer.value[0] & 0x07);
84df91ddf1SArun KS 
85df91ddf1SArun KS 	/* linear conversion to userspace
86df91ddf1SArun KS 	* 000	=	-6db
87df91ddf1SArun KS 	* 001	=	-9db
88df91ddf1SArun KS 	* 010	=	-12db
89df91ddf1SArun KS 	* 011	=	-18db (Min)
90df91ddf1SArun KS 	* 100	=	0db (Max)
91df91ddf1SArun KS 	*/
92df91ddf1SArun KS 	val = (val >= 4) ? 4  : (3 - val);
93df91ddf1SArun KS 
94e348cf54SKuninori Morimoto 	reg = snd_soc_component_read(component, TLV320AIC23_ANLG) & (~0x1C0);
95ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_ANLG, reg | (val << 6));
96df91ddf1SArun KS 
97df91ddf1SArun KS 	return 0;
98df91ddf1SArun KS }
99df91ddf1SArun KS 
100df91ddf1SArun KS static int snd_soc_tlv320aic23_get_volsw(struct snd_kcontrol *kcontrol,
101df91ddf1SArun KS 	struct snd_ctl_elem_value *ucontrol)
102df91ddf1SArun KS {
103ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
104df91ddf1SArun KS 	u16 val;
105df91ddf1SArun KS 
106e348cf54SKuninori Morimoto 	val = snd_soc_component_read(component, TLV320AIC23_ANLG) & (0x1C0);
107df91ddf1SArun KS 	val = val >> 6;
108df91ddf1SArun KS 	val = (val >= 4) ? 4  : (3 -  val);
109df91ddf1SArun KS 	ucontrol->value.integer.value[0] = val;
110df91ddf1SArun KS 	return 0;
111df91ddf1SArun KS 
112df91ddf1SArun KS }
113df91ddf1SArun KS 
114c1f27190SArun KS static const struct snd_kcontrol_new tlv320aic23_snd_controls[] = {
115c1f27190SArun KS 	SOC_DOUBLE_R_TLV("Digital Playback Volume", TLV320AIC23_LCHNVOL,
116c1f27190SArun KS 			 TLV320AIC23_RCHNVOL, 0, 127, 0, out_gain_tlv),
117c1f27190SArun KS 	SOC_SINGLE("Digital Playback Switch", TLV320AIC23_DIGT, 3, 1, 1),
118c1f27190SArun KS 	SOC_DOUBLE_R("Line Input Switch", TLV320AIC23_LINVOL,
119c1f27190SArun KS 		     TLV320AIC23_RINVOL, 7, 1, 0),
120c1f27190SArun KS 	SOC_DOUBLE_R_TLV("Line Input Volume", TLV320AIC23_LINVOL,
121c1f27190SArun KS 			 TLV320AIC23_RINVOL, 0, 31, 0, input_gain_tlv),
122c1f27190SArun KS 	SOC_SINGLE("Mic Input Switch", TLV320AIC23_ANLG, 1, 1, 1),
123c1f27190SArun KS 	SOC_SINGLE("Mic Booster Switch", TLV320AIC23_ANLG, 0, 1, 0),
1240f9887d1SPeter Ujfalusi 	SOC_SINGLE_EXT_TLV("Sidetone Volume", TLV320AIC23_ANLG, 6, 4, 0,
1250f9887d1SPeter Ujfalusi 			   snd_soc_tlv320aic23_get_volsw,
1260f9887d1SPeter Ujfalusi 			   snd_soc_tlv320aic23_put_volsw, sidetone_vol_tlv),
127c1f27190SArun KS 	SOC_ENUM("Playback De-emphasis", tlv320aic23_deemph),
128c1f27190SArun KS };
129c1f27190SArun KS 
130c1f27190SArun KS /* PGA Mixer controls for Line and Mic switch */
131c1f27190SArun KS static const struct snd_kcontrol_new tlv320aic23_output_mixer_controls[] = {
132c1f27190SArun KS 	SOC_DAPM_SINGLE("Line Bypass Switch", TLV320AIC23_ANLG, 3, 1, 0),
133c1f27190SArun KS 	SOC_DAPM_SINGLE("Mic Sidetone Switch", TLV320AIC23_ANLG, 5, 1, 0),
134c1f27190SArun KS 	SOC_DAPM_SINGLE("Playback Switch", TLV320AIC23_ANLG, 4, 1, 0),
135c1f27190SArun KS };
136c1f27190SArun KS 
137c1f27190SArun KS static const struct snd_soc_dapm_widget tlv320aic23_dapm_widgets[] = {
138c1f27190SArun KS 	SND_SOC_DAPM_DAC("DAC", "Playback", TLV320AIC23_PWR, 3, 1),
139c1f27190SArun KS 	SND_SOC_DAPM_ADC("ADC", "Capture", TLV320AIC23_PWR, 2, 1),
140c1f27190SArun KS 	SND_SOC_DAPM_MUX("Capture Source", SND_SOC_NOPM, 0, 0,
141c1f27190SArun KS 			 &tlv320aic23_rec_src_mux_controls),
142c1f27190SArun KS 	SND_SOC_DAPM_MIXER("Output Mixer", TLV320AIC23_PWR, 4, 1,
143c1f27190SArun KS 			   &tlv320aic23_output_mixer_controls[0],
144c1f27190SArun KS 			   ARRAY_SIZE(tlv320aic23_output_mixer_controls)),
145c1f27190SArun KS 	SND_SOC_DAPM_PGA("Line Input", TLV320AIC23_PWR, 0, 1, NULL, 0),
146c1f27190SArun KS 	SND_SOC_DAPM_PGA("Mic Input", TLV320AIC23_PWR, 1, 1, NULL, 0),
147c1f27190SArun KS 
148c1f27190SArun KS 	SND_SOC_DAPM_OUTPUT("LHPOUT"),
149c1f27190SArun KS 	SND_SOC_DAPM_OUTPUT("RHPOUT"),
150c1f27190SArun KS 	SND_SOC_DAPM_OUTPUT("LOUT"),
151c1f27190SArun KS 	SND_SOC_DAPM_OUTPUT("ROUT"),
152c1f27190SArun KS 
153c1f27190SArun KS 	SND_SOC_DAPM_INPUT("LLINEIN"),
154c1f27190SArun KS 	SND_SOC_DAPM_INPUT("RLINEIN"),
155c1f27190SArun KS 
156c1f27190SArun KS 	SND_SOC_DAPM_INPUT("MICIN"),
157c1f27190SArun KS };
158c1f27190SArun KS 
159a7dca707SLu Guanqun static const struct snd_soc_dapm_route tlv320aic23_intercon[] = {
160c1f27190SArun KS 	/* Output Mixer */
161c1f27190SArun KS 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
162c1f27190SArun KS 	{"Output Mixer", "Playback Switch", "DAC"},
163c1f27190SArun KS 	{"Output Mixer", "Mic Sidetone Switch", "Mic Input"},
164c1f27190SArun KS 
165c1f27190SArun KS 	/* Outputs */
166c1f27190SArun KS 	{"RHPOUT", NULL, "Output Mixer"},
167c1f27190SArun KS 	{"LHPOUT", NULL, "Output Mixer"},
168c1f27190SArun KS 	{"LOUT", NULL, "Output Mixer"},
169c1f27190SArun KS 	{"ROUT", NULL, "Output Mixer"},
170c1f27190SArun KS 
171c1f27190SArun KS 	/* Inputs */
172a03faba9SLiviu Dudau 	{"Line Input", NULL, "LLINEIN"},
173a03faba9SLiviu Dudau 	{"Line Input", NULL, "RLINEIN"},
174a03faba9SLiviu Dudau 	{"Mic Input", NULL, "MICIN"},
175c1f27190SArun KS 
176c1f27190SArun KS 	/* input mux */
177c1f27190SArun KS 	{"Capture Source", "Line", "Line Input"},
178c1f27190SArun KS 	{"Capture Source", "Mic", "Mic Input"},
179c1f27190SArun KS 	{"ADC", NULL, "Capture Source"},
180c1f27190SArun KS 
181c1f27190SArun KS };
182c1f27190SArun KS 
18326df91c3STroy Kisky /* AIC23 driver data */
18426df91c3STroy Kisky struct aic23 {
1854aa11d67SMark Brown 	struct regmap *regmap;
18626df91c3STroy Kisky 	int mclk;
18726df91c3STroy Kisky 	int requested_adc;
18826df91c3STroy Kisky 	int requested_dac;
189c1f27190SArun KS };
190c1f27190SArun KS 
19126df91c3STroy Kisky /*
19226df91c3STroy Kisky  * Common Crystals used
19326df91c3STroy Kisky  * 11.2896 Mhz /128 = *88.2k  /192 = 58.8k
19426df91c3STroy Kisky  * 12.0000 Mhz /125 = *96k    /136 = 88.235K
19526df91c3STroy Kisky  * 12.2880 Mhz /128 = *96k    /192 = 64k
19626df91c3STroy Kisky  * 16.9344 Mhz /128 = 132.3k /192 = *88.2k
19726df91c3STroy Kisky  * 18.4320 Mhz /128 = 144k   /192 = *96k
19826df91c3STroy Kisky  */
19926df91c3STroy Kisky 
20026df91c3STroy Kisky /*
20126df91c3STroy Kisky  * Normal BOSR 0-256/2 = 128, 1-384/2 = 192
20226df91c3STroy Kisky  * USB BOSR 0-250/2 = 125, 1-272/2 = 136
20326df91c3STroy Kisky  */
20426df91c3STroy Kisky static const int bosr_usb_divisor_table[] = {
20526df91c3STroy Kisky 	128, 125, 192, 136
20626df91c3STroy Kisky };
20726df91c3STroy Kisky #define LOWER_GROUP ((1<<0) | (1<<1) | (1<<2) | (1<<3) | (1<<6) | (1<<7))
20826df91c3STroy Kisky #define UPPER_GROUP ((1<<8) | (1<<9) | (1<<10) | (1<<11)        | (1<<15))
20926df91c3STroy Kisky static const unsigned short sr_valid_mask[] = {
21026df91c3STroy Kisky 	LOWER_GROUP|UPPER_GROUP,	/* Normal, bosr - 0*/
21126df91c3STroy Kisky 	LOWER_GROUP,			/* Usb, bosr - 0*/
212bab02124STroy Kisky 	LOWER_GROUP|UPPER_GROUP,	/* Normal, bosr - 1*/
21326df91c3STroy Kisky 	UPPER_GROUP,			/* Usb, bosr - 1*/
21426df91c3STroy Kisky };
21526df91c3STroy Kisky /*
21626df91c3STroy Kisky  * Every divisor is a factor of 11*12
21726df91c3STroy Kisky  */
21826df91c3STroy Kisky #define SR_MULT (11*12)
219ccff4b15STroy Kisky #define A(x) (SR_MULT/x)
22026df91c3STroy Kisky static const unsigned char sr_adc_mult_table[] = {
221ccff4b15STroy Kisky 	A(2), A(2), A(12), A(12),  0, 0, A(3), A(1),
222ccff4b15STroy Kisky 	A(2), A(2), A(11), A(11),  0, 0, 0, A(1)
22326df91c3STroy Kisky };
22426df91c3STroy Kisky static const unsigned char sr_dac_mult_table[] = {
225ccff4b15STroy Kisky 	A(2), A(12), A(2), A(12),  0, 0, A(3), A(1),
226ccff4b15STroy Kisky 	A(2), A(11), A(2), A(11),  0, 0, 0, A(1)
22726df91c3STroy Kisky };
22826df91c3STroy Kisky 
22926df91c3STroy Kisky static unsigned get_score(int adc, int adc_l, int adc_h, int need_adc,
23026df91c3STroy Kisky 		int dac, int dac_l, int dac_h, int need_dac)
23126df91c3STroy Kisky {
23226df91c3STroy Kisky 	if ((adc >= adc_l) && (adc <= adc_h) &&
23326df91c3STroy Kisky 			(dac >= dac_l) && (dac <= dac_h)) {
23426df91c3STroy Kisky 		int diff_adc = need_adc - adc;
23526df91c3STroy Kisky 		int diff_dac = need_dac - dac;
23626df91c3STroy Kisky 		return abs(diff_adc) + abs(diff_dac);
23726df91c3STroy Kisky 	}
23826df91c3STroy Kisky 	return UINT_MAX;
23926df91c3STroy Kisky }
24026df91c3STroy Kisky 
24126df91c3STroy Kisky static int find_rate(int mclk, u32 need_adc, u32 need_dac)
24226df91c3STroy Kisky {
24326df91c3STroy Kisky 	int i, j;
24426df91c3STroy Kisky 	int best_i = -1;
24526df91c3STroy Kisky 	int best_j = -1;
24626df91c3STroy Kisky 	int best_div = 0;
24726df91c3STroy Kisky 	unsigned best_score = UINT_MAX;
24826df91c3STroy Kisky 	int adc_l, adc_h, dac_l, dac_h;
24926df91c3STroy Kisky 
25026df91c3STroy Kisky 	need_adc *= SR_MULT;
25126df91c3STroy Kisky 	need_dac *= SR_MULT;
25226df91c3STroy Kisky 	/*
25326df91c3STroy Kisky 	 * rates given are +/- 1/32
25426df91c3STroy Kisky 	 */
25526df91c3STroy Kisky 	adc_l = need_adc - (need_adc >> 5);
25626df91c3STroy Kisky 	adc_h = need_adc + (need_adc >> 5);
25726df91c3STroy Kisky 	dac_l = need_dac - (need_dac >> 5);
25826df91c3STroy Kisky 	dac_h = need_dac + (need_dac >> 5);
2598d702f23SMark Brown 	for (i = 0; i < ARRAY_SIZE(bosr_usb_divisor_table); i++) {
26026df91c3STroy Kisky 		int base = mclk / bosr_usb_divisor_table[i];
26126df91c3STroy Kisky 		int mask = sr_valid_mask[i];
2628d702f23SMark Brown 		for (j = 0; j < ARRAY_SIZE(sr_adc_mult_table);
2638d702f23SMark Brown 				j++, mask >>= 1) {
26426df91c3STroy Kisky 			int adc;
26526df91c3STroy Kisky 			int dac;
26626df91c3STroy Kisky 			int score;
26726df91c3STroy Kisky 			if ((mask & 1) == 0)
26826df91c3STroy Kisky 				continue;
26926df91c3STroy Kisky 			adc = base * sr_adc_mult_table[j];
27026df91c3STroy Kisky 			dac = base * sr_dac_mult_table[j];
27126df91c3STroy Kisky 			score = get_score(adc, adc_l, adc_h, need_adc,
27226df91c3STroy Kisky 					dac, dac_l, dac_h, need_dac);
27326df91c3STroy Kisky 			if (best_score > score) {
27426df91c3STroy Kisky 				best_score = score;
27526df91c3STroy Kisky 				best_i = i;
27626df91c3STroy Kisky 				best_j = j;
27726df91c3STroy Kisky 				best_div = 0;
27826df91c3STroy Kisky 			}
27926df91c3STroy Kisky 			score = get_score((adc >> 1), adc_l, adc_h, need_adc,
28026df91c3STroy Kisky 					(dac >> 1), dac_l, dac_h, need_dac);
28126df91c3STroy Kisky 			/* prefer to have a /2 */
28226df91c3STroy Kisky 			if ((score != UINT_MAX) && (best_score >= score)) {
28326df91c3STroy Kisky 				best_score = score;
28426df91c3STroy Kisky 				best_i = i;
28526df91c3STroy Kisky 				best_j = j;
28626df91c3STroy Kisky 				best_div = 1;
28726df91c3STroy Kisky 			}
28826df91c3STroy Kisky 		}
28926df91c3STroy Kisky 	}
29026df91c3STroy Kisky 	return (best_j << 2) | best_i | (best_div << TLV320AIC23_CLKIN_SHIFT);
29126df91c3STroy Kisky }
29226df91c3STroy Kisky 
2938d702f23SMark Brown #ifdef DEBUG
294ff06ac2aSKuninori Morimoto static void get_current_sample_rates(struct snd_soc_component *component, int mclk,
29526df91c3STroy Kisky 		u32 *sample_rate_adc, u32 *sample_rate_dac)
29626df91c3STroy Kisky {
297e348cf54SKuninori Morimoto 	int src = snd_soc_component_read(component, TLV320AIC23_SRATE);
29826df91c3STroy Kisky 	int sr = (src >> 2) & 0x0f;
29926df91c3STroy Kisky 	int val = (mclk / bosr_usb_divisor_table[src & 3]);
30026df91c3STroy Kisky 	int adc = (val * sr_adc_mult_table[sr]) / SR_MULT;
30126df91c3STroy Kisky 	int dac = (val * sr_dac_mult_table[sr]) / SR_MULT;
30226df91c3STroy Kisky 	if (src & TLV320AIC23_CLKIN_HALF) {
30326df91c3STroy Kisky 		adc >>= 1;
30426df91c3STroy Kisky 		dac >>= 1;
30526df91c3STroy Kisky 	}
30626df91c3STroy Kisky 	*sample_rate_adc = adc;
30726df91c3STroy Kisky 	*sample_rate_dac = dac;
30826df91c3STroy Kisky }
3098d702f23SMark Brown #endif
31026df91c3STroy Kisky 
311ff06ac2aSKuninori Morimoto static int set_sample_rate_control(struct snd_soc_component *component, int mclk,
31226df91c3STroy Kisky 		u32 sample_rate_adc, u32 sample_rate_dac)
31326df91c3STroy Kisky {
31426df91c3STroy Kisky 	/* Search for the right sample rate */
31526df91c3STroy Kisky 	int data = find_rate(mclk, sample_rate_adc, sample_rate_dac);
31626df91c3STroy Kisky 	if (data < 0) {
31726df91c3STroy Kisky 		printk(KERN_ERR "%s:Invalid rate %u,%u requested\n",
31826df91c3STroy Kisky 				__func__, sample_rate_adc, sample_rate_dac);
31926df91c3STroy Kisky 		return -EINVAL;
32026df91c3STroy Kisky 	}
321ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_SRATE, data);
3228d702f23SMark Brown #ifdef DEBUG
3238d702f23SMark Brown 	{
3248d702f23SMark Brown 		u32 adc, dac;
325ff06ac2aSKuninori Morimoto 		get_current_sample_rates(component, mclk, &adc, &dac);
32626df91c3STroy Kisky 		printk(KERN_DEBUG "actual samplerate = %u,%u reg=%x\n",
32726df91c3STroy Kisky 			adc, dac, data);
32826df91c3STroy Kisky 	}
3298d702f23SMark Brown #endif
33026df91c3STroy Kisky 	return 0;
33126df91c3STroy Kisky }
33226df91c3STroy Kisky 
333c1f27190SArun KS static int tlv320aic23_hw_params(struct snd_pcm_substream *substream,
334dee89c4dSMark Brown 				 struct snd_pcm_hw_params *params,
335dee89c4dSMark Brown 				 struct snd_soc_dai *dai)
336c1f27190SArun KS {
337ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
33826df91c3STroy Kisky 	u16 iface_reg;
33926df91c3STroy Kisky 	int ret;
340ff06ac2aSKuninori Morimoto 	struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
34126df91c3STroy Kisky 	u32 sample_rate_adc = aic23->requested_adc;
34226df91c3STroy Kisky 	u32 sample_rate_dac = aic23->requested_dac;
34326df91c3STroy Kisky 	u32 sample_rate = params_rate(params);
34426df91c3STroy Kisky 
34526df91c3STroy Kisky 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
34626df91c3STroy Kisky 		aic23->requested_dac = sample_rate_dac = sample_rate;
34726df91c3STroy Kisky 		if (!sample_rate_adc)
34826df91c3STroy Kisky 			sample_rate_adc = sample_rate;
34926df91c3STroy Kisky 	} else {
35026df91c3STroy Kisky 		aic23->requested_adc = sample_rate_adc = sample_rate;
35126df91c3STroy Kisky 		if (!sample_rate_dac)
35226df91c3STroy Kisky 			sample_rate_dac = sample_rate;
35326df91c3STroy Kisky 	}
354ff06ac2aSKuninori Morimoto 	ret = set_sample_rate_control(component, aic23->mclk, sample_rate_adc,
35526df91c3STroy Kisky 			sample_rate_dac);
35626df91c3STroy Kisky 	if (ret < 0)
35726df91c3STroy Kisky 		return ret;
358c1f27190SArun KS 
359e348cf54SKuninori Morimoto 	iface_reg = snd_soc_component_read(component, TLV320AIC23_DIGT_FMT) & ~(0x03 << 2);
360f9dfbf91SAxel Lin 
36108074dc1SMark Brown 	switch (params_width(params)) {
36208074dc1SMark Brown 	case 16:
363c1f27190SArun KS 		break;
36408074dc1SMark Brown 	case 20:
365c1f27190SArun KS 		iface_reg |= (0x01 << 2);
366c1f27190SArun KS 		break;
36708074dc1SMark Brown 	case 24:
368c1f27190SArun KS 		iface_reg |= (0x02 << 2);
369c1f27190SArun KS 		break;
37008074dc1SMark Brown 	case 32:
371c1f27190SArun KS 		iface_reg |= (0x03 << 2);
372c1f27190SArun KS 		break;
373c1f27190SArun KS 	}
374ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_DIGT_FMT, iface_reg);
375c1f27190SArun KS 
376c1f27190SArun KS 	return 0;
377c1f27190SArun KS }
378c1f27190SArun KS 
379dee89c4dSMark Brown static int tlv320aic23_pcm_prepare(struct snd_pcm_substream *substream,
380dee89c4dSMark Brown 				   struct snd_soc_dai *dai)
381c1f27190SArun KS {
382ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
383c1f27190SArun KS 
384c1f27190SArun KS 	/* set active */
385ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0001);
386c1f27190SArun KS 
387c1f27190SArun KS 	return 0;
388c1f27190SArun KS }
389c1f27190SArun KS 
390dee89c4dSMark Brown static void tlv320aic23_shutdown(struct snd_pcm_substream *substream,
391dee89c4dSMark Brown 				 struct snd_soc_dai *dai)
392c1f27190SArun KS {
393ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
394ff06ac2aSKuninori Morimoto 	struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
395c1f27190SArun KS 
396c1f27190SArun KS 	/* deactivate */
3975e518eddSKuninori Morimoto 	if (!snd_soc_component_active(component)) {
398c1f27190SArun KS 		udelay(50);
399ff06ac2aSKuninori Morimoto 		snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0);
400c1f27190SArun KS 	}
40126df91c3STroy Kisky 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
40226df91c3STroy Kisky 		aic23->requested_dac = 0;
40326df91c3STroy Kisky 	else
40426df91c3STroy Kisky 		aic23->requested_adc = 0;
405c1f27190SArun KS }
406c1f27190SArun KS 
407960af79dSKuninori Morimoto static int tlv320aic23_mute(struct snd_soc_dai *dai, int mute, int direction)
408c1f27190SArun KS {
409ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = dai->component;
410c1f27190SArun KS 	u16 reg;
411c1f27190SArun KS 
412e348cf54SKuninori Morimoto 	reg = snd_soc_component_read(component, TLV320AIC23_DIGT);
413c1f27190SArun KS 	if (mute)
414c1f27190SArun KS 		reg |= TLV320AIC23_DACM_MUTE;
415c1f27190SArun KS 
416c1f27190SArun KS 	else
417c1f27190SArun KS 		reg &= ~TLV320AIC23_DACM_MUTE;
418c1f27190SArun KS 
419ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_DIGT, reg);
420c1f27190SArun KS 
421c1f27190SArun KS 	return 0;
422c1f27190SArun KS }
423c1f27190SArun KS 
424c1f27190SArun KS static int tlv320aic23_set_dai_fmt(struct snd_soc_dai *codec_dai,
425c1f27190SArun KS 				   unsigned int fmt)
426c1f27190SArun KS {
427ff06ac2aSKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
428c1f27190SArun KS 	u16 iface_reg;
429c1f27190SArun KS 
430e348cf54SKuninori Morimoto 	iface_reg = snd_soc_component_read(component, TLV320AIC23_DIGT_FMT) & (~0x03);
431c1f27190SArun KS 
432c1f27190SArun KS 	/* set master/slave audio interface */
433c1f27190SArun KS 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
434c1f27190SArun KS 	case SND_SOC_DAIFMT_CBM_CFM:
435c1f27190SArun KS 		iface_reg |= TLV320AIC23_MS_MASTER;
436c1f27190SArun KS 		break;
437c1f27190SArun KS 	case SND_SOC_DAIFMT_CBS_CFS:
438b01a3d69SAxel Lin 		iface_reg &= ~TLV320AIC23_MS_MASTER;
439c1f27190SArun KS 		break;
440c1f27190SArun KS 	default:
441c1f27190SArun KS 		return -EINVAL;
442c1f27190SArun KS 
443c1f27190SArun KS 	}
444c1f27190SArun KS 
445c1f27190SArun KS 	/* interface format */
446c1f27190SArun KS 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
447c1f27190SArun KS 	case SND_SOC_DAIFMT_I2S:
448c1f27190SArun KS 		iface_reg |= TLV320AIC23_FOR_I2S;
449c1f27190SArun KS 		break;
450894bf92fSPeter Ujfalusi 	case SND_SOC_DAIFMT_DSP_A:
451894bf92fSPeter Ujfalusi 		iface_reg |= TLV320AIC23_LRP_ON;
4523e146b55SGustavo A. R. Silva 		fallthrough;
453bd25867aSJarkko Nikula 	case SND_SOC_DAIFMT_DSP_B:
454c1f27190SArun KS 		iface_reg |= TLV320AIC23_FOR_DSP;
455c1f27190SArun KS 		break;
456c1f27190SArun KS 	case SND_SOC_DAIFMT_RIGHT_J:
457c1f27190SArun KS 		break;
458c1f27190SArun KS 	case SND_SOC_DAIFMT_LEFT_J:
459c1f27190SArun KS 		iface_reg |= TLV320AIC23_FOR_LJUST;
460c1f27190SArun KS 		break;
461c1f27190SArun KS 	default:
462c1f27190SArun KS 		return -EINVAL;
463c1f27190SArun KS 
464c1f27190SArun KS 	}
465c1f27190SArun KS 
466ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_DIGT_FMT, iface_reg);
467c1f27190SArun KS 
468c1f27190SArun KS 	return 0;
469c1f27190SArun KS }
470c1f27190SArun KS 
471c1f27190SArun KS static int tlv320aic23_set_dai_sysclk(struct snd_soc_dai *codec_dai,
472c1f27190SArun KS 				      int clk_id, unsigned int freq, int dir)
473c1f27190SArun KS {
474f0fba2adSLiam Girdwood 	struct aic23 *aic23 = snd_soc_dai_get_drvdata(codec_dai);
47526df91c3STroy Kisky 	aic23->mclk = freq;
476c1f27190SArun KS 	return 0;
477c1f27190SArun KS }
478c1f27190SArun KS 
479ff06ac2aSKuninori Morimoto static int tlv320aic23_set_bias_level(struct snd_soc_component *component,
480c1f27190SArun KS 				      enum snd_soc_bias_level level)
481c1f27190SArun KS {
482e348cf54SKuninori Morimoto 	u16 reg = snd_soc_component_read(component, TLV320AIC23_PWR) & 0x17f;
483c1f27190SArun KS 
484c1f27190SArun KS 	switch (level) {
485c1f27190SArun KS 	case SND_SOC_BIAS_ON:
486c1f27190SArun KS 		/* vref/mid, osc on, dac unmute */
4873d5a4516SEric Bénard 		reg &= ~(TLV320AIC23_DEVICE_PWR_OFF | TLV320AIC23_OSC_OFF | \
4883d5a4516SEric Bénard 			TLV320AIC23_DAC_OFF);
489ff06ac2aSKuninori Morimoto 		snd_soc_component_write(component, TLV320AIC23_PWR, reg);
490c1f27190SArun KS 		break;
491c1f27190SArun KS 	case SND_SOC_BIAS_PREPARE:
492c1f27190SArun KS 		break;
493c1f27190SArun KS 	case SND_SOC_BIAS_STANDBY:
494c1f27190SArun KS 		/* everything off except vref/vmid, */
495ff06ac2aSKuninori Morimoto 		snd_soc_component_write(component, TLV320AIC23_PWR,
496f9dfbf91SAxel Lin 			      reg | TLV320AIC23_CLK_OFF);
497c1f27190SArun KS 		break;
498c1f27190SArun KS 	case SND_SOC_BIAS_OFF:
499c1f27190SArun KS 		/* everything off, dac mute, inactive */
500ff06ac2aSKuninori Morimoto 		snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x0);
501ff06ac2aSKuninori Morimoto 		snd_soc_component_write(component, TLV320AIC23_PWR, 0x1ff);
502c1f27190SArun KS 		break;
503c1f27190SArun KS 	}
504c1f27190SArun KS 	return 0;
505c1f27190SArun KS }
506c1f27190SArun KS 
507c1f27190SArun KS #define AIC23_RATES	SNDRV_PCM_RATE_8000_96000
508c1f27190SArun KS #define AIC23_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
509c1f27190SArun KS 			 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
510c1f27190SArun KS 
51185e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops tlv320aic23_dai_ops = {
5126335d055SEric Miao 	.prepare	= tlv320aic23_pcm_prepare,
5136335d055SEric Miao 	.hw_params	= tlv320aic23_hw_params,
5146335d055SEric Miao 	.shutdown	= tlv320aic23_shutdown,
515960af79dSKuninori Morimoto 	.mute_stream	= tlv320aic23_mute,
5166335d055SEric Miao 	.set_fmt	= tlv320aic23_set_dai_fmt,
5176335d055SEric Miao 	.set_sysclk	= tlv320aic23_set_dai_sysclk,
518960af79dSKuninori Morimoto 	.no_capture_mute = 1,
5196335d055SEric Miao };
5206335d055SEric Miao 
521f0fba2adSLiam Girdwood static struct snd_soc_dai_driver tlv320aic23_dai = {
522f0fba2adSLiam Girdwood 	.name = "tlv320aic23-hifi",
523c1f27190SArun KS 	.playback = {
524c1f27190SArun KS 		     .stream_name = "Playback",
525c1f27190SArun KS 		     .channels_min = 2,
526c1f27190SArun KS 		     .channels_max = 2,
527c1f27190SArun KS 		     .rates = AIC23_RATES,
528c1f27190SArun KS 		     .formats = AIC23_FORMATS,},
529c1f27190SArun KS 	.capture = {
530c1f27190SArun KS 		    .stream_name = "Capture",
531c1f27190SArun KS 		    .channels_min = 2,
532c1f27190SArun KS 		    .channels_max = 2,
533c1f27190SArun KS 		    .rates = AIC23_RATES,
534c1f27190SArun KS 		    .formats = AIC23_FORMATS,},
5356335d055SEric Miao 	.ops = &tlv320aic23_dai_ops,
536c1f27190SArun KS };
537c1f27190SArun KS 
538ff06ac2aSKuninori Morimoto static int tlv320aic23_resume(struct snd_soc_component *component)
539c1f27190SArun KS {
540ff06ac2aSKuninori Morimoto 	struct aic23 *aic23 = snd_soc_component_get_drvdata(component);
5414aa11d67SMark Brown 	regcache_mark_dirty(aic23->regmap);
5424aa11d67SMark Brown 	regcache_sync(aic23->regmap);
543c1f27190SArun KS 
544c1f27190SArun KS 	return 0;
545c1f27190SArun KS }
546c1f27190SArun KS 
547ff06ac2aSKuninori Morimoto static int tlv320aic23_component_probe(struct snd_soc_component *component)
548c1f27190SArun KS {
549c1f27190SArun KS 	/* Reset codec */
550ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_RESET, 0);
551f9dfbf91SAxel Lin 
552ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_DIGT, TLV320AIC23_DEEMP_44K);
553c1f27190SArun KS 
554c1f27190SArun KS 	/* Unmute input */
555ff06ac2aSKuninori Morimoto 	snd_soc_component_update_bits(component, TLV320AIC23_LINVOL,
556f9dfbf91SAxel Lin 			    TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
557c1f27190SArun KS 
558ff06ac2aSKuninori Morimoto 	snd_soc_component_update_bits(component, TLV320AIC23_RINVOL,
559f9dfbf91SAxel Lin 			    TLV320AIC23_LIM_MUTED, TLV320AIC23_LRS_ENABLED);
560c1f27190SArun KS 
561ff06ac2aSKuninori Morimoto 	snd_soc_component_update_bits(component, TLV320AIC23_ANLG,
562f9dfbf91SAxel Lin 			    TLV320AIC23_BYPASS_ON | TLV320AIC23_MICM_MUTED,
563f9dfbf91SAxel Lin 			    0);
564c1f27190SArun KS 
565c1f27190SArun KS 	/* Default output volume */
566ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_LCHNVOL,
567f9dfbf91SAxel Lin 		      TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
568ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_RCHNVOL,
569f9dfbf91SAxel Lin 		      TLV320AIC23_DEFAULT_OUT_VOL & TLV320AIC23_OUT_VOL_MASK);
570c1f27190SArun KS 
571ff06ac2aSKuninori Morimoto 	snd_soc_component_write(component, TLV320AIC23_ACTIVE, 0x1);
572c1f27190SArun KS 
573f0fba2adSLiam Girdwood 	return 0;
574c1f27190SArun KS }
575f0fba2adSLiam Girdwood 
576ff06ac2aSKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_tlv320aic23 = {
577ff06ac2aSKuninori Morimoto 	.probe			= tlv320aic23_component_probe,
578f0fba2adSLiam Girdwood 	.resume			= tlv320aic23_resume,
579f0fba2adSLiam Girdwood 	.set_bias_level		= tlv320aic23_set_bias_level,
580b07c443fSMark Brown 	.controls		= tlv320aic23_snd_controls,
581b07c443fSMark Brown 	.num_controls		= ARRAY_SIZE(tlv320aic23_snd_controls),
582a7dca707SLu Guanqun 	.dapm_widgets		= tlv320aic23_dapm_widgets,
583a7dca707SLu Guanqun 	.num_dapm_widgets	= ARRAY_SIZE(tlv320aic23_dapm_widgets),
584a7dca707SLu Guanqun 	.dapm_routes		= tlv320aic23_intercon,
585a7dca707SLu Guanqun 	.num_dapm_routes	= ARRAY_SIZE(tlv320aic23_intercon),
586ff06ac2aSKuninori Morimoto 	.suspend_bias_off	= 1,
587ff06ac2aSKuninori Morimoto 	.idle_bias_on		= 1,
588ff06ac2aSKuninori Morimoto 	.use_pmdown_time	= 1,
589ff06ac2aSKuninori Morimoto 	.endianness		= 1,
590ff06ac2aSKuninori Morimoto 	.non_legacy_dai_naming	= 1,
591f0fba2adSLiam Girdwood };
592c1f27190SArun KS 
593b3fc5725SMax Filippov int tlv320aic23_probe(struct device *dev, struct regmap *regmap)
594c1f27190SArun KS {
595f0fba2adSLiam Girdwood 	struct aic23 *aic23;
596c1f27190SArun KS 
597b3fc5725SMax Filippov 	if (IS_ERR(regmap))
598b3fc5725SMax Filippov 		return PTR_ERR(regmap);
599c1f27190SArun KS 
600b3fc5725SMax Filippov 	aic23 = devm_kzalloc(dev, sizeof(struct aic23), GFP_KERNEL);
601f0fba2adSLiam Girdwood 	if (aic23 == NULL)
602f0fba2adSLiam Girdwood 		return -ENOMEM;
603c1f27190SArun KS 
604b3fc5725SMax Filippov 	aic23->regmap = regmap;
6054aa11d67SMark Brown 
606b3fc5725SMax Filippov 	dev_set_drvdata(dev, aic23);
607c1f27190SArun KS 
608ff06ac2aSKuninori Morimoto 	return devm_snd_soc_register_component(dev,
609ff06ac2aSKuninori Morimoto 				      &soc_component_dev_tlv320aic23,
610b3fc5725SMax Filippov 				      &tlv320aic23_dai, 1);
611c1f27190SArun KS }
61240423285SMax Filippov EXPORT_SYMBOL(tlv320aic23_probe);
61364089b84SMark Brown 
614c1f27190SArun KS MODULE_DESCRIPTION("ASoC TLV320AIC23 codec driver");
615c1f27190SArun KS MODULE_AUTHOR("Arun KS <arunks@mistralsolutions.com>");
616c1f27190SArun KS MODULE_LICENSE("GPL");
617