xref: /openbmc/linux/sound/soc/codecs/es8316.c (revision 85250a24)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * es8316.c -- es8316 ALSA SoC audio driver
4  * Copyright Everest Semiconductor Co.,Ltd
5  *
6  * Authors: David Yang <yangxiaohua@everest-semi.com>,
7  *          Daniel Drake <drake@endlessm.com>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/acpi.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/i2c.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/mutex.h>
17 #include <linux/regmap.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/soc-dapm.h>
22 #include <sound/tlv.h>
23 #include <sound/jack.h>
24 #include "es8316.h"
25 
26 /* In slave mode at single speed, the codec is documented as accepting 5
27  * MCLK/LRCK ratios, but we also add ratio 400, which is commonly used on
28  * Intel Cherry Trail platforms (19.2MHz MCLK, 48kHz LRCK).
29  */
30 #define NR_SUPPORTED_MCLK_LRCK_RATIOS 6
31 static const unsigned int supported_mclk_lrck_ratios[] = {
32 	256, 384, 400, 512, 768, 1024
33 };
34 
35 struct es8316_priv {
36 	struct mutex lock;
37 	struct clk *mclk;
38 	struct regmap *regmap;
39 	struct snd_soc_component *component;
40 	struct snd_soc_jack *jack;
41 	int irq;
42 	unsigned int sysclk;
43 	unsigned int allowed_rates[NR_SUPPORTED_MCLK_LRCK_RATIOS];
44 	struct snd_pcm_hw_constraint_list sysclk_constraints;
45 	bool jd_inverted;
46 };
47 
48 /*
49  * ES8316 controls
50  */
51 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9600, 50, 1);
52 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9600, 50, 1);
53 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_max_gain_tlv, -650, 150, 0);
54 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_min_gain_tlv, -1200, 150, 0);
55 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_target_tlv, -1650, 150, 0);
56 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpmixer_gain_tlv,
57 	0, 4, TLV_DB_SCALE_ITEM(-1200, 150, 0),
58 	8, 11, TLV_DB_SCALE_ITEM(-450, 150, 0),
59 );
60 
61 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(adc_pga_gain_tlv,
62 	0, 0, TLV_DB_SCALE_ITEM(-350, 0, 0),
63 	1, 1, TLV_DB_SCALE_ITEM(0, 0, 0),
64 	2, 2, TLV_DB_SCALE_ITEM(250, 0, 0),
65 	3, 3, TLV_DB_SCALE_ITEM(450, 0, 0),
66 	4, 7, TLV_DB_SCALE_ITEM(700, 300, 0),
67 	8, 10, TLV_DB_SCALE_ITEM(1800, 300, 0),
68 );
69 
70 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpout_vol_tlv,
71 	0, 0, TLV_DB_SCALE_ITEM(-4800, 0, 0),
72 	1, 3, TLV_DB_SCALE_ITEM(-2400, 1200, 0),
73 );
74 
75 static const char * const ng_type_txt[] =
76 	{ "Constant PGA Gain", "Mute ADC Output" };
77 static const struct soc_enum ng_type =
78 	SOC_ENUM_SINGLE(ES8316_ADC_ALC_NG, 6, 2, ng_type_txt);
79 
80 static const char * const adcpol_txt[] = { "Normal", "Invert" };
81 static const struct soc_enum adcpol =
82 	SOC_ENUM_SINGLE(ES8316_ADC_MUTE, 1, 2, adcpol_txt);
83 static const char *const dacpol_txt[] =
84 	{ "Normal", "R Invert", "L Invert", "L + R Invert" };
85 static const struct soc_enum dacpol =
86 	SOC_ENUM_SINGLE(ES8316_DAC_SET1, 0, 4, dacpol_txt);
87 
88 static const struct snd_kcontrol_new es8316_snd_controls[] = {
89 	SOC_DOUBLE_TLV("Headphone Playback Volume", ES8316_CPHP_ICAL_VOL,
90 		       4, 0, 3, 1, hpout_vol_tlv),
91 	SOC_DOUBLE_TLV("Headphone Mixer Volume", ES8316_HPMIX_VOL,
92 		       4, 0, 11, 0, hpmixer_gain_tlv),
93 
94 	SOC_ENUM("Playback Polarity", dacpol),
95 	SOC_DOUBLE_R_TLV("DAC Playback Volume", ES8316_DAC_VOLL,
96 			 ES8316_DAC_VOLR, 0, 0xc0, 1, dac_vol_tlv),
97 	SOC_SINGLE("DAC Soft Ramp Switch", ES8316_DAC_SET1, 4, 1, 1),
98 	SOC_SINGLE("DAC Soft Ramp Rate", ES8316_DAC_SET1, 2, 4, 0),
99 	SOC_SINGLE("DAC Notch Filter Switch", ES8316_DAC_SET2, 6, 1, 0),
100 	SOC_SINGLE("DAC Double Fs Switch", ES8316_DAC_SET2, 7, 1, 0),
101 	SOC_SINGLE("DAC Stereo Enhancement", ES8316_DAC_SET3, 0, 7, 0),
102 	SOC_SINGLE("DAC Mono Mix Switch", ES8316_DAC_SET3, 3, 1, 0),
103 
104 	SOC_ENUM("Capture Polarity", adcpol),
105 	SOC_SINGLE("Mic Boost Switch", ES8316_ADC_D2SEPGA, 0, 1, 0),
106 	SOC_SINGLE_TLV("ADC Capture Volume", ES8316_ADC_VOLUME,
107 		       0, 0xc0, 1, adc_vol_tlv),
108 	SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8316_ADC_PGAGAIN,
109 		       4, 10, 0, adc_pga_gain_tlv),
110 	SOC_SINGLE("ADC Soft Ramp Switch", ES8316_ADC_MUTE, 4, 1, 0),
111 	SOC_SINGLE("ADC Double Fs Switch", ES8316_ADC_DMIC, 4, 1, 0),
112 
113 	SOC_SINGLE("ALC Capture Switch", ES8316_ADC_ALC1, 6, 1, 0),
114 	SOC_SINGLE_TLV("ALC Capture Max Volume", ES8316_ADC_ALC1, 0, 28, 0,
115 		       alc_max_gain_tlv),
116 	SOC_SINGLE_TLV("ALC Capture Min Volume", ES8316_ADC_ALC2, 0, 28, 0,
117 		       alc_min_gain_tlv),
118 	SOC_SINGLE_TLV("ALC Capture Target Volume", ES8316_ADC_ALC3, 4, 10, 0,
119 		       alc_target_tlv),
120 	SOC_SINGLE("ALC Capture Hold Time", ES8316_ADC_ALC3, 0, 10, 0),
121 	SOC_SINGLE("ALC Capture Decay Time", ES8316_ADC_ALC4, 4, 10, 0),
122 	SOC_SINGLE("ALC Capture Attack Time", ES8316_ADC_ALC4, 0, 10, 0),
123 	SOC_SINGLE("ALC Capture Noise Gate Switch", ES8316_ADC_ALC_NG,
124 		   5, 1, 0),
125 	SOC_SINGLE("ALC Capture Noise Gate Threshold", ES8316_ADC_ALC_NG,
126 		   0, 31, 0),
127 	SOC_ENUM("ALC Capture Noise Gate Type", ng_type),
128 };
129 
130 /* Analog Input Mux */
131 static const char * const es8316_analog_in_txt[] = {
132 		"lin1-rin1",
133 		"lin2-rin2",
134 		"lin1-rin1 with 20db Boost",
135 		"lin2-rin2 with 20db Boost"
136 };
137 static const unsigned int es8316_analog_in_values[] = { 0, 1, 2, 3 };
138 static const struct soc_enum es8316_analog_input_enum =
139 	SOC_VALUE_ENUM_SINGLE(ES8316_ADC_PDN_LINSEL, 4, 3,
140 			      ARRAY_SIZE(es8316_analog_in_txt),
141 			      es8316_analog_in_txt,
142 			      es8316_analog_in_values);
143 static const struct snd_kcontrol_new es8316_analog_in_mux_controls =
144 	SOC_DAPM_ENUM("Route", es8316_analog_input_enum);
145 
146 static const char * const es8316_dmic_txt[] = {
147 		"dmic disable",
148 		"dmic data at high level",
149 		"dmic data at low level",
150 };
151 static const unsigned int es8316_dmic_values[] = { 0, 1, 2 };
152 static const struct soc_enum es8316_dmic_src_enum =
153 	SOC_VALUE_ENUM_SINGLE(ES8316_ADC_DMIC, 0, 3,
154 			      ARRAY_SIZE(es8316_dmic_txt),
155 			      es8316_dmic_txt,
156 			      es8316_dmic_values);
157 static const struct snd_kcontrol_new es8316_dmic_src_controls =
158 	SOC_DAPM_ENUM("Route", es8316_dmic_src_enum);
159 
160 /* hp mixer mux */
161 static const char * const es8316_hpmux_texts[] = {
162 	"lin1-rin1",
163 	"lin2-rin2",
164 	"lin-rin with Boost",
165 	"lin-rin with Boost and PGA"
166 };
167 
168 static SOC_ENUM_SINGLE_DECL(es8316_left_hpmux_enum, ES8316_HPMIX_SEL,
169 	4, es8316_hpmux_texts);
170 
171 static const struct snd_kcontrol_new es8316_left_hpmux_controls =
172 	SOC_DAPM_ENUM("Route", es8316_left_hpmux_enum);
173 
174 static SOC_ENUM_SINGLE_DECL(es8316_right_hpmux_enum, ES8316_HPMIX_SEL,
175 	0, es8316_hpmux_texts);
176 
177 static const struct snd_kcontrol_new es8316_right_hpmux_controls =
178 	SOC_DAPM_ENUM("Route", es8316_right_hpmux_enum);
179 
180 /* headphone Output Mixer */
181 static const struct snd_kcontrol_new es8316_out_left_mix[] = {
182 	SOC_DAPM_SINGLE("LLIN Switch", ES8316_HPMIX_SWITCH, 6, 1, 0),
183 	SOC_DAPM_SINGLE("Left DAC Switch", ES8316_HPMIX_SWITCH, 7, 1, 0),
184 };
185 static const struct snd_kcontrol_new es8316_out_right_mix[] = {
186 	SOC_DAPM_SINGLE("RLIN Switch", ES8316_HPMIX_SWITCH, 2, 1, 0),
187 	SOC_DAPM_SINGLE("Right DAC Switch", ES8316_HPMIX_SWITCH, 3, 1, 0),
188 };
189 
190 /* DAC data source mux */
191 static const char * const es8316_dacsrc_texts[] = {
192 	"LDATA TO LDAC, RDATA TO RDAC",
193 	"LDATA TO LDAC, LDATA TO RDAC",
194 	"RDATA TO LDAC, RDATA TO RDAC",
195 	"RDATA TO LDAC, LDATA TO RDAC",
196 };
197 
198 static SOC_ENUM_SINGLE_DECL(es8316_dacsrc_mux_enum, ES8316_DAC_SET1,
199 	6, es8316_dacsrc_texts);
200 
201 static const struct snd_kcontrol_new es8316_dacsrc_mux_controls =
202 	SOC_DAPM_ENUM("Route", es8316_dacsrc_mux_enum);
203 
204 static const struct snd_soc_dapm_widget es8316_dapm_widgets[] = {
205 	SND_SOC_DAPM_SUPPLY("Bias", ES8316_SYS_PDN, 3, 1, NULL, 0),
206 	SND_SOC_DAPM_SUPPLY("Analog power", ES8316_SYS_PDN, 4, 1, NULL, 0),
207 	SND_SOC_DAPM_SUPPLY("Mic Bias", ES8316_SYS_PDN, 5, 1, NULL, 0),
208 
209 	SND_SOC_DAPM_INPUT("DMIC"),
210 	SND_SOC_DAPM_INPUT("MIC1"),
211 	SND_SOC_DAPM_INPUT("MIC2"),
212 
213 	/* Input Mux */
214 	SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
215 			 &es8316_analog_in_mux_controls),
216 
217 	SND_SOC_DAPM_SUPPLY("ADC Vref", ES8316_SYS_PDN, 1, 1, NULL, 0),
218 	SND_SOC_DAPM_SUPPLY("ADC bias", ES8316_SYS_PDN, 2, 1, NULL, 0),
219 	SND_SOC_DAPM_SUPPLY("ADC Clock", ES8316_CLKMGR_CLKSW, 3, 0, NULL, 0),
220 	SND_SOC_DAPM_PGA("Line input PGA", ES8316_ADC_PDN_LINSEL,
221 			 7, 1, NULL, 0),
222 	SND_SOC_DAPM_ADC("Mono ADC", NULL, ES8316_ADC_PDN_LINSEL, 6, 1),
223 	SND_SOC_DAPM_MUX("Digital Mic Mux", SND_SOC_NOPM, 0, 0,
224 			 &es8316_dmic_src_controls),
225 
226 	/* Digital Interface */
227 	SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture",  1,
228 			     ES8316_SERDATA_ADC, 6, 1),
229 	SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0,
230 			    SND_SOC_NOPM, 0, 0),
231 
232 	SND_SOC_DAPM_MUX("DAC Source Mux", SND_SOC_NOPM, 0, 0,
233 			 &es8316_dacsrc_mux_controls),
234 
235 	SND_SOC_DAPM_SUPPLY("DAC Vref", ES8316_SYS_PDN, 0, 1, NULL, 0),
236 	SND_SOC_DAPM_SUPPLY("DAC Clock", ES8316_CLKMGR_CLKSW, 2, 0, NULL, 0),
237 	SND_SOC_DAPM_DAC("Right DAC", NULL, ES8316_DAC_PDN, 0, 1),
238 	SND_SOC_DAPM_DAC("Left DAC", NULL, ES8316_DAC_PDN, 4, 1),
239 
240 	/* Headphone Output Side */
241 	SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
242 			 &es8316_left_hpmux_controls),
243 	SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
244 			 &es8316_right_hpmux_controls),
245 	SND_SOC_DAPM_MIXER("Left Headphone Mixer", ES8316_HPMIX_PDN,
246 			   5, 1, &es8316_out_left_mix[0],
247 			   ARRAY_SIZE(es8316_out_left_mix)),
248 	SND_SOC_DAPM_MIXER("Right Headphone Mixer", ES8316_HPMIX_PDN,
249 			   1, 1, &es8316_out_right_mix[0],
250 			   ARRAY_SIZE(es8316_out_right_mix)),
251 	SND_SOC_DAPM_PGA("Left Headphone Mixer Out", ES8316_HPMIX_PDN,
252 			 4, 1, NULL, 0),
253 	SND_SOC_DAPM_PGA("Right Headphone Mixer Out", ES8316_HPMIX_PDN,
254 			 0, 1, NULL, 0),
255 
256 	SND_SOC_DAPM_OUT_DRV("Left Headphone Charge Pump", ES8316_CPHP_OUTEN,
257 			     6, 0, NULL, 0),
258 	SND_SOC_DAPM_OUT_DRV("Right Headphone Charge Pump", ES8316_CPHP_OUTEN,
259 			     2, 0, NULL, 0),
260 	SND_SOC_DAPM_SUPPLY("Headphone Charge Pump", ES8316_CPHP_PDN2,
261 			    5, 1, NULL, 0),
262 	SND_SOC_DAPM_SUPPLY("Headphone Charge Pump Clock", ES8316_CLKMGR_CLKSW,
263 			    4, 0, NULL, 0),
264 
265 	SND_SOC_DAPM_OUT_DRV("Left Headphone Driver", ES8316_CPHP_OUTEN,
266 			     5, 0, NULL, 0),
267 	SND_SOC_DAPM_OUT_DRV("Right Headphone Driver", ES8316_CPHP_OUTEN,
268 			     1, 0, NULL, 0),
269 	SND_SOC_DAPM_SUPPLY("Headphone Out", ES8316_CPHP_PDN1, 2, 1, NULL, 0),
270 
271 	/* pdn_Lical and pdn_Rical bits are documented as Reserved, but must
272 	 * be explicitly unset in order to enable HP output
273 	 */
274 	SND_SOC_DAPM_SUPPLY("Left Headphone ical", ES8316_CPHP_ICAL_VOL,
275 			    7, 1, NULL, 0),
276 	SND_SOC_DAPM_SUPPLY("Right Headphone ical", ES8316_CPHP_ICAL_VOL,
277 			    3, 1, NULL, 0),
278 
279 	SND_SOC_DAPM_OUTPUT("HPOL"),
280 	SND_SOC_DAPM_OUTPUT("HPOR"),
281 };
282 
283 static const struct snd_soc_dapm_route es8316_dapm_routes[] = {
284 	/* Recording */
285 	{"MIC1", NULL, "Mic Bias"},
286 	{"MIC2", NULL, "Mic Bias"},
287 	{"MIC1", NULL, "Bias"},
288 	{"MIC2", NULL, "Bias"},
289 	{"MIC1", NULL, "Analog power"},
290 	{"MIC2", NULL, "Analog power"},
291 
292 	{"Differential Mux", "lin1-rin1", "MIC1"},
293 	{"Differential Mux", "lin2-rin2", "MIC2"},
294 	{"Line input PGA", NULL, "Differential Mux"},
295 
296 	{"Mono ADC", NULL, "ADC Clock"},
297 	{"Mono ADC", NULL, "ADC Vref"},
298 	{"Mono ADC", NULL, "ADC bias"},
299 	{"Mono ADC", NULL, "Line input PGA"},
300 
301 	/* It's not clear why, but to avoid recording only silence,
302 	 * the DAC clock must be running for the ADC to work.
303 	 */
304 	{"Mono ADC", NULL, "DAC Clock"},
305 
306 	{"Digital Mic Mux", "dmic disable", "Mono ADC"},
307 
308 	{"I2S OUT", NULL, "Digital Mic Mux"},
309 
310 	/* Playback */
311 	{"DAC Source Mux", "LDATA TO LDAC, RDATA TO RDAC", "I2S IN"},
312 
313 	{"Left DAC", NULL, "DAC Clock"},
314 	{"Right DAC", NULL, "DAC Clock"},
315 
316 	{"Left DAC", NULL, "DAC Vref"},
317 	{"Right DAC", NULL, "DAC Vref"},
318 
319 	{"Left DAC", NULL, "DAC Source Mux"},
320 	{"Right DAC", NULL, "DAC Source Mux"},
321 
322 	{"Left Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
323 	{"Right Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
324 
325 	{"Left Headphone Mixer", "LLIN Switch", "Left Headphone Mux"},
326 	{"Left Headphone Mixer", "Left DAC Switch", "Left DAC"},
327 
328 	{"Right Headphone Mixer", "RLIN Switch", "Right Headphone Mux"},
329 	{"Right Headphone Mixer", "Right DAC Switch", "Right DAC"},
330 
331 	{"Left Headphone Mixer Out", NULL, "Left Headphone Mixer"},
332 	{"Right Headphone Mixer Out", NULL, "Right Headphone Mixer"},
333 
334 	{"Left Headphone Charge Pump", NULL, "Left Headphone Mixer Out"},
335 	{"Right Headphone Charge Pump", NULL, "Right Headphone Mixer Out"},
336 
337 	{"Left Headphone Charge Pump", NULL, "Headphone Charge Pump"},
338 	{"Right Headphone Charge Pump", NULL, "Headphone Charge Pump"},
339 
340 	{"Left Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
341 	{"Right Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
342 
343 	{"Left Headphone Driver", NULL, "Left Headphone Charge Pump"},
344 	{"Right Headphone Driver", NULL, "Right Headphone Charge Pump"},
345 
346 	{"HPOL", NULL, "Left Headphone Driver"},
347 	{"HPOR", NULL, "Right Headphone Driver"},
348 
349 	{"HPOL", NULL, "Left Headphone ical"},
350 	{"HPOR", NULL, "Right Headphone ical"},
351 
352 	{"Headphone Out", NULL, "Bias"},
353 	{"Headphone Out", NULL, "Analog power"},
354 	{"HPOL", NULL, "Headphone Out"},
355 	{"HPOR", NULL, "Headphone Out"},
356 };
357 
358 static int es8316_set_dai_sysclk(struct snd_soc_dai *codec_dai,
359 				 int clk_id, unsigned int freq, int dir)
360 {
361 	struct snd_soc_component *component = codec_dai->component;
362 	struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
363 	int i, ret;
364 	int count = 0;
365 
366 	es8316->sysclk = freq;
367 
368 	if (freq == 0) {
369 		es8316->sysclk_constraints.list = NULL;
370 		es8316->sysclk_constraints.count = 0;
371 
372 		return 0;
373 	}
374 
375 	ret = clk_set_rate(es8316->mclk, freq);
376 	if (ret)
377 		return ret;
378 
379 	/* Limit supported sample rates to ones that can be autodetected
380 	 * by the codec running in slave mode.
381 	 */
382 	for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
383 		const unsigned int ratio = supported_mclk_lrck_ratios[i];
384 
385 		if (freq % ratio == 0)
386 			es8316->allowed_rates[count++] = freq / ratio;
387 	}
388 
389 	es8316->sysclk_constraints.list = es8316->allowed_rates;
390 	es8316->sysclk_constraints.count = count;
391 
392 	return 0;
393 }
394 
395 static int es8316_set_dai_fmt(struct snd_soc_dai *codec_dai,
396 			      unsigned int fmt)
397 {
398 	struct snd_soc_component *component = codec_dai->component;
399 	u8 serdata1 = 0;
400 	u8 serdata2 = 0;
401 	u8 clksw;
402 	u8 mask;
403 
404 	if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBP_CFP)
405 		serdata1 |= ES8316_SERDATA1_MASTER;
406 
407 	if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S) {
408 		dev_err(component->dev, "Codec driver only supports I2S format\n");
409 		return -EINVAL;
410 	}
411 
412 	/* Clock inversion */
413 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
414 	case SND_SOC_DAIFMT_NB_NF:
415 		break;
416 	case SND_SOC_DAIFMT_IB_IF:
417 		serdata1 |= ES8316_SERDATA1_BCLK_INV;
418 		serdata2 |= ES8316_SERDATA2_ADCLRP;
419 		break;
420 	case SND_SOC_DAIFMT_IB_NF:
421 		serdata1 |= ES8316_SERDATA1_BCLK_INV;
422 		break;
423 	case SND_SOC_DAIFMT_NB_IF:
424 		serdata2 |= ES8316_SERDATA2_ADCLRP;
425 		break;
426 	default:
427 		return -EINVAL;
428 	}
429 
430 	mask = ES8316_SERDATA1_MASTER | ES8316_SERDATA1_BCLK_INV;
431 	snd_soc_component_update_bits(component, ES8316_SERDATA1, mask, serdata1);
432 
433 	mask = ES8316_SERDATA2_FMT_MASK | ES8316_SERDATA2_ADCLRP;
434 	snd_soc_component_update_bits(component, ES8316_SERDATA_ADC, mask, serdata2);
435 	snd_soc_component_update_bits(component, ES8316_SERDATA_DAC, mask, serdata2);
436 
437 	/* Enable BCLK and MCLK inputs in slave mode */
438 	clksw = ES8316_CLKMGR_CLKSW_MCLK_ON | ES8316_CLKMGR_CLKSW_BCLK_ON;
439 	snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW, clksw, clksw);
440 
441 	return 0;
442 }
443 
444 static int es8316_pcm_startup(struct snd_pcm_substream *substream,
445 			      struct snd_soc_dai *dai)
446 {
447 	struct snd_soc_component *component = dai->component;
448 	struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
449 
450 	if (es8316->sysclk_constraints.list)
451 		snd_pcm_hw_constraint_list(substream->runtime, 0,
452 					   SNDRV_PCM_HW_PARAM_RATE,
453 					   &es8316->sysclk_constraints);
454 
455 	return 0;
456 }
457 
458 static int es8316_pcm_hw_params(struct snd_pcm_substream *substream,
459 				struct snd_pcm_hw_params *params,
460 				struct snd_soc_dai *dai)
461 {
462 	struct snd_soc_component *component = dai->component;
463 	struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
464 	u8 wordlen = 0;
465 	u8 bclk_divider;
466 	u16 lrck_divider;
467 	int i;
468 
469 	/* Validate supported sample rates that are autodetected from MCLK */
470 	for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
471 		const unsigned int ratio = supported_mclk_lrck_ratios[i];
472 
473 		if (es8316->sysclk % ratio != 0)
474 			continue;
475 		if (es8316->sysclk / ratio == params_rate(params))
476 			break;
477 	}
478 	if (i == NR_SUPPORTED_MCLK_LRCK_RATIOS)
479 		return -EINVAL;
480 	lrck_divider = es8316->sysclk / params_rate(params);
481 	bclk_divider = lrck_divider / 4;
482 	switch (params_format(params)) {
483 	case SNDRV_PCM_FORMAT_S16_LE:
484 		wordlen = ES8316_SERDATA2_LEN_16;
485 		bclk_divider /= 16;
486 		break;
487 	case SNDRV_PCM_FORMAT_S20_3LE:
488 		wordlen = ES8316_SERDATA2_LEN_20;
489 		bclk_divider /= 20;
490 		break;
491 	case SNDRV_PCM_FORMAT_S24_LE:
492 		wordlen = ES8316_SERDATA2_LEN_24;
493 		bclk_divider /= 24;
494 		break;
495 	case SNDRV_PCM_FORMAT_S32_LE:
496 		wordlen = ES8316_SERDATA2_LEN_32;
497 		bclk_divider /= 32;
498 		break;
499 	default:
500 		return -EINVAL;
501 	}
502 
503 	snd_soc_component_update_bits(component, ES8316_SERDATA_DAC,
504 			    ES8316_SERDATA2_LEN_MASK, wordlen);
505 	snd_soc_component_update_bits(component, ES8316_SERDATA_ADC,
506 			    ES8316_SERDATA2_LEN_MASK, wordlen);
507 	snd_soc_component_update_bits(component, ES8316_SERDATA1, 0x1f, bclk_divider);
508 	snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV1, 0x0f, lrck_divider >> 8);
509 	snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV2, 0xff, lrck_divider & 0xff);
510 	snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV1, 0x0f, lrck_divider >> 8);
511 	snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV2, 0xff, lrck_divider & 0xff);
512 	return 0;
513 }
514 
515 static int es8316_mute(struct snd_soc_dai *dai, int mute, int direction)
516 {
517 	snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20,
518 			    mute ? 0x20 : 0);
519 	return 0;
520 }
521 
522 #define ES8316_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
523 			SNDRV_PCM_FMTBIT_S24_LE)
524 
525 static const struct snd_soc_dai_ops es8316_ops = {
526 	.startup = es8316_pcm_startup,
527 	.hw_params = es8316_pcm_hw_params,
528 	.set_fmt = es8316_set_dai_fmt,
529 	.set_sysclk = es8316_set_dai_sysclk,
530 	.mute_stream = es8316_mute,
531 	.no_capture_mute = 1,
532 };
533 
534 static struct snd_soc_dai_driver es8316_dai = {
535 	.name = "ES8316 HiFi",
536 	.playback = {
537 		.stream_name = "Playback",
538 		.channels_min = 1,
539 		.channels_max = 2,
540 		.rates = SNDRV_PCM_RATE_8000_48000,
541 		.formats = ES8316_FORMATS,
542 	},
543 	.capture = {
544 		.stream_name = "Capture",
545 		.channels_min = 1,
546 		.channels_max = 2,
547 		.rates = SNDRV_PCM_RATE_8000_48000,
548 		.formats = ES8316_FORMATS,
549 	},
550 	.ops = &es8316_ops,
551 	.symmetric_rate = 1,
552 };
553 
554 static void es8316_enable_micbias_for_mic_gnd_short_detect(
555 	struct snd_soc_component *component)
556 {
557 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
558 
559 	snd_soc_dapm_mutex_lock(dapm);
560 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "Bias");
561 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "Analog power");
562 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "Mic Bias");
563 	snd_soc_dapm_sync_unlocked(dapm);
564 	snd_soc_dapm_mutex_unlock(dapm);
565 
566 	msleep(20);
567 }
568 
569 static void es8316_disable_micbias_for_mic_gnd_short_detect(
570 	struct snd_soc_component *component)
571 {
572 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
573 
574 	snd_soc_dapm_mutex_lock(dapm);
575 	snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Bias");
576 	snd_soc_dapm_disable_pin_unlocked(dapm, "Analog power");
577 	snd_soc_dapm_disable_pin_unlocked(dapm, "Bias");
578 	snd_soc_dapm_sync_unlocked(dapm);
579 	snd_soc_dapm_mutex_unlock(dapm);
580 }
581 
582 static irqreturn_t es8316_irq(int irq, void *data)
583 {
584 	struct es8316_priv *es8316 = data;
585 	struct snd_soc_component *comp = es8316->component;
586 	unsigned int flags;
587 
588 	mutex_lock(&es8316->lock);
589 
590 	regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
591 	if (flags == 0x00)
592 		goto out; /* Powered-down / reset */
593 
594 	/* Catch spurious IRQ before set_jack is called */
595 	if (!es8316->jack)
596 		goto out;
597 
598 	if (es8316->jd_inverted)
599 		flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
600 
601 	dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
602 	if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
603 		/* Jack removed, or spurious IRQ? */
604 		if (es8316->jack->status & SND_JACK_MICROPHONE)
605 			es8316_disable_micbias_for_mic_gnd_short_detect(comp);
606 
607 		if (es8316->jack->status & SND_JACK_HEADPHONE) {
608 			snd_soc_jack_report(es8316->jack, 0,
609 					    SND_JACK_HEADSET | SND_JACK_BTN_0);
610 			dev_dbg(comp->dev, "jack unplugged\n");
611 		}
612 	} else if (!(es8316->jack->status & SND_JACK_HEADPHONE)) {
613 		/* Jack inserted, determine type */
614 		es8316_enable_micbias_for_mic_gnd_short_detect(comp);
615 		regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
616 		if (es8316->jd_inverted)
617 			flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
618 		dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
619 		if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
620 			/* Jack unplugged underneath us */
621 			es8316_disable_micbias_for_mic_gnd_short_detect(comp);
622 		} else if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
623 			/* Open, headset */
624 			snd_soc_jack_report(es8316->jack,
625 					    SND_JACK_HEADSET,
626 					    SND_JACK_HEADSET);
627 			/* Keep mic-gnd-short detection on for button press */
628 		} else {
629 			/* Shorted, headphones */
630 			snd_soc_jack_report(es8316->jack,
631 					    SND_JACK_HEADPHONE,
632 					    SND_JACK_HEADSET);
633 			/* No longer need mic-gnd-short detection */
634 			es8316_disable_micbias_for_mic_gnd_short_detect(comp);
635 		}
636 	} else if (es8316->jack->status & SND_JACK_MICROPHONE) {
637 		/* Interrupt while jack inserted, report button state */
638 		if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
639 			/* Open, button release */
640 			snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
641 		} else {
642 			/* Short, button press */
643 			snd_soc_jack_report(es8316->jack,
644 					    SND_JACK_BTN_0,
645 					    SND_JACK_BTN_0);
646 		}
647 	}
648 
649 out:
650 	mutex_unlock(&es8316->lock);
651 	return IRQ_HANDLED;
652 }
653 
654 static void es8316_enable_jack_detect(struct snd_soc_component *component,
655 				      struct snd_soc_jack *jack)
656 {
657 	struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
658 
659 	/*
660 	 * Init es8316->jd_inverted here and not in the probe, as we cannot
661 	 * guarantee that the bytchr-es8316 driver, which might set this
662 	 * property, will probe before us.
663 	 */
664 	es8316->jd_inverted = device_property_read_bool(component->dev,
665 							"everest,jack-detect-inverted");
666 
667 	mutex_lock(&es8316->lock);
668 
669 	es8316->jack = jack;
670 
671 	if (es8316->jack->status & SND_JACK_MICROPHONE)
672 		es8316_enable_micbias_for_mic_gnd_short_detect(component);
673 
674 	snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
675 				      ES8316_GPIO_ENABLE_INTERRUPT,
676 				      ES8316_GPIO_ENABLE_INTERRUPT);
677 
678 	mutex_unlock(&es8316->lock);
679 
680 	/* Enable irq and sync initial jack state */
681 	enable_irq(es8316->irq);
682 	es8316_irq(es8316->irq, es8316);
683 }
684 
685 static void es8316_disable_jack_detect(struct snd_soc_component *component)
686 {
687 	struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
688 
689 	if (!es8316->jack)
690 		return; /* Already disabled (or never enabled) */
691 
692 	disable_irq(es8316->irq);
693 
694 	mutex_lock(&es8316->lock);
695 
696 	snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
697 				      ES8316_GPIO_ENABLE_INTERRUPT, 0);
698 
699 	if (es8316->jack->status & SND_JACK_MICROPHONE) {
700 		es8316_disable_micbias_for_mic_gnd_short_detect(component);
701 		snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
702 	}
703 
704 	es8316->jack = NULL;
705 
706 	mutex_unlock(&es8316->lock);
707 }
708 
709 static int es8316_set_jack(struct snd_soc_component *component,
710 			   struct snd_soc_jack *jack, void *data)
711 {
712 	if (jack)
713 		es8316_enable_jack_detect(component, jack);
714 	else
715 		es8316_disable_jack_detect(component);
716 
717 	return 0;
718 }
719 
720 static int es8316_probe(struct snd_soc_component *component)
721 {
722 	struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
723 	int ret;
724 
725 	es8316->component = component;
726 
727 	es8316->mclk = devm_clk_get_optional(component->dev, "mclk");
728 	if (IS_ERR(es8316->mclk)) {
729 		dev_err(component->dev, "unable to get mclk\n");
730 		return PTR_ERR(es8316->mclk);
731 	}
732 	if (!es8316->mclk)
733 		dev_warn(component->dev, "assuming static mclk\n");
734 
735 	ret = clk_prepare_enable(es8316->mclk);
736 	if (ret) {
737 		dev_err(component->dev, "unable to enable mclk\n");
738 		return ret;
739 	}
740 
741 	/* Reset codec and enable current state machine */
742 	snd_soc_component_write(component, ES8316_RESET, 0x3f);
743 	usleep_range(5000, 5500);
744 	snd_soc_component_write(component, ES8316_RESET, ES8316_RESET_CSM_ON);
745 	msleep(30);
746 
747 	/*
748 	 * Documentation is unclear, but this value from the vendor driver is
749 	 * needed otherwise audio output is silent.
750 	 */
751 	snd_soc_component_write(component, ES8316_SYS_VMIDSEL, 0xff);
752 
753 	/*
754 	 * Documentation for this register is unclear and incomplete,
755 	 * but here is a vendor-provided value that improves volume
756 	 * and quality for Intel CHT platforms.
757 	 */
758 	snd_soc_component_write(component, ES8316_CLKMGR_ADCOSR, 0x32);
759 
760 	return 0;
761 }
762 
763 static void es8316_remove(struct snd_soc_component *component)
764 {
765 	struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
766 
767 	clk_disable_unprepare(es8316->mclk);
768 }
769 
770 static int es8316_resume(struct snd_soc_component *component)
771 {
772 	struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
773 
774 	regcache_cache_only(es8316->regmap, false);
775 	regcache_sync(es8316->regmap);
776 
777 	return 0;
778 }
779 
780 static int es8316_suspend(struct snd_soc_component *component)
781 {
782 	struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
783 
784 	regcache_cache_only(es8316->regmap, true);
785 	regcache_mark_dirty(es8316->regmap);
786 
787 	return 0;
788 }
789 
790 static const struct snd_soc_component_driver soc_component_dev_es8316 = {
791 	.probe			= es8316_probe,
792 	.remove			= es8316_remove,
793 	.resume			= es8316_resume,
794 	.suspend		= es8316_suspend,
795 	.set_jack		= es8316_set_jack,
796 	.controls		= es8316_snd_controls,
797 	.num_controls		= ARRAY_SIZE(es8316_snd_controls),
798 	.dapm_widgets		= es8316_dapm_widgets,
799 	.num_dapm_widgets	= ARRAY_SIZE(es8316_dapm_widgets),
800 	.dapm_routes		= es8316_dapm_routes,
801 	.num_dapm_routes	= ARRAY_SIZE(es8316_dapm_routes),
802 	.use_pmdown_time	= 1,
803 	.endianness		= 1,
804 };
805 
806 static const struct regmap_range es8316_volatile_ranges[] = {
807 	regmap_reg_range(ES8316_GPIO_FLAG, ES8316_GPIO_FLAG),
808 };
809 
810 static const struct regmap_access_table es8316_volatile_table = {
811 	.yes_ranges	= es8316_volatile_ranges,
812 	.n_yes_ranges	= ARRAY_SIZE(es8316_volatile_ranges),
813 };
814 
815 static const struct regmap_config es8316_regmap = {
816 	.reg_bits = 8,
817 	.val_bits = 8,
818 	.use_single_read = true,
819 	.use_single_write = true,
820 	.max_register = 0x53,
821 	.volatile_table	= &es8316_volatile_table,
822 	.cache_type = REGCACHE_RBTREE,
823 };
824 
825 static int es8316_i2c_probe(struct i2c_client *i2c_client)
826 {
827 	struct device *dev = &i2c_client->dev;
828 	struct es8316_priv *es8316;
829 	int ret;
830 
831 	es8316 = devm_kzalloc(&i2c_client->dev, sizeof(struct es8316_priv),
832 			      GFP_KERNEL);
833 	if (es8316 == NULL)
834 		return -ENOMEM;
835 
836 	i2c_set_clientdata(i2c_client, es8316);
837 
838 	es8316->regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap);
839 	if (IS_ERR(es8316->regmap))
840 		return PTR_ERR(es8316->regmap);
841 
842 	es8316->irq = i2c_client->irq;
843 	mutex_init(&es8316->lock);
844 
845 	ret = devm_request_threaded_irq(dev, es8316->irq, NULL, es8316_irq,
846 					IRQF_TRIGGER_HIGH | IRQF_ONESHOT | IRQF_NO_AUTOEN,
847 					"es8316", es8316);
848 	if (ret) {
849 		dev_warn(dev, "Failed to get IRQ %d: %d\n", es8316->irq, ret);
850 		es8316->irq = -ENXIO;
851 	}
852 
853 	return devm_snd_soc_register_component(&i2c_client->dev,
854 				      &soc_component_dev_es8316,
855 				      &es8316_dai, 1);
856 }
857 
858 static const struct i2c_device_id es8316_i2c_id[] = {
859 	{"es8316", 0 },
860 	{}
861 };
862 MODULE_DEVICE_TABLE(i2c, es8316_i2c_id);
863 
864 #ifdef CONFIG_OF
865 static const struct of_device_id es8316_of_match[] = {
866 	{ .compatible = "everest,es8316", },
867 	{},
868 };
869 MODULE_DEVICE_TABLE(of, es8316_of_match);
870 #endif
871 
872 #ifdef CONFIG_ACPI
873 static const struct acpi_device_id es8316_acpi_match[] = {
874 	{"ESSX8316", 0},
875 	{"ESSX8336", 0},
876 	{},
877 };
878 MODULE_DEVICE_TABLE(acpi, es8316_acpi_match);
879 #endif
880 
881 static struct i2c_driver es8316_i2c_driver = {
882 	.driver = {
883 		.name			= "es8316",
884 		.acpi_match_table	= ACPI_PTR(es8316_acpi_match),
885 		.of_match_table		= of_match_ptr(es8316_of_match),
886 	},
887 	.probe_new	= es8316_i2c_probe,
888 	.id_table	= es8316_i2c_id,
889 };
890 module_i2c_driver(es8316_i2c_driver);
891 
892 MODULE_DESCRIPTION("Everest Semi ES8316 ALSA SoC Codec Driver");
893 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
894 MODULE_LICENSE("GPL v2");
895