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/clk.h>
13 #include <linux/delay.h>
14 #include <linux/i2c.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/mutex.h>
17 #include <linux/regmap.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/soc-dapm.h>
22 #include <sound/tlv.h>
23 #include <sound/jack.h>
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).
30 #define NR_SUPPORTED_MCLK_LRCK_RATIOS ARRAY_SIZE(supported_mclk_lrck_ratios)
31 static const unsigned int supported_mclk_lrck_ratios[] = {
32 256, 384, 400, 500, 512, 768, 1024
38 struct regmap *regmap;
39 struct snd_soc_component *component;
40 struct snd_soc_jack *jack;
43 unsigned int allowed_rates[NR_SUPPORTED_MCLK_LRCK_RATIOS];
44 struct snd_pcm_hw_constraint_list sysclk_constraints;
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);
56 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(alc_target_tlv,
57 0, 10, TLV_DB_SCALE_ITEM(-1650, 150, 0),
58 11, 11, TLV_DB_SCALE_ITEM(-150, 0, 0),
61 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpmixer_gain_tlv,
62 0, 4, TLV_DB_SCALE_ITEM(-1200, 150, 0),
63 8, 11, TLV_DB_SCALE_ITEM(-450, 150, 0),
66 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(adc_pga_gain_tlv,
67 0, 0, TLV_DB_SCALE_ITEM(-350, 0, 0),
68 1, 1, TLV_DB_SCALE_ITEM(0, 0, 0),
69 2, 2, TLV_DB_SCALE_ITEM(250, 0, 0),
70 3, 3, TLV_DB_SCALE_ITEM(450, 0, 0),
71 4, 7, TLV_DB_SCALE_ITEM(700, 300, 0),
72 8, 10, TLV_DB_SCALE_ITEM(1800, 300, 0),
75 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpout_vol_tlv,
76 0, 0, TLV_DB_SCALE_ITEM(-4800, 0, 0),
77 1, 3, TLV_DB_SCALE_ITEM(-2400, 1200, 0),
80 static const char * const ng_type_txt[] =
81 { "Constant PGA Gain", "Mute ADC Output" };
82 static const struct soc_enum ng_type =
83 SOC_ENUM_SINGLE(ES8316_ADC_ALC_NG, 6, 2, ng_type_txt);
85 static const char * const adcpol_txt[] = { "Normal", "Invert" };
86 static const struct soc_enum adcpol =
87 SOC_ENUM_SINGLE(ES8316_ADC_MUTE, 1, 2, adcpol_txt);
88 static const char *const dacpol_txt[] =
89 { "Normal", "R Invert", "L Invert", "L + R Invert" };
90 static const struct soc_enum dacpol =
91 SOC_ENUM_SINGLE(ES8316_DAC_SET1, 0, 4, dacpol_txt);
93 static const struct snd_kcontrol_new es8316_snd_controls[] = {
94 SOC_DOUBLE_TLV("Headphone Playback Volume", ES8316_CPHP_ICAL_VOL,
95 4, 0, 3, 1, hpout_vol_tlv),
96 SOC_DOUBLE_TLV("Headphone Mixer Volume", ES8316_HPMIX_VOL,
97 4, 0, 11, 0, hpmixer_gain_tlv),
99 SOC_ENUM("Playback Polarity", dacpol),
100 SOC_DOUBLE_R_TLV("DAC Playback Volume", ES8316_DAC_VOLL,
101 ES8316_DAC_VOLR, 0, 0xc0, 1, dac_vol_tlv),
102 SOC_SINGLE("DAC Soft Ramp Switch", ES8316_DAC_SET1, 4, 1, 1),
103 SOC_SINGLE("DAC Soft Ramp Rate", ES8316_DAC_SET1, 2, 4, 0),
104 SOC_SINGLE("DAC Notch Filter Switch", ES8316_DAC_SET2, 6, 1, 0),
105 SOC_SINGLE("DAC Double Fs Switch", ES8316_DAC_SET2, 7, 1, 0),
106 SOC_SINGLE("DAC Stereo Enhancement", ES8316_DAC_SET3, 0, 7, 0),
107 SOC_SINGLE("DAC Mono Mix Switch", ES8316_DAC_SET3, 3, 1, 0),
109 SOC_ENUM("Capture Polarity", adcpol),
110 SOC_SINGLE("Mic Boost Switch", ES8316_ADC_D2SEPGA, 0, 1, 0),
111 SOC_SINGLE_TLV("ADC Capture Volume", ES8316_ADC_VOLUME,
112 0, 0xc0, 1, adc_vol_tlv),
113 SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8316_ADC_PGAGAIN,
114 4, 10, 0, adc_pga_gain_tlv),
115 SOC_SINGLE("ADC Soft Ramp Switch", ES8316_ADC_MUTE, 4, 1, 0),
116 SOC_SINGLE("ADC Double Fs Switch", ES8316_ADC_DMIC, 4, 1, 0),
118 SOC_SINGLE("ALC Capture Switch", ES8316_ADC_ALC1, 6, 1, 0),
119 SOC_SINGLE_TLV("ALC Capture Max Volume", ES8316_ADC_ALC1, 0, 28, 0,
121 SOC_SINGLE_TLV("ALC Capture Min Volume", ES8316_ADC_ALC2, 0, 28, 0,
123 SOC_SINGLE_TLV("ALC Capture Target Volume", ES8316_ADC_ALC3, 4, 11, 0,
125 SOC_SINGLE("ALC Capture Hold Time", ES8316_ADC_ALC3, 0, 10, 0),
126 SOC_SINGLE("ALC Capture Decay Time", ES8316_ADC_ALC4, 4, 10, 0),
127 SOC_SINGLE("ALC Capture Attack Time", ES8316_ADC_ALC4, 0, 10, 0),
128 SOC_SINGLE("ALC Capture Noise Gate Switch", ES8316_ADC_ALC_NG,
130 SOC_SINGLE("ALC Capture Noise Gate Threshold", ES8316_ADC_ALC_NG,
132 SOC_ENUM("ALC Capture Noise Gate Type", ng_type),
135 /* Analog Input Mux */
136 static const char * const es8316_analog_in_txt[] = {
139 "lin1-rin1 with 20db Boost",
140 "lin2-rin2 with 20db Boost"
142 static const unsigned int es8316_analog_in_values[] = { 0, 1, 2, 3 };
143 static const struct soc_enum es8316_analog_input_enum =
144 SOC_VALUE_ENUM_SINGLE(ES8316_ADC_PDN_LINSEL, 4, 3,
145 ARRAY_SIZE(es8316_analog_in_txt),
146 es8316_analog_in_txt,
147 es8316_analog_in_values);
148 static const struct snd_kcontrol_new es8316_analog_in_mux_controls =
149 SOC_DAPM_ENUM("Route", es8316_analog_input_enum);
151 static const char * const es8316_dmic_txt[] = {
153 "dmic data at high level",
154 "dmic data at low level",
156 static const unsigned int es8316_dmic_values[] = { 0, 1, 2 };
157 static const struct soc_enum es8316_dmic_src_enum =
158 SOC_VALUE_ENUM_SINGLE(ES8316_ADC_DMIC, 0, 3,
159 ARRAY_SIZE(es8316_dmic_txt),
162 static const struct snd_kcontrol_new es8316_dmic_src_controls =
163 SOC_DAPM_ENUM("Route", es8316_dmic_src_enum);
166 static const char * const es8316_hpmux_texts[] = {
169 "lin-rin with Boost",
170 "lin-rin with Boost and PGA"
173 static SOC_ENUM_SINGLE_DECL(es8316_left_hpmux_enum, ES8316_HPMIX_SEL,
174 4, es8316_hpmux_texts);
176 static const struct snd_kcontrol_new es8316_left_hpmux_controls =
177 SOC_DAPM_ENUM("Route", es8316_left_hpmux_enum);
179 static SOC_ENUM_SINGLE_DECL(es8316_right_hpmux_enum, ES8316_HPMIX_SEL,
180 0, es8316_hpmux_texts);
182 static const struct snd_kcontrol_new es8316_right_hpmux_controls =
183 SOC_DAPM_ENUM("Route", es8316_right_hpmux_enum);
185 /* headphone Output Mixer */
186 static const struct snd_kcontrol_new es8316_out_left_mix[] = {
187 SOC_DAPM_SINGLE("LLIN Switch", ES8316_HPMIX_SWITCH, 6, 1, 0),
188 SOC_DAPM_SINGLE("Left DAC Switch", ES8316_HPMIX_SWITCH, 7, 1, 0),
190 static const struct snd_kcontrol_new es8316_out_right_mix[] = {
191 SOC_DAPM_SINGLE("RLIN Switch", ES8316_HPMIX_SWITCH, 2, 1, 0),
192 SOC_DAPM_SINGLE("Right DAC Switch", ES8316_HPMIX_SWITCH, 3, 1, 0),
195 /* DAC data source mux */
196 static const char * const es8316_dacsrc_texts[] = {
197 "LDATA TO LDAC, RDATA TO RDAC",
198 "LDATA TO LDAC, LDATA TO RDAC",
199 "RDATA TO LDAC, RDATA TO RDAC",
200 "RDATA TO LDAC, LDATA TO RDAC",
203 static SOC_ENUM_SINGLE_DECL(es8316_dacsrc_mux_enum, ES8316_DAC_SET1,
204 6, es8316_dacsrc_texts);
206 static const struct snd_kcontrol_new es8316_dacsrc_mux_controls =
207 SOC_DAPM_ENUM("Route", es8316_dacsrc_mux_enum);
209 static const struct snd_soc_dapm_widget es8316_dapm_widgets[] = {
210 SND_SOC_DAPM_SUPPLY("Bias", ES8316_SYS_PDN, 3, 1, NULL, 0),
211 SND_SOC_DAPM_SUPPLY("Analog power", ES8316_SYS_PDN, 4, 1, NULL, 0),
212 SND_SOC_DAPM_SUPPLY("Mic Bias", ES8316_SYS_PDN, 5, 1, NULL, 0),
214 SND_SOC_DAPM_INPUT("DMIC"),
215 SND_SOC_DAPM_INPUT("MIC1"),
216 SND_SOC_DAPM_INPUT("MIC2"),
219 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
220 &es8316_analog_in_mux_controls),
222 SND_SOC_DAPM_SUPPLY("ADC Vref", ES8316_SYS_PDN, 1, 1, NULL, 0),
223 SND_SOC_DAPM_SUPPLY("ADC bias", ES8316_SYS_PDN, 2, 1, NULL, 0),
224 SND_SOC_DAPM_SUPPLY("ADC Clock", ES8316_CLKMGR_CLKSW, 3, 0, NULL, 0),
225 SND_SOC_DAPM_PGA("Line input PGA", ES8316_ADC_PDN_LINSEL,
227 SND_SOC_DAPM_ADC("Mono ADC", NULL, ES8316_ADC_PDN_LINSEL, 6, 1),
228 SND_SOC_DAPM_MUX("Digital Mic Mux", SND_SOC_NOPM, 0, 0,
229 &es8316_dmic_src_controls),
231 /* Digital Interface */
232 SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 1,
233 ES8316_SERDATA_ADC, 6, 1),
234 SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0,
237 SND_SOC_DAPM_MUX("DAC Source Mux", SND_SOC_NOPM, 0, 0,
238 &es8316_dacsrc_mux_controls),
240 SND_SOC_DAPM_SUPPLY("DAC Vref", ES8316_SYS_PDN, 0, 1, NULL, 0),
241 SND_SOC_DAPM_SUPPLY("DAC Clock", ES8316_CLKMGR_CLKSW, 2, 0, NULL, 0),
242 SND_SOC_DAPM_DAC("Right DAC", NULL, ES8316_DAC_PDN, 0, 1),
243 SND_SOC_DAPM_DAC("Left DAC", NULL, ES8316_DAC_PDN, 4, 1),
245 /* Headphone Output Side */
246 SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
247 &es8316_left_hpmux_controls),
248 SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
249 &es8316_right_hpmux_controls),
250 SND_SOC_DAPM_MIXER("Left Headphone Mixer", ES8316_HPMIX_PDN,
251 5, 1, &es8316_out_left_mix[0],
252 ARRAY_SIZE(es8316_out_left_mix)),
253 SND_SOC_DAPM_MIXER("Right Headphone Mixer", ES8316_HPMIX_PDN,
254 1, 1, &es8316_out_right_mix[0],
255 ARRAY_SIZE(es8316_out_right_mix)),
256 SND_SOC_DAPM_PGA("Left Headphone Mixer Out", ES8316_HPMIX_PDN,
258 SND_SOC_DAPM_PGA("Right Headphone Mixer Out", ES8316_HPMIX_PDN,
261 SND_SOC_DAPM_OUT_DRV("Left Headphone Charge Pump", ES8316_CPHP_OUTEN,
263 SND_SOC_DAPM_OUT_DRV("Right Headphone Charge Pump", ES8316_CPHP_OUTEN,
265 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump", ES8316_CPHP_PDN2,
267 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump Clock", ES8316_CLKMGR_CLKSW,
270 SND_SOC_DAPM_OUT_DRV("Left Headphone Driver", ES8316_CPHP_OUTEN,
272 SND_SOC_DAPM_OUT_DRV("Right Headphone Driver", ES8316_CPHP_OUTEN,
274 SND_SOC_DAPM_SUPPLY("Headphone Out", ES8316_CPHP_PDN1, 2, 1, NULL, 0),
276 /* pdn_Lical and pdn_Rical bits are documented as Reserved, but must
277 * be explicitly unset in order to enable HP output
279 SND_SOC_DAPM_SUPPLY("Left Headphone ical", ES8316_CPHP_ICAL_VOL,
281 SND_SOC_DAPM_SUPPLY("Right Headphone ical", ES8316_CPHP_ICAL_VOL,
284 SND_SOC_DAPM_OUTPUT("HPOL"),
285 SND_SOC_DAPM_OUTPUT("HPOR"),
288 static const struct snd_soc_dapm_route es8316_dapm_routes[] = {
290 {"MIC1", NULL, "Mic Bias"},
291 {"MIC2", NULL, "Mic Bias"},
292 {"MIC1", NULL, "Bias"},
293 {"MIC2", NULL, "Bias"},
294 {"MIC1", NULL, "Analog power"},
295 {"MIC2", NULL, "Analog power"},
297 {"Differential Mux", "lin1-rin1", "MIC1"},
298 {"Differential Mux", "lin2-rin2", "MIC2"},
299 {"Line input PGA", NULL, "Differential Mux"},
301 {"Mono ADC", NULL, "ADC Clock"},
302 {"Mono ADC", NULL, "ADC Vref"},
303 {"Mono ADC", NULL, "ADC bias"},
304 {"Mono ADC", NULL, "Line input PGA"},
306 /* It's not clear why, but to avoid recording only silence,
307 * the DAC clock must be running for the ADC to work.
309 {"Mono ADC", NULL, "DAC Clock"},
311 {"Digital Mic Mux", "dmic disable", "Mono ADC"},
313 {"I2S OUT", NULL, "Digital Mic Mux"},
316 {"DAC Source Mux", "LDATA TO LDAC, RDATA TO RDAC", "I2S IN"},
318 {"Left DAC", NULL, "DAC Clock"},
319 {"Right DAC", NULL, "DAC Clock"},
321 {"Left DAC", NULL, "DAC Vref"},
322 {"Right DAC", NULL, "DAC Vref"},
324 {"Left DAC", NULL, "DAC Source Mux"},
325 {"Right DAC", NULL, "DAC Source Mux"},
327 {"Left Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
328 {"Right Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
330 {"Left Headphone Mixer", "LLIN Switch", "Left Headphone Mux"},
331 {"Left Headphone Mixer", "Left DAC Switch", "Left DAC"},
333 {"Right Headphone Mixer", "RLIN Switch", "Right Headphone Mux"},
334 {"Right Headphone Mixer", "Right DAC Switch", "Right DAC"},
336 {"Left Headphone Mixer Out", NULL, "Left Headphone Mixer"},
337 {"Right Headphone Mixer Out", NULL, "Right Headphone Mixer"},
339 {"Left Headphone Charge Pump", NULL, "Left Headphone Mixer Out"},
340 {"Right Headphone Charge Pump", NULL, "Right Headphone Mixer Out"},
342 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump"},
343 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump"},
345 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
346 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
348 {"Left Headphone Driver", NULL, "Left Headphone Charge Pump"},
349 {"Right Headphone Driver", NULL, "Right Headphone Charge Pump"},
351 {"HPOL", NULL, "Left Headphone Driver"},
352 {"HPOR", NULL, "Right Headphone Driver"},
354 {"HPOL", NULL, "Left Headphone ical"},
355 {"HPOR", NULL, "Right Headphone ical"},
357 {"Headphone Out", NULL, "Bias"},
358 {"Headphone Out", NULL, "Analog power"},
359 {"HPOL", NULL, "Headphone Out"},
360 {"HPOR", NULL, "Headphone Out"},
363 static int es8316_set_dai_sysclk(struct snd_soc_dai *codec_dai,
364 int clk_id, unsigned int freq, int dir)
366 struct snd_soc_component *component = codec_dai->component;
367 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
371 es8316->sysclk = freq;
372 es8316->sysclk_constraints.list = NULL;
373 es8316->sysclk_constraints.count = 0;
378 ret = clk_set_rate(es8316->mclk, freq);
382 /* Limit supported sample rates to ones that can be autodetected
383 * by the codec running in slave mode.
385 for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
386 const unsigned int ratio = supported_mclk_lrck_ratios[i];
388 if (freq % ratio == 0)
389 es8316->allowed_rates[count++] = freq / ratio;
393 es8316->sysclk_constraints.list = es8316->allowed_rates;
394 es8316->sysclk_constraints.count = count;
400 static int es8316_set_dai_fmt(struct snd_soc_dai *codec_dai,
403 struct snd_soc_component *component = codec_dai->component;
409 if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBP_CFP)
410 serdata1 |= ES8316_SERDATA1_MASTER;
412 if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S) {
413 dev_err(component->dev, "Codec driver only supports I2S format\n");
417 /* Clock inversion */
418 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
419 case SND_SOC_DAIFMT_NB_NF:
421 case SND_SOC_DAIFMT_IB_IF:
422 serdata1 |= ES8316_SERDATA1_BCLK_INV;
423 serdata2 |= ES8316_SERDATA2_ADCLRP;
425 case SND_SOC_DAIFMT_IB_NF:
426 serdata1 |= ES8316_SERDATA1_BCLK_INV;
428 case SND_SOC_DAIFMT_NB_IF:
429 serdata2 |= ES8316_SERDATA2_ADCLRP;
435 mask = ES8316_SERDATA1_MASTER | ES8316_SERDATA1_BCLK_INV;
436 snd_soc_component_update_bits(component, ES8316_SERDATA1, mask, serdata1);
438 mask = ES8316_SERDATA2_FMT_MASK | ES8316_SERDATA2_ADCLRP;
439 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC, mask, serdata2);
440 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC, mask, serdata2);
442 /* Enable BCLK and MCLK inputs in slave mode */
443 clksw = ES8316_CLKMGR_CLKSW_MCLK_ON | ES8316_CLKMGR_CLKSW_BCLK_ON;
444 snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW, clksw, clksw);
449 static int es8316_pcm_startup(struct snd_pcm_substream *substream,
450 struct snd_soc_dai *dai)
452 struct snd_soc_component *component = dai->component;
453 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
455 if (es8316->sysclk_constraints.list)
456 snd_pcm_hw_constraint_list(substream->runtime, 0,
457 SNDRV_PCM_HW_PARAM_RATE,
458 &es8316->sysclk_constraints);
463 static int es8316_pcm_hw_params(struct snd_pcm_substream *substream,
464 struct snd_pcm_hw_params *params,
465 struct snd_soc_dai *dai)
467 struct snd_soc_component *component = dai->component;
468 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
474 /* Validate supported sample rates that are autodetected from MCLK */
475 for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
476 const unsigned int ratio = supported_mclk_lrck_ratios[i];
478 if (es8316->sysclk % ratio != 0)
480 if (es8316->sysclk / ratio == params_rate(params))
483 if (i == NR_SUPPORTED_MCLK_LRCK_RATIOS)
485 lrck_divider = es8316->sysclk / params_rate(params);
486 bclk_divider = lrck_divider / 4;
487 switch (params_format(params)) {
488 case SNDRV_PCM_FORMAT_S16_LE:
489 wordlen = ES8316_SERDATA2_LEN_16;
492 case SNDRV_PCM_FORMAT_S20_3LE:
493 wordlen = ES8316_SERDATA2_LEN_20;
496 case SNDRV_PCM_FORMAT_S24_LE:
497 case SNDRV_PCM_FORMAT_S24_3LE:
498 wordlen = ES8316_SERDATA2_LEN_24;
501 case SNDRV_PCM_FORMAT_S32_LE:
502 wordlen = ES8316_SERDATA2_LEN_32;
509 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC,
510 ES8316_SERDATA2_LEN_MASK, wordlen);
511 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC,
512 ES8316_SERDATA2_LEN_MASK, wordlen);
513 snd_soc_component_update_bits(component, ES8316_SERDATA1, 0x1f, bclk_divider);
514 snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV1, 0x0f, lrck_divider >> 8);
515 snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV2, 0xff, lrck_divider & 0xff);
516 snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV1, 0x0f, lrck_divider >> 8);
517 snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV2, 0xff, lrck_divider & 0xff);
521 static int es8316_mute(struct snd_soc_dai *dai, int mute, int direction)
523 snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20,
528 #define ES8316_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
529 SNDRV_PCM_FMTBIT_S24_LE)
531 static const struct snd_soc_dai_ops es8316_ops = {
532 .startup = es8316_pcm_startup,
533 .hw_params = es8316_pcm_hw_params,
534 .set_fmt = es8316_set_dai_fmt,
535 .set_sysclk = es8316_set_dai_sysclk,
536 .mute_stream = es8316_mute,
537 .no_capture_mute = 1,
540 static struct snd_soc_dai_driver es8316_dai = {
541 .name = "ES8316 HiFi",
543 .stream_name = "Playback",
546 .rates = SNDRV_PCM_RATE_8000_48000,
547 .formats = ES8316_FORMATS,
550 .stream_name = "Capture",
553 .rates = SNDRV_PCM_RATE_8000_48000,
554 .formats = ES8316_FORMATS,
560 static void es8316_enable_micbias_for_mic_gnd_short_detect(
561 struct snd_soc_component *component)
563 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
565 snd_soc_dapm_mutex_lock(dapm);
566 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Bias");
567 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Analog power");
568 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Mic Bias");
569 snd_soc_dapm_sync_unlocked(dapm);
570 snd_soc_dapm_mutex_unlock(dapm);
575 static void es8316_disable_micbias_for_mic_gnd_short_detect(
576 struct snd_soc_component *component)
578 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
580 snd_soc_dapm_mutex_lock(dapm);
581 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Bias");
582 snd_soc_dapm_disable_pin_unlocked(dapm, "Analog power");
583 snd_soc_dapm_disable_pin_unlocked(dapm, "Bias");
584 snd_soc_dapm_sync_unlocked(dapm);
585 snd_soc_dapm_mutex_unlock(dapm);
588 static irqreturn_t es8316_irq(int irq, void *data)
590 struct es8316_priv *es8316 = data;
591 struct snd_soc_component *comp = es8316->component;
594 mutex_lock(&es8316->lock);
596 regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
598 goto out; /* Powered-down / reset */
600 /* Catch spurious IRQ before set_jack is called */
604 if (es8316->jd_inverted)
605 flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
607 dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
608 if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
609 /* Jack removed, or spurious IRQ? */
610 if (es8316->jack->status & SND_JACK_MICROPHONE)
611 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
613 if (es8316->jack->status & SND_JACK_HEADPHONE) {
614 snd_soc_jack_report(es8316->jack, 0,
615 SND_JACK_HEADSET | SND_JACK_BTN_0);
616 dev_dbg(comp->dev, "jack unplugged\n");
618 } else if (!(es8316->jack->status & SND_JACK_HEADPHONE)) {
619 /* Jack inserted, determine type */
620 es8316_enable_micbias_for_mic_gnd_short_detect(comp);
621 regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
622 if (es8316->jd_inverted)
623 flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
624 dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
625 if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
626 /* Jack unplugged underneath us */
627 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
628 } else if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
630 snd_soc_jack_report(es8316->jack,
633 /* Keep mic-gnd-short detection on for button press */
635 /* Shorted, headphones */
636 snd_soc_jack_report(es8316->jack,
639 /* No longer need mic-gnd-short detection */
640 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
642 } else if (es8316->jack->status & SND_JACK_MICROPHONE) {
643 /* Interrupt while jack inserted, report button state */
644 if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
645 /* Open, button release */
646 snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
648 /* Short, button press */
649 snd_soc_jack_report(es8316->jack,
656 mutex_unlock(&es8316->lock);
660 static void es8316_enable_jack_detect(struct snd_soc_component *component,
661 struct snd_soc_jack *jack)
663 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
666 * Init es8316->jd_inverted here and not in the probe, as we cannot
667 * guarantee that the bytchr-es8316 driver, which might set this
668 * property, will probe before us.
670 es8316->jd_inverted = device_property_read_bool(component->dev,
671 "everest,jack-detect-inverted");
673 mutex_lock(&es8316->lock);
677 if (es8316->jack->status & SND_JACK_MICROPHONE)
678 es8316_enable_micbias_for_mic_gnd_short_detect(component);
680 snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
681 ES8316_GPIO_ENABLE_INTERRUPT,
682 ES8316_GPIO_ENABLE_INTERRUPT);
684 mutex_unlock(&es8316->lock);
686 /* Enable irq and sync initial jack state */
687 enable_irq(es8316->irq);
688 es8316_irq(es8316->irq, es8316);
691 static void es8316_disable_jack_detect(struct snd_soc_component *component)
693 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
696 return; /* Already disabled (or never enabled) */
698 disable_irq(es8316->irq);
700 mutex_lock(&es8316->lock);
702 snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
703 ES8316_GPIO_ENABLE_INTERRUPT, 0);
705 if (es8316->jack->status & SND_JACK_MICROPHONE) {
706 es8316_disable_micbias_for_mic_gnd_short_detect(component);
707 snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
712 mutex_unlock(&es8316->lock);
715 static int es8316_set_jack(struct snd_soc_component *component,
716 struct snd_soc_jack *jack, void *data)
719 es8316_enable_jack_detect(component, jack);
721 es8316_disable_jack_detect(component);
726 static int es8316_probe(struct snd_soc_component *component)
728 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
731 es8316->component = component;
733 es8316->mclk = devm_clk_get_optional(component->dev, "mclk");
734 if (IS_ERR(es8316->mclk)) {
735 dev_err(component->dev, "unable to get mclk\n");
736 return PTR_ERR(es8316->mclk);
739 dev_warn(component->dev, "assuming static mclk\n");
741 ret = clk_prepare_enable(es8316->mclk);
743 dev_err(component->dev, "unable to enable mclk\n");
747 /* Reset codec and enable current state machine */
748 snd_soc_component_write(component, ES8316_RESET, 0x3f);
749 usleep_range(5000, 5500);
750 snd_soc_component_write(component, ES8316_RESET, ES8316_RESET_CSM_ON);
754 * Documentation is unclear, but this value from the vendor driver is
755 * needed otherwise audio output is silent.
757 snd_soc_component_write(component, ES8316_SYS_VMIDSEL, 0xff);
760 * Documentation for this register is unclear and incomplete,
761 * but here is a vendor-provided value that improves volume
762 * and quality for Intel CHT platforms.
764 snd_soc_component_write(component, ES8316_CLKMGR_ADCOSR, 0x32);
769 static void es8316_remove(struct snd_soc_component *component)
771 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
773 clk_disable_unprepare(es8316->mclk);
776 static int es8316_resume(struct snd_soc_component *component)
778 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
780 regcache_cache_only(es8316->regmap, false);
781 regcache_sync(es8316->regmap);
786 static int es8316_suspend(struct snd_soc_component *component)
788 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
790 regcache_cache_only(es8316->regmap, true);
791 regcache_mark_dirty(es8316->regmap);
796 static const struct snd_soc_component_driver soc_component_dev_es8316 = {
797 .probe = es8316_probe,
798 .remove = es8316_remove,
799 .resume = es8316_resume,
800 .suspend = es8316_suspend,
801 .set_jack = es8316_set_jack,
802 .controls = es8316_snd_controls,
803 .num_controls = ARRAY_SIZE(es8316_snd_controls),
804 .dapm_widgets = es8316_dapm_widgets,
805 .num_dapm_widgets = ARRAY_SIZE(es8316_dapm_widgets),
806 .dapm_routes = es8316_dapm_routes,
807 .num_dapm_routes = ARRAY_SIZE(es8316_dapm_routes),
808 .use_pmdown_time = 1,
812 static bool es8316_volatile_reg(struct device *dev, unsigned int reg)
815 case ES8316_GPIO_FLAG:
822 static const struct regmap_config es8316_regmap = {
825 .use_single_read = true,
826 .use_single_write = true,
827 .max_register = 0x53,
828 .volatile_reg = es8316_volatile_reg,
829 .cache_type = REGCACHE_MAPLE,
832 static int es8316_i2c_probe(struct i2c_client *i2c_client)
834 struct device *dev = &i2c_client->dev;
835 struct es8316_priv *es8316;
838 es8316 = devm_kzalloc(&i2c_client->dev, sizeof(struct es8316_priv),
843 i2c_set_clientdata(i2c_client, es8316);
845 es8316->regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap);
846 if (IS_ERR(es8316->regmap))
847 return PTR_ERR(es8316->regmap);
849 es8316->irq = i2c_client->irq;
850 mutex_init(&es8316->lock);
852 if (es8316->irq > 0) {
853 ret = devm_request_threaded_irq(dev, es8316->irq, NULL, es8316_irq,
854 IRQF_TRIGGER_HIGH | IRQF_ONESHOT | IRQF_NO_AUTOEN,
857 dev_warn(dev, "Failed to get IRQ %d: %d\n", es8316->irq, ret);
858 es8316->irq = -ENXIO;
862 return devm_snd_soc_register_component(&i2c_client->dev,
863 &soc_component_dev_es8316,
867 static const struct i2c_device_id es8316_i2c_id[] = {
871 MODULE_DEVICE_TABLE(i2c, es8316_i2c_id);
874 static const struct of_device_id es8316_of_match[] = {
875 { .compatible = "everest,es8316", },
878 MODULE_DEVICE_TABLE(of, es8316_of_match);
882 static const struct acpi_device_id es8316_acpi_match[] = {
887 MODULE_DEVICE_TABLE(acpi, es8316_acpi_match);
890 static struct i2c_driver es8316_i2c_driver = {
893 .acpi_match_table = ACPI_PTR(es8316_acpi_match),
894 .of_match_table = of_match_ptr(es8316_of_match),
896 .probe = es8316_i2c_probe,
897 .id_table = es8316_i2c_id,
899 module_i2c_driver(es8316_i2c_driver);
901 MODULE_DESCRIPTION("Everest Semi ES8316 ALSA SoC Codec Driver");
902 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
903 MODULE_LICENSE("GPL v2");