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