xref: /linux/drivers/soundwire/intel.c (revision 63198aaa91acf64237bf5cfde3803ed388815150)
171bb8a1bSVinod Koul // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
271bb8a1bSVinod Koul // Copyright(c) 2015-17 Intel Corporation.
371bb8a1bSVinod Koul 
471bb8a1bSVinod Koul /*
571bb8a1bSVinod Koul  * Soundwire Intel Master Driver
671bb8a1bSVinod Koul  */
771bb8a1bSVinod Koul 
871bb8a1bSVinod Koul #include <linux/acpi.h>
979ee6631SPierre-Louis Bossart #include <linux/debugfs.h>
1071bb8a1bSVinod Koul #include <linux/delay.h>
114abbd783SPaul Gortmaker #include <linux/module.h>
1271bb8a1bSVinod Koul #include <linux/interrupt.h>
13df72b719SPierre-Louis Bossart #include <linux/io.h>
1429a269c6SPierre-Louis Bossart #include <linux/auxiliary_bus.h>
1537a2d22bSVinod Koul #include <sound/pcm_params.h>
16ab2c9132SRander Wang #include <linux/pm_runtime.h>
1737a2d22bSVinod Koul #include <sound/soc.h>
1871bb8a1bSVinod Koul #include <linux/soundwire/sdw_registers.h>
1971bb8a1bSVinod Koul #include <linux/soundwire/sdw.h>
2071bb8a1bSVinod Koul #include <linux/soundwire/sdw_intel.h>
2171bb8a1bSVinod Koul #include "cadence_master.h"
2279ee6631SPierre-Louis Bossart #include "bus.h"
2371bb8a1bSVinod Koul #include "intel.h"
2471bb8a1bSVinod Koul 
25ebf878edSPierre-Louis Bossart #define INTEL_MASTER_SUSPEND_DELAY_MS	3000
26ff560946SPierre-Louis Bossart #define INTEL_MASTER_RESET_ITERATIONS	10
27ebf878edSPierre-Louis Bossart 
28ebf878edSPierre-Louis Bossart /*
29ebf878edSPierre-Louis Bossart  * debug/config flags for the Intel SoundWire Master.
30ebf878edSPierre-Louis Bossart  *
31ebf878edSPierre-Louis Bossart  * Since we may have multiple masters active, we can have up to 8
32ebf878edSPierre-Louis Bossart  * flags reused in each byte, with master0 using the ls-byte, etc.
33ebf878edSPierre-Louis Bossart  */
34ebf878edSPierre-Louis Bossart 
35ebf878edSPierre-Louis Bossart #define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME		BIT(0)
36ebf878edSPierre-Louis Bossart #define SDW_INTEL_MASTER_DISABLE_CLOCK_STOP		BIT(1)
37a2d9c161SPierre-Louis Bossart #define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE	BIT(2)
38857a7c42SPierre-Louis Bossart #define SDW_INTEL_MASTER_DISABLE_MULTI_LINK		BIT(3)
39ebf878edSPierre-Louis Bossart 
40ebf878edSPierre-Louis Bossart static int md_flags;
41ebf878edSPierre-Louis Bossart module_param_named(sdw_md_flags, md_flags, int, 0444);
42ebf878edSPierre-Louis Bossart MODULE_PARM_DESC(sdw_md_flags, "SoundWire Intel Master device flags (0x0 all off)");
43ebf878edSPierre-Louis Bossart 
44c46302ecSVinod Koul enum intel_pdi_type {
45c46302ecSVinod Koul 	INTEL_PDI_IN = 0,
46c46302ecSVinod Koul 	INTEL_PDI_OUT = 1,
47c46302ecSVinod Koul 	INTEL_PDI_BD = 2,
48c46302ecSVinod Koul };
49c46302ecSVinod Koul 
5071bb8a1bSVinod Koul #define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns)
5171bb8a1bSVinod Koul 
5271bb8a1bSVinod Koul /*
5371bb8a1bSVinod Koul  * Read, write helpers for HW registers
5471bb8a1bSVinod Koul  */
5571bb8a1bSVinod Koul static inline int intel_readl(void __iomem *base, int offset)
5671bb8a1bSVinod Koul {
5771bb8a1bSVinod Koul 	return readl(base + offset);
5871bb8a1bSVinod Koul }
5971bb8a1bSVinod Koul 
6071bb8a1bSVinod Koul static inline void intel_writel(void __iomem *base, int offset, int value)
6171bb8a1bSVinod Koul {
6271bb8a1bSVinod Koul 	writel(value, base + offset);
6371bb8a1bSVinod Koul }
6471bb8a1bSVinod Koul 
6571bb8a1bSVinod Koul static inline u16 intel_readw(void __iomem *base, int offset)
6671bb8a1bSVinod Koul {
6771bb8a1bSVinod Koul 	return readw(base + offset);
6871bb8a1bSVinod Koul }
6971bb8a1bSVinod Koul 
7071bb8a1bSVinod Koul static inline void intel_writew(void __iomem *base, int offset, u16 value)
7171bb8a1bSVinod Koul {
7271bb8a1bSVinod Koul 	writew(value, base + offset);
7371bb8a1bSVinod Koul }
7471bb8a1bSVinod Koul 
757d2845d5SPierre-Louis Bossart static int intel_wait_bit(void __iomem *base, int offset, u32 mask, u32 target)
7671bb8a1bSVinod Koul {
7771bb8a1bSVinod Koul 	int timeout = 10;
7871bb8a1bSVinod Koul 	u32 reg_read;
7971bb8a1bSVinod Koul 
8071bb8a1bSVinod Koul 	do {
8171bb8a1bSVinod Koul 		reg_read = readl(base + offset);
827d2845d5SPierre-Louis Bossart 		if ((reg_read & mask) == target)
8371bb8a1bSVinod Koul 			return 0;
8471bb8a1bSVinod Koul 
8571bb8a1bSVinod Koul 		timeout--;
867d2845d5SPierre-Louis Bossart 		usleep_range(50, 100);
8771bb8a1bSVinod Koul 	} while (timeout != 0);
8871bb8a1bSVinod Koul 
8971bb8a1bSVinod Koul 	return -EAGAIN;
9071bb8a1bSVinod Koul }
9171bb8a1bSVinod Koul 
927d2845d5SPierre-Louis Bossart static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
937d2845d5SPierre-Louis Bossart {
947d2845d5SPierre-Louis Bossart 	writel(value, base + offset);
957d2845d5SPierre-Louis Bossart 	return intel_wait_bit(base, offset, mask, 0);
967d2845d5SPierre-Louis Bossart }
977d2845d5SPierre-Louis Bossart 
9871bb8a1bSVinod Koul static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
9971bb8a1bSVinod Koul {
10071bb8a1bSVinod Koul 	writel(value, base + offset);
1017d2845d5SPierre-Louis Bossart 	return intel_wait_bit(base, offset, mask, mask);
10271bb8a1bSVinod Koul }
10371bb8a1bSVinod Koul 
10471bb8a1bSVinod Koul /*
10579ee6631SPierre-Louis Bossart  * debugfs
10679ee6631SPierre-Louis Bossart  */
10779ee6631SPierre-Louis Bossart #ifdef CONFIG_DEBUG_FS
10879ee6631SPierre-Louis Bossart 
10979ee6631SPierre-Louis Bossart #define RD_BUF (2 * PAGE_SIZE)
11079ee6631SPierre-Louis Bossart 
11179ee6631SPierre-Louis Bossart static ssize_t intel_sprintf(void __iomem *mem, bool l,
11279ee6631SPierre-Louis Bossart 			     char *buf, size_t pos, unsigned int reg)
11379ee6631SPierre-Louis Bossart {
11479ee6631SPierre-Louis Bossart 	int value;
11579ee6631SPierre-Louis Bossart 
11679ee6631SPierre-Louis Bossart 	if (l)
11779ee6631SPierre-Louis Bossart 		value = intel_readl(mem, reg);
11879ee6631SPierre-Louis Bossart 	else
11979ee6631SPierre-Louis Bossart 		value = intel_readw(mem, reg);
12079ee6631SPierre-Louis Bossart 
12179ee6631SPierre-Louis Bossart 	return scnprintf(buf + pos, RD_BUF - pos, "%4x\t%4x\n", reg, value);
12279ee6631SPierre-Louis Bossart }
12379ee6631SPierre-Louis Bossart 
12479ee6631SPierre-Louis Bossart static int intel_reg_show(struct seq_file *s_file, void *data)
12579ee6631SPierre-Louis Bossart {
12679ee6631SPierre-Louis Bossart 	struct sdw_intel *sdw = s_file->private;
1272523486bSPierre-Louis Bossart 	void __iomem *s = sdw->link_res->shim;
1282523486bSPierre-Louis Bossart 	void __iomem *a = sdw->link_res->alh;
12979ee6631SPierre-Louis Bossart 	char *buf;
13079ee6631SPierre-Louis Bossart 	ssize_t ret;
13179ee6631SPierre-Louis Bossart 	int i, j;
13279ee6631SPierre-Louis Bossart 	unsigned int links, reg;
13379ee6631SPierre-Louis Bossart 
13479ee6631SPierre-Louis Bossart 	buf = kzalloc(RD_BUF, GFP_KERNEL);
13579ee6631SPierre-Louis Bossart 	if (!buf)
13679ee6631SPierre-Louis Bossart 		return -ENOMEM;
13779ee6631SPierre-Louis Bossart 
13879ee6631SPierre-Louis Bossart 	links = intel_readl(s, SDW_SHIM_LCAP) & GENMASK(2, 0);
13979ee6631SPierre-Louis Bossart 
14079ee6631SPierre-Louis Bossart 	ret = scnprintf(buf, RD_BUF, "Register  Value\n");
14179ee6631SPierre-Louis Bossart 	ret += scnprintf(buf + ret, RD_BUF - ret, "\nShim\n");
14279ee6631SPierre-Louis Bossart 
14379ee6631SPierre-Louis Bossart 	for (i = 0; i < links; i++) {
14479ee6631SPierre-Louis Bossart 		reg = SDW_SHIM_LCAP + i * 4;
14579ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, true, buf, ret, reg);
14679ee6631SPierre-Louis Bossart 	}
14779ee6631SPierre-Louis Bossart 
14879ee6631SPierre-Louis Bossart 	for (i = 0; i < links; i++) {
14979ee6631SPierre-Louis Bossart 		ret += scnprintf(buf + ret, RD_BUF - ret, "\nLink%d\n", i);
15079ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLSCAP(i));
15179ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS0CM(i));
15279ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS1CM(i));
15379ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS2CM(i));
15479ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS3CM(i));
15579ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PCMSCAP(i));
15679ee6631SPierre-Louis Bossart 
15779ee6631SPierre-Louis Bossart 		ret += scnprintf(buf + ret, RD_BUF - ret, "\n PCMSyCH registers\n");
15879ee6631SPierre-Louis Bossart 
15979ee6631SPierre-Louis Bossart 		/*
16079ee6631SPierre-Louis Bossart 		 * the value 10 is the number of PDIs. We will need a
16179ee6631SPierre-Louis Bossart 		 * cleanup to remove hard-coded Intel configurations
16279ee6631SPierre-Louis Bossart 		 * from cadence_master.c
16379ee6631SPierre-Louis Bossart 		 */
16479ee6631SPierre-Louis Bossart 		for (j = 0; j < 10; j++) {
16579ee6631SPierre-Louis Bossart 			ret += intel_sprintf(s, false, buf, ret,
16679ee6631SPierre-Louis Bossart 					SDW_SHIM_PCMSYCHM(i, j));
16779ee6631SPierre-Louis Bossart 			ret += intel_sprintf(s, false, buf, ret,
16879ee6631SPierre-Louis Bossart 					SDW_SHIM_PCMSYCHC(i, j));
16979ee6631SPierre-Louis Bossart 		}
17079ee6631SPierre-Louis Bossart 		ret += scnprintf(buf + ret, RD_BUF - ret, "\n PDMSCAP, IOCTL, CTMCTL\n");
17179ee6631SPierre-Louis Bossart 
17279ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PDMSCAP(i));
17379ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_IOCTL(i));
17479ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTMCTL(i));
17579ee6631SPierre-Louis Bossart 	}
17679ee6631SPierre-Louis Bossart 
17779ee6631SPierre-Louis Bossart 	ret += scnprintf(buf + ret, RD_BUF - ret, "\nWake registers\n");
17879ee6631SPierre-Louis Bossart 	ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKEEN);
17979ee6631SPierre-Louis Bossart 	ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKESTS);
18079ee6631SPierre-Louis Bossart 
18179ee6631SPierre-Louis Bossart 	ret += scnprintf(buf + ret, RD_BUF - ret, "\nALH STRMzCFG\n");
18279ee6631SPierre-Louis Bossart 	for (i = 0; i < SDW_ALH_NUM_STREAMS; i++)
18379ee6631SPierre-Louis Bossart 		ret += intel_sprintf(a, true, buf, ret, SDW_ALH_STRMZCFG(i));
18479ee6631SPierre-Louis Bossart 
18579ee6631SPierre-Louis Bossart 	seq_printf(s_file, "%s", buf);
18679ee6631SPierre-Louis Bossart 	kfree(buf);
18779ee6631SPierre-Louis Bossart 
18879ee6631SPierre-Louis Bossart 	return 0;
18979ee6631SPierre-Louis Bossart }
19079ee6631SPierre-Louis Bossart DEFINE_SHOW_ATTRIBUTE(intel_reg);
19179ee6631SPierre-Louis Bossart 
1920f9138e7SPierre-Louis Bossart static int intel_set_m_datamode(void *data, u64 value)
1930f9138e7SPierre-Louis Bossart {
1940f9138e7SPierre-Louis Bossart 	struct sdw_intel *sdw = data;
1950f9138e7SPierre-Louis Bossart 	struct sdw_bus *bus = &sdw->cdns.bus;
1960f9138e7SPierre-Louis Bossart 
1970f9138e7SPierre-Louis Bossart 	if (value > SDW_PORT_DATA_MODE_STATIC_1)
1980f9138e7SPierre-Louis Bossart 		return -EINVAL;
1990f9138e7SPierre-Louis Bossart 
2000f9138e7SPierre-Louis Bossart 	/* Userspace changed the hardware state behind the kernel's back */
2010f9138e7SPierre-Louis Bossart 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
2020f9138e7SPierre-Louis Bossart 
2030f9138e7SPierre-Louis Bossart 	bus->params.m_data_mode = value;
2040f9138e7SPierre-Louis Bossart 
2050f9138e7SPierre-Louis Bossart 	return 0;
2060f9138e7SPierre-Louis Bossart }
2070f9138e7SPierre-Louis Bossart DEFINE_DEBUGFS_ATTRIBUTE(intel_set_m_datamode_fops, NULL,
2080f9138e7SPierre-Louis Bossart 			 intel_set_m_datamode, "%llu\n");
2090f9138e7SPierre-Louis Bossart 
2100f9138e7SPierre-Louis Bossart static int intel_set_s_datamode(void *data, u64 value)
2110f9138e7SPierre-Louis Bossart {
2120f9138e7SPierre-Louis Bossart 	struct sdw_intel *sdw = data;
2130f9138e7SPierre-Louis Bossart 	struct sdw_bus *bus = &sdw->cdns.bus;
2140f9138e7SPierre-Louis Bossart 
2150f9138e7SPierre-Louis Bossart 	if (value > SDW_PORT_DATA_MODE_STATIC_1)
2160f9138e7SPierre-Louis Bossart 		return -EINVAL;
2170f9138e7SPierre-Louis Bossart 
2180f9138e7SPierre-Louis Bossart 	/* Userspace changed the hardware state behind the kernel's back */
2190f9138e7SPierre-Louis Bossart 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
2200f9138e7SPierre-Louis Bossart 
2210f9138e7SPierre-Louis Bossart 	bus->params.s_data_mode = value;
2220f9138e7SPierre-Louis Bossart 
2230f9138e7SPierre-Louis Bossart 	return 0;
2240f9138e7SPierre-Louis Bossart }
2250f9138e7SPierre-Louis Bossart DEFINE_DEBUGFS_ATTRIBUTE(intel_set_s_datamode_fops, NULL,
2260f9138e7SPierre-Louis Bossart 			 intel_set_s_datamode, "%llu\n");
2270f9138e7SPierre-Louis Bossart 
22879ee6631SPierre-Louis Bossart static void intel_debugfs_init(struct sdw_intel *sdw)
22979ee6631SPierre-Louis Bossart {
23079ee6631SPierre-Louis Bossart 	struct dentry *root = sdw->cdns.bus.debugfs;
23179ee6631SPierre-Louis Bossart 
23279ee6631SPierre-Louis Bossart 	if (!root)
23379ee6631SPierre-Louis Bossart 		return;
23479ee6631SPierre-Louis Bossart 
23579ee6631SPierre-Louis Bossart 	sdw->debugfs = debugfs_create_dir("intel-sdw", root);
23679ee6631SPierre-Louis Bossart 
23779ee6631SPierre-Louis Bossart 	debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw,
23879ee6631SPierre-Louis Bossart 			    &intel_reg_fops);
23979ee6631SPierre-Louis Bossart 
2400f9138e7SPierre-Louis Bossart 	debugfs_create_file("intel-m-datamode", 0200, sdw->debugfs, sdw,
2410f9138e7SPierre-Louis Bossart 			    &intel_set_m_datamode_fops);
2420f9138e7SPierre-Louis Bossart 
2430f9138e7SPierre-Louis Bossart 	debugfs_create_file("intel-s-datamode", 0200, sdw->debugfs, sdw,
2440f9138e7SPierre-Louis Bossart 			    &intel_set_s_datamode_fops);
2450f9138e7SPierre-Louis Bossart 
24679ee6631SPierre-Louis Bossart 	sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
24779ee6631SPierre-Louis Bossart }
24879ee6631SPierre-Louis Bossart 
24979ee6631SPierre-Louis Bossart static void intel_debugfs_exit(struct sdw_intel *sdw)
25079ee6631SPierre-Louis Bossart {
25179ee6631SPierre-Louis Bossart 	debugfs_remove_recursive(sdw->debugfs);
25279ee6631SPierre-Louis Bossart }
25379ee6631SPierre-Louis Bossart #else
25479ee6631SPierre-Louis Bossart static void intel_debugfs_init(struct sdw_intel *sdw) {}
25579ee6631SPierre-Louis Bossart static void intel_debugfs_exit(struct sdw_intel *sdw) {}
25679ee6631SPierre-Louis Bossart #endif /* CONFIG_DEBUG_FS */
25779ee6631SPierre-Louis Bossart 
25879ee6631SPierre-Louis Bossart /*
25971bb8a1bSVinod Koul  * shim ops
26071bb8a1bSVinod Koul  */
26171bb8a1bSVinod Koul 
26271bb8a1bSVinod Koul static int intel_link_power_up(struct sdw_intel *sdw)
26371bb8a1bSVinod Koul {
26471bb8a1bSVinod Koul 	unsigned int link_id = sdw->instance;
2652523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
2664a17c441SPierre-Louis Bossart 	u32 *shim_mask = sdw->link_res->shim_mask;
2674a17c441SPierre-Louis Bossart 	struct sdw_bus *bus = &sdw->cdns.bus;
2684a17c441SPierre-Louis Bossart 	struct sdw_master_prop *prop = &bus->prop;
2695ee74eb2SPierre-Louis Bossart 	u32 spa_mask, cpa_mask;
2705ee74eb2SPierre-Louis Bossart 	u32 link_control;
2714a17c441SPierre-Louis Bossart 	int ret = 0;
2724a17c441SPierre-Louis Bossart 	u32 syncprd;
2734a17c441SPierre-Louis Bossart 	u32 sync_reg;
2744a17c441SPierre-Louis Bossart 
2754a17c441SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
2764a17c441SPierre-Louis Bossart 
2774a17c441SPierre-Louis Bossart 	/*
2784a17c441SPierre-Louis Bossart 	 * The hardware relies on an internal counter, typically 4kHz,
2794a17c441SPierre-Louis Bossart 	 * to generate the SoundWire SSP - which defines a 'safe'
2804a17c441SPierre-Louis Bossart 	 * synchronization point between commands and audio transport
2814a17c441SPierre-Louis Bossart 	 * and allows for multi link synchronization. The SYNCPRD value
2824a17c441SPierre-Louis Bossart 	 * is only dependent on the oscillator clock provided to
2834a17c441SPierre-Louis Bossart 	 * the IP, so adjust based on _DSD properties reported in DSDT
2844a17c441SPierre-Louis Bossart 	 * tables. The values reported are based on either 24MHz
2854a17c441SPierre-Louis Bossart 	 * (CNL/CML) or 38.4 MHz (ICL/TGL+).
2864a17c441SPierre-Louis Bossart 	 */
2874a17c441SPierre-Louis Bossart 	if (prop->mclk_freq % 6000000)
2884a17c441SPierre-Louis Bossart 		syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
2894a17c441SPierre-Louis Bossart 	else
2904a17c441SPierre-Louis Bossart 		syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24;
2914a17c441SPierre-Louis Bossart 
2924a17c441SPierre-Louis Bossart 	if (!*shim_mask) {
293*63198aaaSPierre-Louis Bossart 		dev_dbg(sdw->cdns.dev, "powering up all links\n");
2945ee74eb2SPierre-Louis Bossart 
2954a17c441SPierre-Louis Bossart 		/* we first need to program the SyncPRD/CPU registers */
2964a17c441SPierre-Louis Bossart 		dev_dbg(sdw->cdns.dev,
297*63198aaaSPierre-Louis Bossart 			"first link up, programming SYNCPRD\n");
2984a17c441SPierre-Louis Bossart 
2994a17c441SPierre-Louis Bossart 		/* set SyncPRD period */
3004a17c441SPierre-Louis Bossart 		sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
301f067c925SVinod Koul 		u32p_replace_bits(&sync_reg, syncprd, SDW_SHIM_SYNC_SYNCPRD);
3024a17c441SPierre-Louis Bossart 
3034a17c441SPierre-Louis Bossart 		/* Set SyncCPU bit */
3044a17c441SPierre-Louis Bossart 		sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
3054a17c441SPierre-Louis Bossart 		intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
30671bb8a1bSVinod Koul 
30771bb8a1bSVinod Koul 		/* Link power up sequence */
30871bb8a1bSVinod Koul 		link_control = intel_readl(shim, SDW_SHIM_LCTL);
3095ee74eb2SPierre-Louis Bossart 
3105ee74eb2SPierre-Louis Bossart 		/* only power-up enabled links */
3113b4979caSVinod Koul 		spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, sdw->link_res->link_mask);
3123b4979caSVinod Koul 		cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
3135ee74eb2SPierre-Louis Bossart 
31471bb8a1bSVinod Koul 		link_control |=  spa_mask;
31571bb8a1bSVinod Koul 
31671bb8a1bSVinod Koul 		ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
3174a17c441SPierre-Louis Bossart 		if (ret < 0) {
3184a17c441SPierre-Louis Bossart 			dev_err(sdw->cdns.dev, "Failed to power up link: %d\n", ret);
3194a17c441SPierre-Louis Bossart 			goto out;
32071bb8a1bSVinod Koul 		}
32171bb8a1bSVinod Koul 
3224a17c441SPierre-Louis Bossart 		/* SyncCPU will change once link is active */
3234a17c441SPierre-Louis Bossart 		ret = intel_wait_bit(shim, SDW_SHIM_SYNC,
3244a17c441SPierre-Louis Bossart 				     SDW_SHIM_SYNC_SYNCCPU, 0);
3254a17c441SPierre-Louis Bossart 		if (ret < 0) {
3264a17c441SPierre-Louis Bossart 			dev_err(sdw->cdns.dev,
3274a17c441SPierre-Louis Bossart 				"Failed to set SHIM_SYNC: %d\n", ret);
3284a17c441SPierre-Louis Bossart 			goto out;
3294a17c441SPierre-Louis Bossart 		}
3304a17c441SPierre-Louis Bossart 	}
3314a17c441SPierre-Louis Bossart 
3324a17c441SPierre-Louis Bossart 	*shim_mask |= BIT(link_id);
3334a17c441SPierre-Louis Bossart 
3344a17c441SPierre-Louis Bossart 	sdw->cdns.link_up = true;
3354a17c441SPierre-Louis Bossart out:
3364a17c441SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
3374a17c441SPierre-Louis Bossart 
3384a17c441SPierre-Louis Bossart 	return ret;
3394a17c441SPierre-Louis Bossart }
3404a17c441SPierre-Louis Bossart 
3414a17c441SPierre-Louis Bossart /* this needs to be called with shim_lock */
3424a17c441SPierre-Louis Bossart static void intel_shim_glue_to_master_ip(struct sdw_intel *sdw)
34371bb8a1bSVinod Koul {
3442523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
34571bb8a1bSVinod Koul 	unsigned int link_id = sdw->instance;
3464a17c441SPierre-Louis Bossart 	u16 ioctl;
34771bb8a1bSVinod Koul 
34871bb8a1bSVinod Koul 	/* Switch to MIP from Glue logic */
34971bb8a1bSVinod Koul 	ioctl = intel_readw(shim,  SDW_SHIM_IOCTL(link_id));
35071bb8a1bSVinod Koul 
35171bb8a1bSVinod Koul 	ioctl &= ~(SDW_SHIM_IOCTL_DOE);
35271bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
3534a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
35471bb8a1bSVinod Koul 
35571bb8a1bSVinod Koul 	ioctl &= ~(SDW_SHIM_IOCTL_DO);
35671bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
3574a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
35871bb8a1bSVinod Koul 
35971bb8a1bSVinod Koul 	ioctl |= (SDW_SHIM_IOCTL_MIF);
36071bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
3614a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
36271bb8a1bSVinod Koul 
36371bb8a1bSVinod Koul 	ioctl &= ~(SDW_SHIM_IOCTL_BKE);
36471bb8a1bSVinod Koul 	ioctl &= ~(SDW_SHIM_IOCTL_COE);
36571bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
3664a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
3674a17c441SPierre-Louis Bossart 
3684a17c441SPierre-Louis Bossart 	/* at this point Master IP has full control of the I/Os */
3694a17c441SPierre-Louis Bossart }
3704a17c441SPierre-Louis Bossart 
3714a17c441SPierre-Louis Bossart /* this needs to be called with shim_lock */
3724a17c441SPierre-Louis Bossart static void intel_shim_master_ip_to_glue(struct sdw_intel *sdw)
3734a17c441SPierre-Louis Bossart {
3744a17c441SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
3754a17c441SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
3764a17c441SPierre-Louis Bossart 	u16 ioctl;
3774a17c441SPierre-Louis Bossart 
3784a17c441SPierre-Louis Bossart 	/* Glue logic */
3794a17c441SPierre-Louis Bossart 	ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
3804a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_BKE;
3814a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_COE;
3824a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
3834a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
3844a17c441SPierre-Louis Bossart 
3854a17c441SPierre-Louis Bossart 	ioctl &= ~(SDW_SHIM_IOCTL_MIF);
3864a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
3874a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
3884a17c441SPierre-Louis Bossart 
3894a17c441SPierre-Louis Bossart 	/* at this point Integration Glue has full control of the I/Os */
3904a17c441SPierre-Louis Bossart }
3914a17c441SPierre-Louis Bossart 
3924a17c441SPierre-Louis Bossart static int intel_shim_init(struct sdw_intel *sdw, bool clock_stop)
3934a17c441SPierre-Louis Bossart {
3944a17c441SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
3954a17c441SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
3964a17c441SPierre-Louis Bossart 	int ret = 0;
3974a17c441SPierre-Louis Bossart 	u16 ioctl = 0, act = 0;
3984a17c441SPierre-Louis Bossart 
3994a17c441SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
4004a17c441SPierre-Louis Bossart 
4014a17c441SPierre-Louis Bossart 	/* Initialize Shim */
4024a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_BKE;
4034a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
4044a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
4054a17c441SPierre-Louis Bossart 
4064a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_WPDD;
4074a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
4084a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
4094a17c441SPierre-Louis Bossart 
4104a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_DO;
4114a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
4124a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
4134a17c441SPierre-Louis Bossart 
4144a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_DOE;
4154a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
4164a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
4174a17c441SPierre-Louis Bossart 
4184a17c441SPierre-Louis Bossart 	intel_shim_glue_to_master_ip(sdw);
41971bb8a1bSVinod Koul 
420f067c925SVinod Koul 	u16p_replace_bits(&act, 0x1, SDW_SHIM_CTMCTL_DOAIS);
42171bb8a1bSVinod Koul 	act |= SDW_SHIM_CTMCTL_DACTQE;
42271bb8a1bSVinod Koul 	act |= SDW_SHIM_CTMCTL_DODS;
42371bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
4244a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
42571bb8a1bSVinod Koul 
4264a17c441SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
42771bb8a1bSVinod Koul 
42871bb8a1bSVinod Koul 	return ret;
42971bb8a1bSVinod Koul }
43071bb8a1bSVinod Koul 
431ab2c9132SRander Wang static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
4324a17c441SPierre-Louis Bossart {
4334a17c441SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
4344a17c441SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
4354a17c441SPierre-Louis Bossart 	u16 wake_en, wake_sts;
4364a17c441SPierre-Louis Bossart 
4374a17c441SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
4384a17c441SPierre-Louis Bossart 	wake_en = intel_readw(shim, SDW_SHIM_WAKEEN);
4394a17c441SPierre-Louis Bossart 
4404a17c441SPierre-Louis Bossart 	if (wake_enable) {
4414a17c441SPierre-Louis Bossart 		/* Enable the wakeup */
4424a17c441SPierre-Louis Bossart 		wake_en |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
4434a17c441SPierre-Louis Bossart 		intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
4444a17c441SPierre-Louis Bossart 	} else {
4454a17c441SPierre-Louis Bossart 		/* Disable the wake up interrupt */
4464a17c441SPierre-Louis Bossart 		wake_en &= ~(SDW_SHIM_WAKEEN_ENABLE << link_id);
4474a17c441SPierre-Louis Bossart 		intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
4484a17c441SPierre-Louis Bossart 
4494a17c441SPierre-Louis Bossart 		/* Clear wake status */
4504a17c441SPierre-Louis Bossart 		wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
4513957db3aSLibin Yang 		wake_sts |= (SDW_SHIM_WAKESTS_STATUS << link_id);
4523957db3aSLibin Yang 		intel_writew(shim, SDW_SHIM_WAKESTS, wake_sts);
4534a17c441SPierre-Louis Bossart 	}
4544a17c441SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
4554a17c441SPierre-Louis Bossart }
4564a17c441SPierre-Louis Bossart 
4579b3b4b3fSPierre-Louis Bossart static int intel_link_power_down(struct sdw_intel *sdw)
4584a17c441SPierre-Louis Bossart {
4595ee74eb2SPierre-Louis Bossart 	u32 link_control, spa_mask, cpa_mask;
4604a17c441SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
4614a17c441SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
4624a17c441SPierre-Louis Bossart 	u32 *shim_mask = sdw->link_res->shim_mask;
4634a17c441SPierre-Louis Bossart 	int ret = 0;
4644a17c441SPierre-Louis Bossart 
4654a17c441SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
4664a17c441SPierre-Louis Bossart 
4674a17c441SPierre-Louis Bossart 	if (!(*shim_mask & BIT(link_id)))
4684a17c441SPierre-Louis Bossart 		dev_err(sdw->cdns.dev,
4694a17c441SPierre-Louis Bossart 			"%s: Unbalanced power-up/down calls\n", __func__);
4704a17c441SPierre-Louis Bossart 
471ea6942daSPierre-Louis Bossart 	sdw->cdns.link_up = false;
472ea6942daSPierre-Louis Bossart 
473ea6942daSPierre-Louis Bossart 	intel_shim_master_ip_to_glue(sdw);
474ea6942daSPierre-Louis Bossart 
4754a17c441SPierre-Louis Bossart 	*shim_mask &= ~BIT(link_id);
4764a17c441SPierre-Louis Bossart 
4775ee74eb2SPierre-Louis Bossart 	if (!*shim_mask) {
4785ee74eb2SPierre-Louis Bossart 
479*63198aaaSPierre-Louis Bossart 		dev_dbg(sdw->cdns.dev, "powering down all links\n");
4805ee74eb2SPierre-Louis Bossart 
4815ee74eb2SPierre-Louis Bossart 		/* Link power down sequence */
4825ee74eb2SPierre-Louis Bossart 		link_control = intel_readl(shim, SDW_SHIM_LCTL);
4835ee74eb2SPierre-Louis Bossart 
4845ee74eb2SPierre-Louis Bossart 		/* only power-down enabled links */
4853b4979caSVinod Koul 		spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, ~sdw->link_res->link_mask);
4863b4979caSVinod Koul 		cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
4875ee74eb2SPierre-Louis Bossart 
4885ee74eb2SPierre-Louis Bossart 		link_control &=  spa_mask;
4895ee74eb2SPierre-Louis Bossart 
4905ee74eb2SPierre-Louis Bossart 		ret = intel_clear_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
491ea6942daSPierre-Louis Bossart 		if (ret < 0) {
492ea6942daSPierre-Louis Bossart 			dev_err(sdw->cdns.dev, "%s: could not power down link\n", __func__);
493ea6942daSPierre-Louis Bossart 
494ea6942daSPierre-Louis Bossart 			/*
495ea6942daSPierre-Louis Bossart 			 * we leave the sdw->cdns.link_up flag as false since we've disabled
496ea6942daSPierre-Louis Bossart 			 * the link at this point and cannot handle interrupts any longer.
497ea6942daSPierre-Louis Bossart 			 */
498ea6942daSPierre-Louis Bossart 		}
4995ee74eb2SPierre-Louis Bossart 	}
5005ee74eb2SPierre-Louis Bossart 
5014a17c441SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
5024a17c441SPierre-Louis Bossart 
5034a17c441SPierre-Louis Bossart 	return ret;
5045ee74eb2SPierre-Louis Bossart }
5054a17c441SPierre-Louis Bossart 
50602629e45SPierre-Louis Bossart static void intel_shim_sync_arm(struct sdw_intel *sdw)
50702629e45SPierre-Louis Bossart {
50802629e45SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
50902629e45SPierre-Louis Bossart 	u32 sync_reg;
51002629e45SPierre-Louis Bossart 
51102629e45SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
51202629e45SPierre-Louis Bossart 
51302629e45SPierre-Louis Bossart 	/* update SYNC register */
51402629e45SPierre-Louis Bossart 	sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
51502629e45SPierre-Louis Bossart 	sync_reg |= (SDW_SHIM_SYNC_CMDSYNC << sdw->instance);
51602629e45SPierre-Louis Bossart 	intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
51702629e45SPierre-Louis Bossart 
51802629e45SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
51902629e45SPierre-Louis Bossart }
52002629e45SPierre-Louis Bossart 
521437e3289SPierre-Louis Bossart static int intel_shim_sync_go_unlocked(struct sdw_intel *sdw)
522437e3289SPierre-Louis Bossart {
523437e3289SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
524437e3289SPierre-Louis Bossart 	u32 sync_reg;
525437e3289SPierre-Louis Bossart 	int ret;
526437e3289SPierre-Louis Bossart 
527437e3289SPierre-Louis Bossart 	/* Read SYNC register */
528437e3289SPierre-Louis Bossart 	sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
529437e3289SPierre-Louis Bossart 
530437e3289SPierre-Louis Bossart 	/*
531437e3289SPierre-Louis Bossart 	 * Set SyncGO bit to synchronously trigger a bank switch for
532437e3289SPierre-Louis Bossart 	 * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
533437e3289SPierre-Louis Bossart 	 * the Masters.
534437e3289SPierre-Louis Bossart 	 */
535437e3289SPierre-Louis Bossart 	sync_reg |= SDW_SHIM_SYNC_SYNCGO;
536437e3289SPierre-Louis Bossart 
537437e3289SPierre-Louis Bossart 	ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
538437e3289SPierre-Louis Bossart 			      SDW_SHIM_SYNC_SYNCGO);
539437e3289SPierre-Louis Bossart 
540437e3289SPierre-Louis Bossart 	if (ret < 0)
541437e3289SPierre-Louis Bossart 		dev_err(sdw->cdns.dev, "SyncGO clear failed: %d\n", ret);
54271bb8a1bSVinod Koul 
54371bb8a1bSVinod Koul 	return ret;
54471bb8a1bSVinod Koul }
54571bb8a1bSVinod Koul 
546857a7c42SPierre-Louis Bossart static int intel_shim_sync_go(struct sdw_intel *sdw)
547857a7c42SPierre-Louis Bossart {
548857a7c42SPierre-Louis Bossart 	int ret;
549857a7c42SPierre-Louis Bossart 
550857a7c42SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
551857a7c42SPierre-Louis Bossart 
552857a7c42SPierre-Louis Bossart 	ret = intel_shim_sync_go_unlocked(sdw);
553857a7c42SPierre-Louis Bossart 
554857a7c42SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
555857a7c42SPierre-Louis Bossart 
556857a7c42SPierre-Louis Bossart 	return ret;
557857a7c42SPierre-Louis Bossart }
558857a7c42SPierre-Louis Bossart 
55937a2d22bSVinod Koul /*
56037a2d22bSVinod Koul  * PDI routines
56137a2d22bSVinod Koul  */
56237a2d22bSVinod Koul static void intel_pdi_init(struct sdw_intel *sdw,
56337a2d22bSVinod Koul 			   struct sdw_cdns_stream_config *config)
56437a2d22bSVinod Koul {
5652523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
56637a2d22bSVinod Koul 	unsigned int link_id = sdw->instance;
56763a6aa96SPierre-Louis Bossart 	int pcm_cap;
56837a2d22bSVinod Koul 
56937a2d22bSVinod Koul 	/* PCM Stream Capability */
57037a2d22bSVinod Koul 	pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
57137a2d22bSVinod Koul 
5723b4979caSVinod Koul 	config->pcm_bd = FIELD_GET(SDW_SHIM_PCMSCAP_BSS, pcm_cap);
5733b4979caSVinod Koul 	config->pcm_in = FIELD_GET(SDW_SHIM_PCMSCAP_ISS, pcm_cap);
5743b4979caSVinod Koul 	config->pcm_out = FIELD_GET(SDW_SHIM_PCMSCAP_OSS, pcm_cap);
57537a2d22bSVinod Koul 
576121f4361SPierre-Louis Bossart 	dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
577121f4361SPierre-Louis Bossart 		config->pcm_bd, config->pcm_in, config->pcm_out);
57837a2d22bSVinod Koul }
57937a2d22bSVinod Koul 
58037a2d22bSVinod Koul static int
58163a6aa96SPierre-Louis Bossart intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num)
58237a2d22bSVinod Koul {
5832523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
58437a2d22bSVinod Koul 	unsigned int link_id = sdw->instance;
58537a2d22bSVinod Koul 	int count;
58637a2d22bSVinod Koul 
58737a2d22bSVinod Koul 	count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
58818046335SPierre-Louis Bossart 
58918046335SPierre-Louis Bossart 	/*
59018046335SPierre-Louis Bossart 	 * WORKAROUND: on all existing Intel controllers, pdi
59118046335SPierre-Louis Bossart 	 * number 2 reports channel count as 1 even though it
59218046335SPierre-Louis Bossart 	 * supports 8 channels. Performing hardcoding for pdi
59318046335SPierre-Louis Bossart 	 * number 2.
59418046335SPierre-Louis Bossart 	 */
59518046335SPierre-Louis Bossart 	if (pdi_num == 2)
59618046335SPierre-Louis Bossart 		count = 7;
59718046335SPierre-Louis Bossart 
59837a2d22bSVinod Koul 	/* zero based values for channel count in register */
59937a2d22bSVinod Koul 	count++;
60037a2d22bSVinod Koul 
60137a2d22bSVinod Koul 	return count;
60237a2d22bSVinod Koul }
60337a2d22bSVinod Koul 
60437a2d22bSVinod Koul static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
60537a2d22bSVinod Koul 				   struct sdw_cdns_pdi *pdi,
60637a2d22bSVinod Koul 				   unsigned int num_pdi,
60763a6aa96SPierre-Louis Bossart 				   unsigned int *num_ch)
60837a2d22bSVinod Koul {
60937a2d22bSVinod Koul 	int i, ch_count = 0;
61037a2d22bSVinod Koul 
61137a2d22bSVinod Koul 	for (i = 0; i < num_pdi; i++) {
61263a6aa96SPierre-Louis Bossart 		pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num);
61337a2d22bSVinod Koul 		ch_count += pdi->ch_count;
61437a2d22bSVinod Koul 		pdi++;
61537a2d22bSVinod Koul 	}
61637a2d22bSVinod Koul 
61737a2d22bSVinod Koul 	*num_ch = ch_count;
61837a2d22bSVinod Koul 	return 0;
61937a2d22bSVinod Koul }
62037a2d22bSVinod Koul 
62137a2d22bSVinod Koul static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
62263a6aa96SPierre-Louis Bossart 				      struct sdw_cdns_streams *stream)
62337a2d22bSVinod Koul {
62437a2d22bSVinod Koul 	intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
62563a6aa96SPierre-Louis Bossart 				&stream->num_ch_bd);
62637a2d22bSVinod Koul 
62737a2d22bSVinod Koul 	intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
62863a6aa96SPierre-Louis Bossart 				&stream->num_ch_in);
62937a2d22bSVinod Koul 
63037a2d22bSVinod Koul 	intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
63163a6aa96SPierre-Louis Bossart 				&stream->num_ch_out);
63237a2d22bSVinod Koul 
63337a2d22bSVinod Koul 	return 0;
63437a2d22bSVinod Koul }
63537a2d22bSVinod Koul 
63637a2d22bSVinod Koul static int intel_pdi_ch_update(struct sdw_intel *sdw)
63737a2d22bSVinod Koul {
63863a6aa96SPierre-Louis Bossart 	intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm);
63937a2d22bSVinod Koul 
64037a2d22bSVinod Koul 	return 0;
64137a2d22bSVinod Koul }
64237a2d22bSVinod Koul 
64337a2d22bSVinod Koul static void
64437a2d22bSVinod Koul intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
64537a2d22bSVinod Koul {
6462523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
64737a2d22bSVinod Koul 	unsigned int link_id = sdw->instance;
64837a2d22bSVinod Koul 	int pdi_conf = 0;
64937a2d22bSVinod Koul 
650c134f914SPierre-Louis Bossart 	/* the Bulk and PCM streams are not contiguous */
651c134f914SPierre-Louis Bossart 	pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
652c134f914SPierre-Louis Bossart 	if (pdi->num >= 2)
653c134f914SPierre-Louis Bossart 		pdi->intel_alh_id += 2;
65437a2d22bSVinod Koul 
65537a2d22bSVinod Koul 	/*
65637a2d22bSVinod Koul 	 * Program stream parameters to stream SHIM register
65737a2d22bSVinod Koul 	 * This is applicable for PCM stream only.
65837a2d22bSVinod Koul 	 */
65937a2d22bSVinod Koul 	if (pdi->type != SDW_STREAM_PCM)
66037a2d22bSVinod Koul 		return;
66137a2d22bSVinod Koul 
66237a2d22bSVinod Koul 	if (pdi->dir == SDW_DATA_DIR_RX)
66337a2d22bSVinod Koul 		pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
66437a2d22bSVinod Koul 	else
66537a2d22bSVinod Koul 		pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
66637a2d22bSVinod Koul 
667f067c925SVinod Koul 	u32p_replace_bits(&pdi_conf, pdi->intel_alh_id, SDW_SHIM_PCMSYCM_STREAM);
668f067c925SVinod Koul 	u32p_replace_bits(&pdi_conf, pdi->l_ch_num, SDW_SHIM_PCMSYCM_LCHN);
669f067c925SVinod Koul 	u32p_replace_bits(&pdi_conf, pdi->h_ch_num, SDW_SHIM_PCMSYCM_HCHN);
67037a2d22bSVinod Koul 
67137a2d22bSVinod Koul 	intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
67237a2d22bSVinod Koul }
67337a2d22bSVinod Koul 
67437a2d22bSVinod Koul static void
67537a2d22bSVinod Koul intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
67637a2d22bSVinod Koul {
6772523486bSPierre-Louis Bossart 	void __iomem *alh = sdw->link_res->alh;
67837a2d22bSVinod Koul 	unsigned int link_id = sdw->instance;
67937a2d22bSVinod Koul 	unsigned int conf;
68037a2d22bSVinod Koul 
681c134f914SPierre-Louis Bossart 	/* the Bulk and PCM streams are not contiguous */
682c134f914SPierre-Louis Bossart 	pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
683c134f914SPierre-Louis Bossart 	if (pdi->num >= 2)
684c134f914SPierre-Louis Bossart 		pdi->intel_alh_id += 2;
68537a2d22bSVinod Koul 
68637a2d22bSVinod Koul 	/* Program Stream config ALH register */
68737a2d22bSVinod Koul 	conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
68837a2d22bSVinod Koul 
689f067c925SVinod Koul 	u32p_replace_bits(&conf, SDW_ALH_STRMZCFG_DMAT_VAL, SDW_ALH_STRMZCFG_DMAT);
690f067c925SVinod Koul 	u32p_replace_bits(&conf, pdi->ch_count - 1, SDW_ALH_STRMZCFG_CHN);
69137a2d22bSVinod Koul 
69237a2d22bSVinod Koul 	intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
69337a2d22bSVinod Koul }
69437a2d22bSVinod Koul 
6954b206d34SRander Wang static int intel_params_stream(struct sdw_intel *sdw,
696b86947b5SPierre-Louis Bossart 			       int stream,
697c46302ecSVinod Koul 			       struct snd_soc_dai *dai,
6984b206d34SRander Wang 			       struct snd_pcm_hw_params *hw_params,
6994b206d34SRander Wang 			       int link_id, int alh_stream_id)
700c46302ecSVinod Koul {
7012523486bSPierre-Louis Bossart 	struct sdw_intel_link_res *res = sdw->link_res;
7024b206d34SRander Wang 	struct sdw_intel_stream_params_data params_data;
70305c8afe4SPierre-Louis Bossart 
704b86947b5SPierre-Louis Bossart 	params_data.stream = stream; /* direction */
7054b206d34SRander Wang 	params_data.dai = dai;
7064b206d34SRander Wang 	params_data.hw_params = hw_params;
7074b206d34SRander Wang 	params_data.link_id = link_id;
7084b206d34SRander Wang 	params_data.alh_stream_id = alh_stream_id;
709c46302ecSVinod Koul 
7104b206d34SRander Wang 	if (res->ops && res->ops->params_stream && res->dev)
7114b206d34SRander Wang 		return res->ops->params_stream(res->dev,
7124b206d34SRander Wang 					       &params_data);
713c46302ecSVinod Koul 	return -EIO;
714c46302ecSVinod Koul }
715c46302ecSVinod Koul 
716eff346f2SPierre-Louis Bossart static int intel_free_stream(struct sdw_intel *sdw,
717b86947b5SPierre-Louis Bossart 			     int stream,
718eff346f2SPierre-Louis Bossart 			     struct snd_soc_dai *dai,
719eff346f2SPierre-Louis Bossart 			     int link_id)
720eff346f2SPierre-Louis Bossart {
721eff346f2SPierre-Louis Bossart 	struct sdw_intel_link_res *res = sdw->link_res;
722eff346f2SPierre-Louis Bossart 	struct sdw_intel_stream_free_data free_data;
723eff346f2SPierre-Louis Bossart 
724b86947b5SPierre-Louis Bossart 	free_data.stream = stream; /* direction */
725eff346f2SPierre-Louis Bossart 	free_data.dai = dai;
726eff346f2SPierre-Louis Bossart 	free_data.link_id = link_id;
727eff346f2SPierre-Louis Bossart 
728eff346f2SPierre-Louis Bossart 	if (res->ops && res->ops->free_stream && res->dev)
729eff346f2SPierre-Louis Bossart 		return res->ops->free_stream(res->dev,
730eff346f2SPierre-Louis Bossart 					     &free_data);
731eff346f2SPierre-Louis Bossart 
732eff346f2SPierre-Louis Bossart 	return 0;
733eff346f2SPierre-Louis Bossart }
734eff346f2SPierre-Louis Bossart 
735c46302ecSVinod Koul /*
73630246e2dSShreyas NC  * bank switch routines
73730246e2dSShreyas NC  */
73830246e2dSShreyas NC 
73930246e2dSShreyas NC static int intel_pre_bank_switch(struct sdw_bus *bus)
74030246e2dSShreyas NC {
74130246e2dSShreyas NC 	struct sdw_cdns *cdns = bus_to_cdns(bus);
74230246e2dSShreyas NC 	struct sdw_intel *sdw = cdns_to_intel(cdns);
74330246e2dSShreyas NC 
74430246e2dSShreyas NC 	/* Write to register only for multi-link */
74530246e2dSShreyas NC 	if (!bus->multi_link)
74630246e2dSShreyas NC 		return 0;
74730246e2dSShreyas NC 
74802629e45SPierre-Louis Bossart 	intel_shim_sync_arm(sdw);
74930246e2dSShreyas NC 
75030246e2dSShreyas NC 	return 0;
75130246e2dSShreyas NC }
75230246e2dSShreyas NC 
75330246e2dSShreyas NC static int intel_post_bank_switch(struct sdw_bus *bus)
75430246e2dSShreyas NC {
75530246e2dSShreyas NC 	struct sdw_cdns *cdns = bus_to_cdns(bus);
75630246e2dSShreyas NC 	struct sdw_intel *sdw = cdns_to_intel(cdns);
7572523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
75830246e2dSShreyas NC 	int sync_reg, ret;
75930246e2dSShreyas NC 
76030246e2dSShreyas NC 	/* Write to register only for multi-link */
76130246e2dSShreyas NC 	if (!bus->multi_link)
76230246e2dSShreyas NC 		return 0;
76330246e2dSShreyas NC 
7644a17c441SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
7654a17c441SPierre-Louis Bossart 
76630246e2dSShreyas NC 	/* Read SYNC register */
76730246e2dSShreyas NC 	sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
76830246e2dSShreyas NC 
76930246e2dSShreyas NC 	/*
77030246e2dSShreyas NC 	 * post_bank_switch() ops is called from the bus in loop for
77130246e2dSShreyas NC 	 * all the Masters in the steam with the expectation that
77230246e2dSShreyas NC 	 * we trigger the bankswitch for the only first Master in the list
77330246e2dSShreyas NC 	 * and do nothing for the other Masters
77430246e2dSShreyas NC 	 *
77530246e2dSShreyas NC 	 * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master.
77630246e2dSShreyas NC 	 */
7774a17c441SPierre-Louis Bossart 	if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK)) {
7784a17c441SPierre-Louis Bossart 		ret = 0;
7794a17c441SPierre-Louis Bossart 		goto unlock;
7804a17c441SPierre-Louis Bossart 	}
78130246e2dSShreyas NC 
782437e3289SPierre-Louis Bossart 	ret = intel_shim_sync_go_unlocked(sdw);
7834a17c441SPierre-Louis Bossart unlock:
7844a17c441SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
78530246e2dSShreyas NC 
78630246e2dSShreyas NC 	if (ret < 0)
78717ed5befSPierre-Louis Bossart 		dev_err(sdw->cdns.dev, "Post bank switch failed: %d\n", ret);
78830246e2dSShreyas NC 
78930246e2dSShreyas NC 	return ret;
79030246e2dSShreyas NC }
79130246e2dSShreyas NC 
79230246e2dSShreyas NC /*
793c46302ecSVinod Koul  * DAI routines
794c46302ecSVinod Koul  */
795c46302ecSVinod Koul 
7965e7484d0SRander Wang static int intel_startup(struct snd_pcm_substream *substream,
7975e7484d0SRander Wang 			 struct snd_soc_dai *dai)
7985e7484d0SRander Wang {
799ebf878edSPierre-Louis Bossart 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
800ebf878edSPierre-Louis Bossart 	int ret;
801ebf878edSPierre-Louis Bossart 
802fff1fd9cSPierre-Louis Bossart 	ret = pm_runtime_resume_and_get(cdns->dev);
803ebf878edSPierre-Louis Bossart 	if (ret < 0 && ret != -EACCES) {
804ebf878edSPierre-Louis Bossart 		dev_err_ratelimited(cdns->dev,
805fff1fd9cSPierre-Louis Bossart 				    "pm_runtime_resume_and_get failed in %s, ret %d\n",
806ebf878edSPierre-Louis Bossart 				    __func__, ret);
807ebf878edSPierre-Louis Bossart 		return ret;
808ebf878edSPierre-Louis Bossart 	}
809ff16d1e5SPierre-Louis Bossart 	return 0;
8105e7484d0SRander Wang }
8115e7484d0SRander Wang 
812c46302ecSVinod Koul static int intel_hw_params(struct snd_pcm_substream *substream,
813c46302ecSVinod Koul 			   struct snd_pcm_hw_params *params,
814c46302ecSVinod Koul 			   struct snd_soc_dai *dai)
815c46302ecSVinod Koul {
816c46302ecSVinod Koul 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
817c46302ecSVinod Koul 	struct sdw_intel *sdw = cdns_to_intel(cdns);
818c46302ecSVinod Koul 	struct sdw_cdns_dma_data *dma;
81957a34790SPierre-Louis Bossart 	struct sdw_cdns_pdi *pdi;
820c46302ecSVinod Koul 	struct sdw_stream_config sconfig;
821c46302ecSVinod Koul 	struct sdw_port_config *pconfig;
82257a34790SPierre-Louis Bossart 	int ch, dir;
82357a34790SPierre-Louis Bossart 	int ret;
824c46302ecSVinod Koul 
825c46302ecSVinod Koul 	dma = snd_soc_dai_get_dma_data(dai, substream);
826c46302ecSVinod Koul 	if (!dma)
827c46302ecSVinod Koul 		return -EIO;
828c46302ecSVinod Koul 
829c46302ecSVinod Koul 	ch = params_channels(params);
830c46302ecSVinod Koul 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
831c46302ecSVinod Koul 		dir = SDW_DATA_DIR_RX;
832c46302ecSVinod Koul 	else
833c46302ecSVinod Koul 		dir = SDW_DATA_DIR_TX;
834c46302ecSVinod Koul 
8351b53385eSBard Liao 	pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
836c46302ecSVinod Koul 
83757a34790SPierre-Louis Bossart 	if (!pdi) {
838c46302ecSVinod Koul 		ret = -EINVAL;
83957a34790SPierre-Louis Bossart 		goto error;
840c46302ecSVinod Koul 	}
84157a34790SPierre-Louis Bossart 
84257a34790SPierre-Louis Bossart 	/* do run-time configurations for SHIM, ALH and PDI/PORT */
84357a34790SPierre-Louis Bossart 	intel_pdi_shim_configure(sdw, pdi);
84457a34790SPierre-Louis Bossart 	intel_pdi_alh_configure(sdw, pdi);
84557a34790SPierre-Louis Bossart 	sdw_cdns_config_stream(cdns, ch, dir, pdi);
84657a34790SPierre-Louis Bossart 
847a5a0239cSBard Liao 	/* store pdi and hw_params, may be needed in prepare step */
8488ddeafb9SRanjani Sridharan 	dma->paused = false;
849a5a0239cSBard Liao 	dma->suspended = false;
850a5a0239cSBard Liao 	dma->pdi = pdi;
851a5a0239cSBard Liao 	dma->hw_params = params;
852c46302ecSVinod Koul 
853c46302ecSVinod Koul 	/* Inform DSP about PDI stream number */
854b86947b5SPierre-Louis Bossart 	ret = intel_params_stream(sdw, substream->stream, dai, params,
8554b206d34SRander Wang 				  sdw->instance,
85657a34790SPierre-Louis Bossart 				  pdi->intel_alh_id);
857c46302ecSVinod Koul 	if (ret)
85857a34790SPierre-Louis Bossart 		goto error;
859c46302ecSVinod Koul 
860c46302ecSVinod Koul 	sconfig.direction = dir;
861c46302ecSVinod Koul 	sconfig.ch_count = ch;
862c46302ecSVinod Koul 	sconfig.frame_rate = params_rate(params);
863c46302ecSVinod Koul 	sconfig.type = dma->stream_type;
864c46302ecSVinod Koul 
865c46302ecSVinod Koul 	sconfig.bps = snd_pcm_format_width(params_format(params));
866c46302ecSVinod Koul 
867c46302ecSVinod Koul 	/* Port configuration */
868235ae89bSZheng Yongjun 	pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL);
869c46302ecSVinod Koul 	if (!pconfig) {
870c46302ecSVinod Koul 		ret =  -ENOMEM;
87157a34790SPierre-Louis Bossart 		goto error;
872c46302ecSVinod Koul 	}
873c46302ecSVinod Koul 
87457a34790SPierre-Louis Bossart 	pconfig->num = pdi->num;
87557a34790SPierre-Louis Bossart 	pconfig->ch_mask = (1 << ch) - 1;
876c46302ecSVinod Koul 
877c46302ecSVinod Koul 	ret = sdw_stream_add_master(&cdns->bus, &sconfig,
87857a34790SPierre-Louis Bossart 				    pconfig, 1, dma->stream);
87957a34790SPierre-Louis Bossart 	if (ret)
88017ed5befSPierre-Louis Bossart 		dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
881c46302ecSVinod Koul 
882c46302ecSVinod Koul 	kfree(pconfig);
88357a34790SPierre-Louis Bossart error:
884c46302ecSVinod Koul 	return ret;
885c46302ecSVinod Koul }
886c46302ecSVinod Koul 
88727b198f4SRander Wang static int intel_prepare(struct snd_pcm_substream *substream,
88827b198f4SRander Wang 			 struct snd_soc_dai *dai)
88927b198f4SRander Wang {
890a5a0239cSBard Liao 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
891a5a0239cSBard Liao 	struct sdw_intel *sdw = cdns_to_intel(cdns);
89227b198f4SRander Wang 	struct sdw_cdns_dma_data *dma;
893a5a0239cSBard Liao 	int ch, dir;
894244eb888SPierre-Louis Bossart 	int ret = 0;
89527b198f4SRander Wang 
89627b198f4SRander Wang 	dma = snd_soc_dai_get_dma_data(dai, substream);
89727b198f4SRander Wang 	if (!dma) {
8984e3ea93eSPierre-Louis Bossart 		dev_err(dai->dev, "failed to get dma data in %s\n",
89927b198f4SRander Wang 			__func__);
90027b198f4SRander Wang 		return -EIO;
90127b198f4SRander Wang 	}
90227b198f4SRander Wang 
903a5a0239cSBard Liao 	if (dma->suspended) {
904a5a0239cSBard Liao 		dma->suspended = false;
905a5a0239cSBard Liao 
906a5a0239cSBard Liao 		/*
907a5a0239cSBard Liao 		 * .prepare() is called after system resume, where we
908a5a0239cSBard Liao 		 * need to reinitialize the SHIM/ALH/Cadence IP.
909a5a0239cSBard Liao 		 * .prepare() is also called to deal with underflows,
910a5a0239cSBard Liao 		 * but in those cases we cannot touch ALH/SHIM
911a5a0239cSBard Liao 		 * registers
912a5a0239cSBard Liao 		 */
913a5a0239cSBard Liao 
914a5a0239cSBard Liao 		/* configure stream */
915a5a0239cSBard Liao 		ch = params_channels(dma->hw_params);
916a5a0239cSBard Liao 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
917a5a0239cSBard Liao 			dir = SDW_DATA_DIR_RX;
918a5a0239cSBard Liao 		else
919a5a0239cSBard Liao 			dir = SDW_DATA_DIR_TX;
920a5a0239cSBard Liao 
921a5a0239cSBard Liao 		intel_pdi_shim_configure(sdw, dma->pdi);
922a5a0239cSBard Liao 		intel_pdi_alh_configure(sdw, dma->pdi);
923a5a0239cSBard Liao 		sdw_cdns_config_stream(cdns, ch, dir, dma->pdi);
924a5a0239cSBard Liao 
925a5a0239cSBard Liao 		/* Inform DSP about PDI stream number */
926b86947b5SPierre-Louis Bossart 		ret = intel_params_stream(sdw, substream->stream, dai,
927a5a0239cSBard Liao 					  dma->hw_params,
928a5a0239cSBard Liao 					  sdw->instance,
929a5a0239cSBard Liao 					  dma->pdi->intel_alh_id);
930a5a0239cSBard Liao 	}
931a5a0239cSBard Liao 
932a5a0239cSBard Liao 	return ret;
93327b198f4SRander Wang }
93427b198f4SRander Wang 
935c46302ecSVinod Koul static int
936c46302ecSVinod Koul intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
937c46302ecSVinod Koul {
938c46302ecSVinod Koul 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
939eff346f2SPierre-Louis Bossart 	struct sdw_intel *sdw = cdns_to_intel(cdns);
940c46302ecSVinod Koul 	struct sdw_cdns_dma_data *dma;
941c46302ecSVinod Koul 	int ret;
942c46302ecSVinod Koul 
943c46302ecSVinod Koul 	dma = snd_soc_dai_get_dma_data(dai, substream);
944c46302ecSVinod Koul 	if (!dma)
945c46302ecSVinod Koul 		return -EIO;
946c46302ecSVinod Koul 
947244eb888SPierre-Louis Bossart 	/*
948244eb888SPierre-Louis Bossart 	 * The sdw stream state will transition to RELEASED when stream->
949244eb888SPierre-Louis Bossart 	 * master_list is empty. So the stream state will transition to
950244eb888SPierre-Louis Bossart 	 * DEPREPARED for the first cpu-dai and to RELEASED for the last
951244eb888SPierre-Louis Bossart 	 * cpu-dai.
952244eb888SPierre-Louis Bossart 	 */
953c46302ecSVinod Koul 	ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
954eff346f2SPierre-Louis Bossart 	if (ret < 0) {
95517ed5befSPierre-Louis Bossart 		dev_err(dai->dev, "remove master from stream %s failed: %d\n",
956c46302ecSVinod Koul 			dma->stream->name, ret);
957c46302ecSVinod Koul 		return ret;
958c46302ecSVinod Koul 	}
959c46302ecSVinod Koul 
960b86947b5SPierre-Louis Bossart 	ret = intel_free_stream(sdw, substream->stream, dai, sdw->instance);
961eff346f2SPierre-Louis Bossart 	if (ret < 0) {
9624e3ea93eSPierre-Louis Bossart 		dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
963eff346f2SPierre-Louis Bossart 		return ret;
964eff346f2SPierre-Louis Bossart 	}
965eff346f2SPierre-Louis Bossart 
966a5a0239cSBard Liao 	dma->hw_params = NULL;
967a5a0239cSBard Liao 	dma->pdi = NULL;
968a5a0239cSBard Liao 
969eff346f2SPierre-Louis Bossart 	return 0;
970eff346f2SPierre-Louis Bossart }
971eff346f2SPierre-Louis Bossart 
972183c7687SPierre-Louis Bossart static void intel_shutdown(struct snd_pcm_substream *substream,
973183c7687SPierre-Louis Bossart 			   struct snd_soc_dai *dai)
974183c7687SPierre-Louis Bossart {
975ebf878edSPierre-Louis Bossart 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
976183c7687SPierre-Louis Bossart 
977ebf878edSPierre-Louis Bossart 	pm_runtime_mark_last_busy(cdns->dev);
978ebf878edSPierre-Louis Bossart 	pm_runtime_put_autosuspend(cdns->dev);
979183c7687SPierre-Louis Bossart }
980183c7687SPierre-Louis Bossart 
981c46302ecSVinod Koul static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
982c46302ecSVinod Koul 				    void *stream, int direction)
983c46302ecSVinod Koul {
98463a6aa96SPierre-Louis Bossart 	return cdns_set_sdw_stream(dai, stream, direction);
985c46302ecSVinod Koul }
986c46302ecSVinod Koul 
98709553140SPierre-Louis Bossart static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
98809553140SPierre-Louis Bossart 				  int direction)
98909553140SPierre-Louis Bossart {
99009553140SPierre-Louis Bossart 	struct sdw_cdns_dma_data *dma;
99109553140SPierre-Louis Bossart 
99209553140SPierre-Louis Bossart 	if (direction == SNDRV_PCM_STREAM_PLAYBACK)
99309553140SPierre-Louis Bossart 		dma = dai->playback_dma_data;
99409553140SPierre-Louis Bossart 	else
99509553140SPierre-Louis Bossart 		dma = dai->capture_dma_data;
99609553140SPierre-Louis Bossart 
99709553140SPierre-Louis Bossart 	if (!dma)
99806dcb4e4SPierre-Louis Bossart 		return ERR_PTR(-EINVAL);
99909553140SPierre-Louis Bossart 
100009553140SPierre-Louis Bossart 	return dma->stream;
100109553140SPierre-Louis Bossart }
100209553140SPierre-Louis Bossart 
10038ddeafb9SRanjani Sridharan static int intel_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
10048ddeafb9SRanjani Sridharan {
10058ddeafb9SRanjani Sridharan 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
10068ddeafb9SRanjani Sridharan 	struct sdw_intel *sdw = cdns_to_intel(cdns);
10076d1c1a73SBard Liao 	struct sdw_intel_link_res *res = sdw->link_res;
10088ddeafb9SRanjani Sridharan 	struct sdw_cdns_dma_data *dma;
10098ddeafb9SRanjani Sridharan 	int ret = 0;
10108ddeafb9SRanjani Sridharan 
10116d1c1a73SBard Liao 	/*
10126d1c1a73SBard Liao 	 * The .trigger callback is used to send required IPC to audio
10136d1c1a73SBard Liao 	 * firmware. The .free_stream callback will still be called
10146d1c1a73SBard Liao 	 * by intel_free_stream() in the TRIGGER_SUSPEND case.
10156d1c1a73SBard Liao 	 */
10166d1c1a73SBard Liao 	if (res->ops && res->ops->trigger)
10176d1c1a73SBard Liao 		res->ops->trigger(dai, cmd, substream->stream);
10186d1c1a73SBard Liao 
10198ddeafb9SRanjani Sridharan 	dma = snd_soc_dai_get_dma_data(dai, substream);
10208ddeafb9SRanjani Sridharan 	if (!dma) {
10218ddeafb9SRanjani Sridharan 		dev_err(dai->dev, "failed to get dma data in %s\n",
10228ddeafb9SRanjani Sridharan 			__func__);
10238ddeafb9SRanjani Sridharan 		return -EIO;
10248ddeafb9SRanjani Sridharan 	}
10258ddeafb9SRanjani Sridharan 
10268ddeafb9SRanjani Sridharan 	switch (cmd) {
10278ddeafb9SRanjani Sridharan 	case SNDRV_PCM_TRIGGER_SUSPEND:
10288ddeafb9SRanjani Sridharan 
10298ddeafb9SRanjani Sridharan 		/*
10308ddeafb9SRanjani Sridharan 		 * The .prepare callback is used to deal with xruns and resume operations.
10318ddeafb9SRanjani Sridharan 		 * In the case of xruns, the DMAs and SHIM registers cannot be touched,
10328ddeafb9SRanjani Sridharan 		 * but for resume operations the DMAs and SHIM registers need to be initialized.
10338ddeafb9SRanjani Sridharan 		 * the .trigger callback is used to track the suspend case only.
10348ddeafb9SRanjani Sridharan 		 */
10358ddeafb9SRanjani Sridharan 
10368ddeafb9SRanjani Sridharan 		dma->suspended = true;
10378ddeafb9SRanjani Sridharan 
10388ddeafb9SRanjani Sridharan 		ret = intel_free_stream(sdw, substream->stream, dai, sdw->instance);
10398ddeafb9SRanjani Sridharan 		break;
10408ddeafb9SRanjani Sridharan 
10418ddeafb9SRanjani Sridharan 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
10428ddeafb9SRanjani Sridharan 		dma->paused = true;
10438ddeafb9SRanjani Sridharan 		break;
10448ddeafb9SRanjani Sridharan 	case SNDRV_PCM_TRIGGER_STOP:
10458ddeafb9SRanjani Sridharan 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
10468ddeafb9SRanjani Sridharan 		dma->paused = false;
10478ddeafb9SRanjani Sridharan 		break;
10488ddeafb9SRanjani Sridharan 	default:
10498ddeafb9SRanjani Sridharan 		break;
10508ddeafb9SRanjani Sridharan 	}
10518ddeafb9SRanjani Sridharan 
10528ddeafb9SRanjani Sridharan 	return ret;
10538ddeafb9SRanjani Sridharan }
10548ddeafb9SRanjani Sridharan 
10553e9c9f90SPierre-Louis Bossart static int intel_component_probe(struct snd_soc_component *component)
10563e9c9f90SPierre-Louis Bossart {
10573e9c9f90SPierre-Louis Bossart 	int ret;
10583e9c9f90SPierre-Louis Bossart 
10593e9c9f90SPierre-Louis Bossart 	/*
10603e9c9f90SPierre-Louis Bossart 	 * make sure the device is pm_runtime_active before initiating
10613e9c9f90SPierre-Louis Bossart 	 * bus transactions during the card registration.
10623e9c9f90SPierre-Louis Bossart 	 * We use pm_runtime_resume() here, without taking a reference
10633e9c9f90SPierre-Louis Bossart 	 * and releasing it immediately.
10643e9c9f90SPierre-Louis Bossart 	 */
10653e9c9f90SPierre-Louis Bossart 	ret = pm_runtime_resume(component->dev);
10663e9c9f90SPierre-Louis Bossart 	if (ret < 0 && ret != -EACCES)
10673e9c9f90SPierre-Louis Bossart 		return ret;
10683e9c9f90SPierre-Louis Bossart 
10693e9c9f90SPierre-Louis Bossart 	return 0;
10703e9c9f90SPierre-Louis Bossart }
10713e9c9f90SPierre-Louis Bossart 
10728ddeafb9SRanjani Sridharan static int intel_component_dais_suspend(struct snd_soc_component *component)
10738ddeafb9SRanjani Sridharan {
10748ddeafb9SRanjani Sridharan 	struct snd_soc_dai *dai;
10758ddeafb9SRanjani Sridharan 
10768ddeafb9SRanjani Sridharan 	/*
10778ddeafb9SRanjani Sridharan 	 * In the corner case where a SUSPEND happens during a PAUSE, the ALSA core
10788ddeafb9SRanjani Sridharan 	 * does not throw the TRIGGER_SUSPEND. This leaves the DAIs in an unbalanced state.
10798ddeafb9SRanjani Sridharan 	 * Since the component suspend is called last, we can trap this corner case
10808ddeafb9SRanjani Sridharan 	 * and force the DAIs to release their resources.
10818ddeafb9SRanjani Sridharan 	 */
10828ddeafb9SRanjani Sridharan 	for_each_component_dais(component, dai) {
10838ddeafb9SRanjani Sridharan 		struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
10848ddeafb9SRanjani Sridharan 		struct sdw_intel *sdw = cdns_to_intel(cdns);
10858ddeafb9SRanjani Sridharan 		struct sdw_cdns_dma_data *dma;
10868ddeafb9SRanjani Sridharan 		int stream;
10878ddeafb9SRanjani Sridharan 		int ret;
10888ddeafb9SRanjani Sridharan 
10898ddeafb9SRanjani Sridharan 		dma = dai->playback_dma_data;
10908ddeafb9SRanjani Sridharan 		stream = SNDRV_PCM_STREAM_PLAYBACK;
10918ddeafb9SRanjani Sridharan 		if (!dma) {
10928ddeafb9SRanjani Sridharan 			dma = dai->capture_dma_data;
10938ddeafb9SRanjani Sridharan 			stream = SNDRV_PCM_STREAM_CAPTURE;
10948ddeafb9SRanjani Sridharan 		}
10958ddeafb9SRanjani Sridharan 
10968ddeafb9SRanjani Sridharan 		if (!dma)
10978ddeafb9SRanjani Sridharan 			continue;
10988ddeafb9SRanjani Sridharan 
10998ddeafb9SRanjani Sridharan 		if (dma->suspended)
11008ddeafb9SRanjani Sridharan 			continue;
11018ddeafb9SRanjani Sridharan 
11028ddeafb9SRanjani Sridharan 		if (dma->paused) {
11038ddeafb9SRanjani Sridharan 			dma->suspended = true;
11048ddeafb9SRanjani Sridharan 
11058ddeafb9SRanjani Sridharan 			ret = intel_free_stream(sdw, stream, dai, sdw->instance);
11068ddeafb9SRanjani Sridharan 			if (ret < 0)
11078ddeafb9SRanjani Sridharan 				return ret;
11088ddeafb9SRanjani Sridharan 		}
11098ddeafb9SRanjani Sridharan 	}
11108ddeafb9SRanjani Sridharan 
11118ddeafb9SRanjani Sridharan 	return 0;
11128ddeafb9SRanjani Sridharan }
11138ddeafb9SRanjani Sridharan 
1114b1635596SJulia Lawall static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
11155e7484d0SRander Wang 	.startup = intel_startup,
1116c46302ecSVinod Koul 	.hw_params = intel_hw_params,
111727b198f4SRander Wang 	.prepare = intel_prepare,
1118c46302ecSVinod Koul 	.hw_free = intel_hw_free,
11198ddeafb9SRanjani Sridharan 	.trigger = intel_trigger,
1120183c7687SPierre-Louis Bossart 	.shutdown = intel_shutdown,
1121e8444560SPierre-Louis Bossart 	.set_stream = intel_pcm_set_sdw_stream,
1122e8444560SPierre-Louis Bossart 	.get_stream = intel_get_sdw_stream,
1123c46302ecSVinod Koul };
1124c46302ecSVinod Koul 
1125c46302ecSVinod Koul static const struct snd_soc_component_driver dai_component = {
1126c46302ecSVinod Koul 	.name			= "soundwire",
11273e9c9f90SPierre-Louis Bossart 	.probe			= intel_component_probe,
1128ca682020SCharles Keepax 	.suspend		= intel_component_dais_suspend,
1129ca682020SCharles Keepax 	.legacy_dai_naming	= 1,
1130c46302ecSVinod Koul };
1131c46302ecSVinod Koul 
1132c46302ecSVinod Koul static int intel_create_dai(struct sdw_cdns *cdns,
1133c46302ecSVinod Koul 			    struct snd_soc_dai_driver *dais,
1134c46302ecSVinod Koul 			    enum intel_pdi_type type,
113563a6aa96SPierre-Louis Bossart 			    u32 num, u32 off, u32 max_ch)
1136c46302ecSVinod Koul {
1137c46302ecSVinod Koul 	int i;
1138c46302ecSVinod Koul 
1139c46302ecSVinod Koul 	if (num == 0)
1140c46302ecSVinod Koul 		return 0;
1141c46302ecSVinod Koul 
1142c46302ecSVinod Koul 	 /* TODO: Read supported rates/formats from hardware */
1143c46302ecSVinod Koul 	for (i = off; i < (off + num); i++) {
1144bf6d6e68SPierre-Louis Bossart 		dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
1145bf6d6e68SPierre-Louis Bossart 					      "SDW%d Pin%d",
1146c46302ecSVinod Koul 					      cdns->instance, i);
1147c46302ecSVinod Koul 		if (!dais[i].name)
1148c46302ecSVinod Koul 			return -ENOMEM;
1149c46302ecSVinod Koul 
1150c46302ecSVinod Koul 		if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
1151c46302ecSVinod Koul 			dais[i].playback.channels_min = 1;
1152c46302ecSVinod Koul 			dais[i].playback.channels_max = max_ch;
1153c46302ecSVinod Koul 			dais[i].playback.rates = SNDRV_PCM_RATE_48000;
1154c46302ecSVinod Koul 			dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
1155c46302ecSVinod Koul 		}
1156c46302ecSVinod Koul 
1157c46302ecSVinod Koul 		if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
115839194128SSrinivas Kandagatla 			dais[i].capture.channels_min = 1;
115939194128SSrinivas Kandagatla 			dais[i].capture.channels_max = max_ch;
1160c46302ecSVinod Koul 			dais[i].capture.rates = SNDRV_PCM_RATE_48000;
1161c46302ecSVinod Koul 			dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
1162c46302ecSVinod Koul 		}
1163c46302ecSVinod Koul 
1164c46302ecSVinod Koul 		dais[i].ops = &intel_pcm_dai_ops;
1165c46302ecSVinod Koul 	}
1166c46302ecSVinod Koul 
1167c46302ecSVinod Koul 	return 0;
1168c46302ecSVinod Koul }
1169c46302ecSVinod Koul 
1170c46302ecSVinod Koul static int intel_register_dai(struct sdw_intel *sdw)
1171c46302ecSVinod Koul {
1172c46302ecSVinod Koul 	struct sdw_cdns *cdns = &sdw->cdns;
1173c46302ecSVinod Koul 	struct sdw_cdns_streams *stream;
1174c46302ecSVinod Koul 	struct snd_soc_dai_driver *dais;
1175c46302ecSVinod Koul 	int num_dai, ret, off = 0;
1176c46302ecSVinod Koul 
1177c46302ecSVinod Koul 	/* DAIs are created based on total number of PDIs supported */
117863a6aa96SPierre-Louis Bossart 	num_dai = cdns->pcm.num_pdi;
1179c46302ecSVinod Koul 
1180c46302ecSVinod Koul 	dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
1181c46302ecSVinod Koul 	if (!dais)
1182c46302ecSVinod Koul 		return -ENOMEM;
1183c46302ecSVinod Koul 
1184c46302ecSVinod Koul 	/* Create PCM DAIs */
1185c46302ecSVinod Koul 	stream = &cdns->pcm;
1186c46302ecSVinod Koul 
1187cf924962SBard Liao 	ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
118863a6aa96SPierre-Louis Bossart 			       off, stream->num_ch_in);
1189c46302ecSVinod Koul 	if (ret)
1190c46302ecSVinod Koul 		return ret;
1191c46302ecSVinod Koul 
1192c46302ecSVinod Koul 	off += cdns->pcm.num_in;
11931215daeeSVinod Koul 	ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
119463a6aa96SPierre-Louis Bossart 			       off, stream->num_ch_out);
1195c46302ecSVinod Koul 	if (ret)
1196c46302ecSVinod Koul 		return ret;
1197c46302ecSVinod Koul 
1198c46302ecSVinod Koul 	off += cdns->pcm.num_out;
11991215daeeSVinod Koul 	ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
120063a6aa96SPierre-Louis Bossart 			       off, stream->num_ch_bd);
1201c46302ecSVinod Koul 	if (ret)
1202c46302ecSVinod Koul 		return ret;
1203c46302ecSVinod Koul 
1204c46302ecSVinod Koul 	return snd_soc_register_component(cdns->dev, &dai_component,
1205c46302ecSVinod Koul 					  dais, num_dai);
1206c46302ecSVinod Koul }
1207c46302ecSVinod Koul 
1208085f4aceSPierre-Louis Bossart static int sdw_master_read_intel_prop(struct sdw_bus *bus)
1209085f4aceSPierre-Louis Bossart {
1210085f4aceSPierre-Louis Bossart 	struct sdw_master_prop *prop = &bus->prop;
1211085f4aceSPierre-Louis Bossart 	struct fwnode_handle *link;
1212085f4aceSPierre-Louis Bossart 	char name[32];
1213395713d8SPierre-Louis Bossart 	u32 quirk_mask;
1214085f4aceSPierre-Louis Bossart 
1215085f4aceSPierre-Louis Bossart 	/* Find master handle */
1216085f4aceSPierre-Louis Bossart 	snprintf(name, sizeof(name),
1217085f4aceSPierre-Louis Bossart 		 "mipi-sdw-link-%d-subproperties", bus->link_id);
1218085f4aceSPierre-Louis Bossart 
1219085f4aceSPierre-Louis Bossart 	link = device_get_named_child_node(bus->dev, name);
1220085f4aceSPierre-Louis Bossart 	if (!link) {
1221085f4aceSPierre-Louis Bossart 		dev_err(bus->dev, "Master node %s not found\n", name);
1222085f4aceSPierre-Louis Bossart 		return -EIO;
1223085f4aceSPierre-Louis Bossart 	}
1224085f4aceSPierre-Louis Bossart 
1225085f4aceSPierre-Louis Bossart 	fwnode_property_read_u32(link,
1226085f4aceSPierre-Louis Bossart 				 "intel-sdw-ip-clock",
1227085f4aceSPierre-Louis Bossart 				 &prop->mclk_freq);
1228395713d8SPierre-Louis Bossart 
1229a19efb52SBard Liao 	/* the values reported by BIOS are the 2x clock, not the bus clock */
1230a19efb52SBard Liao 	prop->mclk_freq /= 2;
1231a19efb52SBard Liao 
1232395713d8SPierre-Louis Bossart 	fwnode_property_read_u32(link,
1233395713d8SPierre-Louis Bossart 				 "intel-quirk-mask",
1234395713d8SPierre-Louis Bossart 				 &quirk_mask);
1235395713d8SPierre-Louis Bossart 
1236395713d8SPierre-Louis Bossart 	if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
1237395713d8SPierre-Louis Bossart 		prop->hw_disabled = true;
1238395713d8SPierre-Louis Bossart 
1239bb877bebSBard Liao 	prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
1240bb877bebSBard Liao 		SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
1241bb877bebSBard Liao 
1242085f4aceSPierre-Louis Bossart 	return 0;
1243085f4aceSPierre-Louis Bossart }
1244085f4aceSPierre-Louis Bossart 
124571bb8a1bSVinod Koul static int intel_prop_read(struct sdw_bus *bus)
124671bb8a1bSVinod Koul {
124771bb8a1bSVinod Koul 	/* Initialize with default handler to read all DisCo properties */
124871bb8a1bSVinod Koul 	sdw_master_read_prop(bus);
124971bb8a1bSVinod Koul 
1250085f4aceSPierre-Louis Bossart 	/* read Intel-specific properties */
1251085f4aceSPierre-Louis Bossart 	sdw_master_read_intel_prop(bus);
1252085f4aceSPierre-Louis Bossart 
125371bb8a1bSVinod Koul 	return 0;
125471bb8a1bSVinod Koul }
125571bb8a1bSVinod Koul 
1256c91605f4SShreyas NC static struct sdw_master_ops sdw_intel_ops = {
1257c91605f4SShreyas NC 	.read_prop = sdw_master_read_prop,
1258f6594cdfSPierre-Louis Bossart 	.override_adr = sdw_dmi_override_adr,
1259c91605f4SShreyas NC 	.xfer_msg = cdns_xfer_msg,
1260c91605f4SShreyas NC 	.xfer_msg_defer = cdns_xfer_msg_defer,
1261c91605f4SShreyas NC 	.reset_page_addr = cdns_reset_page_addr,
126207abeff1SVinod Koul 	.set_bus_conf = cdns_bus_conf,
126330246e2dSShreyas NC 	.pre_bank_switch = intel_pre_bank_switch,
126430246e2dSShreyas NC 	.post_bank_switch = intel_post_bank_switch,
1265c91605f4SShreyas NC };
1266c91605f4SShreyas NC 
1267dfbe642dSPierre-Louis Bossart static int intel_init(struct sdw_intel *sdw)
1268dfbe642dSPierre-Louis Bossart {
12694a17c441SPierre-Louis Bossart 	bool clock_stop;
12704a17c441SPierre-Louis Bossart 
1271dfbe642dSPierre-Louis Bossart 	/* Initialize shim and controller */
1272dfbe642dSPierre-Louis Bossart 	intel_link_power_up(sdw);
12734a17c441SPierre-Louis Bossart 
12744a17c441SPierre-Louis Bossart 	clock_stop = sdw_cdns_is_clock_stop(&sdw->cdns);
12754a17c441SPierre-Louis Bossart 
12764a17c441SPierre-Louis Bossart 	intel_shim_init(sdw, clock_stop);
12774a17c441SPierre-Louis Bossart 
12784a17c441SPierre-Louis Bossart 	return 0;
1279dfbe642dSPierre-Louis Bossart }
1280dfbe642dSPierre-Louis Bossart 
128171bb8a1bSVinod Koul /*
128229a269c6SPierre-Louis Bossart  * probe and init (aux_dev_id argument is required by function prototype but not used)
128371bb8a1bSVinod Koul  */
128429a269c6SPierre-Louis Bossart static int intel_link_probe(struct auxiliary_device *auxdev,
128529a269c6SPierre-Louis Bossart 			    const struct auxiliary_device_id *aux_dev_id)
128629a269c6SPierre-Louis Bossart 
128771bb8a1bSVinod Koul {
128829a269c6SPierre-Louis Bossart 	struct device *dev = &auxdev->dev;
128929a269c6SPierre-Louis Bossart 	struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
129071bb8a1bSVinod Koul 	struct sdw_intel *sdw;
129183e129afSPierre-Louis Bossart 	struct sdw_cdns *cdns;
1292b6109dd6SPierre-Louis Bossart 	struct sdw_bus *bus;
129371bb8a1bSVinod Koul 	int ret;
129471bb8a1bSVinod Koul 
1295b6109dd6SPierre-Louis Bossart 	sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
129671bb8a1bSVinod Koul 	if (!sdw)
129771bb8a1bSVinod Koul 		return -ENOMEM;
129871bb8a1bSVinod Koul 
129983e129afSPierre-Louis Bossart 	cdns = &sdw->cdns;
130083e129afSPierre-Louis Bossart 	bus = &cdns->bus;
130171bb8a1bSVinod Koul 
130229a269c6SPierre-Louis Bossart 	sdw->instance = auxdev->id;
130329a269c6SPierre-Louis Bossart 	sdw->link_res = &ldev->link_res;
130483e129afSPierre-Louis Bossart 	cdns->dev = dev;
130583e129afSPierre-Louis Bossart 	cdns->registers = sdw->link_res->registers;
130683e129afSPierre-Louis Bossart 	cdns->instance = sdw->instance;
130783e129afSPierre-Louis Bossart 	cdns->msg_count = 0;
130883e129afSPierre-Louis Bossart 
130929a269c6SPierre-Louis Bossart 	bus->link_id = auxdev->id;
131071bb8a1bSVinod Koul 
131183e129afSPierre-Louis Bossart 	sdw_cdns_probe(cdns);
131271bb8a1bSVinod Koul 
131371bb8a1bSVinod Koul 	/* Set property read ops */
1314c91605f4SShreyas NC 	sdw_intel_ops.read_prop = intel_prop_read;
1315b6109dd6SPierre-Louis Bossart 	bus->ops = &sdw_intel_ops;
131671bb8a1bSVinod Koul 
1317b6109dd6SPierre-Louis Bossart 	/* set driver data, accessed by snd_soc_dai_get_drvdata() */
13183edac08eSDavid E. Box 	auxiliary_set_drvdata(auxdev, cdns);
131971bb8a1bSVinod Koul 
13209026118fSBard Liao 	/* use generic bandwidth allocation algorithm */
13219026118fSBard Liao 	sdw->cdns.bus.compute_params = sdw_compute_params;
13229026118fSBard Liao 
13236d9f2dadSPierre-Louis Bossart 	/* avoid resuming from pm_runtime suspend if it's not required */
13246d9f2dadSPierre-Louis Bossart 	dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
13256d9f2dadSPierre-Louis Bossart 
1326b6109dd6SPierre-Louis Bossart 	ret = sdw_bus_master_add(bus, dev, dev->fwnode);
132771bb8a1bSVinod Koul 	if (ret) {
1328b6109dd6SPierre-Louis Bossart 		dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
13299e3d47fbSPierre-Louis Bossart 		return ret;
133071bb8a1bSVinod Koul 	}
133171bb8a1bSVinod Koul 
13326d2c6669SPierre-Louis Bossart 	if (bus->prop.hw_disabled)
1333b6109dd6SPierre-Louis Bossart 		dev_info(dev,
1334b6109dd6SPierre-Louis Bossart 			 "SoundWire master %d is disabled, will be ignored\n",
1335b6109dd6SPierre-Louis Bossart 			 bus->link_id);
13360ef2986eSPierre-Louis Bossart 	/*
13370ef2986eSPierre-Louis Bossart 	 * Ignore BIOS err_threshold, it's a really bad idea when dealing
13380ef2986eSPierre-Louis Bossart 	 * with multiple hardware synchronized links
13390ef2986eSPierre-Louis Bossart 	 */
13400ef2986eSPierre-Louis Bossart 	bus->prop.err_threshold = 0;
13416d2c6669SPierre-Louis Bossart 
13426d2c6669SPierre-Louis Bossart 	return 0;
13436d2c6669SPierre-Louis Bossart }
13446d2c6669SPierre-Louis Bossart 
134529a269c6SPierre-Louis Bossart int intel_link_startup(struct auxiliary_device *auxdev)
13466d2c6669SPierre-Louis Bossart {
13476d2c6669SPierre-Louis Bossart 	struct sdw_cdns_stream_config config;
134829a269c6SPierre-Louis Bossart 	struct device *dev = &auxdev->dev;
13493edac08eSDavid E. Box 	struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
13506d2c6669SPierre-Louis Bossart 	struct sdw_intel *sdw = cdns_to_intel(cdns);
13516d2c6669SPierre-Louis Bossart 	struct sdw_bus *bus = &cdns->bus;
1352ebf878edSPierre-Louis Bossart 	int link_flags;
1353857a7c42SPierre-Louis Bossart 	bool multi_link;
1354caf68819SPierre-Louis Bossart 	u32 clock_stop_quirks;
13556d2c6669SPierre-Louis Bossart 	int ret;
13566d2c6669SPierre-Louis Bossart 
13576d2c6669SPierre-Louis Bossart 	if (bus->prop.hw_disabled) {
13586d2c6669SPierre-Louis Bossart 		dev_info(dev,
13596d2c6669SPierre-Louis Bossart 			 "SoundWire master %d is disabled, ignoring\n",
13606d2c6669SPierre-Louis Bossart 			 sdw->instance);
1361395713d8SPierre-Louis Bossart 		return 0;
1362395713d8SPierre-Louis Bossart 	}
1363395713d8SPierre-Louis Bossart 
1364857a7c42SPierre-Louis Bossart 	link_flags = md_flags >> (bus->link_id * 8);
1365857a7c42SPierre-Louis Bossart 	multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1366857a7c42SPierre-Louis Bossart 	if (!multi_link) {
1367857a7c42SPierre-Louis Bossart 		dev_dbg(dev, "Multi-link is disabled\n");
1368857a7c42SPierre-Louis Bossart 		bus->multi_link = false;
1369857a7c42SPierre-Louis Bossart 	} else {
137094eed661SPierre-Louis Bossart 		/*
137194eed661SPierre-Louis Bossart 		 * hardware-based synchronization is required regardless
137294eed661SPierre-Louis Bossart 		 * of the number of segments used by a stream: SSP-based
137394eed661SPierre-Louis Bossart 		 * synchronization is gated by gsync when the multi-master
137494eed661SPierre-Louis Bossart 		 * mode is set.
137594eed661SPierre-Louis Bossart 		 */
1376857a7c42SPierre-Louis Bossart 		bus->multi_link = true;
137794eed661SPierre-Louis Bossart 		bus->hw_sync_min_links = 1;
1378857a7c42SPierre-Louis Bossart 	}
1379857a7c42SPierre-Louis Bossart 
1380857a7c42SPierre-Louis Bossart 	/* Initialize shim, controller */
1381dfbe642dSPierre-Louis Bossart 	ret = intel_init(sdw);
138271bb8a1bSVinod Koul 	if (ret)
138371bb8a1bSVinod Koul 		goto err_init;
138471bb8a1bSVinod Koul 
138537a2d22bSVinod Koul 	/* Read the PDI config and initialize cadence PDI */
138637a2d22bSVinod Koul 	intel_pdi_init(sdw, &config);
138783e129afSPierre-Louis Bossart 	ret = sdw_cdns_pdi_init(cdns, config);
138871bb8a1bSVinod Koul 	if (ret)
138971bb8a1bSVinod Koul 		goto err_init;
139071bb8a1bSVinod Koul 
139137a2d22bSVinod Koul 	intel_pdi_ch_update(sdw);
139237a2d22bSVinod Koul 
139383e129afSPierre-Louis Bossart 	ret = sdw_cdns_enable_interrupt(cdns, true);
139471bb8a1bSVinod Koul 	if (ret < 0) {
1395b6109dd6SPierre-Louis Bossart 		dev_err(dev, "cannot enable interrupts\n");
139671bb8a1bSVinod Koul 		goto err_init;
139771bb8a1bSVinod Koul 	}
139871bb8a1bSVinod Koul 
1399857a7c42SPierre-Louis Bossart 	/*
1400857a7c42SPierre-Louis Bossart 	 * follow recommended programming flows to avoid timeouts when
1401857a7c42SPierre-Louis Bossart 	 * gsync is enabled
1402857a7c42SPierre-Louis Bossart 	 */
1403857a7c42SPierre-Louis Bossart 	if (multi_link)
1404857a7c42SPierre-Louis Bossart 		intel_shim_sync_arm(sdw);
1405857a7c42SPierre-Louis Bossart 
1406857a7c42SPierre-Louis Bossart 	ret = sdw_cdns_init(cdns);
1407857a7c42SPierre-Louis Bossart 	if (ret < 0) {
1408857a7c42SPierre-Louis Bossart 		dev_err(dev, "unable to initialize Cadence IP\n");
1409857a7c42SPierre-Louis Bossart 		goto err_interrupt;
1410857a7c42SPierre-Louis Bossart 	}
1411857a7c42SPierre-Louis Bossart 
141283e129afSPierre-Louis Bossart 	ret = sdw_cdns_exit_reset(cdns);
141349ea07d3SPierre-Louis Bossart 	if (ret < 0) {
1414b6109dd6SPierre-Louis Bossart 		dev_err(dev, "unable to exit bus reset sequence\n");
14159e3d47fbSPierre-Louis Bossart 		goto err_interrupt;
141649ea07d3SPierre-Louis Bossart 	}
141749ea07d3SPierre-Louis Bossart 
1418857a7c42SPierre-Louis Bossart 	if (multi_link) {
1419857a7c42SPierre-Louis Bossart 		ret = intel_shim_sync_go(sdw);
1420857a7c42SPierre-Louis Bossart 		if (ret < 0) {
1421857a7c42SPierre-Louis Bossart 			dev_err(dev, "sync go failed: %d\n", ret);
1422857a7c42SPierre-Louis Bossart 			goto err_interrupt;
1423857a7c42SPierre-Louis Bossart 		}
1424857a7c42SPierre-Louis Bossart 	}
1425ff560946SPierre-Louis Bossart 	sdw_cdns_check_self_clearing_bits(cdns, __func__,
1426ff560946SPierre-Louis Bossart 					  true, INTEL_MASTER_RESET_ITERATIONS);
1427857a7c42SPierre-Louis Bossart 
1428c46302ecSVinod Koul 	/* Register DAIs */
1429c46302ecSVinod Koul 	ret = intel_register_dai(sdw);
1430c46302ecSVinod Koul 	if (ret) {
1431b6109dd6SPierre-Louis Bossart 		dev_err(dev, "DAI registration failed: %d\n", ret);
1432b6109dd6SPierre-Louis Bossart 		snd_soc_unregister_component(dev);
14339e3d47fbSPierre-Louis Bossart 		goto err_interrupt;
1434c46302ecSVinod Koul 	}
1435c46302ecSVinod Koul 
143679ee6631SPierre-Louis Bossart 	intel_debugfs_init(sdw);
143779ee6631SPierre-Louis Bossart 
1438ebf878edSPierre-Louis Bossart 	/* Enable runtime PM */
1439ebf878edSPierre-Louis Bossart 	if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
1440ebf878edSPierre-Louis Bossart 		pm_runtime_set_autosuspend_delay(dev,
1441ebf878edSPierre-Louis Bossart 						 INTEL_MASTER_SUSPEND_DELAY_MS);
1442ebf878edSPierre-Louis Bossart 		pm_runtime_use_autosuspend(dev);
1443ebf878edSPierre-Louis Bossart 		pm_runtime_mark_last_busy(dev);
1444ebf878edSPierre-Louis Bossart 
1445ebf878edSPierre-Louis Bossart 		pm_runtime_set_active(dev);
1446ebf878edSPierre-Louis Bossart 		pm_runtime_enable(dev);
1447ebf878edSPierre-Louis Bossart 	}
1448ebf878edSPierre-Louis Bossart 
1449caf68819SPierre-Louis Bossart 	clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1450caf68819SPierre-Louis Bossart 	if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
1451caf68819SPierre-Louis Bossart 		/*
1452caf68819SPierre-Louis Bossart 		 * To keep the clock running we need to prevent
1453caf68819SPierre-Louis Bossart 		 * pm_runtime suspend from happening by increasing the
1454caf68819SPierre-Louis Bossart 		 * reference count.
1455caf68819SPierre-Louis Bossart 		 * This quirk is specified by the parent PCI device in
1456caf68819SPierre-Louis Bossart 		 * case of specific latency requirements. It will have
1457caf68819SPierre-Louis Bossart 		 * no effect if pm_runtime is disabled by the user via
1458caf68819SPierre-Louis Bossart 		 * a module parameter for testing purposes.
1459caf68819SPierre-Louis Bossart 		 */
1460caf68819SPierre-Louis Bossart 		pm_runtime_get_noresume(dev);
1461caf68819SPierre-Louis Bossart 	}
1462caf68819SPierre-Louis Bossart 
1463a2d9c161SPierre-Louis Bossart 	/*
1464a2d9c161SPierre-Louis Bossart 	 * The runtime PM status of Slave devices is "Unsupported"
1465a2d9c161SPierre-Louis Bossart 	 * until they report as ATTACHED. If they don't, e.g. because
1466a2d9c161SPierre-Louis Bossart 	 * there are no Slave devices populated or if the power-on is
1467a2d9c161SPierre-Louis Bossart 	 * delayed or dependent on a power switch, the Master will
1468a2d9c161SPierre-Louis Bossart 	 * remain active and prevent its parent from suspending.
1469a2d9c161SPierre-Louis Bossart 	 *
1470a2d9c161SPierre-Louis Bossart 	 * Conditionally force the pm_runtime core to re-evaluate the
1471a2d9c161SPierre-Louis Bossart 	 * Master status in the absence of any Slave activity. A quirk
1472a2d9c161SPierre-Louis Bossart 	 * is provided to e.g. deal with Slaves that may be powered on
1473a2d9c161SPierre-Louis Bossart 	 * with a delay. A more complete solution would require the
1474a2d9c161SPierre-Louis Bossart 	 * definition of Master properties.
1475a2d9c161SPierre-Louis Bossart 	 */
1476a2d9c161SPierre-Louis Bossart 	if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1477a2d9c161SPierre-Louis Bossart 		pm_runtime_idle(dev);
1478a2d9c161SPierre-Louis Bossart 
1479e4401abbSPierre-Louis Bossart 	sdw->startup_done = true;
148071bb8a1bSVinod Koul 	return 0;
148171bb8a1bSVinod Koul 
14829e3d47fbSPierre-Louis Bossart err_interrupt:
148383e129afSPierre-Louis Bossart 	sdw_cdns_enable_interrupt(cdns, false);
148471bb8a1bSVinod Koul err_init:
148571bb8a1bSVinod Koul 	return ret;
148671bb8a1bSVinod Koul }
148771bb8a1bSVinod Koul 
148829a269c6SPierre-Louis Bossart static void intel_link_remove(struct auxiliary_device *auxdev)
148971bb8a1bSVinod Koul {
149029a269c6SPierre-Louis Bossart 	struct device *dev = &auxdev->dev;
14913edac08eSDavid E. Box 	struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
149283e129afSPierre-Louis Bossart 	struct sdw_intel *sdw = cdns_to_intel(cdns);
149383e129afSPierre-Louis Bossart 	struct sdw_bus *bus = &cdns->bus;
1494b6109dd6SPierre-Louis Bossart 
1495caf68819SPierre-Louis Bossart 	/*
1496caf68819SPierre-Louis Bossart 	 * Since pm_runtime is already disabled, we don't decrease
1497caf68819SPierre-Louis Bossart 	 * the refcount when the clock_stop_quirk is
1498caf68819SPierre-Louis Bossart 	 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
1499caf68819SPierre-Louis Bossart 	 */
1500b6109dd6SPierre-Louis Bossart 	if (!bus->prop.hw_disabled) {
150179ee6631SPierre-Louis Bossart 		intel_debugfs_exit(sdw);
150283e129afSPierre-Louis Bossart 		sdw_cdns_enable_interrupt(cdns, false);
1503b6109dd6SPierre-Louis Bossart 		snd_soc_unregister_component(dev);
1504395713d8SPierre-Louis Bossart 	}
1505b6109dd6SPierre-Louis Bossart 	sdw_bus_master_delete(bus);
150671bb8a1bSVinod Koul }
150771bb8a1bSVinod Koul 
150829a269c6SPierre-Louis Bossart int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
1509ab2c9132SRander Wang {
151029a269c6SPierre-Louis Bossart 	struct device *dev = &auxdev->dev;
151171bb8a1bSVinod Koul 	struct sdw_intel *sdw;
1512ab2c9132SRander Wang 	struct sdw_bus *bus;
1513ab2c9132SRander Wang 	void __iomem *shim;
1514ab2c9132SRander Wang 	u16 wake_sts;
151571bb8a1bSVinod Koul 
15163edac08eSDavid E. Box 	sdw = auxiliary_get_drvdata(auxdev);
1517ab2c9132SRander Wang 	bus = &sdw->cdns.bus;
151871bb8a1bSVinod Koul 
1519e4401abbSPierre-Louis Bossart 	if (bus->prop.hw_disabled || !sdw->startup_done) {
1520e4401abbSPierre-Louis Bossart 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1521e4401abbSPierre-Louis Bossart 			bus->link_id);
1522ab2c9132SRander Wang 		return 0;
152371bb8a1bSVinod Koul 	}
1524ab2c9132SRander Wang 
1525ab2c9132SRander Wang 	shim = sdw->link_res->shim;
1526ab2c9132SRander Wang 	wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
1527ab2c9132SRander Wang 
1528ab2c9132SRander Wang 	if (!(wake_sts & BIT(sdw->instance)))
1529ab2c9132SRander Wang 		return 0;
1530ab2c9132SRander Wang 
1531ab2c9132SRander Wang 	/* disable WAKEEN interrupt ASAP to prevent interrupt flood */
1532ab2c9132SRander Wang 	intel_shim_wake(sdw, false);
1533ab2c9132SRander Wang 
1534ab2c9132SRander Wang 	/*
1535ab2c9132SRander Wang 	 * resume the Master, which will generate a bus reset and result in
1536ab2c9132SRander Wang 	 * Slaves re-attaching and be re-enumerated. The SoundWire physical
1537ab2c9132SRander Wang 	 * device which generated the wake will trigger an interrupt, which
1538ab2c9132SRander Wang 	 * will in turn cause the corresponding Linux Slave device to be
1539ab2c9132SRander Wang 	 * resumed and the Slave codec driver to check the status.
1540ab2c9132SRander Wang 	 */
1541ab2c9132SRander Wang 	pm_request_resume(dev);
154271bb8a1bSVinod Koul 
154371bb8a1bSVinod Koul 	return 0;
154471bb8a1bSVinod Koul }
154571bb8a1bSVinod Koul 
15469b3b4b3fSPierre-Louis Bossart /*
15479b3b4b3fSPierre-Louis Bossart  * PM calls
15489b3b4b3fSPierre-Louis Bossart  */
15499b3b4b3fSPierre-Louis Bossart 
1550029bfd1cSPierre-Louis Bossart static int intel_resume_child_device(struct device *dev, void *data)
1551029bfd1cSPierre-Louis Bossart {
1552029bfd1cSPierre-Louis Bossart 	int ret;
1553029bfd1cSPierre-Louis Bossart 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
1554029bfd1cSPierre-Louis Bossart 
1555029bfd1cSPierre-Louis Bossart 	if (!slave->probed) {
1556*63198aaaSPierre-Louis Bossart 		dev_dbg(dev, "skipping device, no probed driver\n");
1557029bfd1cSPierre-Louis Bossart 		return 0;
1558029bfd1cSPierre-Louis Bossart 	}
1559029bfd1cSPierre-Louis Bossart 	if (!slave->dev_num_sticky) {
1560*63198aaaSPierre-Louis Bossart 		dev_dbg(dev, "skipping device, never detected on bus\n");
1561029bfd1cSPierre-Louis Bossart 		return 0;
1562029bfd1cSPierre-Louis Bossart 	}
1563029bfd1cSPierre-Louis Bossart 
1564029bfd1cSPierre-Louis Bossart 	ret = pm_request_resume(dev);
1565029bfd1cSPierre-Louis Bossart 	if (ret < 0)
1566029bfd1cSPierre-Louis Bossart 		dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
1567029bfd1cSPierre-Louis Bossart 
1568029bfd1cSPierre-Louis Bossart 	return ret;
1569029bfd1cSPierre-Louis Bossart }
1570029bfd1cSPierre-Louis Bossart 
1571029bfd1cSPierre-Louis Bossart static int __maybe_unused intel_pm_prepare(struct device *dev)
1572029bfd1cSPierre-Louis Bossart {
1573029bfd1cSPierre-Louis Bossart 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
1574029bfd1cSPierre-Louis Bossart 	struct sdw_intel *sdw = cdns_to_intel(cdns);
1575029bfd1cSPierre-Louis Bossart 	struct sdw_bus *bus = &cdns->bus;
1576029bfd1cSPierre-Louis Bossart 	u32 clock_stop_quirks;
15779283b6f9SPierre-Louis Bossart 	int ret;
1578029bfd1cSPierre-Louis Bossart 
1579029bfd1cSPierre-Louis Bossart 	if (bus->prop.hw_disabled || !sdw->startup_done) {
1580029bfd1cSPierre-Louis Bossart 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1581029bfd1cSPierre-Louis Bossart 			bus->link_id);
1582029bfd1cSPierre-Louis Bossart 		return 0;
1583029bfd1cSPierre-Louis Bossart 	}
1584029bfd1cSPierre-Louis Bossart 
1585029bfd1cSPierre-Louis Bossart 	clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1586029bfd1cSPierre-Louis Bossart 
1587029bfd1cSPierre-Louis Bossart 	if (pm_runtime_suspended(dev) &&
1588029bfd1cSPierre-Louis Bossart 	    pm_runtime_suspended(dev->parent) &&
1589029bfd1cSPierre-Louis Bossart 	    ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
1590029bfd1cSPierre-Louis Bossart 	     !clock_stop_quirks)) {
1591029bfd1cSPierre-Louis Bossart 		/*
1592029bfd1cSPierre-Louis Bossart 		 * if we've enabled clock stop, and the parent is suspended, the SHIM registers
1593029bfd1cSPierre-Louis Bossart 		 * are not accessible and the shim wake cannot be disabled.
1594029bfd1cSPierre-Louis Bossart 		 * The only solution is to resume the entire bus to full power
1595029bfd1cSPierre-Louis Bossart 		 */
1596029bfd1cSPierre-Louis Bossart 
1597029bfd1cSPierre-Louis Bossart 		/*
1598029bfd1cSPierre-Louis Bossart 		 * If any operation in this block fails, we keep going since we don't want
1599029bfd1cSPierre-Louis Bossart 		 * to prevent system suspend from happening and errors should be recoverable
1600029bfd1cSPierre-Louis Bossart 		 * on resume.
1601029bfd1cSPierre-Louis Bossart 		 */
1602029bfd1cSPierre-Louis Bossart 
1603029bfd1cSPierre-Louis Bossart 		/*
1604029bfd1cSPierre-Louis Bossart 		 * first resume the device for this link. This will also by construction
1605029bfd1cSPierre-Louis Bossart 		 * resume the PCI parent device.
1606029bfd1cSPierre-Louis Bossart 		 */
1607029bfd1cSPierre-Louis Bossart 		ret = pm_request_resume(dev);
1608029bfd1cSPierre-Louis Bossart 		if (ret < 0) {
1609029bfd1cSPierre-Louis Bossart 			dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
1610029bfd1cSPierre-Louis Bossart 			return 0;
1611029bfd1cSPierre-Louis Bossart 		}
1612029bfd1cSPierre-Louis Bossart 
1613029bfd1cSPierre-Louis Bossart 		/*
1614029bfd1cSPierre-Louis Bossart 		 * Continue resuming the entire bus (parent + child devices) to exit
1615029bfd1cSPierre-Louis Bossart 		 * the clock stop mode. If there are no devices connected on this link
1616029bfd1cSPierre-Louis Bossart 		 * this is a no-op.
1617029bfd1cSPierre-Louis Bossart 		 * The resume to full power could have been implemented with a .prepare
1618029bfd1cSPierre-Louis Bossart 		 * step in SoundWire codec drivers. This would however require a lot
1619029bfd1cSPierre-Louis Bossart 		 * of code to handle an Intel-specific corner case. It is simpler in
1620029bfd1cSPierre-Louis Bossart 		 * practice to add a loop at the link level.
1621029bfd1cSPierre-Louis Bossart 		 */
1622029bfd1cSPierre-Louis Bossart 		ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
1623029bfd1cSPierre-Louis Bossart 
1624029bfd1cSPierre-Louis Bossart 		if (ret < 0)
1625029bfd1cSPierre-Louis Bossart 			dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
1626029bfd1cSPierre-Louis Bossart 	}
1627029bfd1cSPierre-Louis Bossart 
1628029bfd1cSPierre-Louis Bossart 	return 0;
1629029bfd1cSPierre-Louis Bossart }
1630029bfd1cSPierre-Louis Bossart 
1631f046b233SBard Liao static int __maybe_unused intel_suspend(struct device *dev)
16329b3b4b3fSPierre-Louis Bossart {
16339b3b4b3fSPierre-Louis Bossart 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
16349b3b4b3fSPierre-Louis Bossart 	struct sdw_intel *sdw = cdns_to_intel(cdns);
16359b3b4b3fSPierre-Louis Bossart 	struct sdw_bus *bus = &cdns->bus;
1636e4be9facSPierre-Louis Bossart 	u32 clock_stop_quirks;
16379b3b4b3fSPierre-Louis Bossart 	int ret;
16389b3b4b3fSPierre-Louis Bossart 
1639e4401abbSPierre-Louis Bossart 	if (bus->prop.hw_disabled || !sdw->startup_done) {
1640e4401abbSPierre-Louis Bossart 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
16419b3b4b3fSPierre-Louis Bossart 			bus->link_id);
16429b3b4b3fSPierre-Louis Bossart 		return 0;
16439b3b4b3fSPierre-Louis Bossart 	}
16449b3b4b3fSPierre-Louis Bossart 
1645b61b8b37SPierre-Louis Bossart 	if (pm_runtime_suspended(dev)) {
1646*63198aaaSPierre-Louis Bossart 		dev_dbg(dev, "pm_runtime status: suspended\n");
1647b61b8b37SPierre-Louis Bossart 
1648e4be9facSPierre-Louis Bossart 		clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1649e4be9facSPierre-Louis Bossart 
1650029bfd1cSPierre-Louis Bossart 		if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
1651029bfd1cSPierre-Louis Bossart 		    !clock_stop_quirks) {
1652e4be9facSPierre-Louis Bossart 
1653029bfd1cSPierre-Louis Bossart 			if (pm_runtime_suspended(dev->parent)) {
1654e4be9facSPierre-Louis Bossart 				/*
1655029bfd1cSPierre-Louis Bossart 				 * paranoia check: this should not happen with the .prepare
1656029bfd1cSPierre-Louis Bossart 				 * resume to full power
1657e4be9facSPierre-Louis Bossart 				 */
1658029bfd1cSPierre-Louis Bossart 				dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
1659029bfd1cSPierre-Louis Bossart 			} else {
1660e4be9facSPierre-Louis Bossart 				intel_shim_wake(sdw, false);
1661e4be9facSPierre-Louis Bossart 			}
1662029bfd1cSPierre-Louis Bossart 		}
1663e4be9facSPierre-Louis Bossart 
1664b61b8b37SPierre-Louis Bossart 		return 0;
1665b61b8b37SPierre-Louis Bossart 	}
1666b61b8b37SPierre-Louis Bossart 
16679b3b4b3fSPierre-Louis Bossart 	ret = sdw_cdns_enable_interrupt(cdns, false);
16689b3b4b3fSPierre-Louis Bossart 	if (ret < 0) {
16699b3b4b3fSPierre-Louis Bossart 		dev_err(dev, "cannot disable interrupts on suspend\n");
16709b3b4b3fSPierre-Louis Bossart 		return ret;
16719b3b4b3fSPierre-Louis Bossart 	}
16729b3b4b3fSPierre-Louis Bossart 
16739b3b4b3fSPierre-Louis Bossart 	ret = intel_link_power_down(sdw);
16749b3b4b3fSPierre-Louis Bossart 	if (ret) {
16754e3ea93eSPierre-Louis Bossart 		dev_err(dev, "Link power down failed: %d\n", ret);
16769b3b4b3fSPierre-Louis Bossart 		return ret;
16779b3b4b3fSPierre-Louis Bossart 	}
16789b3b4b3fSPierre-Louis Bossart 
16799b3b4b3fSPierre-Louis Bossart 	intel_shim_wake(sdw, false);
16809b3b4b3fSPierre-Louis Bossart 
16819b3b4b3fSPierre-Louis Bossart 	return 0;
16829b3b4b3fSPierre-Louis Bossart }
16839b3b4b3fSPierre-Louis Bossart 
168417e0da0bSArnd Bergmann static int __maybe_unused intel_suspend_runtime(struct device *dev)
1685ebf878edSPierre-Louis Bossart {
1686ebf878edSPierre-Louis Bossart 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
1687ebf878edSPierre-Louis Bossart 	struct sdw_intel *sdw = cdns_to_intel(cdns);
1688ebf878edSPierre-Louis Bossart 	struct sdw_bus *bus = &cdns->bus;
1689a320f41eSPierre-Louis Bossart 	u32 clock_stop_quirks;
1690ebf878edSPierre-Louis Bossart 	int ret;
1691ebf878edSPierre-Louis Bossart 
1692e4401abbSPierre-Louis Bossart 	if (bus->prop.hw_disabled || !sdw->startup_done) {
1693e4401abbSPierre-Louis Bossart 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1694ebf878edSPierre-Louis Bossart 			bus->link_id);
1695ebf878edSPierre-Louis Bossart 		return 0;
1696ebf878edSPierre-Louis Bossart 	}
1697ebf878edSPierre-Louis Bossart 
1698a320f41eSPierre-Louis Bossart 	clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1699a320f41eSPierre-Louis Bossart 
1700a320f41eSPierre-Louis Bossart 	if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1701a320f41eSPierre-Louis Bossart 
1702ebf878edSPierre-Louis Bossart 		ret = sdw_cdns_enable_interrupt(cdns, false);
1703ebf878edSPierre-Louis Bossart 		if (ret < 0) {
1704ebf878edSPierre-Louis Bossart 			dev_err(dev, "cannot disable interrupts on suspend\n");
1705ebf878edSPierre-Louis Bossart 			return ret;
1706ebf878edSPierre-Louis Bossart 		}
1707ebf878edSPierre-Louis Bossart 
1708ebf878edSPierre-Louis Bossart 		ret = intel_link_power_down(sdw);
1709ebf878edSPierre-Louis Bossart 		if (ret) {
17104e3ea93eSPierre-Louis Bossart 			dev_err(dev, "Link power down failed: %d\n", ret);
1711ebf878edSPierre-Louis Bossart 			return ret;
1712ebf878edSPierre-Louis Bossart 		}
1713ebf878edSPierre-Louis Bossart 
1714ebf878edSPierre-Louis Bossart 		intel_shim_wake(sdw, false);
1715ebf878edSPierre-Louis Bossart 
171661fb830bSPierre-Louis Bossart 	} else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET ||
171761fb830bSPierre-Louis Bossart 		   !clock_stop_quirks) {
1718ee3db942SBard Liao 		bool wake_enable = true;
1719ee3db942SBard Liao 
17206626a616SRander Wang 		ret = sdw_cdns_clock_stop(cdns, true);
17216626a616SRander Wang 		if (ret < 0) {
17226626a616SRander Wang 			dev_err(dev, "cannot enable clock stop on suspend\n");
1723ee3db942SBard Liao 			wake_enable = false;
17246626a616SRander Wang 		}
17256626a616SRander Wang 
17266626a616SRander Wang 		ret = sdw_cdns_enable_interrupt(cdns, false);
17276626a616SRander Wang 		if (ret < 0) {
17286626a616SRander Wang 			dev_err(dev, "cannot disable interrupts on suspend\n");
17296626a616SRander Wang 			return ret;
17306626a616SRander Wang 		}
17316626a616SRander Wang 
17326626a616SRander Wang 		ret = intel_link_power_down(sdw);
17336626a616SRander Wang 		if (ret) {
17344e3ea93eSPierre-Louis Bossart 			dev_err(dev, "Link power down failed: %d\n", ret);
17356626a616SRander Wang 			return ret;
17366626a616SRander Wang 		}
17376626a616SRander Wang 
1738ee3db942SBard Liao 		intel_shim_wake(sdw, wake_enable);
1739a320f41eSPierre-Louis Bossart 	} else {
1740a320f41eSPierre-Louis Bossart 		dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
1741a320f41eSPierre-Louis Bossart 			__func__, clock_stop_quirks);
1742a320f41eSPierre-Louis Bossart 		ret = -EINVAL;
1743a320f41eSPierre-Louis Bossart 	}
1744a320f41eSPierre-Louis Bossart 
1745a320f41eSPierre-Louis Bossart 	return ret;
1746ebf878edSPierre-Louis Bossart }
1747ebf878edSPierre-Louis Bossart 
1748f046b233SBard Liao static int __maybe_unused intel_resume(struct device *dev)
17499b3b4b3fSPierre-Louis Bossart {
17509b3b4b3fSPierre-Louis Bossart 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
17519b3b4b3fSPierre-Louis Bossart 	struct sdw_intel *sdw = cdns_to_intel(cdns);
17529b3b4b3fSPierre-Louis Bossart 	struct sdw_bus *bus = &cdns->bus;
1753a2d9c161SPierre-Louis Bossart 	int link_flags;
1754857a7c42SPierre-Louis Bossart 	bool multi_link;
17559b3b4b3fSPierre-Louis Bossart 	int ret;
17569b3b4b3fSPierre-Louis Bossart 
1757e4401abbSPierre-Louis Bossart 	if (bus->prop.hw_disabled || !sdw->startup_done) {
1758e4401abbSPierre-Louis Bossart 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
17599b3b4b3fSPierre-Louis Bossart 			bus->link_id);
17609b3b4b3fSPierre-Louis Bossart 		return 0;
17619b3b4b3fSPierre-Louis Bossart 	}
17629b3b4b3fSPierre-Louis Bossart 
1763857a7c42SPierre-Louis Bossart 	link_flags = md_flags >> (bus->link_id * 8);
1764857a7c42SPierre-Louis Bossart 	multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1765857a7c42SPierre-Louis Bossart 
1766b61b8b37SPierre-Louis Bossart 	if (pm_runtime_suspended(dev)) {
1767*63198aaaSPierre-Louis Bossart 		dev_dbg(dev, "pm_runtime status was suspended, forcing active\n");
1768b61b8b37SPierre-Louis Bossart 
1769b61b8b37SPierre-Louis Bossart 		/* follow required sequence from runtime_pm.rst */
1770b61b8b37SPierre-Louis Bossart 		pm_runtime_disable(dev);
1771b61b8b37SPierre-Louis Bossart 		pm_runtime_set_active(dev);
1772b61b8b37SPierre-Louis Bossart 		pm_runtime_mark_last_busy(dev);
1773b61b8b37SPierre-Louis Bossart 		pm_runtime_enable(dev);
1774a2d9c161SPierre-Louis Bossart 
1775a2d9c161SPierre-Louis Bossart 		link_flags = md_flags >> (bus->link_id * 8);
1776857a7c42SPierre-Louis Bossart 
1777a2d9c161SPierre-Louis Bossart 		if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
1778a2d9c161SPierre-Louis Bossart 			pm_runtime_idle(dev);
1779b61b8b37SPierre-Louis Bossart 	}
1780b61b8b37SPierre-Louis Bossart 
17819b3b4b3fSPierre-Louis Bossart 	ret = intel_init(sdw);
17829b3b4b3fSPierre-Louis Bossart 	if (ret) {
17834e3ea93eSPierre-Louis Bossart 		dev_err(dev, "%s failed: %d\n", __func__, ret);
17849b3b4b3fSPierre-Louis Bossart 		return ret;
17859b3b4b3fSPierre-Louis Bossart 	}
17869b3b4b3fSPierre-Louis Bossart 
178799b6a30fSPierre-Louis Bossart 	/*
178899b6a30fSPierre-Louis Bossart 	 * make sure all Slaves are tagged as UNATTACHED and provide
178999b6a30fSPierre-Louis Bossart 	 * reason for reinitialization
179099b6a30fSPierre-Louis Bossart 	 */
179199b6a30fSPierre-Louis Bossart 	sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
179299b6a30fSPierre-Louis Bossart 
17939b3b4b3fSPierre-Louis Bossart 	ret = sdw_cdns_enable_interrupt(cdns, true);
17949b3b4b3fSPierre-Louis Bossart 	if (ret < 0) {
17959b3b4b3fSPierre-Louis Bossart 		dev_err(dev, "cannot enable interrupts during resume\n");
17969b3b4b3fSPierre-Louis Bossart 		return ret;
17979b3b4b3fSPierre-Louis Bossart 	}
17989b3b4b3fSPierre-Louis Bossart 
1799857a7c42SPierre-Louis Bossart 	/*
1800857a7c42SPierre-Louis Bossart 	 * follow recommended programming flows to avoid timeouts when
1801857a7c42SPierre-Louis Bossart 	 * gsync is enabled
1802857a7c42SPierre-Louis Bossart 	 */
1803857a7c42SPierre-Louis Bossart 	if (multi_link)
1804857a7c42SPierre-Louis Bossart 		intel_shim_sync_arm(sdw);
1805857a7c42SPierre-Louis Bossart 
1806857a7c42SPierre-Louis Bossart 	ret = sdw_cdns_init(&sdw->cdns);
1807857a7c42SPierre-Louis Bossart 	if (ret < 0) {
1808857a7c42SPierre-Louis Bossart 		dev_err(dev, "unable to initialize Cadence IP during resume\n");
1809857a7c42SPierre-Louis Bossart 		return ret;
1810857a7c42SPierre-Louis Bossart 	}
1811857a7c42SPierre-Louis Bossart 
18129b3b4b3fSPierre-Louis Bossart 	ret = sdw_cdns_exit_reset(cdns);
18139b3b4b3fSPierre-Louis Bossart 	if (ret < 0) {
18149b3b4b3fSPierre-Louis Bossart 		dev_err(dev, "unable to exit bus reset sequence during resume\n");
18159b3b4b3fSPierre-Louis Bossart 		return ret;
18169b3b4b3fSPierre-Louis Bossart 	}
18179b3b4b3fSPierre-Louis Bossart 
1818857a7c42SPierre-Louis Bossart 	if (multi_link) {
1819857a7c42SPierre-Louis Bossart 		ret = intel_shim_sync_go(sdw);
1820857a7c42SPierre-Louis Bossart 		if (ret < 0) {
1821857a7c42SPierre-Louis Bossart 			dev_err(dev, "sync go failed during resume\n");
1822857a7c42SPierre-Louis Bossart 			return ret;
1823857a7c42SPierre-Louis Bossart 		}
1824857a7c42SPierre-Louis Bossart 	}
1825ff560946SPierre-Louis Bossart 	sdw_cdns_check_self_clearing_bits(cdns, __func__,
1826ff560946SPierre-Louis Bossart 					  true, INTEL_MASTER_RESET_ITERATIONS);
1827857a7c42SPierre-Louis Bossart 
1828cb1e6d59SPierre-Louis Bossart 	/*
1829cb1e6d59SPierre-Louis Bossart 	 * after system resume, the pm_runtime suspend() may kick in
1830cb1e6d59SPierre-Louis Bossart 	 * during the enumeration, before any children device force the
1831cb1e6d59SPierre-Louis Bossart 	 * master device to remain active.  Using pm_runtime_get()
1832cb1e6d59SPierre-Louis Bossart 	 * routines is not really possible, since it'd prevent the
1833cb1e6d59SPierre-Louis Bossart 	 * master from suspending.
1834cb1e6d59SPierre-Louis Bossart 	 * A reasonable compromise is to update the pm_runtime
1835cb1e6d59SPierre-Louis Bossart 	 * counters and delay the pm_runtime suspend by several
1836cb1e6d59SPierre-Louis Bossart 	 * seconds, by when all enumeration should be complete.
1837cb1e6d59SPierre-Louis Bossart 	 */
1838cb1e6d59SPierre-Louis Bossart 	pm_runtime_mark_last_busy(dev);
1839cb1e6d59SPierre-Louis Bossart 
18409b3b4b3fSPierre-Louis Bossart 	return ret;
18419b3b4b3fSPierre-Louis Bossart }
18429b3b4b3fSPierre-Louis Bossart 
184317e0da0bSArnd Bergmann static int __maybe_unused intel_resume_runtime(struct device *dev)
1844ebf878edSPierre-Louis Bossart {
1845ebf878edSPierre-Louis Bossart 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
1846ebf878edSPierre-Louis Bossart 	struct sdw_intel *sdw = cdns_to_intel(cdns);
1847ebf878edSPierre-Louis Bossart 	struct sdw_bus *bus = &cdns->bus;
1848a320f41eSPierre-Louis Bossart 	u32 clock_stop_quirks;
184908abad9fSRander Wang 	bool clock_stop0;
1850857a7c42SPierre-Louis Bossart 	int link_flags;
1851857a7c42SPierre-Louis Bossart 	bool multi_link;
185208abad9fSRander Wang 	int status;
1853ebf878edSPierre-Louis Bossart 	int ret;
1854ebf878edSPierre-Louis Bossart 
1855e4401abbSPierre-Louis Bossart 	if (bus->prop.hw_disabled || !sdw->startup_done) {
1856e4401abbSPierre-Louis Bossart 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
1857ebf878edSPierre-Louis Bossart 			bus->link_id);
1858ebf878edSPierre-Louis Bossart 		return 0;
1859ebf878edSPierre-Louis Bossart 	}
1860ebf878edSPierre-Louis Bossart 
1861e286472cSPierre-Louis Bossart 	/* unconditionally disable WAKEEN interrupt */
1862e286472cSPierre-Louis Bossart 	intel_shim_wake(sdw, false);
1863e286472cSPierre-Louis Bossart 
1864857a7c42SPierre-Louis Bossart 	link_flags = md_flags >> (bus->link_id * 8);
1865857a7c42SPierre-Louis Bossart 	multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
1866857a7c42SPierre-Louis Bossart 
1867a320f41eSPierre-Louis Bossart 	clock_stop_quirks = sdw->link_res->clock_stop_quirks;
1868a320f41eSPierre-Louis Bossart 
1869a320f41eSPierre-Louis Bossart 	if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
1870ebf878edSPierre-Louis Bossart 		ret = intel_init(sdw);
1871ebf878edSPierre-Louis Bossart 		if (ret) {
18724e3ea93eSPierre-Louis Bossart 			dev_err(dev, "%s failed: %d\n", __func__, ret);
1873ebf878edSPierre-Louis Bossart 			return ret;
1874ebf878edSPierre-Louis Bossart 		}
1875ebf878edSPierre-Louis Bossart 
187699b6a30fSPierre-Louis Bossart 		/*
187799b6a30fSPierre-Louis Bossart 		 * make sure all Slaves are tagged as UNATTACHED and provide
187899b6a30fSPierre-Louis Bossart 		 * reason for reinitialization
187999b6a30fSPierre-Louis Bossart 		 */
188099b6a30fSPierre-Louis Bossart 		sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
188199b6a30fSPierre-Louis Bossart 
1882ebf878edSPierre-Louis Bossart 		ret = sdw_cdns_enable_interrupt(cdns, true);
1883ebf878edSPierre-Louis Bossart 		if (ret < 0) {
1884ebf878edSPierre-Louis Bossart 			dev_err(dev, "cannot enable interrupts during resume\n");
1885ebf878edSPierre-Louis Bossart 			return ret;
1886ebf878edSPierre-Louis Bossart 		}
1887ebf878edSPierre-Louis Bossart 
1888857a7c42SPierre-Louis Bossart 		/*
1889857a7c42SPierre-Louis Bossart 		 * follow recommended programming flows to avoid
1890857a7c42SPierre-Louis Bossart 		 * timeouts when gsync is enabled
1891857a7c42SPierre-Louis Bossart 		 */
1892857a7c42SPierre-Louis Bossart 		if (multi_link)
1893857a7c42SPierre-Louis Bossart 			intel_shim_sync_arm(sdw);
1894857a7c42SPierre-Louis Bossart 
1895857a7c42SPierre-Louis Bossart 		ret = sdw_cdns_init(&sdw->cdns);
1896857a7c42SPierre-Louis Bossart 		if (ret < 0) {
1897857a7c42SPierre-Louis Bossart 			dev_err(dev, "unable to initialize Cadence IP during resume\n");
1898857a7c42SPierre-Louis Bossart 			return ret;
1899857a7c42SPierre-Louis Bossart 		}
1900857a7c42SPierre-Louis Bossart 
1901ebf878edSPierre-Louis Bossart 		ret = sdw_cdns_exit_reset(cdns);
1902ebf878edSPierre-Louis Bossart 		if (ret < 0) {
1903ebf878edSPierre-Louis Bossart 			dev_err(dev, "unable to exit bus reset sequence during resume\n");
1904ebf878edSPierre-Louis Bossart 			return ret;
1905ebf878edSPierre-Louis Bossart 		}
1906857a7c42SPierre-Louis Bossart 
1907857a7c42SPierre-Louis Bossart 		if (multi_link) {
1908857a7c42SPierre-Louis Bossart 			ret = intel_shim_sync_go(sdw);
1909857a7c42SPierre-Louis Bossart 			if (ret < 0) {
1910857a7c42SPierre-Louis Bossart 				dev_err(dev, "sync go failed during resume\n");
1911857a7c42SPierre-Louis Bossart 				return ret;
1912857a7c42SPierre-Louis Bossart 			}
1913857a7c42SPierre-Louis Bossart 		}
1914ff560946SPierre-Louis Bossart 		sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime TEARDOWN",
1915ff560946SPierre-Louis Bossart 						  true, INTEL_MASTER_RESET_ITERATIONS);
1916ff560946SPierre-Louis Bossart 
19176626a616SRander Wang 	} else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
19186626a616SRander Wang 		ret = intel_init(sdw);
19196626a616SRander Wang 		if (ret) {
19204e3ea93eSPierre-Louis Bossart 			dev_err(dev, "%s failed: %d\n", __func__, ret);
19216626a616SRander Wang 			return ret;
19226626a616SRander Wang 		}
19236626a616SRander Wang 
19246626a616SRander Wang 		/*
192508abad9fSRander Wang 		 * An exception condition occurs for the CLK_STOP_BUS_RESET
192608abad9fSRander Wang 		 * case if one or more masters remain active. In this condition,
192708abad9fSRander Wang 		 * all the masters are powered on for they are in the same power
192808abad9fSRander Wang 		 * domain. Master can preserve its context for clock stop0, so
192908abad9fSRander Wang 		 * there is no need to clear slave status and reset bus.
193008abad9fSRander Wang 		 */
193108abad9fSRander Wang 		clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
193208abad9fSRander Wang 
1933857a7c42SPierre-Louis Bossart 		if (!clock_stop0) {
1934857a7c42SPierre-Louis Bossart 
1935857a7c42SPierre-Louis Bossart 			/*
19366626a616SRander Wang 			 * make sure all Slaves are tagged as UNATTACHED and
19376626a616SRander Wang 			 * provide reason for reinitialization
19386626a616SRander Wang 			 */
1939857a7c42SPierre-Louis Bossart 
194008abad9fSRander Wang 			status = SDW_UNATTACH_REQUEST_MASTER_RESET;
194108abad9fSRander Wang 			sdw_clear_slave_status(bus, status);
19426626a616SRander Wang 
19436626a616SRander Wang 			ret = sdw_cdns_enable_interrupt(cdns, true);
19446626a616SRander Wang 			if (ret < 0) {
19456626a616SRander Wang 				dev_err(dev, "cannot enable interrupts during resume\n");
19466626a616SRander Wang 				return ret;
19476626a616SRander Wang 			}
19486626a616SRander Wang 
1949d78071b4SPierre-Louis Bossart 			/*
1950d78071b4SPierre-Louis Bossart 			 * follow recommended programming flows to avoid
1951d78071b4SPierre-Louis Bossart 			 * timeouts when gsync is enabled
1952d78071b4SPierre-Louis Bossart 			 */
1953d78071b4SPierre-Louis Bossart 			if (multi_link)
1954d78071b4SPierre-Louis Bossart 				intel_shim_sync_arm(sdw);
1955d78071b4SPierre-Louis Bossart 
1956d78071b4SPierre-Louis Bossart 			/*
1957d78071b4SPierre-Louis Bossart 			 * Re-initialize the IP since it was powered-off
1958d78071b4SPierre-Louis Bossart 			 */
1959d78071b4SPierre-Louis Bossart 			sdw_cdns_init(&sdw->cdns);
1960d78071b4SPierre-Louis Bossart 
1961d78071b4SPierre-Louis Bossart 		} else {
1962d78071b4SPierre-Louis Bossart 			ret = sdw_cdns_enable_interrupt(cdns, true);
1963d78071b4SPierre-Louis Bossart 			if (ret < 0) {
1964d78071b4SPierre-Louis Bossart 				dev_err(dev, "cannot enable interrupts during resume\n");
1965d78071b4SPierre-Louis Bossart 				return ret;
1966d78071b4SPierre-Louis Bossart 			}
1967d78071b4SPierre-Louis Bossart 		}
1968d78071b4SPierre-Louis Bossart 
196908abad9fSRander Wang 		ret = sdw_cdns_clock_restart(cdns, !clock_stop0);
19706626a616SRander Wang 		if (ret < 0) {
19716626a616SRander Wang 			dev_err(dev, "unable to restart clock during resume\n");
19726626a616SRander Wang 			return ret;
19736626a616SRander Wang 		}
1974d78071b4SPierre-Louis Bossart 
1975d78071b4SPierre-Louis Bossart 		if (!clock_stop0) {
1976d78071b4SPierre-Louis Bossart 			ret = sdw_cdns_exit_reset(cdns);
1977d78071b4SPierre-Louis Bossart 			if (ret < 0) {
1978d78071b4SPierre-Louis Bossart 				dev_err(dev, "unable to exit bus reset sequence during resume\n");
1979d78071b4SPierre-Louis Bossart 				return ret;
1980d78071b4SPierre-Louis Bossart 			}
1981d78071b4SPierre-Louis Bossart 
1982d78071b4SPierre-Louis Bossart 			if (multi_link) {
1983d78071b4SPierre-Louis Bossart 				ret = intel_shim_sync_go(sdw);
1984d78071b4SPierre-Louis Bossart 				if (ret < 0) {
1985d78071b4SPierre-Louis Bossart 					dev_err(sdw->cdns.dev, "sync go failed during resume\n");
1986d78071b4SPierre-Louis Bossart 					return ret;
1987d78071b4SPierre-Louis Bossart 				}
1988d78071b4SPierre-Louis Bossart 			}
1989d78071b4SPierre-Louis Bossart 		}
1990ff560946SPierre-Louis Bossart 		sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime BUS_RESET",
1991ff560946SPierre-Louis Bossart 						  true, INTEL_MASTER_RESET_ITERATIONS);
1992ff560946SPierre-Louis Bossart 
199361fb830bSPierre-Louis Bossart 	} else if (!clock_stop_quirks) {
1994f748f34eSPierre-Louis Bossart 
1995f748f34eSPierre-Louis Bossart 		clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
1996f748f34eSPierre-Louis Bossart 		if (!clock_stop0)
1997f748f34eSPierre-Louis Bossart 			dev_err(dev, "%s invalid configuration, clock was not stopped", __func__);
1998f748f34eSPierre-Louis Bossart 
199961fb830bSPierre-Louis Bossart 		ret = intel_init(sdw);
200061fb830bSPierre-Louis Bossart 		if (ret) {
20014e3ea93eSPierre-Louis Bossart 			dev_err(dev, "%s failed: %d\n", __func__, ret);
200261fb830bSPierre-Louis Bossart 			return ret;
200361fb830bSPierre-Louis Bossart 		}
200461fb830bSPierre-Louis Bossart 
200561fb830bSPierre-Louis Bossart 		ret = sdw_cdns_enable_interrupt(cdns, true);
200661fb830bSPierre-Louis Bossart 		if (ret < 0) {
200761fb830bSPierre-Louis Bossart 			dev_err(dev, "cannot enable interrupts during resume\n");
200861fb830bSPierre-Louis Bossart 			return ret;
200961fb830bSPierre-Louis Bossart 		}
201061fb830bSPierre-Louis Bossart 
201161fb830bSPierre-Louis Bossart 		ret = sdw_cdns_clock_restart(cdns, false);
201261fb830bSPierre-Louis Bossart 		if (ret < 0) {
201361fb830bSPierre-Louis Bossart 			dev_err(dev, "unable to resume master during resume\n");
201461fb830bSPierre-Louis Bossart 			return ret;
201561fb830bSPierre-Louis Bossart 		}
2016ff560946SPierre-Louis Bossart 
2017ff560946SPierre-Louis Bossart 		sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime no_quirks",
2018ff560946SPierre-Louis Bossart 						  true, INTEL_MASTER_RESET_ITERATIONS);
2019a320f41eSPierre-Louis Bossart 	} else {
2020a320f41eSPierre-Louis Bossart 		dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
2021a320f41eSPierre-Louis Bossart 			__func__, clock_stop_quirks);
2022a320f41eSPierre-Louis Bossart 		ret = -EINVAL;
2023a320f41eSPierre-Louis Bossart 	}
2024ebf878edSPierre-Louis Bossart 
2025ebf878edSPierre-Louis Bossart 	return ret;
2026ebf878edSPierre-Louis Bossart }
2027ebf878edSPierre-Louis Bossart 
20289b3b4b3fSPierre-Louis Bossart static const struct dev_pm_ops intel_pm = {
2029029bfd1cSPierre-Louis Bossart 	.prepare = intel_pm_prepare,
20309b3b4b3fSPierre-Louis Bossart 	SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
2031ebf878edSPierre-Louis Bossart 	SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
20329b3b4b3fSPierre-Louis Bossart };
20339b3b4b3fSPierre-Louis Bossart 
203429a269c6SPierre-Louis Bossart static const struct auxiliary_device_id intel_link_id_table[] = {
203529a269c6SPierre-Louis Bossart 	{ .name = "soundwire_intel.link" },
203629a269c6SPierre-Louis Bossart 	{},
203771bb8a1bSVinod Koul };
203829a269c6SPierre-Louis Bossart MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
203971bb8a1bSVinod Koul 
204029a269c6SPierre-Louis Bossart static struct auxiliary_driver sdw_intel_drv = {
204129a269c6SPierre-Louis Bossart 	.probe = intel_link_probe,
204229a269c6SPierre-Louis Bossart 	.remove = intel_link_remove,
204329a269c6SPierre-Louis Bossart 	.driver = {
204429a269c6SPierre-Louis Bossart 		/* auxiliary_driver_register() sets .name to be the modname */
204529a269c6SPierre-Louis Bossart 		.pm = &intel_pm,
204629a269c6SPierre-Louis Bossart 	},
204729a269c6SPierre-Louis Bossart 	.id_table = intel_link_id_table
204829a269c6SPierre-Louis Bossart };
204929a269c6SPierre-Louis Bossart module_auxiliary_driver(sdw_intel_drv);
205071bb8a1bSVinod Koul 
205171bb8a1bSVinod Koul MODULE_LICENSE("Dual BSD/GPL");
205229a269c6SPierre-Louis Bossart MODULE_DESCRIPTION("Intel Soundwire Link Driver");
2053