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