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