1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
5 * Soundwire Intel Master Driver
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>
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"
25 #define INTEL_MASTER_SUSPEND_DELAY_MS 3000
26 #define INTEL_MASTER_RESET_ITERATIONS 10
29 * debug/config flags for the Intel SoundWire Master.
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.
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)
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)");
44 /* Intel SHIM Registers Definition */
45 #define SDW_SHIM_LCAP 0x0
46 #define SDW_SHIM_LCTL 0x4
47 #define SDW_SHIM_IPPTR 0x8
48 #define SDW_SHIM_SYNC 0xC
50 #define SDW_SHIM_CTLSCAP(x) (0x010 + 0x60 * (x))
51 #define SDW_SHIM_CTLS0CM(x) (0x012 + 0x60 * (x))
52 #define SDW_SHIM_CTLS1CM(x) (0x014 + 0x60 * (x))
53 #define SDW_SHIM_CTLS2CM(x) (0x016 + 0x60 * (x))
54 #define SDW_SHIM_CTLS3CM(x) (0x018 + 0x60 * (x))
55 #define SDW_SHIM_PCMSCAP(x) (0x020 + 0x60 * (x))
57 #define SDW_SHIM_PCMSYCHM(x, y) (0x022 + (0x60 * (x)) + (0x2 * (y)))
58 #define SDW_SHIM_PCMSYCHC(x, y) (0x042 + (0x60 * (x)) + (0x2 * (y)))
59 #define SDW_SHIM_PDMSCAP(x) (0x062 + 0x60 * (x))
60 #define SDW_SHIM_IOCTL(x) (0x06C + 0x60 * (x))
61 #define SDW_SHIM_CTMCTL(x) (0x06E + 0x60 * (x))
63 #define SDW_SHIM_WAKEEN 0x190
64 #define SDW_SHIM_WAKESTS 0x192
66 #define SDW_SHIM_LCTL_SPA BIT(0)
67 #define SDW_SHIM_LCTL_SPA_MASK GENMASK(3, 0)
68 #define SDW_SHIM_LCTL_CPA BIT(8)
69 #define SDW_SHIM_LCTL_CPA_MASK GENMASK(11, 8)
71 #define SDW_SHIM_SYNC_SYNCPRD_VAL_24 (24000 / SDW_CADENCE_GSYNC_KHZ - 1)
72 #define SDW_SHIM_SYNC_SYNCPRD_VAL_38_4 (38400 / SDW_CADENCE_GSYNC_KHZ - 1)
73 #define SDW_SHIM_SYNC_SYNCPRD GENMASK(14, 0)
74 #define SDW_SHIM_SYNC_SYNCCPU BIT(15)
75 #define SDW_SHIM_SYNC_CMDSYNC_MASK GENMASK(19, 16)
76 #define SDW_SHIM_SYNC_CMDSYNC BIT(16)
77 #define SDW_SHIM_SYNC_SYNCGO BIT(24)
79 #define SDW_SHIM_PCMSCAP_ISS GENMASK(3, 0)
80 #define SDW_SHIM_PCMSCAP_OSS GENMASK(7, 4)
81 #define SDW_SHIM_PCMSCAP_BSS GENMASK(12, 8)
83 #define SDW_SHIM_PCMSYCM_LCHN GENMASK(3, 0)
84 #define SDW_SHIM_PCMSYCM_HCHN GENMASK(7, 4)
85 #define SDW_SHIM_PCMSYCM_STREAM GENMASK(13, 8)
86 #define SDW_SHIM_PCMSYCM_DIR BIT(15)
88 #define SDW_SHIM_PDMSCAP_ISS GENMASK(3, 0)
89 #define SDW_SHIM_PDMSCAP_OSS GENMASK(7, 4)
90 #define SDW_SHIM_PDMSCAP_BSS GENMASK(12, 8)
91 #define SDW_SHIM_PDMSCAP_CPSS GENMASK(15, 13)
93 #define SDW_SHIM_IOCTL_MIF BIT(0)
94 #define SDW_SHIM_IOCTL_CO BIT(1)
95 #define SDW_SHIM_IOCTL_COE BIT(2)
96 #define SDW_SHIM_IOCTL_DO BIT(3)
97 #define SDW_SHIM_IOCTL_DOE BIT(4)
98 #define SDW_SHIM_IOCTL_BKE BIT(5)
99 #define SDW_SHIM_IOCTL_WPDD BIT(6)
100 #define SDW_SHIM_IOCTL_CIBD BIT(8)
101 #define SDW_SHIM_IOCTL_DIBD BIT(9)
103 #define SDW_SHIM_CTMCTL_DACTQE BIT(0)
104 #define SDW_SHIM_CTMCTL_DODS BIT(1)
105 #define SDW_SHIM_CTMCTL_DOAIS GENMASK(4, 3)
107 #define SDW_SHIM_WAKEEN_ENABLE BIT(0)
108 #define SDW_SHIM_WAKESTS_STATUS BIT(0)
110 /* Intel ALH Register definitions */
111 #define SDW_ALH_STRMZCFG(x) (0x000 + (0x4 * (x)))
112 #define SDW_ALH_NUM_STREAMS 64
114 #define SDW_ALH_STRMZCFG_DMAT_VAL 0x3
115 #define SDW_ALH_STRMZCFG_DMAT GENMASK(7, 0)
116 #define SDW_ALH_STRMZCFG_CHN GENMASK(19, 16)
118 enum intel_pdi_type {
124 #define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns)
127 * Read, write helpers for HW registers
129 static inline int intel_readl(void __iomem *base, int offset)
131 return readl(base + offset);
134 static inline void intel_writel(void __iomem *base, int offset, int value)
136 writel(value, base + offset);
139 static inline u16 intel_readw(void __iomem *base, int offset)
141 return readw(base + offset);
144 static inline void intel_writew(void __iomem *base, int offset, u16 value)
146 writew(value, base + offset);
149 static int intel_wait_bit(void __iomem *base, int offset, u32 mask, u32 target)
155 reg_read = readl(base + offset);
156 if ((reg_read & mask) == target)
160 usleep_range(50, 100);
161 } while (timeout != 0);
166 static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
168 writel(value, base + offset);
169 return intel_wait_bit(base, offset, mask, 0);
172 static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
174 writel(value, base + offset);
175 return intel_wait_bit(base, offset, mask, mask);
181 #ifdef CONFIG_DEBUG_FS
183 #define RD_BUF (2 * PAGE_SIZE)
185 static ssize_t intel_sprintf(void __iomem *mem, bool l,
186 char *buf, size_t pos, unsigned int reg)
191 value = intel_readl(mem, reg);
193 value = intel_readw(mem, reg);
195 return scnprintf(buf + pos, RD_BUF - pos, "%4x\t%4x\n", reg, value);
198 static int intel_reg_show(struct seq_file *s_file, void *data)
200 struct sdw_intel *sdw = s_file->private;
201 void __iomem *s = sdw->link_res->shim;
202 void __iomem *a = sdw->link_res->alh;
206 unsigned int links, reg;
208 buf = kzalloc(RD_BUF, GFP_KERNEL);
212 links = intel_readl(s, SDW_SHIM_LCAP) & GENMASK(2, 0);
214 ret = scnprintf(buf, RD_BUF, "Register Value\n");
215 ret += scnprintf(buf + ret, RD_BUF - ret, "\nShim\n");
217 for (i = 0; i < links; i++) {
218 reg = SDW_SHIM_LCAP + i * 4;
219 ret += intel_sprintf(s, true, buf, ret, reg);
222 for (i = 0; i < links; i++) {
223 ret += scnprintf(buf + ret, RD_BUF - ret, "\nLink%d\n", i);
224 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLSCAP(i));
225 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS0CM(i));
226 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS1CM(i));
227 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS2CM(i));
228 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS3CM(i));
229 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PCMSCAP(i));
231 ret += scnprintf(buf + ret, RD_BUF - ret, "\n PCMSyCH registers\n");
234 * the value 10 is the number of PDIs. We will need a
235 * cleanup to remove hard-coded Intel configurations
236 * from cadence_master.c
238 for (j = 0; j < 10; j++) {
239 ret += intel_sprintf(s, false, buf, ret,
240 SDW_SHIM_PCMSYCHM(i, j));
241 ret += intel_sprintf(s, false, buf, ret,
242 SDW_SHIM_PCMSYCHC(i, j));
244 ret += scnprintf(buf + ret, RD_BUF - ret, "\n PDMSCAP, IOCTL, CTMCTL\n");
246 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PDMSCAP(i));
247 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_IOCTL(i));
248 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTMCTL(i));
251 ret += scnprintf(buf + ret, RD_BUF - ret, "\nWake registers\n");
252 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKEEN);
253 ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKESTS);
255 ret += scnprintf(buf + ret, RD_BUF - ret, "\nALH STRMzCFG\n");
256 for (i = 0; i < SDW_ALH_NUM_STREAMS; i++)
257 ret += intel_sprintf(a, true, buf, ret, SDW_ALH_STRMZCFG(i));
259 seq_printf(s_file, "%s", buf);
264 DEFINE_SHOW_ATTRIBUTE(intel_reg);
266 static int intel_set_m_datamode(void *data, u64 value)
268 struct sdw_intel *sdw = data;
269 struct sdw_bus *bus = &sdw->cdns.bus;
271 if (value > SDW_PORT_DATA_MODE_STATIC_1)
274 /* Userspace changed the hardware state behind the kernel's back */
275 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
277 bus->params.m_data_mode = value;
281 DEFINE_DEBUGFS_ATTRIBUTE(intel_set_m_datamode_fops, NULL,
282 intel_set_m_datamode, "%llu\n");
284 static int intel_set_s_datamode(void *data, u64 value)
286 struct sdw_intel *sdw = data;
287 struct sdw_bus *bus = &sdw->cdns.bus;
289 if (value > SDW_PORT_DATA_MODE_STATIC_1)
292 /* Userspace changed the hardware state behind the kernel's back */
293 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
295 bus->params.s_data_mode = value;
299 DEFINE_DEBUGFS_ATTRIBUTE(intel_set_s_datamode_fops, NULL,
300 intel_set_s_datamode, "%llu\n");
302 static void intel_debugfs_init(struct sdw_intel *sdw)
304 struct dentry *root = sdw->cdns.bus.debugfs;
309 sdw->debugfs = debugfs_create_dir("intel-sdw", root);
311 debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw,
314 debugfs_create_file("intel-m-datamode", 0200, sdw->debugfs, sdw,
315 &intel_set_m_datamode_fops);
317 debugfs_create_file("intel-s-datamode", 0200, sdw->debugfs, sdw,
318 &intel_set_s_datamode_fops);
320 sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
323 static void intel_debugfs_exit(struct sdw_intel *sdw)
325 debugfs_remove_recursive(sdw->debugfs);
328 static void intel_debugfs_init(struct sdw_intel *sdw) {}
329 static void intel_debugfs_exit(struct sdw_intel *sdw) {}
330 #endif /* CONFIG_DEBUG_FS */
336 static int intel_link_power_up(struct sdw_intel *sdw)
338 unsigned int link_id = sdw->instance;
339 void __iomem *shim = sdw->link_res->shim;
340 u32 *shim_mask = sdw->link_res->shim_mask;
341 struct sdw_bus *bus = &sdw->cdns.bus;
342 struct sdw_master_prop *prop = &bus->prop;
343 u32 spa_mask, cpa_mask;
349 mutex_lock(sdw->link_res->shim_lock);
352 * The hardware relies on an internal counter, typically 4kHz,
353 * to generate the SoundWire SSP - which defines a 'safe'
354 * synchronization point between commands and audio transport
355 * and allows for multi link synchronization. The SYNCPRD value
356 * is only dependent on the oscillator clock provided to
357 * the IP, so adjust based on _DSD properties reported in DSDT
358 * tables. The values reported are based on either 24MHz
359 * (CNL/CML) or 38.4 MHz (ICL/TGL+).
361 if (prop->mclk_freq % 6000000)
362 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
364 syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24;
367 dev_dbg(sdw->cdns.dev, "%s: powering up all links\n", __func__);
369 /* we first need to program the SyncPRD/CPU registers */
370 dev_dbg(sdw->cdns.dev,
371 "%s: first link up, programming SYNCPRD\n", __func__);
373 /* set SyncPRD period */
374 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
375 u32p_replace_bits(&sync_reg, syncprd, SDW_SHIM_SYNC_SYNCPRD);
377 /* Set SyncCPU bit */
378 sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
379 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
381 /* Link power up sequence */
382 link_control = intel_readl(shim, SDW_SHIM_LCTL);
384 /* only power-up enabled links */
385 spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, sdw->link_res->link_mask);
386 cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
388 link_control |= spa_mask;
390 ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
392 dev_err(sdw->cdns.dev, "Failed to power up link: %d\n", ret);
396 /* SyncCPU will change once link is active */
397 ret = intel_wait_bit(shim, SDW_SHIM_SYNC,
398 SDW_SHIM_SYNC_SYNCCPU, 0);
400 dev_err(sdw->cdns.dev,
401 "Failed to set SHIM_SYNC: %d\n", ret);
406 *shim_mask |= BIT(link_id);
408 sdw->cdns.link_up = true;
410 mutex_unlock(sdw->link_res->shim_lock);
415 /* this needs to be called with shim_lock */
416 static void intel_shim_glue_to_master_ip(struct sdw_intel *sdw)
418 void __iomem *shim = sdw->link_res->shim;
419 unsigned int link_id = sdw->instance;
422 /* Switch to MIP from Glue logic */
423 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
425 ioctl &= ~(SDW_SHIM_IOCTL_DOE);
426 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
427 usleep_range(10, 15);
429 ioctl &= ~(SDW_SHIM_IOCTL_DO);
430 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
431 usleep_range(10, 15);
433 ioctl |= (SDW_SHIM_IOCTL_MIF);
434 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
435 usleep_range(10, 15);
437 ioctl &= ~(SDW_SHIM_IOCTL_BKE);
438 ioctl &= ~(SDW_SHIM_IOCTL_COE);
439 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
440 usleep_range(10, 15);
442 /* at this point Master IP has full control of the I/Os */
445 /* this needs to be called with shim_lock */
446 static void intel_shim_master_ip_to_glue(struct sdw_intel *sdw)
448 unsigned int link_id = sdw->instance;
449 void __iomem *shim = sdw->link_res->shim;
453 ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
454 ioctl |= SDW_SHIM_IOCTL_BKE;
455 ioctl |= SDW_SHIM_IOCTL_COE;
456 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
457 usleep_range(10, 15);
459 ioctl &= ~(SDW_SHIM_IOCTL_MIF);
460 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
461 usleep_range(10, 15);
463 /* at this point Integration Glue has full control of the I/Os */
466 static int intel_shim_init(struct sdw_intel *sdw, bool clock_stop)
468 void __iomem *shim = sdw->link_res->shim;
469 unsigned int link_id = sdw->instance;
471 u16 ioctl = 0, act = 0;
473 mutex_lock(sdw->link_res->shim_lock);
475 /* Initialize Shim */
476 ioctl |= SDW_SHIM_IOCTL_BKE;
477 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
478 usleep_range(10, 15);
480 ioctl |= SDW_SHIM_IOCTL_WPDD;
481 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
482 usleep_range(10, 15);
484 ioctl |= SDW_SHIM_IOCTL_DO;
485 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
486 usleep_range(10, 15);
488 ioctl |= SDW_SHIM_IOCTL_DOE;
489 intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
490 usleep_range(10, 15);
492 intel_shim_glue_to_master_ip(sdw);
494 u16p_replace_bits(&act, 0x1, SDW_SHIM_CTMCTL_DOAIS);
495 act |= SDW_SHIM_CTMCTL_DACTQE;
496 act |= SDW_SHIM_CTMCTL_DODS;
497 intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
498 usleep_range(10, 15);
500 mutex_unlock(sdw->link_res->shim_lock);
505 static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
507 void __iomem *shim = sdw->link_res->shim;
508 unsigned int link_id = sdw->instance;
509 u16 wake_en, wake_sts;
511 mutex_lock(sdw->link_res->shim_lock);
512 wake_en = intel_readw(shim, SDW_SHIM_WAKEEN);
515 /* Enable the wakeup */
516 wake_en |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
517 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
519 /* Disable the wake up interrupt */
520 wake_en &= ~(SDW_SHIM_WAKEEN_ENABLE << link_id);
521 intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
523 /* Clear wake status */
524 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
525 wake_sts |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
526 intel_writew(shim, SDW_SHIM_WAKESTS_STATUS, wake_sts);
528 mutex_unlock(sdw->link_res->shim_lock);
531 static int intel_link_power_down(struct sdw_intel *sdw)
533 u32 link_control, spa_mask, cpa_mask;
534 unsigned int link_id = sdw->instance;
535 void __iomem *shim = sdw->link_res->shim;
536 u32 *shim_mask = sdw->link_res->shim_mask;
539 mutex_lock(sdw->link_res->shim_lock);
541 if (!(*shim_mask & BIT(link_id)))
542 dev_err(sdw->cdns.dev,
543 "%s: Unbalanced power-up/down calls\n", __func__);
545 sdw->cdns.link_up = false;
547 intel_shim_master_ip_to_glue(sdw);
549 *shim_mask &= ~BIT(link_id);
553 dev_dbg(sdw->cdns.dev, "%s: powering down all links\n", __func__);
555 /* Link power down sequence */
556 link_control = intel_readl(shim, SDW_SHIM_LCTL);
558 /* only power-down enabled links */
559 spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, ~sdw->link_res->link_mask);
560 cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
562 link_control &= spa_mask;
564 ret = intel_clear_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
566 dev_err(sdw->cdns.dev, "%s: could not power down link\n", __func__);
569 * we leave the sdw->cdns.link_up flag as false since we've disabled
570 * the link at this point and cannot handle interrupts any longer.
575 mutex_unlock(sdw->link_res->shim_lock);
580 static void intel_shim_sync_arm(struct sdw_intel *sdw)
582 void __iomem *shim = sdw->link_res->shim;
585 mutex_lock(sdw->link_res->shim_lock);
587 /* update SYNC register */
588 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
589 sync_reg |= (SDW_SHIM_SYNC_CMDSYNC << sdw->instance);
590 intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
592 mutex_unlock(sdw->link_res->shim_lock);
595 static int intel_shim_sync_go_unlocked(struct sdw_intel *sdw)
597 void __iomem *shim = sdw->link_res->shim;
601 /* Read SYNC register */
602 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
605 * Set SyncGO bit to synchronously trigger a bank switch for
606 * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
609 sync_reg |= SDW_SHIM_SYNC_SYNCGO;
611 ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
612 SDW_SHIM_SYNC_SYNCGO);
615 dev_err(sdw->cdns.dev, "SyncGO clear failed: %d\n", ret);
620 static int intel_shim_sync_go(struct sdw_intel *sdw)
624 mutex_lock(sdw->link_res->shim_lock);
626 ret = intel_shim_sync_go_unlocked(sdw);
628 mutex_unlock(sdw->link_res->shim_lock);
636 static void intel_pdi_init(struct sdw_intel *sdw,
637 struct sdw_cdns_stream_config *config)
639 void __iomem *shim = sdw->link_res->shim;
640 unsigned int link_id = sdw->instance;
641 int pcm_cap, pdm_cap;
643 /* PCM Stream Capability */
644 pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
646 config->pcm_bd = FIELD_GET(SDW_SHIM_PCMSCAP_BSS, pcm_cap);
647 config->pcm_in = FIELD_GET(SDW_SHIM_PCMSCAP_ISS, pcm_cap);
648 config->pcm_out = FIELD_GET(SDW_SHIM_PCMSCAP_OSS, pcm_cap);
650 dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
651 config->pcm_bd, config->pcm_in, config->pcm_out);
653 /* PDM Stream Capability */
654 pdm_cap = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
656 config->pdm_bd = FIELD_GET(SDW_SHIM_PDMSCAP_BSS, pdm_cap);
657 config->pdm_in = FIELD_GET(SDW_SHIM_PDMSCAP_ISS, pdm_cap);
658 config->pdm_out = FIELD_GET(SDW_SHIM_PDMSCAP_OSS, pdm_cap);
660 dev_dbg(sdw->cdns.dev, "PDM cap bd:%d in:%d out:%d\n",
661 config->pdm_bd, config->pdm_in, config->pdm_out);
665 intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num, bool pcm)
667 void __iomem *shim = sdw->link_res->shim;
668 unsigned int link_id = sdw->instance;
672 count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
675 * WORKAROUND: on all existing Intel controllers, pdi
676 * number 2 reports channel count as 1 even though it
677 * supports 8 channels. Performing hardcoding for pdi
684 count = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
685 count = FIELD_GET(SDW_SHIM_PDMSCAP_CPSS, count);
688 /* zero based values for channel count in register */
694 static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
695 struct sdw_cdns_pdi *pdi,
696 unsigned int num_pdi,
697 unsigned int *num_ch, bool pcm)
701 for (i = 0; i < num_pdi; i++) {
702 pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num, pcm);
703 ch_count += pdi->ch_count;
711 static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
712 struct sdw_cdns_streams *stream, bool pcm)
714 intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
715 &stream->num_ch_bd, pcm);
717 intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
718 &stream->num_ch_in, pcm);
720 intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
721 &stream->num_ch_out, pcm);
726 static int intel_pdi_ch_update(struct sdw_intel *sdw)
728 /* First update PCM streams followed by PDM streams */
729 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm, true);
730 intel_pdi_stream_ch_update(sdw, &sdw->cdns.pdm, false);
736 intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
738 void __iomem *shim = sdw->link_res->shim;
739 unsigned int link_id = sdw->instance;
742 /* the Bulk and PCM streams are not contiguous */
743 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
745 pdi->intel_alh_id += 2;
748 * Program stream parameters to stream SHIM register
749 * This is applicable for PCM stream only.
751 if (pdi->type != SDW_STREAM_PCM)
754 if (pdi->dir == SDW_DATA_DIR_RX)
755 pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
757 pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
759 u32p_replace_bits(&pdi_conf, pdi->intel_alh_id, SDW_SHIM_PCMSYCM_STREAM);
760 u32p_replace_bits(&pdi_conf, pdi->l_ch_num, SDW_SHIM_PCMSYCM_LCHN);
761 u32p_replace_bits(&pdi_conf, pdi->h_ch_num, SDW_SHIM_PCMSYCM_HCHN);
763 intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
767 intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
769 void __iomem *alh = sdw->link_res->alh;
770 unsigned int link_id = sdw->instance;
773 /* the Bulk and PCM streams are not contiguous */
774 pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
776 pdi->intel_alh_id += 2;
778 /* Program Stream config ALH register */
779 conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
781 u32p_replace_bits(&conf, SDW_ALH_STRMZCFG_DMAT_VAL, SDW_ALH_STRMZCFG_DMAT);
782 u32p_replace_bits(&conf, pdi->ch_count - 1, SDW_ALH_STRMZCFG_CHN);
784 intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
787 static int intel_params_stream(struct sdw_intel *sdw,
788 struct snd_pcm_substream *substream,
789 struct snd_soc_dai *dai,
790 struct snd_pcm_hw_params *hw_params,
791 int link_id, int alh_stream_id)
793 struct sdw_intel_link_res *res = sdw->link_res;
794 struct sdw_intel_stream_params_data params_data;
796 params_data.substream = substream;
797 params_data.dai = dai;
798 params_data.hw_params = hw_params;
799 params_data.link_id = link_id;
800 params_data.alh_stream_id = alh_stream_id;
802 if (res->ops && res->ops->params_stream && res->dev)
803 return res->ops->params_stream(res->dev,
808 static int intel_free_stream(struct sdw_intel *sdw,
809 struct snd_pcm_substream *substream,
810 struct snd_soc_dai *dai,
813 struct sdw_intel_link_res *res = sdw->link_res;
814 struct sdw_intel_stream_free_data free_data;
816 free_data.substream = substream;
818 free_data.link_id = link_id;
820 if (res->ops && res->ops->free_stream && res->dev)
821 return res->ops->free_stream(res->dev,
828 * bank switch routines
831 static int intel_pre_bank_switch(struct sdw_bus *bus)
833 struct sdw_cdns *cdns = bus_to_cdns(bus);
834 struct sdw_intel *sdw = cdns_to_intel(cdns);
836 /* Write to register only for multi-link */
837 if (!bus->multi_link)
840 intel_shim_sync_arm(sdw);
845 static int intel_post_bank_switch(struct sdw_bus *bus)
847 struct sdw_cdns *cdns = bus_to_cdns(bus);
848 struct sdw_intel *sdw = cdns_to_intel(cdns);
849 void __iomem *shim = sdw->link_res->shim;
852 /* Write to register only for multi-link */
853 if (!bus->multi_link)
856 mutex_lock(sdw->link_res->shim_lock);
858 /* Read SYNC register */
859 sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
862 * post_bank_switch() ops is called from the bus in loop for
863 * all the Masters in the steam with the expectation that
864 * we trigger the bankswitch for the only first Master in the list
865 * and do nothing for the other Masters
867 * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master.
869 if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK)) {
874 ret = intel_shim_sync_go_unlocked(sdw);
876 mutex_unlock(sdw->link_res->shim_lock);
879 dev_err(sdw->cdns.dev, "Post bank switch failed: %d\n", ret);
888 static int intel_startup(struct snd_pcm_substream *substream,
889 struct snd_soc_dai *dai)
891 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
894 ret = pm_runtime_get_sync(cdns->dev);
895 if (ret < 0 && ret != -EACCES) {
896 dev_err_ratelimited(cdns->dev,
897 "pm_runtime_get_sync failed in %s, ret %d\n",
899 pm_runtime_put_noidle(cdns->dev);
905 static int intel_hw_params(struct snd_pcm_substream *substream,
906 struct snd_pcm_hw_params *params,
907 struct snd_soc_dai *dai)
909 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
910 struct sdw_intel *sdw = cdns_to_intel(cdns);
911 struct sdw_cdns_dma_data *dma;
912 struct sdw_cdns_pdi *pdi;
913 struct sdw_stream_config sconfig;
914 struct sdw_port_config *pconfig;
919 dma = snd_soc_dai_get_dma_data(dai, substream);
923 ch = params_channels(params);
924 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
925 dir = SDW_DATA_DIR_RX;
927 dir = SDW_DATA_DIR_TX;
929 if (dma->stream_type == SDW_STREAM_PDM)
933 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
935 pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pdm, ch, dir, dai->id);
942 /* do run-time configurations for SHIM, ALH and PDI/PORT */
943 intel_pdi_shim_configure(sdw, pdi);
944 intel_pdi_alh_configure(sdw, pdi);
945 sdw_cdns_config_stream(cdns, ch, dir, pdi);
947 /* store pdi and hw_params, may be needed in prepare step */
948 dma->suspended = false;
950 dma->hw_params = params;
952 /* Inform DSP about PDI stream number */
953 ret = intel_params_stream(sdw, substream, dai, params,
959 sconfig.direction = dir;
960 sconfig.ch_count = ch;
961 sconfig.frame_rate = params_rate(params);
962 sconfig.type = dma->stream_type;
964 if (dma->stream_type == SDW_STREAM_PDM) {
965 sconfig.frame_rate *= 50;
968 sconfig.bps = snd_pcm_format_width(params_format(params));
971 /* Port configuration */
972 pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL);
978 pconfig->num = pdi->num;
979 pconfig->ch_mask = (1 << ch) - 1;
981 ret = sdw_stream_add_master(&cdns->bus, &sconfig,
982 pconfig, 1, dma->stream);
984 dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
991 static int intel_prepare(struct snd_pcm_substream *substream,
992 struct snd_soc_dai *dai)
994 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
995 struct sdw_intel *sdw = cdns_to_intel(cdns);
996 struct sdw_cdns_dma_data *dma;
1000 dma = snd_soc_dai_get_dma_data(dai, substream);
1002 dev_err(dai->dev, "failed to get dma data in %s\n",
1007 if (dma->suspended) {
1008 dma->suspended = false;
1011 * .prepare() is called after system resume, where we
1012 * need to reinitialize the SHIM/ALH/Cadence IP.
1013 * .prepare() is also called to deal with underflows,
1014 * but in those cases we cannot touch ALH/SHIM
1018 /* configure stream */
1019 ch = params_channels(dma->hw_params);
1020 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1021 dir = SDW_DATA_DIR_RX;
1023 dir = SDW_DATA_DIR_TX;
1025 intel_pdi_shim_configure(sdw, dma->pdi);
1026 intel_pdi_alh_configure(sdw, dma->pdi);
1027 sdw_cdns_config_stream(cdns, ch, dir, dma->pdi);
1029 /* Inform DSP about PDI stream number */
1030 ret = intel_params_stream(sdw, substream, dai,
1033 dma->pdi->intel_alh_id);
1040 intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
1042 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1043 struct sdw_intel *sdw = cdns_to_intel(cdns);
1044 struct sdw_cdns_dma_data *dma;
1047 dma = snd_soc_dai_get_dma_data(dai, substream);
1052 * The sdw stream state will transition to RELEASED when stream->
1053 * master_list is empty. So the stream state will transition to
1054 * DEPREPARED for the first cpu-dai and to RELEASED for the last
1057 ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
1059 dev_err(dai->dev, "remove master from stream %s failed: %d\n",
1060 dma->stream->name, ret);
1064 ret = intel_free_stream(sdw, substream, dai, sdw->instance);
1066 dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
1070 dma->hw_params = NULL;
1076 static void intel_shutdown(struct snd_pcm_substream *substream,
1077 struct snd_soc_dai *dai)
1079 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1081 pm_runtime_mark_last_busy(cdns->dev);
1082 pm_runtime_put_autosuspend(cdns->dev);
1085 static int intel_component_dais_suspend(struct snd_soc_component *component)
1087 struct sdw_cdns_dma_data *dma;
1088 struct snd_soc_dai *dai;
1090 for_each_component_dais(component, dai) {
1092 * we don't have a .suspend dai_ops, and we don't have access
1093 * to the substream, so let's mark both capture and playback
1094 * DMA contexts as suspended
1096 dma = dai->playback_dma_data;
1098 dma->suspended = true;
1100 dma = dai->capture_dma_data;
1102 dma->suspended = true;
1108 static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
1109 void *stream, int direction)
1111 return cdns_set_sdw_stream(dai, stream, true, direction);
1114 static int intel_pdm_set_sdw_stream(struct snd_soc_dai *dai,
1115 void *stream, int direction)
1117 return cdns_set_sdw_stream(dai, stream, false, direction);
1120 static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
1123 struct sdw_cdns_dma_data *dma;
1125 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1126 dma = dai->playback_dma_data;
1128 dma = dai->capture_dma_data;
1131 return ERR_PTR(-EINVAL);
1136 static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
1137 .startup = intel_startup,
1138 .hw_params = intel_hw_params,
1139 .prepare = intel_prepare,
1140 .hw_free = intel_hw_free,
1141 .shutdown = intel_shutdown,
1142 .set_sdw_stream = intel_pcm_set_sdw_stream,
1143 .get_sdw_stream = intel_get_sdw_stream,
1146 static const struct snd_soc_dai_ops intel_pdm_dai_ops = {
1147 .startup = intel_startup,
1148 .hw_params = intel_hw_params,
1149 .prepare = intel_prepare,
1150 .hw_free = intel_hw_free,
1151 .shutdown = intel_shutdown,
1152 .set_sdw_stream = intel_pdm_set_sdw_stream,
1153 .get_sdw_stream = intel_get_sdw_stream,
1156 static const struct snd_soc_component_driver dai_component = {
1157 .name = "soundwire",
1158 .suspend = intel_component_dais_suspend
1161 static int intel_create_dai(struct sdw_cdns *cdns,
1162 struct snd_soc_dai_driver *dais,
1163 enum intel_pdi_type type,
1164 u32 num, u32 off, u32 max_ch, bool pcm)
1171 /* TODO: Read supported rates/formats from hardware */
1172 for (i = off; i < (off + num); i++) {
1173 dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
1179 if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
1180 dais[i].playback.channels_min = 1;
1181 dais[i].playback.channels_max = max_ch;
1182 dais[i].playback.rates = SNDRV_PCM_RATE_48000;
1183 dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
1186 if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
1187 dais[i].capture.channels_min = 1;
1188 dais[i].capture.channels_max = max_ch;
1189 dais[i].capture.rates = SNDRV_PCM_RATE_48000;
1190 dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
1194 dais[i].ops = &intel_pcm_dai_ops;
1196 dais[i].ops = &intel_pdm_dai_ops;
1202 static int intel_register_dai(struct sdw_intel *sdw)
1204 struct sdw_cdns *cdns = &sdw->cdns;
1205 struct sdw_cdns_streams *stream;
1206 struct snd_soc_dai_driver *dais;
1207 int num_dai, ret, off = 0;
1209 /* DAIs are created based on total number of PDIs supported */
1210 num_dai = cdns->pcm.num_pdi + cdns->pdm.num_pdi;
1212 dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
1216 /* Create PCM DAIs */
1217 stream = &cdns->pcm;
1219 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
1220 off, stream->num_ch_in, true);
1224 off += cdns->pcm.num_in;
1225 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
1226 off, stream->num_ch_out, true);
1230 off += cdns->pcm.num_out;
1231 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
1232 off, stream->num_ch_bd, true);
1236 /* Create PDM DAIs */
1237 stream = &cdns->pdm;
1238 off += cdns->pcm.num_bd;
1239 ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pdm.num_in,
1240 off, stream->num_ch_in, false);
1244 off += cdns->pdm.num_in;
1245 ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pdm.num_out,
1246 off, stream->num_ch_out, false);
1250 off += cdns->pdm.num_out;
1251 ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pdm.num_bd,
1252 off, stream->num_ch_bd, false);
1256 return snd_soc_register_component(cdns->dev, &dai_component,
1260 static int sdw_master_read_intel_prop(struct sdw_bus *bus)
1262 struct sdw_master_prop *prop = &bus->prop;
1263 struct fwnode_handle *link;
1267 /* Find master handle */
1268 snprintf(name, sizeof(name),
1269 "mipi-sdw-link-%d-subproperties", bus->link_id);
1271 link = device_get_named_child_node(bus->dev, name);
1273 dev_err(bus->dev, "Master node %s not found\n", name);
1277 fwnode_property_read_u32(link,
1278 "intel-sdw-ip-clock",
1281 /* the values reported by BIOS are the 2x clock, not the bus clock */
1282 prop->mclk_freq /= 2;
1284 fwnode_property_read_u32(link,
1288 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
1289 prop->hw_disabled = true;
1291 prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
1292 SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
1297 static int intel_prop_read(struct sdw_bus *bus)
1299 /* Initialize with default handler to read all DisCo properties */
1300 sdw_master_read_prop(bus);
1302 /* read Intel-specific properties */
1303 sdw_master_read_intel_prop(bus);
1308 static struct sdw_master_ops sdw_intel_ops = {
1309 .read_prop = sdw_master_read_prop,
1310 .override_adr = sdw_dmi_override_adr,
1311 .xfer_msg = cdns_xfer_msg,
1312 .xfer_msg_defer = cdns_xfer_msg_defer,
1313 .reset_page_addr = cdns_reset_page_addr,
1314 .set_bus_conf = cdns_bus_conf,
1315 .pre_bank_switch = intel_pre_bank_switch,
1316 .post_bank_switch = intel_post_bank_switch,
1319 static int intel_init(struct sdw_intel *sdw)
1323 /* Initialize shim and controller */
1324 intel_link_power_up(sdw);
1326 clock_stop = sdw_cdns_is_clock_stop(&sdw->cdns);
1328 intel_shim_init(sdw, clock_stop);
1334 * probe and init (aux_dev_id argument is required by function prototype but not used)
1336 static int intel_link_probe(struct auxiliary_device *auxdev,
1337 const struct auxiliary_device_id *aux_dev_id)
1340 struct device *dev = &auxdev->dev;
1341 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
1342 struct sdw_intel *sdw;
1343 struct sdw_cdns *cdns;
1344 struct sdw_bus *bus;
1347 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
1354 sdw->instance = auxdev->id;
1355 sdw->link_res = &ldev->link_res;
1357 cdns->registers = sdw->link_res->registers;
1358 cdns->instance = sdw->instance;
1359 cdns->msg_count = 0;
1361 bus->link_id = auxdev->id;
1363 sdw_cdns_probe(cdns);
1365 /* Set property read ops */
1366 sdw_intel_ops.read_prop = intel_prop_read;
1367 bus->ops = &sdw_intel_ops;
1369 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
1370 dev_set_drvdata(dev, cdns);
1372 /* use generic bandwidth allocation algorithm */
1373 sdw->cdns.bus.compute_params = sdw_compute_params;
1375 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
1377 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
1381 if (bus->prop.hw_disabled)
1383 "SoundWire master %d is disabled, will be ignored\n",
1386 * Ignore BIOS err_threshold, it's a really bad idea when dealing
1387 * with multiple hardware synchronized links
1389 bus->prop.err_threshold = 0;
1394 int intel_link_startup(struct auxiliary_device *auxdev)
1396 struct sdw_cdns_stream_config config;
1397 struct device *dev = &auxdev->dev;
1398 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1399 struct sdw_intel *sdw = cdns_to_intel(cdns);
1400 struct sdw_bus *bus = &cdns->bus;
1403 u32 clock_stop_quirks;
1406 if (bus->prop.hw_disabled) {
1408 "SoundWire master %d is disabled, ignoring\n",
1413 link_flags = md_flags >> (bus->link_id * 8);
1414 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1416 dev_dbg(dev, "Multi-link is disabled\n");
1417 bus->multi_link = false;
1420 * hardware-based synchronization is required regardless
1421 * of the number of segments used by a stream: SSP-based
1422 * synchronization is gated by gsync when the multi-master
1425 bus->multi_link = true;
1426 bus->hw_sync_min_links = 1;
1429 /* Initialize shim, controller */
1430 ret = intel_init(sdw);
1434 /* Read the PDI config and initialize cadence PDI */
1435 intel_pdi_init(sdw, &config);
1436 ret = sdw_cdns_pdi_init(cdns, config);
1440 intel_pdi_ch_update(sdw);
1442 ret = sdw_cdns_enable_interrupt(cdns, true);
1444 dev_err(dev, "cannot enable interrupts\n");
1449 * follow recommended programming flows to avoid timeouts when
1453 intel_shim_sync_arm(sdw);
1455 ret = sdw_cdns_init(cdns);
1457 dev_err(dev, "unable to initialize Cadence IP\n");
1461 ret = sdw_cdns_exit_reset(cdns);
1463 dev_err(dev, "unable to exit bus reset sequence\n");
1468 ret = intel_shim_sync_go(sdw);
1470 dev_err(dev, "sync go failed: %d\n", ret);
1474 sdw_cdns_check_self_clearing_bits(cdns, __func__,
1475 true, INTEL_MASTER_RESET_ITERATIONS);
1478 ret = intel_register_dai(sdw);
1480 dev_err(dev, "DAI registration failed: %d\n", ret);
1481 snd_soc_unregister_component(dev);
1485 intel_debugfs_init(sdw);
1487 /* Enable runtime PM */
1488 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
1489 pm_runtime_set_autosuspend_delay(dev,
1490 INTEL_MASTER_SUSPEND_DELAY_MS);
1491 pm_runtime_use_autosuspend(dev);
1492 pm_runtime_mark_last_busy(dev);
1494 pm_runtime_set_active(dev);
1495 pm_runtime_enable(dev);
1498 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1499 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
1501 * To keep the clock running we need to prevent
1502 * pm_runtime suspend from happening by increasing the
1504 * This quirk is specified by the parent PCI device in
1505 * case of specific latency requirements. It will have
1506 * no effect if pm_runtime is disabled by the user via
1507 * a module parameter for testing purposes.
1509 pm_runtime_get_noresume(dev);
1513 * The runtime PM status of Slave devices is "Unsupported"
1514 * until they report as ATTACHED. If they don't, e.g. because
1515 * there are no Slave devices populated or if the power-on is
1516 * delayed or dependent on a power switch, the Master will
1517 * remain active and prevent its parent from suspending.
1519 * Conditionally force the pm_runtime core to re-evaluate the
1520 * Master status in the absence of any Slave activity. A quirk
1521 * is provided to e.g. deal with Slaves that may be powered on
1522 * with a delay. A more complete solution would require the
1523 * definition of Master properties.
1525 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1526 pm_runtime_idle(dev);
1528 sdw->startup_done = true;
1532 sdw_cdns_enable_interrupt(cdns, false);
1537 static void intel_link_remove(struct auxiliary_device *auxdev)
1539 struct device *dev = &auxdev->dev;
1540 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1541 struct sdw_intel *sdw = cdns_to_intel(cdns);
1542 struct sdw_bus *bus = &cdns->bus;
1545 * Since pm_runtime is already disabled, we don't decrease
1546 * the refcount when the clock_stop_quirk is
1547 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
1549 if (!bus->prop.hw_disabled) {
1550 intel_debugfs_exit(sdw);
1551 sdw_cdns_enable_interrupt(cdns, false);
1552 snd_soc_unregister_component(dev);
1554 sdw_bus_master_delete(bus);
1557 int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
1559 struct device *dev = &auxdev->dev;
1560 struct sdw_intel *sdw;
1561 struct sdw_bus *bus;
1565 sdw = dev_get_drvdata(dev);
1566 bus = &sdw->cdns.bus;
1568 if (bus->prop.hw_disabled || !sdw->startup_done) {
1569 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1574 shim = sdw->link_res->shim;
1575 wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
1577 if (!(wake_sts & BIT(sdw->instance)))
1580 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
1581 intel_shim_wake(sdw, false);
1584 * resume the Master, which will generate a bus reset and result in
1585 * Slaves re-attaching and be re-enumerated. The SoundWire physical
1586 * device which generated the wake will trigger an interrupt, which
1587 * will in turn cause the corresponding Linux Slave device to be
1588 * resumed and the Slave codec driver to check the status.
1590 pm_request_resume(dev);
1599 static int intel_resume_child_device(struct device *dev, void *data)
1602 struct sdw_slave *slave = dev_to_sdw_dev(dev);
1604 if (!slave->probed) {
1605 dev_dbg(dev, "%s: skipping device, no probed driver\n", __func__);
1608 if (!slave->dev_num_sticky) {
1609 dev_dbg(dev, "%s: skipping device, never detected on bus\n", __func__);
1613 ret = pm_request_resume(dev);
1615 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
1620 static int __maybe_unused intel_pm_prepare(struct device *dev)
1622 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1623 struct sdw_intel *sdw = cdns_to_intel(cdns);
1624 struct sdw_bus *bus = &cdns->bus;
1625 u32 clock_stop_quirks;
1628 if (bus->prop.hw_disabled || !sdw->startup_done) {
1629 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1634 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1636 if (pm_runtime_suspended(dev) &&
1637 pm_runtime_suspended(dev->parent) &&
1638 ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
1639 !clock_stop_quirks)) {
1641 * if we've enabled clock stop, and the parent is suspended, the SHIM registers
1642 * are not accessible and the shim wake cannot be disabled.
1643 * The only solution is to resume the entire bus to full power
1647 * If any operation in this block fails, we keep going since we don't want
1648 * to prevent system suspend from happening and errors should be recoverable
1653 * first resume the device for this link. This will also by construction
1654 * resume the PCI parent device.
1656 ret = pm_request_resume(dev);
1658 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
1663 * Continue resuming the entire bus (parent + child devices) to exit
1664 * the clock stop mode. If there are no devices connected on this link
1666 * The resume to full power could have been implemented with a .prepare
1667 * step in SoundWire codec drivers. This would however require a lot
1668 * of code to handle an Intel-specific corner case. It is simpler in
1669 * practice to add a loop at the link level.
1671 ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
1674 dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
1680 static int __maybe_unused intel_suspend(struct device *dev)
1682 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1683 struct sdw_intel *sdw = cdns_to_intel(cdns);
1684 struct sdw_bus *bus = &cdns->bus;
1685 u32 clock_stop_quirks;
1688 if (bus->prop.hw_disabled || !sdw->startup_done) {
1689 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1694 if (pm_runtime_suspended(dev)) {
1695 dev_dbg(dev, "%s: pm_runtime status: suspended\n", __func__);
1697 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1699 if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
1700 !clock_stop_quirks) {
1702 if (pm_runtime_suspended(dev->parent)) {
1704 * paranoia check: this should not happen with the .prepare
1705 * resume to full power
1707 dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
1709 intel_shim_wake(sdw, false);
1716 ret = sdw_cdns_enable_interrupt(cdns, false);
1718 dev_err(dev, "cannot disable interrupts on suspend\n");
1722 ret = intel_link_power_down(sdw);
1724 dev_err(dev, "Link power down failed: %d\n", ret);
1728 intel_shim_wake(sdw, false);
1733 static int __maybe_unused intel_suspend_runtime(struct device *dev)
1735 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1736 struct sdw_intel *sdw = cdns_to_intel(cdns);
1737 struct sdw_bus *bus = &cdns->bus;
1738 u32 clock_stop_quirks;
1741 if (bus->prop.hw_disabled || !sdw->startup_done) {
1742 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1747 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1749 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1751 ret = sdw_cdns_enable_interrupt(cdns, false);
1753 dev_err(dev, "cannot disable interrupts on suspend\n");
1757 ret = intel_link_power_down(sdw);
1759 dev_err(dev, "Link power down failed: %d\n", ret);
1763 intel_shim_wake(sdw, false);
1765 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET ||
1766 !clock_stop_quirks) {
1767 bool wake_enable = true;
1769 ret = sdw_cdns_clock_stop(cdns, true);
1771 dev_err(dev, "cannot enable clock stop on suspend\n");
1772 wake_enable = false;
1775 ret = sdw_cdns_enable_interrupt(cdns, false);
1777 dev_err(dev, "cannot disable interrupts on suspend\n");
1781 ret = intel_link_power_down(sdw);
1783 dev_err(dev, "Link power down failed: %d\n", ret);
1787 intel_shim_wake(sdw, wake_enable);
1789 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
1790 __func__, clock_stop_quirks);
1797 static int __maybe_unused intel_resume(struct device *dev)
1799 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1800 struct sdw_intel *sdw = cdns_to_intel(cdns);
1801 struct sdw_bus *bus = &cdns->bus;
1806 if (bus->prop.hw_disabled || !sdw->startup_done) {
1807 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1812 link_flags = md_flags >> (bus->link_id * 8);
1813 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1815 if (pm_runtime_suspended(dev)) {
1816 dev_dbg(dev, "%s: pm_runtime status was suspended, forcing active\n", __func__);
1818 /* follow required sequence from runtime_pm.rst */
1819 pm_runtime_disable(dev);
1820 pm_runtime_set_active(dev);
1821 pm_runtime_mark_last_busy(dev);
1822 pm_runtime_enable(dev);
1824 link_flags = md_flags >> (bus->link_id * 8);
1826 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1827 pm_runtime_idle(dev);
1830 ret = intel_init(sdw);
1832 dev_err(dev, "%s failed: %d\n", __func__, ret);
1837 * make sure all Slaves are tagged as UNATTACHED and provide
1838 * reason for reinitialization
1840 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
1842 ret = sdw_cdns_enable_interrupt(cdns, true);
1844 dev_err(dev, "cannot enable interrupts during resume\n");
1849 * follow recommended programming flows to avoid timeouts when
1853 intel_shim_sync_arm(sdw);
1855 ret = sdw_cdns_init(&sdw->cdns);
1857 dev_err(dev, "unable to initialize Cadence IP during resume\n");
1861 ret = sdw_cdns_exit_reset(cdns);
1863 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1868 ret = intel_shim_sync_go(sdw);
1870 dev_err(dev, "sync go failed during resume\n");
1874 sdw_cdns_check_self_clearing_bits(cdns, __func__,
1875 true, INTEL_MASTER_RESET_ITERATIONS);
1878 * after system resume, the pm_runtime suspend() may kick in
1879 * during the enumeration, before any children device force the
1880 * master device to remain active. Using pm_runtime_get()
1881 * routines is not really possible, since it'd prevent the
1882 * master from suspending.
1883 * A reasonable compromise is to update the pm_runtime
1884 * counters and delay the pm_runtime suspend by several
1885 * seconds, by when all enumeration should be complete.
1887 pm_runtime_mark_last_busy(dev);
1892 static int __maybe_unused intel_resume_runtime(struct device *dev)
1894 struct sdw_cdns *cdns = dev_get_drvdata(dev);
1895 struct sdw_intel *sdw = cdns_to_intel(cdns);
1896 struct sdw_bus *bus = &cdns->bus;
1897 u32 clock_stop_quirks;
1904 if (bus->prop.hw_disabled || !sdw->startup_done) {
1905 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1910 link_flags = md_flags >> (bus->link_id * 8);
1911 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1913 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1915 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1916 ret = intel_init(sdw);
1918 dev_err(dev, "%s failed: %d\n", __func__, ret);
1923 * make sure all Slaves are tagged as UNATTACHED and provide
1924 * reason for reinitialization
1926 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
1928 ret = sdw_cdns_enable_interrupt(cdns, true);
1930 dev_err(dev, "cannot enable interrupts during resume\n");
1935 * follow recommended programming flows to avoid
1936 * timeouts when gsync is enabled
1939 intel_shim_sync_arm(sdw);
1941 ret = sdw_cdns_init(&sdw->cdns);
1943 dev_err(dev, "unable to initialize Cadence IP during resume\n");
1947 ret = sdw_cdns_exit_reset(cdns);
1949 dev_err(dev, "unable to exit bus reset sequence during resume\n");
1954 ret = intel_shim_sync_go(sdw);
1956 dev_err(dev, "sync go failed during resume\n");
1960 sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime TEARDOWN",
1961 true, INTEL_MASTER_RESET_ITERATIONS);
1963 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
1964 ret = intel_init(sdw);
1966 dev_err(dev, "%s failed: %d\n", __func__, ret);
1971 * An exception condition occurs for the CLK_STOP_BUS_RESET
1972 * case if one or more masters remain active. In this condition,
1973 * all the masters are powered on for they are in the same power
1974 * domain. Master can preserve its context for clock stop0, so
1975 * there is no need to clear slave status and reset bus.
1977 clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1982 * make sure all Slaves are tagged as UNATTACHED and
1983 * provide reason for reinitialization
1986 status = SDW_UNATTACH_REQUEST_MASTER_RESET;
1987 sdw_clear_slave_status(bus, status);
1989 ret = sdw_cdns_enable_interrupt(cdns, true);
1991 dev_err(dev, "cannot enable interrupts during resume\n");
1996 * follow recommended programming flows to avoid
1997 * timeouts when gsync is enabled
2000 intel_shim_sync_arm(sdw);
2003 * Re-initialize the IP since it was powered-off
2005 sdw_cdns_init(&sdw->cdns);
2008 ret = sdw_cdns_enable_interrupt(cdns, true);
2010 dev_err(dev, "cannot enable interrupts during resume\n");
2015 ret = sdw_cdns_clock_restart(cdns, !clock_stop0);
2017 dev_err(dev, "unable to restart clock during resume\n");
2022 ret = sdw_cdns_exit_reset(cdns);
2024 dev_err(dev, "unable to exit bus reset sequence during resume\n");
2029 ret = intel_shim_sync_go(sdw);
2031 dev_err(sdw->cdns.dev, "sync go failed during resume\n");
2036 sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime BUS_RESET",
2037 true, INTEL_MASTER_RESET_ITERATIONS);
2039 } else if (!clock_stop_quirks) {
2041 clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
2043 dev_err(dev, "%s invalid configuration, clock was not stopped", __func__);
2045 ret = intel_init(sdw);
2047 dev_err(dev, "%s failed: %d\n", __func__, ret);
2051 ret = sdw_cdns_enable_interrupt(cdns, true);
2053 dev_err(dev, "cannot enable interrupts during resume\n");
2057 ret = sdw_cdns_clock_restart(cdns, false);
2059 dev_err(dev, "unable to resume master during resume\n");
2063 sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime no_quirks",
2064 true, INTEL_MASTER_RESET_ITERATIONS);
2066 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
2067 __func__, clock_stop_quirks);
2074 static const struct dev_pm_ops intel_pm = {
2075 .prepare = intel_pm_prepare,
2076 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
2077 SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
2080 static const struct auxiliary_device_id intel_link_id_table[] = {
2081 { .name = "soundwire_intel.link" },
2084 MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
2086 static struct auxiliary_driver sdw_intel_drv = {
2087 .probe = intel_link_probe,
2088 .remove = intel_link_remove,
2090 /* auxiliary_driver_register() sets .name to be the modname */
2093 .id_table = intel_link_id_table
2095 module_auxiliary_driver(sdw_intel_drv);
2097 MODULE_LICENSE("Dual BSD/GPL");
2098 MODULE_DESCRIPTION("Intel Soundwire Link Driver");