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