xref: /linux/drivers/soundwire/cadence_master.c (revision cdd30ebb1b9f36159d66f088b61aee264e649d7a)
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
3 
4 /*
5  * Cadence SoundWire Master module
6  * Used by Master driver
7  */
8 
9 #include <linux/cleanup.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/debugfs.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/soundwire/sdw_registers.h>
19 #include <linux/soundwire/sdw.h>
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22 #include <linux/workqueue.h>
23 #include "bus.h"
24 #include "cadence_master.h"
25 
26 static int interrupt_mask;
27 module_param_named(cnds_mcp_int_mask, interrupt_mask, int, 0444);
28 MODULE_PARM_DESC(cdns_mcp_int_mask, "Cadence MCP IntMask");
29 
30 #define CDNS_MCP_CONFIG				0x0
31 #define CDNS_MCP_CONFIG_BUS_REL			BIT(6)
32 
33 #define CDNS_IP_MCP_CONFIG			0x0 /* IP offset added at run-time */
34 
35 #define CDNS_IP_MCP_CONFIG_MCMD_RETRY		GENMASK(27, 24)
36 #define CDNS_IP_MCP_CONFIG_MPREQ_DELAY		GENMASK(20, 16)
37 #define CDNS_IP_MCP_CONFIG_MMASTER		BIT(7)
38 #define CDNS_IP_MCP_CONFIG_SNIFFER		BIT(5)
39 #define CDNS_IP_MCP_CONFIG_CMD			BIT(3)
40 #define CDNS_IP_MCP_CONFIG_OP			GENMASK(2, 0)
41 #define CDNS_IP_MCP_CONFIG_OP_NORMAL		0
42 
43 #define CDNS_MCP_CONTROL			0x4
44 
45 #define CDNS_MCP_CONTROL_CMD_RST		BIT(7)
46 #define CDNS_MCP_CONTROL_SOFT_RST		BIT(6)
47 #define CDNS_MCP_CONTROL_HW_RST			BIT(4)
48 #define CDNS_MCP_CONTROL_CLK_STOP_CLR		BIT(2)
49 
50 #define CDNS_IP_MCP_CONTROL			0x4  /* IP offset added at run-time */
51 
52 #define CDNS_IP_MCP_CONTROL_RST_DELAY		GENMASK(10, 8)
53 #define CDNS_IP_MCP_CONTROL_SW_RST		BIT(5)
54 #define CDNS_IP_MCP_CONTROL_CLK_PAUSE		BIT(3)
55 #define CDNS_IP_MCP_CONTROL_CMD_ACCEPT		BIT(1)
56 #define CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP	BIT(0)
57 
58 #define CDNS_IP_MCP_CMDCTRL			0x8 /* IP offset added at run-time */
59 
60 #define CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR	BIT(2)
61 
62 #define CDNS_MCP_SSPSTAT			0xC
63 #define CDNS_MCP_FRAME_SHAPE			0x10
64 #define CDNS_MCP_FRAME_SHAPE_INIT		0x14
65 #define CDNS_MCP_FRAME_SHAPE_COL_MASK		GENMASK(2, 0)
66 #define CDNS_MCP_FRAME_SHAPE_ROW_MASK		GENMASK(7, 3)
67 
68 #define CDNS_MCP_CONFIG_UPDATE			0x18
69 #define CDNS_MCP_CONFIG_UPDATE_BIT		BIT(0)
70 
71 #define CDNS_MCP_PHYCTRL			0x1C
72 #define CDNS_MCP_SSP_CTRL0			0x20
73 #define CDNS_MCP_SSP_CTRL1			0x28
74 #define CDNS_MCP_CLK_CTRL0			0x30
75 #define CDNS_MCP_CLK_CTRL1			0x38
76 #define CDNS_MCP_CLK_MCLKD_MASK		GENMASK(7, 0)
77 
78 #define CDNS_MCP_STAT				0x40
79 
80 #define CDNS_MCP_STAT_ACTIVE_BANK		BIT(20)
81 #define CDNS_MCP_STAT_CLK_STOP			BIT(16)
82 
83 #define CDNS_MCP_INTSTAT			0x44
84 #define CDNS_MCP_INTMASK			0x48
85 
86 #define CDNS_MCP_INT_IRQ			BIT(31)
87 #define CDNS_MCP_INT_RESERVED1			GENMASK(30, 17)
88 #define CDNS_MCP_INT_WAKEUP			BIT(16)
89 #define CDNS_MCP_INT_SLAVE_RSVD			BIT(15)
90 #define CDNS_MCP_INT_SLAVE_ALERT		BIT(14)
91 #define CDNS_MCP_INT_SLAVE_ATTACH		BIT(13)
92 #define CDNS_MCP_INT_SLAVE_NATTACH		BIT(12)
93 #define CDNS_MCP_INT_SLAVE_MASK			GENMASK(15, 12)
94 #define CDNS_MCP_INT_DPINT			BIT(11)
95 #define CDNS_MCP_INT_CTRL_CLASH			BIT(10)
96 #define CDNS_MCP_INT_DATA_CLASH			BIT(9)
97 #define CDNS_MCP_INT_PARITY			BIT(8)
98 #define CDNS_MCP_INT_CMD_ERR			BIT(7)
99 #define CDNS_MCP_INT_RESERVED2			GENMASK(6, 4)
100 #define CDNS_MCP_INT_RX_NE			BIT(3)
101 #define CDNS_MCP_INT_RX_WL			BIT(2)
102 #define CDNS_MCP_INT_TXE			BIT(1)
103 #define CDNS_MCP_INT_TXF			BIT(0)
104 #define CDNS_MCP_INT_RESERVED (CDNS_MCP_INT_RESERVED1 | CDNS_MCP_INT_RESERVED2)
105 
106 #define CDNS_MCP_INTSET				0x4C
107 
108 #define CDNS_MCP_SLAVE_STAT			0x50
109 #define CDNS_MCP_SLAVE_STAT_MASK		GENMASK(1, 0)
110 
111 #define CDNS_MCP_SLAVE_INTSTAT0			0x54
112 #define CDNS_MCP_SLAVE_INTSTAT1			0x58
113 #define CDNS_MCP_SLAVE_INTSTAT_NPRESENT		BIT(0)
114 #define CDNS_MCP_SLAVE_INTSTAT_ATTACHED		BIT(1)
115 #define CDNS_MCP_SLAVE_INTSTAT_ALERT		BIT(2)
116 #define CDNS_MCP_SLAVE_INTSTAT_RESERVED		BIT(3)
117 #define CDNS_MCP_SLAVE_STATUS_BITS		GENMASK(3, 0)
118 #define CDNS_MCP_SLAVE_STATUS_NUM		4
119 
120 #define CDNS_MCP_SLAVE_INTMASK0			0x5C
121 #define CDNS_MCP_SLAVE_INTMASK1			0x60
122 
123 #define CDNS_MCP_SLAVE_INTMASK0_MASK		GENMASK(31, 0)
124 #define CDNS_MCP_SLAVE_INTMASK1_MASK		GENMASK(15, 0)
125 
126 #define CDNS_MCP_PORT_INTSTAT			0x64
127 #define CDNS_MCP_PDI_STAT			0x6C
128 
129 #define CDNS_MCP_FIFOLEVEL			0x78
130 #define CDNS_MCP_FIFOSTAT			0x7C
131 #define CDNS_MCP_RX_FIFO_AVAIL			GENMASK(5, 0)
132 
133 #define CDNS_IP_MCP_CMD_BASE			0x80 /* IP offset added at run-time */
134 #define CDNS_IP_MCP_RESP_BASE			0x80 /* IP offset added at run-time */
135 /* FIFO can hold 8 commands */
136 #define CDNS_MCP_CMD_LEN			8
137 #define CDNS_MCP_CMD_WORD_LEN			0x4
138 
139 #define CDNS_MCP_CMD_SSP_TAG			BIT(31)
140 #define CDNS_MCP_CMD_COMMAND			GENMASK(30, 28)
141 #define CDNS_MCP_CMD_DEV_ADDR			GENMASK(27, 24)
142 #define CDNS_MCP_CMD_REG_ADDR			GENMASK(23, 8)
143 #define CDNS_MCP_CMD_REG_DATA			GENMASK(7, 0)
144 
145 #define CDNS_MCP_CMD_READ			2
146 #define CDNS_MCP_CMD_WRITE			3
147 
148 #define CDNS_MCP_RESP_RDATA			GENMASK(15, 8)
149 #define CDNS_MCP_RESP_ACK			BIT(0)
150 #define CDNS_MCP_RESP_NACK			BIT(1)
151 
152 #define CDNS_DP_SIZE				128
153 
154 #define CDNS_DPN_B0_CONFIG(n)			(0x100 + CDNS_DP_SIZE * (n))
155 #define CDNS_DPN_B0_CH_EN(n)			(0x104 + CDNS_DP_SIZE * (n))
156 #define CDNS_DPN_B0_SAMPLE_CTRL(n)		(0x108 + CDNS_DP_SIZE * (n))
157 #define CDNS_DPN_B0_OFFSET_CTRL(n)		(0x10C + CDNS_DP_SIZE * (n))
158 #define CDNS_DPN_B0_HCTRL(n)			(0x110 + CDNS_DP_SIZE * (n))
159 #define CDNS_DPN_B0_ASYNC_CTRL(n)		(0x114 + CDNS_DP_SIZE * (n))
160 
161 #define CDNS_DPN_B1_CONFIG(n)			(0x118 + CDNS_DP_SIZE * (n))
162 #define CDNS_DPN_B1_CH_EN(n)			(0x11C + CDNS_DP_SIZE * (n))
163 #define CDNS_DPN_B1_SAMPLE_CTRL(n)		(0x120 + CDNS_DP_SIZE * (n))
164 #define CDNS_DPN_B1_OFFSET_CTRL(n)		(0x124 + CDNS_DP_SIZE * (n))
165 #define CDNS_DPN_B1_HCTRL(n)			(0x128 + CDNS_DP_SIZE * (n))
166 #define CDNS_DPN_B1_ASYNC_CTRL(n)		(0x12C + CDNS_DP_SIZE * (n))
167 
168 #define CDNS_DPN_CONFIG_BPM			BIT(18)
169 #define CDNS_DPN_CONFIG_BGC			GENMASK(17, 16)
170 #define CDNS_DPN_CONFIG_WL			GENMASK(12, 8)
171 #define CDNS_DPN_CONFIG_PORT_DAT		GENMASK(3, 2)
172 #define CDNS_DPN_CONFIG_PORT_FLOW		GENMASK(1, 0)
173 
174 #define CDNS_DPN_SAMPLE_CTRL_SI			GENMASK(15, 0)
175 
176 #define CDNS_DPN_OFFSET_CTRL_1			GENMASK(7, 0)
177 #define CDNS_DPN_OFFSET_CTRL_2			GENMASK(15, 8)
178 
179 #define CDNS_DPN_HCTRL_HSTOP			GENMASK(3, 0)
180 #define CDNS_DPN_HCTRL_HSTART			GENMASK(7, 4)
181 #define CDNS_DPN_HCTRL_LCTRL			GENMASK(10, 8)
182 
183 #define CDNS_PORTCTRL				0x130
184 #define CDNS_PORTCTRL_TEST_FAILED		BIT(1)
185 #define CDNS_PORTCTRL_DIRN			BIT(7)
186 #define CDNS_PORTCTRL_BANK_INVERT		BIT(8)
187 
188 #define CDNS_PORT_OFFSET			0x80
189 
190 #define CDNS_PDI_CONFIG(n)			(0x1100 + (n) * 16)
191 
192 #define CDNS_PDI_CONFIG_SOFT_RESET		BIT(24)
193 #define CDNS_PDI_CONFIG_CHANNEL			GENMASK(15, 8)
194 #define CDNS_PDI_CONFIG_PORT			GENMASK(4, 0)
195 
196 /* Driver defaults */
197 #define CDNS_TX_TIMEOUT				500
198 
199 #define CDNS_SCP_RX_FIFOLEVEL			0x2
200 
201 /*
202  * register accessor helpers
203  */
204 static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset)
205 {
206 	return readl(cdns->registers + offset);
207 }
208 
209 static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value)
210 {
211 	writel(value, cdns->registers + offset);
212 }
213 
214 static inline u32 cdns_ip_readl(struct sdw_cdns *cdns, int offset)
215 {
216 	return cdns_readl(cdns, cdns->ip_offset + offset);
217 }
218 
219 static inline void cdns_ip_writel(struct sdw_cdns *cdns, int offset, u32 value)
220 {
221 	return cdns_writel(cdns, cdns->ip_offset + offset, value);
222 }
223 
224 static inline void cdns_updatel(struct sdw_cdns *cdns,
225 				int offset, u32 mask, u32 val)
226 {
227 	u32 tmp;
228 
229 	tmp = cdns_readl(cdns, offset);
230 	tmp = (tmp & ~mask) | val;
231 	cdns_writel(cdns, offset, tmp);
232 }
233 
234 static inline void cdns_ip_updatel(struct sdw_cdns *cdns,
235 				   int offset, u32 mask, u32 val)
236 {
237 	cdns_updatel(cdns, cdns->ip_offset + offset, mask, val);
238 }
239 
240 static int cdns_set_wait(struct sdw_cdns *cdns, int offset, u32 mask, u32 value)
241 {
242 	int timeout = 10;
243 	u32 reg_read;
244 
245 	/* Wait for bit to be set */
246 	do {
247 		reg_read = readl(cdns->registers + offset);
248 		if ((reg_read & mask) == value)
249 			return 0;
250 
251 		timeout--;
252 		usleep_range(50, 100);
253 	} while (timeout != 0);
254 
255 	return -ETIMEDOUT;
256 }
257 
258 static int cdns_clear_bit(struct sdw_cdns *cdns, int offset, u32 value)
259 {
260 	writel(value, cdns->registers + offset);
261 
262 	/* Wait for bit to be self cleared */
263 	return cdns_set_wait(cdns, offset, value, 0);
264 }
265 
266 /*
267  * all changes to the MCP_CONFIG, MCP_CONTROL, MCP_CMDCTRL and MCP_PHYCTRL
268  * need to be confirmed with a write to MCP_CONFIG_UPDATE
269  */
270 static int cdns_config_update(struct sdw_cdns *cdns)
271 {
272 	int ret;
273 
274 	if (sdw_cdns_is_clock_stop(cdns)) {
275 		dev_err(cdns->dev, "Cannot program MCP_CONFIG_UPDATE in ClockStopMode\n");
276 		return -EINVAL;
277 	}
278 
279 	ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE,
280 			     CDNS_MCP_CONFIG_UPDATE_BIT);
281 	if (ret < 0)
282 		dev_err(cdns->dev, "Config update timedout\n");
283 
284 	return ret;
285 }
286 
287 /**
288  * sdw_cdns_config_update() - Update configurations
289  * @cdns: Cadence instance
290  */
291 void sdw_cdns_config_update(struct sdw_cdns *cdns)
292 {
293 	/* commit changes */
294 	cdns_writel(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT);
295 }
296 EXPORT_SYMBOL(sdw_cdns_config_update);
297 
298 /**
299  * sdw_cdns_config_update_set_wait() - wait until configuration update bit is self-cleared
300  * @cdns: Cadence instance
301  */
302 int sdw_cdns_config_update_set_wait(struct sdw_cdns *cdns)
303 {
304 	/* the hardware recommendation is to wait at least 300us */
305 	return cdns_set_wait(cdns, CDNS_MCP_CONFIG_UPDATE,
306 			     CDNS_MCP_CONFIG_UPDATE_BIT, 0);
307 }
308 EXPORT_SYMBOL(sdw_cdns_config_update_set_wait);
309 
310 /*
311  * debugfs
312  */
313 #ifdef CONFIG_DEBUG_FS
314 
315 #define RD_BUF (2 * PAGE_SIZE)
316 
317 static ssize_t cdns_sprintf(struct sdw_cdns *cdns,
318 			    char *buf, size_t pos, unsigned int reg)
319 {
320 	return scnprintf(buf + pos, RD_BUF - pos,
321 			 "%4x\t%8x\n", reg, cdns_readl(cdns, reg));
322 }
323 
324 static int cdns_reg_show(struct seq_file *s, void *data)
325 {
326 	struct sdw_cdns *cdns = s->private;
327 	ssize_t ret;
328 	int num_ports;
329 	int i, j;
330 
331 	char *buf __free(kfree) = kzalloc(RD_BUF, GFP_KERNEL);
332 	if (!buf)
333 		return -ENOMEM;
334 
335 	ret = scnprintf(buf, RD_BUF, "Register  Value\n");
336 	ret += scnprintf(buf + ret, RD_BUF - ret, "\nMCP Registers\n");
337 	/* 8 MCP registers */
338 	for (i = CDNS_MCP_CONFIG; i <= CDNS_MCP_PHYCTRL; i += sizeof(u32))
339 		ret += cdns_sprintf(cdns, buf, ret, i);
340 
341 	ret += scnprintf(buf + ret, RD_BUF - ret,
342 			 "\nStatus & Intr Registers\n");
343 	/* 13 Status & Intr registers (offsets 0x70 and 0x74 not defined) */
344 	for (i = CDNS_MCP_STAT; i <=  CDNS_MCP_FIFOSTAT; i += sizeof(u32))
345 		ret += cdns_sprintf(cdns, buf, ret, i);
346 
347 	ret += scnprintf(buf + ret, RD_BUF - ret,
348 			 "\nSSP & Clk ctrl Registers\n");
349 	ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL0);
350 	ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL1);
351 	ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL0);
352 	ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL1);
353 
354 	ret += scnprintf(buf + ret, RD_BUF - ret,
355 			 "\nDPn B0 Registers\n");
356 
357 	num_ports = cdns->num_ports;
358 
359 	for (i = 0; i < num_ports; i++) {
360 		ret += scnprintf(buf + ret, RD_BUF - ret,
361 				 "\nDP-%d\n", i);
362 		for (j = CDNS_DPN_B0_CONFIG(i);
363 		     j < CDNS_DPN_B0_ASYNC_CTRL(i); j += sizeof(u32))
364 			ret += cdns_sprintf(cdns, buf, ret, j);
365 	}
366 
367 	ret += scnprintf(buf + ret, RD_BUF - ret,
368 			 "\nDPn B1 Registers\n");
369 	for (i = 0; i < num_ports; i++) {
370 		ret += scnprintf(buf + ret, RD_BUF - ret,
371 				 "\nDP-%d\n", i);
372 
373 		for (j = CDNS_DPN_B1_CONFIG(i);
374 		     j < CDNS_DPN_B1_ASYNC_CTRL(i); j += sizeof(u32))
375 			ret += cdns_sprintf(cdns, buf, ret, j);
376 	}
377 
378 	ret += scnprintf(buf + ret, RD_BUF - ret,
379 			 "\nDPn Control Registers\n");
380 	for (i = 0; i < num_ports; i++)
381 		ret += cdns_sprintf(cdns, buf, ret,
382 				CDNS_PORTCTRL + i * CDNS_PORT_OFFSET);
383 
384 	ret += scnprintf(buf + ret, RD_BUF - ret,
385 			 "\nPDIn Config Registers\n");
386 
387 	/* number of PDI and ports is interchangeable */
388 	for (i = 0; i < num_ports; i++)
389 		ret += cdns_sprintf(cdns, buf, ret, CDNS_PDI_CONFIG(i));
390 
391 	seq_printf(s, "%s", buf);
392 
393 	return 0;
394 }
395 DEFINE_SHOW_ATTRIBUTE(cdns_reg);
396 
397 static int cdns_hw_reset(void *data, u64 value)
398 {
399 	struct sdw_cdns *cdns = data;
400 	int ret;
401 
402 	if (value != 1)
403 		return -EINVAL;
404 
405 	/* Userspace changed the hardware state behind the kernel's back */
406 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
407 
408 	ret = sdw_cdns_exit_reset(cdns);
409 
410 	dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret);
411 
412 	return ret;
413 }
414 
415 DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n");
416 
417 static int cdns_parity_error_injection(void *data, u64 value)
418 {
419 	struct sdw_cdns *cdns = data;
420 	struct sdw_bus *bus;
421 	int ret;
422 
423 	if (value != 1)
424 		return -EINVAL;
425 
426 	bus = &cdns->bus;
427 
428 	/*
429 	 * Resume Master device. If this results in a bus reset, the
430 	 * Slave devices will re-attach and be re-enumerated.
431 	 */
432 	ret = pm_runtime_resume_and_get(bus->dev);
433 	if (ret < 0 && ret != -EACCES) {
434 		dev_err_ratelimited(cdns->dev,
435 				    "pm_runtime_resume_and_get failed in %s, ret %d\n",
436 				    __func__, ret);
437 		return ret;
438 	}
439 
440 	/*
441 	 * wait long enough for Slave(s) to be in steady state. This
442 	 * does not need to be super precise.
443 	 */
444 	msleep(200);
445 
446 	/*
447 	 * Take the bus lock here to make sure that any bus transactions
448 	 * will be queued while we inject a parity error on a dummy read
449 	 */
450 	mutex_lock(&bus->bus_lock);
451 
452 	/* program hardware to inject parity error */
453 	cdns_ip_updatel(cdns, CDNS_IP_MCP_CMDCTRL,
454 			CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR,
455 			CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR);
456 
457 	/* commit changes */
458 	ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT);
459 	if (ret < 0)
460 		goto unlock;
461 
462 	/* do a broadcast dummy read to avoid bus clashes */
463 	ret = sdw_bread_no_pm_unlocked(&cdns->bus, 0xf, SDW_SCP_DEVID_0);
464 	dev_info(cdns->dev, "parity error injection, read: %d\n", ret);
465 
466 	/* program hardware to disable parity error */
467 	cdns_ip_updatel(cdns, CDNS_IP_MCP_CMDCTRL,
468 			CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR,
469 			0);
470 
471 	/* commit changes */
472 	ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT);
473 	if (ret < 0)
474 		goto unlock;
475 
476 	/* Userspace changed the hardware state behind the kernel's back */
477 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
478 
479 unlock:
480 	/* Continue bus operation with parity error injection disabled */
481 	mutex_unlock(&bus->bus_lock);
482 
483 	/*
484 	 * allow Master device to enter pm_runtime suspend. This may
485 	 * also result in Slave devices suspending.
486 	 */
487 	pm_runtime_mark_last_busy(bus->dev);
488 	pm_runtime_put_autosuspend(bus->dev);
489 
490 	return 0;
491 }
492 
493 DEFINE_DEBUGFS_ATTRIBUTE(cdns_parity_error_fops, NULL,
494 			 cdns_parity_error_injection, "%llu\n");
495 
496 static int cdns_set_pdi_loopback_source(void *data, u64 value)
497 {
498 	struct sdw_cdns *cdns = data;
499 	unsigned int pdi_out_num = cdns->pcm.num_bd + cdns->pcm.num_out;
500 
501 	if (value > pdi_out_num)
502 		return -EINVAL;
503 
504 	/* Userspace changed the hardware state behind the kernel's back */
505 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
506 
507 	cdns->pdi_loopback_source = value;
508 
509 	return 0;
510 }
511 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_source_fops, NULL, cdns_set_pdi_loopback_source, "%llu\n");
512 
513 static int cdns_set_pdi_loopback_target(void *data, u64 value)
514 {
515 	struct sdw_cdns *cdns = data;
516 	unsigned int pdi_in_num = cdns->pcm.num_bd + cdns->pcm.num_in;
517 
518 	if (value > pdi_in_num)
519 		return -EINVAL;
520 
521 	/* Userspace changed the hardware state behind the kernel's back */
522 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
523 
524 	cdns->pdi_loopback_target = value;
525 
526 	return 0;
527 }
528 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_target_fops, NULL, cdns_set_pdi_loopback_target, "%llu\n");
529 
530 /**
531  * sdw_cdns_debugfs_init() - Cadence debugfs init
532  * @cdns: Cadence instance
533  * @root: debugfs root
534  */
535 void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
536 {
537 	debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
538 
539 	debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
540 			    &cdns_hw_reset_fops);
541 
542 	debugfs_create_file("cdns-parity-error-injection", 0200, root, cdns,
543 			    &cdns_parity_error_fops);
544 
545 	cdns->pdi_loopback_source = -1;
546 	cdns->pdi_loopback_target = -1;
547 
548 	debugfs_create_file("cdns-pdi-loopback-source", 0200, root, cdns,
549 			    &cdns_pdi_loopback_source_fops);
550 
551 	debugfs_create_file("cdns-pdi-loopback-target", 0200, root, cdns,
552 			    &cdns_pdi_loopback_target_fops);
553 
554 }
555 EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
556 
557 #endif /* CONFIG_DEBUG_FS */
558 
559 /*
560  * IO Calls
561  */
562 static enum sdw_command_response
563 cdns_fill_msg_resp(struct sdw_cdns *cdns,
564 		   struct sdw_msg *msg, int count, int offset)
565 {
566 	int nack = 0, no_ack = 0;
567 	int i;
568 
569 	/* check message response */
570 	for (i = 0; i < count; i++) {
571 		if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
572 			no_ack = 1;
573 			dev_vdbg(cdns->dev, "Msg Ack not received, cmd %d\n", i);
574 		}
575 		if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
576 			nack = 1;
577 			dev_err_ratelimited(cdns->dev, "Msg NACK received, cmd %d\n", i);
578 		}
579 	}
580 
581 	if (nack) {
582 		dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
583 		return SDW_CMD_FAIL;
584 	}
585 
586 	if (no_ack) {
587 		dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
588 		return SDW_CMD_IGNORED;
589 	}
590 
591 	if (msg->flags == SDW_MSG_FLAG_READ) {
592 		/* fill response */
593 		for (i = 0; i < count; i++)
594 			msg->buf[i + offset] = FIELD_GET(CDNS_MCP_RESP_RDATA,
595 							 cdns->response_buf[i]);
596 	}
597 
598 	return SDW_CMD_OK;
599 }
600 
601 static void cdns_read_response(struct sdw_cdns *cdns)
602 {
603 	u32 num_resp, cmd_base;
604 	int i;
605 
606 	/* RX_FIFO_AVAIL can be 2 entries more than the FIFO size */
607 	BUILD_BUG_ON(ARRAY_SIZE(cdns->response_buf) < CDNS_MCP_CMD_LEN + 2);
608 
609 	num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
610 	num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
611 	if (num_resp > ARRAY_SIZE(cdns->response_buf)) {
612 		dev_warn(cdns->dev, "RX AVAIL %d too long\n", num_resp);
613 		num_resp = ARRAY_SIZE(cdns->response_buf);
614 	}
615 
616 	cmd_base = CDNS_IP_MCP_CMD_BASE;
617 
618 	for (i = 0; i < num_resp; i++) {
619 		cdns->response_buf[i] = cdns_ip_readl(cdns, cmd_base);
620 		cmd_base += CDNS_MCP_CMD_WORD_LEN;
621 	}
622 }
623 
624 static enum sdw_command_response
625 _cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
626 	       int offset, int count, bool defer)
627 {
628 	unsigned long time;
629 	u32 base, i, data;
630 	u16 addr;
631 
632 	/* Program the watermark level for RX FIFO */
633 	if (cdns->msg_count != count) {
634 		cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count);
635 		cdns->msg_count = count;
636 	}
637 
638 	base = CDNS_IP_MCP_CMD_BASE;
639 	addr = msg->addr + offset;
640 
641 	for (i = 0; i < count; i++) {
642 		data = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
643 		data |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, cmd);
644 		data |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, addr);
645 		addr++;
646 
647 		if (msg->flags == SDW_MSG_FLAG_WRITE)
648 			data |= msg->buf[i + offset];
649 
650 		data |= FIELD_PREP(CDNS_MCP_CMD_SSP_TAG, msg->ssp_sync);
651 		cdns_ip_writel(cdns, base, data);
652 		base += CDNS_MCP_CMD_WORD_LEN;
653 	}
654 
655 	if (defer)
656 		return SDW_CMD_OK;
657 
658 	/* wait for timeout or response */
659 	time = wait_for_completion_timeout(&cdns->tx_complete,
660 					   msecs_to_jiffies(CDNS_TX_TIMEOUT));
661 	if (!time) {
662 		dev_err(cdns->dev, "IO transfer timed out, cmd %d device %d addr %x len %d\n",
663 			cmd, msg->dev_num, msg->addr, msg->len);
664 		msg->len = 0;
665 
666 		/* Drain anything in the RX_FIFO */
667 		cdns_read_response(cdns);
668 
669 		return SDW_CMD_TIMEOUT;
670 	}
671 
672 	return cdns_fill_msg_resp(cdns, msg, count, offset);
673 }
674 
675 static enum sdw_command_response
676 cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
677 {
678 	int nack = 0, no_ack = 0;
679 	unsigned long time;
680 	u32 data[2], base;
681 	int i;
682 
683 	/* Program the watermark level for RX FIFO */
684 	if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
685 		cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
686 		cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
687 	}
688 
689 	data[0] = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
690 	data[0] |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, 0x3);
691 	data[1] = data[0];
692 
693 	data[0] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE1);
694 	data[1] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE2);
695 
696 	data[0] |= msg->addr_page1;
697 	data[1] |= msg->addr_page2;
698 
699 	base = CDNS_IP_MCP_CMD_BASE;
700 	cdns_ip_writel(cdns, base, data[0]);
701 	base += CDNS_MCP_CMD_WORD_LEN;
702 	cdns_ip_writel(cdns, base, data[1]);
703 
704 	time = wait_for_completion_timeout(&cdns->tx_complete,
705 					   msecs_to_jiffies(CDNS_TX_TIMEOUT));
706 	if (!time) {
707 		dev_err(cdns->dev, "SCP Msg trf timed out\n");
708 		msg->len = 0;
709 		return SDW_CMD_TIMEOUT;
710 	}
711 
712 	/* check response the writes */
713 	for (i = 0; i < 2; i++) {
714 		if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
715 			no_ack = 1;
716 			dev_err(cdns->dev, "Program SCP Ack not received\n");
717 			if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
718 				nack = 1;
719 				dev_err(cdns->dev, "Program SCP NACK received\n");
720 			}
721 		}
722 	}
723 
724 	/* For NACK, NO ack, don't return err if we are in Broadcast mode */
725 	if (nack) {
726 		dev_err_ratelimited(cdns->dev,
727 				    "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
728 		return SDW_CMD_FAIL;
729 	}
730 
731 	if (no_ack) {
732 		dev_dbg_ratelimited(cdns->dev,
733 				    "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
734 		return SDW_CMD_IGNORED;
735 	}
736 
737 	return SDW_CMD_OK;
738 }
739 
740 static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
741 {
742 	int ret;
743 
744 	if (msg->page) {
745 		ret = cdns_program_scp_addr(cdns, msg);
746 		if (ret) {
747 			msg->len = 0;
748 			return ret;
749 		}
750 	}
751 
752 	switch (msg->flags) {
753 	case SDW_MSG_FLAG_READ:
754 		*cmd = CDNS_MCP_CMD_READ;
755 		break;
756 
757 	case SDW_MSG_FLAG_WRITE:
758 		*cmd = CDNS_MCP_CMD_WRITE;
759 		break;
760 
761 	default:
762 		dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
763 		return -EINVAL;
764 	}
765 
766 	return 0;
767 }
768 
769 enum sdw_command_response
770 cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
771 {
772 	struct sdw_cdns *cdns = bus_to_cdns(bus);
773 	int cmd = 0, ret, i;
774 
775 	ret = cdns_prep_msg(cdns, msg, &cmd);
776 	if (ret)
777 		return SDW_CMD_FAIL_OTHER;
778 
779 	for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
780 		ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
781 				     CDNS_MCP_CMD_LEN, false);
782 		if (ret != SDW_CMD_OK)
783 			return ret;
784 	}
785 
786 	if (!(msg->len % CDNS_MCP_CMD_LEN))
787 		return SDW_CMD_OK;
788 
789 	return _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
790 			      msg->len % CDNS_MCP_CMD_LEN, false);
791 }
792 EXPORT_SYMBOL(cdns_xfer_msg);
793 
794 enum sdw_command_response
795 cdns_xfer_msg_defer(struct sdw_bus *bus)
796 {
797 	struct sdw_cdns *cdns = bus_to_cdns(bus);
798 	struct sdw_defer *defer = &bus->defer_msg;
799 	struct sdw_msg *msg = defer->msg;
800 	int cmd = 0, ret;
801 
802 	/* for defer only 1 message is supported */
803 	if (msg->len > 1)
804 		return -ENOTSUPP;
805 
806 	ret = cdns_prep_msg(cdns, msg, &cmd);
807 	if (ret)
808 		return SDW_CMD_FAIL_OTHER;
809 
810 	return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
811 }
812 EXPORT_SYMBOL(cdns_xfer_msg_defer);
813 
814 u32 cdns_read_ping_status(struct sdw_bus *bus)
815 {
816 	struct sdw_cdns *cdns = bus_to_cdns(bus);
817 
818 	return cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
819 }
820 EXPORT_SYMBOL(cdns_read_ping_status);
821 
822 /*
823  * IRQ handling
824  */
825 
826 static int cdns_update_slave_status(struct sdw_cdns *cdns,
827 				    u64 slave_intstat)
828 {
829 	enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
830 	bool is_slave = false;
831 	u32 mask;
832 	u32 val;
833 	int i, set_status;
834 
835 	memset(status, 0, sizeof(status));
836 
837 	for (i = 0; i <= SDW_MAX_DEVICES; i++) {
838 		mask = (slave_intstat >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
839 			CDNS_MCP_SLAVE_STATUS_BITS;
840 
841 		set_status = 0;
842 
843 		if (mask) {
844 			is_slave = true;
845 
846 			if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
847 				status[i] = SDW_SLAVE_RESERVED;
848 				set_status++;
849 			}
850 
851 			if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
852 				status[i] = SDW_SLAVE_ATTACHED;
853 				set_status++;
854 			}
855 
856 			if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
857 				status[i] = SDW_SLAVE_ALERT;
858 				set_status++;
859 			}
860 
861 			if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
862 				status[i] = SDW_SLAVE_UNATTACHED;
863 				set_status++;
864 			}
865 		}
866 
867 		/*
868 		 * check that there was a single reported Slave status and when
869 		 * there is not use the latest status extracted from PING commands
870 		 */
871 		if (set_status != 1) {
872 			val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
873 			val >>= (i * 2);
874 
875 			switch (val & 0x3) {
876 			case 0:
877 				status[i] = SDW_SLAVE_UNATTACHED;
878 				break;
879 			case 1:
880 				status[i] = SDW_SLAVE_ATTACHED;
881 				break;
882 			case 2:
883 				status[i] = SDW_SLAVE_ALERT;
884 				break;
885 			case 3:
886 			default:
887 				status[i] = SDW_SLAVE_RESERVED;
888 				break;
889 			}
890 		}
891 	}
892 
893 	if (is_slave) {
894 		int ret;
895 
896 		mutex_lock(&cdns->status_update_lock);
897 		ret = sdw_handle_slave_status(&cdns->bus, status);
898 		mutex_unlock(&cdns->status_update_lock);
899 		return ret;
900 	}
901 
902 	return 0;
903 }
904 
905 /**
906  * sdw_cdns_irq() - Cadence interrupt handler
907  * @irq: irq number
908  * @dev_id: irq context
909  */
910 irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
911 {
912 	struct sdw_cdns *cdns = dev_id;
913 	u32 int_status;
914 
915 	/* Check if the link is up */
916 	if (!cdns->link_up)
917 		return IRQ_NONE;
918 
919 	int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
920 
921 	/* check for reserved values read as zero */
922 	if (int_status & CDNS_MCP_INT_RESERVED)
923 		return IRQ_NONE;
924 
925 	if (!(int_status & CDNS_MCP_INT_IRQ))
926 		return IRQ_NONE;
927 
928 	if (int_status & CDNS_MCP_INT_RX_WL) {
929 		struct sdw_bus *bus = &cdns->bus;
930 		struct sdw_defer *defer = &bus->defer_msg;
931 
932 		cdns_read_response(cdns);
933 
934 		if (defer && defer->msg) {
935 			cdns_fill_msg_resp(cdns, defer->msg,
936 					   defer->length, 0);
937 			complete(&defer->complete);
938 		} else {
939 			complete(&cdns->tx_complete);
940 		}
941 	}
942 
943 	if (int_status & CDNS_MCP_INT_PARITY) {
944 		/* Parity error detected by Master */
945 		dev_err_ratelimited(cdns->dev, "Parity error\n");
946 	}
947 
948 	if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
949 		/* Slave is driving bit slot during control word */
950 		dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
951 	}
952 
953 	if (int_status & CDNS_MCP_INT_DATA_CLASH) {
954 		/*
955 		 * Multiple slaves trying to drive bit slot, or issue with
956 		 * ownership of data bits or Slave gone bonkers
957 		 */
958 		dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
959 	}
960 
961 	if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL &&
962 	    int_status & CDNS_MCP_INT_DPINT) {
963 		u32 port_intstat;
964 
965 		/* just log which ports report an error */
966 		port_intstat = cdns_readl(cdns, CDNS_MCP_PORT_INTSTAT);
967 		dev_err_ratelimited(cdns->dev, "DP interrupt: PortIntStat %8x\n",
968 				    port_intstat);
969 
970 		/* clear status w/ write1 */
971 		cdns_writel(cdns, CDNS_MCP_PORT_INTSTAT, port_intstat);
972 	}
973 
974 	if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
975 		/* Mask the Slave interrupt and wake thread */
976 		cdns_updatel(cdns, CDNS_MCP_INTMASK,
977 			     CDNS_MCP_INT_SLAVE_MASK, 0);
978 
979 		int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
980 
981 		/*
982 		 * Deal with possible race condition between interrupt
983 		 * handling and disabling interrupts on suspend.
984 		 *
985 		 * If the master is in the process of disabling
986 		 * interrupts, don't schedule a workqueue
987 		 */
988 		if (cdns->interrupt_enabled)
989 			schedule_work(&cdns->work);
990 	}
991 
992 	cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
993 	return IRQ_HANDLED;
994 }
995 EXPORT_SYMBOL(sdw_cdns_irq);
996 
997 static void cdns_check_attached_status_dwork(struct work_struct *work)
998 {
999 	struct sdw_cdns *cdns =
1000 		container_of(work, struct sdw_cdns, attach_dwork.work);
1001 	enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
1002 	u32 val;
1003 	int ret;
1004 	int i;
1005 
1006 	val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
1007 
1008 	for (i = 0; i <= SDW_MAX_DEVICES; i++) {
1009 		status[i] = val & 0x3;
1010 		if (status[i])
1011 			dev_dbg(cdns->dev, "Peripheral %d status: %d\n", i, status[i]);
1012 		val >>= 2;
1013 	}
1014 
1015 	mutex_lock(&cdns->status_update_lock);
1016 	ret = sdw_handle_slave_status(&cdns->bus, status);
1017 	mutex_unlock(&cdns->status_update_lock);
1018 	if (ret < 0)
1019 		dev_err(cdns->dev, "%s: sdw_handle_slave_status failed: %d\n", __func__, ret);
1020 }
1021 
1022 /**
1023  * cdns_update_slave_status_work - update slave status in a work since we will need to handle
1024  * other interrupts eg. CDNS_MCP_INT_RX_WL during the update slave
1025  * process.
1026  * @work: cdns worker thread
1027  */
1028 static void cdns_update_slave_status_work(struct work_struct *work)
1029 {
1030 	struct sdw_cdns *cdns =
1031 		container_of(work, struct sdw_cdns, work);
1032 	u32 slave0, slave1;
1033 	u64 slave_intstat;
1034 	u32 device0_status;
1035 	int retry_count = 0;
1036 
1037 	/*
1038 	 * Clear main interrupt first so we don't lose any assertions
1039 	 * that happen during this function.
1040 	 */
1041 	cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
1042 
1043 	slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
1044 	slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
1045 
1046 	/*
1047 	 * Clear the bits before handling so we don't lose any
1048 	 * bits that re-assert.
1049 	 */
1050 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
1051 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
1052 
1053 	/* combine the two status */
1054 	slave_intstat = ((u64)slave1 << 32) | slave0;
1055 
1056 	dev_dbg_ratelimited(cdns->dev, "Slave status change: 0x%llx\n", slave_intstat);
1057 
1058 update_status:
1059 	cdns_update_slave_status(cdns, slave_intstat);
1060 
1061 	/*
1062 	 * When there is more than one peripheral per link, it's
1063 	 * possible that a deviceB becomes attached after we deal with
1064 	 * the attachment of deviceA. Since the hardware does a
1065 	 * logical AND, the attachment of the second device does not
1066 	 * change the status seen by the driver.
1067 	 *
1068 	 * In that case, clearing the registers above would result in
1069 	 * the deviceB never being detected - until a change of status
1070 	 * is observed on the bus.
1071 	 *
1072 	 * To avoid this race condition, re-check if any device0 needs
1073 	 * attention with PING commands. There is no need to check for
1074 	 * ALERTS since they are not allowed until a non-zero
1075 	 * device_number is assigned.
1076 	 *
1077 	 * Do not clear the INTSTAT0/1. While looping to enumerate devices on
1078 	 * #0 there could be status changes on other devices - these must
1079 	 * be kept in the INTSTAT so they can be handled when all #0 devices
1080 	 * have been handled.
1081 	 */
1082 
1083 	device0_status = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
1084 	device0_status &= 3;
1085 
1086 	if (device0_status == SDW_SLAVE_ATTACHED) {
1087 		if (retry_count++ < SDW_MAX_DEVICES) {
1088 			dev_dbg_ratelimited(cdns->dev,
1089 					    "Device0 detected after clearing status, iteration %d\n",
1090 					    retry_count);
1091 			slave_intstat = CDNS_MCP_SLAVE_INTSTAT_ATTACHED;
1092 			goto update_status;
1093 		} else {
1094 			dev_err_ratelimited(cdns->dev,
1095 					    "Device0 detected after %d iterations\n",
1096 					    retry_count);
1097 		}
1098 	}
1099 
1100 	/* unmask Slave interrupt now */
1101 	cdns_updatel(cdns, CDNS_MCP_INTMASK,
1102 		     CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
1103 
1104 }
1105 
1106 /* paranoia check to make sure self-cleared bits are indeed cleared */
1107 void sdw_cdns_check_self_clearing_bits(struct sdw_cdns *cdns, const char *string,
1108 				       bool initial_delay, int reset_iterations)
1109 {
1110 	u32 ip_mcp_control;
1111 	u32 mcp_control;
1112 	u32 mcp_config_update;
1113 	int i;
1114 
1115 	if (initial_delay)
1116 		usleep_range(1000, 1500);
1117 
1118 	ip_mcp_control = cdns_ip_readl(cdns, CDNS_IP_MCP_CONTROL);
1119 
1120 	/* the following bits should be cleared immediately */
1121 	if (ip_mcp_control & CDNS_IP_MCP_CONTROL_SW_RST)
1122 		dev_err(cdns->dev, "%s failed: IP_MCP_CONTROL_SW_RST is not cleared\n", string);
1123 
1124 	mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
1125 
1126 	/* the following bits should be cleared immediately */
1127 	if (mcp_control & CDNS_MCP_CONTROL_CMD_RST)
1128 		dev_err(cdns->dev, "%s failed: MCP_CONTROL_CMD_RST is not cleared\n", string);
1129 	if (mcp_control & CDNS_MCP_CONTROL_SOFT_RST)
1130 		dev_err(cdns->dev, "%s failed: MCP_CONTROL_SOFT_RST is not cleared\n", string);
1131 	if (mcp_control & CDNS_MCP_CONTROL_CLK_STOP_CLR)
1132 		dev_err(cdns->dev, "%s failed: MCP_CONTROL_CLK_STOP_CLR is not cleared\n", string);
1133 
1134 	mcp_config_update = cdns_readl(cdns, CDNS_MCP_CONFIG_UPDATE);
1135 	if (mcp_config_update & CDNS_MCP_CONFIG_UPDATE_BIT)
1136 		dev_err(cdns->dev, "%s failed: MCP_CONFIG_UPDATE_BIT is not cleared\n", string);
1137 
1138 	i = 0;
1139 	while (mcp_control & CDNS_MCP_CONTROL_HW_RST) {
1140 		if (i == reset_iterations) {
1141 			dev_err(cdns->dev, "%s failed: MCP_CONTROL_HW_RST is not cleared\n", string);
1142 			break;
1143 		}
1144 
1145 		dev_dbg(cdns->dev, "%s: MCP_CONTROL_HW_RST is not cleared at iteration %d\n", string, i);
1146 		i++;
1147 
1148 		usleep_range(1000, 1500);
1149 		mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
1150 	}
1151 
1152 }
1153 EXPORT_SYMBOL(sdw_cdns_check_self_clearing_bits);
1154 
1155 /*
1156  * init routines
1157  */
1158 
1159 /**
1160  * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
1161  * @cdns: Cadence instance
1162  */
1163 int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
1164 {
1165 	/* keep reset delay unchanged to 4096 cycles */
1166 
1167 	/* use hardware generated reset */
1168 	cdns_updatel(cdns, CDNS_MCP_CONTROL,
1169 		     CDNS_MCP_CONTROL_HW_RST,
1170 		     CDNS_MCP_CONTROL_HW_RST);
1171 
1172 	/* commit changes */
1173 	return cdns_config_update(cdns);
1174 }
1175 EXPORT_SYMBOL(sdw_cdns_exit_reset);
1176 
1177 /**
1178  * cdns_enable_slave_interrupts() - Enable SDW slave interrupts
1179  * @cdns: Cadence instance
1180  * @state: boolean for true/false
1181  */
1182 static void cdns_enable_slave_interrupts(struct sdw_cdns *cdns, bool state)
1183 {
1184 	u32 mask;
1185 
1186 	mask = cdns_readl(cdns, CDNS_MCP_INTMASK);
1187 	if (state)
1188 		mask |= CDNS_MCP_INT_SLAVE_MASK;
1189 	else
1190 		mask &= ~CDNS_MCP_INT_SLAVE_MASK;
1191 
1192 	cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1193 }
1194 
1195 /**
1196  * sdw_cdns_enable_interrupt() - Enable SDW interrupts
1197  * @cdns: Cadence instance
1198  * @state: True if we are trying to enable interrupt.
1199  */
1200 int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
1201 {
1202 	u32 slave_intmask0 = 0;
1203 	u32 slave_intmask1 = 0;
1204 	u32 mask = 0;
1205 
1206 	if (!state)
1207 		goto update_masks;
1208 
1209 	slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
1210 	slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
1211 
1212 	/* enable detection of all slave state changes */
1213 	mask = CDNS_MCP_INT_SLAVE_MASK;
1214 
1215 	/* enable detection of bus issues */
1216 	mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
1217 		CDNS_MCP_INT_PARITY;
1218 
1219 	/* port interrupt limited to test modes for now */
1220 	if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1221 		mask |= CDNS_MCP_INT_DPINT;
1222 
1223 	/* enable detection of RX fifo level */
1224 	mask |= CDNS_MCP_INT_RX_WL;
1225 
1226 	/*
1227 	 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
1228 	 * settings are irrelevant
1229 	 */
1230 	mask |= CDNS_MCP_INT_IRQ;
1231 
1232 	if (interrupt_mask) /* parameter override */
1233 		mask = interrupt_mask;
1234 
1235 update_masks:
1236 	/* clear slave interrupt status before enabling interrupt */
1237 	if (state) {
1238 		u32 slave_state;
1239 
1240 		slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
1241 		cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state);
1242 		slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
1243 		cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state);
1244 	}
1245 	cdns->interrupt_enabled = state;
1246 
1247 	/*
1248 	 * Complete any on-going status updates before updating masks,
1249 	 * and cancel queued status updates.
1250 	 *
1251 	 * There could be a race with a new interrupt thrown before
1252 	 * the 3 mask updates below are complete, so in the interrupt
1253 	 * we use the 'interrupt_enabled' status to prevent new work
1254 	 * from being queued.
1255 	 */
1256 	if (!state)
1257 		cancel_work_sync(&cdns->work);
1258 
1259 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
1260 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
1261 	cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1262 
1263 	return 0;
1264 }
1265 EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
1266 
1267 static int cdns_allocate_pdi(struct sdw_cdns *cdns,
1268 			     struct sdw_cdns_pdi **stream,
1269 			     u32 num)
1270 {
1271 	struct sdw_cdns_pdi *pdi;
1272 	int i;
1273 
1274 	if (!num)
1275 		return 0;
1276 
1277 	pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
1278 	if (!pdi)
1279 		return -ENOMEM;
1280 
1281 	for (i = 0; i < num; i++) {
1282 		pdi[i].num = i;
1283 	}
1284 
1285 	*stream = pdi;
1286 	return 0;
1287 }
1288 
1289 /**
1290  * sdw_cdns_pdi_init() - PDI initialization routine
1291  *
1292  * @cdns: Cadence instance
1293  * @config: Stream configurations
1294  */
1295 int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
1296 		      struct sdw_cdns_stream_config config)
1297 {
1298 	struct sdw_cdns_streams *stream;
1299 	int ret;
1300 
1301 	cdns->pcm.num_bd = config.pcm_bd;
1302 	cdns->pcm.num_in = config.pcm_in;
1303 	cdns->pcm.num_out = config.pcm_out;
1304 
1305 	/* Allocate PDIs for PCMs */
1306 	stream = &cdns->pcm;
1307 
1308 	/* we allocate PDI0 and PDI1 which are used for Bulk */
1309 	ret = cdns_allocate_pdi(cdns, &stream->bd, stream->num_bd);
1310 	if (ret)
1311 		return ret;
1312 
1313 	ret = cdns_allocate_pdi(cdns, &stream->in, stream->num_in);
1314 	if (ret)
1315 		return ret;
1316 
1317 	ret = cdns_allocate_pdi(cdns, &stream->out, stream->num_out);
1318 	if (ret)
1319 		return ret;
1320 
1321 	/* Update total number of PCM PDIs */
1322 	stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1323 	cdns->num_ports = stream->num_pdi;
1324 
1325 	return 0;
1326 }
1327 EXPORT_SYMBOL(sdw_cdns_pdi_init);
1328 
1329 static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
1330 {
1331 	u32 val;
1332 	int c;
1333 	int r;
1334 
1335 	r = sdw_find_row_index(n_rows);
1336 	c = sdw_find_col_index(n_cols);
1337 
1338 	val = FIELD_PREP(CDNS_MCP_FRAME_SHAPE_ROW_MASK, r);
1339 	val |= FIELD_PREP(CDNS_MCP_FRAME_SHAPE_COL_MASK, c);
1340 
1341 	return val;
1342 }
1343 
1344 static void cdns_init_clock_ctrl(struct sdw_cdns *cdns)
1345 {
1346 	struct sdw_bus *bus = &cdns->bus;
1347 	struct sdw_master_prop *prop = &bus->prop;
1348 	u32 val;
1349 	u32 ssp_interval;
1350 	int divider;
1351 
1352 	dev_dbg(cdns->dev, "mclk %d max %d row %d col %d\n",
1353 		prop->mclk_freq,
1354 		prop->max_clk_freq,
1355 		prop->default_row,
1356 		prop->default_col);
1357 
1358 	/* Set clock divider */
1359 	divider	= (prop->mclk_freq / prop->max_clk_freq) - 1;
1360 
1361 	cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1362 		     CDNS_MCP_CLK_MCLKD_MASK, divider);
1363 	cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1364 		     CDNS_MCP_CLK_MCLKD_MASK, divider);
1365 
1366 	/*
1367 	 * Frame shape changes after initialization have to be done
1368 	 * with the bank switch mechanism
1369 	 */
1370 	val = cdns_set_initial_frame_shape(prop->default_row,
1371 					   prop->default_col);
1372 	cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
1373 
1374 	/* Set SSP interval to default value */
1375 	ssp_interval = prop->default_frame_rate / SDW_CADENCE_GSYNC_HZ;
1376 	cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, ssp_interval);
1377 	cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, ssp_interval);
1378 }
1379 
1380 /**
1381  * sdw_cdns_soft_reset() - Cadence soft-reset
1382  * @cdns: Cadence instance
1383  */
1384 int sdw_cdns_soft_reset(struct sdw_cdns *cdns)
1385 {
1386 	int ret;
1387 
1388 	cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_SOFT_RST,
1389 		     CDNS_MCP_CONTROL_SOFT_RST);
1390 
1391 	ret = cdns_config_update(cdns);
1392 	if (ret < 0) {
1393 		dev_err(cdns->dev, "%s: config update failed\n", __func__);
1394 		return ret;
1395 	}
1396 
1397 	ret = cdns_set_wait(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_SOFT_RST, 0);
1398 	if (ret < 0)
1399 		dev_err(cdns->dev, "%s: Soft Reset timed out\n", __func__);
1400 
1401 	return ret;
1402 }
1403 EXPORT_SYMBOL(sdw_cdns_soft_reset);
1404 
1405 /**
1406  * sdw_cdns_init() - Cadence initialization
1407  * @cdns: Cadence instance
1408  */
1409 int sdw_cdns_init(struct sdw_cdns *cdns)
1410 {
1411 	u32 val;
1412 
1413 	cdns_init_clock_ctrl(cdns);
1414 
1415 	sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1416 
1417 	/* reset msg_count to default value of FIFOLEVEL */
1418 	cdns->msg_count = cdns_readl(cdns, CDNS_MCP_FIFOLEVEL);
1419 
1420 	/* flush command FIFOs */
1421 	cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1422 		     CDNS_MCP_CONTROL_CMD_RST);
1423 
1424 	/* Set cmd accept mode */
1425 	cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, CDNS_IP_MCP_CONTROL_CMD_ACCEPT,
1426 			CDNS_IP_MCP_CONTROL_CMD_ACCEPT);
1427 
1428 	/* disable wakeup */
1429 	cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL,
1430 			CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP,
1431 			0);
1432 
1433 	/* Configure mcp config */
1434 	val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1435 
1436 	/* Disable auto bus release */
1437 	val &= ~CDNS_MCP_CONFIG_BUS_REL;
1438 
1439 	cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1440 
1441 	/* Configure IP mcp config */
1442 	val = cdns_ip_readl(cdns, CDNS_IP_MCP_CONFIG);
1443 
1444 	/* enable bus operations with clock and data */
1445 	val &= ~CDNS_IP_MCP_CONFIG_OP;
1446 	val |= CDNS_IP_MCP_CONFIG_OP_NORMAL;
1447 
1448 	/* Set cmd mode for Tx and Rx cmds */
1449 	val &= ~CDNS_IP_MCP_CONFIG_CMD;
1450 
1451 	/* Disable sniffer mode */
1452 	val &= ~CDNS_IP_MCP_CONFIG_SNIFFER;
1453 
1454 	if (cdns->bus.multi_link)
1455 		/* Set Multi-master mode to take gsync into account */
1456 		val |= CDNS_IP_MCP_CONFIG_MMASTER;
1457 
1458 	/* leave frame delay to hardware default of 0x1F */
1459 
1460 	/* leave command retry to hardware default of 0 */
1461 
1462 	cdns_ip_writel(cdns, CDNS_IP_MCP_CONFIG, val);
1463 
1464 	/* changes will be committed later */
1465 	return 0;
1466 }
1467 EXPORT_SYMBOL(sdw_cdns_init);
1468 
1469 int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1470 {
1471 	struct sdw_master_prop *prop = &bus->prop;
1472 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1473 	int mcp_clkctrl_off;
1474 	int divider;
1475 
1476 	if (!params->curr_dr_freq) {
1477 		dev_err(cdns->dev, "NULL curr_dr_freq\n");
1478 		return -EINVAL;
1479 	}
1480 
1481 	divider	= prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1482 		params->curr_dr_freq;
1483 	divider--; /* divider is 1/(N+1) */
1484 
1485 	if (params->next_bank)
1486 		mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1487 	else
1488 		mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1489 
1490 	cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
1491 
1492 	return 0;
1493 }
1494 EXPORT_SYMBOL(cdns_bus_conf);
1495 
1496 static int cdns_port_params(struct sdw_bus *bus,
1497 			    struct sdw_port_params *p_params, unsigned int bank)
1498 {
1499 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1500 	int dpn_config_off_source;
1501 	int dpn_config_off_target;
1502 	int target_num = p_params->num;
1503 	int source_num = p_params->num;
1504 	bool override = false;
1505 	int dpn_config;
1506 
1507 	if (target_num == cdns->pdi_loopback_target &&
1508 	    cdns->pdi_loopback_source != -1) {
1509 		source_num = cdns->pdi_loopback_source;
1510 		override = true;
1511 	}
1512 
1513 	if (bank) {
1514 		dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1515 		dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1516 	} else {
1517 		dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1518 		dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1519 	}
1520 
1521 	dpn_config = cdns_readl(cdns, dpn_config_off_source);
1522 
1523 	/* use port params if there is no loopback, otherwise use source as is */
1524 	if (!override) {
1525 		u32p_replace_bits(&dpn_config, p_params->bps - 1, CDNS_DPN_CONFIG_WL);
1526 		u32p_replace_bits(&dpn_config, p_params->flow_mode, CDNS_DPN_CONFIG_PORT_FLOW);
1527 		u32p_replace_bits(&dpn_config, p_params->data_mode, CDNS_DPN_CONFIG_PORT_DAT);
1528 	}
1529 
1530 	cdns_writel(cdns, dpn_config_off_target, dpn_config);
1531 
1532 	return 0;
1533 }
1534 
1535 static int cdns_transport_params(struct sdw_bus *bus,
1536 				 struct sdw_transport_params *t_params,
1537 				 enum sdw_reg_bank bank)
1538 {
1539 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1540 	int dpn_config;
1541 	int dpn_config_off_source;
1542 	int dpn_config_off_target;
1543 	int dpn_hctrl;
1544 	int dpn_hctrl_off_source;
1545 	int dpn_hctrl_off_target;
1546 	int dpn_offsetctrl;
1547 	int dpn_offsetctrl_off_source;
1548 	int dpn_offsetctrl_off_target;
1549 	int dpn_samplectrl;
1550 	int dpn_samplectrl_off_source;
1551 	int dpn_samplectrl_off_target;
1552 	int source_num = t_params->port_num;
1553 	int target_num = t_params->port_num;
1554 	bool override = false;
1555 
1556 	if (target_num == cdns->pdi_loopback_target &&
1557 	    cdns->pdi_loopback_source != -1) {
1558 		source_num = cdns->pdi_loopback_source;
1559 		override = true;
1560 	}
1561 
1562 	/*
1563 	 * Note: Only full data port is supported on the Master side for
1564 	 * both PCM and PDM ports.
1565 	 */
1566 
1567 	if (bank) {
1568 		dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1569 		dpn_hctrl_off_source = CDNS_DPN_B1_HCTRL(source_num);
1570 		dpn_offsetctrl_off_source = CDNS_DPN_B1_OFFSET_CTRL(source_num);
1571 		dpn_samplectrl_off_source = CDNS_DPN_B1_SAMPLE_CTRL(source_num);
1572 
1573 		dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1574 		dpn_hctrl_off_target = CDNS_DPN_B1_HCTRL(target_num);
1575 		dpn_offsetctrl_off_target = CDNS_DPN_B1_OFFSET_CTRL(target_num);
1576 		dpn_samplectrl_off_target = CDNS_DPN_B1_SAMPLE_CTRL(target_num);
1577 
1578 	} else {
1579 		dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1580 		dpn_hctrl_off_source = CDNS_DPN_B0_HCTRL(source_num);
1581 		dpn_offsetctrl_off_source = CDNS_DPN_B0_OFFSET_CTRL(source_num);
1582 		dpn_samplectrl_off_source = CDNS_DPN_B0_SAMPLE_CTRL(source_num);
1583 
1584 		dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1585 		dpn_hctrl_off_target = CDNS_DPN_B0_HCTRL(target_num);
1586 		dpn_offsetctrl_off_target = CDNS_DPN_B0_OFFSET_CTRL(target_num);
1587 		dpn_samplectrl_off_target = CDNS_DPN_B0_SAMPLE_CTRL(target_num);
1588 	}
1589 
1590 	dpn_config = cdns_readl(cdns, dpn_config_off_source);
1591 	if (!override) {
1592 		u32p_replace_bits(&dpn_config, t_params->blk_grp_ctrl, CDNS_DPN_CONFIG_BGC);
1593 		u32p_replace_bits(&dpn_config, t_params->blk_pkg_mode, CDNS_DPN_CONFIG_BPM);
1594 	}
1595 	cdns_writel(cdns, dpn_config_off_target, dpn_config);
1596 
1597 	if (!override) {
1598 		dpn_offsetctrl = 0;
1599 		u32p_replace_bits(&dpn_offsetctrl, t_params->offset1, CDNS_DPN_OFFSET_CTRL_1);
1600 		u32p_replace_bits(&dpn_offsetctrl, t_params->offset2, CDNS_DPN_OFFSET_CTRL_2);
1601 	} else {
1602 		dpn_offsetctrl = cdns_readl(cdns, dpn_offsetctrl_off_source);
1603 	}
1604 	cdns_writel(cdns, dpn_offsetctrl_off_target,  dpn_offsetctrl);
1605 
1606 	if (!override) {
1607 		dpn_hctrl = 0;
1608 		u32p_replace_bits(&dpn_hctrl, t_params->hstart, CDNS_DPN_HCTRL_HSTART);
1609 		u32p_replace_bits(&dpn_hctrl, t_params->hstop, CDNS_DPN_HCTRL_HSTOP);
1610 		u32p_replace_bits(&dpn_hctrl, t_params->lane_ctrl, CDNS_DPN_HCTRL_LCTRL);
1611 	} else {
1612 		dpn_hctrl = cdns_readl(cdns, dpn_hctrl_off_source);
1613 	}
1614 	cdns_writel(cdns, dpn_hctrl_off_target, dpn_hctrl);
1615 
1616 	if (!override)
1617 		dpn_samplectrl = t_params->sample_interval - 1;
1618 	else
1619 		dpn_samplectrl = cdns_readl(cdns, dpn_samplectrl_off_source);
1620 	cdns_writel(cdns, dpn_samplectrl_off_target, dpn_samplectrl);
1621 
1622 	return 0;
1623 }
1624 
1625 static int cdns_port_enable(struct sdw_bus *bus,
1626 			    struct sdw_enable_ch *enable_ch, unsigned int bank)
1627 {
1628 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1629 	int dpn_chnen_off, ch_mask;
1630 
1631 	if (bank)
1632 		dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1633 	else
1634 		dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1635 
1636 	ch_mask = enable_ch->ch_mask * enable_ch->enable;
1637 	cdns_writel(cdns, dpn_chnen_off, ch_mask);
1638 
1639 	return 0;
1640 }
1641 
1642 static const struct sdw_master_port_ops cdns_port_ops = {
1643 	.dpn_set_port_params = cdns_port_params,
1644 	.dpn_set_port_transport_params = cdns_transport_params,
1645 	.dpn_port_enable_ch = cdns_port_enable,
1646 };
1647 
1648 /**
1649  * sdw_cdns_is_clock_stop: Check clock status
1650  *
1651  * @cdns: Cadence instance
1652  */
1653 bool sdw_cdns_is_clock_stop(struct sdw_cdns *cdns)
1654 {
1655 	return !!(cdns_readl(cdns, CDNS_MCP_STAT) & CDNS_MCP_STAT_CLK_STOP);
1656 }
1657 EXPORT_SYMBOL(sdw_cdns_is_clock_stop);
1658 
1659 /**
1660  * sdw_cdns_clock_stop: Cadence clock stop configuration routine
1661  *
1662  * @cdns: Cadence instance
1663  * @block_wake: prevent wakes if required by the platform
1664  */
1665 int sdw_cdns_clock_stop(struct sdw_cdns *cdns, bool block_wake)
1666 {
1667 	bool slave_present = false;
1668 	struct sdw_slave *slave;
1669 	int ret;
1670 
1671 	sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1672 
1673 	/* Check suspend status */
1674 	if (sdw_cdns_is_clock_stop(cdns)) {
1675 		dev_dbg(cdns->dev, "Clock is already stopped\n");
1676 		return 0;
1677 	}
1678 
1679 	/*
1680 	 * Before entering clock stop we mask the Slave
1681 	 * interrupts. This helps avoid having to deal with e.g. a
1682 	 * Slave becoming UNATTACHED while the clock is being stopped
1683 	 */
1684 	cdns_enable_slave_interrupts(cdns, false);
1685 
1686 	/*
1687 	 * For specific platforms, it is required to be able to put
1688 	 * master into a state in which it ignores wake-up trials
1689 	 * in clock stop state
1690 	 */
1691 	if (block_wake)
1692 		cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL,
1693 				CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP,
1694 				CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP);
1695 
1696 	list_for_each_entry(slave, &cdns->bus.slaves, node) {
1697 		if (slave->status == SDW_SLAVE_ATTACHED ||
1698 		    slave->status == SDW_SLAVE_ALERT) {
1699 			slave_present = true;
1700 			break;
1701 		}
1702 	}
1703 
1704 	/* commit changes */
1705 	ret = cdns_config_update(cdns);
1706 	if (ret < 0) {
1707 		dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1708 		return ret;
1709 	}
1710 
1711 	/* Prepare slaves for clock stop */
1712 	if (slave_present) {
1713 		ret = sdw_bus_prep_clk_stop(&cdns->bus);
1714 		if (ret < 0 && ret != -ENODATA) {
1715 			dev_err(cdns->dev, "prepare clock stop failed %d\n", ret);
1716 			return ret;
1717 		}
1718 	}
1719 
1720 	/*
1721 	 * Enter clock stop mode and only report errors if there are
1722 	 * Slave devices present (ALERT or ATTACHED)
1723 	 */
1724 	ret = sdw_bus_clk_stop(&cdns->bus);
1725 	if (ret < 0 && slave_present && ret != -ENODATA) {
1726 		dev_err(cdns->dev, "bus clock stop failed %d\n", ret);
1727 		return ret;
1728 	}
1729 
1730 	ret = cdns_set_wait(cdns, CDNS_MCP_STAT,
1731 			    CDNS_MCP_STAT_CLK_STOP,
1732 			    CDNS_MCP_STAT_CLK_STOP);
1733 	if (ret < 0)
1734 		dev_err(cdns->dev, "Clock stop failed %d\n", ret);
1735 
1736 	return ret;
1737 }
1738 EXPORT_SYMBOL(sdw_cdns_clock_stop);
1739 
1740 /**
1741  * sdw_cdns_clock_restart: Cadence PM clock restart configuration routine
1742  *
1743  * @cdns: Cadence instance
1744  * @bus_reset: context may be lost while in low power modes and the bus
1745  * may require a Severe Reset and re-enumeration after a wake.
1746  */
1747 int sdw_cdns_clock_restart(struct sdw_cdns *cdns, bool bus_reset)
1748 {
1749 	int ret;
1750 
1751 	/* unmask Slave interrupts that were masked when stopping the clock */
1752 	cdns_enable_slave_interrupts(cdns, true);
1753 
1754 	ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
1755 			     CDNS_MCP_CONTROL_CLK_STOP_CLR);
1756 	if (ret < 0) {
1757 		dev_err(cdns->dev, "Couldn't exit from clock stop\n");
1758 		return ret;
1759 	}
1760 
1761 	ret = cdns_set_wait(cdns, CDNS_MCP_STAT, CDNS_MCP_STAT_CLK_STOP, 0);
1762 	if (ret < 0) {
1763 		dev_err(cdns->dev, "clock stop exit failed %d\n", ret);
1764 		return ret;
1765 	}
1766 
1767 	cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL,
1768 			CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP, 0);
1769 
1770 	cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, CDNS_IP_MCP_CONTROL_CMD_ACCEPT,
1771 			CDNS_IP_MCP_CONTROL_CMD_ACCEPT);
1772 
1773 	if (!bus_reset) {
1774 
1775 		/* enable bus operations with clock and data */
1776 		cdns_ip_updatel(cdns, CDNS_IP_MCP_CONFIG,
1777 				CDNS_IP_MCP_CONFIG_OP,
1778 				CDNS_IP_MCP_CONFIG_OP_NORMAL);
1779 
1780 		ret = cdns_config_update(cdns);
1781 		if (ret < 0) {
1782 			dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1783 			return ret;
1784 		}
1785 
1786 		ret = sdw_bus_exit_clk_stop(&cdns->bus);
1787 		if (ret < 0)
1788 			dev_err(cdns->dev, "bus failed to exit clock stop %d\n", ret);
1789 	}
1790 
1791 	return ret;
1792 }
1793 EXPORT_SYMBOL(sdw_cdns_clock_restart);
1794 
1795 /**
1796  * sdw_cdns_probe() - Cadence probe routine
1797  * @cdns: Cadence instance
1798  */
1799 int sdw_cdns_probe(struct sdw_cdns *cdns)
1800 {
1801 	init_completion(&cdns->tx_complete);
1802 	cdns->bus.port_ops = &cdns_port_ops;
1803 
1804 	mutex_init(&cdns->status_update_lock);
1805 
1806 	INIT_WORK(&cdns->work, cdns_update_slave_status_work);
1807 	INIT_DELAYED_WORK(&cdns->attach_dwork, cdns_check_attached_status_dwork);
1808 
1809 	return 0;
1810 }
1811 EXPORT_SYMBOL(sdw_cdns_probe);
1812 
1813 int cdns_set_sdw_stream(struct snd_soc_dai *dai,
1814 			void *stream, int direction)
1815 {
1816 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1817 	struct sdw_cdns_dai_runtime *dai_runtime;
1818 
1819 	dai_runtime = cdns->dai_runtime_array[dai->id];
1820 
1821 	if (stream) {
1822 		/* first paranoia check */
1823 		if (dai_runtime) {
1824 			dev_err(dai->dev,
1825 				"dai_runtime already allocated for dai %s\n",
1826 				dai->name);
1827 			return -EINVAL;
1828 		}
1829 
1830 		/* allocate and set dai_runtime info */
1831 		dai_runtime = kzalloc(sizeof(*dai_runtime), GFP_KERNEL);
1832 		if (!dai_runtime)
1833 			return -ENOMEM;
1834 
1835 		dai_runtime->stream_type = SDW_STREAM_PCM;
1836 
1837 		dai_runtime->bus = &cdns->bus;
1838 		dai_runtime->link_id = cdns->instance;
1839 
1840 		dai_runtime->stream = stream;
1841 		dai_runtime->direction = direction;
1842 
1843 		cdns->dai_runtime_array[dai->id] = dai_runtime;
1844 	} else {
1845 		/* second paranoia check */
1846 		if (!dai_runtime) {
1847 			dev_err(dai->dev,
1848 				"dai_runtime not allocated for dai %s\n",
1849 				dai->name);
1850 			return -EINVAL;
1851 		}
1852 
1853 		/* for NULL stream we release allocated dai_runtime */
1854 		kfree(dai_runtime);
1855 		cdns->dai_runtime_array[dai->id] = NULL;
1856 	}
1857 	return 0;
1858 }
1859 EXPORT_SYMBOL(cdns_set_sdw_stream);
1860 
1861 /**
1862  * cdns_find_pdi() - Find a free PDI
1863  *
1864  * @cdns: Cadence instance
1865  * @num: Number of PDIs
1866  * @pdi: PDI instances
1867  * @dai_id: DAI id
1868  *
1869  * Find a PDI for a given PDI array. The PDI num and dai_id are
1870  * expected to match, return NULL otherwise.
1871  */
1872 static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
1873 					  unsigned int num,
1874 					  struct sdw_cdns_pdi *pdi,
1875 					  int dai_id)
1876 {
1877 	int i;
1878 
1879 	for (i = 0; i < num; i++)
1880 		if (pdi[i].num == dai_id)
1881 			return &pdi[i];
1882 
1883 	return NULL;
1884 }
1885 
1886 /**
1887  * sdw_cdns_config_stream: Configure a stream
1888  *
1889  * @cdns: Cadence instance
1890  * @ch: Channel count
1891  * @dir: Data direction
1892  * @pdi: PDI to be used
1893  */
1894 void sdw_cdns_config_stream(struct sdw_cdns *cdns,
1895 			    u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
1896 {
1897 	u32 offset, val = 0;
1898 
1899 	if (dir == SDW_DATA_DIR_RX) {
1900 		val = CDNS_PORTCTRL_DIRN;
1901 
1902 		if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1903 			val |= CDNS_PORTCTRL_TEST_FAILED;
1904 	}
1905 	offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
1906 	cdns_updatel(cdns, offset,
1907 		     CDNS_PORTCTRL_DIRN | CDNS_PORTCTRL_TEST_FAILED,
1908 		     val);
1909 
1910 	val = pdi->num;
1911 	val |= CDNS_PDI_CONFIG_SOFT_RESET;
1912 	val |= FIELD_PREP(CDNS_PDI_CONFIG_CHANNEL, (1 << ch) - 1);
1913 	cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1914 }
1915 EXPORT_SYMBOL(sdw_cdns_config_stream);
1916 
1917 /**
1918  * sdw_cdns_alloc_pdi() - Allocate a PDI
1919  *
1920  * @cdns: Cadence instance
1921  * @stream: Stream to be allocated
1922  * @ch: Channel count
1923  * @dir: Data direction
1924  * @dai_id: DAI id
1925  */
1926 struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1927 					struct sdw_cdns_streams *stream,
1928 					u32 ch, u32 dir, int dai_id)
1929 {
1930 	struct sdw_cdns_pdi *pdi = NULL;
1931 
1932 	if (dir == SDW_DATA_DIR_RX)
1933 		pdi = cdns_find_pdi(cdns, stream->num_in, stream->in,
1934 				    dai_id);
1935 	else
1936 		pdi = cdns_find_pdi(cdns, stream->num_out, stream->out,
1937 				    dai_id);
1938 
1939 	/* check if we found a PDI, else find in bi-directional */
1940 	if (!pdi)
1941 		pdi = cdns_find_pdi(cdns, stream->num_bd, stream->bd,
1942 				    dai_id);
1943 
1944 	if (pdi) {
1945 		pdi->l_ch_num = 0;
1946 		pdi->h_ch_num = ch - 1;
1947 		pdi->dir = dir;
1948 		pdi->ch_count = ch;
1949 	}
1950 
1951 	return pdi;
1952 }
1953 EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
1954 
1955 MODULE_LICENSE("Dual BSD/GPL");
1956 MODULE_DESCRIPTION("Cadence Soundwire Library");
1957