xref: /linux/drivers/soundwire/intel.c (revision ba874a8c2f895d898bbaf67f9e952425aff1557d)
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>
11df72b719SPierre-Louis Bossart #include <linux/io.h>
1237a2d22bSVinod Koul #include <sound/pcm_params.h>
13ab2c9132SRander Wang #include <linux/pm_runtime.h>
1437a2d22bSVinod Koul #include <sound/soc.h>
1571bb8a1bSVinod Koul #include <linux/soundwire/sdw_registers.h>
1671bb8a1bSVinod Koul #include <linux/soundwire/sdw.h>
1771bb8a1bSVinod Koul #include <linux/soundwire/sdw_intel.h>
1871bb8a1bSVinod Koul #include "cadence_master.h"
1979ee6631SPierre-Louis Bossart #include "bus.h"
2071bb8a1bSVinod Koul #include "intel.h"
2171bb8a1bSVinod Koul 
227d2845d5SPierre-Louis Bossart static int intel_wait_bit(void __iomem *base, int offset, u32 mask, u32 target)
2371bb8a1bSVinod Koul {
2471bb8a1bSVinod Koul 	int timeout = 10;
2571bb8a1bSVinod Koul 	u32 reg_read;
2671bb8a1bSVinod Koul 
2771bb8a1bSVinod Koul 	do {
2871bb8a1bSVinod Koul 		reg_read = readl(base + offset);
297d2845d5SPierre-Louis Bossart 		if ((reg_read & mask) == target)
3071bb8a1bSVinod Koul 			return 0;
3171bb8a1bSVinod Koul 
3271bb8a1bSVinod Koul 		timeout--;
337d2845d5SPierre-Louis Bossart 		usleep_range(50, 100);
3471bb8a1bSVinod Koul 	} while (timeout != 0);
3571bb8a1bSVinod Koul 
3671bb8a1bSVinod Koul 	return -EAGAIN;
3771bb8a1bSVinod Koul }
3871bb8a1bSVinod Koul 
397d2845d5SPierre-Louis Bossart static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
407d2845d5SPierre-Louis Bossart {
417d2845d5SPierre-Louis Bossart 	writel(value, base + offset);
427d2845d5SPierre-Louis Bossart 	return intel_wait_bit(base, offset, mask, 0);
437d2845d5SPierre-Louis Bossart }
447d2845d5SPierre-Louis Bossart 
4571bb8a1bSVinod Koul static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
4671bb8a1bSVinod Koul {
4771bb8a1bSVinod Koul 	writel(value, base + offset);
487d2845d5SPierre-Louis Bossart 	return intel_wait_bit(base, offset, mask, mask);
4971bb8a1bSVinod Koul }
5071bb8a1bSVinod Koul 
5171bb8a1bSVinod Koul /*
5279ee6631SPierre-Louis Bossart  * debugfs
5379ee6631SPierre-Louis Bossart  */
5479ee6631SPierre-Louis Bossart #ifdef CONFIG_DEBUG_FS
5579ee6631SPierre-Louis Bossart 
5679ee6631SPierre-Louis Bossart #define RD_BUF (2 * PAGE_SIZE)
5779ee6631SPierre-Louis Bossart 
5879ee6631SPierre-Louis Bossart static ssize_t intel_sprintf(void __iomem *mem, bool l,
5979ee6631SPierre-Louis Bossart 			     char *buf, size_t pos, unsigned int reg)
6079ee6631SPierre-Louis Bossart {
6179ee6631SPierre-Louis Bossart 	int value;
6279ee6631SPierre-Louis Bossart 
6379ee6631SPierre-Louis Bossart 	if (l)
6479ee6631SPierre-Louis Bossart 		value = intel_readl(mem, reg);
6579ee6631SPierre-Louis Bossart 	else
6679ee6631SPierre-Louis Bossart 		value = intel_readw(mem, reg);
6779ee6631SPierre-Louis Bossart 
6879ee6631SPierre-Louis Bossart 	return scnprintf(buf + pos, RD_BUF - pos, "%4x\t%4x\n", reg, value);
6979ee6631SPierre-Louis Bossart }
7079ee6631SPierre-Louis Bossart 
7179ee6631SPierre-Louis Bossart static int intel_reg_show(struct seq_file *s_file, void *data)
7279ee6631SPierre-Louis Bossart {
7379ee6631SPierre-Louis Bossart 	struct sdw_intel *sdw = s_file->private;
742523486bSPierre-Louis Bossart 	void __iomem *s = sdw->link_res->shim;
752523486bSPierre-Louis Bossart 	void __iomem *a = sdw->link_res->alh;
7679ee6631SPierre-Louis Bossart 	char *buf;
7779ee6631SPierre-Louis Bossart 	ssize_t ret;
7879ee6631SPierre-Louis Bossart 	int i, j;
7979ee6631SPierre-Louis Bossart 	unsigned int links, reg;
8079ee6631SPierre-Louis Bossart 
8179ee6631SPierre-Louis Bossart 	buf = 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 	kfree(buf);
13379ee6631SPierre-Louis Bossart 
13479ee6631SPierre-Louis Bossart 	return 0;
13579ee6631SPierre-Louis Bossart }
13679ee6631SPierre-Louis Bossart DEFINE_SHOW_ATTRIBUTE(intel_reg);
13779ee6631SPierre-Louis Bossart 
1380f9138e7SPierre-Louis Bossart static int intel_set_m_datamode(void *data, u64 value)
1390f9138e7SPierre-Louis Bossart {
1400f9138e7SPierre-Louis Bossart 	struct sdw_intel *sdw = data;
1410f9138e7SPierre-Louis Bossart 	struct sdw_bus *bus = &sdw->cdns.bus;
1420f9138e7SPierre-Louis Bossart 
1430f9138e7SPierre-Louis Bossart 	if (value > SDW_PORT_DATA_MODE_STATIC_1)
1440f9138e7SPierre-Louis Bossart 		return -EINVAL;
1450f9138e7SPierre-Louis Bossart 
1460f9138e7SPierre-Louis Bossart 	/* Userspace changed the hardware state behind the kernel's back */
1470f9138e7SPierre-Louis Bossart 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
1480f9138e7SPierre-Louis Bossart 
1490f9138e7SPierre-Louis Bossart 	bus->params.m_data_mode = value;
1500f9138e7SPierre-Louis Bossart 
1510f9138e7SPierre-Louis Bossart 	return 0;
1520f9138e7SPierre-Louis Bossart }
1530f9138e7SPierre-Louis Bossart DEFINE_DEBUGFS_ATTRIBUTE(intel_set_m_datamode_fops, NULL,
1540f9138e7SPierre-Louis Bossart 			 intel_set_m_datamode, "%llu\n");
1550f9138e7SPierre-Louis Bossart 
1560f9138e7SPierre-Louis Bossart static int intel_set_s_datamode(void *data, u64 value)
1570f9138e7SPierre-Louis Bossart {
1580f9138e7SPierre-Louis Bossart 	struct sdw_intel *sdw = data;
1590f9138e7SPierre-Louis Bossart 	struct sdw_bus *bus = &sdw->cdns.bus;
1600f9138e7SPierre-Louis Bossart 
1610f9138e7SPierre-Louis Bossart 	if (value > SDW_PORT_DATA_MODE_STATIC_1)
1620f9138e7SPierre-Louis Bossart 		return -EINVAL;
1630f9138e7SPierre-Louis Bossart 
1640f9138e7SPierre-Louis Bossart 	/* Userspace changed the hardware state behind the kernel's back */
1650f9138e7SPierre-Louis Bossart 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
1660f9138e7SPierre-Louis Bossart 
1670f9138e7SPierre-Louis Bossart 	bus->params.s_data_mode = value;
1680f9138e7SPierre-Louis Bossart 
1690f9138e7SPierre-Louis Bossart 	return 0;
1700f9138e7SPierre-Louis Bossart }
1710f9138e7SPierre-Louis Bossart DEFINE_DEBUGFS_ATTRIBUTE(intel_set_s_datamode_fops, NULL,
1720f9138e7SPierre-Louis Bossart 			 intel_set_s_datamode, "%llu\n");
1730f9138e7SPierre-Louis Bossart 
17479ee6631SPierre-Louis Bossart static void intel_debugfs_init(struct sdw_intel *sdw)
17579ee6631SPierre-Louis Bossart {
17679ee6631SPierre-Louis Bossart 	struct dentry *root = sdw->cdns.bus.debugfs;
17779ee6631SPierre-Louis Bossart 
17879ee6631SPierre-Louis Bossart 	if (!root)
17979ee6631SPierre-Louis Bossart 		return;
18079ee6631SPierre-Louis Bossart 
18179ee6631SPierre-Louis Bossart 	sdw->debugfs = debugfs_create_dir("intel-sdw", root);
18279ee6631SPierre-Louis Bossart 
18379ee6631SPierre-Louis Bossart 	debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw,
18479ee6631SPierre-Louis Bossart 			    &intel_reg_fops);
18579ee6631SPierre-Louis Bossart 
1860f9138e7SPierre-Louis Bossart 	debugfs_create_file("intel-m-datamode", 0200, sdw->debugfs, sdw,
1870f9138e7SPierre-Louis Bossart 			    &intel_set_m_datamode_fops);
1880f9138e7SPierre-Louis Bossart 
1890f9138e7SPierre-Louis Bossart 	debugfs_create_file("intel-s-datamode", 0200, sdw->debugfs, sdw,
1900f9138e7SPierre-Louis Bossart 			    &intel_set_s_datamode_fops);
1910f9138e7SPierre-Louis Bossart 
19279ee6631SPierre-Louis Bossart 	sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
19379ee6631SPierre-Louis Bossart }
19479ee6631SPierre-Louis Bossart 
19579ee6631SPierre-Louis Bossart static void intel_debugfs_exit(struct sdw_intel *sdw)
19679ee6631SPierre-Louis Bossart {
19779ee6631SPierre-Louis Bossart 	debugfs_remove_recursive(sdw->debugfs);
19879ee6631SPierre-Louis Bossart }
19979ee6631SPierre-Louis Bossart #else
20079ee6631SPierre-Louis Bossart static void intel_debugfs_init(struct sdw_intel *sdw) {}
20179ee6631SPierre-Louis Bossart static void intel_debugfs_exit(struct sdw_intel *sdw) {}
20279ee6631SPierre-Louis Bossart #endif /* CONFIG_DEBUG_FS */
20379ee6631SPierre-Louis Bossart 
20479ee6631SPierre-Louis Bossart /*
20571bb8a1bSVinod Koul  * shim ops
20671bb8a1bSVinod Koul  */
2074a17c441SPierre-Louis Bossart /* this needs to be called with shim_lock */
2084a17c441SPierre-Louis Bossart static void intel_shim_glue_to_master_ip(struct sdw_intel *sdw)
20971bb8a1bSVinod Koul {
2102523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
21171bb8a1bSVinod Koul 	unsigned int link_id = sdw->instance;
2124a17c441SPierre-Louis Bossart 	u16 ioctl;
21371bb8a1bSVinod Koul 
21471bb8a1bSVinod Koul 	/* Switch to MIP from Glue logic */
21571bb8a1bSVinod Koul 	ioctl = intel_readw(shim,  SDW_SHIM_IOCTL(link_id));
21671bb8a1bSVinod Koul 
21771bb8a1bSVinod Koul 	ioctl &= ~(SDW_SHIM_IOCTL_DOE);
21871bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2194a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
22071bb8a1bSVinod Koul 
22171bb8a1bSVinod Koul 	ioctl &= ~(SDW_SHIM_IOCTL_DO);
22271bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2234a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
22471bb8a1bSVinod Koul 
22571bb8a1bSVinod Koul 	ioctl |= (SDW_SHIM_IOCTL_MIF);
22671bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2274a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
22871bb8a1bSVinod Koul 
22971bb8a1bSVinod Koul 	ioctl &= ~(SDW_SHIM_IOCTL_BKE);
23071bb8a1bSVinod Koul 	ioctl &= ~(SDW_SHIM_IOCTL_COE);
23171bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2324a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2334a17c441SPierre-Louis Bossart 
2344a17c441SPierre-Louis Bossart 	/* at this point Master IP has full control of the I/Os */
2354a17c441SPierre-Louis Bossart }
2364a17c441SPierre-Louis Bossart 
2374a17c441SPierre-Louis Bossart /* this needs to be called with shim_lock */
2384a17c441SPierre-Louis Bossart static void intel_shim_master_ip_to_glue(struct sdw_intel *sdw)
2394a17c441SPierre-Louis Bossart {
2404a17c441SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
2414a17c441SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
2424a17c441SPierre-Louis Bossart 	u16 ioctl;
2434a17c441SPierre-Louis Bossart 
2444a17c441SPierre-Louis Bossart 	/* Glue logic */
2454a17c441SPierre-Louis Bossart 	ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
2464a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_BKE;
2474a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_COE;
2484a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2494a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2504a17c441SPierre-Louis Bossart 
2514a17c441SPierre-Louis Bossart 	ioctl &= ~(SDW_SHIM_IOCTL_MIF);
2524a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2534a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2544a17c441SPierre-Louis Bossart 
2554a17c441SPierre-Louis Bossart 	/* at this point Integration Glue has full control of the I/Os */
2564a17c441SPierre-Louis Bossart }
2574a17c441SPierre-Louis Bossart 
258b81bcdb4SPierre-Louis Bossart /* this needs to be called with shim_lock */
259b81bcdb4SPierre-Louis Bossart static void intel_shim_init(struct sdw_intel *sdw)
2604a17c441SPierre-Louis Bossart {
2614a17c441SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
2624a17c441SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
2633d912d1aSChao Song 	u16 ioctl = 0, act;
2644a17c441SPierre-Louis Bossart 
2654a17c441SPierre-Louis Bossart 	/* Initialize Shim */
2664a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_BKE;
2674a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2684a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2694a17c441SPierre-Louis Bossart 
2704a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_WPDD;
2714a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2724a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2734a17c441SPierre-Louis Bossart 
2744a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_DO;
2754a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2764a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2774a17c441SPierre-Louis Bossart 
2784a17c441SPierre-Louis Bossart 	ioctl |= SDW_SHIM_IOCTL_DOE;
2794a17c441SPierre-Louis Bossart 	intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
2804a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
2814a17c441SPierre-Louis Bossart 
2824a17c441SPierre-Louis Bossart 	intel_shim_glue_to_master_ip(sdw);
28371bb8a1bSVinod Koul 
2843d912d1aSChao Song 	act = intel_readw(shim, SDW_SHIM_CTMCTL(link_id));
285f067c925SVinod Koul 	u16p_replace_bits(&act, 0x1, SDW_SHIM_CTMCTL_DOAIS);
28671bb8a1bSVinod Koul 	act |= SDW_SHIM_CTMCTL_DACTQE;
28771bb8a1bSVinod Koul 	act |= SDW_SHIM_CTMCTL_DODS;
28871bb8a1bSVinod Koul 	intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
2894a17c441SPierre-Louis Bossart 	usleep_range(10, 15);
29071bb8a1bSVinod Koul }
29171bb8a1bSVinod Koul 
2920f3c54c2SPierre-Louis Bossart static int intel_shim_check_wake(struct sdw_intel *sdw)
2930f3c54c2SPierre-Louis Bossart {
2940f3c54c2SPierre-Louis Bossart 	void __iomem *shim;
2950f3c54c2SPierre-Louis Bossart 	u16 wake_sts;
29671bb8a1bSVinod Koul 
2970f3c54c2SPierre-Louis Bossart 	shim = sdw->link_res->shim;
2980f3c54c2SPierre-Louis Bossart 	wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
2990f3c54c2SPierre-Louis Bossart 
3000f3c54c2SPierre-Louis Bossart 	return wake_sts & BIT(sdw->instance);
30171bb8a1bSVinod Koul }
30271bb8a1bSVinod Koul 
303ab2c9132SRander Wang static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
3044a17c441SPierre-Louis Bossart {
3054a17c441SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
3064a17c441SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
3074a17c441SPierre-Louis Bossart 	u16 wake_en, wake_sts;
3084a17c441SPierre-Louis Bossart 
3094a17c441SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
3104a17c441SPierre-Louis Bossart 	wake_en = intel_readw(shim, SDW_SHIM_WAKEEN);
3114a17c441SPierre-Louis Bossart 
3124a17c441SPierre-Louis Bossart 	if (wake_enable) {
3134a17c441SPierre-Louis Bossart 		/* Enable the wakeup */
3144a17c441SPierre-Louis Bossart 		wake_en |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
3154a17c441SPierre-Louis Bossart 		intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
3164a17c441SPierre-Louis Bossart 	} else {
3174a17c441SPierre-Louis Bossart 		/* Disable the wake up interrupt */
3184a17c441SPierre-Louis Bossart 		wake_en &= ~(SDW_SHIM_WAKEEN_ENABLE << link_id);
3194a17c441SPierre-Louis Bossart 		intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
3204a17c441SPierre-Louis Bossart 
3214a17c441SPierre-Louis Bossart 		/* Clear wake status */
3224a17c441SPierre-Louis Bossart 		wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
3233957db3aSLibin Yang 		wake_sts |= (SDW_SHIM_WAKESTS_STATUS << link_id);
3243957db3aSLibin Yang 		intel_writew(shim, SDW_SHIM_WAKESTS, wake_sts);
3254a17c441SPierre-Louis Bossart 	}
3264a17c441SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
3274a17c441SPierre-Louis Bossart }
3284a17c441SPierre-Louis Bossart 
3291e76de2eSPierre-Louis Bossart static bool intel_check_cmdsync_unlocked(struct sdw_intel *sdw)
3301e76de2eSPierre-Louis Bossart {
3311e76de2eSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
3321e76de2eSPierre-Louis Bossart 	int sync_reg;
3331e76de2eSPierre-Louis Bossart 
3341e76de2eSPierre-Louis Bossart 	sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
3351e76de2eSPierre-Louis Bossart 	return !!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK);
3361e76de2eSPierre-Louis Bossart }
3371e76de2eSPierre-Louis Bossart 
338bc872947SPierre-Louis Bossart static int intel_link_power_up(struct sdw_intel *sdw)
339bc872947SPierre-Louis Bossart {
340bc872947SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
341bc872947SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
342bc872947SPierre-Louis Bossart 	u32 *shim_mask = sdw->link_res->shim_mask;
343bc872947SPierre-Louis Bossart 	struct sdw_bus *bus = &sdw->cdns.bus;
344bc872947SPierre-Louis Bossart 	struct sdw_master_prop *prop = &bus->prop;
345bc872947SPierre-Louis Bossart 	u32 spa_mask, cpa_mask;
346bc872947SPierre-Louis Bossart 	u32 link_control;
347bc872947SPierre-Louis Bossart 	int ret = 0;
34809ee49e3SPierre-Louis Bossart 	u32 clock_source;
349bc872947SPierre-Louis Bossart 	u32 syncprd;
350bc872947SPierre-Louis Bossart 	u32 sync_reg;
35109ee49e3SPierre-Louis Bossart 	bool lcap_mlcs;
352bc872947SPierre-Louis Bossart 
353bc872947SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
354bc872947SPierre-Louis Bossart 
355bc872947SPierre-Louis Bossart 	/*
356bc872947SPierre-Louis Bossart 	 * The hardware relies on an internal counter, typically 4kHz,
357bc872947SPierre-Louis Bossart 	 * to generate the SoundWire SSP - which defines a 'safe'
358bc872947SPierre-Louis Bossart 	 * synchronization point between commands and audio transport
359bc872947SPierre-Louis Bossart 	 * and allows for multi link synchronization. The SYNCPRD value
360bc872947SPierre-Louis Bossart 	 * is only dependent on the oscillator clock provided to
361bc872947SPierre-Louis Bossart 	 * the IP, so adjust based on _DSD properties reported in DSDT
362bc872947SPierre-Louis Bossart 	 * tables. The values reported are based on either 24MHz
36309ee49e3SPierre-Louis Bossart 	 * (CNL/CML) or 38.4 MHz (ICL/TGL+). On MeteorLake additional
36409ee49e3SPierre-Louis Bossart 	 * frequencies are available with the MLCS clock source selection.
365bc872947SPierre-Louis Bossart 	 */
36609ee49e3SPierre-Louis Bossart 	lcap_mlcs = intel_readl(shim, SDW_SHIM_LCAP) & SDW_SHIM_LCAP_MLCS_MASK;
36709ee49e3SPierre-Louis Bossart 
36809ee49e3SPierre-Louis Bossart 	if (prop->mclk_freq % 6000000) {
36909ee49e3SPierre-Louis Bossart 		if (prop->mclk_freq % 2400000) {
37009ee49e3SPierre-Louis Bossart 			if (lcap_mlcs) {
37109ee49e3SPierre-Louis Bossart 				syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24_576;
37209ee49e3SPierre-Louis Bossart 				clock_source = SDW_SHIM_MLCS_CARDINAL_CLK;
37309ee49e3SPierre-Louis Bossart 			} else {
37409ee49e3SPierre-Louis Bossart 				dev_err(sdw->cdns.dev, "%s: invalid clock configuration, mclk %d lcap_mlcs %d\n",
37509ee49e3SPierre-Louis Bossart 					__func__, prop->mclk_freq, lcap_mlcs);
37609ee49e3SPierre-Louis Bossart 				ret = -EINVAL;
37709ee49e3SPierre-Louis Bossart 				goto out;
37809ee49e3SPierre-Louis Bossart 			}
37909ee49e3SPierre-Louis Bossart 		} else {
380bc872947SPierre-Louis Bossart 			syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
38109ee49e3SPierre-Louis Bossart 			clock_source = SDW_SHIM_MLCS_XTAL_CLK;
38209ee49e3SPierre-Louis Bossart 		}
38309ee49e3SPierre-Louis Bossart 	} else {
38409ee49e3SPierre-Louis Bossart 		if (lcap_mlcs) {
38509ee49e3SPierre-Louis Bossart 			syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_96;
38609ee49e3SPierre-Louis Bossart 			clock_source = SDW_SHIM_MLCS_AUDIO_PLL_CLK;
38709ee49e3SPierre-Louis Bossart 		} else {
388bc872947SPierre-Louis Bossart 			syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24;
38909ee49e3SPierre-Louis Bossart 			clock_source = SDW_SHIM_MLCS_XTAL_CLK;
39009ee49e3SPierre-Louis Bossart 		}
39109ee49e3SPierre-Louis Bossart 	}
392bc872947SPierre-Louis Bossart 
393bc872947SPierre-Louis Bossart 	if (!*shim_mask) {
394bc872947SPierre-Louis Bossart 		dev_dbg(sdw->cdns.dev, "powering up all links\n");
395bc872947SPierre-Louis Bossart 
396bc872947SPierre-Louis Bossart 		/* we first need to program the SyncPRD/CPU registers */
397bc872947SPierre-Louis Bossart 		dev_dbg(sdw->cdns.dev,
398bc872947SPierre-Louis Bossart 			"first link up, programming SYNCPRD\n");
399bc872947SPierre-Louis Bossart 
400bc872947SPierre-Louis Bossart 		/* set SyncPRD period */
401bc872947SPierre-Louis Bossart 		sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
402bc872947SPierre-Louis Bossart 		u32p_replace_bits(&sync_reg, syncprd, SDW_SHIM_SYNC_SYNCPRD);
403bc872947SPierre-Louis Bossart 
404bc872947SPierre-Louis Bossart 		/* Set SyncCPU bit */
405bc872947SPierre-Louis Bossart 		sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
406bc872947SPierre-Louis Bossart 		intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
407bc872947SPierre-Louis Bossart 
408bc872947SPierre-Louis Bossart 		/* Link power up sequence */
409bc872947SPierre-Louis Bossart 		link_control = intel_readl(shim, SDW_SHIM_LCTL);
410bc872947SPierre-Louis Bossart 
411bc872947SPierre-Louis Bossart 		/* only power-up enabled links */
412bc872947SPierre-Louis Bossart 		spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, sdw->link_res->link_mask);
413bc872947SPierre-Louis Bossart 		cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
414bc872947SPierre-Louis Bossart 
415bc872947SPierre-Louis Bossart 		link_control |=  spa_mask;
416bc872947SPierre-Louis Bossart 
417bc872947SPierre-Louis Bossart 		ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
418bc872947SPierre-Louis Bossart 		if (ret < 0) {
419bc872947SPierre-Louis Bossart 			dev_err(sdw->cdns.dev, "Failed to power up link: %d\n", ret);
420bc872947SPierre-Louis Bossart 			goto out;
421bc872947SPierre-Louis Bossart 		}
422bc872947SPierre-Louis Bossart 
423bc872947SPierre-Louis Bossart 		/* SyncCPU will change once link is active */
424bc872947SPierre-Louis Bossart 		ret = intel_wait_bit(shim, SDW_SHIM_SYNC,
425bc872947SPierre-Louis Bossart 				     SDW_SHIM_SYNC_SYNCCPU, 0);
426bc872947SPierre-Louis Bossart 		if (ret < 0) {
427bc872947SPierre-Louis Bossart 			dev_err(sdw->cdns.dev,
428bc872947SPierre-Louis Bossart 				"Failed to set SHIM_SYNC: %d\n", ret);
429bc872947SPierre-Louis Bossart 			goto out;
430bc872947SPierre-Louis Bossart 		}
43109ee49e3SPierre-Louis Bossart 
43209ee49e3SPierre-Louis Bossart 		/* update link clock if needed */
43309ee49e3SPierre-Louis Bossart 		if (lcap_mlcs) {
43409ee49e3SPierre-Louis Bossart 			link_control = intel_readl(shim, SDW_SHIM_LCTL);
43509ee49e3SPierre-Louis Bossart 			u32p_replace_bits(&link_control, clock_source, SDW_SHIM_LCTL_MLCS_MASK);
43609ee49e3SPierre-Louis Bossart 			intel_writel(shim, SDW_SHIM_LCTL, link_control);
43709ee49e3SPierre-Louis Bossart 		}
438bc872947SPierre-Louis Bossart 	}
439bc872947SPierre-Louis Bossart 
440bc872947SPierre-Louis Bossart 	*shim_mask |= BIT(link_id);
441bc872947SPierre-Louis Bossart 
442bc872947SPierre-Louis Bossart 	sdw->cdns.link_up = true;
443b81bcdb4SPierre-Louis Bossart 
444b81bcdb4SPierre-Louis Bossart 	intel_shim_init(sdw);
445b81bcdb4SPierre-Louis Bossart 
446bc872947SPierre-Louis Bossart out:
447bc872947SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
448bc872947SPierre-Louis Bossart 
449bc872947SPierre-Louis Bossart 	return ret;
450bc872947SPierre-Louis Bossart }
451bc872947SPierre-Louis Bossart 
4529b3b4b3fSPierre-Louis Bossart static int intel_link_power_down(struct sdw_intel *sdw)
4534a17c441SPierre-Louis Bossart {
4545ee74eb2SPierre-Louis Bossart 	u32 link_control, spa_mask, cpa_mask;
4554a17c441SPierre-Louis Bossart 	unsigned int link_id = sdw->instance;
4564a17c441SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
4574a17c441SPierre-Louis Bossart 	u32 *shim_mask = sdw->link_res->shim_mask;
4584a17c441SPierre-Louis Bossart 	int ret = 0;
4594a17c441SPierre-Louis Bossart 
4604a17c441SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
4614a17c441SPierre-Louis Bossart 
4624a17c441SPierre-Louis Bossart 	if (!(*shim_mask & BIT(link_id)))
4634a17c441SPierre-Louis Bossart 		dev_err(sdw->cdns.dev,
4644a17c441SPierre-Louis Bossart 			"%s: Unbalanced power-up/down calls\n", __func__);
4654a17c441SPierre-Louis Bossart 
466ea6942daSPierre-Louis Bossart 	sdw->cdns.link_up = false;
467ea6942daSPierre-Louis Bossart 
468ea6942daSPierre-Louis Bossart 	intel_shim_master_ip_to_glue(sdw);
469ea6942daSPierre-Louis Bossart 
4704a17c441SPierre-Louis Bossart 	*shim_mask &= ~BIT(link_id);
4714a17c441SPierre-Louis Bossart 
4725ee74eb2SPierre-Louis Bossart 	if (!*shim_mask) {
4735ee74eb2SPierre-Louis Bossart 
47463198aaaSPierre-Louis Bossart 		dev_dbg(sdw->cdns.dev, "powering down all links\n");
4755ee74eb2SPierre-Louis Bossart 
4765ee74eb2SPierre-Louis Bossart 		/* Link power down sequence */
4775ee74eb2SPierre-Louis Bossart 		link_control = intel_readl(shim, SDW_SHIM_LCTL);
4785ee74eb2SPierre-Louis Bossart 
4795ee74eb2SPierre-Louis Bossart 		/* only power-down enabled links */
4803b4979caSVinod Koul 		spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, ~sdw->link_res->link_mask);
4813b4979caSVinod Koul 		cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
4825ee74eb2SPierre-Louis Bossart 
4835ee74eb2SPierre-Louis Bossart 		link_control &=  spa_mask;
4845ee74eb2SPierre-Louis Bossart 
4855ee74eb2SPierre-Louis Bossart 		ret = intel_clear_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
486ea6942daSPierre-Louis Bossart 		if (ret < 0) {
487ea6942daSPierre-Louis Bossart 			dev_err(sdw->cdns.dev, "%s: could not power down link\n", __func__);
488ea6942daSPierre-Louis Bossart 
489ea6942daSPierre-Louis Bossart 			/*
490ea6942daSPierre-Louis Bossart 			 * we leave the sdw->cdns.link_up flag as false since we've disabled
491ea6942daSPierre-Louis Bossart 			 * the link at this point and cannot handle interrupts any longer.
492ea6942daSPierre-Louis Bossart 			 */
493ea6942daSPierre-Louis Bossart 		}
4945ee74eb2SPierre-Louis Bossart 	}
4955ee74eb2SPierre-Louis Bossart 
4964a17c441SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
4974a17c441SPierre-Louis Bossart 
4984a17c441SPierre-Louis Bossart 	return ret;
4995ee74eb2SPierre-Louis Bossart }
5004a17c441SPierre-Louis Bossart 
50102629e45SPierre-Louis Bossart static void intel_shim_sync_arm(struct sdw_intel *sdw)
50202629e45SPierre-Louis Bossart {
50302629e45SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
50402629e45SPierre-Louis Bossart 	u32 sync_reg;
50502629e45SPierre-Louis Bossart 
50602629e45SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
50702629e45SPierre-Louis Bossart 
50802629e45SPierre-Louis Bossart 	/* update SYNC register */
50902629e45SPierre-Louis Bossart 	sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
51002629e45SPierre-Louis Bossart 	sync_reg |= (SDW_SHIM_SYNC_CMDSYNC << sdw->instance);
51102629e45SPierre-Louis Bossart 	intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
51202629e45SPierre-Louis Bossart 
51302629e45SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
51402629e45SPierre-Louis Bossart }
51502629e45SPierre-Louis Bossart 
516437e3289SPierre-Louis Bossart static int intel_shim_sync_go_unlocked(struct sdw_intel *sdw)
517437e3289SPierre-Louis Bossart {
518437e3289SPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
519437e3289SPierre-Louis Bossart 	u32 sync_reg;
520437e3289SPierre-Louis Bossart 
521437e3289SPierre-Louis Bossart 	/* Read SYNC register */
522437e3289SPierre-Louis Bossart 	sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
523437e3289SPierre-Louis Bossart 
524437e3289SPierre-Louis Bossart 	/*
525437e3289SPierre-Louis Bossart 	 * Set SyncGO bit to synchronously trigger a bank switch for
526437e3289SPierre-Louis Bossart 	 * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
527437e3289SPierre-Louis Bossart 	 * the Masters.
528437e3289SPierre-Louis Bossart 	 */
529437e3289SPierre-Louis Bossart 	sync_reg |= SDW_SHIM_SYNC_SYNCGO;
530437e3289SPierre-Louis Bossart 
5319c49a4ddSPierre-Louis Bossart 	intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
532437e3289SPierre-Louis Bossart 
5339c49a4ddSPierre-Louis Bossart 	return 0;
53471bb8a1bSVinod Koul }
53571bb8a1bSVinod Koul 
536857a7c42SPierre-Louis Bossart static int intel_shim_sync_go(struct sdw_intel *sdw)
537857a7c42SPierre-Louis Bossart {
538857a7c42SPierre-Louis Bossart 	int ret;
539857a7c42SPierre-Louis Bossart 
540857a7c42SPierre-Louis Bossart 	mutex_lock(sdw->link_res->shim_lock);
541857a7c42SPierre-Louis Bossart 
542857a7c42SPierre-Louis Bossart 	ret = intel_shim_sync_go_unlocked(sdw);
543857a7c42SPierre-Louis Bossart 
544857a7c42SPierre-Louis Bossart 	mutex_unlock(sdw->link_res->shim_lock);
545857a7c42SPierre-Louis Bossart 
546857a7c42SPierre-Louis Bossart 	return ret;
547857a7c42SPierre-Louis Bossart }
548857a7c42SPierre-Louis Bossart 
54937a2d22bSVinod Koul /*
55037a2d22bSVinod Koul  * PDI routines
55137a2d22bSVinod Koul  */
55237a2d22bSVinod Koul static void intel_pdi_init(struct sdw_intel *sdw,
55337a2d22bSVinod Koul 			   struct sdw_cdns_stream_config *config)
55437a2d22bSVinod Koul {
5552523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
55637a2d22bSVinod Koul 	unsigned int link_id = sdw->instance;
55763a6aa96SPierre-Louis Bossart 	int pcm_cap;
55837a2d22bSVinod Koul 
55937a2d22bSVinod Koul 	/* PCM Stream Capability */
56037a2d22bSVinod Koul 	pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
56137a2d22bSVinod Koul 
5623b4979caSVinod Koul 	config->pcm_bd = FIELD_GET(SDW_SHIM_PCMSCAP_BSS, pcm_cap);
5633b4979caSVinod Koul 	config->pcm_in = FIELD_GET(SDW_SHIM_PCMSCAP_ISS, pcm_cap);
5643b4979caSVinod Koul 	config->pcm_out = FIELD_GET(SDW_SHIM_PCMSCAP_OSS, pcm_cap);
56537a2d22bSVinod Koul 
566121f4361SPierre-Louis Bossart 	dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
567121f4361SPierre-Louis Bossart 		config->pcm_bd, config->pcm_in, config->pcm_out);
56837a2d22bSVinod Koul }
56937a2d22bSVinod Koul 
57037a2d22bSVinod Koul static int
57163a6aa96SPierre-Louis Bossart intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num)
57237a2d22bSVinod Koul {
5732523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
57437a2d22bSVinod Koul 	unsigned int link_id = sdw->instance;
57537a2d22bSVinod Koul 	int count;
57637a2d22bSVinod Koul 
57737a2d22bSVinod Koul 	count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
57818046335SPierre-Louis Bossart 
57918046335SPierre-Louis Bossart 	/*
58018046335SPierre-Louis Bossart 	 * WORKAROUND: on all existing Intel controllers, pdi
58118046335SPierre-Louis Bossart 	 * number 2 reports channel count as 1 even though it
58218046335SPierre-Louis Bossart 	 * supports 8 channels. Performing hardcoding for pdi
58318046335SPierre-Louis Bossart 	 * number 2.
58418046335SPierre-Louis Bossart 	 */
58518046335SPierre-Louis Bossart 	if (pdi_num == 2)
58618046335SPierre-Louis Bossart 		count = 7;
58718046335SPierre-Louis Bossart 
58837a2d22bSVinod Koul 	/* zero based values for channel count in register */
58937a2d22bSVinod Koul 	count++;
59037a2d22bSVinod Koul 
59137a2d22bSVinod Koul 	return count;
59237a2d22bSVinod Koul }
59337a2d22bSVinod Koul 
59437a2d22bSVinod Koul static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
59537a2d22bSVinod Koul 				   struct sdw_cdns_pdi *pdi,
59637a2d22bSVinod Koul 				   unsigned int num_pdi,
59763a6aa96SPierre-Louis Bossart 				   unsigned int *num_ch)
59837a2d22bSVinod Koul {
59937a2d22bSVinod Koul 	int i, ch_count = 0;
60037a2d22bSVinod Koul 
60137a2d22bSVinod Koul 	for (i = 0; i < num_pdi; i++) {
60263a6aa96SPierre-Louis Bossart 		pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num);
60337a2d22bSVinod Koul 		ch_count += pdi->ch_count;
60437a2d22bSVinod Koul 		pdi++;
60537a2d22bSVinod Koul 	}
60637a2d22bSVinod Koul 
60737a2d22bSVinod Koul 	*num_ch = ch_count;
60837a2d22bSVinod Koul 	return 0;
60937a2d22bSVinod Koul }
61037a2d22bSVinod Koul 
61137a2d22bSVinod Koul static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
61263a6aa96SPierre-Louis Bossart 				      struct sdw_cdns_streams *stream)
61337a2d22bSVinod Koul {
61437a2d22bSVinod Koul 	intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
61563a6aa96SPierre-Louis Bossart 				&stream->num_ch_bd);
61637a2d22bSVinod Koul 
61737a2d22bSVinod Koul 	intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
61863a6aa96SPierre-Louis Bossart 				&stream->num_ch_in);
61937a2d22bSVinod Koul 
62037a2d22bSVinod Koul 	intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
62163a6aa96SPierre-Louis Bossart 				&stream->num_ch_out);
62237a2d22bSVinod Koul 
62337a2d22bSVinod Koul 	return 0;
62437a2d22bSVinod Koul }
62537a2d22bSVinod Koul 
62637a2d22bSVinod Koul static void
62737a2d22bSVinod Koul intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
62837a2d22bSVinod Koul {
6292523486bSPierre-Louis Bossart 	void __iomem *shim = sdw->link_res->shim;
63037a2d22bSVinod Koul 	unsigned int link_id = sdw->instance;
63137a2d22bSVinod Koul 	int pdi_conf = 0;
63237a2d22bSVinod Koul 
633c134f914SPierre-Louis Bossart 	/* the Bulk and PCM streams are not contiguous */
634c134f914SPierre-Louis Bossart 	pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
635c134f914SPierre-Louis Bossart 	if (pdi->num >= 2)
636c134f914SPierre-Louis Bossart 		pdi->intel_alh_id += 2;
63737a2d22bSVinod Koul 
63837a2d22bSVinod Koul 	/*
63937a2d22bSVinod Koul 	 * Program stream parameters to stream SHIM register
64037a2d22bSVinod Koul 	 * This is applicable for PCM stream only.
64137a2d22bSVinod Koul 	 */
64237a2d22bSVinod Koul 	if (pdi->type != SDW_STREAM_PCM)
64337a2d22bSVinod Koul 		return;
64437a2d22bSVinod Koul 
64537a2d22bSVinod Koul 	if (pdi->dir == SDW_DATA_DIR_RX)
64637a2d22bSVinod Koul 		pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
64737a2d22bSVinod Koul 	else
64837a2d22bSVinod Koul 		pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
64937a2d22bSVinod Koul 
650f067c925SVinod Koul 	u32p_replace_bits(&pdi_conf, pdi->intel_alh_id, SDW_SHIM_PCMSYCM_STREAM);
651f067c925SVinod Koul 	u32p_replace_bits(&pdi_conf, pdi->l_ch_num, SDW_SHIM_PCMSYCM_LCHN);
652f067c925SVinod Koul 	u32p_replace_bits(&pdi_conf, pdi->h_ch_num, SDW_SHIM_PCMSYCM_HCHN);
65337a2d22bSVinod Koul 
65437a2d22bSVinod Koul 	intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
65537a2d22bSVinod Koul }
65637a2d22bSVinod Koul 
65737a2d22bSVinod Koul static void
65837a2d22bSVinod Koul intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
65937a2d22bSVinod Koul {
6602523486bSPierre-Louis Bossart 	void __iomem *alh = sdw->link_res->alh;
66137a2d22bSVinod Koul 	unsigned int link_id = sdw->instance;
66237a2d22bSVinod Koul 	unsigned int conf;
66337a2d22bSVinod Koul 
664c134f914SPierre-Louis Bossart 	/* the Bulk and PCM streams are not contiguous */
665c134f914SPierre-Louis Bossart 	pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
666c134f914SPierre-Louis Bossart 	if (pdi->num >= 2)
667c134f914SPierre-Louis Bossart 		pdi->intel_alh_id += 2;
66837a2d22bSVinod Koul 
66937a2d22bSVinod Koul 	/* Program Stream config ALH register */
67037a2d22bSVinod Koul 	conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
67137a2d22bSVinod Koul 
672f067c925SVinod Koul 	u32p_replace_bits(&conf, SDW_ALH_STRMZCFG_DMAT_VAL, SDW_ALH_STRMZCFG_DMAT);
673f067c925SVinod Koul 	u32p_replace_bits(&conf, pdi->ch_count - 1, SDW_ALH_STRMZCFG_CHN);
67437a2d22bSVinod Koul 
67537a2d22bSVinod Koul 	intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
67637a2d22bSVinod Koul }
67737a2d22bSVinod Koul 
6784b206d34SRander Wang static int intel_params_stream(struct sdw_intel *sdw,
6791d905d35SPierre-Louis Bossart 			       struct snd_pcm_substream *substream,
680c46302ecSVinod Koul 			       struct snd_soc_dai *dai,
6814b206d34SRander Wang 			       struct snd_pcm_hw_params *hw_params,
6824b206d34SRander Wang 			       int link_id, int alh_stream_id)
683c46302ecSVinod Koul {
6842523486bSPierre-Louis Bossart 	struct sdw_intel_link_res *res = sdw->link_res;
6854b206d34SRander Wang 	struct sdw_intel_stream_params_data params_data;
68605c8afe4SPierre-Louis Bossart 
6871d905d35SPierre-Louis Bossart 	params_data.substream = substream;
6884b206d34SRander Wang 	params_data.dai = dai;
6894b206d34SRander Wang 	params_data.hw_params = hw_params;
6904b206d34SRander Wang 	params_data.link_id = link_id;
6914b206d34SRander Wang 	params_data.alh_stream_id = alh_stream_id;
692c46302ecSVinod Koul 
6934b206d34SRander Wang 	if (res->ops && res->ops->params_stream && res->dev)
6944b206d34SRander Wang 		return res->ops->params_stream(res->dev,
6954b206d34SRander Wang 					       &params_data);
696c46302ecSVinod Koul 	return -EIO;
697c46302ecSVinod Koul }
698c46302ecSVinod Koul 
699c46302ecSVinod Koul /*
700c46302ecSVinod Koul  * DAI routines
701c46302ecSVinod Koul  */
702c46302ecSVinod Koul 
7032a9c6ff5SRanjani Sridharan static int intel_free_stream(struct sdw_intel *sdw,
7042a9c6ff5SRanjani Sridharan 			     struct snd_pcm_substream *substream,
7052a9c6ff5SRanjani Sridharan 			     struct snd_soc_dai *dai,
7062a9c6ff5SRanjani Sridharan 			     int link_id)
7072a9c6ff5SRanjani Sridharan {
7082a9c6ff5SRanjani Sridharan 	struct sdw_intel_link_res *res = sdw->link_res;
7092a9c6ff5SRanjani Sridharan 	struct sdw_intel_stream_free_data free_data;
7102a9c6ff5SRanjani Sridharan 
7112a9c6ff5SRanjani Sridharan 	free_data.substream = substream;
7122a9c6ff5SRanjani Sridharan 	free_data.dai = dai;
7132a9c6ff5SRanjani Sridharan 	free_data.link_id = link_id;
7142a9c6ff5SRanjani Sridharan 
7152a9c6ff5SRanjani Sridharan 	if (res->ops && res->ops->free_stream && res->dev)
7162a9c6ff5SRanjani Sridharan 		return res->ops->free_stream(res->dev, &free_data);
7172a9c6ff5SRanjani Sridharan 
7182a9c6ff5SRanjani Sridharan 	return 0;
7192a9c6ff5SRanjani Sridharan }
7202a9c6ff5SRanjani Sridharan 
721c46302ecSVinod Koul static int intel_hw_params(struct snd_pcm_substream *substream,
722c46302ecSVinod Koul 			   struct snd_pcm_hw_params *params,
723c46302ecSVinod Koul 			   struct snd_soc_dai *dai)
724c46302ecSVinod Koul {
725c46302ecSVinod Koul 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
726c46302ecSVinod Koul 	struct sdw_intel *sdw = cdns_to_intel(cdns);
727e0767e39SPierre-Louis Bossart 	struct sdw_cdns_dai_runtime *dai_runtime;
72857a34790SPierre-Louis Bossart 	struct sdw_cdns_pdi *pdi;
729c46302ecSVinod Koul 	struct sdw_stream_config sconfig;
730c46302ecSVinod Koul 	struct sdw_port_config *pconfig;
73157a34790SPierre-Louis Bossart 	int ch, dir;
73257a34790SPierre-Louis Bossart 	int ret;
733c46302ecSVinod Koul 
7347dddead7SPierre-Louis Bossart 	dai_runtime = cdns->dai_runtime_array[dai->id];
735e0767e39SPierre-Louis Bossart 	if (!dai_runtime)
736c46302ecSVinod Koul 		return -EIO;
737c46302ecSVinod Koul 
738c46302ecSVinod Koul 	ch = params_channels(params);
739c46302ecSVinod Koul 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
740c46302ecSVinod Koul 		dir = SDW_DATA_DIR_RX;
741c46302ecSVinod Koul 	else
742c46302ecSVinod Koul 		dir = SDW_DATA_DIR_TX;
743c46302ecSVinod Koul 
7441b53385eSBard Liao 	pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
745c46302ecSVinod Koul 
746*ba874a8cSKrzysztof Kozlowski 	if (!pdi)
747*ba874a8cSKrzysztof Kozlowski 		return -EINVAL;
74857a34790SPierre-Louis Bossart 
74957a34790SPierre-Louis Bossart 	/* do run-time configurations for SHIM, ALH and PDI/PORT */
75057a34790SPierre-Louis Bossart 	intel_pdi_shim_configure(sdw, pdi);
75157a34790SPierre-Louis Bossart 	intel_pdi_alh_configure(sdw, pdi);
75257a34790SPierre-Louis Bossart 	sdw_cdns_config_stream(cdns, ch, dir, pdi);
75357a34790SPierre-Louis Bossart 
754a5a0239cSBard Liao 	/* store pdi and hw_params, may be needed in prepare step */
755e0767e39SPierre-Louis Bossart 	dai_runtime->paused = false;
756e0767e39SPierre-Louis Bossart 	dai_runtime->suspended = false;
757e0767e39SPierre-Louis Bossart 	dai_runtime->pdi = pdi;
758c46302ecSVinod Koul 
759c46302ecSVinod Koul 	/* Inform DSP about PDI stream number */
7601d905d35SPierre-Louis Bossart 	ret = intel_params_stream(sdw, substream, dai, params,
7614b206d34SRander Wang 				  sdw->instance,
76257a34790SPierre-Louis Bossart 				  pdi->intel_alh_id);
763c46302ecSVinod Koul 	if (ret)
764*ba874a8cSKrzysztof Kozlowski 		return ret;
765c46302ecSVinod Koul 
766c46302ecSVinod Koul 	sconfig.direction = dir;
767c46302ecSVinod Koul 	sconfig.ch_count = ch;
768c46302ecSVinod Koul 	sconfig.frame_rate = params_rate(params);
769e0767e39SPierre-Louis Bossart 	sconfig.type = dai_runtime->stream_type;
770c46302ecSVinod Koul 
771c46302ecSVinod Koul 	sconfig.bps = snd_pcm_format_width(params_format(params));
772c46302ecSVinod Koul 
773c46302ecSVinod Koul 	/* Port configuration */
774235ae89bSZheng Yongjun 	pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL);
775*ba874a8cSKrzysztof Kozlowski 	if (!pconfig)
776*ba874a8cSKrzysztof Kozlowski 		return -ENOMEM;
777c46302ecSVinod Koul 
77857a34790SPierre-Louis Bossart 	pconfig->num = pdi->num;
77957a34790SPierre-Louis Bossart 	pconfig->ch_mask = (1 << ch) - 1;
780c46302ecSVinod Koul 
781c46302ecSVinod Koul 	ret = sdw_stream_add_master(&cdns->bus, &sconfig,
782e0767e39SPierre-Louis Bossart 				    pconfig, 1, dai_runtime->stream);
78357a34790SPierre-Louis Bossart 	if (ret)
78417ed5befSPierre-Louis Bossart 		dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
785c46302ecSVinod Koul 
786c46302ecSVinod Koul 	kfree(pconfig);
787*ba874a8cSKrzysztof Kozlowski 
788c46302ecSVinod Koul 	return ret;
789c46302ecSVinod Koul }
790c46302ecSVinod Koul 
79127b198f4SRander Wang static int intel_prepare(struct snd_pcm_substream *substream,
79227b198f4SRander Wang 			 struct snd_soc_dai *dai)
79327b198f4SRander Wang {
794a5a0239cSBard Liao 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
795a5a0239cSBard Liao 	struct sdw_intel *sdw = cdns_to_intel(cdns);
796e0767e39SPierre-Louis Bossart 	struct sdw_cdns_dai_runtime *dai_runtime;
797a5a0239cSBard Liao 	int ch, dir;
798244eb888SPierre-Louis Bossart 	int ret = 0;
79927b198f4SRander Wang 
8007dddead7SPierre-Louis Bossart 	dai_runtime = cdns->dai_runtime_array[dai->id];
801e0767e39SPierre-Louis Bossart 	if (!dai_runtime) {
802e0767e39SPierre-Louis Bossart 		dev_err(dai->dev, "failed to get dai runtime in %s\n",
80327b198f4SRander Wang 			__func__);
80427b198f4SRander Wang 		return -EIO;
80527b198f4SRander Wang 	}
80627b198f4SRander Wang 
807e0767e39SPierre-Louis Bossart 	if (dai_runtime->suspended) {
80850cd92e0SKuninori Morimoto 		struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
8090a0d1740SPierre-Louis Bossart 		struct snd_pcm_hw_params *hw_params;
8100a0d1740SPierre-Louis Bossart 
8110a0d1740SPierre-Louis Bossart 		hw_params = &rtd->dpcm[substream->stream].hw_params;
8120a0d1740SPierre-Louis Bossart 
813e0767e39SPierre-Louis Bossart 		dai_runtime->suspended = false;
814a5a0239cSBard Liao 
815a5a0239cSBard Liao 		/*
816a5a0239cSBard Liao 		 * .prepare() is called after system resume, where we
817a5a0239cSBard Liao 		 * need to reinitialize the SHIM/ALH/Cadence IP.
818a5a0239cSBard Liao 		 * .prepare() is also called to deal with underflows,
819a5a0239cSBard Liao 		 * but in those cases we cannot touch ALH/SHIM
820a5a0239cSBard Liao 		 * registers
821a5a0239cSBard Liao 		 */
822a5a0239cSBard Liao 
823a5a0239cSBard Liao 		/* configure stream */
8240a0d1740SPierre-Louis Bossart 		ch = params_channels(hw_params);
825a5a0239cSBard Liao 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
826a5a0239cSBard Liao 			dir = SDW_DATA_DIR_RX;
827a5a0239cSBard Liao 		else
828a5a0239cSBard Liao 			dir = SDW_DATA_DIR_TX;
829a5a0239cSBard Liao 
830e0767e39SPierre-Louis Bossart 		intel_pdi_shim_configure(sdw, dai_runtime->pdi);
831e0767e39SPierre-Louis Bossart 		intel_pdi_alh_configure(sdw, dai_runtime->pdi);
832e0767e39SPierre-Louis Bossart 		sdw_cdns_config_stream(cdns, ch, dir, dai_runtime->pdi);
833a5a0239cSBard Liao 
834a5a0239cSBard Liao 		/* Inform DSP about PDI stream number */
8351d905d35SPierre-Louis Bossart 		ret = intel_params_stream(sdw, substream, dai,
8360a0d1740SPierre-Louis Bossart 					  hw_params,
837a5a0239cSBard Liao 					  sdw->instance,
838e0767e39SPierre-Louis Bossart 					  dai_runtime->pdi->intel_alh_id);
839a5a0239cSBard Liao 	}
840a5a0239cSBard Liao 
841a5a0239cSBard Liao 	return ret;
84227b198f4SRander Wang }
84327b198f4SRander Wang 
844c46302ecSVinod Koul static int
845c46302ecSVinod Koul intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
846c46302ecSVinod Koul {
847c46302ecSVinod Koul 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
8482a9c6ff5SRanjani Sridharan 	struct sdw_intel *sdw = cdns_to_intel(cdns);
849e0767e39SPierre-Louis Bossart 	struct sdw_cdns_dai_runtime *dai_runtime;
850c46302ecSVinod Koul 	int ret;
851c46302ecSVinod Koul 
8527dddead7SPierre-Louis Bossart 	dai_runtime = cdns->dai_runtime_array[dai->id];
853e0767e39SPierre-Louis Bossart 	if (!dai_runtime)
854c46302ecSVinod Koul 		return -EIO;
855c46302ecSVinod Koul 
856244eb888SPierre-Louis Bossart 	/*
857244eb888SPierre-Louis Bossart 	 * The sdw stream state will transition to RELEASED when stream->
858244eb888SPierre-Louis Bossart 	 * master_list is empty. So the stream state will transition to
859244eb888SPierre-Louis Bossart 	 * DEPREPARED for the first cpu-dai and to RELEASED for the last
860244eb888SPierre-Louis Bossart 	 * cpu-dai.
861244eb888SPierre-Louis Bossart 	 */
862e0767e39SPierre-Louis Bossart 	ret = sdw_stream_remove_master(&cdns->bus, dai_runtime->stream);
863eff346f2SPierre-Louis Bossart 	if (ret < 0) {
86417ed5befSPierre-Louis Bossart 		dev_err(dai->dev, "remove master from stream %s failed: %d\n",
865e0767e39SPierre-Louis Bossart 			dai_runtime->stream->name, ret);
866c46302ecSVinod Koul 		return ret;
867c46302ecSVinod Koul 	}
868c46302ecSVinod Koul 
8692a9c6ff5SRanjani Sridharan 	ret = intel_free_stream(sdw, substream, dai, sdw->instance);
8702a9c6ff5SRanjani Sridharan 	if (ret < 0) {
8712a9c6ff5SRanjani Sridharan 		dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
8722a9c6ff5SRanjani Sridharan 		return ret;
8732a9c6ff5SRanjani Sridharan 	}
8742a9c6ff5SRanjani Sridharan 
875e0767e39SPierre-Louis Bossart 	dai_runtime->pdi = NULL;
876a5a0239cSBard Liao 
877eff346f2SPierre-Louis Bossart 	return 0;
878eff346f2SPierre-Louis Bossart }
879eff346f2SPierre-Louis Bossart 
880c46302ecSVinod Koul static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
881c46302ecSVinod Koul 				    void *stream, int direction)
882c46302ecSVinod Koul {
88363a6aa96SPierre-Louis Bossart 	return cdns_set_sdw_stream(dai, stream, direction);
884c46302ecSVinod Koul }
885c46302ecSVinod Koul 
88609553140SPierre-Louis Bossart static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
88709553140SPierre-Louis Bossart 				  int direction)
88809553140SPierre-Louis Bossart {
8897dddead7SPierre-Louis Bossart 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
890e0767e39SPierre-Louis Bossart 	struct sdw_cdns_dai_runtime *dai_runtime;
89109553140SPierre-Louis Bossart 
8927dddead7SPierre-Louis Bossart 	dai_runtime = cdns->dai_runtime_array[dai->id];
893e0767e39SPierre-Louis Bossart 	if (!dai_runtime)
89406dcb4e4SPierre-Louis Bossart 		return ERR_PTR(-EINVAL);
89509553140SPierre-Louis Bossart 
896e0767e39SPierre-Louis Bossart 	return dai_runtime->stream;
89709553140SPierre-Louis Bossart }
89809553140SPierre-Louis Bossart 
8998ddeafb9SRanjani Sridharan static int intel_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
9008ddeafb9SRanjani Sridharan {
9018ddeafb9SRanjani Sridharan 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
902e0767e39SPierre-Louis Bossart 	struct sdw_cdns_dai_runtime *dai_runtime;
9038ddeafb9SRanjani Sridharan 	int ret = 0;
9048ddeafb9SRanjani Sridharan 
9057dddead7SPierre-Louis Bossart 	dai_runtime = cdns->dai_runtime_array[dai->id];
906e0767e39SPierre-Louis Bossart 	if (!dai_runtime) {
907e0767e39SPierre-Louis Bossart 		dev_err(dai->dev, "failed to get dai runtime in %s\n",
9088ddeafb9SRanjani Sridharan 			__func__);
9098ddeafb9SRanjani Sridharan 		return -EIO;
9108ddeafb9SRanjani Sridharan 	}
9118ddeafb9SRanjani Sridharan 
9128ddeafb9SRanjani Sridharan 	switch (cmd) {
9138ddeafb9SRanjani Sridharan 	case SNDRV_PCM_TRIGGER_SUSPEND:
9148ddeafb9SRanjani Sridharan 
9158ddeafb9SRanjani Sridharan 		/*
9168ddeafb9SRanjani Sridharan 		 * The .prepare callback is used to deal with xruns and resume operations.
9178ddeafb9SRanjani Sridharan 		 * In the case of xruns, the DMAs and SHIM registers cannot be touched,
9188ddeafb9SRanjani Sridharan 		 * but for resume operations the DMAs and SHIM registers need to be initialized.
9198ddeafb9SRanjani Sridharan 		 * the .trigger callback is used to track the suspend case only.
9208ddeafb9SRanjani Sridharan 		 */
9218ddeafb9SRanjani Sridharan 
922e0767e39SPierre-Louis Bossart 		dai_runtime->suspended = true;
9238ddeafb9SRanjani Sridharan 
9248ddeafb9SRanjani Sridharan 		break;
9258ddeafb9SRanjani Sridharan 
9268ddeafb9SRanjani Sridharan 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
927e0767e39SPierre-Louis Bossart 		dai_runtime->paused = true;
9288ddeafb9SRanjani Sridharan 		break;
9298ddeafb9SRanjani Sridharan 	case SNDRV_PCM_TRIGGER_STOP:
9308ddeafb9SRanjani Sridharan 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
931e0767e39SPierre-Louis Bossart 		dai_runtime->paused = false;
9328ddeafb9SRanjani Sridharan 		break;
9338ddeafb9SRanjani Sridharan 	default:
9348ddeafb9SRanjani Sridharan 		break;
9358ddeafb9SRanjani Sridharan 	}
9368ddeafb9SRanjani Sridharan 
9378ddeafb9SRanjani Sridharan 	return ret;
9388ddeafb9SRanjani Sridharan }
9398ddeafb9SRanjani Sridharan 
9403e9c9f90SPierre-Louis Bossart static int intel_component_probe(struct snd_soc_component *component)
9413e9c9f90SPierre-Louis Bossart {
9423e9c9f90SPierre-Louis Bossart 	int ret;
9433e9c9f90SPierre-Louis Bossart 
9443e9c9f90SPierre-Louis Bossart 	/*
9453e9c9f90SPierre-Louis Bossart 	 * make sure the device is pm_runtime_active before initiating
9463e9c9f90SPierre-Louis Bossart 	 * bus transactions during the card registration.
9473e9c9f90SPierre-Louis Bossart 	 * We use pm_runtime_resume() here, without taking a reference
9483e9c9f90SPierre-Louis Bossart 	 * and releasing it immediately.
9493e9c9f90SPierre-Louis Bossart 	 */
9503e9c9f90SPierre-Louis Bossart 	ret = pm_runtime_resume(component->dev);
9513e9c9f90SPierre-Louis Bossart 	if (ret < 0 && ret != -EACCES)
9523e9c9f90SPierre-Louis Bossart 		return ret;
9533e9c9f90SPierre-Louis Bossart 
9543e9c9f90SPierre-Louis Bossart 	return 0;
9553e9c9f90SPierre-Louis Bossart }
9563e9c9f90SPierre-Louis Bossart 
9578ddeafb9SRanjani Sridharan static int intel_component_dais_suspend(struct snd_soc_component *component)
9588ddeafb9SRanjani Sridharan {
9598ddeafb9SRanjani Sridharan 	struct snd_soc_dai *dai;
9608ddeafb9SRanjani Sridharan 
9618ddeafb9SRanjani Sridharan 	/*
9628ddeafb9SRanjani Sridharan 	 * In the corner case where a SUSPEND happens during a PAUSE, the ALSA core
9638ddeafb9SRanjani Sridharan 	 * does not throw the TRIGGER_SUSPEND. This leaves the DAIs in an unbalanced state.
9648ddeafb9SRanjani Sridharan 	 * Since the component suspend is called last, we can trap this corner case
9658ddeafb9SRanjani Sridharan 	 * and force the DAIs to release their resources.
9668ddeafb9SRanjani Sridharan 	 */
9678ddeafb9SRanjani Sridharan 	for_each_component_dais(component, dai) {
9688ddeafb9SRanjani Sridharan 		struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
969e0767e39SPierre-Louis Bossart 		struct sdw_cdns_dai_runtime *dai_runtime;
9708ddeafb9SRanjani Sridharan 
9717dddead7SPierre-Louis Bossart 		dai_runtime = cdns->dai_runtime_array[dai->id];
9728ddeafb9SRanjani Sridharan 
973e0767e39SPierre-Louis Bossart 		if (!dai_runtime)
9748ddeafb9SRanjani Sridharan 			continue;
9758ddeafb9SRanjani Sridharan 
976e0767e39SPierre-Louis Bossart 		if (dai_runtime->suspended)
9778ddeafb9SRanjani Sridharan 			continue;
9788ddeafb9SRanjani Sridharan 
97901271045SPierre-Louis Bossart 		if (dai_runtime->paused)
980e0767e39SPierre-Louis Bossart 			dai_runtime->suspended = true;
9818ddeafb9SRanjani Sridharan 	}
9828ddeafb9SRanjani Sridharan 
9838ddeafb9SRanjani Sridharan 	return 0;
9848ddeafb9SRanjani Sridharan }
9858ddeafb9SRanjani Sridharan 
986b1635596SJulia Lawall static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
987c46302ecSVinod Koul 	.hw_params = intel_hw_params,
98827b198f4SRander Wang 	.prepare = intel_prepare,
989c46302ecSVinod Koul 	.hw_free = intel_hw_free,
9908ddeafb9SRanjani Sridharan 	.trigger = intel_trigger,
991e8444560SPierre-Louis Bossart 	.set_stream = intel_pcm_set_sdw_stream,
992e8444560SPierre-Louis Bossart 	.get_stream = intel_get_sdw_stream,
993c46302ecSVinod Koul };
994c46302ecSVinod Koul 
995c46302ecSVinod Koul static const struct snd_soc_component_driver dai_component = {
996c46302ecSVinod Koul 	.name			= "soundwire",
9973e9c9f90SPierre-Louis Bossart 	.probe			= intel_component_probe,
998ca682020SCharles Keepax 	.suspend		= intel_component_dais_suspend,
999ca682020SCharles Keepax 	.legacy_dai_naming	= 1,
1000c46302ecSVinod Koul };
1001c46302ecSVinod Koul 
1002c46302ecSVinod Koul static int intel_create_dai(struct sdw_cdns *cdns,
1003c46302ecSVinod Koul 			    struct snd_soc_dai_driver *dais,
1004c46302ecSVinod Koul 			    enum intel_pdi_type type,
100563a6aa96SPierre-Louis Bossart 			    u32 num, u32 off, u32 max_ch)
1006c46302ecSVinod Koul {
1007c46302ecSVinod Koul 	int i;
1008c46302ecSVinod Koul 
1009c46302ecSVinod Koul 	if (num == 0)
1010c46302ecSVinod Koul 		return 0;
1011c46302ecSVinod Koul 
1012c46302ecSVinod Koul 	for (i = off; i < (off + num); i++) {
1013bf6d6e68SPierre-Louis Bossart 		dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
1014bf6d6e68SPierre-Louis Bossart 					      "SDW%d Pin%d",
1015c46302ecSVinod Koul 					      cdns->instance, i);
1016c46302ecSVinod Koul 		if (!dais[i].name)
1017c46302ecSVinod Koul 			return -ENOMEM;
1018c46302ecSVinod Koul 
1019c46302ecSVinod Koul 		if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
1020c46302ecSVinod Koul 			dais[i].playback.channels_min = 1;
1021c46302ecSVinod Koul 			dais[i].playback.channels_max = max_ch;
1022c46302ecSVinod Koul 		}
1023c46302ecSVinod Koul 
1024c46302ecSVinod Koul 		if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
102539194128SSrinivas Kandagatla 			dais[i].capture.channels_min = 1;
102639194128SSrinivas Kandagatla 			dais[i].capture.channels_max = max_ch;
1027c46302ecSVinod Koul 		}
1028c46302ecSVinod Koul 
1029c46302ecSVinod Koul 		dais[i].ops = &intel_pcm_dai_ops;
1030c46302ecSVinod Koul 	}
1031c46302ecSVinod Koul 
1032c46302ecSVinod Koul 	return 0;
1033c46302ecSVinod Koul }
1034c46302ecSVinod Koul 
1035c46302ecSVinod Koul static int intel_register_dai(struct sdw_intel *sdw)
1036c46302ecSVinod Koul {
10377dddead7SPierre-Louis Bossart 	struct sdw_cdns_dai_runtime **dai_runtime_array;
103830cbae66SPierre-Louis Bossart 	struct sdw_cdns_stream_config config;
1039c46302ecSVinod Koul 	struct sdw_cdns *cdns = &sdw->cdns;
1040c46302ecSVinod Koul 	struct sdw_cdns_streams *stream;
1041c46302ecSVinod Koul 	struct snd_soc_dai_driver *dais;
1042c46302ecSVinod Koul 	int num_dai, ret, off = 0;
1043c46302ecSVinod Koul 
104430cbae66SPierre-Louis Bossart 	/* Read the PDI config and initialize cadence PDI */
104530cbae66SPierre-Louis Bossart 	intel_pdi_init(sdw, &config);
104630cbae66SPierre-Louis Bossart 	ret = sdw_cdns_pdi_init(cdns, config);
104730cbae66SPierre-Louis Bossart 	if (ret)
104830cbae66SPierre-Louis Bossart 		return ret;
104930cbae66SPierre-Louis Bossart 
105059e924feSPierre-Louis Bossart 	intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm);
105130cbae66SPierre-Louis Bossart 
1052c46302ecSVinod Koul 	/* DAIs are created based on total number of PDIs supported */
105363a6aa96SPierre-Louis Bossart 	num_dai = cdns->pcm.num_pdi;
1054c46302ecSVinod Koul 
10557dddead7SPierre-Louis Bossart 	dai_runtime_array = devm_kcalloc(cdns->dev, num_dai,
10567dddead7SPierre-Louis Bossart 					 sizeof(struct sdw_cdns_dai_runtime *),
10577dddead7SPierre-Louis Bossart 					 GFP_KERNEL);
10587dddead7SPierre-Louis Bossart 	if (!dai_runtime_array)
10597dddead7SPierre-Louis Bossart 		return -ENOMEM;
10607dddead7SPierre-Louis Bossart 	cdns->dai_runtime_array = dai_runtime_array;
10617dddead7SPierre-Louis Bossart 
1062c46302ecSVinod Koul 	dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
1063c46302ecSVinod Koul 	if (!dais)
1064c46302ecSVinod Koul 		return -ENOMEM;
1065c46302ecSVinod Koul 
1066c46302ecSVinod Koul 	/* Create PCM DAIs */
1067c46302ecSVinod Koul 	stream = &cdns->pcm;
1068c46302ecSVinod Koul 
1069cf924962SBard Liao 	ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
107063a6aa96SPierre-Louis Bossart 			       off, stream->num_ch_in);
1071c46302ecSVinod Koul 	if (ret)
1072c46302ecSVinod Koul 		return ret;
1073c46302ecSVinod Koul 
1074c46302ecSVinod Koul 	off += cdns->pcm.num_in;
10751215daeeSVinod Koul 	ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
107663a6aa96SPierre-Louis Bossart 			       off, stream->num_ch_out);
1077c46302ecSVinod Koul 	if (ret)
1078c46302ecSVinod Koul 		return ret;
1079c46302ecSVinod Koul 
1080c46302ecSVinod Koul 	off += cdns->pcm.num_out;
10811215daeeSVinod Koul 	ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
108263a6aa96SPierre-Louis Bossart 			       off, stream->num_ch_bd);
1083c46302ecSVinod Koul 	if (ret)
1084c46302ecSVinod Koul 		return ret;
1085c46302ecSVinod Koul 
108654f391ddSPierre-Louis Bossart 	return devm_snd_soc_register_component(cdns->dev, &dai_component,
1087c46302ecSVinod Koul 					       dais, num_dai);
1088c46302ecSVinod Koul }
1089c46302ecSVinod Koul 
1090503ae285SPierre-Louis Bossart 
1091b3ad31f3SPierre-Louis Bossart const struct sdw_intel_hw_ops sdw_intel_cnl_hw_ops = {
1092fb2dc6a0SPierre-Louis Bossart 	.debugfs_init = intel_debugfs_init,
1093fb2dc6a0SPierre-Louis Bossart 	.debugfs_exit = intel_debugfs_exit,
1094fb2dc6a0SPierre-Louis Bossart 
1095b6234bccSPierre-Louis Bossart 	.register_dai = intel_register_dai,
1096b6234bccSPierre-Louis Bossart 
10973db0c5a6SPierre-Louis Bossart 	.check_clock_stop = intel_check_clock_stop,
10983db0c5a6SPierre-Louis Bossart 	.start_bus = intel_start_bus,
10993db0c5a6SPierre-Louis Bossart 	.start_bus_after_reset = intel_start_bus_after_reset,
11003db0c5a6SPierre-Louis Bossart 	.start_bus_after_clock_stop = intel_start_bus_after_clock_stop,
11013db0c5a6SPierre-Louis Bossart 	.stop_bus = intel_stop_bus,
11023db0c5a6SPierre-Louis Bossart 
110349c9ff45SPierre-Louis Bossart 	.link_power_up = intel_link_power_up,
110449c9ff45SPierre-Louis Bossart 	.link_power_down = intel_link_power_down,
110549c9ff45SPierre-Louis Bossart 
110636e3b385SPierre-Louis Bossart 	.shim_check_wake = intel_shim_check_wake,
110736e3b385SPierre-Louis Bossart 	.shim_wake = intel_shim_wake,
110836e3b385SPierre-Louis Bossart 
1109b3ad31f3SPierre-Louis Bossart 	.pre_bank_switch = intel_pre_bank_switch,
1110b3ad31f3SPierre-Louis Bossart 	.post_bank_switch = intel_post_bank_switch,
111184706e9aSPierre-Louis Bossart 
111284706e9aSPierre-Louis Bossart 	.sync_arm = intel_shim_sync_arm,
111384706e9aSPierre-Louis Bossart 	.sync_go_unlocked = intel_shim_sync_go_unlocked,
111484706e9aSPierre-Louis Bossart 	.sync_go = intel_shim_sync_go,
11151e76de2eSPierre-Louis Bossart 	.sync_check_cmdsync_unlocked = intel_check_cmdsync_unlocked,
1116b3ad31f3SPierre-Louis Bossart };
1117b3ad31f3SPierre-Louis Bossart EXPORT_SYMBOL_NS(sdw_intel_cnl_hw_ops, SOUNDWIRE_INTEL);
1118