xref: /openbmc/linux/sound/soc/codecs/es8316.c (revision 93f5715e)
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/regmap.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22 #include <sound/soc-dapm.h>
23 #include <sound/tlv.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 	unsigned int sysclk;
37 	unsigned int allowed_rates[NR_SUPPORTED_MCLK_LRCK_RATIOS];
38 	struct snd_pcm_hw_constraint_list sysclk_constraints;
39 };
40 
41 /*
42  * ES8316 controls
43  */
44 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9600, 50, 1);
45 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9600, 50, 1);
46 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_max_gain_tlv, -650, 150, 0);
47 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_min_gain_tlv, -1200, 150, 0);
48 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_target_tlv, -1650, 150, 0);
49 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(hpmixer_gain_tlv, -1200, 150, 0);
50 
51 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(adc_pga_gain_tlv,
52 	0, 0, TLV_DB_SCALE_ITEM(-350, 0, 0),
53 	1, 1, TLV_DB_SCALE_ITEM(0, 0, 0),
54 	2, 2, TLV_DB_SCALE_ITEM(250, 0, 0),
55 	3, 3, TLV_DB_SCALE_ITEM(450, 0, 0),
56 	4, 4, TLV_DB_SCALE_ITEM(700, 0, 0),
57 	5, 5, TLV_DB_SCALE_ITEM(1000, 0, 0),
58 	6, 6, TLV_DB_SCALE_ITEM(1300, 0, 0),
59 	7, 7, TLV_DB_SCALE_ITEM(1600, 0, 0),
60 	8, 8, TLV_DB_SCALE_ITEM(1800, 0, 0),
61 	9, 9, TLV_DB_SCALE_ITEM(2100, 0, 0),
62 	10, 10, TLV_DB_SCALE_ITEM(2400, 0, 0),
63 );
64 
65 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpout_vol_tlv,
66 	0, 0, TLV_DB_SCALE_ITEM(-4800, 0, 0),
67 	1, 3, TLV_DB_SCALE_ITEM(-2400, 1200, 0),
68 );
69 
70 static const char * const ng_type_txt[] =
71 	{ "Constant PGA Gain", "Mute ADC Output" };
72 static const struct soc_enum ng_type =
73 	SOC_ENUM_SINGLE(ES8316_ADC_ALC_NG, 6, 2, ng_type_txt);
74 
75 static const char * const adcpol_txt[] = { "Normal", "Invert" };
76 static const struct soc_enum adcpol =
77 	SOC_ENUM_SINGLE(ES8316_ADC_MUTE, 1, 2, adcpol_txt);
78 static const char *const dacpol_txt[] =
79 	{ "Normal", "R Invert", "L Invert", "L + R Invert" };
80 static const struct soc_enum dacpol =
81 	SOC_ENUM_SINGLE(ES8316_DAC_SET1, 0, 4, dacpol_txt);
82 
83 static const struct snd_kcontrol_new es8316_snd_controls[] = {
84 	SOC_DOUBLE_TLV("Headphone Playback Volume", ES8316_CPHP_ICAL_VOL,
85 		       4, 0, 3, 1, hpout_vol_tlv),
86 	SOC_DOUBLE_TLV("Headphone Mixer Volume", ES8316_HPMIX_VOL,
87 		       0, 4, 7, 0, hpmixer_gain_tlv),
88 
89 	SOC_ENUM("Playback Polarity", dacpol),
90 	SOC_DOUBLE_R_TLV("DAC Playback Volume", ES8316_DAC_VOLL,
91 			 ES8316_DAC_VOLR, 0, 0xc0, 1, dac_vol_tlv),
92 	SOC_SINGLE("DAC Soft Ramp Switch", ES8316_DAC_SET1, 4, 1, 1),
93 	SOC_SINGLE("DAC Soft Ramp Rate", ES8316_DAC_SET1, 2, 4, 0),
94 	SOC_SINGLE("DAC Notch Filter Switch", ES8316_DAC_SET2, 6, 1, 0),
95 	SOC_SINGLE("DAC Double Fs Switch", ES8316_DAC_SET2, 7, 1, 0),
96 	SOC_SINGLE("DAC Stereo Enhancement", ES8316_DAC_SET3, 0, 7, 0),
97 
98 	SOC_ENUM("Capture Polarity", adcpol),
99 	SOC_SINGLE("Mic Boost Switch", ES8316_ADC_D2SEPGA, 0, 1, 0),
100 	SOC_SINGLE_TLV("ADC Capture Volume", ES8316_ADC_VOLUME,
101 		       0, 0xc0, 1, adc_vol_tlv),
102 	SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8316_ADC_PGAGAIN,
103 		       4, 10, 0, adc_pga_gain_tlv),
104 	SOC_SINGLE("ADC Soft Ramp Switch", ES8316_ADC_MUTE, 4, 1, 0),
105 	SOC_SINGLE("ADC Double Fs Switch", ES8316_ADC_DMIC, 4, 1, 0),
106 
107 	SOC_SINGLE("ALC Capture Switch", ES8316_ADC_ALC1, 6, 1, 0),
108 	SOC_SINGLE_TLV("ALC Capture Max Volume", ES8316_ADC_ALC1, 0, 28, 0,
109 		       alc_max_gain_tlv),
110 	SOC_SINGLE_TLV("ALC Capture Min Volume", ES8316_ADC_ALC2, 0, 28, 0,
111 		       alc_min_gain_tlv),
112 	SOC_SINGLE_TLV("ALC Capture Target Volume", ES8316_ADC_ALC3, 4, 10, 0,
113 		       alc_target_tlv),
114 	SOC_SINGLE("ALC Capture Hold Time", ES8316_ADC_ALC3, 0, 10, 0),
115 	SOC_SINGLE("ALC Capture Decay Time", ES8316_ADC_ALC4, 4, 10, 0),
116 	SOC_SINGLE("ALC Capture Attack Time", ES8316_ADC_ALC4, 0, 10, 0),
117 	SOC_SINGLE("ALC Capture Noise Gate Switch", ES8316_ADC_ALC_NG,
118 		   5, 1, 0),
119 	SOC_SINGLE("ALC Capture Noise Gate Threshold", ES8316_ADC_ALC_NG,
120 		   0, 31, 0),
121 	SOC_ENUM("ALC Capture Noise Gate Type", ng_type),
122 };
123 
124 /* Analog Input Mux */
125 static const char * const es8316_analog_in_txt[] = {
126 		"lin1-rin1",
127 		"lin2-rin2",
128 		"lin1-rin1 with 20db Boost",
129 		"lin2-rin2 with 20db Boost"
130 };
131 static const unsigned int es8316_analog_in_values[] = { 0, 1, 2, 3 };
132 static const struct soc_enum es8316_analog_input_enum =
133 	SOC_VALUE_ENUM_SINGLE(ES8316_ADC_PDN_LINSEL, 4, 3,
134 			      ARRAY_SIZE(es8316_analog_in_txt),
135 			      es8316_analog_in_txt,
136 			      es8316_analog_in_values);
137 static const struct snd_kcontrol_new es8316_analog_in_mux_controls =
138 	SOC_DAPM_ENUM("Route", es8316_analog_input_enum);
139 
140 static const char * const es8316_dmic_txt[] = {
141 		"dmic disable",
142 		"dmic data at high level",
143 		"dmic data at low level",
144 };
145 static const unsigned int es8316_dmic_values[] = { 0, 1, 2 };
146 static const struct soc_enum es8316_dmic_src_enum =
147 	SOC_VALUE_ENUM_SINGLE(ES8316_ADC_DMIC, 0, 3,
148 			      ARRAY_SIZE(es8316_dmic_txt),
149 			      es8316_dmic_txt,
150 			      es8316_dmic_values);
151 static const struct snd_kcontrol_new es8316_dmic_src_controls =
152 	SOC_DAPM_ENUM("Route", es8316_dmic_src_enum);
153 
154 /* hp mixer mux */
155 static const char * const es8316_hpmux_texts[] = {
156 	"lin1-rin1",
157 	"lin2-rin2",
158 	"lin-rin with Boost",
159 	"lin-rin with Boost and PGA"
160 };
161 
162 static const unsigned int es8316_hpmux_values[] = { 0, 1, 2, 3 };
163 
164 static SOC_ENUM_SINGLE_DECL(es8316_left_hpmux_enum, ES8316_HPMIX_SEL,
165 	4, es8316_hpmux_texts);
166 
167 static const struct snd_kcontrol_new es8316_left_hpmux_controls =
168 	SOC_DAPM_ENUM("Route", es8316_left_hpmux_enum);
169 
170 static SOC_ENUM_SINGLE_DECL(es8316_right_hpmux_enum, ES8316_HPMIX_SEL,
171 	0, es8316_hpmux_texts);
172 
173 static const struct snd_kcontrol_new es8316_right_hpmux_controls =
174 	SOC_DAPM_ENUM("Route", es8316_right_hpmux_enum);
175 
176 /* headphone Output Mixer */
177 static const struct snd_kcontrol_new es8316_out_left_mix[] = {
178 	SOC_DAPM_SINGLE("LLIN Switch", ES8316_HPMIX_SWITCH, 6, 1, 0),
179 	SOC_DAPM_SINGLE("Left DAC Switch", ES8316_HPMIX_SWITCH, 7, 1, 0),
180 };
181 static const struct snd_kcontrol_new es8316_out_right_mix[] = {
182 	SOC_DAPM_SINGLE("RLIN Switch", ES8316_HPMIX_SWITCH, 2, 1, 0),
183 	SOC_DAPM_SINGLE("Right DAC Switch", ES8316_HPMIX_SWITCH, 3, 1, 0),
184 };
185 
186 /* DAC data source mux */
187 static const char * const es8316_dacsrc_texts[] = {
188 	"LDATA TO LDAC, RDATA TO RDAC",
189 	"LDATA TO LDAC, LDATA TO RDAC",
190 	"RDATA TO LDAC, RDATA TO RDAC",
191 	"RDATA TO LDAC, LDATA TO RDAC",
192 };
193 
194 static const unsigned int es8316_dacsrc_values[] = { 0, 1, 2, 3 };
195 
196 static SOC_ENUM_SINGLE_DECL(es8316_dacsrc_mux_enum, ES8316_DAC_SET1,
197 	6, es8316_dacsrc_texts);
198 
199 static const struct snd_kcontrol_new es8316_dacsrc_mux_controls =
200 	SOC_DAPM_ENUM("Route", es8316_dacsrc_mux_enum);
201 
202 static const struct snd_soc_dapm_widget es8316_dapm_widgets[] = {
203 	SND_SOC_DAPM_SUPPLY("Bias", ES8316_SYS_PDN, 3, 1, NULL, 0),
204 	SND_SOC_DAPM_SUPPLY("Analog power", ES8316_SYS_PDN, 4, 1, NULL, 0),
205 	SND_SOC_DAPM_SUPPLY("Mic Bias", ES8316_SYS_PDN, 5, 1, NULL, 0),
206 
207 	SND_SOC_DAPM_INPUT("DMIC"),
208 	SND_SOC_DAPM_INPUT("MIC1"),
209 	SND_SOC_DAPM_INPUT("MIC2"),
210 
211 	/* Input Mux */
212 	SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
213 			 &es8316_analog_in_mux_controls),
214 
215 	SND_SOC_DAPM_SUPPLY("ADC Vref", ES8316_SYS_PDN, 1, 1, NULL, 0),
216 	SND_SOC_DAPM_SUPPLY("ADC bias", ES8316_SYS_PDN, 2, 1, NULL, 0),
217 	SND_SOC_DAPM_SUPPLY("ADC Clock", ES8316_CLKMGR_CLKSW, 3, 0, NULL, 0),
218 	SND_SOC_DAPM_PGA("Line input PGA", ES8316_ADC_PDN_LINSEL,
219 			 7, 1, NULL, 0),
220 	SND_SOC_DAPM_ADC("Mono ADC", NULL, ES8316_ADC_PDN_LINSEL, 6, 1),
221 	SND_SOC_DAPM_MUX("Digital Mic Mux", SND_SOC_NOPM, 0, 0,
222 			 &es8316_dmic_src_controls),
223 
224 	/* Digital Interface */
225 	SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture",  1,
226 			     ES8316_SERDATA_ADC, 6, 1),
227 	SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0,
228 			    SND_SOC_NOPM, 0, 0),
229 
230 	SND_SOC_DAPM_MUX("DAC Source Mux", SND_SOC_NOPM, 0, 0,
231 			 &es8316_dacsrc_mux_controls),
232 
233 	SND_SOC_DAPM_SUPPLY("DAC Vref", ES8316_SYS_PDN, 0, 1, NULL, 0),
234 	SND_SOC_DAPM_SUPPLY("DAC Clock", ES8316_CLKMGR_CLKSW, 2, 0, NULL, 0),
235 	SND_SOC_DAPM_DAC("Right DAC", NULL, ES8316_DAC_PDN, 0, 1),
236 	SND_SOC_DAPM_DAC("Left DAC", NULL, ES8316_DAC_PDN, 4, 1),
237 
238 	/* Headphone Output Side */
239 	SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
240 			 &es8316_left_hpmux_controls),
241 	SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
242 			 &es8316_right_hpmux_controls),
243 	SND_SOC_DAPM_MIXER("Left Headphone Mixer", ES8316_HPMIX_PDN,
244 			   5, 1, &es8316_out_left_mix[0],
245 			   ARRAY_SIZE(es8316_out_left_mix)),
246 	SND_SOC_DAPM_MIXER("Right Headphone Mixer", ES8316_HPMIX_PDN,
247 			   1, 1, &es8316_out_right_mix[0],
248 			   ARRAY_SIZE(es8316_out_right_mix)),
249 	SND_SOC_DAPM_PGA("Left Headphone Mixer Out", ES8316_HPMIX_PDN,
250 			 4, 1, NULL, 0),
251 	SND_SOC_DAPM_PGA("Right Headphone Mixer Out", ES8316_HPMIX_PDN,
252 			 0, 1, NULL, 0),
253 
254 	SND_SOC_DAPM_OUT_DRV("Left Headphone Charge Pump", ES8316_CPHP_OUTEN,
255 			     6, 0, NULL, 0),
256 	SND_SOC_DAPM_OUT_DRV("Right Headphone Charge Pump", ES8316_CPHP_OUTEN,
257 			     2, 0, NULL, 0),
258 	SND_SOC_DAPM_SUPPLY("Headphone Charge Pump", ES8316_CPHP_PDN2,
259 			    5, 1, NULL, 0),
260 	SND_SOC_DAPM_SUPPLY("Headphone Charge Pump Clock", ES8316_CLKMGR_CLKSW,
261 			    4, 0, NULL, 0),
262 
263 	SND_SOC_DAPM_OUT_DRV("Left Headphone Driver", ES8316_CPHP_OUTEN,
264 			     5, 0, NULL, 0),
265 	SND_SOC_DAPM_OUT_DRV("Right Headphone Driver", ES8316_CPHP_OUTEN,
266 			     1, 0, NULL, 0),
267 	SND_SOC_DAPM_SUPPLY("Headphone Out", ES8316_CPHP_PDN1, 2, 1, NULL, 0),
268 
269 	/* pdn_Lical and pdn_Rical bits are documented as Reserved, but must
270 	 * be explicitly unset in order to enable HP output
271 	 */
272 	SND_SOC_DAPM_SUPPLY("Left Headphone ical", ES8316_CPHP_ICAL_VOL,
273 			    7, 1, NULL, 0),
274 	SND_SOC_DAPM_SUPPLY("Right Headphone ical", ES8316_CPHP_ICAL_VOL,
275 			    3, 1, NULL, 0),
276 
277 	SND_SOC_DAPM_OUTPUT("HPOL"),
278 	SND_SOC_DAPM_OUTPUT("HPOR"),
279 };
280 
281 static const struct snd_soc_dapm_route es8316_dapm_routes[] = {
282 	/* Recording */
283 	{"MIC1", NULL, "Mic Bias"},
284 	{"MIC2", NULL, "Mic Bias"},
285 	{"MIC1", NULL, "Bias"},
286 	{"MIC2", NULL, "Bias"},
287 	{"MIC1", NULL, "Analog power"},
288 	{"MIC2", NULL, "Analog power"},
289 
290 	{"Differential Mux", "lin1-rin1", "MIC1"},
291 	{"Differential Mux", "lin2-rin2", "MIC2"},
292 	{"Line input PGA", NULL, "Differential Mux"},
293 
294 	{"Mono ADC", NULL, "ADC Clock"},
295 	{"Mono ADC", NULL, "ADC Vref"},
296 	{"Mono ADC", NULL, "ADC bias"},
297 	{"Mono ADC", NULL, "Line input PGA"},
298 
299 	/* It's not clear why, but to avoid recording only silence,
300 	 * the DAC clock must be running for the ADC to work.
301 	 */
302 	{"Mono ADC", NULL, "DAC Clock"},
303 
304 	{"Digital Mic Mux", "dmic disable", "Mono ADC"},
305 
306 	{"I2S OUT", NULL, "Digital Mic Mux"},
307 
308 	/* Playback */
309 	{"DAC Source Mux", "LDATA TO LDAC, RDATA TO RDAC", "I2S IN"},
310 
311 	{"Left DAC", NULL, "DAC Clock"},
312 	{"Right DAC", NULL, "DAC Clock"},
313 
314 	{"Left DAC", NULL, "DAC Vref"},
315 	{"Right DAC", NULL, "DAC Vref"},
316 
317 	{"Left DAC", NULL, "DAC Source Mux"},
318 	{"Right DAC", NULL, "DAC Source Mux"},
319 
320 	{"Left Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
321 	{"Right Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
322 
323 	{"Left Headphone Mixer", "LLIN Switch", "Left Headphone Mux"},
324 	{"Left Headphone Mixer", "Left DAC Switch", "Left DAC"},
325 
326 	{"Right Headphone Mixer", "RLIN Switch", "Right Headphone Mux"},
327 	{"Right Headphone Mixer", "Right DAC Switch", "Right DAC"},
328 
329 	{"Left Headphone Mixer Out", NULL, "Left Headphone Mixer"},
330 	{"Right Headphone Mixer Out", NULL, "Right Headphone Mixer"},
331 
332 	{"Left Headphone Charge Pump", NULL, "Left Headphone Mixer Out"},
333 	{"Right Headphone Charge Pump", NULL, "Right Headphone Mixer Out"},
334 
335 	{"Left Headphone Charge Pump", NULL, "Headphone Charge Pump"},
336 	{"Right Headphone Charge Pump", NULL, "Headphone Charge Pump"},
337 
338 	{"Left Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
339 	{"Right Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
340 
341 	{"Left Headphone Driver", NULL, "Left Headphone Charge Pump"},
342 	{"Right Headphone Driver", NULL, "Right Headphone Charge Pump"},
343 
344 	{"HPOL", NULL, "Left Headphone Driver"},
345 	{"HPOR", NULL, "Right Headphone Driver"},
346 
347 	{"HPOL", NULL, "Left Headphone ical"},
348 	{"HPOR", NULL, "Right Headphone ical"},
349 
350 	{"Headphone Out", NULL, "Bias"},
351 	{"Headphone Out", NULL, "Analog power"},
352 	{"HPOL", NULL, "Headphone Out"},
353 	{"HPOR", NULL, "Headphone Out"},
354 };
355 
356 static int es8316_set_dai_sysclk(struct snd_soc_dai *codec_dai,
357 				 int clk_id, unsigned int freq, int dir)
358 {
359 	struct snd_soc_component *component = codec_dai->component;
360 	struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
361 	int i;
362 	int count = 0;
363 
364 	es8316->sysclk = freq;
365 
366 	if (freq == 0)
367 		return 0;
368 
369 	/* Limit supported sample rates to ones that can be autodetected
370 	 * by the codec running in slave mode.
371 	 */
372 	for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
373 		const unsigned int ratio = supported_mclk_lrck_ratios[i];
374 
375 		if (freq % ratio == 0)
376 			es8316->allowed_rates[count++] = freq / ratio;
377 	}
378 
379 	es8316->sysclk_constraints.list = es8316->allowed_rates;
380 	es8316->sysclk_constraints.count = count;
381 
382 	return 0;
383 }
384 
385 static int es8316_set_dai_fmt(struct snd_soc_dai *codec_dai,
386 			      unsigned int fmt)
387 {
388 	struct snd_soc_component *component = codec_dai->component;
389 	u8 serdata1 = 0;
390 	u8 serdata2 = 0;
391 	u8 clksw;
392 	u8 mask;
393 
394 	if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) {
395 		dev_err(component->dev, "Codec driver only supports slave mode\n");
396 		return -EINVAL;
397 	}
398 
399 	if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S) {
400 		dev_err(component->dev, "Codec driver only supports I2S format\n");
401 		return -EINVAL;
402 	}
403 
404 	/* Clock inversion */
405 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
406 	case SND_SOC_DAIFMT_NB_NF:
407 		break;
408 	case SND_SOC_DAIFMT_IB_IF:
409 		serdata1 |= ES8316_SERDATA1_BCLK_INV;
410 		serdata2 |= ES8316_SERDATA2_ADCLRP;
411 		break;
412 	case SND_SOC_DAIFMT_IB_NF:
413 		serdata1 |= ES8316_SERDATA1_BCLK_INV;
414 		break;
415 	case SND_SOC_DAIFMT_NB_IF:
416 		serdata2 |= ES8316_SERDATA2_ADCLRP;
417 		break;
418 	default:
419 		return -EINVAL;
420 	}
421 
422 	mask = ES8316_SERDATA1_MASTER | ES8316_SERDATA1_BCLK_INV;
423 	snd_soc_component_update_bits(component, ES8316_SERDATA1, mask, serdata1);
424 
425 	mask = ES8316_SERDATA2_FMT_MASK | ES8316_SERDATA2_ADCLRP;
426 	snd_soc_component_update_bits(component, ES8316_SERDATA_ADC, mask, serdata2);
427 	snd_soc_component_update_bits(component, ES8316_SERDATA_DAC, mask, serdata2);
428 
429 	/* Enable BCLK and MCLK inputs in slave mode */
430 	clksw = ES8316_CLKMGR_CLKSW_MCLK_ON | ES8316_CLKMGR_CLKSW_BCLK_ON;
431 	snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW, clksw, clksw);
432 
433 	return 0;
434 }
435 
436 static int es8316_pcm_startup(struct snd_pcm_substream *substream,
437 			      struct snd_soc_dai *dai)
438 {
439 	struct snd_soc_component *component = dai->component;
440 	struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
441 
442 	if (es8316->sysclk == 0) {
443 		dev_err(component->dev, "No sysclk provided\n");
444 		return -EINVAL;
445 	}
446 
447 	/* The set of sample rates that can be supported depends on the
448 	 * MCLK supplied to the CODEC.
449 	 */
450 	snd_pcm_hw_constraint_list(substream->runtime, 0,
451 				   SNDRV_PCM_HW_PARAM_RATE,
452 				   &es8316->sysclk_constraints);
453 
454 	return 0;
455 }
456 
457 static int es8316_pcm_hw_params(struct snd_pcm_substream *substream,
458 				struct snd_pcm_hw_params *params,
459 				struct snd_soc_dai *dai)
460 {
461 	struct snd_soc_component *component = dai->component;
462 	struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
463 	u8 wordlen = 0;
464 
465 	if (!es8316->sysclk) {
466 		dev_err(component->dev, "No MCLK configured\n");
467 		return -EINVAL;
468 	}
469 
470 	switch (params_format(params)) {
471 	case SNDRV_PCM_FORMAT_S16_LE:
472 		wordlen = ES8316_SERDATA2_LEN_16;
473 		break;
474 	case SNDRV_PCM_FORMAT_S20_3LE:
475 		wordlen = ES8316_SERDATA2_LEN_20;
476 		break;
477 	case SNDRV_PCM_FORMAT_S24_LE:
478 		wordlen = ES8316_SERDATA2_LEN_24;
479 		break;
480 	case SNDRV_PCM_FORMAT_S32_LE:
481 		wordlen = ES8316_SERDATA2_LEN_32;
482 		break;
483 	default:
484 		return -EINVAL;
485 	}
486 
487 	snd_soc_component_update_bits(component, ES8316_SERDATA_DAC,
488 			    ES8316_SERDATA2_LEN_MASK, wordlen);
489 	snd_soc_component_update_bits(component, ES8316_SERDATA_ADC,
490 			    ES8316_SERDATA2_LEN_MASK, wordlen);
491 	return 0;
492 }
493 
494 static int es8316_mute(struct snd_soc_dai *dai, int mute)
495 {
496 	snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20,
497 			    mute ? 0x20 : 0);
498 	return 0;
499 }
500 
501 #define ES8316_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
502 			SNDRV_PCM_FMTBIT_S24_LE)
503 
504 static const struct snd_soc_dai_ops es8316_ops = {
505 	.startup = es8316_pcm_startup,
506 	.hw_params = es8316_pcm_hw_params,
507 	.set_fmt = es8316_set_dai_fmt,
508 	.set_sysclk = es8316_set_dai_sysclk,
509 	.digital_mute = es8316_mute,
510 };
511 
512 static struct snd_soc_dai_driver es8316_dai = {
513 	.name = "ES8316 HiFi",
514 	.playback = {
515 		.stream_name = "Playback",
516 		.channels_min = 1,
517 		.channels_max = 2,
518 		.rates = SNDRV_PCM_RATE_8000_48000,
519 		.formats = ES8316_FORMATS,
520 	},
521 	.capture = {
522 		.stream_name = "Capture",
523 		.channels_min = 1,
524 		.channels_max = 2,
525 		.rates = SNDRV_PCM_RATE_8000_48000,
526 		.formats = ES8316_FORMATS,
527 	},
528 	.ops = &es8316_ops,
529 	.symmetric_rates = 1,
530 };
531 
532 static int es8316_probe(struct snd_soc_component *component)
533 {
534 	/* Reset codec and enable current state machine */
535 	snd_soc_component_write(component, ES8316_RESET, 0x3f);
536 	usleep_range(5000, 5500);
537 	snd_soc_component_write(component, ES8316_RESET, ES8316_RESET_CSM_ON);
538 	msleep(30);
539 
540 	/*
541 	 * Documentation is unclear, but this value from the vendor driver is
542 	 * needed otherwise audio output is silent.
543 	 */
544 	snd_soc_component_write(component, ES8316_SYS_VMIDSEL, 0xff);
545 
546 	/*
547 	 * Documentation for this register is unclear and incomplete,
548 	 * but here is a vendor-provided value that improves volume
549 	 * and quality for Intel CHT platforms.
550 	 */
551 	snd_soc_component_write(component, ES8316_CLKMGR_ADCOSR, 0x32);
552 
553 	return 0;
554 }
555 
556 static const struct snd_soc_component_driver soc_component_dev_es8316 = {
557 	.probe			= es8316_probe,
558 	.controls		= es8316_snd_controls,
559 	.num_controls		= ARRAY_SIZE(es8316_snd_controls),
560 	.dapm_widgets		= es8316_dapm_widgets,
561 	.num_dapm_widgets	= ARRAY_SIZE(es8316_dapm_widgets),
562 	.dapm_routes		= es8316_dapm_routes,
563 	.num_dapm_routes	= ARRAY_SIZE(es8316_dapm_routes),
564 	.use_pmdown_time	= 1,
565 	.endianness		= 1,
566 	.non_legacy_dai_naming	= 1,
567 };
568 
569 static const struct regmap_config es8316_regmap = {
570 	.reg_bits = 8,
571 	.val_bits = 8,
572 	.max_register = 0x53,
573 	.cache_type = REGCACHE_RBTREE,
574 };
575 
576 static int es8316_i2c_probe(struct i2c_client *i2c_client,
577 			    const struct i2c_device_id *id)
578 {
579 	struct es8316_priv *es8316;
580 	struct regmap *regmap;
581 
582 	es8316 = devm_kzalloc(&i2c_client->dev, sizeof(struct es8316_priv),
583 			      GFP_KERNEL);
584 	if (es8316 == NULL)
585 		return -ENOMEM;
586 
587 	i2c_set_clientdata(i2c_client, es8316);
588 
589 	regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap);
590 	if (IS_ERR(regmap))
591 		return PTR_ERR(regmap);
592 
593 	return devm_snd_soc_register_component(&i2c_client->dev,
594 				      &soc_component_dev_es8316,
595 				      &es8316_dai, 1);
596 }
597 
598 static const struct i2c_device_id es8316_i2c_id[] = {
599 	{"es8316", 0 },
600 	{}
601 };
602 MODULE_DEVICE_TABLE(i2c, es8316_i2c_id);
603 
604 static const struct of_device_id es8316_of_match[] = {
605 	{ .compatible = "everest,es8316", },
606 	{},
607 };
608 MODULE_DEVICE_TABLE(of, es8316_of_match);
609 
610 static const struct acpi_device_id es8316_acpi_match[] = {
611 	{"ESSX8316", 0},
612 	{},
613 };
614 MODULE_DEVICE_TABLE(acpi, es8316_acpi_match);
615 
616 static struct i2c_driver es8316_i2c_driver = {
617 	.driver = {
618 		.name			= "es8316",
619 		.acpi_match_table	= ACPI_PTR(es8316_acpi_match),
620 		.of_match_table		= of_match_ptr(es8316_of_match),
621 	},
622 	.probe		= es8316_i2c_probe,
623 	.id_table	= es8316_i2c_id,
624 };
625 module_i2c_driver(es8316_i2c_driver);
626 
627 MODULE_DESCRIPTION("Everest Semi ES8316 ALSA SoC Codec Driver");
628 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
629 MODULE_LICENSE("GPL v2");
630