xref: /linux/sound/soc/codecs/rt711-sdca-sdw.c (revision fbf46565c67c626849c7ce2a326972d3008d2a91)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // rt711-sdw-sdca.c -- rt711 SDCA ALSA SoC audio driver
4 //
5 // Copyright(c) 2021 Realtek Semiconductor Corp.
6 //
7 //
8 
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>
15 
16 #include "rt711-sdca.h"
17 #include "rt711-sdca-sdw.h"
18 
19 static bool rt711_sdca_readable_register(struct device *dev, unsigned int reg)
20 {
21 	switch (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:
32 	case 0x2f60:
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:
39 		return true;
40 	default:
41 		return false;
42 	}
43 }
44 
45 static bool rt711_sdca_volatile_register(struct device *dev, unsigned int reg)
46 {
47 	switch (reg) {
48 	case 0x201b:
49 	case 0x201c:
50 	case 0x201d:
51 	case 0x201f:
52 	case 0x2021:
53 	case 0x2023:
54 	case 0x2230:
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:
62 		return true;
63 	default:
64 		return false;
65 	}
66 }
67 
68 static bool rt711_sdca_mbq_readable_register(struct device *dev, unsigned int reg)
69 {
70 	switch (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):
89 		return true;
90 	default:
91 		return false;
92 	}
93 }
94 
95 static bool rt711_sdca_mbq_volatile_register(struct device *dev, unsigned int reg)
96 {
97 	switch (reg) {
98 	case 0x2000000:
99 	case 0x200001a:
100 	case 0x2000046:
101 	case 0x2000080:
102 	case 0x2000081:
103 	case 0x2000083:
104 	case 0x5800000:
105 	case 0x5800001:
106 	case 0x5f00001:
107 	case 0x6100008:
108 		return true;
109 	default:
110 		return false;
111 	}
112 }
113 
114 static const struct regmap_config rt711_sdca_regmap = {
115 	.reg_bits = 32,
116 	.val_bits = 8,
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_RBTREE,
123 	.use_single_read = true,
124 	.use_single_write = true,
125 };
126 
127 static const struct regmap_config rt711_sdca_mbq_regmap = {
128 	.name = "sdw-mbq",
129 	.reg_bits = 32,
130 	.val_bits = 16,
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_RBTREE,
137 	.use_single_read = true,
138 	.use_single_write = true,
139 };
140 
141 static int rt711_sdca_update_status(struct sdw_slave *slave,
142 				enum sdw_slave_status status)
143 {
144 	struct rt711_sdca_priv *rt711 = dev_get_drvdata(&slave->dev);
145 
146 	/* Update the status */
147 	rt711->status = status;
148 
149 	if (status == SDW_SLAVE_UNATTACHED)
150 		rt711->hw_init = false;
151 
152 	if (status == SDW_SLAVE_ATTACHED) {
153 		if (rt711->hs_jack) {
154 			/*
155 			 * Due to the SCP_SDCA_INTMASK will be cleared by any reset, and then
156 			 * if the device attached again, we will need to set the setting back.
157 			 * It could avoid losing the jack detection interrupt.
158 			 * This also could sync with the cache value as the rt711_sdca_jack_init set.
159 			 */
160 			sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK1,
161 				SDW_SCP_SDCA_INTMASK_SDCA_0);
162 			sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK2,
163 				SDW_SCP_SDCA_INTMASK_SDCA_8);
164 		}
165 	}
166 
167 	/*
168 	 * Perform initialization only if slave status is present and
169 	 * hw_init flag is false
170 	 */
171 	if (rt711->hw_init || rt711->status != SDW_SLAVE_ATTACHED)
172 		return 0;
173 
174 	/* perform I/O transfers required for Slave initialization */
175 	return rt711_sdca_io_init(&slave->dev, slave);
176 }
177 
178 static int rt711_sdca_read_prop(struct sdw_slave *slave)
179 {
180 	struct sdw_slave_prop *prop = &slave->prop;
181 	int nval;
182 	int i, j;
183 	u32 bit;
184 	unsigned long addr;
185 	struct sdw_dpn_prop *dpn;
186 
187 	prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
188 	prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
189 	prop->is_sdca = true;
190 
191 	prop->paging_support = true;
192 
193 	/* first we need to allocate memory for set bits in port lists */
194 	prop->source_ports = 0x14; /* BITMAP: 00010100 */
195 	prop->sink_ports = 0x8; /* BITMAP:  00001000 */
196 
197 	nval = hweight32(prop->source_ports);
198 	prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
199 		sizeof(*prop->src_dpn_prop), GFP_KERNEL);
200 	if (!prop->src_dpn_prop)
201 		return -ENOMEM;
202 
203 	i = 0;
204 	dpn = prop->src_dpn_prop;
205 	addr = prop->source_ports;
206 	for_each_set_bit(bit, &addr, 32) {
207 		dpn[i].num = bit;
208 		dpn[i].type = SDW_DPN_FULL;
209 		dpn[i].simple_ch_prep_sm = true;
210 		dpn[i].ch_prep_timeout = 10;
211 		i++;
212 	}
213 
214 	/* do this again for sink now */
215 	nval = hweight32(prop->sink_ports);
216 	prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
217 		sizeof(*prop->sink_dpn_prop), GFP_KERNEL);
218 	if (!prop->sink_dpn_prop)
219 		return -ENOMEM;
220 
221 	j = 0;
222 	dpn = prop->sink_dpn_prop;
223 	addr = prop->sink_ports;
224 	for_each_set_bit(bit, &addr, 32) {
225 		dpn[j].num = bit;
226 		dpn[j].type = SDW_DPN_FULL;
227 		dpn[j].simple_ch_prep_sm = true;
228 		dpn[j].ch_prep_timeout = 10;
229 		j++;
230 	}
231 
232 	/* set the timeout values */
233 	prop->clk_stop_timeout = 700;
234 
235 	/* wake-up event */
236 	prop->wake_capable = 1;
237 
238 	return 0;
239 }
240 
241 static int rt711_sdca_interrupt_callback(struct sdw_slave *slave,
242 					struct sdw_slave_intr_status *status)
243 {
244 	struct rt711_sdca_priv *rt711 = dev_get_drvdata(&slave->dev);
245 	int ret, stat;
246 	int count = 0, retry = 3;
247 	unsigned int sdca_cascade, scp_sdca_stat1, scp_sdca_stat2 = 0;
248 
249 	dev_dbg(&slave->dev,
250 		"%s control_port_stat=%x, sdca_cascade=%x", __func__,
251 		status->control_port, status->sdca_cascade);
252 
253 	if (cancel_delayed_work_sync(&rt711->jack_detect_work)) {
254 		dev_warn(&slave->dev, "%s the pending delayed_work was cancelled", __func__);
255 		/* avoid the HID owner doesn't change to device */
256 		if (rt711->scp_sdca_stat2)
257 			scp_sdca_stat2 = rt711->scp_sdca_stat2;
258 	}
259 
260 	/*
261 	 * The critical section below intentionally protects a rather large piece of code.
262 	 * We don't want to allow the system suspend to disable an interrupt while we are
263 	 * processing it, which could be problematic given the quirky SoundWire interrupt
264 	 * scheme. We do want however to prevent new workqueues from being scheduled if
265 	 * the disable_irq flag was set during system suspend.
266 	 */
267 	mutex_lock(&rt711->disable_irq_lock);
268 
269 	ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT1);
270 	if (ret < 0)
271 		goto io_error;
272 	rt711->scp_sdca_stat1 = ret;
273 	ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT2);
274 	if (ret < 0)
275 		goto io_error;
276 	rt711->scp_sdca_stat2 = ret;
277 	if (scp_sdca_stat2)
278 		rt711->scp_sdca_stat2 |= scp_sdca_stat2;
279 
280 	do {
281 		/* clear flag */
282 		ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT1);
283 		if (ret < 0)
284 			goto io_error;
285 		if (ret & SDW_SCP_SDCA_INTMASK_SDCA_0) {
286 			ret = sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INT1,
287 						SDW_SCP_SDCA_INTMASK_SDCA_0);
288 			if (ret < 0)
289 				goto io_error;
290 		}
291 		ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT2);
292 		if (ret < 0)
293 			goto io_error;
294 		if (ret & SDW_SCP_SDCA_INTMASK_SDCA_8) {
295 			ret = sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INT2,
296 						SDW_SCP_SDCA_INTMASK_SDCA_8);
297 			if (ret < 0)
298 				goto io_error;
299 		}
300 
301 		/* check if flag clear or not */
302 		ret = sdw_read_no_pm(rt711->slave, SDW_DP0_INT);
303 		if (ret < 0)
304 			goto io_error;
305 		sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
306 
307 		ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT1);
308 		if (ret < 0)
309 			goto io_error;
310 		scp_sdca_stat1 = ret & SDW_SCP_SDCA_INTMASK_SDCA_0;
311 
312 		ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT2);
313 		if (ret < 0)
314 			goto io_error;
315 		scp_sdca_stat2 = ret & SDW_SCP_SDCA_INTMASK_SDCA_8;
316 
317 		stat = scp_sdca_stat1 || scp_sdca_stat2 || sdca_cascade;
318 
319 		count++;
320 	} while (stat != 0 && count < retry);
321 
322 	if (stat)
323 		dev_warn(&slave->dev,
324 			"%s scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__,
325 			rt711->scp_sdca_stat1, rt711->scp_sdca_stat2);
326 
327 	if (status->sdca_cascade && !rt711->disable_irq)
328 		mod_delayed_work(system_power_efficient_wq,
329 			&rt711->jack_detect_work, msecs_to_jiffies(30));
330 
331 	mutex_unlock(&rt711->disable_irq_lock);
332 
333 	return 0;
334 
335 io_error:
336 	mutex_unlock(&rt711->disable_irq_lock);
337 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
338 	return ret;
339 }
340 
341 static struct sdw_slave_ops rt711_sdca_slave_ops = {
342 	.read_prop = rt711_sdca_read_prop,
343 	.interrupt_callback = rt711_sdca_interrupt_callback,
344 	.update_status = rt711_sdca_update_status,
345 };
346 
347 static int rt711_sdca_sdw_probe(struct sdw_slave *slave,
348 				const struct sdw_device_id *id)
349 {
350 	struct regmap *regmap, *mbq_regmap;
351 
352 	/* Regmap Initialization */
353 	mbq_regmap = devm_regmap_init_sdw_mbq(slave, &rt711_sdca_mbq_regmap);
354 	if (IS_ERR(mbq_regmap))
355 		return PTR_ERR(mbq_regmap);
356 
357 	regmap = devm_regmap_init_sdw(slave, &rt711_sdca_regmap);
358 	if (IS_ERR(regmap))
359 		return PTR_ERR(regmap);
360 
361 	return rt711_sdca_init(&slave->dev, regmap, mbq_regmap, slave);
362 }
363 
364 static int rt711_sdca_sdw_remove(struct sdw_slave *slave)
365 {
366 	struct rt711_sdca_priv *rt711 = dev_get_drvdata(&slave->dev);
367 
368 	if (rt711->hw_init) {
369 		cancel_delayed_work_sync(&rt711->jack_detect_work);
370 		cancel_delayed_work_sync(&rt711->jack_btn_check_work);
371 	}
372 
373 	if (rt711->first_hw_init)
374 		pm_runtime_disable(&slave->dev);
375 
376 	mutex_destroy(&rt711->calibrate_mutex);
377 	mutex_destroy(&rt711->disable_irq_lock);
378 
379 	return 0;
380 }
381 
382 static const struct sdw_device_id rt711_sdca_id[] = {
383 	SDW_SLAVE_ENTRY_EXT(0x025d, 0x711, 0x3, 0x1, 0),
384 	{},
385 };
386 MODULE_DEVICE_TABLE(sdw, rt711_sdca_id);
387 
388 static int __maybe_unused rt711_sdca_dev_suspend(struct device *dev)
389 {
390 	struct rt711_sdca_priv *rt711 = dev_get_drvdata(dev);
391 
392 	if (!rt711->hw_init)
393 		return 0;
394 
395 	cancel_delayed_work_sync(&rt711->jack_detect_work);
396 	cancel_delayed_work_sync(&rt711->jack_btn_check_work);
397 
398 	regcache_cache_only(rt711->regmap, true);
399 	regcache_cache_only(rt711->mbq_regmap, true);
400 
401 	return 0;
402 }
403 
404 static int __maybe_unused rt711_sdca_dev_system_suspend(struct device *dev)
405 {
406 	struct rt711_sdca_priv *rt711_sdca = dev_get_drvdata(dev);
407 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
408 	int ret1, ret2;
409 
410 	if (!rt711_sdca->hw_init)
411 		return 0;
412 
413 	/*
414 	 * prevent new interrupts from being handled after the
415 	 * deferred work completes and before the parent disables
416 	 * interrupts on the link
417 	 */
418 	mutex_lock(&rt711_sdca->disable_irq_lock);
419 	rt711_sdca->disable_irq = true;
420 	ret1 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK1,
421 				SDW_SCP_SDCA_INTMASK_SDCA_0, 0);
422 	ret2 = sdw_update_no_pm(slave, SDW_SCP_SDCA_INTMASK2,
423 				SDW_SCP_SDCA_INTMASK_SDCA_8, 0);
424 	mutex_unlock(&rt711_sdca->disable_irq_lock);
425 
426 	if (ret1 < 0 || ret2 < 0) {
427 		/* log but don't prevent suspend from happening */
428 		dev_dbg(&slave->dev, "%s: could not disable SDCA interrupts\n:", __func__);
429 	}
430 
431 	return rt711_sdca_dev_suspend(dev);
432 }
433 
434 #define RT711_PROBE_TIMEOUT 5000
435 
436 static int __maybe_unused rt711_sdca_dev_resume(struct device *dev)
437 {
438 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
439 	struct rt711_sdca_priv *rt711 = dev_get_drvdata(dev);
440 	unsigned long time;
441 
442 	if (!rt711->first_hw_init)
443 		return 0;
444 
445 	if (!slave->unattach_request)
446 		goto regmap_sync;
447 
448 	time = wait_for_completion_timeout(&slave->initialization_complete,
449 				msecs_to_jiffies(RT711_PROBE_TIMEOUT));
450 	if (!time) {
451 		dev_err(&slave->dev, "Initialization not complete, timed out\n");
452 		sdw_show_ping_status(slave->bus, true);
453 
454 		return -ETIMEDOUT;
455 	}
456 
457 regmap_sync:
458 	slave->unattach_request = 0;
459 	regcache_cache_only(rt711->regmap, false);
460 	regcache_sync(rt711->regmap);
461 	regcache_cache_only(rt711->mbq_regmap, false);
462 	regcache_sync(rt711->mbq_regmap);
463 	return 0;
464 }
465 
466 static const struct dev_pm_ops rt711_sdca_pm = {
467 	SET_SYSTEM_SLEEP_PM_OPS(rt711_sdca_dev_system_suspend, rt711_sdca_dev_resume)
468 	SET_RUNTIME_PM_OPS(rt711_sdca_dev_suspend, rt711_sdca_dev_resume, NULL)
469 };
470 
471 static struct sdw_driver rt711_sdca_sdw_driver = {
472 	.driver = {
473 		.name = "rt711-sdca",
474 		.owner = THIS_MODULE,
475 		.pm = &rt711_sdca_pm,
476 	},
477 	.probe = rt711_sdca_sdw_probe,
478 	.remove = rt711_sdca_sdw_remove,
479 	.ops = &rt711_sdca_slave_ops,
480 	.id_table = rt711_sdca_id,
481 };
482 module_sdw_driver(rt711_sdca_sdw_driver);
483 
484 MODULE_DESCRIPTION("ASoC RT711 SDCA SDW driver");
485 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
486 MODULE_LICENSE("GPL");
487