xref: /openbmc/linux/sound/hda/hdac_stream.c (revision 2ea13c83bf7bb3471e33b2d902b101af977ef2d4)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
214752412STakashi Iwai /*
314752412STakashi Iwai  * HD-audio stream operations
414752412STakashi Iwai  */
514752412STakashi Iwai 
614752412STakashi Iwai #include <linux/kernel.h>
714752412STakashi Iwai #include <linux/delay.h>
814752412STakashi Iwai #include <linux/export.h>
95f26faceSTakashi Iwai #include <linux/clocksource.h>
1014752412STakashi Iwai #include <sound/core.h>
1114752412STakashi Iwai #include <sound/pcm.h>
1214752412STakashi Iwai #include <sound/hdaudio.h>
1314752412STakashi Iwai #include <sound/hda_register.h>
14598dfb56SLibin Yang #include "trace.h"
1514752412STakashi Iwai 
1614752412STakashi Iwai /**
175dd3d271SSameer Pujar  * snd_hdac_get_stream_stripe_ctl - get stripe control value
185dd3d271SSameer Pujar  * @bus: HD-audio core bus
195dd3d271SSameer Pujar  * @substream: PCM substream
205dd3d271SSameer Pujar  */
215dd3d271SSameer Pujar int snd_hdac_get_stream_stripe_ctl(struct hdac_bus *bus,
225dd3d271SSameer Pujar 				   struct snd_pcm_substream *substream)
235dd3d271SSameer Pujar {
245dd3d271SSameer Pujar 	struct snd_pcm_runtime *runtime = substream->runtime;
255dd3d271SSameer Pujar 	unsigned int channels = runtime->channels,
265dd3d271SSameer Pujar 		     rate = runtime->rate,
275dd3d271SSameer Pujar 		     bits_per_sample = runtime->sample_bits,
285dd3d271SSameer Pujar 		     max_sdo_lines, value, sdo_line;
295dd3d271SSameer Pujar 
305dd3d271SSameer Pujar 	/* T_AZA_GCAP_NSDO is 1:2 bitfields in GCAP */
315dd3d271SSameer Pujar 	max_sdo_lines = snd_hdac_chip_readl(bus, GCAP) & AZX_GCAP_NSDO;
325dd3d271SSameer Pujar 
335dd3d271SSameer Pujar 	/* following is from HD audio spec */
345dd3d271SSameer Pujar 	for (sdo_line = max_sdo_lines; sdo_line > 0; sdo_line >>= 1) {
355dd3d271SSameer Pujar 		if (rate > 48000)
365dd3d271SSameer Pujar 			value = (channels * bits_per_sample *
375dd3d271SSameer Pujar 					(rate / 48000)) / sdo_line;
385dd3d271SSameer Pujar 		else
395dd3d271SSameer Pujar 			value = (channels * bits_per_sample) / sdo_line;
405dd3d271SSameer Pujar 
4167ae482aSSameer Pujar 		if (value >= bus->sdo_limit)
425dd3d271SSameer Pujar 			break;
435dd3d271SSameer Pujar 	}
445dd3d271SSameer Pujar 
455dd3d271SSameer Pujar 	/* stripe value: 0 for 1SDO, 1 for 2SDO, 2 for 4SDO lines */
465dd3d271SSameer Pujar 	return sdo_line >> 1;
475dd3d271SSameer Pujar }
485dd3d271SSameer Pujar EXPORT_SYMBOL_GPL(snd_hdac_get_stream_stripe_ctl);
495dd3d271SSameer Pujar 
505dd3d271SSameer Pujar /**
5114752412STakashi Iwai  * snd_hdac_stream_init - initialize each stream (aka device)
5214752412STakashi Iwai  * @bus: HD-audio core bus
5314752412STakashi Iwai  * @azx_dev: HD-audio core stream object to initialize
5414752412STakashi Iwai  * @idx: stream index number
5514752412STakashi Iwai  * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE)
5614752412STakashi Iwai  * @tag: the tag id to assign
5714752412STakashi Iwai  *
5814752412STakashi Iwai  * Assign the starting bdl address to each stream (device) and initialize.
5914752412STakashi Iwai  */
6014752412STakashi Iwai void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev,
6114752412STakashi Iwai 			  int idx, int direction, int tag)
6214752412STakashi Iwai {
6314752412STakashi Iwai 	azx_dev->bus = bus;
6414752412STakashi Iwai 	/* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
6514752412STakashi Iwai 	azx_dev->sd_addr = bus->remap_addr + (0x20 * idx + 0x80);
6614752412STakashi Iwai 	/* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
6714752412STakashi Iwai 	azx_dev->sd_int_sta_mask = 1 << idx;
6814752412STakashi Iwai 	azx_dev->index = idx;
6914752412STakashi Iwai 	azx_dev->direction = direction;
7014752412STakashi Iwai 	azx_dev->stream_tag = tag;
718f3f600bSTakashi Iwai 	snd_hdac_dsp_lock_init(azx_dev);
7214752412STakashi Iwai 	list_add_tail(&azx_dev->list, &bus->stream_list);
7314752412STakashi Iwai }
7414752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_init);
7514752412STakashi Iwai 
7614752412STakashi Iwai /**
7714752412STakashi Iwai  * snd_hdac_stream_start - start a stream
7814752412STakashi Iwai  * @azx_dev: HD-audio core stream to start
7914752412STakashi Iwai  * @fresh_start: false = wallclock timestamp relative to period wallclock
8014752412STakashi Iwai  *
8114752412STakashi Iwai  * Start a stream, set start_wallclk and set the running flag.
8214752412STakashi Iwai  */
8314752412STakashi Iwai void snd_hdac_stream_start(struct hdac_stream *azx_dev, bool fresh_start)
8414752412STakashi Iwai {
8514752412STakashi Iwai 	struct hdac_bus *bus = azx_dev->bus;
869b6f7e7aSSameer Pujar 	int stripe_ctl;
8714752412STakashi Iwai 
88598dfb56SLibin Yang 	trace_snd_hdac_stream_start(bus, azx_dev);
89598dfb56SLibin Yang 
9014752412STakashi Iwai 	azx_dev->start_wallclk = snd_hdac_chip_readl(bus, WALLCLK);
9114752412STakashi Iwai 	if (!fresh_start)
9214752412STakashi Iwai 		azx_dev->start_wallclk -= azx_dev->period_wallclk;
9314752412STakashi Iwai 
9414752412STakashi Iwai 	/* enable SIE */
95fc2a6cf0SKeyon Jie 	snd_hdac_chip_updatel(bus, INTCTL,
96fc2a6cf0SKeyon Jie 			      1 << azx_dev->index,
97fc2a6cf0SKeyon Jie 			      1 << azx_dev->index);
989b6f7e7aSSameer Pujar 	/* set stripe control */
99e38e486dSTakashi Iwai 	if (azx_dev->stripe) {
100d344e079SMariusz Ceier 		if (azx_dev->substream)
1019b6f7e7aSSameer Pujar 			stripe_ctl = snd_hdac_get_stream_stripe_ctl(bus, azx_dev->substream);
102d344e079SMariusz Ceier 		else
103d344e079SMariusz Ceier 			stripe_ctl = 0;
1049b6f7e7aSSameer Pujar 		snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK,
1059b6f7e7aSSameer Pujar 					stripe_ctl);
106e38e486dSTakashi Iwai 	}
10714752412STakashi Iwai 	/* set DMA start and interrupt mask */
10814752412STakashi Iwai 	snd_hdac_stream_updateb(azx_dev, SD_CTL,
10914752412STakashi Iwai 				0, SD_CTL_DMA_START | SD_INT_MASK);
11014752412STakashi Iwai 	azx_dev->running = true;
11114752412STakashi Iwai }
11214752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_start);
11314752412STakashi Iwai 
11414752412STakashi Iwai /**
115*2ea13c83SPierre-Louis Bossart  * snd_hdac_stream_clear - helper to clear stream registers and stop DMA transfers
11614752412STakashi Iwai  * @azx_dev: HD-audio core stream to stop
11714752412STakashi Iwai  */
118*2ea13c83SPierre-Louis Bossart static void snd_hdac_stream_clear(struct hdac_stream *azx_dev)
11914752412STakashi Iwai {
12014752412STakashi Iwai 	snd_hdac_stream_updateb(azx_dev, SD_CTL,
12114752412STakashi Iwai 				SD_CTL_DMA_START | SD_INT_MASK, 0);
12214752412STakashi Iwai 	snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
1236fd739c0STakashi Iwai 	if (azx_dev->stripe)
1249b6f7e7aSSameer Pujar 		snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK, 0);
12514752412STakashi Iwai 	azx_dev->running = false;
12614752412STakashi Iwai }
12714752412STakashi Iwai 
12814752412STakashi Iwai /**
12914752412STakashi Iwai  * snd_hdac_stream_stop - stop a stream
13014752412STakashi Iwai  * @azx_dev: HD-audio core stream to stop
13114752412STakashi Iwai  *
13214752412STakashi Iwai  * Stop a stream DMA and disable stream interrupt
13314752412STakashi Iwai  */
13414752412STakashi Iwai void snd_hdac_stream_stop(struct hdac_stream *azx_dev)
13514752412STakashi Iwai {
136598dfb56SLibin Yang 	trace_snd_hdac_stream_stop(azx_dev->bus, azx_dev);
137598dfb56SLibin Yang 
13814752412STakashi Iwai 	snd_hdac_stream_clear(azx_dev);
13914752412STakashi Iwai 	/* disable SIE */
14014752412STakashi Iwai 	snd_hdac_chip_updatel(azx_dev->bus, INTCTL, 1 << azx_dev->index, 0);
14114752412STakashi Iwai }
14214752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_stop);
14314752412STakashi Iwai 
14414752412STakashi Iwai /**
14512054f0cSPierre-Louis Bossart  * snd_hdac_stop_streams_and_chip - stop all streams and chip if running
14612054f0cSPierre-Louis Bossart  * @bus: HD-audio core bus
14712054f0cSPierre-Louis Bossart  */
14812054f0cSPierre-Louis Bossart void snd_hdac_stop_streams_and_chip(struct hdac_bus *bus)
14912054f0cSPierre-Louis Bossart {
15012054f0cSPierre-Louis Bossart 	struct hdac_stream *stream;
15112054f0cSPierre-Louis Bossart 
15212054f0cSPierre-Louis Bossart 	if (bus->chip_init) {
15312054f0cSPierre-Louis Bossart 		list_for_each_entry(stream, &bus->stream_list, list)
15412054f0cSPierre-Louis Bossart 			snd_hdac_stream_stop(stream);
15512054f0cSPierre-Louis Bossart 		snd_hdac_bus_stop_chip(bus);
15612054f0cSPierre-Louis Bossart 	}
15712054f0cSPierre-Louis Bossart }
15812054f0cSPierre-Louis Bossart EXPORT_SYMBOL_GPL(snd_hdac_stop_streams_and_chip);
15912054f0cSPierre-Louis Bossart 
16012054f0cSPierre-Louis Bossart /**
16114752412STakashi Iwai  * snd_hdac_stream_reset - reset a stream
16214752412STakashi Iwai  * @azx_dev: HD-audio core stream to reset
16314752412STakashi Iwai  */
16414752412STakashi Iwai void snd_hdac_stream_reset(struct hdac_stream *azx_dev)
16514752412STakashi Iwai {
16614752412STakashi Iwai 	unsigned char val;
1674106820bSMohan Kumar 	int dma_run_state;
16814752412STakashi Iwai 
16914752412STakashi Iwai 	snd_hdac_stream_clear(azx_dev);
17014752412STakashi Iwai 
1714106820bSMohan Kumar 	dma_run_state = snd_hdac_stream_readb(azx_dev, SD_CTL) & SD_CTL_DMA_START;
1724106820bSMohan Kumar 
17314752412STakashi Iwai 	snd_hdac_stream_updateb(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET);
174d9185705SAmadeusz Sławiński 
175d9185705SAmadeusz Sławiński 	/* wait for hardware to report that the stream entered reset */
176d9185705SAmadeusz Sławiński 	snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, (val & SD_CTL_STREAM_RESET), 3, 300);
1774106820bSMohan Kumar 
1784106820bSMohan Kumar 	if (azx_dev->bus->dma_stop_delay && dma_run_state)
1794106820bSMohan Kumar 		udelay(azx_dev->bus->dma_stop_delay);
1804106820bSMohan Kumar 
181d9185705SAmadeusz Sławiński 	snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_CTL_STREAM_RESET, 0);
18214752412STakashi Iwai 
183d9185705SAmadeusz Sławiński 	/* wait for hardware to report that the stream is out of reset */
184d9185705SAmadeusz Sławiński 	snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, !(val & SD_CTL_STREAM_RESET), 3, 300);
18514752412STakashi Iwai 
18614752412STakashi Iwai 	/* reset first position - may not be synced with hw at this time */
18714752412STakashi Iwai 	if (azx_dev->posbuf)
18814752412STakashi Iwai 		*azx_dev->posbuf = 0;
18914752412STakashi Iwai }
19014752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_reset);
19114752412STakashi Iwai 
19214752412STakashi Iwai /**
19314752412STakashi Iwai  * snd_hdac_stream_setup -  set up the SD for streaming
19414752412STakashi Iwai  * @azx_dev: HD-audio core stream to set up
19514752412STakashi Iwai  */
19614752412STakashi Iwai int snd_hdac_stream_setup(struct hdac_stream *azx_dev)
19714752412STakashi Iwai {
19814752412STakashi Iwai 	struct hdac_bus *bus = azx_dev->bus;
1994214c534STakashi Iwai 	struct snd_pcm_runtime *runtime;
20014752412STakashi Iwai 	unsigned int val;
20114752412STakashi Iwai 
2024214c534STakashi Iwai 	if (azx_dev->substream)
2034214c534STakashi Iwai 		runtime = azx_dev->substream->runtime;
2044214c534STakashi Iwai 	else
2054214c534STakashi Iwai 		runtime = NULL;
20614752412STakashi Iwai 	/* make sure the run bit is zero for SD */
20714752412STakashi Iwai 	snd_hdac_stream_clear(azx_dev);
20814752412STakashi Iwai 	/* program the stream_tag */
20914752412STakashi Iwai 	val = snd_hdac_stream_readl(azx_dev, SD_CTL);
21014752412STakashi Iwai 	val = (val & ~SD_CTL_STREAM_TAG_MASK) |
21114752412STakashi Iwai 		(azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
21214752412STakashi Iwai 	if (!bus->snoop)
21314752412STakashi Iwai 		val |= SD_CTL_TRAFFIC_PRIO;
21414752412STakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_CTL, val);
21514752412STakashi Iwai 
21614752412STakashi Iwai 	/* program the length of samples in cyclic buffer */
21714752412STakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_CBL, azx_dev->bufsize);
21814752412STakashi Iwai 
21914752412STakashi Iwai 	/* program the stream format */
22014752412STakashi Iwai 	/* this value needs to be the same as the one programmed */
22114752412STakashi Iwai 	snd_hdac_stream_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
22214752412STakashi Iwai 
22314752412STakashi Iwai 	/* program the stream LVI (last valid index) of the BDL */
22414752412STakashi Iwai 	snd_hdac_stream_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
22514752412STakashi Iwai 
22614752412STakashi Iwai 	/* program the BDL address */
22714752412STakashi Iwai 	/* lower BDL address */
22814752412STakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
22914752412STakashi Iwai 	/* upper BDL address */
23014752412STakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_BDLPU,
23114752412STakashi Iwai 			       upper_32_bits(azx_dev->bdl.addr));
23214752412STakashi Iwai 
23314752412STakashi Iwai 	/* enable the position buffer */
23414752412STakashi Iwai 	if (bus->use_posbuf && bus->posbuf.addr) {
23514752412STakashi Iwai 		if (!(snd_hdac_chip_readl(bus, DPLBASE) & AZX_DPLBASE_ENABLE))
23614752412STakashi Iwai 			snd_hdac_chip_writel(bus, DPLBASE,
23714752412STakashi Iwai 				(u32)bus->posbuf.addr | AZX_DPLBASE_ENABLE);
23814752412STakashi Iwai 	}
23914752412STakashi Iwai 
24014752412STakashi Iwai 	/* set the interrupt enable bits in the descriptor control register */
24114752412STakashi Iwai 	snd_hdac_stream_updatel(azx_dev, SD_CTL, 0, SD_INT_MASK);
24214752412STakashi Iwai 
2437da20788STakashi Iwai 	azx_dev->fifo_size = snd_hdac_stream_readw(azx_dev, SD_FIFOSIZE) + 1;
24414752412STakashi Iwai 
24514752412STakashi Iwai 	/* when LPIB delay correction gives a small negative value,
24614752412STakashi Iwai 	 * we ignore it; currently set the threshold statically to
24714752412STakashi Iwai 	 * 64 frames
24814752412STakashi Iwai 	 */
2494214c534STakashi Iwai 	if (runtime && runtime->period_size > 64)
25014752412STakashi Iwai 		azx_dev->delay_negative_threshold =
25114752412STakashi Iwai 			-frames_to_bytes(runtime, 64);
25214752412STakashi Iwai 	else
25314752412STakashi Iwai 		azx_dev->delay_negative_threshold = 0;
25414752412STakashi Iwai 
25514752412STakashi Iwai 	/* wallclk has 24Mhz clock source */
2564214c534STakashi Iwai 	if (runtime)
25714752412STakashi Iwai 		azx_dev->period_wallclk = (((runtime->period_size * 24000) /
25814752412STakashi Iwai 				    runtime->rate) * 1000);
25914752412STakashi Iwai 
26014752412STakashi Iwai 	return 0;
26114752412STakashi Iwai }
26214752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_setup);
26314752412STakashi Iwai 
26414752412STakashi Iwai /**
26514752412STakashi Iwai  * snd_hdac_stream_cleanup - cleanup a stream
26614752412STakashi Iwai  * @azx_dev: HD-audio core stream to clean up
26714752412STakashi Iwai  */
26814752412STakashi Iwai void snd_hdac_stream_cleanup(struct hdac_stream *azx_dev)
26914752412STakashi Iwai {
27014752412STakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
27114752412STakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
27214752412STakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
27314752412STakashi Iwai 	azx_dev->bufsize = 0;
27414752412STakashi Iwai 	azx_dev->period_bytes = 0;
27514752412STakashi Iwai 	azx_dev->format_val = 0;
27614752412STakashi Iwai }
27714752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_cleanup);
27814752412STakashi Iwai 
27914752412STakashi Iwai /**
28014752412STakashi Iwai  * snd_hdac_stream_assign - assign a stream for the PCM
28114752412STakashi Iwai  * @bus: HD-audio core bus
28214752412STakashi Iwai  * @substream: PCM substream to assign
28314752412STakashi Iwai  *
28414752412STakashi Iwai  * Look for an unused stream for the given PCM substream, assign it
28514752412STakashi Iwai  * and return the stream object.  If no stream is free, returns NULL.
28614752412STakashi Iwai  * The function tries to keep using the same stream object when it's used
28714752412STakashi Iwai  * beforehand.  Also, when bus->reverse_assign flag is set, the last free
28814752412STakashi Iwai  * or matching entry is returned.  This is needed for some strange codecs.
28914752412STakashi Iwai  */
29014752412STakashi Iwai struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus,
29114752412STakashi Iwai 					   struct snd_pcm_substream *substream)
29214752412STakashi Iwai {
29314752412STakashi Iwai 	struct hdac_stream *azx_dev;
29414752412STakashi Iwai 	struct hdac_stream *res = NULL;
29514752412STakashi Iwai 
29614752412STakashi Iwai 	/* make a non-zero unique key for the substream */
29714752412STakashi Iwai 	int key = (substream->pcm->device << 16) | (substream->number << 2) |
29814752412STakashi Iwai 		(substream->stream + 1);
29914752412STakashi Iwai 
3001465d06aSPierre-Louis Bossart 	spin_lock_irq(&bus->reg_lock);
30114752412STakashi Iwai 	list_for_each_entry(azx_dev, &bus->stream_list, list) {
30214752412STakashi Iwai 		if (azx_dev->direction != substream->stream)
30314752412STakashi Iwai 			continue;
30414752412STakashi Iwai 		if (azx_dev->opened)
30514752412STakashi Iwai 			continue;
30614752412STakashi Iwai 		if (azx_dev->assigned_key == key) {
30714752412STakashi Iwai 			res = azx_dev;
30814752412STakashi Iwai 			break;
30914752412STakashi Iwai 		}
31014752412STakashi Iwai 		if (!res || bus->reverse_assign)
31114752412STakashi Iwai 			res = azx_dev;
31214752412STakashi Iwai 	}
31314752412STakashi Iwai 	if (res) {
31414752412STakashi Iwai 		res->opened = 1;
31514752412STakashi Iwai 		res->running = 0;
31614752412STakashi Iwai 		res->assigned_key = key;
31714752412STakashi Iwai 		res->substream = substream;
31814752412STakashi Iwai 	}
3191465d06aSPierre-Louis Bossart 	spin_unlock_irq(&bus->reg_lock);
32014752412STakashi Iwai 	return res;
32114752412STakashi Iwai }
32214752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_assign);
32314752412STakashi Iwai 
32414752412STakashi Iwai /**
32514752412STakashi Iwai  * snd_hdac_stream_release - release the assigned stream
32614752412STakashi Iwai  * @azx_dev: HD-audio core stream to release
32714752412STakashi Iwai  *
32814752412STakashi Iwai  * Release the stream that has been assigned by snd_hdac_stream_assign().
32914752412STakashi Iwai  */
33014752412STakashi Iwai void snd_hdac_stream_release(struct hdac_stream *azx_dev)
33114752412STakashi Iwai {
33214752412STakashi Iwai 	struct hdac_bus *bus = azx_dev->bus;
33314752412STakashi Iwai 
33414752412STakashi Iwai 	spin_lock_irq(&bus->reg_lock);
33514752412STakashi Iwai 	azx_dev->opened = 0;
33614752412STakashi Iwai 	azx_dev->running = 0;
33714752412STakashi Iwai 	azx_dev->substream = NULL;
33814752412STakashi Iwai 	spin_unlock_irq(&bus->reg_lock);
33914752412STakashi Iwai }
34014752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_release);
34114752412STakashi Iwai 
3424308c9b0SJeeja KP /**
3434308c9b0SJeeja KP  * snd_hdac_get_stream - return hdac_stream based on stream_tag and
3444308c9b0SJeeja KP  * direction
3454308c9b0SJeeja KP  *
3464308c9b0SJeeja KP  * @bus: HD-audio core bus
3474308c9b0SJeeja KP  * @dir: direction for the stream to be found
3484308c9b0SJeeja KP  * @stream_tag: stream tag for stream to be found
3494308c9b0SJeeja KP  */
3504308c9b0SJeeja KP struct hdac_stream *snd_hdac_get_stream(struct hdac_bus *bus,
3514308c9b0SJeeja KP 					int dir, int stream_tag)
3524308c9b0SJeeja KP {
3534308c9b0SJeeja KP 	struct hdac_stream *s;
3544308c9b0SJeeja KP 
3554308c9b0SJeeja KP 	list_for_each_entry(s, &bus->stream_list, list) {
3564308c9b0SJeeja KP 		if (s->direction == dir && s->stream_tag == stream_tag)
3574308c9b0SJeeja KP 			return s;
3584308c9b0SJeeja KP 	}
3594308c9b0SJeeja KP 
3604308c9b0SJeeja KP 	return NULL;
3614308c9b0SJeeja KP }
3624308c9b0SJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_get_stream);
3634308c9b0SJeeja KP 
36414752412STakashi Iwai /*
36514752412STakashi Iwai  * set up a BDL entry
36614752412STakashi Iwai  */
36714752412STakashi Iwai static int setup_bdle(struct hdac_bus *bus,
36814752412STakashi Iwai 		      struct snd_dma_buffer *dmab,
36914752412STakashi Iwai 		      struct hdac_stream *azx_dev, __le32 **bdlp,
37014752412STakashi Iwai 		      int ofs, int size, int with_ioc)
37114752412STakashi Iwai {
37214752412STakashi Iwai 	__le32 *bdl = *bdlp;
37314752412STakashi Iwai 
37414752412STakashi Iwai 	while (size > 0) {
37514752412STakashi Iwai 		dma_addr_t addr;
37614752412STakashi Iwai 		int chunk;
37714752412STakashi Iwai 
37814752412STakashi Iwai 		if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
37914752412STakashi Iwai 			return -EINVAL;
38014752412STakashi Iwai 
38114752412STakashi Iwai 		addr = snd_sgbuf_get_addr(dmab, ofs);
38214752412STakashi Iwai 		/* program the address field of the BDL entry */
38314752412STakashi Iwai 		bdl[0] = cpu_to_le32((u32)addr);
38414752412STakashi Iwai 		bdl[1] = cpu_to_le32(upper_32_bits(addr));
38514752412STakashi Iwai 		/* program the size field of the BDL entry */
38614752412STakashi Iwai 		chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
38714752412STakashi Iwai 		/* one BDLE cannot cross 4K boundary on CTHDA chips */
38814752412STakashi Iwai 		if (bus->align_bdle_4k) {
38914752412STakashi Iwai 			u32 remain = 0x1000 - (ofs & 0xfff);
39014752412STakashi Iwai 
39114752412STakashi Iwai 			if (chunk > remain)
39214752412STakashi Iwai 				chunk = remain;
39314752412STakashi Iwai 		}
39414752412STakashi Iwai 		bdl[2] = cpu_to_le32(chunk);
39514752412STakashi Iwai 		/* program the IOC to enable interrupt
39614752412STakashi Iwai 		 * only when the whole fragment is processed
39714752412STakashi Iwai 		 */
39814752412STakashi Iwai 		size -= chunk;
39914752412STakashi Iwai 		bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
40014752412STakashi Iwai 		bdl += 4;
40114752412STakashi Iwai 		azx_dev->frags++;
40214752412STakashi Iwai 		ofs += chunk;
40314752412STakashi Iwai 	}
40414752412STakashi Iwai 	*bdlp = bdl;
40514752412STakashi Iwai 	return ofs;
40614752412STakashi Iwai }
40714752412STakashi Iwai 
40814752412STakashi Iwai /**
40914752412STakashi Iwai  * snd_hdac_stream_setup_periods - set up BDL entries
41014752412STakashi Iwai  * @azx_dev: HD-audio core stream to set up
41114752412STakashi Iwai  *
41214752412STakashi Iwai  * Set up the buffer descriptor table of the given stream based on the
41314752412STakashi Iwai  * period and buffer sizes of the assigned PCM substream.
41414752412STakashi Iwai  */
41514752412STakashi Iwai int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev)
41614752412STakashi Iwai {
41714752412STakashi Iwai 	struct hdac_bus *bus = azx_dev->bus;
41814752412STakashi Iwai 	struct snd_pcm_substream *substream = azx_dev->substream;
41914752412STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
42014752412STakashi Iwai 	__le32 *bdl;
42114752412STakashi Iwai 	int i, ofs, periods, period_bytes;
42214752412STakashi Iwai 	int pos_adj, pos_align;
42314752412STakashi Iwai 
42414752412STakashi Iwai 	/* reset BDL address */
42514752412STakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
42614752412STakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
42714752412STakashi Iwai 
42814752412STakashi Iwai 	period_bytes = azx_dev->period_bytes;
42914752412STakashi Iwai 	periods = azx_dev->bufsize / period_bytes;
43014752412STakashi Iwai 
43114752412STakashi Iwai 	/* program the initial BDL entries */
43214752412STakashi Iwai 	bdl = (__le32 *)azx_dev->bdl.area;
43314752412STakashi Iwai 	ofs = 0;
43414752412STakashi Iwai 	azx_dev->frags = 0;
43514752412STakashi Iwai 
43614752412STakashi Iwai 	pos_adj = bus->bdl_pos_adj;
43714752412STakashi Iwai 	if (!azx_dev->no_period_wakeup && pos_adj > 0) {
43814752412STakashi Iwai 		pos_align = pos_adj;
43981d0ec43SLars-Peter Clausen 		pos_adj = DIV_ROUND_UP(pos_adj * runtime->rate, 48000);
44014752412STakashi Iwai 		if (!pos_adj)
44114752412STakashi Iwai 			pos_adj = pos_align;
44214752412STakashi Iwai 		else
44381d0ec43SLars-Peter Clausen 			pos_adj = roundup(pos_adj, pos_align);
44414752412STakashi Iwai 		pos_adj = frames_to_bytes(runtime, pos_adj);
44514752412STakashi Iwai 		if (pos_adj >= period_bytes) {
44614752412STakashi Iwai 			dev_warn(bus->dev, "Too big adjustment %d\n",
44714752412STakashi Iwai 				 pos_adj);
44814752412STakashi Iwai 			pos_adj = 0;
44914752412STakashi Iwai 		} else {
45014752412STakashi Iwai 			ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
45114752412STakashi Iwai 					 azx_dev,
45214752412STakashi Iwai 					 &bdl, ofs, pos_adj, true);
45314752412STakashi Iwai 			if (ofs < 0)
45414752412STakashi Iwai 				goto error;
45514752412STakashi Iwai 		}
45614752412STakashi Iwai 	} else
45714752412STakashi Iwai 		pos_adj = 0;
45814752412STakashi Iwai 
45914752412STakashi Iwai 	for (i = 0; i < periods; i++) {
46014752412STakashi Iwai 		if (i == periods - 1 && pos_adj)
46114752412STakashi Iwai 			ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
46214752412STakashi Iwai 					 azx_dev, &bdl, ofs,
46314752412STakashi Iwai 					 period_bytes - pos_adj, 0);
46414752412STakashi Iwai 		else
46514752412STakashi Iwai 			ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
46614752412STakashi Iwai 					 azx_dev, &bdl, ofs,
46714752412STakashi Iwai 					 period_bytes,
46814752412STakashi Iwai 					 !azx_dev->no_period_wakeup);
46914752412STakashi Iwai 		if (ofs < 0)
47014752412STakashi Iwai 			goto error;
47114752412STakashi Iwai 	}
47214752412STakashi Iwai 	return 0;
47314752412STakashi Iwai 
47414752412STakashi Iwai  error:
47514752412STakashi Iwai 	dev_err(bus->dev, "Too many BDL entries: buffer=%d, period=%d\n",
47614752412STakashi Iwai 		azx_dev->bufsize, period_bytes);
47714752412STakashi Iwai 	return -EINVAL;
47814752412STakashi Iwai }
47914752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_setup_periods);
48014752412STakashi Iwai 
48178dd5e21STakashi Iwai /**
48278dd5e21STakashi Iwai  * snd_hdac_stream_set_params - set stream parameters
48386f6501bSJeeja KP  * @azx_dev: HD-audio core stream for which parameters are to be set
48486f6501bSJeeja KP  * @format_val: format value parameter
48586f6501bSJeeja KP  *
48686f6501bSJeeja KP  * Setup the HD-audio core stream parameters from substream of the stream
48786f6501bSJeeja KP  * and passed format value
48886f6501bSJeeja KP  */
48986f6501bSJeeja KP int snd_hdac_stream_set_params(struct hdac_stream *azx_dev,
49086f6501bSJeeja KP 				 unsigned int format_val)
49186f6501bSJeeja KP {
49286f6501bSJeeja KP 
49386f6501bSJeeja KP 	unsigned int bufsize, period_bytes;
49486f6501bSJeeja KP 	struct snd_pcm_substream *substream = azx_dev->substream;
49586f6501bSJeeja KP 	struct snd_pcm_runtime *runtime;
49686f6501bSJeeja KP 	int err;
49786f6501bSJeeja KP 
49886f6501bSJeeja KP 	if (!substream)
49986f6501bSJeeja KP 		return -EINVAL;
50086f6501bSJeeja KP 	runtime = substream->runtime;
50186f6501bSJeeja KP 	bufsize = snd_pcm_lib_buffer_bytes(substream);
50286f6501bSJeeja KP 	period_bytes = snd_pcm_lib_period_bytes(substream);
50386f6501bSJeeja KP 
50486f6501bSJeeja KP 	if (bufsize != azx_dev->bufsize ||
50586f6501bSJeeja KP 	    period_bytes != azx_dev->period_bytes ||
50686f6501bSJeeja KP 	    format_val != azx_dev->format_val ||
50786f6501bSJeeja KP 	    runtime->no_period_wakeup != azx_dev->no_period_wakeup) {
50886f6501bSJeeja KP 		azx_dev->bufsize = bufsize;
50986f6501bSJeeja KP 		azx_dev->period_bytes = period_bytes;
51086f6501bSJeeja KP 		azx_dev->format_val = format_val;
51186f6501bSJeeja KP 		azx_dev->no_period_wakeup = runtime->no_period_wakeup;
51286f6501bSJeeja KP 		err = snd_hdac_stream_setup_periods(azx_dev);
51386f6501bSJeeja KP 		if (err < 0)
51486f6501bSJeeja KP 			return err;
51586f6501bSJeeja KP 	}
51686f6501bSJeeja KP 	return 0;
51786f6501bSJeeja KP }
51886f6501bSJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_stream_set_params);
51986f6501bSJeeja KP 
520a5a1d1c2SThomas Gleixner static u64 azx_cc_read(const struct cyclecounter *cc)
52114752412STakashi Iwai {
52214752412STakashi Iwai 	struct hdac_stream *azx_dev = container_of(cc, struct hdac_stream, cc);
52314752412STakashi Iwai 
52414752412STakashi Iwai 	return snd_hdac_chip_readl(azx_dev->bus, WALLCLK);
52514752412STakashi Iwai }
52614752412STakashi Iwai 
52714752412STakashi Iwai static void azx_timecounter_init(struct hdac_stream *azx_dev,
528a5a1d1c2SThomas Gleixner 				 bool force, u64 last)
52914752412STakashi Iwai {
53014752412STakashi Iwai 	struct timecounter *tc = &azx_dev->tc;
53114752412STakashi Iwai 	struct cyclecounter *cc = &azx_dev->cc;
53214752412STakashi Iwai 	u64 nsec;
53314752412STakashi Iwai 
53414752412STakashi Iwai 	cc->read = azx_cc_read;
53514752412STakashi Iwai 	cc->mask = CLOCKSOURCE_MASK(32);
53614752412STakashi Iwai 
53714752412STakashi Iwai 	/*
5386dd21ad8SThomas Gleixner 	 * Calculate the optimal mult/shift values. The counter wraps
5396dd21ad8SThomas Gleixner 	 * around after ~178.9 seconds.
54014752412STakashi Iwai 	 */
5416dd21ad8SThomas Gleixner 	clocks_calc_mult_shift(&cc->mult, &cc->shift, 24000000,
5426dd21ad8SThomas Gleixner 			       NSEC_PER_SEC, 178);
54314752412STakashi Iwai 
54414752412STakashi Iwai 	nsec = 0; /* audio time is elapsed time since trigger */
54514752412STakashi Iwai 	timecounter_init(tc, cc, nsec);
54614752412STakashi Iwai 	if (force) {
54714752412STakashi Iwai 		/*
54814752412STakashi Iwai 		 * force timecounter to use predefined value,
54914752412STakashi Iwai 		 * used for synchronized starts
55014752412STakashi Iwai 		 */
55114752412STakashi Iwai 		tc->cycle_last = last;
55214752412STakashi Iwai 	}
55314752412STakashi Iwai }
55414752412STakashi Iwai 
55514752412STakashi Iwai /**
55614752412STakashi Iwai  * snd_hdac_stream_timecounter_init - initialize time counter
55714752412STakashi Iwai  * @azx_dev: HD-audio core stream (master stream)
55814752412STakashi Iwai  * @streams: bit flags of streams to set up
55914752412STakashi Iwai  *
56014752412STakashi Iwai  * Initializes the time counter of streams marked by the bit flags (each
56114752412STakashi Iwai  * bit corresponds to the stream index).
56214752412STakashi Iwai  * The trigger timestamp of PCM substream assigned to the given stream is
56314752412STakashi Iwai  * updated accordingly, too.
56414752412STakashi Iwai  */
56514752412STakashi Iwai void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev,
56614752412STakashi Iwai 				      unsigned int streams)
56714752412STakashi Iwai {
56814752412STakashi Iwai 	struct hdac_bus *bus = azx_dev->bus;
56914752412STakashi Iwai 	struct snd_pcm_runtime *runtime = azx_dev->substream->runtime;
57014752412STakashi Iwai 	struct hdac_stream *s;
57114752412STakashi Iwai 	bool inited = false;
572a5a1d1c2SThomas Gleixner 	u64 cycle_last = 0;
57314752412STakashi Iwai 	int i = 0;
57414752412STakashi Iwai 
57514752412STakashi Iwai 	list_for_each_entry(s, &bus->stream_list, list) {
57614752412STakashi Iwai 		if (streams & (1 << i)) {
57714752412STakashi Iwai 			azx_timecounter_init(s, inited, cycle_last);
57814752412STakashi Iwai 			if (!inited) {
57914752412STakashi Iwai 				inited = true;
58014752412STakashi Iwai 				cycle_last = s->tc.cycle_last;
58114752412STakashi Iwai 			}
58214752412STakashi Iwai 		}
58314752412STakashi Iwai 		i++;
58414752412STakashi Iwai 	}
58514752412STakashi Iwai 
58614752412STakashi Iwai 	snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
58714752412STakashi Iwai 	runtime->trigger_tstamp_latched = true;
58814752412STakashi Iwai }
58914752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_timecounter_init);
59014752412STakashi Iwai 
59114752412STakashi Iwai /**
59214752412STakashi Iwai  * snd_hdac_stream_sync_trigger - turn on/off stream sync register
59314752412STakashi Iwai  * @azx_dev: HD-audio core stream (master stream)
5946e57188fSKeyon Jie  * @set: true = set, false = clear
59514752412STakashi Iwai  * @streams: bit flags of streams to sync
5966e57188fSKeyon Jie  * @reg: the stream sync register address
59714752412STakashi Iwai  */
59814752412STakashi Iwai void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set,
59914752412STakashi Iwai 				  unsigned int streams, unsigned int reg)
60014752412STakashi Iwai {
60114752412STakashi Iwai 	struct hdac_bus *bus = azx_dev->bus;
60214752412STakashi Iwai 	unsigned int val;
60314752412STakashi Iwai 
60414752412STakashi Iwai 	if (!reg)
60514752412STakashi Iwai 		reg = AZX_REG_SSYNC;
6062c1f8138STakashi Iwai 	val = _snd_hdac_chip_readl(bus, reg);
60714752412STakashi Iwai 	if (set)
60814752412STakashi Iwai 		val |= streams;
60914752412STakashi Iwai 	else
61014752412STakashi Iwai 		val &= ~streams;
6112c1f8138STakashi Iwai 	_snd_hdac_chip_writel(bus, reg, val);
61214752412STakashi Iwai }
61314752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_sync_trigger);
61414752412STakashi Iwai 
61514752412STakashi Iwai /**
6168518c648Shuangjianghui  * snd_hdac_stream_sync - sync with start/stop trigger operation
61714752412STakashi Iwai  * @azx_dev: HD-audio core stream (master stream)
61814752412STakashi Iwai  * @start: true = start, false = stop
61914752412STakashi Iwai  * @streams: bit flags of streams to sync
62014752412STakashi Iwai  *
62114752412STakashi Iwai  * For @start = true, wait until all FIFOs get ready.
62214752412STakashi Iwai  * For @start = false, wait until all RUN bits are cleared.
62314752412STakashi Iwai  */
62414752412STakashi Iwai void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start,
62514752412STakashi Iwai 			  unsigned int streams)
62614752412STakashi Iwai {
62714752412STakashi Iwai 	struct hdac_bus *bus = azx_dev->bus;
62814752412STakashi Iwai 	int i, nwait, timeout;
62914752412STakashi Iwai 	struct hdac_stream *s;
63014752412STakashi Iwai 
63114752412STakashi Iwai 	for (timeout = 5000; timeout; timeout--) {
63214752412STakashi Iwai 		nwait = 0;
63314752412STakashi Iwai 		i = 0;
63414752412STakashi Iwai 		list_for_each_entry(s, &bus->stream_list, list) {
6357faa26c1SMohan Kumar 			if (!(streams & (1 << i++)))
6367faa26c1SMohan Kumar 				continue;
6377faa26c1SMohan Kumar 
63814752412STakashi Iwai 			if (start) {
63914752412STakashi Iwai 				/* check FIFO gets ready */
64014752412STakashi Iwai 				if (!(snd_hdac_stream_readb(s, SD_STS) &
64114752412STakashi Iwai 				      SD_STS_FIFO_READY))
64214752412STakashi Iwai 					nwait++;
64314752412STakashi Iwai 			} else {
64414752412STakashi Iwai 				/* check RUN bit is cleared */
64514752412STakashi Iwai 				if (snd_hdac_stream_readb(s, SD_CTL) &
6467faa26c1SMohan Kumar 				    SD_CTL_DMA_START) {
64714752412STakashi Iwai 					nwait++;
6487faa26c1SMohan Kumar 					/*
6497faa26c1SMohan Kumar 					 * Perform stream reset if DMA RUN
6507faa26c1SMohan Kumar 					 * bit not cleared within given timeout
6517faa26c1SMohan Kumar 					 */
6527faa26c1SMohan Kumar 					if (timeout == 1)
6537faa26c1SMohan Kumar 						snd_hdac_stream_reset(s);
65414752412STakashi Iwai 				}
65514752412STakashi Iwai 			}
65614752412STakashi Iwai 		}
65714752412STakashi Iwai 		if (!nwait)
65814752412STakashi Iwai 			break;
65914752412STakashi Iwai 		cpu_relax();
66014752412STakashi Iwai 	}
66114752412STakashi Iwai }
66214752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_sync);
6638f3f600bSTakashi Iwai 
6648f3f600bSTakashi Iwai #ifdef CONFIG_SND_HDA_DSP_LOADER
6658f3f600bSTakashi Iwai /**
6668f3f600bSTakashi Iwai  * snd_hdac_dsp_prepare - prepare for DSP loading
6678f3f600bSTakashi Iwai  * @azx_dev: HD-audio core stream used for DSP loading
6688f3f600bSTakashi Iwai  * @format: HD-audio stream format
6698f3f600bSTakashi Iwai  * @byte_size: data chunk byte size
6708f3f600bSTakashi Iwai  * @bufp: allocated buffer
6718f3f600bSTakashi Iwai  *
6728f3f600bSTakashi Iwai  * Allocate the buffer for the given size and set up the given stream for
6738f3f600bSTakashi Iwai  * DSP loading.  Returns the stream tag (>= 0), or a negative error code.
6748f3f600bSTakashi Iwai  */
6758f3f600bSTakashi Iwai int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
6768f3f600bSTakashi Iwai 			 unsigned int byte_size, struct snd_dma_buffer *bufp)
6778f3f600bSTakashi Iwai {
6788f3f600bSTakashi Iwai 	struct hdac_bus *bus = azx_dev->bus;
6797362b0fcSTakashi Iwai 	__le32 *bdl;
6808f3f600bSTakashi Iwai 	int err;
6818f3f600bSTakashi Iwai 
6828f3f600bSTakashi Iwai 	snd_hdac_dsp_lock(azx_dev);
6838f3f600bSTakashi Iwai 	spin_lock_irq(&bus->reg_lock);
6848f3f600bSTakashi Iwai 	if (azx_dev->running || azx_dev->locked) {
6858f3f600bSTakashi Iwai 		spin_unlock_irq(&bus->reg_lock);
6868f3f600bSTakashi Iwai 		err = -EBUSY;
6878f3f600bSTakashi Iwai 		goto unlock;
6888f3f600bSTakashi Iwai 	}
6898f3f600bSTakashi Iwai 	azx_dev->locked = true;
6908f3f600bSTakashi Iwai 	spin_unlock_irq(&bus->reg_lock);
6918f3f600bSTakashi Iwai 
692619a1f19STakashi Iwai 	err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, bus->dev,
6938f3f600bSTakashi Iwai 				  byte_size, bufp);
6948f3f600bSTakashi Iwai 	if (err < 0)
6958f3f600bSTakashi Iwai 		goto err_alloc;
6968f3f600bSTakashi Iwai 
6974214c534STakashi Iwai 	azx_dev->substream = NULL;
6988f3f600bSTakashi Iwai 	azx_dev->bufsize = byte_size;
6998f3f600bSTakashi Iwai 	azx_dev->period_bytes = byte_size;
7008f3f600bSTakashi Iwai 	azx_dev->format_val = format;
7018f3f600bSTakashi Iwai 
7028f3f600bSTakashi Iwai 	snd_hdac_stream_reset(azx_dev);
7038f3f600bSTakashi Iwai 
7048f3f600bSTakashi Iwai 	/* reset BDL address */
7058f3f600bSTakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
7068f3f600bSTakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
7078f3f600bSTakashi Iwai 
7088f3f600bSTakashi Iwai 	azx_dev->frags = 0;
7097362b0fcSTakashi Iwai 	bdl = (__le32 *)azx_dev->bdl.area;
7108f3f600bSTakashi Iwai 	err = setup_bdle(bus, bufp, azx_dev, &bdl, 0, byte_size, 0);
7118f3f600bSTakashi Iwai 	if (err < 0)
7128f3f600bSTakashi Iwai 		goto error;
7138f3f600bSTakashi Iwai 
7148f3f600bSTakashi Iwai 	snd_hdac_stream_setup(azx_dev);
7158f3f600bSTakashi Iwai 	snd_hdac_dsp_unlock(azx_dev);
7168f3f600bSTakashi Iwai 	return azx_dev->stream_tag;
7178f3f600bSTakashi Iwai 
7188f3f600bSTakashi Iwai  error:
719619a1f19STakashi Iwai 	snd_dma_free_pages(bufp);
7208f3f600bSTakashi Iwai  err_alloc:
7218f3f600bSTakashi Iwai 	spin_lock_irq(&bus->reg_lock);
7228f3f600bSTakashi Iwai 	azx_dev->locked = false;
7238f3f600bSTakashi Iwai 	spin_unlock_irq(&bus->reg_lock);
7248f3f600bSTakashi Iwai  unlock:
7258f3f600bSTakashi Iwai 	snd_hdac_dsp_unlock(azx_dev);
7268f3f600bSTakashi Iwai 	return err;
7278f3f600bSTakashi Iwai }
7288f3f600bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare);
7298f3f600bSTakashi Iwai 
7308f3f600bSTakashi Iwai /**
7318f3f600bSTakashi Iwai  * snd_hdac_dsp_trigger - start / stop DSP loading
7328f3f600bSTakashi Iwai  * @azx_dev: HD-audio core stream used for DSP loading
7338f3f600bSTakashi Iwai  * @start: trigger start or stop
7348f3f600bSTakashi Iwai  */
7358f3f600bSTakashi Iwai void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start)
7368f3f600bSTakashi Iwai {
7378f3f600bSTakashi Iwai 	if (start)
7388f3f600bSTakashi Iwai 		snd_hdac_stream_start(azx_dev, true);
7398f3f600bSTakashi Iwai 	else
7408f3f600bSTakashi Iwai 		snd_hdac_stream_stop(azx_dev);
7418f3f600bSTakashi Iwai }
7428f3f600bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_dsp_trigger);
7438f3f600bSTakashi Iwai 
7448f3f600bSTakashi Iwai /**
7458f3f600bSTakashi Iwai  * snd_hdac_dsp_cleanup - clean up the stream from DSP loading to normal
7468f3f600bSTakashi Iwai  * @azx_dev: HD-audio core stream used for DSP loading
7478f3f600bSTakashi Iwai  * @dmab: buffer used by DSP loading
7488f3f600bSTakashi Iwai  */
7498f3f600bSTakashi Iwai void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
7508f3f600bSTakashi Iwai 			  struct snd_dma_buffer *dmab)
7518f3f600bSTakashi Iwai {
7528f3f600bSTakashi Iwai 	struct hdac_bus *bus = azx_dev->bus;
7538f3f600bSTakashi Iwai 
7548f3f600bSTakashi Iwai 	if (!dmab->area || !azx_dev->locked)
7558f3f600bSTakashi Iwai 		return;
7568f3f600bSTakashi Iwai 
7578f3f600bSTakashi Iwai 	snd_hdac_dsp_lock(azx_dev);
7588f3f600bSTakashi Iwai 	/* reset BDL address */
7598f3f600bSTakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
7608f3f600bSTakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
7618f3f600bSTakashi Iwai 	snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
7628f3f600bSTakashi Iwai 	azx_dev->bufsize = 0;
7638f3f600bSTakashi Iwai 	azx_dev->period_bytes = 0;
7648f3f600bSTakashi Iwai 	azx_dev->format_val = 0;
7658f3f600bSTakashi Iwai 
766619a1f19STakashi Iwai 	snd_dma_free_pages(dmab);
7678f3f600bSTakashi Iwai 	dmab->area = NULL;
7688f3f600bSTakashi Iwai 
7698f3f600bSTakashi Iwai 	spin_lock_irq(&bus->reg_lock);
7708f3f600bSTakashi Iwai 	azx_dev->locked = false;
7718f3f600bSTakashi Iwai 	spin_unlock_irq(&bus->reg_lock);
7728f3f600bSTakashi Iwai 	snd_hdac_dsp_unlock(azx_dev);
7738f3f600bSTakashi Iwai }
7748f3f600bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup);
7758f3f600bSTakashi Iwai #endif /* CONFIG_SND_HDA_DSP_LOADER */
776