xref: /linux/drivers/soundwire/intel.c (revision e4fcf153d91809aefa6860d285e747fd7dd9e61c)
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>
9*e4fcf153SKrzysztof Kozlowski #include <linux/cleanup.h>
1079ee6631SPierre-Louis Bossart #include <linux/debugfs.h>
1171bb8a1bSVinod Koul #include <linux/delay.h>
12df72b719SPierre-Louis Bossart #include <linux/io.h>
1337a2d22bSVinod Koul #include <sound/pcm_params.h>
14ab2c9132SRander Wang #include <linux/pm_runtime.h>
1537a2d22bSVinod Koul #include <sound/soc.h>
1671bb8a1bSVinod Koul #include <linux/soundwire/sdw_registers.h>
1771bb8a1bSVinod Koul #include <linux/soundwire/sdw.h>
1871bb8a1bSVinod Koul #include <linux/soundwire/sdw_intel.h>
1971bb8a1bSVinod Koul #include "cadence_master.h"
2079ee6631SPierre-Louis Bossart #include "bus.h"
2171bb8a1bSVinod Koul #include "intel.h"
2271bb8a1bSVinod Koul 
237d2845d5SPierre-Louis Bossart static int intel_wait_bit(void __iomem *base, int offset, u32 mask, u32 target)
2471bb8a1bSVinod Koul {
2571bb8a1bSVinod Koul 	int timeout = 10;
2671bb8a1bSVinod Koul 	u32 reg_read;
2771bb8a1bSVinod Koul 
2871bb8a1bSVinod Koul 	do {
2971bb8a1bSVinod Koul 		reg_read = readl(base + offset);
307d2845d5SPierre-Louis Bossart 		if ((reg_read & mask) == target)
3171bb8a1bSVinod Koul 			return 0;
3271bb8a1bSVinod Koul 
3371bb8a1bSVinod Koul 		timeout--;
347d2845d5SPierre-Louis Bossart 		usleep_range(50, 100);
3571bb8a1bSVinod Koul 	} while (timeout != 0);
3671bb8a1bSVinod Koul 
3771bb8a1bSVinod Koul 	return -EAGAIN;
3871bb8a1bSVinod Koul }
3971bb8a1bSVinod Koul 
407d2845d5SPierre-Louis Bossart static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
417d2845d5SPierre-Louis Bossart {
427d2845d5SPierre-Louis Bossart 	writel(value, base + offset);
437d2845d5SPierre-Louis Bossart 	return intel_wait_bit(base, offset, mask, 0);
447d2845d5SPierre-Louis Bossart }
457d2845d5SPierre-Louis Bossart 
4671bb8a1bSVinod Koul static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
4771bb8a1bSVinod Koul {
4871bb8a1bSVinod Koul 	writel(value, base + offset);
497d2845d5SPierre-Louis Bossart 	return intel_wait_bit(base, offset, mask, mask);
5071bb8a1bSVinod Koul }
5171bb8a1bSVinod Koul 
5271bb8a1bSVinod Koul /*
5379ee6631SPierre-Louis Bossart  * debugfs
5479ee6631SPierre-Louis Bossart  */
5579ee6631SPierre-Louis Bossart #ifdef CONFIG_DEBUG_FS
5679ee6631SPierre-Louis Bossart 
5779ee6631SPierre-Louis Bossart #define RD_BUF (2 * PAGE_SIZE)
5879ee6631SPierre-Louis Bossart 
5979ee6631SPierre-Louis Bossart static ssize_t intel_sprintf(void __iomem *mem, bool l,
6079ee6631SPierre-Louis Bossart 			     char *buf, size_t pos, unsigned int reg)
6179ee6631SPierre-Louis Bossart {
6279ee6631SPierre-Louis Bossart 	int value;
6379ee6631SPierre-Louis Bossart 
6479ee6631SPierre-Louis Bossart 	if (l)
6579ee6631SPierre-Louis Bossart 		value = intel_readl(mem, reg);
6679ee6631SPierre-Louis Bossart 	else
6779ee6631SPierre-Louis Bossart 		value = intel_readw(mem, reg);
6879ee6631SPierre-Louis Bossart 
6979ee6631SPierre-Louis Bossart 	return scnprintf(buf + pos, RD_BUF - pos, "%4x\t%4x\n", reg, value);
7079ee6631SPierre-Louis Bossart }
7179ee6631SPierre-Louis Bossart 
7279ee6631SPierre-Louis Bossart static int intel_reg_show(struct seq_file *s_file, void *data)
7379ee6631SPierre-Louis Bossart {
7479ee6631SPierre-Louis Bossart 	struct sdw_intel *sdw = s_file->private;
752523486bSPierre-Louis Bossart 	void __iomem *s = sdw->link_res->shim;
762523486bSPierre-Louis Bossart 	void __iomem *a = sdw->link_res->alh;
7779ee6631SPierre-Louis Bossart 	ssize_t ret;
7879ee6631SPierre-Louis Bossart 	int i, j;
7979ee6631SPierre-Louis Bossart 	unsigned int links, reg;
8079ee6631SPierre-Louis Bossart 
81*e4fcf153SKrzysztof Kozlowski 	char *buf __free(kfree) = kzalloc(RD_BUF, GFP_KERNEL);
8279ee6631SPierre-Louis Bossart 	if (!buf)
8379ee6631SPierre-Louis Bossart 		return -ENOMEM;
8479ee6631SPierre-Louis Bossart 
857f817068SPierre-Louis Bossart 	links = intel_readl(s, SDW_SHIM_LCAP) & SDW_SHIM_LCAP_LCOUNT_MASK;
8679ee6631SPierre-Louis Bossart 
8779ee6631SPierre-Louis Bossart 	ret = scnprintf(buf, RD_BUF, "Register  Value\n");
8879ee6631SPierre-Louis Bossart 	ret += scnprintf(buf + ret, RD_BUF - ret, "\nShim\n");
8979ee6631SPierre-Louis Bossart 
9079ee6631SPierre-Louis Bossart 	for (i = 0; i < links; i++) {
9179ee6631SPierre-Louis Bossart 		reg = SDW_SHIM_LCAP + i * 4;
9279ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, true, buf, ret, reg);
9379ee6631SPierre-Louis Bossart 	}
9479ee6631SPierre-Louis Bossart 
9579ee6631SPierre-Louis Bossart 	for (i = 0; i < links; i++) {
9679ee6631SPierre-Louis Bossart 		ret += scnprintf(buf + ret, RD_BUF - ret, "\nLink%d\n", i);
9779ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLSCAP(i));
9879ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS0CM(i));
9979ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS1CM(i));
10079ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS2CM(i));
10179ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS3CM(i));
10279ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PCMSCAP(i));
10379ee6631SPierre-Louis Bossart 
10479ee6631SPierre-Louis Bossart 		ret += scnprintf(buf + ret, RD_BUF - ret, "\n PCMSyCH registers\n");
10579ee6631SPierre-Louis Bossart 
10679ee6631SPierre-Louis Bossart 		/*
10779ee6631SPierre-Louis Bossart 		 * the value 10 is the number of PDIs. We will need a
10879ee6631SPierre-Louis Bossart 		 * cleanup to remove hard-coded Intel configurations
10979ee6631SPierre-Louis Bossart 		 * from cadence_master.c
11079ee6631SPierre-Louis Bossart 		 */
11179ee6631SPierre-Louis Bossart 		for (j = 0; j < 10; j++) {
11279ee6631SPierre-Louis Bossart 			ret += intel_sprintf(s, false, buf, ret,
11379ee6631SPierre-Louis Bossart 					SDW_SHIM_PCMSYCHM(i, j));
11479ee6631SPierre-Louis Bossart 			ret += intel_sprintf(s, false, buf, ret,
11579ee6631SPierre-Louis Bossart 					SDW_SHIM_PCMSYCHC(i, j));
11679ee6631SPierre-Louis Bossart 		}
117c27ce5c9SPierre-Louis Bossart 		ret += scnprintf(buf + ret, RD_BUF - ret, "\n IOCTL, CTMCTL\n");
11879ee6631SPierre-Louis Bossart 
11979ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_IOCTL(i));
12079ee6631SPierre-Louis Bossart 		ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTMCTL(i));
12179ee6631SPierre-Louis Bossart 	}
12279ee6631SPierre-Louis Bossart 
12379ee6631SPierre-Louis Bossart 	ret += scnprintf(buf + ret, RD_BUF - ret, "\nWake registers\n");
12479ee6631SPierre-Louis Bossart 	ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKEEN);
12579ee6631SPierre-Louis Bossart 	ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKESTS);
12679ee6631SPierre-Louis Bossart 
12779ee6631SPierre-Louis Bossart 	ret += scnprintf(buf + ret, RD_BUF - ret, "\nALH STRMzCFG\n");
12879ee6631SPierre-Louis Bossart 	for (i = 0; i < SDW_ALH_NUM_STREAMS; i++)
12979ee6631SPierre-Louis Bossart 		ret += intel_sprintf(a, true, buf, ret, SDW_ALH_STRMZCFG(i));
13079ee6631SPierre-Louis Bossart 
13179ee6631SPierre-Louis Bossart 	seq_printf(s_file, "%s", buf);
13279ee6631SPierre-Louis Bossart 
13379ee6631SPierre-Louis Bossart 	return 0;
13479ee6631SPierre-Louis Bossart }
13579ee6631SPierre-Louis Bossart DEFINE_SHOW_ATTRIBUTE(intel_reg);
13679ee6631SPierre-Louis Bossart 
1370f9138e7SPierre-Louis Bossart static int intel_set_m_datamode(void *data, u64 value)
1380f9138e7SPierre-Louis Bossart {
1390f9138e7SPierre-Louis Bossart 	struct sdw_intel *sdw = data;
1400f9138e7SPierre-Louis Bossart 	struct sdw_bus *bus = &sdw->cdns.bus;
1410f9138e7SPierre-Louis Bossart 
1420f9138e7SPierre-Louis Bossart 	if (value > SDW_PORT_DATA_MODE_STATIC_1)
1430f9138e7SPierre-Louis Bossart 		return -EINVAL;
1440f9138e7SPierre-Louis Bossart 
1450f9138e7SPierre-Louis Bossart 	/* Userspace changed the hardware state behind the kernel's back */
1460f9138e7SPierre-Louis Bossart 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
1470f9138e7SPierre-Louis Bossart 
1480f9138e7SPierre-Louis Bossart 	bus->params.m_data_mode = value;
1490f9138e7SPierre-Louis Bossart 
1500f9138e7SPierre-Louis Bossart 	return 0;
1510f9138e7SPierre-Louis Bossart }
1520f9138e7SPierre-Louis Bossart DEFINE_DEBUGFS_ATTRIBUTE(intel_set_m_datamode_fops, NULL,
1530f9138e7SPierre-Louis Bossart 			 intel_set_m_datamode, "%llu\n");
1540f9138e7SPierre-Louis Bossart 
1550f9138e7SPierre-Louis Bossart static int intel_set_s_datamode(void *data, u64 value)
1560f9138e7SPierre-Louis Bossart {
1570f9138e7SPierre-Louis Bossart 	struct sdw_intel *sdw = data;
1580f9138e7SPierre-Louis Bossart 	struct sdw_bus *bus = &sdw->cdns.bus;
1590f9138e7SPierre-Louis Bossart 
1600f9138e7SPierre-Louis Bossart 	if (value > SDW_PORT_DATA_MODE_STATIC_1)
1610f9138e7SPierre-Louis Bossart 		return -EINVAL;
1620f9138e7SPierre-Louis Bossart 
1630f9138e7SPierre-Louis Bossart 	/* Userspace changed the hardware state behind the kernel's back */
1640f9138e7SPierre-Louis Bossart 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
1650f9138e7SPierre-Louis Bossart 
1660f9138e7SPierre-Louis Bossart 	bus->params.s_data_mode = value;
1670f9138e7SPierre-Louis Bossart 
1680f9138e7SPierre-Louis Bossart 	return 0;
1690f9138e7SPierre-Louis Bossart }
1700f9138e7SPierre-Louis Bossart DEFINE_DEBUGFS_ATTRIBUTE(intel_set_s_datamode_fops, NULL,
1710f9138e7SPierre-Louis Bossart 			 intel_set_s_datamode, "%llu\n");
1720f9138e7SPierre-Louis Bossart 
17379ee6631SPierre-Louis Bossart static void intel_debugfs_init(struct sdw_intel *sdw)
17479ee6631SPierre-Louis Bossart {
17579ee6631SPierre-Louis Bossart 	struct dentry *root = sdw->cdns.bus.debugfs;
17679ee6631SPierre-Louis Bossart 
17779ee6631SPierre-Louis Bossart 	if (!root)
17879ee6631SPierre-Louis Bossart 		return;
17979ee6631SPierre-Louis Bossart 
18079ee6631SPierre-Louis Bossart 	sdw->debugfs = debugfs_create_dir("intel-sdw", root);
18179ee6631SPierre-Louis Bossart 
18279ee6631SPierre-Louis Bossart 	debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw,
18379ee6631SPierre-Louis Bossart 			    &intel_reg_fops);
18479ee6631SPierre-Louis Bossart 
1850f9138e7SPierre-Louis Bossart 	debugfs_create_file("intel-m-datamode", 0200, sdw->debugfs, sdw,
1860f9138e7SPierre-Louis Bossart 			    &intel_set_m_datamode_fops);
1870f9138e7SPierre-Louis Bossart 
1880f9138e7SPierre-Louis Bossart 	debugfs_create_file("intel-s-datamode", 0200, sdw->debugfs, sdw,
1890f9138e7SPierre-Louis Bossart 			    &intel_set_s_datamode_fops);
1900f9138e7SPierre-Louis Bossart 
19179ee6631SPierre-Louis Bossart 	sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
19279ee6631SPierre-Louis Bossart }
19379ee6631SPierre-Louis Bossart 
19479ee6631SPierre-Louis Bossart static void intel_debugfs_exit(struct sdw_intel *sdw)
19579ee6631SPierre-Louis Bossart {
19679ee6631SPierre-Louis Bossart 	debugfs_remove_recursive(sdw->debugfs);
19779ee6631SPierre-Louis Bossart }
19879ee6631SPierre-Louis Bossart #else
19979ee6631SPierre-Louis Bossart static void intel_debugfs_init(struct sdw_intel *sdw) {}
20079ee6631SPierre-Louis Bossart static void intel_debugfs_exit(struct sdw_intel *sdw) {}
20179ee6631SPierre-Louis Bossart #endif /* CONFIG_DEBUG_FS */
20279ee6631SPierre-Louis Bossart 
20379ee6631SPierre-Louis Bossart /*
20471bb8a1bSVinod Koul  * shim ops
20571bb8a1bSVinod Koul  */
2064a17c441SPierre-Louis Bossart /* this needs to be called with shim_lock */
2074a17c441SPierre-Louis Bossart static void intel_shim_glue_to_master_ip(struct sdw_intel *sdw)
20871bb8a1bSVinod Koul {
2092523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
21071bb8a1bSVinod Koul 	unsigned int link_id = sdw->instance;
2114a17c441SPierre-Louis Bossart 	u16 ioctl;
21271bb8a1bSVinod Koul 
21371bb8a1bSVinod Koul 	/* Switch to MIP from Glue logic */
21471bb8a1bSVinod Koul 	ioctl = intel_readw(shim,  SDW_SHIM_IOCTL(link_id));
21571bb8a1bSVinod Koul 
21671bb8a1bSVinod Koul 	ioctl &= ~(SDW_SHIM_IOCTL_DOE);
21771bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2184a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
21971bb8a1bSVinod Koul 
22071bb8a1bSVinod Koul 	ioctl &= ~(SDW_SHIM_IOCTL_DO);
22171bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2224a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
22371bb8a1bSVinod Koul 
22471bb8a1bSVinod Koul 	ioctl |= (SDW_SHIM_IOCTL_MIF);
22571bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2264a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
22771bb8a1bSVinod Koul 
22871bb8a1bSVinod Koul 	ioctl &= ~(SDW_SHIM_IOCTL_BKE);
22971bb8a1bSVinod Koul 	ioctl &= ~(SDW_SHIM_IOCTL_COE);
23071bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2314a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2324a17c441SPierre-Louis Bossart 
2334a17c441SPierre-Louis Bossart 	/* at this point Master IP has full control of the I/Os */
2344a17c441SPierre-Louis Bossart }
2354a17c441SPierre-Louis Bossart 
2364a17c441SPierre-Louis Bossart /* this needs to be called with shim_lock */
2374a17c441SPierre-Louis Bossart static void intel_shim_master_ip_to_glue(struct sdw_intel *sdw)
2384a17c441SPierre-Louis Bossart {
2394a17c441SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
2404a17c441SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
2414a17c441SPierre-Louis Bossart 	u16 ioctl;
2424a17c441SPierre-Louis Bossart 
2434a17c441SPierre-Louis Bossart 	/* Glue logic */
2444a17c441SPierre-Louis Bossart 	ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
2454a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_BKE;
2464a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_COE;
2474a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2484a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2494a17c441SPierre-Louis Bossart 
2504a17c441SPierre-Louis Bossart 	ioctl &= ~(SDW_SHIM_IOCTL_MIF);
2514a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2524a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2534a17c441SPierre-Louis Bossart 
2544a17c441SPierre-Louis Bossart 	/* at this point Integration Glue has full control of the I/Os */
2554a17c441SPierre-Louis Bossart }
2564a17c441SPierre-Louis Bossart 
257b81bcdb4SPierre-Louis Bossart /* this needs to be called with shim_lock */
258b81bcdb4SPierre-Louis Bossart static void intel_shim_init(struct sdw_intel *sdw)
2594a17c441SPierre-Louis Bossart {
2604a17c441SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
2614a17c441SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
2623d912d1aSChao Song 	u16 ioctl = 0, act;
2634a17c441SPierre-Louis Bossart 
2644a17c441SPierre-Louis Bossart 	/* Initialize Shim */
2654a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_BKE;
2664a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2674a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2684a17c441SPierre-Louis Bossart 
2694a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_WPDD;
2704a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2714a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2724a17c441SPierre-Louis Bossart 
2734a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_DO;
2744a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2754a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2764a17c441SPierre-Louis Bossart 
2774a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_DOE;
2784a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2794a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2804a17c441SPierre-Louis Bossart 
2814a17c441SPierre-Louis Bossart 	intel_shim_glue_to_master_ip(sdw);
28271bb8a1bSVinod Koul 
2833d912d1aSChao Song 	act = intel_readw(shim, SDW_SHIM_CTMCTL(link_id));
284f067c925SVinod Koul 	u16p_replace_bits(&act, 0x1, SDW_SHIM_CTMCTL_DOAIS);
28571bb8a1bSVinod Koul 	act |= SDW_SHIM_CTMCTL_DACTQE;
28671bb8a1bSVinod Koul 	act |= SDW_SHIM_CTMCTL_DODS;
28771bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
2884a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
28971bb8a1bSVinod Koul }
29071bb8a1bSVinod Koul 
2910f3c54c2SPierre-Louis Bossart static int intel_shim_check_wake(struct sdw_intel *sdw)
2920f3c54c2SPierre-Louis Bossart {
2930f3c54c2SPierre-Louis Bossart 	void __iomem *shim;
2940f3c54c2SPierre-Louis Bossart 	u16 wake_sts;
29571bb8a1bSVinod Koul 
2960f3c54c2SPierre-Louis Bossart 	shim = sdw->link_res->shim;
2970f3c54c2SPierre-Louis Bossart 	wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
2980f3c54c2SPierre-Louis Bossart 
2990f3c54c2SPierre-Louis Bossart 	return wake_sts & BIT(sdw->instance);
30071bb8a1bSVinod Koul }
30171bb8a1bSVinod Koul 
302ab2c9132SRander Wang static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
3034a17c441SPierre-Louis Bossart {
3044a17c441SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
3054a17c441SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
3064a17c441SPierre-Louis Bossart 	u16 wake_en, wake_sts;
3074a17c441SPierre-Louis Bossart 
3084a17c441SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
3094a17c441SPierre-Louis Bossart 	wake_en = intel_readw(shim, SDW_SHIM_WAKEEN);
3104a17c441SPierre-Louis Bossart 
3114a17c441SPierre-Louis Bossart 	if (wake_enable) {
3124a17c441SPierre-Louis Bossart 		/* Enable the wakeup */
3134a17c441SPierre-Louis Bossart 		wake_en |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
3144a17c441SPierre-Louis Bossart 		intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
3154a17c441SPierre-Louis Bossart 	} else {
3164a17c441SPierre-Louis Bossart 		/* Disable the wake up interrupt */
3174a17c441SPierre-Louis Bossart 		wake_en &= ~(SDW_SHIM_WAKEEN_ENABLE << link_id);
3184a17c441SPierre-Louis Bossart 		intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
3194a17c441SPierre-Louis Bossart 
3204a17c441SPierre-Louis Bossart 		/* Clear wake status */
3214a17c441SPierre-Louis Bossart 		wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
3223957db3aSLibin Yang 		wake_sts |= (SDW_SHIM_WAKESTS_STATUS << link_id);
3233957db3aSLibin Yang 		intel_writew(shim, SDW_SHIM_WAKESTS, wake_sts);
3244a17c441SPierre-Louis Bossart 	}
3254a17c441SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
3264a17c441SPierre-Louis Bossart }
3274a17c441SPierre-Louis Bossart 
3281e76de2eSPierre-Louis Bossart static bool intel_check_cmdsync_unlocked(struct sdw_intel *sdw)
3291e76de2eSPierre-Louis Bossart {
3301e76de2eSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
3311e76de2eSPierre-Louis Bossart 	int sync_reg;
3321e76de2eSPierre-Louis Bossart 
3331e76de2eSPierre-Louis Bossart 	sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
3341e76de2eSPierre-Louis Bossart 	return !!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK);
3351e76de2eSPierre-Louis Bossart }
3361e76de2eSPierre-Louis Bossart 
337bc872947SPierre-Louis Bossart static int intel_link_power_up(struct sdw_intel *sdw)
338bc872947SPierre-Louis Bossart {
339bc872947SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
340bc872947SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
341bc872947SPierre-Louis Bossart 	u32 *shim_mask = sdw->link_res->shim_mask;
342bc872947SPierre-Louis Bossart 	struct sdw_bus *bus = &sdw->cdns.bus;
343bc872947SPierre-Louis Bossart 	struct sdw_master_prop *prop = &bus->prop;
344bc872947SPierre-Louis Bossart 	u32 spa_mask, cpa_mask;
345bc872947SPierre-Louis Bossart 	u32 link_control;
346bc872947SPierre-Louis Bossart 	int ret = 0;
34709ee49e3SPierre-Louis Bossart 	u32 clock_source;
348bc872947SPierre-Louis Bossart 	u32 syncprd;
349bc872947SPierre-Louis Bossart 	u32 sync_reg;
35009ee49e3SPierre-Louis Bossart 	bool lcap_mlcs;
351bc872947SPierre-Louis Bossart 
352bc872947SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
353bc872947SPierre-Louis Bossart 
354bc872947SPierre-Louis Bossart 	/*
355bc872947SPierre-Louis Bossart 	 * The hardware relies on an internal counter, typically 4kHz,
356bc872947SPierre-Louis Bossart 	 * to generate the SoundWire SSP - which defines a 'safe'
357bc872947SPierre-Louis Bossart 	 * synchronization point between commands and audio transport
358bc872947SPierre-Louis Bossart 	 * and allows for multi link synchronization. The SYNCPRD value
359bc872947SPierre-Louis Bossart 	 * is only dependent on the oscillator clock provided to
360bc872947SPierre-Louis Bossart 	 * the IP, so adjust based on _DSD properties reported in DSDT
361bc872947SPierre-Louis Bossart 	 * tables. The values reported are based on either 24MHz
36209ee49e3SPierre-Louis Bossart 	 * (CNL/CML) or 38.4 MHz (ICL/TGL+). On MeteorLake additional
36309ee49e3SPierre-Louis Bossart 	 * frequencies are available with the MLCS clock source selection.
364bc872947SPierre-Louis Bossart 	 */
36509ee49e3SPierre-Louis Bossart 	lcap_mlcs = intel_readl(shim, SDW_SHIM_LCAP) & SDW_SHIM_LCAP_MLCS_MASK;
36609ee49e3SPierre-Louis Bossart 
36709ee49e3SPierre-Louis Bossart 	if (prop->mclk_freq % 6000000) {
36809ee49e3SPierre-Louis Bossart 		if (prop->mclk_freq % 2400000) {
36909ee49e3SPierre-Louis Bossart 			if (lcap_mlcs) {
37009ee49e3SPierre-Louis Bossart 				syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24_576;
37109ee49e3SPierre-Louis Bossart 				clock_source = SDW_SHIM_MLCS_CARDINAL_CLK;
37209ee49e3SPierre-Louis Bossart 			} else {
37309ee49e3SPierre-Louis Bossart 				dev_err(sdw->cdns.dev, "%s: invalid clock configuration, mclk %d lcap_mlcs %d\n",
37409ee49e3SPierre-Louis Bossart 					__func__, prop->mclk_freq, lcap_mlcs);
37509ee49e3SPierre-Louis Bossart 				ret = -EINVAL;
37609ee49e3SPierre-Louis Bossart 				goto out;
37709ee49e3SPierre-Louis Bossart 			}
37809ee49e3SPierre-Louis Bossart 		} else {
379bc872947SPierre-Louis Bossart 			syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
38009ee49e3SPierre-Louis Bossart 			clock_source = SDW_SHIM_MLCS_XTAL_CLK;
38109ee49e3SPierre-Louis Bossart 		}
38209ee49e3SPierre-Louis Bossart 	} else {
38309ee49e3SPierre-Louis Bossart 		if (lcap_mlcs) {
38409ee49e3SPierre-Louis Bossart 			syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_96;
38509ee49e3SPierre-Louis Bossart 			clock_source = SDW_SHIM_MLCS_AUDIO_PLL_CLK;
38609ee49e3SPierre-Louis Bossart 		} else {
387bc872947SPierre-Louis Bossart 			syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24;
38809ee49e3SPierre-Louis Bossart 			clock_source = SDW_SHIM_MLCS_XTAL_CLK;
38909ee49e3SPierre-Louis Bossart 		}
39009ee49e3SPierre-Louis Bossart 	}
391bc872947SPierre-Louis Bossart 
392bc872947SPierre-Louis Bossart 	if (!*shim_mask) {
393bc872947SPierre-Louis Bossart 		dev_dbg(sdw->cdns.dev, "powering up all links\n");
394bc872947SPierre-Louis Bossart 
395bc872947SPierre-Louis Bossart 		/* we first need to program the SyncPRD/CPU registers */
396bc872947SPierre-Louis Bossart 		dev_dbg(sdw->cdns.dev,
397bc872947SPierre-Louis Bossart 			"first link up, programming SYNCPRD\n");
398bc872947SPierre-Louis Bossart 
399bc872947SPierre-Louis Bossart 		/* set SyncPRD period */
400bc872947SPierre-Louis Bossart 		sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
401bc872947SPierre-Louis Bossart 		u32p_replace_bits(&sync_reg, syncprd, SDW_SHIM_SYNC_SYNCPRD);
402bc872947SPierre-Louis Bossart 
403bc872947SPierre-Louis Bossart 		/* Set SyncCPU bit */
404bc872947SPierre-Louis Bossart 		sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
405bc872947SPierre-Louis Bossart 		intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
406bc872947SPierre-Louis Bossart 
407bc872947SPierre-Louis Bossart 		/* Link power up sequence */
408bc872947SPierre-Louis Bossart 		link_control = intel_readl(shim, SDW_SHIM_LCTL);
409bc872947SPierre-Louis Bossart 
410bc872947SPierre-Louis Bossart 		/* only power-up enabled links */
411bc872947SPierre-Louis Bossart 		spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, sdw->link_res->link_mask);
412bc872947SPierre-Louis Bossart 		cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
413bc872947SPierre-Louis Bossart 
414bc872947SPierre-Louis Bossart 		link_control |=  spa_mask;
415bc872947SPierre-Louis Bossart 
416bc872947SPierre-Louis Bossart 		ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
417bc872947SPierre-Louis Bossart 		if (ret < 0) {
418bc872947SPierre-Louis Bossart 			dev_err(sdw->cdns.dev, "Failed to power up link: %d\n", ret);
419bc872947SPierre-Louis Bossart 			goto out;
420bc872947SPierre-Louis Bossart 		}
421bc872947SPierre-Louis Bossart 
422bc872947SPierre-Louis Bossart 		/* SyncCPU will change once link is active */
423bc872947SPierre-Louis Bossart 		ret = intel_wait_bit(shim, SDW_SHIM_SYNC,
424bc872947SPierre-Louis Bossart 				     SDW_SHIM_SYNC_SYNCCPU, 0);
425bc872947SPierre-Louis Bossart 		if (ret < 0) {
426bc872947SPierre-Louis Bossart 			dev_err(sdw->cdns.dev,
427bc872947SPierre-Louis Bossart 				"Failed to set SHIM_SYNC: %d\n", ret);
428bc872947SPierre-Louis Bossart 			goto out;
429bc872947SPierre-Louis Bossart 		}
43009ee49e3SPierre-Louis Bossart 
43109ee49e3SPierre-Louis Bossart 		/* update link clock if needed */
43209ee49e3SPierre-Louis Bossart 		if (lcap_mlcs) {
43309ee49e3SPierre-Louis Bossart 			link_control = intel_readl(shim, SDW_SHIM_LCTL);
43409ee49e3SPierre-Louis Bossart 			u32p_replace_bits(&link_control, clock_source, SDW_SHIM_LCTL_MLCS_MASK);
43509ee49e3SPierre-Louis Bossart 			intel_writel(shim, SDW_SHIM_LCTL, link_control);
43609ee49e3SPierre-Louis Bossart 		}
437bc872947SPierre-Louis Bossart 	}
438bc872947SPierre-Louis Bossart 
439bc872947SPierre-Louis Bossart 	*shim_mask |= BIT(link_id);
440bc872947SPierre-Louis Bossart 
441bc872947SPierre-Louis Bossart 	sdw->cdns.link_up = true;
442b81bcdb4SPierre-Louis Bossart 
443b81bcdb4SPierre-Louis Bossart 	intel_shim_init(sdw);
444b81bcdb4SPierre-Louis Bossart 
445bc872947SPierre-Louis Bossart out:
446bc872947SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
447bc872947SPierre-Louis Bossart 
448bc872947SPierre-Louis Bossart 	return ret;
449bc872947SPierre-Louis Bossart }
450bc872947SPierre-Louis Bossart 
4519b3b4b3fSPierre-Louis Bossart static int intel_link_power_down(struct sdw_intel *sdw)
4524a17c441SPierre-Louis Bossart {
4535ee74eb2SPierre-Louis Bossart 	u32 link_control, spa_mask, cpa_mask;
4544a17c441SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
4554a17c441SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
4564a17c441SPierre-Louis Bossart 	u32 *shim_mask = sdw->link_res->shim_mask;
4574a17c441SPierre-Louis Bossart 	int ret = 0;
4584a17c441SPierre-Louis Bossart 
4594a17c441SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
4604a17c441SPierre-Louis Bossart 
4614a17c441SPierre-Louis Bossart 	if (!(*shim_mask & BIT(link_id)))
4624a17c441SPierre-Louis Bossart 		dev_err(sdw->cdns.dev,
4634a17c441SPierre-Louis Bossart 			"%s: Unbalanced power-up/down calls\n", __func__);
4644a17c441SPierre-Louis Bossart 
465ea6942daSPierre-Louis Bossart 	sdw->cdns.link_up = false;
466ea6942daSPierre-Louis Bossart 
467ea6942daSPierre-Louis Bossart 	intel_shim_master_ip_to_glue(sdw);
468ea6942daSPierre-Louis Bossart 
4694a17c441SPierre-Louis Bossart 	*shim_mask &= ~BIT(link_id);
4704a17c441SPierre-Louis Bossart 
4715ee74eb2SPierre-Louis Bossart 	if (!*shim_mask) {
4725ee74eb2SPierre-Louis Bossart 
47363198aaaSPierre-Louis Bossart 		dev_dbg(sdw->cdns.dev, "powering down all links\n");
4745ee74eb2SPierre-Louis Bossart 
4755ee74eb2SPierre-Louis Bossart 		/* Link power down sequence */
4765ee74eb2SPierre-Louis Bossart 		link_control = intel_readl(shim, SDW_SHIM_LCTL);
4775ee74eb2SPierre-Louis Bossart 
4785ee74eb2SPierre-Louis Bossart 		/* only power-down enabled links */
4793b4979caSVinod Koul 		spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, ~sdw->link_res->link_mask);
4803b4979caSVinod Koul 		cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
4815ee74eb2SPierre-Louis Bossart 
4825ee74eb2SPierre-Louis Bossart 		link_control &=  spa_mask;
4835ee74eb2SPierre-Louis Bossart 
4845ee74eb2SPierre-Louis Bossart 		ret = intel_clear_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
485ea6942daSPierre-Louis Bossart 		if (ret < 0) {
486ea6942daSPierre-Louis Bossart 			dev_err(sdw->cdns.dev, "%s: could not power down link\n", __func__);
487ea6942daSPierre-Louis Bossart 
488ea6942daSPierre-Louis Bossart 			/*
489ea6942daSPierre-Louis Bossart 			 * we leave the sdw->cdns.link_up flag as false since we've disabled
490ea6942daSPierre-Louis Bossart 			 * the link at this point and cannot handle interrupts any longer.
491ea6942daSPierre-Louis Bossart 			 */
492ea6942daSPierre-Louis Bossart 		}
4935ee74eb2SPierre-Louis Bossart 	}
4945ee74eb2SPierre-Louis Bossart 
4954a17c441SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
4964a17c441SPierre-Louis Bossart 
4974a17c441SPierre-Louis Bossart 	return ret;
4985ee74eb2SPierre-Louis Bossart }
4994a17c441SPierre-Louis Bossart 
50002629e45SPierre-Louis Bossart static void intel_shim_sync_arm(struct sdw_intel *sdw)
50102629e45SPierre-Louis Bossart {
50202629e45SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
50302629e45SPierre-Louis Bossart 	u32 sync_reg;
50402629e45SPierre-Louis Bossart 
50502629e45SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
50602629e45SPierre-Louis Bossart 
50702629e45SPierre-Louis Bossart 	/* update SYNC register */
50802629e45SPierre-Louis Bossart 	sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
50902629e45SPierre-Louis Bossart 	sync_reg |= (SDW_SHIM_SYNC_CMDSYNC << sdw->instance);
51002629e45SPierre-Louis Bossart 	intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
51102629e45SPierre-Louis Bossart 
51202629e45SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
51302629e45SPierre-Louis Bossart }
51402629e45SPierre-Louis Bossart 
515437e3289SPierre-Louis Bossart static int intel_shim_sync_go_unlocked(struct sdw_intel *sdw)
516437e3289SPierre-Louis Bossart {
517437e3289SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
518437e3289SPierre-Louis Bossart 	u32 sync_reg;
519437e3289SPierre-Louis Bossart 
520437e3289SPierre-Louis Bossart 	/* Read SYNC register */
521437e3289SPierre-Louis Bossart 	sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
522437e3289SPierre-Louis Bossart 
523437e3289SPierre-Louis Bossart 	/*
524437e3289SPierre-Louis Bossart 	 * Set SyncGO bit to synchronously trigger a bank switch for
525437e3289SPierre-Louis Bossart 	 * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
526437e3289SPierre-Louis Bossart 	 * the Masters.
527437e3289SPierre-Louis Bossart 	 */
528437e3289SPierre-Louis Bossart 	sync_reg |= SDW_SHIM_SYNC_SYNCGO;
529437e3289SPierre-Louis Bossart 
5309c49a4ddSPierre-Louis Bossart 	intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
531437e3289SPierre-Louis Bossart 
5329c49a4ddSPierre-Louis Bossart 	return 0;
53371bb8a1bSVinod Koul }
53471bb8a1bSVinod Koul 
535857a7c42SPierre-Louis Bossart static int intel_shim_sync_go(struct sdw_intel *sdw)
536857a7c42SPierre-Louis Bossart {
537857a7c42SPierre-Louis Bossart 	int ret;
538857a7c42SPierre-Louis Bossart 
539857a7c42SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
540857a7c42SPierre-Louis Bossart 
541857a7c42SPierre-Louis Bossart 	ret = intel_shim_sync_go_unlocked(sdw);
542857a7c42SPierre-Louis Bossart 
543857a7c42SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
544857a7c42SPierre-Louis Bossart 
545857a7c42SPierre-Louis Bossart 	return ret;
546857a7c42SPierre-Louis Bossart }
547857a7c42SPierre-Louis Bossart 
54837a2d22bSVinod Koul /*
54937a2d22bSVinod Koul  * PDI routines
55037a2d22bSVinod Koul  */
55137a2d22bSVinod Koul static void intel_pdi_init(struct sdw_intel *sdw,
55237a2d22bSVinod Koul 			   struct sdw_cdns_stream_config *config)
55337a2d22bSVinod Koul {
5542523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
55537a2d22bSVinod Koul 	unsigned int link_id = sdw->instance;
55663a6aa96SPierre-Louis Bossart 	int pcm_cap;
55737a2d22bSVinod Koul 
55837a2d22bSVinod Koul 	/* PCM Stream Capability */
55937a2d22bSVinod Koul 	pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
56037a2d22bSVinod Koul 
5613b4979caSVinod Koul 	config->pcm_bd = FIELD_GET(SDW_SHIM_PCMSCAP_BSS, pcm_cap);
5623b4979caSVinod Koul 	config->pcm_in = FIELD_GET(SDW_SHIM_PCMSCAP_ISS, pcm_cap);
5633b4979caSVinod Koul 	config->pcm_out = FIELD_GET(SDW_SHIM_PCMSCAP_OSS, pcm_cap);
56437a2d22bSVinod Koul 
565121f4361SPierre-Louis Bossart 	dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
566121f4361SPierre-Louis Bossart 		config->pcm_bd, config->pcm_in, config->pcm_out);
56737a2d22bSVinod Koul }
56837a2d22bSVinod Koul 
56937a2d22bSVinod Koul static int
57063a6aa96SPierre-Louis Bossart intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num)
57137a2d22bSVinod Koul {
5722523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
57337a2d22bSVinod Koul 	unsigned int link_id = sdw->instance;
57437a2d22bSVinod Koul 	int count;
57537a2d22bSVinod Koul 
57637a2d22bSVinod Koul 	count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
57718046335SPierre-Louis Bossart 
57818046335SPierre-Louis Bossart 	/*
57918046335SPierre-Louis Bossart 	 * WORKAROUND: on all existing Intel controllers, pdi
58018046335SPierre-Louis Bossart 	 * number 2 reports channel count as 1 even though it
58118046335SPierre-Louis Bossart 	 * supports 8 channels. Performing hardcoding for pdi
58218046335SPierre-Louis Bossart 	 * number 2.
58318046335SPierre-Louis Bossart 	 */
58418046335SPierre-Louis Bossart 	if (pdi_num == 2)
58518046335SPierre-Louis Bossart 		count = 7;
58618046335SPierre-Louis Bossart 
58737a2d22bSVinod Koul 	/* zero based values for channel count in register */
58837a2d22bSVinod Koul 	count++;
58937a2d22bSVinod Koul 
59037a2d22bSVinod Koul 	return count;
59137a2d22bSVinod Koul }
59237a2d22bSVinod Koul 
59337a2d22bSVinod Koul static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
59437a2d22bSVinod Koul 				   struct sdw_cdns_pdi *pdi,
59537a2d22bSVinod Koul 				   unsigned int num_pdi,
59663a6aa96SPierre-Louis Bossart 				   unsigned int *num_ch)
59737a2d22bSVinod Koul {
59837a2d22bSVinod Koul 	int i, ch_count = 0;
59937a2d22bSVinod Koul 
60037a2d22bSVinod Koul 	for (i = 0; i < num_pdi; i++) {
60163a6aa96SPierre-Louis Bossart 		pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num);
60237a2d22bSVinod Koul 		ch_count += pdi->ch_count;
60337a2d22bSVinod Koul 		pdi++;
60437a2d22bSVinod Koul 	}
60537a2d22bSVinod Koul 
60637a2d22bSVinod Koul 	*num_ch = ch_count;
60737a2d22bSVinod Koul 	return 0;
60837a2d22bSVinod Koul }
60937a2d22bSVinod Koul 
61037a2d22bSVinod Koul static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
61163a6aa96SPierre-Louis Bossart 				      struct sdw_cdns_streams *stream)
61237a2d22bSVinod Koul {
61337a2d22bSVinod Koul 	intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
61463a6aa96SPierre-Louis Bossart 				&stream->num_ch_bd);
61537a2d22bSVinod Koul 
61637a2d22bSVinod Koul 	intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
61763a6aa96SPierre-Louis Bossart 				&stream->num_ch_in);
61837a2d22bSVinod Koul 
61937a2d22bSVinod Koul 	intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
62063a6aa96SPierre-Louis Bossart 				&stream->num_ch_out);
62137a2d22bSVinod Koul 
62237a2d22bSVinod Koul 	return 0;
62337a2d22bSVinod Koul }
62437a2d22bSVinod Koul 
62537a2d22bSVinod Koul static void
62637a2d22bSVinod Koul intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
62737a2d22bSVinod Koul {
6282523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
62937a2d22bSVinod Koul 	unsigned int link_id = sdw->instance;
63037a2d22bSVinod Koul 	int pdi_conf = 0;
63137a2d22bSVinod Koul 
632c134f914SPierre-Louis Bossart 	/* the Bulk and PCM streams are not contiguous */
633c134f914SPierre-Louis Bossart 	pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
634c134f914SPierre-Louis Bossart 	if (pdi->num >= 2)
635c134f914SPierre-Louis Bossart 		pdi->intel_alh_id += 2;
63637a2d22bSVinod Koul 
63737a2d22bSVinod Koul 	/*
63837a2d22bSVinod Koul 	 * Program stream parameters to stream SHIM register
63937a2d22bSVinod Koul 	 * This is applicable for PCM stream only.
64037a2d22bSVinod Koul 	 */
64137a2d22bSVinod Koul 	if (pdi->type != SDW_STREAM_PCM)
64237a2d22bSVinod Koul 		return;
64337a2d22bSVinod Koul 
64437a2d22bSVinod Koul 	if (pdi->dir == SDW_DATA_DIR_RX)
64537a2d22bSVinod Koul 		pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
64637a2d22bSVinod Koul 	else
64737a2d22bSVinod Koul 		pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
64837a2d22bSVinod Koul 
649f067c925SVinod Koul 	u32p_replace_bits(&pdi_conf, pdi->intel_alh_id, SDW_SHIM_PCMSYCM_STREAM);
650f067c925SVinod Koul 	u32p_replace_bits(&pdi_conf, pdi->l_ch_num, SDW_SHIM_PCMSYCM_LCHN);
651f067c925SVinod Koul 	u32p_replace_bits(&pdi_conf, pdi->h_ch_num, SDW_SHIM_PCMSYCM_HCHN);
65237a2d22bSVinod Koul 
65337a2d22bSVinod Koul 	intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
65437a2d22bSVinod Koul }
65537a2d22bSVinod Koul 
65637a2d22bSVinod Koul static void
65737a2d22bSVinod Koul intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
65837a2d22bSVinod Koul {
6592523486bSPierre-Louis Bossart 	void __iomem *alh = sdw->link_res->alh;
66037a2d22bSVinod Koul 	unsigned int link_id = sdw->instance;
66137a2d22bSVinod Koul 	unsigned int conf;
66237a2d22bSVinod Koul 
663c134f914SPierre-Louis Bossart 	/* the Bulk and PCM streams are not contiguous */
664c134f914SPierre-Louis Bossart 	pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
665c134f914SPierre-Louis Bossart 	if (pdi->num >= 2)
666c134f914SPierre-Louis Bossart 		pdi->intel_alh_id += 2;
66737a2d22bSVinod Koul 
66837a2d22bSVinod Koul 	/* Program Stream config ALH register */
66937a2d22bSVinod Koul 	conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
67037a2d22bSVinod Koul 
671f067c925SVinod Koul 	u32p_replace_bits(&conf, SDW_ALH_STRMZCFG_DMAT_VAL, SDW_ALH_STRMZCFG_DMAT);
672f067c925SVinod Koul 	u32p_replace_bits(&conf, pdi->ch_count - 1, SDW_ALH_STRMZCFG_CHN);
67337a2d22bSVinod Koul 
67437a2d22bSVinod Koul 	intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
67537a2d22bSVinod Koul }
67637a2d22bSVinod Koul 
6774b206d34SRander Wang static int intel_params_stream(struct sdw_intel *sdw,
6781d905d35SPierre-Louis Bossart 			       struct snd_pcm_substream *substream,
679c46302ecSVinod Koul 			       struct snd_soc_dai *dai,
6804b206d34SRander Wang 			       struct snd_pcm_hw_params *hw_params,
6814b206d34SRander Wang 			       int link_id, int alh_stream_id)
682c46302ecSVinod Koul {
6832523486bSPierre-Louis Bossart 	struct sdw_intel_link_res *res = sdw->link_res;
6844b206d34SRander Wang 	struct sdw_intel_stream_params_data params_data;
68505c8afe4SPierre-Louis Bossart 
6861d905d35SPierre-Louis Bossart 	params_data.substream = substream;
6874b206d34SRander Wang 	params_data.dai = dai;
6884b206d34SRander Wang 	params_data.hw_params = hw_params;
6894b206d34SRander Wang 	params_data.link_id = link_id;
6904b206d34SRander Wang 	params_data.alh_stream_id = alh_stream_id;
691c46302ecSVinod Koul 
6924b206d34SRander Wang 	if (res->ops && res->ops->params_stream && res->dev)
6934b206d34SRander Wang 		return res->ops->params_stream(res->dev,
6944b206d34SRander Wang 					       &params_data);
695c46302ecSVinod Koul 	return -EIO;
696c46302ecSVinod Koul }
697c46302ecSVinod Koul 
698c46302ecSVinod Koul /*
699c46302ecSVinod Koul  * DAI routines
700c46302ecSVinod Koul  */
701c46302ecSVinod Koul 
7022a9c6ff5SRanjani Sridharan static int intel_free_stream(struct sdw_intel *sdw,
7032a9c6ff5SRanjani Sridharan 			     struct snd_pcm_substream *substream,
7042a9c6ff5SRanjani Sridharan 			     struct snd_soc_dai *dai,
7052a9c6ff5SRanjani Sridharan 			     int link_id)
7062a9c6ff5SRanjani Sridharan {
7072a9c6ff5SRanjani Sridharan 	struct sdw_intel_link_res *res = sdw->link_res;
7082a9c6ff5SRanjani Sridharan 	struct sdw_intel_stream_free_data free_data;
7092a9c6ff5SRanjani Sridharan 
7102a9c6ff5SRanjani Sridharan 	free_data.substream = substream;
7112a9c6ff5SRanjani Sridharan 	free_data.dai = dai;
7122a9c6ff5SRanjani Sridharan 	free_data.link_id = link_id;
7132a9c6ff5SRanjani Sridharan 
7142a9c6ff5SRanjani Sridharan 	if (res->ops && res->ops->free_stream && res->dev)
7152a9c6ff5SRanjani Sridharan 		return res->ops->free_stream(res->dev, &free_data);
7162a9c6ff5SRanjani Sridharan 
7172a9c6ff5SRanjani Sridharan 	return 0;
7182a9c6ff5SRanjani Sridharan }
7192a9c6ff5SRanjani Sridharan 
720c46302ecSVinod Koul static int intel_hw_params(struct snd_pcm_substream *substream,
721c46302ecSVinod Koul 			   struct snd_pcm_hw_params *params,
722c46302ecSVinod Koul 			   struct snd_soc_dai *dai)
723c46302ecSVinod Koul {
724c46302ecSVinod Koul 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
725c46302ecSVinod Koul 	struct sdw_intel *sdw = cdns_to_intel(cdns);
726e0767e39SPierre-Louis Bossart 	struct sdw_cdns_dai_runtime *dai_runtime;
72757a34790SPierre-Louis Bossart 	struct sdw_cdns_pdi *pdi;
728c46302ecSVinod Koul 	struct sdw_stream_config sconfig;
72957a34790SPierre-Louis Bossart 	int ch, dir;
73057a34790SPierre-Louis Bossart 	int ret;
731c46302ecSVinod Koul 
7327dddead7SPierre-Louis Bossart 	dai_runtime = cdns->dai_runtime_array[dai->id];
733e0767e39SPierre-Louis Bossart 	if (!dai_runtime)
734c46302ecSVinod Koul 		return -EIO;
735c46302ecSVinod Koul 
736c46302ecSVinod Koul 	ch = params_channels(params);
737c46302ecSVinod Koul 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
738c46302ecSVinod Koul 		dir = SDW_DATA_DIR_RX;
739c46302ecSVinod Koul 	else
740c46302ecSVinod Koul 		dir = SDW_DATA_DIR_TX;
741c46302ecSVinod Koul 
7421b53385eSBard Liao 	pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
743c46302ecSVinod Koul 
744ba874a8cSKrzysztof Kozlowski 	if (!pdi)
745ba874a8cSKrzysztof Kozlowski 		return -EINVAL;
74657a34790SPierre-Louis Bossart 
74757a34790SPierre-Louis Bossart 	/* do run-time configurations for SHIM, ALH and PDI/PORT */
74857a34790SPierre-Louis Bossart 	intel_pdi_shim_configure(sdw, pdi);
74957a34790SPierre-Louis Bossart 	intel_pdi_alh_configure(sdw, pdi);
75057a34790SPierre-Louis Bossart 	sdw_cdns_config_stream(cdns, ch, dir, pdi);
75157a34790SPierre-Louis Bossart 
752a5a0239cSBard Liao 	/* store pdi and hw_params, may be needed in prepare step */
753e0767e39SPierre-Louis Bossart 	dai_runtime->paused = false;
754e0767e39SPierre-Louis Bossart 	dai_runtime->suspended = false;
755e0767e39SPierre-Louis Bossart 	dai_runtime->pdi = pdi;
756c46302ecSVinod Koul 
757c46302ecSVinod Koul 	/* Inform DSP about PDI stream number */
7581d905d35SPierre-Louis Bossart 	ret = intel_params_stream(sdw, substream, dai, params,
7594b206d34SRander Wang 				  sdw->instance,
76057a34790SPierre-Louis Bossart 				  pdi->intel_alh_id);
761c46302ecSVinod Koul 	if (ret)
762ba874a8cSKrzysztof Kozlowski 		return ret;
763c46302ecSVinod Koul 
764c46302ecSVinod Koul 	sconfig.direction = dir;
765c46302ecSVinod Koul 	sconfig.ch_count = ch;
766c46302ecSVinod Koul 	sconfig.frame_rate = params_rate(params);
767e0767e39SPierre-Louis Bossart 	sconfig.type = dai_runtime->stream_type;
768c46302ecSVinod Koul 
769c46302ecSVinod Koul 	sconfig.bps = snd_pcm_format_width(params_format(params));
770c46302ecSVinod Koul 
771c46302ecSVinod Koul 	/* Port configuration */
772*e4fcf153SKrzysztof Kozlowski 	struct sdw_port_config *pconfig __free(kfree) = kzalloc(sizeof(*pconfig),
773*e4fcf153SKrzysztof Kozlowski 								GFP_KERNEL);
774ba874a8cSKrzysztof Kozlowski 	if (!pconfig)
775ba874a8cSKrzysztof Kozlowski 		return -ENOMEM;
776c46302ecSVinod Koul 
77757a34790SPierre-Louis Bossart 	pconfig->num = pdi->num;
77857a34790SPierre-Louis Bossart 	pconfig->ch_mask = (1 << ch) - 1;
779c46302ecSVinod Koul 
780c46302ecSVinod Koul 	ret = sdw_stream_add_master(&cdns->bus, &sconfig,
781e0767e39SPierre-Louis Bossart 				    pconfig, 1, dai_runtime->stream);
78257a34790SPierre-Louis Bossart 	if (ret)
78317ed5befSPierre-Louis Bossart 		dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
784c46302ecSVinod Koul 
785c46302ecSVinod Koul 	return ret;
786c46302ecSVinod Koul }
787c46302ecSVinod Koul 
78827b198f4SRander Wang static int intel_prepare(struct snd_pcm_substream *substream,
78927b198f4SRander Wang 			 struct snd_soc_dai *dai)
79027b198f4SRander Wang {
791a5a0239cSBard Liao 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
792a5a0239cSBard Liao 	struct sdw_intel *sdw = cdns_to_intel(cdns);
793e0767e39SPierre-Louis Bossart 	struct sdw_cdns_dai_runtime *dai_runtime;
794a5a0239cSBard Liao 	int ch, dir;
795244eb888SPierre-Louis Bossart 	int ret = 0;
79627b198f4SRander Wang 
7977dddead7SPierre-Louis Bossart 	dai_runtime = cdns->dai_runtime_array[dai->id];
798e0767e39SPierre-Louis Bossart 	if (!dai_runtime) {
799e0767e39SPierre-Louis Bossart 		dev_err(dai->dev, "failed to get dai runtime in %s\n",
80027b198f4SRander Wang 			__func__);
80127b198f4SRander Wang 		return -EIO;
80227b198f4SRander Wang 	}
80327b198f4SRander Wang 
804e0767e39SPierre-Louis Bossart 	if (dai_runtime->suspended) {
80550cd92e0SKuninori Morimoto 		struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
8060a0d1740SPierre-Louis Bossart 		struct snd_pcm_hw_params *hw_params;
8070a0d1740SPierre-Louis Bossart 
8080a0d1740SPierre-Louis Bossart 		hw_params = &rtd->dpcm[substream->stream].hw_params;
8090a0d1740SPierre-Louis Bossart 
810e0767e39SPierre-Louis Bossart 		dai_runtime->suspended = false;
811a5a0239cSBard Liao 
812a5a0239cSBard Liao 		/*
813a5a0239cSBard Liao 		 * .prepare() is called after system resume, where we
814a5a0239cSBard Liao 		 * need to reinitialize the SHIM/ALH/Cadence IP.
815a5a0239cSBard Liao 		 * .prepare() is also called to deal with underflows,
816a5a0239cSBard Liao 		 * but in those cases we cannot touch ALH/SHIM
817a5a0239cSBard Liao 		 * registers
818a5a0239cSBard Liao 		 */
819a5a0239cSBard Liao 
820a5a0239cSBard Liao 		/* configure stream */
8210a0d1740SPierre-Louis Bossart 		ch = params_channels(hw_params);
822a5a0239cSBard Liao 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
823a5a0239cSBard Liao 			dir = SDW_DATA_DIR_RX;
824a5a0239cSBard Liao 		else
825a5a0239cSBard Liao 			dir = SDW_DATA_DIR_TX;
826a5a0239cSBard Liao 
827e0767e39SPierre-Louis Bossart 		intel_pdi_shim_configure(sdw, dai_runtime->pdi);
828e0767e39SPierre-Louis Bossart 		intel_pdi_alh_configure(sdw, dai_runtime->pdi);
829e0767e39SPierre-Louis Bossart 		sdw_cdns_config_stream(cdns, ch, dir, dai_runtime->pdi);
830a5a0239cSBard Liao 
831a5a0239cSBard Liao 		/* Inform DSP about PDI stream number */
8321d905d35SPierre-Louis Bossart 		ret = intel_params_stream(sdw, substream, dai,
8330a0d1740SPierre-Louis Bossart 					  hw_params,
834a5a0239cSBard Liao 					  sdw->instance,
835e0767e39SPierre-Louis Bossart 					  dai_runtime->pdi->intel_alh_id);
836a5a0239cSBard Liao 	}
837a5a0239cSBard Liao 
838a5a0239cSBard Liao 	return ret;
83927b198f4SRander Wang }
84027b198f4SRander Wang 
841c46302ecSVinod Koul static int
842c46302ecSVinod Koul intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
843c46302ecSVinod Koul {
844c46302ecSVinod Koul 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
8452a9c6ff5SRanjani Sridharan 	struct sdw_intel *sdw = cdns_to_intel(cdns);
846e0767e39SPierre-Louis Bossart 	struct sdw_cdns_dai_runtime *dai_runtime;
847c46302ecSVinod Koul 	int ret;
848c46302ecSVinod Koul 
8497dddead7SPierre-Louis Bossart 	dai_runtime = cdns->dai_runtime_array[dai->id];
850e0767e39SPierre-Louis Bossart 	if (!dai_runtime)
851c46302ecSVinod Koul 		return -EIO;
852c46302ecSVinod Koul 
853244eb888SPierre-Louis Bossart 	/*
854244eb888SPierre-Louis Bossart 	 * The sdw stream state will transition to RELEASED when stream->
855244eb888SPierre-Louis Bossart 	 * master_list is empty. So the stream state will transition to
856244eb888SPierre-Louis Bossart 	 * DEPREPARED for the first cpu-dai and to RELEASED for the last
857244eb888SPierre-Louis Bossart 	 * cpu-dai.
858244eb888SPierre-Louis Bossart 	 */
859e0767e39SPierre-Louis Bossart 	ret = sdw_stream_remove_master(&cdns->bus, dai_runtime->stream);
860eff346f2SPierre-Louis Bossart 	if (ret < 0) {
86117ed5befSPierre-Louis Bossart 		dev_err(dai->dev, "remove master from stream %s failed: %d\n",
862e0767e39SPierre-Louis Bossart 			dai_runtime->stream->name, ret);
863c46302ecSVinod Koul 		return ret;
864c46302ecSVinod Koul 	}
865c46302ecSVinod Koul 
8662a9c6ff5SRanjani Sridharan 	ret = intel_free_stream(sdw, substream, dai, sdw->instance);
8672a9c6ff5SRanjani Sridharan 	if (ret < 0) {
8682a9c6ff5SRanjani Sridharan 		dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
8692a9c6ff5SRanjani Sridharan 		return ret;
8702a9c6ff5SRanjani Sridharan 	}
8712a9c6ff5SRanjani Sridharan 
872e0767e39SPierre-Louis Bossart 	dai_runtime->pdi = NULL;
873a5a0239cSBard Liao 
874eff346f2SPierre-Louis Bossart 	return 0;
875eff346f2SPierre-Louis Bossart }
876eff346f2SPierre-Louis Bossart 
877c46302ecSVinod Koul static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
878c46302ecSVinod Koul 				    void *stream, int direction)
879c46302ecSVinod Koul {
88063a6aa96SPierre-Louis Bossart 	return cdns_set_sdw_stream(dai, stream, direction);
881c46302ecSVinod Koul }
882c46302ecSVinod Koul 
88309553140SPierre-Louis Bossart static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
88409553140SPierre-Louis Bossart 				  int direction)
88509553140SPierre-Louis Bossart {
8867dddead7SPierre-Louis Bossart 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
887e0767e39SPierre-Louis Bossart 	struct sdw_cdns_dai_runtime *dai_runtime;
88809553140SPierre-Louis Bossart 
8897dddead7SPierre-Louis Bossart 	dai_runtime = cdns->dai_runtime_array[dai->id];
890e0767e39SPierre-Louis Bossart 	if (!dai_runtime)
89106dcb4e4SPierre-Louis Bossart 		return ERR_PTR(-EINVAL);
89209553140SPierre-Louis Bossart 
893e0767e39SPierre-Louis Bossart 	return dai_runtime->stream;
89409553140SPierre-Louis Bossart }
89509553140SPierre-Louis Bossart 
8968ddeafb9SRanjani Sridharan static int intel_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
8978ddeafb9SRanjani Sridharan {
8988ddeafb9SRanjani Sridharan 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
899e0767e39SPierre-Louis Bossart 	struct sdw_cdns_dai_runtime *dai_runtime;
9008ddeafb9SRanjani Sridharan 	int ret = 0;
9018ddeafb9SRanjani Sridharan 
9027dddead7SPierre-Louis Bossart 	dai_runtime = cdns->dai_runtime_array[dai->id];
903e0767e39SPierre-Louis Bossart 	if (!dai_runtime) {
904e0767e39SPierre-Louis Bossart 		dev_err(dai->dev, "failed to get dai runtime in %s\n",
9058ddeafb9SRanjani Sridharan 			__func__);
9068ddeafb9SRanjani Sridharan 		return -EIO;
9078ddeafb9SRanjani Sridharan 	}
9088ddeafb9SRanjani Sridharan 
9098ddeafb9SRanjani Sridharan 	switch (cmd) {
9108ddeafb9SRanjani Sridharan 	case SNDRV_PCM_TRIGGER_SUSPEND:
9118ddeafb9SRanjani Sridharan 
9128ddeafb9SRanjani Sridharan 		/*
9138ddeafb9SRanjani Sridharan 		 * The .prepare callback is used to deal with xruns and resume operations.
9148ddeafb9SRanjani Sridharan 		 * In the case of xruns, the DMAs and SHIM registers cannot be touched,
9158ddeafb9SRanjani Sridharan 		 * but for resume operations the DMAs and SHIM registers need to be initialized.
9168ddeafb9SRanjani Sridharan 		 * the .trigger callback is used to track the suspend case only.
9178ddeafb9SRanjani Sridharan 		 */
9188ddeafb9SRanjani Sridharan 
919e0767e39SPierre-Louis Bossart 		dai_runtime->suspended = true;
9208ddeafb9SRanjani Sridharan 
9218ddeafb9SRanjani Sridharan 		break;
9228ddeafb9SRanjani Sridharan 
9238ddeafb9SRanjani Sridharan 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
924e0767e39SPierre-Louis Bossart 		dai_runtime->paused = true;
9258ddeafb9SRanjani Sridharan 		break;
9268ddeafb9SRanjani Sridharan 	case SNDRV_PCM_TRIGGER_STOP:
9278ddeafb9SRanjani Sridharan 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
928e0767e39SPierre-Louis Bossart 		dai_runtime->paused = false;
9298ddeafb9SRanjani Sridharan 		break;
9308ddeafb9SRanjani Sridharan 	default:
9318ddeafb9SRanjani Sridharan 		break;
9328ddeafb9SRanjani Sridharan 	}
9338ddeafb9SRanjani Sridharan 
9348ddeafb9SRanjani Sridharan 	return ret;
9358ddeafb9SRanjani Sridharan }
9368ddeafb9SRanjani Sridharan 
9373e9c9f90SPierre-Louis Bossart static int intel_component_probe(struct snd_soc_component *component)
9383e9c9f90SPierre-Louis Bossart {
9393e9c9f90SPierre-Louis Bossart 	int ret;
9403e9c9f90SPierre-Louis Bossart 
9413e9c9f90SPierre-Louis Bossart 	/*
9423e9c9f90SPierre-Louis Bossart 	 * make sure the device is pm_runtime_active before initiating
9433e9c9f90SPierre-Louis Bossart 	 * bus transactions during the card registration.
9443e9c9f90SPierre-Louis Bossart 	 * We use pm_runtime_resume() here, without taking a reference
9453e9c9f90SPierre-Louis Bossart 	 * and releasing it immediately.
9463e9c9f90SPierre-Louis Bossart 	 */
9473e9c9f90SPierre-Louis Bossart 	ret = pm_runtime_resume(component->dev);
9483e9c9f90SPierre-Louis Bossart 	if (ret < 0 && ret != -EACCES)
9493e9c9f90SPierre-Louis Bossart 		return ret;
9503e9c9f90SPierre-Louis Bossart 
9513e9c9f90SPierre-Louis Bossart 	return 0;
9523e9c9f90SPierre-Louis Bossart }
9533e9c9f90SPierre-Louis Bossart 
9548ddeafb9SRanjani Sridharan static int intel_component_dais_suspend(struct snd_soc_component *component)
9558ddeafb9SRanjani Sridharan {
9568ddeafb9SRanjani Sridharan 	struct snd_soc_dai *dai;
9578ddeafb9SRanjani Sridharan 
9588ddeafb9SRanjani Sridharan 	/*
9598ddeafb9SRanjani Sridharan 	 * In the corner case where a SUSPEND happens during a PAUSE, the ALSA core
9608ddeafb9SRanjani Sridharan 	 * does not throw the TRIGGER_SUSPEND. This leaves the DAIs in an unbalanced state.
9618ddeafb9SRanjani Sridharan 	 * Since the component suspend is called last, we can trap this corner case
9628ddeafb9SRanjani Sridharan 	 * and force the DAIs to release their resources.
9638ddeafb9SRanjani Sridharan 	 */
9648ddeafb9SRanjani Sridharan 	for_each_component_dais(component, dai) {
9658ddeafb9SRanjani Sridharan 		struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
966e0767e39SPierre-Louis Bossart 		struct sdw_cdns_dai_runtime *dai_runtime;
9678ddeafb9SRanjani Sridharan 
9687dddead7SPierre-Louis Bossart 		dai_runtime = cdns->dai_runtime_array[dai->id];
9698ddeafb9SRanjani Sridharan 
970e0767e39SPierre-Louis Bossart 		if (!dai_runtime)
9718ddeafb9SRanjani Sridharan 			continue;
9728ddeafb9SRanjani Sridharan 
973e0767e39SPierre-Louis Bossart 		if (dai_runtime->suspended)
9748ddeafb9SRanjani Sridharan 			continue;
9758ddeafb9SRanjani Sridharan 
97601271045SPierre-Louis Bossart 		if (dai_runtime->paused)
977e0767e39SPierre-Louis Bossart 			dai_runtime->suspended = true;
9788ddeafb9SRanjani Sridharan 	}
9798ddeafb9SRanjani Sridharan 
9808ddeafb9SRanjani Sridharan 	return 0;
9818ddeafb9SRanjani Sridharan }
9828ddeafb9SRanjani Sridharan 
983b1635596SJulia Lawall static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
984c46302ecSVinod Koul 	.hw_params = intel_hw_params,
98527b198f4SRander Wang 	.prepare = intel_prepare,
986c46302ecSVinod Koul 	.hw_free = intel_hw_free,
9878ddeafb9SRanjani Sridharan 	.trigger = intel_trigger,
988e8444560SPierre-Louis Bossart 	.set_stream = intel_pcm_set_sdw_stream,
989e8444560SPierre-Louis Bossart 	.get_stream = intel_get_sdw_stream,
990c46302ecSVinod Koul };
991c46302ecSVinod Koul 
992c46302ecSVinod Koul static const struct snd_soc_component_driver dai_component = {
993c46302ecSVinod Koul 	.name			= "soundwire",
9943e9c9f90SPierre-Louis Bossart 	.probe			= intel_component_probe,
995ca682020SCharles Keepax 	.suspend		= intel_component_dais_suspend,
996ca682020SCharles Keepax 	.legacy_dai_naming	= 1,
997c46302ecSVinod Koul };
998c46302ecSVinod Koul 
999c46302ecSVinod Koul static int intel_create_dai(struct sdw_cdns *cdns,
1000c46302ecSVinod Koul 			    struct snd_soc_dai_driver *dais,
1001c46302ecSVinod Koul 			    enum intel_pdi_type type,
100263a6aa96SPierre-Louis Bossart 			    u32 num, u32 off, u32 max_ch)
1003c46302ecSVinod Koul {
1004c46302ecSVinod Koul 	int i;
1005c46302ecSVinod Koul 
1006c46302ecSVinod Koul 	if (num == 0)
1007c46302ecSVinod Koul 		return 0;
1008c46302ecSVinod Koul 
1009c46302ecSVinod Koul 	for (i = off; i < (off + num); i++) {
1010bf6d6e68SPierre-Louis Bossart 		dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
1011bf6d6e68SPierre-Louis Bossart 					      "SDW%d Pin%d",
1012c46302ecSVinod Koul 					      cdns->instance, i);
1013c46302ecSVinod Koul 		if (!dais[i].name)
1014c46302ecSVinod Koul 			return -ENOMEM;
1015c46302ecSVinod Koul 
1016c46302ecSVinod Koul 		if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
1017c46302ecSVinod Koul 			dais[i].playback.channels_min = 1;
1018c46302ecSVinod Koul 			dais[i].playback.channels_max = max_ch;
1019c46302ecSVinod Koul 		}
1020c46302ecSVinod Koul 
1021c46302ecSVinod Koul 		if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
102239194128SSrinivas Kandagatla 			dais[i].capture.channels_min = 1;
102339194128SSrinivas Kandagatla 			dais[i].capture.channels_max = max_ch;
1024c46302ecSVinod Koul 		}
1025c46302ecSVinod Koul 
1026c46302ecSVinod Koul 		dais[i].ops = &intel_pcm_dai_ops;
1027c46302ecSVinod Koul 	}
1028c46302ecSVinod Koul 
1029c46302ecSVinod Koul 	return 0;
1030c46302ecSVinod Koul }
1031c46302ecSVinod Koul 
1032c46302ecSVinod Koul static int intel_register_dai(struct sdw_intel *sdw)
1033c46302ecSVinod Koul {
10347dddead7SPierre-Louis Bossart 	struct sdw_cdns_dai_runtime **dai_runtime_array;
103530cbae66SPierre-Louis Bossart 	struct sdw_cdns_stream_config config;
1036c46302ecSVinod Koul 	struct sdw_cdns *cdns = &sdw->cdns;
1037c46302ecSVinod Koul 	struct sdw_cdns_streams *stream;
1038c46302ecSVinod Koul 	struct snd_soc_dai_driver *dais;
1039c46302ecSVinod Koul 	int num_dai, ret, off = 0;
1040c46302ecSVinod Koul 
104130cbae66SPierre-Louis Bossart 	/* Read the PDI config and initialize cadence PDI */
104230cbae66SPierre-Louis Bossart 	intel_pdi_init(sdw, &config);
104330cbae66SPierre-Louis Bossart 	ret = sdw_cdns_pdi_init(cdns, config);
104430cbae66SPierre-Louis Bossart 	if (ret)
104530cbae66SPierre-Louis Bossart 		return ret;
104630cbae66SPierre-Louis Bossart 
104759e924feSPierre-Louis Bossart 	intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm);
104830cbae66SPierre-Louis Bossart 
1049c46302ecSVinod Koul 	/* DAIs are created based on total number of PDIs supported */
105063a6aa96SPierre-Louis Bossart 	num_dai = cdns->pcm.num_pdi;
1051c46302ecSVinod Koul 
10527dddead7SPierre-Louis Bossart 	dai_runtime_array = devm_kcalloc(cdns->dev, num_dai,
10537dddead7SPierre-Louis Bossart 					 sizeof(struct sdw_cdns_dai_runtime *),
10547dddead7SPierre-Louis Bossart 					 GFP_KERNEL);
10557dddead7SPierre-Louis Bossart 	if (!dai_runtime_array)
10567dddead7SPierre-Louis Bossart 		return -ENOMEM;
10577dddead7SPierre-Louis Bossart 	cdns->dai_runtime_array = dai_runtime_array;
10587dddead7SPierre-Louis Bossart 
1059c46302ecSVinod Koul 	dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
1060c46302ecSVinod Koul 	if (!dais)
1061c46302ecSVinod Koul 		return -ENOMEM;
1062c46302ecSVinod Koul 
1063c46302ecSVinod Koul 	/* Create PCM DAIs */
1064c46302ecSVinod Koul 	stream = &cdns->pcm;
1065c46302ecSVinod Koul 
1066cf924962SBard Liao 	ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
106763a6aa96SPierre-Louis Bossart 			       off, stream->num_ch_in);
1068c46302ecSVinod Koul 	if (ret)
1069c46302ecSVinod Koul 		return ret;
1070c46302ecSVinod Koul 
1071c46302ecSVinod Koul 	off += cdns->pcm.num_in;
10721215daeeSVinod Koul 	ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
107363a6aa96SPierre-Louis Bossart 			       off, stream->num_ch_out);
1074c46302ecSVinod Koul 	if (ret)
1075c46302ecSVinod Koul 		return ret;
1076c46302ecSVinod Koul 
1077c46302ecSVinod Koul 	off += cdns->pcm.num_out;
10781215daeeSVinod Koul 	ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
107963a6aa96SPierre-Louis Bossart 			       off, stream->num_ch_bd);
1080c46302ecSVinod Koul 	if (ret)
1081c46302ecSVinod Koul 		return ret;
1082c46302ecSVinod Koul 
108354f391ddSPierre-Louis Bossart 	return devm_snd_soc_register_component(cdns->dev, &dai_component,
1084c46302ecSVinod Koul 					       dais, num_dai);
1085c46302ecSVinod Koul }
1086c46302ecSVinod Koul 
1087503ae285SPierre-Louis Bossart 
1088b3ad31f3SPierre-Louis Bossart const struct sdw_intel_hw_ops sdw_intel_cnl_hw_ops = {
1089fb2dc6a0SPierre-Louis Bossart 	.debugfs_init = intel_debugfs_init,
1090fb2dc6a0SPierre-Louis Bossart 	.debugfs_exit = intel_debugfs_exit,
1091fb2dc6a0SPierre-Louis Bossart 
1092b6234bccSPierre-Louis Bossart 	.register_dai = intel_register_dai,
1093b6234bccSPierre-Louis Bossart 
10943db0c5a6SPierre-Louis Bossart 	.check_clock_stop = intel_check_clock_stop,
10953db0c5a6SPierre-Louis Bossart 	.start_bus = intel_start_bus,
10963db0c5a6SPierre-Louis Bossart 	.start_bus_after_reset = intel_start_bus_after_reset,
10973db0c5a6SPierre-Louis Bossart 	.start_bus_after_clock_stop = intel_start_bus_after_clock_stop,
10983db0c5a6SPierre-Louis Bossart 	.stop_bus = intel_stop_bus,
10993db0c5a6SPierre-Louis Bossart 
110049c9ff45SPierre-Louis Bossart 	.link_power_up = intel_link_power_up,
110149c9ff45SPierre-Louis Bossart 	.link_power_down = intel_link_power_down,
110249c9ff45SPierre-Louis Bossart 
110336e3b385SPierre-Louis Bossart 	.shim_check_wake = intel_shim_check_wake,
110436e3b385SPierre-Louis Bossart 	.shim_wake = intel_shim_wake,
110536e3b385SPierre-Louis Bossart 
1106b3ad31f3SPierre-Louis Bossart 	.pre_bank_switch = intel_pre_bank_switch,
1107b3ad31f3SPierre-Louis Bossart 	.post_bank_switch = intel_post_bank_switch,
110884706e9aSPierre-Louis Bossart 
110984706e9aSPierre-Louis Bossart 	.sync_arm = intel_shim_sync_arm,
111084706e9aSPierre-Louis Bossart 	.sync_go_unlocked = intel_shim_sync_go_unlocked,
111184706e9aSPierre-Louis Bossart 	.sync_go = intel_shim_sync_go,
11121e76de2eSPierre-Louis Bossart 	.sync_check_cmdsync_unlocked = intel_check_cmdsync_unlocked,
1113b3ad31f3SPierre-Louis Bossart };
1114b3ad31f3SPierre-Louis Bossart EXPORT_SYMBOL_NS(sdw_intel_cnl_hw_ops, SOUNDWIRE_INTEL);
1115