linux/sound/soc/codecs/rt711.c
<<
>>
Prefs
   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
  31static 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
  45static 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
  60static 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
  76static 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
  84static 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
 136static 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        }
 179read_error:
 180        return btn_type;
 181}
 182
 183static 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
 223to_error:
 224        ret = -ETIMEDOUT;
 225        pr_err_ratelimited("Time-out error in %s\n", __func__);
 226        return ret;
 227io_error:
 228        pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
 229        return ret;
 230remove_error:
 231        pr_err_ratelimited("Jack removal in %s\n", __func__);
 232        return -ENODEV;
 233}
 234
 235static 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
 294io_error:
 295        pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
 296}
 297
 298static 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, &reg);
 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
 349io_error:
 350        pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
 351}
 352
 353static 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                default:
 393                        dev_warn(rt711->component->dev, "Wrong JD source\n");
 394                        break;
 395                }
 396
 397                dev_dbg(&rt711->slave->dev, "in %s enable\n", __func__);
 398
 399                mod_delayed_work(system_power_efficient_wq,
 400                        &rt711->jack_detect_work, msecs_to_jiffies(250));
 401        } else {
 402                regmap_write(rt711->regmap,
 403                        RT711_SET_MIC2_UNSOLICITED_ENABLE, 0x00);
 404                regmap_write(rt711->regmap,
 405                        RT711_SET_HP_UNSOLICITED_ENABLE, 0x00);
 406                regmap_write(rt711->regmap,
 407                        RT711_SET_INLINE_UNSOLICITED_ENABLE, 0x00);
 408
 409                dev_dbg(&rt711->slave->dev, "in %s disable\n", __func__);
 410        }
 411
 412        /* power off */
 413        if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
 414                regmap_write(rt711->regmap,
 415                        RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
 416        mutex_unlock(&rt711->calibrate_mutex);
 417}
 418
 419static int rt711_set_jack_detect(struct snd_soc_component *component,
 420        struct snd_soc_jack *hs_jack, void *data)
 421{
 422        struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
 423
 424        rt711->hs_jack = hs_jack;
 425
 426        if (!rt711->hw_init) {
 427                dev_dbg(&rt711->slave->dev,
 428                        "%s hw_init not ready yet\n", __func__);
 429                return 0;
 430        }
 431
 432        rt711_jack_init(rt711);
 433
 434        return 0;
 435}
 436
 437static void rt711_get_gain(struct rt711_priv *rt711, unsigned int addr_h,
 438                                unsigned int addr_l, unsigned int val_h,
 439                                unsigned int *r_val, unsigned int *l_val)
 440{
 441        /* R Channel */
 442        *r_val = (val_h << 8);
 443        regmap_read(rt711->regmap, addr_l, r_val);
 444
 445        /* L Channel */
 446        val_h |= 0x20;
 447        *l_val = (val_h << 8);
 448        regmap_read(rt711->regmap, addr_h, l_val);
 449}
 450
 451/* For Verb-Set Amplifier Gain (Verb ID = 3h) */
 452static int rt711_set_amp_gain_put(struct snd_kcontrol *kcontrol,
 453                struct snd_ctl_elem_value *ucontrol)
 454{
 455        struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 456        struct snd_soc_dapm_context *dapm =
 457                snd_soc_component_get_dapm(component);
 458        struct soc_mixer_control *mc =
 459                (struct soc_mixer_control *)kcontrol->private_value;
 460        struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
 461        unsigned int addr_h, addr_l, val_h, val_ll, val_lr;
 462        unsigned int read_ll, read_rl;
 463        int i;
 464
 465        mutex_lock(&rt711->calibrate_mutex);
 466
 467        /* Can't use update bit function, so read the original value first */
 468        addr_h = mc->reg;
 469        addr_l = mc->rreg;
 470        if (mc->shift == RT711_DIR_OUT_SFT) /* output */
 471                val_h = 0x80;
 472        else /* input */
 473                val_h = 0x0;
 474
 475        rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll);
 476
 477        /* L Channel */
 478        if (mc->invert) {
 479                /* for mute/unmute */
 480                val_ll = (mc->max - ucontrol->value.integer.value[0])
 481                                        << RT711_MUTE_SFT;
 482                /* keep gain */
 483                read_ll = read_ll & 0x7f;
 484                val_ll |= read_ll;
 485        } else {
 486                /* for gain */
 487                val_ll = ((ucontrol->value.integer.value[0]) & 0x7f);
 488                if (val_ll > mc->max)
 489                        val_ll = mc->max;
 490                /* keep mute status */
 491                read_ll = read_ll & (1 << RT711_MUTE_SFT);
 492                val_ll |= read_ll;
 493        }
 494
 495        if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
 496                regmap_write(rt711->regmap,
 497                                RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
 498
 499        /* R Channel */
 500        if (mc->invert) {
 501                /* for mute/unmute */
 502                val_lr = (mc->max - ucontrol->value.integer.value[1])
 503                                        << RT711_MUTE_SFT;
 504                /* keep gain */
 505                read_rl = read_rl & 0x7f;
 506                val_lr |= read_rl;
 507        } else {
 508                /* for gain */
 509                val_lr = ((ucontrol->value.integer.value[1]) & 0x7f);
 510                if (val_lr > mc->max)
 511                        val_lr = mc->max;
 512                /* keep mute status */
 513                read_rl = read_rl & (1 << RT711_MUTE_SFT);
 514                val_lr |= read_rl;
 515        }
 516
 517        for (i = 0; i < 3; i++) { /* retry 3 times at most */
 518
 519                if (val_ll == val_lr) {
 520                        /* Set both L/R channels at the same time */
 521                        val_h = (1 << mc->shift) | (3 << 4);
 522                        regmap_write(rt711->regmap,
 523                                addr_h, (val_h << 8 | val_ll));
 524                        regmap_write(rt711->regmap,
 525                                addr_l, (val_h << 8 | val_ll));
 526                } else {
 527                        /* Lch*/
 528                        val_h = (1 << mc->shift) | (1 << 5);
 529                        regmap_write(rt711->regmap,
 530                                addr_h, (val_h << 8 | val_ll));
 531
 532                        /* Rch */
 533                        val_h = (1 << mc->shift) | (1 << 4);
 534                        regmap_write(rt711->regmap,
 535                                addr_l, (val_h << 8 | val_lr));
 536                }
 537                /* check result */
 538                if (mc->shift == RT711_DIR_OUT_SFT) /* output */
 539                        val_h = 0x80;
 540                else /* input */
 541                        val_h = 0x0;
 542
 543                rt711_get_gain(rt711, addr_h, addr_l, val_h,
 544                                        &read_rl, &read_ll);
 545                if (read_rl == val_lr && read_ll == val_ll)
 546                        break;
 547        }
 548
 549        if (dapm->bias_level <= SND_SOC_BIAS_STANDBY)
 550                regmap_write(rt711->regmap,
 551                                RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
 552
 553        mutex_unlock(&rt711->calibrate_mutex);
 554        return 0;
 555}
 556
 557static int rt711_set_amp_gain_get(struct snd_kcontrol *kcontrol,
 558                struct snd_ctl_elem_value *ucontrol)
 559{
 560        struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 561        struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
 562        struct soc_mixer_control *mc =
 563                (struct soc_mixer_control *)kcontrol->private_value;
 564        unsigned int addr_h, addr_l, val_h;
 565        unsigned int read_ll, read_rl;
 566
 567        /* switch to get command */
 568        addr_h = mc->reg;
 569        addr_l = mc->rreg;
 570        if (mc->shift == RT711_DIR_OUT_SFT) /* output */
 571                val_h = 0x80;
 572        else /* input */
 573                val_h = 0x0;
 574
 575        rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll);
 576
 577        if (mc->invert) {
 578                /* mute/unmute for switch controls */
 579                read_ll = !((read_ll & 0x80) >> RT711_MUTE_SFT);
 580                read_rl = !((read_rl & 0x80) >> RT711_MUTE_SFT);
 581        } else {
 582                /* for gain volume controls */
 583                read_ll = read_ll & 0x7f;
 584                read_rl = read_rl & 0x7f;
 585        }
 586        ucontrol->value.integer.value[0] = read_ll;
 587        ucontrol->value.integer.value[1] = read_rl;
 588
 589        return 0;
 590}
 591
 592static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0);
 593static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0);
 594static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0);
 595
 596static const struct snd_kcontrol_new rt711_snd_controls[] = {
 597        SOC_DOUBLE_R_EXT_TLV("DAC Surr Playback Volume",
 598                RT711_SET_GAIN_DAC2_H, RT711_SET_GAIN_DAC2_L,
 599                RT711_DIR_OUT_SFT, 0x57, 0,
 600                rt711_set_amp_gain_get, rt711_set_amp_gain_put, out_vol_tlv),
 601        SOC_DOUBLE_R_EXT("ADC 08 Capture Switch",
 602                RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L,
 603                RT711_DIR_IN_SFT, 1, 1,
 604                rt711_set_amp_gain_get, rt711_set_amp_gain_put),
 605        SOC_DOUBLE_R_EXT("ADC 09 Capture Switch",
 606                RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L,
 607                RT711_DIR_IN_SFT, 1, 1,
 608                rt711_set_amp_gain_get, rt711_set_amp_gain_put),
 609        SOC_DOUBLE_R_EXT_TLV("ADC 08 Capture Volume",
 610                RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L,
 611                RT711_DIR_IN_SFT, 0x3f, 0,
 612                rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv),
 613        SOC_DOUBLE_R_EXT_TLV("ADC 09 Capture Volume",
 614                RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L,
 615                RT711_DIR_IN_SFT, 0x3f, 0,
 616                rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv),
 617        SOC_DOUBLE_R_EXT_TLV("AMIC Volume",
 618                RT711_SET_GAIN_AMIC_H, RT711_SET_GAIN_AMIC_L,
 619                RT711_DIR_IN_SFT, 3, 0,
 620                rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv),
 621        SOC_DOUBLE_R_EXT_TLV("DMIC1 Volume",
 622                RT711_SET_GAIN_DMIC1_H, RT711_SET_GAIN_DMIC1_L,
 623                RT711_DIR_IN_SFT, 3, 0,
 624                rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv),
 625        SOC_DOUBLE_R_EXT_TLV("DMIC2 Volume",
 626                RT711_SET_GAIN_DMIC2_H, RT711_SET_GAIN_DMIC2_L,
 627                RT711_DIR_IN_SFT, 3, 0,
 628                rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv),
 629};
 630
 631static int rt711_mux_get(struct snd_kcontrol *kcontrol,
 632                        struct snd_ctl_elem_value *ucontrol)
 633{
 634        struct snd_soc_component *component =
 635                snd_soc_dapm_kcontrol_component(kcontrol);
 636        struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
 637        unsigned int reg, val = 0, nid;
 638        int ret;
 639
 640        if (strstr(ucontrol->id.name, "ADC 22 Mux"))
 641                nid = RT711_MIXER_IN1;
 642        else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
 643                nid = RT711_MIXER_IN2;
 644        else
 645                return -EINVAL;
 646
 647        /* vid = 0xf01 */
 648        reg = RT711_VERB_SET_CONNECT_SEL | nid;
 649        ret = regmap_read(rt711->regmap, reg, &val);
 650        if (ret < 0) {
 651                dev_err(component->dev, "%s: sdw read failed: %d\n",
 652                        __func__, ret);
 653                return ret;
 654        }
 655
 656        ucontrol->value.enumerated.item[0] = val;
 657
 658        return 0;
 659}
 660
 661static int rt711_mux_put(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 snd_soc_dapm_context *dapm =
 667                snd_soc_dapm_kcontrol_dapm(kcontrol);
 668        struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
 669        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
 670        unsigned int *item = ucontrol->value.enumerated.item;
 671        unsigned int val, val2 = 0, change, reg, nid;
 672        int ret;
 673
 674        if (item[0] >= e->items)
 675                return -EINVAL;
 676
 677        if (strstr(ucontrol->id.name, "ADC 22 Mux"))
 678                nid = RT711_MIXER_IN1;
 679        else if (strstr(ucontrol->id.name, "ADC 23 Mux"))
 680                nid = RT711_MIXER_IN2;
 681        else
 682                return -EINVAL;
 683
 684        /* Verb ID = 0x701h */
 685        val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
 686
 687        reg = RT711_VERB_SET_CONNECT_SEL | nid;
 688        ret = regmap_read(rt711->regmap, reg, &val2);
 689        if (ret < 0) {
 690                dev_err(component->dev, "%s: sdw read failed: %d\n",
 691                        __func__, ret);
 692                return ret;
 693        }
 694
 695        if (val == val2)
 696                change = 0;
 697        else
 698                change = 1;
 699
 700        if (change) {
 701                reg = RT711_VERB_SET_CONNECT_SEL | nid;
 702                regmap_write(rt711->regmap, reg, val);
 703        }
 704
 705        snd_soc_dapm_mux_update_power(dapm, kcontrol,
 706                                                item[0], e, NULL);
 707
 708        return change;
 709}
 710
 711static const char * const adc_mux_text[] = {
 712        "MIC2",
 713        "LINE1",
 714        "LINE2",
 715        "DMIC",
 716};
 717
 718static SOC_ENUM_SINGLE_DECL(
 719        rt711_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text);
 720
 721static SOC_ENUM_SINGLE_DECL(
 722        rt711_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text);
 723
 724static const struct snd_kcontrol_new rt711_adc22_mux =
 725        SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt711_adc22_enum,
 726                        rt711_mux_get, rt711_mux_put);
 727
 728static const struct snd_kcontrol_new rt711_adc23_mux =
 729        SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt711_adc23_enum,
 730                        rt711_mux_get, rt711_mux_put);
 731
 732static int rt711_dac_surround_event(struct snd_soc_dapm_widget *w,
 733        struct snd_kcontrol *kcontrol, int event)
 734{
 735        struct snd_soc_component *component =
 736                snd_soc_dapm_to_component(w->dapm);
 737        struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
 738        unsigned int val_h = (1 << RT711_DIR_OUT_SFT) | (0x3 << 4);
 739        unsigned int val_l;
 740
 741        switch (event) {
 742        case SND_SOC_DAPM_POST_PMU:
 743                regmap_write(rt711->regmap,
 744                        RT711_SET_STREAMID_DAC2, 0x10);
 745
 746                val_l = 0x00;
 747                regmap_write(rt711->regmap,
 748                        RT711_SET_GAIN_HP_H, (val_h << 8 | val_l));
 749                break;
 750        case SND_SOC_DAPM_PRE_PMD:
 751                val_l = (1 << RT711_MUTE_SFT);
 752                regmap_write(rt711->regmap,
 753                        RT711_SET_GAIN_HP_H, (val_h << 8 | val_l));
 754                usleep_range(50000, 55000);
 755
 756                regmap_write(rt711->regmap,
 757                        RT711_SET_STREAMID_DAC2, 0x00);
 758                break;
 759        }
 760        return 0;
 761}
 762
 763static int rt711_adc_09_event(struct snd_soc_dapm_widget *w,
 764        struct snd_kcontrol *kcontrol, int event)
 765{
 766        struct snd_soc_component *component =
 767                snd_soc_dapm_to_component(w->dapm);
 768        struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
 769
 770        switch (event) {
 771        case SND_SOC_DAPM_POST_PMU:
 772                regmap_write(rt711->regmap,
 773                        RT711_SET_STREAMID_ADC1, 0x10);
 774                break;
 775        case SND_SOC_DAPM_PRE_PMD:
 776                regmap_write(rt711->regmap,
 777                        RT711_SET_STREAMID_ADC1, 0x00);
 778                break;
 779        }
 780        return 0;
 781}
 782
 783static int rt711_adc_08_event(struct snd_soc_dapm_widget *w,
 784        struct snd_kcontrol *kcontrol, int event)
 785{
 786        struct snd_soc_component *component =
 787                snd_soc_dapm_to_component(w->dapm);
 788        struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
 789
 790        switch (event) {
 791        case SND_SOC_DAPM_POST_PMU:
 792                regmap_write(rt711->regmap,
 793                        RT711_SET_STREAMID_ADC2, 0x10);
 794                break;
 795        case SND_SOC_DAPM_PRE_PMD:
 796                regmap_write(rt711->regmap,
 797                        RT711_SET_STREAMID_ADC2, 0x00);
 798                break;
 799        }
 800        return 0;
 801}
 802
 803static const struct snd_soc_dapm_widget rt711_dapm_widgets[] = {
 804        SND_SOC_DAPM_OUTPUT("HP"),
 805        SND_SOC_DAPM_INPUT("MIC2"),
 806        SND_SOC_DAPM_INPUT("DMIC1"),
 807        SND_SOC_DAPM_INPUT("DMIC2"),
 808        SND_SOC_DAPM_INPUT("LINE1"),
 809        SND_SOC_DAPM_INPUT("LINE2"),
 810
 811        SND_SOC_DAPM_DAC_E("DAC Surround", NULL, SND_SOC_NOPM, 0, 0,
 812                rt711_dac_surround_event,
 813                SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
 814        SND_SOC_DAPM_ADC_E("ADC 09", NULL, SND_SOC_NOPM, 0, 0,
 815                rt711_adc_09_event,
 816                SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
 817        SND_SOC_DAPM_ADC_E("ADC 08", NULL, SND_SOC_NOPM, 0, 0,
 818                rt711_adc_08_event,
 819                SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
 820        SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0,
 821                &rt711_adc22_mux),
 822        SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0,
 823                &rt711_adc23_mux),
 824
 825        SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0),
 826        SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0),
 827        SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0),
 828};
 829
 830static const struct snd_soc_dapm_route rt711_audio_map[] = {
 831        {"DAC Surround", NULL, "DP3RX"},
 832        {"DP2TX", NULL, "ADC 09"},
 833        {"DP4TX", NULL, "ADC 08"},
 834
 835        {"ADC 09", NULL, "ADC 22 Mux"},
 836        {"ADC 08", NULL, "ADC 23 Mux"},
 837        {"ADC 22 Mux", "DMIC", "DMIC1"},
 838        {"ADC 22 Mux", "LINE1", "LINE1"},
 839        {"ADC 22 Mux", "LINE2", "LINE2"},
 840        {"ADC 22 Mux", "MIC2", "MIC2"},
 841        {"ADC 23 Mux", "DMIC", "DMIC2"},
 842        {"ADC 23 Mux", "LINE1", "LINE1"},
 843        {"ADC 23 Mux", "LINE2", "LINE2"},
 844        {"ADC 23 Mux", "MIC2", "MIC2"},
 845
 846        {"HP", NULL, "DAC Surround"},
 847};
 848
 849static int rt711_set_bias_level(struct snd_soc_component *component,
 850                                enum snd_soc_bias_level level)
 851{
 852        struct snd_soc_dapm_context *dapm =
 853                snd_soc_component_get_dapm(component);
 854        struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
 855
 856        switch (level) {
 857        case SND_SOC_BIAS_PREPARE:
 858                if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
 859                        regmap_write(rt711->regmap,
 860                                RT711_SET_AUDIO_POWER_STATE,
 861                                AC_PWRST_D0);
 862                }
 863                break;
 864
 865        case SND_SOC_BIAS_STANDBY:
 866                mutex_lock(&rt711->calibrate_mutex);
 867                regmap_write(rt711->regmap,
 868                        RT711_SET_AUDIO_POWER_STATE,
 869                        AC_PWRST_D3);
 870                mutex_unlock(&rt711->calibrate_mutex);
 871                break;
 872
 873        default:
 874                break;
 875        }
 876
 877        return 0;
 878}
 879
 880static int rt711_parse_dt(struct rt711_priv *rt711, struct device *dev)
 881{
 882        device_property_read_u32(dev, "realtek,jd-src",
 883                &rt711->jd_src);
 884
 885        return 0;
 886}
 887
 888static int rt711_probe(struct snd_soc_component *component)
 889{
 890        struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
 891
 892        rt711_parse_dt(rt711, &rt711->slave->dev);
 893        rt711->component = component;
 894
 895        return 0;
 896}
 897
 898static void rt711_remove(struct snd_soc_component *component)
 899{
 900        struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
 901
 902        regcache_cache_only(rt711->regmap, true);
 903}
 904
 905static const struct snd_soc_component_driver soc_codec_dev_rt711 = {
 906        .probe = rt711_probe,
 907        .set_bias_level = rt711_set_bias_level,
 908        .controls = rt711_snd_controls,
 909        .num_controls = ARRAY_SIZE(rt711_snd_controls),
 910        .dapm_widgets = rt711_dapm_widgets,
 911        .num_dapm_widgets = ARRAY_SIZE(rt711_dapm_widgets),
 912        .dapm_routes = rt711_audio_map,
 913        .num_dapm_routes = ARRAY_SIZE(rt711_audio_map),
 914        .set_jack = rt711_set_jack_detect,
 915        .remove = rt711_remove,
 916};
 917
 918static int rt711_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
 919                                int direction)
 920{
 921        struct sdw_stream_data *stream;
 922
 923        if (!sdw_stream)
 924                return 0;
 925
 926        stream = kzalloc(sizeof(*stream), GFP_KERNEL);
 927        if (!stream)
 928                return -ENOMEM;
 929
 930        stream->sdw_stream = sdw_stream;
 931
 932        /* Use tx_mask or rx_mask to configure stream tag and set dma_data */
 933        if (direction == SNDRV_PCM_STREAM_PLAYBACK)
 934                dai->playback_dma_data = stream;
 935        else
 936                dai->capture_dma_data = stream;
 937
 938        return 0;
 939}
 940
 941static void rt711_shutdown(struct snd_pcm_substream *substream,
 942                                struct snd_soc_dai *dai)
 943{
 944        struct sdw_stream_data *stream;
 945
 946        stream = snd_soc_dai_get_dma_data(dai, substream);
 947        snd_soc_dai_set_dma_data(dai, substream, NULL);
 948        kfree(stream);
 949}
 950
 951static int rt711_pcm_hw_params(struct snd_pcm_substream *substream,
 952                                struct snd_pcm_hw_params *params,
 953                                struct snd_soc_dai *dai)
 954{
 955        struct snd_soc_component *component = dai->component;
 956        struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
 957        struct sdw_stream_config stream_config;
 958        struct sdw_port_config port_config;
 959        enum sdw_data_direction direction;
 960        struct sdw_stream_data *stream;
 961        int retval, port, num_channels;
 962        unsigned int val = 0;
 963
 964        dev_dbg(dai->dev, "%s %s", __func__, dai->name);
 965        stream = snd_soc_dai_get_dma_data(dai, substream);
 966
 967        if (!stream)
 968                return -EINVAL;
 969
 970        if (!rt711->slave)
 971                return -EINVAL;
 972
 973        /* SoundWire specific configuration */
 974        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 975                direction = SDW_DATA_DIR_RX;
 976                port = 3;
 977        } else {
 978                direction = SDW_DATA_DIR_TX;
 979                if (dai->id == RT711_AIF1)
 980                        port = 4;
 981                else if (dai->id == RT711_AIF2)
 982                        port = 2;
 983                else
 984                        return -EINVAL;
 985        }
 986
 987        stream_config.frame_rate = params_rate(params);
 988        stream_config.ch_count = params_channels(params);
 989        stream_config.bps = snd_pcm_format_width(params_format(params));
 990        stream_config.direction = direction;
 991
 992        num_channels = params_channels(params);
 993        port_config.ch_mask = (1 << (num_channels)) - 1;
 994        port_config.num = port;
 995
 996        retval = sdw_stream_add_slave(rt711->slave, &stream_config,
 997                                        &port_config, 1, stream->sdw_stream);
 998        if (retval) {
 999                dev_err(dai->dev, "Unable to configure port\n");
1000                return retval;
1001        }
1002
1003        if (params_channels(params) <= 16) {
1004                /* bit 3:0 Number of Channel */
1005                val |= (params_channels(params) - 1);
1006        } else {
1007                dev_err(component->dev, "Unsupported channels %d\n",
1008                        params_channels(params));
1009                return -EINVAL;
1010        }
1011
1012        switch (params_width(params)) {
1013        /* bit 6:4 Bits per Sample */
1014        case 8:
1015                break;
1016        case 16:
1017                val |= (0x1 << 4);
1018                break;
1019        case 20:
1020                val |= (0x2 << 4);
1021                break;
1022        case 24:
1023                val |= (0x3 << 4);
1024                break;
1025        case 32:
1026                val |= (0x4 << 4);
1027                break;
1028        default:
1029                return -EINVAL;
1030        }
1031
1032        /* 48Khz */
1033        regmap_write(rt711->regmap, RT711_DAC_FORMAT_H, val);
1034        regmap_write(rt711->regmap, RT711_ADC1_FORMAT_H, val);
1035        regmap_write(rt711->regmap, RT711_ADC2_FORMAT_H, val);
1036
1037        return retval;
1038}
1039
1040static int rt711_pcm_hw_free(struct snd_pcm_substream *substream,
1041                                struct snd_soc_dai *dai)
1042{
1043        struct snd_soc_component *component = dai->component;
1044        struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component);
1045        struct sdw_stream_data *stream =
1046                snd_soc_dai_get_dma_data(dai, substream);
1047
1048        if (!rt711->slave)
1049                return -EINVAL;
1050
1051        sdw_stream_remove_slave(rt711->slave, stream->sdw_stream);
1052        return 0;
1053}
1054
1055#define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
1056#define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1057                        SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
1058
1059static const struct snd_soc_dai_ops rt711_ops = {
1060        .hw_params      = rt711_pcm_hw_params,
1061        .hw_free        = rt711_pcm_hw_free,
1062        .set_sdw_stream = rt711_set_sdw_stream,
1063        .shutdown       = rt711_shutdown,
1064};
1065
1066static struct snd_soc_dai_driver rt711_dai[] = {
1067        {
1068                .name = "rt711-aif1",
1069                .id = RT711_AIF1,
1070                .playback = {
1071                        .stream_name = "DP3 Playback",
1072                        .channels_min = 1,
1073                        .channels_max = 2,
1074                        .rates = RT711_STEREO_RATES,
1075                        .formats = RT711_FORMATS,
1076                },
1077                .capture = {
1078                        .stream_name = "DP4 Capture",
1079                        .channels_min = 1,
1080                        .channels_max = 2,
1081                        .rates = RT711_STEREO_RATES,
1082                        .formats = RT711_FORMATS,
1083                },
1084                .ops = &rt711_ops,
1085        },
1086        {
1087                .name = "rt711-aif2",
1088                .id = RT711_AIF2,
1089                .capture = {
1090                        .stream_name = "DP2 Capture",
1091                        .channels_min = 1,
1092                        .channels_max = 2,
1093                        .rates = RT711_STEREO_RATES,
1094                        .formats = RT711_FORMATS,
1095                },
1096                .ops = &rt711_ops,
1097        }
1098};
1099
1100/* Bus clock frequency */
1101#define RT711_CLK_FREQ_9600000HZ 9600000
1102#define RT711_CLK_FREQ_12000000HZ 12000000
1103#define RT711_CLK_FREQ_6000000HZ 6000000
1104#define RT711_CLK_FREQ_4800000HZ 4800000
1105#define RT711_CLK_FREQ_2400000HZ 2400000
1106#define RT711_CLK_FREQ_12288000HZ 12288000
1107
1108int rt711_clock_config(struct device *dev)
1109{
1110        struct rt711_priv *rt711 = dev_get_drvdata(dev);
1111        unsigned int clk_freq, value;
1112
1113        clk_freq = (rt711->params.curr_dr_freq >> 1);
1114
1115        switch (clk_freq) {
1116        case RT711_CLK_FREQ_12000000HZ:
1117                value = 0x0;
1118                break;
1119        case RT711_CLK_FREQ_6000000HZ:
1120                value = 0x1;
1121                break;
1122        case RT711_CLK_FREQ_9600000HZ:
1123                value = 0x2;
1124                break;
1125        case RT711_CLK_FREQ_4800000HZ:
1126                value = 0x3;
1127                break;
1128        case RT711_CLK_FREQ_2400000HZ:
1129                value = 0x4;
1130                break;
1131        case RT711_CLK_FREQ_12288000HZ:
1132                value = 0x5;
1133                break;
1134        default:
1135                return -EINVAL;
1136        }
1137
1138        regmap_write(rt711->regmap, 0xe0, value);
1139        regmap_write(rt711->regmap, 0xf0, value);
1140
1141        dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
1142
1143        return 0;
1144}
1145
1146static void rt711_calibration_work(struct work_struct *work)
1147{
1148        struct rt711_priv *rt711 =
1149                container_of(work, struct rt711_priv, calibration_work);
1150
1151        rt711_calibration(rt711);
1152}
1153
1154int rt711_init(struct device *dev, struct regmap *sdw_regmap,
1155                        struct regmap *regmap, struct sdw_slave *slave)
1156{
1157        struct rt711_priv *rt711;
1158        int ret;
1159
1160        rt711 = devm_kzalloc(dev, sizeof(*rt711), GFP_KERNEL);
1161        if (!rt711)
1162                return -ENOMEM;
1163
1164        dev_set_drvdata(dev, rt711);
1165        rt711->slave = slave;
1166        rt711->sdw_regmap = sdw_regmap;
1167        rt711->regmap = regmap;
1168
1169        /*
1170         * Mark hw_init to false
1171         * HW init will be performed when device reports present
1172         */
1173        rt711->hw_init = false;
1174        rt711->first_hw_init = false;
1175
1176        /* JD source uses JD2 in default */
1177        rt711->jd_src = RT711_JD2;
1178
1179        ret =  devm_snd_soc_register_component(dev,
1180                                &soc_codec_dev_rt711,
1181                                rt711_dai,
1182                                ARRAY_SIZE(rt711_dai));
1183
1184        dev_dbg(&slave->dev, "%s\n", __func__);
1185
1186        return ret;
1187}
1188
1189int rt711_io_init(struct device *dev, struct sdw_slave *slave)
1190{
1191        struct rt711_priv *rt711 = dev_get_drvdata(dev);
1192
1193        if (rt711->hw_init)
1194                return 0;
1195
1196        if (rt711->first_hw_init) {
1197                regcache_cache_only(rt711->regmap, false);
1198                regcache_cache_bypass(rt711->regmap, true);
1199        }
1200
1201        /*
1202         * PM runtime is only enabled when a Slave reports as Attached
1203         */
1204        if (!rt711->first_hw_init) {
1205                /* set autosuspend parameters */
1206                pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
1207                pm_runtime_use_autosuspend(&slave->dev);
1208
1209                /* update count of parent 'active' children */
1210                pm_runtime_set_active(&slave->dev);
1211
1212                /* make sure the device does not suspend immediately */
1213                pm_runtime_mark_last_busy(&slave->dev);
1214
1215                pm_runtime_enable(&slave->dev);
1216        }
1217
1218        pm_runtime_get_noresume(&slave->dev);
1219
1220        rt711_reset(rt711->regmap);
1221
1222        /* power on */
1223        regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0);
1224
1225        /* Set Pin Widget */
1226        regmap_write(rt711->regmap, RT711_SET_PIN_MIC2, 0x25);
1227        regmap_write(rt711->regmap, RT711_SET_PIN_HP, 0xc0);
1228        regmap_write(rt711->regmap, RT711_SET_PIN_DMIC1, 0x20);
1229        regmap_write(rt711->regmap, RT711_SET_PIN_DMIC2, 0x20);
1230        regmap_write(rt711->regmap, RT711_SET_PIN_LINE1, 0x20);
1231        regmap_write(rt711->regmap, RT711_SET_PIN_LINE2, 0x20);
1232
1233        /* Mute HP/ADC1/ADC2 */
1234        regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0xa080);
1235        regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0x9080);
1236        regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x6080);
1237        regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x5080);
1238        regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x6080);
1239        regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x5080);
1240
1241        /* Set Configuration Default */
1242        regmap_write(rt711->regmap, 0x4f12, 0x91);
1243        regmap_write(rt711->regmap, 0x4e12, 0xd6);
1244        regmap_write(rt711->regmap, 0x4d12, 0x11);
1245        regmap_write(rt711->regmap, 0x4c12, 0x20);
1246        regmap_write(rt711->regmap, 0x4f13, 0x91);
1247        regmap_write(rt711->regmap, 0x4e13, 0xd6);
1248        regmap_write(rt711->regmap, 0x4d13, 0x11);
1249        regmap_write(rt711->regmap, 0x4c13, 0x21);
1250        regmap_write(rt711->regmap, 0x4c21, 0xf0);
1251        regmap_write(rt711->regmap, 0x4d21, 0x11);
1252        regmap_write(rt711->regmap, 0x4e21, 0x11);
1253        regmap_write(rt711->regmap, 0x4f21, 0x01);
1254
1255        /* Data port arrangement */
1256        rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1257                RT711_TX_RX_MUX_CTL, 0x0154);
1258
1259        /* Set index */
1260        rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1261                RT711_DIGITAL_MISC_CTRL4, 0x201b);
1262        rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1263                RT711_COMBO_JACK_AUTO_CTL1, 0x5089);
1264        rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1265                RT711_VREFOUT_CTL, 0x5064);
1266        rt711_index_write(rt711->regmap, RT711_VENDOR_REG,
1267                RT711_INLINE_CMD_CTL, 0xd249);
1268
1269        /* Finish Initial Settings, set power to D3 */
1270        regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3);
1271
1272        if (rt711->first_hw_init)
1273                rt711_calibration(rt711);
1274        else {
1275                INIT_DELAYED_WORK(&rt711->jack_detect_work,
1276                        rt711_jack_detect_handler);
1277                INIT_DELAYED_WORK(&rt711->jack_btn_check_work,
1278                        rt711_btn_check_handler);
1279                mutex_init(&rt711->calibrate_mutex);
1280                INIT_WORK(&rt711->calibration_work, rt711_calibration_work);
1281                schedule_work(&rt711->calibration_work);
1282        }
1283
1284        /*
1285         * if set_jack callback occurred early than io_init,
1286         * we set up the jack detection function now
1287         */
1288        if (rt711->hs_jack)
1289                rt711_jack_init(rt711);
1290
1291        if (rt711->first_hw_init) {
1292                regcache_cache_bypass(rt711->regmap, false);
1293                regcache_mark_dirty(rt711->regmap);
1294        } else
1295                rt711->first_hw_init = true;
1296
1297        /* Mark Slave initialization complete */
1298        rt711->hw_init = true;
1299
1300        pm_runtime_mark_last_busy(&slave->dev);
1301        pm_runtime_put_autosuspend(&slave->dev);
1302
1303        dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
1304        return 0;
1305}
1306
1307MODULE_DESCRIPTION("ASoC RT711 SDW driver");
1308MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
1309MODULE_LICENSE("GPL");
1310