blob: 558390af44b64d34f888b59cd8b2d677845e183d [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>
Pierre-Louis Bossart32d2a892020-09-08 21:45:21 +080016#include <linux/pm_runtime.h>
Vinod Koul2f52a512017-12-14 11:19:41 +053017#include <linux/soundwire/sdw_registers.h>
18#include <linux/soundwire/sdw.h>
Vinod Koul5d6b3c82018-04-26 18:38:53 +053019#include <sound/pcm_params.h>
20#include <sound/soc.h>
Bard Liao4a98a6b2020-07-16 23:09:45 +080021#include <linux/workqueue.h>
Vinod Koul2f52a512017-12-14 11:19:41 +053022#include "bus.h"
23#include "cadence_master.h"
24
Pierre-Louis Bossart04592dc2019-08-05 19:55:21 -050025static int interrupt_mask;
26module_param_named(cnds_mcp_int_mask, interrupt_mask, int, 0444);
27MODULE_PARM_DESC(cdns_mcp_int_mask, "Cadence MCP IntMask");
28
Vinod Koul2f52a512017-12-14 11:19:41 +053029#define CDNS_MCP_CONFIG 0x0
30
31#define CDNS_MCP_CONFIG_MCMD_RETRY GENMASK(27, 24)
32#define CDNS_MCP_CONFIG_MPREQ_DELAY GENMASK(20, 16)
33#define CDNS_MCP_CONFIG_MMASTER BIT(7)
34#define CDNS_MCP_CONFIG_BUS_REL BIT(6)
35#define CDNS_MCP_CONFIG_SNIFFER BIT(5)
36#define CDNS_MCP_CONFIG_SSPMOD BIT(4)
37#define CDNS_MCP_CONFIG_CMD BIT(3)
38#define CDNS_MCP_CONFIG_OP GENMASK(2, 0)
39#define CDNS_MCP_CONFIG_OP_NORMAL 0
40
41#define CDNS_MCP_CONTROL 0x4
42
43#define CDNS_MCP_CONTROL_RST_DELAY GENMASK(10, 8)
44#define CDNS_MCP_CONTROL_CMD_RST BIT(7)
45#define CDNS_MCP_CONTROL_SOFT_RST BIT(6)
46#define CDNS_MCP_CONTROL_SW_RST BIT(5)
47#define CDNS_MCP_CONTROL_HW_RST BIT(4)
48#define CDNS_MCP_CONTROL_CLK_PAUSE BIT(3)
49#define CDNS_MCP_CONTROL_CLK_STOP_CLR BIT(2)
50#define CDNS_MCP_CONTROL_CMD_ACCEPT BIT(1)
51#define CDNS_MCP_CONTROL_BLOCK_WAKEUP BIT(0)
52
Vinod Koul2f52a512017-12-14 11:19:41 +053053#define CDNS_MCP_CMDCTRL 0x8
Pierre-Louis Bossart32d2a892020-09-08 21:45:21 +080054
55#define CDNS_MCP_CMDCTRL_INSERT_PARITY_ERR BIT(2)
56
Vinod Koul2f52a512017-12-14 11:19:41 +053057#define CDNS_MCP_SSPSTAT 0xC
58#define CDNS_MCP_FRAME_SHAPE 0x10
59#define CDNS_MCP_FRAME_SHAPE_INIT 0x14
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -050060#define CDNS_MCP_FRAME_SHAPE_COL_MASK GENMASK(2, 0)
Vinod Koul3cf25d62020-09-03 17:15:01 +053061#define CDNS_MCP_FRAME_SHAPE_ROW_MASK GENMASK(7, 3)
Vinod Koul2f52a512017-12-14 11:19:41 +053062
63#define CDNS_MCP_CONFIG_UPDATE 0x18
64#define CDNS_MCP_CONFIG_UPDATE_BIT BIT(0)
65
66#define CDNS_MCP_PHYCTRL 0x1C
67#define CDNS_MCP_SSP_CTRL0 0x20
68#define CDNS_MCP_SSP_CTRL1 0x28
69#define CDNS_MCP_CLK_CTRL0 0x30
70#define CDNS_MCP_CLK_CTRL1 0x38
Rander Wanga50954e2019-08-05 19:55:18 -050071#define CDNS_MCP_CLK_MCLKD_MASK GENMASK(7, 0)
Vinod Koul2f52a512017-12-14 11:19:41 +053072
73#define CDNS_MCP_STAT 0x40
74
75#define CDNS_MCP_STAT_ACTIVE_BANK BIT(20)
76#define CDNS_MCP_STAT_CLK_STOP BIT(16)
77
78#define CDNS_MCP_INTSTAT 0x44
79#define CDNS_MCP_INTMASK 0x48
80
81#define CDNS_MCP_INT_IRQ BIT(31)
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -060082#define CDNS_MCP_INT_RESERVED1 GENMASK(30, 17)
Vinod Koul2f52a512017-12-14 11:19:41 +053083#define CDNS_MCP_INT_WAKEUP BIT(16)
84#define CDNS_MCP_INT_SLAVE_RSVD BIT(15)
85#define CDNS_MCP_INT_SLAVE_ALERT BIT(14)
86#define CDNS_MCP_INT_SLAVE_ATTACH BIT(13)
87#define CDNS_MCP_INT_SLAVE_NATTACH BIT(12)
88#define CDNS_MCP_INT_SLAVE_MASK GENMASK(15, 12)
89#define CDNS_MCP_INT_DPINT BIT(11)
90#define CDNS_MCP_INT_CTRL_CLASH BIT(10)
91#define CDNS_MCP_INT_DATA_CLASH BIT(9)
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -050092#define CDNS_MCP_INT_PARITY BIT(8)
Vinod Koul2f52a512017-12-14 11:19:41 +053093#define CDNS_MCP_INT_CMD_ERR BIT(7)
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -060094#define CDNS_MCP_INT_RESERVED2 GENMASK(6, 4)
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -050095#define CDNS_MCP_INT_RX_NE BIT(3)
Vinod Koul2f52a512017-12-14 11:19:41 +053096#define CDNS_MCP_INT_RX_WL BIT(2)
97#define CDNS_MCP_INT_TXE BIT(1)
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -050098#define CDNS_MCP_INT_TXF BIT(0)
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -060099#define CDNS_MCP_INT_RESERVED (CDNS_MCP_INT_RESERVED1 | CDNS_MCP_INT_RESERVED2)
Vinod Koul2f52a512017-12-14 11:19:41 +0530100
101#define CDNS_MCP_INTSET 0x4C
102
Pierre-Louis Bossartb07dd9b2019-07-25 18:40:05 -0500103#define CDNS_MCP_SLAVE_STAT 0x50
104#define CDNS_MCP_SLAVE_STAT_MASK GENMASK(1, 0)
Vinod Koul2f52a512017-12-14 11:19:41 +0530105
106#define CDNS_MCP_SLAVE_INTSTAT0 0x54
107#define CDNS_MCP_SLAVE_INTSTAT1 0x58
108#define CDNS_MCP_SLAVE_INTSTAT_NPRESENT BIT(0)
109#define CDNS_MCP_SLAVE_INTSTAT_ATTACHED BIT(1)
110#define CDNS_MCP_SLAVE_INTSTAT_ALERT BIT(2)
111#define CDNS_MCP_SLAVE_INTSTAT_RESERVED BIT(3)
112#define CDNS_MCP_SLAVE_STATUS_BITS GENMASK(3, 0)
113#define CDNS_MCP_SLAVE_STATUS_NUM 4
114
115#define CDNS_MCP_SLAVE_INTMASK0 0x5C
116#define CDNS_MCP_SLAVE_INTMASK1 0x60
117
Pierre-Louis Bossart664b1652019-07-25 18:40:06 -0500118#define CDNS_MCP_SLAVE_INTMASK0_MASK GENMASK(31, 0)
119#define CDNS_MCP_SLAVE_INTMASK1_MASK GENMASK(15, 0)
Vinod Koul2f52a512017-12-14 11:19:41 +0530120
121#define CDNS_MCP_PORT_INTSTAT 0x64
122#define CDNS_MCP_PDI_STAT 0x6C
123
124#define CDNS_MCP_FIFOLEVEL 0x78
125#define CDNS_MCP_FIFOSTAT 0x7C
126#define CDNS_MCP_RX_FIFO_AVAIL GENMASK(5, 0)
127
128#define CDNS_MCP_CMD_BASE 0x80
129#define CDNS_MCP_RESP_BASE 0x80
130#define CDNS_MCP_CMD_LEN 0x20
131#define CDNS_MCP_CMD_WORD_LEN 0x4
132
133#define CDNS_MCP_CMD_SSP_TAG BIT(31)
134#define CDNS_MCP_CMD_COMMAND GENMASK(30, 28)
135#define CDNS_MCP_CMD_DEV_ADDR GENMASK(27, 24)
Vinod Koul3cf25d62020-09-03 17:15:01 +0530136#define CDNS_MCP_CMD_REG_ADDR GENMASK(23, 8)
Vinod Koul2f52a512017-12-14 11:19:41 +0530137#define CDNS_MCP_CMD_REG_DATA GENMASK(7, 0)
138
139#define CDNS_MCP_CMD_READ 2
140#define CDNS_MCP_CMD_WRITE 3
141
142#define CDNS_MCP_RESP_RDATA GENMASK(15, 8)
143#define CDNS_MCP_RESP_ACK BIT(0)
144#define CDNS_MCP_RESP_NACK BIT(1)
145
146#define CDNS_DP_SIZE 128
147
148#define CDNS_DPN_B0_CONFIG(n) (0x100 + CDNS_DP_SIZE * (n))
149#define CDNS_DPN_B0_CH_EN(n) (0x104 + CDNS_DP_SIZE * (n))
150#define CDNS_DPN_B0_SAMPLE_CTRL(n) (0x108 + CDNS_DP_SIZE * (n))
151#define CDNS_DPN_B0_OFFSET_CTRL(n) (0x10C + CDNS_DP_SIZE * (n))
152#define CDNS_DPN_B0_HCTRL(n) (0x110 + CDNS_DP_SIZE * (n))
153#define CDNS_DPN_B0_ASYNC_CTRL(n) (0x114 + CDNS_DP_SIZE * (n))
154
155#define CDNS_DPN_B1_CONFIG(n) (0x118 + CDNS_DP_SIZE * (n))
156#define CDNS_DPN_B1_CH_EN(n) (0x11C + CDNS_DP_SIZE * (n))
157#define CDNS_DPN_B1_SAMPLE_CTRL(n) (0x120 + CDNS_DP_SIZE * (n))
158#define CDNS_DPN_B1_OFFSET_CTRL(n) (0x124 + CDNS_DP_SIZE * (n))
159#define CDNS_DPN_B1_HCTRL(n) (0x128 + CDNS_DP_SIZE * (n))
160#define CDNS_DPN_B1_ASYNC_CTRL(n) (0x12C + CDNS_DP_SIZE * (n))
161
162#define CDNS_DPN_CONFIG_BPM BIT(18)
163#define CDNS_DPN_CONFIG_BGC GENMASK(17, 16)
164#define CDNS_DPN_CONFIG_WL GENMASK(12, 8)
165#define CDNS_DPN_CONFIG_PORT_DAT GENMASK(3, 2)
166#define CDNS_DPN_CONFIG_PORT_FLOW GENMASK(1, 0)
167
168#define CDNS_DPN_SAMPLE_CTRL_SI GENMASK(15, 0)
169
170#define CDNS_DPN_OFFSET_CTRL_1 GENMASK(7, 0)
171#define CDNS_DPN_OFFSET_CTRL_2 GENMASK(15, 8)
172
173#define CDNS_DPN_HCTRL_HSTOP GENMASK(3, 0)
174#define CDNS_DPN_HCTRL_HSTART GENMASK(7, 4)
175#define CDNS_DPN_HCTRL_LCTRL GENMASK(10, 8)
176
177#define CDNS_PORTCTRL 0x130
Pierre-Louis Bossart9e4e6012020-09-21 03:32:07 +0800178#define CDNS_PORTCTRL_TEST_FAILED BIT(1)
Vinod Koul2f52a512017-12-14 11:19:41 +0530179#define CDNS_PORTCTRL_DIRN BIT(7)
180#define CDNS_PORTCTRL_BANK_INVERT BIT(8)
181
182#define CDNS_PORT_OFFSET 0x80
183
184#define CDNS_PDI_CONFIG(n) (0x1100 + (n) * 16)
185
186#define CDNS_PDI_CONFIG_SOFT_RESET BIT(24)
187#define CDNS_PDI_CONFIG_CHANNEL GENMASK(15, 8)
188#define CDNS_PDI_CONFIG_PORT GENMASK(4, 0)
189
190/* Driver defaults */
Pierre-Louis Bossart0cff9912021-01-15 14:16:51 +0800191#define CDNS_TX_TIMEOUT 500
Vinod Koul2f52a512017-12-14 11:19:41 +0530192
Vinod Koul2f52a512017-12-14 11:19:41 +0530193#define CDNS_SCP_RX_FIFOLEVEL 0x2
194
195/*
196 * register accessor helpers
197 */
198static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset)
199{
200 return readl(cdns->registers + offset);
201}
202
203static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value)
204{
205 writel(value, cdns->registers + offset);
206}
207
208static inline void cdns_updatel(struct sdw_cdns *cdns,
209 int offset, u32 mask, u32 val)
210{
211 u32 tmp;
212
213 tmp = cdns_readl(cdns, offset);
214 tmp = (tmp & ~mask) | val;
215 cdns_writel(cdns, offset, tmp);
216}
217
Rander Wang10325042020-03-17 11:33:17 -0500218static int cdns_set_wait(struct sdw_cdns *cdns, int offset, u32 mask, u32 value)
219{
220 int timeout = 10;
221 u32 reg_read;
222
223 /* Wait for bit to be set */
224 do {
225 reg_read = readl(cdns->registers + offset);
226 if ((reg_read & mask) == value)
227 return 0;
228
229 timeout--;
230 usleep_range(50, 100);
231 } while (timeout != 0);
232
233 return -ETIMEDOUT;
234}
235
Pierre-Louis Bossart12632452020-03-17 11:33:20 -0500236static int cdns_clear_bit(struct sdw_cdns *cdns, int offset, u32 value)
237{
238 writel(value, cdns->registers + offset);
239
240 /* Wait for bit to be self cleared */
241 return cdns_set_wait(cdns, offset, value, 0);
242}
243
Vinod Koul2f52a512017-12-14 11:19:41 +0530244/*
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500245 * all changes to the MCP_CONFIG, MCP_CONTROL, MCP_CMDCTRL and MCP_PHYCTRL
246 * need to be confirmed with a write to MCP_CONFIG_UPDATE
247 */
Pierre-Louis Bossartce1acf02020-03-17 11:33:13 -0500248static int cdns_config_update(struct sdw_cdns *cdns)
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500249{
250 int ret;
251
Pierre-Louis Bossart9bc87cc2020-03-17 11:33:16 -0500252 if (sdw_cdns_is_clock_stop(cdns)) {
253 dev_err(cdns->dev, "Cannot program MCP_CONFIG_UPDATE in ClockStopMode\n");
254 return -EINVAL;
255 }
256
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500257 ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE,
258 CDNS_MCP_CONFIG_UPDATE_BIT);
259 if (ret < 0)
260 dev_err(cdns->dev, "Config update timedout\n");
261
262 return ret;
263}
264
265/*
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500266 * debugfs
267 */
268#ifdef CONFIG_DEBUG_FS
269
270#define RD_BUF (2 * PAGE_SIZE)
271
272static ssize_t cdns_sprintf(struct sdw_cdns *cdns,
273 char *buf, size_t pos, unsigned int reg)
274{
275 return scnprintf(buf + pos, RD_BUF - pos,
276 "%4x\t%8x\n", reg, cdns_readl(cdns, reg));
277}
278
279static int cdns_reg_show(struct seq_file *s, void *data)
280{
281 struct sdw_cdns *cdns = s->private;
282 char *buf;
283 ssize_t ret;
284 int num_ports;
285 int i, j;
286
287 buf = kzalloc(RD_BUF, GFP_KERNEL);
288 if (!buf)
289 return -ENOMEM;
290
291 ret = scnprintf(buf, RD_BUF, "Register Value\n");
292 ret += scnprintf(buf + ret, RD_BUF - ret, "\nMCP Registers\n");
293 /* 8 MCP registers */
294 for (i = CDNS_MCP_CONFIG; i <= CDNS_MCP_PHYCTRL; i += sizeof(u32))
295 ret += cdns_sprintf(cdns, buf, ret, i);
296
297 ret += scnprintf(buf + ret, RD_BUF - ret,
298 "\nStatus & Intr Registers\n");
299 /* 13 Status & Intr registers (offsets 0x70 and 0x74 not defined) */
300 for (i = CDNS_MCP_STAT; i <= CDNS_MCP_FIFOSTAT; i += sizeof(u32))
301 ret += cdns_sprintf(cdns, buf, ret, i);
302
303 ret += scnprintf(buf + ret, RD_BUF - ret,
304 "\nSSP & Clk ctrl Registers\n");
305 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL0);
306 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL1);
307 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL0);
308 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL1);
309
310 ret += scnprintf(buf + ret, RD_BUF - ret,
311 "\nDPn B0 Registers\n");
312
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -0500313 num_ports = cdns->num_ports;
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500314
315 for (i = 0; i < num_ports; i++) {
316 ret += scnprintf(buf + ret, RD_BUF - ret,
317 "\nDP-%d\n", i);
318 for (j = CDNS_DPN_B0_CONFIG(i);
319 j < CDNS_DPN_B0_ASYNC_CTRL(i); j += sizeof(u32))
320 ret += cdns_sprintf(cdns, buf, ret, j);
321 }
322
323 ret += scnprintf(buf + ret, RD_BUF - ret,
324 "\nDPn B1 Registers\n");
325 for (i = 0; i < num_ports; i++) {
326 ret += scnprintf(buf + ret, RD_BUF - ret,
327 "\nDP-%d\n", i);
328
329 for (j = CDNS_DPN_B1_CONFIG(i);
330 j < CDNS_DPN_B1_ASYNC_CTRL(i); j += sizeof(u32))
331 ret += cdns_sprintf(cdns, buf, ret, j);
332 }
333
334 ret += scnprintf(buf + ret, RD_BUF - ret,
335 "\nDPn Control Registers\n");
336 for (i = 0; i < num_ports; i++)
337 ret += cdns_sprintf(cdns, buf, ret,
338 CDNS_PORTCTRL + i * CDNS_PORT_OFFSET);
339
340 ret += scnprintf(buf + ret, RD_BUF - ret,
341 "\nPDIn Config Registers\n");
342
343 /* number of PDI and ports is interchangeable */
344 for (i = 0; i < num_ports; i++)
345 ret += cdns_sprintf(cdns, buf, ret, CDNS_PDI_CONFIG(i));
346
347 seq_printf(s, "%s", buf);
348 kfree(buf);
349
350 return 0;
351}
352DEFINE_SHOW_ATTRIBUTE(cdns_reg);
353
Pierre-Louis Bossart675d4c92019-10-22 18:54:45 -0500354static int cdns_hw_reset(void *data, u64 value)
355{
356 struct sdw_cdns *cdns = data;
357 int ret;
358
359 if (value != 1)
360 return -EINVAL;
361
362 /* Userspace changed the hardware state behind the kernel's back */
363 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
364
365 ret = sdw_cdns_exit_reset(cdns);
366
367 dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret);
368
369 return ret;
370}
371
372DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n");
373
Pierre-Louis Bossart32d2a892020-09-08 21:45:21 +0800374static int cdns_parity_error_injection(void *data, u64 value)
375{
376 struct sdw_cdns *cdns = data;
377 struct sdw_bus *bus;
378 int ret;
379
380 if (value != 1)
381 return -EINVAL;
382
383 bus = &cdns->bus;
384
385 /*
386 * Resume Master device. If this results in a bus reset, the
387 * Slave devices will re-attach and be re-enumerated.
388 */
389 ret = pm_runtime_get_sync(bus->dev);
390 if (ret < 0 && ret != -EACCES) {
391 dev_err_ratelimited(cdns->dev,
392 "pm_runtime_get_sync failed in %s, ret %d\n",
393 __func__, ret);
394 pm_runtime_put_noidle(bus->dev);
395 return ret;
396 }
397
398 /*
399 * wait long enough for Slave(s) to be in steady state. This
400 * does not need to be super precise.
401 */
402 msleep(200);
403
404 /*
405 * Take the bus lock here to make sure that any bus transactions
406 * will be queued while we inject a parity error on a dummy read
407 */
408 mutex_lock(&bus->bus_lock);
409
410 /* program hardware to inject parity error */
411 cdns_updatel(cdns, CDNS_MCP_CMDCTRL,
412 CDNS_MCP_CMDCTRL_INSERT_PARITY_ERR,
413 CDNS_MCP_CMDCTRL_INSERT_PARITY_ERR);
414
415 /* commit changes */
416 cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
417 CDNS_MCP_CONFIG_UPDATE_BIT,
418 CDNS_MCP_CONFIG_UPDATE_BIT);
419
420 /* do a broadcast dummy read to avoid bus clashes */
421 ret = sdw_bread_no_pm_unlocked(&cdns->bus, 0xf, SDW_SCP_DEVID_0);
422 dev_info(cdns->dev, "parity error injection, read: %d\n", ret);
423
424 /* program hardware to disable parity error */
425 cdns_updatel(cdns, CDNS_MCP_CMDCTRL,
426 CDNS_MCP_CMDCTRL_INSERT_PARITY_ERR,
427 0);
428
429 /* commit changes */
430 cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
431 CDNS_MCP_CONFIG_UPDATE_BIT,
432 CDNS_MCP_CONFIG_UPDATE_BIT);
433
434 /* Continue bus operation with parity error injection disabled */
435 mutex_unlock(&bus->bus_lock);
436
437 /* Userspace changed the hardware state behind the kernel's back */
438 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
439
440 /*
441 * allow Master device to enter pm_runtime suspend. This may
442 * also result in Slave devices suspending.
443 */
444 pm_runtime_mark_last_busy(bus->dev);
445 pm_runtime_put_autosuspend(bus->dev);
446
447 return 0;
448}
449
450DEFINE_DEBUGFS_ATTRIBUTE(cdns_parity_error_fops, NULL,
451 cdns_parity_error_injection, "%llu\n");
452
Pierre-Louis Bossart8fba8ac2021-07-14 11:22:08 +0800453static int cdns_set_pdi_loopback_source(void *data, u64 value)
454{
455 struct sdw_cdns *cdns = data;
456 unsigned int pdi_out_num = cdns->pcm.num_bd + cdns->pcm.num_out;
457
458 if (value > pdi_out_num)
459 return -EINVAL;
460
461 /* Userspace changed the hardware state behind the kernel's back */
462 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
463
464 cdns->pdi_loopback_source = value;
465
466 return 0;
467}
468DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_source_fops, NULL, cdns_set_pdi_loopback_source, "%llu\n");
469
470static int cdns_set_pdi_loopback_target(void *data, u64 value)
471{
472 struct sdw_cdns *cdns = data;
473 unsigned int pdi_in_num = cdns->pcm.num_bd + cdns->pcm.num_in;
474
475 if (value > pdi_in_num)
476 return -EINVAL;
477
478 /* Userspace changed the hardware state behind the kernel's back */
479 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
480
481 cdns->pdi_loopback_target = value;
482
483 return 0;
484}
485DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_target_fops, NULL, cdns_set_pdi_loopback_target, "%llu\n");
486
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500487/**
488 * sdw_cdns_debugfs_init() - Cadence debugfs init
489 * @cdns: Cadence instance
490 * @root: debugfs root
491 */
492void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
493{
494 debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
Pierre-Louis Bossart675d4c92019-10-22 18:54:45 -0500495
496 debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
497 &cdns_hw_reset_fops);
Pierre-Louis Bossart32d2a892020-09-08 21:45:21 +0800498
499 debugfs_create_file("cdns-parity-error-injection", 0200, root, cdns,
500 &cdns_parity_error_fops);
Pierre-Louis Bossart8fba8ac2021-07-14 11:22:08 +0800501
502 cdns->pdi_loopback_source = -1;
503 cdns->pdi_loopback_target = -1;
504
505 debugfs_create_file("cdns-pdi-loopback-source", 0200, root, cdns,
506 &cdns_pdi_loopback_source_fops);
507
508 debugfs_create_file("cdns-pdi-loopback-target", 0200, root, cdns,
509 &cdns_pdi_loopback_target_fops);
510
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500511}
512EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
513
514#endif /* CONFIG_DEBUG_FS */
515
516/*
Sanyog Kale956baa12017-12-14 11:19:42 +0530517 * IO Calls
518 */
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500519static enum sdw_command_response
520cdns_fill_msg_resp(struct sdw_cdns *cdns,
521 struct sdw_msg *msg, int count, int offset)
Sanyog Kale956baa12017-12-14 11:19:42 +0530522{
523 int nack = 0, no_ack = 0;
524 int i;
525
526 /* check message response */
527 for (i = 0; i < count; i++) {
528 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
529 no_ack = 1;
Pierre-Louis Bossart9a0c7982021-01-15 13:37:38 +0800530 dev_vdbg(cdns->dev, "Msg Ack not received, cmd %d\n", i);
Pierre-Louis Bossartdb9d9f92021-01-15 13:37:37 +0800531 }
532 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
533 nack = 1;
Pierre-Louis Bossart9a0c7982021-01-15 13:37:38 +0800534 dev_err_ratelimited(cdns->dev, "Msg NACK received, cmd %d\n", i);
Sanyog Kale956baa12017-12-14 11:19:42 +0530535 }
536 }
537
538 if (nack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500539 dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530540 return SDW_CMD_FAIL;
Guennadi Liakhovetski6f7219f2020-05-08 02:30:46 +0200541 }
542
543 if (no_ack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500544 dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530545 return SDW_CMD_IGNORED;
546 }
547
548 /* fill response */
549 for (i = 0; i < count; i++)
Vinod Koul3cf25d62020-09-03 17:15:01 +0530550 msg->buf[i + offset] = FIELD_GET(CDNS_MCP_RESP_RDATA, cdns->response_buf[i]);
Sanyog Kale956baa12017-12-14 11:19:42 +0530551
552 return SDW_CMD_OK;
553}
554
555static enum sdw_command_response
556_cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500557 int offset, int count, bool defer)
Sanyog Kale956baa12017-12-14 11:19:42 +0530558{
559 unsigned long time;
560 u32 base, i, data;
561 u16 addr;
562
563 /* Program the watermark level for RX FIFO */
564 if (cdns->msg_count != count) {
565 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count);
566 cdns->msg_count = count;
567 }
568
569 base = CDNS_MCP_CMD_BASE;
570 addr = msg->addr;
571
572 for (i = 0; i < count; i++) {
Vinod Koul3cf25d62020-09-03 17:15:01 +0530573 data = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
574 data |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, cmd);
575 data |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, addr);
576 addr++;
Sanyog Kale956baa12017-12-14 11:19:42 +0530577
578 if (msg->flags == SDW_MSG_FLAG_WRITE)
579 data |= msg->buf[i + offset];
580
Vinod Koul3cf25d62020-09-03 17:15:01 +0530581 data |= FIELD_PREP(CDNS_MCP_CMD_SSP_TAG, msg->ssp_sync);
Sanyog Kale956baa12017-12-14 11:19:42 +0530582 cdns_writel(cdns, base, data);
583 base += CDNS_MCP_CMD_WORD_LEN;
584 }
585
586 if (defer)
587 return SDW_CMD_OK;
588
589 /* wait for timeout or response */
590 time = wait_for_completion_timeout(&cdns->tx_complete,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500591 msecs_to_jiffies(CDNS_TX_TIMEOUT));
Sanyog Kale956baa12017-12-14 11:19:42 +0530592 if (!time) {
Pierre-Louis Bossart53ee9572020-01-10 15:57:28 -0600593 dev_err(cdns->dev, "IO transfer timed out, cmd %d device %d addr %x len %d\n",
594 cmd, msg->dev_num, msg->addr, msg->len);
Sanyog Kale956baa12017-12-14 11:19:42 +0530595 msg->len = 0;
596 return SDW_CMD_TIMEOUT;
597 }
598
599 return cdns_fill_msg_resp(cdns, msg, count, offset);
600}
601
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500602static enum sdw_command_response
603cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
Sanyog Kale956baa12017-12-14 11:19:42 +0530604{
605 int nack = 0, no_ack = 0;
606 unsigned long time;
607 u32 data[2], base;
608 int i;
609
610 /* Program the watermark level for RX FIFO */
611 if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
612 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
613 cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
614 }
615
Vinod Koul3cf25d62020-09-03 17:15:01 +0530616 data[0] = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
617 data[0] |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, 0x3);
Sanyog Kale956baa12017-12-14 11:19:42 +0530618 data[1] = data[0];
619
Vinod Koul3cf25d62020-09-03 17:15:01 +0530620 data[0] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE1);
621 data[1] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE2);
Sanyog Kale956baa12017-12-14 11:19:42 +0530622
623 data[0] |= msg->addr_page1;
624 data[1] |= msg->addr_page2;
625
626 base = CDNS_MCP_CMD_BASE;
627 cdns_writel(cdns, base, data[0]);
628 base += CDNS_MCP_CMD_WORD_LEN;
629 cdns_writel(cdns, base, data[1]);
630
631 time = wait_for_completion_timeout(&cdns->tx_complete,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500632 msecs_to_jiffies(CDNS_TX_TIMEOUT));
Sanyog Kale956baa12017-12-14 11:19:42 +0530633 if (!time) {
634 dev_err(cdns->dev, "SCP Msg trf timed out\n");
635 msg->len = 0;
636 return SDW_CMD_TIMEOUT;
637 }
638
639 /* check response the writes */
640 for (i = 0; i < 2; i++) {
641 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
642 no_ack = 1;
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500643 dev_err(cdns->dev, "Program SCP Ack not received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530644 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
645 nack = 1;
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500646 dev_err(cdns->dev, "Program SCP NACK received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530647 }
648 }
649 }
650
651 /* For NACK, NO ack, don't return err if we are in Broadcast mode */
652 if (nack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500653 dev_err_ratelimited(cdns->dev,
654 "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530655 return SDW_CMD_FAIL;
Guennadi Liakhovetski6f7219f2020-05-08 02:30:46 +0200656 }
657
658 if (no_ack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500659 dev_dbg_ratelimited(cdns->dev,
660 "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530661 return SDW_CMD_IGNORED;
662 }
663
664 return SDW_CMD_OK;
665}
666
667static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
668{
669 int ret;
670
671 if (msg->page) {
672 ret = cdns_program_scp_addr(cdns, msg);
673 if (ret) {
674 msg->len = 0;
675 return ret;
676 }
677 }
678
679 switch (msg->flags) {
680 case SDW_MSG_FLAG_READ:
681 *cmd = CDNS_MCP_CMD_READ;
682 break;
683
684 case SDW_MSG_FLAG_WRITE:
685 *cmd = CDNS_MCP_CMD_WRITE;
686 break;
687
688 default:
689 dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
690 return -EINVAL;
691 }
692
693 return 0;
694}
695
Shreyas NCc91605f2018-04-26 18:38:43 +0530696enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530697cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
698{
699 struct sdw_cdns *cdns = bus_to_cdns(bus);
700 int cmd = 0, ret, i;
701
702 ret = cdns_prep_msg(cdns, msg, &cmd);
703 if (ret)
704 return SDW_CMD_FAIL_OTHER;
705
706 for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
707 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500708 CDNS_MCP_CMD_LEN, false);
Sanyog Kale956baa12017-12-14 11:19:42 +0530709 if (ret < 0)
710 goto exit;
711 }
712
713 if (!(msg->len % CDNS_MCP_CMD_LEN))
714 goto exit;
715
716 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500717 msg->len % CDNS_MCP_CMD_LEN, false);
Sanyog Kale956baa12017-12-14 11:19:42 +0530718
719exit:
720 return ret;
721}
Shreyas NCc91605f2018-04-26 18:38:43 +0530722EXPORT_SYMBOL(cdns_xfer_msg);
Sanyog Kale956baa12017-12-14 11:19:42 +0530723
Shreyas NCc91605f2018-04-26 18:38:43 +0530724enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530725cdns_xfer_msg_defer(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500726 struct sdw_msg *msg, struct sdw_defer *defer)
Sanyog Kale956baa12017-12-14 11:19:42 +0530727{
728 struct sdw_cdns *cdns = bus_to_cdns(bus);
729 int cmd = 0, ret;
730
731 /* for defer only 1 message is supported */
732 if (msg->len > 1)
733 return -ENOTSUPP;
734
735 ret = cdns_prep_msg(cdns, msg, &cmd);
736 if (ret)
737 return SDW_CMD_FAIL_OTHER;
738
739 cdns->defer = defer;
740 cdns->defer->length = msg->len;
741
742 return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
743}
Shreyas NCc91605f2018-04-26 18:38:43 +0530744EXPORT_SYMBOL(cdns_xfer_msg_defer);
Sanyog Kale956baa12017-12-14 11:19:42 +0530745
Shreyas NCc91605f2018-04-26 18:38:43 +0530746enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530747cdns_reset_page_addr(struct sdw_bus *bus, unsigned int dev_num)
748{
749 struct sdw_cdns *cdns = bus_to_cdns(bus);
750 struct sdw_msg msg;
751
752 /* Create dummy message with valid device number */
753 memset(&msg, 0, sizeof(msg));
754 msg.dev_num = dev_num;
755
756 return cdns_program_scp_addr(cdns, &msg);
757}
Shreyas NCc91605f2018-04-26 18:38:43 +0530758EXPORT_SYMBOL(cdns_reset_page_addr);
Sanyog Kale956baa12017-12-14 11:19:42 +0530759
760/*
Vinod Koul2f52a512017-12-14 11:19:41 +0530761 * IRQ handling
762 */
763
Sanyog Kale956baa12017-12-14 11:19:42 +0530764static void cdns_read_response(struct sdw_cdns *cdns)
765{
766 u32 num_resp, cmd_base;
767 int i;
768
769 num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
770 num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
771
772 cmd_base = CDNS_MCP_CMD_BASE;
773
774 for (i = 0; i < num_resp; i++) {
775 cdns->response_buf[i] = cdns_readl(cdns, cmd_base);
776 cmd_base += CDNS_MCP_CMD_WORD_LEN;
777 }
778}
779
Vinod Koul2f52a512017-12-14 11:19:41 +0530780static int cdns_update_slave_status(struct sdw_cdns *cdns,
Pierre-Louis Bossart6f206832021-01-15 13:37:35 +0800781 u64 slave_intstat)
Vinod Koul2f52a512017-12-14 11:19:41 +0530782{
783 enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
784 bool is_slave = false;
Pierre-Louis Bossarta78b32d2019-05-22 14:47:27 -0500785 u32 mask;
Vinod Koul2f52a512017-12-14 11:19:41 +0530786 int i, set_status;
787
Vinod Koul2f52a512017-12-14 11:19:41 +0530788 memset(status, 0, sizeof(status));
789
790 for (i = 0; i <= SDW_MAX_DEVICES; i++) {
Pierre-Louis Bossart6f206832021-01-15 13:37:35 +0800791 mask = (slave_intstat >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
792 CDNS_MCP_SLAVE_STATUS_BITS;
Vinod Koul2f52a512017-12-14 11:19:41 +0530793 if (!mask)
794 continue;
795
796 is_slave = true;
797 set_status = 0;
798
799 if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
800 status[i] = SDW_SLAVE_RESERVED;
801 set_status++;
802 }
803
804 if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
805 status[i] = SDW_SLAVE_ATTACHED;
806 set_status++;
807 }
808
809 if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
810 status[i] = SDW_SLAVE_ALERT;
811 set_status++;
812 }
813
814 if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
815 status[i] = SDW_SLAVE_UNATTACHED;
816 set_status++;
817 }
818
819 /* first check if Slave reported multiple status */
820 if (set_status > 1) {
Pierre-Louis Bossart7181b1d2020-01-10 15:57:30 -0600821 u32 val;
822
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500823 dev_warn_ratelimited(cdns->dev,
Pierre-Louis Bossart7181b1d2020-01-10 15:57:30 -0600824 "Slave %d reported multiple Status: %d\n",
825 i, mask);
826
827 /* check latest status extracted from PING commands */
828 val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
829 val >>= (i * 2);
830
831 switch (val & 0x3) {
832 case 0:
833 status[i] = SDW_SLAVE_UNATTACHED;
834 break;
835 case 1:
836 status[i] = SDW_SLAVE_ATTACHED;
837 break;
838 case 2:
839 status[i] = SDW_SLAVE_ALERT;
840 break;
841 case 3:
842 default:
843 status[i] = SDW_SLAVE_RESERVED;
844 break;
845 }
846
847 dev_warn_ratelimited(cdns->dev,
848 "Slave %d status updated to %d\n",
849 i, status[i]);
850
Vinod Koul2f52a512017-12-14 11:19:41 +0530851 }
852 }
853
854 if (is_slave)
855 return sdw_handle_slave_status(&cdns->bus, status);
856
857 return 0;
858}
859
860/**
861 * sdw_cdns_irq() - Cadence interrupt handler
862 * @irq: irq number
863 * @dev_id: irq context
864 */
865irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
866{
867 struct sdw_cdns *cdns = dev_id;
868 u32 int_status;
Vinod Koul2f52a512017-12-14 11:19:41 +0530869
870 /* Check if the link is up */
871 if (!cdns->link_up)
872 return IRQ_NONE;
873
874 int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
875
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -0600876 /* check for reserved values read as zero */
877 if (int_status & CDNS_MCP_INT_RESERVED)
878 return IRQ_NONE;
879
Vinod Koul2f52a512017-12-14 11:19:41 +0530880 if (!(int_status & CDNS_MCP_INT_IRQ))
881 return IRQ_NONE;
882
Sanyog Kale956baa12017-12-14 11:19:42 +0530883 if (int_status & CDNS_MCP_INT_RX_WL) {
884 cdns_read_response(cdns);
885
886 if (cdns->defer) {
887 cdns_fill_msg_resp(cdns, cdns->defer->msg,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500888 cdns->defer->length, 0);
Sanyog Kale956baa12017-12-14 11:19:42 +0530889 complete(&cdns->defer->complete);
890 cdns->defer = NULL;
Pierre-Louis Bossartf6e20962019-05-01 10:57:42 -0500891 } else {
Sanyog Kale956baa12017-12-14 11:19:42 +0530892 complete(&cdns->tx_complete);
Pierre-Louis Bossartf6e20962019-05-01 10:57:42 -0500893 }
Sanyog Kale956baa12017-12-14 11:19:42 +0530894 }
895
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -0500896 if (int_status & CDNS_MCP_INT_PARITY) {
897 /* Parity error detected by Master */
898 dev_err_ratelimited(cdns->dev, "Parity error\n");
899 }
900
Vinod Koul2f52a512017-12-14 11:19:41 +0530901 if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
Vinod Koul2f52a512017-12-14 11:19:41 +0530902 /* Slave is driving bit slot during control word */
903 dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
Vinod Koul2f52a512017-12-14 11:19:41 +0530904 }
905
906 if (int_status & CDNS_MCP_INT_DATA_CLASH) {
907 /*
908 * Multiple slaves trying to drive bit slot, or issue with
909 * ownership of data bits or Slave gone bonkers
910 */
911 dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
Vinod Koul2f52a512017-12-14 11:19:41 +0530912 }
913
Pierre-Louis Bossart9e4e6012020-09-21 03:32:07 +0800914 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL &&
915 int_status & CDNS_MCP_INT_DPINT) {
916 u32 port_intstat;
917
918 /* just log which ports report an error */
919 port_intstat = cdns_readl(cdns, CDNS_MCP_PORT_INTSTAT);
920 dev_err_ratelimited(cdns->dev, "DP interrupt: PortIntStat %8x\n",
921 port_intstat);
922
923 /* clear status w/ write1 */
924 cdns_writel(cdns, CDNS_MCP_PORT_INTSTAT, port_intstat);
925 }
926
Vinod Koul2f52a512017-12-14 11:19:41 +0530927 if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
928 /* Mask the Slave interrupt and wake thread */
929 cdns_updatel(cdns, CDNS_MCP_INTMASK,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500930 CDNS_MCP_INT_SLAVE_MASK, 0);
Vinod Koul2f52a512017-12-14 11:19:41 +0530931
932 int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
Pierre-Louis Bossartd2068da2020-08-18 06:23:40 +0800933
934 /*
935 * Deal with possible race condition between interrupt
936 * handling and disabling interrupts on suspend.
937 *
938 * If the master is in the process of disabling
939 * interrupts, don't schedule a workqueue
940 */
941 if (cdns->interrupt_enabled)
942 schedule_work(&cdns->work);
Vinod Koul2f52a512017-12-14 11:19:41 +0530943 }
944
945 cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
Peter Ujfalusi00d3c2b2021-07-14 09:55:55 +0800946 return IRQ_HANDLED;
Vinod Koul2f52a512017-12-14 11:19:41 +0530947}
948EXPORT_SYMBOL(sdw_cdns_irq);
949
950/**
Pierre-Louis Bossartb76f3fb2021-03-01 11:47:14 -0600951 * cdns_update_slave_status_work - update slave status in a work since we will need to handle
Bard Liao4a98a6b2020-07-16 23:09:45 +0800952 * other interrupts eg. CDNS_MCP_INT_RX_WL during the update slave
953 * process.
954 * @work: cdns worker thread
Vinod Koul2f52a512017-12-14 11:19:41 +0530955 */
Bard Liao4a98a6b2020-07-16 23:09:45 +0800956static void cdns_update_slave_status_work(struct work_struct *work)
Vinod Koul2f52a512017-12-14 11:19:41 +0530957{
Bard Liao4a98a6b2020-07-16 23:09:45 +0800958 struct sdw_cdns *cdns =
959 container_of(work, struct sdw_cdns, work);
Vinod Koul2f52a512017-12-14 11:19:41 +0530960 u32 slave0, slave1;
Pierre-Louis Bossart6f206832021-01-15 13:37:35 +0800961 u64 slave_intstat;
Vinod Koul2f52a512017-12-14 11:19:41 +0530962
963 slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
964 slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
965
Pierre-Louis Bossart6f206832021-01-15 13:37:35 +0800966 /* combine the two status */
967 slave_intstat = ((u64)slave1 << 32) | slave0;
968
969 dev_dbg_ratelimited(cdns->dev, "Slave status change: 0x%llx\n", slave_intstat);
970
971 cdns_update_slave_status(cdns, slave_intstat);
Vinod Koul2f52a512017-12-14 11:19:41 +0530972 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
973 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
974
975 /* clear and unmask Slave interrupt now */
976 cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
977 cdns_updatel(cdns, CDNS_MCP_INTMASK,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500978 CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
Vinod Koul2f52a512017-12-14 11:19:41 +0530979
Vinod Koul2f52a512017-12-14 11:19:41 +0530980}
Vinod Koul2f52a512017-12-14 11:19:41 +0530981
Pierre-Louis Bossartff560942021-07-14 13:13:49 +0800982/* paranoia check to make sure self-cleared bits are indeed cleared */
983void sdw_cdns_check_self_clearing_bits(struct sdw_cdns *cdns, const char *string,
984 bool initial_delay, int reset_iterations)
985{
986 u32 mcp_control;
987 u32 mcp_config_update;
988 int i;
989
990 if (initial_delay)
991 usleep_range(1000, 1500);
992
993 mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
994
995 /* the following bits should be cleared immediately */
996 if (mcp_control & CDNS_MCP_CONTROL_CMD_RST)
997 dev_err(cdns->dev, "%s failed: MCP_CONTROL_CMD_RST is not cleared\n", string);
998 if (mcp_control & CDNS_MCP_CONTROL_SOFT_RST)
999 dev_err(cdns->dev, "%s failed: MCP_CONTROL_SOFT_RST is not cleared\n", string);
1000 if (mcp_control & CDNS_MCP_CONTROL_SW_RST)
1001 dev_err(cdns->dev, "%s failed: MCP_CONTROL_SW_RST is not cleared\n", string);
1002 if (mcp_control & CDNS_MCP_CONTROL_CLK_STOP_CLR)
1003 dev_err(cdns->dev, "%s failed: MCP_CONTROL_CLK_STOP_CLR is not cleared\n", string);
1004 mcp_config_update = cdns_readl(cdns, CDNS_MCP_CONFIG_UPDATE);
1005 if (mcp_config_update & CDNS_MCP_CONFIG_UPDATE_BIT)
1006 dev_err(cdns->dev, "%s failed: MCP_CONFIG_UPDATE_BIT is not cleared\n", string);
1007
1008 i = 0;
1009 while (mcp_control & CDNS_MCP_CONTROL_HW_RST) {
1010 if (i == reset_iterations) {
1011 dev_err(cdns->dev, "%s failed: MCP_CONTROL_HW_RST is not cleared\n", string);
1012 break;
1013 }
1014
1015 dev_dbg(cdns->dev, "%s: MCP_CONTROL_HW_RST is not cleared at iteration %d\n", string, i);
1016 i++;
1017
1018 usleep_range(1000, 1500);
1019 mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
1020 }
1021
1022}
1023EXPORT_SYMBOL(sdw_cdns_check_self_clearing_bits);
1024
Vinod Koul2f52a512017-12-14 11:19:41 +05301025/*
1026 * init routines
1027 */
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001028
1029/**
1030 * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
1031 * @cdns: Cadence instance
1032 */
1033int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
1034{
Pierre-Louis Bossart2564a2d2021-08-18 11:01:30 +08001035 /* keep reset delay unchanged to 4096 cycles */
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001036
1037 /* use hardware generated reset */
1038 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1039 CDNS_MCP_CONTROL_HW_RST,
1040 CDNS_MCP_CONTROL_HW_RST);
1041
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001042 /* commit changes */
Pierre-Louis Bossart2c800e32020-03-17 11:33:28 -05001043 cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
1044 CDNS_MCP_CONFIG_UPDATE_BIT,
1045 CDNS_MCP_CONFIG_UPDATE_BIT);
1046
1047 /* don't wait here */
1048 return 0;
1049
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001050}
1051EXPORT_SYMBOL(sdw_cdns_exit_reset);
1052
1053/**
Pierre-Louis Bossartb76f3fb2021-03-01 11:47:14 -06001054 * cdns_enable_slave_interrupts() - Enable SDW slave interrupts
Pierre-Louis Bossartaf4cc912020-03-17 11:33:19 -05001055 * @cdns: Cadence instance
1056 * @state: boolean for true/false
1057 */
1058static void cdns_enable_slave_interrupts(struct sdw_cdns *cdns, bool state)
1059{
1060 u32 mask;
1061
1062 mask = cdns_readl(cdns, CDNS_MCP_INTMASK);
1063 if (state)
1064 mask |= CDNS_MCP_INT_SLAVE_MASK;
1065 else
1066 mask &= ~CDNS_MCP_INT_SLAVE_MASK;
1067
1068 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1069}
1070
1071/**
Rander Wangae478d62020-01-10 15:57:29 -06001072 * sdw_cdns_enable_interrupt() - Enable SDW interrupts
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001073 * @cdns: Cadence instance
Pierre-Louis Bossart550f9052020-01-14 17:31:24 -06001074 * @state: True if we are trying to enable interrupt.
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001075 */
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001076int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
Sanyog Kale956baa12017-12-14 11:19:42 +05301077{
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001078 u32 slave_intmask0 = 0;
1079 u32 slave_intmask1 = 0;
1080 u32 mask = 0;
Sanyog Kale956baa12017-12-14 11:19:42 +05301081
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001082 if (!state)
1083 goto update_masks;
1084
1085 slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
1086 slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
Sanyog Kale956baa12017-12-14 11:19:42 +05301087
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -05001088 /* enable detection of all slave state changes */
1089 mask = CDNS_MCP_INT_SLAVE_MASK;
1090
1091 /* enable detection of bus issues */
1092 mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
1093 CDNS_MCP_INT_PARITY;
1094
Pierre-Louis Bossart9e4e6012020-09-21 03:32:07 +08001095 /* port interrupt limited to test modes for now */
1096 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1097 mask |= CDNS_MCP_INT_DPINT;
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -05001098
1099 /* enable detection of RX fifo level */
1100 mask |= CDNS_MCP_INT_RX_WL;
1101
1102 /*
1103 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
1104 * settings are irrelevant
1105 */
1106 mask |= CDNS_MCP_INT_IRQ;
Sanyog Kale956baa12017-12-14 11:19:42 +05301107
Pierre-Louis Bossart04592dc2019-08-05 19:55:21 -05001108 if (interrupt_mask) /* parameter override */
1109 mask = interrupt_mask;
Sanyog Kale956baa12017-12-14 11:19:42 +05301110
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001111update_masks:
Rander Wang5ebb0942020-01-10 15:57:27 -06001112 /* clear slave interrupt status before enabling interrupt */
1113 if (state) {
1114 u32 slave_state;
1115
1116 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
1117 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state);
1118 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
1119 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state);
1120 }
Pierre-Louis Bossartd2068da2020-08-18 06:23:40 +08001121 cdns->interrupt_enabled = state;
1122
1123 /*
1124 * Complete any on-going status updates before updating masks,
1125 * and cancel queued status updates.
1126 *
1127 * There could be a race with a new interrupt thrown before
1128 * the 3 mask updates below are complete, so in the interrupt
1129 * we use the 'interrupt_enabled' status to prevent new work
1130 * from being queued.
1131 */
1132 if (!state)
1133 cancel_work_sync(&cdns->work);
Rander Wang5ebb0942020-01-10 15:57:27 -06001134
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001135 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
1136 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
Sanyog Kale956baa12017-12-14 11:19:42 +05301137 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1138
Rander Wangae478d62020-01-10 15:57:29 -06001139 return 0;
Sanyog Kale956baa12017-12-14 11:19:42 +05301140}
1141EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
Vinod Koul2f52a512017-12-14 11:19:41 +05301142
Vinod Koul07abeff2018-04-26 18:38:48 +05301143static int cdns_allocate_pdi(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001144 struct sdw_cdns_pdi **stream,
1145 u32 num, u32 pdi_offset)
Vinod Koul07abeff2018-04-26 18:38:48 +05301146{
1147 struct sdw_cdns_pdi *pdi;
1148 int i;
1149
1150 if (!num)
1151 return 0;
1152
1153 pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
1154 if (!pdi)
1155 return -ENOMEM;
1156
1157 for (i = 0; i < num; i++) {
1158 pdi[i].num = i + pdi_offset;
Vinod Koul07abeff2018-04-26 18:38:48 +05301159 }
1160
1161 *stream = pdi;
1162 return 0;
1163}
1164
1165/**
1166 * sdw_cdns_pdi_init() - PDI initialization routine
1167 *
1168 * @cdns: Cadence instance
1169 * @config: Stream configurations
1170 */
1171int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001172 struct sdw_cdns_stream_config config)
Vinod Koul07abeff2018-04-26 18:38:48 +05301173{
1174 struct sdw_cdns_streams *stream;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001175 int offset;
1176 int ret;
Vinod Koul07abeff2018-04-26 18:38:48 +05301177
1178 cdns->pcm.num_bd = config.pcm_bd;
1179 cdns->pcm.num_in = config.pcm_in;
1180 cdns->pcm.num_out = config.pcm_out;
Vinod Koul07abeff2018-04-26 18:38:48 +05301181
1182 /* Allocate PDIs for PCMs */
1183 stream = &cdns->pcm;
1184
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -05001185 /* we allocate PDI0 and PDI1 which are used for Bulk */
1186 offset = 0;
Vinod Koul07abeff2018-04-26 18:38:48 +05301187
1188 ret = cdns_allocate_pdi(cdns, &stream->bd,
1189 stream->num_bd, offset);
1190 if (ret)
1191 return ret;
1192
1193 offset += stream->num_bd;
1194
1195 ret = cdns_allocate_pdi(cdns, &stream->in,
1196 stream->num_in, offset);
1197 if (ret)
1198 return ret;
1199
1200 offset += stream->num_in;
1201
1202 ret = cdns_allocate_pdi(cdns, &stream->out,
1203 stream->num_out, offset);
1204 if (ret)
1205 return ret;
1206
1207 /* Update total number of PCM PDIs */
1208 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1209 cdns->num_ports = stream->num_pdi;
1210
Vinod Koul07abeff2018-04-26 18:38:48 +05301211 return 0;
1212}
1213EXPORT_SYMBOL(sdw_cdns_pdi_init);
1214
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -05001215static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
1216{
1217 u32 val;
1218 int c;
1219 int r;
1220
1221 r = sdw_find_row_index(n_rows);
Vinod Koul3cf25d62020-09-03 17:15:01 +05301222 c = sdw_find_col_index(n_cols);
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -05001223
Vinod Koul3cf25d62020-09-03 17:15:01 +05301224 val = FIELD_PREP(CDNS_MCP_FRAME_SHAPE_ROW_MASK, r);
1225 val |= FIELD_PREP(CDNS_MCP_FRAME_SHAPE_COL_MASK, c);
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -05001226
1227 return val;
1228}
1229
Pierre-Louis Bossart0cdcded2020-03-17 11:33:21 -05001230static void cdns_init_clock_ctrl(struct sdw_cdns *cdns)
Vinod Koul2f52a512017-12-14 11:19:41 +05301231{
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001232 struct sdw_bus *bus = &cdns->bus;
1233 struct sdw_master_prop *prop = &bus->prop;
Vinod Koul2f52a512017-12-14 11:19:41 +05301234 u32 val;
Pierre-Louis Bossart1dd6a172020-03-17 11:33:22 -05001235 u32 ssp_interval;
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001236 int divider;
Vinod Koul2f52a512017-12-14 11:19:41 +05301237
1238 /* Set clock divider */
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001239 divider = (prop->mclk_freq / prop->max_clk_freq) - 1;
Vinod Koul2f52a512017-12-14 11:19:41 +05301240
Rander Wanga50954e2019-08-05 19:55:18 -05001241 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1242 CDNS_MCP_CLK_MCLKD_MASK, divider);
1243 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1244 CDNS_MCP_CLK_MCLKD_MASK, divider);
Vinod Koul2f52a512017-12-14 11:19:41 +05301245
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -05001246 /*
1247 * Frame shape changes after initialization have to be done
1248 * with the bank switch mechanism
1249 */
1250 val = cdns_set_initial_frame_shape(prop->default_row,
1251 prop->default_col);
1252 cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
Vinod Koul2f52a512017-12-14 11:19:41 +05301253
1254 /* Set SSP interval to default value */
Pierre-Louis Bossart1dd6a172020-03-17 11:33:22 -05001255 ssp_interval = prop->default_frame_rate / SDW_CADENCE_GSYNC_HZ;
1256 cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, ssp_interval);
1257 cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, ssp_interval);
Pierre-Louis Bossart0cdcded2020-03-17 11:33:21 -05001258}
1259
1260/**
1261 * sdw_cdns_init() - Cadence initialization
1262 * @cdns: Cadence instance
1263 */
1264int sdw_cdns_init(struct sdw_cdns *cdns)
1265{
1266 u32 val;
Pierre-Louis Bossart0cdcded2020-03-17 11:33:21 -05001267
1268 cdns_init_clock_ctrl(cdns);
Vinod Koul2f52a512017-12-14 11:19:41 +05301269
Pierre-Louis Bossartff560942021-07-14 13:13:49 +08001270 sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1271
Rander Wang0d667d02020-03-17 11:33:18 -05001272 /* reset msg_count to default value of FIFOLEVEL */
1273 cdns->msg_count = cdns_readl(cdns, CDNS_MCP_FIFOLEVEL);
1274
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001275 /* flush command FIFOs */
1276 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1277 CDNS_MCP_CONTROL_CMD_RST);
1278
Vinod Koul2f52a512017-12-14 11:19:41 +05301279 /* Set cmd accept mode */
1280 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001281 CDNS_MCP_CONTROL_CMD_ACCEPT);
Vinod Koul2f52a512017-12-14 11:19:41 +05301282
1283 /* Configure mcp config */
1284 val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1285
Pierre-Louis Bossart5c8f0f682020-03-17 11:33:24 -05001286 /* enable bus operations with clock and data */
1287 val &= ~CDNS_MCP_CONFIG_OP;
1288 val |= CDNS_MCP_CONFIG_OP_NORMAL;
1289
Pierre-Louis Bossartb62e76c2020-03-17 11:33:23 -05001290 /* Set cmd mode for Tx and Rx cmds */
1291 val &= ~CDNS_MCP_CONFIG_CMD;
Vinod Koul2f52a512017-12-14 11:19:41 +05301292
1293 /* Disable sniffer mode */
1294 val &= ~CDNS_MCP_CONFIG_SNIFFER;
1295
Pierre-Louis Bossartb62e76c2020-03-17 11:33:23 -05001296 /* Disable auto bus release */
1297 val &= ~CDNS_MCP_CONFIG_BUS_REL;
1298
Pierre-Louis Bossart2c800e32020-03-17 11:33:28 -05001299 if (cdns->bus.multi_link)
1300 /* Set Multi-master mode to take gsync into account */
1301 val |= CDNS_MCP_CONFIG_MMASTER;
Pierre-Louis Bossartb62e76c2020-03-17 11:33:23 -05001302
Pierre-Louis Bossart91080112020-03-17 11:33:25 -05001303 /* leave frame delay to hardware default of 0x1F */
Pierre-Louis Bossartb62e76c2020-03-17 11:33:23 -05001304
Pierre-Louis Bossartad473db2020-03-17 11:33:26 -05001305 /* leave command retry to hardware default of 0 */
Vinod Koul2f52a512017-12-14 11:19:41 +05301306
Vinod Koul2f52a512017-12-14 11:19:41 +05301307 cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1308
Pierre-Louis Bossartb17350e2020-03-17 11:33:27 -05001309 /* changes will be committed later */
1310 return 0;
Vinod Koul2f52a512017-12-14 11:19:41 +05301311}
1312EXPORT_SYMBOL(sdw_cdns_init);
1313
Vinod Koul07abeff2018-04-26 18:38:48 +05301314int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1315{
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001316 struct sdw_master_prop *prop = &bus->prop;
Vinod Koul07abeff2018-04-26 18:38:48 +05301317 struct sdw_cdns *cdns = bus_to_cdns(bus);
Rander Wanga50954e2019-08-05 19:55:18 -05001318 int mcp_clkctrl_off;
Vinod Koul07abeff2018-04-26 18:38:48 +05301319 int divider;
1320
1321 if (!params->curr_dr_freq) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001322 dev_err(cdns->dev, "NULL curr_dr_freq\n");
Vinod Koul07abeff2018-04-26 18:38:48 +05301323 return -EINVAL;
1324 }
1325
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001326 divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1327 params->curr_dr_freq;
1328 divider--; /* divider is 1/(N+1) */
Vinod Koul07abeff2018-04-26 18:38:48 +05301329
1330 if (params->next_bank)
1331 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1332 else
1333 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1334
Rander Wanga50954e2019-08-05 19:55:18 -05001335 cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
Vinod Koul07abeff2018-04-26 18:38:48 +05301336
1337 return 0;
1338}
1339EXPORT_SYMBOL(cdns_bus_conf);
1340
1341static int cdns_port_params(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001342 struct sdw_port_params *p_params, unsigned int bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301343{
1344 struct sdw_cdns *cdns = bus_to_cdns(bus);
Pierre-Louis Bossartdd81e7c2021-07-14 11:22:09 +08001345 int dpn_config_off_source;
1346 int dpn_config_off_target;
1347 int target_num = p_params->num;
1348 int source_num = p_params->num;
1349 bool override = false;
1350 int dpn_config;
Vinod Koul07abeff2018-04-26 18:38:48 +05301351
Pierre-Louis Bossartdd81e7c2021-07-14 11:22:09 +08001352 if (target_num == cdns->pdi_loopback_target &&
1353 cdns->pdi_loopback_source != -1) {
1354 source_num = cdns->pdi_loopback_source;
1355 override = true;
1356 }
Vinod Koul07abeff2018-04-26 18:38:48 +05301357
Pierre-Louis Bossartdd81e7c2021-07-14 11:22:09 +08001358 if (bank) {
1359 dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1360 dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1361 } else {
1362 dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1363 dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1364 }
Vinod Koul07abeff2018-04-26 18:38:48 +05301365
Pierre-Louis Bossartdd81e7c2021-07-14 11:22:09 +08001366 dpn_config = cdns_readl(cdns, dpn_config_off_source);
Vinod Koul07abeff2018-04-26 18:38:48 +05301367
Pierre-Louis Bossartdd81e7c2021-07-14 11:22:09 +08001368 /* use port params if there is no loopback, otherwise use source as is */
1369 if (!override) {
1370 u32p_replace_bits(&dpn_config, p_params->bps - 1, CDNS_DPN_CONFIG_WL);
1371 u32p_replace_bits(&dpn_config, p_params->flow_mode, CDNS_DPN_CONFIG_PORT_FLOW);
1372 u32p_replace_bits(&dpn_config, p_params->data_mode, CDNS_DPN_CONFIG_PORT_DAT);
1373 }
1374
1375 cdns_writel(cdns, dpn_config_off_target, dpn_config);
Vinod Koul07abeff2018-04-26 18:38:48 +05301376
1377 return 0;
1378}
1379
1380static int cdns_transport_params(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001381 struct sdw_transport_params *t_params,
1382 enum sdw_reg_bank bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301383{
1384 struct sdw_cdns *cdns = bus_to_cdns(bus);
Pierre-Louis Bossartdd81e7c2021-07-14 11:22:09 +08001385 int dpn_config;
1386 int dpn_config_off_source;
1387 int dpn_config_off_target;
1388 int dpn_hctrl;
1389 int dpn_hctrl_off_source;
1390 int dpn_hctrl_off_target;
1391 int dpn_offsetctrl;
1392 int dpn_offsetctrl_off_source;
1393 int dpn_offsetctrl_off_target;
1394 int dpn_samplectrl;
1395 int dpn_samplectrl_off_source;
1396 int dpn_samplectrl_off_target;
1397 int source_num = t_params->port_num;
1398 int target_num = t_params->port_num;
1399 bool override = false;
1400
1401 if (target_num == cdns->pdi_loopback_target &&
1402 cdns->pdi_loopback_source != -1) {
1403 source_num = cdns->pdi_loopback_source;
1404 override = true;
1405 }
Vinod Koul07abeff2018-04-26 18:38:48 +05301406
1407 /*
1408 * Note: Only full data port is supported on the Master side for
1409 * both PCM and PDM ports.
1410 */
1411
1412 if (bank) {
Pierre-Louis Bossartdd81e7c2021-07-14 11:22:09 +08001413 dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1414 dpn_hctrl_off_source = CDNS_DPN_B1_HCTRL(source_num);
1415 dpn_offsetctrl_off_source = CDNS_DPN_B1_OFFSET_CTRL(source_num);
1416 dpn_samplectrl_off_source = CDNS_DPN_B1_SAMPLE_CTRL(source_num);
1417
1418 dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1419 dpn_hctrl_off_target = CDNS_DPN_B1_HCTRL(target_num);
1420 dpn_offsetctrl_off_target = CDNS_DPN_B1_OFFSET_CTRL(target_num);
1421 dpn_samplectrl_off_target = CDNS_DPN_B1_SAMPLE_CTRL(target_num);
1422
Vinod Koul07abeff2018-04-26 18:38:48 +05301423 } else {
Pierre-Louis Bossartdd81e7c2021-07-14 11:22:09 +08001424 dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1425 dpn_hctrl_off_source = CDNS_DPN_B0_HCTRL(source_num);
1426 dpn_offsetctrl_off_source = CDNS_DPN_B0_OFFSET_CTRL(source_num);
1427 dpn_samplectrl_off_source = CDNS_DPN_B0_SAMPLE_CTRL(source_num);
1428
1429 dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1430 dpn_hctrl_off_target = CDNS_DPN_B0_HCTRL(target_num);
1431 dpn_offsetctrl_off_target = CDNS_DPN_B0_OFFSET_CTRL(target_num);
1432 dpn_samplectrl_off_target = CDNS_DPN_B0_SAMPLE_CTRL(target_num);
Vinod Koul07abeff2018-04-26 18:38:48 +05301433 }
1434
Pierre-Louis Bossartdd81e7c2021-07-14 11:22:09 +08001435 dpn_config = cdns_readl(cdns, dpn_config_off_source);
1436 if (!override) {
1437 u32p_replace_bits(&dpn_config, t_params->blk_grp_ctrl, CDNS_DPN_CONFIG_BGC);
1438 u32p_replace_bits(&dpn_config, t_params->blk_pkg_mode, CDNS_DPN_CONFIG_BPM);
1439 }
1440 cdns_writel(cdns, dpn_config_off_target, dpn_config);
Vinod Koul07abeff2018-04-26 18:38:48 +05301441
Pierre-Louis Bossartdd81e7c2021-07-14 11:22:09 +08001442 if (!override) {
1443 dpn_offsetctrl = 0;
1444 u32p_replace_bits(&dpn_offsetctrl, t_params->offset1, CDNS_DPN_OFFSET_CTRL_1);
1445 u32p_replace_bits(&dpn_offsetctrl, t_params->offset2, CDNS_DPN_OFFSET_CTRL_2);
1446 } else {
1447 dpn_offsetctrl = cdns_readl(cdns, dpn_offsetctrl_off_source);
1448 }
1449 cdns_writel(cdns, dpn_offsetctrl_off_target, dpn_offsetctrl);
Vinod Koul07abeff2018-04-26 18:38:48 +05301450
Pierre-Louis Bossartdd81e7c2021-07-14 11:22:09 +08001451 if (!override) {
1452 dpn_hctrl = 0;
1453 u32p_replace_bits(&dpn_hctrl, t_params->hstart, CDNS_DPN_HCTRL_HSTART);
1454 u32p_replace_bits(&dpn_hctrl, t_params->hstop, CDNS_DPN_HCTRL_HSTOP);
1455 u32p_replace_bits(&dpn_hctrl, t_params->lane_ctrl, CDNS_DPN_HCTRL_LCTRL);
1456 } else {
1457 dpn_hctrl = cdns_readl(cdns, dpn_hctrl_off_source);
1458 }
1459 cdns_writel(cdns, dpn_hctrl_off_target, dpn_hctrl);
Vinod Koul07abeff2018-04-26 18:38:48 +05301460
Pierre-Louis Bossartdd81e7c2021-07-14 11:22:09 +08001461 if (!override)
1462 dpn_samplectrl = t_params->sample_interval - 1;
1463 else
1464 dpn_samplectrl = cdns_readl(cdns, dpn_samplectrl_off_source);
1465 cdns_writel(cdns, dpn_samplectrl_off_target, dpn_samplectrl);
Vinod Koul07abeff2018-04-26 18:38:48 +05301466
1467 return 0;
1468}
1469
1470static int cdns_port_enable(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001471 struct sdw_enable_ch *enable_ch, unsigned int bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301472{
1473 struct sdw_cdns *cdns = bus_to_cdns(bus);
1474 int dpn_chnen_off, ch_mask;
1475
1476 if (bank)
1477 dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1478 else
1479 dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1480
1481 ch_mask = enable_ch->ch_mask * enable_ch->enable;
1482 cdns_writel(cdns, dpn_chnen_off, ch_mask);
1483
1484 return 0;
1485}
1486
1487static const struct sdw_master_port_ops cdns_port_ops = {
1488 .dpn_set_port_params = cdns_port_params,
1489 .dpn_set_port_transport_params = cdns_transport_params,
1490 .dpn_port_enable_ch = cdns_port_enable,
1491};
1492
Sanyog Kale956baa12017-12-14 11:19:42 +05301493/**
Rander Wang5a885c52020-03-17 11:33:15 -05001494 * sdw_cdns_is_clock_stop: Check clock status
1495 *
1496 * @cdns: Cadence instance
1497 */
1498bool sdw_cdns_is_clock_stop(struct sdw_cdns *cdns)
1499{
1500 return !!(cdns_readl(cdns, CDNS_MCP_STAT) & CDNS_MCP_STAT_CLK_STOP);
1501}
1502EXPORT_SYMBOL(sdw_cdns_is_clock_stop);
1503
1504/**
Rander Wang10325042020-03-17 11:33:17 -05001505 * sdw_cdns_clock_stop: Cadence clock stop configuration routine
1506 *
1507 * @cdns: Cadence instance
1508 * @block_wake: prevent wakes if required by the platform
1509 */
1510int sdw_cdns_clock_stop(struct sdw_cdns *cdns, bool block_wake)
1511{
1512 bool slave_present = false;
1513 struct sdw_slave *slave;
1514 int ret;
1515
Pierre-Louis Bossartff560942021-07-14 13:13:49 +08001516 sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1517
Rander Wang10325042020-03-17 11:33:17 -05001518 /* Check suspend status */
1519 if (sdw_cdns_is_clock_stop(cdns)) {
1520 dev_dbg(cdns->dev, "Clock is already stopped\n");
1521 return 0;
1522 }
1523
1524 /*
Pierre-Louis Bossartaf4cc912020-03-17 11:33:19 -05001525 * Before entering clock stop we mask the Slave
1526 * interrupts. This helps avoid having to deal with e.g. a
1527 * Slave becoming UNATTACHED while the clock is being stopped
1528 */
1529 cdns_enable_slave_interrupts(cdns, false);
1530
1531 /*
Rander Wang10325042020-03-17 11:33:17 -05001532 * For specific platforms, it is required to be able to put
1533 * master into a state in which it ignores wake-up trials
1534 * in clock stop state
1535 */
1536 if (block_wake)
1537 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1538 CDNS_MCP_CONTROL_BLOCK_WAKEUP,
1539 CDNS_MCP_CONTROL_BLOCK_WAKEUP);
1540
1541 list_for_each_entry(slave, &cdns->bus.slaves, node) {
1542 if (slave->status == SDW_SLAVE_ATTACHED ||
1543 slave->status == SDW_SLAVE_ALERT) {
1544 slave_present = true;
1545 break;
1546 }
1547 }
1548
Rander Wang10325042020-03-17 11:33:17 -05001549 /* commit changes */
1550 ret = cdns_config_update(cdns);
1551 if (ret < 0) {
1552 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1553 return ret;
1554 }
1555
1556 /* Prepare slaves for clock stop */
Pierre-Louis Bossart58ef9352021-03-23 09:37:07 +08001557 if (slave_present) {
1558 ret = sdw_bus_prep_clk_stop(&cdns->bus);
1559 if (ret < 0 && ret != -ENODATA) {
1560 dev_err(cdns->dev, "prepare clock stop failed %d\n", ret);
1561 return ret;
1562 }
Rander Wang10325042020-03-17 11:33:17 -05001563 }
1564
1565 /*
1566 * Enter clock stop mode and only report errors if there are
1567 * Slave devices present (ALERT or ATTACHED)
1568 */
1569 ret = sdw_bus_clk_stop(&cdns->bus);
1570 if (ret < 0 && slave_present && ret != -ENODATA) {
Pierre-Louis Bossart7dbdcd62021-03-23 08:58:53 +08001571 dev_err(cdns->dev, "bus clock stop failed %d\n", ret);
Rander Wang10325042020-03-17 11:33:17 -05001572 return ret;
1573 }
1574
1575 ret = cdns_set_wait(cdns, CDNS_MCP_STAT,
1576 CDNS_MCP_STAT_CLK_STOP,
1577 CDNS_MCP_STAT_CLK_STOP);
1578 if (ret < 0)
1579 dev_err(cdns->dev, "Clock stop failed %d\n", ret);
1580
1581 return ret;
1582}
1583EXPORT_SYMBOL(sdw_cdns_clock_stop);
1584
1585/**
1586 * sdw_cdns_clock_restart: Cadence PM clock restart configuration routine
1587 *
1588 * @cdns: Cadence instance
1589 * @bus_reset: context may be lost while in low power modes and the bus
1590 * may require a Severe Reset and re-enumeration after a wake.
1591 */
1592int sdw_cdns_clock_restart(struct sdw_cdns *cdns, bool bus_reset)
1593{
1594 int ret;
1595
Pierre-Louis Bossartaf4cc912020-03-17 11:33:19 -05001596 /* unmask Slave interrupts that were masked when stopping the clock */
1597 cdns_enable_slave_interrupts(cdns, true);
1598
Rander Wang10325042020-03-17 11:33:17 -05001599 ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
1600 CDNS_MCP_CONTROL_CLK_STOP_CLR);
1601 if (ret < 0) {
1602 dev_err(cdns->dev, "Couldn't exit from clock stop\n");
1603 return ret;
1604 }
1605
1606 ret = cdns_set_wait(cdns, CDNS_MCP_STAT, CDNS_MCP_STAT_CLK_STOP, 0);
1607 if (ret < 0) {
1608 dev_err(cdns->dev, "clock stop exit failed %d\n", ret);
1609 return ret;
1610 }
1611
1612 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1613 CDNS_MCP_CONTROL_BLOCK_WAKEUP, 0);
1614
Pierre-Louis Bossart36eee232021-05-11 10:52:47 +08001615 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT,
1616 CDNS_MCP_CONTROL_CMD_ACCEPT);
Rander Wang10325042020-03-17 11:33:17 -05001617
1618 if (!bus_reset) {
1619
1620 /* enable bus operations with clock and data */
1621 cdns_updatel(cdns, CDNS_MCP_CONFIG,
1622 CDNS_MCP_CONFIG_OP,
1623 CDNS_MCP_CONFIG_OP_NORMAL);
1624
1625 ret = cdns_config_update(cdns);
1626 if (ret < 0) {
1627 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1628 return ret;
1629 }
1630
1631 ret = sdw_bus_exit_clk_stop(&cdns->bus);
1632 if (ret < 0)
1633 dev_err(cdns->dev, "bus failed to exit clock stop %d\n", ret);
1634 }
1635
1636 return ret;
1637}
1638EXPORT_SYMBOL(sdw_cdns_clock_restart);
1639
1640/**
Sanyog Kale956baa12017-12-14 11:19:42 +05301641 * sdw_cdns_probe() - Cadence probe routine
1642 * @cdns: Cadence instance
1643 */
1644int sdw_cdns_probe(struct sdw_cdns *cdns)
1645{
1646 init_completion(&cdns->tx_complete);
Vinod Koul07abeff2018-04-26 18:38:48 +05301647 cdns->bus.port_ops = &cdns_port_ops;
Sanyog Kale956baa12017-12-14 11:19:42 +05301648
Bard Liao4a98a6b2020-07-16 23:09:45 +08001649 INIT_WORK(&cdns->work, cdns_update_slave_status_work);
Sanyog Kale956baa12017-12-14 11:19:42 +05301650 return 0;
1651}
1652EXPORT_SYMBOL(sdw_cdns_probe);
1653
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301654int cdns_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossart63a6aa92021-12-24 10:10:34 +08001655 void *stream, int direction)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301656{
1657 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1658 struct sdw_cdns_dma_data *dma;
1659
Pierre-Louis Bossartb5e9e682020-07-01 02:43:55 +08001660 if (stream) {
1661 /* first paranoia check */
1662 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1663 dma = dai->playback_dma_data;
1664 else
1665 dma = dai->capture_dma_data;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301666
Pierre-Louis Bossartb5e9e682020-07-01 02:43:55 +08001667 if (dma) {
1668 dev_err(dai->dev,
1669 "dma_data already allocated for dai %s\n",
1670 dai->name);
1671 return -EINVAL;
1672 }
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301673
Pierre-Louis Bossartb5e9e682020-07-01 02:43:55 +08001674 /* allocate and set dma info */
1675 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
1676 if (!dma)
1677 return -ENOMEM;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301678
Pierre-Louis Bossart63a6aa92021-12-24 10:10:34 +08001679 dma->stream_type = SDW_STREAM_PCM;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301680
Pierre-Louis Bossartb5e9e682020-07-01 02:43:55 +08001681 dma->bus = &cdns->bus;
1682 dma->link_id = cdns->instance;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301683
Pierre-Louis Bossartb5e9e682020-07-01 02:43:55 +08001684 dma->stream = stream;
1685
1686 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1687 dai->playback_dma_data = dma;
1688 else
1689 dai->capture_dma_data = dma;
1690 } else {
1691 /* for NULL stream we release allocated dma_data */
1692 if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
1693 kfree(dai->playback_dma_data);
1694 dai->playback_dma_data = NULL;
1695 } else {
1696 kfree(dai->capture_dma_data);
1697 dai->capture_dma_data = NULL;
1698 }
1699 }
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301700 return 0;
1701}
1702EXPORT_SYMBOL(cdns_set_sdw_stream);
1703
1704/**
1705 * cdns_find_pdi() - Find a free PDI
1706 *
1707 * @cdns: Cadence instance
Pierre-Louis Bossart39737a32020-01-13 15:10:24 -06001708 * @offset: Starting offset
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301709 * @num: Number of PDIs
1710 * @pdi: PDI instances
Pierre-Louis Bossart39737a32020-01-13 15:10:24 -06001711 * @dai_id: DAI id
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301712 *
Bard Liao1b53385e2019-09-16 14:23:48 -05001713 * Find a PDI for a given PDI array. The PDI num and dai_id are
1714 * expected to match, return NULL otherwise.
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301715 */
1716static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -05001717 unsigned int offset,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001718 unsigned int num,
Bard Liao1b53385e2019-09-16 14:23:48 -05001719 struct sdw_cdns_pdi *pdi,
1720 int dai_id)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301721{
1722 int i;
1723
Bard Liao1b53385e2019-09-16 14:23:48 -05001724 for (i = offset; i < offset + num; i++)
1725 if (pdi[i].num == dai_id)
1726 return &pdi[i];
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301727
1728 return NULL;
1729}
1730
1731/**
1732 * sdw_cdns_config_stream: Configure a stream
1733 *
1734 * @cdns: Cadence instance
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301735 * @ch: Channel count
1736 * @dir: Data direction
1737 * @pdi: PDI to be used
1738 */
1739void sdw_cdns_config_stream(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001740 u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301741{
1742 u32 offset, val = 0;
1743
Pierre-Louis Bossart9e4e6012020-09-21 03:32:07 +08001744 if (dir == SDW_DATA_DIR_RX) {
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301745 val = CDNS_PORTCTRL_DIRN;
1746
Pierre-Louis Bossart9e4e6012020-09-21 03:32:07 +08001747 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1748 val |= CDNS_PORTCTRL_TEST_FAILED;
1749 }
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001750 offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
Pierre-Louis Bossart9e4e6012020-09-21 03:32:07 +08001751 cdns_updatel(cdns, offset,
1752 CDNS_PORTCTRL_DIRN | CDNS_PORTCTRL_TEST_FAILED,
1753 val);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301754
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001755 val = pdi->num;
randerwangb468a782020-03-17 11:33:29 -05001756 val |= CDNS_PDI_CONFIG_SOFT_RESET;
Vinod Koul3cf25d62020-09-03 17:15:01 +05301757 val |= FIELD_PREP(CDNS_PDI_CONFIG_CHANNEL, (1 << ch) - 1);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301758 cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1759}
1760EXPORT_SYMBOL(sdw_cdns_config_stream);
1761
1762/**
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001763 * sdw_cdns_alloc_pdi() - Allocate a PDI
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301764 *
1765 * @cdns: Cadence instance
1766 * @stream: Stream to be allocated
1767 * @ch: Channel count
1768 * @dir: Data direction
Pierre-Louis Bossart39737a32020-01-13 15:10:24 -06001769 * @dai_id: DAI id
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301770 */
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001771struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1772 struct sdw_cdns_streams *stream,
Bard Liao1b53385e2019-09-16 14:23:48 -05001773 u32 ch, u32 dir, int dai_id)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301774{
1775 struct sdw_cdns_pdi *pdi = NULL;
1776
1777 if (dir == SDW_DATA_DIR_RX)
Bard Liao1b53385e2019-09-16 14:23:48 -05001778 pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1779 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301780 else
Bard Liao1b53385e2019-09-16 14:23:48 -05001781 pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1782 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301783
1784 /* check if we found a PDI, else find in bi-directional */
1785 if (!pdi)
Bard Liao1b53385e2019-09-16 14:23:48 -05001786 pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd,
1787 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301788
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001789 if (pdi) {
1790 pdi->l_ch_num = 0;
1791 pdi->h_ch_num = ch - 1;
1792 pdi->dir = dir;
1793 pdi->ch_count = ch;
1794 }
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301795
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001796 return pdi;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301797}
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001798EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301799
Vinod Koul2f52a512017-12-14 11:19:41 +05301800MODULE_LICENSE("Dual BSD/GPL");
1801MODULE_DESCRIPTION("Cadence Soundwire Library");