xref: /linux/sound/soc/codecs/es8326.c (revision 0663286e58e6f611f3578b5e63e1faa576d139fd)
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;
445c439937SZhu Ning };
455c439937SZhu Ning 
465c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9550, 50, 0);
475c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9550, 50, 0);
485c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_analog_pga_tlv, 0, 300, 0);
495c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_pga_tlv, 0, 600, 0);
505c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(softramp_rate, 0, 100, 0);
515c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_target_tlv, -3200, 200, 0);
525c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_recovery_tlv, -125, 250, 0);
535c439937SZhu Ning 
545c439937SZhu Ning static const char *const winsize[] = {
555c439937SZhu Ning 	"0.25db/2  LRCK",
565c439937SZhu Ning 	"0.25db/4  LRCK",
575c439937SZhu Ning 	"0.25db/8  LRCK",
585c439937SZhu Ning 	"0.25db/16  LRCK",
595c439937SZhu Ning 	"0.25db/32  LRCK",
605c439937SZhu Ning 	"0.25db/64  LRCK",
615c439937SZhu Ning 	"0.25db/128  LRCK",
625c439937SZhu Ning 	"0.25db/256  LRCK",
635c439937SZhu Ning 	"0.25db/512  LRCK",
645c439937SZhu Ning 	"0.25db/1024  LRCK",
655c439937SZhu Ning 	"0.25db/2048  LRCK",
665c439937SZhu Ning 	"0.25db/4096  LRCK",
675c439937SZhu Ning 	"0.25db/8192  LRCK",
685c439937SZhu Ning 	"0.25db/16384  LRCK",
695c439937SZhu Ning 	"0.25db/32768  LRCK",
705c439937SZhu Ning 	"0.25db/65536  LRCK",
715c439937SZhu Ning };
725c439937SZhu Ning 
735c439937SZhu Ning static const char *const dacpol_txt[] =	{
745c439937SZhu Ning 	"Normal", "R Invert", "L Invert", "L + R Invert" };
755c439937SZhu Ning 
765c439937SZhu Ning static const struct soc_enum dacpol =
775c439937SZhu Ning 	SOC_ENUM_SINGLE(ES8326_DAC_DSM, 4, 4, dacpol_txt);
785c439937SZhu Ning static const struct soc_enum alc_winsize =
795c439937SZhu Ning 	SOC_ENUM_SINGLE(ES8326_ADC_RAMPRATE, 4, 16, winsize);
805c439937SZhu Ning static const struct soc_enum drc_winsize =
815c439937SZhu Ning 	SOC_ENUM_SINGLE(ES8326_DRC_WINSIZE, 4, 16, winsize);
825c439937SZhu Ning 
835c439937SZhu Ning static const struct snd_kcontrol_new es8326_snd_controls[] = {
845c439937SZhu Ning 	SOC_SINGLE_TLV("DAC Playback Volume", ES8326_DAC_VOL, 0, 0xbf, 0, dac_vol_tlv),
855c439937SZhu Ning 	SOC_ENUM("Playback Polarity", dacpol),
865c439937SZhu Ning 	SOC_SINGLE_TLV("DAC Ramp Rate", ES8326_DAC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
875c439937SZhu Ning 	SOC_SINGLE_TLV("DRC Recovery Level", ES8326_DRC_RECOVERY, 0, 4, 0, drc_recovery_tlv),
885c439937SZhu Ning 	SOC_ENUM("DRC Winsize", drc_winsize),
895c439937SZhu Ning 	SOC_SINGLE_TLV("DRC Target Level", ES8326_DRC_WINSIZE, 0, 0x0f, 0, drc_target_tlv),
905c439937SZhu Ning 
915c439937SZhu Ning 	SOC_DOUBLE_R_TLV("ADC Capture Volume", ES8326_ADC1_VOL, ES8326_ADC2_VOL, 0, 0xff, 0,
925c439937SZhu Ning 			 adc_vol_tlv),
935c439937SZhu Ning 	SOC_DOUBLE_TLV("ADC PGA Volume", ES8326_ADC_SCALE, 4, 0, 5, 0, adc_pga_tlv),
945c439937SZhu Ning 	SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8326_PGAGAIN, 0, 10, 0, adc_analog_pga_tlv),
955c439937SZhu Ning 	SOC_SINGLE_TLV("ADC Ramp Rate", ES8326_ADC_RAMPRATE, 0, 0x0f, 0, softramp_rate),
965c439937SZhu Ning 	SOC_SINGLE("ALC Capture Switch", ES8326_ALC_RECOVERY, 3, 1, 0),
975c439937SZhu Ning 	SOC_SINGLE_TLV("ALC Capture Recovery Level", ES8326_ALC_LEVEL,
985c439937SZhu Ning 			0, 4, 0, drc_recovery_tlv),
995c439937SZhu Ning 	SOC_ENUM("ALC Capture Winsize", alc_winsize),
1005c439937SZhu Ning 	SOC_SINGLE_TLV("ALC Capture Target Level", ES8326_ALC_LEVEL,
1015c439937SZhu Ning 			0, 0x0f, 0, drc_target_tlv),
1025c439937SZhu Ning 
1035c439937SZhu Ning };
1045c439937SZhu Ning 
1055c439937SZhu Ning static const struct snd_soc_dapm_widget es8326_dapm_widgets[] = {
1065c439937SZhu Ning 	SND_SOC_DAPM_INPUT("MIC1"),
1075c439937SZhu Ning 	SND_SOC_DAPM_INPUT("MIC2"),
1085c439937SZhu Ning 	SND_SOC_DAPM_INPUT("MIC3"),
1095c439937SZhu Ning 	SND_SOC_DAPM_INPUT("MIC4"),
1105c439937SZhu Ning 
1115c439937SZhu Ning 	SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0),
1125c439937SZhu Ning 	SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0),
1135c439937SZhu Ning 
1145c439937SZhu Ning 	/* Digital Interface */
1155c439937SZhu Ning 	SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 0, SND_SOC_NOPM, 0, 0),
1165c439937SZhu Ning 	SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0, SND_SOC_NOPM, 0, 0),
1175c439937SZhu Ning 
1185c439937SZhu Ning 	/* ADC Digital Mute */
1195c439937SZhu Ning 	SND_SOC_DAPM_PGA("ADC L1", ES8326_ADC_MUTE, 0, 1, NULL, 0),
1205c439937SZhu Ning 	SND_SOC_DAPM_PGA("ADC R1", ES8326_ADC_MUTE, 1, 1, NULL, 0),
1215c439937SZhu Ning 	SND_SOC_DAPM_PGA("ADC L2", ES8326_ADC_MUTE, 2, 1, NULL, 0),
1225c439937SZhu Ning 	SND_SOC_DAPM_PGA("ADC R2", ES8326_ADC_MUTE, 3, 1, NULL, 0),
1235c439937SZhu Ning 
1245c439937SZhu Ning 	/* Analog Power Supply*/
1255c439937SZhu Ning 	SND_SOC_DAPM_DAC("Right DAC", NULL, ES8326_ANA_PDN, 0, 1),
1265c439937SZhu Ning 	SND_SOC_DAPM_DAC("Left DAC", NULL, ES8326_ANA_PDN, 1, 1),
1275c439937SZhu Ning 	SND_SOC_DAPM_SUPPLY("MICBIAS1", ES8326_ANA_MICBIAS, 2, 0, NULL, 0),
1285c439937SZhu Ning 	SND_SOC_DAPM_SUPPLY("MICBIAS2", ES8326_ANA_MICBIAS, 3, 0, NULL, 0),
1295c439937SZhu Ning 
1305c439937SZhu Ning 	SND_SOC_DAPM_PGA("LHPMIX", ES8326_DAC2HPMIX, 7, 0, NULL, 0),
1315c439937SZhu Ning 	SND_SOC_DAPM_PGA("RHPMIX", ES8326_DAC2HPMIX, 3, 0, NULL, 0),
1325c439937SZhu Ning 
1335c439937SZhu Ning 	SND_SOC_DAPM_OUTPUT("HPOL"),
1345c439937SZhu Ning 	SND_SOC_DAPM_OUTPUT("HPOR"),
1355c439937SZhu Ning };
1365c439937SZhu Ning 
1375c439937SZhu Ning static const struct snd_soc_dapm_route es8326_dapm_routes[] = {
1385c439937SZhu Ning 	{"ADC L1", NULL, "MIC1"},
1395c439937SZhu Ning 	{"ADC R1", NULL, "MIC2"},
1405c439937SZhu Ning 	{"ADC L2", NULL, "MIC3"},
1415c439937SZhu Ning 	{"ADC R2", NULL, "MIC4"},
1425c439937SZhu Ning 
1435c439937SZhu Ning 	{"ADC L", NULL, "ADC L1"},
1445c439937SZhu Ning 	{"ADC R", NULL, "ADC R1"},
1455c439937SZhu Ning 	{"ADC L", NULL, "ADC L2"},
1465c439937SZhu Ning 	{"ADC R", NULL, "ADC R2"},
1475c439937SZhu Ning 
1485c439937SZhu Ning 	{"I2S OUT", NULL, "ADC L"},
1495c439937SZhu Ning 	{"I2S OUT", NULL, "ADC R"},
1505c439937SZhu Ning 
1515c439937SZhu Ning 	{"Right DAC", NULL, "I2S IN"},
1525c439937SZhu Ning 	{"Left DAC", NULL, "I2S IN"},
1535c439937SZhu Ning 
1545c439937SZhu Ning 	{"LHPMIX", NULL, "Left DAC"},
1555c439937SZhu Ning 	{"RHPMIX", NULL, "Right DAC"},
1565c439937SZhu Ning 
1575c439937SZhu Ning 	{"HPOL", NULL, "LHPMIX"},
1585c439937SZhu Ning 	{"HPOR", NULL, "RHPMIX"},
1595c439937SZhu Ning };
1605c439937SZhu Ning 
161f1230a27SZhu Ning static bool es8326_volatile_register(struct device *dev, unsigned int reg)
162f1230a27SZhu Ning {
163f1230a27SZhu Ning 	switch (reg) {
164f1230a27SZhu Ning 	case ES8326_HPL_OFFSET_INI:
165f1230a27SZhu Ning 	case ES8326_HPR_OFFSET_INI:
166f1230a27SZhu Ning 	case ES8326_HPDET_STA:
167f1230a27SZhu Ning 	case ES8326_CTIA_OMTP_STA:
168f1230a27SZhu Ning 	case ES8326_CSM_MUTE_STA:
169f1230a27SZhu Ning 		return true;
170f1230a27SZhu Ning 	default:
171f1230a27SZhu Ning 		return false;
172f1230a27SZhu Ning 	}
173f1230a27SZhu Ning }
1745c439937SZhu Ning 
1755c69f11cSYang Yingliang static const struct regmap_config es8326_regmap_config = {
1765c439937SZhu Ning 	.reg_bits = 8,
1775c439937SZhu Ning 	.val_bits = 8,
1785c439937SZhu Ning 	.max_register = 0xff,
179f1230a27SZhu Ning 	.volatile_reg = es8326_volatile_register,
1805c439937SZhu Ning 	.cache_type = REGCACHE_RBTREE,
1815c439937SZhu Ning };
1825c439937SZhu Ning 
1835c439937SZhu Ning struct _coeff_div {
1845c439937SZhu Ning 	u16 fs;
1855c439937SZhu Ning 	u32 rate;
1865c439937SZhu Ning 	u32 mclk;
1875c439937SZhu Ning 	u8 reg4;
1885c439937SZhu Ning 	u8 reg5;
1895c439937SZhu Ning 	u8 reg6;
1905c439937SZhu Ning 	u8 reg7;
1915c439937SZhu Ning 	u8 reg8;
1925c439937SZhu Ning 	u8 reg9;
1935c439937SZhu Ning 	u8 rega;
1945c439937SZhu Ning 	u8 regb;
1955c439937SZhu Ning };
1965c439937SZhu Ning 
1975c439937SZhu Ning /* codec hifi mclk clock divider coefficients */
1985c439937SZhu Ning /* {ratio, LRCK, MCLK, REG04, REG05, REG06, REG07, REG08, REG09, REG10, REG11} */
1995c439937SZhu Ning static const struct _coeff_div coeff_div[] = {
2005c439937SZhu Ning 	{32, 8000, 256000, 0x60, 0x00, 0x0F, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
2015c439937SZhu Ning 	{32, 16000, 512000, 0x20, 0x00, 0x0D, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
2025c439937SZhu Ning 	{32, 44100, 1411200, 0x00, 0x00, 0x13, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2035c439937SZhu Ning 	{32, 48000, 1536000, 0x00, 0x00, 0x13, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2045c439937SZhu Ning 	{36, 8000, 288000, 0x20, 0x00, 0x0D, 0x75, 0x0A, 0x1B, 0x23, 0x47},
2055c439937SZhu Ning 	{36, 16000, 576000, 0x20, 0x00, 0x0D, 0x75, 0x0A, 0x1B, 0x23, 0x47},
2065c439937SZhu Ning 	{48, 8000, 384000, 0x60, 0x02, 0x1F, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
2075c439937SZhu Ning 	{48, 16000, 768000, 0x20, 0x02, 0x0F, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
2085c439937SZhu Ning 	{48, 48000, 2304000, 0x00, 0x02, 0x0D, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2095c439937SZhu Ning 	{64, 8000, 512000, 0x60, 0x00, 0x0D, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
2105c439937SZhu Ning 	{64, 16000, 1024000, 0x20, 0x00, 0x05, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
2115c439937SZhu Ning 
2125c439937SZhu Ning 	{64, 44100, 2822400, 0x00, 0x00, 0x11, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2135c439937SZhu Ning 	{64, 48000, 3072000, 0x00, 0x00, 0x11, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2145c439937SZhu Ning 	{72, 8000, 576000, 0x20, 0x00, 0x13, 0x35, 0x0A, 0x1B, 0x23, 0x47},
2155c439937SZhu Ning 	{72, 16000, 1152000, 0x20, 0x00, 0x05, 0x75, 0x0A, 0x1B, 0x23, 0x47},
2165c439937SZhu Ning 	{96, 8000, 768000, 0x60, 0x02, 0x1D, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
2175c439937SZhu Ning 	{96, 16000, 1536000, 0x20, 0x02, 0x0D, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
2185c439937SZhu Ning 	{100, 48000, 4800000, 0x04, 0x04, 0x3F, 0x6D, 0x38, 0x08, 0x4f, 0x1f},
2195c439937SZhu Ning 	{125, 48000, 6000000, 0x04, 0x04, 0x1F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
2205c439937SZhu Ning 	{128, 8000, 1024000, 0x60, 0x00, 0x13, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
2215c439937SZhu Ning 	{128, 16000, 2048000, 0x20, 0x00, 0x11, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
2225c439937SZhu Ning 
2235c439937SZhu Ning 	{128, 44100, 5644800, 0x00, 0x00, 0x01, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2245c439937SZhu Ning 	{128, 48000, 6144000, 0x00, 0x00, 0x01, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2255c439937SZhu Ning 	{144, 8000, 1152000, 0x20, 0x00, 0x03, 0x35, 0x0A, 0x1B, 0x23, 0x47},
2265c439937SZhu Ning 	{144, 16000, 2304000, 0x20, 0x00, 0x11, 0x35, 0x0A, 0x1B, 0x23, 0x47},
2275c439937SZhu Ning 	{192, 8000, 1536000, 0x60, 0x02, 0x0D, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
2285c439937SZhu Ning 	{192, 16000, 3072000, 0x20, 0x02, 0x05, 0x75, 0x0A, 0x1B, 0x1F, 0x3F},
2295c439937SZhu Ning 	{200, 48000, 9600000, 0x04, 0x04, 0x0F, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2305c439937SZhu Ning 	{250, 48000, 12000000, 0x04, 0x04, 0x0F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
2315c439937SZhu Ning 	{256, 8000, 2048000, 0x60, 0x00, 0x11, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
2325c439937SZhu Ning 	{256, 16000, 4096000, 0x20, 0x00, 0x01, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
2335c439937SZhu Ning 
2345c439937SZhu Ning 	{256, 44100, 11289600, 0x00, 0x00, 0x10, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2355c439937SZhu Ning 	{256, 48000, 12288000, 0x00, 0x00, 0x30, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2365c439937SZhu Ning 	{288, 8000, 2304000, 0x20, 0x00, 0x01, 0x35, 0x0A, 0x1B, 0x23, 0x47},
2375c439937SZhu Ning 	{384, 8000, 3072000, 0x60, 0x02, 0x05, 0x75, 0x0A, 0x1B, 0x1F, 0x7F},
2385c439937SZhu Ning 	{384, 16000, 6144000, 0x20, 0x02, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
2395c439937SZhu Ning 	{384, 48000, 18432000, 0x00, 0x02, 0x01, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2405c439937SZhu Ning 	{400, 48000, 19200000, 0x09, 0x04, 0x0f, 0x6d, 0x3a, 0x0A, 0x4F, 0x1F},
2415c439937SZhu Ning 	{500, 48000, 24000000, 0x18, 0x04, 0x1F, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2425c439937SZhu Ning 	{512, 8000, 4096000, 0x60, 0x00, 0x01, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
2435c439937SZhu Ning 	{512, 16000, 8192000, 0x20, 0x00, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
2445c439937SZhu Ning 
2455c439937SZhu Ning 	{512, 44100, 22579200, 0x00, 0x00, 0x00, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2465c439937SZhu Ning 	{512, 48000, 24576000, 0x00, 0x00, 0x00, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2475c439937SZhu Ning 	{768, 8000, 6144000, 0x60, 0x02, 0x11, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
2485c439937SZhu Ning 	{768, 16000, 12288000, 0x20, 0x02, 0x01, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
2495c439937SZhu Ning 	{800, 48000, 38400000, 0x00, 0x18, 0x13, 0x2D, 0x0A, 0x0A, 0x1F, 0x1F},
2505c439937SZhu Ning 	{1024, 8000, 8192000, 0x60, 0x00, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
2515c439937SZhu Ning 	{1024, 16000, 16384000, 0x20, 0x00, 0x00, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
2525c439937SZhu Ning 	{1152, 16000, 18432000, 0x20, 0x08, 0x11, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
2535c439937SZhu Ning 	{1536, 8000, 12288000, 0x60, 0x02, 0x01, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
2545c439937SZhu Ning 
2555c439937SZhu Ning 	{1536, 16000, 24576000, 0x20, 0x02, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x3F},
2565c439937SZhu Ning 	{1625, 8000, 13000000, 0x0C, 0x18, 0x1F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
2575c439937SZhu Ning 	{1625, 16000, 26000000, 0x0C, 0x18, 0x1F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
2585c439937SZhu Ning 	{2048, 8000, 16384000, 0x60, 0x00, 0x00, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
2595c439937SZhu Ning 	{2304, 8000, 18432000, 0x40, 0x02, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x5F},
2605c439937SZhu Ning 	{3072, 8000, 24576000, 0x60, 0x02, 0x10, 0x35, 0x0A, 0x1B, 0x1F, 0x7F},
2615c439937SZhu Ning 	{3250, 8000, 26000000, 0x0C, 0x18, 0x0F, 0x2D, 0x0A, 0x0A, 0x27, 0x27},
2625c439937SZhu Ning 
2635c439937SZhu Ning };
2645c439937SZhu Ning 
2655c439937SZhu Ning static inline int get_coeff(int mclk, int rate)
2665c439937SZhu Ning {
2675c439937SZhu Ning 	int i;
2685c439937SZhu Ning 
2695c439937SZhu Ning 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
2705c439937SZhu Ning 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
2715c439937SZhu Ning 			return i;
2725c439937SZhu Ning 	}
2735c439937SZhu Ning 
2745c439937SZhu Ning 	return -EINVAL;
2755c439937SZhu Ning }
2765c439937SZhu Ning 
2775c439937SZhu Ning static int es8326_set_dai_sysclk(struct snd_soc_dai *codec_dai,
2785c439937SZhu Ning 				 int clk_id, unsigned int freq, int dir)
2795c439937SZhu Ning {
2805c439937SZhu Ning 	struct snd_soc_component *codec = codec_dai->component;
2815c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
2825c439937SZhu Ning 
2835c439937SZhu Ning 	es8326->sysclk = freq;
2845c439937SZhu Ning 
2855c439937SZhu Ning 	return 0;
2865c439937SZhu Ning }
2875c439937SZhu Ning 
2885c439937SZhu Ning static int es8326_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
2895c439937SZhu Ning {
2905c439937SZhu Ning 	struct snd_soc_component *component = codec_dai->component;
2915c439937SZhu Ning 	u8 iface = 0;
2925c439937SZhu Ning 
2935c439937SZhu Ning 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
2945c439937SZhu Ning 	case SND_SOC_DAIFMT_CBC_CFP:
2955c439937SZhu Ning 		snd_soc_component_update_bits(component, ES8326_RESET,
2965c439937SZhu Ning 					      ES8326_MASTER_MODE_EN, ES8326_MASTER_MODE_EN);
2975c439937SZhu Ning 		break;
2985c439937SZhu Ning 	case SND_SOC_DAIFMT_CBC_CFC:
2995c439937SZhu Ning 		break;
3005c439937SZhu Ning 	default:
3015c439937SZhu Ning 		return -EINVAL;
3025c439937SZhu Ning 	}
3035c439937SZhu Ning 
3045c439937SZhu Ning 	/* interface format */
3055c439937SZhu Ning 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
3065c439937SZhu Ning 	case SND_SOC_DAIFMT_I2S:
3075c439937SZhu Ning 		break;
3085c439937SZhu Ning 	case SND_SOC_DAIFMT_RIGHT_J:
3095c439937SZhu Ning 		dev_err(component->dev, "Codec driver does not support right justified\n");
3105c439937SZhu Ning 		return -EINVAL;
3115c439937SZhu Ning 	case SND_SOC_DAIFMT_LEFT_J:
3125c439937SZhu Ning 		iface |= ES8326_DAIFMT_LEFT_J;
3135c439937SZhu Ning 		break;
3145c439937SZhu Ning 	case SND_SOC_DAIFMT_DSP_A:
3155c439937SZhu Ning 		iface |= ES8326_DAIFMT_DSP_A;
3165c439937SZhu Ning 		break;
3175c439937SZhu Ning 	case SND_SOC_DAIFMT_DSP_B:
3185c439937SZhu Ning 		iface |= ES8326_DAIFMT_DSP_B;
3195c439937SZhu Ning 		break;
3205c439937SZhu Ning 	default:
3215c439937SZhu Ning 		return -EINVAL;
3225c439937SZhu Ning 	}
3235c439937SZhu Ning 
3245c439937SZhu Ning 	snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DAIFMT_MASK, iface);
3255c439937SZhu Ning 
3265c439937SZhu Ning 	return 0;
3275c439937SZhu Ning }
3285c439937SZhu Ning 
3295c439937SZhu Ning static int es8326_pcm_hw_params(struct snd_pcm_substream *substream,
3305c439937SZhu Ning 				struct snd_pcm_hw_params *params,
3315c439937SZhu Ning 				struct snd_soc_dai *dai)
3325c439937SZhu Ning {
3335c439937SZhu Ning 	struct snd_soc_component *component = dai->component;
3345c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
3355c439937SZhu Ning 	u8 srate = 0;
3365c439937SZhu Ning 	int coeff;
3375c439937SZhu Ning 
3385c439937SZhu Ning 	coeff = get_coeff(es8326->sysclk, params_rate(params));
3395c439937SZhu Ning 	/* bit size */
3405c439937SZhu Ning 	switch (params_format(params)) {
3415c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S16_LE:
3425c439937SZhu Ning 		srate |= ES8326_S16_LE;
3435c439937SZhu Ning 		break;
3445c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S20_3LE:
3455c439937SZhu Ning 		srate |= ES8326_S20_3_LE;
3465c439937SZhu Ning 		break;
3475c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S18_3LE:
3485c439937SZhu Ning 		srate |= ES8326_S18_LE;
3495c439937SZhu Ning 		break;
3505c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S24_LE:
3515c439937SZhu Ning 		srate |= ES8326_S24_LE;
3525c439937SZhu Ning 		break;
3535c439937SZhu Ning 	case SNDRV_PCM_FORMAT_S32_LE:
3545c439937SZhu Ning 		srate |= ES8326_S32_LE;
3555c439937SZhu Ning 		break;
3565c439937SZhu Ning 	default:
3575c439937SZhu Ning 		return -EINVAL;
3585c439937SZhu Ning 	}
3595c439937SZhu Ning 
3605c439937SZhu Ning 	/* set iface & srate */
3615c439937SZhu Ning 	snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DATA_LEN_MASK, srate);
3625c439937SZhu Ning 
3635c439937SZhu Ning 	if (coeff >= 0) {
3645c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DIV1,
3655c439937SZhu Ning 			     coeff_div[coeff].reg4);
3665c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DIV2,
3675c439937SZhu Ning 			     coeff_div[coeff].reg5);
3685c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DLL,
3695c439937SZhu Ning 			     coeff_div[coeff].reg6);
3705c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_MUX,
3715c439937SZhu Ning 			     coeff_div[coeff].reg7);
3725c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_ADC_SEL,
3735c439937SZhu Ning 			     coeff_div[coeff].reg8);
3745c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DAC_SEL,
3755c439937SZhu Ning 			     coeff_div[coeff].reg9);
3765c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_ADC_OSR,
3775c439937SZhu Ning 			     coeff_div[coeff].rega);
3785c439937SZhu Ning 		regmap_write(es8326->regmap,  ES8326_CLK_DAC_OSR,
3795c439937SZhu Ning 			     coeff_div[coeff].regb);
3805c439937SZhu Ning 	} else {
3815c439937SZhu Ning 		dev_warn(component->dev, "Clock coefficients do not match");
3825c439937SZhu Ning 	}
3835c439937SZhu Ning 
3845c439937SZhu Ning 	return 0;
3855c439937SZhu Ning }
3865c439937SZhu Ning 
387083912c2SZhu Ning static int es8326_mute(struct snd_soc_dai *dai, int mute, int direction)
388083912c2SZhu Ning {
389083912c2SZhu Ning 	struct snd_soc_component *component = dai->component;
390083912c2SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
391*0663286eSZhu Ning 	unsigned int offset_l, offset_r;
392083912c2SZhu Ning 
393083912c2SZhu Ning 	if (mute) {
394083912c2SZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
395083912c2SZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE,
396083912c2SZhu Ning 				ES8326_MUTE_MASK, ES8326_MUTE);
397083912c2SZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xf0);
398083912c2SZhu Ning 	} else {
399083912c2SZhu Ning 		if (!es8326->calibrated) {
400083912c2SZhu Ning 			regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_FORCE_CAL);
401083912c2SZhu Ning 			msleep(30);
402*0663286eSZhu Ning 			regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
403*0663286eSZhu Ning 			regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l);
404*0663286eSZhu Ning 			regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r);
405*0663286eSZhu Ning 			regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
406*0663286eSZhu Ning 			regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l);
407*0663286eSZhu Ning 			regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r);
408083912c2SZhu Ning 			es8326->calibrated = true;
409083912c2SZhu Ning 		}
410083912c2SZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa0);
411*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_VOL, 0x80);
412083912c2SZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_ON);
413083912c2SZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE,
414083912c2SZhu Ning 				ES8326_MUTE_MASK, ~(ES8326_MUTE));
415083912c2SZhu Ning 	}
416083912c2SZhu Ning 	return 0;
417083912c2SZhu Ning }
418083912c2SZhu Ning 
4195c439937SZhu Ning static int es8326_set_bias_level(struct snd_soc_component *codec,
4205c439937SZhu Ning 				 enum snd_soc_bias_level level)
4215c439937SZhu Ning {
4225c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec);
4235c439937SZhu Ning 	int ret;
4245c439937SZhu Ning 
4255c439937SZhu Ning 	switch (level) {
4265c439937SZhu Ning 	case SND_SOC_BIAS_ON:
4275c439937SZhu Ning 		ret = clk_prepare_enable(es8326->mclk);
4285c439937SZhu Ning 		if (ret)
4295c439937SZhu Ning 			return ret;
430*0663286eSZhu Ning 
431*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_RESET, 0x9f);
432*0663286eSZhu Ning 		msleep(20);
433*0663286eSZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_DAC_DSM, 0x01, 0x00);
434*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_INTOUT_IO, es8326->interrupt_clk);
4355c439937SZhu Ning 		regmap_write(es8326->regmap, ES8326_SDINOUT1_IO,
4365c439937SZhu Ning 			    (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT));
437*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x0E);
4385c439937SZhu Ning 		regmap_write(es8326->regmap, ES8326_PGA_PDN, 0x40);
439*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x00);
440*0663286eSZhu Ning 		regmap_update_bits(es8326->regmap,  ES8326_CLK_CTL, 0x20, 0x20);
4415c439937SZhu Ning 		regmap_write(es8326->regmap, ES8326_RESET, ES8326_CSM_ON);
4425c439937SZhu Ning 		break;
4435c439937SZhu Ning 	case SND_SOC_BIAS_PREPARE:
4445c439937SZhu Ning 		break;
4455c439937SZhu Ning 	case SND_SOC_BIAS_STANDBY:
4465c439937SZhu Ning 		break;
4475c439937SZhu Ning 	case SND_SOC_BIAS_OFF:
4485c439937SZhu Ning 		clk_disable_unprepare(es8326->mclk);
449*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x3b);
4505c439937SZhu Ning 		regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x00);
451*0663286eSZhu Ning 		regmap_update_bits(es8326->regmap, ES8326_CLK_CTL, 0x20, 0x00);
4525c439937SZhu Ning 		regmap_write(es8326->regmap, ES8326_SDINOUT1_IO, ES8326_IO_INPUT);
4535c439937SZhu Ning 		break;
4545c439937SZhu Ning 	}
4555c439937SZhu Ning 
4565c439937SZhu Ning 	return 0;
4575c439937SZhu Ning }
4585c439937SZhu Ning 
4595c439937SZhu Ning #define es8326_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
4605c439937SZhu Ning 	SNDRV_PCM_FMTBIT_S24_LE)
4615c439937SZhu Ning 
4625c439937SZhu Ning static const struct snd_soc_dai_ops es8326_ops = {
4635c439937SZhu Ning 	.hw_params = es8326_pcm_hw_params,
4645c439937SZhu Ning 	.set_fmt = es8326_set_dai_fmt,
4655c439937SZhu Ning 	.set_sysclk = es8326_set_dai_sysclk,
466083912c2SZhu Ning 	.mute_stream = es8326_mute,
467083912c2SZhu Ning 	.no_capture_mute = 1,
4685c439937SZhu Ning };
4695c439937SZhu Ning 
4705c439937SZhu Ning static struct snd_soc_dai_driver es8326_dai = {
4715c439937SZhu Ning 	.name = "ES8326 HiFi",
4725c439937SZhu Ning 	.playback = {
4735c439937SZhu Ning 		.stream_name = "Playback",
4745c439937SZhu Ning 		.channels_min = 1,
4755c439937SZhu Ning 		.channels_max = 2,
4765c439937SZhu Ning 		.rates = SNDRV_PCM_RATE_8000_48000,
4775c439937SZhu Ning 		.formats = es8326_FORMATS,
4785c439937SZhu Ning 		},
4795c439937SZhu Ning 	.capture = {
4805c439937SZhu Ning 		.stream_name = "Capture",
4815c439937SZhu Ning 		.channels_min = 1,
4825c439937SZhu Ning 		.channels_max = 2,
4835c439937SZhu Ning 		.rates = SNDRV_PCM_RATE_8000_48000,
4845c439937SZhu Ning 		.formats = es8326_FORMATS,
4855c439937SZhu Ning 		},
4865c439937SZhu Ning 	.ops = &es8326_ops,
4875c439937SZhu Ning 	.symmetric_rate = 1,
4885c439937SZhu Ning };
4895c439937SZhu Ning 
4905c439937SZhu Ning static void es8326_enable_micbias(struct snd_soc_component *component)
4915c439937SZhu Ning {
4925c439937SZhu Ning 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
4935c439937SZhu Ning 
4945c439937SZhu Ning 	snd_soc_dapm_mutex_lock(dapm);
4955c439937SZhu Ning 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS1");
4965c439937SZhu Ning 	snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS2");
4975c439937SZhu Ning 	snd_soc_dapm_sync_unlocked(dapm);
4985c439937SZhu Ning 	snd_soc_dapm_mutex_unlock(dapm);
4995c439937SZhu Ning }
5005c439937SZhu Ning 
5015c439937SZhu Ning static void es8326_disable_micbias(struct snd_soc_component *component)
5025c439937SZhu Ning {
5035c439937SZhu Ning 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
5045c439937SZhu Ning 
5055c439937SZhu Ning 	snd_soc_dapm_mutex_lock(dapm);
5065c439937SZhu Ning 	snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS1");
5075c439937SZhu Ning 	snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS2");
5085c439937SZhu Ning 	snd_soc_dapm_sync_unlocked(dapm);
5095c439937SZhu Ning 	snd_soc_dapm_mutex_unlock(dapm);
5105c439937SZhu Ning }
5115c439937SZhu Ning 
5125c439937SZhu Ning /*
5135c439937SZhu Ning  *	For button detection, set the following in soundcard
5145c439937SZhu Ning  *	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
5155c439937SZhu Ning  *	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
5165c439937SZhu Ning  *	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
5175c439937SZhu Ning  */
5185c439937SZhu Ning static void es8326_jack_button_handler(struct work_struct *work)
5195c439937SZhu Ning {
5205c439937SZhu Ning 	struct es8326_priv *es8326 =
5215c439937SZhu Ning 		container_of(work, struct es8326_priv, button_press_work.work);
5225c439937SZhu Ning 	struct snd_soc_component *comp = es8326->component;
5235c439937SZhu Ning 	unsigned int iface;
5245c439937SZhu Ning 	static int button_to_report, press_count;
5255c439937SZhu Ning 	static int prev_button, cur_button;
5265c439937SZhu Ning 
5275c439937SZhu Ning 	if (!(es8326->jack->status & SND_JACK_HEADSET)) /* Jack unplugged */
5285c439937SZhu Ning 		return;
5295c439937SZhu Ning 
5305c439937SZhu Ning 	mutex_lock(&es8326->lock);
5314ddad00cSZhu Ning 	iface = snd_soc_component_read(comp, ES8326_HPDET_STA);
5325c439937SZhu Ning 	switch (iface) {
5335c439937SZhu Ning 	case 0x93:
5345c439937SZhu Ning 		/* pause button detected */
5355c439937SZhu Ning 		cur_button = SND_JACK_BTN_0;
5365c439937SZhu Ning 		break;
5375c439937SZhu Ning 	case 0x6f:
5385c439937SZhu Ning 		/* button volume up */
5395c439937SZhu Ning 		cur_button = SND_JACK_BTN_1;
5405c439937SZhu Ning 		break;
5415c439937SZhu Ning 	case 0x27:
5425c439937SZhu Ning 		/* button volume down */
5435c439937SZhu Ning 		cur_button = SND_JACK_BTN_2;
5445c439937SZhu Ning 		break;
5455c439937SZhu Ning 	case 0x1e:
5465c439937SZhu Ning 		/* button released or not pressed */
5475c439937SZhu Ning 		cur_button = 0;
5485c439937SZhu Ning 		break;
5495c439937SZhu Ning 	default:
5505c439937SZhu Ning 		break;
5515c439937SZhu Ning 	}
5525c439937SZhu Ning 
5535c439937SZhu Ning 	if ((prev_button == cur_button) && (cur_button != 0)) {
5545c439937SZhu Ning 		press_count++;
5555c439937SZhu Ning 		if (press_count > 10) {
5565c439937SZhu Ning 			/* report a press every 500ms */
5575c439937SZhu Ning 			snd_soc_jack_report(es8326->jack, cur_button,
5585c439937SZhu Ning 					SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
5595c439937SZhu Ning 			press_count = 0;
5605c439937SZhu Ning 		}
5615c439937SZhu Ning 		button_to_report = cur_button;
5625c439937SZhu Ning 		queue_delayed_work(system_wq, &es8326->button_press_work,
5635c439937SZhu Ning 				   msecs_to_jiffies(50));
5645c439937SZhu Ning 	} else if (prev_button != cur_button) {
5655c439937SZhu Ning 		/* mismatch, detect again */
5665c439937SZhu Ning 		prev_button = cur_button;
5675c439937SZhu Ning 		queue_delayed_work(system_wq, &es8326->button_press_work,
5685c439937SZhu Ning 				   msecs_to_jiffies(50));
5695c439937SZhu Ning 	} else {
5705c439937SZhu Ning 		/* released or no pressed */
5715c439937SZhu Ning 		if (button_to_report != 0) {
5725c439937SZhu Ning 			snd_soc_jack_report(es8326->jack, button_to_report,
5735c439937SZhu Ning 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
5745c439937SZhu Ning 			snd_soc_jack_report(es8326->jack, 0,
5755c439937SZhu Ning 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2);
5765c439937SZhu Ning 			button_to_report = 0;
5775c439937SZhu Ning 		}
5785c439937SZhu Ning 	}
5795c439937SZhu Ning 	mutex_unlock(&es8326->lock);
5805c439937SZhu Ning }
5815c439937SZhu Ning 
5825c439937SZhu Ning static void es8326_jack_detect_handler(struct work_struct *work)
5835c439937SZhu Ning {
5845c439937SZhu Ning 	struct es8326_priv *es8326 =
5855c439937SZhu Ning 		container_of(work, struct es8326_priv, jack_detect_work.work);
5865c439937SZhu Ning 	struct snd_soc_component *comp = es8326->component;
5875c439937SZhu Ning 	unsigned int iface;
5885c439937SZhu Ning 
5895c439937SZhu Ning 	mutex_lock(&es8326->lock);
5904ddad00cSZhu Ning 	iface = snd_soc_component_read(comp, ES8326_HPDET_STA);
5915c439937SZhu Ning 	dev_dbg(comp->dev, "gpio flag %#04x", iface);
5925c439937SZhu Ning 	if ((iface & ES8326_HPINSERT_FLAG) == 0) {
5935c439937SZhu Ning 		/* Jack unplugged or spurious IRQ */
5945c439937SZhu Ning 		dev_dbg(comp->dev, "No headset detected");
5955c439937SZhu Ning 		if (es8326->jack->status & SND_JACK_HEADPHONE) {
5965c439937SZhu Ning 			snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
5975c439937SZhu Ning 			snd_soc_component_write(comp, ES8326_ADC1_SRC, es8326->mic2_src);
5985c439937SZhu Ning 			es8326_disable_micbias(comp);
5995c439937SZhu Ning 		}
6005c439937SZhu Ning 	} else if ((iface & ES8326_HPINSERT_FLAG) == ES8326_HPINSERT_FLAG) {
6015c439937SZhu Ning 		if (es8326->jack->status & SND_JACK_HEADSET) {
6025c439937SZhu Ning 			/* detect button */
6035c439937SZhu Ning 			queue_delayed_work(system_wq, &es8326->button_press_work, 10);
6045c439937SZhu Ning 		} else {
6055c439937SZhu Ning 			if ((iface & ES8326_HPBUTTON_FLAG) == 0x00) {
6065c439937SZhu Ning 				dev_dbg(comp->dev, "Headset detected");
6075c439937SZhu Ning 				snd_soc_jack_report(es8326->jack,
6085c439937SZhu Ning 						    SND_JACK_HEADSET, SND_JACK_HEADSET);
6095c439937SZhu Ning 				snd_soc_component_write(comp,
6105c439937SZhu Ning 							ES8326_ADC1_SRC, es8326->mic1_src);
6115c439937SZhu Ning 			} else {
6125c439937SZhu Ning 				dev_dbg(comp->dev, "Headphone detected");
6135c439937SZhu Ning 				snd_soc_jack_report(es8326->jack,
6145c439937SZhu Ning 						    SND_JACK_HEADPHONE, SND_JACK_HEADSET);
6155c439937SZhu Ning 			}
6165c439937SZhu Ning 		}
6175c439937SZhu Ning 	}
6185c439937SZhu Ning 	mutex_unlock(&es8326->lock);
6195c439937SZhu Ning }
6205c439937SZhu Ning 
6215c439937SZhu Ning static irqreturn_t es8326_irq(int irq, void *dev_id)
6225c439937SZhu Ning {
6235c439937SZhu Ning 	struct es8326_priv *es8326 = dev_id;
6245c439937SZhu Ning 	struct snd_soc_component *comp = es8326->component;
6255c439937SZhu Ning 
6265c439937SZhu Ning 	if (!es8326->jack)
6275c439937SZhu Ning 		goto out;
6285c439937SZhu Ning 
6295c439937SZhu Ning 	es8326_enable_micbias(comp);
6305c439937SZhu Ning 
6315c439937SZhu Ning 	if (es8326->jack->status & SND_JACK_HEADSET)
6325c439937SZhu Ning 		queue_delayed_work(system_wq, &es8326->jack_detect_work,
6335c439937SZhu Ning 				   msecs_to_jiffies(10));
6345c439937SZhu Ning 	else
6355c439937SZhu Ning 		queue_delayed_work(system_wq, &es8326->jack_detect_work,
6365c439937SZhu Ning 				   msecs_to_jiffies(300));
6375c439937SZhu Ning 
6385c439937SZhu Ning out:
6395c439937SZhu Ning 	return IRQ_HANDLED;
6405c439937SZhu Ning }
6415c439937SZhu Ning 
642*0663286eSZhu Ning static int es8326_calibrate(struct snd_soc_component *component)
643*0663286eSZhu Ning {
644*0663286eSZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
645*0663286eSZhu Ning 	unsigned int reg;
646*0663286eSZhu Ning 	unsigned int offset_l, offset_r;
647*0663286eSZhu Ning 
648*0663286eSZhu Ning 	regmap_read(es8326->regmap, ES8326_CHIP_VERSION, &reg);
649*0663286eSZhu Ning 	es8326->version = reg;
650*0663286eSZhu Ning 
651*0663286eSZhu Ning 	if ((es8326->version == ES8326_VERSION_B) && (es8326->calibrated == false)) {
652*0663286eSZhu Ning 		dev_dbg(component->dev, "ES8326_VERSION_B, calibrating\n");
653*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_INV, 0xc0);
654*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_DIV1, 0x01);
655*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_DLL, 0x30);
656*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_MUX, 0xed);
657*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_TRI, 0xc1);
658*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_DAC_MUTE, 0x03);
659*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7f);
660*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x33);
661*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_DAC2HPMIX, 0x88);
662*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_VOL, 0x80);
663*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
664*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_RESET, 0xc0);
665*0663286eSZhu Ning 		usleep_range(15000, 20000);
666*0663286eSZhu Ning 
667*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, ES8326_HP_OFF);
668*0663286eSZhu Ning 		regmap_read(es8326->regmap, ES8326_CSM_MUTE_STA, &reg);
669*0663286eSZhu Ning 		if ((reg & 0xf0) != 0x40)
670*0663286eSZhu Ning 			msleep(50);
671*0663286eSZhu Ning 
672*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_CAL, 0xd4);
673*0663286eSZhu Ning 		msleep(200);
674*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_CAL, 0x4d);
675*0663286eSZhu Ning 		msleep(200);
676*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF);
677*0663286eSZhu Ning 		regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l);
678*0663286eSZhu Ning 		regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r);
679*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c);
680*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l);
681*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r);
682*0663286eSZhu Ning 		regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00);
683*0663286eSZhu Ning 
684*0663286eSZhu Ning 		es8326->calibrated = true;
685*0663286eSZhu Ning 	}
686*0663286eSZhu Ning 
687*0663286eSZhu Ning 	return 0;
688*0663286eSZhu Ning }
689*0663286eSZhu Ning 
6905c439937SZhu Ning static int es8326_resume(struct snd_soc_component *component)
6915c439937SZhu Ning {
6925c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
6935c439937SZhu Ning 
6945c439937SZhu Ning 	regcache_cache_only(es8326->regmap, false);
6955c439937SZhu Ning 	regcache_sync(es8326->regmap);
6965c439937SZhu Ning 
697ac20a73dSZhu Ning 	/* reset internal clock state */
698ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_RESET, 0x1f);
699ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x0E);
700ac20a73dSZhu Ning 	usleep_range(10000, 15000);
7015c439937SZhu Ning 	regmap_write(es8326->regmap, ES8326_HPJACK_TIMER, 0x88);
702ac20a73dSZhu Ning 	/* set headphone default type and detect pin */
703ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x81);
704ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_RESAMPLE, 0x05);
7055c439937SZhu Ning 
706ac20a73dSZhu Ning 	/* set internal oscillator as clock source of headpone cp */
707ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_DIV_CPC, 0x84);
708ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_ON);
709ac20a73dSZhu Ning 	/* clock manager reset release */
710ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_RESET, 0x17);
711ac20a73dSZhu Ning 	/* set headphone detection as half scan mode */
712ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_MISC, 0x08);
713ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_PULLUP_CTL, 0x00);
714ac20a73dSZhu Ning 
715ac20a73dSZhu Ning 	/* enable headphone driver */
716ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa7);
717ac20a73dSZhu Ning 	usleep_range(2000, 5000);
718ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0xab);
719ac20a73dSZhu Ning 	usleep_range(2000, 5000);
720ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0xbb);
721ac20a73dSZhu Ning 	usleep_range(2000, 5000);
722ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa1);
723ac20a73dSZhu Ning 
724ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00);
725ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_VMIDS1, 0xc4);
726ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_VMIDS2, 0x81);
727ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_CAL_TIME, 0x00);
728*0663286eSZhu Ning 	/* calibrate for B version */
729*0663286eSZhu Ning 	es8326_calibrate(component);
730ac20a73dSZhu Ning 	/* turn off headphone out */
731ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HP_CAL, 0x00);
732ac20a73dSZhu Ning 	/* set ADC and DAC in low power mode */
733ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_ANA_LP, 0xf0);
734ac20a73dSZhu Ning 
735ac20a73dSZhu Ning 	/* force micbias on */
736ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_ANA_MICBIAS, 0x4f);
737ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_SYS_BIAS, 0x08);
7385c439937SZhu Ning 	regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7F);
739ac20a73dSZhu Ning 	/* select vdda as micbias source */
740ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x23);
741ac20a73dSZhu Ning 	/* set dac dsmclip = 1 */
742ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_DAC_DSM, 0x08);
743ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_DAC_VPPSCALE, 0x15);
744ac20a73dSZhu Ning 
745ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_INT_SOURCE,
746ac20a73dSZhu Ning 		    (ES8326_INT_SRC_PIN9 | ES8326_INT_SRC_BUTTON));
747ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_INTOUT_IO,
748ac20a73dSZhu Ning 		     es8326->interrupt_clk);
749ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_SDINOUT1_IO,
750ac20a73dSZhu Ning 		    (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT));
751ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_SDINOUT23_IO, ES8326_IO_INPUT);
752ac20a73dSZhu Ning 
753ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x3b);
754ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_RESET, ES8326_CSM_ON);
755ac20a73dSZhu Ning 	regmap_update_bits(es8326->regmap, ES8326_PGAGAIN, ES8326_MIC_SEL_MASK,
756ac20a73dSZhu Ning 			   ES8326_MIC1_SEL);
757ac20a73dSZhu Ning 
758ac20a73dSZhu Ning 	regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE, ES8326_MUTE_MASK,
759ac20a73dSZhu Ning 			   ES8326_MUTE);
760ac20a73dSZhu Ning 
761ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x80 |
762ac20a73dSZhu Ning 			((es8326->version == ES8326_VERSION_B) ?
763ac20a73dSZhu Ning 			(ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol) :
764ac20a73dSZhu Ning 			(ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol | 0x04)));
7655c439937SZhu Ning 
7665c439937SZhu Ning 	es8326_irq(es8326->irq, es8326);
7675c439937SZhu Ning 	return 0;
7685c439937SZhu Ning }
7695c439937SZhu Ning 
7705c439937SZhu Ning static int es8326_suspend(struct snd_soc_component *component)
7715c439937SZhu Ning {
7725c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
7735c439937SZhu Ning 
7745c439937SZhu Ning 	cancel_delayed_work_sync(&es8326->jack_detect_work);
7755c439937SZhu Ning 	es8326_disable_micbias(component);
776083912c2SZhu Ning 	es8326->calibrated = false;
7775c439937SZhu Ning 	regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_OFF);
7785c439937SZhu Ning 	regcache_cache_only(es8326->regmap, true);
7795c439937SZhu Ning 	regcache_mark_dirty(es8326->regmap);
7805c439937SZhu Ning 
781ac20a73dSZhu Ning 	/* reset register value to default */
782ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x01);
783ac20a73dSZhu Ning 	usleep_range(1000, 3000);
784ac20a73dSZhu Ning 	regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x00);
7855c439937SZhu Ning 	return 0;
7865c439937SZhu Ning }
7875c439937SZhu Ning 
7885c439937SZhu Ning static int es8326_probe(struct snd_soc_component *component)
7895c439937SZhu Ning {
7905c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
7915c439937SZhu Ning 	int ret;
7925c439937SZhu Ning 
7935c439937SZhu Ning 	es8326->component = component;
7945c439937SZhu Ning 	es8326->jd_inverted = device_property_read_bool(component->dev,
7955c439937SZhu Ning 							"everest,jack-detect-inverted");
7965c439937SZhu Ning 
7975c439937SZhu Ning 	ret = device_property_read_u8(component->dev, "everest,mic1-src", &es8326->mic1_src);
7985c439937SZhu Ning 	if (ret != 0) {
7995c439937SZhu Ning 		dev_dbg(component->dev, "mic1-src return %d", ret);
8005c439937SZhu Ning 		es8326->mic1_src = ES8326_ADC_AMIC;
8015c439937SZhu Ning 	}
8025c439937SZhu Ning 	dev_dbg(component->dev, "mic1-src %x", es8326->mic1_src);
8035c439937SZhu Ning 
8045c439937SZhu Ning 	ret = device_property_read_u8(component->dev, "everest,mic2-src", &es8326->mic2_src);
8055c439937SZhu Ning 	if (ret != 0) {
8065c439937SZhu Ning 		dev_dbg(component->dev, "mic2-src return %d", ret);
8075c439937SZhu Ning 		es8326->mic2_src = ES8326_ADC_DMIC;
8085c439937SZhu Ning 	}
8095c439937SZhu Ning 	dev_dbg(component->dev, "mic2-src %x", es8326->mic2_src);
8105c439937SZhu Ning 
8115c439937SZhu Ning 	ret = device_property_read_u8(component->dev, "everest,jack-pol", &es8326->jack_pol);
8125c439937SZhu Ning 	if (ret != 0) {
8135c439937SZhu Ning 		dev_dbg(component->dev, "jack-pol return %d", ret);
814ac20a73dSZhu Ning 		es8326->jack_pol = ES8326_HP_TYPE_AUTO;
8155c439937SZhu Ning 	}
8165c439937SZhu Ning 	dev_dbg(component->dev, "jack-pol %x", es8326->jack_pol);
8175c439937SZhu Ning 
818fe1e7e8cSAlexey Firago 	ret = device_property_read_u8(component->dev, "everest,interrupt-src",
819fe1e7e8cSAlexey Firago 				      &es8326->interrupt_src);
8205c439937SZhu Ning 	if (ret != 0) {
8215c439937SZhu Ning 		dev_dbg(component->dev, "interrupt-src return %d", ret);
8225c439937SZhu Ning 		es8326->interrupt_src = ES8326_HP_DET_SRC_PIN9;
8235c439937SZhu Ning 	}
8245c439937SZhu Ning 	dev_dbg(component->dev, "interrupt-src %x", es8326->interrupt_src);
8255c439937SZhu Ning 
826fe1e7e8cSAlexey Firago 	ret = device_property_read_u8(component->dev, "everest,interrupt-clk",
827fe1e7e8cSAlexey Firago 				      &es8326->interrupt_clk);
8285c439937SZhu Ning 	if (ret != 0) {
8295c439937SZhu Ning 		dev_dbg(component->dev, "interrupt-clk return %d", ret);
8305c439937SZhu Ning 		es8326->interrupt_clk = 0x45;
8315c439937SZhu Ning 	}
8325c439937SZhu Ning 	dev_dbg(component->dev, "interrupt-clk %x", es8326->interrupt_clk);
8335c439937SZhu Ning 
8345c439937SZhu Ning 	es8326_resume(component);
8355c439937SZhu Ning 	return 0;
8365c439937SZhu Ning }
8375c439937SZhu Ning 
8385c439937SZhu Ning static void es8326_enable_jack_detect(struct snd_soc_component *component,
8395c439937SZhu Ning 				struct snd_soc_jack *jack)
8405c439937SZhu Ning {
8415c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
8425c439937SZhu Ning 
8435c439937SZhu Ning 	mutex_lock(&es8326->lock);
8445c439937SZhu Ning 	if (es8326->jd_inverted)
8454ddad00cSZhu Ning 		snd_soc_component_update_bits(component, ES8326_HPDET_TYPE,
8465c439937SZhu Ning 					      ES8326_HP_DET_JACK_POL, ~es8326->jack_pol);
8475c439937SZhu Ning 	es8326->jack = jack;
8485c439937SZhu Ning 
8495c439937SZhu Ning 	mutex_unlock(&es8326->lock);
8505c439937SZhu Ning 	es8326_irq(es8326->irq, es8326);
8515c439937SZhu Ning }
8525c439937SZhu Ning 
8535c439937SZhu Ning static void es8326_disable_jack_detect(struct snd_soc_component *component)
8545c439937SZhu Ning {
8555c439937SZhu Ning 	struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component);
8565c439937SZhu Ning 
8575c439937SZhu Ning 	dev_dbg(component->dev, "Enter into %s\n", __func__);
8585c439937SZhu Ning 	if (!es8326->jack)
8595c439937SZhu Ning 		return; /* Already disabled (or never enabled) */
8605c439937SZhu Ning 	cancel_delayed_work_sync(&es8326->jack_detect_work);
8615c439937SZhu Ning 
8625c439937SZhu Ning 	mutex_lock(&es8326->lock);
8635c439937SZhu Ning 	if (es8326->jack->status & SND_JACK_MICROPHONE) {
8645c439937SZhu Ning 		es8326_disable_micbias(component);
8655c439937SZhu Ning 		snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET);
8665c439937SZhu Ning 	}
8675c439937SZhu Ning 	es8326->jack = NULL;
8685c439937SZhu Ning 	mutex_unlock(&es8326->lock);
8695c439937SZhu Ning }
8705c439937SZhu Ning 
8715c439937SZhu Ning static int es8326_set_jack(struct snd_soc_component *component,
8725c439937SZhu Ning 			struct snd_soc_jack *jack, void *data)
8735c439937SZhu Ning {
8745c439937SZhu Ning 	if (jack)
8755c439937SZhu Ning 		es8326_enable_jack_detect(component, jack);
8765c439937SZhu Ning 	else
8775c439937SZhu Ning 		es8326_disable_jack_detect(component);
8785c439937SZhu Ning 
8795c439937SZhu Ning 	return 0;
8805c439937SZhu Ning }
8815c439937SZhu Ning 
8825c439937SZhu Ning static void es8326_remove(struct snd_soc_component *component)
8835c439937SZhu Ning {
8845c439937SZhu Ning 	es8326_disable_jack_detect(component);
8855c439937SZhu Ning 	es8326_set_bias_level(component, SND_SOC_BIAS_OFF);
8865c439937SZhu Ning }
8875c439937SZhu Ning 
8885c439937SZhu Ning static const struct snd_soc_component_driver soc_component_dev_es8326 = {
8895c439937SZhu Ning 	.probe		= es8326_probe,
8905c439937SZhu Ning 	.remove		= es8326_remove,
8915c439937SZhu Ning 	.resume		= es8326_resume,
8925c439937SZhu Ning 	.suspend	= es8326_suspend,
8935c439937SZhu Ning 	.set_bias_level = es8326_set_bias_level,
8945c439937SZhu Ning 	.set_jack	= es8326_set_jack,
8955c439937SZhu Ning 	.dapm_widgets	= es8326_dapm_widgets,
8965c439937SZhu Ning 	.num_dapm_widgets	= ARRAY_SIZE(es8326_dapm_widgets),
8975c439937SZhu Ning 	.dapm_routes		= es8326_dapm_routes,
8985c439937SZhu Ning 	.num_dapm_routes	= ARRAY_SIZE(es8326_dapm_routes),
8995c439937SZhu Ning 	.controls		= es8326_snd_controls,
9005c439937SZhu Ning 	.num_controls		= ARRAY_SIZE(es8326_snd_controls),
9015c439937SZhu Ning 	.use_pmdown_time	= 1,
9025c439937SZhu Ning 	.endianness		= 1,
9035c439937SZhu Ning };
9045c439937SZhu Ning 
905784252baSUwe Kleine-König static int es8326_i2c_probe(struct i2c_client *i2c)
9065c439937SZhu Ning {
9075c439937SZhu Ning 	struct es8326_priv *es8326;
9085c439937SZhu Ning 	int ret;
9095c439937SZhu Ning 
9105c439937SZhu Ning 	es8326 = devm_kzalloc(&i2c->dev, sizeof(struct es8326_priv), GFP_KERNEL);
9115c439937SZhu Ning 	if (!es8326)
9125c439937SZhu Ning 		return -ENOMEM;
9135c439937SZhu Ning 
9145c439937SZhu Ning 	i2c_set_clientdata(i2c, es8326);
9155c439937SZhu Ning 	es8326->i2c = i2c;
9165c439937SZhu Ning 	mutex_init(&es8326->lock);
9175c439937SZhu Ning 	es8326->regmap = devm_regmap_init_i2c(i2c, &es8326_regmap_config);
9185c439937SZhu Ning 	if (IS_ERR(es8326->regmap)) {
9195c439937SZhu Ning 		ret = PTR_ERR(es8326->regmap);
9205c439937SZhu Ning 		dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
9215c439937SZhu Ning 		return ret;
9225c439937SZhu Ning 	}
9235c439937SZhu Ning 
9245c439937SZhu Ning 	es8326->irq = i2c->irq;
9255c439937SZhu Ning 	INIT_DELAYED_WORK(&es8326->jack_detect_work,
9265c439937SZhu Ning 			  es8326_jack_detect_handler);
9275c439937SZhu Ning 	INIT_DELAYED_WORK(&es8326->button_press_work,
9285c439937SZhu Ning 			  es8326_jack_button_handler);
9295c439937SZhu Ning 	/* ES8316 is level-based while ES8326 is edge-based */
9305c439937SZhu Ning 	ret = devm_request_threaded_irq(&i2c->dev, es8326->irq, NULL, es8326_irq,
9315c439937SZhu Ning 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
9325c439937SZhu Ning 					"es8326", es8326);
9335c439937SZhu Ning 	if (ret) {
9345c439937SZhu Ning 		dev_warn(&i2c->dev, "Failed to request IRQ: %d: %d\n",
9355c439937SZhu Ning 		es8326->irq, ret);
9365c439937SZhu Ning 		es8326->irq = -ENXIO;
9375c439937SZhu Ning 	}
9385c439937SZhu Ning 
9395c439937SZhu Ning 	es8326->mclk = devm_clk_get_optional(&i2c->dev, "mclk");
9405c439937SZhu Ning 	if (IS_ERR(es8326->mclk)) {
9415c439937SZhu Ning 		dev_err(&i2c->dev, "unable to get mclk\n");
9425c439937SZhu Ning 		return PTR_ERR(es8326->mclk);
9435c439937SZhu Ning 	}
9445c439937SZhu Ning 	if (!es8326->mclk)
9455c439937SZhu Ning 		dev_warn(&i2c->dev, "assuming static mclk\n");
9465c439937SZhu Ning 
9475c439937SZhu Ning 	ret = clk_prepare_enable(es8326->mclk);
9485c439937SZhu Ning 	if (ret) {
9495c439937SZhu Ning 		dev_err(&i2c->dev, "unable to enable mclk\n");
9505c439937SZhu Ning 		return ret;
9515c439937SZhu Ning 	}
9525c439937SZhu Ning 	return devm_snd_soc_register_component(&i2c->dev,
9535c439937SZhu Ning 					&soc_component_dev_es8326,
9545c439937SZhu Ning 					&es8326_dai, 1);
9555c439937SZhu Ning }
9565c439937SZhu Ning 
9575c439937SZhu Ning static const struct i2c_device_id es8326_i2c_id[] = {
9585c439937SZhu Ning 	{"es8326", 0 },
9595c439937SZhu Ning 	{}
9605c439937SZhu Ning };
9615c439937SZhu Ning MODULE_DEVICE_TABLE(i2c, es8326_i2c_id);
9625c439937SZhu Ning 
9635c439937SZhu Ning #ifdef CONFIG_OF
9645c439937SZhu Ning static const struct of_device_id es8326_of_match[] = {
9655c439937SZhu Ning 	{ .compatible = "everest,es8326", },
9665c439937SZhu Ning 	{}
9675c439937SZhu Ning };
9685c439937SZhu Ning MODULE_DEVICE_TABLE(of, es8326_of_match);
9695c439937SZhu Ning #endif
9705c439937SZhu Ning 
9715c439937SZhu Ning #ifdef CONFIG_ACPI
9725c439937SZhu Ning static const struct acpi_device_id es8326_acpi_match[] = {
9735c439937SZhu Ning 	{"ESSX8326", 0},
9745c439937SZhu Ning 	{},
9755c439937SZhu Ning };
9765c439937SZhu Ning MODULE_DEVICE_TABLE(acpi, es8326_acpi_match);
9775c439937SZhu Ning #endif
9785c439937SZhu Ning 
9795c439937SZhu Ning static struct i2c_driver es8326_i2c_driver = {
9805c439937SZhu Ning 	.driver = {
9815c439937SZhu Ning 		.name = "es8326",
9825c439937SZhu Ning 		.acpi_match_table = ACPI_PTR(es8326_acpi_match),
9835c439937SZhu Ning 		.of_match_table = of_match_ptr(es8326_of_match),
9845c439937SZhu Ning 	},
9859abcd240SUwe Kleine-König 	.probe = es8326_i2c_probe,
9865c439937SZhu Ning 	.id_table = es8326_i2c_id,
9875c439937SZhu Ning };
9885c439937SZhu Ning module_i2c_driver(es8326_i2c_driver);
9895c439937SZhu Ning 
9905c439937SZhu Ning MODULE_DESCRIPTION("ASoC es8326 driver");
9915c439937SZhu Ning MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
9925c439937SZhu Ning MODULE_LICENSE("GPL");
993