2 * hdac-ext-stream.c - HD-audio extended stream operations.
4 * Copyright (C) 2015 Intel Corp
5 * Author: Jeeja KP <jeeja.kp@intel.com>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <sound/pcm.h>
23 #include <sound/hda_register.h>
24 #include <sound/hdaudio_ext.h>
27 * snd_hdac_ext_stream_init - initialize each stream (aka device)
28 * @ebus: HD-audio ext core bus
29 * @stream: HD-audio ext core stream object to initialize
30 * @idx: stream index number
31 * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE)
32 * @tag: the tag id to assign
34 * initialize the stream, if ppcap is enabled then init those and then
35 * invoke hdac stream initialization routine
37 void snd_hdac_ext_stream_init(struct hdac_ext_bus *ebus,
38 struct hdac_ext_stream *stream,
39 int idx, int direction, int tag)
41 struct hdac_bus *bus = &ebus->bus;
44 stream->pphc_addr = ebus->ppcap + AZX_PPHC_BASE +
45 AZX_PPHC_INTERVAL * idx;
47 stream->pplc_addr = ebus->ppcap + AZX_PPLC_BASE +
48 AZX_PPLC_MULTI * ebus->num_streams +
49 AZX_PPLC_INTERVAL * idx;
53 stream->spib_addr = ebus->spbcap + AZX_SPB_BASE +
54 AZX_SPB_INTERVAL * idx +
57 stream->fifo_addr = ebus->spbcap + AZX_SPB_BASE +
58 AZX_SPB_INTERVAL * idx +
62 stream->decoupled = false;
63 snd_hdac_stream_init(bus, &stream->hstream, idx, direction, tag);
65 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init);
68 * snd_hdac_ext_stream_init_all - create and initialize the stream objects
69 * for an extended hda bus
70 * @ebus: HD-audio ext core bus
71 * @start_idx: start index for streams
72 * @num_stream: number of streams to initialize
73 * @dir: direction of streams
75 int snd_hdac_ext_stream_init_all(struct hdac_ext_bus *ebus, int start_idx,
76 int num_stream, int dir)
79 int i, tag, idx = start_idx;
81 for (i = 0; i < num_stream; i++) {
82 struct hdac_ext_stream *stream =
83 kzalloc(sizeof(*stream), GFP_KERNEL);
87 snd_hdac_ext_stream_init(ebus, stream, idx, dir, tag);
94 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init_all);
97 * snd_hdac_stream_free_all - free hdac extended stream objects
99 * @ebus: HD-audio ext core bus
101 void snd_hdac_stream_free_all(struct hdac_ext_bus *ebus)
103 struct hdac_stream *s;
104 struct hdac_ext_stream *stream;
105 struct hdac_bus *bus = ebus_to_hbus(ebus);
107 while (!list_empty(&bus->stream_list)) {
108 s = list_first_entry(&bus->stream_list, struct hdac_stream, list);
109 stream = stream_to_hdac_ext_stream(s);
114 EXPORT_SYMBOL_GPL(snd_hdac_stream_free_all);
117 * snd_hdac_ext_stream_decouple - decouple the hdac stream
118 * @ebus: HD-audio ext core bus
119 * @stream: HD-audio ext core stream object to initialize
120 * @decouple: flag to decouple
122 void snd_hdac_ext_stream_decouple(struct hdac_ext_bus *ebus,
123 struct hdac_ext_stream *stream, bool decouple)
125 struct hdac_stream *hstream = &stream->hstream;
126 struct hdac_bus *bus = &ebus->bus;
128 spin_lock_irq(&bus->reg_lock);
130 snd_hdac_updatel(ebus->ppcap, AZX_REG_PP_PPCTL, 0,
131 AZX_PPCTL_PROCEN(hstream->index));
133 snd_hdac_updatel(ebus->ppcap, AZX_REG_PP_PPCTL,
134 AZX_PPCTL_PROCEN(hstream->index), 0);
135 stream->decoupled = decouple;
136 spin_unlock_irq(&bus->reg_lock);
138 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_decouple);
141 * snd_hdac_ext_linkstream_start - start a stream
142 * @stream: HD-audio ext core stream to start
144 void snd_hdac_ext_link_stream_start(struct hdac_ext_stream *stream)
146 snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, 0, AZX_PPLCCTL_RUN);
148 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_start);
151 * snd_hdac_ext_link_stream_clear - stop a stream DMA
152 * @stream: HD-audio ext core stream to stop
154 void snd_hdac_ext_link_stream_clear(struct hdac_ext_stream *stream)
156 snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, AZX_PPLCCTL_RUN, 0);
158 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_clear);
161 * snd_hdac_ext_link_stream_reset - reset a stream
162 * @stream: HD-audio ext core stream to reset
164 void snd_hdac_ext_link_stream_reset(struct hdac_ext_stream *stream)
169 snd_hdac_ext_link_stream_clear(stream);
171 snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, 0, AZX_PPLCCTL_STRST);
175 val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) &
181 val &= ~AZX_PPLCCTL_STRST;
182 writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
186 /* waiting for hardware to report that the stream is out of reset */
188 val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) & AZX_PPLCCTL_STRST;
195 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_reset);
198 * snd_hdac_ext_link_stream_setup - set up the SD for streaming
199 * @stream: HD-audio ext core stream to set up
200 * @fmt: stream format
202 int snd_hdac_ext_link_stream_setup(struct hdac_ext_stream *stream, int fmt)
204 struct hdac_stream *hstream = &stream->hstream;
207 /* make sure the run bit is zero for SD */
208 snd_hdac_ext_link_stream_clear(stream);
209 /* program the stream_tag */
210 val = readl(stream->pplc_addr + AZX_REG_PPLCCTL);
211 val = (val & ~AZX_PPLCCTL_STRM_MASK) |
212 (hstream->stream_tag << AZX_PPLCCTL_STRM_SHIFT);
213 writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
215 /* program the stream format */
216 writew(fmt, stream->pplc_addr + AZX_REG_PPLCFMT);
220 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_setup);
223 * snd_hdac_ext_link_set_stream_id - maps stream id to link output
224 * @link: HD-audio ext link to set up
227 void snd_hdac_ext_link_set_stream_id(struct hdac_ext_link *link,
230 snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 0);
232 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_set_stream_id);
235 * snd_hdac_ext_link_clear_stream_id - maps stream id to link output
236 * @link: HD-audio ext link to set up
239 void snd_hdac_ext_link_clear_stream_id(struct hdac_ext_link *link,
242 snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, 0, (1 << stream));
244 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_clear_stream_id);
246 static struct hdac_ext_stream *
247 hdac_ext_link_stream_assign(struct hdac_ext_bus *ebus,
248 struct snd_pcm_substream *substream)
250 struct hdac_ext_stream *res = NULL;
251 struct hdac_stream *stream = NULL;
252 struct hdac_bus *hbus = &ebus->bus;
255 dev_err(hbus->dev, "stream type not supported\n");
259 list_for_each_entry(stream, &hbus->stream_list, list) {
260 struct hdac_ext_stream *hstream = container_of(stream,
261 struct hdac_ext_stream,
263 if (stream->direction != substream->stream)
266 /* check if decoupled stream and not in use is available */
267 if (hstream->decoupled && !hstream->link_locked) {
272 if (!hstream->link_locked) {
273 snd_hdac_ext_stream_decouple(ebus, hstream, true);
279 spin_lock_irq(&hbus->reg_lock);
280 res->link_locked = 1;
281 res->link_substream = substream;
282 spin_unlock_irq(&hbus->reg_lock);
287 static struct hdac_ext_stream *
288 hdac_ext_host_stream_assign(struct hdac_ext_bus *ebus,
289 struct snd_pcm_substream *substream)
291 struct hdac_ext_stream *res = NULL;
292 struct hdac_stream *stream = NULL;
293 struct hdac_bus *hbus = &ebus->bus;
296 dev_err(hbus->dev, "stream type not supported\n");
300 list_for_each_entry(stream, &hbus->stream_list, list) {
301 struct hdac_ext_stream *hstream = container_of(stream,
302 struct hdac_ext_stream,
304 if (stream->direction != substream->stream)
307 if (!stream->opened) {
308 if (!hstream->decoupled)
309 snd_hdac_ext_stream_decouple(ebus, hstream, true);
315 spin_lock_irq(&hbus->reg_lock);
316 res->hstream.opened = 1;
317 res->hstream.running = 0;
318 res->hstream.substream = substream;
319 spin_unlock_irq(&hbus->reg_lock);
326 * snd_hdac_ext_stream_assign - assign a stream for the PCM
327 * @ebus: HD-audio ext core bus
328 * @substream: PCM substream to assign
329 * @type: type of stream (coupled, host or link stream)
331 * This assigns the stream based on the type (coupled/host/link), for the
332 * given PCM substream, assigns it and returns the stream object
334 * coupled: Looks for an unused stream
335 * host: Looks for an unused decoupled host stream
336 * link: Looks for an unused decoupled link stream
338 * If no stream is free, returns NULL. The function tries to keep using
339 * the same stream object when it's used beforehand. when a stream is
340 * decoupled, it becomes a host stream and link stream.
342 struct hdac_ext_stream *snd_hdac_ext_stream_assign(struct hdac_ext_bus *ebus,
343 struct snd_pcm_substream *substream,
346 struct hdac_ext_stream *hstream = NULL;
347 struct hdac_stream *stream = NULL;
348 struct hdac_bus *hbus = &ebus->bus;
351 case HDAC_EXT_STREAM_TYPE_COUPLED:
352 stream = snd_hdac_stream_assign(hbus, substream);
354 hstream = container_of(stream,
355 struct hdac_ext_stream, hstream);
358 case HDAC_EXT_STREAM_TYPE_HOST:
359 return hdac_ext_host_stream_assign(ebus, substream);
361 case HDAC_EXT_STREAM_TYPE_LINK:
362 return hdac_ext_link_stream_assign(ebus, substream);
368 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_assign);
371 * snd_hdac_ext_stream_release - release the assigned stream
372 * @stream: HD-audio ext core stream to release
373 * @type: type of stream (coupled, host or link stream)
375 * Release the stream that has been assigned by snd_hdac_ext_stream_assign().
377 void snd_hdac_ext_stream_release(struct hdac_ext_stream *stream, int type)
379 struct hdac_bus *bus = stream->hstream.bus;
380 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
383 case HDAC_EXT_STREAM_TYPE_COUPLED:
384 snd_hdac_stream_release(&stream->hstream);
387 case HDAC_EXT_STREAM_TYPE_HOST:
388 if (stream->decoupled) {
389 snd_hdac_ext_stream_decouple(ebus, stream, false);
390 snd_hdac_stream_release(&stream->hstream);
394 case HDAC_EXT_STREAM_TYPE_LINK:
395 if (stream->decoupled)
396 snd_hdac_ext_stream_decouple(ebus, stream, false);
397 spin_lock_irq(&bus->reg_lock);
398 stream->link_locked = 0;
399 stream->link_substream = NULL;
400 spin_unlock_irq(&bus->reg_lock);
404 dev_dbg(bus->dev, "Invalid type %d\n", type);
408 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_release);
411 * snd_hdac_ext_stream_spbcap_enable - enable SPIB for a stream
412 * @ebus: HD-audio ext core bus
413 * @enable: flag to enable/disable SPIB
414 * @index: stream index for which SPIB need to be enabled
416 void snd_hdac_ext_stream_spbcap_enable(struct hdac_ext_bus *ebus,
417 bool enable, int index)
420 u32 register_mask = 0;
421 struct hdac_bus *bus = &ebus->bus;
424 dev_err(bus->dev, "Address of SPB capability is NULL");
428 mask |= (1 << index);
430 register_mask = readl(ebus->spbcap + AZX_REG_SPB_SPBFCCTL);
432 mask |= register_mask;
435 snd_hdac_updatel(ebus->spbcap, AZX_REG_SPB_SPBFCCTL, 0, mask);
437 snd_hdac_updatel(ebus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, 0);
439 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_spbcap_enable);
442 * snd_hdac_ext_stream_set_spib - sets the spib value of a stream
443 * @ebus: HD-audio ext core bus
444 * @stream: hdac_ext_stream
445 * @value: spib value to set
447 int snd_hdac_ext_stream_set_spib(struct hdac_ext_bus *ebus,
448 struct hdac_ext_stream *stream, u32 value)
450 struct hdac_bus *bus = &ebus->bus;
453 dev_err(bus->dev, "Address of SPB capability is NULL");
457 writel(value, stream->spib_addr);
461 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_spib);
464 * snd_hdac_ext_stream_get_spbmaxfifo - gets the spib value of a stream
465 * @ebus: HD-audio ext core bus
466 * @stream: hdac_ext_stream
468 * Return maxfifo for the stream
470 int snd_hdac_ext_stream_get_spbmaxfifo(struct hdac_ext_bus *ebus,
471 struct hdac_ext_stream *stream)
473 struct hdac_bus *bus = &ebus->bus;
476 dev_err(bus->dev, "Address of SPB capability is NULL");
480 return readl(stream->fifo_addr);
482 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_get_spbmaxfifo);
486 * snd_hdac_ext_stop_streams - stop all stream if running
487 * @ebus: HD-audio ext core bus
489 void snd_hdac_ext_stop_streams(struct hdac_ext_bus *ebus)
491 struct hdac_bus *bus = ebus_to_hbus(ebus);
492 struct hdac_stream *stream;
494 if (bus->chip_init) {
495 list_for_each_entry(stream, &bus->stream_list, list)
496 snd_hdac_stream_stop(stream);
497 snd_hdac_bus_stop_chip(bus);
500 EXPORT_SYMBOL_GPL(snd_hdac_ext_stop_streams);