1 // SPDX-License-Identifier: GPL-2.0-only
3 // rt711-sdw-sdca.c -- rt711 SDCA ALSA SoC audio driver
5 // Copyright(c) 2021 Realtek Semiconductor Corp.
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/soundwire/sdw_registers.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
16 #include "rt711-sdca.h"
17 #include "rt711-sdca-sdw.h"
19 static bool rt711_sdca_readable_register(struct device *dev, unsigned int reg)
22 case 0x201a ... 0x2027:
23 case 0x2029 ... 0x202a:
24 case 0x202d ... 0x2034:
25 case 0x2200 ... 0x2204:
26 case 0x2206 ... 0x2212:
27 case 0x2220 ... 0x2223:
28 case 0x2230 ... 0x2239:
29 case 0x2f01 ... 0x2f0f:
30 case 0x2f30 ... 0x2f36:
31 case 0x2f50 ... 0x2f5a:
33 case 0x3200 ... 0x3212:
34 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_SELECTED_MODE, 0):
35 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_DETECTED_MODE, 0):
36 case SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_CURRENT_OWNER, 0) ...
37 SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_MESSAGE_LENGTH, 0):
38 case RT711_BUF_ADDR_HID1 ... RT711_BUF_ADDR_HID2:
45 static bool rt711_sdca_volatile_register(struct device *dev, unsigned int reg)
55 case 0x202d ... 0x202f: /* BRA */
56 case 0x2200 ... 0x2212: /* i2c debug */
57 case RT711_RC_CAL_STATUS:
58 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_DETECTED_MODE, 0):
59 case SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_CURRENT_OWNER, 0) ...
60 SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_MESSAGE_LENGTH, 0):
61 case RT711_BUF_ADDR_HID1 ... RT711_BUF_ADDR_HID2:
68 static bool rt711_sdca_mbq_readable_register(struct device *dev, unsigned int reg)
71 case 0x2000000 ... 0x20000ff:
72 case 0x5600000 ... 0x56000ff:
73 case 0x5700000 ... 0x57000ff:
74 case 0x5800000 ... 0x58000ff:
75 case 0x5900000 ... 0x59000ff:
76 case 0x5b00000 ... 0x5b000ff:
77 case 0x5f00000 ... 0x5f000ff:
78 case 0x6100000 ... 0x61000ff:
79 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_VOLUME, CH_L):
80 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU05, RT711_SDCA_CTL_FU_VOLUME, CH_R):
81 case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_VOLUME, CH_L):
82 case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_USER_FU1E, RT711_SDCA_CTL_FU_VOLUME, CH_R):
83 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_VOLUME, CH_L):
84 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_USER_FU0F, RT711_SDCA_CTL_FU_VOLUME, CH_R):
85 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PLATFORM_FU44, RT711_SDCA_CTL_FU_CH_GAIN, CH_L):
86 case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_PLATFORM_FU44, RT711_SDCA_CTL_FU_CH_GAIN, CH_R):
87 case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PLATFORM_FU15, RT711_SDCA_CTL_FU_CH_GAIN, CH_L):
88 case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT711_SDCA_ENT_PLATFORM_FU15, RT711_SDCA_CTL_FU_CH_GAIN, CH_R):
95 static bool rt711_sdca_mbq_volatile_register(struct device *dev, unsigned int reg)
114 static const struct regmap_config rt711_sdca_regmap = {
117 .readable_reg = rt711_sdca_readable_register,
118 .volatile_reg = rt711_sdca_volatile_register,
119 .max_register = 0x44ffffff,
120 .reg_defaults = rt711_sdca_reg_defaults,
121 .num_reg_defaults = ARRAY_SIZE(rt711_sdca_reg_defaults),
122 .cache_type = REGCACHE_MAPLE,
123 .use_single_read = true,
124 .use_single_write = true,
127 static const struct regmap_config rt711_sdca_mbq_regmap = {
131 .readable_reg = rt711_sdca_mbq_readable_register,
132 .volatile_reg = rt711_sdca_mbq_volatile_register,
133 .max_register = 0x40800f12,
134 .reg_defaults = rt711_sdca_mbq_defaults,
135 .num_reg_defaults = ARRAY_SIZE(rt711_sdca_mbq_defaults),
136 .cache_type = REGCACHE_MAPLE,
137 .use_single_read = true,
138 .use_single_write = true,
141 static int rt711_sdca_update_status(struct sdw_slave *slave,
142 enum sdw_slave_status status)
144 struct rt711_sdca_priv *rt711 = dev_get_drvdata(&slave->dev);
146 if (status == SDW_SLAVE_UNATTACHED)
147 rt711->hw_init = false;
149 if (status == SDW_SLAVE_ATTACHED) {
150 if (rt711->hs_jack) {
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 rt711_sdca_jack_init set.
157 sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK1,
158 SDW_SCP_SDCA_INTMASK_SDCA_0);
159 sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK2,
160 SDW_SCP_SDCA_INTMASK_SDCA_8);
165 * Perform initialization only if slave status is present and
166 * hw_init flag is false
168 if (rt711->hw_init || status != SDW_SLAVE_ATTACHED)
171 /* perform I/O transfers required for Slave initialization */
172 return rt711_sdca_io_init(&slave->dev, slave);
175 static int rt711_sdca_read_prop(struct sdw_slave *slave)
177 struct sdw_slave_prop *prop = &slave->prop;
182 struct sdw_dpn_prop *dpn;
184 prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
185 prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
187 prop->paging_support = true;
189 /* first we need to allocate memory for set bits in port lists */
190 prop->source_ports = 0x14; /* BITMAP: 00010100 */
191 prop->sink_ports = 0x8; /* BITMAP: 00001000 */
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)
200 dpn = prop->src_dpn_prop;
201 addr = prop->source_ports;
202 for_each_set_bit(bit, &addr, 32) {
204 dpn[i].type = SDW_DPN_FULL;
205 dpn[i].simple_ch_prep_sm = true;
206 dpn[i].ch_prep_timeout = 10;
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)
218 dpn = prop->sink_dpn_prop;
219 addr = prop->sink_ports;
220 for_each_set_bit(bit, &addr, 32) {
222 dpn[j].type = SDW_DPN_FULL;
223 dpn[j].simple_ch_prep_sm = true;
224 dpn[j].ch_prep_timeout = 10;
228 /* set the timeout values */
229 prop->clk_stop_timeout = 700;
232 prop->wake_capable = 1;
237 static int rt711_sdca_interrupt_callback(struct sdw_slave *slave,
238 struct sdw_slave_intr_status *status)
240 struct rt711_sdca_priv *rt711 = dev_get_drvdata(&slave->dev);
242 int count = 0, retry = 3;
243 unsigned int sdca_cascade, scp_sdca_stat1, scp_sdca_stat2 = 0;
246 "%s control_port_stat=%x, sdca_cascade=%x", __func__,
247 status->control_port, status->sdca_cascade);
249 if (cancel_delayed_work_sync(&rt711->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 (rt711->scp_sdca_stat2)
253 scp_sdca_stat2 = rt711->scp_sdca_stat2;
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.
263 mutex_lock(&rt711->disable_irq_lock);
265 ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT1);
268 rt711->scp_sdca_stat1 = ret;
269 ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT2);
272 rt711->scp_sdca_stat2 = ret;
274 rt711->scp_sdca_stat2 |= scp_sdca_stat2;
278 ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT1);
281 if (ret & SDW_SCP_SDCA_INTMASK_SDCA_0) {
282 ret = sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INT1,
283 SDW_SCP_SDCA_INTMASK_SDCA_0);
287 ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT2);
290 if (ret & SDW_SCP_SDCA_INTMASK_SDCA_8) {
291 ret = sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INT2,
292 SDW_SCP_SDCA_INTMASK_SDCA_8);
297 /* check if flag clear or not */
298 ret = sdw_read_no_pm(rt711->slave, SDW_DP0_INT);
301 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
303 ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT1);
306 scp_sdca_stat1 = ret & SDW_SCP_SDCA_INTMASK_SDCA_0;
308 ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT2);
311 scp_sdca_stat2 = ret & SDW_SCP_SDCA_INTMASK_SDCA_8;
313 stat = scp_sdca_stat1 || scp_sdca_stat2 || sdca_cascade;
316 } while (stat != 0 && count < retry);
319 dev_warn(&slave->dev,
320 "%s scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__,
321 rt711->scp_sdca_stat1, rt711->scp_sdca_stat2);
323 if (status->sdca_cascade && !rt711->disable_irq)
324 mod_delayed_work(system_power_efficient_wq,
325 &rt711->jack_detect_work, msecs_to_jiffies(30));
327 mutex_unlock(&rt711->disable_irq_lock);
332 mutex_unlock(&rt711->disable_irq_lock);
333 pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
337 static const struct sdw_slave_ops rt711_sdca_slave_ops = {
338 .read_prop = rt711_sdca_read_prop,
339 .interrupt_callback = rt711_sdca_interrupt_callback,
340 .update_status = rt711_sdca_update_status,
343 static int rt711_sdca_sdw_probe(struct sdw_slave *slave,
344 const struct sdw_device_id *id)
346 struct regmap *regmap, *mbq_regmap;
348 /* Regmap Initialization */
349 mbq_regmap = devm_regmap_init_sdw_mbq(slave, &rt711_sdca_mbq_regmap);
350 if (IS_ERR(mbq_regmap))
351 return PTR_ERR(mbq_regmap);
353 regmap = devm_regmap_init_sdw(slave, &rt711_sdca_regmap);
355 return PTR_ERR(regmap);
357 return rt711_sdca_init(&slave->dev, regmap, mbq_regmap, slave);
360 static int rt711_sdca_sdw_remove(struct sdw_slave *slave)
362 struct rt711_sdca_priv *rt711 = dev_get_drvdata(&slave->dev);
364 if (rt711->hw_init) {
365 cancel_delayed_work_sync(&rt711->jack_detect_work);
366 cancel_delayed_work_sync(&rt711->jack_btn_check_work);
369 if (rt711->first_hw_init)
370 pm_runtime_disable(&slave->dev);
372 mutex_destroy(&rt711->calibrate_mutex);
373 mutex_destroy(&rt711->disable_irq_lock);
378 static const struct sdw_device_id rt711_sdca_id[] = {
379 SDW_SLAVE_ENTRY_EXT(0x025d, 0x711, 0x3, 0x1, 0),
382 MODULE_DEVICE_TABLE(sdw, rt711_sdca_id);
384 static int __maybe_unused rt711_sdca_dev_suspend(struct device *dev)
386 struct rt711_sdca_priv *rt711 = dev_get_drvdata(dev);
391 cancel_delayed_work_sync(&rt711->jack_detect_work);
392 cancel_delayed_work_sync(&rt711->jack_btn_check_work);
394 regcache_cache_only(rt711->regmap, true);
395 regcache_cache_only(rt711->mbq_regmap, true);
400 static int __maybe_unused rt711_sdca_dev_system_suspend(struct device *dev)
402 struct rt711_sdca_priv *rt711_sdca = dev_get_drvdata(dev);
403 struct sdw_slave *slave = dev_to_sdw_dev(dev);
406 if (!rt711_sdca->hw_init)
410 * prevent new interrupts from being handled after the
411 * deferred work completes and before the parent disables
412 * interrupts on the link
414 mutex_lock(&rt711_sdca->disable_irq_lock);
415 rt711_sdca->disable_irq = true;
416 ret1 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK1,
417 SDW_SCP_SDCA_INTMASK_SDCA_0, 0);
418 ret2 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK2,
419 SDW_SCP_SDCA_INTMASK_SDCA_8, 0);
420 mutex_unlock(&rt711_sdca->disable_irq_lock);
422 if (ret1 < 0 || ret2 < 0) {
423 /* log but don't prevent suspend from happening */
424 dev_dbg(&slave->dev, "%s: could not disable SDCA interrupts\n:", __func__);
427 return rt711_sdca_dev_suspend(dev);
430 #define RT711_PROBE_TIMEOUT 5000
432 static int __maybe_unused rt711_sdca_dev_resume(struct device *dev)
434 struct sdw_slave *slave = dev_to_sdw_dev(dev);
435 struct rt711_sdca_priv *rt711 = dev_get_drvdata(dev);
438 if (!rt711->first_hw_init)
441 if (!slave->unattach_request) {
442 if (rt711->disable_irq == true) {
443 mutex_lock(&rt711->disable_irq_lock);
444 sdw_write_no_pm(slave, SDW_SCP_SDCA_INTMASK1, SDW_SCP_SDCA_INTMASK_SDCA_0);
445 sdw_write_no_pm(slave, SDW_SCP_SDCA_INTMASK2, SDW_SCP_SDCA_INTMASK_SDCA_8);
446 rt711->disable_irq = false;
447 mutex_unlock(&rt711->disable_irq_lock);
452 time = wait_for_completion_timeout(&slave->initialization_complete,
453 msecs_to_jiffies(RT711_PROBE_TIMEOUT));
455 dev_err(&slave->dev, "Initialization not complete, timed out\n");
456 sdw_show_ping_status(slave->bus, true);
462 slave->unattach_request = 0;
463 regcache_cache_only(rt711->regmap, false);
464 regcache_sync(rt711->regmap);
465 regcache_cache_only(rt711->mbq_regmap, false);
466 regcache_sync(rt711->mbq_regmap);
470 static const struct dev_pm_ops rt711_sdca_pm = {
471 SET_SYSTEM_SLEEP_PM_OPS(rt711_sdca_dev_system_suspend, rt711_sdca_dev_resume)
472 SET_RUNTIME_PM_OPS(rt711_sdca_dev_suspend, rt711_sdca_dev_resume, NULL)
475 static struct sdw_driver rt711_sdca_sdw_driver = {
477 .name = "rt711-sdca",
478 .owner = THIS_MODULE,
479 .pm = &rt711_sdca_pm,
481 .probe = rt711_sdca_sdw_probe,
482 .remove = rt711_sdca_sdw_remove,
483 .ops = &rt711_sdca_slave_ops,
484 .id_table = rt711_sdca_id,
486 module_sdw_driver(rt711_sdca_sdw_driver);
488 MODULE_DESCRIPTION("ASoC RT711 SDCA SDW driver");
489 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
490 MODULE_LICENSE("GPL");