xref: /openbmc/linux/sound/soc/codecs/rt711-sdw.c (revision 234489ac)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // rt711-sdw.c -- rt711 ALSA SoC audio driver
4 //
5 // Copyright(c) 2019 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.h>
13 #include <linux/soundwire/sdw_type.h>
14 #include <linux/soundwire/sdw_registers.h>
15 #include <linux/module.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regmap.h>
18 #include <sound/soc.h>
19 #include "rt711.h"
20 #include "rt711-sdw.h"
21 
22 static bool rt711_readable_register(struct device *dev, unsigned int reg)
23 {
24 	switch (reg) {
25 	case 0x00e0:
26 	case 0x00f0:
27 	case 0x2012 ... 0x2016:
28 	case 0x201a ... 0x2027:
29 	case 0x2029 ... 0x202a:
30 	case 0x202d ... 0x2034:
31 	case 0x2201 ... 0x2204:
32 	case 0x2206 ... 0x2212:
33 	case 0x2220 ... 0x2223:
34 	case 0x2230 ... 0x2239:
35 	case 0x2f01 ... 0x2f0f:
36 	case 0x3000 ... 0x3fff:
37 	case 0x7000 ... 0x7fff:
38 	case 0x8300 ... 0x83ff:
39 	case 0x9c00 ... 0x9cff:
40 	case 0xb900 ... 0xb9ff:
41 	case 0x752009:
42 	case 0x752011:
43 	case 0x75201a:
44 	case 0x752045:
45 	case 0x752046:
46 	case 0x752048:
47 	case 0x75204a:
48 	case 0x75206b:
49 	case 0x75206f:
50 	case 0x752080:
51 	case 0x752081:
52 	case 0x752091:
53 	case 0x755800:
54 		return true;
55 	default:
56 		return false;
57 	}
58 }
59 
60 static bool rt711_volatile_register(struct device *dev, unsigned int reg)
61 {
62 	switch (reg) {
63 	case 0x2016:
64 	case 0x201b:
65 	case 0x201c:
66 	case 0x201d:
67 	case 0x201f:
68 	case 0x2021:
69 	case 0x2023:
70 	case 0x2230:
71 	case 0x2012 ... 0x2015: /* HD-A read */
72 	case 0x202d ... 0x202f: /* BRA */
73 	case 0x2201 ... 0x2212: /* i2c debug */
74 	case 0x2220 ... 0x2223: /* decoded HD-A */
75 	case 0x9c00 ... 0x9cff:
76 	case 0xb900 ... 0xb9ff:
77 	case 0xff01:
78 	case 0x75201a:
79 	case 0x752046:
80 	case 0x752080:
81 	case 0x752081:
82 	case 0x755800:
83 		return true;
84 	default:
85 		return false;
86 	}
87 }
88 
89 static int rt711_sdw_read(void *context, unsigned int reg, unsigned int *val)
90 {
91 	struct device *dev = context;
92 	struct rt711_priv *rt711 = dev_get_drvdata(dev);
93 	unsigned int sdw_data_3, sdw_data_2, sdw_data_1, sdw_data_0;
94 	unsigned int reg2 = 0, reg3 = 0, reg4 = 0, mask, nid, val2;
95 	unsigned int is_hda_reg = 1, is_index_reg = 0;
96 	int ret;
97 
98 	if (reg > 0xffff)
99 		is_index_reg = 1;
100 
101 	mask = reg & 0xf000;
102 
103 	if (is_index_reg) { /* index registers */
104 		val2 = reg & 0xff;
105 		reg = reg >> 8;
106 		nid = reg & 0xff;
107 		ret = regmap_write(rt711->sdw_regmap, reg, 0);
108 		if (ret < 0)
109 			return ret;
110 		reg2 = reg + 0x1000;
111 		reg2 |= 0x80;
112 		ret = regmap_write(rt711->sdw_regmap, reg2, val2);
113 		if (ret < 0)
114 			return ret;
115 
116 		reg3 = RT711_PRIV_DATA_R_H | nid;
117 		ret = regmap_write(rt711->sdw_regmap,
118 			reg3, ((*val >> 8) & 0xff));
119 		if (ret < 0)
120 			return ret;
121 		reg4 = reg3 + 0x1000;
122 		reg4 |= 0x80;
123 		ret = regmap_write(rt711->sdw_regmap, reg4, (*val & 0xff));
124 		if (ret < 0)
125 			return ret;
126 	} else if (mask   == 0x3000) {
127 		reg += 0x8000;
128 		ret = regmap_write(rt711->sdw_regmap, reg, *val);
129 		if (ret < 0)
130 			return ret;
131 	} else if (mask == 0x7000) {
132 		reg += 0x2000;
133 		reg |= 0x800;
134 		ret = regmap_write(rt711->sdw_regmap,
135 			reg, ((*val >> 8) & 0xff));
136 		if (ret < 0)
137 			return ret;
138 		reg2 = reg + 0x1000;
139 		reg2 |= 0x80;
140 		ret = regmap_write(rt711->sdw_regmap, reg2, (*val & 0xff));
141 		if (ret < 0)
142 			return ret;
143 	} else if ((reg & 0xff00) == 0x8300) { /* for R channel */
144 		reg2 = reg - 0x1000;
145 		reg2 &= ~0x80;
146 		ret = regmap_write(rt711->sdw_regmap,
147 			reg2, ((*val >> 8) & 0xff));
148 		if (ret < 0)
149 			return ret;
150 		ret = regmap_write(rt711->sdw_regmap, reg, (*val & 0xff));
151 		if (ret < 0)
152 			return ret;
153 	} else if (mask == 0x9000) {
154 		ret = regmap_write(rt711->sdw_regmap,
155 			reg, ((*val >> 8) & 0xff));
156 		if (ret < 0)
157 			return ret;
158 		reg2 = reg + 0x1000;
159 		reg2 |= 0x80;
160 		ret = regmap_write(rt711->sdw_regmap, reg2, (*val & 0xff));
161 		if (ret < 0)
162 			return ret;
163 	} else if (mask == 0xb000) {
164 		ret = regmap_write(rt711->sdw_regmap, reg, *val);
165 		if (ret < 0)
166 			return ret;
167 	} else {
168 		ret = regmap_read(rt711->sdw_regmap, reg, val);
169 		if (ret < 0)
170 			return ret;
171 		is_hda_reg = 0;
172 	}
173 
174 	if (is_hda_reg || is_index_reg) {
175 		sdw_data_3 = 0;
176 		sdw_data_2 = 0;
177 		sdw_data_1 = 0;
178 		sdw_data_0 = 0;
179 		ret = regmap_read(rt711->sdw_regmap,
180 			RT711_READ_HDA_3, &sdw_data_3);
181 		if (ret < 0)
182 			return ret;
183 		ret = regmap_read(rt711->sdw_regmap,
184 			RT711_READ_HDA_2, &sdw_data_2);
185 		if (ret < 0)
186 			return ret;
187 		ret = regmap_read(rt711->sdw_regmap,
188 			RT711_READ_HDA_1, &sdw_data_1);
189 		if (ret < 0)
190 			return ret;
191 		ret = regmap_read(rt711->sdw_regmap,
192 			RT711_READ_HDA_0, &sdw_data_0);
193 		if (ret < 0)
194 			return ret;
195 		*val = ((sdw_data_3 & 0xff) << 24) |
196 			((sdw_data_2 & 0xff) << 16) |
197 			((sdw_data_1 & 0xff) << 8) | (sdw_data_0 & 0xff);
198 	}
199 
200 	if (is_hda_reg == 0)
201 		dev_dbg(dev, "[%s] %04x => %08x\n", __func__, reg, *val);
202 	else if (is_index_reg)
203 		dev_dbg(dev, "[%s] %04x %04x %04x %04x => %08x\n",
204 			__func__, reg, reg2, reg3, reg4, *val);
205 	else
206 		dev_dbg(dev, "[%s] %04x %04x => %08x\n",
207 			__func__, reg, reg2, *val);
208 
209 	return 0;
210 }
211 
212 static int rt711_sdw_write(void *context, unsigned int reg, unsigned int val)
213 {
214 	struct device *dev = context;
215 	struct rt711_priv *rt711 = dev_get_drvdata(dev);
216 	unsigned int reg2 = 0, reg3, reg4, nid, mask, val2;
217 	unsigned int is_index_reg = 0;
218 	int ret;
219 
220 	if (reg > 0xffff)
221 		is_index_reg = 1;
222 
223 	mask = reg & 0xf000;
224 
225 	if (is_index_reg) { /* index registers */
226 		val2 = reg & 0xff;
227 		reg = reg >> 8;
228 		nid = reg & 0xff;
229 		ret = regmap_write(rt711->sdw_regmap, reg, 0);
230 		if (ret < 0)
231 			return ret;
232 		reg2 = reg + 0x1000;
233 		reg2 |= 0x80;
234 		ret = regmap_write(rt711->sdw_regmap, reg2, val2);
235 		if (ret < 0)
236 			return ret;
237 
238 		reg3 = RT711_PRIV_DATA_W_H | nid;
239 		ret = regmap_write(rt711->sdw_regmap,
240 			reg3, ((val >> 8) & 0xff));
241 		if (ret < 0)
242 			return ret;
243 		reg4 = reg3 + 0x1000;
244 		reg4 |= 0x80;
245 		ret = regmap_write(rt711->sdw_regmap, reg4, (val & 0xff));
246 		if (ret < 0)
247 			return ret;
248 		is_index_reg = 1;
249 	} else if (reg < 0x4fff) {
250 		ret = regmap_write(rt711->sdw_regmap, reg, val);
251 		if (ret < 0)
252 			return ret;
253 	} else if (reg == RT711_FUNC_RESET) {
254 		ret = regmap_write(rt711->sdw_regmap, reg, val);
255 		if (ret < 0)
256 			return ret;
257 	} else if (mask == 0x7000) {
258 		ret = regmap_write(rt711->sdw_regmap,
259 			reg, ((val >> 8) & 0xff));
260 		if (ret < 0)
261 			return ret;
262 		reg2 = reg + 0x1000;
263 		reg2 |= 0x80;
264 		ret = regmap_write(rt711->sdw_regmap, reg2, (val & 0xff));
265 		if (ret < 0)
266 			return ret;
267 	} else if ((reg & 0xff00) == 0x8300) {  /* for R channel */
268 		reg2 = reg - 0x1000;
269 		reg2 &= ~0x80;
270 		ret = regmap_write(rt711->sdw_regmap,
271 			reg2, ((val >> 8) & 0xff));
272 		if (ret < 0)
273 			return ret;
274 		ret = regmap_write(rt711->sdw_regmap, reg, (val & 0xff));
275 		if (ret < 0)
276 			return ret;
277 	}
278 
279 	if (reg2 == 0)
280 		dev_dbg(dev, "[%s] %04x <= %04x\n", __func__, reg, val);
281 	else if (is_index_reg)
282 		dev_dbg(dev, "[%s] %04x %04x %04x %04x <= %04x %04x\n",
283 			__func__, reg, reg2, reg3, reg4, val2, val);
284 	else
285 		dev_dbg(dev, "[%s] %04x %04x <= %04x\n",
286 			__func__, reg, reg2, val);
287 
288 	return 0;
289 }
290 
291 static const struct regmap_config rt711_regmap = {
292 	.reg_bits = 24,
293 	.val_bits = 32,
294 	.readable_reg = rt711_readable_register,
295 	.volatile_reg = rt711_volatile_register,
296 	.max_register = 0x755800,
297 	.reg_defaults = rt711_reg_defaults,
298 	.num_reg_defaults = ARRAY_SIZE(rt711_reg_defaults),
299 	.cache_type = REGCACHE_RBTREE,
300 	.use_single_read = true,
301 	.use_single_write = true,
302 	.reg_read = rt711_sdw_read,
303 	.reg_write = rt711_sdw_write,
304 };
305 
306 static const struct regmap_config rt711_sdw_regmap = {
307 	.name = "sdw",
308 	.reg_bits = 32,
309 	.val_bits = 8,
310 	.readable_reg = rt711_readable_register,
311 	.max_register = 0xff01,
312 	.cache_type = REGCACHE_NONE,
313 	.use_single_read = true,
314 	.use_single_write = true,
315 };
316 
317 static int rt711_update_status(struct sdw_slave *slave,
318 				enum sdw_slave_status status)
319 {
320 	struct rt711_priv *rt711 = dev_get_drvdata(&slave->dev);
321 
322 	/* Update the status */
323 	rt711->status = status;
324 
325 	if (status == SDW_SLAVE_UNATTACHED)
326 		rt711->hw_init = false;
327 
328 	/*
329 	 * Perform initialization only if slave status is present and
330 	 * hw_init flag is false
331 	 */
332 	if (rt711->hw_init || rt711->status != SDW_SLAVE_ATTACHED)
333 		return 0;
334 
335 	/* perform I/O transfers required for Slave initialization */
336 	return rt711_io_init(&slave->dev, slave);
337 }
338 
339 static int rt711_read_prop(struct sdw_slave *slave)
340 {
341 	struct sdw_slave_prop *prop = &slave->prop;
342 	int nval;
343 	int i, j;
344 	u32 bit;
345 	unsigned long addr;
346 	struct sdw_dpn_prop *dpn;
347 
348 	prop->scp_int1_mask = SDW_SCP_INT1_IMPL_DEF | SDW_SCP_INT1_BUS_CLASH |
349 		SDW_SCP_INT1_PARITY;
350 	prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
351 
352 	prop->paging_support = false;
353 
354 	/* first we need to allocate memory for set bits in port lists */
355 	prop->source_ports = 0x14; /* BITMAP: 00010100 */
356 	prop->sink_ports = 0x8; /* BITMAP:  00001000 */
357 
358 	nval = hweight32(prop->source_ports);
359 	prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
360 						sizeof(*prop->src_dpn_prop),
361 						GFP_KERNEL);
362 	if (!prop->src_dpn_prop)
363 		return -ENOMEM;
364 
365 	i = 0;
366 	dpn = prop->src_dpn_prop;
367 	addr = prop->source_ports;
368 	for_each_set_bit(bit, &addr, 32) {
369 		dpn[i].num = bit;
370 		dpn[i].type = SDW_DPN_FULL;
371 		dpn[i].simple_ch_prep_sm = true;
372 		dpn[i].ch_prep_timeout = 10;
373 		i++;
374 	}
375 
376 	/* do this again for sink now */
377 	nval = hweight32(prop->sink_ports);
378 	prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
379 						sizeof(*prop->sink_dpn_prop),
380 						GFP_KERNEL);
381 	if (!prop->sink_dpn_prop)
382 		return -ENOMEM;
383 
384 	j = 0;
385 	dpn = prop->sink_dpn_prop;
386 	addr = prop->sink_ports;
387 	for_each_set_bit(bit, &addr, 32) {
388 		dpn[j].num = bit;
389 		dpn[j].type = SDW_DPN_FULL;
390 		dpn[j].simple_ch_prep_sm = true;
391 		dpn[j].ch_prep_timeout = 10;
392 		j++;
393 	}
394 
395 	/* set the timeout values */
396 	prop->clk_stop_timeout = 20;
397 
398 	/* wake-up event */
399 	prop->wake_capable = 1;
400 
401 	return 0;
402 }
403 
404 static int rt711_bus_config(struct sdw_slave *slave,
405 				struct sdw_bus_params *params)
406 {
407 	struct rt711_priv *rt711 = dev_get_drvdata(&slave->dev);
408 	int ret;
409 
410 	memcpy(&rt711->params, params, sizeof(*params));
411 
412 	ret = rt711_clock_config(&slave->dev);
413 	if (ret < 0)
414 		dev_err(&slave->dev, "Invalid clk config");
415 
416 	return ret;
417 }
418 
419 static int rt711_interrupt_callback(struct sdw_slave *slave,
420 					struct sdw_slave_intr_status *status)
421 {
422 	struct rt711_priv *rt711 = dev_get_drvdata(&slave->dev);
423 
424 	dev_dbg(&slave->dev,
425 		"%s control_port_stat=%x", __func__, status->control_port);
426 
427 	mutex_lock(&rt711->disable_irq_lock);
428 	if (status->control_port & 0x4 && !rt711->disable_irq) {
429 		mod_delayed_work(system_power_efficient_wq,
430 			&rt711->jack_detect_work, msecs_to_jiffies(250));
431 	}
432 	mutex_unlock(&rt711->disable_irq_lock);
433 
434 	return 0;
435 }
436 
437 static const struct sdw_slave_ops rt711_slave_ops = {
438 	.read_prop = rt711_read_prop,
439 	.interrupt_callback = rt711_interrupt_callback,
440 	.update_status = rt711_update_status,
441 	.bus_config = rt711_bus_config,
442 };
443 
444 static int rt711_sdw_probe(struct sdw_slave *slave,
445 				const struct sdw_device_id *id)
446 {
447 	struct regmap *sdw_regmap, *regmap;
448 
449 	/* Regmap Initialization */
450 	sdw_regmap = devm_regmap_init_sdw(slave, &rt711_sdw_regmap);
451 	if (IS_ERR(sdw_regmap))
452 		return PTR_ERR(sdw_regmap);
453 
454 	regmap = devm_regmap_init(&slave->dev, NULL,
455 		&slave->dev, &rt711_regmap);
456 	if (IS_ERR(regmap))
457 		return PTR_ERR(regmap);
458 
459 	rt711_init(&slave->dev, sdw_regmap, regmap, slave);
460 
461 	return 0;
462 }
463 
464 static int rt711_sdw_remove(struct sdw_slave *slave)
465 {
466 	struct rt711_priv *rt711 = dev_get_drvdata(&slave->dev);
467 
468 	if (rt711->hw_init) {
469 		cancel_delayed_work_sync(&rt711->jack_detect_work);
470 		cancel_delayed_work_sync(&rt711->jack_btn_check_work);
471 		cancel_work_sync(&rt711->calibration_work);
472 	}
473 
474 	if (rt711->first_hw_init)
475 		pm_runtime_disable(&slave->dev);
476 
477 	mutex_destroy(&rt711->calibrate_mutex);
478 	mutex_destroy(&rt711->disable_irq_lock);
479 
480 	return 0;
481 }
482 
483 static const struct sdw_device_id rt711_id[] = {
484 	SDW_SLAVE_ENTRY_EXT(0x025d, 0x711, 0x2, 0, 0),
485 	{},
486 };
487 MODULE_DEVICE_TABLE(sdw, rt711_id);
488 
489 static int __maybe_unused rt711_dev_suspend(struct device *dev)
490 {
491 	struct rt711_priv *rt711 = dev_get_drvdata(dev);
492 
493 	if (!rt711->hw_init)
494 		return 0;
495 
496 	cancel_delayed_work_sync(&rt711->jack_detect_work);
497 	cancel_delayed_work_sync(&rt711->jack_btn_check_work);
498 	cancel_work_sync(&rt711->calibration_work);
499 
500 	regcache_cache_only(rt711->regmap, true);
501 
502 	return 0;
503 }
504 
505 static int __maybe_unused rt711_dev_system_suspend(struct device *dev)
506 {
507 	struct rt711_priv *rt711 = dev_get_drvdata(dev);
508 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
509 	int ret;
510 
511 	if (!rt711->hw_init)
512 		return 0;
513 
514 	/*
515 	 * prevent new interrupts from being handled after the
516 	 * deferred work completes and before the parent disables
517 	 * interrupts on the link
518 	 */
519 	mutex_lock(&rt711->disable_irq_lock);
520 	rt711->disable_irq = true;
521 	ret = sdw_update_no_pm(slave, SDW_SCP_INTMASK1,
522 			       SDW_SCP_INT1_IMPL_DEF, 0);
523 	mutex_unlock(&rt711->disable_irq_lock);
524 
525 	if (ret < 0) {
526 		/* log but don't prevent suspend from happening */
527 		dev_dbg(&slave->dev, "%s: could not disable imp-def interrupts\n:", __func__);
528 	}
529 
530 	return rt711_dev_suspend(dev);
531 }
532 
533 #define RT711_PROBE_TIMEOUT 5000
534 
535 static int __maybe_unused rt711_dev_resume(struct device *dev)
536 {
537 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
538 	struct rt711_priv *rt711 = dev_get_drvdata(dev);
539 	unsigned long time;
540 
541 	if (!rt711->first_hw_init)
542 		return 0;
543 
544 	if (!slave->unattach_request)
545 		goto regmap_sync;
546 
547 	time = wait_for_completion_timeout(&slave->initialization_complete,
548 				msecs_to_jiffies(RT711_PROBE_TIMEOUT));
549 	if (!time) {
550 		dev_err(&slave->dev, "Initialization not complete, timed out\n");
551 		return -ETIMEDOUT;
552 	}
553 
554 regmap_sync:
555 	slave->unattach_request = 0;
556 	regcache_cache_only(rt711->regmap, false);
557 	regcache_sync_region(rt711->regmap, 0x3000, 0x8fff);
558 	regcache_sync_region(rt711->regmap, 0x752009, 0x752091);
559 
560 	return 0;
561 }
562 
563 static const struct dev_pm_ops rt711_pm = {
564 	SET_SYSTEM_SLEEP_PM_OPS(rt711_dev_system_suspend, rt711_dev_resume)
565 	SET_RUNTIME_PM_OPS(rt711_dev_suspend, rt711_dev_resume, NULL)
566 };
567 
568 static struct sdw_driver rt711_sdw_driver = {
569 	.driver = {
570 		.name = "rt711",
571 		.owner = THIS_MODULE,
572 		.pm = &rt711_pm,
573 	},
574 	.probe = rt711_sdw_probe,
575 	.remove = rt711_sdw_remove,
576 	.ops = &rt711_slave_ops,
577 	.id_table = rt711_id,
578 };
579 module_sdw_driver(rt711_sdw_driver);
580 
581 MODULE_DESCRIPTION("ASoC RT711 SDW driver");
582 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
583 MODULE_LICENSE("GPL");
584