1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019, Linaro Limited
5 #include <linux/completion.h>
6 #include <linux/interrupt.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/debugfs.h>
12 #include <linux/of_irq.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regmap.h>
15 #include <linux/reset.h>
16 #include <linux/slab.h>
17 #include <linux/pm_wakeirq.h>
18 #include <linux/slimbus.h>
19 #include <linux/soundwire/sdw.h>
20 #include <linux/soundwire/sdw_registers.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
25 #define SWRM_COMP_SW_RESET 0x008
26 #define SWRM_COMP_STATUS 0x014
27 #define SWRM_LINK_MANAGER_EE 0x018
29 #define SWRM_FRM_GEN_ENABLED BIT(0)
30 #define SWRM_VERSION_1_3_0 0x01030000
31 #define SWRM_VERSION_1_5_1 0x01050001
32 #define SWRM_VERSION_1_7_0 0x01070000
33 #define SWRM_VERSION_2_0_0 0x02000000
34 #define SWRM_COMP_HW_VERSION 0x00
35 #define SWRM_COMP_CFG_ADDR 0x04
36 #define SWRM_COMP_CFG_IRQ_LEVEL_OR_PULSE_MSK BIT(1)
37 #define SWRM_COMP_CFG_ENABLE_MSK BIT(0)
38 #define SWRM_COMP_PARAMS 0x100
39 #define SWRM_COMP_PARAMS_WR_FIFO_DEPTH GENMASK(14, 10)
40 #define SWRM_COMP_PARAMS_RD_FIFO_DEPTH GENMASK(19, 15)
41 #define SWRM_COMP_PARAMS_DOUT_PORTS_MASK GENMASK(4, 0)
42 #define SWRM_COMP_PARAMS_DIN_PORTS_MASK GENMASK(9, 5)
43 #define SWRM_COMP_MASTER_ID 0x104
44 #define SWRM_V1_3_INTERRUPT_STATUS 0x200
45 #define SWRM_V2_0_INTERRUPT_STATUS 0x5000
46 #define SWRM_INTERRUPT_STATUS_RMSK GENMASK(16, 0)
47 #define SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ BIT(0)
48 #define SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED BIT(1)
49 #define SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS BIT(2)
50 #define SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET BIT(3)
51 #define SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW BIT(4)
52 #define SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW BIT(5)
53 #define SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW BIT(6)
54 #define SWRM_INTERRUPT_STATUS_CMD_ERROR BIT(7)
55 #define SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION BIT(8)
56 #define SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH BIT(9)
57 #define SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED BIT(10)
58 #define SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED BIT(11)
59 #define SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL BIT(12)
60 #define SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2 BIT(13)
61 #define SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2 BIT(14)
62 #define SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP BIT(16)
63 #define SWRM_INTERRUPT_MAX 17
64 #define SWRM_V1_3_INTERRUPT_MASK_ADDR 0x204
65 #define SWRM_V1_3_INTERRUPT_CLEAR 0x208
66 #define SWRM_V2_0_INTERRUPT_CLEAR 0x5008
67 #define SWRM_V1_3_INTERRUPT_CPU_EN 0x210
68 #define SWRM_V2_0_INTERRUPT_CPU_EN 0x5004
69 #define SWRM_V1_3_CMD_FIFO_WR_CMD 0x300
70 #define SWRM_V2_0_CMD_FIFO_WR_CMD 0x5020
71 #define SWRM_V1_3_CMD_FIFO_RD_CMD 0x304
72 #define SWRM_V2_0_CMD_FIFO_RD_CMD 0x5024
73 #define SWRM_CMD_FIFO_CMD 0x308
74 #define SWRM_CMD_FIFO_FLUSH 0x1
75 #define SWRM_V1_3_CMD_FIFO_STATUS 0x30C
76 #define SWRM_V2_0_CMD_FIFO_STATUS 0x5050
77 #define SWRM_RD_CMD_FIFO_CNT_MASK GENMASK(20, 16)
78 #define SWRM_WR_CMD_FIFO_CNT_MASK GENMASK(12, 8)
79 #define SWRM_CMD_FIFO_CFG_ADDR 0x314
80 #define SWRM_CONTINUE_EXEC_ON_CMD_IGNORE BIT(31)
81 #define SWRM_RD_WR_CMD_RETRIES 0x7
82 #define SWRM_V1_3_CMD_FIFO_RD_FIFO_ADDR 0x318
83 #define SWRM_V2_0_CMD_FIFO_RD_FIFO_ADDR 0x5040
84 #define SWRM_RD_FIFO_CMD_ID_MASK GENMASK(11, 8)
85 #define SWRM_ENUMERATOR_CFG_ADDR 0x500
86 #define SWRM_ENUMERATOR_SLAVE_DEV_ID_1(m) (0x530 + 0x8 * (m))
87 #define SWRM_ENUMERATOR_SLAVE_DEV_ID_2(m) (0x534 + 0x8 * (m))
88 #define SWRM_MCP_FRAME_CTRL_BANK_ADDR(m) (0x101C + 0x40 * (m))
89 #define SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK GENMASK(2, 0)
90 #define SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK GENMASK(7, 3)
91 #define SWRM_MCP_BUS_CTRL 0x1044
92 #define SWRM_MCP_BUS_CLK_START BIT(1)
93 #define SWRM_MCP_CFG_ADDR 0x1048
94 #define SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK GENMASK(21, 17)
95 #define SWRM_DEF_CMD_NO_PINGS 0x1f
96 #define SWRM_MCP_STATUS 0x104C
97 #define SWRM_MCP_STATUS_BANK_NUM_MASK BIT(0)
98 #define SWRM_MCP_SLV_STATUS 0x1090
99 #define SWRM_MCP_SLV_STATUS_MASK GENMASK(1, 0)
100 #define SWRM_MCP_SLV_STATUS_SZ 2
101 #define SWRM_DP_PORT_CTRL_BANK(n, m) (0x1124 + 0x100 * (n - 1) + 0x40 * m)
102 #define SWRM_DP_PORT_CTRL_2_BANK(n, m) (0x1128 + 0x100 * (n - 1) + 0x40 * m)
103 #define SWRM_DP_BLOCK_CTRL_1(n) (0x112C + 0x100 * (n - 1))
104 #define SWRM_DP_BLOCK_CTRL2_BANK(n, m) (0x1130 + 0x100 * (n - 1) + 0x40 * m)
105 #define SWRM_DP_PORT_HCTRL_BANK(n, m) (0x1134 + 0x100 * (n - 1) + 0x40 * m)
106 #define SWRM_DP_BLOCK_CTRL3_BANK(n, m) (0x1138 + 0x100 * (n - 1) + 0x40 * m)
107 #define SWRM_DP_SAMPLECTRL2_BANK(n, m) (0x113C + 0x100 * (n - 1) + 0x40 * m)
108 #define SWRM_DIN_DPn_PCM_PORT_CTRL(n) (0x1054 + 0x100 * (n - 1))
109 #define SWR_V1_3_MSTR_MAX_REG_ADDR 0x1740
110 #define SWR_V2_0_MSTR_MAX_REG_ADDR 0x50ac
112 #define SWRM_V2_0_CLK_CTRL 0x5060
113 #define SWRM_V2_0_CLK_CTRL_CLK_START BIT(0)
114 #define SWRM_V2_0_LINK_STATUS 0x5064
116 #define SWRM_DP_PORT_CTRL_EN_CHAN_SHFT 0x18
117 #define SWRM_DP_PORT_CTRL_OFFSET2_SHFT 0x10
118 #define SWRM_DP_PORT_CTRL_OFFSET1_SHFT 0x08
119 #define SWRM_AHB_BRIDGE_WR_DATA_0 0xc85
120 #define SWRM_AHB_BRIDGE_WR_ADDR_0 0xc89
121 #define SWRM_AHB_BRIDGE_RD_ADDR_0 0xc8d
122 #define SWRM_AHB_BRIDGE_RD_DATA_0 0xc91
124 #define SWRM_REG_VAL_PACK(data, dev, id, reg) \
125 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
127 #define MAX_FREQ_NUM 1
128 #define TIMEOUT_MS 100
129 #define QCOM_SWRM_MAX_RD_LEN 0x1
130 #define QCOM_SDW_MAX_PORTS 14
131 #define DEFAULT_CLK_FREQ 9600000
132 #define SWRM_MAX_DAIS 0xF
133 #define SWR_INVALID_PARAM 0xFF
134 #define SWR_HSTOP_MAX_VAL 0xF
135 #define SWR_HSTART_MIN_VAL 0x0
136 #define SWR_BROADCAST_CMD_ID 0x0F
137 #define SWR_MAX_CMD_ID 14
138 #define MAX_FIFO_RD_RETRY 3
139 #define SWR_OVERFLOW_RETRY_COUNT 30
140 #define SWRM_LINK_STATUS_RETRY_CNT 100
148 struct qcom_swrm_port_config {
161 * Internal IDs for different register layouts. Only few registers differ per
162 * each variant, so the list of IDs below does not include all of registers.
165 SWRM_REG_FRAME_GEN_ENABLED,
166 SWRM_REG_INTERRUPT_STATUS,
167 SWRM_REG_INTERRUPT_MASK_ADDR,
168 SWRM_REG_INTERRUPT_CLEAR,
169 SWRM_REG_INTERRUPT_CPU_EN,
170 SWRM_REG_CMD_FIFO_WR_CMD,
171 SWRM_REG_CMD_FIFO_RD_CMD,
172 SWRM_REG_CMD_FIFO_STATUS,
173 SWRM_REG_CMD_FIFO_RD_FIFO_ADDR,
176 struct qcom_swrm_ctrl {
179 struct regmap *regmap;
181 const unsigned int *reg_layout;
183 struct reset_control *audio_cgcr;
184 #ifdef CONFIG_DEBUG_FS
185 struct dentry *debugfs;
187 struct completion broadcast;
188 struct completion enumeration;
189 /* Port alloc/free lock */
190 struct mutex port_lock;
193 unsigned int version;
199 unsigned long dout_port_mask;
200 unsigned long din_port_mask;
204 /* Port numbers are 1 - 14 */
205 struct qcom_swrm_port_config pconfig[QCOM_SDW_MAX_PORTS + 1];
206 struct sdw_stream_runtime *sruntime[SWRM_MAX_DAIS];
207 enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
208 int (*reg_read)(struct qcom_swrm_ctrl *ctrl, int reg, u32 *val);
209 int (*reg_write)(struct qcom_swrm_ctrl *ctrl, int reg, int val);
213 bool clock_stop_not_supported;
216 struct qcom_swrm_data {
219 bool sw_clk_gate_required;
221 const unsigned int *reg_layout;
224 static const unsigned int swrm_v1_3_reg_layout[] = {
225 [SWRM_REG_FRAME_GEN_ENABLED] = SWRM_COMP_STATUS,
226 [SWRM_REG_INTERRUPT_STATUS] = SWRM_V1_3_INTERRUPT_STATUS,
227 [SWRM_REG_INTERRUPT_MASK_ADDR] = SWRM_V1_3_INTERRUPT_MASK_ADDR,
228 [SWRM_REG_INTERRUPT_CLEAR] = SWRM_V1_3_INTERRUPT_CLEAR,
229 [SWRM_REG_INTERRUPT_CPU_EN] = SWRM_V1_3_INTERRUPT_CPU_EN,
230 [SWRM_REG_CMD_FIFO_WR_CMD] = SWRM_V1_3_CMD_FIFO_WR_CMD,
231 [SWRM_REG_CMD_FIFO_RD_CMD] = SWRM_V1_3_CMD_FIFO_RD_CMD,
232 [SWRM_REG_CMD_FIFO_STATUS] = SWRM_V1_3_CMD_FIFO_STATUS,
233 [SWRM_REG_CMD_FIFO_RD_FIFO_ADDR] = SWRM_V1_3_CMD_FIFO_RD_FIFO_ADDR,
236 static const struct qcom_swrm_data swrm_v1_3_data = {
239 .max_reg = SWR_V1_3_MSTR_MAX_REG_ADDR,
240 .reg_layout = swrm_v1_3_reg_layout,
243 static const struct qcom_swrm_data swrm_v1_5_data = {
246 .max_reg = SWR_V1_3_MSTR_MAX_REG_ADDR,
247 .reg_layout = swrm_v1_3_reg_layout,
250 static const struct qcom_swrm_data swrm_v1_6_data = {
253 .sw_clk_gate_required = true,
254 .max_reg = SWR_V1_3_MSTR_MAX_REG_ADDR,
255 .reg_layout = swrm_v1_3_reg_layout,
258 static const unsigned int swrm_v2_0_reg_layout[] = {
259 [SWRM_REG_FRAME_GEN_ENABLED] = SWRM_V2_0_LINK_STATUS,
260 [SWRM_REG_INTERRUPT_STATUS] = SWRM_V2_0_INTERRUPT_STATUS,
261 [SWRM_REG_INTERRUPT_MASK_ADDR] = 0, /* Not present */
262 [SWRM_REG_INTERRUPT_CLEAR] = SWRM_V2_0_INTERRUPT_CLEAR,
263 [SWRM_REG_INTERRUPT_CPU_EN] = SWRM_V2_0_INTERRUPT_CPU_EN,
264 [SWRM_REG_CMD_FIFO_WR_CMD] = SWRM_V2_0_CMD_FIFO_WR_CMD,
265 [SWRM_REG_CMD_FIFO_RD_CMD] = SWRM_V2_0_CMD_FIFO_RD_CMD,
266 [SWRM_REG_CMD_FIFO_STATUS] = SWRM_V2_0_CMD_FIFO_STATUS,
267 [SWRM_REG_CMD_FIFO_RD_FIFO_ADDR] = SWRM_V2_0_CMD_FIFO_RD_FIFO_ADDR,
270 static const struct qcom_swrm_data swrm_v2_0_data = {
273 .sw_clk_gate_required = true,
274 .max_reg = SWR_V2_0_MSTR_MAX_REG_ADDR,
275 .reg_layout = swrm_v2_0_reg_layout,
278 #define to_qcom_sdw(b) container_of(b, struct qcom_swrm_ctrl, bus)
280 static int qcom_swrm_ahb_reg_read(struct qcom_swrm_ctrl *ctrl, int reg,
283 struct regmap *wcd_regmap = ctrl->regmap;
286 /* pg register + offset */
287 ret = regmap_bulk_write(wcd_regmap, SWRM_AHB_BRIDGE_RD_ADDR_0,
292 ret = regmap_bulk_read(wcd_regmap, SWRM_AHB_BRIDGE_RD_DATA_0,
300 static int qcom_swrm_ahb_reg_write(struct qcom_swrm_ctrl *ctrl,
303 struct regmap *wcd_regmap = ctrl->regmap;
305 /* pg register + offset */
306 ret = regmap_bulk_write(wcd_regmap, SWRM_AHB_BRIDGE_WR_DATA_0,
311 /* write address register */
312 ret = regmap_bulk_write(wcd_regmap, SWRM_AHB_BRIDGE_WR_ADDR_0,
320 static int qcom_swrm_cpu_reg_read(struct qcom_swrm_ctrl *ctrl, int reg,
323 *val = readl(ctrl->mmio + reg);
327 static int qcom_swrm_cpu_reg_write(struct qcom_swrm_ctrl *ctrl, int reg,
330 writel(val, ctrl->mmio + reg);
334 static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
335 u8 dev_addr, u16 reg_addr)
340 if (id != SWR_BROADCAST_CMD_ID) {
341 if (id < SWR_MAX_CMD_ID)
347 val = SWRM_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
352 static int swrm_wait_for_rd_fifo_avail(struct qcom_swrm_ctrl *ctrl)
354 u32 fifo_outstanding_data, value;
355 int fifo_retry_count = SWR_OVERFLOW_RETRY_COUNT;
358 /* Check for fifo underflow during read */
359 ctrl->reg_read(ctrl, ctrl->reg_layout[SWRM_REG_CMD_FIFO_STATUS],
361 fifo_outstanding_data = FIELD_GET(SWRM_RD_CMD_FIFO_CNT_MASK, value);
363 /* Check if read data is available in read fifo */
364 if (fifo_outstanding_data > 0)
367 usleep_range(500, 510);
368 } while (fifo_retry_count--);
370 if (fifo_outstanding_data == 0) {
371 dev_err_ratelimited(ctrl->dev, "%s err read underflow\n", __func__);
378 static int swrm_wait_for_wr_fifo_avail(struct qcom_swrm_ctrl *ctrl)
380 u32 fifo_outstanding_cmds, value;
381 int fifo_retry_count = SWR_OVERFLOW_RETRY_COUNT;
384 /* Check for fifo overflow during write */
385 ctrl->reg_read(ctrl, ctrl->reg_layout[SWRM_REG_CMD_FIFO_STATUS],
387 fifo_outstanding_cmds = FIELD_GET(SWRM_WR_CMD_FIFO_CNT_MASK, value);
389 /* Check for space in write fifo before writing */
390 if (fifo_outstanding_cmds < ctrl->wr_fifo_depth)
393 usleep_range(500, 510);
394 } while (fifo_retry_count--);
396 if (fifo_outstanding_cmds == ctrl->wr_fifo_depth) {
397 dev_err_ratelimited(ctrl->dev, "%s err write overflow\n", __func__);
404 static bool swrm_wait_for_wr_fifo_done(struct qcom_swrm_ctrl *ctrl)
406 u32 fifo_outstanding_cmds, value;
407 int fifo_retry_count = SWR_OVERFLOW_RETRY_COUNT;
409 /* Check for fifo overflow during write */
410 ctrl->reg_read(ctrl, ctrl->reg_layout[SWRM_REG_CMD_FIFO_STATUS], &value);
411 fifo_outstanding_cmds = FIELD_GET(SWRM_WR_CMD_FIFO_CNT_MASK, value);
413 if (fifo_outstanding_cmds) {
414 while (fifo_retry_count) {
415 usleep_range(500, 510);
416 ctrl->reg_read(ctrl, ctrl->reg_layout[SWRM_REG_CMD_FIFO_STATUS], &value);
417 fifo_outstanding_cmds = FIELD_GET(SWRM_WR_CMD_FIFO_CNT_MASK, value);
419 if (fifo_outstanding_cmds == 0)
430 static int qcom_swrm_cmd_fifo_wr_cmd(struct qcom_swrm_ctrl *ctrl, u8 cmd_data,
431 u8 dev_addr, u16 reg_addr)
438 if (dev_addr == SDW_BROADCAST_DEV_NUM) {
439 cmd_id = SWR_BROADCAST_CMD_ID;
440 val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
443 val = swrm_get_packed_reg_val(&ctrl->wcmd_id, cmd_data,
447 if (swrm_wait_for_wr_fifo_avail(ctrl))
448 return SDW_CMD_FAIL_OTHER;
450 if (cmd_id == SWR_BROADCAST_CMD_ID)
451 reinit_completion(&ctrl->broadcast);
453 /* Its assumed that write is okay as we do not get any status back */
454 ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_CMD_FIFO_WR_CMD], val);
456 if (ctrl->version <= SWRM_VERSION_1_3_0)
457 usleep_range(150, 155);
459 if (cmd_id == SWR_BROADCAST_CMD_ID) {
460 swrm_wait_for_wr_fifo_done(ctrl);
462 * sleep for 10ms for MSM soundwire variant to allow broadcast
463 * command to complete.
465 ret = wait_for_completion_timeout(&ctrl->broadcast,
466 msecs_to_jiffies(TIMEOUT_MS));
468 ret = SDW_CMD_IGNORED;
478 static int qcom_swrm_cmd_fifo_rd_cmd(struct qcom_swrm_ctrl *ctrl,
479 u8 dev_addr, u16 reg_addr,
482 u32 cmd_data, cmd_id, val, retry_attempt = 0;
484 val = swrm_get_packed_reg_val(&ctrl->rcmd_id, len, dev_addr, reg_addr);
487 * Check for outstanding cmd wrt. write fifo depth to avoid
488 * overflow as read will also increase write fifo cnt.
490 swrm_wait_for_wr_fifo_avail(ctrl);
492 /* wait for FIFO RD to complete to avoid overflow */
493 usleep_range(100, 105);
494 ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_CMD_FIFO_RD_CMD], val);
495 /* wait for FIFO RD CMD complete to avoid overflow */
496 usleep_range(250, 255);
498 if (swrm_wait_for_rd_fifo_avail(ctrl))
499 return SDW_CMD_FAIL_OTHER;
502 ctrl->reg_read(ctrl, ctrl->reg_layout[SWRM_REG_CMD_FIFO_RD_FIFO_ADDR],
504 rval[0] = cmd_data & 0xFF;
505 cmd_id = FIELD_GET(SWRM_RD_FIFO_CMD_ID_MASK, cmd_data);
507 if (cmd_id != ctrl->rcmd_id) {
508 if (retry_attempt < (MAX_FIFO_RD_RETRY - 1)) {
509 /* wait 500 us before retry on fifo read failure */
510 usleep_range(500, 505);
511 ctrl->reg_write(ctrl, SWRM_CMD_FIFO_CMD,
512 SWRM_CMD_FIFO_FLUSH);
513 ctrl->reg_write(ctrl,
514 ctrl->reg_layout[SWRM_REG_CMD_FIFO_RD_CMD],
522 } while (retry_attempt < MAX_FIFO_RD_RETRY);
524 dev_err(ctrl->dev, "failed to read fifo: reg: 0x%x, rcmd_id: 0x%x,\
525 dev_num: 0x%x, cmd_data: 0x%x\n",
526 reg_addr, ctrl->rcmd_id, dev_addr, cmd_data);
528 return SDW_CMD_IGNORED;
531 static int qcom_swrm_get_alert_slave_dev_num(struct qcom_swrm_ctrl *ctrl)
536 ctrl->reg_read(ctrl, SWRM_MCP_SLV_STATUS, &val);
538 for (dev_num = 1; dev_num <= SDW_MAX_DEVICES; dev_num++) {
539 status = (val >> (dev_num * SWRM_MCP_SLV_STATUS_SZ));
541 if ((status & SWRM_MCP_SLV_STATUS_MASK) == SDW_SLAVE_ALERT) {
542 ctrl->status[dev_num] = status & SWRM_MCP_SLV_STATUS_MASK;
550 static void qcom_swrm_get_device_status(struct qcom_swrm_ctrl *ctrl)
555 ctrl->reg_read(ctrl, SWRM_MCP_SLV_STATUS, &val);
556 ctrl->slave_status = val;
558 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
561 s = (val >> (i * 2));
562 s &= SWRM_MCP_SLV_STATUS_MASK;
567 static void qcom_swrm_set_slave_dev_num(struct sdw_bus *bus,
568 struct sdw_slave *slave, int devnum)
570 struct qcom_swrm_ctrl *ctrl = to_qcom_sdw(bus);
573 ctrl->reg_read(ctrl, SWRM_MCP_SLV_STATUS, &status);
574 status = (status >> (devnum * SWRM_MCP_SLV_STATUS_SZ));
575 status &= SWRM_MCP_SLV_STATUS_MASK;
577 if (status == SDW_SLAVE_ATTACHED) {
579 slave->dev_num = devnum;
580 mutex_lock(&bus->bus_lock);
581 set_bit(devnum, bus->assigned);
582 mutex_unlock(&bus->bus_lock);
586 static int qcom_swrm_enumerate(struct sdw_bus *bus)
588 struct qcom_swrm_ctrl *ctrl = to_qcom_sdw(bus);
589 struct sdw_slave *slave, *_s;
590 struct sdw_slave_id id;
595 char *buf1 = (char *)&val1, *buf2 = (char *)&val2;
597 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
598 /* do not continue if the status is Not Present */
599 if (!ctrl->status[i])
602 /*SCP_Devid5 - Devid 4*/
603 ctrl->reg_read(ctrl, SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i), &val1);
605 /*SCP_Devid3 - DevId 2 Devid 1 Devid 0*/
606 ctrl->reg_read(ctrl, SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i), &val2);
611 addr = buf2[1] | (buf2[0] << 8) | (buf1[3] << 16) |
612 ((u64)buf1[2] << 24) | ((u64)buf1[1] << 32) |
613 ((u64)buf1[0] << 40);
615 sdw_extract_slave_id(bus, addr, &id);
617 ctrl->clock_stop_not_supported = false;
618 /* Now compare with entries */
619 list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
620 if (sdw_compare_devid(slave, id) == 0) {
621 qcom_swrm_set_slave_dev_num(bus, slave, i);
622 if (slave->prop.clk_stop_mode1)
623 ctrl->clock_stop_not_supported = true;
631 qcom_swrm_set_slave_dev_num(bus, NULL, i);
632 sdw_slave_add(bus, &id, NULL);
636 complete(&ctrl->enumeration);
640 static irqreturn_t qcom_swrm_wake_irq_handler(int irq, void *dev_id)
642 struct qcom_swrm_ctrl *ctrl = dev_id;
645 ret = pm_runtime_get_sync(ctrl->dev);
646 if (ret < 0 && ret != -EACCES) {
647 dev_err_ratelimited(ctrl->dev,
648 "pm_runtime_get_sync failed in %s, ret %d\n",
650 pm_runtime_put_noidle(ctrl->dev);
654 if (ctrl->wake_irq > 0) {
655 if (!irqd_irq_disabled(irq_get_irq_data(ctrl->wake_irq)))
656 disable_irq_nosync(ctrl->wake_irq);
659 pm_runtime_mark_last_busy(ctrl->dev);
660 pm_runtime_put_autosuspend(ctrl->dev);
665 static irqreturn_t qcom_swrm_irq_handler(int irq, void *dev_id)
667 struct qcom_swrm_ctrl *ctrl = dev_id;
668 u32 value, intr_sts, intr_sts_masked, slave_status;
671 int ret = IRQ_HANDLED;
672 clk_prepare_enable(ctrl->hclk);
674 ctrl->reg_read(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_STATUS],
676 intr_sts_masked = intr_sts & ctrl->intr_mask;
679 for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
680 value = intr_sts_masked & BIT(i);
685 case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
686 devnum = qcom_swrm_get_alert_slave_dev_num(ctrl);
688 dev_err_ratelimited(ctrl->dev,
689 "no slave alert found.spurious interrupt\n");
691 sdw_handle_slave_status(&ctrl->bus, ctrl->status);
695 case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
696 case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
697 dev_dbg_ratelimited(ctrl->dev, "SWR new slave attached\n");
698 ctrl->reg_read(ctrl, SWRM_MCP_SLV_STATUS, &slave_status);
699 if (ctrl->slave_status == slave_status) {
700 dev_dbg(ctrl->dev, "Slave status not changed %x\n",
703 qcom_swrm_get_device_status(ctrl);
704 qcom_swrm_enumerate(&ctrl->bus);
705 sdw_handle_slave_status(&ctrl->bus, ctrl->status);
708 case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
709 dev_err_ratelimited(ctrl->dev,
710 "%s: SWR bus clsh detected\n",
712 ctrl->intr_mask &= ~SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET;
713 ctrl->reg_write(ctrl,
714 ctrl->reg_layout[SWRM_REG_INTERRUPT_CPU_EN],
717 case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
719 ctrl->reg_layout[SWRM_REG_CMD_FIFO_STATUS],
721 dev_err_ratelimited(ctrl->dev,
722 "%s: SWR read FIFO overflow fifo status 0x%x\n",
725 case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
727 ctrl->reg_layout[SWRM_REG_CMD_FIFO_STATUS],
729 dev_err_ratelimited(ctrl->dev,
730 "%s: SWR read FIFO underflow fifo status 0x%x\n",
733 case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
735 ctrl->reg_layout[SWRM_REG_CMD_FIFO_STATUS],
738 "%s: SWR write FIFO overflow fifo status %x\n",
740 ctrl->reg_write(ctrl, SWRM_CMD_FIFO_CMD, 0x1);
742 case SWRM_INTERRUPT_STATUS_CMD_ERROR:
744 ctrl->reg_layout[SWRM_REG_CMD_FIFO_STATUS],
746 dev_err_ratelimited(ctrl->dev,
747 "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
749 ctrl->reg_write(ctrl, SWRM_CMD_FIFO_CMD, 0x1);
751 case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
752 dev_err_ratelimited(ctrl->dev,
753 "%s: SWR Port collision detected\n",
755 ctrl->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
756 ctrl->reg_write(ctrl,
757 ctrl->reg_layout[SWRM_REG_INTERRUPT_CPU_EN],
760 case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
761 dev_err_ratelimited(ctrl->dev,
762 "%s: SWR read enable valid mismatch\n",
765 ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
766 ctrl->reg_write(ctrl,
767 ctrl->reg_layout[SWRM_REG_INTERRUPT_CPU_EN],
770 case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
771 complete(&ctrl->broadcast);
773 case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
775 case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
777 case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
780 dev_err_ratelimited(ctrl->dev,
781 "%s: SWR unknown interrupt value: %d\n",
787 ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_CLEAR],
789 ctrl->reg_read(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_STATUS],
791 intr_sts_masked = intr_sts & ctrl->intr_mask;
792 } while (intr_sts_masked);
794 clk_disable_unprepare(ctrl->hclk);
798 static bool swrm_wait_for_frame_gen_enabled(struct qcom_swrm_ctrl *ctrl)
800 int retry = SWRM_LINK_STATUS_RETRY_CNT;
804 ctrl->reg_read(ctrl, SWRM_COMP_STATUS, &comp_sts);
806 if (comp_sts & SWRM_FRM_GEN_ENABLED)
809 usleep_range(500, 510);
812 dev_err(ctrl->dev, "%s: link status not %s\n", __func__,
813 comp_sts & SWRM_FRM_GEN_ENABLED ? "connected" : "disconnected");
818 static int qcom_swrm_init(struct qcom_swrm_ctrl *ctrl)
822 /* Clear Rows and Cols */
823 val = FIELD_PREP(SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK, ctrl->rows_index);
824 val |= FIELD_PREP(SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK, ctrl->cols_index);
826 reset_control_reset(ctrl->audio_cgcr);
828 ctrl->reg_write(ctrl, SWRM_MCP_FRAME_CTRL_BANK_ADDR(0), val);
830 /* Enable Auto enumeration */
831 ctrl->reg_write(ctrl, SWRM_ENUMERATOR_CFG_ADDR, 1);
833 ctrl->intr_mask = SWRM_INTERRUPT_STATUS_RMSK;
834 /* Mask soundwire interrupts */
835 if (ctrl->version < SWRM_VERSION_2_0_0)
836 ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_MASK_ADDR],
837 SWRM_INTERRUPT_STATUS_RMSK);
839 /* Configure No pings */
840 ctrl->reg_read(ctrl, SWRM_MCP_CFG_ADDR, &val);
841 u32p_replace_bits(&val, SWRM_DEF_CMD_NO_PINGS, SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK);
842 ctrl->reg_write(ctrl, SWRM_MCP_CFG_ADDR, val);
844 if (ctrl->version == SWRM_VERSION_1_7_0) {
845 ctrl->reg_write(ctrl, SWRM_LINK_MANAGER_EE, SWRM_EE_CPU);
846 ctrl->reg_write(ctrl, SWRM_MCP_BUS_CTRL,
847 SWRM_MCP_BUS_CLK_START << SWRM_EE_CPU);
848 } else if (ctrl->version >= SWRM_VERSION_2_0_0) {
849 ctrl->reg_write(ctrl, SWRM_LINK_MANAGER_EE, SWRM_EE_CPU);
850 ctrl->reg_write(ctrl, SWRM_V2_0_CLK_CTRL,
851 SWRM_V2_0_CLK_CTRL_CLK_START);
853 ctrl->reg_write(ctrl, SWRM_MCP_BUS_CTRL, SWRM_MCP_BUS_CLK_START);
856 /* Configure number of retries of a read/write cmd */
857 if (ctrl->version >= SWRM_VERSION_1_5_1) {
858 ctrl->reg_write(ctrl, SWRM_CMD_FIFO_CFG_ADDR,
859 SWRM_RD_WR_CMD_RETRIES |
860 SWRM_CONTINUE_EXEC_ON_CMD_IGNORE);
862 ctrl->reg_write(ctrl, SWRM_CMD_FIFO_CFG_ADDR,
863 SWRM_RD_WR_CMD_RETRIES);
867 ctrl->reg_write(ctrl, SWRM_COMP_CFG_ADDR, SWRM_COMP_CFG_ENABLE_MSK);
869 /* Set IRQ to PULSE */
870 ctrl->reg_write(ctrl, SWRM_COMP_CFG_ADDR,
871 SWRM_COMP_CFG_IRQ_LEVEL_OR_PULSE_MSK);
873 ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_CLEAR],
876 /* enable CPU IRQs */
878 ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_CPU_EN],
879 SWRM_INTERRUPT_STATUS_RMSK);
882 /* Set IRQ to PULSE */
883 ctrl->reg_write(ctrl, SWRM_COMP_CFG_ADDR,
884 SWRM_COMP_CFG_IRQ_LEVEL_OR_PULSE_MSK |
885 SWRM_COMP_CFG_ENABLE_MSK);
887 swrm_wait_for_frame_gen_enabled(ctrl);
888 ctrl->slave_status = 0;
889 ctrl->reg_read(ctrl, SWRM_COMP_PARAMS, &val);
890 ctrl->rd_fifo_depth = FIELD_GET(SWRM_COMP_PARAMS_RD_FIFO_DEPTH, val);
891 ctrl->wr_fifo_depth = FIELD_GET(SWRM_COMP_PARAMS_WR_FIFO_DEPTH, val);
896 static enum sdw_command_response qcom_swrm_xfer_msg(struct sdw_bus *bus,
899 struct qcom_swrm_ctrl *ctrl = to_qcom_sdw(bus);
902 if (msg->flags == SDW_MSG_FLAG_READ) {
903 for (i = 0; i < msg->len;) {
904 if ((msg->len - i) < QCOM_SWRM_MAX_RD_LEN)
907 len = QCOM_SWRM_MAX_RD_LEN;
909 ret = qcom_swrm_cmd_fifo_rd_cmd(ctrl, msg->dev_num,
917 } else if (msg->flags == SDW_MSG_FLAG_WRITE) {
918 for (i = 0; i < msg->len; i++) {
919 ret = qcom_swrm_cmd_fifo_wr_cmd(ctrl, msg->buf[i],
923 return SDW_CMD_IGNORED;
930 static int qcom_swrm_pre_bank_switch(struct sdw_bus *bus)
932 u32 reg = SWRM_MCP_FRAME_CTRL_BANK_ADDR(bus->params.next_bank);
933 struct qcom_swrm_ctrl *ctrl = to_qcom_sdw(bus);
936 ctrl->reg_read(ctrl, reg, &val);
938 u32p_replace_bits(&val, ctrl->cols_index, SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK);
939 u32p_replace_bits(&val, ctrl->rows_index, SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK);
941 return ctrl->reg_write(ctrl, reg, val);
944 static int qcom_swrm_port_params(struct sdw_bus *bus,
945 struct sdw_port_params *p_params,
948 struct qcom_swrm_ctrl *ctrl = to_qcom_sdw(bus);
950 return ctrl->reg_write(ctrl, SWRM_DP_BLOCK_CTRL_1(p_params->num),
955 static int qcom_swrm_transport_params(struct sdw_bus *bus,
956 struct sdw_transport_params *params,
957 enum sdw_reg_bank bank)
959 struct qcom_swrm_ctrl *ctrl = to_qcom_sdw(bus);
960 struct qcom_swrm_port_config *pcfg;
962 int reg = SWRM_DP_PORT_CTRL_BANK((params->port_num), bank);
965 pcfg = &ctrl->pconfig[params->port_num];
967 value = pcfg->off1 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT;
968 value |= pcfg->off2 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT;
969 value |= pcfg->si & 0xff;
971 ret = ctrl->reg_write(ctrl, reg, value);
975 if (pcfg->si > 0xff) {
976 value = (pcfg->si >> 8) & 0xff;
977 reg = SWRM_DP_SAMPLECTRL2_BANK(params->port_num, bank);
978 ret = ctrl->reg_write(ctrl, reg, value);
983 if (pcfg->lane_control != SWR_INVALID_PARAM) {
984 reg = SWRM_DP_PORT_CTRL_2_BANK(params->port_num, bank);
985 value = pcfg->lane_control;
986 ret = ctrl->reg_write(ctrl, reg, value);
991 if (pcfg->blk_group_count != SWR_INVALID_PARAM) {
992 reg = SWRM_DP_BLOCK_CTRL2_BANK(params->port_num, bank);
993 value = pcfg->blk_group_count;
994 ret = ctrl->reg_write(ctrl, reg, value);
999 if (pcfg->hstart != SWR_INVALID_PARAM
1000 && pcfg->hstop != SWR_INVALID_PARAM) {
1001 reg = SWRM_DP_PORT_HCTRL_BANK(params->port_num, bank);
1002 value = (pcfg->hstop << 4) | pcfg->hstart;
1003 ret = ctrl->reg_write(ctrl, reg, value);
1005 reg = SWRM_DP_PORT_HCTRL_BANK(params->port_num, bank);
1006 value = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
1007 ret = ctrl->reg_write(ctrl, reg, value);
1013 if (pcfg->bp_mode != SWR_INVALID_PARAM) {
1014 reg = SWRM_DP_BLOCK_CTRL3_BANK(params->port_num, bank);
1015 ret = ctrl->reg_write(ctrl, reg, pcfg->bp_mode);
1022 static int qcom_swrm_port_enable(struct sdw_bus *bus,
1023 struct sdw_enable_ch *enable_ch,
1026 u32 reg = SWRM_DP_PORT_CTRL_BANK(enable_ch->port_num, bank);
1027 struct qcom_swrm_ctrl *ctrl = to_qcom_sdw(bus);
1030 ctrl->reg_read(ctrl, reg, &val);
1032 if (enable_ch->enable)
1033 val |= (enable_ch->ch_mask << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
1035 val &= ~(0xff << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
1037 return ctrl->reg_write(ctrl, reg, val);
1040 static const struct sdw_master_port_ops qcom_swrm_port_ops = {
1041 .dpn_set_port_params = qcom_swrm_port_params,
1042 .dpn_set_port_transport_params = qcom_swrm_transport_params,
1043 .dpn_port_enable_ch = qcom_swrm_port_enable,
1046 static const struct sdw_master_ops qcom_swrm_ops = {
1047 .xfer_msg = qcom_swrm_xfer_msg,
1048 .pre_bank_switch = qcom_swrm_pre_bank_switch,
1051 static int qcom_swrm_compute_params(struct sdw_bus *bus)
1053 struct qcom_swrm_ctrl *ctrl = to_qcom_sdw(bus);
1054 struct sdw_master_runtime *m_rt;
1055 struct sdw_slave_runtime *s_rt;
1056 struct sdw_port_runtime *p_rt;
1057 struct qcom_swrm_port_config *pcfg;
1058 struct sdw_slave *slave;
1059 unsigned int m_port;
1062 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
1063 list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
1064 pcfg = &ctrl->pconfig[p_rt->num];
1065 p_rt->transport_params.port_num = p_rt->num;
1066 if (pcfg->word_length != SWR_INVALID_PARAM) {
1067 sdw_fill_port_params(&p_rt->port_params,
1068 p_rt->num, pcfg->word_length + 1,
1069 SDW_PORT_FLOW_MODE_ISOCH,
1070 SDW_PORT_DATA_MODE_NORMAL);
1075 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
1076 slave = s_rt->slave;
1077 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
1078 m_port = slave->m_port_map[p_rt->num];
1079 /* port config starts at offset 0 so -1 from actual port number */
1081 pcfg = &ctrl->pconfig[m_port];
1083 pcfg = &ctrl->pconfig[i];
1084 p_rt->transport_params.port_num = p_rt->num;
1085 p_rt->transport_params.sample_interval =
1087 p_rt->transport_params.offset1 = pcfg->off1;
1088 p_rt->transport_params.offset2 = pcfg->off2;
1089 p_rt->transport_params.blk_pkg_mode = pcfg->bp_mode;
1090 p_rt->transport_params.blk_grp_ctrl = pcfg->blk_group_count;
1092 p_rt->transport_params.hstart = pcfg->hstart;
1093 p_rt->transport_params.hstop = pcfg->hstop;
1094 p_rt->transport_params.lane_ctrl = pcfg->lane_control;
1095 if (pcfg->word_length != SWR_INVALID_PARAM) {
1096 sdw_fill_port_params(&p_rt->port_params,
1098 pcfg->word_length + 1,
1099 SDW_PORT_FLOW_MODE_ISOCH,
1100 SDW_PORT_DATA_MODE_NORMAL);
1110 static u32 qcom_swrm_freq_tbl[MAX_FREQ_NUM] = {
1114 static void qcom_swrm_stream_free_ports(struct qcom_swrm_ctrl *ctrl,
1115 struct sdw_stream_runtime *stream)
1117 struct sdw_master_runtime *m_rt;
1118 struct sdw_port_runtime *p_rt;
1119 unsigned long *port_mask;
1121 mutex_lock(&ctrl->port_lock);
1123 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1124 if (m_rt->direction == SDW_DATA_DIR_RX)
1125 port_mask = &ctrl->dout_port_mask;
1127 port_mask = &ctrl->din_port_mask;
1129 list_for_each_entry(p_rt, &m_rt->port_list, port_node)
1130 clear_bit(p_rt->num, port_mask);
1133 mutex_unlock(&ctrl->port_lock);
1136 static int qcom_swrm_stream_alloc_ports(struct qcom_swrm_ctrl *ctrl,
1137 struct sdw_stream_runtime *stream,
1138 struct snd_pcm_hw_params *params,
1141 struct sdw_port_config pconfig[QCOM_SDW_MAX_PORTS];
1142 struct sdw_stream_config sconfig;
1143 struct sdw_master_runtime *m_rt;
1144 struct sdw_slave_runtime *s_rt;
1145 struct sdw_port_runtime *p_rt;
1146 struct sdw_slave *slave;
1147 unsigned long *port_mask;
1148 int i, maxport, pn, nports = 0, ret = 0;
1149 unsigned int m_port;
1151 mutex_lock(&ctrl->port_lock);
1152 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1153 if (m_rt->direction == SDW_DATA_DIR_RX) {
1154 maxport = ctrl->num_dout_ports;
1155 port_mask = &ctrl->dout_port_mask;
1157 maxport = ctrl->num_din_ports;
1158 port_mask = &ctrl->din_port_mask;
1161 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
1162 slave = s_rt->slave;
1163 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
1164 m_port = slave->m_port_map[p_rt->num];
1165 /* Port numbers start from 1 - 14*/
1169 pn = find_first_zero_bit(port_mask, maxport);
1172 dev_err(ctrl->dev, "All ports busy\n");
1176 set_bit(pn, port_mask);
1177 pconfig[nports].num = pn;
1178 pconfig[nports].ch_mask = p_rt->ch_mask;
1184 if (direction == SNDRV_PCM_STREAM_CAPTURE)
1185 sconfig.direction = SDW_DATA_DIR_TX;
1187 sconfig.direction = SDW_DATA_DIR_RX;
1189 /* hw parameters wil be ignored as we only support PDM */
1190 sconfig.ch_count = 1;
1191 sconfig.frame_rate = params_rate(params);
1192 sconfig.type = stream->type;
1194 sdw_stream_add_master(&ctrl->bus, &sconfig, pconfig,
1198 for (i = 0; i < nports; i++)
1199 clear_bit(pconfig[i].num, port_mask);
1202 mutex_unlock(&ctrl->port_lock);
1207 static int qcom_swrm_hw_params(struct snd_pcm_substream *substream,
1208 struct snd_pcm_hw_params *params,
1209 struct snd_soc_dai *dai)
1211 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dai->dev);
1212 struct sdw_stream_runtime *sruntime = ctrl->sruntime[dai->id];
1215 ret = qcom_swrm_stream_alloc_ports(ctrl, sruntime, params,
1218 qcom_swrm_stream_free_ports(ctrl, sruntime);
1223 static int qcom_swrm_hw_free(struct snd_pcm_substream *substream,
1224 struct snd_soc_dai *dai)
1226 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dai->dev);
1227 struct sdw_stream_runtime *sruntime = ctrl->sruntime[dai->id];
1229 qcom_swrm_stream_free_ports(ctrl, sruntime);
1230 sdw_stream_remove_master(&ctrl->bus, sruntime);
1235 static int qcom_swrm_set_sdw_stream(struct snd_soc_dai *dai,
1236 void *stream, int direction)
1238 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dai->dev);
1240 ctrl->sruntime[dai->id] = stream;
1245 static void *qcom_swrm_get_sdw_stream(struct snd_soc_dai *dai, int direction)
1247 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dai->dev);
1249 return ctrl->sruntime[dai->id];
1252 static int qcom_swrm_startup(struct snd_pcm_substream *substream,
1253 struct snd_soc_dai *dai)
1255 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dai->dev);
1256 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1257 struct sdw_stream_runtime *sruntime;
1258 struct snd_soc_dai *codec_dai;
1261 ret = pm_runtime_get_sync(ctrl->dev);
1262 if (ret < 0 && ret != -EACCES) {
1263 dev_err_ratelimited(ctrl->dev,
1264 "pm_runtime_get_sync failed in %s, ret %d\n",
1266 pm_runtime_put_noidle(ctrl->dev);
1270 sruntime = sdw_alloc_stream(dai->name);
1276 ctrl->sruntime[dai->id] = sruntime;
1278 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1279 ret = snd_soc_dai_set_stream(codec_dai, sruntime,
1281 if (ret < 0 && ret != -ENOTSUPP) {
1282 dev_err(dai->dev, "Failed to set sdw stream on %s\n",
1284 goto err_set_stream;
1291 sdw_release_stream(sruntime);
1293 pm_runtime_mark_last_busy(ctrl->dev);
1294 pm_runtime_put_autosuspend(ctrl->dev);
1299 static void qcom_swrm_shutdown(struct snd_pcm_substream *substream,
1300 struct snd_soc_dai *dai)
1302 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dai->dev);
1304 swrm_wait_for_wr_fifo_done(ctrl);
1305 sdw_release_stream(ctrl->sruntime[dai->id]);
1306 ctrl->sruntime[dai->id] = NULL;
1307 pm_runtime_mark_last_busy(ctrl->dev);
1308 pm_runtime_put_autosuspend(ctrl->dev);
1312 static const struct snd_soc_dai_ops qcom_swrm_pdm_dai_ops = {
1313 .hw_params = qcom_swrm_hw_params,
1314 .hw_free = qcom_swrm_hw_free,
1315 .startup = qcom_swrm_startup,
1316 .shutdown = qcom_swrm_shutdown,
1317 .set_stream = qcom_swrm_set_sdw_stream,
1318 .get_stream = qcom_swrm_get_sdw_stream,
1321 static const struct snd_soc_component_driver qcom_swrm_dai_component = {
1322 .name = "soundwire",
1325 static int qcom_swrm_register_dais(struct qcom_swrm_ctrl *ctrl)
1327 int num_dais = ctrl->num_dout_ports + ctrl->num_din_ports;
1328 struct snd_soc_dai_driver *dais;
1329 struct snd_soc_pcm_stream *stream;
1330 struct device *dev = ctrl->dev;
1333 /* PDM dais are only tested for now */
1334 dais = devm_kcalloc(dev, num_dais, sizeof(*dais), GFP_KERNEL);
1338 for (i = 0; i < num_dais; i++) {
1339 dais[i].name = devm_kasprintf(dev, GFP_KERNEL, "SDW Pin%d", i);
1343 if (i < ctrl->num_dout_ports)
1344 stream = &dais[i].playback;
1346 stream = &dais[i].capture;
1348 stream->channels_min = 1;
1349 stream->channels_max = 1;
1350 stream->rates = SNDRV_PCM_RATE_48000;
1351 stream->formats = SNDRV_PCM_FMTBIT_S16_LE;
1353 dais[i].ops = &qcom_swrm_pdm_dai_ops;
1357 return devm_snd_soc_register_component(ctrl->dev,
1358 &qcom_swrm_dai_component,
1362 static int qcom_swrm_get_port_config(struct qcom_swrm_ctrl *ctrl)
1364 struct device_node *np = ctrl->dev->of_node;
1365 u8 off1[QCOM_SDW_MAX_PORTS];
1366 u8 off2[QCOM_SDW_MAX_PORTS];
1367 u16 si[QCOM_SDW_MAX_PORTS];
1368 u8 bp_mode[QCOM_SDW_MAX_PORTS] = { 0, };
1369 u8 hstart[QCOM_SDW_MAX_PORTS];
1370 u8 hstop[QCOM_SDW_MAX_PORTS];
1371 u8 word_length[QCOM_SDW_MAX_PORTS];
1372 u8 blk_group_count[QCOM_SDW_MAX_PORTS];
1373 u8 lane_control[QCOM_SDW_MAX_PORTS];
1374 int i, ret, nports, val;
1377 ctrl->reg_read(ctrl, SWRM_COMP_PARAMS, &val);
1379 ctrl->num_dout_ports = FIELD_GET(SWRM_COMP_PARAMS_DOUT_PORTS_MASK, val);
1380 ctrl->num_din_ports = FIELD_GET(SWRM_COMP_PARAMS_DIN_PORTS_MASK, val);
1382 ret = of_property_read_u32(np, "qcom,din-ports", &val);
1386 if (val > ctrl->num_din_ports)
1389 ctrl->num_din_ports = val;
1391 ret = of_property_read_u32(np, "qcom,dout-ports", &val);
1395 if (val > ctrl->num_dout_ports)
1398 ctrl->num_dout_ports = val;
1400 nports = ctrl->num_dout_ports + ctrl->num_din_ports;
1401 if (nports > QCOM_SDW_MAX_PORTS)
1404 /* Valid port numbers are from 1-14, so mask out port 0 explicitly */
1405 set_bit(0, &ctrl->dout_port_mask);
1406 set_bit(0, &ctrl->din_port_mask);
1408 ret = of_property_read_u8_array(np, "qcom,ports-offset1",
1413 ret = of_property_read_u8_array(np, "qcom,ports-offset2",
1418 ret = of_property_read_u8_array(np, "qcom,ports-sinterval-low",
1421 ret = of_property_read_u16_array(np, "qcom,ports-sinterval",
1428 ret = of_property_read_u8_array(np, "qcom,ports-block-pack-mode",
1431 if (ctrl->version <= SWRM_VERSION_1_3_0)
1432 memset(bp_mode, SWR_INVALID_PARAM, QCOM_SDW_MAX_PORTS);
1437 memset(hstart, SWR_INVALID_PARAM, QCOM_SDW_MAX_PORTS);
1438 of_property_read_u8_array(np, "qcom,ports-hstart", hstart, nports);
1440 memset(hstop, SWR_INVALID_PARAM, QCOM_SDW_MAX_PORTS);
1441 of_property_read_u8_array(np, "qcom,ports-hstop", hstop, nports);
1443 memset(word_length, SWR_INVALID_PARAM, QCOM_SDW_MAX_PORTS);
1444 of_property_read_u8_array(np, "qcom,ports-word-length", word_length, nports);
1446 memset(blk_group_count, SWR_INVALID_PARAM, QCOM_SDW_MAX_PORTS);
1447 of_property_read_u8_array(np, "qcom,ports-block-group-count", blk_group_count, nports);
1449 memset(lane_control, SWR_INVALID_PARAM, QCOM_SDW_MAX_PORTS);
1450 of_property_read_u8_array(np, "qcom,ports-lane-control", lane_control, nports);
1452 for (i = 0; i < nports; i++) {
1453 /* Valid port number range is from 1-14 */
1455 ctrl->pconfig[i + 1].si = si[i];
1457 ctrl->pconfig[i + 1].si = ((u8 *)si)[i];
1458 ctrl->pconfig[i + 1].off1 = off1[i];
1459 ctrl->pconfig[i + 1].off2 = off2[i];
1460 ctrl->pconfig[i + 1].bp_mode = bp_mode[i];
1461 ctrl->pconfig[i + 1].hstart = hstart[i];
1462 ctrl->pconfig[i + 1].hstop = hstop[i];
1463 ctrl->pconfig[i + 1].word_length = word_length[i];
1464 ctrl->pconfig[i + 1].blk_group_count = blk_group_count[i];
1465 ctrl->pconfig[i + 1].lane_control = lane_control[i];
1471 #ifdef CONFIG_DEBUG_FS
1472 static int swrm_reg_show(struct seq_file *s_file, void *data)
1474 struct qcom_swrm_ctrl *ctrl = s_file->private;
1475 int reg, reg_val, ret;
1477 ret = pm_runtime_get_sync(ctrl->dev);
1478 if (ret < 0 && ret != -EACCES) {
1479 dev_err_ratelimited(ctrl->dev,
1480 "pm_runtime_get_sync failed in %s, ret %d\n",
1482 pm_runtime_put_noidle(ctrl->dev);
1486 for (reg = 0; reg <= ctrl->max_reg; reg += 4) {
1487 ctrl->reg_read(ctrl, reg, ®_val);
1488 seq_printf(s_file, "0x%.3x: 0x%.2x\n", reg, reg_val);
1490 pm_runtime_mark_last_busy(ctrl->dev);
1491 pm_runtime_put_autosuspend(ctrl->dev);
1496 DEFINE_SHOW_ATTRIBUTE(swrm_reg);
1499 static int qcom_swrm_probe(struct platform_device *pdev)
1501 struct device *dev = &pdev->dev;
1502 struct sdw_master_prop *prop;
1503 struct sdw_bus_params *params;
1504 struct qcom_swrm_ctrl *ctrl;
1505 const struct qcom_swrm_data *data;
1509 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
1513 data = of_device_get_match_data(dev);
1514 ctrl->max_reg = data->max_reg;
1515 ctrl->reg_layout = data->reg_layout;
1516 ctrl->rows_index = sdw_find_row_index(data->default_rows);
1517 ctrl->cols_index = sdw_find_col_index(data->default_cols);
1518 #if IS_REACHABLE(CONFIG_SLIMBUS)
1519 if (dev->parent->bus == &slimbus_bus) {
1523 ctrl->reg_read = qcom_swrm_ahb_reg_read;
1524 ctrl->reg_write = qcom_swrm_ahb_reg_write;
1525 ctrl->regmap = dev_get_regmap(dev->parent, NULL);
1529 ctrl->reg_read = qcom_swrm_cpu_reg_read;
1530 ctrl->reg_write = qcom_swrm_cpu_reg_write;
1531 ctrl->mmio = devm_platform_ioremap_resource(pdev, 0);
1532 if (IS_ERR(ctrl->mmio))
1533 return PTR_ERR(ctrl->mmio);
1536 if (data->sw_clk_gate_required) {
1537 ctrl->audio_cgcr = devm_reset_control_get_optional_exclusive(dev, "swr_audio_cgcr");
1538 if (IS_ERR(ctrl->audio_cgcr)) {
1539 dev_err(dev, "Failed to get cgcr reset ctrl required for SW gating\n");
1540 ret = PTR_ERR(ctrl->audio_cgcr);
1545 ctrl->irq = of_irq_get(dev->of_node, 0);
1546 if (ctrl->irq < 0) {
1551 ctrl->hclk = devm_clk_get(dev, "iface");
1552 if (IS_ERR(ctrl->hclk)) {
1553 ret = PTR_ERR(ctrl->hclk);
1557 clk_prepare_enable(ctrl->hclk);
1560 dev_set_drvdata(&pdev->dev, ctrl);
1561 mutex_init(&ctrl->port_lock);
1562 init_completion(&ctrl->broadcast);
1563 init_completion(&ctrl->enumeration);
1565 ctrl->bus.ops = &qcom_swrm_ops;
1566 ctrl->bus.port_ops = &qcom_swrm_port_ops;
1567 ctrl->bus.compute_params = &qcom_swrm_compute_params;
1568 ctrl->bus.clk_stop_timeout = 300;
1570 ret = qcom_swrm_get_port_config(ctrl);
1574 params = &ctrl->bus.params;
1575 params->max_dr_freq = DEFAULT_CLK_FREQ;
1576 params->curr_dr_freq = DEFAULT_CLK_FREQ;
1577 params->col = data->default_cols;
1578 params->row = data->default_rows;
1579 ctrl->reg_read(ctrl, SWRM_MCP_STATUS, &val);
1580 params->curr_bank = val & SWRM_MCP_STATUS_BANK_NUM_MASK;
1581 params->next_bank = !params->curr_bank;
1583 prop = &ctrl->bus.prop;
1584 prop->max_clk_freq = DEFAULT_CLK_FREQ;
1585 prop->num_clk_gears = 0;
1586 prop->num_clk_freq = MAX_FREQ_NUM;
1587 prop->clk_freq = &qcom_swrm_freq_tbl[0];
1588 prop->default_col = data->default_cols;
1589 prop->default_row = data->default_rows;
1591 ctrl->reg_read(ctrl, SWRM_COMP_HW_VERSION, &ctrl->version);
1593 ret = devm_request_threaded_irq(dev, ctrl->irq, NULL,
1594 qcom_swrm_irq_handler,
1595 IRQF_TRIGGER_RISING |
1599 dev_err(dev, "Failed to request soundwire irq\n");
1603 ctrl->wake_irq = of_irq_get(dev->of_node, 1);
1604 if (ctrl->wake_irq > 0) {
1605 ret = devm_request_threaded_irq(dev, ctrl->wake_irq, NULL,
1606 qcom_swrm_wake_irq_handler,
1607 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1608 "swr_wake_irq", ctrl);
1610 dev_err(dev, "Failed to request soundwire wake irq\n");
1615 ret = sdw_bus_master_add(&ctrl->bus, dev, dev->fwnode);
1617 dev_err(dev, "Failed to register Soundwire controller (%d)\n",
1622 qcom_swrm_init(ctrl);
1623 wait_for_completion_timeout(&ctrl->enumeration,
1624 msecs_to_jiffies(TIMEOUT_MS));
1625 ret = qcom_swrm_register_dais(ctrl);
1627 goto err_master_add;
1629 dev_info(dev, "Qualcomm Soundwire controller v%x.%x.%x Registered\n",
1630 (ctrl->version >> 24) & 0xff, (ctrl->version >> 16) & 0xff,
1631 ctrl->version & 0xffff);
1633 pm_runtime_set_autosuspend_delay(dev, 3000);
1634 pm_runtime_use_autosuspend(dev);
1635 pm_runtime_mark_last_busy(dev);
1636 pm_runtime_set_active(dev);
1637 pm_runtime_enable(dev);
1639 #ifdef CONFIG_DEBUG_FS
1640 ctrl->debugfs = debugfs_create_dir("qualcomm-sdw", ctrl->bus.debugfs);
1641 debugfs_create_file("qualcomm-registers", 0400, ctrl->debugfs, ctrl,
1648 sdw_bus_master_delete(&ctrl->bus);
1650 clk_disable_unprepare(ctrl->hclk);
1655 static int qcom_swrm_remove(struct platform_device *pdev)
1657 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(&pdev->dev);
1659 sdw_bus_master_delete(&ctrl->bus);
1660 clk_disable_unprepare(ctrl->hclk);
1665 static int __maybe_unused swrm_runtime_resume(struct device *dev)
1667 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dev);
1670 if (ctrl->wake_irq > 0) {
1671 if (!irqd_irq_disabled(irq_get_irq_data(ctrl->wake_irq)))
1672 disable_irq_nosync(ctrl->wake_irq);
1675 clk_prepare_enable(ctrl->hclk);
1677 if (ctrl->clock_stop_not_supported) {
1678 reinit_completion(&ctrl->enumeration);
1679 ctrl->reg_write(ctrl, SWRM_COMP_SW_RESET, 0x01);
1680 usleep_range(100, 105);
1682 qcom_swrm_init(ctrl);
1684 usleep_range(100, 105);
1685 if (!swrm_wait_for_frame_gen_enabled(ctrl))
1686 dev_err(ctrl->dev, "link failed to connect\n");
1688 /* wait for hw enumeration to complete */
1689 wait_for_completion_timeout(&ctrl->enumeration,
1690 msecs_to_jiffies(TIMEOUT_MS));
1691 qcom_swrm_get_device_status(ctrl);
1692 sdw_handle_slave_status(&ctrl->bus, ctrl->status);
1694 reset_control_reset(ctrl->audio_cgcr);
1696 if (ctrl->version == SWRM_VERSION_1_7_0) {
1697 ctrl->reg_write(ctrl, SWRM_LINK_MANAGER_EE, SWRM_EE_CPU);
1698 ctrl->reg_write(ctrl, SWRM_MCP_BUS_CTRL,
1699 SWRM_MCP_BUS_CLK_START << SWRM_EE_CPU);
1700 } else if (ctrl->version >= SWRM_VERSION_2_0_0) {
1701 ctrl->reg_write(ctrl, SWRM_LINK_MANAGER_EE, SWRM_EE_CPU);
1702 ctrl->reg_write(ctrl, SWRM_V2_0_CLK_CTRL,
1703 SWRM_V2_0_CLK_CTRL_CLK_START);
1705 ctrl->reg_write(ctrl, SWRM_MCP_BUS_CTRL, SWRM_MCP_BUS_CLK_START);
1707 ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_CLEAR],
1708 SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET);
1710 ctrl->intr_mask |= SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET;
1711 if (ctrl->version < SWRM_VERSION_2_0_0)
1712 ctrl->reg_write(ctrl,
1713 ctrl->reg_layout[SWRM_REG_INTERRUPT_MASK_ADDR],
1715 ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_CPU_EN],
1718 usleep_range(100, 105);
1719 if (!swrm_wait_for_frame_gen_enabled(ctrl))
1720 dev_err(ctrl->dev, "link failed to connect\n");
1722 ret = sdw_bus_exit_clk_stop(&ctrl->bus);
1724 dev_err(ctrl->dev, "bus failed to exit clock stop %d\n", ret);
1730 static int __maybe_unused swrm_runtime_suspend(struct device *dev)
1732 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dev);
1735 swrm_wait_for_wr_fifo_done(ctrl);
1736 if (!ctrl->clock_stop_not_supported) {
1737 /* Mask bus clash interrupt */
1738 ctrl->intr_mask &= ~SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET;
1739 if (ctrl->version < SWRM_VERSION_2_0_0)
1740 ctrl->reg_write(ctrl,
1741 ctrl->reg_layout[SWRM_REG_INTERRUPT_MASK_ADDR],
1743 ctrl->reg_write(ctrl, ctrl->reg_layout[SWRM_REG_INTERRUPT_CPU_EN],
1745 /* Prepare slaves for clock stop */
1746 ret = sdw_bus_prep_clk_stop(&ctrl->bus);
1747 if (ret < 0 && ret != -ENODATA) {
1748 dev_err(dev, "prepare clock stop failed %d", ret);
1752 ret = sdw_bus_clk_stop(&ctrl->bus);
1753 if (ret < 0 && ret != -ENODATA) {
1754 dev_err(dev, "bus clock stop failed %d", ret);
1759 clk_disable_unprepare(ctrl->hclk);
1761 usleep_range(300, 305);
1763 if (ctrl->wake_irq > 0) {
1764 if (irqd_irq_disabled(irq_get_irq_data(ctrl->wake_irq)))
1765 enable_irq(ctrl->wake_irq);
1771 static const struct dev_pm_ops swrm_dev_pm_ops = {
1772 SET_RUNTIME_PM_OPS(swrm_runtime_suspend, swrm_runtime_resume, NULL)
1775 static const struct of_device_id qcom_swrm_of_match[] = {
1776 { .compatible = "qcom,soundwire-v1.3.0", .data = &swrm_v1_3_data },
1777 { .compatible = "qcom,soundwire-v1.5.1", .data = &swrm_v1_5_data },
1778 { .compatible = "qcom,soundwire-v1.6.0", .data = &swrm_v1_6_data },
1779 { .compatible = "qcom,soundwire-v1.7.0", .data = &swrm_v1_5_data },
1780 { .compatible = "qcom,soundwire-v2.0.0", .data = &swrm_v2_0_data },
1784 MODULE_DEVICE_TABLE(of, qcom_swrm_of_match);
1786 static struct platform_driver qcom_swrm_driver = {
1787 .probe = &qcom_swrm_probe,
1788 .remove = &qcom_swrm_remove,
1790 .name = "qcom-soundwire",
1791 .of_match_table = qcom_swrm_of_match,
1792 .pm = &swrm_dev_pm_ops,
1795 module_platform_driver(qcom_swrm_driver);
1797 MODULE_DESCRIPTION("Qualcomm soundwire driver");
1798 MODULE_LICENSE("GPL v2");