1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // rt711-sdca.c -- rt711 SDCA ALSA SoC audio driver 4 // 5 // Copyright(c) 2021 Realtek Semiconductor Corp. 6 // 7 // 8 9 #include <linux/module.h> 10 #include <linux/moduleparam.h> 11 #include <linux/kernel.h> 12 #include <linux/init.h> 13 #include <linux/delay.h> 14 #include <linux/pm_runtime.h> 15 #include <linux/soundwire/sdw_registers.h> 16 #include <linux/slab.h> 17 #include <linux/bitops.h> 18 #include <sound/core.h> 19 #include <sound/pcm.h> 20 #include <sound/pcm_params.h> 21 #include <sound/soc-dapm.h> 22 #include <sound/initval.h> 23 #include <sound/tlv.h> 24 #include <sound/jack.h> 25 26 #include "rt711-sdca.h" 27 28 static int rt711_sdca_index_write(struct rt711_sdca_priv *rt711, 29 unsigned int nid, unsigned int reg, unsigned int value) 30 { 31 int ret; 32 struct regmap *regmap = rt711->mbq_regmap; 33 unsigned int addr = (nid << 20) | reg; 34 35 ret = regmap_write(regmap, addr, value); 36 if (ret < 0) 37 dev_err(&rt711->slave->dev, 38 "Failed to set private value: %06x <= %04x ret=%d\n", 39 addr, value, ret); 40 41 return ret; 42 } 43 44 static int rt711_sdca_index_read(struct rt711_sdca_priv *rt711, 45 unsigned int nid, unsigned int reg, unsigned int *value) 46 { 47 int ret; 48 struct regmap *regmap = rt711->mbq_regmap; 49 unsigned int addr = (nid << 20) | reg; 50 51 ret = regmap_read(regmap, addr, value); 52 if (ret < 0) 53 dev_err(&rt711->slave->dev, 54 "Failed to get private value: %06x => %04x ret=%d\n", 55 addr, *value, ret); 56 57 return ret; 58 } 59 60 static int rt711_sdca_index_update_bits(struct rt711_sdca_priv *rt711, 61 unsigned int nid, unsigned int reg, unsigned int mask, unsigned int val) 62 { 63 unsigned int tmp; 64 int ret; 65 66 ret = rt711_sdca_index_read(rt711, nid, reg, &tmp); 67 if (ret < 0) 68 return ret; 69 70 set_mask_bits(&tmp, mask, val); 71 return rt711_sdca_index_write(rt711, nid, reg, tmp); 72 } 73 74 static void rt711_sdca_reset(struct rt711_sdca_priv *rt711) 75 { 76 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 77 RT711_PARA_VERB_CTL, RT711_HIDDEN_REG_SW_RESET, 78 RT711_HIDDEN_REG_SW_RESET); 79 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 80 RT711_HDA_LEGACY_RESET_CTL, 0x1, 0x1); 81 } 82 83 static int rt711_sdca_calibration(struct rt711_sdca_priv *rt711) 84 { 85 unsigned int val, loop_rc = 0, loop_dc = 0; 86 struct device *dev; 87 struct regmap *regmap = rt711->regmap; 88 int chk_cnt = 100; 89 int ret = 0; 90 91 mutex_lock(&rt711->calibrate_mutex); 92 dev = regmap_get_device(regmap); 93 94 regmap_read(rt711->regmap, RT711_RC_CAL_STATUS, &val); 95 /* RC calibration */ 96 if (!(val & 0x40)) 97 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_ANALOG_CTL, 98 RT711_MISC_POWER_CTL0, 0x0010, 0x0010); 99 100 for (loop_rc = 0; loop_rc < chk_cnt && !(val & 0x40); loop_rc++) { 101 usleep_range(10000, 11000); 102 ret = regmap_read(rt711->regmap, RT711_RC_CAL_STATUS, &val); 103 if (ret < 0) 104 goto _cali_fail_; 105 } 106 if (loop_rc == chk_cnt) 107 dev_err(dev, "%s, RC calibration time-out!\n", __func__); 108 109 /* HP calibration by manual mode setting */ 110 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 111 RT711_FSM_CTL, 0x2000, 0x2000); 112 113 /* Calibration manual mode */ 114 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 115 RT711_FSM_CTL, 0xf, RT711_CALI_CTL); 116 117 /* reset HP calibration */ 118 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_CALI, 119 RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_FORCE_CALI_RST, 0x00); 120 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_CALI, 121 RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_FORCE_CALI_RST, 122 RT711_DAC_DC_FORCE_CALI_RST); 123 124 /* cal_clk_en_reg */ 125 if (rt711->hw_ver == RT711_VER_VD0) 126 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_CALI, 127 RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_CALI_CLK_EN, 128 RT711_DAC_DC_CALI_CLK_EN); 129 130 /* trigger */ 131 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_CALI, 132 RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_CALI_TRIGGER, 133 RT711_DAC_DC_CALI_TRIGGER); 134 135 /* wait for calibration process */ 136 rt711_sdca_index_read(rt711, RT711_VENDOR_CALI, 137 RT711_DAC_DC_CALI_CTL1, &val); 138 139 for (loop_dc = 0; loop_dc < chk_cnt && 140 (val & RT711_DAC_DC_CALI_TRIGGER); loop_dc++) { 141 usleep_range(10000, 11000); 142 ret = rt711_sdca_index_read(rt711, RT711_VENDOR_CALI, 143 RT711_DAC_DC_CALI_CTL1, &val); 144 if (ret < 0) 145 goto _cali_fail_; 146 } 147 if (loop_dc == chk_cnt) 148 dev_err(dev, "%s, calibration time-out!\n", __func__); 149 150 if (loop_dc == chk_cnt || loop_rc == chk_cnt) 151 ret = -ETIMEDOUT; 152 153 _cali_fail_: 154 /* enable impedance sense */ 155 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 156 RT711_FSM_CTL, RT711_FSM_IMP_EN, RT711_FSM_IMP_EN); 157 158 /* release HP-JD and trigger FSM */ 159 rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 160 RT711_DIGITAL_MISC_CTRL4, 0x201b); 161 162 mutex_unlock(&rt711->calibrate_mutex); 163 dev_dbg(dev, "%s calibration complete, ret=%d\n", __func__, ret); 164 return ret; 165 } 166 167 static unsigned int rt711_sdca_button_detect(struct rt711_sdca_priv *rt711) 168 { 169 unsigned int btn_type = 0, offset, idx, val, owner; 170 int ret; 171 unsigned char buf[3]; 172 173 /* get current UMP message owner */ 174 ret = regmap_read(rt711->regmap, 175 SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_CURRENT_OWNER, 0), 176 &owner); 177 if (ret < 0) 178 return 0; 179 180 /* if owner is device then there is no button event from device */ 181 if (owner == 1) 182 return 0; 183 184 /* read UMP message offset */ 185 ret = regmap_read(rt711->regmap, 186 SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_MESSAGE_OFFSET, 0), 187 &offset); 188 if (ret < 0) 189 goto _end_btn_det_; 190 191 for (idx = 0; idx < sizeof(buf); idx++) { 192 ret = regmap_read(rt711->regmap, 193 RT711_BUF_ADDR_HID1 + offset + idx, &val); 194 if (ret < 0) 195 goto _end_btn_det_; 196 buf[idx] = val & 0xff; 197 } 198 199 if (buf[0] == 0x11) { 200 switch (buf[1] & 0xf0) { 201 case 0x10: 202 btn_type |= SND_JACK_BTN_2; 203 break; 204 case 0x20: 205 btn_type |= SND_JACK_BTN_3; 206 break; 207 case 0x40: 208 btn_type |= SND_JACK_BTN_0; 209 break; 210 case 0x80: 211 btn_type |= SND_JACK_BTN_1; 212 break; 213 } 214 switch (buf[2]) { 215 case 0x01: 216 case 0x10: 217 btn_type |= SND_JACK_BTN_2; 218 break; 219 case 0x02: 220 case 0x20: 221 btn_type |= SND_JACK_BTN_3; 222 break; 223 case 0x04: 224 case 0x40: 225 btn_type |= SND_JACK_BTN_0; 226 break; 227 case 0x08: 228 case 0x80: 229 btn_type |= SND_JACK_BTN_1; 230 break; 231 } 232 } 233 234 _end_btn_det_: 235 /* Host is owner, so set back to device */ 236 if (owner == 0) 237 /* set owner to device */ 238 regmap_write(rt711->regmap, 239 SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, 240 RT711_SDCA_CTL_HIDTX_SET_OWNER_TO_DEVICE, 0), 0x01); 241 242 return btn_type; 243 } 244 245 static int rt711_sdca_headset_detect(struct rt711_sdca_priv *rt711) 246 { 247 unsigned int det_mode; 248 int ret; 249 250 /* get detected_mode */ 251 ret = regmap_read(rt711->regmap, 252 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_DETECTED_MODE, 0), 253 &det_mode); 254 if (ret < 0) 255 goto io_error; 256 257 switch (det_mode) { 258 case 0x00: 259 rt711->jack_type = 0; 260 break; 261 case 0x03: 262 rt711->jack_type = SND_JACK_HEADPHONE; 263 break; 264 case 0x05: 265 rt711->jack_type = SND_JACK_HEADSET; 266 break; 267 } 268 269 /* write selected_mode */ 270 if (det_mode) { 271 ret = regmap_write(rt711->regmap, 272 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_SELECTED_MODE, 0), 273 det_mode); 274 if (ret < 0) 275 goto io_error; 276 } 277 278 dev_dbg(&rt711->slave->dev, 279 "%s, detected_mode=0x%x\n", __func__, det_mode); 280 281 return 0; 282 283 io_error: 284 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); 285 return ret; 286 } 287 288 static void rt711_sdca_jack_detect_handler(struct work_struct *work) 289 { 290 struct rt711_sdca_priv *rt711 = 291 container_of(work, struct rt711_sdca_priv, jack_detect_work.work); 292 int btn_type = 0, ret; 293 294 if (!rt711->hs_jack) 295 return; 296 297 if (!rt711->component->card || !rt711->component->card->instantiated) 298 return; 299 300 /* SDW_SCP_SDCA_INT_SDCA_0 is used for jack detection */ 301 if (rt711->scp_sdca_stat1 & SDW_SCP_SDCA_INT_SDCA_0) { 302 ret = rt711_sdca_headset_detect(rt711); 303 if (ret < 0) 304 return; 305 } 306 307 /* SDW_SCP_SDCA_INT_SDCA_8 is used for button detection */ 308 if (rt711->scp_sdca_stat2 & SDW_SCP_SDCA_INT_SDCA_8) 309 btn_type = rt711_sdca_button_detect(rt711); 310 311 if (rt711->jack_type == 0) 312 btn_type = 0; 313 314 dev_dbg(&rt711->slave->dev, 315 "in %s, jack_type=0x%x\n", __func__, rt711->jack_type); 316 dev_dbg(&rt711->slave->dev, 317 "in %s, btn_type=0x%x\n", __func__, btn_type); 318 dev_dbg(&rt711->slave->dev, 319 "in %s, scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__, 320 rt711->scp_sdca_stat1, rt711->scp_sdca_stat2); 321 322 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type, 323 SND_JACK_HEADSET | 324 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 325 SND_JACK_BTN_2 | SND_JACK_BTN_3); 326 327 if (btn_type) { 328 /* button released */ 329 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type, 330 SND_JACK_HEADSET | 331 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 332 SND_JACK_BTN_2 | SND_JACK_BTN_3); 333 334 mod_delayed_work(system_power_efficient_wq, 335 &rt711->jack_btn_check_work, msecs_to_jiffies(200)); 336 } 337 } 338 339 static void rt711_sdca_btn_check_handler(struct work_struct *work) 340 { 341 struct rt711_sdca_priv *rt711 = 342 container_of(work, struct rt711_sdca_priv, jack_btn_check_work.work); 343 int btn_type = 0, ret, idx; 344 unsigned int det_mode, offset, val; 345 unsigned char buf[3]; 346 347 ret = regmap_read(rt711->regmap, 348 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_DETECTED_MODE, 0), 349 &det_mode); 350 if (ret < 0) 351 goto io_error; 352 353 /* pin attached */ 354 if (det_mode) { 355 /* read UMP message offset */ 356 ret = regmap_read(rt711->regmap, 357 SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_MESSAGE_OFFSET, 0), 358 &offset); 359 if (ret < 0) 360 goto io_error; 361 362 for (idx = 0; idx < sizeof(buf); idx++) { 363 ret = regmap_read(rt711->regmap, 364 RT711_BUF_ADDR_HID1 + offset + idx, &val); 365 if (ret < 0) 366 goto io_error; 367 buf[idx] = val & 0xff; 368 } 369 370 if (buf[0] == 0x11) { 371 switch (buf[1] & 0xf0) { 372 case 0x10: 373 btn_type |= SND_JACK_BTN_2; 374 break; 375 case 0x20: 376 btn_type |= SND_JACK_BTN_3; 377 break; 378 case 0x40: 379 btn_type |= SND_JACK_BTN_0; 380 break; 381 case 0x80: 382 btn_type |= SND_JACK_BTN_1; 383 break; 384 } 385 switch (buf[2]) { 386 case 0x01: 387 case 0x10: 388 btn_type |= SND_JACK_BTN_2; 389 break; 390 case 0x02: 391 case 0x20: 392 btn_type |= SND_JACK_BTN_3; 393 break; 394 case 0x04: 395 case 0x40: 396 btn_type |= SND_JACK_BTN_0; 397 break; 398 case 0x08: 399 case 0x80: 400 btn_type |= SND_JACK_BTN_1; 401 break; 402 } 403 } 404 } else 405 rt711->jack_type = 0; 406 407 dev_dbg(&rt711->slave->dev, "%s, btn_type=0x%x\n", __func__, btn_type); 408 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type, 409 SND_JACK_HEADSET | 410 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 411 SND_JACK_BTN_2 | SND_JACK_BTN_3); 412 413 if (btn_type) { 414 /* button released */ 415 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type, 416 SND_JACK_HEADSET | 417 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 418 SND_JACK_BTN_2 | SND_JACK_BTN_3); 419 420 mod_delayed_work(system_power_efficient_wq, 421 &rt711->jack_btn_check_work, msecs_to_jiffies(200)); 422 } 423 424 return; 425 426 io_error: 427 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); 428 } 429 430 static void rt711_sdca_jack_init(struct rt711_sdca_priv *rt711) 431 { 432 mutex_lock(&rt711->calibrate_mutex); 433 434 if (rt711->hs_jack) { 435 /* Enable HID1 event & set button RTC mode */ 436 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 437 RT711_PUSH_BTN_INT_CTL6, 0x80f0, 0x8000); 438 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 439 RT711_PUSH_BTN_INT_CTL2, 0x11dd, 0x11dd); 440 rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL, 441 RT711_PUSH_BTN_INT_CTL7, 0xffff); 442 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 443 RT711_PUSH_BTN_INT_CTL9, 0xf000, 0x0000); 444 445 /* GE_mode_change_event_en & Hid1_push_button_event_en */ 446 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 447 RT711_GE_MODE_RELATED_CTL, 0x0c00, 0x0c00); 448 449 switch (rt711->jd_src) { 450 case RT711_JD1: 451 /* default settings was already for JD1 */ 452 break; 453 case RT711_JD2: 454 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 455 RT711_JD_CTL1, RT711_JD2_DIGITAL_MODE_SEL, 456 RT711_JD2_DIGITAL_MODE_SEL); 457 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 458 RT711_JD_CTL2, RT711_JD2_2PORT_200K_DECODE_HP | RT711_HP_JD_SEL_JD2, 459 RT711_JD2_2PORT_200K_DECODE_HP | RT711_HP_JD_SEL_JD2); 460 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 461 RT711_CC_DET1, 462 RT711_HP_JD_FINAL_RESULT_CTL_JD12, 463 RT711_HP_JD_FINAL_RESULT_CTL_JD12); 464 break; 465 default: 466 dev_warn(rt711->component->dev, "Wrong JD source\n"); 467 break; 468 } 469 470 /* set SCP_SDCA_IntMask1[0]=1 */ 471 sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK1, SDW_SCP_SDCA_INTMASK_SDCA_0); 472 /* set SCP_SDCA_IntMask2[0]=1 */ 473 sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK2, SDW_SCP_SDCA_INTMASK_SDCA_8); 474 dev_dbg(&rt711->slave->dev, "in %s enable\n", __func__); 475 } else { 476 /* disable HID 1/2 event */ 477 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 478 RT711_GE_MODE_RELATED_CTL, 0x0c00, 0x0000); 479 480 dev_dbg(&rt711->slave->dev, "in %s disable\n", __func__); 481 } 482 483 mutex_unlock(&rt711->calibrate_mutex); 484 } 485 486 static int rt711_sdca_set_jack_detect(struct snd_soc_component *component, 487 struct snd_soc_jack *hs_jack, void *data) 488 { 489 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 490 int ret; 491 492 rt711->hs_jack = hs_jack; 493 494 ret = pm_runtime_resume_and_get(component->dev); 495 if (ret < 0) { 496 if (ret != -EACCES) { 497 dev_err(component->dev, "%s: failed to resume %d\n", __func__, ret); 498 return ret; 499 } 500 501 /* pm_runtime not enabled yet */ 502 dev_dbg(component->dev, "%s: skipping jack init for now\n", __func__); 503 return 0; 504 } 505 506 rt711_sdca_jack_init(rt711); 507 508 pm_runtime_mark_last_busy(component->dev); 509 pm_runtime_put_autosuspend(component->dev); 510 511 return 0; 512 } 513 514 /* For SDCA control DAC/ADC Gain */ 515 static int rt711_sdca_set_gain_put(struct snd_kcontrol *kcontrol, 516 struct snd_ctl_elem_value *ucontrol) 517 { 518 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 519 struct soc_mixer_control *mc = 520 (struct soc_mixer_control *)kcontrol->private_value; 521 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 522 unsigned int read_l, read_r, gain_l_val, gain_r_val; 523 unsigned int i, adc_vol_flag = 0, changed = 0; 524 unsigned int lvalue, rvalue; 525 526 if (strstr(ucontrol->id.name, "FU1E Capture Volume") || 527 strstr(ucontrol->id.name, "FU0F Capture Volume")) 528 adc_vol_flag = 1; 529 530 regmap_read(rt711->mbq_regmap, mc->reg, &lvalue); 531 regmap_read(rt711->mbq_regmap, mc->rreg, &rvalue); 532 533 /* control value to 2's complement value */ 534 /* L Channel */ 535 gain_l_val = ucontrol->value.integer.value[0]; 536 if (gain_l_val > mc->max) 537 gain_l_val = mc->max; 538 read_l = gain_l_val; 539 540 if (mc->shift == 8) /* boost gain */ 541 gain_l_val = (gain_l_val * 10) << mc->shift; 542 else { /* ADC/DAC gain */ 543 if (adc_vol_flag && gain_l_val > mc->shift) 544 gain_l_val = (gain_l_val - mc->shift) * 75; 545 else 546 gain_l_val = (mc->shift - gain_l_val) * 75; 547 gain_l_val <<= 8; 548 gain_l_val /= 100; 549 if (!(adc_vol_flag && read_l > mc->shift)) { 550 gain_l_val = ~gain_l_val; 551 gain_l_val += 1; 552 } 553 gain_l_val &= 0xffff; 554 } 555 556 /* R Channel */ 557 gain_r_val = ucontrol->value.integer.value[1]; 558 if (gain_r_val > mc->max) 559 gain_r_val = mc->max; 560 read_r = gain_r_val; 561 562 if (mc->shift == 8) /* boost gain */ 563 gain_r_val = (gain_r_val * 10) << mc->shift; 564 else { /* ADC/DAC gain */ 565 if (adc_vol_flag && gain_r_val > mc->shift) 566 gain_r_val = (gain_r_val - mc->shift) * 75; 567 else 568 gain_r_val = (mc->shift - gain_r_val) * 75; 569 gain_r_val <<= 8; 570 gain_r_val /= 100; 571 if (!(adc_vol_flag && read_r > mc->shift)) { 572 gain_r_val = ~gain_r_val; 573 gain_r_val += 1; 574 } 575 gain_r_val &= 0xffff; 576 } 577 578 if (lvalue != gain_l_val || rvalue != gain_r_val) 579 changed = 1; 580 else 581 return 0; 582 583 for (i = 0; i < 3; i++) { /* retry 3 times at most */ 584 /* Lch*/ 585 regmap_write(rt711->mbq_regmap, mc->reg, gain_l_val); 586 587 /* Rch */ 588 regmap_write(rt711->mbq_regmap, mc->rreg, gain_r_val); 589 590 regmap_read(rt711->mbq_regmap, mc->reg, &read_l); 591 regmap_read(rt711->mbq_regmap, mc->rreg, &read_r); 592 if (read_r == gain_r_val && read_l == gain_l_val) 593 break; 594 } 595 596 return i == 3 ? -EIO : changed; 597 } 598 599 static int rt711_sdca_set_gain_get(struct snd_kcontrol *kcontrol, 600 struct snd_ctl_elem_value *ucontrol) 601 { 602 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 603 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 604 struct soc_mixer_control *mc = 605 (struct soc_mixer_control *)kcontrol->private_value; 606 unsigned int read_l, read_r, ctl_l = 0, ctl_r = 0; 607 unsigned int adc_vol_flag = 0, neg_flag = 0; 608 609 if (strstr(ucontrol->id.name, "FU1E Capture Volume") || 610 strstr(ucontrol->id.name, "FU0F Capture Volume")) 611 adc_vol_flag = 1; 612 613 regmap_read(rt711->mbq_regmap, mc->reg, &read_l); 614 regmap_read(rt711->mbq_regmap, mc->rreg, &read_r); 615 616 /* 2's complement value to control value */ 617 if (mc->shift == 8) /* boost gain */ 618 ctl_l = (read_l >> mc->shift) / 10; 619 else { /* ADC/DAC gain */ 620 ctl_l = read_l; 621 if (read_l & BIT(15)) { 622 ctl_l = 0xffff & ~(read_l - 1); 623 neg_flag = 1; 624 } 625 ctl_l *= 100; 626 ctl_l >>= 8; 627 if (adc_vol_flag) { 628 if (neg_flag) 629 ctl_l = mc->shift - (ctl_l / 75); 630 else 631 ctl_l = mc->shift + (ctl_l / 75); 632 } else 633 ctl_l = mc->max - (ctl_l / 75); 634 } 635 636 neg_flag = 0; 637 if (read_l != read_r) { 638 if (mc->shift == 8) /* boost gain */ 639 ctl_r = (read_r >> mc->shift) / 10; 640 else { /* ADC/DAC gain */ 641 ctl_r = read_r; 642 if (read_r & BIT(15)) { 643 ctl_r = 0xffff & ~(read_r - 1); 644 neg_flag = 1; 645 } 646 ctl_r *= 100; 647 ctl_r >>= 8; 648 if (adc_vol_flag) { 649 if (neg_flag) 650 ctl_r = mc->shift - (ctl_r / 75); 651 else 652 ctl_r = mc->shift + (ctl_r / 75); 653 } else 654 ctl_r = mc->max - (ctl_r / 75); 655 } 656 } else 657 ctl_r = ctl_l; 658 659 ucontrol->value.integer.value[0] = ctl_l; 660 ucontrol->value.integer.value[1] = ctl_r; 661 662 return 0; 663 } 664 665 static int rt711_sdca_set_fu0f_capture_ctl(struct rt711_sdca_priv *rt711) 666 { 667 int err; 668 unsigned int ch_l, ch_r; 669 670 ch_l = (rt711->fu0f_dapm_mute || rt711->fu0f_mixer_l_mute) ? 0x01 : 0x00; 671 ch_r = (rt711->fu0f_dapm_mute || rt711->fu0f_mixer_r_mute) ? 0x01 : 0x00; 672 673 err = regmap_write(rt711->regmap, 674 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, 675 RT711_SDCA_CTL_FU_MUTE, CH_L), ch_l); 676 if (err < 0) 677 return err; 678 679 err = regmap_write(rt711->regmap, 680 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, 681 RT711_SDCA_CTL_FU_MUTE, CH_R), ch_r); 682 if (err < 0) 683 return err; 684 685 return 0; 686 } 687 688 static int rt711_sdca_set_fu1e_capture_ctl(struct rt711_sdca_priv *rt711) 689 { 690 int err; 691 unsigned int ch_l, ch_r; 692 693 ch_l = (rt711->fu1e_dapm_mute || rt711->fu1e_mixer_l_mute) ? 0x01 : 0x00; 694 ch_r = (rt711->fu1e_dapm_mute || rt711->fu1e_mixer_r_mute) ? 0x01 : 0x00; 695 696 err = regmap_write(rt711->regmap, 697 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, 698 RT711_SDCA_CTL_FU_MUTE, CH_L), ch_l); 699 if (err < 0) 700 return err; 701 702 err = regmap_write(rt711->regmap, 703 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, 704 RT711_SDCA_CTL_FU_MUTE, CH_R), ch_r); 705 if (err < 0) 706 return err; 707 708 return 0; 709 } 710 711 static int rt711_sdca_fu1e_capture_get(struct snd_kcontrol *kcontrol, 712 struct snd_ctl_elem_value *ucontrol) 713 { 714 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 715 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 716 717 ucontrol->value.integer.value[0] = !rt711->fu1e_mixer_l_mute; 718 ucontrol->value.integer.value[1] = !rt711->fu1e_mixer_r_mute; 719 return 0; 720 } 721 722 static int rt711_sdca_fu1e_capture_put(struct snd_kcontrol *kcontrol, 723 struct snd_ctl_elem_value *ucontrol) 724 { 725 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 726 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 727 int err, changed = 0; 728 729 if (rt711->fu1e_mixer_l_mute != !ucontrol->value.integer.value[0] || 730 rt711->fu1e_mixer_r_mute != !ucontrol->value.integer.value[1]) 731 changed = 1; 732 733 rt711->fu1e_mixer_l_mute = !ucontrol->value.integer.value[0]; 734 rt711->fu1e_mixer_r_mute = !ucontrol->value.integer.value[1]; 735 err = rt711_sdca_set_fu1e_capture_ctl(rt711); 736 if (err < 0) 737 return err; 738 739 return changed; 740 } 741 742 static int rt711_sdca_fu0f_capture_get(struct snd_kcontrol *kcontrol, 743 struct snd_ctl_elem_value *ucontrol) 744 { 745 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 746 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 747 748 ucontrol->value.integer.value[0] = !rt711->fu0f_mixer_l_mute; 749 ucontrol->value.integer.value[1] = !rt711->fu0f_mixer_r_mute; 750 return 0; 751 } 752 753 static int rt711_sdca_fu0f_capture_put(struct snd_kcontrol *kcontrol, 754 struct snd_ctl_elem_value *ucontrol) 755 { 756 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 757 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 758 int err, changed = 0; 759 760 if (rt711->fu0f_mixer_l_mute != !ucontrol->value.integer.value[0] || 761 rt711->fu0f_mixer_r_mute != !ucontrol->value.integer.value[1]) 762 changed = 1; 763 764 rt711->fu0f_mixer_l_mute = !ucontrol->value.integer.value[0]; 765 rt711->fu0f_mixer_r_mute = !ucontrol->value.integer.value[1]; 766 err = rt711_sdca_set_fu0f_capture_ctl(rt711); 767 if (err < 0) 768 return err; 769 770 return changed; 771 } 772 773 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0); 774 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0); 775 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0); 776 777 static const struct snd_kcontrol_new rt711_sdca_snd_controls[] = { 778 SOC_DOUBLE_R_EXT_TLV("FU05 Playback Volume", 779 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_VOLUME, CH_L), 780 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_VOLUME, CH_R), 781 0x57, 0x57, 0, 782 rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, out_vol_tlv), 783 SOC_DOUBLE_EXT("FU1E Capture Switch", SND_SOC_NOPM, 0, 1, 1, 0, 784 rt711_sdca_fu1e_capture_get, rt711_sdca_fu1e_capture_put), 785 SOC_DOUBLE_EXT("FU0F Capture Switch", SND_SOC_NOPM, 0, 1, 1, 0, 786 rt711_sdca_fu0f_capture_get, rt711_sdca_fu0f_capture_put), 787 SOC_DOUBLE_R_EXT_TLV("FU1E Capture Volume", 788 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_VOLUME, CH_L), 789 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_VOLUME, CH_R), 790 0x17, 0x3f, 0, 791 rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, in_vol_tlv), 792 SOC_DOUBLE_R_EXT_TLV("FU0F Capture Volume", 793 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_VOLUME, CH_L), 794 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_VOLUME, CH_R), 795 0x17, 0x3f, 0, 796 rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, in_vol_tlv), 797 SOC_DOUBLE_R_EXT_TLV("FU44 Gain Volume", 798 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PLATFORM_FU44, RT711_SDCA_CTL_FU_CH_GAIN, CH_L), 799 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PLATFORM_FU44, RT711_SDCA_CTL_FU_CH_GAIN, CH_R), 800 8, 3, 0, 801 rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, mic_vol_tlv), 802 SOC_DOUBLE_R_EXT_TLV("FU15 Gain Volume", 803 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PLATFORM_FU15, RT711_SDCA_CTL_FU_CH_GAIN, CH_L), 804 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PLATFORM_FU15, RT711_SDCA_CTL_FU_CH_GAIN, CH_R), 805 8, 3, 0, 806 rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, mic_vol_tlv), 807 }; 808 809 static int rt711_sdca_mux_get(struct snd_kcontrol *kcontrol, 810 struct snd_ctl_elem_value *ucontrol) 811 { 812 struct snd_soc_component *component = 813 snd_soc_dapm_kcontrol_component(kcontrol); 814 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 815 unsigned int val = 0, mask_sft; 816 817 if (strstr(ucontrol->id.name, "ADC 22 Mux")) 818 mask_sft = 10; 819 else if (strstr(ucontrol->id.name, "ADC 23 Mux")) 820 mask_sft = 13; 821 else 822 return -EINVAL; 823 824 rt711_sdca_index_read(rt711, RT711_VENDOR_HDA_CTL, 825 RT711_HDA_LEGACY_MUX_CTL1, &val); 826 827 ucontrol->value.enumerated.item[0] = (val >> mask_sft) & 0x7; 828 829 return 0; 830 } 831 832 static int rt711_sdca_mux_put(struct snd_kcontrol *kcontrol, 833 struct snd_ctl_elem_value *ucontrol) 834 { 835 struct snd_soc_component *component = 836 snd_soc_dapm_kcontrol_component(kcontrol); 837 struct snd_soc_dapm_context *dapm = 838 snd_soc_dapm_kcontrol_dapm(kcontrol); 839 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 840 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 841 unsigned int *item = ucontrol->value.enumerated.item; 842 unsigned int val, val2 = 0, change, mask_sft; 843 844 if (item[0] >= e->items) 845 return -EINVAL; 846 847 if (strstr(ucontrol->id.name, "ADC 22 Mux")) 848 mask_sft = 10; 849 else if (strstr(ucontrol->id.name, "ADC 23 Mux")) 850 mask_sft = 13; 851 else 852 return -EINVAL; 853 854 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; 855 856 rt711_sdca_index_read(rt711, RT711_VENDOR_HDA_CTL, 857 RT711_HDA_LEGACY_MUX_CTL1, &val2); 858 val2 = (val2 >> mask_sft) & 0x7; 859 860 if (val == val2) 861 change = 0; 862 else 863 change = 1; 864 865 if (change) 866 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 867 RT711_HDA_LEGACY_MUX_CTL1, 0x7 << mask_sft, 868 val << mask_sft); 869 870 snd_soc_dapm_mux_update_power(dapm, kcontrol, 871 item[0], e, NULL); 872 873 return change; 874 } 875 876 static const char * const adc_mux_text[] = { 877 "MIC2", 878 "LINE1", 879 "LINE2", 880 "DMIC", 881 }; 882 883 static SOC_ENUM_SINGLE_DECL( 884 rt711_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text); 885 886 static SOC_ENUM_SINGLE_DECL( 887 rt711_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text); 888 889 static const struct snd_kcontrol_new rt711_sdca_adc22_mux = 890 SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt711_adc22_enum, 891 rt711_sdca_mux_get, rt711_sdca_mux_put); 892 893 static const struct snd_kcontrol_new rt711_sdca_adc23_mux = 894 SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt711_adc23_enum, 895 rt711_sdca_mux_get, rt711_sdca_mux_put); 896 897 static int rt711_sdca_fu05_event(struct snd_soc_dapm_widget *w, 898 struct snd_kcontrol *kcontrol, int event) 899 { 900 struct snd_soc_component *component = 901 snd_soc_dapm_to_component(w->dapm); 902 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 903 unsigned char unmute = 0x0, mute = 0x1; 904 905 switch (event) { 906 case SND_SOC_DAPM_POST_PMU: 907 regmap_write(rt711->regmap, 908 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, 909 RT711_SDCA_CTL_FU_MUTE, CH_L), 910 unmute); 911 regmap_write(rt711->regmap, 912 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, 913 RT711_SDCA_CTL_FU_MUTE, CH_R), 914 unmute); 915 break; 916 case SND_SOC_DAPM_PRE_PMD: 917 regmap_write(rt711->regmap, 918 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, 919 RT711_SDCA_CTL_FU_MUTE, CH_L), 920 mute); 921 regmap_write(rt711->regmap, 922 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, 923 RT711_SDCA_CTL_FU_MUTE, CH_R), 924 mute); 925 break; 926 } 927 return 0; 928 } 929 930 static int rt711_sdca_fu0f_event(struct snd_soc_dapm_widget *w, 931 struct snd_kcontrol *kcontrol, int event) 932 { 933 struct snd_soc_component *component = 934 snd_soc_dapm_to_component(w->dapm); 935 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 936 937 switch (event) { 938 case SND_SOC_DAPM_POST_PMU: 939 rt711->fu0f_dapm_mute = false; 940 rt711_sdca_set_fu0f_capture_ctl(rt711); 941 break; 942 case SND_SOC_DAPM_PRE_PMD: 943 rt711->fu0f_dapm_mute = true; 944 rt711_sdca_set_fu0f_capture_ctl(rt711); 945 break; 946 } 947 return 0; 948 } 949 950 static int rt711_sdca_fu1e_event(struct snd_soc_dapm_widget *w, 951 struct snd_kcontrol *kcontrol, int event) 952 { 953 struct snd_soc_component *component = 954 snd_soc_dapm_to_component(w->dapm); 955 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 956 957 switch (event) { 958 case SND_SOC_DAPM_POST_PMU: 959 rt711->fu1e_dapm_mute = false; 960 rt711_sdca_set_fu1e_capture_ctl(rt711); 961 break; 962 case SND_SOC_DAPM_PRE_PMD: 963 rt711->fu1e_dapm_mute = true; 964 rt711_sdca_set_fu1e_capture_ctl(rt711); 965 break; 966 } 967 return 0; 968 } 969 970 static int rt711_sdca_pde28_event(struct snd_soc_dapm_widget *w, 971 struct snd_kcontrol *kcontrol, int event) 972 { 973 struct snd_soc_component *component = 974 snd_soc_dapm_to_component(w->dapm); 975 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 976 unsigned char ps0 = 0x0, ps3 = 0x3; 977 978 switch (event) { 979 case SND_SOC_DAPM_POST_PMU: 980 regmap_write(rt711->regmap, 981 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE28, 982 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 983 ps0); 984 break; 985 case SND_SOC_DAPM_PRE_PMD: 986 regmap_write(rt711->regmap, 987 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE28, 988 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 989 ps3); 990 break; 991 } 992 return 0; 993 } 994 995 static int rt711_sdca_pde29_event(struct snd_soc_dapm_widget *w, 996 struct snd_kcontrol *kcontrol, int event) 997 { 998 struct snd_soc_component *component = 999 snd_soc_dapm_to_component(w->dapm); 1000 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1001 unsigned char ps0 = 0x0, ps3 = 0x3; 1002 1003 switch (event) { 1004 case SND_SOC_DAPM_POST_PMU: 1005 regmap_write(rt711->regmap, 1006 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE29, 1007 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 1008 ps0); 1009 break; 1010 case SND_SOC_DAPM_PRE_PMD: 1011 regmap_write(rt711->regmap, 1012 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE29, 1013 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 1014 ps3); 1015 break; 1016 } 1017 return 0; 1018 } 1019 1020 static int rt711_sdca_pde2a_event(struct snd_soc_dapm_widget *w, 1021 struct snd_kcontrol *kcontrol, int event) 1022 { 1023 struct snd_soc_component *component = 1024 snd_soc_dapm_to_component(w->dapm); 1025 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1026 unsigned char ps0 = 0x0, ps3 = 0x3; 1027 1028 switch (event) { 1029 case SND_SOC_DAPM_POST_PMU: 1030 regmap_write(rt711->regmap, 1031 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PDE2A, 1032 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 1033 ps0); 1034 break; 1035 case SND_SOC_DAPM_PRE_PMD: 1036 regmap_write(rt711->regmap, 1037 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PDE2A, 1038 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 1039 ps3); 1040 break; 1041 } 1042 return 0; 1043 } 1044 1045 static int rt711_sdca_line1_power_event(struct snd_soc_dapm_widget *w, 1046 struct snd_kcontrol *kcontrol, int event) 1047 { 1048 struct snd_soc_component *component = 1049 snd_soc_dapm_to_component(w->dapm); 1050 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1051 static unsigned int sel_mode = 0xffff; 1052 1053 switch (event) { 1054 case SND_SOC_DAPM_POST_PMU: 1055 regmap_read(rt711->regmap, 1056 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, 1057 RT711_SDCA_CTL_SELECTED_MODE, 0), 1058 &sel_mode); 1059 regmap_write(rt711->regmap, 1060 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE1, 1061 RT711_SDCA_CTL_VENDOR_DEF, 0), 1062 0x1); 1063 regmap_write(rt711->regmap, 1064 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, 1065 RT711_SDCA_CTL_SELECTED_MODE, 0), 1066 0x7); 1067 break; 1068 case SND_SOC_DAPM_PRE_PMD: 1069 regmap_write(rt711->regmap, 1070 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE1, 1071 RT711_SDCA_CTL_VENDOR_DEF, 0), 1072 0x0); 1073 if (sel_mode != 0xffff) 1074 regmap_write(rt711->regmap, 1075 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, 1076 RT711_SDCA_CTL_SELECTED_MODE, 0), 1077 sel_mode); 1078 break; 1079 } 1080 1081 return 0; 1082 } 1083 1084 static int rt711_sdca_line2_power_event(struct snd_soc_dapm_widget *w, 1085 struct snd_kcontrol *kcontrol, int event) 1086 { 1087 struct snd_soc_component *component = 1088 snd_soc_dapm_to_component(w->dapm); 1089 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1090 unsigned char ps0 = 0x0, ps3 = 0x3; 1091 1092 switch (event) { 1093 case SND_SOC_DAPM_POST_PMU: 1094 regmap_write(rt711->regmap, 1095 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDELINE2, 1096 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 1097 ps0); 1098 regmap_write(rt711->regmap, 1099 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE2, 1100 RT711_SDCA_CTL_VENDOR_DEF, 0), 1101 0x1); 1102 break; 1103 case SND_SOC_DAPM_PRE_PMD: 1104 regmap_write(rt711->regmap, 1105 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE2, 1106 RT711_SDCA_CTL_VENDOR_DEF, 0), 1107 0x0); 1108 regmap_write(rt711->regmap, 1109 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDELINE2, 1110 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 1111 ps3); 1112 break; 1113 } 1114 1115 return 0; 1116 } 1117 1118 static const struct snd_soc_dapm_widget rt711_sdca_dapm_widgets[] = { 1119 SND_SOC_DAPM_OUTPUT("HP"), 1120 SND_SOC_DAPM_INPUT("MIC2"), 1121 SND_SOC_DAPM_INPUT("DMIC1"), 1122 SND_SOC_DAPM_INPUT("DMIC2"), 1123 SND_SOC_DAPM_INPUT("LINE1"), 1124 SND_SOC_DAPM_INPUT("LINE2"), 1125 1126 SND_SOC_DAPM_PGA_E("LINE1 Power", SND_SOC_NOPM, 1127 0, 0, NULL, 0, rt711_sdca_line1_power_event, 1128 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1129 SND_SOC_DAPM_PGA_E("LINE2 Power", SND_SOC_NOPM, 1130 0, 0, NULL, 0, rt711_sdca_line2_power_event, 1131 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1132 1133 SND_SOC_DAPM_SUPPLY("PDE 28", SND_SOC_NOPM, 0, 0, 1134 rt711_sdca_pde28_event, 1135 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1136 SND_SOC_DAPM_SUPPLY("PDE 29", SND_SOC_NOPM, 0, 0, 1137 rt711_sdca_pde29_event, 1138 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1139 SND_SOC_DAPM_SUPPLY("PDE 2A", SND_SOC_NOPM, 0, 0, 1140 rt711_sdca_pde2a_event, 1141 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1142 1143 SND_SOC_DAPM_DAC_E("FU 05", NULL, SND_SOC_NOPM, 0, 0, 1144 rt711_sdca_fu05_event, 1145 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1146 SND_SOC_DAPM_ADC_E("FU 0F", NULL, SND_SOC_NOPM, 0, 0, 1147 rt711_sdca_fu0f_event, 1148 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1149 SND_SOC_DAPM_ADC_E("FU 1E", NULL, SND_SOC_NOPM, 0, 0, 1150 rt711_sdca_fu1e_event, 1151 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1152 SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0, 1153 &rt711_sdca_adc22_mux), 1154 SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0, 1155 &rt711_sdca_adc23_mux), 1156 1157 SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0), 1158 SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0), 1159 SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0), 1160 }; 1161 1162 static const struct snd_soc_dapm_route rt711_sdca_audio_map[] = { 1163 {"FU 05", NULL, "DP3RX"}, 1164 {"DP2TX", NULL, "FU 0F"}, 1165 {"DP4TX", NULL, "FU 1E"}, 1166 1167 {"LINE1 Power", NULL, "LINE1"}, 1168 {"LINE2 Power", NULL, "LINE2"}, 1169 {"HP", NULL, "PDE 28"}, 1170 {"FU 0F", NULL, "PDE 29"}, 1171 {"FU 1E", NULL, "PDE 2A"}, 1172 1173 {"FU 0F", NULL, "ADC 22 Mux"}, 1174 {"FU 1E", NULL, "ADC 23 Mux"}, 1175 {"ADC 22 Mux", "DMIC", "DMIC1"}, 1176 {"ADC 22 Mux", "LINE1", "LINE1 Power"}, 1177 {"ADC 22 Mux", "LINE2", "LINE2 Power"}, 1178 {"ADC 22 Mux", "MIC2", "MIC2"}, 1179 {"ADC 23 Mux", "DMIC", "DMIC2"}, 1180 {"ADC 23 Mux", "LINE1", "LINE1 Power"}, 1181 {"ADC 23 Mux", "LINE2", "LINE2 Power"}, 1182 {"ADC 23 Mux", "MIC2", "MIC2"}, 1183 1184 {"HP", NULL, "FU 05"}, 1185 }; 1186 1187 static int rt711_sdca_parse_dt(struct rt711_sdca_priv *rt711, struct device *dev) 1188 { 1189 device_property_read_u32(dev, "realtek,jd-src", &rt711->jd_src); 1190 1191 return 0; 1192 } 1193 1194 static int rt711_sdca_probe(struct snd_soc_component *component) 1195 { 1196 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1197 1198 rt711_sdca_parse_dt(rt711, &rt711->slave->dev); 1199 rt711->component = component; 1200 1201 return 0; 1202 } 1203 1204 static const struct snd_soc_component_driver soc_sdca_dev_rt711 = { 1205 .probe = rt711_sdca_probe, 1206 .controls = rt711_sdca_snd_controls, 1207 .num_controls = ARRAY_SIZE(rt711_sdca_snd_controls), 1208 .dapm_widgets = rt711_sdca_dapm_widgets, 1209 .num_dapm_widgets = ARRAY_SIZE(rt711_sdca_dapm_widgets), 1210 .dapm_routes = rt711_sdca_audio_map, 1211 .num_dapm_routes = ARRAY_SIZE(rt711_sdca_audio_map), 1212 .set_jack = rt711_sdca_set_jack_detect, 1213 .endianness = 1, 1214 }; 1215 1216 static int rt711_sdca_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream, 1217 int direction) 1218 { 1219 struct sdw_stream_data *stream; 1220 1221 if (!sdw_stream) 1222 return 0; 1223 1224 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 1225 if (!stream) 1226 return -ENOMEM; 1227 1228 stream->sdw_stream = sdw_stream; 1229 1230 /* Use tx_mask or rx_mask to configure stream tag and set dma_data */ 1231 if (direction == SNDRV_PCM_STREAM_PLAYBACK) 1232 dai->playback_dma_data = stream; 1233 else 1234 dai->capture_dma_data = stream; 1235 1236 return 0; 1237 } 1238 1239 static void rt711_sdca_shutdown(struct snd_pcm_substream *substream, 1240 struct snd_soc_dai *dai) 1241 { 1242 struct sdw_stream_data *stream; 1243 1244 stream = snd_soc_dai_get_dma_data(dai, substream); 1245 snd_soc_dai_set_dma_data(dai, substream, NULL); 1246 kfree(stream); 1247 } 1248 1249 static int rt711_sdca_pcm_hw_params(struct snd_pcm_substream *substream, 1250 struct snd_pcm_hw_params *params, 1251 struct snd_soc_dai *dai) 1252 { 1253 struct snd_soc_component *component = dai->component; 1254 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1255 struct sdw_stream_config stream_config; 1256 struct sdw_port_config port_config; 1257 enum sdw_data_direction direction; 1258 struct sdw_stream_data *stream; 1259 int retval, port, num_channels; 1260 unsigned int sampling_rate; 1261 1262 dev_dbg(dai->dev, "%s %s", __func__, dai->name); 1263 stream = snd_soc_dai_get_dma_data(dai, substream); 1264 1265 if (!stream) 1266 return -EINVAL; 1267 1268 if (!rt711->slave) 1269 return -EINVAL; 1270 1271 /* SoundWire specific configuration */ 1272 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1273 direction = SDW_DATA_DIR_RX; 1274 port = 3; 1275 } else { 1276 direction = SDW_DATA_DIR_TX; 1277 if (dai->id == RT711_AIF1) 1278 port = 2; 1279 else if (dai->id == RT711_AIF2) 1280 port = 4; 1281 else 1282 return -EINVAL; 1283 } 1284 1285 stream_config.frame_rate = params_rate(params); 1286 stream_config.ch_count = params_channels(params); 1287 stream_config.bps = snd_pcm_format_width(params_format(params)); 1288 stream_config.direction = direction; 1289 1290 num_channels = params_channels(params); 1291 port_config.ch_mask = GENMASK(num_channels - 1, 0); 1292 port_config.num = port; 1293 1294 retval = sdw_stream_add_slave(rt711->slave, &stream_config, 1295 &port_config, 1, stream->sdw_stream); 1296 if (retval) { 1297 dev_err(dai->dev, "Unable to configure port\n"); 1298 return retval; 1299 } 1300 1301 if (params_channels(params) > 16) { 1302 dev_err(component->dev, "Unsupported channels %d\n", 1303 params_channels(params)); 1304 return -EINVAL; 1305 } 1306 1307 /* sampling rate configuration */ 1308 switch (params_rate(params)) { 1309 case 44100: 1310 sampling_rate = RT711_SDCA_RATE_44100HZ; 1311 break; 1312 case 48000: 1313 sampling_rate = RT711_SDCA_RATE_48000HZ; 1314 break; 1315 case 96000: 1316 sampling_rate = RT711_SDCA_RATE_96000HZ; 1317 break; 1318 case 192000: 1319 sampling_rate = RT711_SDCA_RATE_192000HZ; 1320 break; 1321 default: 1322 dev_err(component->dev, "Rate %d is not supported\n", 1323 params_rate(params)); 1324 return -EINVAL; 1325 } 1326 1327 /* set sampling frequency */ 1328 regmap_write(rt711->regmap, 1329 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_CS01, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0), 1330 sampling_rate); 1331 regmap_write(rt711->regmap, 1332 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_CS11, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0), 1333 sampling_rate); 1334 regmap_write(rt711->regmap, 1335 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_CS1F, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0), 1336 sampling_rate); 1337 1338 return 0; 1339 } 1340 1341 static int rt711_sdca_pcm_hw_free(struct snd_pcm_substream *substream, 1342 struct snd_soc_dai *dai) 1343 { 1344 struct snd_soc_component *component = dai->component; 1345 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1346 struct sdw_stream_data *stream = 1347 snd_soc_dai_get_dma_data(dai, substream); 1348 1349 if (!rt711->slave) 1350 return -EINVAL; 1351 1352 sdw_stream_remove_slave(rt711->slave, stream->sdw_stream); 1353 return 0; 1354 } 1355 1356 #define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \ 1357 SNDRV_PCM_RATE_192000) 1358 #define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ 1359 SNDRV_PCM_FMTBIT_S24_LE) 1360 1361 static const struct snd_soc_dai_ops rt711_sdca_ops = { 1362 .hw_params = rt711_sdca_pcm_hw_params, 1363 .hw_free = rt711_sdca_pcm_hw_free, 1364 .set_stream = rt711_sdca_set_sdw_stream, 1365 .shutdown = rt711_sdca_shutdown, 1366 }; 1367 1368 static struct snd_soc_dai_driver rt711_sdca_dai[] = { 1369 { 1370 .name = "rt711-sdca-aif1", 1371 .id = RT711_AIF1, 1372 .playback = { 1373 .stream_name = "DP3 Playback", 1374 .channels_min = 1, 1375 .channels_max = 2, 1376 .rates = RT711_STEREO_RATES, 1377 .formats = RT711_FORMATS, 1378 }, 1379 .capture = { 1380 .stream_name = "DP2 Capture", 1381 .channels_min = 1, 1382 .channels_max = 2, 1383 .rates = RT711_STEREO_RATES, 1384 .formats = RT711_FORMATS, 1385 }, 1386 .ops = &rt711_sdca_ops, 1387 }, 1388 { 1389 .name = "rt711-sdca-aif2", 1390 .id = RT711_AIF2, 1391 .capture = { 1392 .stream_name = "DP4 Capture", 1393 .channels_min = 1, 1394 .channels_max = 2, 1395 .rates = RT711_STEREO_RATES, 1396 .formats = RT711_FORMATS, 1397 }, 1398 .ops = &rt711_sdca_ops, 1399 } 1400 }; 1401 1402 int rt711_sdca_init(struct device *dev, struct regmap *regmap, 1403 struct regmap *mbq_regmap, struct sdw_slave *slave) 1404 { 1405 struct rt711_sdca_priv *rt711; 1406 int ret; 1407 1408 rt711 = devm_kzalloc(dev, sizeof(*rt711), GFP_KERNEL); 1409 if (!rt711) 1410 return -ENOMEM; 1411 1412 dev_set_drvdata(dev, rt711); 1413 rt711->slave = slave; 1414 rt711->regmap = regmap; 1415 rt711->mbq_regmap = mbq_regmap; 1416 1417 mutex_init(&rt711->calibrate_mutex); 1418 mutex_init(&rt711->disable_irq_lock); 1419 1420 INIT_DELAYED_WORK(&rt711->jack_detect_work, rt711_sdca_jack_detect_handler); 1421 INIT_DELAYED_WORK(&rt711->jack_btn_check_work, rt711_sdca_btn_check_handler); 1422 1423 /* 1424 * Mark hw_init to false 1425 * HW init will be performed when device reports present 1426 */ 1427 rt711->hw_init = false; 1428 rt711->first_hw_init = false; 1429 rt711->fu0f_dapm_mute = true; 1430 rt711->fu1e_dapm_mute = true; 1431 rt711->fu0f_mixer_l_mute = rt711->fu0f_mixer_r_mute = true; 1432 rt711->fu1e_mixer_l_mute = rt711->fu1e_mixer_r_mute = true; 1433 1434 /* JD source uses JD2 in default */ 1435 rt711->jd_src = RT711_JD2; 1436 1437 ret = devm_snd_soc_register_component(dev, 1438 &soc_sdca_dev_rt711, 1439 rt711_sdca_dai, 1440 ARRAY_SIZE(rt711_sdca_dai)); 1441 1442 dev_dbg(&slave->dev, "%s\n", __func__); 1443 1444 return ret; 1445 } 1446 1447 static void rt711_sdca_vd0_io_init(struct rt711_sdca_priv *rt711) 1448 { 1449 rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1450 RT711_GPIO_TEST_MODE_CTL2, 0x0e00); 1451 rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL, 1452 RT711_HDA_LEGACY_GPIO_CTL, 0x0008); 1453 1454 regmap_write(rt711->regmap, 0x2f5a, 0x01); 1455 1456 rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1457 RT711_ADC27_VOL_SET, 0x8728); 1458 1459 rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1460 RT711_COMBO_JACK_AUTO_CTL3, 0xa472); 1461 1462 regmap_write(rt711->regmap, 0x2f50, 0x02); 1463 1464 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_ANALOG_CTL, 1465 RT711_MISC_POWER_CTL4, 0x6000, 0x6000); 1466 1467 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 1468 RT711_COMBO_JACK_AUTO_CTL3, 0x000c, 0x000c); 1469 1470 rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL, 1471 RT711_HDA_LEGACY_CONFIG_CTL, 0x0000); 1472 1473 rt711_sdca_index_write(rt711, RT711_VENDOR_VAD, 1474 RT711_VAD_SRAM_CTL1, 0x0050); 1475 } 1476 1477 static void rt711_sdca_vd1_io_init(struct rt711_sdca_priv *rt711) 1478 { 1479 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 1480 RT711_HDA_LEGACY_UNSOLICITED_CTL, 0x0300, 0x0000); 1481 1482 rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1483 RT711_COMBO_JACK_AUTO_CTL3, 0xa43e); 1484 1485 regmap_write(rt711->regmap, 0x2f5a, 0x05); 1486 1487 rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1488 RT711_JD_CTRL6, 0x0500); 1489 1490 rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1491 RT711_DMIC_CTL1, 0x6173); 1492 1493 rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL, 1494 RT711_HDA_LEGACY_CONFIG_CTL, 0x0000); 1495 1496 rt711_sdca_index_write(rt711, RT711_VENDOR_VAD, 1497 RT711_VAD_SRAM_CTL1, 0x0050); 1498 } 1499 1500 int rt711_sdca_io_init(struct device *dev, struct sdw_slave *slave) 1501 { 1502 struct rt711_sdca_priv *rt711 = dev_get_drvdata(dev); 1503 int ret = 0; 1504 unsigned int val; 1505 1506 rt711->disable_irq = false; 1507 1508 if (rt711->hw_init) 1509 return 0; 1510 1511 if (rt711->first_hw_init) { 1512 regcache_cache_only(rt711->regmap, false); 1513 regcache_cache_bypass(rt711->regmap, true); 1514 regcache_cache_only(rt711->mbq_regmap, false); 1515 regcache_cache_bypass(rt711->mbq_regmap, true); 1516 } else { 1517 /* 1518 * PM runtime is only enabled when a Slave reports as Attached 1519 */ 1520 1521 /* set autosuspend parameters */ 1522 pm_runtime_set_autosuspend_delay(&slave->dev, 3000); 1523 pm_runtime_use_autosuspend(&slave->dev); 1524 1525 /* update count of parent 'active' children */ 1526 pm_runtime_set_active(&slave->dev); 1527 1528 /* make sure the device does not suspend immediately */ 1529 pm_runtime_mark_last_busy(&slave->dev); 1530 1531 pm_runtime_enable(&slave->dev); 1532 } 1533 1534 pm_runtime_get_noresume(&slave->dev); 1535 1536 rt711_sdca_reset(rt711); 1537 1538 rt711_sdca_index_read(rt711, RT711_VENDOR_REG, RT711_JD_PRODUCT_NUM, &val); 1539 rt711->hw_ver = val & 0xf; 1540 1541 if (rt711->hw_ver == RT711_VER_VD0) 1542 rt711_sdca_vd0_io_init(rt711); 1543 else 1544 rt711_sdca_vd1_io_init(rt711); 1545 1546 /* DP4 mux select from 08_filter_Out_pri */ 1547 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 1548 RT711_FILTER_SRC_SEL, 0x1800, 0x0800); 1549 1550 /* ge_exclusive_inbox_en disable */ 1551 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 1552 RT711_PUSH_BTN_INT_CTL0, 0x20, 0x00); 1553 1554 /* calibration */ 1555 ret = rt711_sdca_calibration(rt711); 1556 if (ret < 0) 1557 dev_err(dev, "%s, calibration failed!\n", __func__); 1558 1559 /* HP output enable */ 1560 regmap_write(rt711->regmap, 1561 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_OT1, RT711_SDCA_CTL_VENDOR_DEF, 0), 0x4); 1562 1563 /* 1564 * if set_jack callback occurred early than io_init, 1565 * we set up the jack detection function now 1566 */ 1567 if (rt711->hs_jack) 1568 rt711_sdca_jack_init(rt711); 1569 1570 if (rt711->first_hw_init) { 1571 regcache_cache_bypass(rt711->regmap, false); 1572 regcache_mark_dirty(rt711->regmap); 1573 regcache_cache_bypass(rt711->mbq_regmap, false); 1574 regcache_mark_dirty(rt711->mbq_regmap); 1575 } else 1576 rt711->first_hw_init = true; 1577 1578 /* Mark Slave initialization complete */ 1579 rt711->hw_init = true; 1580 1581 pm_runtime_mark_last_busy(&slave->dev); 1582 pm_runtime_put_autosuspend(&slave->dev); 1583 1584 dev_dbg(&slave->dev, "%s hw_init complete\n", __func__); 1585 return 0; 1586 } 1587 1588 MODULE_DESCRIPTION("ASoC RT711 SDCA SDW driver"); 1589 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>"); 1590 MODULE_LICENSE("GPL"); 1591