dm writecache: fix incorrect flush sequence when doing SSD mode commit
[linux/fpc-iii.git] / drivers / soundwire / cadence_master.c
blobfed21e2b22774de380d464499ae349b20dbd690e
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_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)
93 #define CDNS_MCP_INTSET 0x4C
95 #define CDNS_MCP_SLAVE_STAT 0x50
96 #define CDNS_MCP_SLAVE_STAT_MASK GENMASK(1, 0)
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
107 #define CDNS_MCP_SLAVE_INTMASK0 0x5C
108 #define CDNS_MCP_SLAVE_INTMASK1 0x60
110 #define CDNS_MCP_SLAVE_INTMASK0_MASK GENMASK(31, 0)
111 #define CDNS_MCP_SLAVE_INTMASK1_MASK GENMASK(15, 0)
113 #define CDNS_MCP_PORT_INTSTAT 0x64
114 #define CDNS_MCP_PDI_STAT 0x6C
116 #define CDNS_MCP_FIFOLEVEL 0x78
117 #define CDNS_MCP_FIFOSTAT 0x7C
118 #define CDNS_MCP_RX_FIFO_AVAIL GENMASK(5, 0)
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
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)
132 #define CDNS_MCP_CMD_READ 2
133 #define CDNS_MCP_CMD_WRITE 3
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)
139 #define CDNS_DP_SIZE 128
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))
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))
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)
161 #define CDNS_DPN_SAMPLE_CTRL_SI GENMASK(15, 0)
163 #define CDNS_DPN_OFFSET_CTRL_1 GENMASK(7, 0)
164 #define CDNS_DPN_OFFSET_CTRL_2 GENMASK(15, 8)
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)
170 #define CDNS_PORTCTRL 0x130
171 #define CDNS_PORTCTRL_DIRN BIT(7)
172 #define CDNS_PORTCTRL_BANK_INVERT BIT(8)
174 #define CDNS_PORT_OFFSET 0x80
176 #define CDNS_PDI_CONFIG(n) (0x1100 + (n) * 16)
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)
182 /* Driver defaults */
183 #define CDNS_DEFAULT_SSP_INTERVAL 0x18
184 #define CDNS_TX_TIMEOUT 2000
186 #define CDNS_SCP_RX_FIFOLEVEL 0x2
189 * register accessor helpers
191 static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset)
193 return readl(cdns->registers + offset);
196 static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value)
198 writel(value, cdns->registers + offset);
201 static inline void cdns_updatel(struct sdw_cdns *cdns,
202 int offset, u32 mask, u32 val)
204 u32 tmp;
206 tmp = cdns_readl(cdns, offset);
207 tmp = (tmp & ~mask) | val;
208 cdns_writel(cdns, offset, tmp);
211 static int cdns_clear_bit(struct sdw_cdns *cdns, int offset, u32 value)
213 int timeout = 10;
214 u32 reg_read;
216 writel(value, cdns->registers + offset);
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;
224 timeout--;
225 udelay(50);
226 } while (timeout != 0);
228 return -EAGAIN;
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
235 static int cdns_update_config(struct sdw_cdns *cdns)
237 int ret;
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");
244 return ret;
248 * debugfs
250 #ifdef CONFIG_DEBUG_FS
252 #define RD_BUF (2 * PAGE_SIZE)
254 static ssize_t cdns_sprintf(struct sdw_cdns *cdns,
255 char *buf, size_t pos, unsigned int reg)
257 return scnprintf(buf + pos, RD_BUF - pos,
258 "%4x\t%8x\n", reg, cdns_readl(cdns, reg));
261 static int cdns_reg_show(struct seq_file *s, void *data)
263 struct sdw_cdns *cdns = s->private;
264 char *buf;
265 ssize_t ret;
266 int num_ports;
267 int i, j;
269 buf = kzalloc(RD_BUF, GFP_KERNEL);
270 if (!buf)
271 return -ENOMEM;
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);
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);
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);
292 ret += scnprintf(buf + ret, RD_BUF - ret,
293 "\nDPn B0 Registers\n");
295 num_ports = cdns->num_ports;
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);
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);
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);
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);
322 ret += scnprintf(buf + ret, RD_BUF - ret,
323 "\nPDIn Config Registers\n");
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));
329 seq_printf(s, "%s", buf);
330 kfree(buf);
332 return 0;
334 DEFINE_SHOW_ATTRIBUTE(cdns_reg);
336 static int cdns_hw_reset(void *data, u64 value)
338 struct sdw_cdns *cdns = data;
339 int ret;
341 if (value != 1)
342 return -EINVAL;
344 /* Userspace changed the hardware state behind the kernel's back */
345 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
347 ret = sdw_cdns_exit_reset(cdns);
349 dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret);
351 return ret;
354 DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n");
357 * sdw_cdns_debugfs_init() - Cadence debugfs init
358 * @cdns: Cadence instance
359 * @root: debugfs root
361 void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
363 debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
365 debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
366 &cdns_hw_reset_fops);
368 EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
370 #endif /* CONFIG_DEBUG_FS */
373 * IO Calls
375 static enum sdw_command_response
376 cdns_fill_msg_resp(struct sdw_cdns *cdns,
377 struct sdw_msg *msg, int count, int offset)
379 int nack = 0, no_ack = 0;
380 int i;
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");
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;
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);
407 return SDW_CMD_OK;
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)
414 unsigned long time;
415 u32 base, i, data;
416 u16 addr;
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;
424 base = CDNS_MCP_CMD_BASE;
425 addr = msg->addr;
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);
432 if (msg->flags == SDW_MSG_FLAG_WRITE)
433 data |= msg->buf[i + offset];
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;
440 if (defer)
441 return SDW_CMD_OK;
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;
452 return cdns_fill_msg_resp(cdns, msg, count, offset);
455 static enum sdw_command_response
456 cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
458 int nack = 0, no_ack = 0;
459 unsigned long time;
460 u32 data[2], base;
461 int i;
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;
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];
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);
476 data[0] |= msg->addr_page1;
477 data[1] |= msg->addr_page2;
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]);
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;
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");
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;
515 return SDW_CMD_OK;
518 static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
520 int ret;
522 if (msg->page) {
523 ret = cdns_program_scp_addr(cdns, msg);
524 if (ret) {
525 msg->len = 0;
526 return ret;
530 switch (msg->flags) {
531 case SDW_MSG_FLAG_READ:
532 *cmd = CDNS_MCP_CMD_READ;
533 break;
535 case SDW_MSG_FLAG_WRITE:
536 *cmd = CDNS_MCP_CMD_WRITE;
537 break;
539 default:
540 dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
541 return -EINVAL;
544 return 0;
547 enum sdw_command_response
548 cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
550 struct sdw_cdns *cdns = bus_to_cdns(bus);
551 int cmd = 0, ret, i;
553 ret = cdns_prep_msg(cdns, msg, &cmd);
554 if (ret)
555 return SDW_CMD_FAIL_OTHER;
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;
564 if (!(msg->len % CDNS_MCP_CMD_LEN))
565 goto exit;
567 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
568 msg->len % CDNS_MCP_CMD_LEN, false);
570 exit:
571 return ret;
573 EXPORT_SYMBOL(cdns_xfer_msg);
575 enum sdw_command_response
576 cdns_xfer_msg_defer(struct sdw_bus *bus,
577 struct sdw_msg *msg, struct sdw_defer *defer)
579 struct sdw_cdns *cdns = bus_to_cdns(bus);
580 int cmd = 0, ret;
582 /* for defer only 1 message is supported */
583 if (msg->len > 1)
584 return -ENOTSUPP;
586 ret = cdns_prep_msg(cdns, msg, &cmd);
587 if (ret)
588 return SDW_CMD_FAIL_OTHER;
590 cdns->defer = defer;
591 cdns->defer->length = msg->len;
593 return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
595 EXPORT_SYMBOL(cdns_xfer_msg_defer);
597 enum sdw_command_response
598 cdns_reset_page_addr(struct sdw_bus *bus, unsigned int dev_num)
600 struct sdw_cdns *cdns = bus_to_cdns(bus);
601 struct sdw_msg msg;
603 /* Create dummy message with valid device number */
604 memset(&msg, 0, sizeof(msg));
605 msg.dev_num = dev_num;
607 return cdns_program_scp_addr(cdns, &msg);
609 EXPORT_SYMBOL(cdns_reset_page_addr);
612 * IRQ handling
615 static void cdns_read_response(struct sdw_cdns *cdns)
617 u32 num_resp, cmd_base;
618 int i;
620 num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
621 num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
623 cmd_base = CDNS_MCP_CMD_BASE;
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;
631 static int cdns_update_slave_status(struct sdw_cdns *cdns,
632 u32 slave0, u32 slave1)
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;
640 /* combine the two status */
641 slave = ((u64)slave1 << 32) | slave0;
642 memset(status, 0, sizeof(status));
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;
650 is_slave = true;
651 set_status = 0;
653 if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
654 status[i] = SDW_SLAVE_RESERVED;
655 set_status++;
658 if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
659 status[i] = SDW_SLAVE_ATTACHED;
660 set_status++;
663 if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
664 status[i] = SDW_SLAVE_ALERT;
665 set_status++;
668 if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
669 status[i] = SDW_SLAVE_UNATTACHED;
670 set_status++;
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);
679 * TODO: we need to reread the status here by
680 * issuing a PING cmd
685 if (is_slave)
686 return sdw_handle_slave_status(&cdns->bus, status);
688 return 0;
692 * sdw_cdns_irq() - Cadence interrupt handler
693 * @irq: irq number
694 * @dev_id: irq context
696 irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
698 struct sdw_cdns *cdns = dev_id;
699 u32 int_status;
700 int ret = IRQ_HANDLED;
702 /* Check if the link is up */
703 if (!cdns->link_up)
704 return IRQ_NONE;
706 int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
708 if (!(int_status & CDNS_MCP_INT_IRQ))
709 return IRQ_NONE;
711 if (int_status & CDNS_MCP_INT_RX_WL) {
712 cdns_read_response(cdns);
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);
724 if (int_status & CDNS_MCP_INT_PARITY) {
725 /* Parity error detected by Master */
726 dev_err_ratelimited(cdns->dev, "Parity error\n");
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");
734 if (int_status & CDNS_MCP_INT_DATA_CLASH) {
736 * Multiple slaves trying to drive bit slot, or issue with
737 * ownership of data bits or Slave gone bonkers
739 dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
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);
747 int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
748 ret = IRQ_WAKE_THREAD;
751 cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
752 return ret;
754 EXPORT_SYMBOL(sdw_cdns_irq);
757 * sdw_cdns_thread() - Cadence irq thread handler
758 * @irq: irq number
759 * @dev_id: irq context
761 irqreturn_t sdw_cdns_thread(int irq, void *dev_id)
763 struct sdw_cdns *cdns = dev_id;
764 u32 slave0, slave1;
766 dev_dbg_ratelimited(cdns->dev, "Slave status change\n");
768 slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
769 slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
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);
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);
780 return IRQ_HANDLED;
782 EXPORT_SYMBOL(sdw_cdns_thread);
785 * init routines
789 * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
790 * @cdns: Cadence instance
792 int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
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);
799 /* use hardware generated reset */
800 cdns_updatel(cdns, CDNS_MCP_CONTROL,
801 CDNS_MCP_CONTROL_HW_RST,
802 CDNS_MCP_CONTROL_HW_RST);
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);
809 /* commit changes */
810 return cdns_update_config(cdns);
812 EXPORT_SYMBOL(sdw_cdns_exit_reset);
815 * sdw_cdns_enable_interrupt() - Enable SDW interrupts and update config
816 * @cdns: Cadence instance
818 int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
820 u32 slave_intmask0 = 0;
821 u32 slave_intmask1 = 0;
822 u32 mask = 0;
824 if (!state)
825 goto update_masks;
827 slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
828 slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
830 /* enable detection of all slave state changes */
831 mask = CDNS_MCP_INT_SLAVE_MASK;
833 /* enable detection of bus issues */
834 mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
835 CDNS_MCP_INT_PARITY;
837 /* no detection of port interrupts for now */
839 /* enable detection of RX fifo level */
840 mask |= CDNS_MCP_INT_RX_WL;
843 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
844 * settings are irrelevant
846 mask |= CDNS_MCP_INT_IRQ;
848 if (interrupt_mask) /* parameter override */
849 mask = interrupt_mask;
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);
856 /* commit changes */
857 return cdns_update_config(cdns);
859 EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
861 static int cdns_allocate_pdi(struct sdw_cdns *cdns,
862 struct sdw_cdns_pdi **stream,
863 u32 num, u32 pdi_offset)
865 struct sdw_cdns_pdi *pdi;
866 int i;
868 if (!num)
869 return 0;
871 pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
872 if (!pdi)
873 return -ENOMEM;
875 for (i = 0; i < num; i++) {
876 pdi[i].num = i + pdi_offset;
879 *stream = pdi;
880 return 0;
884 * sdw_cdns_pdi_init() - PDI initialization routine
886 * @cdns: Cadence instance
887 * @config: Stream configurations
889 int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
890 struct sdw_cdns_stream_config config)
892 struct sdw_cdns_streams *stream;
893 int offset;
894 int ret;
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;
903 /* Allocate PDIs for PCMs */
904 stream = &cdns->pcm;
906 /* we allocate PDI0 and PDI1 which are used for Bulk */
907 offset = 0;
909 ret = cdns_allocate_pdi(cdns, &stream->bd,
910 stream->num_bd, offset);
911 if (ret)
912 return ret;
914 offset += stream->num_bd;
916 ret = cdns_allocate_pdi(cdns, &stream->in,
917 stream->num_in, offset);
918 if (ret)
919 return ret;
921 offset += stream->num_in;
923 ret = cdns_allocate_pdi(cdns, &stream->out,
924 stream->num_out, offset);
925 if (ret)
926 return ret;
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;
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;
939 offset += stream->num_bd;
941 ret = cdns_allocate_pdi(cdns, &stream->in,
942 stream->num_in, offset);
943 if (ret)
944 return ret;
946 offset += stream->num_in;
948 ret = cdns_allocate_pdi(cdns, &stream->out,
949 stream->num_out, offset);
951 offset += stream->num_out;
953 if (ret)
954 return ret;
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;
960 return 0;
962 EXPORT_SYMBOL(sdw_cdns_pdi_init);
964 static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
966 u32 val;
967 int c;
968 int r;
970 r = sdw_find_row_index(n_rows);
971 c = sdw_find_col_index(n_cols) & CDNS_MCP_FRAME_SHAPE_COL_MASK;
973 val = (r << CDNS_MCP_FRAME_SHAPE_ROW_OFFSET) | c;
975 return val;
979 * sdw_cdns_init() - Cadence initialization
980 * @cdns: Cadence instance
982 int sdw_cdns_init(struct sdw_cdns *cdns, bool clock_stop_exit)
984 struct sdw_bus *bus = &cdns->bus;
985 struct sdw_master_prop *prop = &bus->prop;
986 u32 val;
987 int divider;
988 int ret;
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;
999 /* Set clock divider */
1000 divider = (prop->mclk_freq / prop->max_clk_freq) - 1;
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);
1008 * Frame shape changes after initialization have to be done
1009 * with the bank switch mechanism
1011 val = cdns_set_initial_frame_shape(prop->default_row,
1012 prop->default_col);
1013 cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
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);
1019 /* flush command FIFOs */
1020 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1021 CDNS_MCP_CONTROL_CMD_RST);
1023 /* Set cmd accept mode */
1024 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT,
1025 CDNS_MCP_CONTROL_CMD_ACCEPT);
1027 /* Configure mcp config */
1028 val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1030 /* Set Max cmd retry to 15 */
1031 val |= CDNS_MCP_CONFIG_MCMD_RETRY;
1033 /* Set frame delay between PREQ and ping frame to 15 frames */
1034 val |= 0xF << SDW_REG_SHIFT(CDNS_MCP_CONFIG_MPREQ_DELAY);
1036 /* Disable auto bus release */
1037 val &= ~CDNS_MCP_CONFIG_BUS_REL;
1039 /* Disable sniffer mode */
1040 val &= ~CDNS_MCP_CONFIG_SNIFFER;
1042 /* Set cmd mode for Tx and Rx cmds */
1043 val &= ~CDNS_MCP_CONFIG_CMD;
1045 cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1047 /* commit changes */
1048 return cdns_update_config(cdns);
1050 EXPORT_SYMBOL(sdw_cdns_init);
1052 int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
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;
1059 if (!params->curr_dr_freq) {
1060 dev_err(cdns->dev, "NULL curr_dr_freq\n");
1061 return -EINVAL;
1064 divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1065 params->curr_dr_freq;
1066 divider--; /* divider is 1/(N+1) */
1068 if (params->next_bank)
1069 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1070 else
1071 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1073 cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
1075 return 0;
1077 EXPORT_SYMBOL(cdns_bus_conf);
1079 static int cdns_port_params(struct sdw_bus *bus,
1080 struct sdw_port_params *p_params, unsigned int bank)
1082 struct sdw_cdns *cdns = bus_to_cdns(bus);
1083 int dpn_config = 0, dpn_config_off;
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);
1090 dpn_config = cdns_readl(cdns, dpn_config_off);
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));
1099 cdns_writel(cdns, dpn_config_off, dpn_config);
1101 return 0;
1104 static int cdns_transport_params(struct sdw_bus *bus,
1105 struct sdw_transport_params *t_params,
1106 enum sdw_reg_bank bank)
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;
1116 * Note: Only full data port is supported on the Master side for
1117 * both PCM and PDM ports.
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);
1132 dpn_config = cdns_readl(cdns, dpn_config_off);
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);
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);
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));
1152 cdns_writel(cdns, dpn_hctrl_off, dpn_hctrl);
1153 cdns_writel(cdns, dpn_samplectrl_off, (t_params->sample_interval - 1));
1155 return 0;
1158 static int cdns_port_enable(struct sdw_bus *bus,
1159 struct sdw_enable_ch *enable_ch, unsigned int bank)
1161 struct sdw_cdns *cdns = bus_to_cdns(bus);
1162 int dpn_chnen_off, ch_mask;
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);
1169 ch_mask = enable_ch->ch_mask * enable_ch->enable;
1170 cdns_writel(cdns, dpn_chnen_off, ch_mask);
1172 return 0;
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,
1182 * sdw_cdns_probe() - Cadence probe routine
1183 * @cdns: Cadence instance
1185 int sdw_cdns_probe(struct sdw_cdns *cdns)
1187 init_completion(&cdns->tx_complete);
1188 cdns->bus.port_ops = &cdns_port_ops;
1190 return 0;
1192 EXPORT_SYMBOL(sdw_cdns_probe);
1194 int cdns_set_sdw_stream(struct snd_soc_dai *dai,
1195 void *stream, bool pcm, int direction)
1197 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1198 struct sdw_cdns_dma_data *dma;
1200 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
1201 if (!dma)
1202 return -ENOMEM;
1204 if (pcm)
1205 dma->stream_type = SDW_STREAM_PCM;
1206 else
1207 dma->stream_type = SDW_STREAM_PDM;
1209 dma->bus = &cdns->bus;
1210 dma->link_id = cdns->instance;
1212 dma->stream = stream;
1214 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1215 dai->playback_dma_data = dma;
1216 else
1217 dai->capture_dma_data = dma;
1219 return 0;
1221 EXPORT_SYMBOL(cdns_set_sdw_stream);
1224 * cdns_find_pdi() - Find a free PDI
1226 * @cdns: Cadence instance
1227 * @num: Number of PDIs
1228 * @pdi: PDI instances
1230 * Find a PDI for a given PDI array. The PDI num and dai_id are
1231 * expected to match, return NULL otherwise.
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)
1239 int i;
1241 for (i = offset; i < offset + num; i++)
1242 if (pdi[i].num == dai_id)
1243 return &pdi[i];
1245 return NULL;
1249 * sdw_cdns_config_stream: Configure a stream
1251 * @cdns: Cadence instance
1252 * @ch: Channel count
1253 * @dir: Data direction
1254 * @pdi: PDI to be used
1256 void sdw_cdns_config_stream(struct sdw_cdns *cdns,
1257 u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
1259 u32 offset, val = 0;
1261 if (dir == SDW_DATA_DIR_RX)
1262 val = CDNS_PORTCTRL_DIRN;
1264 offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
1265 cdns_updatel(cdns, offset, CDNS_PORTCTRL_DIRN, val);
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);
1271 EXPORT_SYMBOL(sdw_cdns_config_stream);
1274 * sdw_cdns_alloc_pdi() - Allocate a PDI
1276 * @cdns: Cadence instance
1277 * @stream: Stream to be allocated
1278 * @ch: Channel count
1279 * @dir: Data direction
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)
1285 struct sdw_cdns_pdi *pdi = NULL;
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);
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);
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;
1306 return pdi;
1308 EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
1310 MODULE_LICENSE("Dual BSD/GPL");
1311 MODULE_DESCRIPTION("Cadence Soundwire Library");