1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * es8316.c -- es8316 ALSA SoC audio driver 4 * Copyright Everest Semiconductor Co.,Ltd 5 * 6 * Authors: David Yang <yangxiaohua@everest-semi.com>, 7 * Daniel Drake <drake@endlessm.com> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/acpi.h> 12 #include <linux/clk.h> 13 #include <linux/delay.h> 14 #include <linux/i2c.h> 15 #include <linux/mod_devicetable.h> 16 #include <linux/mutex.h> 17 #include <linux/regmap.h> 18 #include <sound/pcm.h> 19 #include <sound/pcm_params.h> 20 #include <sound/soc.h> 21 #include <sound/soc-dapm.h> 22 #include <sound/tlv.h> 23 #include <sound/jack.h> 24 #include "es8316.h" 25 26 /* In slave mode at single speed, the codec is documented as accepting 5 27 * MCLK/LRCK ratios, but we also add ratio 400, which is commonly used on 28 * Intel Cherry Trail platforms (19.2MHz MCLK, 48kHz LRCK). 29 */ 30 static const unsigned int supported_mclk_lrck_ratios[] = { 31 256, 384, 400, 500, 512, 768, 1024 32 }; 33 34 struct es8316_priv { 35 struct mutex lock; 36 struct clk *mclk; 37 struct regmap *regmap; 38 struct snd_soc_component *component; 39 struct snd_soc_jack *jack; 40 int irq; 41 unsigned int sysclk; 42 /* ES83xx supports halving the MCLK so it supports twice as many rates 43 */ 44 unsigned int allowed_rates[ARRAY_SIZE(supported_mclk_lrck_ratios) * 2]; 45 struct snd_pcm_hw_constraint_list sysclk_constraints; 46 bool jd_inverted; 47 }; 48 49 /* 50 * ES8316 controls 51 */ 52 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(dac_vol_tlv, -9600, 50, 1); 53 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(adc_vol_tlv, -9600, 50, 1); 54 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_max_gain_tlv, -650, 150, 0); 55 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(alc_min_gain_tlv, -1200, 150, 0); 56 57 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(alc_target_tlv, 58 0, 10, TLV_DB_SCALE_ITEM(-1650, 150, 0), 59 11, 11, TLV_DB_SCALE_ITEM(-150, 0, 0), 60 ); 61 62 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpmixer_gain_tlv, 63 0, 4, TLV_DB_SCALE_ITEM(-1200, 150, 0), 64 8, 11, TLV_DB_SCALE_ITEM(-450, 150, 0), 65 ); 66 67 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(adc_pga_gain_tlv, 68 0, 0, TLV_DB_SCALE_ITEM(-350, 0, 0), 69 1, 1, TLV_DB_SCALE_ITEM(0, 0, 0), 70 2, 2, TLV_DB_SCALE_ITEM(250, 0, 0), 71 3, 3, TLV_DB_SCALE_ITEM(450, 0, 0), 72 4, 7, TLV_DB_SCALE_ITEM(700, 300, 0), 73 8, 10, TLV_DB_SCALE_ITEM(1800, 300, 0), 74 ); 75 76 static const SNDRV_CTL_TLVD_DECLARE_DB_RANGE(hpout_vol_tlv, 77 0, 0, TLV_DB_SCALE_ITEM(-4800, 0, 0), 78 1, 3, TLV_DB_SCALE_ITEM(-2400, 1200, 0), 79 ); 80 81 static const char * const ng_type_txt[] = 82 { "Constant PGA Gain", "Mute ADC Output" }; 83 static const struct soc_enum ng_type = 84 SOC_ENUM_SINGLE(ES8316_ADC_ALC_NG, 6, 2, ng_type_txt); 85 86 static const char * const adcpol_txt[] = { "Normal", "Invert" }; 87 static const struct soc_enum adcpol = 88 SOC_ENUM_SINGLE(ES8316_ADC_MUTE, 1, 2, adcpol_txt); 89 static const char *const dacpol_txt[] = 90 { "Normal", "R Invert", "L Invert", "L + R Invert" }; 91 static const struct soc_enum dacpol = 92 SOC_ENUM_SINGLE(ES8316_DAC_SET1, 0, 4, dacpol_txt); 93 94 static const struct snd_kcontrol_new es8316_snd_controls[] = { 95 SOC_DOUBLE_TLV("Headphone Playback Volume", ES8316_CPHP_ICAL_VOL, 96 4, 0, 3, 1, hpout_vol_tlv), 97 SOC_DOUBLE_TLV("Headphone Mixer Volume", ES8316_HPMIX_VOL, 98 4, 0, 11, 0, hpmixer_gain_tlv), 99 100 SOC_ENUM("Playback Polarity", dacpol), 101 SOC_DOUBLE_R_TLV("DAC Playback Volume", ES8316_DAC_VOLL, 102 ES8316_DAC_VOLR, 0, 0xc0, 1, dac_vol_tlv), 103 SOC_SINGLE("DAC Soft Ramp Switch", ES8316_DAC_SET1, 4, 1, 1), 104 SOC_SINGLE("DAC Soft Ramp Rate", ES8316_DAC_SET1, 2, 4, 0), 105 SOC_SINGLE("DAC Notch Filter Switch", ES8316_DAC_SET2, 6, 1, 0), 106 SOC_SINGLE("DAC Double Fs Switch", ES8316_DAC_SET2, 7, 1, 0), 107 SOC_SINGLE("DAC Stereo Enhancement", ES8316_DAC_SET3, 0, 7, 0), 108 SOC_SINGLE("DAC Mono Mix Switch", ES8316_DAC_SET3, 3, 1, 0), 109 110 SOC_ENUM("Capture Polarity", adcpol), 111 SOC_SINGLE("Mic Boost Switch", ES8316_ADC_D2SEPGA, 0, 1, 0), 112 SOC_SINGLE_TLV("ADC Capture Volume", ES8316_ADC_VOLUME, 113 0, 0xc0, 1, adc_vol_tlv), 114 SOC_SINGLE_TLV("ADC PGA Gain Volume", ES8316_ADC_PGAGAIN, 115 4, 10, 0, adc_pga_gain_tlv), 116 SOC_SINGLE("ADC Soft Ramp Switch", ES8316_ADC_MUTE, 4, 1, 0), 117 SOC_SINGLE("ADC Double Fs Switch", ES8316_ADC_DMIC, 4, 1, 0), 118 119 SOC_SINGLE("ALC Capture Switch", ES8316_ADC_ALC1, 6, 1, 0), 120 SOC_SINGLE_TLV("ALC Capture Max Volume", ES8316_ADC_ALC1, 0, 28, 0, 121 alc_max_gain_tlv), 122 SOC_SINGLE_TLV("ALC Capture Min Volume", ES8316_ADC_ALC2, 0, 28, 0, 123 alc_min_gain_tlv), 124 SOC_SINGLE_TLV("ALC Capture Target Volume", ES8316_ADC_ALC3, 4, 11, 0, 125 alc_target_tlv), 126 SOC_SINGLE("ALC Capture Hold Time", ES8316_ADC_ALC3, 0, 10, 0), 127 SOC_SINGLE("ALC Capture Decay Time", ES8316_ADC_ALC4, 4, 10, 0), 128 SOC_SINGLE("ALC Capture Attack Time", ES8316_ADC_ALC4, 0, 10, 0), 129 SOC_SINGLE("ALC Capture Noise Gate Switch", ES8316_ADC_ALC_NG, 130 5, 1, 0), 131 SOC_SINGLE("ALC Capture Noise Gate Threshold", ES8316_ADC_ALC_NG, 132 0, 31, 0), 133 SOC_ENUM("ALC Capture Noise Gate Type", ng_type), 134 }; 135 136 /* Analog Input Mux */ 137 static const char * const es8316_analog_in_txt[] = { 138 "lin1-rin1", 139 "lin2-rin2", 140 "lin1-rin1 with 20db Boost", 141 "lin2-rin2 with 20db Boost" 142 }; 143 static const unsigned int es8316_analog_in_values[] = { 0, 1, 2, 3 }; 144 static const struct soc_enum es8316_analog_input_enum = 145 SOC_VALUE_ENUM_SINGLE(ES8316_ADC_PDN_LINSEL, 4, 3, 146 ARRAY_SIZE(es8316_analog_in_txt), 147 es8316_analog_in_txt, 148 es8316_analog_in_values); 149 static const struct snd_kcontrol_new es8316_analog_in_mux_controls = 150 SOC_DAPM_ENUM("Route", es8316_analog_input_enum); 151 152 static const char * const es8316_dmic_txt[] = { 153 "dmic disable", 154 "dmic data at high level", 155 "dmic data at low level", 156 }; 157 static const unsigned int es8316_dmic_values[] = { 0, 2, 3 }; 158 static const struct soc_enum es8316_dmic_src_enum = 159 SOC_VALUE_ENUM_SINGLE(ES8316_ADC_DMIC, 0, 3, 160 ARRAY_SIZE(es8316_dmic_txt), 161 es8316_dmic_txt, 162 es8316_dmic_values); 163 static const struct snd_kcontrol_new es8316_dmic_src_controls = 164 SOC_DAPM_ENUM("Route", es8316_dmic_src_enum); 165 166 /* hp mixer mux */ 167 static const char * const es8316_hpmux_texts[] = { 168 "lin1-rin1", 169 "lin2-rin2", 170 "lin-rin with Boost", 171 "lin-rin with Boost and PGA" 172 }; 173 174 static SOC_ENUM_SINGLE_DECL(es8316_left_hpmux_enum, ES8316_HPMIX_SEL, 175 4, es8316_hpmux_texts); 176 177 static const struct snd_kcontrol_new es8316_left_hpmux_controls = 178 SOC_DAPM_ENUM("Route", es8316_left_hpmux_enum); 179 180 static SOC_ENUM_SINGLE_DECL(es8316_right_hpmux_enum, ES8316_HPMIX_SEL, 181 0, es8316_hpmux_texts); 182 183 static const struct snd_kcontrol_new es8316_right_hpmux_controls = 184 SOC_DAPM_ENUM("Route", es8316_right_hpmux_enum); 185 186 /* headphone Output Mixer */ 187 static const struct snd_kcontrol_new es8316_out_left_mix[] = { 188 SOC_DAPM_SINGLE("LLIN Switch", ES8316_HPMIX_SWITCH, 6, 1, 0), 189 SOC_DAPM_SINGLE("Left DAC Switch", ES8316_HPMIX_SWITCH, 7, 1, 0), 190 }; 191 static const struct snd_kcontrol_new es8316_out_right_mix[] = { 192 SOC_DAPM_SINGLE("RLIN Switch", ES8316_HPMIX_SWITCH, 2, 1, 0), 193 SOC_DAPM_SINGLE("Right DAC Switch", ES8316_HPMIX_SWITCH, 3, 1, 0), 194 }; 195 196 /* DAC data source mux */ 197 static const char * const es8316_dacsrc_texts[] = { 198 "LDATA TO LDAC, RDATA TO RDAC", 199 "LDATA TO LDAC, LDATA TO RDAC", 200 "RDATA TO LDAC, RDATA TO RDAC", 201 "RDATA TO LDAC, LDATA TO RDAC", 202 }; 203 204 static SOC_ENUM_SINGLE_DECL(es8316_dacsrc_mux_enum, ES8316_DAC_SET1, 205 6, es8316_dacsrc_texts); 206 207 static const struct snd_kcontrol_new es8316_dacsrc_mux_controls = 208 SOC_DAPM_ENUM("Route", es8316_dacsrc_mux_enum); 209 210 static const struct snd_soc_dapm_widget es8316_dapm_widgets[] = { 211 SND_SOC_DAPM_SUPPLY("Bias", ES8316_SYS_PDN, 3, 1, NULL, 0), 212 SND_SOC_DAPM_SUPPLY("Analog power", ES8316_SYS_PDN, 4, 1, NULL, 0), 213 SND_SOC_DAPM_SUPPLY("Mic Bias", ES8316_SYS_PDN, 5, 1, NULL, 0), 214 215 SND_SOC_DAPM_INPUT("DMIC"), 216 SND_SOC_DAPM_INPUT("MIC1"), 217 SND_SOC_DAPM_INPUT("MIC2"), 218 219 /* Input Mux */ 220 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0, 221 &es8316_analog_in_mux_controls), 222 223 SND_SOC_DAPM_SUPPLY("ADC Vref", ES8316_SYS_PDN, 1, 1, NULL, 0), 224 SND_SOC_DAPM_SUPPLY("ADC bias", ES8316_SYS_PDN, 2, 1, NULL, 0), 225 SND_SOC_DAPM_SUPPLY("ADC Clock", ES8316_CLKMGR_CLKSW, 3, 0, NULL, 0), 226 SND_SOC_DAPM_PGA("Line input PGA", ES8316_ADC_PDN_LINSEL, 227 7, 1, NULL, 0), 228 SND_SOC_DAPM_ADC("Mono ADC", NULL, ES8316_ADC_PDN_LINSEL, 6, 1), 229 SND_SOC_DAPM_MUX("Digital Mic Mux", SND_SOC_NOPM, 0, 0, 230 &es8316_dmic_src_controls), 231 232 /* Digital Interface */ 233 SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 1, 234 ES8316_SERDATA_ADC, 6, 1), 235 SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0, 236 SND_SOC_NOPM, 0, 0), 237 238 SND_SOC_DAPM_MUX("DAC Source Mux", SND_SOC_NOPM, 0, 0, 239 &es8316_dacsrc_mux_controls), 240 241 SND_SOC_DAPM_SUPPLY("DAC Vref", ES8316_SYS_PDN, 0, 1, NULL, 0), 242 SND_SOC_DAPM_SUPPLY("DAC Clock", ES8316_CLKMGR_CLKSW, 2, 0, NULL, 0), 243 SND_SOC_DAPM_DAC("Right DAC", NULL, ES8316_DAC_PDN, 0, 1), 244 SND_SOC_DAPM_DAC("Left DAC", NULL, ES8316_DAC_PDN, 4, 1), 245 246 /* Headphone Output Side */ 247 SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, 248 &es8316_left_hpmux_controls), 249 SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, 250 &es8316_right_hpmux_controls), 251 SND_SOC_DAPM_MIXER("Left Headphone Mixer", ES8316_HPMIX_PDN, 252 5, 1, &es8316_out_left_mix[0], 253 ARRAY_SIZE(es8316_out_left_mix)), 254 SND_SOC_DAPM_MIXER("Right Headphone Mixer", ES8316_HPMIX_PDN, 255 1, 1, &es8316_out_right_mix[0], 256 ARRAY_SIZE(es8316_out_right_mix)), 257 SND_SOC_DAPM_PGA("Left Headphone Mixer Out", ES8316_HPMIX_PDN, 258 4, 1, NULL, 0), 259 SND_SOC_DAPM_PGA("Right Headphone Mixer Out", ES8316_HPMIX_PDN, 260 0, 1, NULL, 0), 261 262 SND_SOC_DAPM_OUT_DRV("Left Headphone Charge Pump", ES8316_CPHP_OUTEN, 263 6, 0, NULL, 0), 264 SND_SOC_DAPM_OUT_DRV("Right Headphone Charge Pump", ES8316_CPHP_OUTEN, 265 2, 0, NULL, 0), 266 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump", ES8316_CPHP_PDN2, 267 5, 1, NULL, 0), 268 SND_SOC_DAPM_SUPPLY("Headphone Charge Pump Clock", ES8316_CLKMGR_CLKSW, 269 4, 0, NULL, 0), 270 271 SND_SOC_DAPM_OUT_DRV("Left Headphone Driver", ES8316_CPHP_OUTEN, 272 5, 0, NULL, 0), 273 SND_SOC_DAPM_OUT_DRV("Right Headphone Driver", ES8316_CPHP_OUTEN, 274 1, 0, NULL, 0), 275 SND_SOC_DAPM_SUPPLY("Headphone Out", ES8316_CPHP_PDN1, 2, 1, NULL, 0), 276 277 /* pdn_Lical and pdn_Rical bits are documented as Reserved, but must 278 * be explicitly unset in order to enable HP output 279 */ 280 SND_SOC_DAPM_SUPPLY("Left Headphone ical", ES8316_CPHP_ICAL_VOL, 281 7, 1, NULL, 0), 282 SND_SOC_DAPM_SUPPLY("Right Headphone ical", ES8316_CPHP_ICAL_VOL, 283 3, 1, NULL, 0), 284 285 SND_SOC_DAPM_OUTPUT("HPOL"), 286 SND_SOC_DAPM_OUTPUT("HPOR"), 287 }; 288 289 static const struct snd_soc_dapm_route es8316_dapm_routes[] = { 290 /* Recording */ 291 {"MIC1", NULL, "Mic Bias"}, 292 {"MIC2", NULL, "Mic Bias"}, 293 {"MIC1", NULL, "Bias"}, 294 {"MIC2", NULL, "Bias"}, 295 {"MIC1", NULL, "Analog power"}, 296 {"MIC2", NULL, "Analog power"}, 297 298 {"Differential Mux", "lin1-rin1", "MIC1"}, 299 {"Differential Mux", "lin2-rin2", "MIC2"}, 300 {"Line input PGA", NULL, "Differential Mux"}, 301 302 {"Mono ADC", NULL, "ADC Clock"}, 303 {"Mono ADC", NULL, "ADC Vref"}, 304 {"Mono ADC", NULL, "ADC bias"}, 305 {"Mono ADC", NULL, "Line input PGA"}, 306 307 /* It's not clear why, but to avoid recording only silence, 308 * the DAC clock must be running for the ADC to work. 309 */ 310 {"Mono ADC", NULL, "DAC Clock"}, 311 312 {"Digital Mic Mux", "dmic disable", "Mono ADC"}, 313 314 {"I2S OUT", NULL, "Digital Mic Mux"}, 315 316 /* Playback */ 317 {"DAC Source Mux", "LDATA TO LDAC, RDATA TO RDAC", "I2S IN"}, 318 319 {"Left DAC", NULL, "DAC Clock"}, 320 {"Right DAC", NULL, "DAC Clock"}, 321 322 {"Left DAC", NULL, "DAC Vref"}, 323 {"Right DAC", NULL, "DAC Vref"}, 324 325 {"Left DAC", NULL, "DAC Source Mux"}, 326 {"Right DAC", NULL, "DAC Source Mux"}, 327 328 {"Left Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"}, 329 {"Right Headphone Mux", "lin-rin with Boost and PGA", "Line input PGA"}, 330 331 {"Left Headphone Mixer", "LLIN Switch", "Left Headphone Mux"}, 332 {"Left Headphone Mixer", "Left DAC Switch", "Left DAC"}, 333 334 {"Right Headphone Mixer", "RLIN Switch", "Right Headphone Mux"}, 335 {"Right Headphone Mixer", "Right DAC Switch", "Right DAC"}, 336 337 {"Left Headphone Mixer Out", NULL, "Left Headphone Mixer"}, 338 {"Right Headphone Mixer Out", NULL, "Right Headphone Mixer"}, 339 340 {"Left Headphone Charge Pump", NULL, "Left Headphone Mixer Out"}, 341 {"Right Headphone Charge Pump", NULL, "Right Headphone Mixer Out"}, 342 343 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump"}, 344 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump"}, 345 346 {"Left Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"}, 347 {"Right Headphone Charge Pump", NULL, "Headphone Charge Pump Clock"}, 348 349 {"Left Headphone Driver", NULL, "Left Headphone Charge Pump"}, 350 {"Right Headphone Driver", NULL, "Right Headphone Charge Pump"}, 351 352 {"HPOL", NULL, "Left Headphone Driver"}, 353 {"HPOR", NULL, "Right Headphone Driver"}, 354 355 {"HPOL", NULL, "Left Headphone ical"}, 356 {"HPOR", NULL, "Right Headphone ical"}, 357 358 {"Headphone Out", NULL, "Bias"}, 359 {"Headphone Out", NULL, "Analog power"}, 360 {"HPOL", NULL, "Headphone Out"}, 361 {"HPOR", NULL, "Headphone Out"}, 362 }; 363 364 static int es8316_set_dai_sysclk(struct snd_soc_dai *codec_dai, 365 int clk_id, unsigned int freq, int dir) 366 { 367 struct snd_soc_component *component = codec_dai->component; 368 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); 369 int i, ret; 370 int count = 0; 371 372 es8316->sysclk = freq; 373 es8316->sysclk_constraints.list = NULL; 374 es8316->sysclk_constraints.count = 0; 375 376 if (freq == 0) 377 return 0; 378 379 ret = clk_set_rate(es8316->mclk, freq); 380 if (ret) 381 return ret; 382 383 /* Limit supported sample rates to ones that can be autodetected 384 * by the codec running in slave mode. 385 */ 386 for (i = 0; i < ARRAY_SIZE(supported_mclk_lrck_ratios); i++) { 387 const unsigned int ratio = supported_mclk_lrck_ratios[i]; 388 389 if (freq % ratio == 0) 390 es8316->allowed_rates[count++] = freq / ratio; 391 392 /* We also check if the halved MCLK produces a valid rate 393 * since the codec supports halving the MCLK. 394 */ 395 if ((freq / ratio) % 2 == 0) 396 es8316->allowed_rates[count++] = freq / ratio / 2; 397 } 398 399 if (count) { 400 es8316->sysclk_constraints.list = es8316->allowed_rates; 401 es8316->sysclk_constraints.count = count; 402 } 403 404 return 0; 405 } 406 407 static int es8316_set_dai_fmt(struct snd_soc_dai *codec_dai, 408 unsigned int fmt) 409 { 410 struct snd_soc_component *component = codec_dai->component; 411 u8 serdata1 = 0; 412 u8 serdata2 = 0; 413 u8 clksw; 414 u8 mask; 415 416 if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBP_CFP) 417 serdata1 |= ES8316_SERDATA1_MASTER; 418 419 if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S) { 420 dev_err(component->dev, "Codec driver only supports I2S format\n"); 421 return -EINVAL; 422 } 423 424 /* Clock inversion */ 425 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 426 case SND_SOC_DAIFMT_NB_NF: 427 break; 428 case SND_SOC_DAIFMT_IB_IF: 429 serdata1 |= ES8316_SERDATA1_BCLK_INV; 430 serdata2 |= ES8316_SERDATA2_ADCLRP; 431 break; 432 case SND_SOC_DAIFMT_IB_NF: 433 serdata1 |= ES8316_SERDATA1_BCLK_INV; 434 break; 435 case SND_SOC_DAIFMT_NB_IF: 436 serdata2 |= ES8316_SERDATA2_ADCLRP; 437 break; 438 default: 439 return -EINVAL; 440 } 441 442 mask = ES8316_SERDATA1_MASTER | ES8316_SERDATA1_BCLK_INV; 443 snd_soc_component_update_bits(component, ES8316_SERDATA1, mask, serdata1); 444 445 mask = ES8316_SERDATA2_FMT_MASK | ES8316_SERDATA2_ADCLRP; 446 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC, mask, serdata2); 447 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC, mask, serdata2); 448 449 /* Enable BCLK and MCLK inputs in slave mode */ 450 clksw = ES8316_CLKMGR_CLKSW_MCLK_ON | ES8316_CLKMGR_CLKSW_BCLK_ON; 451 snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW, clksw, clksw); 452 453 return 0; 454 } 455 456 static int es8316_pcm_startup(struct snd_pcm_substream *substream, 457 struct snd_soc_dai *dai) 458 { 459 struct snd_soc_component *component = dai->component; 460 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); 461 462 if (es8316->sysclk_constraints.list) 463 snd_pcm_hw_constraint_list(substream->runtime, 0, 464 SNDRV_PCM_HW_PARAM_RATE, 465 &es8316->sysclk_constraints); 466 467 return 0; 468 } 469 470 static int es8316_pcm_hw_params(struct snd_pcm_substream *substream, 471 struct snd_pcm_hw_params *params, 472 struct snd_soc_dai *dai) 473 { 474 struct snd_soc_component *component = dai->component; 475 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); 476 u8 wordlen = 0; 477 u8 bclk_divider; 478 u16 lrck_divider; 479 int i; 480 unsigned int clk = es8316->sysclk / 2; 481 bool clk_valid = false; 482 483 /* We will start with halved sysclk and see if we can use it 484 * for proper clocking. This is to minimise the risk of running 485 * the CODEC with a too high frequency. We have an SKU where 486 * the sysclk frequency is 48Mhz and this causes the sound to be 487 * sped up. If we can run with a halved sysclk, we will use it, 488 * if we can't use it, then full sysclk will be used. 489 */ 490 do { 491 /* Validate supported sample rates that are autodetected from MCLK */ 492 for (i = 0; i < ARRAY_SIZE(supported_mclk_lrck_ratios); i++) { 493 const unsigned int ratio = supported_mclk_lrck_ratios[i]; 494 495 if (clk % ratio != 0) 496 continue; 497 if (clk / ratio == params_rate(params)) 498 break; 499 } 500 if (i == ARRAY_SIZE(supported_mclk_lrck_ratios)) { 501 if (clk == es8316->sysclk) 502 return -EINVAL; 503 clk = es8316->sysclk; 504 } else { 505 clk_valid = true; 506 } 507 } while (!clk_valid); 508 509 if (clk != es8316->sysclk) { 510 snd_soc_component_update_bits(component, ES8316_CLKMGR_CLKSW, 511 ES8316_CLKMGR_CLKSW_MCLK_DIV, 512 ES8316_CLKMGR_CLKSW_MCLK_DIV); 513 } 514 515 lrck_divider = clk / params_rate(params); 516 bclk_divider = lrck_divider / 4; 517 switch (params_format(params)) { 518 case SNDRV_PCM_FORMAT_S16_LE: 519 wordlen = ES8316_SERDATA2_LEN_16; 520 bclk_divider /= 16; 521 break; 522 case SNDRV_PCM_FORMAT_S20_3LE: 523 wordlen = ES8316_SERDATA2_LEN_20; 524 bclk_divider /= 20; 525 break; 526 case SNDRV_PCM_FORMAT_S24_LE: 527 case SNDRV_PCM_FORMAT_S24_3LE: 528 wordlen = ES8316_SERDATA2_LEN_24; 529 bclk_divider /= 24; 530 break; 531 case SNDRV_PCM_FORMAT_S32_LE: 532 wordlen = ES8316_SERDATA2_LEN_32; 533 bclk_divider /= 32; 534 break; 535 default: 536 return -EINVAL; 537 } 538 539 snd_soc_component_update_bits(component, ES8316_SERDATA_DAC, 540 ES8316_SERDATA2_LEN_MASK, wordlen); 541 snd_soc_component_update_bits(component, ES8316_SERDATA_ADC, 542 ES8316_SERDATA2_LEN_MASK, wordlen); 543 snd_soc_component_update_bits(component, ES8316_SERDATA1, 0x1f, bclk_divider); 544 snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV1, 0x0f, lrck_divider >> 8); 545 snd_soc_component_update_bits(component, ES8316_CLKMGR_ADCDIV2, 0xff, lrck_divider & 0xff); 546 snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV1, 0x0f, lrck_divider >> 8); 547 snd_soc_component_update_bits(component, ES8316_CLKMGR_DACDIV2, 0xff, lrck_divider & 0xff); 548 return 0; 549 } 550 551 static int es8316_mute(struct snd_soc_dai *dai, int mute, int direction) 552 { 553 snd_soc_component_update_bits(dai->component, ES8316_DAC_SET1, 0x20, 554 mute ? 0x20 : 0); 555 return 0; 556 } 557 558 #define ES8316_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ 559 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) 560 561 static const struct snd_soc_dai_ops es8316_ops = { 562 .startup = es8316_pcm_startup, 563 .hw_params = es8316_pcm_hw_params, 564 .set_fmt = es8316_set_dai_fmt, 565 .set_sysclk = es8316_set_dai_sysclk, 566 .mute_stream = es8316_mute, 567 .no_capture_mute = 1, 568 }; 569 570 static struct snd_soc_dai_driver es8316_dai = { 571 .name = "ES8316 HiFi", 572 .playback = { 573 .stream_name = "Playback", 574 .channels_min = 1, 575 .channels_max = 2, 576 .rates = SNDRV_PCM_RATE_8000_48000, 577 .formats = ES8316_FORMATS, 578 }, 579 .capture = { 580 .stream_name = "Capture", 581 .channels_min = 1, 582 .channels_max = 2, 583 .rates = SNDRV_PCM_RATE_8000_48000, 584 .formats = ES8316_FORMATS, 585 }, 586 .ops = &es8316_ops, 587 .symmetric_rate = 1, 588 }; 589 590 static void es8316_enable_micbias_for_mic_gnd_short_detect( 591 struct snd_soc_component *component) 592 { 593 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 594 595 snd_soc_dapm_mutex_lock(dapm); 596 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Bias"); 597 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Analog power"); 598 snd_soc_dapm_force_enable_pin_unlocked(dapm, "Mic Bias"); 599 snd_soc_dapm_sync_unlocked(dapm); 600 snd_soc_dapm_mutex_unlock(dapm); 601 602 msleep(20); 603 } 604 605 static void es8316_disable_micbias_for_mic_gnd_short_detect( 606 struct snd_soc_component *component) 607 { 608 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 609 610 snd_soc_dapm_mutex_lock(dapm); 611 snd_soc_dapm_disable_pin_unlocked(dapm, "Mic Bias"); 612 snd_soc_dapm_disable_pin_unlocked(dapm, "Analog power"); 613 snd_soc_dapm_disable_pin_unlocked(dapm, "Bias"); 614 snd_soc_dapm_sync_unlocked(dapm); 615 snd_soc_dapm_mutex_unlock(dapm); 616 } 617 618 static irqreturn_t es8316_irq(int irq, void *data) 619 { 620 struct es8316_priv *es8316 = data; 621 struct snd_soc_component *comp = es8316->component; 622 unsigned int flags; 623 624 mutex_lock(&es8316->lock); 625 626 regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags); 627 if (flags == 0x00) 628 goto out; /* Powered-down / reset */ 629 630 /* Catch spurious IRQ before set_jack is called */ 631 if (!es8316->jack) 632 goto out; 633 634 if (es8316->jd_inverted) 635 flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED; 636 637 dev_dbg(comp->dev, "gpio flags %#04x\n", flags); 638 if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) { 639 /* Jack removed, or spurious IRQ? */ 640 if (es8316->jack->status & SND_JACK_MICROPHONE) 641 es8316_disable_micbias_for_mic_gnd_short_detect(comp); 642 643 if (es8316->jack->status & SND_JACK_HEADPHONE) { 644 snd_soc_jack_report(es8316->jack, 0, 645 SND_JACK_HEADSET | SND_JACK_BTN_0); 646 dev_dbg(comp->dev, "jack unplugged\n"); 647 } 648 } else if (!(es8316->jack->status & SND_JACK_HEADPHONE)) { 649 /* Jack inserted, determine type */ 650 es8316_enable_micbias_for_mic_gnd_short_detect(comp); 651 regmap_read(es8316->regmap, ES8316_GPIO_FLAG, &flags); 652 if (es8316->jd_inverted) 653 flags ^= ES8316_GPIO_FLAG_HP_NOT_INSERTED; 654 dev_dbg(comp->dev, "gpio flags %#04x\n", flags); 655 if (flags & ES8316_GPIO_FLAG_HP_NOT_INSERTED) { 656 /* Jack unplugged underneath us */ 657 es8316_disable_micbias_for_mic_gnd_short_detect(comp); 658 } else if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) { 659 /* Open, headset */ 660 snd_soc_jack_report(es8316->jack, 661 SND_JACK_HEADSET, 662 SND_JACK_HEADSET); 663 /* Keep mic-gnd-short detection on for button press */ 664 } else { 665 /* Shorted, headphones */ 666 snd_soc_jack_report(es8316->jack, 667 SND_JACK_HEADPHONE, 668 SND_JACK_HEADSET); 669 /* No longer need mic-gnd-short detection */ 670 es8316_disable_micbias_for_mic_gnd_short_detect(comp); 671 } 672 } else if (es8316->jack->status & SND_JACK_MICROPHONE) { 673 /* Interrupt while jack inserted, report button state */ 674 if (flags & ES8316_GPIO_FLAG_GM_NOT_SHORTED) { 675 /* Open, button release */ 676 snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0); 677 } else { 678 /* Short, button press */ 679 snd_soc_jack_report(es8316->jack, 680 SND_JACK_BTN_0, 681 SND_JACK_BTN_0); 682 } 683 } 684 685 out: 686 mutex_unlock(&es8316->lock); 687 return IRQ_HANDLED; 688 } 689 690 static void es8316_enable_jack_detect(struct snd_soc_component *component, 691 struct snd_soc_jack *jack) 692 { 693 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); 694 695 /* 696 * Init es8316->jd_inverted here and not in the probe, as we cannot 697 * guarantee that the bytchr-es8316 driver, which might set this 698 * property, will probe before us. 699 */ 700 es8316->jd_inverted = device_property_read_bool(component->dev, 701 "everest,jack-detect-inverted"); 702 703 mutex_lock(&es8316->lock); 704 705 es8316->jack = jack; 706 707 if (es8316->jack->status & SND_JACK_MICROPHONE) 708 es8316_enable_micbias_for_mic_gnd_short_detect(component); 709 710 snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE, 711 ES8316_GPIO_ENABLE_INTERRUPT, 712 ES8316_GPIO_ENABLE_INTERRUPT); 713 714 mutex_unlock(&es8316->lock); 715 716 /* Enable irq and sync initial jack state */ 717 enable_irq(es8316->irq); 718 es8316_irq(es8316->irq, es8316); 719 } 720 721 static void es8316_disable_jack_detect(struct snd_soc_component *component) 722 { 723 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); 724 725 if (!es8316->jack) 726 return; /* Already disabled (or never enabled) */ 727 728 disable_irq(es8316->irq); 729 730 mutex_lock(&es8316->lock); 731 732 snd_soc_component_update_bits(component, ES8316_GPIO_DEBOUNCE, 733 ES8316_GPIO_ENABLE_INTERRUPT, 0); 734 735 if (es8316->jack->status & SND_JACK_MICROPHONE) { 736 es8316_disable_micbias_for_mic_gnd_short_detect(component); 737 snd_soc_jack_report(es8316->jack, 0, SND_JACK_BTN_0); 738 } 739 740 es8316->jack = NULL; 741 742 mutex_unlock(&es8316->lock); 743 } 744 745 static int es8316_set_jack(struct snd_soc_component *component, 746 struct snd_soc_jack *jack, void *data) 747 { 748 if (jack) 749 es8316_enable_jack_detect(component, jack); 750 else 751 es8316_disable_jack_detect(component); 752 753 return 0; 754 } 755 756 static int es8316_probe(struct snd_soc_component *component) 757 { 758 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); 759 int ret; 760 761 es8316->component = component; 762 763 es8316->mclk = devm_clk_get_optional(component->dev, "mclk"); 764 if (IS_ERR(es8316->mclk)) { 765 dev_err(component->dev, "unable to get mclk\n"); 766 return PTR_ERR(es8316->mclk); 767 } 768 if (!es8316->mclk) 769 dev_warn(component->dev, "assuming static mclk\n"); 770 771 ret = clk_prepare_enable(es8316->mclk); 772 if (ret) { 773 dev_err(component->dev, "unable to enable mclk\n"); 774 return ret; 775 } 776 777 /* Reset codec and enable current state machine */ 778 snd_soc_component_write(component, ES8316_RESET, 0x3f); 779 usleep_range(5000, 5500); 780 snd_soc_component_write(component, ES8316_RESET, ES8316_RESET_CSM_ON); 781 msleep(30); 782 783 /* 784 * Documentation is unclear, but this value from the vendor driver is 785 * needed otherwise audio output is silent. 786 */ 787 snd_soc_component_write(component, ES8316_SYS_VMIDSEL, 0xff); 788 789 /* 790 * Documentation for this register is unclear and incomplete, 791 * but here is a vendor-provided value that improves volume 792 * and quality for Intel CHT platforms. 793 */ 794 snd_soc_component_write(component, ES8316_CLKMGR_ADCOSR, 0x32); 795 796 return 0; 797 } 798 799 static void es8316_remove(struct snd_soc_component *component) 800 { 801 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); 802 803 clk_disable_unprepare(es8316->mclk); 804 } 805 806 static int es8316_resume(struct snd_soc_component *component) 807 { 808 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); 809 810 regcache_cache_only(es8316->regmap, false); 811 regcache_sync(es8316->regmap); 812 813 return 0; 814 } 815 816 static int es8316_suspend(struct snd_soc_component *component) 817 { 818 struct es8316_priv *es8316 = snd_soc_component_get_drvdata(component); 819 820 regcache_cache_only(es8316->regmap, true); 821 regcache_mark_dirty(es8316->regmap); 822 823 return 0; 824 } 825 826 static const struct snd_soc_component_driver soc_component_dev_es8316 = { 827 .probe = es8316_probe, 828 .remove = es8316_remove, 829 .resume = es8316_resume, 830 .suspend = es8316_suspend, 831 .set_jack = es8316_set_jack, 832 .controls = es8316_snd_controls, 833 .num_controls = ARRAY_SIZE(es8316_snd_controls), 834 .dapm_widgets = es8316_dapm_widgets, 835 .num_dapm_widgets = ARRAY_SIZE(es8316_dapm_widgets), 836 .dapm_routes = es8316_dapm_routes, 837 .num_dapm_routes = ARRAY_SIZE(es8316_dapm_routes), 838 .use_pmdown_time = 1, 839 .endianness = 1, 840 }; 841 842 static bool es8316_volatile_reg(struct device *dev, unsigned int reg) 843 { 844 switch (reg) { 845 case ES8316_GPIO_FLAG: 846 return true; 847 default: 848 return false; 849 } 850 } 851 852 static const struct regmap_config es8316_regmap = { 853 .reg_bits = 8, 854 .val_bits = 8, 855 .use_single_read = true, 856 .use_single_write = true, 857 .max_register = 0x53, 858 .volatile_reg = es8316_volatile_reg, 859 .cache_type = REGCACHE_MAPLE, 860 }; 861 862 static int es8316_i2c_probe(struct i2c_client *i2c_client) 863 { 864 struct device *dev = &i2c_client->dev; 865 struct es8316_priv *es8316; 866 int ret; 867 868 es8316 = devm_kzalloc(&i2c_client->dev, sizeof(struct es8316_priv), 869 GFP_KERNEL); 870 if (es8316 == NULL) 871 return -ENOMEM; 872 873 i2c_set_clientdata(i2c_client, es8316); 874 875 es8316->regmap = devm_regmap_init_i2c(i2c_client, &es8316_regmap); 876 if (IS_ERR(es8316->regmap)) 877 return PTR_ERR(es8316->regmap); 878 879 es8316->irq = i2c_client->irq; 880 mutex_init(&es8316->lock); 881 882 if (es8316->irq > 0) { 883 ret = devm_request_threaded_irq(dev, es8316->irq, NULL, es8316_irq, 884 IRQF_TRIGGER_HIGH | IRQF_ONESHOT | IRQF_NO_AUTOEN, 885 "es8316", es8316); 886 if (ret) { 887 dev_warn(dev, "Failed to get IRQ %d: %d\n", es8316->irq, ret); 888 es8316->irq = -ENXIO; 889 } 890 } 891 892 return devm_snd_soc_register_component(&i2c_client->dev, 893 &soc_component_dev_es8316, 894 &es8316_dai, 1); 895 } 896 897 static const struct i2c_device_id es8316_i2c_id[] = { 898 {"es8316" }, 899 {} 900 }; 901 MODULE_DEVICE_TABLE(i2c, es8316_i2c_id); 902 903 #ifdef CONFIG_OF 904 static const struct of_device_id es8316_of_match[] = { 905 { .compatible = "everest,es8316", }, 906 {}, 907 }; 908 MODULE_DEVICE_TABLE(of, es8316_of_match); 909 #endif 910 911 #ifdef CONFIG_ACPI 912 static const struct acpi_device_id es8316_acpi_match[] = { 913 {"ESSX8316", 0}, 914 {"ESSX8336", 0}, 915 {}, 916 }; 917 MODULE_DEVICE_TABLE(acpi, es8316_acpi_match); 918 #endif 919 920 static struct i2c_driver es8316_i2c_driver = { 921 .driver = { 922 .name = "es8316", 923 .acpi_match_table = ACPI_PTR(es8316_acpi_match), 924 .of_match_table = of_match_ptr(es8316_of_match), 925 }, 926 .probe = es8316_i2c_probe, 927 .id_table = es8316_i2c_id, 928 }; 929 module_i2c_driver(es8316_i2c_driver); 930 931 MODULE_DESCRIPTION("Everest Semi ES8316 ALSA SoC Codec Driver"); 932 MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>"); 933 MODULE_LICENSE("GPL v2"); 934