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 int es8326_crosstalk1_get(struct snd_kcontrol *kcontrol,
49 struct snd_ctl_elem_value *ucontrol)
51 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
52 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
53 unsigned int crosstalk_h, crosstalk_l;
54 unsigned int crosstalk;
56 regmap_read(es8326->regmap, ES8326_DAC_RAMPRATE, &crosstalk_h);
57 regmap_read(es8326->regmap, ES8326_DAC_CROSSTALK, &crosstalk_l);
60 crosstalk = crosstalk_h >> 1 | crosstalk_l >> 4;
61 ucontrol->value.integer.value[0] = crosstalk;
66 static int es8326_crosstalk1_set(struct snd_kcontrol *kcontrol,
67 struct snd_ctl_elem_value *ucontrol)
69 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
70 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
71 unsigned int crosstalk_h, crosstalk_l;
72 unsigned int crosstalk;
74 crosstalk = ucontrol->value.integer.value[0];
75 regmap_read(es8326->regmap, ES8326_DAC_CROSSTALK, &crosstalk_l);
76 crosstalk_h = (crosstalk & 0x10) << 1;
78 crosstalk_l |= (crosstalk & 0x0f) << 4;
79 regmap_update_bits(es8326->regmap, ES8326_DAC_RAMPRATE,
81 regmap_write(es8326->regmap, ES8326_DAC_CROSSTALK, crosstalk_l);
86 static int es8326_crosstalk2_get(struct snd_kcontrol *kcontrol,
87 struct snd_ctl_elem_value *ucontrol)
89 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
90 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
91 unsigned int crosstalk_h, crosstalk_l;
92 unsigned int crosstalk;
94 regmap_read(es8326->regmap, ES8326_DAC_RAMPRATE, &crosstalk_h);
95 regmap_read(es8326->regmap, ES8326_DAC_CROSSTALK, &crosstalk_l);
98 crosstalk = crosstalk_h | crosstalk_l;
99 ucontrol->value.integer.value[0] = crosstalk;
104 static int es8326_crosstalk2_set(struct snd_kcontrol *kcontrol,
105 struct snd_ctl_elem_value *ucontrol)
107 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
108 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
109 unsigned int crosstalk_h, crosstalk_l;
110 unsigned int crosstalk;
112 crosstalk = ucontrol->value.integer.value[0];
113 regmap_read(es8326->regmap, ES8326_DAC_CROSSTALK, &crosstalk_l);
114 crosstalk_h = crosstalk & 0x10;
116 crosstalk_l |= crosstalk & 0x0f;
117 regmap_update_bits(es8326->regmap, ES8326_DAC_RAMPRATE,
119 regmap_write(es8326->regmap, ES8326_DAC_CROSSTALK, crosstalk_l);
124 static int es8326_hplvol_get(struct snd_kcontrol *kcontrol,
125 struct snd_ctl_elem_value *ucontrol)
127 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
128 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
130 ucontrol->value.integer.value[0] = es8326->hpl_vol;
135 static int es8326_hplvol_set(struct snd_kcontrol *kcontrol,
136 struct snd_ctl_elem_value *ucontrol)
138 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
139 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
142 hp_vol = ucontrol->value.integer.value[0];
145 if (es8326->hpl_vol != hp_vol) {
146 es8326->hpl_vol = hp_vol;
149 regmap_update_bits(es8326->regmap, ES8326_HP_VOL,
150 0x70, (hp_vol << 4));
157 static int es8326_hprvol_get(struct snd_kcontrol *kcontrol,
158 struct snd_ctl_elem_value *ucontrol)
160 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
161 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
163 ucontrol->value.integer.value[0] = es8326->hpr_vol;
168 static int es8326_hprvol_set(struct snd_kcontrol *kcontrol,
169 struct snd_ctl_elem_value *ucontrol)
171 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
172 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
175 hp_vol = ucontrol->value.integer.value[0];
178 if (es8326->hpr_vol != hp_vol) {
179 es8326->hpr_vol = hp_vol;
182 regmap_update_bits(es8326->regmap, ES8326_HP_VOL,
190 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9550, 50, 0);
191 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9550, 50, 0);
192 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_analog_pga_tlv, 0, 300, 0);
193 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_pga_tlv, 0, 600, 0);
194 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(softramp_rate, 0, 100, 0);
195 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_target_tlv, -3200, 200, 0);
196 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_recovery_tlv, -125, 250, 0);
198 static const char *const winsize[] = {
217 static const char *const dacpol_txt[] = {
218 "Normal", "R Invert", "L Invert", "L + R Invert" };
220 static const char *const hp_spkvol_switch[] = {
221 "HPVOL: HPL+HPL, SPKVOL: HPL+HPL",
222 "HPVOL: HPL+HPR, SPKVOL: HPL+HPR",
223 "HPVOL: HPL+HPL, SPKVOL: SPKL+SPKR",
224 "HPVOL: HPL+HPR, SPKVOL: SPKL+SPKR",
227 static const struct soc_enum dacpol =
228 SOC_ENUM_SINGLE(ES8326_DAC_DSM, 4, 4, dacpol_txt);
229 static const struct soc_enum alc_winsize =
230 SOC_ENUM_SINGLE(ES8326_ADC_RAMPRATE, 4, 16, winsize);
231 static const struct soc_enum drc_winsize =
232 SOC_ENUM_SINGLE(ES8326_DRC_WINSIZE, 4, 16, winsize);
233 static const struct soc_enum hpvol_spkvol_switch =
234 SOC_ENUM_SINGLE(ES8326_HP_MISC, 6, 4, hp_spkvol_switch);
236 static const struct snd_kcontrol_new es8326_snd_controls[] = {
237 SOC_SINGLE_TLV("DAC Playback Volume", ES8326_DACL_VOL, 0, 0xbf, 0, dac_vol_tlv),
238 SOC_ENUM("Playback Polarity", dacpol),
239 SOC_SINGLE_TLV("DAC Ramp Rate", ES8326_DAC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
240 SOC_SINGLE_TLV("DRC Recovery Level", ES8326_DRC_RECOVERY, 0, 4, 0, drc_recovery_tlv),
241 SOC_ENUM("DRC Winsize", drc_winsize),
242 SOC_SINGLE_TLV("DRC Target Level", ES8326_DRC_WINSIZE, 0, 0x0f, 0, drc_target_tlv),
244 SOC_DOUBLE_R_TLV("ADC Capture Volume", ES8326_ADC1_VOL, ES8326_ADC2_VOL, 0, 0xff, 0,
246 SOC_DOUBLE_TLV("ADC PGA Volume", ES8326_ADC_SCALE, 4, 0, 5, 0, adc_pga_tlv),
247 SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8326_PGAGAIN, 0, 10, 0, adc_analog_pga_tlv),
248 SOC_SINGLE_TLV("ADC Ramp Rate", ES8326_ADC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
249 SOC_SINGLE("ALC Capture Switch", ES8326_ALC_RECOVERY, 3, 1, 0),
250 SOC_SINGLE_TLV("ALC Capture Recovery Level", ES8326_ALC_LEVEL,
251 0, 4, 0, drc_recovery_tlv),
252 SOC_ENUM("ALC Capture Winsize", alc_winsize),
253 SOC_SINGLE_TLV("ALC Capture Target Level", ES8326_ALC_LEVEL,
254 0, 0x0f, 0, drc_target_tlv),
256 SOC_SINGLE_EXT("CROSSTALK1", SND_SOC_NOPM, 0, 31, 0,
257 es8326_crosstalk1_get, es8326_crosstalk1_set),
258 SOC_SINGLE_EXT("CROSSTALK2", SND_SOC_NOPM, 0, 31, 0,
259 es8326_crosstalk2_get, es8326_crosstalk2_set),
260 SOC_SINGLE_EXT("HPL Volume", SND_SOC_NOPM, 0, 5, 0,
261 es8326_hplvol_get, es8326_hplvol_set),
262 SOC_SINGLE_EXT("HPR Volume", SND_SOC_NOPM, 0, 5, 0,
263 es8326_hprvol_get, es8326_hprvol_set),
265 SOC_SINGLE_TLV("HPL Playback Volume", ES8326_DACL_VOL, 0, 0xbf, 0, dac_vol_tlv),
266 SOC_SINGLE_TLV("HPR Playback Volume", ES8326_DACR_VOL, 0, 0xbf, 0, dac_vol_tlv),
267 SOC_SINGLE_TLV("SPKL Playback Volume", ES8326_SPKL_VOL, 0, 0xbf, 0, dac_vol_tlv),
268 SOC_SINGLE_TLV("SPKR Playback Volume", ES8326_SPKR_VOL, 0, 0xbf, 0, dac_vol_tlv),
270 SOC_ENUM("HPVol SPKVol Switch", hpvol_spkvol_switch),
273 static const struct snd_soc_dapm_widget es8326_dapm_widgets[] = {
274 SND_SOC_DAPM_INPUT("MIC1"),
275 SND_SOC_DAPM_INPUT("MIC2"),
276 SND_SOC_DAPM_INPUT("MIC3"),
277 SND_SOC_DAPM_INPUT("MIC4"),
279 SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
280 SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
282 /* Digital Interface */
283 SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 0, SND_SOC_NOPM, 0, 0),
284 SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0, SND_SOC_NOPM, 0, 0),
286 /* Analog Power Supply*/
287 SND_SOC_DAPM_DAC("Right DAC", NULL, ES8326_ANA_PDN, 0, 1),
288 SND_SOC_DAPM_DAC("Left DAC", NULL, ES8326_ANA_PDN, 1, 1),
289 SND_SOC_DAPM_SUPPLY("MICBIAS1", ES8326_ANA_MICBIAS, 2, 0, NULL, 0),
290 SND_SOC_DAPM_SUPPLY("MICBIAS2", ES8326_ANA_MICBIAS, 3, 0, NULL, 0),
292 SND_SOC_DAPM_PGA("LHPMIX", ES8326_DAC2HPMIX, 7, 0, NULL, 0),
293 SND_SOC_DAPM_PGA("RHPMIX", ES8326_DAC2HPMIX, 3, 0, NULL, 0),
295 SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPOR Supply", ES8326_HP_CAL,
297 SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPOL Supply", ES8326_HP_CAL,
300 SND_SOC_DAPM_OUTPUT("HPOL"),
301 SND_SOC_DAPM_OUTPUT("HPOR"),
304 static const struct snd_soc_dapm_route es8326_dapm_routes[] = {
305 {"ADC L", NULL, "MIC1"},
306 {"ADC R", NULL, "MIC2"},
307 {"ADC L", NULL, "MIC3"},
308 {"ADC R", NULL, "MIC4"},
310 {"I2S OUT", NULL, "ADC L"},
311 {"I2S OUT", NULL, "ADC R"},
313 {"Right DAC", NULL, "I2S IN"},
314 {"Left DAC", NULL, "I2S IN"},
316 {"LHPMIX", NULL, "Left DAC"},
317 {"RHPMIX", NULL, "Right DAC"},
319 {"HPOR", NULL, "HPOR Supply"},
320 {"HPOL", NULL, "HPOL Supply"},
322 {"HPOL", NULL, "LHPMIX"},
323 {"HPOR", NULL, "RHPMIX"},
326 static bool es8326_volatile_register(struct device *dev, unsigned int reg)
329 case ES8326_HPL_OFFSET_INI:
330 case ES8326_HPR_OFFSET_INI:
331 case ES8326_HPDET_STA:
332 case ES8326_CTIA_OMTP_STA:
333 case ES8326_CSM_MUTE_STA:
340 static const struct regmap_config es8326_regmap_config = {
343 .max_register = 0xff,
344 .volatile_reg = es8326_volatile_register,
345 .cache_type = REGCACHE_RBTREE,
362 /* codec hifi mclk clock divider coefficients */
363 /* {ratio, LRCK, MCLK, REG04, REG05, REG06, REG07, REG08, REG09, REG10, REG11} */
364 static const struct _coeff_div coeff_div_v0[] = {
365 {64, 8000, 512000, 0x60, 0x01, 0x0F, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
366 {64, 16000, 1024000, 0x20, 0x00, 0x33, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
367 {64, 44100, 2822400, 0xE0, 0x00, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
368 {64, 48000, 3072000, 0xE0, 0x00, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
369 {128, 8000, 1024000, 0x60, 0x00, 0x33, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
370 {128, 16000, 2048000, 0x20, 0x00, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
371 {128, 44100, 5644800, 0xE0, 0x01, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
372 {128, 48000, 6144000, 0xE0, 0x01, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
374 {192, 32000, 6144000, 0xE0, 0x02, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
375 {256, 8000, 2048000, 0x60, 0x00, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
376 {256, 16000, 4096000, 0x20, 0x01, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
377 {256, 44100, 11289600, 0xE0, 0x00, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
378 {256, 48000, 12288000, 0xE0, 0x00, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
379 {384, 32000, 12288000, 0xE0, 0x05, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
380 {400, 48000, 19200000, 0xE9, 0x04, 0x0F, 0x6d, 0x4A, 0x0A, 0x1F, 0x1F},
382 {500, 48000, 24000000, 0xF8, 0x04, 0x3F, 0x6D, 0x4A, 0x0A, 0x1F, 0x1F},
383 {512, 8000, 4096000, 0x60, 0x01, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
384 {512, 16000, 8192000, 0x20, 0x00, 0x30, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
385 {512, 44100, 22579200, 0xE0, 0x00, 0x00, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
386 {512, 48000, 24576000, 0xE0, 0x00, 0x00, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
387 {768, 32000, 24576000, 0xE0, 0x02, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
388 {1024, 8000, 8192000, 0x60, 0x00, 0x30, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
389 {1024, 16000, 16384000, 0x20, 0x00, 0x00, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
392 static const struct _coeff_div coeff_div_v3[] = {
393 {32, 8000, 256000, 0x60, 0x00, 0x0F, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
394 {32, 16000, 512000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
395 {32, 44100, 1411200, 0x00, 0x00, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
396 {32, 48000, 1536000, 0x00, 0x00, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
397 {36, 8000, 288000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x23, 0x47},
398 {36, 16000, 576000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x23, 0x47},
399 {48, 8000, 384000, 0x60, 0x02, 0x1F, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
400 {48, 16000, 768000, 0x20, 0x02, 0x0F, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
401 {48, 48000, 2304000, 0x00, 0x02, 0x0D, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
403 {64, 8000, 512000, 0x60, 0x00, 0x35, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
404 {64, 16000, 1024000, 0x20, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
405 {64, 44100, 2822400, 0xE0, 0x00, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
406 {64, 48000, 3072000, 0xE0, 0x00, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
407 {72, 8000, 576000, 0x20, 0x00, 0x13, 0x35, 0x8A, 0x1B, 0x23, 0x47},
408 {72, 16000, 1152000, 0x20, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x23, 0x47},
409 {96, 8000, 768000, 0x60, 0x02, 0x1D, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
410 {96, 16000, 1536000, 0x20, 0x02, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
411 {100, 48000, 4800000, 0x04, 0x04, 0x3F, 0x6D, 0xB8, 0x08, 0x4f, 0x1f},
412 {125, 48000, 6000000, 0x04, 0x04, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
414 {128, 8000, 1024000, 0x60, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
415 {128, 16000, 2048000, 0x20, 0x00, 0x31, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
416 {128, 44100, 5644800, 0xE0, 0x00, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
417 {128, 48000, 6144000, 0xE0, 0x00, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
418 {144, 8000, 1152000, 0x20, 0x00, 0x03, 0x35, 0x8A, 0x1B, 0x23, 0x47},
419 {144, 16000, 2304000, 0x20, 0x00, 0x11, 0x35, 0x8A, 0x1B, 0x23, 0x47},
420 {192, 8000, 1536000, 0x60, 0x02, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
421 {192, 32000, 6144000, 0xE0, 0x02, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
422 {192, 16000, 3072000, 0x20, 0x02, 0x05, 0x75, 0xCA, 0x1B, 0x1F, 0x3F},
424 {200, 48000, 9600000, 0x04, 0x04, 0x0F, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
425 {250, 48000, 12000000, 0x04, 0x04, 0x0F, 0x2D, 0xCA, 0x0A, 0x27, 0x27},
426 {256, 8000, 2048000, 0x60, 0x00, 0x31, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
427 {256, 16000, 4096000, 0x20, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
428 {256, 44100, 11289600, 0xE0, 0x00, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
429 {256, 48000, 12288000, 0xE0, 0x00, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
430 {288, 8000, 2304000, 0x20, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x23, 0x47},
431 {384, 8000, 3072000, 0x60, 0x02, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
432 {384, 16000, 6144000, 0x20, 0x02, 0x03, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
433 {384, 32000, 12288000, 0xE0, 0x02, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
434 {384, 48000, 18432000, 0x00, 0x02, 0x01, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
436 {400, 48000, 19200000, 0xE4, 0x04, 0x35, 0x6d, 0xCA, 0x0A, 0x1F, 0x1F},
437 {500, 48000, 24000000, 0xF8, 0x04, 0x3F, 0x6D, 0xCA, 0x0A, 0x1F, 0x1F},
438 {512, 8000, 4096000, 0x60, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
439 {512, 16000, 8192000, 0x20, 0x00, 0x30, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
440 {512, 44100, 22579200, 0xE0, 0x00, 0x00, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
441 {512, 48000, 24576000, 0xE0, 0x00, 0x00, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
442 {768, 8000, 6144000, 0x60, 0x02, 0x11, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
443 {768, 16000, 12288000, 0x20, 0x02, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
444 {768, 32000, 24576000, 0xE0, 0x02, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
445 {800, 48000, 38400000, 0x00, 0x18, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
447 {1024, 8000, 8192000, 0x60, 0x00, 0x30, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
448 {1024, 16000, 16384000, 0x20, 0x00, 0x00, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
449 {1152, 16000, 18432000, 0x20, 0x08, 0x11, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
450 {1536, 8000, 12288000, 0x60, 0x02, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
451 {1536, 16000, 24576000, 0x20, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
452 {1625, 8000, 13000000, 0x0C, 0x18, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
453 {1625, 16000, 26000000, 0x0C, 0x18, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
454 {2048, 8000, 16384000, 0x60, 0x00, 0x00, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
455 {2304, 8000, 18432000, 0x40, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x5F},
456 {3072, 8000, 24576000, 0x60, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
457 {3250, 8000, 26000000, 0x0C, 0x18, 0x0F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
460 static inline int get_coeff(int mclk, int rate, int array,
461 const struct _coeff_div *coeff_div)
465 for (i = 0; i < array; i++) {
466 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
473 static int es8326_set_dai_sysclk(struct snd_soc_dai *codec_dai,
474 int clk_id, unsigned int freq, int dir)
476 struct snd_soc_component *codec = codec_dai->component;
477 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
479 es8326->sysclk = freq;
484 static int es8326_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
486 struct snd_soc_component *component = codec_dai->component;
489 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
490 case SND_SOC_DAIFMT_CBC_CFP:
491 snd_soc_component_update_bits(component, ES8326_RESET,
492 ES8326_MASTER_MODE_EN, ES8326_MASTER_MODE_EN);
494 case SND_SOC_DAIFMT_CBC_CFC:
500 /* interface format */
501 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
502 case SND_SOC_DAIFMT_I2S:
504 case SND_SOC_DAIFMT_RIGHT_J:
505 dev_err(component->dev, "Codec driver does not support right justified\n");
507 case SND_SOC_DAIFMT_LEFT_J:
508 iface |= ES8326_DAIFMT_LEFT_J;
510 case SND_SOC_DAIFMT_DSP_A:
511 iface |= ES8326_DAIFMT_DSP_A;
513 case SND_SOC_DAIFMT_DSP_B:
514 iface |= ES8326_DAIFMT_DSP_B;
520 snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DAIFMT_MASK, iface);
525 static int es8326_pcm_hw_params(struct snd_pcm_substream *substream,
526 struct snd_pcm_hw_params *params,
527 struct snd_soc_dai *dai)
529 struct snd_soc_component *component = dai->component;
530 const struct _coeff_div *coeff_div;
531 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
535 if (es8326->version == 0) {
536 coeff_div = coeff_div_v0;
537 array = ARRAY_SIZE(coeff_div_v0);
539 coeff_div = coeff_div_v3;
540 array = ARRAY_SIZE(coeff_div_v3);
542 coeff = get_coeff(es8326->sysclk, params_rate(params), array, coeff_div);
544 switch (params_format(params)) {
545 case SNDRV_PCM_FORMAT_S16_LE:
546 srate |= ES8326_S16_LE;
548 case SNDRV_PCM_FORMAT_S20_3LE:
549 srate |= ES8326_S20_3_LE;
551 case SNDRV_PCM_FORMAT_S18_3LE:
552 srate |= ES8326_S18_LE;
554 case SNDRV_PCM_FORMAT_S24_LE:
555 srate |= ES8326_S24_LE;
557 case SNDRV_PCM_FORMAT_S32_LE:
558 srate |= ES8326_S32_LE;
564 /* set iface & srate */
565 snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DATA_LEN_MASK, srate);
568 regmap_write(es8326->regmap, ES8326_CLK_DIV1,
569 coeff_div[coeff].reg4);
570 regmap_write(es8326->regmap, ES8326_CLK_DIV2,
571 coeff_div[coeff].reg5);
572 regmap_write(es8326->regmap, ES8326_CLK_DLL,
573 coeff_div[coeff].reg6);
574 regmap_write(es8326->regmap, ES8326_CLK_MUX,
575 coeff_div[coeff].reg7);
576 regmap_write(es8326->regmap, ES8326_CLK_ADC_SEL,
577 coeff_div[coeff].reg8);
578 regmap_write(es8326->regmap, ES8326_CLK_DAC_SEL,
579 coeff_div[coeff].reg9);
580 regmap_write(es8326->regmap, ES8326_CLK_ADC_OSR,
581 coeff_div[coeff].rega);
582 regmap_write(es8326->regmap, ES8326_CLK_DAC_OSR,
583 coeff_div[coeff].regb);
585 dev_warn(component->dev, "Clock coefficients do not match");
591 static int es8326_mute(struct snd_soc_dai *dai, int mute, int direction)
593 struct snd_soc_component *component = dai->component;
594 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
595 unsigned int offset_l, offset_r;
598 if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
599 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
600 regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE,
601 ES8326_MUTE_MASK, ES8326_MUTE);
602 regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF,
605 regmap_update_bits(es8326->regmap, ES8326_ADC_MUTE,
609 if (!es8326->calibrated) {
610 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_FORCE_CAL);
612 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
613 regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l);
614 regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r);
615 regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
616 regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l);
617 regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r);
618 es8326->calibrated = true;
620 if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
621 regmap_update_bits(es8326->regmap, ES8326_DAC_DSM, 0x01, 0x01);
622 usleep_range(1000, 5000);
623 regmap_update_bits(es8326->regmap, ES8326_DAC_DSM, 0x01, 0x00);
624 usleep_range(1000, 5000);
625 regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x30, 0x20);
626 regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x30, 0x30);
627 regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa1);
628 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_ON);
629 regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE,
630 ES8326_MUTE_MASK, ~(ES8326_MUTE));
633 regmap_update_bits(es8326->regmap, ES8326_ADC_MUTE,
640 static int es8326_set_bias_level(struct snd_soc_component *codec,
641 enum snd_soc_bias_level level)
643 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
647 case SND_SOC_BIAS_ON:
648 ret = clk_prepare_enable(es8326->mclk);
652 regmap_update_bits(es8326->regmap, ES8326_RESET, 0x02, 0x02);
653 usleep_range(5000, 10000);
654 regmap_write(es8326->regmap, ES8326_INTOUT_IO, es8326->interrupt_clk);
655 regmap_write(es8326->regmap, ES8326_SDINOUT1_IO,
656 (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT));
657 regmap_write(es8326->regmap, ES8326_PGA_PDN, 0x40);
658 regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x00);
659 regmap_update_bits(es8326->regmap, ES8326_CLK_CTL, 0x20, 0x20);
660 regmap_update_bits(es8326->regmap, ES8326_RESET, 0x02, 0x00);
662 case SND_SOC_BIAS_PREPARE:
664 case SND_SOC_BIAS_STANDBY:
665 regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x3b);
666 regmap_update_bits(es8326->regmap, ES8326_CLK_CTL, 0x20, 0x00);
667 regmap_write(es8326->regmap, ES8326_SDINOUT1_IO, ES8326_IO_INPUT);
669 case SND_SOC_BIAS_OFF:
670 clk_disable_unprepare(es8326->mclk);
677 #define es8326_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
678 SNDRV_PCM_FMTBIT_S24_LE)
680 static const struct snd_soc_dai_ops es8326_ops = {
681 .hw_params = es8326_pcm_hw_params,
682 .set_fmt = es8326_set_dai_fmt,
683 .set_sysclk = es8326_set_dai_sysclk,
684 .mute_stream = es8326_mute,
685 .no_capture_mute = 0,
688 static struct snd_soc_dai_driver es8326_dai = {
689 .name = "ES8326 HiFi",
691 .stream_name = "Playback",
694 .rates = SNDRV_PCM_RATE_8000_48000,
695 .formats = es8326_FORMATS,
698 .stream_name = "Capture",
701 .rates = SNDRV_PCM_RATE_8000_48000,
702 .formats = es8326_FORMATS,
708 static void es8326_enable_micbias(struct snd_soc_component *component)
710 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
712 snd_soc_dapm_mutex_lock(dapm);
713 snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS1");
714 snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS2");
715 snd_soc_dapm_sync_unlocked(dapm);
716 snd_soc_dapm_mutex_unlock(dapm);
719 static void es8326_disable_micbias(struct snd_soc_component *component)
721 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
723 snd_soc_dapm_mutex_lock(dapm);
724 snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS1");
725 snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS2");
726 snd_soc_dapm_sync_unlocked(dapm);
727 snd_soc_dapm_mutex_unlock(dapm);
731 * For button detection, set the following in soundcard
732 * snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
733 * snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
734 * snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
736 static void es8326_jack_button_handler(struct work_struct *work)
738 struct es8326_priv *es8326 =
739 container_of(work, struct es8326_priv, button_press_work.work);
740 struct snd_soc_component *comp = es8326->component;
742 static int button_to_report, press_count;
743 static int prev_button, cur_button;
745 if (!(es8326->jack->status & SND_JACK_HEADSET)) /* Jack unplugged */
748 mutex_lock(&es8326->lock);
749 iface = snd_soc_component_read(comp, ES8326_HPDET_STA);
752 /* pause button detected */
753 cur_button = SND_JACK_BTN_0;
757 /* button volume up */
758 cur_button = SND_JACK_BTN_1;
761 /* button volume down */
762 cur_button = SND_JACK_BTN_2;
766 /* button released or not pressed */
773 if ((prev_button == cur_button) && (cur_button != 0)) {
775 if (press_count > 3) {
776 /* report a press every 120ms */
777 snd_soc_jack_report(es8326->jack, cur_button,
778 SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
781 button_to_report = cur_button;
782 queue_delayed_work(system_wq, &es8326->button_press_work,
783 msecs_to_jiffies(35));
784 } else if (prev_button != cur_button) {
785 /* mismatch, detect again */
786 prev_button = cur_button;
787 queue_delayed_work(system_wq, &es8326->button_press_work,
788 msecs_to_jiffies(35));
790 /* released or no pressed */
791 if (button_to_report != 0) {
792 snd_soc_jack_report(es8326->jack, button_to_report,
793 SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
794 snd_soc_jack_report(es8326->jack, 0,
795 SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
796 button_to_report = 0;
799 mutex_unlock(&es8326->lock);
802 static void es8326_jack_detect_handler(struct work_struct *work)
804 struct es8326_priv *es8326 =
805 container_of(work, struct es8326_priv, jack_detect_work.work);
806 struct snd_soc_component *comp = es8326->component;
809 mutex_lock(&es8326->lock);
810 iface = snd_soc_component_read(comp, ES8326_HPDET_STA);
811 dev_dbg(comp->dev, "gpio flag %#04x", iface);
813 if ((es8326->jack_remove_retry == 1) && (es8326->version != ES8326_VERSION_B)) {
814 if (iface & ES8326_HPINSERT_FLAG)
815 es8326->jack_remove_retry = 2;
817 es8326->jack_remove_retry = 0;
819 dev_dbg(comp->dev, "remove event check, set HPJACK_POL normal, cnt = %d\n",
820 es8326->jack_remove_retry);
822 * Inverted HPJACK_POL bit to trigger one IRQ to double check HP Removal event
824 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE,
825 ES8326_HP_DET_JACK_POL, (es8326->jd_inverted ?
826 ~es8326->jack_pol : es8326->jack_pol));
830 if ((iface & ES8326_HPINSERT_FLAG) == 0) {
831 /* Jack unplugged or spurious IRQ */
832 dev_dbg(comp->dev, "No headset detected\n");
833 es8326_disable_micbias(es8326->component);
834 if (es8326->jack->status & SND_JACK_HEADPHONE) {
835 dev_dbg(comp->dev, "Report hp remove event\n");
836 snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
837 /* mute adc when mic path switch */
838 regmap_write(es8326->regmap, ES8326_ADC_SCALE, 0x33);
839 regmap_write(es8326->regmap, ES8326_ADC1_SRC, 0x44);
840 regmap_write(es8326->regmap, ES8326_ADC2_SRC, 0x66);
843 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x01);
844 regmap_write(es8326->regmap, ES8326_SYS_BIAS, 0x0a);
845 regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x0f, 0x03);
847 * Inverted HPJACK_POL bit to trigger one IRQ to double check HP Removal event
849 if ((es8326->jack_remove_retry == 0) && (es8326->version != ES8326_VERSION_B)) {
850 es8326->jack_remove_retry = 1;
851 dev_dbg(comp->dev, "remove event check, invert HPJACK_POL, cnt = %d\n",
852 es8326->jack_remove_retry);
853 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE,
854 ES8326_HP_DET_JACK_POL, (es8326->jd_inverted ?
855 es8326->jack_pol : ~es8326->jack_pol));
858 es8326->jack_remove_retry = 0;
860 } else if ((iface & ES8326_HPINSERT_FLAG) == ES8326_HPINSERT_FLAG) {
861 es8326->jack_remove_retry = 0;
862 if (es8326->hp == 0) {
863 dev_dbg(comp->dev, "First insert, start OMTP/CTIA type check\n");
865 * set auto-check mode, then restart jack_detect_work after 400ms.
866 * Don't report jack status.
868 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x01);
869 es8326_enable_micbias(es8326->component);
870 usleep_range(50000, 70000);
871 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x00);
872 regmap_write(es8326->regmap, ES8326_SYS_BIAS, 0x1f);
873 regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x0f, 0x08);
874 queue_delayed_work(system_wq, &es8326->jack_detect_work,
875 msecs_to_jiffies(400));
879 if (es8326->jack->status & SND_JACK_HEADSET) {
881 dev_dbg(comp->dev, "button pressed\n");
882 queue_delayed_work(system_wq, &es8326->button_press_work, 10);
885 if ((iface & ES8326_HPBUTTON_FLAG) == 0x01) {
886 dev_dbg(comp->dev, "Headphone detected\n");
887 snd_soc_jack_report(es8326->jack,
888 SND_JACK_HEADPHONE, SND_JACK_HEADSET);
890 dev_dbg(comp->dev, "Headset detected\n");
891 snd_soc_jack_report(es8326->jack,
892 SND_JACK_HEADSET, SND_JACK_HEADSET);
894 regmap_write(es8326->regmap, ES8326_ADC_SCALE, 0x33);
895 regmap_update_bits(es8326->regmap, ES8326_PGA_PDN,
897 regmap_update_bits(es8326->regmap, ES8326_PGAGAIN,
899 regmap_write(es8326->regmap, ES8326_ADC1_SRC, 0x00);
900 regmap_write(es8326->regmap, ES8326_ADC2_SRC, 0x00);
901 regmap_update_bits(es8326->regmap, ES8326_PGA_PDN,
903 usleep_range(10000, 15000);
907 mutex_unlock(&es8326->lock);
910 static irqreturn_t es8326_irq(int irq, void *dev_id)
912 struct es8326_priv *es8326 = dev_id;
917 if (es8326->jack->status & SND_JACK_HEADSET)
918 queue_delayed_work(system_wq, &es8326->jack_detect_work,
919 msecs_to_jiffies(10));
921 queue_delayed_work(system_wq, &es8326->jack_detect_work,
922 msecs_to_jiffies(300));
928 static int es8326_calibrate(struct snd_soc_component *component)
930 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
932 unsigned int offset_l, offset_r;
934 regmap_read(es8326->regmap, ES8326_CHIP_VERSION, ®);
935 es8326->version = reg;
937 if ((es8326->version == ES8326_VERSION_B) && (es8326->calibrated == false)) {
938 dev_dbg(component->dev, "ES8326_VERSION_B, calibrating\n");
939 regmap_write(es8326->regmap, ES8326_CLK_INV, 0xc0);
940 regmap_write(es8326->regmap, ES8326_CLK_DIV1, 0x03);
941 regmap_write(es8326->regmap, ES8326_CLK_DLL, 0x30);
942 regmap_write(es8326->regmap, ES8326_CLK_MUX, 0xed);
943 regmap_write(es8326->regmap, ES8326_CLK_DAC_SEL, 0x08);
944 regmap_write(es8326->regmap, ES8326_CLK_TRI, 0xc1);
945 regmap_write(es8326->regmap, ES8326_DAC_MUTE, 0x03);
946 regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7f);
947 regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x23);
948 regmap_write(es8326->regmap, ES8326_DAC2HPMIX, 0x88);
949 usleep_range(15000, 20000);
950 regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
951 usleep_range(15000, 20000);
952 regmap_write(es8326->regmap, ES8326_RESET, 0xc0);
953 usleep_range(15000, 20000);
955 regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, ES8326_HP_OFF);
956 regmap_read(es8326->regmap, ES8326_CSM_MUTE_STA, ®);
957 if ((reg & 0xf0) != 0x40)
960 regmap_write(es8326->regmap, ES8326_HP_CAL, 0xd4);
962 regmap_write(es8326->regmap, ES8326_HP_CAL, 0x4d);
964 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
965 regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l);
966 regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r);
967 regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
968 regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l);
969 regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r);
970 regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00);
972 es8326->calibrated = true;
978 static int es8326_resume(struct snd_soc_component *component)
980 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
982 regcache_cache_only(es8326->regmap, false);
983 regcache_sync(es8326->regmap);
985 /* reset internal clock state */
986 regmap_write(es8326->regmap, ES8326_RESET, 0x1f);
987 regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x0E);
988 regmap_write(es8326->regmap, ES8326_ANA_LP, 0xf0);
989 usleep_range(10000, 15000);
990 regmap_write(es8326->regmap, ES8326_HPJACK_TIMER, 0xe9);
991 regmap_write(es8326->regmap, ES8326_ANA_MICBIAS, 0xcb);
992 /* set headphone default type and detect pin */
993 regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x83);
994 regmap_write(es8326->regmap, ES8326_CLK_RESAMPLE, 0x05);
996 /* set internal oscillator as clock source of headpone cp */
997 regmap_write(es8326->regmap, ES8326_CLK_DIV_CPC, 0x89);
998 regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_ON);
999 /* clock manager reset release */
1000 regmap_write(es8326->regmap, ES8326_RESET, 0x17);
1001 /* set headphone detection as half scan mode */
1002 regmap_write(es8326->regmap, ES8326_HP_MISC, 0x3d);
1003 regmap_write(es8326->regmap, ES8326_PULLUP_CTL, 0x00);
1005 /* enable headphone driver */
1006 regmap_write(es8326->regmap, ES8326_HP_VOL, 0xc4);
1007 regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa7);
1008 usleep_range(2000, 5000);
1009 regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0x23);
1010 regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0x33);
1011 regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa1);
1013 regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00);
1014 regmap_write(es8326->regmap, ES8326_CLK_VMIDS1, 0xc4);
1015 regmap_write(es8326->regmap, ES8326_CLK_VMIDS2, 0x81);
1016 regmap_write(es8326->regmap, ES8326_CLK_CAL_TIME, 0x00);
1017 /* calibrate for B version */
1018 es8326_calibrate(component);
1019 regmap_write(es8326->regmap, ES8326_DAC_CROSSTALK, 0xaa);
1020 regmap_write(es8326->regmap, ES8326_DAC_RAMPRATE, 0x00);
1021 /* turn off headphone out */
1022 regmap_write(es8326->regmap, ES8326_HP_CAL, 0x00);
1023 /* set ADC and DAC in low power mode */
1024 regmap_write(es8326->regmap, ES8326_ANA_LP, 0xf0);
1026 regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7F);
1027 /* select vdda as micbias source */
1028 regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x23);
1029 /* set dac dsmclip = 1 */
1030 regmap_write(es8326->regmap, ES8326_DAC_DSM, 0x08);
1031 regmap_write(es8326->regmap, ES8326_DAC_VPPSCALE, 0x15);
1033 regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x80 |
1034 ((es8326->version == ES8326_VERSION_B) ?
1035 (ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol) :
1036 (ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol | 0x04)));
1037 usleep_range(5000, 10000);
1038 es8326_enable_micbias(es8326->component);
1039 usleep_range(50000, 70000);
1040 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x00);
1041 regmap_write(es8326->regmap, ES8326_INT_SOURCE,
1042 (ES8326_INT_SRC_PIN9 | ES8326_INT_SRC_BUTTON));
1043 regmap_write(es8326->regmap, ES8326_INTOUT_IO,
1044 es8326->interrupt_clk);
1045 regmap_write(es8326->regmap, ES8326_SDINOUT1_IO,
1046 (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT));
1047 regmap_write(es8326->regmap, ES8326_SDINOUT23_IO, ES8326_IO_INPUT);
1049 regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x00);
1050 regmap_write(es8326->regmap, ES8326_RESET, ES8326_CSM_ON);
1051 regmap_update_bits(es8326->regmap, ES8326_PGAGAIN, ES8326_MIC_SEL_MASK,
1054 regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE, ES8326_MUTE_MASK,
1057 regmap_write(es8326->regmap, ES8326_ADC_MUTE, 0x0f);
1059 es8326->jack_remove_retry = 0;
1061 es8326->hpl_vol = 0x03;
1062 es8326->hpr_vol = 0x03;
1066 static int es8326_suspend(struct snd_soc_component *component)
1068 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
1070 cancel_delayed_work_sync(&es8326->jack_detect_work);
1071 es8326_disable_micbias(component);
1072 es8326->calibrated = false;
1073 regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_OFF);
1074 regcache_cache_only(es8326->regmap, true);
1075 regcache_mark_dirty(es8326->regmap);
1077 /* reset register value to default */
1078 regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x01);
1079 usleep_range(1000, 3000);
1080 regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x00);
1084 static int es8326_probe(struct snd_soc_component *component)
1086 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
1089 es8326->component = component;
1090 es8326->jd_inverted = device_property_read_bool(component->dev,
1091 "everest,jack-detect-inverted");
1093 ret = device_property_read_u8(component->dev, "everest,jack-pol", &es8326->jack_pol);
1095 dev_dbg(component->dev, "jack-pol return %d", ret);
1096 es8326->jack_pol = ES8326_HP_TYPE_AUTO;
1098 dev_dbg(component->dev, "jack-pol %x", es8326->jack_pol);
1100 ret = device_property_read_u8(component->dev, "everest,interrupt-src",
1101 &es8326->interrupt_src);
1103 dev_dbg(component->dev, "interrupt-src return %d", ret);
1104 es8326->interrupt_src = ES8326_HP_DET_SRC_PIN9;
1106 dev_dbg(component->dev, "interrupt-src %x", es8326->interrupt_src);
1108 ret = device_property_read_u8(component->dev, "everest,interrupt-clk",
1109 &es8326->interrupt_clk);
1111 dev_dbg(component->dev, "interrupt-clk return %d", ret);
1112 es8326->interrupt_clk = 0x00;
1114 dev_dbg(component->dev, "interrupt-clk %x", es8326->interrupt_clk);
1116 es8326_resume(component);
1120 static void es8326_enable_jack_detect(struct snd_soc_component *component,
1121 struct snd_soc_jack *jack)
1123 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
1125 mutex_lock(&es8326->lock);
1126 if (es8326->jd_inverted)
1127 snd_soc_component_update_bits(component, ES8326_HPDET_TYPE,
1128 ES8326_HP_DET_JACK_POL, ~es8326->jack_pol);
1129 es8326->jack = jack;
1131 mutex_unlock(&es8326->lock);
1132 es8326_irq(es8326->irq, es8326);
1135 static void es8326_disable_jack_detect(struct snd_soc_component *component)
1137 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
1139 dev_dbg(component->dev, "Enter into %s\n", __func__);
1141 return; /* Already disabled (or never enabled) */
1142 cancel_delayed_work_sync(&es8326->jack_detect_work);
1144 mutex_lock(&es8326->lock);
1145 if (es8326->jack->status & SND_JACK_MICROPHONE) {
1146 es8326_disable_micbias(component);
1147 snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
1149 es8326->jack = NULL;
1150 mutex_unlock(&es8326->lock);
1153 static int es8326_set_jack(struct snd_soc_component *component,
1154 struct snd_soc_jack *jack, void *data)
1157 es8326_enable_jack_detect(component, jack);
1159 es8326_disable_jack_detect(component);
1164 static void es8326_remove(struct snd_soc_component *component)
1166 es8326_disable_jack_detect(component);
1167 es8326_set_bias_level(component, SND_SOC_BIAS_OFF);
1170 static const struct snd_soc_component_driver soc_component_dev_es8326 = {
1171 .probe = es8326_probe,
1172 .remove = es8326_remove,
1173 .resume = es8326_resume,
1174 .suspend = es8326_suspend,
1175 .set_bias_level = es8326_set_bias_level,
1176 .set_jack = es8326_set_jack,
1177 .dapm_widgets = es8326_dapm_widgets,
1178 .num_dapm_widgets = ARRAY_SIZE(es8326_dapm_widgets),
1179 .dapm_routes = es8326_dapm_routes,
1180 .num_dapm_routes = ARRAY_SIZE(es8326_dapm_routes),
1181 .controls = es8326_snd_controls,
1182 .num_controls = ARRAY_SIZE(es8326_snd_controls),
1183 .use_pmdown_time = 1,
1187 static int es8326_i2c_probe(struct i2c_client *i2c)
1189 struct es8326_priv *es8326;
1192 es8326 = devm_kzalloc(&i2c->dev, sizeof(struct es8326_priv), GFP_KERNEL);
1196 i2c_set_clientdata(i2c, es8326);
1198 mutex_init(&es8326->lock);
1199 es8326->regmap = devm_regmap_init_i2c(i2c, &es8326_regmap_config);
1200 if (IS_ERR(es8326->regmap)) {
1201 ret = PTR_ERR(es8326->regmap);
1202 dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
1206 es8326->irq = i2c->irq;
1207 INIT_DELAYED_WORK(&es8326->jack_detect_work,
1208 es8326_jack_detect_handler);
1209 INIT_DELAYED_WORK(&es8326->button_press_work,
1210 es8326_jack_button_handler);
1211 /* ES8316 is level-based while ES8326 is edge-based */
1212 ret = devm_request_threaded_irq(&i2c->dev, es8326->irq, NULL, es8326_irq,
1213 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1216 dev_warn(&i2c->dev, "Failed to request IRQ: %d: %d\n",
1218 es8326->irq = -ENXIO;
1221 es8326->mclk = devm_clk_get_optional(&i2c->dev, "mclk");
1222 if (IS_ERR(es8326->mclk)) {
1223 dev_err(&i2c->dev, "unable to get mclk\n");
1224 return PTR_ERR(es8326->mclk);
1227 dev_warn(&i2c->dev, "assuming static mclk\n");
1229 ret = clk_prepare_enable(es8326->mclk);
1231 dev_err(&i2c->dev, "unable to enable mclk\n");
1234 return devm_snd_soc_register_component(&i2c->dev,
1235 &soc_component_dev_es8326,
1239 static const struct i2c_device_id es8326_i2c_id[] = {
1243 MODULE_DEVICE_TABLE(i2c, es8326_i2c_id);
1246 static const struct of_device_id es8326_of_match[] = {
1247 { .compatible = "everest,es8326", },
1250 MODULE_DEVICE_TABLE(of, es8326_of_match);
1254 static const struct acpi_device_id es8326_acpi_match[] = {
1258 MODULE_DEVICE_TABLE(acpi, es8326_acpi_match);
1261 static struct i2c_driver es8326_i2c_driver = {
1264 .acpi_match_table = ACPI_PTR(es8326_acpi_match),
1265 .of_match_table = of_match_ptr(es8326_of_match),
1267 .probe = es8326_i2c_probe,
1268 .id_table = es8326_i2c_id,
1270 module_i2c_driver(es8326_i2c_driver);
1272 MODULE_DESCRIPTION("ASoC es8326 driver");
1273 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
1274 MODULE_LICENSE("GPL");