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