1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // rt711.c -- rt711 ALSA SoC audio driver 4 // 5 // Copyright(c) 2019 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/pm.h> 16 #include <linux/soundwire/sdw.h> 17 #include <linux/regmap.h> 18 #include <linux/slab.h> 19 #include <sound/core.h> 20 #include <sound/pcm.h> 21 #include <sound/pcm_params.h> 22 #include <sound/soc.h> 23 #include <sound/soc-dapm.h> 24 #include <sound/initval.h> 25 #include <sound/tlv.h> 26 #include <sound/hda_verbs.h> 27 #include <sound/jack.h> 28 29 #include "rt711.h" 30 31 static int rt711_index_write(struct regmap *regmap, 32 unsigned int nid, unsigned int reg, unsigned int value) 33 { 34 int ret; 35 unsigned int addr = ((RT711_PRIV_INDEX_W_H | nid) << 8) | reg; 36 37 ret = regmap_write(regmap, addr, value); 38 if (ret < 0) 39 pr_err("Failed to set private value: %06x <= %04x ret=%d\n", 40 addr, value, ret); 41 42 return ret; 43 } 44 45 static int rt711_index_read(struct regmap *regmap, 46 unsigned int nid, unsigned int reg, unsigned int *value) 47 { 48 int ret; 49 unsigned int addr = ((RT711_PRIV_INDEX_W_H | nid) << 8) | reg; 50 51 *value = 0; 52 ret = regmap_read(regmap, addr, value); 53 if (ret < 0) 54 pr_err("Failed to get private value: %06x => %04x ret=%d\n", 55 addr, *value, ret); 56 57 return ret; 58 } 59 60 static int rt711_index_update_bits(struct regmap *regmap, unsigned int nid, 61 unsigned int reg, unsigned int mask, unsigned int val) 62 { 63 unsigned int tmp, orig; 64 int ret; 65 66 ret = rt711_index_read(regmap, nid, reg, &orig); 67 if (ret < 0) 68 return ret; 69 70 tmp = orig & ~mask; 71 tmp |= val & mask; 72 73 return rt711_index_write(regmap, nid, reg, tmp); 74 } 75 76 static void rt711_reset(struct regmap *regmap) 77 { 78 regmap_write(regmap, RT711_FUNC_RESET, 0); 79 rt711_index_update_bits(regmap, RT711_VENDOR_REG, 80 RT711_PARA_VERB_CTL, RT711_HIDDEN_REG_SW_RESET, 81 RT711_HIDDEN_REG_SW_RESET); 82 } 83 84 static int rt711_calibration(struct rt711_priv *rt711) 85 { 86 unsigned int val, loop = 0; 87 struct device *dev; 88 struct regmap *regmap = rt711->regmap; 89 int ret = 0; 90 91 mutex_lock(&rt711->calibrate_mutex); 92 regmap_write(rt711->regmap, 93 RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0); 94 95 dev = regmap_get_device(regmap); 96 97 /* Calibration manual mode */ 98 rt711_index_update_bits(regmap, RT711_VENDOR_REG, RT711_FSM_CTL, 99 0xf, 0x0); 100 101 /* trigger */ 102 rt711_index_update_bits(regmap, RT711_VENDOR_CALI, 103 RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_CALI_TRIGGER, 104 RT711_DAC_DC_CALI_TRIGGER); 105 106 /* wait for calibration process */ 107 rt711_index_read(regmap, RT711_VENDOR_CALI, 108 RT711_DAC_DC_CALI_CTL1, &val); 109 110 while (val & RT711_DAC_DC_CALI_TRIGGER) { 111 if (loop >= 500) { 112 pr_err("%s, calibration time-out!\n", 113 __func__); 114 ret = -ETIMEDOUT; 115 break; 116 } 117 loop++; 118 119 usleep_range(10000, 11000); 120 rt711_index_read(regmap, RT711_VENDOR_CALI, 121 RT711_DAC_DC_CALI_CTL1, &val); 122 } 123 124 /* depop mode */ 125 rt711_index_update_bits(regmap, RT711_VENDOR_REG, 126 RT711_FSM_CTL, 0xf, RT711_DEPOP_CTL); 127 128 regmap_write(rt711->regmap, 129 RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3); 130 mutex_unlock(&rt711->calibrate_mutex); 131 132 dev_dbg(dev, "%s calibration complete, ret=%d\n", __func__, ret); 133 return ret; 134 } 135 136 static unsigned int rt711_button_detect(struct rt711_priv *rt711) 137 { 138 unsigned int btn_type = 0, val80, val81; 139 int ret; 140 141 ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, 142 RT711_IRQ_FLAG_TABLE1, &val80); 143 if (ret < 0) 144 goto read_error; 145 ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, 146 RT711_IRQ_FLAG_TABLE2, &val81); 147 if (ret < 0) 148 goto read_error; 149 150 val80 &= 0x0381; 151 val81 &= 0xff00; 152 153 switch (val80) { 154 case 0x0200: 155 case 0x0100: 156 case 0x0080: 157 btn_type |= SND_JACK_BTN_0; 158 break; 159 case 0x0001: 160 btn_type |= SND_JACK_BTN_3; 161 break; 162 } 163 switch (val81) { 164 case 0x8000: 165 case 0x4000: 166 case 0x2000: 167 btn_type |= SND_JACK_BTN_1; 168 break; 169 case 0x1000: 170 case 0x0800: 171 case 0x0400: 172 btn_type |= SND_JACK_BTN_2; 173 break; 174 case 0x0200: 175 case 0x0100: 176 btn_type |= SND_JACK_BTN_3; 177 break; 178 } 179 read_error: 180 return btn_type; 181 } 182 183 static int rt711_headset_detect(struct rt711_priv *rt711) 184 { 185 unsigned int buf, loop = 0; 186 int ret; 187 unsigned int jack_status = 0, reg; 188 189 ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, 190 RT711_COMBO_JACK_AUTO_CTL2, &buf); 191 if (ret < 0) 192 goto io_error; 193 194 while (loop < 500 && 195 (buf & RT711_COMBOJACK_AUTO_DET_STATUS) == 0) { 196 loop++; 197 198 usleep_range(9000, 10000); 199 ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, 200 RT711_COMBO_JACK_AUTO_CTL2, &buf); 201 if (ret < 0) 202 goto io_error; 203 204 reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT; 205 ret = regmap_read(rt711->regmap, reg, &jack_status); 206 if (ret < 0) 207 goto io_error; 208 if ((jack_status & (1 << 31)) == 0) 209 goto remove_error; 210 } 211 212 if (loop >= 500) 213 goto to_error; 214 215 if (buf & RT711_COMBOJACK_AUTO_DET_TRS) 216 rt711->jack_type = SND_JACK_HEADPHONE; 217 else if ((buf & RT711_COMBOJACK_AUTO_DET_CTIA) || 218 (buf & RT711_COMBOJACK_AUTO_DET_OMTP)) 219 rt711->jack_type = SND_JACK_HEADSET; 220 221 return 0; 222 223 to_error: 224 ret = -ETIMEDOUT; 225 pr_err_ratelimited("Time-out error in %s\n", __func__); 226 return ret; 227 io_error: 228 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); 229 return ret; 230 remove_error: 231 pr_err_ratelimited("Jack removal in %s\n", __func__); 232 return -ENODEV; 233 } 234 235 static void rt711_jack_detect_handler(struct work_struct *work) 236 { 237 struct rt711_priv *rt711 = 238 container_of(work, struct rt711_priv, jack_detect_work.work); 239 int btn_type = 0, ret; 240 unsigned int jack_status = 0, reg; 241 242 if (!rt711->hs_jack) 243 return; 244 245 if (!rt711->component->card->instantiated) 246 return; 247 248 reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT; 249 ret = regmap_read(rt711->regmap, reg, &jack_status); 250 if (ret < 0) 251 goto io_error; 252 253 /* pin attached */ 254 if (jack_status & (1 << 31)) { 255 /* jack in */ 256 if (rt711->jack_type == 0) { 257 ret = rt711_headset_detect(rt711); 258 if (ret < 0) 259 return; 260 if (rt711->jack_type == SND_JACK_HEADSET) 261 btn_type = rt711_button_detect(rt711); 262 } else if (rt711->jack_type == SND_JACK_HEADSET) { 263 /* jack is already in, report button event */ 264 btn_type = rt711_button_detect(rt711); 265 } 266 } else { 267 /* jack out */ 268 rt711->jack_type = 0; 269 } 270 271 dev_dbg(&rt711->slave->dev, 272 "in %s, jack_type=0x%x\n", __func__, rt711->jack_type); 273 dev_dbg(&rt711->slave->dev, 274 "in %s, btn_type=0x%x\n", __func__, btn_type); 275 276 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type, 277 SND_JACK_HEADSET | 278 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 279 SND_JACK_BTN_2 | SND_JACK_BTN_3); 280 281 if (btn_type) { 282 /* button released */ 283 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type, 284 SND_JACK_HEADSET | 285 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 286 SND_JACK_BTN_2 | SND_JACK_BTN_3); 287 288 mod_delayed_work(system_power_efficient_wq, 289 &rt711->jack_btn_check_work, msecs_to_jiffies(200)); 290 } 291 292 return; 293 294 io_error: 295 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); 296 } 297 298 static void rt711_btn_check_handler(struct work_struct *work) 299 { 300 struct rt711_priv *rt711 = container_of(work, struct rt711_priv, 301 jack_btn_check_work.work); 302 int btn_type = 0, ret; 303 unsigned int jack_status = 0, reg; 304 305 reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT; 306 ret = regmap_read(rt711->regmap, reg, &jack_status); 307 if (ret < 0) 308 goto io_error; 309 310 /* pin attached */ 311 if (jack_status & (1 << 31)) { 312 if (rt711->jack_type == SND_JACK_HEADSET) { 313 /* jack is already in, report button event */ 314 btn_type = rt711_button_detect(rt711); 315 } 316 } else { 317 rt711->jack_type = 0; 318 } 319 320 /* cbj comparator */ 321 ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, 322 RT711_COMBO_JACK_AUTO_CTL2, ®); 323 if (ret < 0) 324 goto io_error; 325 326 if ((reg & 0xf0) == 0xf0) 327 btn_type = 0; 328 329 dev_dbg(&rt711->slave->dev, 330 "%s, btn_type=0x%x\n", __func__, btn_type); 331 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type, 332 SND_JACK_HEADSET | 333 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 334 SND_JACK_BTN_2 | SND_JACK_BTN_3); 335 336 if (btn_type) { 337 /* button released */ 338 snd_soc_jack_report(rt711->hs_jack, rt711->jack_type, 339 SND_JACK_HEADSET | 340 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 341 SND_JACK_BTN_2 | SND_JACK_BTN_3); 342 343 mod_delayed_work(system_power_efficient_wq, 344 &rt711->jack_btn_check_work, msecs_to_jiffies(200)); 345 } 346 347 return; 348 349 io_error: 350 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); 351 } 352 353 static void rt711_jack_init(struct rt711_priv *rt711) 354 { 355 struct snd_soc_dapm_context *dapm = 356 snd_soc_component_get_dapm(rt711->component); 357 358 mutex_lock(&rt711->calibrate_mutex); 359 /* power on */ 360 if (dapm->bias_level <= SND_SOC_BIAS_STANDBY) 361 regmap_write(rt711->regmap, 362 RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0); 363 364 if (rt711->hs_jack) { 365 /* unsolicited response & IRQ control */ 366 regmap_write(rt711->regmap, 367 RT711_SET_MIC2_UNSOLICITED_ENABLE, 0x82); 368 regmap_write(rt711->regmap, 369 RT711_SET_HP_UNSOLICITED_ENABLE, 0x81); 370 regmap_write(rt711->regmap, 371 RT711_SET_INLINE_UNSOLICITED_ENABLE, 0x83); 372 rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 373 0x10, 0x2420); 374 rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 375 0x19, 0x2e11); 376 377 switch (rt711->jd_src) { 378 case RT711_JD1: 379 /* default settings was already for JD1 */ 380 break; 381 case RT711_JD2: 382 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 383 RT711_JD_CTL2, RT711_JD2_2PORT_200K_DECODE_HP | 384 RT711_HP_JD_SEL_JD2, 385 RT711_JD2_2PORT_200K_DECODE_HP | 386 RT711_HP_JD_SEL_JD2); 387 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 388 RT711_CC_DET1, 389 RT711_HP_JD_FINAL_RESULT_CTL_JD12, 390 RT711_HP_JD_FINAL_RESULT_CTL_JD12); 391 break; 392 case RT711_JD2_100K: 393 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 394 RT711_JD_CTL2, RT711_JD2_2PORT_100K_DECODE | RT711_JD2_1PORT_TYPE_DECODE | 395 RT711_HP_JD_SEL_JD2 | RT711_JD1_2PORT_TYPE_100K_DECODE, 396 RT711_JD2_2PORT_100K_DECODE_HP | RT711_JD2_1PORT_JD_HP | 397 RT711_HP_JD_SEL_JD2 | RT711_JD1_2PORT_JD_RESERVED); 398 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 399 RT711_CC_DET1, 400 RT711_HP_JD_FINAL_RESULT_CTL_JD12, 401 RT711_HP_JD_FINAL_RESULT_CTL_JD12); 402 break; 403 case RT711_JD2_1P8V_1PORT: 404 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 405 RT711_JD_CTL1, RT711_JD2_DIGITAL_JD_MODE_SEL, 406 RT711_JD2_1_JD_MODE); 407 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 408 RT711_JD_CTL2, RT711_JD2_1PORT_TYPE_DECODE | 409 RT711_HP_JD_SEL_JD2, 410 RT711_JD2_1PORT_JD_HP | 411 RT711_HP_JD_SEL_JD2); 412 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 413 RT711_JD_CTL4, RT711_JD2_PAD_PULL_UP_MASK | 414 RT711_JD2_MODE_SEL_MASK, 415 RT711_JD2_PAD_PULL_UP | 416 RT711_JD2_MODE2_1P8V_1PORT); 417 rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 418 RT711_CC_DET1, 419 RT711_HP_JD_FINAL_RESULT_CTL_JD12, 420 RT711_HP_JD_FINAL_RESULT_CTL_JD12); 421 break; 422 default: 423 dev_warn(rt711->component->dev, "Wrong JD source\n"); 424 break; 425 } 426 427 dev_dbg(&rt711->slave->dev, "in %s enable\n", __func__); 428 429 mod_delayed_work(system_power_efficient_wq, 430 &rt711->jack_detect_work, msecs_to_jiffies(250)); 431 } else { 432 regmap_write(rt711->regmap, 433 RT711_SET_MIC2_UNSOLICITED_ENABLE, 0x00); 434 regmap_write(rt711->regmap, 435 RT711_SET_HP_UNSOLICITED_ENABLE, 0x00); 436 regmap_write(rt711->regmap, 437 RT711_SET_INLINE_UNSOLICITED_ENABLE, 0x00); 438 439 dev_dbg(&rt711->slave->dev, "in %s disable\n", __func__); 440 } 441 442 /* power off */ 443 if (dapm->bias_level <= SND_SOC_BIAS_STANDBY) 444 regmap_write(rt711->regmap, 445 RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3); 446 mutex_unlock(&rt711->calibrate_mutex); 447 } 448 449 static int rt711_set_jack_detect(struct snd_soc_component *component, 450 struct snd_soc_jack *hs_jack, void *data) 451 { 452 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 453 454 rt711->hs_jack = hs_jack; 455 456 if (!rt711->hw_init) { 457 dev_dbg(&rt711->slave->dev, 458 "%s hw_init not ready yet\n", __func__); 459 return 0; 460 } 461 462 rt711_jack_init(rt711); 463 464 return 0; 465 } 466 467 static void rt711_get_gain(struct rt711_priv *rt711, unsigned int addr_h, 468 unsigned int addr_l, unsigned int val_h, 469 unsigned int *r_val, unsigned int *l_val) 470 { 471 /* R Channel */ 472 *r_val = (val_h << 8); 473 regmap_read(rt711->regmap, addr_l, r_val); 474 475 /* L Channel */ 476 val_h |= 0x20; 477 *l_val = (val_h << 8); 478 regmap_read(rt711->regmap, addr_h, l_val); 479 } 480 481 /* For Verb-Set Amplifier Gain (Verb ID = 3h) */ 482 static int rt711_set_amp_gain_put(struct snd_kcontrol *kcontrol, 483 struct snd_ctl_elem_value *ucontrol) 484 { 485 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 486 struct snd_soc_dapm_context *dapm = 487 snd_soc_component_get_dapm(component); 488 struct soc_mixer_control *mc = 489 (struct soc_mixer_control *)kcontrol->private_value; 490 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 491 unsigned int addr_h, addr_l, val_h, val_ll, val_lr; 492 unsigned int read_ll, read_rl; 493 int i; 494 495 mutex_lock(&rt711->calibrate_mutex); 496 497 /* Can't use update bit function, so read the original value first */ 498 addr_h = mc->reg; 499 addr_l = mc->rreg; 500 if (mc->shift == RT711_DIR_OUT_SFT) /* output */ 501 val_h = 0x80; 502 else /* input */ 503 val_h = 0x0; 504 505 rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll); 506 507 /* L Channel */ 508 if (mc->invert) { 509 /* for mute/unmute */ 510 val_ll = (mc->max - ucontrol->value.integer.value[0]) 511 << RT711_MUTE_SFT; 512 /* keep gain */ 513 read_ll = read_ll & 0x7f; 514 val_ll |= read_ll; 515 } else { 516 /* for gain */ 517 val_ll = ((ucontrol->value.integer.value[0]) & 0x7f); 518 if (val_ll > mc->max) 519 val_ll = mc->max; 520 /* keep mute status */ 521 read_ll = read_ll & (1 << RT711_MUTE_SFT); 522 val_ll |= read_ll; 523 } 524 525 if (dapm->bias_level <= SND_SOC_BIAS_STANDBY) 526 regmap_write(rt711->regmap, 527 RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0); 528 529 /* R Channel */ 530 if (mc->invert) { 531 /* for mute/unmute */ 532 val_lr = (mc->max - ucontrol->value.integer.value[1]) 533 << RT711_MUTE_SFT; 534 /* keep gain */ 535 read_rl = read_rl & 0x7f; 536 val_lr |= read_rl; 537 } else { 538 /* for gain */ 539 val_lr = ((ucontrol->value.integer.value[1]) & 0x7f); 540 if (val_lr > mc->max) 541 val_lr = mc->max; 542 /* keep mute status */ 543 read_rl = read_rl & (1 << RT711_MUTE_SFT); 544 val_lr |= read_rl; 545 } 546 547 for (i = 0; i < 3; i++) { /* retry 3 times at most */ 548 549 if (val_ll == val_lr) { 550 /* Set both L/R channels at the same time */ 551 val_h = (1 << mc->shift) | (3 << 4); 552 regmap_write(rt711->regmap, 553 addr_h, (val_h << 8 | val_ll)); 554 regmap_write(rt711->regmap, 555 addr_l, (val_h << 8 | val_ll)); 556 } else { 557 /* Lch*/ 558 val_h = (1 << mc->shift) | (1 << 5); 559 regmap_write(rt711->regmap, 560 addr_h, (val_h << 8 | val_ll)); 561 562 /* Rch */ 563 val_h = (1 << mc->shift) | (1 << 4); 564 regmap_write(rt711->regmap, 565 addr_l, (val_h << 8 | val_lr)); 566 } 567 /* check result */ 568 if (mc->shift == RT711_DIR_OUT_SFT) /* output */ 569 val_h = 0x80; 570 else /* input */ 571 val_h = 0x0; 572 573 rt711_get_gain(rt711, addr_h, addr_l, val_h, 574 &read_rl, &read_ll); 575 if (read_rl == val_lr && read_ll == val_ll) 576 break; 577 } 578 579 if (dapm->bias_level <= SND_SOC_BIAS_STANDBY) 580 regmap_write(rt711->regmap, 581 RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3); 582 583 mutex_unlock(&rt711->calibrate_mutex); 584 return 0; 585 } 586 587 static int rt711_set_amp_gain_get(struct snd_kcontrol *kcontrol, 588 struct snd_ctl_elem_value *ucontrol) 589 { 590 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 591 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 592 struct soc_mixer_control *mc = 593 (struct soc_mixer_control *)kcontrol->private_value; 594 unsigned int addr_h, addr_l, val_h; 595 unsigned int read_ll, read_rl; 596 597 /* switch to get command */ 598 addr_h = mc->reg; 599 addr_l = mc->rreg; 600 if (mc->shift == RT711_DIR_OUT_SFT) /* output */ 601 val_h = 0x80; 602 else /* input */ 603 val_h = 0x0; 604 605 rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll); 606 607 if (mc->invert) { 608 /* mute/unmute for switch controls */ 609 read_ll = !((read_ll & 0x80) >> RT711_MUTE_SFT); 610 read_rl = !((read_rl & 0x80) >> RT711_MUTE_SFT); 611 } else { 612 /* for gain volume controls */ 613 read_ll = read_ll & 0x7f; 614 read_rl = read_rl & 0x7f; 615 } 616 ucontrol->value.integer.value[0] = read_ll; 617 ucontrol->value.integer.value[1] = read_rl; 618 619 return 0; 620 } 621 622 static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0); 623 static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0); 624 static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0); 625 626 static const struct snd_kcontrol_new rt711_snd_controls[] = { 627 SOC_DOUBLE_R_EXT_TLV("DAC Surr Playback Volume", 628 RT711_SET_GAIN_DAC2_H, RT711_SET_GAIN_DAC2_L, 629 RT711_DIR_OUT_SFT, 0x57, 0, 630 rt711_set_amp_gain_get, rt711_set_amp_gain_put, out_vol_tlv), 631 SOC_DOUBLE_R_EXT("ADC 08 Capture Switch", 632 RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L, 633 RT711_DIR_IN_SFT, 1, 1, 634 rt711_set_amp_gain_get, rt711_set_amp_gain_put), 635 SOC_DOUBLE_R_EXT("ADC 09 Capture Switch", 636 RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L, 637 RT711_DIR_IN_SFT, 1, 1, 638 rt711_set_amp_gain_get, rt711_set_amp_gain_put), 639 SOC_DOUBLE_R_EXT_TLV("ADC 08 Capture Volume", 640 RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L, 641 RT711_DIR_IN_SFT, 0x3f, 0, 642 rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv), 643 SOC_DOUBLE_R_EXT_TLV("ADC 09 Capture Volume", 644 RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L, 645 RT711_DIR_IN_SFT, 0x3f, 0, 646 rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv), 647 SOC_DOUBLE_R_EXT_TLV("AMIC Volume", 648 RT711_SET_GAIN_AMIC_H, RT711_SET_GAIN_AMIC_L, 649 RT711_DIR_IN_SFT, 3, 0, 650 rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv), 651 SOC_DOUBLE_R_EXT_TLV("DMIC1 Volume", 652 RT711_SET_GAIN_DMIC1_H, RT711_SET_GAIN_DMIC1_L, 653 RT711_DIR_IN_SFT, 3, 0, 654 rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv), 655 SOC_DOUBLE_R_EXT_TLV("DMIC2 Volume", 656 RT711_SET_GAIN_DMIC2_H, RT711_SET_GAIN_DMIC2_L, 657 RT711_DIR_IN_SFT, 3, 0, 658 rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv), 659 }; 660 661 static int rt711_mux_get(struct snd_kcontrol *kcontrol, 662 struct snd_ctl_elem_value *ucontrol) 663 { 664 struct snd_soc_component *component = 665 snd_soc_dapm_kcontrol_component(kcontrol); 666 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 667 unsigned int reg, val = 0, nid; 668 int ret; 669 670 if (strstr(ucontrol->id.name, "ADC 22 Mux")) 671 nid = RT711_MIXER_IN1; 672 else if (strstr(ucontrol->id.name, "ADC 23 Mux")) 673 nid = RT711_MIXER_IN2; 674 else 675 return -EINVAL; 676 677 /* vid = 0xf01 */ 678 reg = RT711_VERB_SET_CONNECT_SEL | nid; 679 ret = regmap_read(rt711->regmap, reg, &val); 680 if (ret < 0) { 681 dev_err(component->dev, "%s: sdw read failed: %d\n", 682 __func__, ret); 683 return ret; 684 } 685 686 ucontrol->value.enumerated.item[0] = val; 687 688 return 0; 689 } 690 691 static int rt711_mux_put(struct snd_kcontrol *kcontrol, 692 struct snd_ctl_elem_value *ucontrol) 693 { 694 struct snd_soc_component *component = 695 snd_soc_dapm_kcontrol_component(kcontrol); 696 struct snd_soc_dapm_context *dapm = 697 snd_soc_dapm_kcontrol_dapm(kcontrol); 698 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 699 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 700 unsigned int *item = ucontrol->value.enumerated.item; 701 unsigned int val, val2 = 0, change, reg, nid; 702 int ret; 703 704 if (item[0] >= e->items) 705 return -EINVAL; 706 707 if (strstr(ucontrol->id.name, "ADC 22 Mux")) 708 nid = RT711_MIXER_IN1; 709 else if (strstr(ucontrol->id.name, "ADC 23 Mux")) 710 nid = RT711_MIXER_IN2; 711 else 712 return -EINVAL; 713 714 /* Verb ID = 0x701h */ 715 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; 716 717 reg = RT711_VERB_SET_CONNECT_SEL | nid; 718 ret = regmap_read(rt711->regmap, reg, &val2); 719 if (ret < 0) { 720 dev_err(component->dev, "%s: sdw read failed: %d\n", 721 __func__, ret); 722 return ret; 723 } 724 725 if (val == val2) 726 change = 0; 727 else 728 change = 1; 729 730 if (change) { 731 reg = RT711_VERB_SET_CONNECT_SEL | nid; 732 regmap_write(rt711->regmap, reg, val); 733 } 734 735 snd_soc_dapm_mux_update_power(dapm, kcontrol, 736 item[0], e, NULL); 737 738 return change; 739 } 740 741 static const char * const adc_mux_text[] = { 742 "MIC2", 743 "LINE1", 744 "LINE2", 745 "DMIC", 746 }; 747 748 static SOC_ENUM_SINGLE_DECL( 749 rt711_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text); 750 751 static SOC_ENUM_SINGLE_DECL( 752 rt711_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text); 753 754 static const struct snd_kcontrol_new rt711_adc22_mux = 755 SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt711_adc22_enum, 756 rt711_mux_get, rt711_mux_put); 757 758 static const struct snd_kcontrol_new rt711_adc23_mux = 759 SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt711_adc23_enum, 760 rt711_mux_get, rt711_mux_put); 761 762 static int rt711_dac_surround_event(struct snd_soc_dapm_widget *w, 763 struct snd_kcontrol *kcontrol, int event) 764 { 765 struct snd_soc_component *component = 766 snd_soc_dapm_to_component(w->dapm); 767 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 768 unsigned int val_h = (1 << RT711_DIR_OUT_SFT) | (0x3 << 4); 769 unsigned int val_l; 770 771 switch (event) { 772 case SND_SOC_DAPM_POST_PMU: 773 regmap_write(rt711->regmap, 774 RT711_SET_STREAMID_DAC2, 0x10); 775 776 val_l = 0x00; 777 regmap_write(rt711->regmap, 778 RT711_SET_GAIN_HP_H, (val_h << 8 | val_l)); 779 break; 780 case SND_SOC_DAPM_PRE_PMD: 781 val_l = (1 << RT711_MUTE_SFT); 782 regmap_write(rt711->regmap, 783 RT711_SET_GAIN_HP_H, (val_h << 8 | val_l)); 784 usleep_range(50000, 55000); 785 786 regmap_write(rt711->regmap, 787 RT711_SET_STREAMID_DAC2, 0x00); 788 break; 789 } 790 return 0; 791 } 792 793 static int rt711_adc_09_event(struct snd_soc_dapm_widget *w, 794 struct snd_kcontrol *kcontrol, int event) 795 { 796 struct snd_soc_component *component = 797 snd_soc_dapm_to_component(w->dapm); 798 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 799 800 switch (event) { 801 case SND_SOC_DAPM_POST_PMU: 802 regmap_write(rt711->regmap, 803 RT711_SET_STREAMID_ADC1, 0x10); 804 break; 805 case SND_SOC_DAPM_PRE_PMD: 806 regmap_write(rt711->regmap, 807 RT711_SET_STREAMID_ADC1, 0x00); 808 break; 809 } 810 return 0; 811 } 812 813 static int rt711_adc_08_event(struct snd_soc_dapm_widget *w, 814 struct snd_kcontrol *kcontrol, int event) 815 { 816 struct snd_soc_component *component = 817 snd_soc_dapm_to_component(w->dapm); 818 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 819 820 switch (event) { 821 case SND_SOC_DAPM_POST_PMU: 822 regmap_write(rt711->regmap, 823 RT711_SET_STREAMID_ADC2, 0x10); 824 break; 825 case SND_SOC_DAPM_PRE_PMD: 826 regmap_write(rt711->regmap, 827 RT711_SET_STREAMID_ADC2, 0x00); 828 break; 829 } 830 return 0; 831 } 832 833 static const struct snd_soc_dapm_widget rt711_dapm_widgets[] = { 834 SND_SOC_DAPM_OUTPUT("HP"), 835 SND_SOC_DAPM_INPUT("MIC2"), 836 SND_SOC_DAPM_INPUT("DMIC1"), 837 SND_SOC_DAPM_INPUT("DMIC2"), 838 SND_SOC_DAPM_INPUT("LINE1"), 839 SND_SOC_DAPM_INPUT("LINE2"), 840 841 SND_SOC_DAPM_DAC_E("DAC Surround", NULL, SND_SOC_NOPM, 0, 0, 842 rt711_dac_surround_event, 843 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 844 SND_SOC_DAPM_ADC_E("ADC 09", NULL, SND_SOC_NOPM, 0, 0, 845 rt711_adc_09_event, 846 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 847 SND_SOC_DAPM_ADC_E("ADC 08", NULL, SND_SOC_NOPM, 0, 0, 848 rt711_adc_08_event, 849 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 850 SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0, 851 &rt711_adc22_mux), 852 SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0, 853 &rt711_adc23_mux), 854 855 SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0), 856 SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0), 857 SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0), 858 }; 859 860 static const struct snd_soc_dapm_route rt711_audio_map[] = { 861 {"DAC Surround", NULL, "DP3RX"}, 862 {"DP2TX", NULL, "ADC 09"}, 863 {"DP4TX", NULL, "ADC 08"}, 864 865 {"ADC 09", NULL, "ADC 22 Mux"}, 866 {"ADC 08", NULL, "ADC 23 Mux"}, 867 {"ADC 22 Mux", "DMIC", "DMIC1"}, 868 {"ADC 22 Mux", "LINE1", "LINE1"}, 869 {"ADC 22 Mux", "LINE2", "LINE2"}, 870 {"ADC 22 Mux", "MIC2", "MIC2"}, 871 {"ADC 23 Mux", "DMIC", "DMIC2"}, 872 {"ADC 23 Mux", "LINE1", "LINE1"}, 873 {"ADC 23 Mux", "LINE2", "LINE2"}, 874 {"ADC 23 Mux", "MIC2", "MIC2"}, 875 876 {"HP", NULL, "DAC Surround"}, 877 }; 878 879 static int rt711_set_bias_level(struct snd_soc_component *component, 880 enum snd_soc_bias_level level) 881 { 882 struct snd_soc_dapm_context *dapm = 883 snd_soc_component_get_dapm(component); 884 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 885 886 switch (level) { 887 case SND_SOC_BIAS_PREPARE: 888 if (dapm->bias_level == SND_SOC_BIAS_STANDBY) { 889 regmap_write(rt711->regmap, 890 RT711_SET_AUDIO_POWER_STATE, 891 AC_PWRST_D0); 892 } 893 break; 894 895 case SND_SOC_BIAS_STANDBY: 896 mutex_lock(&rt711->calibrate_mutex); 897 regmap_write(rt711->regmap, 898 RT711_SET_AUDIO_POWER_STATE, 899 AC_PWRST_D3); 900 mutex_unlock(&rt711->calibrate_mutex); 901 break; 902 903 default: 904 break; 905 } 906 907 return 0; 908 } 909 910 static int rt711_parse_dt(struct rt711_priv *rt711, struct device *dev) 911 { 912 device_property_read_u32(dev, "realtek,jd-src", 913 &rt711->jd_src); 914 915 return 0; 916 } 917 918 static int rt711_probe(struct snd_soc_component *component) 919 { 920 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 921 922 rt711_parse_dt(rt711, &rt711->slave->dev); 923 rt711->component = component; 924 925 return 0; 926 } 927 928 static void rt711_remove(struct snd_soc_component *component) 929 { 930 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 931 932 regcache_cache_only(rt711->regmap, true); 933 } 934 935 static const struct snd_soc_component_driver soc_codec_dev_rt711 = { 936 .probe = rt711_probe, 937 .set_bias_level = rt711_set_bias_level, 938 .controls = rt711_snd_controls, 939 .num_controls = ARRAY_SIZE(rt711_snd_controls), 940 .dapm_widgets = rt711_dapm_widgets, 941 .num_dapm_widgets = ARRAY_SIZE(rt711_dapm_widgets), 942 .dapm_routes = rt711_audio_map, 943 .num_dapm_routes = ARRAY_SIZE(rt711_audio_map), 944 .set_jack = rt711_set_jack_detect, 945 .remove = rt711_remove, 946 }; 947 948 static int rt711_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream, 949 int direction) 950 { 951 struct sdw_stream_data *stream; 952 953 if (!sdw_stream) 954 return 0; 955 956 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 957 if (!stream) 958 return -ENOMEM; 959 960 stream->sdw_stream = sdw_stream; 961 962 /* Use tx_mask or rx_mask to configure stream tag and set dma_data */ 963 if (direction == SNDRV_PCM_STREAM_PLAYBACK) 964 dai->playback_dma_data = stream; 965 else 966 dai->capture_dma_data = stream; 967 968 return 0; 969 } 970 971 static void rt711_shutdown(struct snd_pcm_substream *substream, 972 struct snd_soc_dai *dai) 973 { 974 struct sdw_stream_data *stream; 975 976 stream = snd_soc_dai_get_dma_data(dai, substream); 977 snd_soc_dai_set_dma_data(dai, substream, NULL); 978 kfree(stream); 979 } 980 981 static int rt711_pcm_hw_params(struct snd_pcm_substream *substream, 982 struct snd_pcm_hw_params *params, 983 struct snd_soc_dai *dai) 984 { 985 struct snd_soc_component *component = dai->component; 986 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 987 struct sdw_stream_config stream_config; 988 struct sdw_port_config port_config; 989 enum sdw_data_direction direction; 990 struct sdw_stream_data *stream; 991 int retval, port, num_channels; 992 unsigned int val = 0; 993 994 dev_dbg(dai->dev, "%s %s", __func__, dai->name); 995 stream = snd_soc_dai_get_dma_data(dai, substream); 996 997 if (!stream) 998 return -EINVAL; 999 1000 if (!rt711->slave) 1001 return -EINVAL; 1002 1003 /* SoundWire specific configuration */ 1004 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1005 direction = SDW_DATA_DIR_RX; 1006 port = 3; 1007 } else { 1008 direction = SDW_DATA_DIR_TX; 1009 if (dai->id == RT711_AIF1) 1010 port = 4; 1011 else if (dai->id == RT711_AIF2) 1012 port = 2; 1013 else 1014 return -EINVAL; 1015 } 1016 1017 stream_config.frame_rate = params_rate(params); 1018 stream_config.ch_count = params_channels(params); 1019 stream_config.bps = snd_pcm_format_width(params_format(params)); 1020 stream_config.direction = direction; 1021 1022 num_channels = params_channels(params); 1023 port_config.ch_mask = (1 << (num_channels)) - 1; 1024 port_config.num = port; 1025 1026 retval = sdw_stream_add_slave(rt711->slave, &stream_config, 1027 &port_config, 1, stream->sdw_stream); 1028 if (retval) { 1029 dev_err(dai->dev, "Unable to configure port\n"); 1030 return retval; 1031 } 1032 1033 if (params_channels(params) <= 16) { 1034 /* bit 3:0 Number of Channel */ 1035 val |= (params_channels(params) - 1); 1036 } else { 1037 dev_err(component->dev, "Unsupported channels %d\n", 1038 params_channels(params)); 1039 return -EINVAL; 1040 } 1041 1042 switch (params_width(params)) { 1043 /* bit 6:4 Bits per Sample */ 1044 case 8: 1045 break; 1046 case 16: 1047 val |= (0x1 << 4); 1048 break; 1049 case 20: 1050 val |= (0x2 << 4); 1051 break; 1052 case 24: 1053 val |= (0x3 << 4); 1054 break; 1055 case 32: 1056 val |= (0x4 << 4); 1057 break; 1058 default: 1059 return -EINVAL; 1060 } 1061 1062 /* 48Khz */ 1063 regmap_write(rt711->regmap, RT711_DAC_FORMAT_H, val); 1064 regmap_write(rt711->regmap, RT711_ADC1_FORMAT_H, val); 1065 regmap_write(rt711->regmap, RT711_ADC2_FORMAT_H, val); 1066 1067 return retval; 1068 } 1069 1070 static int rt711_pcm_hw_free(struct snd_pcm_substream *substream, 1071 struct snd_soc_dai *dai) 1072 { 1073 struct snd_soc_component *component = dai->component; 1074 struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 1075 struct sdw_stream_data *stream = 1076 snd_soc_dai_get_dma_data(dai, substream); 1077 1078 if (!rt711->slave) 1079 return -EINVAL; 1080 1081 sdw_stream_remove_slave(rt711->slave, stream->sdw_stream); 1082 return 0; 1083 } 1084 1085 #define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000) 1086 #define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ 1087 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8) 1088 1089 static const struct snd_soc_dai_ops rt711_ops = { 1090 .hw_params = rt711_pcm_hw_params, 1091 .hw_free = rt711_pcm_hw_free, 1092 .set_sdw_stream = rt711_set_sdw_stream, 1093 .shutdown = rt711_shutdown, 1094 }; 1095 1096 static struct snd_soc_dai_driver rt711_dai[] = { 1097 { 1098 .name = "rt711-aif1", 1099 .id = RT711_AIF1, 1100 .playback = { 1101 .stream_name = "DP3 Playback", 1102 .channels_min = 1, 1103 .channels_max = 2, 1104 .rates = RT711_STEREO_RATES, 1105 .formats = RT711_FORMATS, 1106 }, 1107 .capture = { 1108 .stream_name = "DP4 Capture", 1109 .channels_min = 1, 1110 .channels_max = 2, 1111 .rates = RT711_STEREO_RATES, 1112 .formats = RT711_FORMATS, 1113 }, 1114 .ops = &rt711_ops, 1115 }, 1116 { 1117 .name = "rt711-aif2", 1118 .id = RT711_AIF2, 1119 .capture = { 1120 .stream_name = "DP2 Capture", 1121 .channels_min = 1, 1122 .channels_max = 2, 1123 .rates = RT711_STEREO_RATES, 1124 .formats = RT711_FORMATS, 1125 }, 1126 .ops = &rt711_ops, 1127 } 1128 }; 1129 1130 /* Bus clock frequency */ 1131 #define RT711_CLK_FREQ_9600000HZ 9600000 1132 #define RT711_CLK_FREQ_12000000HZ 12000000 1133 #define RT711_CLK_FREQ_6000000HZ 6000000 1134 #define RT711_CLK_FREQ_4800000HZ 4800000 1135 #define RT711_CLK_FREQ_2400000HZ 2400000 1136 #define RT711_CLK_FREQ_12288000HZ 12288000 1137 1138 int rt711_clock_config(struct device *dev) 1139 { 1140 struct rt711_priv *rt711 = dev_get_drvdata(dev); 1141 unsigned int clk_freq, value; 1142 1143 clk_freq = (rt711->params.curr_dr_freq >> 1); 1144 1145 switch (clk_freq) { 1146 case RT711_CLK_FREQ_12000000HZ: 1147 value = 0x0; 1148 break; 1149 case RT711_CLK_FREQ_6000000HZ: 1150 value = 0x1; 1151 break; 1152 case RT711_CLK_FREQ_9600000HZ: 1153 value = 0x2; 1154 break; 1155 case RT711_CLK_FREQ_4800000HZ: 1156 value = 0x3; 1157 break; 1158 case RT711_CLK_FREQ_2400000HZ: 1159 value = 0x4; 1160 break; 1161 case RT711_CLK_FREQ_12288000HZ: 1162 value = 0x5; 1163 break; 1164 default: 1165 return -EINVAL; 1166 } 1167 1168 regmap_write(rt711->regmap, 0xe0, value); 1169 regmap_write(rt711->regmap, 0xf0, value); 1170 1171 dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq); 1172 1173 return 0; 1174 } 1175 1176 static void rt711_calibration_work(struct work_struct *work) 1177 { 1178 struct rt711_priv *rt711 = 1179 container_of(work, struct rt711_priv, calibration_work); 1180 1181 rt711_calibration(rt711); 1182 } 1183 1184 int rt711_init(struct device *dev, struct regmap *sdw_regmap, 1185 struct regmap *regmap, struct sdw_slave *slave) 1186 { 1187 struct rt711_priv *rt711; 1188 int ret; 1189 1190 rt711 = devm_kzalloc(dev, sizeof(*rt711), GFP_KERNEL); 1191 if (!rt711) 1192 return -ENOMEM; 1193 1194 dev_set_drvdata(dev, rt711); 1195 rt711->slave = slave; 1196 rt711->sdw_regmap = sdw_regmap; 1197 rt711->regmap = regmap; 1198 1199 mutex_init(&rt711->disable_irq_lock); 1200 1201 /* 1202 * Mark hw_init to false 1203 * HW init will be performed when device reports present 1204 */ 1205 rt711->hw_init = false; 1206 rt711->first_hw_init = false; 1207 1208 /* JD source uses JD2 in default */ 1209 rt711->jd_src = RT711_JD2; 1210 1211 ret = devm_snd_soc_register_component(dev, 1212 &soc_codec_dev_rt711, 1213 rt711_dai, 1214 ARRAY_SIZE(rt711_dai)); 1215 1216 dev_dbg(&slave->dev, "%s\n", __func__); 1217 1218 return ret; 1219 } 1220 1221 int rt711_io_init(struct device *dev, struct sdw_slave *slave) 1222 { 1223 struct rt711_priv *rt711 = dev_get_drvdata(dev); 1224 1225 rt711->disable_irq = false; 1226 1227 if (rt711->hw_init) 1228 return 0; 1229 1230 if (rt711->first_hw_init) { 1231 regcache_cache_only(rt711->regmap, false); 1232 regcache_cache_bypass(rt711->regmap, true); 1233 } 1234 1235 /* 1236 * PM runtime is only enabled when a Slave reports as Attached 1237 */ 1238 if (!rt711->first_hw_init) { 1239 /* set autosuspend parameters */ 1240 pm_runtime_set_autosuspend_delay(&slave->dev, 3000); 1241 pm_runtime_use_autosuspend(&slave->dev); 1242 1243 /* update count of parent 'active' children */ 1244 pm_runtime_set_active(&slave->dev); 1245 1246 /* make sure the device does not suspend immediately */ 1247 pm_runtime_mark_last_busy(&slave->dev); 1248 1249 pm_runtime_enable(&slave->dev); 1250 } 1251 1252 pm_runtime_get_noresume(&slave->dev); 1253 1254 rt711_reset(rt711->regmap); 1255 1256 /* power on */ 1257 regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0); 1258 1259 /* Set Pin Widget */ 1260 regmap_write(rt711->regmap, RT711_SET_PIN_MIC2, 0x25); 1261 regmap_write(rt711->regmap, RT711_SET_PIN_HP, 0xc0); 1262 regmap_write(rt711->regmap, RT711_SET_PIN_DMIC1, 0x20); 1263 regmap_write(rt711->regmap, RT711_SET_PIN_DMIC2, 0x20); 1264 regmap_write(rt711->regmap, RT711_SET_PIN_LINE1, 0x20); 1265 regmap_write(rt711->regmap, RT711_SET_PIN_LINE2, 0x20); 1266 1267 /* Mute HP/ADC1/ADC2 */ 1268 regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0xa080); 1269 regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0x9080); 1270 regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x6080); 1271 regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x5080); 1272 regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x6080); 1273 regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x5080); 1274 1275 /* Set Configuration Default */ 1276 regmap_write(rt711->regmap, 0x4f12, 0x91); 1277 regmap_write(rt711->regmap, 0x4e12, 0xd6); 1278 regmap_write(rt711->regmap, 0x4d12, 0x11); 1279 regmap_write(rt711->regmap, 0x4c12, 0x20); 1280 regmap_write(rt711->regmap, 0x4f13, 0x91); 1281 regmap_write(rt711->regmap, 0x4e13, 0xd6); 1282 regmap_write(rt711->regmap, 0x4d13, 0x11); 1283 regmap_write(rt711->regmap, 0x4c13, 0x21); 1284 regmap_write(rt711->regmap, 0x4c21, 0xf0); 1285 regmap_write(rt711->regmap, 0x4d21, 0x11); 1286 regmap_write(rt711->regmap, 0x4e21, 0x11); 1287 regmap_write(rt711->regmap, 0x4f21, 0x01); 1288 1289 /* Data port arrangement */ 1290 rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 1291 RT711_TX_RX_MUX_CTL, 0x0154); 1292 1293 /* Set index */ 1294 rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 1295 RT711_DIGITAL_MISC_CTRL4, 0x201b); 1296 rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 1297 RT711_COMBO_JACK_AUTO_CTL1, 0x5089); 1298 rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 1299 RT711_VREFOUT_CTL, 0x5064); 1300 rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 1301 RT711_INLINE_CMD_CTL, 0xd249); 1302 1303 /* Finish Initial Settings, set power to D3 */ 1304 regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3); 1305 1306 if (rt711->first_hw_init) 1307 rt711_calibration(rt711); 1308 else { 1309 INIT_DELAYED_WORK(&rt711->jack_detect_work, 1310 rt711_jack_detect_handler); 1311 INIT_DELAYED_WORK(&rt711->jack_btn_check_work, 1312 rt711_btn_check_handler); 1313 mutex_init(&rt711->calibrate_mutex); 1314 INIT_WORK(&rt711->calibration_work, rt711_calibration_work); 1315 schedule_work(&rt711->calibration_work); 1316 } 1317 1318 /* 1319 * if set_jack callback occurred early than io_init, 1320 * we set up the jack detection function now 1321 */ 1322 if (rt711->hs_jack) 1323 rt711_jack_init(rt711); 1324 1325 if (rt711->first_hw_init) { 1326 regcache_cache_bypass(rt711->regmap, false); 1327 regcache_mark_dirty(rt711->regmap); 1328 } else 1329 rt711->first_hw_init = true; 1330 1331 /* Mark Slave initialization complete */ 1332 rt711->hw_init = true; 1333 1334 pm_runtime_mark_last_busy(&slave->dev); 1335 pm_runtime_put_autosuspend(&slave->dev); 1336 1337 dev_dbg(&slave->dev, "%s hw_init complete\n", __func__); 1338 return 0; 1339 } 1340 1341 MODULE_DESCRIPTION("ASoC RT711 SDW driver"); 1342 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>"); 1343 MODULE_LICENSE("GPL"); 1344