xref: /linux/sound/soc/codecs/es8326.c (revision 966323dd9a65dde599f59176280468a0cb04c875)
15c439937SZhu Ning // SPDX-License-Identifier: GPL-2.0-only
25c439937SZhu Ning //
35c439937SZhu Ning // es8326.c -- es8326 ALSA SoC audio driver
45c439937SZhu Ning // Copyright Everest Semiconductor Co., Ltd
55c439937SZhu Ning //
65c439937SZhu Ning // Authors: David Yang <yangxiaohua@everest-semi.com>
75c439937SZhu Ning //
85c439937SZhu Ning 
95c439937SZhu Ning #include <linux/clk.h>
105c439937SZhu Ning #include <linux/i2c.h>
115c439937SZhu Ning #include <linux/interrupt.h>
125c439937SZhu Ning #include <linux/irq.h>
135c439937SZhu Ning #include <linux/module.h>
145c439937SZhu Ning #include <sound/jack.h>
155c439937SZhu Ning #include <sound/pcm_params.h>
165c439937SZhu Ning #include <sound/soc.h>
175c439937SZhu Ning #include <sound/soc-dapm.h>
185c439937SZhu Ning #include <sound/tlv.h>
195c439937SZhu Ning #include "es8326.h"
205c439937SZhu Ning 
215c439937SZhu Ning struct es8326_priv {
225c439937SZhu Ning 	struct clk *mclk;
235c439937SZhu Ning 	struct i2c_client *i2c;
245c439937SZhu Ning 	struct regmap *regmap;
255c439937SZhu Ning 	struct snd_soc_component *component;
265c439937SZhu Ning 	struct delayed_work jack_detect_work;
275c439937SZhu Ning 	struct delayed_work button_press_work;
285c439937SZhu Ning 	struct snd_soc_jack *jack;
295c439937SZhu Ning 	int irq;
305c439937SZhu Ning 	/* The lock protects the situation that an irq is generated
315c439937SZhu Ning 	 * while enabling or disabling or during an irq.
325c439937SZhu Ning 	 */
335c439937SZhu Ning 	struct mutex lock;
345c439937SZhu Ning 	u8 mic1_src;
355c439937SZhu Ning 	u8 mic2_src;
365c439937SZhu Ning 	u8 jack_pol;
375c439937SZhu Ning 	u8 interrupt_src;
385c439937SZhu Ning 	u8 interrupt_clk;
39*966323ddSZhu Ning 	u8 hpl_vol;
40*966323ddSZhu Ning 	u8 hpr_vol;
415c439937SZhu Ning 	bool jd_inverted;
425c439937SZhu Ning 	unsigned int sysclk;
43083912c2SZhu Ning 
44083912c2SZhu Ning 	bool calibrated;
45083912c2SZhu Ning 	int version;
4604f96c93SZhu Ning 	int hp;
4704f96c93SZhu Ning 	int jack_remove_retry;
485c439937SZhu Ning };
495c439937SZhu Ning 
50523d242dSZhu Ning static int es8326_crosstalk1_get(struct snd_kcontrol *kcontrol,
51523d242dSZhu Ning 		struct snd_ctl_elem_value *ucontrol)
52523d242dSZhu Ning {
53523d242dSZhu Ning 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
54523d242dSZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
55523d242dSZhu Ning 	unsigned int crosstalk_h, crosstalk_l;
56523d242dSZhu Ning 	unsigned int crosstalk;
57523d242dSZhu Ning 
58523d242dSZhu Ning 	regmap_read(es8326->regmap, ES8326_DAC_RAMPRATE, &crosstalk_h);
59523d242dSZhu Ning 	regmap_read(es8326->regmap, ES8326_DAC_CROSSTALK, &crosstalk_l);
60523d242dSZhu Ning 	crosstalk_h &= 0x20;
61523d242dSZhu Ning 	crosstalk_l &= 0xf0;
62523d242dSZhu Ning 	crosstalk = crosstalk_h >> 1 | crosstalk_l >> 4;
63523d242dSZhu Ning 	ucontrol->value.integer.value[0] = crosstalk;
64523d242dSZhu Ning 
65523d242dSZhu Ning 	return 0;
66523d242dSZhu Ning }
67523d242dSZhu Ning 
68523d242dSZhu Ning static int es8326_crosstalk1_set(struct snd_kcontrol *kcontrol,
69523d242dSZhu Ning 		struct snd_ctl_elem_value *ucontrol)
70523d242dSZhu Ning {
71523d242dSZhu Ning 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
72523d242dSZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
73523d242dSZhu Ning 	unsigned int crosstalk_h, crosstalk_l;
74523d242dSZhu Ning 	unsigned int crosstalk;
75523d242dSZhu Ning 
76523d242dSZhu Ning 	crosstalk = ucontrol->value.integer.value[0];
77523d242dSZhu Ning 	regmap_read(es8326->regmap, ES8326_DAC_CROSSTALK, &crosstalk_l);
78523d242dSZhu Ning 	crosstalk_h = (crosstalk & 0x10) << 1;
79523d242dSZhu Ning 	crosstalk_l &= 0x0f;
80523d242dSZhu Ning 	crosstalk_l |= (crosstalk & 0x0f) << 4;
81523d242dSZhu Ning 	regmap_update_bits(es8326->regmap, ES8326_DAC_RAMPRATE,
82523d242dSZhu Ning 			0x20, crosstalk_h);
83523d242dSZhu Ning 	regmap_write(es8326->regmap, ES8326_DAC_CROSSTALK, crosstalk_l);
84523d242dSZhu Ning 
85523d242dSZhu Ning 	return 0;
86523d242dSZhu Ning }
87523d242dSZhu Ning 
88523d242dSZhu Ning static int es8326_crosstalk2_get(struct snd_kcontrol *kcontrol,
89523d242dSZhu Ning 		struct snd_ctl_elem_value *ucontrol)
90523d242dSZhu Ning {
91523d242dSZhu Ning 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
92523d242dSZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
93523d242dSZhu Ning 	unsigned int crosstalk_h, crosstalk_l;
94523d242dSZhu Ning 	unsigned int crosstalk;
95523d242dSZhu Ning 
96523d242dSZhu Ning 	regmap_read(es8326->regmap, ES8326_DAC_RAMPRATE, &crosstalk_h);
97523d242dSZhu Ning 	regmap_read(es8326->regmap, ES8326_DAC_CROSSTALK, &crosstalk_l);
98523d242dSZhu Ning 	crosstalk_h &= 0x10;
99523d242dSZhu Ning 	crosstalk_l &= 0x0f;
100523d242dSZhu Ning 	crosstalk = crosstalk_h  | crosstalk_l;
101523d242dSZhu Ning 	ucontrol->value.integer.value[0] = crosstalk;
102523d242dSZhu Ning 
103523d242dSZhu Ning 	return 0;
104523d242dSZhu Ning }
105523d242dSZhu Ning 
106523d242dSZhu Ning static int es8326_crosstalk2_set(struct snd_kcontrol *kcontrol,
107523d242dSZhu Ning 		struct snd_ctl_elem_value *ucontrol)
108523d242dSZhu Ning {
109523d242dSZhu Ning 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
110523d242dSZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
111523d242dSZhu Ning 	unsigned int crosstalk_h, crosstalk_l;
112523d242dSZhu Ning 	unsigned int crosstalk;
113523d242dSZhu Ning 
114523d242dSZhu Ning 	crosstalk = ucontrol->value.integer.value[0];
115523d242dSZhu Ning 	regmap_read(es8326->regmap, ES8326_DAC_CROSSTALK, &crosstalk_l);
116523d242dSZhu Ning 	crosstalk_h = crosstalk & 0x10;
117523d242dSZhu Ning 	crosstalk_l &= 0xf0;
118523d242dSZhu Ning 	crosstalk_l |= crosstalk & 0x0f;
119523d242dSZhu Ning 	regmap_update_bits(es8326->regmap, ES8326_DAC_RAMPRATE,
120523d242dSZhu Ning 			0x10, crosstalk_h);
121523d242dSZhu Ning 	regmap_write(es8326->regmap, ES8326_DAC_CROSSTALK, crosstalk_l);
122523d242dSZhu Ning 
123523d242dSZhu Ning 	return 0;
124523d242dSZhu Ning }
125523d242dSZhu Ning 
126*966323ddSZhu Ning static int es8326_hplvol_get(struct snd_kcontrol *kcontrol,
127*966323ddSZhu Ning 		struct snd_ctl_elem_value *ucontrol)
128*966323ddSZhu Ning {
129*966323ddSZhu Ning 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
130*966323ddSZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
131*966323ddSZhu Ning 
132*966323ddSZhu Ning 	ucontrol->value.integer.value[0] = es8326->hpl_vol;
133*966323ddSZhu Ning 
134*966323ddSZhu Ning 	return 0;
135*966323ddSZhu Ning }
136*966323ddSZhu Ning 
137*966323ddSZhu Ning static int es8326_hplvol_set(struct snd_kcontrol *kcontrol,
138*966323ddSZhu Ning 		struct snd_ctl_elem_value *ucontrol)
139*966323ddSZhu Ning {
140*966323ddSZhu Ning 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
141*966323ddSZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
142*966323ddSZhu Ning 	unsigned int hp_vol;
143*966323ddSZhu Ning 
144*966323ddSZhu Ning 	hp_vol = ucontrol->value.integer.value[0];
145*966323ddSZhu Ning 	if (hp_vol > 5)
146*966323ddSZhu Ning 		return -EINVAL;
147*966323ddSZhu Ning 	if (es8326->hpl_vol != hp_vol) {
148*966323ddSZhu Ning 		es8326->hpl_vol = hp_vol;
149*966323ddSZhu Ning 		if (hp_vol >= 3)
150*966323ddSZhu Ning 			hp_vol++;
151*966323ddSZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_HP_VOL,
152*966323ddSZhu Ning 				0x70, (hp_vol << 4));
153*966323ddSZhu Ning 		return 1;
154*966323ddSZhu Ning 	}
155*966323ddSZhu Ning 
156*966323ddSZhu Ning 	return 0;
157*966323ddSZhu Ning }
158*966323ddSZhu Ning 
159*966323ddSZhu Ning static int es8326_hprvol_get(struct snd_kcontrol *kcontrol,
160*966323ddSZhu Ning 		struct snd_ctl_elem_value *ucontrol)
161*966323ddSZhu Ning {
162*966323ddSZhu Ning 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
163*966323ddSZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
164*966323ddSZhu Ning 
165*966323ddSZhu Ning 	ucontrol->value.integer.value[0] = es8326->hpr_vol;
166*966323ddSZhu Ning 
167*966323ddSZhu Ning 	return 0;
168*966323ddSZhu Ning }
169*966323ddSZhu Ning 
170*966323ddSZhu Ning static int es8326_hprvol_set(struct snd_kcontrol *kcontrol,
171*966323ddSZhu Ning 		struct snd_ctl_elem_value *ucontrol)
172*966323ddSZhu Ning {
173*966323ddSZhu Ning 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
174*966323ddSZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
175*966323ddSZhu Ning 	unsigned int hp_vol;
176*966323ddSZhu Ning 
177*966323ddSZhu Ning 	hp_vol = ucontrol->value.integer.value[0];
178*966323ddSZhu Ning 	if (hp_vol > 5)
179*966323ddSZhu Ning 		return -EINVAL;
180*966323ddSZhu Ning 	if (es8326->hpr_vol != hp_vol) {
181*966323ddSZhu Ning 		es8326->hpr_vol = hp_vol;
182*966323ddSZhu Ning 		if (hp_vol >= 3)
183*966323ddSZhu Ning 			hp_vol++;
184*966323ddSZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_HP_VOL,
185*966323ddSZhu Ning 				0x07, hp_vol);
186*966323ddSZhu Ning 		return 1;
187*966323ddSZhu Ning 	}
188*966323ddSZhu Ning 
189*966323ddSZhu Ning 	return 0;
190*966323ddSZhu Ning }
191*966323ddSZhu Ning 
1925c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9550, 50, 0);
1935c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9550, 50, 0);
1945c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_analog_pga_tlv, 0, 300, 0);
1955c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_pga_tlv, 0, 600, 0);
1965c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(softramp_rate, 0, 100, 0);
1975c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_target_tlv, -3200, 200, 0);
1985c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_recovery_tlv, -125, 250, 0);
1995c439937SZhu Ning 
2005c439937SZhu Ning static const char *const winsize[] = {
2015c439937SZhu Ning 	"0.25db/2  LRCK",
2025c439937SZhu Ning 	"0.25db/4  LRCK",
2035c439937SZhu Ning 	"0.25db/8  LRCK",
2045c439937SZhu Ning 	"0.25db/16  LRCK",
2055c439937SZhu Ning 	"0.25db/32  LRCK",
2065c439937SZhu Ning 	"0.25db/64  LRCK",
2075c439937SZhu Ning 	"0.25db/128  LRCK",
2085c439937SZhu Ning 	"0.25db/256  LRCK",
2095c439937SZhu Ning 	"0.25db/512  LRCK",
2105c439937SZhu Ning 	"0.25db/1024  LRCK",
2115c439937SZhu Ning 	"0.25db/2048  LRCK",
2125c439937SZhu Ning 	"0.25db/4096  LRCK",
2135c439937SZhu Ning 	"0.25db/8192  LRCK",
2145c439937SZhu Ning 	"0.25db/16384  LRCK",
2155c439937SZhu Ning 	"0.25db/32768  LRCK",
2165c439937SZhu Ning 	"0.25db/65536  LRCK",
2175c439937SZhu Ning };
2185c439937SZhu Ning 
2195c439937SZhu Ning static const char *const dacpol_txt[] =	{
2205c439937SZhu Ning 	"Normal", "R Invert", "L Invert", "L + R Invert" };
2215c439937SZhu Ning 
222*966323ddSZhu Ning static const char *const hp_spkvol_switch[] = {
223*966323ddSZhu Ning 	"HPVOL: HPL+HPL, SPKVOL: HPL+HPL",
224*966323ddSZhu Ning 	"HPVOL: HPL+HPR, SPKVOL: HPL+HPR",
225*966323ddSZhu Ning 	"HPVOL: HPL+HPL, SPKVOL: SPKL+SPKR",
226*966323ddSZhu Ning 	"HPVOL: HPL+HPR, SPKVOL: SPKL+SPKR",
227*966323ddSZhu Ning };
228*966323ddSZhu Ning 
2295c439937SZhu Ning static const struct soc_enum dacpol =
2305c439937SZhu Ning 	SOC_ENUM_SINGLE(ES8326_DAC_DSM, 4, 4, dacpol_txt);
2315c439937SZhu Ning static const struct soc_enum alc_winsize =
2325c439937SZhu Ning 	SOC_ENUM_SINGLE(ES8326_ADC_RAMPRATE, 4, 16, winsize);
2335c439937SZhu Ning static const struct soc_enum drc_winsize =
2345c439937SZhu Ning 	SOC_ENUM_SINGLE(ES8326_DRC_WINSIZE, 4, 16, winsize);
235*966323ddSZhu Ning static const struct soc_enum hpvol_spkvol_switch =
236*966323ddSZhu Ning 	SOC_ENUM_SINGLE(ES8326_HP_MISC, 6, 4, hp_spkvol_switch);
2375c439937SZhu Ning 
2385c439937SZhu Ning static const struct snd_kcontrol_new es8326_snd_controls[] = {
239*966323ddSZhu Ning 	SOC_SINGLE_TLV("DAC Playback Volume", ES8326_DACL_VOL, 0, 0xbf, 0, dac_vol_tlv),
2405c439937SZhu Ning 	SOC_ENUM("Playback Polarity", dacpol),
2415c439937SZhu Ning 	SOC_SINGLE_TLV("DAC Ramp Rate", ES8326_DAC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
2425c439937SZhu Ning 	SOC_SINGLE_TLV("DRC Recovery Level", ES8326_DRC_RECOVERY, 0, 4, 0, drc_recovery_tlv),
2435c439937SZhu Ning 	SOC_ENUM("DRC Winsize", drc_winsize),
2445c439937SZhu Ning 	SOC_SINGLE_TLV("DRC Target Level", ES8326_DRC_WINSIZE, 0, 0x0f, 0, drc_target_tlv),
2455c439937SZhu Ning 
2465c439937SZhu Ning 	SOC_DOUBLE_R_TLV("ADC Capture Volume", ES8326_ADC1_VOL, ES8326_ADC2_VOL, 0, 0xff, 0,
2475c439937SZhu Ning 			 adc_vol_tlv),
2485c439937SZhu Ning 	SOC_DOUBLE_TLV("ADC PGA Volume", ES8326_ADC_SCALE, 4, 0, 5, 0, adc_pga_tlv),
2495c439937SZhu Ning 	SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8326_PGAGAIN, 0, 10, 0, adc_analog_pga_tlv),
2505c439937SZhu Ning 	SOC_SINGLE_TLV("ADC Ramp Rate", ES8326_ADC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
2515c439937SZhu Ning 	SOC_SINGLE("ALC Capture Switch", ES8326_ALC_RECOVERY, 3, 1, 0),
2525c439937SZhu Ning 	SOC_SINGLE_TLV("ALC Capture Recovery Level", ES8326_ALC_LEVEL,
2535c439937SZhu Ning 			0, 4, 0, drc_recovery_tlv),
2545c439937SZhu Ning 	SOC_ENUM("ALC Capture Winsize", alc_winsize),
2555c439937SZhu Ning 	SOC_SINGLE_TLV("ALC Capture Target Level", ES8326_ALC_LEVEL,
2565c439937SZhu Ning 			0, 0x0f, 0, drc_target_tlv),
2575c439937SZhu Ning 
258523d242dSZhu Ning 	SOC_SINGLE_EXT("CROSSTALK1", SND_SOC_NOPM, 0, 31, 0,
259523d242dSZhu Ning 			es8326_crosstalk1_get, es8326_crosstalk1_set),
260523d242dSZhu Ning 	SOC_SINGLE_EXT("CROSSTALK2", SND_SOC_NOPM, 0, 31, 0,
261523d242dSZhu Ning 			es8326_crosstalk2_get, es8326_crosstalk2_set),
262*966323ddSZhu Ning 	SOC_SINGLE_EXT("HPL Volume", SND_SOC_NOPM, 0, 5, 0,
263*966323ddSZhu Ning 			es8326_hplvol_get, es8326_hplvol_set),
264*966323ddSZhu Ning 	SOC_SINGLE_EXT("HPR Volume", SND_SOC_NOPM, 0, 5, 0,
265*966323ddSZhu Ning 			es8326_hprvol_get, es8326_hprvol_set),
266*966323ddSZhu Ning 
267*966323ddSZhu Ning 	SOC_SINGLE_TLV("HPL Playback Volume", ES8326_DACL_VOL, 0, 0xbf, 0, dac_vol_tlv),
268*966323ddSZhu Ning 	SOC_SINGLE_TLV("HPR Playback Volume", ES8326_DACR_VOL, 0, 0xbf, 0, dac_vol_tlv),
269*966323ddSZhu Ning 	SOC_SINGLE_TLV("SPKL Playback Volume", ES8326_SPKL_VOL, 0, 0xbf, 0, dac_vol_tlv),
270*966323ddSZhu Ning 	SOC_SINGLE_TLV("SPKR Playback Volume", ES8326_SPKR_VOL, 0, 0xbf, 0, dac_vol_tlv),
271*966323ddSZhu Ning 
272*966323ddSZhu Ning 	SOC_ENUM("HPVol SPKVol Switch", hpvol_spkvol_switch),
2735c439937SZhu Ning };
2745c439937SZhu Ning 
2755c439937SZhu Ning static const struct snd_soc_dapm_widget es8326_dapm_widgets[] = {
2765c439937SZhu Ning 	SND_SOC_DAPM_INPUT("MIC1"),
2775c439937SZhu Ning 	SND_SOC_DAPM_INPUT("MIC2"),
2785c439937SZhu Ning 	SND_SOC_DAPM_INPUT("MIC3"),
2795c439937SZhu Ning 	SND_SOC_DAPM_INPUT("MIC4"),
2805c439937SZhu Ning 
2815c439937SZhu Ning 	SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
2825c439937SZhu Ning 	SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
2835c439937SZhu Ning 
2845c439937SZhu Ning 	/* Digital Interface */
2855c439937SZhu Ning 	SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 0, SND_SOC_NOPM, 0, 0),
2865c439937SZhu Ning 	SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0, SND_SOC_NOPM, 0, 0),
2875c439937SZhu Ning 
2885c439937SZhu Ning 	/* Analog Power Supply*/
2895c439937SZhu Ning 	SND_SOC_DAPM_DAC("Right DAC", NULL, ES8326_ANA_PDN, 0, 1),
2905c439937SZhu Ning 	SND_SOC_DAPM_DAC("Left DAC", NULL, ES8326_ANA_PDN, 1, 1),
2915c439937SZhu Ning 	SND_SOC_DAPM_SUPPLY("MICBIAS1", ES8326_ANA_MICBIAS, 2, 0, NULL, 0),
2925c439937SZhu Ning 	SND_SOC_DAPM_SUPPLY("MICBIAS2", ES8326_ANA_MICBIAS, 3, 0, NULL, 0),
2935c439937SZhu Ning 
2945c439937SZhu Ning 	SND_SOC_DAPM_PGA("LHPMIX", ES8326_DAC2HPMIX, 7, 0, NULL, 0),
2955c439937SZhu Ning 	SND_SOC_DAPM_PGA("RHPMIX", ES8326_DAC2HPMIX, 3, 0, NULL, 0),
2965c439937SZhu Ning 
297fc702b2cSZhu Ning 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPOR Supply", ES8326_HP_CAL,
298fc702b2cSZhu Ning 			 4, 7, 0, 0),
299fc702b2cSZhu Ning 	SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPOL Supply", ES8326_HP_CAL,
300fc702b2cSZhu Ning 			 0, 7, 0, 0),
301fc702b2cSZhu Ning 
3025c439937SZhu Ning 	SND_SOC_DAPM_OUTPUT("HPOL"),
3035c439937SZhu Ning 	SND_SOC_DAPM_OUTPUT("HPOR"),
3045c439937SZhu Ning };
3055c439937SZhu Ning 
3065c439937SZhu Ning static const struct snd_soc_dapm_route es8326_dapm_routes[] = {
3078c99a0a6SZhu Ning 	{"ADC L", NULL, "MIC1"},
3088c99a0a6SZhu Ning 	{"ADC R", NULL, "MIC2"},
3098c99a0a6SZhu Ning 	{"ADC L", NULL, "MIC3"},
3108c99a0a6SZhu Ning 	{"ADC R", NULL, "MIC4"},
3115c439937SZhu Ning 
3125c439937SZhu Ning 	{"I2S OUT", NULL, "ADC L"},
3135c439937SZhu Ning 	{"I2S OUT", NULL, "ADC R"},
3145c439937SZhu Ning 
3155c439937SZhu Ning 	{"Right DAC", NULL, "I2S IN"},
3165c439937SZhu Ning 	{"Left DAC", NULL, "I2S IN"},
3175c439937SZhu Ning 
3185c439937SZhu Ning 	{"LHPMIX", NULL, "Left DAC"},
3195c439937SZhu Ning 	{"RHPMIX", NULL, "Right DAC"},
3205c439937SZhu Ning 
321fc702b2cSZhu Ning 	{"HPOR", NULL, "HPOR Supply"},
322fc702b2cSZhu Ning 	{"HPOL", NULL, "HPOL Supply"},
323fc702b2cSZhu Ning 
3245c439937SZhu Ning 	{"HPOL", NULL, "LHPMIX"},
3255c439937SZhu Ning 	{"HPOR", NULL, "RHPMIX"},
3265c439937SZhu Ning };
3275c439937SZhu Ning 
328f1230a27SZhu Ning static bool es8326_volatile_register(struct device *dev, unsigned int reg)
329f1230a27SZhu Ning {
330f1230a27SZhu Ning 	switch (reg) {
331f1230a27SZhu Ning 	case ES8326_HPL_OFFSET_INI:
332f1230a27SZhu Ning 	case ES8326_HPR_OFFSET_INI:
333f1230a27SZhu Ning 	case ES8326_HPDET_STA:
334f1230a27SZhu Ning 	case ES8326_CTIA_OMTP_STA:
335f1230a27SZhu Ning 	case ES8326_CSM_MUTE_STA:
336f1230a27SZhu Ning 		return true;
337f1230a27SZhu Ning 	default:
338f1230a27SZhu Ning 		return false;
339f1230a27SZhu Ning 	}
340f1230a27SZhu Ning }
3415c439937SZhu Ning 
3425c69f11cSYang Yingliang static const struct regmap_config es8326_regmap_config = {
3435c439937SZhu Ning 	.reg_bits = 8,
3445c439937SZhu Ning 	.val_bits = 8,
3455c439937SZhu Ning 	.max_register = 0xff,
346f1230a27SZhu Ning 	.volatile_reg = es8326_volatile_register,
3475c439937SZhu Ning 	.cache_type = REGCACHE_RBTREE,
3485c439937SZhu Ning };
3495c439937SZhu Ning 
3505c439937SZhu Ning struct _coeff_div {
3515c439937SZhu Ning 	u16 fs;
3525c439937SZhu Ning 	u32 rate;
3535c439937SZhu Ning 	u32 mclk;
3545c439937SZhu Ning 	u8 reg4;
3555c439937SZhu Ning 	u8 reg5;
3565c439937SZhu Ning 	u8 reg6;
3575c439937SZhu Ning 	u8 reg7;
3585c439937SZhu Ning 	u8 reg8;
3595c439937SZhu Ning 	u8 reg9;
3605c439937SZhu Ning 	u8 rega;
3615c439937SZhu Ning 	u8 regb;
3625c439937SZhu Ning };
3635c439937SZhu Ning 
3645c439937SZhu Ning /* codec hifi mclk clock divider coefficients */
3655c439937SZhu Ning /* {ratio, LRCK, MCLK, REG04, REG05, REG06, REG07, REG08, REG09, REG10, REG11} */
366ee09084fSZhu Ning static const struct _coeff_div coeff_div_v0[] = {
367ee09084fSZhu Ning 	{64, 8000, 512000, 0x60, 0x01, 0x0F, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
368ee09084fSZhu Ning 	{64, 16000, 1024000, 0x20, 0x00, 0x33, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
369ee09084fSZhu Ning 	{64, 44100, 2822400, 0xE0, 0x00, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
370ee09084fSZhu Ning 	{64, 48000, 3072000, 0xE0, 0x00, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
371ee09084fSZhu Ning 	{128, 8000, 1024000, 0x60, 0x00, 0x33, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
372ee09084fSZhu Ning 	{128, 16000, 2048000, 0x20, 0x00, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
373ee09084fSZhu Ning 	{128, 44100, 5644800, 0xE0, 0x01, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
374ee09084fSZhu Ning 	{128, 48000, 6144000, 0xE0, 0x01, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
3755c439937SZhu Ning 
376ee09084fSZhu Ning 	{192, 32000, 6144000, 0xE0, 0x02, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
377ee09084fSZhu Ning 	{256, 8000, 2048000, 0x60, 0x00, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
378ee09084fSZhu Ning 	{256, 16000, 4096000, 0x20, 0x01, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
379ee09084fSZhu Ning 	{256, 44100, 11289600, 0xE0, 0x00, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
380ee09084fSZhu Ning 	{256, 48000, 12288000, 0xE0, 0x00, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
381ee09084fSZhu Ning 	{384, 32000, 12288000, 0xE0, 0x05, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
382ee09084fSZhu Ning 	{400, 48000, 19200000, 0xE9, 0x04, 0x0F, 0x6d, 0x4A, 0x0A, 0x1F, 0x1F},
3835c439937SZhu Ning 
384ee09084fSZhu Ning 	{500, 48000, 24000000, 0xF8, 0x04, 0x3F, 0x6D, 0x4A, 0x0A, 0x1F, 0x1F},
385ee09084fSZhu Ning 	{512, 8000, 4096000, 0x60, 0x01, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
386ee09084fSZhu Ning 	{512, 16000, 8192000, 0x20, 0x00, 0x30, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
387ee09084fSZhu Ning 	{512, 44100, 22579200, 0xE0, 0x00, 0x00, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
388ee09084fSZhu Ning 	{512, 48000, 24576000, 0xE0, 0x00, 0x00, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
389ee09084fSZhu Ning 	{768, 32000, 24576000, 0xE0, 0x02, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
390ee09084fSZhu Ning 	{1024, 8000, 8192000, 0x60, 0x00, 0x30, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
3915c439937SZhu Ning 	{1024, 16000, 16384000, 0x20, 0x00, 0x00, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
3925c439937SZhu Ning };
3935c439937SZhu Ning 
394ee09084fSZhu Ning static const struct _coeff_div coeff_div_v3[] = {
395ee09084fSZhu Ning 	{32, 8000, 256000, 0x60, 0x00, 0x0F, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
396ee09084fSZhu Ning 	{32, 16000, 512000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
397ee09084fSZhu Ning 	{32, 44100, 1411200, 0x00, 0x00, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
398ee09084fSZhu Ning 	{32, 48000, 1536000, 0x00, 0x00, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
399ee09084fSZhu Ning 	{36, 8000, 288000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x23, 0x47},
400ee09084fSZhu Ning 	{36, 16000, 576000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x23, 0x47},
401ee09084fSZhu Ning 	{48, 8000, 384000, 0x60, 0x02, 0x1F, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
402ee09084fSZhu Ning 	{48, 16000, 768000, 0x20, 0x02, 0x0F, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
403ee09084fSZhu Ning 	{48, 48000, 2304000, 0x00, 0x02, 0x0D, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
404ee09084fSZhu Ning 
405ee09084fSZhu Ning 	{64, 8000, 512000, 0x60, 0x00, 0x35, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
406ee09084fSZhu Ning 	{64, 16000, 1024000, 0x20, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
407ee09084fSZhu Ning 	{64, 44100, 2822400, 0xE0, 0x00, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
408ee09084fSZhu Ning 	{64, 48000, 3072000, 0xE0, 0x00, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
409ee09084fSZhu Ning 	{72, 8000, 576000, 0x20, 0x00, 0x13, 0x35, 0x8A, 0x1B, 0x23, 0x47},
410ee09084fSZhu Ning 	{72, 16000, 1152000, 0x20, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x23, 0x47},
411ee09084fSZhu Ning 	{96, 8000, 768000, 0x60, 0x02, 0x1D, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
412ee09084fSZhu Ning 	{96, 16000, 1536000, 0x20, 0x02, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
413ee09084fSZhu Ning 	{100, 48000, 4800000, 0x04, 0x04, 0x3F, 0x6D, 0xB8, 0x08, 0x4f, 0x1f},
414ee09084fSZhu Ning 	{125, 48000, 6000000, 0x04, 0x04, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
415ee09084fSZhu Ning 
416ee09084fSZhu Ning 	{128, 8000, 1024000, 0x60, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
417ee09084fSZhu Ning 	{128, 16000, 2048000, 0x20, 0x00, 0x31, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
418ee09084fSZhu Ning 	{128, 44100, 5644800, 0xE0, 0x00, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
419ee09084fSZhu Ning 	{128, 48000, 6144000, 0xE0, 0x00, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
420ee09084fSZhu Ning 	{144, 8000, 1152000, 0x20, 0x00, 0x03, 0x35, 0x8A, 0x1B, 0x23, 0x47},
421ee09084fSZhu Ning 	{144, 16000, 2304000, 0x20, 0x00, 0x11, 0x35, 0x8A, 0x1B, 0x23, 0x47},
422ee09084fSZhu Ning 	{192, 8000, 1536000, 0x60, 0x02, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
423ee09084fSZhu Ning 	{192, 32000, 6144000, 0xE0, 0x02, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
424ee09084fSZhu Ning 	{192, 16000, 3072000, 0x20, 0x02, 0x05, 0x75, 0xCA, 0x1B, 0x1F, 0x3F},
425ee09084fSZhu Ning 
426ee09084fSZhu Ning 	{200, 48000, 9600000, 0x04, 0x04, 0x0F, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
427ee09084fSZhu Ning 	{250, 48000, 12000000, 0x04, 0x04, 0x0F, 0x2D, 0xCA, 0x0A, 0x27, 0x27},
428ee09084fSZhu Ning 	{256, 8000, 2048000, 0x60, 0x00, 0x31, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
429ee09084fSZhu Ning 	{256, 16000, 4096000, 0x20, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
430ee09084fSZhu Ning 	{256, 44100, 11289600, 0xE0, 0x00, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
431ee09084fSZhu Ning 	{256, 48000, 12288000, 0xE0, 0x00, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
432ee09084fSZhu Ning 	{288, 8000, 2304000, 0x20, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x23, 0x47},
433ee09084fSZhu Ning 	{384, 8000, 3072000, 0x60, 0x02, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
434ee09084fSZhu Ning 	{384, 16000, 6144000, 0x20, 0x02, 0x03, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
435ee09084fSZhu Ning 	{384, 32000, 12288000, 0xE0, 0x02, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
436ee09084fSZhu Ning 	{384, 48000, 18432000, 0x00, 0x02, 0x01, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
437ee09084fSZhu Ning 
438ee09084fSZhu Ning 	{400, 48000, 19200000, 0xE4, 0x04, 0x35, 0x6d, 0xCA, 0x0A, 0x1F, 0x1F},
439ee09084fSZhu Ning 	{500, 48000, 24000000, 0xF8, 0x04, 0x3F, 0x6D, 0xCA, 0x0A, 0x1F, 0x1F},
440ee09084fSZhu Ning 	{512, 8000, 4096000, 0x60, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
441ee09084fSZhu Ning 	{512, 16000, 8192000, 0x20, 0x00, 0x30, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
442ee09084fSZhu Ning 	{512, 44100, 22579200, 0xE0, 0x00, 0x00, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
443ee09084fSZhu Ning 	{512, 48000, 24576000, 0xE0, 0x00, 0x00, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
444ee09084fSZhu Ning 	{768, 8000, 6144000, 0x60, 0x02, 0x11, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
445ee09084fSZhu Ning 	{768, 16000, 12288000, 0x20, 0x02, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
446ee09084fSZhu Ning 	{768, 32000, 24576000, 0xE0, 0x02, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
447ee09084fSZhu Ning 	{800, 48000, 38400000, 0x00, 0x18, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
448ee09084fSZhu Ning 
449ee09084fSZhu Ning 	{1024, 8000, 8192000, 0x60, 0x00, 0x30, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
450ee09084fSZhu Ning 	{1024, 16000, 16384000, 0x20, 0x00, 0x00, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
451ee09084fSZhu Ning 	{1152, 16000, 18432000, 0x20, 0x08, 0x11, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
452ee09084fSZhu Ning 	{1536, 8000, 12288000, 0x60, 0x02, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
453ee09084fSZhu Ning 	{1536, 16000, 24576000, 0x20, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
454ee09084fSZhu Ning 	{1625, 8000, 13000000, 0x0C, 0x18, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
455ee09084fSZhu Ning 	{1625, 16000, 26000000, 0x0C, 0x18, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
456ee09084fSZhu Ning 	{2048, 8000, 16384000, 0x60, 0x00, 0x00, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
457ee09084fSZhu Ning 	{2304, 8000, 18432000, 0x40, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x5F},
458ee09084fSZhu Ning 	{3072, 8000, 24576000, 0x60, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
459ee09084fSZhu Ning 	{3250, 8000, 26000000, 0x0C, 0x18, 0x0F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
460ee09084fSZhu Ning };
461ee09084fSZhu Ning 
462ee09084fSZhu Ning static inline int get_coeff(int mclk, int rate, int array,
463ee09084fSZhu Ning 				const struct _coeff_div *coeff_div)
4645c439937SZhu Ning {
4655c439937SZhu Ning 	int i;
4665c439937SZhu Ning 
467ee09084fSZhu Ning 	for (i = 0; i < array; i++) {
4685c439937SZhu Ning 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
4695c439937SZhu Ning 			return i;
4705c439937SZhu Ning 	}
4715c439937SZhu Ning 
4725c439937SZhu Ning 	return -EINVAL;
4735c439937SZhu Ning }
4745c439937SZhu Ning 
4755c439937SZhu Ning static int es8326_set_dai_sysclk(struct snd_soc_dai *codec_dai,
4765c439937SZhu Ning 				 int clk_id, unsigned int freq, int dir)
4775c439937SZhu Ning {
4785c439937SZhu Ning 	struct snd_soc_component *codec = codec_dai->component;
4795c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
4805c439937SZhu Ning 
4815c439937SZhu Ning 	es8326->sysclk = freq;
4825c439937SZhu Ning 
4835c439937SZhu Ning 	return 0;
4845c439937SZhu Ning }
4855c439937SZhu Ning 
4865c439937SZhu Ning static int es8326_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
4875c439937SZhu Ning {
4885c439937SZhu Ning 	struct snd_soc_component *component = codec_dai->component;
4895c439937SZhu Ning 	u8 iface = 0;
4905c439937SZhu Ning 
4915c439937SZhu Ning 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
4925c439937SZhu Ning 	case SND_SOC_DAIFMT_CBC_CFP:
4935c439937SZhu Ning 		snd_soc_component_update_bits(component, ES8326_RESET,
4945c439937SZhu Ning 					      ES8326_MASTER_MODE_EN, ES8326_MASTER_MODE_EN);
4955c439937SZhu Ning 		break;
4965c439937SZhu Ning 	case SND_SOC_DAIFMT_CBC_CFC:
4975c439937SZhu Ning 		break;
4985c439937SZhu Ning 	default:
4995c439937SZhu Ning 		return -EINVAL;
5005c439937SZhu Ning 	}
5015c439937SZhu Ning 
5025c439937SZhu Ning 	/* interface format */
5035c439937SZhu Ning 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
5045c439937SZhu Ning 	case SND_SOC_DAIFMT_I2S:
5055c439937SZhu Ning 		break;
5065c439937SZhu Ning 	case SND_SOC_DAIFMT_RIGHT_J:
5075c439937SZhu Ning 		dev_err(component->dev, "Codec driver does not support right justified\n");
5085c439937SZhu Ning 		return -EINVAL;
5095c439937SZhu Ning 	case SND_SOC_DAIFMT_LEFT_J:
5105c439937SZhu Ning 		iface |= ES8326_DAIFMT_LEFT_J;
5115c439937SZhu Ning 		break;
5125c439937SZhu Ning 	case SND_SOC_DAIFMT_DSP_A:
5135c439937SZhu Ning 		iface |= ES8326_DAIFMT_DSP_A;
5145c439937SZhu Ning 		break;
5155c439937SZhu Ning 	case SND_SOC_DAIFMT_DSP_B:
5165c439937SZhu Ning 		iface |= ES8326_DAIFMT_DSP_B;
5175c439937SZhu Ning 		break;
5185c439937SZhu Ning 	default:
5195c439937SZhu Ning 		return -EINVAL;
5205c439937SZhu Ning 	}
5215c439937SZhu Ning 
5225c439937SZhu Ning 	snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DAIFMT_MASK, iface);
5235c439937SZhu Ning 
5245c439937SZhu Ning 	return 0;
5255c439937SZhu Ning }
5265c439937SZhu Ning 
5275c439937SZhu Ning static int es8326_pcm_hw_params(struct snd_pcm_substream *substream,
5285c439937SZhu Ning 				struct snd_pcm_hw_params *params,
5295c439937SZhu Ning 				struct snd_soc_dai *dai)
5305c439937SZhu Ning {
5315c439937SZhu Ning 	struct snd_soc_component *component = dai->component;
532ee09084fSZhu Ning 	const struct _coeff_div *coeff_div;
5335c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
5345c439937SZhu Ning 	u8 srate = 0;
535ee09084fSZhu Ning 	int coeff, array;
5365c439937SZhu Ning 
537ee09084fSZhu Ning 	if (es8326->version == 0) {
538ee09084fSZhu Ning 		coeff_div =  coeff_div_v0;
539ee09084fSZhu Ning 		array = ARRAY_SIZE(coeff_div_v0);
540ee09084fSZhu Ning 	} else {
541ee09084fSZhu Ning 		coeff_div =  coeff_div_v3;
542ee09084fSZhu Ning 		array = ARRAY_SIZE(coeff_div_v3);
543ee09084fSZhu Ning 	}
544ee09084fSZhu Ning 	coeff = get_coeff(es8326->sysclk, params_rate(params), array, coeff_div);
5455c439937SZhu Ning 	/* bit size */
5465c439937SZhu Ning 	switch (params_format(params)) {
5475c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S16_LE:
5485c439937SZhu Ning 		srate |= ES8326_S16_LE;
5495c439937SZhu Ning 		break;
5505c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S20_3LE:
5515c439937SZhu Ning 		srate |= ES8326_S20_3_LE;
5525c439937SZhu Ning 		break;
5535c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S18_3LE:
5545c439937SZhu Ning 		srate |= ES8326_S18_LE;
5555c439937SZhu Ning 		break;
5565c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S24_LE:
5575c439937SZhu Ning 		srate |= ES8326_S24_LE;
5585c439937SZhu Ning 		break;
5595c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S32_LE:
5605c439937SZhu Ning 		srate |= ES8326_S32_LE;
5615c439937SZhu Ning 		break;
5625c439937SZhu Ning 	default:
5635c439937SZhu Ning 		return -EINVAL;
5645c439937SZhu Ning 	}
5655c439937SZhu Ning 
5665c439937SZhu Ning 	/* set iface & srate */
5675c439937SZhu Ning 	snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DATA_LEN_MASK, srate);
5685c439937SZhu Ning 
5695c439937SZhu Ning 	if (coeff >= 0) {
5705c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DIV1,
5715c439937SZhu Ning 			     coeff_div[coeff].reg4);
5725c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DIV2,
5735c439937SZhu Ning 			     coeff_div[coeff].reg5);
5745c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DLL,
5755c439937SZhu Ning 			     coeff_div[coeff].reg6);
5765c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_MUX,
5775c439937SZhu Ning 			     coeff_div[coeff].reg7);
5785c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_ADC_SEL,
5795c439937SZhu Ning 			     coeff_div[coeff].reg8);
5805c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DAC_SEL,
5815c439937SZhu Ning 			     coeff_div[coeff].reg9);
5825c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_ADC_OSR,
5835c439937SZhu Ning 			     coeff_div[coeff].rega);
5845c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DAC_OSR,
5855c439937SZhu Ning 			     coeff_div[coeff].regb);
5865c439937SZhu Ning 	} else {
5875c439937SZhu Ning 		dev_warn(component->dev, "Clock coefficients do not match");
5885c439937SZhu Ning 	}
5895c439937SZhu Ning 
5905c439937SZhu Ning 	return 0;
5915c439937SZhu Ning }
5925c439937SZhu Ning 
593083912c2SZhu Ning static int es8326_mute(struct snd_soc_dai *dai, int mute, int direction)
594083912c2SZhu Ning {
595083912c2SZhu Ning 	struct snd_soc_component *component = dai->component;
596083912c2SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
5970663286eSZhu Ning 	unsigned int offset_l, offset_r;
598083912c2SZhu Ning 
599083912c2SZhu Ning 	if (mute) {
6008c99a0a6SZhu Ning 		if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
601083912c2SZhu Ning 			regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
602083912c2SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE,
603083912c2SZhu Ning 					ES8326_MUTE_MASK, ES8326_MUTE);
604a3aa9255SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF,
605a3aa9255SZhu Ning 					0x30, 0x00);
606083912c2SZhu Ning 		} else {
6078c99a0a6SZhu Ning 			regmap_update_bits(es8326->regmap,  ES8326_ADC_MUTE,
6088c99a0a6SZhu Ning 					0x0F, 0x0F);
6098c99a0a6SZhu Ning 		}
6108c99a0a6SZhu Ning 	} else {
611083912c2SZhu Ning 		if (!es8326->calibrated) {
612083912c2SZhu Ning 			regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_FORCE_CAL);
613083912c2SZhu Ning 			msleep(30);
6140663286eSZhu Ning 			regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
6150663286eSZhu Ning 			regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l);
6160663286eSZhu Ning 			regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r);
6170663286eSZhu Ning 			regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
6180663286eSZhu Ning 			regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l);
6190663286eSZhu Ning 			regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r);
620083912c2SZhu Ning 			es8326->calibrated = true;
621083912c2SZhu Ning 		}
6228c99a0a6SZhu Ning 		if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
623a3aa9255SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_DAC_DSM, 0x01, 0x01);
624a3aa9255SZhu Ning 			usleep_range(1000, 5000);
625a3aa9255SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_DAC_DSM, 0x01, 0x00);
626a3aa9255SZhu Ning 			usleep_range(1000, 5000);
627a3aa9255SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x30, 0x20);
628a3aa9255SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x30, 0x30);
629fc702b2cSZhu Ning 			regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa1);
630083912c2SZhu Ning 			regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_ON);
631083912c2SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE,
632083912c2SZhu Ning 					ES8326_MUTE_MASK, ~(ES8326_MUTE));
6338c99a0a6SZhu Ning 		} else {
6348c99a0a6SZhu Ning 			msleep(300);
6358c99a0a6SZhu Ning 			regmap_update_bits(es8326->regmap,  ES8326_ADC_MUTE,
6368c99a0a6SZhu Ning 					0x0F, 0x00);
6378c99a0a6SZhu Ning 		}
638083912c2SZhu Ning 	}
639083912c2SZhu Ning 	return 0;
640083912c2SZhu Ning }
641083912c2SZhu Ning 
6425c439937SZhu Ning static int es8326_set_bias_level(struct snd_soc_component *codec,
6435c439937SZhu Ning 				 enum snd_soc_bias_level level)
6445c439937SZhu Ning {
6455c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
6465c439937SZhu Ning 	int ret;
6475c439937SZhu Ning 
6485c439937SZhu Ning 	switch (level) {
6495c439937SZhu Ning 	case SND_SOC_BIAS_ON:
6505c439937SZhu Ning 		ret = clk_prepare_enable(es8326->mclk);
6515c439937SZhu Ning 		if (ret)
6525c439937SZhu Ning 			return ret;
6530663286eSZhu Ning 
654a3aa9255SZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_RESET, 0x02, 0x02);
655a3aa9255SZhu Ning 		usleep_range(5000, 10000);
6560663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_INTOUT_IO, es8326->interrupt_clk);
6575c439937SZhu Ning 		regmap_write(es8326->regmap, ES8326_SDINOUT1_IO,
6585c439937SZhu Ning 			    (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT));
6595c439937SZhu Ning 		regmap_write(es8326->regmap, ES8326_PGA_PDN, 0x40);
6600663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x00);
6610663286eSZhu Ning 		regmap_update_bits(es8326->regmap,  ES8326_CLK_CTL, 0x20, 0x20);
662a3aa9255SZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_RESET, 0x02, 0x00);
6635c439937SZhu Ning 		break;
6645c439937SZhu Ning 	case SND_SOC_BIAS_PREPARE:
6655c439937SZhu Ning 		break;
6665c439937SZhu Ning 	case SND_SOC_BIAS_STANDBY:
6670663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x3b);
6680663286eSZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_CLK_CTL, 0x20, 0x00);
6695c439937SZhu Ning 		regmap_write(es8326->regmap, ES8326_SDINOUT1_IO, ES8326_IO_INPUT);
6705c439937SZhu Ning 		break;
671fc702b2cSZhu Ning 	case SND_SOC_BIAS_OFF:
672fc702b2cSZhu Ning 		clk_disable_unprepare(es8326->mclk);
673fc702b2cSZhu Ning 		break;
6745c439937SZhu Ning 	}
6755c439937SZhu Ning 
6765c439937SZhu Ning 	return 0;
6775c439937SZhu Ning }
6785c439937SZhu Ning 
6795c439937SZhu Ning #define es8326_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
6805c439937SZhu Ning 	SNDRV_PCM_FMTBIT_S24_LE)
6815c439937SZhu Ning 
6825c439937SZhu Ning static const struct snd_soc_dai_ops es8326_ops = {
6835c439937SZhu Ning 	.hw_params = es8326_pcm_hw_params,
6845c439937SZhu Ning 	.set_fmt = es8326_set_dai_fmt,
6855c439937SZhu Ning 	.set_sysclk = es8326_set_dai_sysclk,
686083912c2SZhu Ning 	.mute_stream = es8326_mute,
6878c99a0a6SZhu Ning 	.no_capture_mute = 0,
6885c439937SZhu Ning };
6895c439937SZhu Ning 
6905c439937SZhu Ning static struct snd_soc_dai_driver es8326_dai = {
6915c439937SZhu Ning 	.name = "ES8326 HiFi",
6925c439937SZhu Ning 	.playback = {
6935c439937SZhu Ning 		.stream_name = "Playback",
6945c439937SZhu Ning 		.channels_min = 1,
6955c439937SZhu Ning 		.channels_max = 2,
6965c439937SZhu Ning 		.rates = SNDRV_PCM_RATE_8000_48000,
6975c439937SZhu Ning 		.formats = es8326_FORMATS,
6985c439937SZhu Ning 		},
6995c439937SZhu Ning 	.capture = {
7005c439937SZhu Ning 		.stream_name = "Capture",
7015c439937SZhu Ning 		.channels_min = 1,
7025c439937SZhu Ning 		.channels_max = 2,
7035c439937SZhu Ning 		.rates = SNDRV_PCM_RATE_8000_48000,
7045c439937SZhu Ning 		.formats = es8326_FORMATS,
7055c439937SZhu Ning 		},
7065c439937SZhu Ning 	.ops = &es8326_ops,
7075c439937SZhu Ning 	.symmetric_rate = 1,
7085c439937SZhu Ning };
7095c439937SZhu Ning 
7105c439937SZhu Ning static void es8326_enable_micbias(struct snd_soc_component *component)
7115c439937SZhu Ning {
7125c439937SZhu Ning 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
7135c439937SZhu Ning 
7145c439937SZhu Ning 	snd_soc_dapm_mutex_lock(dapm);
7155c439937SZhu Ning 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS1");
7165c439937SZhu Ning 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS2");
7175c439937SZhu Ning 	snd_soc_dapm_sync_unlocked(dapm);
7185c439937SZhu Ning 	snd_soc_dapm_mutex_unlock(dapm);
7195c439937SZhu Ning }
7205c439937SZhu Ning 
7215c439937SZhu Ning static void es8326_disable_micbias(struct snd_soc_component *component)
7225c439937SZhu Ning {
7235c439937SZhu Ning 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
7245c439937SZhu Ning 
7255c439937SZhu Ning 	snd_soc_dapm_mutex_lock(dapm);
7265c439937SZhu Ning 	snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS1");
7275c439937SZhu Ning 	snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS2");
7285c439937SZhu Ning 	snd_soc_dapm_sync_unlocked(dapm);
7295c439937SZhu Ning 	snd_soc_dapm_mutex_unlock(dapm);
7305c439937SZhu Ning }
7315c439937SZhu Ning 
7325c439937SZhu Ning /*
7335c439937SZhu Ning  *	For button detection, set the following in soundcard
7345c439937SZhu Ning  *	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
7355c439937SZhu Ning  *	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
7365c439937SZhu Ning  *	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
7375c439937SZhu Ning  */
7385c439937SZhu Ning static void es8326_jack_button_handler(struct work_struct *work)
7395c439937SZhu Ning {
7405c439937SZhu Ning 	struct es8326_priv *es8326 =
7415c439937SZhu Ning 		container_of(work, struct es8326_priv, button_press_work.work);
7425c439937SZhu Ning 	struct snd_soc_component *comp = es8326->component;
7435c439937SZhu Ning 	unsigned int iface;
7445c439937SZhu Ning 	static int button_to_report, press_count;
7455c439937SZhu Ning 	static int prev_button, cur_button;
7465c439937SZhu Ning 
7475c439937SZhu Ning 	if (!(es8326->jack->status & SND_JACK_HEADSET)) /* Jack unplugged */
7485c439937SZhu Ning 		return;
7495c439937SZhu Ning 
7505c439937SZhu Ning 	mutex_lock(&es8326->lock);
7514ddad00cSZhu Ning 	iface = snd_soc_component_read(comp, ES8326_HPDET_STA);
7525c439937SZhu Ning 	switch (iface) {
7535c439937SZhu Ning 	case 0x93:
7545c439937SZhu Ning 		/* pause button detected */
7555c439937SZhu Ning 		cur_button = SND_JACK_BTN_0;
7565c439937SZhu Ning 		break;
7575c439937SZhu Ning 	case 0x6f:
75804f96c93SZhu Ning 	case 0x4b:
7595c439937SZhu Ning 		/* button volume up */
7605c439937SZhu Ning 		cur_button = SND_JACK_BTN_1;
7615c439937SZhu Ning 		break;
7625c439937SZhu Ning 	case 0x27:
7635c439937SZhu Ning 		/* button volume down */
7645c439937SZhu Ning 		cur_button = SND_JACK_BTN_2;
7655c439937SZhu Ning 		break;
7665c439937SZhu Ning 	case 0x1e:
76704f96c93SZhu Ning 	case 0xe2:
7685c439937SZhu Ning 		/* button released or not pressed */
7695c439937SZhu Ning 		cur_button = 0;
7705c439937SZhu Ning 		break;
7715c439937SZhu Ning 	default:
7725c439937SZhu Ning 		break;
7735c439937SZhu Ning 	}
7745c439937SZhu Ning 
7755c439937SZhu Ning 	if ((prev_button == cur_button) && (cur_button != 0)) {
7765c439937SZhu Ning 		press_count++;
77704f96c93SZhu Ning 		if (press_count > 3) {
77804f96c93SZhu Ning 			/* report a press every 120ms */
7795c439937SZhu Ning 			snd_soc_jack_report(es8326->jack, cur_button,
7805c439937SZhu Ning 					SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
7815c439937SZhu Ning 			press_count = 0;
7825c439937SZhu Ning 		}
7835c439937SZhu Ning 		button_to_report = cur_button;
7845c439937SZhu Ning 		queue_delayed_work(system_wq, &es8326->button_press_work,
78504f96c93SZhu Ning 				   msecs_to_jiffies(35));
7865c439937SZhu Ning 	} else if (prev_button != cur_button) {
7875c439937SZhu Ning 		/* mismatch, detect again */
7885c439937SZhu Ning 		prev_button = cur_button;
7895c439937SZhu Ning 		queue_delayed_work(system_wq, &es8326->button_press_work,
79004f96c93SZhu Ning 				   msecs_to_jiffies(35));
7915c439937SZhu Ning 	} else {
7925c439937SZhu Ning 		/* released or no pressed */
7935c439937SZhu Ning 		if (button_to_report != 0) {
7945c439937SZhu Ning 			snd_soc_jack_report(es8326->jack, button_to_report,
7955c439937SZhu Ning 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
7965c439937SZhu Ning 			snd_soc_jack_report(es8326->jack, 0,
7975c439937SZhu Ning 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
7985c439937SZhu Ning 			button_to_report = 0;
7995c439937SZhu Ning 		}
8005c439937SZhu Ning 	}
8015c439937SZhu Ning 	mutex_unlock(&es8326->lock);
8025c439937SZhu Ning }
8035c439937SZhu Ning 
8045c439937SZhu Ning static void es8326_jack_detect_handler(struct work_struct *work)
8055c439937SZhu Ning {
8065c439937SZhu Ning 	struct es8326_priv *es8326 =
8075c439937SZhu Ning 		container_of(work, struct es8326_priv, jack_detect_work.work);
8085c439937SZhu Ning 	struct snd_soc_component *comp = es8326->component;
8095c439937SZhu Ning 	unsigned int iface;
8105c439937SZhu Ning 
8115c439937SZhu Ning 	mutex_lock(&es8326->lock);
8124ddad00cSZhu Ning 	iface = snd_soc_component_read(comp, ES8326_HPDET_STA);
8135c439937SZhu Ning 	dev_dbg(comp->dev, "gpio flag %#04x", iface);
81404f96c93SZhu Ning 
815ee09084fSZhu Ning 	if ((es8326->jack_remove_retry == 1) && (es8326->version != ES8326_VERSION_B)) {
81604f96c93SZhu Ning 		if (iface & ES8326_HPINSERT_FLAG)
81704f96c93SZhu Ning 			es8326->jack_remove_retry = 2;
81804f96c93SZhu Ning 		else
81904f96c93SZhu Ning 			es8326->jack_remove_retry = 0;
82004f96c93SZhu Ning 
82104f96c93SZhu Ning 		dev_dbg(comp->dev, "remove event check, set HPJACK_POL normal, cnt = %d\n",
82204f96c93SZhu Ning 				es8326->jack_remove_retry);
82304f96c93SZhu Ning 		/*
82404f96c93SZhu Ning 		 * Inverted HPJACK_POL bit to trigger one IRQ to double check HP Removal event
82504f96c93SZhu Ning 		 */
82604f96c93SZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE,
82704f96c93SZhu Ning 					ES8326_HP_DET_JACK_POL, (es8326->jd_inverted ?
82804f96c93SZhu Ning 					~es8326->jack_pol : es8326->jack_pol));
82904f96c93SZhu Ning 		goto exit;
83004f96c93SZhu Ning 	}
83104f96c93SZhu Ning 
8325c439937SZhu Ning 	if ((iface & ES8326_HPINSERT_FLAG) == 0) {
8335c439937SZhu Ning 		/* Jack unplugged or spurious IRQ */
83404f96c93SZhu Ning 		dev_dbg(comp->dev, "No headset detected\n");
83504f96c93SZhu Ning 		es8326_disable_micbias(es8326->component);
8365c439937SZhu Ning 		if (es8326->jack->status & SND_JACK_HEADPHONE) {
83704f96c93SZhu Ning 			dev_dbg(comp->dev, "Report hp remove event\n");
8385c439937SZhu Ning 			snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
83904f96c93SZhu Ning 			/* mute adc when mic path switch */
84004f96c93SZhu Ning 			regmap_write(es8326->regmap, ES8326_ADC_SCALE, 0x33);
84104f96c93SZhu Ning 			regmap_write(es8326->regmap, ES8326_ADC1_SRC, 0x44);
84204f96c93SZhu Ning 			regmap_write(es8326->regmap, ES8326_ADC2_SRC, 0x66);
84304f96c93SZhu Ning 			es8326->hp = 0;
84404f96c93SZhu Ning 		}
84504f96c93SZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x01);
84614a0a1ecSZhu Ning 		regmap_write(es8326->regmap, ES8326_SYS_BIAS, 0x0a);
84714a0a1ecSZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x0f, 0x03);
84804f96c93SZhu Ning 		/*
84904f96c93SZhu Ning 		 * Inverted HPJACK_POL bit to trigger one IRQ to double check HP Removal event
85004f96c93SZhu Ning 		 */
851ee09084fSZhu Ning 		if ((es8326->jack_remove_retry == 0) && (es8326->version != ES8326_VERSION_B)) {
85204f96c93SZhu Ning 			es8326->jack_remove_retry = 1;
85304f96c93SZhu Ning 			dev_dbg(comp->dev, "remove event check, invert HPJACK_POL, cnt = %d\n",
85404f96c93SZhu Ning 					es8326->jack_remove_retry);
85504f96c93SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE,
85604f96c93SZhu Ning 					ES8326_HP_DET_JACK_POL, (es8326->jd_inverted ?
85704f96c93SZhu Ning 					es8326->jack_pol : ~es8326->jack_pol));
85804f96c93SZhu Ning 
85904f96c93SZhu Ning 		} else {
86004f96c93SZhu Ning 			es8326->jack_remove_retry = 0;
8615c439937SZhu Ning 		}
8625c439937SZhu Ning 	} else if ((iface & ES8326_HPINSERT_FLAG) == ES8326_HPINSERT_FLAG) {
86304f96c93SZhu Ning 		es8326->jack_remove_retry = 0;
86404f96c93SZhu Ning 		if (es8326->hp == 0) {
86504f96c93SZhu Ning 			dev_dbg(comp->dev, "First insert, start OMTP/CTIA type check\n");
86604f96c93SZhu Ning 			/*
8678a81491aSZhu Ning 			 * set auto-check mode, then restart jack_detect_work after 400ms.
86804f96c93SZhu Ning 			 * Don't report jack status.
86904f96c93SZhu Ning 			 */
87004f96c93SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x01);
871a3aa9255SZhu Ning 			es8326_enable_micbias(es8326->component);
87204f96c93SZhu Ning 			usleep_range(50000, 70000);
87304f96c93SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x00);
87414a0a1ecSZhu Ning 			regmap_write(es8326->regmap, ES8326_SYS_BIAS, 0x1f);
87514a0a1ecSZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x0f, 0x08);
87604f96c93SZhu Ning 			queue_delayed_work(system_wq, &es8326->jack_detect_work,
8778a81491aSZhu Ning 					msecs_to_jiffies(400));
87804f96c93SZhu Ning 			es8326->hp = 1;
87904f96c93SZhu Ning 			goto exit;
88004f96c93SZhu Ning 		}
8815c439937SZhu Ning 		if (es8326->jack->status & SND_JACK_HEADSET) {
8825c439937SZhu Ning 			/* detect button */
88304f96c93SZhu Ning 			dev_dbg(comp->dev, "button pressed\n");
8845c439937SZhu Ning 			queue_delayed_work(system_wq, &es8326->button_press_work, 10);
88504f96c93SZhu Ning 			goto exit;
88604f96c93SZhu Ning 		}
88704f96c93SZhu Ning 		if ((iface & ES8326_HPBUTTON_FLAG) == 0x01) {
88804f96c93SZhu Ning 			dev_dbg(comp->dev, "Headphone detected\n");
8895c439937SZhu Ning 			snd_soc_jack_report(es8326->jack,
8905c439937SZhu Ning 					SND_JACK_HEADPHONE, SND_JACK_HEADSET);
89104f96c93SZhu Ning 		} else {
89204f96c93SZhu Ning 			dev_dbg(comp->dev, "Headset detected\n");
89304f96c93SZhu Ning 			snd_soc_jack_report(es8326->jack,
89404f96c93SZhu Ning 					SND_JACK_HEADSET, SND_JACK_HEADSET);
89504f96c93SZhu Ning 
89604f96c93SZhu Ning 			regmap_write(es8326->regmap, ES8326_ADC_SCALE, 0x33);
89704f96c93SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_PGA_PDN,
89804f96c93SZhu Ning 					0x08, 0x08);
89904f96c93SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_PGAGAIN,
90004f96c93SZhu Ning 					0x80, 0x80);
90104f96c93SZhu Ning 			regmap_write(es8326->regmap, ES8326_ADC1_SRC, 0x00);
90204f96c93SZhu Ning 			regmap_write(es8326->regmap, ES8326_ADC2_SRC, 0x00);
90304f96c93SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_PGA_PDN,
90404f96c93SZhu Ning 					0x08, 0x00);
90504f96c93SZhu Ning 			usleep_range(10000, 15000);
9065c439937SZhu Ning 		}
9075c439937SZhu Ning 	}
90804f96c93SZhu Ning exit:
9095c439937SZhu Ning 	mutex_unlock(&es8326->lock);
9105c439937SZhu Ning }
9115c439937SZhu Ning 
9125c439937SZhu Ning static irqreturn_t es8326_irq(int irq, void *dev_id)
9135c439937SZhu Ning {
9145c439937SZhu Ning 	struct es8326_priv *es8326 = dev_id;
9155c439937SZhu Ning 
9165c439937SZhu Ning 	if (!es8326->jack)
9175c439937SZhu Ning 		goto out;
9185c439937SZhu Ning 
9195c439937SZhu Ning 	if (es8326->jack->status & SND_JACK_HEADSET)
9205c439937SZhu Ning 		queue_delayed_work(system_wq, &es8326->jack_detect_work,
9215c439937SZhu Ning 				   msecs_to_jiffies(10));
9225c439937SZhu Ning 	else
9235c439937SZhu Ning 		queue_delayed_work(system_wq, &es8326->jack_detect_work,
9248a81491aSZhu Ning 				   msecs_to_jiffies(300));
9255c439937SZhu Ning 
9265c439937SZhu Ning out:
9275c439937SZhu Ning 	return IRQ_HANDLED;
9285c439937SZhu Ning }
9295c439937SZhu Ning 
9300663286eSZhu Ning static int es8326_calibrate(struct snd_soc_component *component)
9310663286eSZhu Ning {
9320663286eSZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
9330663286eSZhu Ning 	unsigned int reg;
9340663286eSZhu Ning 	unsigned int offset_l, offset_r;
9350663286eSZhu Ning 
9360663286eSZhu Ning 	regmap_read(es8326->regmap, ES8326_CHIP_VERSION, &reg);
9370663286eSZhu Ning 	es8326->version = reg;
9380663286eSZhu Ning 
9390663286eSZhu Ning 	if ((es8326->version == ES8326_VERSION_B) && (es8326->calibrated == false)) {
9400663286eSZhu Ning 		dev_dbg(component->dev, "ES8326_VERSION_B, calibrating\n");
9410663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_INV, 0xc0);
94214a0a1ecSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_DIV1, 0x03);
9430663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_DLL, 0x30);
9440663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_MUX, 0xed);
945fc702b2cSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_DAC_SEL, 0x08);
9460663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_TRI, 0xc1);
9470663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_DAC_MUTE, 0x03);
9480663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7f);
94914a0a1ecSZhu Ning 		regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x23);
9500663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_DAC2HPMIX, 0x88);
951fc702b2cSZhu Ning 		usleep_range(15000, 20000);
9520663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
953fc702b2cSZhu Ning 		usleep_range(15000, 20000);
9540663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_RESET, 0xc0);
9550663286eSZhu Ning 		usleep_range(15000, 20000);
9560663286eSZhu Ning 
9570663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, ES8326_HP_OFF);
9580663286eSZhu Ning 		regmap_read(es8326->regmap, ES8326_CSM_MUTE_STA, &reg);
9590663286eSZhu Ning 		if ((reg & 0xf0) != 0x40)
9600663286eSZhu Ning 			msleep(50);
9610663286eSZhu Ning 
9620663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_CAL, 0xd4);
9630663286eSZhu Ning 		msleep(200);
9640663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_CAL, 0x4d);
9650663286eSZhu Ning 		msleep(200);
9660663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
9670663286eSZhu Ning 		regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l);
9680663286eSZhu Ning 		regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r);
9690663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
9700663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l);
9710663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r);
9720663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00);
9730663286eSZhu Ning 
9740663286eSZhu Ning 		es8326->calibrated = true;
9750663286eSZhu Ning 	}
9760663286eSZhu Ning 
9770663286eSZhu Ning 	return 0;
9780663286eSZhu Ning }
9790663286eSZhu Ning 
9805c439937SZhu Ning static int es8326_resume(struct snd_soc_component *component)
9815c439937SZhu Ning {
9825c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
9835c439937SZhu Ning 
9845c439937SZhu Ning 	regcache_cache_only(es8326->regmap, false);
9855c439937SZhu Ning 	regcache_sync(es8326->regmap);
9865c439937SZhu Ning 
987ac20a73dSZhu Ning 	/* reset internal clock state */
988ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_RESET, 0x1f);
989ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x0E);
99014a0a1ecSZhu Ning 	regmap_write(es8326->regmap, ES8326_ANA_LP, 0xf0);
991ac20a73dSZhu Ning 	usleep_range(10000, 15000);
992fc702b2cSZhu Ning 	regmap_write(es8326->regmap, ES8326_HPJACK_TIMER, 0xe9);
99314a0a1ecSZhu Ning 	regmap_write(es8326->regmap, ES8326_ANA_MICBIAS, 0xcb);
994ac20a73dSZhu Ning 	/* set headphone default type and detect pin */
995fc702b2cSZhu Ning 	regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x83);
996ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_RESAMPLE, 0x05);
9975c439937SZhu Ning 
998ac20a73dSZhu Ning 	/* set internal oscillator as clock source of headpone cp */
999fc702b2cSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_DIV_CPC, 0x89);
1000ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_ON);
1001ac20a73dSZhu Ning 	/* clock manager reset release */
1002ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_RESET, 0x17);
1003ac20a73dSZhu Ning 	/* set headphone detection as half scan mode */
100414a0a1ecSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_MISC, 0x3d);
1005ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_PULLUP_CTL, 0x00);
1006ac20a73dSZhu Ning 
1007ac20a73dSZhu Ning 	/* enable headphone driver */
100814a0a1ecSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_VOL, 0xc4);
1009ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa7);
1010ac20a73dSZhu Ning 	usleep_range(2000, 5000);
101114a0a1ecSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0x23);
101214a0a1ecSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0x33);
1013ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa1);
1014ac20a73dSZhu Ning 
1015ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00);
1016ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_VMIDS1, 0xc4);
1017ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_VMIDS2, 0x81);
1018ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_CAL_TIME, 0x00);
10190663286eSZhu Ning 	/* calibrate for B version */
10200663286eSZhu Ning 	es8326_calibrate(component);
1021523d242dSZhu Ning 	regmap_write(es8326->regmap, ES8326_DAC_CROSSTALK, 0xaa);
1022523d242dSZhu Ning 	regmap_write(es8326->regmap, ES8326_DAC_RAMPRATE, 0x00);
1023ac20a73dSZhu Ning 	/* turn off headphone out */
1024ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_CAL, 0x00);
1025ac20a73dSZhu Ning 	/* set ADC and DAC in low power mode */
1026ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_ANA_LP, 0xf0);
1027ac20a73dSZhu Ning 
10285c439937SZhu Ning 	regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7F);
1029ac20a73dSZhu Ning 	/* select vdda as micbias source */
1030ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x23);
1031ac20a73dSZhu Ning 	/* set dac dsmclip = 1 */
1032ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_DAC_DSM, 0x08);
1033ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_DAC_VPPSCALE, 0x15);
1034ac20a73dSZhu Ning 
1035a3aa9255SZhu Ning 	regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x80 |
1036a3aa9255SZhu Ning 			((es8326->version == ES8326_VERSION_B) ?
1037a3aa9255SZhu Ning 			(ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol) :
1038a3aa9255SZhu Ning 			(ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol | 0x04)));
1039a3aa9255SZhu Ning 	usleep_range(5000, 10000);
1040a3aa9255SZhu Ning 	es8326_enable_micbias(es8326->component);
1041a3aa9255SZhu Ning 	usleep_range(50000, 70000);
1042a3aa9255SZhu Ning 	regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x00);
1043ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_INT_SOURCE,
1044ac20a73dSZhu Ning 		    (ES8326_INT_SRC_PIN9 | ES8326_INT_SRC_BUTTON));
1045ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_INTOUT_IO,
1046ac20a73dSZhu Ning 		     es8326->interrupt_clk);
1047ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_SDINOUT1_IO,
1048ac20a73dSZhu Ning 		    (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT));
1049ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_SDINOUT23_IO, ES8326_IO_INPUT);
1050ac20a73dSZhu Ning 
105114a0a1ecSZhu Ning 	regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x00);
1052ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_RESET, ES8326_CSM_ON);
1053ac20a73dSZhu Ning 	regmap_update_bits(es8326->regmap, ES8326_PGAGAIN, ES8326_MIC_SEL_MASK,
1054ac20a73dSZhu Ning 			   ES8326_MIC1_SEL);
1055ac20a73dSZhu Ning 
1056ac20a73dSZhu Ning 	regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE, ES8326_MUTE_MASK,
1057ac20a73dSZhu Ning 			   ES8326_MUTE);
1058ac20a73dSZhu Ning 
10598c99a0a6SZhu Ning 	regmap_write(es8326->regmap, ES8326_ADC_MUTE, 0x0f);
10605c439937SZhu Ning 
106104f96c93SZhu Ning 	es8326->jack_remove_retry = 0;
106204f96c93SZhu Ning 	es8326->hp = 0;
1063*966323ddSZhu Ning 	es8326->hpl_vol = 0x03;
1064*966323ddSZhu Ning 	es8326->hpr_vol = 0x03;
10655c439937SZhu Ning 	return 0;
10665c439937SZhu Ning }
10675c439937SZhu Ning 
10685c439937SZhu Ning static int es8326_suspend(struct snd_soc_component *component)
10695c439937SZhu Ning {
10705c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
10715c439937SZhu Ning 
10725c439937SZhu Ning 	cancel_delayed_work_sync(&es8326->jack_detect_work);
10735c439937SZhu Ning 	es8326_disable_micbias(component);
1074083912c2SZhu Ning 	es8326->calibrated = false;
10755c439937SZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_OFF);
10765c439937SZhu Ning 	regcache_cache_only(es8326->regmap, true);
10775c439937SZhu Ning 	regcache_mark_dirty(es8326->regmap);
10785c439937SZhu Ning 
1079ac20a73dSZhu Ning 	/* reset register value to default */
1080ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x01);
1081ac20a73dSZhu Ning 	usleep_range(1000, 3000);
1082ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x00);
10835c439937SZhu Ning 	return 0;
10845c439937SZhu Ning }
10855c439937SZhu Ning 
10865c439937SZhu Ning static int es8326_probe(struct snd_soc_component *component)
10875c439937SZhu Ning {
10885c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
10895c439937SZhu Ning 	int ret;
10905c439937SZhu Ning 
10915c439937SZhu Ning 	es8326->component = component;
10925c439937SZhu Ning 	es8326->jd_inverted = device_property_read_bool(component->dev,
10935c439937SZhu Ning 							"everest,jack-detect-inverted");
10945c439937SZhu Ning 
10955c439937SZhu Ning 	ret = device_property_read_u8(component->dev, "everest,mic1-src", &es8326->mic1_src);
10965c439937SZhu Ning 	if (ret != 0) {
10975c439937SZhu Ning 		dev_dbg(component->dev, "mic1-src return %d", ret);
10985c439937SZhu Ning 		es8326->mic1_src = ES8326_ADC_AMIC;
10995c439937SZhu Ning 	}
11005c439937SZhu Ning 	dev_dbg(component->dev, "mic1-src %x", es8326->mic1_src);
11015c439937SZhu Ning 
11025c439937SZhu Ning 	ret = device_property_read_u8(component->dev, "everest,mic2-src", &es8326->mic2_src);
11035c439937SZhu Ning 	if (ret != 0) {
11045c439937SZhu Ning 		dev_dbg(component->dev, "mic2-src return %d", ret);
11055c439937SZhu Ning 		es8326->mic2_src = ES8326_ADC_DMIC;
11065c439937SZhu Ning 	}
11075c439937SZhu Ning 	dev_dbg(component->dev, "mic2-src %x", es8326->mic2_src);
11085c439937SZhu Ning 
11095c439937SZhu Ning 	ret = device_property_read_u8(component->dev, "everest,jack-pol", &es8326->jack_pol);
11105c439937SZhu Ning 	if (ret != 0) {
11115c439937SZhu Ning 		dev_dbg(component->dev, "jack-pol return %d", ret);
1112ac20a73dSZhu Ning 		es8326->jack_pol = ES8326_HP_TYPE_AUTO;
11135c439937SZhu Ning 	}
11145c439937SZhu Ning 	dev_dbg(component->dev, "jack-pol %x", es8326->jack_pol);
11155c439937SZhu Ning 
1116fe1e7e8cSAlexey Firago 	ret = device_property_read_u8(component->dev, "everest,interrupt-src",
1117fe1e7e8cSAlexey Firago 				      &es8326->interrupt_src);
11185c439937SZhu Ning 	if (ret != 0) {
11195c439937SZhu Ning 		dev_dbg(component->dev, "interrupt-src return %d", ret);
11205c439937SZhu Ning 		es8326->interrupt_src = ES8326_HP_DET_SRC_PIN9;
11215c439937SZhu Ning 	}
11225c439937SZhu Ning 	dev_dbg(component->dev, "interrupt-src %x", es8326->interrupt_src);
11235c439937SZhu Ning 
1124fe1e7e8cSAlexey Firago 	ret = device_property_read_u8(component->dev, "everest,interrupt-clk",
1125fe1e7e8cSAlexey Firago 				      &es8326->interrupt_clk);
11265c439937SZhu Ning 	if (ret != 0) {
11275c439937SZhu Ning 		dev_dbg(component->dev, "interrupt-clk return %d", ret);
11285c439937SZhu Ning 		es8326->interrupt_clk = 0x45;
11295c439937SZhu Ning 	}
11305c439937SZhu Ning 	dev_dbg(component->dev, "interrupt-clk %x", es8326->interrupt_clk);
11315c439937SZhu Ning 
11325c439937SZhu Ning 	es8326_resume(component);
11335c439937SZhu Ning 	return 0;
11345c439937SZhu Ning }
11355c439937SZhu Ning 
11365c439937SZhu Ning static void es8326_enable_jack_detect(struct snd_soc_component *component,
11375c439937SZhu Ning 				struct snd_soc_jack *jack)
11385c439937SZhu Ning {
11395c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
11405c439937SZhu Ning 
11415c439937SZhu Ning 	mutex_lock(&es8326->lock);
11425c439937SZhu Ning 	if (es8326->jd_inverted)
11434ddad00cSZhu Ning 		snd_soc_component_update_bits(component, ES8326_HPDET_TYPE,
11445c439937SZhu Ning 					      ES8326_HP_DET_JACK_POL, ~es8326->jack_pol);
11455c439937SZhu Ning 	es8326->jack = jack;
11465c439937SZhu Ning 
11475c439937SZhu Ning 	mutex_unlock(&es8326->lock);
11485c439937SZhu Ning 	es8326_irq(es8326->irq, es8326);
11495c439937SZhu Ning }
11505c439937SZhu Ning 
11515c439937SZhu Ning static void es8326_disable_jack_detect(struct snd_soc_component *component)
11525c439937SZhu Ning {
11535c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
11545c439937SZhu Ning 
11555c439937SZhu Ning 	dev_dbg(component->dev, "Enter into %s\n", __func__);
11565c439937SZhu Ning 	if (!es8326->jack)
11575c439937SZhu Ning 		return; /* Already disabled (or never enabled) */
11585c439937SZhu Ning 	cancel_delayed_work_sync(&es8326->jack_detect_work);
11595c439937SZhu Ning 
11605c439937SZhu Ning 	mutex_lock(&es8326->lock);
11615c439937SZhu Ning 	if (es8326->jack->status & SND_JACK_MICROPHONE) {
11625c439937SZhu Ning 		es8326_disable_micbias(component);
11635c439937SZhu Ning 		snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
11645c439937SZhu Ning 	}
11655c439937SZhu Ning 	es8326->jack = NULL;
11665c439937SZhu Ning 	mutex_unlock(&es8326->lock);
11675c439937SZhu Ning }
11685c439937SZhu Ning 
11695c439937SZhu Ning static int es8326_set_jack(struct snd_soc_component *component,
11705c439937SZhu Ning 			struct snd_soc_jack *jack, void *data)
11715c439937SZhu Ning {
11725c439937SZhu Ning 	if (jack)
11735c439937SZhu Ning 		es8326_enable_jack_detect(component, jack);
11745c439937SZhu Ning 	else
11755c439937SZhu Ning 		es8326_disable_jack_detect(component);
11765c439937SZhu Ning 
11775c439937SZhu Ning 	return 0;
11785c439937SZhu Ning }
11795c439937SZhu Ning 
11805c439937SZhu Ning static void es8326_remove(struct snd_soc_component *component)
11815c439937SZhu Ning {
11825c439937SZhu Ning 	es8326_disable_jack_detect(component);
11835c439937SZhu Ning 	es8326_set_bias_level(component, SND_SOC_BIAS_OFF);
11845c439937SZhu Ning }
11855c439937SZhu Ning 
11865c439937SZhu Ning static const struct snd_soc_component_driver soc_component_dev_es8326 = {
11875c439937SZhu Ning 	.probe		= es8326_probe,
11885c439937SZhu Ning 	.remove		= es8326_remove,
11895c439937SZhu Ning 	.resume		= es8326_resume,
11905c439937SZhu Ning 	.suspend	= es8326_suspend,
11915c439937SZhu Ning 	.set_bias_level = es8326_set_bias_level,
11925c439937SZhu Ning 	.set_jack	= es8326_set_jack,
11935c439937SZhu Ning 	.dapm_widgets	= es8326_dapm_widgets,
11945c439937SZhu Ning 	.num_dapm_widgets	= ARRAY_SIZE(es8326_dapm_widgets),
11955c439937SZhu Ning 	.dapm_routes		= es8326_dapm_routes,
11965c439937SZhu Ning 	.num_dapm_routes	= ARRAY_SIZE(es8326_dapm_routes),
11975c439937SZhu Ning 	.controls		= es8326_snd_controls,
11985c439937SZhu Ning 	.num_controls		= ARRAY_SIZE(es8326_snd_controls),
11995c439937SZhu Ning 	.use_pmdown_time	= 1,
12005c439937SZhu Ning 	.endianness		= 1,
12015c439937SZhu Ning };
12025c439937SZhu Ning 
1203784252baSUwe Kleine-König static int es8326_i2c_probe(struct i2c_client *i2c)
12045c439937SZhu Ning {
12055c439937SZhu Ning 	struct es8326_priv *es8326;
12065c439937SZhu Ning 	int ret;
12075c439937SZhu Ning 
12085c439937SZhu Ning 	es8326 = devm_kzalloc(&i2c->dev, sizeof(struct es8326_priv), GFP_KERNEL);
12095c439937SZhu Ning 	if (!es8326)
12105c439937SZhu Ning 		return -ENOMEM;
12115c439937SZhu Ning 
12125c439937SZhu Ning 	i2c_set_clientdata(i2c, es8326);
12135c439937SZhu Ning 	es8326->i2c = i2c;
12145c439937SZhu Ning 	mutex_init(&es8326->lock);
12155c439937SZhu Ning 	es8326->regmap = devm_regmap_init_i2c(i2c, &es8326_regmap_config);
12165c439937SZhu Ning 	if (IS_ERR(es8326->regmap)) {
12175c439937SZhu Ning 		ret = PTR_ERR(es8326->regmap);
12185c439937SZhu Ning 		dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
12195c439937SZhu Ning 		return ret;
12205c439937SZhu Ning 	}
12215c439937SZhu Ning 
12225c439937SZhu Ning 	es8326->irq = i2c->irq;
12235c439937SZhu Ning 	INIT_DELAYED_WORK(&es8326->jack_detect_work,
12245c439937SZhu Ning 			  es8326_jack_detect_handler);
12255c439937SZhu Ning 	INIT_DELAYED_WORK(&es8326->button_press_work,
12265c439937SZhu Ning 			  es8326_jack_button_handler);
12275c439937SZhu Ning 	/* ES8316 is level-based while ES8326 is edge-based */
12285c439937SZhu Ning 	ret = devm_request_threaded_irq(&i2c->dev, es8326->irq, NULL, es8326_irq,
12295c439937SZhu Ning 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
12305c439937SZhu Ning 					"es8326", es8326);
12315c439937SZhu Ning 	if (ret) {
12325c439937SZhu Ning 		dev_warn(&i2c->dev, "Failed to request IRQ: %d: %d\n",
12335c439937SZhu Ning 		es8326->irq, ret);
12345c439937SZhu Ning 		es8326->irq = -ENXIO;
12355c439937SZhu Ning 	}
12365c439937SZhu Ning 
12375c439937SZhu Ning 	es8326->mclk = devm_clk_get_optional(&i2c->dev, "mclk");
12385c439937SZhu Ning 	if (IS_ERR(es8326->mclk)) {
12395c439937SZhu Ning 		dev_err(&i2c->dev, "unable to get mclk\n");
12405c439937SZhu Ning 		return PTR_ERR(es8326->mclk);
12415c439937SZhu Ning 	}
12425c439937SZhu Ning 	if (!es8326->mclk)
12435c439937SZhu Ning 		dev_warn(&i2c->dev, "assuming static mclk\n");
12445c439937SZhu Ning 
12455c439937SZhu Ning 	ret = clk_prepare_enable(es8326->mclk);
12465c439937SZhu Ning 	if (ret) {
12475c439937SZhu Ning 		dev_err(&i2c->dev, "unable to enable mclk\n");
12485c439937SZhu Ning 		return ret;
12495c439937SZhu Ning 	}
12505c439937SZhu Ning 	return devm_snd_soc_register_component(&i2c->dev,
12515c439937SZhu Ning 					&soc_component_dev_es8326,
12525c439937SZhu Ning 					&es8326_dai, 1);
12535c439937SZhu Ning }
12545c439937SZhu Ning 
12555c439937SZhu Ning static const struct i2c_device_id es8326_i2c_id[] = {
12565c439937SZhu Ning 	{"es8326", 0 },
12575c439937SZhu Ning 	{}
12585c439937SZhu Ning };
12595c439937SZhu Ning MODULE_DEVICE_TABLE(i2c, es8326_i2c_id);
12605c439937SZhu Ning 
12615c439937SZhu Ning #ifdef CONFIG_OF
12625c439937SZhu Ning static const struct of_device_id es8326_of_match[] = {
12635c439937SZhu Ning 	{ .compatible = "everest,es8326", },
12645c439937SZhu Ning 	{}
12655c439937SZhu Ning };
12665c439937SZhu Ning MODULE_DEVICE_TABLE(of, es8326_of_match);
12675c439937SZhu Ning #endif
12685c439937SZhu Ning 
12695c439937SZhu Ning #ifdef CONFIG_ACPI
12705c439937SZhu Ning static const struct acpi_device_id es8326_acpi_match[] = {
12715c439937SZhu Ning 	{"ESSX8326", 0},
12725c439937SZhu Ning 	{},
12735c439937SZhu Ning };
12745c439937SZhu Ning MODULE_DEVICE_TABLE(acpi, es8326_acpi_match);
12755c439937SZhu Ning #endif
12765c439937SZhu Ning 
12775c439937SZhu Ning static struct i2c_driver es8326_i2c_driver = {
12785c439937SZhu Ning 	.driver = {
12795c439937SZhu Ning 		.name = "es8326",
12805c439937SZhu Ning 		.acpi_match_table = ACPI_PTR(es8326_acpi_match),
12815c439937SZhu Ning 		.of_match_table = of_match_ptr(es8326_of_match),
12825c439937SZhu Ning 	},
12839abcd240SUwe Kleine-König 	.probe = es8326_i2c_probe,
12845c439937SZhu Ning 	.id_table = es8326_i2c_id,
12855c439937SZhu Ning };
12865c439937SZhu Ning module_i2c_driver(es8326_i2c_driver);
12875c439937SZhu Ning 
12885c439937SZhu Ning MODULE_DESCRIPTION("ASoC es8326 driver");
12895c439937SZhu Ning MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
12905c439937SZhu Ning MODULE_LICENSE("GPL");
1291