1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // rt721-sdca-sdw.c -- rt721 SDCA ALSA SoC audio driver 4 // 5 // Copyright(c) 2024 Realtek Semiconductor Corp. 6 // 7 // 8 9 #include <linux/delay.h> 10 #include <linux/device.h> 11 #include <linux/module.h> 12 #include <linux/mod_devicetable.h> 13 #include <linux/pm_runtime.h> 14 #include <linux/soundwire/sdw_registers.h> 15 16 #include "rt721-sdca.h" 17 #include "rt721-sdca-sdw.h" 18 #include "rt-sdw-common.h" 19 20 static bool rt721_sdca_readable_register(struct device *dev, unsigned int reg) 21 { 22 switch (reg) { 23 case 0x2f01 ... 0x2f0a: 24 case 0x2f35: 25 case 0x2f50: 26 case 0x2f51: 27 case 0x2f58 ... 0x2f5d: 28 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT721_SDCA_ENT_XUV, 29 RT721_SDCA_CTL_XUV, 0): 30 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT721_SDCA_ENT_GE49, 31 RT721_SDCA_CTL_SELECTED_MODE, 0): 32 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT721_SDCA_ENT_GE49, 33 RT721_SDCA_CTL_DETECTED_MODE, 0): 34 case SDW_SDCA_CTL(FUNC_NUM_HID, RT721_SDCA_ENT_HID01, 35 RT721_SDCA_CTL_HIDTX_CURRENT_OWNER, 0) ... SDW_SDCA_CTL(FUNC_NUM_HID, 36 RT721_SDCA_ENT_HID01, RT721_SDCA_CTL_HIDTX_MESSAGE_LENGTH, 0): 37 case RT721_BUF_ADDR_HID1 ... RT721_BUF_ADDR_HID2: 38 return true; 39 default: 40 return false; 41 } 42 } 43 44 static bool rt721_sdca_volatile_register(struct device *dev, unsigned int reg) 45 { 46 switch (reg) { 47 case 0x2f01: 48 case 0x2f51: 49 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT721_SDCA_ENT_GE49, 50 RT721_SDCA_CTL_DETECTED_MODE, 0): 51 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT721_SDCA_ENT_XUV, 52 RT721_SDCA_CTL_XUV, 0): 53 case SDW_SDCA_CTL(FUNC_NUM_HID, RT721_SDCA_ENT_HID01, 54 RT721_SDCA_CTL_HIDTX_CURRENT_OWNER, 0) ... SDW_SDCA_CTL(FUNC_NUM_HID, 55 RT721_SDCA_ENT_HID01, RT721_SDCA_CTL_HIDTX_MESSAGE_LENGTH, 0): 56 case RT721_BUF_ADDR_HID1 ... RT721_BUF_ADDR_HID2: 57 return true; 58 default: 59 return false; 60 } 61 } 62 63 static bool rt721_sdca_mbq_readable_register(struct device *dev, unsigned int reg) 64 { 65 switch (reg) { 66 case 0x0900007: 67 case 0x0a00005: 68 case 0x0c00005: 69 case 0x0d00014: 70 case 0x0310100: 71 case 0x2000001: 72 case 0x2000002: 73 case 0x2000003: 74 case 0x2000013: 75 case 0x200003c: 76 case 0x2000046: 77 case 0x5810000: 78 case 0x5810036: 79 case 0x5810037: 80 case 0x5810038: 81 case 0x5810039: 82 case 0x5b10018: 83 case 0x5b10019: 84 case 0x5f00045: 85 case 0x5f00048: 86 case 0x6100000: 87 case 0x6100005: 88 case 0x6100006: 89 case 0x610000d: 90 case 0x6100010: 91 case 0x6100011: 92 case 0x6100013: 93 case 0x6100015: 94 case 0x6100017: 95 case 0x6100025: 96 case 0x6100029: 97 case 0x610002c ... 0x610002f: 98 case 0x6100053 ... 0x6100055: 99 case 0x6100057: 100 case 0x610005a: 101 case 0x610005b: 102 case 0x610006a: 103 case 0x610006d: 104 case 0x6100092: 105 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT721_SDCA_ENT_USER_FU05, RT721_SDCA_CTL_FU_VOLUME, 106 CH_L): 107 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT721_SDCA_ENT_USER_FU05, RT721_SDCA_CTL_FU_VOLUME, 108 CH_R): 109 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT721_SDCA_ENT_USER_FU0F, RT721_SDCA_CTL_FU_VOLUME, 110 CH_L): 111 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT721_SDCA_ENT_USER_FU0F, RT721_SDCA_CTL_FU_VOLUME, 112 CH_R): 113 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT721_SDCA_ENT_PLATFORM_FU44, 114 RT721_SDCA_CTL_FU_CH_GAIN, CH_L): 115 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT721_SDCA_ENT_PLATFORM_FU44, 116 RT721_SDCA_CTL_FU_CH_GAIN, CH_R): 117 case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT721_SDCA_ENT_USER_FU1E, RT721_SDCA_CTL_FU_VOLUME, 118 CH_01): 119 case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT721_SDCA_ENT_USER_FU1E, RT721_SDCA_CTL_FU_VOLUME, 120 CH_02): 121 case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT721_SDCA_ENT_USER_FU1E, RT721_SDCA_CTL_FU_VOLUME, 122 CH_03): 123 case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT721_SDCA_ENT_USER_FU1E, RT721_SDCA_CTL_FU_VOLUME, 124 CH_04): 125 case SDW_SDCA_CTL(FUNC_NUM_AMP, RT721_SDCA_ENT_USER_FU06, RT721_SDCA_CTL_FU_VOLUME, CH_L): 126 case SDW_SDCA_CTL(FUNC_NUM_AMP, RT721_SDCA_ENT_USER_FU06, RT721_SDCA_CTL_FU_VOLUME, CH_R): 127 return true; 128 default: 129 return false; 130 } 131 } 132 133 static bool rt721_sdca_mbq_volatile_register(struct device *dev, unsigned int reg) 134 { 135 switch (reg) { 136 case 0x0310100: 137 case 0x0a00005: 138 case 0x0c00005: 139 case 0x0d00014: 140 case 0x2000000: 141 case 0x200000d: 142 case 0x2000019: 143 case 0x2000020: 144 case 0x2000030: 145 case 0x2000046: 146 case 0x2000067: 147 case 0x2000084: 148 case 0x2000086: 149 case 0x5810000: 150 case 0x5810036: 151 case 0x5810037: 152 case 0x5810038: 153 case 0x5810039: 154 case 0x5b10018: 155 case 0x5b10019: 156 return true; 157 default: 158 return false; 159 } 160 } 161 162 static const struct regmap_config rt721_sdca_regmap = { 163 .reg_bits = 32, 164 .val_bits = 8, 165 .readable_reg = rt721_sdca_readable_register, 166 .volatile_reg = rt721_sdca_volatile_register, 167 .max_register = 0x44ffffff, 168 .reg_defaults = rt721_sdca_reg_defaults, 169 .num_reg_defaults = ARRAY_SIZE(rt721_sdca_reg_defaults), 170 .cache_type = REGCACHE_MAPLE, 171 .use_single_read = true, 172 .use_single_write = true, 173 }; 174 175 static const struct regmap_config rt721_sdca_mbq_regmap = { 176 .name = "sdw-mbq", 177 .reg_bits = 32, 178 .val_bits = 16, 179 .readable_reg = rt721_sdca_mbq_readable_register, 180 .volatile_reg = rt721_sdca_mbq_volatile_register, 181 .max_register = 0x41000312, 182 .reg_defaults = rt721_sdca_mbq_defaults, 183 .num_reg_defaults = ARRAY_SIZE(rt721_sdca_mbq_defaults), 184 .cache_type = REGCACHE_MAPLE, 185 .use_single_read = true, 186 .use_single_write = true, 187 }; 188 189 static int rt721_sdca_update_status(struct sdw_slave *slave, 190 enum sdw_slave_status status) 191 { 192 struct rt721_sdca_priv *rt721 = dev_get_drvdata(&slave->dev); 193 194 if (status == SDW_SLAVE_UNATTACHED) 195 rt721->hw_init = false; 196 197 if (status == SDW_SLAVE_ATTACHED) { 198 if (rt721->hs_jack) { 199 /* 200 * Due to the SCP_SDCA_INTMASK will be cleared by any reset, and then 201 * if the device attached again, we will need to set the setting back. 202 * It could avoid losing the jack detection interrupt. 203 * This also could sync with the cache value as the rt721_sdca_jack_init set. 204 */ 205 sdw_write_no_pm(rt721->slave, SDW_SCP_SDCA_INTMASK1, 206 SDW_SCP_SDCA_INTMASK_SDCA_0); 207 sdw_write_no_pm(rt721->slave, SDW_SCP_SDCA_INTMASK2, 208 SDW_SCP_SDCA_INTMASK_SDCA_8); 209 } 210 } 211 212 /* 213 * Perform initialization only if slave status is present and 214 * hw_init flag is false 215 */ 216 if (rt721->hw_init || status != SDW_SLAVE_ATTACHED) 217 return 0; 218 219 /* perform I/O transfers required for Slave initialization */ 220 return rt721_sdca_io_init(&slave->dev, slave); 221 } 222 223 static int rt721_sdca_read_prop(struct sdw_slave *slave) 224 { 225 struct sdw_slave_prop *prop = &slave->prop; 226 int nval; 227 int i, j; 228 u32 bit; 229 unsigned long addr; 230 struct sdw_dpn_prop *dpn; 231 232 sdw_slave_read_prop(slave); 233 prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY; 234 prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY; 235 236 prop->paging_support = true; 237 238 /* 239 * port = 1 for headphone playback 240 * port = 2 for headset-mic capture 241 * port = 3 for speaker playback 242 * port = 6 for digital-mic capture 243 */ 244 prop->source_ports = BIT(6) | BIT(2); /* BITMAP: 01000100 */ 245 prop->sink_ports = BIT(3) | BIT(1); /* BITMAP: 00001010 */ 246 247 nval = hweight32(prop->source_ports); 248 prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval, 249 sizeof(*prop->src_dpn_prop), GFP_KERNEL); 250 if (!prop->src_dpn_prop) 251 return -ENOMEM; 252 253 i = 0; 254 dpn = prop->src_dpn_prop; 255 addr = prop->source_ports; 256 for_each_set_bit(bit, &addr, 32) { 257 dpn[i].num = bit; 258 dpn[i].type = SDW_DPN_FULL; 259 dpn[i].simple_ch_prep_sm = true; 260 dpn[i].ch_prep_timeout = 10; 261 i++; 262 } 263 264 /* do this again for sink now */ 265 nval = hweight32(prop->sink_ports); 266 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval, 267 sizeof(*prop->sink_dpn_prop), GFP_KERNEL); 268 if (!prop->sink_dpn_prop) 269 return -ENOMEM; 270 271 j = 0; 272 dpn = prop->sink_dpn_prop; 273 addr = prop->sink_ports; 274 for_each_set_bit(bit, &addr, 32) { 275 dpn[j].num = bit; 276 dpn[j].type = SDW_DPN_FULL; 277 dpn[j].simple_ch_prep_sm = true; 278 dpn[j].ch_prep_timeout = 10; 279 j++; 280 } 281 282 /* set the timeout values */ 283 prop->clk_stop_timeout = 1380; 284 285 /* wake-up event */ 286 prop->wake_capable = 1; 287 288 /* Three data lanes are supported by rt721-sdca codec */ 289 prop->lane_control_support = true; 290 291 return 0; 292 } 293 294 static int rt721_sdca_interrupt_callback(struct sdw_slave *slave, 295 struct sdw_slave_intr_status *status) 296 { 297 struct rt721_sdca_priv *rt721 = dev_get_drvdata(&slave->dev); 298 int ret, stat; 299 int count = 0, retry = 3; 300 unsigned int sdca_cascade, scp_sdca_stat1, scp_sdca_stat2 = 0; 301 302 if (cancel_delayed_work_sync(&rt721->jack_detect_work)) { 303 dev_warn(&slave->dev, "%s the pending delayed_work was cancelled", __func__); 304 /* avoid the HID owner doesn't change to device */ 305 if (rt721->scp_sdca_stat2) 306 scp_sdca_stat2 = rt721->scp_sdca_stat2; 307 } 308 309 /* 310 * The critical section below intentionally protects a rather large piece of code. 311 * We don't want to allow the system suspend to disable an interrupt while we are 312 * processing it, which could be problematic given the quirky SoundWire interrupt 313 * scheme. We do want however to prevent new workqueues from being scheduled if 314 * the disable_irq flag was set during system suspend. 315 */ 316 mutex_lock(&rt721->disable_irq_lock); 317 318 ret = sdw_read_no_pm(rt721->slave, SDW_SCP_SDCA_INT1); 319 if (ret < 0) 320 goto io_error; 321 322 rt721->scp_sdca_stat1 = ret; 323 ret = sdw_read_no_pm(rt721->slave, SDW_SCP_SDCA_INT2); 324 if (ret < 0) 325 goto io_error; 326 327 rt721->scp_sdca_stat2 = ret; 328 if (scp_sdca_stat2) 329 rt721->scp_sdca_stat2 |= scp_sdca_stat2; 330 do { 331 /* clear flag */ 332 ret = sdw_read_no_pm(rt721->slave, SDW_SCP_SDCA_INT1); 333 if (ret < 0) 334 goto io_error; 335 if (ret & SDW_SCP_SDCA_INTMASK_SDCA_0) { 336 ret = sdw_update_no_pm(rt721->slave, SDW_SCP_SDCA_INT1, 337 SDW_SCP_SDCA_INT_SDCA_0, SDW_SCP_SDCA_INT_SDCA_0); 338 if (ret < 0) 339 goto io_error; 340 } 341 ret = sdw_read_no_pm(rt721->slave, SDW_SCP_SDCA_INT2); 342 if (ret < 0) 343 goto io_error; 344 if (ret & SDW_SCP_SDCA_INTMASK_SDCA_8) { 345 ret = sdw_write_no_pm(rt721->slave, SDW_SCP_SDCA_INT2, 346 SDW_SCP_SDCA_INTMASK_SDCA_8); 347 if (ret < 0) 348 goto io_error; 349 } 350 351 /* check if flag clear or not */ 352 ret = sdw_read_no_pm(rt721->slave, SDW_DP0_INT); 353 if (ret < 0) 354 goto io_error; 355 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE; 356 357 ret = sdw_read_no_pm(rt721->slave, SDW_SCP_SDCA_INT1); 358 if (ret < 0) 359 goto io_error; 360 scp_sdca_stat1 = ret & SDW_SCP_SDCA_INTMASK_SDCA_0; 361 362 ret = sdw_read_no_pm(rt721->slave, SDW_SCP_SDCA_INT2); 363 if (ret < 0) 364 goto io_error; 365 scp_sdca_stat2 = ret & SDW_SCP_SDCA_INTMASK_SDCA_8; 366 367 stat = scp_sdca_stat1 || scp_sdca_stat2 || sdca_cascade; 368 369 count++; 370 } while (stat != 0 && count < retry); 371 372 if (stat) 373 dev_warn(&slave->dev, 374 "%s scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__, 375 rt721->scp_sdca_stat1, rt721->scp_sdca_stat2); 376 ret = sdw_read_no_pm(rt721->slave, SDW_SCP_SDCA_INT1); 377 ret = sdw_read_no_pm(rt721->slave, SDW_SCP_SDCA_INT2); 378 379 if (status->sdca_cascade && !rt721->disable_irq) 380 mod_delayed_work(system_power_efficient_wq, 381 &rt721->jack_detect_work, msecs_to_jiffies(280)); 382 383 mutex_unlock(&rt721->disable_irq_lock); 384 385 return 0; 386 387 io_error: 388 mutex_unlock(&rt721->disable_irq_lock); 389 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); 390 return ret; 391 } 392 393 static const struct sdw_slave_ops rt721_sdca_slave_ops = { 394 .read_prop = rt721_sdca_read_prop, 395 .interrupt_callback = rt721_sdca_interrupt_callback, 396 .update_status = rt721_sdca_update_status, 397 }; 398 399 static int rt721_sdca_sdw_probe(struct sdw_slave *slave, 400 const struct sdw_device_id *id) 401 { 402 struct regmap *regmap, *mbq_regmap; 403 404 /* Regmap Initialization */ 405 mbq_regmap = devm_regmap_init_sdw_mbq(slave, &rt721_sdca_mbq_regmap); 406 if (IS_ERR(mbq_regmap)) 407 return PTR_ERR(mbq_regmap); 408 409 regmap = devm_regmap_init_sdw(slave, &rt721_sdca_regmap); 410 if (IS_ERR(regmap)) 411 return PTR_ERR(regmap); 412 413 return rt721_sdca_init(&slave->dev, regmap, mbq_regmap, slave); 414 } 415 416 static int rt721_sdca_sdw_remove(struct sdw_slave *slave) 417 { 418 struct rt721_sdca_priv *rt721 = dev_get_drvdata(&slave->dev); 419 420 if (rt721->hw_init) { 421 cancel_delayed_work_sync(&rt721->jack_detect_work); 422 cancel_delayed_work_sync(&rt721->jack_btn_check_work); 423 } 424 425 if (rt721->first_hw_init) 426 pm_runtime_disable(&slave->dev); 427 428 mutex_destroy(&rt721->calibrate_mutex); 429 mutex_destroy(&rt721->disable_irq_lock); 430 431 return 0; 432 } 433 434 static const struct sdw_device_id rt721_sdca_id[] = { 435 SDW_SLAVE_ENTRY_EXT(0x025d, 0x721, 0x3, 0x1, 0), 436 {}, 437 }; 438 MODULE_DEVICE_TABLE(sdw, rt721_sdca_id); 439 440 static int __maybe_unused rt721_sdca_dev_suspend(struct device *dev) 441 { 442 struct rt721_sdca_priv *rt721 = dev_get_drvdata(dev); 443 444 if (!rt721->hw_init) 445 return 0; 446 447 cancel_delayed_work_sync(&rt721->jack_detect_work); 448 cancel_delayed_work_sync(&rt721->jack_btn_check_work); 449 450 regcache_cache_only(rt721->regmap, true); 451 regcache_cache_only(rt721->mbq_regmap, true); 452 453 return 0; 454 } 455 456 static int __maybe_unused rt721_sdca_dev_system_suspend(struct device *dev) 457 { 458 struct rt721_sdca_priv *rt721_sdca = dev_get_drvdata(dev); 459 struct sdw_slave *slave = dev_to_sdw_dev(dev); 460 int ret1, ret2; 461 462 if (!rt721_sdca->hw_init) 463 return 0; 464 465 /* 466 * prevent new interrupts from being handled after the 467 * deferred work completes and before the parent disables 468 * interrupts on the link 469 */ 470 mutex_lock(&rt721_sdca->disable_irq_lock); 471 rt721_sdca->disable_irq = true; 472 ret1 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK1, 473 SDW_SCP_SDCA_INTMASK_SDCA_0, 0); 474 ret2 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK2, 475 SDW_SCP_SDCA_INTMASK_SDCA_8, 0); 476 mutex_unlock(&rt721_sdca->disable_irq_lock); 477 478 if (ret1 < 0 || ret2 < 0) { 479 /* log but don't prevent suspend from happening */ 480 dev_dbg(&slave->dev, "%s: could not disable SDCA interrupts\n:", __func__); 481 } 482 483 return rt721_sdca_dev_suspend(dev); 484 } 485 486 #define RT721_PROBE_TIMEOUT 5000 487 488 static int __maybe_unused rt721_sdca_dev_resume(struct device *dev) 489 { 490 struct sdw_slave *slave = dev_to_sdw_dev(dev); 491 struct rt721_sdca_priv *rt721 = dev_get_drvdata(dev); 492 unsigned long time; 493 494 if (!rt721->first_hw_init) 495 return 0; 496 497 if (!slave->unattach_request) { 498 mutex_lock(&rt721->disable_irq_lock); 499 if (rt721->disable_irq == true) { 500 sdw_write_no_pm(slave, SDW_SCP_SDCA_INTMASK1, SDW_SCP_SDCA_INTMASK_SDCA_0); 501 sdw_write_no_pm(slave, SDW_SCP_SDCA_INTMASK2, SDW_SCP_SDCA_INTMASK_SDCA_8); 502 rt721->disable_irq = false; 503 } 504 mutex_unlock(&rt721->disable_irq_lock); 505 goto regmap_sync; 506 } 507 508 time = wait_for_completion_timeout(&slave->initialization_complete, 509 msecs_to_jiffies(RT721_PROBE_TIMEOUT)); 510 if (!time) { 511 dev_err(&slave->dev, "Initialization not complete, timed out\n"); 512 sdw_show_ping_status(slave->bus, true); 513 514 return -ETIMEDOUT; 515 } 516 517 regmap_sync: 518 slave->unattach_request = 0; 519 regcache_cache_only(rt721->regmap, false); 520 regcache_sync(rt721->regmap); 521 regcache_cache_only(rt721->mbq_regmap, false); 522 regcache_sync(rt721->mbq_regmap); 523 return 0; 524 } 525 526 static const struct dev_pm_ops rt721_sdca_pm = { 527 SET_SYSTEM_SLEEP_PM_OPS(rt721_sdca_dev_system_suspend, rt721_sdca_dev_resume) 528 SET_RUNTIME_PM_OPS(rt721_sdca_dev_suspend, rt721_sdca_dev_resume, NULL) 529 }; 530 531 static struct sdw_driver rt721_sdca_sdw_driver = { 532 .driver = { 533 .name = "rt721-sdca", 534 .owner = THIS_MODULE, 535 .pm = &rt721_sdca_pm, 536 }, 537 .probe = rt721_sdca_sdw_probe, 538 .remove = rt721_sdca_sdw_remove, 539 .ops = &rt721_sdca_slave_ops, 540 .id_table = rt721_sdca_id, 541 }; 542 module_sdw_driver(rt721_sdca_sdw_driver); 543 544 MODULE_DESCRIPTION("ASoC RT721 SDCA SDW driver"); 545 MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>"); 546 MODULE_LICENSE("GPL"); 547