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->component->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->component->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->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 491 rt711->hs_jack = hs_jack; 492 493 if (!rt711->hw_init) { 494 dev_dbg(&rt711->slave->dev, 495 "%s hw_init not ready yet\n", __func__); 496 return 0; 497 } 498 499 rt711_sdca_jack_init(rt711); 500 return 0; 501 } 502 503 /* For SDCA control DAC/ADC Gain */ 504 static int rt711_sdca_set_gain_put(struct snd_kcontrol *kcontrol, 505 struct snd_ctl_elem_value *ucontrol) 506 { 507 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 508 struct soc_mixer_control *mc = 509 (struct soc_mixer_control *)kcontrol->private_value; 510 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 511 unsigned int read_l, read_r, gain_l_val, gain_r_val; 512 unsigned int i, adc_vol_flag = 0, changed = 0; 513 unsigned int lvalue, rvalue; 514 515 if (strstr(ucontrol->id.name, "FU1E Capture Volume") || 516 strstr(ucontrol->id.name, "FU0F Capture Volume")) 517 adc_vol_flag = 1; 518 519 regmap_read(rt711->mbq_regmap, mc->reg, &lvalue); 520 regmap_read(rt711->mbq_regmap, mc->rreg, &rvalue); 521 522 /* control value to 2's complement value */ 523 /* L Channel */ 524 gain_l_val = ucontrol->value.integer.value[0]; 525 if (gain_l_val > mc->max) 526 gain_l_val = mc->max; 527 read_l = gain_l_val; 528 529 if (mc->shift == 8) /* boost gain */ 530 gain_l_val = (gain_l_val * 10) << mc->shift; 531 else { /* ADC/DAC gain */ 532 if (adc_vol_flag && gain_l_val > mc->shift) 533 gain_l_val = (gain_l_val - mc->shift) * 75; 534 else 535 gain_l_val = (mc->shift - gain_l_val) * 75; 536 gain_l_val <<= 8; 537 gain_l_val /= 100; 538 if (!(adc_vol_flag && read_l > mc->shift)) { 539 gain_l_val = ~gain_l_val; 540 gain_l_val += 1; 541 } 542 gain_l_val &= 0xffff; 543 } 544 545 /* R Channel */ 546 gain_r_val = ucontrol->value.integer.value[1]; 547 if (gain_r_val > mc->max) 548 gain_r_val = mc->max; 549 read_r = gain_r_val; 550 551 if (mc->shift == 8) /* boost gain */ 552 gain_r_val = (gain_r_val * 10) << mc->shift; 553 else { /* ADC/DAC gain */ 554 if (adc_vol_flag && gain_r_val > mc->shift) 555 gain_r_val = (gain_r_val - mc->shift) * 75; 556 else 557 gain_r_val = (mc->shift - gain_r_val) * 75; 558 gain_r_val <<= 8; 559 gain_r_val /= 100; 560 if (!(adc_vol_flag && read_r > mc->shift)) { 561 gain_r_val = ~gain_r_val; 562 gain_r_val += 1; 563 } 564 gain_r_val &= 0xffff; 565 } 566 567 if (lvalue != gain_l_val || rvalue != gain_r_val) 568 changed = 1; 569 else 570 return 0; 571 572 for (i = 0; i < 3; i++) { /* retry 3 times at most */ 573 /* Lch*/ 574 regmap_write(rt711->mbq_regmap, mc->reg, gain_l_val); 575 576 /* Rch */ 577 regmap_write(rt711->mbq_regmap, mc->rreg, gain_r_val); 578 579 regmap_read(rt711->mbq_regmap, mc->reg, &read_l); 580 regmap_read(rt711->mbq_regmap, mc->rreg, &read_r); 581 if (read_r == gain_r_val && read_l == gain_l_val) 582 break; 583 } 584 585 return i == 3 ? -EIO : changed; 586 } 587 588 static int rt711_sdca_set_gain_get(struct snd_kcontrol *kcontrol, 589 struct snd_ctl_elem_value *ucontrol) 590 { 591 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 592 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 593 struct soc_mixer_control *mc = 594 (struct soc_mixer_control *)kcontrol->private_value; 595 unsigned int read_l, read_r, ctl_l = 0, ctl_r = 0; 596 unsigned int adc_vol_flag = 0, neg_flag = 0; 597 598 if (strstr(ucontrol->id.name, "FU1E Capture Volume") || 599 strstr(ucontrol->id.name, "FU0F Capture Volume")) 600 adc_vol_flag = 1; 601 602 regmap_read(rt711->mbq_regmap, mc->reg, &read_l); 603 regmap_read(rt711->mbq_regmap, mc->rreg, &read_r); 604 605 /* 2's complement value to control value */ 606 if (mc->shift == 8) /* boost gain */ 607 ctl_l = (read_l >> mc->shift) / 10; 608 else { /* ADC/DAC gain */ 609 ctl_l = read_l; 610 if (read_l & BIT(15)) { 611 ctl_l = 0xffff & ~(read_l - 1); 612 neg_flag = 1; 613 } 614 ctl_l *= 100; 615 ctl_l >>= 8; 616 if (adc_vol_flag) { 617 if (neg_flag) 618 ctl_l = mc->shift - (ctl_l / 75); 619 else 620 ctl_l = mc->shift + (ctl_l / 75); 621 } else 622 ctl_l = mc->max - (ctl_l / 75); 623 } 624 625 neg_flag = 0; 626 if (read_l != read_r) { 627 if (mc->shift == 8) /* boost gain */ 628 ctl_r = (read_r >> mc->shift) / 10; 629 else { /* ADC/DAC gain */ 630 ctl_r = read_r; 631 if (read_r & BIT(15)) { 632 ctl_r = 0xffff & ~(read_r - 1); 633 neg_flag = 1; 634 } 635 ctl_r *= 100; 636 ctl_r >>= 8; 637 if (adc_vol_flag) { 638 if (neg_flag) 639 ctl_r = mc->shift - (ctl_r / 75); 640 else 641 ctl_r = mc->shift + (ctl_r / 75); 642 } else 643 ctl_r = mc->max - (ctl_r / 75); 644 } 645 } else 646 ctl_r = ctl_l; 647 648 ucontrol->value.integer.value[0] = ctl_l; 649 ucontrol->value.integer.value[1] = ctl_r; 650 651 return 0; 652 } 653 654 static int rt711_sdca_set_fu0f_capture_ctl(struct rt711_sdca_priv *rt711) 655 { 656 int err; 657 unsigned int ch_l, ch_r; 658 659 ch_l = (rt711->fu0f_dapm_mute || rt711->fu0f_mixer_l_mute) ? 0x01 : 0x00; 660 ch_r = (rt711->fu0f_dapm_mute || rt711->fu0f_mixer_r_mute) ? 0x01 : 0x00; 661 662 err = regmap_write(rt711->regmap, 663 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, 664 RT711_SDCA_CTL_FU_MUTE, CH_L), ch_l); 665 if (err < 0) 666 return err; 667 668 err = regmap_write(rt711->regmap, 669 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, 670 RT711_SDCA_CTL_FU_MUTE, CH_R), ch_r); 671 if (err < 0) 672 return err; 673 674 return 0; 675 } 676 677 static int rt711_sdca_set_fu1e_capture_ctl(struct rt711_sdca_priv *rt711) 678 { 679 int err; 680 unsigned int ch_l, ch_r; 681 682 ch_l = (rt711->fu1e_dapm_mute || rt711->fu1e_mixer_l_mute) ? 0x01 : 0x00; 683 ch_r = (rt711->fu1e_dapm_mute || rt711->fu1e_mixer_r_mute) ? 0x01 : 0x00; 684 685 err = regmap_write(rt711->regmap, 686 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU1E, 687 RT711_SDCA_CTL_FU_MUTE, CH_L), ch_l); 688 if (err < 0) 689 return err; 690 691 err = regmap_write(rt711->regmap, 692 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU1E, 693 RT711_SDCA_CTL_FU_MUTE, CH_R), ch_r); 694 if (err < 0) 695 return err; 696 697 return 0; 698 } 699 700 static int rt711_sdca_fu1e_capture_get(struct snd_kcontrol *kcontrol, 701 struct snd_ctl_elem_value *ucontrol) 702 { 703 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 704 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 705 706 ucontrol->value.integer.value[0] = !rt711->fu1e_mixer_l_mute; 707 ucontrol->value.integer.value[1] = !rt711->fu1e_mixer_r_mute; 708 return 0; 709 } 710 711 static int rt711_sdca_fu1e_capture_put(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 int err, changed = 0; 717 718 if (rt711->fu1e_mixer_l_mute != !ucontrol->value.integer.value[0] || 719 rt711->fu1e_mixer_r_mute != !ucontrol->value.integer.value[1]) 720 changed = 1; 721 722 rt711->fu1e_mixer_l_mute = !ucontrol->value.integer.value[0]; 723 rt711->fu1e_mixer_r_mute = !ucontrol->value.integer.value[1]; 724 err = rt711_sdca_set_fu1e_capture_ctl(rt711); 725 if (err < 0) 726 return err; 727 728 return changed; 729 } 730 731 static int rt711_sdca_fu0f_capture_get(struct snd_kcontrol *kcontrol, 732 struct snd_ctl_elem_value *ucontrol) 733 { 734 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 735 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 736 737 ucontrol->value.integer.value[0] = !rt711->fu0f_mixer_l_mute; 738 ucontrol->value.integer.value[1] = !rt711->fu0f_mixer_r_mute; 739 return 0; 740 } 741 742 static int rt711_sdca_fu0f_capture_put(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 int err, changed = 0; 748 749 if (rt711->fu0f_mixer_l_mute != !ucontrol->value.integer.value[0] || 750 rt711->fu0f_mixer_r_mute != !ucontrol->value.integer.value[1]) 751 changed = 1; 752 753 rt711->fu0f_mixer_l_mute = !ucontrol->value.integer.value[0]; 754 rt711->fu0f_mixer_r_mute = !ucontrol->value.integer.value[1]; 755 err = rt711_sdca_set_fu0f_capture_ctl(rt711); 756 if (err < 0) 757 return err; 758 759 return changed; 760 } 761 762 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0); 763 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0); 764 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0); 765 766 static const struct snd_kcontrol_new rt711_sdca_snd_controls[] = { 767 SOC_DOUBLE_R_EXT_TLV("FU05 Playback Volume", 768 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_VOLUME, CH_L), 769 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_VOLUME, CH_R), 770 0x57, 0x57, 0, 771 rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, out_vol_tlv), 772 SOC_DOUBLE_EXT("FU1E Capture Switch", SND_SOC_NOPM, 0, 1, 1, 0, 773 rt711_sdca_fu1e_capture_get, rt711_sdca_fu1e_capture_put), 774 SOC_DOUBLE_EXT("FU0F Capture Switch", SND_SOC_NOPM, 0, 1, 1, 0, 775 rt711_sdca_fu0f_capture_get, rt711_sdca_fu0f_capture_put), 776 SOC_DOUBLE_R_EXT_TLV("FU1E Capture Volume", 777 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_VOLUME, CH_L), 778 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_VOLUME, CH_R), 779 0x17, 0x3f, 0, 780 rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, in_vol_tlv), 781 SOC_DOUBLE_R_EXT_TLV("FU0F Capture Volume", 782 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_VOLUME, CH_L), 783 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_VOLUME, CH_R), 784 0x17, 0x3f, 0, 785 rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, in_vol_tlv), 786 SOC_DOUBLE_R_EXT_TLV("FU44 Gain Volume", 787 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PLATFORM_FU44, RT711_SDCA_CTL_FU_CH_GAIN, CH_L), 788 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PLATFORM_FU44, RT711_SDCA_CTL_FU_CH_GAIN, CH_R), 789 8, 3, 0, 790 rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, mic_vol_tlv), 791 SOC_DOUBLE_R_EXT_TLV("FU15 Gain Volume", 792 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PLATFORM_FU15, RT711_SDCA_CTL_FU_CH_GAIN, CH_L), 793 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PLATFORM_FU15, RT711_SDCA_CTL_FU_CH_GAIN, CH_R), 794 8, 3, 0, 795 rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, mic_vol_tlv), 796 }; 797 798 static int rt711_sdca_mux_get(struct snd_kcontrol *kcontrol, 799 struct snd_ctl_elem_value *ucontrol) 800 { 801 struct snd_soc_component *component = 802 snd_soc_dapm_kcontrol_component(kcontrol); 803 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 804 unsigned int val = 0, mask_sft; 805 806 if (strstr(ucontrol->id.name, "ADC 22 Mux")) 807 mask_sft = 10; 808 else if (strstr(ucontrol->id.name, "ADC 23 Mux")) 809 mask_sft = 13; 810 else 811 return -EINVAL; 812 813 rt711_sdca_index_read(rt711, RT711_VENDOR_HDA_CTL, 814 RT711_HDA_LEGACY_MUX_CTL1, &val); 815 816 ucontrol->value.enumerated.item[0] = (val >> mask_sft) & 0x7; 817 818 return 0; 819 } 820 821 static int rt711_sdca_mux_put(struct snd_kcontrol *kcontrol, 822 struct snd_ctl_elem_value *ucontrol) 823 { 824 struct snd_soc_component *component = 825 snd_soc_dapm_kcontrol_component(kcontrol); 826 struct snd_soc_dapm_context *dapm = 827 snd_soc_dapm_kcontrol_dapm(kcontrol); 828 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 829 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 830 unsigned int *item = ucontrol->value.enumerated.item; 831 unsigned int val, val2 = 0, change, mask_sft; 832 833 if (item[0] >= e->items) 834 return -EINVAL; 835 836 if (strstr(ucontrol->id.name, "ADC 22 Mux")) 837 mask_sft = 10; 838 else if (strstr(ucontrol->id.name, "ADC 23 Mux")) 839 mask_sft = 13; 840 else 841 return -EINVAL; 842 843 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; 844 845 rt711_sdca_index_read(rt711, RT711_VENDOR_HDA_CTL, 846 RT711_HDA_LEGACY_MUX_CTL1, &val2); 847 val2 = (val2 >> mask_sft) & 0x7; 848 849 if (val == val2) 850 change = 0; 851 else 852 change = 1; 853 854 if (change) 855 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 856 RT711_HDA_LEGACY_MUX_CTL1, 0x7 << mask_sft, 857 val << mask_sft); 858 859 snd_soc_dapm_mux_update_power(dapm, kcontrol, 860 item[0], e, NULL); 861 862 return change; 863 } 864 865 static const char * const adc_mux_text[] = { 866 "MIC2", 867 "LINE1", 868 "LINE2", 869 "DMIC", 870 }; 871 872 static SOC_ENUM_SINGLE_DECL( 873 rt711_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text); 874 875 static SOC_ENUM_SINGLE_DECL( 876 rt711_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text); 877 878 static const struct snd_kcontrol_new rt711_sdca_adc22_mux = 879 SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt711_adc22_enum, 880 rt711_sdca_mux_get, rt711_sdca_mux_put); 881 882 static const struct snd_kcontrol_new rt711_sdca_adc23_mux = 883 SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt711_adc23_enum, 884 rt711_sdca_mux_get, rt711_sdca_mux_put); 885 886 static int rt711_sdca_fu05_event(struct snd_soc_dapm_widget *w, 887 struct snd_kcontrol *kcontrol, int event) 888 { 889 struct snd_soc_component *component = 890 snd_soc_dapm_to_component(w->dapm); 891 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 892 unsigned char unmute = 0x0, mute = 0x1; 893 894 switch (event) { 895 case SND_SOC_DAPM_POST_PMU: 896 regmap_write(rt711->regmap, 897 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, 898 RT711_SDCA_CTL_FU_MUTE, CH_L), 899 unmute); 900 regmap_write(rt711->regmap, 901 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, 902 RT711_SDCA_CTL_FU_MUTE, CH_R), 903 unmute); 904 break; 905 case SND_SOC_DAPM_PRE_PMD: 906 regmap_write(rt711->regmap, 907 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, 908 RT711_SDCA_CTL_FU_MUTE, CH_L), 909 mute); 910 regmap_write(rt711->regmap, 911 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, 912 RT711_SDCA_CTL_FU_MUTE, CH_R), 913 mute); 914 break; 915 } 916 return 0; 917 } 918 919 static int rt711_sdca_fu0f_event(struct snd_soc_dapm_widget *w, 920 struct snd_kcontrol *kcontrol, int event) 921 { 922 struct snd_soc_component *component = 923 snd_soc_dapm_to_component(w->dapm); 924 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 925 926 switch (event) { 927 case SND_SOC_DAPM_POST_PMU: 928 rt711->fu0f_dapm_mute = false; 929 rt711_sdca_set_fu0f_capture_ctl(rt711); 930 break; 931 case SND_SOC_DAPM_PRE_PMD: 932 rt711->fu0f_dapm_mute = true; 933 rt711_sdca_set_fu0f_capture_ctl(rt711); 934 break; 935 } 936 return 0; 937 } 938 939 static int rt711_sdca_fu1e_event(struct snd_soc_dapm_widget *w, 940 struct snd_kcontrol *kcontrol, int event) 941 { 942 struct snd_soc_component *component = 943 snd_soc_dapm_to_component(w->dapm); 944 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 945 946 switch (event) { 947 case SND_SOC_DAPM_POST_PMU: 948 rt711->fu1e_dapm_mute = false; 949 rt711_sdca_set_fu1e_capture_ctl(rt711); 950 break; 951 case SND_SOC_DAPM_PRE_PMD: 952 rt711->fu1e_dapm_mute = true; 953 rt711_sdca_set_fu1e_capture_ctl(rt711); 954 break; 955 } 956 return 0; 957 } 958 959 static int rt711_sdca_pde28_event(struct snd_soc_dapm_widget *w, 960 struct snd_kcontrol *kcontrol, int event) 961 { 962 struct snd_soc_component *component = 963 snd_soc_dapm_to_component(w->dapm); 964 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 965 unsigned char ps0 = 0x0, ps3 = 0x3; 966 967 switch (event) { 968 case SND_SOC_DAPM_POST_PMU: 969 regmap_write(rt711->regmap, 970 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE28, 971 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 972 ps0); 973 break; 974 case SND_SOC_DAPM_PRE_PMD: 975 regmap_write(rt711->regmap, 976 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE28, 977 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 978 ps3); 979 break; 980 } 981 return 0; 982 } 983 984 static int rt711_sdca_pde29_event(struct snd_soc_dapm_widget *w, 985 struct snd_kcontrol *kcontrol, int event) 986 { 987 struct snd_soc_component *component = 988 snd_soc_dapm_to_component(w->dapm); 989 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 990 unsigned char ps0 = 0x0, ps3 = 0x3; 991 992 switch (event) { 993 case SND_SOC_DAPM_POST_PMU: 994 regmap_write(rt711->regmap, 995 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE29, 996 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 997 ps0); 998 break; 999 case SND_SOC_DAPM_PRE_PMD: 1000 regmap_write(rt711->regmap, 1001 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE29, 1002 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 1003 ps3); 1004 break; 1005 } 1006 return 0; 1007 } 1008 1009 static int rt711_sdca_pde2a_event(struct snd_soc_dapm_widget *w, 1010 struct snd_kcontrol *kcontrol, int event) 1011 { 1012 struct snd_soc_component *component = 1013 snd_soc_dapm_to_component(w->dapm); 1014 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1015 unsigned char ps0 = 0x0, ps3 = 0x3; 1016 1017 switch (event) { 1018 case SND_SOC_DAPM_POST_PMU: 1019 regmap_write(rt711->regmap, 1020 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PDE2A, 1021 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 1022 ps0); 1023 break; 1024 case SND_SOC_DAPM_PRE_PMD: 1025 regmap_write(rt711->regmap, 1026 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PDE2A, 1027 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 1028 ps3); 1029 break; 1030 } 1031 return 0; 1032 } 1033 1034 static int rt711_sdca_line1_power_event(struct snd_soc_dapm_widget *w, 1035 struct snd_kcontrol *kcontrol, int event) 1036 { 1037 struct snd_soc_component *component = 1038 snd_soc_dapm_to_component(w->dapm); 1039 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1040 static unsigned int sel_mode = 0xffff; 1041 1042 switch (event) { 1043 case SND_SOC_DAPM_POST_PMU: 1044 regmap_read(rt711->regmap, 1045 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, 1046 RT711_SDCA_CTL_SELECTED_MODE, 0), 1047 &sel_mode); 1048 regmap_write(rt711->regmap, 1049 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE1, 1050 RT711_SDCA_CTL_VENDOR_DEF, 0), 1051 0x1); 1052 regmap_write(rt711->regmap, 1053 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, 1054 RT711_SDCA_CTL_SELECTED_MODE, 0), 1055 0x7); 1056 break; 1057 case SND_SOC_DAPM_PRE_PMD: 1058 regmap_write(rt711->regmap, 1059 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE1, 1060 RT711_SDCA_CTL_VENDOR_DEF, 0), 1061 0x0); 1062 if (sel_mode != 0xffff) 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 sel_mode); 1067 break; 1068 } 1069 1070 return 0; 1071 } 1072 1073 static int rt711_sdca_line2_power_event(struct snd_soc_dapm_widget *w, 1074 struct snd_kcontrol *kcontrol, int event) 1075 { 1076 struct snd_soc_component *component = 1077 snd_soc_dapm_to_component(w->dapm); 1078 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1079 unsigned char ps0 = 0x0, ps3 = 0x3; 1080 1081 switch (event) { 1082 case SND_SOC_DAPM_POST_PMU: 1083 regmap_write(rt711->regmap, 1084 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDELINE2, 1085 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 1086 ps0); 1087 regmap_write(rt711->regmap, 1088 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE2, 1089 RT711_SDCA_CTL_VENDOR_DEF, 0), 1090 0x1); 1091 break; 1092 case SND_SOC_DAPM_PRE_PMD: 1093 regmap_write(rt711->regmap, 1094 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE2, 1095 RT711_SDCA_CTL_VENDOR_DEF, 0), 1096 0x0); 1097 regmap_write(rt711->regmap, 1098 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDELINE2, 1099 RT711_SDCA_CTL_REQ_POWER_STATE, 0), 1100 ps3); 1101 break; 1102 } 1103 1104 return 0; 1105 } 1106 1107 static const struct snd_soc_dapm_widget rt711_sdca_dapm_widgets[] = { 1108 SND_SOC_DAPM_OUTPUT("HP"), 1109 SND_SOC_DAPM_INPUT("MIC2"), 1110 SND_SOC_DAPM_INPUT("DMIC1"), 1111 SND_SOC_DAPM_INPUT("DMIC2"), 1112 SND_SOC_DAPM_INPUT("LINE1"), 1113 SND_SOC_DAPM_INPUT("LINE2"), 1114 1115 SND_SOC_DAPM_PGA_E("LINE1 Power", SND_SOC_NOPM, 1116 0, 0, NULL, 0, rt711_sdca_line1_power_event, 1117 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1118 SND_SOC_DAPM_PGA_E("LINE2 Power", SND_SOC_NOPM, 1119 0, 0, NULL, 0, rt711_sdca_line2_power_event, 1120 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1121 1122 SND_SOC_DAPM_SUPPLY("PDE 28", SND_SOC_NOPM, 0, 0, 1123 rt711_sdca_pde28_event, 1124 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1125 SND_SOC_DAPM_SUPPLY("PDE 29", SND_SOC_NOPM, 0, 0, 1126 rt711_sdca_pde29_event, 1127 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1128 SND_SOC_DAPM_SUPPLY("PDE 2A", SND_SOC_NOPM, 0, 0, 1129 rt711_sdca_pde2a_event, 1130 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1131 1132 SND_SOC_DAPM_DAC_E("FU 05", NULL, SND_SOC_NOPM, 0, 0, 1133 rt711_sdca_fu05_event, 1134 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1135 SND_SOC_DAPM_ADC_E("FU 0F", NULL, SND_SOC_NOPM, 0, 0, 1136 rt711_sdca_fu0f_event, 1137 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1138 SND_SOC_DAPM_ADC_E("FU 1E", NULL, SND_SOC_NOPM, 0, 0, 1139 rt711_sdca_fu1e_event, 1140 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1141 SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0, 1142 &rt711_sdca_adc22_mux), 1143 SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0, 1144 &rt711_sdca_adc23_mux), 1145 1146 SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0), 1147 SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0), 1148 SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0), 1149 }; 1150 1151 static const struct snd_soc_dapm_route rt711_sdca_audio_map[] = { 1152 {"FU 05", NULL, "DP3RX"}, 1153 {"DP2TX", NULL, "FU 0F"}, 1154 {"DP4TX", NULL, "FU 1E"}, 1155 1156 {"LINE1 Power", NULL, "LINE1"}, 1157 {"LINE2 Power", NULL, "LINE2"}, 1158 {"HP", NULL, "PDE 28"}, 1159 {"FU 0F", NULL, "PDE 29"}, 1160 {"FU 1E", NULL, "PDE 2A"}, 1161 1162 {"FU 0F", NULL, "ADC 22 Mux"}, 1163 {"FU 1E", NULL, "ADC 23 Mux"}, 1164 {"ADC 22 Mux", "DMIC", "DMIC1"}, 1165 {"ADC 22 Mux", "LINE1", "LINE1 Power"}, 1166 {"ADC 22 Mux", "LINE2", "LINE2 Power"}, 1167 {"ADC 22 Mux", "MIC2", "MIC2"}, 1168 {"ADC 23 Mux", "DMIC", "DMIC2"}, 1169 {"ADC 23 Mux", "LINE1", "LINE1 Power"}, 1170 {"ADC 23 Mux", "LINE2", "LINE2 Power"}, 1171 {"ADC 23 Mux", "MIC2", "MIC2"}, 1172 1173 {"HP", NULL, "FU 05"}, 1174 }; 1175 1176 static int rt711_sdca_parse_dt(struct rt711_sdca_priv *rt711, struct device *dev) 1177 { 1178 device_property_read_u32(dev, "realtek,jd-src", &rt711->jd_src); 1179 1180 return 0; 1181 } 1182 1183 static int rt711_sdca_probe(struct snd_soc_component *component) 1184 { 1185 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1186 1187 rt711_sdca_parse_dt(rt711, &rt711->slave->dev); 1188 rt711->component = component; 1189 1190 return 0; 1191 } 1192 1193 static void rt711_sdca_remove(struct snd_soc_component *component) 1194 { 1195 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1196 1197 regcache_cache_only(rt711->regmap, true); 1198 regcache_cache_only(rt711->mbq_regmap, true); 1199 } 1200 1201 static const struct snd_soc_component_driver soc_sdca_dev_rt711 = { 1202 .probe = rt711_sdca_probe, 1203 .controls = rt711_sdca_snd_controls, 1204 .num_controls = ARRAY_SIZE(rt711_sdca_snd_controls), 1205 .dapm_widgets = rt711_sdca_dapm_widgets, 1206 .num_dapm_widgets = ARRAY_SIZE(rt711_sdca_dapm_widgets), 1207 .dapm_routes = rt711_sdca_audio_map, 1208 .num_dapm_routes = ARRAY_SIZE(rt711_sdca_audio_map), 1209 .set_jack = rt711_sdca_set_jack_detect, 1210 .remove = rt711_sdca_remove, 1211 }; 1212 1213 static int rt711_sdca_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream, 1214 int direction) 1215 { 1216 struct sdw_stream_data *stream; 1217 1218 if (!sdw_stream) 1219 return 0; 1220 1221 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 1222 if (!stream) 1223 return -ENOMEM; 1224 1225 stream->sdw_stream = sdw_stream; 1226 1227 /* Use tx_mask or rx_mask to configure stream tag and set dma_data */ 1228 if (direction == SNDRV_PCM_STREAM_PLAYBACK) 1229 dai->playback_dma_data = stream; 1230 else 1231 dai->capture_dma_data = stream; 1232 1233 return 0; 1234 } 1235 1236 static void rt711_sdca_shutdown(struct snd_pcm_substream *substream, 1237 struct snd_soc_dai *dai) 1238 { 1239 struct sdw_stream_data *stream; 1240 1241 stream = snd_soc_dai_get_dma_data(dai, substream); 1242 snd_soc_dai_set_dma_data(dai, substream, NULL); 1243 kfree(stream); 1244 } 1245 1246 static int rt711_sdca_pcm_hw_params(struct snd_pcm_substream *substream, 1247 struct snd_pcm_hw_params *params, 1248 struct snd_soc_dai *dai) 1249 { 1250 struct snd_soc_component *component = dai->component; 1251 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1252 struct sdw_stream_config stream_config; 1253 struct sdw_port_config port_config; 1254 enum sdw_data_direction direction; 1255 struct sdw_stream_data *stream; 1256 int retval, port, num_channels; 1257 unsigned int sampling_rate; 1258 1259 dev_dbg(dai->dev, "%s %s", __func__, dai->name); 1260 stream = snd_soc_dai_get_dma_data(dai, substream); 1261 1262 if (!stream) 1263 return -EINVAL; 1264 1265 if (!rt711->slave) 1266 return -EINVAL; 1267 1268 /* SoundWire specific configuration */ 1269 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1270 direction = SDW_DATA_DIR_RX; 1271 port = 3; 1272 } else { 1273 direction = SDW_DATA_DIR_TX; 1274 if (dai->id == RT711_AIF1) 1275 port = 2; 1276 else if (dai->id == RT711_AIF2) 1277 port = 4; 1278 else 1279 return -EINVAL; 1280 } 1281 1282 stream_config.frame_rate = params_rate(params); 1283 stream_config.ch_count = params_channels(params); 1284 stream_config.bps = snd_pcm_format_width(params_format(params)); 1285 stream_config.direction = direction; 1286 1287 num_channels = params_channels(params); 1288 port_config.ch_mask = GENMASK(num_channels - 1, 0); 1289 port_config.num = port; 1290 1291 retval = sdw_stream_add_slave(rt711->slave, &stream_config, 1292 &port_config, 1, stream->sdw_stream); 1293 if (retval) { 1294 dev_err(dai->dev, "Unable to configure port\n"); 1295 return retval; 1296 } 1297 1298 if (params_channels(params) > 16) { 1299 dev_err(component->dev, "Unsupported channels %d\n", 1300 params_channels(params)); 1301 return -EINVAL; 1302 } 1303 1304 /* sampling rate configuration */ 1305 switch (params_rate(params)) { 1306 case 44100: 1307 sampling_rate = RT711_SDCA_RATE_44100HZ; 1308 break; 1309 case 48000: 1310 sampling_rate = RT711_SDCA_RATE_48000HZ; 1311 break; 1312 case 96000: 1313 sampling_rate = RT711_SDCA_RATE_96000HZ; 1314 break; 1315 case 192000: 1316 sampling_rate = RT711_SDCA_RATE_192000HZ; 1317 break; 1318 default: 1319 dev_err(component->dev, "Rate %d is not supported\n", 1320 params_rate(params)); 1321 return -EINVAL; 1322 } 1323 1324 /* set sampling frequency */ 1325 regmap_write(rt711->regmap, 1326 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_CS01, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0), 1327 sampling_rate); 1328 regmap_write(rt711->regmap, 1329 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_CS11, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0), 1330 sampling_rate); 1331 regmap_write(rt711->regmap, 1332 SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_CS1F, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0), 1333 sampling_rate); 1334 1335 return 0; 1336 } 1337 1338 static int rt711_sdca_pcm_hw_free(struct snd_pcm_substream *substream, 1339 struct snd_soc_dai *dai) 1340 { 1341 struct snd_soc_component *component = dai->component; 1342 struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1343 struct sdw_stream_data *stream = 1344 snd_soc_dai_get_dma_data(dai, substream); 1345 1346 if (!rt711->slave) 1347 return -EINVAL; 1348 1349 sdw_stream_remove_slave(rt711->slave, stream->sdw_stream); 1350 return 0; 1351 } 1352 1353 #define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \ 1354 SNDRV_PCM_RATE_192000) 1355 #define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ 1356 SNDRV_PCM_FMTBIT_S24_LE) 1357 1358 static const struct snd_soc_dai_ops rt711_sdca_ops = { 1359 .hw_params = rt711_sdca_pcm_hw_params, 1360 .hw_free = rt711_sdca_pcm_hw_free, 1361 .set_sdw_stream = rt711_sdca_set_sdw_stream, 1362 .shutdown = rt711_sdca_shutdown, 1363 }; 1364 1365 static struct snd_soc_dai_driver rt711_sdca_dai[] = { 1366 { 1367 .name = "rt711-sdca-aif1", 1368 .id = RT711_AIF1, 1369 .playback = { 1370 .stream_name = "DP3 Playback", 1371 .channels_min = 1, 1372 .channels_max = 2, 1373 .rates = RT711_STEREO_RATES, 1374 .formats = RT711_FORMATS, 1375 }, 1376 .capture = { 1377 .stream_name = "DP2 Capture", 1378 .channels_min = 1, 1379 .channels_max = 2, 1380 .rates = RT711_STEREO_RATES, 1381 .formats = RT711_FORMATS, 1382 }, 1383 .ops = &rt711_sdca_ops, 1384 }, 1385 { 1386 .name = "rt711-sdca-aif2", 1387 .id = RT711_AIF2, 1388 .capture = { 1389 .stream_name = "DP4 Capture", 1390 .channels_min = 1, 1391 .channels_max = 2, 1392 .rates = RT711_STEREO_RATES, 1393 .formats = RT711_FORMATS, 1394 }, 1395 .ops = &rt711_sdca_ops, 1396 } 1397 }; 1398 1399 int rt711_sdca_init(struct device *dev, struct regmap *regmap, 1400 struct regmap *mbq_regmap, struct sdw_slave *slave) 1401 { 1402 struct rt711_sdca_priv *rt711; 1403 int ret; 1404 1405 rt711 = devm_kzalloc(dev, sizeof(*rt711), GFP_KERNEL); 1406 if (!rt711) 1407 return -ENOMEM; 1408 1409 dev_set_drvdata(dev, rt711); 1410 rt711->slave = slave; 1411 rt711->regmap = regmap; 1412 rt711->mbq_regmap = mbq_regmap; 1413 1414 /* 1415 * Mark hw_init to false 1416 * HW init will be performed when device reports present 1417 */ 1418 rt711->hw_init = false; 1419 rt711->first_hw_init = false; 1420 rt711->fu0f_dapm_mute = true; 1421 rt711->fu1e_dapm_mute = true; 1422 rt711->fu0f_mixer_l_mute = rt711->fu0f_mixer_r_mute = true; 1423 rt711->fu1e_mixer_l_mute = rt711->fu1e_mixer_r_mute = true; 1424 1425 /* JD source uses JD2 in default */ 1426 rt711->jd_src = RT711_JD2; 1427 1428 ret = devm_snd_soc_register_component(dev, 1429 &soc_sdca_dev_rt711, 1430 rt711_sdca_dai, 1431 ARRAY_SIZE(rt711_sdca_dai)); 1432 1433 dev_dbg(&slave->dev, "%s\n", __func__); 1434 1435 return ret; 1436 } 1437 1438 static void rt711_sdca_vd0_io_init(struct rt711_sdca_priv *rt711) 1439 { 1440 rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1441 RT711_GPIO_TEST_MODE_CTL2, 0x0e00); 1442 rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL, 1443 RT711_HDA_LEGACY_GPIO_CTL, 0x0008); 1444 1445 regmap_write(rt711->regmap, 0x2f5a, 0x01); 1446 1447 rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1448 RT711_ADC27_VOL_SET, 0x8728); 1449 1450 rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1451 RT711_COMBO_JACK_AUTO_CTL3, 0xa472); 1452 1453 regmap_write(rt711->regmap, 0x2f50, 0x02); 1454 1455 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_ANALOG_CTL, 1456 RT711_MISC_POWER_CTL4, 0x6000, 0x6000); 1457 1458 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 1459 RT711_COMBO_JACK_AUTO_CTL3, 0x000c, 0x000c); 1460 1461 rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL, 1462 RT711_HDA_LEGACY_CONFIG_CTL, 0x0000); 1463 1464 rt711_sdca_index_write(rt711, RT711_VENDOR_VAD, 1465 RT711_VAD_SRAM_CTL1, 0x0050); 1466 } 1467 1468 static void rt711_sdca_vd1_io_init(struct rt711_sdca_priv *rt711) 1469 { 1470 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 1471 RT711_HDA_LEGACY_UNSOLICITED_CTL, 0x0300, 0x0000); 1472 1473 rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1474 RT711_COMBO_JACK_AUTO_CTL3, 0xa43e); 1475 1476 regmap_write(rt711->regmap, 0x2f5a, 0x05); 1477 1478 rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1479 RT711_JD_CTRL6, 0x0500); 1480 1481 rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1482 RT711_DMIC_CTL1, 0x6173); 1483 1484 rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL, 1485 RT711_HDA_LEGACY_CONFIG_CTL, 0x0000); 1486 1487 rt711_sdca_index_write(rt711, RT711_VENDOR_VAD, 1488 RT711_VAD_SRAM_CTL1, 0x0050); 1489 } 1490 1491 int rt711_sdca_io_init(struct device *dev, struct sdw_slave *slave) 1492 { 1493 struct rt711_sdca_priv *rt711 = dev_get_drvdata(dev); 1494 int ret = 0; 1495 unsigned int val; 1496 1497 if (rt711->hw_init) 1498 return 0; 1499 1500 if (rt711->first_hw_init) { 1501 regcache_cache_only(rt711->regmap, false); 1502 regcache_cache_bypass(rt711->regmap, true); 1503 } else { 1504 /* 1505 * PM runtime is only enabled when a Slave reports as Attached 1506 */ 1507 1508 /* set autosuspend parameters */ 1509 pm_runtime_set_autosuspend_delay(&slave->dev, 3000); 1510 pm_runtime_use_autosuspend(&slave->dev); 1511 1512 /* update count of parent 'active' children */ 1513 pm_runtime_set_active(&slave->dev); 1514 1515 /* make sure the device does not suspend immediately */ 1516 pm_runtime_mark_last_busy(&slave->dev); 1517 1518 pm_runtime_enable(&slave->dev); 1519 } 1520 1521 pm_runtime_get_noresume(&slave->dev); 1522 1523 rt711_sdca_reset(rt711); 1524 1525 rt711_sdca_index_read(rt711, RT711_VENDOR_REG, RT711_JD_PRODUCT_NUM, &val); 1526 rt711->hw_ver = val & 0xf; 1527 1528 if (rt711->hw_ver == RT711_VER_VD0) 1529 rt711_sdca_vd0_io_init(rt711); 1530 else 1531 rt711_sdca_vd1_io_init(rt711); 1532 1533 /* DP4 mux select from 08_filter_Out_pri */ 1534 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 1535 RT711_FILTER_SRC_SEL, 0x1800, 0x0800); 1536 1537 /* ge_exclusive_inbox_en disable */ 1538 rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 1539 RT711_PUSH_BTN_INT_CTL0, 0x20, 0x00); 1540 1541 if (!rt711->first_hw_init) { 1542 INIT_DELAYED_WORK(&rt711->jack_detect_work, 1543 rt711_sdca_jack_detect_handler); 1544 INIT_DELAYED_WORK(&rt711->jack_btn_check_work, 1545 rt711_sdca_btn_check_handler); 1546 mutex_init(&rt711->calibrate_mutex); 1547 } 1548 1549 /* calibration */ 1550 ret = rt711_sdca_calibration(rt711); 1551 if (ret < 0) 1552 dev_err(dev, "%s, calibration failed!\n", __func__); 1553 1554 /* HP output enable */ 1555 regmap_write(rt711->regmap, 1556 SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_OT1, RT711_SDCA_CTL_VENDOR_DEF, 0), 0x4); 1557 1558 /* 1559 * if set_jack callback occurred early than io_init, 1560 * we set up the jack detection function now 1561 */ 1562 if (rt711->hs_jack) 1563 rt711_sdca_jack_init(rt711); 1564 1565 if (rt711->first_hw_init) { 1566 regcache_cache_bypass(rt711->regmap, false); 1567 regcache_mark_dirty(rt711->regmap); 1568 } else 1569 rt711->first_hw_init = true; 1570 1571 /* Mark Slave initialization complete */ 1572 rt711->hw_init = true; 1573 1574 pm_runtime_mark_last_busy(&slave->dev); 1575 pm_runtime_put_autosuspend(&slave->dev); 1576 1577 dev_dbg(&slave->dev, "%s hw_init complete\n", __func__); 1578 return 0; 1579 } 1580 1581 MODULE_DESCRIPTION("ASoC RT711 SDCA SDW driver"); 1582 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>"); 1583 MODULE_LICENSE("GPL"); 1584