Merge tag 'for-6.5-rc2-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[platform/kernel/linux-rpi.git] / drivers / soundwire / cadence_master.c
1 // 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>
11 #include <linux/debugfs.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/soundwire/sdw_registers.h>
18 #include <linux/soundwire/sdw.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <linux/workqueue.h>
22 #include "bus.h"
23 #include "cadence_master.h"
24
25 static int interrupt_mask;
26 module_param_named(cnds_mcp_int_mask, interrupt_mask, int, 0444);
27 MODULE_PARM_DESC(cdns_mcp_int_mask, "Cadence MCP IntMask");
28
29 #define CDNS_MCP_CONFIG                         0x0
30 #define CDNS_MCP_CONFIG_BUS_REL                 BIT(6)
31
32 #define CDNS_IP_MCP_CONFIG                      0x0 /* IP offset added at run-time */
33
34 #define CDNS_IP_MCP_CONFIG_MCMD_RETRY           GENMASK(27, 24)
35 #define CDNS_IP_MCP_CONFIG_MPREQ_DELAY          GENMASK(20, 16)
36 #define CDNS_IP_MCP_CONFIG_MMASTER              BIT(7)
37 #define CDNS_IP_MCP_CONFIG_SNIFFER              BIT(5)
38 #define CDNS_IP_MCP_CONFIG_CMD                  BIT(3)
39 #define CDNS_IP_MCP_CONFIG_OP                   GENMASK(2, 0)
40 #define CDNS_IP_MCP_CONFIG_OP_NORMAL            0
41
42 #define CDNS_MCP_CONTROL                        0x4
43
44 #define CDNS_MCP_CONTROL_CMD_RST                BIT(7)
45 #define CDNS_MCP_CONTROL_SOFT_RST               BIT(6)
46 #define CDNS_MCP_CONTROL_HW_RST                 BIT(4)
47 #define CDNS_MCP_CONTROL_CLK_STOP_CLR           BIT(2)
48
49 #define CDNS_IP_MCP_CONTROL                     0x4  /* IP offset added at run-time */
50
51 #define CDNS_IP_MCP_CONTROL_RST_DELAY           GENMASK(10, 8)
52 #define CDNS_IP_MCP_CONTROL_SW_RST              BIT(5)
53 #define CDNS_IP_MCP_CONTROL_CLK_PAUSE           BIT(3)
54 #define CDNS_IP_MCP_CONTROL_CMD_ACCEPT          BIT(1)
55 #define CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP        BIT(0)
56
57 #define CDNS_IP_MCP_CMDCTRL                     0x8 /* IP offset added at run-time */
58
59 #define CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR   BIT(2)
60
61 #define CDNS_MCP_SSPSTAT                        0xC
62 #define CDNS_MCP_FRAME_SHAPE                    0x10
63 #define CDNS_MCP_FRAME_SHAPE_INIT               0x14
64 #define CDNS_MCP_FRAME_SHAPE_COL_MASK           GENMASK(2, 0)
65 #define CDNS_MCP_FRAME_SHAPE_ROW_MASK           GENMASK(7, 3)
66
67 #define CDNS_MCP_CONFIG_UPDATE                  0x18
68 #define CDNS_MCP_CONFIG_UPDATE_BIT              BIT(0)
69
70 #define CDNS_MCP_PHYCTRL                        0x1C
71 #define CDNS_MCP_SSP_CTRL0                      0x20
72 #define CDNS_MCP_SSP_CTRL1                      0x28
73 #define CDNS_MCP_CLK_CTRL0                      0x30
74 #define CDNS_MCP_CLK_CTRL1                      0x38
75 #define CDNS_MCP_CLK_MCLKD_MASK         GENMASK(7, 0)
76
77 #define CDNS_MCP_STAT                           0x40
78
79 #define CDNS_MCP_STAT_ACTIVE_BANK               BIT(20)
80 #define CDNS_MCP_STAT_CLK_STOP                  BIT(16)
81
82 #define CDNS_MCP_INTSTAT                        0x44
83 #define CDNS_MCP_INTMASK                        0x48
84
85 #define CDNS_MCP_INT_IRQ                        BIT(31)
86 #define CDNS_MCP_INT_RESERVED1                  GENMASK(30, 17)
87 #define CDNS_MCP_INT_WAKEUP                     BIT(16)
88 #define CDNS_MCP_INT_SLAVE_RSVD                 BIT(15)
89 #define CDNS_MCP_INT_SLAVE_ALERT                BIT(14)
90 #define CDNS_MCP_INT_SLAVE_ATTACH               BIT(13)
91 #define CDNS_MCP_INT_SLAVE_NATTACH              BIT(12)
92 #define CDNS_MCP_INT_SLAVE_MASK                 GENMASK(15, 12)
93 #define CDNS_MCP_INT_DPINT                      BIT(11)
94 #define CDNS_MCP_INT_CTRL_CLASH                 BIT(10)
95 #define CDNS_MCP_INT_DATA_CLASH                 BIT(9)
96 #define CDNS_MCP_INT_PARITY                     BIT(8)
97 #define CDNS_MCP_INT_CMD_ERR                    BIT(7)
98 #define CDNS_MCP_INT_RESERVED2                  GENMASK(6, 4)
99 #define CDNS_MCP_INT_RX_NE                      BIT(3)
100 #define CDNS_MCP_INT_RX_WL                      BIT(2)
101 #define CDNS_MCP_INT_TXE                        BIT(1)
102 #define CDNS_MCP_INT_TXF                        BIT(0)
103 #define CDNS_MCP_INT_RESERVED (CDNS_MCP_INT_RESERVED1 | CDNS_MCP_INT_RESERVED2)
104
105 #define CDNS_MCP_INTSET                         0x4C
106
107 #define CDNS_MCP_SLAVE_STAT                     0x50
108 #define CDNS_MCP_SLAVE_STAT_MASK                GENMASK(1, 0)
109
110 #define CDNS_MCP_SLAVE_INTSTAT0                 0x54
111 #define CDNS_MCP_SLAVE_INTSTAT1                 0x58
112 #define CDNS_MCP_SLAVE_INTSTAT_NPRESENT         BIT(0)
113 #define CDNS_MCP_SLAVE_INTSTAT_ATTACHED         BIT(1)
114 #define CDNS_MCP_SLAVE_INTSTAT_ALERT            BIT(2)
115 #define CDNS_MCP_SLAVE_INTSTAT_RESERVED         BIT(3)
116 #define CDNS_MCP_SLAVE_STATUS_BITS              GENMASK(3, 0)
117 #define CDNS_MCP_SLAVE_STATUS_NUM               4
118
119 #define CDNS_MCP_SLAVE_INTMASK0                 0x5C
120 #define CDNS_MCP_SLAVE_INTMASK1                 0x60
121
122 #define CDNS_MCP_SLAVE_INTMASK0_MASK            GENMASK(31, 0)
123 #define CDNS_MCP_SLAVE_INTMASK1_MASK            GENMASK(15, 0)
124
125 #define CDNS_MCP_PORT_INTSTAT                   0x64
126 #define CDNS_MCP_PDI_STAT                       0x6C
127
128 #define CDNS_MCP_FIFOLEVEL                      0x78
129 #define CDNS_MCP_FIFOSTAT                       0x7C
130 #define CDNS_MCP_RX_FIFO_AVAIL                  GENMASK(5, 0)
131
132 #define CDNS_IP_MCP_CMD_BASE                    0x80 /* IP offset added at run-time */
133 #define CDNS_IP_MCP_RESP_BASE                   0x80 /* IP offset added at run-time */
134 /* FIFO can hold 8 commands */
135 #define CDNS_MCP_CMD_LEN                        8
136 #define CDNS_MCP_CMD_WORD_LEN                   0x4
137
138 #define CDNS_MCP_CMD_SSP_TAG                    BIT(31)
139 #define CDNS_MCP_CMD_COMMAND                    GENMASK(30, 28)
140 #define CDNS_MCP_CMD_DEV_ADDR                   GENMASK(27, 24)
141 #define CDNS_MCP_CMD_REG_ADDR                   GENMASK(23, 8)
142 #define CDNS_MCP_CMD_REG_DATA                   GENMASK(7, 0)
143
144 #define CDNS_MCP_CMD_READ                       2
145 #define CDNS_MCP_CMD_WRITE                      3
146
147 #define CDNS_MCP_RESP_RDATA                     GENMASK(15, 8)
148 #define CDNS_MCP_RESP_ACK                       BIT(0)
149 #define CDNS_MCP_RESP_NACK                      BIT(1)
150
151 #define CDNS_DP_SIZE                            128
152
153 #define CDNS_DPN_B0_CONFIG(n)                   (0x100 + CDNS_DP_SIZE * (n))
154 #define CDNS_DPN_B0_CH_EN(n)                    (0x104 + CDNS_DP_SIZE * (n))
155 #define CDNS_DPN_B0_SAMPLE_CTRL(n)              (0x108 + CDNS_DP_SIZE * (n))
156 #define CDNS_DPN_B0_OFFSET_CTRL(n)              (0x10C + CDNS_DP_SIZE * (n))
157 #define CDNS_DPN_B0_HCTRL(n)                    (0x110 + CDNS_DP_SIZE * (n))
158 #define CDNS_DPN_B0_ASYNC_CTRL(n)               (0x114 + CDNS_DP_SIZE * (n))
159
160 #define CDNS_DPN_B1_CONFIG(n)                   (0x118 + CDNS_DP_SIZE * (n))
161 #define CDNS_DPN_B1_CH_EN(n)                    (0x11C + CDNS_DP_SIZE * (n))
162 #define CDNS_DPN_B1_SAMPLE_CTRL(n)              (0x120 + CDNS_DP_SIZE * (n))
163 #define CDNS_DPN_B1_OFFSET_CTRL(n)              (0x124 + CDNS_DP_SIZE * (n))
164 #define CDNS_DPN_B1_HCTRL(n)                    (0x128 + CDNS_DP_SIZE * (n))
165 #define CDNS_DPN_B1_ASYNC_CTRL(n)               (0x12C + CDNS_DP_SIZE * (n))
166
167 #define CDNS_DPN_CONFIG_BPM                     BIT(18)
168 #define CDNS_DPN_CONFIG_BGC                     GENMASK(17, 16)
169 #define CDNS_DPN_CONFIG_WL                      GENMASK(12, 8)
170 #define CDNS_DPN_CONFIG_PORT_DAT                GENMASK(3, 2)
171 #define CDNS_DPN_CONFIG_PORT_FLOW               GENMASK(1, 0)
172
173 #define CDNS_DPN_SAMPLE_CTRL_SI                 GENMASK(15, 0)
174
175 #define CDNS_DPN_OFFSET_CTRL_1                  GENMASK(7, 0)
176 #define CDNS_DPN_OFFSET_CTRL_2                  GENMASK(15, 8)
177
178 #define CDNS_DPN_HCTRL_HSTOP                    GENMASK(3, 0)
179 #define CDNS_DPN_HCTRL_HSTART                   GENMASK(7, 4)
180 #define CDNS_DPN_HCTRL_LCTRL                    GENMASK(10, 8)
181
182 #define CDNS_PORTCTRL                           0x130
183 #define CDNS_PORTCTRL_TEST_FAILED               BIT(1)
184 #define CDNS_PORTCTRL_DIRN                      BIT(7)
185 #define CDNS_PORTCTRL_BANK_INVERT               BIT(8)
186
187 #define CDNS_PORT_OFFSET                        0x80
188
189 #define CDNS_PDI_CONFIG(n)                      (0x1100 + (n) * 16)
190
191 #define CDNS_PDI_CONFIG_SOFT_RESET              BIT(24)
192 #define CDNS_PDI_CONFIG_CHANNEL                 GENMASK(15, 8)
193 #define CDNS_PDI_CONFIG_PORT                    GENMASK(4, 0)
194
195 /* Driver defaults */
196 #define CDNS_TX_TIMEOUT                         500
197
198 #define CDNS_SCP_RX_FIFOLEVEL                   0x2
199
200 /*
201  * register accessor helpers
202  */
203 static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset)
204 {
205         return readl(cdns->registers + offset);
206 }
207
208 static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value)
209 {
210         writel(value, cdns->registers + offset);
211 }
212
213 static inline u32 cdns_ip_readl(struct sdw_cdns *cdns, int offset)
214 {
215         return cdns_readl(cdns, cdns->ip_offset + offset);
216 }
217
218 static inline void cdns_ip_writel(struct sdw_cdns *cdns, int offset, u32 value)
219 {
220         return cdns_writel(cdns, cdns->ip_offset + offset, value);
221 }
222
223 static inline void cdns_updatel(struct sdw_cdns *cdns,
224                                 int offset, u32 mask, u32 val)
225 {
226         u32 tmp;
227
228         tmp = cdns_readl(cdns, offset);
229         tmp = (tmp & ~mask) | val;
230         cdns_writel(cdns, offset, tmp);
231 }
232
233 static inline void cdns_ip_updatel(struct sdw_cdns *cdns,
234                                    int offset, u32 mask, u32 val)
235 {
236         cdns_updatel(cdns, cdns->ip_offset + offset, mask, val);
237 }
238
239 static int cdns_set_wait(struct sdw_cdns *cdns, int offset, u32 mask, u32 value)
240 {
241         int timeout = 10;
242         u32 reg_read;
243
244         /* Wait for bit to be set */
245         do {
246                 reg_read = readl(cdns->registers + offset);
247                 if ((reg_read & mask) == value)
248                         return 0;
249
250                 timeout--;
251                 usleep_range(50, 100);
252         } while (timeout != 0);
253
254         return -ETIMEDOUT;
255 }
256
257 static int cdns_clear_bit(struct sdw_cdns *cdns, int offset, u32 value)
258 {
259         writel(value, cdns->registers + offset);
260
261         /* Wait for bit to be self cleared */
262         return cdns_set_wait(cdns, offset, value, 0);
263 }
264
265 /*
266  * all changes to the MCP_CONFIG, MCP_CONTROL, MCP_CMDCTRL and MCP_PHYCTRL
267  * need to be confirmed with a write to MCP_CONFIG_UPDATE
268  */
269 static int cdns_config_update(struct sdw_cdns *cdns)
270 {
271         int ret;
272
273         if (sdw_cdns_is_clock_stop(cdns)) {
274                 dev_err(cdns->dev, "Cannot program MCP_CONFIG_UPDATE in ClockStopMode\n");
275                 return -EINVAL;
276         }
277
278         ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE,
279                              CDNS_MCP_CONFIG_UPDATE_BIT);
280         if (ret < 0)
281                 dev_err(cdns->dev, "Config update timedout\n");
282
283         return ret;
284 }
285
286 /**
287  * sdw_cdns_config_update() - Update configurations
288  * @cdns: Cadence instance
289  */
290 void sdw_cdns_config_update(struct sdw_cdns *cdns)
291 {
292         /* commit changes */
293         cdns_writel(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT);
294 }
295 EXPORT_SYMBOL(sdw_cdns_config_update);
296
297 /**
298  * sdw_cdns_config_update_set_wait() - wait until configuration update bit is self-cleared
299  * @cdns: Cadence instance
300  */
301 int sdw_cdns_config_update_set_wait(struct sdw_cdns *cdns)
302 {
303         /* the hardware recommendation is to wait at least 300us */
304         return cdns_set_wait(cdns, CDNS_MCP_CONFIG_UPDATE,
305                              CDNS_MCP_CONFIG_UPDATE_BIT, 0);
306 }
307 EXPORT_SYMBOL(sdw_cdns_config_update_set_wait);
308
309 /*
310  * debugfs
311  */
312 #ifdef CONFIG_DEBUG_FS
313
314 #define RD_BUF (2 * PAGE_SIZE)
315
316 static ssize_t cdns_sprintf(struct sdw_cdns *cdns,
317                             char *buf, size_t pos, unsigned int reg)
318 {
319         return scnprintf(buf + pos, RD_BUF - pos,
320                          "%4x\t%8x\n", reg, cdns_readl(cdns, reg));
321 }
322
323 static int cdns_reg_show(struct seq_file *s, void *data)
324 {
325         struct sdw_cdns *cdns = s->private;
326         char *buf;
327         ssize_t ret;
328         int num_ports;
329         int i, j;
330
331         buf = kzalloc(RD_BUF, GFP_KERNEL);
332         if (!buf)
333                 return -ENOMEM;
334
335         ret = scnprintf(buf, RD_BUF, "Register  Value\n");
336         ret += scnprintf(buf + ret, RD_BUF - ret, "\nMCP Registers\n");
337         /* 8 MCP registers */
338         for (i = CDNS_MCP_CONFIG; i <= CDNS_MCP_PHYCTRL; i += sizeof(u32))
339                 ret += cdns_sprintf(cdns, buf, ret, i);
340
341         ret += scnprintf(buf + ret, RD_BUF - ret,
342                          "\nStatus & Intr Registers\n");
343         /* 13 Status & Intr registers (offsets 0x70 and 0x74 not defined) */
344         for (i = CDNS_MCP_STAT; i <=  CDNS_MCP_FIFOSTAT; i += sizeof(u32))
345                 ret += cdns_sprintf(cdns, buf, ret, i);
346
347         ret += scnprintf(buf + ret, RD_BUF - ret,
348                          "\nSSP & Clk ctrl Registers\n");
349         ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL0);
350         ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL1);
351         ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL0);
352         ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL1);
353
354         ret += scnprintf(buf + ret, RD_BUF - ret,
355                          "\nDPn B0 Registers\n");
356
357         num_ports = cdns->num_ports;
358
359         for (i = 0; i < num_ports; i++) {
360                 ret += scnprintf(buf + ret, RD_BUF - ret,
361                                  "\nDP-%d\n", i);
362                 for (j = CDNS_DPN_B0_CONFIG(i);
363                      j < CDNS_DPN_B0_ASYNC_CTRL(i); j += sizeof(u32))
364                         ret += cdns_sprintf(cdns, buf, ret, j);
365         }
366
367         ret += scnprintf(buf + ret, RD_BUF - ret,
368                          "\nDPn B1 Registers\n");
369         for (i = 0; i < num_ports; i++) {
370                 ret += scnprintf(buf + ret, RD_BUF - ret,
371                                  "\nDP-%d\n", i);
372
373                 for (j = CDNS_DPN_B1_CONFIG(i);
374                      j < CDNS_DPN_B1_ASYNC_CTRL(i); j += sizeof(u32))
375                         ret += cdns_sprintf(cdns, buf, ret, j);
376         }
377
378         ret += scnprintf(buf + ret, RD_BUF - ret,
379                          "\nDPn Control Registers\n");
380         for (i = 0; i < num_ports; i++)
381                 ret += cdns_sprintf(cdns, buf, ret,
382                                 CDNS_PORTCTRL + i * CDNS_PORT_OFFSET);
383
384         ret += scnprintf(buf + ret, RD_BUF - ret,
385                          "\nPDIn Config Registers\n");
386
387         /* number of PDI and ports is interchangeable */
388         for (i = 0; i < num_ports; i++)
389                 ret += cdns_sprintf(cdns, buf, ret, CDNS_PDI_CONFIG(i));
390
391         seq_printf(s, "%s", buf);
392         kfree(buf);
393
394         return 0;
395 }
396 DEFINE_SHOW_ATTRIBUTE(cdns_reg);
397
398 static int cdns_hw_reset(void *data, u64 value)
399 {
400         struct sdw_cdns *cdns = data;
401         int ret;
402
403         if (value != 1)
404                 return -EINVAL;
405
406         /* Userspace changed the hardware state behind the kernel's back */
407         add_taint(TAINT_USER, LOCKDEP_STILL_OK);
408
409         ret = sdw_cdns_exit_reset(cdns);
410
411         dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret);
412
413         return ret;
414 }
415
416 DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n");
417
418 static int cdns_parity_error_injection(void *data, u64 value)
419 {
420         struct sdw_cdns *cdns = data;
421         struct sdw_bus *bus;
422         int ret;
423
424         if (value != 1)
425                 return -EINVAL;
426
427         bus = &cdns->bus;
428
429         /*
430          * Resume Master device. If this results in a bus reset, the
431          * Slave devices will re-attach and be re-enumerated.
432          */
433         ret = pm_runtime_resume_and_get(bus->dev);
434         if (ret < 0 && ret != -EACCES) {
435                 dev_err_ratelimited(cdns->dev,
436                                     "pm_runtime_resume_and_get failed in %s, ret %d\n",
437                                     __func__, ret);
438                 return ret;
439         }
440
441         /*
442          * wait long enough for Slave(s) to be in steady state. This
443          * does not need to be super precise.
444          */
445         msleep(200);
446
447         /*
448          * Take the bus lock here to make sure that any bus transactions
449          * will be queued while we inject a parity error on a dummy read
450          */
451         mutex_lock(&bus->bus_lock);
452
453         /* program hardware to inject parity error */
454         cdns_ip_updatel(cdns, CDNS_IP_MCP_CMDCTRL,
455                         CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR,
456                         CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR);
457
458         /* commit changes */
459         ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT);
460         if (ret < 0)
461                 goto unlock;
462
463         /* do a broadcast dummy read to avoid bus clashes */
464         ret = sdw_bread_no_pm_unlocked(&cdns->bus, 0xf, SDW_SCP_DEVID_0);
465         dev_info(cdns->dev, "parity error injection, read: %d\n", ret);
466
467         /* program hardware to disable parity error */
468         cdns_ip_updatel(cdns, CDNS_IP_MCP_CMDCTRL,
469                         CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR,
470                         0);
471
472         /* commit changes */
473         ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT);
474         if (ret < 0)
475                 goto unlock;
476
477         /* Userspace changed the hardware state behind the kernel's back */
478         add_taint(TAINT_USER, LOCKDEP_STILL_OK);
479
480 unlock:
481         /* Continue bus operation with parity error injection disabled */
482         mutex_unlock(&bus->bus_lock);
483
484         /*
485          * allow Master device to enter pm_runtime suspend. This may
486          * also result in Slave devices suspending.
487          */
488         pm_runtime_mark_last_busy(bus->dev);
489         pm_runtime_put_autosuspend(bus->dev);
490
491         return 0;
492 }
493
494 DEFINE_DEBUGFS_ATTRIBUTE(cdns_parity_error_fops, NULL,
495                          cdns_parity_error_injection, "%llu\n");
496
497 static int cdns_set_pdi_loopback_source(void *data, u64 value)
498 {
499         struct sdw_cdns *cdns = data;
500         unsigned int pdi_out_num = cdns->pcm.num_bd + cdns->pcm.num_out;
501
502         if (value > pdi_out_num)
503                 return -EINVAL;
504
505         /* Userspace changed the hardware state behind the kernel's back */
506         add_taint(TAINT_USER, LOCKDEP_STILL_OK);
507
508         cdns->pdi_loopback_source = value;
509
510         return 0;
511 }
512 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_source_fops, NULL, cdns_set_pdi_loopback_source, "%llu\n");
513
514 static int cdns_set_pdi_loopback_target(void *data, u64 value)
515 {
516         struct sdw_cdns *cdns = data;
517         unsigned int pdi_in_num = cdns->pcm.num_bd + cdns->pcm.num_in;
518
519         if (value > pdi_in_num)
520                 return -EINVAL;
521
522         /* Userspace changed the hardware state behind the kernel's back */
523         add_taint(TAINT_USER, LOCKDEP_STILL_OK);
524
525         cdns->pdi_loopback_target = value;
526
527         return 0;
528 }
529 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_target_fops, NULL, cdns_set_pdi_loopback_target, "%llu\n");
530
531 /**
532  * sdw_cdns_debugfs_init() - Cadence debugfs init
533  * @cdns: Cadence instance
534  * @root: debugfs root
535  */
536 void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
537 {
538         debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
539
540         debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
541                             &cdns_hw_reset_fops);
542
543         debugfs_create_file("cdns-parity-error-injection", 0200, root, cdns,
544                             &cdns_parity_error_fops);
545
546         cdns->pdi_loopback_source = -1;
547         cdns->pdi_loopback_target = -1;
548
549         debugfs_create_file("cdns-pdi-loopback-source", 0200, root, cdns,
550                             &cdns_pdi_loopback_source_fops);
551
552         debugfs_create_file("cdns-pdi-loopback-target", 0200, root, cdns,
553                             &cdns_pdi_loopback_target_fops);
554
555 }
556 EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
557
558 #endif /* CONFIG_DEBUG_FS */
559
560 /*
561  * IO Calls
562  */
563 static enum sdw_command_response
564 cdns_fill_msg_resp(struct sdw_cdns *cdns,
565                    struct sdw_msg *msg, int count, int offset)
566 {
567         int nack = 0, no_ack = 0;
568         int i;
569
570         /* check message response */
571         for (i = 0; i < count; i++) {
572                 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
573                         no_ack = 1;
574                         dev_vdbg(cdns->dev, "Msg Ack not received, cmd %d\n", i);
575                 }
576                 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
577                         nack = 1;
578                         dev_err_ratelimited(cdns->dev, "Msg NACK received, cmd %d\n", i);
579                 }
580         }
581
582         if (nack) {
583                 dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
584                 return SDW_CMD_FAIL;
585         }
586
587         if (no_ack) {
588                 dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
589                 return SDW_CMD_IGNORED;
590         }
591
592         if (msg->flags == SDW_MSG_FLAG_READ) {
593                 /* fill response */
594                 for (i = 0; i < count; i++)
595                         msg->buf[i + offset] = FIELD_GET(CDNS_MCP_RESP_RDATA,
596                                                          cdns->response_buf[i]);
597         }
598
599         return SDW_CMD_OK;
600 }
601
602 static void cdns_read_response(struct sdw_cdns *cdns)
603 {
604         u32 num_resp, cmd_base;
605         int i;
606
607         /* RX_FIFO_AVAIL can be 2 entries more than the FIFO size */
608         BUILD_BUG_ON(ARRAY_SIZE(cdns->response_buf) < CDNS_MCP_CMD_LEN + 2);
609
610         num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
611         num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
612         if (num_resp > ARRAY_SIZE(cdns->response_buf)) {
613                 dev_warn(cdns->dev, "RX AVAIL %d too long\n", num_resp);
614                 num_resp = ARRAY_SIZE(cdns->response_buf);
615         }
616
617         cmd_base = CDNS_IP_MCP_CMD_BASE;
618
619         for (i = 0; i < num_resp; i++) {
620                 cdns->response_buf[i] = cdns_ip_readl(cdns, cmd_base);
621                 cmd_base += CDNS_MCP_CMD_WORD_LEN;
622         }
623 }
624
625 static enum sdw_command_response
626 _cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
627                int offset, int count, bool defer)
628 {
629         unsigned long time;
630         u32 base, i, data;
631         u16 addr;
632
633         /* Program the watermark level for RX FIFO */
634         if (cdns->msg_count != count) {
635                 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count);
636                 cdns->msg_count = count;
637         }
638
639         base = CDNS_IP_MCP_CMD_BASE;
640         addr = msg->addr + offset;
641
642         for (i = 0; i < count; i++) {
643                 data = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
644                 data |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, cmd);
645                 data |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, addr);
646                 addr++;
647
648                 if (msg->flags == SDW_MSG_FLAG_WRITE)
649                         data |= msg->buf[i + offset];
650
651                 data |= FIELD_PREP(CDNS_MCP_CMD_SSP_TAG, msg->ssp_sync);
652                 cdns_ip_writel(cdns, base, data);
653                 base += CDNS_MCP_CMD_WORD_LEN;
654         }
655
656         if (defer)
657                 return SDW_CMD_OK;
658
659         /* wait for timeout or response */
660         time = wait_for_completion_timeout(&cdns->tx_complete,
661                                            msecs_to_jiffies(CDNS_TX_TIMEOUT));
662         if (!time) {
663                 dev_err(cdns->dev, "IO transfer timed out, cmd %d device %d addr %x len %d\n",
664                         cmd, msg->dev_num, msg->addr, msg->len);
665                 msg->len = 0;
666
667                 /* Drain anything in the RX_FIFO */
668                 cdns_read_response(cdns);
669
670                 return SDW_CMD_TIMEOUT;
671         }
672
673         return cdns_fill_msg_resp(cdns, msg, count, offset);
674 }
675
676 static enum sdw_command_response
677 cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
678 {
679         int nack = 0, no_ack = 0;
680         unsigned long time;
681         u32 data[2], base;
682         int i;
683
684         /* Program the watermark level for RX FIFO */
685         if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
686                 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
687                 cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
688         }
689
690         data[0] = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
691         data[0] |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, 0x3);
692         data[1] = data[0];
693
694         data[0] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE1);
695         data[1] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE2);
696
697         data[0] |= msg->addr_page1;
698         data[1] |= msg->addr_page2;
699
700         base = CDNS_IP_MCP_CMD_BASE;
701         cdns_ip_writel(cdns, base, data[0]);
702         base += CDNS_MCP_CMD_WORD_LEN;
703         cdns_ip_writel(cdns, base, data[1]);
704
705         time = wait_for_completion_timeout(&cdns->tx_complete,
706                                            msecs_to_jiffies(CDNS_TX_TIMEOUT));
707         if (!time) {
708                 dev_err(cdns->dev, "SCP Msg trf timed out\n");
709                 msg->len = 0;
710                 return SDW_CMD_TIMEOUT;
711         }
712
713         /* check response the writes */
714         for (i = 0; i < 2; i++) {
715                 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
716                         no_ack = 1;
717                         dev_err(cdns->dev, "Program SCP Ack not received\n");
718                         if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
719                                 nack = 1;
720                                 dev_err(cdns->dev, "Program SCP NACK received\n");
721                         }
722                 }
723         }
724
725         /* For NACK, NO ack, don't return err if we are in Broadcast mode */
726         if (nack) {
727                 dev_err_ratelimited(cdns->dev,
728                                     "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
729                 return SDW_CMD_FAIL;
730         }
731
732         if (no_ack) {
733                 dev_dbg_ratelimited(cdns->dev,
734                                     "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
735                 return SDW_CMD_IGNORED;
736         }
737
738         return SDW_CMD_OK;
739 }
740
741 static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
742 {
743         int ret;
744
745         if (msg->page) {
746                 ret = cdns_program_scp_addr(cdns, msg);
747                 if (ret) {
748                         msg->len = 0;
749                         return ret;
750                 }
751         }
752
753         switch (msg->flags) {
754         case SDW_MSG_FLAG_READ:
755                 *cmd = CDNS_MCP_CMD_READ;
756                 break;
757
758         case SDW_MSG_FLAG_WRITE:
759                 *cmd = CDNS_MCP_CMD_WRITE;
760                 break;
761
762         default:
763                 dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
764                 return -EINVAL;
765         }
766
767         return 0;
768 }
769
770 enum sdw_command_response
771 cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
772 {
773         struct sdw_cdns *cdns = bus_to_cdns(bus);
774         int cmd = 0, ret, i;
775
776         ret = cdns_prep_msg(cdns, msg, &cmd);
777         if (ret)
778                 return SDW_CMD_FAIL_OTHER;
779
780         for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
781                 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
782                                      CDNS_MCP_CMD_LEN, false);
783                 if (ret != SDW_CMD_OK)
784                         return ret;
785         }
786
787         if (!(msg->len % CDNS_MCP_CMD_LEN))
788                 return SDW_CMD_OK;
789
790         return _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
791                               msg->len % CDNS_MCP_CMD_LEN, false);
792 }
793 EXPORT_SYMBOL(cdns_xfer_msg);
794
795 enum sdw_command_response
796 cdns_xfer_msg_defer(struct sdw_bus *bus)
797 {
798         struct sdw_cdns *cdns = bus_to_cdns(bus);
799         struct sdw_defer *defer = &bus->defer_msg;
800         struct sdw_msg *msg = defer->msg;
801         int cmd = 0, ret;
802
803         /* for defer only 1 message is supported */
804         if (msg->len > 1)
805                 return -ENOTSUPP;
806
807         ret = cdns_prep_msg(cdns, msg, &cmd);
808         if (ret)
809                 return SDW_CMD_FAIL_OTHER;
810
811         return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
812 }
813 EXPORT_SYMBOL(cdns_xfer_msg_defer);
814
815 u32 cdns_read_ping_status(struct sdw_bus *bus)
816 {
817         struct sdw_cdns *cdns = bus_to_cdns(bus);
818
819         return cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
820 }
821 EXPORT_SYMBOL(cdns_read_ping_status);
822
823 /*
824  * IRQ handling
825  */
826
827 static int cdns_update_slave_status(struct sdw_cdns *cdns,
828                                     u64 slave_intstat)
829 {
830         enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
831         bool is_slave = false;
832         u32 mask;
833         u32 val;
834         int i, set_status;
835
836         memset(status, 0, sizeof(status));
837
838         for (i = 0; i <= SDW_MAX_DEVICES; i++) {
839                 mask = (slave_intstat >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
840                         CDNS_MCP_SLAVE_STATUS_BITS;
841
842                 set_status = 0;
843
844                 if (mask) {
845                         is_slave = true;
846
847                         if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
848                                 status[i] = SDW_SLAVE_RESERVED;
849                                 set_status++;
850                         }
851
852                         if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
853                                 status[i] = SDW_SLAVE_ATTACHED;
854                                 set_status++;
855                         }
856
857                         if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
858                                 status[i] = SDW_SLAVE_ALERT;
859                                 set_status++;
860                         }
861
862                         if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
863                                 status[i] = SDW_SLAVE_UNATTACHED;
864                                 set_status++;
865                         }
866                 }
867
868                 /*
869                  * check that there was a single reported Slave status and when
870                  * there is not use the latest status extracted from PING commands
871                  */
872                 if (set_status != 1) {
873                         val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
874                         val >>= (i * 2);
875
876                         switch (val & 0x3) {
877                         case 0:
878                                 status[i] = SDW_SLAVE_UNATTACHED;
879                                 break;
880                         case 1:
881                                 status[i] = SDW_SLAVE_ATTACHED;
882                                 break;
883                         case 2:
884                                 status[i] = SDW_SLAVE_ALERT;
885                                 break;
886                         case 3:
887                         default:
888                                 status[i] = SDW_SLAVE_RESERVED;
889                                 break;
890                         }
891                 }
892         }
893
894         if (is_slave)
895                 return sdw_handle_slave_status(&cdns->bus, status);
896
897         return 0;
898 }
899
900 /**
901  * sdw_cdns_irq() - Cadence interrupt handler
902  * @irq: irq number
903  * @dev_id: irq context
904  */
905 irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
906 {
907         struct sdw_cdns *cdns = dev_id;
908         u32 int_status;
909
910         /* Check if the link is up */
911         if (!cdns->link_up)
912                 return IRQ_NONE;
913
914         int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
915
916         /* check for reserved values read as zero */
917         if (int_status & CDNS_MCP_INT_RESERVED)
918                 return IRQ_NONE;
919
920         if (!(int_status & CDNS_MCP_INT_IRQ))
921                 return IRQ_NONE;
922
923         if (int_status & CDNS_MCP_INT_RX_WL) {
924                 struct sdw_bus *bus = &cdns->bus;
925                 struct sdw_defer *defer = &bus->defer_msg;
926
927                 cdns_read_response(cdns);
928
929                 if (defer && defer->msg) {
930                         cdns_fill_msg_resp(cdns, defer->msg,
931                                            defer->length, 0);
932                         complete(&defer->complete);
933                 } else {
934                         complete(&cdns->tx_complete);
935                 }
936         }
937
938         if (int_status & CDNS_MCP_INT_PARITY) {
939                 /* Parity error detected by Master */
940                 dev_err_ratelimited(cdns->dev, "Parity error\n");
941         }
942
943         if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
944                 /* Slave is driving bit slot during control word */
945                 dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
946         }
947
948         if (int_status & CDNS_MCP_INT_DATA_CLASH) {
949                 /*
950                  * Multiple slaves trying to drive bit slot, or issue with
951                  * ownership of data bits or Slave gone bonkers
952                  */
953                 dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
954         }
955
956         if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL &&
957             int_status & CDNS_MCP_INT_DPINT) {
958                 u32 port_intstat;
959
960                 /* just log which ports report an error */
961                 port_intstat = cdns_readl(cdns, CDNS_MCP_PORT_INTSTAT);
962                 dev_err_ratelimited(cdns->dev, "DP interrupt: PortIntStat %8x\n",
963                                     port_intstat);
964
965                 /* clear status w/ write1 */
966                 cdns_writel(cdns, CDNS_MCP_PORT_INTSTAT, port_intstat);
967         }
968
969         if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
970                 /* Mask the Slave interrupt and wake thread */
971                 cdns_updatel(cdns, CDNS_MCP_INTMASK,
972                              CDNS_MCP_INT_SLAVE_MASK, 0);
973
974                 int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
975
976                 /*
977                  * Deal with possible race condition between interrupt
978                  * handling and disabling interrupts on suspend.
979                  *
980                  * If the master is in the process of disabling
981                  * interrupts, don't schedule a workqueue
982                  */
983                 if (cdns->interrupt_enabled)
984                         schedule_work(&cdns->work);
985         }
986
987         cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
988         return IRQ_HANDLED;
989 }
990 EXPORT_SYMBOL(sdw_cdns_irq);
991
992 /**
993  * cdns_update_slave_status_work - update slave status in a work since we will need to handle
994  * other interrupts eg. CDNS_MCP_INT_RX_WL during the update slave
995  * process.
996  * @work: cdns worker thread
997  */
998 static void cdns_update_slave_status_work(struct work_struct *work)
999 {
1000         struct sdw_cdns *cdns =
1001                 container_of(work, struct sdw_cdns, work);
1002         u32 slave0, slave1;
1003         u64 slave_intstat;
1004         u32 device0_status;
1005         int retry_count = 0;
1006
1007         /*
1008          * Clear main interrupt first so we don't lose any assertions
1009          * that happen during this function.
1010          */
1011         cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
1012
1013         slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
1014         slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
1015
1016         /*
1017          * Clear the bits before handling so we don't lose any
1018          * bits that re-assert.
1019          */
1020         cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
1021         cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
1022
1023         /* combine the two status */
1024         slave_intstat = ((u64)slave1 << 32) | slave0;
1025
1026         dev_dbg_ratelimited(cdns->dev, "Slave status change: 0x%llx\n", slave_intstat);
1027
1028 update_status:
1029         cdns_update_slave_status(cdns, slave_intstat);
1030
1031         /*
1032          * When there is more than one peripheral per link, it's
1033          * possible that a deviceB becomes attached after we deal with
1034          * the attachment of deviceA. Since the hardware does a
1035          * logical AND, the attachment of the second device does not
1036          * change the status seen by the driver.
1037          *
1038          * In that case, clearing the registers above would result in
1039          * the deviceB never being detected - until a change of status
1040          * is observed on the bus.
1041          *
1042          * To avoid this race condition, re-check if any device0 needs
1043          * attention with PING commands. There is no need to check for
1044          * ALERTS since they are not allowed until a non-zero
1045          * device_number is assigned.
1046          *
1047          * Do not clear the INTSTAT0/1. While looping to enumerate devices on
1048          * #0 there could be status changes on other devices - these must
1049          * be kept in the INTSTAT so they can be handled when all #0 devices
1050          * have been handled.
1051          */
1052
1053         device0_status = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
1054         device0_status &= 3;
1055
1056         if (device0_status == SDW_SLAVE_ATTACHED) {
1057                 if (retry_count++ < SDW_MAX_DEVICES) {
1058                         dev_dbg_ratelimited(cdns->dev,
1059                                             "Device0 detected after clearing status, iteration %d\n",
1060                                             retry_count);
1061                         slave_intstat = CDNS_MCP_SLAVE_INTSTAT_ATTACHED;
1062                         goto update_status;
1063                 } else {
1064                         dev_err_ratelimited(cdns->dev,
1065                                             "Device0 detected after %d iterations\n",
1066                                             retry_count);
1067                 }
1068         }
1069
1070         /* unmask Slave interrupt now */
1071         cdns_updatel(cdns, CDNS_MCP_INTMASK,
1072                      CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
1073
1074 }
1075
1076 /* paranoia check to make sure self-cleared bits are indeed cleared */
1077 void sdw_cdns_check_self_clearing_bits(struct sdw_cdns *cdns, const char *string,
1078                                        bool initial_delay, int reset_iterations)
1079 {
1080         u32 ip_mcp_control;
1081         u32 mcp_control;
1082         u32 mcp_config_update;
1083         int i;
1084
1085         if (initial_delay)
1086                 usleep_range(1000, 1500);
1087
1088         ip_mcp_control = cdns_ip_readl(cdns, CDNS_IP_MCP_CONTROL);
1089
1090         /* the following bits should be cleared immediately */
1091         if (ip_mcp_control & CDNS_IP_MCP_CONTROL_SW_RST)
1092                 dev_err(cdns->dev, "%s failed: IP_MCP_CONTROL_SW_RST is not cleared\n", string);
1093
1094         mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
1095
1096         /* the following bits should be cleared immediately */
1097         if (mcp_control & CDNS_MCP_CONTROL_CMD_RST)
1098                 dev_err(cdns->dev, "%s failed: MCP_CONTROL_CMD_RST is not cleared\n", string);
1099         if (mcp_control & CDNS_MCP_CONTROL_SOFT_RST)
1100                 dev_err(cdns->dev, "%s failed: MCP_CONTROL_SOFT_RST is not cleared\n", string);
1101         if (mcp_control & CDNS_MCP_CONTROL_CLK_STOP_CLR)
1102                 dev_err(cdns->dev, "%s failed: MCP_CONTROL_CLK_STOP_CLR is not cleared\n", string);
1103
1104         mcp_config_update = cdns_readl(cdns, CDNS_MCP_CONFIG_UPDATE);
1105         if (mcp_config_update & CDNS_MCP_CONFIG_UPDATE_BIT)
1106                 dev_err(cdns->dev, "%s failed: MCP_CONFIG_UPDATE_BIT is not cleared\n", string);
1107
1108         i = 0;
1109         while (mcp_control & CDNS_MCP_CONTROL_HW_RST) {
1110                 if (i == reset_iterations) {
1111                         dev_err(cdns->dev, "%s failed: MCP_CONTROL_HW_RST is not cleared\n", string);
1112                         break;
1113                 }
1114
1115                 dev_dbg(cdns->dev, "%s: MCP_CONTROL_HW_RST is not cleared at iteration %d\n", string, i);
1116                 i++;
1117
1118                 usleep_range(1000, 1500);
1119                 mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
1120         }
1121
1122 }
1123 EXPORT_SYMBOL(sdw_cdns_check_self_clearing_bits);
1124
1125 /*
1126  * init routines
1127  */
1128
1129 /**
1130  * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
1131  * @cdns: Cadence instance
1132  */
1133 int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
1134 {
1135         /* keep reset delay unchanged to 4096 cycles */
1136
1137         /* use hardware generated reset */
1138         cdns_updatel(cdns, CDNS_MCP_CONTROL,
1139                      CDNS_MCP_CONTROL_HW_RST,
1140                      CDNS_MCP_CONTROL_HW_RST);
1141
1142         /* commit changes */
1143         return cdns_config_update(cdns);
1144 }
1145 EXPORT_SYMBOL(sdw_cdns_exit_reset);
1146
1147 /**
1148  * cdns_enable_slave_interrupts() - Enable SDW slave interrupts
1149  * @cdns: Cadence instance
1150  * @state: boolean for true/false
1151  */
1152 static void cdns_enable_slave_interrupts(struct sdw_cdns *cdns, bool state)
1153 {
1154         u32 mask;
1155
1156         mask = cdns_readl(cdns, CDNS_MCP_INTMASK);
1157         if (state)
1158                 mask |= CDNS_MCP_INT_SLAVE_MASK;
1159         else
1160                 mask &= ~CDNS_MCP_INT_SLAVE_MASK;
1161
1162         cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1163 }
1164
1165 /**
1166  * sdw_cdns_enable_interrupt() - Enable SDW interrupts
1167  * @cdns: Cadence instance
1168  * @state: True if we are trying to enable interrupt.
1169  */
1170 int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
1171 {
1172         u32 slave_intmask0 = 0;
1173         u32 slave_intmask1 = 0;
1174         u32 mask = 0;
1175
1176         if (!state)
1177                 goto update_masks;
1178
1179         slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
1180         slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
1181
1182         /* enable detection of all slave state changes */
1183         mask = CDNS_MCP_INT_SLAVE_MASK;
1184
1185         /* enable detection of bus issues */
1186         mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
1187                 CDNS_MCP_INT_PARITY;
1188
1189         /* port interrupt limited to test modes for now */
1190         if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1191                 mask |= CDNS_MCP_INT_DPINT;
1192
1193         /* enable detection of RX fifo level */
1194         mask |= CDNS_MCP_INT_RX_WL;
1195
1196         /*
1197          * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
1198          * settings are irrelevant
1199          */
1200         mask |= CDNS_MCP_INT_IRQ;
1201
1202         if (interrupt_mask) /* parameter override */
1203                 mask = interrupt_mask;
1204
1205 update_masks:
1206         /* clear slave interrupt status before enabling interrupt */
1207         if (state) {
1208                 u32 slave_state;
1209
1210                 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
1211                 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state);
1212                 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
1213                 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state);
1214         }
1215         cdns->interrupt_enabled = state;
1216
1217         /*
1218          * Complete any on-going status updates before updating masks,
1219          * and cancel queued status updates.
1220          *
1221          * There could be a race with a new interrupt thrown before
1222          * the 3 mask updates below are complete, so in the interrupt
1223          * we use the 'interrupt_enabled' status to prevent new work
1224          * from being queued.
1225          */
1226         if (!state)
1227                 cancel_work_sync(&cdns->work);
1228
1229         cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
1230         cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
1231         cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1232
1233         return 0;
1234 }
1235 EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
1236
1237 static int cdns_allocate_pdi(struct sdw_cdns *cdns,
1238                              struct sdw_cdns_pdi **stream,
1239                              u32 num, u32 pdi_offset)
1240 {
1241         struct sdw_cdns_pdi *pdi;
1242         int i;
1243
1244         if (!num)
1245                 return 0;
1246
1247         pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
1248         if (!pdi)
1249                 return -ENOMEM;
1250
1251         for (i = 0; i < num; i++) {
1252                 pdi[i].num = i + pdi_offset;
1253         }
1254
1255         *stream = pdi;
1256         return 0;
1257 }
1258
1259 /**
1260  * sdw_cdns_pdi_init() - PDI initialization routine
1261  *
1262  * @cdns: Cadence instance
1263  * @config: Stream configurations
1264  */
1265 int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
1266                       struct sdw_cdns_stream_config config)
1267 {
1268         struct sdw_cdns_streams *stream;
1269         int offset;
1270         int ret;
1271
1272         cdns->pcm.num_bd = config.pcm_bd;
1273         cdns->pcm.num_in = config.pcm_in;
1274         cdns->pcm.num_out = config.pcm_out;
1275
1276         /* Allocate PDIs for PCMs */
1277         stream = &cdns->pcm;
1278
1279         /* we allocate PDI0 and PDI1 which are used for Bulk */
1280         offset = 0;
1281
1282         ret = cdns_allocate_pdi(cdns, &stream->bd,
1283                                 stream->num_bd, offset);
1284         if (ret)
1285                 return ret;
1286
1287         offset += stream->num_bd;
1288
1289         ret = cdns_allocate_pdi(cdns, &stream->in,
1290                                 stream->num_in, offset);
1291         if (ret)
1292                 return ret;
1293
1294         offset += stream->num_in;
1295
1296         ret = cdns_allocate_pdi(cdns, &stream->out,
1297                                 stream->num_out, offset);
1298         if (ret)
1299                 return ret;
1300
1301         /* Update total number of PCM PDIs */
1302         stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1303         cdns->num_ports = stream->num_pdi;
1304
1305         return 0;
1306 }
1307 EXPORT_SYMBOL(sdw_cdns_pdi_init);
1308
1309 static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
1310 {
1311         u32 val;
1312         int c;
1313         int r;
1314
1315         r = sdw_find_row_index(n_rows);
1316         c = sdw_find_col_index(n_cols);
1317
1318         val = FIELD_PREP(CDNS_MCP_FRAME_SHAPE_ROW_MASK, r);
1319         val |= FIELD_PREP(CDNS_MCP_FRAME_SHAPE_COL_MASK, c);
1320
1321         return val;
1322 }
1323
1324 static void cdns_init_clock_ctrl(struct sdw_cdns *cdns)
1325 {
1326         struct sdw_bus *bus = &cdns->bus;
1327         struct sdw_master_prop *prop = &bus->prop;
1328         u32 val;
1329         u32 ssp_interval;
1330         int divider;
1331
1332         /* Set clock divider */
1333         divider = (prop->mclk_freq / prop->max_clk_freq) - 1;
1334
1335         cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1336                      CDNS_MCP_CLK_MCLKD_MASK, divider);
1337         cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1338                      CDNS_MCP_CLK_MCLKD_MASK, divider);
1339
1340         /*
1341          * Frame shape changes after initialization have to be done
1342          * with the bank switch mechanism
1343          */
1344         val = cdns_set_initial_frame_shape(prop->default_row,
1345                                            prop->default_col);
1346         cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
1347
1348         /* Set SSP interval to default value */
1349         ssp_interval = prop->default_frame_rate / SDW_CADENCE_GSYNC_HZ;
1350         cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, ssp_interval);
1351         cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, ssp_interval);
1352 }
1353
1354 /**
1355  * sdw_cdns_init() - Cadence initialization
1356  * @cdns: Cadence instance
1357  */
1358 int sdw_cdns_init(struct sdw_cdns *cdns)
1359 {
1360         u32 val;
1361
1362         cdns_init_clock_ctrl(cdns);
1363
1364         sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1365
1366         /* reset msg_count to default value of FIFOLEVEL */
1367         cdns->msg_count = cdns_readl(cdns, CDNS_MCP_FIFOLEVEL);
1368
1369         /* flush command FIFOs */
1370         cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1371                      CDNS_MCP_CONTROL_CMD_RST);
1372
1373         /* Set cmd accept mode */
1374         cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, CDNS_IP_MCP_CONTROL_CMD_ACCEPT,
1375                         CDNS_IP_MCP_CONTROL_CMD_ACCEPT);
1376
1377         /* Configure mcp config */
1378         val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1379
1380         /* Disable auto bus release */
1381         val &= ~CDNS_MCP_CONFIG_BUS_REL;
1382
1383         cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1384
1385         /* Configure IP mcp config */
1386         val = cdns_ip_readl(cdns, CDNS_IP_MCP_CONFIG);
1387
1388         /* enable bus operations with clock and data */
1389         val &= ~CDNS_IP_MCP_CONFIG_OP;
1390         val |= CDNS_IP_MCP_CONFIG_OP_NORMAL;
1391
1392         /* Set cmd mode for Tx and Rx cmds */
1393         val &= ~CDNS_IP_MCP_CONFIG_CMD;
1394
1395         /* Disable sniffer mode */
1396         val &= ~CDNS_IP_MCP_CONFIG_SNIFFER;
1397
1398         if (cdns->bus.multi_link)
1399                 /* Set Multi-master mode to take gsync into account */
1400                 val |= CDNS_IP_MCP_CONFIG_MMASTER;
1401
1402         /* leave frame delay to hardware default of 0x1F */
1403
1404         /* leave command retry to hardware default of 0 */
1405
1406         cdns_ip_writel(cdns, CDNS_IP_MCP_CONFIG, val);
1407
1408         /* changes will be committed later */
1409         return 0;
1410 }
1411 EXPORT_SYMBOL(sdw_cdns_init);
1412
1413 int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1414 {
1415         struct sdw_master_prop *prop = &bus->prop;
1416         struct sdw_cdns *cdns = bus_to_cdns(bus);
1417         int mcp_clkctrl_off;
1418         int divider;
1419
1420         if (!params->curr_dr_freq) {
1421                 dev_err(cdns->dev, "NULL curr_dr_freq\n");
1422                 return -EINVAL;
1423         }
1424
1425         divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1426                 params->curr_dr_freq;
1427         divider--; /* divider is 1/(N+1) */
1428
1429         if (params->next_bank)
1430                 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1431         else
1432                 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1433
1434         cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
1435
1436         return 0;
1437 }
1438 EXPORT_SYMBOL(cdns_bus_conf);
1439
1440 static int cdns_port_params(struct sdw_bus *bus,
1441                             struct sdw_port_params *p_params, unsigned int bank)
1442 {
1443         struct sdw_cdns *cdns = bus_to_cdns(bus);
1444         int dpn_config_off_source;
1445         int dpn_config_off_target;
1446         int target_num = p_params->num;
1447         int source_num = p_params->num;
1448         bool override = false;
1449         int dpn_config;
1450
1451         if (target_num == cdns->pdi_loopback_target &&
1452             cdns->pdi_loopback_source != -1) {
1453                 source_num = cdns->pdi_loopback_source;
1454                 override = true;
1455         }
1456
1457         if (bank) {
1458                 dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1459                 dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1460         } else {
1461                 dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1462                 dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1463         }
1464
1465         dpn_config = cdns_readl(cdns, dpn_config_off_source);
1466
1467         /* use port params if there is no loopback, otherwise use source as is */
1468         if (!override) {
1469                 u32p_replace_bits(&dpn_config, p_params->bps - 1, CDNS_DPN_CONFIG_WL);
1470                 u32p_replace_bits(&dpn_config, p_params->flow_mode, CDNS_DPN_CONFIG_PORT_FLOW);
1471                 u32p_replace_bits(&dpn_config, p_params->data_mode, CDNS_DPN_CONFIG_PORT_DAT);
1472         }
1473
1474         cdns_writel(cdns, dpn_config_off_target, dpn_config);
1475
1476         return 0;
1477 }
1478
1479 static int cdns_transport_params(struct sdw_bus *bus,
1480                                  struct sdw_transport_params *t_params,
1481                                  enum sdw_reg_bank bank)
1482 {
1483         struct sdw_cdns *cdns = bus_to_cdns(bus);
1484         int dpn_config;
1485         int dpn_config_off_source;
1486         int dpn_config_off_target;
1487         int dpn_hctrl;
1488         int dpn_hctrl_off_source;
1489         int dpn_hctrl_off_target;
1490         int dpn_offsetctrl;
1491         int dpn_offsetctrl_off_source;
1492         int dpn_offsetctrl_off_target;
1493         int dpn_samplectrl;
1494         int dpn_samplectrl_off_source;
1495         int dpn_samplectrl_off_target;
1496         int source_num = t_params->port_num;
1497         int target_num = t_params->port_num;
1498         bool override = false;
1499
1500         if (target_num == cdns->pdi_loopback_target &&
1501             cdns->pdi_loopback_source != -1) {
1502                 source_num = cdns->pdi_loopback_source;
1503                 override = true;
1504         }
1505
1506         /*
1507          * Note: Only full data port is supported on the Master side for
1508          * both PCM and PDM ports.
1509          */
1510
1511         if (bank) {
1512                 dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1513                 dpn_hctrl_off_source = CDNS_DPN_B1_HCTRL(source_num);
1514                 dpn_offsetctrl_off_source = CDNS_DPN_B1_OFFSET_CTRL(source_num);
1515                 dpn_samplectrl_off_source = CDNS_DPN_B1_SAMPLE_CTRL(source_num);
1516
1517                 dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1518                 dpn_hctrl_off_target = CDNS_DPN_B1_HCTRL(target_num);
1519                 dpn_offsetctrl_off_target = CDNS_DPN_B1_OFFSET_CTRL(target_num);
1520                 dpn_samplectrl_off_target = CDNS_DPN_B1_SAMPLE_CTRL(target_num);
1521
1522         } else {
1523                 dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1524                 dpn_hctrl_off_source = CDNS_DPN_B0_HCTRL(source_num);
1525                 dpn_offsetctrl_off_source = CDNS_DPN_B0_OFFSET_CTRL(source_num);
1526                 dpn_samplectrl_off_source = CDNS_DPN_B0_SAMPLE_CTRL(source_num);
1527
1528                 dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1529                 dpn_hctrl_off_target = CDNS_DPN_B0_HCTRL(target_num);
1530                 dpn_offsetctrl_off_target = CDNS_DPN_B0_OFFSET_CTRL(target_num);
1531                 dpn_samplectrl_off_target = CDNS_DPN_B0_SAMPLE_CTRL(target_num);
1532         }
1533
1534         dpn_config = cdns_readl(cdns, dpn_config_off_source);
1535         if (!override) {
1536                 u32p_replace_bits(&dpn_config, t_params->blk_grp_ctrl, CDNS_DPN_CONFIG_BGC);
1537                 u32p_replace_bits(&dpn_config, t_params->blk_pkg_mode, CDNS_DPN_CONFIG_BPM);
1538         }
1539         cdns_writel(cdns, dpn_config_off_target, dpn_config);
1540
1541         if (!override) {
1542                 dpn_offsetctrl = 0;
1543                 u32p_replace_bits(&dpn_offsetctrl, t_params->offset1, CDNS_DPN_OFFSET_CTRL_1);
1544                 u32p_replace_bits(&dpn_offsetctrl, t_params->offset2, CDNS_DPN_OFFSET_CTRL_2);
1545         } else {
1546                 dpn_offsetctrl = cdns_readl(cdns, dpn_offsetctrl_off_source);
1547         }
1548         cdns_writel(cdns, dpn_offsetctrl_off_target,  dpn_offsetctrl);
1549
1550         if (!override) {
1551                 dpn_hctrl = 0;
1552                 u32p_replace_bits(&dpn_hctrl, t_params->hstart, CDNS_DPN_HCTRL_HSTART);
1553                 u32p_replace_bits(&dpn_hctrl, t_params->hstop, CDNS_DPN_HCTRL_HSTOP);
1554                 u32p_replace_bits(&dpn_hctrl, t_params->lane_ctrl, CDNS_DPN_HCTRL_LCTRL);
1555         } else {
1556                 dpn_hctrl = cdns_readl(cdns, dpn_hctrl_off_source);
1557         }
1558         cdns_writel(cdns, dpn_hctrl_off_target, dpn_hctrl);
1559
1560         if (!override)
1561                 dpn_samplectrl = t_params->sample_interval - 1;
1562         else
1563                 dpn_samplectrl = cdns_readl(cdns, dpn_samplectrl_off_source);
1564         cdns_writel(cdns, dpn_samplectrl_off_target, dpn_samplectrl);
1565
1566         return 0;
1567 }
1568
1569 static int cdns_port_enable(struct sdw_bus *bus,
1570                             struct sdw_enable_ch *enable_ch, unsigned int bank)
1571 {
1572         struct sdw_cdns *cdns = bus_to_cdns(bus);
1573         int dpn_chnen_off, ch_mask;
1574
1575         if (bank)
1576                 dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1577         else
1578                 dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1579
1580         ch_mask = enable_ch->ch_mask * enable_ch->enable;
1581         cdns_writel(cdns, dpn_chnen_off, ch_mask);
1582
1583         return 0;
1584 }
1585
1586 static const struct sdw_master_port_ops cdns_port_ops = {
1587         .dpn_set_port_params = cdns_port_params,
1588         .dpn_set_port_transport_params = cdns_transport_params,
1589         .dpn_port_enable_ch = cdns_port_enable,
1590 };
1591
1592 /**
1593  * sdw_cdns_is_clock_stop: Check clock status
1594  *
1595  * @cdns: Cadence instance
1596  */
1597 bool sdw_cdns_is_clock_stop(struct sdw_cdns *cdns)
1598 {
1599         return !!(cdns_readl(cdns, CDNS_MCP_STAT) & CDNS_MCP_STAT_CLK_STOP);
1600 }
1601 EXPORT_SYMBOL(sdw_cdns_is_clock_stop);
1602
1603 /**
1604  * sdw_cdns_clock_stop: Cadence clock stop configuration routine
1605  *
1606  * @cdns: Cadence instance
1607  * @block_wake: prevent wakes if required by the platform
1608  */
1609 int sdw_cdns_clock_stop(struct sdw_cdns *cdns, bool block_wake)
1610 {
1611         bool slave_present = false;
1612         struct sdw_slave *slave;
1613         int ret;
1614
1615         sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1616
1617         /* Check suspend status */
1618         if (sdw_cdns_is_clock_stop(cdns)) {
1619                 dev_dbg(cdns->dev, "Clock is already stopped\n");
1620                 return 0;
1621         }
1622
1623         /*
1624          * Before entering clock stop we mask the Slave
1625          * interrupts. This helps avoid having to deal with e.g. a
1626          * Slave becoming UNATTACHED while the clock is being stopped
1627          */
1628         cdns_enable_slave_interrupts(cdns, false);
1629
1630         /*
1631          * For specific platforms, it is required to be able to put
1632          * master into a state in which it ignores wake-up trials
1633          * in clock stop state
1634          */
1635         if (block_wake)
1636                 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL,
1637                                 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP,
1638                                 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP);
1639
1640         list_for_each_entry(slave, &cdns->bus.slaves, node) {
1641                 if (slave->status == SDW_SLAVE_ATTACHED ||
1642                     slave->status == SDW_SLAVE_ALERT) {
1643                         slave_present = true;
1644                         break;
1645                 }
1646         }
1647
1648         /* commit changes */
1649         ret = cdns_config_update(cdns);
1650         if (ret < 0) {
1651                 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1652                 return ret;
1653         }
1654
1655         /* Prepare slaves for clock stop */
1656         if (slave_present) {
1657                 ret = sdw_bus_prep_clk_stop(&cdns->bus);
1658                 if (ret < 0 && ret != -ENODATA) {
1659                         dev_err(cdns->dev, "prepare clock stop failed %d\n", ret);
1660                         return ret;
1661                 }
1662         }
1663
1664         /*
1665          * Enter clock stop mode and only report errors if there are
1666          * Slave devices present (ALERT or ATTACHED)
1667          */
1668         ret = sdw_bus_clk_stop(&cdns->bus);
1669         if (ret < 0 && slave_present && ret != -ENODATA) {
1670                 dev_err(cdns->dev, "bus clock stop failed %d\n", ret);
1671                 return ret;
1672         }
1673
1674         ret = cdns_set_wait(cdns, CDNS_MCP_STAT,
1675                             CDNS_MCP_STAT_CLK_STOP,
1676                             CDNS_MCP_STAT_CLK_STOP);
1677         if (ret < 0)
1678                 dev_err(cdns->dev, "Clock stop failed %d\n", ret);
1679
1680         return ret;
1681 }
1682 EXPORT_SYMBOL(sdw_cdns_clock_stop);
1683
1684 /**
1685  * sdw_cdns_clock_restart: Cadence PM clock restart configuration routine
1686  *
1687  * @cdns: Cadence instance
1688  * @bus_reset: context may be lost while in low power modes and the bus
1689  * may require a Severe Reset and re-enumeration after a wake.
1690  */
1691 int sdw_cdns_clock_restart(struct sdw_cdns *cdns, bool bus_reset)
1692 {
1693         int ret;
1694
1695         /* unmask Slave interrupts that were masked when stopping the clock */
1696         cdns_enable_slave_interrupts(cdns, true);
1697
1698         ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
1699                              CDNS_MCP_CONTROL_CLK_STOP_CLR);
1700         if (ret < 0) {
1701                 dev_err(cdns->dev, "Couldn't exit from clock stop\n");
1702                 return ret;
1703         }
1704
1705         ret = cdns_set_wait(cdns, CDNS_MCP_STAT, CDNS_MCP_STAT_CLK_STOP, 0);
1706         if (ret < 0) {
1707                 dev_err(cdns->dev, "clock stop exit failed %d\n", ret);
1708                 return ret;
1709         }
1710
1711         cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL,
1712                         CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP, 0);
1713
1714         cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, CDNS_IP_MCP_CONTROL_CMD_ACCEPT,
1715                         CDNS_IP_MCP_CONTROL_CMD_ACCEPT);
1716
1717         if (!bus_reset) {
1718
1719                 /* enable bus operations with clock and data */
1720                 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONFIG,
1721                                 CDNS_IP_MCP_CONFIG_OP,
1722                                 CDNS_IP_MCP_CONFIG_OP_NORMAL);
1723
1724                 ret = cdns_config_update(cdns);
1725                 if (ret < 0) {
1726                         dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1727                         return ret;
1728                 }
1729
1730                 ret = sdw_bus_exit_clk_stop(&cdns->bus);
1731                 if (ret < 0)
1732                         dev_err(cdns->dev, "bus failed to exit clock stop %d\n", ret);
1733         }
1734
1735         return ret;
1736 }
1737 EXPORT_SYMBOL(sdw_cdns_clock_restart);
1738
1739 /**
1740  * sdw_cdns_probe() - Cadence probe routine
1741  * @cdns: Cadence instance
1742  */
1743 int sdw_cdns_probe(struct sdw_cdns *cdns)
1744 {
1745         init_completion(&cdns->tx_complete);
1746         cdns->bus.port_ops = &cdns_port_ops;
1747
1748         INIT_WORK(&cdns->work, cdns_update_slave_status_work);
1749         return 0;
1750 }
1751 EXPORT_SYMBOL(sdw_cdns_probe);
1752
1753 int cdns_set_sdw_stream(struct snd_soc_dai *dai,
1754                         void *stream, int direction)
1755 {
1756         struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1757         struct sdw_cdns_dai_runtime *dai_runtime;
1758
1759         dai_runtime = cdns->dai_runtime_array[dai->id];
1760
1761         if (stream) {
1762                 /* first paranoia check */
1763                 if (dai_runtime) {
1764                         dev_err(dai->dev,
1765                                 "dai_runtime already allocated for dai %s\n",
1766                                 dai->name);
1767                         return -EINVAL;
1768                 }
1769
1770                 /* allocate and set dai_runtime info */
1771                 dai_runtime = kzalloc(sizeof(*dai_runtime), GFP_KERNEL);
1772                 if (!dai_runtime)
1773                         return -ENOMEM;
1774
1775                 dai_runtime->stream_type = SDW_STREAM_PCM;
1776
1777                 dai_runtime->bus = &cdns->bus;
1778                 dai_runtime->link_id = cdns->instance;
1779
1780                 dai_runtime->stream = stream;
1781                 dai_runtime->direction = direction;
1782
1783                 cdns->dai_runtime_array[dai->id] = dai_runtime;
1784         } else {
1785                 /* second paranoia check */
1786                 if (!dai_runtime) {
1787                         dev_err(dai->dev,
1788                                 "dai_runtime not allocated for dai %s\n",
1789                                 dai->name);
1790                         return -EINVAL;
1791                 }
1792
1793                 /* for NULL stream we release allocated dai_runtime */
1794                 kfree(dai_runtime);
1795                 cdns->dai_runtime_array[dai->id] = NULL;
1796         }
1797         return 0;
1798 }
1799 EXPORT_SYMBOL(cdns_set_sdw_stream);
1800
1801 /**
1802  * cdns_find_pdi() - Find a free PDI
1803  *
1804  * @cdns: Cadence instance
1805  * @offset: Starting offset
1806  * @num: Number of PDIs
1807  * @pdi: PDI instances
1808  * @dai_id: DAI id
1809  *
1810  * Find a PDI for a given PDI array. The PDI num and dai_id are
1811  * expected to match, return NULL otherwise.
1812  */
1813 static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
1814                                           unsigned int offset,
1815                                           unsigned int num,
1816                                           struct sdw_cdns_pdi *pdi,
1817                                           int dai_id)
1818 {
1819         int i;
1820
1821         for (i = offset; i < offset + num; i++)
1822                 if (pdi[i].num == dai_id)
1823                         return &pdi[i];
1824
1825         return NULL;
1826 }
1827
1828 /**
1829  * sdw_cdns_config_stream: Configure a stream
1830  *
1831  * @cdns: Cadence instance
1832  * @ch: Channel count
1833  * @dir: Data direction
1834  * @pdi: PDI to be used
1835  */
1836 void sdw_cdns_config_stream(struct sdw_cdns *cdns,
1837                             u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
1838 {
1839         u32 offset, val = 0;
1840
1841         if (dir == SDW_DATA_DIR_RX) {
1842                 val = CDNS_PORTCTRL_DIRN;
1843
1844                 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1845                         val |= CDNS_PORTCTRL_TEST_FAILED;
1846         }
1847         offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
1848         cdns_updatel(cdns, offset,
1849                      CDNS_PORTCTRL_DIRN | CDNS_PORTCTRL_TEST_FAILED,
1850                      val);
1851
1852         val = pdi->num;
1853         val |= CDNS_PDI_CONFIG_SOFT_RESET;
1854         val |= FIELD_PREP(CDNS_PDI_CONFIG_CHANNEL, (1 << ch) - 1);
1855         cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1856 }
1857 EXPORT_SYMBOL(sdw_cdns_config_stream);
1858
1859 /**
1860  * sdw_cdns_alloc_pdi() - Allocate a PDI
1861  *
1862  * @cdns: Cadence instance
1863  * @stream: Stream to be allocated
1864  * @ch: Channel count
1865  * @dir: Data direction
1866  * @dai_id: DAI id
1867  */
1868 struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1869                                         struct sdw_cdns_streams *stream,
1870                                         u32 ch, u32 dir, int dai_id)
1871 {
1872         struct sdw_cdns_pdi *pdi = NULL;
1873
1874         if (dir == SDW_DATA_DIR_RX)
1875                 pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1876                                     dai_id);
1877         else
1878                 pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1879                                     dai_id);
1880
1881         /* check if we found a PDI, else find in bi-directional */
1882         if (!pdi)
1883                 pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd,
1884                                     dai_id);
1885
1886         if (pdi) {
1887                 pdi->l_ch_num = 0;
1888                 pdi->h_ch_num = ch - 1;
1889                 pdi->dir = dir;
1890                 pdi->ch_count = ch;
1891         }
1892
1893         return pdi;
1894 }
1895 EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
1896
1897 MODULE_LICENSE("Dual BSD/GPL");
1898 MODULE_DESCRIPTION("Cadence Soundwire Library");