xref: /openbmc/linux/drivers/soundwire/amd_manager.c (revision 154cfc3d2738210704f90e9ac602d79044ee9580)
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 
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 
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 
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 
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 
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 
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 
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 
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 
151d8f48fbdSVijendar Mukunda static void amd_sdw_ctl_word_prep(u32 *lower_word, u32 *upper_word, struct sdw_msg *msg,
152d8f48fbdSVijendar Mukunda 				  int cmd_offset)
153d8f48fbdSVijendar Mukunda {
154d8f48fbdSVijendar Mukunda 	u32 upper_data;
155d8f48fbdSVijendar Mukunda 	u32 lower_data = 0;
156d8f48fbdSVijendar Mukunda 	u16 addr;
157d8f48fbdSVijendar Mukunda 	u8 upper_addr, lower_addr;
158d8f48fbdSVijendar Mukunda 	u8 data = 0;
159d8f48fbdSVijendar Mukunda 
160d8f48fbdSVijendar Mukunda 	addr = msg->addr + cmd_offset;
161d8f48fbdSVijendar Mukunda 	upper_addr = (addr & 0xFF00) >> 8;
162d8f48fbdSVijendar Mukunda 	lower_addr = addr & 0xFF;
163d8f48fbdSVijendar Mukunda 
164d8f48fbdSVijendar Mukunda 	if (msg->flags == SDW_MSG_FLAG_WRITE)
165d8f48fbdSVijendar Mukunda 		data = msg->buf[cmd_offset];
166d8f48fbdSVijendar Mukunda 
167d8f48fbdSVijendar Mukunda 	upper_data = FIELD_PREP(AMD_SDW_MCP_CMD_DEV_ADDR, msg->dev_num);
168d8f48fbdSVijendar Mukunda 	upper_data |= FIELD_PREP(AMD_SDW_MCP_CMD_COMMAND, msg->flags + 2);
169d8f48fbdSVijendar Mukunda 	upper_data |= FIELD_PREP(AMD_SDW_MCP_CMD_REG_ADDR_HIGH, upper_addr);
170d8f48fbdSVijendar Mukunda 	lower_data |= FIELD_PREP(AMD_SDW_MCP_CMD_REG_ADDR_LOW, lower_addr);
171d8f48fbdSVijendar Mukunda 	lower_data |= FIELD_PREP(AMD_SDW_MCP_CMD_REG_DATA, data);
172d8f48fbdSVijendar Mukunda 
173d8f48fbdSVijendar Mukunda 	*upper_word = upper_data;
174d8f48fbdSVijendar Mukunda 	*lower_word = lower_data;
175d8f48fbdSVijendar Mukunda }
176d8f48fbdSVijendar Mukunda 
177d8f48fbdSVijendar Mukunda static u64 amd_sdw_send_cmd_get_resp(struct amd_sdw_manager *amd_manager, u32 lower_data,
178d8f48fbdSVijendar Mukunda 				     u32 upper_data)
179d8f48fbdSVijendar Mukunda {
180d8f48fbdSVijendar Mukunda 	u64 resp;
181d8f48fbdSVijendar Mukunda 	u32 lower_resp, upper_resp;
182d8f48fbdSVijendar Mukunda 	u32 sts;
183d8f48fbdSVijendar Mukunda 	int ret;
184d8f48fbdSVijendar Mukunda 
185d8f48fbdSVijendar Mukunda 	ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_IMM_CMD_STS, sts,
186d8f48fbdSVijendar Mukunda 				 !(sts & AMD_SDW_IMM_CMD_BUSY), ACP_DELAY_US, AMD_SDW_TIMEOUT);
187d8f48fbdSVijendar Mukunda 	if (ret) {
188d8f48fbdSVijendar Mukunda 		dev_err(amd_manager->dev, "SDW%x previous cmd status clear failed\n",
189d8f48fbdSVijendar Mukunda 			amd_manager->instance);
190d8f48fbdSVijendar Mukunda 		return ret;
191d8f48fbdSVijendar Mukunda 	}
192d8f48fbdSVijendar Mukunda 
193d8f48fbdSVijendar Mukunda 	if (sts & AMD_SDW_IMM_RES_VALID) {
194d8f48fbdSVijendar Mukunda 		dev_err(amd_manager->dev, "SDW%x manager is in bad state\n", amd_manager->instance);
195d8f48fbdSVijendar Mukunda 		writel(0x00, amd_manager->mmio + ACP_SW_IMM_CMD_STS);
196d8f48fbdSVijendar Mukunda 	}
197d8f48fbdSVijendar Mukunda 	writel(upper_data, amd_manager->mmio + ACP_SW_IMM_CMD_UPPER_WORD);
198d8f48fbdSVijendar Mukunda 	writel(lower_data, amd_manager->mmio + ACP_SW_IMM_CMD_LOWER_QWORD);
199d8f48fbdSVijendar Mukunda 
200d8f48fbdSVijendar Mukunda 	ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_IMM_CMD_STS, sts,
201d8f48fbdSVijendar Mukunda 				 (sts & AMD_SDW_IMM_RES_VALID), ACP_DELAY_US, AMD_SDW_TIMEOUT);
202d8f48fbdSVijendar Mukunda 	if (ret) {
203d8f48fbdSVijendar Mukunda 		dev_err(amd_manager->dev, "SDW%x cmd response timeout occurred\n",
204d8f48fbdSVijendar Mukunda 			amd_manager->instance);
205d8f48fbdSVijendar Mukunda 		return ret;
206d8f48fbdSVijendar Mukunda 	}
207d8f48fbdSVijendar Mukunda 	upper_resp = readl(amd_manager->mmio + ACP_SW_IMM_RESP_UPPER_WORD);
208d8f48fbdSVijendar Mukunda 	lower_resp = readl(amd_manager->mmio + ACP_SW_IMM_RESP_LOWER_QWORD);
209d8f48fbdSVijendar Mukunda 
210d8f48fbdSVijendar Mukunda 	writel(AMD_SDW_IMM_RES_VALID, amd_manager->mmio + ACP_SW_IMM_CMD_STS);
211d8f48fbdSVijendar Mukunda 	ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_IMM_CMD_STS, sts,
212d8f48fbdSVijendar Mukunda 				 !(sts & AMD_SDW_IMM_RES_VALID), ACP_DELAY_US, AMD_SDW_TIMEOUT);
213d8f48fbdSVijendar Mukunda 	if (ret) {
214d8f48fbdSVijendar Mukunda 		dev_err(amd_manager->dev, "SDW%x cmd status retry failed\n",
215d8f48fbdSVijendar Mukunda 			amd_manager->instance);
216d8f48fbdSVijendar Mukunda 		return ret;
217d8f48fbdSVijendar Mukunda 	}
218d8f48fbdSVijendar Mukunda 	resp = upper_resp;
219d8f48fbdSVijendar Mukunda 	resp = (resp << 32) | lower_resp;
220d8f48fbdSVijendar Mukunda 	return resp;
221d8f48fbdSVijendar Mukunda }
222d8f48fbdSVijendar Mukunda 
223d8f48fbdSVijendar Mukunda static enum sdw_command_response
224d8f48fbdSVijendar Mukunda amd_program_scp_addr(struct amd_sdw_manager *amd_manager, struct sdw_msg *msg)
225d8f48fbdSVijendar Mukunda {
226d8f48fbdSVijendar Mukunda 	struct sdw_msg scp_msg = {0};
227d8f48fbdSVijendar Mukunda 	u64 response_buf[2] = {0};
228d8f48fbdSVijendar Mukunda 	u32 upper_data = 0, lower_data = 0;
229d8f48fbdSVijendar Mukunda 	int index;
230d8f48fbdSVijendar Mukunda 
231d8f48fbdSVijendar Mukunda 	scp_msg.dev_num = msg->dev_num;
232d8f48fbdSVijendar Mukunda 	scp_msg.addr = SDW_SCP_ADDRPAGE1;
233d8f48fbdSVijendar Mukunda 	scp_msg.buf = &msg->addr_page1;
234d8f48fbdSVijendar Mukunda 	scp_msg.flags = SDW_MSG_FLAG_WRITE;
235d8f48fbdSVijendar Mukunda 	amd_sdw_ctl_word_prep(&lower_data, &upper_data, &scp_msg, 0);
236d8f48fbdSVijendar Mukunda 	response_buf[0] = amd_sdw_send_cmd_get_resp(amd_manager, lower_data, upper_data);
237d8f48fbdSVijendar Mukunda 	scp_msg.addr = SDW_SCP_ADDRPAGE2;
238d8f48fbdSVijendar Mukunda 	scp_msg.buf = &msg->addr_page2;
239d8f48fbdSVijendar Mukunda 	amd_sdw_ctl_word_prep(&lower_data, &upper_data, &scp_msg, 0);
240d8f48fbdSVijendar Mukunda 	response_buf[1] = amd_sdw_send_cmd_get_resp(amd_manager, lower_data, upper_data);
241d8f48fbdSVijendar Mukunda 
242d8f48fbdSVijendar Mukunda 	for (index = 0; index < 2; index++) {
243d8f48fbdSVijendar Mukunda 		if (response_buf[index] == -ETIMEDOUT) {
244d8f48fbdSVijendar Mukunda 			dev_err_ratelimited(amd_manager->dev,
245d8f48fbdSVijendar Mukunda 					    "SCP_addrpage command timeout for Slave %d\n",
246d8f48fbdSVijendar Mukunda 					    msg->dev_num);
247d8f48fbdSVijendar Mukunda 			return SDW_CMD_TIMEOUT;
248d8f48fbdSVijendar Mukunda 		} else if (!(response_buf[index] & AMD_SDW_MCP_RESP_ACK)) {
249d8f48fbdSVijendar Mukunda 			if (response_buf[index] & AMD_SDW_MCP_RESP_NACK) {
250d8f48fbdSVijendar Mukunda 				dev_err_ratelimited(amd_manager->dev,
251d8f48fbdSVijendar Mukunda 						    "SCP_addrpage NACKed for Slave %d\n",
252d8f48fbdSVijendar Mukunda 						    msg->dev_num);
253d8f48fbdSVijendar Mukunda 				return SDW_CMD_FAIL;
254d8f48fbdSVijendar Mukunda 			}
255d8f48fbdSVijendar Mukunda 			dev_dbg_ratelimited(amd_manager->dev, "SCP_addrpage ignored for Slave %d\n",
256d8f48fbdSVijendar Mukunda 					    msg->dev_num);
257d8f48fbdSVijendar Mukunda 			return SDW_CMD_IGNORED;
258d8f48fbdSVijendar Mukunda 		}
259d8f48fbdSVijendar Mukunda 	}
260d8f48fbdSVijendar Mukunda 	return SDW_CMD_OK;
261d8f48fbdSVijendar Mukunda }
262d8f48fbdSVijendar Mukunda 
263d8f48fbdSVijendar Mukunda static int amd_prep_msg(struct amd_sdw_manager *amd_manager, struct sdw_msg *msg)
264d8f48fbdSVijendar Mukunda {
265d8f48fbdSVijendar Mukunda 	int ret;
266d8f48fbdSVijendar Mukunda 
267d8f48fbdSVijendar Mukunda 	if (msg->page) {
268d8f48fbdSVijendar Mukunda 		ret = amd_program_scp_addr(amd_manager, msg);
269d8f48fbdSVijendar Mukunda 		if (ret) {
270d8f48fbdSVijendar Mukunda 			msg->len = 0;
271d8f48fbdSVijendar Mukunda 			return ret;
272d8f48fbdSVijendar Mukunda 		}
273d8f48fbdSVijendar Mukunda 	}
274d8f48fbdSVijendar Mukunda 	switch (msg->flags) {
275d8f48fbdSVijendar Mukunda 	case SDW_MSG_FLAG_READ:
276d8f48fbdSVijendar Mukunda 	case SDW_MSG_FLAG_WRITE:
277d8f48fbdSVijendar Mukunda 		break;
278d8f48fbdSVijendar Mukunda 	default:
279d8f48fbdSVijendar Mukunda 		dev_err(amd_manager->dev, "Invalid msg cmd: %d\n", msg->flags);
280d8f48fbdSVijendar Mukunda 		return -EINVAL;
281d8f48fbdSVijendar Mukunda 	}
282d8f48fbdSVijendar Mukunda 	return 0;
283d8f48fbdSVijendar Mukunda }
284d8f48fbdSVijendar Mukunda 
285d8f48fbdSVijendar Mukunda static enum sdw_command_response amd_sdw_fill_msg_resp(struct amd_sdw_manager *amd_manager,
286d8f48fbdSVijendar Mukunda 						       struct sdw_msg *msg, u64 response,
287d8f48fbdSVijendar Mukunda 						       int offset)
288d8f48fbdSVijendar Mukunda {
289d8f48fbdSVijendar Mukunda 	if (response & AMD_SDW_MCP_RESP_ACK) {
290d8f48fbdSVijendar Mukunda 		if (msg->flags == SDW_MSG_FLAG_READ)
291d8f48fbdSVijendar Mukunda 			msg->buf[offset] = FIELD_GET(AMD_SDW_MCP_RESP_RDATA, response);
292d8f48fbdSVijendar Mukunda 	} else {
293d8f48fbdSVijendar Mukunda 		if (response == -ETIMEDOUT) {
294d8f48fbdSVijendar Mukunda 			dev_err_ratelimited(amd_manager->dev, "command timeout for Slave %d\n",
295d8f48fbdSVijendar Mukunda 					    msg->dev_num);
296d8f48fbdSVijendar Mukunda 			return SDW_CMD_TIMEOUT;
297d8f48fbdSVijendar Mukunda 		} else if (response & AMD_SDW_MCP_RESP_NACK) {
298d8f48fbdSVijendar Mukunda 			dev_err_ratelimited(amd_manager->dev,
299d8f48fbdSVijendar Mukunda 					    "command response NACK received for Slave %d\n",
300d8f48fbdSVijendar Mukunda 					    msg->dev_num);
301d8f48fbdSVijendar Mukunda 			return SDW_CMD_FAIL;
302d8f48fbdSVijendar Mukunda 		}
303d8f48fbdSVijendar Mukunda 		dev_err_ratelimited(amd_manager->dev, "command is ignored for Slave %d\n",
304d8f48fbdSVijendar Mukunda 				    msg->dev_num);
305d8f48fbdSVijendar Mukunda 		return SDW_CMD_IGNORED;
306d8f48fbdSVijendar Mukunda 	}
307d8f48fbdSVijendar Mukunda 	return SDW_CMD_OK;
308d8f48fbdSVijendar Mukunda }
309d8f48fbdSVijendar Mukunda 
310d8f48fbdSVijendar Mukunda static unsigned int _amd_sdw_xfer_msg(struct amd_sdw_manager *amd_manager, struct sdw_msg *msg,
311d8f48fbdSVijendar Mukunda 				      int cmd_offset)
312d8f48fbdSVijendar Mukunda {
313d8f48fbdSVijendar Mukunda 	u64 response;
314d8f48fbdSVijendar Mukunda 	u32 upper_data = 0, lower_data = 0;
315d8f48fbdSVijendar Mukunda 
316d8f48fbdSVijendar Mukunda 	amd_sdw_ctl_word_prep(&lower_data, &upper_data, msg, cmd_offset);
317d8f48fbdSVijendar Mukunda 	response = amd_sdw_send_cmd_get_resp(amd_manager, lower_data, upper_data);
318d8f48fbdSVijendar Mukunda 	return amd_sdw_fill_msg_resp(amd_manager, msg, response, cmd_offset);
319d8f48fbdSVijendar Mukunda }
320d8f48fbdSVijendar Mukunda 
321d8f48fbdSVijendar Mukunda static enum sdw_command_response amd_sdw_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
322d8f48fbdSVijendar Mukunda {
323d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = to_amd_sdw(bus);
324d8f48fbdSVijendar Mukunda 	int ret, i;
325d8f48fbdSVijendar Mukunda 
326d8f48fbdSVijendar Mukunda 	ret = amd_prep_msg(amd_manager, msg);
327d8f48fbdSVijendar Mukunda 	if (ret)
328d8f48fbdSVijendar Mukunda 		return SDW_CMD_FAIL_OTHER;
329d8f48fbdSVijendar Mukunda 	for (i = 0; i < msg->len; i++) {
330d8f48fbdSVijendar Mukunda 		ret = _amd_sdw_xfer_msg(amd_manager, msg, i);
331d8f48fbdSVijendar Mukunda 		if (ret)
332d8f48fbdSVijendar Mukunda 			return ret;
333d8f48fbdSVijendar Mukunda 	}
334d8f48fbdSVijendar Mukunda 	return SDW_CMD_OK;
335d8f48fbdSVijendar Mukunda }
336d8f48fbdSVijendar Mukunda 
33765f93e40SVijendar Mukunda static void amd_sdw_fill_slave_status(struct amd_sdw_manager *amd_manager, u16 index, u32 status)
33865f93e40SVijendar Mukunda {
33965f93e40SVijendar Mukunda 	switch (status) {
34065f93e40SVijendar Mukunda 	case SDW_SLAVE_ATTACHED:
34165f93e40SVijendar Mukunda 	case SDW_SLAVE_UNATTACHED:
34265f93e40SVijendar Mukunda 	case SDW_SLAVE_ALERT:
34365f93e40SVijendar Mukunda 		amd_manager->status[index] = status;
34465f93e40SVijendar Mukunda 		break;
34565f93e40SVijendar Mukunda 	default:
34665f93e40SVijendar Mukunda 		amd_manager->status[index] = SDW_SLAVE_RESERVED;
34765f93e40SVijendar Mukunda 		break;
34865f93e40SVijendar Mukunda 	}
34965f93e40SVijendar Mukunda }
35065f93e40SVijendar Mukunda 
35165f93e40SVijendar Mukunda static void amd_sdw_process_ping_status(u64 response, struct amd_sdw_manager *amd_manager)
35265f93e40SVijendar Mukunda {
35365f93e40SVijendar Mukunda 	u64 slave_stat;
35465f93e40SVijendar Mukunda 	u32 val;
35565f93e40SVijendar Mukunda 	u16 dev_index;
35665f93e40SVijendar Mukunda 
35765f93e40SVijendar Mukunda 	/* slave status response */
35865f93e40SVijendar Mukunda 	slave_stat = FIELD_GET(AMD_SDW_MCP_SLAVE_STAT_0_3, response);
35965f93e40SVijendar Mukunda 	slave_stat |= FIELD_GET(AMD_SDW_MCP_SLAVE_STAT_4_11, response) << 8;
36065f93e40SVijendar Mukunda 	dev_dbg(amd_manager->dev, "slave_stat:0x%llx\n", slave_stat);
36165f93e40SVijendar Mukunda 	for (dev_index = 0; dev_index <= SDW_MAX_DEVICES; ++dev_index) {
36265f93e40SVijendar Mukunda 		val = (slave_stat >> (dev_index * 2)) & AMD_SDW_MCP_SLAVE_STATUS_MASK;
36365f93e40SVijendar Mukunda 		dev_dbg(amd_manager->dev, "val:0x%x\n", val);
36465f93e40SVijendar Mukunda 		amd_sdw_fill_slave_status(amd_manager, dev_index, val);
36565f93e40SVijendar Mukunda 	}
36665f93e40SVijendar Mukunda }
36765f93e40SVijendar Mukunda 
36865f93e40SVijendar Mukunda static void amd_sdw_read_and_process_ping_status(struct amd_sdw_manager *amd_manager)
36965f93e40SVijendar Mukunda {
37065f93e40SVijendar Mukunda 	u64 response;
37165f93e40SVijendar Mukunda 
37265f93e40SVijendar Mukunda 	mutex_lock(&amd_manager->bus.msg_lock);
37365f93e40SVijendar Mukunda 	response = amd_sdw_send_cmd_get_resp(amd_manager, 0, 0);
37465f93e40SVijendar Mukunda 	mutex_unlock(&amd_manager->bus.msg_lock);
37565f93e40SVijendar Mukunda 	amd_sdw_process_ping_status(response, amd_manager);
37665f93e40SVijendar Mukunda }
37765f93e40SVijendar Mukunda 
378d8f48fbdSVijendar Mukunda static u32 amd_sdw_read_ping_status(struct sdw_bus *bus)
379d8f48fbdSVijendar Mukunda {
380d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = to_amd_sdw(bus);
381d8f48fbdSVijendar Mukunda 	u64 response;
382d8f48fbdSVijendar Mukunda 	u32 slave_stat;
383d8f48fbdSVijendar Mukunda 
384d8f48fbdSVijendar Mukunda 	response = amd_sdw_send_cmd_get_resp(amd_manager, 0, 0);
385d8f48fbdSVijendar Mukunda 	/* slave status from ping response */
386d8f48fbdSVijendar Mukunda 	slave_stat = FIELD_GET(AMD_SDW_MCP_SLAVE_STAT_0_3, response);
387d8f48fbdSVijendar Mukunda 	slave_stat |= FIELD_GET(AMD_SDW_MCP_SLAVE_STAT_4_11, response) << 8;
388d8f48fbdSVijendar Mukunda 	dev_dbg(amd_manager->dev, "slave_stat:0x%x\n", slave_stat);
389d8f48fbdSVijendar Mukunda 	return slave_stat;
390d8f48fbdSVijendar Mukunda }
391d8f48fbdSVijendar Mukunda 
392d8f48fbdSVijendar Mukunda static int amd_sdw_compute_params(struct sdw_bus *bus)
393d8f48fbdSVijendar Mukunda {
394d8f48fbdSVijendar Mukunda 	struct sdw_transport_data t_data = {0};
395d8f48fbdSVijendar Mukunda 	struct sdw_master_runtime *m_rt;
396d8f48fbdSVijendar Mukunda 	struct sdw_port_runtime *p_rt;
397d8f48fbdSVijendar Mukunda 	struct sdw_bus_params *b_params = &bus->params;
398d8f48fbdSVijendar Mukunda 	int port_bo, hstart, hstop, sample_int;
399d8f48fbdSVijendar Mukunda 	unsigned int rate, bps;
400d8f48fbdSVijendar Mukunda 
401d8f48fbdSVijendar Mukunda 	port_bo = 0;
402d8f48fbdSVijendar Mukunda 	hstart = 1;
403d8f48fbdSVijendar Mukunda 	hstop = bus->params.col - 1;
404d8f48fbdSVijendar Mukunda 	t_data.hstop = hstop;
405d8f48fbdSVijendar Mukunda 	t_data.hstart = hstart;
406d8f48fbdSVijendar Mukunda 
407d8f48fbdSVijendar Mukunda 	list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
408d8f48fbdSVijendar Mukunda 		rate = m_rt->stream->params.rate;
409d8f48fbdSVijendar Mukunda 		bps = m_rt->stream->params.bps;
410d8f48fbdSVijendar Mukunda 		sample_int = (bus->params.curr_dr_freq / rate);
411d8f48fbdSVijendar Mukunda 		list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
412d8f48fbdSVijendar Mukunda 			port_bo = (p_rt->num * 64) + 1;
413d8f48fbdSVijendar Mukunda 			dev_dbg(bus->dev, "p_rt->num=%d hstart=%d hstop=%d port_bo=%d\n",
414d8f48fbdSVijendar Mukunda 				p_rt->num, hstart, hstop, port_bo);
415d8f48fbdSVijendar Mukunda 			sdw_fill_xport_params(&p_rt->transport_params, p_rt->num,
416d8f48fbdSVijendar Mukunda 					      false, SDW_BLK_GRP_CNT_1, sample_int,
417d8f48fbdSVijendar Mukunda 					      port_bo, port_bo >> 8, hstart, hstop,
418d8f48fbdSVijendar Mukunda 					      SDW_BLK_PKG_PER_PORT, 0x0);
419d8f48fbdSVijendar Mukunda 
420d8f48fbdSVijendar Mukunda 			sdw_fill_port_params(&p_rt->port_params,
421d8f48fbdSVijendar Mukunda 					     p_rt->num, bps,
422d8f48fbdSVijendar Mukunda 					     SDW_PORT_FLOW_MODE_ISOCH,
423d8f48fbdSVijendar Mukunda 					     b_params->m_data_mode);
424d8f48fbdSVijendar Mukunda 			t_data.hstart = hstart;
425d8f48fbdSVijendar Mukunda 			t_data.hstop = hstop;
426d8f48fbdSVijendar Mukunda 			t_data.block_offset = port_bo;
427d8f48fbdSVijendar Mukunda 			t_data.sub_block_offset = 0;
428d8f48fbdSVijendar Mukunda 		}
429d8f48fbdSVijendar Mukunda 		sdw_compute_slave_ports(m_rt, &t_data);
430d8f48fbdSVijendar Mukunda 	}
431d8f48fbdSVijendar Mukunda 	return 0;
432d8f48fbdSVijendar Mukunda }
433d8f48fbdSVijendar Mukunda 
434d8f48fbdSVijendar Mukunda static int amd_sdw_port_params(struct sdw_bus *bus, struct sdw_port_params *p_params,
435d8f48fbdSVijendar Mukunda 			       unsigned int bank)
436d8f48fbdSVijendar Mukunda {
437d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = to_amd_sdw(bus);
438d8f48fbdSVijendar Mukunda 	u32 frame_fmt_reg, dpn_frame_fmt;
439d8f48fbdSVijendar Mukunda 
440d8f48fbdSVijendar Mukunda 	dev_dbg(amd_manager->dev, "p_params->num:0x%x\n", p_params->num);
441d8f48fbdSVijendar Mukunda 	switch (amd_manager->instance) {
442d8f48fbdSVijendar Mukunda 	case ACP_SDW0:
443d8f48fbdSVijendar Mukunda 		frame_fmt_reg = sdw0_manager_dp_reg[p_params->num].frame_fmt_reg;
444d8f48fbdSVijendar Mukunda 		break;
445d8f48fbdSVijendar Mukunda 	case ACP_SDW1:
446d8f48fbdSVijendar Mukunda 		frame_fmt_reg = sdw1_manager_dp_reg[p_params->num].frame_fmt_reg;
447d8f48fbdSVijendar Mukunda 		break;
448d8f48fbdSVijendar Mukunda 	default:
449d8f48fbdSVijendar Mukunda 		return -EINVAL;
450d8f48fbdSVijendar Mukunda 	}
451d8f48fbdSVijendar Mukunda 
452d8f48fbdSVijendar Mukunda 	dpn_frame_fmt = readl(amd_manager->mmio + frame_fmt_reg);
453d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_frame_fmt, p_params->flow_mode, AMD_DPN_FRAME_FMT_PFM);
454d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_frame_fmt, p_params->data_mode, AMD_DPN_FRAME_FMT_PDM);
455d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_frame_fmt, p_params->bps - 1, AMD_DPN_FRAME_FMT_WORD_LEN);
456d8f48fbdSVijendar Mukunda 	writel(dpn_frame_fmt, amd_manager->mmio + frame_fmt_reg);
457d8f48fbdSVijendar Mukunda 	return 0;
458d8f48fbdSVijendar Mukunda }
459d8f48fbdSVijendar Mukunda 
460d8f48fbdSVijendar Mukunda static int amd_sdw_transport_params(struct sdw_bus *bus,
461d8f48fbdSVijendar Mukunda 				    struct sdw_transport_params *params,
462d8f48fbdSVijendar Mukunda 				    enum sdw_reg_bank bank)
463d8f48fbdSVijendar Mukunda {
464d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = to_amd_sdw(bus);
465d8f48fbdSVijendar Mukunda 	u32 dpn_frame_fmt;
466d8f48fbdSVijendar Mukunda 	u32 dpn_sampleinterval;
467d8f48fbdSVijendar Mukunda 	u32 dpn_hctrl;
468d8f48fbdSVijendar Mukunda 	u32 dpn_offsetctrl;
469d8f48fbdSVijendar Mukunda 	u32 dpn_lanectrl;
470d8f48fbdSVijendar Mukunda 	u32 frame_fmt_reg, sample_int_reg, hctrl_dp0_reg;
471d8f48fbdSVijendar Mukunda 	u32 offset_reg, lane_ctrl_ch_en_reg;
472d8f48fbdSVijendar Mukunda 
473d8f48fbdSVijendar Mukunda 	switch (amd_manager->instance) {
474d8f48fbdSVijendar Mukunda 	case ACP_SDW0:
475d8f48fbdSVijendar Mukunda 		frame_fmt_reg = sdw0_manager_dp_reg[params->port_num].frame_fmt_reg;
476d8f48fbdSVijendar Mukunda 		sample_int_reg = sdw0_manager_dp_reg[params->port_num].sample_int_reg;
477d8f48fbdSVijendar Mukunda 		hctrl_dp0_reg = sdw0_manager_dp_reg[params->port_num].hctrl_dp0_reg;
478d8f48fbdSVijendar Mukunda 		offset_reg = sdw0_manager_dp_reg[params->port_num].offset_reg;
479d8f48fbdSVijendar Mukunda 		lane_ctrl_ch_en_reg = sdw0_manager_dp_reg[params->port_num].lane_ctrl_ch_en_reg;
480d8f48fbdSVijendar Mukunda 		break;
481d8f48fbdSVijendar Mukunda 	case ACP_SDW1:
482d8f48fbdSVijendar Mukunda 		frame_fmt_reg = sdw1_manager_dp_reg[params->port_num].frame_fmt_reg;
483d8f48fbdSVijendar Mukunda 		sample_int_reg = sdw1_manager_dp_reg[params->port_num].sample_int_reg;
484d8f48fbdSVijendar Mukunda 		hctrl_dp0_reg = sdw1_manager_dp_reg[params->port_num].hctrl_dp0_reg;
485d8f48fbdSVijendar Mukunda 		offset_reg = sdw1_manager_dp_reg[params->port_num].offset_reg;
486d8f48fbdSVijendar Mukunda 		lane_ctrl_ch_en_reg = sdw1_manager_dp_reg[params->port_num].lane_ctrl_ch_en_reg;
487d8f48fbdSVijendar Mukunda 		break;
488d8f48fbdSVijendar Mukunda 	default:
489d8f48fbdSVijendar Mukunda 		return -EINVAL;
490d8f48fbdSVijendar Mukunda 	}
491d8f48fbdSVijendar Mukunda 	writel(AMD_SDW_SSP_COUNTER_VAL, amd_manager->mmio + ACP_SW_SSP_COUNTER);
492d8f48fbdSVijendar Mukunda 
493d8f48fbdSVijendar Mukunda 	dpn_frame_fmt = readl(amd_manager->mmio + frame_fmt_reg);
494d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_frame_fmt, params->blk_pkg_mode, AMD_DPN_FRAME_FMT_BLK_PKG_MODE);
495d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_frame_fmt, params->blk_grp_ctrl, AMD_DPN_FRAME_FMT_BLK_GRP_CTRL);
496d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_frame_fmt, SDW_STREAM_PCM, AMD_DPN_FRAME_FMT_PCM_OR_PDM);
497d8f48fbdSVijendar Mukunda 	writel(dpn_frame_fmt, amd_manager->mmio + frame_fmt_reg);
498d8f48fbdSVijendar Mukunda 
499d8f48fbdSVijendar Mukunda 	dpn_sampleinterval = params->sample_interval - 1;
500d8f48fbdSVijendar Mukunda 	writel(dpn_sampleinterval, amd_manager->mmio + sample_int_reg);
501d8f48fbdSVijendar Mukunda 
502d8f48fbdSVijendar Mukunda 	dpn_hctrl = FIELD_PREP(AMD_DPN_HCTRL_HSTOP, params->hstop);
503d8f48fbdSVijendar Mukunda 	dpn_hctrl |= FIELD_PREP(AMD_DPN_HCTRL_HSTART, params->hstart);
504d8f48fbdSVijendar Mukunda 	writel(dpn_hctrl, amd_manager->mmio + hctrl_dp0_reg);
505d8f48fbdSVijendar Mukunda 
506d8f48fbdSVijendar Mukunda 	dpn_offsetctrl = FIELD_PREP(AMD_DPN_OFFSET_CTRL_1, params->offset1);
507d8f48fbdSVijendar Mukunda 	dpn_offsetctrl |= FIELD_PREP(AMD_DPN_OFFSET_CTRL_2, params->offset2);
508d8f48fbdSVijendar Mukunda 	writel(dpn_offsetctrl, amd_manager->mmio + offset_reg);
509d8f48fbdSVijendar Mukunda 
510d8f48fbdSVijendar Mukunda 	/*
511d8f48fbdSVijendar Mukunda 	 * lane_ctrl_ch_en_reg will be used to program lane_ctrl and ch_mask
512d8f48fbdSVijendar Mukunda 	 * parameters.
513d8f48fbdSVijendar Mukunda 	 */
514d8f48fbdSVijendar Mukunda 	dpn_lanectrl = readl(amd_manager->mmio + lane_ctrl_ch_en_reg);
515d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_lanectrl, params->lane_ctrl, AMD_DPN_CH_EN_LCTRL);
516d8f48fbdSVijendar Mukunda 	writel(dpn_lanectrl, amd_manager->mmio + lane_ctrl_ch_en_reg);
517d8f48fbdSVijendar Mukunda 	return 0;
518d8f48fbdSVijendar Mukunda }
519d8f48fbdSVijendar Mukunda 
520d8f48fbdSVijendar Mukunda static int amd_sdw_port_enable(struct sdw_bus *bus,
521d8f48fbdSVijendar Mukunda 			       struct sdw_enable_ch *enable_ch,
522d8f48fbdSVijendar Mukunda 			       unsigned int bank)
523d8f48fbdSVijendar Mukunda {
524d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = to_amd_sdw(bus);
525d8f48fbdSVijendar Mukunda 	u32 dpn_ch_enable;
526d8f48fbdSVijendar Mukunda 	u32 lane_ctrl_ch_en_reg;
527d8f48fbdSVijendar Mukunda 
528d8f48fbdSVijendar Mukunda 	switch (amd_manager->instance) {
529d8f48fbdSVijendar Mukunda 	case ACP_SDW0:
530d8f48fbdSVijendar Mukunda 		lane_ctrl_ch_en_reg = sdw0_manager_dp_reg[enable_ch->port_num].lane_ctrl_ch_en_reg;
531d8f48fbdSVijendar Mukunda 		break;
532d8f48fbdSVijendar Mukunda 	case ACP_SDW1:
533d8f48fbdSVijendar Mukunda 		lane_ctrl_ch_en_reg = sdw1_manager_dp_reg[enable_ch->port_num].lane_ctrl_ch_en_reg;
534d8f48fbdSVijendar Mukunda 		break;
535d8f48fbdSVijendar Mukunda 	default:
536d8f48fbdSVijendar Mukunda 		return -EINVAL;
537d8f48fbdSVijendar Mukunda 	}
538d8f48fbdSVijendar Mukunda 
539d8f48fbdSVijendar Mukunda 	/*
540d8f48fbdSVijendar Mukunda 	 * lane_ctrl_ch_en_reg will be used to program lane_ctrl and ch_mask
541d8f48fbdSVijendar Mukunda 	 * parameters.
542d8f48fbdSVijendar Mukunda 	 */
543d8f48fbdSVijendar Mukunda 	dpn_ch_enable = readl(amd_manager->mmio + lane_ctrl_ch_en_reg);
544d8f48fbdSVijendar Mukunda 	u32p_replace_bits(&dpn_ch_enable, enable_ch->ch_mask, AMD_DPN_CH_EN_CHMASK);
545d8f48fbdSVijendar Mukunda 	if (enable_ch->enable)
546d8f48fbdSVijendar Mukunda 		writel(dpn_ch_enable, amd_manager->mmio + lane_ctrl_ch_en_reg);
547d8f48fbdSVijendar Mukunda 	else
548d8f48fbdSVijendar Mukunda 		writel(0, amd_manager->mmio + lane_ctrl_ch_en_reg);
549d8f48fbdSVijendar Mukunda 	return 0;
550d8f48fbdSVijendar Mukunda }
551d8f48fbdSVijendar Mukunda 
552d8f48fbdSVijendar Mukunda static int sdw_master_read_amd_prop(struct sdw_bus *bus)
553d8f48fbdSVijendar Mukunda {
554d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = to_amd_sdw(bus);
555d8f48fbdSVijendar Mukunda 	struct fwnode_handle *link;
556d8f48fbdSVijendar Mukunda 	struct sdw_master_prop *prop;
557d8f48fbdSVijendar Mukunda 	u32 quirk_mask = 0;
558d8f48fbdSVijendar Mukunda 	u32 wake_en_mask = 0;
559d8f48fbdSVijendar Mukunda 	u32 power_mode_mask = 0;
560d8f48fbdSVijendar Mukunda 	char name[32];
561d8f48fbdSVijendar Mukunda 
562d8f48fbdSVijendar Mukunda 	prop = &bus->prop;
563d8f48fbdSVijendar Mukunda 	/* Find manager handle */
564d8f48fbdSVijendar Mukunda 	snprintf(name, sizeof(name), "mipi-sdw-link-%d-subproperties", bus->link_id);
565d8f48fbdSVijendar Mukunda 	link = device_get_named_child_node(bus->dev, name);
566d8f48fbdSVijendar Mukunda 	if (!link) {
567d8f48fbdSVijendar Mukunda 		dev_err(bus->dev, "Manager node %s not found\n", name);
568d8f48fbdSVijendar Mukunda 		return -EIO;
569d8f48fbdSVijendar Mukunda 	}
570d8f48fbdSVijendar Mukunda 	fwnode_property_read_u32(link, "amd-sdw-enable", &quirk_mask);
571d8f48fbdSVijendar Mukunda 	if (!(quirk_mask & AMD_SDW_QUIRK_MASK_BUS_ENABLE))
572d8f48fbdSVijendar Mukunda 		prop->hw_disabled = true;
573d8f48fbdSVijendar Mukunda 	prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
574d8f48fbdSVijendar Mukunda 		       SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
575d8f48fbdSVijendar Mukunda 
576d8f48fbdSVijendar Mukunda 	fwnode_property_read_u32(link, "amd-sdw-wakeup-enable", &wake_en_mask);
577d8f48fbdSVijendar Mukunda 	amd_manager->wake_en_mask = wake_en_mask;
578d8f48fbdSVijendar Mukunda 	fwnode_property_read_u32(link, "amd-sdw-power-mode", &power_mode_mask);
579d8f48fbdSVijendar Mukunda 	amd_manager->power_mode_mask = power_mode_mask;
580d8f48fbdSVijendar Mukunda 	return 0;
581d8f48fbdSVijendar Mukunda }
582d8f48fbdSVijendar Mukunda 
583d8f48fbdSVijendar Mukunda static int amd_prop_read(struct sdw_bus *bus)
584d8f48fbdSVijendar Mukunda {
585d8f48fbdSVijendar Mukunda 	sdw_master_read_prop(bus);
586d8f48fbdSVijendar Mukunda 	sdw_master_read_amd_prop(bus);
587d8f48fbdSVijendar Mukunda 	return 0;
588d8f48fbdSVijendar Mukunda }
589d8f48fbdSVijendar Mukunda 
590d8f48fbdSVijendar Mukunda static const struct sdw_master_port_ops amd_sdw_port_ops = {
591d8f48fbdSVijendar Mukunda 	.dpn_set_port_params = amd_sdw_port_params,
592d8f48fbdSVijendar Mukunda 	.dpn_set_port_transport_params = amd_sdw_transport_params,
593d8f48fbdSVijendar Mukunda 	.dpn_port_enable_ch = amd_sdw_port_enable,
594d8f48fbdSVijendar Mukunda };
595d8f48fbdSVijendar Mukunda 
596d8f48fbdSVijendar Mukunda static const struct sdw_master_ops amd_sdw_ops = {
597d8f48fbdSVijendar Mukunda 	.read_prop = amd_prop_read,
598d8f48fbdSVijendar Mukunda 	.xfer_msg = amd_sdw_xfer_msg,
599d8f48fbdSVijendar Mukunda 	.read_ping_status = amd_sdw_read_ping_status,
600d8f48fbdSVijendar Mukunda };
601d8f48fbdSVijendar Mukunda 
6022b13596fSVijendar Mukunda static int amd_sdw_hw_params(struct snd_pcm_substream *substream,
6032b13596fSVijendar Mukunda 			     struct snd_pcm_hw_params *params,
6042b13596fSVijendar Mukunda 			     struct snd_soc_dai *dai)
6052b13596fSVijendar Mukunda {
6062b13596fSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = snd_soc_dai_get_drvdata(dai);
6072b13596fSVijendar Mukunda 	struct sdw_amd_dai_runtime *dai_runtime;
6082b13596fSVijendar Mukunda 	struct sdw_stream_config sconfig;
6092b13596fSVijendar Mukunda 	struct sdw_port_config *pconfig;
6102b13596fSVijendar Mukunda 	int ch, dir;
6112b13596fSVijendar Mukunda 	int ret;
6122b13596fSVijendar Mukunda 
6132b13596fSVijendar Mukunda 	dai_runtime = amd_manager->dai_runtime_array[dai->id];
6142b13596fSVijendar Mukunda 	if (!dai_runtime)
6152b13596fSVijendar Mukunda 		return -EIO;
6162b13596fSVijendar Mukunda 
6172b13596fSVijendar Mukunda 	ch = params_channels(params);
6182b13596fSVijendar Mukunda 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
6192b13596fSVijendar Mukunda 		dir = SDW_DATA_DIR_RX;
6202b13596fSVijendar Mukunda 	else
6212b13596fSVijendar Mukunda 		dir = SDW_DATA_DIR_TX;
6222b13596fSVijendar Mukunda 	dev_dbg(amd_manager->dev, "dir:%d dai->id:0x%x\n", dir, dai->id);
6232b13596fSVijendar Mukunda 
6242b13596fSVijendar Mukunda 	sconfig.direction = dir;
6252b13596fSVijendar Mukunda 	sconfig.ch_count = ch;
6262b13596fSVijendar Mukunda 	sconfig.frame_rate = params_rate(params);
6272b13596fSVijendar Mukunda 	sconfig.type = dai_runtime->stream_type;
6282b13596fSVijendar Mukunda 
6292b13596fSVijendar Mukunda 	sconfig.bps = snd_pcm_format_width(params_format(params));
6302b13596fSVijendar Mukunda 
6312b13596fSVijendar Mukunda 	/* Port configuration */
6322b13596fSVijendar Mukunda 	pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL);
6332b13596fSVijendar Mukunda 	if (!pconfig) {
6342b13596fSVijendar Mukunda 		ret =  -ENOMEM;
6352b13596fSVijendar Mukunda 		goto error;
6362b13596fSVijendar Mukunda 	}
6372b13596fSVijendar Mukunda 
6382b13596fSVijendar Mukunda 	pconfig->num = dai->id;
6392b13596fSVijendar Mukunda 	pconfig->ch_mask = (1 << ch) - 1;
6402b13596fSVijendar Mukunda 	ret = sdw_stream_add_master(&amd_manager->bus, &sconfig,
6412b13596fSVijendar Mukunda 				    pconfig, 1, dai_runtime->stream);
6422b13596fSVijendar Mukunda 	if (ret)
6432b13596fSVijendar Mukunda 		dev_err(amd_manager->dev, "add manager to stream failed:%d\n", ret);
6442b13596fSVijendar Mukunda 
6452b13596fSVijendar Mukunda 	kfree(pconfig);
6462b13596fSVijendar Mukunda error:
6472b13596fSVijendar Mukunda 	return ret;
6482b13596fSVijendar Mukunda }
6492b13596fSVijendar Mukunda 
6502b13596fSVijendar Mukunda static int amd_sdw_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
6512b13596fSVijendar Mukunda {
6522b13596fSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = snd_soc_dai_get_drvdata(dai);
6532b13596fSVijendar Mukunda 	struct sdw_amd_dai_runtime *dai_runtime;
6542b13596fSVijendar Mukunda 	int ret;
6552b13596fSVijendar Mukunda 
6562b13596fSVijendar Mukunda 	dai_runtime = amd_manager->dai_runtime_array[dai->id];
6572b13596fSVijendar Mukunda 	if (!dai_runtime)
6582b13596fSVijendar Mukunda 		return -EIO;
6592b13596fSVijendar Mukunda 
6602b13596fSVijendar Mukunda 	ret = sdw_stream_remove_master(&amd_manager->bus, dai_runtime->stream);
6612b13596fSVijendar Mukunda 	if (ret < 0)
6622b13596fSVijendar Mukunda 		dev_err(dai->dev, "remove manager from stream %s failed: %d\n",
6632b13596fSVijendar Mukunda 			dai_runtime->stream->name, ret);
6642b13596fSVijendar Mukunda 	return ret;
6652b13596fSVijendar Mukunda }
6662b13596fSVijendar Mukunda 
6672b13596fSVijendar Mukunda static int amd_set_sdw_stream(struct snd_soc_dai *dai, void *stream, int direction)
6682b13596fSVijendar Mukunda {
6692b13596fSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = snd_soc_dai_get_drvdata(dai);
6702b13596fSVijendar Mukunda 	struct sdw_amd_dai_runtime *dai_runtime;
6712b13596fSVijendar Mukunda 
6722b13596fSVijendar Mukunda 	dai_runtime = amd_manager->dai_runtime_array[dai->id];
6732b13596fSVijendar Mukunda 	if (stream) {
6742b13596fSVijendar Mukunda 		/* first paranoia check */
6752b13596fSVijendar Mukunda 		if (dai_runtime) {
6762b13596fSVijendar Mukunda 			dev_err(dai->dev, "dai_runtime already allocated for dai %s\n",	dai->name);
6772b13596fSVijendar Mukunda 			return -EINVAL;
6782b13596fSVijendar Mukunda 		}
6792b13596fSVijendar Mukunda 
6802b13596fSVijendar Mukunda 		/* allocate and set dai_runtime info */
6812b13596fSVijendar Mukunda 		dai_runtime = kzalloc(sizeof(*dai_runtime), GFP_KERNEL);
6822b13596fSVijendar Mukunda 		if (!dai_runtime)
6832b13596fSVijendar Mukunda 			return -ENOMEM;
6842b13596fSVijendar Mukunda 
6852b13596fSVijendar Mukunda 		dai_runtime->stream_type = SDW_STREAM_PCM;
6862b13596fSVijendar Mukunda 		dai_runtime->bus = &amd_manager->bus;
6872b13596fSVijendar Mukunda 		dai_runtime->stream = stream;
6882b13596fSVijendar Mukunda 		amd_manager->dai_runtime_array[dai->id] = dai_runtime;
6892b13596fSVijendar Mukunda 	} else {
6902b13596fSVijendar Mukunda 		/* second paranoia check */
6912b13596fSVijendar Mukunda 		if (!dai_runtime) {
6922b13596fSVijendar Mukunda 			dev_err(dai->dev, "dai_runtime not allocated for dai %s\n", dai->name);
6932b13596fSVijendar Mukunda 			return -EINVAL;
6942b13596fSVijendar Mukunda 		}
6952b13596fSVijendar Mukunda 
6962b13596fSVijendar Mukunda 		/* for NULL stream we release allocated dai_runtime */
6972b13596fSVijendar Mukunda 		kfree(dai_runtime);
6982b13596fSVijendar Mukunda 		amd_manager->dai_runtime_array[dai->id] = NULL;
6992b13596fSVijendar Mukunda 	}
7002b13596fSVijendar Mukunda 	return 0;
7012b13596fSVijendar Mukunda }
7022b13596fSVijendar Mukunda 
7032b13596fSVijendar Mukunda static int amd_pcm_set_sdw_stream(struct snd_soc_dai *dai, void *stream, int direction)
7042b13596fSVijendar Mukunda {
7052b13596fSVijendar Mukunda 	return amd_set_sdw_stream(dai, stream, direction);
7062b13596fSVijendar Mukunda }
7072b13596fSVijendar Mukunda 
7082b13596fSVijendar Mukunda static void *amd_get_sdw_stream(struct snd_soc_dai *dai, int direction)
7092b13596fSVijendar Mukunda {
7102b13596fSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = snd_soc_dai_get_drvdata(dai);
7112b13596fSVijendar Mukunda 	struct sdw_amd_dai_runtime *dai_runtime;
7122b13596fSVijendar Mukunda 
7132b13596fSVijendar Mukunda 	dai_runtime = amd_manager->dai_runtime_array[dai->id];
7142b13596fSVijendar Mukunda 	if (!dai_runtime)
7152b13596fSVijendar Mukunda 		return ERR_PTR(-EINVAL);
7162b13596fSVijendar Mukunda 
7172b13596fSVijendar Mukunda 	return dai_runtime->stream;
7182b13596fSVijendar Mukunda }
7192b13596fSVijendar Mukunda 
7202b13596fSVijendar Mukunda static const struct snd_soc_dai_ops amd_sdw_dai_ops = {
7212b13596fSVijendar Mukunda 	.hw_params = amd_sdw_hw_params,
7222b13596fSVijendar Mukunda 	.hw_free = amd_sdw_hw_free,
7232b13596fSVijendar Mukunda 	.set_stream = amd_pcm_set_sdw_stream,
7242b13596fSVijendar Mukunda 	.get_stream = amd_get_sdw_stream,
7252b13596fSVijendar Mukunda };
7262b13596fSVijendar Mukunda 
7272b13596fSVijendar Mukunda static const struct snd_soc_component_driver amd_sdw_dai_component = {
7282b13596fSVijendar Mukunda 	.name = "soundwire",
7292b13596fSVijendar Mukunda };
7302b13596fSVijendar Mukunda 
7312b13596fSVijendar Mukunda static int amd_sdw_register_dais(struct amd_sdw_manager *amd_manager)
7322b13596fSVijendar Mukunda {
7332b13596fSVijendar Mukunda 	struct sdw_amd_dai_runtime **dai_runtime_array;
7342b13596fSVijendar Mukunda 	struct snd_soc_dai_driver *dais;
7352b13596fSVijendar Mukunda 	struct snd_soc_pcm_stream *stream;
7362b13596fSVijendar Mukunda 	struct device *dev;
7372b13596fSVijendar Mukunda 	int i, num_dais;
7382b13596fSVijendar Mukunda 
7392b13596fSVijendar Mukunda 	dev = amd_manager->dev;
7402b13596fSVijendar Mukunda 	num_dais = amd_manager->num_dout_ports + amd_manager->num_din_ports;
7412b13596fSVijendar Mukunda 	dais = devm_kcalloc(dev, num_dais, sizeof(*dais), GFP_KERNEL);
7422b13596fSVijendar Mukunda 	if (!dais)
7432b13596fSVijendar Mukunda 		return -ENOMEM;
7442b13596fSVijendar Mukunda 
7452b13596fSVijendar Mukunda 	dai_runtime_array = devm_kcalloc(dev, num_dais,
7462b13596fSVijendar Mukunda 					 sizeof(struct sdw_amd_dai_runtime *),
7472b13596fSVijendar Mukunda 					 GFP_KERNEL);
7482b13596fSVijendar Mukunda 	if (!dai_runtime_array)
7492b13596fSVijendar Mukunda 		return -ENOMEM;
7502b13596fSVijendar Mukunda 	amd_manager->dai_runtime_array = dai_runtime_array;
7512b13596fSVijendar Mukunda 	for (i = 0; i < num_dais; i++) {
7522b13596fSVijendar Mukunda 		dais[i].name = devm_kasprintf(dev, GFP_KERNEL, "SDW%d Pin%d", amd_manager->instance,
7532b13596fSVijendar Mukunda 					      i);
7542b13596fSVijendar Mukunda 		if (!dais[i].name)
7552b13596fSVijendar Mukunda 			return -ENOMEM;
7562b13596fSVijendar Mukunda 		if (i < amd_manager->num_dout_ports)
7572b13596fSVijendar Mukunda 			stream = &dais[i].playback;
7582b13596fSVijendar Mukunda 		else
7592b13596fSVijendar Mukunda 			stream = &dais[i].capture;
7602b13596fSVijendar Mukunda 
7612b13596fSVijendar Mukunda 		stream->channels_min = 2;
7622b13596fSVijendar Mukunda 		stream->channels_max = 2;
7632b13596fSVijendar Mukunda 		stream->rates = SNDRV_PCM_RATE_48000;
7642b13596fSVijendar Mukunda 		stream->formats = SNDRV_PCM_FMTBIT_S16_LE;
7652b13596fSVijendar Mukunda 
7662b13596fSVijendar Mukunda 		dais[i].ops = &amd_sdw_dai_ops;
7672b13596fSVijendar Mukunda 		dais[i].id = i;
7682b13596fSVijendar Mukunda 	}
7692b13596fSVijendar Mukunda 
7702b13596fSVijendar Mukunda 	return devm_snd_soc_register_component(dev, &amd_sdw_dai_component,
7712b13596fSVijendar Mukunda 					       dais, num_dais);
7722b13596fSVijendar Mukunda }
7732b13596fSVijendar Mukunda 
77465f93e40SVijendar Mukunda static void amd_sdw_update_slave_status_work(struct work_struct *work)
77565f93e40SVijendar Mukunda {
77665f93e40SVijendar Mukunda 	struct amd_sdw_manager *amd_manager =
77765f93e40SVijendar Mukunda 		container_of(work, struct amd_sdw_manager, amd_sdw_work);
77865f93e40SVijendar Mukunda 	int retry_count = 0;
77965f93e40SVijendar Mukunda 
78065f93e40SVijendar Mukunda 	if (amd_manager->status[0] == SDW_SLAVE_ATTACHED) {
78165f93e40SVijendar Mukunda 		writel(0, amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_MASK_0TO7);
78265f93e40SVijendar Mukunda 		writel(0, amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_MASK_8TO11);
78365f93e40SVijendar Mukunda 	}
78465f93e40SVijendar Mukunda 
78565f93e40SVijendar Mukunda update_status:
78665f93e40SVijendar Mukunda 	sdw_handle_slave_status(&amd_manager->bus, amd_manager->status);
78765f93e40SVijendar Mukunda 	/*
78865f93e40SVijendar Mukunda 	 * During the peripheral enumeration sequence, the SoundWire manager interrupts
78965f93e40SVijendar Mukunda 	 * are masked. Once the device number programming is done for all peripherals,
79065f93e40SVijendar Mukunda 	 * interrupts will be unmasked. Read the peripheral device status from ping command
79165f93e40SVijendar Mukunda 	 * and process the response. This sequence will ensure all peripheral devices enumerated
79265f93e40SVijendar Mukunda 	 * and initialized properly.
79365f93e40SVijendar Mukunda 	 */
79465f93e40SVijendar Mukunda 	if (amd_manager->status[0] == SDW_SLAVE_ATTACHED) {
79565f93e40SVijendar Mukunda 		if (retry_count++ < SDW_MAX_DEVICES) {
79665f93e40SVijendar Mukunda 			writel(AMD_SDW_IRQ_MASK_0TO7, amd_manager->mmio +
79765f93e40SVijendar Mukunda 			       ACP_SW_STATE_CHANGE_STATUS_MASK_0TO7);
79865f93e40SVijendar Mukunda 			writel(AMD_SDW_IRQ_MASK_8TO11, amd_manager->mmio +
79965f93e40SVijendar Mukunda 			       ACP_SW_STATE_CHANGE_STATUS_MASK_8TO11);
80065f93e40SVijendar Mukunda 			amd_sdw_read_and_process_ping_status(amd_manager);
80165f93e40SVijendar Mukunda 			goto update_status;
80265f93e40SVijendar Mukunda 		} else {
80365f93e40SVijendar Mukunda 			dev_err_ratelimited(amd_manager->dev,
80465f93e40SVijendar Mukunda 					    "Device0 detected after %d iterations\n",
80565f93e40SVijendar Mukunda 					    retry_count);
80665f93e40SVijendar Mukunda 		}
80765f93e40SVijendar Mukunda 	}
80865f93e40SVijendar Mukunda }
80965f93e40SVijendar Mukunda 
81065f93e40SVijendar Mukunda static void amd_sdw_update_slave_status(u32 status_change_0to7, u32 status_change_8to11,
81165f93e40SVijendar Mukunda 					struct amd_sdw_manager *amd_manager)
81265f93e40SVijendar Mukunda {
81365f93e40SVijendar Mukunda 	u64 slave_stat;
81465f93e40SVijendar Mukunda 	u32 val;
81565f93e40SVijendar Mukunda 	int dev_index;
81665f93e40SVijendar Mukunda 
81765f93e40SVijendar Mukunda 	if (status_change_0to7 == AMD_SDW_SLAVE_0_ATTACHED)
81865f93e40SVijendar Mukunda 		memset(amd_manager->status, 0, sizeof(amd_manager->status));
81965f93e40SVijendar Mukunda 	slave_stat = status_change_0to7;
82065f93e40SVijendar Mukunda 	slave_stat |= FIELD_GET(AMD_SDW_MCP_SLAVE_STATUS_8TO_11, status_change_8to11) << 32;
82165f93e40SVijendar Mukunda 	dev_dbg(amd_manager->dev, "status_change_0to7:0x%x status_change_8to11:0x%x\n",
82265f93e40SVijendar Mukunda 		status_change_0to7, status_change_8to11);
82365f93e40SVijendar Mukunda 	if (slave_stat) {
82465f93e40SVijendar Mukunda 		for (dev_index = 0; dev_index <= SDW_MAX_DEVICES; ++dev_index) {
82565f93e40SVijendar Mukunda 			if (slave_stat & AMD_SDW_MCP_SLAVE_STATUS_VALID_MASK(dev_index)) {
82665f93e40SVijendar Mukunda 				val = (slave_stat >> AMD_SDW_MCP_SLAVE_STAT_SHIFT_MASK(dev_index)) &
82765f93e40SVijendar Mukunda 				      AMD_SDW_MCP_SLAVE_STATUS_MASK;
82865f93e40SVijendar Mukunda 				amd_sdw_fill_slave_status(amd_manager, dev_index, val);
82965f93e40SVijendar Mukunda 			}
83065f93e40SVijendar Mukunda 		}
83165f93e40SVijendar Mukunda 	}
83265f93e40SVijendar Mukunda }
83365f93e40SVijendar Mukunda 
83466c87883SVijendar Mukunda static void amd_sdw_process_wake_event(struct amd_sdw_manager *amd_manager)
83566c87883SVijendar Mukunda {
83666c87883SVijendar Mukunda 	pm_request_resume(amd_manager->dev);
83766c87883SVijendar Mukunda 	writel(0x00, amd_manager->acp_mmio + ACP_SW_WAKE_EN(amd_manager->instance));
83866c87883SVijendar Mukunda 	writel(0x00, amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_8TO11);
83966c87883SVijendar Mukunda }
84066c87883SVijendar Mukunda 
84165f93e40SVijendar Mukunda static void amd_sdw_irq_thread(struct work_struct *work)
84265f93e40SVijendar Mukunda {
84365f93e40SVijendar Mukunda 	struct amd_sdw_manager *amd_manager =
84465f93e40SVijendar Mukunda 			container_of(work, struct amd_sdw_manager, amd_sdw_irq_thread);
84565f93e40SVijendar Mukunda 	u32 status_change_8to11;
84665f93e40SVijendar Mukunda 	u32 status_change_0to7;
84765f93e40SVijendar Mukunda 
84865f93e40SVijendar Mukunda 	status_change_8to11 = readl(amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_8TO11);
84965f93e40SVijendar Mukunda 	status_change_0to7 = readl(amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_0TO7);
85065f93e40SVijendar Mukunda 	dev_dbg(amd_manager->dev, "[SDW%d] SDW INT: 0to7=0x%x, 8to11=0x%x\n",
85165f93e40SVijendar Mukunda 		amd_manager->instance, status_change_0to7, status_change_8to11);
85266c87883SVijendar Mukunda 	if (status_change_8to11 & AMD_SDW_WAKE_STAT_MASK)
85366c87883SVijendar Mukunda 		return amd_sdw_process_wake_event(amd_manager);
85466c87883SVijendar Mukunda 
85565f93e40SVijendar Mukunda 	if (status_change_8to11 & AMD_SDW_PREQ_INTR_STAT) {
85665f93e40SVijendar Mukunda 		amd_sdw_read_and_process_ping_status(amd_manager);
85765f93e40SVijendar Mukunda 	} else {
85865f93e40SVijendar Mukunda 		/* Check for the updated status on peripheral device */
85965f93e40SVijendar Mukunda 		amd_sdw_update_slave_status(status_change_0to7, status_change_8to11, amd_manager);
86065f93e40SVijendar Mukunda 	}
86165f93e40SVijendar Mukunda 	if (status_change_8to11 || status_change_0to7)
86265f93e40SVijendar Mukunda 		schedule_work(&amd_manager->amd_sdw_work);
86365f93e40SVijendar Mukunda 	writel(0x00, amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_8TO11);
86465f93e40SVijendar Mukunda 	writel(0x00, amd_manager->mmio + ACP_SW_STATE_CHANGE_STATUS_0TO7);
86565f93e40SVijendar Mukunda }
86665f93e40SVijendar Mukunda 
867d8f48fbdSVijendar Mukunda static void amd_sdw_probe_work(struct work_struct *work)
868d8f48fbdSVijendar Mukunda {
869d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = container_of(work, struct amd_sdw_manager,
870d8f48fbdSVijendar Mukunda 							   probe_work);
871d8f48fbdSVijendar Mukunda 	struct sdw_master_prop *prop;
872d8f48fbdSVijendar Mukunda 	int ret;
873d8f48fbdSVijendar Mukunda 
874d8f48fbdSVijendar Mukunda 	prop = &amd_manager->bus.prop;
875d8f48fbdSVijendar Mukunda 	if (!prop->hw_disabled) {
876d8f48fbdSVijendar Mukunda 		amd_enable_sdw_pads(amd_manager);
877d8f48fbdSVijendar Mukunda 		ret = amd_init_sdw_manager(amd_manager);
878d8f48fbdSVijendar Mukunda 		if (ret)
879d8f48fbdSVijendar Mukunda 			return;
880d8f48fbdSVijendar Mukunda 		amd_enable_sdw_interrupts(amd_manager);
881d8f48fbdSVijendar Mukunda 		ret = amd_enable_sdw_manager(amd_manager);
882d8f48fbdSVijendar Mukunda 		if (ret)
883d8f48fbdSVijendar Mukunda 			return;
884d8f48fbdSVijendar Mukunda 		amd_sdw_set_frameshape(amd_manager);
885d8f48fbdSVijendar Mukunda 	}
88681ff58ffSVijendar Mukunda 	/* Enable runtime PM */
88781ff58ffSVijendar Mukunda 	pm_runtime_set_autosuspend_delay(amd_manager->dev, AMD_SDW_MASTER_SUSPEND_DELAY_MS);
88881ff58ffSVijendar Mukunda 	pm_runtime_use_autosuspend(amd_manager->dev);
88981ff58ffSVijendar Mukunda 	pm_runtime_mark_last_busy(amd_manager->dev);
89081ff58ffSVijendar Mukunda 	pm_runtime_set_active(amd_manager->dev);
89181ff58ffSVijendar Mukunda 	pm_runtime_enable(amd_manager->dev);
892d8f48fbdSVijendar Mukunda }
893d8f48fbdSVijendar Mukunda 
894d8f48fbdSVijendar Mukunda static int amd_sdw_manager_probe(struct platform_device *pdev)
895d8f48fbdSVijendar Mukunda {
896d8f48fbdSVijendar Mukunda 	const struct acp_sdw_pdata *pdata = pdev->dev.platform_data;
897d8f48fbdSVijendar Mukunda 	struct resource *res;
898d8f48fbdSVijendar Mukunda 	struct device *dev = &pdev->dev;
899d8f48fbdSVijendar Mukunda 	struct sdw_master_prop *prop;
900d8f48fbdSVijendar Mukunda 	struct sdw_bus_params *params;
901d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager;
902d8f48fbdSVijendar Mukunda 	int ret;
903d8f48fbdSVijendar Mukunda 
904d8f48fbdSVijendar Mukunda 	amd_manager = devm_kzalloc(dev, sizeof(struct amd_sdw_manager), GFP_KERNEL);
905d8f48fbdSVijendar Mukunda 	if (!amd_manager)
906d8f48fbdSVijendar Mukunda 		return -ENOMEM;
907d8f48fbdSVijendar Mukunda 
908d8f48fbdSVijendar Mukunda 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
909d8f48fbdSVijendar Mukunda 	if (!res)
910d8f48fbdSVijendar Mukunda 		return -ENOMEM;
911d8f48fbdSVijendar Mukunda 
912d8f48fbdSVijendar Mukunda 	amd_manager->acp_mmio = devm_ioremap(dev, res->start, resource_size(res));
9137891d0a5SDan Carpenter 	if (!amd_manager->acp_mmio) {
914d8f48fbdSVijendar Mukunda 		dev_err(dev, "mmio not found\n");
9157891d0a5SDan Carpenter 		return -ENOMEM;
916d8f48fbdSVijendar Mukunda 	}
917d8f48fbdSVijendar Mukunda 	amd_manager->instance = pdata->instance;
918d8f48fbdSVijendar Mukunda 	amd_manager->mmio = amd_manager->acp_mmio +
919d8f48fbdSVijendar Mukunda 			    (amd_manager->instance * SDW_MANAGER_REG_OFFSET);
920d8f48fbdSVijendar Mukunda 	amd_manager->acp_sdw_lock = pdata->acp_sdw_lock;
921d8f48fbdSVijendar Mukunda 	amd_manager->cols_index = sdw_find_col_index(AMD_SDW_DEFAULT_COLUMNS);
922d8f48fbdSVijendar Mukunda 	amd_manager->rows_index = sdw_find_row_index(AMD_SDW_DEFAULT_ROWS);
923d8f48fbdSVijendar Mukunda 	amd_manager->dev = dev;
924d8f48fbdSVijendar Mukunda 	amd_manager->bus.ops = &amd_sdw_ops;
925d8f48fbdSVijendar Mukunda 	amd_manager->bus.port_ops = &amd_sdw_port_ops;
926d8f48fbdSVijendar Mukunda 	amd_manager->bus.compute_params = &amd_sdw_compute_params;
927d8f48fbdSVijendar Mukunda 	amd_manager->bus.clk_stop_timeout = 200;
928d8f48fbdSVijendar Mukunda 	amd_manager->bus.link_id = amd_manager->instance;
929d8f48fbdSVijendar Mukunda 
930*154cfc3dSPierre-Louis Bossart 	/*
931*154cfc3dSPierre-Louis Bossart 	 * Due to BIOS compatibility, the two links are exposed within
932*154cfc3dSPierre-Louis Bossart 	 * the scope of a single controller. If this changes, the
933*154cfc3dSPierre-Louis Bossart 	 * controller_id will have to be updated with drv_data
934*154cfc3dSPierre-Louis Bossart 	 * information.
935*154cfc3dSPierre-Louis Bossart 	 */
936*154cfc3dSPierre-Louis Bossart 	amd_manager->bus.controller_id = 0;
937*154cfc3dSPierre-Louis Bossart 
938d8f48fbdSVijendar Mukunda 	switch (amd_manager->instance) {
939d8f48fbdSVijendar Mukunda 	case ACP_SDW0:
940d8f48fbdSVijendar Mukunda 		amd_manager->num_dout_ports = AMD_SDW0_MAX_TX_PORTS;
941d8f48fbdSVijendar Mukunda 		amd_manager->num_din_ports = AMD_SDW0_MAX_RX_PORTS;
942d8f48fbdSVijendar Mukunda 		break;
943d8f48fbdSVijendar Mukunda 	case ACP_SDW1:
944d8f48fbdSVijendar Mukunda 		amd_manager->num_dout_ports = AMD_SDW1_MAX_TX_PORTS;
945d8f48fbdSVijendar Mukunda 		amd_manager->num_din_ports = AMD_SDW1_MAX_RX_PORTS;
946d8f48fbdSVijendar Mukunda 		break;
947d8f48fbdSVijendar Mukunda 	default:
948d8f48fbdSVijendar Mukunda 		return -EINVAL;
949d8f48fbdSVijendar Mukunda 	}
950d8f48fbdSVijendar Mukunda 
951d8f48fbdSVijendar Mukunda 	amd_manager->reg_mask = &sdw_manager_reg_mask_array[amd_manager->instance];
952d8f48fbdSVijendar Mukunda 	params = &amd_manager->bus.params;
953d8f48fbdSVijendar Mukunda 	params->max_dr_freq = AMD_SDW_DEFAULT_CLK_FREQ * 2;
954d8f48fbdSVijendar Mukunda 	params->curr_dr_freq = AMD_SDW_DEFAULT_CLK_FREQ * 2;
955d8f48fbdSVijendar Mukunda 	params->col = AMD_SDW_DEFAULT_COLUMNS;
956d8f48fbdSVijendar Mukunda 	params->row = AMD_SDW_DEFAULT_ROWS;
957d8f48fbdSVijendar Mukunda 	prop = &amd_manager->bus.prop;
958d8f48fbdSVijendar Mukunda 	prop->clk_freq = &amd_sdw_freq_tbl[0];
959d8f48fbdSVijendar Mukunda 	prop->mclk_freq = AMD_SDW_BUS_BASE_FREQ;
960d8f48fbdSVijendar Mukunda 
961d8f48fbdSVijendar Mukunda 	ret = sdw_bus_master_add(&amd_manager->bus, dev, dev->fwnode);
962d8f48fbdSVijendar Mukunda 	if (ret) {
963d8f48fbdSVijendar Mukunda 		dev_err(dev, "Failed to register SoundWire manager(%d)\n", ret);
964d8f48fbdSVijendar Mukunda 		return ret;
965d8f48fbdSVijendar Mukunda 	}
9662b13596fSVijendar Mukunda 	ret = amd_sdw_register_dais(amd_manager);
9672b13596fSVijendar Mukunda 	if (ret) {
9682b13596fSVijendar Mukunda 		dev_err(dev, "CPU DAI registration failed\n");
9692b13596fSVijendar Mukunda 		sdw_bus_master_delete(&amd_manager->bus);
9702b13596fSVijendar Mukunda 		return ret;
9712b13596fSVijendar Mukunda 	}
972d8f48fbdSVijendar Mukunda 	dev_set_drvdata(dev, amd_manager);
97365f93e40SVijendar Mukunda 	INIT_WORK(&amd_manager->amd_sdw_irq_thread, amd_sdw_irq_thread);
97465f93e40SVijendar Mukunda 	INIT_WORK(&amd_manager->amd_sdw_work, amd_sdw_update_slave_status_work);
975d8f48fbdSVijendar Mukunda 	INIT_WORK(&amd_manager->probe_work, amd_sdw_probe_work);
976d8f48fbdSVijendar Mukunda 	/*
977d8f48fbdSVijendar Mukunda 	 * Instead of having lengthy probe sequence, use deferred probe.
978d8f48fbdSVijendar Mukunda 	 */
979d8f48fbdSVijendar Mukunda 	schedule_work(&amd_manager->probe_work);
980d8f48fbdSVijendar Mukunda 	return 0;
981d8f48fbdSVijendar Mukunda }
982d8f48fbdSVijendar Mukunda 
9831ec33e22SUwe Kleine-König static void amd_sdw_manager_remove(struct platform_device *pdev)
984d8f48fbdSVijendar Mukunda {
985d8f48fbdSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = dev_get_drvdata(&pdev->dev);
9861ec33e22SUwe Kleine-König 	int ret;
987d8f48fbdSVijendar Mukunda 
98881ff58ffSVijendar Mukunda 	pm_runtime_disable(&pdev->dev);
989d8f48fbdSVijendar Mukunda 	cancel_work_sync(&amd_manager->probe_work);
990d8f48fbdSVijendar Mukunda 	amd_disable_sdw_interrupts(amd_manager);
991d8f48fbdSVijendar Mukunda 	sdw_bus_master_delete(&amd_manager->bus);
9921ec33e22SUwe Kleine-König 	ret = amd_disable_sdw_manager(amd_manager);
9931ec33e22SUwe Kleine-König 	if (ret)
9941ec33e22SUwe Kleine-König 		dev_err(&pdev->dev, "Failed to disable device (%pe)\n", ERR_PTR(ret));
995d8f48fbdSVijendar Mukunda }
996d8f48fbdSVijendar Mukunda 
99781ff58ffSVijendar Mukunda static int amd_sdw_clock_stop(struct amd_sdw_manager *amd_manager)
99881ff58ffSVijendar Mukunda {
99981ff58ffSVijendar Mukunda 	u32 val;
100081ff58ffSVijendar Mukunda 	int ret;
100181ff58ffSVijendar Mukunda 
100281ff58ffSVijendar Mukunda 	ret = sdw_bus_prep_clk_stop(&amd_manager->bus);
100381ff58ffSVijendar Mukunda 	if (ret < 0 && ret != -ENODATA) {
100481ff58ffSVijendar Mukunda 		dev_err(amd_manager->dev, "prepare clock stop failed %d", ret);
100581ff58ffSVijendar Mukunda 		return 0;
100681ff58ffSVijendar Mukunda 	}
100781ff58ffSVijendar Mukunda 	ret = sdw_bus_clk_stop(&amd_manager->bus);
100881ff58ffSVijendar Mukunda 	if (ret < 0 && ret != -ENODATA) {
100981ff58ffSVijendar Mukunda 		dev_err(amd_manager->dev, "bus clock stop failed %d", ret);
101081ff58ffSVijendar Mukunda 		return 0;
101181ff58ffSVijendar Mukunda 	}
101281ff58ffSVijendar Mukunda 
101381ff58ffSVijendar Mukunda 	ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL, val,
101481ff58ffSVijendar Mukunda 				 (val & AMD_SDW_CLK_STOP_DONE), ACP_DELAY_US, AMD_SDW_TIMEOUT);
101581ff58ffSVijendar Mukunda 	if (ret) {
101681ff58ffSVijendar Mukunda 		dev_err(amd_manager->dev, "SDW%x clock stop failed\n", amd_manager->instance);
101781ff58ffSVijendar Mukunda 		return 0;
101881ff58ffSVijendar Mukunda 	}
101981ff58ffSVijendar Mukunda 
102081ff58ffSVijendar Mukunda 	amd_manager->clk_stopped = true;
102181ff58ffSVijendar Mukunda 	if (amd_manager->wake_en_mask)
102281ff58ffSVijendar Mukunda 		writel(0x01, amd_manager->acp_mmio + ACP_SW_WAKE_EN(amd_manager->instance));
102381ff58ffSVijendar Mukunda 
102481ff58ffSVijendar Mukunda 	dev_dbg(amd_manager->dev, "SDW%x clock stop successful\n", amd_manager->instance);
102581ff58ffSVijendar Mukunda 	return 0;
102681ff58ffSVijendar Mukunda }
102781ff58ffSVijendar Mukunda 
102881ff58ffSVijendar Mukunda static int amd_sdw_clock_stop_exit(struct amd_sdw_manager *amd_manager)
102981ff58ffSVijendar Mukunda {
103081ff58ffSVijendar Mukunda 	int ret;
103181ff58ffSVijendar Mukunda 	u32 val;
103281ff58ffSVijendar Mukunda 
103381ff58ffSVijendar Mukunda 	if (amd_manager->clk_stopped) {
103481ff58ffSVijendar Mukunda 		val = readl(amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL);
103581ff58ffSVijendar Mukunda 		val |= AMD_SDW_CLK_RESUME_REQ;
103681ff58ffSVijendar Mukunda 		writel(val, amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL);
103781ff58ffSVijendar Mukunda 		ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL, val,
103881ff58ffSVijendar Mukunda 					 (val & AMD_SDW_CLK_RESUME_DONE), ACP_DELAY_US,
103981ff58ffSVijendar Mukunda 					 AMD_SDW_TIMEOUT);
104081ff58ffSVijendar Mukunda 		if (val & AMD_SDW_CLK_RESUME_DONE) {
104181ff58ffSVijendar Mukunda 			writel(0, amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL);
104281ff58ffSVijendar Mukunda 			ret = sdw_bus_exit_clk_stop(&amd_manager->bus);
104381ff58ffSVijendar Mukunda 			if (ret < 0)
104481ff58ffSVijendar Mukunda 				dev_err(amd_manager->dev, "bus failed to exit clock stop %d\n",
104581ff58ffSVijendar Mukunda 					ret);
104681ff58ffSVijendar Mukunda 			amd_manager->clk_stopped = false;
104781ff58ffSVijendar Mukunda 		}
104881ff58ffSVijendar Mukunda 	}
104981ff58ffSVijendar Mukunda 	if (amd_manager->clk_stopped) {
105081ff58ffSVijendar Mukunda 		dev_err(amd_manager->dev, "SDW%x clock stop exit failed\n", amd_manager->instance);
105181ff58ffSVijendar Mukunda 		return 0;
105281ff58ffSVijendar Mukunda 	}
105381ff58ffSVijendar Mukunda 	dev_dbg(amd_manager->dev, "SDW%x clock stop exit successful\n", amd_manager->instance);
105481ff58ffSVijendar Mukunda 	return 0;
105581ff58ffSVijendar Mukunda }
105681ff58ffSVijendar Mukunda 
10579cf1efc5SVijendar Mukunda static int amd_resume_child_device(struct device *dev, void *data)
10589cf1efc5SVijendar Mukunda {
10599cf1efc5SVijendar Mukunda 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
10609cf1efc5SVijendar Mukunda 	int ret;
10619cf1efc5SVijendar Mukunda 
10629cf1efc5SVijendar Mukunda 	if (!slave->probed) {
10639cf1efc5SVijendar Mukunda 		dev_dbg(dev, "skipping device, no probed driver\n");
10649cf1efc5SVijendar Mukunda 		return 0;
10659cf1efc5SVijendar Mukunda 	}
10669cf1efc5SVijendar Mukunda 	if (!slave->dev_num_sticky) {
10679cf1efc5SVijendar Mukunda 		dev_dbg(dev, "skipping device, never detected on bus\n");
10689cf1efc5SVijendar Mukunda 		return 0;
10699cf1efc5SVijendar Mukunda 	}
10709cf1efc5SVijendar Mukunda 	ret = pm_request_resume(dev);
10719cf1efc5SVijendar Mukunda 	if (ret < 0) {
10729cf1efc5SVijendar Mukunda 		dev_err(dev, "pm_request_resume failed: %d\n", ret);
10739cf1efc5SVijendar Mukunda 		return ret;
10749cf1efc5SVijendar Mukunda 	}
10759cf1efc5SVijendar Mukunda 	return 0;
10769cf1efc5SVijendar Mukunda }
10779cf1efc5SVijendar Mukunda 
10789cf1efc5SVijendar Mukunda static int __maybe_unused amd_pm_prepare(struct device *dev)
10799cf1efc5SVijendar Mukunda {
10809cf1efc5SVijendar Mukunda 	struct amd_sdw_manager *amd_manager = dev_get_drvdata(dev);
10819cf1efc5SVijendar Mukunda 	struct sdw_bus *bus = &amd_manager->bus;
10829cf1efc5SVijendar Mukunda 	int ret;
10839cf1efc5SVijendar Mukunda 
10849cf1efc5SVijendar Mukunda 	if (bus->prop.hw_disabled) {
10859cf1efc5SVijendar Mukunda 		dev_dbg(bus->dev, "SoundWire manager %d is disabled, ignoring\n",
10869cf1efc5SVijendar Mukunda 			bus->link_id);
10879cf1efc5SVijendar Mukunda 		return 0;
10889cf1efc5SVijendar Mukunda 	}
10899cf1efc5SVijendar Mukunda 	/*
10909cf1efc5SVijendar Mukunda 	 * When multiple peripheral devices connected over the same link, if SoundWire manager
10919cf1efc5SVijendar Mukunda 	 * device is not in runtime suspend state, observed that device alerts are missing
10929cf1efc5SVijendar Mukunda 	 * without pm_prepare on AMD platforms in clockstop mode0.
10939cf1efc5SVijendar Mukunda 	 */
10949cf1efc5SVijendar Mukunda 	if (amd_manager->power_mode_mask & AMD_SDW_CLK_STOP_MODE) {
10959cf1efc5SVijendar Mukunda 		ret = pm_request_resume(dev);
10969cf1efc5SVijendar Mukunda 		if (ret < 0) {
10979cf1efc5SVijendar Mukunda 			dev_err(bus->dev, "pm_request_resume failed: %d\n", ret);
10989cf1efc5SVijendar Mukunda 			return 0;
10999cf1efc5SVijendar Mukunda 		}
11009cf1efc5SVijendar Mukunda 	}
11019cf1efc5SVijendar Mukunda 	/* To force peripheral devices to system level suspend state, resume the devices
11029cf1efc5SVijendar Mukunda 	 * from runtime suspend state first. Without that unable to dispatch the alert
11039cf1efc5SVijendar Mukunda 	 * status to peripheral driver during system level resume as they are in runtime
11049cf1efc5SVijendar Mukunda 	 * suspend state.
11059cf1efc5SVijendar Mukunda 	 */
11069cf1efc5SVijendar Mukunda 	ret = device_for_each_child(bus->dev, NULL, amd_resume_child_device);
11079cf1efc5SVijendar Mukunda 	if (ret < 0)
11089cf1efc5SVijendar Mukunda 		dev_err(dev, "amd_resume_child_device failed: %d\n", ret);
11099cf1efc5SVijendar Mukunda 	return 0;
11109cf1efc5SVijendar Mukunda }
11119cf1efc5SVijendar Mukunda 
11129cf1efc5SVijendar Mukunda static int __maybe_unused amd_suspend(struct device *dev)
11139cf1efc5SVijendar Mukunda {
11149cf1efc5SVijendar Mukunda 	struct amd_sdw_manager *amd_manager = dev_get_drvdata(dev);
11159cf1efc5SVijendar Mukunda 	struct sdw_bus *bus = &amd_manager->bus;
11169cf1efc5SVijendar Mukunda 	int ret;
11179cf1efc5SVijendar Mukunda 
11189cf1efc5SVijendar Mukunda 	if (bus->prop.hw_disabled) {
11199cf1efc5SVijendar Mukunda 		dev_dbg(bus->dev, "SoundWire manager %d is disabled, ignoring\n",
11209cf1efc5SVijendar Mukunda 			bus->link_id);
11219cf1efc5SVijendar Mukunda 		return 0;
11229cf1efc5SVijendar Mukunda 	}
11239cf1efc5SVijendar Mukunda 
11249cf1efc5SVijendar Mukunda 	if (amd_manager->power_mode_mask & AMD_SDW_CLK_STOP_MODE) {
11259cf1efc5SVijendar Mukunda 		return amd_sdw_clock_stop(amd_manager);
11269cf1efc5SVijendar Mukunda 	} else if (amd_manager->power_mode_mask & AMD_SDW_POWER_OFF_MODE) {
11279cf1efc5SVijendar Mukunda 		/*
11289cf1efc5SVijendar Mukunda 		 * As per hardware programming sequence on AMD platforms,
11299cf1efc5SVijendar Mukunda 		 * clock stop should be invoked first before powering-off
11309cf1efc5SVijendar Mukunda 		 */
11319cf1efc5SVijendar Mukunda 		ret = amd_sdw_clock_stop(amd_manager);
11329cf1efc5SVijendar Mukunda 		if (ret)
11339cf1efc5SVijendar Mukunda 			return ret;
11349cf1efc5SVijendar Mukunda 		return amd_deinit_sdw_manager(amd_manager);
11359cf1efc5SVijendar Mukunda 	}
11369cf1efc5SVijendar Mukunda 	return 0;
11379cf1efc5SVijendar Mukunda }
11389cf1efc5SVijendar Mukunda 
113981ff58ffSVijendar Mukunda static int __maybe_unused amd_suspend_runtime(struct device *dev)
114081ff58ffSVijendar Mukunda {
114181ff58ffSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = dev_get_drvdata(dev);
114281ff58ffSVijendar Mukunda 	struct sdw_bus *bus = &amd_manager->bus;
114381ff58ffSVijendar Mukunda 	int ret;
114481ff58ffSVijendar Mukunda 
114581ff58ffSVijendar Mukunda 	if (bus->prop.hw_disabled) {
114681ff58ffSVijendar Mukunda 		dev_dbg(bus->dev, "SoundWire manager %d is disabled,\n",
114781ff58ffSVijendar Mukunda 			bus->link_id);
114881ff58ffSVijendar Mukunda 		return 0;
114981ff58ffSVijendar Mukunda 	}
115081ff58ffSVijendar Mukunda 	if (amd_manager->power_mode_mask & AMD_SDW_CLK_STOP_MODE) {
115181ff58ffSVijendar Mukunda 		return amd_sdw_clock_stop(amd_manager);
115281ff58ffSVijendar Mukunda 	} else if (amd_manager->power_mode_mask & AMD_SDW_POWER_OFF_MODE) {
115381ff58ffSVijendar Mukunda 		ret = amd_sdw_clock_stop(amd_manager);
115481ff58ffSVijendar Mukunda 		if (ret)
115581ff58ffSVijendar Mukunda 			return ret;
115681ff58ffSVijendar Mukunda 		return amd_deinit_sdw_manager(amd_manager);
115781ff58ffSVijendar Mukunda 	}
115881ff58ffSVijendar Mukunda 	return 0;
115981ff58ffSVijendar Mukunda }
116081ff58ffSVijendar Mukunda 
116181ff58ffSVijendar Mukunda static int __maybe_unused amd_resume_runtime(struct device *dev)
116281ff58ffSVijendar Mukunda {
116381ff58ffSVijendar Mukunda 	struct amd_sdw_manager *amd_manager = dev_get_drvdata(dev);
116481ff58ffSVijendar Mukunda 	struct sdw_bus *bus = &amd_manager->bus;
116581ff58ffSVijendar Mukunda 	int ret;
116681ff58ffSVijendar Mukunda 	u32 val;
116781ff58ffSVijendar Mukunda 
116881ff58ffSVijendar Mukunda 	if (bus->prop.hw_disabled) {
116981ff58ffSVijendar Mukunda 		dev_dbg(bus->dev, "SoundWire manager %d is disabled, ignoring\n",
117081ff58ffSVijendar Mukunda 			bus->link_id);
117181ff58ffSVijendar Mukunda 		return 0;
117281ff58ffSVijendar Mukunda 	}
117381ff58ffSVijendar Mukunda 
117481ff58ffSVijendar Mukunda 	if (amd_manager->power_mode_mask & AMD_SDW_CLK_STOP_MODE) {
117581ff58ffSVijendar Mukunda 		return amd_sdw_clock_stop_exit(amd_manager);
117681ff58ffSVijendar Mukunda 	} else if (amd_manager->power_mode_mask & AMD_SDW_POWER_OFF_MODE) {
117781ff58ffSVijendar Mukunda 		val = readl(amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL);
117881ff58ffSVijendar Mukunda 		if (val) {
117981ff58ffSVijendar Mukunda 			val |= AMD_SDW_CLK_RESUME_REQ;
118081ff58ffSVijendar Mukunda 			writel(val, amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL);
118181ff58ffSVijendar Mukunda 			ret = readl_poll_timeout(amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL, val,
118281ff58ffSVijendar Mukunda 						 (val & AMD_SDW_CLK_RESUME_DONE), ACP_DELAY_US,
118381ff58ffSVijendar Mukunda 						 AMD_SDW_TIMEOUT);
118481ff58ffSVijendar Mukunda 			if (val & AMD_SDW_CLK_RESUME_DONE) {
118581ff58ffSVijendar Mukunda 				writel(0, amd_manager->mmio + ACP_SW_CLK_RESUME_CTRL);
118681ff58ffSVijendar Mukunda 				amd_manager->clk_stopped = false;
118781ff58ffSVijendar Mukunda 			}
118881ff58ffSVijendar Mukunda 		}
118981ff58ffSVijendar Mukunda 		sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
119081ff58ffSVijendar Mukunda 		amd_init_sdw_manager(amd_manager);
119181ff58ffSVijendar Mukunda 		amd_enable_sdw_interrupts(amd_manager);
119281ff58ffSVijendar Mukunda 		ret = amd_enable_sdw_manager(amd_manager);
119381ff58ffSVijendar Mukunda 		if (ret)
119481ff58ffSVijendar Mukunda 			return ret;
119581ff58ffSVijendar Mukunda 		amd_sdw_set_frameshape(amd_manager);
119681ff58ffSVijendar Mukunda 	}
119781ff58ffSVijendar Mukunda 	return 0;
119881ff58ffSVijendar Mukunda }
119981ff58ffSVijendar Mukunda 
120081ff58ffSVijendar Mukunda static const struct dev_pm_ops amd_pm = {
12019cf1efc5SVijendar Mukunda 	.prepare = amd_pm_prepare,
12029cf1efc5SVijendar Mukunda 	SET_SYSTEM_SLEEP_PM_OPS(amd_suspend, amd_resume_runtime)
120381ff58ffSVijendar Mukunda 	SET_RUNTIME_PM_OPS(amd_suspend_runtime, amd_resume_runtime, NULL)
120481ff58ffSVijendar Mukunda };
120581ff58ffSVijendar Mukunda 
1206d8f48fbdSVijendar Mukunda static struct platform_driver amd_sdw_driver = {
1207d8f48fbdSVijendar Mukunda 	.probe	= &amd_sdw_manager_probe,
12081ec33e22SUwe Kleine-König 	.remove_new = &amd_sdw_manager_remove,
1209d8f48fbdSVijendar Mukunda 	.driver = {
1210d8f48fbdSVijendar Mukunda 		.name	= "amd_sdw_manager",
121181ff58ffSVijendar Mukunda 		.pm = &amd_pm,
1212d8f48fbdSVijendar Mukunda 	}
1213d8f48fbdSVijendar Mukunda };
1214d8f48fbdSVijendar Mukunda module_platform_driver(amd_sdw_driver);
1215d8f48fbdSVijendar Mukunda 
1216d8f48fbdSVijendar Mukunda MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
1217d8f48fbdSVijendar Mukunda MODULE_DESCRIPTION("AMD SoundWire driver");
1218d8f48fbdSVijendar Mukunda MODULE_LICENSE("GPL");
1219d8f48fbdSVijendar Mukunda MODULE_ALIAS("platform:" DRV_NAME);
1220