xref: /OK3568_Linux_fs/kernel/sound/hda/ext/hdac_ext_stream.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  hdac-ext-stream.c - HD-audio extended stream operations.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  Copyright (C) 2015 Intel Corp
6*4882a593Smuzhiyun  *  Author: Jeeja KP <jeeja.kp@intel.com>
7*4882a593Smuzhiyun  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/delay.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <sound/pcm.h>
15*4882a593Smuzhiyun #include <sound/hda_register.h>
16*4882a593Smuzhiyun #include <sound/hdaudio_ext.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /**
19*4882a593Smuzhiyun  * snd_hdac_ext_stream_init - initialize each stream (aka device)
20*4882a593Smuzhiyun  * @bus: HD-audio core bus
21*4882a593Smuzhiyun  * @stream: HD-audio ext core stream object to initialize
22*4882a593Smuzhiyun  * @idx: stream index number
23*4882a593Smuzhiyun  * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE)
24*4882a593Smuzhiyun  * @tag: the tag id to assign
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * initialize the stream, if ppcap is enabled then init those and then
27*4882a593Smuzhiyun  * invoke hdac stream initialization routine
28*4882a593Smuzhiyun  */
snd_hdac_ext_stream_init(struct hdac_bus * bus,struct hdac_ext_stream * stream,int idx,int direction,int tag)29*4882a593Smuzhiyun void snd_hdac_ext_stream_init(struct hdac_bus *bus,
30*4882a593Smuzhiyun 				struct hdac_ext_stream *stream,
31*4882a593Smuzhiyun 				int idx, int direction, int tag)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	if (bus->ppcap) {
34*4882a593Smuzhiyun 		stream->pphc_addr = bus->ppcap + AZX_PPHC_BASE +
35*4882a593Smuzhiyun 				AZX_PPHC_INTERVAL * idx;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 		stream->pplc_addr = bus->ppcap + AZX_PPLC_BASE +
38*4882a593Smuzhiyun 				AZX_PPLC_MULTI * bus->num_streams +
39*4882a593Smuzhiyun 				AZX_PPLC_INTERVAL * idx;
40*4882a593Smuzhiyun 	}
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	if (bus->spbcap) {
43*4882a593Smuzhiyun 		stream->spib_addr = bus->spbcap + AZX_SPB_BASE +
44*4882a593Smuzhiyun 					AZX_SPB_INTERVAL * idx +
45*4882a593Smuzhiyun 					AZX_SPB_SPIB;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 		stream->fifo_addr = bus->spbcap + AZX_SPB_BASE +
48*4882a593Smuzhiyun 					AZX_SPB_INTERVAL * idx +
49*4882a593Smuzhiyun 					AZX_SPB_MAXFIFO;
50*4882a593Smuzhiyun 	}
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	if (bus->drsmcap)
53*4882a593Smuzhiyun 		stream->dpibr_addr = bus->drsmcap + AZX_DRSM_BASE +
54*4882a593Smuzhiyun 					AZX_DRSM_INTERVAL * idx;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	stream->decoupled = false;
57*4882a593Smuzhiyun 	snd_hdac_stream_init(bus, &stream->hstream, idx, direction, tag);
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /**
62*4882a593Smuzhiyun  * snd_hdac_ext_stream_init_all - create and initialize the stream objects
63*4882a593Smuzhiyun  *   for an extended hda bus
64*4882a593Smuzhiyun  * @bus: HD-audio core bus
65*4882a593Smuzhiyun  * @start_idx: start index for streams
66*4882a593Smuzhiyun  * @num_stream: number of streams to initialize
67*4882a593Smuzhiyun  * @dir: direction of streams
68*4882a593Smuzhiyun  */
snd_hdac_ext_stream_init_all(struct hdac_bus * bus,int start_idx,int num_stream,int dir)69*4882a593Smuzhiyun int snd_hdac_ext_stream_init_all(struct hdac_bus *bus, int start_idx,
70*4882a593Smuzhiyun 		int num_stream, int dir)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	int stream_tag = 0;
73*4882a593Smuzhiyun 	int i, tag, idx = start_idx;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	for (i = 0; i < num_stream; i++) {
76*4882a593Smuzhiyun 		struct hdac_ext_stream *stream =
77*4882a593Smuzhiyun 				kzalloc(sizeof(*stream), GFP_KERNEL);
78*4882a593Smuzhiyun 		if (!stream)
79*4882a593Smuzhiyun 			return -ENOMEM;
80*4882a593Smuzhiyun 		tag = ++stream_tag;
81*4882a593Smuzhiyun 		snd_hdac_ext_stream_init(bus, stream, idx, dir, tag);
82*4882a593Smuzhiyun 		idx++;
83*4882a593Smuzhiyun 	}
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	return 0;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init_all);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /**
91*4882a593Smuzhiyun  * snd_hdac_stream_free_all - free hdac extended stream objects
92*4882a593Smuzhiyun  *
93*4882a593Smuzhiyun  * @bus: HD-audio core bus
94*4882a593Smuzhiyun  */
snd_hdac_stream_free_all(struct hdac_bus * bus)95*4882a593Smuzhiyun void snd_hdac_stream_free_all(struct hdac_bus *bus)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	struct hdac_stream *s, *_s;
98*4882a593Smuzhiyun 	struct hdac_ext_stream *stream;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	list_for_each_entry_safe(s, _s, &bus->stream_list, list) {
101*4882a593Smuzhiyun 		stream = stream_to_hdac_ext_stream(s);
102*4882a593Smuzhiyun 		snd_hdac_ext_stream_decouple(bus, stream, false);
103*4882a593Smuzhiyun 		list_del(&s->list);
104*4882a593Smuzhiyun 		kfree(stream);
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_stream_free_all);
108*4882a593Smuzhiyun 
snd_hdac_ext_stream_decouple_locked(struct hdac_bus * bus,struct hdac_ext_stream * stream,bool decouple)109*4882a593Smuzhiyun void snd_hdac_ext_stream_decouple_locked(struct hdac_bus *bus,
110*4882a593Smuzhiyun 					 struct hdac_ext_stream *stream,
111*4882a593Smuzhiyun 					 bool decouple)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun 	struct hdac_stream *hstream = &stream->hstream;
114*4882a593Smuzhiyun 	u32 val;
115*4882a593Smuzhiyun 	int mask = AZX_PPCTL_PROCEN(hstream->index);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	val = readw(bus->ppcap + AZX_REG_PP_PPCTL) & mask;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	if (decouple && !val)
120*4882a593Smuzhiyun 		snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, mask, mask);
121*4882a593Smuzhiyun 	else if (!decouple && val)
122*4882a593Smuzhiyun 		snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, mask, 0);
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	stream->decoupled = decouple;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_decouple_locked);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun /**
129*4882a593Smuzhiyun  * snd_hdac_ext_stream_decouple - decouple the hdac stream
130*4882a593Smuzhiyun  * @bus: HD-audio core bus
131*4882a593Smuzhiyun  * @stream: HD-audio ext core stream object to initialize
132*4882a593Smuzhiyun  * @decouple: flag to decouple
133*4882a593Smuzhiyun  */
snd_hdac_ext_stream_decouple(struct hdac_bus * bus,struct hdac_ext_stream * stream,bool decouple)134*4882a593Smuzhiyun void snd_hdac_ext_stream_decouple(struct hdac_bus *bus,
135*4882a593Smuzhiyun 				  struct hdac_ext_stream *stream, bool decouple)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	spin_lock_irq(&bus->reg_lock);
138*4882a593Smuzhiyun 	snd_hdac_ext_stream_decouple_locked(bus, stream, decouple);
139*4882a593Smuzhiyun 	spin_unlock_irq(&bus->reg_lock);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_decouple);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun /**
144*4882a593Smuzhiyun  * snd_hdac_ext_linkstream_start - start a stream
145*4882a593Smuzhiyun  * @stream: HD-audio ext core stream to start
146*4882a593Smuzhiyun  */
snd_hdac_ext_link_stream_start(struct hdac_ext_stream * stream)147*4882a593Smuzhiyun void snd_hdac_ext_link_stream_start(struct hdac_ext_stream *stream)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL,
150*4882a593Smuzhiyun 			 AZX_PPLCCTL_RUN, AZX_PPLCCTL_RUN);
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_start);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /**
155*4882a593Smuzhiyun  * snd_hdac_ext_link_stream_clear - stop a stream DMA
156*4882a593Smuzhiyun  * @stream: HD-audio ext core stream to stop
157*4882a593Smuzhiyun  */
snd_hdac_ext_link_stream_clear(struct hdac_ext_stream * stream)158*4882a593Smuzhiyun void snd_hdac_ext_link_stream_clear(struct hdac_ext_stream *stream)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, AZX_PPLCCTL_RUN, 0);
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_clear);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /**
165*4882a593Smuzhiyun  * snd_hdac_ext_link_stream_reset - reset a stream
166*4882a593Smuzhiyun  * @stream: HD-audio ext core stream to reset
167*4882a593Smuzhiyun  */
snd_hdac_ext_link_stream_reset(struct hdac_ext_stream * stream)168*4882a593Smuzhiyun void snd_hdac_ext_link_stream_reset(struct hdac_ext_stream *stream)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun 	unsigned char val;
171*4882a593Smuzhiyun 	int timeout;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	snd_hdac_ext_link_stream_clear(stream);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL,
176*4882a593Smuzhiyun 			 AZX_PPLCCTL_STRST, AZX_PPLCCTL_STRST);
177*4882a593Smuzhiyun 	udelay(3);
178*4882a593Smuzhiyun 	timeout = 50;
179*4882a593Smuzhiyun 	do {
180*4882a593Smuzhiyun 		val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) &
181*4882a593Smuzhiyun 				AZX_PPLCCTL_STRST;
182*4882a593Smuzhiyun 		if (val)
183*4882a593Smuzhiyun 			break;
184*4882a593Smuzhiyun 		udelay(3);
185*4882a593Smuzhiyun 	} while (--timeout);
186*4882a593Smuzhiyun 	val &= ~AZX_PPLCCTL_STRST;
187*4882a593Smuzhiyun 	writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
188*4882a593Smuzhiyun 	udelay(3);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	timeout = 50;
191*4882a593Smuzhiyun 	/* waiting for hardware to report that the stream is out of reset */
192*4882a593Smuzhiyun 	do {
193*4882a593Smuzhiyun 		val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) & AZX_PPLCCTL_STRST;
194*4882a593Smuzhiyun 		if (!val)
195*4882a593Smuzhiyun 			break;
196*4882a593Smuzhiyun 		udelay(3);
197*4882a593Smuzhiyun 	} while (--timeout);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_reset);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun /**
203*4882a593Smuzhiyun  * snd_hdac_ext_link_stream_setup -  set up the SD for streaming
204*4882a593Smuzhiyun  * @stream: HD-audio ext core stream to set up
205*4882a593Smuzhiyun  * @fmt: stream format
206*4882a593Smuzhiyun  */
snd_hdac_ext_link_stream_setup(struct hdac_ext_stream * stream,int fmt)207*4882a593Smuzhiyun int snd_hdac_ext_link_stream_setup(struct hdac_ext_stream *stream, int fmt)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun 	struct hdac_stream *hstream = &stream->hstream;
210*4882a593Smuzhiyun 	unsigned int val;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	/* make sure the run bit is zero for SD */
213*4882a593Smuzhiyun 	snd_hdac_ext_link_stream_clear(stream);
214*4882a593Smuzhiyun 	/* program the stream_tag */
215*4882a593Smuzhiyun 	val = readl(stream->pplc_addr + AZX_REG_PPLCCTL);
216*4882a593Smuzhiyun 	val = (val & ~AZX_PPLCCTL_STRM_MASK) |
217*4882a593Smuzhiyun 		(hstream->stream_tag << AZX_PPLCCTL_STRM_SHIFT);
218*4882a593Smuzhiyun 	writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	/* program the stream format */
221*4882a593Smuzhiyun 	writew(fmt, stream->pplc_addr + AZX_REG_PPLCFMT);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	return 0;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_setup);
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun /**
228*4882a593Smuzhiyun  * snd_hdac_ext_link_set_stream_id - maps stream id to link output
229*4882a593Smuzhiyun  * @link: HD-audio ext link to set up
230*4882a593Smuzhiyun  * @stream: stream id
231*4882a593Smuzhiyun  */
snd_hdac_ext_link_set_stream_id(struct hdac_ext_link * link,int stream)232*4882a593Smuzhiyun void snd_hdac_ext_link_set_stream_id(struct hdac_ext_link *link,
233*4882a593Smuzhiyun 				 int stream)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun 	snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 1 << stream);
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_link_set_stream_id);
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun /**
240*4882a593Smuzhiyun  * snd_hdac_ext_link_clear_stream_id - maps stream id to link output
241*4882a593Smuzhiyun  * @link: HD-audio ext link to set up
242*4882a593Smuzhiyun  * @stream: stream id
243*4882a593Smuzhiyun  */
snd_hdac_ext_link_clear_stream_id(struct hdac_ext_link * link,int stream)244*4882a593Smuzhiyun void snd_hdac_ext_link_clear_stream_id(struct hdac_ext_link *link,
245*4882a593Smuzhiyun 				 int stream)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun 	snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 0);
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_link_clear_stream_id);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun static struct hdac_ext_stream *
hdac_ext_link_stream_assign(struct hdac_bus * bus,struct snd_pcm_substream * substream)252*4882a593Smuzhiyun hdac_ext_link_stream_assign(struct hdac_bus *bus,
253*4882a593Smuzhiyun 				struct snd_pcm_substream *substream)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	struct hdac_ext_stream *res = NULL;
256*4882a593Smuzhiyun 	struct hdac_stream *stream = NULL;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	if (!bus->ppcap) {
259*4882a593Smuzhiyun 		dev_err(bus->dev, "stream type not supported\n");
260*4882a593Smuzhiyun 		return NULL;
261*4882a593Smuzhiyun 	}
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	spin_lock_irq(&bus->reg_lock);
264*4882a593Smuzhiyun 	list_for_each_entry(stream, &bus->stream_list, list) {
265*4882a593Smuzhiyun 		struct hdac_ext_stream *hstream = container_of(stream,
266*4882a593Smuzhiyun 						struct hdac_ext_stream,
267*4882a593Smuzhiyun 						hstream);
268*4882a593Smuzhiyun 		if (stream->direction != substream->stream)
269*4882a593Smuzhiyun 			continue;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 		/* check if decoupled stream and not in use is available */
272*4882a593Smuzhiyun 		if (hstream->decoupled && !hstream->link_locked) {
273*4882a593Smuzhiyun 			res = hstream;
274*4882a593Smuzhiyun 			break;
275*4882a593Smuzhiyun 		}
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 		if (!hstream->link_locked) {
278*4882a593Smuzhiyun 			snd_hdac_ext_stream_decouple_locked(bus, hstream, true);
279*4882a593Smuzhiyun 			res = hstream;
280*4882a593Smuzhiyun 			break;
281*4882a593Smuzhiyun 		}
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 	if (res) {
284*4882a593Smuzhiyun 		res->link_locked = 1;
285*4882a593Smuzhiyun 		res->link_substream = substream;
286*4882a593Smuzhiyun 	}
287*4882a593Smuzhiyun 	spin_unlock_irq(&bus->reg_lock);
288*4882a593Smuzhiyun 	return res;
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun static struct hdac_ext_stream *
hdac_ext_host_stream_assign(struct hdac_bus * bus,struct snd_pcm_substream * substream)292*4882a593Smuzhiyun hdac_ext_host_stream_assign(struct hdac_bus *bus,
293*4882a593Smuzhiyun 				struct snd_pcm_substream *substream)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun 	struct hdac_ext_stream *res = NULL;
296*4882a593Smuzhiyun 	struct hdac_stream *stream = NULL;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (!bus->ppcap) {
299*4882a593Smuzhiyun 		dev_err(bus->dev, "stream type not supported\n");
300*4882a593Smuzhiyun 		return NULL;
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	spin_lock_irq(&bus->reg_lock);
304*4882a593Smuzhiyun 	list_for_each_entry(stream, &bus->stream_list, list) {
305*4882a593Smuzhiyun 		struct hdac_ext_stream *hstream = container_of(stream,
306*4882a593Smuzhiyun 						struct hdac_ext_stream,
307*4882a593Smuzhiyun 						hstream);
308*4882a593Smuzhiyun 		if (stream->direction != substream->stream)
309*4882a593Smuzhiyun 			continue;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 		if (!stream->opened) {
312*4882a593Smuzhiyun 			if (!hstream->decoupled)
313*4882a593Smuzhiyun 				snd_hdac_ext_stream_decouple_locked(bus, hstream, true);
314*4882a593Smuzhiyun 			res = hstream;
315*4882a593Smuzhiyun 			break;
316*4882a593Smuzhiyun 		}
317*4882a593Smuzhiyun 	}
318*4882a593Smuzhiyun 	if (res) {
319*4882a593Smuzhiyun 		res->hstream.opened = 1;
320*4882a593Smuzhiyun 		res->hstream.running = 0;
321*4882a593Smuzhiyun 		res->hstream.substream = substream;
322*4882a593Smuzhiyun 	}
323*4882a593Smuzhiyun 	spin_unlock_irq(&bus->reg_lock);
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	return res;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun /**
329*4882a593Smuzhiyun  * snd_hdac_ext_stream_assign - assign a stream for the PCM
330*4882a593Smuzhiyun  * @bus: HD-audio core bus
331*4882a593Smuzhiyun  * @substream: PCM substream to assign
332*4882a593Smuzhiyun  * @type: type of stream (coupled, host or link stream)
333*4882a593Smuzhiyun  *
334*4882a593Smuzhiyun  * This assigns the stream based on the type (coupled/host/link), for the
335*4882a593Smuzhiyun  * given PCM substream, assigns it and returns the stream object
336*4882a593Smuzhiyun  *
337*4882a593Smuzhiyun  * coupled: Looks for an unused stream
338*4882a593Smuzhiyun  * host: Looks for an unused decoupled host stream
339*4882a593Smuzhiyun  * link: Looks for an unused decoupled link stream
340*4882a593Smuzhiyun  *
341*4882a593Smuzhiyun  * If no stream is free, returns NULL. The function tries to keep using
342*4882a593Smuzhiyun  * the same stream object when it's used beforehand.  when a stream is
343*4882a593Smuzhiyun  * decoupled, it becomes a host stream and link stream.
344*4882a593Smuzhiyun  */
snd_hdac_ext_stream_assign(struct hdac_bus * bus,struct snd_pcm_substream * substream,int type)345*4882a593Smuzhiyun struct hdac_ext_stream *snd_hdac_ext_stream_assign(struct hdac_bus *bus,
346*4882a593Smuzhiyun 					   struct snd_pcm_substream *substream,
347*4882a593Smuzhiyun 					   int type)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun 	struct hdac_ext_stream *hstream = NULL;
350*4882a593Smuzhiyun 	struct hdac_stream *stream = NULL;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	switch (type) {
353*4882a593Smuzhiyun 	case HDAC_EXT_STREAM_TYPE_COUPLED:
354*4882a593Smuzhiyun 		stream = snd_hdac_stream_assign(bus, substream);
355*4882a593Smuzhiyun 		if (stream)
356*4882a593Smuzhiyun 			hstream = container_of(stream,
357*4882a593Smuzhiyun 					struct hdac_ext_stream, hstream);
358*4882a593Smuzhiyun 		return hstream;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	case HDAC_EXT_STREAM_TYPE_HOST:
361*4882a593Smuzhiyun 		return hdac_ext_host_stream_assign(bus, substream);
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	case HDAC_EXT_STREAM_TYPE_LINK:
364*4882a593Smuzhiyun 		return hdac_ext_link_stream_assign(bus, substream);
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	default:
367*4882a593Smuzhiyun 		return NULL;
368*4882a593Smuzhiyun 	}
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_assign);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun /**
373*4882a593Smuzhiyun  * snd_hdac_ext_stream_release - release the assigned stream
374*4882a593Smuzhiyun  * @stream: HD-audio ext core stream to release
375*4882a593Smuzhiyun  * @type: type of stream (coupled, host or link stream)
376*4882a593Smuzhiyun  *
377*4882a593Smuzhiyun  * Release the stream that has been assigned by snd_hdac_ext_stream_assign().
378*4882a593Smuzhiyun  */
snd_hdac_ext_stream_release(struct hdac_ext_stream * stream,int type)379*4882a593Smuzhiyun void snd_hdac_ext_stream_release(struct hdac_ext_stream *stream, int type)
380*4882a593Smuzhiyun {
381*4882a593Smuzhiyun 	struct hdac_bus *bus = stream->hstream.bus;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	switch (type) {
384*4882a593Smuzhiyun 	case HDAC_EXT_STREAM_TYPE_COUPLED:
385*4882a593Smuzhiyun 		snd_hdac_stream_release(&stream->hstream);
386*4882a593Smuzhiyun 		break;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	case HDAC_EXT_STREAM_TYPE_HOST:
389*4882a593Smuzhiyun 		spin_lock_irq(&bus->reg_lock);
390*4882a593Smuzhiyun 		if (stream->decoupled && !stream->link_locked)
391*4882a593Smuzhiyun 			snd_hdac_ext_stream_decouple_locked(bus, stream, false);
392*4882a593Smuzhiyun 		spin_unlock_irq(&bus->reg_lock);
393*4882a593Smuzhiyun 		snd_hdac_stream_release(&stream->hstream);
394*4882a593Smuzhiyun 		break;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	case HDAC_EXT_STREAM_TYPE_LINK:
397*4882a593Smuzhiyun 		spin_lock_irq(&bus->reg_lock);
398*4882a593Smuzhiyun 		if (stream->decoupled && !stream->hstream.opened)
399*4882a593Smuzhiyun 			snd_hdac_ext_stream_decouple_locked(bus, stream, false);
400*4882a593Smuzhiyun 		stream->link_locked = 0;
401*4882a593Smuzhiyun 		stream->link_substream = NULL;
402*4882a593Smuzhiyun 		spin_unlock_irq(&bus->reg_lock);
403*4882a593Smuzhiyun 		break;
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	default:
406*4882a593Smuzhiyun 		dev_dbg(bus->dev, "Invalid type %d\n", type);
407*4882a593Smuzhiyun 	}
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_release);
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun /**
413*4882a593Smuzhiyun  * snd_hdac_ext_stream_spbcap_enable - enable SPIB for a stream
414*4882a593Smuzhiyun  * @bus: HD-audio core bus
415*4882a593Smuzhiyun  * @enable: flag to enable/disable SPIB
416*4882a593Smuzhiyun  * @index: stream index for which SPIB need to be enabled
417*4882a593Smuzhiyun  */
snd_hdac_ext_stream_spbcap_enable(struct hdac_bus * bus,bool enable,int index)418*4882a593Smuzhiyun void snd_hdac_ext_stream_spbcap_enable(struct hdac_bus *bus,
419*4882a593Smuzhiyun 				 bool enable, int index)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun 	u32 mask = 0;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	if (!bus->spbcap) {
424*4882a593Smuzhiyun 		dev_err(bus->dev, "Address of SPB capability is NULL\n");
425*4882a593Smuzhiyun 		return;
426*4882a593Smuzhiyun 	}
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	mask |= (1 << index);
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	if (enable)
431*4882a593Smuzhiyun 		snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, mask);
432*4882a593Smuzhiyun 	else
433*4882a593Smuzhiyun 		snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, 0);
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_spbcap_enable);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun /**
438*4882a593Smuzhiyun  * snd_hdac_ext_stream_set_spib - sets the spib value of a stream
439*4882a593Smuzhiyun  * @bus: HD-audio core bus
440*4882a593Smuzhiyun  * @stream: hdac_ext_stream
441*4882a593Smuzhiyun  * @value: spib value to set
442*4882a593Smuzhiyun  */
snd_hdac_ext_stream_set_spib(struct hdac_bus * bus,struct hdac_ext_stream * stream,u32 value)443*4882a593Smuzhiyun int snd_hdac_ext_stream_set_spib(struct hdac_bus *bus,
444*4882a593Smuzhiyun 				 struct hdac_ext_stream *stream, u32 value)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	if (!bus->spbcap) {
448*4882a593Smuzhiyun 		dev_err(bus->dev, "Address of SPB capability is NULL\n");
449*4882a593Smuzhiyun 		return -EINVAL;
450*4882a593Smuzhiyun 	}
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	writel(value, stream->spib_addr);
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	return 0;
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_spib);
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun /**
459*4882a593Smuzhiyun  * snd_hdac_ext_stream_get_spbmaxfifo - gets the spib value of a stream
460*4882a593Smuzhiyun  * @bus: HD-audio core bus
461*4882a593Smuzhiyun  * @stream: hdac_ext_stream
462*4882a593Smuzhiyun  *
463*4882a593Smuzhiyun  * Return maxfifo for the stream
464*4882a593Smuzhiyun  */
snd_hdac_ext_stream_get_spbmaxfifo(struct hdac_bus * bus,struct hdac_ext_stream * stream)465*4882a593Smuzhiyun int snd_hdac_ext_stream_get_spbmaxfifo(struct hdac_bus *bus,
466*4882a593Smuzhiyun 				 struct hdac_ext_stream *stream)
467*4882a593Smuzhiyun {
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	if (!bus->spbcap) {
470*4882a593Smuzhiyun 		dev_err(bus->dev, "Address of SPB capability is NULL\n");
471*4882a593Smuzhiyun 		return -EINVAL;
472*4882a593Smuzhiyun 	}
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	return readl(stream->fifo_addr);
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_get_spbmaxfifo);
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun /**
480*4882a593Smuzhiyun  * snd_hdac_ext_stop_streams - stop all stream if running
481*4882a593Smuzhiyun  * @bus: HD-audio core bus
482*4882a593Smuzhiyun  */
snd_hdac_ext_stop_streams(struct hdac_bus * bus)483*4882a593Smuzhiyun void snd_hdac_ext_stop_streams(struct hdac_bus *bus)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun 	struct hdac_stream *stream;
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	if (bus->chip_init) {
488*4882a593Smuzhiyun 		list_for_each_entry(stream, &bus->stream_list, list)
489*4882a593Smuzhiyun 			snd_hdac_stream_stop(stream);
490*4882a593Smuzhiyun 		snd_hdac_bus_stop_chip(bus);
491*4882a593Smuzhiyun 	}
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_stop_streams);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun /**
496*4882a593Smuzhiyun  * snd_hdac_ext_stream_drsm_enable - enable DMA resume for a stream
497*4882a593Smuzhiyun  * @bus: HD-audio core bus
498*4882a593Smuzhiyun  * @enable: flag to enable/disable DRSM
499*4882a593Smuzhiyun  * @index: stream index for which DRSM need to be enabled
500*4882a593Smuzhiyun  */
snd_hdac_ext_stream_drsm_enable(struct hdac_bus * bus,bool enable,int index)501*4882a593Smuzhiyun void snd_hdac_ext_stream_drsm_enable(struct hdac_bus *bus,
502*4882a593Smuzhiyun 				bool enable, int index)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	u32 mask = 0;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	if (!bus->drsmcap) {
507*4882a593Smuzhiyun 		dev_err(bus->dev, "Address of DRSM capability is NULL\n");
508*4882a593Smuzhiyun 		return;
509*4882a593Smuzhiyun 	}
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	mask |= (1 << index);
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	if (enable)
514*4882a593Smuzhiyun 		snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, mask);
515*4882a593Smuzhiyun 	else
516*4882a593Smuzhiyun 		snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, 0);
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_drsm_enable);
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun /**
521*4882a593Smuzhiyun  * snd_hdac_ext_stream_set_dpibr - sets the dpibr value of a stream
522*4882a593Smuzhiyun  * @bus: HD-audio core bus
523*4882a593Smuzhiyun  * @stream: hdac_ext_stream
524*4882a593Smuzhiyun  * @value: dpib value to set
525*4882a593Smuzhiyun  */
snd_hdac_ext_stream_set_dpibr(struct hdac_bus * bus,struct hdac_ext_stream * stream,u32 value)526*4882a593Smuzhiyun int snd_hdac_ext_stream_set_dpibr(struct hdac_bus *bus,
527*4882a593Smuzhiyun 				 struct hdac_ext_stream *stream, u32 value)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	if (!bus->drsmcap) {
531*4882a593Smuzhiyun 		dev_err(bus->dev, "Address of DRSM capability is NULL\n");
532*4882a593Smuzhiyun 		return -EINVAL;
533*4882a593Smuzhiyun 	}
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	writel(value, stream->dpibr_addr);
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	return 0;
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_dpibr);
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun /**
542*4882a593Smuzhiyun  * snd_hdac_ext_stream_set_lpib - sets the lpib value of a stream
543*4882a593Smuzhiyun  * @stream: hdac_ext_stream
544*4882a593Smuzhiyun  * @value: lpib value to set
545*4882a593Smuzhiyun  */
snd_hdac_ext_stream_set_lpib(struct hdac_ext_stream * stream,u32 value)546*4882a593Smuzhiyun int snd_hdac_ext_stream_set_lpib(struct hdac_ext_stream *stream, u32 value)
547*4882a593Smuzhiyun {
548*4882a593Smuzhiyun 	snd_hdac_stream_writel(&stream->hstream, SD_LPIB, value);
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	return 0;
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_lpib);
553