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>
11 #include <linux/of_irq.h>
12 #include <linux/of_device.h>
13 #include <linux/regmap.h>
14 #include <linux/slab.h>
15 #include <linux/slimbus.h>
16 #include <linux/soundwire/sdw.h>
17 #include <linux/soundwire/sdw_registers.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
22 #define SWRM_COMP_HW_VERSION 0x00
23 #define SWRM_COMP_CFG_ADDR 0x04
24 #define SWRM_COMP_CFG_IRQ_LEVEL_OR_PULSE_MSK BIT(1)
25 #define SWRM_COMP_CFG_ENABLE_MSK BIT(0)
26 #define SWRM_COMP_PARAMS 0x100
27 #define SWRM_COMP_PARAMS_DOUT_PORTS_MASK GENMASK(4, 0)
28 #define SWRM_COMP_PARAMS_DIN_PORTS_MASK GENMASK(9, 5)
29 #define SWRM_INTERRUPT_STATUS 0x200
30 #define SWRM_INTERRUPT_STATUS_RMSK GENMASK(16, 0)
31 #define SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED BIT(1)
32 #define SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS BIT(2)
33 #define SWRM_INTERRUPT_STATUS_CMD_ERROR BIT(7)
34 #define SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED BIT(10)
35 #define SWRM_INTERRUPT_MASK_ADDR 0x204
36 #define SWRM_INTERRUPT_CLEAR 0x208
37 #define SWRM_CMD_FIFO_WR_CMD 0x300
38 #define SWRM_CMD_FIFO_RD_CMD 0x304
39 #define SWRM_CMD_FIFO_CMD 0x308
40 #define SWRM_CMD_FIFO_STATUS 0x30C
41 #define SWRM_CMD_FIFO_CFG_ADDR 0x314
42 #define SWRM_RD_WR_CMD_RETRIES 0x7
43 #define SWRM_CMD_FIFO_RD_FIFO_ADDR 0x318
44 #define SWRM_ENUMERATOR_CFG_ADDR 0x500
45 #define SWRM_MCP_FRAME_CTRL_BANK_ADDR(m) (0x101C + 0x40 * (m))
46 #define SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT 3
47 #define SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK GENMASK(2, 0)
48 #define SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK GENMASK(7, 3)
49 #define SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT 0
50 #define SWRM_MCP_CFG_ADDR 0x1048
51 #define SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK GENMASK(21, 17)
52 #define SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT 0x11
53 #define SWRM_DEF_CMD_NO_PINGS 0x1f
54 #define SWRM_MCP_STATUS 0x104C
55 #define SWRM_MCP_STATUS_BANK_NUM_MASK BIT(0)
56 #define SWRM_MCP_SLV_STATUS 0x1090
57 #define SWRM_MCP_SLV_STATUS_MASK GENMASK(1, 0)
58 #define SWRM_DP_PORT_CTRL_BANK(n, m) (0x1124 + 0x100 * (n - 1) + 0x40 * m)
59 #define SWRM_DP_PORT_CTRL_EN_CHAN_SHFT 0x18
60 #define SWRM_DP_PORT_CTRL_OFFSET2_SHFT 0x10
61 #define SWRM_DP_PORT_CTRL_OFFSET1_SHFT 0x08
62 #define SWRM_AHB_BRIDGE_WR_DATA_0 0xc85
63 #define SWRM_AHB_BRIDGE_WR_ADDR_0 0xc89
64 #define SWRM_AHB_BRIDGE_RD_ADDR_0 0xc8d
65 #define SWRM_AHB_BRIDGE_RD_DATA_0 0xc91
67 #define SWRM_REG_VAL_PACK(data, dev, id, reg) \
68 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
70 #define SWRM_MAX_ROW_VAL 0 /* Rows = 48 */
71 #define SWRM_DEFAULT_ROWS 48
72 #define SWRM_MIN_COL_VAL 0 /* Cols = 2 */
73 #define SWRM_DEFAULT_COL 16
74 #define SWRM_MAX_COL_VAL 7
75 #define SWRM_SPECIAL_CMD_ID 0xF
76 #define MAX_FREQ_NUM 1
77 #define TIMEOUT_MS (2 * HZ)
78 #define QCOM_SWRM_MAX_RD_LEN 0xf
79 #define QCOM_SDW_MAX_PORTS 14
80 #define DEFAULT_CLK_FREQ 9600000
81 #define SWRM_MAX_DAIS 0xF
83 struct qcom_swrm_port_config
{
89 struct qcom_swrm_ctrl
{
92 struct regmap
*regmap
;
93 struct completion
*comp
;
94 struct work_struct slave_work
;
97 /* Port alloc/free lock */
98 struct mutex port_lock
;
103 unsigned int version
;
106 unsigned long dout_port_mask
;
107 unsigned long din_port_mask
;
108 struct qcom_swrm_port_config pconfig
[QCOM_SDW_MAX_PORTS
];
109 struct sdw_stream_runtime
*sruntime
[SWRM_MAX_DAIS
];
110 enum sdw_slave_status status
[SDW_MAX_DEVICES
];
111 int (*reg_read
)(struct qcom_swrm_ctrl
*ctrl
, int reg
, u32
*val
);
112 int (*reg_write
)(struct qcom_swrm_ctrl
*ctrl
, int reg
, int val
);
115 #define to_qcom_sdw(b) container_of(b, struct qcom_swrm_ctrl, bus)
117 static int qcom_swrm_abh_reg_read(struct qcom_swrm_ctrl
*ctrl
, int reg
,
120 struct regmap
*wcd_regmap
= ctrl
->regmap
;
123 /* pg register + offset */
124 ret
= regmap_bulk_write(wcd_regmap
, SWRM_AHB_BRIDGE_RD_ADDR_0
,
129 ret
= regmap_bulk_read(wcd_regmap
, SWRM_AHB_BRIDGE_RD_DATA_0
,
137 static int qcom_swrm_ahb_reg_write(struct qcom_swrm_ctrl
*ctrl
,
140 struct regmap
*wcd_regmap
= ctrl
->regmap
;
142 /* pg register + offset */
143 ret
= regmap_bulk_write(wcd_regmap
, SWRM_AHB_BRIDGE_WR_DATA_0
,
148 /* write address register */
149 ret
= regmap_bulk_write(wcd_regmap
, SWRM_AHB_BRIDGE_WR_ADDR_0
,
157 static int qcom_swrm_cmd_fifo_wr_cmd(struct qcom_swrm_ctrl
*ctrl
, u8 cmd_data
,
158 u8 dev_addr
, u16 reg_addr
)
160 DECLARE_COMPLETION_ONSTACK(comp
);
165 spin_lock_irqsave(&ctrl
->comp_lock
, flags
);
167 spin_unlock_irqrestore(&ctrl
->comp_lock
, flags
);
168 val
= SWRM_REG_VAL_PACK(cmd_data
, dev_addr
,
169 SWRM_SPECIAL_CMD_ID
, reg_addr
);
170 ret
= ctrl
->reg_write(ctrl
, SWRM_CMD_FIFO_WR_CMD
, val
);
174 ret
= wait_for_completion_timeout(ctrl
->comp
,
175 msecs_to_jiffies(TIMEOUT_MS
));
178 ret
= SDW_CMD_IGNORED
;
182 spin_lock_irqsave(&ctrl
->comp_lock
, flags
);
184 spin_unlock_irqrestore(&ctrl
->comp_lock
, flags
);
189 static int qcom_swrm_cmd_fifo_rd_cmd(struct qcom_swrm_ctrl
*ctrl
,
190 u8 dev_addr
, u16 reg_addr
,
195 DECLARE_COMPLETION_ONSTACK(comp
);
198 spin_lock_irqsave(&ctrl
->comp_lock
, flags
);
200 spin_unlock_irqrestore(&ctrl
->comp_lock
, flags
);
202 val
= SWRM_REG_VAL_PACK(len
, dev_addr
, SWRM_SPECIAL_CMD_ID
, reg_addr
);
203 ret
= ctrl
->reg_write(ctrl
, SWRM_CMD_FIFO_RD_CMD
, val
);
207 ret
= wait_for_completion_timeout(ctrl
->comp
,
208 msecs_to_jiffies(TIMEOUT_MS
));
211 ret
= SDW_CMD_IGNORED
;
217 for (i
= 0; i
< len
; i
++) {
218 ctrl
->reg_read(ctrl
, SWRM_CMD_FIFO_RD_FIFO_ADDR
, &val
);
219 rval
[i
] = val
& 0xFF;
223 spin_lock_irqsave(&ctrl
->comp_lock
, flags
);
225 spin_unlock_irqrestore(&ctrl
->comp_lock
, flags
);
230 static void qcom_swrm_get_device_status(struct qcom_swrm_ctrl
*ctrl
)
235 ctrl
->reg_read(ctrl
, SWRM_MCP_SLV_STATUS
, &val
);
237 for (i
= 0; i
< SDW_MAX_DEVICES
; i
++) {
240 s
= (val
>> (i
* 2));
241 s
&= SWRM_MCP_SLV_STATUS_MASK
;
246 static irqreturn_t
qcom_swrm_irq_handler(int irq
, void *dev_id
)
248 struct qcom_swrm_ctrl
*ctrl
= dev_id
;
252 ctrl
->reg_read(ctrl
, SWRM_INTERRUPT_STATUS
, &sts
);
254 if (sts
& SWRM_INTERRUPT_STATUS_CMD_ERROR
) {
255 ctrl
->reg_read(ctrl
, SWRM_CMD_FIFO_STATUS
, &value
);
256 dev_err_ratelimited(ctrl
->dev
,
257 "CMD error, fifo status 0x%x\n",
259 ctrl
->reg_write(ctrl
, SWRM_CMD_FIFO_CMD
, 0x1);
262 if ((sts
& SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED
) ||
263 sts
& SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS
)
264 schedule_work(&ctrl
->slave_work
);
267 * clear the interrupt before complete() is called, as complete can
268 * schedule new read/writes which require interrupts, clearing the
269 * interrupt would avoid missing interrupts in such cases.
271 ctrl
->reg_write(ctrl
, SWRM_INTERRUPT_CLEAR
, sts
);
273 if (sts
& SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED
) {
274 spin_lock_irqsave(&ctrl
->comp_lock
, flags
);
276 complete(ctrl
->comp
);
277 spin_unlock_irqrestore(&ctrl
->comp_lock
, flags
);
282 static int qcom_swrm_init(struct qcom_swrm_ctrl
*ctrl
)
286 /* Clear Rows and Cols */
287 val
= (SWRM_MAX_ROW_VAL
<< SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT
|
288 SWRM_MIN_COL_VAL
<< SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT
);
290 ctrl
->reg_write(ctrl
, SWRM_MCP_FRAME_CTRL_BANK_ADDR(0), val
);
292 /* Disable Auto enumeration */
293 ctrl
->reg_write(ctrl
, SWRM_ENUMERATOR_CFG_ADDR
, 0);
295 /* Mask soundwire interrupts */
296 ctrl
->reg_write(ctrl
, SWRM_INTERRUPT_MASK_ADDR
,
297 SWRM_INTERRUPT_STATUS_RMSK
);
299 /* Configure No pings */
300 ctrl
->reg_read(ctrl
, SWRM_MCP_CFG_ADDR
, &val
);
301 val
&= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK
;
302 val
|= (SWRM_DEF_CMD_NO_PINGS
<<
303 SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT
);
304 ctrl
->reg_write(ctrl
, SWRM_MCP_CFG_ADDR
, val
);
306 /* Configure number of retries of a read/write cmd */
307 ctrl
->reg_write(ctrl
, SWRM_CMD_FIFO_CFG_ADDR
, SWRM_RD_WR_CMD_RETRIES
);
309 /* Set IRQ to PULSE */
310 ctrl
->reg_write(ctrl
, SWRM_COMP_CFG_ADDR
,
311 SWRM_COMP_CFG_IRQ_LEVEL_OR_PULSE_MSK
|
312 SWRM_COMP_CFG_ENABLE_MSK
);
316 static enum sdw_command_response
qcom_swrm_xfer_msg(struct sdw_bus
*bus
,
319 struct qcom_swrm_ctrl
*ctrl
= to_qcom_sdw(bus
);
322 if (msg
->flags
== SDW_MSG_FLAG_READ
) {
323 for (i
= 0; i
< msg
->len
;) {
324 if ((msg
->len
- i
) < QCOM_SWRM_MAX_RD_LEN
)
327 len
= QCOM_SWRM_MAX_RD_LEN
;
329 ret
= qcom_swrm_cmd_fifo_rd_cmd(ctrl
, msg
->dev_num
,
337 } else if (msg
->flags
== SDW_MSG_FLAG_WRITE
) {
338 for (i
= 0; i
< msg
->len
; i
++) {
339 ret
= qcom_swrm_cmd_fifo_wr_cmd(ctrl
, msg
->buf
[i
],
343 return SDW_CMD_IGNORED
;
350 static int qcom_swrm_pre_bank_switch(struct sdw_bus
*bus
)
352 u32 reg
= SWRM_MCP_FRAME_CTRL_BANK_ADDR(bus
->params
.next_bank
);
353 struct qcom_swrm_ctrl
*ctrl
= to_qcom_sdw(bus
);
356 ctrl
->reg_read(ctrl
, reg
, &val
);
358 val
&= ~SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK
;
359 val
&= ~SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK
;
361 val
|= (SWRM_MAX_ROW_VAL
<< SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT
|
362 SWRM_MAX_COL_VAL
<< SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT
);
364 return ctrl
->reg_write(ctrl
, reg
, val
);
367 static int qcom_swrm_port_params(struct sdw_bus
*bus
,
368 struct sdw_port_params
*p_params
,
375 static int qcom_swrm_transport_params(struct sdw_bus
*bus
,
376 struct sdw_transport_params
*params
,
377 enum sdw_reg_bank bank
)
379 struct qcom_swrm_ctrl
*ctrl
= to_qcom_sdw(bus
);
382 value
= params
->offset1
<< SWRM_DP_PORT_CTRL_OFFSET1_SHFT
;
383 value
|= params
->offset2
<< SWRM_DP_PORT_CTRL_OFFSET2_SHFT
;
384 value
|= params
->sample_interval
- 1;
386 return ctrl
->reg_write(ctrl
,
387 SWRM_DP_PORT_CTRL_BANK((params
->port_num
), bank
),
391 static int qcom_swrm_port_enable(struct sdw_bus
*bus
,
392 struct sdw_enable_ch
*enable_ch
,
395 u32 reg
= SWRM_DP_PORT_CTRL_BANK(enable_ch
->port_num
, bank
);
396 struct qcom_swrm_ctrl
*ctrl
= to_qcom_sdw(bus
);
399 ctrl
->reg_read(ctrl
, reg
, &val
);
401 if (enable_ch
->enable
)
402 val
|= (enable_ch
->ch_mask
<< SWRM_DP_PORT_CTRL_EN_CHAN_SHFT
);
404 val
&= ~(0xff << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT
);
406 return ctrl
->reg_write(ctrl
, reg
, val
);
409 static const struct sdw_master_port_ops qcom_swrm_port_ops
= {
410 .dpn_set_port_params
= qcom_swrm_port_params
,
411 .dpn_set_port_transport_params
= qcom_swrm_transport_params
,
412 .dpn_port_enable_ch
= qcom_swrm_port_enable
,
415 static const struct sdw_master_ops qcom_swrm_ops
= {
416 .xfer_msg
= qcom_swrm_xfer_msg
,
417 .pre_bank_switch
= qcom_swrm_pre_bank_switch
,
420 static int qcom_swrm_compute_params(struct sdw_bus
*bus
)
422 struct qcom_swrm_ctrl
*ctrl
= to_qcom_sdw(bus
);
423 struct sdw_master_runtime
*m_rt
;
424 struct sdw_slave_runtime
*s_rt
;
425 struct sdw_port_runtime
*p_rt
;
426 struct qcom_swrm_port_config
*pcfg
;
429 list_for_each_entry(m_rt
, &bus
->m_rt_list
, bus_node
) {
430 list_for_each_entry(p_rt
, &m_rt
->port_list
, port_node
) {
431 pcfg
= &ctrl
->pconfig
[p_rt
->num
- 1];
432 p_rt
->transport_params
.port_num
= p_rt
->num
;
433 p_rt
->transport_params
.sample_interval
= pcfg
->si
+ 1;
434 p_rt
->transport_params
.offset1
= pcfg
->off1
;
435 p_rt
->transport_params
.offset2
= pcfg
->off2
;
438 list_for_each_entry(s_rt
, &m_rt
->slave_rt_list
, m_rt_node
) {
439 list_for_each_entry(p_rt
, &s_rt
->port_list
, port_node
) {
440 pcfg
= &ctrl
->pconfig
[i
];
441 p_rt
->transport_params
.port_num
= p_rt
->num
;
442 p_rt
->transport_params
.sample_interval
=
444 p_rt
->transport_params
.offset1
= pcfg
->off1
;
445 p_rt
->transport_params
.offset2
= pcfg
->off2
;
454 static u32 qcom_swrm_freq_tbl
[MAX_FREQ_NUM
] = {
458 static void qcom_swrm_slave_wq(struct work_struct
*work
)
460 struct qcom_swrm_ctrl
*ctrl
=
461 container_of(work
, struct qcom_swrm_ctrl
, slave_work
);
463 qcom_swrm_get_device_status(ctrl
);
464 sdw_handle_slave_status(&ctrl
->bus
, ctrl
->status
);
468 static void qcom_swrm_stream_free_ports(struct qcom_swrm_ctrl
*ctrl
,
469 struct sdw_stream_runtime
*stream
)
471 struct sdw_master_runtime
*m_rt
;
472 struct sdw_port_runtime
*p_rt
;
473 unsigned long *port_mask
;
475 mutex_lock(&ctrl
->port_lock
);
477 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
478 if (m_rt
->direction
== SDW_DATA_DIR_RX
)
479 port_mask
= &ctrl
->dout_port_mask
;
481 port_mask
= &ctrl
->din_port_mask
;
483 list_for_each_entry(p_rt
, &m_rt
->port_list
, port_node
)
484 clear_bit(p_rt
->num
- 1, port_mask
);
487 mutex_unlock(&ctrl
->port_lock
);
490 static int qcom_swrm_stream_alloc_ports(struct qcom_swrm_ctrl
*ctrl
,
491 struct sdw_stream_runtime
*stream
,
492 struct snd_pcm_hw_params
*params
,
495 struct sdw_port_config pconfig
[QCOM_SDW_MAX_PORTS
];
496 struct sdw_stream_config sconfig
;
497 struct sdw_master_runtime
*m_rt
;
498 struct sdw_slave_runtime
*s_rt
;
499 struct sdw_port_runtime
*p_rt
;
500 unsigned long *port_mask
;
501 int i
, maxport
, pn
, nports
= 0, ret
= 0;
503 mutex_lock(&ctrl
->port_lock
);
504 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
505 if (m_rt
->direction
== SDW_DATA_DIR_RX
) {
506 maxport
= ctrl
->num_dout_ports
;
507 port_mask
= &ctrl
->dout_port_mask
;
509 maxport
= ctrl
->num_din_ports
;
510 port_mask
= &ctrl
->din_port_mask
;
513 list_for_each_entry(s_rt
, &m_rt
->slave_rt_list
, m_rt_node
) {
514 list_for_each_entry(p_rt
, &s_rt
->port_list
, port_node
) {
515 /* Port numbers start from 1 - 14*/
516 pn
= find_first_zero_bit(port_mask
, maxport
);
517 if (pn
> (maxport
- 1)) {
518 dev_err(ctrl
->dev
, "All ports busy\n");
522 set_bit(pn
, port_mask
);
523 pconfig
[nports
].num
= pn
+ 1;
524 pconfig
[nports
].ch_mask
= p_rt
->ch_mask
;
530 if (direction
== SNDRV_PCM_STREAM_CAPTURE
)
531 sconfig
.direction
= SDW_DATA_DIR_TX
;
533 sconfig
.direction
= SDW_DATA_DIR_RX
;
535 /* hw parameters wil be ignored as we only support PDM */
536 sconfig
.ch_count
= 1;
537 sconfig
.frame_rate
= params_rate(params
);
538 sconfig
.type
= stream
->type
;
540 sdw_stream_add_master(&ctrl
->bus
, &sconfig
, pconfig
,
544 for (i
= 0; i
< nports
; i
++)
545 clear_bit(pconfig
[i
].num
- 1, port_mask
);
548 mutex_unlock(&ctrl
->port_lock
);
553 static int qcom_swrm_hw_params(struct snd_pcm_substream
*substream
,
554 struct snd_pcm_hw_params
*params
,
555 struct snd_soc_dai
*dai
)
557 struct qcom_swrm_ctrl
*ctrl
= dev_get_drvdata(dai
->dev
);
558 struct sdw_stream_runtime
*sruntime
= ctrl
->sruntime
[dai
->id
];
561 ret
= qcom_swrm_stream_alloc_ports(ctrl
, sruntime
, params
,
564 qcom_swrm_stream_free_ports(ctrl
, sruntime
);
569 static int qcom_swrm_hw_free(struct snd_pcm_substream
*substream
,
570 struct snd_soc_dai
*dai
)
572 struct qcom_swrm_ctrl
*ctrl
= dev_get_drvdata(dai
->dev
);
573 struct sdw_stream_runtime
*sruntime
= ctrl
->sruntime
[dai
->id
];
575 qcom_swrm_stream_free_ports(ctrl
, sruntime
);
576 sdw_stream_remove_master(&ctrl
->bus
, sruntime
);
581 static int qcom_swrm_set_sdw_stream(struct snd_soc_dai
*dai
,
582 void *stream
, int direction
)
584 struct qcom_swrm_ctrl
*ctrl
= dev_get_drvdata(dai
->dev
);
586 ctrl
->sruntime
[dai
->id
] = stream
;
591 static void *qcom_swrm_get_sdw_stream(struct snd_soc_dai
*dai
, int direction
)
593 struct qcom_swrm_ctrl
*ctrl
= dev_get_drvdata(dai
->dev
);
595 return ctrl
->sruntime
[dai
->id
];
598 static int qcom_swrm_startup(struct snd_pcm_substream
*substream
,
599 struct snd_soc_dai
*dai
)
601 struct qcom_swrm_ctrl
*ctrl
= dev_get_drvdata(dai
->dev
);
602 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
603 struct sdw_stream_runtime
*sruntime
;
604 struct snd_soc_dai
*codec_dai
;
607 sruntime
= sdw_alloc_stream(dai
->name
);
611 ctrl
->sruntime
[dai
->id
] = sruntime
;
613 for_each_rtd_codec_dais(rtd
, i
, codec_dai
) {
614 ret
= snd_soc_dai_set_sdw_stream(codec_dai
, sruntime
,
616 if (ret
< 0 && ret
!= -ENOTSUPP
) {
617 dev_err(dai
->dev
, "Failed to set sdw stream on %s",
619 sdw_release_stream(sruntime
);
627 static void qcom_swrm_shutdown(struct snd_pcm_substream
*substream
,
628 struct snd_soc_dai
*dai
)
630 struct qcom_swrm_ctrl
*ctrl
= dev_get_drvdata(dai
->dev
);
632 sdw_release_stream(ctrl
->sruntime
[dai
->id
]);
633 ctrl
->sruntime
[dai
->id
] = NULL
;
636 static const struct snd_soc_dai_ops qcom_swrm_pdm_dai_ops
= {
637 .hw_params
= qcom_swrm_hw_params
,
638 .hw_free
= qcom_swrm_hw_free
,
639 .startup
= qcom_swrm_startup
,
640 .shutdown
= qcom_swrm_shutdown
,
641 .set_sdw_stream
= qcom_swrm_set_sdw_stream
,
642 .get_sdw_stream
= qcom_swrm_get_sdw_stream
,
645 static const struct snd_soc_component_driver qcom_swrm_dai_component
= {
649 static int qcom_swrm_register_dais(struct qcom_swrm_ctrl
*ctrl
)
651 int num_dais
= ctrl
->num_dout_ports
+ ctrl
->num_din_ports
;
652 struct snd_soc_dai_driver
*dais
;
653 struct snd_soc_pcm_stream
*stream
;
654 struct device
*dev
= ctrl
->dev
;
657 /* PDM dais are only tested for now */
658 dais
= devm_kcalloc(dev
, num_dais
, sizeof(*dais
), GFP_KERNEL
);
662 for (i
= 0; i
< num_dais
; i
++) {
663 dais
[i
].name
= devm_kasprintf(dev
, GFP_KERNEL
, "SDW Pin%d", i
);
667 if (i
< ctrl
->num_dout_ports
)
668 stream
= &dais
[i
].playback
;
670 stream
= &dais
[i
].capture
;
672 stream
->channels_min
= 1;
673 stream
->channels_max
= 1;
674 stream
->rates
= SNDRV_PCM_RATE_48000
;
675 stream
->formats
= SNDRV_PCM_FMTBIT_S16_LE
;
677 dais
[i
].ops
= &qcom_swrm_pdm_dai_ops
;
681 return devm_snd_soc_register_component(ctrl
->dev
,
682 &qcom_swrm_dai_component
,
686 static int qcom_swrm_get_port_config(struct qcom_swrm_ctrl
*ctrl
)
688 struct device_node
*np
= ctrl
->dev
->of_node
;
689 u8 off1
[QCOM_SDW_MAX_PORTS
];
690 u8 off2
[QCOM_SDW_MAX_PORTS
];
691 u8 si
[QCOM_SDW_MAX_PORTS
];
692 int i
, ret
, nports
, val
;
694 ctrl
->reg_read(ctrl
, SWRM_COMP_PARAMS
, &val
);
696 ctrl
->num_dout_ports
= val
& SWRM_COMP_PARAMS_DOUT_PORTS_MASK
;
697 ctrl
->num_din_ports
= (val
& SWRM_COMP_PARAMS_DIN_PORTS_MASK
) >> 5;
699 ret
= of_property_read_u32(np
, "qcom,din-ports", &val
);
703 if (val
> ctrl
->num_din_ports
)
706 ctrl
->num_din_ports
= val
;
708 ret
= of_property_read_u32(np
, "qcom,dout-ports", &val
);
712 if (val
> ctrl
->num_dout_ports
)
715 ctrl
->num_dout_ports
= val
;
717 nports
= ctrl
->num_dout_ports
+ ctrl
->num_din_ports
;
719 ret
= of_property_read_u8_array(np
, "qcom,ports-offset1",
724 ret
= of_property_read_u8_array(np
, "qcom,ports-offset2",
729 ret
= of_property_read_u8_array(np
, "qcom,ports-sinterval-low",
734 for (i
= 0; i
< nports
; i
++) {
735 ctrl
->pconfig
[i
].si
= si
[i
];
736 ctrl
->pconfig
[i
].off1
= off1
[i
];
737 ctrl
->pconfig
[i
].off2
= off2
[i
];
743 static int qcom_swrm_probe(struct platform_device
*pdev
)
745 struct device
*dev
= &pdev
->dev
;
746 struct sdw_master_prop
*prop
;
747 struct sdw_bus_params
*params
;
748 struct qcom_swrm_ctrl
*ctrl
;
752 ctrl
= devm_kzalloc(dev
, sizeof(*ctrl
), GFP_KERNEL
);
756 if (dev
->parent
->bus
== &slimbus_bus
) {
757 ctrl
->reg_read
= qcom_swrm_abh_reg_read
;
758 ctrl
->reg_write
= qcom_swrm_ahb_reg_write
;
759 ctrl
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
763 /* Only WCD based SoundWire controller is supported */
767 ctrl
->irq
= of_irq_get(dev
->of_node
, 0);
773 ctrl
->hclk
= devm_clk_get(dev
, "iface");
774 if (IS_ERR(ctrl
->hclk
)) {
775 ret
= PTR_ERR(ctrl
->hclk
);
779 clk_prepare_enable(ctrl
->hclk
);
782 dev_set_drvdata(&pdev
->dev
, ctrl
);
783 spin_lock_init(&ctrl
->comp_lock
);
784 mutex_init(&ctrl
->port_lock
);
785 INIT_WORK(&ctrl
->slave_work
, qcom_swrm_slave_wq
);
787 ctrl
->bus
.ops
= &qcom_swrm_ops
;
788 ctrl
->bus
.port_ops
= &qcom_swrm_port_ops
;
789 ctrl
->bus
.compute_params
= &qcom_swrm_compute_params
;
791 ret
= qcom_swrm_get_port_config(ctrl
);
795 params
= &ctrl
->bus
.params
;
796 params
->max_dr_freq
= DEFAULT_CLK_FREQ
;
797 params
->curr_dr_freq
= DEFAULT_CLK_FREQ
;
798 params
->col
= SWRM_DEFAULT_COL
;
799 params
->row
= SWRM_DEFAULT_ROWS
;
800 ctrl
->reg_read(ctrl
, SWRM_MCP_STATUS
, &val
);
801 params
->curr_bank
= val
& SWRM_MCP_STATUS_BANK_NUM_MASK
;
802 params
->next_bank
= !params
->curr_bank
;
804 prop
= &ctrl
->bus
.prop
;
805 prop
->max_clk_freq
= DEFAULT_CLK_FREQ
;
806 prop
->num_clk_gears
= 0;
807 prop
->num_clk_freq
= MAX_FREQ_NUM
;
808 prop
->clk_freq
= &qcom_swrm_freq_tbl
[0];
809 prop
->default_col
= SWRM_DEFAULT_COL
;
810 prop
->default_row
= SWRM_DEFAULT_ROWS
;
812 ctrl
->reg_read(ctrl
, SWRM_COMP_HW_VERSION
, &ctrl
->version
);
814 ret
= devm_request_threaded_irq(dev
, ctrl
->irq
, NULL
,
815 qcom_swrm_irq_handler
,
816 IRQF_TRIGGER_RISING
|
820 dev_err(dev
, "Failed to request soundwire irq\n");
824 ret
= sdw_bus_master_add(&ctrl
->bus
, dev
, dev
->fwnode
);
826 dev_err(dev
, "Failed to register Soundwire controller (%d)\n",
831 qcom_swrm_init(ctrl
);
832 ret
= qcom_swrm_register_dais(ctrl
);
836 dev_info(dev
, "Qualcomm Soundwire controller v%x.%x.%x Registered\n",
837 (ctrl
->version
>> 24) & 0xff, (ctrl
->version
>> 16) & 0xff,
838 ctrl
->version
& 0xffff);
843 sdw_bus_master_delete(&ctrl
->bus
);
845 clk_disable_unprepare(ctrl
->hclk
);
850 static int qcom_swrm_remove(struct platform_device
*pdev
)
852 struct qcom_swrm_ctrl
*ctrl
= dev_get_drvdata(&pdev
->dev
);
854 sdw_bus_master_delete(&ctrl
->bus
);
855 clk_disable_unprepare(ctrl
->hclk
);
860 static const struct of_device_id qcom_swrm_of_match
[] = {
861 { .compatible
= "qcom,soundwire-v1.3.0", },
865 MODULE_DEVICE_TABLE(of
, qcom_swrm_of_match
);
867 static struct platform_driver qcom_swrm_driver
= {
868 .probe
= &qcom_swrm_probe
,
869 .remove
= &qcom_swrm_remove
,
871 .name
= "qcom-soundwire",
872 .of_match_table
= qcom_swrm_of_match
,
875 module_platform_driver(qcom_swrm_driver
);
877 MODULE_DESCRIPTION("Qualcomm soundwire driver");
878 MODULE_LICENSE("GPL v2");