Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ASoC: rt711-sdca: Add RT711 SDCA vendor-specific driver

This is the initial codec driver for rt711 SDCA version.

Signed-off-by: Shuming Fan <shumingf@realtek.com>
Link: https://lore.kernel.org/r/20210302091506.18745-1-shumingf@realtek.com
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Shuming Fan and committed by
Mark Brown
7ad4d237 3f004d2d

+2263
+7
sound/soc/codecs/Kconfig
··· 180 180 imply SND_SOC_RT5682_SDW 181 181 imply SND_SOC_RT700_SDW 182 182 imply SND_SOC_RT711_SDW 183 + imply SND_SOC_RT711_SDCA_SDW 183 184 imply SND_SOC_RT715_SDW 184 185 imply SND_SOC_RT715_SDCA_SDW 185 186 imply SND_SOC_RT1308_SDW ··· 1248 1247 depends on SOUNDWIRE 1249 1248 select SND_SOC_RT711 1250 1249 select REGMAP_SOUNDWIRE 1250 + 1251 + config SND_SOC_RT711_SDCA_SDW 1252 + tristate "Realtek RT711 SDCA Codec - SDW" 1253 + depends on SOUNDWIRE 1254 + select REGMAP_SOUNDWIRE 1255 + select REGMAP_SOUNDWIRE_MBQ 1251 1256 1252 1257 config SND_SOC_RT715 1253 1258 tristate
+2
sound/soc/codecs/Makefile
··· 197 197 snd-soc-rt5682-i2c-objs := rt5682-i2c.o 198 198 snd-soc-rt700-objs := rt700.o rt700-sdw.o 199 199 snd-soc-rt711-objs := rt711.o rt711-sdw.o 200 + snd-soc-rt711-sdca-objs := rt711-sdca.o rt711-sdca-sdw.o 200 201 snd-soc-rt715-objs := rt715.o rt715-sdw.o 201 202 snd-soc-rt715-sdca-objs := rt715-sdca.o rt715-sdca-sdw.o 202 203 snd-soc-sgtl5000-objs := sgtl5000.o ··· 515 514 obj-$(CONFIG_SND_SOC_RT5682_SDW) += snd-soc-rt5682-sdw.o 516 515 obj-$(CONFIG_SND_SOC_RT700) += snd-soc-rt700.o 517 516 obj-$(CONFIG_SND_SOC_RT711) += snd-soc-rt711.o 517 + obj-$(CONFIG_SND_SOC_RT711_SDCA_SDW) += snd-soc-rt711-sdca.o 518 518 obj-$(CONFIG_SND_SOC_RT715) += snd-soc-rt715.o 519 519 obj-$(CONFIG_SND_SOC_RT715_SDCA_SDW) += snd-soc-rt715-sdca.o 520 520 obj-$(CONFIG_SND_SOC_SGTL5000) += snd-soc-sgtl5000.o
+425
sound/soc/codecs/rt711-sdca-sdw.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + // 3 + // rt711-sdw-sdca.c -- rt711 SDCA ALSA SoC audio driver 4 + // 5 + // Copyright(c) 2021 Realtek Semiconductor Corp. 6 + // 7 + // 8 + 9 + #include <linux/delay.h> 10 + #include <linux/device.h> 11 + #include <linux/mod_devicetable.h> 12 + #include <linux/soundwire/sdw_registers.h> 13 + #include <linux/module.h> 14 + 15 + #include "rt711-sdca.h" 16 + #include "rt711-sdca-sdw.h" 17 + 18 + static bool rt711_sdca_readable_register(struct device *dev, unsigned int reg) 19 + { 20 + switch (reg) { 21 + case 0x201a ... 0x2027: 22 + case 0x2029 ... 0x202a: 23 + case 0x202d ... 0x2034: 24 + case 0x2200 ... 0x2204: 25 + case 0x2206 ... 0x2212: 26 + case 0x2220 ... 0x2223: 27 + case 0x2230 ... 0x2239: 28 + case 0x2f01 ... 0x2f0f: 29 + case 0x2f30 ... 0x2f36: 30 + case 0x2f50 ... 0x2f5a: 31 + case 0x2f60: 32 + case 0x3200 ... 0x3212: 33 + case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_SELECTED_MODE, 0): 34 + case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_DETECTED_MODE, 0): 35 + case SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_CURRENT_OWNER, 0) ... 36 + SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_MESSAGE_LENGTH, 0): 37 + case RT711_BUF_ADDR_HID1 ... RT711_BUF_ADDR_HID2: 38 + return true; 39 + default: 40 + return false; 41 + } 42 + } 43 + 44 + static bool rt711_sdca_volatile_register(struct device *dev, unsigned int reg) 45 + { 46 + switch (reg) { 47 + case 0x201b: 48 + case 0x201c: 49 + case 0x201d: 50 + case 0x201f: 51 + case 0x2021: 52 + case 0x2023: 53 + case 0x2230: 54 + case 0x202d ... 0x202f: /* BRA */ 55 + case 0x2200 ... 0x2212: /* i2c debug */ 56 + case RT711_RC_CAL_STATUS: 57 + case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_DETECTED_MODE, 0): 58 + case SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_CURRENT_OWNER, 0) ... 59 + SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_MESSAGE_LENGTH, 0): 60 + case RT711_BUF_ADDR_HID1 ... RT711_BUF_ADDR_HID2: 61 + return true; 62 + default: 63 + return false; 64 + } 65 + } 66 + 67 + static bool rt711_sdca_mbq_readable_register(struct device *dev, unsigned int reg) 68 + { 69 + switch (reg) { 70 + case 0x2000000 ... 0x20000ff: 71 + case 0x5600000 ... 0x56000ff: 72 + case 0x5700000 ... 0x57000ff: 73 + case 0x5800000 ... 0x58000ff: 74 + case 0x5900000 ... 0x59000ff: 75 + case 0x5b00000 ... 0x5b000ff: 76 + case 0x5f00000 ... 0x5f000ff: 77 + case 0x6100000 ... 0x61000ff: 78 + return true; 79 + default: 80 + return false; 81 + } 82 + } 83 + 84 + static bool rt711_sdca_mbq_volatile_register(struct device *dev, unsigned int reg) 85 + { 86 + switch (reg) { 87 + case 0x2000000: 88 + case 0x200001a: 89 + case 0x2000046: 90 + case 0x2000080: 91 + case 0x2000081: 92 + case 0x2000083: 93 + case 0x5800000: 94 + case 0x5800001: 95 + case 0x5f00001: 96 + case 0x6100008: 97 + return true; 98 + default: 99 + return false; 100 + } 101 + } 102 + 103 + static const struct regmap_config rt711_sdca_regmap = { 104 + .reg_bits = 32, 105 + .val_bits = 8, 106 + .readable_reg = rt711_sdca_readable_register, 107 + .volatile_reg = rt711_sdca_volatile_register, 108 + .max_register = 0x44ffffff, 109 + .reg_defaults = rt711_sdca_reg_defaults, 110 + .num_reg_defaults = ARRAY_SIZE(rt711_sdca_reg_defaults), 111 + .cache_type = REGCACHE_RBTREE, 112 + .use_single_read = true, 113 + .use_single_write = true, 114 + }; 115 + 116 + static const struct regmap_config rt711_sdca_mbq_regmap = { 117 + .name = "sdw-mbq", 118 + .reg_bits = 32, 119 + .val_bits = 16, 120 + .readable_reg = rt711_sdca_mbq_readable_register, 121 + .volatile_reg = rt711_sdca_mbq_volatile_register, 122 + .max_register = 0x40800f12, 123 + .reg_defaults = rt711_sdca_mbq_defaults, 124 + .num_reg_defaults = ARRAY_SIZE(rt711_sdca_mbq_defaults), 125 + .cache_type = REGCACHE_RBTREE, 126 + .use_single_read = true, 127 + .use_single_write = true, 128 + }; 129 + 130 + static int rt711_sdca_update_status(struct sdw_slave *slave, 131 + enum sdw_slave_status status) 132 + { 133 + struct rt711_sdca_priv *rt711 = dev_get_drvdata(&slave->dev); 134 + 135 + /* Update the status */ 136 + rt711->status = status; 137 + 138 + if (status == SDW_SLAVE_UNATTACHED) 139 + rt711->hw_init = false; 140 + 141 + if (status == SDW_SLAVE_ATTACHED) { 142 + if (rt711->hs_jack) { 143 + /* 144 + * Due to the SCP_SDCA_INTMASK will be cleared by any reset, and then 145 + * if the device attached again, we will need to set the setting back. 146 + * It could avoid losing the jack detection interrupt. 147 + * This also could sync with the cache value as the rt711_sdca_jack_init set. 148 + */ 149 + sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK1, 150 + SDW_SCP_SDCA_INTMASK_SDCA_0); 151 + sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK2, 152 + SDW_SCP_SDCA_INTMASK_SDCA_8); 153 + } 154 + } 155 + 156 + /* 157 + * Perform initialization only if slave status is present and 158 + * hw_init flag is false 159 + */ 160 + if (rt711->hw_init || rt711->status != SDW_SLAVE_ATTACHED) 161 + return 0; 162 + 163 + /* perform I/O transfers required for Slave initialization */ 164 + return rt711_sdca_io_init(&slave->dev, slave); 165 + } 166 + 167 + static int rt711_sdca_read_prop(struct sdw_slave *slave) 168 + { 169 + struct sdw_slave_prop *prop = &slave->prop; 170 + int nval; 171 + int i, j; 172 + u32 bit; 173 + unsigned long addr; 174 + struct sdw_dpn_prop *dpn; 175 + 176 + prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY; 177 + prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY; 178 + prop->is_sdca = true; 179 + 180 + prop->paging_support = true; 181 + 182 + /* first we need to allocate memory for set bits in port lists */ 183 + prop->source_ports = 0x14; /* BITMAP: 00010100 */ 184 + prop->sink_ports = 0x8; /* BITMAP: 00001000 */ 185 + 186 + nval = hweight32(prop->source_ports); 187 + prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval, 188 + sizeof(*prop->src_dpn_prop), GFP_KERNEL); 189 + if (!prop->src_dpn_prop) 190 + return -ENOMEM; 191 + 192 + i = 0; 193 + dpn = prop->src_dpn_prop; 194 + addr = prop->source_ports; 195 + for_each_set_bit(bit, &addr, 32) { 196 + dpn[i].num = bit; 197 + dpn[i].type = SDW_DPN_FULL; 198 + dpn[i].simple_ch_prep_sm = true; 199 + dpn[i].ch_prep_timeout = 10; 200 + i++; 201 + } 202 + 203 + /* do this again for sink now */ 204 + nval = hweight32(prop->sink_ports); 205 + prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval, 206 + sizeof(*prop->sink_dpn_prop), GFP_KERNEL); 207 + if (!prop->sink_dpn_prop) 208 + return -ENOMEM; 209 + 210 + j = 0; 211 + dpn = prop->sink_dpn_prop; 212 + addr = prop->sink_ports; 213 + for_each_set_bit(bit, &addr, 32) { 214 + dpn[j].num = bit; 215 + dpn[j].type = SDW_DPN_FULL; 216 + dpn[j].simple_ch_prep_sm = true; 217 + dpn[j].ch_prep_timeout = 10; 218 + j++; 219 + } 220 + 221 + /* set the timeout values */ 222 + prop->clk_stop_timeout = 20; 223 + 224 + /* wake-up event */ 225 + prop->wake_capable = 1; 226 + 227 + return 0; 228 + } 229 + 230 + static int rt711_sdca_interrupt_callback(struct sdw_slave *slave, 231 + struct sdw_slave_intr_status *status) 232 + { 233 + struct rt711_sdca_priv *rt711 = dev_get_drvdata(&slave->dev); 234 + int ret, stat; 235 + int count = 0, retry = 3; 236 + unsigned int sdca_cascade, scp_sdca_stat1, scp_sdca_stat2 = 0; 237 + 238 + dev_dbg(&slave->dev, 239 + "%s control_port_stat=%x, sdca_cascade=%x", __func__, 240 + status->control_port, status->sdca_cascade); 241 + 242 + if (cancel_delayed_work_sync(&rt711->jack_detect_work)) { 243 + dev_warn(&slave->dev, "%s the pending delayed_work was cancelled", __func__); 244 + /* avoid the HID owner doesn't change to device */ 245 + if (rt711->scp_sdca_stat2) 246 + scp_sdca_stat2 = rt711->scp_sdca_stat2; 247 + } 248 + 249 + ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT1); 250 + if (ret < 0) 251 + goto io_error; 252 + rt711->scp_sdca_stat1 = ret; 253 + ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT2); 254 + if (ret < 0) 255 + goto io_error; 256 + rt711->scp_sdca_stat2 = ret; 257 + if (scp_sdca_stat2) 258 + rt711->scp_sdca_stat2 |= scp_sdca_stat2; 259 + 260 + do { 261 + /* clear flag */ 262 + ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT1); 263 + if (ret < 0) 264 + goto io_error; 265 + if (ret & SDW_SCP_SDCA_INTMASK_SDCA_0) { 266 + ret = sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INT1, 267 + SDW_SCP_SDCA_INTMASK_SDCA_0); 268 + if (ret < 0) 269 + goto io_error; 270 + } 271 + ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT2); 272 + if (ret < 0) 273 + goto io_error; 274 + if (ret & SDW_SCP_SDCA_INTMASK_SDCA_8) { 275 + ret = sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INT2, 276 + SDW_SCP_SDCA_INTMASK_SDCA_8); 277 + if (ret < 0) 278 + goto io_error; 279 + } 280 + 281 + /* check if flag clear or not */ 282 + ret = sdw_read_no_pm(rt711->slave, SDW_DP0_INT); 283 + if (ret < 0) 284 + goto io_error; 285 + sdca_cascade = ret & SDW_DP0_SDCA_CASCADE; 286 + 287 + ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT1); 288 + if (ret < 0) 289 + goto io_error; 290 + scp_sdca_stat1 = ret & SDW_SCP_SDCA_INTMASK_SDCA_0; 291 + 292 + ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT2); 293 + if (ret < 0) 294 + goto io_error; 295 + scp_sdca_stat2 = ret & SDW_SCP_SDCA_INTMASK_SDCA_8; 296 + 297 + stat = scp_sdca_stat1 || scp_sdca_stat2 || sdca_cascade; 298 + 299 + count++; 300 + } while (stat != 0 && count < retry); 301 + 302 + if (stat) 303 + dev_warn(&slave->dev, 304 + "%s scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__, 305 + rt711->scp_sdca_stat1, rt711->scp_sdca_stat2); 306 + 307 + if (status->sdca_cascade) 308 + mod_delayed_work(system_power_efficient_wq, 309 + &rt711->jack_detect_work, msecs_to_jiffies(30)); 310 + 311 + return 0; 312 + 313 + io_error: 314 + pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); 315 + return ret; 316 + } 317 + 318 + static struct sdw_slave_ops rt711_sdca_slave_ops = { 319 + .read_prop = rt711_sdca_read_prop, 320 + .interrupt_callback = rt711_sdca_interrupt_callback, 321 + .update_status = rt711_sdca_update_status, 322 + }; 323 + 324 + static int rt711_sdca_sdw_probe(struct sdw_slave *slave, 325 + const struct sdw_device_id *id) 326 + { 327 + struct regmap *regmap, *mbq_regmap; 328 + 329 + /* Regmap Initialization */ 330 + mbq_regmap = devm_regmap_init_sdw_mbq(slave, &rt711_sdca_mbq_regmap); 331 + if (IS_ERR(mbq_regmap)) 332 + return PTR_ERR(mbq_regmap); 333 + 334 + regmap = devm_regmap_init_sdw(slave, &rt711_sdca_regmap); 335 + if (IS_ERR(regmap)) 336 + return PTR_ERR(regmap); 337 + 338 + return rt711_sdca_init(&slave->dev, regmap, mbq_regmap, slave); 339 + } 340 + 341 + static int rt711_sdca_sdw_remove(struct sdw_slave *slave) 342 + { 343 + struct rt711_sdca_priv *rt711 = dev_get_drvdata(&slave->dev); 344 + 345 + if (rt711 && rt711->hw_init) { 346 + cancel_delayed_work_sync(&rt711->jack_detect_work); 347 + cancel_delayed_work_sync(&rt711->jack_btn_check_work); 348 + } 349 + 350 + return 0; 351 + } 352 + 353 + static const struct sdw_device_id rt711_sdca_id[] = { 354 + SDW_SLAVE_ENTRY_EXT(0x025d, 0x711, 0x3, 0x1, 0), 355 + {}, 356 + }; 357 + MODULE_DEVICE_TABLE(sdw, rt711_sdca_id); 358 + 359 + static int __maybe_unused rt711_sdca_dev_suspend(struct device *dev) 360 + { 361 + struct rt711_sdca_priv *rt711 = dev_get_drvdata(dev); 362 + 363 + if (!rt711->hw_init) 364 + return 0; 365 + 366 + cancel_delayed_work_sync(&rt711->jack_detect_work); 367 + cancel_delayed_work_sync(&rt711->jack_btn_check_work); 368 + 369 + regcache_cache_only(rt711->regmap, true); 370 + regcache_cache_only(rt711->mbq_regmap, true); 371 + 372 + return 0; 373 + } 374 + 375 + #define RT711_PROBE_TIMEOUT 5000 376 + 377 + static int __maybe_unused rt711_sdca_dev_resume(struct device *dev) 378 + { 379 + struct sdw_slave *slave = dev_to_sdw_dev(dev); 380 + struct rt711_sdca_priv *rt711 = dev_get_drvdata(dev); 381 + unsigned long time; 382 + 383 + if (!rt711->hw_init) 384 + return 0; 385 + 386 + if (!slave->unattach_request) 387 + goto regmap_sync; 388 + 389 + time = wait_for_completion_timeout(&slave->initialization_complete, 390 + msecs_to_jiffies(RT711_PROBE_TIMEOUT)); 391 + if (!time) { 392 + dev_err(&slave->dev, "Initialization not complete, timed out\n"); 393 + return -ETIMEDOUT; 394 + } 395 + 396 + regmap_sync: 397 + slave->unattach_request = 0; 398 + regcache_cache_only(rt711->regmap, false); 399 + regcache_sync(rt711->regmap); 400 + regcache_cache_only(rt711->mbq_regmap, false); 401 + regcache_sync(rt711->mbq_regmap); 402 + return 0; 403 + } 404 + 405 + static const struct dev_pm_ops rt711_sdca_pm = { 406 + SET_SYSTEM_SLEEP_PM_OPS(rt711_sdca_dev_suspend, rt711_sdca_dev_resume) 407 + SET_RUNTIME_PM_OPS(rt711_sdca_dev_suspend, rt711_sdca_dev_resume, NULL) 408 + }; 409 + 410 + static struct sdw_driver rt711_sdca_sdw_driver = { 411 + .driver = { 412 + .name = "rt711-sdca", 413 + .owner = THIS_MODULE, 414 + .pm = &rt711_sdca_pm, 415 + }, 416 + .probe = rt711_sdca_sdw_probe, 417 + .remove = rt711_sdca_sdw_remove, 418 + .ops = &rt711_sdca_slave_ops, 419 + .id_table = rt711_sdca_id, 420 + }; 421 + module_sdw_driver(rt711_sdca_sdw_driver); 422 + 423 + MODULE_DESCRIPTION("ASoC RT711 SDCA SDW driver"); 424 + MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>"); 425 + MODULE_LICENSE("GPL");
+99
sound/soc/codecs/rt711-sdca-sdw.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + /* 3 + * rt711-sdw-sdca.h -- RT711 SDCA ALSA SoC audio driver header 4 + * 5 + * Copyright(c) 2021 Realtek Semiconductor Corp. 6 + */ 7 + 8 + #ifndef __RT711_SDW_SDCA_H__ 9 + #define __RT711_SDW_SDCA_H__ 10 + 11 + #include <linux/regmap.h> 12 + #include <linux/soundwire/sdw_registers.h> 13 + 14 + static const struct reg_default rt711_sdca_reg_defaults[] = { 15 + { 0x201a, 0x00 }, 16 + { 0x201e, 0x00 }, 17 + { 0x201f, 0x00 }, 18 + { 0x2020, 0x00 }, 19 + { 0x2021, 0x00 }, 20 + { 0x2022, 0x00 }, 21 + { 0x2023, 0x00 }, 22 + { 0x2024, 0x00 }, 23 + { 0x2025, 0x01 }, 24 + { 0x2026, 0x00 }, 25 + { 0x2027, 0x00 }, 26 + { 0x2029, 0x00 }, 27 + { 0x202a, 0x00 }, 28 + { 0x202d, 0x00 }, 29 + { 0x202e, 0x00 }, 30 + { 0x202f, 0x00 }, 31 + { 0x2030, 0x00 }, 32 + { 0x2031, 0x00 }, 33 + { 0x2032, 0x00 }, 34 + { 0x2033, 0x00 }, 35 + { 0x2230, 0x00 }, 36 + { 0x2231, 0x2f }, 37 + { 0x2232, 0x80 }, 38 + { 0x2233, 0x00 }, 39 + { 0x2234, 0x00 }, 40 + { 0x2235, 0x00 }, 41 + { 0x2236, 0x00 }, 42 + { 0x2237, 0x00 }, 43 + { 0x2238, 0x00 }, 44 + { 0x2239, 0x00 }, 45 + { 0x2f01, 0x00 }, 46 + { 0x2f02, 0x09 }, 47 + { 0x2f03, 0x00 }, 48 + { 0x2f04, 0x00 }, 49 + { 0x2f05, 0x0b }, 50 + { 0x2f06, 0x01 }, 51 + { 0x2f08, 0x00 }, 52 + { 0x2f09, 0x00 }, 53 + { 0x2f0a, 0x00 }, 54 + { 0x2f0b, 0x00 }, 55 + { 0x2f0c, 0x00 }, 56 + { 0x2f0d, 0x00 }, 57 + { 0x2f0e, 0x14 }, 58 + { 0x2f0f, 0x00 }, 59 + { 0x2f50, 0x03 }, 60 + { 0x2f5a, 0x00 }, 61 + { SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_CS01, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0), 0x09 }, 62 + { SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_MUTE, CH_L), 0x01 }, 63 + { SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_MUTE, CH_R), 0x01 }, 64 + { SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_MUTE, CH_L), 0x01 }, 65 + { SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_MUTE, CH_R), 0x01 }, 66 + { SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE28, RT711_SDCA_CTL_REQ_POWER_STATE, 0), 0x03 }, 67 + { SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_MUTE, CH_L), 0x01 }, 68 + { SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_MUTE, CH_R), 0x01 }, 69 + }; 70 + 71 + static const struct reg_default rt711_sdca_mbq_defaults[] = { 72 + { 0x2000009, 0x1029 }, 73 + { 0x2000011, 0x007a }, 74 + { 0x200001a, 0x8003 }, 75 + { 0x2000045, 0x5289 }, 76 + { 0x2000048, 0x8049 }, 77 + { 0x200004a, 0xa83b }, 78 + { 0x200006b, 0x5064 }, 79 + { 0x200006f, 0x058b }, 80 + { 0x5800000, 0x0008 }, 81 + { 0x5800001, 0x0000 }, 82 + { 0x5f00001, 0x000a }, 83 + { 0x6100000, 0x6100 }, 84 + { 0x6100035, 0x0060 }, 85 + { 0x6100036, 0x0029 }, 86 + { 0x610003f, 0xff12 }, 87 + { SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_VOLUME, CH_L), 0x00 }, 88 + { SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_VOLUME, CH_R), 0x00 }, 89 + { SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_VOLUME, CH_L), 0x00 }, 90 + { SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_VOLUME, CH_R), 0x00 }, 91 + { SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_VOLUME, CH_L), 0x00 }, 92 + { SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_VOLUME, CH_R), 0x00 }, 93 + { SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PLATFORM_FU44, RT711_SDCA_CTL_FU_CH_GAIN, CH_L), 0x00 }, 94 + { SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PLATFORM_FU44, RT711_SDCA_CTL_FU_CH_GAIN, CH_R), 0x00 }, 95 + { SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PLATFORM_FU15, RT711_SDCA_CTL_FU_CH_GAIN, CH_L), 0x00 }, 96 + { SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PLATFORM_FU15, RT711_SDCA_CTL_FU_CH_GAIN, CH_R), 0x00 }, 97 + }; 98 + 99 + #endif /* __RT711_SDW_SDCA_H__ */
+1492
sound/soc/codecs/rt711-sdca.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + // 3 + // rt711-sdca.c -- rt711 SDCA ALSA SoC audio driver 4 + // 5 + // Copyright(c) 2021 Realtek Semiconductor Corp. 6 + // 7 + // 8 + 9 + #include <linux/module.h> 10 + #include <linux/moduleparam.h> 11 + #include <linux/kernel.h> 12 + #include <linux/init.h> 13 + #include <linux/delay.h> 14 + #include <linux/pm_runtime.h> 15 + #include <linux/soundwire/sdw_registers.h> 16 + #include <linux/slab.h> 17 + #include <linux/bitops.h> 18 + #include <sound/core.h> 19 + #include <sound/pcm.h> 20 + #include <sound/pcm_params.h> 21 + #include <sound/soc-dapm.h> 22 + #include <sound/initval.h> 23 + #include <sound/tlv.h> 24 + #include <sound/jack.h> 25 + 26 + #include "rt711-sdca.h" 27 + 28 + static int rt711_sdca_index_write(struct rt711_sdca_priv *rt711, 29 + unsigned int nid, unsigned int reg, unsigned int value) 30 + { 31 + int ret; 32 + struct regmap *regmap = rt711->mbq_regmap; 33 + unsigned int addr = (nid << 20) | reg; 34 + 35 + ret = regmap_write(regmap, addr, value); 36 + if (ret < 0) 37 + dev_err(rt711->component->dev, 38 + "Failed to set private value: %06x <= %04x ret=%d\n", 39 + addr, value, ret); 40 + 41 + return ret; 42 + } 43 + 44 + static int rt711_sdca_index_read(struct rt711_sdca_priv *rt711, 45 + unsigned int nid, unsigned int reg, unsigned int *value) 46 + { 47 + int ret; 48 + struct regmap *regmap = rt711->mbq_regmap; 49 + unsigned int addr = (nid << 20) | reg; 50 + 51 + ret = regmap_read(regmap, addr, value); 52 + if (ret < 0) 53 + dev_err(rt711->component->dev, 54 + "Failed to get private value: %06x => %04x ret=%d\n", 55 + addr, *value, ret); 56 + 57 + return ret; 58 + } 59 + 60 + static int rt711_sdca_index_update_bits(struct rt711_sdca_priv *rt711, 61 + unsigned int nid, unsigned int reg, unsigned int mask, unsigned int val) 62 + { 63 + unsigned int tmp; 64 + int ret; 65 + 66 + ret = rt711_sdca_index_read(rt711, nid, reg, &tmp); 67 + if (ret < 0) 68 + return ret; 69 + 70 + set_mask_bits(&tmp, mask, val); 71 + return rt711_sdca_index_write(rt711, nid, reg, tmp); 72 + } 73 + 74 + static void rt711_sdca_reset(struct rt711_sdca_priv *rt711) 75 + { 76 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 77 + RT711_PARA_VERB_CTL, RT711_HIDDEN_REG_SW_RESET, 78 + RT711_HIDDEN_REG_SW_RESET); 79 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 80 + RT711_HDA_LEGACY_RESET_CTL, 0x1, 0x1); 81 + } 82 + 83 + static int rt711_sdca_calibration(struct rt711_sdca_priv *rt711) 84 + { 85 + unsigned int val, loop_rc = 0, loop_dc = 0; 86 + struct device *dev; 87 + struct regmap *regmap = rt711->regmap; 88 + int chk_cnt = 100; 89 + int ret = 0; 90 + 91 + mutex_lock(&rt711->calibrate_mutex); 92 + dev = regmap_get_device(regmap); 93 + 94 + regmap_read(rt711->regmap, RT711_RC_CAL_STATUS, &val); 95 + /* RC calibration */ 96 + if (!(val & 0x40)) 97 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_ANALOG_CTL, 98 + RT711_MISC_POWER_CTL0, 0x0010, 0x0010); 99 + 100 + for (loop_rc = 0; loop_rc < chk_cnt && !(val & 0x40); loop_rc++) { 101 + usleep_range(10000, 11000); 102 + ret = regmap_read(rt711->regmap, RT711_RC_CAL_STATUS, &val); 103 + if (ret < 0) 104 + goto _cali_fail_; 105 + } 106 + if (loop_rc == chk_cnt) 107 + dev_err(dev, "%s, RC calibration time-out!\n", __func__); 108 + 109 + /* HP calibration by manual mode setting */ 110 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 111 + RT711_FSM_CTL, 0x2000, 0x2000); 112 + 113 + /* Calibration manual mode */ 114 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 115 + RT711_FSM_CTL, 0xf, RT711_CALI_CTL); 116 + 117 + /* reset HP calibration */ 118 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_CALI, 119 + RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_FORCE_CALI_RST, 0x00); 120 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_CALI, 121 + RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_FORCE_CALI_RST, 122 + RT711_DAC_DC_FORCE_CALI_RST); 123 + 124 + /* cal_clk_en_reg */ 125 + if (rt711->hw_ver == RT711_VER_VD0) 126 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_CALI, 127 + RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_CALI_CLK_EN, 128 + RT711_DAC_DC_CALI_CLK_EN); 129 + 130 + /* trigger */ 131 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_CALI, 132 + RT711_DAC_DC_CALI_CTL1, RT711_DAC_DC_CALI_TRIGGER, 133 + RT711_DAC_DC_CALI_TRIGGER); 134 + 135 + /* wait for calibration process */ 136 + rt711_sdca_index_read(rt711, RT711_VENDOR_CALI, 137 + RT711_DAC_DC_CALI_CTL1, &val); 138 + 139 + for (loop_dc = 0; loop_dc < chk_cnt && 140 + (val & RT711_DAC_DC_CALI_TRIGGER); loop_dc++) { 141 + usleep_range(10000, 11000); 142 + ret = rt711_sdca_index_read(rt711, RT711_VENDOR_CALI, 143 + RT711_DAC_DC_CALI_CTL1, &val); 144 + if (ret < 0) 145 + goto _cali_fail_; 146 + } 147 + if (loop_dc == chk_cnt) 148 + dev_err(dev, "%s, calibration time-out!\n", __func__); 149 + 150 + if (loop_dc == chk_cnt || loop_rc == chk_cnt) 151 + ret = -ETIMEDOUT; 152 + 153 + _cali_fail_: 154 + /* enable impedance sense */ 155 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 156 + RT711_FSM_CTL, RT711_FSM_IMP_EN, RT711_FSM_IMP_EN); 157 + 158 + /* release HP-JD and trigger FSM */ 159 + rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 160 + RT711_DIGITAL_MISC_CTRL4, 0x201b); 161 + 162 + mutex_unlock(&rt711->calibrate_mutex); 163 + dev_dbg(dev, "%s calibration complete, ret=%d\n", __func__, ret); 164 + return ret; 165 + } 166 + 167 + static unsigned int rt711_sdca_button_detect(struct rt711_sdca_priv *rt711) 168 + { 169 + unsigned int btn_type = 0, offset, idx, val, owner; 170 + int ret; 171 + unsigned char buf[3]; 172 + 173 + /* get current UMP message owner */ 174 + ret = regmap_read(rt711->regmap, 175 + SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_CURRENT_OWNER, 0), 176 + &owner); 177 + if (ret < 0) 178 + return 0; 179 + 180 + /* if owner is device then there is no button event from device */ 181 + if (owner == 1) 182 + return 0; 183 + 184 + /* read UMP message offset */ 185 + ret = regmap_read(rt711->regmap, 186 + SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_MESSAGE_OFFSET, 0), 187 + &offset); 188 + if (ret < 0) 189 + goto _end_btn_det_; 190 + 191 + for (idx = 0; idx < sizeof(buf); idx++) { 192 + ret = regmap_read(rt711->regmap, 193 + RT711_BUF_ADDR_HID1 + offset + idx, &val); 194 + if (ret < 0) 195 + goto _end_btn_det_; 196 + buf[idx] = val & 0xff; 197 + } 198 + 199 + if (buf[0] == 0x11) { 200 + switch (buf[1] & 0xf0) { 201 + case 0x10: 202 + btn_type |= SND_JACK_BTN_2; 203 + break; 204 + case 0x20: 205 + btn_type |= SND_JACK_BTN_3; 206 + break; 207 + case 0x40: 208 + btn_type |= SND_JACK_BTN_0; 209 + break; 210 + case 0x80: 211 + btn_type |= SND_JACK_BTN_1; 212 + break; 213 + } 214 + switch (buf[2]) { 215 + case 0x01: 216 + case 0x10: 217 + btn_type |= SND_JACK_BTN_2; 218 + break; 219 + case 0x02: 220 + case 0x20: 221 + btn_type |= SND_JACK_BTN_3; 222 + break; 223 + case 0x04: 224 + case 0x40: 225 + btn_type |= SND_JACK_BTN_0; 226 + break; 227 + case 0x08: 228 + case 0x80: 229 + btn_type |= SND_JACK_BTN_1; 230 + break; 231 + } 232 + } 233 + 234 + _end_btn_det_: 235 + /* Host is owner, so set back to device */ 236 + if (owner == 0) 237 + /* set owner to device */ 238 + regmap_write(rt711->regmap, 239 + SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, 240 + RT711_SDCA_CTL_HIDTX_SET_OWNER_TO_DEVICE, 0), 0x01); 241 + 242 + return btn_type; 243 + } 244 + 245 + static int rt711_sdca_headset_detect(struct rt711_sdca_priv *rt711) 246 + { 247 + unsigned int det_mode; 248 + int ret; 249 + 250 + /* get detected_mode */ 251 + ret = regmap_read(rt711->regmap, 252 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_DETECTED_MODE, 0), 253 + &det_mode); 254 + if (ret < 0) 255 + goto io_error; 256 + 257 + switch (det_mode) { 258 + case 0x00: 259 + rt711->jack_type = 0; 260 + break; 261 + case 0x03: 262 + rt711->jack_type = SND_JACK_HEADPHONE; 263 + break; 264 + case 0x05: 265 + rt711->jack_type = SND_JACK_HEADSET; 266 + break; 267 + } 268 + 269 + /* write selected_mode */ 270 + if (det_mode) { 271 + ret = regmap_write(rt711->regmap, 272 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_SELECTED_MODE, 0), 273 + det_mode); 274 + if (ret < 0) 275 + goto io_error; 276 + } 277 + 278 + dev_dbg(&rt711->slave->dev, 279 + "%s, detected_mode=0x%x\n", __func__, det_mode); 280 + 281 + return 0; 282 + 283 + io_error: 284 + pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); 285 + return ret; 286 + } 287 + 288 + static void rt711_sdca_jack_detect_handler(struct work_struct *work) 289 + { 290 + struct rt711_sdca_priv *rt711 = 291 + container_of(work, struct rt711_sdca_priv, jack_detect_work.work); 292 + int btn_type = 0, ret; 293 + 294 + if (!rt711->hs_jack) 295 + return; 296 + 297 + if (!rt711->component->card->instantiated) 298 + return; 299 + 300 + /* SDW_SCP_SDCA_INT_SDCA_0 is used for jack detection */ 301 + if (rt711->scp_sdca_stat1 & SDW_SCP_SDCA_INT_SDCA_0) { 302 + ret = rt711_sdca_headset_detect(rt711); 303 + if (ret < 0) 304 + return; 305 + } 306 + 307 + /* SDW_SCP_SDCA_INT_SDCA_8 is used for button detection */ 308 + if (rt711->scp_sdca_stat2 & SDW_SCP_SDCA_INT_SDCA_8) 309 + btn_type = rt711_sdca_button_detect(rt711); 310 + 311 + if (rt711->jack_type == 0) 312 + btn_type = 0; 313 + 314 + dev_dbg(&rt711->slave->dev, 315 + "in %s, jack_type=0x%x\n", __func__, rt711->jack_type); 316 + dev_dbg(&rt711->slave->dev, 317 + "in %s, btn_type=0x%x\n", __func__, btn_type); 318 + dev_dbg(&rt711->slave->dev, 319 + "in %s, scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__, 320 + rt711->scp_sdca_stat1, rt711->scp_sdca_stat2); 321 + 322 + snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type, 323 + SND_JACK_HEADSET | 324 + SND_JACK_BTN_0 | SND_JACK_BTN_1 | 325 + SND_JACK_BTN_2 | SND_JACK_BTN_3); 326 + 327 + if (btn_type) { 328 + /* button released */ 329 + snd_soc_jack_report(rt711->hs_jack, rt711->jack_type, 330 + SND_JACK_HEADSET | 331 + SND_JACK_BTN_0 | SND_JACK_BTN_1 | 332 + SND_JACK_BTN_2 | SND_JACK_BTN_3); 333 + 334 + mod_delayed_work(system_power_efficient_wq, 335 + &rt711->jack_btn_check_work, msecs_to_jiffies(200)); 336 + } 337 + } 338 + 339 + static void rt711_sdca_btn_check_handler(struct work_struct *work) 340 + { 341 + struct rt711_sdca_priv *rt711 = 342 + container_of(work, struct rt711_sdca_priv, jack_btn_check_work.work); 343 + int btn_type = 0, ret, idx; 344 + unsigned int det_mode, offset, val; 345 + unsigned char buf[3]; 346 + 347 + ret = regmap_read(rt711->regmap, 348 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_DETECTED_MODE, 0), 349 + &det_mode); 350 + if (ret < 0) 351 + goto io_error; 352 + 353 + /* pin attached */ 354 + if (det_mode) { 355 + /* read UMP message offset */ 356 + ret = regmap_read(rt711->regmap, 357 + SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_MESSAGE_OFFSET, 0), 358 + &offset); 359 + if (ret < 0) 360 + goto io_error; 361 + 362 + for (idx = 0; idx < sizeof(buf); idx++) { 363 + ret = regmap_read(rt711->regmap, 364 + RT711_BUF_ADDR_HID1 + offset + idx, &val); 365 + if (ret < 0) 366 + goto io_error; 367 + buf[idx] = val & 0xff; 368 + } 369 + 370 + if (buf[0] == 0x11) { 371 + switch (buf[1] & 0xf0) { 372 + case 0x10: 373 + btn_type |= SND_JACK_BTN_2; 374 + break; 375 + case 0x20: 376 + btn_type |= SND_JACK_BTN_3; 377 + break; 378 + case 0x40: 379 + btn_type |= SND_JACK_BTN_0; 380 + break; 381 + case 0x80: 382 + btn_type |= SND_JACK_BTN_1; 383 + break; 384 + } 385 + switch (buf[2]) { 386 + case 0x01: 387 + case 0x10: 388 + btn_type |= SND_JACK_BTN_2; 389 + break; 390 + case 0x02: 391 + case 0x20: 392 + btn_type |= SND_JACK_BTN_3; 393 + break; 394 + case 0x04: 395 + case 0x40: 396 + btn_type |= SND_JACK_BTN_0; 397 + break; 398 + case 0x08: 399 + case 0x80: 400 + btn_type |= SND_JACK_BTN_1; 401 + break; 402 + } 403 + } 404 + } else 405 + rt711->jack_type = 0; 406 + 407 + dev_dbg(&rt711->slave->dev, "%s, btn_type=0x%x\n", __func__, btn_type); 408 + snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type, 409 + SND_JACK_HEADSET | 410 + SND_JACK_BTN_0 | SND_JACK_BTN_1 | 411 + SND_JACK_BTN_2 | SND_JACK_BTN_3); 412 + 413 + if (btn_type) { 414 + /* button released */ 415 + snd_soc_jack_report(rt711->hs_jack, rt711->jack_type, 416 + SND_JACK_HEADSET | 417 + SND_JACK_BTN_0 | SND_JACK_BTN_1 | 418 + SND_JACK_BTN_2 | SND_JACK_BTN_3); 419 + 420 + mod_delayed_work(system_power_efficient_wq, 421 + &rt711->jack_btn_check_work, msecs_to_jiffies(200)); 422 + } 423 + 424 + return; 425 + 426 + io_error: 427 + pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); 428 + } 429 + 430 + static void rt711_sdca_jack_init(struct rt711_sdca_priv *rt711) 431 + { 432 + mutex_lock(&rt711->calibrate_mutex); 433 + 434 + if (rt711->hs_jack) { 435 + /* Enable HID1 event & set button RTC mode */ 436 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 437 + RT711_PUSH_BTN_INT_CTL6, 0x80f0, 0x8000); 438 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 439 + RT711_PUSH_BTN_INT_CTL2, 0x11dd, 0x11dd); 440 + rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL, 441 + RT711_PUSH_BTN_INT_CTL7, 0xffff); 442 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 443 + RT711_PUSH_BTN_INT_CTL9, 0xf000, 0x0000); 444 + 445 + /* GE_mode_change_event_en & Hid1_push_button_event_en */ 446 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 447 + RT711_GE_MODE_RELATED_CTL, 0x0c00, 0x0c00); 448 + 449 + switch (rt711->jd_src) { 450 + case RT711_JD1: 451 + /* default settings was already for JD1 */ 452 + break; 453 + case RT711_JD2: 454 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 455 + RT711_JD_CTL1, RT711_JD2_DIGITAL_MODE_SEL, 456 + RT711_JD2_DIGITAL_MODE_SEL); 457 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 458 + RT711_JD_CTL2, RT711_JD2_2PORT_200K_DECODE_HP | RT711_HP_JD_SEL_JD2, 459 + RT711_JD2_2PORT_200K_DECODE_HP | RT711_HP_JD_SEL_JD2); 460 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 461 + RT711_CC_DET1, 462 + RT711_HP_JD_FINAL_RESULT_CTL_JD12, 463 + RT711_HP_JD_FINAL_RESULT_CTL_JD12); 464 + break; 465 + default: 466 + dev_warn(rt711->component->dev, "Wrong JD source\n"); 467 + break; 468 + } 469 + 470 + /* set SCP_SDCA_IntMask1[0]=1 */ 471 + sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK1, SDW_SCP_SDCA_INTMASK_SDCA_0); 472 + /* set SCP_SDCA_IntMask2[0]=1 */ 473 + sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK2, SDW_SCP_SDCA_INTMASK_SDCA_8); 474 + dev_dbg(&rt711->slave->dev, "in %s enable\n", __func__); 475 + } else { 476 + /* disable HID 1/2 event */ 477 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 478 + RT711_GE_MODE_RELATED_CTL, 0x0c00, 0x0000); 479 + 480 + dev_dbg(&rt711->slave->dev, "in %s disable\n", __func__); 481 + } 482 + 483 + mutex_unlock(&rt711->calibrate_mutex); 484 + } 485 + 486 + static int rt711_sdca_set_jack_detect(struct snd_soc_component *component, 487 + struct snd_soc_jack *hs_jack, void *data) 488 + { 489 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 490 + 491 + rt711->hs_jack = hs_jack; 492 + 493 + if (!rt711->hw_init) { 494 + dev_dbg(&rt711->slave->dev, 495 + "%s hw_init not ready yet\n", __func__); 496 + return 0; 497 + } 498 + 499 + rt711_sdca_jack_init(rt711); 500 + return 0; 501 + } 502 + 503 + /* For SDCA control DAC/ADC Gain */ 504 + static int rt711_sdca_set_gain_put(struct snd_kcontrol *kcontrol, 505 + struct snd_ctl_elem_value *ucontrol) 506 + { 507 + struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 508 + struct soc_mixer_control *mc = 509 + (struct soc_mixer_control *)kcontrol->private_value; 510 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 511 + unsigned int read_l, read_r, gain_l_val, gain_r_val; 512 + unsigned int i, adc_vol_flag = 0; 513 + 514 + if (strstr(ucontrol->id.name, "FU1E Capture Volume") || 515 + strstr(ucontrol->id.name, "FU0F Capture Volume")) 516 + adc_vol_flag = 1; 517 + 518 + /* control value to 2's complement value */ 519 + /* L Channel */ 520 + gain_l_val = ucontrol->value.integer.value[0]; 521 + if (gain_l_val > mc->max) 522 + gain_l_val = mc->max; 523 + read_l = gain_l_val; 524 + 525 + if (mc->shift == 8) /* boost gain */ 526 + gain_l_val = (gain_l_val * 10) << mc->shift; 527 + else { /* ADC/DAC gain */ 528 + if (adc_vol_flag && gain_l_val > mc->shift) 529 + gain_l_val = (gain_l_val - mc->shift) * 75; 530 + else 531 + gain_l_val = (mc->shift - gain_l_val) * 75; 532 + gain_l_val <<= 8; 533 + gain_l_val /= 100; 534 + if (!(adc_vol_flag && read_l > mc->shift)) { 535 + gain_l_val = ~gain_l_val; 536 + gain_l_val += 1; 537 + } 538 + gain_l_val &= 0xffff; 539 + } 540 + 541 + /* R Channel */ 542 + gain_r_val = ucontrol->value.integer.value[1]; 543 + if (gain_r_val > mc->max) 544 + gain_r_val = mc->max; 545 + read_r = gain_r_val; 546 + 547 + if (mc->shift == 8) /* boost gain */ 548 + gain_r_val = (gain_r_val * 10) << mc->shift; 549 + else { /* ADC/DAC gain */ 550 + if (adc_vol_flag && gain_r_val > mc->shift) 551 + gain_r_val = (gain_r_val - mc->shift) * 75; 552 + else 553 + gain_r_val = (mc->shift - gain_r_val) * 75; 554 + gain_r_val <<= 8; 555 + gain_r_val /= 100; 556 + if (!(adc_vol_flag && read_r > mc->shift)) { 557 + gain_r_val = ~gain_r_val; 558 + gain_r_val += 1; 559 + } 560 + gain_r_val &= 0xffff; 561 + } 562 + 563 + for (i = 0; i < 3; i++) { /* retry 3 times at most */ 564 + /* Lch*/ 565 + regmap_write(rt711->mbq_regmap, mc->reg, gain_l_val); 566 + 567 + /* Rch */ 568 + regmap_write(rt711->mbq_regmap, mc->rreg, gain_r_val); 569 + 570 + regmap_read(rt711->mbq_regmap, mc->reg, &read_l); 571 + regmap_read(rt711->mbq_regmap, mc->rreg, &read_r); 572 + if (read_r == gain_r_val && read_l == gain_l_val) 573 + break; 574 + } 575 + 576 + return i == 3 ? -EIO : 0; 577 + } 578 + 579 + static int rt711_sdca_set_gain_get(struct snd_kcontrol *kcontrol, 580 + struct snd_ctl_elem_value *ucontrol) 581 + { 582 + struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 583 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 584 + struct soc_mixer_control *mc = 585 + (struct soc_mixer_control *)kcontrol->private_value; 586 + unsigned int read_l, read_r, ctl_l = 0, ctl_r = 0; 587 + unsigned int adc_vol_flag = 0, neg_flag = 0; 588 + 589 + if (strstr(ucontrol->id.name, "FU1E Capture Volume") || 590 + strstr(ucontrol->id.name, "FU0F Capture Volume")) 591 + adc_vol_flag = 1; 592 + 593 + regmap_read(rt711->mbq_regmap, mc->reg, &read_l); 594 + regmap_read(rt711->mbq_regmap, mc->rreg, &read_r); 595 + 596 + /* 2's complement value to control value */ 597 + if (mc->shift == 8) /* boost gain */ 598 + ctl_l = (read_l >> mc->shift) / 10; 599 + else { /* ADC/DAC gain */ 600 + ctl_l = read_l; 601 + if (read_l & BIT(15)) { 602 + ctl_l = 0xffff & ~(read_l - 1); 603 + neg_flag = 1; 604 + } 605 + ctl_l *= 100; 606 + ctl_l >>= 8; 607 + if (adc_vol_flag) { 608 + if (neg_flag) 609 + ctl_l = mc->shift - (ctl_l / 75); 610 + else 611 + ctl_l = mc->shift + (ctl_l / 75); 612 + } else 613 + ctl_l = mc->max - (ctl_l / 75); 614 + } 615 + 616 + neg_flag = 0; 617 + if (read_l != read_r) { 618 + if (mc->shift == 8) /* boost gain */ 619 + ctl_r = (read_r >> mc->shift) / 10; 620 + else { /* ADC/DAC gain */ 621 + ctl_r = read_r; 622 + if (read_r & BIT(15)) { 623 + ctl_r = 0xffff & ~(read_r - 1); 624 + neg_flag = 1; 625 + } 626 + ctl_r *= 100; 627 + ctl_r >>= 8; 628 + if (adc_vol_flag) { 629 + if (neg_flag) 630 + ctl_r = mc->shift - (ctl_r / 75); 631 + else 632 + ctl_r = mc->shift + (ctl_r / 75); 633 + } else 634 + ctl_r = mc->max - (ctl_r / 75); 635 + } 636 + } else 637 + ctl_r = ctl_l; 638 + 639 + ucontrol->value.integer.value[0] = ctl_l; 640 + ucontrol->value.integer.value[1] = ctl_r; 641 + 642 + return 0; 643 + } 644 + 645 + static const DECLARE_TLV_DB_SCALE(out_vol_tlv, -6525, 75, 0); 646 + static const DECLARE_TLV_DB_SCALE(in_vol_tlv, -1725, 75, 0); 647 + static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, 0, 1000, 0); 648 + 649 + static const struct snd_kcontrol_new rt711_sdca_snd_controls[] = { 650 + SOC_DOUBLE_R_EXT_TLV("FU05 Playback Volume", 651 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_VOLUME, CH_L), 652 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_VOLUME, CH_R), 653 + 0x57, 0x57, 0, 654 + rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, out_vol_tlv), 655 + SOC_DOUBLE_R("FU1E Capture Switch", 656 + SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_MUTE, CH_L), 657 + SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_MUTE, CH_R), 658 + 0, 1, 1), 659 + SOC_DOUBLE_R("FU0F Capture Switch", 660 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_MUTE, CH_L), 661 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_MUTE, CH_R), 662 + 0, 1, 1), 663 + SOC_DOUBLE_R_EXT_TLV("FU1E Capture Volume", 664 + SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_VOLUME, CH_L), 665 + SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_VOLUME, CH_R), 666 + 0x17, 0x3f, 0, 667 + rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, in_vol_tlv), 668 + SOC_DOUBLE_R_EXT_TLV("FU0F Capture Volume", 669 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_VOLUME, CH_L), 670 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_VOLUME, CH_R), 671 + 0x17, 0x3f, 0, 672 + rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, in_vol_tlv), 673 + SOC_DOUBLE_R_EXT_TLV("FU44 Gain Volume", 674 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PLATFORM_FU44, RT711_SDCA_CTL_FU_CH_GAIN, CH_L), 675 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PLATFORM_FU44, RT711_SDCA_CTL_FU_CH_GAIN, CH_R), 676 + 8, 3, 0, 677 + rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, mic_vol_tlv), 678 + SOC_DOUBLE_R_EXT_TLV("FU15 Gain Volume", 679 + SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PLATFORM_FU15, RT711_SDCA_CTL_FU_CH_GAIN, CH_L), 680 + SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PLATFORM_FU15, RT711_SDCA_CTL_FU_CH_GAIN, CH_R), 681 + 8, 3, 0, 682 + rt711_sdca_set_gain_get, rt711_sdca_set_gain_put, mic_vol_tlv), 683 + }; 684 + 685 + static int rt711_sdca_mux_get(struct snd_kcontrol *kcontrol, 686 + struct snd_ctl_elem_value *ucontrol) 687 + { 688 + struct snd_soc_component *component = 689 + snd_soc_dapm_kcontrol_component(kcontrol); 690 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 691 + unsigned int val = 0, mask_sft; 692 + 693 + if (strstr(ucontrol->id.name, "ADC 22 Mux")) 694 + mask_sft = 10; 695 + else if (strstr(ucontrol->id.name, "ADC 23 Mux")) 696 + mask_sft = 13; 697 + else 698 + return -EINVAL; 699 + 700 + rt711_sdca_index_read(rt711, RT711_VENDOR_HDA_CTL, 701 + RT711_HDA_LEGACY_MUX_CTL1, &val); 702 + 703 + ucontrol->value.enumerated.item[0] = (val >> mask_sft) & 0x7; 704 + 705 + return 0; 706 + } 707 + 708 + static int rt711_sdca_mux_put(struct snd_kcontrol *kcontrol, 709 + struct snd_ctl_elem_value *ucontrol) 710 + { 711 + struct snd_soc_component *component = 712 + snd_soc_dapm_kcontrol_component(kcontrol); 713 + struct snd_soc_dapm_context *dapm = 714 + snd_soc_dapm_kcontrol_dapm(kcontrol); 715 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 716 + struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 717 + unsigned int *item = ucontrol->value.enumerated.item; 718 + unsigned int val, val2 = 0, change, mask_sft; 719 + 720 + if (item[0] >= e->items) 721 + return -EINVAL; 722 + 723 + if (strstr(ucontrol->id.name, "ADC 22 Mux")) 724 + mask_sft = 10; 725 + else if (strstr(ucontrol->id.name, "ADC 23 Mux")) 726 + mask_sft = 13; 727 + else 728 + return -EINVAL; 729 + 730 + val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; 731 + 732 + rt711_sdca_index_read(rt711, RT711_VENDOR_HDA_CTL, 733 + RT711_HDA_LEGACY_MUX_CTL1, &val2); 734 + val2 = (val2 >> mask_sft) & 0x7; 735 + 736 + if (val == val2) 737 + change = 0; 738 + else 739 + change = 1; 740 + 741 + if (change) 742 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 743 + RT711_HDA_LEGACY_MUX_CTL1, 0x7 << mask_sft, 744 + val << mask_sft); 745 + 746 + snd_soc_dapm_mux_update_power(dapm, kcontrol, 747 + item[0], e, NULL); 748 + 749 + return change; 750 + } 751 + 752 + static const char * const adc_mux_text[] = { 753 + "MIC2", 754 + "LINE1", 755 + "LINE2", 756 + "DMIC", 757 + }; 758 + 759 + static SOC_ENUM_SINGLE_DECL( 760 + rt711_adc22_enum, SND_SOC_NOPM, 0, adc_mux_text); 761 + 762 + static SOC_ENUM_SINGLE_DECL( 763 + rt711_adc23_enum, SND_SOC_NOPM, 0, adc_mux_text); 764 + 765 + static const struct snd_kcontrol_new rt711_sdca_adc22_mux = 766 + SOC_DAPM_ENUM_EXT("ADC 22 Mux", rt711_adc22_enum, 767 + rt711_sdca_mux_get, rt711_sdca_mux_put); 768 + 769 + static const struct snd_kcontrol_new rt711_sdca_adc23_mux = 770 + SOC_DAPM_ENUM_EXT("ADC 23 Mux", rt711_adc23_enum, 771 + rt711_sdca_mux_get, rt711_sdca_mux_put); 772 + 773 + static int rt711_sdca_fu05_event(struct snd_soc_dapm_widget *w, 774 + struct snd_kcontrol *kcontrol, int event) 775 + { 776 + struct snd_soc_component *component = 777 + snd_soc_dapm_to_component(w->dapm); 778 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 779 + unsigned char unmute = 0x0, mute = 0x1; 780 + 781 + switch (event) { 782 + case SND_SOC_DAPM_POST_PMU: 783 + regmap_write(rt711->regmap, 784 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, 785 + RT711_SDCA_CTL_FU_MUTE, CH_L), 786 + unmute); 787 + regmap_write(rt711->regmap, 788 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, 789 + RT711_SDCA_CTL_FU_MUTE, CH_R), 790 + unmute); 791 + break; 792 + case SND_SOC_DAPM_PRE_PMD: 793 + regmap_write(rt711->regmap, 794 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, 795 + RT711_SDCA_CTL_FU_MUTE, CH_L), 796 + mute); 797 + regmap_write(rt711->regmap, 798 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, 799 + RT711_SDCA_CTL_FU_MUTE, CH_R), 800 + mute); 801 + break; 802 + } 803 + return 0; 804 + } 805 + 806 + static int rt711_sdca_fu0f_event(struct snd_soc_dapm_widget *w, 807 + struct snd_kcontrol *kcontrol, int event) 808 + { 809 + struct snd_soc_component *component = 810 + snd_soc_dapm_to_component(w->dapm); 811 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 812 + unsigned char unmute = 0x0, mute = 0x1; 813 + 814 + switch (event) { 815 + case SND_SOC_DAPM_POST_PMU: 816 + regmap_write(rt711->regmap, 817 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, 818 + RT711_SDCA_CTL_FU_MUTE, CH_L), 819 + unmute); 820 + regmap_write(rt711->regmap, 821 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, 822 + RT711_SDCA_CTL_FU_MUTE, CH_R), 823 + unmute); 824 + break; 825 + case SND_SOC_DAPM_PRE_PMD: 826 + regmap_write(rt711->regmap, 827 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, 828 + RT711_SDCA_CTL_FU_MUTE, CH_L), 829 + mute); 830 + regmap_write(rt711->regmap, 831 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, 832 + RT711_SDCA_CTL_FU_MUTE, CH_R), 833 + mute); 834 + break; 835 + } 836 + return 0; 837 + } 838 + 839 + static int rt711_sdca_fu1e_event(struct snd_soc_dapm_widget *w, 840 + struct snd_kcontrol *kcontrol, int event) 841 + { 842 + struct snd_soc_component *component = 843 + snd_soc_dapm_to_component(w->dapm); 844 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 845 + unsigned char unmute = 0x0, mute = 0x1; 846 + 847 + switch (event) { 848 + case SND_SOC_DAPM_POST_PMU: 849 + regmap_write(rt711->regmap, 850 + SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, 851 + RT711_SDCA_CTL_FU_MUTE, CH_L), 852 + unmute); 853 + regmap_write(rt711->regmap, 854 + SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, 855 + RT711_SDCA_CTL_FU_MUTE, CH_R), 856 + unmute); 857 + break; 858 + case SND_SOC_DAPM_PRE_PMD: 859 + regmap_write(rt711->regmap, 860 + SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, 861 + RT711_SDCA_CTL_FU_MUTE, CH_L), 862 + mute); 863 + regmap_write(rt711->regmap, 864 + SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, 865 + RT711_SDCA_CTL_FU_MUTE, CH_R), 866 + mute); 867 + break; 868 + } 869 + return 0; 870 + } 871 + 872 + static int rt711_sdca_pde28_event(struct snd_soc_dapm_widget *w, 873 + struct snd_kcontrol *kcontrol, int event) 874 + { 875 + struct snd_soc_component *component = 876 + snd_soc_dapm_to_component(w->dapm); 877 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 878 + unsigned char ps0 = 0x0, ps3 = 0x3; 879 + 880 + switch (event) { 881 + case SND_SOC_DAPM_POST_PMU: 882 + regmap_write(rt711->regmap, 883 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE28, 884 + RT711_SDCA_CTL_REQ_POWER_STATE, 0), 885 + ps0); 886 + break; 887 + case SND_SOC_DAPM_PRE_PMD: 888 + regmap_write(rt711->regmap, 889 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE28, 890 + RT711_SDCA_CTL_REQ_POWER_STATE, 0), 891 + ps3); 892 + break; 893 + } 894 + return 0; 895 + } 896 + 897 + static int rt711_sdca_pde29_event(struct snd_soc_dapm_widget *w, 898 + struct snd_kcontrol *kcontrol, int event) 899 + { 900 + struct snd_soc_component *component = 901 + snd_soc_dapm_to_component(w->dapm); 902 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 903 + unsigned char ps0 = 0x0, ps3 = 0x3; 904 + 905 + switch (event) { 906 + case SND_SOC_DAPM_POST_PMU: 907 + regmap_write(rt711->regmap, 908 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE29, 909 + RT711_SDCA_CTL_REQ_POWER_STATE, 0), 910 + ps0); 911 + break; 912 + case SND_SOC_DAPM_PRE_PMD: 913 + regmap_write(rt711->regmap, 914 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDE29, 915 + RT711_SDCA_CTL_REQ_POWER_STATE, 0), 916 + ps3); 917 + break; 918 + } 919 + return 0; 920 + } 921 + 922 + static int rt711_sdca_pde2a_event(struct snd_soc_dapm_widget *w, 923 + struct snd_kcontrol *kcontrol, int event) 924 + { 925 + struct snd_soc_component *component = 926 + snd_soc_dapm_to_component(w->dapm); 927 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 928 + unsigned char ps0 = 0x0, ps3 = 0x3; 929 + 930 + switch (event) { 931 + case SND_SOC_DAPM_POST_PMU: 932 + regmap_write(rt711->regmap, 933 + SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PDE2A, 934 + RT711_SDCA_CTL_REQ_POWER_STATE, 0), 935 + ps0); 936 + break; 937 + case SND_SOC_DAPM_PRE_PMD: 938 + regmap_write(rt711->regmap, 939 + SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PDE2A, 940 + RT711_SDCA_CTL_REQ_POWER_STATE, 0), 941 + ps3); 942 + break; 943 + } 944 + return 0; 945 + } 946 + 947 + static int rt711_sdca_line1_power_event(struct snd_soc_dapm_widget *w, 948 + struct snd_kcontrol *kcontrol, int event) 949 + { 950 + struct snd_soc_component *component = 951 + snd_soc_dapm_to_component(w->dapm); 952 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 953 + static unsigned int sel_mode = 0xffff; 954 + 955 + switch (event) { 956 + case SND_SOC_DAPM_POST_PMU: 957 + regmap_read(rt711->regmap, 958 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, 959 + RT711_SDCA_CTL_SELECTED_MODE, 0), 960 + &sel_mode); 961 + regmap_write(rt711->regmap, 962 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE1, 963 + RT711_SDCA_CTL_VENDOR_DEF, 0), 964 + 0x1); 965 + regmap_write(rt711->regmap, 966 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, 967 + RT711_SDCA_CTL_SELECTED_MODE, 0), 968 + 0x7); 969 + break; 970 + case SND_SOC_DAPM_PRE_PMD: 971 + regmap_write(rt711->regmap, 972 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE1, 973 + RT711_SDCA_CTL_VENDOR_DEF, 0), 974 + 0x0); 975 + if (sel_mode != 0xffff) 976 + regmap_write(rt711->regmap, 977 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, 978 + RT711_SDCA_CTL_SELECTED_MODE, 0), 979 + sel_mode); 980 + break; 981 + } 982 + 983 + return 0; 984 + } 985 + 986 + static int rt711_sdca_line2_power_event(struct snd_soc_dapm_widget *w, 987 + struct snd_kcontrol *kcontrol, int event) 988 + { 989 + struct snd_soc_component *component = 990 + snd_soc_dapm_to_component(w->dapm); 991 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 992 + unsigned char ps0 = 0x0, ps3 = 0x3; 993 + 994 + switch (event) { 995 + case SND_SOC_DAPM_POST_PMU: 996 + regmap_write(rt711->regmap, 997 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDELINE2, 998 + RT711_SDCA_CTL_REQ_POWER_STATE, 0), 999 + ps0); 1000 + regmap_write(rt711->regmap, 1001 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE2, 1002 + RT711_SDCA_CTL_VENDOR_DEF, 0), 1003 + 0x1); 1004 + break; 1005 + case SND_SOC_DAPM_PRE_PMD: 1006 + regmap_write(rt711->regmap, 1007 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_LINE2, 1008 + RT711_SDCA_CTL_VENDOR_DEF, 0), 1009 + 0x0); 1010 + regmap_write(rt711->regmap, 1011 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PDELINE2, 1012 + RT711_SDCA_CTL_REQ_POWER_STATE, 0), 1013 + ps3); 1014 + break; 1015 + } 1016 + 1017 + return 0; 1018 + } 1019 + 1020 + static const struct snd_soc_dapm_widget rt711_sdca_dapm_widgets[] = { 1021 + SND_SOC_DAPM_OUTPUT("HP"), 1022 + SND_SOC_DAPM_INPUT("MIC2"), 1023 + SND_SOC_DAPM_INPUT("DMIC1"), 1024 + SND_SOC_DAPM_INPUT("DMIC2"), 1025 + SND_SOC_DAPM_INPUT("LINE1"), 1026 + SND_SOC_DAPM_INPUT("LINE2"), 1027 + 1028 + SND_SOC_DAPM_PGA_E("LINE1 Power", SND_SOC_NOPM, 1029 + 0, 0, NULL, 0, rt711_sdca_line1_power_event, 1030 + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1031 + SND_SOC_DAPM_PGA_E("LINE2 Power", SND_SOC_NOPM, 1032 + 0, 0, NULL, 0, rt711_sdca_line2_power_event, 1033 + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1034 + 1035 + SND_SOC_DAPM_SUPPLY("PDE 28", SND_SOC_NOPM, 0, 0, 1036 + rt711_sdca_pde28_event, 1037 + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1038 + SND_SOC_DAPM_SUPPLY("PDE 29", SND_SOC_NOPM, 0, 0, 1039 + rt711_sdca_pde29_event, 1040 + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1041 + SND_SOC_DAPM_SUPPLY("PDE 2A", SND_SOC_NOPM, 0, 0, 1042 + rt711_sdca_pde2a_event, 1043 + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1044 + 1045 + SND_SOC_DAPM_DAC_E("FU 05", NULL, SND_SOC_NOPM, 0, 0, 1046 + rt711_sdca_fu05_event, 1047 + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1048 + SND_SOC_DAPM_ADC_E("FU 0F", NULL, SND_SOC_NOPM, 0, 0, 1049 + rt711_sdca_fu0f_event, 1050 + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1051 + SND_SOC_DAPM_ADC_E("FU 1E", NULL, SND_SOC_NOPM, 0, 0, 1052 + rt711_sdca_fu1e_event, 1053 + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1054 + SND_SOC_DAPM_MUX("ADC 22 Mux", SND_SOC_NOPM, 0, 0, 1055 + &rt711_sdca_adc22_mux), 1056 + SND_SOC_DAPM_MUX("ADC 23 Mux", SND_SOC_NOPM, 0, 0, 1057 + &rt711_sdca_adc23_mux), 1058 + 1059 + SND_SOC_DAPM_AIF_IN("DP3RX", "DP3 Playback", 0, SND_SOC_NOPM, 0, 0), 1060 + SND_SOC_DAPM_AIF_OUT("DP2TX", "DP2 Capture", 0, SND_SOC_NOPM, 0, 0), 1061 + SND_SOC_DAPM_AIF_OUT("DP4TX", "DP4 Capture", 0, SND_SOC_NOPM, 0, 0), 1062 + }; 1063 + 1064 + static const struct snd_soc_dapm_route rt711_sdca_audio_map[] = { 1065 + {"FU 05", NULL, "DP3RX"}, 1066 + {"DP2TX", NULL, "FU 0F"}, 1067 + {"DP4TX", NULL, "FU 1E"}, 1068 + 1069 + {"LINE1 Power", NULL, "LINE1"}, 1070 + {"LINE2 Power", NULL, "LINE2"}, 1071 + {"HP", NULL, "PDE 28"}, 1072 + {"FU 0F", NULL, "PDE 29"}, 1073 + {"FU 1E", NULL, "PDE 2A"}, 1074 + 1075 + {"FU 0F", NULL, "ADC 22 Mux"}, 1076 + {"FU 1E", NULL, "ADC 23 Mux"}, 1077 + {"ADC 22 Mux", "DMIC", "DMIC1"}, 1078 + {"ADC 22 Mux", "LINE1", "LINE1 Power"}, 1079 + {"ADC 22 Mux", "LINE2", "LINE2 Power"}, 1080 + {"ADC 22 Mux", "MIC2", "MIC2"}, 1081 + {"ADC 23 Mux", "DMIC", "DMIC2"}, 1082 + {"ADC 23 Mux", "LINE1", "LINE1 Power"}, 1083 + {"ADC 23 Mux", "LINE2", "LINE2 Power"}, 1084 + {"ADC 23 Mux", "MIC2", "MIC2"}, 1085 + 1086 + {"HP", NULL, "FU 05"}, 1087 + }; 1088 + 1089 + static int rt711_sdca_parse_dt(struct rt711_sdca_priv *rt711, struct device *dev) 1090 + { 1091 + device_property_read_u32(dev, "realtek,jd-src", &rt711->jd_src); 1092 + 1093 + return 0; 1094 + } 1095 + 1096 + static int rt711_sdca_probe(struct snd_soc_component *component) 1097 + { 1098 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1099 + 1100 + rt711_sdca_parse_dt(rt711, &rt711->slave->dev); 1101 + rt711->component = component; 1102 + 1103 + return 0; 1104 + } 1105 + 1106 + static void rt711_sdca_remove(struct snd_soc_component *component) 1107 + { 1108 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1109 + 1110 + regcache_cache_only(rt711->regmap, true); 1111 + regcache_cache_only(rt711->mbq_regmap, true); 1112 + } 1113 + 1114 + static const struct snd_soc_component_driver soc_sdca_dev_rt711 = { 1115 + .probe = rt711_sdca_probe, 1116 + .controls = rt711_sdca_snd_controls, 1117 + .num_controls = ARRAY_SIZE(rt711_sdca_snd_controls), 1118 + .dapm_widgets = rt711_sdca_dapm_widgets, 1119 + .num_dapm_widgets = ARRAY_SIZE(rt711_sdca_dapm_widgets), 1120 + .dapm_routes = rt711_sdca_audio_map, 1121 + .num_dapm_routes = ARRAY_SIZE(rt711_sdca_audio_map), 1122 + .set_jack = rt711_sdca_set_jack_detect, 1123 + .remove = rt711_sdca_remove, 1124 + }; 1125 + 1126 + static int rt711_sdca_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream, 1127 + int direction) 1128 + { 1129 + struct sdw_stream_data *stream; 1130 + 1131 + if (!sdw_stream) 1132 + return 0; 1133 + 1134 + stream = kzalloc(sizeof(*stream), GFP_KERNEL); 1135 + if (!stream) 1136 + return -ENOMEM; 1137 + 1138 + stream->sdw_stream = sdw_stream; 1139 + 1140 + /* Use tx_mask or rx_mask to configure stream tag and set dma_data */ 1141 + if (direction == SNDRV_PCM_STREAM_PLAYBACK) 1142 + dai->playback_dma_data = stream; 1143 + else 1144 + dai->capture_dma_data = stream; 1145 + 1146 + return 0; 1147 + } 1148 + 1149 + static void rt711_sdca_shutdown(struct snd_pcm_substream *substream, 1150 + struct snd_soc_dai *dai) 1151 + { 1152 + struct sdw_stream_data *stream; 1153 + 1154 + stream = snd_soc_dai_get_dma_data(dai, substream); 1155 + snd_soc_dai_set_dma_data(dai, substream, NULL); 1156 + kfree(stream); 1157 + } 1158 + 1159 + static int rt711_sdca_pcm_hw_params(struct snd_pcm_substream *substream, 1160 + struct snd_pcm_hw_params *params, 1161 + struct snd_soc_dai *dai) 1162 + { 1163 + struct snd_soc_component *component = dai->component; 1164 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1165 + struct sdw_stream_config stream_config; 1166 + struct sdw_port_config port_config; 1167 + enum sdw_data_direction direction; 1168 + struct sdw_stream_data *stream; 1169 + int retval, port, num_channels; 1170 + unsigned int sampling_rate; 1171 + 1172 + dev_dbg(dai->dev, "%s %s", __func__, dai->name); 1173 + stream = snd_soc_dai_get_dma_data(dai, substream); 1174 + 1175 + if (!stream) 1176 + return -EINVAL; 1177 + 1178 + if (!rt711->slave) 1179 + return -EINVAL; 1180 + 1181 + /* SoundWire specific configuration */ 1182 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 1183 + direction = SDW_DATA_DIR_RX; 1184 + port = 3; 1185 + } else { 1186 + direction = SDW_DATA_DIR_TX; 1187 + if (dai->id == RT711_AIF1) 1188 + port = 2; 1189 + else if (dai->id == RT711_AIF2) 1190 + port = 4; 1191 + else 1192 + return -EINVAL; 1193 + } 1194 + 1195 + stream_config.frame_rate = params_rate(params); 1196 + stream_config.ch_count = params_channels(params); 1197 + stream_config.bps = snd_pcm_format_width(params_format(params)); 1198 + stream_config.direction = direction; 1199 + 1200 + num_channels = params_channels(params); 1201 + port_config.ch_mask = GENMASK(num_channels - 1, 0); 1202 + port_config.num = port; 1203 + 1204 + retval = sdw_stream_add_slave(rt711->slave, &stream_config, 1205 + &port_config, 1, stream->sdw_stream); 1206 + if (retval) { 1207 + dev_err(dai->dev, "Unable to configure port\n"); 1208 + return retval; 1209 + } 1210 + 1211 + if (params_channels(params) > 16) { 1212 + dev_err(component->dev, "Unsupported channels %d\n", 1213 + params_channels(params)); 1214 + return -EINVAL; 1215 + } 1216 + 1217 + /* sampling rate configuration */ 1218 + switch (params_rate(params)) { 1219 + case 44100: 1220 + sampling_rate = RT711_SDCA_RATE_44100HZ; 1221 + break; 1222 + case 48000: 1223 + sampling_rate = RT711_SDCA_RATE_48000HZ; 1224 + break; 1225 + case 96000: 1226 + sampling_rate = RT711_SDCA_RATE_96000HZ; 1227 + break; 1228 + case 192000: 1229 + sampling_rate = RT711_SDCA_RATE_192000HZ; 1230 + break; 1231 + default: 1232 + dev_err(component->dev, "Rate %d is not supported\n", 1233 + params_rate(params)); 1234 + return -EINVAL; 1235 + } 1236 + 1237 + /* set sampling frequency */ 1238 + regmap_write(rt711->regmap, 1239 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_CS01, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0), 1240 + sampling_rate); 1241 + regmap_write(rt711->regmap, 1242 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_CS11, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0), 1243 + sampling_rate); 1244 + regmap_write(rt711->regmap, 1245 + SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_CS1F, RT711_SDCA_CTL_SAMPLE_FREQ_INDEX, 0), 1246 + sampling_rate); 1247 + 1248 + return 0; 1249 + } 1250 + 1251 + static int rt711_sdca_pcm_hw_free(struct snd_pcm_substream *substream, 1252 + struct snd_soc_dai *dai) 1253 + { 1254 + struct snd_soc_component *component = dai->component; 1255 + struct rt711_sdca_priv *rt711 = snd_soc_component_get_drvdata(component); 1256 + struct sdw_stream_data *stream = 1257 + snd_soc_dai_get_dma_data(dai, substream); 1258 + 1259 + if (!rt711->slave) 1260 + return -EINVAL; 1261 + 1262 + sdw_stream_remove_slave(rt711->slave, stream->sdw_stream); 1263 + return 0; 1264 + } 1265 + 1266 + #define RT711_STEREO_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \ 1267 + SNDRV_PCM_RATE_192000) 1268 + #define RT711_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ 1269 + SNDRV_PCM_FMTBIT_S24_LE) 1270 + 1271 + static struct snd_soc_dai_ops rt711_sdca_ops = { 1272 + .hw_params = rt711_sdca_pcm_hw_params, 1273 + .hw_free = rt711_sdca_pcm_hw_free, 1274 + .set_sdw_stream = rt711_sdca_set_sdw_stream, 1275 + .shutdown = rt711_sdca_shutdown, 1276 + }; 1277 + 1278 + static struct snd_soc_dai_driver rt711_sdca_dai[] = { 1279 + { 1280 + .name = "rt711-sdca-aif1", 1281 + .id = RT711_AIF1, 1282 + .playback = { 1283 + .stream_name = "DP3 Playback", 1284 + .channels_min = 1, 1285 + .channels_max = 2, 1286 + .rates = RT711_STEREO_RATES, 1287 + .formats = RT711_FORMATS, 1288 + }, 1289 + .capture = { 1290 + .stream_name = "DP2 Capture", 1291 + .channels_min = 1, 1292 + .channels_max = 2, 1293 + .rates = RT711_STEREO_RATES, 1294 + .formats = RT711_FORMATS, 1295 + }, 1296 + .ops = &rt711_sdca_ops, 1297 + }, 1298 + { 1299 + .name = "rt711-sdca-aif2", 1300 + .id = RT711_AIF2, 1301 + .capture = { 1302 + .stream_name = "DP4 Capture", 1303 + .channels_min = 1, 1304 + .channels_max = 2, 1305 + .rates = RT711_STEREO_RATES, 1306 + .formats = RT711_FORMATS, 1307 + }, 1308 + .ops = &rt711_sdca_ops, 1309 + } 1310 + }; 1311 + 1312 + int rt711_sdca_init(struct device *dev, struct regmap *regmap, 1313 + struct regmap *mbq_regmap, struct sdw_slave *slave) 1314 + { 1315 + struct rt711_sdca_priv *rt711; 1316 + int ret; 1317 + 1318 + rt711 = devm_kzalloc(dev, sizeof(*rt711), GFP_KERNEL); 1319 + if (!rt711) 1320 + return -ENOMEM; 1321 + 1322 + dev_set_drvdata(dev, rt711); 1323 + rt711->slave = slave; 1324 + rt711->regmap = regmap; 1325 + rt711->mbq_regmap = mbq_regmap; 1326 + 1327 + /* 1328 + * Mark hw_init to false 1329 + * HW init will be performed when device reports present 1330 + */ 1331 + rt711->hw_init = false; 1332 + rt711->first_hw_init = false; 1333 + 1334 + /* JD source uses JD2 in default */ 1335 + rt711->jd_src = RT711_JD2; 1336 + 1337 + ret = devm_snd_soc_register_component(dev, 1338 + &soc_sdca_dev_rt711, 1339 + rt711_sdca_dai, 1340 + ARRAY_SIZE(rt711_sdca_dai)); 1341 + 1342 + dev_dbg(&slave->dev, "%s\n", __func__); 1343 + 1344 + return ret; 1345 + } 1346 + 1347 + static void rt711_sdca_vd0_io_init(struct rt711_sdca_priv *rt711) 1348 + { 1349 + rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1350 + RT711_GPIO_TEST_MODE_CTL2, 0x0e00); 1351 + rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL, 1352 + RT711_HDA_LEGACY_GPIO_CTL, 0x0008); 1353 + 1354 + regmap_write(rt711->regmap, 0x2f5a, 0x01); 1355 + 1356 + rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1357 + RT711_ADC27_VOL_SET, 0x8728); 1358 + 1359 + rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1360 + RT711_COMBO_JACK_AUTO_CTL3, 0xa472); 1361 + 1362 + regmap_write(rt711->regmap, 0x2f50, 0x02); 1363 + 1364 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_ANALOG_CTL, 1365 + RT711_MISC_POWER_CTL4, 0x6000, 0x6000); 1366 + 1367 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 1368 + RT711_COMBO_JACK_AUTO_CTL3, 0x000c, 0x000c); 1369 + 1370 + rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL, 1371 + RT711_HDA_LEGACY_CONFIG_CTL, 0x0000); 1372 + 1373 + rt711_sdca_index_write(rt711, RT711_VENDOR_VAD, 1374 + RT711_VAD_SRAM_CTL1, 0x0050); 1375 + } 1376 + 1377 + static void rt711_sdca_vd1_io_init(struct rt711_sdca_priv *rt711) 1378 + { 1379 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 1380 + RT711_HDA_LEGACY_UNSOLICITED_CTL, 0x0300, 0x0000); 1381 + 1382 + rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1383 + RT711_COMBO_JACK_AUTO_CTL3, 0xa43e); 1384 + 1385 + regmap_write(rt711->regmap, 0x2f5a, 0x05); 1386 + 1387 + rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1388 + RT711_JD_CTRL6, 0x0500); 1389 + 1390 + rt711_sdca_index_write(rt711, RT711_VENDOR_REG, 1391 + RT711_DMIC_CTL1, 0x6173); 1392 + 1393 + rt711_sdca_index_write(rt711, RT711_VENDOR_HDA_CTL, 1394 + RT711_HDA_LEGACY_CONFIG_CTL, 0x0000); 1395 + 1396 + rt711_sdca_index_write(rt711, RT711_VENDOR_VAD, 1397 + RT711_VAD_SRAM_CTL1, 0x0050); 1398 + } 1399 + 1400 + int rt711_sdca_io_init(struct device *dev, struct sdw_slave *slave) 1401 + { 1402 + struct rt711_sdca_priv *rt711 = dev_get_drvdata(dev); 1403 + int ret = 0; 1404 + unsigned int val; 1405 + 1406 + if (rt711->hw_init) 1407 + return 0; 1408 + 1409 + if (rt711->first_hw_init) { 1410 + regcache_cache_only(rt711->regmap, false); 1411 + regcache_cache_bypass(rt711->regmap, true); 1412 + } else { 1413 + /* 1414 + * PM runtime is only enabled when a Slave reports as Attached 1415 + */ 1416 + 1417 + /* set autosuspend parameters */ 1418 + pm_runtime_set_autosuspend_delay(&slave->dev, 3000); 1419 + pm_runtime_use_autosuspend(&slave->dev); 1420 + 1421 + /* update count of parent 'active' children */ 1422 + pm_runtime_set_active(&slave->dev); 1423 + 1424 + /* make sure the device does not suspend immediately */ 1425 + pm_runtime_mark_last_busy(&slave->dev); 1426 + 1427 + pm_runtime_enable(&slave->dev); 1428 + } 1429 + 1430 + pm_runtime_get_noresume(&slave->dev); 1431 + 1432 + rt711_sdca_reset(rt711); 1433 + 1434 + rt711_sdca_index_read(rt711, RT711_VENDOR_REG, RT711_JD_PRODUCT_NUM, &val); 1435 + rt711->hw_ver = val & 0xf; 1436 + 1437 + if (rt711->hw_ver == RT711_VER_VD0) 1438 + rt711_sdca_vd0_io_init(rt711); 1439 + else 1440 + rt711_sdca_vd1_io_init(rt711); 1441 + 1442 + /* DP4 mux select from 08_filter_Out_pri */ 1443 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_REG, 1444 + RT711_FILTER_SRC_SEL, 0x1800, 0x0800); 1445 + 1446 + /* ge_exclusive_inbox_en disable */ 1447 + rt711_sdca_index_update_bits(rt711, RT711_VENDOR_HDA_CTL, 1448 + RT711_PUSH_BTN_INT_CTL0, 0x20, 0x00); 1449 + 1450 + if (!rt711->first_hw_init) { 1451 + INIT_DELAYED_WORK(&rt711->jack_detect_work, 1452 + rt711_sdca_jack_detect_handler); 1453 + INIT_DELAYED_WORK(&rt711->jack_btn_check_work, 1454 + rt711_sdca_btn_check_handler); 1455 + mutex_init(&rt711->calibrate_mutex); 1456 + } 1457 + 1458 + /* calibration */ 1459 + ret = rt711_sdca_calibration(rt711); 1460 + if (ret < 0) 1461 + dev_err(dev, "%s, calibration failed!\n", __func__); 1462 + 1463 + /* HP output enable */ 1464 + regmap_write(rt711->regmap, 1465 + SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_OT1, RT711_SDCA_CTL_VENDOR_DEF, 0), 0x4); 1466 + 1467 + /* 1468 + * if set_jack callback occurred early than io_init, 1469 + * we set up the jack detection function now 1470 + */ 1471 + if (rt711->hs_jack) 1472 + rt711_sdca_jack_init(rt711); 1473 + 1474 + if (rt711->first_hw_init) { 1475 + regcache_cache_bypass(rt711->regmap, false); 1476 + regcache_mark_dirty(rt711->regmap); 1477 + } else 1478 + rt711->first_hw_init = true; 1479 + 1480 + /* Mark Slave initialization complete */ 1481 + rt711->hw_init = true; 1482 + 1483 + pm_runtime_mark_last_busy(&slave->dev); 1484 + pm_runtime_put_autosuspend(&slave->dev); 1485 + 1486 + dev_dbg(&slave->dev, "%s hw_init complete\n", __func__); 1487 + return 0; 1488 + } 1489 + 1490 + MODULE_DESCRIPTION("ASoC RT711 SDCA SDW driver"); 1491 + MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>"); 1492 + MODULE_LICENSE("GPL");
+238
sound/soc/codecs/rt711-sdca.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + /* 3 + * rt711-sdca.h -- RT711 SDCA ALSA SoC audio driver header 4 + * 5 + * Copyright(c) 2021 Realtek Semiconductor Corp. 6 + */ 7 + 8 + #ifndef __RT711_SDCA_H__ 9 + #define __RT711_SDCA_H__ 10 + 11 + #include <linux/pm.h> 12 + #include <linux/regmap.h> 13 + #include <linux/soundwire/sdw.h> 14 + #include <linux/soundwire/sdw_type.h> 15 + #include <sound/soc.h> 16 + #include <linux/workqueue.h> 17 + 18 + struct rt711_sdca_priv { 19 + struct regmap *regmap, *mbq_regmap; 20 + struct snd_soc_component *component; 21 + struct sdw_slave *slave; 22 + enum sdw_slave_status status; 23 + struct sdw_bus_params params; 24 + bool hw_init; 25 + bool first_hw_init; 26 + struct snd_soc_jack *hs_jack; 27 + struct delayed_work jack_detect_work; 28 + struct delayed_work jack_btn_check_work; 29 + struct mutex calibrate_mutex; /* for headset calibration */ 30 + int jack_type, jd_src; 31 + unsigned int scp_sdca_stat1, scp_sdca_stat2; 32 + int hw_ver; 33 + }; 34 + 35 + struct sdw_stream_data { 36 + struct sdw_stream_runtime *sdw_stream; 37 + }; 38 + 39 + /* NID */ 40 + #define RT711_AUDIO_FUNCTION_GROUP 0x01 41 + #define RT711_DAC_OUT2 0x03 42 + #define RT711_ADC_IN1 0x09 43 + #define RT711_ADC_IN2 0x08 44 + #define RT711_DMIC1 0x12 45 + #define RT711_DMIC2 0x13 46 + #define RT711_MIC2 0x19 47 + #define RT711_LINE1 0x1a 48 + #define RT711_LINE2 0x1b 49 + #define RT711_BEEP 0x1d 50 + #define RT711_VENDOR_REG 0x20 51 + #define RT711_HP_OUT 0x21 52 + #define RT711_MIXER_IN1 0x22 53 + #define RT711_MIXER_IN2 0x23 54 + #define RT711_INLINE_CMD 0x55 55 + #define RT711_VENDOR_CALI 0x58 56 + #define RT711_VENDOR_IMS_DRE 0x5b 57 + #define RT711_VENDOR_VAD 0x5e 58 + #define RT711_VENDOR_ANALOG_CTL 0x5f 59 + #define RT711_VENDOR_HDA_CTL 0x61 60 + 61 + /* Index (NID:20h) */ 62 + #define RT711_JD_PRODUCT_NUM 0x00 63 + #define RT711_DMIC_CTL1 0x06 64 + #define RT711_JD_CTL1 0x08 65 + #define RT711_JD_CTL2 0x09 66 + #define RT711_CC_DET1 0x11 67 + #define RT711_PARA_VERB_CTL 0x1a 68 + #define RT711_COMBO_JACK_AUTO_CTL1 0x45 69 + #define RT711_COMBO_JACK_AUTO_CTL2 0x46 70 + #define RT711_COMBO_JACK_AUTO_CTL3 0x47 71 + #define RT711_INLINE_CMD_CTL 0x48 72 + #define RT711_DIGITAL_MISC_CTRL4 0x4a 73 + #define RT711_JD_CTRL6 0x6a 74 + #define RT711_VREFOUT_CTL 0x6b 75 + #define RT711_GPIO_TEST_MODE_CTL2 0x6d 76 + #define RT711_FSM_CTL 0x6f 77 + #define RT711_IRQ_FLAG_TABLE1 0x80 78 + #define RT711_IRQ_FLAG_TABLE2 0x81 79 + #define RT711_IRQ_FLAG_TABLE3 0x82 80 + #define RT711_HP_FSM_CTL 0x83 81 + #define RT711_TX_RX_MUX_CTL 0x91 82 + #define RT711_FILTER_SRC_SEL 0xb0 83 + #define RT711_ADC27_VOL_SET 0xb7 84 + 85 + /* Index (NID:58h) */ 86 + #define RT711_DAC_DC_CALI_CTL1 0x00 87 + #define RT711_DAC_DC_CALI_CTL2 0x01 88 + 89 + /* Index (NID:5bh) */ 90 + #define RT711_IMS_DIGITAL_CTL1 0x00 91 + #define RT711_HP_IMS_RESULT_L 0x20 92 + #define RT711_HP_IMS_RESULT_R 0x21 93 + 94 + /* Index (NID:5eh) */ 95 + #define RT711_VAD_SRAM_CTL1 0x10 96 + 97 + /* Index (NID:5fh) */ 98 + #define RT711_MISC_POWER_CTL0 0x01 99 + #define RT711_MISC_POWER_CTL4 0x05 100 + 101 + /* Index (NID:61h) */ 102 + #define RT711_HDA_LEGACY_MUX_CTL1 0x00 103 + #define RT711_HDA_LEGACY_UNSOLICITED_CTL 0x03 104 + #define RT711_HDA_LEGACY_CONFIG_CTL 0x06 105 + #define RT711_HDA_LEGACY_RESET_CTL 0x08 106 + #define RT711_HDA_LEGACY_GPIO_CTL 0x0a 107 + #define RT711_ADC08_09_PDE_CTL 0x24 108 + #define RT711_GE_MODE_RELATED_CTL 0x35 109 + #define RT711_PUSH_BTN_INT_CTL0 0x36 110 + #define RT711_PUSH_BTN_INT_CTL1 0x37 111 + #define RT711_PUSH_BTN_INT_CTL2 0x38 112 + #define RT711_PUSH_BTN_INT_CTL6 0x3c 113 + #define RT711_PUSH_BTN_INT_CTL7 0x3d 114 + #define RT711_PUSH_BTN_INT_CTL9 0x3f 115 + 116 + /* DAC DC offset calibration control-1 (0x00)(NID:20h) */ 117 + #define RT711_DAC_DC_CALI_TRIGGER (0x1 << 15) 118 + #define RT711_DAC_DC_CALI_CLK_EN (0x1 << 14) 119 + #define RT711_DAC_DC_FORCE_CALI_RST (0x1 << 3) 120 + 121 + /* jack detect control 1 (0x08)(NID:20h) */ 122 + #define RT711_JD2_DIGITAL_MODE_SEL (0x1 << 1) 123 + 124 + /* jack detect control 2 (0x09)(NID:20h) */ 125 + #define RT711_JD2_2PORT_200K_DECODE_HP (0x1 << 13) 126 + #define RT711_HP_JD_SEL_JD1 (0x0 << 1) 127 + #define RT711_HP_JD_SEL_JD2 (0x1 << 1) 128 + 129 + /* CC DET1 (0x11)(NID:20h) */ 130 + #define RT711_HP_JD_FINAL_RESULT_CTL_JD12 (0x1 << 10) 131 + #define RT711_HP_JD_FINAL_RESULT_CTL_CCDET (0x0 << 10) 132 + 133 + /* Parameter & Verb control (0x1a)(NID:20h) */ 134 + #define RT711_HIDDEN_REG_SW_RESET (0x1 << 14) 135 + 136 + /* combo jack auto switch control 2 (0x46)(NID:20h) */ 137 + #define RT711_COMBOJACK_AUTO_DET_STATUS (0x1 << 11) 138 + #define RT711_COMBOJACK_AUTO_DET_TRS (0x1 << 10) 139 + #define RT711_COMBOJACK_AUTO_DET_CTIA (0x1 << 9) 140 + #define RT711_COMBOJACK_AUTO_DET_OMTP (0x1 << 8) 141 + 142 + /* FSM control (0x6f)(NID:20h) */ 143 + #define RT711_CALI_CTL (0x0 << 0) 144 + #define RT711_COMBOJACK_CTL (0x1 << 0) 145 + #define RT711_IMS_CTL (0x2 << 0) 146 + #define RT711_DEPOP_CTL (0x3 << 0) 147 + #define RT711_FSM_IMP_EN (0x1 << 6) 148 + 149 + /* Impedance Sense Digital Control 1 (0x00)(NID:5bh) */ 150 + #define RT711_TRIGGER_IMS (0x1 << 15) 151 + #define RT711_IMS_EN (0x1 << 6) 152 + 153 + #define RT711_EAPD_HIGH 0x2 154 + #define RT711_EAPD_LOW 0x0 155 + #define RT711_MUTE_SFT 7 156 + /* set input/output mapping to payload[14][15] separately */ 157 + #define RT711_DIR_IN_SFT 6 158 + #define RT711_DIR_OUT_SFT 7 159 + 160 + /* RC Calibration register */ 161 + #define RT711_RC_CAL_STATUS 0x320c 162 + 163 + /* Buffer address for HID */ 164 + #define RT711_BUF_ADDR_HID1 0x44030000 165 + #define RT711_BUF_ADDR_HID2 0x44030020 166 + 167 + /* RT711 SDCA Control - function number */ 168 + #define FUNC_NUM_JACK_CODEC 0x01 169 + #define FUNC_NUM_MIC_ARRAY 0x02 170 + #define FUNC_NUM_HID 0x03 171 + 172 + /* RT711 SDCA entity */ 173 + #define RT711_SDCA_ENT_HID01 0x01 174 + #define RT711_SDCA_ENT_GE49 0x49 175 + #define RT711_SDCA_ENT_USER_FU05 0x05 176 + #define RT711_SDCA_ENT_USER_FU0F 0x0f 177 + #define RT711_SDCA_ENT_USER_FU1E 0x1e 178 + #define RT711_SDCA_ENT_PLATFORM_FU15 0x15 179 + #define RT711_SDCA_ENT_PLATFORM_FU44 0x44 180 + #define RT711_SDCA_ENT_PDE28 0x28 181 + #define RT711_SDCA_ENT_PDE29 0x29 182 + #define RT711_SDCA_ENT_PDE2A 0x2a 183 + #define RT711_SDCA_ENT_CS01 0x01 184 + #define RT711_SDCA_ENT_CS11 0x11 185 + #define RT711_SDCA_ENT_CS1F 0x1f 186 + #define RT711_SDCA_ENT_OT1 0x06 187 + #define RT711_SDCA_ENT_LINE1 0x09 188 + #define RT711_SDCA_ENT_LINE2 0x31 189 + #define RT711_SDCA_ENT_PDELINE2 0x36 190 + #define RT711_SDCA_ENT_USER_FU9 0x41 191 + 192 + /* RT711 SDCA control */ 193 + #define RT711_SDCA_CTL_SAMPLE_FREQ_INDEX 0x10 194 + #define RT711_SDCA_CTL_FU_CH_GAIN 0x0b 195 + #define RT711_SDCA_CTL_FU_MUTE 0x01 196 + #define RT711_SDCA_CTL_FU_VOLUME 0x02 197 + #define RT711_SDCA_CTL_HIDTX_CURRENT_OWNER 0x10 198 + #define RT711_SDCA_CTL_HIDTX_SET_OWNER_TO_DEVICE 0x11 199 + #define RT711_SDCA_CTL_HIDTX_MESSAGE_OFFSET 0x12 200 + #define RT711_SDCA_CTL_HIDTX_MESSAGE_LENGTH 0x13 201 + #define RT711_SDCA_CTL_SELECTED_MODE 0x01 202 + #define RT711_SDCA_CTL_DETECTED_MODE 0x02 203 + #define RT711_SDCA_CTL_REQ_POWER_STATE 0x01 204 + #define RT711_SDCA_CTL_VENDOR_DEF 0x30 205 + 206 + /* RT711 SDCA channel */ 207 + #define CH_L 0x01 208 + #define CH_R 0x02 209 + 210 + /* sample frequency index */ 211 + #define RT711_SDCA_RATE_44100HZ 0x08 212 + #define RT711_SDCA_RATE_48000HZ 0x09 213 + #define RT711_SDCA_RATE_96000HZ 0x0b 214 + #define RT711_SDCA_RATE_192000HZ 0x0d 215 + 216 + enum { 217 + RT711_AIF1, 218 + RT711_AIF2, 219 + RT711_AIFS, 220 + }; 221 + 222 + enum rt711_sdca_jd_src { 223 + RT711_JD_NULL, 224 + RT711_JD1, 225 + RT711_JD2 226 + }; 227 + 228 + enum rt711_sdca_ver { 229 + RT711_VER_VD0, 230 + RT711_VER_VD1 231 + }; 232 + 233 + int rt711_sdca_io_init(struct device *dev, struct sdw_slave *slave); 234 + int rt711_sdca_init(struct device *dev, struct regmap *regmap, 235 + struct regmap *mbq_regmap, struct sdw_slave *slave); 236 + 237 + int rt711_sdca_jack_detect(struct rt711_sdca_priv *rt711, bool *hp, bool *mic); 238 + #endif /* __RT711_SDCA_H__ */