xref: /OK3568_Linux_fs/kernel/sound/pci/hda/hda_controller.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *  Implementation of primary alsa driver code base for Intel HD Audio.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *  Copyright(c) 2004 Intel Corporation. All rights reserved.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *  Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
9*4882a593Smuzhiyun  *                     PeiSen Hou <pshou@realtek.com.tw>
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/clocksource.h>
13*4882a593Smuzhiyun #include <linux/delay.h>
14*4882a593Smuzhiyun #include <linux/interrupt.h>
15*4882a593Smuzhiyun #include <linux/kernel.h>
16*4882a593Smuzhiyun #include <linux/module.h>
17*4882a593Smuzhiyun #include <linux/pm_runtime.h>
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #ifdef CONFIG_X86
21*4882a593Smuzhiyun /* for art-tsc conversion */
22*4882a593Smuzhiyun #include <asm/tsc.h>
23*4882a593Smuzhiyun #endif
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <sound/core.h>
26*4882a593Smuzhiyun #include <sound/initval.h>
27*4882a593Smuzhiyun #include "hda_controller.h"
28*4882a593Smuzhiyun #include "hda_local.h"
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define CREATE_TRACE_POINTS
31*4882a593Smuzhiyun #include "hda_controller_trace.h"
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /* DSP lock helpers */
34*4882a593Smuzhiyun #define dsp_lock(dev)		snd_hdac_dsp_lock(azx_stream(dev))
35*4882a593Smuzhiyun #define dsp_unlock(dev)		snd_hdac_dsp_unlock(azx_stream(dev))
36*4882a593Smuzhiyun #define dsp_is_locked(dev)	snd_hdac_stream_is_locked(azx_stream(dev))
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /* assign a stream for the PCM */
39*4882a593Smuzhiyun static inline struct azx_dev *
azx_assign_device(struct azx * chip,struct snd_pcm_substream * substream)40*4882a593Smuzhiyun azx_assign_device(struct azx *chip, struct snd_pcm_substream *substream)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	struct hdac_stream *s;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	s = snd_hdac_stream_assign(azx_bus(chip), substream);
45*4882a593Smuzhiyun 	if (!s)
46*4882a593Smuzhiyun 		return NULL;
47*4882a593Smuzhiyun 	return stream_to_azx_dev(s);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /* release the assigned stream */
azx_release_device(struct azx_dev * azx_dev)51*4882a593Smuzhiyun static inline void azx_release_device(struct azx_dev *azx_dev)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	snd_hdac_stream_release(azx_stream(azx_dev));
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun static inline struct hda_pcm_stream *
to_hda_pcm_stream(struct snd_pcm_substream * substream)57*4882a593Smuzhiyun to_hda_pcm_stream(struct snd_pcm_substream *substream)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
60*4882a593Smuzhiyun 	return &apcm->info->stream[substream->stream];
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
azx_adjust_codec_delay(struct snd_pcm_substream * substream,u64 nsec)63*4882a593Smuzhiyun static u64 azx_adjust_codec_delay(struct snd_pcm_substream *substream,
64*4882a593Smuzhiyun 				u64 nsec)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
67*4882a593Smuzhiyun 	struct hda_pcm_stream *hinfo = to_hda_pcm_stream(substream);
68*4882a593Smuzhiyun 	u64 codec_frames, codec_nsecs;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	if (!hinfo->ops.get_delay)
71*4882a593Smuzhiyun 		return nsec;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	codec_frames = hinfo->ops.get_delay(hinfo, apcm->codec, substream);
74*4882a593Smuzhiyun 	codec_nsecs = div_u64(codec_frames * 1000000000LL,
75*4882a593Smuzhiyun 			      substream->runtime->rate);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
78*4882a593Smuzhiyun 		return nsec + codec_nsecs;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /*
84*4882a593Smuzhiyun  * PCM ops
85*4882a593Smuzhiyun  */
86*4882a593Smuzhiyun 
azx_pcm_close(struct snd_pcm_substream * substream)87*4882a593Smuzhiyun static int azx_pcm_close(struct snd_pcm_substream *substream)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
90*4882a593Smuzhiyun 	struct hda_pcm_stream *hinfo = to_hda_pcm_stream(substream);
91*4882a593Smuzhiyun 	struct azx *chip = apcm->chip;
92*4882a593Smuzhiyun 	struct azx_dev *azx_dev = get_azx_dev(substream);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	trace_azx_pcm_close(chip, azx_dev);
95*4882a593Smuzhiyun 	mutex_lock(&chip->open_mutex);
96*4882a593Smuzhiyun 	azx_release_device(azx_dev);
97*4882a593Smuzhiyun 	if (hinfo->ops.close)
98*4882a593Smuzhiyun 		hinfo->ops.close(hinfo, apcm->codec, substream);
99*4882a593Smuzhiyun 	snd_hda_power_down(apcm->codec);
100*4882a593Smuzhiyun 	mutex_unlock(&chip->open_mutex);
101*4882a593Smuzhiyun 	snd_hda_codec_pcm_put(apcm->info);
102*4882a593Smuzhiyun 	return 0;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
azx_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)105*4882a593Smuzhiyun static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
106*4882a593Smuzhiyun 			     struct snd_pcm_hw_params *hw_params)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
109*4882a593Smuzhiyun 	struct azx *chip = apcm->chip;
110*4882a593Smuzhiyun 	struct azx_dev *azx_dev = get_azx_dev(substream);
111*4882a593Smuzhiyun 	int ret = 0;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	trace_azx_pcm_hw_params(chip, azx_dev);
114*4882a593Smuzhiyun 	dsp_lock(azx_dev);
115*4882a593Smuzhiyun 	if (dsp_is_locked(azx_dev)) {
116*4882a593Smuzhiyun 		ret = -EBUSY;
117*4882a593Smuzhiyun 		goto unlock;
118*4882a593Smuzhiyun 	}
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	azx_dev->core.bufsize = 0;
121*4882a593Smuzhiyun 	azx_dev->core.period_bytes = 0;
122*4882a593Smuzhiyun 	azx_dev->core.format_val = 0;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun unlock:
125*4882a593Smuzhiyun 	dsp_unlock(azx_dev);
126*4882a593Smuzhiyun 	return ret;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
azx_pcm_hw_free(struct snd_pcm_substream * substream)129*4882a593Smuzhiyun static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
132*4882a593Smuzhiyun 	struct azx_dev *azx_dev = get_azx_dev(substream);
133*4882a593Smuzhiyun 	struct hda_pcm_stream *hinfo = to_hda_pcm_stream(substream);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/* reset BDL address */
136*4882a593Smuzhiyun 	dsp_lock(azx_dev);
137*4882a593Smuzhiyun 	if (!dsp_is_locked(azx_dev))
138*4882a593Smuzhiyun 		snd_hdac_stream_cleanup(azx_stream(azx_dev));
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	snd_hda_codec_cleanup(apcm->codec, hinfo, substream);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	azx_stream(azx_dev)->prepared = 0;
143*4882a593Smuzhiyun 	dsp_unlock(azx_dev);
144*4882a593Smuzhiyun 	return 0;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
azx_pcm_prepare(struct snd_pcm_substream * substream)147*4882a593Smuzhiyun static int azx_pcm_prepare(struct snd_pcm_substream *substream)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
150*4882a593Smuzhiyun 	struct azx *chip = apcm->chip;
151*4882a593Smuzhiyun 	struct azx_dev *azx_dev = get_azx_dev(substream);
152*4882a593Smuzhiyun 	struct hda_pcm_stream *hinfo = to_hda_pcm_stream(substream);
153*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
154*4882a593Smuzhiyun 	unsigned int format_val, stream_tag;
155*4882a593Smuzhiyun 	int err;
156*4882a593Smuzhiyun 	struct hda_spdif_out *spdif =
157*4882a593Smuzhiyun 		snd_hda_spdif_out_of_nid(apcm->codec, hinfo->nid);
158*4882a593Smuzhiyun 	unsigned short ctls = spdif ? spdif->ctls : 0;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	trace_azx_pcm_prepare(chip, azx_dev);
161*4882a593Smuzhiyun 	dsp_lock(azx_dev);
162*4882a593Smuzhiyun 	if (dsp_is_locked(azx_dev)) {
163*4882a593Smuzhiyun 		err = -EBUSY;
164*4882a593Smuzhiyun 		goto unlock;
165*4882a593Smuzhiyun 	}
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	snd_hdac_stream_reset(azx_stream(azx_dev));
168*4882a593Smuzhiyun 	format_val = snd_hdac_calc_stream_format(runtime->rate,
169*4882a593Smuzhiyun 						runtime->channels,
170*4882a593Smuzhiyun 						runtime->format,
171*4882a593Smuzhiyun 						hinfo->maxbps,
172*4882a593Smuzhiyun 						ctls);
173*4882a593Smuzhiyun 	if (!format_val) {
174*4882a593Smuzhiyun 		dev_err(chip->card->dev,
175*4882a593Smuzhiyun 			"invalid format_val, rate=%d, ch=%d, format=%d\n",
176*4882a593Smuzhiyun 			runtime->rate, runtime->channels, runtime->format);
177*4882a593Smuzhiyun 		err = -EINVAL;
178*4882a593Smuzhiyun 		goto unlock;
179*4882a593Smuzhiyun 	}
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	err = snd_hdac_stream_set_params(azx_stream(azx_dev), format_val);
182*4882a593Smuzhiyun 	if (err < 0)
183*4882a593Smuzhiyun 		goto unlock;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	snd_hdac_stream_setup(azx_stream(azx_dev));
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	stream_tag = azx_dev->core.stream_tag;
188*4882a593Smuzhiyun 	/* CA-IBG chips need the playback stream starting from 1 */
189*4882a593Smuzhiyun 	if ((chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND) &&
190*4882a593Smuzhiyun 	    stream_tag > chip->capture_streams)
191*4882a593Smuzhiyun 		stream_tag -= chip->capture_streams;
192*4882a593Smuzhiyun 	err = snd_hda_codec_prepare(apcm->codec, hinfo, stream_tag,
193*4882a593Smuzhiyun 				     azx_dev->core.format_val, substream);
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun  unlock:
196*4882a593Smuzhiyun 	if (!err)
197*4882a593Smuzhiyun 		azx_stream(azx_dev)->prepared = 1;
198*4882a593Smuzhiyun 	dsp_unlock(azx_dev);
199*4882a593Smuzhiyun 	return err;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
azx_pcm_trigger(struct snd_pcm_substream * substream,int cmd)202*4882a593Smuzhiyun static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
205*4882a593Smuzhiyun 	struct azx *chip = apcm->chip;
206*4882a593Smuzhiyun 	struct hdac_bus *bus = azx_bus(chip);
207*4882a593Smuzhiyun 	struct azx_dev *azx_dev;
208*4882a593Smuzhiyun 	struct snd_pcm_substream *s;
209*4882a593Smuzhiyun 	struct hdac_stream *hstr;
210*4882a593Smuzhiyun 	bool start;
211*4882a593Smuzhiyun 	int sbits = 0;
212*4882a593Smuzhiyun 	int sync_reg;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	azx_dev = get_azx_dev(substream);
215*4882a593Smuzhiyun 	trace_azx_pcm_trigger(chip, azx_dev, cmd);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	hstr = azx_stream(azx_dev);
218*4882a593Smuzhiyun 	if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
219*4882a593Smuzhiyun 		sync_reg = AZX_REG_OLD_SSYNC;
220*4882a593Smuzhiyun 	else
221*4882a593Smuzhiyun 		sync_reg = AZX_REG_SSYNC;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	if (dsp_is_locked(azx_dev) || !hstr->prepared)
224*4882a593Smuzhiyun 		return -EPIPE;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	switch (cmd) {
227*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_START:
228*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
229*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_RESUME:
230*4882a593Smuzhiyun 		start = true;
231*4882a593Smuzhiyun 		break;
232*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
233*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_SUSPEND:
234*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_STOP:
235*4882a593Smuzhiyun 		start = false;
236*4882a593Smuzhiyun 		break;
237*4882a593Smuzhiyun 	default:
238*4882a593Smuzhiyun 		return -EINVAL;
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	snd_pcm_group_for_each_entry(s, substream) {
242*4882a593Smuzhiyun 		if (s->pcm->card != substream->pcm->card)
243*4882a593Smuzhiyun 			continue;
244*4882a593Smuzhiyun 		azx_dev = get_azx_dev(s);
245*4882a593Smuzhiyun 		sbits |= 1 << azx_dev->core.index;
246*4882a593Smuzhiyun 		snd_pcm_trigger_done(s, substream);
247*4882a593Smuzhiyun 	}
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	spin_lock(&bus->reg_lock);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	/* first, set SYNC bits of corresponding streams */
252*4882a593Smuzhiyun 	snd_hdac_stream_sync_trigger(hstr, true, sbits, sync_reg);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	snd_pcm_group_for_each_entry(s, substream) {
255*4882a593Smuzhiyun 		if (s->pcm->card != substream->pcm->card)
256*4882a593Smuzhiyun 			continue;
257*4882a593Smuzhiyun 		azx_dev = get_azx_dev(s);
258*4882a593Smuzhiyun 		if (start) {
259*4882a593Smuzhiyun 			azx_dev->insufficient = 1;
260*4882a593Smuzhiyun 			snd_hdac_stream_start(azx_stream(azx_dev), true);
261*4882a593Smuzhiyun 		} else {
262*4882a593Smuzhiyun 			snd_hdac_stream_stop(azx_stream(azx_dev));
263*4882a593Smuzhiyun 		}
264*4882a593Smuzhiyun 	}
265*4882a593Smuzhiyun 	spin_unlock(&bus->reg_lock);
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	snd_hdac_stream_sync(hstr, start, sbits);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	spin_lock(&bus->reg_lock);
270*4882a593Smuzhiyun 	/* reset SYNC bits */
271*4882a593Smuzhiyun 	snd_hdac_stream_sync_trigger(hstr, false, sbits, sync_reg);
272*4882a593Smuzhiyun 	if (start)
273*4882a593Smuzhiyun 		snd_hdac_stream_timecounter_init(hstr, sbits);
274*4882a593Smuzhiyun 	spin_unlock(&bus->reg_lock);
275*4882a593Smuzhiyun 	return 0;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun 
azx_get_pos_lpib(struct azx * chip,struct azx_dev * azx_dev)278*4882a593Smuzhiyun unsigned int azx_get_pos_lpib(struct azx *chip, struct azx_dev *azx_dev)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun 	return snd_hdac_stream_get_pos_lpib(azx_stream(azx_dev));
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(azx_get_pos_lpib);
283*4882a593Smuzhiyun 
azx_get_pos_posbuf(struct azx * chip,struct azx_dev * azx_dev)284*4882a593Smuzhiyun unsigned int azx_get_pos_posbuf(struct azx *chip, struct azx_dev *azx_dev)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun 	return snd_hdac_stream_get_pos_posbuf(azx_stream(azx_dev));
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(azx_get_pos_posbuf);
289*4882a593Smuzhiyun 
azx_get_position(struct azx * chip,struct azx_dev * azx_dev)290*4882a593Smuzhiyun unsigned int azx_get_position(struct azx *chip,
291*4882a593Smuzhiyun 			      struct azx_dev *azx_dev)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	struct snd_pcm_substream *substream = azx_dev->core.substream;
294*4882a593Smuzhiyun 	unsigned int pos;
295*4882a593Smuzhiyun 	int stream = substream->stream;
296*4882a593Smuzhiyun 	int delay = 0;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (chip->get_position[stream])
299*4882a593Smuzhiyun 		pos = chip->get_position[stream](chip, azx_dev);
300*4882a593Smuzhiyun 	else /* use the position buffer as default */
301*4882a593Smuzhiyun 		pos = azx_get_pos_posbuf(chip, azx_dev);
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	if (pos >= azx_dev->core.bufsize)
304*4882a593Smuzhiyun 		pos = 0;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	if (substream->runtime) {
307*4882a593Smuzhiyun 		struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
308*4882a593Smuzhiyun 		struct hda_pcm_stream *hinfo = to_hda_pcm_stream(substream);
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 		if (chip->get_delay[stream])
311*4882a593Smuzhiyun 			delay += chip->get_delay[stream](chip, azx_dev, pos);
312*4882a593Smuzhiyun 		if (hinfo->ops.get_delay)
313*4882a593Smuzhiyun 			delay += hinfo->ops.get_delay(hinfo, apcm->codec,
314*4882a593Smuzhiyun 						      substream);
315*4882a593Smuzhiyun 		substream->runtime->delay = delay;
316*4882a593Smuzhiyun 	}
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	trace_azx_get_position(chip, azx_dev, pos, delay);
319*4882a593Smuzhiyun 	return pos;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(azx_get_position);
322*4882a593Smuzhiyun 
azx_pcm_pointer(struct snd_pcm_substream * substream)323*4882a593Smuzhiyun static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun 	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
326*4882a593Smuzhiyun 	struct azx *chip = apcm->chip;
327*4882a593Smuzhiyun 	struct azx_dev *azx_dev = get_azx_dev(substream);
328*4882a593Smuzhiyun 	return bytes_to_frames(substream->runtime,
329*4882a593Smuzhiyun 			       azx_get_position(chip, azx_dev));
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun /*
333*4882a593Smuzhiyun  * azx_scale64: Scale base by mult/div while not overflowing sanely
334*4882a593Smuzhiyun  *
335*4882a593Smuzhiyun  * Derived from scale64_check_overflow in kernel/time/timekeeping.c
336*4882a593Smuzhiyun  *
337*4882a593Smuzhiyun  * The tmestamps for a 48Khz stream can overflow after (2^64/10^9)/48K which
338*4882a593Smuzhiyun  * is about 384307 ie ~4.5 days.
339*4882a593Smuzhiyun  *
340*4882a593Smuzhiyun  * This scales the calculation so that overflow will happen but after 2^64 /
341*4882a593Smuzhiyun  * 48000 secs, which is pretty large!
342*4882a593Smuzhiyun  *
343*4882a593Smuzhiyun  * In caln below:
344*4882a593Smuzhiyun  *	base may overflow, but since there isn’t any additional division
345*4882a593Smuzhiyun  *	performed on base it’s OK
346*4882a593Smuzhiyun  *	rem can’t overflow because both are 32-bit values
347*4882a593Smuzhiyun  */
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun #ifdef CONFIG_X86
azx_scale64(u64 base,u32 num,u32 den)350*4882a593Smuzhiyun static u64 azx_scale64(u64 base, u32 num, u32 den)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun 	u64 rem;
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	rem = do_div(base, den);
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	base *= num;
357*4882a593Smuzhiyun 	rem *= num;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	do_div(rem, den);
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	return base + rem;
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun 
azx_get_sync_time(ktime_t * device,struct system_counterval_t * system,void * ctx)364*4882a593Smuzhiyun static int azx_get_sync_time(ktime_t *device,
365*4882a593Smuzhiyun 		struct system_counterval_t *system, void *ctx)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun 	struct snd_pcm_substream *substream = ctx;
368*4882a593Smuzhiyun 	struct azx_dev *azx_dev = get_azx_dev(substream);
369*4882a593Smuzhiyun 	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
370*4882a593Smuzhiyun 	struct azx *chip = apcm->chip;
371*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime;
372*4882a593Smuzhiyun 	u64 ll_counter, ll_counter_l, ll_counter_h;
373*4882a593Smuzhiyun 	u64 tsc_counter, tsc_counter_l, tsc_counter_h;
374*4882a593Smuzhiyun 	u32 wallclk_ctr, wallclk_cycles;
375*4882a593Smuzhiyun 	bool direction;
376*4882a593Smuzhiyun 	u32 dma_select;
377*4882a593Smuzhiyun 	u32 timeout;
378*4882a593Smuzhiyun 	u32 retry_count = 0;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	runtime = substream->runtime;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
383*4882a593Smuzhiyun 		direction = 1;
384*4882a593Smuzhiyun 	else
385*4882a593Smuzhiyun 		direction = 0;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	/* 0th stream tag is not used, so DMA ch 0 is for 1st stream tag */
388*4882a593Smuzhiyun 	do {
389*4882a593Smuzhiyun 		timeout = 100;
390*4882a593Smuzhiyun 		dma_select = (direction << GTSCC_CDMAS_DMA_DIR_SHIFT) |
391*4882a593Smuzhiyun 					(azx_dev->core.stream_tag - 1);
392*4882a593Smuzhiyun 		snd_hdac_chip_writel(azx_bus(chip), GTSCC, dma_select);
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 		/* Enable the capture */
395*4882a593Smuzhiyun 		snd_hdac_chip_updatel(azx_bus(chip), GTSCC, 0, GTSCC_TSCCI_MASK);
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 		while (timeout) {
398*4882a593Smuzhiyun 			if (snd_hdac_chip_readl(azx_bus(chip), GTSCC) &
399*4882a593Smuzhiyun 						GTSCC_TSCCD_MASK)
400*4882a593Smuzhiyun 				break;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 			timeout--;
403*4882a593Smuzhiyun 		}
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 		if (!timeout) {
406*4882a593Smuzhiyun 			dev_err(chip->card->dev, "GTSCC capture Timedout!\n");
407*4882a593Smuzhiyun 			return -EIO;
408*4882a593Smuzhiyun 		}
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 		/* Read wall clock counter */
411*4882a593Smuzhiyun 		wallclk_ctr = snd_hdac_chip_readl(azx_bus(chip), WALFCC);
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 		/* Read TSC counter */
414*4882a593Smuzhiyun 		tsc_counter_l = snd_hdac_chip_readl(azx_bus(chip), TSCCL);
415*4882a593Smuzhiyun 		tsc_counter_h = snd_hdac_chip_readl(azx_bus(chip), TSCCU);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 		/* Read Link counter */
418*4882a593Smuzhiyun 		ll_counter_l = snd_hdac_chip_readl(azx_bus(chip), LLPCL);
419*4882a593Smuzhiyun 		ll_counter_h = snd_hdac_chip_readl(azx_bus(chip), LLPCU);
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 		/* Ack: registers read done */
422*4882a593Smuzhiyun 		snd_hdac_chip_writel(azx_bus(chip), GTSCC, GTSCC_TSCCD_SHIFT);
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 		tsc_counter = (tsc_counter_h << TSCCU_CCU_SHIFT) |
425*4882a593Smuzhiyun 						tsc_counter_l;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 		ll_counter = (ll_counter_h << LLPC_CCU_SHIFT) |	ll_counter_l;
428*4882a593Smuzhiyun 		wallclk_cycles = wallclk_ctr & WALFCC_CIF_MASK;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 		/*
431*4882a593Smuzhiyun 		 * An error occurs near frame "rollover". The clocks in
432*4882a593Smuzhiyun 		 * frame value indicates whether this error may have
433*4882a593Smuzhiyun 		 * occurred. Here we use the value of 10 i.e.,
434*4882a593Smuzhiyun 		 * HDA_MAX_CYCLE_OFFSET
435*4882a593Smuzhiyun 		 */
436*4882a593Smuzhiyun 		if (wallclk_cycles < HDA_MAX_CYCLE_VALUE - HDA_MAX_CYCLE_OFFSET
437*4882a593Smuzhiyun 					&& wallclk_cycles > HDA_MAX_CYCLE_OFFSET)
438*4882a593Smuzhiyun 			break;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 		/*
441*4882a593Smuzhiyun 		 * Sleep before we read again, else we may again get
442*4882a593Smuzhiyun 		 * value near to MAX_CYCLE. Try to sleep for different
443*4882a593Smuzhiyun 		 * amount of time so we dont hit the same number again
444*4882a593Smuzhiyun 		 */
445*4882a593Smuzhiyun 		udelay(retry_count++);
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	} while (retry_count != HDA_MAX_CYCLE_READ_RETRY);
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	if (retry_count == HDA_MAX_CYCLE_READ_RETRY) {
450*4882a593Smuzhiyun 		dev_err_ratelimited(chip->card->dev,
451*4882a593Smuzhiyun 			"Error in WALFCC cycle count\n");
452*4882a593Smuzhiyun 		return -EIO;
453*4882a593Smuzhiyun 	}
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	*device = ns_to_ktime(azx_scale64(ll_counter,
456*4882a593Smuzhiyun 				NSEC_PER_SEC, runtime->rate));
457*4882a593Smuzhiyun 	*device = ktime_add_ns(*device, (wallclk_cycles * NSEC_PER_SEC) /
458*4882a593Smuzhiyun 			       ((HDA_MAX_CYCLE_VALUE + 1) * runtime->rate));
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	*system = convert_art_to_tsc(tsc_counter);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	return 0;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun #else
azx_get_sync_time(ktime_t * device,struct system_counterval_t * system,void * ctx)466*4882a593Smuzhiyun static int azx_get_sync_time(ktime_t *device,
467*4882a593Smuzhiyun 		struct system_counterval_t *system, void *ctx)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun 	return -ENXIO;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun #endif
472*4882a593Smuzhiyun 
azx_get_crosststamp(struct snd_pcm_substream * substream,struct system_device_crosststamp * xtstamp)473*4882a593Smuzhiyun static int azx_get_crosststamp(struct snd_pcm_substream *substream,
474*4882a593Smuzhiyun 			      struct system_device_crosststamp *xtstamp)
475*4882a593Smuzhiyun {
476*4882a593Smuzhiyun 	return get_device_system_crosststamp(azx_get_sync_time,
477*4882a593Smuzhiyun 					substream, NULL, xtstamp);
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun 
is_link_time_supported(struct snd_pcm_runtime * runtime,struct snd_pcm_audio_tstamp_config * ts)480*4882a593Smuzhiyun static inline bool is_link_time_supported(struct snd_pcm_runtime *runtime,
481*4882a593Smuzhiyun 				struct snd_pcm_audio_tstamp_config *ts)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	if (runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_SYNCHRONIZED_ATIME)
484*4882a593Smuzhiyun 		if (ts->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK_SYNCHRONIZED)
485*4882a593Smuzhiyun 			return true;
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	return false;
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun 
azx_get_time_info(struct snd_pcm_substream * substream,struct timespec64 * system_ts,struct timespec64 * audio_ts,struct snd_pcm_audio_tstamp_config * audio_tstamp_config,struct snd_pcm_audio_tstamp_report * audio_tstamp_report)490*4882a593Smuzhiyun static int azx_get_time_info(struct snd_pcm_substream *substream,
491*4882a593Smuzhiyun 			struct timespec64 *system_ts, struct timespec64 *audio_ts,
492*4882a593Smuzhiyun 			struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
493*4882a593Smuzhiyun 			struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
494*4882a593Smuzhiyun {
495*4882a593Smuzhiyun 	struct azx_dev *azx_dev = get_azx_dev(substream);
496*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
497*4882a593Smuzhiyun 	struct system_device_crosststamp xtstamp;
498*4882a593Smuzhiyun 	int ret;
499*4882a593Smuzhiyun 	u64 nsec;
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	if ((substream->runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_ATIME) &&
502*4882a593Smuzhiyun 		(audio_tstamp_config->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK)) {
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 		snd_pcm_gettime(substream->runtime, system_ts);
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 		nsec = timecounter_read(&azx_dev->core.tc);
507*4882a593Smuzhiyun 		nsec = div_u64(nsec, 3); /* can be optimized */
508*4882a593Smuzhiyun 		if (audio_tstamp_config->report_delay)
509*4882a593Smuzhiyun 			nsec = azx_adjust_codec_delay(substream, nsec);
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 		*audio_ts = ns_to_timespec64(nsec);
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 		audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
514*4882a593Smuzhiyun 		audio_tstamp_report->accuracy_report = 1; /* rest of structure is valid */
515*4882a593Smuzhiyun 		audio_tstamp_report->accuracy = 42; /* 24 MHz WallClock == 42ns resolution */
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	} else if (is_link_time_supported(runtime, audio_tstamp_config)) {
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 		ret = azx_get_crosststamp(substream, &xtstamp);
520*4882a593Smuzhiyun 		if (ret)
521*4882a593Smuzhiyun 			return ret;
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 		switch (runtime->tstamp_type) {
524*4882a593Smuzhiyun 		case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC:
525*4882a593Smuzhiyun 			return -EINVAL;
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 		case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC_RAW:
528*4882a593Smuzhiyun 			*system_ts = ktime_to_timespec64(xtstamp.sys_monoraw);
529*4882a593Smuzhiyun 			break;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 		default:
532*4882a593Smuzhiyun 			*system_ts = ktime_to_timespec64(xtstamp.sys_realtime);
533*4882a593Smuzhiyun 			break;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 		}
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 		*audio_ts = ktime_to_timespec64(xtstamp.device);
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 		audio_tstamp_report->actual_type =
540*4882a593Smuzhiyun 			SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK_SYNCHRONIZED;
541*4882a593Smuzhiyun 		audio_tstamp_report->accuracy_report = 1;
542*4882a593Smuzhiyun 		/* 24 MHz WallClock == 42ns resolution */
543*4882a593Smuzhiyun 		audio_tstamp_report->accuracy = 42;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	} else {
546*4882a593Smuzhiyun 		audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
547*4882a593Smuzhiyun 	}
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	return 0;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun static const struct snd_pcm_hardware azx_pcm_hw = {
553*4882a593Smuzhiyun 	.info =			(SNDRV_PCM_INFO_MMAP |
554*4882a593Smuzhiyun 				 SNDRV_PCM_INFO_INTERLEAVED |
555*4882a593Smuzhiyun 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
556*4882a593Smuzhiyun 				 SNDRV_PCM_INFO_MMAP_VALID |
557*4882a593Smuzhiyun 				 /* No full-resume yet implemented */
558*4882a593Smuzhiyun 				 /* SNDRV_PCM_INFO_RESUME |*/
559*4882a593Smuzhiyun 				 SNDRV_PCM_INFO_PAUSE |
560*4882a593Smuzhiyun 				 SNDRV_PCM_INFO_SYNC_START |
561*4882a593Smuzhiyun 				 SNDRV_PCM_INFO_HAS_WALL_CLOCK | /* legacy */
562*4882a593Smuzhiyun 				 SNDRV_PCM_INFO_HAS_LINK_ATIME |
563*4882a593Smuzhiyun 				 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
564*4882a593Smuzhiyun 	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
565*4882a593Smuzhiyun 	.rates =		SNDRV_PCM_RATE_48000,
566*4882a593Smuzhiyun 	.rate_min =		48000,
567*4882a593Smuzhiyun 	.rate_max =		48000,
568*4882a593Smuzhiyun 	.channels_min =		2,
569*4882a593Smuzhiyun 	.channels_max =		2,
570*4882a593Smuzhiyun 	.buffer_bytes_max =	AZX_MAX_BUF_SIZE,
571*4882a593Smuzhiyun 	.period_bytes_min =	128,
572*4882a593Smuzhiyun 	.period_bytes_max =	AZX_MAX_BUF_SIZE / 2,
573*4882a593Smuzhiyun 	.periods_min =		2,
574*4882a593Smuzhiyun 	.periods_max =		AZX_MAX_FRAG,
575*4882a593Smuzhiyun 	.fifo_size =		0,
576*4882a593Smuzhiyun };
577*4882a593Smuzhiyun 
azx_pcm_open(struct snd_pcm_substream * substream)578*4882a593Smuzhiyun static int azx_pcm_open(struct snd_pcm_substream *substream)
579*4882a593Smuzhiyun {
580*4882a593Smuzhiyun 	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
581*4882a593Smuzhiyun 	struct hda_pcm_stream *hinfo = to_hda_pcm_stream(substream);
582*4882a593Smuzhiyun 	struct azx *chip = apcm->chip;
583*4882a593Smuzhiyun 	struct azx_dev *azx_dev;
584*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
585*4882a593Smuzhiyun 	int err;
586*4882a593Smuzhiyun 	int buff_step;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	snd_hda_codec_pcm_get(apcm->info);
589*4882a593Smuzhiyun 	mutex_lock(&chip->open_mutex);
590*4882a593Smuzhiyun 	azx_dev = azx_assign_device(chip, substream);
591*4882a593Smuzhiyun 	trace_azx_pcm_open(chip, azx_dev);
592*4882a593Smuzhiyun 	if (azx_dev == NULL) {
593*4882a593Smuzhiyun 		err = -EBUSY;
594*4882a593Smuzhiyun 		goto unlock;
595*4882a593Smuzhiyun 	}
596*4882a593Smuzhiyun 	runtime->private_data = azx_dev;
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	runtime->hw = azx_pcm_hw;
599*4882a593Smuzhiyun 	if (chip->gts_present)
600*4882a593Smuzhiyun 		runtime->hw.info |= SNDRV_PCM_INFO_HAS_LINK_SYNCHRONIZED_ATIME;
601*4882a593Smuzhiyun 	runtime->hw.channels_min = hinfo->channels_min;
602*4882a593Smuzhiyun 	runtime->hw.channels_max = hinfo->channels_max;
603*4882a593Smuzhiyun 	runtime->hw.formats = hinfo->formats;
604*4882a593Smuzhiyun 	runtime->hw.rates = hinfo->rates;
605*4882a593Smuzhiyun 	snd_pcm_limit_hw_rates(runtime);
606*4882a593Smuzhiyun 	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	/* avoid wrap-around with wall-clock */
609*4882a593Smuzhiyun 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
610*4882a593Smuzhiyun 				     20,
611*4882a593Smuzhiyun 				     178000000);
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	if (chip->align_buffer_size)
614*4882a593Smuzhiyun 		/* constrain buffer sizes to be multiple of 128
615*4882a593Smuzhiyun 		   bytes. This is more efficient in terms of memory
616*4882a593Smuzhiyun 		   access but isn't required by the HDA spec and
617*4882a593Smuzhiyun 		   prevents users from specifying exact period/buffer
618*4882a593Smuzhiyun 		   sizes. For example for 44.1kHz, a period size set
619*4882a593Smuzhiyun 		   to 20ms will be rounded to 19.59ms. */
620*4882a593Smuzhiyun 		buff_step = 128;
621*4882a593Smuzhiyun 	else
622*4882a593Smuzhiyun 		/* Don't enforce steps on buffer sizes, still need to
623*4882a593Smuzhiyun 		   be multiple of 4 bytes (HDA spec). Tested on Intel
624*4882a593Smuzhiyun 		   HDA controllers, may not work on all devices where
625*4882a593Smuzhiyun 		   option needs to be disabled */
626*4882a593Smuzhiyun 		buff_step = 4;
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
629*4882a593Smuzhiyun 				   buff_step);
630*4882a593Smuzhiyun 	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
631*4882a593Smuzhiyun 				   buff_step);
632*4882a593Smuzhiyun 	snd_hda_power_up(apcm->codec);
633*4882a593Smuzhiyun 	if (hinfo->ops.open)
634*4882a593Smuzhiyun 		err = hinfo->ops.open(hinfo, apcm->codec, substream);
635*4882a593Smuzhiyun 	else
636*4882a593Smuzhiyun 		err = -ENODEV;
637*4882a593Smuzhiyun 	if (err < 0) {
638*4882a593Smuzhiyun 		azx_release_device(azx_dev);
639*4882a593Smuzhiyun 		goto powerdown;
640*4882a593Smuzhiyun 	}
641*4882a593Smuzhiyun 	snd_pcm_limit_hw_rates(runtime);
642*4882a593Smuzhiyun 	/* sanity check */
643*4882a593Smuzhiyun 	if (snd_BUG_ON(!runtime->hw.channels_min) ||
644*4882a593Smuzhiyun 	    snd_BUG_ON(!runtime->hw.channels_max) ||
645*4882a593Smuzhiyun 	    snd_BUG_ON(!runtime->hw.formats) ||
646*4882a593Smuzhiyun 	    snd_BUG_ON(!runtime->hw.rates)) {
647*4882a593Smuzhiyun 		azx_release_device(azx_dev);
648*4882a593Smuzhiyun 		if (hinfo->ops.close)
649*4882a593Smuzhiyun 			hinfo->ops.close(hinfo, apcm->codec, substream);
650*4882a593Smuzhiyun 		err = -EINVAL;
651*4882a593Smuzhiyun 		goto powerdown;
652*4882a593Smuzhiyun 	}
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	/* disable LINK_ATIME timestamps for capture streams
655*4882a593Smuzhiyun 	   until we figure out how to handle digital inputs */
656*4882a593Smuzhiyun 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
657*4882a593Smuzhiyun 		runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK; /* legacy */
658*4882a593Smuzhiyun 		runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_LINK_ATIME;
659*4882a593Smuzhiyun 	}
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	snd_pcm_set_sync(substream);
662*4882a593Smuzhiyun 	mutex_unlock(&chip->open_mutex);
663*4882a593Smuzhiyun 	return 0;
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun  powerdown:
666*4882a593Smuzhiyun 	snd_hda_power_down(apcm->codec);
667*4882a593Smuzhiyun  unlock:
668*4882a593Smuzhiyun 	mutex_unlock(&chip->open_mutex);
669*4882a593Smuzhiyun 	snd_hda_codec_pcm_put(apcm->info);
670*4882a593Smuzhiyun 	return err;
671*4882a593Smuzhiyun }
672*4882a593Smuzhiyun 
azx_pcm_mmap(struct snd_pcm_substream * substream,struct vm_area_struct * area)673*4882a593Smuzhiyun static int azx_pcm_mmap(struct snd_pcm_substream *substream,
674*4882a593Smuzhiyun 			struct vm_area_struct *area)
675*4882a593Smuzhiyun {
676*4882a593Smuzhiyun 	struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
677*4882a593Smuzhiyun 	struct azx *chip = apcm->chip;
678*4882a593Smuzhiyun 	if (chip->ops->pcm_mmap_prepare)
679*4882a593Smuzhiyun 		chip->ops->pcm_mmap_prepare(substream, area);
680*4882a593Smuzhiyun 	return snd_pcm_lib_default_mmap(substream, area);
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun static const struct snd_pcm_ops azx_pcm_ops = {
684*4882a593Smuzhiyun 	.open = azx_pcm_open,
685*4882a593Smuzhiyun 	.close = azx_pcm_close,
686*4882a593Smuzhiyun 	.hw_params = azx_pcm_hw_params,
687*4882a593Smuzhiyun 	.hw_free = azx_pcm_hw_free,
688*4882a593Smuzhiyun 	.prepare = azx_pcm_prepare,
689*4882a593Smuzhiyun 	.trigger = azx_pcm_trigger,
690*4882a593Smuzhiyun 	.pointer = azx_pcm_pointer,
691*4882a593Smuzhiyun 	.get_time_info =  azx_get_time_info,
692*4882a593Smuzhiyun 	.mmap = azx_pcm_mmap,
693*4882a593Smuzhiyun };
694*4882a593Smuzhiyun 
azx_pcm_free(struct snd_pcm * pcm)695*4882a593Smuzhiyun static void azx_pcm_free(struct snd_pcm *pcm)
696*4882a593Smuzhiyun {
697*4882a593Smuzhiyun 	struct azx_pcm *apcm = pcm->private_data;
698*4882a593Smuzhiyun 	if (apcm) {
699*4882a593Smuzhiyun 		list_del(&apcm->list);
700*4882a593Smuzhiyun 		apcm->info->pcm = NULL;
701*4882a593Smuzhiyun 		kfree(apcm);
702*4882a593Smuzhiyun 	}
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun #define MAX_PREALLOC_SIZE	(32 * 1024 * 1024)
706*4882a593Smuzhiyun 
snd_hda_attach_pcm_stream(struct hda_bus * _bus,struct hda_codec * codec,struct hda_pcm * cpcm)707*4882a593Smuzhiyun int snd_hda_attach_pcm_stream(struct hda_bus *_bus, struct hda_codec *codec,
708*4882a593Smuzhiyun 			      struct hda_pcm *cpcm)
709*4882a593Smuzhiyun {
710*4882a593Smuzhiyun 	struct hdac_bus *bus = &_bus->core;
711*4882a593Smuzhiyun 	struct azx *chip = bus_to_azx(bus);
712*4882a593Smuzhiyun 	struct snd_pcm *pcm;
713*4882a593Smuzhiyun 	struct azx_pcm *apcm;
714*4882a593Smuzhiyun 	int pcm_dev = cpcm->device;
715*4882a593Smuzhiyun 	unsigned int size;
716*4882a593Smuzhiyun 	int s, err;
717*4882a593Smuzhiyun 	int type = SNDRV_DMA_TYPE_DEV_SG;
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	list_for_each_entry(apcm, &chip->pcm_list, list) {
720*4882a593Smuzhiyun 		if (apcm->pcm->device == pcm_dev) {
721*4882a593Smuzhiyun 			dev_err(chip->card->dev, "PCM %d already exists\n",
722*4882a593Smuzhiyun 				pcm_dev);
723*4882a593Smuzhiyun 			return -EBUSY;
724*4882a593Smuzhiyun 		}
725*4882a593Smuzhiyun 	}
726*4882a593Smuzhiyun 	err = snd_pcm_new(chip->card, cpcm->name, pcm_dev,
727*4882a593Smuzhiyun 			  cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
728*4882a593Smuzhiyun 			  cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams,
729*4882a593Smuzhiyun 			  &pcm);
730*4882a593Smuzhiyun 	if (err < 0)
731*4882a593Smuzhiyun 		return err;
732*4882a593Smuzhiyun 	strlcpy(pcm->name, cpcm->name, sizeof(pcm->name));
733*4882a593Smuzhiyun 	apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
734*4882a593Smuzhiyun 	if (apcm == NULL) {
735*4882a593Smuzhiyun 		snd_device_free(chip->card, pcm);
736*4882a593Smuzhiyun 		return -ENOMEM;
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun 	apcm->chip = chip;
739*4882a593Smuzhiyun 	apcm->pcm = pcm;
740*4882a593Smuzhiyun 	apcm->codec = codec;
741*4882a593Smuzhiyun 	apcm->info = cpcm;
742*4882a593Smuzhiyun 	pcm->private_data = apcm;
743*4882a593Smuzhiyun 	pcm->private_free = azx_pcm_free;
744*4882a593Smuzhiyun 	if (cpcm->pcm_type == HDA_PCM_TYPE_MODEM)
745*4882a593Smuzhiyun 		pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
746*4882a593Smuzhiyun 	list_add_tail(&apcm->list, &chip->pcm_list);
747*4882a593Smuzhiyun 	cpcm->pcm = pcm;
748*4882a593Smuzhiyun 	for (s = 0; s < 2; s++) {
749*4882a593Smuzhiyun 		if (cpcm->stream[s].substreams)
750*4882a593Smuzhiyun 			snd_pcm_set_ops(pcm, s, &azx_pcm_ops);
751*4882a593Smuzhiyun 	}
752*4882a593Smuzhiyun 	/* buffer pre-allocation */
753*4882a593Smuzhiyun 	size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
754*4882a593Smuzhiyun 	if (size > MAX_PREALLOC_SIZE)
755*4882a593Smuzhiyun 		size = MAX_PREALLOC_SIZE;
756*4882a593Smuzhiyun 	if (chip->uc_buffer)
757*4882a593Smuzhiyun 		type = SNDRV_DMA_TYPE_DEV_UC_SG;
758*4882a593Smuzhiyun 	snd_pcm_set_managed_buffer_all(pcm, type, chip->card->dev,
759*4882a593Smuzhiyun 				       size, MAX_PREALLOC_SIZE);
760*4882a593Smuzhiyun 	return 0;
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun 
azx_command_addr(u32 cmd)763*4882a593Smuzhiyun static unsigned int azx_command_addr(u32 cmd)
764*4882a593Smuzhiyun {
765*4882a593Smuzhiyun 	unsigned int addr = cmd >> 28;
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	if (addr >= AZX_MAX_CODECS) {
768*4882a593Smuzhiyun 		snd_BUG();
769*4882a593Smuzhiyun 		addr = 0;
770*4882a593Smuzhiyun 	}
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	return addr;
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun /* receive a response */
azx_rirb_get_response(struct hdac_bus * bus,unsigned int addr,unsigned int * res)776*4882a593Smuzhiyun static int azx_rirb_get_response(struct hdac_bus *bus, unsigned int addr,
777*4882a593Smuzhiyun 				 unsigned int *res)
778*4882a593Smuzhiyun {
779*4882a593Smuzhiyun 	struct azx *chip = bus_to_azx(bus);
780*4882a593Smuzhiyun 	struct hda_bus *hbus = &chip->bus;
781*4882a593Smuzhiyun 	int err;
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun  again:
784*4882a593Smuzhiyun 	err = snd_hdac_bus_get_response(bus, addr, res);
785*4882a593Smuzhiyun 	if (!err)
786*4882a593Smuzhiyun 		return 0;
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	if (hbus->no_response_fallback)
789*4882a593Smuzhiyun 		return -EIO;
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	if (!bus->polling_mode) {
792*4882a593Smuzhiyun 		dev_warn(chip->card->dev,
793*4882a593Smuzhiyun 			 "azx_get_response timeout, switching to polling mode: last cmd=0x%08x\n",
794*4882a593Smuzhiyun 			 bus->last_cmd[addr]);
795*4882a593Smuzhiyun 		bus->polling_mode = 1;
796*4882a593Smuzhiyun 		goto again;
797*4882a593Smuzhiyun 	}
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	if (chip->msi) {
800*4882a593Smuzhiyun 		dev_warn(chip->card->dev,
801*4882a593Smuzhiyun 			 "No response from codec, disabling MSI: last cmd=0x%08x\n",
802*4882a593Smuzhiyun 			 bus->last_cmd[addr]);
803*4882a593Smuzhiyun 		if (chip->ops->disable_msi_reset_irq &&
804*4882a593Smuzhiyun 		    chip->ops->disable_msi_reset_irq(chip) < 0)
805*4882a593Smuzhiyun 			return -EIO;
806*4882a593Smuzhiyun 		goto again;
807*4882a593Smuzhiyun 	}
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	if (chip->probing) {
810*4882a593Smuzhiyun 		/* If this critical timeout happens during the codec probing
811*4882a593Smuzhiyun 		 * phase, this is likely an access to a non-existing codec
812*4882a593Smuzhiyun 		 * slot.  Better to return an error and reset the system.
813*4882a593Smuzhiyun 		 */
814*4882a593Smuzhiyun 		return -EIO;
815*4882a593Smuzhiyun 	}
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	/* no fallback mechanism? */
818*4882a593Smuzhiyun 	if (!chip->fallback_to_single_cmd)
819*4882a593Smuzhiyun 		return -EIO;
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 	/* a fatal communication error; need either to reset or to fallback
822*4882a593Smuzhiyun 	 * to the single_cmd mode
823*4882a593Smuzhiyun 	 */
824*4882a593Smuzhiyun 	if (hbus->allow_bus_reset && !hbus->response_reset && !hbus->in_reset) {
825*4882a593Smuzhiyun 		hbus->response_reset = 1;
826*4882a593Smuzhiyun 		dev_err(chip->card->dev,
827*4882a593Smuzhiyun 			"No response from codec, resetting bus: last cmd=0x%08x\n",
828*4882a593Smuzhiyun 			bus->last_cmd[addr]);
829*4882a593Smuzhiyun 		return -EAGAIN; /* give a chance to retry */
830*4882a593Smuzhiyun 	}
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	dev_err(chip->card->dev,
833*4882a593Smuzhiyun 		"azx_get_response timeout, switching to single_cmd mode: last cmd=0x%08x\n",
834*4882a593Smuzhiyun 		bus->last_cmd[addr]);
835*4882a593Smuzhiyun 	chip->single_cmd = 1;
836*4882a593Smuzhiyun 	hbus->response_reset = 0;
837*4882a593Smuzhiyun 	snd_hdac_bus_stop_cmd_io(bus);
838*4882a593Smuzhiyun 	return -EIO;
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun /*
842*4882a593Smuzhiyun  * Use the single immediate command instead of CORB/RIRB for simplicity
843*4882a593Smuzhiyun  *
844*4882a593Smuzhiyun  * Note: according to Intel, this is not preferred use.  The command was
845*4882a593Smuzhiyun  *       intended for the BIOS only, and may get confused with unsolicited
846*4882a593Smuzhiyun  *       responses.  So, we shouldn't use it for normal operation from the
847*4882a593Smuzhiyun  *       driver.
848*4882a593Smuzhiyun  *       I left the codes, however, for debugging/testing purposes.
849*4882a593Smuzhiyun  */
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun /* receive a response */
azx_single_wait_for_response(struct azx * chip,unsigned int addr)852*4882a593Smuzhiyun static int azx_single_wait_for_response(struct azx *chip, unsigned int addr)
853*4882a593Smuzhiyun {
854*4882a593Smuzhiyun 	int timeout = 50;
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	while (timeout--) {
857*4882a593Smuzhiyun 		/* check IRV busy bit */
858*4882a593Smuzhiyun 		if (azx_readw(chip, IRS) & AZX_IRS_VALID) {
859*4882a593Smuzhiyun 			/* reuse rirb.res as the response return value */
860*4882a593Smuzhiyun 			azx_bus(chip)->rirb.res[addr] = azx_readl(chip, IR);
861*4882a593Smuzhiyun 			return 0;
862*4882a593Smuzhiyun 		}
863*4882a593Smuzhiyun 		udelay(1);
864*4882a593Smuzhiyun 	}
865*4882a593Smuzhiyun 	if (printk_ratelimit())
866*4882a593Smuzhiyun 		dev_dbg(chip->card->dev, "get_response timeout: IRS=0x%x\n",
867*4882a593Smuzhiyun 			azx_readw(chip, IRS));
868*4882a593Smuzhiyun 	azx_bus(chip)->rirb.res[addr] = -1;
869*4882a593Smuzhiyun 	return -EIO;
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun /* send a command */
azx_single_send_cmd(struct hdac_bus * bus,u32 val)873*4882a593Smuzhiyun static int azx_single_send_cmd(struct hdac_bus *bus, u32 val)
874*4882a593Smuzhiyun {
875*4882a593Smuzhiyun 	struct azx *chip = bus_to_azx(bus);
876*4882a593Smuzhiyun 	unsigned int addr = azx_command_addr(val);
877*4882a593Smuzhiyun 	int timeout = 50;
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 	bus->last_cmd[azx_command_addr(val)] = val;
880*4882a593Smuzhiyun 	while (timeout--) {
881*4882a593Smuzhiyun 		/* check ICB busy bit */
882*4882a593Smuzhiyun 		if (!((azx_readw(chip, IRS) & AZX_IRS_BUSY))) {
883*4882a593Smuzhiyun 			/* Clear IRV valid bit */
884*4882a593Smuzhiyun 			azx_writew(chip, IRS, azx_readw(chip, IRS) |
885*4882a593Smuzhiyun 				   AZX_IRS_VALID);
886*4882a593Smuzhiyun 			azx_writel(chip, IC, val);
887*4882a593Smuzhiyun 			azx_writew(chip, IRS, azx_readw(chip, IRS) |
888*4882a593Smuzhiyun 				   AZX_IRS_BUSY);
889*4882a593Smuzhiyun 			return azx_single_wait_for_response(chip, addr);
890*4882a593Smuzhiyun 		}
891*4882a593Smuzhiyun 		udelay(1);
892*4882a593Smuzhiyun 	}
893*4882a593Smuzhiyun 	if (printk_ratelimit())
894*4882a593Smuzhiyun 		dev_dbg(chip->card->dev,
895*4882a593Smuzhiyun 			"send_cmd timeout: IRS=0x%x, val=0x%x\n",
896*4882a593Smuzhiyun 			azx_readw(chip, IRS), val);
897*4882a593Smuzhiyun 	return -EIO;
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun /* receive a response */
azx_single_get_response(struct hdac_bus * bus,unsigned int addr,unsigned int * res)901*4882a593Smuzhiyun static int azx_single_get_response(struct hdac_bus *bus, unsigned int addr,
902*4882a593Smuzhiyun 				   unsigned int *res)
903*4882a593Smuzhiyun {
904*4882a593Smuzhiyun 	if (res)
905*4882a593Smuzhiyun 		*res = bus->rirb.res[addr];
906*4882a593Smuzhiyun 	return 0;
907*4882a593Smuzhiyun }
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun /*
910*4882a593Smuzhiyun  * The below are the main callbacks from hda_codec.
911*4882a593Smuzhiyun  *
912*4882a593Smuzhiyun  * They are just the skeleton to call sub-callbacks according to the
913*4882a593Smuzhiyun  * current setting of chip->single_cmd.
914*4882a593Smuzhiyun  */
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun /* send a command */
azx_send_cmd(struct hdac_bus * bus,unsigned int val)917*4882a593Smuzhiyun static int azx_send_cmd(struct hdac_bus *bus, unsigned int val)
918*4882a593Smuzhiyun {
919*4882a593Smuzhiyun 	struct azx *chip = bus_to_azx(bus);
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	if (chip->disabled)
922*4882a593Smuzhiyun 		return 0;
923*4882a593Smuzhiyun 	if (chip->single_cmd)
924*4882a593Smuzhiyun 		return azx_single_send_cmd(bus, val);
925*4882a593Smuzhiyun 	else
926*4882a593Smuzhiyun 		return snd_hdac_bus_send_cmd(bus, val);
927*4882a593Smuzhiyun }
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun /* get a response */
azx_get_response(struct hdac_bus * bus,unsigned int addr,unsigned int * res)930*4882a593Smuzhiyun static int azx_get_response(struct hdac_bus *bus, unsigned int addr,
931*4882a593Smuzhiyun 			    unsigned int *res)
932*4882a593Smuzhiyun {
933*4882a593Smuzhiyun 	struct azx *chip = bus_to_azx(bus);
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	if (chip->disabled)
936*4882a593Smuzhiyun 		return 0;
937*4882a593Smuzhiyun 	if (chip->single_cmd)
938*4882a593Smuzhiyun 		return azx_single_get_response(bus, addr, res);
939*4882a593Smuzhiyun 	else
940*4882a593Smuzhiyun 		return azx_rirb_get_response(bus, addr, res);
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun static const struct hdac_bus_ops bus_core_ops = {
944*4882a593Smuzhiyun 	.command = azx_send_cmd,
945*4882a593Smuzhiyun 	.get_response = azx_get_response,
946*4882a593Smuzhiyun };
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun #ifdef CONFIG_SND_HDA_DSP_LOADER
949*4882a593Smuzhiyun /*
950*4882a593Smuzhiyun  * DSP loading code (e.g. for CA0132)
951*4882a593Smuzhiyun  */
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun /* use the first stream for loading DSP */
954*4882a593Smuzhiyun static struct azx_dev *
azx_get_dsp_loader_dev(struct azx * chip)955*4882a593Smuzhiyun azx_get_dsp_loader_dev(struct azx *chip)
956*4882a593Smuzhiyun {
957*4882a593Smuzhiyun 	struct hdac_bus *bus = azx_bus(chip);
958*4882a593Smuzhiyun 	struct hdac_stream *s;
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	list_for_each_entry(s, &bus->stream_list, list)
961*4882a593Smuzhiyun 		if (s->index == chip->playback_index_offset)
962*4882a593Smuzhiyun 			return stream_to_azx_dev(s);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	return NULL;
965*4882a593Smuzhiyun }
966*4882a593Smuzhiyun 
snd_hda_codec_load_dsp_prepare(struct hda_codec * codec,unsigned int format,unsigned int byte_size,struct snd_dma_buffer * bufp)967*4882a593Smuzhiyun int snd_hda_codec_load_dsp_prepare(struct hda_codec *codec, unsigned int format,
968*4882a593Smuzhiyun 				   unsigned int byte_size,
969*4882a593Smuzhiyun 				   struct snd_dma_buffer *bufp)
970*4882a593Smuzhiyun {
971*4882a593Smuzhiyun 	struct hdac_bus *bus = &codec->bus->core;
972*4882a593Smuzhiyun 	struct azx *chip = bus_to_azx(bus);
973*4882a593Smuzhiyun 	struct azx_dev *azx_dev;
974*4882a593Smuzhiyun 	struct hdac_stream *hstr;
975*4882a593Smuzhiyun 	bool saved = false;
976*4882a593Smuzhiyun 	int err;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	azx_dev = azx_get_dsp_loader_dev(chip);
979*4882a593Smuzhiyun 	hstr = azx_stream(azx_dev);
980*4882a593Smuzhiyun 	spin_lock_irq(&bus->reg_lock);
981*4882a593Smuzhiyun 	if (hstr->opened) {
982*4882a593Smuzhiyun 		chip->saved_azx_dev = *azx_dev;
983*4882a593Smuzhiyun 		saved = true;
984*4882a593Smuzhiyun 	}
985*4882a593Smuzhiyun 	spin_unlock_irq(&bus->reg_lock);
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun 	err = snd_hdac_dsp_prepare(hstr, format, byte_size, bufp);
988*4882a593Smuzhiyun 	if (err < 0) {
989*4882a593Smuzhiyun 		spin_lock_irq(&bus->reg_lock);
990*4882a593Smuzhiyun 		if (saved)
991*4882a593Smuzhiyun 			*azx_dev = chip->saved_azx_dev;
992*4882a593Smuzhiyun 		spin_unlock_irq(&bus->reg_lock);
993*4882a593Smuzhiyun 		return err;
994*4882a593Smuzhiyun 	}
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 	hstr->prepared = 0;
997*4882a593Smuzhiyun 	return err;
998*4882a593Smuzhiyun }
999*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hda_codec_load_dsp_prepare);
1000*4882a593Smuzhiyun 
snd_hda_codec_load_dsp_trigger(struct hda_codec * codec,bool start)1001*4882a593Smuzhiyun void snd_hda_codec_load_dsp_trigger(struct hda_codec *codec, bool start)
1002*4882a593Smuzhiyun {
1003*4882a593Smuzhiyun 	struct hdac_bus *bus = &codec->bus->core;
1004*4882a593Smuzhiyun 	struct azx *chip = bus_to_azx(bus);
1005*4882a593Smuzhiyun 	struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	snd_hdac_dsp_trigger(azx_stream(azx_dev), start);
1008*4882a593Smuzhiyun }
1009*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hda_codec_load_dsp_trigger);
1010*4882a593Smuzhiyun 
snd_hda_codec_load_dsp_cleanup(struct hda_codec * codec,struct snd_dma_buffer * dmab)1011*4882a593Smuzhiyun void snd_hda_codec_load_dsp_cleanup(struct hda_codec *codec,
1012*4882a593Smuzhiyun 				    struct snd_dma_buffer *dmab)
1013*4882a593Smuzhiyun {
1014*4882a593Smuzhiyun 	struct hdac_bus *bus = &codec->bus->core;
1015*4882a593Smuzhiyun 	struct azx *chip = bus_to_azx(bus);
1016*4882a593Smuzhiyun 	struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
1017*4882a593Smuzhiyun 	struct hdac_stream *hstr = azx_stream(azx_dev);
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	if (!dmab->area || !hstr->locked)
1020*4882a593Smuzhiyun 		return;
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 	snd_hdac_dsp_cleanup(hstr, dmab);
1023*4882a593Smuzhiyun 	spin_lock_irq(&bus->reg_lock);
1024*4882a593Smuzhiyun 	if (hstr->opened)
1025*4882a593Smuzhiyun 		*azx_dev = chip->saved_azx_dev;
1026*4882a593Smuzhiyun 	hstr->locked = false;
1027*4882a593Smuzhiyun 	spin_unlock_irq(&bus->reg_lock);
1028*4882a593Smuzhiyun }
1029*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hda_codec_load_dsp_cleanup);
1030*4882a593Smuzhiyun #endif /* CONFIG_SND_HDA_DSP_LOADER */
1031*4882a593Smuzhiyun 
1032*4882a593Smuzhiyun /*
1033*4882a593Smuzhiyun  * reset and start the controller registers
1034*4882a593Smuzhiyun  */
azx_init_chip(struct azx * chip,bool full_reset)1035*4882a593Smuzhiyun void azx_init_chip(struct azx *chip, bool full_reset)
1036*4882a593Smuzhiyun {
1037*4882a593Smuzhiyun 	if (snd_hdac_bus_init_chip(azx_bus(chip), full_reset)) {
1038*4882a593Smuzhiyun 		/* correct RINTCNT for CXT */
1039*4882a593Smuzhiyun 		if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND)
1040*4882a593Smuzhiyun 			azx_writew(chip, RINTCNT, 0xc0);
1041*4882a593Smuzhiyun 	}
1042*4882a593Smuzhiyun }
1043*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(azx_init_chip);
1044*4882a593Smuzhiyun 
azx_stop_all_streams(struct azx * chip)1045*4882a593Smuzhiyun void azx_stop_all_streams(struct azx *chip)
1046*4882a593Smuzhiyun {
1047*4882a593Smuzhiyun 	struct hdac_bus *bus = azx_bus(chip);
1048*4882a593Smuzhiyun 	struct hdac_stream *s;
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	list_for_each_entry(s, &bus->stream_list, list)
1051*4882a593Smuzhiyun 		snd_hdac_stream_stop(s);
1052*4882a593Smuzhiyun }
1053*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(azx_stop_all_streams);
1054*4882a593Smuzhiyun 
azx_stop_chip(struct azx * chip)1055*4882a593Smuzhiyun void azx_stop_chip(struct azx *chip)
1056*4882a593Smuzhiyun {
1057*4882a593Smuzhiyun 	snd_hdac_bus_stop_chip(azx_bus(chip));
1058*4882a593Smuzhiyun }
1059*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(azx_stop_chip);
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun /*
1062*4882a593Smuzhiyun  * interrupt handler
1063*4882a593Smuzhiyun  */
stream_update(struct hdac_bus * bus,struct hdac_stream * s)1064*4882a593Smuzhiyun static void stream_update(struct hdac_bus *bus, struct hdac_stream *s)
1065*4882a593Smuzhiyun {
1066*4882a593Smuzhiyun 	struct azx *chip = bus_to_azx(bus);
1067*4882a593Smuzhiyun 	struct azx_dev *azx_dev = stream_to_azx_dev(s);
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 	/* check whether this IRQ is really acceptable */
1070*4882a593Smuzhiyun 	if (!chip->ops->position_check ||
1071*4882a593Smuzhiyun 	    chip->ops->position_check(chip, azx_dev)) {
1072*4882a593Smuzhiyun 		spin_unlock(&bus->reg_lock);
1073*4882a593Smuzhiyun 		snd_pcm_period_elapsed(azx_stream(azx_dev)->substream);
1074*4882a593Smuzhiyun 		spin_lock(&bus->reg_lock);
1075*4882a593Smuzhiyun 	}
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun 
azx_interrupt(int irq,void * dev_id)1078*4882a593Smuzhiyun irqreturn_t azx_interrupt(int irq, void *dev_id)
1079*4882a593Smuzhiyun {
1080*4882a593Smuzhiyun 	struct azx *chip = dev_id;
1081*4882a593Smuzhiyun 	struct hdac_bus *bus = azx_bus(chip);
1082*4882a593Smuzhiyun 	u32 status;
1083*4882a593Smuzhiyun 	bool active, handled = false;
1084*4882a593Smuzhiyun 	int repeat = 0; /* count for avoiding endless loop */
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun #ifdef CONFIG_PM
1087*4882a593Smuzhiyun 	if (azx_has_pm_runtime(chip))
1088*4882a593Smuzhiyun 		if (!pm_runtime_active(chip->card->dev))
1089*4882a593Smuzhiyun 			return IRQ_NONE;
1090*4882a593Smuzhiyun #endif
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	spin_lock(&bus->reg_lock);
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 	if (chip->disabled)
1095*4882a593Smuzhiyun 		goto unlock;
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 	do {
1098*4882a593Smuzhiyun 		status = azx_readl(chip, INTSTS);
1099*4882a593Smuzhiyun 		if (status == 0 || status == 0xffffffff)
1100*4882a593Smuzhiyun 			break;
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun 		handled = true;
1103*4882a593Smuzhiyun 		active = false;
1104*4882a593Smuzhiyun 		if (snd_hdac_bus_handle_stream_irq(bus, status, stream_update))
1105*4882a593Smuzhiyun 			active = true;
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 		status = azx_readb(chip, RIRBSTS);
1108*4882a593Smuzhiyun 		if (status & RIRB_INT_MASK) {
1109*4882a593Smuzhiyun 			/*
1110*4882a593Smuzhiyun 			 * Clearing the interrupt status here ensures that no
1111*4882a593Smuzhiyun 			 * interrupt gets masked after the RIRB wp is read in
1112*4882a593Smuzhiyun 			 * snd_hdac_bus_update_rirb. This avoids a possible
1113*4882a593Smuzhiyun 			 * race condition where codec response in RIRB may
1114*4882a593Smuzhiyun 			 * remain unserviced by IRQ, eventually falling back
1115*4882a593Smuzhiyun 			 * to polling mode in azx_rirb_get_response.
1116*4882a593Smuzhiyun 			 */
1117*4882a593Smuzhiyun 			azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
1118*4882a593Smuzhiyun 			active = true;
1119*4882a593Smuzhiyun 			if (status & RIRB_INT_RESPONSE) {
1120*4882a593Smuzhiyun 				if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND)
1121*4882a593Smuzhiyun 					udelay(80);
1122*4882a593Smuzhiyun 				snd_hdac_bus_update_rirb(bus);
1123*4882a593Smuzhiyun 			}
1124*4882a593Smuzhiyun 		}
1125*4882a593Smuzhiyun 	} while (active && ++repeat < 10);
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun  unlock:
1128*4882a593Smuzhiyun 	spin_unlock(&bus->reg_lock);
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 	return IRQ_RETVAL(handled);
1131*4882a593Smuzhiyun }
1132*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(azx_interrupt);
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun /*
1135*4882a593Smuzhiyun  * Codec initerface
1136*4882a593Smuzhiyun  */
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun /*
1139*4882a593Smuzhiyun  * Probe the given codec address
1140*4882a593Smuzhiyun  */
probe_codec(struct azx * chip,int addr)1141*4882a593Smuzhiyun static int probe_codec(struct azx *chip, int addr)
1142*4882a593Smuzhiyun {
1143*4882a593Smuzhiyun 	unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
1144*4882a593Smuzhiyun 		(AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
1145*4882a593Smuzhiyun 	struct hdac_bus *bus = azx_bus(chip);
1146*4882a593Smuzhiyun 	int err;
1147*4882a593Smuzhiyun 	unsigned int res = -1;
1148*4882a593Smuzhiyun 
1149*4882a593Smuzhiyun 	mutex_lock(&bus->cmd_mutex);
1150*4882a593Smuzhiyun 	chip->probing = 1;
1151*4882a593Smuzhiyun 	azx_send_cmd(bus, cmd);
1152*4882a593Smuzhiyun 	err = azx_get_response(bus, addr, &res);
1153*4882a593Smuzhiyun 	chip->probing = 0;
1154*4882a593Smuzhiyun 	mutex_unlock(&bus->cmd_mutex);
1155*4882a593Smuzhiyun 	if (err < 0 || res == -1)
1156*4882a593Smuzhiyun 		return -EIO;
1157*4882a593Smuzhiyun 	dev_dbg(chip->card->dev, "codec #%d probed OK\n", addr);
1158*4882a593Smuzhiyun 	return 0;
1159*4882a593Smuzhiyun }
1160*4882a593Smuzhiyun 
snd_hda_bus_reset(struct hda_bus * bus)1161*4882a593Smuzhiyun void snd_hda_bus_reset(struct hda_bus *bus)
1162*4882a593Smuzhiyun {
1163*4882a593Smuzhiyun 	struct azx *chip = bus_to_azx(&bus->core);
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	bus->in_reset = 1;
1166*4882a593Smuzhiyun 	azx_stop_chip(chip);
1167*4882a593Smuzhiyun 	azx_init_chip(chip, true);
1168*4882a593Smuzhiyun 	if (bus->core.chip_init)
1169*4882a593Smuzhiyun 		snd_hda_bus_reset_codecs(bus);
1170*4882a593Smuzhiyun 	bus->in_reset = 0;
1171*4882a593Smuzhiyun }
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun /* HD-audio bus initialization */
azx_bus_init(struct azx * chip,const char * model)1174*4882a593Smuzhiyun int azx_bus_init(struct azx *chip, const char *model)
1175*4882a593Smuzhiyun {
1176*4882a593Smuzhiyun 	struct hda_bus *bus = &chip->bus;
1177*4882a593Smuzhiyun 	int err;
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun 	err = snd_hdac_bus_init(&bus->core, chip->card->dev, &bus_core_ops);
1180*4882a593Smuzhiyun 	if (err < 0)
1181*4882a593Smuzhiyun 		return err;
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	bus->card = chip->card;
1184*4882a593Smuzhiyun 	mutex_init(&bus->prepare_mutex);
1185*4882a593Smuzhiyun 	bus->pci = chip->pci;
1186*4882a593Smuzhiyun 	bus->modelname = model;
1187*4882a593Smuzhiyun 	bus->mixer_assigned = -1;
1188*4882a593Smuzhiyun 	bus->core.snoop = azx_snoop(chip);
1189*4882a593Smuzhiyun 	if (chip->get_position[0] != azx_get_pos_lpib ||
1190*4882a593Smuzhiyun 	    chip->get_position[1] != azx_get_pos_lpib)
1191*4882a593Smuzhiyun 		bus->core.use_posbuf = true;
1192*4882a593Smuzhiyun 	bus->core.bdl_pos_adj = chip->bdl_pos_adj;
1193*4882a593Smuzhiyun 	if (chip->driver_caps & AZX_DCAPS_CORBRP_SELF_CLEAR)
1194*4882a593Smuzhiyun 		bus->core.corbrp_self_clear = true;
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun 	if (chip->driver_caps & AZX_DCAPS_4K_BDLE_BOUNDARY)
1197*4882a593Smuzhiyun 		bus->core.align_bdle_4k = true;
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 	/* enable sync_write flag for stable communication as default */
1200*4882a593Smuzhiyun 	bus->core.sync_write = 1;
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 	return 0;
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(azx_bus_init);
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun /* Probe codecs */
azx_probe_codecs(struct azx * chip,unsigned int max_slots)1207*4882a593Smuzhiyun int azx_probe_codecs(struct azx *chip, unsigned int max_slots)
1208*4882a593Smuzhiyun {
1209*4882a593Smuzhiyun 	struct hdac_bus *bus = azx_bus(chip);
1210*4882a593Smuzhiyun 	int c, codecs, err;
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	codecs = 0;
1213*4882a593Smuzhiyun 	if (!max_slots)
1214*4882a593Smuzhiyun 		max_slots = AZX_DEFAULT_CODECS;
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	/* First try to probe all given codec slots */
1217*4882a593Smuzhiyun 	for (c = 0; c < max_slots; c++) {
1218*4882a593Smuzhiyun 		if ((bus->codec_mask & (1 << c)) & chip->codec_probe_mask) {
1219*4882a593Smuzhiyun 			if (probe_codec(chip, c) < 0) {
1220*4882a593Smuzhiyun 				/* Some BIOSen give you wrong codec addresses
1221*4882a593Smuzhiyun 				 * that don't exist
1222*4882a593Smuzhiyun 				 */
1223*4882a593Smuzhiyun 				dev_warn(chip->card->dev,
1224*4882a593Smuzhiyun 					 "Codec #%d probe error; disabling it...\n", c);
1225*4882a593Smuzhiyun 				bus->codec_mask &= ~(1 << c);
1226*4882a593Smuzhiyun 				/* More badly, accessing to a non-existing
1227*4882a593Smuzhiyun 				 * codec often screws up the controller chip,
1228*4882a593Smuzhiyun 				 * and disturbs the further communications.
1229*4882a593Smuzhiyun 				 * Thus if an error occurs during probing,
1230*4882a593Smuzhiyun 				 * better to reset the controller chip to
1231*4882a593Smuzhiyun 				 * get back to the sanity state.
1232*4882a593Smuzhiyun 				 */
1233*4882a593Smuzhiyun 				azx_stop_chip(chip);
1234*4882a593Smuzhiyun 				azx_init_chip(chip, true);
1235*4882a593Smuzhiyun 			}
1236*4882a593Smuzhiyun 		}
1237*4882a593Smuzhiyun 	}
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 	/* Then create codec instances */
1240*4882a593Smuzhiyun 	for (c = 0; c < max_slots; c++) {
1241*4882a593Smuzhiyun 		if ((bus->codec_mask & (1 << c)) & chip->codec_probe_mask) {
1242*4882a593Smuzhiyun 			struct hda_codec *codec;
1243*4882a593Smuzhiyun 			err = snd_hda_codec_new(&chip->bus, chip->card, c, &codec);
1244*4882a593Smuzhiyun 			if (err < 0)
1245*4882a593Smuzhiyun 				continue;
1246*4882a593Smuzhiyun 			codec->jackpoll_interval = chip->jackpoll_interval;
1247*4882a593Smuzhiyun 			codec->beep_mode = chip->beep_mode;
1248*4882a593Smuzhiyun 			codecs++;
1249*4882a593Smuzhiyun 		}
1250*4882a593Smuzhiyun 	}
1251*4882a593Smuzhiyun 	if (!codecs) {
1252*4882a593Smuzhiyun 		dev_err(chip->card->dev, "no codecs initialized\n");
1253*4882a593Smuzhiyun 		return -ENXIO;
1254*4882a593Smuzhiyun 	}
1255*4882a593Smuzhiyun 	return 0;
1256*4882a593Smuzhiyun }
1257*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(azx_probe_codecs);
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun /* configure each codec instance */
azx_codec_configure(struct azx * chip)1260*4882a593Smuzhiyun int azx_codec_configure(struct azx *chip)
1261*4882a593Smuzhiyun {
1262*4882a593Smuzhiyun 	struct hda_codec *codec, *next;
1263*4882a593Smuzhiyun 	int success = 0;
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 	list_for_each_codec(codec, &chip->bus) {
1266*4882a593Smuzhiyun 		if (!snd_hda_codec_configure(codec))
1267*4882a593Smuzhiyun 			success++;
1268*4882a593Smuzhiyun 	}
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	if (success) {
1271*4882a593Smuzhiyun 		/* unregister failed codecs if any codec has been probed */
1272*4882a593Smuzhiyun 		list_for_each_codec_safe(codec, next, &chip->bus) {
1273*4882a593Smuzhiyun 			if (!codec->configured) {
1274*4882a593Smuzhiyun 				codec_err(codec, "Unable to configure, disabling\n");
1275*4882a593Smuzhiyun 				snd_hdac_device_unregister(&codec->core);
1276*4882a593Smuzhiyun 			}
1277*4882a593Smuzhiyun 		}
1278*4882a593Smuzhiyun 	}
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	return success ? 0 : -ENODEV;
1281*4882a593Smuzhiyun }
1282*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(azx_codec_configure);
1283*4882a593Smuzhiyun 
stream_direction(struct azx * chip,unsigned char index)1284*4882a593Smuzhiyun static int stream_direction(struct azx *chip, unsigned char index)
1285*4882a593Smuzhiyun {
1286*4882a593Smuzhiyun 	if (index >= chip->capture_index_offset &&
1287*4882a593Smuzhiyun 	    index < chip->capture_index_offset + chip->capture_streams)
1288*4882a593Smuzhiyun 		return SNDRV_PCM_STREAM_CAPTURE;
1289*4882a593Smuzhiyun 	return SNDRV_PCM_STREAM_PLAYBACK;
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun /* initialize SD streams */
azx_init_streams(struct azx * chip)1293*4882a593Smuzhiyun int azx_init_streams(struct azx *chip)
1294*4882a593Smuzhiyun {
1295*4882a593Smuzhiyun 	int i;
1296*4882a593Smuzhiyun 	int stream_tags[2] = { 0, 0 };
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	/* initialize each stream (aka device)
1299*4882a593Smuzhiyun 	 * assign the starting bdl address to each stream (device)
1300*4882a593Smuzhiyun 	 * and initialize
1301*4882a593Smuzhiyun 	 */
1302*4882a593Smuzhiyun 	for (i = 0; i < chip->num_streams; i++) {
1303*4882a593Smuzhiyun 		struct azx_dev *azx_dev = kzalloc(sizeof(*azx_dev), GFP_KERNEL);
1304*4882a593Smuzhiyun 		int dir, tag;
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 		if (!azx_dev)
1307*4882a593Smuzhiyun 			return -ENOMEM;
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun 		dir = stream_direction(chip, i);
1310*4882a593Smuzhiyun 		/* stream tag must be unique throughout
1311*4882a593Smuzhiyun 		 * the stream direction group,
1312*4882a593Smuzhiyun 		 * valid values 1...15
1313*4882a593Smuzhiyun 		 * use separate stream tag if the flag
1314*4882a593Smuzhiyun 		 * AZX_DCAPS_SEPARATE_STREAM_TAG is used
1315*4882a593Smuzhiyun 		 */
1316*4882a593Smuzhiyun 		if (chip->driver_caps & AZX_DCAPS_SEPARATE_STREAM_TAG)
1317*4882a593Smuzhiyun 			tag = ++stream_tags[dir];
1318*4882a593Smuzhiyun 		else
1319*4882a593Smuzhiyun 			tag = i + 1;
1320*4882a593Smuzhiyun 		snd_hdac_stream_init(azx_bus(chip), azx_stream(azx_dev),
1321*4882a593Smuzhiyun 				     i, dir, tag);
1322*4882a593Smuzhiyun 	}
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun 	return 0;
1325*4882a593Smuzhiyun }
1326*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(azx_init_streams);
1327*4882a593Smuzhiyun 
azx_free_streams(struct azx * chip)1328*4882a593Smuzhiyun void azx_free_streams(struct azx *chip)
1329*4882a593Smuzhiyun {
1330*4882a593Smuzhiyun 	struct hdac_bus *bus = azx_bus(chip);
1331*4882a593Smuzhiyun 	struct hdac_stream *s;
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	while (!list_empty(&bus->stream_list)) {
1334*4882a593Smuzhiyun 		s = list_first_entry(&bus->stream_list, struct hdac_stream, list);
1335*4882a593Smuzhiyun 		list_del(&s->list);
1336*4882a593Smuzhiyun 		kfree(stream_to_azx_dev(s));
1337*4882a593Smuzhiyun 	}
1338*4882a593Smuzhiyun }
1339*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(azx_free_streams);
1340