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 0x5300000 ... 0x5300002:
72 	case 0x5400002:
73 	case 0x5600000 ... 0x5600007:
74 	case 0x5700000 ... 0x5700004:
75 	case 0x5800000 ... 0x5800004:
76 	case 0x5b00003:
77 	case 0x5c00011:
78 	case 0x5d00006:
79 	case 0x5f00000 ... 0x5f0000d:
80 	case 0x5f00030:
81 	case 0x6100000 ... 0x6100051:
82 	case 0x6100055 ... 0x6100057:
83 	case 0x6100062:
84 	case 0x6100064 ... 0x6100065:
85 	case 0x6100067:
86 	case 0x6100070 ... 0x610007c:
87 	case 0x6100080:
88 	case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT722_SDCA_ENT_USER_FU1E, RT722_SDCA_CTL_FU_VOLUME,
89 			CH_01):
90 	case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT722_SDCA_ENT_USER_FU1E, RT722_SDCA_CTL_FU_VOLUME,
91 			CH_02):
92 	case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT722_SDCA_ENT_USER_FU1E, RT722_SDCA_CTL_FU_VOLUME,
93 			CH_03):
94 	case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT722_SDCA_ENT_USER_FU1E, RT722_SDCA_CTL_FU_VOLUME,
95 			CH_04):
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,
99 			CH_L):
100 	case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_USER_FU05, RT722_SDCA_CTL_FU_VOLUME,
101 			CH_R):
102 	case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_USER_FU0F, RT722_SDCA_CTL_FU_VOLUME,
103 			CH_L):
104 	case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_USER_FU0F, RT722_SDCA_CTL_FU_VOLUME,
105 			CH_R):
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):
110 		return true;
111 	default:
112 		return false;
113 	}
114 }
115 
116 static bool rt722_sdca_mbq_volatile_register(struct device *dev, unsigned int reg)
117 {
118 	switch (reg) {
119 	case 0x2000000:
120 	case 0x200000d:
121 	case 0x2000019:
122 	case 0x2000020:
123 	case 0x2000030:
124 	case 0x2000046:
125 	case 0x2000067:
126 	case 0x2000084:
127 	case 0x2000086:
128 		return true;
129 	default:
130 		return false;
131 	}
132 }
133 
134 static const struct regmap_config rt722_sdca_regmap = {
135 	.reg_bits = 32,
136 	.val_bits = 8,
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,
145 };
146 
147 static const struct regmap_config rt722_sdca_mbq_regmap = {
148 	.name = "sdw-mbq",
149 	.reg_bits = 32,
150 	.val_bits = 16,
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,
159 };
160 
161 static int rt722_sdca_update_status(struct sdw_slave *slave,
162 				enum sdw_slave_status status)
163 {
164 	struct rt722_sdca_priv *rt722 = dev_get_drvdata(&slave->dev);
165 
166 	if (status == SDW_SLAVE_UNATTACHED)
167 		rt722->hw_init = false;
168 
169 	if (status == SDW_SLAVE_ATTACHED) {
170 		if (rt722->hs_jack) {
171 		/*
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.
176 		 */
177 			sdw_write_no_pm(rt722->slave, SDW_SCP_SDCA_INTMASK1,
178 				SDW_SCP_SDCA_INTMASK_SDCA_6);
179 			sdw_write_no_pm(rt722->slave, SDW_SCP_SDCA_INTMASK2,
180 				SDW_SCP_SDCA_INTMASK_SDCA_8);
181 		}
182 	}
183 
184 	/*
185 	 * Perform initialization only if slave status is present and
186 	 * hw_init flag is false
187 	 */
188 	if (rt722->hw_init || status != SDW_SLAVE_ATTACHED)
189 		return 0;
190 
191 	/* perform I/O transfers required for Slave initialization */
192 	return rt722_sdca_io_init(&slave->dev, slave);
193 }
194 
195 static int rt722_sdca_read_prop(struct sdw_slave *slave)
196 {
197 	struct sdw_slave_prop *prop = &slave->prop;
198 	int nval;
199 	int i, j;
200 	u32 bit;
201 	unsigned long addr;
202 	struct sdw_dpn_prop *dpn;
203 
204 	prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
205 	prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
206 
207 	prop->paging_support = true;
208 
209 	/*
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
214 	 */
215 	prop->source_ports = BIT(6) | BIT(2); /* BITMAP: 01000100 */
216 	prop->sink_ports = BIT(3) | BIT(1); /* BITMAP:  00001010 */
217 
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)
222 		return -ENOMEM;
223 
224 	i = 0;
225 	dpn = prop->src_dpn_prop;
226 	addr = prop->source_ports;
227 	for_each_set_bit(bit, &addr, 32) {
228 		dpn[i].num = bit;
229 		dpn[i].type = SDW_DPN_FULL;
230 		dpn[i].simple_ch_prep_sm = true;
231 		dpn[i].ch_prep_timeout = 10;
232 		i++;
233 	}
234 
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)
240 		return -ENOMEM;
241 
242 	j = 0;
243 	dpn = prop->sink_dpn_prop;
244 	addr = prop->sink_ports;
245 	for_each_set_bit(bit, &addr, 32) {
246 		dpn[j].num = bit;
247 		dpn[j].type = SDW_DPN_FULL;
248 		dpn[j].simple_ch_prep_sm = true;
249 		dpn[j].ch_prep_timeout = 10;
250 		j++;
251 	}
252 
253 	/* set the timeout values */
254 	prop->clk_stop_timeout = 200;
255 
256 	/* wake-up event */
257 	prop->wake_capable = 1;
258 
259 	return 0;
260 }
261 
262 static int rt722_sdca_interrupt_callback(struct sdw_slave *slave,
263 					struct sdw_slave_intr_status *status)
264 {
265 	struct rt722_sdca_priv *rt722 = dev_get_drvdata(&slave->dev);
266 	int ret, stat;
267 	int count = 0, retry = 3;
268 	unsigned int sdca_cascade, scp_sdca_stat1, scp_sdca_stat2 = 0;
269 
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;
275 	}
276 
277 	/*
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.
283 	 */
284 	mutex_lock(&rt722->disable_irq_lock);
285 
286 	ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT1);
287 	if (ret < 0)
288 		goto io_error;
289 	rt722->scp_sdca_stat1 = ret;
290 	ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT2);
291 	if (ret < 0)
292 		goto io_error;
293 	rt722->scp_sdca_stat2 = ret;
294 	if (scp_sdca_stat2)
295 		rt722->scp_sdca_stat2 |= scp_sdca_stat2;
296 	do {
297 		/* clear flag */
298 		ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT1);
299 		if (ret < 0)
300 			goto io_error;
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);
304 			if (ret < 0)
305 				goto io_error;
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);
309 			if (ret < 0)
310 				goto io_error;
311 		}
312 		ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT2);
313 		if (ret < 0)
314 			goto io_error;
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);
318 			if (ret < 0)
319 				goto io_error;
320 		}
321 
322 		/* check if flag clear or not */
323 		ret = sdw_read_no_pm(rt722->slave, SDW_DP0_INT);
324 		if (ret < 0)
325 			goto io_error;
326 		sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
327 
328 		ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT1);
329 		if (ret < 0)
330 			goto io_error;
331 		scp_sdca_stat1 = ret & SDW_SCP_SDCA_INTMASK_SDCA_0;
332 
333 		ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT2);
334 		if (ret < 0)
335 			goto io_error;
336 		scp_sdca_stat2 = ret & SDW_SCP_SDCA_INTMASK_SDCA_8;
337 
338 		stat = scp_sdca_stat1 || scp_sdca_stat2 || sdca_cascade;
339 
340 		count++;
341 	} while (stat != 0 && count < retry);
342 
343 	if (stat)
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);
347 
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));
351 
352 	mutex_unlock(&rt722->disable_irq_lock);
353 
354 	return 0;
355 
356 io_error:
357 	mutex_unlock(&rt722->disable_irq_lock);
358 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
359 	return ret;
360 }
361 
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,
366 };
367 
368 static int rt722_sdca_sdw_probe(struct sdw_slave *slave,
369 				const struct sdw_device_id *id)
370 {
371 	struct regmap *regmap, *mbq_regmap;
372 
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);
377 
378 	regmap = devm_regmap_init_sdw(slave, &rt722_sdca_regmap);
379 	if (IS_ERR(regmap))
380 		return PTR_ERR(regmap);
381 
382 	return rt722_sdca_init(&slave->dev, regmap, mbq_regmap, slave);
383 }
384 
385 static int rt722_sdca_sdw_remove(struct sdw_slave *slave)
386 {
387 	struct rt722_sdca_priv *rt722 = dev_get_drvdata(&slave->dev);
388 
389 	if (rt722->hw_init) {
390 		cancel_delayed_work_sync(&rt722->jack_detect_work);
391 		cancel_delayed_work_sync(&rt722->jack_btn_check_work);
392 	}
393 
394 	if (rt722->first_hw_init)
395 		pm_runtime_disable(&slave->dev);
396 
397 	mutex_destroy(&rt722->calibrate_mutex);
398 	mutex_destroy(&rt722->disable_irq_lock);
399 
400 	return 0;
401 }
402 
403 static const struct sdw_device_id rt722_sdca_id[] = {
404 	SDW_SLAVE_ENTRY_EXT(0x025d, 0x722, 0x3, 0x1, 0),
405 	{},
406 };
407 MODULE_DEVICE_TABLE(sdw, rt722_sdca_id);
408 
409 static int __maybe_unused rt722_sdca_dev_suspend(struct device *dev)
410 {
411 	struct rt722_sdca_priv *rt722 = dev_get_drvdata(dev);
412 
413 	if (!rt722->hw_init)
414 		return 0;
415 
416 	cancel_delayed_work_sync(&rt722->jack_detect_work);
417 	cancel_delayed_work_sync(&rt722->jack_btn_check_work);
418 
419 	regcache_cache_only(rt722->regmap, true);
420 	regcache_cache_only(rt722->mbq_regmap, true);
421 
422 	return 0;
423 }
424 
425 static int __maybe_unused rt722_sdca_dev_system_suspend(struct device *dev)
426 {
427 	struct rt722_sdca_priv *rt722_sdca = dev_get_drvdata(dev);
428 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
429 	int ret1, ret2;
430 
431 	if (!rt722_sdca->hw_init)
432 		return 0;
433 
434 	/*
435 	 * prevent new interrupts from being handled after the
436 	 * deferred work completes and before the parent disables
437 	 * interrupts on the link
438 	 */
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);
446 
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__);
450 	}
451 
452 	return rt722_sdca_dev_suspend(dev);
453 }
454 
455 #define RT722_PROBE_TIMEOUT 5000
456 
457 static int __maybe_unused rt722_sdca_dev_resume(struct device *dev)
458 {
459 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
460 	struct rt722_sdca_priv *rt722 = dev_get_drvdata(dev);
461 	unsigned long time;
462 
463 	if (!rt722->first_hw_init)
464 		return 0;
465 
466 	if (!slave->unattach_request) {
467 		if (rt722->disable_irq == true) {
468 			mutex_lock(&rt722->disable_irq_lock);
469 			sdw_write_no_pm(slave, SDW_SCP_SDCA_INTMASK1, SDW_SCP_SDCA_INTMASK_SDCA_6);
470 			sdw_write_no_pm(slave, SDW_SCP_SDCA_INTMASK2, SDW_SCP_SDCA_INTMASK_SDCA_8);
471 			rt722->disable_irq = false;
472 			mutex_unlock(&rt722->disable_irq_lock);
473 		}
474 		goto regmap_sync;
475 	}
476 
477 	time = wait_for_completion_timeout(&slave->initialization_complete,
478 				msecs_to_jiffies(RT722_PROBE_TIMEOUT));
479 	if (!time) {
480 		dev_err(&slave->dev, "Initialization not complete, timed out\n");
481 		sdw_show_ping_status(slave->bus, true);
482 
483 		return -ETIMEDOUT;
484 	}
485 
486 regmap_sync:
487 	slave->unattach_request = 0;
488 	regcache_cache_only(rt722->regmap, false);
489 	regcache_sync(rt722->regmap);
490 	regcache_cache_only(rt722->mbq_regmap, false);
491 	regcache_sync(rt722->mbq_regmap);
492 	return 0;
493 }
494 
495 static const struct dev_pm_ops rt722_sdca_pm = {
496 	SET_SYSTEM_SLEEP_PM_OPS(rt722_sdca_dev_system_suspend, rt722_sdca_dev_resume)
497 	SET_RUNTIME_PM_OPS(rt722_sdca_dev_suspend, rt722_sdca_dev_resume, NULL)
498 };
499 
500 static struct sdw_driver rt722_sdca_sdw_driver = {
501 	.driver = {
502 		.name = "rt722-sdca",
503 		.owner = THIS_MODULE,
504 		.pm = &rt722_sdca_pm,
505 	},
506 	.probe = rt722_sdca_sdw_probe,
507 	.remove = rt722_sdca_sdw_remove,
508 	.ops = &rt722_sdca_slave_ops,
509 	.id_table = rt722_sdca_id,
510 };
511 module_sdw_driver(rt722_sdca_sdw_driver);
512 
513 MODULE_DESCRIPTION("ASoC RT722 SDCA SDW driver");
514 MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>");
515 MODULE_LICENSE("GPL");
516