1 // SPDX-License-Identifier: GPL-2.0-only
3 // rt722-sdca-sdw.c -- rt722 SDCA ALSA SoC audio driver
5 // Copyright(c) 2023 Realtek Semiconductor Corp.
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>
16 #include "rt722-sdca.h"
17 #include "rt722-sdca-sdw.h"
19 static bool rt722_sdca_readable_register(struct device *dev, unsigned int reg)
22 case 0x2f01 ... 0x2f0a:
23 case 0x2f35 ... 0x2f36:
26 case 0x2f58 ... 0x2f5d:
27 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_GE49, RT722_SDCA_CTL_SELECTED_MODE,
29 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_GE49, RT722_SDCA_CTL_DETECTED_MODE,
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:
41 static bool rt722_sdca_volatile_register(struct device *dev, unsigned int reg)
46 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_GE49, RT722_SDCA_CTL_DETECTED_MODE,
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:
58 static bool rt722_sdca_mbq_readable_register(struct device *dev, unsigned int reg)
61 case 0x2000000 ... 0x2000024:
62 case 0x2000029 ... 0x200004a:
63 case 0x2000051 ... 0x2000052:
64 case 0x200005a ... 0x200005b:
65 case 0x2000061 ... 0x2000069:
69 case 0x2000082 ... 0x200008e:
70 case 0x2000090 ... 0x2000094:
71 case 0x5300000 ... 0x5300002:
73 case 0x5600000 ... 0x5600007:
74 case 0x5700000 ... 0x5700004:
75 case 0x5800000 ... 0x5800004:
79 case 0x5f00000 ... 0x5f0000d:
81 case 0x6100000 ... 0x6100051:
82 case 0x6100055 ... 0x6100057:
84 case 0x6100064 ... 0x6100065:
86 case 0x6100070 ... 0x610007c:
88 case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT722_SDCA_ENT_USER_FU1E, RT722_SDCA_CTL_FU_VOLUME,
90 case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT722_SDCA_ENT_USER_FU1E, RT722_SDCA_CTL_FU_VOLUME,
92 case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT722_SDCA_ENT_USER_FU1E, RT722_SDCA_CTL_FU_VOLUME,
94 case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT722_SDCA_ENT_USER_FU1E, RT722_SDCA_CTL_FU_VOLUME,
96 case SDW_SDCA_CTL(FUNC_NUM_AMP, RT722_SDCA_ENT_USER_FU06, RT722_SDCA_CTL_FU_VOLUME, CH_L):
97 case SDW_SDCA_CTL(FUNC_NUM_AMP, RT722_SDCA_ENT_USER_FU06, RT722_SDCA_CTL_FU_VOLUME, CH_R):
98 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_USER_FU05, RT722_SDCA_CTL_FU_VOLUME,
100 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_USER_FU05, RT722_SDCA_CTL_FU_VOLUME,
102 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_USER_FU0F, RT722_SDCA_CTL_FU_VOLUME,
104 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_USER_FU0F, RT722_SDCA_CTL_FU_VOLUME,
106 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_PLATFORM_FU44,
107 RT722_SDCA_CTL_FU_CH_GAIN, CH_L):
108 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_PLATFORM_FU44,
109 RT722_SDCA_CTL_FU_CH_GAIN, CH_R):
116 static bool rt722_sdca_mbq_volatile_register(struct device *dev, unsigned int reg)
134 static const struct regmap_config rt722_sdca_regmap = {
137 .readable_reg = rt722_sdca_readable_register,
138 .volatile_reg = rt722_sdca_volatile_register,
139 .max_register = 0x44ffffff,
140 .reg_defaults = rt722_sdca_reg_defaults,
141 .num_reg_defaults = ARRAY_SIZE(rt722_sdca_reg_defaults),
142 .cache_type = REGCACHE_MAPLE,
143 .use_single_read = true,
144 .use_single_write = true,
147 static const struct regmap_config rt722_sdca_mbq_regmap = {
151 .readable_reg = rt722_sdca_mbq_readable_register,
152 .volatile_reg = rt722_sdca_mbq_volatile_register,
153 .max_register = 0x41000312,
154 .reg_defaults = rt722_sdca_mbq_defaults,
155 .num_reg_defaults = ARRAY_SIZE(rt722_sdca_mbq_defaults),
156 .cache_type = REGCACHE_MAPLE,
157 .use_single_read = true,
158 .use_single_write = true,
161 static int rt722_sdca_update_status(struct sdw_slave *slave,
162 enum sdw_slave_status status)
164 struct rt722_sdca_priv *rt722 = dev_get_drvdata(&slave->dev);
166 if (status == SDW_SLAVE_UNATTACHED)
167 rt722->hw_init = false;
169 if (status == SDW_SLAVE_ATTACHED) {
170 if (rt722->hs_jack) {
172 * Due to the SCP_SDCA_INTMASK will be cleared by any reset, and then
173 * if the device attached again, we will need to set the setting back.
174 * It could avoid losing the jack detection interrupt.
175 * This also could sync with the cache value as the rt722_sdca_jack_init set.
177 sdw_write_no_pm(rt722->slave, SDW_SCP_SDCA_INTMASK1,
178 SDW_SCP_SDCA_INTMASK_SDCA_0 | SDW_SCP_SDCA_INTMASK_SDCA_6);
179 sdw_write_no_pm(rt722->slave, SDW_SCP_SDCA_INTMASK2,
180 SDW_SCP_SDCA_INTMASK_SDCA_8);
185 * Perform initialization only if slave status is present and
186 * hw_init flag is false
188 if (rt722->hw_init || status != SDW_SLAVE_ATTACHED)
191 /* perform I/O transfers required for Slave initialization */
192 return rt722_sdca_io_init(&slave->dev, slave);
195 static int rt722_sdca_read_prop(struct sdw_slave *slave)
197 struct sdw_slave_prop *prop = &slave->prop;
202 struct sdw_dpn_prop *dpn;
204 prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
205 prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
207 prop->paging_support = true;
210 * port = 1 for headphone playback
211 * port = 2 for headset-mic capture
212 * port = 3 for speaker playback
213 * port = 6 for digital-mic capture
215 prop->source_ports = BIT(6) | BIT(2); /* BITMAP: 01000100 */
216 prop->sink_ports = BIT(3) | BIT(1); /* BITMAP: 00001010 */
218 nval = hweight32(prop->source_ports);
219 prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
220 sizeof(*prop->src_dpn_prop), GFP_KERNEL);
221 if (!prop->src_dpn_prop)
225 dpn = prop->src_dpn_prop;
226 addr = prop->source_ports;
227 for_each_set_bit(bit, &addr, 32) {
229 dpn[i].type = SDW_DPN_FULL;
230 dpn[i].simple_ch_prep_sm = true;
231 dpn[i].ch_prep_timeout = 10;
235 /* do this again for sink now */
236 nval = hweight32(prop->sink_ports);
237 prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
238 sizeof(*prop->sink_dpn_prop), GFP_KERNEL);
239 if (!prop->sink_dpn_prop)
243 dpn = prop->sink_dpn_prop;
244 addr = prop->sink_ports;
245 for_each_set_bit(bit, &addr, 32) {
247 dpn[j].type = SDW_DPN_FULL;
248 dpn[j].simple_ch_prep_sm = true;
249 dpn[j].ch_prep_timeout = 10;
253 /* set the timeout values */
254 prop->clk_stop_timeout = 200;
257 prop->wake_capable = 1;
262 static int rt722_sdca_interrupt_callback(struct sdw_slave *slave,
263 struct sdw_slave_intr_status *status)
265 struct rt722_sdca_priv *rt722 = dev_get_drvdata(&slave->dev);
267 int count = 0, retry = 3;
268 unsigned int sdca_cascade, scp_sdca_stat1, scp_sdca_stat2 = 0;
270 if (cancel_delayed_work_sync(&rt722->jack_detect_work)) {
271 dev_warn(&slave->dev, "%s the pending delayed_work was cancelled", __func__);
272 /* avoid the HID owner doesn't change to device */
273 if (rt722->scp_sdca_stat2)
274 scp_sdca_stat2 = rt722->scp_sdca_stat2;
278 * The critical section below intentionally protects a rather large piece of code.
279 * We don't want to allow the system suspend to disable an interrupt while we are
280 * processing it, which could be problematic given the quirky SoundWire interrupt
281 * scheme. We do want however to prevent new workqueues from being scheduled if
282 * the disable_irq flag was set during system suspend.
284 mutex_lock(&rt722->disable_irq_lock);
286 ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT1);
289 rt722->scp_sdca_stat1 = ret;
290 ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT2);
293 rt722->scp_sdca_stat2 = ret;
295 rt722->scp_sdca_stat2 |= scp_sdca_stat2;
298 ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT1);
301 if (ret & SDW_SCP_SDCA_INTMASK_SDCA_0) {
302 ret = sdw_update_no_pm(rt722->slave, SDW_SCP_SDCA_INT1,
303 SDW_SCP_SDCA_INT_SDCA_0, SDW_SCP_SDCA_INT_SDCA_0);
306 } else if (ret & SDW_SCP_SDCA_INTMASK_SDCA_6) {
307 ret = sdw_update_no_pm(rt722->slave, SDW_SCP_SDCA_INT1,
308 SDW_SCP_SDCA_INT_SDCA_6, SDW_SCP_SDCA_INT_SDCA_6);
312 ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT2);
315 if (ret & SDW_SCP_SDCA_INTMASK_SDCA_8) {
316 ret = sdw_write_no_pm(rt722->slave, SDW_SCP_SDCA_INT2,
317 SDW_SCP_SDCA_INTMASK_SDCA_8);
322 /* check if flag clear or not */
323 ret = sdw_read_no_pm(rt722->slave, SDW_DP0_INT);
326 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
328 ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT1);
331 scp_sdca_stat1 = ret & SDW_SCP_SDCA_INTMASK_SDCA_0;
333 ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT2);
336 scp_sdca_stat2 = ret & SDW_SCP_SDCA_INTMASK_SDCA_8;
338 stat = scp_sdca_stat1 || scp_sdca_stat2 || sdca_cascade;
341 } while (stat != 0 && count < retry);
344 dev_warn(&slave->dev,
345 "%s scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__,
346 rt722->scp_sdca_stat1, rt722->scp_sdca_stat2);
348 if (status->sdca_cascade && !rt722->disable_irq)
349 mod_delayed_work(system_power_efficient_wq,
350 &rt722->jack_detect_work, msecs_to_jiffies(30));
352 mutex_unlock(&rt722->disable_irq_lock);
357 mutex_unlock(&rt722->disable_irq_lock);
358 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
362 static struct sdw_slave_ops rt722_sdca_slave_ops = {
363 .read_prop = rt722_sdca_read_prop,
364 .interrupt_callback = rt722_sdca_interrupt_callback,
365 .update_status = rt722_sdca_update_status,
368 static int rt722_sdca_sdw_probe(struct sdw_slave *slave,
369 const struct sdw_device_id *id)
371 struct regmap *regmap, *mbq_regmap;
373 /* Regmap Initialization */
374 mbq_regmap = devm_regmap_init_sdw_mbq(slave, &rt722_sdca_mbq_regmap);
375 if (IS_ERR(mbq_regmap))
376 return PTR_ERR(mbq_regmap);
378 regmap = devm_regmap_init_sdw(slave, &rt722_sdca_regmap);
380 return PTR_ERR(regmap);
382 return rt722_sdca_init(&slave->dev, regmap, mbq_regmap, slave);
385 static int rt722_sdca_sdw_remove(struct sdw_slave *slave)
387 struct rt722_sdca_priv *rt722 = dev_get_drvdata(&slave->dev);
389 if (rt722->hw_init) {
390 cancel_delayed_work_sync(&rt722->jack_detect_work);
391 cancel_delayed_work_sync(&rt722->jack_btn_check_work);
394 if (rt722->first_hw_init)
395 pm_runtime_disable(&slave->dev);
397 mutex_destroy(&rt722->calibrate_mutex);
398 mutex_destroy(&rt722->disable_irq_lock);
403 static const struct sdw_device_id rt722_sdca_id[] = {
404 SDW_SLAVE_ENTRY_EXT(0x025d, 0x722, 0x3, 0x1, 0),
407 MODULE_DEVICE_TABLE(sdw, rt722_sdca_id);
409 static int __maybe_unused rt722_sdca_dev_suspend(struct device *dev)
411 struct rt722_sdca_priv *rt722 = dev_get_drvdata(dev);
416 cancel_delayed_work_sync(&rt722->jack_detect_work);
417 cancel_delayed_work_sync(&rt722->jack_btn_check_work);
419 regcache_cache_only(rt722->regmap, true);
420 regcache_cache_only(rt722->mbq_regmap, true);
425 static int __maybe_unused rt722_sdca_dev_system_suspend(struct device *dev)
427 struct rt722_sdca_priv *rt722_sdca = dev_get_drvdata(dev);
428 struct sdw_slave *slave = dev_to_sdw_dev(dev);
431 if (!rt722_sdca->hw_init)
435 * prevent new interrupts from being handled after the
436 * deferred work completes and before the parent disables
437 * interrupts on the link
439 mutex_lock(&rt722_sdca->disable_irq_lock);
440 rt722_sdca->disable_irq = true;
441 ret1 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK1,
442 SDW_SCP_SDCA_INTMASK_SDCA_0 | SDW_SCP_SDCA_INTMASK_SDCA_6, 0);
443 ret2 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK2,
444 SDW_SCP_SDCA_INTMASK_SDCA_8, 0);
445 mutex_unlock(&rt722_sdca->disable_irq_lock);
447 if (ret1 < 0 || ret2 < 0) {
448 /* log but don't prevent suspend from happening */
449 dev_dbg(&slave->dev, "%s: could not disable SDCA interrupts\n:", __func__);
452 return rt722_sdca_dev_suspend(dev);
455 #define RT722_PROBE_TIMEOUT 5000
457 static int __maybe_unused rt722_sdca_dev_resume(struct device *dev)
459 struct sdw_slave *slave = dev_to_sdw_dev(dev);
460 struct rt722_sdca_priv *rt722 = dev_get_drvdata(dev);
463 if (!rt722->first_hw_init)
466 if (!slave->unattach_request)
469 time = wait_for_completion_timeout(&slave->initialization_complete,
470 msecs_to_jiffies(RT722_PROBE_TIMEOUT));
472 dev_err(&slave->dev, "Initialization not complete, timed out\n");
473 sdw_show_ping_status(slave->bus, true);
479 slave->unattach_request = 0;
480 regcache_cache_only(rt722->regmap, false);
481 regcache_sync(rt722->regmap);
482 regcache_cache_only(rt722->mbq_regmap, false);
483 regcache_sync(rt722->mbq_regmap);
487 static const struct dev_pm_ops rt722_sdca_pm = {
488 SET_SYSTEM_SLEEP_PM_OPS(rt722_sdca_dev_system_suspend, rt722_sdca_dev_resume)
489 SET_RUNTIME_PM_OPS(rt722_sdca_dev_suspend, rt722_sdca_dev_resume, NULL)
492 static struct sdw_driver rt722_sdca_sdw_driver = {
494 .name = "rt722-sdca",
495 .owner = THIS_MODULE,
496 .pm = &rt722_sdca_pm,
498 .probe = rt722_sdca_sdw_probe,
499 .remove = rt722_sdca_sdw_remove,
500 .ops = &rt722_sdca_slave_ops,
501 .id_table = rt722_sdca_id,
503 module_sdw_driver(rt722_sdca_sdw_driver);
505 MODULE_DESCRIPTION("ASoC RT722 SDCA SDW driver");
506 MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>");
507 MODULE_LICENSE("GPL");