xref: /openbmc/linux/sound/soc/sof/intel/hda-dsp.c (revision aae7c82d01219bf568a7b12f5839d7ca52b17d59)
1747503b1SLiam Girdwood // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2747503b1SLiam Girdwood //
3747503b1SLiam Girdwood // This file is provided under a dual BSD/GPLv2 license.  When using or
4747503b1SLiam Girdwood // redistributing this file, you may do so under either license.
5747503b1SLiam Girdwood //
6747503b1SLiam Girdwood // Copyright(c) 2018 Intel Corporation. All rights reserved.
7747503b1SLiam Girdwood //
8747503b1SLiam Girdwood // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9747503b1SLiam Girdwood //	    Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
10747503b1SLiam Girdwood //	    Rander Wang <rander.wang@intel.com>
11747503b1SLiam Girdwood //          Keyon Jie <yang.jie@linux.intel.com>
12747503b1SLiam Girdwood //
13747503b1SLiam Girdwood 
14747503b1SLiam Girdwood /*
15747503b1SLiam Girdwood  * Hardware interface for generic Intel audio DSP HDA IP
16747503b1SLiam Girdwood  */
17747503b1SLiam Girdwood 
18747503b1SLiam Girdwood #include <sound/hdaudio_ext.h>
19747503b1SLiam Girdwood #include <sound/hda_register.h>
20747503b1SLiam Girdwood #include "../ops.h"
21747503b1SLiam Girdwood #include "hda.h"
22747503b1SLiam Girdwood 
23747503b1SLiam Girdwood /*
24747503b1SLiam Girdwood  * DSP Core control.
25747503b1SLiam Girdwood  */
26747503b1SLiam Girdwood 
27747503b1SLiam Girdwood int hda_dsp_core_reset_enter(struct snd_sof_dev *sdev, unsigned int core_mask)
28747503b1SLiam Girdwood {
29747503b1SLiam Girdwood 	u32 adspcs;
30747503b1SLiam Girdwood 	u32 reset;
31747503b1SLiam Girdwood 	int ret;
32747503b1SLiam Girdwood 
33747503b1SLiam Girdwood 	/* set reset bits for cores */
34747503b1SLiam Girdwood 	reset = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
35747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
36747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
37747503b1SLiam Girdwood 					 reset, reset),
38747503b1SLiam Girdwood 
39747503b1SLiam Girdwood 	/* poll with timeout to check if operation successful */
40747503b1SLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
41747503b1SLiam Girdwood 					HDA_DSP_REG_ADSPCS, adspcs,
42747503b1SLiam Girdwood 					((adspcs & reset) == reset),
43747503b1SLiam Girdwood 					HDA_DSP_REG_POLL_INTERVAL_US,
44747503b1SLiam Girdwood 					HDA_DSP_RESET_TIMEOUT_US);
456a414489SPierre-Louis Bossart 	if (ret < 0) {
466a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
476a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
486a414489SPierre-Louis Bossart 			__func__);
496a414489SPierre-Louis Bossart 		return ret;
506a414489SPierre-Louis Bossart 	}
51747503b1SLiam Girdwood 
52747503b1SLiam Girdwood 	/* has core entered reset ? */
53747503b1SLiam Girdwood 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
54747503b1SLiam Girdwood 				  HDA_DSP_REG_ADSPCS);
55747503b1SLiam Girdwood 	if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) !=
56747503b1SLiam Girdwood 		HDA_DSP_ADSPCS_CRST_MASK(core_mask)) {
57747503b1SLiam Girdwood 		dev_err(sdev->dev,
58747503b1SLiam Girdwood 			"error: reset enter failed: core_mask %x adspcs 0x%x\n",
59747503b1SLiam Girdwood 			core_mask, adspcs);
60747503b1SLiam Girdwood 		ret = -EIO;
61747503b1SLiam Girdwood 	}
62747503b1SLiam Girdwood 
63747503b1SLiam Girdwood 	return ret;
64747503b1SLiam Girdwood }
65747503b1SLiam Girdwood 
66747503b1SLiam Girdwood int hda_dsp_core_reset_leave(struct snd_sof_dev *sdev, unsigned int core_mask)
67747503b1SLiam Girdwood {
68747503b1SLiam Girdwood 	unsigned int crst;
69747503b1SLiam Girdwood 	u32 adspcs;
70747503b1SLiam Girdwood 	int ret;
71747503b1SLiam Girdwood 
72747503b1SLiam Girdwood 	/* clear reset bits for cores */
73747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
74747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
75747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CRST_MASK(core_mask),
76747503b1SLiam Girdwood 					 0);
77747503b1SLiam Girdwood 
78747503b1SLiam Girdwood 	/* poll with timeout to check if operation successful */
79747503b1SLiam Girdwood 	crst = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
80747503b1SLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
81747503b1SLiam Girdwood 					    HDA_DSP_REG_ADSPCS, adspcs,
82747503b1SLiam Girdwood 					    !(adspcs & crst),
83747503b1SLiam Girdwood 					    HDA_DSP_REG_POLL_INTERVAL_US,
84747503b1SLiam Girdwood 					    HDA_DSP_RESET_TIMEOUT_US);
85747503b1SLiam Girdwood 
866a414489SPierre-Louis Bossart 	if (ret < 0) {
876a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
886a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
896a414489SPierre-Louis Bossart 			__func__);
906a414489SPierre-Louis Bossart 		return ret;
916a414489SPierre-Louis Bossart 	}
926a414489SPierre-Louis Bossart 
93747503b1SLiam Girdwood 	/* has core left reset ? */
94747503b1SLiam Girdwood 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
95747503b1SLiam Girdwood 				  HDA_DSP_REG_ADSPCS);
96747503b1SLiam Girdwood 	if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) != 0) {
97747503b1SLiam Girdwood 		dev_err(sdev->dev,
98747503b1SLiam Girdwood 			"error: reset leave failed: core_mask %x adspcs 0x%x\n",
99747503b1SLiam Girdwood 			core_mask, adspcs);
100747503b1SLiam Girdwood 		ret = -EIO;
101747503b1SLiam Girdwood 	}
102747503b1SLiam Girdwood 
103747503b1SLiam Girdwood 	return ret;
104747503b1SLiam Girdwood }
105747503b1SLiam Girdwood 
106747503b1SLiam Girdwood int hda_dsp_core_stall_reset(struct snd_sof_dev *sdev, unsigned int core_mask)
107747503b1SLiam Girdwood {
108747503b1SLiam Girdwood 	/* stall core */
109747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
110747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
111747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
112747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask));
113747503b1SLiam Girdwood 
114747503b1SLiam Girdwood 	/* set reset state */
115747503b1SLiam Girdwood 	return hda_dsp_core_reset_enter(sdev, core_mask);
116747503b1SLiam Girdwood }
117747503b1SLiam Girdwood 
118747503b1SLiam Girdwood int hda_dsp_core_run(struct snd_sof_dev *sdev, unsigned int core_mask)
119747503b1SLiam Girdwood {
120747503b1SLiam Girdwood 	int ret;
121747503b1SLiam Girdwood 
122747503b1SLiam Girdwood 	/* leave reset state */
123747503b1SLiam Girdwood 	ret = hda_dsp_core_reset_leave(sdev, core_mask);
124747503b1SLiam Girdwood 	if (ret < 0)
125747503b1SLiam Girdwood 		return ret;
126747503b1SLiam Girdwood 
127747503b1SLiam Girdwood 	/* run core */
128747503b1SLiam Girdwood 	dev_dbg(sdev->dev, "unstall/run core: core_mask = %x\n", core_mask);
129747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
130747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
131747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
132747503b1SLiam Girdwood 					 0);
133747503b1SLiam Girdwood 
134747503b1SLiam Girdwood 	/* is core now running ? */
135747503b1SLiam Girdwood 	if (!hda_dsp_core_is_enabled(sdev, core_mask)) {
136747503b1SLiam Girdwood 		hda_dsp_core_stall_reset(sdev, core_mask);
137747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: DSP start core failed: core_mask %x\n",
138747503b1SLiam Girdwood 			core_mask);
139747503b1SLiam Girdwood 		ret = -EIO;
140747503b1SLiam Girdwood 	}
141747503b1SLiam Girdwood 
142747503b1SLiam Girdwood 	return ret;
143747503b1SLiam Girdwood }
144747503b1SLiam Girdwood 
145747503b1SLiam Girdwood /*
146747503b1SLiam Girdwood  * Power Management.
147747503b1SLiam Girdwood  */
148747503b1SLiam Girdwood 
149747503b1SLiam Girdwood int hda_dsp_core_power_up(struct snd_sof_dev *sdev, unsigned int core_mask)
150747503b1SLiam Girdwood {
151747503b1SLiam Girdwood 	unsigned int cpa;
152747503b1SLiam Girdwood 	u32 adspcs;
153747503b1SLiam Girdwood 	int ret;
154747503b1SLiam Girdwood 
155747503b1SLiam Girdwood 	/* update bits */
156747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS,
157747503b1SLiam Girdwood 				HDA_DSP_ADSPCS_SPA_MASK(core_mask),
158747503b1SLiam Girdwood 				HDA_DSP_ADSPCS_SPA_MASK(core_mask));
159747503b1SLiam Girdwood 
160747503b1SLiam Girdwood 	/* poll with timeout to check if operation successful */
161747503b1SLiam Girdwood 	cpa = HDA_DSP_ADSPCS_CPA_MASK(core_mask);
162747503b1SLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
163747503b1SLiam Girdwood 					    HDA_DSP_REG_ADSPCS, adspcs,
164747503b1SLiam Girdwood 					    (adspcs & cpa) == cpa,
165747503b1SLiam Girdwood 					    HDA_DSP_REG_POLL_INTERVAL_US,
166747503b1SLiam Girdwood 					    HDA_DSP_RESET_TIMEOUT_US);
1676a414489SPierre-Louis Bossart 	if (ret < 0) {
1686a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
1696a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
1706a414489SPierre-Louis Bossart 			__func__);
1716a414489SPierre-Louis Bossart 		return ret;
1726a414489SPierre-Louis Bossart 	}
173747503b1SLiam Girdwood 
174747503b1SLiam Girdwood 	/* did core power up ? */
175747503b1SLiam Girdwood 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
176747503b1SLiam Girdwood 				  HDA_DSP_REG_ADSPCS);
177747503b1SLiam Girdwood 	if ((adspcs & HDA_DSP_ADSPCS_CPA_MASK(core_mask)) !=
178747503b1SLiam Girdwood 		HDA_DSP_ADSPCS_CPA_MASK(core_mask)) {
179747503b1SLiam Girdwood 		dev_err(sdev->dev,
180747503b1SLiam Girdwood 			"error: power up core failed core_mask %xadspcs 0x%x\n",
181747503b1SLiam Girdwood 			core_mask, adspcs);
182747503b1SLiam Girdwood 		ret = -EIO;
183747503b1SLiam Girdwood 	}
184747503b1SLiam Girdwood 
185747503b1SLiam Girdwood 	return ret;
186747503b1SLiam Girdwood }
187747503b1SLiam Girdwood 
188747503b1SLiam Girdwood int hda_dsp_core_power_down(struct snd_sof_dev *sdev, unsigned int core_mask)
189747503b1SLiam Girdwood {
190747503b1SLiam Girdwood 	u32 adspcs;
1916a414489SPierre-Louis Bossart 	int ret;
192747503b1SLiam Girdwood 
193747503b1SLiam Girdwood 	/* update bits */
194747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
195747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
196747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_SPA_MASK(core_mask), 0);
197747503b1SLiam Girdwood 
1986a414489SPierre-Louis Bossart 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
199747503b1SLiam Girdwood 				HDA_DSP_REG_ADSPCS, adspcs,
200747503b1SLiam Girdwood 				!(adspcs & HDA_DSP_ADSPCS_SPA_MASK(core_mask)),
201747503b1SLiam Girdwood 				HDA_DSP_REG_POLL_INTERVAL_US,
202747503b1SLiam Girdwood 				HDA_DSP_PD_TIMEOUT * USEC_PER_MSEC);
2036a414489SPierre-Louis Bossart 	if (ret < 0)
2046a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
2056a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
2066a414489SPierre-Louis Bossart 			__func__);
2076a414489SPierre-Louis Bossart 
2086a414489SPierre-Louis Bossart 	return ret;
209747503b1SLiam Girdwood }
210747503b1SLiam Girdwood 
211747503b1SLiam Girdwood bool hda_dsp_core_is_enabled(struct snd_sof_dev *sdev,
212747503b1SLiam Girdwood 			     unsigned int core_mask)
213747503b1SLiam Girdwood {
214747503b1SLiam Girdwood 	int val;
215747503b1SLiam Girdwood 	bool is_enable;
216747503b1SLiam Girdwood 
217747503b1SLiam Girdwood 	val = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS);
218747503b1SLiam Girdwood 
219747503b1SLiam Girdwood 	is_enable = ((val & HDA_DSP_ADSPCS_CPA_MASK(core_mask)) &&
220747503b1SLiam Girdwood 			(val & HDA_DSP_ADSPCS_SPA_MASK(core_mask)) &&
221747503b1SLiam Girdwood 			!(val & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) &&
222747503b1SLiam Girdwood 			!(val & HDA_DSP_ADSPCS_CSTALL_MASK(core_mask)));
223747503b1SLiam Girdwood 
224747503b1SLiam Girdwood 	dev_dbg(sdev->dev, "DSP core(s) enabled? %d : core_mask %x\n",
225747503b1SLiam Girdwood 		is_enable, core_mask);
226747503b1SLiam Girdwood 
227747503b1SLiam Girdwood 	return is_enable;
228747503b1SLiam Girdwood }
229747503b1SLiam Girdwood 
230747503b1SLiam Girdwood int hda_dsp_enable_core(struct snd_sof_dev *sdev, unsigned int core_mask)
231747503b1SLiam Girdwood {
232747503b1SLiam Girdwood 	int ret;
233747503b1SLiam Girdwood 
234747503b1SLiam Girdwood 	/* return if core is already enabled */
235747503b1SLiam Girdwood 	if (hda_dsp_core_is_enabled(sdev, core_mask))
236747503b1SLiam Girdwood 		return 0;
237747503b1SLiam Girdwood 
238747503b1SLiam Girdwood 	/* power up */
239747503b1SLiam Girdwood 	ret = hda_dsp_core_power_up(sdev, core_mask);
240747503b1SLiam Girdwood 	if (ret < 0) {
241747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core power up failed: core_mask %x\n",
242747503b1SLiam Girdwood 			core_mask);
243747503b1SLiam Girdwood 		return ret;
244747503b1SLiam Girdwood 	}
245747503b1SLiam Girdwood 
246747503b1SLiam Girdwood 	return hda_dsp_core_run(sdev, core_mask);
247747503b1SLiam Girdwood }
248747503b1SLiam Girdwood 
249747503b1SLiam Girdwood int hda_dsp_core_reset_power_down(struct snd_sof_dev *sdev,
250747503b1SLiam Girdwood 				  unsigned int core_mask)
251747503b1SLiam Girdwood {
252747503b1SLiam Girdwood 	int ret;
253747503b1SLiam Girdwood 
254747503b1SLiam Girdwood 	/* place core in reset prior to power down */
255747503b1SLiam Girdwood 	ret = hda_dsp_core_stall_reset(sdev, core_mask);
256747503b1SLiam Girdwood 	if (ret < 0) {
257747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core reset failed: core_mask %x\n",
258747503b1SLiam Girdwood 			core_mask);
259747503b1SLiam Girdwood 		return ret;
260747503b1SLiam Girdwood 	}
261747503b1SLiam Girdwood 
262747503b1SLiam Girdwood 	/* power down core */
263747503b1SLiam Girdwood 	ret = hda_dsp_core_power_down(sdev, core_mask);
264747503b1SLiam Girdwood 	if (ret < 0) {
265747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core power down fail mask %x: %d\n",
266747503b1SLiam Girdwood 			core_mask, ret);
267747503b1SLiam Girdwood 		return ret;
268747503b1SLiam Girdwood 	}
269747503b1SLiam Girdwood 
270747503b1SLiam Girdwood 	/* make sure we are in OFF state */
271747503b1SLiam Girdwood 	if (hda_dsp_core_is_enabled(sdev, core_mask)) {
272747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core disable fail mask %x: %d\n",
273747503b1SLiam Girdwood 			core_mask, ret);
274747503b1SLiam Girdwood 		ret = -EIO;
275747503b1SLiam Girdwood 	}
276747503b1SLiam Girdwood 
277747503b1SLiam Girdwood 	return ret;
278747503b1SLiam Girdwood }
279747503b1SLiam Girdwood 
280747503b1SLiam Girdwood void hda_dsp_ipc_int_enable(struct snd_sof_dev *sdev)
281747503b1SLiam Girdwood {
282747503b1SLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
283747503b1SLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
284747503b1SLiam Girdwood 
285747503b1SLiam Girdwood 	/* enable IPC DONE and BUSY interrupts */
286747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
287747503b1SLiam Girdwood 			HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY,
288747503b1SLiam Girdwood 			HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY);
289747503b1SLiam Girdwood 
290747503b1SLiam Girdwood 	/* enable IPC interrupt */
291747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
292747503b1SLiam Girdwood 				HDA_DSP_ADSPIC_IPC, HDA_DSP_ADSPIC_IPC);
293747503b1SLiam Girdwood }
294747503b1SLiam Girdwood 
295747503b1SLiam Girdwood void hda_dsp_ipc_int_disable(struct snd_sof_dev *sdev)
296747503b1SLiam Girdwood {
297747503b1SLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
298747503b1SLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
299747503b1SLiam Girdwood 
300747503b1SLiam Girdwood 	/* disable IPC interrupt */
301747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
302747503b1SLiam Girdwood 				HDA_DSP_ADSPIC_IPC, 0);
303747503b1SLiam Girdwood 
304747503b1SLiam Girdwood 	/* disable IPC BUSY and DONE interrupt */
305747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
306747503b1SLiam Girdwood 			HDA_DSP_REG_HIPCCTL_BUSY | HDA_DSP_REG_HIPCCTL_DONE, 0);
307747503b1SLiam Girdwood }
308747503b1SLiam Girdwood 
30962f8f766SKeyon Jie static int hda_dsp_wait_d0i3c_done(struct snd_sof_dev *sdev, int retry)
31062f8f766SKeyon Jie {
31162f8f766SKeyon Jie 	struct hdac_bus *bus = sof_to_bus(sdev);
31262f8f766SKeyon Jie 
31362f8f766SKeyon Jie 	while (snd_hdac_chip_readb(bus, VS_D0I3C) & SOF_HDA_VS_D0I3C_CIP) {
31462f8f766SKeyon Jie 		if (!retry--)
31562f8f766SKeyon Jie 			return -ETIMEDOUT;
31662f8f766SKeyon Jie 		usleep_range(10, 15);
31762f8f766SKeyon Jie 	}
31862f8f766SKeyon Jie 
31962f8f766SKeyon Jie 	return 0;
32062f8f766SKeyon Jie }
32162f8f766SKeyon Jie 
32262f8f766SKeyon Jie int hda_dsp_set_power_state(struct snd_sof_dev *sdev,
32362f8f766SKeyon Jie 			    enum sof_d0_substate d0_substate)
32462f8f766SKeyon Jie {
32562f8f766SKeyon Jie 	struct hdac_bus *bus = sof_to_bus(sdev);
32662f8f766SKeyon Jie 	int retry = 50;
32762f8f766SKeyon Jie 	int ret;
32862f8f766SKeyon Jie 	u8 value;
32962f8f766SKeyon Jie 
33062f8f766SKeyon Jie 	/* Write to D0I3C after Command-In-Progress bit is cleared */
33162f8f766SKeyon Jie 	ret = hda_dsp_wait_d0i3c_done(sdev, retry);
33262f8f766SKeyon Jie 	if (ret < 0) {
333*aae7c82dSKeyon Jie 		dev_err(bus->dev, "CIP timeout before D0I3C update!\n");
33462f8f766SKeyon Jie 		return ret;
33562f8f766SKeyon Jie 	}
33662f8f766SKeyon Jie 
33762f8f766SKeyon Jie 	/* Update D0I3C register */
33862f8f766SKeyon Jie 	value = d0_substate == SOF_DSP_D0I3 ? SOF_HDA_VS_D0I3C_I3 : 0;
33962f8f766SKeyon Jie 	snd_hdac_chip_updateb(bus, VS_D0I3C, SOF_HDA_VS_D0I3C_I3, value);
34062f8f766SKeyon Jie 
34162f8f766SKeyon Jie 	/* Wait for cmd in progress to be cleared before exiting the function */
34262f8f766SKeyon Jie 	retry = 50;
34362f8f766SKeyon Jie 	ret = hda_dsp_wait_d0i3c_done(sdev, retry);
34462f8f766SKeyon Jie 	if (ret < 0) {
345*aae7c82dSKeyon Jie 		dev_err(bus->dev, "CIP timeout after D0I3C update!\n");
34662f8f766SKeyon Jie 		return ret;
34762f8f766SKeyon Jie 	}
34862f8f766SKeyon Jie 
34962f8f766SKeyon Jie 	dev_vdbg(bus->dev, "D0I3C updated, register = 0x%x\n",
35062f8f766SKeyon Jie 		 snd_hdac_chip_readb(bus, VS_D0I3C));
35162f8f766SKeyon Jie 
35262f8f766SKeyon Jie 	return 0;
35362f8f766SKeyon Jie }
35462f8f766SKeyon Jie 
3551c38c922SFred Oh static int hda_suspend(struct snd_sof_dev *sdev, bool runtime_suspend)
356747503b1SLiam Girdwood {
357747503b1SLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
358747503b1SLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
359747503b1SLiam Girdwood #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
360747503b1SLiam Girdwood 	struct hdac_bus *bus = sof_to_bus(sdev);
361747503b1SLiam Girdwood #endif
362747503b1SLiam Girdwood 	int ret;
363747503b1SLiam Girdwood 
364747503b1SLiam Girdwood 	/* disable IPC interrupts */
365747503b1SLiam Girdwood 	hda_dsp_ipc_int_disable(sdev);
366747503b1SLiam Girdwood 
367747503b1SLiam Girdwood #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
368fd15f2f5SRander Wang 	if (runtime_suspend)
369fd15f2f5SRander Wang 		hda_codec_jack_wake_enable(sdev);
370fd15f2f5SRander Wang 
371747503b1SLiam Girdwood 	/* power down all hda link */
372747503b1SLiam Girdwood 	snd_hdac_ext_bus_link_power_down_all(bus);
373747503b1SLiam Girdwood #endif
374747503b1SLiam Girdwood 
375747503b1SLiam Girdwood 	/* power down DSP */
376747503b1SLiam Girdwood 	ret = hda_dsp_core_reset_power_down(sdev, chip->cores_mask);
377747503b1SLiam Girdwood 	if (ret < 0) {
378747503b1SLiam Girdwood 		dev_err(sdev->dev,
379747503b1SLiam Girdwood 			"error: failed to power down core during suspend\n");
380747503b1SLiam Girdwood 		return ret;
381747503b1SLiam Girdwood 	}
382747503b1SLiam Girdwood 
383747503b1SLiam Girdwood 	/* disable ppcap interrupt */
384747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_enable(sdev, false);
385747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_int_enable(sdev, false);
386747503b1SLiam Girdwood 
3879a50ee58SZhu Yingjiang 	/* disable hda bus irq and streams */
3889a50ee58SZhu Yingjiang 	hda_dsp_ctrl_stop_chip(sdev);
389747503b1SLiam Girdwood 
390747503b1SLiam Girdwood 	/* disable LP retention mode */
391747503b1SLiam Girdwood 	snd_sof_pci_update_bits(sdev, PCI_PGCTL,
392747503b1SLiam Girdwood 				PCI_PGCTL_LSRMD_MASK, PCI_PGCTL_LSRMD_MASK);
393747503b1SLiam Girdwood 
394747503b1SLiam Girdwood 	/* reset controller */
395747503b1SLiam Girdwood 	ret = hda_dsp_ctrl_link_reset(sdev, true);
396747503b1SLiam Girdwood 	if (ret < 0) {
397747503b1SLiam Girdwood 		dev_err(sdev->dev,
398747503b1SLiam Girdwood 			"error: failed to reset controller during suspend\n");
399747503b1SLiam Girdwood 		return ret;
400747503b1SLiam Girdwood 	}
401747503b1SLiam Girdwood 
402747503b1SLiam Girdwood 	return 0;
403747503b1SLiam Girdwood }
404747503b1SLiam Girdwood 
405fd15f2f5SRander Wang static int hda_resume(struct snd_sof_dev *sdev, bool runtime_resume)
406747503b1SLiam Girdwood {
407747503b1SLiam Girdwood #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
408747503b1SLiam Girdwood 	struct hdac_bus *bus = sof_to_bus(sdev);
409747503b1SLiam Girdwood 	struct hdac_ext_link *hlink = NULL;
410747503b1SLiam Girdwood #endif
411747503b1SLiam Girdwood 	int ret;
412747503b1SLiam Girdwood 
413747503b1SLiam Girdwood 	/*
414747503b1SLiam Girdwood 	 * clear TCSEL to clear playback on some HD Audio
415747503b1SLiam Girdwood 	 * codecs. PCI TCSEL is defined in the Intel manuals.
416747503b1SLiam Girdwood 	 */
417747503b1SLiam Girdwood 	snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
418747503b1SLiam Girdwood 
419747503b1SLiam Girdwood 	/* reset and start hda controller */
420747503b1SLiam Girdwood 	ret = hda_dsp_ctrl_init_chip(sdev, true);
421747503b1SLiam Girdwood 	if (ret < 0) {
422747503b1SLiam Girdwood 		dev_err(sdev->dev,
423747503b1SLiam Girdwood 			"error: failed to start controller after resume\n");
424747503b1SLiam Girdwood 		return ret;
425747503b1SLiam Girdwood 	}
426747503b1SLiam Girdwood 
427fd15f2f5SRander Wang #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
428fd15f2f5SRander Wang 	/* check jack status */
429fd15f2f5SRander Wang 	if (runtime_resume)
430fd15f2f5SRander Wang 		hda_codec_jack_check(sdev);
4316aa232e1SRander Wang 
4326aa232e1SRander Wang 	/* turn off the links that were off before suspend */
4336aa232e1SRander Wang 	list_for_each_entry(hlink, &bus->hlink_list, list) {
4346aa232e1SRander Wang 		if (!hlink->ref_count)
4356aa232e1SRander Wang 			snd_hdac_ext_bus_link_power_down(hlink);
4366aa232e1SRander Wang 	}
4376aa232e1SRander Wang 
4386aa232e1SRander Wang 	/* check dma status and clean up CORB/RIRB buffers */
4396aa232e1SRander Wang 	if (!bus->cmd_dma_state)
4406aa232e1SRander Wang 		snd_hdac_bus_stop_cmd_io(bus);
44124b6ff68SZhu Yingjiang #endif
442747503b1SLiam Girdwood 
443747503b1SLiam Girdwood 	/* enable ppcap interrupt */
444747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_enable(sdev, true);
445747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_int_enable(sdev, true);
446747503b1SLiam Girdwood 
447747503b1SLiam Girdwood 	return 0;
448747503b1SLiam Girdwood }
449747503b1SLiam Girdwood 
450747503b1SLiam Girdwood int hda_dsp_resume(struct snd_sof_dev *sdev)
451747503b1SLiam Girdwood {
452747503b1SLiam Girdwood 	/* init hda controller. DSP cores will be powered up during fw boot */
453fd15f2f5SRander Wang 	return hda_resume(sdev, false);
454747503b1SLiam Girdwood }
455747503b1SLiam Girdwood 
456747503b1SLiam Girdwood int hda_dsp_runtime_resume(struct snd_sof_dev *sdev)
457747503b1SLiam Girdwood {
458747503b1SLiam Girdwood 	/* init hda controller. DSP cores will be powered up during fw boot */
459fd15f2f5SRander Wang 	return hda_resume(sdev, true);
460747503b1SLiam Girdwood }
461747503b1SLiam Girdwood 
46287a6fe80SKai Vehmanen int hda_dsp_runtime_idle(struct snd_sof_dev *sdev)
46387a6fe80SKai Vehmanen {
46487a6fe80SKai Vehmanen 	struct hdac_bus *hbus = sof_to_bus(sdev);
46587a6fe80SKai Vehmanen 
46687a6fe80SKai Vehmanen 	if (hbus->codec_powered) {
46787a6fe80SKai Vehmanen 		dev_dbg(sdev->dev, "some codecs still powered (%08X), not idle\n",
46887a6fe80SKai Vehmanen 			(unsigned int)hbus->codec_powered);
46987a6fe80SKai Vehmanen 		return -EBUSY;
47087a6fe80SKai Vehmanen 	}
47187a6fe80SKai Vehmanen 
47287a6fe80SKai Vehmanen 	return 0;
47387a6fe80SKai Vehmanen }
47487a6fe80SKai Vehmanen 
4751c38c922SFred Oh int hda_dsp_runtime_suspend(struct snd_sof_dev *sdev)
476747503b1SLiam Girdwood {
477747503b1SLiam Girdwood 	/* stop hda controller and power dsp off */
4781c38c922SFred Oh 	return hda_suspend(sdev, true);
479747503b1SLiam Girdwood }
480747503b1SLiam Girdwood 
4811c38c922SFred Oh int hda_dsp_suspend(struct snd_sof_dev *sdev)
482747503b1SLiam Girdwood {
483747503b1SLiam Girdwood 	struct hdac_bus *bus = sof_to_bus(sdev);
484747503b1SLiam Girdwood 	int ret;
485747503b1SLiam Girdwood 
486747503b1SLiam Girdwood 	/* stop hda controller and power dsp off */
4871c38c922SFred Oh 	ret = hda_suspend(sdev, false);
488747503b1SLiam Girdwood 	if (ret < 0) {
489747503b1SLiam Girdwood 		dev_err(bus->dev, "error: suspending dsp\n");
490747503b1SLiam Girdwood 		return ret;
491747503b1SLiam Girdwood 	}
492747503b1SLiam Girdwood 
493747503b1SLiam Girdwood 	return 0;
494747503b1SLiam Girdwood }
495ed3baacdSRanjani Sridharan 
4967077a07aSRanjani Sridharan int hda_dsp_set_hw_params_upon_resume(struct snd_sof_dev *sdev)
497ed3baacdSRanjani Sridharan {
4987077a07aSRanjani Sridharan #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
499a3ebccb5SKai Vehmanen 	struct hdac_bus *bus = sof_to_bus(sdev);
5007077a07aSRanjani Sridharan 	struct snd_soc_pcm_runtime *rtd;
501a3ebccb5SKai Vehmanen 	struct hdac_ext_stream *stream;
5027077a07aSRanjani Sridharan 	struct hdac_ext_link *link;
503a3ebccb5SKai Vehmanen 	struct hdac_stream *s;
5047077a07aSRanjani Sridharan 	const char *name;
5057077a07aSRanjani Sridharan 	int stream_tag;
5067077a07aSRanjani Sridharan 
507ed3baacdSRanjani Sridharan 	/* set internal flag for BE */
508ed3baacdSRanjani Sridharan 	list_for_each_entry(s, &bus->stream_list, list) {
509ed3baacdSRanjani Sridharan 		stream = stream_to_hdac_ext_stream(s);
510a3ebccb5SKai Vehmanen 
5117077a07aSRanjani Sridharan 		/*
512934bf822SRander Wang 		 * clear stream. This should already be taken care for running
513934bf822SRander Wang 		 * streams when the SUSPEND trigger is called. But paused
514934bf822SRander Wang 		 * streams do not get suspended, so this needs to be done
515934bf822SRander Wang 		 * explicitly during suspend.
5167077a07aSRanjani Sridharan 		 */
5177077a07aSRanjani Sridharan 		if (stream->link_substream) {
5187077a07aSRanjani Sridharan 			rtd = snd_pcm_substream_chip(stream->link_substream);
5197077a07aSRanjani Sridharan 			name = rtd->codec_dai->component->name;
5207077a07aSRanjani Sridharan 			link = snd_hdac_ext_bus_get_link(bus, name);
5217077a07aSRanjani Sridharan 			if (!link)
5227077a07aSRanjani Sridharan 				return -EINVAL;
523810dbea3SRander Wang 
524810dbea3SRander Wang 			stream->link_prepared = 0;
525810dbea3SRander Wang 
526810dbea3SRander Wang 			if (hdac_stream(stream)->direction ==
527810dbea3SRander Wang 				SNDRV_PCM_STREAM_CAPTURE)
528810dbea3SRander Wang 				continue;
529810dbea3SRander Wang 
5307077a07aSRanjani Sridharan 			stream_tag = hdac_stream(stream)->stream_tag;
5317077a07aSRanjani Sridharan 			snd_hdac_ext_link_clear_stream_id(link, stream_tag);
532a3ebccb5SKai Vehmanen 		}
533ed3baacdSRanjani Sridharan 	}
5347077a07aSRanjani Sridharan #endif
5357077a07aSRanjani Sridharan 	return 0;
536ed3baacdSRanjani Sridharan }
537