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