1320b8b0dSShuming Fan // SPDX-License-Identifier: GPL-2.0 2320b8b0dSShuming Fan // 3320b8b0dSShuming Fan // rt711.c -- rt711 ALSA SoC audio driver 4320b8b0dSShuming Fan // 5320b8b0dSShuming Fan // Copyright(c) 2019 Realtek Semiconductor Corp. 6320b8b0dSShuming Fan // 7320b8b0dSShuming Fan // 8320b8b0dSShuming Fan 9320b8b0dSShuming Fan #include <linux/module.h> 10320b8b0dSShuming Fan #include <linux/moduleparam.h> 11320b8b0dSShuming Fan #include <linux/kernel.h> 12320b8b0dSShuming Fan #include <linux/init.h> 13320b8b0dSShuming Fan #include <linux/delay.h> 14320b8b0dSShuming Fan #include <linux/pm_runtime.h> 15320b8b0dSShuming Fan #include <linux/pm.h> 16320b8b0dSShuming Fan #include <linux/soundwire/sdw.h> 17320b8b0dSShuming Fan #include <linux/regmap.h> 18320b8b0dSShuming Fan #include <linux/slab.h> 19320b8b0dSShuming Fan #include <sound/core.h> 20320b8b0dSShuming Fan #include <sound/pcm.h> 21320b8b0dSShuming Fan #include <sound/pcm_params.h> 22320b8b0dSShuming Fan #include <sound/soc.h> 23320b8b0dSShuming Fan #include <sound/soc-dapm.h> 24320b8b0dSShuming Fan #include <sound/initval.h> 25320b8b0dSShuming Fan #include <sound/tlv.h> 26320b8b0dSShuming Fan #include <sound/hda_verbs.h> 27320b8b0dSShuming Fan #include <sound/jack.h> 28320b8b0dSShuming Fan 29320b8b0dSShuming Fan #include "rt711.h" 30320b8b0dSShuming Fan 31320b8b0dSShuming Fan static int rt711_index_write(struct regmap *regmap, 32320b8b0dSShuming Fan unsigned int nid, unsigned int reg, unsigned int value) 33320b8b0dSShuming Fan { 34320b8b0dSShuming Fan int ret; 35320b8b0dSShuming Fan unsigned int addr = ((RT711_PRIV_INDEX_W_H | nid) << 8) | reg; 36320b8b0dSShuming Fan 37320b8b0dSShuming Fan ret = regmap_write(regmap, addr, value); 38320b8b0dSShuming Fan if (ret < 0) 39320b8b0dSShuming Fan pr_err("Failed to set private value: %06x <= %04x ret=%d\n", 40320b8b0dSShuming Fan addr, value, ret); 41320b8b0dSShuming Fan 42320b8b0dSShuming Fan return ret; 43320b8b0dSShuming Fan } 44320b8b0dSShuming Fan 45320b8b0dSShuming Fan static int rt711_index_read(struct regmap *regmap, 46320b8b0dSShuming Fan unsigned int nid, unsigned int reg, unsigned int *value) 47320b8b0dSShuming Fan { 48320b8b0dSShuming Fan int ret; 49320b8b0dSShuming Fan unsigned int addr = ((RT711_PRIV_INDEX_W_H | nid) << 8) | reg; 50320b8b0dSShuming Fan 51320b8b0dSShuming Fan *value = 0; 52320b8b0dSShuming Fan ret = regmap_read(regmap, addr, value); 53320b8b0dSShuming Fan if (ret < 0) 54320b8b0dSShuming Fan pr_err("Failed to get private value: %06x => %04x ret=%d\n", 55320b8b0dSShuming Fan addr, *value, ret); 56320b8b0dSShuming Fan 57320b8b0dSShuming Fan return ret; 58320b8b0dSShuming Fan } 59320b8b0dSShuming Fan 60320b8b0dSShuming Fan static int rt711_index_update_bits(struct regmap *regmap, unsigned int nid, 61320b8b0dSShuming Fan unsigned int reg, unsigned int mask, unsigned int val) 62320b8b0dSShuming Fan { 63320b8b0dSShuming Fan unsigned int tmp, orig; 64320b8b0dSShuming Fan int ret; 65320b8b0dSShuming Fan 66320b8b0dSShuming Fan ret = rt711_index_read(regmap, nid, reg, &orig); 67320b8b0dSShuming Fan if (ret < 0) 68320b8b0dSShuming Fan return ret; 69320b8b0dSShuming Fan 70320b8b0dSShuming Fan tmp = orig & ~mask; 71320b8b0dSShuming Fan tmp |= val & mask; 72320b8b0dSShuming Fan 73320b8b0dSShuming Fan return rt711_index_write(regmap, nid, reg, tmp); 74320b8b0dSShuming Fan } 75320b8b0dSShuming Fan 76320b8b0dSShuming Fan static void rt711_reset(struct regmap *regmap) 77320b8b0dSShuming Fan { 78320b8b0dSShuming Fan regmap_write(regmap, RT711_FUNC_RESET, 0); 79320b8b0dSShuming Fan rt711_index_update_bits(regmap, RT711_VENDOR_REG, 80320b8b0dSShuming Fan RT711_PARA_VERB_CTL, RT711_HIDDEN_REG_SW_RESET, 81320b8b0dSShuming Fan RT711_HIDDEN_REG_SW_RESET); 82320b8b0dSShuming Fan } 83320b8b0dSShuming Fan 84320b8b0dSShuming Fan static int rt711_calibration(struct rt711_priv *rt711) 85320b8b0dSShuming Fan { 86320b8b0dSShuming Fan unsigned int val, loop = 0; 87320b8b0dSShuming Fan struct device *dev; 88320b8b0dSShuming Fan struct regmap *regmap = rt711->regmap; 89320b8b0dSShuming Fan int ret = 0; 90320b8b0dSShuming Fan 91320b8b0dSShuming Fan mutex_lock(&rt711->calibrate_mutex); 92320b8b0dSShuming Fan regmap_write(rt711->regmap, 93320b8b0dSShuming Fan RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0); 94320b8b0dSShuming Fan 95320b8b0dSShuming Fan dev = regmap_get_device(regmap); 96320b8b0dSShuming Fan 97320b8b0dSShuming Fan /* Calibration manual mode */ 98320b8b0dSShuming Fan rt711_index_update_bits(regmap, RT711_VENDOR_REG, RT711_FSM_CTL, 99320b8b0dSShuming Fan 0xf, 0x0); 100320b8b0dSShuming Fan 101320b8b0dSShuming Fan /* trigger */ 102320b8b0dSShuming Fan rt711_index_update_bits(regmap, RT711_VENDOR_CALI, 103320b8b0dSShuming Fan RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_CALI_TRIGGER, 104320b8b0dSShuming Fan RT711_DAC_DC_CALI_TRIGGER); 105320b8b0dSShuming Fan 106320b8b0dSShuming Fan /* wait for calibration process */ 107320b8b0dSShuming Fan rt711_index_read(regmap, RT711_VENDOR_CALI, 108320b8b0dSShuming Fan RT711_DAC_DC_CALI_CTL1, &val); 109320b8b0dSShuming Fan 110320b8b0dSShuming Fan while (val & RT711_DAC_DC_CALI_TRIGGER) { 111320b8b0dSShuming Fan if (loop >= 500) { 112320b8b0dSShuming Fan pr_err("%s, calibration time-out!\n", 113320b8b0dSShuming Fan __func__); 114320b8b0dSShuming Fan ret = -ETIMEDOUT; 115320b8b0dSShuming Fan break; 116320b8b0dSShuming Fan } 117320b8b0dSShuming Fan loop++; 118320b8b0dSShuming Fan 119320b8b0dSShuming Fan usleep_range(10000, 11000); 120320b8b0dSShuming Fan rt711_index_read(regmap, RT711_VENDOR_CALI, 121320b8b0dSShuming Fan RT711_DAC_DC_CALI_CTL1, &val); 122320b8b0dSShuming Fan } 123320b8b0dSShuming Fan 124320b8b0dSShuming Fan /* depop mode */ 125320b8b0dSShuming Fan rt711_index_update_bits(regmap, RT711_VENDOR_REG, 126320b8b0dSShuming Fan RT711_FSM_CTL, 0xf, RT711_DEPOP_CTL); 127320b8b0dSShuming Fan 128320b8b0dSShuming Fan regmap_write(rt711->regmap, 129320b8b0dSShuming Fan RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3); 130320b8b0dSShuming Fan mutex_unlock(&rt711->calibrate_mutex); 131320b8b0dSShuming Fan 132320b8b0dSShuming Fan dev_dbg(dev, "%s calibration complete, ret=%d\n", __func__, ret); 133320b8b0dSShuming Fan return ret; 134320b8b0dSShuming Fan } 135320b8b0dSShuming Fan 136320b8b0dSShuming Fan static unsigned int rt711_button_detect(struct rt711_priv *rt711) 137320b8b0dSShuming Fan { 138320b8b0dSShuming Fan unsigned int btn_type = 0, val80, val81; 139320b8b0dSShuming Fan int ret; 140320b8b0dSShuming Fan 141320b8b0dSShuming Fan ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, 142320b8b0dSShuming Fan RT711_IRQ_FLAG_TABLE1, &val80); 143320b8b0dSShuming Fan if (ret < 0) 144320b8b0dSShuming Fan goto read_error; 145320b8b0dSShuming Fan ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, 146320b8b0dSShuming Fan RT711_IRQ_FLAG_TABLE2, &val81); 147320b8b0dSShuming Fan if (ret < 0) 148320b8b0dSShuming Fan goto read_error; 149320b8b0dSShuming Fan 150320b8b0dSShuming Fan val80 &= 0x0381; 151320b8b0dSShuming Fan val81 &= 0xff00; 152320b8b0dSShuming Fan 153320b8b0dSShuming Fan switch (val80) { 154320b8b0dSShuming Fan case 0x0200: 155320b8b0dSShuming Fan case 0x0100: 156320b8b0dSShuming Fan case 0x0080: 157320b8b0dSShuming Fan btn_type |= SND_JACK_BTN_0; 158320b8b0dSShuming Fan break; 159320b8b0dSShuming Fan case 0x0001: 160320b8b0dSShuming Fan btn_type |= SND_JACK_BTN_3; 161320b8b0dSShuming Fan break; 162320b8b0dSShuming Fan } 163320b8b0dSShuming Fan switch (val81) { 164320b8b0dSShuming Fan case 0x8000: 165320b8b0dSShuming Fan case 0x4000: 166320b8b0dSShuming Fan case 0x2000: 167320b8b0dSShuming Fan btn_type |= SND_JACK_BTN_1; 168320b8b0dSShuming Fan break; 169320b8b0dSShuming Fan case 0x1000: 170320b8b0dSShuming Fan case 0x0800: 171320b8b0dSShuming Fan case 0x0400: 172320b8b0dSShuming Fan btn_type |= SND_JACK_BTN_2; 173320b8b0dSShuming Fan break; 174320b8b0dSShuming Fan case 0x0200: 175320b8b0dSShuming Fan case 0x0100: 176320b8b0dSShuming Fan btn_type |= SND_JACK_BTN_3; 177320b8b0dSShuming Fan break; 178320b8b0dSShuming Fan } 179320b8b0dSShuming Fan read_error: 180320b8b0dSShuming Fan return btn_type; 181320b8b0dSShuming Fan } 182320b8b0dSShuming Fan 183320b8b0dSShuming Fan static int rt711_headset_detect(struct rt711_priv *rt711) 184320b8b0dSShuming Fan { 185320b8b0dSShuming Fan unsigned int buf, loop = 0; 186320b8b0dSShuming Fan int ret; 187320b8b0dSShuming Fan unsigned int jack_status = 0, reg; 188320b8b0dSShuming Fan 189320b8b0dSShuming Fan ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, 190320b8b0dSShuming Fan RT711_COMBO_JACK_AUTO_CTL2, &buf); 191320b8b0dSShuming Fan if (ret < 0) 192320b8b0dSShuming Fan goto io_error; 193320b8b0dSShuming Fan 194320b8b0dSShuming Fan while (loop < 500 && 195320b8b0dSShuming Fan (buf & RT711_COMBOJACK_AUTO_DET_STATUS) == 0) { 196320b8b0dSShuming Fan loop++; 197320b8b0dSShuming Fan 198320b8b0dSShuming Fan usleep_range(9000, 10000); 199320b8b0dSShuming Fan ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, 200320b8b0dSShuming Fan RT711_COMBO_JACK_AUTO_CTL2, &buf); 201320b8b0dSShuming Fan if (ret < 0) 202320b8b0dSShuming Fan goto io_error; 203320b8b0dSShuming Fan 204320b8b0dSShuming Fan reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT; 205320b8b0dSShuming Fan ret = regmap_read(rt711->regmap, reg, &jack_status); 206320b8b0dSShuming Fan if (ret < 0) 207320b8b0dSShuming Fan goto io_error; 208320b8b0dSShuming Fan if ((jack_status & (1 << 31)) == 0) 209320b8b0dSShuming Fan goto remove_error; 210320b8b0dSShuming Fan } 211320b8b0dSShuming Fan 212320b8b0dSShuming Fan if (loop >= 500) 213320b8b0dSShuming Fan goto to_error; 214320b8b0dSShuming Fan 215320b8b0dSShuming Fan if (buf & RT711_COMBOJACK_AUTO_DET_TRS) 216320b8b0dSShuming Fan rt711->jack_type = SND_JACK_HEADPHONE; 217320b8b0dSShuming Fan else if ((buf & RT711_COMBOJACK_AUTO_DET_CTIA) || 218320b8b0dSShuming Fan (buf & RT711_COMBOJACK_AUTO_DET_OMTP)) 219320b8b0dSShuming Fan rt711->jack_type = SND_JACK_HEADSET; 220320b8b0dSShuming Fan 221320b8b0dSShuming Fan return 0; 222320b8b0dSShuming Fan 223320b8b0dSShuming Fan to_error: 224320b8b0dSShuming Fan ret = -ETIMEDOUT; 225320b8b0dSShuming Fan pr_err_ratelimited("Time-out error in %s\n", __func__); 226320b8b0dSShuming Fan return ret; 227320b8b0dSShuming Fan io_error: 228320b8b0dSShuming Fan pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); 229320b8b0dSShuming Fan return ret; 230320b8b0dSShuming Fan remove_error: 231320b8b0dSShuming Fan pr_err_ratelimited("Jack removal in %s\n", __func__); 232320b8b0dSShuming Fan return -ENODEV; 233320b8b0dSShuming Fan } 234320b8b0dSShuming Fan 235320b8b0dSShuming Fan static void rt711_jack_detect_handler(struct work_struct *work) 236320b8b0dSShuming Fan { 237320b8b0dSShuming Fan struct rt711_priv *rt711 = 238320b8b0dSShuming Fan container_of(work, struct rt711_priv, jack_detect_work.work); 239320b8b0dSShuming Fan int btn_type = 0, ret; 240320b8b0dSShuming Fan unsigned int jack_status = 0, reg; 241320b8b0dSShuming Fan 242320b8b0dSShuming Fan if (!rt711->hs_jack) 243320b8b0dSShuming Fan return; 244320b8b0dSShuming Fan 24505ba4c00SPierre-Louis Bossart if (!rt711->component->card || !rt711->component->card->instantiated) 246320b8b0dSShuming Fan return; 247320b8b0dSShuming Fan 248770f3d99SPierre-Louis Bossart if (pm_runtime_status_suspended(rt711->slave->dev.parent)) { 249770f3d99SPierre-Louis Bossart dev_dbg(&rt711->slave->dev, 250770f3d99SPierre-Louis Bossart "%s: parent device is pm_runtime_status_suspended, skipping jack detection\n", 251770f3d99SPierre-Louis Bossart __func__); 252770f3d99SPierre-Louis Bossart return; 253770f3d99SPierre-Louis Bossart } 254770f3d99SPierre-Louis Bossart 255320b8b0dSShuming Fan reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT; 256320b8b0dSShuming Fan ret = regmap_read(rt711->regmap, reg, &jack_status); 257320b8b0dSShuming Fan if (ret < 0) 258320b8b0dSShuming Fan goto io_error; 259320b8b0dSShuming Fan 260320b8b0dSShuming Fan /* pin attached */ 261320b8b0dSShuming Fan if (jack_status & (1 << 31)) { 262320b8b0dSShuming Fan /* jack in */ 263320b8b0dSShuming Fan if (rt711->jack_type == 0) { 264320b8b0dSShuming Fan ret = rt711_headset_detect(rt711); 265320b8b0dSShuming Fan if (ret < 0) 266320b8b0dSShuming Fan return; 267320b8b0dSShuming Fan if (rt711->jack_type == SND_JACK_HEADSET) 268320b8b0dSShuming Fan btn_type = rt711_button_detect(rt711); 269320b8b0dSShuming Fan } else if (rt711->jack_type == SND_JACK_HEADSET) { 270320b8b0dSShuming Fan /* jack is already in, report button event */ 271320b8b0dSShuming Fan btn_type = rt711_button_detect(rt711); 272320b8b0dSShuming Fan } 273320b8b0dSShuming Fan } else { 274320b8b0dSShuming Fan /* jack out */ 275320b8b0dSShuming Fan rt711->jack_type = 0; 276320b8b0dSShuming Fan } 277320b8b0dSShuming Fan 278320b8b0dSShuming Fan dev_dbg(&rt711->slave->dev, 279320b8b0dSShuming Fan "in %s, jack_type=0x%x\n", __func__, rt711->jack_type); 280320b8b0dSShuming Fan dev_dbg(&rt711->slave->dev, 281320b8b0dSShuming Fan "in %s, btn_type=0x%x\n", __func__, btn_type); 282320b8b0dSShuming Fan 283320b8b0dSShuming Fan snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type, 284320b8b0dSShuming Fan SND_JACK_HEADSET | 285320b8b0dSShuming Fan SND_JACK_BTN_0 | SND_JACK_BTN_1 | 286320b8b0dSShuming Fan SND_JACK_BTN_2 | SND_JACK_BTN_3); 287320b8b0dSShuming Fan 288320b8b0dSShuming Fan if (btn_type) { 289320b8b0dSShuming Fan /* button released */ 290320b8b0dSShuming Fan snd_soc_jack_report(rt711->hs_jack, rt711->jack_type, 291320b8b0dSShuming Fan SND_JACK_HEADSET | 292320b8b0dSShuming Fan SND_JACK_BTN_0 | SND_JACK_BTN_1 | 293320b8b0dSShuming Fan SND_JACK_BTN_2 | SND_JACK_BTN_3); 294320b8b0dSShuming Fan 295320b8b0dSShuming Fan mod_delayed_work(system_power_efficient_wq, 296320b8b0dSShuming Fan &rt711->jack_btn_check_work, msecs_to_jiffies(200)); 297320b8b0dSShuming Fan } 298320b8b0dSShuming Fan 299320b8b0dSShuming Fan return; 300320b8b0dSShuming Fan 301320b8b0dSShuming Fan io_error: 302320b8b0dSShuming Fan pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); 303320b8b0dSShuming Fan } 304320b8b0dSShuming Fan 305320b8b0dSShuming Fan static void rt711_btn_check_handler(struct work_struct *work) 306320b8b0dSShuming Fan { 307320b8b0dSShuming Fan struct rt711_priv *rt711 = container_of(work, struct rt711_priv, 308320b8b0dSShuming Fan jack_btn_check_work.work); 309320b8b0dSShuming Fan int btn_type = 0, ret; 310320b8b0dSShuming Fan unsigned int jack_status = 0, reg; 311320b8b0dSShuming Fan 312320b8b0dSShuming Fan reg = RT711_VERB_GET_PIN_SENSE | RT711_HP_OUT; 313320b8b0dSShuming Fan ret = regmap_read(rt711->regmap, reg, &jack_status); 314320b8b0dSShuming Fan if (ret < 0) 315320b8b0dSShuming Fan goto io_error; 316320b8b0dSShuming Fan 317320b8b0dSShuming Fan /* pin attached */ 318320b8b0dSShuming Fan if (jack_status & (1 << 31)) { 319320b8b0dSShuming Fan if (rt711->jack_type == SND_JACK_HEADSET) { 320320b8b0dSShuming Fan /* jack is already in, report button event */ 321320b8b0dSShuming Fan btn_type = rt711_button_detect(rt711); 322320b8b0dSShuming Fan } 323320b8b0dSShuming Fan } else { 324320b8b0dSShuming Fan rt711->jack_type = 0; 325320b8b0dSShuming Fan } 326320b8b0dSShuming Fan 327320b8b0dSShuming Fan /* cbj comparator */ 328320b8b0dSShuming Fan ret = rt711_index_read(rt711->regmap, RT711_VENDOR_REG, 329320b8b0dSShuming Fan RT711_COMBO_JACK_AUTO_CTL2, ®); 330320b8b0dSShuming Fan if (ret < 0) 331320b8b0dSShuming Fan goto io_error; 332320b8b0dSShuming Fan 333320b8b0dSShuming Fan if ((reg & 0xf0) == 0xf0) 334320b8b0dSShuming Fan btn_type = 0; 335320b8b0dSShuming Fan 336320b8b0dSShuming Fan dev_dbg(&rt711->slave->dev, 337320b8b0dSShuming Fan "%s, btn_type=0x%x\n", __func__, btn_type); 338320b8b0dSShuming Fan snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type, 339320b8b0dSShuming Fan SND_JACK_HEADSET | 340320b8b0dSShuming Fan SND_JACK_BTN_0 | SND_JACK_BTN_1 | 341320b8b0dSShuming Fan SND_JACK_BTN_2 | SND_JACK_BTN_3); 342320b8b0dSShuming Fan 343320b8b0dSShuming Fan if (btn_type) { 344320b8b0dSShuming Fan /* button released */ 345320b8b0dSShuming Fan snd_soc_jack_report(rt711->hs_jack, rt711->jack_type, 346320b8b0dSShuming Fan SND_JACK_HEADSET | 347320b8b0dSShuming Fan SND_JACK_BTN_0 | SND_JACK_BTN_1 | 348320b8b0dSShuming Fan SND_JACK_BTN_2 | SND_JACK_BTN_3); 349320b8b0dSShuming Fan 350320b8b0dSShuming Fan mod_delayed_work(system_power_efficient_wq, 351320b8b0dSShuming Fan &rt711->jack_btn_check_work, msecs_to_jiffies(200)); 352320b8b0dSShuming Fan } 353320b8b0dSShuming Fan 354320b8b0dSShuming Fan return; 355320b8b0dSShuming Fan 356320b8b0dSShuming Fan io_error: 357320b8b0dSShuming Fan pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); 358320b8b0dSShuming Fan } 359320b8b0dSShuming Fan 360320b8b0dSShuming Fan static void rt711_jack_init(struct rt711_priv *rt711) 361320b8b0dSShuming Fan { 362320b8b0dSShuming Fan struct snd_soc_dapm_context *dapm = 363320b8b0dSShuming Fan snd_soc_component_get_dapm(rt711->component); 364320b8b0dSShuming Fan 365320b8b0dSShuming Fan mutex_lock(&rt711->calibrate_mutex); 366320b8b0dSShuming Fan /* power on */ 367320b8b0dSShuming Fan if (dapm->bias_level <= SND_SOC_BIAS_STANDBY) 368320b8b0dSShuming Fan regmap_write(rt711->regmap, 369320b8b0dSShuming Fan RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0); 370320b8b0dSShuming Fan 371320b8b0dSShuming Fan if (rt711->hs_jack) { 372320b8b0dSShuming Fan /* unsolicited response & IRQ control */ 373320b8b0dSShuming Fan regmap_write(rt711->regmap, 374320b8b0dSShuming Fan RT711_SET_MIC2_UNSOLICITED_ENABLE, 0x82); 375320b8b0dSShuming Fan regmap_write(rt711->regmap, 376320b8b0dSShuming Fan RT711_SET_HP_UNSOLICITED_ENABLE, 0x81); 377320b8b0dSShuming Fan regmap_write(rt711->regmap, 378320b8b0dSShuming Fan RT711_SET_INLINE_UNSOLICITED_ENABLE, 0x83); 379320b8b0dSShuming Fan rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 380320b8b0dSShuming Fan 0x10, 0x2420); 381320b8b0dSShuming Fan rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 382320b8b0dSShuming Fan 0x19, 0x2e11); 383320b8b0dSShuming Fan 384320b8b0dSShuming Fan switch (rt711->jd_src) { 385320b8b0dSShuming Fan case RT711_JD1: 386320b8b0dSShuming Fan /* default settings was already for JD1 */ 387320b8b0dSShuming Fan break; 388320b8b0dSShuming Fan case RT711_JD2: 389320b8b0dSShuming Fan rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 390320b8b0dSShuming Fan RT711_JD_CTL2, RT711_JD2_2PORT_200K_DECODE_HP | 391320b8b0dSShuming Fan RT711_HP_JD_SEL_JD2, 392320b8b0dSShuming Fan RT711_JD2_2PORT_200K_DECODE_HP | 393320b8b0dSShuming Fan RT711_HP_JD_SEL_JD2); 394320b8b0dSShuming Fan rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 395320b8b0dSShuming Fan RT711_CC_DET1, 396320b8b0dSShuming Fan RT711_HP_JD_FINAL_RESULT_CTL_JD12, 397320b8b0dSShuming Fan RT711_HP_JD_FINAL_RESULT_CTL_JD12); 398320b8b0dSShuming Fan break; 399683b0df2SShuming Fan case RT711_JD2_100K: 400683b0df2SShuming Fan rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 401683b0df2SShuming Fan RT711_JD_CTL2, RT711_JD2_2PORT_100K_DECODE | RT711_JD2_1PORT_TYPE_DECODE | 402683b0df2SShuming Fan RT711_HP_JD_SEL_JD2 | RT711_JD1_2PORT_TYPE_100K_DECODE, 403683b0df2SShuming Fan RT711_JD2_2PORT_100K_DECODE_HP | RT711_JD2_1PORT_JD_HP | 404683b0df2SShuming Fan RT711_HP_JD_SEL_JD2 | RT711_JD1_2PORT_JD_RESERVED); 405683b0df2SShuming Fan rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 406683b0df2SShuming Fan RT711_CC_DET1, 407683b0df2SShuming Fan RT711_HP_JD_FINAL_RESULT_CTL_JD12, 408683b0df2SShuming Fan RT711_HP_JD_FINAL_RESULT_CTL_JD12); 409683b0df2SShuming Fan break; 410683b0df2SShuming Fan case RT711_JD2_1P8V_1PORT: 411683b0df2SShuming Fan rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 412683b0df2SShuming Fan RT711_JD_CTL1, RT711_JD2_DIGITAL_JD_MODE_SEL, 413683b0df2SShuming Fan RT711_JD2_1_JD_MODE); 414683b0df2SShuming Fan rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 415683b0df2SShuming Fan RT711_JD_CTL2, RT711_JD2_1PORT_TYPE_DECODE | 416683b0df2SShuming Fan RT711_HP_JD_SEL_JD2, 417683b0df2SShuming Fan RT711_JD2_1PORT_JD_HP | 418683b0df2SShuming Fan RT711_HP_JD_SEL_JD2); 419683b0df2SShuming Fan rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 420683b0df2SShuming Fan RT711_JD_CTL4, RT711_JD2_PAD_PULL_UP_MASK | 421683b0df2SShuming Fan RT711_JD2_MODE_SEL_MASK, 422683b0df2SShuming Fan RT711_JD2_PAD_PULL_UP | 423683b0df2SShuming Fan RT711_JD2_MODE2_1P8V_1PORT); 424683b0df2SShuming Fan rt711_index_update_bits(rt711->regmap, RT711_VENDOR_REG, 425683b0df2SShuming Fan RT711_CC_DET1, 426683b0df2SShuming Fan RT711_HP_JD_FINAL_RESULT_CTL_JD12, 427683b0df2SShuming Fan RT711_HP_JD_FINAL_RESULT_CTL_JD12); 428683b0df2SShuming Fan break; 429320b8b0dSShuming Fan default: 430320b8b0dSShuming Fan dev_warn(rt711->component->dev, "Wrong JD source\n"); 431320b8b0dSShuming Fan break; 432320b8b0dSShuming Fan } 433320b8b0dSShuming Fan 434320b8b0dSShuming Fan dev_dbg(&rt711->slave->dev, "in %s enable\n", __func__); 435320b8b0dSShuming Fan 436320b8b0dSShuming Fan mod_delayed_work(system_power_efficient_wq, 437320b8b0dSShuming Fan &rt711->jack_detect_work, msecs_to_jiffies(250)); 438320b8b0dSShuming Fan } else { 439320b8b0dSShuming Fan regmap_write(rt711->regmap, 440320b8b0dSShuming Fan RT711_SET_MIC2_UNSOLICITED_ENABLE, 0x00); 441320b8b0dSShuming Fan regmap_write(rt711->regmap, 442320b8b0dSShuming Fan RT711_SET_HP_UNSOLICITED_ENABLE, 0x00); 443320b8b0dSShuming Fan regmap_write(rt711->regmap, 444320b8b0dSShuming Fan RT711_SET_INLINE_UNSOLICITED_ENABLE, 0x00); 445320b8b0dSShuming Fan 446320b8b0dSShuming Fan dev_dbg(&rt711->slave->dev, "in %s disable\n", __func__); 447320b8b0dSShuming Fan } 448320b8b0dSShuming Fan 449320b8b0dSShuming Fan /* power off */ 450320b8b0dSShuming Fan if (dapm->bias_level <= SND_SOC_BIAS_STANDBY) 451320b8b0dSShuming Fan regmap_write(rt711->regmap, 452320b8b0dSShuming Fan RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3); 453320b8b0dSShuming Fan mutex_unlock(&rt711->calibrate_mutex); 454320b8b0dSShuming Fan } 455320b8b0dSShuming Fan 456320b8b0dSShuming Fan static int rt711_set_jack_detect(struct snd_soc_component *component, 457320b8b0dSShuming Fan struct snd_soc_jack *hs_jack, void *data) 458320b8b0dSShuming Fan { 459320b8b0dSShuming Fan struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 460320b8b0dSShuming Fan 461320b8b0dSShuming Fan rt711->hs_jack = hs_jack; 462320b8b0dSShuming Fan 463320b8b0dSShuming Fan if (!rt711->hw_init) { 464320b8b0dSShuming Fan dev_dbg(&rt711->slave->dev, 465320b8b0dSShuming Fan "%s hw_init not ready yet\n", __func__); 466320b8b0dSShuming Fan return 0; 467320b8b0dSShuming Fan } 468320b8b0dSShuming Fan 469320b8b0dSShuming Fan rt711_jack_init(rt711); 470320b8b0dSShuming Fan 471320b8b0dSShuming Fan return 0; 472320b8b0dSShuming Fan } 473320b8b0dSShuming Fan 474320b8b0dSShuming Fan static void rt711_get_gain(struct rt711_priv *rt711, unsigned int addr_h, 475320b8b0dSShuming Fan unsigned int addr_l, unsigned int val_h, 476320b8b0dSShuming Fan unsigned int *r_val, unsigned int *l_val) 477320b8b0dSShuming Fan { 478320b8b0dSShuming Fan /* R Channel */ 479320b8b0dSShuming Fan *r_val = (val_h << 8); 480320b8b0dSShuming Fan regmap_read(rt711->regmap, addr_l, r_val); 481320b8b0dSShuming Fan 482320b8b0dSShuming Fan /* L Channel */ 483320b8b0dSShuming Fan val_h |= 0x20; 484320b8b0dSShuming Fan *l_val = (val_h << 8); 485320b8b0dSShuming Fan regmap_read(rt711->regmap, addr_h, l_val); 486320b8b0dSShuming Fan } 487320b8b0dSShuming Fan 488320b8b0dSShuming Fan /* For Verb-Set Amplifier Gain (Verb ID = 3h) */ 489320b8b0dSShuming Fan static int rt711_set_amp_gain_put(struct snd_kcontrol *kcontrol, 490320b8b0dSShuming Fan struct snd_ctl_elem_value *ucontrol) 491320b8b0dSShuming Fan { 492320b8b0dSShuming Fan struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 493320b8b0dSShuming Fan struct snd_soc_dapm_context *dapm = 494320b8b0dSShuming Fan snd_soc_component_get_dapm(component); 495320b8b0dSShuming Fan struct soc_mixer_control *mc = 496320b8b0dSShuming Fan (struct soc_mixer_control *)kcontrol->private_value; 497320b8b0dSShuming Fan struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 498320b8b0dSShuming Fan unsigned int addr_h, addr_l, val_h, val_ll, val_lr; 499320b8b0dSShuming Fan unsigned int read_ll, read_rl; 500320b8b0dSShuming Fan int i; 501320b8b0dSShuming Fan 5026108f990SShuming Fan mutex_lock(&rt711->calibrate_mutex); 5036108f990SShuming Fan 504320b8b0dSShuming Fan /* Can't use update bit function, so read the original value first */ 505320b8b0dSShuming Fan addr_h = mc->reg; 506320b8b0dSShuming Fan addr_l = mc->rreg; 507320b8b0dSShuming Fan if (mc->shift == RT711_DIR_OUT_SFT) /* output */ 508320b8b0dSShuming Fan val_h = 0x80; 509320b8b0dSShuming Fan else /* input */ 510320b8b0dSShuming Fan val_h = 0x0; 511320b8b0dSShuming Fan 512320b8b0dSShuming Fan rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll); 513320b8b0dSShuming Fan 514320b8b0dSShuming Fan /* L Channel */ 515320b8b0dSShuming Fan if (mc->invert) { 516320b8b0dSShuming Fan /* for mute/unmute */ 517320b8b0dSShuming Fan val_ll = (mc->max - ucontrol->value.integer.value[0]) 518320b8b0dSShuming Fan << RT711_MUTE_SFT; 519320b8b0dSShuming Fan /* keep gain */ 520320b8b0dSShuming Fan read_ll = read_ll & 0x7f; 521320b8b0dSShuming Fan val_ll |= read_ll; 522320b8b0dSShuming Fan } else { 523320b8b0dSShuming Fan /* for gain */ 524320b8b0dSShuming Fan val_ll = ((ucontrol->value.integer.value[0]) & 0x7f); 525320b8b0dSShuming Fan if (val_ll > mc->max) 526320b8b0dSShuming Fan val_ll = mc->max; 527320b8b0dSShuming Fan /* keep mute status */ 528320b8b0dSShuming Fan read_ll = read_ll & (1 << RT711_MUTE_SFT); 529320b8b0dSShuming Fan val_ll |= read_ll; 530320b8b0dSShuming Fan } 531320b8b0dSShuming Fan 532320b8b0dSShuming Fan if (dapm->bias_level <= SND_SOC_BIAS_STANDBY) 533320b8b0dSShuming Fan regmap_write(rt711->regmap, 534320b8b0dSShuming Fan RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0); 535320b8b0dSShuming Fan 536320b8b0dSShuming Fan /* R Channel */ 537320b8b0dSShuming Fan if (mc->invert) { 538320b8b0dSShuming Fan /* for mute/unmute */ 539320b8b0dSShuming Fan val_lr = (mc->max - ucontrol->value.integer.value[1]) 540320b8b0dSShuming Fan << RT711_MUTE_SFT; 541320b8b0dSShuming Fan /* keep gain */ 542320b8b0dSShuming Fan read_rl = read_rl & 0x7f; 543320b8b0dSShuming Fan val_lr |= read_rl; 544320b8b0dSShuming Fan } else { 545320b8b0dSShuming Fan /* for gain */ 546320b8b0dSShuming Fan val_lr = ((ucontrol->value.integer.value[1]) & 0x7f); 547320b8b0dSShuming Fan if (val_lr > mc->max) 548320b8b0dSShuming Fan val_lr = mc->max; 549320b8b0dSShuming Fan /* keep mute status */ 550320b8b0dSShuming Fan read_rl = read_rl & (1 << RT711_MUTE_SFT); 551320b8b0dSShuming Fan val_lr |= read_rl; 552320b8b0dSShuming Fan } 553320b8b0dSShuming Fan 554320b8b0dSShuming Fan for (i = 0; i < 3; i++) { /* retry 3 times at most */ 555320b8b0dSShuming Fan 556320b8b0dSShuming Fan if (val_ll == val_lr) { 557320b8b0dSShuming Fan /* Set both L/R channels at the same time */ 558320b8b0dSShuming Fan val_h = (1 << mc->shift) | (3 << 4); 559320b8b0dSShuming Fan regmap_write(rt711->regmap, 560320b8b0dSShuming Fan addr_h, (val_h << 8 | val_ll)); 561320b8b0dSShuming Fan regmap_write(rt711->regmap, 562320b8b0dSShuming Fan addr_l, (val_h << 8 | val_ll)); 563320b8b0dSShuming Fan } else { 564320b8b0dSShuming Fan /* Lch*/ 565320b8b0dSShuming Fan val_h = (1 << mc->shift) | (1 << 5); 566320b8b0dSShuming Fan regmap_write(rt711->regmap, 567320b8b0dSShuming Fan addr_h, (val_h << 8 | val_ll)); 568320b8b0dSShuming Fan 569320b8b0dSShuming Fan /* Rch */ 570320b8b0dSShuming Fan val_h = (1 << mc->shift) | (1 << 4); 571320b8b0dSShuming Fan regmap_write(rt711->regmap, 572320b8b0dSShuming Fan addr_l, (val_h << 8 | val_lr)); 573320b8b0dSShuming Fan } 574320b8b0dSShuming Fan /* check result */ 575320b8b0dSShuming Fan if (mc->shift == RT711_DIR_OUT_SFT) /* output */ 576320b8b0dSShuming Fan val_h = 0x80; 577320b8b0dSShuming Fan else /* input */ 578320b8b0dSShuming Fan val_h = 0x0; 579320b8b0dSShuming Fan 580320b8b0dSShuming Fan rt711_get_gain(rt711, addr_h, addr_l, val_h, 581320b8b0dSShuming Fan &read_rl, &read_ll); 582320b8b0dSShuming Fan if (read_rl == val_lr && read_ll == val_ll) 583320b8b0dSShuming Fan break; 584320b8b0dSShuming Fan } 585320b8b0dSShuming Fan 586320b8b0dSShuming Fan if (dapm->bias_level <= SND_SOC_BIAS_STANDBY) 587320b8b0dSShuming Fan regmap_write(rt711->regmap, 588320b8b0dSShuming Fan RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3); 5896108f990SShuming Fan 5906108f990SShuming Fan mutex_unlock(&rt711->calibrate_mutex); 591320b8b0dSShuming Fan return 0; 592320b8b0dSShuming Fan } 593320b8b0dSShuming Fan 594320b8b0dSShuming Fan static int rt711_set_amp_gain_get(struct snd_kcontrol *kcontrol, 595320b8b0dSShuming Fan struct snd_ctl_elem_value *ucontrol) 596320b8b0dSShuming Fan { 597320b8b0dSShuming Fan struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 598320b8b0dSShuming Fan struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 599320b8b0dSShuming Fan struct soc_mixer_control *mc = 600320b8b0dSShuming Fan (struct soc_mixer_control *)kcontrol->private_value; 601320b8b0dSShuming Fan unsigned int addr_h, addr_l, val_h; 602320b8b0dSShuming Fan unsigned int read_ll, read_rl; 603320b8b0dSShuming Fan 604320b8b0dSShuming Fan /* switch to get command */ 605320b8b0dSShuming Fan addr_h = mc->reg; 606320b8b0dSShuming Fan addr_l = mc->rreg; 607320b8b0dSShuming Fan if (mc->shift == RT711_DIR_OUT_SFT) /* output */ 608320b8b0dSShuming Fan val_h = 0x80; 609320b8b0dSShuming Fan else /* input */ 610320b8b0dSShuming Fan val_h = 0x0; 611320b8b0dSShuming Fan 612320b8b0dSShuming Fan rt711_get_gain(rt711, addr_h, addr_l, val_h, &read_rl, &read_ll); 613320b8b0dSShuming Fan 614320b8b0dSShuming Fan if (mc->invert) { 615320b8b0dSShuming Fan /* mute/unmute for switch controls */ 616320b8b0dSShuming Fan read_ll = !((read_ll & 0x80) >> RT711_MUTE_SFT); 617320b8b0dSShuming Fan read_rl = !((read_rl & 0x80) >> RT711_MUTE_SFT); 618320b8b0dSShuming Fan } else { 619320b8b0dSShuming Fan /* for gain volume controls */ 620320b8b0dSShuming Fan read_ll = read_ll & 0x7f; 621320b8b0dSShuming Fan read_rl = read_rl & 0x7f; 622320b8b0dSShuming Fan } 623320b8b0dSShuming Fan ucontrol->value.integer.value[0] = read_ll; 624320b8b0dSShuming Fan ucontrol->value.integer.value[1] = read_rl; 625320b8b0dSShuming Fan 626320b8b0dSShuming Fan return 0; 627320b8b0dSShuming Fan } 628320b8b0dSShuming Fan 629320b8b0dSShuming Fan static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0); 630320b8b0dSShuming Fan static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0); 631320b8b0dSShuming Fan static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0); 632320b8b0dSShuming Fan 633320b8b0dSShuming Fan static const struct snd_kcontrol_new rt711_snd_controls[] = { 634320b8b0dSShuming Fan SOC_DOUBLE_R_EXT_TLV("DAC Surr Playback Volume", 635320b8b0dSShuming Fan RT711_SET_GAIN_DAC2_H, RT711_SET_GAIN_DAC2_L, 636320b8b0dSShuming Fan RT711_DIR_OUT_SFT, 0x57, 0, 637320b8b0dSShuming Fan rt711_set_amp_gain_get, rt711_set_amp_gain_put, out_vol_tlv), 638320b8b0dSShuming Fan SOC_DOUBLE_R_EXT("ADC 08 Capture Switch", 639320b8b0dSShuming Fan RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L, 640320b8b0dSShuming Fan RT711_DIR_IN_SFT, 1, 1, 641320b8b0dSShuming Fan rt711_set_amp_gain_get, rt711_set_amp_gain_put), 642320b8b0dSShuming Fan SOC_DOUBLE_R_EXT("ADC 09 Capture Switch", 643320b8b0dSShuming Fan RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L, 644320b8b0dSShuming Fan RT711_DIR_IN_SFT, 1, 1, 645320b8b0dSShuming Fan rt711_set_amp_gain_get, rt711_set_amp_gain_put), 646320b8b0dSShuming Fan SOC_DOUBLE_R_EXT_TLV("ADC 08 Capture Volume", 647320b8b0dSShuming Fan RT711_SET_GAIN_ADC2_H, RT711_SET_GAIN_ADC2_L, 648320b8b0dSShuming Fan RT711_DIR_IN_SFT, 0x3f, 0, 649320b8b0dSShuming Fan rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv), 650320b8b0dSShuming Fan SOC_DOUBLE_R_EXT_TLV("ADC 09 Capture Volume", 651320b8b0dSShuming Fan RT711_SET_GAIN_ADC1_H, RT711_SET_GAIN_ADC1_L, 652320b8b0dSShuming Fan RT711_DIR_IN_SFT, 0x3f, 0, 653320b8b0dSShuming Fan rt711_set_amp_gain_get, rt711_set_amp_gain_put, in_vol_tlv), 654320b8b0dSShuming Fan SOC_DOUBLE_R_EXT_TLV("AMIC Volume", 655320b8b0dSShuming Fan RT711_SET_GAIN_AMIC_H, RT711_SET_GAIN_AMIC_L, 656320b8b0dSShuming Fan RT711_DIR_IN_SFT, 3, 0, 657320b8b0dSShuming Fan rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv), 658320b8b0dSShuming Fan SOC_DOUBLE_R_EXT_TLV("DMIC1 Volume", 659320b8b0dSShuming Fan RT711_SET_GAIN_DMIC1_H, RT711_SET_GAIN_DMIC1_L, 660320b8b0dSShuming Fan RT711_DIR_IN_SFT, 3, 0, 661320b8b0dSShuming Fan rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv), 662320b8b0dSShuming Fan SOC_DOUBLE_R_EXT_TLV("DMIC2 Volume", 663320b8b0dSShuming Fan RT711_SET_GAIN_DMIC2_H, RT711_SET_GAIN_DMIC2_L, 664320b8b0dSShuming Fan RT711_DIR_IN_SFT, 3, 0, 665320b8b0dSShuming Fan rt711_set_amp_gain_get, rt711_set_amp_gain_put, mic_vol_tlv), 666320b8b0dSShuming Fan }; 667320b8b0dSShuming Fan 668320b8b0dSShuming Fan static int rt711_mux_get(struct snd_kcontrol *kcontrol, 669320b8b0dSShuming Fan struct snd_ctl_elem_value *ucontrol) 670320b8b0dSShuming Fan { 671320b8b0dSShuming Fan struct snd_soc_component *component = 672320b8b0dSShuming Fan snd_soc_dapm_kcontrol_component(kcontrol); 673320b8b0dSShuming Fan struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 674320b8b0dSShuming Fan unsigned int reg, val = 0, nid; 675320b8b0dSShuming Fan int ret; 676320b8b0dSShuming Fan 677320b8b0dSShuming Fan if (strstr(ucontrol->id.name, "ADC 22 Mux")) 678320b8b0dSShuming Fan nid = RT711_MIXER_IN1; 679320b8b0dSShuming Fan else if (strstr(ucontrol->id.name, "ADC 23 Mux")) 680320b8b0dSShuming Fan nid = RT711_MIXER_IN2; 681320b8b0dSShuming Fan else 682320b8b0dSShuming Fan return -EINVAL; 683320b8b0dSShuming Fan 684320b8b0dSShuming Fan /* vid = 0xf01 */ 685320b8b0dSShuming Fan reg = RT711_VERB_SET_CONNECT_SEL | nid; 686320b8b0dSShuming Fan ret = regmap_read(rt711->regmap, reg, &val); 687320b8b0dSShuming Fan if (ret < 0) { 688320b8b0dSShuming Fan dev_err(component->dev, "%s: sdw read failed: %d\n", 689320b8b0dSShuming Fan __func__, ret); 690320b8b0dSShuming Fan return ret; 691320b8b0dSShuming Fan } 692320b8b0dSShuming Fan 693320b8b0dSShuming Fan ucontrol->value.enumerated.item[0] = val; 694320b8b0dSShuming Fan 695320b8b0dSShuming Fan return 0; 696320b8b0dSShuming Fan } 697320b8b0dSShuming Fan 698320b8b0dSShuming Fan static int rt711_mux_put(struct snd_kcontrol *kcontrol, 699320b8b0dSShuming Fan struct snd_ctl_elem_value *ucontrol) 700320b8b0dSShuming Fan { 701320b8b0dSShuming Fan struct snd_soc_component *component = 702320b8b0dSShuming Fan snd_soc_dapm_kcontrol_component(kcontrol); 703320b8b0dSShuming Fan struct snd_soc_dapm_context *dapm = 704320b8b0dSShuming Fan snd_soc_dapm_kcontrol_dapm(kcontrol); 705320b8b0dSShuming Fan struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 706320b8b0dSShuming Fan struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 707320b8b0dSShuming Fan unsigned int *item = ucontrol->value.enumerated.item; 708320b8b0dSShuming Fan unsigned int val, val2 = 0, change, reg, nid; 709320b8b0dSShuming Fan int ret; 710320b8b0dSShuming Fan 711320b8b0dSShuming Fan if (item[0] >= e->items) 712320b8b0dSShuming Fan return -EINVAL; 713320b8b0dSShuming Fan 714320b8b0dSShuming Fan if (strstr(ucontrol->id.name, "ADC 22 Mux")) 715320b8b0dSShuming Fan nid = RT711_MIXER_IN1; 716320b8b0dSShuming Fan else if (strstr(ucontrol->id.name, "ADC 23 Mux")) 717320b8b0dSShuming Fan nid = RT711_MIXER_IN2; 718320b8b0dSShuming Fan else 719320b8b0dSShuming Fan return -EINVAL; 720320b8b0dSShuming Fan 721320b8b0dSShuming Fan /* Verb ID = 0x701h */ 722320b8b0dSShuming Fan val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; 723320b8b0dSShuming Fan 724320b8b0dSShuming Fan reg = RT711_VERB_SET_CONNECT_SEL | nid; 725320b8b0dSShuming Fan ret = regmap_read(rt711->regmap, reg, &val2); 726320b8b0dSShuming Fan if (ret < 0) { 727320b8b0dSShuming Fan dev_err(component->dev, "%s: sdw read failed: %d\n", 728320b8b0dSShuming Fan __func__, ret); 729320b8b0dSShuming Fan return ret; 730320b8b0dSShuming Fan } 731320b8b0dSShuming Fan 732320b8b0dSShuming Fan if (val == val2) 733320b8b0dSShuming Fan change = 0; 734320b8b0dSShuming Fan else 735320b8b0dSShuming Fan change = 1; 736320b8b0dSShuming Fan 737320b8b0dSShuming Fan if (change) { 738320b8b0dSShuming Fan reg = RT711_VERB_SET_CONNECT_SEL | nid; 739320b8b0dSShuming Fan regmap_write(rt711->regmap, reg, val); 740320b8b0dSShuming Fan } 741320b8b0dSShuming Fan 742320b8b0dSShuming Fan snd_soc_dapm_mux_update_power(dapm, kcontrol, 743320b8b0dSShuming Fan item[0], e, NULL); 744320b8b0dSShuming Fan 745320b8b0dSShuming Fan return change; 746320b8b0dSShuming Fan } 747320b8b0dSShuming Fan 748320b8b0dSShuming Fan static const char * const adc_mux_text[] = { 749320b8b0dSShuming Fan "MIC2", 750320b8b0dSShuming Fan "LINE1", 751320b8b0dSShuming Fan "LINE2", 752320b8b0dSShuming Fan "DMIC", 753320b8b0dSShuming Fan }; 754320b8b0dSShuming Fan 755320b8b0dSShuming Fan static SOC_ENUM_SINGLE_DECL( 756320b8b0dSShuming Fan rt711_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text); 757320b8b0dSShuming Fan 758320b8b0dSShuming Fan static SOC_ENUM_SINGLE_DECL( 759320b8b0dSShuming Fan rt711_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text); 760320b8b0dSShuming Fan 761320b8b0dSShuming Fan static const struct snd_kcontrol_new rt711_adc22_mux = 762320b8b0dSShuming Fan SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt711_adc22_enum, 763320b8b0dSShuming Fan rt711_mux_get, rt711_mux_put); 764320b8b0dSShuming Fan 765320b8b0dSShuming Fan static const struct snd_kcontrol_new rt711_adc23_mux = 766320b8b0dSShuming Fan SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt711_adc23_enum, 767320b8b0dSShuming Fan rt711_mux_get, rt711_mux_put); 768320b8b0dSShuming Fan 769320b8b0dSShuming Fan static int rt711_dac_surround_event(struct snd_soc_dapm_widget *w, 770320b8b0dSShuming Fan struct snd_kcontrol *kcontrol, int event) 771320b8b0dSShuming Fan { 772320b8b0dSShuming Fan struct snd_soc_component *component = 773320b8b0dSShuming Fan snd_soc_dapm_to_component(w->dapm); 774320b8b0dSShuming Fan struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 775320b8b0dSShuming Fan unsigned int val_h = (1 << RT711_DIR_OUT_SFT) | (0x3 << 4); 776320b8b0dSShuming Fan unsigned int val_l; 777320b8b0dSShuming Fan 778320b8b0dSShuming Fan switch (event) { 779320b8b0dSShuming Fan case SND_SOC_DAPM_POST_PMU: 780320b8b0dSShuming Fan regmap_write(rt711->regmap, 781320b8b0dSShuming Fan RT711_SET_STREAMID_DAC2, 0x10); 782320b8b0dSShuming Fan 783320b8b0dSShuming Fan val_l = 0x00; 784320b8b0dSShuming Fan regmap_write(rt711->regmap, 785320b8b0dSShuming Fan RT711_SET_GAIN_HP_H, (val_h << 8 | val_l)); 786320b8b0dSShuming Fan break; 787320b8b0dSShuming Fan case SND_SOC_DAPM_PRE_PMD: 788320b8b0dSShuming Fan val_l = (1 << RT711_MUTE_SFT); 789320b8b0dSShuming Fan regmap_write(rt711->regmap, 790320b8b0dSShuming Fan RT711_SET_GAIN_HP_H, (val_h << 8 | val_l)); 791320b8b0dSShuming Fan usleep_range(50000, 55000); 792320b8b0dSShuming Fan 793320b8b0dSShuming Fan regmap_write(rt711->regmap, 794320b8b0dSShuming Fan RT711_SET_STREAMID_DAC2, 0x00); 795320b8b0dSShuming Fan break; 796320b8b0dSShuming Fan } 797320b8b0dSShuming Fan return 0; 798320b8b0dSShuming Fan } 799320b8b0dSShuming Fan 800320b8b0dSShuming Fan static int rt711_adc_09_event(struct snd_soc_dapm_widget *w, 801320b8b0dSShuming Fan struct snd_kcontrol *kcontrol, int event) 802320b8b0dSShuming Fan { 803320b8b0dSShuming Fan struct snd_soc_component *component = 804320b8b0dSShuming Fan snd_soc_dapm_to_component(w->dapm); 805320b8b0dSShuming Fan struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 806320b8b0dSShuming Fan 807320b8b0dSShuming Fan switch (event) { 808320b8b0dSShuming Fan case SND_SOC_DAPM_POST_PMU: 809320b8b0dSShuming Fan regmap_write(rt711->regmap, 810320b8b0dSShuming Fan RT711_SET_STREAMID_ADC1, 0x10); 811320b8b0dSShuming Fan break; 812320b8b0dSShuming Fan case SND_SOC_DAPM_PRE_PMD: 813320b8b0dSShuming Fan regmap_write(rt711->regmap, 814320b8b0dSShuming Fan RT711_SET_STREAMID_ADC1, 0x00); 815320b8b0dSShuming Fan break; 816320b8b0dSShuming Fan } 817320b8b0dSShuming Fan return 0; 818320b8b0dSShuming Fan } 819320b8b0dSShuming Fan 820320b8b0dSShuming Fan static int rt711_adc_08_event(struct snd_soc_dapm_widget *w, 821320b8b0dSShuming Fan struct snd_kcontrol *kcontrol, int event) 822320b8b0dSShuming Fan { 823320b8b0dSShuming Fan struct snd_soc_component *component = 824320b8b0dSShuming Fan snd_soc_dapm_to_component(w->dapm); 825320b8b0dSShuming Fan struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 826320b8b0dSShuming Fan 827320b8b0dSShuming Fan switch (event) { 828320b8b0dSShuming Fan case SND_SOC_DAPM_POST_PMU: 829320b8b0dSShuming Fan regmap_write(rt711->regmap, 830320b8b0dSShuming Fan RT711_SET_STREAMID_ADC2, 0x10); 831320b8b0dSShuming Fan break; 832320b8b0dSShuming Fan case SND_SOC_DAPM_PRE_PMD: 833320b8b0dSShuming Fan regmap_write(rt711->regmap, 834320b8b0dSShuming Fan RT711_SET_STREAMID_ADC2, 0x00); 835320b8b0dSShuming Fan break; 836320b8b0dSShuming Fan } 837320b8b0dSShuming Fan return 0; 838320b8b0dSShuming Fan } 839320b8b0dSShuming Fan 840320b8b0dSShuming Fan static const struct snd_soc_dapm_widget rt711_dapm_widgets[] = { 841320b8b0dSShuming Fan SND_SOC_DAPM_OUTPUT("HP"), 842320b8b0dSShuming Fan SND_SOC_DAPM_INPUT("MIC2"), 843320b8b0dSShuming Fan SND_SOC_DAPM_INPUT("DMIC1"), 844320b8b0dSShuming Fan SND_SOC_DAPM_INPUT("DMIC2"), 845320b8b0dSShuming Fan SND_SOC_DAPM_INPUT("LINE1"), 846320b8b0dSShuming Fan SND_SOC_DAPM_INPUT("LINE2"), 847320b8b0dSShuming Fan 848320b8b0dSShuming Fan SND_SOC_DAPM_DAC_E("DAC Surround", NULL, SND_SOC_NOPM, 0, 0, 849320b8b0dSShuming Fan rt711_dac_surround_event, 850320b8b0dSShuming Fan SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 851320b8b0dSShuming Fan SND_SOC_DAPM_ADC_E("ADC 09", NULL, SND_SOC_NOPM, 0, 0, 852320b8b0dSShuming Fan rt711_adc_09_event, 853320b8b0dSShuming Fan SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 854320b8b0dSShuming Fan SND_SOC_DAPM_ADC_E("ADC 08", NULL, SND_SOC_NOPM, 0, 0, 855320b8b0dSShuming Fan rt711_adc_08_event, 856320b8b0dSShuming Fan SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 857320b8b0dSShuming Fan SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0, 858320b8b0dSShuming Fan &rt711_adc22_mux), 859320b8b0dSShuming Fan SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0, 860320b8b0dSShuming Fan &rt711_adc23_mux), 861320b8b0dSShuming Fan 862320b8b0dSShuming Fan SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0), 863320b8b0dSShuming Fan SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0), 864320b8b0dSShuming Fan SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0), 865320b8b0dSShuming Fan }; 866320b8b0dSShuming Fan 867320b8b0dSShuming Fan static const struct snd_soc_dapm_route rt711_audio_map[] = { 868320b8b0dSShuming Fan {"DAC Surround", NULL, "DP3RX"}, 869320b8b0dSShuming Fan {"DP2TX", NULL, "ADC 09"}, 870320b8b0dSShuming Fan {"DP4TX", NULL, "ADC 08"}, 871320b8b0dSShuming Fan 872320b8b0dSShuming Fan {"ADC 09", NULL, "ADC 22 Mux"}, 873320b8b0dSShuming Fan {"ADC 08", NULL, "ADC 23 Mux"}, 874320b8b0dSShuming Fan {"ADC 22 Mux", "DMIC", "DMIC1"}, 875320b8b0dSShuming Fan {"ADC 22 Mux", "LINE1", "LINE1"}, 876320b8b0dSShuming Fan {"ADC 22 Mux", "LINE2", "LINE2"}, 877320b8b0dSShuming Fan {"ADC 22 Mux", "MIC2", "MIC2"}, 878320b8b0dSShuming Fan {"ADC 23 Mux", "DMIC", "DMIC2"}, 879320b8b0dSShuming Fan {"ADC 23 Mux", "LINE1", "LINE1"}, 880320b8b0dSShuming Fan {"ADC 23 Mux", "LINE2", "LINE2"}, 881320b8b0dSShuming Fan {"ADC 23 Mux", "MIC2", "MIC2"}, 882320b8b0dSShuming Fan 883320b8b0dSShuming Fan {"HP", NULL, "DAC Surround"}, 884320b8b0dSShuming Fan }; 885320b8b0dSShuming Fan 886320b8b0dSShuming Fan static int rt711_set_bias_level(struct snd_soc_component *component, 887320b8b0dSShuming Fan enum snd_soc_bias_level level) 888320b8b0dSShuming Fan { 889320b8b0dSShuming Fan struct snd_soc_dapm_context *dapm = 890320b8b0dSShuming Fan snd_soc_component_get_dapm(component); 891320b8b0dSShuming Fan struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 892320b8b0dSShuming Fan 893320b8b0dSShuming Fan switch (level) { 894320b8b0dSShuming Fan case SND_SOC_BIAS_PREPARE: 895320b8b0dSShuming Fan if (dapm->bias_level == SND_SOC_BIAS_STANDBY) { 896320b8b0dSShuming Fan regmap_write(rt711->regmap, 897320b8b0dSShuming Fan RT711_SET_AUDIO_POWER_STATE, 898320b8b0dSShuming Fan AC_PWRST_D0); 899320b8b0dSShuming Fan } 900320b8b0dSShuming Fan break; 901320b8b0dSShuming Fan 902320b8b0dSShuming Fan case SND_SOC_BIAS_STANDBY: 9036108f990SShuming Fan mutex_lock(&rt711->calibrate_mutex); 904320b8b0dSShuming Fan regmap_write(rt711->regmap, 905320b8b0dSShuming Fan RT711_SET_AUDIO_POWER_STATE, 906320b8b0dSShuming Fan AC_PWRST_D3); 9076108f990SShuming Fan mutex_unlock(&rt711->calibrate_mutex); 908320b8b0dSShuming Fan break; 909320b8b0dSShuming Fan 910320b8b0dSShuming Fan default: 911320b8b0dSShuming Fan break; 912320b8b0dSShuming Fan } 913320b8b0dSShuming Fan 914320b8b0dSShuming Fan return 0; 915320b8b0dSShuming Fan } 916320b8b0dSShuming Fan 917320b8b0dSShuming Fan static int rt711_parse_dt(struct rt711_priv *rt711, struct device *dev) 918320b8b0dSShuming Fan { 919320b8b0dSShuming Fan device_property_read_u32(dev, "realtek,jd-src", 920320b8b0dSShuming Fan &rt711->jd_src); 921320b8b0dSShuming Fan 922320b8b0dSShuming Fan return 0; 923320b8b0dSShuming Fan } 924320b8b0dSShuming Fan 925320b8b0dSShuming Fan static int rt711_probe(struct snd_soc_component *component) 926320b8b0dSShuming Fan { 927320b8b0dSShuming Fan struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 928320b8b0dSShuming Fan 929320b8b0dSShuming Fan rt711_parse_dt(rt711, &rt711->slave->dev); 930320b8b0dSShuming Fan rt711->component = component; 931320b8b0dSShuming Fan 932320b8b0dSShuming Fan return 0; 933320b8b0dSShuming Fan } 934320b8b0dSShuming Fan 935899b1254SBard Liao static void rt711_remove(struct snd_soc_component *component) 936899b1254SBard Liao { 937899b1254SBard Liao struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 938899b1254SBard Liao 939899b1254SBard Liao regcache_cache_only(rt711->regmap, true); 940899b1254SBard Liao } 941899b1254SBard Liao 942320b8b0dSShuming Fan static const struct snd_soc_component_driver soc_codec_dev_rt711 = { 943320b8b0dSShuming Fan .probe = rt711_probe, 944320b8b0dSShuming Fan .set_bias_level = rt711_set_bias_level, 945320b8b0dSShuming Fan .controls = rt711_snd_controls, 946320b8b0dSShuming Fan .num_controls = ARRAY_SIZE(rt711_snd_controls), 947320b8b0dSShuming Fan .dapm_widgets = rt711_dapm_widgets, 948320b8b0dSShuming Fan .num_dapm_widgets = ARRAY_SIZE(rt711_dapm_widgets), 949320b8b0dSShuming Fan .dapm_routes = rt711_audio_map, 950320b8b0dSShuming Fan .num_dapm_routes = ARRAY_SIZE(rt711_audio_map), 951320b8b0dSShuming Fan .set_jack = rt711_set_jack_detect, 952899b1254SBard Liao .remove = rt711_remove, 95333f06beaSCharles Keepax .endianness = 1, 954320b8b0dSShuming Fan }; 955320b8b0dSShuming Fan 956320b8b0dSShuming Fan static int rt711_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream, 957320b8b0dSShuming Fan int direction) 958320b8b0dSShuming Fan { 959320b8b0dSShuming Fan struct sdw_stream_data *stream; 960320b8b0dSShuming Fan 96107b542feSPierre-Louis Bossart if (!sdw_stream) 96207b542feSPierre-Louis Bossart return 0; 96307b542feSPierre-Louis Bossart 964320b8b0dSShuming Fan stream = kzalloc(sizeof(*stream), GFP_KERNEL); 965320b8b0dSShuming Fan if (!stream) 966320b8b0dSShuming Fan return -ENOMEM; 967320b8b0dSShuming Fan 9684a550007SPierre-Louis Bossart stream->sdw_stream = sdw_stream; 969320b8b0dSShuming Fan 970320b8b0dSShuming Fan /* Use tx_mask or rx_mask to configure stream tag and set dma_data */ 971320b8b0dSShuming Fan if (direction == SNDRV_PCM_STREAM_PLAYBACK) 972320b8b0dSShuming Fan dai->playback_dma_data = stream; 973320b8b0dSShuming Fan else 974320b8b0dSShuming Fan dai->capture_dma_data = stream; 975320b8b0dSShuming Fan 976320b8b0dSShuming Fan return 0; 977320b8b0dSShuming Fan } 978320b8b0dSShuming Fan 979320b8b0dSShuming Fan static void rt711_shutdown(struct snd_pcm_substream *substream, 980320b8b0dSShuming Fan struct snd_soc_dai *dai) 981320b8b0dSShuming Fan { 982320b8b0dSShuming Fan struct sdw_stream_data *stream; 983320b8b0dSShuming Fan 984320b8b0dSShuming Fan stream = snd_soc_dai_get_dma_data(dai, substream); 985320b8b0dSShuming Fan snd_soc_dai_set_dma_data(dai, substream, NULL); 986320b8b0dSShuming Fan kfree(stream); 987320b8b0dSShuming Fan } 988320b8b0dSShuming Fan 989320b8b0dSShuming Fan static int rt711_pcm_hw_params(struct snd_pcm_substream *substream, 990320b8b0dSShuming Fan struct snd_pcm_hw_params *params, 991320b8b0dSShuming Fan struct snd_soc_dai *dai) 992320b8b0dSShuming Fan { 993320b8b0dSShuming Fan struct snd_soc_component *component = dai->component; 994320b8b0dSShuming Fan struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 995320b8b0dSShuming Fan struct sdw_stream_config stream_config; 996320b8b0dSShuming Fan struct sdw_port_config port_config; 997320b8b0dSShuming Fan enum sdw_data_direction direction; 998320b8b0dSShuming Fan struct sdw_stream_data *stream; 999320b8b0dSShuming Fan int retval, port, num_channels; 1000320b8b0dSShuming Fan unsigned int val = 0; 1001320b8b0dSShuming Fan 1002320b8b0dSShuming Fan dev_dbg(dai->dev, "%s %s", __func__, dai->name); 1003320b8b0dSShuming Fan stream = snd_soc_dai_get_dma_data(dai, substream); 1004320b8b0dSShuming Fan 1005320b8b0dSShuming Fan if (!stream) 1006320b8b0dSShuming Fan return -EINVAL; 1007320b8b0dSShuming Fan 1008320b8b0dSShuming Fan if (!rt711->slave) 1009320b8b0dSShuming Fan return -EINVAL; 1010320b8b0dSShuming Fan 1011320b8b0dSShuming Fan /* SoundWire specific configuration */ 1012320b8b0dSShuming Fan if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1013320b8b0dSShuming Fan direction = SDW_DATA_DIR_RX; 1014320b8b0dSShuming Fan port = 3; 1015320b8b0dSShuming Fan } else { 1016320b8b0dSShuming Fan direction = SDW_DATA_DIR_TX; 1017320b8b0dSShuming Fan if (dai->id == RT711_AIF1) 1018320b8b0dSShuming Fan port = 4; 1019320b8b0dSShuming Fan else if (dai->id == RT711_AIF2) 1020320b8b0dSShuming Fan port = 2; 1021320b8b0dSShuming Fan else 1022320b8b0dSShuming Fan return -EINVAL; 1023320b8b0dSShuming Fan } 1024320b8b0dSShuming Fan 1025320b8b0dSShuming Fan stream_config.frame_rate = params_rate(params); 1026320b8b0dSShuming Fan stream_config.ch_count = params_channels(params); 1027320b8b0dSShuming Fan stream_config.bps = snd_pcm_format_width(params_format(params)); 1028320b8b0dSShuming Fan stream_config.direction = direction; 1029320b8b0dSShuming Fan 1030320b8b0dSShuming Fan num_channels = params_channels(params); 1031320b8b0dSShuming Fan port_config.ch_mask = (1 << (num_channels)) - 1; 1032320b8b0dSShuming Fan port_config.num = port; 1033320b8b0dSShuming Fan 1034320b8b0dSShuming Fan retval = sdw_stream_add_slave(rt711->slave, &stream_config, 1035320b8b0dSShuming Fan &port_config, 1, stream->sdw_stream); 1036320b8b0dSShuming Fan if (retval) { 1037320b8b0dSShuming Fan dev_err(dai->dev, "Unable to configure port\n"); 1038320b8b0dSShuming Fan return retval; 1039320b8b0dSShuming Fan } 1040320b8b0dSShuming Fan 1041320b8b0dSShuming Fan if (params_channels(params) <= 16) { 1042320b8b0dSShuming Fan /* bit 3:0 Number of Channel */ 1043320b8b0dSShuming Fan val |= (params_channels(params) - 1); 1044320b8b0dSShuming Fan } else { 1045320b8b0dSShuming Fan dev_err(component->dev, "Unsupported channels %d\n", 1046320b8b0dSShuming Fan params_channels(params)); 1047320b8b0dSShuming Fan return -EINVAL; 1048320b8b0dSShuming Fan } 1049320b8b0dSShuming Fan 1050320b8b0dSShuming Fan switch (params_width(params)) { 1051320b8b0dSShuming Fan /* bit 6:4 Bits per Sample */ 1052320b8b0dSShuming Fan case 8: 1053320b8b0dSShuming Fan break; 1054320b8b0dSShuming Fan case 16: 1055320b8b0dSShuming Fan val |= (0x1 << 4); 1056320b8b0dSShuming Fan break; 1057320b8b0dSShuming Fan case 20: 1058320b8b0dSShuming Fan val |= (0x2 << 4); 1059320b8b0dSShuming Fan break; 1060320b8b0dSShuming Fan case 24: 1061320b8b0dSShuming Fan val |= (0x3 << 4); 1062320b8b0dSShuming Fan break; 1063320b8b0dSShuming Fan case 32: 1064320b8b0dSShuming Fan val |= (0x4 << 4); 1065320b8b0dSShuming Fan break; 1066320b8b0dSShuming Fan default: 1067320b8b0dSShuming Fan return -EINVAL; 1068320b8b0dSShuming Fan } 1069320b8b0dSShuming Fan 1070320b8b0dSShuming Fan /* 48Khz */ 1071320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_DAC_FORMAT_H, val); 1072320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_ADC1_FORMAT_H, val); 1073320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_ADC2_FORMAT_H, val); 1074320b8b0dSShuming Fan 1075320b8b0dSShuming Fan return retval; 1076320b8b0dSShuming Fan } 1077320b8b0dSShuming Fan 1078320b8b0dSShuming Fan static int rt711_pcm_hw_free(struct snd_pcm_substream *substream, 1079320b8b0dSShuming Fan struct snd_soc_dai *dai) 1080320b8b0dSShuming Fan { 1081320b8b0dSShuming Fan struct snd_soc_component *component = dai->component; 1082320b8b0dSShuming Fan struct rt711_priv *rt711 = snd_soc_component_get_drvdata(component); 1083320b8b0dSShuming Fan struct sdw_stream_data *stream = 1084320b8b0dSShuming Fan snd_soc_dai_get_dma_data(dai, substream); 1085320b8b0dSShuming Fan 1086320b8b0dSShuming Fan if (!rt711->slave) 1087320b8b0dSShuming Fan return -EINVAL; 1088320b8b0dSShuming Fan 1089320b8b0dSShuming Fan sdw_stream_remove_slave(rt711->slave, stream->sdw_stream); 1090320b8b0dSShuming Fan return 0; 1091320b8b0dSShuming Fan } 1092320b8b0dSShuming Fan 1093320b8b0dSShuming Fan #define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000) 1094320b8b0dSShuming Fan #define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ 1095320b8b0dSShuming Fan SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8) 1096320b8b0dSShuming Fan 1097f9e56a34SRikard Falkeborn static const struct snd_soc_dai_ops rt711_ops = { 1098320b8b0dSShuming Fan .hw_params = rt711_pcm_hw_params, 1099320b8b0dSShuming Fan .hw_free = rt711_pcm_hw_free, 1100e8444560SPierre-Louis Bossart .set_stream = rt711_set_sdw_stream, 1101320b8b0dSShuming Fan .shutdown = rt711_shutdown, 1102320b8b0dSShuming Fan }; 1103320b8b0dSShuming Fan 1104320b8b0dSShuming Fan static struct snd_soc_dai_driver rt711_dai[] = { 1105320b8b0dSShuming Fan { 1106320b8b0dSShuming Fan .name = "rt711-aif1", 1107320b8b0dSShuming Fan .id = RT711_AIF1, 1108320b8b0dSShuming Fan .playback = { 1109320b8b0dSShuming Fan .stream_name = "DP3 Playback", 1110320b8b0dSShuming Fan .channels_min = 1, 1111320b8b0dSShuming Fan .channels_max = 2, 1112320b8b0dSShuming Fan .rates = RT711_STEREO_RATES, 1113320b8b0dSShuming Fan .formats = RT711_FORMATS, 1114320b8b0dSShuming Fan }, 1115320b8b0dSShuming Fan .capture = { 1116320b8b0dSShuming Fan .stream_name = "DP4 Capture", 1117320b8b0dSShuming Fan .channels_min = 1, 1118320b8b0dSShuming Fan .channels_max = 2, 1119320b8b0dSShuming Fan .rates = RT711_STEREO_RATES, 1120320b8b0dSShuming Fan .formats = RT711_FORMATS, 1121320b8b0dSShuming Fan }, 1122320b8b0dSShuming Fan .ops = &rt711_ops, 1123320b8b0dSShuming Fan }, 1124320b8b0dSShuming Fan { 1125320b8b0dSShuming Fan .name = "rt711-aif2", 1126320b8b0dSShuming Fan .id = RT711_AIF2, 1127320b8b0dSShuming Fan .capture = { 1128320b8b0dSShuming Fan .stream_name = "DP2 Capture", 1129320b8b0dSShuming Fan .channels_min = 1, 1130320b8b0dSShuming Fan .channels_max = 2, 1131320b8b0dSShuming Fan .rates = RT711_STEREO_RATES, 1132320b8b0dSShuming Fan .formats = RT711_FORMATS, 1133320b8b0dSShuming Fan }, 1134320b8b0dSShuming Fan .ops = &rt711_ops, 1135320b8b0dSShuming Fan } 1136320b8b0dSShuming Fan }; 1137320b8b0dSShuming Fan 1138320b8b0dSShuming Fan /* Bus clock frequency */ 1139320b8b0dSShuming Fan #define RT711_CLK_FREQ_9600000HZ 9600000 1140320b8b0dSShuming Fan #define RT711_CLK_FREQ_12000000HZ 12000000 1141320b8b0dSShuming Fan #define RT711_CLK_FREQ_6000000HZ 6000000 1142320b8b0dSShuming Fan #define RT711_CLK_FREQ_4800000HZ 4800000 1143320b8b0dSShuming Fan #define RT711_CLK_FREQ_2400000HZ 2400000 1144320b8b0dSShuming Fan #define RT711_CLK_FREQ_12288000HZ 12288000 1145320b8b0dSShuming Fan 1146320b8b0dSShuming Fan int rt711_clock_config(struct device *dev) 1147320b8b0dSShuming Fan { 1148320b8b0dSShuming Fan struct rt711_priv *rt711 = dev_get_drvdata(dev); 1149320b8b0dSShuming Fan unsigned int clk_freq, value; 1150320b8b0dSShuming Fan 1151320b8b0dSShuming Fan clk_freq = (rt711->params.curr_dr_freq >> 1); 1152320b8b0dSShuming Fan 1153320b8b0dSShuming Fan switch (clk_freq) { 1154320b8b0dSShuming Fan case RT711_CLK_FREQ_12000000HZ: 1155320b8b0dSShuming Fan value = 0x0; 1156320b8b0dSShuming Fan break; 1157320b8b0dSShuming Fan case RT711_CLK_FREQ_6000000HZ: 1158320b8b0dSShuming Fan value = 0x1; 1159320b8b0dSShuming Fan break; 1160320b8b0dSShuming Fan case RT711_CLK_FREQ_9600000HZ: 1161320b8b0dSShuming Fan value = 0x2; 1162320b8b0dSShuming Fan break; 1163320b8b0dSShuming Fan case RT711_CLK_FREQ_4800000HZ: 1164320b8b0dSShuming Fan value = 0x3; 1165320b8b0dSShuming Fan break; 1166320b8b0dSShuming Fan case RT711_CLK_FREQ_2400000HZ: 1167320b8b0dSShuming Fan value = 0x4; 1168320b8b0dSShuming Fan break; 1169320b8b0dSShuming Fan case RT711_CLK_FREQ_12288000HZ: 1170320b8b0dSShuming Fan value = 0x5; 1171320b8b0dSShuming Fan break; 1172320b8b0dSShuming Fan default: 1173320b8b0dSShuming Fan return -EINVAL; 1174320b8b0dSShuming Fan } 1175320b8b0dSShuming Fan 1176320b8b0dSShuming Fan regmap_write(rt711->regmap, 0xe0, value); 1177320b8b0dSShuming Fan regmap_write(rt711->regmap, 0xf0, value); 1178320b8b0dSShuming Fan 1179320b8b0dSShuming Fan dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq); 1180320b8b0dSShuming Fan 1181320b8b0dSShuming Fan return 0; 1182320b8b0dSShuming Fan } 1183320b8b0dSShuming Fan 1184320b8b0dSShuming Fan static void rt711_calibration_work(struct work_struct *work) 1185320b8b0dSShuming Fan { 1186320b8b0dSShuming Fan struct rt711_priv *rt711 = 1187320b8b0dSShuming Fan container_of(work, struct rt711_priv, calibration_work); 1188320b8b0dSShuming Fan 1189320b8b0dSShuming Fan rt711_calibration(rt711); 1190320b8b0dSShuming Fan } 1191320b8b0dSShuming Fan 1192320b8b0dSShuming Fan int rt711_init(struct device *dev, struct regmap *sdw_regmap, 1193320b8b0dSShuming Fan struct regmap *regmap, struct sdw_slave *slave) 1194320b8b0dSShuming Fan { 1195320b8b0dSShuming Fan struct rt711_priv *rt711; 1196320b8b0dSShuming Fan int ret; 1197320b8b0dSShuming Fan 1198320b8b0dSShuming Fan rt711 = devm_kzalloc(dev, sizeof(*rt711), GFP_KERNEL); 1199320b8b0dSShuming Fan if (!rt711) 1200320b8b0dSShuming Fan return -ENOMEM; 1201320b8b0dSShuming Fan 1202320b8b0dSShuming Fan dev_set_drvdata(dev, rt711); 1203320b8b0dSShuming Fan rt711->slave = slave; 1204320b8b0dSShuming Fan rt711->sdw_regmap = sdw_regmap; 1205320b8b0dSShuming Fan rt711->regmap = regmap; 1206320b8b0dSShuming Fan 120774d40901SPierre-Louis Bossart mutex_init(&rt711->calibrate_mutex); 120818236370SPierre-Louis Bossart mutex_init(&rt711->disable_irq_lock); 120918236370SPierre-Louis Bossart 1210*a49267a3SPierre-Louis Bossart INIT_DELAYED_WORK(&rt711->jack_detect_work, rt711_jack_detect_handler); 1211*a49267a3SPierre-Louis Bossart INIT_DELAYED_WORK(&rt711->jack_btn_check_work, rt711_btn_check_handler); 1212*a49267a3SPierre-Louis Bossart INIT_WORK(&rt711->calibration_work, rt711_calibration_work); 1213*a49267a3SPierre-Louis Bossart 1214320b8b0dSShuming Fan /* 1215320b8b0dSShuming Fan * Mark hw_init to false 1216320b8b0dSShuming Fan * HW init will be performed when device reports present 1217320b8b0dSShuming Fan */ 1218320b8b0dSShuming Fan rt711->hw_init = false; 1219320b8b0dSShuming Fan rt711->first_hw_init = false; 1220320b8b0dSShuming Fan 1221320b8b0dSShuming Fan /* JD source uses JD2 in default */ 1222320b8b0dSShuming Fan rt711->jd_src = RT711_JD2; 1223320b8b0dSShuming Fan 1224320b8b0dSShuming Fan ret = devm_snd_soc_register_component(dev, 1225320b8b0dSShuming Fan &soc_codec_dev_rt711, 1226320b8b0dSShuming Fan rt711_dai, 1227320b8b0dSShuming Fan ARRAY_SIZE(rt711_dai)); 1228320b8b0dSShuming Fan 1229320b8b0dSShuming Fan dev_dbg(&slave->dev, "%s\n", __func__); 1230320b8b0dSShuming Fan 1231320b8b0dSShuming Fan return ret; 1232320b8b0dSShuming Fan } 1233320b8b0dSShuming Fan 1234320b8b0dSShuming Fan int rt711_io_init(struct device *dev, struct sdw_slave *slave) 1235320b8b0dSShuming Fan { 1236320b8b0dSShuming Fan struct rt711_priv *rt711 = dev_get_drvdata(dev); 1237320b8b0dSShuming Fan 123818236370SPierre-Louis Bossart rt711->disable_irq = false; 123918236370SPierre-Louis Bossart 1240320b8b0dSShuming Fan if (rt711->hw_init) 1241320b8b0dSShuming Fan return 0; 1242320b8b0dSShuming Fan 1243320b8b0dSShuming Fan if (rt711->first_hw_init) { 1244320b8b0dSShuming Fan regcache_cache_only(rt711->regmap, false); 1245320b8b0dSShuming Fan regcache_cache_bypass(rt711->regmap, true); 1246320b8b0dSShuming Fan } 1247320b8b0dSShuming Fan 1248320b8b0dSShuming Fan /* 1249320b8b0dSShuming Fan * PM runtime is only enabled when a Slave reports as Attached 1250320b8b0dSShuming Fan */ 1251320b8b0dSShuming Fan if (!rt711->first_hw_init) { 1252320b8b0dSShuming Fan /* set autosuspend parameters */ 1253320b8b0dSShuming Fan pm_runtime_set_autosuspend_delay(&slave->dev, 3000); 1254320b8b0dSShuming Fan pm_runtime_use_autosuspend(&slave->dev); 1255320b8b0dSShuming Fan 1256320b8b0dSShuming Fan /* update count of parent 'active' children */ 1257320b8b0dSShuming Fan pm_runtime_set_active(&slave->dev); 1258320b8b0dSShuming Fan 1259320b8b0dSShuming Fan /* make sure the device does not suspend immediately */ 1260320b8b0dSShuming Fan pm_runtime_mark_last_busy(&slave->dev); 1261320b8b0dSShuming Fan 1262320b8b0dSShuming Fan pm_runtime_enable(&slave->dev); 1263320b8b0dSShuming Fan } 1264320b8b0dSShuming Fan 1265320b8b0dSShuming Fan pm_runtime_get_noresume(&slave->dev); 1266320b8b0dSShuming Fan 1267320b8b0dSShuming Fan rt711_reset(rt711->regmap); 1268320b8b0dSShuming Fan 1269320b8b0dSShuming Fan /* power on */ 1270320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D0); 1271320b8b0dSShuming Fan 1272320b8b0dSShuming Fan /* Set Pin Widget */ 1273320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_PIN_MIC2, 0x25); 1274320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_PIN_HP, 0xc0); 1275320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_PIN_DMIC1, 0x20); 1276320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_PIN_DMIC2, 0x20); 1277320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_PIN_LINE1, 0x20); 1278320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_PIN_LINE2, 0x20); 1279320b8b0dSShuming Fan 1280320b8b0dSShuming Fan /* Mute HP/ADC1/ADC2 */ 1281320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0xa080); 1282320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_GAIN_HP_H, 0x9080); 1283320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x6080); 1284320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_GAIN_ADC2_H, 0x5080); 1285320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x6080); 1286320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_GAIN_ADC1_H, 0x5080); 1287320b8b0dSShuming Fan 1288320b8b0dSShuming Fan /* Set Configuration Default */ 1289320b8b0dSShuming Fan regmap_write(rt711->regmap, 0x4f12, 0x91); 1290320b8b0dSShuming Fan regmap_write(rt711->regmap, 0x4e12, 0xd6); 1291320b8b0dSShuming Fan regmap_write(rt711->regmap, 0x4d12, 0x11); 1292320b8b0dSShuming Fan regmap_write(rt711->regmap, 0x4c12, 0x20); 1293320b8b0dSShuming Fan regmap_write(rt711->regmap, 0x4f13, 0x91); 1294320b8b0dSShuming Fan regmap_write(rt711->regmap, 0x4e13, 0xd6); 1295320b8b0dSShuming Fan regmap_write(rt711->regmap, 0x4d13, 0x11); 1296320b8b0dSShuming Fan regmap_write(rt711->regmap, 0x4c13, 0x21); 1297320b8b0dSShuming Fan regmap_write(rt711->regmap, 0x4c21, 0xf0); 1298320b8b0dSShuming Fan regmap_write(rt711->regmap, 0x4d21, 0x11); 1299320b8b0dSShuming Fan regmap_write(rt711->regmap, 0x4e21, 0x11); 1300320b8b0dSShuming Fan regmap_write(rt711->regmap, 0x4f21, 0x01); 1301320b8b0dSShuming Fan 1302320b8b0dSShuming Fan /* Data port arrangement */ 1303320b8b0dSShuming Fan rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 1304320b8b0dSShuming Fan RT711_TX_RX_MUX_CTL, 0x0154); 1305320b8b0dSShuming Fan 1306320b8b0dSShuming Fan /* Set index */ 1307320b8b0dSShuming Fan rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 1308320b8b0dSShuming Fan RT711_DIGITAL_MISC_CTRL4, 0x201b); 1309320b8b0dSShuming Fan rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 1310320b8b0dSShuming Fan RT711_COMBO_JACK_AUTO_CTL1, 0x5089); 1311320b8b0dSShuming Fan rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 1312320b8b0dSShuming Fan RT711_VREFOUT_CTL, 0x5064); 1313320b8b0dSShuming Fan rt711_index_write(rt711->regmap, RT711_VENDOR_REG, 1314320b8b0dSShuming Fan RT711_INLINE_CMD_CTL, 0xd249); 1315320b8b0dSShuming Fan 1316320b8b0dSShuming Fan /* Finish Initial Settings, set power to D3 */ 1317320b8b0dSShuming Fan regmap_write(rt711->regmap, RT711_SET_AUDIO_POWER_STATE, AC_PWRST_D3); 1318320b8b0dSShuming Fan 1319320b8b0dSShuming Fan if (rt711->first_hw_init) 1320320b8b0dSShuming Fan rt711_calibration(rt711); 1321*a49267a3SPierre-Louis Bossart else 1322320b8b0dSShuming Fan schedule_work(&rt711->calibration_work); 1323320b8b0dSShuming Fan 1324320b8b0dSShuming Fan /* 1325320b8b0dSShuming Fan * if set_jack callback occurred early than io_init, 1326320b8b0dSShuming Fan * we set up the jack detection function now 1327320b8b0dSShuming Fan */ 1328320b8b0dSShuming Fan if (rt711->hs_jack) 1329320b8b0dSShuming Fan rt711_jack_init(rt711); 1330320b8b0dSShuming Fan 1331320b8b0dSShuming Fan if (rt711->first_hw_init) { 1332320b8b0dSShuming Fan regcache_cache_bypass(rt711->regmap, false); 1333320b8b0dSShuming Fan regcache_mark_dirty(rt711->regmap); 1334320b8b0dSShuming Fan } else 1335320b8b0dSShuming Fan rt711->first_hw_init = true; 1336320b8b0dSShuming Fan 1337320b8b0dSShuming Fan /* Mark Slave initialization complete */ 1338320b8b0dSShuming Fan rt711->hw_init = true; 1339320b8b0dSShuming Fan 1340320b8b0dSShuming Fan pm_runtime_mark_last_busy(&slave->dev); 1341320b8b0dSShuming Fan pm_runtime_put_autosuspend(&slave->dev); 1342320b8b0dSShuming Fan 1343320b8b0dSShuming Fan dev_dbg(&slave->dev, "%s hw_init complete\n", __func__); 1344320b8b0dSShuming Fan return 0; 1345320b8b0dSShuming Fan } 1346320b8b0dSShuming Fan 1347320b8b0dSShuming Fan MODULE_DESCRIPTION("ASoC RT711 SDW driver"); 1348320b8b0dSShuming Fan MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>"); 1349320b8b0dSShuming Fan MODULE_LICENSE("GPL"); 1350