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