soundwire: bus: add dev_warn() messages to track UNATTACHED devices
[linux-block.git] / drivers / soundwire / intel.c
CommitLineData
71bb8a1b
VK
1// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2// Copyright(c) 2015-17 Intel Corporation.
3
4/*
5 * Soundwire Intel Master Driver
6 */
7
8#include <linux/acpi.h>
79ee6631 9#include <linux/debugfs.h>
71bb8a1b 10#include <linux/delay.h>
4abbd783 11#include <linux/module.h>
71bb8a1b 12#include <linux/interrupt.h>
df72b719 13#include <linux/io.h>
29a269c6 14#include <linux/auxiliary_bus.h>
37a2d22b 15#include <sound/pcm_params.h>
ab2c9132 16#include <linux/pm_runtime.h>
37a2d22b 17#include <sound/soc.h>
71bb8a1b
VK
18#include <linux/soundwire/sdw_registers.h>
19#include <linux/soundwire/sdw.h>
20#include <linux/soundwire/sdw_intel.h>
21#include "cadence_master.h"
79ee6631 22#include "bus.h"
71bb8a1b
VK
23#include "intel.h"
24
ebf878ed 25#define INTEL_MASTER_SUSPEND_DELAY_MS 3000
ff560946 26#define INTEL_MASTER_RESET_ITERATIONS 10
ebf878ed
PLB
27
28/*
29 * debug/config flags for the Intel SoundWire Master.
30 *
31 * Since we may have multiple masters active, we can have up to 8
32 * flags reused in each byte, with master0 using the ls-byte, etc.
33 */
34
a2d9c161
PLB
35#define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME BIT(0)
36#define SDW_INTEL_MASTER_DISABLE_CLOCK_STOP BIT(1)
37#define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE BIT(2)
857a7c42 38#define SDW_INTEL_MASTER_DISABLE_MULTI_LINK BIT(3)
ebf878ed
PLB
39
40static int md_flags;
41module_param_named(sdw_md_flags, md_flags, int, 0444);
42MODULE_PARM_DESC(sdw_md_flags, "SoundWire Intel Master device flags (0x0 all off)");
43
c46302ec
VK
44enum intel_pdi_type {
45 INTEL_PDI_IN = 0,
46 INTEL_PDI_OUT = 1,
47 INTEL_PDI_BD = 2,
48};
49
71bb8a1b
VK
50#define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns)
51
52/*
53 * Read, write helpers for HW registers
54 */
55static inline int intel_readl(void __iomem *base, int offset)
56{
57 return readl(base + offset);
58}
59
60static inline void intel_writel(void __iomem *base, int offset, int value)
61{
62 writel(value, base + offset);
63}
64
65static inline u16 intel_readw(void __iomem *base, int offset)
66{
67 return readw(base + offset);
68}
69
70static inline void intel_writew(void __iomem *base, int offset, u16 value)
71{
72 writew(value, base + offset);
73}
74
7d2845d5 75static int intel_wait_bit(void __iomem *base, int offset, u32 mask, u32 target)
71bb8a1b
VK
76{
77 int timeout = 10;
78 u32 reg_read;
79
71bb8a1b
VK
80 do {
81 reg_read = readl(base + offset);
7d2845d5 82 if ((reg_read & mask) == target)
71bb8a1b
VK
83 return 0;
84
85 timeout--;
7d2845d5 86 usleep_range(50, 100);
71bb8a1b
VK
87 } while (timeout != 0);
88
89 return -EAGAIN;
90}
91
7d2845d5 92static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
71bb8a1b 93{
71bb8a1b 94 writel(value, base + offset);
7d2845d5
PLB
95 return intel_wait_bit(base, offset, mask, 0);
96}
71bb8a1b 97
7d2845d5
PLB
98static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
99{
100 writel(value, base + offset);
101 return intel_wait_bit(base, offset, mask, mask);
71bb8a1b
VK
102}
103
79ee6631
PLB
104/*
105 * debugfs
106 */
107#ifdef CONFIG_DEBUG_FS
108
109#define RD_BUF (2 * PAGE_SIZE)
110
111static ssize_t intel_sprintf(void __iomem *mem, bool l,
112 char *buf, size_t pos, unsigned int reg)
113{
114 int value;
115
116 if (l)
117 value = intel_readl(mem, reg);
118 else
119 value = intel_readw(mem, reg);
120
121 return scnprintf(buf + pos, RD_BUF - pos, "%4x\t%4x\n", reg, value);
122}
123
124static int intel_reg_show(struct seq_file *s_file, void *data)
125{
126 struct sdw_intel *sdw = s_file->private;
2523486b
PLB
127 void __iomem *s = sdw->link_res->shim;
128 void __iomem *a = sdw->link_res->alh;
79ee6631
PLB
129 char *buf;
130 ssize_t ret;
131 int i, j;
132 unsigned int links, reg;
133
134 buf = kzalloc(RD_BUF, GFP_KERNEL);
135 if (!buf)
136 return -ENOMEM;
137
138 links = intel_readl(s, SDW_SHIM_LCAP) & GENMASK(2, 0);
139
140 ret = scnprintf(buf, RD_BUF, "Register Value\n");
141 ret += scnprintf(buf + ret, RD_BUF - ret, "\nShim\n");
142
143 for (i = 0; i < links; i++) {
144 reg = SDW_SHIM_LCAP + i * 4;
145 ret += intel_sprintf(s, true, buf, ret, reg);
146 }
147
148 for (i = 0; i < links; i++) {
149 ret += scnprintf(buf + ret, RD_BUF - ret, "\nLink%d\n", i);
150 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLSCAP(i));
151 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS0CM(i));
152 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS1CM(i));
153 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS2CM(i));
154 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS3CM(i));
155 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PCMSCAP(i));
156
157 ret += scnprintf(buf + ret, RD_BUF - ret, "\n PCMSyCH registers\n");
158
159 /*
160 * the value 10 is the number of PDIs. We will need a
161 * cleanup to remove hard-coded Intel configurations
162 * from cadence_master.c
163 */
164 for (j = 0; j < 10; j++) {
165 ret += intel_sprintf(s, false, buf, ret,
166 SDW_SHIM_PCMSYCHM(i, j));
167 ret += intel_sprintf(s, false, buf, ret,
168 SDW_SHIM_PCMSYCHC(i, j));
169 }
170 ret += scnprintf(buf + ret, RD_BUF - ret, "\n PDMSCAP, IOCTL, CTMCTL\n");
171
172 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PDMSCAP(i));
173 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_IOCTL(i));
174 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTMCTL(i));
175 }
176
177 ret += scnprintf(buf + ret, RD_BUF - ret, "\nWake registers\n");
178 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKEEN);
179 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKESTS);
180
181 ret += scnprintf(buf + ret, RD_BUF - ret, "\nALH STRMzCFG\n");
182 for (i = 0; i < SDW_ALH_NUM_STREAMS; i++)
183 ret += intel_sprintf(a, true, buf, ret, SDW_ALH_STRMZCFG(i));
184
185 seq_printf(s_file, "%s", buf);
186 kfree(buf);
187
188 return 0;
189}
190DEFINE_SHOW_ATTRIBUTE(intel_reg);
191
0f9138e7
PLB
192static int intel_set_m_datamode(void *data, u64 value)
193{
194 struct sdw_intel *sdw = data;
195 struct sdw_bus *bus = &sdw->cdns.bus;
196
197 if (value > SDW_PORT_DATA_MODE_STATIC_1)
198 return -EINVAL;
199
200 /* Userspace changed the hardware state behind the kernel's back */
201 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
202
203 bus->params.m_data_mode = value;
204
205 return 0;
206}
207DEFINE_DEBUGFS_ATTRIBUTE(intel_set_m_datamode_fops, NULL,
208 intel_set_m_datamode, "%llu\n");
209
210static int intel_set_s_datamode(void *data, u64 value)
211{
212 struct sdw_intel *sdw = data;
213 struct sdw_bus *bus = &sdw->cdns.bus;
214
215 if (value > SDW_PORT_DATA_MODE_STATIC_1)
216 return -EINVAL;
217
218 /* Userspace changed the hardware state behind the kernel's back */
219 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
220
221 bus->params.s_data_mode = value;
222
223 return 0;
224}
225DEFINE_DEBUGFS_ATTRIBUTE(intel_set_s_datamode_fops, NULL,
226 intel_set_s_datamode, "%llu\n");
227
79ee6631
PLB
228static void intel_debugfs_init(struct sdw_intel *sdw)
229{
230 struct dentry *root = sdw->cdns.bus.debugfs;
231
232 if (!root)
233 return;
234
235 sdw->debugfs = debugfs_create_dir("intel-sdw", root);
236
237 debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw,
238 &intel_reg_fops);
239
0f9138e7
PLB
240 debugfs_create_file("intel-m-datamode", 0200, sdw->debugfs, sdw,
241 &intel_set_m_datamode_fops);
242
243 debugfs_create_file("intel-s-datamode", 0200, sdw->debugfs, sdw,
244 &intel_set_s_datamode_fops);
245
79ee6631
PLB
246 sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
247}
248
249static void intel_debugfs_exit(struct sdw_intel *sdw)
250{
251 debugfs_remove_recursive(sdw->debugfs);
252}
253#else
254static void intel_debugfs_init(struct sdw_intel *sdw) {}
255static void intel_debugfs_exit(struct sdw_intel *sdw) {}
256#endif /* CONFIG_DEBUG_FS */
257
71bb8a1b
VK
258/*
259 * shim ops
260 */
261
262static int intel_link_power_up(struct sdw_intel *sdw)
263{
264 unsigned int link_id = sdw->instance;
2523486b 265 void __iomem *shim = sdw->link_res->shim;
4a17c441
PLB
266 u32 *shim_mask = sdw->link_res->shim_mask;
267 struct sdw_bus *bus = &sdw->cdns.bus;
268 struct sdw_master_prop *prop = &bus->prop;
5ee74eb2
PLB
269 u32 spa_mask, cpa_mask;
270 u32 link_control;
4a17c441
PLB
271 int ret = 0;
272 u32 syncprd;
273 u32 sync_reg;
274
275 mutex_lock(sdw->link_res->shim_lock);
276
277 /*
278 * The hardware relies on an internal counter, typically 4kHz,
279 * to generate the SoundWire SSP - which defines a 'safe'
280 * synchronization point between commands and audio transport
281 * and allows for multi link synchronization. The SYNCPRD value
282 * is only dependent on the oscillator clock provided to
283 * the IP, so adjust based on _DSD properties reported in DSDT
284 * tables. The values reported are based on either 24MHz
285 * (CNL/CML) or 38.4 MHz (ICL/TGL+).
286 */
287 if (prop->mclk_freq % 6000000)
288 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
289 else
290 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24;
291
292 if (!*shim_mask) {
5ee74eb2
PLB
293 dev_dbg(sdw->cdns.dev, "%s: powering up all links\n", __func__);
294
4a17c441
PLB
295 /* we first need to program the SyncPRD/CPU registers */
296 dev_dbg(sdw->cdns.dev,
297 "%s: first link up, programming SYNCPRD\n", __func__);
298
299 /* set SyncPRD period */
300 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
f067c925 301 u32p_replace_bits(&sync_reg, syncprd, SDW_SHIM_SYNC_SYNCPRD);
4a17c441
PLB
302
303 /* Set SyncCPU bit */
304 sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
305 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
71bb8a1b 306
5ee74eb2
PLB
307 /* Link power up sequence */
308 link_control = intel_readl(shim, SDW_SHIM_LCTL);
71bb8a1b 309
5ee74eb2 310 /* only power-up enabled links */
3b4979ca
VK
311 spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, sdw->link_res->link_mask);
312 cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
5ee74eb2
PLB
313
314 link_control |= spa_mask;
315
316 ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
317 if (ret < 0) {
318 dev_err(sdw->cdns.dev, "Failed to power up link: %d\n", ret);
319 goto out;
320 }
4a17c441 321
4a17c441
PLB
322 /* SyncCPU will change once link is active */
323 ret = intel_wait_bit(shim, SDW_SHIM_SYNC,
324 SDW_SHIM_SYNC_SYNCCPU, 0);
325 if (ret < 0) {
326 dev_err(sdw->cdns.dev,
327 "Failed to set SHIM_SYNC: %d\n", ret);
328 goto out;
329 }
330 }
331
332 *shim_mask |= BIT(link_id);
71bb8a1b
VK
333
334 sdw->cdns.link_up = true;
4a17c441
PLB
335out:
336 mutex_unlock(sdw->link_res->shim_lock);
337
338 return ret;
71bb8a1b
VK
339}
340
4a17c441
PLB
341/* this needs to be called with shim_lock */
342static void intel_shim_glue_to_master_ip(struct sdw_intel *sdw)
71bb8a1b 343{
2523486b 344 void __iomem *shim = sdw->link_res->shim;
71bb8a1b 345 unsigned int link_id = sdw->instance;
4a17c441 346 u16 ioctl;
71bb8a1b 347
4a17c441
PLB
348 /* Switch to MIP from Glue logic */
349 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
350
351 ioctl &= ~(SDW_SHIM_IOCTL_DOE);
71bb8a1b 352 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
4a17c441 353 usleep_range(10, 15);
71bb8a1b 354
4a17c441 355 ioctl &= ~(SDW_SHIM_IOCTL_DO);
71bb8a1b 356 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
4a17c441 357 usleep_range(10, 15);
71bb8a1b 358
4a17c441 359 ioctl |= (SDW_SHIM_IOCTL_MIF);
71bb8a1b 360 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
4a17c441 361 usleep_range(10, 15);
71bb8a1b 362
4a17c441
PLB
363 ioctl &= ~(SDW_SHIM_IOCTL_BKE);
364 ioctl &= ~(SDW_SHIM_IOCTL_COE);
71bb8a1b 365 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
4a17c441 366 usleep_range(10, 15);
71bb8a1b 367
4a17c441
PLB
368 /* at this point Master IP has full control of the I/Os */
369}
71bb8a1b 370
4a17c441
PLB
371/* this needs to be called with shim_lock */
372static void intel_shim_master_ip_to_glue(struct sdw_intel *sdw)
373{
374 unsigned int link_id = sdw->instance;
375 void __iomem *shim = sdw->link_res->shim;
376 u16 ioctl;
377
378 /* Glue logic */
379 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
380 ioctl |= SDW_SHIM_IOCTL_BKE;
381 ioctl |= SDW_SHIM_IOCTL_COE;
71bb8a1b 382 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
4a17c441 383 usleep_range(10, 15);
71bb8a1b 384
4a17c441 385 ioctl &= ~(SDW_SHIM_IOCTL_MIF);
71bb8a1b 386 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
4a17c441 387 usleep_range(10, 15);
71bb8a1b 388
4a17c441
PLB
389 /* at this point Integration Glue has full control of the I/Os */
390}
391
392static int intel_shim_init(struct sdw_intel *sdw, bool clock_stop)
393{
394 void __iomem *shim = sdw->link_res->shim;
395 unsigned int link_id = sdw->instance;
396 int ret = 0;
397 u16 ioctl = 0, act = 0;
398
399 mutex_lock(sdw->link_res->shim_lock);
400
401 /* Initialize Shim */
402 ioctl |= SDW_SHIM_IOCTL_BKE;
71bb8a1b 403 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
4a17c441 404 usleep_range(10, 15);
71bb8a1b 405
4a17c441
PLB
406 ioctl |= SDW_SHIM_IOCTL_WPDD;
407 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
408 usleep_range(10, 15);
71bb8a1b 409
4a17c441 410 ioctl |= SDW_SHIM_IOCTL_DO;
71bb8a1b 411 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
4a17c441
PLB
412 usleep_range(10, 15);
413
414 ioctl |= SDW_SHIM_IOCTL_DOE;
71bb8a1b 415 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
4a17c441
PLB
416 usleep_range(10, 15);
417
418 intel_shim_glue_to_master_ip(sdw);
71bb8a1b 419
f067c925 420 u16p_replace_bits(&act, 0x1, SDW_SHIM_CTMCTL_DOAIS);
71bb8a1b
VK
421 act |= SDW_SHIM_CTMCTL_DACTQE;
422 act |= SDW_SHIM_CTMCTL_DODS;
423 intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
4a17c441 424 usleep_range(10, 15);
71bb8a1b 425
4a17c441
PLB
426 mutex_unlock(sdw->link_res->shim_lock);
427
428 return ret;
429}
430
ab2c9132 431static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
4a17c441
PLB
432{
433 void __iomem *shim = sdw->link_res->shim;
434 unsigned int link_id = sdw->instance;
435 u16 wake_en, wake_sts;
436
437 mutex_lock(sdw->link_res->shim_lock);
438 wake_en = intel_readw(shim, SDW_SHIM_WAKEEN);
439
440 if (wake_enable) {
441 /* Enable the wakeup */
442 wake_en |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
443 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
444 } else {
445 /* Disable the wake up interrupt */
446 wake_en &= ~(SDW_SHIM_WAKEEN_ENABLE << link_id);
447 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
448
449 /* Clear wake status */
450 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
451 wake_sts |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
452 intel_writew(shim, SDW_SHIM_WAKESTS_STATUS, wake_sts);
453 }
454 mutex_unlock(sdw->link_res->shim_lock);
455}
456
9b3b4b3f 457static int intel_link_power_down(struct sdw_intel *sdw)
4a17c441 458{
5ee74eb2 459 u32 link_control, spa_mask, cpa_mask;
4a17c441
PLB
460 unsigned int link_id = sdw->instance;
461 void __iomem *shim = sdw->link_res->shim;
462 u32 *shim_mask = sdw->link_res->shim_mask;
463 int ret = 0;
464
465 mutex_lock(sdw->link_res->shim_lock);
466
4a17c441
PLB
467 if (!(*shim_mask & BIT(link_id)))
468 dev_err(sdw->cdns.dev,
469 "%s: Unbalanced power-up/down calls\n", __func__);
470
ea6942da
PLB
471 sdw->cdns.link_up = false;
472
473 intel_shim_master_ip_to_glue(sdw);
474
4a17c441
PLB
475 *shim_mask &= ~BIT(link_id);
476
5ee74eb2
PLB
477 if (!*shim_mask) {
478
479 dev_dbg(sdw->cdns.dev, "%s: powering down all links\n", __func__);
480
481 /* Link power down sequence */
482 link_control = intel_readl(shim, SDW_SHIM_LCTL);
483
484 /* only power-down enabled links */
3b4979ca
VK
485 spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, ~sdw->link_res->link_mask);
486 cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
5ee74eb2
PLB
487
488 link_control &= spa_mask;
489
490 ret = intel_clear_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
ea6942da
PLB
491 if (ret < 0) {
492 dev_err(sdw->cdns.dev, "%s: could not power down link\n", __func__);
493
494 /*
495 * we leave the sdw->cdns.link_up flag as false since we've disabled
496 * the link at this point and cannot handle interrupts any longer.
497 */
498 }
5ee74eb2
PLB
499 }
500
4a17c441 501 mutex_unlock(sdw->link_res->shim_lock);
71bb8a1b 502
ea6942da 503 return ret;
71bb8a1b
VK
504}
505
02629e45
PLB
506static void intel_shim_sync_arm(struct sdw_intel *sdw)
507{
508 void __iomem *shim = sdw->link_res->shim;
509 u32 sync_reg;
510
511 mutex_lock(sdw->link_res->shim_lock);
512
513 /* update SYNC register */
514 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
515 sync_reg |= (SDW_SHIM_SYNC_CMDSYNC << sdw->instance);
516 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
517
518 mutex_unlock(sdw->link_res->shim_lock);
519}
520
437e3289
PLB
521static int intel_shim_sync_go_unlocked(struct sdw_intel *sdw)
522{
523 void __iomem *shim = sdw->link_res->shim;
524 u32 sync_reg;
525 int ret;
71bb8a1b 526
437e3289 527 /* Read SYNC register */
71bb8a1b 528 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
71bb8a1b 529
437e3289
PLB
530 /*
531 * Set SyncGO bit to synchronously trigger a bank switch for
532 * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
533 * the Masters.
534 */
535 sync_reg |= SDW_SHIM_SYNC_SYNCGO;
536
71bb8a1b 537 ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
437e3289
PLB
538 SDW_SHIM_SYNC_SYNCGO);
539
71bb8a1b 540 if (ret < 0)
437e3289 541 dev_err(sdw->cdns.dev, "SyncGO clear failed: %d\n", ret);
71bb8a1b
VK
542
543 return ret;
544}
545
857a7c42
PLB
546static int intel_shim_sync_go(struct sdw_intel *sdw)
547{
548 int ret;
549
550 mutex_lock(sdw->link_res->shim_lock);
551
552 ret = intel_shim_sync_go_unlocked(sdw);
553
554 mutex_unlock(sdw->link_res->shim_lock);
555
556 return ret;
557}
558
37a2d22b
VK
559/*
560 * PDI routines
561 */
562static void intel_pdi_init(struct sdw_intel *sdw,
d542bc9e 563 struct sdw_cdns_stream_config *config)
37a2d22b 564{
2523486b 565 void __iomem *shim = sdw->link_res->shim;
37a2d22b 566 unsigned int link_id = sdw->instance;
63a6aa96 567 int pcm_cap;
37a2d22b
VK
568
569 /* PCM Stream Capability */
570 pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
571
3b4979ca
VK
572 config->pcm_bd = FIELD_GET(SDW_SHIM_PCMSCAP_BSS, pcm_cap);
573 config->pcm_in = FIELD_GET(SDW_SHIM_PCMSCAP_ISS, pcm_cap);
574 config->pcm_out = FIELD_GET(SDW_SHIM_PCMSCAP_OSS, pcm_cap);
37a2d22b 575
121f4361
PLB
576 dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
577 config->pcm_bd, config->pcm_in, config->pcm_out);
37a2d22b
VK
578}
579
580static int
63a6aa96 581intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num)
37a2d22b 582{
2523486b 583 void __iomem *shim = sdw->link_res->shim;
37a2d22b
VK
584 unsigned int link_id = sdw->instance;
585 int count;
586
63a6aa96 587 count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
18046335 588
63a6aa96
PLB
589 /*
590 * WORKAROUND: on all existing Intel controllers, pdi
591 * number 2 reports channel count as 1 even though it
592 * supports 8 channels. Performing hardcoding for pdi
593 * number 2.
594 */
595 if (pdi_num == 2)
596 count = 7;
37a2d22b
VK
597
598 /* zero based values for channel count in register */
599 count++;
600
601 return count;
602}
603
604static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
d542bc9e
PLB
605 struct sdw_cdns_pdi *pdi,
606 unsigned int num_pdi,
63a6aa96 607 unsigned int *num_ch)
37a2d22b
VK
608{
609 int i, ch_count = 0;
610
611 for (i = 0; i < num_pdi; i++) {
63a6aa96 612 pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num);
37a2d22b
VK
613 ch_count += pdi->ch_count;
614 pdi++;
615 }
616
617 *num_ch = ch_count;
618 return 0;
619}
620
621static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
63a6aa96 622 struct sdw_cdns_streams *stream)
37a2d22b
VK
623{
624 intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
63a6aa96 625 &stream->num_ch_bd);
37a2d22b
VK
626
627 intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
63a6aa96 628 &stream->num_ch_in);
37a2d22b
VK
629
630 intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
63a6aa96 631 &stream->num_ch_out);
37a2d22b
VK
632
633 return 0;
634}
635
636static int intel_pdi_ch_update(struct sdw_intel *sdw)
637{
63a6aa96 638 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm);
37a2d22b
VK
639
640 return 0;
641}
642
643static void
644intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
645{
2523486b 646 void __iomem *shim = sdw->link_res->shim;
37a2d22b
VK
647 unsigned int link_id = sdw->instance;
648 int pdi_conf = 0;
649
c134f914
PLB
650 /* the Bulk and PCM streams are not contiguous */
651 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
652 if (pdi->num >= 2)
653 pdi->intel_alh_id += 2;
37a2d22b
VK
654
655 /*
656 * Program stream parameters to stream SHIM register
657 * This is applicable for PCM stream only.
658 */
659 if (pdi->type != SDW_STREAM_PCM)
660 return;
661
662 if (pdi->dir == SDW_DATA_DIR_RX)
663 pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
664 else
665 pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
666
f067c925
VK
667 u32p_replace_bits(&pdi_conf, pdi->intel_alh_id, SDW_SHIM_PCMSYCM_STREAM);
668 u32p_replace_bits(&pdi_conf, pdi->l_ch_num, SDW_SHIM_PCMSYCM_LCHN);
669 u32p_replace_bits(&pdi_conf, pdi->h_ch_num, SDW_SHIM_PCMSYCM_HCHN);
37a2d22b
VK
670
671 intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
672}
673
674static void
675intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
676{
2523486b 677 void __iomem *alh = sdw->link_res->alh;
37a2d22b
VK
678 unsigned int link_id = sdw->instance;
679 unsigned int conf;
680
c134f914
PLB
681 /* the Bulk and PCM streams are not contiguous */
682 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
683 if (pdi->num >= 2)
684 pdi->intel_alh_id += 2;
37a2d22b
VK
685
686 /* Program Stream config ALH register */
687 conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
688
f067c925
VK
689 u32p_replace_bits(&conf, SDW_ALH_STRMZCFG_DMAT_VAL, SDW_ALH_STRMZCFG_DMAT);
690 u32p_replace_bits(&conf, pdi->ch_count - 1, SDW_ALH_STRMZCFG_CHN);
37a2d22b
VK
691
692 intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
693}
694
4b206d34 695static int intel_params_stream(struct sdw_intel *sdw,
b86947b5 696 int stream,
d542bc9e 697 struct snd_soc_dai *dai,
4b206d34
RW
698 struct snd_pcm_hw_params *hw_params,
699 int link_id, int alh_stream_id)
c46302ec 700{
2523486b 701 struct sdw_intel_link_res *res = sdw->link_res;
4b206d34 702 struct sdw_intel_stream_params_data params_data;
05c8afe4 703
b86947b5 704 params_data.stream = stream; /* direction */
4b206d34
RW
705 params_data.dai = dai;
706 params_data.hw_params = hw_params;
707 params_data.link_id = link_id;
708 params_data.alh_stream_id = alh_stream_id;
c46302ec 709
4b206d34
RW
710 if (res->ops && res->ops->params_stream && res->dev)
711 return res->ops->params_stream(res->dev,
712 &params_data);
c46302ec
VK
713 return -EIO;
714}
715
eff346f2 716static int intel_free_stream(struct sdw_intel *sdw,
b86947b5 717 int stream,
eff346f2
PLB
718 struct snd_soc_dai *dai,
719 int link_id)
720{
721 struct sdw_intel_link_res *res = sdw->link_res;
722 struct sdw_intel_stream_free_data free_data;
723
b86947b5 724 free_data.stream = stream; /* direction */
eff346f2
PLB
725 free_data.dai = dai;
726 free_data.link_id = link_id;
727
728 if (res->ops && res->ops->free_stream && res->dev)
729 return res->ops->free_stream(res->dev,
730 &free_data);
731
732 return 0;
733}
734
30246e2d
SN
735/*
736 * bank switch routines
737 */
738
739static int intel_pre_bank_switch(struct sdw_bus *bus)
740{
741 struct sdw_cdns *cdns = bus_to_cdns(bus);
742 struct sdw_intel *sdw = cdns_to_intel(cdns);
30246e2d
SN
743
744 /* Write to register only for multi-link */
745 if (!bus->multi_link)
746 return 0;
747
02629e45 748 intel_shim_sync_arm(sdw);
30246e2d
SN
749
750 return 0;
751}
752
753static int intel_post_bank_switch(struct sdw_bus *bus)
754{
755 struct sdw_cdns *cdns = bus_to_cdns(bus);
756 struct sdw_intel *sdw = cdns_to_intel(cdns);
2523486b 757 void __iomem *shim = sdw->link_res->shim;
30246e2d
SN
758 int sync_reg, ret;
759
760 /* Write to register only for multi-link */
761 if (!bus->multi_link)
762 return 0;
763
4a17c441
PLB
764 mutex_lock(sdw->link_res->shim_lock);
765
30246e2d
SN
766 /* Read SYNC register */
767 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
768
769 /*
770 * post_bank_switch() ops is called from the bus in loop for
771 * all the Masters in the steam with the expectation that
772 * we trigger the bankswitch for the only first Master in the list
773 * and do nothing for the other Masters
774 *
775 * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master.
776 */
4a17c441
PLB
777 if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK)) {
778 ret = 0;
779 goto unlock;
780 }
30246e2d 781
437e3289 782 ret = intel_shim_sync_go_unlocked(sdw);
4a17c441
PLB
783unlock:
784 mutex_unlock(sdw->link_res->shim_lock);
30246e2d 785
30246e2d 786 if (ret < 0)
17ed5bef 787 dev_err(sdw->cdns.dev, "Post bank switch failed: %d\n", ret);
30246e2d
SN
788
789 return ret;
790}
791
c46302ec
VK
792/*
793 * DAI routines
794 */
795
5e7484d0
RW
796static int intel_startup(struct snd_pcm_substream *substream,
797 struct snd_soc_dai *dai)
798{
ebf878ed
PLB
799 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
800 int ret;
801
802 ret = pm_runtime_get_sync(cdns->dev);
803 if (ret < 0 && ret != -EACCES) {
804 dev_err_ratelimited(cdns->dev,
805 "pm_runtime_get_sync failed in %s, ret %d\n",
806 __func__, ret);
807 pm_runtime_put_noidle(cdns->dev);
808 return ret;
809 }
ff16d1e5 810 return 0;
5e7484d0
RW
811}
812
c46302ec 813static int intel_hw_params(struct snd_pcm_substream *substream,
d542bc9e
PLB
814 struct snd_pcm_hw_params *params,
815 struct snd_soc_dai *dai)
c46302ec
VK
816{
817 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
818 struct sdw_intel *sdw = cdns_to_intel(cdns);
819 struct sdw_cdns_dma_data *dma;
57a34790 820 struct sdw_cdns_pdi *pdi;
c46302ec
VK
821 struct sdw_stream_config sconfig;
822 struct sdw_port_config *pconfig;
57a34790
PLB
823 int ch, dir;
824 int ret;
c46302ec
VK
825
826 dma = snd_soc_dai_get_dma_data(dai, substream);
827 if (!dma)
828 return -EIO;
829
830 ch = params_channels(params);
831 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
832 dir = SDW_DATA_DIR_RX;
833 else
834 dir = SDW_DATA_DIR_TX;
835
63a6aa96 836 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
57a34790
PLB
837
838 if (!pdi) {
839 ret = -EINVAL;
840 goto error;
c46302ec
VK
841 }
842
57a34790
PLB
843 /* do run-time configurations for SHIM, ALH and PDI/PORT */
844 intel_pdi_shim_configure(sdw, pdi);
845 intel_pdi_alh_configure(sdw, pdi);
846 sdw_cdns_config_stream(cdns, ch, dir, pdi);
c46302ec 847
a5a0239c 848 /* store pdi and hw_params, may be needed in prepare step */
8ddeafb9 849 dma->paused = false;
a5a0239c
BL
850 dma->suspended = false;
851 dma->pdi = pdi;
852 dma->hw_params = params;
c46302ec
VK
853
854 /* Inform DSP about PDI stream number */
b86947b5 855 ret = intel_params_stream(sdw, substream->stream, dai, params,
4b206d34 856 sdw->instance,
57a34790
PLB
857 pdi->intel_alh_id);
858 if (ret)
859 goto error;
c46302ec
VK
860
861 sconfig.direction = dir;
862 sconfig.ch_count = ch;
863 sconfig.frame_rate = params_rate(params);
864 sconfig.type = dma->stream_type;
865
63a6aa96 866 sconfig.bps = snd_pcm_format_width(params_format(params));
c46302ec
VK
867
868 /* Port configuration */
235ae89b 869 pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL);
c46302ec
VK
870 if (!pconfig) {
871 ret = -ENOMEM;
57a34790 872 goto error;
c46302ec
VK
873 }
874
57a34790
PLB
875 pconfig->num = pdi->num;
876 pconfig->ch_mask = (1 << ch) - 1;
c46302ec
VK
877
878 ret = sdw_stream_add_master(&cdns->bus, &sconfig,
57a34790
PLB
879 pconfig, 1, dma->stream);
880 if (ret)
17ed5bef 881 dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
c46302ec 882
c46302ec 883 kfree(pconfig);
57a34790 884error:
c46302ec
VK
885 return ret;
886}
887
27b198f4
RW
888static int intel_prepare(struct snd_pcm_substream *substream,
889 struct snd_soc_dai *dai)
890{
a5a0239c
BL
891 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
892 struct sdw_intel *sdw = cdns_to_intel(cdns);
27b198f4 893 struct sdw_cdns_dma_data *dma;
a5a0239c 894 int ch, dir;
244eb888 895 int ret = 0;
27b198f4
RW
896
897 dma = snd_soc_dai_get_dma_data(dai, substream);
898 if (!dma) {
4e3ea93e 899 dev_err(dai->dev, "failed to get dma data in %s\n",
27b198f4
RW
900 __func__);
901 return -EIO;
902 }
903
a5a0239c
BL
904 if (dma->suspended) {
905 dma->suspended = false;
906
907 /*
908 * .prepare() is called after system resume, where we
909 * need to reinitialize the SHIM/ALH/Cadence IP.
910 * .prepare() is also called to deal with underflows,
911 * but in those cases we cannot touch ALH/SHIM
912 * registers
913 */
914
915 /* configure stream */
916 ch = params_channels(dma->hw_params);
917 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
918 dir = SDW_DATA_DIR_RX;
919 else
920 dir = SDW_DATA_DIR_TX;
921
922 intel_pdi_shim_configure(sdw, dma->pdi);
923 intel_pdi_alh_configure(sdw, dma->pdi);
924 sdw_cdns_config_stream(cdns, ch, dir, dma->pdi);
925
926 /* Inform DSP about PDI stream number */
b86947b5 927 ret = intel_params_stream(sdw, substream->stream, dai,
a5a0239c
BL
928 dma->hw_params,
929 sdw->instance,
930 dma->pdi->intel_alh_id);
a5a0239c
BL
931 }
932
a5a0239c 933 return ret;
27b198f4
RW
934}
935
c46302ec
VK
936static int
937intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
938{
939 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
eff346f2 940 struct sdw_intel *sdw = cdns_to_intel(cdns);
c46302ec
VK
941 struct sdw_cdns_dma_data *dma;
942 int ret;
943
944 dma = snd_soc_dai_get_dma_data(dai, substream);
945 if (!dma)
946 return -EIO;
947
244eb888
PLB
948 /*
949 * The sdw stream state will transition to RELEASED when stream->
950 * master_list is empty. So the stream state will transition to
951 * DEPREPARED for the first cpu-dai and to RELEASED for the last
952 * cpu-dai.
953 */
c46302ec 954 ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
eff346f2 955 if (ret < 0) {
17ed5bef 956 dev_err(dai->dev, "remove master from stream %s failed: %d\n",
d542bc9e 957 dma->stream->name, ret);
eff346f2
PLB
958 return ret;
959 }
c46302ec 960
b86947b5 961 ret = intel_free_stream(sdw, substream->stream, dai, sdw->instance);
eff346f2 962 if (ret < 0) {
4e3ea93e 963 dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
eff346f2
PLB
964 return ret;
965 }
966
a5a0239c
BL
967 dma->hw_params = NULL;
968 dma->pdi = NULL;
969
eff346f2 970 return 0;
c46302ec
VK
971}
972
183c7687
PLB
973static void intel_shutdown(struct snd_pcm_substream *substream,
974 struct snd_soc_dai *dai)
975{
ebf878ed 976 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
183c7687 977
ebf878ed
PLB
978 pm_runtime_mark_last_busy(cdns->dev);
979 pm_runtime_put_autosuspend(cdns->dev);
183c7687
PLB
980}
981
c46302ec 982static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
d542bc9e 983 void *stream, int direction)
c46302ec 984{
63a6aa96 985 return cdns_set_sdw_stream(dai, stream, direction);
c46302ec
VK
986}
987
09553140
PLB
988static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
989 int direction)
990{
991 struct sdw_cdns_dma_data *dma;
992
993 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
994 dma = dai->playback_dma_data;
995 else
996 dma = dai->capture_dma_data;
997
998 if (!dma)
06dcb4e4 999 return ERR_PTR(-EINVAL);
09553140
PLB
1000
1001 return dma->stream;
1002}
1003
8ddeafb9
RS
1004static int intel_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
1005{
1006 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1007 struct sdw_intel *sdw = cdns_to_intel(cdns);
1008 struct sdw_cdns_dma_data *dma;
1009 int ret = 0;
1010
1011 dma = snd_soc_dai_get_dma_data(dai, substream);
1012 if (!dma) {
1013 dev_err(dai->dev, "failed to get dma data in %s\n",
1014 __func__);
1015 return -EIO;
1016 }
1017
1018 switch (cmd) {
1019 case SNDRV_PCM_TRIGGER_SUSPEND:
1020
1021 /*
1022 * The .prepare callback is used to deal with xruns and resume operations.
1023 * In the case of xruns, the DMAs and SHIM registers cannot be touched,
1024 * but for resume operations the DMAs and SHIM registers need to be initialized.
1025 * the .trigger callback is used to track the suspend case only.
1026 */
1027
1028 dma->suspended = true;
1029
1030 ret = intel_free_stream(sdw, substream->stream, dai, sdw->instance);
1031 break;
1032
1033 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1034 dma->paused = true;
1035 break;
1036 case SNDRV_PCM_TRIGGER_STOP:
1037 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1038 dma->paused = false;
1039 break;
1040 default:
1041 break;
1042 }
1043
1044 return ret;
1045}
1046
1047static int intel_component_dais_suspend(struct snd_soc_component *component)
1048{
1049 struct snd_soc_dai *dai;
1050
1051 /*
1052 * In the corner case where a SUSPEND happens during a PAUSE, the ALSA core
1053 * does not throw the TRIGGER_SUSPEND. This leaves the DAIs in an unbalanced state.
1054 * Since the component suspend is called last, we can trap this corner case
1055 * and force the DAIs to release their resources.
1056 */
1057 for_each_component_dais(component, dai) {
1058 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1059 struct sdw_intel *sdw = cdns_to_intel(cdns);
1060 struct sdw_cdns_dma_data *dma;
1061 int stream;
1062 int ret;
1063
1064 dma = dai->playback_dma_data;
1065 stream = SNDRV_PCM_STREAM_PLAYBACK;
1066 if (!dma) {
1067 dma = dai->capture_dma_data;
1068 stream = SNDRV_PCM_STREAM_CAPTURE;
1069 }
1070
1071 if (!dma)
1072 continue;
1073
1074 if (dma->suspended)
1075 continue;
c46302ec 1076
8ddeafb9
RS
1077 if (dma->paused) {
1078 dma->suspended = true;
1079
1080 ret = intel_free_stream(sdw, stream, dai, sdw->instance);
1081 if (ret < 0)
1082 return ret;
1083 }
1084 }
1085
1086 return 0;
1087}
1088
b1635596 1089static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
5e7484d0 1090 .startup = intel_startup,
c46302ec 1091 .hw_params = intel_hw_params,
27b198f4 1092 .prepare = intel_prepare,
c46302ec 1093 .hw_free = intel_hw_free,
8ddeafb9 1094 .trigger = intel_trigger,
183c7687 1095 .shutdown = intel_shutdown,
e8444560
PLB
1096 .set_stream = intel_pcm_set_sdw_stream,
1097 .get_stream = intel_get_sdw_stream,
c46302ec
VK
1098};
1099
1100static const struct snd_soc_component_driver dai_component = {
1101 .name = "soundwire",
a5a0239c 1102 .suspend = intel_component_dais_suspend
c46302ec
VK
1103};
1104
1105static int intel_create_dai(struct sdw_cdns *cdns,
d542bc9e
PLB
1106 struct snd_soc_dai_driver *dais,
1107 enum intel_pdi_type type,
63a6aa96 1108 u32 num, u32 off, u32 max_ch)
c46302ec
VK
1109{
1110 int i;
1111
1112 if (num == 0)
1113 return 0;
1114
1115 /* TODO: Read supported rates/formats from hardware */
1116 for (i = off; i < (off + num); i++) {
bf6d6e68
PLB
1117 dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
1118 "SDW%d Pin%d",
1119 cdns->instance, i);
c46302ec
VK
1120 if (!dais[i].name)
1121 return -ENOMEM;
1122
1123 if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
c46302ec
VK
1124 dais[i].playback.channels_min = 1;
1125 dais[i].playback.channels_max = max_ch;
1126 dais[i].playback.rates = SNDRV_PCM_RATE_48000;
1127 dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
1128 }
1129
1130 if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
39194128
SK
1131 dais[i].capture.channels_min = 1;
1132 dais[i].capture.channels_max = max_ch;
c46302ec
VK
1133 dais[i].capture.rates = SNDRV_PCM_RATE_48000;
1134 dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
1135 }
1136
63a6aa96 1137 dais[i].ops = &intel_pcm_dai_ops;
c46302ec
VK
1138 }
1139
1140 return 0;
1141}
1142
1143static int intel_register_dai(struct sdw_intel *sdw)
1144{
1145 struct sdw_cdns *cdns = &sdw->cdns;
1146 struct sdw_cdns_streams *stream;
1147 struct snd_soc_dai_driver *dais;
1148 int num_dai, ret, off = 0;
1149
1150 /* DAIs are created based on total number of PDIs supported */
63a6aa96 1151 num_dai = cdns->pcm.num_pdi;
c46302ec
VK
1152
1153 dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
1154 if (!dais)
1155 return -ENOMEM;
1156
1157 /* Create PCM DAIs */
1158 stream = &cdns->pcm;
1159
cf924962 1160 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
63a6aa96 1161 off, stream->num_ch_in);
c46302ec
VK
1162 if (ret)
1163 return ret;
1164
1165 off += cdns->pcm.num_in;
1215daee 1166 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
63a6aa96 1167 off, stream->num_ch_out);
c46302ec
VK
1168 if (ret)
1169 return ret;
1170
1171 off += cdns->pcm.num_out;
1215daee 1172 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
63a6aa96 1173 off, stream->num_ch_bd);
c46302ec
VK
1174 if (ret)
1175 return ret;
1176
1177 return snd_soc_register_component(cdns->dev, &dai_component,
d542bc9e 1178 dais, num_dai);
c46302ec
VK
1179}
1180
085f4ace
PLB
1181static int sdw_master_read_intel_prop(struct sdw_bus *bus)
1182{
1183 struct sdw_master_prop *prop = &bus->prop;
1184 struct fwnode_handle *link;
1185 char name[32];
395713d8 1186 u32 quirk_mask;
085f4ace
PLB
1187
1188 /* Find master handle */
1189 snprintf(name, sizeof(name),
1190 "mipi-sdw-link-%d-subproperties", bus->link_id);
1191
1192 link = device_get_named_child_node(bus->dev, name);
1193 if (!link) {
1194 dev_err(bus->dev, "Master node %s not found\n", name);
1195 return -EIO;
1196 }
1197
1198 fwnode_property_read_u32(link,
1199 "intel-sdw-ip-clock",
1200 &prop->mclk_freq);
395713d8 1201
a19efb52
BL
1202 /* the values reported by BIOS are the 2x clock, not the bus clock */
1203 prop->mclk_freq /= 2;
1204
395713d8
PLB
1205 fwnode_property_read_u32(link,
1206 "intel-quirk-mask",
1207 &quirk_mask);
1208
1209 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
1210 prop->hw_disabled = true;
1211
bb877beb
BL
1212 prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
1213 SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
1214
085f4ace
PLB
1215 return 0;
1216}
1217
71bb8a1b
VK
1218static int intel_prop_read(struct sdw_bus *bus)
1219{
1220 /* Initialize with default handler to read all DisCo properties */
1221 sdw_master_read_prop(bus);
1222
085f4ace
PLB
1223 /* read Intel-specific properties */
1224 sdw_master_read_intel_prop(bus);
1225
71bb8a1b
VK
1226 return 0;
1227}
1228
c91605f4
SN
1229static struct sdw_master_ops sdw_intel_ops = {
1230 .read_prop = sdw_master_read_prop,
f6594cdf 1231 .override_adr = sdw_dmi_override_adr,
c91605f4
SN
1232 .xfer_msg = cdns_xfer_msg,
1233 .xfer_msg_defer = cdns_xfer_msg_defer,
1234 .reset_page_addr = cdns_reset_page_addr,
07abeff1 1235 .set_bus_conf = cdns_bus_conf,
30246e2d
SN
1236 .pre_bank_switch = intel_pre_bank_switch,
1237 .post_bank_switch = intel_post_bank_switch,
c91605f4
SN
1238};
1239
dfbe642d
PLB
1240static int intel_init(struct sdw_intel *sdw)
1241{
4a17c441
PLB
1242 bool clock_stop;
1243
dfbe642d
PLB
1244 /* Initialize shim and controller */
1245 intel_link_power_up(sdw);
4a17c441
PLB
1246
1247 clock_stop = sdw_cdns_is_clock_stop(&sdw->cdns);
1248
1249 intel_shim_init(sdw, clock_stop);
1250
857a7c42 1251 return 0;
dfbe642d
PLB
1252}
1253
71bb8a1b 1254/*
29a269c6 1255 * probe and init (aux_dev_id argument is required by function prototype but not used)
71bb8a1b 1256 */
29a269c6
PLB
1257static int intel_link_probe(struct auxiliary_device *auxdev,
1258 const struct auxiliary_device_id *aux_dev_id)
1259
71bb8a1b 1260{
29a269c6
PLB
1261 struct device *dev = &auxdev->dev;
1262 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
71bb8a1b 1263 struct sdw_intel *sdw;
83e129af 1264 struct sdw_cdns *cdns;
b6109dd6 1265 struct sdw_bus *bus;
71bb8a1b
VK
1266 int ret;
1267
b6109dd6 1268 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
71bb8a1b
VK
1269 if (!sdw)
1270 return -ENOMEM;
1271
83e129af
PLB
1272 cdns = &sdw->cdns;
1273 bus = &cdns->bus;
b6109dd6 1274
29a269c6
PLB
1275 sdw->instance = auxdev->id;
1276 sdw->link_res = &ldev->link_res;
83e129af
PLB
1277 cdns->dev = dev;
1278 cdns->registers = sdw->link_res->registers;
1279 cdns->instance = sdw->instance;
1280 cdns->msg_count = 0;
1281
29a269c6 1282 bus->link_id = auxdev->id;
71bb8a1b 1283
83e129af 1284 sdw_cdns_probe(cdns);
71bb8a1b
VK
1285
1286 /* Set property read ops */
c91605f4 1287 sdw_intel_ops.read_prop = intel_prop_read;
b6109dd6 1288 bus->ops = &sdw_intel_ops;
71bb8a1b 1289
b6109dd6 1290 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
3edac08e 1291 auxiliary_set_drvdata(auxdev, cdns);
71bb8a1b 1292
9026118f
BL
1293 /* use generic bandwidth allocation algorithm */
1294 sdw->cdns.bus.compute_params = sdw_compute_params;
1295
b6109dd6 1296 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
71bb8a1b 1297 if (ret) {
b6109dd6 1298 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
9e3d47fb 1299 return ret;
71bb8a1b
VK
1300 }
1301
6d2c6669 1302 if (bus->prop.hw_disabled)
b6109dd6
PLB
1303 dev_info(dev,
1304 "SoundWire master %d is disabled, will be ignored\n",
1305 bus->link_id);
0ef2986e
PLB
1306 /*
1307 * Ignore BIOS err_threshold, it's a really bad idea when dealing
1308 * with multiple hardware synchronized links
1309 */
1310 bus->prop.err_threshold = 0;
6d2c6669 1311
6d2c6669 1312 return 0;
6d2c6669
PLB
1313}
1314
29a269c6 1315int intel_link_startup(struct auxiliary_device *auxdev)
6d2c6669
PLB
1316{
1317 struct sdw_cdns_stream_config config;
29a269c6 1318 struct device *dev = &auxdev->dev;
3edac08e 1319 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
6d2c6669
PLB
1320 struct sdw_intel *sdw = cdns_to_intel(cdns);
1321 struct sdw_bus *bus = &cdns->bus;
ebf878ed 1322 int link_flags;
857a7c42 1323 bool multi_link;
caf68819 1324 u32 clock_stop_quirks;
6d2c6669
PLB
1325 int ret;
1326
1327 if (bus->prop.hw_disabled) {
1328 dev_info(dev,
1329 "SoundWire master %d is disabled, ignoring\n",
1330 sdw->instance);
395713d8
PLB
1331 return 0;
1332 }
1333
857a7c42
PLB
1334 link_flags = md_flags >> (bus->link_id * 8);
1335 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1336 if (!multi_link) {
1337 dev_dbg(dev, "Multi-link is disabled\n");
1338 bus->multi_link = false;
1339 } else {
94eed661
PLB
1340 /*
1341 * hardware-based synchronization is required regardless
1342 * of the number of segments used by a stream: SSP-based
1343 * synchronization is gated by gsync when the multi-master
1344 * mode is set.
1345 */
857a7c42 1346 bus->multi_link = true;
94eed661 1347 bus->hw_sync_min_links = 1;
857a7c42
PLB
1348 }
1349
1350 /* Initialize shim, controller */
dfbe642d 1351 ret = intel_init(sdw);
71bb8a1b
VK
1352 if (ret)
1353 goto err_init;
1354
37a2d22b
VK
1355 /* Read the PDI config and initialize cadence PDI */
1356 intel_pdi_init(sdw, &config);
83e129af 1357 ret = sdw_cdns_pdi_init(cdns, config);
71bb8a1b
VK
1358 if (ret)
1359 goto err_init;
1360
37a2d22b
VK
1361 intel_pdi_ch_update(sdw);
1362
83e129af 1363 ret = sdw_cdns_enable_interrupt(cdns, true);
71bb8a1b 1364 if (ret < 0) {
b6109dd6 1365 dev_err(dev, "cannot enable interrupts\n");
71bb8a1b
VK
1366 goto err_init;
1367 }
1368
857a7c42
PLB
1369 /*
1370 * follow recommended programming flows to avoid timeouts when
1371 * gsync is enabled
1372 */
1373 if (multi_link)
1374 intel_shim_sync_arm(sdw);
1375
1376 ret = sdw_cdns_init(cdns);
1377 if (ret < 0) {
1378 dev_err(dev, "unable to initialize Cadence IP\n");
1379 goto err_interrupt;
1380 }
1381
83e129af 1382 ret = sdw_cdns_exit_reset(cdns);
49ea07d3 1383 if (ret < 0) {
b6109dd6 1384 dev_err(dev, "unable to exit bus reset sequence\n");
9e3d47fb 1385 goto err_interrupt;
49ea07d3
PLB
1386 }
1387
857a7c42
PLB
1388 if (multi_link) {
1389 ret = intel_shim_sync_go(sdw);
1390 if (ret < 0) {
1391 dev_err(dev, "sync go failed: %d\n", ret);
1392 goto err_interrupt;
1393 }
1394 }
ff560946
PLB
1395 sdw_cdns_check_self_clearing_bits(cdns, __func__,
1396 true, INTEL_MASTER_RESET_ITERATIONS);
857a7c42 1397
c46302ec
VK
1398 /* Register DAIs */
1399 ret = intel_register_dai(sdw);
1400 if (ret) {
b6109dd6
PLB
1401 dev_err(dev, "DAI registration failed: %d\n", ret);
1402 snd_soc_unregister_component(dev);
9e3d47fb 1403 goto err_interrupt;
c46302ec
VK
1404 }
1405
79ee6631
PLB
1406 intel_debugfs_init(sdw);
1407
ebf878ed 1408 /* Enable runtime PM */
ebf878ed
PLB
1409 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
1410 pm_runtime_set_autosuspend_delay(dev,
1411 INTEL_MASTER_SUSPEND_DELAY_MS);
1412 pm_runtime_use_autosuspend(dev);
1413 pm_runtime_mark_last_busy(dev);
1414
1415 pm_runtime_set_active(dev);
1416 pm_runtime_enable(dev);
1417 }
1418
caf68819
PLB
1419 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1420 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
1421 /*
1422 * To keep the clock running we need to prevent
1423 * pm_runtime suspend from happening by increasing the
1424 * reference count.
1425 * This quirk is specified by the parent PCI device in
1426 * case of specific latency requirements. It will have
1427 * no effect if pm_runtime is disabled by the user via
1428 * a module parameter for testing purposes.
1429 */
1430 pm_runtime_get_noresume(dev);
1431 }
1432
a2d9c161
PLB
1433 /*
1434 * The runtime PM status of Slave devices is "Unsupported"
1435 * until they report as ATTACHED. If they don't, e.g. because
1436 * there are no Slave devices populated or if the power-on is
1437 * delayed or dependent on a power switch, the Master will
1438 * remain active and prevent its parent from suspending.
1439 *
1440 * Conditionally force the pm_runtime core to re-evaluate the
1441 * Master status in the absence of any Slave activity. A quirk
1442 * is provided to e.g. deal with Slaves that may be powered on
1443 * with a delay. A more complete solution would require the
1444 * definition of Master properties.
1445 */
1446 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1447 pm_runtime_idle(dev);
1448
e4401abb 1449 sdw->startup_done = true;
71bb8a1b
VK
1450 return 0;
1451
9e3d47fb 1452err_interrupt:
83e129af 1453 sdw_cdns_enable_interrupt(cdns, false);
71bb8a1b 1454err_init:
71bb8a1b
VK
1455 return ret;
1456}
1457
29a269c6 1458static void intel_link_remove(struct auxiliary_device *auxdev)
71bb8a1b 1459{
29a269c6 1460 struct device *dev = &auxdev->dev;
3edac08e 1461 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
83e129af
PLB
1462 struct sdw_intel *sdw = cdns_to_intel(cdns);
1463 struct sdw_bus *bus = &cdns->bus;
b6109dd6 1464
caf68819
PLB
1465 /*
1466 * Since pm_runtime is already disabled, we don't decrease
1467 * the refcount when the clock_stop_quirk is
1468 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
1469 */
b6109dd6 1470 if (!bus->prop.hw_disabled) {
395713d8 1471 intel_debugfs_exit(sdw);
83e129af 1472 sdw_cdns_enable_interrupt(cdns, false);
b6109dd6 1473 snd_soc_unregister_component(dev);
395713d8 1474 }
b6109dd6 1475 sdw_bus_master_delete(bus);
71bb8a1b
VK
1476}
1477
29a269c6 1478int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
ab2c9132 1479{
29a269c6 1480 struct device *dev = &auxdev->dev;
71bb8a1b 1481 struct sdw_intel *sdw;
ab2c9132
RW
1482 struct sdw_bus *bus;
1483 void __iomem *shim;
1484 u16 wake_sts;
71bb8a1b 1485
3edac08e 1486 sdw = auxiliary_get_drvdata(auxdev);
ab2c9132 1487 bus = &sdw->cdns.bus;
71bb8a1b 1488
e4401abb
PLB
1489 if (bus->prop.hw_disabled || !sdw->startup_done) {
1490 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1491 bus->link_id);
ab2c9132 1492 return 0;
395713d8 1493 }
ab2c9132
RW
1494
1495 shim = sdw->link_res->shim;
1496 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
1497
1498 if (!(wake_sts & BIT(sdw->instance)))
1499 return 0;
1500
1501 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
1502 intel_shim_wake(sdw, false);
1503
1504 /*
1505 * resume the Master, which will generate a bus reset and result in
1506 * Slaves re-attaching and be re-enumerated. The SoundWire physical
1507 * device which generated the wake will trigger an interrupt, which
1508 * will in turn cause the corresponding Linux Slave device to be
1509 * resumed and the Slave codec driver to check the status.
1510 */
1511 pm_request_resume(dev);
71bb8a1b
VK
1512
1513 return 0;
1514}
1515
9b3b4b3f
PLB
1516/*
1517 * PM calls
1518 */
1519
029bfd1c
PLB
1520static int intel_resume_child_device(struct device *dev, void *data)
1521{
1522 int ret;
1523 struct sdw_slave *slave = dev_to_sdw_dev(dev);
1524
1525 if (!slave->probed) {
1526 dev_dbg(dev, "%s: skipping device, no probed driver\n", __func__);
1527 return 0;
1528 }
1529 if (!slave->dev_num_sticky) {
1530 dev_dbg(dev, "%s: skipping device, never detected on bus\n", __func__);
1531 return 0;
1532 }
1533
1534 ret = pm_request_resume(dev);
1535 if (ret < 0)
1536 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
1537
1538 return ret;
1539}
1540
1541static int __maybe_unused intel_pm_prepare(struct device *dev)
1542{
1543 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1544 struct sdw_intel *sdw = cdns_to_intel(cdns);
1545 struct sdw_bus *bus = &cdns->bus;
1546 u32 clock_stop_quirks;
9283b6f9 1547 int ret;
029bfd1c
PLB
1548
1549 if (bus->prop.hw_disabled || !sdw->startup_done) {
1550 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1551 bus->link_id);
1552 return 0;
1553 }
1554
1555 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1556
1557 if (pm_runtime_suspended(dev) &&
1558 pm_runtime_suspended(dev->parent) &&
1559 ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
1560 !clock_stop_quirks)) {
1561 /*
1562 * if we've enabled clock stop, and the parent is suspended, the SHIM registers
1563 * are not accessible and the shim wake cannot be disabled.
1564 * The only solution is to resume the entire bus to full power
1565 */
1566
1567 /*
1568 * If any operation in this block fails, we keep going since we don't want
1569 * to prevent system suspend from happening and errors should be recoverable
1570 * on resume.
1571 */
1572
1573 /*
1574 * first resume the device for this link. This will also by construction
1575 * resume the PCI parent device.
1576 */
1577 ret = pm_request_resume(dev);
1578 if (ret < 0) {
1579 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
1580 return 0;
1581 }
1582
1583 /*
1584 * Continue resuming the entire bus (parent + child devices) to exit
1585 * the clock stop mode. If there are no devices connected on this link
1586 * this is a no-op.
1587 * The resume to full power could have been implemented with a .prepare
1588 * step in SoundWire codec drivers. This would however require a lot
1589 * of code to handle an Intel-specific corner case. It is simpler in
1590 * practice to add a loop at the link level.
1591 */
1592 ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
1593
1594 if (ret < 0)
1595 dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
1596 }
1597
1598 return 0;
1599}
1600
f046b233 1601static int __maybe_unused intel_suspend(struct device *dev)
9b3b4b3f
PLB
1602{
1603 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1604 struct sdw_intel *sdw = cdns_to_intel(cdns);
1605 struct sdw_bus *bus = &cdns->bus;
e4be9fac 1606 u32 clock_stop_quirks;
9b3b4b3f
PLB
1607 int ret;
1608
e4401abb
PLB
1609 if (bus->prop.hw_disabled || !sdw->startup_done) {
1610 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
9b3b4b3f
PLB
1611 bus->link_id);
1612 return 0;
1613 }
1614
b61b8b37
PLB
1615 if (pm_runtime_suspended(dev)) {
1616 dev_dbg(dev, "%s: pm_runtime status: suspended\n", __func__);
1617
e4be9fac
PLB
1618 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1619
029bfd1c
PLB
1620 if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
1621 !clock_stop_quirks) {
1622
1623 if (pm_runtime_suspended(dev->parent)) {
1624 /*
1625 * paranoia check: this should not happen with the .prepare
1626 * resume to full power
1627 */
1628 dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
1629 } else {
1630 intel_shim_wake(sdw, false);
1631 }
e4be9fac
PLB
1632 }
1633
b61b8b37
PLB
1634 return 0;
1635 }
1636
9b3b4b3f
PLB
1637 ret = sdw_cdns_enable_interrupt(cdns, false);
1638 if (ret < 0) {
1639 dev_err(dev, "cannot disable interrupts on suspend\n");
1640 return ret;
1641 }
1642
1643 ret = intel_link_power_down(sdw);
1644 if (ret) {
4e3ea93e 1645 dev_err(dev, "Link power down failed: %d\n", ret);
9b3b4b3f
PLB
1646 return ret;
1647 }
1648
1649 intel_shim_wake(sdw, false);
1650
1651 return 0;
1652}
1653
17e0da0b 1654static int __maybe_unused intel_suspend_runtime(struct device *dev)
ebf878ed
PLB
1655{
1656 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1657 struct sdw_intel *sdw = cdns_to_intel(cdns);
1658 struct sdw_bus *bus = &cdns->bus;
a320f41e 1659 u32 clock_stop_quirks;
ebf878ed
PLB
1660 int ret;
1661
e4401abb
PLB
1662 if (bus->prop.hw_disabled || !sdw->startup_done) {
1663 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
ebf878ed
PLB
1664 bus->link_id);
1665 return 0;
1666 }
1667
a320f41e 1668 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
ebf878ed 1669
a320f41e 1670 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
ebf878ed 1671
a320f41e
PLB
1672 ret = sdw_cdns_enable_interrupt(cdns, false);
1673 if (ret < 0) {
1674 dev_err(dev, "cannot disable interrupts on suspend\n");
1675 return ret;
1676 }
ebf878ed 1677
a320f41e
PLB
1678 ret = intel_link_power_down(sdw);
1679 if (ret) {
4e3ea93e 1680 dev_err(dev, "Link power down failed: %d\n", ret);
a320f41e
PLB
1681 return ret;
1682 }
1683
1684 intel_shim_wake(sdw, false);
1685
61fb830b
PLB
1686 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET ||
1687 !clock_stop_quirks) {
ee3db942
BL
1688 bool wake_enable = true;
1689
6626a616
RW
1690 ret = sdw_cdns_clock_stop(cdns, true);
1691 if (ret < 0) {
1692 dev_err(dev, "cannot enable clock stop on suspend\n");
ee3db942 1693 wake_enable = false;
6626a616
RW
1694 }
1695
1696 ret = sdw_cdns_enable_interrupt(cdns, false);
1697 if (ret < 0) {
1698 dev_err(dev, "cannot disable interrupts on suspend\n");
1699 return ret;
1700 }
1701
1702 ret = intel_link_power_down(sdw);
1703 if (ret) {
4e3ea93e 1704 dev_err(dev, "Link power down failed: %d\n", ret);
6626a616
RW
1705 return ret;
1706 }
1707
ee3db942 1708 intel_shim_wake(sdw, wake_enable);
a320f41e
PLB
1709 } else {
1710 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
1711 __func__, clock_stop_quirks);
1712 ret = -EINVAL;
1713 }
1714
1715 return ret;
ebf878ed
PLB
1716}
1717
f046b233 1718static int __maybe_unused intel_resume(struct device *dev)
9b3b4b3f
PLB
1719{
1720 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1721 struct sdw_intel *sdw = cdns_to_intel(cdns);
1722 struct sdw_bus *bus = &cdns->bus;
a2d9c161 1723 int link_flags;
857a7c42 1724 bool multi_link;
9b3b4b3f
PLB
1725 int ret;
1726
e4401abb
PLB
1727 if (bus->prop.hw_disabled || !sdw->startup_done) {
1728 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
9b3b4b3f
PLB
1729 bus->link_id);
1730 return 0;
1731 }
1732
857a7c42
PLB
1733 link_flags = md_flags >> (bus->link_id * 8);
1734 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1735
b61b8b37
PLB
1736 if (pm_runtime_suspended(dev)) {
1737 dev_dbg(dev, "%s: pm_runtime status was suspended, forcing active\n", __func__);
1738
1739 /* follow required sequence from runtime_pm.rst */
1740 pm_runtime_disable(dev);
1741 pm_runtime_set_active(dev);
1742 pm_runtime_mark_last_busy(dev);
1743 pm_runtime_enable(dev);
a2d9c161
PLB
1744
1745 link_flags = md_flags >> (bus->link_id * 8);
857a7c42 1746
a2d9c161
PLB
1747 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1748 pm_runtime_idle(dev);
b61b8b37
PLB
1749 }
1750
9b3b4b3f
PLB
1751 ret = intel_init(sdw);
1752 if (ret) {
4e3ea93e 1753 dev_err(dev, "%s failed: %d\n", __func__, ret);
9b3b4b3f
PLB
1754 return ret;
1755 }
1756
99b6a30f
PLB
1757 /*
1758 * make sure all Slaves are tagged as UNATTACHED and provide
1759 * reason for reinitialization
1760 */
1761 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
1762
9b3b4b3f
PLB
1763 ret = sdw_cdns_enable_interrupt(cdns, true);
1764 if (ret < 0) {
1765 dev_err(dev, "cannot enable interrupts during resume\n");
1766 return ret;
1767 }
1768
857a7c42
PLB
1769 /*
1770 * follow recommended programming flows to avoid timeouts when
1771 * gsync is enabled
1772 */
1773 if (multi_link)
1774 intel_shim_sync_arm(sdw);
1775
1776 ret = sdw_cdns_init(&sdw->cdns);
1777 if (ret < 0) {
1778 dev_err(dev, "unable to initialize Cadence IP during resume\n");
1779 return ret;
1780 }
1781
9b3b4b3f
PLB
1782 ret = sdw_cdns_exit_reset(cdns);
1783 if (ret < 0) {
1784 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1785 return ret;
1786 }
1787
857a7c42
PLB
1788 if (multi_link) {
1789 ret = intel_shim_sync_go(sdw);
1790 if (ret < 0) {
1791 dev_err(dev, "sync go failed during resume\n");
1792 return ret;
1793 }
1794 }
ff560946
PLB
1795 sdw_cdns_check_self_clearing_bits(cdns, __func__,
1796 true, INTEL_MASTER_RESET_ITERATIONS);
857a7c42 1797
cb1e6d59
PLB
1798 /*
1799 * after system resume, the pm_runtime suspend() may kick in
1800 * during the enumeration, before any children device force the
1801 * master device to remain active. Using pm_runtime_get()
1802 * routines is not really possible, since it'd prevent the
1803 * master from suspending.
1804 * A reasonable compromise is to update the pm_runtime
1805 * counters and delay the pm_runtime suspend by several
1806 * seconds, by when all enumeration should be complete.
1807 */
1808 pm_runtime_mark_last_busy(dev);
1809
9b3b4b3f
PLB
1810 return ret;
1811}
1812
17e0da0b 1813static int __maybe_unused intel_resume_runtime(struct device *dev)
ebf878ed
PLB
1814{
1815 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1816 struct sdw_intel *sdw = cdns_to_intel(cdns);
1817 struct sdw_bus *bus = &cdns->bus;
a320f41e 1818 u32 clock_stop_quirks;
08abad9f 1819 bool clock_stop0;
857a7c42
PLB
1820 int link_flags;
1821 bool multi_link;
08abad9f 1822 int status;
ebf878ed
PLB
1823 int ret;
1824
e4401abb
PLB
1825 if (bus->prop.hw_disabled || !sdw->startup_done) {
1826 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
ebf878ed
PLB
1827 bus->link_id);
1828 return 0;
1829 }
1830
857a7c42
PLB
1831 link_flags = md_flags >> (bus->link_id * 8);
1832 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1833
a320f41e 1834 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
ebf878ed 1835
a320f41e
PLB
1836 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1837 ret = intel_init(sdw);
1838 if (ret) {
4e3ea93e 1839 dev_err(dev, "%s failed: %d\n", __func__, ret);
a320f41e
PLB
1840 return ret;
1841 }
99b6a30f 1842
a320f41e
PLB
1843 /*
1844 * make sure all Slaves are tagged as UNATTACHED and provide
1845 * reason for reinitialization
1846 */
1847 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
ebf878ed 1848
a320f41e
PLB
1849 ret = sdw_cdns_enable_interrupt(cdns, true);
1850 if (ret < 0) {
1851 dev_err(dev, "cannot enable interrupts during resume\n");
1852 return ret;
1853 }
1854
857a7c42
PLB
1855 /*
1856 * follow recommended programming flows to avoid
1857 * timeouts when gsync is enabled
1858 */
1859 if (multi_link)
1860 intel_shim_sync_arm(sdw);
1861
1862 ret = sdw_cdns_init(&sdw->cdns);
1863 if (ret < 0) {
1864 dev_err(dev, "unable to initialize Cadence IP during resume\n");
1865 return ret;
1866 }
1867
a320f41e
PLB
1868 ret = sdw_cdns_exit_reset(cdns);
1869 if (ret < 0) {
1870 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1871 return ret;
1872 }
857a7c42
PLB
1873
1874 if (multi_link) {
1875 ret = intel_shim_sync_go(sdw);
1876 if (ret < 0) {
1877 dev_err(dev, "sync go failed during resume\n");
1878 return ret;
1879 }
1880 }
ff560946
PLB
1881 sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime TEARDOWN",
1882 true, INTEL_MASTER_RESET_ITERATIONS);
1883
6626a616
RW
1884 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
1885 ret = intel_init(sdw);
1886 if (ret) {
4e3ea93e 1887 dev_err(dev, "%s failed: %d\n", __func__, ret);
6626a616
RW
1888 return ret;
1889 }
1890
08abad9f
RW
1891 /*
1892 * An exception condition occurs for the CLK_STOP_BUS_RESET
1893 * case if one or more masters remain active. In this condition,
1894 * all the masters are powered on for they are in the same power
1895 * domain. Master can preserve its context for clock stop0, so
1896 * there is no need to clear slave status and reset bus.
1897 */
1898 clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1899
08abad9f 1900 if (!clock_stop0) {
857a7c42 1901
857a7c42
PLB
1902 /*
1903 * make sure all Slaves are tagged as UNATTACHED and
1904 * provide reason for reinitialization
1905 */
1906
08abad9f
RW
1907 status = SDW_UNATTACH_REQUEST_MASTER_RESET;
1908 sdw_clear_slave_status(bus, status);
08abad9f 1909
d78071b4
PLB
1910 ret = sdw_cdns_enable_interrupt(cdns, true);
1911 if (ret < 0) {
1912 dev_err(dev, "cannot enable interrupts during resume\n");
1913 return ret;
1914 }
1915
1916 /*
1917 * follow recommended programming flows to avoid
1918 * timeouts when gsync is enabled
1919 */
1920 if (multi_link)
1921 intel_shim_sync_arm(sdw);
6626a616 1922
d78071b4
PLB
1923 /*
1924 * Re-initialize the IP since it was powered-off
1925 */
1926 sdw_cdns_init(&sdw->cdns);
1927
1928 } else {
1929 ret = sdw_cdns_enable_interrupt(cdns, true);
1930 if (ret < 0) {
1931 dev_err(dev, "cannot enable interrupts during resume\n");
1932 return ret;
1933 }
6626a616
RW
1934 }
1935
08abad9f 1936 ret = sdw_cdns_clock_restart(cdns, !clock_stop0);
6626a616
RW
1937 if (ret < 0) {
1938 dev_err(dev, "unable to restart clock during resume\n");
1939 return ret;
1940 }
d78071b4
PLB
1941
1942 if (!clock_stop0) {
1943 ret = sdw_cdns_exit_reset(cdns);
1944 if (ret < 0) {
1945 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1946 return ret;
1947 }
1948
1949 if (multi_link) {
1950 ret = intel_shim_sync_go(sdw);
1951 if (ret < 0) {
1952 dev_err(sdw->cdns.dev, "sync go failed during resume\n");
1953 return ret;
1954 }
1955 }
1956 }
ff560946
PLB
1957 sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime BUS_RESET",
1958 true, INTEL_MASTER_RESET_ITERATIONS);
1959
61fb830b 1960 } else if (!clock_stop_quirks) {
f748f34e
PLB
1961
1962 clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1963 if (!clock_stop0)
1964 dev_err(dev, "%s invalid configuration, clock was not stopped", __func__);
1965
61fb830b
PLB
1966 ret = intel_init(sdw);
1967 if (ret) {
4e3ea93e 1968 dev_err(dev, "%s failed: %d\n", __func__, ret);
61fb830b
PLB
1969 return ret;
1970 }
1971
1972 ret = sdw_cdns_enable_interrupt(cdns, true);
1973 if (ret < 0) {
1974 dev_err(dev, "cannot enable interrupts during resume\n");
1975 return ret;
1976 }
1977
1978 ret = sdw_cdns_clock_restart(cdns, false);
1979 if (ret < 0) {
1980 dev_err(dev, "unable to resume master during resume\n");
1981 return ret;
1982 }
ff560946
PLB
1983
1984 sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime no_quirks",
1985 true, INTEL_MASTER_RESET_ITERATIONS);
a320f41e
PLB
1986 } else {
1987 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
1988 __func__, clock_stop_quirks);
1989 ret = -EINVAL;
ebf878ed
PLB
1990 }
1991
1992 return ret;
1993}
1994
9b3b4b3f 1995static const struct dev_pm_ops intel_pm = {
029bfd1c 1996 .prepare = intel_pm_prepare,
9b3b4b3f 1997 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
ebf878ed 1998 SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
9b3b4b3f
PLB
1999};
2000
29a269c6
PLB
2001static const struct auxiliary_device_id intel_link_id_table[] = {
2002 { .name = "soundwire_intel.link" },
2003 {},
2004};
2005MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
2006
2007static struct auxiliary_driver sdw_intel_drv = {
2008 .probe = intel_link_probe,
2009 .remove = intel_link_remove,
71bb8a1b 2010 .driver = {
29a269c6 2011 /* auxiliary_driver_register() sets .name to be the modname */
9b3b4b3f 2012 .pm = &intel_pm,
29a269c6
PLB
2013 },
2014 .id_table = intel_link_id_table
71bb8a1b 2015};
29a269c6 2016module_auxiliary_driver(sdw_intel_drv);
71bb8a1b
VK
2017
2018MODULE_LICENSE("Dual BSD/GPL");
29a269c6 2019MODULE_DESCRIPTION("Intel Soundwire Link Driver");