1 // SPDX-License-Identifier: GPL-2.0-only
3 // es8326.c -- es8326 ALSA SoC audio driver
4 // Copyright Everest Semiconductor Co., Ltd
6 // Authors: David Yang <yangxiaohua@everest-semi.com>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/module.h>
14 #include <sound/jack.h>
15 #include <sound/pcm_params.h>
16 #include <sound/soc.h>
17 #include <sound/soc-dapm.h>
18 #include <sound/tlv.h>
23 struct i2c_client *i2c;
24 struct regmap *regmap;
25 struct snd_soc_component *component;
26 struct delayed_work jack_detect_work;
27 struct delayed_work button_press_work;
28 struct snd_soc_jack *jack;
30 /* The lock protects the situation that an irq is generated
31 * while enabling or disabling or during an irq.
45 int jack_remove_retry;
48 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9550, 50, 0);
49 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9550, 50, 0);
50 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_analog_pga_tlv, 0, 300, 0);
51 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_pga_tlv, 0, 600, 0);
52 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(softramp_rate, 0, 100, 0);
53 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_target_tlv, -3200, 200, 0);
54 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_recovery_tlv, -125, 250, 0);
56 static const char *const winsize[] = {
75 static const char *const dacpol_txt[] = {
76 "Normal", "R Invert", "L Invert", "L + R Invert" };
78 static const struct soc_enum dacpol =
79 SOC_ENUM_SINGLE(ES8326_DAC_DSM, 4, 4, dacpol_txt);
80 static const struct soc_enum alc_winsize =
81 SOC_ENUM_SINGLE(ES8326_ADC_RAMPRATE, 4, 16, winsize);
82 static const struct soc_enum drc_winsize =
83 SOC_ENUM_SINGLE(ES8326_DRC_WINSIZE, 4, 16, winsize);
85 static const struct snd_kcontrol_new es8326_snd_controls[] = {
86 SOC_SINGLE_TLV("DAC Playback Volume", ES8326_DAC_VOL, 0, 0xbf, 0, dac_vol_tlv),
87 SOC_ENUM("Playback Polarity", dacpol),
88 SOC_SINGLE_TLV("DAC Ramp Rate", ES8326_DAC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
89 SOC_SINGLE_TLV("DRC Recovery Level", ES8326_DRC_RECOVERY, 0, 4, 0, drc_recovery_tlv),
90 SOC_ENUM("DRC Winsize", drc_winsize),
91 SOC_SINGLE_TLV("DRC Target Level", ES8326_DRC_WINSIZE, 0, 0x0f, 0, drc_target_tlv),
93 SOC_DOUBLE_R_TLV("ADC Capture Volume", ES8326_ADC1_VOL, ES8326_ADC2_VOL, 0, 0xff, 0,
95 SOC_DOUBLE_TLV("ADC PGA Volume", ES8326_ADC_SCALE, 4, 0, 5, 0, adc_pga_tlv),
96 SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8326_PGAGAIN, 0, 10, 0, adc_analog_pga_tlv),
97 SOC_SINGLE_TLV("ADC Ramp Rate", ES8326_ADC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
98 SOC_SINGLE("ALC Capture Switch", ES8326_ALC_RECOVERY, 3, 1, 0),
99 SOC_SINGLE_TLV("ALC Capture Recovery Level", ES8326_ALC_LEVEL,
100 0, 4, 0, drc_recovery_tlv),
101 SOC_ENUM("ALC Capture Winsize", alc_winsize),
102 SOC_SINGLE_TLV("ALC Capture Target Level", ES8326_ALC_LEVEL,
103 0, 0x0f, 0, drc_target_tlv),
107 static const struct snd_soc_dapm_widget es8326_dapm_widgets[] = {
108 SND_SOC_DAPM_INPUT("MIC1"),
109 SND_SOC_DAPM_INPUT("MIC2"),
110 SND_SOC_DAPM_INPUT("MIC3"),
111 SND_SOC_DAPM_INPUT("MIC4"),
113 SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
114 SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
116 /* Digital Interface */
117 SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 0, SND_SOC_NOPM, 0, 0),
118 SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0, SND_SOC_NOPM, 0, 0),
120 /* ADC Digital Mute */
121 SND_SOC_DAPM_PGA("ADC L1", ES8326_ADC_MUTE, 0, 1, NULL, 0),
122 SND_SOC_DAPM_PGA("ADC R1", ES8326_ADC_MUTE, 1, 1, NULL, 0),
123 SND_SOC_DAPM_PGA("ADC L2", ES8326_ADC_MUTE, 2, 1, NULL, 0),
124 SND_SOC_DAPM_PGA("ADC R2", ES8326_ADC_MUTE, 3, 1, NULL, 0),
126 /* Analog Power Supply*/
127 SND_SOC_DAPM_DAC("Right DAC", NULL, ES8326_ANA_PDN, 0, 1),
128 SND_SOC_DAPM_DAC("Left DAC", NULL, ES8326_ANA_PDN, 1, 1),
129 SND_SOC_DAPM_SUPPLY("MICBIAS1", ES8326_ANA_MICBIAS, 2, 0, NULL, 0),
130 SND_SOC_DAPM_SUPPLY("MICBIAS2", ES8326_ANA_MICBIAS, 3, 0, NULL, 0),
132 SND_SOC_DAPM_PGA("LHPMIX", ES8326_DAC2HPMIX, 7, 0, NULL, 0),
133 SND_SOC_DAPM_PGA("RHPMIX", ES8326_DAC2HPMIX, 3, 0, NULL, 0),
135 SND_SOC_DAPM_OUTPUT("HPOL"),
136 SND_SOC_DAPM_OUTPUT("HPOR"),
139 static const struct snd_soc_dapm_route es8326_dapm_routes[] = {
140 {"ADC L1", NULL, "MIC1"},
141 {"ADC R1", NULL, "MIC2"},
142 {"ADC L2", NULL, "MIC3"},
143 {"ADC R2", NULL, "MIC4"},
145 {"ADC L", NULL, "ADC L1"},
146 {"ADC R", NULL, "ADC R1"},
147 {"ADC L", NULL, "ADC L2"},
148 {"ADC R", NULL, "ADC R2"},
150 {"I2S OUT", NULL, "ADC L"},
151 {"I2S OUT", NULL, "ADC R"},
153 {"Right DAC", NULL, "I2S IN"},
154 {"Left DAC", NULL, "I2S IN"},
156 {"LHPMIX", NULL, "Left DAC"},
157 {"RHPMIX", NULL, "Right DAC"},
159 {"HPOL", NULL, "LHPMIX"},
160 {"HPOR", NULL, "RHPMIX"},
163 static bool es8326_volatile_register(struct device *dev, unsigned int reg)
166 case ES8326_HPL_OFFSET_INI:
167 case ES8326_HPR_OFFSET_INI:
168 case ES8326_HPDET_STA:
169 case ES8326_CTIA_OMTP_STA:
170 case ES8326_CSM_MUTE_STA:
177 static const struct regmap_config es8326_regmap_config = {
180 .max_register = 0xff,
181 .volatile_reg = es8326_volatile_register,
182 .cache_type = REGCACHE_RBTREE,
199 /* codec hifi mclk clock divider coefficients */
200 /* {ratio, LRCK, MCLK, REG04, REG05, REG06, REG07, REG08, REG09, REG10, REG11} */
201 static const struct _coeff_div coeff_div[] = {
202 {32, 8000, 256000, 0x60, 0x00, 0x0F, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
203 {32, 16000, 512000, 0x20, 0x00, 0x0D, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
204 {32, 44100, 1411200, 0x00, 0x00, 0x13, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
205 {32, 48000, 1536000, 0x00, 0x00, 0x13, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
206 {36, 8000, 288000, 0x20, 0x00, 0x0D, 0x75, 0x0A, 0x1B, 0x23, 0x47},
207 {36, 16000, 576000, 0x20, 0x00, 0x0D, 0x75, 0x0A, 0x1B, 0x23, 0x47},
208 {48, 8000, 384000, 0x60, 0x02, 0x1F, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
209 {48, 16000, 768000, 0x20, 0x02, 0x0F, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
210 {48, 48000, 2304000, 0x00, 0x02, 0x0D, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
211 {64, 8000, 512000, 0x60, 0x00, 0x0D, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
212 {64, 16000, 1024000, 0x20, 0x00, 0x05, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
214 {64, 44100, 2822400, 0x00, 0x00, 0x11, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
215 {64, 48000, 3072000, 0x00, 0x00, 0x11, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
216 {72, 8000, 576000, 0x20, 0x00, 0x13, 0x35, 0x0A, 0x1B, 0x23, 0x47},
217 {72, 16000, 1152000, 0x20, 0x00, 0x05, 0x75, 0x0A, 0x1B, 0x23, 0x47},
218 {96, 8000, 768000, 0x60, 0x02, 0x1D, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
219 {96, 16000, 1536000, 0x20, 0x02, 0x0D, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
220 {100, 48000, 4800000, 0x04, 0x04, 0x3F, 0x6D, 0x38, 0x08, 0x4f, 0x1f},
221 {125, 48000, 6000000, 0x04, 0x04, 0x1F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
222 {128, 8000, 1024000, 0x60, 0x00, 0x13, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
223 {128, 16000, 2048000, 0x20, 0x00, 0x11, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
225 {128, 44100, 5644800, 0x00, 0x00, 0x01, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
226 {128, 48000, 6144000, 0x00, 0x00, 0x01, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
227 {144, 8000, 1152000, 0x20, 0x00, 0x03, 0x35, 0x0A, 0x1B, 0x23, 0x47},
228 {144, 16000, 2304000, 0x20, 0x00, 0x11, 0x35, 0x0A, 0x1B, 0x23, 0x47},
229 {192, 8000, 1536000, 0x60, 0x02, 0x0D, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
230 {192, 16000, 3072000, 0x20, 0x02, 0x05, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
231 {200, 48000, 9600000, 0x04, 0x04, 0x0F, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
232 {250, 48000, 12000000, 0x04, 0x04, 0x0F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
233 {256, 8000, 2048000, 0x60, 0x00, 0x11, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
234 {256, 16000, 4096000, 0x20, 0x00, 0x01, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
236 {256, 44100, 11289600, 0x00, 0x00, 0x10, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
237 {256, 48000, 12288000, 0x00, 0x00, 0x30, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
238 {288, 8000, 2304000, 0x20, 0x00, 0x01, 0x35, 0x0A, 0x1B, 0x23, 0x47},
239 {384, 8000, 3072000, 0x60, 0x02, 0x05, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
240 {384, 16000, 6144000, 0x20, 0x02, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
241 {384, 48000, 18432000, 0x00, 0x02, 0x01, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
242 {400, 48000, 19200000, 0x09, 0x04, 0x0f, 0x6d, 0x3a, 0x0A, 0x4F, 0x1F},
243 {500, 48000, 24000000, 0x18, 0x04, 0x1F, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
244 {512, 8000, 4096000, 0x60, 0x00, 0x01, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
245 {512, 16000, 8192000, 0x20, 0x00, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
247 {512, 44100, 22579200, 0x00, 0x00, 0x00, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
248 {512, 48000, 24576000, 0x00, 0x00, 0x00, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
249 {768, 8000, 6144000, 0x60, 0x02, 0x11, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
250 {768, 16000, 12288000, 0x20, 0x02, 0x01, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
251 {800, 48000, 38400000, 0x00, 0x18, 0x13, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
252 {1024, 8000, 8192000, 0x60, 0x00, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
253 {1024, 16000, 16384000, 0x20, 0x00, 0x00, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
254 {1152, 16000, 18432000, 0x20, 0x08, 0x11, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
255 {1536, 8000, 12288000, 0x60, 0x02, 0x01, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
257 {1536, 16000, 24576000, 0x20, 0x02, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
258 {1625, 8000, 13000000, 0x0C, 0x18, 0x1F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
259 {1625, 16000, 26000000, 0x0C, 0x18, 0x1F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
260 {2048, 8000, 16384000, 0x60, 0x00, 0x00, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
261 {2304, 8000, 18432000, 0x40, 0x02, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x5F},
262 {3072, 8000, 24576000, 0x60, 0x02, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
263 {3250, 8000, 26000000, 0x0C, 0x18, 0x0F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
267 static inline int get_coeff(int mclk, int rate)
271 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
272 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
279 static int es8326_set_dai_sysclk(struct snd_soc_dai *codec_dai,
280 int clk_id, unsigned int freq, int dir)
282 struct snd_soc_component *codec = codec_dai->component;
283 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
285 es8326->sysclk = freq;
290 static int es8326_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
292 struct snd_soc_component *component = codec_dai->component;
295 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
296 case SND_SOC_DAIFMT_CBC_CFP:
297 snd_soc_component_update_bits(component, ES8326_RESET,
298 ES8326_MASTER_MODE_EN, ES8326_MASTER_MODE_EN);
300 case SND_SOC_DAIFMT_CBC_CFC:
306 /* interface format */
307 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
308 case SND_SOC_DAIFMT_I2S:
310 case SND_SOC_DAIFMT_RIGHT_J:
311 dev_err(component->dev, "Codec driver does not support right justified\n");
313 case SND_SOC_DAIFMT_LEFT_J:
314 iface |= ES8326_DAIFMT_LEFT_J;
316 case SND_SOC_DAIFMT_DSP_A:
317 iface |= ES8326_DAIFMT_DSP_A;
319 case SND_SOC_DAIFMT_DSP_B:
320 iface |= ES8326_DAIFMT_DSP_B;
326 snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DAIFMT_MASK, iface);
331 static int es8326_pcm_hw_params(struct snd_pcm_substream *substream,
332 struct snd_pcm_hw_params *params,
333 struct snd_soc_dai *dai)
335 struct snd_soc_component *component = dai->component;
336 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
340 coeff = get_coeff(es8326->sysclk, params_rate(params));
342 switch (params_format(params)) {
343 case SNDRV_PCM_FORMAT_S16_LE:
344 srate |= ES8326_S16_LE;
346 case SNDRV_PCM_FORMAT_S20_3LE:
347 srate |= ES8326_S20_3_LE;
349 case SNDRV_PCM_FORMAT_S18_3LE:
350 srate |= ES8326_S18_LE;
352 case SNDRV_PCM_FORMAT_S24_LE:
353 srate |= ES8326_S24_LE;
355 case SNDRV_PCM_FORMAT_S32_LE:
356 srate |= ES8326_S32_LE;
362 /* set iface & srate */
363 snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DATA_LEN_MASK, srate);
366 regmap_write(es8326->regmap, ES8326_CLK_DIV1,
367 coeff_div[coeff].reg4);
368 regmap_write(es8326->regmap, ES8326_CLK_DIV2,
369 coeff_div[coeff].reg5);
370 regmap_write(es8326->regmap, ES8326_CLK_DLL,
371 coeff_div[coeff].reg6);
372 regmap_write(es8326->regmap, ES8326_CLK_MUX,
373 coeff_div[coeff].reg7);
374 regmap_write(es8326->regmap, ES8326_CLK_ADC_SEL,
375 coeff_div[coeff].reg8);
376 regmap_write(es8326->regmap, ES8326_CLK_DAC_SEL,
377 coeff_div[coeff].reg9);
378 regmap_write(es8326->regmap, ES8326_CLK_ADC_OSR,
379 coeff_div[coeff].rega);
380 regmap_write(es8326->regmap, ES8326_CLK_DAC_OSR,
381 coeff_div[coeff].regb);
383 dev_warn(component->dev, "Clock coefficients do not match");
389 static int es8326_mute(struct snd_soc_dai *dai, int mute, int direction)
391 struct snd_soc_component *component = dai->component;
392 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
393 unsigned int offset_l, offset_r;
396 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
397 regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE,
398 ES8326_MUTE_MASK, ES8326_MUTE);
399 regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xf0);
401 if (!es8326->calibrated) {
402 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_FORCE_CAL);
404 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
405 regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l);
406 regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r);
407 regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
408 regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l);
409 regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r);
410 es8326->calibrated = true;
412 regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa0);
413 regmap_write(es8326->regmap, ES8326_HP_VOL, 0x80);
414 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_ON);
415 regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE,
416 ES8326_MUTE_MASK, ~(ES8326_MUTE));
421 static int es8326_set_bias_level(struct snd_soc_component *codec,
422 enum snd_soc_bias_level level)
424 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
428 case SND_SOC_BIAS_ON:
429 ret = clk_prepare_enable(es8326->mclk);
433 regmap_write(es8326->regmap, ES8326_RESET, 0x9f);
435 regmap_update_bits(es8326->regmap, ES8326_DAC_DSM, 0x01, 0x00);
436 regmap_write(es8326->regmap, ES8326_INTOUT_IO, es8326->interrupt_clk);
437 regmap_write(es8326->regmap, ES8326_SDINOUT1_IO,
438 (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT));
439 regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x0E);
440 regmap_write(es8326->regmap, ES8326_PGA_PDN, 0x40);
441 regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x00);
442 regmap_update_bits(es8326->regmap, ES8326_CLK_CTL, 0x20, 0x20);
443 regmap_write(es8326->regmap, ES8326_RESET, ES8326_CSM_ON);
445 case SND_SOC_BIAS_PREPARE:
447 case SND_SOC_BIAS_STANDBY:
449 case SND_SOC_BIAS_OFF:
450 clk_disable_unprepare(es8326->mclk);
451 regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x3b);
452 regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x00);
453 regmap_update_bits(es8326->regmap, ES8326_CLK_CTL, 0x20, 0x00);
454 regmap_write(es8326->regmap, ES8326_SDINOUT1_IO, ES8326_IO_INPUT);
461 #define es8326_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
462 SNDRV_PCM_FMTBIT_S24_LE)
464 static const struct snd_soc_dai_ops es8326_ops = {
465 .hw_params = es8326_pcm_hw_params,
466 .set_fmt = es8326_set_dai_fmt,
467 .set_sysclk = es8326_set_dai_sysclk,
468 .mute_stream = es8326_mute,
469 .no_capture_mute = 1,
472 static struct snd_soc_dai_driver es8326_dai = {
473 .name = "ES8326 HiFi",
475 .stream_name = "Playback",
478 .rates = SNDRV_PCM_RATE_8000_48000,
479 .formats = es8326_FORMATS,
482 .stream_name = "Capture",
485 .rates = SNDRV_PCM_RATE_8000_48000,
486 .formats = es8326_FORMATS,
492 static void es8326_enable_micbias(struct snd_soc_component *component)
494 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
496 snd_soc_dapm_mutex_lock(dapm);
497 snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS1");
498 snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS2");
499 snd_soc_dapm_sync_unlocked(dapm);
500 snd_soc_dapm_mutex_unlock(dapm);
503 static void es8326_disable_micbias(struct snd_soc_component *component)
505 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
507 snd_soc_dapm_mutex_lock(dapm);
508 snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS1");
509 snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS2");
510 snd_soc_dapm_sync_unlocked(dapm);
511 snd_soc_dapm_mutex_unlock(dapm);
515 * For button detection, set the following in soundcard
516 * snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
517 * snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
518 * snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
520 static void es8326_jack_button_handler(struct work_struct *work)
522 struct es8326_priv *es8326 =
523 container_of(work, struct es8326_priv, button_press_work.work);
524 struct snd_soc_component *comp = es8326->component;
526 static int button_to_report, press_count;
527 static int prev_button, cur_button;
529 if (!(es8326->jack->status & SND_JACK_HEADSET)) /* Jack unplugged */
532 mutex_lock(&es8326->lock);
533 iface = snd_soc_component_read(comp, ES8326_HPDET_STA);
536 /* pause button detected */
537 cur_button = SND_JACK_BTN_0;
541 /* button volume up */
542 cur_button = SND_JACK_BTN_1;
545 /* button volume down */
546 cur_button = SND_JACK_BTN_2;
550 /* button released or not pressed */
557 if ((prev_button == cur_button) && (cur_button != 0)) {
559 if (press_count > 3) {
560 /* report a press every 120ms */
561 snd_soc_jack_report(es8326->jack, cur_button,
562 SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
565 button_to_report = cur_button;
566 queue_delayed_work(system_wq, &es8326->button_press_work,
567 msecs_to_jiffies(35));
568 } else if (prev_button != cur_button) {
569 /* mismatch, detect again */
570 prev_button = cur_button;
571 queue_delayed_work(system_wq, &es8326->button_press_work,
572 msecs_to_jiffies(35));
574 /* released or no pressed */
575 if (button_to_report != 0) {
576 snd_soc_jack_report(es8326->jack, button_to_report,
577 SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
578 snd_soc_jack_report(es8326->jack, 0,
579 SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
580 button_to_report = 0;
583 mutex_unlock(&es8326->lock);
586 static void es8326_jack_detect_handler(struct work_struct *work)
588 struct es8326_priv *es8326 =
589 container_of(work, struct es8326_priv, jack_detect_work.work);
590 struct snd_soc_component *comp = es8326->component;
593 mutex_lock(&es8326->lock);
594 iface = snd_soc_component_read(comp, ES8326_HPDET_STA);
595 dev_dbg(comp->dev, "gpio flag %#04x", iface);
597 if (es8326->jack_remove_retry == 1) {
598 if (iface & ES8326_HPINSERT_FLAG)
599 es8326->jack_remove_retry = 2;
601 es8326->jack_remove_retry = 0;
603 dev_dbg(comp->dev, "remove event check, set HPJACK_POL normal, cnt = %d\n",
604 es8326->jack_remove_retry);
606 * Inverted HPJACK_POL bit to trigger one IRQ to double check HP Removal event
608 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE,
609 ES8326_HP_DET_JACK_POL, (es8326->jd_inverted ?
610 ~es8326->jack_pol : es8326->jack_pol));
614 if ((iface & ES8326_HPINSERT_FLAG) == 0) {
615 /* Jack unplugged or spurious IRQ */
616 dev_dbg(comp->dev, "No headset detected\n");
617 es8326_disable_micbias(es8326->component);
618 if (es8326->jack->status & SND_JACK_HEADPHONE) {
619 dev_dbg(comp->dev, "Report hp remove event\n");
620 snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
621 /* mute adc when mic path switch */
622 regmap_write(es8326->regmap, ES8326_ADC_SCALE, 0x33);
623 regmap_write(es8326->regmap, ES8326_ADC1_SRC, 0x44);
624 regmap_write(es8326->regmap, ES8326_ADC2_SRC, 0x66);
627 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x01);
629 * Inverted HPJACK_POL bit to trigger one IRQ to double check HP Removal event
631 if (es8326->jack_remove_retry == 0) {
632 es8326->jack_remove_retry = 1;
633 dev_dbg(comp->dev, "remove event check, invert HPJACK_POL, cnt = %d\n",
634 es8326->jack_remove_retry);
635 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE,
636 ES8326_HP_DET_JACK_POL, (es8326->jd_inverted ?
637 es8326->jack_pol : ~es8326->jack_pol));
640 es8326->jack_remove_retry = 0;
642 } else if ((iface & ES8326_HPINSERT_FLAG) == ES8326_HPINSERT_FLAG) {
643 es8326->jack_remove_retry = 0;
644 if (es8326->hp == 0) {
645 dev_dbg(comp->dev, "First insert, start OMTP/CTIA type check\n");
647 * set auto-check mode, then restart jack_detect_work after 100ms.
648 * Don't report jack status.
650 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x01);
651 usleep_range(50000, 70000);
652 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x00);
653 queue_delayed_work(system_wq, &es8326->jack_detect_work,
654 msecs_to_jiffies(100));
658 if (es8326->jack->status & SND_JACK_HEADSET) {
660 dev_dbg(comp->dev, "button pressed\n");
661 queue_delayed_work(system_wq, &es8326->button_press_work, 10);
664 if ((iface & ES8326_HPBUTTON_FLAG) == 0x01) {
665 dev_dbg(comp->dev, "Headphone detected\n");
666 snd_soc_jack_report(es8326->jack,
667 SND_JACK_HEADPHONE, SND_JACK_HEADSET);
669 dev_dbg(comp->dev, "Headset detected\n");
670 snd_soc_jack_report(es8326->jack,
671 SND_JACK_HEADSET, SND_JACK_HEADSET);
673 regmap_write(es8326->regmap, ES8326_ADC_SCALE, 0x33);
674 regmap_update_bits(es8326->regmap, ES8326_PGA_PDN,
676 regmap_update_bits(es8326->regmap, ES8326_PGAGAIN,
678 regmap_write(es8326->regmap, ES8326_ADC1_SRC, 0x00);
679 regmap_write(es8326->regmap, ES8326_ADC2_SRC, 0x00);
680 regmap_update_bits(es8326->regmap, ES8326_PGA_PDN,
682 usleep_range(10000, 15000);
686 mutex_unlock(&es8326->lock);
689 static irqreturn_t es8326_irq(int irq, void *dev_id)
691 struct es8326_priv *es8326 = dev_id;
692 struct snd_soc_component *comp = es8326->component;
697 es8326_enable_micbias(comp);
699 if (es8326->jack->status & SND_JACK_HEADSET)
700 queue_delayed_work(system_wq, &es8326->jack_detect_work,
701 msecs_to_jiffies(10));
703 queue_delayed_work(system_wq, &es8326->jack_detect_work,
704 msecs_to_jiffies(600));
710 static int es8326_calibrate(struct snd_soc_component *component)
712 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
714 unsigned int offset_l, offset_r;
716 regmap_read(es8326->regmap, ES8326_CHIP_VERSION, ®);
717 es8326->version = reg;
719 if ((es8326->version == ES8326_VERSION_B) && (es8326->calibrated == false)) {
720 dev_dbg(component->dev, "ES8326_VERSION_B, calibrating\n");
721 regmap_write(es8326->regmap, ES8326_CLK_INV, 0xc0);
722 regmap_write(es8326->regmap, ES8326_CLK_DIV1, 0x01);
723 regmap_write(es8326->regmap, ES8326_CLK_DLL, 0x30);
724 regmap_write(es8326->regmap, ES8326_CLK_MUX, 0xed);
725 regmap_write(es8326->regmap, ES8326_CLK_TRI, 0xc1);
726 regmap_write(es8326->regmap, ES8326_DAC_MUTE, 0x03);
727 regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7f);
728 regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x33);
729 regmap_write(es8326->regmap, ES8326_DAC2HPMIX, 0x88);
730 regmap_write(es8326->regmap, ES8326_HP_VOL, 0x80);
731 regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
732 regmap_write(es8326->regmap, ES8326_RESET, 0xc0);
733 usleep_range(15000, 20000);
735 regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, ES8326_HP_OFF);
736 regmap_read(es8326->regmap, ES8326_CSM_MUTE_STA, ®);
737 if ((reg & 0xf0) != 0x40)
740 regmap_write(es8326->regmap, ES8326_HP_CAL, 0xd4);
742 regmap_write(es8326->regmap, ES8326_HP_CAL, 0x4d);
744 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
745 regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l);
746 regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r);
747 regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
748 regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l);
749 regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r);
750 regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00);
752 es8326->calibrated = true;
758 static int es8326_resume(struct snd_soc_component *component)
760 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
762 regcache_cache_only(es8326->regmap, false);
763 regcache_sync(es8326->regmap);
765 /* reset internal clock state */
766 regmap_write(es8326->regmap, ES8326_RESET, 0x1f);
767 regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x0E);
768 usleep_range(10000, 15000);
769 regmap_write(es8326->regmap, ES8326_HPJACK_TIMER, 0x88);
770 /* set headphone default type and detect pin */
771 regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x81);
772 regmap_write(es8326->regmap, ES8326_CLK_RESAMPLE, 0x05);
774 /* set internal oscillator as clock source of headpone cp */
775 regmap_write(es8326->regmap, ES8326_CLK_DIV_CPC, 0x84);
776 regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_ON);
777 /* clock manager reset release */
778 regmap_write(es8326->regmap, ES8326_RESET, 0x17);
779 /* set headphone detection as half scan mode */
780 regmap_write(es8326->regmap, ES8326_HP_MISC, 0x08);
781 regmap_write(es8326->regmap, ES8326_PULLUP_CTL, 0x00);
783 /* enable headphone driver */
784 regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa7);
785 usleep_range(2000, 5000);
786 regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0xab);
787 usleep_range(2000, 5000);
788 regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0xbb);
789 usleep_range(2000, 5000);
790 regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa1);
792 regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00);
793 regmap_write(es8326->regmap, ES8326_CLK_VMIDS1, 0xc4);
794 regmap_write(es8326->regmap, ES8326_CLK_VMIDS2, 0x81);
795 regmap_write(es8326->regmap, ES8326_CLK_CAL_TIME, 0x00);
796 /* calibrate for B version */
797 es8326_calibrate(component);
798 /* turn off headphone out */
799 regmap_write(es8326->regmap, ES8326_HP_CAL, 0x00);
800 /* set ADC and DAC in low power mode */
801 regmap_write(es8326->regmap, ES8326_ANA_LP, 0xf0);
803 /* force micbias on */
804 regmap_write(es8326->regmap, ES8326_ANA_MICBIAS, 0x4f);
805 regmap_write(es8326->regmap, ES8326_SYS_BIAS, 0x08);
806 regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7F);
807 /* select vdda as micbias source */
808 regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x23);
809 /* set dac dsmclip = 1 */
810 regmap_write(es8326->regmap, ES8326_DAC_DSM, 0x08);
811 regmap_write(es8326->regmap, ES8326_DAC_VPPSCALE, 0x15);
813 regmap_write(es8326->regmap, ES8326_INT_SOURCE,
814 (ES8326_INT_SRC_PIN9 | ES8326_INT_SRC_BUTTON));
815 regmap_write(es8326->regmap, ES8326_INTOUT_IO,
816 es8326->interrupt_clk);
817 regmap_write(es8326->regmap, ES8326_SDINOUT1_IO,
818 (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT));
819 regmap_write(es8326->regmap, ES8326_SDINOUT23_IO, ES8326_IO_INPUT);
821 regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x3b);
822 regmap_write(es8326->regmap, ES8326_RESET, ES8326_CSM_ON);
823 regmap_update_bits(es8326->regmap, ES8326_PGAGAIN, ES8326_MIC_SEL_MASK,
826 regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE, ES8326_MUTE_MASK,
829 regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x80 |
830 ((es8326->version == ES8326_VERSION_B) ?
831 (ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol) :
832 (ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol | 0x04)));
834 es8326->jack_remove_retry = 0;
839 static int es8326_suspend(struct snd_soc_component *component)
841 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
843 cancel_delayed_work_sync(&es8326->jack_detect_work);
844 es8326_disable_micbias(component);
845 es8326->calibrated = false;
846 regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_OFF);
847 regcache_cache_only(es8326->regmap, true);
848 regcache_mark_dirty(es8326->regmap);
850 /* reset register value to default */
851 regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x01);
852 usleep_range(1000, 3000);
853 regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x00);
857 static int es8326_probe(struct snd_soc_component *component)
859 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
862 es8326->component = component;
863 es8326->jd_inverted = device_property_read_bool(component->dev,
864 "everest,jack-detect-inverted");
866 ret = device_property_read_u8(component->dev, "everest,mic1-src", &es8326->mic1_src);
868 dev_dbg(component->dev, "mic1-src return %d", ret);
869 es8326->mic1_src = ES8326_ADC_AMIC;
871 dev_dbg(component->dev, "mic1-src %x", es8326->mic1_src);
873 ret = device_property_read_u8(component->dev, "everest,mic2-src", &es8326->mic2_src);
875 dev_dbg(component->dev, "mic2-src return %d", ret);
876 es8326->mic2_src = ES8326_ADC_DMIC;
878 dev_dbg(component->dev, "mic2-src %x", es8326->mic2_src);
880 ret = device_property_read_u8(component->dev, "everest,jack-pol", &es8326->jack_pol);
882 dev_dbg(component->dev, "jack-pol return %d", ret);
883 es8326->jack_pol = ES8326_HP_TYPE_AUTO;
885 dev_dbg(component->dev, "jack-pol %x", es8326->jack_pol);
887 ret = device_property_read_u8(component->dev, "everest,interrupt-src",
888 &es8326->interrupt_src);
890 dev_dbg(component->dev, "interrupt-src return %d", ret);
891 es8326->interrupt_src = ES8326_HP_DET_SRC_PIN9;
893 dev_dbg(component->dev, "interrupt-src %x", es8326->interrupt_src);
895 ret = device_property_read_u8(component->dev, "everest,interrupt-clk",
896 &es8326->interrupt_clk);
898 dev_dbg(component->dev, "interrupt-clk return %d", ret);
899 es8326->interrupt_clk = 0x45;
901 dev_dbg(component->dev, "interrupt-clk %x", es8326->interrupt_clk);
903 es8326_resume(component);
907 static void es8326_enable_jack_detect(struct snd_soc_component *component,
908 struct snd_soc_jack *jack)
910 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
912 mutex_lock(&es8326->lock);
913 if (es8326->jd_inverted)
914 snd_soc_component_update_bits(component, ES8326_HPDET_TYPE,
915 ES8326_HP_DET_JACK_POL, ~es8326->jack_pol);
918 mutex_unlock(&es8326->lock);
919 es8326_irq(es8326->irq, es8326);
922 static void es8326_disable_jack_detect(struct snd_soc_component *component)
924 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
926 dev_dbg(component->dev, "Enter into %s\n", __func__);
928 return; /* Already disabled (or never enabled) */
929 cancel_delayed_work_sync(&es8326->jack_detect_work);
931 mutex_lock(&es8326->lock);
932 if (es8326->jack->status & SND_JACK_MICROPHONE) {
933 es8326_disable_micbias(component);
934 snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
937 mutex_unlock(&es8326->lock);
940 static int es8326_set_jack(struct snd_soc_component *component,
941 struct snd_soc_jack *jack, void *data)
944 es8326_enable_jack_detect(component, jack);
946 es8326_disable_jack_detect(component);
951 static void es8326_remove(struct snd_soc_component *component)
953 es8326_disable_jack_detect(component);
954 es8326_set_bias_level(component, SND_SOC_BIAS_OFF);
957 static const struct snd_soc_component_driver soc_component_dev_es8326 = {
958 .probe = es8326_probe,
959 .remove = es8326_remove,
960 .resume = es8326_resume,
961 .suspend = es8326_suspend,
962 .set_bias_level = es8326_set_bias_level,
963 .set_jack = es8326_set_jack,
964 .dapm_widgets = es8326_dapm_widgets,
965 .num_dapm_widgets = ARRAY_SIZE(es8326_dapm_widgets),
966 .dapm_routes = es8326_dapm_routes,
967 .num_dapm_routes = ARRAY_SIZE(es8326_dapm_routes),
968 .controls = es8326_snd_controls,
969 .num_controls = ARRAY_SIZE(es8326_snd_controls),
970 .use_pmdown_time = 1,
974 static int es8326_i2c_probe(struct i2c_client *i2c)
976 struct es8326_priv *es8326;
979 es8326 = devm_kzalloc(&i2c->dev, sizeof(struct es8326_priv), GFP_KERNEL);
983 i2c_set_clientdata(i2c, es8326);
985 mutex_init(&es8326->lock);
986 es8326->regmap = devm_regmap_init_i2c(i2c, &es8326_regmap_config);
987 if (IS_ERR(es8326->regmap)) {
988 ret = PTR_ERR(es8326->regmap);
989 dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
993 es8326->irq = i2c->irq;
994 INIT_DELAYED_WORK(&es8326->jack_detect_work,
995 es8326_jack_detect_handler);
996 INIT_DELAYED_WORK(&es8326->button_press_work,
997 es8326_jack_button_handler);
998 /* ES8316 is level-based while ES8326 is edge-based */
999 ret = devm_request_threaded_irq(&i2c->dev, es8326->irq, NULL, es8326_irq,
1000 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1003 dev_warn(&i2c->dev, "Failed to request IRQ: %d: %d\n",
1005 es8326->irq = -ENXIO;
1008 es8326->mclk = devm_clk_get_optional(&i2c->dev, "mclk");
1009 if (IS_ERR(es8326->mclk)) {
1010 dev_err(&i2c->dev, "unable to get mclk\n");
1011 return PTR_ERR(es8326->mclk);
1014 dev_warn(&i2c->dev, "assuming static mclk\n");
1016 ret = clk_prepare_enable(es8326->mclk);
1018 dev_err(&i2c->dev, "unable to enable mclk\n");
1021 return devm_snd_soc_register_component(&i2c->dev,
1022 &soc_component_dev_es8326,
1026 static const struct i2c_device_id es8326_i2c_id[] = {
1030 MODULE_DEVICE_TABLE(i2c, es8326_i2c_id);
1033 static const struct of_device_id es8326_of_match[] = {
1034 { .compatible = "everest,es8326", },
1037 MODULE_DEVICE_TABLE(of, es8326_of_match);
1041 static const struct acpi_device_id es8326_acpi_match[] = {
1045 MODULE_DEVICE_TABLE(acpi, es8326_acpi_match);
1048 static struct i2c_driver es8326_i2c_driver = {
1051 .acpi_match_table = ACPI_PTR(es8326_acpi_match),
1052 .of_match_table = of_match_ptr(es8326_of_match),
1054 .probe = es8326_i2c_probe,
1055 .id_table = es8326_i2c_id,
1057 module_i2c_driver(es8326_i2c_driver);
1059 MODULE_DESCRIPTION("ASoC es8326 driver");
1060 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
1061 MODULE_LICENSE("GPL");