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.
43 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9550, 50, 0);
44 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9550, 50, 0);
45 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_analog_pga_tlv, 0, 300, 0);
46 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_pga_tlv, 0, 600, 0);
47 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(softramp_rate, 0, 100, 0);
48 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_target_tlv, -3200, 200, 0);
49 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_recovery_tlv, -125, 250, 0);
51 static const char *const winsize[] = {
70 static const char *const dacpol_txt[] = {
71 "Normal", "R Invert", "L Invert", "L + R Invert" };
73 static const struct soc_enum dacpol =
74 SOC_ENUM_SINGLE(ES8326_DAC_DSM, 4, 4, dacpol_txt);
75 static const struct soc_enum alc_winsize =
76 SOC_ENUM_SINGLE(ES8326_ADC_RAMPRATE, 4, 16, winsize);
77 static const struct soc_enum drc_winsize =
78 SOC_ENUM_SINGLE(ES8326_DRC_WINSIZE, 4, 16, winsize);
80 static const struct snd_kcontrol_new es8326_snd_controls[] = {
81 SOC_SINGLE_TLV("DAC Playback Volume", ES8326_DAC_VOL, 0, 0xbf, 0, dac_vol_tlv),
82 SOC_ENUM("Playback Polarity", dacpol),
83 SOC_SINGLE_TLV("DAC Ramp Rate", ES8326_DAC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
84 SOC_SINGLE_TLV("DRC Recovery Level", ES8326_DRC_RECOVERY, 0, 4, 0, drc_recovery_tlv),
85 SOC_ENUM("DRC Winsize", drc_winsize),
86 SOC_SINGLE_TLV("DRC Target Level", ES8326_DRC_WINSIZE, 0, 0x0f, 0, drc_target_tlv),
88 SOC_DOUBLE_R_TLV("ADC Capture Volume", ES8326_ADC1_VOL, ES8326_ADC2_VOL, 0, 0xff, 0,
90 SOC_DOUBLE_TLV("ADC PGA Volume", ES8326_ADC_SCALE, 4, 0, 5, 0, adc_pga_tlv),
91 SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8326_PGAGAIN, 0, 10, 0, adc_analog_pga_tlv),
92 SOC_SINGLE_TLV("ADC Ramp Rate", ES8326_ADC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
93 SOC_SINGLE("ALC Capture Switch", ES8326_ALC_RECOVERY, 3, 1, 0),
94 SOC_SINGLE_TLV("ALC Capture Recovery Level", ES8326_ALC_LEVEL,
95 0, 4, 0, drc_recovery_tlv),
96 SOC_ENUM("ALC Capture Winsize", alc_winsize),
97 SOC_SINGLE_TLV("ALC Capture Target Level", ES8326_ALC_LEVEL,
98 0, 0x0f, 0, drc_target_tlv),
102 static const struct snd_soc_dapm_widget es8326_dapm_widgets[] = {
103 SND_SOC_DAPM_INPUT("MIC1"),
104 SND_SOC_DAPM_INPUT("MIC2"),
105 SND_SOC_DAPM_INPUT("MIC3"),
106 SND_SOC_DAPM_INPUT("MIC4"),
108 SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
109 SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
111 /* Digital Interface */
112 SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 0, SND_SOC_NOPM, 0, 0),
113 SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0, SND_SOC_NOPM, 0, 0),
115 /* ADC Digital Mute */
116 SND_SOC_DAPM_PGA("ADC L1", ES8326_ADC_MUTE, 0, 1, NULL, 0),
117 SND_SOC_DAPM_PGA("ADC R1", ES8326_ADC_MUTE, 1, 1, NULL, 0),
118 SND_SOC_DAPM_PGA("ADC L2", ES8326_ADC_MUTE, 2, 1, NULL, 0),
119 SND_SOC_DAPM_PGA("ADC R2", ES8326_ADC_MUTE, 3, 1, NULL, 0),
121 /* Analog Power Supply*/
122 SND_SOC_DAPM_DAC("Right DAC", NULL, ES8326_ANA_PDN, 0, 1),
123 SND_SOC_DAPM_DAC("Left DAC", NULL, ES8326_ANA_PDN, 1, 1),
124 SND_SOC_DAPM_SUPPLY("Analog Power", ES8326_ANA_PDN, 7, 1, NULL, 0),
125 SND_SOC_DAPM_SUPPLY("IBias Power", ES8326_ANA_PDN, 6, 1, NULL, 0),
126 SND_SOC_DAPM_SUPPLY("ADC Vref", ES8326_ANA_PDN, 5, 1, NULL, 0),
127 SND_SOC_DAPM_SUPPLY("DAC Vref", ES8326_ANA_PDN, 4, 1, NULL, 0),
128 SND_SOC_DAPM_SUPPLY("Vref Power", ES8326_ANA_PDN, 3, 1, NULL, 0),
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 /* Headphone Charge Pump and Output */
136 SND_SOC_DAPM_SUPPLY("HPOR Cal", ES8326_HP_CAL, 7, 1, NULL, 0),
137 SND_SOC_DAPM_SUPPLY("HPOL Cal", ES8326_HP_CAL, 3, 1, NULL, 0),
138 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump", ES8326_HP_DRIVER,
140 SND_SOC_DAPM_SUPPLY("Headphone Driver Bias", ES8326_HP_DRIVER,
142 SND_SOC_DAPM_SUPPLY("Headphone LDO", ES8326_HP_DRIVER,
144 SND_SOC_DAPM_SUPPLY("Headphone Reference", ES8326_HP_DRIVER,
146 SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPOR Supply", ES8326_HP_CAL,
147 ES8326_HPOR_SHIFT, 7, 7, 0),
148 SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPOL Supply", ES8326_HP_CAL,
151 SND_SOC_DAPM_OUTPUT("HPOL"),
152 SND_SOC_DAPM_OUTPUT("HPOR"),
155 static const struct snd_soc_dapm_route es8326_dapm_routes[] = {
156 {"ADC L1", NULL, "MIC1"},
157 {"ADC R1", NULL, "MIC2"},
158 {"ADC L2", NULL, "MIC3"},
159 {"ADC R2", NULL, "MIC4"},
161 {"ADC L", NULL, "ADC L1"},
162 {"ADC R", NULL, "ADC R1"},
163 {"ADC L", NULL, "ADC L2"},
164 {"ADC R", NULL, "ADC R2"},
166 {"I2S OUT", NULL, "ADC L"},
167 {"I2S OUT", NULL, "ADC R"},
169 {"I2S OUT", NULL, "Analog Power"},
170 {"I2S OUT", NULL, "ADC Vref"},
171 {"I2S OUT", NULL, "Vref Power"},
172 {"I2S OUT", NULL, "IBias Power"},
173 {"I2S IN", NULL, "Analog Power"},
174 {"I2S IN", NULL, "DAC Vref"},
175 {"I2S IN", NULL, "Vref Power"},
176 {"I2S IN", NULL, "IBias Power"},
178 {"Right DAC", NULL, "I2S IN"},
179 {"Left DAC", NULL, "I2S IN"},
181 {"LHPMIX", NULL, "Left DAC"},
182 {"RHPMIX", NULL, "Right DAC"},
184 {"HPOR", NULL, "HPOR Cal"},
185 {"HPOL", NULL, "HPOL Cal"},
186 {"HPOR", NULL, "HPOR Supply"},
187 {"HPOL", NULL, "HPOL Supply"},
188 {"HPOL", NULL, "Headphone Charge Pump"},
189 {"HPOR", NULL, "Headphone Charge Pump"},
190 {"HPOL", NULL, "Headphone Driver Bias"},
191 {"HPOR", NULL, "Headphone Driver Bias"},
192 {"HPOL", NULL, "Headphone LDO"},
193 {"HPOR", NULL, "Headphone LDO"},
194 {"HPOL", NULL, "Headphone Reference"},
195 {"HPOR", NULL, "Headphone Reference"},
197 {"HPOL", NULL, "LHPMIX"},
198 {"HPOR", NULL, "RHPMIX"},
201 static const struct regmap_range es8326_volatile_ranges[] = {
202 regmap_reg_range(ES8326_HP_DETECT, ES8326_HP_DETECT),
205 static const struct regmap_access_table es8326_volatile_table = {
206 .yes_ranges = es8326_volatile_ranges,
207 .n_yes_ranges = ARRAY_SIZE(es8326_volatile_ranges),
210 static const struct regmap_config es8326_regmap_config = {
213 .max_register = 0xff,
214 .volatile_table = &es8326_volatile_table,
215 .cache_type = REGCACHE_RBTREE,
232 /* codec hifi mclk clock divider coefficients */
233 /* {ratio, LRCK, MCLK, REG04, REG05, REG06, REG07, REG08, REG09, REG10, REG11} */
234 static const struct _coeff_div coeff_div[] = {
235 {32, 8000, 256000, 0x60, 0x00, 0x0F, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
236 {32, 16000, 512000, 0x20, 0x00, 0x0D, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
237 {32, 44100, 1411200, 0x00, 0x00, 0x13, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
238 {32, 48000, 1536000, 0x00, 0x00, 0x13, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
239 {36, 8000, 288000, 0x20, 0x00, 0x0D, 0x75, 0x0A, 0x1B, 0x23, 0x47},
240 {36, 16000, 576000, 0x20, 0x00, 0x0D, 0x75, 0x0A, 0x1B, 0x23, 0x47},
241 {48, 8000, 384000, 0x60, 0x02, 0x1F, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
242 {48, 16000, 768000, 0x20, 0x02, 0x0F, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
243 {48, 48000, 2304000, 0x00, 0x02, 0x0D, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
244 {64, 8000, 512000, 0x60, 0x00, 0x0D, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
245 {64, 16000, 1024000, 0x20, 0x00, 0x05, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
247 {64, 44100, 2822400, 0x00, 0x00, 0x11, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
248 {64, 48000, 3072000, 0x00, 0x00, 0x11, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
249 {72, 8000, 576000, 0x20, 0x00, 0x13, 0x35, 0x0A, 0x1B, 0x23, 0x47},
250 {72, 16000, 1152000, 0x20, 0x00, 0x05, 0x75, 0x0A, 0x1B, 0x23, 0x47},
251 {96, 8000, 768000, 0x60, 0x02, 0x1D, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
252 {96, 16000, 1536000, 0x20, 0x02, 0x0D, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
253 {100, 48000, 4800000, 0x04, 0x04, 0x3F, 0x6D, 0x38, 0x08, 0x4f, 0x1f},
254 {125, 48000, 6000000, 0x04, 0x04, 0x1F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
255 {128, 8000, 1024000, 0x60, 0x00, 0x13, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
256 {128, 16000, 2048000, 0x20, 0x00, 0x11, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
258 {128, 44100, 5644800, 0x00, 0x00, 0x01, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
259 {128, 48000, 6144000, 0x00, 0x00, 0x01, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
260 {144, 8000, 1152000, 0x20, 0x00, 0x03, 0x35, 0x0A, 0x1B, 0x23, 0x47},
261 {144, 16000, 2304000, 0x20, 0x00, 0x11, 0x35, 0x0A, 0x1B, 0x23, 0x47},
262 {192, 8000, 1536000, 0x60, 0x02, 0x0D, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
263 {192, 16000, 3072000, 0x20, 0x02, 0x05, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
264 {200, 48000, 9600000, 0x04, 0x04, 0x0F, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
265 {250, 48000, 12000000, 0x04, 0x04, 0x0F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
266 {256, 8000, 2048000, 0x60, 0x00, 0x11, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
267 {256, 16000, 4096000, 0x20, 0x00, 0x01, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
269 {256, 44100, 11289600, 0x00, 0x00, 0x10, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
270 {256, 48000, 12288000, 0x00, 0x00, 0x30, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
271 {288, 8000, 2304000, 0x20, 0x00, 0x01, 0x35, 0x0A, 0x1B, 0x23, 0x47},
272 {384, 8000, 3072000, 0x60, 0x02, 0x05, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
273 {384, 16000, 6144000, 0x20, 0x02, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
274 {384, 48000, 18432000, 0x00, 0x02, 0x01, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
275 {400, 48000, 19200000, 0x09, 0x04, 0x0f, 0x6d, 0x3a, 0x0A, 0x4F, 0x1F},
276 {500, 48000, 24000000, 0x18, 0x04, 0x1F, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
277 {512, 8000, 4096000, 0x60, 0x00, 0x01, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
278 {512, 16000, 8192000, 0x20, 0x00, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
280 {512, 44100, 22579200, 0x00, 0x00, 0x00, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
281 {512, 48000, 24576000, 0x00, 0x00, 0x00, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
282 {768, 8000, 6144000, 0x60, 0x02, 0x11, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
283 {768, 16000, 12288000, 0x20, 0x02, 0x01, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
284 {800, 48000, 38400000, 0x00, 0x18, 0x13, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
285 {1024, 8000, 8192000, 0x60, 0x00, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
286 {1024, 16000, 16384000, 0x20, 0x00, 0x00, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
287 {1152, 16000, 18432000, 0x20, 0x08, 0x11, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
288 {1536, 8000, 12288000, 0x60, 0x02, 0x01, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
290 {1536, 16000, 24576000, 0x20, 0x02, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
291 {1625, 8000, 13000000, 0x0C, 0x18, 0x1F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
292 {1625, 16000, 26000000, 0x0C, 0x18, 0x1F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
293 {2048, 8000, 16384000, 0x60, 0x00, 0x00, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
294 {2304, 8000, 18432000, 0x40, 0x02, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x5F},
295 {3072, 8000, 24576000, 0x60, 0x02, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
296 {3250, 8000, 26000000, 0x0C, 0x18, 0x0F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
300 static inline int get_coeff(int mclk, int rate)
304 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
305 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
312 static int es8326_set_dai_sysclk(struct snd_soc_dai *codec_dai,
313 int clk_id, unsigned int freq, int dir)
315 struct snd_soc_component *codec = codec_dai->component;
316 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
318 es8326->sysclk = freq;
323 static int es8326_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
325 struct snd_soc_component *component = codec_dai->component;
328 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
329 case SND_SOC_DAIFMT_CBC_CFP:
330 snd_soc_component_update_bits(component, ES8326_RESET,
331 ES8326_MASTER_MODE_EN, ES8326_MASTER_MODE_EN);
333 case SND_SOC_DAIFMT_CBC_CFC:
339 /* interface format */
340 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
341 case SND_SOC_DAIFMT_I2S:
343 case SND_SOC_DAIFMT_RIGHT_J:
344 dev_err(component->dev, "Codec driver does not support right justified\n");
346 case SND_SOC_DAIFMT_LEFT_J:
347 iface |= ES8326_DAIFMT_LEFT_J;
349 case SND_SOC_DAIFMT_DSP_A:
350 iface |= ES8326_DAIFMT_DSP_A;
352 case SND_SOC_DAIFMT_DSP_B:
353 iface |= ES8326_DAIFMT_DSP_B;
359 snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DAIFMT_MASK, iface);
364 static int es8326_pcm_hw_params(struct snd_pcm_substream *substream,
365 struct snd_pcm_hw_params *params,
366 struct snd_soc_dai *dai)
368 struct snd_soc_component *component = dai->component;
369 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
373 coeff = get_coeff(es8326->sysclk, params_rate(params));
375 switch (params_format(params)) {
376 case SNDRV_PCM_FORMAT_S16_LE:
377 srate |= ES8326_S16_LE;
379 case SNDRV_PCM_FORMAT_S20_3LE:
380 srate |= ES8326_S20_3_LE;
382 case SNDRV_PCM_FORMAT_S18_3LE:
383 srate |= ES8326_S18_LE;
385 case SNDRV_PCM_FORMAT_S24_LE:
386 srate |= ES8326_S24_LE;
388 case SNDRV_PCM_FORMAT_S32_LE:
389 srate |= ES8326_S32_LE;
395 /* set iface & srate */
396 snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DATA_LEN_MASK, srate);
399 regmap_write(es8326->regmap, ES8326_CLK_DIV1,
400 coeff_div[coeff].reg4);
401 regmap_write(es8326->regmap, ES8326_CLK_DIV2,
402 coeff_div[coeff].reg5);
403 regmap_write(es8326->regmap, ES8326_CLK_DLL,
404 coeff_div[coeff].reg6);
405 regmap_write(es8326->regmap, ES8326_CLK_MUX,
406 coeff_div[coeff].reg7);
407 regmap_write(es8326->regmap, ES8326_CLK_ADC_SEL,
408 coeff_div[coeff].reg8);
409 regmap_write(es8326->regmap, ES8326_CLK_DAC_SEL,
410 coeff_div[coeff].reg9);
411 regmap_write(es8326->regmap, ES8326_CLK_ADC_OSR,
412 coeff_div[coeff].rega);
413 regmap_write(es8326->regmap, ES8326_CLK_DAC_OSR,
414 coeff_div[coeff].regb);
416 dev_warn(component->dev, "Clock coefficients do not match");
422 static int es8326_set_bias_level(struct snd_soc_component *codec,
423 enum snd_soc_bias_level level)
425 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
429 case SND_SOC_BIAS_ON:
430 ret = clk_prepare_enable(es8326->mclk);
433 regmap_write(es8326->regmap, ES8326_RESET, ES8326_PWRUP_SEQ_EN);
434 regmap_write(es8326->regmap, ES8326_INTOUT_IO, 0x45);
435 regmap_write(es8326->regmap, ES8326_SDINOUT1_IO,
436 (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT));
437 regmap_write(es8326->regmap, ES8326_SDINOUT23_IO, ES8326_IO_INPUT);
438 regmap_write(es8326->regmap, ES8326_CLK_RESAMPLE, 0x05);
439 regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x02);
440 regmap_write(es8326->regmap, ES8326_PGA_PDN, 0x40);
441 regmap_write(es8326->regmap, ES8326_DAC2HPMIX, 0xAA);
442 regmap_write(es8326->regmap, ES8326_RESET, ES8326_CSM_ON);
444 case SND_SOC_BIAS_PREPARE:
446 case SND_SOC_BIAS_STANDBY:
448 case SND_SOC_BIAS_OFF:
449 clk_disable_unprepare(es8326->mclk);
450 regmap_write(es8326->regmap, ES8326_DAC2HPMIX, 0x11);
451 regmap_write(es8326->regmap, ES8326_RESET, ES8326_CSM_OFF);
452 regmap_write(es8326->regmap, ES8326_PGA_PDN, 0xF8);
453 regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x00);
454 regmap_write(es8326->regmap, ES8326_INT_SOURCE, 0x08);
455 regmap_write(es8326->regmap, ES8326_SDINOUT1_IO, ES8326_IO_INPUT);
456 regmap_write(es8326->regmap, ES8326_SDINOUT23_IO, ES8326_IO_INPUT);
457 regmap_write(es8326->regmap, ES8326_RESET,
458 ES8326_CODEC_RESET | ES8326_PWRUP_SEQ_EN);
465 #define es8326_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
466 SNDRV_PCM_FMTBIT_S24_LE)
468 static const struct snd_soc_dai_ops es8326_ops = {
469 .hw_params = es8326_pcm_hw_params,
470 .set_fmt = es8326_set_dai_fmt,
471 .set_sysclk = es8326_set_dai_sysclk,
474 static struct snd_soc_dai_driver es8326_dai = {
475 .name = "ES8326 HiFi",
477 .stream_name = "Playback",
480 .rates = SNDRV_PCM_RATE_8000_48000,
481 .formats = es8326_FORMATS,
484 .stream_name = "Capture",
487 .rates = SNDRV_PCM_RATE_8000_48000,
488 .formats = es8326_FORMATS,
494 static void es8326_enable_micbias(struct snd_soc_component *component)
496 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
498 snd_soc_dapm_mutex_lock(dapm);
499 snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS1");
500 snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS2");
501 snd_soc_dapm_sync_unlocked(dapm);
502 snd_soc_dapm_mutex_unlock(dapm);
505 static void es8326_disable_micbias(struct snd_soc_component *component)
507 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
509 snd_soc_dapm_mutex_lock(dapm);
510 snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS1");
511 snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS2");
512 snd_soc_dapm_sync_unlocked(dapm);
513 snd_soc_dapm_mutex_unlock(dapm);
517 * For button detection, set the following in soundcard
518 * snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
519 * snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
520 * snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
522 static void es8326_jack_button_handler(struct work_struct *work)
524 struct es8326_priv *es8326 =
525 container_of(work, struct es8326_priv, button_press_work.work);
526 struct snd_soc_component *comp = es8326->component;
528 static int button_to_report, press_count;
529 static int prev_button, cur_button;
531 if (!(es8326->jack->status & SND_JACK_HEADSET)) /* Jack unplugged */
534 mutex_lock(&es8326->lock);
535 iface = snd_soc_component_read(comp, ES8326_HP_DETECT);
538 /* pause button detected */
539 cur_button = SND_JACK_BTN_0;
542 /* button volume up */
543 cur_button = SND_JACK_BTN_1;
546 /* button volume down */
547 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 > 10) {
560 /* report a press every 500ms */
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(50));
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(50));
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_HP_DETECT);
595 dev_dbg(comp->dev, "gpio flag %#04x", iface);
596 if ((iface & ES8326_HPINSERT_FLAG) == 0) {
597 /* Jack unplugged or spurious IRQ */
598 dev_dbg(comp->dev, "No headset detected");
599 if (es8326->jack->status & SND_JACK_HEADPHONE) {
600 snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
601 snd_soc_component_write(comp, ES8326_ADC1_SRC, es8326->mic2_src);
602 es8326_disable_micbias(comp);
604 } else if ((iface & ES8326_HPINSERT_FLAG) == ES8326_HPINSERT_FLAG) {
605 if (es8326->jack->status & SND_JACK_HEADSET) {
607 queue_delayed_work(system_wq, &es8326->button_press_work, 10);
609 if ((iface & ES8326_HPBUTTON_FLAG) == 0x00) {
610 dev_dbg(comp->dev, "Headset detected");
611 snd_soc_jack_report(es8326->jack,
612 SND_JACK_HEADSET, SND_JACK_HEADSET);
613 snd_soc_component_write(comp,
614 ES8326_ADC1_SRC, es8326->mic1_src);
616 dev_dbg(comp->dev, "Headphone detected");
617 snd_soc_jack_report(es8326->jack,
618 SND_JACK_HEADPHONE, SND_JACK_HEADSET);
622 mutex_unlock(&es8326->lock);
625 static irqreturn_t es8326_irq(int irq, void *dev_id)
627 struct es8326_priv *es8326 = dev_id;
628 struct snd_soc_component *comp = es8326->component;
633 es8326_enable_micbias(comp);
635 if (es8326->jack->status & SND_JACK_HEADSET)
636 queue_delayed_work(system_wq, &es8326->jack_detect_work,
637 msecs_to_jiffies(10));
639 queue_delayed_work(system_wq, &es8326->jack_detect_work,
640 msecs_to_jiffies(300));
646 static int es8326_resume(struct snd_soc_component *component)
648 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
651 regcache_cache_only(es8326->regmap, false);
652 regcache_sync(es8326->regmap);
654 regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_ON);
655 /* Two channel ADC */
656 regmap_write(es8326->regmap, ES8326_PULLUP_CTL, 0x02);
657 regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00);
658 regmap_write(es8326->regmap, ES8326_CLK_DIV_CPC, 0x1F);
659 regmap_write(es8326->regmap, ES8326_CLK_VMIDS1, 0xC8);
660 regmap_write(es8326->regmap, ES8326_CLK_VMIDS2, 0x88);
661 regmap_write(es8326->regmap, ES8326_CLK_CAL_TIME, 0x20);
662 regmap_write(es8326->regmap, ES8326_SYS_BIAS, 0x08);
663 regmap_write(es8326->regmap, ES8326_DAC2HPMIX, 0x22);
664 regmap_write(es8326->regmap, ES8326_ADC1_SRC, es8326->mic1_src);
665 regmap_write(es8326->regmap, ES8326_ADC2_SRC, es8326->mic2_src);
666 regmap_write(es8326->regmap, ES8326_HPJACK_TIMER, 0x88);
667 regmap_write(es8326->regmap, ES8326_HP_DET,
668 ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol);
669 regmap_write(es8326->regmap, ES8326_INT_SOURCE, es8326->interrupt_src);
670 regmap_write(es8326->regmap, ES8326_INTOUT_IO, es8326->interrupt_clk);
671 regmap_write(es8326->regmap, ES8326_RESET, ES8326_CSM_ON);
672 snd_soc_component_update_bits(component, ES8326_PGAGAIN,
673 ES8326_MIC_SEL_MASK, ES8326_MIC1_SEL);
675 regmap_read(es8326->regmap, ES8326_CHIP_VERSION, ®);
676 if ((reg & ES8326_VERSION_B) == 1) {
677 regmap_write(es8326->regmap, ES8326_ANA_MICBIAS, 0xDD);
678 regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7F);
679 regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x0F);
680 /* enable button detect */
681 regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xA0);
684 es8326_irq(es8326->irq, es8326);
688 static int es8326_suspend(struct snd_soc_component *component)
690 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
692 cancel_delayed_work_sync(&es8326->jack_detect_work);
693 es8326_disable_micbias(component);
695 regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_OFF);
696 regcache_cache_only(es8326->regmap, true);
697 regcache_mark_dirty(es8326->regmap);
702 static int es8326_probe(struct snd_soc_component *component)
704 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
707 es8326->component = component;
708 es8326->jd_inverted = device_property_read_bool(component->dev,
709 "everest,jack-detect-inverted");
711 ret = device_property_read_u8(component->dev, "everest,mic1-src", &es8326->mic1_src);
713 dev_dbg(component->dev, "mic1-src return %d", ret);
714 es8326->mic1_src = ES8326_ADC_AMIC;
716 dev_dbg(component->dev, "mic1-src %x", es8326->mic1_src);
718 ret = device_property_read_u8(component->dev, "everest,mic2-src", &es8326->mic2_src);
720 dev_dbg(component->dev, "mic2-src return %d", ret);
721 es8326->mic2_src = ES8326_ADC_DMIC;
723 dev_dbg(component->dev, "mic2-src %x", es8326->mic2_src);
725 ret = device_property_read_u8(component->dev, "everest,jack-pol", &es8326->jack_pol);
727 dev_dbg(component->dev, "jack-pol return %d", ret);
728 es8326->jack_pol = ES8326_HP_DET_BUTTON_POL | ES8326_HP_TYPE_OMTP;
730 dev_dbg(component->dev, "jack-pol %x", es8326->jack_pol);
732 ret = device_property_read_u8(component->dev, "everest,interrupt-src", &es8326->jack_pol);
734 dev_dbg(component->dev, "interrupt-src return %d", ret);
735 es8326->interrupt_src = ES8326_HP_DET_SRC_PIN9;
737 dev_dbg(component->dev, "interrupt-src %x", es8326->interrupt_src);
739 ret = device_property_read_u8(component->dev, "everest,interrupt-clk", &es8326->jack_pol);
741 dev_dbg(component->dev, "interrupt-clk return %d", ret);
742 es8326->interrupt_clk = 0x45;
744 dev_dbg(component->dev, "interrupt-clk %x", es8326->interrupt_clk);
746 es8326_resume(component);
750 static void es8326_enable_jack_detect(struct snd_soc_component *component,
751 struct snd_soc_jack *jack)
753 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
755 mutex_lock(&es8326->lock);
756 if (es8326->jd_inverted)
757 snd_soc_component_update_bits(component, ES8326_HP_DET,
758 ES8326_HP_DET_JACK_POL, ~es8326->jack_pol);
761 mutex_unlock(&es8326->lock);
762 es8326_irq(es8326->irq, es8326);
765 static void es8326_disable_jack_detect(struct snd_soc_component *component)
767 struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
769 dev_dbg(component->dev, "Enter into %s\n", __func__);
771 return; /* Already disabled (or never enabled) */
772 cancel_delayed_work_sync(&es8326->jack_detect_work);
774 mutex_lock(&es8326->lock);
775 if (es8326->jack->status & SND_JACK_MICROPHONE) {
776 es8326_disable_micbias(component);
777 snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
780 mutex_unlock(&es8326->lock);
783 static int es8326_set_jack(struct snd_soc_component *component,
784 struct snd_soc_jack *jack, void *data)
787 es8326_enable_jack_detect(component, jack);
789 es8326_disable_jack_detect(component);
794 static void es8326_remove(struct snd_soc_component *component)
796 es8326_disable_jack_detect(component);
797 es8326_set_bias_level(component, SND_SOC_BIAS_OFF);
800 static const struct snd_soc_component_driver soc_component_dev_es8326 = {
801 .probe = es8326_probe,
802 .remove = es8326_remove,
803 .resume = es8326_resume,
804 .suspend = es8326_suspend,
805 .set_bias_level = es8326_set_bias_level,
806 .set_jack = es8326_set_jack,
807 .dapm_widgets = es8326_dapm_widgets,
808 .num_dapm_widgets = ARRAY_SIZE(es8326_dapm_widgets),
809 .dapm_routes = es8326_dapm_routes,
810 .num_dapm_routes = ARRAY_SIZE(es8326_dapm_routes),
811 .controls = es8326_snd_controls,
812 .num_controls = ARRAY_SIZE(es8326_snd_controls),
813 .use_pmdown_time = 1,
817 static int es8326_i2c_probe(struct i2c_client *i2c)
819 struct es8326_priv *es8326;
822 es8326 = devm_kzalloc(&i2c->dev, sizeof(struct es8326_priv), GFP_KERNEL);
826 i2c_set_clientdata(i2c, es8326);
828 mutex_init(&es8326->lock);
829 es8326->regmap = devm_regmap_init_i2c(i2c, &es8326_regmap_config);
830 if (IS_ERR(es8326->regmap)) {
831 ret = PTR_ERR(es8326->regmap);
832 dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
836 es8326->irq = i2c->irq;
837 INIT_DELAYED_WORK(&es8326->jack_detect_work,
838 es8326_jack_detect_handler);
839 INIT_DELAYED_WORK(&es8326->button_press_work,
840 es8326_jack_button_handler);
841 /* ES8316 is level-based while ES8326 is edge-based */
842 ret = devm_request_threaded_irq(&i2c->dev, es8326->irq, NULL, es8326_irq,
843 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
846 dev_warn(&i2c->dev, "Failed to request IRQ: %d: %d\n",
848 es8326->irq = -ENXIO;
851 es8326->mclk = devm_clk_get_optional(&i2c->dev, "mclk");
852 if (IS_ERR(es8326->mclk)) {
853 dev_err(&i2c->dev, "unable to get mclk\n");
854 return PTR_ERR(es8326->mclk);
857 dev_warn(&i2c->dev, "assuming static mclk\n");
859 ret = clk_prepare_enable(es8326->mclk);
861 dev_err(&i2c->dev, "unable to enable mclk\n");
864 return devm_snd_soc_register_component(&i2c->dev,
865 &soc_component_dev_es8326,
869 static const struct i2c_device_id es8326_i2c_id[] = {
873 MODULE_DEVICE_TABLE(i2c, es8326_i2c_id);
876 static const struct of_device_id es8326_of_match[] = {
877 { .compatible = "everest,es8326", },
880 MODULE_DEVICE_TABLE(of, es8326_of_match);
884 static const struct acpi_device_id es8326_acpi_match[] = {
888 MODULE_DEVICE_TABLE(acpi, es8326_acpi_match);
891 static struct i2c_driver es8326_i2c_driver = {
894 .acpi_match_table = ACPI_PTR(es8326_acpi_match),
895 .of_match_table = of_match_ptr(es8326_of_match),
897 .probe_new = es8326_i2c_probe,
898 .id_table = es8326_i2c_id,
900 module_i2c_driver(es8326_i2c_driver);
902 MODULE_DESCRIPTION("ASoC es8326 driver");
903 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
904 MODULE_LICENSE("GPL");