0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/module.h>
0011 #include <linux/acpi.h>
0012 #include <linux/clk.h>
0013 #include <linux/delay.h>
0014 #include <linux/i2c.h>
0015 #include <linux/mod_devicetable.h>
0016 #include <linux/mutex.h>
0017 #include <linux/regmap.h>
0018 #include <sound/pcm.h>
0019 #include <sound/pcm_params.h>
0020 #include <sound/soc.h>
0021 #include <sound/soc-dapm.h>
0022 #include <sound/tlv.h>
0023 #include <sound/jack.h>
0024 #include "es8316.h"
0025
0026
0027
0028
0029
0030 #define NR_SUPPORTED_MCLK_LRCK_RATIOS 6
0031 static const unsigned int supported_mclk_lrck_ratios[] = {
0032 256, 384, 400, 512, 768, 1024
0033 };
0034
0035 struct es8316_priv {
0036 struct mutex lock;
0037 struct clk *mclk;
0038 struct regmap *regmap;
0039 struct snd_soc_component *component;
0040 struct snd_soc_jack *jack;
0041 int irq;
0042 unsigned int sysclk;
0043 unsigned int allowed_rates[NR_SUPPORTED_MCLK_LRCK_RATIOS];
0044 struct snd_pcm_hw_constraint_list sysclk_constraints;
0045 bool jd_inverted;
0046 };
0047
0048
0049
0050
0051 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9600, 50, 1);
0052 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9600, 50, 1);
0053 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_max_gain_tlv, -650, 150, 0);
0054 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_min_gain_tlv, -1200, 150, 0);
0055 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_target_tlv, -1650, 150, 0);
0056 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpmixer_gain_tlv,
0057 0, 4, TLV_DB_SCALE_ITEM(-1200, 150, 0),
0058 8, 11, TLV_DB_SCALE_ITEM(-450, 150, 0),
0059 );
0060
0061 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(adc_pga_gain_tlv,
0062 0, 0, TLV_DB_SCALE_ITEM(-350, 0, 0),
0063 1, 1, TLV_DB_SCALE_ITEM(0, 0, 0),
0064 2, 2, TLV_DB_SCALE_ITEM(250, 0, 0),
0065 3, 3, TLV_DB_SCALE_ITEM(450, 0, 0),
0066 4, 7, TLV_DB_SCALE_ITEM(700, 300, 0),
0067 8, 10, TLV_DB_SCALE_ITEM(1800, 300, 0),
0068 );
0069
0070 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpout_vol_tlv,
0071 0, 0, TLV_DB_SCALE_ITEM(-4800, 0, 0),
0072 1, 3, TLV_DB_SCALE_ITEM(-2400, 1200, 0),
0073 );
0074
0075 static const char * const ng_type_txt[] =
0076 { "Constant PGA Gain", "Mute ADC Output" };
0077 static const struct soc_enum ng_type =
0078 SOC_ENUM_SINGLE(ES8316_ADC_ALC_NG, 6, 2, ng_type_txt);
0079
0080 static const char * const adcpol_txt[] = { "Normal", "Invert" };
0081 static const struct soc_enum adcpol =
0082 SOC_ENUM_SINGLE(ES8316_ADC_MUTE, 1, 2, adcpol_txt);
0083 static const char *const dacpol_txt[] =
0084 { "Normal", "R Invert", "L Invert", "L + R Invert" };
0085 static const struct soc_enum dacpol =
0086 SOC_ENUM_SINGLE(ES8316_DAC_SET1, 0, 4, dacpol_txt);
0087
0088 static const struct snd_kcontrol_new es8316_snd_controls[] = {
0089 SOC_DOUBLE_TLV("Headphone Playback Volume", ES8316_CPHP_ICAL_VOL,
0090 4, 0, 3, 1, hpout_vol_tlv),
0091 SOC_DOUBLE_TLV("Headphone Mixer Volume", ES8316_HPMIX_VOL,
0092 4, 0, 11, 0, hpmixer_gain_tlv),
0093
0094 SOC_ENUM("Playback Polarity", dacpol),
0095 SOC_DOUBLE_R_TLV("DAC Playback Volume", ES8316_DAC_VOLL,
0096 ES8316_DAC_VOLR, 0, 0xc0, 1, dac_vol_tlv),
0097 SOC_SINGLE("DAC Soft Ramp Switch", ES8316_DAC_SET1, 4, 1, 1),
0098 SOC_SINGLE("DAC Soft Ramp Rate", ES8316_DAC_SET1, 2, 4, 0),
0099 SOC_SINGLE("DAC Notch Filter Switch", ES8316_DAC_SET2, 6, 1, 0),
0100 SOC_SINGLE("DAC Double Fs Switch", ES8316_DAC_SET2, 7, 1, 0),
0101 SOC_SINGLE("DAC Stereo Enhancement", ES8316_DAC_SET3, 0, 7, 0),
0102 SOC_SINGLE("DAC Mono Mix Switch", ES8316_DAC_SET3, 3, 1, 0),
0103
0104 SOC_ENUM("Capture Polarity", adcpol),
0105 SOC_SINGLE("Mic Boost Switch", ES8316_ADC_D2SEPGA, 0, 1, 0),
0106 SOC_SINGLE_TLV("ADC Capture Volume", ES8316_ADC_VOLUME,
0107 0, 0xc0, 1, adc_vol_tlv),
0108 SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8316_ADC_PGAGAIN,
0109 4, 10, 0, adc_pga_gain_tlv),
0110 SOC_SINGLE("ADC Soft Ramp Switch", ES8316_ADC_MUTE, 4, 1, 0),
0111 SOC_SINGLE("ADC Double Fs Switch", ES8316_ADC_DMIC, 4, 1, 0),
0112
0113 SOC_SINGLE("ALC Capture Switch", ES8316_ADC_ALC1, 6, 1, 0),
0114 SOC_SINGLE_TLV("ALC Capture Max Volume", ES8316_ADC_ALC1, 0, 28, 0,
0115 alc_max_gain_tlv),
0116 SOC_SINGLE_TLV("ALC Capture Min Volume", ES8316_ADC_ALC2, 0, 28, 0,
0117 alc_min_gain_tlv),
0118 SOC_SINGLE_TLV("ALC Capture Target Volume", ES8316_ADC_ALC3, 4, 10, 0,
0119 alc_target_tlv),
0120 SOC_SINGLE("ALC Capture Hold Time", ES8316_ADC_ALC3, 0, 10, 0),
0121 SOC_SINGLE("ALC Capture Decay Time", ES8316_ADC_ALC4, 4, 10, 0),
0122 SOC_SINGLE("ALC Capture Attack Time", ES8316_ADC_ALC4, 0, 10, 0),
0123 SOC_SINGLE("ALC Capture Noise Gate Switch", ES8316_ADC_ALC_NG,
0124 5, 1, 0),
0125 SOC_SINGLE("ALC Capture Noise Gate Threshold", ES8316_ADC_ALC_NG,
0126 0, 31, 0),
0127 SOC_ENUM("ALC Capture Noise Gate Type", ng_type),
0128 };
0129
0130
0131 static const char * const es8316_analog_in_txt[] = {
0132 "lin1-rin1",
0133 "lin2-rin2",
0134 "lin1-rin1 with 20db Boost",
0135 "lin2-rin2 with 20db Boost"
0136 };
0137 static const unsigned int es8316_analog_in_values[] = { 0, 1, 2, 3 };
0138 static const struct soc_enum es8316_analog_input_enum =
0139 SOC_VALUE_ENUM_SINGLE(ES8316_ADC_PDN_LINSEL, 4, 3,
0140 ARRAY_SIZE(es8316_analog_in_txt),
0141 es8316_analog_in_txt,
0142 es8316_analog_in_values);
0143 static const struct snd_kcontrol_new es8316_analog_in_mux_controls =
0144 SOC_DAPM_ENUM("Route", es8316_analog_input_enum);
0145
0146 static const char * const es8316_dmic_txt[] = {
0147 "dmic disable",
0148 "dmic data at high level",
0149 "dmic data at low level",
0150 };
0151 static const unsigned int es8316_dmic_values[] = { 0, 1, 2 };
0152 static const struct soc_enum es8316_dmic_src_enum =
0153 SOC_VALUE_ENUM_SINGLE(ES8316_ADC_DMIC, 0, 3,
0154 ARRAY_SIZE(es8316_dmic_txt),
0155 es8316_dmic_txt,
0156 es8316_dmic_values);
0157 static const struct snd_kcontrol_new es8316_dmic_src_controls =
0158 SOC_DAPM_ENUM("Route", es8316_dmic_src_enum);
0159
0160
0161 static const char * const es8316_hpmux_texts[] = {
0162 "lin1-rin1",
0163 "lin2-rin2",
0164 "lin-rin with Boost",
0165 "lin-rin with Boost and PGA"
0166 };
0167
0168 static SOC_ENUM_SINGLE_DECL(es8316_left_hpmux_enum, ES8316_HPMIX_SEL,
0169 4, es8316_hpmux_texts);
0170
0171 static const struct snd_kcontrol_new es8316_left_hpmux_controls =
0172 SOC_DAPM_ENUM("Route", es8316_left_hpmux_enum);
0173
0174 static SOC_ENUM_SINGLE_DECL(es8316_right_hpmux_enum, ES8316_HPMIX_SEL,
0175 0, es8316_hpmux_texts);
0176
0177 static const struct snd_kcontrol_new es8316_right_hpmux_controls =
0178 SOC_DAPM_ENUM("Route", es8316_right_hpmux_enum);
0179
0180
0181 static const struct snd_kcontrol_new es8316_out_left_mix[] = {
0182 SOC_DAPM_SINGLE("LLIN Switch", ES8316_HPMIX_SWITCH, 6, 1, 0),
0183 SOC_DAPM_SINGLE("Left DAC Switch", ES8316_HPMIX_SWITCH, 7, 1, 0),
0184 };
0185 static const struct snd_kcontrol_new es8316_out_right_mix[] = {
0186 SOC_DAPM_SINGLE("RLIN Switch", ES8316_HPMIX_SWITCH, 2, 1, 0),
0187 SOC_DAPM_SINGLE("Right DAC Switch", ES8316_HPMIX_SWITCH, 3, 1, 0),
0188 };
0189
0190
0191 static const char * const es8316_dacsrc_texts[] = {
0192 "LDATA TO LDAC, RDATA TO RDAC",
0193 "LDATA TO LDAC, LDATA TO RDAC",
0194 "RDATA TO LDAC, RDATA TO RDAC",
0195 "RDATA TO LDAC, LDATA TO RDAC",
0196 };
0197
0198 static SOC_ENUM_SINGLE_DECL(es8316_dacsrc_mux_enum, ES8316_DAC_SET1,
0199 6, es8316_dacsrc_texts);
0200
0201 static const struct snd_kcontrol_new es8316_dacsrc_mux_controls =
0202 SOC_DAPM_ENUM("Route", es8316_dacsrc_mux_enum);
0203
0204 static const struct snd_soc_dapm_widget es8316_dapm_widgets[] = {
0205 SND_SOC_DAPM_SUPPLY("Bias", ES8316_SYS_PDN, 3, 1, NULL, 0),
0206 SND_SOC_DAPM_SUPPLY("Analog power", ES8316_SYS_PDN, 4, 1, NULL, 0),
0207 SND_SOC_DAPM_SUPPLY("Mic Bias", ES8316_SYS_PDN, 5, 1, NULL, 0),
0208
0209 SND_SOC_DAPM_INPUT("DMIC"),
0210 SND_SOC_DAPM_INPUT("MIC1"),
0211 SND_SOC_DAPM_INPUT("MIC2"),
0212
0213
0214 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
0215 &es8316_analog_in_mux_controls),
0216
0217 SND_SOC_DAPM_SUPPLY("ADC Vref", ES8316_SYS_PDN, 1, 1, NULL, 0),
0218 SND_SOC_DAPM_SUPPLY("ADC bias", ES8316_SYS_PDN, 2, 1, NULL, 0),
0219 SND_SOC_DAPM_SUPPLY("ADC Clock", ES8316_CLKMGR_CLKSW, 3, 0, NULL, 0),
0220 SND_SOC_DAPM_PGA("Line input PGA", ES8316_ADC_PDN_LINSEL,
0221 7, 1, NULL, 0),
0222 SND_SOC_DAPM_ADC("Mono ADC", NULL, ES8316_ADC_PDN_LINSEL, 6, 1),
0223 SND_SOC_DAPM_MUX("Digital Mic Mux", SND_SOC_NOPM, 0, 0,
0224 &es8316_dmic_src_controls),
0225
0226
0227 SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 1,
0228 ES8316_SERDATA_ADC, 6, 1),
0229 SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0,
0230 SND_SOC_NOPM, 0, 0),
0231
0232 SND_SOC_DAPM_MUX("DAC Source Mux", SND_SOC_NOPM, 0, 0,
0233 &es8316_dacsrc_mux_controls),
0234
0235 SND_SOC_DAPM_SUPPLY("DAC Vref", ES8316_SYS_PDN, 0, 1, NULL, 0),
0236 SND_SOC_DAPM_SUPPLY("DAC Clock", ES8316_CLKMGR_CLKSW, 2, 0, NULL, 0),
0237 SND_SOC_DAPM_DAC("Right DAC", NULL, ES8316_DAC_PDN, 0, 1),
0238 SND_SOC_DAPM_DAC("Left DAC", NULL, ES8316_DAC_PDN, 4, 1),
0239
0240
0241 SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0,
0242 &es8316_left_hpmux_controls),
0243 SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0,
0244 &es8316_right_hpmux_controls),
0245 SND_SOC_DAPM_MIXER("Left Headphone Mixer", ES8316_HPMIX_PDN,
0246 5, 1, &es8316_out_left_mix[0],
0247 ARRAY_SIZE(es8316_out_left_mix)),
0248 SND_SOC_DAPM_MIXER("Right Headphone Mixer", ES8316_HPMIX_PDN,
0249 1, 1, &es8316_out_right_mix[0],
0250 ARRAY_SIZE(es8316_out_right_mix)),
0251 SND_SOC_DAPM_PGA("Left Headphone Mixer Out", ES8316_HPMIX_PDN,
0252 4, 1, NULL, 0),
0253 SND_SOC_DAPM_PGA("Right Headphone Mixer Out", ES8316_HPMIX_PDN,
0254 0, 1, NULL, 0),
0255
0256 SND_SOC_DAPM_OUT_DRV("Left Headphone Charge Pump", ES8316_CPHP_OUTEN,
0257 6, 0, NULL, 0),
0258 SND_SOC_DAPM_OUT_DRV("Right Headphone Charge Pump", ES8316_CPHP_OUTEN,
0259 2, 0, NULL, 0),
0260 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump", ES8316_CPHP_PDN2,
0261 5, 1, NULL, 0),
0262 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump Clock", ES8316_CLKMGR_CLKSW,
0263 4, 0, NULL, 0),
0264
0265 SND_SOC_DAPM_OUT_DRV("Left Headphone Driver", ES8316_CPHP_OUTEN,
0266 5, 0, NULL, 0),
0267 SND_SOC_DAPM_OUT_DRV("Right Headphone Driver", ES8316_CPHP_OUTEN,
0268 1, 0, NULL, 0),
0269 SND_SOC_DAPM_SUPPLY("Headphone Out", ES8316_CPHP_PDN1, 2, 1, NULL, 0),
0270
0271
0272
0273
0274 SND_SOC_DAPM_SUPPLY("Left Headphone ical", ES8316_CPHP_ICAL_VOL,
0275 7, 1, NULL, 0),
0276 SND_SOC_DAPM_SUPPLY("Right Headphone ical", ES8316_CPHP_ICAL_VOL,
0277 3, 1, NULL, 0),
0278
0279 SND_SOC_DAPM_OUTPUT("HPOL"),
0280 SND_SOC_DAPM_OUTPUT("HPOR"),
0281 };
0282
0283 static const struct snd_soc_dapm_route es8316_dapm_routes[] = {
0284
0285 {"MIC1", NULL, "Mic Bias"},
0286 {"MIC2", NULL, "Mic Bias"},
0287 {"MIC1", NULL, "Bias"},
0288 {"MIC2", NULL, "Bias"},
0289 {"MIC1", NULL, "Analog power"},
0290 {"MIC2", NULL, "Analog power"},
0291
0292 {"Differential Mux", "lin1-rin1", "MIC1"},
0293 {"Differential Mux", "lin2-rin2", "MIC2"},
0294 {"Line input PGA", NULL, "Differential Mux"},
0295
0296 {"Mono ADC", NULL, "ADC Clock"},
0297 {"Mono ADC", NULL, "ADC Vref"},
0298 {"Mono ADC", NULL, "ADC bias"},
0299 {"Mono ADC", NULL, "Line input PGA"},
0300
0301
0302
0303
0304 {"Mono ADC", NULL, "DAC Clock"},
0305
0306 {"Digital Mic Mux", "dmic disable", "Mono ADC"},
0307
0308 {"I2S OUT", NULL, "Digital Mic Mux"},
0309
0310
0311 {"DAC Source Mux", "LDATA TO LDAC, RDATA TO RDAC", "I2S IN"},
0312
0313 {"Left DAC", NULL, "DAC Clock"},
0314 {"Right DAC", NULL, "DAC Clock"},
0315
0316 {"Left DAC", NULL, "DAC Vref"},
0317 {"Right DAC", NULL, "DAC Vref"},
0318
0319 {"Left DAC", NULL, "DAC Source Mux"},
0320 {"Right DAC", NULL, "DAC Source Mux"},
0321
0322 {"Left Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
0323 {"Right Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"},
0324
0325 {"Left Headphone Mixer", "LLIN Switch", "Left Headphone Mux"},
0326 {"Left Headphone Mixer", "Left DAC Switch", "Left DAC"},
0327
0328 {"Right Headphone Mixer", "RLIN Switch", "Right Headphone Mux"},
0329 {"Right Headphone Mixer", "Right DAC Switch", "Right DAC"},
0330
0331 {"Left Headphone Mixer Out", NULL, "Left Headphone Mixer"},
0332 {"Right Headphone Mixer Out", NULL, "Right Headphone Mixer"},
0333
0334 {"Left Headphone Charge Pump", NULL, "Left Headphone Mixer Out"},
0335 {"Right Headphone Charge Pump", NULL, "Right Headphone Mixer Out"},
0336
0337 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump"},
0338 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump"},
0339
0340 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
0341 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"},
0342
0343 {"Left Headphone Driver", NULL, "Left Headphone Charge Pump"},
0344 {"Right Headphone Driver", NULL, "Right Headphone Charge Pump"},
0345
0346 {"HPOL", NULL, "Left Headphone Driver"},
0347 {"HPOR", NULL, "Right Headphone Driver"},
0348
0349 {"HPOL", NULL, "Left Headphone ical"},
0350 {"HPOR", NULL, "Right Headphone ical"},
0351
0352 {"Headphone Out", NULL, "Bias"},
0353 {"Headphone Out", NULL, "Analog power"},
0354 {"HPOL", NULL, "Headphone Out"},
0355 {"HPOR", NULL, "Headphone Out"},
0356 };
0357
0358 static int es8316_set_dai_sysclk(struct snd_soc_dai *codec_dai,
0359 int clk_id, unsigned int freq, int dir)
0360 {
0361 struct snd_soc_component *component = codec_dai->component;
0362 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
0363 int i, ret;
0364 int count = 0;
0365
0366 es8316->sysclk = freq;
0367
0368 if (freq == 0) {
0369 es8316->sysclk_constraints.list = NULL;
0370 es8316->sysclk_constraints.count = 0;
0371
0372 return 0;
0373 }
0374
0375 ret = clk_set_rate(es8316->mclk, freq);
0376 if (ret)
0377 return ret;
0378
0379
0380
0381
0382 for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
0383 const unsigned int ratio = supported_mclk_lrck_ratios[i];
0384
0385 if (freq % ratio == 0)
0386 es8316->allowed_rates[count++] = freq / ratio;
0387 }
0388
0389 es8316->sysclk_constraints.list = es8316->allowed_rates;
0390 es8316->sysclk_constraints.count = count;
0391
0392 return 0;
0393 }
0394
0395 static int es8316_set_dai_fmt(struct snd_soc_dai *codec_dai,
0396 unsigned int fmt)
0397 {
0398 struct snd_soc_component *component = codec_dai->component;
0399 u8 serdata1 = 0;
0400 u8 serdata2 = 0;
0401 u8 clksw;
0402 u8 mask;
0403
0404 if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBP_CFP)
0405 serdata1 |= ES8316_SERDATA1_MASTER;
0406
0407 if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S) {
0408 dev_err(component->dev, "Codec driver only supports I2S format\n");
0409 return -EINVAL;
0410 }
0411
0412
0413 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
0414 case SND_SOC_DAIFMT_NB_NF:
0415 break;
0416 case SND_SOC_DAIFMT_IB_IF:
0417 serdata1 |= ES8316_SERDATA1_BCLK_INV;
0418 serdata2 |= ES8316_SERDATA2_ADCLRP;
0419 break;
0420 case SND_SOC_DAIFMT_IB_NF:
0421 serdata1 |= ES8316_SERDATA1_BCLK_INV;
0422 break;
0423 case SND_SOC_DAIFMT_NB_IF:
0424 serdata2 |= ES8316_SERDATA2_ADCLRP;
0425 break;
0426 default:
0427 return -EINVAL;
0428 }
0429
0430 mask = ES8316_SERDATA1_MASTER | ES8316_SERDATA1_BCLK_INV;
0431 snd_soc_component_update_bits(component, ES8316_SERDATA1, mask, serdata1);
0432
0433 mask = ES8316_SERDATA2_FMT_MASK | ES8316_SERDATA2_ADCLRP;
0434 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC, mask, serdata2);
0435 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC, mask, serdata2);
0436
0437
0438 clksw = ES8316_CLKMGR_CLKSW_MCLK_ON | ES8316_CLKMGR_CLKSW_BCLK_ON;
0439 snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW, clksw, clksw);
0440
0441 return 0;
0442 }
0443
0444 static int es8316_pcm_startup(struct snd_pcm_substream *substream,
0445 struct snd_soc_dai *dai)
0446 {
0447 struct snd_soc_component *component = dai->component;
0448 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
0449
0450 if (es8316->sysclk_constraints.list)
0451 snd_pcm_hw_constraint_list(substream->runtime, 0,
0452 SNDRV_PCM_HW_PARAM_RATE,
0453 &es8316->sysclk_constraints);
0454
0455 return 0;
0456 }
0457
0458 static int es8316_pcm_hw_params(struct snd_pcm_substream *substream,
0459 struct snd_pcm_hw_params *params,
0460 struct snd_soc_dai *dai)
0461 {
0462 struct snd_soc_component *component = dai->component;
0463 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
0464 u8 wordlen = 0;
0465 u8 bclk_divider;
0466 u16 lrck_divider;
0467 int i;
0468
0469
0470 for (i = 0; i < NR_SUPPORTED_MCLK_LRCK_RATIOS; i++) {
0471 const unsigned int ratio = supported_mclk_lrck_ratios[i];
0472
0473 if (es8316->sysclk % ratio != 0)
0474 continue;
0475 if (es8316->sysclk / ratio == params_rate(params))
0476 break;
0477 }
0478 if (i == NR_SUPPORTED_MCLK_LRCK_RATIOS)
0479 return -EINVAL;
0480 lrck_divider = es8316->sysclk / params_rate(params);
0481 bclk_divider = lrck_divider / 4;
0482 switch (params_format(params)) {
0483 case SNDRV_PCM_FORMAT_S16_LE:
0484 wordlen = ES8316_SERDATA2_LEN_16;
0485 bclk_divider /= 16;
0486 break;
0487 case SNDRV_PCM_FORMAT_S20_3LE:
0488 wordlen = ES8316_SERDATA2_LEN_20;
0489 bclk_divider /= 20;
0490 break;
0491 case SNDRV_PCM_FORMAT_S24_LE:
0492 wordlen = ES8316_SERDATA2_LEN_24;
0493 bclk_divider /= 24;
0494 break;
0495 case SNDRV_PCM_FORMAT_S32_LE:
0496 wordlen = ES8316_SERDATA2_LEN_32;
0497 bclk_divider /= 32;
0498 break;
0499 default:
0500 return -EINVAL;
0501 }
0502
0503 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC,
0504 ES8316_SERDATA2_LEN_MASK, wordlen);
0505 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC,
0506 ES8316_SERDATA2_LEN_MASK, wordlen);
0507 snd_soc_component_update_bits(component, ES8316_SERDATA1, 0x1f, bclk_divider);
0508 snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV1, 0x0f, lrck_divider >> 8);
0509 snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV2, 0xff, lrck_divider & 0xff);
0510 snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV1, 0x0f, lrck_divider >> 8);
0511 snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV2, 0xff, lrck_divider & 0xff);
0512 return 0;
0513 }
0514
0515 static int es8316_mute(struct snd_soc_dai *dai, int mute, int direction)
0516 {
0517 snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20,
0518 mute ? 0x20 : 0);
0519 return 0;
0520 }
0521
0522 #define ES8316_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
0523 SNDRV_PCM_FMTBIT_S24_LE)
0524
0525 static const struct snd_soc_dai_ops es8316_ops = {
0526 .startup = es8316_pcm_startup,
0527 .hw_params = es8316_pcm_hw_params,
0528 .set_fmt = es8316_set_dai_fmt,
0529 .set_sysclk = es8316_set_dai_sysclk,
0530 .mute_stream = es8316_mute,
0531 .no_capture_mute = 1,
0532 };
0533
0534 static struct snd_soc_dai_driver es8316_dai = {
0535 .name = "ES8316 HiFi",
0536 .playback = {
0537 .stream_name = "Playback",
0538 .channels_min = 1,
0539 .channels_max = 2,
0540 .rates = SNDRV_PCM_RATE_8000_48000,
0541 .formats = ES8316_FORMATS,
0542 },
0543 .capture = {
0544 .stream_name = "Capture",
0545 .channels_min = 1,
0546 .channels_max = 2,
0547 .rates = SNDRV_PCM_RATE_8000_48000,
0548 .formats = ES8316_FORMATS,
0549 },
0550 .ops = &es8316_ops,
0551 .symmetric_rate = 1,
0552 };
0553
0554 static void es8316_enable_micbias_for_mic_gnd_short_detect(
0555 struct snd_soc_component *component)
0556 {
0557 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
0558
0559 snd_soc_dapm_mutex_lock(dapm);
0560 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Bias");
0561 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Analog power");
0562 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Mic Bias");
0563 snd_soc_dapm_sync_unlocked(dapm);
0564 snd_soc_dapm_mutex_unlock(dapm);
0565
0566 msleep(20);
0567 }
0568
0569 static void es8316_disable_micbias_for_mic_gnd_short_detect(
0570 struct snd_soc_component *component)
0571 {
0572 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
0573
0574 snd_soc_dapm_mutex_lock(dapm);
0575 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Bias");
0576 snd_soc_dapm_disable_pin_unlocked(dapm, "Analog power");
0577 snd_soc_dapm_disable_pin_unlocked(dapm, "Bias");
0578 snd_soc_dapm_sync_unlocked(dapm);
0579 snd_soc_dapm_mutex_unlock(dapm);
0580 }
0581
0582 static irqreturn_t es8316_irq(int irq, void *data)
0583 {
0584 struct es8316_priv *es8316 = data;
0585 struct snd_soc_component *comp = es8316->component;
0586 unsigned int flags;
0587
0588 mutex_lock(&es8316->lock);
0589
0590 regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
0591 if (flags == 0x00)
0592 goto out;
0593
0594
0595 if (!es8316->jack)
0596 goto out;
0597
0598 if (es8316->jd_inverted)
0599 flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
0600
0601 dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
0602 if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
0603
0604 if (es8316->jack->status & SND_JACK_MICROPHONE)
0605 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
0606
0607 if (es8316->jack->status & SND_JACK_HEADPHONE) {
0608 snd_soc_jack_report(es8316->jack, 0,
0609 SND_JACK_HEADSET | SND_JACK_BTN_0);
0610 dev_dbg(comp->dev, "jack unplugged\n");
0611 }
0612 } else if (!(es8316->jack->status & SND_JACK_HEADPHONE)) {
0613
0614 es8316_enable_micbias_for_mic_gnd_short_detect(comp);
0615 regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags);
0616 if (es8316->jd_inverted)
0617 flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED;
0618 dev_dbg(comp->dev, "gpio flags %#04x\n", flags);
0619 if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) {
0620
0621 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
0622 } else if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
0623
0624 snd_soc_jack_report(es8316->jack,
0625 SND_JACK_HEADSET,
0626 SND_JACK_HEADSET);
0627
0628 } else {
0629
0630 snd_soc_jack_report(es8316->jack,
0631 SND_JACK_HEADPHONE,
0632 SND_JACK_HEADSET);
0633
0634 es8316_disable_micbias_for_mic_gnd_short_detect(comp);
0635 }
0636 } else if (es8316->jack->status & SND_JACK_MICROPHONE) {
0637
0638 if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) {
0639
0640 snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
0641 } else {
0642
0643 snd_soc_jack_report(es8316->jack,
0644 SND_JACK_BTN_0,
0645 SND_JACK_BTN_0);
0646 }
0647 }
0648
0649 out:
0650 mutex_unlock(&es8316->lock);
0651 return IRQ_HANDLED;
0652 }
0653
0654 static void es8316_enable_jack_detect(struct snd_soc_component *component,
0655 struct snd_soc_jack *jack)
0656 {
0657 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
0658
0659
0660
0661
0662
0663
0664 es8316->jd_inverted = device_property_read_bool(component->dev,
0665 "everest,jack-detect-inverted");
0666
0667 mutex_lock(&es8316->lock);
0668
0669 es8316->jack = jack;
0670
0671 if (es8316->jack->status & SND_JACK_MICROPHONE)
0672 es8316_enable_micbias_for_mic_gnd_short_detect(component);
0673
0674 snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
0675 ES8316_GPIO_ENABLE_INTERRUPT,
0676 ES8316_GPIO_ENABLE_INTERRUPT);
0677
0678 mutex_unlock(&es8316->lock);
0679
0680
0681 enable_irq(es8316->irq);
0682 es8316_irq(es8316->irq, es8316);
0683 }
0684
0685 static void es8316_disable_jack_detect(struct snd_soc_component *component)
0686 {
0687 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
0688
0689 if (!es8316->jack)
0690 return;
0691
0692 disable_irq(es8316->irq);
0693
0694 mutex_lock(&es8316->lock);
0695
0696 snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE,
0697 ES8316_GPIO_ENABLE_INTERRUPT, 0);
0698
0699 if (es8316->jack->status & SND_JACK_MICROPHONE) {
0700 es8316_disable_micbias_for_mic_gnd_short_detect(component);
0701 snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0);
0702 }
0703
0704 es8316->jack = NULL;
0705
0706 mutex_unlock(&es8316->lock);
0707 }
0708
0709 static int es8316_set_jack(struct snd_soc_component *component,
0710 struct snd_soc_jack *jack, void *data)
0711 {
0712 if (jack)
0713 es8316_enable_jack_detect(component, jack);
0714 else
0715 es8316_disable_jack_detect(component);
0716
0717 return 0;
0718 }
0719
0720 static int es8316_probe(struct snd_soc_component *component)
0721 {
0722 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
0723 int ret;
0724
0725 es8316->component = component;
0726
0727 es8316->mclk = devm_clk_get_optional(component->dev, "mclk");
0728 if (IS_ERR(es8316->mclk)) {
0729 dev_err(component->dev, "unable to get mclk\n");
0730 return PTR_ERR(es8316->mclk);
0731 }
0732 if (!es8316->mclk)
0733 dev_warn(component->dev, "assuming static mclk\n");
0734
0735 ret = clk_prepare_enable(es8316->mclk);
0736 if (ret) {
0737 dev_err(component->dev, "unable to enable mclk\n");
0738 return ret;
0739 }
0740
0741
0742 snd_soc_component_write(component, ES8316_RESET, 0x3f);
0743 usleep_range(5000, 5500);
0744 snd_soc_component_write(component, ES8316_RESET, ES8316_RESET_CSM_ON);
0745 msleep(30);
0746
0747
0748
0749
0750
0751 snd_soc_component_write(component, ES8316_SYS_VMIDSEL, 0xff);
0752
0753
0754
0755
0756
0757
0758 snd_soc_component_write(component, ES8316_CLKMGR_ADCOSR, 0x32);
0759
0760 return 0;
0761 }
0762
0763 static void es8316_remove(struct snd_soc_component *component)
0764 {
0765 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component);
0766
0767 clk_disable_unprepare(es8316->mclk);
0768 }
0769
0770 static const struct snd_soc_component_driver soc_component_dev_es8316 = {
0771 .probe = es8316_probe,
0772 .remove = es8316_remove,
0773 .set_jack = es8316_set_jack,
0774 .controls = es8316_snd_controls,
0775 .num_controls = ARRAY_SIZE(es8316_snd_controls),
0776 .dapm_widgets = es8316_dapm_widgets,
0777 .num_dapm_widgets = ARRAY_SIZE(es8316_dapm_widgets),
0778 .dapm_routes = es8316_dapm_routes,
0779 .num_dapm_routes = ARRAY_SIZE(es8316_dapm_routes),
0780 .use_pmdown_time = 1,
0781 .endianness = 1,
0782 };
0783
0784 static const struct regmap_range es8316_volatile_ranges[] = {
0785 regmap_reg_range(ES8316_GPIO_FLAG, ES8316_GPIO_FLAG),
0786 };
0787
0788 static const struct regmap_access_table es8316_volatile_table = {
0789 .yes_ranges = es8316_volatile_ranges,
0790 .n_yes_ranges = ARRAY_SIZE(es8316_volatile_ranges),
0791 };
0792
0793 static const struct regmap_config es8316_regmap = {
0794 .reg_bits = 8,
0795 .val_bits = 8,
0796 .max_register = 0x53,
0797 .volatile_table = &es8316_volatile_table,
0798 .cache_type = REGCACHE_RBTREE,
0799 };
0800
0801 static int es8316_i2c_probe(struct i2c_client *i2c_client)
0802 {
0803 struct device *dev = &i2c_client->dev;
0804 struct es8316_priv *es8316;
0805 int ret;
0806
0807 es8316 = devm_kzalloc(&i2c_client->dev, sizeof(struct es8316_priv),
0808 GFP_KERNEL);
0809 if (es8316 == NULL)
0810 return -ENOMEM;
0811
0812 i2c_set_clientdata(i2c_client, es8316);
0813
0814 es8316->regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap);
0815 if (IS_ERR(es8316->regmap))
0816 return PTR_ERR(es8316->regmap);
0817
0818 es8316->irq = i2c_client->irq;
0819 mutex_init(&es8316->lock);
0820
0821 ret = devm_request_threaded_irq(dev, es8316->irq, NULL, es8316_irq,
0822 IRQF_TRIGGER_HIGH | IRQF_ONESHOT | IRQF_NO_AUTOEN,
0823 "es8316", es8316);
0824 if (ret) {
0825 dev_warn(dev, "Failed to get IRQ %d: %d\n", es8316->irq, ret);
0826 es8316->irq = -ENXIO;
0827 }
0828
0829 return devm_snd_soc_register_component(&i2c_client->dev,
0830 &soc_component_dev_es8316,
0831 &es8316_dai, 1);
0832 }
0833
0834 static const struct i2c_device_id es8316_i2c_id[] = {
0835 {"es8316", 0 },
0836 {}
0837 };
0838 MODULE_DEVICE_TABLE(i2c, es8316_i2c_id);
0839
0840 #ifdef CONFIG_OF
0841 static const struct of_device_id es8316_of_match[] = {
0842 { .compatible = "everest,es8316", },
0843 {},
0844 };
0845 MODULE_DEVICE_TABLE(of, es8316_of_match);
0846 #endif
0847
0848 #ifdef CONFIG_ACPI
0849 static const struct acpi_device_id es8316_acpi_match[] = {
0850 {"ESSX8316", 0},
0851 {"ESSX8336", 0},
0852 {},
0853 };
0854 MODULE_DEVICE_TABLE(acpi, es8316_acpi_match);
0855 #endif
0856
0857 static struct i2c_driver es8316_i2c_driver = {
0858 .driver = {
0859 .name = "es8316",
0860 .acpi_match_table = ACPI_PTR(es8316_acpi_match),
0861 .of_match_table = of_match_ptr(es8316_of_match),
0862 },
0863 .probe_new = es8316_i2c_probe,
0864 .id_table = es8316_i2c_id,
0865 };
0866 module_i2c_driver(es8316_i2c_driver);
0867
0868 MODULE_DESCRIPTION("Everest Semi ES8316 ALSA SoC Codec Driver");
0869 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
0870 MODULE_LICENSE("GPL v2");