mm: shmem: disable interrupt when acquiring info->lock in userfaultfd_copy path
[linux/fpc-iii.git] / drivers / soundwire / cadence_master.c
blobecd357d1c63dc562c21fc04c5d76aeb56d6e59de
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
4 /*
5 * Cadence SoundWire Master module
6 * Used by Master driver
7 */
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"
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");
27 #define CDNS_MCP_CONFIG 0x0
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
39 #define CDNS_MCP_CONTROL 0x4
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)
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
58 #define CDNS_MCP_CONFIG_UPDATE 0x18
59 #define CDNS_MCP_CONFIG_UPDATE_BIT BIT(0)
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)
68 #define CDNS_MCP_STAT 0x40
70 #define CDNS_MCP_STAT_ACTIVE_BANK BIT(20)
71 #define CDNS_MCP_STAT_CLK_STOP BIT(16)
73 #define CDNS_MCP_INTSTAT 0x44
74 #define CDNS_MCP_INTMASK 0x48
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)
96 #define CDNS_MCP_INTSET 0x4C
98 #define CDNS_MCP_SLAVE_STAT 0x50
99 #define CDNS_MCP_SLAVE_STAT_MASK GENMASK(1, 0)
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
110 #define CDNS_MCP_SLAVE_INTMASK0 0x5C
111 #define CDNS_MCP_SLAVE_INTMASK1 0x60
113 #define CDNS_MCP_SLAVE_INTMASK0_MASK GENMASK(31, 0)
114 #define CDNS_MCP_SLAVE_INTMASK1_MASK GENMASK(15, 0)
116 #define CDNS_MCP_PORT_INTSTAT 0x64
117 #define CDNS_MCP_PDI_STAT 0x6C
119 #define CDNS_MCP_FIFOLEVEL 0x78
120 #define CDNS_MCP_FIFOSTAT 0x7C
121 #define CDNS_MCP_RX_FIFO_AVAIL GENMASK(5, 0)
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
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)
135 #define CDNS_MCP_CMD_READ 2
136 #define CDNS_MCP_CMD_WRITE 3
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)
142 #define CDNS_DP_SIZE 128
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))
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))
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)
164 #define CDNS_DPN_SAMPLE_CTRL_SI GENMASK(15, 0)
166 #define CDNS_DPN_OFFSET_CTRL_1 GENMASK(7, 0)
167 #define CDNS_DPN_OFFSET_CTRL_2 GENMASK(15, 8)
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)
173 #define CDNS_PORTCTRL 0x130
174 #define CDNS_PORTCTRL_DIRN BIT(7)
175 #define CDNS_PORTCTRL_BANK_INVERT BIT(8)
177 #define CDNS_PORT_OFFSET 0x80
179 #define CDNS_PDI_CONFIG(n) (0x1100 + (n) * 16)
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)
185 /* Driver defaults */
186 #define CDNS_TX_TIMEOUT 2000
188 #define CDNS_SCP_RX_FIFOLEVEL 0x2
191 * register accessor helpers
193 static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset)
195 return readl(cdns->registers + offset);
198 static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value)
200 writel(value, cdns->registers + offset);
203 static inline void cdns_updatel(struct sdw_cdns *cdns,
204 int offset, u32 mask, u32 val)
206 u32 tmp;
208 tmp = cdns_readl(cdns, offset);
209 tmp = (tmp & ~mask) | val;
210 cdns_writel(cdns, offset, tmp);
213 static int cdns_set_wait(struct sdw_cdns *cdns, int offset, u32 mask, u32 value)
215 int timeout = 10;
216 u32 reg_read;
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;
224 timeout--;
225 usleep_range(50, 100);
226 } while (timeout != 0);
228 return -ETIMEDOUT;
231 static int cdns_clear_bit(struct sdw_cdns *cdns, int offset, u32 value)
233 writel(value, cdns->registers + offset);
235 /* Wait for bit to be self cleared */
236 return cdns_set_wait(cdns, offset, value, 0);
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
243 static int cdns_config_update(struct sdw_cdns *cdns)
245 int ret;
247 if (sdw_cdns_is_clock_stop(cdns)) {
248 dev_err(cdns->dev, "Cannot program MCP_CONFIG_UPDATE in ClockStopMode\n");
249 return -EINVAL;
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");
257 return ret;
261 * debugfs
263 #ifdef CONFIG_DEBUG_FS
265 #define RD_BUF (2 * PAGE_SIZE)
267 static ssize_t cdns_sprintf(struct sdw_cdns *cdns,
268 char *buf, size_t pos, unsigned int reg)
270 return scnprintf(buf + pos, RD_BUF - pos,
271 "%4x\t%8x\n", reg, cdns_readl(cdns, reg));
274 static int cdns_reg_show(struct seq_file *s, void *data)
276 struct sdw_cdns *cdns = s->private;
277 char *buf;
278 ssize_t ret;
279 int num_ports;
280 int i, j;
282 buf = kzalloc(RD_BUF, GFP_KERNEL);
283 if (!buf)
284 return -ENOMEM;
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);
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);
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);
305 ret += scnprintf(buf + ret, RD_BUF - ret,
306 "\nDPn B0 Registers\n");
308 num_ports = cdns->num_ports;
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);
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);
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);
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);
335 ret += scnprintf(buf + ret, RD_BUF - ret,
336 "\nPDIn Config Registers\n");
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));
342 seq_printf(s, "%s", buf);
343 kfree(buf);
345 return 0;
347 DEFINE_SHOW_ATTRIBUTE(cdns_reg);
349 static int cdns_hw_reset(void *data, u64 value)
351 struct sdw_cdns *cdns = data;
352 int ret;
354 if (value != 1)
355 return -EINVAL;
357 /* Userspace changed the hardware state behind the kernel's back */
358 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
360 ret = sdw_cdns_exit_reset(cdns);
362 dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret);
364 return ret;
367 DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n");
370 * sdw_cdns_debugfs_init() - Cadence debugfs init
371 * @cdns: Cadence instance
372 * @root: debugfs root
374 void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
376 debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
378 debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
379 &cdns_hw_reset_fops);
381 EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
383 #endif /* CONFIG_DEBUG_FS */
386 * IO Calls
388 static enum sdw_command_response
389 cdns_fill_msg_resp(struct sdw_cdns *cdns,
390 struct sdw_msg *msg, int count, int offset)
392 int nack = 0, no_ack = 0;
393 int i;
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");
407 if (nack) {
408 dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
409 return SDW_CMD_FAIL;
410 } else if (no_ack) {
411 dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
412 return SDW_CMD_IGNORED;
415 /* fill response */
416 for (i = 0; i < count; i++)
417 msg->buf[i + offset] = cdns->response_buf[i] >>
418 SDW_REG_SHIFT(CDNS_MCP_RESP_RDATA);
420 return SDW_CMD_OK;
423 static enum sdw_command_response
424 _cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
425 int offset, int count, bool defer)
427 unsigned long time;
428 u32 base, i, data;
429 u16 addr;
431 /* Program the watermark level for RX FIFO */
432 if (cdns->msg_count != count) {
433 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count);
434 cdns->msg_count = count;
437 base = CDNS_MCP_CMD_BASE;
438 addr = msg->addr;
440 for (i = 0; i < count; i++) {
441 data = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR);
442 data |= cmd << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND);
443 data |= addr++ << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
445 if (msg->flags == SDW_MSG_FLAG_WRITE)
446 data |= msg->buf[i + offset];
448 data |= msg->ssp_sync << SDW_REG_SHIFT(CDNS_MCP_CMD_SSP_TAG);
449 cdns_writel(cdns, base, data);
450 base += CDNS_MCP_CMD_WORD_LEN;
453 if (defer)
454 return SDW_CMD_OK;
456 /* wait for timeout or response */
457 time = wait_for_completion_timeout(&cdns->tx_complete,
458 msecs_to_jiffies(CDNS_TX_TIMEOUT));
459 if (!time) {
460 dev_err(cdns->dev, "IO transfer timed out, cmd %d device %d addr %x len %d\n",
461 cmd, msg->dev_num, msg->addr, msg->len);
462 msg->len = 0;
463 return SDW_CMD_TIMEOUT;
466 return cdns_fill_msg_resp(cdns, msg, count, offset);
469 static enum sdw_command_response
470 cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
472 int nack = 0, no_ack = 0;
473 unsigned long time;
474 u32 data[2], base;
475 int i;
477 /* Program the watermark level for RX FIFO */
478 if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
479 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
480 cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
483 data[0] = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR);
484 data[0] |= 0x3 << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND);
485 data[1] = data[0];
487 data[0] |= SDW_SCP_ADDRPAGE1 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
488 data[1] |= SDW_SCP_ADDRPAGE2 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
490 data[0] |= msg->addr_page1;
491 data[1] |= msg->addr_page2;
493 base = CDNS_MCP_CMD_BASE;
494 cdns_writel(cdns, base, data[0]);
495 base += CDNS_MCP_CMD_WORD_LEN;
496 cdns_writel(cdns, base, data[1]);
498 time = wait_for_completion_timeout(&cdns->tx_complete,
499 msecs_to_jiffies(CDNS_TX_TIMEOUT));
500 if (!time) {
501 dev_err(cdns->dev, "SCP Msg trf timed out\n");
502 msg->len = 0;
503 return SDW_CMD_TIMEOUT;
506 /* check response the writes */
507 for (i = 0; i < 2; i++) {
508 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
509 no_ack = 1;
510 dev_err(cdns->dev, "Program SCP Ack not received\n");
511 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
512 nack = 1;
513 dev_err(cdns->dev, "Program SCP NACK received\n");
518 /* For NACK, NO ack, don't return err if we are in Broadcast mode */
519 if (nack) {
520 dev_err_ratelimited(cdns->dev,
521 "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
522 return SDW_CMD_FAIL;
523 } else if (no_ack) {
524 dev_dbg_ratelimited(cdns->dev,
525 "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
526 return SDW_CMD_IGNORED;
529 return SDW_CMD_OK;
532 static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
534 int ret;
536 if (msg->page) {
537 ret = cdns_program_scp_addr(cdns, msg);
538 if (ret) {
539 msg->len = 0;
540 return ret;
544 switch (msg->flags) {
545 case SDW_MSG_FLAG_READ:
546 *cmd = CDNS_MCP_CMD_READ;
547 break;
549 case SDW_MSG_FLAG_WRITE:
550 *cmd = CDNS_MCP_CMD_WRITE;
551 break;
553 default:
554 dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
555 return -EINVAL;
558 return 0;
561 enum sdw_command_response
562 cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
564 struct sdw_cdns *cdns = bus_to_cdns(bus);
565 int cmd = 0, ret, i;
567 ret = cdns_prep_msg(cdns, msg, &cmd);
568 if (ret)
569 return SDW_CMD_FAIL_OTHER;
571 for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
572 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
573 CDNS_MCP_CMD_LEN, false);
574 if (ret < 0)
575 goto exit;
578 if (!(msg->len % CDNS_MCP_CMD_LEN))
579 goto exit;
581 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
582 msg->len % CDNS_MCP_CMD_LEN, false);
584 exit:
585 return ret;
587 EXPORT_SYMBOL(cdns_xfer_msg);
589 enum sdw_command_response
590 cdns_xfer_msg_defer(struct sdw_bus *bus,
591 struct sdw_msg *msg, struct sdw_defer *defer)
593 struct sdw_cdns *cdns = bus_to_cdns(bus);
594 int cmd = 0, ret;
596 /* for defer only 1 message is supported */
597 if (msg->len > 1)
598 return -ENOTSUPP;
600 ret = cdns_prep_msg(cdns, msg, &cmd);
601 if (ret)
602 return SDW_CMD_FAIL_OTHER;
604 cdns->defer = defer;
605 cdns->defer->length = msg->len;
607 return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
609 EXPORT_SYMBOL(cdns_xfer_msg_defer);
611 enum sdw_command_response
612 cdns_reset_page_addr(struct sdw_bus *bus, unsigned int dev_num)
614 struct sdw_cdns *cdns = bus_to_cdns(bus);
615 struct sdw_msg msg;
617 /* Create dummy message with valid device number */
618 memset(&msg, 0, sizeof(msg));
619 msg.dev_num = dev_num;
621 return cdns_program_scp_addr(cdns, &msg);
623 EXPORT_SYMBOL(cdns_reset_page_addr);
626 * IRQ handling
629 static void cdns_read_response(struct sdw_cdns *cdns)
631 u32 num_resp, cmd_base;
632 int i;
634 num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
635 num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
637 cmd_base = CDNS_MCP_CMD_BASE;
639 for (i = 0; i < num_resp; i++) {
640 cdns->response_buf[i] = cdns_readl(cdns, cmd_base);
641 cmd_base += CDNS_MCP_CMD_WORD_LEN;
645 static int cdns_update_slave_status(struct sdw_cdns *cdns,
646 u32 slave0, u32 slave1)
648 enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
649 bool is_slave = false;
650 u64 slave;
651 u32 mask;
652 int i, set_status;
654 /* combine the two status */
655 slave = ((u64)slave1 << 32) | slave0;
656 memset(status, 0, sizeof(status));
658 for (i = 0; i <= SDW_MAX_DEVICES; i++) {
659 mask = (slave >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
660 CDNS_MCP_SLAVE_STATUS_BITS;
661 if (!mask)
662 continue;
664 is_slave = true;
665 set_status = 0;
667 if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
668 status[i] = SDW_SLAVE_RESERVED;
669 set_status++;
672 if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
673 status[i] = SDW_SLAVE_ATTACHED;
674 set_status++;
677 if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
678 status[i] = SDW_SLAVE_ALERT;
679 set_status++;
682 if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
683 status[i] = SDW_SLAVE_UNATTACHED;
684 set_status++;
687 /* first check if Slave reported multiple status */
688 if (set_status > 1) {
689 u32 val;
691 dev_warn_ratelimited(cdns->dev,
692 "Slave %d reported multiple Status: %d\n",
693 i, mask);
695 /* check latest status extracted from PING commands */
696 val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
697 val >>= (i * 2);
699 switch (val & 0x3) {
700 case 0:
701 status[i] = SDW_SLAVE_UNATTACHED;
702 break;
703 case 1:
704 status[i] = SDW_SLAVE_ATTACHED;
705 break;
706 case 2:
707 status[i] = SDW_SLAVE_ALERT;
708 break;
709 case 3:
710 default:
711 status[i] = SDW_SLAVE_RESERVED;
712 break;
715 dev_warn_ratelimited(cdns->dev,
716 "Slave %d status updated to %d\n",
717 i, status[i]);
722 if (is_slave)
723 return sdw_handle_slave_status(&cdns->bus, status);
725 return 0;
729 * sdw_cdns_irq() - Cadence interrupt handler
730 * @irq: irq number
731 * @dev_id: irq context
733 irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
735 struct sdw_cdns *cdns = dev_id;
736 u32 int_status;
737 int ret = IRQ_HANDLED;
739 /* Check if the link is up */
740 if (!cdns->link_up)
741 return IRQ_NONE;
743 int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
745 /* check for reserved values read as zero */
746 if (int_status & CDNS_MCP_INT_RESERVED)
747 return IRQ_NONE;
749 if (!(int_status & CDNS_MCP_INT_IRQ))
750 return IRQ_NONE;
752 if (int_status & CDNS_MCP_INT_RX_WL) {
753 cdns_read_response(cdns);
755 if (cdns->defer) {
756 cdns_fill_msg_resp(cdns, cdns->defer->msg,
757 cdns->defer->length, 0);
758 complete(&cdns->defer->complete);
759 cdns->defer = NULL;
760 } else {
761 complete(&cdns->tx_complete);
765 if (int_status & CDNS_MCP_INT_PARITY) {
766 /* Parity error detected by Master */
767 dev_err_ratelimited(cdns->dev, "Parity error\n");
770 if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
771 /* Slave is driving bit slot during control word */
772 dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
775 if (int_status & CDNS_MCP_INT_DATA_CLASH) {
777 * Multiple slaves trying to drive bit slot, or issue with
778 * ownership of data bits or Slave gone bonkers
780 dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
783 if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
784 /* Mask the Slave interrupt and wake thread */
785 cdns_updatel(cdns, CDNS_MCP_INTMASK,
786 CDNS_MCP_INT_SLAVE_MASK, 0);
788 int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
789 ret = IRQ_WAKE_THREAD;
792 cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
793 return ret;
795 EXPORT_SYMBOL(sdw_cdns_irq);
798 * sdw_cdns_thread() - Cadence irq thread handler
799 * @irq: irq number
800 * @dev_id: irq context
802 irqreturn_t sdw_cdns_thread(int irq, void *dev_id)
804 struct sdw_cdns *cdns = dev_id;
805 u32 slave0, slave1;
807 dev_dbg_ratelimited(cdns->dev, "Slave status change\n");
809 slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
810 slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
812 cdns_update_slave_status(cdns, slave0, slave1);
813 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
814 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
816 /* clear and unmask Slave interrupt now */
817 cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
818 cdns_updatel(cdns, CDNS_MCP_INTMASK,
819 CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
821 return IRQ_HANDLED;
823 EXPORT_SYMBOL(sdw_cdns_thread);
826 * init routines
830 * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
831 * @cdns: Cadence instance
833 int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
835 /* program maximum length reset to be safe */
836 cdns_updatel(cdns, CDNS_MCP_CONTROL,
837 CDNS_MCP_CONTROL_RST_DELAY,
838 CDNS_MCP_CONTROL_RST_DELAY);
840 /* use hardware generated reset */
841 cdns_updatel(cdns, CDNS_MCP_CONTROL,
842 CDNS_MCP_CONTROL_HW_RST,
843 CDNS_MCP_CONTROL_HW_RST);
845 /* commit changes */
846 cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
847 CDNS_MCP_CONFIG_UPDATE_BIT,
848 CDNS_MCP_CONFIG_UPDATE_BIT);
850 /* don't wait here */
851 return 0;
854 EXPORT_SYMBOL(sdw_cdns_exit_reset);
857 * sdw_cdns_enable_slave_interrupt() - Enable SDW slave interrupts
858 * @cdns: Cadence instance
859 * @state: boolean for true/false
861 static void cdns_enable_slave_interrupts(struct sdw_cdns *cdns, bool state)
863 u32 mask;
865 mask = cdns_readl(cdns, CDNS_MCP_INTMASK);
866 if (state)
867 mask |= CDNS_MCP_INT_SLAVE_MASK;
868 else
869 mask &= ~CDNS_MCP_INT_SLAVE_MASK;
871 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
875 * sdw_cdns_enable_interrupt() - Enable SDW interrupts
876 * @cdns: Cadence instance
877 * @state: True if we are trying to enable interrupt.
879 int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
881 u32 slave_intmask0 = 0;
882 u32 slave_intmask1 = 0;
883 u32 mask = 0;
885 if (!state)
886 goto update_masks;
888 slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
889 slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
891 /* enable detection of all slave state changes */
892 mask = CDNS_MCP_INT_SLAVE_MASK;
894 /* enable detection of bus issues */
895 mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
896 CDNS_MCP_INT_PARITY;
898 /* no detection of port interrupts for now */
900 /* enable detection of RX fifo level */
901 mask |= CDNS_MCP_INT_RX_WL;
904 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
905 * settings are irrelevant
907 mask |= CDNS_MCP_INT_IRQ;
909 if (interrupt_mask) /* parameter override */
910 mask = interrupt_mask;
912 update_masks:
913 /* clear slave interrupt status before enabling interrupt */
914 if (state) {
915 u32 slave_state;
917 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
918 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state);
919 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
920 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state);
923 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
924 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
925 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
927 return 0;
929 EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
931 static int cdns_allocate_pdi(struct sdw_cdns *cdns,
932 struct sdw_cdns_pdi **stream,
933 u32 num, u32 pdi_offset)
935 struct sdw_cdns_pdi *pdi;
936 int i;
938 if (!num)
939 return 0;
941 pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
942 if (!pdi)
943 return -ENOMEM;
945 for (i = 0; i < num; i++) {
946 pdi[i].num = i + pdi_offset;
949 *stream = pdi;
950 return 0;
954 * sdw_cdns_pdi_init() - PDI initialization routine
956 * @cdns: Cadence instance
957 * @config: Stream configurations
959 int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
960 struct sdw_cdns_stream_config config)
962 struct sdw_cdns_streams *stream;
963 int offset;
964 int ret;
966 cdns->pcm.num_bd = config.pcm_bd;
967 cdns->pcm.num_in = config.pcm_in;
968 cdns->pcm.num_out = config.pcm_out;
969 cdns->pdm.num_bd = config.pdm_bd;
970 cdns->pdm.num_in = config.pdm_in;
971 cdns->pdm.num_out = config.pdm_out;
973 /* Allocate PDIs for PCMs */
974 stream = &cdns->pcm;
976 /* we allocate PDI0 and PDI1 which are used for Bulk */
977 offset = 0;
979 ret = cdns_allocate_pdi(cdns, &stream->bd,
980 stream->num_bd, offset);
981 if (ret)
982 return ret;
984 offset += stream->num_bd;
986 ret = cdns_allocate_pdi(cdns, &stream->in,
987 stream->num_in, offset);
988 if (ret)
989 return ret;
991 offset += stream->num_in;
993 ret = cdns_allocate_pdi(cdns, &stream->out,
994 stream->num_out, offset);
995 if (ret)
996 return ret;
998 /* Update total number of PCM PDIs */
999 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1000 cdns->num_ports = stream->num_pdi;
1002 /* Allocate PDIs for PDMs */
1003 stream = &cdns->pdm;
1004 ret = cdns_allocate_pdi(cdns, &stream->bd,
1005 stream->num_bd, offset);
1006 if (ret)
1007 return ret;
1009 offset += stream->num_bd;
1011 ret = cdns_allocate_pdi(cdns, &stream->in,
1012 stream->num_in, offset);
1013 if (ret)
1014 return ret;
1016 offset += stream->num_in;
1018 ret = cdns_allocate_pdi(cdns, &stream->out,
1019 stream->num_out, offset);
1021 if (ret)
1022 return ret;
1024 /* Update total number of PDM PDIs */
1025 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1026 cdns->num_ports += stream->num_pdi;
1028 return 0;
1030 EXPORT_SYMBOL(sdw_cdns_pdi_init);
1032 static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
1034 u32 val;
1035 int c;
1036 int r;
1038 r = sdw_find_row_index(n_rows);
1039 c = sdw_find_col_index(n_cols) & CDNS_MCP_FRAME_SHAPE_COL_MASK;
1041 val = (r << CDNS_MCP_FRAME_SHAPE_ROW_OFFSET) | c;
1043 return val;
1046 static void cdns_init_clock_ctrl(struct sdw_cdns *cdns)
1048 struct sdw_bus *bus = &cdns->bus;
1049 struct sdw_master_prop *prop = &bus->prop;
1050 u32 val;
1051 u32 ssp_interval;
1052 int divider;
1054 /* Set clock divider */
1055 divider = (prop->mclk_freq / prop->max_clk_freq) - 1;
1057 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1058 CDNS_MCP_CLK_MCLKD_MASK, divider);
1059 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1060 CDNS_MCP_CLK_MCLKD_MASK, divider);
1063 * Frame shape changes after initialization have to be done
1064 * with the bank switch mechanism
1066 val = cdns_set_initial_frame_shape(prop->default_row,
1067 prop->default_col);
1068 cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
1070 /* Set SSP interval to default value */
1071 ssp_interval = prop->default_frame_rate / SDW_CADENCE_GSYNC_HZ;
1072 cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, ssp_interval);
1073 cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, ssp_interval);
1077 * sdw_cdns_init() - Cadence initialization
1078 * @cdns: Cadence instance
1080 int sdw_cdns_init(struct sdw_cdns *cdns)
1082 u32 val;
1084 cdns_init_clock_ctrl(cdns);
1086 /* reset msg_count to default value of FIFOLEVEL */
1087 cdns->msg_count = cdns_readl(cdns, CDNS_MCP_FIFOLEVEL);
1089 /* flush command FIFOs */
1090 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1091 CDNS_MCP_CONTROL_CMD_RST);
1093 /* Set cmd accept mode */
1094 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT,
1095 CDNS_MCP_CONTROL_CMD_ACCEPT);
1097 /* Configure mcp config */
1098 val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1100 /* enable bus operations with clock and data */
1101 val &= ~CDNS_MCP_CONFIG_OP;
1102 val |= CDNS_MCP_CONFIG_OP_NORMAL;
1104 /* Set cmd mode for Tx and Rx cmds */
1105 val &= ~CDNS_MCP_CONFIG_CMD;
1107 /* Disable sniffer mode */
1108 val &= ~CDNS_MCP_CONFIG_SNIFFER;
1110 /* Disable auto bus release */
1111 val &= ~CDNS_MCP_CONFIG_BUS_REL;
1113 if (cdns->bus.multi_link)
1114 /* Set Multi-master mode to take gsync into account */
1115 val |= CDNS_MCP_CONFIG_MMASTER;
1117 /* leave frame delay to hardware default of 0x1F */
1119 /* leave command retry to hardware default of 0 */
1121 cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1123 /* changes will be committed later */
1124 return 0;
1126 EXPORT_SYMBOL(sdw_cdns_init);
1128 int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1130 struct sdw_master_prop *prop = &bus->prop;
1131 struct sdw_cdns *cdns = bus_to_cdns(bus);
1132 int mcp_clkctrl_off;
1133 int divider;
1135 if (!params->curr_dr_freq) {
1136 dev_err(cdns->dev, "NULL curr_dr_freq\n");
1137 return -EINVAL;
1140 divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1141 params->curr_dr_freq;
1142 divider--; /* divider is 1/(N+1) */
1144 if (params->next_bank)
1145 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1146 else
1147 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1149 cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
1151 return 0;
1153 EXPORT_SYMBOL(cdns_bus_conf);
1155 static int cdns_port_params(struct sdw_bus *bus,
1156 struct sdw_port_params *p_params, unsigned int bank)
1158 struct sdw_cdns *cdns = bus_to_cdns(bus);
1159 int dpn_config = 0, dpn_config_off;
1161 if (bank)
1162 dpn_config_off = CDNS_DPN_B1_CONFIG(p_params->num);
1163 else
1164 dpn_config_off = CDNS_DPN_B0_CONFIG(p_params->num);
1166 dpn_config = cdns_readl(cdns, dpn_config_off);
1168 dpn_config |= ((p_params->bps - 1) <<
1169 SDW_REG_SHIFT(CDNS_DPN_CONFIG_WL));
1170 dpn_config |= (p_params->flow_mode <<
1171 SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_FLOW));
1172 dpn_config |= (p_params->data_mode <<
1173 SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_DAT));
1175 cdns_writel(cdns, dpn_config_off, dpn_config);
1177 return 0;
1180 static int cdns_transport_params(struct sdw_bus *bus,
1181 struct sdw_transport_params *t_params,
1182 enum sdw_reg_bank bank)
1184 struct sdw_cdns *cdns = bus_to_cdns(bus);
1185 int dpn_offsetctrl = 0, dpn_offsetctrl_off;
1186 int dpn_config = 0, dpn_config_off;
1187 int dpn_hctrl = 0, dpn_hctrl_off;
1188 int num = t_params->port_num;
1189 int dpn_samplectrl_off;
1192 * Note: Only full data port is supported on the Master side for
1193 * both PCM and PDM ports.
1196 if (bank) {
1197 dpn_config_off = CDNS_DPN_B1_CONFIG(num);
1198 dpn_samplectrl_off = CDNS_DPN_B1_SAMPLE_CTRL(num);
1199 dpn_hctrl_off = CDNS_DPN_B1_HCTRL(num);
1200 dpn_offsetctrl_off = CDNS_DPN_B1_OFFSET_CTRL(num);
1201 } else {
1202 dpn_config_off = CDNS_DPN_B0_CONFIG(num);
1203 dpn_samplectrl_off = CDNS_DPN_B0_SAMPLE_CTRL(num);
1204 dpn_hctrl_off = CDNS_DPN_B0_HCTRL(num);
1205 dpn_offsetctrl_off = CDNS_DPN_B0_OFFSET_CTRL(num);
1208 dpn_config = cdns_readl(cdns, dpn_config_off);
1210 dpn_config |= (t_params->blk_grp_ctrl <<
1211 SDW_REG_SHIFT(CDNS_DPN_CONFIG_BGC));
1212 dpn_config |= (t_params->blk_pkg_mode <<
1213 SDW_REG_SHIFT(CDNS_DPN_CONFIG_BPM));
1214 cdns_writel(cdns, dpn_config_off, dpn_config);
1216 dpn_offsetctrl |= (t_params->offset1 <<
1217 SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_1));
1218 dpn_offsetctrl |= (t_params->offset2 <<
1219 SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_2));
1220 cdns_writel(cdns, dpn_offsetctrl_off, dpn_offsetctrl);
1222 dpn_hctrl |= (t_params->hstart <<
1223 SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTART));
1224 dpn_hctrl |= (t_params->hstop << SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTOP));
1225 dpn_hctrl |= (t_params->lane_ctrl <<
1226 SDW_REG_SHIFT(CDNS_DPN_HCTRL_LCTRL));
1228 cdns_writel(cdns, dpn_hctrl_off, dpn_hctrl);
1229 cdns_writel(cdns, dpn_samplectrl_off, (t_params->sample_interval - 1));
1231 return 0;
1234 static int cdns_port_enable(struct sdw_bus *bus,
1235 struct sdw_enable_ch *enable_ch, unsigned int bank)
1237 struct sdw_cdns *cdns = bus_to_cdns(bus);
1238 int dpn_chnen_off, ch_mask;
1240 if (bank)
1241 dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1242 else
1243 dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1245 ch_mask = enable_ch->ch_mask * enable_ch->enable;
1246 cdns_writel(cdns, dpn_chnen_off, ch_mask);
1248 return 0;
1251 static const struct sdw_master_port_ops cdns_port_ops = {
1252 .dpn_set_port_params = cdns_port_params,
1253 .dpn_set_port_transport_params = cdns_transport_params,
1254 .dpn_port_enable_ch = cdns_port_enable,
1258 * sdw_cdns_is_clock_stop: Check clock status
1260 * @cdns: Cadence instance
1262 bool sdw_cdns_is_clock_stop(struct sdw_cdns *cdns)
1264 return !!(cdns_readl(cdns, CDNS_MCP_STAT) & CDNS_MCP_STAT_CLK_STOP);
1266 EXPORT_SYMBOL(sdw_cdns_is_clock_stop);
1269 * sdw_cdns_clock_stop: Cadence clock stop configuration routine
1271 * @cdns: Cadence instance
1272 * @block_wake: prevent wakes if required by the platform
1274 int sdw_cdns_clock_stop(struct sdw_cdns *cdns, bool block_wake)
1276 bool slave_present = false;
1277 struct sdw_slave *slave;
1278 int ret;
1280 /* Check suspend status */
1281 if (sdw_cdns_is_clock_stop(cdns)) {
1282 dev_dbg(cdns->dev, "Clock is already stopped\n");
1283 return 0;
1287 * Before entering clock stop we mask the Slave
1288 * interrupts. This helps avoid having to deal with e.g. a
1289 * Slave becoming UNATTACHED while the clock is being stopped
1291 cdns_enable_slave_interrupts(cdns, false);
1294 * For specific platforms, it is required to be able to put
1295 * master into a state in which it ignores wake-up trials
1296 * in clock stop state
1298 if (block_wake)
1299 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1300 CDNS_MCP_CONTROL_BLOCK_WAKEUP,
1301 CDNS_MCP_CONTROL_BLOCK_WAKEUP);
1303 list_for_each_entry(slave, &cdns->bus.slaves, node) {
1304 if (slave->status == SDW_SLAVE_ATTACHED ||
1305 slave->status == SDW_SLAVE_ALERT) {
1306 slave_present = true;
1307 break;
1312 * This CMD_ACCEPT should be used when there are no devices
1313 * attached on the link when entering clock stop mode. If this is
1314 * not set and there is a broadcast write then the command ignored
1315 * will be treated as a failure
1317 if (!slave_present)
1318 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1319 CDNS_MCP_CONTROL_CMD_ACCEPT,
1320 CDNS_MCP_CONTROL_CMD_ACCEPT);
1321 else
1322 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1323 CDNS_MCP_CONTROL_CMD_ACCEPT, 0);
1325 /* commit changes */
1326 ret = cdns_config_update(cdns);
1327 if (ret < 0) {
1328 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1329 return ret;
1332 /* Prepare slaves for clock stop */
1333 ret = sdw_bus_prep_clk_stop(&cdns->bus);
1334 if (ret < 0) {
1335 dev_err(cdns->dev, "prepare clock stop failed %d", ret);
1336 return ret;
1340 * Enter clock stop mode and only report errors if there are
1341 * Slave devices present (ALERT or ATTACHED)
1343 ret = sdw_bus_clk_stop(&cdns->bus);
1344 if (ret < 0 && slave_present && ret != -ENODATA) {
1345 dev_err(cdns->dev, "bus clock stop failed %d", ret);
1346 return ret;
1349 ret = cdns_set_wait(cdns, CDNS_MCP_STAT,
1350 CDNS_MCP_STAT_CLK_STOP,
1351 CDNS_MCP_STAT_CLK_STOP);
1352 if (ret < 0)
1353 dev_err(cdns->dev, "Clock stop failed %d\n", ret);
1355 return ret;
1357 EXPORT_SYMBOL(sdw_cdns_clock_stop);
1360 * sdw_cdns_clock_restart: Cadence PM clock restart configuration routine
1362 * @cdns: Cadence instance
1363 * @bus_reset: context may be lost while in low power modes and the bus
1364 * may require a Severe Reset and re-enumeration after a wake.
1366 int sdw_cdns_clock_restart(struct sdw_cdns *cdns, bool bus_reset)
1368 int ret;
1370 /* unmask Slave interrupts that were masked when stopping the clock */
1371 cdns_enable_slave_interrupts(cdns, true);
1373 ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
1374 CDNS_MCP_CONTROL_CLK_STOP_CLR);
1375 if (ret < 0) {
1376 dev_err(cdns->dev, "Couldn't exit from clock stop\n");
1377 return ret;
1380 ret = cdns_set_wait(cdns, CDNS_MCP_STAT, CDNS_MCP_STAT_CLK_STOP, 0);
1381 if (ret < 0) {
1382 dev_err(cdns->dev, "clock stop exit failed %d\n", ret);
1383 return ret;
1386 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1387 CDNS_MCP_CONTROL_BLOCK_WAKEUP, 0);
1390 * clear CMD_ACCEPT so that the command ignored
1391 * will be treated as a failure during a broadcast write
1393 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT, 0);
1395 if (!bus_reset) {
1397 /* enable bus operations with clock and data */
1398 cdns_updatel(cdns, CDNS_MCP_CONFIG,
1399 CDNS_MCP_CONFIG_OP,
1400 CDNS_MCP_CONFIG_OP_NORMAL);
1402 ret = cdns_config_update(cdns);
1403 if (ret < 0) {
1404 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1405 return ret;
1408 ret = sdw_bus_exit_clk_stop(&cdns->bus);
1409 if (ret < 0)
1410 dev_err(cdns->dev, "bus failed to exit clock stop %d\n", ret);
1413 return ret;
1415 EXPORT_SYMBOL(sdw_cdns_clock_restart);
1418 * sdw_cdns_probe() - Cadence probe routine
1419 * @cdns: Cadence instance
1421 int sdw_cdns_probe(struct sdw_cdns *cdns)
1423 init_completion(&cdns->tx_complete);
1424 cdns->bus.port_ops = &cdns_port_ops;
1426 return 0;
1428 EXPORT_SYMBOL(sdw_cdns_probe);
1430 int cdns_set_sdw_stream(struct snd_soc_dai *dai,
1431 void *stream, bool pcm, int direction)
1433 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1434 struct sdw_cdns_dma_data *dma;
1436 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
1437 if (!dma)
1438 return -ENOMEM;
1440 if (pcm)
1441 dma->stream_type = SDW_STREAM_PCM;
1442 else
1443 dma->stream_type = SDW_STREAM_PDM;
1445 dma->bus = &cdns->bus;
1446 dma->link_id = cdns->instance;
1448 dma->stream = stream;
1450 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1451 dai->playback_dma_data = dma;
1452 else
1453 dai->capture_dma_data = dma;
1455 return 0;
1457 EXPORT_SYMBOL(cdns_set_sdw_stream);
1460 * cdns_find_pdi() - Find a free PDI
1462 * @cdns: Cadence instance
1463 * @offset: Starting offset
1464 * @num: Number of PDIs
1465 * @pdi: PDI instances
1466 * @dai_id: DAI id
1468 * Find a PDI for a given PDI array. The PDI num and dai_id are
1469 * expected to match, return NULL otherwise.
1471 static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
1472 unsigned int offset,
1473 unsigned int num,
1474 struct sdw_cdns_pdi *pdi,
1475 int dai_id)
1477 int i;
1479 for (i = offset; i < offset + num; i++)
1480 if (pdi[i].num == dai_id)
1481 return &pdi[i];
1483 return NULL;
1487 * sdw_cdns_config_stream: Configure a stream
1489 * @cdns: Cadence instance
1490 * @ch: Channel count
1491 * @dir: Data direction
1492 * @pdi: PDI to be used
1494 void sdw_cdns_config_stream(struct sdw_cdns *cdns,
1495 u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
1497 u32 offset, val = 0;
1499 if (dir == SDW_DATA_DIR_RX)
1500 val = CDNS_PORTCTRL_DIRN;
1502 offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
1503 cdns_updatel(cdns, offset, CDNS_PORTCTRL_DIRN, val);
1505 val = pdi->num;
1506 val |= CDNS_PDI_CONFIG_SOFT_RESET;
1507 val |= ((1 << ch) - 1) << SDW_REG_SHIFT(CDNS_PDI_CONFIG_CHANNEL);
1508 cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1510 EXPORT_SYMBOL(sdw_cdns_config_stream);
1513 * sdw_cdns_alloc_pdi() - Allocate a PDI
1515 * @cdns: Cadence instance
1516 * @stream: Stream to be allocated
1517 * @ch: Channel count
1518 * @dir: Data direction
1519 * @dai_id: DAI id
1521 struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1522 struct sdw_cdns_streams *stream,
1523 u32 ch, u32 dir, int dai_id)
1525 struct sdw_cdns_pdi *pdi = NULL;
1527 if (dir == SDW_DATA_DIR_RX)
1528 pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1529 dai_id);
1530 else
1531 pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1532 dai_id);
1534 /* check if we found a PDI, else find in bi-directional */
1535 if (!pdi)
1536 pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd,
1537 dai_id);
1539 if (pdi) {
1540 pdi->l_ch_num = 0;
1541 pdi->h_ch_num = ch - 1;
1542 pdi->dir = dir;
1543 pdi->ch_count = ch;
1546 return pdi;
1548 EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
1550 MODULE_LICENSE("Dual BSD/GPL");
1551 MODULE_DESCRIPTION("Cadence Soundwire Library");