xref: /linux/sound/soc/codecs/es8326.c (revision ee09084fbf9fdda6bf0179bcdca52196d0cde8a1)
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;
395c439937SZhu Ning 	bool jd_inverted;
405c439937SZhu Ning 	unsigned int sysclk;
41083912c2SZhu Ning 
42083912c2SZhu Ning 	bool calibrated;
43083912c2SZhu Ning 	int version;
4404f96c93SZhu Ning 	int hp;
4504f96c93SZhu Ning 	int jack_remove_retry;
465c439937SZhu Ning };
475c439937SZhu Ning 
485c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9550, 50, 0);
495c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9550, 50, 0);
505c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_analog_pga_tlv, 0, 300, 0);
515c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_pga_tlv, 0, 600, 0);
525c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(softramp_rate, 0, 100, 0);
535c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_target_tlv, -3200, 200, 0);
545c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_recovery_tlv, -125, 250, 0);
555c439937SZhu Ning 
565c439937SZhu Ning static const char *const winsize[] = {
575c439937SZhu Ning 	"0.25db/2  LRCK",
585c439937SZhu Ning 	"0.25db/4  LRCK",
595c439937SZhu Ning 	"0.25db/8  LRCK",
605c439937SZhu Ning 	"0.25db/16  LRCK",
615c439937SZhu Ning 	"0.25db/32  LRCK",
625c439937SZhu Ning 	"0.25db/64  LRCK",
635c439937SZhu Ning 	"0.25db/128  LRCK",
645c439937SZhu Ning 	"0.25db/256  LRCK",
655c439937SZhu Ning 	"0.25db/512  LRCK",
665c439937SZhu Ning 	"0.25db/1024  LRCK",
675c439937SZhu Ning 	"0.25db/2048  LRCK",
685c439937SZhu Ning 	"0.25db/4096  LRCK",
695c439937SZhu Ning 	"0.25db/8192  LRCK",
705c439937SZhu Ning 	"0.25db/16384  LRCK",
715c439937SZhu Ning 	"0.25db/32768  LRCK",
725c439937SZhu Ning 	"0.25db/65536  LRCK",
735c439937SZhu Ning };
745c439937SZhu Ning 
755c439937SZhu Ning static const char *const dacpol_txt[] =	{
765c439937SZhu Ning 	"Normal", "R Invert", "L Invert", "L + R Invert" };
775c439937SZhu Ning 
785c439937SZhu Ning static const struct soc_enum dacpol =
795c439937SZhu Ning 	SOC_ENUM_SINGLE(ES8326_DAC_DSM, 4, 4, dacpol_txt);
805c439937SZhu Ning static const struct soc_enum alc_winsize =
815c439937SZhu Ning 	SOC_ENUM_SINGLE(ES8326_ADC_RAMPRATE, 4, 16, winsize);
825c439937SZhu Ning static const struct soc_enum drc_winsize =
835c439937SZhu Ning 	SOC_ENUM_SINGLE(ES8326_DRC_WINSIZE, 4, 16, winsize);
845c439937SZhu Ning 
855c439937SZhu Ning static const struct snd_kcontrol_new es8326_snd_controls[] = {
865c439937SZhu Ning 	SOC_SINGLE_TLV("DAC Playback Volume", ES8326_DAC_VOL, 0, 0xbf, 0, dac_vol_tlv),
875c439937SZhu Ning 	SOC_ENUM("Playback Polarity", dacpol),
885c439937SZhu Ning 	SOC_SINGLE_TLV("DAC Ramp Rate", ES8326_DAC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
895c439937SZhu Ning 	SOC_SINGLE_TLV("DRC Recovery Level", ES8326_DRC_RECOVERY, 0, 4, 0, drc_recovery_tlv),
905c439937SZhu Ning 	SOC_ENUM("DRC Winsize", drc_winsize),
915c439937SZhu Ning 	SOC_SINGLE_TLV("DRC Target Level", ES8326_DRC_WINSIZE, 0, 0x0f, 0, drc_target_tlv),
925c439937SZhu Ning 
935c439937SZhu Ning 	SOC_DOUBLE_R_TLV("ADC Capture Volume", ES8326_ADC1_VOL, ES8326_ADC2_VOL, 0, 0xff, 0,
945c439937SZhu Ning 			 adc_vol_tlv),
955c439937SZhu Ning 	SOC_DOUBLE_TLV("ADC PGA Volume", ES8326_ADC_SCALE, 4, 0, 5, 0, adc_pga_tlv),
965c439937SZhu Ning 	SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8326_PGAGAIN, 0, 10, 0, adc_analog_pga_tlv),
975c439937SZhu Ning 	SOC_SINGLE_TLV("ADC Ramp Rate", ES8326_ADC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
985c439937SZhu Ning 	SOC_SINGLE("ALC Capture Switch", ES8326_ALC_RECOVERY, 3, 1, 0),
995c439937SZhu Ning 	SOC_SINGLE_TLV("ALC Capture Recovery Level", ES8326_ALC_LEVEL,
1005c439937SZhu Ning 			0, 4, 0, drc_recovery_tlv),
1015c439937SZhu Ning 	SOC_ENUM("ALC Capture Winsize", alc_winsize),
1025c439937SZhu Ning 	SOC_SINGLE_TLV("ALC Capture Target Level", ES8326_ALC_LEVEL,
1035c439937SZhu Ning 			0, 0x0f, 0, drc_target_tlv),
1045c439937SZhu Ning 
1055c439937SZhu Ning };
1065c439937SZhu Ning 
1075c439937SZhu Ning static const struct snd_soc_dapm_widget es8326_dapm_widgets[] = {
1085c439937SZhu Ning 	SND_SOC_DAPM_INPUT("MIC1"),
1095c439937SZhu Ning 	SND_SOC_DAPM_INPUT("MIC2"),
1105c439937SZhu Ning 	SND_SOC_DAPM_INPUT("MIC3"),
1115c439937SZhu Ning 	SND_SOC_DAPM_INPUT("MIC4"),
1125c439937SZhu Ning 
1135c439937SZhu Ning 	SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
1145c439937SZhu Ning 	SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
1155c439937SZhu Ning 
1165c439937SZhu Ning 	/* Digital Interface */
1175c439937SZhu Ning 	SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 0, SND_SOC_NOPM, 0, 0),
1185c439937SZhu Ning 	SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0, SND_SOC_NOPM, 0, 0),
1195c439937SZhu Ning 
1205c439937SZhu Ning 	/* ADC Digital Mute */
1215c439937SZhu Ning 	SND_SOC_DAPM_PGA("ADC L1", ES8326_ADC_MUTE, 0, 1, NULL, 0),
1225c439937SZhu Ning 	SND_SOC_DAPM_PGA("ADC R1", ES8326_ADC_MUTE, 1, 1, NULL, 0),
1235c439937SZhu Ning 	SND_SOC_DAPM_PGA("ADC L2", ES8326_ADC_MUTE, 2, 1, NULL, 0),
1245c439937SZhu Ning 	SND_SOC_DAPM_PGA("ADC R2", ES8326_ADC_MUTE, 3, 1, NULL, 0),
1255c439937SZhu Ning 
1265c439937SZhu Ning 	/* Analog Power Supply*/
1275c439937SZhu Ning 	SND_SOC_DAPM_DAC("Right DAC", NULL, ES8326_ANA_PDN, 0, 1),
1285c439937SZhu Ning 	SND_SOC_DAPM_DAC("Left DAC", NULL, ES8326_ANA_PDN, 1, 1),
1295c439937SZhu Ning 	SND_SOC_DAPM_SUPPLY("MICBIAS1", ES8326_ANA_MICBIAS, 2, 0, NULL, 0),
1305c439937SZhu Ning 	SND_SOC_DAPM_SUPPLY("MICBIAS2", ES8326_ANA_MICBIAS, 3, 0, NULL, 0),
1315c439937SZhu Ning 
1325c439937SZhu Ning 	SND_SOC_DAPM_PGA("LHPMIX", ES8326_DAC2HPMIX, 7, 0, NULL, 0),
1335c439937SZhu Ning 	SND_SOC_DAPM_PGA("RHPMIX", ES8326_DAC2HPMIX, 3, 0, NULL, 0),
1345c439937SZhu Ning 
1355c439937SZhu Ning 	SND_SOC_DAPM_OUTPUT("HPOL"),
1365c439937SZhu Ning 	SND_SOC_DAPM_OUTPUT("HPOR"),
1375c439937SZhu Ning };
1385c439937SZhu Ning 
1395c439937SZhu Ning static const struct snd_soc_dapm_route es8326_dapm_routes[] = {
1405c439937SZhu Ning 	{"ADC L1", NULL, "MIC1"},
1415c439937SZhu Ning 	{"ADC R1", NULL, "MIC2"},
1425c439937SZhu Ning 	{"ADC L2", NULL, "MIC3"},
1435c439937SZhu Ning 	{"ADC R2", NULL, "MIC4"},
1445c439937SZhu Ning 
1455c439937SZhu Ning 	{"ADC L", NULL, "ADC L1"},
1465c439937SZhu Ning 	{"ADC R", NULL, "ADC R1"},
1475c439937SZhu Ning 	{"ADC L", NULL, "ADC L2"},
1485c439937SZhu Ning 	{"ADC R", NULL, "ADC R2"},
1495c439937SZhu Ning 
1505c439937SZhu Ning 	{"I2S OUT", NULL, "ADC L"},
1515c439937SZhu Ning 	{"I2S OUT", NULL, "ADC R"},
1525c439937SZhu Ning 
1535c439937SZhu Ning 	{"Right DAC", NULL, "I2S IN"},
1545c439937SZhu Ning 	{"Left DAC", NULL, "I2S IN"},
1555c439937SZhu Ning 
1565c439937SZhu Ning 	{"LHPMIX", NULL, "Left DAC"},
1575c439937SZhu Ning 	{"RHPMIX", NULL, "Right DAC"},
1585c439937SZhu Ning 
1595c439937SZhu Ning 	{"HPOL", NULL, "LHPMIX"},
1605c439937SZhu Ning 	{"HPOR", NULL, "RHPMIX"},
1615c439937SZhu Ning };
1625c439937SZhu Ning 
163f1230a27SZhu Ning static bool es8326_volatile_register(struct device *dev, unsigned int reg)
164f1230a27SZhu Ning {
165f1230a27SZhu Ning 	switch (reg) {
166f1230a27SZhu Ning 	case ES8326_HPL_OFFSET_INI:
167f1230a27SZhu Ning 	case ES8326_HPR_OFFSET_INI:
168f1230a27SZhu Ning 	case ES8326_HPDET_STA:
169f1230a27SZhu Ning 	case ES8326_CTIA_OMTP_STA:
170f1230a27SZhu Ning 	case ES8326_CSM_MUTE_STA:
171f1230a27SZhu Ning 		return true;
172f1230a27SZhu Ning 	default:
173f1230a27SZhu Ning 		return false;
174f1230a27SZhu Ning 	}
175f1230a27SZhu Ning }
1765c439937SZhu Ning 
1775c69f11cSYang Yingliang static const struct regmap_config es8326_regmap_config = {
1785c439937SZhu Ning 	.reg_bits = 8,
1795c439937SZhu Ning 	.val_bits = 8,
1805c439937SZhu Ning 	.max_register = 0xff,
181f1230a27SZhu Ning 	.volatile_reg = es8326_volatile_register,
1825c439937SZhu Ning 	.cache_type = REGCACHE_RBTREE,
1835c439937SZhu Ning };
1845c439937SZhu Ning 
1855c439937SZhu Ning struct _coeff_div {
1865c439937SZhu Ning 	u16 fs;
1875c439937SZhu Ning 	u32 rate;
1885c439937SZhu Ning 	u32 mclk;
1895c439937SZhu Ning 	u8 reg4;
1905c439937SZhu Ning 	u8 reg5;
1915c439937SZhu Ning 	u8 reg6;
1925c439937SZhu Ning 	u8 reg7;
1935c439937SZhu Ning 	u8 reg8;
1945c439937SZhu Ning 	u8 reg9;
1955c439937SZhu Ning 	u8 rega;
1965c439937SZhu Ning 	u8 regb;
1975c439937SZhu Ning };
1985c439937SZhu Ning 
1995c439937SZhu Ning /* codec hifi mclk clock divider coefficients */
2005c439937SZhu Ning /* {ratio, LRCK, MCLK, REG04, REG05, REG06, REG07, REG08, REG09, REG10, REG11} */
201*ee09084fSZhu Ning static const struct _coeff_div coeff_div_v0[] = {
202*ee09084fSZhu Ning 	{64, 8000, 512000, 0x60, 0x01, 0x0F, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
203*ee09084fSZhu Ning 	{64, 16000, 1024000, 0x20, 0x00, 0x33, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
204*ee09084fSZhu Ning 	{64, 44100, 2822400, 0xE0, 0x00, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
205*ee09084fSZhu Ning 	{64, 48000, 3072000, 0xE0, 0x00, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
206*ee09084fSZhu Ning 	{128, 8000, 1024000, 0x60, 0x00, 0x33, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
207*ee09084fSZhu Ning 	{128, 16000, 2048000, 0x20, 0x00, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
208*ee09084fSZhu Ning 	{128, 44100, 5644800, 0xE0, 0x01, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
209*ee09084fSZhu Ning 	{128, 48000, 6144000, 0xE0, 0x01, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
2105c439937SZhu Ning 
211*ee09084fSZhu Ning 	{192, 32000, 6144000, 0xE0, 0x02, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
212*ee09084fSZhu Ning 	{256, 8000, 2048000, 0x60, 0x00, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
213*ee09084fSZhu Ning 	{256, 16000, 4096000, 0x20, 0x01, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
214*ee09084fSZhu Ning 	{256, 44100, 11289600, 0xE0, 0x00, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
215*ee09084fSZhu Ning 	{256, 48000, 12288000, 0xE0, 0x00, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
216*ee09084fSZhu Ning 	{384, 32000, 12288000, 0xE0, 0x05, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
217*ee09084fSZhu Ning 	{400, 48000, 19200000, 0xE9, 0x04, 0x0F, 0x6d, 0x4A, 0x0A, 0x1F, 0x1F},
2185c439937SZhu Ning 
219*ee09084fSZhu Ning 	{500, 48000, 24000000, 0xF8, 0x04, 0x3F, 0x6D, 0x4A, 0x0A, 0x1F, 0x1F},
220*ee09084fSZhu Ning 	{512, 8000, 4096000, 0x60, 0x01, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
221*ee09084fSZhu Ning 	{512, 16000, 8192000, 0x20, 0x00, 0x30, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
222*ee09084fSZhu Ning 	{512, 44100, 22579200, 0xE0, 0x00, 0x00, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
223*ee09084fSZhu Ning 	{512, 48000, 24576000, 0xE0, 0x00, 0x00, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
224*ee09084fSZhu Ning 	{768, 32000, 24576000, 0xE0, 0x02, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F},
225*ee09084fSZhu Ning 	{1024, 8000, 8192000, 0x60, 0x00, 0x30, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
2265c439937SZhu Ning 	{1024, 16000, 16384000, 0x20, 0x00, 0x00, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
2275c439937SZhu Ning };
2285c439937SZhu Ning 
229*ee09084fSZhu Ning static const struct _coeff_div coeff_div_v3[] = {
230*ee09084fSZhu Ning 	{32, 8000, 256000, 0x60, 0x00, 0x0F, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
231*ee09084fSZhu Ning 	{32, 16000, 512000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
232*ee09084fSZhu Ning 	{32, 44100, 1411200, 0x00, 0x00, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
233*ee09084fSZhu Ning 	{32, 48000, 1536000, 0x00, 0x00, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
234*ee09084fSZhu Ning 	{36, 8000, 288000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x23, 0x47},
235*ee09084fSZhu Ning 	{36, 16000, 576000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x23, 0x47},
236*ee09084fSZhu Ning 	{48, 8000, 384000, 0x60, 0x02, 0x1F, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
237*ee09084fSZhu Ning 	{48, 16000, 768000, 0x20, 0x02, 0x0F, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
238*ee09084fSZhu Ning 	{48, 48000, 2304000, 0x00, 0x02, 0x0D, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
239*ee09084fSZhu Ning 
240*ee09084fSZhu Ning 	{64, 8000, 512000, 0x60, 0x00, 0x35, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
241*ee09084fSZhu Ning 	{64, 16000, 1024000, 0x20, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
242*ee09084fSZhu Ning 	{64, 44100, 2822400, 0xE0, 0x00, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
243*ee09084fSZhu Ning 	{64, 48000, 3072000, 0xE0, 0x00, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
244*ee09084fSZhu Ning 	{72, 8000, 576000, 0x20, 0x00, 0x13, 0x35, 0x8A, 0x1B, 0x23, 0x47},
245*ee09084fSZhu Ning 	{72, 16000, 1152000, 0x20, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x23, 0x47},
246*ee09084fSZhu Ning 	{96, 8000, 768000, 0x60, 0x02, 0x1D, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
247*ee09084fSZhu Ning 	{96, 16000, 1536000, 0x20, 0x02, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x3F},
248*ee09084fSZhu Ning 	{100, 48000, 4800000, 0x04, 0x04, 0x3F, 0x6D, 0xB8, 0x08, 0x4f, 0x1f},
249*ee09084fSZhu Ning 	{125, 48000, 6000000, 0x04, 0x04, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
250*ee09084fSZhu Ning 
251*ee09084fSZhu Ning 	{128, 8000, 1024000, 0x60, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
252*ee09084fSZhu Ning 	{128, 16000, 2048000, 0x20, 0x00, 0x31, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
253*ee09084fSZhu Ning 	{128, 44100, 5644800, 0xE0, 0x00, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
254*ee09084fSZhu Ning 	{128, 48000, 6144000, 0xE0, 0x00, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
255*ee09084fSZhu Ning 	{144, 8000, 1152000, 0x20, 0x00, 0x03, 0x35, 0x8A, 0x1B, 0x23, 0x47},
256*ee09084fSZhu Ning 	{144, 16000, 2304000, 0x20, 0x00, 0x11, 0x35, 0x8A, 0x1B, 0x23, 0x47},
257*ee09084fSZhu Ning 	{192, 8000, 1536000, 0x60, 0x02, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
258*ee09084fSZhu Ning 	{192, 32000, 6144000, 0xE0, 0x02, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
259*ee09084fSZhu Ning 	{192, 16000, 3072000, 0x20, 0x02, 0x05, 0x75, 0xCA, 0x1B, 0x1F, 0x3F},
260*ee09084fSZhu Ning 
261*ee09084fSZhu Ning 	{200, 48000, 9600000, 0x04, 0x04, 0x0F, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
262*ee09084fSZhu Ning 	{250, 48000, 12000000, 0x04, 0x04, 0x0F, 0x2D, 0xCA, 0x0A, 0x27, 0x27},
263*ee09084fSZhu Ning 	{256, 8000, 2048000, 0x60, 0x00, 0x31, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
264*ee09084fSZhu Ning 	{256, 16000, 4096000, 0x20, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
265*ee09084fSZhu Ning 	{256, 44100, 11289600, 0xE0, 0x00, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
266*ee09084fSZhu Ning 	{256, 48000, 12288000, 0xE0, 0x00, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
267*ee09084fSZhu Ning 	{288, 8000, 2304000, 0x20, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x23, 0x47},
268*ee09084fSZhu Ning 	{384, 8000, 3072000, 0x60, 0x02, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x7F},
269*ee09084fSZhu Ning 	{384, 16000, 6144000, 0x20, 0x02, 0x03, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
270*ee09084fSZhu Ning 	{384, 32000, 12288000, 0xE0, 0x02, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
271*ee09084fSZhu Ning 	{384, 48000, 18432000, 0x00, 0x02, 0x01, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
272*ee09084fSZhu Ning 
273*ee09084fSZhu Ning 	{400, 48000, 19200000, 0xE4, 0x04, 0x35, 0x6d, 0xCA, 0x0A, 0x1F, 0x1F},
274*ee09084fSZhu Ning 	{500, 48000, 24000000, 0xF8, 0x04, 0x3F, 0x6D, 0xCA, 0x0A, 0x1F, 0x1F},
275*ee09084fSZhu Ning 	{512, 8000, 4096000, 0x60, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
276*ee09084fSZhu Ning 	{512, 16000, 8192000, 0x20, 0x00, 0x30, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
277*ee09084fSZhu Ning 	{512, 44100, 22579200, 0xE0, 0x00, 0x00, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
278*ee09084fSZhu Ning 	{512, 48000, 24576000, 0xE0, 0x00, 0x00, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
279*ee09084fSZhu Ning 	{768, 8000, 6144000, 0x60, 0x02, 0x11, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
280*ee09084fSZhu Ning 	{768, 16000, 12288000, 0x20, 0x02, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
281*ee09084fSZhu Ning 	{768, 32000, 24576000, 0xE0, 0x02, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F},
282*ee09084fSZhu Ning 	{800, 48000, 38400000, 0x00, 0x18, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F},
283*ee09084fSZhu Ning 
284*ee09084fSZhu Ning 	{1024, 8000, 8192000, 0x60, 0x00, 0x30, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
285*ee09084fSZhu Ning 	{1024, 16000, 16384000, 0x20, 0x00, 0x00, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
286*ee09084fSZhu Ning 	{1152, 16000, 18432000, 0x20, 0x08, 0x11, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
287*ee09084fSZhu Ning 	{1536, 8000, 12288000, 0x60, 0x02, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
288*ee09084fSZhu Ning 	{1536, 16000, 24576000, 0x20, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x3F},
289*ee09084fSZhu Ning 	{1625, 8000, 13000000, 0x0C, 0x18, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
290*ee09084fSZhu Ning 	{1625, 16000, 26000000, 0x0C, 0x18, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
291*ee09084fSZhu Ning 	{2048, 8000, 16384000, 0x60, 0x00, 0x00, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
292*ee09084fSZhu Ning 	{2304, 8000, 18432000, 0x40, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x5F},
293*ee09084fSZhu Ning 	{3072, 8000, 24576000, 0x60, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x7F},
294*ee09084fSZhu Ning 	{3250, 8000, 26000000, 0x0C, 0x18, 0x0F, 0x2D, 0x8A, 0x0A, 0x27, 0x27},
295*ee09084fSZhu Ning };
296*ee09084fSZhu Ning 
297*ee09084fSZhu Ning static inline int get_coeff(int mclk, int rate, int array,
298*ee09084fSZhu Ning 				const struct _coeff_div *coeff_div)
2995c439937SZhu Ning {
3005c439937SZhu Ning 	int i;
3015c439937SZhu Ning 
302*ee09084fSZhu Ning 	for (i = 0; i < array; i++) {
3035c439937SZhu Ning 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
3045c439937SZhu Ning 			return i;
3055c439937SZhu Ning 	}
3065c439937SZhu Ning 
3075c439937SZhu Ning 	return -EINVAL;
3085c439937SZhu Ning }
3095c439937SZhu Ning 
310*ee09084fSZhu Ning 
3115c439937SZhu Ning static int es8326_set_dai_sysclk(struct snd_soc_dai *codec_dai,
3125c439937SZhu Ning 				 int clk_id, unsigned int freq, int dir)
3135c439937SZhu Ning {
3145c439937SZhu Ning 	struct snd_soc_component *codec = codec_dai->component;
3155c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
3165c439937SZhu Ning 
3175c439937SZhu Ning 	es8326->sysclk = freq;
3185c439937SZhu Ning 
3195c439937SZhu Ning 	return 0;
3205c439937SZhu Ning }
3215c439937SZhu Ning 
3225c439937SZhu Ning static int es8326_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
3235c439937SZhu Ning {
3245c439937SZhu Ning 	struct snd_soc_component *component = codec_dai->component;
3255c439937SZhu Ning 	u8 iface = 0;
3265c439937SZhu Ning 
3275c439937SZhu Ning 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
3285c439937SZhu Ning 	case SND_SOC_DAIFMT_CBC_CFP:
3295c439937SZhu Ning 		snd_soc_component_update_bits(component, ES8326_RESET,
3305c439937SZhu Ning 					      ES8326_MASTER_MODE_EN, ES8326_MASTER_MODE_EN);
3315c439937SZhu Ning 		break;
3325c439937SZhu Ning 	case SND_SOC_DAIFMT_CBC_CFC:
3335c439937SZhu Ning 		break;
3345c439937SZhu Ning 	default:
3355c439937SZhu Ning 		return -EINVAL;
3365c439937SZhu Ning 	}
3375c439937SZhu Ning 
3385c439937SZhu Ning 	/* interface format */
3395c439937SZhu Ning 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
3405c439937SZhu Ning 	case SND_SOC_DAIFMT_I2S:
3415c439937SZhu Ning 		break;
3425c439937SZhu Ning 	case SND_SOC_DAIFMT_RIGHT_J:
3435c439937SZhu Ning 		dev_err(component->dev, "Codec driver does not support right justified\n");
3445c439937SZhu Ning 		return -EINVAL;
3455c439937SZhu Ning 	case SND_SOC_DAIFMT_LEFT_J:
3465c439937SZhu Ning 		iface |= ES8326_DAIFMT_LEFT_J;
3475c439937SZhu Ning 		break;
3485c439937SZhu Ning 	case SND_SOC_DAIFMT_DSP_A:
3495c439937SZhu Ning 		iface |= ES8326_DAIFMT_DSP_A;
3505c439937SZhu Ning 		break;
3515c439937SZhu Ning 	case SND_SOC_DAIFMT_DSP_B:
3525c439937SZhu Ning 		iface |= ES8326_DAIFMT_DSP_B;
3535c439937SZhu Ning 		break;
3545c439937SZhu Ning 	default:
3555c439937SZhu Ning 		return -EINVAL;
3565c439937SZhu Ning 	}
3575c439937SZhu Ning 
3585c439937SZhu Ning 	snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DAIFMT_MASK, iface);
3595c439937SZhu Ning 
3605c439937SZhu Ning 	return 0;
3615c439937SZhu Ning }
3625c439937SZhu Ning 
3635c439937SZhu Ning static int es8326_pcm_hw_params(struct snd_pcm_substream *substream,
3645c439937SZhu Ning 				struct snd_pcm_hw_params *params,
3655c439937SZhu Ning 				struct snd_soc_dai *dai)
3665c439937SZhu Ning {
3675c439937SZhu Ning 	struct snd_soc_component *component = dai->component;
368*ee09084fSZhu Ning 	const struct _coeff_div *coeff_div;
3695c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
3705c439937SZhu Ning 	u8 srate = 0;
371*ee09084fSZhu Ning 	int coeff, array;
3725c439937SZhu Ning 
373*ee09084fSZhu Ning 	if (es8326->version == 0) {
374*ee09084fSZhu Ning 		coeff_div =  coeff_div_v0;
375*ee09084fSZhu Ning 		array = ARRAY_SIZE(coeff_div_v0);
376*ee09084fSZhu Ning 	} else {
377*ee09084fSZhu Ning 		coeff_div =  coeff_div_v3;
378*ee09084fSZhu Ning 		array = ARRAY_SIZE(coeff_div_v3);
379*ee09084fSZhu Ning 	}
380*ee09084fSZhu Ning 	coeff = get_coeff(es8326->sysclk, params_rate(params), array, coeff_div);
3815c439937SZhu Ning 	/* bit size */
3825c439937SZhu Ning 	switch (params_format(params)) {
3835c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S16_LE:
3845c439937SZhu Ning 		srate |= ES8326_S16_LE;
3855c439937SZhu Ning 		break;
3865c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S20_3LE:
3875c439937SZhu Ning 		srate |= ES8326_S20_3_LE;
3885c439937SZhu Ning 		break;
3895c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S18_3LE:
3905c439937SZhu Ning 		srate |= ES8326_S18_LE;
3915c439937SZhu Ning 		break;
3925c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S24_LE:
3935c439937SZhu Ning 		srate |= ES8326_S24_LE;
3945c439937SZhu Ning 		break;
3955c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S32_LE:
3965c439937SZhu Ning 		srate |= ES8326_S32_LE;
3975c439937SZhu Ning 		break;
3985c439937SZhu Ning 	default:
3995c439937SZhu Ning 		return -EINVAL;
4005c439937SZhu Ning 	}
4015c439937SZhu Ning 
4025c439937SZhu Ning 	/* set iface & srate */
4035c439937SZhu Ning 	snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DATA_LEN_MASK, srate);
4045c439937SZhu Ning 
4055c439937SZhu Ning 	if (coeff >= 0) {
4065c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DIV1,
4075c439937SZhu Ning 			     coeff_div[coeff].reg4);
4085c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DIV2,
4095c439937SZhu Ning 			     coeff_div[coeff].reg5);
4105c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DLL,
4115c439937SZhu Ning 			     coeff_div[coeff].reg6);
4125c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_MUX,
4135c439937SZhu Ning 			     coeff_div[coeff].reg7);
4145c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_ADC_SEL,
4155c439937SZhu Ning 			     coeff_div[coeff].reg8);
4165c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DAC_SEL,
4175c439937SZhu Ning 			     coeff_div[coeff].reg9);
4185c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_ADC_OSR,
4195c439937SZhu Ning 			     coeff_div[coeff].rega);
4205c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DAC_OSR,
4215c439937SZhu Ning 			     coeff_div[coeff].regb);
4225c439937SZhu Ning 	} else {
4235c439937SZhu Ning 		dev_warn(component->dev, "Clock coefficients do not match");
4245c439937SZhu Ning 	}
4255c439937SZhu Ning 
4265c439937SZhu Ning 	return 0;
4275c439937SZhu Ning }
4285c439937SZhu Ning 
429083912c2SZhu Ning static int es8326_mute(struct snd_soc_dai *dai, int mute, int direction)
430083912c2SZhu Ning {
431083912c2SZhu Ning 	struct snd_soc_component *component = dai->component;
432083912c2SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
4330663286eSZhu Ning 	unsigned int offset_l, offset_r;
434083912c2SZhu Ning 
435083912c2SZhu Ning 	if (mute) {
436083912c2SZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
437083912c2SZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE,
438083912c2SZhu Ning 				ES8326_MUTE_MASK, ES8326_MUTE);
439083912c2SZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xf0);
440083912c2SZhu Ning 	} else {
441083912c2SZhu Ning 		if (!es8326->calibrated) {
442083912c2SZhu Ning 			regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_FORCE_CAL);
443083912c2SZhu Ning 			msleep(30);
4440663286eSZhu Ning 			regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
4450663286eSZhu Ning 			regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l);
4460663286eSZhu Ning 			regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r);
4470663286eSZhu Ning 			regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
4480663286eSZhu Ning 			regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l);
4490663286eSZhu Ning 			regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r);
450083912c2SZhu Ning 			es8326->calibrated = true;
451083912c2SZhu Ning 		}
452083912c2SZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa0);
4530663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_VOL, 0x80);
454083912c2SZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_ON);
455083912c2SZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE,
456083912c2SZhu Ning 				ES8326_MUTE_MASK, ~(ES8326_MUTE));
457083912c2SZhu Ning 	}
458083912c2SZhu Ning 	return 0;
459083912c2SZhu Ning }
460083912c2SZhu Ning 
4615c439937SZhu Ning static int es8326_set_bias_level(struct snd_soc_component *codec,
4625c439937SZhu Ning 				 enum snd_soc_bias_level level)
4635c439937SZhu Ning {
4645c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
4655c439937SZhu Ning 	int ret;
4665c439937SZhu Ning 
4675c439937SZhu Ning 	switch (level) {
4685c439937SZhu Ning 	case SND_SOC_BIAS_ON:
4695c439937SZhu Ning 		ret = clk_prepare_enable(es8326->mclk);
4705c439937SZhu Ning 		if (ret)
4715c439937SZhu Ning 			return ret;
4720663286eSZhu Ning 
4730663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_RESET, 0x9f);
4740663286eSZhu Ning 		msleep(20);
4750663286eSZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_DAC_DSM, 0x01, 0x00);
4760663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_INTOUT_IO, es8326->interrupt_clk);
4775c439937SZhu Ning 		regmap_write(es8326->regmap, ES8326_SDINOUT1_IO,
4785c439937SZhu Ning 			    (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT));
4790663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x0E);
4805c439937SZhu Ning 		regmap_write(es8326->regmap, ES8326_PGA_PDN, 0x40);
4810663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x00);
4820663286eSZhu Ning 		regmap_update_bits(es8326->regmap,  ES8326_CLK_CTL, 0x20, 0x20);
4835c439937SZhu Ning 		regmap_write(es8326->regmap, ES8326_RESET, ES8326_CSM_ON);
4845c439937SZhu Ning 		break;
4855c439937SZhu Ning 	case SND_SOC_BIAS_PREPARE:
4865c439937SZhu Ning 		break;
4875c439937SZhu Ning 	case SND_SOC_BIAS_STANDBY:
4885c439937SZhu Ning 		break;
4895c439937SZhu Ning 	case SND_SOC_BIAS_OFF:
4905c439937SZhu Ning 		clk_disable_unprepare(es8326->mclk);
4910663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x3b);
4925c439937SZhu Ning 		regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x00);
4930663286eSZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_CLK_CTL, 0x20, 0x00);
4945c439937SZhu Ning 		regmap_write(es8326->regmap, ES8326_SDINOUT1_IO, ES8326_IO_INPUT);
4955c439937SZhu Ning 		break;
4965c439937SZhu Ning 	}
4975c439937SZhu Ning 
4985c439937SZhu Ning 	return 0;
4995c439937SZhu Ning }
5005c439937SZhu Ning 
5015c439937SZhu Ning #define es8326_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
5025c439937SZhu Ning 	SNDRV_PCM_FMTBIT_S24_LE)
5035c439937SZhu Ning 
5045c439937SZhu Ning static const struct snd_soc_dai_ops es8326_ops = {
5055c439937SZhu Ning 	.hw_params = es8326_pcm_hw_params,
5065c439937SZhu Ning 	.set_fmt = es8326_set_dai_fmt,
5075c439937SZhu Ning 	.set_sysclk = es8326_set_dai_sysclk,
508083912c2SZhu Ning 	.mute_stream = es8326_mute,
509083912c2SZhu Ning 	.no_capture_mute = 1,
5105c439937SZhu Ning };
5115c439937SZhu Ning 
5125c439937SZhu Ning static struct snd_soc_dai_driver es8326_dai = {
5135c439937SZhu Ning 	.name = "ES8326 HiFi",
5145c439937SZhu Ning 	.playback = {
5155c439937SZhu Ning 		.stream_name = "Playback",
5165c439937SZhu Ning 		.channels_min = 1,
5175c439937SZhu Ning 		.channels_max = 2,
5185c439937SZhu Ning 		.rates = SNDRV_PCM_RATE_8000_48000,
5195c439937SZhu Ning 		.formats = es8326_FORMATS,
5205c439937SZhu Ning 		},
5215c439937SZhu Ning 	.capture = {
5225c439937SZhu Ning 		.stream_name = "Capture",
5235c439937SZhu Ning 		.channels_min = 1,
5245c439937SZhu Ning 		.channels_max = 2,
5255c439937SZhu Ning 		.rates = SNDRV_PCM_RATE_8000_48000,
5265c439937SZhu Ning 		.formats = es8326_FORMATS,
5275c439937SZhu Ning 		},
5285c439937SZhu Ning 	.ops = &es8326_ops,
5295c439937SZhu Ning 	.symmetric_rate = 1,
5305c439937SZhu Ning };
5315c439937SZhu Ning 
5325c439937SZhu Ning static void es8326_enable_micbias(struct snd_soc_component *component)
5335c439937SZhu Ning {
5345c439937SZhu Ning 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
5355c439937SZhu Ning 
5365c439937SZhu Ning 	snd_soc_dapm_mutex_lock(dapm);
5375c439937SZhu Ning 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS1");
5385c439937SZhu Ning 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS2");
5395c439937SZhu Ning 	snd_soc_dapm_sync_unlocked(dapm);
5405c439937SZhu Ning 	snd_soc_dapm_mutex_unlock(dapm);
5415c439937SZhu Ning }
5425c439937SZhu Ning 
5435c439937SZhu Ning static void es8326_disable_micbias(struct snd_soc_component *component)
5445c439937SZhu Ning {
5455c439937SZhu Ning 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
5465c439937SZhu Ning 
5475c439937SZhu Ning 	snd_soc_dapm_mutex_lock(dapm);
5485c439937SZhu Ning 	snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS1");
5495c439937SZhu Ning 	snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS2");
5505c439937SZhu Ning 	snd_soc_dapm_sync_unlocked(dapm);
5515c439937SZhu Ning 	snd_soc_dapm_mutex_unlock(dapm);
5525c439937SZhu Ning }
5535c439937SZhu Ning 
5545c439937SZhu Ning /*
5555c439937SZhu Ning  *	For button detection, set the following in soundcard
5565c439937SZhu Ning  *	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
5575c439937SZhu Ning  *	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
5585c439937SZhu Ning  *	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
5595c439937SZhu Ning  */
5605c439937SZhu Ning static void es8326_jack_button_handler(struct work_struct *work)
5615c439937SZhu Ning {
5625c439937SZhu Ning 	struct es8326_priv *es8326 =
5635c439937SZhu Ning 		container_of(work, struct es8326_priv, button_press_work.work);
5645c439937SZhu Ning 	struct snd_soc_component *comp = es8326->component;
5655c439937SZhu Ning 	unsigned int iface;
5665c439937SZhu Ning 	static int button_to_report, press_count;
5675c439937SZhu Ning 	static int prev_button, cur_button;
5685c439937SZhu Ning 
5695c439937SZhu Ning 	if (!(es8326->jack->status & SND_JACK_HEADSET)) /* Jack unplugged */
5705c439937SZhu Ning 		return;
5715c439937SZhu Ning 
5725c439937SZhu Ning 	mutex_lock(&es8326->lock);
5734ddad00cSZhu Ning 	iface = snd_soc_component_read(comp, ES8326_HPDET_STA);
5745c439937SZhu Ning 	switch (iface) {
5755c439937SZhu Ning 	case 0x93:
5765c439937SZhu Ning 		/* pause button detected */
5775c439937SZhu Ning 		cur_button = SND_JACK_BTN_0;
5785c439937SZhu Ning 		break;
5795c439937SZhu Ning 	case 0x6f:
58004f96c93SZhu Ning 	case 0x4b:
5815c439937SZhu Ning 		/* button volume up */
5825c439937SZhu Ning 		cur_button = SND_JACK_BTN_1;
5835c439937SZhu Ning 		break;
5845c439937SZhu Ning 	case 0x27:
5855c439937SZhu Ning 		/* button volume down */
5865c439937SZhu Ning 		cur_button = SND_JACK_BTN_2;
5875c439937SZhu Ning 		break;
5885c439937SZhu Ning 	case 0x1e:
58904f96c93SZhu Ning 	case 0xe2:
5905c439937SZhu Ning 		/* button released or not pressed */
5915c439937SZhu Ning 		cur_button = 0;
5925c439937SZhu Ning 		break;
5935c439937SZhu Ning 	default:
5945c439937SZhu Ning 		break;
5955c439937SZhu Ning 	}
5965c439937SZhu Ning 
5975c439937SZhu Ning 	if ((prev_button == cur_button) && (cur_button != 0)) {
5985c439937SZhu Ning 		press_count++;
59904f96c93SZhu Ning 		if (press_count > 3) {
60004f96c93SZhu Ning 			/* report a press every 120ms */
6015c439937SZhu Ning 			snd_soc_jack_report(es8326->jack, cur_button,
6025c439937SZhu Ning 					SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
6035c439937SZhu Ning 			press_count = 0;
6045c439937SZhu Ning 		}
6055c439937SZhu Ning 		button_to_report = cur_button;
6065c439937SZhu Ning 		queue_delayed_work(system_wq, &es8326->button_press_work,
60704f96c93SZhu Ning 				   msecs_to_jiffies(35));
6085c439937SZhu Ning 	} else if (prev_button != cur_button) {
6095c439937SZhu Ning 		/* mismatch, detect again */
6105c439937SZhu Ning 		prev_button = cur_button;
6115c439937SZhu Ning 		queue_delayed_work(system_wq, &es8326->button_press_work,
61204f96c93SZhu Ning 				   msecs_to_jiffies(35));
6135c439937SZhu Ning 	} else {
6145c439937SZhu Ning 		/* released or no pressed */
6155c439937SZhu Ning 		if (button_to_report != 0) {
6165c439937SZhu Ning 			snd_soc_jack_report(es8326->jack, button_to_report,
6175c439937SZhu Ning 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
6185c439937SZhu Ning 			snd_soc_jack_report(es8326->jack, 0,
6195c439937SZhu Ning 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
6205c439937SZhu Ning 			button_to_report = 0;
6215c439937SZhu Ning 		}
6225c439937SZhu Ning 	}
6235c439937SZhu Ning 	mutex_unlock(&es8326->lock);
6245c439937SZhu Ning }
6255c439937SZhu Ning 
6265c439937SZhu Ning static void es8326_jack_detect_handler(struct work_struct *work)
6275c439937SZhu Ning {
6285c439937SZhu Ning 	struct es8326_priv *es8326 =
6295c439937SZhu Ning 		container_of(work, struct es8326_priv, jack_detect_work.work);
6305c439937SZhu Ning 	struct snd_soc_component *comp = es8326->component;
6315c439937SZhu Ning 	unsigned int iface;
6325c439937SZhu Ning 
6335c439937SZhu Ning 	mutex_lock(&es8326->lock);
6344ddad00cSZhu Ning 	iface = snd_soc_component_read(comp, ES8326_HPDET_STA);
6355c439937SZhu Ning 	dev_dbg(comp->dev, "gpio flag %#04x", iface);
63604f96c93SZhu Ning 
637*ee09084fSZhu Ning 	if ((es8326->jack_remove_retry == 1) && (es8326->version != ES8326_VERSION_B)) {
63804f96c93SZhu Ning 		if (iface & ES8326_HPINSERT_FLAG)
63904f96c93SZhu Ning 			es8326->jack_remove_retry = 2;
64004f96c93SZhu Ning 		else
64104f96c93SZhu Ning 			es8326->jack_remove_retry = 0;
64204f96c93SZhu Ning 
64304f96c93SZhu Ning 		dev_dbg(comp->dev, "remove event check, set HPJACK_POL normal, cnt = %d\n",
64404f96c93SZhu Ning 				es8326->jack_remove_retry);
64504f96c93SZhu Ning 		/*
64604f96c93SZhu Ning 		 * Inverted HPJACK_POL bit to trigger one IRQ to double check HP Removal event
64704f96c93SZhu Ning 		 */
64804f96c93SZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE,
64904f96c93SZhu Ning 					ES8326_HP_DET_JACK_POL, (es8326->jd_inverted ?
65004f96c93SZhu Ning 					~es8326->jack_pol : es8326->jack_pol));
65104f96c93SZhu Ning 		goto exit;
65204f96c93SZhu Ning 	}
65304f96c93SZhu Ning 
6545c439937SZhu Ning 	if ((iface & ES8326_HPINSERT_FLAG) == 0) {
6555c439937SZhu Ning 		/* Jack unplugged or spurious IRQ */
65604f96c93SZhu Ning 		dev_dbg(comp->dev, "No headset detected\n");
65704f96c93SZhu Ning 		es8326_disable_micbias(es8326->component);
6585c439937SZhu Ning 		if (es8326->jack->status & SND_JACK_HEADPHONE) {
65904f96c93SZhu Ning 			dev_dbg(comp->dev, "Report hp remove event\n");
6605c439937SZhu Ning 			snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
66104f96c93SZhu Ning 			/* mute adc when mic path switch */
66204f96c93SZhu Ning 			regmap_write(es8326->regmap, ES8326_ADC_SCALE, 0x33);
66304f96c93SZhu Ning 			regmap_write(es8326->regmap, ES8326_ADC1_SRC, 0x44);
66404f96c93SZhu Ning 			regmap_write(es8326->regmap, ES8326_ADC2_SRC, 0x66);
66504f96c93SZhu Ning 			es8326->hp = 0;
66604f96c93SZhu Ning 		}
66704f96c93SZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x01);
66804f96c93SZhu Ning 		/*
66904f96c93SZhu Ning 		 * Inverted HPJACK_POL bit to trigger one IRQ to double check HP Removal event
67004f96c93SZhu Ning 		 */
671*ee09084fSZhu Ning 		if ((es8326->jack_remove_retry == 0) && (es8326->version != ES8326_VERSION_B)) {
67204f96c93SZhu Ning 			es8326->jack_remove_retry = 1;
67304f96c93SZhu Ning 			dev_dbg(comp->dev, "remove event check, invert HPJACK_POL, cnt = %d\n",
67404f96c93SZhu Ning 					es8326->jack_remove_retry);
67504f96c93SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE,
67604f96c93SZhu Ning 					ES8326_HP_DET_JACK_POL, (es8326->jd_inverted ?
67704f96c93SZhu Ning 					es8326->jack_pol : ~es8326->jack_pol));
67804f96c93SZhu Ning 
67904f96c93SZhu Ning 		} else {
68004f96c93SZhu Ning 			es8326->jack_remove_retry = 0;
6815c439937SZhu Ning 		}
6825c439937SZhu Ning 	} else if ((iface & ES8326_HPINSERT_FLAG) == ES8326_HPINSERT_FLAG) {
68304f96c93SZhu Ning 		es8326->jack_remove_retry = 0;
68404f96c93SZhu Ning 		if (es8326->hp == 0) {
68504f96c93SZhu Ning 			dev_dbg(comp->dev, "First insert, start OMTP/CTIA type check\n");
68604f96c93SZhu Ning 			/*
68704f96c93SZhu Ning 			 * set auto-check mode, then restart jack_detect_work after 100ms.
68804f96c93SZhu Ning 			 * Don't report jack status.
68904f96c93SZhu Ning 			 */
69004f96c93SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x01);
69104f96c93SZhu Ning 			usleep_range(50000, 70000);
69204f96c93SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x00);
69304f96c93SZhu Ning 			queue_delayed_work(system_wq, &es8326->jack_detect_work,
69404f96c93SZhu Ning 					msecs_to_jiffies(100));
69504f96c93SZhu Ning 			es8326->hp = 1;
69604f96c93SZhu Ning 			goto exit;
69704f96c93SZhu Ning 		}
6985c439937SZhu Ning 		if (es8326->jack->status & SND_JACK_HEADSET) {
6995c439937SZhu Ning 			/* detect button */
70004f96c93SZhu Ning 			dev_dbg(comp->dev, "button pressed\n");
7015c439937SZhu Ning 			queue_delayed_work(system_wq, &es8326->button_press_work, 10);
70204f96c93SZhu Ning 			goto exit;
70304f96c93SZhu Ning 		}
70404f96c93SZhu Ning 		if ((iface & ES8326_HPBUTTON_FLAG) == 0x01) {
70504f96c93SZhu Ning 			dev_dbg(comp->dev, "Headphone detected\n");
7065c439937SZhu Ning 			snd_soc_jack_report(es8326->jack,
7075c439937SZhu Ning 					SND_JACK_HEADPHONE, SND_JACK_HEADSET);
70804f96c93SZhu Ning 		} else {
70904f96c93SZhu Ning 			dev_dbg(comp->dev, "Headset detected\n");
71004f96c93SZhu Ning 			snd_soc_jack_report(es8326->jack,
71104f96c93SZhu Ning 					SND_JACK_HEADSET, SND_JACK_HEADSET);
71204f96c93SZhu Ning 
71304f96c93SZhu Ning 			regmap_write(es8326->regmap, ES8326_ADC_SCALE, 0x33);
71404f96c93SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_PGA_PDN,
71504f96c93SZhu Ning 					0x08, 0x08);
71604f96c93SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_PGAGAIN,
71704f96c93SZhu Ning 					0x80, 0x80);
71804f96c93SZhu Ning 			regmap_write(es8326->regmap, ES8326_ADC1_SRC, 0x00);
71904f96c93SZhu Ning 			regmap_write(es8326->regmap, ES8326_ADC2_SRC, 0x00);
72004f96c93SZhu Ning 			regmap_update_bits(es8326->regmap, ES8326_PGA_PDN,
72104f96c93SZhu Ning 					0x08, 0x00);
72204f96c93SZhu Ning 			usleep_range(10000, 15000);
7235c439937SZhu Ning 		}
7245c439937SZhu Ning 	}
72504f96c93SZhu Ning exit:
7265c439937SZhu Ning 	mutex_unlock(&es8326->lock);
7275c439937SZhu Ning }
7285c439937SZhu Ning 
7295c439937SZhu Ning static irqreturn_t es8326_irq(int irq, void *dev_id)
7305c439937SZhu Ning {
7315c439937SZhu Ning 	struct es8326_priv *es8326 = dev_id;
7325c439937SZhu Ning 	struct snd_soc_component *comp = es8326->component;
7335c439937SZhu Ning 
7345c439937SZhu Ning 	if (!es8326->jack)
7355c439937SZhu Ning 		goto out;
7365c439937SZhu Ning 
7375c439937SZhu Ning 	es8326_enable_micbias(comp);
7385c439937SZhu Ning 
7395c439937SZhu Ning 	if (es8326->jack->status & SND_JACK_HEADSET)
7405c439937SZhu Ning 		queue_delayed_work(system_wq, &es8326->jack_detect_work,
7415c439937SZhu Ning 				   msecs_to_jiffies(10));
7425c439937SZhu Ning 	else
7435c439937SZhu Ning 		queue_delayed_work(system_wq, &es8326->jack_detect_work,
74404f96c93SZhu Ning 				   msecs_to_jiffies(600));
7455c439937SZhu Ning 
7465c439937SZhu Ning out:
7475c439937SZhu Ning 	return IRQ_HANDLED;
7485c439937SZhu Ning }
7495c439937SZhu Ning 
7500663286eSZhu Ning static int es8326_calibrate(struct snd_soc_component *component)
7510663286eSZhu Ning {
7520663286eSZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
7530663286eSZhu Ning 	unsigned int reg;
7540663286eSZhu Ning 	unsigned int offset_l, offset_r;
7550663286eSZhu Ning 
7560663286eSZhu Ning 	regmap_read(es8326->regmap, ES8326_CHIP_VERSION, &reg);
7570663286eSZhu Ning 	es8326->version = reg;
7580663286eSZhu Ning 
7590663286eSZhu Ning 	if ((es8326->version == ES8326_VERSION_B) && (es8326->calibrated == false)) {
7600663286eSZhu Ning 		dev_dbg(component->dev, "ES8326_VERSION_B, calibrating\n");
7610663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_INV, 0xc0);
7620663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_DIV1, 0x01);
7630663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_DLL, 0x30);
7640663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_MUX, 0xed);
7650663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_TRI, 0xc1);
7660663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_DAC_MUTE, 0x03);
7670663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7f);
7680663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x33);
7690663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_DAC2HPMIX, 0x88);
7700663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_VOL, 0x80);
7710663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
7720663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_RESET, 0xc0);
7730663286eSZhu Ning 		usleep_range(15000, 20000);
7740663286eSZhu Ning 
7750663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, ES8326_HP_OFF);
7760663286eSZhu Ning 		regmap_read(es8326->regmap, ES8326_CSM_MUTE_STA, &reg);
7770663286eSZhu Ning 		if ((reg & 0xf0) != 0x40)
7780663286eSZhu Ning 			msleep(50);
7790663286eSZhu Ning 
7800663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_CAL, 0xd4);
7810663286eSZhu Ning 		msleep(200);
7820663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_CAL, 0x4d);
7830663286eSZhu Ning 		msleep(200);
7840663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
7850663286eSZhu Ning 		regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l);
7860663286eSZhu Ning 		regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r);
7870663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
7880663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l);
7890663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r);
7900663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00);
7910663286eSZhu Ning 
7920663286eSZhu Ning 		es8326->calibrated = true;
7930663286eSZhu Ning 	}
7940663286eSZhu Ning 
7950663286eSZhu Ning 	return 0;
7960663286eSZhu Ning }
7970663286eSZhu Ning 
7985c439937SZhu Ning static int es8326_resume(struct snd_soc_component *component)
7995c439937SZhu Ning {
8005c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
8015c439937SZhu Ning 
8025c439937SZhu Ning 	regcache_cache_only(es8326->regmap, false);
8035c439937SZhu Ning 	regcache_sync(es8326->regmap);
8045c439937SZhu Ning 
805ac20a73dSZhu Ning 	/* reset internal clock state */
806ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_RESET, 0x1f);
807ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x0E);
808ac20a73dSZhu Ning 	usleep_range(10000, 15000);
8095c439937SZhu Ning 	regmap_write(es8326->regmap, ES8326_HPJACK_TIMER, 0x88);
810ac20a73dSZhu Ning 	/* set headphone default type and detect pin */
811ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x81);
812ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_RESAMPLE, 0x05);
8135c439937SZhu Ning 
814ac20a73dSZhu Ning 	/* set internal oscillator as clock source of headpone cp */
815ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_DIV_CPC, 0x84);
816ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_ON);
817ac20a73dSZhu Ning 	/* clock manager reset release */
818ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_RESET, 0x17);
819ac20a73dSZhu Ning 	/* set headphone detection as half scan mode */
820ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_MISC, 0x08);
821ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_PULLUP_CTL, 0x00);
822ac20a73dSZhu Ning 
823ac20a73dSZhu Ning 	/* enable headphone driver */
824ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa7);
825ac20a73dSZhu Ning 	usleep_range(2000, 5000);
826ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0xab);
827ac20a73dSZhu Ning 	usleep_range(2000, 5000);
828ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0xbb);
829ac20a73dSZhu Ning 	usleep_range(2000, 5000);
830ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa1);
831ac20a73dSZhu Ning 
832ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00);
833ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_VMIDS1, 0xc4);
834ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_VMIDS2, 0x81);
835ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_CAL_TIME, 0x00);
8360663286eSZhu Ning 	/* calibrate for B version */
8370663286eSZhu Ning 	es8326_calibrate(component);
838ac20a73dSZhu Ning 	/* turn off headphone out */
839ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_CAL, 0x00);
840ac20a73dSZhu Ning 	/* set ADC and DAC in low power mode */
841ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_ANA_LP, 0xf0);
842ac20a73dSZhu Ning 
843ac20a73dSZhu Ning 	/* force micbias on */
844ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_ANA_MICBIAS, 0x4f);
845ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_SYS_BIAS, 0x08);
8465c439937SZhu Ning 	regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7F);
847ac20a73dSZhu Ning 	/* select vdda as micbias source */
848ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x23);
849ac20a73dSZhu Ning 	/* set dac dsmclip = 1 */
850ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_DAC_DSM, 0x08);
851ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_DAC_VPPSCALE, 0x15);
852ac20a73dSZhu Ning 
853ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_INT_SOURCE,
854ac20a73dSZhu Ning 		    (ES8326_INT_SRC_PIN9 | ES8326_INT_SRC_BUTTON));
855ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_INTOUT_IO,
856ac20a73dSZhu Ning 		     es8326->interrupt_clk);
857ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_SDINOUT1_IO,
858ac20a73dSZhu Ning 		    (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT));
859ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_SDINOUT23_IO, ES8326_IO_INPUT);
860ac20a73dSZhu Ning 
861ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x3b);
862ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_RESET, ES8326_CSM_ON);
863ac20a73dSZhu Ning 	regmap_update_bits(es8326->regmap, ES8326_PGAGAIN, ES8326_MIC_SEL_MASK,
864ac20a73dSZhu Ning 			   ES8326_MIC1_SEL);
865ac20a73dSZhu Ning 
866ac20a73dSZhu Ning 	regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE, ES8326_MUTE_MASK,
867ac20a73dSZhu Ning 			   ES8326_MUTE);
868ac20a73dSZhu Ning 
869ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x80 |
870ac20a73dSZhu Ning 			((es8326->version == ES8326_VERSION_B) ?
871ac20a73dSZhu Ning 			(ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol) :
872ac20a73dSZhu Ning 			(ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol | 0x04)));
8735c439937SZhu Ning 
87404f96c93SZhu Ning 	es8326->jack_remove_retry = 0;
87504f96c93SZhu Ning 	es8326->hp = 0;
8765c439937SZhu Ning 	return 0;
8775c439937SZhu Ning }
8785c439937SZhu Ning 
8795c439937SZhu Ning static int es8326_suspend(struct snd_soc_component *component)
8805c439937SZhu Ning {
8815c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
8825c439937SZhu Ning 
8835c439937SZhu Ning 	cancel_delayed_work_sync(&es8326->jack_detect_work);
8845c439937SZhu Ning 	es8326_disable_micbias(component);
885083912c2SZhu Ning 	es8326->calibrated = false;
8865c439937SZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_OFF);
8875c439937SZhu Ning 	regcache_cache_only(es8326->regmap, true);
8885c439937SZhu Ning 	regcache_mark_dirty(es8326->regmap);
8895c439937SZhu Ning 
890ac20a73dSZhu Ning 	/* reset register value to default */
891ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x01);
892ac20a73dSZhu Ning 	usleep_range(1000, 3000);
893ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x00);
8945c439937SZhu Ning 	return 0;
8955c439937SZhu Ning }
8965c439937SZhu Ning 
8975c439937SZhu Ning static int es8326_probe(struct snd_soc_component *component)
8985c439937SZhu Ning {
8995c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
9005c439937SZhu Ning 	int ret;
9015c439937SZhu Ning 
9025c439937SZhu Ning 	es8326->component = component;
9035c439937SZhu Ning 	es8326->jd_inverted = device_property_read_bool(component->dev,
9045c439937SZhu Ning 							"everest,jack-detect-inverted");
9055c439937SZhu Ning 
9065c439937SZhu Ning 	ret = device_property_read_u8(component->dev, "everest,mic1-src", &es8326->mic1_src);
9075c439937SZhu Ning 	if (ret != 0) {
9085c439937SZhu Ning 		dev_dbg(component->dev, "mic1-src return %d", ret);
9095c439937SZhu Ning 		es8326->mic1_src = ES8326_ADC_AMIC;
9105c439937SZhu Ning 	}
9115c439937SZhu Ning 	dev_dbg(component->dev, "mic1-src %x", es8326->mic1_src);
9125c439937SZhu Ning 
9135c439937SZhu Ning 	ret = device_property_read_u8(component->dev, "everest,mic2-src", &es8326->mic2_src);
9145c439937SZhu Ning 	if (ret != 0) {
9155c439937SZhu Ning 		dev_dbg(component->dev, "mic2-src return %d", ret);
9165c439937SZhu Ning 		es8326->mic2_src = ES8326_ADC_DMIC;
9175c439937SZhu Ning 	}
9185c439937SZhu Ning 	dev_dbg(component->dev, "mic2-src %x", es8326->mic2_src);
9195c439937SZhu Ning 
9205c439937SZhu Ning 	ret = device_property_read_u8(component->dev, "everest,jack-pol", &es8326->jack_pol);
9215c439937SZhu Ning 	if (ret != 0) {
9225c439937SZhu Ning 		dev_dbg(component->dev, "jack-pol return %d", ret);
923ac20a73dSZhu Ning 		es8326->jack_pol = ES8326_HP_TYPE_AUTO;
9245c439937SZhu Ning 	}
9255c439937SZhu Ning 	dev_dbg(component->dev, "jack-pol %x", es8326->jack_pol);
9265c439937SZhu Ning 
927fe1e7e8cSAlexey Firago 	ret = device_property_read_u8(component->dev, "everest,interrupt-src",
928fe1e7e8cSAlexey Firago 				      &es8326->interrupt_src);
9295c439937SZhu Ning 	if (ret != 0) {
9305c439937SZhu Ning 		dev_dbg(component->dev, "interrupt-src return %d", ret);
9315c439937SZhu Ning 		es8326->interrupt_src = ES8326_HP_DET_SRC_PIN9;
9325c439937SZhu Ning 	}
9335c439937SZhu Ning 	dev_dbg(component->dev, "interrupt-src %x", es8326->interrupt_src);
9345c439937SZhu Ning 
935fe1e7e8cSAlexey Firago 	ret = device_property_read_u8(component->dev, "everest,interrupt-clk",
936fe1e7e8cSAlexey Firago 				      &es8326->interrupt_clk);
9375c439937SZhu Ning 	if (ret != 0) {
9385c439937SZhu Ning 		dev_dbg(component->dev, "interrupt-clk return %d", ret);
9395c439937SZhu Ning 		es8326->interrupt_clk = 0x45;
9405c439937SZhu Ning 	}
9415c439937SZhu Ning 	dev_dbg(component->dev, "interrupt-clk %x", es8326->interrupt_clk);
9425c439937SZhu Ning 
9435c439937SZhu Ning 	es8326_resume(component);
9445c439937SZhu Ning 	return 0;
9455c439937SZhu Ning }
9465c439937SZhu Ning 
9475c439937SZhu Ning static void es8326_enable_jack_detect(struct snd_soc_component *component,
9485c439937SZhu Ning 				struct snd_soc_jack *jack)
9495c439937SZhu Ning {
9505c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
9515c439937SZhu Ning 
9525c439937SZhu Ning 	mutex_lock(&es8326->lock);
9535c439937SZhu Ning 	if (es8326->jd_inverted)
9544ddad00cSZhu Ning 		snd_soc_component_update_bits(component, ES8326_HPDET_TYPE,
9555c439937SZhu Ning 					      ES8326_HP_DET_JACK_POL, ~es8326->jack_pol);
9565c439937SZhu Ning 	es8326->jack = jack;
9575c439937SZhu Ning 
9585c439937SZhu Ning 	mutex_unlock(&es8326->lock);
9595c439937SZhu Ning 	es8326_irq(es8326->irq, es8326);
9605c439937SZhu Ning }
9615c439937SZhu Ning 
9625c439937SZhu Ning static void es8326_disable_jack_detect(struct snd_soc_component *component)
9635c439937SZhu Ning {
9645c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
9655c439937SZhu Ning 
9665c439937SZhu Ning 	dev_dbg(component->dev, "Enter into %s\n", __func__);
9675c439937SZhu Ning 	if (!es8326->jack)
9685c439937SZhu Ning 		return; /* Already disabled (or never enabled) */
9695c439937SZhu Ning 	cancel_delayed_work_sync(&es8326->jack_detect_work);
9705c439937SZhu Ning 
9715c439937SZhu Ning 	mutex_lock(&es8326->lock);
9725c439937SZhu Ning 	if (es8326->jack->status & SND_JACK_MICROPHONE) {
9735c439937SZhu Ning 		es8326_disable_micbias(component);
9745c439937SZhu Ning 		snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
9755c439937SZhu Ning 	}
9765c439937SZhu Ning 	es8326->jack = NULL;
9775c439937SZhu Ning 	mutex_unlock(&es8326->lock);
9785c439937SZhu Ning }
9795c439937SZhu Ning 
9805c439937SZhu Ning static int es8326_set_jack(struct snd_soc_component *component,
9815c439937SZhu Ning 			struct snd_soc_jack *jack, void *data)
9825c439937SZhu Ning {
9835c439937SZhu Ning 	if (jack)
9845c439937SZhu Ning 		es8326_enable_jack_detect(component, jack);
9855c439937SZhu Ning 	else
9865c439937SZhu Ning 		es8326_disable_jack_detect(component);
9875c439937SZhu Ning 
9885c439937SZhu Ning 	return 0;
9895c439937SZhu Ning }
9905c439937SZhu Ning 
9915c439937SZhu Ning static void es8326_remove(struct snd_soc_component *component)
9925c439937SZhu Ning {
9935c439937SZhu Ning 	es8326_disable_jack_detect(component);
9945c439937SZhu Ning 	es8326_set_bias_level(component, SND_SOC_BIAS_OFF);
9955c439937SZhu Ning }
9965c439937SZhu Ning 
9975c439937SZhu Ning static const struct snd_soc_component_driver soc_component_dev_es8326 = {
9985c439937SZhu Ning 	.probe		= es8326_probe,
9995c439937SZhu Ning 	.remove		= es8326_remove,
10005c439937SZhu Ning 	.resume		= es8326_resume,
10015c439937SZhu Ning 	.suspend	= es8326_suspend,
10025c439937SZhu Ning 	.set_bias_level = es8326_set_bias_level,
10035c439937SZhu Ning 	.set_jack	= es8326_set_jack,
10045c439937SZhu Ning 	.dapm_widgets	= es8326_dapm_widgets,
10055c439937SZhu Ning 	.num_dapm_widgets	= ARRAY_SIZE(es8326_dapm_widgets),
10065c439937SZhu Ning 	.dapm_routes		= es8326_dapm_routes,
10075c439937SZhu Ning 	.num_dapm_routes	= ARRAY_SIZE(es8326_dapm_routes),
10085c439937SZhu Ning 	.controls		= es8326_snd_controls,
10095c439937SZhu Ning 	.num_controls		= ARRAY_SIZE(es8326_snd_controls),
10105c439937SZhu Ning 	.use_pmdown_time	= 1,
10115c439937SZhu Ning 	.endianness		= 1,
10125c439937SZhu Ning };
10135c439937SZhu Ning 
1014784252baSUwe Kleine-König static int es8326_i2c_probe(struct i2c_client *i2c)
10155c439937SZhu Ning {
10165c439937SZhu Ning 	struct es8326_priv *es8326;
10175c439937SZhu Ning 	int ret;
10185c439937SZhu Ning 
10195c439937SZhu Ning 	es8326 = devm_kzalloc(&i2c->dev, sizeof(struct es8326_priv), GFP_KERNEL);
10205c439937SZhu Ning 	if (!es8326)
10215c439937SZhu Ning 		return -ENOMEM;
10225c439937SZhu Ning 
10235c439937SZhu Ning 	i2c_set_clientdata(i2c, es8326);
10245c439937SZhu Ning 	es8326->i2c = i2c;
10255c439937SZhu Ning 	mutex_init(&es8326->lock);
10265c439937SZhu Ning 	es8326->regmap = devm_regmap_init_i2c(i2c, &es8326_regmap_config);
10275c439937SZhu Ning 	if (IS_ERR(es8326->regmap)) {
10285c439937SZhu Ning 		ret = PTR_ERR(es8326->regmap);
10295c439937SZhu Ning 		dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
10305c439937SZhu Ning 		return ret;
10315c439937SZhu Ning 	}
10325c439937SZhu Ning 
10335c439937SZhu Ning 	es8326->irq = i2c->irq;
10345c439937SZhu Ning 	INIT_DELAYED_WORK(&es8326->jack_detect_work,
10355c439937SZhu Ning 			  es8326_jack_detect_handler);
10365c439937SZhu Ning 	INIT_DELAYED_WORK(&es8326->button_press_work,
10375c439937SZhu Ning 			  es8326_jack_button_handler);
10385c439937SZhu Ning 	/* ES8316 is level-based while ES8326 is edge-based */
10395c439937SZhu Ning 	ret = devm_request_threaded_irq(&i2c->dev, es8326->irq, NULL, es8326_irq,
10405c439937SZhu Ning 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
10415c439937SZhu Ning 					"es8326", es8326);
10425c439937SZhu Ning 	if (ret) {
10435c439937SZhu Ning 		dev_warn(&i2c->dev, "Failed to request IRQ: %d: %d\n",
10445c439937SZhu Ning 		es8326->irq, ret);
10455c439937SZhu Ning 		es8326->irq = -ENXIO;
10465c439937SZhu Ning 	}
10475c439937SZhu Ning 
10485c439937SZhu Ning 	es8326->mclk = devm_clk_get_optional(&i2c->dev, "mclk");
10495c439937SZhu Ning 	if (IS_ERR(es8326->mclk)) {
10505c439937SZhu Ning 		dev_err(&i2c->dev, "unable to get mclk\n");
10515c439937SZhu Ning 		return PTR_ERR(es8326->mclk);
10525c439937SZhu Ning 	}
10535c439937SZhu Ning 	if (!es8326->mclk)
10545c439937SZhu Ning 		dev_warn(&i2c->dev, "assuming static mclk\n");
10555c439937SZhu Ning 
10565c439937SZhu Ning 	ret = clk_prepare_enable(es8326->mclk);
10575c439937SZhu Ning 	if (ret) {
10585c439937SZhu Ning 		dev_err(&i2c->dev, "unable to enable mclk\n");
10595c439937SZhu Ning 		return ret;
10605c439937SZhu Ning 	}
10615c439937SZhu Ning 	return devm_snd_soc_register_component(&i2c->dev,
10625c439937SZhu Ning 					&soc_component_dev_es8326,
10635c439937SZhu Ning 					&es8326_dai, 1);
10645c439937SZhu Ning }
10655c439937SZhu Ning 
10665c439937SZhu Ning static const struct i2c_device_id es8326_i2c_id[] = {
10675c439937SZhu Ning 	{"es8326", 0 },
10685c439937SZhu Ning 	{}
10695c439937SZhu Ning };
10705c439937SZhu Ning MODULE_DEVICE_TABLE(i2c, es8326_i2c_id);
10715c439937SZhu Ning 
10725c439937SZhu Ning #ifdef CONFIG_OF
10735c439937SZhu Ning static const struct of_device_id es8326_of_match[] = {
10745c439937SZhu Ning 	{ .compatible = "everest,es8326", },
10755c439937SZhu Ning 	{}
10765c439937SZhu Ning };
10775c439937SZhu Ning MODULE_DEVICE_TABLE(of, es8326_of_match);
10785c439937SZhu Ning #endif
10795c439937SZhu Ning 
10805c439937SZhu Ning #ifdef CONFIG_ACPI
10815c439937SZhu Ning static const struct acpi_device_id es8326_acpi_match[] = {
10825c439937SZhu Ning 	{"ESSX8326", 0},
10835c439937SZhu Ning 	{},
10845c439937SZhu Ning };
10855c439937SZhu Ning MODULE_DEVICE_TABLE(acpi, es8326_acpi_match);
10865c439937SZhu Ning #endif
10875c439937SZhu Ning 
10885c439937SZhu Ning static struct i2c_driver es8326_i2c_driver = {
10895c439937SZhu Ning 	.driver = {
10905c439937SZhu Ning 		.name = "es8326",
10915c439937SZhu Ning 		.acpi_match_table = ACPI_PTR(es8326_acpi_match),
10925c439937SZhu Ning 		.of_match_table = of_match_ptr(es8326_of_match),
10935c439937SZhu Ning 	},
10949abcd240SUwe Kleine-König 	.probe = es8326_i2c_probe,
10955c439937SZhu Ning 	.id_table = es8326_i2c_id,
10965c439937SZhu Ning };
10975c439937SZhu Ning module_i2c_driver(es8326_i2c_driver);
10985c439937SZhu Ning 
10995c439937SZhu Ning MODULE_DESCRIPTION("ASoC es8326 driver");
11005c439937SZhu Ning MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
11015c439937SZhu Ning MODULE_LICENSE("GPL");
1102