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>
103e958226SCezary Rojewski #include <sound/compress_driver.h>
1114752412STakashi Iwai #include <sound/core.h>
1214752412STakashi Iwai #include <sound/pcm.h>
1314752412STakashi Iwai #include <sound/hdaudio.h>
1414752412STakashi Iwai #include <sound/hda_register.h>
15598dfb56SLibin Yang #include "trace.h"
1614752412STakashi Iwai
17ea2ddd25SPierre-Louis Bossart /*
18ea2ddd25SPierre-Louis Bossart * the hdac_stream library is intended to be used with the following
19ea2ddd25SPierre-Louis Bossart * transitions. The states are not formally defined in the code but loosely
20ea2ddd25SPierre-Louis Bossart * inspired by boolean variables. Note that the 'prepared' field is not used
21ea2ddd25SPierre-Louis Bossart * in this library but by the callers during the hw_params/prepare transitions
22ea2ddd25SPierre-Louis Bossart *
23ea2ddd25SPierre-Louis Bossart * |
24ea2ddd25SPierre-Louis Bossart * stream_init() |
25ea2ddd25SPierre-Louis Bossart * v
26ea2ddd25SPierre-Louis Bossart * +--+-------+
27ea2ddd25SPierre-Louis Bossart * | unused |
28ea2ddd25SPierre-Louis Bossart * +--+----+--+
29ea2ddd25SPierre-Louis Bossart * | ^
30ea2ddd25SPierre-Louis Bossart * stream_assign() | | stream_release()
31ea2ddd25SPierre-Louis Bossart * v |
32ea2ddd25SPierre-Louis Bossart * +--+----+--+
33ea2ddd25SPierre-Louis Bossart * | opened |
34ea2ddd25SPierre-Louis Bossart * +--+----+--+
35ea2ddd25SPierre-Louis Bossart * | ^
36ea2ddd25SPierre-Louis Bossart * stream_reset() | |
37ea2ddd25SPierre-Louis Bossart * stream_setup() | | stream_cleanup()
38ea2ddd25SPierre-Louis Bossart * v |
39ea2ddd25SPierre-Louis Bossart * +--+----+--+
40ea2ddd25SPierre-Louis Bossart * | prepared |
41ea2ddd25SPierre-Louis Bossart * +--+----+--+
42ea2ddd25SPierre-Louis Bossart * | ^
43ea2ddd25SPierre-Louis Bossart * stream_start() | | stream_stop()
44ea2ddd25SPierre-Louis Bossart * v |
45ea2ddd25SPierre-Louis Bossart * +--+----+--+
46ea2ddd25SPierre-Louis Bossart * | running |
47ea2ddd25SPierre-Louis Bossart * +----------+
48ea2ddd25SPierre-Louis Bossart */
49ea2ddd25SPierre-Louis Bossart
5014752412STakashi Iwai /**
515dd3d271SSameer Pujar * snd_hdac_get_stream_stripe_ctl - get stripe control value
525dd3d271SSameer Pujar * @bus: HD-audio core bus
535dd3d271SSameer Pujar * @substream: PCM substream
545dd3d271SSameer Pujar */
snd_hdac_get_stream_stripe_ctl(struct hdac_bus * bus,struct snd_pcm_substream * substream)555dd3d271SSameer Pujar int snd_hdac_get_stream_stripe_ctl(struct hdac_bus *bus,
565dd3d271SSameer Pujar struct snd_pcm_substream *substream)
575dd3d271SSameer Pujar {
585dd3d271SSameer Pujar struct snd_pcm_runtime *runtime = substream->runtime;
595dd3d271SSameer Pujar unsigned int channels = runtime->channels,
605dd3d271SSameer Pujar rate = runtime->rate,
615dd3d271SSameer Pujar bits_per_sample = runtime->sample_bits,
625dd3d271SSameer Pujar max_sdo_lines, value, sdo_line;
635dd3d271SSameer Pujar
645dd3d271SSameer Pujar /* T_AZA_GCAP_NSDO is 1:2 bitfields in GCAP */
655dd3d271SSameer Pujar max_sdo_lines = snd_hdac_chip_readl(bus, GCAP) & AZX_GCAP_NSDO;
665dd3d271SSameer Pujar
675dd3d271SSameer Pujar /* following is from HD audio spec */
685dd3d271SSameer Pujar for (sdo_line = max_sdo_lines; sdo_line > 0; sdo_line >>= 1) {
695dd3d271SSameer Pujar if (rate > 48000)
705dd3d271SSameer Pujar value = (channels * bits_per_sample *
715dd3d271SSameer Pujar (rate / 48000)) / sdo_line;
725dd3d271SSameer Pujar else
735dd3d271SSameer Pujar value = (channels * bits_per_sample) / sdo_line;
745dd3d271SSameer Pujar
7567ae482aSSameer Pujar if (value >= bus->sdo_limit)
765dd3d271SSameer Pujar break;
775dd3d271SSameer Pujar }
785dd3d271SSameer Pujar
795dd3d271SSameer Pujar /* stripe value: 0 for 1SDO, 1 for 2SDO, 2 for 4SDO lines */
805dd3d271SSameer Pujar return sdo_line >> 1;
815dd3d271SSameer Pujar }
825dd3d271SSameer Pujar EXPORT_SYMBOL_GPL(snd_hdac_get_stream_stripe_ctl);
835dd3d271SSameer Pujar
845dd3d271SSameer Pujar /**
8514752412STakashi Iwai * snd_hdac_stream_init - initialize each stream (aka device)
8614752412STakashi Iwai * @bus: HD-audio core bus
8714752412STakashi Iwai * @azx_dev: HD-audio core stream object to initialize
8814752412STakashi Iwai * @idx: stream index number
8914752412STakashi Iwai * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE)
9014752412STakashi Iwai * @tag: the tag id to assign
9114752412STakashi Iwai *
9214752412STakashi Iwai * Assign the starting bdl address to each stream (device) and initialize.
9314752412STakashi Iwai */
snd_hdac_stream_init(struct hdac_bus * bus,struct hdac_stream * azx_dev,int idx,int direction,int tag)9414752412STakashi Iwai void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev,
9514752412STakashi Iwai int idx, int direction, int tag)
9614752412STakashi Iwai {
9714752412STakashi Iwai azx_dev->bus = bus;
9814752412STakashi Iwai /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
9914752412STakashi Iwai azx_dev->sd_addr = bus->remap_addr + (0x20 * idx + 0x80);
10014752412STakashi Iwai /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
10114752412STakashi Iwai azx_dev->sd_int_sta_mask = 1 << idx;
10214752412STakashi Iwai azx_dev->index = idx;
10314752412STakashi Iwai azx_dev->direction = direction;
10414752412STakashi Iwai azx_dev->stream_tag = tag;
1058f3f600bSTakashi Iwai snd_hdac_dsp_lock_init(azx_dev);
10614752412STakashi Iwai list_add_tail(&azx_dev->list, &bus->stream_list);
10762582341SPierre-Louis Bossart
10862582341SPierre-Louis Bossart if (bus->spbcap) {
10962582341SPierre-Louis Bossart azx_dev->spib_addr = bus->spbcap + AZX_SPB_BASE +
11062582341SPierre-Louis Bossart AZX_SPB_INTERVAL * idx +
11162582341SPierre-Louis Bossart AZX_SPB_SPIB;
11262582341SPierre-Louis Bossart
11362582341SPierre-Louis Bossart azx_dev->fifo_addr = bus->spbcap + AZX_SPB_BASE +
11462582341SPierre-Louis Bossart AZX_SPB_INTERVAL * idx +
11562582341SPierre-Louis Bossart AZX_SPB_MAXFIFO;
11662582341SPierre-Louis Bossart }
11762582341SPierre-Louis Bossart
11862582341SPierre-Louis Bossart if (bus->drsmcap)
11962582341SPierre-Louis Bossart azx_dev->dpibr_addr = bus->drsmcap + AZX_DRSM_BASE +
12062582341SPierre-Louis Bossart AZX_DRSM_INTERVAL * idx;
12114752412STakashi Iwai }
12214752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_init);
12314752412STakashi Iwai
12414752412STakashi Iwai /**
12514752412STakashi Iwai * snd_hdac_stream_start - start a stream
12614752412STakashi Iwai * @azx_dev: HD-audio core stream to start
12714752412STakashi Iwai *
12814752412STakashi Iwai * Start a stream, set start_wallclk and set the running flag.
12914752412STakashi Iwai */
snd_hdac_stream_start(struct hdac_stream * azx_dev)1304fe20d62SZhang Yiqun void snd_hdac_stream_start(struct hdac_stream *azx_dev)
13114752412STakashi Iwai {
13214752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus;
1339b6f7e7aSSameer Pujar int stripe_ctl;
13414752412STakashi Iwai
135598dfb56SLibin Yang trace_snd_hdac_stream_start(bus, azx_dev);
136598dfb56SLibin Yang
13714752412STakashi Iwai azx_dev->start_wallclk = snd_hdac_chip_readl(bus, WALLCLK);
13814752412STakashi Iwai
13914752412STakashi Iwai /* enable SIE */
140fc2a6cf0SKeyon Jie snd_hdac_chip_updatel(bus, INTCTL,
141fc2a6cf0SKeyon Jie 1 << azx_dev->index,
142fc2a6cf0SKeyon Jie 1 << azx_dev->index);
1439b6f7e7aSSameer Pujar /* set stripe control */
144e38e486dSTakashi Iwai if (azx_dev->stripe) {
145d344e079SMariusz Ceier if (azx_dev->substream)
1469b6f7e7aSSameer Pujar stripe_ctl = snd_hdac_get_stream_stripe_ctl(bus, azx_dev->substream);
147d344e079SMariusz Ceier else
148d344e079SMariusz Ceier stripe_ctl = 0;
1499b6f7e7aSSameer Pujar snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK,
1509b6f7e7aSSameer Pujar stripe_ctl);
151e38e486dSTakashi Iwai }
15214752412STakashi Iwai /* set DMA start and interrupt mask */
153942ccdd8SYanteng Si if (bus->access_sdnctl_in_dword)
154942ccdd8SYanteng Si snd_hdac_stream_updatel(azx_dev, SD_CTL,
155942ccdd8SYanteng Si 0, SD_CTL_DMA_START | SD_INT_MASK);
156942ccdd8SYanteng Si else
15714752412STakashi Iwai snd_hdac_stream_updateb(azx_dev, SD_CTL,
15814752412STakashi Iwai 0, SD_CTL_DMA_START | SD_INT_MASK);
15914752412STakashi Iwai azx_dev->running = true;
16014752412STakashi Iwai }
16114752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_start);
16214752412STakashi Iwai
16314752412STakashi Iwai /**
1642ea13c83SPierre-Louis Bossart * snd_hdac_stream_clear - helper to clear stream registers and stop DMA transfers
16514752412STakashi Iwai * @azx_dev: HD-audio core stream to stop
16614752412STakashi Iwai */
snd_hdac_stream_clear(struct hdac_stream * azx_dev)1672ea13c83SPierre-Louis Bossart static void snd_hdac_stream_clear(struct hdac_stream *azx_dev)
16814752412STakashi Iwai {
16914752412STakashi Iwai snd_hdac_stream_updateb(azx_dev, SD_CTL,
17014752412STakashi Iwai SD_CTL_DMA_START | SD_INT_MASK, 0);
17114752412STakashi Iwai snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
1726fd739c0STakashi Iwai if (azx_dev->stripe)
1739b6f7e7aSSameer Pujar snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK, 0);
17414752412STakashi Iwai azx_dev->running = false;
17514752412STakashi Iwai }
17614752412STakashi Iwai
17714752412STakashi Iwai /**
17814752412STakashi Iwai * snd_hdac_stream_stop - stop a stream
17914752412STakashi Iwai * @azx_dev: HD-audio core stream to stop
18014752412STakashi Iwai *
18114752412STakashi Iwai * Stop a stream DMA and disable stream interrupt
18214752412STakashi Iwai */
snd_hdac_stream_stop(struct hdac_stream * azx_dev)18314752412STakashi Iwai void snd_hdac_stream_stop(struct hdac_stream *azx_dev)
18414752412STakashi Iwai {
185598dfb56SLibin Yang trace_snd_hdac_stream_stop(azx_dev->bus, azx_dev);
186598dfb56SLibin Yang
18714752412STakashi Iwai snd_hdac_stream_clear(azx_dev);
18814752412STakashi Iwai /* disable SIE */
18914752412STakashi Iwai snd_hdac_chip_updatel(azx_dev->bus, INTCTL, 1 << azx_dev->index, 0);
19014752412STakashi Iwai }
19114752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_stop);
19214752412STakashi Iwai
19314752412STakashi Iwai /**
19424ad3835SPierre-Louis Bossart * snd_hdac_stop_streams - stop all streams
19524ad3835SPierre-Louis Bossart * @bus: HD-audio core bus
19624ad3835SPierre-Louis Bossart */
snd_hdac_stop_streams(struct hdac_bus * bus)19724ad3835SPierre-Louis Bossart void snd_hdac_stop_streams(struct hdac_bus *bus)
19824ad3835SPierre-Louis Bossart {
19924ad3835SPierre-Louis Bossart struct hdac_stream *stream;
20024ad3835SPierre-Louis Bossart
20124ad3835SPierre-Louis Bossart list_for_each_entry(stream, &bus->stream_list, list)
20224ad3835SPierre-Louis Bossart snd_hdac_stream_stop(stream);
20324ad3835SPierre-Louis Bossart }
20424ad3835SPierre-Louis Bossart EXPORT_SYMBOL_GPL(snd_hdac_stop_streams);
20524ad3835SPierre-Louis Bossart
20624ad3835SPierre-Louis Bossart /**
20712054f0cSPierre-Louis Bossart * snd_hdac_stop_streams_and_chip - stop all streams and chip if running
20812054f0cSPierre-Louis Bossart * @bus: HD-audio core bus
20912054f0cSPierre-Louis Bossart */
snd_hdac_stop_streams_and_chip(struct hdac_bus * bus)21012054f0cSPierre-Louis Bossart void snd_hdac_stop_streams_and_chip(struct hdac_bus *bus)
21112054f0cSPierre-Louis Bossart {
21212054f0cSPierre-Louis Bossart
21312054f0cSPierre-Louis Bossart if (bus->chip_init) {
21424ad3835SPierre-Louis Bossart snd_hdac_stop_streams(bus);
21512054f0cSPierre-Louis Bossart snd_hdac_bus_stop_chip(bus);
21612054f0cSPierre-Louis Bossart }
21712054f0cSPierre-Louis Bossart }
21812054f0cSPierre-Louis Bossart EXPORT_SYMBOL_GPL(snd_hdac_stop_streams_and_chip);
21912054f0cSPierre-Louis Bossart
22012054f0cSPierre-Louis Bossart /**
22114752412STakashi Iwai * snd_hdac_stream_reset - reset a stream
22214752412STakashi Iwai * @azx_dev: HD-audio core stream to reset
22314752412STakashi Iwai */
snd_hdac_stream_reset(struct hdac_stream * azx_dev)22414752412STakashi Iwai void snd_hdac_stream_reset(struct hdac_stream *azx_dev)
22514752412STakashi Iwai {
22614752412STakashi Iwai unsigned char val;
2274106820bSMohan Kumar int dma_run_state;
22814752412STakashi Iwai
22914752412STakashi Iwai snd_hdac_stream_clear(azx_dev);
23014752412STakashi Iwai
2314106820bSMohan Kumar dma_run_state = snd_hdac_stream_readb(azx_dev, SD_CTL) & SD_CTL_DMA_START;
2324106820bSMohan Kumar
23314752412STakashi Iwai snd_hdac_stream_updateb(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET);
234d9185705SAmadeusz Sławiński
235d9185705SAmadeusz Sławiński /* wait for hardware to report that the stream entered reset */
236d9185705SAmadeusz Sławiński snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, (val & SD_CTL_STREAM_RESET), 3, 300);
2374106820bSMohan Kumar
2384106820bSMohan Kumar if (azx_dev->bus->dma_stop_delay && dma_run_state)
2394106820bSMohan Kumar udelay(azx_dev->bus->dma_stop_delay);
2404106820bSMohan Kumar
241d9185705SAmadeusz Sławiński snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_CTL_STREAM_RESET, 0);
24214752412STakashi Iwai
243d9185705SAmadeusz Sławiński /* wait for hardware to report that the stream is out of reset */
244d9185705SAmadeusz Sławiński snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, !(val & SD_CTL_STREAM_RESET), 3, 300);
24514752412STakashi Iwai
24614752412STakashi Iwai /* reset first position - may not be synced with hw at this time */
24714752412STakashi Iwai if (azx_dev->posbuf)
24814752412STakashi Iwai *azx_dev->posbuf = 0;
24914752412STakashi Iwai }
25014752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_reset);
25114752412STakashi Iwai
25214752412STakashi Iwai /**
25314752412STakashi Iwai * snd_hdac_stream_setup - set up the SD for streaming
25414752412STakashi Iwai * @azx_dev: HD-audio core stream to set up
25514752412STakashi Iwai */
snd_hdac_stream_setup(struct hdac_stream * azx_dev)25614752412STakashi Iwai int snd_hdac_stream_setup(struct hdac_stream *azx_dev)
25714752412STakashi Iwai {
25814752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus;
2594214c534STakashi Iwai struct snd_pcm_runtime *runtime;
26014752412STakashi Iwai unsigned int val;
26114752412STakashi Iwai
2624214c534STakashi Iwai if (azx_dev->substream)
2634214c534STakashi Iwai runtime = azx_dev->substream->runtime;
2644214c534STakashi Iwai else
2654214c534STakashi Iwai runtime = NULL;
26614752412STakashi Iwai /* make sure the run bit is zero for SD */
26714752412STakashi Iwai snd_hdac_stream_clear(azx_dev);
26814752412STakashi Iwai /* program the stream_tag */
26914752412STakashi Iwai val = snd_hdac_stream_readl(azx_dev, SD_CTL);
27014752412STakashi Iwai val = (val & ~SD_CTL_STREAM_TAG_MASK) |
27114752412STakashi Iwai (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
27214752412STakashi Iwai if (!bus->snoop)
27314752412STakashi Iwai val |= SD_CTL_TRAFFIC_PRIO;
27414752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_CTL, val);
27514752412STakashi Iwai
27614752412STakashi Iwai /* program the length of samples in cyclic buffer */
27714752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_CBL, azx_dev->bufsize);
27814752412STakashi Iwai
27914752412STakashi Iwai /* program the stream format */
28014752412STakashi Iwai /* this value needs to be the same as the one programmed */
28114752412STakashi Iwai snd_hdac_stream_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
28214752412STakashi Iwai
28314752412STakashi Iwai /* program the stream LVI (last valid index) of the BDL */
28414752412STakashi Iwai snd_hdac_stream_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
28514752412STakashi Iwai
28614752412STakashi Iwai /* program the BDL address */
28714752412STakashi Iwai /* lower BDL address */
28814752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
28914752412STakashi Iwai /* upper BDL address */
29014752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU,
29114752412STakashi Iwai upper_32_bits(azx_dev->bdl.addr));
29214752412STakashi Iwai
29314752412STakashi Iwai /* enable the position buffer */
29414752412STakashi Iwai if (bus->use_posbuf && bus->posbuf.addr) {
29514752412STakashi Iwai if (!(snd_hdac_chip_readl(bus, DPLBASE) & AZX_DPLBASE_ENABLE))
29614752412STakashi Iwai snd_hdac_chip_writel(bus, DPLBASE,
29714752412STakashi Iwai (u32)bus->posbuf.addr | AZX_DPLBASE_ENABLE);
29814752412STakashi Iwai }
29914752412STakashi Iwai
30014752412STakashi Iwai /* set the interrupt enable bits in the descriptor control register */
30114752412STakashi Iwai snd_hdac_stream_updatel(azx_dev, SD_CTL, 0, SD_INT_MASK);
30214752412STakashi Iwai
3037da20788STakashi Iwai azx_dev->fifo_size = snd_hdac_stream_readw(azx_dev, SD_FIFOSIZE) + 1;
30414752412STakashi Iwai
30514752412STakashi Iwai /* when LPIB delay correction gives a small negative value,
30614752412STakashi Iwai * we ignore it; currently set the threshold statically to
30714752412STakashi Iwai * 64 frames
30814752412STakashi Iwai */
3094214c534STakashi Iwai if (runtime && runtime->period_size > 64)
31014752412STakashi Iwai azx_dev->delay_negative_threshold =
31114752412STakashi Iwai -frames_to_bytes(runtime, 64);
31214752412STakashi Iwai else
31314752412STakashi Iwai azx_dev->delay_negative_threshold = 0;
31414752412STakashi Iwai
31514752412STakashi Iwai /* wallclk has 24Mhz clock source */
3164214c534STakashi Iwai if (runtime)
31714752412STakashi Iwai azx_dev->period_wallclk = (((runtime->period_size * 24000) /
31814752412STakashi Iwai runtime->rate) * 1000);
31914752412STakashi Iwai
32014752412STakashi Iwai return 0;
32114752412STakashi Iwai }
32214752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_setup);
32314752412STakashi Iwai
32414752412STakashi Iwai /**
32514752412STakashi Iwai * snd_hdac_stream_cleanup - cleanup a stream
32614752412STakashi Iwai * @azx_dev: HD-audio core stream to clean up
32714752412STakashi Iwai */
snd_hdac_stream_cleanup(struct hdac_stream * azx_dev)32814752412STakashi Iwai void snd_hdac_stream_cleanup(struct hdac_stream *azx_dev)
32914752412STakashi Iwai {
33014752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
33114752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
33214752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
33314752412STakashi Iwai azx_dev->bufsize = 0;
33414752412STakashi Iwai azx_dev->period_bytes = 0;
33514752412STakashi Iwai azx_dev->format_val = 0;
33614752412STakashi Iwai }
33714752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_cleanup);
33814752412STakashi Iwai
33914752412STakashi Iwai /**
34014752412STakashi Iwai * snd_hdac_stream_assign - assign a stream for the PCM
34114752412STakashi Iwai * @bus: HD-audio core bus
34214752412STakashi Iwai * @substream: PCM substream to assign
34314752412STakashi Iwai *
34414752412STakashi Iwai * Look for an unused stream for the given PCM substream, assign it
34514752412STakashi Iwai * and return the stream object. If no stream is free, returns NULL.
34614752412STakashi Iwai * The function tries to keep using the same stream object when it's used
34714752412STakashi Iwai * beforehand. Also, when bus->reverse_assign flag is set, the last free
34814752412STakashi Iwai * or matching entry is returned. This is needed for some strange codecs.
34914752412STakashi Iwai */
snd_hdac_stream_assign(struct hdac_bus * bus,struct snd_pcm_substream * substream)35014752412STakashi Iwai struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus,
35114752412STakashi Iwai struct snd_pcm_substream *substream)
35214752412STakashi Iwai {
35314752412STakashi Iwai struct hdac_stream *azx_dev;
35414752412STakashi Iwai struct hdac_stream *res = NULL;
35514752412STakashi Iwai
35614752412STakashi Iwai /* make a non-zero unique key for the substream */
3574a320da7SCezary Rojewski int key = (substream->number << 2) | (substream->stream + 1);
3584a320da7SCezary Rojewski
3594a320da7SCezary Rojewski if (substream->pcm)
3604a320da7SCezary Rojewski key |= (substream->pcm->device << 16);
36114752412STakashi Iwai
3621465d06aSPierre-Louis Bossart spin_lock_irq(&bus->reg_lock);
36314752412STakashi Iwai list_for_each_entry(azx_dev, &bus->stream_list, list) {
36414752412STakashi Iwai if (azx_dev->direction != substream->stream)
36514752412STakashi Iwai continue;
36614752412STakashi Iwai if (azx_dev->opened)
36714752412STakashi Iwai continue;
36814752412STakashi Iwai if (azx_dev->assigned_key == key) {
36914752412STakashi Iwai res = azx_dev;
37014752412STakashi Iwai break;
37114752412STakashi Iwai }
37214752412STakashi Iwai if (!res || bus->reverse_assign)
37314752412STakashi Iwai res = azx_dev;
37414752412STakashi Iwai }
37514752412STakashi Iwai if (res) {
37614752412STakashi Iwai res->opened = 1;
37714752412STakashi Iwai res->running = 0;
37814752412STakashi Iwai res->assigned_key = key;
37914752412STakashi Iwai res->substream = substream;
38014752412STakashi Iwai }
3811465d06aSPierre-Louis Bossart spin_unlock_irq(&bus->reg_lock);
38214752412STakashi Iwai return res;
38314752412STakashi Iwai }
38414752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_assign);
38514752412STakashi Iwai
38614752412STakashi Iwai /**
387ac3467adSPierre-Louis Bossart * snd_hdac_stream_release_locked - release the assigned stream
388ac3467adSPierre-Louis Bossart * @azx_dev: HD-audio core stream to release
389ac3467adSPierre-Louis Bossart *
390ac3467adSPierre-Louis Bossart * Release the stream that has been assigned by snd_hdac_stream_assign().
391ac3467adSPierre-Louis Bossart * The bus->reg_lock needs to be taken at a higher level
392ac3467adSPierre-Louis Bossart */
snd_hdac_stream_release_locked(struct hdac_stream * azx_dev)393ac3467adSPierre-Louis Bossart void snd_hdac_stream_release_locked(struct hdac_stream *azx_dev)
394ac3467adSPierre-Louis Bossart {
395ac3467adSPierre-Louis Bossart azx_dev->opened = 0;
396ac3467adSPierre-Louis Bossart azx_dev->running = 0;
397ac3467adSPierre-Louis Bossart azx_dev->substream = NULL;
398ac3467adSPierre-Louis Bossart }
399ac3467adSPierre-Louis Bossart EXPORT_SYMBOL_GPL(snd_hdac_stream_release_locked);
400ac3467adSPierre-Louis Bossart
401ac3467adSPierre-Louis Bossart /**
40214752412STakashi Iwai * snd_hdac_stream_release - release the assigned stream
40314752412STakashi Iwai * @azx_dev: HD-audio core stream to release
40414752412STakashi Iwai *
40514752412STakashi Iwai * Release the stream that has been assigned by snd_hdac_stream_assign().
40614752412STakashi Iwai */
snd_hdac_stream_release(struct hdac_stream * azx_dev)40714752412STakashi Iwai void snd_hdac_stream_release(struct hdac_stream *azx_dev)
40814752412STakashi Iwai {
40914752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus;
41014752412STakashi Iwai
41114752412STakashi Iwai spin_lock_irq(&bus->reg_lock);
412ac3467adSPierre-Louis Bossart snd_hdac_stream_release_locked(azx_dev);
41314752412STakashi Iwai spin_unlock_irq(&bus->reg_lock);
41414752412STakashi Iwai }
41514752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_release);
41614752412STakashi Iwai
4174308c9b0SJeeja KP /**
4184308c9b0SJeeja KP * snd_hdac_get_stream - return hdac_stream based on stream_tag and
4194308c9b0SJeeja KP * direction
4204308c9b0SJeeja KP *
4214308c9b0SJeeja KP * @bus: HD-audio core bus
4224308c9b0SJeeja KP * @dir: direction for the stream to be found
4234308c9b0SJeeja KP * @stream_tag: stream tag for stream to be found
4244308c9b0SJeeja KP */
snd_hdac_get_stream(struct hdac_bus * bus,int dir,int stream_tag)4254308c9b0SJeeja KP struct hdac_stream *snd_hdac_get_stream(struct hdac_bus *bus,
4264308c9b0SJeeja KP int dir, int stream_tag)
4274308c9b0SJeeja KP {
4284308c9b0SJeeja KP struct hdac_stream *s;
4294308c9b0SJeeja KP
4304308c9b0SJeeja KP list_for_each_entry(s, &bus->stream_list, list) {
4314308c9b0SJeeja KP if (s->direction == dir && s->stream_tag == stream_tag)
4324308c9b0SJeeja KP return s;
4334308c9b0SJeeja KP }
4344308c9b0SJeeja KP
4354308c9b0SJeeja KP return NULL;
4364308c9b0SJeeja KP }
4374308c9b0SJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_get_stream);
4384308c9b0SJeeja KP
43914752412STakashi Iwai /*
44014752412STakashi Iwai * set up a BDL entry
44114752412STakashi Iwai */
setup_bdle(struct hdac_bus * bus,struct snd_dma_buffer * dmab,struct hdac_stream * azx_dev,__le32 ** bdlp,int ofs,int size,int with_ioc)44214752412STakashi Iwai static int setup_bdle(struct hdac_bus *bus,
44314752412STakashi Iwai struct snd_dma_buffer *dmab,
44414752412STakashi Iwai struct hdac_stream *azx_dev, __le32 **bdlp,
44514752412STakashi Iwai int ofs, int size, int with_ioc)
44614752412STakashi Iwai {
44714752412STakashi Iwai __le32 *bdl = *bdlp;
44814752412STakashi Iwai
44914752412STakashi Iwai while (size > 0) {
45014752412STakashi Iwai dma_addr_t addr;
45114752412STakashi Iwai int chunk;
45214752412STakashi Iwai
45314752412STakashi Iwai if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
45414752412STakashi Iwai return -EINVAL;
45514752412STakashi Iwai
45614752412STakashi Iwai addr = snd_sgbuf_get_addr(dmab, ofs);
45714752412STakashi Iwai /* program the address field of the BDL entry */
45814752412STakashi Iwai bdl[0] = cpu_to_le32((u32)addr);
45914752412STakashi Iwai bdl[1] = cpu_to_le32(upper_32_bits(addr));
46014752412STakashi Iwai /* program the size field of the BDL entry */
46114752412STakashi Iwai chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
46214752412STakashi Iwai /* one BDLE cannot cross 4K boundary on CTHDA chips */
46314752412STakashi Iwai if (bus->align_bdle_4k) {
46414752412STakashi Iwai u32 remain = 0x1000 - (ofs & 0xfff);
46514752412STakashi Iwai
46614752412STakashi Iwai if (chunk > remain)
46714752412STakashi Iwai chunk = remain;
46814752412STakashi Iwai }
46914752412STakashi Iwai bdl[2] = cpu_to_le32(chunk);
47014752412STakashi Iwai /* program the IOC to enable interrupt
47114752412STakashi Iwai * only when the whole fragment is processed
47214752412STakashi Iwai */
47314752412STakashi Iwai size -= chunk;
47414752412STakashi Iwai bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
47514752412STakashi Iwai bdl += 4;
47614752412STakashi Iwai azx_dev->frags++;
47714752412STakashi Iwai ofs += chunk;
47814752412STakashi Iwai }
47914752412STakashi Iwai *bdlp = bdl;
48014752412STakashi Iwai return ofs;
48114752412STakashi Iwai }
48214752412STakashi Iwai
48314752412STakashi Iwai /**
48414752412STakashi Iwai * snd_hdac_stream_setup_periods - set up BDL entries
48514752412STakashi Iwai * @azx_dev: HD-audio core stream to set up
48614752412STakashi Iwai *
48714752412STakashi Iwai * Set up the buffer descriptor table of the given stream based on the
48814752412STakashi Iwai * period and buffer sizes of the assigned PCM substream.
48914752412STakashi Iwai */
snd_hdac_stream_setup_periods(struct hdac_stream * azx_dev)49014752412STakashi Iwai int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev)
49114752412STakashi Iwai {
49214752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus;
49314752412STakashi Iwai struct snd_pcm_substream *substream = azx_dev->substream;
4943e958226SCezary Rojewski struct snd_compr_stream *cstream = azx_dev->cstream;
4953e958226SCezary Rojewski struct snd_pcm_runtime *runtime = NULL;
496f6b12546SCezary Rojewski struct snd_dma_buffer *dmab;
49714752412STakashi Iwai __le32 *bdl;
49814752412STakashi Iwai int i, ofs, periods, period_bytes;
49914752412STakashi Iwai int pos_adj, pos_align;
50014752412STakashi Iwai
5013e958226SCezary Rojewski if (substream) {
502f6b12546SCezary Rojewski runtime = substream->runtime;
503f6b12546SCezary Rojewski dmab = snd_pcm_get_dma_buf(substream);
5043e958226SCezary Rojewski } else if (cstream) {
5053e958226SCezary Rojewski dmab = snd_pcm_get_dma_buf(cstream);
506084ca216SCezary Rojewski } else {
507084ca216SCezary Rojewski WARN(1, "No substream or cstream assigned\n");
508084ca216SCezary Rojewski return -EINVAL;
5093e958226SCezary Rojewski }
510f6b12546SCezary Rojewski
51114752412STakashi Iwai /* reset BDL address */
51214752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
51314752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
51414752412STakashi Iwai
51514752412STakashi Iwai period_bytes = azx_dev->period_bytes;
51614752412STakashi Iwai periods = azx_dev->bufsize / period_bytes;
51714752412STakashi Iwai
51814752412STakashi Iwai /* program the initial BDL entries */
51914752412STakashi Iwai bdl = (__le32 *)azx_dev->bdl.area;
52014752412STakashi Iwai ofs = 0;
52114752412STakashi Iwai azx_dev->frags = 0;
52214752412STakashi Iwai
52314752412STakashi Iwai pos_adj = bus->bdl_pos_adj;
524f6b12546SCezary Rojewski if (runtime && !azx_dev->no_period_wakeup && pos_adj > 0) {
52514752412STakashi Iwai pos_align = pos_adj;
52681d0ec43SLars-Peter Clausen pos_adj = DIV_ROUND_UP(pos_adj * runtime->rate, 48000);
52714752412STakashi Iwai if (!pos_adj)
52814752412STakashi Iwai pos_adj = pos_align;
52914752412STakashi Iwai else
53081d0ec43SLars-Peter Clausen pos_adj = roundup(pos_adj, pos_align);
53114752412STakashi Iwai pos_adj = frames_to_bytes(runtime, pos_adj);
53214752412STakashi Iwai if (pos_adj >= period_bytes) {
53314752412STakashi Iwai dev_warn(bus->dev, "Too big adjustment %d\n",
53414752412STakashi Iwai pos_adj);
53514752412STakashi Iwai pos_adj = 0;
53614752412STakashi Iwai } else {
537f6b12546SCezary Rojewski ofs = setup_bdle(bus, dmab, azx_dev,
53814752412STakashi Iwai &bdl, ofs, pos_adj, true);
53914752412STakashi Iwai if (ofs < 0)
54014752412STakashi Iwai goto error;
54114752412STakashi Iwai }
54214752412STakashi Iwai } else
54314752412STakashi Iwai pos_adj = 0;
54414752412STakashi Iwai
54514752412STakashi Iwai for (i = 0; i < periods; i++) {
54614752412STakashi Iwai if (i == periods - 1 && pos_adj)
547f6b12546SCezary Rojewski ofs = setup_bdle(bus, dmab, azx_dev,
548f6b12546SCezary Rojewski &bdl, ofs, period_bytes - pos_adj, 0);
54914752412STakashi Iwai else
550f6b12546SCezary Rojewski ofs = setup_bdle(bus, dmab, azx_dev,
551f6b12546SCezary Rojewski &bdl, ofs, period_bytes,
55214752412STakashi Iwai !azx_dev->no_period_wakeup);
55314752412STakashi Iwai if (ofs < 0)
55414752412STakashi Iwai goto error;
55514752412STakashi Iwai }
55614752412STakashi Iwai return 0;
55714752412STakashi Iwai
55814752412STakashi Iwai error:
55914752412STakashi Iwai dev_err(bus->dev, "Too many BDL entries: buffer=%d, period=%d\n",
56014752412STakashi Iwai azx_dev->bufsize, period_bytes);
56114752412STakashi Iwai return -EINVAL;
56214752412STakashi Iwai }
56314752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_setup_periods);
56414752412STakashi Iwai
56578dd5e21STakashi Iwai /**
56678dd5e21STakashi Iwai * snd_hdac_stream_set_params - set stream parameters
56786f6501bSJeeja KP * @azx_dev: HD-audio core stream for which parameters are to be set
56886f6501bSJeeja KP * @format_val: format value parameter
56986f6501bSJeeja KP *
57086f6501bSJeeja KP * Setup the HD-audio core stream parameters from substream of the stream
57186f6501bSJeeja KP * and passed format value
57286f6501bSJeeja KP */
snd_hdac_stream_set_params(struct hdac_stream * azx_dev,unsigned int format_val)57386f6501bSJeeja KP int snd_hdac_stream_set_params(struct hdac_stream *azx_dev,
57486f6501bSJeeja KP unsigned int format_val)
57586f6501bSJeeja KP {
57686f6501bSJeeja KP struct snd_pcm_substream *substream = azx_dev->substream;
5773e958226SCezary Rojewski struct snd_compr_stream *cstream = azx_dev->cstream;
578f6b12546SCezary Rojewski unsigned int bufsize, period_bytes;
579f6b12546SCezary Rojewski unsigned int no_period_wakeup;
58086f6501bSJeeja KP int err;
58186f6501bSJeeja KP
5823e958226SCezary Rojewski if (substream) {
58386f6501bSJeeja KP bufsize = snd_pcm_lib_buffer_bytes(substream);
58486f6501bSJeeja KP period_bytes = snd_pcm_lib_period_bytes(substream);
585f6b12546SCezary Rojewski no_period_wakeup = substream->runtime->no_period_wakeup;
5863e958226SCezary Rojewski } else if (cstream) {
5873e958226SCezary Rojewski bufsize = cstream->runtime->buffer_size;
5883e958226SCezary Rojewski period_bytes = cstream->runtime->fragment_size;
5893e958226SCezary Rojewski no_period_wakeup = 0;
5903e958226SCezary Rojewski } else {
5913e958226SCezary Rojewski return -EINVAL;
5923e958226SCezary Rojewski }
59386f6501bSJeeja KP
59486f6501bSJeeja KP if (bufsize != azx_dev->bufsize ||
59586f6501bSJeeja KP period_bytes != azx_dev->period_bytes ||
59686f6501bSJeeja KP format_val != azx_dev->format_val ||
597f6b12546SCezary Rojewski no_period_wakeup != azx_dev->no_period_wakeup) {
59886f6501bSJeeja KP azx_dev->bufsize = bufsize;
59986f6501bSJeeja KP azx_dev->period_bytes = period_bytes;
60086f6501bSJeeja KP azx_dev->format_val = format_val;
601f6b12546SCezary Rojewski azx_dev->no_period_wakeup = no_period_wakeup;
60286f6501bSJeeja KP err = snd_hdac_stream_setup_periods(azx_dev);
60386f6501bSJeeja KP if (err < 0)
60486f6501bSJeeja KP return err;
60586f6501bSJeeja KP }
60686f6501bSJeeja KP return 0;
60786f6501bSJeeja KP }
60886f6501bSJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_stream_set_params);
60986f6501bSJeeja KP
azx_cc_read(const struct cyclecounter * cc)610a5a1d1c2SThomas Gleixner static u64 azx_cc_read(const struct cyclecounter *cc)
61114752412STakashi Iwai {
61214752412STakashi Iwai struct hdac_stream *azx_dev = container_of(cc, struct hdac_stream, cc);
61314752412STakashi Iwai
61414752412STakashi Iwai return snd_hdac_chip_readl(azx_dev->bus, WALLCLK);
61514752412STakashi Iwai }
61614752412STakashi Iwai
azx_timecounter_init(struct hdac_stream * azx_dev,bool force,u64 last)61714752412STakashi Iwai static void azx_timecounter_init(struct hdac_stream *azx_dev,
618a5a1d1c2SThomas Gleixner bool force, u64 last)
61914752412STakashi Iwai {
62014752412STakashi Iwai struct timecounter *tc = &azx_dev->tc;
62114752412STakashi Iwai struct cyclecounter *cc = &azx_dev->cc;
62214752412STakashi Iwai u64 nsec;
62314752412STakashi Iwai
62414752412STakashi Iwai cc->read = azx_cc_read;
62514752412STakashi Iwai cc->mask = CLOCKSOURCE_MASK(32);
62614752412STakashi Iwai
62714752412STakashi Iwai /*
6286dd21ad8SThomas Gleixner * Calculate the optimal mult/shift values. The counter wraps
6296dd21ad8SThomas Gleixner * around after ~178.9 seconds.
63014752412STakashi Iwai */
6316dd21ad8SThomas Gleixner clocks_calc_mult_shift(&cc->mult, &cc->shift, 24000000,
6326dd21ad8SThomas Gleixner NSEC_PER_SEC, 178);
63314752412STakashi Iwai
63414752412STakashi Iwai nsec = 0; /* audio time is elapsed time since trigger */
63514752412STakashi Iwai timecounter_init(tc, cc, nsec);
63614752412STakashi Iwai if (force) {
63714752412STakashi Iwai /*
63814752412STakashi Iwai * force timecounter to use predefined value,
63914752412STakashi Iwai * used for synchronized starts
64014752412STakashi Iwai */
64114752412STakashi Iwai tc->cycle_last = last;
64214752412STakashi Iwai }
64314752412STakashi Iwai }
64414752412STakashi Iwai
64514752412STakashi Iwai /**
64614752412STakashi Iwai * snd_hdac_stream_timecounter_init - initialize time counter
64714752412STakashi Iwai * @azx_dev: HD-audio core stream (master stream)
64814752412STakashi Iwai * @streams: bit flags of streams to set up
64914752412STakashi Iwai *
65014752412STakashi Iwai * Initializes the time counter of streams marked by the bit flags (each
65114752412STakashi Iwai * bit corresponds to the stream index).
65214752412STakashi Iwai * The trigger timestamp of PCM substream assigned to the given stream is
65314752412STakashi Iwai * updated accordingly, too.
65414752412STakashi Iwai */
snd_hdac_stream_timecounter_init(struct hdac_stream * azx_dev,unsigned int streams)65514752412STakashi Iwai void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev,
65614752412STakashi Iwai unsigned int streams)
65714752412STakashi Iwai {
65814752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus;
65914752412STakashi Iwai struct snd_pcm_runtime *runtime = azx_dev->substream->runtime;
66014752412STakashi Iwai struct hdac_stream *s;
66114752412STakashi Iwai bool inited = false;
662a5a1d1c2SThomas Gleixner u64 cycle_last = 0;
66314752412STakashi Iwai
66414752412STakashi Iwai list_for_each_entry(s, &bus->stream_list, list) {
665*32a600b8STakashi Iwai if ((streams & (1 << s->index))) {
66614752412STakashi Iwai azx_timecounter_init(s, inited, cycle_last);
66714752412STakashi Iwai if (!inited) {
66814752412STakashi Iwai inited = true;
66914752412STakashi Iwai cycle_last = s->tc.cycle_last;
67014752412STakashi Iwai }
67114752412STakashi Iwai }
67214752412STakashi Iwai }
67314752412STakashi Iwai
67414752412STakashi Iwai snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
67514752412STakashi Iwai runtime->trigger_tstamp_latched = true;
67614752412STakashi Iwai }
67714752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_timecounter_init);
67814752412STakashi Iwai
67914752412STakashi Iwai /**
68014752412STakashi Iwai * snd_hdac_stream_sync_trigger - turn on/off stream sync register
68114752412STakashi Iwai * @azx_dev: HD-audio core stream (master stream)
6826e57188fSKeyon Jie * @set: true = set, false = clear
68314752412STakashi Iwai * @streams: bit flags of streams to sync
6846e57188fSKeyon Jie * @reg: the stream sync register address
68514752412STakashi Iwai */
snd_hdac_stream_sync_trigger(struct hdac_stream * azx_dev,bool set,unsigned int streams,unsigned int reg)68614752412STakashi Iwai void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set,
68714752412STakashi Iwai unsigned int streams, unsigned int reg)
68814752412STakashi Iwai {
68914752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus;
69014752412STakashi Iwai unsigned int val;
69114752412STakashi Iwai
69214752412STakashi Iwai if (!reg)
69314752412STakashi Iwai reg = AZX_REG_SSYNC;
6942c1f8138STakashi Iwai val = _snd_hdac_chip_readl(bus, reg);
69514752412STakashi Iwai if (set)
69614752412STakashi Iwai val |= streams;
69714752412STakashi Iwai else
69814752412STakashi Iwai val &= ~streams;
6992c1f8138STakashi Iwai _snd_hdac_chip_writel(bus, reg, val);
70014752412STakashi Iwai }
70114752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_sync_trigger);
70214752412STakashi Iwai
70314752412STakashi Iwai /**
7048518c648Shuangjianghui * snd_hdac_stream_sync - sync with start/stop trigger operation
70514752412STakashi Iwai * @azx_dev: HD-audio core stream (master stream)
70614752412STakashi Iwai * @start: true = start, false = stop
70714752412STakashi Iwai * @streams: bit flags of streams to sync
70814752412STakashi Iwai *
70914752412STakashi Iwai * For @start = true, wait until all FIFOs get ready.
71014752412STakashi Iwai * For @start = false, wait until all RUN bits are cleared.
71114752412STakashi Iwai */
snd_hdac_stream_sync(struct hdac_stream * azx_dev,bool start,unsigned int streams)71214752412STakashi Iwai void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start,
71314752412STakashi Iwai unsigned int streams)
71414752412STakashi Iwai {
71514752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus;
716*32a600b8STakashi Iwai int nwait, timeout;
71714752412STakashi Iwai struct hdac_stream *s;
71814752412STakashi Iwai
71914752412STakashi Iwai for (timeout = 5000; timeout; timeout--) {
72014752412STakashi Iwai nwait = 0;
72114752412STakashi Iwai list_for_each_entry(s, &bus->stream_list, list) {
722*32a600b8STakashi Iwai if (!(streams & (1 << s->index)))
7237faa26c1SMohan Kumar continue;
7247faa26c1SMohan Kumar
72514752412STakashi Iwai if (start) {
72614752412STakashi Iwai /* check FIFO gets ready */
72714752412STakashi Iwai if (!(snd_hdac_stream_readb(s, SD_STS) &
72814752412STakashi Iwai SD_STS_FIFO_READY))
72914752412STakashi Iwai nwait++;
73014752412STakashi Iwai } else {
73114752412STakashi Iwai /* check RUN bit is cleared */
73214752412STakashi Iwai if (snd_hdac_stream_readb(s, SD_CTL) &
7337faa26c1SMohan Kumar SD_CTL_DMA_START) {
73414752412STakashi Iwai nwait++;
7357faa26c1SMohan Kumar /*
7367faa26c1SMohan Kumar * Perform stream reset if DMA RUN
7377faa26c1SMohan Kumar * bit not cleared within given timeout
7387faa26c1SMohan Kumar */
7397faa26c1SMohan Kumar if (timeout == 1)
7407faa26c1SMohan Kumar snd_hdac_stream_reset(s);
74114752412STakashi Iwai }
74214752412STakashi Iwai }
74314752412STakashi Iwai }
74414752412STakashi Iwai if (!nwait)
74514752412STakashi Iwai break;
74614752412STakashi Iwai cpu_relax();
74714752412STakashi Iwai }
74814752412STakashi Iwai }
74914752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_sync);
7508f3f600bSTakashi Iwai
75162582341SPierre-Louis Bossart /**
75262582341SPierre-Louis Bossart * snd_hdac_stream_spbcap_enable - enable SPIB for a stream
75362582341SPierre-Louis Bossart * @bus: HD-audio core bus
75462582341SPierre-Louis Bossart * @enable: flag to enable/disable SPIB
75562582341SPierre-Louis Bossart * @index: stream index for which SPIB need to be enabled
75662582341SPierre-Louis Bossart */
snd_hdac_stream_spbcap_enable(struct hdac_bus * bus,bool enable,int index)75762582341SPierre-Louis Bossart void snd_hdac_stream_spbcap_enable(struct hdac_bus *bus,
75862582341SPierre-Louis Bossart bool enable, int index)
75962582341SPierre-Louis Bossart {
76062582341SPierre-Louis Bossart u32 mask = 0;
76162582341SPierre-Louis Bossart
76262582341SPierre-Louis Bossart if (!bus->spbcap) {
76362582341SPierre-Louis Bossart dev_err(bus->dev, "Address of SPB capability is NULL\n");
76462582341SPierre-Louis Bossart return;
76562582341SPierre-Louis Bossart }
76662582341SPierre-Louis Bossart
76762582341SPierre-Louis Bossart mask |= (1 << index);
76862582341SPierre-Louis Bossart
76962582341SPierre-Louis Bossart if (enable)
77062582341SPierre-Louis Bossart snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, mask);
77162582341SPierre-Louis Bossart else
77262582341SPierre-Louis Bossart snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, 0);
77362582341SPierre-Louis Bossart }
77462582341SPierre-Louis Bossart EXPORT_SYMBOL_GPL(snd_hdac_stream_spbcap_enable);
77562582341SPierre-Louis Bossart
77662582341SPierre-Louis Bossart /**
77762582341SPierre-Louis Bossart * snd_hdac_stream_set_spib - sets the spib value of a stream
77862582341SPierre-Louis Bossart * @bus: HD-audio core bus
77962582341SPierre-Louis Bossart * @azx_dev: hdac_stream
78062582341SPierre-Louis Bossart * @value: spib value to set
78162582341SPierre-Louis Bossart */
snd_hdac_stream_set_spib(struct hdac_bus * bus,struct hdac_stream * azx_dev,u32 value)78262582341SPierre-Louis Bossart int snd_hdac_stream_set_spib(struct hdac_bus *bus,
78362582341SPierre-Louis Bossart struct hdac_stream *azx_dev, u32 value)
78462582341SPierre-Louis Bossart {
78562582341SPierre-Louis Bossart if (!bus->spbcap) {
78662582341SPierre-Louis Bossart dev_err(bus->dev, "Address of SPB capability is NULL\n");
78762582341SPierre-Louis Bossart return -EINVAL;
78862582341SPierre-Louis Bossart }
78962582341SPierre-Louis Bossart
79062582341SPierre-Louis Bossart writel(value, azx_dev->spib_addr);
79162582341SPierre-Louis Bossart
79262582341SPierre-Louis Bossart return 0;
79362582341SPierre-Louis Bossart }
79462582341SPierre-Louis Bossart EXPORT_SYMBOL_GPL(snd_hdac_stream_set_spib);
79562582341SPierre-Louis Bossart
79662582341SPierre-Louis Bossart /**
79762582341SPierre-Louis Bossart * snd_hdac_stream_get_spbmaxfifo - gets the spib value of a stream
79862582341SPierre-Louis Bossart * @bus: HD-audio core bus
79962582341SPierre-Louis Bossart * @azx_dev: hdac_stream
80062582341SPierre-Louis Bossart *
80162582341SPierre-Louis Bossart * Return maxfifo for the stream
80262582341SPierre-Louis Bossart */
snd_hdac_stream_get_spbmaxfifo(struct hdac_bus * bus,struct hdac_stream * azx_dev)80362582341SPierre-Louis Bossart int snd_hdac_stream_get_spbmaxfifo(struct hdac_bus *bus,
80462582341SPierre-Louis Bossart struct hdac_stream *azx_dev)
80562582341SPierre-Louis Bossart {
80662582341SPierre-Louis Bossart if (!bus->spbcap) {
80762582341SPierre-Louis Bossart dev_err(bus->dev, "Address of SPB capability is NULL\n");
80862582341SPierre-Louis Bossart return -EINVAL;
80962582341SPierre-Louis Bossart }
81062582341SPierre-Louis Bossart
81162582341SPierre-Louis Bossart return readl(azx_dev->fifo_addr);
81262582341SPierre-Louis Bossart }
81362582341SPierre-Louis Bossart EXPORT_SYMBOL_GPL(snd_hdac_stream_get_spbmaxfifo);
81462582341SPierre-Louis Bossart
81562582341SPierre-Louis Bossart /**
81662582341SPierre-Louis Bossart * snd_hdac_stream_drsm_enable - enable DMA resume for a stream
81762582341SPierre-Louis Bossart * @bus: HD-audio core bus
81862582341SPierre-Louis Bossart * @enable: flag to enable/disable DRSM
81962582341SPierre-Louis Bossart * @index: stream index for which DRSM need to be enabled
82062582341SPierre-Louis Bossart */
snd_hdac_stream_drsm_enable(struct hdac_bus * bus,bool enable,int index)82162582341SPierre-Louis Bossart void snd_hdac_stream_drsm_enable(struct hdac_bus *bus,
82262582341SPierre-Louis Bossart bool enable, int index)
82362582341SPierre-Louis Bossart {
82462582341SPierre-Louis Bossart u32 mask = 0;
82562582341SPierre-Louis Bossart
82662582341SPierre-Louis Bossart if (!bus->drsmcap) {
82762582341SPierre-Louis Bossart dev_err(bus->dev, "Address of DRSM capability is NULL\n");
82862582341SPierre-Louis Bossart return;
82962582341SPierre-Louis Bossart }
83062582341SPierre-Louis Bossart
83162582341SPierre-Louis Bossart mask |= (1 << index);
83262582341SPierre-Louis Bossart
83362582341SPierre-Louis Bossart if (enable)
83462582341SPierre-Louis Bossart snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, mask);
83562582341SPierre-Louis Bossart else
83662582341SPierre-Louis Bossart snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, 0);
83762582341SPierre-Louis Bossart }
83862582341SPierre-Louis Bossart EXPORT_SYMBOL_GPL(snd_hdac_stream_drsm_enable);
83962582341SPierre-Louis Bossart
840efffb014SCezary Rojewski /*
841efffb014SCezary Rojewski * snd_hdac_stream_wait_drsm - wait for HW to clear RSM for a stream
842efffb014SCezary Rojewski * @azx_dev: HD-audio core stream to await RSM for
843efffb014SCezary Rojewski *
844efffb014SCezary Rojewski * Returns 0 on success and -ETIMEDOUT upon a timeout.
845efffb014SCezary Rojewski */
snd_hdac_stream_wait_drsm(struct hdac_stream * azx_dev)846efffb014SCezary Rojewski int snd_hdac_stream_wait_drsm(struct hdac_stream *azx_dev)
847efffb014SCezary Rojewski {
848efffb014SCezary Rojewski struct hdac_bus *bus = azx_dev->bus;
849efffb014SCezary Rojewski u32 mask, reg;
850efffb014SCezary Rojewski int ret;
851efffb014SCezary Rojewski
852efffb014SCezary Rojewski mask = 1 << azx_dev->index;
853efffb014SCezary Rojewski
854efffb014SCezary Rojewski ret = read_poll_timeout(snd_hdac_reg_readl, reg, !(reg & mask), 250, 2000, false, bus,
855efffb014SCezary Rojewski bus->drsmcap + AZX_REG_DRSM_CTL);
856efffb014SCezary Rojewski if (ret)
857efffb014SCezary Rojewski dev_dbg(bus->dev, "polling RSM 0x%08x failed: %d\n", mask, ret);
858efffb014SCezary Rojewski return ret;
859efffb014SCezary Rojewski }
860efffb014SCezary Rojewski EXPORT_SYMBOL_GPL(snd_hdac_stream_wait_drsm);
861efffb014SCezary Rojewski
86262582341SPierre-Louis Bossart /**
86362582341SPierre-Louis Bossart * snd_hdac_stream_set_dpibr - sets the dpibr value of a stream
86462582341SPierre-Louis Bossart * @bus: HD-audio core bus
86562582341SPierre-Louis Bossart * @azx_dev: hdac_stream
86662582341SPierre-Louis Bossart * @value: dpib value to set
86762582341SPierre-Louis Bossart */
snd_hdac_stream_set_dpibr(struct hdac_bus * bus,struct hdac_stream * azx_dev,u32 value)86862582341SPierre-Louis Bossart int snd_hdac_stream_set_dpibr(struct hdac_bus *bus,
86962582341SPierre-Louis Bossart struct hdac_stream *azx_dev, u32 value)
87062582341SPierre-Louis Bossart {
87162582341SPierre-Louis Bossart if (!bus->drsmcap) {
87262582341SPierre-Louis Bossart dev_err(bus->dev, "Address of DRSM capability is NULL\n");
87362582341SPierre-Louis Bossart return -EINVAL;
87462582341SPierre-Louis Bossart }
87562582341SPierre-Louis Bossart
87662582341SPierre-Louis Bossart writel(value, azx_dev->dpibr_addr);
87762582341SPierre-Louis Bossart
87862582341SPierre-Louis Bossart return 0;
87962582341SPierre-Louis Bossart }
88062582341SPierre-Louis Bossart EXPORT_SYMBOL_GPL(snd_hdac_stream_set_dpibr);
88162582341SPierre-Louis Bossart
88262582341SPierre-Louis Bossart /**
88362582341SPierre-Louis Bossart * snd_hdac_stream_set_lpib - sets the lpib value of a stream
88462582341SPierre-Louis Bossart * @azx_dev: hdac_stream
88562582341SPierre-Louis Bossart * @value: lpib value to set
88662582341SPierre-Louis Bossart */
snd_hdac_stream_set_lpib(struct hdac_stream * azx_dev,u32 value)88762582341SPierre-Louis Bossart int snd_hdac_stream_set_lpib(struct hdac_stream *azx_dev, u32 value)
88862582341SPierre-Louis Bossart {
88962582341SPierre-Louis Bossart snd_hdac_stream_writel(azx_dev, SD_LPIB, value);
89062582341SPierre-Louis Bossart
89162582341SPierre-Louis Bossart return 0;
89262582341SPierre-Louis Bossart }
89362582341SPierre-Louis Bossart EXPORT_SYMBOL_GPL(snd_hdac_stream_set_lpib);
89462582341SPierre-Louis Bossart
8958f3f600bSTakashi Iwai #ifdef CONFIG_SND_HDA_DSP_LOADER
8968f3f600bSTakashi Iwai /**
8978f3f600bSTakashi Iwai * snd_hdac_dsp_prepare - prepare for DSP loading
8988f3f600bSTakashi Iwai * @azx_dev: HD-audio core stream used for DSP loading
8998f3f600bSTakashi Iwai * @format: HD-audio stream format
9008f3f600bSTakashi Iwai * @byte_size: data chunk byte size
9018f3f600bSTakashi Iwai * @bufp: allocated buffer
9028f3f600bSTakashi Iwai *
9038f3f600bSTakashi Iwai * Allocate the buffer for the given size and set up the given stream for
9048f3f600bSTakashi Iwai * DSP loading. Returns the stream tag (>= 0), or a negative error code.
9058f3f600bSTakashi Iwai */
snd_hdac_dsp_prepare(struct hdac_stream * azx_dev,unsigned int format,unsigned int byte_size,struct snd_dma_buffer * bufp)9068f3f600bSTakashi Iwai int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format,
9078f3f600bSTakashi Iwai unsigned int byte_size, struct snd_dma_buffer *bufp)
9088f3f600bSTakashi Iwai {
9098f3f600bSTakashi Iwai struct hdac_bus *bus = azx_dev->bus;
9107362b0fcSTakashi Iwai __le32 *bdl;
9118f3f600bSTakashi Iwai int err;
9128f3f600bSTakashi Iwai
9138f3f600bSTakashi Iwai snd_hdac_dsp_lock(azx_dev);
9148f3f600bSTakashi Iwai spin_lock_irq(&bus->reg_lock);
9158f3f600bSTakashi Iwai if (azx_dev->running || azx_dev->locked) {
9168f3f600bSTakashi Iwai spin_unlock_irq(&bus->reg_lock);
9178f3f600bSTakashi Iwai err = -EBUSY;
9188f3f600bSTakashi Iwai goto unlock;
9198f3f600bSTakashi Iwai }
9208f3f600bSTakashi Iwai azx_dev->locked = true;
9218f3f600bSTakashi Iwai spin_unlock_irq(&bus->reg_lock);
9228f3f600bSTakashi Iwai
923619a1f19STakashi Iwai err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, bus->dev,
9248f3f600bSTakashi Iwai byte_size, bufp);
9258f3f600bSTakashi Iwai if (err < 0)
9268f3f600bSTakashi Iwai goto err_alloc;
9278f3f600bSTakashi Iwai
9284214c534STakashi Iwai azx_dev->substream = NULL;
9298f3f600bSTakashi Iwai azx_dev->bufsize = byte_size;
9308f3f600bSTakashi Iwai azx_dev->period_bytes = byte_size;
9318f3f600bSTakashi Iwai azx_dev->format_val = format;
9328f3f600bSTakashi Iwai
9338f3f600bSTakashi Iwai snd_hdac_stream_reset(azx_dev);
9348f3f600bSTakashi Iwai
9358f3f600bSTakashi Iwai /* reset BDL address */
9368f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
9378f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
9388f3f600bSTakashi Iwai
9398f3f600bSTakashi Iwai azx_dev->frags = 0;
9407362b0fcSTakashi Iwai bdl = (__le32 *)azx_dev->bdl.area;
9418f3f600bSTakashi Iwai err = setup_bdle(bus, bufp, azx_dev, &bdl, 0, byte_size, 0);
9428f3f600bSTakashi Iwai if (err < 0)
9438f3f600bSTakashi Iwai goto error;
9448f3f600bSTakashi Iwai
9458f3f600bSTakashi Iwai snd_hdac_stream_setup(azx_dev);
9468f3f600bSTakashi Iwai snd_hdac_dsp_unlock(azx_dev);
9478f3f600bSTakashi Iwai return azx_dev->stream_tag;
9488f3f600bSTakashi Iwai
9498f3f600bSTakashi Iwai error:
950619a1f19STakashi Iwai snd_dma_free_pages(bufp);
9518f3f600bSTakashi Iwai err_alloc:
9528f3f600bSTakashi Iwai spin_lock_irq(&bus->reg_lock);
9538f3f600bSTakashi Iwai azx_dev->locked = false;
9548f3f600bSTakashi Iwai spin_unlock_irq(&bus->reg_lock);
9558f3f600bSTakashi Iwai unlock:
9568f3f600bSTakashi Iwai snd_hdac_dsp_unlock(azx_dev);
9578f3f600bSTakashi Iwai return err;
9588f3f600bSTakashi Iwai }
9598f3f600bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare);
9608f3f600bSTakashi Iwai
9618f3f600bSTakashi Iwai /**
9628f3f600bSTakashi Iwai * snd_hdac_dsp_trigger - start / stop DSP loading
9638f3f600bSTakashi Iwai * @azx_dev: HD-audio core stream used for DSP loading
9648f3f600bSTakashi Iwai * @start: trigger start or stop
9658f3f600bSTakashi Iwai */
snd_hdac_dsp_trigger(struct hdac_stream * azx_dev,bool start)9668f3f600bSTakashi Iwai void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start)
9678f3f600bSTakashi Iwai {
9688f3f600bSTakashi Iwai if (start)
9694fe20d62SZhang Yiqun snd_hdac_stream_start(azx_dev);
9708f3f600bSTakashi Iwai else
9718f3f600bSTakashi Iwai snd_hdac_stream_stop(azx_dev);
9728f3f600bSTakashi Iwai }
9738f3f600bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_dsp_trigger);
9748f3f600bSTakashi Iwai
9758f3f600bSTakashi Iwai /**
9768f3f600bSTakashi Iwai * snd_hdac_dsp_cleanup - clean up the stream from DSP loading to normal
9778f3f600bSTakashi Iwai * @azx_dev: HD-audio core stream used for DSP loading
9788f3f600bSTakashi Iwai * @dmab: buffer used by DSP loading
9798f3f600bSTakashi Iwai */
snd_hdac_dsp_cleanup(struct hdac_stream * azx_dev,struct snd_dma_buffer * dmab)9808f3f600bSTakashi Iwai void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev,
9818f3f600bSTakashi Iwai struct snd_dma_buffer *dmab)
9828f3f600bSTakashi Iwai {
9838f3f600bSTakashi Iwai struct hdac_bus *bus = azx_dev->bus;
9848f3f600bSTakashi Iwai
9858f3f600bSTakashi Iwai if (!dmab->area || !azx_dev->locked)
9868f3f600bSTakashi Iwai return;
9878f3f600bSTakashi Iwai
9888f3f600bSTakashi Iwai snd_hdac_dsp_lock(azx_dev);
9898f3f600bSTakashi Iwai /* reset BDL address */
9908f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
9918f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
9928f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
9938f3f600bSTakashi Iwai azx_dev->bufsize = 0;
9948f3f600bSTakashi Iwai azx_dev->period_bytes = 0;
9958f3f600bSTakashi Iwai azx_dev->format_val = 0;
9968f3f600bSTakashi Iwai
997619a1f19STakashi Iwai snd_dma_free_pages(dmab);
9988f3f600bSTakashi Iwai dmab->area = NULL;
9998f3f600bSTakashi Iwai
10008f3f600bSTakashi Iwai spin_lock_irq(&bus->reg_lock);
10018f3f600bSTakashi Iwai azx_dev->locked = false;
10028f3f600bSTakashi Iwai spin_unlock_irq(&bus->reg_lock);
10038f3f600bSTakashi Iwai snd_hdac_dsp_unlock(azx_dev);
10048f3f600bSTakashi Iwai }
10058f3f600bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup);
10068f3f600bSTakashi Iwai #endif /* CONFIG_SND_HDA_DSP_LOADER */
1007