1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // rt766-sdca-sdw.c -- rt766 SDCA ALSA SoC audio driver 4 // 5 // Copyright(c) 2026 Realtek Semiconductor Corp. 6 // 7 // 8 9 #include <linux/delay.h> 10 #include <linux/device.h> 11 #include <linux/module.h> 12 #include <linux/pm_runtime.h> 13 #include <linux/soundwire/sdw_registers.h> 14 #include <sound/sdca.h> 15 #include <sound/sdca_function.h> 16 #include "rt766-sdca.h" 17 #include "rt766-sdca-sdw.h" 18 19 #define RT766_PROBE_TIMEOUT 5000 20 21 static bool rt766_sdca_readable_register(struct device *dev, unsigned int reg) 22 { 23 switch (reg) { 24 case SDW_SCP_SDCA_INT1 ... SDW_SCP_SDCA_INTMASK4: 25 case RT766_VERSION_ID ... RT766_BOND_LATCH_ID: 26 case 0xc344 ... 0xc345: 27 case 0xc900: 28 case 0xc920: 29 case 0xd540 ... 0xd542: 30 case 0xf01e: 31 case RT766_HP_POWER_STATE ... RT766_HP_FSM_CTL2_1: 32 case 0x310100: 33 case RT766_MCU_PATCH_ADDR1_START ... RT766_MCU_PATCH_ADDR1_END: 34 case RT766_MCU_PATCH_ADDR2_START ... RT766_MCU_PATCH_ADDR2_END: 35 case RT766_MUTE_REG(UAJ, USER_FU41, 1): 36 case RT766_MUTE_REG(UAJ, USER_FU41, 2): 37 case RT766_VOLUME_REG(UAJ, USER_FU41, 1): 38 case RT766_VOLUME_REG(UAJ, USER_FU41, 2): 39 case RT766_MUTE_REG(UAJ, USER_FU36, 1): 40 case RT766_MUTE_REG(UAJ, USER_FU36, 2): 41 case RT766_VOLUME_REG(UAJ, USER_FU36, 1): 42 case RT766_VOLUME_REG(UAJ, USER_FU36, 2): 43 case RT766_PDE_REQ_REG(UAJ, PDE47): 44 case RT766_PDE_REQ_REG(UAJ, PDE34): 45 case RT766_SDCA_CTL(UAJ, CS41, SDCA_CTL_CS_SAMPLERATEINDEX): 46 case RT766_SDCA_CTL(UAJ, CS36, SDCA_CTL_CS_SAMPLERATEINDEX): 47 case RT766_FUNC_STATUS_REG(UAJ): /* 0x40480000 */ 48 case RT766_PDE_ACTUAL_REG(UAJ, PDE47): /* 0x40481400 */ 49 case RT766_PDE_ACTUAL_REG(UAJ, PDE34): /* 0x40481480 */ 50 case RT766_GAIN_REG(UAJ, PLATFORM_FU33, 1): 51 case RT766_GAIN_REG(UAJ, PLATFORM_FU33, 2): 52 case RT766_SDCA_CTL(UAJ, GE49, SDCA_CTL_GE_SELECTED_MODE): 53 case RT766_SDCA_CTL(UAJ, GE49, SDCA_CTL_GE_DETECTED_MODE): /* 0x40600490 */ 54 case RT766_PDE_REQ_REG(MIC, PDE11): 55 case RT766_MUTE_REG(MIC, USER_FU113, 1): 56 case RT766_MUTE_REG(MIC, USER_FU113, 2): 57 case RT766_MUTE_REG(MIC, USER_FU113, 3): 58 case RT766_MUTE_REG(MIC, USER_FU113, 4): 59 case RT766_VOLUME_REG(MIC, USER_FU113, 1): 60 case RT766_VOLUME_REG(MIC, USER_FU113, 2): 61 case RT766_VOLUME_REG(MIC, USER_FU113, 3): 62 case RT766_VOLUME_REG(MIC, USER_FU113, 4): 63 case RT766_FUNC_STATUS_REG(MIC): /* 0x40880000 */ 64 case RT766_SDCA_CTL(MIC, CS113, SDCA_CTL_CS_SAMPLERATEINDEX): 65 case RT766_PDE_ACTUAL_REG(MIC, PDE11): /* 0x40881500 */ 66 case RT766_FUNC_STATUS_REG(HID): /* 0x40c80000 */ 67 /* 0x40c80080 - 0x40c80098 */ 68 case RT766_SDCA_CTL(HID, HID101, SDCA_CTL_HIDE_HIDTX_CURRENTOWNER) ... 69 RT766_SDCA_CTL(HID, HID101, SDCA_CTL_HIDE_HIDTX_MESSAGELENGTH): 70 case RT766_MUTE_REG(AMP, USER_FU21, 1): 71 case RT766_MUTE_REG(AMP, USER_FU21, 2): 72 case RT766_VOLUME_REG(AMP, USER_FU21, 1): 73 case RT766_VOLUME_REG(AMP, USER_FU21, 2): 74 case RT766_PDE_REQ_REG(AMP, PDE23): 75 case RT766_FUNC_STATUS_REG(AMP): /* 0x41080000 */ 76 case RT766_SDCA_CTL(AMP, PPU21, SDCA_CTL_PPU_POSTURENUMBER): 77 case RT766_SDCA_CTL(AMP, CS21, SDCA_CTL_CS_SAMPLERATEINDEX): 78 case RT766_PDE_ACTUAL_REG(AMP, PDE23): /* 0x41081980 */ 79 case RT766_BUF_ADDR_HID1 ... RT766_BUF_ADDR_HID2: 80 return true; 81 default: 82 return false; 83 } 84 } 85 86 static bool rt766_sdca_volatile_register(struct device *dev, unsigned int reg) 87 { 88 switch (reg) { 89 case SDW_SCP_SDCA_INT1 ... SDW_SCP_SDCA_INTMASK4: 90 case RT766_VERSION_ID ... RT766_BOND_LATCH_ID: 91 case 0xc344 ... 0xc345: 92 case 0xc900: 93 case 0xc920: 94 case 0xd540 ... 0xd542: 95 case 0xf01e: 96 case RT766_HP_POWER_STATE ... RT766_HP_FSM_CTL2_1: 97 case 0x310100: 98 case RT766_MCU_PATCH_ADDR1_START ... RT766_MCU_PATCH_ADDR1_END: 99 case RT766_MCU_PATCH_ADDR2_START ... RT766_MCU_PATCH_ADDR2_END: 100 case RT766_FUNC_STATUS_REG(UAJ): 101 case RT766_PDE_ACTUAL_REG(UAJ, PDE47): 102 case RT766_PDE_ACTUAL_REG(UAJ, PDE34): 103 case RT766_SDCA_CTL(UAJ, GE49, SDCA_CTL_GE_DETECTED_MODE): 104 case RT766_FUNC_STATUS_REG(MIC): 105 case RT766_PDE_ACTUAL_REG(MIC, PDE11): 106 case RT766_FUNC_STATUS_REG(HID): 107 case RT766_SDCA_CTL(HID, HID101, SDCA_CTL_HIDE_HIDTX_CURRENTOWNER) ... 108 RT766_SDCA_CTL(HID, HID101, SDCA_CTL_HIDE_HIDTX_MESSAGELENGTH): 109 case RT766_FUNC_STATUS_REG(AMP): 110 case RT766_PDE_ACTUAL_REG(AMP, PDE23): 111 case RT766_BUF_ADDR_HID1 ... RT766_BUF_ADDR_HID2: 112 return true; 113 default: 114 return false; 115 } 116 } 117 118 static int rt766_sdca_mbq_size(struct device *dev, unsigned int reg) 119 { 120 switch (reg) { 121 case RT766_VOLUME_REG(UAJ, USER_FU41, 1): 122 case RT766_VOLUME_REG(UAJ, USER_FU41, 2): 123 case RT766_VOLUME_REG(UAJ, USER_FU36, 1): 124 case RT766_VOLUME_REG(UAJ, USER_FU36, 2): 125 case RT766_GAIN_REG(UAJ, PLATFORM_FU33, 1): 126 case RT766_GAIN_REG(UAJ, PLATFORM_FU33, 2): 127 case RT766_VOLUME_REG(MIC, USER_FU113, 1): 128 case RT766_VOLUME_REG(MIC, USER_FU113, 2): 129 case RT766_VOLUME_REG(MIC, USER_FU113, 3): 130 case RT766_VOLUME_REG(MIC, USER_FU113, 4): 131 case RT766_VOLUME_REG(AMP, USER_FU21, 1): 132 case RT766_VOLUME_REG(AMP, USER_FU21, 2): 133 return 2; 134 default: 135 return 1; 136 } 137 } 138 139 static const struct regmap_sdw_mbq_cfg rt766_sdca_mbq_cfg = { 140 .mbq_size = rt766_sdca_mbq_size, 141 }; 142 143 static const struct regmap_config rt766_sdca_regmap = { 144 .reg_bits = 32, 145 .val_bits = 16, 146 .readable_reg = rt766_sdca_readable_register, 147 .volatile_reg = rt766_sdca_volatile_register, 148 .reg_defaults = rt766_sdca_defaults, 149 .num_reg_defaults = ARRAY_SIZE(rt766_sdca_defaults), 150 .max_register = SDW_SDCA_MAX_REGISTER, 151 .cache_type = REGCACHE_MAPLE, 152 .use_single_read = true, 153 .use_single_write = true, 154 }; 155 156 static int rt766_sdca_update_status(struct sdw_slave *slave, 157 enum sdw_slave_status status) 158 { 159 struct rt766_sdca_priv *rt766 = dev_get_drvdata(&slave->dev); 160 161 if (status == SDW_SLAVE_UNATTACHED) 162 rt766->hw_init = false; 163 164 if (status == SDW_SLAVE_ATTACHED) { 165 if (rt766->hs_jack) { 166 /* 167 * Due to the SCP_SDCA_INTMASK will be cleared by any reset, and then 168 * if the device attached again, we will need to set the setting back. 169 * It could avoid losing the jack detection interrupt. 170 * This also could sync with the cache value as the rt766_sdca_jack_init set. 171 */ 172 sdw_write_no_pm(rt766->slave, SDW_SCP_SDCA_INTMASK3, 173 SDW_SCP_SDCA_INTMASK_SDCA_16); 174 sdw_write_no_pm(rt766->slave, SDW_SCP_SDCA_INTMASK4, 175 SDW_SCP_SDCA_INTMASK_SDCA_24); 176 } 177 } 178 179 /* 180 * Perform initialization only if slave status is present and 181 * hw_init flag is false 182 */ 183 if (rt766->hw_init || status != SDW_SLAVE_ATTACHED) 184 return 0; 185 186 /* perform I/O transfers required for Slave initialization */ 187 return rt766_sdca_io_init(&slave->dev, slave); 188 } 189 190 static int rt766_sdca_read_prop(struct sdw_slave *slave) 191 { 192 struct sdw_slave_prop *prop = &slave->prop; 193 int ret; 194 195 ret = sdw_slave_read_prop(slave); 196 if (ret < 0) 197 return ret; 198 199 prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY; 200 prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY; 201 /* 202 * SDCA interrupts are routed through SoundWire domain IRQ. 203 */ 204 prop->use_domain_irq = true; 205 206 return 0; 207 } 208 209 static const struct sdw_slave_ops rt766_sdca_slave_ops = { 210 .read_prop = rt766_sdca_read_prop, 211 .update_status = rt766_sdca_update_status, 212 }; 213 214 static int rt766_sdca_sdw_probe(struct sdw_slave *slave, 215 const struct sdw_device_id *id) 216 { 217 struct regmap *regmap; 218 219 /* Regmap Initialization */ 220 regmap = devm_regmap_init_sdw_mbq_cfg(&slave->dev, slave, 221 &rt766_sdca_regmap, &rt766_sdca_mbq_cfg); 222 if (IS_ERR(regmap)) 223 return PTR_ERR(regmap); 224 225 return rt766_sdca_init(&slave->dev, regmap, slave); 226 } 227 228 static void rt766_sdca_sdw_remove(struct sdw_slave *slave) 229 { 230 pm_runtime_disable(&slave->dev); 231 } 232 233 static const struct sdw_device_id rt766_sdca_id[] = { 234 SDW_SLAVE_ENTRY_EXT(0x025d, 0x766, 0x3, 0x1, 0), 235 SDW_SLAVE_ENTRY_EXT(0x025d, 0x767, 0x3, 0x1, 0), 236 SDW_SLAVE_ENTRY_EXT(0x025d, 0x766, 0x4, 0x1, 0), 237 SDW_SLAVE_ENTRY_EXT(0x025d, 0x767, 0x4, 0x1, 0), 238 {}, 239 }; 240 MODULE_DEVICE_TABLE(sdw, rt766_sdca_id); 241 242 static int rt766_sdca_dev_suspend(struct device *dev) 243 { 244 struct rt766_sdca_priv *rt766 = dev_get_drvdata(dev); 245 246 if (!rt766->hw_init) 247 return 0; 248 249 regcache_cache_only(rt766->regmap, true); 250 return 0; 251 } 252 253 static int rt766_sdca_dev_system_suspend(struct device *dev) 254 { 255 struct rt766_sdca_priv *rt766 = dev_get_drvdata(dev); 256 struct sdw_slave *slave = dev_to_sdw_dev(dev); 257 int ret1, ret2; 258 259 if (!rt766->hw_init) 260 return 0; 261 262 /* 263 * prevent new interrupts from being handled after the 264 * deferred work completes and before the parent disables 265 * interrupts on the link 266 */ 267 mutex_lock(&rt766->disable_irq_lock); 268 rt766->disable_irq = true; 269 ret1 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK3, 270 SDW_SCP_SDCA_INTMASK_SDCA_16, 0); 271 ret2 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK4, 272 SDW_SCP_SDCA_INTMASK_SDCA_24, 0); 273 mutex_unlock(&rt766->disable_irq_lock); 274 275 if (ret1 < 0 || ret2 < 0) { 276 /* log but don't prevent suspend from happening */ 277 dev_dbg(&slave->dev, "%s: could not disable SDCA interrupts\n:", __func__); 278 } 279 280 return rt766_sdca_dev_suspend(dev); 281 } 282 283 static int rt766_sdca_dev_resume(struct device *dev) 284 { 285 struct sdw_slave *slave = dev_to_sdw_dev(dev); 286 struct rt766_sdca_priv *rt766 = dev_get_drvdata(dev); 287 int ret; 288 289 if (!rt766->first_hw_init) 290 return 0; 291 292 if (!slave->unattach_request) { 293 mutex_lock(&rt766->disable_irq_lock); 294 if (rt766->disable_irq == true) { 295 sdw_write_no_pm(slave, SDW_SCP_SDCA_INTMASK3, SDW_SCP_SDCA_INTMASK_SDCA_16); 296 sdw_write_no_pm(slave, SDW_SCP_SDCA_INTMASK4, SDW_SCP_SDCA_INTMASK_SDCA_24); 297 rt766->disable_irq = false; 298 } 299 mutex_unlock(&rt766->disable_irq_lock); 300 goto regmap_sync; 301 } 302 303 ret = sdw_slave_wait_for_init(slave, RT766_PROBE_TIMEOUT); 304 if (ret) { 305 sdw_show_ping_status(slave->bus, true); 306 return ret; 307 } 308 309 regmap_sync: 310 regcache_cache_only(rt766->regmap, false); 311 ret = regcache_sync(rt766->regmap); 312 if (ret) { 313 regcache_cache_only(rt766->regmap, true); 314 regcache_mark_dirty(rt766->regmap); 315 return ret; 316 } 317 318 return 0; 319 } 320 321 static const struct dev_pm_ops rt766_sdca_pm = { 322 SYSTEM_SLEEP_PM_OPS(rt766_sdca_dev_system_suspend, rt766_sdca_dev_resume) 323 RUNTIME_PM_OPS(rt766_sdca_dev_suspend, rt766_sdca_dev_resume, NULL) 324 }; 325 326 static struct sdw_driver rt766_sdca_sdw_driver = { 327 .driver = { 328 .name = "rt766-sdca", 329 .pm = pm_ptr(&rt766_sdca_pm), 330 }, 331 .probe = rt766_sdca_sdw_probe, 332 .remove = rt766_sdca_sdw_remove, 333 .ops = &rt766_sdca_slave_ops, 334 .id_table = rt766_sdca_id, 335 }; 336 module_sdw_driver(rt766_sdca_sdw_driver); 337 338 MODULE_DESCRIPTION("ASoC RT766 SDCA SDW driver"); 339 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>"); 340 MODULE_LICENSE("GPL"); 341 MODULE_IMPORT_NS("SND_SOC_SDCA"); 342