blob: dafa3f3dd1ab14cda87be21d1ce4566b6ad5131e [file] [log] [blame]
Srinivas Kandagatla02efb492020-01-13 13:21:53 +00001// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2019, Linaro Limited
3
4#include <linux/clk.h>
5#include <linux/completion.h>
6#include <linux/interrupt.h>
7#include <linux/io.h>
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/of.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>
20#include "bus.h"
21
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))
Srinivas Kandagatla02efb492020-01-13 13:21:53 +000046#define SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK GENMASK(2, 0)
47#define SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK GENMASK(7, 3)
Srinivas Kandagatla02efb492020-01-13 13:21:53 +000048#define SWRM_MCP_CFG_ADDR 0x1048
49#define SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK GENMASK(21, 17)
Srinivas Kandagatla02efb492020-01-13 13:21:53 +000050#define SWRM_DEF_CMD_NO_PINGS 0x1f
51#define SWRM_MCP_STATUS 0x104C
52#define SWRM_MCP_STATUS_BANK_NUM_MASK BIT(0)
53#define SWRM_MCP_SLV_STATUS 0x1090
54#define SWRM_MCP_SLV_STATUS_MASK GENMASK(1, 0)
55#define SWRM_DP_PORT_CTRL_BANK(n, m) (0x1124 + 0x100 * (n - 1) + 0x40 * m)
56#define SWRM_DP_PORT_CTRL_EN_CHAN_SHFT 0x18
57#define SWRM_DP_PORT_CTRL_OFFSET2_SHFT 0x10
58#define SWRM_DP_PORT_CTRL_OFFSET1_SHFT 0x08
59#define SWRM_AHB_BRIDGE_WR_DATA_0 0xc85
60#define SWRM_AHB_BRIDGE_WR_ADDR_0 0xc89
61#define SWRM_AHB_BRIDGE_RD_ADDR_0 0xc8d
62#define SWRM_AHB_BRIDGE_RD_DATA_0 0xc91
63
64#define SWRM_REG_VAL_PACK(data, dev, id, reg) \
65 ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
66
67#define SWRM_MAX_ROW_VAL 0 /* Rows = 48 */
68#define SWRM_DEFAULT_ROWS 48
69#define SWRM_MIN_COL_VAL 0 /* Cols = 2 */
70#define SWRM_DEFAULT_COL 16
71#define SWRM_MAX_COL_VAL 7
72#define SWRM_SPECIAL_CMD_ID 0xF
73#define MAX_FREQ_NUM 1
74#define TIMEOUT_MS (2 * HZ)
75#define QCOM_SWRM_MAX_RD_LEN 0xf
76#define QCOM_SDW_MAX_PORTS 14
77#define DEFAULT_CLK_FREQ 9600000
78#define SWRM_MAX_DAIS 0xF
79
80struct qcom_swrm_port_config {
81 u8 si;
82 u8 off1;
83 u8 off2;
84};
85
86struct qcom_swrm_ctrl {
87 struct sdw_bus bus;
88 struct device *dev;
89 struct regmap *regmap;
90 struct completion *comp;
91 struct work_struct slave_work;
92 /* read/write lock */
93 spinlock_t comp_lock;
94 /* Port alloc/free lock */
95 struct mutex port_lock;
96 struct clk *hclk;
97 u8 wr_cmd_id;
98 u8 rd_cmd_id;
99 int irq;
100 unsigned int version;
101 int num_din_ports;
102 int num_dout_ports;
103 unsigned long dout_port_mask;
104 unsigned long din_port_mask;
105 struct qcom_swrm_port_config pconfig[QCOM_SDW_MAX_PORTS];
106 struct sdw_stream_runtime *sruntime[SWRM_MAX_DAIS];
107 enum sdw_slave_status status[SDW_MAX_DEVICES];
108 int (*reg_read)(struct qcom_swrm_ctrl *ctrl, int reg, u32 *val);
109 int (*reg_write)(struct qcom_swrm_ctrl *ctrl, int reg, int val);
110};
111
112#define to_qcom_sdw(b) container_of(b, struct qcom_swrm_ctrl, bus)
113
114static int qcom_swrm_abh_reg_read(struct qcom_swrm_ctrl *ctrl, int reg,
115 u32 *val)
116{
117 struct regmap *wcd_regmap = ctrl->regmap;
118 int ret;
119
120 /* pg register + offset */
121 ret = regmap_bulk_write(wcd_regmap, SWRM_AHB_BRIDGE_RD_ADDR_0,
122 (u8 *)&reg, 4);
123 if (ret < 0)
124 return SDW_CMD_FAIL;
125
126 ret = regmap_bulk_read(wcd_regmap, SWRM_AHB_BRIDGE_RD_DATA_0,
127 val, 4);
128 if (ret < 0)
129 return SDW_CMD_FAIL;
130
131 return SDW_CMD_OK;
132}
133
134static int qcom_swrm_ahb_reg_write(struct qcom_swrm_ctrl *ctrl,
135 int reg, int val)
136{
137 struct regmap *wcd_regmap = ctrl->regmap;
138 int ret;
139 /* pg register + offset */
140 ret = regmap_bulk_write(wcd_regmap, SWRM_AHB_BRIDGE_WR_DATA_0,
141 (u8 *)&val, 4);
142 if (ret)
143 return SDW_CMD_FAIL;
144
145 /* write address register */
146 ret = regmap_bulk_write(wcd_regmap, SWRM_AHB_BRIDGE_WR_ADDR_0,
147 (u8 *)&reg, 4);
148 if (ret)
149 return SDW_CMD_FAIL;
150
151 return SDW_CMD_OK;
152}
153
154static int qcom_swrm_cmd_fifo_wr_cmd(struct qcom_swrm_ctrl *ctrl, u8 cmd_data,
155 u8 dev_addr, u16 reg_addr)
156{
157 DECLARE_COMPLETION_ONSTACK(comp);
158 unsigned long flags;
159 u32 val;
160 int ret;
161
162 spin_lock_irqsave(&ctrl->comp_lock, flags);
163 ctrl->comp = &comp;
164 spin_unlock_irqrestore(&ctrl->comp_lock, flags);
165 val = SWRM_REG_VAL_PACK(cmd_data, dev_addr,
166 SWRM_SPECIAL_CMD_ID, reg_addr);
167 ret = ctrl->reg_write(ctrl, SWRM_CMD_FIFO_WR_CMD, val);
168 if (ret)
169 goto err;
170
171 ret = wait_for_completion_timeout(ctrl->comp,
172 msecs_to_jiffies(TIMEOUT_MS));
173
174 if (!ret)
175 ret = SDW_CMD_IGNORED;
176 else
177 ret = SDW_CMD_OK;
178err:
179 spin_lock_irqsave(&ctrl->comp_lock, flags);
180 ctrl->comp = NULL;
181 spin_unlock_irqrestore(&ctrl->comp_lock, flags);
182
183 return ret;
184}
185
186static int qcom_swrm_cmd_fifo_rd_cmd(struct qcom_swrm_ctrl *ctrl,
187 u8 dev_addr, u16 reg_addr,
188 u32 len, u8 *rval)
189{
190 int i, ret;
191 u32 val;
192 DECLARE_COMPLETION_ONSTACK(comp);
193 unsigned long flags;
194
195 spin_lock_irqsave(&ctrl->comp_lock, flags);
196 ctrl->comp = &comp;
197 spin_unlock_irqrestore(&ctrl->comp_lock, flags);
198
199 val = SWRM_REG_VAL_PACK(len, dev_addr, SWRM_SPECIAL_CMD_ID, reg_addr);
200 ret = ctrl->reg_write(ctrl, SWRM_CMD_FIFO_RD_CMD, val);
201 if (ret)
202 goto err;
203
204 ret = wait_for_completion_timeout(ctrl->comp,
205 msecs_to_jiffies(TIMEOUT_MS));
206
207 if (!ret) {
208 ret = SDW_CMD_IGNORED;
209 goto err;
210 } else {
211 ret = SDW_CMD_OK;
212 }
213
214 for (i = 0; i < len; i++) {
215 ctrl->reg_read(ctrl, SWRM_CMD_FIFO_RD_FIFO_ADDR, &val);
216 rval[i] = val & 0xFF;
217 }
218
219err:
220 spin_lock_irqsave(&ctrl->comp_lock, flags);
221 ctrl->comp = NULL;
222 spin_unlock_irqrestore(&ctrl->comp_lock, flags);
223
224 return ret;
225}
226
227static void qcom_swrm_get_device_status(struct qcom_swrm_ctrl *ctrl)
228{
229 u32 val;
230 int i;
231
232 ctrl->reg_read(ctrl, SWRM_MCP_SLV_STATUS, &val);
233
234 for (i = 0; i < SDW_MAX_DEVICES; i++) {
235 u32 s;
236
237 s = (val >> (i * 2));
238 s &= SWRM_MCP_SLV_STATUS_MASK;
239 ctrl->status[i] = s;
240 }
241}
242
243static irqreturn_t qcom_swrm_irq_handler(int irq, void *dev_id)
244{
245 struct qcom_swrm_ctrl *ctrl = dev_id;
246 u32 sts, value;
247 unsigned long flags;
248
249 ctrl->reg_read(ctrl, SWRM_INTERRUPT_STATUS, &sts);
250
251 if (sts & SWRM_INTERRUPT_STATUS_CMD_ERROR) {
252 ctrl->reg_read(ctrl, SWRM_CMD_FIFO_STATUS, &value);
253 dev_err_ratelimited(ctrl->dev,
254 "CMD error, fifo status 0x%x\n",
255 value);
256 ctrl->reg_write(ctrl, SWRM_CMD_FIFO_CMD, 0x1);
257 }
258
259 if ((sts & SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED) ||
260 sts & SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS)
261 schedule_work(&ctrl->slave_work);
262
263 /**
264 * clear the interrupt before complete() is called, as complete can
265 * schedule new read/writes which require interrupts, clearing the
266 * interrupt would avoid missing interrupts in such cases.
267 */
268 ctrl->reg_write(ctrl, SWRM_INTERRUPT_CLEAR, sts);
269
270 if (sts & SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED) {
271 spin_lock_irqsave(&ctrl->comp_lock, flags);
272 if (ctrl->comp)
273 complete(ctrl->comp);
274 spin_unlock_irqrestore(&ctrl->comp_lock, flags);
275 }
276
277 return IRQ_HANDLED;
278}
279static int qcom_swrm_init(struct qcom_swrm_ctrl *ctrl)
280{
281 u32 val;
282
283 /* Clear Rows and Cols */
Vinod Koul9972b902020-09-03 17:15:00 +0530284 val = FIELD_PREP(SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK, SWRM_MAX_ROW_VAL);
285 val |= FIELD_PREP(SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK, SWRM_MIN_COL_VAL);
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000286
287 ctrl->reg_write(ctrl, SWRM_MCP_FRAME_CTRL_BANK_ADDR(0), val);
288
289 /* Disable Auto enumeration */
290 ctrl->reg_write(ctrl, SWRM_ENUMERATOR_CFG_ADDR, 0);
291
292 /* Mask soundwire interrupts */
293 ctrl->reg_write(ctrl, SWRM_INTERRUPT_MASK_ADDR,
294 SWRM_INTERRUPT_STATUS_RMSK);
295
296 /* Configure No pings */
297 ctrl->reg_read(ctrl, SWRM_MCP_CFG_ADDR, &val);
Vinod Koul9972b902020-09-03 17:15:00 +0530298 val |= FIELD_PREP(SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK, SWRM_DEF_CMD_NO_PINGS);
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000299 ctrl->reg_write(ctrl, SWRM_MCP_CFG_ADDR, val);
300
301 /* Configure number of retries of a read/write cmd */
302 ctrl->reg_write(ctrl, SWRM_CMD_FIFO_CFG_ADDR, SWRM_RD_WR_CMD_RETRIES);
303
304 /* Set IRQ to PULSE */
305 ctrl->reg_write(ctrl, SWRM_COMP_CFG_ADDR,
306 SWRM_COMP_CFG_IRQ_LEVEL_OR_PULSE_MSK |
307 SWRM_COMP_CFG_ENABLE_MSK);
308 return 0;
309}
310
311static enum sdw_command_response qcom_swrm_xfer_msg(struct sdw_bus *bus,
312 struct sdw_msg *msg)
313{
314 struct qcom_swrm_ctrl *ctrl = to_qcom_sdw(bus);
315 int ret, i, len;
316
317 if (msg->flags == SDW_MSG_FLAG_READ) {
318 for (i = 0; i < msg->len;) {
319 if ((msg->len - i) < QCOM_SWRM_MAX_RD_LEN)
320 len = msg->len - i;
321 else
322 len = QCOM_SWRM_MAX_RD_LEN;
323
324 ret = qcom_swrm_cmd_fifo_rd_cmd(ctrl, msg->dev_num,
325 msg->addr + i, len,
326 &msg->buf[i]);
327 if (ret)
328 return ret;
329
330 i = i + len;
331 }
332 } else if (msg->flags == SDW_MSG_FLAG_WRITE) {
333 for (i = 0; i < msg->len; i++) {
334 ret = qcom_swrm_cmd_fifo_wr_cmd(ctrl, msg->buf[i],
335 msg->dev_num,
336 msg->addr + i);
337 if (ret)
338 return SDW_CMD_IGNORED;
339 }
340 }
341
342 return SDW_CMD_OK;
343}
344
345static int qcom_swrm_pre_bank_switch(struct sdw_bus *bus)
346{
347 u32 reg = SWRM_MCP_FRAME_CTRL_BANK_ADDR(bus->params.next_bank);
348 struct qcom_swrm_ctrl *ctrl = to_qcom_sdw(bus);
349 u32 val;
350
351 ctrl->reg_read(ctrl, reg, &val);
352
Vinod Koul9972b902020-09-03 17:15:00 +0530353 val |= FIELD_PREP(SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK, SWRM_MAX_COL_VAL);
354 val |= FIELD_PREP(SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK, SWRM_MAX_ROW_VAL);
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000355
356 return ctrl->reg_write(ctrl, reg, val);
357}
358
359static int qcom_swrm_port_params(struct sdw_bus *bus,
360 struct sdw_port_params *p_params,
361 unsigned int bank)
362{
363 /* TBD */
364 return 0;
365}
366
367static int qcom_swrm_transport_params(struct sdw_bus *bus,
368 struct sdw_transport_params *params,
369 enum sdw_reg_bank bank)
370{
371 struct qcom_swrm_ctrl *ctrl = to_qcom_sdw(bus);
372 u32 value;
373
374 value = params->offset1 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT;
375 value |= params->offset2 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT;
376 value |= params->sample_interval - 1;
377
378 return ctrl->reg_write(ctrl,
379 SWRM_DP_PORT_CTRL_BANK((params->port_num), bank),
380 value);
381}
382
383static int qcom_swrm_port_enable(struct sdw_bus *bus,
384 struct sdw_enable_ch *enable_ch,
385 unsigned int bank)
386{
387 u32 reg = SWRM_DP_PORT_CTRL_BANK(enable_ch->port_num, bank);
388 struct qcom_swrm_ctrl *ctrl = to_qcom_sdw(bus);
389 u32 val;
390
391 ctrl->reg_read(ctrl, reg, &val);
392
393 if (enable_ch->enable)
394 val |= (enable_ch->ch_mask << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
395 else
396 val &= ~(0xff << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
397
398 return ctrl->reg_write(ctrl, reg, val);
399}
400
Rikard Falkeborn51fe3882020-06-10 01:00:29 +0200401static const struct sdw_master_port_ops qcom_swrm_port_ops = {
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000402 .dpn_set_port_params = qcom_swrm_port_params,
403 .dpn_set_port_transport_params = qcom_swrm_transport_params,
404 .dpn_port_enable_ch = qcom_swrm_port_enable,
405};
406
Rikard Falkeborn51fe3882020-06-10 01:00:29 +0200407static const struct sdw_master_ops qcom_swrm_ops = {
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000408 .xfer_msg = qcom_swrm_xfer_msg,
409 .pre_bank_switch = qcom_swrm_pre_bank_switch,
410};
411
412static int qcom_swrm_compute_params(struct sdw_bus *bus)
413{
414 struct qcom_swrm_ctrl *ctrl = to_qcom_sdw(bus);
415 struct sdw_master_runtime *m_rt;
416 struct sdw_slave_runtime *s_rt;
417 struct sdw_port_runtime *p_rt;
418 struct qcom_swrm_port_config *pcfg;
419 int i = 0;
420
421 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
422 list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
423 pcfg = &ctrl->pconfig[p_rt->num - 1];
424 p_rt->transport_params.port_num = p_rt->num;
425 p_rt->transport_params.sample_interval = pcfg->si + 1;
426 p_rt->transport_params.offset1 = pcfg->off1;
427 p_rt->transport_params.offset2 = pcfg->off2;
428 }
429
430 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
431 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
432 pcfg = &ctrl->pconfig[i];
433 p_rt->transport_params.port_num = p_rt->num;
434 p_rt->transport_params.sample_interval =
435 pcfg->si + 1;
436 p_rt->transport_params.offset1 = pcfg->off1;
437 p_rt->transport_params.offset2 = pcfg->off2;
438 i++;
439 }
440 }
441 }
442
443 return 0;
444}
445
446static u32 qcom_swrm_freq_tbl[MAX_FREQ_NUM] = {
447 DEFAULT_CLK_FREQ,
448};
449
450static void qcom_swrm_slave_wq(struct work_struct *work)
451{
452 struct qcom_swrm_ctrl *ctrl =
453 container_of(work, struct qcom_swrm_ctrl, slave_work);
454
455 qcom_swrm_get_device_status(ctrl);
456 sdw_handle_slave_status(&ctrl->bus, ctrl->status);
457}
458
459
460static void qcom_swrm_stream_free_ports(struct qcom_swrm_ctrl *ctrl,
461 struct sdw_stream_runtime *stream)
462{
463 struct sdw_master_runtime *m_rt;
464 struct sdw_port_runtime *p_rt;
465 unsigned long *port_mask;
466
467 mutex_lock(&ctrl->port_lock);
468
469 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
470 if (m_rt->direction == SDW_DATA_DIR_RX)
471 port_mask = &ctrl->dout_port_mask;
472 else
473 port_mask = &ctrl->din_port_mask;
474
475 list_for_each_entry(p_rt, &m_rt->port_list, port_node)
476 clear_bit(p_rt->num - 1, port_mask);
477 }
478
479 mutex_unlock(&ctrl->port_lock);
480}
481
482static int qcom_swrm_stream_alloc_ports(struct qcom_swrm_ctrl *ctrl,
483 struct sdw_stream_runtime *stream,
484 struct snd_pcm_hw_params *params,
485 int direction)
486{
487 struct sdw_port_config pconfig[QCOM_SDW_MAX_PORTS];
488 struct sdw_stream_config sconfig;
489 struct sdw_master_runtime *m_rt;
490 struct sdw_slave_runtime *s_rt;
491 struct sdw_port_runtime *p_rt;
492 unsigned long *port_mask;
493 int i, maxport, pn, nports = 0, ret = 0;
494
495 mutex_lock(&ctrl->port_lock);
496 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
497 if (m_rt->direction == SDW_DATA_DIR_RX) {
498 maxport = ctrl->num_dout_ports;
499 port_mask = &ctrl->dout_port_mask;
500 } else {
501 maxport = ctrl->num_din_ports;
502 port_mask = &ctrl->din_port_mask;
503 }
504
505 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
506 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
507 /* Port numbers start from 1 - 14*/
508 pn = find_first_zero_bit(port_mask, maxport);
509 if (pn > (maxport - 1)) {
510 dev_err(ctrl->dev, "All ports busy\n");
511 ret = -EBUSY;
512 goto err;
513 }
514 set_bit(pn, port_mask);
515 pconfig[nports].num = pn + 1;
516 pconfig[nports].ch_mask = p_rt->ch_mask;
517 nports++;
518 }
519 }
520 }
521
522 if (direction == SNDRV_PCM_STREAM_CAPTURE)
523 sconfig.direction = SDW_DATA_DIR_TX;
524 else
525 sconfig.direction = SDW_DATA_DIR_RX;
526
527 /* hw parameters wil be ignored as we only support PDM */
528 sconfig.ch_count = 1;
529 sconfig.frame_rate = params_rate(params);
530 sconfig.type = stream->type;
531 sconfig.bps = 1;
532 sdw_stream_add_master(&ctrl->bus, &sconfig, pconfig,
533 nports, stream);
534err:
535 if (ret) {
536 for (i = 0; i < nports; i++)
537 clear_bit(pconfig[i].num - 1, port_mask);
538 }
539
540 mutex_unlock(&ctrl->port_lock);
541
542 return ret;
543}
544
545static int qcom_swrm_hw_params(struct snd_pcm_substream *substream,
546 struct snd_pcm_hw_params *params,
547 struct snd_soc_dai *dai)
548{
549 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dai->dev);
550 struct sdw_stream_runtime *sruntime = ctrl->sruntime[dai->id];
551 int ret;
552
553 ret = qcom_swrm_stream_alloc_ports(ctrl, sruntime, params,
554 substream->stream);
555 if (ret)
556 qcom_swrm_stream_free_ports(ctrl, sruntime);
557
558 return ret;
559}
560
561static int qcom_swrm_hw_free(struct snd_pcm_substream *substream,
562 struct snd_soc_dai *dai)
563{
564 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dai->dev);
565 struct sdw_stream_runtime *sruntime = ctrl->sruntime[dai->id];
566
567 qcom_swrm_stream_free_ports(ctrl, sruntime);
568 sdw_stream_remove_master(&ctrl->bus, sruntime);
569
570 return 0;
571}
572
573static int qcom_swrm_set_sdw_stream(struct snd_soc_dai *dai,
574 void *stream, int direction)
575{
576 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dai->dev);
577
578 ctrl->sruntime[dai->id] = stream;
579
580 return 0;
581}
582
Srinivas Kandagatla39ec6f92020-03-17 09:26:45 +0000583static void *qcom_swrm_get_sdw_stream(struct snd_soc_dai *dai, int direction)
584{
585 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dai->dev);
586
587 return ctrl->sruntime[dai->id];
588}
589
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000590static int qcom_swrm_startup(struct snd_pcm_substream *substream,
591 struct snd_soc_dai *dai)
592{
593 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dai->dev);
594 struct snd_soc_pcm_runtime *rtd = substream->private_data;
595 struct sdw_stream_runtime *sruntime;
Kuninori Morimotoce83bac2020-02-19 15:55:53 +0900596 struct snd_soc_dai *codec_dai;
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000597 int ret, i;
598
599 sruntime = sdw_alloc_stream(dai->name);
600 if (!sruntime)
601 return -ENOMEM;
602
603 ctrl->sruntime[dai->id] = sruntime;
604
Kuninori Morimotoc998ee32020-03-09 13:07:57 +0900605 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Kuninori Morimotoce83bac2020-02-19 15:55:53 +0900606 ret = snd_soc_dai_set_sdw_stream(codec_dai, sruntime,
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000607 substream->stream);
608 if (ret < 0 && ret != -ENOTSUPP) {
609 dev_err(dai->dev, "Failed to set sdw stream on %s",
Kuninori Morimotoce83bac2020-02-19 15:55:53 +0900610 codec_dai->name);
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000611 sdw_release_stream(sruntime);
612 return ret;
613 }
614 }
615
616 return 0;
617}
618
619static void qcom_swrm_shutdown(struct snd_pcm_substream *substream,
620 struct snd_soc_dai *dai)
621{
622 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(dai->dev);
623
624 sdw_release_stream(ctrl->sruntime[dai->id]);
625 ctrl->sruntime[dai->id] = NULL;
626}
627
628static const struct snd_soc_dai_ops qcom_swrm_pdm_dai_ops = {
629 .hw_params = qcom_swrm_hw_params,
630 .hw_free = qcom_swrm_hw_free,
631 .startup = qcom_swrm_startup,
632 .shutdown = qcom_swrm_shutdown,
633 .set_sdw_stream = qcom_swrm_set_sdw_stream,
Srinivas Kandagatla39ec6f92020-03-17 09:26:45 +0000634 .get_sdw_stream = qcom_swrm_get_sdw_stream,
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000635};
636
637static const struct snd_soc_component_driver qcom_swrm_dai_component = {
638 .name = "soundwire",
639};
640
641static int qcom_swrm_register_dais(struct qcom_swrm_ctrl *ctrl)
642{
643 int num_dais = ctrl->num_dout_ports + ctrl->num_din_ports;
644 struct snd_soc_dai_driver *dais;
645 struct snd_soc_pcm_stream *stream;
646 struct device *dev = ctrl->dev;
647 int i;
648
649 /* PDM dais are only tested for now */
650 dais = devm_kcalloc(dev, num_dais, sizeof(*dais), GFP_KERNEL);
651 if (!dais)
652 return -ENOMEM;
653
654 for (i = 0; i < num_dais; i++) {
655 dais[i].name = devm_kasprintf(dev, GFP_KERNEL, "SDW Pin%d", i);
656 if (!dais[i].name)
657 return -ENOMEM;
658
659 if (i < ctrl->num_dout_ports)
660 stream = &dais[i].playback;
661 else
662 stream = &dais[i].capture;
663
664 stream->channels_min = 1;
665 stream->channels_max = 1;
666 stream->rates = SNDRV_PCM_RATE_48000;
667 stream->formats = SNDRV_PCM_FMTBIT_S16_LE;
668
669 dais[i].ops = &qcom_swrm_pdm_dai_ops;
670 dais[i].id = i;
671 }
672
673 return devm_snd_soc_register_component(ctrl->dev,
674 &qcom_swrm_dai_component,
675 dais, num_dais);
676}
677
678static int qcom_swrm_get_port_config(struct qcom_swrm_ctrl *ctrl)
679{
680 struct device_node *np = ctrl->dev->of_node;
681 u8 off1[QCOM_SDW_MAX_PORTS];
682 u8 off2[QCOM_SDW_MAX_PORTS];
683 u8 si[QCOM_SDW_MAX_PORTS];
684 int i, ret, nports, val;
685
686 ctrl->reg_read(ctrl, SWRM_COMP_PARAMS, &val);
687
Vinod Koul9972b902020-09-03 17:15:00 +0530688 ctrl->num_dout_ports = FIELD_GET(SWRM_COMP_PARAMS_DOUT_PORTS_MASK, val);
689 ctrl->num_din_ports = FIELD_GET(SWRM_COMP_PARAMS_DIN_PORTS_MASK, val);
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000690
691 ret = of_property_read_u32(np, "qcom,din-ports", &val);
692 if (ret)
693 return ret;
694
695 if (val > ctrl->num_din_ports)
696 return -EINVAL;
697
698 ctrl->num_din_ports = val;
699
700 ret = of_property_read_u32(np, "qcom,dout-ports", &val);
701 if (ret)
702 return ret;
703
704 if (val > ctrl->num_dout_ports)
705 return -EINVAL;
706
707 ctrl->num_dout_ports = val;
708
709 nports = ctrl->num_dout_ports + ctrl->num_din_ports;
710
711 ret = of_property_read_u8_array(np, "qcom,ports-offset1",
712 off1, nports);
713 if (ret)
714 return ret;
715
716 ret = of_property_read_u8_array(np, "qcom,ports-offset2",
717 off2, nports);
718 if (ret)
719 return ret;
720
721 ret = of_property_read_u8_array(np, "qcom,ports-sinterval-low",
722 si, nports);
723 if (ret)
724 return ret;
725
726 for (i = 0; i < nports; i++) {
727 ctrl->pconfig[i].si = si[i];
728 ctrl->pconfig[i].off1 = off1[i];
729 ctrl->pconfig[i].off2 = off2[i];
730 }
731
732 return 0;
733}
734
735static int qcom_swrm_probe(struct platform_device *pdev)
736{
737 struct device *dev = &pdev->dev;
738 struct sdw_master_prop *prop;
739 struct sdw_bus_params *params;
740 struct qcom_swrm_ctrl *ctrl;
741 int ret;
742 u32 val;
743
744 ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
745 if (!ctrl)
746 return -ENOMEM;
747
748 if (dev->parent->bus == &slimbus_bus) {
749 ctrl->reg_read = qcom_swrm_abh_reg_read;
750 ctrl->reg_write = qcom_swrm_ahb_reg_write;
751 ctrl->regmap = dev_get_regmap(dev->parent, NULL);
752 if (!ctrl->regmap)
753 return -EINVAL;
754 } else {
755 /* Only WCD based SoundWire controller is supported */
756 return -ENOTSUPP;
757 }
758
759 ctrl->irq = of_irq_get(dev->of_node, 0);
Pierre-Louis Bossart91b5cfc2020-04-30 02:50:57 +0800760 if (ctrl->irq < 0) {
761 ret = ctrl->irq;
762 goto err_init;
763 }
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000764
765 ctrl->hclk = devm_clk_get(dev, "iface");
Pierre-Louis Bossart91b5cfc2020-04-30 02:50:57 +0800766 if (IS_ERR(ctrl->hclk)) {
767 ret = PTR_ERR(ctrl->hclk);
768 goto err_init;
769 }
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000770
771 clk_prepare_enable(ctrl->hclk);
772
773 ctrl->dev = dev;
774 dev_set_drvdata(&pdev->dev, ctrl);
775 spin_lock_init(&ctrl->comp_lock);
776 mutex_init(&ctrl->port_lock);
777 INIT_WORK(&ctrl->slave_work, qcom_swrm_slave_wq);
778
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000779 ctrl->bus.ops = &qcom_swrm_ops;
780 ctrl->bus.port_ops = &qcom_swrm_port_ops;
781 ctrl->bus.compute_params = &qcom_swrm_compute_params;
782
783 ret = qcom_swrm_get_port_config(ctrl);
784 if (ret)
Pierre-Louis Bossart91b5cfc2020-04-30 02:50:57 +0800785 goto err_clk;
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000786
787 params = &ctrl->bus.params;
788 params->max_dr_freq = DEFAULT_CLK_FREQ;
789 params->curr_dr_freq = DEFAULT_CLK_FREQ;
790 params->col = SWRM_DEFAULT_COL;
791 params->row = SWRM_DEFAULT_ROWS;
792 ctrl->reg_read(ctrl, SWRM_MCP_STATUS, &val);
793 params->curr_bank = val & SWRM_MCP_STATUS_BANK_NUM_MASK;
794 params->next_bank = !params->curr_bank;
795
796 prop = &ctrl->bus.prop;
797 prop->max_clk_freq = DEFAULT_CLK_FREQ;
798 prop->num_clk_gears = 0;
799 prop->num_clk_freq = MAX_FREQ_NUM;
800 prop->clk_freq = &qcom_swrm_freq_tbl[0];
801 prop->default_col = SWRM_DEFAULT_COL;
802 prop->default_row = SWRM_DEFAULT_ROWS;
803
804 ctrl->reg_read(ctrl, SWRM_COMP_HW_VERSION, &ctrl->version);
805
806 ret = devm_request_threaded_irq(dev, ctrl->irq, NULL,
807 qcom_swrm_irq_handler,
Samuel Zou4f1738f2020-05-06 11:25:53 +0800808 IRQF_TRIGGER_RISING |
809 IRQF_ONESHOT,
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000810 "soundwire", ctrl);
811 if (ret) {
812 dev_err(dev, "Failed to request soundwire irq\n");
Pierre-Louis Bossart91b5cfc2020-04-30 02:50:57 +0800813 goto err_clk;
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000814 }
815
Pierre-Louis Bossart5cab3ff2020-05-19 01:43:18 +0800816 ret = sdw_bus_master_add(&ctrl->bus, dev, dev->fwnode);
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000817 if (ret) {
818 dev_err(dev, "Failed to register Soundwire controller (%d)\n",
819 ret);
Pierre-Louis Bossart91b5cfc2020-04-30 02:50:57 +0800820 goto err_clk;
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000821 }
822
823 qcom_swrm_init(ctrl);
824 ret = qcom_swrm_register_dais(ctrl);
825 if (ret)
Pierre-Louis Bossart91b5cfc2020-04-30 02:50:57 +0800826 goto err_master_add;
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000827
828 dev_info(dev, "Qualcomm Soundwire controller v%x.%x.%x Registered\n",
829 (ctrl->version >> 24) & 0xff, (ctrl->version >> 16) & 0xff,
830 ctrl->version & 0xffff);
831
832 return 0;
Pierre-Louis Bossart91b5cfc2020-04-30 02:50:57 +0800833
834err_master_add:
Pierre-Louis Bossart5cab3ff2020-05-19 01:43:18 +0800835 sdw_bus_master_delete(&ctrl->bus);
Pierre-Louis Bossart91b5cfc2020-04-30 02:50:57 +0800836err_clk:
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000837 clk_disable_unprepare(ctrl->hclk);
Pierre-Louis Bossart91b5cfc2020-04-30 02:50:57 +0800838err_init:
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000839 return ret;
840}
841
842static int qcom_swrm_remove(struct platform_device *pdev)
843{
844 struct qcom_swrm_ctrl *ctrl = dev_get_drvdata(&pdev->dev);
845
Pierre-Louis Bossart5cab3ff2020-05-19 01:43:18 +0800846 sdw_bus_master_delete(&ctrl->bus);
Srinivas Kandagatla02efb492020-01-13 13:21:53 +0000847 clk_disable_unprepare(ctrl->hclk);
848
849 return 0;
850}
851
852static const struct of_device_id qcom_swrm_of_match[] = {
853 { .compatible = "qcom,soundwire-v1.3.0", },
854 {/* sentinel */},
855};
856
857MODULE_DEVICE_TABLE(of, qcom_swrm_of_match);
858
859static struct platform_driver qcom_swrm_driver = {
860 .probe = &qcom_swrm_probe,
861 .remove = &qcom_swrm_remove,
862 .driver = {
863 .name = "qcom-soundwire",
864 .of_match_table = qcom_swrm_of_match,
865 }
866};
867module_platform_driver(qcom_swrm_driver);
868
869MODULE_DESCRIPTION("Qualcomm soundwire driver");
870MODULE_LICENSE("GPL v2");