blob: 37e16199933cbd9bbeb6474bf959187cacc7f99e [file] [log] [blame]
Vinod Koul2f52a512017-12-14 11:19:41 +05301// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2// Copyright(c) 2015-17 Intel Corporation.
3
4/*
5 * Cadence SoundWire Master module
6 * Used by Master driver
7 */
8
9#include <linux/delay.h>
10#include <linux/device.h>
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -050011#include <linux/debugfs.h>
Vinod Koul2f52a512017-12-14 11:19:41 +053012#include <linux/interrupt.h>
Jan Kotas18de65d2019-04-04 09:12:21 +010013#include <linux/io.h>
Vinod Koul2f52a512017-12-14 11:19:41 +053014#include <linux/module.h>
15#include <linux/mod_devicetable.h>
16#include <linux/soundwire/sdw_registers.h>
17#include <linux/soundwire/sdw.h>
Vinod Koul5d6b3c82018-04-26 18:38:53 +053018#include <sound/pcm_params.h>
19#include <sound/soc.h>
Vinod Koul2f52a512017-12-14 11:19:41 +053020#include "bus.h"
21#include "cadence_master.h"
22
Pierre-Louis Bossart04592dc2019-08-05 19:55:21 -050023static int interrupt_mask;
24module_param_named(cnds_mcp_int_mask, interrupt_mask, int, 0444);
25MODULE_PARM_DESC(cdns_mcp_int_mask, "Cadence MCP IntMask");
26
Vinod Koul2f52a512017-12-14 11:19:41 +053027#define CDNS_MCP_CONFIG 0x0
28
29#define CDNS_MCP_CONFIG_MCMD_RETRY GENMASK(27, 24)
30#define CDNS_MCP_CONFIG_MPREQ_DELAY GENMASK(20, 16)
31#define CDNS_MCP_CONFIG_MMASTER BIT(7)
32#define CDNS_MCP_CONFIG_BUS_REL BIT(6)
33#define CDNS_MCP_CONFIG_SNIFFER BIT(5)
34#define CDNS_MCP_CONFIG_SSPMOD BIT(4)
35#define CDNS_MCP_CONFIG_CMD BIT(3)
36#define CDNS_MCP_CONFIG_OP GENMASK(2, 0)
37#define CDNS_MCP_CONFIG_OP_NORMAL 0
38
39#define CDNS_MCP_CONTROL 0x4
40
41#define CDNS_MCP_CONTROL_RST_DELAY GENMASK(10, 8)
42#define CDNS_MCP_CONTROL_CMD_RST BIT(7)
43#define CDNS_MCP_CONTROL_SOFT_RST BIT(6)
44#define CDNS_MCP_CONTROL_SW_RST BIT(5)
45#define CDNS_MCP_CONTROL_HW_RST BIT(4)
46#define CDNS_MCP_CONTROL_CLK_PAUSE BIT(3)
47#define CDNS_MCP_CONTROL_CLK_STOP_CLR BIT(2)
48#define CDNS_MCP_CONTROL_CMD_ACCEPT BIT(1)
49#define CDNS_MCP_CONTROL_BLOCK_WAKEUP BIT(0)
50
Vinod Koul2f52a512017-12-14 11:19:41 +053051#define CDNS_MCP_CMDCTRL 0x8
52#define CDNS_MCP_SSPSTAT 0xC
53#define CDNS_MCP_FRAME_SHAPE 0x10
54#define CDNS_MCP_FRAME_SHAPE_INIT 0x14
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -050055#define CDNS_MCP_FRAME_SHAPE_COL_MASK GENMASK(2, 0)
56#define CDNS_MCP_FRAME_SHAPE_ROW_OFFSET 3
Vinod Koul2f52a512017-12-14 11:19:41 +053057
58#define CDNS_MCP_CONFIG_UPDATE 0x18
59#define CDNS_MCP_CONFIG_UPDATE_BIT BIT(0)
60
61#define CDNS_MCP_PHYCTRL 0x1C
62#define CDNS_MCP_SSP_CTRL0 0x20
63#define CDNS_MCP_SSP_CTRL1 0x28
64#define CDNS_MCP_CLK_CTRL0 0x30
65#define CDNS_MCP_CLK_CTRL1 0x38
Rander Wanga50954e2019-08-05 19:55:18 -050066#define CDNS_MCP_CLK_MCLKD_MASK GENMASK(7, 0)
Vinod Koul2f52a512017-12-14 11:19:41 +053067
68#define CDNS_MCP_STAT 0x40
69
70#define CDNS_MCP_STAT_ACTIVE_BANK BIT(20)
71#define CDNS_MCP_STAT_CLK_STOP BIT(16)
72
73#define CDNS_MCP_INTSTAT 0x44
74#define CDNS_MCP_INTMASK 0x48
75
76#define CDNS_MCP_INT_IRQ BIT(31)
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -060077#define CDNS_MCP_INT_RESERVED1 GENMASK(30, 17)
Vinod Koul2f52a512017-12-14 11:19:41 +053078#define CDNS_MCP_INT_WAKEUP BIT(16)
79#define CDNS_MCP_INT_SLAVE_RSVD BIT(15)
80#define CDNS_MCP_INT_SLAVE_ALERT BIT(14)
81#define CDNS_MCP_INT_SLAVE_ATTACH BIT(13)
82#define CDNS_MCP_INT_SLAVE_NATTACH BIT(12)
83#define CDNS_MCP_INT_SLAVE_MASK GENMASK(15, 12)
84#define CDNS_MCP_INT_DPINT BIT(11)
85#define CDNS_MCP_INT_CTRL_CLASH BIT(10)
86#define CDNS_MCP_INT_DATA_CLASH BIT(9)
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -050087#define CDNS_MCP_INT_PARITY BIT(8)
Vinod Koul2f52a512017-12-14 11:19:41 +053088#define CDNS_MCP_INT_CMD_ERR BIT(7)
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -060089#define CDNS_MCP_INT_RESERVED2 GENMASK(6, 4)
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -050090#define CDNS_MCP_INT_RX_NE BIT(3)
Vinod Koul2f52a512017-12-14 11:19:41 +053091#define CDNS_MCP_INT_RX_WL BIT(2)
92#define CDNS_MCP_INT_TXE BIT(1)
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -050093#define CDNS_MCP_INT_TXF BIT(0)
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -060094#define CDNS_MCP_INT_RESERVED (CDNS_MCP_INT_RESERVED1 | CDNS_MCP_INT_RESERVED2)
Vinod Koul2f52a512017-12-14 11:19:41 +053095
96#define CDNS_MCP_INTSET 0x4C
97
Pierre-Louis Bossartb07dd9b2019-07-25 18:40:05 -050098#define CDNS_MCP_SLAVE_STAT 0x50
99#define CDNS_MCP_SLAVE_STAT_MASK GENMASK(1, 0)
Vinod Koul2f52a512017-12-14 11:19:41 +0530100
101#define CDNS_MCP_SLAVE_INTSTAT0 0x54
102#define CDNS_MCP_SLAVE_INTSTAT1 0x58
103#define CDNS_MCP_SLAVE_INTSTAT_NPRESENT BIT(0)
104#define CDNS_MCP_SLAVE_INTSTAT_ATTACHED BIT(1)
105#define CDNS_MCP_SLAVE_INTSTAT_ALERT BIT(2)
106#define CDNS_MCP_SLAVE_INTSTAT_RESERVED BIT(3)
107#define CDNS_MCP_SLAVE_STATUS_BITS GENMASK(3, 0)
108#define CDNS_MCP_SLAVE_STATUS_NUM 4
109
110#define CDNS_MCP_SLAVE_INTMASK0 0x5C
111#define CDNS_MCP_SLAVE_INTMASK1 0x60
112
Pierre-Louis Bossart664b1652019-07-25 18:40:06 -0500113#define CDNS_MCP_SLAVE_INTMASK0_MASK GENMASK(31, 0)
114#define CDNS_MCP_SLAVE_INTMASK1_MASK GENMASK(15, 0)
Vinod Koul2f52a512017-12-14 11:19:41 +0530115
116#define CDNS_MCP_PORT_INTSTAT 0x64
117#define CDNS_MCP_PDI_STAT 0x6C
118
119#define CDNS_MCP_FIFOLEVEL 0x78
120#define CDNS_MCP_FIFOSTAT 0x7C
121#define CDNS_MCP_RX_FIFO_AVAIL GENMASK(5, 0)
122
123#define CDNS_MCP_CMD_BASE 0x80
124#define CDNS_MCP_RESP_BASE 0x80
125#define CDNS_MCP_CMD_LEN 0x20
126#define CDNS_MCP_CMD_WORD_LEN 0x4
127
128#define CDNS_MCP_CMD_SSP_TAG BIT(31)
129#define CDNS_MCP_CMD_COMMAND GENMASK(30, 28)
130#define CDNS_MCP_CMD_DEV_ADDR GENMASK(27, 24)
131#define CDNS_MCP_CMD_REG_ADDR_H GENMASK(23, 16)
132#define CDNS_MCP_CMD_REG_ADDR_L GENMASK(15, 8)
133#define CDNS_MCP_CMD_REG_DATA GENMASK(7, 0)
134
135#define CDNS_MCP_CMD_READ 2
136#define CDNS_MCP_CMD_WRITE 3
137
138#define CDNS_MCP_RESP_RDATA GENMASK(15, 8)
139#define CDNS_MCP_RESP_ACK BIT(0)
140#define CDNS_MCP_RESP_NACK BIT(1)
141
142#define CDNS_DP_SIZE 128
143
144#define CDNS_DPN_B0_CONFIG(n) (0x100 + CDNS_DP_SIZE * (n))
145#define CDNS_DPN_B0_CH_EN(n) (0x104 + CDNS_DP_SIZE * (n))
146#define CDNS_DPN_B0_SAMPLE_CTRL(n) (0x108 + CDNS_DP_SIZE * (n))
147#define CDNS_DPN_B0_OFFSET_CTRL(n) (0x10C + CDNS_DP_SIZE * (n))
148#define CDNS_DPN_B0_HCTRL(n) (0x110 + CDNS_DP_SIZE * (n))
149#define CDNS_DPN_B0_ASYNC_CTRL(n) (0x114 + CDNS_DP_SIZE * (n))
150
151#define CDNS_DPN_B1_CONFIG(n) (0x118 + CDNS_DP_SIZE * (n))
152#define CDNS_DPN_B1_CH_EN(n) (0x11C + CDNS_DP_SIZE * (n))
153#define CDNS_DPN_B1_SAMPLE_CTRL(n) (0x120 + CDNS_DP_SIZE * (n))
154#define CDNS_DPN_B1_OFFSET_CTRL(n) (0x124 + CDNS_DP_SIZE * (n))
155#define CDNS_DPN_B1_HCTRL(n) (0x128 + CDNS_DP_SIZE * (n))
156#define CDNS_DPN_B1_ASYNC_CTRL(n) (0x12C + CDNS_DP_SIZE * (n))
157
158#define CDNS_DPN_CONFIG_BPM BIT(18)
159#define CDNS_DPN_CONFIG_BGC GENMASK(17, 16)
160#define CDNS_DPN_CONFIG_WL GENMASK(12, 8)
161#define CDNS_DPN_CONFIG_PORT_DAT GENMASK(3, 2)
162#define CDNS_DPN_CONFIG_PORT_FLOW GENMASK(1, 0)
163
164#define CDNS_DPN_SAMPLE_CTRL_SI GENMASK(15, 0)
165
166#define CDNS_DPN_OFFSET_CTRL_1 GENMASK(7, 0)
167#define CDNS_DPN_OFFSET_CTRL_2 GENMASK(15, 8)
168
169#define CDNS_DPN_HCTRL_HSTOP GENMASK(3, 0)
170#define CDNS_DPN_HCTRL_HSTART GENMASK(7, 4)
171#define CDNS_DPN_HCTRL_LCTRL GENMASK(10, 8)
172
173#define CDNS_PORTCTRL 0x130
174#define CDNS_PORTCTRL_DIRN BIT(7)
175#define CDNS_PORTCTRL_BANK_INVERT BIT(8)
176
177#define CDNS_PORT_OFFSET 0x80
178
179#define CDNS_PDI_CONFIG(n) (0x1100 + (n) * 16)
180
181#define CDNS_PDI_CONFIG_SOFT_RESET BIT(24)
182#define CDNS_PDI_CONFIG_CHANNEL GENMASK(15, 8)
183#define CDNS_PDI_CONFIG_PORT GENMASK(4, 0)
184
185/* Driver defaults */
Vinod Koul2f52a512017-12-14 11:19:41 +0530186#define CDNS_DEFAULT_SSP_INTERVAL 0x18
187#define CDNS_TX_TIMEOUT 2000
188
Vinod Koul2f52a512017-12-14 11:19:41 +0530189#define CDNS_SCP_RX_FIFOLEVEL 0x2
190
191/*
192 * register accessor helpers
193 */
194static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset)
195{
196 return readl(cdns->registers + offset);
197}
198
199static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value)
200{
201 writel(value, cdns->registers + offset);
202}
203
204static inline void cdns_updatel(struct sdw_cdns *cdns,
205 int offset, u32 mask, u32 val)
206{
207 u32 tmp;
208
209 tmp = cdns_readl(cdns, offset);
210 tmp = (tmp & ~mask) | val;
211 cdns_writel(cdns, offset, tmp);
212}
213
214static int cdns_clear_bit(struct sdw_cdns *cdns, int offset, u32 value)
215{
216 int timeout = 10;
217 u32 reg_read;
218
219 writel(value, cdns->registers + offset);
220
221 /* Wait for bit to be self cleared */
222 do {
223 reg_read = readl(cdns->registers + offset);
224 if ((reg_read & value) == 0)
225 return 0;
226
227 timeout--;
228 udelay(50);
229 } while (timeout != 0);
230
231 return -EAGAIN;
232}
233
Rander Wang10325042020-03-17 11:33:17 -0500234static int cdns_set_wait(struct sdw_cdns *cdns, int offset, u32 mask, u32 value)
235{
236 int timeout = 10;
237 u32 reg_read;
238
239 /* Wait for bit to be set */
240 do {
241 reg_read = readl(cdns->registers + offset);
242 if ((reg_read & mask) == value)
243 return 0;
244
245 timeout--;
246 usleep_range(50, 100);
247 } while (timeout != 0);
248
249 return -ETIMEDOUT;
250}
251
Vinod Koul2f52a512017-12-14 11:19:41 +0530252/*
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500253 * all changes to the MCP_CONFIG, MCP_CONTROL, MCP_CMDCTRL and MCP_PHYCTRL
254 * need to be confirmed with a write to MCP_CONFIG_UPDATE
255 */
Pierre-Louis Bossartce1acf02020-03-17 11:33:13 -0500256static int cdns_config_update(struct sdw_cdns *cdns)
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500257{
258 int ret;
259
Pierre-Louis Bossart9bc87cc2020-03-17 11:33:16 -0500260 if (sdw_cdns_is_clock_stop(cdns)) {
261 dev_err(cdns->dev, "Cannot program MCP_CONFIG_UPDATE in ClockStopMode\n");
262 return -EINVAL;
263 }
264
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500265 ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE,
266 CDNS_MCP_CONFIG_UPDATE_BIT);
267 if (ret < 0)
268 dev_err(cdns->dev, "Config update timedout\n");
269
270 return ret;
271}
272
273/*
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500274 * debugfs
275 */
276#ifdef CONFIG_DEBUG_FS
277
278#define RD_BUF (2 * PAGE_SIZE)
279
280static ssize_t cdns_sprintf(struct sdw_cdns *cdns,
281 char *buf, size_t pos, unsigned int reg)
282{
283 return scnprintf(buf + pos, RD_BUF - pos,
284 "%4x\t%8x\n", reg, cdns_readl(cdns, reg));
285}
286
287static int cdns_reg_show(struct seq_file *s, void *data)
288{
289 struct sdw_cdns *cdns = s->private;
290 char *buf;
291 ssize_t ret;
292 int num_ports;
293 int i, j;
294
295 buf = kzalloc(RD_BUF, GFP_KERNEL);
296 if (!buf)
297 return -ENOMEM;
298
299 ret = scnprintf(buf, RD_BUF, "Register Value\n");
300 ret += scnprintf(buf + ret, RD_BUF - ret, "\nMCP Registers\n");
301 /* 8 MCP registers */
302 for (i = CDNS_MCP_CONFIG; i <= CDNS_MCP_PHYCTRL; i += sizeof(u32))
303 ret += cdns_sprintf(cdns, buf, ret, i);
304
305 ret += scnprintf(buf + ret, RD_BUF - ret,
306 "\nStatus & Intr Registers\n");
307 /* 13 Status & Intr registers (offsets 0x70 and 0x74 not defined) */
308 for (i = CDNS_MCP_STAT; i <= CDNS_MCP_FIFOSTAT; i += sizeof(u32))
309 ret += cdns_sprintf(cdns, buf, ret, i);
310
311 ret += scnprintf(buf + ret, RD_BUF - ret,
312 "\nSSP & Clk ctrl Registers\n");
313 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL0);
314 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL1);
315 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL0);
316 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL1);
317
318 ret += scnprintf(buf + ret, RD_BUF - ret,
319 "\nDPn B0 Registers\n");
320
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -0500321 num_ports = cdns->num_ports;
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500322
323 for (i = 0; i < num_ports; i++) {
324 ret += scnprintf(buf + ret, RD_BUF - ret,
325 "\nDP-%d\n", i);
326 for (j = CDNS_DPN_B0_CONFIG(i);
327 j < CDNS_DPN_B0_ASYNC_CTRL(i); j += sizeof(u32))
328 ret += cdns_sprintf(cdns, buf, ret, j);
329 }
330
331 ret += scnprintf(buf + ret, RD_BUF - ret,
332 "\nDPn B1 Registers\n");
333 for (i = 0; i < num_ports; i++) {
334 ret += scnprintf(buf + ret, RD_BUF - ret,
335 "\nDP-%d\n", i);
336
337 for (j = CDNS_DPN_B1_CONFIG(i);
338 j < CDNS_DPN_B1_ASYNC_CTRL(i); j += sizeof(u32))
339 ret += cdns_sprintf(cdns, buf, ret, j);
340 }
341
342 ret += scnprintf(buf + ret, RD_BUF - ret,
343 "\nDPn Control Registers\n");
344 for (i = 0; i < num_ports; i++)
345 ret += cdns_sprintf(cdns, buf, ret,
346 CDNS_PORTCTRL + i * CDNS_PORT_OFFSET);
347
348 ret += scnprintf(buf + ret, RD_BUF - ret,
349 "\nPDIn Config Registers\n");
350
351 /* number of PDI and ports is interchangeable */
352 for (i = 0; i < num_ports; i++)
353 ret += cdns_sprintf(cdns, buf, ret, CDNS_PDI_CONFIG(i));
354
355 seq_printf(s, "%s", buf);
356 kfree(buf);
357
358 return 0;
359}
360DEFINE_SHOW_ATTRIBUTE(cdns_reg);
361
Pierre-Louis Bossart675d4c92019-10-22 18:54:45 -0500362static int cdns_hw_reset(void *data, u64 value)
363{
364 struct sdw_cdns *cdns = data;
365 int ret;
366
367 if (value != 1)
368 return -EINVAL;
369
370 /* Userspace changed the hardware state behind the kernel's back */
371 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
372
373 ret = sdw_cdns_exit_reset(cdns);
374
375 dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret);
376
377 return ret;
378}
379
380DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n");
381
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500382/**
383 * sdw_cdns_debugfs_init() - Cadence debugfs init
384 * @cdns: Cadence instance
385 * @root: debugfs root
386 */
387void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
388{
389 debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
Pierre-Louis Bossart675d4c92019-10-22 18:54:45 -0500390
391 debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
392 &cdns_hw_reset_fops);
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500393}
394EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
395
396#endif /* CONFIG_DEBUG_FS */
397
398/*
Sanyog Kale956baa12017-12-14 11:19:42 +0530399 * IO Calls
400 */
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500401static enum sdw_command_response
402cdns_fill_msg_resp(struct sdw_cdns *cdns,
403 struct sdw_msg *msg, int count, int offset)
Sanyog Kale956baa12017-12-14 11:19:42 +0530404{
405 int nack = 0, no_ack = 0;
406 int i;
407
408 /* check message response */
409 for (i = 0; i < count; i++) {
410 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
411 no_ack = 1;
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500412 dev_dbg_ratelimited(cdns->dev, "Msg Ack not received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530413 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
414 nack = 1;
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500415 dev_err_ratelimited(cdns->dev, "Msg NACK received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530416 }
417 }
418 }
419
420 if (nack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500421 dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530422 return SDW_CMD_FAIL;
423 } else if (no_ack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500424 dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530425 return SDW_CMD_IGNORED;
426 }
427
428 /* fill response */
429 for (i = 0; i < count; i++)
430 msg->buf[i + offset] = cdns->response_buf[i] >>
431 SDW_REG_SHIFT(CDNS_MCP_RESP_RDATA);
432
433 return SDW_CMD_OK;
434}
435
436static enum sdw_command_response
437_cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500438 int offset, int count, bool defer)
Sanyog Kale956baa12017-12-14 11:19:42 +0530439{
440 unsigned long time;
441 u32 base, i, data;
442 u16 addr;
443
444 /* Program the watermark level for RX FIFO */
445 if (cdns->msg_count != count) {
446 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count);
447 cdns->msg_count = count;
448 }
449
450 base = CDNS_MCP_CMD_BASE;
451 addr = msg->addr;
452
453 for (i = 0; i < count; i++) {
454 data = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR);
455 data |= cmd << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND);
456 data |= addr++ << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
457
458 if (msg->flags == SDW_MSG_FLAG_WRITE)
459 data |= msg->buf[i + offset];
460
461 data |= msg->ssp_sync << SDW_REG_SHIFT(CDNS_MCP_CMD_SSP_TAG);
462 cdns_writel(cdns, base, data);
463 base += CDNS_MCP_CMD_WORD_LEN;
464 }
465
466 if (defer)
467 return SDW_CMD_OK;
468
469 /* wait for timeout or response */
470 time = wait_for_completion_timeout(&cdns->tx_complete,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500471 msecs_to_jiffies(CDNS_TX_TIMEOUT));
Sanyog Kale956baa12017-12-14 11:19:42 +0530472 if (!time) {
Pierre-Louis Bossart53ee9572020-01-10 15:57:28 -0600473 dev_err(cdns->dev, "IO transfer timed out, cmd %d device %d addr %x len %d\n",
474 cmd, msg->dev_num, msg->addr, msg->len);
Sanyog Kale956baa12017-12-14 11:19:42 +0530475 msg->len = 0;
476 return SDW_CMD_TIMEOUT;
477 }
478
479 return cdns_fill_msg_resp(cdns, msg, count, offset);
480}
481
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500482static enum sdw_command_response
483cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
Sanyog Kale956baa12017-12-14 11:19:42 +0530484{
485 int nack = 0, no_ack = 0;
486 unsigned long time;
487 u32 data[2], base;
488 int i;
489
490 /* Program the watermark level for RX FIFO */
491 if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
492 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
493 cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
494 }
495
496 data[0] = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR);
497 data[0] |= 0x3 << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND);
498 data[1] = data[0];
499
500 data[0] |= SDW_SCP_ADDRPAGE1 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
501 data[1] |= SDW_SCP_ADDRPAGE2 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
502
503 data[0] |= msg->addr_page1;
504 data[1] |= msg->addr_page2;
505
506 base = CDNS_MCP_CMD_BASE;
507 cdns_writel(cdns, base, data[0]);
508 base += CDNS_MCP_CMD_WORD_LEN;
509 cdns_writel(cdns, base, data[1]);
510
511 time = wait_for_completion_timeout(&cdns->tx_complete,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500512 msecs_to_jiffies(CDNS_TX_TIMEOUT));
Sanyog Kale956baa12017-12-14 11:19:42 +0530513 if (!time) {
514 dev_err(cdns->dev, "SCP Msg trf timed out\n");
515 msg->len = 0;
516 return SDW_CMD_TIMEOUT;
517 }
518
519 /* check response the writes */
520 for (i = 0; i < 2; i++) {
521 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
522 no_ack = 1;
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500523 dev_err(cdns->dev, "Program SCP Ack not received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530524 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
525 nack = 1;
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500526 dev_err(cdns->dev, "Program SCP NACK received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530527 }
528 }
529 }
530
531 /* For NACK, NO ack, don't return err if we are in Broadcast mode */
532 if (nack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500533 dev_err_ratelimited(cdns->dev,
534 "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530535 return SDW_CMD_FAIL;
536 } else if (no_ack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500537 dev_dbg_ratelimited(cdns->dev,
538 "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530539 return SDW_CMD_IGNORED;
540 }
541
542 return SDW_CMD_OK;
543}
544
545static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
546{
547 int ret;
548
549 if (msg->page) {
550 ret = cdns_program_scp_addr(cdns, msg);
551 if (ret) {
552 msg->len = 0;
553 return ret;
554 }
555 }
556
557 switch (msg->flags) {
558 case SDW_MSG_FLAG_READ:
559 *cmd = CDNS_MCP_CMD_READ;
560 break;
561
562 case SDW_MSG_FLAG_WRITE:
563 *cmd = CDNS_MCP_CMD_WRITE;
564 break;
565
566 default:
567 dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
568 return -EINVAL;
569 }
570
571 return 0;
572}
573
Shreyas NCc91605f2018-04-26 18:38:43 +0530574enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530575cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
576{
577 struct sdw_cdns *cdns = bus_to_cdns(bus);
578 int cmd = 0, ret, i;
579
580 ret = cdns_prep_msg(cdns, msg, &cmd);
581 if (ret)
582 return SDW_CMD_FAIL_OTHER;
583
584 for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
585 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500586 CDNS_MCP_CMD_LEN, false);
Sanyog Kale956baa12017-12-14 11:19:42 +0530587 if (ret < 0)
588 goto exit;
589 }
590
591 if (!(msg->len % CDNS_MCP_CMD_LEN))
592 goto exit;
593
594 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500595 msg->len % CDNS_MCP_CMD_LEN, false);
Sanyog Kale956baa12017-12-14 11:19:42 +0530596
597exit:
598 return ret;
599}
Shreyas NCc91605f2018-04-26 18:38:43 +0530600EXPORT_SYMBOL(cdns_xfer_msg);
Sanyog Kale956baa12017-12-14 11:19:42 +0530601
Shreyas NCc91605f2018-04-26 18:38:43 +0530602enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530603cdns_xfer_msg_defer(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500604 struct sdw_msg *msg, struct sdw_defer *defer)
Sanyog Kale956baa12017-12-14 11:19:42 +0530605{
606 struct sdw_cdns *cdns = bus_to_cdns(bus);
607 int cmd = 0, ret;
608
609 /* for defer only 1 message is supported */
610 if (msg->len > 1)
611 return -ENOTSUPP;
612
613 ret = cdns_prep_msg(cdns, msg, &cmd);
614 if (ret)
615 return SDW_CMD_FAIL_OTHER;
616
617 cdns->defer = defer;
618 cdns->defer->length = msg->len;
619
620 return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
621}
Shreyas NCc91605f2018-04-26 18:38:43 +0530622EXPORT_SYMBOL(cdns_xfer_msg_defer);
Sanyog Kale956baa12017-12-14 11:19:42 +0530623
Shreyas NCc91605f2018-04-26 18:38:43 +0530624enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530625cdns_reset_page_addr(struct sdw_bus *bus, unsigned int dev_num)
626{
627 struct sdw_cdns *cdns = bus_to_cdns(bus);
628 struct sdw_msg msg;
629
630 /* Create dummy message with valid device number */
631 memset(&msg, 0, sizeof(msg));
632 msg.dev_num = dev_num;
633
634 return cdns_program_scp_addr(cdns, &msg);
635}
Shreyas NCc91605f2018-04-26 18:38:43 +0530636EXPORT_SYMBOL(cdns_reset_page_addr);
Sanyog Kale956baa12017-12-14 11:19:42 +0530637
638/*
Vinod Koul2f52a512017-12-14 11:19:41 +0530639 * IRQ handling
640 */
641
Sanyog Kale956baa12017-12-14 11:19:42 +0530642static void cdns_read_response(struct sdw_cdns *cdns)
643{
644 u32 num_resp, cmd_base;
645 int i;
646
647 num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
648 num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
649
650 cmd_base = CDNS_MCP_CMD_BASE;
651
652 for (i = 0; i < num_resp; i++) {
653 cdns->response_buf[i] = cdns_readl(cdns, cmd_base);
654 cmd_base += CDNS_MCP_CMD_WORD_LEN;
655 }
656}
657
Vinod Koul2f52a512017-12-14 11:19:41 +0530658static int cdns_update_slave_status(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500659 u32 slave0, u32 slave1)
Vinod Koul2f52a512017-12-14 11:19:41 +0530660{
661 enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
662 bool is_slave = false;
Pierre-Louis Bossarta78b32d2019-05-22 14:47:27 -0500663 u64 slave;
664 u32 mask;
Vinod Koul2f52a512017-12-14 11:19:41 +0530665 int i, set_status;
666
667 /* combine the two status */
668 slave = ((u64)slave1 << 32) | slave0;
669 memset(status, 0, sizeof(status));
670
671 for (i = 0; i <= SDW_MAX_DEVICES; i++) {
672 mask = (slave >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
673 CDNS_MCP_SLAVE_STATUS_BITS;
674 if (!mask)
675 continue;
676
677 is_slave = true;
678 set_status = 0;
679
680 if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
681 status[i] = SDW_SLAVE_RESERVED;
682 set_status++;
683 }
684
685 if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
686 status[i] = SDW_SLAVE_ATTACHED;
687 set_status++;
688 }
689
690 if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
691 status[i] = SDW_SLAVE_ALERT;
692 set_status++;
693 }
694
695 if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
696 status[i] = SDW_SLAVE_UNATTACHED;
697 set_status++;
698 }
699
700 /* first check if Slave reported multiple status */
701 if (set_status > 1) {
Pierre-Louis Bossart7181b1d2020-01-10 15:57:30 -0600702 u32 val;
703
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500704 dev_warn_ratelimited(cdns->dev,
Pierre-Louis Bossart7181b1d2020-01-10 15:57:30 -0600705 "Slave %d reported multiple Status: %d\n",
706 i, mask);
707
708 /* check latest status extracted from PING commands */
709 val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
710 val >>= (i * 2);
711
712 switch (val & 0x3) {
713 case 0:
714 status[i] = SDW_SLAVE_UNATTACHED;
715 break;
716 case 1:
717 status[i] = SDW_SLAVE_ATTACHED;
718 break;
719 case 2:
720 status[i] = SDW_SLAVE_ALERT;
721 break;
722 case 3:
723 default:
724 status[i] = SDW_SLAVE_RESERVED;
725 break;
726 }
727
728 dev_warn_ratelimited(cdns->dev,
729 "Slave %d status updated to %d\n",
730 i, status[i]);
731
Vinod Koul2f52a512017-12-14 11:19:41 +0530732 }
733 }
734
735 if (is_slave)
736 return sdw_handle_slave_status(&cdns->bus, status);
737
738 return 0;
739}
740
741/**
742 * sdw_cdns_irq() - Cadence interrupt handler
743 * @irq: irq number
744 * @dev_id: irq context
745 */
746irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
747{
748 struct sdw_cdns *cdns = dev_id;
749 u32 int_status;
750 int ret = IRQ_HANDLED;
751
752 /* Check if the link is up */
753 if (!cdns->link_up)
754 return IRQ_NONE;
755
756 int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
757
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -0600758 /* check for reserved values read as zero */
759 if (int_status & CDNS_MCP_INT_RESERVED)
760 return IRQ_NONE;
761
Vinod Koul2f52a512017-12-14 11:19:41 +0530762 if (!(int_status & CDNS_MCP_INT_IRQ))
763 return IRQ_NONE;
764
Sanyog Kale956baa12017-12-14 11:19:42 +0530765 if (int_status & CDNS_MCP_INT_RX_WL) {
766 cdns_read_response(cdns);
767
768 if (cdns->defer) {
769 cdns_fill_msg_resp(cdns, cdns->defer->msg,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500770 cdns->defer->length, 0);
Sanyog Kale956baa12017-12-14 11:19:42 +0530771 complete(&cdns->defer->complete);
772 cdns->defer = NULL;
Pierre-Louis Bossartf6e20962019-05-01 10:57:42 -0500773 } else {
Sanyog Kale956baa12017-12-14 11:19:42 +0530774 complete(&cdns->tx_complete);
Pierre-Louis Bossartf6e20962019-05-01 10:57:42 -0500775 }
Sanyog Kale956baa12017-12-14 11:19:42 +0530776 }
777
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -0500778 if (int_status & CDNS_MCP_INT_PARITY) {
779 /* Parity error detected by Master */
780 dev_err_ratelimited(cdns->dev, "Parity error\n");
781 }
782
Vinod Koul2f52a512017-12-14 11:19:41 +0530783 if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
Vinod Koul2f52a512017-12-14 11:19:41 +0530784 /* Slave is driving bit slot during control word */
785 dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
Vinod Koul2f52a512017-12-14 11:19:41 +0530786 }
787
788 if (int_status & CDNS_MCP_INT_DATA_CLASH) {
789 /*
790 * Multiple slaves trying to drive bit slot, or issue with
791 * ownership of data bits or Slave gone bonkers
792 */
793 dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
Vinod Koul2f52a512017-12-14 11:19:41 +0530794 }
795
796 if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
797 /* Mask the Slave interrupt and wake thread */
798 cdns_updatel(cdns, CDNS_MCP_INTMASK,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500799 CDNS_MCP_INT_SLAVE_MASK, 0);
Vinod Koul2f52a512017-12-14 11:19:41 +0530800
801 int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
802 ret = IRQ_WAKE_THREAD;
803 }
804
805 cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
806 return ret;
807}
808EXPORT_SYMBOL(sdw_cdns_irq);
809
810/**
811 * sdw_cdns_thread() - Cadence irq thread handler
812 * @irq: irq number
813 * @dev_id: irq context
814 */
815irqreturn_t sdw_cdns_thread(int irq, void *dev_id)
816{
817 struct sdw_cdns *cdns = dev_id;
818 u32 slave0, slave1;
819
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500820 dev_dbg_ratelimited(cdns->dev, "Slave status change\n");
Vinod Koul2f52a512017-12-14 11:19:41 +0530821
822 slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
823 slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
824
825 cdns_update_slave_status(cdns, slave0, slave1);
826 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
827 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
828
829 /* clear and unmask Slave interrupt now */
830 cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
831 cdns_updatel(cdns, CDNS_MCP_INTMASK,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500832 CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
Vinod Koul2f52a512017-12-14 11:19:41 +0530833
834 return IRQ_HANDLED;
835}
836EXPORT_SYMBOL(sdw_cdns_thread);
837
838/*
839 * init routines
840 */
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500841
842/**
843 * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
844 * @cdns: Cadence instance
845 */
846int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
847{
848 /* program maximum length reset to be safe */
849 cdns_updatel(cdns, CDNS_MCP_CONTROL,
850 CDNS_MCP_CONTROL_RST_DELAY,
851 CDNS_MCP_CONTROL_RST_DELAY);
852
853 /* use hardware generated reset */
854 cdns_updatel(cdns, CDNS_MCP_CONTROL,
855 CDNS_MCP_CONTROL_HW_RST,
856 CDNS_MCP_CONTROL_HW_RST);
857
858 /* enable bus operations with clock and data */
859 cdns_updatel(cdns, CDNS_MCP_CONFIG,
860 CDNS_MCP_CONFIG_OP,
861 CDNS_MCP_CONFIG_OP_NORMAL);
862
863 /* commit changes */
Pierre-Louis Bossartce1acf02020-03-17 11:33:13 -0500864 return cdns_config_update(cdns);
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500865}
866EXPORT_SYMBOL(sdw_cdns_exit_reset);
867
868/**
Rander Wangae478d62020-01-10 15:57:29 -0600869 * sdw_cdns_enable_interrupt() - Enable SDW interrupts
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500870 * @cdns: Cadence instance
Pierre-Louis Bossart550f9052020-01-14 17:31:24 -0600871 * @state: True if we are trying to enable interrupt.
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500872 */
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500873int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
Sanyog Kale956baa12017-12-14 11:19:42 +0530874{
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500875 u32 slave_intmask0 = 0;
876 u32 slave_intmask1 = 0;
877 u32 mask = 0;
Sanyog Kale956baa12017-12-14 11:19:42 +0530878
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500879 if (!state)
880 goto update_masks;
881
882 slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
883 slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
Sanyog Kale956baa12017-12-14 11:19:42 +0530884
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -0500885 /* enable detection of all slave state changes */
886 mask = CDNS_MCP_INT_SLAVE_MASK;
887
888 /* enable detection of bus issues */
889 mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
890 CDNS_MCP_INT_PARITY;
891
892 /* no detection of port interrupts for now */
893
894 /* enable detection of RX fifo level */
895 mask |= CDNS_MCP_INT_RX_WL;
896
897 /*
898 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
899 * settings are irrelevant
900 */
901 mask |= CDNS_MCP_INT_IRQ;
Sanyog Kale956baa12017-12-14 11:19:42 +0530902
Pierre-Louis Bossart04592dc2019-08-05 19:55:21 -0500903 if (interrupt_mask) /* parameter override */
904 mask = interrupt_mask;
Sanyog Kale956baa12017-12-14 11:19:42 +0530905
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500906update_masks:
Rander Wang5ebb0942020-01-10 15:57:27 -0600907 /* clear slave interrupt status before enabling interrupt */
908 if (state) {
909 u32 slave_state;
910
911 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
912 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state);
913 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
914 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state);
915 }
916
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500917 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
918 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
Sanyog Kale956baa12017-12-14 11:19:42 +0530919 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
920
Rander Wangae478d62020-01-10 15:57:29 -0600921 return 0;
Sanyog Kale956baa12017-12-14 11:19:42 +0530922}
923EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
Vinod Koul2f52a512017-12-14 11:19:41 +0530924
Vinod Koul07abeff2018-04-26 18:38:48 +0530925static int cdns_allocate_pdi(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500926 struct sdw_cdns_pdi **stream,
927 u32 num, u32 pdi_offset)
Vinod Koul07abeff2018-04-26 18:38:48 +0530928{
929 struct sdw_cdns_pdi *pdi;
930 int i;
931
932 if (!num)
933 return 0;
934
935 pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
936 if (!pdi)
937 return -ENOMEM;
938
939 for (i = 0; i < num; i++) {
940 pdi[i].num = i + pdi_offset;
Vinod Koul07abeff2018-04-26 18:38:48 +0530941 }
942
943 *stream = pdi;
944 return 0;
945}
946
947/**
948 * sdw_cdns_pdi_init() - PDI initialization routine
949 *
950 * @cdns: Cadence instance
951 * @config: Stream configurations
952 */
953int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500954 struct sdw_cdns_stream_config config)
Vinod Koul07abeff2018-04-26 18:38:48 +0530955{
956 struct sdw_cdns_streams *stream;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500957 int offset;
958 int ret;
Vinod Koul07abeff2018-04-26 18:38:48 +0530959
960 cdns->pcm.num_bd = config.pcm_bd;
961 cdns->pcm.num_in = config.pcm_in;
962 cdns->pcm.num_out = config.pcm_out;
963 cdns->pdm.num_bd = config.pdm_bd;
964 cdns->pdm.num_in = config.pdm_in;
965 cdns->pdm.num_out = config.pdm_out;
966
967 /* Allocate PDIs for PCMs */
968 stream = &cdns->pcm;
969
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -0500970 /* we allocate PDI0 and PDI1 which are used for Bulk */
971 offset = 0;
Vinod Koul07abeff2018-04-26 18:38:48 +0530972
973 ret = cdns_allocate_pdi(cdns, &stream->bd,
974 stream->num_bd, offset);
975 if (ret)
976 return ret;
977
978 offset += stream->num_bd;
979
980 ret = cdns_allocate_pdi(cdns, &stream->in,
981 stream->num_in, offset);
982 if (ret)
983 return ret;
984
985 offset += stream->num_in;
986
987 ret = cdns_allocate_pdi(cdns, &stream->out,
988 stream->num_out, offset);
989 if (ret)
990 return ret;
991
992 /* Update total number of PCM PDIs */
993 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
994 cdns->num_ports = stream->num_pdi;
995
996 /* Allocate PDIs for PDMs */
997 stream = &cdns->pdm;
Vinod Koul07abeff2018-04-26 18:38:48 +0530998 ret = cdns_allocate_pdi(cdns, &stream->bd,
999 stream->num_bd, offset);
1000 if (ret)
1001 return ret;
1002
1003 offset += stream->num_bd;
1004
1005 ret = cdns_allocate_pdi(cdns, &stream->in,
1006 stream->num_in, offset);
1007 if (ret)
1008 return ret;
1009
1010 offset += stream->num_in;
1011
1012 ret = cdns_allocate_pdi(cdns, &stream->out,
1013 stream->num_out, offset);
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -05001014
Vinod Koul07abeff2018-04-26 18:38:48 +05301015 if (ret)
1016 return ret;
1017
1018 /* Update total number of PDM PDIs */
1019 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1020 cdns->num_ports += stream->num_pdi;
1021
Vinod Koul07abeff2018-04-26 18:38:48 +05301022 return 0;
1023}
1024EXPORT_SYMBOL(sdw_cdns_pdi_init);
1025
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -05001026static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
1027{
1028 u32 val;
1029 int c;
1030 int r;
1031
1032 r = sdw_find_row_index(n_rows);
1033 c = sdw_find_col_index(n_cols) & CDNS_MCP_FRAME_SHAPE_COL_MASK;
1034
1035 val = (r << CDNS_MCP_FRAME_SHAPE_ROW_OFFSET) | c;
1036
1037 return val;
1038}
1039
Vinod Koul2f52a512017-12-14 11:19:41 +05301040/**
1041 * sdw_cdns_init() - Cadence initialization
1042 * @cdns: Cadence instance
1043 */
Rander Wang7b174f22020-03-17 11:33:14 -05001044int sdw_cdns_init(struct sdw_cdns *cdns)
Vinod Koul2f52a512017-12-14 11:19:41 +05301045{
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001046 struct sdw_bus *bus = &cdns->bus;
1047 struct sdw_master_prop *prop = &bus->prop;
Vinod Koul2f52a512017-12-14 11:19:41 +05301048 u32 val;
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001049 int divider;
Vinod Koul2f52a512017-12-14 11:19:41 +05301050
1051 /* Set clock divider */
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001052 divider = (prop->mclk_freq / prop->max_clk_freq) - 1;
Vinod Koul2f52a512017-12-14 11:19:41 +05301053
Rander Wanga50954e2019-08-05 19:55:18 -05001054 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1055 CDNS_MCP_CLK_MCLKD_MASK, divider);
1056 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1057 CDNS_MCP_CLK_MCLKD_MASK, divider);
Vinod Koul2f52a512017-12-14 11:19:41 +05301058
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -05001059 /*
1060 * Frame shape changes after initialization have to be done
1061 * with the bank switch mechanism
1062 */
1063 val = cdns_set_initial_frame_shape(prop->default_row,
1064 prop->default_col);
1065 cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
Vinod Koul2f52a512017-12-14 11:19:41 +05301066
1067 /* Set SSP interval to default value */
1068 cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, CDNS_DEFAULT_SSP_INTERVAL);
1069 cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, CDNS_DEFAULT_SSP_INTERVAL);
1070
Rander Wang0d667d02020-03-17 11:33:18 -05001071 /* reset msg_count to default value of FIFOLEVEL */
1072 cdns->msg_count = cdns_readl(cdns, CDNS_MCP_FIFOLEVEL);
1073
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001074 /* flush command FIFOs */
1075 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1076 CDNS_MCP_CONTROL_CMD_RST);
1077
Vinod Koul2f52a512017-12-14 11:19:41 +05301078 /* Set cmd accept mode */
1079 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001080 CDNS_MCP_CONTROL_CMD_ACCEPT);
Vinod Koul2f52a512017-12-14 11:19:41 +05301081
1082 /* Configure mcp config */
1083 val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1084
1085 /* Set Max cmd retry to 15 */
1086 val |= CDNS_MCP_CONFIG_MCMD_RETRY;
1087
1088 /* Set frame delay between PREQ and ping frame to 15 frames */
1089 val |= 0xF << SDW_REG_SHIFT(CDNS_MCP_CONFIG_MPREQ_DELAY);
1090
1091 /* Disable auto bus release */
1092 val &= ~CDNS_MCP_CONFIG_BUS_REL;
1093
1094 /* Disable sniffer mode */
1095 val &= ~CDNS_MCP_CONFIG_SNIFFER;
1096
1097 /* Set cmd mode for Tx and Rx cmds */
1098 val &= ~CDNS_MCP_CONFIG_CMD;
1099
Vinod Koul2f52a512017-12-14 11:19:41 +05301100 cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1101
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001102 /* commit changes */
Pierre-Louis Bossartce1acf02020-03-17 11:33:13 -05001103 return cdns_config_update(cdns);
Vinod Koul2f52a512017-12-14 11:19:41 +05301104}
1105EXPORT_SYMBOL(sdw_cdns_init);
1106
Vinod Koul07abeff2018-04-26 18:38:48 +05301107int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1108{
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001109 struct sdw_master_prop *prop = &bus->prop;
Vinod Koul07abeff2018-04-26 18:38:48 +05301110 struct sdw_cdns *cdns = bus_to_cdns(bus);
Rander Wanga50954e2019-08-05 19:55:18 -05001111 int mcp_clkctrl_off;
Vinod Koul07abeff2018-04-26 18:38:48 +05301112 int divider;
1113
1114 if (!params->curr_dr_freq) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001115 dev_err(cdns->dev, "NULL curr_dr_freq\n");
Vinod Koul07abeff2018-04-26 18:38:48 +05301116 return -EINVAL;
1117 }
1118
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001119 divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1120 params->curr_dr_freq;
1121 divider--; /* divider is 1/(N+1) */
Vinod Koul07abeff2018-04-26 18:38:48 +05301122
1123 if (params->next_bank)
1124 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1125 else
1126 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1127
Rander Wanga50954e2019-08-05 19:55:18 -05001128 cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
Vinod Koul07abeff2018-04-26 18:38:48 +05301129
1130 return 0;
1131}
1132EXPORT_SYMBOL(cdns_bus_conf);
1133
1134static int cdns_port_params(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001135 struct sdw_port_params *p_params, unsigned int bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301136{
1137 struct sdw_cdns *cdns = bus_to_cdns(bus);
1138 int dpn_config = 0, dpn_config_off;
1139
1140 if (bank)
1141 dpn_config_off = CDNS_DPN_B1_CONFIG(p_params->num);
1142 else
1143 dpn_config_off = CDNS_DPN_B0_CONFIG(p_params->num);
1144
1145 dpn_config = cdns_readl(cdns, dpn_config_off);
1146
1147 dpn_config |= ((p_params->bps - 1) <<
1148 SDW_REG_SHIFT(CDNS_DPN_CONFIG_WL));
1149 dpn_config |= (p_params->flow_mode <<
1150 SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_FLOW));
1151 dpn_config |= (p_params->data_mode <<
1152 SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_DAT));
1153
1154 cdns_writel(cdns, dpn_config_off, dpn_config);
1155
1156 return 0;
1157}
1158
1159static int cdns_transport_params(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001160 struct sdw_transport_params *t_params,
1161 enum sdw_reg_bank bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301162{
1163 struct sdw_cdns *cdns = bus_to_cdns(bus);
1164 int dpn_offsetctrl = 0, dpn_offsetctrl_off;
1165 int dpn_config = 0, dpn_config_off;
1166 int dpn_hctrl = 0, dpn_hctrl_off;
1167 int num = t_params->port_num;
1168 int dpn_samplectrl_off;
1169
1170 /*
1171 * Note: Only full data port is supported on the Master side for
1172 * both PCM and PDM ports.
1173 */
1174
1175 if (bank) {
1176 dpn_config_off = CDNS_DPN_B1_CONFIG(num);
1177 dpn_samplectrl_off = CDNS_DPN_B1_SAMPLE_CTRL(num);
1178 dpn_hctrl_off = CDNS_DPN_B1_HCTRL(num);
1179 dpn_offsetctrl_off = CDNS_DPN_B1_OFFSET_CTRL(num);
1180 } else {
1181 dpn_config_off = CDNS_DPN_B0_CONFIG(num);
1182 dpn_samplectrl_off = CDNS_DPN_B0_SAMPLE_CTRL(num);
1183 dpn_hctrl_off = CDNS_DPN_B0_HCTRL(num);
1184 dpn_offsetctrl_off = CDNS_DPN_B0_OFFSET_CTRL(num);
1185 }
1186
1187 dpn_config = cdns_readl(cdns, dpn_config_off);
1188
1189 dpn_config |= (t_params->blk_grp_ctrl <<
1190 SDW_REG_SHIFT(CDNS_DPN_CONFIG_BGC));
1191 dpn_config |= (t_params->blk_pkg_mode <<
1192 SDW_REG_SHIFT(CDNS_DPN_CONFIG_BPM));
1193 cdns_writel(cdns, dpn_config_off, dpn_config);
1194
1195 dpn_offsetctrl |= (t_params->offset1 <<
1196 SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_1));
1197 dpn_offsetctrl |= (t_params->offset2 <<
1198 SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_2));
1199 cdns_writel(cdns, dpn_offsetctrl_off, dpn_offsetctrl);
1200
1201 dpn_hctrl |= (t_params->hstart <<
1202 SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTART));
1203 dpn_hctrl |= (t_params->hstop << SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTOP));
1204 dpn_hctrl |= (t_params->lane_ctrl <<
1205 SDW_REG_SHIFT(CDNS_DPN_HCTRL_LCTRL));
1206
1207 cdns_writel(cdns, dpn_hctrl_off, dpn_hctrl);
1208 cdns_writel(cdns, dpn_samplectrl_off, (t_params->sample_interval - 1));
1209
1210 return 0;
1211}
1212
1213static int cdns_port_enable(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001214 struct sdw_enable_ch *enable_ch, unsigned int bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301215{
1216 struct sdw_cdns *cdns = bus_to_cdns(bus);
1217 int dpn_chnen_off, ch_mask;
1218
1219 if (bank)
1220 dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1221 else
1222 dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1223
1224 ch_mask = enable_ch->ch_mask * enable_ch->enable;
1225 cdns_writel(cdns, dpn_chnen_off, ch_mask);
1226
1227 return 0;
1228}
1229
1230static const struct sdw_master_port_ops cdns_port_ops = {
1231 .dpn_set_port_params = cdns_port_params,
1232 .dpn_set_port_transport_params = cdns_transport_params,
1233 .dpn_port_enable_ch = cdns_port_enable,
1234};
1235
Sanyog Kale956baa12017-12-14 11:19:42 +05301236/**
Rander Wang5a885c52020-03-17 11:33:15 -05001237 * sdw_cdns_is_clock_stop: Check clock status
1238 *
1239 * @cdns: Cadence instance
1240 */
1241bool sdw_cdns_is_clock_stop(struct sdw_cdns *cdns)
1242{
1243 return !!(cdns_readl(cdns, CDNS_MCP_STAT) & CDNS_MCP_STAT_CLK_STOP);
1244}
1245EXPORT_SYMBOL(sdw_cdns_is_clock_stop);
1246
1247/**
Rander Wang10325042020-03-17 11:33:17 -05001248 * sdw_cdns_clock_stop: Cadence clock stop configuration routine
1249 *
1250 * @cdns: Cadence instance
1251 * @block_wake: prevent wakes if required by the platform
1252 */
1253int sdw_cdns_clock_stop(struct sdw_cdns *cdns, bool block_wake)
1254{
1255 bool slave_present = false;
1256 struct sdw_slave *slave;
1257 int ret;
1258
1259 /* Check suspend status */
1260 if (sdw_cdns_is_clock_stop(cdns)) {
1261 dev_dbg(cdns->dev, "Clock is already stopped\n");
1262 return 0;
1263 }
1264
1265 /*
1266 * For specific platforms, it is required to be able to put
1267 * master into a state in which it ignores wake-up trials
1268 * in clock stop state
1269 */
1270 if (block_wake)
1271 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1272 CDNS_MCP_CONTROL_BLOCK_WAKEUP,
1273 CDNS_MCP_CONTROL_BLOCK_WAKEUP);
1274
1275 list_for_each_entry(slave, &cdns->bus.slaves, node) {
1276 if (slave->status == SDW_SLAVE_ATTACHED ||
1277 slave->status == SDW_SLAVE_ALERT) {
1278 slave_present = true;
1279 break;
1280 }
1281 }
1282
1283 /*
1284 * This CMD_ACCEPT should be used when there are no devices
1285 * attached on the link when entering clock stop mode. If this is
1286 * not set and there is a broadcast write then the command ignored
1287 * will be treated as a failure
1288 */
1289 if (!slave_present)
1290 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1291 CDNS_MCP_CONTROL_CMD_ACCEPT,
1292 CDNS_MCP_CONTROL_CMD_ACCEPT);
1293 else
1294 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1295 CDNS_MCP_CONTROL_CMD_ACCEPT, 0);
1296
1297 /* commit changes */
1298 ret = cdns_config_update(cdns);
1299 if (ret < 0) {
1300 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1301 return ret;
1302 }
1303
1304 /* Prepare slaves for clock stop */
1305 ret = sdw_bus_prep_clk_stop(&cdns->bus);
1306 if (ret < 0) {
1307 dev_err(cdns->dev, "prepare clock stop failed %d", ret);
1308 return ret;
1309 }
1310
1311 /*
1312 * Enter clock stop mode and only report errors if there are
1313 * Slave devices present (ALERT or ATTACHED)
1314 */
1315 ret = sdw_bus_clk_stop(&cdns->bus);
1316 if (ret < 0 && slave_present && ret != -ENODATA) {
1317 dev_err(cdns->dev, "bus clock stop failed %d", ret);
1318 return ret;
1319 }
1320
1321 ret = cdns_set_wait(cdns, CDNS_MCP_STAT,
1322 CDNS_MCP_STAT_CLK_STOP,
1323 CDNS_MCP_STAT_CLK_STOP);
1324 if (ret < 0)
1325 dev_err(cdns->dev, "Clock stop failed %d\n", ret);
1326
1327 return ret;
1328}
1329EXPORT_SYMBOL(sdw_cdns_clock_stop);
1330
1331/**
1332 * sdw_cdns_clock_restart: Cadence PM clock restart configuration routine
1333 *
1334 * @cdns: Cadence instance
1335 * @bus_reset: context may be lost while in low power modes and the bus
1336 * may require a Severe Reset and re-enumeration after a wake.
1337 */
1338int sdw_cdns_clock_restart(struct sdw_cdns *cdns, bool bus_reset)
1339{
1340 int ret;
1341
1342 ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
1343 CDNS_MCP_CONTROL_CLK_STOP_CLR);
1344 if (ret < 0) {
1345 dev_err(cdns->dev, "Couldn't exit from clock stop\n");
1346 return ret;
1347 }
1348
1349 ret = cdns_set_wait(cdns, CDNS_MCP_STAT, CDNS_MCP_STAT_CLK_STOP, 0);
1350 if (ret < 0) {
1351 dev_err(cdns->dev, "clock stop exit failed %d\n", ret);
1352 return ret;
1353 }
1354
1355 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1356 CDNS_MCP_CONTROL_BLOCK_WAKEUP, 0);
1357
1358 /*
1359 * clear CMD_ACCEPT so that the command ignored
1360 * will be treated as a failure during a broadcast write
1361 */
1362 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT, 0);
1363
1364 if (!bus_reset) {
1365
1366 /* enable bus operations with clock and data */
1367 cdns_updatel(cdns, CDNS_MCP_CONFIG,
1368 CDNS_MCP_CONFIG_OP,
1369 CDNS_MCP_CONFIG_OP_NORMAL);
1370
1371 ret = cdns_config_update(cdns);
1372 if (ret < 0) {
1373 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1374 return ret;
1375 }
1376
1377 ret = sdw_bus_exit_clk_stop(&cdns->bus);
1378 if (ret < 0)
1379 dev_err(cdns->dev, "bus failed to exit clock stop %d\n", ret);
1380 }
1381
1382 return ret;
1383}
1384EXPORT_SYMBOL(sdw_cdns_clock_restart);
1385
1386/**
Sanyog Kale956baa12017-12-14 11:19:42 +05301387 * sdw_cdns_probe() - Cadence probe routine
1388 * @cdns: Cadence instance
1389 */
1390int sdw_cdns_probe(struct sdw_cdns *cdns)
1391{
1392 init_completion(&cdns->tx_complete);
Vinod Koul07abeff2018-04-26 18:38:48 +05301393 cdns->bus.port_ops = &cdns_port_ops;
Sanyog Kale956baa12017-12-14 11:19:42 +05301394
1395 return 0;
1396}
1397EXPORT_SYMBOL(sdw_cdns_probe);
1398
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301399int cdns_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001400 void *stream, bool pcm, int direction)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301401{
1402 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1403 struct sdw_cdns_dma_data *dma;
1404
1405 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
1406 if (!dma)
1407 return -ENOMEM;
1408
1409 if (pcm)
1410 dma->stream_type = SDW_STREAM_PCM;
1411 else
1412 dma->stream_type = SDW_STREAM_PDM;
1413
1414 dma->bus = &cdns->bus;
1415 dma->link_id = cdns->instance;
1416
1417 dma->stream = stream;
1418
1419 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1420 dai->playback_dma_data = dma;
1421 else
1422 dai->capture_dma_data = dma;
1423
1424 return 0;
1425}
1426EXPORT_SYMBOL(cdns_set_sdw_stream);
1427
1428/**
1429 * cdns_find_pdi() - Find a free PDI
1430 *
1431 * @cdns: Cadence instance
Pierre-Louis Bossart39737a32020-01-13 15:10:24 -06001432 * @offset: Starting offset
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301433 * @num: Number of PDIs
1434 * @pdi: PDI instances
Pierre-Louis Bossart39737a32020-01-13 15:10:24 -06001435 * @dai_id: DAI id
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301436 *
Bard Liao1b53385e2019-09-16 14:23:48 -05001437 * Find a PDI for a given PDI array. The PDI num and dai_id are
1438 * expected to match, return NULL otherwise.
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301439 */
1440static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -05001441 unsigned int offset,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001442 unsigned int num,
Bard Liao1b53385e2019-09-16 14:23:48 -05001443 struct sdw_cdns_pdi *pdi,
1444 int dai_id)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301445{
1446 int i;
1447
Bard Liao1b53385e2019-09-16 14:23:48 -05001448 for (i = offset; i < offset + num; i++)
1449 if (pdi[i].num == dai_id)
1450 return &pdi[i];
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301451
1452 return NULL;
1453}
1454
1455/**
1456 * sdw_cdns_config_stream: Configure a stream
1457 *
1458 * @cdns: Cadence instance
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301459 * @ch: Channel count
1460 * @dir: Data direction
1461 * @pdi: PDI to be used
1462 */
1463void sdw_cdns_config_stream(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001464 u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301465{
1466 u32 offset, val = 0;
1467
1468 if (dir == SDW_DATA_DIR_RX)
1469 val = CDNS_PORTCTRL_DIRN;
1470
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001471 offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301472 cdns_updatel(cdns, offset, CDNS_PORTCTRL_DIRN, val);
1473
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001474 val = pdi->num;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301475 val |= ((1 << ch) - 1) << SDW_REG_SHIFT(CDNS_PDI_CONFIG_CHANNEL);
1476 cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1477}
1478EXPORT_SYMBOL(sdw_cdns_config_stream);
1479
1480/**
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001481 * sdw_cdns_alloc_pdi() - Allocate a PDI
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301482 *
1483 * @cdns: Cadence instance
1484 * @stream: Stream to be allocated
1485 * @ch: Channel count
1486 * @dir: Data direction
Pierre-Louis Bossart39737a32020-01-13 15:10:24 -06001487 * @dai_id: DAI id
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301488 */
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001489struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1490 struct sdw_cdns_streams *stream,
Bard Liao1b53385e2019-09-16 14:23:48 -05001491 u32 ch, u32 dir, int dai_id)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301492{
1493 struct sdw_cdns_pdi *pdi = NULL;
1494
1495 if (dir == SDW_DATA_DIR_RX)
Bard Liao1b53385e2019-09-16 14:23:48 -05001496 pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1497 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301498 else
Bard Liao1b53385e2019-09-16 14:23:48 -05001499 pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1500 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301501
1502 /* check if we found a PDI, else find in bi-directional */
1503 if (!pdi)
Bard Liao1b53385e2019-09-16 14:23:48 -05001504 pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd,
1505 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301506
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001507 if (pdi) {
1508 pdi->l_ch_num = 0;
1509 pdi->h_ch_num = ch - 1;
1510 pdi->dir = dir;
1511 pdi->ch_count = ch;
1512 }
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301513
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001514 return pdi;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301515}
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001516EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301517
Vinod Koul2f52a512017-12-14 11:19:41 +05301518MODULE_LICENSE("Dual BSD/GPL");
1519MODULE_DESCRIPTION("Cadence Soundwire Library");