xref: /linux/sound/soc/codecs/rt722-sdca-sdw.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
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 0x3110000:
72 	case 0x5300000 ... 0x5300002:
73 	case 0x5400002:
74 	case 0x5600000 ... 0x5600007:
75 	case 0x5700000 ... 0x5700004:
76 	case 0x5800000 ... 0x5800004:
77 	case 0x5b00003:
78 	case 0x5c00011:
79 	case 0x5d00006:
80 	case 0x5f00000 ... 0x5f0000d:
81 	case 0x5f00030:
82 	case 0x6100000 ... 0x6100051:
83 	case 0x6100055 ... 0x6100057:
84 	case 0x6100062:
85 	case 0x6100064 ... 0x6100065:
86 	case 0x6100067:
87 	case 0x6100070 ... 0x610007c:
88 	case 0x6100080:
89 	case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT722_SDCA_ENT_USER_FU1E, RT722_SDCA_CTL_FU_VOLUME,
90 			CH_01):
91 	case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT722_SDCA_ENT_USER_FU1E, RT722_SDCA_CTL_FU_VOLUME,
92 			CH_02):
93 	case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT722_SDCA_ENT_USER_FU1E, RT722_SDCA_CTL_FU_VOLUME,
94 			CH_03):
95 	case SDW_SDCA_CTL(FUNC_NUM_MIC_ARRAY, RT722_SDCA_ENT_USER_FU1E, RT722_SDCA_CTL_FU_VOLUME,
96 			CH_04):
97 	case SDW_SDCA_CTL(FUNC_NUM_AMP, RT722_SDCA_ENT_USER_FU06, RT722_SDCA_CTL_FU_VOLUME, CH_L):
98 	case SDW_SDCA_CTL(FUNC_NUM_AMP, RT722_SDCA_ENT_USER_FU06, RT722_SDCA_CTL_FU_VOLUME, CH_R):
99 	case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_USER_FU05, RT722_SDCA_CTL_FU_VOLUME,
100 			CH_L):
101 	case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_USER_FU05, RT722_SDCA_CTL_FU_VOLUME,
102 			CH_R):
103 	case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_USER_FU0F, RT722_SDCA_CTL_FU_VOLUME,
104 			CH_L):
105 	case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_USER_FU0F, RT722_SDCA_CTL_FU_VOLUME,
106 			CH_R):
107 	case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_PLATFORM_FU44,
108 			RT722_SDCA_CTL_FU_CH_GAIN, CH_L):
109 	case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT722_SDCA_ENT_PLATFORM_FU44,
110 			RT722_SDCA_CTL_FU_CH_GAIN, CH_R):
111 		return true;
112 	default:
113 		return false;
114 	}
115 }
116 
117 static bool rt722_sdca_mbq_volatile_register(struct device *dev, unsigned int reg)
118 {
119 	switch (reg) {
120 	case 0x2000000:
121 	case 0x200000d:
122 	case 0x2000019:
123 	case 0x2000020:
124 	case 0x2000030:
125 	case 0x2000046:
126 	case 0x2000067:
127 	case 0x2000084:
128 	case 0x2000086:
129 	case 0x3110000:
130 		return true;
131 	default:
132 		return false;
133 	}
134 }
135 
136 static const struct regmap_config rt722_sdca_regmap = {
137 	.reg_bits = 32,
138 	.val_bits = 8,
139 	.readable_reg = rt722_sdca_readable_register,
140 	.volatile_reg = rt722_sdca_volatile_register,
141 	.max_register = 0x44ffffff,
142 	.reg_defaults = rt722_sdca_reg_defaults,
143 	.num_reg_defaults = ARRAY_SIZE(rt722_sdca_reg_defaults),
144 	.cache_type = REGCACHE_MAPLE,
145 	.use_single_read = true,
146 	.use_single_write = true,
147 };
148 
149 static const struct regmap_config rt722_sdca_mbq_regmap = {
150 	.name = "sdw-mbq",
151 	.reg_bits = 32,
152 	.val_bits = 16,
153 	.readable_reg = rt722_sdca_mbq_readable_register,
154 	.volatile_reg = rt722_sdca_mbq_volatile_register,
155 	.max_register = 0x41000312,
156 	.reg_defaults = rt722_sdca_mbq_defaults,
157 	.num_reg_defaults = ARRAY_SIZE(rt722_sdca_mbq_defaults),
158 	.cache_type = REGCACHE_MAPLE,
159 	.use_single_read = true,
160 	.use_single_write = true,
161 };
162 
163 static int rt722_sdca_update_status(struct sdw_slave *slave,
164 				enum sdw_slave_status status)
165 {
166 	struct rt722_sdca_priv *rt722 = dev_get_drvdata(&slave->dev);
167 
168 	if (status == SDW_SLAVE_UNATTACHED)
169 		rt722->hw_init = false;
170 
171 	if (status == SDW_SLAVE_ATTACHED) {
172 		if (rt722->hs_jack) {
173 		/*
174 		 * Due to the SCP_SDCA_INTMASK will be cleared by any reset, and then
175 		 * if the device attached again, we will need to set the setting back.
176 		 * It could avoid losing the jack detection interrupt.
177 		 * This also could sync with the cache value as the rt722_sdca_jack_init set.
178 		 */
179 			sdw_write_no_pm(rt722->slave, SDW_SCP_SDCA_INTMASK1,
180 				SDW_SCP_SDCA_INTMASK_SDCA_0);
181 			sdw_write_no_pm(rt722->slave, SDW_SCP_SDCA_INTMASK2,
182 				SDW_SCP_SDCA_INTMASK_SDCA_8);
183 		}
184 	}
185 
186 	/*
187 	 * Perform initialization only if slave status is present and
188 	 * hw_init flag is false
189 	 */
190 	if (rt722->hw_init || status != SDW_SLAVE_ATTACHED)
191 		return 0;
192 
193 	/* perform I/O transfers required for Slave initialization */
194 	return rt722_sdca_io_init(&slave->dev, slave);
195 }
196 
197 static int rt722_sdca_read_prop(struct sdw_slave *slave)
198 {
199 	struct sdw_slave_prop *prop = &slave->prop;
200 	int nval;
201 	int i, j;
202 	u32 bit;
203 	unsigned long addr;
204 	struct sdw_dpn_prop *dpn;
205 
206 	prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
207 	prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
208 
209 	prop->paging_support = true;
210 
211 	/*
212 	 * port = 1 for headphone playback
213 	 * port = 2 for headset-mic capture
214 	 * port = 3 for speaker playback
215 	 * port = 6 for digital-mic capture
216 	 */
217 	prop->source_ports = BIT(6) | BIT(2); /* BITMAP: 01000100 */
218 	prop->sink_ports = BIT(3) | BIT(1); /* BITMAP:  00001010 */
219 
220 	nval = hweight32(prop->source_ports);
221 	prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
222 		sizeof(*prop->src_dpn_prop), GFP_KERNEL);
223 	if (!prop->src_dpn_prop)
224 		return -ENOMEM;
225 
226 	i = 0;
227 	dpn = prop->src_dpn_prop;
228 	addr = prop->source_ports;
229 	for_each_set_bit(bit, &addr, 32) {
230 		dpn[i].num = bit;
231 		dpn[i].type = SDW_DPN_FULL;
232 		dpn[i].simple_ch_prep_sm = true;
233 		dpn[i].ch_prep_timeout = 10;
234 		i++;
235 	}
236 
237 	/* do this again for sink now */
238 	nval = hweight32(prop->sink_ports);
239 	prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
240 		sizeof(*prop->sink_dpn_prop), GFP_KERNEL);
241 	if (!prop->sink_dpn_prop)
242 		return -ENOMEM;
243 
244 	j = 0;
245 	dpn = prop->sink_dpn_prop;
246 	addr = prop->sink_ports;
247 	for_each_set_bit(bit, &addr, 32) {
248 		dpn[j].num = bit;
249 		dpn[j].type = SDW_DPN_FULL;
250 		dpn[j].simple_ch_prep_sm = true;
251 		dpn[j].ch_prep_timeout = 10;
252 		j++;
253 	}
254 
255 	/* set the timeout values */
256 	prop->clk_stop_timeout = 900;
257 
258 	/* wake-up event */
259 	prop->wake_capable = 1;
260 
261 	/* Three data lanes are supported by rt722-sdca codec */
262 	prop->lane_control_support = true;
263 
264 	return 0;
265 }
266 
267 static int rt722_sdca_interrupt_callback(struct sdw_slave *slave,
268 					struct sdw_slave_intr_status *status)
269 {
270 	struct rt722_sdca_priv *rt722 = dev_get_drvdata(&slave->dev);
271 	int ret, stat;
272 	int count = 0, retry = 3;
273 	unsigned int sdca_cascade, scp_sdca_stat1, scp_sdca_stat2 = 0;
274 
275 	if (cancel_delayed_work_sync(&rt722->jack_detect_work)) {
276 		dev_warn(&slave->dev, "%s the pending delayed_work was cancelled", __func__);
277 		/* avoid the HID owner doesn't change to device */
278 		if (rt722->scp_sdca_stat2)
279 			scp_sdca_stat2 = rt722->scp_sdca_stat2;
280 	}
281 
282 	/*
283 	 * The critical section below intentionally protects a rather large piece of code.
284 	 * We don't want to allow the system suspend to disable an interrupt while we are
285 	 * processing it, which could be problematic given the quirky SoundWire interrupt
286 	 * scheme. We do want however to prevent new workqueues from being scheduled if
287 	 * the disable_irq flag was set during system suspend.
288 	 */
289 	mutex_lock(&rt722->disable_irq_lock);
290 
291 	ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT1);
292 	if (ret < 0)
293 		goto io_error;
294 	rt722->scp_sdca_stat1 = ret;
295 	ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT2);
296 	if (ret < 0)
297 		goto io_error;
298 	rt722->scp_sdca_stat2 = ret;
299 	if (scp_sdca_stat2)
300 		rt722->scp_sdca_stat2 |= scp_sdca_stat2;
301 	do {
302 		/* clear flag */
303 		ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT1);
304 		if (ret < 0)
305 			goto io_error;
306 		if (ret & SDW_SCP_SDCA_INTMASK_SDCA_0) {
307 			ret = sdw_update_no_pm(rt722->slave, SDW_SCP_SDCA_INT1,
308 				SDW_SCP_SDCA_INT_SDCA_0, SDW_SCP_SDCA_INT_SDCA_0);
309 			if (ret < 0)
310 				goto io_error;
311 		}
312 
313 		ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT2);
314 		if (ret < 0)
315 			goto io_error;
316 		if (ret & SDW_SCP_SDCA_INTMASK_SDCA_8) {
317 			ret = sdw_write_no_pm(rt722->slave, SDW_SCP_SDCA_INT2,
318 						SDW_SCP_SDCA_INTMASK_SDCA_8);
319 			if (ret < 0)
320 				goto io_error;
321 		}
322 
323 		/* check if flag clear or not */
324 		ret = sdw_read_no_pm(rt722->slave, SDW_DP0_INT);
325 		if (ret < 0)
326 			goto io_error;
327 		sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
328 
329 		ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT1);
330 		if (ret < 0)
331 			goto io_error;
332 		scp_sdca_stat1 = ret & SDW_SCP_SDCA_INTMASK_SDCA_0;
333 
334 		ret = sdw_read_no_pm(rt722->slave, SDW_SCP_SDCA_INT2);
335 		if (ret < 0)
336 			goto io_error;
337 		scp_sdca_stat2 = ret & SDW_SCP_SDCA_INTMASK_SDCA_8;
338 
339 		stat = scp_sdca_stat1 || scp_sdca_stat2 || sdca_cascade;
340 
341 		count++;
342 	} while (stat != 0 && count < retry);
343 
344 	if (stat)
345 		dev_warn(&slave->dev,
346 			"%s scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__,
347 			rt722->scp_sdca_stat1, rt722->scp_sdca_stat2);
348 
349 	if (status->sdca_cascade && !rt722->disable_irq)
350 		mod_delayed_work(system_power_efficient_wq,
351 			&rt722->jack_detect_work, msecs_to_jiffies(280));
352 
353 	mutex_unlock(&rt722->disable_irq_lock);
354 
355 	return 0;
356 
357 io_error:
358 	mutex_unlock(&rt722->disable_irq_lock);
359 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
360 	return ret;
361 }
362 
363 static const struct sdw_slave_ops rt722_sdca_slave_ops = {
364 	.read_prop = rt722_sdca_read_prop,
365 	.interrupt_callback = rt722_sdca_interrupt_callback,
366 	.update_status = rt722_sdca_update_status,
367 };
368 
369 static int rt722_sdca_sdw_probe(struct sdw_slave *slave,
370 				const struct sdw_device_id *id)
371 {
372 	struct regmap *regmap, *mbq_regmap;
373 
374 	/* Regmap Initialization */
375 	mbq_regmap = devm_regmap_init_sdw_mbq(slave, &rt722_sdca_mbq_regmap);
376 	if (IS_ERR(mbq_regmap))
377 		return PTR_ERR(mbq_regmap);
378 
379 	regmap = devm_regmap_init_sdw(slave, &rt722_sdca_regmap);
380 	if (IS_ERR(regmap))
381 		return PTR_ERR(regmap);
382 
383 	return rt722_sdca_init(&slave->dev, regmap, mbq_regmap, slave);
384 }
385 
386 static int rt722_sdca_sdw_remove(struct sdw_slave *slave)
387 {
388 	struct rt722_sdca_priv *rt722 = dev_get_drvdata(&slave->dev);
389 
390 	if (rt722->hw_init) {
391 		cancel_delayed_work_sync(&rt722->jack_detect_work);
392 		cancel_delayed_work_sync(&rt722->jack_btn_check_work);
393 	}
394 
395 	if (rt722->first_hw_init)
396 		pm_runtime_disable(&slave->dev);
397 
398 	mutex_destroy(&rt722->calibrate_mutex);
399 	mutex_destroy(&rt722->disable_irq_lock);
400 
401 	return 0;
402 }
403 
404 static const struct sdw_device_id rt722_sdca_id[] = {
405 	SDW_SLAVE_ENTRY_EXT(0x025d, 0x722, 0x3, 0x1, 0),
406 	{},
407 };
408 MODULE_DEVICE_TABLE(sdw, rt722_sdca_id);
409 
410 static int __maybe_unused rt722_sdca_dev_suspend(struct device *dev)
411 {
412 	struct rt722_sdca_priv *rt722 = dev_get_drvdata(dev);
413 
414 	if (!rt722->hw_init)
415 		return 0;
416 
417 	cancel_delayed_work_sync(&rt722->jack_detect_work);
418 	cancel_delayed_work_sync(&rt722->jack_btn_check_work);
419 
420 	regcache_cache_only(rt722->regmap, true);
421 	regcache_cache_only(rt722->mbq_regmap, true);
422 
423 	return 0;
424 }
425 
426 static int __maybe_unused rt722_sdca_dev_system_suspend(struct device *dev)
427 {
428 	struct rt722_sdca_priv *rt722_sdca = dev_get_drvdata(dev);
429 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
430 	int ret1, ret2;
431 
432 	if (!rt722_sdca->hw_init)
433 		return 0;
434 
435 	/*
436 	 * prevent new interrupts from being handled after the
437 	 * deferred work completes and before the parent disables
438 	 * interrupts on the link
439 	 */
440 	mutex_lock(&rt722_sdca->disable_irq_lock);
441 	rt722_sdca->disable_irq = true;
442 	ret1 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK1,
443 				SDW_SCP_SDCA_INTMASK_SDCA_0, 0);
444 	ret2 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK2,
445 				SDW_SCP_SDCA_INTMASK_SDCA_8, 0);
446 	mutex_unlock(&rt722_sdca->disable_irq_lock);
447 
448 	if (ret1 < 0 || ret2 < 0) {
449 		/* log but don't prevent suspend from happening */
450 		dev_dbg(&slave->dev, "%s: could not disable SDCA interrupts\n:", __func__);
451 	}
452 
453 	return rt722_sdca_dev_suspend(dev);
454 }
455 
456 #define RT722_PROBE_TIMEOUT 5000
457 
458 static int __maybe_unused rt722_sdca_dev_resume(struct device *dev)
459 {
460 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
461 	struct rt722_sdca_priv *rt722 = dev_get_drvdata(dev);
462 	unsigned long time;
463 
464 	if (!rt722->first_hw_init)
465 		return 0;
466 
467 	if (!slave->unattach_request) {
468 		mutex_lock(&rt722->disable_irq_lock);
469 		if (rt722->disable_irq == true) {
470 			sdw_write_no_pm(slave, SDW_SCP_SDCA_INTMASK1, SDW_SCP_SDCA_INTMASK_SDCA_0);
471 			sdw_write_no_pm(slave, SDW_SCP_SDCA_INTMASK2, SDW_SCP_SDCA_INTMASK_SDCA_8);
472 			rt722->disable_irq = false;
473 		}
474 		mutex_unlock(&rt722->disable_irq_lock);
475 		goto regmap_sync;
476 	}
477 
478 	time = wait_for_completion_timeout(&slave->initialization_complete,
479 				msecs_to_jiffies(RT722_PROBE_TIMEOUT));
480 	if (!time) {
481 		dev_err(&slave->dev, "Initialization not complete, timed out\n");
482 		sdw_show_ping_status(slave->bus, true);
483 
484 		return -ETIMEDOUT;
485 	}
486 
487 regmap_sync:
488 	slave->unattach_request = 0;
489 	regcache_cache_only(rt722->regmap, false);
490 	regcache_sync(rt722->regmap);
491 	regcache_cache_only(rt722->mbq_regmap, false);
492 	regcache_sync(rt722->mbq_regmap);
493 	return 0;
494 }
495 
496 static const struct dev_pm_ops rt722_sdca_pm = {
497 	SET_SYSTEM_SLEEP_PM_OPS(rt722_sdca_dev_system_suspend, rt722_sdca_dev_resume)
498 	SET_RUNTIME_PM_OPS(rt722_sdca_dev_suspend, rt722_sdca_dev_resume, NULL)
499 };
500 
501 static struct sdw_driver rt722_sdca_sdw_driver = {
502 	.driver = {
503 		.name = "rt722-sdca",
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