]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - sound/hda/hdac_stream.c
ALSA: hda: add verbs for stripe control
[mirror_ubuntu-jammy-kernel.git] / sound / hda / hdac_stream.c
CommitLineData
14752412
TI
1/*
2 * HD-audio stream operations
3 */
4
5#include <linux/kernel.h>
6#include <linux/delay.h>
7#include <linux/export.h>
5f26face 8#include <linux/clocksource.h>
14752412
TI
9#include <sound/core.h>
10#include <sound/pcm.h>
11#include <sound/hdaudio.h>
12#include <sound/hda_register.h>
598dfb56 13#include "trace.h"
14752412
TI
14
15/**
16 * snd_hdac_stream_init - initialize each stream (aka device)
17 * @bus: HD-audio core bus
18 * @azx_dev: HD-audio core stream object to initialize
19 * @idx: stream index number
20 * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE)
21 * @tag: the tag id to assign
22 *
23 * Assign the starting bdl address to each stream (device) and initialize.
24 */
25void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev,
26 int idx, int direction, int tag)
27{
28 azx_dev->bus = bus;
14752412
TI
29 /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
30 azx_dev->sd_addr = bus->remap_addr + (0x20 * idx + 0x80);
31 /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
32 azx_dev->sd_int_sta_mask = 1 << idx;
33 azx_dev->index = idx;
34 azx_dev->direction = direction;
35 azx_dev->stream_tag = tag;
8f3f600b 36 snd_hdac_dsp_lock_init(azx_dev);
14752412
TI
37 list_add_tail(&azx_dev->list, &bus->stream_list);
38}
39EXPORT_SYMBOL_GPL(snd_hdac_stream_init);
40
41/**
42 * snd_hdac_stream_start - start a stream
43 * @azx_dev: HD-audio core stream to start
44 * @fresh_start: false = wallclock timestamp relative to period wallclock
45 *
46 * Start a stream, set start_wallclk and set the running flag.
47 */
48void snd_hdac_stream_start(struct hdac_stream *azx_dev, bool fresh_start)
49{
50 struct hdac_bus *bus = azx_dev->bus;
51
598dfb56
LY
52 trace_snd_hdac_stream_start(bus, azx_dev);
53
14752412
TI
54 azx_dev->start_wallclk = snd_hdac_chip_readl(bus, WALLCLK);
55 if (!fresh_start)
56 azx_dev->start_wallclk -= azx_dev->period_wallclk;
57
58 /* enable SIE */
fc2a6cf0
KJ
59 snd_hdac_chip_updatel(bus, INTCTL,
60 1 << azx_dev->index,
61 1 << azx_dev->index);
14752412
TI
62 /* set DMA start and interrupt mask */
63 snd_hdac_stream_updateb(azx_dev, SD_CTL,
64 0, SD_CTL_DMA_START | SD_INT_MASK);
65 azx_dev->running = true;
66}
67EXPORT_SYMBOL_GPL(snd_hdac_stream_start);
68
69/**
70 * snd_hdac_stream_clear - stop a stream DMA
71 * @azx_dev: HD-audio core stream to stop
72 */
73void snd_hdac_stream_clear(struct hdac_stream *azx_dev)
74{
75 snd_hdac_stream_updateb(azx_dev, SD_CTL,
76 SD_CTL_DMA_START | SD_INT_MASK, 0);
77 snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
78 azx_dev->running = false;
79}
80EXPORT_SYMBOL_GPL(snd_hdac_stream_clear);
81
82/**
83 * snd_hdac_stream_stop - stop a stream
84 * @azx_dev: HD-audio core stream to stop
85 *
86 * Stop a stream DMA and disable stream interrupt
87 */
88void snd_hdac_stream_stop(struct hdac_stream *azx_dev)
89{
598dfb56
LY
90 trace_snd_hdac_stream_stop(azx_dev->bus, azx_dev);
91
14752412
TI
92 snd_hdac_stream_clear(azx_dev);
93 /* disable SIE */
94 snd_hdac_chip_updatel(azx_dev->bus, INTCTL, 1 << azx_dev->index, 0);
95}
96EXPORT_SYMBOL_GPL(snd_hdac_stream_stop);
97
98/**
99 * snd_hdac_stream_reset - reset a stream
100 * @azx_dev: HD-audio core stream to reset
101 */
102void snd_hdac_stream_reset(struct hdac_stream *azx_dev)
103{
104 unsigned char val;
105 int timeout;
106
107 snd_hdac_stream_clear(azx_dev);
108
109 snd_hdac_stream_updateb(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET);
110 udelay(3);
111 timeout = 300;
112 do {
113 val = snd_hdac_stream_readb(azx_dev, SD_CTL) &
114 SD_CTL_STREAM_RESET;
115 if (val)
116 break;
117 } while (--timeout);
118 val &= ~SD_CTL_STREAM_RESET;
119 snd_hdac_stream_writeb(azx_dev, SD_CTL, val);
120 udelay(3);
121
122 timeout = 300;
123 /* waiting for hardware to report that the stream is out of reset */
124 do {
125 val = snd_hdac_stream_readb(azx_dev, SD_CTL) &
126 SD_CTL_STREAM_RESET;
127 if (!val)
128 break;
129 } while (--timeout);
130
131 /* reset first position - may not be synced with hw at this time */
132 if (azx_dev->posbuf)
133 *azx_dev->posbuf = 0;
134}
135EXPORT_SYMBOL_GPL(snd_hdac_stream_reset);
136
137/**
138 * snd_hdac_stream_setup - set up the SD for streaming
139 * @azx_dev: HD-audio core stream to set up
140 */
141int snd_hdac_stream_setup(struct hdac_stream *azx_dev)
142{
143 struct hdac_bus *bus = azx_dev->bus;
4214c534 144 struct snd_pcm_runtime *runtime;
14752412
TI
145 unsigned int val;
146
4214c534
TI
147 if (azx_dev->substream)
148 runtime = azx_dev->substream->runtime;
149 else
150 runtime = NULL;
14752412
TI
151 /* make sure the run bit is zero for SD */
152 snd_hdac_stream_clear(azx_dev);
153 /* program the stream_tag */
154 val = snd_hdac_stream_readl(azx_dev, SD_CTL);
155 val = (val & ~SD_CTL_STREAM_TAG_MASK) |
156 (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
157 if (!bus->snoop)
158 val |= SD_CTL_TRAFFIC_PRIO;
159 snd_hdac_stream_writel(azx_dev, SD_CTL, val);
160
161 /* program the length of samples in cyclic buffer */
162 snd_hdac_stream_writel(azx_dev, SD_CBL, azx_dev->bufsize);
163
164 /* program the stream format */
165 /* this value needs to be the same as the one programmed */
166 snd_hdac_stream_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
167
168 /* program the stream LVI (last valid index) of the BDL */
169 snd_hdac_stream_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
170
171 /* program the BDL address */
172 /* lower BDL address */
173 snd_hdac_stream_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
174 /* upper BDL address */
175 snd_hdac_stream_writel(azx_dev, SD_BDLPU,
176 upper_32_bits(azx_dev->bdl.addr));
177
178 /* enable the position buffer */
179 if (bus->use_posbuf && bus->posbuf.addr) {
180 if (!(snd_hdac_chip_readl(bus, DPLBASE) & AZX_DPLBASE_ENABLE))
181 snd_hdac_chip_writel(bus, DPLBASE,
182 (u32)bus->posbuf.addr | AZX_DPLBASE_ENABLE);
183 }
184
185 /* set the interrupt enable bits in the descriptor control register */
186 snd_hdac_stream_updatel(azx_dev, SD_CTL, 0, SD_INT_MASK);
187
188 if (azx_dev->direction == SNDRV_PCM_STREAM_PLAYBACK)
189 azx_dev->fifo_size =
190 snd_hdac_stream_readw(azx_dev, SD_FIFOSIZE) + 1;
191 else
192 azx_dev->fifo_size = 0;
193
194 /* when LPIB delay correction gives a small negative value,
195 * we ignore it; currently set the threshold statically to
196 * 64 frames
197 */
4214c534 198 if (runtime && runtime->period_size > 64)
14752412
TI
199 azx_dev->delay_negative_threshold =
200 -frames_to_bytes(runtime, 64);
201 else
202 azx_dev->delay_negative_threshold = 0;
203
204 /* wallclk has 24Mhz clock source */
4214c534
TI
205 if (runtime)
206 azx_dev->period_wallclk = (((runtime->period_size * 24000) /
14752412
TI
207 runtime->rate) * 1000);
208
209 return 0;
210}
211EXPORT_SYMBOL_GPL(snd_hdac_stream_setup);
212
213/**
214 * snd_hdac_stream_cleanup - cleanup a stream
215 * @azx_dev: HD-audio core stream to clean up
216 */
217void snd_hdac_stream_cleanup(struct hdac_stream *azx_dev)
218{
219 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
220 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
221 snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
222 azx_dev->bufsize = 0;
223 azx_dev->period_bytes = 0;
224 azx_dev->format_val = 0;
225}
226EXPORT_SYMBOL_GPL(snd_hdac_stream_cleanup);
227
228/**
229 * snd_hdac_stream_assign - assign a stream for the PCM
230 * @bus: HD-audio core bus
231 * @substream: PCM substream to assign
232 *
233 * Look for an unused stream for the given PCM substream, assign it
234 * and return the stream object. If no stream is free, returns NULL.
235 * The function tries to keep using the same stream object when it's used
236 * beforehand. Also, when bus->reverse_assign flag is set, the last free
237 * or matching entry is returned. This is needed for some strange codecs.
238 */
239struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus,
240 struct snd_pcm_substream *substream)
241{
242 struct hdac_stream *azx_dev;
243 struct hdac_stream *res = NULL;
244
245 /* make a non-zero unique key for the substream */
246 int key = (substream->pcm->device << 16) | (substream->number << 2) |
247 (substream->stream + 1);
248
249 list_for_each_entry(azx_dev, &bus->stream_list, list) {
250 if (azx_dev->direction != substream->stream)
251 continue;
252 if (azx_dev->opened)
253 continue;
254 if (azx_dev->assigned_key == key) {
255 res = azx_dev;
256 break;
257 }
258 if (!res || bus->reverse_assign)
259 res = azx_dev;
260 }
261 if (res) {
262 spin_lock_irq(&bus->reg_lock);
263 res->opened = 1;
264 res->running = 0;
265 res->assigned_key = key;
266 res->substream = substream;
267 spin_unlock_irq(&bus->reg_lock);
268 }
269 return res;
270}
271EXPORT_SYMBOL_GPL(snd_hdac_stream_assign);
272
273/**
274 * snd_hdac_stream_release - release the assigned stream
275 * @azx_dev: HD-audio core stream to release
276 *
277 * Release the stream that has been assigned by snd_hdac_stream_assign().
278 */
279void snd_hdac_stream_release(struct hdac_stream *azx_dev)
280{
281 struct hdac_bus *bus = azx_dev->bus;
282
283 spin_lock_irq(&bus->reg_lock);
284 azx_dev->opened = 0;
285 azx_dev->running = 0;
286 azx_dev->substream = NULL;
287 spin_unlock_irq(&bus->reg_lock);
288}
289EXPORT_SYMBOL_GPL(snd_hdac_stream_release);
290
4308c9b0
JK
291/**
292 * snd_hdac_get_stream - return hdac_stream based on stream_tag and
293 * direction
294 *
295 * @bus: HD-audio core bus
296 * @dir: direction for the stream to be found
297 * @stream_tag: stream tag for stream to be found
298 */
299struct hdac_stream *snd_hdac_get_stream(struct hdac_bus *bus,
300 int dir, int stream_tag)
301{
302 struct hdac_stream *s;
303
304 list_for_each_entry(s, &bus->stream_list, list) {
305 if (s->direction == dir && s->stream_tag == stream_tag)
306 return s;
307 }
308
309 return NULL;
310}
311EXPORT_SYMBOL_GPL(snd_hdac_get_stream);
312
14752412
TI
313/*
314 * set up a BDL entry
315 */
316static int setup_bdle(struct hdac_bus *bus,
317 struct snd_dma_buffer *dmab,
318 struct hdac_stream *azx_dev, __le32 **bdlp,
319 int ofs, int size, int with_ioc)
320{
321 __le32 *bdl = *bdlp;
322
323 while (size > 0) {
324 dma_addr_t addr;
325 int chunk;
326
327 if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
328 return -EINVAL;
329
330 addr = snd_sgbuf_get_addr(dmab, ofs);
331 /* program the address field of the BDL entry */
332 bdl[0] = cpu_to_le32((u32)addr);
333 bdl[1] = cpu_to_le32(upper_32_bits(addr));
334 /* program the size field of the BDL entry */
335 chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
336 /* one BDLE cannot cross 4K boundary on CTHDA chips */
337 if (bus->align_bdle_4k) {
338 u32 remain = 0x1000 - (ofs & 0xfff);
339
340 if (chunk > remain)
341 chunk = remain;
342 }
343 bdl[2] = cpu_to_le32(chunk);
344 /* program the IOC to enable interrupt
345 * only when the whole fragment is processed
346 */
347 size -= chunk;
348 bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
349 bdl += 4;
350 azx_dev->frags++;
351 ofs += chunk;
352 }
353 *bdlp = bdl;
354 return ofs;
355}
356
357/**
358 * snd_hdac_stream_setup_periods - set up BDL entries
359 * @azx_dev: HD-audio core stream to set up
360 *
361 * Set up the buffer descriptor table of the given stream based on the
362 * period and buffer sizes of the assigned PCM substream.
363 */
364int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev)
365{
366 struct hdac_bus *bus = azx_dev->bus;
367 struct snd_pcm_substream *substream = azx_dev->substream;
368 struct snd_pcm_runtime *runtime = substream->runtime;
369 __le32 *bdl;
370 int i, ofs, periods, period_bytes;
371 int pos_adj, pos_align;
372
373 /* reset BDL address */
374 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
375 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
376
377 period_bytes = azx_dev->period_bytes;
378 periods = azx_dev->bufsize / period_bytes;
379
380 /* program the initial BDL entries */
381 bdl = (__le32 *)azx_dev->bdl.area;
382 ofs = 0;
383 azx_dev->frags = 0;
384
385 pos_adj = bus->bdl_pos_adj;
386 if (!azx_dev->no_period_wakeup && pos_adj > 0) {
387 pos_align = pos_adj;
388 pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
389 if (!pos_adj)
390 pos_adj = pos_align;
391 else
392 pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
393 pos_align;
394 pos_adj = frames_to_bytes(runtime, pos_adj);
395 if (pos_adj >= period_bytes) {
396 dev_warn(bus->dev, "Too big adjustment %d\n",
397 pos_adj);
398 pos_adj = 0;
399 } else {
400 ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
401 azx_dev,
402 &bdl, ofs, pos_adj, true);
403 if (ofs < 0)
404 goto error;
405 }
406 } else
407 pos_adj = 0;
408
409 for (i = 0; i < periods; i++) {
410 if (i == periods - 1 && pos_adj)
411 ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
412 azx_dev, &bdl, ofs,
413 period_bytes - pos_adj, 0);
414 else
415 ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
416 azx_dev, &bdl, ofs,
417 period_bytes,
418 !azx_dev->no_period_wakeup);
419 if (ofs < 0)
420 goto error;
421 }
422 return 0;
423
424 error:
425 dev_err(bus->dev, "Too many BDL entries: buffer=%d, period=%d\n",
426 azx_dev->bufsize, period_bytes);
427 return -EINVAL;
428}
429EXPORT_SYMBOL_GPL(snd_hdac_stream_setup_periods);
430
78dd5e21
TI
431/**
432 * snd_hdac_stream_set_params - set stream parameters
86f6501b
JK
433 * @azx_dev: HD-audio core stream for which parameters are to be set
434 * @format_val: format value parameter
435 *
436 * Setup the HD-audio core stream parameters from substream of the stream
437 * and passed format value
438 */
439int snd_hdac_stream_set_params(struct hdac_stream *azx_dev,
440 unsigned int format_val)
441{
442
443 unsigned int bufsize, period_bytes;
444 struct snd_pcm_substream *substream = azx_dev->substream;
445 struct snd_pcm_runtime *runtime;
446 int err;
447
448 if (!substream)
449 return -EINVAL;
450 runtime = substream->runtime;
451 bufsize = snd_pcm_lib_buffer_bytes(substream);
452 period_bytes = snd_pcm_lib_period_bytes(substream);
453
454 if (bufsize != azx_dev->bufsize ||
455 period_bytes != azx_dev->period_bytes ||
456 format_val != azx_dev->format_val ||
457 runtime->no_period_wakeup != azx_dev->no_period_wakeup) {
458 azx_dev->bufsize = bufsize;
459 azx_dev->period_bytes = period_bytes;
460 azx_dev->format_val = format_val;
461 azx_dev->no_period_wakeup = runtime->no_period_wakeup;
462 err = snd_hdac_stream_setup_periods(azx_dev);
463 if (err < 0)
464 return err;
465 }
466 return 0;
467}
468EXPORT_SYMBOL_GPL(snd_hdac_stream_set_params);
469
a5a1d1c2 470static u64 azx_cc_read(const struct cyclecounter *cc)
14752412
TI
471{
472 struct hdac_stream *azx_dev = container_of(cc, struct hdac_stream, cc);
473
474 return snd_hdac_chip_readl(azx_dev->bus, WALLCLK);
475}
476
477static void azx_timecounter_init(struct hdac_stream *azx_dev,
a5a1d1c2 478 bool force, u64 last)
14752412
TI
479{
480 struct timecounter *tc = &azx_dev->tc;
481 struct cyclecounter *cc = &azx_dev->cc;
482 u64 nsec;
483
484 cc->read = azx_cc_read;
485 cc->mask = CLOCKSOURCE_MASK(32);
486
487 /*
488 * Converting from 24 MHz to ns means applying a 125/3 factor.
489 * To avoid any saturation issues in intermediate operations,
490 * the 125 factor is applied first. The division is applied
491 * last after reading the timecounter value.
492 * Applying the 1/3 factor as part of the multiplication
493 * requires at least 20 bits for a decent precision, however
494 * overflows occur after about 4 hours or less, not a option.
495 */
496
497 cc->mult = 125; /* saturation after 195 years */
498 cc->shift = 0;
499
500 nsec = 0; /* audio time is elapsed time since trigger */
501 timecounter_init(tc, cc, nsec);
502 if (force) {
503 /*
504 * force timecounter to use predefined value,
505 * used for synchronized starts
506 */
507 tc->cycle_last = last;
508 }
509}
510
511/**
512 * snd_hdac_stream_timecounter_init - initialize time counter
513 * @azx_dev: HD-audio core stream (master stream)
514 * @streams: bit flags of streams to set up
515 *
516 * Initializes the time counter of streams marked by the bit flags (each
517 * bit corresponds to the stream index).
518 * The trigger timestamp of PCM substream assigned to the given stream is
519 * updated accordingly, too.
520 */
521void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev,
522 unsigned int streams)
523{
524 struct hdac_bus *bus = azx_dev->bus;
525 struct snd_pcm_runtime *runtime = azx_dev->substream->runtime;
526 struct hdac_stream *s;
527 bool inited = false;
a5a1d1c2 528 u64 cycle_last = 0;
14752412
TI
529 int i = 0;
530
531 list_for_each_entry(s, &bus->stream_list, list) {
532 if (streams & (1 << i)) {
533 azx_timecounter_init(s, inited, cycle_last);
534 if (!inited) {
535 inited = true;
536 cycle_last = s->tc.cycle_last;
537 }
538 }
539 i++;
540 }
541
542 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
543 runtime->trigger_tstamp_latched = true;
544}
545EXPORT_SYMBOL_GPL(snd_hdac_stream_timecounter_init);
546
547/**
548 * snd_hdac_stream_sync_trigger - turn on/off stream sync register
549 * @azx_dev: HD-audio core stream (master stream)
550 * @streams: bit flags of streams to sync
551 */
552void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set,
553 unsigned int streams, unsigned int reg)
554{
555 struct hdac_bus *bus = azx_dev->bus;
556 unsigned int val;
557
558 if (!reg)
559 reg = AZX_REG_SSYNC;
2c1f8138 560 val = _snd_hdac_chip_readl(bus, reg);
14752412
TI
561 if (set)
562 val |= streams;
563 else
564 val &= ~streams;
2c1f8138 565 _snd_hdac_chip_writel(bus, reg, val);
14752412
TI
566}
567EXPORT_SYMBOL_GPL(snd_hdac_stream_sync_trigger);
568
569/**
570 * snd_hdac_stream_sync - sync with start/strop trigger operation
571 * @azx_dev: HD-audio core stream (master stream)
572 * @start: true = start, false = stop
573 * @streams: bit flags of streams to sync
574 *
575 * For @start = true, wait until all FIFOs get ready.
576 * For @start = false, wait until all RUN bits are cleared.
577 */
578void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start,
579 unsigned int streams)
580{
581 struct hdac_bus *bus = azx_dev->bus;
582 int i, nwait, timeout;
583 struct hdac_stream *s;
584
585 for (timeout = 5000; timeout; timeout--) {
586 nwait = 0;
587 i = 0;
588 list_for_each_entry(s, &bus->stream_list, list) {
589 if (streams & (1 << i)) {
590 if (start) {
591 /* check FIFO gets ready */
592 if (!(snd_hdac_stream_readb(s, SD_STS) &
593 SD_STS_FIFO_READY))
594 nwait++;
595 } else {
596 /* check RUN bit is cleared */
597 if (snd_hdac_stream_readb(s, SD_CTL) &
598 SD_CTL_DMA_START)
599 nwait++;
600 }
601 }
602 i++;
603 }
604 if (!nwait)
605 break;
606 cpu_relax();
607 }
608}
609EXPORT_SYMBOL_GPL(snd_hdac_stream_sync);
8f3f600b
TI
610
611#ifdef CONFIG_SND_HDA_DSP_LOADER
612/**
613 * snd_hdac_dsp_prepare - prepare for DSP loading
614 * @azx_dev: HD-audio core stream used for DSP loading
615 * @format: HD-audio stream format
616 * @byte_size: data chunk byte size
617 * @bufp: allocated buffer
618 *
619 * Allocate the buffer for the given size and set up the given stream for
620 * DSP loading. Returns the stream tag (>= 0), or a negative error code.
621 */
622int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
623 unsigned int byte_size, struct snd_dma_buffer *bufp)
624{
625 struct hdac_bus *bus = azx_dev->bus;
7362b0fc 626 __le32 *bdl;
8f3f600b
TI
627 int err;
628
629 snd_hdac_dsp_lock(azx_dev);
630 spin_lock_irq(&bus->reg_lock);
631 if (azx_dev->running || azx_dev->locked) {
632 spin_unlock_irq(&bus->reg_lock);
633 err = -EBUSY;
634 goto unlock;
635 }
636 azx_dev->locked = true;
637 spin_unlock_irq(&bus->reg_lock);
638
639 err = bus->io_ops->dma_alloc_pages(bus, SNDRV_DMA_TYPE_DEV_SG,
640 byte_size, bufp);
641 if (err < 0)
642 goto err_alloc;
643
4214c534 644 azx_dev->substream = NULL;
8f3f600b
TI
645 azx_dev->bufsize = byte_size;
646 azx_dev->period_bytes = byte_size;
647 azx_dev->format_val = format;
648
649 snd_hdac_stream_reset(azx_dev);
650
651 /* reset BDL address */
652 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
653 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
654
655 azx_dev->frags = 0;
7362b0fc 656 bdl = (__le32 *)azx_dev->bdl.area;
8f3f600b
TI
657 err = setup_bdle(bus, bufp, azx_dev, &bdl, 0, byte_size, 0);
658 if (err < 0)
659 goto error;
660
661 snd_hdac_stream_setup(azx_dev);
662 snd_hdac_dsp_unlock(azx_dev);
663 return azx_dev->stream_tag;
664
665 error:
666 bus->io_ops->dma_free_pages(bus, bufp);
667 err_alloc:
668 spin_lock_irq(&bus->reg_lock);
669 azx_dev->locked = false;
670 spin_unlock_irq(&bus->reg_lock);
671 unlock:
672 snd_hdac_dsp_unlock(azx_dev);
673 return err;
674}
675EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare);
676
677/**
678 * snd_hdac_dsp_trigger - start / stop DSP loading
679 * @azx_dev: HD-audio core stream used for DSP loading
680 * @start: trigger start or stop
681 */
682void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start)
683{
684 if (start)
685 snd_hdac_stream_start(azx_dev, true);
686 else
687 snd_hdac_stream_stop(azx_dev);
688}
689EXPORT_SYMBOL_GPL(snd_hdac_dsp_trigger);
690
691/**
692 * snd_hdac_dsp_cleanup - clean up the stream from DSP loading to normal
693 * @azx_dev: HD-audio core stream used for DSP loading
694 * @dmab: buffer used by DSP loading
695 */
696void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
697 struct snd_dma_buffer *dmab)
698{
699 struct hdac_bus *bus = azx_dev->bus;
700
701 if (!dmab->area || !azx_dev->locked)
702 return;
703
704 snd_hdac_dsp_lock(azx_dev);
705 /* reset BDL address */
706 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
707 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
708 snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
709 azx_dev->bufsize = 0;
710 azx_dev->period_bytes = 0;
711 azx_dev->format_val = 0;
712
713 bus->io_ops->dma_free_pages(bus, dmab);
714 dmab->area = NULL;
715
716 spin_lock_irq(&bus->reg_lock);
717 azx_dev->locked = false;
718 spin_unlock_irq(&bus->reg_lock);
719 snd_hdac_dsp_unlock(azx_dev);
720}
721EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup);
722#endif /* CONFIG_SND_HDA_DSP_LOADER */