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