1 // SPDX-License-Identifier: GPL-2.0-only
3 * HD-audio stream operations
6 #include <linux/kernel.h>
7 #include <linux/delay.h>
8 #include <linux/export.h>
9 #include <linux/clocksource.h>
10 #include <sound/compress_driver.h>
11 #include <sound/core.h>
12 #include <sound/pcm.h>
13 #include <sound/hdaudio.h>
14 #include <sound/hda_register.h>
18 * the hdac_stream library is intended to be used with the following
19 * transitions. The states are not formally defined in the code but loosely
20 * inspired by boolean variables. Note that the 'prepared' field is not used
21 * in this library but by the callers during the hw_params/prepare transitions
30 * stream_assign() | | stream_release()
37 * stream_setup() | | stream_cleanup()
43 * stream_start() | | stream_stop()
51 * snd_hdac_get_stream_stripe_ctl - get stripe control value
52 * @bus: HD-audio core bus
53 * @substream: PCM substream
55 int snd_hdac_get_stream_stripe_ctl(struct hdac_bus *bus,
56 struct snd_pcm_substream *substream)
58 struct snd_pcm_runtime *runtime = substream->runtime;
59 unsigned int channels = runtime->channels,
61 bits_per_sample = runtime->sample_bits,
62 max_sdo_lines, value, sdo_line;
64 /* T_AZA_GCAP_NSDO is 1:2 bitfields in GCAP */
65 max_sdo_lines = snd_hdac_chip_readl(bus, GCAP) & AZX_GCAP_NSDO;
67 /* following is from HD audio spec */
68 for (sdo_line = max_sdo_lines; sdo_line > 0; sdo_line >>= 1) {
70 value = (channels * bits_per_sample *
71 (rate / 48000)) / sdo_line;
73 value = (channels * bits_per_sample) / sdo_line;
75 if (value >= bus->sdo_limit)
79 /* stripe value: 0 for 1SDO, 1 for 2SDO, 2 for 4SDO lines */
82 EXPORT_SYMBOL_GPL(snd_hdac_get_stream_stripe_ctl);
85 * snd_hdac_stream_init - initialize each stream (aka device)
86 * @bus: HD-audio core bus
87 * @azx_dev: HD-audio core stream object to initialize
88 * @idx: stream index number
89 * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE)
90 * @tag: the tag id to assign
92 * Assign the starting bdl address to each stream (device) and initialize.
94 void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev,
95 int idx, int direction, int tag)
98 /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
99 azx_dev->sd_addr = bus->remap_addr + (0x20 * idx + 0x80);
100 /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
101 azx_dev->sd_int_sta_mask = 1 << idx;
102 azx_dev->index = idx;
103 azx_dev->direction = direction;
104 azx_dev->stream_tag = tag;
105 snd_hdac_dsp_lock_init(azx_dev);
106 list_add_tail(&azx_dev->list, &bus->stream_list);
109 azx_dev->spib_addr = bus->spbcap + AZX_SPB_BASE +
110 AZX_SPB_INTERVAL * idx +
113 azx_dev->fifo_addr = bus->spbcap + AZX_SPB_BASE +
114 AZX_SPB_INTERVAL * idx +
119 azx_dev->dpibr_addr = bus->drsmcap + AZX_DRSM_BASE +
120 AZX_DRSM_INTERVAL * idx;
122 EXPORT_SYMBOL_GPL(snd_hdac_stream_init);
125 * snd_hdac_stream_start - start a stream
126 * @azx_dev: HD-audio core stream to start
127 * @fresh_start: false = wallclock timestamp relative to period wallclock
129 * Start a stream, set start_wallclk and set the running flag.
131 void snd_hdac_stream_start(struct hdac_stream *azx_dev, bool fresh_start)
133 struct hdac_bus *bus = azx_dev->bus;
136 trace_snd_hdac_stream_start(bus, azx_dev);
138 azx_dev->start_wallclk = snd_hdac_chip_readl(bus, WALLCLK);
140 azx_dev->start_wallclk -= azx_dev->period_wallclk;
143 snd_hdac_chip_updatel(bus, INTCTL,
145 1 << azx_dev->index);
146 /* set stripe control */
147 if (azx_dev->stripe) {
148 if (azx_dev->substream)
149 stripe_ctl = snd_hdac_get_stream_stripe_ctl(bus, azx_dev->substream);
152 snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK,
155 /* set DMA start and interrupt mask */
156 snd_hdac_stream_updateb(azx_dev, SD_CTL,
157 0, SD_CTL_DMA_START | SD_INT_MASK);
158 azx_dev->running = true;
160 EXPORT_SYMBOL_GPL(snd_hdac_stream_start);
163 * snd_hdac_stream_clear - helper to clear stream registers and stop DMA transfers
164 * @azx_dev: HD-audio core stream to stop
166 static void snd_hdac_stream_clear(struct hdac_stream *azx_dev)
168 snd_hdac_stream_updateb(azx_dev, SD_CTL,
169 SD_CTL_DMA_START | SD_INT_MASK, 0);
170 snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
172 snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK, 0);
173 azx_dev->running = false;
177 * snd_hdac_stream_stop - stop a stream
178 * @azx_dev: HD-audio core stream to stop
180 * Stop a stream DMA and disable stream interrupt
182 void snd_hdac_stream_stop(struct hdac_stream *azx_dev)
184 trace_snd_hdac_stream_stop(azx_dev->bus, azx_dev);
186 snd_hdac_stream_clear(azx_dev);
188 snd_hdac_chip_updatel(azx_dev->bus, INTCTL, 1 << azx_dev->index, 0);
190 EXPORT_SYMBOL_GPL(snd_hdac_stream_stop);
193 * snd_hdac_stop_streams - stop all streams
194 * @bus: HD-audio core bus
196 void snd_hdac_stop_streams(struct hdac_bus *bus)
198 struct hdac_stream *stream;
200 list_for_each_entry(stream, &bus->stream_list, list)
201 snd_hdac_stream_stop(stream);
203 EXPORT_SYMBOL_GPL(snd_hdac_stop_streams);
206 * snd_hdac_stop_streams_and_chip - stop all streams and chip if running
207 * @bus: HD-audio core bus
209 void snd_hdac_stop_streams_and_chip(struct hdac_bus *bus)
212 if (bus->chip_init) {
213 snd_hdac_stop_streams(bus);
214 snd_hdac_bus_stop_chip(bus);
217 EXPORT_SYMBOL_GPL(snd_hdac_stop_streams_and_chip);
220 * snd_hdac_stream_reset - reset a stream
221 * @azx_dev: HD-audio core stream to reset
223 void snd_hdac_stream_reset(struct hdac_stream *azx_dev)
228 snd_hdac_stream_clear(azx_dev);
230 dma_run_state = snd_hdac_stream_readb(azx_dev, SD_CTL) & SD_CTL_DMA_START;
232 snd_hdac_stream_updateb(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET);
234 /* wait for hardware to report that the stream entered reset */
235 snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, (val & SD_CTL_STREAM_RESET), 3, 300);
237 if (azx_dev->bus->dma_stop_delay && dma_run_state)
238 udelay(azx_dev->bus->dma_stop_delay);
240 snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_CTL_STREAM_RESET, 0);
242 /* wait for hardware to report that the stream is out of reset */
243 snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, !(val & SD_CTL_STREAM_RESET), 3, 300);
245 /* reset first position - may not be synced with hw at this time */
247 *azx_dev->posbuf = 0;
249 EXPORT_SYMBOL_GPL(snd_hdac_stream_reset);
252 * snd_hdac_stream_setup - set up the SD for streaming
253 * @azx_dev: HD-audio core stream to set up
255 int snd_hdac_stream_setup(struct hdac_stream *azx_dev)
257 struct hdac_bus *bus = azx_dev->bus;
258 struct snd_pcm_runtime *runtime;
261 if (azx_dev->substream)
262 runtime = azx_dev->substream->runtime;
265 /* make sure the run bit is zero for SD */
266 snd_hdac_stream_clear(azx_dev);
267 /* program the stream_tag */
268 val = snd_hdac_stream_readl(azx_dev, SD_CTL);
269 val = (val & ~SD_CTL_STREAM_TAG_MASK) |
270 (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
272 val |= SD_CTL_TRAFFIC_PRIO;
273 snd_hdac_stream_writel(azx_dev, SD_CTL, val);
275 /* program the length of samples in cyclic buffer */
276 snd_hdac_stream_writel(azx_dev, SD_CBL, azx_dev->bufsize);
278 /* program the stream format */
279 /* this value needs to be the same as the one programmed */
280 snd_hdac_stream_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
282 /* program the stream LVI (last valid index) of the BDL */
283 snd_hdac_stream_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
285 /* program the BDL address */
286 /* lower BDL address */
287 snd_hdac_stream_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
288 /* upper BDL address */
289 snd_hdac_stream_writel(azx_dev, SD_BDLPU,
290 upper_32_bits(azx_dev->bdl.addr));
292 /* enable the position buffer */
293 if (bus->use_posbuf && bus->posbuf.addr) {
294 if (!(snd_hdac_chip_readl(bus, DPLBASE) & AZX_DPLBASE_ENABLE))
295 snd_hdac_chip_writel(bus, DPLBASE,
296 (u32)bus->posbuf.addr | AZX_DPLBASE_ENABLE);
299 /* set the interrupt enable bits in the descriptor control register */
300 snd_hdac_stream_updatel(azx_dev, SD_CTL, 0, SD_INT_MASK);
302 azx_dev->fifo_size = snd_hdac_stream_readw(azx_dev, SD_FIFOSIZE) + 1;
304 /* when LPIB delay correction gives a small negative value,
305 * we ignore it; currently set the threshold statically to
308 if (runtime && runtime->period_size > 64)
309 azx_dev->delay_negative_threshold =
310 -frames_to_bytes(runtime, 64);
312 azx_dev->delay_negative_threshold = 0;
314 /* wallclk has 24Mhz clock source */
316 azx_dev->period_wallclk = (((runtime->period_size * 24000) /
317 runtime->rate) * 1000);
321 EXPORT_SYMBOL_GPL(snd_hdac_stream_setup);
324 * snd_hdac_stream_cleanup - cleanup a stream
325 * @azx_dev: HD-audio core stream to clean up
327 void snd_hdac_stream_cleanup(struct hdac_stream *azx_dev)
329 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
330 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
331 snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
332 azx_dev->bufsize = 0;
333 azx_dev->period_bytes = 0;
334 azx_dev->format_val = 0;
336 EXPORT_SYMBOL_GPL(snd_hdac_stream_cleanup);
339 * snd_hdac_stream_assign - assign a stream for the PCM
340 * @bus: HD-audio core bus
341 * @substream: PCM substream to assign
343 * Look for an unused stream for the given PCM substream, assign it
344 * and return the stream object. If no stream is free, returns NULL.
345 * The function tries to keep using the same stream object when it's used
346 * beforehand. Also, when bus->reverse_assign flag is set, the last free
347 * or matching entry is returned. This is needed for some strange codecs.
349 struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus,
350 struct snd_pcm_substream *substream)
352 struct hdac_stream *azx_dev;
353 struct hdac_stream *res = NULL;
355 /* make a non-zero unique key for the substream */
356 int key = (substream->pcm->device << 16) | (substream->number << 2) |
357 (substream->stream + 1);
359 spin_lock_irq(&bus->reg_lock);
360 list_for_each_entry(azx_dev, &bus->stream_list, list) {
361 if (azx_dev->direction != substream->stream)
365 if (azx_dev->assigned_key == key) {
369 if (!res || bus->reverse_assign)
375 res->assigned_key = key;
376 res->substream = substream;
378 spin_unlock_irq(&bus->reg_lock);
381 EXPORT_SYMBOL_GPL(snd_hdac_stream_assign);
384 * snd_hdac_stream_release_locked - release the assigned stream
385 * @azx_dev: HD-audio core stream to release
387 * Release the stream that has been assigned by snd_hdac_stream_assign().
388 * The bus->reg_lock needs to be taken at a higher level
390 void snd_hdac_stream_release_locked(struct hdac_stream *azx_dev)
393 azx_dev->running = 0;
394 azx_dev->substream = NULL;
396 EXPORT_SYMBOL_GPL(snd_hdac_stream_release_locked);
399 * snd_hdac_stream_release - release the assigned stream
400 * @azx_dev: HD-audio core stream to release
402 * Release the stream that has been assigned by snd_hdac_stream_assign().
404 void snd_hdac_stream_release(struct hdac_stream *azx_dev)
406 struct hdac_bus *bus = azx_dev->bus;
408 spin_lock_irq(&bus->reg_lock);
409 snd_hdac_stream_release_locked(azx_dev);
410 spin_unlock_irq(&bus->reg_lock);
412 EXPORT_SYMBOL_GPL(snd_hdac_stream_release);
415 * snd_hdac_get_stream - return hdac_stream based on stream_tag and
418 * @bus: HD-audio core bus
419 * @dir: direction for the stream to be found
420 * @stream_tag: stream tag for stream to be found
422 struct hdac_stream *snd_hdac_get_stream(struct hdac_bus *bus,
423 int dir, int stream_tag)
425 struct hdac_stream *s;
427 list_for_each_entry(s, &bus->stream_list, list) {
428 if (s->direction == dir && s->stream_tag == stream_tag)
434 EXPORT_SYMBOL_GPL(snd_hdac_get_stream);
439 static int setup_bdle(struct hdac_bus *bus,
440 struct snd_dma_buffer *dmab,
441 struct hdac_stream *azx_dev, __le32 **bdlp,
442 int ofs, int size, int with_ioc)
450 if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
453 addr = snd_sgbuf_get_addr(dmab, ofs);
454 /* program the address field of the BDL entry */
455 bdl[0] = cpu_to_le32((u32)addr);
456 bdl[1] = cpu_to_le32(upper_32_bits(addr));
457 /* program the size field of the BDL entry */
458 chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
459 /* one BDLE cannot cross 4K boundary on CTHDA chips */
460 if (bus->align_bdle_4k) {
461 u32 remain = 0x1000 - (ofs & 0xfff);
466 bdl[2] = cpu_to_le32(chunk);
467 /* program the IOC to enable interrupt
468 * only when the whole fragment is processed
471 bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
481 * snd_hdac_stream_setup_periods - set up BDL entries
482 * @azx_dev: HD-audio core stream to set up
484 * Set up the buffer descriptor table of the given stream based on the
485 * period and buffer sizes of the assigned PCM substream.
487 int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev)
489 struct hdac_bus *bus = azx_dev->bus;
490 struct snd_pcm_substream *substream = azx_dev->substream;
491 struct snd_compr_stream *cstream = azx_dev->cstream;
492 struct snd_pcm_runtime *runtime = NULL;
493 struct snd_dma_buffer *dmab;
495 int i, ofs, periods, period_bytes;
496 int pos_adj, pos_align;
499 runtime = substream->runtime;
500 dmab = snd_pcm_get_dma_buf(substream);
501 } else if (cstream) {
502 dmab = snd_pcm_get_dma_buf(cstream);
504 WARN(1, "No substream or cstream assigned\n");
508 /* reset BDL address */
509 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
510 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
512 period_bytes = azx_dev->period_bytes;
513 periods = azx_dev->bufsize / period_bytes;
515 /* program the initial BDL entries */
516 bdl = (__le32 *)azx_dev->bdl.area;
520 pos_adj = bus->bdl_pos_adj;
521 if (runtime && !azx_dev->no_period_wakeup && pos_adj > 0) {
523 pos_adj = DIV_ROUND_UP(pos_adj * runtime->rate, 48000);
527 pos_adj = roundup(pos_adj, pos_align);
528 pos_adj = frames_to_bytes(runtime, pos_adj);
529 if (pos_adj >= period_bytes) {
530 dev_warn(bus->dev, "Too big adjustment %d\n",
534 ofs = setup_bdle(bus, dmab, azx_dev,
535 &bdl, ofs, pos_adj, true);
542 for (i = 0; i < periods; i++) {
543 if (i == periods - 1 && pos_adj)
544 ofs = setup_bdle(bus, dmab, azx_dev,
545 &bdl, ofs, period_bytes - pos_adj, 0);
547 ofs = setup_bdle(bus, dmab, azx_dev,
548 &bdl, ofs, period_bytes,
549 !azx_dev->no_period_wakeup);
556 dev_err(bus->dev, "Too many BDL entries: buffer=%d, period=%d\n",
557 azx_dev->bufsize, period_bytes);
560 EXPORT_SYMBOL_GPL(snd_hdac_stream_setup_periods);
563 * snd_hdac_stream_set_params - set stream parameters
564 * @azx_dev: HD-audio core stream for which parameters are to be set
565 * @format_val: format value parameter
567 * Setup the HD-audio core stream parameters from substream of the stream
568 * and passed format value
570 int snd_hdac_stream_set_params(struct hdac_stream *azx_dev,
571 unsigned int format_val)
573 struct snd_pcm_substream *substream = azx_dev->substream;
574 struct snd_compr_stream *cstream = azx_dev->cstream;
575 unsigned int bufsize, period_bytes;
576 unsigned int no_period_wakeup;
580 bufsize = snd_pcm_lib_buffer_bytes(substream);
581 period_bytes = snd_pcm_lib_period_bytes(substream);
582 no_period_wakeup = substream->runtime->no_period_wakeup;
583 } else if (cstream) {
584 bufsize = cstream->runtime->buffer_size;
585 period_bytes = cstream->runtime->fragment_size;
586 no_period_wakeup = 0;
591 if (bufsize != azx_dev->bufsize ||
592 period_bytes != azx_dev->period_bytes ||
593 format_val != azx_dev->format_val ||
594 no_period_wakeup != azx_dev->no_period_wakeup) {
595 azx_dev->bufsize = bufsize;
596 azx_dev->period_bytes = period_bytes;
597 azx_dev->format_val = format_val;
598 azx_dev->no_period_wakeup = no_period_wakeup;
599 err = snd_hdac_stream_setup_periods(azx_dev);
605 EXPORT_SYMBOL_GPL(snd_hdac_stream_set_params);
607 static u64 azx_cc_read(const struct cyclecounter *cc)
609 struct hdac_stream *azx_dev = container_of(cc, struct hdac_stream, cc);
611 return snd_hdac_chip_readl(azx_dev->bus, WALLCLK);
614 static void azx_timecounter_init(struct hdac_stream *azx_dev,
615 bool force, u64 last)
617 struct timecounter *tc = &azx_dev->tc;
618 struct cyclecounter *cc = &azx_dev->cc;
621 cc->read = azx_cc_read;
622 cc->mask = CLOCKSOURCE_MASK(32);
625 * Calculate the optimal mult/shift values. The counter wraps
626 * around after ~178.9 seconds.
628 clocks_calc_mult_shift(&cc->mult, &cc->shift, 24000000,
631 nsec = 0; /* audio time is elapsed time since trigger */
632 timecounter_init(tc, cc, nsec);
635 * force timecounter to use predefined value,
636 * used for synchronized starts
638 tc->cycle_last = last;
643 * snd_hdac_stream_timecounter_init - initialize time counter
644 * @azx_dev: HD-audio core stream (master stream)
645 * @streams: bit flags of streams to set up
647 * Initializes the time counter of streams marked by the bit flags (each
648 * bit corresponds to the stream index).
649 * The trigger timestamp of PCM substream assigned to the given stream is
650 * updated accordingly, too.
652 void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev,
653 unsigned int streams)
655 struct hdac_bus *bus = azx_dev->bus;
656 struct snd_pcm_runtime *runtime = azx_dev->substream->runtime;
657 struct hdac_stream *s;
662 list_for_each_entry(s, &bus->stream_list, list) {
663 if (streams & (1 << i)) {
664 azx_timecounter_init(s, inited, cycle_last);
667 cycle_last = s->tc.cycle_last;
673 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
674 runtime->trigger_tstamp_latched = true;
676 EXPORT_SYMBOL_GPL(snd_hdac_stream_timecounter_init);
679 * snd_hdac_stream_sync_trigger - turn on/off stream sync register
680 * @azx_dev: HD-audio core stream (master stream)
681 * @set: true = set, false = clear
682 * @streams: bit flags of streams to sync
683 * @reg: the stream sync register address
685 void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set,
686 unsigned int streams, unsigned int reg)
688 struct hdac_bus *bus = azx_dev->bus;
693 val = _snd_hdac_chip_readl(bus, reg);
698 _snd_hdac_chip_writel(bus, reg, val);
700 EXPORT_SYMBOL_GPL(snd_hdac_stream_sync_trigger);
703 * snd_hdac_stream_sync - sync with start/stop trigger operation
704 * @azx_dev: HD-audio core stream (master stream)
705 * @start: true = start, false = stop
706 * @streams: bit flags of streams to sync
708 * For @start = true, wait until all FIFOs get ready.
709 * For @start = false, wait until all RUN bits are cleared.
711 void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start,
712 unsigned int streams)
714 struct hdac_bus *bus = azx_dev->bus;
715 int i, nwait, timeout;
716 struct hdac_stream *s;
718 for (timeout = 5000; timeout; timeout--) {
721 list_for_each_entry(s, &bus->stream_list, list) {
722 if (!(streams & (1 << i++)))
726 /* check FIFO gets ready */
727 if (!(snd_hdac_stream_readb(s, SD_STS) &
731 /* check RUN bit is cleared */
732 if (snd_hdac_stream_readb(s, SD_CTL) &
736 * Perform stream reset if DMA RUN
737 * bit not cleared within given timeout
740 snd_hdac_stream_reset(s);
749 EXPORT_SYMBOL_GPL(snd_hdac_stream_sync);
752 * snd_hdac_stream_spbcap_enable - enable SPIB for a stream
753 * @bus: HD-audio core bus
754 * @enable: flag to enable/disable SPIB
755 * @index: stream index for which SPIB need to be enabled
757 void snd_hdac_stream_spbcap_enable(struct hdac_bus *bus,
758 bool enable, int index)
763 dev_err(bus->dev, "Address of SPB capability is NULL\n");
767 mask |= (1 << index);
770 snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, mask);
772 snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, 0);
774 EXPORT_SYMBOL_GPL(snd_hdac_stream_spbcap_enable);
777 * snd_hdac_stream_set_spib - sets the spib value of a stream
778 * @bus: HD-audio core bus
779 * @azx_dev: hdac_stream
780 * @value: spib value to set
782 int snd_hdac_stream_set_spib(struct hdac_bus *bus,
783 struct hdac_stream *azx_dev, u32 value)
786 dev_err(bus->dev, "Address of SPB capability is NULL\n");
790 writel(value, azx_dev->spib_addr);
794 EXPORT_SYMBOL_GPL(snd_hdac_stream_set_spib);
797 * snd_hdac_stream_get_spbmaxfifo - gets the spib value of a stream
798 * @bus: HD-audio core bus
799 * @azx_dev: hdac_stream
801 * Return maxfifo for the stream
803 int snd_hdac_stream_get_spbmaxfifo(struct hdac_bus *bus,
804 struct hdac_stream *azx_dev)
807 dev_err(bus->dev, "Address of SPB capability is NULL\n");
811 return readl(azx_dev->fifo_addr);
813 EXPORT_SYMBOL_GPL(snd_hdac_stream_get_spbmaxfifo);
816 * snd_hdac_stream_drsm_enable - enable DMA resume for a stream
817 * @bus: HD-audio core bus
818 * @enable: flag to enable/disable DRSM
819 * @index: stream index for which DRSM need to be enabled
821 void snd_hdac_stream_drsm_enable(struct hdac_bus *bus,
822 bool enable, int index)
827 dev_err(bus->dev, "Address of DRSM capability is NULL\n");
831 mask |= (1 << index);
834 snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, mask);
836 snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, 0);
838 EXPORT_SYMBOL_GPL(snd_hdac_stream_drsm_enable);
841 * snd_hdac_stream_wait_drsm - wait for HW to clear RSM for a stream
842 * @azx_dev: HD-audio core stream to await RSM for
844 * Returns 0 on success and -ETIMEDOUT upon a timeout.
846 int snd_hdac_stream_wait_drsm(struct hdac_stream *azx_dev)
848 struct hdac_bus *bus = azx_dev->bus;
852 mask = 1 << azx_dev->index;
854 ret = read_poll_timeout(snd_hdac_reg_readl, reg, !(reg & mask), 250, 2000, false, bus,
855 bus->drsmcap + AZX_REG_DRSM_CTL);
857 dev_dbg(bus->dev, "polling RSM 0x%08x failed: %d\n", mask, ret);
860 EXPORT_SYMBOL_GPL(snd_hdac_stream_wait_drsm);
863 * snd_hdac_stream_set_dpibr - sets the dpibr value of a stream
864 * @bus: HD-audio core bus
865 * @azx_dev: hdac_stream
866 * @value: dpib value to set
868 int snd_hdac_stream_set_dpibr(struct hdac_bus *bus,
869 struct hdac_stream *azx_dev, u32 value)
872 dev_err(bus->dev, "Address of DRSM capability is NULL\n");
876 writel(value, azx_dev->dpibr_addr);
880 EXPORT_SYMBOL_GPL(snd_hdac_stream_set_dpibr);
883 * snd_hdac_stream_set_lpib - sets the lpib value of a stream
884 * @azx_dev: hdac_stream
885 * @value: lpib value to set
887 int snd_hdac_stream_set_lpib(struct hdac_stream *azx_dev, u32 value)
889 snd_hdac_stream_writel(azx_dev, SD_LPIB, value);
893 EXPORT_SYMBOL_GPL(snd_hdac_stream_set_lpib);
895 #ifdef CONFIG_SND_HDA_DSP_LOADER
897 * snd_hdac_dsp_prepare - prepare for DSP loading
898 * @azx_dev: HD-audio core stream used for DSP loading
899 * @format: HD-audio stream format
900 * @byte_size: data chunk byte size
901 * @bufp: allocated buffer
903 * Allocate the buffer for the given size and set up the given stream for
904 * DSP loading. Returns the stream tag (>= 0), or a negative error code.
906 int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
907 unsigned int byte_size, struct snd_dma_buffer *bufp)
909 struct hdac_bus *bus = azx_dev->bus;
913 snd_hdac_dsp_lock(azx_dev);
914 spin_lock_irq(&bus->reg_lock);
915 if (azx_dev->running || azx_dev->locked) {
916 spin_unlock_irq(&bus->reg_lock);
920 azx_dev->locked = true;
921 spin_unlock_irq(&bus->reg_lock);
923 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, bus->dev,
928 azx_dev->substream = NULL;
929 azx_dev->bufsize = byte_size;
930 azx_dev->period_bytes = byte_size;
931 azx_dev->format_val = format;
933 snd_hdac_stream_reset(azx_dev);
935 /* reset BDL address */
936 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
937 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
940 bdl = (__le32 *)azx_dev->bdl.area;
941 err = setup_bdle(bus, bufp, azx_dev, &bdl, 0, byte_size, 0);
945 snd_hdac_stream_setup(azx_dev);
946 snd_hdac_dsp_unlock(azx_dev);
947 return azx_dev->stream_tag;
950 snd_dma_free_pages(bufp);
952 spin_lock_irq(&bus->reg_lock);
953 azx_dev->locked = false;
954 spin_unlock_irq(&bus->reg_lock);
956 snd_hdac_dsp_unlock(azx_dev);
959 EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare);
962 * snd_hdac_dsp_trigger - start / stop DSP loading
963 * @azx_dev: HD-audio core stream used for DSP loading
964 * @start: trigger start or stop
966 void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start)
969 snd_hdac_stream_start(azx_dev, true);
971 snd_hdac_stream_stop(azx_dev);
973 EXPORT_SYMBOL_GPL(snd_hdac_dsp_trigger);
976 * snd_hdac_dsp_cleanup - clean up the stream from DSP loading to normal
977 * @azx_dev: HD-audio core stream used for DSP loading
978 * @dmab: buffer used by DSP loading
980 void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
981 struct snd_dma_buffer *dmab)
983 struct hdac_bus *bus = azx_dev->bus;
985 if (!dmab->area || !azx_dev->locked)
988 snd_hdac_dsp_lock(azx_dev);
989 /* reset BDL address */
990 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
991 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
992 snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
993 azx_dev->bufsize = 0;
994 azx_dev->period_bytes = 0;
995 azx_dev->format_val = 0;
997 snd_dma_free_pages(dmab);
1000 spin_lock_irq(&bus->reg_lock);
1001 azx_dev->locked = false;
1002 spin_unlock_irq(&bus->reg_lock);
1003 snd_hdac_dsp_unlock(azx_dev);
1005 EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup);
1006 #endif /* CONFIG_SND_HDA_DSP_LOADER */