1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // rt712-sdca-sdw.c -- rt712 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/mod_devicetable.h> 12 #include <linux/module.h> 13 #include <linux/pm_runtime.h> 14 #include <linux/soundwire/sdw_registers.h> 15 #include "rt712-sdca.h" 16 #include "rt712-sdca-sdw.h" 17 18 static bool rt712_sdca_readable_register(struct device *dev, unsigned int reg) 19 { 20 switch (reg) { 21 case 0x201a ... 0x201f: 22 case 0x2029 ... 0x202a: 23 case 0x202d ... 0x2034: 24 case 0x2230 ... 0x2232: 25 case 0x2f01 ... 0x2f0a: 26 case 0x2f35 ... 0x2f36: 27 case 0x2f50: 28 case 0x2f54: 29 case 0x2f58 ... 0x2f5d: 30 case 0x3201: 31 case 0x320c: 32 case 0x3301 ... 0x3303: 33 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_GE49, RT712_SDCA_CTL_SELECTED_MODE, 0): 34 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_GE49, RT712_SDCA_CTL_DETECTED_MODE, 0): 35 case SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01, RT712_SDCA_CTL_HIDTX_CURRENT_OWNER, 0) ... 36 SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01, RT712_SDCA_CTL_HIDTX_MESSAGE_LENGTH, 0): 37 case RT712_BUF_ADDR_HID1 ... RT712_BUF_ADDR_HID2: 38 return true; 39 default: 40 return false; 41 } 42 } 43 44 static bool rt712_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 0x202d ... 0x202f: 52 case 0x2230: 53 case 0x2f01: 54 case 0x2f35: 55 case 0x320c: 56 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_GE49, RT712_SDCA_CTL_DETECTED_MODE, 0): 57 case SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01, RT712_SDCA_CTL_HIDTX_CURRENT_OWNER, 0) ... 58 SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01, RT712_SDCA_CTL_HIDTX_MESSAGE_LENGTH, 0): 59 case RT712_BUF_ADDR_HID1 ... RT712_BUF_ADDR_HID2: 60 return true; 61 default: 62 return false; 63 } 64 } 65 66 static bool rt712_sdca_mbq_readable_register(struct device *dev, unsigned int reg) 67 { 68 switch (reg) { 69 case 0x2000000 ... 0x200008e: 70 case 0x5300000 ... 0x530000e: 71 case 0x5400000 ... 0x540000e: 72 case 0x5600000 ... 0x5600008: 73 case 0x5700000 ... 0x570000d: 74 case 0x5800000 ... 0x5800021: 75 case 0x5900000 ... 0x5900028: 76 case 0x5a00000 ... 0x5a00009: 77 case 0x5b00000 ... 0x5b00051: 78 case 0x5c00000 ... 0x5c0009a: 79 case 0x5d00000 ... 0x5d00009: 80 case 0x5f00000 ... 0x5f00030: 81 case 0x6100000 ... 0x6100068: 82 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05, RT712_SDCA_CTL_FU_VOLUME, CH_L): 83 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU05, RT712_SDCA_CTL_FU_VOLUME, CH_R): 84 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F, RT712_SDCA_CTL_FU_VOLUME, CH_L): 85 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT712_SDCA_ENT_USER_FU0F, RT712_SDCA_CTL_FU_VOLUME, CH_R): 86 case SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_VOLUME, CH_L): 87 case SDW_SDCA_CTL(FUNC_NUM_AMP, RT712_SDCA_ENT_USER_FU06, RT712_SDCA_CTL_FU_VOLUME, CH_R): 88 return true; 89 default: 90 return false; 91 } 92 } 93 94 static bool rt712_sdca_mbq_volatile_register(struct device *dev, unsigned int reg) 95 { 96 switch (reg) { 97 case 0x2000000: 98 case 0x200001a: 99 case 0x2000024: 100 case 0x2000046: 101 case 0x200008a: 102 case 0x5800000: 103 case 0x5800001: 104 case 0x6100008: 105 return true; 106 default: 107 return false; 108 } 109 } 110 111 static const struct regmap_config rt712_sdca_regmap = { 112 .reg_bits = 32, 113 .val_bits = 8, 114 .readable_reg = rt712_sdca_readable_register, 115 .volatile_reg = rt712_sdca_volatile_register, 116 .max_register = 0x44ffffff, 117 .reg_defaults = rt712_sdca_reg_defaults, 118 .num_reg_defaults = ARRAY_SIZE(rt712_sdca_reg_defaults), 119 .cache_type = REGCACHE_RBTREE, 120 .use_single_read = true, 121 .use_single_write = true, 122 }; 123 124 static const struct regmap_config rt712_sdca_mbq_regmap = { 125 .name = "sdw-mbq", 126 .reg_bits = 32, 127 .val_bits = 16, 128 .readable_reg = rt712_sdca_mbq_readable_register, 129 .volatile_reg = rt712_sdca_mbq_volatile_register, 130 .max_register = 0x41000312, 131 .reg_defaults = rt712_sdca_mbq_defaults, 132 .num_reg_defaults = ARRAY_SIZE(rt712_sdca_mbq_defaults), 133 .cache_type = REGCACHE_RBTREE, 134 .use_single_read = true, 135 .use_single_write = true, 136 }; 137 138 static int rt712_sdca_update_status(struct sdw_slave *slave, 139 enum sdw_slave_status status) 140 { 141 struct rt712_sdca_priv *rt712 = dev_get_drvdata(&slave->dev); 142 143 /* Update the status */ 144 rt712->status = status; 145 146 if (status == SDW_SLAVE_UNATTACHED) 147 rt712->hw_init = false; 148 149 if (status == SDW_SLAVE_ATTACHED) { 150 if (rt712->hs_jack) { 151 /* 152 * Due to the SCP_SDCA_INTMASK will be cleared by any reset, and then 153 * if the device attached again, we will need to set the setting back. 154 * It could avoid losing the jack detection interrupt. 155 * This also could sync with the cache value as the rt712_sdca_jack_init set. 156 */ 157 sdw_write_no_pm(rt712->slave, SDW_SCP_SDCA_INTMASK1, 158 SDW_SCP_SDCA_INTMASK_SDCA_0); 159 sdw_write_no_pm(rt712->slave, SDW_SCP_SDCA_INTMASK2, 160 SDW_SCP_SDCA_INTMASK_SDCA_8); 161 } 162 } 163 164 /* 165 * Perform initialization only if slave status is present and 166 * hw_init flag is false 167 */ 168 if (rt712->hw_init || rt712->status != SDW_SLAVE_ATTACHED) 169 return 0; 170 171 /* perform I/O transfers required for Slave initialization */ 172 return rt712_sdca_io_init(&slave->dev, slave); 173 } 174 175 static int rt712_sdca_read_prop(struct sdw_slave *slave) 176 { 177 struct sdw_slave_prop *prop = &slave->prop; 178 int nval; 179 int i, j; 180 u32 bit; 181 unsigned long addr; 182 struct sdw_dpn_prop *dpn; 183 184 prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY; 185 prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY; 186 187 prop->paging_support = true; 188 189 /* first we need to allocate memory for set bits in port lists */ 190 prop->source_ports = BIT(4); /* BITMAP: 00010000 */ 191 prop->sink_ports = BIT(3) | BIT(1); /* BITMAP: 00001010 */ 192 193 nval = hweight32(prop->source_ports); 194 prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval, 195 sizeof(*prop->src_dpn_prop), GFP_KERNEL); 196 if (!prop->src_dpn_prop) 197 return -ENOMEM; 198 199 i = 0; 200 dpn = prop->src_dpn_prop; 201 addr = prop->source_ports; 202 for_each_set_bit(bit, &addr, 32) { 203 dpn[i].num = bit; 204 dpn[i].type = SDW_DPN_FULL; 205 dpn[i].simple_ch_prep_sm = true; 206 dpn[i].ch_prep_timeout = 10; 207 i++; 208 } 209 210 /* do this again for sink now */ 211 nval = hweight32(prop->sink_ports); 212 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval, 213 sizeof(*prop->sink_dpn_prop), GFP_KERNEL); 214 if (!prop->sink_dpn_prop) 215 return -ENOMEM; 216 217 j = 0; 218 dpn = prop->sink_dpn_prop; 219 addr = prop->sink_ports; 220 for_each_set_bit(bit, &addr, 32) { 221 dpn[j].num = bit; 222 dpn[j].type = SDW_DPN_FULL; 223 dpn[j].simple_ch_prep_sm = true; 224 dpn[j].ch_prep_timeout = 10; 225 j++; 226 } 227 228 /* set the timeout values */ 229 prop->clk_stop_timeout = 1380; 230 231 /* wake-up event */ 232 prop->wake_capable = 1; 233 234 return 0; 235 } 236 237 static int rt712_sdca_interrupt_callback(struct sdw_slave *slave, 238 struct sdw_slave_intr_status *status) 239 { 240 struct rt712_sdca_priv *rt712 = dev_get_drvdata(&slave->dev); 241 int ret, stat; 242 int count = 0, retry = 3; 243 unsigned int sdca_cascade, scp_sdca_stat1, scp_sdca_stat2 = 0; 244 245 dev_dbg(&slave->dev, 246 "%s control_port_stat=%x, sdca_cascade=%x", __func__, 247 status->control_port, status->sdca_cascade); 248 249 if (cancel_delayed_work_sync(&rt712->jack_detect_work)) { 250 dev_warn(&slave->dev, "%s the pending delayed_work was cancelled", __func__); 251 /* avoid the HID owner doesn't change to device */ 252 if (rt712->scp_sdca_stat2) 253 scp_sdca_stat2 = rt712->scp_sdca_stat2; 254 } 255 256 /* 257 * The critical section below intentionally protects a rather large piece of code. 258 * We don't want to allow the system suspend to disable an interrupt while we are 259 * processing it, which could be problematic given the quirky SoundWire interrupt 260 * scheme. We do want however to prevent new workqueues from being scheduled if 261 * the disable_irq flag was set during system suspend. 262 */ 263 mutex_lock(&rt712->disable_irq_lock); 264 265 ret = sdw_read_no_pm(rt712->slave, SDW_SCP_SDCA_INT1); 266 if (ret < 0) 267 goto io_error; 268 rt712->scp_sdca_stat1 = ret; 269 ret = sdw_read_no_pm(rt712->slave, SDW_SCP_SDCA_INT2); 270 if (ret < 0) 271 goto io_error; 272 rt712->scp_sdca_stat2 = ret; 273 if (scp_sdca_stat2) 274 rt712->scp_sdca_stat2 |= scp_sdca_stat2; 275 276 do { 277 /* clear flag */ 278 ret = sdw_read_no_pm(rt712->slave, SDW_SCP_SDCA_INT1); 279 if (ret < 0) 280 goto io_error; 281 if (ret & SDW_SCP_SDCA_INTMASK_SDCA_0) { 282 ret = sdw_write_no_pm(rt712->slave, SDW_SCP_SDCA_INT1, 283 SDW_SCP_SDCA_INTMASK_SDCA_0); 284 if (ret < 0) 285 goto io_error; 286 } 287 ret = sdw_read_no_pm(rt712->slave, SDW_SCP_SDCA_INT2); 288 if (ret < 0) 289 goto io_error; 290 if (ret & SDW_SCP_SDCA_INTMASK_SDCA_8) { 291 ret = sdw_write_no_pm(rt712->slave, SDW_SCP_SDCA_INT2, 292 SDW_SCP_SDCA_INTMASK_SDCA_8); 293 if (ret < 0) 294 goto io_error; 295 } 296 297 /* check if flag clear or not */ 298 ret = sdw_read_no_pm(rt712->slave, SDW_DP0_INT); 299 if (ret < 0) 300 goto io_error; 301 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE; 302 303 ret = sdw_read_no_pm(rt712->slave, SDW_SCP_SDCA_INT1); 304 if (ret < 0) 305 goto io_error; 306 scp_sdca_stat1 = ret & SDW_SCP_SDCA_INTMASK_SDCA_0; 307 308 ret = sdw_read_no_pm(rt712->slave, SDW_SCP_SDCA_INT2); 309 if (ret < 0) 310 goto io_error; 311 scp_sdca_stat2 = ret & SDW_SCP_SDCA_INTMASK_SDCA_8; 312 313 stat = scp_sdca_stat1 || scp_sdca_stat2 || sdca_cascade; 314 315 count++; 316 } while (stat != 0 && count < retry); 317 318 if (stat) 319 dev_warn(&slave->dev, 320 "%s scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__, 321 rt712->scp_sdca_stat1, rt712->scp_sdca_stat2); 322 323 if (status->sdca_cascade && !rt712->disable_irq) 324 mod_delayed_work(system_power_efficient_wq, 325 &rt712->jack_detect_work, msecs_to_jiffies(30)); 326 327 mutex_unlock(&rt712->disable_irq_lock); 328 329 return 0; 330 331 io_error: 332 mutex_unlock(&rt712->disable_irq_lock); 333 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret); 334 return ret; 335 } 336 337 static struct sdw_slave_ops rt712_sdca_slave_ops = { 338 .read_prop = rt712_sdca_read_prop, 339 .interrupt_callback = rt712_sdca_interrupt_callback, 340 .update_status = rt712_sdca_update_status, 341 }; 342 343 static int rt712_sdca_sdw_probe(struct sdw_slave *slave, 344 const struct sdw_device_id *id) 345 { 346 struct regmap *regmap, *mbq_regmap; 347 348 /* Regmap Initialization */ 349 mbq_regmap = devm_regmap_init_sdw_mbq(slave, &rt712_sdca_mbq_regmap); 350 if (IS_ERR(mbq_regmap)) 351 return PTR_ERR(mbq_regmap); 352 353 regmap = devm_regmap_init_sdw(slave, &rt712_sdca_regmap); 354 if (IS_ERR(regmap)) 355 return PTR_ERR(regmap); 356 357 return rt712_sdca_init(&slave->dev, regmap, mbq_regmap, slave); 358 } 359 360 static int rt712_sdca_sdw_remove(struct sdw_slave *slave) 361 { 362 struct rt712_sdca_priv *rt712 = dev_get_drvdata(&slave->dev); 363 364 if (rt712->hw_init) { 365 cancel_delayed_work_sync(&rt712->jack_detect_work); 366 cancel_delayed_work_sync(&rt712->jack_btn_check_work); 367 } 368 369 if (rt712->first_hw_init) 370 pm_runtime_disable(&slave->dev); 371 372 mutex_destroy(&rt712->calibrate_mutex); 373 mutex_destroy(&rt712->disable_irq_lock); 374 375 return 0; 376 } 377 378 static const struct sdw_device_id rt712_sdca_id[] = { 379 SDW_SLAVE_ENTRY_EXT(0x025d, 0x712, 0x3, 0x1, 0), 380 SDW_SLAVE_ENTRY_EXT(0x025d, 0x713, 0x3, 0x1, 0), 381 SDW_SLAVE_ENTRY_EXT(0x025d, 0x716, 0x3, 0x1, 0), 382 SDW_SLAVE_ENTRY_EXT(0x025d, 0x717, 0x3, 0x1, 0), 383 {}, 384 }; 385 MODULE_DEVICE_TABLE(sdw, rt712_sdca_id); 386 387 static int __maybe_unused rt712_sdca_dev_suspend(struct device *dev) 388 { 389 struct rt712_sdca_priv *rt712 = dev_get_drvdata(dev); 390 391 if (!rt712->hw_init) 392 return 0; 393 394 cancel_delayed_work_sync(&rt712->jack_detect_work); 395 cancel_delayed_work_sync(&rt712->jack_btn_check_work); 396 397 regcache_cache_only(rt712->regmap, true); 398 regcache_cache_only(rt712->mbq_regmap, true); 399 400 return 0; 401 } 402 403 static int __maybe_unused rt712_sdca_dev_system_suspend(struct device *dev) 404 { 405 struct rt712_sdca_priv *rt712_sdca = dev_get_drvdata(dev); 406 struct sdw_slave *slave = dev_to_sdw_dev(dev); 407 int ret1, ret2; 408 409 if (!rt712_sdca->hw_init) 410 return 0; 411 412 /* 413 * prevent new interrupts from being handled after the 414 * deferred work completes and before the parent disables 415 * interrupts on the link 416 */ 417 mutex_lock(&rt712_sdca->disable_irq_lock); 418 rt712_sdca->disable_irq = true; 419 ret1 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK1, 420 SDW_SCP_SDCA_INTMASK_SDCA_0, 0); 421 ret2 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK2, 422 SDW_SCP_SDCA_INTMASK_SDCA_8, 0); 423 mutex_unlock(&rt712_sdca->disable_irq_lock); 424 425 if (ret1 < 0 || ret2 < 0) { 426 /* log but don't prevent suspend from happening */ 427 dev_dbg(&slave->dev, "%s: could not disable SDCA interrupts\n:", __func__); 428 } 429 430 return rt712_sdca_dev_suspend(dev); 431 } 432 433 #define RT712_PROBE_TIMEOUT 5000 434 435 static int __maybe_unused rt712_sdca_dev_resume(struct device *dev) 436 { 437 struct sdw_slave *slave = dev_to_sdw_dev(dev); 438 struct rt712_sdca_priv *rt712 = dev_get_drvdata(dev); 439 unsigned long time; 440 441 if (!rt712->first_hw_init) 442 return 0; 443 444 if (!slave->unattach_request) 445 goto regmap_sync; 446 447 time = wait_for_completion_timeout(&slave->initialization_complete, 448 msecs_to_jiffies(RT712_PROBE_TIMEOUT)); 449 if (!time) { 450 dev_err(&slave->dev, "Initialization not complete, timed out\n"); 451 sdw_show_ping_status(slave->bus, true); 452 453 return -ETIMEDOUT; 454 } 455 456 regmap_sync: 457 slave->unattach_request = 0; 458 regcache_cache_only(rt712->regmap, false); 459 regcache_sync(rt712->regmap); 460 regcache_cache_only(rt712->mbq_regmap, false); 461 regcache_sync(rt712->mbq_regmap); 462 return 0; 463 } 464 465 static const struct dev_pm_ops rt712_sdca_pm = { 466 SET_SYSTEM_SLEEP_PM_OPS(rt712_sdca_dev_system_suspend, rt712_sdca_dev_resume) 467 SET_RUNTIME_PM_OPS(rt712_sdca_dev_suspend, rt712_sdca_dev_resume, NULL) 468 }; 469 470 static struct sdw_driver rt712_sdca_sdw_driver = { 471 .driver = { 472 .name = "rt712-sdca", 473 .owner = THIS_MODULE, 474 .pm = &rt712_sdca_pm, 475 }, 476 .probe = rt712_sdca_sdw_probe, 477 .remove = rt712_sdca_sdw_remove, 478 .ops = &rt712_sdca_slave_ops, 479 .id_table = rt712_sdca_id, 480 }; 481 module_sdw_driver(rt712_sdca_sdw_driver); 482 483 MODULE_DESCRIPTION("ASoC RT712 SDCA SDW driver"); 484 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>"); 485 MODULE_LICENSE("GPL"); 486