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 static int cdns_hw_reset(void *data, u64 value)
337 {
338 	struct sdw_cdns *cdns = data;
339 	int ret;
340 
341 	if (value != 1)
342 		return -EINVAL;
343 
344 	/* Userspace changed the hardware state behind the kernel's back */
345 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
346 
347 	ret = sdw_cdns_exit_reset(cdns);
348 
349 	dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret);
350 
351 	return ret;
352 }
353 
354 DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n");
355 
356 /**
357  * sdw_cdns_debugfs_init() - Cadence debugfs init
358  * @cdns: Cadence instance
359  * @root: debugfs root
360  */
361 void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
362 {
363 	debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
364 
365 	debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
366 			    &cdns_hw_reset_fops);
367 }
368 EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
369 
370 #endif /* CONFIG_DEBUG_FS */
371 
372 /*
373  * IO Calls
374  */
375 static enum sdw_command_response
376 cdns_fill_msg_resp(struct sdw_cdns *cdns,
377 		   struct sdw_msg *msg, int count, int offset)
378 {
379 	int nack = 0, no_ack = 0;
380 	int i;
381 
382 	/* check message response */
383 	for (i = 0; i < count; i++) {
384 		if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
385 			no_ack = 1;
386 			dev_dbg_ratelimited(cdns->dev, "Msg Ack not received\n");
387 			if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
388 				nack = 1;
389 				dev_err_ratelimited(cdns->dev, "Msg NACK received\n");
390 			}
391 		}
392 	}
393 
394 	if (nack) {
395 		dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
396 		return SDW_CMD_FAIL;
397 	} else if (no_ack) {
398 		dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
399 		return SDW_CMD_IGNORED;
400 	}
401 
402 	/* fill response */
403 	for (i = 0; i < count; i++)
404 		msg->buf[i + offset] = cdns->response_buf[i] >>
405 				SDW_REG_SHIFT(CDNS_MCP_RESP_RDATA);
406 
407 	return SDW_CMD_OK;
408 }
409 
410 static enum sdw_command_response
411 _cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
412 	       int offset, int count, bool defer)
413 {
414 	unsigned long time;
415 	u32 base, i, data;
416 	u16 addr;
417 
418 	/* Program the watermark level for RX FIFO */
419 	if (cdns->msg_count != count) {
420 		cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count);
421 		cdns->msg_count = count;
422 	}
423 
424 	base = CDNS_MCP_CMD_BASE;
425 	addr = msg->addr;
426 
427 	for (i = 0; i < count; i++) {
428 		data = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR);
429 		data |= cmd << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND);
430 		data |= addr++  << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
431 
432 		if (msg->flags == SDW_MSG_FLAG_WRITE)
433 			data |= msg->buf[i + offset];
434 
435 		data |= msg->ssp_sync << SDW_REG_SHIFT(CDNS_MCP_CMD_SSP_TAG);
436 		cdns_writel(cdns, base, data);
437 		base += CDNS_MCP_CMD_WORD_LEN;
438 	}
439 
440 	if (defer)
441 		return SDW_CMD_OK;
442 
443 	/* wait for timeout or response */
444 	time = wait_for_completion_timeout(&cdns->tx_complete,
445 					   msecs_to_jiffies(CDNS_TX_TIMEOUT));
446 	if (!time) {
447 		dev_err(cdns->dev, "IO transfer timed out\n");
448 		msg->len = 0;
449 		return SDW_CMD_TIMEOUT;
450 	}
451 
452 	return cdns_fill_msg_resp(cdns, msg, count, offset);
453 }
454 
455 static enum sdw_command_response
456 cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
457 {
458 	int nack = 0, no_ack = 0;
459 	unsigned long time;
460 	u32 data[2], base;
461 	int i;
462 
463 	/* Program the watermark level for RX FIFO */
464 	if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
465 		cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
466 		cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
467 	}
468 
469 	data[0] = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR);
470 	data[0] |= 0x3 << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND);
471 	data[1] = data[0];
472 
473 	data[0] |= SDW_SCP_ADDRPAGE1 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
474 	data[1] |= SDW_SCP_ADDRPAGE2 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
475 
476 	data[0] |= msg->addr_page1;
477 	data[1] |= msg->addr_page2;
478 
479 	base = CDNS_MCP_CMD_BASE;
480 	cdns_writel(cdns, base, data[0]);
481 	base += CDNS_MCP_CMD_WORD_LEN;
482 	cdns_writel(cdns, base, data[1]);
483 
484 	time = wait_for_completion_timeout(&cdns->tx_complete,
485 					   msecs_to_jiffies(CDNS_TX_TIMEOUT));
486 	if (!time) {
487 		dev_err(cdns->dev, "SCP Msg trf timed out\n");
488 		msg->len = 0;
489 		return SDW_CMD_TIMEOUT;
490 	}
491 
492 	/* check response the writes */
493 	for (i = 0; i < 2; i++) {
494 		if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
495 			no_ack = 1;
496 			dev_err(cdns->dev, "Program SCP Ack not received\n");
497 			if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
498 				nack = 1;
499 				dev_err(cdns->dev, "Program SCP NACK received\n");
500 			}
501 		}
502 	}
503 
504 	/* For NACK, NO ack, don't return err if we are in Broadcast mode */
505 	if (nack) {
506 		dev_err_ratelimited(cdns->dev,
507 				    "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
508 		return SDW_CMD_FAIL;
509 	} else if (no_ack) {
510 		dev_dbg_ratelimited(cdns->dev,
511 				    "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
512 		return SDW_CMD_IGNORED;
513 	}
514 
515 	return SDW_CMD_OK;
516 }
517 
518 static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
519 {
520 	int ret;
521 
522 	if (msg->page) {
523 		ret = cdns_program_scp_addr(cdns, msg);
524 		if (ret) {
525 			msg->len = 0;
526 			return ret;
527 		}
528 	}
529 
530 	switch (msg->flags) {
531 	case SDW_MSG_FLAG_READ:
532 		*cmd = CDNS_MCP_CMD_READ;
533 		break;
534 
535 	case SDW_MSG_FLAG_WRITE:
536 		*cmd = CDNS_MCP_CMD_WRITE;
537 		break;
538 
539 	default:
540 		dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
541 		return -EINVAL;
542 	}
543 
544 	return 0;
545 }
546 
547 enum sdw_command_response
548 cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
549 {
550 	struct sdw_cdns *cdns = bus_to_cdns(bus);
551 	int cmd = 0, ret, i;
552 
553 	ret = cdns_prep_msg(cdns, msg, &cmd);
554 	if (ret)
555 		return SDW_CMD_FAIL_OTHER;
556 
557 	for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
558 		ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
559 				     CDNS_MCP_CMD_LEN, false);
560 		if (ret < 0)
561 			goto exit;
562 	}
563 
564 	if (!(msg->len % CDNS_MCP_CMD_LEN))
565 		goto exit;
566 
567 	ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
568 			     msg->len % CDNS_MCP_CMD_LEN, false);
569 
570 exit:
571 	return ret;
572 }
573 EXPORT_SYMBOL(cdns_xfer_msg);
574 
575 enum sdw_command_response
576 cdns_xfer_msg_defer(struct sdw_bus *bus,
577 		    struct sdw_msg *msg, struct sdw_defer *defer)
578 {
579 	struct sdw_cdns *cdns = bus_to_cdns(bus);
580 	int cmd = 0, ret;
581 
582 	/* for defer only 1 message is supported */
583 	if (msg->len > 1)
584 		return -ENOTSUPP;
585 
586 	ret = cdns_prep_msg(cdns, msg, &cmd);
587 	if (ret)
588 		return SDW_CMD_FAIL_OTHER;
589 
590 	cdns->defer = defer;
591 	cdns->defer->length = msg->len;
592 
593 	return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
594 }
595 EXPORT_SYMBOL(cdns_xfer_msg_defer);
596 
597 enum sdw_command_response
598 cdns_reset_page_addr(struct sdw_bus *bus, unsigned int dev_num)
599 {
600 	struct sdw_cdns *cdns = bus_to_cdns(bus);
601 	struct sdw_msg msg;
602 
603 	/* Create dummy message with valid device number */
604 	memset(&msg, 0, sizeof(msg));
605 	msg.dev_num = dev_num;
606 
607 	return cdns_program_scp_addr(cdns, &msg);
608 }
609 EXPORT_SYMBOL(cdns_reset_page_addr);
610 
611 /*
612  * IRQ handling
613  */
614 
615 static void cdns_read_response(struct sdw_cdns *cdns)
616 {
617 	u32 num_resp, cmd_base;
618 	int i;
619 
620 	num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
621 	num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
622 
623 	cmd_base = CDNS_MCP_CMD_BASE;
624 
625 	for (i = 0; i < num_resp; i++) {
626 		cdns->response_buf[i] = cdns_readl(cdns, cmd_base);
627 		cmd_base += CDNS_MCP_CMD_WORD_LEN;
628 	}
629 }
630 
631 static int cdns_update_slave_status(struct sdw_cdns *cdns,
632 				    u32 slave0, u32 slave1)
633 {
634 	enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
635 	bool is_slave = false;
636 	u64 slave;
637 	u32 mask;
638 	int i, set_status;
639 
640 	/* combine the two status */
641 	slave = ((u64)slave1 << 32) | slave0;
642 	memset(status, 0, sizeof(status));
643 
644 	for (i = 0; i <= SDW_MAX_DEVICES; i++) {
645 		mask = (slave >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
646 				CDNS_MCP_SLAVE_STATUS_BITS;
647 		if (!mask)
648 			continue;
649 
650 		is_slave = true;
651 		set_status = 0;
652 
653 		if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
654 			status[i] = SDW_SLAVE_RESERVED;
655 			set_status++;
656 		}
657 
658 		if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
659 			status[i] = SDW_SLAVE_ATTACHED;
660 			set_status++;
661 		}
662 
663 		if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
664 			status[i] = SDW_SLAVE_ALERT;
665 			set_status++;
666 		}
667 
668 		if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
669 			status[i] = SDW_SLAVE_UNATTACHED;
670 			set_status++;
671 		}
672 
673 		/* first check if Slave reported multiple status */
674 		if (set_status > 1) {
675 			dev_warn_ratelimited(cdns->dev,
676 					     "Slave reported multiple Status: %d\n",
677 					     mask);
678 			/*
679 			 * TODO: we need to reread the status here by
680 			 * issuing a PING cmd
681 			 */
682 		}
683 	}
684 
685 	if (is_slave)
686 		return sdw_handle_slave_status(&cdns->bus, status);
687 
688 	return 0;
689 }
690 
691 /**
692  * sdw_cdns_irq() - Cadence interrupt handler
693  * @irq: irq number
694  * @dev_id: irq context
695  */
696 irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
697 {
698 	struct sdw_cdns *cdns = dev_id;
699 	u32 int_status;
700 	int ret = IRQ_HANDLED;
701 
702 	/* Check if the link is up */
703 	if (!cdns->link_up)
704 		return IRQ_NONE;
705 
706 	int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
707 
708 	if (!(int_status & CDNS_MCP_INT_IRQ))
709 		return IRQ_NONE;
710 
711 	if (int_status & CDNS_MCP_INT_RX_WL) {
712 		cdns_read_response(cdns);
713 
714 		if (cdns->defer) {
715 			cdns_fill_msg_resp(cdns, cdns->defer->msg,
716 					   cdns->defer->length, 0);
717 			complete(&cdns->defer->complete);
718 			cdns->defer = NULL;
719 		} else {
720 			complete(&cdns->tx_complete);
721 		}
722 	}
723 
724 	if (int_status & CDNS_MCP_INT_PARITY) {
725 		/* Parity error detected by Master */
726 		dev_err_ratelimited(cdns->dev, "Parity error\n");
727 	}
728 
729 	if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
730 		/* Slave is driving bit slot during control word */
731 		dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
732 	}
733 
734 	if (int_status & CDNS_MCP_INT_DATA_CLASH) {
735 		/*
736 		 * Multiple slaves trying to drive bit slot, or issue with
737 		 * ownership of data bits or Slave gone bonkers
738 		 */
739 		dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
740 	}
741 
742 	if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
743 		/* Mask the Slave interrupt and wake thread */
744 		cdns_updatel(cdns, CDNS_MCP_INTMASK,
745 			     CDNS_MCP_INT_SLAVE_MASK, 0);
746 
747 		int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
748 		ret = IRQ_WAKE_THREAD;
749 	}
750 
751 	cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
752 	return ret;
753 }
754 EXPORT_SYMBOL(sdw_cdns_irq);
755 
756 /**
757  * sdw_cdns_thread() - Cadence irq thread handler
758  * @irq: irq number
759  * @dev_id: irq context
760  */
761 irqreturn_t sdw_cdns_thread(int irq, void *dev_id)
762 {
763 	struct sdw_cdns *cdns = dev_id;
764 	u32 slave0, slave1;
765 
766 	dev_dbg_ratelimited(cdns->dev, "Slave status change\n");
767 
768 	slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
769 	slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
770 
771 	cdns_update_slave_status(cdns, slave0, slave1);
772 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
773 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
774 
775 	/* clear and unmask Slave interrupt now */
776 	cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
777 	cdns_updatel(cdns, CDNS_MCP_INTMASK,
778 		     CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
779 
780 	return IRQ_HANDLED;
781 }
782 EXPORT_SYMBOL(sdw_cdns_thread);
783 
784 /*
785  * init routines
786  */
787 
788 /**
789  * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
790  * @cdns: Cadence instance
791  */
792 int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
793 {
794 	/* program maximum length reset to be safe */
795 	cdns_updatel(cdns, CDNS_MCP_CONTROL,
796 		     CDNS_MCP_CONTROL_RST_DELAY,
797 		     CDNS_MCP_CONTROL_RST_DELAY);
798 
799 	/* use hardware generated reset */
800 	cdns_updatel(cdns, CDNS_MCP_CONTROL,
801 		     CDNS_MCP_CONTROL_HW_RST,
802 		     CDNS_MCP_CONTROL_HW_RST);
803 
804 	/* enable bus operations with clock and data */
805 	cdns_updatel(cdns, CDNS_MCP_CONFIG,
806 		     CDNS_MCP_CONFIG_OP,
807 		     CDNS_MCP_CONFIG_OP_NORMAL);
808 
809 	/* commit changes */
810 	return cdns_update_config(cdns);
811 }
812 EXPORT_SYMBOL(sdw_cdns_exit_reset);
813 
814 /**
815  * sdw_cdns_enable_interrupt() - Enable SDW interrupts and update config
816  * @cdns: Cadence instance
817  */
818 int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
819 {
820 	u32 slave_intmask0 = 0;
821 	u32 slave_intmask1 = 0;
822 	u32 mask = 0;
823 
824 	if (!state)
825 		goto update_masks;
826 
827 	slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
828 	slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
829 
830 	/* enable detection of all slave state changes */
831 	mask = CDNS_MCP_INT_SLAVE_MASK;
832 
833 	/* enable detection of bus issues */
834 	mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
835 		CDNS_MCP_INT_PARITY;
836 
837 	/* no detection of port interrupts for now */
838 
839 	/* enable detection of RX fifo level */
840 	mask |= CDNS_MCP_INT_RX_WL;
841 
842 	/*
843 	 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
844 	 * settings are irrelevant
845 	 */
846 	mask |= CDNS_MCP_INT_IRQ;
847 
848 	if (interrupt_mask) /* parameter override */
849 		mask = interrupt_mask;
850 
851 update_masks:
852 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
853 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
854 	cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
855 
856 	/* commit changes */
857 	return cdns_update_config(cdns);
858 }
859 EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
860 
861 static int cdns_allocate_pdi(struct sdw_cdns *cdns,
862 			     struct sdw_cdns_pdi **stream,
863 			     u32 num, u32 pdi_offset)
864 {
865 	struct sdw_cdns_pdi *pdi;
866 	int i;
867 
868 	if (!num)
869 		return 0;
870 
871 	pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
872 	if (!pdi)
873 		return -ENOMEM;
874 
875 	for (i = 0; i < num; i++) {
876 		pdi[i].num = i + pdi_offset;
877 	}
878 
879 	*stream = pdi;
880 	return 0;
881 }
882 
883 /**
884  * sdw_cdns_pdi_init() - PDI initialization routine
885  *
886  * @cdns: Cadence instance
887  * @config: Stream configurations
888  */
889 int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
890 		      struct sdw_cdns_stream_config config)
891 {
892 	struct sdw_cdns_streams *stream;
893 	int offset;
894 	int ret;
895 
896 	cdns->pcm.num_bd = config.pcm_bd;
897 	cdns->pcm.num_in = config.pcm_in;
898 	cdns->pcm.num_out = config.pcm_out;
899 	cdns->pdm.num_bd = config.pdm_bd;
900 	cdns->pdm.num_in = config.pdm_in;
901 	cdns->pdm.num_out = config.pdm_out;
902 
903 	/* Allocate PDIs for PCMs */
904 	stream = &cdns->pcm;
905 
906 	/* we allocate PDI0 and PDI1 which are used for Bulk */
907 	offset = 0;
908 
909 	ret = cdns_allocate_pdi(cdns, &stream->bd,
910 				stream->num_bd, offset);
911 	if (ret)
912 		return ret;
913 
914 	offset += stream->num_bd;
915 
916 	ret = cdns_allocate_pdi(cdns, &stream->in,
917 				stream->num_in, offset);
918 	if (ret)
919 		return ret;
920 
921 	offset += stream->num_in;
922 
923 	ret = cdns_allocate_pdi(cdns, &stream->out,
924 				stream->num_out, offset);
925 	if (ret)
926 		return ret;
927 
928 	/* Update total number of PCM PDIs */
929 	stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
930 	cdns->num_ports = stream->num_pdi;
931 
932 	/* Allocate PDIs for PDMs */
933 	stream = &cdns->pdm;
934 	ret = cdns_allocate_pdi(cdns, &stream->bd,
935 				stream->num_bd, offset);
936 	if (ret)
937 		return ret;
938 
939 	offset += stream->num_bd;
940 
941 	ret = cdns_allocate_pdi(cdns, &stream->in,
942 				stream->num_in, offset);
943 	if (ret)
944 		return ret;
945 
946 	offset += stream->num_in;
947 
948 	ret = cdns_allocate_pdi(cdns, &stream->out,
949 				stream->num_out, offset);
950 
951 	offset += stream->num_out;
952 
953 	if (ret)
954 		return ret;
955 
956 	/* Update total number of PDM PDIs */
957 	stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
958 	cdns->num_ports += stream->num_pdi;
959 
960 	return 0;
961 }
962 EXPORT_SYMBOL(sdw_cdns_pdi_init);
963 
964 static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
965 {
966 	u32 val;
967 	int c;
968 	int r;
969 
970 	r = sdw_find_row_index(n_rows);
971 	c = sdw_find_col_index(n_cols) & CDNS_MCP_FRAME_SHAPE_COL_MASK;
972 
973 	val = (r << CDNS_MCP_FRAME_SHAPE_ROW_OFFSET) | c;
974 
975 	return val;
976 }
977 
978 /**
979  * sdw_cdns_init() - Cadence initialization
980  * @cdns: Cadence instance
981  */
982 int sdw_cdns_init(struct sdw_cdns *cdns, bool clock_stop_exit)
983 {
984 	struct sdw_bus *bus = &cdns->bus;
985 	struct sdw_master_prop *prop = &bus->prop;
986 	u32 val;
987 	int divider;
988 	int ret;
989 
990 	if (clock_stop_exit) {
991 		ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
992 				     CDNS_MCP_CONTROL_CLK_STOP_CLR);
993 		if (ret < 0) {
994 			dev_err(cdns->dev, "Couldn't exit from clock stop\n");
995 			return ret;
996 		}
997 	}
998 
999 	/* Set clock divider */
1000 	divider	= (prop->mclk_freq / prop->max_clk_freq) - 1;
1001 
1002 	cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1003 		     CDNS_MCP_CLK_MCLKD_MASK, divider);
1004 	cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1005 		     CDNS_MCP_CLK_MCLKD_MASK, divider);
1006 
1007 	/*
1008 	 * Frame shape changes after initialization have to be done
1009 	 * with the bank switch mechanism
1010 	 */
1011 	val = cdns_set_initial_frame_shape(prop->default_row,
1012 					   prop->default_col);
1013 	cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
1014 
1015 	/* Set SSP interval to default value */
1016 	cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, CDNS_DEFAULT_SSP_INTERVAL);
1017 	cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, CDNS_DEFAULT_SSP_INTERVAL);
1018 
1019 	/* flush command FIFOs */
1020 	cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1021 		     CDNS_MCP_CONTROL_CMD_RST);
1022 
1023 	/* Set cmd accept mode */
1024 	cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT,
1025 		     CDNS_MCP_CONTROL_CMD_ACCEPT);
1026 
1027 	/* Configure mcp config */
1028 	val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1029 
1030 	/* Set Max cmd retry to 15 */
1031 	val |= CDNS_MCP_CONFIG_MCMD_RETRY;
1032 
1033 	/* Set frame delay between PREQ and ping frame to 15 frames */
1034 	val |= 0xF << SDW_REG_SHIFT(CDNS_MCP_CONFIG_MPREQ_DELAY);
1035 
1036 	/* Disable auto bus release */
1037 	val &= ~CDNS_MCP_CONFIG_BUS_REL;
1038 
1039 	/* Disable sniffer mode */
1040 	val &= ~CDNS_MCP_CONFIG_SNIFFER;
1041 
1042 	/* Set cmd mode for Tx and Rx cmds */
1043 	val &= ~CDNS_MCP_CONFIG_CMD;
1044 
1045 	cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1046 
1047 	/* commit changes */
1048 	return cdns_update_config(cdns);
1049 }
1050 EXPORT_SYMBOL(sdw_cdns_init);
1051 
1052 int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1053 {
1054 	struct sdw_master_prop *prop = &bus->prop;
1055 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1056 	int mcp_clkctrl_off;
1057 	int divider;
1058 
1059 	if (!params->curr_dr_freq) {
1060 		dev_err(cdns->dev, "NULL curr_dr_freq\n");
1061 		return -EINVAL;
1062 	}
1063 
1064 	divider	= prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1065 		params->curr_dr_freq;
1066 	divider--; /* divider is 1/(N+1) */
1067 
1068 	if (params->next_bank)
1069 		mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1070 	else
1071 		mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1072 
1073 	cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
1074 
1075 	return 0;
1076 }
1077 EXPORT_SYMBOL(cdns_bus_conf);
1078 
1079 static int cdns_port_params(struct sdw_bus *bus,
1080 			    struct sdw_port_params *p_params, unsigned int bank)
1081 {
1082 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1083 	int dpn_config = 0, dpn_config_off;
1084 
1085 	if (bank)
1086 		dpn_config_off = CDNS_DPN_B1_CONFIG(p_params->num);
1087 	else
1088 		dpn_config_off = CDNS_DPN_B0_CONFIG(p_params->num);
1089 
1090 	dpn_config = cdns_readl(cdns, dpn_config_off);
1091 
1092 	dpn_config |= ((p_params->bps - 1) <<
1093 				SDW_REG_SHIFT(CDNS_DPN_CONFIG_WL));
1094 	dpn_config |= (p_params->flow_mode <<
1095 				SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_FLOW));
1096 	dpn_config |= (p_params->data_mode <<
1097 				SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_DAT));
1098 
1099 	cdns_writel(cdns, dpn_config_off, dpn_config);
1100 
1101 	return 0;
1102 }
1103 
1104 static int cdns_transport_params(struct sdw_bus *bus,
1105 				 struct sdw_transport_params *t_params,
1106 				 enum sdw_reg_bank bank)
1107 {
1108 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1109 	int dpn_offsetctrl = 0, dpn_offsetctrl_off;
1110 	int dpn_config = 0, dpn_config_off;
1111 	int dpn_hctrl = 0, dpn_hctrl_off;
1112 	int num = t_params->port_num;
1113 	int dpn_samplectrl_off;
1114 
1115 	/*
1116 	 * Note: Only full data port is supported on the Master side for
1117 	 * both PCM and PDM ports.
1118 	 */
1119 
1120 	if (bank) {
1121 		dpn_config_off = CDNS_DPN_B1_CONFIG(num);
1122 		dpn_samplectrl_off = CDNS_DPN_B1_SAMPLE_CTRL(num);
1123 		dpn_hctrl_off = CDNS_DPN_B1_HCTRL(num);
1124 		dpn_offsetctrl_off = CDNS_DPN_B1_OFFSET_CTRL(num);
1125 	} else {
1126 		dpn_config_off = CDNS_DPN_B0_CONFIG(num);
1127 		dpn_samplectrl_off = CDNS_DPN_B0_SAMPLE_CTRL(num);
1128 		dpn_hctrl_off = CDNS_DPN_B0_HCTRL(num);
1129 		dpn_offsetctrl_off = CDNS_DPN_B0_OFFSET_CTRL(num);
1130 	}
1131 
1132 	dpn_config = cdns_readl(cdns, dpn_config_off);
1133 
1134 	dpn_config |= (t_params->blk_grp_ctrl <<
1135 				SDW_REG_SHIFT(CDNS_DPN_CONFIG_BGC));
1136 	dpn_config |= (t_params->blk_pkg_mode <<
1137 				SDW_REG_SHIFT(CDNS_DPN_CONFIG_BPM));
1138 	cdns_writel(cdns, dpn_config_off, dpn_config);
1139 
1140 	dpn_offsetctrl |= (t_params->offset1 <<
1141 				SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_1));
1142 	dpn_offsetctrl |= (t_params->offset2 <<
1143 				SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_2));
1144 	cdns_writel(cdns, dpn_offsetctrl_off,  dpn_offsetctrl);
1145 
1146 	dpn_hctrl |= (t_params->hstart <<
1147 				SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTART));
1148 	dpn_hctrl |= (t_params->hstop << SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTOP));
1149 	dpn_hctrl |= (t_params->lane_ctrl <<
1150 				SDW_REG_SHIFT(CDNS_DPN_HCTRL_LCTRL));
1151 
1152 	cdns_writel(cdns, dpn_hctrl_off, dpn_hctrl);
1153 	cdns_writel(cdns, dpn_samplectrl_off, (t_params->sample_interval - 1));
1154 
1155 	return 0;
1156 }
1157 
1158 static int cdns_port_enable(struct sdw_bus *bus,
1159 			    struct sdw_enable_ch *enable_ch, unsigned int bank)
1160 {
1161 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1162 	int dpn_chnen_off, ch_mask;
1163 
1164 	if (bank)
1165 		dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1166 	else
1167 		dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1168 
1169 	ch_mask = enable_ch->ch_mask * enable_ch->enable;
1170 	cdns_writel(cdns, dpn_chnen_off, ch_mask);
1171 
1172 	return 0;
1173 }
1174 
1175 static const struct sdw_master_port_ops cdns_port_ops = {
1176 	.dpn_set_port_params = cdns_port_params,
1177 	.dpn_set_port_transport_params = cdns_transport_params,
1178 	.dpn_port_enable_ch = cdns_port_enable,
1179 };
1180 
1181 /**
1182  * sdw_cdns_probe() - Cadence probe routine
1183  * @cdns: Cadence instance
1184  */
1185 int sdw_cdns_probe(struct sdw_cdns *cdns)
1186 {
1187 	init_completion(&cdns->tx_complete);
1188 	cdns->bus.port_ops = &cdns_port_ops;
1189 
1190 	return 0;
1191 }
1192 EXPORT_SYMBOL(sdw_cdns_probe);
1193 
1194 int cdns_set_sdw_stream(struct snd_soc_dai *dai,
1195 			void *stream, bool pcm, int direction)
1196 {
1197 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1198 	struct sdw_cdns_dma_data *dma;
1199 
1200 	dma = kzalloc(sizeof(*dma), GFP_KERNEL);
1201 	if (!dma)
1202 		return -ENOMEM;
1203 
1204 	if (pcm)
1205 		dma->stream_type = SDW_STREAM_PCM;
1206 	else
1207 		dma->stream_type = SDW_STREAM_PDM;
1208 
1209 	dma->bus = &cdns->bus;
1210 	dma->link_id = cdns->instance;
1211 
1212 	dma->stream = stream;
1213 
1214 	if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1215 		dai->playback_dma_data = dma;
1216 	else
1217 		dai->capture_dma_data = dma;
1218 
1219 	return 0;
1220 }
1221 EXPORT_SYMBOL(cdns_set_sdw_stream);
1222 
1223 /**
1224  * cdns_find_pdi() - Find a free PDI
1225  *
1226  * @cdns: Cadence instance
1227  * @num: Number of PDIs
1228  * @pdi: PDI instances
1229  *
1230  * Find a PDI for a given PDI array. The PDI num and dai_id are
1231  * expected to match, return NULL otherwise.
1232  */
1233 static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
1234 					  unsigned int offset,
1235 					  unsigned int num,
1236 					  struct sdw_cdns_pdi *pdi,
1237 					  int dai_id)
1238 {
1239 	int i;
1240 
1241 	for (i = offset; i < offset + num; i++)
1242 		if (pdi[i].num == dai_id)
1243 			return &pdi[i];
1244 
1245 	return NULL;
1246 }
1247 
1248 /**
1249  * sdw_cdns_config_stream: Configure a stream
1250  *
1251  * @cdns: Cadence instance
1252  * @ch: Channel count
1253  * @dir: Data direction
1254  * @pdi: PDI to be used
1255  */
1256 void sdw_cdns_config_stream(struct sdw_cdns *cdns,
1257 			    u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
1258 {
1259 	u32 offset, val = 0;
1260 
1261 	if (dir == SDW_DATA_DIR_RX)
1262 		val = CDNS_PORTCTRL_DIRN;
1263 
1264 	offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
1265 	cdns_updatel(cdns, offset, CDNS_PORTCTRL_DIRN, val);
1266 
1267 	val = pdi->num;
1268 	val |= ((1 << ch) - 1) << SDW_REG_SHIFT(CDNS_PDI_CONFIG_CHANNEL);
1269 	cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1270 }
1271 EXPORT_SYMBOL(sdw_cdns_config_stream);
1272 
1273 /**
1274  * sdw_cdns_alloc_pdi() - Allocate a PDI
1275  *
1276  * @cdns: Cadence instance
1277  * @stream: Stream to be allocated
1278  * @ch: Channel count
1279  * @dir: Data direction
1280  */
1281 struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1282 					struct sdw_cdns_streams *stream,
1283 					u32 ch, u32 dir, int dai_id)
1284 {
1285 	struct sdw_cdns_pdi *pdi = NULL;
1286 
1287 	if (dir == SDW_DATA_DIR_RX)
1288 		pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1289 				    dai_id);
1290 	else
1291 		pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1292 				    dai_id);
1293 
1294 	/* check if we found a PDI, else find in bi-directional */
1295 	if (!pdi)
1296 		pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd,
1297 				    dai_id);
1298 
1299 	if (pdi) {
1300 		pdi->l_ch_num = 0;
1301 		pdi->h_ch_num = ch - 1;
1302 		pdi->dir = dir;
1303 		pdi->ch_count = ch;
1304 	}
1305 
1306 	return pdi;
1307 }
1308 EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
1309 
1310 MODULE_LICENSE("Dual BSD/GPL");
1311 MODULE_DESCRIPTION("Cadence Soundwire Library");
1312