xref: /openbmc/linux/drivers/soundwire/intel.c (revision bb26cfd9)
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>
9 #include <linux/debugfs.h>
10 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/auxiliary_bus.h>
15 #include <sound/pcm_params.h>
16 #include <linux/pm_runtime.h>
17 #include <sound/soc.h>
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"
22 #include "bus.h"
23 #include "intel.h"
24 
25 #define INTEL_MASTER_SUSPEND_DELAY_MS	3000
26 #define INTEL_MASTER_RESET_ITERATIONS	10
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 
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)
38 #define SDW_INTEL_MASTER_DISABLE_MULTI_LINK		BIT(3)
39 
40 static int md_flags;
41 module_param_named(sdw_md_flags, md_flags, int, 0444);
42 MODULE_PARM_DESC(sdw_md_flags, "SoundWire Intel Master device flags (0x0 all off)");
43 
44 enum intel_pdi_type {
45 	INTEL_PDI_IN = 0,
46 	INTEL_PDI_OUT = 1,
47 	INTEL_PDI_BD = 2,
48 };
49 
50 #define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns)
51 
52 /*
53  * Read, write helpers for HW registers
54  */
55 static inline int intel_readl(void __iomem *base, int offset)
56 {
57 	return readl(base + offset);
58 }
59 
60 static inline void intel_writel(void __iomem *base, int offset, int value)
61 {
62 	writel(value, base + offset);
63 }
64 
65 static inline u16 intel_readw(void __iomem *base, int offset)
66 {
67 	return readw(base + offset);
68 }
69 
70 static inline void intel_writew(void __iomem *base, int offset, u16 value)
71 {
72 	writew(value, base + offset);
73 }
74 
75 static int intel_wait_bit(void __iomem *base, int offset, u32 mask, u32 target)
76 {
77 	int timeout = 10;
78 	u32 reg_read;
79 
80 	do {
81 		reg_read = readl(base + offset);
82 		if ((reg_read & mask) == target)
83 			return 0;
84 
85 		timeout--;
86 		usleep_range(50, 100);
87 	} while (timeout != 0);
88 
89 	return -EAGAIN;
90 }
91 
92 static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
93 {
94 	writel(value, base + offset);
95 	return intel_wait_bit(base, offset, mask, 0);
96 }
97 
98 static 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);
102 }
103 
104 /*
105  * debugfs
106  */
107 #ifdef CONFIG_DEBUG_FS
108 
109 #define RD_BUF (2 * PAGE_SIZE)
110 
111 static 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 
124 static int intel_reg_show(struct seq_file *s_file, void *data)
125 {
126 	struct sdw_intel *sdw = s_file->private;
127 	void __iomem *s = sdw->link_res->shim;
128 	void __iomem *a = sdw->link_res->alh;
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 }
190 DEFINE_SHOW_ATTRIBUTE(intel_reg);
191 
192 static 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 }
207 DEFINE_DEBUGFS_ATTRIBUTE(intel_set_m_datamode_fops, NULL,
208 			 intel_set_m_datamode, "%llu\n");
209 
210 static 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 }
225 DEFINE_DEBUGFS_ATTRIBUTE(intel_set_s_datamode_fops, NULL,
226 			 intel_set_s_datamode, "%llu\n");
227 
228 static 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 
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 
246 	sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
247 }
248 
249 static void intel_debugfs_exit(struct sdw_intel *sdw)
250 {
251 	debugfs_remove_recursive(sdw->debugfs);
252 }
253 #else
254 static void intel_debugfs_init(struct sdw_intel *sdw) {}
255 static void intel_debugfs_exit(struct sdw_intel *sdw) {}
256 #endif /* CONFIG_DEBUG_FS */
257 
258 /*
259  * shim ops
260  */
261 
262 static int intel_link_power_up(struct sdw_intel *sdw)
263 {
264 	unsigned int link_id = sdw->instance;
265 	void __iomem *shim = sdw->link_res->shim;
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;
269 	u32 spa_mask, cpa_mask;
270 	u32 link_control;
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) {
293 		dev_dbg(sdw->cdns.dev, "%s: powering up all links\n", __func__);
294 
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);
301 		u32p_replace_bits(&sync_reg, syncprd, SDW_SHIM_SYNC_SYNCPRD);
302 
303 		/* Set SyncCPU bit */
304 		sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
305 		intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
306 
307 		/* Link power up sequence */
308 		link_control = intel_readl(shim, SDW_SHIM_LCTL);
309 
310 		/* only power-up enabled links */
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);
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 		}
321 
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);
333 
334 	sdw->cdns.link_up = true;
335 out:
336 	mutex_unlock(sdw->link_res->shim_lock);
337 
338 	return ret;
339 }
340 
341 /* this needs to be called with shim_lock */
342 static void intel_shim_glue_to_master_ip(struct sdw_intel *sdw)
343 {
344 	void __iomem *shim = sdw->link_res->shim;
345 	unsigned int link_id = sdw->instance;
346 	u16 ioctl;
347 
348 	/* Switch to MIP from Glue logic */
349 	ioctl = intel_readw(shim,  SDW_SHIM_IOCTL(link_id));
350 
351 	ioctl &= ~(SDW_SHIM_IOCTL_DOE);
352 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
353 	usleep_range(10, 15);
354 
355 	ioctl &= ~(SDW_SHIM_IOCTL_DO);
356 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
357 	usleep_range(10, 15);
358 
359 	ioctl |= (SDW_SHIM_IOCTL_MIF);
360 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
361 	usleep_range(10, 15);
362 
363 	ioctl &= ~(SDW_SHIM_IOCTL_BKE);
364 	ioctl &= ~(SDW_SHIM_IOCTL_COE);
365 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
366 	usleep_range(10, 15);
367 
368 	/* at this point Master IP has full control of the I/Os */
369 }
370 
371 /* this needs to be called with shim_lock */
372 static 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;
382 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
383 	usleep_range(10, 15);
384 
385 	ioctl &= ~(SDW_SHIM_IOCTL_MIF);
386 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
387 	usleep_range(10, 15);
388 
389 	/* at this point Integration Glue has full control of the I/Os */
390 }
391 
392 static 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;
403 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
404 	usleep_range(10, 15);
405 
406 	ioctl |= SDW_SHIM_IOCTL_WPDD;
407 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
408 	usleep_range(10, 15);
409 
410 	ioctl |= SDW_SHIM_IOCTL_DO;
411 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
412 	usleep_range(10, 15);
413 
414 	ioctl |= SDW_SHIM_IOCTL_DOE;
415 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
416 	usleep_range(10, 15);
417 
418 	intel_shim_glue_to_master_ip(sdw);
419 
420 	u16p_replace_bits(&act, 0x1, SDW_SHIM_CTMCTL_DOAIS);
421 	act |= SDW_SHIM_CTMCTL_DACTQE;
422 	act |= SDW_SHIM_CTMCTL_DODS;
423 	intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
424 	usleep_range(10, 15);
425 
426 	mutex_unlock(sdw->link_res->shim_lock);
427 
428 	return ret;
429 }
430 
431 static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
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_WAKESTS_STATUS << link_id);
452 		intel_writew(shim, SDW_SHIM_WAKESTS, wake_sts);
453 	}
454 	mutex_unlock(sdw->link_res->shim_lock);
455 }
456 
457 static int intel_link_power_down(struct sdw_intel *sdw)
458 {
459 	u32 link_control, spa_mask, cpa_mask;
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 
467 	if (!(*shim_mask & BIT(link_id)))
468 		dev_err(sdw->cdns.dev,
469 			"%s: Unbalanced power-up/down calls\n", __func__);
470 
471 	sdw->cdns.link_up = false;
472 
473 	intel_shim_master_ip_to_glue(sdw);
474 
475 	*shim_mask &= ~BIT(link_id);
476 
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 */
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);
487 
488 		link_control &=  spa_mask;
489 
490 		ret = intel_clear_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
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 		}
499 	}
500 
501 	mutex_unlock(sdw->link_res->shim_lock);
502 
503 	return ret;
504 }
505 
506 static 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 
521 static 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;
526 
527 	/* Read SYNC register */
528 	sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
529 
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 
537 	ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
538 			      SDW_SHIM_SYNC_SYNCGO);
539 
540 	if (ret < 0)
541 		dev_err(sdw->cdns.dev, "SyncGO clear failed: %d\n", ret);
542 
543 	return ret;
544 }
545 
546 static 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 
559 /*
560  * PDI routines
561  */
562 static void intel_pdi_init(struct sdw_intel *sdw,
563 			   struct sdw_cdns_stream_config *config)
564 {
565 	void __iomem *shim = sdw->link_res->shim;
566 	unsigned int link_id = sdw->instance;
567 	int pcm_cap;
568 
569 	/* PCM Stream Capability */
570 	pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
571 
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);
575 
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);
578 }
579 
580 static int
581 intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num)
582 {
583 	void __iomem *shim = sdw->link_res->shim;
584 	unsigned int link_id = sdw->instance;
585 	int count;
586 
587 	count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
588 
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;
597 
598 	/* zero based values for channel count in register */
599 	count++;
600 
601 	return count;
602 }
603 
604 static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
605 				   struct sdw_cdns_pdi *pdi,
606 				   unsigned int num_pdi,
607 				   unsigned int *num_ch)
608 {
609 	int i, ch_count = 0;
610 
611 	for (i = 0; i < num_pdi; i++) {
612 		pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num);
613 		ch_count += pdi->ch_count;
614 		pdi++;
615 	}
616 
617 	*num_ch = ch_count;
618 	return 0;
619 }
620 
621 static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
622 				      struct sdw_cdns_streams *stream)
623 {
624 	intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
625 				&stream->num_ch_bd);
626 
627 	intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
628 				&stream->num_ch_in);
629 
630 	intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
631 				&stream->num_ch_out);
632 
633 	return 0;
634 }
635 
636 static int intel_pdi_ch_update(struct sdw_intel *sdw)
637 {
638 	intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm);
639 
640 	return 0;
641 }
642 
643 static void
644 intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
645 {
646 	void __iomem *shim = sdw->link_res->shim;
647 	unsigned int link_id = sdw->instance;
648 	int pdi_conf = 0;
649 
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;
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 
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);
670 
671 	intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
672 }
673 
674 static void
675 intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
676 {
677 	void __iomem *alh = sdw->link_res->alh;
678 	unsigned int link_id = sdw->instance;
679 	unsigned int conf;
680 
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;
685 
686 	/* Program Stream config ALH register */
687 	conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
688 
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);
691 
692 	intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
693 }
694 
695 static int intel_params_stream(struct sdw_intel *sdw,
696 			       int stream,
697 			       struct snd_soc_dai *dai,
698 			       struct snd_pcm_hw_params *hw_params,
699 			       int link_id, int alh_stream_id)
700 {
701 	struct sdw_intel_link_res *res = sdw->link_res;
702 	struct sdw_intel_stream_params_data params_data;
703 
704 	params_data.stream = stream; /* direction */
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;
709 
710 	if (res->ops && res->ops->params_stream && res->dev)
711 		return res->ops->params_stream(res->dev,
712 					       &params_data);
713 	return -EIO;
714 }
715 
716 static int intel_free_stream(struct sdw_intel *sdw,
717 			     int stream,
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 
724 	free_data.stream = stream; /* direction */
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 
735 /*
736  * bank switch routines
737  */
738 
739 static 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);
743 
744 	/* Write to register only for multi-link */
745 	if (!bus->multi_link)
746 		return 0;
747 
748 	intel_shim_sync_arm(sdw);
749 
750 	return 0;
751 }
752 
753 static 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);
757 	void __iomem *shim = sdw->link_res->shim;
758 	int sync_reg, ret;
759 
760 	/* Write to register only for multi-link */
761 	if (!bus->multi_link)
762 		return 0;
763 
764 	mutex_lock(sdw->link_res->shim_lock);
765 
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 	 */
777 	if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK)) {
778 		ret = 0;
779 		goto unlock;
780 	}
781 
782 	ret = intel_shim_sync_go_unlocked(sdw);
783 unlock:
784 	mutex_unlock(sdw->link_res->shim_lock);
785 
786 	if (ret < 0)
787 		dev_err(sdw->cdns.dev, "Post bank switch failed: %d\n", ret);
788 
789 	return ret;
790 }
791 
792 /*
793  * DAI routines
794  */
795 
796 static int intel_startup(struct snd_pcm_substream *substream,
797 			 struct snd_soc_dai *dai)
798 {
799 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
800 	int ret;
801 
802 	ret = pm_runtime_resume_and_get(cdns->dev);
803 	if (ret < 0 && ret != -EACCES) {
804 		dev_err_ratelimited(cdns->dev,
805 				    "pm_runtime_resume_and_get failed in %s, ret %d\n",
806 				    __func__, ret);
807 		return ret;
808 	}
809 	return 0;
810 }
811 
812 static int intel_hw_params(struct snd_pcm_substream *substream,
813 			   struct snd_pcm_hw_params *params,
814 			   struct snd_soc_dai *dai)
815 {
816 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
817 	struct sdw_intel *sdw = cdns_to_intel(cdns);
818 	struct sdw_cdns_dma_data *dma;
819 	struct sdw_cdns_pdi *pdi;
820 	struct sdw_stream_config sconfig;
821 	struct sdw_port_config *pconfig;
822 	int ch, dir;
823 	int ret;
824 
825 	dma = snd_soc_dai_get_dma_data(dai, substream);
826 	if (!dma)
827 		return -EIO;
828 
829 	ch = params_channels(params);
830 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
831 		dir = SDW_DATA_DIR_RX;
832 	else
833 		dir = SDW_DATA_DIR_TX;
834 
835 	pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
836 
837 	if (!pdi) {
838 		ret = -EINVAL;
839 		goto error;
840 	}
841 
842 	/* do run-time configurations for SHIM, ALH and PDI/PORT */
843 	intel_pdi_shim_configure(sdw, pdi);
844 	intel_pdi_alh_configure(sdw, pdi);
845 	sdw_cdns_config_stream(cdns, ch, dir, pdi);
846 
847 	/* store pdi and hw_params, may be needed in prepare step */
848 	dma->paused = false;
849 	dma->suspended = false;
850 	dma->pdi = pdi;
851 	dma->hw_params = params;
852 
853 	/* Inform DSP about PDI stream number */
854 	ret = intel_params_stream(sdw, substream->stream, dai, params,
855 				  sdw->instance,
856 				  pdi->intel_alh_id);
857 	if (ret)
858 		goto error;
859 
860 	sconfig.direction = dir;
861 	sconfig.ch_count = ch;
862 	sconfig.frame_rate = params_rate(params);
863 	sconfig.type = dma->stream_type;
864 
865 	sconfig.bps = snd_pcm_format_width(params_format(params));
866 
867 	/* Port configuration */
868 	pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL);
869 	if (!pconfig) {
870 		ret =  -ENOMEM;
871 		goto error;
872 	}
873 
874 	pconfig->num = pdi->num;
875 	pconfig->ch_mask = (1 << ch) - 1;
876 
877 	ret = sdw_stream_add_master(&cdns->bus, &sconfig,
878 				    pconfig, 1, dma->stream);
879 	if (ret)
880 		dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
881 
882 	kfree(pconfig);
883 error:
884 	return ret;
885 }
886 
887 static int intel_prepare(struct snd_pcm_substream *substream,
888 			 struct snd_soc_dai *dai)
889 {
890 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
891 	struct sdw_intel *sdw = cdns_to_intel(cdns);
892 	struct sdw_cdns_dma_data *dma;
893 	int ch, dir;
894 	int ret = 0;
895 
896 	dma = snd_soc_dai_get_dma_data(dai, substream);
897 	if (!dma) {
898 		dev_err(dai->dev, "failed to get dma data in %s\n",
899 			__func__);
900 		return -EIO;
901 	}
902 
903 	if (dma->suspended) {
904 		dma->suspended = false;
905 
906 		/*
907 		 * .prepare() is called after system resume, where we
908 		 * need to reinitialize the SHIM/ALH/Cadence IP.
909 		 * .prepare() is also called to deal with underflows,
910 		 * but in those cases we cannot touch ALH/SHIM
911 		 * registers
912 		 */
913 
914 		/* configure stream */
915 		ch = params_channels(dma->hw_params);
916 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
917 			dir = SDW_DATA_DIR_RX;
918 		else
919 			dir = SDW_DATA_DIR_TX;
920 
921 		intel_pdi_shim_configure(sdw, dma->pdi);
922 		intel_pdi_alh_configure(sdw, dma->pdi);
923 		sdw_cdns_config_stream(cdns, ch, dir, dma->pdi);
924 
925 		/* Inform DSP about PDI stream number */
926 		ret = intel_params_stream(sdw, substream->stream, dai,
927 					  dma->hw_params,
928 					  sdw->instance,
929 					  dma->pdi->intel_alh_id);
930 	}
931 
932 	return ret;
933 }
934 
935 static int
936 intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
937 {
938 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
939 	struct sdw_intel *sdw = cdns_to_intel(cdns);
940 	struct sdw_cdns_dma_data *dma;
941 	int ret;
942 
943 	dma = snd_soc_dai_get_dma_data(dai, substream);
944 	if (!dma)
945 		return -EIO;
946 
947 	/*
948 	 * The sdw stream state will transition to RELEASED when stream->
949 	 * master_list is empty. So the stream state will transition to
950 	 * DEPREPARED for the first cpu-dai and to RELEASED for the last
951 	 * cpu-dai.
952 	 */
953 	ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
954 	if (ret < 0) {
955 		dev_err(dai->dev, "remove master from stream %s failed: %d\n",
956 			dma->stream->name, ret);
957 		return ret;
958 	}
959 
960 	ret = intel_free_stream(sdw, substream->stream, dai, sdw->instance);
961 	if (ret < 0) {
962 		dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
963 		return ret;
964 	}
965 
966 	dma->hw_params = NULL;
967 	dma->pdi = NULL;
968 
969 	return 0;
970 }
971 
972 static void intel_shutdown(struct snd_pcm_substream *substream,
973 			   struct snd_soc_dai *dai)
974 {
975 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
976 
977 	pm_runtime_mark_last_busy(cdns->dev);
978 	pm_runtime_put_autosuspend(cdns->dev);
979 }
980 
981 static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
982 				    void *stream, int direction)
983 {
984 	return cdns_set_sdw_stream(dai, stream, direction);
985 }
986 
987 static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
988 				  int direction)
989 {
990 	struct sdw_cdns_dma_data *dma;
991 
992 	if (direction == SNDRV_PCM_STREAM_PLAYBACK)
993 		dma = dai->playback_dma_data;
994 	else
995 		dma = dai->capture_dma_data;
996 
997 	if (!dma)
998 		return ERR_PTR(-EINVAL);
999 
1000 	return dma->stream;
1001 }
1002 
1003 static int intel_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
1004 {
1005 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1006 	struct sdw_intel *sdw = cdns_to_intel(cdns);
1007 	struct sdw_cdns_dma_data *dma;
1008 	int ret = 0;
1009 
1010 	dma = snd_soc_dai_get_dma_data(dai, substream);
1011 	if (!dma) {
1012 		dev_err(dai->dev, "failed to get dma data in %s\n",
1013 			__func__);
1014 		return -EIO;
1015 	}
1016 
1017 	switch (cmd) {
1018 	case SNDRV_PCM_TRIGGER_SUSPEND:
1019 
1020 		/*
1021 		 * The .prepare callback is used to deal with xruns and resume operations.
1022 		 * In the case of xruns, the DMAs and SHIM registers cannot be touched,
1023 		 * but for resume operations the DMAs and SHIM registers need to be initialized.
1024 		 * the .trigger callback is used to track the suspend case only.
1025 		 */
1026 
1027 		dma->suspended = true;
1028 
1029 		ret = intel_free_stream(sdw, substream->stream, dai, sdw->instance);
1030 		break;
1031 
1032 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1033 		dma->paused = true;
1034 		break;
1035 	case SNDRV_PCM_TRIGGER_STOP:
1036 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1037 		dma->paused = false;
1038 		break;
1039 	default:
1040 		break;
1041 	}
1042 
1043 	return ret;
1044 }
1045 
1046 static int intel_component_probe(struct snd_soc_component *component)
1047 {
1048 	int ret;
1049 
1050 	/*
1051 	 * make sure the device is pm_runtime_active before initiating
1052 	 * bus transactions during the card registration.
1053 	 * We use pm_runtime_resume() here, without taking a reference
1054 	 * and releasing it immediately.
1055 	 */
1056 	ret = pm_runtime_resume(component->dev);
1057 	if (ret < 0 && ret != -EACCES)
1058 		return ret;
1059 
1060 	return 0;
1061 }
1062 
1063 static int intel_component_dais_suspend(struct snd_soc_component *component)
1064 {
1065 	struct snd_soc_dai *dai;
1066 
1067 	/*
1068 	 * In the corner case where a SUSPEND happens during a PAUSE, the ALSA core
1069 	 * does not throw the TRIGGER_SUSPEND. This leaves the DAIs in an unbalanced state.
1070 	 * Since the component suspend is called last, we can trap this corner case
1071 	 * and force the DAIs to release their resources.
1072 	 */
1073 	for_each_component_dais(component, dai) {
1074 		struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1075 		struct sdw_intel *sdw = cdns_to_intel(cdns);
1076 		struct sdw_cdns_dma_data *dma;
1077 		int stream;
1078 		int ret;
1079 
1080 		dma = dai->playback_dma_data;
1081 		stream = SNDRV_PCM_STREAM_PLAYBACK;
1082 		if (!dma) {
1083 			dma = dai->capture_dma_data;
1084 			stream = SNDRV_PCM_STREAM_CAPTURE;
1085 		}
1086 
1087 		if (!dma)
1088 			continue;
1089 
1090 		if (dma->suspended)
1091 			continue;
1092 
1093 		if (dma->paused) {
1094 			dma->suspended = true;
1095 
1096 			ret = intel_free_stream(sdw, stream, dai, sdw->instance);
1097 			if (ret < 0)
1098 				return ret;
1099 		}
1100 	}
1101 
1102 	return 0;
1103 }
1104 
1105 static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
1106 	.startup = intel_startup,
1107 	.hw_params = intel_hw_params,
1108 	.prepare = intel_prepare,
1109 	.hw_free = intel_hw_free,
1110 	.trigger = intel_trigger,
1111 	.shutdown = intel_shutdown,
1112 	.set_stream = intel_pcm_set_sdw_stream,
1113 	.get_stream = intel_get_sdw_stream,
1114 };
1115 
1116 static const struct snd_soc_component_driver dai_component = {
1117 	.name           = "soundwire",
1118 	.probe		= intel_component_probe,
1119 	.suspend	= intel_component_dais_suspend
1120 };
1121 
1122 static int intel_create_dai(struct sdw_cdns *cdns,
1123 			    struct snd_soc_dai_driver *dais,
1124 			    enum intel_pdi_type type,
1125 			    u32 num, u32 off, u32 max_ch)
1126 {
1127 	int i;
1128 
1129 	if (num == 0)
1130 		return 0;
1131 
1132 	 /* TODO: Read supported rates/formats from hardware */
1133 	for (i = off; i < (off + num); i++) {
1134 		dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
1135 					      "SDW%d Pin%d",
1136 					      cdns->instance, i);
1137 		if (!dais[i].name)
1138 			return -ENOMEM;
1139 
1140 		if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
1141 			dais[i].playback.channels_min = 1;
1142 			dais[i].playback.channels_max = max_ch;
1143 			dais[i].playback.rates = SNDRV_PCM_RATE_48000;
1144 			dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
1145 		}
1146 
1147 		if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
1148 			dais[i].capture.channels_min = 1;
1149 			dais[i].capture.channels_max = max_ch;
1150 			dais[i].capture.rates = SNDRV_PCM_RATE_48000;
1151 			dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
1152 		}
1153 
1154 		dais[i].ops = &intel_pcm_dai_ops;
1155 	}
1156 
1157 	return 0;
1158 }
1159 
1160 static int intel_register_dai(struct sdw_intel *sdw)
1161 {
1162 	struct sdw_cdns *cdns = &sdw->cdns;
1163 	struct sdw_cdns_streams *stream;
1164 	struct snd_soc_dai_driver *dais;
1165 	int num_dai, ret, off = 0;
1166 
1167 	/* DAIs are created based on total number of PDIs supported */
1168 	num_dai = cdns->pcm.num_pdi;
1169 
1170 	dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
1171 	if (!dais)
1172 		return -ENOMEM;
1173 
1174 	/* Create PCM DAIs */
1175 	stream = &cdns->pcm;
1176 
1177 	ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
1178 			       off, stream->num_ch_in);
1179 	if (ret)
1180 		return ret;
1181 
1182 	off += cdns->pcm.num_in;
1183 	ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
1184 			       off, stream->num_ch_out);
1185 	if (ret)
1186 		return ret;
1187 
1188 	off += cdns->pcm.num_out;
1189 	ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
1190 			       off, stream->num_ch_bd);
1191 	if (ret)
1192 		return ret;
1193 
1194 	return snd_soc_register_component(cdns->dev, &dai_component,
1195 					  dais, num_dai);
1196 }
1197 
1198 static int sdw_master_read_intel_prop(struct sdw_bus *bus)
1199 {
1200 	struct sdw_master_prop *prop = &bus->prop;
1201 	struct fwnode_handle *link;
1202 	char name[32];
1203 	u32 quirk_mask;
1204 
1205 	/* Find master handle */
1206 	snprintf(name, sizeof(name),
1207 		 "mipi-sdw-link-%d-subproperties", bus->link_id);
1208 
1209 	link = device_get_named_child_node(bus->dev, name);
1210 	if (!link) {
1211 		dev_err(bus->dev, "Master node %s not found\n", name);
1212 		return -EIO;
1213 	}
1214 
1215 	fwnode_property_read_u32(link,
1216 				 "intel-sdw-ip-clock",
1217 				 &prop->mclk_freq);
1218 
1219 	/* the values reported by BIOS are the 2x clock, not the bus clock */
1220 	prop->mclk_freq /= 2;
1221 
1222 	fwnode_property_read_u32(link,
1223 				 "intel-quirk-mask",
1224 				 &quirk_mask);
1225 
1226 	if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
1227 		prop->hw_disabled = true;
1228 
1229 	prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
1230 		SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
1231 
1232 	return 0;
1233 }
1234 
1235 static int intel_prop_read(struct sdw_bus *bus)
1236 {
1237 	/* Initialize with default handler to read all DisCo properties */
1238 	sdw_master_read_prop(bus);
1239 
1240 	/* read Intel-specific properties */
1241 	sdw_master_read_intel_prop(bus);
1242 
1243 	return 0;
1244 }
1245 
1246 static struct sdw_master_ops sdw_intel_ops = {
1247 	.read_prop = sdw_master_read_prop,
1248 	.override_adr = sdw_dmi_override_adr,
1249 	.xfer_msg = cdns_xfer_msg,
1250 	.xfer_msg_defer = cdns_xfer_msg_defer,
1251 	.reset_page_addr = cdns_reset_page_addr,
1252 	.set_bus_conf = cdns_bus_conf,
1253 	.pre_bank_switch = intel_pre_bank_switch,
1254 	.post_bank_switch = intel_post_bank_switch,
1255 };
1256 
1257 static int intel_init(struct sdw_intel *sdw)
1258 {
1259 	bool clock_stop;
1260 
1261 	/* Initialize shim and controller */
1262 	intel_link_power_up(sdw);
1263 
1264 	clock_stop = sdw_cdns_is_clock_stop(&sdw->cdns);
1265 
1266 	intel_shim_init(sdw, clock_stop);
1267 
1268 	return 0;
1269 }
1270 
1271 /*
1272  * probe and init (aux_dev_id argument is required by function prototype but not used)
1273  */
1274 static int intel_link_probe(struct auxiliary_device *auxdev,
1275 			    const struct auxiliary_device_id *aux_dev_id)
1276 
1277 {
1278 	struct device *dev = &auxdev->dev;
1279 	struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
1280 	struct sdw_intel *sdw;
1281 	struct sdw_cdns *cdns;
1282 	struct sdw_bus *bus;
1283 	int ret;
1284 
1285 	sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
1286 	if (!sdw)
1287 		return -ENOMEM;
1288 
1289 	cdns = &sdw->cdns;
1290 	bus = &cdns->bus;
1291 
1292 	sdw->instance = auxdev->id;
1293 	sdw->link_res = &ldev->link_res;
1294 	cdns->dev = dev;
1295 	cdns->registers = sdw->link_res->registers;
1296 	cdns->instance = sdw->instance;
1297 	cdns->msg_count = 0;
1298 
1299 	bus->link_id = auxdev->id;
1300 
1301 	sdw_cdns_probe(cdns);
1302 
1303 	/* Set property read ops */
1304 	sdw_intel_ops.read_prop = intel_prop_read;
1305 	bus->ops = &sdw_intel_ops;
1306 
1307 	/* set driver data, accessed by snd_soc_dai_get_drvdata() */
1308 	auxiliary_set_drvdata(auxdev, cdns);
1309 
1310 	/* use generic bandwidth allocation algorithm */
1311 	sdw->cdns.bus.compute_params = sdw_compute_params;
1312 
1313 	/* avoid resuming from pm_runtime suspend if it's not required */
1314 	dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
1315 
1316 	ret = sdw_bus_master_add(bus, dev, dev->fwnode);
1317 	if (ret) {
1318 		dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
1319 		return ret;
1320 	}
1321 
1322 	if (bus->prop.hw_disabled)
1323 		dev_info(dev,
1324 			 "SoundWire master %d is disabled, will be ignored\n",
1325 			 bus->link_id);
1326 	/*
1327 	 * Ignore BIOS err_threshold, it's a really bad idea when dealing
1328 	 * with multiple hardware synchronized links
1329 	 */
1330 	bus->prop.err_threshold = 0;
1331 
1332 	return 0;
1333 }
1334 
1335 int intel_link_startup(struct auxiliary_device *auxdev)
1336 {
1337 	struct sdw_cdns_stream_config config;
1338 	struct device *dev = &auxdev->dev;
1339 	struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
1340 	struct sdw_intel *sdw = cdns_to_intel(cdns);
1341 	struct sdw_bus *bus = &cdns->bus;
1342 	int link_flags;
1343 	bool multi_link;
1344 	u32 clock_stop_quirks;
1345 	int ret;
1346 
1347 	if (bus->prop.hw_disabled) {
1348 		dev_info(dev,
1349 			 "SoundWire master %d is disabled, ignoring\n",
1350 			 sdw->instance);
1351 		return 0;
1352 	}
1353 
1354 	link_flags = md_flags >> (bus->link_id * 8);
1355 	multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1356 	if (!multi_link) {
1357 		dev_dbg(dev, "Multi-link is disabled\n");
1358 		bus->multi_link = false;
1359 	} else {
1360 		/*
1361 		 * hardware-based synchronization is required regardless
1362 		 * of the number of segments used by a stream: SSP-based
1363 		 * synchronization is gated by gsync when the multi-master
1364 		 * mode is set.
1365 		 */
1366 		bus->multi_link = true;
1367 		bus->hw_sync_min_links = 1;
1368 	}
1369 
1370 	/* Initialize shim, controller */
1371 	ret = intel_init(sdw);
1372 	if (ret)
1373 		goto err_init;
1374 
1375 	/* Read the PDI config and initialize cadence PDI */
1376 	intel_pdi_init(sdw, &config);
1377 	ret = sdw_cdns_pdi_init(cdns, config);
1378 	if (ret)
1379 		goto err_init;
1380 
1381 	intel_pdi_ch_update(sdw);
1382 
1383 	ret = sdw_cdns_enable_interrupt(cdns, true);
1384 	if (ret < 0) {
1385 		dev_err(dev, "cannot enable interrupts\n");
1386 		goto err_init;
1387 	}
1388 
1389 	/*
1390 	 * follow recommended programming flows to avoid timeouts when
1391 	 * gsync is enabled
1392 	 */
1393 	if (multi_link)
1394 		intel_shim_sync_arm(sdw);
1395 
1396 	ret = sdw_cdns_init(cdns);
1397 	if (ret < 0) {
1398 		dev_err(dev, "unable to initialize Cadence IP\n");
1399 		goto err_interrupt;
1400 	}
1401 
1402 	ret = sdw_cdns_exit_reset(cdns);
1403 	if (ret < 0) {
1404 		dev_err(dev, "unable to exit bus reset sequence\n");
1405 		goto err_interrupt;
1406 	}
1407 
1408 	if (multi_link) {
1409 		ret = intel_shim_sync_go(sdw);
1410 		if (ret < 0) {
1411 			dev_err(dev, "sync go failed: %d\n", ret);
1412 			goto err_interrupt;
1413 		}
1414 	}
1415 	sdw_cdns_check_self_clearing_bits(cdns, __func__,
1416 					  true, INTEL_MASTER_RESET_ITERATIONS);
1417 
1418 	/* Register DAIs */
1419 	ret = intel_register_dai(sdw);
1420 	if (ret) {
1421 		dev_err(dev, "DAI registration failed: %d\n", ret);
1422 		snd_soc_unregister_component(dev);
1423 		goto err_interrupt;
1424 	}
1425 
1426 	intel_debugfs_init(sdw);
1427 
1428 	/* Enable runtime PM */
1429 	if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
1430 		pm_runtime_set_autosuspend_delay(dev,
1431 						 INTEL_MASTER_SUSPEND_DELAY_MS);
1432 		pm_runtime_use_autosuspend(dev);
1433 		pm_runtime_mark_last_busy(dev);
1434 
1435 		pm_runtime_set_active(dev);
1436 		pm_runtime_enable(dev);
1437 	}
1438 
1439 	clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1440 	if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
1441 		/*
1442 		 * To keep the clock running we need to prevent
1443 		 * pm_runtime suspend from happening by increasing the
1444 		 * reference count.
1445 		 * This quirk is specified by the parent PCI device in
1446 		 * case of specific latency requirements. It will have
1447 		 * no effect if pm_runtime is disabled by the user via
1448 		 * a module parameter for testing purposes.
1449 		 */
1450 		pm_runtime_get_noresume(dev);
1451 	}
1452 
1453 	/*
1454 	 * The runtime PM status of Slave devices is "Unsupported"
1455 	 * until they report as ATTACHED. If they don't, e.g. because
1456 	 * there are no Slave devices populated or if the power-on is
1457 	 * delayed or dependent on a power switch, the Master will
1458 	 * remain active and prevent its parent from suspending.
1459 	 *
1460 	 * Conditionally force the pm_runtime core to re-evaluate the
1461 	 * Master status in the absence of any Slave activity. A quirk
1462 	 * is provided to e.g. deal with Slaves that may be powered on
1463 	 * with a delay. A more complete solution would require the
1464 	 * definition of Master properties.
1465 	 */
1466 	if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1467 		pm_runtime_idle(dev);
1468 
1469 	sdw->startup_done = true;
1470 	return 0;
1471 
1472 err_interrupt:
1473 	sdw_cdns_enable_interrupt(cdns, false);
1474 err_init:
1475 	return ret;
1476 }
1477 
1478 static void intel_link_remove(struct auxiliary_device *auxdev)
1479 {
1480 	struct device *dev = &auxdev->dev;
1481 	struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
1482 	struct sdw_intel *sdw = cdns_to_intel(cdns);
1483 	struct sdw_bus *bus = &cdns->bus;
1484 
1485 	/*
1486 	 * Since pm_runtime is already disabled, we don't decrease
1487 	 * the refcount when the clock_stop_quirk is
1488 	 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
1489 	 */
1490 	if (!bus->prop.hw_disabled) {
1491 		intel_debugfs_exit(sdw);
1492 		sdw_cdns_enable_interrupt(cdns, false);
1493 		snd_soc_unregister_component(dev);
1494 	}
1495 	sdw_bus_master_delete(bus);
1496 }
1497 
1498 int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
1499 {
1500 	struct device *dev = &auxdev->dev;
1501 	struct sdw_intel *sdw;
1502 	struct sdw_bus *bus;
1503 	void __iomem *shim;
1504 	u16 wake_sts;
1505 
1506 	sdw = auxiliary_get_drvdata(auxdev);
1507 	bus = &sdw->cdns.bus;
1508 
1509 	if (bus->prop.hw_disabled || !sdw->startup_done) {
1510 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1511 			bus->link_id);
1512 		return 0;
1513 	}
1514 
1515 	shim = sdw->link_res->shim;
1516 	wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
1517 
1518 	if (!(wake_sts & BIT(sdw->instance)))
1519 		return 0;
1520 
1521 	/* disable WAKEEN interrupt ASAP to prevent interrupt flood */
1522 	intel_shim_wake(sdw, false);
1523 
1524 	/*
1525 	 * resume the Master, which will generate a bus reset and result in
1526 	 * Slaves re-attaching and be re-enumerated. The SoundWire physical
1527 	 * device which generated the wake will trigger an interrupt, which
1528 	 * will in turn cause the corresponding Linux Slave device to be
1529 	 * resumed and the Slave codec driver to check the status.
1530 	 */
1531 	pm_request_resume(dev);
1532 
1533 	return 0;
1534 }
1535 
1536 /*
1537  * PM calls
1538  */
1539 
1540 static int intel_resume_child_device(struct device *dev, void *data)
1541 {
1542 	int ret;
1543 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
1544 
1545 	if (!slave->probed) {
1546 		dev_dbg(dev, "%s: skipping device, no probed driver\n", __func__);
1547 		return 0;
1548 	}
1549 	if (!slave->dev_num_sticky) {
1550 		dev_dbg(dev, "%s: skipping device, never detected on bus\n", __func__);
1551 		return 0;
1552 	}
1553 
1554 	ret = pm_request_resume(dev);
1555 	if (ret < 0)
1556 		dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
1557 
1558 	return ret;
1559 }
1560 
1561 static int __maybe_unused intel_pm_prepare(struct device *dev)
1562 {
1563 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
1564 	struct sdw_intel *sdw = cdns_to_intel(cdns);
1565 	struct sdw_bus *bus = &cdns->bus;
1566 	u32 clock_stop_quirks;
1567 	int ret;
1568 
1569 	if (bus->prop.hw_disabled || !sdw->startup_done) {
1570 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1571 			bus->link_id);
1572 		return 0;
1573 	}
1574 
1575 	clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1576 
1577 	if (pm_runtime_suspended(dev) &&
1578 	    pm_runtime_suspended(dev->parent) &&
1579 	    ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
1580 	     !clock_stop_quirks)) {
1581 		/*
1582 		 * if we've enabled clock stop, and the parent is suspended, the SHIM registers
1583 		 * are not accessible and the shim wake cannot be disabled.
1584 		 * The only solution is to resume the entire bus to full power
1585 		 */
1586 
1587 		/*
1588 		 * If any operation in this block fails, we keep going since we don't want
1589 		 * to prevent system suspend from happening and errors should be recoverable
1590 		 * on resume.
1591 		 */
1592 
1593 		/*
1594 		 * first resume the device for this link. This will also by construction
1595 		 * resume the PCI parent device.
1596 		 */
1597 		ret = pm_request_resume(dev);
1598 		if (ret < 0) {
1599 			dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
1600 			return 0;
1601 		}
1602 
1603 		/*
1604 		 * Continue resuming the entire bus (parent + child devices) to exit
1605 		 * the clock stop mode. If there are no devices connected on this link
1606 		 * this is a no-op.
1607 		 * The resume to full power could have been implemented with a .prepare
1608 		 * step in SoundWire codec drivers. This would however require a lot
1609 		 * of code to handle an Intel-specific corner case. It is simpler in
1610 		 * practice to add a loop at the link level.
1611 		 */
1612 		ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
1613 
1614 		if (ret < 0)
1615 			dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
1616 	}
1617 
1618 	return 0;
1619 }
1620 
1621 static int __maybe_unused intel_suspend(struct device *dev)
1622 {
1623 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
1624 	struct sdw_intel *sdw = cdns_to_intel(cdns);
1625 	struct sdw_bus *bus = &cdns->bus;
1626 	u32 clock_stop_quirks;
1627 	int ret;
1628 
1629 	if (bus->prop.hw_disabled || !sdw->startup_done) {
1630 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1631 			bus->link_id);
1632 		return 0;
1633 	}
1634 
1635 	if (pm_runtime_suspended(dev)) {
1636 		dev_dbg(dev, "%s: pm_runtime status: suspended\n", __func__);
1637 
1638 		clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1639 
1640 		if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
1641 		    !clock_stop_quirks) {
1642 
1643 			if (pm_runtime_suspended(dev->parent)) {
1644 				/*
1645 				 * paranoia check: this should not happen with the .prepare
1646 				 * resume to full power
1647 				 */
1648 				dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
1649 			} else {
1650 				intel_shim_wake(sdw, false);
1651 			}
1652 		}
1653 
1654 		return 0;
1655 	}
1656 
1657 	ret = sdw_cdns_enable_interrupt(cdns, false);
1658 	if (ret < 0) {
1659 		dev_err(dev, "cannot disable interrupts on suspend\n");
1660 		return ret;
1661 	}
1662 
1663 	ret = intel_link_power_down(sdw);
1664 	if (ret) {
1665 		dev_err(dev, "Link power down failed: %d\n", ret);
1666 		return ret;
1667 	}
1668 
1669 	intel_shim_wake(sdw, false);
1670 
1671 	return 0;
1672 }
1673 
1674 static int __maybe_unused intel_suspend_runtime(struct device *dev)
1675 {
1676 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
1677 	struct sdw_intel *sdw = cdns_to_intel(cdns);
1678 	struct sdw_bus *bus = &cdns->bus;
1679 	u32 clock_stop_quirks;
1680 	int ret;
1681 
1682 	if (bus->prop.hw_disabled || !sdw->startup_done) {
1683 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1684 			bus->link_id);
1685 		return 0;
1686 	}
1687 
1688 	clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1689 
1690 	if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1691 
1692 		ret = sdw_cdns_enable_interrupt(cdns, false);
1693 		if (ret < 0) {
1694 			dev_err(dev, "cannot disable interrupts on suspend\n");
1695 			return ret;
1696 		}
1697 
1698 		ret = intel_link_power_down(sdw);
1699 		if (ret) {
1700 			dev_err(dev, "Link power down failed: %d\n", ret);
1701 			return ret;
1702 		}
1703 
1704 		intel_shim_wake(sdw, false);
1705 
1706 	} else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET ||
1707 		   !clock_stop_quirks) {
1708 		bool wake_enable = true;
1709 
1710 		ret = sdw_cdns_clock_stop(cdns, true);
1711 		if (ret < 0) {
1712 			dev_err(dev, "cannot enable clock stop on suspend\n");
1713 			wake_enable = false;
1714 		}
1715 
1716 		ret = sdw_cdns_enable_interrupt(cdns, false);
1717 		if (ret < 0) {
1718 			dev_err(dev, "cannot disable interrupts on suspend\n");
1719 			return ret;
1720 		}
1721 
1722 		ret = intel_link_power_down(sdw);
1723 		if (ret) {
1724 			dev_err(dev, "Link power down failed: %d\n", ret);
1725 			return ret;
1726 		}
1727 
1728 		intel_shim_wake(sdw, wake_enable);
1729 	} else {
1730 		dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
1731 			__func__, clock_stop_quirks);
1732 		ret = -EINVAL;
1733 	}
1734 
1735 	return ret;
1736 }
1737 
1738 static int __maybe_unused intel_resume(struct device *dev)
1739 {
1740 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
1741 	struct sdw_intel *sdw = cdns_to_intel(cdns);
1742 	struct sdw_bus *bus = &cdns->bus;
1743 	int link_flags;
1744 	bool multi_link;
1745 	int ret;
1746 
1747 	if (bus->prop.hw_disabled || !sdw->startup_done) {
1748 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1749 			bus->link_id);
1750 		return 0;
1751 	}
1752 
1753 	link_flags = md_flags >> (bus->link_id * 8);
1754 	multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1755 
1756 	if (pm_runtime_suspended(dev)) {
1757 		dev_dbg(dev, "%s: pm_runtime status was suspended, forcing active\n", __func__);
1758 
1759 		/* follow required sequence from runtime_pm.rst */
1760 		pm_runtime_disable(dev);
1761 		pm_runtime_set_active(dev);
1762 		pm_runtime_mark_last_busy(dev);
1763 		pm_runtime_enable(dev);
1764 
1765 		link_flags = md_flags >> (bus->link_id * 8);
1766 
1767 		if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1768 			pm_runtime_idle(dev);
1769 	}
1770 
1771 	ret = intel_init(sdw);
1772 	if (ret) {
1773 		dev_err(dev, "%s failed: %d\n", __func__, ret);
1774 		return ret;
1775 	}
1776 
1777 	/*
1778 	 * make sure all Slaves are tagged as UNATTACHED and provide
1779 	 * reason for reinitialization
1780 	 */
1781 	sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
1782 
1783 	ret = sdw_cdns_enable_interrupt(cdns, true);
1784 	if (ret < 0) {
1785 		dev_err(dev, "cannot enable interrupts during resume\n");
1786 		return ret;
1787 	}
1788 
1789 	/*
1790 	 * follow recommended programming flows to avoid timeouts when
1791 	 * gsync is enabled
1792 	 */
1793 	if (multi_link)
1794 		intel_shim_sync_arm(sdw);
1795 
1796 	ret = sdw_cdns_init(&sdw->cdns);
1797 	if (ret < 0) {
1798 		dev_err(dev, "unable to initialize Cadence IP during resume\n");
1799 		return ret;
1800 	}
1801 
1802 	ret = sdw_cdns_exit_reset(cdns);
1803 	if (ret < 0) {
1804 		dev_err(dev, "unable to exit bus reset sequence during resume\n");
1805 		return ret;
1806 	}
1807 
1808 	if (multi_link) {
1809 		ret = intel_shim_sync_go(sdw);
1810 		if (ret < 0) {
1811 			dev_err(dev, "sync go failed during resume\n");
1812 			return ret;
1813 		}
1814 	}
1815 	sdw_cdns_check_self_clearing_bits(cdns, __func__,
1816 					  true, INTEL_MASTER_RESET_ITERATIONS);
1817 
1818 	/*
1819 	 * after system resume, the pm_runtime suspend() may kick in
1820 	 * during the enumeration, before any children device force the
1821 	 * master device to remain active.  Using pm_runtime_get()
1822 	 * routines is not really possible, since it'd prevent the
1823 	 * master from suspending.
1824 	 * A reasonable compromise is to update the pm_runtime
1825 	 * counters and delay the pm_runtime suspend by several
1826 	 * seconds, by when all enumeration should be complete.
1827 	 */
1828 	pm_runtime_mark_last_busy(dev);
1829 
1830 	return ret;
1831 }
1832 
1833 static int __maybe_unused intel_resume_runtime(struct device *dev)
1834 {
1835 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
1836 	struct sdw_intel *sdw = cdns_to_intel(cdns);
1837 	struct sdw_bus *bus = &cdns->bus;
1838 	u32 clock_stop_quirks;
1839 	bool clock_stop0;
1840 	int link_flags;
1841 	bool multi_link;
1842 	int status;
1843 	int ret;
1844 
1845 	if (bus->prop.hw_disabled || !sdw->startup_done) {
1846 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1847 			bus->link_id);
1848 		return 0;
1849 	}
1850 
1851 	/* unconditionally disable WAKEEN interrupt */
1852 	intel_shim_wake(sdw, false);
1853 
1854 	link_flags = md_flags >> (bus->link_id * 8);
1855 	multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1856 
1857 	clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1858 
1859 	if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1860 		ret = intel_init(sdw);
1861 		if (ret) {
1862 			dev_err(dev, "%s failed: %d\n", __func__, ret);
1863 			return ret;
1864 		}
1865 
1866 		/*
1867 		 * make sure all Slaves are tagged as UNATTACHED and provide
1868 		 * reason for reinitialization
1869 		 */
1870 		sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
1871 
1872 		ret = sdw_cdns_enable_interrupt(cdns, true);
1873 		if (ret < 0) {
1874 			dev_err(dev, "cannot enable interrupts during resume\n");
1875 			return ret;
1876 		}
1877 
1878 		/*
1879 		 * follow recommended programming flows to avoid
1880 		 * timeouts when gsync is enabled
1881 		 */
1882 		if (multi_link)
1883 			intel_shim_sync_arm(sdw);
1884 
1885 		ret = sdw_cdns_init(&sdw->cdns);
1886 		if (ret < 0) {
1887 			dev_err(dev, "unable to initialize Cadence IP during resume\n");
1888 			return ret;
1889 		}
1890 
1891 		ret = sdw_cdns_exit_reset(cdns);
1892 		if (ret < 0) {
1893 			dev_err(dev, "unable to exit bus reset sequence during resume\n");
1894 			return ret;
1895 		}
1896 
1897 		if (multi_link) {
1898 			ret = intel_shim_sync_go(sdw);
1899 			if (ret < 0) {
1900 				dev_err(dev, "sync go failed during resume\n");
1901 				return ret;
1902 			}
1903 		}
1904 		sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime TEARDOWN",
1905 						  true, INTEL_MASTER_RESET_ITERATIONS);
1906 
1907 	} else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
1908 		ret = intel_init(sdw);
1909 		if (ret) {
1910 			dev_err(dev, "%s failed: %d\n", __func__, ret);
1911 			return ret;
1912 		}
1913 
1914 		/*
1915 		 * An exception condition occurs for the CLK_STOP_BUS_RESET
1916 		 * case if one or more masters remain active. In this condition,
1917 		 * all the masters are powered on for they are in the same power
1918 		 * domain. Master can preserve its context for clock stop0, so
1919 		 * there is no need to clear slave status and reset bus.
1920 		 */
1921 		clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1922 
1923 		if (!clock_stop0) {
1924 
1925 			/*
1926 			 * make sure all Slaves are tagged as UNATTACHED and
1927 			 * provide reason for reinitialization
1928 			 */
1929 
1930 			status = SDW_UNATTACH_REQUEST_MASTER_RESET;
1931 			sdw_clear_slave_status(bus, status);
1932 
1933 			ret = sdw_cdns_enable_interrupt(cdns, true);
1934 			if (ret < 0) {
1935 				dev_err(dev, "cannot enable interrupts during resume\n");
1936 				return ret;
1937 			}
1938 
1939 			/*
1940 			 * follow recommended programming flows to avoid
1941 			 * timeouts when gsync is enabled
1942 			 */
1943 			if (multi_link)
1944 				intel_shim_sync_arm(sdw);
1945 
1946 			/*
1947 			 * Re-initialize the IP since it was powered-off
1948 			 */
1949 			sdw_cdns_init(&sdw->cdns);
1950 
1951 		} else {
1952 			ret = sdw_cdns_enable_interrupt(cdns, true);
1953 			if (ret < 0) {
1954 				dev_err(dev, "cannot enable interrupts during resume\n");
1955 				return ret;
1956 			}
1957 		}
1958 
1959 		ret = sdw_cdns_clock_restart(cdns, !clock_stop0);
1960 		if (ret < 0) {
1961 			dev_err(dev, "unable to restart clock during resume\n");
1962 			return ret;
1963 		}
1964 
1965 		if (!clock_stop0) {
1966 			ret = sdw_cdns_exit_reset(cdns);
1967 			if (ret < 0) {
1968 				dev_err(dev, "unable to exit bus reset sequence during resume\n");
1969 				return ret;
1970 			}
1971 
1972 			if (multi_link) {
1973 				ret = intel_shim_sync_go(sdw);
1974 				if (ret < 0) {
1975 					dev_err(sdw->cdns.dev, "sync go failed during resume\n");
1976 					return ret;
1977 				}
1978 			}
1979 		}
1980 		sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime BUS_RESET",
1981 						  true, INTEL_MASTER_RESET_ITERATIONS);
1982 
1983 	} else if (!clock_stop_quirks) {
1984 
1985 		clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1986 		if (!clock_stop0)
1987 			dev_err(dev, "%s invalid configuration, clock was not stopped", __func__);
1988 
1989 		ret = intel_init(sdw);
1990 		if (ret) {
1991 			dev_err(dev, "%s failed: %d\n", __func__, ret);
1992 			return ret;
1993 		}
1994 
1995 		ret = sdw_cdns_enable_interrupt(cdns, true);
1996 		if (ret < 0) {
1997 			dev_err(dev, "cannot enable interrupts during resume\n");
1998 			return ret;
1999 		}
2000 
2001 		ret = sdw_cdns_clock_restart(cdns, false);
2002 		if (ret < 0) {
2003 			dev_err(dev, "unable to resume master during resume\n");
2004 			return ret;
2005 		}
2006 
2007 		sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime no_quirks",
2008 						  true, INTEL_MASTER_RESET_ITERATIONS);
2009 	} else {
2010 		dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
2011 			__func__, clock_stop_quirks);
2012 		ret = -EINVAL;
2013 	}
2014 
2015 	return ret;
2016 }
2017 
2018 static const struct dev_pm_ops intel_pm = {
2019 	.prepare = intel_pm_prepare,
2020 	SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
2021 	SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
2022 };
2023 
2024 static const struct auxiliary_device_id intel_link_id_table[] = {
2025 	{ .name = "soundwire_intel.link" },
2026 	{},
2027 };
2028 MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
2029 
2030 static struct auxiliary_driver sdw_intel_drv = {
2031 	.probe = intel_link_probe,
2032 	.remove = intel_link_remove,
2033 	.driver = {
2034 		/* auxiliary_driver_register() sets .name to be the modname */
2035 		.pm = &intel_pm,
2036 	},
2037 	.id_table = intel_link_id_table
2038 };
2039 module_auxiliary_driver(sdw_intel_drv);
2040 
2041 MODULE_LICENSE("Dual BSD/GPL");
2042 MODULE_DESCRIPTION("Intel Soundwire Link Driver");
2043