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 
15 #include "rt711-sdca.h"
16 #include "rt711-sdca-sdw.h"
17 
18 static bool rt711_sdca_readable_register(struct device *dev, unsigned int reg)
19 {
20 	switch (reg) {
21 	case 0x201a ... 0x2027:
22 	case 0x2029 ... 0x202a:
23 	case 0x202d ... 0x2034:
24 	case 0x2200 ... 0x2204:
25 	case 0x2206 ... 0x2212:
26 	case 0x2220 ... 0x2223:
27 	case 0x2230 ... 0x2239:
28 	case 0x2f01 ... 0x2f0f:
29 	case 0x2f30 ... 0x2f36:
30 	case 0x2f50 ... 0x2f5a:
31 	case 0x2f60:
32 	case 0x3200 ... 0x3212:
33 	case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_SELECTED_MODE, 0):
34 	case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_DETECTED_MODE, 0):
35 	case SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_CURRENT_OWNER, 0) ...
36 		SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_MESSAGE_LENGTH, 0):
37 	case RT711_BUF_ADDR_HID1 ... RT711_BUF_ADDR_HID2:
38 		return true;
39 	default:
40 		return false;
41 	}
42 }
43 
44 static bool rt711_sdca_volatile_register(struct device *dev, unsigned int reg)
45 {
46 	switch (reg) {
47 	case 0x201b:
48 	case 0x201c:
49 	case 0x201d:
50 	case 0x201f:
51 	case 0x2021:
52 	case 0x2023:
53 	case 0x2230:
54 	case 0x202d ... 0x202f: /* BRA */
55 	case 0x2200 ... 0x2212: /* i2c debug */
56 	case RT711_RC_CAL_STATUS:
57 	case SDW_SDCA_CTL(FUNC_NUM_JACK_CODEC, RT711_SDCA_ENT_GE49, RT711_SDCA_CTL_DETECTED_MODE, 0):
58 	case SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_CURRENT_OWNER, 0) ...
59 		SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_MESSAGE_LENGTH, 0):
60 	case RT711_BUF_ADDR_HID1 ... RT711_BUF_ADDR_HID2:
61 		return true;
62 	default:
63 		return false;
64 	}
65 }
66 
67 static bool rt711_sdca_mbq_readable_register(struct device *dev, unsigned int reg)
68 {
69 	switch (reg) {
70 	case 0x2000000 ... 0x20000ff:
71 	case 0x5600000 ... 0x56000ff:
72 	case 0x5700000 ... 0x57000ff:
73 	case 0x5800000 ... 0x58000ff:
74 	case 0x5900000 ... 0x59000ff:
75 	case 0x5b00000 ... 0x5b000ff:
76 	case 0x5f00000 ... 0x5f000ff:
77 	case 0x6100000 ... 0x61000ff:
78 		return true;
79 	default:
80 		return false;
81 	}
82 }
83 
84 static bool rt711_sdca_mbq_volatile_register(struct device *dev, unsigned int reg)
85 {
86 	switch (reg) {
87 	case 0x2000000:
88 	case 0x200001a:
89 	case 0x2000046:
90 	case 0x2000080:
91 	case 0x2000081:
92 	case 0x2000083:
93 	case 0x5800000:
94 	case 0x5800001:
95 	case 0x5f00001:
96 	case 0x6100008:
97 		return true;
98 	default:
99 		return false;
100 	}
101 }
102 
103 static const struct regmap_config rt711_sdca_regmap = {
104 	.reg_bits = 32,
105 	.val_bits = 8,
106 	.readable_reg = rt711_sdca_readable_register,
107 	.volatile_reg = rt711_sdca_volatile_register,
108 	.max_register = 0x44ffffff,
109 	.reg_defaults = rt711_sdca_reg_defaults,
110 	.num_reg_defaults = ARRAY_SIZE(rt711_sdca_reg_defaults),
111 	.cache_type = REGCACHE_RBTREE,
112 	.use_single_read = true,
113 	.use_single_write = true,
114 };
115 
116 static const struct regmap_config rt711_sdca_mbq_regmap = {
117 	.name = "sdw-mbq",
118 	.reg_bits = 32,
119 	.val_bits = 16,
120 	.readable_reg = rt711_sdca_mbq_readable_register,
121 	.volatile_reg = rt711_sdca_mbq_volatile_register,
122 	.max_register = 0x40800f12,
123 	.reg_defaults = rt711_sdca_mbq_defaults,
124 	.num_reg_defaults = ARRAY_SIZE(rt711_sdca_mbq_defaults),
125 	.cache_type = REGCACHE_RBTREE,
126 	.use_single_read = true,
127 	.use_single_write = true,
128 };
129 
130 static int rt711_sdca_update_status(struct sdw_slave *slave,
131 				enum sdw_slave_status status)
132 {
133 	struct rt711_sdca_priv *rt711 = dev_get_drvdata(&slave->dev);
134 
135 	/* Update the status */
136 	rt711->status = status;
137 
138 	if (status == SDW_SLAVE_UNATTACHED)
139 		rt711->hw_init = false;
140 
141 	if (status == SDW_SLAVE_ATTACHED) {
142 		if (rt711->hs_jack) {
143 			/*
144 			 * Due to the SCP_SDCA_INTMASK will be cleared by any reset, and then
145 			 * if the device attached again, we will need to set the setting back.
146 			 * It could avoid losing the jack detection interrupt.
147 			 * This also could sync with the cache value as the rt711_sdca_jack_init set.
148 			 */
149 			sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK1,
150 				SDW_SCP_SDCA_INTMASK_SDCA_0);
151 			sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INTMASK2,
152 				SDW_SCP_SDCA_INTMASK_SDCA_8);
153 		}
154 	}
155 
156 	/*
157 	 * Perform initialization only if slave status is present and
158 	 * hw_init flag is false
159 	 */
160 	if (rt711->hw_init || rt711->status != SDW_SLAVE_ATTACHED)
161 		return 0;
162 
163 	/* perform I/O transfers required for Slave initialization */
164 	return rt711_sdca_io_init(&slave->dev, slave);
165 }
166 
167 static int rt711_sdca_read_prop(struct sdw_slave *slave)
168 {
169 	struct sdw_slave_prop *prop = &slave->prop;
170 	int nval;
171 	int i, j;
172 	u32 bit;
173 	unsigned long addr;
174 	struct sdw_dpn_prop *dpn;
175 
176 	prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
177 	prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
178 	prop->is_sdca = true;
179 
180 	prop->paging_support = true;
181 
182 	/* first we need to allocate memory for set bits in port lists */
183 	prop->source_ports = 0x14; /* BITMAP: 00010100 */
184 	prop->sink_ports = 0x8; /* BITMAP:  00001000 */
185 
186 	nval = hweight32(prop->source_ports);
187 	prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
188 		sizeof(*prop->src_dpn_prop), GFP_KERNEL);
189 	if (!prop->src_dpn_prop)
190 		return -ENOMEM;
191 
192 	i = 0;
193 	dpn = prop->src_dpn_prop;
194 	addr = prop->source_ports;
195 	for_each_set_bit(bit, &addr, 32) {
196 		dpn[i].num = bit;
197 		dpn[i].type = SDW_DPN_FULL;
198 		dpn[i].simple_ch_prep_sm = true;
199 		dpn[i].ch_prep_timeout = 10;
200 		i++;
201 	}
202 
203 	/* do this again for sink now */
204 	nval = hweight32(prop->sink_ports);
205 	prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
206 		sizeof(*prop->sink_dpn_prop), GFP_KERNEL);
207 	if (!prop->sink_dpn_prop)
208 		return -ENOMEM;
209 
210 	j = 0;
211 	dpn = prop->sink_dpn_prop;
212 	addr = prop->sink_ports;
213 	for_each_set_bit(bit, &addr, 32) {
214 		dpn[j].num = bit;
215 		dpn[j].type = SDW_DPN_FULL;
216 		dpn[j].simple_ch_prep_sm = true;
217 		dpn[j].ch_prep_timeout = 10;
218 		j++;
219 	}
220 
221 	/* set the timeout values */
222 	prop->clk_stop_timeout = 20;
223 
224 	/* wake-up event */
225 	prop->wake_capable = 1;
226 
227 	return 0;
228 }
229 
230 static int rt711_sdca_interrupt_callback(struct sdw_slave *slave,
231 					struct sdw_slave_intr_status *status)
232 {
233 	struct rt711_sdca_priv *rt711 = dev_get_drvdata(&slave->dev);
234 	int ret, stat;
235 	int count = 0, retry = 3;
236 	unsigned int sdca_cascade, scp_sdca_stat1, scp_sdca_stat2 = 0;
237 
238 	dev_dbg(&slave->dev,
239 		"%s control_port_stat=%x, sdca_cascade=%x", __func__,
240 		status->control_port, status->sdca_cascade);
241 
242 	if (cancel_delayed_work_sync(&rt711->jack_detect_work)) {
243 		dev_warn(&slave->dev, "%s the pending delayed_work was cancelled", __func__);
244 		/* avoid the HID owner doesn't change to device */
245 		if (rt711->scp_sdca_stat2)
246 			scp_sdca_stat2 = rt711->scp_sdca_stat2;
247 	}
248 
249 	ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT1);
250 	if (ret < 0)
251 		goto io_error;
252 	rt711->scp_sdca_stat1 = ret;
253 	ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT2);
254 	if (ret < 0)
255 		goto io_error;
256 	rt711->scp_sdca_stat2 = ret;
257 	if (scp_sdca_stat2)
258 		rt711->scp_sdca_stat2 |= scp_sdca_stat2;
259 
260 	do {
261 		/* clear flag */
262 		ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT1);
263 		if (ret < 0)
264 			goto io_error;
265 		if (ret & SDW_SCP_SDCA_INTMASK_SDCA_0) {
266 			ret = sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INT1,
267 						SDW_SCP_SDCA_INTMASK_SDCA_0);
268 			if (ret < 0)
269 				goto io_error;
270 		}
271 		ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT2);
272 		if (ret < 0)
273 			goto io_error;
274 		if (ret & SDW_SCP_SDCA_INTMASK_SDCA_8) {
275 			ret = sdw_write_no_pm(rt711->slave, SDW_SCP_SDCA_INT2,
276 						SDW_SCP_SDCA_INTMASK_SDCA_8);
277 			if (ret < 0)
278 				goto io_error;
279 		}
280 
281 		/* check if flag clear or not */
282 		ret = sdw_read_no_pm(rt711->slave, SDW_DP0_INT);
283 		if (ret < 0)
284 			goto io_error;
285 		sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
286 
287 		ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT1);
288 		if (ret < 0)
289 			goto io_error;
290 		scp_sdca_stat1 = ret & SDW_SCP_SDCA_INTMASK_SDCA_0;
291 
292 		ret = sdw_read_no_pm(rt711->slave, SDW_SCP_SDCA_INT2);
293 		if (ret < 0)
294 			goto io_error;
295 		scp_sdca_stat2 = ret & SDW_SCP_SDCA_INTMASK_SDCA_8;
296 
297 		stat = scp_sdca_stat1 || scp_sdca_stat2 || sdca_cascade;
298 
299 		count++;
300 	} while (stat != 0 && count < retry);
301 
302 	if (stat)
303 		dev_warn(&slave->dev,
304 			"%s scp_sdca_stat1=0x%x, scp_sdca_stat2=0x%x\n", __func__,
305 			rt711->scp_sdca_stat1, rt711->scp_sdca_stat2);
306 
307 	if (status->sdca_cascade)
308 		mod_delayed_work(system_power_efficient_wq,
309 			&rt711->jack_detect_work, msecs_to_jiffies(30));
310 
311 	return 0;
312 
313 io_error:
314 	pr_err_ratelimited("IO error in %s, ret %d\n", __func__, ret);
315 	return ret;
316 }
317 
318 static struct sdw_slave_ops rt711_sdca_slave_ops = {
319 	.read_prop = rt711_sdca_read_prop,
320 	.interrupt_callback = rt711_sdca_interrupt_callback,
321 	.update_status = rt711_sdca_update_status,
322 };
323 
324 static int rt711_sdca_sdw_probe(struct sdw_slave *slave,
325 				const struct sdw_device_id *id)
326 {
327 	struct regmap *regmap, *mbq_regmap;
328 
329 	/* Regmap Initialization */
330 	mbq_regmap = devm_regmap_init_sdw_mbq(slave, &rt711_sdca_mbq_regmap);
331 	if (IS_ERR(mbq_regmap))
332 		return PTR_ERR(mbq_regmap);
333 
334 	regmap = devm_regmap_init_sdw(slave, &rt711_sdca_regmap);
335 	if (IS_ERR(regmap))
336 		return PTR_ERR(regmap);
337 
338 	return rt711_sdca_init(&slave->dev, regmap, mbq_regmap, slave);
339 }
340 
341 static int rt711_sdca_sdw_remove(struct sdw_slave *slave)
342 {
343 	struct rt711_sdca_priv *rt711 = dev_get_drvdata(&slave->dev);
344 
345 	if (rt711 && rt711->hw_init) {
346 		cancel_delayed_work_sync(&rt711->jack_detect_work);
347 		cancel_delayed_work_sync(&rt711->jack_btn_check_work);
348 	}
349 
350 	return 0;
351 }
352 
353 static const struct sdw_device_id rt711_sdca_id[] = {
354 	SDW_SLAVE_ENTRY_EXT(0x025d, 0x711, 0x3, 0x1, 0),
355 	{},
356 };
357 MODULE_DEVICE_TABLE(sdw, rt711_sdca_id);
358 
359 static int __maybe_unused rt711_sdca_dev_suspend(struct device *dev)
360 {
361 	struct rt711_sdca_priv *rt711 = dev_get_drvdata(dev);
362 
363 	if (!rt711->hw_init)
364 		return 0;
365 
366 	cancel_delayed_work_sync(&rt711->jack_detect_work);
367 	cancel_delayed_work_sync(&rt711->jack_btn_check_work);
368 
369 	regcache_cache_only(rt711->regmap, true);
370 	regcache_cache_only(rt711->mbq_regmap, true);
371 
372 	return 0;
373 }
374 
375 #define RT711_PROBE_TIMEOUT 5000
376 
377 static int __maybe_unused rt711_sdca_dev_resume(struct device *dev)
378 {
379 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
380 	struct rt711_sdca_priv *rt711 = dev_get_drvdata(dev);
381 	unsigned long time;
382 
383 	if (!rt711->hw_init)
384 		return 0;
385 
386 	if (!slave->unattach_request)
387 		goto regmap_sync;
388 
389 	time = wait_for_completion_timeout(&slave->initialization_complete,
390 				msecs_to_jiffies(RT711_PROBE_TIMEOUT));
391 	if (!time) {
392 		dev_err(&slave->dev, "Initialization not complete, timed out\n");
393 		return -ETIMEDOUT;
394 	}
395 
396 regmap_sync:
397 	slave->unattach_request = 0;
398 	regcache_cache_only(rt711->regmap, false);
399 	regcache_sync(rt711->regmap);
400 	regcache_cache_only(rt711->mbq_regmap, false);
401 	regcache_sync(rt711->mbq_regmap);
402 	return 0;
403 }
404 
405 static const struct dev_pm_ops rt711_sdca_pm = {
406 	SET_SYSTEM_SLEEP_PM_OPS(rt711_sdca_dev_suspend, rt711_sdca_dev_resume)
407 	SET_RUNTIME_PM_OPS(rt711_sdca_dev_suspend, rt711_sdca_dev_resume, NULL)
408 };
409 
410 static struct sdw_driver rt711_sdca_sdw_driver = {
411 	.driver = {
412 		.name = "rt711-sdca",
413 		.owner = THIS_MODULE,
414 		.pm = &rt711_sdca_pm,
415 	},
416 	.probe = rt711_sdca_sdw_probe,
417 	.remove = rt711_sdca_sdw_remove,
418 	.ops = &rt711_sdca_slave_ops,
419 	.id_table = rt711_sdca_id,
420 };
421 module_sdw_driver(rt711_sdca_sdw_driver);
422 
423 MODULE_DESCRIPTION("ASoC RT711 SDCA SDW driver");
424 MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
425 MODULE_LICENSE("GPL");
426