1 // SPDX-License-Identifier: GPL-2.0-only
3 * hdac-ext-stream.c - HD-audio extended stream operations.
5 * Copyright (C) 2015 Intel Corp
6 * Author: Jeeja KP <jeeja.kp@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <sound/pcm.h>
15 #include <sound/hda_register.h>
16 #include <sound/hdaudio_ext.h>
19 * snd_hdac_ext_stream_init - initialize each stream (aka device)
20 * @bus: HD-audio core bus
21 * @stream: HD-audio ext core stream object to initialize
22 * @idx: stream index number
23 * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE)
24 * @tag: the tag id to assign
26 * initialize the stream, if ppcap is enabled then init those and then
27 * invoke hdac stream initialization routine
29 void snd_hdac_ext_stream_init(struct hdac_bus *bus,
30 struct hdac_ext_stream *stream,
31 int idx, int direction, int tag)
34 stream->pphc_addr = bus->ppcap + AZX_PPHC_BASE +
35 AZX_PPHC_INTERVAL * idx;
37 stream->pplc_addr = bus->ppcap + AZX_PPLC_BASE +
38 AZX_PPLC_MULTI * bus->num_streams +
39 AZX_PPLC_INTERVAL * idx;
43 stream->spib_addr = bus->spbcap + AZX_SPB_BASE +
44 AZX_SPB_INTERVAL * idx +
47 stream->fifo_addr = bus->spbcap + AZX_SPB_BASE +
48 AZX_SPB_INTERVAL * idx +
53 stream->dpibr_addr = bus->drsmcap + AZX_DRSM_BASE +
54 AZX_DRSM_INTERVAL * idx;
56 stream->decoupled = false;
57 snd_hdac_stream_init(bus, &stream->hstream, idx, direction, tag);
59 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init);
62 * snd_hdac_ext_stream_init_all - create and initialize the stream objects
63 * for an extended hda bus
64 * @bus: HD-audio core bus
65 * @start_idx: start index for streams
66 * @num_stream: number of streams to initialize
67 * @dir: direction of streams
69 int snd_hdac_ext_stream_init_all(struct hdac_bus *bus, int start_idx,
70 int num_stream, int dir)
73 int i, tag, idx = start_idx;
75 for (i = 0; i < num_stream; i++) {
76 struct hdac_ext_stream *stream =
77 kzalloc(sizeof(*stream), GFP_KERNEL);
81 snd_hdac_ext_stream_init(bus, stream, idx, dir, tag);
88 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init_all);
91 * snd_hdac_stream_free_all - free hdac extended stream objects
93 * @bus: HD-audio core bus
95 void snd_hdac_stream_free_all(struct hdac_bus *bus)
97 struct hdac_stream *s, *_s;
98 struct hdac_ext_stream *stream;
100 list_for_each_entry_safe(s, _s, &bus->stream_list, list) {
101 stream = stream_to_hdac_ext_stream(s);
102 snd_hdac_ext_stream_decouple(bus, stream, false);
107 EXPORT_SYMBOL_GPL(snd_hdac_stream_free_all);
109 void snd_hdac_ext_stream_decouple_locked(struct hdac_bus *bus,
110 struct hdac_ext_stream *stream,
113 struct hdac_stream *hstream = &stream->hstream;
115 int mask = AZX_PPCTL_PROCEN(hstream->index);
117 val = readw(bus->ppcap + AZX_REG_PP_PPCTL) & mask;
119 if (decouple && !val)
120 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, mask, mask);
121 else if (!decouple && val)
122 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, mask, 0);
124 stream->decoupled = decouple;
126 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_decouple_locked);
129 * snd_hdac_ext_stream_decouple - decouple the hdac stream
130 * @bus: HD-audio core bus
131 * @stream: HD-audio ext core stream object to initialize
132 * @decouple: flag to decouple
134 void snd_hdac_ext_stream_decouple(struct hdac_bus *bus,
135 struct hdac_ext_stream *stream, bool decouple)
137 spin_lock_irq(&bus->reg_lock);
138 snd_hdac_ext_stream_decouple_locked(bus, stream, decouple);
139 spin_unlock_irq(&bus->reg_lock);
141 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_decouple);
144 * snd_hdac_ext_linkstream_start - start a stream
145 * @stream: HD-audio ext core stream to start
147 void snd_hdac_ext_link_stream_start(struct hdac_ext_stream *stream)
149 snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL,
150 AZX_PPLCCTL_RUN, AZX_PPLCCTL_RUN);
152 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_start);
155 * snd_hdac_ext_link_stream_clear - stop a stream DMA
156 * @stream: HD-audio ext core stream to stop
158 void snd_hdac_ext_link_stream_clear(struct hdac_ext_stream *stream)
160 snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, AZX_PPLCCTL_RUN, 0);
162 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_clear);
165 * snd_hdac_ext_link_stream_reset - reset a stream
166 * @stream: HD-audio ext core stream to reset
168 void snd_hdac_ext_link_stream_reset(struct hdac_ext_stream *stream)
173 snd_hdac_ext_link_stream_clear(stream);
175 snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL,
176 AZX_PPLCCTL_STRST, AZX_PPLCCTL_STRST);
180 val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) &
186 val &= ~AZX_PPLCCTL_STRST;
187 writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
191 /* waiting for hardware to report that the stream is out of reset */
193 val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) & AZX_PPLCCTL_STRST;
200 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_reset);
203 * snd_hdac_ext_link_stream_setup - set up the SD for streaming
204 * @stream: HD-audio ext core stream to set up
205 * @fmt: stream format
207 int snd_hdac_ext_link_stream_setup(struct hdac_ext_stream *stream, int fmt)
209 struct hdac_stream *hstream = &stream->hstream;
212 /* make sure the run bit is zero for SD */
213 snd_hdac_ext_link_stream_clear(stream);
214 /* program the stream_tag */
215 val = readl(stream->pplc_addr + AZX_REG_PPLCCTL);
216 val = (val & ~AZX_PPLCCTL_STRM_MASK) |
217 (hstream->stream_tag << AZX_PPLCCTL_STRM_SHIFT);
218 writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
220 /* program the stream format */
221 writew(fmt, stream->pplc_addr + AZX_REG_PPLCFMT);
225 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_setup);
228 * snd_hdac_ext_link_set_stream_id - maps stream id to link output
229 * @link: HD-audio ext link to set up
232 void snd_hdac_ext_link_set_stream_id(struct hdac_ext_link *link,
235 snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 1 << stream);
237 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_set_stream_id);
240 * snd_hdac_ext_link_clear_stream_id - maps stream id to link output
241 * @link: HD-audio ext link to set up
244 void snd_hdac_ext_link_clear_stream_id(struct hdac_ext_link *link,
247 snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 0);
249 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_clear_stream_id);
251 static struct hdac_ext_stream *
252 hdac_ext_link_stream_assign(struct hdac_bus *bus,
253 struct snd_pcm_substream *substream)
255 struct hdac_ext_stream *res = NULL;
256 struct hdac_stream *stream = NULL;
259 dev_err(bus->dev, "stream type not supported\n");
263 spin_lock_irq(&bus->reg_lock);
264 list_for_each_entry(stream, &bus->stream_list, list) {
265 struct hdac_ext_stream *hstream = container_of(stream,
266 struct hdac_ext_stream,
268 if (stream->direction != substream->stream)
271 /* check if decoupled stream and not in use is available */
272 if (hstream->decoupled && !hstream->link_locked) {
277 if (!hstream->link_locked) {
278 snd_hdac_ext_stream_decouple_locked(bus, hstream, true);
284 res->link_locked = 1;
285 res->link_substream = substream;
287 spin_unlock_irq(&bus->reg_lock);
291 static struct hdac_ext_stream *
292 hdac_ext_host_stream_assign(struct hdac_bus *bus,
293 struct snd_pcm_substream *substream)
295 struct hdac_ext_stream *res = NULL;
296 struct hdac_stream *stream = NULL;
299 dev_err(bus->dev, "stream type not supported\n");
303 spin_lock_irq(&bus->reg_lock);
304 list_for_each_entry(stream, &bus->stream_list, list) {
305 struct hdac_ext_stream *hstream = container_of(stream,
306 struct hdac_ext_stream,
308 if (stream->direction != substream->stream)
311 if (!stream->opened) {
312 if (!hstream->decoupled)
313 snd_hdac_ext_stream_decouple_locked(bus, hstream, true);
319 res->hstream.opened = 1;
320 res->hstream.running = 0;
321 res->hstream.substream = substream;
323 spin_unlock_irq(&bus->reg_lock);
329 * snd_hdac_ext_stream_assign - assign a stream for the PCM
330 * @bus: HD-audio core bus
331 * @substream: PCM substream to assign
332 * @type: type of stream (coupled, host or link stream)
334 * This assigns the stream based on the type (coupled/host/link), for the
335 * given PCM substream, assigns it and returns the stream object
337 * coupled: Looks for an unused stream
338 * host: Looks for an unused decoupled host stream
339 * link: Looks for an unused decoupled link stream
341 * If no stream is free, returns NULL. The function tries to keep using
342 * the same stream object when it's used beforehand. when a stream is
343 * decoupled, it becomes a host stream and link stream.
345 struct hdac_ext_stream *snd_hdac_ext_stream_assign(struct hdac_bus *bus,
346 struct snd_pcm_substream *substream,
349 struct hdac_ext_stream *hstream = NULL;
350 struct hdac_stream *stream = NULL;
353 case HDAC_EXT_STREAM_TYPE_COUPLED:
354 stream = snd_hdac_stream_assign(bus, substream);
356 hstream = container_of(stream,
357 struct hdac_ext_stream, hstream);
360 case HDAC_EXT_STREAM_TYPE_HOST:
361 return hdac_ext_host_stream_assign(bus, substream);
363 case HDAC_EXT_STREAM_TYPE_LINK:
364 return hdac_ext_link_stream_assign(bus, substream);
370 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_assign);
373 * snd_hdac_ext_stream_release - release the assigned stream
374 * @stream: HD-audio ext core stream to release
375 * @type: type of stream (coupled, host or link stream)
377 * Release the stream that has been assigned by snd_hdac_ext_stream_assign().
379 void snd_hdac_ext_stream_release(struct hdac_ext_stream *stream, int type)
381 struct hdac_bus *bus = stream->hstream.bus;
384 case HDAC_EXT_STREAM_TYPE_COUPLED:
385 snd_hdac_stream_release(&stream->hstream);
388 case HDAC_EXT_STREAM_TYPE_HOST:
389 spin_lock_irq(&bus->reg_lock);
390 if (stream->decoupled && !stream->link_locked)
391 snd_hdac_ext_stream_decouple_locked(bus, stream, false);
392 spin_unlock_irq(&bus->reg_lock);
393 snd_hdac_stream_release(&stream->hstream);
396 case HDAC_EXT_STREAM_TYPE_LINK:
397 spin_lock_irq(&bus->reg_lock);
398 if (stream->decoupled && !stream->hstream.opened)
399 snd_hdac_ext_stream_decouple_locked(bus, stream, false);
400 stream->link_locked = 0;
401 stream->link_substream = NULL;
402 spin_unlock_irq(&bus->reg_lock);
406 dev_dbg(bus->dev, "Invalid type %d\n", type);
410 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_release);
413 * snd_hdac_ext_stream_spbcap_enable - enable SPIB for a stream
414 * @bus: HD-audio core bus
415 * @enable: flag to enable/disable SPIB
416 * @index: stream index for which SPIB need to be enabled
418 void snd_hdac_ext_stream_spbcap_enable(struct hdac_bus *bus,
419 bool enable, int index)
424 dev_err(bus->dev, "Address of SPB capability is NULL\n");
428 mask |= (1 << index);
431 snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, mask);
433 snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, 0);
435 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_spbcap_enable);
438 * snd_hdac_ext_stream_set_spib - sets the spib value of a stream
439 * @bus: HD-audio core bus
440 * @stream: hdac_ext_stream
441 * @value: spib value to set
443 int snd_hdac_ext_stream_set_spib(struct hdac_bus *bus,
444 struct hdac_ext_stream *stream, u32 value)
448 dev_err(bus->dev, "Address of SPB capability is NULL\n");
452 writel(value, stream->spib_addr);
456 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_spib);
459 * snd_hdac_ext_stream_get_spbmaxfifo - gets the spib value of a stream
460 * @bus: HD-audio core bus
461 * @stream: hdac_ext_stream
463 * Return maxfifo for the stream
465 int snd_hdac_ext_stream_get_spbmaxfifo(struct hdac_bus *bus,
466 struct hdac_ext_stream *stream)
470 dev_err(bus->dev, "Address of SPB capability is NULL\n");
474 return readl(stream->fifo_addr);
476 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_get_spbmaxfifo);
479 * snd_hdac_ext_stream_drsm_enable - enable DMA resume for a stream
480 * @bus: HD-audio core bus
481 * @enable: flag to enable/disable DRSM
482 * @index: stream index for which DRSM need to be enabled
484 void snd_hdac_ext_stream_drsm_enable(struct hdac_bus *bus,
485 bool enable, int index)
490 dev_err(bus->dev, "Address of DRSM capability is NULL\n");
494 mask |= (1 << index);
497 snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, mask);
499 snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, 0);
501 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_drsm_enable);
504 * snd_hdac_ext_stream_set_dpibr - sets the dpibr value of a stream
505 * @bus: HD-audio core bus
506 * @stream: hdac_ext_stream
507 * @value: dpib value to set
509 int snd_hdac_ext_stream_set_dpibr(struct hdac_bus *bus,
510 struct hdac_ext_stream *stream, u32 value)
514 dev_err(bus->dev, "Address of DRSM capability is NULL\n");
518 writel(value, stream->dpibr_addr);
522 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_dpibr);
525 * snd_hdac_ext_stream_set_lpib - sets the lpib value of a stream
526 * @bus: HD-audio core bus
527 * @stream: hdac_ext_stream
528 * @value: lpib value to set
530 int snd_hdac_ext_stream_set_lpib(struct hdac_ext_stream *stream, u32 value)
532 snd_hdac_stream_writel(&stream->hstream, SD_LPIB, value);
536 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_lpib);