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 const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9550, 50, 0);
125 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9550, 50, 0);
126 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_analog_pga_tlv, 0, 300, 0);
127 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_pga_tlv, 0, 600, 0);
128 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(softramp_rate, 0, 100, 0);
129 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_target_tlv, -3200, 200, 0);
130 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_recovery_tlv, -125, 250, 0);
132 static const char *const winsize[] = {
151 static const char *const dacpol_txt[] = {
152 "Normal", "R Invert", "L Invert", "L + R Invert" };
154 static const struct soc_enum dacpol =
155 SOC_ENUM_SINGLE(ES8326_DAC_DSM, 4, 4, dacpol_txt);
156 static const struct soc_enum alc_winsize =
157 SOC_ENUM_SINGLE(ES8326_ADC_RAMPRATE, 4, 16, winsize);
158 static const struct soc_enum drc_winsize =
159 SOC_ENUM_SINGLE(ES8326_DRC_WINSIZE, 4, 16, winsize);
161 static const struct snd_kcontrol_new es8326_snd_controls[] = {
162 SOC_SINGLE_TLV("DAC Playback Volume", ES8326_DAC_VOL, 0, 0xbf, 0, dac_vol_tlv),
163 SOC_ENUM("Playback Polarity", dacpol),
164 SOC_SINGLE_TLV("DAC Ramp Rate", ES8326_DAC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
165 SOC_SINGLE_TLV("DRC Recovery Level", ES8326_DRC_RECOVERY, 0, 4, 0, drc_recovery_tlv),
166 SOC_ENUM("DRC Winsize", drc_winsize),
167 SOC_SINGLE_TLV("DRC Target Level", ES8326_DRC_WINSIZE, 0, 0x0f, 0, drc_target_tlv),
169 SOC_DOUBLE_R_TLV("ADC Capture Volume", ES8326_ADC1_VOL, ES8326_ADC2_VOL, 0, 0xff, 0,
171 SOC_DOUBLE_TLV("ADC PGA Volume", ES8326_ADC_SCALE, 4, 0, 5, 0, adc_pga_tlv),
172 SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8326_PGAGAIN, 0, 10, 0, adc_analog_pga_tlv),
173 SOC_SINGLE_TLV("ADC Ramp Rate", ES8326_ADC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
174 SOC_SINGLE("ALC Capture Switch", ES8326_ALC_RECOVERY, 3, 1, 0),
175 SOC_SINGLE_TLV("ALC Capture Recovery Level", ES8326_ALC_LEVEL,
176 0, 4, 0, drc_recovery_tlv),
177 SOC_ENUM("ALC Capture Winsize", alc_winsize),
178 SOC_SINGLE_TLV("ALC Capture Target Level", ES8326_ALC_LEVEL,
179 0, 0x0f, 0, drc_target_tlv),
181 SOC_SINGLE_EXT("CROSSTALK1", SND_SOC_NOPM, 0, 31, 0,
182 es8326_crosstalk1_get, es8326_crosstalk1_set),
183 SOC_SINGLE_EXT("CROSSTALK2", SND_SOC_NOPM, 0, 31, 0,
184 es8326_crosstalk2_get, es8326_crosstalk2_set),
187 static const struct snd_soc_dapm_widget es8326_dapm_widgets[] = {
188 SND_SOC_DAPM_INPUT("MIC1"),
189 SND_SOC_DAPM_INPUT("MIC2"),
190 SND_SOC_DAPM_INPUT("MIC3"),
191 SND_SOC_DAPM_INPUT("MIC4"),
193 SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
194 SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
196 /* Digital Interface */
197 SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 0, SND_SOC_NOPM, 0, 0),
198 SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0, SND_SOC_NOPM, 0, 0),
200 /* ADC Digital Mute */
201 SND_SOC_DAPM_PGA("ADC L1", ES8326_ADC_MUTE, 0, 1, NULL, 0),
202 SND_SOC_DAPM_PGA("ADC R1", ES8326_ADC_MUTE, 1, 1, NULL, 0),
203 SND_SOC_DAPM_PGA("ADC L2", ES8326_ADC_MUTE, 2, 1, NULL, 0),
204 SND_SOC_DAPM_PGA("ADC R2", ES8326_ADC_MUTE, 3, 1, NULL, 0),
206 /* Analog Power Supply*/
207 SND_SOC_DAPM_DAC("Right DAC", NULL, ES8326_ANA_PDN, 0, 1),
208 SND_SOC_DAPM_DAC("Left DAC", NULL, ES8326_ANA_PDN, 1, 1),
209 SND_SOC_DAPM_SUPPLY("MICBIAS1", ES8326_ANA_MICBIAS, 2, 0, NULL, 0),
210 SND_SOC_DAPM_SUPPLY("MICBIAS2", ES8326_ANA_MICBIAS, 3, 0, NULL, 0),
212 SND_SOC_DAPM_PGA("LHPMIX", ES8326_DAC2HPMIX, 7, 0, NULL, 0),
213 SND_SOC_DAPM_PGA("RHPMIX", ES8326_DAC2HPMIX, 3, 0, NULL, 0),
215 SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPOR Supply", ES8326_HP_CAL,
217 SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPOL Supply", ES8326_HP_CAL,
220 SND_SOC_DAPM_OUTPUT("HPOL"),
221 SND_SOC_DAPM_OUTPUT("HPOR"),
224 static const struct snd_soc_dapm_route es8326_dapm_routes[] = {
225 {"ADC L1", NULL, "MIC1"},
226 {"ADC R1", NULL, "MIC2"},
227 {"ADC L2", NULL, "MIC3"},
228 {"ADC R2", NULL, "MIC4"},
230 {"ADC L", NULL, "ADC L1"},
231 {"ADC R", NULL, "ADC R1"},
232 {"ADC L", NULL, "ADC L2"},
233 {"ADC R", NULL, "ADC R2"},
235 {"I2S OUT", NULL, "ADC L"},
236 {"I2S OUT", NULL, "ADC R"},
238 {"Right DAC", NULL, "I2S IN"},
239 {"Left DAC", NULL, "I2S IN"},
241 {"LHPMIX", NULL, "Left DAC"},
242 {"RHPMIX", NULL, "Right DAC"},
244 {"HPOR", NULL, "HPOR Supply"},
245 {"HPOL", NULL, "HPOL Supply"},
247 {"HPOL", NULL, "LHPMIX"},
248 {"HPOR", NULL, "RHPMIX"},
251 static bool es8326_volatile_register(struct device *dev, unsigned int reg)
254 case ES8326_HPL_OFFSET_INI:
255 case ES8326_HPR_OFFSET_INI:
256 case ES8326_HPDET_STA:
257 case ES8326_CTIA_OMTP_STA:
258 case ES8326_CSM_MUTE_STA:
265 static const struct regmap_config es8326_regmap_config = {
268 .max_register = 0xff,
269 .volatile_reg = es8326_volatile_register,
270 .cache_type = REGCACHE_RBTREE,
287 /* codec hifi mclk clock divider coefficients */
288 /* {ratio, LRCK, MCLK, REG04, REG05, REG06, REG07, REG08, REG09, REG10, REG11} */
289 static const struct _coeff_div coeff_div_v0[] = {
290 {64, 8000, 512000, 0x60, 0x01, 0x0F, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
291 {64, 16000, 1024000, 0x20, 0x00, 0x33, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
292 {64, 44100, 2822400, 0xE0, 0x00, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
293 {64, 48000, 3072000, 0xE0, 0x00, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
294 {128, 8000, 1024000, 0x60, 0x00, 0x33, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
295 {128, 16000, 2048000, 0x20, 0x00, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
296 {128, 44100, 5644800, 0xE0, 0x01, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
297 {128, 48000, 6144000, 0xE0, 0x01, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
299 {192, 32000, 6144000, 0xE0, 0x02, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
300 {256, 8000, 2048000, 0x60, 0x00, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
301 {256, 16000, 4096000, 0x20, 0x01, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
302 {256, 44100, 11289600, 0xE0, 0x00, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
303 {256, 48000, 12288000, 0xE0, 0x00, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
304 {384, 32000, 12288000, 0xE0, 0x05, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
305 {400, 48000, 19200000, 0xE9, 0x04, 0x0F, 0x6d, 0x4A, 0x0A, 0x1F, 0x1F},
307 {500, 48000, 24000000, 0xF8, 0x04, 0x3F, 0x6D, 0x4A, 0x0A, 0x1F, 0x1F},
308 {512, 8000, 4096000, 0x60, 0x01, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
309 {512, 16000, 8192000, 0x20, 0x00, 0x30, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
310 {512, 44100, 22579200, 0xE0, 0x00, 0x00, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
311 {512, 48000, 24576000, 0xE0, 0x00, 0x00, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
312 {768, 32000, 24576000, 0xE0, 0x02, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
313 {1024, 8000, 8192000, 0x60, 0x00, 0x30, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
314 {1024, 16000, 16384000, 0x20, 0x00, 0x00, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
317 static const struct _coeff_div coeff_div_v3[] = {
318 {32, 8000, 256000, 0x60, 0x00, 0x0F, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
319 {32, 16000, 512000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
320 {32, 44100, 1411200, 0x00, 0x00, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
321 {32, 48000, 1536000, 0x00, 0x00, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
322 {36, 8000, 288000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x23, 0x47},
323 {36, 16000, 576000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x23, 0x47},
324 {48, 8000, 384000, 0x60, 0x02, 0x1F, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
325 {48, 16000, 768000, 0x20, 0x02, 0x0F, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
326 {48, 48000, 2304000, 0x00, 0x02, 0x0D, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
328 {64, 8000, 512000, 0x60, 0x00, 0x35, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
329 {64, 16000, 1024000, 0x20, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
330 {64, 44100, 2822400, 0xE0, 0x00, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
331 {64, 48000, 3072000, 0xE0, 0x00, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
332 {72, 8000, 576000, 0x20, 0x00, 0x13, 0x35, 0x8A, 0x1B, 0x23, 0x47},
333 {72, 16000, 1152000, 0x20, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x23, 0x47},
334 {96, 8000, 768000, 0x60, 0x02, 0x1D, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
335 {96, 16000, 1536000, 0x20, 0x02, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
336 {100, 48000, 4800000, 0x04, 0x04, 0x3F, 0x6D, 0xB8, 0x08, 0x4f, 0x1f},
337 {125, 48000, 6000000, 0x04, 0x04, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
339 {128, 8000, 1024000, 0x60, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
340 {128, 16000, 2048000, 0x20, 0x00, 0x31, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
341 {128, 44100, 5644800, 0xE0, 0x00, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
342 {128, 48000, 6144000, 0xE0, 0x00, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
343 {144, 8000, 1152000, 0x20, 0x00, 0x03, 0x35, 0x8A, 0x1B, 0x23, 0x47},
344 {144, 16000, 2304000, 0x20, 0x00, 0x11, 0x35, 0x8A, 0x1B, 0x23, 0x47},
345 {192, 8000, 1536000, 0x60, 0x02, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
346 {192, 32000, 6144000, 0xE0, 0x02, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
347 {192, 16000, 3072000, 0x20, 0x02, 0x05, 0x75, 0xCA, 0x1B, 0x1F, 0x3F},
349 {200, 48000, 9600000, 0x04, 0x04, 0x0F, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
350 {250, 48000, 12000000, 0x04, 0x04, 0x0F, 0x2D, 0xCA, 0x0A, 0x27, 0x27},
351 {256, 8000, 2048000, 0x60, 0x00, 0x31, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
352 {256, 16000, 4096000, 0x20, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
353 {256, 44100, 11289600, 0xE0, 0x00, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
354 {256, 48000, 12288000, 0xE0, 0x00, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
355 {288, 8000, 2304000, 0x20, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x23, 0x47},
356 {384, 8000, 3072000, 0x60, 0x02, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
357 {384, 16000, 6144000, 0x20, 0x02, 0x03, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
358 {384, 32000, 12288000, 0xE0, 0x02, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
359 {384, 48000, 18432000, 0x00, 0x02, 0x01, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
361 {400, 48000, 19200000, 0xE4, 0x04, 0x35, 0x6d, 0xCA, 0x0A, 0x1F, 0x1F},
362 {500, 48000, 24000000, 0xF8, 0x04, 0x3F, 0x6D, 0xCA, 0x0A, 0x1F, 0x1F},
363 {512, 8000, 4096000, 0x60, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
364 {512, 16000, 8192000, 0x20, 0x00, 0x30, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
365 {512, 44100, 22579200, 0xE0, 0x00, 0x00, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
366 {512, 48000, 24576000, 0xE0, 0x00, 0x00, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
367 {768, 8000, 6144000, 0x60, 0x02, 0x11, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
368 {768, 16000, 12288000, 0x20, 0x02, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
369 {768, 32000, 24576000, 0xE0, 0x02, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
370 {800, 48000, 38400000, 0x00, 0x18, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
372 {1024, 8000, 8192000, 0x60, 0x00, 0x30, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
373 {1024, 16000, 16384000, 0x20, 0x00, 0x00, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
374 {1152, 16000, 18432000, 0x20, 0x08, 0x11, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
375 {1536, 8000, 12288000, 0x60, 0x02, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
376 {1536, 16000, 24576000, 0x20, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
377 {1625, 8000, 13000000, 0x0C, 0x18, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
378 {1625, 16000, 26000000, 0x0C, 0x18, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
379 {2048, 8000, 16384000, 0x60, 0x00, 0x00, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
380 {2304, 8000, 18432000, 0x40, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x5F},
381 {3072, 8000, 24576000, 0x60, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
382 {3250, 8000, 26000000, 0x0C, 0x18, 0x0F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
385 static inline int get_coeff(int mclk, int rate, int array,
386 const struct _coeff_div *coeff_div)
390 for (i = 0; i < array; i++) {
391 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
398 static int es8326_set_dai_sysclk(struct snd_soc_dai *codec_dai,
399 int clk_id, unsigned int freq, int dir)
401 struct snd_soc_component *codec = codec_dai->component;
402 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
404 es8326->sysclk = freq;
409 static int es8326_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
411 struct snd_soc_component *component = codec_dai->component;
414 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
415 case SND_SOC_DAIFMT_CBC_CFP:
416 snd_soc_component_update_bits(component, ES8326_RESET,
417 ES8326_MASTER_MODE_EN, ES8326_MASTER_MODE_EN);
419 case SND_SOC_DAIFMT_CBC_CFC:
425 /* interface format */
426 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
427 case SND_SOC_DAIFMT_I2S:
429 case SND_SOC_DAIFMT_RIGHT_J:
430 dev_err(component->dev, "Codec driver does not support right justified\n");
432 case SND_SOC_DAIFMT_LEFT_J:
433 iface |= ES8326_DAIFMT_LEFT_J;
435 case SND_SOC_DAIFMT_DSP_A:
436 iface |= ES8326_DAIFMT_DSP_A;
438 case SND_SOC_DAIFMT_DSP_B:
439 iface |= ES8326_DAIFMT_DSP_B;
445 snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DAIFMT_MASK, iface);
450 static int es8326_pcm_hw_params(struct snd_pcm_substream *substream,
451 struct snd_pcm_hw_params *params,
452 struct snd_soc_dai *dai)
454 struct snd_soc_component *component = dai->component;
455 const struct _coeff_div *coeff_div;
456 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
460 if (es8326->version == 0) {
461 coeff_div = coeff_div_v0;
462 array = ARRAY_SIZE(coeff_div_v0);
464 coeff_div = coeff_div_v3;
465 array = ARRAY_SIZE(coeff_div_v3);
467 coeff = get_coeff(es8326->sysclk, params_rate(params), array, coeff_div);
469 switch (params_format(params)) {
470 case SNDRV_PCM_FORMAT_S16_LE:
471 srate |= ES8326_S16_LE;
473 case SNDRV_PCM_FORMAT_S20_3LE:
474 srate |= ES8326_S20_3_LE;
476 case SNDRV_PCM_FORMAT_S18_3LE:
477 srate |= ES8326_S18_LE;
479 case SNDRV_PCM_FORMAT_S24_LE:
480 srate |= ES8326_S24_LE;
482 case SNDRV_PCM_FORMAT_S32_LE:
483 srate |= ES8326_S32_LE;
489 /* set iface & srate */
490 snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DATA_LEN_MASK, srate);
493 regmap_write(es8326->regmap, ES8326_CLK_DIV1,
494 coeff_div[coeff].reg4);
495 regmap_write(es8326->regmap, ES8326_CLK_DIV2,
496 coeff_div[coeff].reg5);
497 regmap_write(es8326->regmap, ES8326_CLK_DLL,
498 coeff_div[coeff].reg6);
499 regmap_write(es8326->regmap, ES8326_CLK_MUX,
500 coeff_div[coeff].reg7);
501 regmap_write(es8326->regmap, ES8326_CLK_ADC_SEL,
502 coeff_div[coeff].reg8);
503 regmap_write(es8326->regmap, ES8326_CLK_DAC_SEL,
504 coeff_div[coeff].reg9);
505 regmap_write(es8326->regmap, ES8326_CLK_ADC_OSR,
506 coeff_div[coeff].rega);
507 regmap_write(es8326->regmap, ES8326_CLK_DAC_OSR,
508 coeff_div[coeff].regb);
510 dev_warn(component->dev, "Clock coefficients do not match");
516 static int es8326_mute(struct snd_soc_dai *dai, int mute, int direction)
518 struct snd_soc_component *component = dai->component;
519 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
520 unsigned int offset_l, offset_r;
523 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
524 regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE,
525 ES8326_MUTE_MASK, ES8326_MUTE);
526 regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF,
529 if (!es8326->calibrated) {
530 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_FORCE_CAL);
532 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
533 regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l);
534 regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r);
535 regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
536 regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l);
537 regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r);
538 es8326->calibrated = true;
540 regmap_update_bits(es8326->regmap, ES8326_DAC_DSM, 0x01, 0x01);
541 usleep_range(1000, 5000);
542 regmap_update_bits(es8326->regmap, ES8326_DAC_DSM, 0x01, 0x00);
543 usleep_range(1000, 5000);
544 regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x30, 0x20);
545 regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x30, 0x30);
546 regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa1);
547 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_ON);
548 regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE,
549 ES8326_MUTE_MASK, ~(ES8326_MUTE));
554 static int es8326_set_bias_level(struct snd_soc_component *codec,
555 enum snd_soc_bias_level level)
557 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
561 case SND_SOC_BIAS_ON:
562 ret = clk_prepare_enable(es8326->mclk);
566 regmap_update_bits(es8326->regmap, ES8326_RESET, 0x02, 0x02);
567 usleep_range(5000, 10000);
568 regmap_write(es8326->regmap, ES8326_INTOUT_IO, es8326->interrupt_clk);
569 regmap_write(es8326->regmap, ES8326_SDINOUT1_IO,
570 (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT));
571 regmap_write(es8326->regmap, ES8326_PGA_PDN, 0x40);
572 regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x00);
573 regmap_update_bits(es8326->regmap, ES8326_CLK_CTL, 0x20, 0x20);
574 regmap_update_bits(es8326->regmap, ES8326_RESET, 0x02, 0x00);
576 case SND_SOC_BIAS_PREPARE:
578 case SND_SOC_BIAS_STANDBY:
579 regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x3b);
580 regmap_update_bits(es8326->regmap, ES8326_CLK_CTL, 0x20, 0x00);
581 regmap_write(es8326->regmap, ES8326_SDINOUT1_IO, ES8326_IO_INPUT);
583 case SND_SOC_BIAS_OFF:
584 clk_disable_unprepare(es8326->mclk);
591 #define es8326_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
592 SNDRV_PCM_FMTBIT_S24_LE)
594 static const struct snd_soc_dai_ops es8326_ops = {
595 .hw_params = es8326_pcm_hw_params,
596 .set_fmt = es8326_set_dai_fmt,
597 .set_sysclk = es8326_set_dai_sysclk,
598 .mute_stream = es8326_mute,
599 .no_capture_mute = 1,
602 static struct snd_soc_dai_driver es8326_dai = {
603 .name = "ES8326 HiFi",
605 .stream_name = "Playback",
608 .rates = SNDRV_PCM_RATE_8000_48000,
609 .formats = es8326_FORMATS,
612 .stream_name = "Capture",
615 .rates = SNDRV_PCM_RATE_8000_48000,
616 .formats = es8326_FORMATS,
622 static void es8326_enable_micbias(struct snd_soc_component *component)
624 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
626 snd_soc_dapm_mutex_lock(dapm);
627 snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS1");
628 snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS2");
629 snd_soc_dapm_sync_unlocked(dapm);
630 snd_soc_dapm_mutex_unlock(dapm);
633 static void es8326_disable_micbias(struct snd_soc_component *component)
635 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
637 snd_soc_dapm_mutex_lock(dapm);
638 snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS1");
639 snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS2");
640 snd_soc_dapm_sync_unlocked(dapm);
641 snd_soc_dapm_mutex_unlock(dapm);
645 * For button detection, set the following in soundcard
646 * snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
647 * snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
648 * snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
650 static void es8326_jack_button_handler(struct work_struct *work)
652 struct es8326_priv *es8326 =
653 container_of(work, struct es8326_priv, button_press_work.work);
654 struct snd_soc_component *comp = es8326->component;
656 static int button_to_report, press_count;
657 static int prev_button, cur_button;
659 if (!(es8326->jack->status & SND_JACK_HEADSET)) /* Jack unplugged */
662 mutex_lock(&es8326->lock);
663 iface = snd_soc_component_read(comp, ES8326_HPDET_STA);
666 /* pause button detected */
667 cur_button = SND_JACK_BTN_0;
671 /* button volume up */
672 cur_button = SND_JACK_BTN_1;
675 /* button volume down */
676 cur_button = SND_JACK_BTN_2;
680 /* button released or not pressed */
687 if ((prev_button == cur_button) && (cur_button != 0)) {
689 if (press_count > 3) {
690 /* report a press every 120ms */
691 snd_soc_jack_report(es8326->jack, cur_button,
692 SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
695 button_to_report = cur_button;
696 queue_delayed_work(system_wq, &es8326->button_press_work,
697 msecs_to_jiffies(35));
698 } else if (prev_button != cur_button) {
699 /* mismatch, detect again */
700 prev_button = cur_button;
701 queue_delayed_work(system_wq, &es8326->button_press_work,
702 msecs_to_jiffies(35));
704 /* released or no pressed */
705 if (button_to_report != 0) {
706 snd_soc_jack_report(es8326->jack, button_to_report,
707 SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
708 snd_soc_jack_report(es8326->jack, 0,
709 SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
710 button_to_report = 0;
713 mutex_unlock(&es8326->lock);
716 static void es8326_jack_detect_handler(struct work_struct *work)
718 struct es8326_priv *es8326 =
719 container_of(work, struct es8326_priv, jack_detect_work.work);
720 struct snd_soc_component *comp = es8326->component;
723 mutex_lock(&es8326->lock);
724 iface = snd_soc_component_read(comp, ES8326_HPDET_STA);
725 dev_dbg(comp->dev, "gpio flag %#04x", iface);
727 if ((es8326->jack_remove_retry == 1) && (es8326->version != ES8326_VERSION_B)) {
728 if (iface & ES8326_HPINSERT_FLAG)
729 es8326->jack_remove_retry = 2;
731 es8326->jack_remove_retry = 0;
733 dev_dbg(comp->dev, "remove event check, set HPJACK_POL normal, cnt = %d\n",
734 es8326->jack_remove_retry);
736 * Inverted HPJACK_POL bit to trigger one IRQ to double check HP Removal event
738 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE,
739 ES8326_HP_DET_JACK_POL, (es8326->jd_inverted ?
740 ~es8326->jack_pol : es8326->jack_pol));
744 if ((iface & ES8326_HPINSERT_FLAG) == 0) {
745 /* Jack unplugged or spurious IRQ */
746 dev_dbg(comp->dev, "No headset detected\n");
747 es8326_disable_micbias(es8326->component);
748 if (es8326->jack->status & SND_JACK_HEADPHONE) {
749 dev_dbg(comp->dev, "Report hp remove event\n");
750 snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
751 /* mute adc when mic path switch */
752 regmap_write(es8326->regmap, ES8326_ADC_SCALE, 0x33);
753 regmap_write(es8326->regmap, ES8326_ADC1_SRC, 0x44);
754 regmap_write(es8326->regmap, ES8326_ADC2_SRC, 0x66);
757 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x01);
758 regmap_write(es8326->regmap, ES8326_SYS_BIAS, 0x0a);
759 regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x0f, 0x03);
761 * Inverted HPJACK_POL bit to trigger one IRQ to double check HP Removal event
763 if ((es8326->jack_remove_retry == 0) && (es8326->version != ES8326_VERSION_B)) {
764 es8326->jack_remove_retry = 1;
765 dev_dbg(comp->dev, "remove event check, invert HPJACK_POL, cnt = %d\n",
766 es8326->jack_remove_retry);
767 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE,
768 ES8326_HP_DET_JACK_POL, (es8326->jd_inverted ?
769 es8326->jack_pol : ~es8326->jack_pol));
772 es8326->jack_remove_retry = 0;
774 } else if ((iface & ES8326_HPINSERT_FLAG) == ES8326_HPINSERT_FLAG) {
775 es8326->jack_remove_retry = 0;
776 if (es8326->hp == 0) {
777 dev_dbg(comp->dev, "First insert, start OMTP/CTIA type check\n");
779 * set auto-check mode, then restart jack_detect_work after 400ms.
780 * Don't report jack status.
782 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x01);
783 es8326_enable_micbias(es8326->component);
784 usleep_range(50000, 70000);
785 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x00);
786 regmap_write(es8326->regmap, ES8326_SYS_BIAS, 0x1f);
787 regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x0f, 0x08);
788 queue_delayed_work(system_wq, &es8326->jack_detect_work,
789 msecs_to_jiffies(400));
793 if (es8326->jack->status & SND_JACK_HEADSET) {
795 dev_dbg(comp->dev, "button pressed\n");
796 queue_delayed_work(system_wq, &es8326->button_press_work, 10);
799 if ((iface & ES8326_HPBUTTON_FLAG) == 0x01) {
800 dev_dbg(comp->dev, "Headphone detected\n");
801 snd_soc_jack_report(es8326->jack,
802 SND_JACK_HEADPHONE, SND_JACK_HEADSET);
804 dev_dbg(comp->dev, "Headset detected\n");
805 snd_soc_jack_report(es8326->jack,
806 SND_JACK_HEADSET, SND_JACK_HEADSET);
808 regmap_write(es8326->regmap, ES8326_ADC_SCALE, 0x33);
809 regmap_update_bits(es8326->regmap, ES8326_PGA_PDN,
811 regmap_update_bits(es8326->regmap, ES8326_PGAGAIN,
813 regmap_write(es8326->regmap, ES8326_ADC1_SRC, 0x00);
814 regmap_write(es8326->regmap, ES8326_ADC2_SRC, 0x00);
815 regmap_update_bits(es8326->regmap, ES8326_PGA_PDN,
817 usleep_range(10000, 15000);
821 mutex_unlock(&es8326->lock);
824 static irqreturn_t es8326_irq(int irq, void *dev_id)
826 struct es8326_priv *es8326 = dev_id;
831 if (es8326->jack->status & SND_JACK_HEADSET)
832 queue_delayed_work(system_wq, &es8326->jack_detect_work,
833 msecs_to_jiffies(10));
835 queue_delayed_work(system_wq, &es8326->jack_detect_work,
836 msecs_to_jiffies(300));
842 static int es8326_calibrate(struct snd_soc_component *component)
844 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
846 unsigned int offset_l, offset_r;
848 regmap_read(es8326->regmap, ES8326_CHIP_VERSION, ®);
849 es8326->version = reg;
851 if ((es8326->version == ES8326_VERSION_B) && (es8326->calibrated == false)) {
852 dev_dbg(component->dev, "ES8326_VERSION_B, calibrating\n");
853 regmap_write(es8326->regmap, ES8326_CLK_INV, 0xc0);
854 regmap_write(es8326->regmap, ES8326_CLK_DIV1, 0x03);
855 regmap_write(es8326->regmap, ES8326_CLK_DLL, 0x30);
856 regmap_write(es8326->regmap, ES8326_CLK_MUX, 0xed);
857 regmap_write(es8326->regmap, ES8326_CLK_DAC_SEL, 0x08);
858 regmap_write(es8326->regmap, ES8326_CLK_TRI, 0xc1);
859 regmap_write(es8326->regmap, ES8326_DAC_MUTE, 0x03);
860 regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7f);
861 regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x23);
862 regmap_write(es8326->regmap, ES8326_DAC2HPMIX, 0x88);
863 usleep_range(15000, 20000);
864 regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
865 usleep_range(15000, 20000);
866 regmap_write(es8326->regmap, ES8326_RESET, 0xc0);
867 usleep_range(15000, 20000);
869 regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, ES8326_HP_OFF);
870 regmap_read(es8326->regmap, ES8326_CSM_MUTE_STA, ®);
871 if ((reg & 0xf0) != 0x40)
874 regmap_write(es8326->regmap, ES8326_HP_CAL, 0xd4);
876 regmap_write(es8326->regmap, ES8326_HP_CAL, 0x4d);
878 regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
879 regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l);
880 regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r);
881 regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
882 regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l);
883 regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r);
884 regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00);
886 es8326->calibrated = true;
892 static int es8326_resume(struct snd_soc_component *component)
894 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
896 regcache_cache_only(es8326->regmap, false);
897 regcache_sync(es8326->regmap);
899 /* reset internal clock state */
900 regmap_write(es8326->regmap, ES8326_RESET, 0x1f);
901 regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x0E);
902 regmap_write(es8326->regmap, ES8326_ANA_LP, 0xf0);
903 usleep_range(10000, 15000);
904 regmap_write(es8326->regmap, ES8326_HPJACK_TIMER, 0xe9);
905 regmap_write(es8326->regmap, ES8326_ANA_MICBIAS, 0xcb);
906 /* set headphone default type and detect pin */
907 regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x83);
908 regmap_write(es8326->regmap, ES8326_CLK_RESAMPLE, 0x05);
910 /* set internal oscillator as clock source of headpone cp */
911 regmap_write(es8326->regmap, ES8326_CLK_DIV_CPC, 0x89);
912 regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_ON);
913 /* clock manager reset release */
914 regmap_write(es8326->regmap, ES8326_RESET, 0x17);
915 /* set headphone detection as half scan mode */
916 regmap_write(es8326->regmap, ES8326_HP_MISC, 0x3d);
917 regmap_write(es8326->regmap, ES8326_PULLUP_CTL, 0x00);
919 /* enable headphone driver */
920 regmap_write(es8326->regmap, ES8326_HP_VOL, 0xc4);
921 regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa7);
922 usleep_range(2000, 5000);
923 regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0x23);
924 regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0x33);
925 regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa1);
927 regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00);
928 regmap_write(es8326->regmap, ES8326_CLK_VMIDS1, 0xc4);
929 regmap_write(es8326->regmap, ES8326_CLK_VMIDS2, 0x81);
930 regmap_write(es8326->regmap, ES8326_CLK_CAL_TIME, 0x00);
931 /* calibrate for B version */
932 es8326_calibrate(component);
933 regmap_write(es8326->regmap, ES8326_DAC_CROSSTALK, 0xaa);
934 regmap_write(es8326->regmap, ES8326_DAC_RAMPRATE, 0x00);
935 /* turn off headphone out */
936 regmap_write(es8326->regmap, ES8326_HP_CAL, 0x00);
937 /* set ADC and DAC in low power mode */
938 regmap_write(es8326->regmap, ES8326_ANA_LP, 0xf0);
940 regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7F);
941 /* select vdda as micbias source */
942 regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x23);
943 /* set dac dsmclip = 1 */
944 regmap_write(es8326->regmap, ES8326_DAC_DSM, 0x08);
945 regmap_write(es8326->regmap, ES8326_DAC_VPPSCALE, 0x15);
947 regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x80 |
948 ((es8326->version == ES8326_VERSION_B) ?
949 (ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol) :
950 (ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol | 0x04)));
951 usleep_range(5000, 10000);
952 es8326_enable_micbias(es8326->component);
953 usleep_range(50000, 70000);
954 regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x00);
955 regmap_write(es8326->regmap, ES8326_INT_SOURCE,
956 (ES8326_INT_SRC_PIN9 | ES8326_INT_SRC_BUTTON));
957 regmap_write(es8326->regmap, ES8326_INTOUT_IO,
958 es8326->interrupt_clk);
959 regmap_write(es8326->regmap, ES8326_SDINOUT1_IO,
960 (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT));
961 regmap_write(es8326->regmap, ES8326_SDINOUT23_IO, ES8326_IO_INPUT);
963 regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x00);
964 regmap_write(es8326->regmap, ES8326_RESET, ES8326_CSM_ON);
965 regmap_update_bits(es8326->regmap, ES8326_PGAGAIN, ES8326_MIC_SEL_MASK,
968 regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE, ES8326_MUTE_MASK,
972 es8326->jack_remove_retry = 0;
977 static int es8326_suspend(struct snd_soc_component *component)
979 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
981 cancel_delayed_work_sync(&es8326->jack_detect_work);
982 es8326_disable_micbias(component);
983 es8326->calibrated = false;
984 regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_OFF);
985 regcache_cache_only(es8326->regmap, true);
986 regcache_mark_dirty(es8326->regmap);
988 /* reset register value to default */
989 regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x01);
990 usleep_range(1000, 3000);
991 regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x00);
995 static int es8326_probe(struct snd_soc_component *component)
997 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
1000 es8326->component = component;
1001 es8326->jd_inverted = device_property_read_bool(component->dev,
1002 "everest,jack-detect-inverted");
1004 ret = device_property_read_u8(component->dev, "everest,mic1-src", &es8326->mic1_src);
1006 dev_dbg(component->dev, "mic1-src return %d", ret);
1007 es8326->mic1_src = ES8326_ADC_AMIC;
1009 dev_dbg(component->dev, "mic1-src %x", es8326->mic1_src);
1011 ret = device_property_read_u8(component->dev, "everest,mic2-src", &es8326->mic2_src);
1013 dev_dbg(component->dev, "mic2-src return %d", ret);
1014 es8326->mic2_src = ES8326_ADC_DMIC;
1016 dev_dbg(component->dev, "mic2-src %x", es8326->mic2_src);
1018 ret = device_property_read_u8(component->dev, "everest,jack-pol", &es8326->jack_pol);
1020 dev_dbg(component->dev, "jack-pol return %d", ret);
1021 es8326->jack_pol = ES8326_HP_TYPE_AUTO;
1023 dev_dbg(component->dev, "jack-pol %x", es8326->jack_pol);
1025 ret = device_property_read_u8(component->dev, "everest,interrupt-src",
1026 &es8326->interrupt_src);
1028 dev_dbg(component->dev, "interrupt-src return %d", ret);
1029 es8326->interrupt_src = ES8326_HP_DET_SRC_PIN9;
1031 dev_dbg(component->dev, "interrupt-src %x", es8326->interrupt_src);
1033 ret = device_property_read_u8(component->dev, "everest,interrupt-clk",
1034 &es8326->interrupt_clk);
1036 dev_dbg(component->dev, "interrupt-clk return %d", ret);
1037 es8326->interrupt_clk = 0x45;
1039 dev_dbg(component->dev, "interrupt-clk %x", es8326->interrupt_clk);
1041 es8326_resume(component);
1045 static void es8326_enable_jack_detect(struct snd_soc_component *component,
1046 struct snd_soc_jack *jack)
1048 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
1050 mutex_lock(&es8326->lock);
1051 if (es8326->jd_inverted)
1052 snd_soc_component_update_bits(component, ES8326_HPDET_TYPE,
1053 ES8326_HP_DET_JACK_POL, ~es8326->jack_pol);
1054 es8326->jack = jack;
1056 mutex_unlock(&es8326->lock);
1057 es8326_irq(es8326->irq, es8326);
1060 static void es8326_disable_jack_detect(struct snd_soc_component *component)
1062 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
1064 dev_dbg(component->dev, "Enter into %s\n", __func__);
1066 return; /* Already disabled (or never enabled) */
1067 cancel_delayed_work_sync(&es8326->jack_detect_work);
1069 mutex_lock(&es8326->lock);
1070 if (es8326->jack->status & SND_JACK_MICROPHONE) {
1071 es8326_disable_micbias(component);
1072 snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
1074 es8326->jack = NULL;
1075 mutex_unlock(&es8326->lock);
1078 static int es8326_set_jack(struct snd_soc_component *component,
1079 struct snd_soc_jack *jack, void *data)
1082 es8326_enable_jack_detect(component, jack);
1084 es8326_disable_jack_detect(component);
1089 static void es8326_remove(struct snd_soc_component *component)
1091 es8326_disable_jack_detect(component);
1092 es8326_set_bias_level(component, SND_SOC_BIAS_OFF);
1095 static const struct snd_soc_component_driver soc_component_dev_es8326 = {
1096 .probe = es8326_probe,
1097 .remove = es8326_remove,
1098 .resume = es8326_resume,
1099 .suspend = es8326_suspend,
1100 .set_bias_level = es8326_set_bias_level,
1101 .set_jack = es8326_set_jack,
1102 .dapm_widgets = es8326_dapm_widgets,
1103 .num_dapm_widgets = ARRAY_SIZE(es8326_dapm_widgets),
1104 .dapm_routes = es8326_dapm_routes,
1105 .num_dapm_routes = ARRAY_SIZE(es8326_dapm_routes),
1106 .controls = es8326_snd_controls,
1107 .num_controls = ARRAY_SIZE(es8326_snd_controls),
1108 .use_pmdown_time = 1,
1112 static int es8326_i2c_probe(struct i2c_client *i2c)
1114 struct es8326_priv *es8326;
1117 es8326 = devm_kzalloc(&i2c->dev, sizeof(struct es8326_priv), GFP_KERNEL);
1121 i2c_set_clientdata(i2c, es8326);
1123 mutex_init(&es8326->lock);
1124 es8326->regmap = devm_regmap_init_i2c(i2c, &es8326_regmap_config);
1125 if (IS_ERR(es8326->regmap)) {
1126 ret = PTR_ERR(es8326->regmap);
1127 dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
1131 es8326->irq = i2c->irq;
1132 INIT_DELAYED_WORK(&es8326->jack_detect_work,
1133 es8326_jack_detect_handler);
1134 INIT_DELAYED_WORK(&es8326->button_press_work,
1135 es8326_jack_button_handler);
1136 /* ES8316 is level-based while ES8326 is edge-based */
1137 ret = devm_request_threaded_irq(&i2c->dev, es8326->irq, NULL, es8326_irq,
1138 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1141 dev_warn(&i2c->dev, "Failed to request IRQ: %d: %d\n",
1143 es8326->irq = -ENXIO;
1146 es8326->mclk = devm_clk_get_optional(&i2c->dev, "mclk");
1147 if (IS_ERR(es8326->mclk)) {
1148 dev_err(&i2c->dev, "unable to get mclk\n");
1149 return PTR_ERR(es8326->mclk);
1152 dev_warn(&i2c->dev, "assuming static mclk\n");
1154 ret = clk_prepare_enable(es8326->mclk);
1156 dev_err(&i2c->dev, "unable to enable mclk\n");
1159 return devm_snd_soc_register_component(&i2c->dev,
1160 &soc_component_dev_es8326,
1164 static const struct i2c_device_id es8326_i2c_id[] = {
1168 MODULE_DEVICE_TABLE(i2c, es8326_i2c_id);
1171 static const struct of_device_id es8326_of_match[] = {
1172 { .compatible = "everest,es8326", },
1175 MODULE_DEVICE_TABLE(of, es8326_of_match);
1179 static const struct acpi_device_id es8326_acpi_match[] = {
1183 MODULE_DEVICE_TABLE(acpi, es8326_acpi_match);
1186 static struct i2c_driver es8326_i2c_driver = {
1189 .acpi_match_table = ACPI_PTR(es8326_acpi_match),
1190 .of_match_table = of_match_ptr(es8326_of_match),
1192 .probe = es8326_i2c_probe,
1193 .id_table = es8326_i2c_id,
1195 module_i2c_driver(es8326_i2c_driver);
1197 MODULE_DESCRIPTION("ASoC es8326 driver");
1198 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
1199 MODULE_LICENSE("GPL");