1 // SPDX-License-Identifier: GPL-2.0-only
3 * es8316.c -- es8316 ALSA SoC audio driver
4 * Copyright Everest Semiconductor Co.,Ltd
6 * Authors: David Yang <yangxiaohua@everest-semi.com>,
7 * Daniel Drake <drake@endlessm.com>
10 #include <linux/module.h>
11 #include <linux/acpi.h>
12 #include <linux/delay.h>
13 #include <linux/i2c.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/mutex.h>
16 #include <linux/regmap.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 #include <sound/soc-dapm.h>
21 #include <sound/tlv.h>
22 #include <sound/jack.h>
25 /* In slave mode at single speed, the codec is documented as accepting 5
26 * MCLK/LRCK ratios, but we also add ratio 400, which is commonly used on
27 * Intel Cherry Trail platforms (19.2MHz MCLK, 48kHz LRCK).
29 #define NR_SUPPORTED_MCLK_LRCK_RATIOS 6
30 static const unsigned int supported_mclk_lrck_ratios[] = {
31 256, 384, 400, 512, 768, 1024
36 struct regmap *regmap;
37 struct snd_soc_component *component;
38 struct snd_soc_jack *jack;
41 unsigned int allowed_rates[NR_SUPPORTED_MCLK_LRCK_RATIOS];
42 struct snd_pcm_hw_constraint_list sysclk_constraints;
49 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9600, 50, 1);
50 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9600, 50, 1);
51 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_max_gain_tlv, -650, 150, 0);
52 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_min_gain_tlv, -1200, 150, 0);
53 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_target_tlv, -1650, 150, 0);
54 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(hpmixer_gain_tlv, -1200, 150, 0);
56 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(adc_pga_gain_tlv,
57 0, 0, TLV_DB_SCALE_ITEM(-350, 0, 0),
58 1, 1, TLV_DB_SCALE_ITEM(0, 0, 0),
59 2, 2, TLV_DB_SCALE_ITEM(250, 0, 0),
60 3, 3, TLV_DB_SCALE_ITEM(450, 0, 0),
61 4, 4, TLV_DB_SCALE_ITEM(700, 0, 0),
62 5, 5, TLV_DB_SCALE_ITEM(1000, 0, 0),
63 6, 6, TLV_DB_SCALE_ITEM(1300, 0, 0),
64 7, 7, TLV_DB_SCALE_ITEM(1600, 0, 0),
65 8, 8, TLV_DB_SCALE_ITEM(1800, 0, 0),
66 9, 9, TLV_DB_SCALE_ITEM(2100, 0, 0),
67 10, 10, TLV_DB_SCALE_ITEM(2400, 0, 0),
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),
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);
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);
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 0, 4, 7, 0, hpmixer_gain_tlv),
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),
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),
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,
116 SOC_SINGLE_TLV("ALC Capture Min Volume", ES8316_ADC_ALC2, 0, 28, 0,
118 SOC_SINGLE_TLV("ALC Capture Target Volume", ES8316_ADC_ALC3, 4, 10, 0,
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,
125 SOC_SINGLE("ALC Capture Noise Gate Threshold", ES8316_ADC_ALC_NG,
127 SOC_ENUM("ALC Capture Noise Gate Type", ng_type),
130 /* Analog Input Mux */
131 static const char * const es8316_analog_in_txt[] = {
134 "lin1-rin1 with 20db Boost",
135 "lin2-rin2 with 20db Boost"
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);
146 static const char * const es8316_dmic_txt[] = {
148 "dmic data at high level",
149 "dmic data at low level",
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),
157 static const struct snd_kcontrol_new es8316_dmic_src_controls =
158 SOC_DAPM_ENUM("Route", es8316_dmic_src_enum);
161 static const char * const es8316_hpmux_texts[] = {
164 "lin-rin with Boost",
165 "lin-rin with Boost and PGA"
168 static SOC_ENUM_SINGLE_DECL(es8316_left_hpmux_enum, ES8316_HPMIX_SEL,
169 4, es8316_hpmux_texts);
171 static const struct snd_kcontrol_new es8316_left_hpmux_controls =
172 SOC_DAPM_ENUM("Route", es8316_left_hpmux_enum);
174 static SOC_ENUM_SINGLE_DECL(es8316_right_hpmux_enum, ES8316_HPMIX_SEL,
175 0, es8316_hpmux_texts);
177 static const struct snd_kcontrol_new es8316_right_hpmux_controls =
178 SOC_DAPM_ENUM("Route", es8316_right_hpmux_enum);
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),
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),
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",
198 static SOC_ENUM_SINGLE_DECL(es8316_dacsrc_mux_enum, ES8316_DAC_SET1,
199 6, es8316_dacsrc_texts);
201 static const struct snd_kcontrol_new es8316_dacsrc_mux_controls =
202 SOC_DAPM_ENUM("Route", es8316_dacsrc_mux_enum);
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),
209 SND_SOC_DAPM_INPUT("DMIC"),
210 SND_SOC_DAPM_INPUT("MIC1"),
211 SND_SOC_DAPM_INPUT("MIC2"),
214 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
215 &es8316_analog_in_mux_controls),
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,
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),
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,
232 SND_SOC_DAPM_MUX("DAC Source Mux", SND_SOC_NOPM, 0, 0,
233 &es8316_dacsrc_mux_controls),
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),
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,
253 SND_SOC_DAPM_PGA("Right Headphone Mixer Out", ES8316_HPMIX_PDN,
256 SND_SOC_DAPM_OUT_DRV("Left Headphone Charge Pump", ES8316_CPHP_OUTEN,
258 SND_SOC_DAPM_OUT_DRV("Right Headphone Charge Pump", ES8316_CPHP_OUTEN,
260 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump", ES8316_CPHP_PDN2,
262 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump Clock", ES8316_CLKMGR_CLKSW,
265 SND_SOC_DAPM_OUT_DRV("Left Headphone Driver", ES8316_CPHP_OUTEN,
267 SND_SOC_DAPM_OUT_DRV("Right Headphone Driver", ES8316_CPHP_OUTEN,
269 SND_SOC_DAPM_SUPPLY("Headphone Out", ES8316_CPHP_PDN1, 2, 1, NULL, 0),
271 /* pdn_Lical and pdn_Rical bits are documented as Reserved, but must
272 * be explicitly unset in order to enable HP output
274 SND_SOC_DAPM_SUPPLY("Left Headphone ical", ES8316_CPHP_ICAL_VOL,
276 SND_SOC_DAPM_SUPPLY("Right Headphone ical", ES8316_CPHP_ICAL_VOL,
279 SND_SOC_DAPM_OUTPUT("HPOL"),
280 SND_SOC_DAPM_OUTPUT("HPOR"),
283 static const struct snd_soc_dapm_route es8316_dapm_routes[] = {
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"},
292 {"Differential Mux", "lin1-rin1", "MIC1"},
293 {"Differential Mux", "lin2-rin2", "MIC2"},
294 {"Line input PGA", NULL, "Differential Mux"},
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"},
301 /* It's not clear why, but to avoid recording only silence,
302 * the DAC clock must be running for the ADC to work.
304 {"Mono ADC", NULL, "DAC Clock"},
306 {"Digital Mic Mux", "dmic disable", "Mono ADC"},
308 {"I2S OUT", NULL, "Digital Mic Mux"},
311 {"DAC Source Mux", "LDATA TO LDAC, RDATA TO RDAC", "I2S IN"},
313 {"Left DAC", NULL, "DAC Clock"},
314 {"Right DAC", NULL, "DAC Clock"},
316 {"Left DAC", NULL, "DAC Vref"},
317 {"Right DAC", NULL, "DAC Vref"},
319 {"Left DAC", NULL, "DAC Source Mux"},
320 {"Right DAC", NULL, "DAC Source Mux"},
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"},
325 {"Left Headphone Mixer", "LLIN Switch", "Left Headphone Mux"},
326 {"Left Headphone Mixer", "Left DAC Switch", "Left DAC"},
328 {"Right Headphone Mixer", "RLIN Switch", "Right Headphone Mux"},
329 {"Right Headphone Mixer", "Right DAC Switch", "Right DAC"},
331 {"Left Headphone Mixer Out", NULL, "Left Headphone Mixer"},
332 {"Right Headphone Mixer Out", NULL, "Right Headphone Mixer"},
334 {"Left Headphone Charge Pump", NULL, "Left Headphone Mixer Out"},
335 {"Right Headphone Charge Pump", NULL, "Right Headphone Mixer Out"},
337 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump"},
338 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump"},
340 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
341 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
343 {"Left Headphone Driver", NULL, "Left Headphone Charge Pump"},
344 {"Right Headphone Driver", NULL, "Right Headphone Charge Pump"},
346 {"HPOL", NULL, "Left Headphone Driver"},
347 {"HPOR", NULL, "Right Headphone Driver"},
349 {"HPOL", NULL, "Left Headphone ical"},
350 {"HPOR", NULL, "Right Headphone ical"},
352 {"Headphone Out", NULL, "Bias"},
353 {"Headphone Out", NULL, "Analog power"},
354 {"HPOL", NULL, "Headphone Out"},
355 {"HPOR", NULL, "Headphone Out"},
358 static int es8316_set_dai_sysclk(struct snd_soc_dai *codec_dai,
359 int clk_id, unsigned int freq, int dir)
361 struct snd_soc_component *component = codec_dai->component;
362 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
366 es8316->sysclk = freq;
371 /* Limit supported sample rates to ones that can be autodetected
372 * by the codec running in slave mode.
374 for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
375 const unsigned int ratio = supported_mclk_lrck_ratios[i];
377 if (freq % ratio == 0)
378 es8316->allowed_rates[count++] = freq / ratio;
381 es8316->sysclk_constraints.list = es8316->allowed_rates;
382 es8316->sysclk_constraints.count = count;
387 static int es8316_set_dai_fmt(struct snd_soc_dai *codec_dai,
390 struct snd_soc_component *component = codec_dai->component;
396 if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) {
397 dev_err(component->dev, "Codec driver only supports slave mode\n");
401 if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S) {
402 dev_err(component->dev, "Codec driver only supports I2S format\n");
406 /* Clock inversion */
407 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
408 case SND_SOC_DAIFMT_NB_NF:
410 case SND_SOC_DAIFMT_IB_IF:
411 serdata1 |= ES8316_SERDATA1_BCLK_INV;
412 serdata2 |= ES8316_SERDATA2_ADCLRP;
414 case SND_SOC_DAIFMT_IB_NF:
415 serdata1 |= ES8316_SERDATA1_BCLK_INV;
417 case SND_SOC_DAIFMT_NB_IF:
418 serdata2 |= ES8316_SERDATA2_ADCLRP;
424 mask = ES8316_SERDATA1_MASTER | ES8316_SERDATA1_BCLK_INV;
425 snd_soc_component_update_bits(component, ES8316_SERDATA1, mask, serdata1);
427 mask = ES8316_SERDATA2_FMT_MASK | ES8316_SERDATA2_ADCLRP;
428 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC, mask, serdata2);
429 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC, mask, serdata2);
431 /* Enable BCLK and MCLK inputs in slave mode */
432 clksw = ES8316_CLKMGR_CLKSW_MCLK_ON | ES8316_CLKMGR_CLKSW_BCLK_ON;
433 snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW, clksw, clksw);
438 static int es8316_pcm_startup(struct snd_pcm_substream *substream,
439 struct snd_soc_dai *dai)
441 struct snd_soc_component *component = dai->component;
442 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
444 if (es8316->sysclk == 0) {
445 dev_err(component->dev, "No sysclk provided\n");
449 /* The set of sample rates that can be supported depends on the
450 * MCLK supplied to the CODEC.
452 snd_pcm_hw_constraint_list(substream->runtime, 0,
453 SNDRV_PCM_HW_PARAM_RATE,
454 &es8316->sysclk_constraints);
459 static int es8316_pcm_hw_params(struct snd_pcm_substream *substream,
460 struct snd_pcm_hw_params *params,
461 struct snd_soc_dai *dai)
463 struct snd_soc_component *component = dai->component;
464 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
467 if (!es8316->sysclk) {
468 dev_err(component->dev, "No MCLK configured\n");
472 switch (params_format(params)) {
473 case SNDRV_PCM_FORMAT_S16_LE:
474 wordlen = ES8316_SERDATA2_LEN_16;
476 case SNDRV_PCM_FORMAT_S20_3LE:
477 wordlen = ES8316_SERDATA2_LEN_20;
479 case SNDRV_PCM_FORMAT_S24_LE:
480 wordlen = ES8316_SERDATA2_LEN_24;
482 case SNDRV_PCM_FORMAT_S32_LE:
483 wordlen = ES8316_SERDATA2_LEN_32;
489 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC,
490 ES8316_SERDATA2_LEN_MASK, wordlen);
491 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC,
492 ES8316_SERDATA2_LEN_MASK, wordlen);
496 static int es8316_mute(struct snd_soc_dai *dai, int mute)
498 snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20,
503 #define ES8316_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
504 SNDRV_PCM_FMTBIT_S24_LE)
506 static const struct snd_soc_dai_ops es8316_ops = {
507 .startup = es8316_pcm_startup,
508 .hw_params = es8316_pcm_hw_params,
509 .set_fmt = es8316_set_dai_fmt,
510 .set_sysclk = es8316_set_dai_sysclk,
511 .digital_mute = es8316_mute,
514 static struct snd_soc_dai_driver es8316_dai = {
515 .name = "ES8316 HiFi",
517 .stream_name = "Playback",
520 .rates = SNDRV_PCM_RATE_8000_48000,
521 .formats = ES8316_FORMATS,
524 .stream_name = "Capture",
527 .rates = SNDRV_PCM_RATE_8000_48000,
528 .formats = ES8316_FORMATS,
531 .symmetric_rates = 1,
534 static void es8316_enable_micbias_for_mic_gnd_short_detect(
535 struct snd_soc_component *component)
537 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
539 snd_soc_dapm_mutex_lock(dapm);
540 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Bias");
541 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Analog power");
542 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Mic Bias");
543 snd_soc_dapm_sync_unlocked(dapm);
544 snd_soc_dapm_mutex_unlock(dapm);
549 static void es8316_disable_micbias_for_mic_gnd_short_detect(
550 struct snd_soc_component *component)
552 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
554 snd_soc_dapm_mutex_lock(dapm);
555 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Bias");
556 snd_soc_dapm_disable_pin_unlocked(dapm, "Analog power");
557 snd_soc_dapm_disable_pin_unlocked(dapm, "Bias");
558 snd_soc_dapm_sync_unlocked(dapm);
559 snd_soc_dapm_mutex_unlock(dapm);
562 static irqreturn_t es8316_irq(int irq, void *data)
564 struct es8316_priv *es8316 = data;
565 struct snd_soc_component *comp = es8316->component;
568 mutex_lock(&es8316->lock);
570 regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
572 goto out; /* Powered-down / reset */
574 /* Catch spurious IRQ before set_jack is called */
578 if (es8316->jd_inverted)
579 flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
581 dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
582 if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
583 /* Jack removed, or spurious IRQ? */
584 if (es8316->jack->status & SND_JACK_MICROPHONE)
585 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
587 if (es8316->jack->status & SND_JACK_HEADPHONE) {
588 snd_soc_jack_report(es8316->jack, 0,
589 SND_JACK_HEADSET | SND_JACK_BTN_0);
590 dev_dbg(comp->dev, "jack unplugged\n");
592 } else if (!(es8316->jack->status & SND_JACK_HEADPHONE)) {
593 /* Jack inserted, determine type */
594 es8316_enable_micbias_for_mic_gnd_short_detect(comp);
595 regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
596 if (es8316->jd_inverted)
597 flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
598 dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
599 if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
600 /* Jack unplugged underneath us */
601 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
602 } else if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
604 snd_soc_jack_report(es8316->jack,
607 /* Keep mic-gnd-short detection on for button press */
609 /* Shorted, headphones */
610 snd_soc_jack_report(es8316->jack,
613 /* No longer need mic-gnd-short detection */
614 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
616 } else if (es8316->jack->status & SND_JACK_MICROPHONE) {
617 /* Interrupt while jack inserted, report button state */
618 if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
619 /* Open, button release */
620 snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
622 /* Short, button press */
623 snd_soc_jack_report(es8316->jack,
630 mutex_unlock(&es8316->lock);
634 static void es8316_enable_jack_detect(struct snd_soc_component *component,
635 struct snd_soc_jack *jack)
637 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
640 * Init es8316->jd_inverted here and not in the probe, as we cannot
641 * guarantee that the bytchr-es8316 driver, which might set this
642 * property, will probe before us.
644 es8316->jd_inverted = device_property_read_bool(component->dev,
645 "everest,jack-detect-inverted");
647 mutex_lock(&es8316->lock);
651 if (es8316->jack->status & SND_JACK_MICROPHONE)
652 es8316_enable_micbias_for_mic_gnd_short_detect(component);
654 snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
655 ES8316_GPIO_ENABLE_INTERRUPT,
656 ES8316_GPIO_ENABLE_INTERRUPT);
658 mutex_unlock(&es8316->lock);
660 /* Enable irq and sync initial jack state */
661 enable_irq(es8316->irq);
662 es8316_irq(es8316->irq, es8316);
665 static void es8316_disable_jack_detect(struct snd_soc_component *component)
667 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
669 disable_irq(es8316->irq);
671 mutex_lock(&es8316->lock);
673 snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
674 ES8316_GPIO_ENABLE_INTERRUPT, 0);
676 if (es8316->jack->status & SND_JACK_MICROPHONE) {
677 es8316_disable_micbias_for_mic_gnd_short_detect(component);
678 snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
683 mutex_unlock(&es8316->lock);
686 static int es8316_set_jack(struct snd_soc_component *component,
687 struct snd_soc_jack *jack, void *data)
690 es8316_enable_jack_detect(component, jack);
692 es8316_disable_jack_detect(component);
697 static int es8316_probe(struct snd_soc_component *component)
699 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
701 es8316->component = component;
703 /* Reset codec and enable current state machine */
704 snd_soc_component_write(component, ES8316_RESET, 0x3f);
705 usleep_range(5000, 5500);
706 snd_soc_component_write(component, ES8316_RESET, ES8316_RESET_CSM_ON);
710 * Documentation is unclear, but this value from the vendor driver is
711 * needed otherwise audio output is silent.
713 snd_soc_component_write(component, ES8316_SYS_VMIDSEL, 0xff);
716 * Documentation for this register is unclear and incomplete,
717 * but here is a vendor-provided value that improves volume
718 * and quality for Intel CHT platforms.
720 snd_soc_component_write(component, ES8316_CLKMGR_ADCOSR, 0x32);
725 static const struct snd_soc_component_driver soc_component_dev_es8316 = {
726 .probe = es8316_probe,
727 .set_jack = es8316_set_jack,
728 .controls = es8316_snd_controls,
729 .num_controls = ARRAY_SIZE(es8316_snd_controls),
730 .dapm_widgets = es8316_dapm_widgets,
731 .num_dapm_widgets = ARRAY_SIZE(es8316_dapm_widgets),
732 .dapm_routes = es8316_dapm_routes,
733 .num_dapm_routes = ARRAY_SIZE(es8316_dapm_routes),
734 .use_pmdown_time = 1,
736 .non_legacy_dai_naming = 1,
739 static const struct regmap_range es8316_volatile_ranges[] = {
740 regmap_reg_range(ES8316_GPIO_FLAG, ES8316_GPIO_FLAG),
743 static const struct regmap_access_table es8316_volatile_table = {
744 .yes_ranges = es8316_volatile_ranges,
745 .n_yes_ranges = ARRAY_SIZE(es8316_volatile_ranges),
748 static const struct regmap_config es8316_regmap = {
751 .max_register = 0x53,
752 .volatile_table = &es8316_volatile_table,
753 .cache_type = REGCACHE_RBTREE,
756 static int es8316_i2c_probe(struct i2c_client *i2c_client,
757 const struct i2c_device_id *id)
759 struct device *dev = &i2c_client->dev;
760 struct es8316_priv *es8316;
763 es8316 = devm_kzalloc(&i2c_client->dev, sizeof(struct es8316_priv),
768 i2c_set_clientdata(i2c_client, es8316);
770 es8316->regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap);
771 if (IS_ERR(es8316->regmap))
772 return PTR_ERR(es8316->regmap);
774 es8316->irq = i2c_client->irq;
775 mutex_init(&es8316->lock);
777 ret = devm_request_threaded_irq(dev, es8316->irq, NULL, es8316_irq,
778 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
781 /* Gets re-enabled by es8316_set_jack() */
782 disable_irq(es8316->irq);
784 dev_warn(dev, "Failed to get IRQ %d: %d\n", es8316->irq, ret);
785 es8316->irq = -ENXIO;
788 return devm_snd_soc_register_component(&i2c_client->dev,
789 &soc_component_dev_es8316,
793 static const struct i2c_device_id es8316_i2c_id[] = {
797 MODULE_DEVICE_TABLE(i2c, es8316_i2c_id);
799 static const struct of_device_id es8316_of_match[] = {
800 { .compatible = "everest,es8316", },
803 MODULE_DEVICE_TABLE(of, es8316_of_match);
805 static const struct acpi_device_id es8316_acpi_match[] = {
809 MODULE_DEVICE_TABLE(acpi, es8316_acpi_match);
811 static struct i2c_driver es8316_i2c_driver = {
814 .acpi_match_table = ACPI_PTR(es8316_acpi_match),
815 .of_match_table = of_match_ptr(es8316_of_match),
817 .probe = es8316_i2c_probe,
818 .id_table = es8316_i2c_id,
820 module_i2c_driver(es8316_i2c_driver);
822 MODULE_DESCRIPTION("Everest Semi ES8316 ALSA SoC Codec Driver");
823 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
824 MODULE_LICENSE("GPL v2");