xref: /openbmc/linux/drivers/soundwire/amd_manager.c (revision c1e01cdbe0312d95b8c1542abd67fe786b534f57)
1d8f48fbdSVijendar Mukunda // SPDX-License-Identifier: GPL-2.0+
2d8f48fbdSVijendar Mukunda /*
3d8f48fbdSVijendar Mukunda  * SoundWire AMD Manager driver
4d8f48fbdSVijendar Mukunda  *
5d8f48fbdSVijendar Mukunda  * Copyright 2023 Advanced Micro Devices, Inc.
6d8f48fbdSVijendar Mukunda  */
7d8f48fbdSVijendar Mukunda 
8d8f48fbdSVijendar Mukunda #include <linux/completion.h>
9d8f48fbdSVijendar Mukunda #include <linux/device.h>
10d8f48fbdSVijendar Mukunda #include <linux/io.h>
11d8f48fbdSVijendar Mukunda #include <linux/jiffies.h>
12d8f48fbdSVijendar Mukunda #include <linux/kernel.h>
13d8f48fbdSVijendar Mukunda #include <linux/module.h>
14d8f48fbdSVijendar Mukunda #include <linux/slab.h>
15d8f48fbdSVijendar Mukunda #include <linux/soundwire/sdw.h>
16d8f48fbdSVijendar Mukunda #include <linux/soundwire/sdw_registers.h>
1781ff58ffSVijendar Mukunda #include <linux/pm_runtime.h>
18d8f48fbdSVijendar Mukunda #include <linux/wait.h>
19d8f48fbdSVijendar Mukunda #include <sound/pcm_params.h>
20d8f48fbdSVijendar Mukunda #include <sound/soc.h>
21d8f48fbdSVijendar Mukunda #include "bus.h"
22d8f48fbdSVijendar Mukunda #include "amd_manager.h"
23d8f48fbdSVijendar Mukunda 
24d8f48fbdSVijendar Mukunda #define DRV_NAME "amd_sdw_manager"
25d8f48fbdSVijendar Mukunda 
26d8f48fbdSVijendar Mukunda #define to_amd_sdw(b)	container_of(b, struct amd_sdw_manager, bus)
27d8f48fbdSVijendar Mukunda 
amd_enable_sdw_pads(struct amd_sdw_manager * amd_manager)28d8f48fbdSVijendar Mukunda static void amd_enable_sdw_pads(struct amd_sdw_manager *amd_manager)
29d8f48fbdSVijendar Mukunda {
30d8f48fbdSVijendar Mukunda 	u32 sw_pad_pulldown_val;
31d8f48fbdSVijendar Mukunda 	u32 val;
32d8f48fbdSVijendar Mukunda 
33d8f48fbdSVijendar Mukunda 	mutex_lock(amd_manager->acp_sdw_lock);
34d8f48fbdSVijendar Mukunda 	val = readl(amd_manager->acp_mmio + ACP_SW_PAD_KEEPER_EN);
35d8f48fbdSVijendar Mukunda 	val |= amd_manager->reg_mask->sw_pad_enable_mask;
36d8f48fbdSVijendar Mukunda 	writel(val, amd_manager->acp_mmio + ACP_SW_PAD_KEEPER_EN);
37d8f48fbdSVijendar Mukunda 	usleep_range(1000, 1500);
38d8f48fbdSVijendar Mukunda 
39d8f48fbdSVijendar Mukunda 	sw_pad_pulldown_val = readl(amd_manager->acp_mmio + ACP_PAD_PULLDOWN_CTRL);
40d8f48fbdSVijendar Mukunda 	sw_pad_pulldown_val &= amd_manager->reg_mask->sw_pad_pulldown_mask;
41d8f48fbdSVijendar Mukunda 	writel(sw_pad_pulldown_val, amd_manager->acp_mmio + ACP_PAD_PULLDOWN_CTRL);
42d8f48fbdSVijendar Mukunda 	mutex_unlock(amd_manager->acp_sdw_lock);
43d8f48fbdSVijendar Mukunda }
44d8f48fbdSVijendar Mukunda 
amd_init_sdw_manager(struct amd_sdw_manager * amd_manager)45d8f48fbdSVijendar Mukunda static int amd_init_sdw_manager(struct amd_sdw_manager *amd_manager)
46d8f48fbdSVijendar Mukunda {
47d8f48fbdSVijendar Mukunda 	u32 val;
48d8f48fbdSVijendar Mukunda 	int ret;
49d8f48fbdSVijendar Mukunda 
50d8f48fbdSVijendar Mukunda 	writel(AMD_SDW_ENABLE, amd_manager->mmio + ACP_SW_EN);
51d8f48fbdSVijendar Mukunda 	ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_EN_STATUS, val, val, ACP_DELAY_US,
52d8f48fbdSVijendar Mukunda 				 AMD_SDW_TIMEOUT);
53d8f48fbdSVijendar Mukunda 	if (ret)
54d8f48fbdSVijendar Mukunda 		return ret;
55d8f48fbdSVijendar Mukunda 
56d8f48fbdSVijendar Mukunda 	/* SoundWire manager bus reset */
57d8f48fbdSVijendar Mukunda 	writel(AMD_SDW_BUS_RESET_REQ, amd_manager->mmio + ACP_SW_BUS_RESET_CTRL);
58d8f48fbdSVijendar Mukunda 	ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_BUS_RESET_CTRL, val,
59d8f48fbdSVijendar Mukunda 				 (val & AMD_SDW_BUS_RESET_DONE), ACP_DELAY_US, AMD_SDW_TIMEOUT);
60d8f48fbdSVijendar Mukunda 	if (ret)
61d8f48fbdSVijendar Mukunda 		return ret;
62d8f48fbdSVijendar Mukunda 
63d8f48fbdSVijendar Mukunda 	writel(AMD_SDW_BUS_RESET_CLEAR_REQ, amd_manager->mmio + ACP_SW_BUS_RESET_CTRL);
64d8f48fbdSVijendar Mukunda 	ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_BUS_RESET_CTRL, val, !val,
65d8f48fbdSVijendar Mukunda 				 ACP_DELAY_US, AMD_SDW_TIMEOUT);
66d8f48fbdSVijendar Mukunda 	if (ret) {
67d8f48fbdSVijendar Mukunda 		dev_err(amd_manager->dev, "Failed to reset SoundWire manager instance%d\n",
68d8f48fbdSVijendar Mukunda 			amd_manager->instance);
69d8f48fbdSVijendar Mukunda 		return ret;
70d8f48fbdSVijendar Mukunda 	}
71d8f48fbdSVijendar Mukunda 
72d8f48fbdSVijendar Mukunda 	writel(AMD_SDW_DISABLE, amd_manager->mmio + ACP_SW_EN);
73d8f48fbdSVijendar Mukunda 	return readl_poll_timeout(amd_manager->mmio + ACP_SW_EN_STATUS, val, !val, ACP_DELAY_US,
74d8f48fbdSVijendar Mukunda 				  AMD_SDW_TIMEOUT);
75d8f48fbdSVijendar Mukunda }
76d8f48fbdSVijendar Mukunda 
amd_enable_sdw_manager(struct amd_sdw_manager * amd_manager)77d8f48fbdSVijendar Mukunda static int amd_enable_sdw_manager(struct amd_sdw_manager *amd_manager)
78d8f48fbdSVijendar Mukunda {
79d8f48fbdSVijendar Mukunda 	u32 val;
80d8f48fbdSVijendar Mukunda 
81d8f48fbdSVijendar Mukunda 	writel(AMD_SDW_ENABLE, amd_manager->mmio + ACP_SW_EN);
82d8f48fbdSVijendar Mukunda 	return readl_poll_timeout(amd_manager->mmio + ACP_SW_EN_STATUS, val, val, ACP_DELAY_US,
83d8f48fbdSVijendar Mukunda 				  AMD_SDW_TIMEOUT);
84d8f48fbdSVijendar Mukunda }
85d8f48fbdSVijendar Mukunda 
amd_disable_sdw_manager(struct amd_sdw_manager * amd_manager)86d8f48fbdSVijendar Mukunda static int amd_disable_sdw_manager(struct amd_sdw_manager *amd_manager)
87d8f48fbdSVijendar Mukunda {
88d8f48fbdSVijendar Mukunda 	u32 val;
89d8f48fbdSVijendar Mukunda 
90d8f48fbdSVijendar Mukunda 	writel(AMD_SDW_DISABLE, amd_manager->mmio + ACP_SW_EN);
91d8f48fbdSVijendar Mukunda 	/*
92d8f48fbdSVijendar Mukunda 	 * After invoking manager disable sequence, check whether
93d8f48fbdSVijendar Mukunda 	 * manager has executed clock stop sequence. In this case,
94d8f48fbdSVijendar Mukunda 	 * manager should ignore checking enable status register.
95d8f48fbdSVijendar Mukunda 	 */
96d8f48fbdSVijendar Mukunda 	val = readl(amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL);
97d8f48fbdSVijendar Mukunda 	if (val)
98d8f48fbdSVijendar Mukunda 		return 0;
99d8f48fbdSVijendar Mukunda 	return readl_poll_timeout(amd_manager->mmio + ACP_SW_EN_STATUS, val, !val, ACP_DELAY_US,
100d8f48fbdSVijendar Mukunda 				  AMD_SDW_TIMEOUT);
101d8f48fbdSVijendar Mukunda }
102d8f48fbdSVijendar Mukunda 
amd_enable_sdw_interrupts(struct amd_sdw_manager * amd_manager)103d8f48fbdSVijendar Mukunda static void amd_enable_sdw_interrupts(struct amd_sdw_manager *amd_manager)
104d8f48fbdSVijendar Mukunda {
105d8f48fbdSVijendar Mukunda 	struct sdw_manager_reg_mask *reg_mask = amd_manager->reg_mask;
106d8f48fbdSVijendar Mukunda 	u32 val;
107d8f48fbdSVijendar Mukunda 
108d8f48fbdSVijendar Mukunda 	mutex_lock(amd_manager->acp_sdw_lock);
109d8f48fbdSVijendar Mukunda 	val = readl(amd_manager->acp_mmio + ACP_EXTERNAL_INTR_CNTL(amd_manager->instance));
110d8f48fbdSVijendar Mukunda 	val |= reg_mask->acp_sdw_intr_mask;
111d8f48fbdSVijendar Mukunda 	writel(val, amd_manager->acp_mmio + ACP_EXTERNAL_INTR_CNTL(amd_manager->instance));
112d8f48fbdSVijendar Mukunda 	mutex_unlock(amd_manager->acp_sdw_lock);
113d8f48fbdSVijendar Mukunda 
114d8f48fbdSVijendar Mukunda 	writel(AMD_SDW_IRQ_MASK_0TO7, amd_manager->mmio +
115d8f48fbdSVijendar Mukunda 		       ACP_SW_STATE_CHANGE_STATUS_MASK_0TO7);
116d8f48fbdSVijendar Mukunda 	writel(AMD_SDW_IRQ_MASK_8TO11, amd_manager->mmio +
117d8f48fbdSVijendar Mukunda 		       ACP_SW_STATE_CHANGE_STATUS_MASK_8TO11);
118d8f48fbdSVijendar Mukunda 	writel(AMD_SDW_IRQ_ERROR_MASK, amd_manager->mmio + ACP_SW_ERROR_INTR_MASK);
119d8f48fbdSVijendar Mukunda }
120d8f48fbdSVijendar Mukunda 
amd_disable_sdw_interrupts(struct amd_sdw_manager * amd_manager)121d8f48fbdSVijendar Mukunda static void amd_disable_sdw_interrupts(struct amd_sdw_manager *amd_manager)
122d8f48fbdSVijendar Mukunda {
123d8f48fbdSVijendar Mukunda 	struct sdw_manager_reg_mask *reg_mask = amd_manager->reg_mask;
124d8f48fbdSVijendar Mukunda 	u32 val;
125d8f48fbdSVijendar Mukunda 
126d8f48fbdSVijendar Mukunda 	mutex_lock(amd_manager->acp_sdw_lock);
127d8f48fbdSVijendar Mukunda 	val = readl(amd_manager->acp_mmio + ACP_EXTERNAL_INTR_CNTL(amd_manager->instance));
128d8f48fbdSVijendar Mukunda 	val &= ~reg_mask->acp_sdw_intr_mask;
129d8f48fbdSVijendar Mukunda 	writel(val, amd_manager->acp_mmio + ACP_EXTERNAL_INTR_CNTL(amd_manager->instance));
130d8f48fbdSVijendar Mukunda 	mutex_unlock(amd_manager->acp_sdw_lock);
131d8f48fbdSVijendar Mukunda 
132d8f48fbdSVijendar Mukunda 	writel(0x00, amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_MASK_0TO7);
133d8f48fbdSVijendar Mukunda 	writel(0x00, amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_MASK_8TO11);
134d8f48fbdSVijendar Mukunda 	writel(0x00, amd_manager->mmio + ACP_SW_ERROR_INTR_MASK);
135d8f48fbdSVijendar Mukunda }
136d8f48fbdSVijendar Mukunda 
amd_deinit_sdw_manager(struct amd_sdw_manager * amd_manager)13781ff58ffSVijendar Mukunda static int amd_deinit_sdw_manager(struct amd_sdw_manager *amd_manager)
13881ff58ffSVijendar Mukunda {
13981ff58ffSVijendar Mukunda 	amd_disable_sdw_interrupts(amd_manager);
14081ff58ffSVijendar Mukunda 	return amd_disable_sdw_manager(amd_manager);
14181ff58ffSVijendar Mukunda }
14281ff58ffSVijendar Mukunda 
amd_sdw_set_frameshape(struct amd_sdw_manager * amd_manager)143d8f48fbdSVijendar Mukunda static void amd_sdw_set_frameshape(struct amd_sdw_manager *amd_manager)
144d8f48fbdSVijendar Mukunda {
145d8f48fbdSVijendar Mukunda 	u32 frame_size;
146d8f48fbdSVijendar Mukunda 
147d8f48fbdSVijendar Mukunda 	frame_size = (amd_manager->rows_index << 3) | amd_manager->cols_index;
148d8f48fbdSVijendar Mukunda 	writel(frame_size, amd_manager->mmio + ACP_SW_FRAMESIZE);
149d8f48fbdSVijendar Mukunda }
150d8f48fbdSVijendar Mukunda 
amd_sdw_wake_enable(struct amd_sdw_manager * amd_manager,bool enable)151*935d4c7cSVijendar Mukunda static void amd_sdw_wake_enable(struct amd_sdw_manager *amd_manager, bool enable)
152*935d4c7cSVijendar Mukunda {
153*935d4c7cSVijendar Mukunda 	u32 wake_ctrl;
154*935d4c7cSVijendar Mukunda 
155*935d4c7cSVijendar Mukunda 	wake_ctrl = readl(amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_MASK_8TO11);
156*935d4c7cSVijendar Mukunda 	if (enable)
157*935d4c7cSVijendar Mukunda 		wake_ctrl |= AMD_SDW_WAKE_INTR_MASK;
158*935d4c7cSVijendar Mukunda 	else
159*935d4c7cSVijendar Mukunda 		wake_ctrl &= ~AMD_SDW_WAKE_INTR_MASK;
160*935d4c7cSVijendar Mukunda 
161*935d4c7cSVijendar Mukunda 	writel(wake_ctrl, amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_MASK_8TO11);
162*935d4c7cSVijendar Mukunda }
163*935d4c7cSVijendar Mukunda 
amd_sdw_ctl_word_prep(u32 * lower_word,u32 * upper_word,struct sdw_msg * msg,int cmd_offset)164d8f48fbdSVijendar Mukunda static void amd_sdw_ctl_word_prep(u32 *lower_word, u32 *upper_word, struct sdw_msg *msg,
165d8f48fbdSVijendar Mukunda 				  int cmd_offset)
166d8f48fbdSVijendar Mukunda {
167d8f48fbdSVijendar Mukunda 	u32 upper_data;
168d8f48fbdSVijendar Mukunda 	u32 lower_data = 0;
169d8f48fbdSVijendar Mukunda 	u16 addr;
170d8f48fbdSVijendar Mukunda 	u8 upper_addr, lower_addr;
171d8f48fbdSVijendar Mukunda 	u8 data = 0;
172d8f48fbdSVijendar Mukunda 
173d8f48fbdSVijendar Mukunda 	addr = msg->addr + cmd_offset;
174d8f48fbdSVijendar Mukunda 	upper_addr = (addr & 0xFF00) >> 8;
175d8f48fbdSVijendar Mukunda 	lower_addr = addr & 0xFF;
176d8f48fbdSVijendar Mukunda 
177d8f48fbdSVijendar Mukunda 	if (msg->flags == SDW_MSG_FLAG_WRITE)
178d8f48fbdSVijendar Mukunda 		data = msg->buf[cmd_offset];
179d8f48fbdSVijendar Mukunda 
180d8f48fbdSVijendar Mukunda 	upper_data = FIELD_PREP(AMD_SDW_MCP_CMD_DEV_ADDR, msg->dev_num);
181d8f48fbdSVijendar Mukunda 	upper_data |= FIELD_PREP(AMD_SDW_MCP_CMD_COMMAND, msg->flags + 2);
182d8f48fbdSVijendar Mukunda 	upper_data |= FIELD_PREP(AMD_SDW_MCP_CMD_REG_ADDR_HIGH, upper_addr);
183d8f48fbdSVijendar Mukunda 	lower_data |= FIELD_PREP(AMD_SDW_MCP_CMD_REG_ADDR_LOW, lower_addr);
184d8f48fbdSVijendar Mukunda 	lower_data |= FIELD_PREP(AMD_SDW_MCP_CMD_REG_DATA, data);
185d8f48fbdSVijendar Mukunda 
186d8f48fbdSVijendar Mukunda 	*upper_word = upper_data;
187d8f48fbdSVijendar Mukunda 	*lower_word = lower_data;
188d8f48fbdSVijendar Mukunda }
189d8f48fbdSVijendar Mukunda 
amd_sdw_send_cmd_get_resp(struct amd_sdw_manager * amd_manager,u32 lower_data,u32 upper_data)190d8f48fbdSVijendar Mukunda static u64 amd_sdw_send_cmd_get_resp(struct amd_sdw_manager *amd_manager, u32 lower_data,
191d8f48fbdSVijendar Mukunda 				     u32 upper_data)
192d8f48fbdSVijendar Mukunda {
193d8f48fbdSVijendar Mukunda 	u64 resp;
194d8f48fbdSVijendar Mukunda 	u32 lower_resp, upper_resp;
195d8f48fbdSVijendar Mukunda 	u32 sts;
196d8f48fbdSVijendar Mukunda 	int ret;
197d8f48fbdSVijendar Mukunda 
198d8f48fbdSVijendar Mukunda 	ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_IMM_CMD_STS, sts,
199d8f48fbdSVijendar Mukunda 				 !(sts & AMD_SDW_IMM_CMD_BUSY), ACP_DELAY_US, AMD_SDW_TIMEOUT);
200d8f48fbdSVijendar Mukunda 	if (ret) {
201d8f48fbdSVijendar Mukunda 		dev_err(amd_manager->dev, "SDW%x previous cmd status clear failed\n",
202d8f48fbdSVijendar Mukunda 			amd_manager->instance);
203d8f48fbdSVijendar Mukunda 		return ret;
204d8f48fbdSVijendar Mukunda 	}
205d8f48fbdSVijendar Mukunda 
206d8f48fbdSVijendar Mukunda 	if (sts & AMD_SDW_IMM_RES_VALID) {
207d8f48fbdSVijendar Mukunda 		dev_err(amd_manager->dev, "SDW%x manager is in bad state\n", amd_manager->instance);
208d8f48fbdSVijendar Mukunda 		writel(0x00, amd_manager->mmio + ACP_SW_IMM_CMD_STS);
209d8f48fbdSVijendar Mukunda 	}
210d8f48fbdSVijendar Mukunda 	writel(upper_data, amd_manager->mmio + ACP_SW_IMM_CMD_UPPER_WORD);
211d8f48fbdSVijendar Mukunda 	writel(lower_data, amd_manager->mmio + ACP_SW_IMM_CMD_LOWER_QWORD);
212d8f48fbdSVijendar Mukunda 
213d8f48fbdSVijendar Mukunda 	ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_IMM_CMD_STS, sts,
214d8f48fbdSVijendar Mukunda 				 (sts & AMD_SDW_IMM_RES_VALID), ACP_DELAY_US, AMD_SDW_TIMEOUT);
215d8f48fbdSVijendar Mukunda 	if (ret) {
216d8f48fbdSVijendar Mukunda 		dev_err(amd_manager->dev, "SDW%x cmd response timeout occurred\n",
217d8f48fbdSVijendar Mukunda 			amd_manager->instance);
218d8f48fbdSVijendar Mukunda 		return ret;
219d8f48fbdSVijendar Mukunda 	}
220d8f48fbdSVijendar Mukunda 	upper_resp = readl(amd_manager->mmio + ACP_SW_IMM_RESP_UPPER_WORD);
221d8f48fbdSVijendar Mukunda 	lower_resp = readl(amd_manager->mmio + ACP_SW_IMM_RESP_LOWER_QWORD);
222d8f48fbdSVijendar Mukunda 
223d8f48fbdSVijendar Mukunda 	writel(AMD_SDW_IMM_RES_VALID, amd_manager->mmio + ACP_SW_IMM_CMD_STS);
224d8f48fbdSVijendar Mukunda 	ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_IMM_CMD_STS, sts,
225d8f48fbdSVijendar Mukunda 				 !(sts & AMD_SDW_IMM_RES_VALID), ACP_DELAY_US, AMD_SDW_TIMEOUT);
226d8f48fbdSVijendar Mukunda 	if (ret) {
227d8f48fbdSVijendar Mukunda 		dev_err(amd_manager->dev, "SDW%x cmd status retry failed\n",
228d8f48fbdSVijendar Mukunda 			amd_manager->instance);
229d8f48fbdSVijendar Mukunda 		return ret;
230d8f48fbdSVijendar Mukunda 	}
231d8f48fbdSVijendar Mukunda 	resp = upper_resp;
232d8f48fbdSVijendar Mukunda 	resp = (resp << 32) | lower_resp;
233d8f48fbdSVijendar Mukunda 	return resp;
234d8f48fbdSVijendar Mukunda }
235d8f48fbdSVijendar Mukunda 
236d8f48fbdSVijendar Mukunda static enum sdw_command_response
amd_program_scp_addr(struct amd_sdw_manager * amd_manager,struct sdw_msg * msg)237d8f48fbdSVijendar Mukunda amd_program_scp_addr(struct amd_sdw_manager *amd_manager, struct sdw_msg *msg)
238d8f48fbdSVijendar Mukunda {
239d8f48fbdSVijendar Mukunda 	struct sdw_msg scp_msg = {0};
240d8f48fbdSVijendar Mukunda 	u64 response_buf[2] = {0};
241d8f48fbdSVijendar Mukunda 	u32 upper_data = 0, lower_data = 0;
242d8f48fbdSVijendar Mukunda 	int index;
243d8f48fbdSVijendar Mukunda 
244d8f48fbdSVijendar Mukunda 	scp_msg.dev_num = msg->dev_num;
245d8f48fbdSVijendar Mukunda 	scp_msg.addr = SDW_SCP_ADDRPAGE1;
246d8f48fbdSVijendar Mukunda 	scp_msg.buf = &msg->addr_page1;
247d8f48fbdSVijendar Mukunda 	scp_msg.flags = SDW_MSG_FLAG_WRITE;
248d8f48fbdSVijendar Mukunda 	amd_sdw_ctl_word_prep(&lower_data, &upper_data, &scp_msg, 0);
249d8f48fbdSVijendar Mukunda 	response_buf[0] = amd_sdw_send_cmd_get_resp(amd_manager, lower_data, upper_data);
250d8f48fbdSVijendar Mukunda 	scp_msg.addr = SDW_SCP_ADDRPAGE2;
251d8f48fbdSVijendar Mukunda 	scp_msg.buf = &msg->addr_page2;
252d8f48fbdSVijendar Mukunda 	amd_sdw_ctl_word_prep(&lower_data, &upper_data, &scp_msg, 0);
253d8f48fbdSVijendar Mukunda 	response_buf[1] = amd_sdw_send_cmd_get_resp(amd_manager, lower_data, upper_data);
254d8f48fbdSVijendar Mukunda 
255d8f48fbdSVijendar Mukunda 	for (index = 0; index < 2; index++) {
256d8f48fbdSVijendar Mukunda 		if (response_buf[index] == -ETIMEDOUT) {
257d8f48fbdSVijendar Mukunda 			dev_err_ratelimited(amd_manager->dev,
258d8f48fbdSVijendar Mukunda 					    "SCP_addrpage command timeout for Slave %d\n",
259d8f48fbdSVijendar Mukunda 					    msg->dev_num);
260d8f48fbdSVijendar Mukunda 			return SDW_CMD_TIMEOUT;
261d8f48fbdSVijendar Mukunda 		} else if (!(response_buf[index] & AMD_SDW_MCP_RESP_ACK)) {
262d8f48fbdSVijendar Mukunda 			if (response_buf[index] & AMD_SDW_MCP_RESP_NACK) {
263d8f48fbdSVijendar Mukunda 				dev_err_ratelimited(amd_manager->dev,
264d8f48fbdSVijendar Mukunda 						    "SCP_addrpage NACKed for Slave %d\n",
265d8f48fbdSVijendar Mukunda 						    msg->dev_num);
266d8f48fbdSVijendar Mukunda 				return SDW_CMD_FAIL;
267d8f48fbdSVijendar Mukunda 			}
268d8f48fbdSVijendar Mukunda 			dev_dbg_ratelimited(amd_manager->dev, "SCP_addrpage ignored for Slave %d\n",
269d8f48fbdSVijendar Mukunda 					    msg->dev_num);
270d8f48fbdSVijendar Mukunda 			return SDW_CMD_IGNORED;
271d8f48fbdSVijendar Mukunda 		}
272d8f48fbdSVijendar Mukunda 	}
273d8f48fbdSVijendar Mukunda 	return SDW_CMD_OK;
274d8f48fbdSVijendar Mukunda }
275d8f48fbdSVijendar Mukunda 
amd_prep_msg(struct amd_sdw_manager * amd_manager,struct sdw_msg * msg)276d8f48fbdSVijendar Mukunda static int amd_prep_msg(struct amd_sdw_manager *amd_manager, struct sdw_msg *msg)
277d8f48fbdSVijendar Mukunda {
278d8f48fbdSVijendar Mukunda 	int ret;
279d8f48fbdSVijendar Mukunda 
280d8f48fbdSVijendar Mukunda 	if (msg->page) {
281d8f48fbdSVijendar Mukunda 		ret = amd_program_scp_addr(amd_manager, msg);
282d8f48fbdSVijendar Mukunda 		if (ret) {
283d8f48fbdSVijendar Mukunda 			msg->len = 0;
284d8f48fbdSVijendar Mukunda 			return ret;
285d8f48fbdSVijendar Mukunda 		}
286d8f48fbdSVijendar Mukunda 	}
287d8f48fbdSVijendar Mukunda 	switch (msg->flags) {
288d8f48fbdSVijendar Mukunda 	case SDW_MSG_FLAG_READ:
289d8f48fbdSVijendar Mukunda 	case SDW_MSG_FLAG_WRITE:
290d8f48fbdSVijendar Mukunda 		break;
291d8f48fbdSVijendar Mukunda 	default:
292d8f48fbdSVijendar Mukunda 		dev_err(amd_manager->dev, "Invalid msg cmd: %d\n", msg->flags);
293d8f48fbdSVijendar Mukunda 		return -EINVAL;
294d8f48fbdSVijendar Mukunda 	}
295d8f48fbdSVijendar Mukunda 	return 0;
296d8f48fbdSVijendar Mukunda }
297d8f48fbdSVijendar Mukunda 
amd_sdw_fill_msg_resp(struct amd_sdw_manager * amd_manager,struct sdw_msg * msg,u64 response,int offset)298d8f48fbdSVijendar Mukunda static enum sdw_command_response amd_sdw_fill_msg_resp(struct amd_sdw_manager *amd_manager,
299d8f48fbdSVijendar Mukunda 						       struct sdw_msg *msg, u64 response,
300d8f48fbdSVijendar Mukunda 						       int offset)
301d8f48fbdSVijendar Mukunda {
302d8f48fbdSVijendar Mukunda 	if (response & AMD_SDW_MCP_RESP_ACK) {
303d8f48fbdSVijendar Mukunda 		if (msg->flags == SDW_MSG_FLAG_READ)
304d8f48fbdSVijendar Mukunda 			msg->buf[offset] = FIELD_GET(AMD_SDW_MCP_RESP_RDATA, response);
305d8f48fbdSVijendar Mukunda 	} else {
306d8f48fbdSVijendar Mukunda 		if (response == -ETIMEDOUT) {
307d8f48fbdSVijendar Mukunda 			dev_err_ratelimited(amd_manager->dev, "command timeout for Slave %d\n",
308d8f48fbdSVijendar Mukunda 					    msg->dev_num);
309d8f48fbdSVijendar Mukunda 			return SDW_CMD_TIMEOUT;
310d8f48fbdSVijendar Mukunda 		} else if (response & AMD_SDW_MCP_RESP_NACK) {
311d8f48fbdSVijendar Mukunda 			dev_err_ratelimited(amd_manager->dev,
312d8f48fbdSVijendar Mukunda 					    "command response NACK received for Slave %d\n",
313d8f48fbdSVijendar Mukunda 					    msg->dev_num);
314d8f48fbdSVijendar Mukunda 			return SDW_CMD_FAIL;
315d8f48fbdSVijendar Mukunda 		}
316d8f48fbdSVijendar Mukunda 		dev_err_ratelimited(amd_manager->dev, "command is ignored for Slave %d\n",
317d8f48fbdSVijendar Mukunda 				    msg->dev_num);
318d8f48fbdSVijendar Mukunda 		return SDW_CMD_IGNORED;
319d8f48fbdSVijendar Mukunda 	}
320d8f48fbdSVijendar Mukunda 	return SDW_CMD_OK;
321d8f48fbdSVijendar Mukunda }
322d8f48fbdSVijendar Mukunda 
_amd_sdw_xfer_msg(struct amd_sdw_manager * amd_manager,struct sdw_msg * msg,int cmd_offset)323d8f48fbdSVijendar Mukunda static unsigned int _amd_sdw_xfer_msg(struct amd_sdw_manager *amd_manager, struct sdw_msg *msg,
324d8f48fbdSVijendar Mukunda 				      int cmd_offset)
325d8f48fbdSVijendar Mukunda {
326d8f48fbdSVijendar Mukunda 	u64 response;
327d8f48fbdSVijendar Mukunda 	u32 upper_data = 0, lower_data = 0;
328d8f48fbdSVijendar Mukunda 
329d8f48fbdSVijendar Mukunda 	amd_sdw_ctl_word_prep(&lower_data, &upper_data, msg, cmd_offset);
330d8f48fbdSVijendar Mukunda 	response = amd_sdw_send_cmd_get_resp(amd_manager, lower_data, upper_data);
331d8f48fbdSVijendar Mukunda 	return amd_sdw_fill_msg_resp(amd_manager, msg, response, cmd_offset);
332d8f48fbdSVijendar Mukunda }
333d8f48fbdSVijendar Mukunda 
amd_sdw_xfer_msg(struct sdw_bus * bus,struct sdw_msg * msg)334d8f48fbdSVijendar Mukunda static enum sdw_command_response amd_sdw_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
335d8f48fbdSVijendar Mukunda {
336d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = to_amd_sdw(bus);
337d8f48fbdSVijendar Mukunda 	int ret, i;
338d8f48fbdSVijendar Mukunda 
339d8f48fbdSVijendar Mukunda 	ret = amd_prep_msg(amd_manager, msg);
340d8f48fbdSVijendar Mukunda 	if (ret)
341d8f48fbdSVijendar Mukunda 		return SDW_CMD_FAIL_OTHER;
342d8f48fbdSVijendar Mukunda 	for (i = 0; i < msg->len; i++) {
343d8f48fbdSVijendar Mukunda 		ret = _amd_sdw_xfer_msg(amd_manager, msg, i);
344d8f48fbdSVijendar Mukunda 		if (ret)
345d8f48fbdSVijendar Mukunda 			return ret;
346d8f48fbdSVijendar Mukunda 	}
347d8f48fbdSVijendar Mukunda 	return SDW_CMD_OK;
348d8f48fbdSVijendar Mukunda }
349d8f48fbdSVijendar Mukunda 
amd_sdw_fill_slave_status(struct amd_sdw_manager * amd_manager,u16 index,u32 status)35065f93e40SVijendar Mukunda static void amd_sdw_fill_slave_status(struct amd_sdw_manager *amd_manager, u16 index, u32 status)
35165f93e40SVijendar Mukunda {
35265f93e40SVijendar Mukunda 	switch (status) {
35365f93e40SVijendar Mukunda 	case SDW_SLAVE_ATTACHED:
35465f93e40SVijendar Mukunda 	case SDW_SLAVE_UNATTACHED:
35565f93e40SVijendar Mukunda 	case SDW_SLAVE_ALERT:
35665f93e40SVijendar Mukunda 		amd_manager->status[index] = status;
35765f93e40SVijendar Mukunda 		break;
35865f93e40SVijendar Mukunda 	default:
35965f93e40SVijendar Mukunda 		amd_manager->status[index] = SDW_SLAVE_RESERVED;
36065f93e40SVijendar Mukunda 		break;
36165f93e40SVijendar Mukunda 	}
36265f93e40SVijendar Mukunda }
36365f93e40SVijendar Mukunda 
amd_sdw_process_ping_status(u64 response,struct amd_sdw_manager * amd_manager)36465f93e40SVijendar Mukunda static void amd_sdw_process_ping_status(u64 response, struct amd_sdw_manager *amd_manager)
36565f93e40SVijendar Mukunda {
36665f93e40SVijendar Mukunda 	u64 slave_stat;
36765f93e40SVijendar Mukunda 	u32 val;
36865f93e40SVijendar Mukunda 	u16 dev_index;
36965f93e40SVijendar Mukunda 
37065f93e40SVijendar Mukunda 	/* slave status response */
37165f93e40SVijendar Mukunda 	slave_stat = FIELD_GET(AMD_SDW_MCP_SLAVE_STAT_0_3, response);
37265f93e40SVijendar Mukunda 	slave_stat |= FIELD_GET(AMD_SDW_MCP_SLAVE_STAT_4_11, response) << 8;
37365f93e40SVijendar Mukunda 	dev_dbg(amd_manager->dev, "slave_stat:0x%llx\n", slave_stat);
37465f93e40SVijendar Mukunda 	for (dev_index = 0; dev_index <= SDW_MAX_DEVICES; ++dev_index) {
37565f93e40SVijendar Mukunda 		val = (slave_stat >> (dev_index * 2)) & AMD_SDW_MCP_SLAVE_STATUS_MASK;
37665f93e40SVijendar Mukunda 		dev_dbg(amd_manager->dev, "val:0x%x\n", val);
37765f93e40SVijendar Mukunda 		amd_sdw_fill_slave_status(amd_manager, dev_index, val);
37865f93e40SVijendar Mukunda 	}
37965f93e40SVijendar Mukunda }
38065f93e40SVijendar Mukunda 
amd_sdw_read_and_process_ping_status(struct amd_sdw_manager * amd_manager)38165f93e40SVijendar Mukunda static void amd_sdw_read_and_process_ping_status(struct amd_sdw_manager *amd_manager)
38265f93e40SVijendar Mukunda {
38365f93e40SVijendar Mukunda 	u64 response;
38465f93e40SVijendar Mukunda 
38565f93e40SVijendar Mukunda 	mutex_lock(&amd_manager->bus.msg_lock);
38665f93e40SVijendar Mukunda 	response = amd_sdw_send_cmd_get_resp(amd_manager, 0, 0);
38765f93e40SVijendar Mukunda 	mutex_unlock(&amd_manager->bus.msg_lock);
38865f93e40SVijendar Mukunda 	amd_sdw_process_ping_status(response, amd_manager);
38965f93e40SVijendar Mukunda }
39065f93e40SVijendar Mukunda 
amd_sdw_read_ping_status(struct sdw_bus * bus)391d8f48fbdSVijendar Mukunda static u32 amd_sdw_read_ping_status(struct sdw_bus *bus)
392d8f48fbdSVijendar Mukunda {
393d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = to_amd_sdw(bus);
394d8f48fbdSVijendar Mukunda 	u64 response;
395d8f48fbdSVijendar Mukunda 	u32 slave_stat;
396d8f48fbdSVijendar Mukunda 
397d8f48fbdSVijendar Mukunda 	response = amd_sdw_send_cmd_get_resp(amd_manager, 0, 0);
398d8f48fbdSVijendar Mukunda 	/* slave status from ping response */
399d8f48fbdSVijendar Mukunda 	slave_stat = FIELD_GET(AMD_SDW_MCP_SLAVE_STAT_0_3, response);
400d8f48fbdSVijendar Mukunda 	slave_stat |= FIELD_GET(AMD_SDW_MCP_SLAVE_STAT_4_11, response) << 8;
401d8f48fbdSVijendar Mukunda 	dev_dbg(amd_manager->dev, "slave_stat:0x%x\n", slave_stat);
402d8f48fbdSVijendar Mukunda 	return slave_stat;
403d8f48fbdSVijendar Mukunda }
404d8f48fbdSVijendar Mukunda 
amd_sdw_compute_params(struct sdw_bus * bus)405d8f48fbdSVijendar Mukunda static int amd_sdw_compute_params(struct sdw_bus *bus)
406d8f48fbdSVijendar Mukunda {
407d8f48fbdSVijendar Mukunda 	struct sdw_transport_data t_data = {0};
408d8f48fbdSVijendar Mukunda 	struct sdw_master_runtime *m_rt;
409d8f48fbdSVijendar Mukunda 	struct sdw_port_runtime *p_rt;
410d8f48fbdSVijendar Mukunda 	struct sdw_bus_params *b_params = &bus->params;
411d8f48fbdSVijendar Mukunda 	int port_bo, hstart, hstop, sample_int;
412d8f48fbdSVijendar Mukunda 	unsigned int rate, bps;
413d8f48fbdSVijendar Mukunda 
414d8f48fbdSVijendar Mukunda 	port_bo = 0;
415d8f48fbdSVijendar Mukunda 	hstart = 1;
416d8f48fbdSVijendar Mukunda 	hstop = bus->params.col - 1;
417d8f48fbdSVijendar Mukunda 	t_data.hstop = hstop;
418d8f48fbdSVijendar Mukunda 	t_data.hstart = hstart;
419d8f48fbdSVijendar Mukunda 
420d8f48fbdSVijendar Mukunda 	list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
421d8f48fbdSVijendar Mukunda 		rate = m_rt->stream->params.rate;
422d8f48fbdSVijendar Mukunda 		bps = m_rt->stream->params.bps;
423d8f48fbdSVijendar Mukunda 		sample_int = (bus->params.curr_dr_freq / rate);
424d8f48fbdSVijendar Mukunda 		list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
425d8f48fbdSVijendar Mukunda 			port_bo = (p_rt->num * 64) + 1;
426d8f48fbdSVijendar Mukunda 			dev_dbg(bus->dev, "p_rt->num=%d hstart=%d hstop=%d port_bo=%d\n",
427d8f48fbdSVijendar Mukunda 				p_rt->num, hstart, hstop, port_bo);
428d8f48fbdSVijendar Mukunda 			sdw_fill_xport_params(&p_rt->transport_params, p_rt->num,
429d8f48fbdSVijendar Mukunda 					      false, SDW_BLK_GRP_CNT_1, sample_int,
430d8f48fbdSVijendar Mukunda 					      port_bo, port_bo >> 8, hstart, hstop,
431d8f48fbdSVijendar Mukunda 					      SDW_BLK_PKG_PER_PORT, 0x0);
432d8f48fbdSVijendar Mukunda 
433d8f48fbdSVijendar Mukunda 			sdw_fill_port_params(&p_rt->port_params,
434d8f48fbdSVijendar Mukunda 					     p_rt->num, bps,
435d8f48fbdSVijendar Mukunda 					     SDW_PORT_FLOW_MODE_ISOCH,
436d8f48fbdSVijendar Mukunda 					     b_params->m_data_mode);
437d8f48fbdSVijendar Mukunda 			t_data.hstart = hstart;
438d8f48fbdSVijendar Mukunda 			t_data.hstop = hstop;
439d8f48fbdSVijendar Mukunda 			t_data.block_offset = port_bo;
440d8f48fbdSVijendar Mukunda 			t_data.sub_block_offset = 0;
441d8f48fbdSVijendar Mukunda 		}
442d8f48fbdSVijendar Mukunda 		sdw_compute_slave_ports(m_rt, &t_data);
443d8f48fbdSVijendar Mukunda 	}
444d8f48fbdSVijendar Mukunda 	return 0;
445d8f48fbdSVijendar Mukunda }
446d8f48fbdSVijendar Mukunda 
amd_sdw_port_params(struct sdw_bus * bus,struct sdw_port_params * p_params,unsigned int bank)447d8f48fbdSVijendar Mukunda static int amd_sdw_port_params(struct sdw_bus *bus, struct sdw_port_params *p_params,
448d8f48fbdSVijendar Mukunda 			       unsigned int bank)
449d8f48fbdSVijendar Mukunda {
450d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = to_amd_sdw(bus);
451d8f48fbdSVijendar Mukunda 	u32 frame_fmt_reg, dpn_frame_fmt;
452d8f48fbdSVijendar Mukunda 
453d8f48fbdSVijendar Mukunda 	dev_dbg(amd_manager->dev, "p_params->num:0x%x\n", p_params->num);
454d8f48fbdSVijendar Mukunda 	switch (amd_manager->instance) {
455d8f48fbdSVijendar Mukunda 	case ACP_SDW0:
456d8f48fbdSVijendar Mukunda 		frame_fmt_reg = sdw0_manager_dp_reg[p_params->num].frame_fmt_reg;
457d8f48fbdSVijendar Mukunda 		break;
458d8f48fbdSVijendar Mukunda 	case ACP_SDW1:
459d8f48fbdSVijendar Mukunda 		frame_fmt_reg = sdw1_manager_dp_reg[p_params->num].frame_fmt_reg;
460d8f48fbdSVijendar Mukunda 		break;
461d8f48fbdSVijendar Mukunda 	default:
462d8f48fbdSVijendar Mukunda 		return -EINVAL;
463d8f48fbdSVijendar Mukunda 	}
464d8f48fbdSVijendar Mukunda 
465d8f48fbdSVijendar Mukunda 	dpn_frame_fmt = readl(amd_manager->mmio + frame_fmt_reg);
466d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_frame_fmt, p_params->flow_mode, AMD_DPN_FRAME_FMT_PFM);
467d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_frame_fmt, p_params->data_mode, AMD_DPN_FRAME_FMT_PDM);
468d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_frame_fmt, p_params->bps - 1, AMD_DPN_FRAME_FMT_WORD_LEN);
469d8f48fbdSVijendar Mukunda 	writel(dpn_frame_fmt, amd_manager->mmio + frame_fmt_reg);
470d8f48fbdSVijendar Mukunda 	return 0;
471d8f48fbdSVijendar Mukunda }
472d8f48fbdSVijendar Mukunda 
amd_sdw_transport_params(struct sdw_bus * bus,struct sdw_transport_params * params,enum sdw_reg_bank bank)473d8f48fbdSVijendar Mukunda static int amd_sdw_transport_params(struct sdw_bus *bus,
474d8f48fbdSVijendar Mukunda 				    struct sdw_transport_params *params,
475d8f48fbdSVijendar Mukunda 				    enum sdw_reg_bank bank)
476d8f48fbdSVijendar Mukunda {
477d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = to_amd_sdw(bus);
478d8f48fbdSVijendar Mukunda 	u32 dpn_frame_fmt;
479d8f48fbdSVijendar Mukunda 	u32 dpn_sampleinterval;
480d8f48fbdSVijendar Mukunda 	u32 dpn_hctrl;
481d8f48fbdSVijendar Mukunda 	u32 dpn_offsetctrl;
482d8f48fbdSVijendar Mukunda 	u32 dpn_lanectrl;
483d8f48fbdSVijendar Mukunda 	u32 frame_fmt_reg, sample_int_reg, hctrl_dp0_reg;
484d8f48fbdSVijendar Mukunda 	u32 offset_reg, lane_ctrl_ch_en_reg;
485d8f48fbdSVijendar Mukunda 
486d8f48fbdSVijendar Mukunda 	switch (amd_manager->instance) {
487d8f48fbdSVijendar Mukunda 	case ACP_SDW0:
488d8f48fbdSVijendar Mukunda 		frame_fmt_reg = sdw0_manager_dp_reg[params->port_num].frame_fmt_reg;
489d8f48fbdSVijendar Mukunda 		sample_int_reg = sdw0_manager_dp_reg[params->port_num].sample_int_reg;
490d8f48fbdSVijendar Mukunda 		hctrl_dp0_reg = sdw0_manager_dp_reg[params->port_num].hctrl_dp0_reg;
491d8f48fbdSVijendar Mukunda 		offset_reg = sdw0_manager_dp_reg[params->port_num].offset_reg;
492d8f48fbdSVijendar Mukunda 		lane_ctrl_ch_en_reg = sdw0_manager_dp_reg[params->port_num].lane_ctrl_ch_en_reg;
493d8f48fbdSVijendar Mukunda 		break;
494d8f48fbdSVijendar Mukunda 	case ACP_SDW1:
495d8f48fbdSVijendar Mukunda 		frame_fmt_reg = sdw1_manager_dp_reg[params->port_num].frame_fmt_reg;
496d8f48fbdSVijendar Mukunda 		sample_int_reg = sdw1_manager_dp_reg[params->port_num].sample_int_reg;
497d8f48fbdSVijendar Mukunda 		hctrl_dp0_reg = sdw1_manager_dp_reg[params->port_num].hctrl_dp0_reg;
498d8f48fbdSVijendar Mukunda 		offset_reg = sdw1_manager_dp_reg[params->port_num].offset_reg;
499d8f48fbdSVijendar Mukunda 		lane_ctrl_ch_en_reg = sdw1_manager_dp_reg[params->port_num].lane_ctrl_ch_en_reg;
500d8f48fbdSVijendar Mukunda 		break;
501d8f48fbdSVijendar Mukunda 	default:
502d8f48fbdSVijendar Mukunda 		return -EINVAL;
503d8f48fbdSVijendar Mukunda 	}
504d8f48fbdSVijendar Mukunda 	writel(AMD_SDW_SSP_COUNTER_VAL, amd_manager->mmio + ACP_SW_SSP_COUNTER);
505d8f48fbdSVijendar Mukunda 
506d8f48fbdSVijendar Mukunda 	dpn_frame_fmt = readl(amd_manager->mmio + frame_fmt_reg);
507d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_frame_fmt, params->blk_pkg_mode, AMD_DPN_FRAME_FMT_BLK_PKG_MODE);
508d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_frame_fmt, params->blk_grp_ctrl, AMD_DPN_FRAME_FMT_BLK_GRP_CTRL);
509d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_frame_fmt, SDW_STREAM_PCM, AMD_DPN_FRAME_FMT_PCM_OR_PDM);
510d8f48fbdSVijendar Mukunda 	writel(dpn_frame_fmt, amd_manager->mmio + frame_fmt_reg);
511d8f48fbdSVijendar Mukunda 
512d8f48fbdSVijendar Mukunda 	dpn_sampleinterval = params->sample_interval - 1;
513d8f48fbdSVijendar Mukunda 	writel(dpn_sampleinterval, amd_manager->mmio + sample_int_reg);
514d8f48fbdSVijendar Mukunda 
515d8f48fbdSVijendar Mukunda 	dpn_hctrl = FIELD_PREP(AMD_DPN_HCTRL_HSTOP, params->hstop);
516d8f48fbdSVijendar Mukunda 	dpn_hctrl |= FIELD_PREP(AMD_DPN_HCTRL_HSTART, params->hstart);
517d8f48fbdSVijendar Mukunda 	writel(dpn_hctrl, amd_manager->mmio + hctrl_dp0_reg);
518d8f48fbdSVijendar Mukunda 
519d8f48fbdSVijendar Mukunda 	dpn_offsetctrl = FIELD_PREP(AMD_DPN_OFFSET_CTRL_1, params->offset1);
520d8f48fbdSVijendar Mukunda 	dpn_offsetctrl |= FIELD_PREP(AMD_DPN_OFFSET_CTRL_2, params->offset2);
521d8f48fbdSVijendar Mukunda 	writel(dpn_offsetctrl, amd_manager->mmio + offset_reg);
522d8f48fbdSVijendar Mukunda 
523d8f48fbdSVijendar Mukunda 	/*
524d8f48fbdSVijendar Mukunda 	 * lane_ctrl_ch_en_reg will be used to program lane_ctrl and ch_mask
525d8f48fbdSVijendar Mukunda 	 * parameters.
526d8f48fbdSVijendar Mukunda 	 */
527d8f48fbdSVijendar Mukunda 	dpn_lanectrl = readl(amd_manager->mmio + lane_ctrl_ch_en_reg);
528d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_lanectrl, params->lane_ctrl, AMD_DPN_CH_EN_LCTRL);
529d8f48fbdSVijendar Mukunda 	writel(dpn_lanectrl, amd_manager->mmio + lane_ctrl_ch_en_reg);
530d8f48fbdSVijendar Mukunda 	return 0;
531d8f48fbdSVijendar Mukunda }
532d8f48fbdSVijendar Mukunda 
amd_sdw_port_enable(struct sdw_bus * bus,struct sdw_enable_ch * enable_ch,unsigned int bank)533d8f48fbdSVijendar Mukunda static int amd_sdw_port_enable(struct sdw_bus *bus,
534d8f48fbdSVijendar Mukunda 			       struct sdw_enable_ch *enable_ch,
535d8f48fbdSVijendar Mukunda 			       unsigned int bank)
536d8f48fbdSVijendar Mukunda {
537d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = to_amd_sdw(bus);
538d8f48fbdSVijendar Mukunda 	u32 dpn_ch_enable;
539d8f48fbdSVijendar Mukunda 	u32 lane_ctrl_ch_en_reg;
540d8f48fbdSVijendar Mukunda 
541d8f48fbdSVijendar Mukunda 	switch (amd_manager->instance) {
542d8f48fbdSVijendar Mukunda 	case ACP_SDW0:
543d8f48fbdSVijendar Mukunda 		lane_ctrl_ch_en_reg = sdw0_manager_dp_reg[enable_ch->port_num].lane_ctrl_ch_en_reg;
544d8f48fbdSVijendar Mukunda 		break;
545d8f48fbdSVijendar Mukunda 	case ACP_SDW1:
546d8f48fbdSVijendar Mukunda 		lane_ctrl_ch_en_reg = sdw1_manager_dp_reg[enable_ch->port_num].lane_ctrl_ch_en_reg;
547d8f48fbdSVijendar Mukunda 		break;
548d8f48fbdSVijendar Mukunda 	default:
549d8f48fbdSVijendar Mukunda 		return -EINVAL;
550d8f48fbdSVijendar Mukunda 	}
551d8f48fbdSVijendar Mukunda 
552d8f48fbdSVijendar Mukunda 	/*
553d8f48fbdSVijendar Mukunda 	 * lane_ctrl_ch_en_reg will be used to program lane_ctrl and ch_mask
554d8f48fbdSVijendar Mukunda 	 * parameters.
555d8f48fbdSVijendar Mukunda 	 */
556d8f48fbdSVijendar Mukunda 	dpn_ch_enable = readl(amd_manager->mmio + lane_ctrl_ch_en_reg);
557d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_ch_enable, enable_ch->ch_mask, AMD_DPN_CH_EN_CHMASK);
558d8f48fbdSVijendar Mukunda 	if (enable_ch->enable)
559d8f48fbdSVijendar Mukunda 		writel(dpn_ch_enable, amd_manager->mmio + lane_ctrl_ch_en_reg);
560d8f48fbdSVijendar Mukunda 	else
561d8f48fbdSVijendar Mukunda 		writel(0, amd_manager->mmio + lane_ctrl_ch_en_reg);
562d8f48fbdSVijendar Mukunda 	return 0;
563d8f48fbdSVijendar Mukunda }
564d8f48fbdSVijendar Mukunda 
sdw_master_read_amd_prop(struct sdw_bus * bus)565d8f48fbdSVijendar Mukunda static int sdw_master_read_amd_prop(struct sdw_bus *bus)
566d8f48fbdSVijendar Mukunda {
567d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = to_amd_sdw(bus);
568d8f48fbdSVijendar Mukunda 	struct fwnode_handle *link;
569d8f48fbdSVijendar Mukunda 	struct sdw_master_prop *prop;
570d8f48fbdSVijendar Mukunda 	u32 quirk_mask = 0;
571d8f48fbdSVijendar Mukunda 	u32 wake_en_mask = 0;
572d8f48fbdSVijendar Mukunda 	u32 power_mode_mask = 0;
573d8f48fbdSVijendar Mukunda 	char name[32];
574d8f48fbdSVijendar Mukunda 
575d8f48fbdSVijendar Mukunda 	prop = &bus->prop;
576d8f48fbdSVijendar Mukunda 	/* Find manager handle */
577d8f48fbdSVijendar Mukunda 	snprintf(name, sizeof(name), "mipi-sdw-link-%d-subproperties", bus->link_id);
578d8f48fbdSVijendar Mukunda 	link = device_get_named_child_node(bus->dev, name);
579d8f48fbdSVijendar Mukunda 	if (!link) {
580d8f48fbdSVijendar Mukunda 		dev_err(bus->dev, "Manager node %s not found\n", name);
581d8f48fbdSVijendar Mukunda 		return -EIO;
582d8f48fbdSVijendar Mukunda 	}
583d8f48fbdSVijendar Mukunda 	fwnode_property_read_u32(link, "amd-sdw-enable", &quirk_mask);
584d8f48fbdSVijendar Mukunda 	if (!(quirk_mask & AMD_SDW_QUIRK_MASK_BUS_ENABLE))
585d8f48fbdSVijendar Mukunda 		prop->hw_disabled = true;
586d8f48fbdSVijendar Mukunda 	prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
587d8f48fbdSVijendar Mukunda 		       SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
588d8f48fbdSVijendar Mukunda 
589d8f48fbdSVijendar Mukunda 	fwnode_property_read_u32(link, "amd-sdw-wakeup-enable", &wake_en_mask);
590d8f48fbdSVijendar Mukunda 	amd_manager->wake_en_mask = wake_en_mask;
591d8f48fbdSVijendar Mukunda 	fwnode_property_read_u32(link, "amd-sdw-power-mode", &power_mode_mask);
592d8f48fbdSVijendar Mukunda 	amd_manager->power_mode_mask = power_mode_mask;
593d8f48fbdSVijendar Mukunda 	return 0;
594d8f48fbdSVijendar Mukunda }
595d8f48fbdSVijendar Mukunda 
amd_prop_read(struct sdw_bus * bus)596d8f48fbdSVijendar Mukunda static int amd_prop_read(struct sdw_bus *bus)
597d8f48fbdSVijendar Mukunda {
598d8f48fbdSVijendar Mukunda 	sdw_master_read_prop(bus);
599d8f48fbdSVijendar Mukunda 	sdw_master_read_amd_prop(bus);
600d8f48fbdSVijendar Mukunda 	return 0;
601d8f48fbdSVijendar Mukunda }
602d8f48fbdSVijendar Mukunda 
603d8f48fbdSVijendar Mukunda static const struct sdw_master_port_ops amd_sdw_port_ops = {
604d8f48fbdSVijendar Mukunda 	.dpn_set_port_params = amd_sdw_port_params,
605d8f48fbdSVijendar Mukunda 	.dpn_set_port_transport_params = amd_sdw_transport_params,
606d8f48fbdSVijendar Mukunda 	.dpn_port_enable_ch = amd_sdw_port_enable,
607d8f48fbdSVijendar Mukunda };
608d8f48fbdSVijendar Mukunda 
609d8f48fbdSVijendar Mukunda static const struct sdw_master_ops amd_sdw_ops = {
610d8f48fbdSVijendar Mukunda 	.read_prop = amd_prop_read,
611d8f48fbdSVijendar Mukunda 	.xfer_msg = amd_sdw_xfer_msg,
612d8f48fbdSVijendar Mukunda 	.read_ping_status = amd_sdw_read_ping_status,
613d8f48fbdSVijendar Mukunda };
614d8f48fbdSVijendar Mukunda 
amd_sdw_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)6152b13596fSVijendar Mukunda static int amd_sdw_hw_params(struct snd_pcm_substream *substream,
6162b13596fSVijendar Mukunda 			     struct snd_pcm_hw_params *params,
6172b13596fSVijendar Mukunda 			     struct snd_soc_dai *dai)
6182b13596fSVijendar Mukunda {
6192b13596fSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = snd_soc_dai_get_drvdata(dai);
6202b13596fSVijendar Mukunda 	struct sdw_amd_dai_runtime *dai_runtime;
6212b13596fSVijendar Mukunda 	struct sdw_stream_config sconfig;
6222b13596fSVijendar Mukunda 	struct sdw_port_config *pconfig;
6232b13596fSVijendar Mukunda 	int ch, dir;
6242b13596fSVijendar Mukunda 	int ret;
6252b13596fSVijendar Mukunda 
6262b13596fSVijendar Mukunda 	dai_runtime = amd_manager->dai_runtime_array[dai->id];
6272b13596fSVijendar Mukunda 	if (!dai_runtime)
6282b13596fSVijendar Mukunda 		return -EIO;
6292b13596fSVijendar Mukunda 
6302b13596fSVijendar Mukunda 	ch = params_channels(params);
6312b13596fSVijendar Mukunda 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
6322b13596fSVijendar Mukunda 		dir = SDW_DATA_DIR_RX;
6332b13596fSVijendar Mukunda 	else
6342b13596fSVijendar Mukunda 		dir = SDW_DATA_DIR_TX;
6352b13596fSVijendar Mukunda 	dev_dbg(amd_manager->dev, "dir:%d dai->id:0x%x\n", dir, dai->id);
6362b13596fSVijendar Mukunda 
6372b13596fSVijendar Mukunda 	sconfig.direction = dir;
6382b13596fSVijendar Mukunda 	sconfig.ch_count = ch;
6392b13596fSVijendar Mukunda 	sconfig.frame_rate = params_rate(params);
6402b13596fSVijendar Mukunda 	sconfig.type = dai_runtime->stream_type;
6412b13596fSVijendar Mukunda 
6422b13596fSVijendar Mukunda 	sconfig.bps = snd_pcm_format_width(params_format(params));
6432b13596fSVijendar Mukunda 
6442b13596fSVijendar Mukunda 	/* Port configuration */
6452b13596fSVijendar Mukunda 	pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL);
6462b13596fSVijendar Mukunda 	if (!pconfig) {
6472b13596fSVijendar Mukunda 		ret =  -ENOMEM;
6482b13596fSVijendar Mukunda 		goto error;
6492b13596fSVijendar Mukunda 	}
6502b13596fSVijendar Mukunda 
6512b13596fSVijendar Mukunda 	pconfig->num = dai->id;
6522b13596fSVijendar Mukunda 	pconfig->ch_mask = (1 << ch) - 1;
6532b13596fSVijendar Mukunda 	ret = sdw_stream_add_master(&amd_manager->bus, &sconfig,
6542b13596fSVijendar Mukunda 				    pconfig, 1, dai_runtime->stream);
6552b13596fSVijendar Mukunda 	if (ret)
6562b13596fSVijendar Mukunda 		dev_err(amd_manager->dev, "add manager to stream failed:%d\n", ret);
6572b13596fSVijendar Mukunda 
6582b13596fSVijendar Mukunda 	kfree(pconfig);
6592b13596fSVijendar Mukunda error:
6602b13596fSVijendar Mukunda 	return ret;
6612b13596fSVijendar Mukunda }
6622b13596fSVijendar Mukunda 
amd_sdw_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)6632b13596fSVijendar Mukunda static int amd_sdw_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
6642b13596fSVijendar Mukunda {
6652b13596fSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = snd_soc_dai_get_drvdata(dai);
6662b13596fSVijendar Mukunda 	struct sdw_amd_dai_runtime *dai_runtime;
6672b13596fSVijendar Mukunda 	int ret;
6682b13596fSVijendar Mukunda 
6692b13596fSVijendar Mukunda 	dai_runtime = amd_manager->dai_runtime_array[dai->id];
6702b13596fSVijendar Mukunda 	if (!dai_runtime)
6712b13596fSVijendar Mukunda 		return -EIO;
6722b13596fSVijendar Mukunda 
6732b13596fSVijendar Mukunda 	ret = sdw_stream_remove_master(&amd_manager->bus, dai_runtime->stream);
6742b13596fSVijendar Mukunda 	if (ret < 0)
6752b13596fSVijendar Mukunda 		dev_err(dai->dev, "remove manager from stream %s failed: %d\n",
6762b13596fSVijendar Mukunda 			dai_runtime->stream->name, ret);
6772b13596fSVijendar Mukunda 	return ret;
6782b13596fSVijendar Mukunda }
6792b13596fSVijendar Mukunda 
amd_set_sdw_stream(struct snd_soc_dai * dai,void * stream,int direction)6802b13596fSVijendar Mukunda static int amd_set_sdw_stream(struct snd_soc_dai *dai, void *stream, int direction)
6812b13596fSVijendar Mukunda {
6822b13596fSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = snd_soc_dai_get_drvdata(dai);
6832b13596fSVijendar Mukunda 	struct sdw_amd_dai_runtime *dai_runtime;
6842b13596fSVijendar Mukunda 
6852b13596fSVijendar Mukunda 	dai_runtime = amd_manager->dai_runtime_array[dai->id];
6862b13596fSVijendar Mukunda 	if (stream) {
6872b13596fSVijendar Mukunda 		/* first paranoia check */
6882b13596fSVijendar Mukunda 		if (dai_runtime) {
6892b13596fSVijendar Mukunda 			dev_err(dai->dev, "dai_runtime already allocated for dai %s\n",	dai->name);
6902b13596fSVijendar Mukunda 			return -EINVAL;
6912b13596fSVijendar Mukunda 		}
6922b13596fSVijendar Mukunda 
6932b13596fSVijendar Mukunda 		/* allocate and set dai_runtime info */
6942b13596fSVijendar Mukunda 		dai_runtime = kzalloc(sizeof(*dai_runtime), GFP_KERNEL);
6952b13596fSVijendar Mukunda 		if (!dai_runtime)
6962b13596fSVijendar Mukunda 			return -ENOMEM;
6972b13596fSVijendar Mukunda 
6982b13596fSVijendar Mukunda 		dai_runtime->stream_type = SDW_STREAM_PCM;
6992b13596fSVijendar Mukunda 		dai_runtime->bus = &amd_manager->bus;
7002b13596fSVijendar Mukunda 		dai_runtime->stream = stream;
7012b13596fSVijendar Mukunda 		amd_manager->dai_runtime_array[dai->id] = dai_runtime;
7022b13596fSVijendar Mukunda 	} else {
7032b13596fSVijendar Mukunda 		/* second paranoia check */
7042b13596fSVijendar Mukunda 		if (!dai_runtime) {
7052b13596fSVijendar Mukunda 			dev_err(dai->dev, "dai_runtime not allocated for dai %s\n", dai->name);
7062b13596fSVijendar Mukunda 			return -EINVAL;
7072b13596fSVijendar Mukunda 		}
7082b13596fSVijendar Mukunda 
7092b13596fSVijendar Mukunda 		/* for NULL stream we release allocated dai_runtime */
7102b13596fSVijendar Mukunda 		kfree(dai_runtime);
7112b13596fSVijendar Mukunda 		amd_manager->dai_runtime_array[dai->id] = NULL;
7122b13596fSVijendar Mukunda 	}
7132b13596fSVijendar Mukunda 	return 0;
7142b13596fSVijendar Mukunda }
7152b13596fSVijendar Mukunda 
amd_pcm_set_sdw_stream(struct snd_soc_dai * dai,void * stream,int direction)7162b13596fSVijendar Mukunda static int amd_pcm_set_sdw_stream(struct snd_soc_dai *dai, void *stream, int direction)
7172b13596fSVijendar Mukunda {
7182b13596fSVijendar Mukunda 	return amd_set_sdw_stream(dai, stream, direction);
7192b13596fSVijendar Mukunda }
7202b13596fSVijendar Mukunda 
amd_get_sdw_stream(struct snd_soc_dai * dai,int direction)7212b13596fSVijendar Mukunda static void *amd_get_sdw_stream(struct snd_soc_dai *dai, int direction)
7222b13596fSVijendar Mukunda {
7232b13596fSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = snd_soc_dai_get_drvdata(dai);
7242b13596fSVijendar Mukunda 	struct sdw_amd_dai_runtime *dai_runtime;
7252b13596fSVijendar Mukunda 
7262b13596fSVijendar Mukunda 	dai_runtime = amd_manager->dai_runtime_array[dai->id];
7272b13596fSVijendar Mukunda 	if (!dai_runtime)
7282b13596fSVijendar Mukunda 		return ERR_PTR(-EINVAL);
7292b13596fSVijendar Mukunda 
7302b13596fSVijendar Mukunda 	return dai_runtime->stream;
7312b13596fSVijendar Mukunda }
7322b13596fSVijendar Mukunda 
7332b13596fSVijendar Mukunda static const struct snd_soc_dai_ops amd_sdw_dai_ops = {
7342b13596fSVijendar Mukunda 	.hw_params = amd_sdw_hw_params,
7352b13596fSVijendar Mukunda 	.hw_free = amd_sdw_hw_free,
7362b13596fSVijendar Mukunda 	.set_stream = amd_pcm_set_sdw_stream,
7372b13596fSVijendar Mukunda 	.get_stream = amd_get_sdw_stream,
7382b13596fSVijendar Mukunda };
7392b13596fSVijendar Mukunda 
7402b13596fSVijendar Mukunda static const struct snd_soc_component_driver amd_sdw_dai_component = {
7412b13596fSVijendar Mukunda 	.name = "soundwire",
7422b13596fSVijendar Mukunda };
7432b13596fSVijendar Mukunda 
amd_sdw_register_dais(struct amd_sdw_manager * amd_manager)7442b13596fSVijendar Mukunda static int amd_sdw_register_dais(struct amd_sdw_manager *amd_manager)
7452b13596fSVijendar Mukunda {
7462b13596fSVijendar Mukunda 	struct sdw_amd_dai_runtime **dai_runtime_array;
7472b13596fSVijendar Mukunda 	struct snd_soc_dai_driver *dais;
7482b13596fSVijendar Mukunda 	struct snd_soc_pcm_stream *stream;
7492b13596fSVijendar Mukunda 	struct device *dev;
7502b13596fSVijendar Mukunda 	int i, num_dais;
7512b13596fSVijendar Mukunda 
7522b13596fSVijendar Mukunda 	dev = amd_manager->dev;
7532b13596fSVijendar Mukunda 	num_dais = amd_manager->num_dout_ports + amd_manager->num_din_ports;
7542b13596fSVijendar Mukunda 	dais = devm_kcalloc(dev, num_dais, sizeof(*dais), GFP_KERNEL);
7552b13596fSVijendar Mukunda 	if (!dais)
7562b13596fSVijendar Mukunda 		return -ENOMEM;
7572b13596fSVijendar Mukunda 
7582b13596fSVijendar Mukunda 	dai_runtime_array = devm_kcalloc(dev, num_dais,
7592b13596fSVijendar Mukunda 					 sizeof(struct sdw_amd_dai_runtime *),
7602b13596fSVijendar Mukunda 					 GFP_KERNEL);
7612b13596fSVijendar Mukunda 	if (!dai_runtime_array)
7622b13596fSVijendar Mukunda 		return -ENOMEM;
7632b13596fSVijendar Mukunda 	amd_manager->dai_runtime_array = dai_runtime_array;
7642b13596fSVijendar Mukunda 	for (i = 0; i < num_dais; i++) {
7652b13596fSVijendar Mukunda 		dais[i].name = devm_kasprintf(dev, GFP_KERNEL, "SDW%d Pin%d", amd_manager->instance,
7662b13596fSVijendar Mukunda 					      i);
7672b13596fSVijendar Mukunda 		if (!dais[i].name)
7682b13596fSVijendar Mukunda 			return -ENOMEM;
7692b13596fSVijendar Mukunda 		if (i < amd_manager->num_dout_ports)
7702b13596fSVijendar Mukunda 			stream = &dais[i].playback;
7712b13596fSVijendar Mukunda 		else
7722b13596fSVijendar Mukunda 			stream = &dais[i].capture;
7732b13596fSVijendar Mukunda 
7742b13596fSVijendar Mukunda 		stream->channels_min = 2;
7752b13596fSVijendar Mukunda 		stream->channels_max = 2;
7762b13596fSVijendar Mukunda 		stream->rates = SNDRV_PCM_RATE_48000;
7772b13596fSVijendar Mukunda 		stream->formats = SNDRV_PCM_FMTBIT_S16_LE;
7782b13596fSVijendar Mukunda 
7792b13596fSVijendar Mukunda 		dais[i].ops = &amd_sdw_dai_ops;
7802b13596fSVijendar Mukunda 		dais[i].id = i;
7812b13596fSVijendar Mukunda 	}
7822b13596fSVijendar Mukunda 
7832b13596fSVijendar Mukunda 	return devm_snd_soc_register_component(dev, &amd_sdw_dai_component,
7842b13596fSVijendar Mukunda 					       dais, num_dais);
7852b13596fSVijendar Mukunda }
7862b13596fSVijendar Mukunda 
amd_sdw_update_slave_status_work(struct work_struct * work)78765f93e40SVijendar Mukunda static void amd_sdw_update_slave_status_work(struct work_struct *work)
78865f93e40SVijendar Mukunda {
78965f93e40SVijendar Mukunda 	struct amd_sdw_manager *amd_manager =
79065f93e40SVijendar Mukunda 		container_of(work, struct amd_sdw_manager, amd_sdw_work);
79165f93e40SVijendar Mukunda 	int retry_count = 0;
79265f93e40SVijendar Mukunda 
79365f93e40SVijendar Mukunda 	if (amd_manager->status[0] == SDW_SLAVE_ATTACHED) {
79465f93e40SVijendar Mukunda 		writel(0, amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_MASK_0TO7);
79565f93e40SVijendar Mukunda 		writel(0, amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_MASK_8TO11);
79665f93e40SVijendar Mukunda 	}
79765f93e40SVijendar Mukunda 
79865f93e40SVijendar Mukunda update_status:
79965f93e40SVijendar Mukunda 	sdw_handle_slave_status(&amd_manager->bus, amd_manager->status);
80065f93e40SVijendar Mukunda 	/*
80165f93e40SVijendar Mukunda 	 * During the peripheral enumeration sequence, the SoundWire manager interrupts
80265f93e40SVijendar Mukunda 	 * are masked. Once the device number programming is done for all peripherals,
80365f93e40SVijendar Mukunda 	 * interrupts will be unmasked. Read the peripheral device status from ping command
80465f93e40SVijendar Mukunda 	 * and process the response. This sequence will ensure all peripheral devices enumerated
80565f93e40SVijendar Mukunda 	 * and initialized properly.
80665f93e40SVijendar Mukunda 	 */
80765f93e40SVijendar Mukunda 	if (amd_manager->status[0] == SDW_SLAVE_ATTACHED) {
80865f93e40SVijendar Mukunda 		if (retry_count++ < SDW_MAX_DEVICES) {
80965f93e40SVijendar Mukunda 			writel(AMD_SDW_IRQ_MASK_0TO7, amd_manager->mmio +
81065f93e40SVijendar Mukunda 			       ACP_SW_STATE_CHANGE_STATUS_MASK_0TO7);
81165f93e40SVijendar Mukunda 			writel(AMD_SDW_IRQ_MASK_8TO11, amd_manager->mmio +
81265f93e40SVijendar Mukunda 			       ACP_SW_STATE_CHANGE_STATUS_MASK_8TO11);
81365f93e40SVijendar Mukunda 			amd_sdw_read_and_process_ping_status(amd_manager);
81465f93e40SVijendar Mukunda 			goto update_status;
81565f93e40SVijendar Mukunda 		} else {
81665f93e40SVijendar Mukunda 			dev_err_ratelimited(amd_manager->dev,
81765f93e40SVijendar Mukunda 					    "Device0 detected after %d iterations\n",
81865f93e40SVijendar Mukunda 					    retry_count);
81965f93e40SVijendar Mukunda 		}
82065f93e40SVijendar Mukunda 	}
82165f93e40SVijendar Mukunda }
82265f93e40SVijendar Mukunda 
amd_sdw_update_slave_status(u32 status_change_0to7,u32 status_change_8to11,struct amd_sdw_manager * amd_manager)82365f93e40SVijendar Mukunda static void amd_sdw_update_slave_status(u32 status_change_0to7, u32 status_change_8to11,
82465f93e40SVijendar Mukunda 					struct amd_sdw_manager *amd_manager)
82565f93e40SVijendar Mukunda {
82665f93e40SVijendar Mukunda 	u64 slave_stat;
82765f93e40SVijendar Mukunda 	u32 val;
82865f93e40SVijendar Mukunda 	int dev_index;
82965f93e40SVijendar Mukunda 
83065f93e40SVijendar Mukunda 	if (status_change_0to7 == AMD_SDW_SLAVE_0_ATTACHED)
83165f93e40SVijendar Mukunda 		memset(amd_manager->status, 0, sizeof(amd_manager->status));
83265f93e40SVijendar Mukunda 	slave_stat = status_change_0to7;
83365f93e40SVijendar Mukunda 	slave_stat |= FIELD_GET(AMD_SDW_MCP_SLAVE_STATUS_8TO_11, status_change_8to11) << 32;
83465f93e40SVijendar Mukunda 	dev_dbg(amd_manager->dev, "status_change_0to7:0x%x status_change_8to11:0x%x\n",
83565f93e40SVijendar Mukunda 		status_change_0to7, status_change_8to11);
83665f93e40SVijendar Mukunda 	if (slave_stat) {
83765f93e40SVijendar Mukunda 		for (dev_index = 0; dev_index <= SDW_MAX_DEVICES; ++dev_index) {
83865f93e40SVijendar Mukunda 			if (slave_stat & AMD_SDW_MCP_SLAVE_STATUS_VALID_MASK(dev_index)) {
83965f93e40SVijendar Mukunda 				val = (slave_stat >> AMD_SDW_MCP_SLAVE_STAT_SHIFT_MASK(dev_index)) &
84065f93e40SVijendar Mukunda 				      AMD_SDW_MCP_SLAVE_STATUS_MASK;
84165f93e40SVijendar Mukunda 				amd_sdw_fill_slave_status(amd_manager, dev_index, val);
84265f93e40SVijendar Mukunda 			}
84365f93e40SVijendar Mukunda 		}
84465f93e40SVijendar Mukunda 	}
84565f93e40SVijendar Mukunda }
84665f93e40SVijendar Mukunda 
amd_sdw_process_wake_event(struct amd_sdw_manager * amd_manager)84766c87883SVijendar Mukunda static void amd_sdw_process_wake_event(struct amd_sdw_manager *amd_manager)
84866c87883SVijendar Mukunda {
84966c87883SVijendar Mukunda 	pm_request_resume(amd_manager->dev);
85066c87883SVijendar Mukunda 	writel(0x00, amd_manager->acp_mmio + ACP_SW_WAKE_EN(amd_manager->instance));
85166c87883SVijendar Mukunda 	writel(0x00, amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_8TO11);
85266c87883SVijendar Mukunda }
85366c87883SVijendar Mukunda 
amd_sdw_irq_thread(struct work_struct * work)85465f93e40SVijendar Mukunda static void amd_sdw_irq_thread(struct work_struct *work)
85565f93e40SVijendar Mukunda {
85665f93e40SVijendar Mukunda 	struct amd_sdw_manager *amd_manager =
85765f93e40SVijendar Mukunda 			container_of(work, struct amd_sdw_manager, amd_sdw_irq_thread);
85865f93e40SVijendar Mukunda 	u32 status_change_8to11;
85965f93e40SVijendar Mukunda 	u32 status_change_0to7;
86065f93e40SVijendar Mukunda 
86165f93e40SVijendar Mukunda 	status_change_8to11 = readl(amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_8TO11);
86265f93e40SVijendar Mukunda 	status_change_0to7 = readl(amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_0TO7);
86365f93e40SVijendar Mukunda 	dev_dbg(amd_manager->dev, "[SDW%d] SDW INT: 0to7=0x%x, 8to11=0x%x\n",
86465f93e40SVijendar Mukunda 		amd_manager->instance, status_change_0to7, status_change_8to11);
86566c87883SVijendar Mukunda 	if (status_change_8to11 & AMD_SDW_WAKE_STAT_MASK)
86666c87883SVijendar Mukunda 		return amd_sdw_process_wake_event(amd_manager);
86766c87883SVijendar Mukunda 
86865f93e40SVijendar Mukunda 	if (status_change_8to11 & AMD_SDW_PREQ_INTR_STAT) {
86965f93e40SVijendar Mukunda 		amd_sdw_read_and_process_ping_status(amd_manager);
87065f93e40SVijendar Mukunda 	} else {
87165f93e40SVijendar Mukunda 		/* Check for the updated status on peripheral device */
87265f93e40SVijendar Mukunda 		amd_sdw_update_slave_status(status_change_0to7, status_change_8to11, amd_manager);
87365f93e40SVijendar Mukunda 	}
87465f93e40SVijendar Mukunda 	if (status_change_8to11 || status_change_0to7)
87565f93e40SVijendar Mukunda 		schedule_work(&amd_manager->amd_sdw_work);
87665f93e40SVijendar Mukunda 	writel(0x00, amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_8TO11);
87765f93e40SVijendar Mukunda 	writel(0x00, amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_0TO7);
87865f93e40SVijendar Mukunda }
87965f93e40SVijendar Mukunda 
amd_sdw_probe_work(struct work_struct * work)880d8f48fbdSVijendar Mukunda static void amd_sdw_probe_work(struct work_struct *work)
881d8f48fbdSVijendar Mukunda {
882d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = container_of(work, struct amd_sdw_manager,
883d8f48fbdSVijendar Mukunda 							   probe_work);
884d8f48fbdSVijendar Mukunda 	struct sdw_master_prop *prop;
885d8f48fbdSVijendar Mukunda 	int ret;
886d8f48fbdSVijendar Mukunda 
887d8f48fbdSVijendar Mukunda 	prop = &amd_manager->bus.prop;
888d8f48fbdSVijendar Mukunda 	if (!prop->hw_disabled) {
889d8f48fbdSVijendar Mukunda 		amd_enable_sdw_pads(amd_manager);
890d8f48fbdSVijendar Mukunda 		ret = amd_init_sdw_manager(amd_manager);
891d8f48fbdSVijendar Mukunda 		if (ret)
892d8f48fbdSVijendar Mukunda 			return;
893d8f48fbdSVijendar Mukunda 		amd_enable_sdw_interrupts(amd_manager);
894d8f48fbdSVijendar Mukunda 		ret = amd_enable_sdw_manager(amd_manager);
895d8f48fbdSVijendar Mukunda 		if (ret)
896d8f48fbdSVijendar Mukunda 			return;
897d8f48fbdSVijendar Mukunda 		amd_sdw_set_frameshape(amd_manager);
898d8f48fbdSVijendar Mukunda 	}
89981ff58ffSVijendar Mukunda 	/* Enable runtime PM */
90081ff58ffSVijendar Mukunda 	pm_runtime_set_autosuspend_delay(amd_manager->dev, AMD_SDW_MASTER_SUSPEND_DELAY_MS);
90181ff58ffSVijendar Mukunda 	pm_runtime_use_autosuspend(amd_manager->dev);
90281ff58ffSVijendar Mukunda 	pm_runtime_mark_last_busy(amd_manager->dev);
90381ff58ffSVijendar Mukunda 	pm_runtime_set_active(amd_manager->dev);
90481ff58ffSVijendar Mukunda 	pm_runtime_enable(amd_manager->dev);
905d8f48fbdSVijendar Mukunda }
906d8f48fbdSVijendar Mukunda 
amd_sdw_manager_probe(struct platform_device * pdev)907d8f48fbdSVijendar Mukunda static int amd_sdw_manager_probe(struct platform_device *pdev)
908d8f48fbdSVijendar Mukunda {
909d8f48fbdSVijendar Mukunda 	const struct acp_sdw_pdata *pdata = pdev->dev.platform_data;
910d8f48fbdSVijendar Mukunda 	struct resource *res;
911d8f48fbdSVijendar Mukunda 	struct device *dev = &pdev->dev;
912d8f48fbdSVijendar Mukunda 	struct sdw_master_prop *prop;
913d8f48fbdSVijendar Mukunda 	struct sdw_bus_params *params;
914d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager;
915d8f48fbdSVijendar Mukunda 	int ret;
916d8f48fbdSVijendar Mukunda 
917d8f48fbdSVijendar Mukunda 	amd_manager = devm_kzalloc(dev, sizeof(struct amd_sdw_manager), GFP_KERNEL);
918d8f48fbdSVijendar Mukunda 	if (!amd_manager)
919d8f48fbdSVijendar Mukunda 		return -ENOMEM;
920d8f48fbdSVijendar Mukunda 
921d8f48fbdSVijendar Mukunda 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
922d8f48fbdSVijendar Mukunda 	if (!res)
923d8f48fbdSVijendar Mukunda 		return -ENOMEM;
924d8f48fbdSVijendar Mukunda 
925d8f48fbdSVijendar Mukunda 	amd_manager->acp_mmio = devm_ioremap(dev, res->start, resource_size(res));
9267891d0a5SDan Carpenter 	if (!amd_manager->acp_mmio) {
927d8f48fbdSVijendar Mukunda 		dev_err(dev, "mmio not found\n");
9287891d0a5SDan Carpenter 		return -ENOMEM;
929d8f48fbdSVijendar Mukunda 	}
930d8f48fbdSVijendar Mukunda 	amd_manager->instance = pdata->instance;
931d8f48fbdSVijendar Mukunda 	amd_manager->mmio = amd_manager->acp_mmio +
932d8f48fbdSVijendar Mukunda 			    (amd_manager->instance * SDW_MANAGER_REG_OFFSET);
933d8f48fbdSVijendar Mukunda 	amd_manager->acp_sdw_lock = pdata->acp_sdw_lock;
934d8f48fbdSVijendar Mukunda 	amd_manager->cols_index = sdw_find_col_index(AMD_SDW_DEFAULT_COLUMNS);
935d8f48fbdSVijendar Mukunda 	amd_manager->rows_index = sdw_find_row_index(AMD_SDW_DEFAULT_ROWS);
936d8f48fbdSVijendar Mukunda 	amd_manager->dev = dev;
937d8f48fbdSVijendar Mukunda 	amd_manager->bus.ops = &amd_sdw_ops;
938d8f48fbdSVijendar Mukunda 	amd_manager->bus.port_ops = &amd_sdw_port_ops;
939d8f48fbdSVijendar Mukunda 	amd_manager->bus.compute_params = &amd_sdw_compute_params;
940d8f48fbdSVijendar Mukunda 	amd_manager->bus.clk_stop_timeout = 200;
941d8f48fbdSVijendar Mukunda 	amd_manager->bus.link_id = amd_manager->instance;
942d8f48fbdSVijendar Mukunda 
943154cfc3dSPierre-Louis Bossart 	/*
944154cfc3dSPierre-Louis Bossart 	 * Due to BIOS compatibility, the two links are exposed within
945154cfc3dSPierre-Louis Bossart 	 * the scope of a single controller. If this changes, the
946154cfc3dSPierre-Louis Bossart 	 * controller_id will have to be updated with drv_data
947154cfc3dSPierre-Louis Bossart 	 * information.
948154cfc3dSPierre-Louis Bossart 	 */
949154cfc3dSPierre-Louis Bossart 	amd_manager->bus.controller_id = 0;
950154cfc3dSPierre-Louis Bossart 
951d8f48fbdSVijendar Mukunda 	switch (amd_manager->instance) {
952d8f48fbdSVijendar Mukunda 	case ACP_SDW0:
953d8f48fbdSVijendar Mukunda 		amd_manager->num_dout_ports = AMD_SDW0_MAX_TX_PORTS;
954d8f48fbdSVijendar Mukunda 		amd_manager->num_din_ports = AMD_SDW0_MAX_RX_PORTS;
955d8f48fbdSVijendar Mukunda 		break;
956d8f48fbdSVijendar Mukunda 	case ACP_SDW1:
957d8f48fbdSVijendar Mukunda 		amd_manager->num_dout_ports = AMD_SDW1_MAX_TX_PORTS;
958d8f48fbdSVijendar Mukunda 		amd_manager->num_din_ports = AMD_SDW1_MAX_RX_PORTS;
959d8f48fbdSVijendar Mukunda 		break;
960d8f48fbdSVijendar Mukunda 	default:
961d8f48fbdSVijendar Mukunda 		return -EINVAL;
962d8f48fbdSVijendar Mukunda 	}
963d8f48fbdSVijendar Mukunda 
964d8f48fbdSVijendar Mukunda 	amd_manager->reg_mask = &sdw_manager_reg_mask_array[amd_manager->instance];
965d8f48fbdSVijendar Mukunda 	params = &amd_manager->bus.params;
966d8f48fbdSVijendar Mukunda 	params->max_dr_freq = AMD_SDW_DEFAULT_CLK_FREQ * 2;
967d8f48fbdSVijendar Mukunda 	params->curr_dr_freq = AMD_SDW_DEFAULT_CLK_FREQ * 2;
968d8f48fbdSVijendar Mukunda 	params->col = AMD_SDW_DEFAULT_COLUMNS;
969d8f48fbdSVijendar Mukunda 	params->row = AMD_SDW_DEFAULT_ROWS;
970d8f48fbdSVijendar Mukunda 	prop = &amd_manager->bus.prop;
971d8f48fbdSVijendar Mukunda 	prop->clk_freq = &amd_sdw_freq_tbl[0];
972d8f48fbdSVijendar Mukunda 	prop->mclk_freq = AMD_SDW_BUS_BASE_FREQ;
973d8f48fbdSVijendar Mukunda 
974d8f48fbdSVijendar Mukunda 	ret = sdw_bus_master_add(&amd_manager->bus, dev, dev->fwnode);
975d8f48fbdSVijendar Mukunda 	if (ret) {
976d8f48fbdSVijendar Mukunda 		dev_err(dev, "Failed to register SoundWire manager(%d)\n", ret);
977d8f48fbdSVijendar Mukunda 		return ret;
978d8f48fbdSVijendar Mukunda 	}
9792b13596fSVijendar Mukunda 	ret = amd_sdw_register_dais(amd_manager);
9802b13596fSVijendar Mukunda 	if (ret) {
9812b13596fSVijendar Mukunda 		dev_err(dev, "CPU DAI registration failed\n");
9822b13596fSVijendar Mukunda 		sdw_bus_master_delete(&amd_manager->bus);
9832b13596fSVijendar Mukunda 		return ret;
9842b13596fSVijendar Mukunda 	}
985d8f48fbdSVijendar Mukunda 	dev_set_drvdata(dev, amd_manager);
98665f93e40SVijendar Mukunda 	INIT_WORK(&amd_manager->amd_sdw_irq_thread, amd_sdw_irq_thread);
98765f93e40SVijendar Mukunda 	INIT_WORK(&amd_manager->amd_sdw_work, amd_sdw_update_slave_status_work);
988d8f48fbdSVijendar Mukunda 	INIT_WORK(&amd_manager->probe_work, amd_sdw_probe_work);
989d8f48fbdSVijendar Mukunda 	/*
990d8f48fbdSVijendar Mukunda 	 * Instead of having lengthy probe sequence, use deferred probe.
991d8f48fbdSVijendar Mukunda 	 */
992d8f48fbdSVijendar Mukunda 	schedule_work(&amd_manager->probe_work);
993d8f48fbdSVijendar Mukunda 	return 0;
994d8f48fbdSVijendar Mukunda }
995d8f48fbdSVijendar Mukunda 
amd_sdw_manager_remove(struct platform_device * pdev)9961ec33e22SUwe Kleine-König static void amd_sdw_manager_remove(struct platform_device *pdev)
997d8f48fbdSVijendar Mukunda {
998d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = dev_get_drvdata(&pdev->dev);
9991ec33e22SUwe Kleine-König 	int ret;
1000d8f48fbdSVijendar Mukunda 
100181ff58ffSVijendar Mukunda 	pm_runtime_disable(&pdev->dev);
1002d8f48fbdSVijendar Mukunda 	cancel_work_sync(&amd_manager->probe_work);
1003d8f48fbdSVijendar Mukunda 	amd_disable_sdw_interrupts(amd_manager);
1004d8f48fbdSVijendar Mukunda 	sdw_bus_master_delete(&amd_manager->bus);
10051ec33e22SUwe Kleine-König 	ret = amd_disable_sdw_manager(amd_manager);
10061ec33e22SUwe Kleine-König 	if (ret)
10071ec33e22SUwe Kleine-König 		dev_err(&pdev->dev, "Failed to disable device (%pe)\n", ERR_PTR(ret));
1008d8f48fbdSVijendar Mukunda }
1009d8f48fbdSVijendar Mukunda 
amd_sdw_clock_stop(struct amd_sdw_manager * amd_manager)101081ff58ffSVijendar Mukunda static int amd_sdw_clock_stop(struct amd_sdw_manager *amd_manager)
101181ff58ffSVijendar Mukunda {
101281ff58ffSVijendar Mukunda 	u32 val;
101381ff58ffSVijendar Mukunda 	int ret;
101481ff58ffSVijendar Mukunda 
101581ff58ffSVijendar Mukunda 	ret = sdw_bus_prep_clk_stop(&amd_manager->bus);
101681ff58ffSVijendar Mukunda 	if (ret < 0 && ret != -ENODATA) {
101781ff58ffSVijendar Mukunda 		dev_err(amd_manager->dev, "prepare clock stop failed %d", ret);
101881ff58ffSVijendar Mukunda 		return 0;
101981ff58ffSVijendar Mukunda 	}
102081ff58ffSVijendar Mukunda 	ret = sdw_bus_clk_stop(&amd_manager->bus);
102181ff58ffSVijendar Mukunda 	if (ret < 0 && ret != -ENODATA) {
102281ff58ffSVijendar Mukunda 		dev_err(amd_manager->dev, "bus clock stop failed %d", ret);
102381ff58ffSVijendar Mukunda 		return 0;
102481ff58ffSVijendar Mukunda 	}
102581ff58ffSVijendar Mukunda 
102681ff58ffSVijendar Mukunda 	ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL, val,
102781ff58ffSVijendar Mukunda 				 (val & AMD_SDW_CLK_STOP_DONE), ACP_DELAY_US, AMD_SDW_TIMEOUT);
102881ff58ffSVijendar Mukunda 	if (ret) {
102981ff58ffSVijendar Mukunda 		dev_err(amd_manager->dev, "SDW%x clock stop failed\n", amd_manager->instance);
103081ff58ffSVijendar Mukunda 		return 0;
103181ff58ffSVijendar Mukunda 	}
103281ff58ffSVijendar Mukunda 
103381ff58ffSVijendar Mukunda 	amd_manager->clk_stopped = true;
103481ff58ffSVijendar Mukunda 	if (amd_manager->wake_en_mask)
103581ff58ffSVijendar Mukunda 		writel(0x01, amd_manager->acp_mmio + ACP_SW_WAKE_EN(amd_manager->instance));
103681ff58ffSVijendar Mukunda 
103781ff58ffSVijendar Mukunda 	dev_dbg(amd_manager->dev, "SDW%x clock stop successful\n", amd_manager->instance);
103881ff58ffSVijendar Mukunda 	return 0;
103981ff58ffSVijendar Mukunda }
104081ff58ffSVijendar Mukunda 
amd_sdw_clock_stop_exit(struct amd_sdw_manager * amd_manager)104181ff58ffSVijendar Mukunda static int amd_sdw_clock_stop_exit(struct amd_sdw_manager *amd_manager)
104281ff58ffSVijendar Mukunda {
104381ff58ffSVijendar Mukunda 	int ret;
104481ff58ffSVijendar Mukunda 	u32 val;
104581ff58ffSVijendar Mukunda 
104681ff58ffSVijendar Mukunda 	if (amd_manager->clk_stopped) {
104781ff58ffSVijendar Mukunda 		val = readl(amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL);
104881ff58ffSVijendar Mukunda 		val |= AMD_SDW_CLK_RESUME_REQ;
104981ff58ffSVijendar Mukunda 		writel(val, amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL);
105081ff58ffSVijendar Mukunda 		ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL, val,
105181ff58ffSVijendar Mukunda 					 (val & AMD_SDW_CLK_RESUME_DONE), ACP_DELAY_US,
105281ff58ffSVijendar Mukunda 					 AMD_SDW_TIMEOUT);
105381ff58ffSVijendar Mukunda 		if (val & AMD_SDW_CLK_RESUME_DONE) {
105481ff58ffSVijendar Mukunda 			writel(0, amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL);
105581ff58ffSVijendar Mukunda 			ret = sdw_bus_exit_clk_stop(&amd_manager->bus);
105681ff58ffSVijendar Mukunda 			if (ret < 0)
105781ff58ffSVijendar Mukunda 				dev_err(amd_manager->dev, "bus failed to exit clock stop %d\n",
105881ff58ffSVijendar Mukunda 					ret);
105981ff58ffSVijendar Mukunda 			amd_manager->clk_stopped = false;
106081ff58ffSVijendar Mukunda 		}
106181ff58ffSVijendar Mukunda 	}
106281ff58ffSVijendar Mukunda 	if (amd_manager->clk_stopped) {
106381ff58ffSVijendar Mukunda 		dev_err(amd_manager->dev, "SDW%x clock stop exit failed\n", amd_manager->instance);
106481ff58ffSVijendar Mukunda 		return 0;
106581ff58ffSVijendar Mukunda 	}
106681ff58ffSVijendar Mukunda 	dev_dbg(amd_manager->dev, "SDW%x clock stop exit successful\n", amd_manager->instance);
106781ff58ffSVijendar Mukunda 	return 0;
106881ff58ffSVijendar Mukunda }
106981ff58ffSVijendar Mukunda 
amd_resume_child_device(struct device * dev,void * data)10709cf1efc5SVijendar Mukunda static int amd_resume_child_device(struct device *dev, void *data)
10719cf1efc5SVijendar Mukunda {
10729cf1efc5SVijendar Mukunda 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
10739cf1efc5SVijendar Mukunda 	int ret;
10749cf1efc5SVijendar Mukunda 
10759cf1efc5SVijendar Mukunda 	if (!slave->probed) {
10769cf1efc5SVijendar Mukunda 		dev_dbg(dev, "skipping device, no probed driver\n");
10779cf1efc5SVijendar Mukunda 		return 0;
10789cf1efc5SVijendar Mukunda 	}
10799cf1efc5SVijendar Mukunda 	if (!slave->dev_num_sticky) {
10809cf1efc5SVijendar Mukunda 		dev_dbg(dev, "skipping device, never detected on bus\n");
10819cf1efc5SVijendar Mukunda 		return 0;
10829cf1efc5SVijendar Mukunda 	}
10839cf1efc5SVijendar Mukunda 	ret = pm_request_resume(dev);
10849cf1efc5SVijendar Mukunda 	if (ret < 0) {
10859cf1efc5SVijendar Mukunda 		dev_err(dev, "pm_request_resume failed: %d\n", ret);
10869cf1efc5SVijendar Mukunda 		return ret;
10879cf1efc5SVijendar Mukunda 	}
10889cf1efc5SVijendar Mukunda 	return 0;
10899cf1efc5SVijendar Mukunda }
10909cf1efc5SVijendar Mukunda 
amd_pm_prepare(struct device * dev)10919cf1efc5SVijendar Mukunda static int __maybe_unused amd_pm_prepare(struct device *dev)
10929cf1efc5SVijendar Mukunda {
10939cf1efc5SVijendar Mukunda 	struct amd_sdw_manager *amd_manager = dev_get_drvdata(dev);
10949cf1efc5SVijendar Mukunda 	struct sdw_bus *bus = &amd_manager->bus;
10959cf1efc5SVijendar Mukunda 	int ret;
10969cf1efc5SVijendar Mukunda 
10979cf1efc5SVijendar Mukunda 	if (bus->prop.hw_disabled) {
10989cf1efc5SVijendar Mukunda 		dev_dbg(bus->dev, "SoundWire manager %d is disabled, ignoring\n",
10999cf1efc5SVijendar Mukunda 			bus->link_id);
11009cf1efc5SVijendar Mukunda 		return 0;
11019cf1efc5SVijendar Mukunda 	}
11029cf1efc5SVijendar Mukunda 	/*
11039cf1efc5SVijendar Mukunda 	 * When multiple peripheral devices connected over the same link, if SoundWire manager
11049cf1efc5SVijendar Mukunda 	 * device is not in runtime suspend state, observed that device alerts are missing
11059cf1efc5SVijendar Mukunda 	 * without pm_prepare on AMD platforms in clockstop mode0.
11069cf1efc5SVijendar Mukunda 	 */
11079cf1efc5SVijendar Mukunda 	if (amd_manager->power_mode_mask & AMD_SDW_CLK_STOP_MODE) {
11089cf1efc5SVijendar Mukunda 		ret = pm_request_resume(dev);
11099cf1efc5SVijendar Mukunda 		if (ret < 0) {
11109cf1efc5SVijendar Mukunda 			dev_err(bus->dev, "pm_request_resume failed: %d\n", ret);
11119cf1efc5SVijendar Mukunda 			return 0;
11129cf1efc5SVijendar Mukunda 		}
11139cf1efc5SVijendar Mukunda 	}
11149cf1efc5SVijendar Mukunda 	/* To force peripheral devices to system level suspend state, resume the devices
11159cf1efc5SVijendar Mukunda 	 * from runtime suspend state first. Without that unable to dispatch the alert
11169cf1efc5SVijendar Mukunda 	 * status to peripheral driver during system level resume as they are in runtime
11179cf1efc5SVijendar Mukunda 	 * suspend state.
11189cf1efc5SVijendar Mukunda 	 */
11199cf1efc5SVijendar Mukunda 	ret = device_for_each_child(bus->dev, NULL, amd_resume_child_device);
11209cf1efc5SVijendar Mukunda 	if (ret < 0)
11219cf1efc5SVijendar Mukunda 		dev_err(dev, "amd_resume_child_device failed: %d\n", ret);
11229cf1efc5SVijendar Mukunda 	return 0;
11239cf1efc5SVijendar Mukunda }
11249cf1efc5SVijendar Mukunda 
amd_suspend(struct device * dev)11259cf1efc5SVijendar Mukunda static int __maybe_unused amd_suspend(struct device *dev)
11269cf1efc5SVijendar Mukunda {
11279cf1efc5SVijendar Mukunda 	struct amd_sdw_manager *amd_manager = dev_get_drvdata(dev);
11289cf1efc5SVijendar Mukunda 	struct sdw_bus *bus = &amd_manager->bus;
11299cf1efc5SVijendar Mukunda 	int ret;
11309cf1efc5SVijendar Mukunda 
11319cf1efc5SVijendar Mukunda 	if (bus->prop.hw_disabled) {
11329cf1efc5SVijendar Mukunda 		dev_dbg(bus->dev, "SoundWire manager %d is disabled, ignoring\n",
11339cf1efc5SVijendar Mukunda 			bus->link_id);
11349cf1efc5SVijendar Mukunda 		return 0;
11359cf1efc5SVijendar Mukunda 	}
11369cf1efc5SVijendar Mukunda 
11379cf1efc5SVijendar Mukunda 	if (amd_manager->power_mode_mask & AMD_SDW_CLK_STOP_MODE) {
1138*935d4c7cSVijendar Mukunda 		amd_sdw_wake_enable(amd_manager, false);
11399cf1efc5SVijendar Mukunda 		return amd_sdw_clock_stop(amd_manager);
11409cf1efc5SVijendar Mukunda 	} else if (amd_manager->power_mode_mask & AMD_SDW_POWER_OFF_MODE) {
11419cf1efc5SVijendar Mukunda 		/*
11429cf1efc5SVijendar Mukunda 		 * As per hardware programming sequence on AMD platforms,
11439cf1efc5SVijendar Mukunda 		 * clock stop should be invoked first before powering-off
11449cf1efc5SVijendar Mukunda 		 */
11459cf1efc5SVijendar Mukunda 		ret = amd_sdw_clock_stop(amd_manager);
11469cf1efc5SVijendar Mukunda 		if (ret)
11479cf1efc5SVijendar Mukunda 			return ret;
11489cf1efc5SVijendar Mukunda 		return amd_deinit_sdw_manager(amd_manager);
11499cf1efc5SVijendar Mukunda 	}
11509cf1efc5SVijendar Mukunda 	return 0;
11519cf1efc5SVijendar Mukunda }
11529cf1efc5SVijendar Mukunda 
amd_suspend_runtime(struct device * dev)115381ff58ffSVijendar Mukunda static int __maybe_unused amd_suspend_runtime(struct device *dev)
115481ff58ffSVijendar Mukunda {
115581ff58ffSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = dev_get_drvdata(dev);
115681ff58ffSVijendar Mukunda 	struct sdw_bus *bus = &amd_manager->bus;
115781ff58ffSVijendar Mukunda 	int ret;
115881ff58ffSVijendar Mukunda 
115981ff58ffSVijendar Mukunda 	if (bus->prop.hw_disabled) {
116081ff58ffSVijendar Mukunda 		dev_dbg(bus->dev, "SoundWire manager %d is disabled,\n",
116181ff58ffSVijendar Mukunda 			bus->link_id);
116281ff58ffSVijendar Mukunda 		return 0;
116381ff58ffSVijendar Mukunda 	}
116481ff58ffSVijendar Mukunda 	if (amd_manager->power_mode_mask & AMD_SDW_CLK_STOP_MODE) {
1165*935d4c7cSVijendar Mukunda 		amd_sdw_wake_enable(amd_manager, true);
116681ff58ffSVijendar Mukunda 		return amd_sdw_clock_stop(amd_manager);
116781ff58ffSVijendar Mukunda 	} else if (amd_manager->power_mode_mask & AMD_SDW_POWER_OFF_MODE) {
116881ff58ffSVijendar Mukunda 		ret = amd_sdw_clock_stop(amd_manager);
116981ff58ffSVijendar Mukunda 		if (ret)
117081ff58ffSVijendar Mukunda 			return ret;
117181ff58ffSVijendar Mukunda 		return amd_deinit_sdw_manager(amd_manager);
117281ff58ffSVijendar Mukunda 	}
117381ff58ffSVijendar Mukunda 	return 0;
117481ff58ffSVijendar Mukunda }
117581ff58ffSVijendar Mukunda 
amd_resume_runtime(struct device * dev)117681ff58ffSVijendar Mukunda static int __maybe_unused amd_resume_runtime(struct device *dev)
117781ff58ffSVijendar Mukunda {
117881ff58ffSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = dev_get_drvdata(dev);
117981ff58ffSVijendar Mukunda 	struct sdw_bus *bus = &amd_manager->bus;
118081ff58ffSVijendar Mukunda 	int ret;
118181ff58ffSVijendar Mukunda 	u32 val;
118281ff58ffSVijendar Mukunda 
118381ff58ffSVijendar Mukunda 	if (bus->prop.hw_disabled) {
118481ff58ffSVijendar Mukunda 		dev_dbg(bus->dev, "SoundWire manager %d is disabled, ignoring\n",
118581ff58ffSVijendar Mukunda 			bus->link_id);
118681ff58ffSVijendar Mukunda 		return 0;
118781ff58ffSVijendar Mukunda 	}
118881ff58ffSVijendar Mukunda 
118981ff58ffSVijendar Mukunda 	if (amd_manager->power_mode_mask & AMD_SDW_CLK_STOP_MODE) {
119081ff58ffSVijendar Mukunda 		return amd_sdw_clock_stop_exit(amd_manager);
119181ff58ffSVijendar Mukunda 	} else if (amd_manager->power_mode_mask & AMD_SDW_POWER_OFF_MODE) {
119281ff58ffSVijendar Mukunda 		val = readl(amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL);
119381ff58ffSVijendar Mukunda 		if (val) {
119481ff58ffSVijendar Mukunda 			val |= AMD_SDW_CLK_RESUME_REQ;
119581ff58ffSVijendar Mukunda 			writel(val, amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL);
119681ff58ffSVijendar Mukunda 			ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL, val,
119781ff58ffSVijendar Mukunda 						 (val & AMD_SDW_CLK_RESUME_DONE), ACP_DELAY_US,
119881ff58ffSVijendar Mukunda 						 AMD_SDW_TIMEOUT);
119981ff58ffSVijendar Mukunda 			if (val & AMD_SDW_CLK_RESUME_DONE) {
120081ff58ffSVijendar Mukunda 				writel(0, amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL);
120181ff58ffSVijendar Mukunda 				amd_manager->clk_stopped = false;
120281ff58ffSVijendar Mukunda 			}
120381ff58ffSVijendar Mukunda 		}
120481ff58ffSVijendar Mukunda 		sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
120581ff58ffSVijendar Mukunda 		amd_init_sdw_manager(amd_manager);
120681ff58ffSVijendar Mukunda 		amd_enable_sdw_interrupts(amd_manager);
120781ff58ffSVijendar Mukunda 		ret = amd_enable_sdw_manager(amd_manager);
120881ff58ffSVijendar Mukunda 		if (ret)
120981ff58ffSVijendar Mukunda 			return ret;
121081ff58ffSVijendar Mukunda 		amd_sdw_set_frameshape(amd_manager);
121181ff58ffSVijendar Mukunda 	}
121281ff58ffSVijendar Mukunda 	return 0;
121381ff58ffSVijendar Mukunda }
121481ff58ffSVijendar Mukunda 
121581ff58ffSVijendar Mukunda static const struct dev_pm_ops amd_pm = {
12169cf1efc5SVijendar Mukunda 	.prepare = amd_pm_prepare,
12179cf1efc5SVijendar Mukunda 	SET_SYSTEM_SLEEP_PM_OPS(amd_suspend, amd_resume_runtime)
121881ff58ffSVijendar Mukunda 	SET_RUNTIME_PM_OPS(amd_suspend_runtime, amd_resume_runtime, NULL)
121981ff58ffSVijendar Mukunda };
122081ff58ffSVijendar Mukunda 
1221d8f48fbdSVijendar Mukunda static struct platform_driver amd_sdw_driver = {
1222d8f48fbdSVijendar Mukunda 	.probe	= &amd_sdw_manager_probe,
12231ec33e22SUwe Kleine-König 	.remove_new = &amd_sdw_manager_remove,
1224d8f48fbdSVijendar Mukunda 	.driver = {
1225d8f48fbdSVijendar Mukunda 		.name	= "amd_sdw_manager",
122681ff58ffSVijendar Mukunda 		.pm = &amd_pm,
1227d8f48fbdSVijendar Mukunda 	}
1228d8f48fbdSVijendar Mukunda };
1229d8f48fbdSVijendar Mukunda module_platform_driver(amd_sdw_driver);
1230d8f48fbdSVijendar Mukunda 
1231d8f48fbdSVijendar Mukunda MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
1232d8f48fbdSVijendar Mukunda MODULE_DESCRIPTION("AMD SoundWire driver");
1233d8f48fbdSVijendar Mukunda MODULE_LICENSE("GPL");
1234d8f48fbdSVijendar Mukunda MODULE_ALIAS("platform:" DRV_NAME);
1235