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