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/soundwire/sdw_registers.h>
17 #include <linux/soundwire/sdw.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 #include "bus.h"
21 #include "cadence_master.h"
22 
23 static int interrupt_mask;
24 module_param_named(cnds_mcp_int_mask, interrupt_mask, int, 0444);
25 MODULE_PARM_DESC(cdns_mcp_int_mask, "Cadence MCP IntMask");
26 
27 #define CDNS_MCP_CONFIG				0x0
28 
29 #define CDNS_MCP_CONFIG_MCMD_RETRY		GENMASK(27, 24)
30 #define CDNS_MCP_CONFIG_MPREQ_DELAY		GENMASK(20, 16)
31 #define CDNS_MCP_CONFIG_MMASTER			BIT(7)
32 #define CDNS_MCP_CONFIG_BUS_REL			BIT(6)
33 #define CDNS_MCP_CONFIG_SNIFFER			BIT(5)
34 #define CDNS_MCP_CONFIG_SSPMOD			BIT(4)
35 #define CDNS_MCP_CONFIG_CMD			BIT(3)
36 #define CDNS_MCP_CONFIG_OP			GENMASK(2, 0)
37 #define CDNS_MCP_CONFIG_OP_NORMAL		0
38 
39 #define CDNS_MCP_CONTROL			0x4
40 
41 #define CDNS_MCP_CONTROL_RST_DELAY		GENMASK(10, 8)
42 #define CDNS_MCP_CONTROL_CMD_RST		BIT(7)
43 #define CDNS_MCP_CONTROL_SOFT_RST		BIT(6)
44 #define CDNS_MCP_CONTROL_SW_RST			BIT(5)
45 #define CDNS_MCP_CONTROL_HW_RST			BIT(4)
46 #define CDNS_MCP_CONTROL_CLK_PAUSE		BIT(3)
47 #define CDNS_MCP_CONTROL_CLK_STOP_CLR		BIT(2)
48 #define CDNS_MCP_CONTROL_CMD_ACCEPT		BIT(1)
49 #define CDNS_MCP_CONTROL_BLOCK_WAKEUP		BIT(0)
50 
51 #define CDNS_MCP_CMDCTRL			0x8
52 #define CDNS_MCP_SSPSTAT			0xC
53 #define CDNS_MCP_FRAME_SHAPE			0x10
54 #define CDNS_MCP_FRAME_SHAPE_INIT		0x14
55 #define CDNS_MCP_FRAME_SHAPE_COL_MASK		GENMASK(2, 0)
56 #define CDNS_MCP_FRAME_SHAPE_ROW_OFFSET		3
57 
58 #define CDNS_MCP_CONFIG_UPDATE			0x18
59 #define CDNS_MCP_CONFIG_UPDATE_BIT		BIT(0)
60 
61 #define CDNS_MCP_PHYCTRL			0x1C
62 #define CDNS_MCP_SSP_CTRL0			0x20
63 #define CDNS_MCP_SSP_CTRL1			0x28
64 #define CDNS_MCP_CLK_CTRL0			0x30
65 #define CDNS_MCP_CLK_CTRL1			0x38
66 #define CDNS_MCP_CLK_MCLKD_MASK		GENMASK(7, 0)
67 
68 #define CDNS_MCP_STAT				0x40
69 
70 #define CDNS_MCP_STAT_ACTIVE_BANK		BIT(20)
71 #define CDNS_MCP_STAT_CLK_STOP			BIT(16)
72 
73 #define CDNS_MCP_INTSTAT			0x44
74 #define CDNS_MCP_INTMASK			0x48
75 
76 #define CDNS_MCP_INT_IRQ			BIT(31)
77 #define CDNS_MCP_INT_WAKEUP			BIT(16)
78 #define CDNS_MCP_INT_SLAVE_RSVD			BIT(15)
79 #define CDNS_MCP_INT_SLAVE_ALERT		BIT(14)
80 #define CDNS_MCP_INT_SLAVE_ATTACH		BIT(13)
81 #define CDNS_MCP_INT_SLAVE_NATTACH		BIT(12)
82 #define CDNS_MCP_INT_SLAVE_MASK			GENMASK(15, 12)
83 #define CDNS_MCP_INT_DPINT			BIT(11)
84 #define CDNS_MCP_INT_CTRL_CLASH			BIT(10)
85 #define CDNS_MCP_INT_DATA_CLASH			BIT(9)
86 #define CDNS_MCP_INT_PARITY			BIT(8)
87 #define CDNS_MCP_INT_CMD_ERR			BIT(7)
88 #define CDNS_MCP_INT_RX_NE			BIT(3)
89 #define CDNS_MCP_INT_RX_WL			BIT(2)
90 #define CDNS_MCP_INT_TXE			BIT(1)
91 #define CDNS_MCP_INT_TXF			BIT(0)
92 
93 #define CDNS_MCP_INTSET				0x4C
94 
95 #define CDNS_MCP_SLAVE_STAT			0x50
96 #define CDNS_MCP_SLAVE_STAT_MASK		GENMASK(1, 0)
97 
98 #define CDNS_MCP_SLAVE_INTSTAT0			0x54
99 #define CDNS_MCP_SLAVE_INTSTAT1			0x58
100 #define CDNS_MCP_SLAVE_INTSTAT_NPRESENT		BIT(0)
101 #define CDNS_MCP_SLAVE_INTSTAT_ATTACHED		BIT(1)
102 #define CDNS_MCP_SLAVE_INTSTAT_ALERT		BIT(2)
103 #define CDNS_MCP_SLAVE_INTSTAT_RESERVED		BIT(3)
104 #define CDNS_MCP_SLAVE_STATUS_BITS		GENMASK(3, 0)
105 #define CDNS_MCP_SLAVE_STATUS_NUM		4
106 
107 #define CDNS_MCP_SLAVE_INTMASK0			0x5C
108 #define CDNS_MCP_SLAVE_INTMASK1			0x60
109 
110 #define CDNS_MCP_SLAVE_INTMASK0_MASK		GENMASK(31, 0)
111 #define CDNS_MCP_SLAVE_INTMASK1_MASK		GENMASK(15, 0)
112 
113 #define CDNS_MCP_PORT_INTSTAT			0x64
114 #define CDNS_MCP_PDI_STAT			0x6C
115 
116 #define CDNS_MCP_FIFOLEVEL			0x78
117 #define CDNS_MCP_FIFOSTAT			0x7C
118 #define CDNS_MCP_RX_FIFO_AVAIL			GENMASK(5, 0)
119 
120 #define CDNS_MCP_CMD_BASE			0x80
121 #define CDNS_MCP_RESP_BASE			0x80
122 #define CDNS_MCP_CMD_LEN			0x20
123 #define CDNS_MCP_CMD_WORD_LEN			0x4
124 
125 #define CDNS_MCP_CMD_SSP_TAG			BIT(31)
126 #define CDNS_MCP_CMD_COMMAND			GENMASK(30, 28)
127 #define CDNS_MCP_CMD_DEV_ADDR			GENMASK(27, 24)
128 #define CDNS_MCP_CMD_REG_ADDR_H			GENMASK(23, 16)
129 #define CDNS_MCP_CMD_REG_ADDR_L			GENMASK(15, 8)
130 #define CDNS_MCP_CMD_REG_DATA			GENMASK(7, 0)
131 
132 #define CDNS_MCP_CMD_READ			2
133 #define CDNS_MCP_CMD_WRITE			3
134 
135 #define CDNS_MCP_RESP_RDATA			GENMASK(15, 8)
136 #define CDNS_MCP_RESP_ACK			BIT(0)
137 #define CDNS_MCP_RESP_NACK			BIT(1)
138 
139 #define CDNS_DP_SIZE				128
140 
141 #define CDNS_DPN_B0_CONFIG(n)			(0x100 + CDNS_DP_SIZE * (n))
142 #define CDNS_DPN_B0_CH_EN(n)			(0x104 + CDNS_DP_SIZE * (n))
143 #define CDNS_DPN_B0_SAMPLE_CTRL(n)		(0x108 + CDNS_DP_SIZE * (n))
144 #define CDNS_DPN_B0_OFFSET_CTRL(n)		(0x10C + CDNS_DP_SIZE * (n))
145 #define CDNS_DPN_B0_HCTRL(n)			(0x110 + CDNS_DP_SIZE * (n))
146 #define CDNS_DPN_B0_ASYNC_CTRL(n)		(0x114 + CDNS_DP_SIZE * (n))
147 
148 #define CDNS_DPN_B1_CONFIG(n)			(0x118 + CDNS_DP_SIZE * (n))
149 #define CDNS_DPN_B1_CH_EN(n)			(0x11C + CDNS_DP_SIZE * (n))
150 #define CDNS_DPN_B1_SAMPLE_CTRL(n)		(0x120 + CDNS_DP_SIZE * (n))
151 #define CDNS_DPN_B1_OFFSET_CTRL(n)		(0x124 + CDNS_DP_SIZE * (n))
152 #define CDNS_DPN_B1_HCTRL(n)			(0x128 + CDNS_DP_SIZE * (n))
153 #define CDNS_DPN_B1_ASYNC_CTRL(n)		(0x12C + CDNS_DP_SIZE * (n))
154 
155 #define CDNS_DPN_CONFIG_BPM			BIT(18)
156 #define CDNS_DPN_CONFIG_BGC			GENMASK(17, 16)
157 #define CDNS_DPN_CONFIG_WL			GENMASK(12, 8)
158 #define CDNS_DPN_CONFIG_PORT_DAT		GENMASK(3, 2)
159 #define CDNS_DPN_CONFIG_PORT_FLOW		GENMASK(1, 0)
160 
161 #define CDNS_DPN_SAMPLE_CTRL_SI			GENMASK(15, 0)
162 
163 #define CDNS_DPN_OFFSET_CTRL_1			GENMASK(7, 0)
164 #define CDNS_DPN_OFFSET_CTRL_2			GENMASK(15, 8)
165 
166 #define CDNS_DPN_HCTRL_HSTOP			GENMASK(3, 0)
167 #define CDNS_DPN_HCTRL_HSTART			GENMASK(7, 4)
168 #define CDNS_DPN_HCTRL_LCTRL			GENMASK(10, 8)
169 
170 #define CDNS_PORTCTRL				0x130
171 #define CDNS_PORTCTRL_DIRN			BIT(7)
172 #define CDNS_PORTCTRL_BANK_INVERT		BIT(8)
173 
174 #define CDNS_PORT_OFFSET			0x80
175 
176 #define CDNS_PDI_CONFIG(n)			(0x1100 + (n) * 16)
177 
178 #define CDNS_PDI_CONFIG_SOFT_RESET		BIT(24)
179 #define CDNS_PDI_CONFIG_CHANNEL			GENMASK(15, 8)
180 #define CDNS_PDI_CONFIG_PORT			GENMASK(4, 0)
181 
182 /* Driver defaults */
183 #define CDNS_DEFAULT_SSP_INTERVAL		0x18
184 #define CDNS_TX_TIMEOUT				2000
185 
186 #define CDNS_SCP_RX_FIFOLEVEL			0x2
187 
188 /*
189  * register accessor helpers
190  */
191 static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset)
192 {
193 	return readl(cdns->registers + offset);
194 }
195 
196 static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value)
197 {
198 	writel(value, cdns->registers + offset);
199 }
200 
201 static inline void cdns_updatel(struct sdw_cdns *cdns,
202 				int offset, u32 mask, u32 val)
203 {
204 	u32 tmp;
205 
206 	tmp = cdns_readl(cdns, offset);
207 	tmp = (tmp & ~mask) | val;
208 	cdns_writel(cdns, offset, tmp);
209 }
210 
211 static int cdns_clear_bit(struct sdw_cdns *cdns, int offset, u32 value)
212 {
213 	int timeout = 10;
214 	u32 reg_read;
215 
216 	writel(value, cdns->registers + offset);
217 
218 	/* Wait for bit to be self cleared */
219 	do {
220 		reg_read = readl(cdns->registers + offset);
221 		if ((reg_read & value) == 0)
222 			return 0;
223 
224 		timeout--;
225 		udelay(50);
226 	} while (timeout != 0);
227 
228 	return -EAGAIN;
229 }
230 
231 /*
232  * all changes to the MCP_CONFIG, MCP_CONTROL, MCP_CMDCTRL and MCP_PHYCTRL
233  * need to be confirmed with a write to MCP_CONFIG_UPDATE
234  */
235 static int cdns_update_config(struct sdw_cdns *cdns)
236 {
237 	int ret;
238 
239 	ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE,
240 			     CDNS_MCP_CONFIG_UPDATE_BIT);
241 	if (ret < 0)
242 		dev_err(cdns->dev, "Config update timedout\n");
243 
244 	return ret;
245 }
246 
247 /*
248  * debugfs
249  */
250 #ifdef CONFIG_DEBUG_FS
251 
252 #define RD_BUF (2 * PAGE_SIZE)
253 
254 static ssize_t cdns_sprintf(struct sdw_cdns *cdns,
255 			    char *buf, size_t pos, unsigned int reg)
256 {
257 	return scnprintf(buf + pos, RD_BUF - pos,
258 			 "%4x\t%8x\n", reg, cdns_readl(cdns, reg));
259 }
260 
261 static int cdns_reg_show(struct seq_file *s, void *data)
262 {
263 	struct sdw_cdns *cdns = s->private;
264 	char *buf;
265 	ssize_t ret;
266 	int num_ports;
267 	int i, j;
268 
269 	buf = kzalloc(RD_BUF, GFP_KERNEL);
270 	if (!buf)
271 		return -ENOMEM;
272 
273 	ret = scnprintf(buf, RD_BUF, "Register  Value\n");
274 	ret += scnprintf(buf + ret, RD_BUF - ret, "\nMCP Registers\n");
275 	/* 8 MCP registers */
276 	for (i = CDNS_MCP_CONFIG; i <= CDNS_MCP_PHYCTRL; i += sizeof(u32))
277 		ret += cdns_sprintf(cdns, buf, ret, i);
278 
279 	ret += scnprintf(buf + ret, RD_BUF - ret,
280 			 "\nStatus & Intr Registers\n");
281 	/* 13 Status & Intr registers (offsets 0x70 and 0x74 not defined) */
282 	for (i = CDNS_MCP_STAT; i <=  CDNS_MCP_FIFOSTAT; i += sizeof(u32))
283 		ret += cdns_sprintf(cdns, buf, ret, i);
284 
285 	ret += scnprintf(buf + ret, RD_BUF - ret,
286 			 "\nSSP & Clk ctrl Registers\n");
287 	ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL0);
288 	ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL1);
289 	ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL0);
290 	ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL1);
291 
292 	ret += scnprintf(buf + ret, RD_BUF - ret,
293 			 "\nDPn B0 Registers\n");
294 
295 	num_ports = cdns->num_ports;
296 
297 	for (i = 0; i < num_ports; i++) {
298 		ret += scnprintf(buf + ret, RD_BUF - ret,
299 				 "\nDP-%d\n", i);
300 		for (j = CDNS_DPN_B0_CONFIG(i);
301 		     j < CDNS_DPN_B0_ASYNC_CTRL(i); j += sizeof(u32))
302 			ret += cdns_sprintf(cdns, buf, ret, j);
303 	}
304 
305 	ret += scnprintf(buf + ret, RD_BUF - ret,
306 			 "\nDPn B1 Registers\n");
307 	for (i = 0; i < num_ports; i++) {
308 		ret += scnprintf(buf + ret, RD_BUF - ret,
309 				 "\nDP-%d\n", i);
310 
311 		for (j = CDNS_DPN_B1_CONFIG(i);
312 		     j < CDNS_DPN_B1_ASYNC_CTRL(i); j += sizeof(u32))
313 			ret += cdns_sprintf(cdns, buf, ret, j);
314 	}
315 
316 	ret += scnprintf(buf + ret, RD_BUF - ret,
317 			 "\nDPn Control Registers\n");
318 	for (i = 0; i < num_ports; i++)
319 		ret += cdns_sprintf(cdns, buf, ret,
320 				CDNS_PORTCTRL + i * CDNS_PORT_OFFSET);
321 
322 	ret += scnprintf(buf + ret, RD_BUF - ret,
323 			 "\nPDIn Config Registers\n");
324 
325 	/* number of PDI and ports is interchangeable */
326 	for (i = 0; i < num_ports; i++)
327 		ret += cdns_sprintf(cdns, buf, ret, CDNS_PDI_CONFIG(i));
328 
329 	seq_printf(s, "%s", buf);
330 	kfree(buf);
331 
332 	return 0;
333 }
334 DEFINE_SHOW_ATTRIBUTE(cdns_reg);
335 
336 /**
337  * sdw_cdns_debugfs_init() - Cadence debugfs init
338  * @cdns: Cadence instance
339  * @root: debugfs root
340  */
341 void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
342 {
343 	debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
344 }
345 EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
346 
347 #endif /* CONFIG_DEBUG_FS */
348 
349 /*
350  * IO Calls
351  */
352 static enum sdw_command_response
353 cdns_fill_msg_resp(struct sdw_cdns *cdns,
354 		   struct sdw_msg *msg, int count, int offset)
355 {
356 	int nack = 0, no_ack = 0;
357 	int i;
358 
359 	/* check message response */
360 	for (i = 0; i < count; i++) {
361 		if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
362 			no_ack = 1;
363 			dev_dbg_ratelimited(cdns->dev, "Msg Ack not received\n");
364 			if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
365 				nack = 1;
366 				dev_err_ratelimited(cdns->dev, "Msg NACK received\n");
367 			}
368 		}
369 	}
370 
371 	if (nack) {
372 		dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
373 		return SDW_CMD_FAIL;
374 	} else if (no_ack) {
375 		dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
376 		return SDW_CMD_IGNORED;
377 	}
378 
379 	/* fill response */
380 	for (i = 0; i < count; i++)
381 		msg->buf[i + offset] = cdns->response_buf[i] >>
382 				SDW_REG_SHIFT(CDNS_MCP_RESP_RDATA);
383 
384 	return SDW_CMD_OK;
385 }
386 
387 static enum sdw_command_response
388 _cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
389 	       int offset, int count, bool defer)
390 {
391 	unsigned long time;
392 	u32 base, i, data;
393 	u16 addr;
394 
395 	/* Program the watermark level for RX FIFO */
396 	if (cdns->msg_count != count) {
397 		cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count);
398 		cdns->msg_count = count;
399 	}
400 
401 	base = CDNS_MCP_CMD_BASE;
402 	addr = msg->addr;
403 
404 	for (i = 0; i < count; i++) {
405 		data = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR);
406 		data |= cmd << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND);
407 		data |= addr++  << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
408 
409 		if (msg->flags == SDW_MSG_FLAG_WRITE)
410 			data |= msg->buf[i + offset];
411 
412 		data |= msg->ssp_sync << SDW_REG_SHIFT(CDNS_MCP_CMD_SSP_TAG);
413 		cdns_writel(cdns, base, data);
414 		base += CDNS_MCP_CMD_WORD_LEN;
415 	}
416 
417 	if (defer)
418 		return SDW_CMD_OK;
419 
420 	/* wait for timeout or response */
421 	time = wait_for_completion_timeout(&cdns->tx_complete,
422 					   msecs_to_jiffies(CDNS_TX_TIMEOUT));
423 	if (!time) {
424 		dev_err(cdns->dev, "IO transfer timed out\n");
425 		msg->len = 0;
426 		return SDW_CMD_TIMEOUT;
427 	}
428 
429 	return cdns_fill_msg_resp(cdns, msg, count, offset);
430 }
431 
432 static enum sdw_command_response
433 cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
434 {
435 	int nack = 0, no_ack = 0;
436 	unsigned long time;
437 	u32 data[2], base;
438 	int i;
439 
440 	/* Program the watermark level for RX FIFO */
441 	if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
442 		cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
443 		cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
444 	}
445 
446 	data[0] = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR);
447 	data[0] |= 0x3 << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND);
448 	data[1] = data[0];
449 
450 	data[0] |= SDW_SCP_ADDRPAGE1 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
451 	data[1] |= SDW_SCP_ADDRPAGE2 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
452 
453 	data[0] |= msg->addr_page1;
454 	data[1] |= msg->addr_page2;
455 
456 	base = CDNS_MCP_CMD_BASE;
457 	cdns_writel(cdns, base, data[0]);
458 	base += CDNS_MCP_CMD_WORD_LEN;
459 	cdns_writel(cdns, base, data[1]);
460 
461 	time = wait_for_completion_timeout(&cdns->tx_complete,
462 					   msecs_to_jiffies(CDNS_TX_TIMEOUT));
463 	if (!time) {
464 		dev_err(cdns->dev, "SCP Msg trf timed out\n");
465 		msg->len = 0;
466 		return SDW_CMD_TIMEOUT;
467 	}
468 
469 	/* check response the writes */
470 	for (i = 0; i < 2; i++) {
471 		if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
472 			no_ack = 1;
473 			dev_err(cdns->dev, "Program SCP Ack not received\n");
474 			if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
475 				nack = 1;
476 				dev_err(cdns->dev, "Program SCP NACK received\n");
477 			}
478 		}
479 	}
480 
481 	/* For NACK, NO ack, don't return err if we are in Broadcast mode */
482 	if (nack) {
483 		dev_err_ratelimited(cdns->dev,
484 				    "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
485 		return SDW_CMD_FAIL;
486 	} else if (no_ack) {
487 		dev_dbg_ratelimited(cdns->dev,
488 				    "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
489 		return SDW_CMD_IGNORED;
490 	}
491 
492 	return SDW_CMD_OK;
493 }
494 
495 static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
496 {
497 	int ret;
498 
499 	if (msg->page) {
500 		ret = cdns_program_scp_addr(cdns, msg);
501 		if (ret) {
502 			msg->len = 0;
503 			return ret;
504 		}
505 	}
506 
507 	switch (msg->flags) {
508 	case SDW_MSG_FLAG_READ:
509 		*cmd = CDNS_MCP_CMD_READ;
510 		break;
511 
512 	case SDW_MSG_FLAG_WRITE:
513 		*cmd = CDNS_MCP_CMD_WRITE;
514 		break;
515 
516 	default:
517 		dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
518 		return -EINVAL;
519 	}
520 
521 	return 0;
522 }
523 
524 enum sdw_command_response
525 cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
526 {
527 	struct sdw_cdns *cdns = bus_to_cdns(bus);
528 	int cmd = 0, ret, i;
529 
530 	ret = cdns_prep_msg(cdns, msg, &cmd);
531 	if (ret)
532 		return SDW_CMD_FAIL_OTHER;
533 
534 	for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
535 		ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
536 				     CDNS_MCP_CMD_LEN, false);
537 		if (ret < 0)
538 			goto exit;
539 	}
540 
541 	if (!(msg->len % CDNS_MCP_CMD_LEN))
542 		goto exit;
543 
544 	ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
545 			     msg->len % CDNS_MCP_CMD_LEN, false);
546 
547 exit:
548 	return ret;
549 }
550 EXPORT_SYMBOL(cdns_xfer_msg);
551 
552 enum sdw_command_response
553 cdns_xfer_msg_defer(struct sdw_bus *bus,
554 		    struct sdw_msg *msg, struct sdw_defer *defer)
555 {
556 	struct sdw_cdns *cdns = bus_to_cdns(bus);
557 	int cmd = 0, ret;
558 
559 	/* for defer only 1 message is supported */
560 	if (msg->len > 1)
561 		return -ENOTSUPP;
562 
563 	ret = cdns_prep_msg(cdns, msg, &cmd);
564 	if (ret)
565 		return SDW_CMD_FAIL_OTHER;
566 
567 	cdns->defer = defer;
568 	cdns->defer->length = msg->len;
569 
570 	return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
571 }
572 EXPORT_SYMBOL(cdns_xfer_msg_defer);
573 
574 enum sdw_command_response
575 cdns_reset_page_addr(struct sdw_bus *bus, unsigned int dev_num)
576 {
577 	struct sdw_cdns *cdns = bus_to_cdns(bus);
578 	struct sdw_msg msg;
579 
580 	/* Create dummy message with valid device number */
581 	memset(&msg, 0, sizeof(msg));
582 	msg.dev_num = dev_num;
583 
584 	return cdns_program_scp_addr(cdns, &msg);
585 }
586 EXPORT_SYMBOL(cdns_reset_page_addr);
587 
588 /*
589  * IRQ handling
590  */
591 
592 static void cdns_read_response(struct sdw_cdns *cdns)
593 {
594 	u32 num_resp, cmd_base;
595 	int i;
596 
597 	num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
598 	num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
599 
600 	cmd_base = CDNS_MCP_CMD_BASE;
601 
602 	for (i = 0; i < num_resp; i++) {
603 		cdns->response_buf[i] = cdns_readl(cdns, cmd_base);
604 		cmd_base += CDNS_MCP_CMD_WORD_LEN;
605 	}
606 }
607 
608 static int cdns_update_slave_status(struct sdw_cdns *cdns,
609 				    u32 slave0, u32 slave1)
610 {
611 	enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
612 	bool is_slave = false;
613 	u64 slave;
614 	u32 mask;
615 	int i, set_status;
616 
617 	/* combine the two status */
618 	slave = ((u64)slave1 << 32) | slave0;
619 	memset(status, 0, sizeof(status));
620 
621 	for (i = 0; i <= SDW_MAX_DEVICES; i++) {
622 		mask = (slave >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
623 				CDNS_MCP_SLAVE_STATUS_BITS;
624 		if (!mask)
625 			continue;
626 
627 		is_slave = true;
628 		set_status = 0;
629 
630 		if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
631 			status[i] = SDW_SLAVE_RESERVED;
632 			set_status++;
633 		}
634 
635 		if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
636 			status[i] = SDW_SLAVE_ATTACHED;
637 			set_status++;
638 		}
639 
640 		if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
641 			status[i] = SDW_SLAVE_ALERT;
642 			set_status++;
643 		}
644 
645 		if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
646 			status[i] = SDW_SLAVE_UNATTACHED;
647 			set_status++;
648 		}
649 
650 		/* first check if Slave reported multiple status */
651 		if (set_status > 1) {
652 			dev_warn_ratelimited(cdns->dev,
653 					     "Slave reported multiple Status: %d\n",
654 					     mask);
655 			/*
656 			 * TODO: we need to reread the status here by
657 			 * issuing a PING cmd
658 			 */
659 		}
660 	}
661 
662 	if (is_slave)
663 		return sdw_handle_slave_status(&cdns->bus, status);
664 
665 	return 0;
666 }
667 
668 /**
669  * sdw_cdns_irq() - Cadence interrupt handler
670  * @irq: irq number
671  * @dev_id: irq context
672  */
673 irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
674 {
675 	struct sdw_cdns *cdns = dev_id;
676 	u32 int_status;
677 	int ret = IRQ_HANDLED;
678 
679 	/* Check if the link is up */
680 	if (!cdns->link_up)
681 		return IRQ_NONE;
682 
683 	int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
684 
685 	if (!(int_status & CDNS_MCP_INT_IRQ))
686 		return IRQ_NONE;
687 
688 	if (int_status & CDNS_MCP_INT_RX_WL) {
689 		cdns_read_response(cdns);
690 
691 		if (cdns->defer) {
692 			cdns_fill_msg_resp(cdns, cdns->defer->msg,
693 					   cdns->defer->length, 0);
694 			complete(&cdns->defer->complete);
695 			cdns->defer = NULL;
696 		} else {
697 			complete(&cdns->tx_complete);
698 		}
699 	}
700 
701 	if (int_status & CDNS_MCP_INT_PARITY) {
702 		/* Parity error detected by Master */
703 		dev_err_ratelimited(cdns->dev, "Parity error\n");
704 	}
705 
706 	if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
707 		/* Slave is driving bit slot during control word */
708 		dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
709 	}
710 
711 	if (int_status & CDNS_MCP_INT_DATA_CLASH) {
712 		/*
713 		 * Multiple slaves trying to drive bit slot, or issue with
714 		 * ownership of data bits or Slave gone bonkers
715 		 */
716 		dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
717 	}
718 
719 	if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
720 		/* Mask the Slave interrupt and wake thread */
721 		cdns_updatel(cdns, CDNS_MCP_INTMASK,
722 			     CDNS_MCP_INT_SLAVE_MASK, 0);
723 
724 		int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
725 		ret = IRQ_WAKE_THREAD;
726 	}
727 
728 	cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
729 	return ret;
730 }
731 EXPORT_SYMBOL(sdw_cdns_irq);
732 
733 /**
734  * sdw_cdns_thread() - Cadence irq thread handler
735  * @irq: irq number
736  * @dev_id: irq context
737  */
738 irqreturn_t sdw_cdns_thread(int irq, void *dev_id)
739 {
740 	struct sdw_cdns *cdns = dev_id;
741 	u32 slave0, slave1;
742 
743 	dev_dbg_ratelimited(cdns->dev, "Slave status change\n");
744 
745 	slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
746 	slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
747 
748 	cdns_update_slave_status(cdns, slave0, slave1);
749 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
750 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
751 
752 	/* clear and unmask Slave interrupt now */
753 	cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
754 	cdns_updatel(cdns, CDNS_MCP_INTMASK,
755 		     CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
756 
757 	return IRQ_HANDLED;
758 }
759 EXPORT_SYMBOL(sdw_cdns_thread);
760 
761 /*
762  * init routines
763  */
764 
765 /**
766  * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
767  * @cdns: Cadence instance
768  */
769 int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
770 {
771 	/* program maximum length reset to be safe */
772 	cdns_updatel(cdns, CDNS_MCP_CONTROL,
773 		     CDNS_MCP_CONTROL_RST_DELAY,
774 		     CDNS_MCP_CONTROL_RST_DELAY);
775 
776 	/* use hardware generated reset */
777 	cdns_updatel(cdns, CDNS_MCP_CONTROL,
778 		     CDNS_MCP_CONTROL_HW_RST,
779 		     CDNS_MCP_CONTROL_HW_RST);
780 
781 	/* enable bus operations with clock and data */
782 	cdns_updatel(cdns, CDNS_MCP_CONFIG,
783 		     CDNS_MCP_CONFIG_OP,
784 		     CDNS_MCP_CONFIG_OP_NORMAL);
785 
786 	/* commit changes */
787 	return cdns_update_config(cdns);
788 }
789 EXPORT_SYMBOL(sdw_cdns_exit_reset);
790 
791 /**
792  * sdw_cdns_enable_interrupt() - Enable SDW interrupts and update config
793  * @cdns: Cadence instance
794  */
795 int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns)
796 {
797 	u32 mask;
798 
799 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0,
800 		    CDNS_MCP_SLAVE_INTMASK0_MASK);
801 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1,
802 		    CDNS_MCP_SLAVE_INTMASK1_MASK);
803 
804 	/* enable detection of all slave state changes */
805 	mask = CDNS_MCP_INT_SLAVE_MASK;
806 
807 	/* enable detection of bus issues */
808 	mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
809 		CDNS_MCP_INT_PARITY;
810 
811 	/* no detection of port interrupts for now */
812 
813 	/* enable detection of RX fifo level */
814 	mask |= CDNS_MCP_INT_RX_WL;
815 
816 	/*
817 	 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
818 	 * settings are irrelevant
819 	 */
820 	mask |= CDNS_MCP_INT_IRQ;
821 
822 	if (interrupt_mask) /* parameter override */
823 		mask = interrupt_mask;
824 
825 	cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
826 
827 	/* commit changes */
828 	return cdns_update_config(cdns);
829 }
830 EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
831 
832 static int cdns_allocate_pdi(struct sdw_cdns *cdns,
833 			     struct sdw_cdns_pdi **stream,
834 			     u32 num, u32 pdi_offset)
835 {
836 	struct sdw_cdns_pdi *pdi;
837 	int i;
838 
839 	if (!num)
840 		return 0;
841 
842 	pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
843 	if (!pdi)
844 		return -ENOMEM;
845 
846 	for (i = 0; i < num; i++) {
847 		pdi[i].num = i + pdi_offset;
848 	}
849 
850 	*stream = pdi;
851 	return 0;
852 }
853 
854 /**
855  * sdw_cdns_pdi_init() - PDI initialization routine
856  *
857  * @cdns: Cadence instance
858  * @config: Stream configurations
859  */
860 int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
861 		      struct sdw_cdns_stream_config config)
862 {
863 	struct sdw_cdns_streams *stream;
864 	int offset;
865 	int ret;
866 
867 	cdns->pcm.num_bd = config.pcm_bd;
868 	cdns->pcm.num_in = config.pcm_in;
869 	cdns->pcm.num_out = config.pcm_out;
870 	cdns->pdm.num_bd = config.pdm_bd;
871 	cdns->pdm.num_in = config.pdm_in;
872 	cdns->pdm.num_out = config.pdm_out;
873 
874 	/* Allocate PDIs for PCMs */
875 	stream = &cdns->pcm;
876 
877 	/* we allocate PDI0 and PDI1 which are used for Bulk */
878 	offset = 0;
879 
880 	ret = cdns_allocate_pdi(cdns, &stream->bd,
881 				stream->num_bd, offset);
882 	if (ret)
883 		return ret;
884 
885 	offset += stream->num_bd;
886 
887 	ret = cdns_allocate_pdi(cdns, &stream->in,
888 				stream->num_in, offset);
889 	if (ret)
890 		return ret;
891 
892 	offset += stream->num_in;
893 
894 	ret = cdns_allocate_pdi(cdns, &stream->out,
895 				stream->num_out, offset);
896 	if (ret)
897 		return ret;
898 
899 	/* Update total number of PCM PDIs */
900 	stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
901 	cdns->num_ports = stream->num_pdi;
902 
903 	/* Allocate PDIs for PDMs */
904 	stream = &cdns->pdm;
905 	ret = cdns_allocate_pdi(cdns, &stream->bd,
906 				stream->num_bd, offset);
907 	if (ret)
908 		return ret;
909 
910 	offset += stream->num_bd;
911 
912 	ret = cdns_allocate_pdi(cdns, &stream->in,
913 				stream->num_in, offset);
914 	if (ret)
915 		return ret;
916 
917 	offset += stream->num_in;
918 
919 	ret = cdns_allocate_pdi(cdns, &stream->out,
920 				stream->num_out, offset);
921 
922 	offset += stream->num_out;
923 
924 	if (ret)
925 		return ret;
926 
927 	/* Update total number of PDM PDIs */
928 	stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
929 	cdns->num_ports += stream->num_pdi;
930 
931 	return 0;
932 }
933 EXPORT_SYMBOL(sdw_cdns_pdi_init);
934 
935 static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
936 {
937 	u32 val;
938 	int c;
939 	int r;
940 
941 	r = sdw_find_row_index(n_rows);
942 	c = sdw_find_col_index(n_cols) & CDNS_MCP_FRAME_SHAPE_COL_MASK;
943 
944 	val = (r << CDNS_MCP_FRAME_SHAPE_ROW_OFFSET) | c;
945 
946 	return val;
947 }
948 
949 /**
950  * sdw_cdns_init() - Cadence initialization
951  * @cdns: Cadence instance
952  */
953 int sdw_cdns_init(struct sdw_cdns *cdns)
954 {
955 	struct sdw_bus *bus = &cdns->bus;
956 	struct sdw_master_prop *prop = &bus->prop;
957 	u32 val;
958 	int divider;
959 	int ret;
960 
961 	/* Exit clock stop */
962 	ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
963 			     CDNS_MCP_CONTROL_CLK_STOP_CLR);
964 	if (ret < 0) {
965 		dev_err(cdns->dev, "Couldn't exit from clock stop\n");
966 		return ret;
967 	}
968 
969 	/* Set clock divider */
970 	divider	= (prop->mclk_freq / prop->max_clk_freq) - 1;
971 
972 	cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
973 		     CDNS_MCP_CLK_MCLKD_MASK, divider);
974 	cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
975 		     CDNS_MCP_CLK_MCLKD_MASK, divider);
976 
977 	/*
978 	 * Frame shape changes after initialization have to be done
979 	 * with the bank switch mechanism
980 	 */
981 	val = cdns_set_initial_frame_shape(prop->default_row,
982 					   prop->default_col);
983 	cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
984 
985 	/* Set SSP interval to default value */
986 	cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, CDNS_DEFAULT_SSP_INTERVAL);
987 	cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, CDNS_DEFAULT_SSP_INTERVAL);
988 
989 	/* flush command FIFOs */
990 	cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
991 		     CDNS_MCP_CONTROL_CMD_RST);
992 
993 	/* Set cmd accept mode */
994 	cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT,
995 		     CDNS_MCP_CONTROL_CMD_ACCEPT);
996 
997 	/* Configure mcp config */
998 	val = cdns_readl(cdns, CDNS_MCP_CONFIG);
999 
1000 	/* Set Max cmd retry to 15 */
1001 	val |= CDNS_MCP_CONFIG_MCMD_RETRY;
1002 
1003 	/* Set frame delay between PREQ and ping frame to 15 frames */
1004 	val |= 0xF << SDW_REG_SHIFT(CDNS_MCP_CONFIG_MPREQ_DELAY);
1005 
1006 	/* Disable auto bus release */
1007 	val &= ~CDNS_MCP_CONFIG_BUS_REL;
1008 
1009 	/* Disable sniffer mode */
1010 	val &= ~CDNS_MCP_CONFIG_SNIFFER;
1011 
1012 	/* Set cmd mode for Tx and Rx cmds */
1013 	val &= ~CDNS_MCP_CONFIG_CMD;
1014 
1015 	cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1016 
1017 	/* commit changes */
1018 	return cdns_update_config(cdns);
1019 }
1020 EXPORT_SYMBOL(sdw_cdns_init);
1021 
1022 int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1023 {
1024 	struct sdw_master_prop *prop = &bus->prop;
1025 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1026 	int mcp_clkctrl_off;
1027 	int divider;
1028 
1029 	if (!params->curr_dr_freq) {
1030 		dev_err(cdns->dev, "NULL curr_dr_freq\n");
1031 		return -EINVAL;
1032 	}
1033 
1034 	divider	= prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1035 		params->curr_dr_freq;
1036 	divider--; /* divider is 1/(N+1) */
1037 
1038 	if (params->next_bank)
1039 		mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1040 	else
1041 		mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1042 
1043 	cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
1044 
1045 	return 0;
1046 }
1047 EXPORT_SYMBOL(cdns_bus_conf);
1048 
1049 static int cdns_port_params(struct sdw_bus *bus,
1050 			    struct sdw_port_params *p_params, unsigned int bank)
1051 {
1052 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1053 	int dpn_config = 0, dpn_config_off;
1054 
1055 	if (bank)
1056 		dpn_config_off = CDNS_DPN_B1_CONFIG(p_params->num);
1057 	else
1058 		dpn_config_off = CDNS_DPN_B0_CONFIG(p_params->num);
1059 
1060 	dpn_config = cdns_readl(cdns, dpn_config_off);
1061 
1062 	dpn_config |= ((p_params->bps - 1) <<
1063 				SDW_REG_SHIFT(CDNS_DPN_CONFIG_WL));
1064 	dpn_config |= (p_params->flow_mode <<
1065 				SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_FLOW));
1066 	dpn_config |= (p_params->data_mode <<
1067 				SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_DAT));
1068 
1069 	cdns_writel(cdns, dpn_config_off, dpn_config);
1070 
1071 	return 0;
1072 }
1073 
1074 static int cdns_transport_params(struct sdw_bus *bus,
1075 				 struct sdw_transport_params *t_params,
1076 				 enum sdw_reg_bank bank)
1077 {
1078 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1079 	int dpn_offsetctrl = 0, dpn_offsetctrl_off;
1080 	int dpn_config = 0, dpn_config_off;
1081 	int dpn_hctrl = 0, dpn_hctrl_off;
1082 	int num = t_params->port_num;
1083 	int dpn_samplectrl_off;
1084 
1085 	/*
1086 	 * Note: Only full data port is supported on the Master side for
1087 	 * both PCM and PDM ports.
1088 	 */
1089 
1090 	if (bank) {
1091 		dpn_config_off = CDNS_DPN_B1_CONFIG(num);
1092 		dpn_samplectrl_off = CDNS_DPN_B1_SAMPLE_CTRL(num);
1093 		dpn_hctrl_off = CDNS_DPN_B1_HCTRL(num);
1094 		dpn_offsetctrl_off = CDNS_DPN_B1_OFFSET_CTRL(num);
1095 	} else {
1096 		dpn_config_off = CDNS_DPN_B0_CONFIG(num);
1097 		dpn_samplectrl_off = CDNS_DPN_B0_SAMPLE_CTRL(num);
1098 		dpn_hctrl_off = CDNS_DPN_B0_HCTRL(num);
1099 		dpn_offsetctrl_off = CDNS_DPN_B0_OFFSET_CTRL(num);
1100 	}
1101 
1102 	dpn_config = cdns_readl(cdns, dpn_config_off);
1103 
1104 	dpn_config |= (t_params->blk_grp_ctrl <<
1105 				SDW_REG_SHIFT(CDNS_DPN_CONFIG_BGC));
1106 	dpn_config |= (t_params->blk_pkg_mode <<
1107 				SDW_REG_SHIFT(CDNS_DPN_CONFIG_BPM));
1108 	cdns_writel(cdns, dpn_config_off, dpn_config);
1109 
1110 	dpn_offsetctrl |= (t_params->offset1 <<
1111 				SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_1));
1112 	dpn_offsetctrl |= (t_params->offset2 <<
1113 				SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_2));
1114 	cdns_writel(cdns, dpn_offsetctrl_off,  dpn_offsetctrl);
1115 
1116 	dpn_hctrl |= (t_params->hstart <<
1117 				SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTART));
1118 	dpn_hctrl |= (t_params->hstop << SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTOP));
1119 	dpn_hctrl |= (t_params->lane_ctrl <<
1120 				SDW_REG_SHIFT(CDNS_DPN_HCTRL_LCTRL));
1121 
1122 	cdns_writel(cdns, dpn_hctrl_off, dpn_hctrl);
1123 	cdns_writel(cdns, dpn_samplectrl_off, (t_params->sample_interval - 1));
1124 
1125 	return 0;
1126 }
1127 
1128 static int cdns_port_enable(struct sdw_bus *bus,
1129 			    struct sdw_enable_ch *enable_ch, unsigned int bank)
1130 {
1131 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1132 	int dpn_chnen_off, ch_mask;
1133 
1134 	if (bank)
1135 		dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1136 	else
1137 		dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1138 
1139 	ch_mask = enable_ch->ch_mask * enable_ch->enable;
1140 	cdns_writel(cdns, dpn_chnen_off, ch_mask);
1141 
1142 	return 0;
1143 }
1144 
1145 static const struct sdw_master_port_ops cdns_port_ops = {
1146 	.dpn_set_port_params = cdns_port_params,
1147 	.dpn_set_port_transport_params = cdns_transport_params,
1148 	.dpn_port_enable_ch = cdns_port_enable,
1149 };
1150 
1151 /**
1152  * sdw_cdns_probe() - Cadence probe routine
1153  * @cdns: Cadence instance
1154  */
1155 int sdw_cdns_probe(struct sdw_cdns *cdns)
1156 {
1157 	init_completion(&cdns->tx_complete);
1158 	cdns->bus.port_ops = &cdns_port_ops;
1159 
1160 	return 0;
1161 }
1162 EXPORT_SYMBOL(sdw_cdns_probe);
1163 
1164 int cdns_set_sdw_stream(struct snd_soc_dai *dai,
1165 			void *stream, bool pcm, int direction)
1166 {
1167 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1168 	struct sdw_cdns_dma_data *dma;
1169 
1170 	dma = kzalloc(sizeof(*dma), GFP_KERNEL);
1171 	if (!dma)
1172 		return -ENOMEM;
1173 
1174 	if (pcm)
1175 		dma->stream_type = SDW_STREAM_PCM;
1176 	else
1177 		dma->stream_type = SDW_STREAM_PDM;
1178 
1179 	dma->bus = &cdns->bus;
1180 	dma->link_id = cdns->instance;
1181 
1182 	dma->stream = stream;
1183 
1184 	if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1185 		dai->playback_dma_data = dma;
1186 	else
1187 		dai->capture_dma_data = dma;
1188 
1189 	return 0;
1190 }
1191 EXPORT_SYMBOL(cdns_set_sdw_stream);
1192 
1193 /**
1194  * cdns_find_pdi() - Find a free PDI
1195  *
1196  * @cdns: Cadence instance
1197  * @num: Number of PDIs
1198  * @pdi: PDI instances
1199  *
1200  * Find a PDI for a given PDI array. The PDI num and dai_id are
1201  * expected to match, return NULL otherwise.
1202  */
1203 static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
1204 					  unsigned int offset,
1205 					  unsigned int num,
1206 					  struct sdw_cdns_pdi *pdi,
1207 					  int dai_id)
1208 {
1209 	int i;
1210 
1211 	for (i = offset; i < offset + num; i++)
1212 		if (pdi[i].num == dai_id)
1213 			return &pdi[i];
1214 
1215 	return NULL;
1216 }
1217 
1218 /**
1219  * sdw_cdns_config_stream: Configure a stream
1220  *
1221  * @cdns: Cadence instance
1222  * @ch: Channel count
1223  * @dir: Data direction
1224  * @pdi: PDI to be used
1225  */
1226 void sdw_cdns_config_stream(struct sdw_cdns *cdns,
1227 			    u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
1228 {
1229 	u32 offset, val = 0;
1230 
1231 	if (dir == SDW_DATA_DIR_RX)
1232 		val = CDNS_PORTCTRL_DIRN;
1233 
1234 	offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
1235 	cdns_updatel(cdns, offset, CDNS_PORTCTRL_DIRN, val);
1236 
1237 	val = pdi->num;
1238 	val |= ((1 << ch) - 1) << SDW_REG_SHIFT(CDNS_PDI_CONFIG_CHANNEL);
1239 	cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1240 }
1241 EXPORT_SYMBOL(sdw_cdns_config_stream);
1242 
1243 /**
1244  * sdw_cdns_alloc_pdi() - Allocate a PDI
1245  *
1246  * @cdns: Cadence instance
1247  * @stream: Stream to be allocated
1248  * @ch: Channel count
1249  * @dir: Data direction
1250  */
1251 struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1252 					struct sdw_cdns_streams *stream,
1253 					u32 ch, u32 dir, int dai_id)
1254 {
1255 	struct sdw_cdns_pdi *pdi = NULL;
1256 
1257 	if (dir == SDW_DATA_DIR_RX)
1258 		pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1259 				    dai_id);
1260 	else
1261 		pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1262 				    dai_id);
1263 
1264 	/* check if we found a PDI, else find in bi-directional */
1265 	if (!pdi)
1266 		pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd,
1267 				    dai_id);
1268 
1269 	if (pdi) {
1270 		pdi->l_ch_num = 0;
1271 		pdi->h_ch_num = ch - 1;
1272 		pdi->dir = dir;
1273 		pdi->ch_count = ch;
1274 	}
1275 
1276 	return pdi;
1277 }
1278 EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
1279 
1280 MODULE_LICENSE("Dual BSD/GPL");
1281 MODULE_DESCRIPTION("Cadence Soundwire Library");
1282