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 * @bus: HD-audio 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_bus *bus,
38 struct hdac_ext_stream *stream,
39 int idx, int direction, int tag)
42 stream->pphc_addr = bus->ppcap + AZX_PPHC_BASE +
43 AZX_PPHC_INTERVAL * idx;
45 stream->pplc_addr = bus->ppcap + AZX_PPLC_BASE +
46 AZX_PPLC_MULTI * bus->num_streams +
47 AZX_PPLC_INTERVAL * idx;
51 stream->spib_addr = bus->spbcap + AZX_SPB_BASE +
52 AZX_SPB_INTERVAL * idx +
55 stream->fifo_addr = bus->spbcap + AZX_SPB_BASE +
56 AZX_SPB_INTERVAL * idx +
61 stream->dpibr_addr = bus->drsmcap + AZX_DRSM_BASE +
62 AZX_DRSM_INTERVAL * idx;
64 stream->decoupled = false;
65 snd_hdac_stream_init(bus, &stream->hstream, idx, direction, tag);
67 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init);
70 * snd_hdac_ext_stream_init_all - create and initialize the stream objects
71 * for an extended hda bus
72 * @bus: HD-audio core bus
73 * @start_idx: start index for streams
74 * @num_stream: number of streams to initialize
75 * @dir: direction of streams
77 int snd_hdac_ext_stream_init_all(struct hdac_bus *bus, int start_idx,
78 int num_stream, int dir)
81 int i, tag, idx = start_idx;
83 for (i = 0; i < num_stream; i++) {
84 struct hdac_ext_stream *stream =
85 kzalloc(sizeof(*stream), GFP_KERNEL);
89 snd_hdac_ext_stream_init(bus, stream, idx, dir, tag);
96 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_init_all);
99 * snd_hdac_stream_free_all - free hdac extended stream objects
101 * @bus: HD-audio core bus
103 void snd_hdac_stream_free_all(struct hdac_bus *bus)
105 struct hdac_stream *s, *_s;
106 struct hdac_ext_stream *stream;
108 list_for_each_entry_safe(s, _s, &bus->stream_list, list) {
109 stream = stream_to_hdac_ext_stream(s);
110 snd_hdac_ext_stream_decouple(bus, stream, false);
115 EXPORT_SYMBOL_GPL(snd_hdac_stream_free_all);
118 * snd_hdac_ext_stream_decouple - decouple the hdac stream
119 * @bus: HD-audio core bus
120 * @stream: HD-audio ext core stream object to initialize
121 * @decouple: flag to decouple
123 void snd_hdac_ext_stream_decouple(struct hdac_bus *bus,
124 struct hdac_ext_stream *stream, bool decouple)
126 struct hdac_stream *hstream = &stream->hstream;
128 int mask = AZX_PPCTL_PROCEN(hstream->index);
130 spin_lock_irq(&bus->reg_lock);
131 val = readw(bus->ppcap + AZX_REG_PP_PPCTL) & mask;
133 if (decouple && !val)
134 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, mask, mask);
135 else if (!decouple && val)
136 snd_hdac_updatel(bus->ppcap, AZX_REG_PP_PPCTL, mask, 0);
138 stream->decoupled = 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, 0, AZX_PPLCCTL_RUN);
151 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_start);
154 * snd_hdac_ext_link_stream_clear - stop a stream DMA
155 * @stream: HD-audio ext core stream to stop
157 void snd_hdac_ext_link_stream_clear(struct hdac_ext_stream *stream)
159 snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, AZX_PPLCCTL_RUN, 0);
161 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_clear);
164 * snd_hdac_ext_link_stream_reset - reset a stream
165 * @stream: HD-audio ext core stream to reset
167 void snd_hdac_ext_link_stream_reset(struct hdac_ext_stream *stream)
172 snd_hdac_ext_link_stream_clear(stream);
174 snd_hdac_updatel(stream->pplc_addr, AZX_REG_PPLCCTL, 0, AZX_PPLCCTL_STRST);
178 val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) &
184 val &= ~AZX_PPLCCTL_STRST;
185 writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
189 /* waiting for hardware to report that the stream is out of reset */
191 val = readl(stream->pplc_addr + AZX_REG_PPLCCTL) & AZX_PPLCCTL_STRST;
198 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_reset);
201 * snd_hdac_ext_link_stream_setup - set up the SD for streaming
202 * @stream: HD-audio ext core stream to set up
203 * @fmt: stream format
205 int snd_hdac_ext_link_stream_setup(struct hdac_ext_stream *stream, int fmt)
207 struct hdac_stream *hstream = &stream->hstream;
210 /* make sure the run bit is zero for SD */
211 snd_hdac_ext_link_stream_clear(stream);
212 /* program the stream_tag */
213 val = readl(stream->pplc_addr + AZX_REG_PPLCCTL);
214 val = (val & ~AZX_PPLCCTL_STRM_MASK) |
215 (hstream->stream_tag << AZX_PPLCCTL_STRM_SHIFT);
216 writel(val, stream->pplc_addr + AZX_REG_PPLCCTL);
218 /* program the stream format */
219 writew(fmt, stream->pplc_addr + AZX_REG_PPLCFMT);
223 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_stream_setup);
226 * snd_hdac_ext_link_set_stream_id - maps stream id to link output
227 * @link: HD-audio ext link to set up
230 void snd_hdac_ext_link_set_stream_id(struct hdac_ext_link *link,
233 snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, (1 << stream), 1 << stream);
235 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_set_stream_id);
238 * snd_hdac_ext_link_clear_stream_id - maps stream id to link output
239 * @link: HD-audio ext link to set up
242 void snd_hdac_ext_link_clear_stream_id(struct hdac_ext_link *link,
245 snd_hdac_updatew(link->ml_addr, AZX_REG_ML_LOSIDV, 0, (1 << stream));
247 EXPORT_SYMBOL_GPL(snd_hdac_ext_link_clear_stream_id);
249 static struct hdac_ext_stream *
250 hdac_ext_link_stream_assign(struct hdac_bus *bus,
251 struct snd_pcm_substream *substream)
253 struct hdac_ext_stream *res = NULL;
254 struct hdac_stream *stream = NULL;
257 dev_err(bus->dev, "stream type not supported\n");
261 list_for_each_entry(stream, &bus->stream_list, list) {
262 struct hdac_ext_stream *hstream = container_of(stream,
263 struct hdac_ext_stream,
265 if (stream->direction != substream->stream)
268 /* check if decoupled stream and not in use is available */
269 if (hstream->decoupled && !hstream->link_locked) {
274 if (!hstream->link_locked) {
275 snd_hdac_ext_stream_decouple(bus, hstream, true);
281 spin_lock_irq(&bus->reg_lock);
282 res->link_locked = 1;
283 res->link_substream = substream;
284 spin_unlock_irq(&bus->reg_lock);
289 static struct hdac_ext_stream *
290 hdac_ext_host_stream_assign(struct hdac_bus *bus,
291 struct snd_pcm_substream *substream)
293 struct hdac_ext_stream *res = NULL;
294 struct hdac_stream *stream = NULL;
297 dev_err(bus->dev, "stream type not supported\n");
301 list_for_each_entry(stream, &bus->stream_list, list) {
302 struct hdac_ext_stream *hstream = container_of(stream,
303 struct hdac_ext_stream,
305 if (stream->direction != substream->stream)
308 if (!stream->opened) {
309 if (!hstream->decoupled)
310 snd_hdac_ext_stream_decouple(bus, hstream, true);
316 spin_lock_irq(&bus->reg_lock);
317 res->hstream.opened = 1;
318 res->hstream.running = 0;
319 res->hstream.substream = substream;
320 spin_unlock_irq(&bus->reg_lock);
327 * snd_hdac_ext_stream_assign - assign a stream for the PCM
328 * @bus: HD-audio core bus
329 * @substream: PCM substream to assign
330 * @type: type of stream (coupled, host or link stream)
332 * This assigns the stream based on the type (coupled/host/link), for the
333 * given PCM substream, assigns it and returns the stream object
335 * coupled: Looks for an unused stream
336 * host: Looks for an unused decoupled host stream
337 * link: Looks for an unused decoupled link stream
339 * If no stream is free, returns NULL. The function tries to keep using
340 * the same stream object when it's used beforehand. when a stream is
341 * decoupled, it becomes a host stream and link stream.
343 struct hdac_ext_stream *snd_hdac_ext_stream_assign(struct hdac_bus *bus,
344 struct snd_pcm_substream *substream,
347 struct hdac_ext_stream *hstream = NULL;
348 struct hdac_stream *stream = NULL;
351 case HDAC_EXT_STREAM_TYPE_COUPLED:
352 stream = snd_hdac_stream_assign(bus, 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(bus, substream);
361 case HDAC_EXT_STREAM_TYPE_LINK:
362 return hdac_ext_link_stream_assign(bus, 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;
382 case HDAC_EXT_STREAM_TYPE_COUPLED:
383 snd_hdac_stream_release(&stream->hstream);
386 case HDAC_EXT_STREAM_TYPE_HOST:
387 if (stream->decoupled && !stream->link_locked)
388 snd_hdac_ext_stream_decouple(bus, stream, false);
389 snd_hdac_stream_release(&stream->hstream);
392 case HDAC_EXT_STREAM_TYPE_LINK:
393 if (stream->decoupled && !stream->hstream.opened)
394 snd_hdac_ext_stream_decouple(bus, stream, false);
395 spin_lock_irq(&bus->reg_lock);
396 stream->link_locked = 0;
397 stream->link_substream = NULL;
398 spin_unlock_irq(&bus->reg_lock);
402 dev_dbg(bus->dev, "Invalid type %d\n", type);
406 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_release);
409 * snd_hdac_ext_stream_spbcap_enable - enable SPIB for a stream
410 * @bus: HD-audio core bus
411 * @enable: flag to enable/disable SPIB
412 * @index: stream index for which SPIB need to be enabled
414 void snd_hdac_ext_stream_spbcap_enable(struct hdac_bus *bus,
415 bool enable, int index)
418 u32 register_mask = 0;
421 dev_err(bus->dev, "Address of SPB capability is NULL\n");
425 mask |= (1 << index);
427 register_mask = readl(bus->spbcap + AZX_REG_SPB_SPBFCCTL);
429 mask |= register_mask;
432 snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, 0, mask);
434 snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, 0);
436 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_spbcap_enable);
439 * snd_hdac_ext_stream_set_spib - sets the spib value of a stream
440 * @bus: HD-audio core bus
441 * @stream: hdac_ext_stream
442 * @value: spib value to set
444 int snd_hdac_ext_stream_set_spib(struct hdac_bus *bus,
445 struct hdac_ext_stream *stream, u32 value)
449 dev_err(bus->dev, "Address of SPB capability is NULL\n");
453 writel(value, stream->spib_addr);
457 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_spib);
460 * snd_hdac_ext_stream_get_spbmaxfifo - gets the spib value of a stream
461 * @bus: HD-audio core bus
462 * @stream: hdac_ext_stream
464 * Return maxfifo for the stream
466 int snd_hdac_ext_stream_get_spbmaxfifo(struct hdac_bus *bus,
467 struct hdac_ext_stream *stream)
471 dev_err(bus->dev, "Address of SPB capability is NULL\n");
475 return readl(stream->fifo_addr);
477 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_get_spbmaxfifo);
481 * snd_hdac_ext_stop_streams - stop all stream if running
482 * @bus: HD-audio core bus
484 void snd_hdac_ext_stop_streams(struct hdac_bus *bus)
486 struct hdac_stream *stream;
488 if (bus->chip_init) {
489 list_for_each_entry(stream, &bus->stream_list, list)
490 snd_hdac_stream_stop(stream);
491 snd_hdac_bus_stop_chip(bus);
494 EXPORT_SYMBOL_GPL(snd_hdac_ext_stop_streams);
497 * snd_hdac_ext_stream_drsm_enable - enable DMA resume for a stream
498 * @bus: HD-audio core bus
499 * @enable: flag to enable/disable DRSM
500 * @index: stream index for which DRSM need to be enabled
502 void snd_hdac_ext_stream_drsm_enable(struct hdac_bus *bus,
503 bool enable, int index)
506 u32 register_mask = 0;
509 dev_err(bus->dev, "Address of DRSM capability is NULL\n");
513 mask |= (1 << index);
515 register_mask = readl(bus->drsmcap + AZX_REG_SPB_SPBFCCTL);
517 mask |= register_mask;
520 snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, 0, mask);
522 snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, 0);
524 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_drsm_enable);
527 * snd_hdac_ext_stream_set_dpibr - sets the dpibr value of a stream
528 * @bus: HD-audio core bus
529 * @stream: hdac_ext_stream
530 * @value: dpib value to set
532 int snd_hdac_ext_stream_set_dpibr(struct hdac_bus *bus,
533 struct hdac_ext_stream *stream, u32 value)
537 dev_err(bus->dev, "Address of DRSM capability is NULL\n");
541 writel(value, stream->dpibr_addr);
545 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_dpibr);
548 * snd_hdac_ext_stream_set_lpib - sets the lpib value of a stream
549 * @bus: HD-audio core bus
550 * @stream: hdac_ext_stream
551 * @value: lpib value to set
553 int snd_hdac_ext_stream_set_lpib(struct hdac_ext_stream *stream, u32 value)
555 snd_hdac_stream_writel(&stream->hstream, SD_LPIB, value);
559 EXPORT_SYMBOL_GPL(snd_hdac_ext_stream_set_lpib);