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 48523d242dSZhu Ning static int es8326_crosstalk1_get(struct snd_kcontrol *kcontrol, 49523d242dSZhu Ning struct snd_ctl_elem_value *ucontrol) 50523d242dSZhu Ning { 51523d242dSZhu Ning struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 52523d242dSZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component); 53523d242dSZhu Ning unsigned int crosstalk_h, crosstalk_l; 54523d242dSZhu Ning unsigned int crosstalk; 55523d242dSZhu Ning 56523d242dSZhu Ning regmap_read(es8326->regmap, ES8326_DAC_RAMPRATE, &crosstalk_h); 57523d242dSZhu Ning regmap_read(es8326->regmap, ES8326_DAC_CROSSTALK, &crosstalk_l); 58523d242dSZhu Ning crosstalk_h &= 0x20; 59523d242dSZhu Ning crosstalk_l &= 0xf0; 60523d242dSZhu Ning crosstalk = crosstalk_h >> 1 | crosstalk_l >> 4; 61523d242dSZhu Ning ucontrol->value.integer.value[0] = crosstalk; 62523d242dSZhu Ning 63523d242dSZhu Ning return 0; 64523d242dSZhu Ning } 65523d242dSZhu Ning 66523d242dSZhu Ning static int es8326_crosstalk1_set(struct snd_kcontrol *kcontrol, 67523d242dSZhu Ning struct snd_ctl_elem_value *ucontrol) 68523d242dSZhu Ning { 69523d242dSZhu Ning struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 70523d242dSZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component); 71523d242dSZhu Ning unsigned int crosstalk_h, crosstalk_l; 72523d242dSZhu Ning unsigned int crosstalk; 73523d242dSZhu Ning 74523d242dSZhu Ning crosstalk = ucontrol->value.integer.value[0]; 75523d242dSZhu Ning regmap_read(es8326->regmap, ES8326_DAC_CROSSTALK, &crosstalk_l); 76523d242dSZhu Ning crosstalk_h = (crosstalk & 0x10) << 1; 77523d242dSZhu Ning crosstalk_l &= 0x0f; 78523d242dSZhu Ning crosstalk_l |= (crosstalk & 0x0f) << 4; 79523d242dSZhu Ning regmap_update_bits(es8326->regmap, ES8326_DAC_RAMPRATE, 80523d242dSZhu Ning 0x20, crosstalk_h); 81523d242dSZhu Ning regmap_write(es8326->regmap, ES8326_DAC_CROSSTALK, crosstalk_l); 82523d242dSZhu Ning 83523d242dSZhu Ning return 0; 84523d242dSZhu Ning } 85523d242dSZhu Ning 86523d242dSZhu Ning static int es8326_crosstalk2_get(struct snd_kcontrol *kcontrol, 87523d242dSZhu Ning struct snd_ctl_elem_value *ucontrol) 88523d242dSZhu Ning { 89523d242dSZhu Ning struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 90523d242dSZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component); 91523d242dSZhu Ning unsigned int crosstalk_h, crosstalk_l; 92523d242dSZhu Ning unsigned int crosstalk; 93523d242dSZhu Ning 94523d242dSZhu Ning regmap_read(es8326->regmap, ES8326_DAC_RAMPRATE, &crosstalk_h); 95523d242dSZhu Ning regmap_read(es8326->regmap, ES8326_DAC_CROSSTALK, &crosstalk_l); 96523d242dSZhu Ning crosstalk_h &= 0x10; 97523d242dSZhu Ning crosstalk_l &= 0x0f; 98523d242dSZhu Ning crosstalk = crosstalk_h | crosstalk_l; 99523d242dSZhu Ning ucontrol->value.integer.value[0] = crosstalk; 100523d242dSZhu Ning 101523d242dSZhu Ning return 0; 102523d242dSZhu Ning } 103523d242dSZhu Ning 104523d242dSZhu Ning static int es8326_crosstalk2_set(struct snd_kcontrol *kcontrol, 105523d242dSZhu Ning struct snd_ctl_elem_value *ucontrol) 106523d242dSZhu Ning { 107523d242dSZhu Ning struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 108523d242dSZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component); 109523d242dSZhu Ning unsigned int crosstalk_h, crosstalk_l; 110523d242dSZhu Ning unsigned int crosstalk; 111523d242dSZhu Ning 112523d242dSZhu Ning crosstalk = ucontrol->value.integer.value[0]; 113523d242dSZhu Ning regmap_read(es8326->regmap, ES8326_DAC_CROSSTALK, &crosstalk_l); 114523d242dSZhu Ning crosstalk_h = crosstalk & 0x10; 115523d242dSZhu Ning crosstalk_l &= 0xf0; 116523d242dSZhu Ning crosstalk_l |= crosstalk & 0x0f; 117523d242dSZhu Ning regmap_update_bits(es8326->regmap, ES8326_DAC_RAMPRATE, 118523d242dSZhu Ning 0x10, crosstalk_h); 119523d242dSZhu Ning regmap_write(es8326->regmap, ES8326_DAC_CROSSTALK, crosstalk_l); 120523d242dSZhu Ning 121523d242dSZhu Ning return 0; 122523d242dSZhu Ning } 123523d242dSZhu Ning 1245c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9550, 50, 0); 1255c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9550, 50, 0); 1265c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_analog_pga_tlv, 0, 300, 0); 1275c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_pga_tlv, 0, 600, 0); 1285c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(softramp_rate, 0, 100, 0); 1295c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_target_tlv, -3200, 200, 0); 1305c439937SZhu Ning static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(drc_recovery_tlv, -125, 250, 0); 1315c439937SZhu Ning 1325c439937SZhu Ning static const char *const winsize[] = { 1335c439937SZhu Ning "0.25db/2 LRCK", 1345c439937SZhu Ning "0.25db/4 LRCK", 1355c439937SZhu Ning "0.25db/8 LRCK", 1365c439937SZhu Ning "0.25db/16 LRCK", 1375c439937SZhu Ning "0.25db/32 LRCK", 1385c439937SZhu Ning "0.25db/64 LRCK", 1395c439937SZhu Ning "0.25db/128 LRCK", 1405c439937SZhu Ning "0.25db/256 LRCK", 1415c439937SZhu Ning "0.25db/512 LRCK", 1425c439937SZhu Ning "0.25db/1024 LRCK", 1435c439937SZhu Ning "0.25db/2048 LRCK", 1445c439937SZhu Ning "0.25db/4096 LRCK", 1455c439937SZhu Ning "0.25db/8192 LRCK", 1465c439937SZhu Ning "0.25db/16384 LRCK", 1475c439937SZhu Ning "0.25db/32768 LRCK", 1485c439937SZhu Ning "0.25db/65536 LRCK", 1495c439937SZhu Ning }; 1505c439937SZhu Ning 1515c439937SZhu Ning static const char *const dacpol_txt[] = { 1525c439937SZhu Ning "Normal", "R Invert", "L Invert", "L + R Invert" }; 1535c439937SZhu Ning 1545c439937SZhu Ning static const struct soc_enum dacpol = 1555c439937SZhu Ning SOC_ENUM_SINGLE(ES8326_DAC_DSM, 4, 4, dacpol_txt); 1565c439937SZhu Ning static const struct soc_enum alc_winsize = 1575c439937SZhu Ning SOC_ENUM_SINGLE(ES8326_ADC_RAMPRATE, 4, 16, winsize); 1585c439937SZhu Ning static const struct soc_enum drc_winsize = 1595c439937SZhu Ning SOC_ENUM_SINGLE(ES8326_DRC_WINSIZE, 4, 16, winsize); 1605c439937SZhu Ning 1615c439937SZhu Ning static const struct snd_kcontrol_new es8326_snd_controls[] = { 1625c439937SZhu Ning SOC_SINGLE_TLV("DAC Playback Volume", ES8326_DAC_VOL, 0, 0xbf, 0, dac_vol_tlv), 1635c439937SZhu Ning SOC_ENUM("Playback Polarity", dacpol), 1645c439937SZhu Ning SOC_SINGLE_TLV("DAC Ramp Rate", ES8326_DAC_RAMPRATE, 0, 0x0f, 0, softramp_rate), 1655c439937SZhu Ning SOC_SINGLE_TLV("DRC Recovery Level", ES8326_DRC_RECOVERY, 0, 4, 0, drc_recovery_tlv), 1665c439937SZhu Ning SOC_ENUM("DRC Winsize", drc_winsize), 1675c439937SZhu Ning SOC_SINGLE_TLV("DRC Target Level", ES8326_DRC_WINSIZE, 0, 0x0f, 0, drc_target_tlv), 1685c439937SZhu Ning 1695c439937SZhu Ning SOC_DOUBLE_R_TLV("ADC Capture Volume", ES8326_ADC1_VOL, ES8326_ADC2_VOL, 0, 0xff, 0, 1705c439937SZhu Ning adc_vol_tlv), 1715c439937SZhu Ning SOC_DOUBLE_TLV("ADC PGA Volume", ES8326_ADC_SCALE, 4, 0, 5, 0, adc_pga_tlv), 1725c439937SZhu Ning SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8326_PGAGAIN, 0, 10, 0, adc_analog_pga_tlv), 1735c439937SZhu Ning SOC_SINGLE_TLV("ADC Ramp Rate", ES8326_ADC_RAMPRATE, 0, 0x0f, 0, softramp_rate), 1745c439937SZhu Ning SOC_SINGLE("ALC Capture Switch", ES8326_ALC_RECOVERY, 3, 1, 0), 1755c439937SZhu Ning SOC_SINGLE_TLV("ALC Capture Recovery Level", ES8326_ALC_LEVEL, 1765c439937SZhu Ning 0, 4, 0, drc_recovery_tlv), 1775c439937SZhu Ning SOC_ENUM("ALC Capture Winsize", alc_winsize), 1785c439937SZhu Ning SOC_SINGLE_TLV("ALC Capture Target Level", ES8326_ALC_LEVEL, 1795c439937SZhu Ning 0, 0x0f, 0, drc_target_tlv), 1805c439937SZhu Ning 181523d242dSZhu Ning SOC_SINGLE_EXT("CROSSTALK1", SND_SOC_NOPM, 0, 31, 0, 182523d242dSZhu Ning es8326_crosstalk1_get, es8326_crosstalk1_set), 183523d242dSZhu Ning SOC_SINGLE_EXT("CROSSTALK2", SND_SOC_NOPM, 0, 31, 0, 184523d242dSZhu Ning es8326_crosstalk2_get, es8326_crosstalk2_set), 1855c439937SZhu Ning }; 1865c439937SZhu Ning 1875c439937SZhu Ning static const struct snd_soc_dapm_widget es8326_dapm_widgets[] = { 1885c439937SZhu Ning SND_SOC_DAPM_INPUT("MIC1"), 1895c439937SZhu Ning SND_SOC_DAPM_INPUT("MIC2"), 1905c439937SZhu Ning SND_SOC_DAPM_INPUT("MIC3"), 1915c439937SZhu Ning SND_SOC_DAPM_INPUT("MIC4"), 1925c439937SZhu Ning 1935c439937SZhu Ning SND_SOC_DAPM_ADC("ADC L", NULL, SND_SOC_NOPM, 0, 0), 1945c439937SZhu Ning SND_SOC_DAPM_ADC("ADC R", NULL, SND_SOC_NOPM, 0, 0), 1955c439937SZhu Ning 1965c439937SZhu Ning /* Digital Interface */ 1975c439937SZhu Ning SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 0, SND_SOC_NOPM, 0, 0), 1985c439937SZhu Ning SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0, SND_SOC_NOPM, 0, 0), 1995c439937SZhu Ning 2005c439937SZhu Ning /* ADC Digital Mute */ 2015c439937SZhu Ning SND_SOC_DAPM_PGA("ADC L1", ES8326_ADC_MUTE, 0, 1, NULL, 0), 2025c439937SZhu Ning SND_SOC_DAPM_PGA("ADC R1", ES8326_ADC_MUTE, 1, 1, NULL, 0), 2035c439937SZhu Ning SND_SOC_DAPM_PGA("ADC L2", ES8326_ADC_MUTE, 2, 1, NULL, 0), 2045c439937SZhu Ning SND_SOC_DAPM_PGA("ADC R2", ES8326_ADC_MUTE, 3, 1, NULL, 0), 2055c439937SZhu Ning 2065c439937SZhu Ning /* Analog Power Supply*/ 2075c439937SZhu Ning SND_SOC_DAPM_DAC("Right DAC", NULL, ES8326_ANA_PDN, 0, 1), 2085c439937SZhu Ning SND_SOC_DAPM_DAC("Left DAC", NULL, ES8326_ANA_PDN, 1, 1), 2095c439937SZhu Ning SND_SOC_DAPM_SUPPLY("MICBIAS1", ES8326_ANA_MICBIAS, 2, 0, NULL, 0), 2105c439937SZhu Ning SND_SOC_DAPM_SUPPLY("MICBIAS2", ES8326_ANA_MICBIAS, 3, 0, NULL, 0), 2115c439937SZhu Ning 2125c439937SZhu Ning SND_SOC_DAPM_PGA("LHPMIX", ES8326_DAC2HPMIX, 7, 0, NULL, 0), 2135c439937SZhu Ning SND_SOC_DAPM_PGA("RHPMIX", ES8326_DAC2HPMIX, 3, 0, NULL, 0), 2145c439937SZhu Ning 215fc702b2cSZhu Ning SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPOR Supply", ES8326_HP_CAL, 216fc702b2cSZhu Ning 4, 7, 0, 0), 217fc702b2cSZhu Ning SND_SOC_DAPM_REG(snd_soc_dapm_supply, "HPOL Supply", ES8326_HP_CAL, 218fc702b2cSZhu Ning 0, 7, 0, 0), 219fc702b2cSZhu Ning 2205c439937SZhu Ning SND_SOC_DAPM_OUTPUT("HPOL"), 2215c439937SZhu Ning SND_SOC_DAPM_OUTPUT("HPOR"), 2225c439937SZhu Ning }; 2235c439937SZhu Ning 2245c439937SZhu Ning static const struct snd_soc_dapm_route es8326_dapm_routes[] = { 2255c439937SZhu Ning {"ADC L1", NULL, "MIC1"}, 2265c439937SZhu Ning {"ADC R1", NULL, "MIC2"}, 2275c439937SZhu Ning {"ADC L2", NULL, "MIC3"}, 2285c439937SZhu Ning {"ADC R2", NULL, "MIC4"}, 2295c439937SZhu Ning 2305c439937SZhu Ning {"ADC L", NULL, "ADC L1"}, 2315c439937SZhu Ning {"ADC R", NULL, "ADC R1"}, 2325c439937SZhu Ning {"ADC L", NULL, "ADC L2"}, 2335c439937SZhu Ning {"ADC R", NULL, "ADC R2"}, 2345c439937SZhu Ning 2355c439937SZhu Ning {"I2S OUT", NULL, "ADC L"}, 2365c439937SZhu Ning {"I2S OUT", NULL, "ADC R"}, 2375c439937SZhu Ning 2385c439937SZhu Ning {"Right DAC", NULL, "I2S IN"}, 2395c439937SZhu Ning {"Left DAC", NULL, "I2S IN"}, 2405c439937SZhu Ning 2415c439937SZhu Ning {"LHPMIX", NULL, "Left DAC"}, 2425c439937SZhu Ning {"RHPMIX", NULL, "Right DAC"}, 2435c439937SZhu Ning 244fc702b2cSZhu Ning {"HPOR", NULL, "HPOR Supply"}, 245fc702b2cSZhu Ning {"HPOL", NULL, "HPOL Supply"}, 246fc702b2cSZhu Ning 2475c439937SZhu Ning {"HPOL", NULL, "LHPMIX"}, 2485c439937SZhu Ning {"HPOR", NULL, "RHPMIX"}, 2495c439937SZhu Ning }; 2505c439937SZhu Ning 251f1230a27SZhu Ning static bool es8326_volatile_register(struct device *dev, unsigned int reg) 252f1230a27SZhu Ning { 253f1230a27SZhu Ning switch (reg) { 254f1230a27SZhu Ning case ES8326_HPL_OFFSET_INI: 255f1230a27SZhu Ning case ES8326_HPR_OFFSET_INI: 256f1230a27SZhu Ning case ES8326_HPDET_STA: 257f1230a27SZhu Ning case ES8326_CTIA_OMTP_STA: 258f1230a27SZhu Ning case ES8326_CSM_MUTE_STA: 259f1230a27SZhu Ning return true; 260f1230a27SZhu Ning default: 261f1230a27SZhu Ning return false; 262f1230a27SZhu Ning } 263f1230a27SZhu Ning } 2645c439937SZhu Ning 2655c69f11cSYang Yingliang static const struct regmap_config es8326_regmap_config = { 2665c439937SZhu Ning .reg_bits = 8, 2675c439937SZhu Ning .val_bits = 8, 2685c439937SZhu Ning .max_register = 0xff, 269f1230a27SZhu Ning .volatile_reg = es8326_volatile_register, 2705c439937SZhu Ning .cache_type = REGCACHE_RBTREE, 2715c439937SZhu Ning }; 2725c439937SZhu Ning 2735c439937SZhu Ning struct _coeff_div { 2745c439937SZhu Ning u16 fs; 2755c439937SZhu Ning u32 rate; 2765c439937SZhu Ning u32 mclk; 2775c439937SZhu Ning u8 reg4; 2785c439937SZhu Ning u8 reg5; 2795c439937SZhu Ning u8 reg6; 2805c439937SZhu Ning u8 reg7; 2815c439937SZhu Ning u8 reg8; 2825c439937SZhu Ning u8 reg9; 2835c439937SZhu Ning u8 rega; 2845c439937SZhu Ning u8 regb; 2855c439937SZhu Ning }; 2865c439937SZhu Ning 2875c439937SZhu Ning /* codec hifi mclk clock divider coefficients */ 2885c439937SZhu Ning /* {ratio, LRCK, MCLK, REG04, REG05, REG06, REG07, REG08, REG09, REG10, REG11} */ 289ee09084fSZhu Ning static const struct _coeff_div coeff_div_v0[] = { 290ee09084fSZhu Ning {64, 8000, 512000, 0x60, 0x01, 0x0F, 0x75, 0x0A, 0x1B, 0x1F, 0x7F}, 291ee09084fSZhu Ning {64, 16000, 1024000, 0x20, 0x00, 0x33, 0x35, 0x0A, 0x1B, 0x1F, 0x3F}, 292ee09084fSZhu Ning {64, 44100, 2822400, 0xE0, 0x00, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F}, 293ee09084fSZhu Ning {64, 48000, 3072000, 0xE0, 0x00, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F}, 294ee09084fSZhu Ning {128, 8000, 1024000, 0x60, 0x00, 0x33, 0x35, 0x0A, 0x1B, 0x1F, 0x7F}, 295ee09084fSZhu Ning {128, 16000, 2048000, 0x20, 0x00, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x3F}, 296ee09084fSZhu Ning {128, 44100, 5644800, 0xE0, 0x01, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F}, 297ee09084fSZhu Ning {128, 48000, 6144000, 0xE0, 0x01, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F}, 2985c439937SZhu Ning 299ee09084fSZhu Ning {192, 32000, 6144000, 0xE0, 0x02, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F}, 300ee09084fSZhu Ning {256, 8000, 2048000, 0x60, 0x00, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x7F}, 301ee09084fSZhu Ning {256, 16000, 4096000, 0x20, 0x01, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x3F}, 302ee09084fSZhu Ning {256, 44100, 11289600, 0xE0, 0x00, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F}, 303ee09084fSZhu Ning {256, 48000, 12288000, 0xE0, 0x00, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F}, 304ee09084fSZhu Ning {384, 32000, 12288000, 0xE0, 0x05, 0x03, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F}, 305ee09084fSZhu Ning {400, 48000, 19200000, 0xE9, 0x04, 0x0F, 0x6d, 0x4A, 0x0A, 0x1F, 0x1F}, 3065c439937SZhu Ning 307ee09084fSZhu Ning {500, 48000, 24000000, 0xF8, 0x04, 0x3F, 0x6D, 0x4A, 0x0A, 0x1F, 0x1F}, 308ee09084fSZhu Ning {512, 8000, 4096000, 0x60, 0x01, 0x03, 0x35, 0x0A, 0x1B, 0x1F, 0x7F}, 309ee09084fSZhu Ning {512, 16000, 8192000, 0x20, 0x00, 0x30, 0x35, 0x0A, 0x1B, 0x1F, 0x3F}, 310ee09084fSZhu Ning {512, 44100, 22579200, 0xE0, 0x00, 0x00, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F}, 311ee09084fSZhu Ning {512, 48000, 24576000, 0xE0, 0x00, 0x00, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F}, 312ee09084fSZhu Ning {768, 32000, 24576000, 0xE0, 0x02, 0x30, 0x2D, 0x4A, 0x0A, 0x1F, 0x1F}, 313ee09084fSZhu Ning {1024, 8000, 8192000, 0x60, 0x00, 0x30, 0x35, 0x0A, 0x1B, 0x1F, 0x7F}, 3145c439937SZhu Ning {1024, 16000, 16384000, 0x20, 0x00, 0x00, 0x35, 0x0A, 0x1B, 0x1F, 0x3F}, 3155c439937SZhu Ning }; 3165c439937SZhu Ning 317ee09084fSZhu Ning static const struct _coeff_div coeff_div_v3[] = { 318ee09084fSZhu Ning {32, 8000, 256000, 0x60, 0x00, 0x0F, 0x75, 0x8A, 0x1B, 0x1F, 0x7F}, 319ee09084fSZhu Ning {32, 16000, 512000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x3F}, 320ee09084fSZhu Ning {32, 44100, 1411200, 0x00, 0x00, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F}, 321ee09084fSZhu Ning {32, 48000, 1536000, 0x00, 0x00, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F}, 322ee09084fSZhu Ning {36, 8000, 288000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x23, 0x47}, 323ee09084fSZhu Ning {36, 16000, 576000, 0x20, 0x00, 0x0D, 0x75, 0x8A, 0x1B, 0x23, 0x47}, 324ee09084fSZhu Ning {48, 8000, 384000, 0x60, 0x02, 0x1F, 0x75, 0x8A, 0x1B, 0x1F, 0x7F}, 325ee09084fSZhu Ning {48, 16000, 768000, 0x20, 0x02, 0x0F, 0x75, 0x8A, 0x1B, 0x1F, 0x3F}, 326ee09084fSZhu Ning {48, 48000, 2304000, 0x00, 0x02, 0x0D, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F}, 327ee09084fSZhu Ning 328ee09084fSZhu Ning {64, 8000, 512000, 0x60, 0x00, 0x35, 0x75, 0x8A, 0x1B, 0x1F, 0x7F}, 329ee09084fSZhu Ning {64, 16000, 1024000, 0x20, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x3F}, 330ee09084fSZhu Ning {64, 44100, 2822400, 0xE0, 0x00, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F}, 331ee09084fSZhu Ning {64, 48000, 3072000, 0xE0, 0x00, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F}, 332ee09084fSZhu Ning {72, 8000, 576000, 0x20, 0x00, 0x13, 0x35, 0x8A, 0x1B, 0x23, 0x47}, 333ee09084fSZhu Ning {72, 16000, 1152000, 0x20, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x23, 0x47}, 334ee09084fSZhu Ning {96, 8000, 768000, 0x60, 0x02, 0x1D, 0x75, 0x8A, 0x1B, 0x1F, 0x7F}, 335ee09084fSZhu Ning {96, 16000, 1536000, 0x20, 0x02, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x3F}, 336ee09084fSZhu Ning {100, 48000, 4800000, 0x04, 0x04, 0x3F, 0x6D, 0xB8, 0x08, 0x4f, 0x1f}, 337ee09084fSZhu Ning {125, 48000, 6000000, 0x04, 0x04, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27}, 338ee09084fSZhu Ning 339ee09084fSZhu Ning {128, 8000, 1024000, 0x60, 0x00, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x7F}, 340ee09084fSZhu Ning {128, 16000, 2048000, 0x20, 0x00, 0x31, 0x35, 0x8A, 0x1B, 0x1F, 0x3F}, 341ee09084fSZhu Ning {128, 44100, 5644800, 0xE0, 0x00, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F}, 342ee09084fSZhu Ning {128, 48000, 6144000, 0xE0, 0x00, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F}, 343ee09084fSZhu Ning {144, 8000, 1152000, 0x20, 0x00, 0x03, 0x35, 0x8A, 0x1B, 0x23, 0x47}, 344ee09084fSZhu Ning {144, 16000, 2304000, 0x20, 0x00, 0x11, 0x35, 0x8A, 0x1B, 0x23, 0x47}, 345ee09084fSZhu Ning {192, 8000, 1536000, 0x60, 0x02, 0x0D, 0x75, 0x8A, 0x1B, 0x1F, 0x7F}, 346ee09084fSZhu Ning {192, 32000, 6144000, 0xE0, 0x02, 0x31, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F}, 347ee09084fSZhu Ning {192, 16000, 3072000, 0x20, 0x02, 0x05, 0x75, 0xCA, 0x1B, 0x1F, 0x3F}, 348ee09084fSZhu Ning 349ee09084fSZhu Ning {200, 48000, 9600000, 0x04, 0x04, 0x0F, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F}, 350ee09084fSZhu Ning {250, 48000, 12000000, 0x04, 0x04, 0x0F, 0x2D, 0xCA, 0x0A, 0x27, 0x27}, 351ee09084fSZhu Ning {256, 8000, 2048000, 0x60, 0x00, 0x31, 0x35, 0x8A, 0x1B, 0x1F, 0x7F}, 352ee09084fSZhu Ning {256, 16000, 4096000, 0x20, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x3F}, 353ee09084fSZhu Ning {256, 44100, 11289600, 0xE0, 0x00, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F}, 354ee09084fSZhu Ning {256, 48000, 12288000, 0xE0, 0x00, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F}, 355ee09084fSZhu Ning {288, 8000, 2304000, 0x20, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x23, 0x47}, 356ee09084fSZhu Ning {384, 8000, 3072000, 0x60, 0x02, 0x05, 0x75, 0x8A, 0x1B, 0x1F, 0x7F}, 357ee09084fSZhu Ning {384, 16000, 6144000, 0x20, 0x02, 0x03, 0x35, 0x8A, 0x1B, 0x1F, 0x3F}, 358ee09084fSZhu Ning {384, 32000, 12288000, 0xE0, 0x02, 0x01, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F}, 359ee09084fSZhu Ning {384, 48000, 18432000, 0x00, 0x02, 0x01, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F}, 360ee09084fSZhu Ning 361ee09084fSZhu Ning {400, 48000, 19200000, 0xE4, 0x04, 0x35, 0x6d, 0xCA, 0x0A, 0x1F, 0x1F}, 362ee09084fSZhu Ning {500, 48000, 24000000, 0xF8, 0x04, 0x3F, 0x6D, 0xCA, 0x0A, 0x1F, 0x1F}, 363ee09084fSZhu Ning {512, 8000, 4096000, 0x60, 0x00, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x7F}, 364ee09084fSZhu Ning {512, 16000, 8192000, 0x20, 0x00, 0x30, 0x35, 0x8A, 0x1B, 0x1F, 0x3F}, 365ee09084fSZhu Ning {512, 44100, 22579200, 0xE0, 0x00, 0x00, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F}, 366ee09084fSZhu Ning {512, 48000, 24576000, 0xE0, 0x00, 0x00, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F}, 367ee09084fSZhu Ning {768, 8000, 6144000, 0x60, 0x02, 0x11, 0x35, 0x8A, 0x1B, 0x1F, 0x7F}, 368ee09084fSZhu Ning {768, 16000, 12288000, 0x20, 0x02, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x3F}, 369ee09084fSZhu Ning {768, 32000, 24576000, 0xE0, 0x02, 0x30, 0x2D, 0xCA, 0x0A, 0x1F, 0x1F}, 370ee09084fSZhu Ning {800, 48000, 38400000, 0x00, 0x18, 0x13, 0x2D, 0x8A, 0x0A, 0x1F, 0x1F}, 371ee09084fSZhu Ning 372ee09084fSZhu Ning {1024, 8000, 8192000, 0x60, 0x00, 0x30, 0x35, 0x8A, 0x1B, 0x1F, 0x7F}, 373ee09084fSZhu Ning {1024, 16000, 16384000, 0x20, 0x00, 0x00, 0x35, 0x8A, 0x1B, 0x1F, 0x3F}, 374ee09084fSZhu Ning {1152, 16000, 18432000, 0x20, 0x08, 0x11, 0x35, 0x8A, 0x1B, 0x1F, 0x3F}, 375ee09084fSZhu Ning {1536, 8000, 12288000, 0x60, 0x02, 0x01, 0x35, 0x8A, 0x1B, 0x1F, 0x7F}, 376ee09084fSZhu Ning {1536, 16000, 24576000, 0x20, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x3F}, 377ee09084fSZhu Ning {1625, 8000, 13000000, 0x0C, 0x18, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27}, 378ee09084fSZhu Ning {1625, 16000, 26000000, 0x0C, 0x18, 0x1F, 0x2D, 0x8A, 0x0A, 0x27, 0x27}, 379ee09084fSZhu Ning {2048, 8000, 16384000, 0x60, 0x00, 0x00, 0x35, 0x8A, 0x1B, 0x1F, 0x7F}, 380ee09084fSZhu Ning {2304, 8000, 18432000, 0x40, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x5F}, 381ee09084fSZhu Ning {3072, 8000, 24576000, 0x60, 0x02, 0x10, 0x35, 0x8A, 0x1B, 0x1F, 0x7F}, 382ee09084fSZhu Ning {3250, 8000, 26000000, 0x0C, 0x18, 0x0F, 0x2D, 0x8A, 0x0A, 0x27, 0x27}, 383ee09084fSZhu Ning }; 384ee09084fSZhu Ning 385ee09084fSZhu Ning static inline int get_coeff(int mclk, int rate, int array, 386ee09084fSZhu Ning const struct _coeff_div *coeff_div) 3875c439937SZhu Ning { 3885c439937SZhu Ning int i; 3895c439937SZhu Ning 390ee09084fSZhu Ning for (i = 0; i < array; i++) { 3915c439937SZhu Ning if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) 3925c439937SZhu Ning return i; 3935c439937SZhu Ning } 3945c439937SZhu Ning 3955c439937SZhu Ning return -EINVAL; 3965c439937SZhu Ning } 3975c439937SZhu Ning 3985c439937SZhu Ning static int es8326_set_dai_sysclk(struct snd_soc_dai *codec_dai, 3995c439937SZhu Ning int clk_id, unsigned int freq, int dir) 4005c439937SZhu Ning { 4015c439937SZhu Ning struct snd_soc_component *codec = codec_dai->component; 4025c439937SZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec); 4035c439937SZhu Ning 4045c439937SZhu Ning es8326->sysclk = freq; 4055c439937SZhu Ning 4065c439937SZhu Ning return 0; 4075c439937SZhu Ning } 4085c439937SZhu Ning 4095c439937SZhu Ning static int es8326_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt) 4105c439937SZhu Ning { 4115c439937SZhu Ning struct snd_soc_component *component = codec_dai->component; 4125c439937SZhu Ning u8 iface = 0; 4135c439937SZhu Ning 4145c439937SZhu Ning switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { 4155c439937SZhu Ning case SND_SOC_DAIFMT_CBC_CFP: 4165c439937SZhu Ning snd_soc_component_update_bits(component, ES8326_RESET, 4175c439937SZhu Ning ES8326_MASTER_MODE_EN, ES8326_MASTER_MODE_EN); 4185c439937SZhu Ning break; 4195c439937SZhu Ning case SND_SOC_DAIFMT_CBC_CFC: 4205c439937SZhu Ning break; 4215c439937SZhu Ning default: 4225c439937SZhu Ning return -EINVAL; 4235c439937SZhu Ning } 4245c439937SZhu Ning 4255c439937SZhu Ning /* interface format */ 4265c439937SZhu Ning switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 4275c439937SZhu Ning case SND_SOC_DAIFMT_I2S: 4285c439937SZhu Ning break; 4295c439937SZhu Ning case SND_SOC_DAIFMT_RIGHT_J: 4305c439937SZhu Ning dev_err(component->dev, "Codec driver does not support right justified\n"); 4315c439937SZhu Ning return -EINVAL; 4325c439937SZhu Ning case SND_SOC_DAIFMT_LEFT_J: 4335c439937SZhu Ning iface |= ES8326_DAIFMT_LEFT_J; 4345c439937SZhu Ning break; 4355c439937SZhu Ning case SND_SOC_DAIFMT_DSP_A: 4365c439937SZhu Ning iface |= ES8326_DAIFMT_DSP_A; 4375c439937SZhu Ning break; 4385c439937SZhu Ning case SND_SOC_DAIFMT_DSP_B: 4395c439937SZhu Ning iface |= ES8326_DAIFMT_DSP_B; 4405c439937SZhu Ning break; 4415c439937SZhu Ning default: 4425c439937SZhu Ning return -EINVAL; 4435c439937SZhu Ning } 4445c439937SZhu Ning 4455c439937SZhu Ning snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DAIFMT_MASK, iface); 4465c439937SZhu Ning 4475c439937SZhu Ning return 0; 4485c439937SZhu Ning } 4495c439937SZhu Ning 4505c439937SZhu Ning static int es8326_pcm_hw_params(struct snd_pcm_substream *substream, 4515c439937SZhu Ning struct snd_pcm_hw_params *params, 4525c439937SZhu Ning struct snd_soc_dai *dai) 4535c439937SZhu Ning { 4545c439937SZhu Ning struct snd_soc_component *component = dai->component; 455ee09084fSZhu Ning const struct _coeff_div *coeff_div; 4565c439937SZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component); 4575c439937SZhu Ning u8 srate = 0; 458ee09084fSZhu Ning int coeff, array; 4595c439937SZhu Ning 460ee09084fSZhu Ning if (es8326->version == 0) { 461ee09084fSZhu Ning coeff_div = coeff_div_v0; 462ee09084fSZhu Ning array = ARRAY_SIZE(coeff_div_v0); 463ee09084fSZhu Ning } else { 464ee09084fSZhu Ning coeff_div = coeff_div_v3; 465ee09084fSZhu Ning array = ARRAY_SIZE(coeff_div_v3); 466ee09084fSZhu Ning } 467ee09084fSZhu Ning coeff = get_coeff(es8326->sysclk, params_rate(params), array, coeff_div); 4685c439937SZhu Ning /* bit size */ 4695c439937SZhu Ning switch (params_format(params)) { 4705c439937SZhu Ning case SNDRV_PCM_FORMAT_S16_LE: 4715c439937SZhu Ning srate |= ES8326_S16_LE; 4725c439937SZhu Ning break; 4735c439937SZhu Ning case SNDRV_PCM_FORMAT_S20_3LE: 4745c439937SZhu Ning srate |= ES8326_S20_3_LE; 4755c439937SZhu Ning break; 4765c439937SZhu Ning case SNDRV_PCM_FORMAT_S18_3LE: 4775c439937SZhu Ning srate |= ES8326_S18_LE; 4785c439937SZhu Ning break; 4795c439937SZhu Ning case SNDRV_PCM_FORMAT_S24_LE: 4805c439937SZhu Ning srate |= ES8326_S24_LE; 4815c439937SZhu Ning break; 4825c439937SZhu Ning case SNDRV_PCM_FORMAT_S32_LE: 4835c439937SZhu Ning srate |= ES8326_S32_LE; 4845c439937SZhu Ning break; 4855c439937SZhu Ning default: 4865c439937SZhu Ning return -EINVAL; 4875c439937SZhu Ning } 4885c439937SZhu Ning 4895c439937SZhu Ning /* set iface & srate */ 4905c439937SZhu Ning snd_soc_component_update_bits(component, ES8326_FMT, ES8326_DATA_LEN_MASK, srate); 4915c439937SZhu Ning 4925c439937SZhu Ning if (coeff >= 0) { 4935c439937SZhu Ning regmap_write(es8326->regmap, ES8326_CLK_DIV1, 4945c439937SZhu Ning coeff_div[coeff].reg4); 4955c439937SZhu Ning regmap_write(es8326->regmap, ES8326_CLK_DIV2, 4965c439937SZhu Ning coeff_div[coeff].reg5); 4975c439937SZhu Ning regmap_write(es8326->regmap, ES8326_CLK_DLL, 4985c439937SZhu Ning coeff_div[coeff].reg6); 4995c439937SZhu Ning regmap_write(es8326->regmap, ES8326_CLK_MUX, 5005c439937SZhu Ning coeff_div[coeff].reg7); 5015c439937SZhu Ning regmap_write(es8326->regmap, ES8326_CLK_ADC_SEL, 5025c439937SZhu Ning coeff_div[coeff].reg8); 5035c439937SZhu Ning regmap_write(es8326->regmap, ES8326_CLK_DAC_SEL, 5045c439937SZhu Ning coeff_div[coeff].reg9); 5055c439937SZhu Ning regmap_write(es8326->regmap, ES8326_CLK_ADC_OSR, 5065c439937SZhu Ning coeff_div[coeff].rega); 5075c439937SZhu Ning regmap_write(es8326->regmap, ES8326_CLK_DAC_OSR, 5085c439937SZhu Ning coeff_div[coeff].regb); 5095c439937SZhu Ning } else { 5105c439937SZhu Ning dev_warn(component->dev, "Clock coefficients do not match"); 5115c439937SZhu Ning } 5125c439937SZhu Ning 5135c439937SZhu Ning return 0; 5145c439937SZhu Ning } 5155c439937SZhu Ning 516083912c2SZhu Ning static int es8326_mute(struct snd_soc_dai *dai, int mute, int direction) 517083912c2SZhu Ning { 518083912c2SZhu Ning struct snd_soc_component *component = dai->component; 519083912c2SZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component); 5200663286eSZhu Ning unsigned int offset_l, offset_r; 521083912c2SZhu Ning 522083912c2SZhu Ning if (mute) { 523083912c2SZhu Ning regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF); 524083912c2SZhu Ning regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE, 525083912c2SZhu Ning ES8326_MUTE_MASK, ES8326_MUTE); 526*a3aa9255SZhu Ning regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 527*a3aa9255SZhu Ning 0x30, 0x00); 528083912c2SZhu Ning } else { 529083912c2SZhu Ning if (!es8326->calibrated) { 530083912c2SZhu Ning regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_FORCE_CAL); 531083912c2SZhu Ning msleep(30); 5320663286eSZhu Ning regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF); 5330663286eSZhu Ning regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l); 5340663286eSZhu Ning regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r); 5350663286eSZhu Ning regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c); 5360663286eSZhu Ning regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l); 5370663286eSZhu Ning regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r); 538083912c2SZhu Ning es8326->calibrated = true; 539083912c2SZhu Ning } 540*a3aa9255SZhu Ning regmap_update_bits(es8326->regmap, ES8326_DAC_DSM, 0x01, 0x01); 541*a3aa9255SZhu Ning usleep_range(1000, 5000); 542*a3aa9255SZhu Ning regmap_update_bits(es8326->regmap, ES8326_DAC_DSM, 0x01, 0x00); 543*a3aa9255SZhu Ning usleep_range(1000, 5000); 544*a3aa9255SZhu Ning regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x30, 0x20); 545*a3aa9255SZhu Ning regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x30, 0x30); 546fc702b2cSZhu Ning regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa1); 547083912c2SZhu Ning regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_ON); 548083912c2SZhu Ning regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE, 549083912c2SZhu Ning ES8326_MUTE_MASK, ~(ES8326_MUTE)); 550083912c2SZhu Ning } 551083912c2SZhu Ning return 0; 552083912c2SZhu Ning } 553083912c2SZhu Ning 5545c439937SZhu Ning static int es8326_set_bias_level(struct snd_soc_component *codec, 5555c439937SZhu Ning enum snd_soc_bias_level level) 5565c439937SZhu Ning { 5575c439937SZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(codec); 5585c439937SZhu Ning int ret; 5595c439937SZhu Ning 5605c439937SZhu Ning switch (level) { 5615c439937SZhu Ning case SND_SOC_BIAS_ON: 5625c439937SZhu Ning ret = clk_prepare_enable(es8326->mclk); 5635c439937SZhu Ning if (ret) 5645c439937SZhu Ning return ret; 5650663286eSZhu Ning 566*a3aa9255SZhu Ning regmap_update_bits(es8326->regmap, ES8326_RESET, 0x02, 0x02); 567*a3aa9255SZhu Ning usleep_range(5000, 10000); 5680663286eSZhu Ning regmap_write(es8326->regmap, ES8326_INTOUT_IO, es8326->interrupt_clk); 5695c439937SZhu Ning regmap_write(es8326->regmap, ES8326_SDINOUT1_IO, 5705c439937SZhu Ning (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT)); 5715c439937SZhu Ning regmap_write(es8326->regmap, ES8326_PGA_PDN, 0x40); 5720663286eSZhu Ning regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x00); 5730663286eSZhu Ning regmap_update_bits(es8326->regmap, ES8326_CLK_CTL, 0x20, 0x20); 574*a3aa9255SZhu Ning regmap_update_bits(es8326->regmap, ES8326_RESET, 0x02, 0x00); 5755c439937SZhu Ning break; 5765c439937SZhu Ning case SND_SOC_BIAS_PREPARE: 5775c439937SZhu Ning break; 5785c439937SZhu Ning case SND_SOC_BIAS_STANDBY: 5790663286eSZhu Ning regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x3b); 5800663286eSZhu Ning regmap_update_bits(es8326->regmap, ES8326_CLK_CTL, 0x20, 0x00); 5815c439937SZhu Ning regmap_write(es8326->regmap, ES8326_SDINOUT1_IO, ES8326_IO_INPUT); 5825c439937SZhu Ning break; 583fc702b2cSZhu Ning case SND_SOC_BIAS_OFF: 584fc702b2cSZhu Ning clk_disable_unprepare(es8326->mclk); 585fc702b2cSZhu Ning break; 5865c439937SZhu Ning } 5875c439937SZhu Ning 5885c439937SZhu Ning return 0; 5895c439937SZhu Ning } 5905c439937SZhu Ning 5915c439937SZhu Ning #define es8326_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ 5925c439937SZhu Ning SNDRV_PCM_FMTBIT_S24_LE) 5935c439937SZhu Ning 5945c439937SZhu Ning static const struct snd_soc_dai_ops es8326_ops = { 5955c439937SZhu Ning .hw_params = es8326_pcm_hw_params, 5965c439937SZhu Ning .set_fmt = es8326_set_dai_fmt, 5975c439937SZhu Ning .set_sysclk = es8326_set_dai_sysclk, 598083912c2SZhu Ning .mute_stream = es8326_mute, 599083912c2SZhu Ning .no_capture_mute = 1, 6005c439937SZhu Ning }; 6015c439937SZhu Ning 6025c439937SZhu Ning static struct snd_soc_dai_driver es8326_dai = { 6035c439937SZhu Ning .name = "ES8326 HiFi", 6045c439937SZhu Ning .playback = { 6055c439937SZhu Ning .stream_name = "Playback", 6065c439937SZhu Ning .channels_min = 1, 6075c439937SZhu Ning .channels_max = 2, 6085c439937SZhu Ning .rates = SNDRV_PCM_RATE_8000_48000, 6095c439937SZhu Ning .formats = es8326_FORMATS, 6105c439937SZhu Ning }, 6115c439937SZhu Ning .capture = { 6125c439937SZhu Ning .stream_name = "Capture", 6135c439937SZhu Ning .channels_min = 1, 6145c439937SZhu Ning .channels_max = 2, 6155c439937SZhu Ning .rates = SNDRV_PCM_RATE_8000_48000, 6165c439937SZhu Ning .formats = es8326_FORMATS, 6175c439937SZhu Ning }, 6185c439937SZhu Ning .ops = &es8326_ops, 6195c439937SZhu Ning .symmetric_rate = 1, 6205c439937SZhu Ning }; 6215c439937SZhu Ning 6225c439937SZhu Ning static void es8326_enable_micbias(struct snd_soc_component *component) 6235c439937SZhu Ning { 6245c439937SZhu Ning struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 6255c439937SZhu Ning 6265c439937SZhu Ning snd_soc_dapm_mutex_lock(dapm); 6275c439937SZhu Ning snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS1"); 6285c439937SZhu Ning snd_soc_dapm_force_enable_pin_unlocked(dapm, "MICBIAS2"); 6295c439937SZhu Ning snd_soc_dapm_sync_unlocked(dapm); 6305c439937SZhu Ning snd_soc_dapm_mutex_unlock(dapm); 6315c439937SZhu Ning } 6325c439937SZhu Ning 6335c439937SZhu Ning static void es8326_disable_micbias(struct snd_soc_component *component) 6345c439937SZhu Ning { 6355c439937SZhu Ning struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 6365c439937SZhu Ning 6375c439937SZhu Ning snd_soc_dapm_mutex_lock(dapm); 6385c439937SZhu Ning snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS1"); 6395c439937SZhu Ning snd_soc_dapm_disable_pin_unlocked(dapm, "MICBIAS2"); 6405c439937SZhu Ning snd_soc_dapm_sync_unlocked(dapm); 6415c439937SZhu Ning snd_soc_dapm_mutex_unlock(dapm); 6425c439937SZhu Ning } 6435c439937SZhu Ning 6445c439937SZhu Ning /* 6455c439937SZhu Ning * For button detection, set the following in soundcard 6465c439937SZhu Ning * snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 6475c439937SZhu Ning * snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP); 6485c439937SZhu Ning * snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN); 6495c439937SZhu Ning */ 6505c439937SZhu Ning static void es8326_jack_button_handler(struct work_struct *work) 6515c439937SZhu Ning { 6525c439937SZhu Ning struct es8326_priv *es8326 = 6535c439937SZhu Ning container_of(work, struct es8326_priv, button_press_work.work); 6545c439937SZhu Ning struct snd_soc_component *comp = es8326->component; 6555c439937SZhu Ning unsigned int iface; 6565c439937SZhu Ning static int button_to_report, press_count; 6575c439937SZhu Ning static int prev_button, cur_button; 6585c439937SZhu Ning 6595c439937SZhu Ning if (!(es8326->jack->status & SND_JACK_HEADSET)) /* Jack unplugged */ 6605c439937SZhu Ning return; 6615c439937SZhu Ning 6625c439937SZhu Ning mutex_lock(&es8326->lock); 6634ddad00cSZhu Ning iface = snd_soc_component_read(comp, ES8326_HPDET_STA); 6645c439937SZhu Ning switch (iface) { 6655c439937SZhu Ning case 0x93: 6665c439937SZhu Ning /* pause button detected */ 6675c439937SZhu Ning cur_button = SND_JACK_BTN_0; 6685c439937SZhu Ning break; 6695c439937SZhu Ning case 0x6f: 67004f96c93SZhu Ning case 0x4b: 6715c439937SZhu Ning /* button volume up */ 6725c439937SZhu Ning cur_button = SND_JACK_BTN_1; 6735c439937SZhu Ning break; 6745c439937SZhu Ning case 0x27: 6755c439937SZhu Ning /* button volume down */ 6765c439937SZhu Ning cur_button = SND_JACK_BTN_2; 6775c439937SZhu Ning break; 6785c439937SZhu Ning case 0x1e: 67904f96c93SZhu Ning case 0xe2: 6805c439937SZhu Ning /* button released or not pressed */ 6815c439937SZhu Ning cur_button = 0; 6825c439937SZhu Ning break; 6835c439937SZhu Ning default: 6845c439937SZhu Ning break; 6855c439937SZhu Ning } 6865c439937SZhu Ning 6875c439937SZhu Ning if ((prev_button == cur_button) && (cur_button != 0)) { 6885c439937SZhu Ning press_count++; 68904f96c93SZhu Ning if (press_count > 3) { 69004f96c93SZhu Ning /* report a press every 120ms */ 6915c439937SZhu Ning snd_soc_jack_report(es8326->jack, cur_button, 6925c439937SZhu Ning SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2); 6935c439937SZhu Ning press_count = 0; 6945c439937SZhu Ning } 6955c439937SZhu Ning button_to_report = cur_button; 6965c439937SZhu Ning queue_delayed_work(system_wq, &es8326->button_press_work, 69704f96c93SZhu Ning msecs_to_jiffies(35)); 6985c439937SZhu Ning } else if (prev_button != cur_button) { 6995c439937SZhu Ning /* mismatch, detect again */ 7005c439937SZhu Ning prev_button = cur_button; 7015c439937SZhu Ning queue_delayed_work(system_wq, &es8326->button_press_work, 70204f96c93SZhu Ning msecs_to_jiffies(35)); 7035c439937SZhu Ning } else { 7045c439937SZhu Ning /* released or no pressed */ 7055c439937SZhu Ning if (button_to_report != 0) { 7065c439937SZhu Ning snd_soc_jack_report(es8326->jack, button_to_report, 7075c439937SZhu Ning SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2); 7085c439937SZhu Ning snd_soc_jack_report(es8326->jack, 0, 7095c439937SZhu Ning SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_BTN_2); 7105c439937SZhu Ning button_to_report = 0; 7115c439937SZhu Ning } 7125c439937SZhu Ning } 7135c439937SZhu Ning mutex_unlock(&es8326->lock); 7145c439937SZhu Ning } 7155c439937SZhu Ning 7165c439937SZhu Ning static void es8326_jack_detect_handler(struct work_struct *work) 7175c439937SZhu Ning { 7185c439937SZhu Ning struct es8326_priv *es8326 = 7195c439937SZhu Ning container_of(work, struct es8326_priv, jack_detect_work.work); 7205c439937SZhu Ning struct snd_soc_component *comp = es8326->component; 7215c439937SZhu Ning unsigned int iface; 7225c439937SZhu Ning 7235c439937SZhu Ning mutex_lock(&es8326->lock); 7244ddad00cSZhu Ning iface = snd_soc_component_read(comp, ES8326_HPDET_STA); 7255c439937SZhu Ning dev_dbg(comp->dev, "gpio flag %#04x", iface); 72604f96c93SZhu Ning 727ee09084fSZhu Ning if ((es8326->jack_remove_retry == 1) && (es8326->version != ES8326_VERSION_B)) { 72804f96c93SZhu Ning if (iface & ES8326_HPINSERT_FLAG) 72904f96c93SZhu Ning es8326->jack_remove_retry = 2; 73004f96c93SZhu Ning else 73104f96c93SZhu Ning es8326->jack_remove_retry = 0; 73204f96c93SZhu Ning 73304f96c93SZhu Ning dev_dbg(comp->dev, "remove event check, set HPJACK_POL normal, cnt = %d\n", 73404f96c93SZhu Ning es8326->jack_remove_retry); 73504f96c93SZhu Ning /* 73604f96c93SZhu Ning * Inverted HPJACK_POL bit to trigger one IRQ to double check HP Removal event 73704f96c93SZhu Ning */ 73804f96c93SZhu Ning regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 73904f96c93SZhu Ning ES8326_HP_DET_JACK_POL, (es8326->jd_inverted ? 74004f96c93SZhu Ning ~es8326->jack_pol : es8326->jack_pol)); 74104f96c93SZhu Ning goto exit; 74204f96c93SZhu Ning } 74304f96c93SZhu Ning 7445c439937SZhu Ning if ((iface & ES8326_HPINSERT_FLAG) == 0) { 7455c439937SZhu Ning /* Jack unplugged or spurious IRQ */ 74604f96c93SZhu Ning dev_dbg(comp->dev, "No headset detected\n"); 74704f96c93SZhu Ning es8326_disable_micbias(es8326->component); 7485c439937SZhu Ning if (es8326->jack->status & SND_JACK_HEADPHONE) { 74904f96c93SZhu Ning dev_dbg(comp->dev, "Report hp remove event\n"); 7505c439937SZhu Ning snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET); 75104f96c93SZhu Ning /* mute adc when mic path switch */ 75204f96c93SZhu Ning regmap_write(es8326->regmap, ES8326_ADC_SCALE, 0x33); 75304f96c93SZhu Ning regmap_write(es8326->regmap, ES8326_ADC1_SRC, 0x44); 75404f96c93SZhu Ning regmap_write(es8326->regmap, ES8326_ADC2_SRC, 0x66); 75504f96c93SZhu Ning es8326->hp = 0; 75604f96c93SZhu Ning } 75704f96c93SZhu Ning regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x01); 75814a0a1ecSZhu Ning regmap_write(es8326->regmap, ES8326_SYS_BIAS, 0x0a); 75914a0a1ecSZhu Ning regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x0f, 0x03); 76004f96c93SZhu Ning /* 76104f96c93SZhu Ning * Inverted HPJACK_POL bit to trigger one IRQ to double check HP Removal event 76204f96c93SZhu Ning */ 763ee09084fSZhu Ning if ((es8326->jack_remove_retry == 0) && (es8326->version != ES8326_VERSION_B)) { 76404f96c93SZhu Ning es8326->jack_remove_retry = 1; 76504f96c93SZhu Ning dev_dbg(comp->dev, "remove event check, invert HPJACK_POL, cnt = %d\n", 76604f96c93SZhu Ning es8326->jack_remove_retry); 76704f96c93SZhu Ning regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 76804f96c93SZhu Ning ES8326_HP_DET_JACK_POL, (es8326->jd_inverted ? 76904f96c93SZhu Ning es8326->jack_pol : ~es8326->jack_pol)); 77004f96c93SZhu Ning 77104f96c93SZhu Ning } else { 77204f96c93SZhu Ning es8326->jack_remove_retry = 0; 7735c439937SZhu Ning } 7745c439937SZhu Ning } else if ((iface & ES8326_HPINSERT_FLAG) == ES8326_HPINSERT_FLAG) { 77504f96c93SZhu Ning es8326->jack_remove_retry = 0; 77604f96c93SZhu Ning if (es8326->hp == 0) { 77704f96c93SZhu Ning dev_dbg(comp->dev, "First insert, start OMTP/CTIA type check\n"); 77804f96c93SZhu Ning /* 7798a81491aSZhu Ning * set auto-check mode, then restart jack_detect_work after 400ms. 78004f96c93SZhu Ning * Don't report jack status. 78104f96c93SZhu Ning */ 78204f96c93SZhu Ning regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x01); 783*a3aa9255SZhu Ning es8326_enable_micbias(es8326->component); 78404f96c93SZhu Ning usleep_range(50000, 70000); 78504f96c93SZhu Ning regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x00); 78614a0a1ecSZhu Ning regmap_write(es8326->regmap, ES8326_SYS_BIAS, 0x1f); 78714a0a1ecSZhu Ning regmap_update_bits(es8326->regmap, ES8326_HP_DRIVER_REF, 0x0f, 0x08); 78804f96c93SZhu Ning queue_delayed_work(system_wq, &es8326->jack_detect_work, 7898a81491aSZhu Ning msecs_to_jiffies(400)); 79004f96c93SZhu Ning es8326->hp = 1; 79104f96c93SZhu Ning goto exit; 79204f96c93SZhu Ning } 7935c439937SZhu Ning if (es8326->jack->status & SND_JACK_HEADSET) { 7945c439937SZhu Ning /* detect button */ 79504f96c93SZhu Ning dev_dbg(comp->dev, "button pressed\n"); 7965c439937SZhu Ning queue_delayed_work(system_wq, &es8326->button_press_work, 10); 79704f96c93SZhu Ning goto exit; 79804f96c93SZhu Ning } 79904f96c93SZhu Ning if ((iface & ES8326_HPBUTTON_FLAG) == 0x01) { 80004f96c93SZhu Ning dev_dbg(comp->dev, "Headphone detected\n"); 8015c439937SZhu Ning snd_soc_jack_report(es8326->jack, 8025c439937SZhu Ning SND_JACK_HEADPHONE, SND_JACK_HEADSET); 80304f96c93SZhu Ning } else { 80404f96c93SZhu Ning dev_dbg(comp->dev, "Headset detected\n"); 80504f96c93SZhu Ning snd_soc_jack_report(es8326->jack, 80604f96c93SZhu Ning SND_JACK_HEADSET, SND_JACK_HEADSET); 80704f96c93SZhu Ning 80804f96c93SZhu Ning regmap_write(es8326->regmap, ES8326_ADC_SCALE, 0x33); 80904f96c93SZhu Ning regmap_update_bits(es8326->regmap, ES8326_PGA_PDN, 81004f96c93SZhu Ning 0x08, 0x08); 81104f96c93SZhu Ning regmap_update_bits(es8326->regmap, ES8326_PGAGAIN, 81204f96c93SZhu Ning 0x80, 0x80); 81304f96c93SZhu Ning regmap_write(es8326->regmap, ES8326_ADC1_SRC, 0x00); 81404f96c93SZhu Ning regmap_write(es8326->regmap, ES8326_ADC2_SRC, 0x00); 81504f96c93SZhu Ning regmap_update_bits(es8326->regmap, ES8326_PGA_PDN, 81604f96c93SZhu Ning 0x08, 0x00); 81704f96c93SZhu Ning usleep_range(10000, 15000); 8185c439937SZhu Ning } 8195c439937SZhu Ning } 82004f96c93SZhu Ning exit: 8215c439937SZhu Ning mutex_unlock(&es8326->lock); 8225c439937SZhu Ning } 8235c439937SZhu Ning 8245c439937SZhu Ning static irqreturn_t es8326_irq(int irq, void *dev_id) 8255c439937SZhu Ning { 8265c439937SZhu Ning struct es8326_priv *es8326 = dev_id; 8275c439937SZhu Ning 8285c439937SZhu Ning if (!es8326->jack) 8295c439937SZhu Ning goto out; 8305c439937SZhu Ning 8315c439937SZhu Ning if (es8326->jack->status & SND_JACK_HEADSET) 8325c439937SZhu Ning queue_delayed_work(system_wq, &es8326->jack_detect_work, 8335c439937SZhu Ning msecs_to_jiffies(10)); 8345c439937SZhu Ning else 8355c439937SZhu Ning queue_delayed_work(system_wq, &es8326->jack_detect_work, 8368a81491aSZhu Ning msecs_to_jiffies(300)); 8375c439937SZhu Ning 8385c439937SZhu Ning out: 8395c439937SZhu Ning return IRQ_HANDLED; 8405c439937SZhu Ning } 8415c439937SZhu Ning 8420663286eSZhu Ning static int es8326_calibrate(struct snd_soc_component *component) 8430663286eSZhu Ning { 8440663286eSZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component); 8450663286eSZhu Ning unsigned int reg; 8460663286eSZhu Ning unsigned int offset_l, offset_r; 8470663286eSZhu Ning 8480663286eSZhu Ning regmap_read(es8326->regmap, ES8326_CHIP_VERSION, ®); 8490663286eSZhu Ning es8326->version = reg; 8500663286eSZhu Ning 8510663286eSZhu Ning if ((es8326->version == ES8326_VERSION_B) && (es8326->calibrated == false)) { 8520663286eSZhu Ning dev_dbg(component->dev, "ES8326_VERSION_B, calibrating\n"); 8530663286eSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_INV, 0xc0); 85414a0a1ecSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_DIV1, 0x03); 8550663286eSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_DLL, 0x30); 8560663286eSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_MUX, 0xed); 857fc702b2cSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_DAC_SEL, 0x08); 8580663286eSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_TRI, 0xc1); 8590663286eSZhu Ning regmap_write(es8326->regmap, ES8326_DAC_MUTE, 0x03); 8600663286eSZhu Ning regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7f); 86114a0a1ecSZhu Ning regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x23); 8620663286eSZhu Ning regmap_write(es8326->regmap, ES8326_DAC2HPMIX, 0x88); 863fc702b2cSZhu Ning usleep_range(15000, 20000); 8640663286eSZhu Ning regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c); 865fc702b2cSZhu Ning usleep_range(15000, 20000); 8660663286eSZhu Ning regmap_write(es8326->regmap, ES8326_RESET, 0xc0); 8670663286eSZhu Ning usleep_range(15000, 20000); 8680663286eSZhu Ning 8690663286eSZhu Ning regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, ES8326_HP_OFF); 8700663286eSZhu Ning regmap_read(es8326->regmap, ES8326_CSM_MUTE_STA, ®); 8710663286eSZhu Ning if ((reg & 0xf0) != 0x40) 8720663286eSZhu Ning msleep(50); 8730663286eSZhu Ning 8740663286eSZhu Ning regmap_write(es8326->regmap, ES8326_HP_CAL, 0xd4); 8750663286eSZhu Ning msleep(200); 8760663286eSZhu Ning regmap_write(es8326->regmap, ES8326_HP_CAL, 0x4d); 8770663286eSZhu Ning msleep(200); 8780663286eSZhu Ning regmap_write(es8326->regmap, ES8326_HP_CAL, ES8326_HP_OFF); 8790663286eSZhu Ning regmap_read(es8326->regmap, ES8326_HPL_OFFSET_INI, &offset_l); 8800663286eSZhu Ning regmap_read(es8326->regmap, ES8326_HPR_OFFSET_INI, &offset_r); 8810663286eSZhu Ning regmap_write(es8326->regmap, ES8326_HP_OFFSET_CAL, 0x8c); 8820663286eSZhu Ning regmap_write(es8326->regmap, ES8326_HPL_OFFSET_INI, offset_l); 8830663286eSZhu Ning regmap_write(es8326->regmap, ES8326_HPR_OFFSET_INI, offset_r); 8840663286eSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00); 8850663286eSZhu Ning 8860663286eSZhu Ning es8326->calibrated = true; 8870663286eSZhu Ning } 8880663286eSZhu Ning 8890663286eSZhu Ning return 0; 8900663286eSZhu Ning } 8910663286eSZhu Ning 8925c439937SZhu Ning static int es8326_resume(struct snd_soc_component *component) 8935c439937SZhu Ning { 8945c439937SZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component); 8955c439937SZhu Ning 8965c439937SZhu Ning regcache_cache_only(es8326->regmap, false); 8975c439937SZhu Ning regcache_sync(es8326->regmap); 8985c439937SZhu Ning 899ac20a73dSZhu Ning /* reset internal clock state */ 900ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_RESET, 0x1f); 901ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_VMIDSEL, 0x0E); 90214a0a1ecSZhu Ning regmap_write(es8326->regmap, ES8326_ANA_LP, 0xf0); 903ac20a73dSZhu Ning usleep_range(10000, 15000); 904fc702b2cSZhu Ning regmap_write(es8326->regmap, ES8326_HPJACK_TIMER, 0xe9); 90514a0a1ecSZhu Ning regmap_write(es8326->regmap, ES8326_ANA_MICBIAS, 0xcb); 906ac20a73dSZhu Ning /* set headphone default type and detect pin */ 907fc702b2cSZhu Ning regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x83); 908ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_RESAMPLE, 0x05); 9095c439937SZhu Ning 910ac20a73dSZhu Ning /* set internal oscillator as clock source of headpone cp */ 911fc702b2cSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_DIV_CPC, 0x89); 912ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_ON); 913ac20a73dSZhu Ning /* clock manager reset release */ 914ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_RESET, 0x17); 915ac20a73dSZhu Ning /* set headphone detection as half scan mode */ 91614a0a1ecSZhu Ning regmap_write(es8326->regmap, ES8326_HP_MISC, 0x3d); 917ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_PULLUP_CTL, 0x00); 918ac20a73dSZhu Ning 919ac20a73dSZhu Ning /* enable headphone driver */ 92014a0a1ecSZhu Ning regmap_write(es8326->regmap, ES8326_HP_VOL, 0xc4); 921ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa7); 922ac20a73dSZhu Ning usleep_range(2000, 5000); 92314a0a1ecSZhu Ning regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0x23); 92414a0a1ecSZhu Ning regmap_write(es8326->regmap, ES8326_HP_DRIVER_REF, 0x33); 925ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_HP_DRIVER, 0xa1); 926ac20a73dSZhu Ning 927ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_INV, 0x00); 928ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_VMIDS1, 0xc4); 929ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_VMIDS2, 0x81); 930ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_CLK_CAL_TIME, 0x00); 9310663286eSZhu Ning /* calibrate for B version */ 9320663286eSZhu Ning es8326_calibrate(component); 933523d242dSZhu Ning regmap_write(es8326->regmap, ES8326_DAC_CROSSTALK, 0xaa); 934523d242dSZhu Ning regmap_write(es8326->regmap, ES8326_DAC_RAMPRATE, 0x00); 935ac20a73dSZhu Ning /* turn off headphone out */ 936ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_HP_CAL, 0x00); 937ac20a73dSZhu Ning /* set ADC and DAC in low power mode */ 938ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_ANA_LP, 0xf0); 939ac20a73dSZhu Ning 9405c439937SZhu Ning regmap_write(es8326->regmap, ES8326_ANA_VSEL, 0x7F); 941ac20a73dSZhu Ning /* select vdda as micbias source */ 942ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_VMIDLOW, 0x23); 943ac20a73dSZhu Ning /* set dac dsmclip = 1 */ 944ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_DAC_DSM, 0x08); 945ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_DAC_VPPSCALE, 0x15); 946ac20a73dSZhu Ning 947*a3aa9255SZhu Ning regmap_write(es8326->regmap, ES8326_HPDET_TYPE, 0x80 | 948*a3aa9255SZhu Ning ((es8326->version == ES8326_VERSION_B) ? 949*a3aa9255SZhu Ning (ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol) : 950*a3aa9255SZhu Ning (ES8326_HP_DET_SRC_PIN9 | es8326->jack_pol | 0x04))); 951*a3aa9255SZhu Ning usleep_range(5000, 10000); 952*a3aa9255SZhu Ning es8326_enable_micbias(es8326->component); 953*a3aa9255SZhu Ning usleep_range(50000, 70000); 954*a3aa9255SZhu Ning regmap_update_bits(es8326->regmap, ES8326_HPDET_TYPE, 0x03, 0x00); 955ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_INT_SOURCE, 956ac20a73dSZhu Ning (ES8326_INT_SRC_PIN9 | ES8326_INT_SRC_BUTTON)); 957ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_INTOUT_IO, 958ac20a73dSZhu Ning es8326->interrupt_clk); 959ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_SDINOUT1_IO, 960ac20a73dSZhu Ning (ES8326_IO_DMIC_CLK << ES8326_SDINOUT1_SHIFT)); 961ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_SDINOUT23_IO, ES8326_IO_INPUT); 962ac20a73dSZhu Ning 96314a0a1ecSZhu Ning regmap_write(es8326->regmap, ES8326_ANA_PDN, 0x00); 964ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_RESET, ES8326_CSM_ON); 965ac20a73dSZhu Ning regmap_update_bits(es8326->regmap, ES8326_PGAGAIN, ES8326_MIC_SEL_MASK, 966ac20a73dSZhu Ning ES8326_MIC1_SEL); 967ac20a73dSZhu Ning 968ac20a73dSZhu Ning regmap_update_bits(es8326->regmap, ES8326_DAC_MUTE, ES8326_MUTE_MASK, 969ac20a73dSZhu Ning ES8326_MUTE); 970ac20a73dSZhu Ning 9715c439937SZhu Ning 97204f96c93SZhu Ning es8326->jack_remove_retry = 0; 97304f96c93SZhu Ning es8326->hp = 0; 9745c439937SZhu Ning return 0; 9755c439937SZhu Ning } 9765c439937SZhu Ning 9775c439937SZhu Ning static int es8326_suspend(struct snd_soc_component *component) 9785c439937SZhu Ning { 9795c439937SZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component); 9805c439937SZhu Ning 9815c439937SZhu Ning cancel_delayed_work_sync(&es8326->jack_detect_work); 9825c439937SZhu Ning es8326_disable_micbias(component); 983083912c2SZhu Ning es8326->calibrated = false; 9845c439937SZhu Ning regmap_write(es8326->regmap, ES8326_CLK_CTL, ES8326_CLK_OFF); 9855c439937SZhu Ning regcache_cache_only(es8326->regmap, true); 9865c439937SZhu Ning regcache_mark_dirty(es8326->regmap); 9875c439937SZhu Ning 988ac20a73dSZhu Ning /* reset register value to default */ 989ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x01); 990ac20a73dSZhu Ning usleep_range(1000, 3000); 991ac20a73dSZhu Ning regmap_write(es8326->regmap, ES8326_CSM_I2C_STA, 0x00); 9925c439937SZhu Ning return 0; 9935c439937SZhu Ning } 9945c439937SZhu Ning 9955c439937SZhu Ning static int es8326_probe(struct snd_soc_component *component) 9965c439937SZhu Ning { 9975c439937SZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component); 9985c439937SZhu Ning int ret; 9995c439937SZhu Ning 10005c439937SZhu Ning es8326->component = component; 10015c439937SZhu Ning es8326->jd_inverted = device_property_read_bool(component->dev, 10025c439937SZhu Ning "everest,jack-detect-inverted"); 10035c439937SZhu Ning 10045c439937SZhu Ning ret = device_property_read_u8(component->dev, "everest,mic1-src", &es8326->mic1_src); 10055c439937SZhu Ning if (ret != 0) { 10065c439937SZhu Ning dev_dbg(component->dev, "mic1-src return %d", ret); 10075c439937SZhu Ning es8326->mic1_src = ES8326_ADC_AMIC; 10085c439937SZhu Ning } 10095c439937SZhu Ning dev_dbg(component->dev, "mic1-src %x", es8326->mic1_src); 10105c439937SZhu Ning 10115c439937SZhu Ning ret = device_property_read_u8(component->dev, "everest,mic2-src", &es8326->mic2_src); 10125c439937SZhu Ning if (ret != 0) { 10135c439937SZhu Ning dev_dbg(component->dev, "mic2-src return %d", ret); 10145c439937SZhu Ning es8326->mic2_src = ES8326_ADC_DMIC; 10155c439937SZhu Ning } 10165c439937SZhu Ning dev_dbg(component->dev, "mic2-src %x", es8326->mic2_src); 10175c439937SZhu Ning 10185c439937SZhu Ning ret = device_property_read_u8(component->dev, "everest,jack-pol", &es8326->jack_pol); 10195c439937SZhu Ning if (ret != 0) { 10205c439937SZhu Ning dev_dbg(component->dev, "jack-pol return %d", ret); 1021ac20a73dSZhu Ning es8326->jack_pol = ES8326_HP_TYPE_AUTO; 10225c439937SZhu Ning } 10235c439937SZhu Ning dev_dbg(component->dev, "jack-pol %x", es8326->jack_pol); 10245c439937SZhu Ning 1025fe1e7e8cSAlexey Firago ret = device_property_read_u8(component->dev, "everest,interrupt-src", 1026fe1e7e8cSAlexey Firago &es8326->interrupt_src); 10275c439937SZhu Ning if (ret != 0) { 10285c439937SZhu Ning dev_dbg(component->dev, "interrupt-src return %d", ret); 10295c439937SZhu Ning es8326->interrupt_src = ES8326_HP_DET_SRC_PIN9; 10305c439937SZhu Ning } 10315c439937SZhu Ning dev_dbg(component->dev, "interrupt-src %x", es8326->interrupt_src); 10325c439937SZhu Ning 1033fe1e7e8cSAlexey Firago ret = device_property_read_u8(component->dev, "everest,interrupt-clk", 1034fe1e7e8cSAlexey Firago &es8326->interrupt_clk); 10355c439937SZhu Ning if (ret != 0) { 10365c439937SZhu Ning dev_dbg(component->dev, "interrupt-clk return %d", ret); 10375c439937SZhu Ning es8326->interrupt_clk = 0x45; 10385c439937SZhu Ning } 10395c439937SZhu Ning dev_dbg(component->dev, "interrupt-clk %x", es8326->interrupt_clk); 10405c439937SZhu Ning 10415c439937SZhu Ning es8326_resume(component); 10425c439937SZhu Ning return 0; 10435c439937SZhu Ning } 10445c439937SZhu Ning 10455c439937SZhu Ning static void es8326_enable_jack_detect(struct snd_soc_component *component, 10465c439937SZhu Ning struct snd_soc_jack *jack) 10475c439937SZhu Ning { 10485c439937SZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component); 10495c439937SZhu Ning 10505c439937SZhu Ning mutex_lock(&es8326->lock); 10515c439937SZhu Ning if (es8326->jd_inverted) 10524ddad00cSZhu Ning snd_soc_component_update_bits(component, ES8326_HPDET_TYPE, 10535c439937SZhu Ning ES8326_HP_DET_JACK_POL, ~es8326->jack_pol); 10545c439937SZhu Ning es8326->jack = jack; 10555c439937SZhu Ning 10565c439937SZhu Ning mutex_unlock(&es8326->lock); 10575c439937SZhu Ning es8326_irq(es8326->irq, es8326); 10585c439937SZhu Ning } 10595c439937SZhu Ning 10605c439937SZhu Ning static void es8326_disable_jack_detect(struct snd_soc_component *component) 10615c439937SZhu Ning { 10625c439937SZhu Ning struct es8326_priv *es8326 = snd_soc_component_get_drvdata(component); 10635c439937SZhu Ning 10645c439937SZhu Ning dev_dbg(component->dev, "Enter into %s\n", __func__); 10655c439937SZhu Ning if (!es8326->jack) 10665c439937SZhu Ning return; /* Already disabled (or never enabled) */ 10675c439937SZhu Ning cancel_delayed_work_sync(&es8326->jack_detect_work); 10685c439937SZhu Ning 10695c439937SZhu Ning mutex_lock(&es8326->lock); 10705c439937SZhu Ning if (es8326->jack->status & SND_JACK_MICROPHONE) { 10715c439937SZhu Ning es8326_disable_micbias(component); 10725c439937SZhu Ning snd_soc_jack_report(es8326->jack, 0, SND_JACK_HEADSET); 10735c439937SZhu Ning } 10745c439937SZhu Ning es8326->jack = NULL; 10755c439937SZhu Ning mutex_unlock(&es8326->lock); 10765c439937SZhu Ning } 10775c439937SZhu Ning 10785c439937SZhu Ning static int es8326_set_jack(struct snd_soc_component *component, 10795c439937SZhu Ning struct snd_soc_jack *jack, void *data) 10805c439937SZhu Ning { 10815c439937SZhu Ning if (jack) 10825c439937SZhu Ning es8326_enable_jack_detect(component, jack); 10835c439937SZhu Ning else 10845c439937SZhu Ning es8326_disable_jack_detect(component); 10855c439937SZhu Ning 10865c439937SZhu Ning return 0; 10875c439937SZhu Ning } 10885c439937SZhu Ning 10895c439937SZhu Ning static void es8326_remove(struct snd_soc_component *component) 10905c439937SZhu Ning { 10915c439937SZhu Ning es8326_disable_jack_detect(component); 10925c439937SZhu Ning es8326_set_bias_level(component, SND_SOC_BIAS_OFF); 10935c439937SZhu Ning } 10945c439937SZhu Ning 10955c439937SZhu Ning static const struct snd_soc_component_driver soc_component_dev_es8326 = { 10965c439937SZhu Ning .probe = es8326_probe, 10975c439937SZhu Ning .remove = es8326_remove, 10985c439937SZhu Ning .resume = es8326_resume, 10995c439937SZhu Ning .suspend = es8326_suspend, 11005c439937SZhu Ning .set_bias_level = es8326_set_bias_level, 11015c439937SZhu Ning .set_jack = es8326_set_jack, 11025c439937SZhu Ning .dapm_widgets = es8326_dapm_widgets, 11035c439937SZhu Ning .num_dapm_widgets = ARRAY_SIZE(es8326_dapm_widgets), 11045c439937SZhu Ning .dapm_routes = es8326_dapm_routes, 11055c439937SZhu Ning .num_dapm_routes = ARRAY_SIZE(es8326_dapm_routes), 11065c439937SZhu Ning .controls = es8326_snd_controls, 11075c439937SZhu Ning .num_controls = ARRAY_SIZE(es8326_snd_controls), 11085c439937SZhu Ning .use_pmdown_time = 1, 11095c439937SZhu Ning .endianness = 1, 11105c439937SZhu Ning }; 11115c439937SZhu Ning 1112784252baSUwe Kleine-König static int es8326_i2c_probe(struct i2c_client *i2c) 11135c439937SZhu Ning { 11145c439937SZhu Ning struct es8326_priv *es8326; 11155c439937SZhu Ning int ret; 11165c439937SZhu Ning 11175c439937SZhu Ning es8326 = devm_kzalloc(&i2c->dev, sizeof(struct es8326_priv), GFP_KERNEL); 11185c439937SZhu Ning if (!es8326) 11195c439937SZhu Ning return -ENOMEM; 11205c439937SZhu Ning 11215c439937SZhu Ning i2c_set_clientdata(i2c, es8326); 11225c439937SZhu Ning es8326->i2c = i2c; 11235c439937SZhu Ning mutex_init(&es8326->lock); 11245c439937SZhu Ning es8326->regmap = devm_regmap_init_i2c(i2c, &es8326_regmap_config); 11255c439937SZhu Ning if (IS_ERR(es8326->regmap)) { 11265c439937SZhu Ning ret = PTR_ERR(es8326->regmap); 11275c439937SZhu Ning dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret); 11285c439937SZhu Ning return ret; 11295c439937SZhu Ning } 11305c439937SZhu Ning 11315c439937SZhu Ning es8326->irq = i2c->irq; 11325c439937SZhu Ning INIT_DELAYED_WORK(&es8326->jack_detect_work, 11335c439937SZhu Ning es8326_jack_detect_handler); 11345c439937SZhu Ning INIT_DELAYED_WORK(&es8326->button_press_work, 11355c439937SZhu Ning es8326_jack_button_handler); 11365c439937SZhu Ning /* ES8316 is level-based while ES8326 is edge-based */ 11375c439937SZhu Ning ret = devm_request_threaded_irq(&i2c->dev, es8326->irq, NULL, es8326_irq, 11385c439937SZhu Ning IRQF_TRIGGER_RISING | IRQF_ONESHOT, 11395c439937SZhu Ning "es8326", es8326); 11405c439937SZhu Ning if (ret) { 11415c439937SZhu Ning dev_warn(&i2c->dev, "Failed to request IRQ: %d: %d\n", 11425c439937SZhu Ning es8326->irq, ret); 11435c439937SZhu Ning es8326->irq = -ENXIO; 11445c439937SZhu Ning } 11455c439937SZhu Ning 11465c439937SZhu Ning es8326->mclk = devm_clk_get_optional(&i2c->dev, "mclk"); 11475c439937SZhu Ning if (IS_ERR(es8326->mclk)) { 11485c439937SZhu Ning dev_err(&i2c->dev, "unable to get mclk\n"); 11495c439937SZhu Ning return PTR_ERR(es8326->mclk); 11505c439937SZhu Ning } 11515c439937SZhu Ning if (!es8326->mclk) 11525c439937SZhu Ning dev_warn(&i2c->dev, "assuming static mclk\n"); 11535c439937SZhu Ning 11545c439937SZhu Ning ret = clk_prepare_enable(es8326->mclk); 11555c439937SZhu Ning if (ret) { 11565c439937SZhu Ning dev_err(&i2c->dev, "unable to enable mclk\n"); 11575c439937SZhu Ning return ret; 11585c439937SZhu Ning } 11595c439937SZhu Ning return devm_snd_soc_register_component(&i2c->dev, 11605c439937SZhu Ning &soc_component_dev_es8326, 11615c439937SZhu Ning &es8326_dai, 1); 11625c439937SZhu Ning } 11635c439937SZhu Ning 11645c439937SZhu Ning static const struct i2c_device_id es8326_i2c_id[] = { 11655c439937SZhu Ning {"es8326", 0 }, 11665c439937SZhu Ning {} 11675c439937SZhu Ning }; 11685c439937SZhu Ning MODULE_DEVICE_TABLE(i2c, es8326_i2c_id); 11695c439937SZhu Ning 11705c439937SZhu Ning #ifdef CONFIG_OF 11715c439937SZhu Ning static const struct of_device_id es8326_of_match[] = { 11725c439937SZhu Ning { .compatible = "everest,es8326", }, 11735c439937SZhu Ning {} 11745c439937SZhu Ning }; 11755c439937SZhu Ning MODULE_DEVICE_TABLE(of, es8326_of_match); 11765c439937SZhu Ning #endif 11775c439937SZhu Ning 11785c439937SZhu Ning #ifdef CONFIG_ACPI 11795c439937SZhu Ning static const struct acpi_device_id es8326_acpi_match[] = { 11805c439937SZhu Ning {"ESSX8326", 0}, 11815c439937SZhu Ning {}, 11825c439937SZhu Ning }; 11835c439937SZhu Ning MODULE_DEVICE_TABLE(acpi, es8326_acpi_match); 11845c439937SZhu Ning #endif 11855c439937SZhu Ning 11865c439937SZhu Ning static struct i2c_driver es8326_i2c_driver = { 11875c439937SZhu Ning .driver = { 11885c439937SZhu Ning .name = "es8326", 11895c439937SZhu Ning .acpi_match_table = ACPI_PTR(es8326_acpi_match), 11905c439937SZhu Ning .of_match_table = of_match_ptr(es8326_of_match), 11915c439937SZhu Ning }, 11929abcd240SUwe Kleine-König .probe = es8326_i2c_probe, 11935c439937SZhu Ning .id_table = es8326_i2c_id, 11945c439937SZhu Ning }; 11955c439937SZhu Ning module_i2c_driver(es8326_i2c_driver); 11965c439937SZhu Ning 11975c439937SZhu Ning MODULE_DESCRIPTION("ASoC es8326 driver"); 11985c439937SZhu Ning MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>"); 11995c439937SZhu Ning MODULE_LICENSE("GPL"); 1200