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 16ea2ddd25SPierre-Louis Bossart /* 17ea2ddd25SPierre-Louis Bossart * the hdac_stream library is intended to be used with the following 18ea2ddd25SPierre-Louis Bossart * transitions. The states are not formally defined in the code but loosely 19ea2ddd25SPierre-Louis Bossart * inspired by boolean variables. Note that the 'prepared' field is not used 20ea2ddd25SPierre-Louis Bossart * in this library but by the callers during the hw_params/prepare transitions 21ea2ddd25SPierre-Louis Bossart * 22ea2ddd25SPierre-Louis Bossart * | 23ea2ddd25SPierre-Louis Bossart * stream_init() | 24ea2ddd25SPierre-Louis Bossart * v 25ea2ddd25SPierre-Louis Bossart * +--+-------+ 26ea2ddd25SPierre-Louis Bossart * | unused | 27ea2ddd25SPierre-Louis Bossart * +--+----+--+ 28ea2ddd25SPierre-Louis Bossart * | ^ 29ea2ddd25SPierre-Louis Bossart * stream_assign() | | stream_release() 30ea2ddd25SPierre-Louis Bossart * v | 31ea2ddd25SPierre-Louis Bossart * +--+----+--+ 32ea2ddd25SPierre-Louis Bossart * | opened | 33ea2ddd25SPierre-Louis Bossart * +--+----+--+ 34ea2ddd25SPierre-Louis Bossart * | ^ 35ea2ddd25SPierre-Louis Bossart * stream_reset() | | 36ea2ddd25SPierre-Louis Bossart * stream_setup() | | stream_cleanup() 37ea2ddd25SPierre-Louis Bossart * v | 38ea2ddd25SPierre-Louis Bossart * +--+----+--+ 39ea2ddd25SPierre-Louis Bossart * | prepared | 40ea2ddd25SPierre-Louis Bossart * +--+----+--+ 41ea2ddd25SPierre-Louis Bossart * | ^ 42ea2ddd25SPierre-Louis Bossart * stream_start() | | stream_stop() 43ea2ddd25SPierre-Louis Bossart * v | 44ea2ddd25SPierre-Louis Bossart * +--+----+--+ 45ea2ddd25SPierre-Louis Bossart * | running | 46ea2ddd25SPierre-Louis Bossart * +----------+ 47ea2ddd25SPierre-Louis Bossart */ 48ea2ddd25SPierre-Louis Bossart 4914752412STakashi Iwai /** 505dd3d271SSameer Pujar * snd_hdac_get_stream_stripe_ctl - get stripe control value 515dd3d271SSameer Pujar * @bus: HD-audio core bus 525dd3d271SSameer Pujar * @substream: PCM substream 535dd3d271SSameer Pujar */ 545dd3d271SSameer Pujar int snd_hdac_get_stream_stripe_ctl(struct hdac_bus *bus, 555dd3d271SSameer Pujar struct snd_pcm_substream *substream) 565dd3d271SSameer Pujar { 575dd3d271SSameer Pujar struct snd_pcm_runtime *runtime = substream->runtime; 585dd3d271SSameer Pujar unsigned int channels = runtime->channels, 595dd3d271SSameer Pujar rate = runtime->rate, 605dd3d271SSameer Pujar bits_per_sample = runtime->sample_bits, 615dd3d271SSameer Pujar max_sdo_lines, value, sdo_line; 625dd3d271SSameer Pujar 635dd3d271SSameer Pujar /* T_AZA_GCAP_NSDO is 1:2 bitfields in GCAP */ 645dd3d271SSameer Pujar max_sdo_lines = snd_hdac_chip_readl(bus, GCAP) & AZX_GCAP_NSDO; 655dd3d271SSameer Pujar 665dd3d271SSameer Pujar /* following is from HD audio spec */ 675dd3d271SSameer Pujar for (sdo_line = max_sdo_lines; sdo_line > 0; sdo_line >>= 1) { 685dd3d271SSameer Pujar if (rate > 48000) 695dd3d271SSameer Pujar value = (channels * bits_per_sample * 705dd3d271SSameer Pujar (rate / 48000)) / sdo_line; 715dd3d271SSameer Pujar else 725dd3d271SSameer Pujar value = (channels * bits_per_sample) / sdo_line; 735dd3d271SSameer Pujar 7467ae482aSSameer Pujar if (value >= bus->sdo_limit) 755dd3d271SSameer Pujar break; 765dd3d271SSameer Pujar } 775dd3d271SSameer Pujar 785dd3d271SSameer Pujar /* stripe value: 0 for 1SDO, 1 for 2SDO, 2 for 4SDO lines */ 795dd3d271SSameer Pujar return sdo_line >> 1; 805dd3d271SSameer Pujar } 815dd3d271SSameer Pujar EXPORT_SYMBOL_GPL(snd_hdac_get_stream_stripe_ctl); 825dd3d271SSameer Pujar 835dd3d271SSameer Pujar /** 8414752412STakashi Iwai * snd_hdac_stream_init - initialize each stream (aka device) 8514752412STakashi Iwai * @bus: HD-audio core bus 8614752412STakashi Iwai * @azx_dev: HD-audio core stream object to initialize 8714752412STakashi Iwai * @idx: stream index number 8814752412STakashi Iwai * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE) 8914752412STakashi Iwai * @tag: the tag id to assign 9014752412STakashi Iwai * 9114752412STakashi Iwai * Assign the starting bdl address to each stream (device) and initialize. 9214752412STakashi Iwai */ 9314752412STakashi Iwai void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev, 9414752412STakashi Iwai int idx, int direction, int tag) 9514752412STakashi Iwai { 9614752412STakashi Iwai azx_dev->bus = bus; 9714752412STakashi Iwai /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */ 9814752412STakashi Iwai azx_dev->sd_addr = bus->remap_addr + (0x20 * idx + 0x80); 9914752412STakashi Iwai /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */ 10014752412STakashi Iwai azx_dev->sd_int_sta_mask = 1 << idx; 10114752412STakashi Iwai azx_dev->index = idx; 10214752412STakashi Iwai azx_dev->direction = direction; 10314752412STakashi Iwai azx_dev->stream_tag = tag; 1048f3f600bSTakashi Iwai snd_hdac_dsp_lock_init(azx_dev); 10514752412STakashi Iwai list_add_tail(&azx_dev->list, &bus->stream_list); 10614752412STakashi Iwai } 10714752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_init); 10814752412STakashi Iwai 10914752412STakashi Iwai /** 11014752412STakashi Iwai * snd_hdac_stream_start - start a stream 11114752412STakashi Iwai * @azx_dev: HD-audio core stream to start 11214752412STakashi Iwai * @fresh_start: false = wallclock timestamp relative to period wallclock 11314752412STakashi Iwai * 11414752412STakashi Iwai * Start a stream, set start_wallclk and set the running flag. 11514752412STakashi Iwai */ 11614752412STakashi Iwai void snd_hdac_stream_start(struct hdac_stream *azx_dev, bool fresh_start) 11714752412STakashi Iwai { 11814752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 1199b6f7e7aSSameer Pujar int stripe_ctl; 12014752412STakashi Iwai 121598dfb56SLibin Yang trace_snd_hdac_stream_start(bus, azx_dev); 122598dfb56SLibin Yang 12314752412STakashi Iwai azx_dev->start_wallclk = snd_hdac_chip_readl(bus, WALLCLK); 12414752412STakashi Iwai if (!fresh_start) 12514752412STakashi Iwai azx_dev->start_wallclk -= azx_dev->period_wallclk; 12614752412STakashi Iwai 12714752412STakashi Iwai /* enable SIE */ 128fc2a6cf0SKeyon Jie snd_hdac_chip_updatel(bus, INTCTL, 129fc2a6cf0SKeyon Jie 1 << azx_dev->index, 130fc2a6cf0SKeyon Jie 1 << azx_dev->index); 1319b6f7e7aSSameer Pujar /* set stripe control */ 132e38e486dSTakashi Iwai if (azx_dev->stripe) { 133d344e079SMariusz Ceier if (azx_dev->substream) 1349b6f7e7aSSameer Pujar stripe_ctl = snd_hdac_get_stream_stripe_ctl(bus, azx_dev->substream); 135d344e079SMariusz Ceier else 136d344e079SMariusz Ceier stripe_ctl = 0; 1379b6f7e7aSSameer Pujar snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK, 1389b6f7e7aSSameer Pujar stripe_ctl); 139e38e486dSTakashi Iwai } 14014752412STakashi Iwai /* set DMA start and interrupt mask */ 14114752412STakashi Iwai snd_hdac_stream_updateb(azx_dev, SD_CTL, 14214752412STakashi Iwai 0, SD_CTL_DMA_START | SD_INT_MASK); 14314752412STakashi Iwai azx_dev->running = true; 14414752412STakashi Iwai } 14514752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_start); 14614752412STakashi Iwai 14714752412STakashi Iwai /** 1482ea13c83SPierre-Louis Bossart * snd_hdac_stream_clear - helper to clear stream registers and stop DMA transfers 14914752412STakashi Iwai * @azx_dev: HD-audio core stream to stop 15014752412STakashi Iwai */ 1512ea13c83SPierre-Louis Bossart static void snd_hdac_stream_clear(struct hdac_stream *azx_dev) 15214752412STakashi Iwai { 15314752412STakashi Iwai snd_hdac_stream_updateb(azx_dev, SD_CTL, 15414752412STakashi Iwai SD_CTL_DMA_START | SD_INT_MASK, 0); 15514752412STakashi Iwai snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */ 1566fd739c0STakashi Iwai if (azx_dev->stripe) 1579b6f7e7aSSameer Pujar snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK, 0); 15814752412STakashi Iwai azx_dev->running = false; 15914752412STakashi Iwai } 16014752412STakashi Iwai 16114752412STakashi Iwai /** 16214752412STakashi Iwai * snd_hdac_stream_stop - stop a stream 16314752412STakashi Iwai * @azx_dev: HD-audio core stream to stop 16414752412STakashi Iwai * 16514752412STakashi Iwai * Stop a stream DMA and disable stream interrupt 16614752412STakashi Iwai */ 16714752412STakashi Iwai void snd_hdac_stream_stop(struct hdac_stream *azx_dev) 16814752412STakashi Iwai { 169598dfb56SLibin Yang trace_snd_hdac_stream_stop(azx_dev->bus, azx_dev); 170598dfb56SLibin Yang 17114752412STakashi Iwai snd_hdac_stream_clear(azx_dev); 17214752412STakashi Iwai /* disable SIE */ 17314752412STakashi Iwai snd_hdac_chip_updatel(azx_dev->bus, INTCTL, 1 << azx_dev->index, 0); 17414752412STakashi Iwai } 17514752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_stop); 17614752412STakashi Iwai 17714752412STakashi Iwai /** 17824ad3835SPierre-Louis Bossart * snd_hdac_stop_streams - stop all streams 17924ad3835SPierre-Louis Bossart * @bus: HD-audio core bus 18024ad3835SPierre-Louis Bossart */ 18124ad3835SPierre-Louis Bossart void snd_hdac_stop_streams(struct hdac_bus *bus) 18224ad3835SPierre-Louis Bossart { 18324ad3835SPierre-Louis Bossart struct hdac_stream *stream; 18424ad3835SPierre-Louis Bossart 18524ad3835SPierre-Louis Bossart list_for_each_entry(stream, &bus->stream_list, list) 18624ad3835SPierre-Louis Bossart snd_hdac_stream_stop(stream); 18724ad3835SPierre-Louis Bossart } 18824ad3835SPierre-Louis Bossart EXPORT_SYMBOL_GPL(snd_hdac_stop_streams); 18924ad3835SPierre-Louis Bossart 19024ad3835SPierre-Louis Bossart /** 19112054f0cSPierre-Louis Bossart * snd_hdac_stop_streams_and_chip - stop all streams and chip if running 19212054f0cSPierre-Louis Bossart * @bus: HD-audio core bus 19312054f0cSPierre-Louis Bossart */ 19412054f0cSPierre-Louis Bossart void snd_hdac_stop_streams_and_chip(struct hdac_bus *bus) 19512054f0cSPierre-Louis Bossart { 19612054f0cSPierre-Louis Bossart 19712054f0cSPierre-Louis Bossart if (bus->chip_init) { 19824ad3835SPierre-Louis Bossart snd_hdac_stop_streams(bus); 19912054f0cSPierre-Louis Bossart snd_hdac_bus_stop_chip(bus); 20012054f0cSPierre-Louis Bossart } 20112054f0cSPierre-Louis Bossart } 20212054f0cSPierre-Louis Bossart EXPORT_SYMBOL_GPL(snd_hdac_stop_streams_and_chip); 20312054f0cSPierre-Louis Bossart 20412054f0cSPierre-Louis Bossart /** 20514752412STakashi Iwai * snd_hdac_stream_reset - reset a stream 20614752412STakashi Iwai * @azx_dev: HD-audio core stream to reset 20714752412STakashi Iwai */ 20814752412STakashi Iwai void snd_hdac_stream_reset(struct hdac_stream *azx_dev) 20914752412STakashi Iwai { 21014752412STakashi Iwai unsigned char val; 2114106820bSMohan Kumar int dma_run_state; 21214752412STakashi Iwai 21314752412STakashi Iwai snd_hdac_stream_clear(azx_dev); 21414752412STakashi Iwai 2154106820bSMohan Kumar dma_run_state = snd_hdac_stream_readb(azx_dev, SD_CTL) & SD_CTL_DMA_START; 2164106820bSMohan Kumar 21714752412STakashi Iwai snd_hdac_stream_updateb(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET); 218d9185705SAmadeusz Sławiński 219d9185705SAmadeusz Sławiński /* wait for hardware to report that the stream entered reset */ 220d9185705SAmadeusz Sławiński snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, (val & SD_CTL_STREAM_RESET), 3, 300); 2214106820bSMohan Kumar 2224106820bSMohan Kumar if (azx_dev->bus->dma_stop_delay && dma_run_state) 2234106820bSMohan Kumar udelay(azx_dev->bus->dma_stop_delay); 2244106820bSMohan Kumar 225d9185705SAmadeusz Sławiński snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_CTL_STREAM_RESET, 0); 22614752412STakashi Iwai 227d9185705SAmadeusz Sławiński /* wait for hardware to report that the stream is out of reset */ 228d9185705SAmadeusz Sławiński snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, !(val & SD_CTL_STREAM_RESET), 3, 300); 22914752412STakashi Iwai 23014752412STakashi Iwai /* reset first position - may not be synced with hw at this time */ 23114752412STakashi Iwai if (azx_dev->posbuf) 23214752412STakashi Iwai *azx_dev->posbuf = 0; 23314752412STakashi Iwai } 23414752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_reset); 23514752412STakashi Iwai 23614752412STakashi Iwai /** 23714752412STakashi Iwai * snd_hdac_stream_setup - set up the SD for streaming 23814752412STakashi Iwai * @azx_dev: HD-audio core stream to set up 23914752412STakashi Iwai */ 24014752412STakashi Iwai int snd_hdac_stream_setup(struct hdac_stream *azx_dev) 24114752412STakashi Iwai { 24214752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 2434214c534STakashi Iwai struct snd_pcm_runtime *runtime; 24414752412STakashi Iwai unsigned int val; 24514752412STakashi Iwai 2464214c534STakashi Iwai if (azx_dev->substream) 2474214c534STakashi Iwai runtime = azx_dev->substream->runtime; 2484214c534STakashi Iwai else 2494214c534STakashi Iwai runtime = NULL; 25014752412STakashi Iwai /* make sure the run bit is zero for SD */ 25114752412STakashi Iwai snd_hdac_stream_clear(azx_dev); 25214752412STakashi Iwai /* program the stream_tag */ 25314752412STakashi Iwai val = snd_hdac_stream_readl(azx_dev, SD_CTL); 25414752412STakashi Iwai val = (val & ~SD_CTL_STREAM_TAG_MASK) | 25514752412STakashi Iwai (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT); 25614752412STakashi Iwai if (!bus->snoop) 25714752412STakashi Iwai val |= SD_CTL_TRAFFIC_PRIO; 25814752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_CTL, val); 25914752412STakashi Iwai 26014752412STakashi Iwai /* program the length of samples in cyclic buffer */ 26114752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_CBL, azx_dev->bufsize); 26214752412STakashi Iwai 26314752412STakashi Iwai /* program the stream format */ 26414752412STakashi Iwai /* this value needs to be the same as the one programmed */ 26514752412STakashi Iwai snd_hdac_stream_writew(azx_dev, SD_FORMAT, azx_dev->format_val); 26614752412STakashi Iwai 26714752412STakashi Iwai /* program the stream LVI (last valid index) of the BDL */ 26814752412STakashi Iwai snd_hdac_stream_writew(azx_dev, SD_LVI, azx_dev->frags - 1); 26914752412STakashi Iwai 27014752412STakashi Iwai /* program the BDL address */ 27114752412STakashi Iwai /* lower BDL address */ 27214752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr); 27314752412STakashi Iwai /* upper BDL address */ 27414752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 27514752412STakashi Iwai upper_32_bits(azx_dev->bdl.addr)); 27614752412STakashi Iwai 27714752412STakashi Iwai /* enable the position buffer */ 27814752412STakashi Iwai if (bus->use_posbuf && bus->posbuf.addr) { 27914752412STakashi Iwai if (!(snd_hdac_chip_readl(bus, DPLBASE) & AZX_DPLBASE_ENABLE)) 28014752412STakashi Iwai snd_hdac_chip_writel(bus, DPLBASE, 28114752412STakashi Iwai (u32)bus->posbuf.addr | AZX_DPLBASE_ENABLE); 28214752412STakashi Iwai } 28314752412STakashi Iwai 28414752412STakashi Iwai /* set the interrupt enable bits in the descriptor control register */ 28514752412STakashi Iwai snd_hdac_stream_updatel(azx_dev, SD_CTL, 0, SD_INT_MASK); 28614752412STakashi Iwai 2877da20788STakashi Iwai azx_dev->fifo_size = snd_hdac_stream_readw(azx_dev, SD_FIFOSIZE) + 1; 28814752412STakashi Iwai 28914752412STakashi Iwai /* when LPIB delay correction gives a small negative value, 29014752412STakashi Iwai * we ignore it; currently set the threshold statically to 29114752412STakashi Iwai * 64 frames 29214752412STakashi Iwai */ 2934214c534STakashi Iwai if (runtime && runtime->period_size > 64) 29414752412STakashi Iwai azx_dev->delay_negative_threshold = 29514752412STakashi Iwai -frames_to_bytes(runtime, 64); 29614752412STakashi Iwai else 29714752412STakashi Iwai azx_dev->delay_negative_threshold = 0; 29814752412STakashi Iwai 29914752412STakashi Iwai /* wallclk has 24Mhz clock source */ 3004214c534STakashi Iwai if (runtime) 30114752412STakashi Iwai azx_dev->period_wallclk = (((runtime->period_size * 24000) / 30214752412STakashi Iwai runtime->rate) * 1000); 30314752412STakashi Iwai 30414752412STakashi Iwai return 0; 30514752412STakashi Iwai } 30614752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_setup); 30714752412STakashi Iwai 30814752412STakashi Iwai /** 30914752412STakashi Iwai * snd_hdac_stream_cleanup - cleanup a stream 31014752412STakashi Iwai * @azx_dev: HD-audio core stream to clean up 31114752412STakashi Iwai */ 31214752412STakashi Iwai void snd_hdac_stream_cleanup(struct hdac_stream *azx_dev) 31314752412STakashi Iwai { 31414752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0); 31514752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0); 31614752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_CTL, 0); 31714752412STakashi Iwai azx_dev->bufsize = 0; 31814752412STakashi Iwai azx_dev->period_bytes = 0; 31914752412STakashi Iwai azx_dev->format_val = 0; 32014752412STakashi Iwai } 32114752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_cleanup); 32214752412STakashi Iwai 32314752412STakashi Iwai /** 32414752412STakashi Iwai * snd_hdac_stream_assign - assign a stream for the PCM 32514752412STakashi Iwai * @bus: HD-audio core bus 32614752412STakashi Iwai * @substream: PCM substream to assign 32714752412STakashi Iwai * 32814752412STakashi Iwai * Look for an unused stream for the given PCM substream, assign it 32914752412STakashi Iwai * and return the stream object. If no stream is free, returns NULL. 33014752412STakashi Iwai * The function tries to keep using the same stream object when it's used 33114752412STakashi Iwai * beforehand. Also, when bus->reverse_assign flag is set, the last free 33214752412STakashi Iwai * or matching entry is returned. This is needed for some strange codecs. 33314752412STakashi Iwai */ 33414752412STakashi Iwai struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus, 33514752412STakashi Iwai struct snd_pcm_substream *substream) 33614752412STakashi Iwai { 33714752412STakashi Iwai struct hdac_stream *azx_dev; 33814752412STakashi Iwai struct hdac_stream *res = NULL; 33914752412STakashi Iwai 34014752412STakashi Iwai /* make a non-zero unique key for the substream */ 34114752412STakashi Iwai int key = (substream->pcm->device << 16) | (substream->number << 2) | 34214752412STakashi Iwai (substream->stream + 1); 34314752412STakashi Iwai 3441465d06aSPierre-Louis Bossart spin_lock_irq(&bus->reg_lock); 34514752412STakashi Iwai list_for_each_entry(azx_dev, &bus->stream_list, list) { 34614752412STakashi Iwai if (azx_dev->direction != substream->stream) 34714752412STakashi Iwai continue; 34814752412STakashi Iwai if (azx_dev->opened) 34914752412STakashi Iwai continue; 35014752412STakashi Iwai if (azx_dev->assigned_key == key) { 35114752412STakashi Iwai res = azx_dev; 35214752412STakashi Iwai break; 35314752412STakashi Iwai } 35414752412STakashi Iwai if (!res || bus->reverse_assign) 35514752412STakashi Iwai res = azx_dev; 35614752412STakashi Iwai } 35714752412STakashi Iwai if (res) { 35814752412STakashi Iwai res->opened = 1; 35914752412STakashi Iwai res->running = 0; 36014752412STakashi Iwai res->assigned_key = key; 36114752412STakashi Iwai res->substream = substream; 36214752412STakashi Iwai } 3631465d06aSPierre-Louis Bossart spin_unlock_irq(&bus->reg_lock); 36414752412STakashi Iwai return res; 36514752412STakashi Iwai } 36614752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_assign); 36714752412STakashi Iwai 36814752412STakashi Iwai /** 369*ac3467adSPierre-Louis Bossart * snd_hdac_stream_release_locked - release the assigned stream 370*ac3467adSPierre-Louis Bossart * @azx_dev: HD-audio core stream to release 371*ac3467adSPierre-Louis Bossart * 372*ac3467adSPierre-Louis Bossart * Release the stream that has been assigned by snd_hdac_stream_assign(). 373*ac3467adSPierre-Louis Bossart * The bus->reg_lock needs to be taken at a higher level 374*ac3467adSPierre-Louis Bossart */ 375*ac3467adSPierre-Louis Bossart void snd_hdac_stream_release_locked(struct hdac_stream *azx_dev) 376*ac3467adSPierre-Louis Bossart { 377*ac3467adSPierre-Louis Bossart azx_dev->opened = 0; 378*ac3467adSPierre-Louis Bossart azx_dev->running = 0; 379*ac3467adSPierre-Louis Bossart azx_dev->substream = NULL; 380*ac3467adSPierre-Louis Bossart } 381*ac3467adSPierre-Louis Bossart EXPORT_SYMBOL_GPL(snd_hdac_stream_release_locked); 382*ac3467adSPierre-Louis Bossart 383*ac3467adSPierre-Louis Bossart /** 38414752412STakashi Iwai * snd_hdac_stream_release - release the assigned stream 38514752412STakashi Iwai * @azx_dev: HD-audio core stream to release 38614752412STakashi Iwai * 38714752412STakashi Iwai * Release the stream that has been assigned by snd_hdac_stream_assign(). 38814752412STakashi Iwai */ 38914752412STakashi Iwai void snd_hdac_stream_release(struct hdac_stream *azx_dev) 39014752412STakashi Iwai { 39114752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 39214752412STakashi Iwai 39314752412STakashi Iwai spin_lock_irq(&bus->reg_lock); 394*ac3467adSPierre-Louis Bossart snd_hdac_stream_release_locked(azx_dev); 39514752412STakashi Iwai spin_unlock_irq(&bus->reg_lock); 39614752412STakashi Iwai } 39714752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_release); 39814752412STakashi Iwai 3994308c9b0SJeeja KP /** 4004308c9b0SJeeja KP * snd_hdac_get_stream - return hdac_stream based on stream_tag and 4014308c9b0SJeeja KP * direction 4024308c9b0SJeeja KP * 4034308c9b0SJeeja KP * @bus: HD-audio core bus 4044308c9b0SJeeja KP * @dir: direction for the stream to be found 4054308c9b0SJeeja KP * @stream_tag: stream tag for stream to be found 4064308c9b0SJeeja KP */ 4074308c9b0SJeeja KP struct hdac_stream *snd_hdac_get_stream(struct hdac_bus *bus, 4084308c9b0SJeeja KP int dir, int stream_tag) 4094308c9b0SJeeja KP { 4104308c9b0SJeeja KP struct hdac_stream *s; 4114308c9b0SJeeja KP 4124308c9b0SJeeja KP list_for_each_entry(s, &bus->stream_list, list) { 4134308c9b0SJeeja KP if (s->direction == dir && s->stream_tag == stream_tag) 4144308c9b0SJeeja KP return s; 4154308c9b0SJeeja KP } 4164308c9b0SJeeja KP 4174308c9b0SJeeja KP return NULL; 4184308c9b0SJeeja KP } 4194308c9b0SJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_get_stream); 4204308c9b0SJeeja KP 42114752412STakashi Iwai /* 42214752412STakashi Iwai * set up a BDL entry 42314752412STakashi Iwai */ 42414752412STakashi Iwai static int setup_bdle(struct hdac_bus *bus, 42514752412STakashi Iwai struct snd_dma_buffer *dmab, 42614752412STakashi Iwai struct hdac_stream *azx_dev, __le32 **bdlp, 42714752412STakashi Iwai int ofs, int size, int with_ioc) 42814752412STakashi Iwai { 42914752412STakashi Iwai __le32 *bdl = *bdlp; 43014752412STakashi Iwai 43114752412STakashi Iwai while (size > 0) { 43214752412STakashi Iwai dma_addr_t addr; 43314752412STakashi Iwai int chunk; 43414752412STakashi Iwai 43514752412STakashi Iwai if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES) 43614752412STakashi Iwai return -EINVAL; 43714752412STakashi Iwai 43814752412STakashi Iwai addr = snd_sgbuf_get_addr(dmab, ofs); 43914752412STakashi Iwai /* program the address field of the BDL entry */ 44014752412STakashi Iwai bdl[0] = cpu_to_le32((u32)addr); 44114752412STakashi Iwai bdl[1] = cpu_to_le32(upper_32_bits(addr)); 44214752412STakashi Iwai /* program the size field of the BDL entry */ 44314752412STakashi Iwai chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size); 44414752412STakashi Iwai /* one BDLE cannot cross 4K boundary on CTHDA chips */ 44514752412STakashi Iwai if (bus->align_bdle_4k) { 44614752412STakashi Iwai u32 remain = 0x1000 - (ofs & 0xfff); 44714752412STakashi Iwai 44814752412STakashi Iwai if (chunk > remain) 44914752412STakashi Iwai chunk = remain; 45014752412STakashi Iwai } 45114752412STakashi Iwai bdl[2] = cpu_to_le32(chunk); 45214752412STakashi Iwai /* program the IOC to enable interrupt 45314752412STakashi Iwai * only when the whole fragment is processed 45414752412STakashi Iwai */ 45514752412STakashi Iwai size -= chunk; 45614752412STakashi Iwai bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01); 45714752412STakashi Iwai bdl += 4; 45814752412STakashi Iwai azx_dev->frags++; 45914752412STakashi Iwai ofs += chunk; 46014752412STakashi Iwai } 46114752412STakashi Iwai *bdlp = bdl; 46214752412STakashi Iwai return ofs; 46314752412STakashi Iwai } 46414752412STakashi Iwai 46514752412STakashi Iwai /** 46614752412STakashi Iwai * snd_hdac_stream_setup_periods - set up BDL entries 46714752412STakashi Iwai * @azx_dev: HD-audio core stream to set up 46814752412STakashi Iwai * 46914752412STakashi Iwai * Set up the buffer descriptor table of the given stream based on the 47014752412STakashi Iwai * period and buffer sizes of the assigned PCM substream. 47114752412STakashi Iwai */ 47214752412STakashi Iwai int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev) 47314752412STakashi Iwai { 47414752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 47514752412STakashi Iwai struct snd_pcm_substream *substream = azx_dev->substream; 47614752412STakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 47714752412STakashi Iwai __le32 *bdl; 47814752412STakashi Iwai int i, ofs, periods, period_bytes; 47914752412STakashi Iwai int pos_adj, pos_align; 48014752412STakashi Iwai 48114752412STakashi Iwai /* reset BDL address */ 48214752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0); 48314752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0); 48414752412STakashi Iwai 48514752412STakashi Iwai period_bytes = azx_dev->period_bytes; 48614752412STakashi Iwai periods = azx_dev->bufsize / period_bytes; 48714752412STakashi Iwai 48814752412STakashi Iwai /* program the initial BDL entries */ 48914752412STakashi Iwai bdl = (__le32 *)azx_dev->bdl.area; 49014752412STakashi Iwai ofs = 0; 49114752412STakashi Iwai azx_dev->frags = 0; 49214752412STakashi Iwai 49314752412STakashi Iwai pos_adj = bus->bdl_pos_adj; 49414752412STakashi Iwai if (!azx_dev->no_period_wakeup && pos_adj > 0) { 49514752412STakashi Iwai pos_align = pos_adj; 49681d0ec43SLars-Peter Clausen pos_adj = DIV_ROUND_UP(pos_adj * runtime->rate, 48000); 49714752412STakashi Iwai if (!pos_adj) 49814752412STakashi Iwai pos_adj = pos_align; 49914752412STakashi Iwai else 50081d0ec43SLars-Peter Clausen pos_adj = roundup(pos_adj, pos_align); 50114752412STakashi Iwai pos_adj = frames_to_bytes(runtime, pos_adj); 50214752412STakashi Iwai if (pos_adj >= period_bytes) { 50314752412STakashi Iwai dev_warn(bus->dev, "Too big adjustment %d\n", 50414752412STakashi Iwai pos_adj); 50514752412STakashi Iwai pos_adj = 0; 50614752412STakashi Iwai } else { 50714752412STakashi Iwai ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream), 50814752412STakashi Iwai azx_dev, 50914752412STakashi Iwai &bdl, ofs, pos_adj, true); 51014752412STakashi Iwai if (ofs < 0) 51114752412STakashi Iwai goto error; 51214752412STakashi Iwai } 51314752412STakashi Iwai } else 51414752412STakashi Iwai pos_adj = 0; 51514752412STakashi Iwai 51614752412STakashi Iwai for (i = 0; i < periods; i++) { 51714752412STakashi Iwai if (i == periods - 1 && pos_adj) 51814752412STakashi Iwai ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream), 51914752412STakashi Iwai azx_dev, &bdl, ofs, 52014752412STakashi Iwai period_bytes - pos_adj, 0); 52114752412STakashi Iwai else 52214752412STakashi Iwai ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream), 52314752412STakashi Iwai azx_dev, &bdl, ofs, 52414752412STakashi Iwai period_bytes, 52514752412STakashi Iwai !azx_dev->no_period_wakeup); 52614752412STakashi Iwai if (ofs < 0) 52714752412STakashi Iwai goto error; 52814752412STakashi Iwai } 52914752412STakashi Iwai return 0; 53014752412STakashi Iwai 53114752412STakashi Iwai error: 53214752412STakashi Iwai dev_err(bus->dev, "Too many BDL entries: buffer=%d, period=%d\n", 53314752412STakashi Iwai azx_dev->bufsize, period_bytes); 53414752412STakashi Iwai return -EINVAL; 53514752412STakashi Iwai } 53614752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_setup_periods); 53714752412STakashi Iwai 53878dd5e21STakashi Iwai /** 53978dd5e21STakashi Iwai * snd_hdac_stream_set_params - set stream parameters 54086f6501bSJeeja KP * @azx_dev: HD-audio core stream for which parameters are to be set 54186f6501bSJeeja KP * @format_val: format value parameter 54286f6501bSJeeja KP * 54386f6501bSJeeja KP * Setup the HD-audio core stream parameters from substream of the stream 54486f6501bSJeeja KP * and passed format value 54586f6501bSJeeja KP */ 54686f6501bSJeeja KP int snd_hdac_stream_set_params(struct hdac_stream *azx_dev, 54786f6501bSJeeja KP unsigned int format_val) 54886f6501bSJeeja KP { 54986f6501bSJeeja KP 55086f6501bSJeeja KP unsigned int bufsize, period_bytes; 55186f6501bSJeeja KP struct snd_pcm_substream *substream = azx_dev->substream; 55286f6501bSJeeja KP struct snd_pcm_runtime *runtime; 55386f6501bSJeeja KP int err; 55486f6501bSJeeja KP 55586f6501bSJeeja KP if (!substream) 55686f6501bSJeeja KP return -EINVAL; 55786f6501bSJeeja KP runtime = substream->runtime; 55886f6501bSJeeja KP bufsize = snd_pcm_lib_buffer_bytes(substream); 55986f6501bSJeeja KP period_bytes = snd_pcm_lib_period_bytes(substream); 56086f6501bSJeeja KP 56186f6501bSJeeja KP if (bufsize != azx_dev->bufsize || 56286f6501bSJeeja KP period_bytes != azx_dev->period_bytes || 56386f6501bSJeeja KP format_val != azx_dev->format_val || 56486f6501bSJeeja KP runtime->no_period_wakeup != azx_dev->no_period_wakeup) { 56586f6501bSJeeja KP azx_dev->bufsize = bufsize; 56686f6501bSJeeja KP azx_dev->period_bytes = period_bytes; 56786f6501bSJeeja KP azx_dev->format_val = format_val; 56886f6501bSJeeja KP azx_dev->no_period_wakeup = runtime->no_period_wakeup; 56986f6501bSJeeja KP err = snd_hdac_stream_setup_periods(azx_dev); 57086f6501bSJeeja KP if (err < 0) 57186f6501bSJeeja KP return err; 57286f6501bSJeeja KP } 57386f6501bSJeeja KP return 0; 57486f6501bSJeeja KP } 57586f6501bSJeeja KP EXPORT_SYMBOL_GPL(snd_hdac_stream_set_params); 57686f6501bSJeeja KP 577a5a1d1c2SThomas Gleixner static u64 azx_cc_read(const struct cyclecounter *cc) 57814752412STakashi Iwai { 57914752412STakashi Iwai struct hdac_stream *azx_dev = container_of(cc, struct hdac_stream, cc); 58014752412STakashi Iwai 58114752412STakashi Iwai return snd_hdac_chip_readl(azx_dev->bus, WALLCLK); 58214752412STakashi Iwai } 58314752412STakashi Iwai 58414752412STakashi Iwai static void azx_timecounter_init(struct hdac_stream *azx_dev, 585a5a1d1c2SThomas Gleixner bool force, u64 last) 58614752412STakashi Iwai { 58714752412STakashi Iwai struct timecounter *tc = &azx_dev->tc; 58814752412STakashi Iwai struct cyclecounter *cc = &azx_dev->cc; 58914752412STakashi Iwai u64 nsec; 59014752412STakashi Iwai 59114752412STakashi Iwai cc->read = azx_cc_read; 59214752412STakashi Iwai cc->mask = CLOCKSOURCE_MASK(32); 59314752412STakashi Iwai 59414752412STakashi Iwai /* 5956dd21ad8SThomas Gleixner * Calculate the optimal mult/shift values. The counter wraps 5966dd21ad8SThomas Gleixner * around after ~178.9 seconds. 59714752412STakashi Iwai */ 5986dd21ad8SThomas Gleixner clocks_calc_mult_shift(&cc->mult, &cc->shift, 24000000, 5996dd21ad8SThomas Gleixner NSEC_PER_SEC, 178); 60014752412STakashi Iwai 60114752412STakashi Iwai nsec = 0; /* audio time is elapsed time since trigger */ 60214752412STakashi Iwai timecounter_init(tc, cc, nsec); 60314752412STakashi Iwai if (force) { 60414752412STakashi Iwai /* 60514752412STakashi Iwai * force timecounter to use predefined value, 60614752412STakashi Iwai * used for synchronized starts 60714752412STakashi Iwai */ 60814752412STakashi Iwai tc->cycle_last = last; 60914752412STakashi Iwai } 61014752412STakashi Iwai } 61114752412STakashi Iwai 61214752412STakashi Iwai /** 61314752412STakashi Iwai * snd_hdac_stream_timecounter_init - initialize time counter 61414752412STakashi Iwai * @azx_dev: HD-audio core stream (master stream) 61514752412STakashi Iwai * @streams: bit flags of streams to set up 61614752412STakashi Iwai * 61714752412STakashi Iwai * Initializes the time counter of streams marked by the bit flags (each 61814752412STakashi Iwai * bit corresponds to the stream index). 61914752412STakashi Iwai * The trigger timestamp of PCM substream assigned to the given stream is 62014752412STakashi Iwai * updated accordingly, too. 62114752412STakashi Iwai */ 62214752412STakashi Iwai void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev, 62314752412STakashi Iwai unsigned int streams) 62414752412STakashi Iwai { 62514752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 62614752412STakashi Iwai struct snd_pcm_runtime *runtime = azx_dev->substream->runtime; 62714752412STakashi Iwai struct hdac_stream *s; 62814752412STakashi Iwai bool inited = false; 629a5a1d1c2SThomas Gleixner u64 cycle_last = 0; 63014752412STakashi Iwai int i = 0; 63114752412STakashi Iwai 63214752412STakashi Iwai list_for_each_entry(s, &bus->stream_list, list) { 63314752412STakashi Iwai if (streams & (1 << i)) { 63414752412STakashi Iwai azx_timecounter_init(s, inited, cycle_last); 63514752412STakashi Iwai if (!inited) { 63614752412STakashi Iwai inited = true; 63714752412STakashi Iwai cycle_last = s->tc.cycle_last; 63814752412STakashi Iwai } 63914752412STakashi Iwai } 64014752412STakashi Iwai i++; 64114752412STakashi Iwai } 64214752412STakashi Iwai 64314752412STakashi Iwai snd_pcm_gettime(runtime, &runtime->trigger_tstamp); 64414752412STakashi Iwai runtime->trigger_tstamp_latched = true; 64514752412STakashi Iwai } 64614752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_timecounter_init); 64714752412STakashi Iwai 64814752412STakashi Iwai /** 64914752412STakashi Iwai * snd_hdac_stream_sync_trigger - turn on/off stream sync register 65014752412STakashi Iwai * @azx_dev: HD-audio core stream (master stream) 6516e57188fSKeyon Jie * @set: true = set, false = clear 65214752412STakashi Iwai * @streams: bit flags of streams to sync 6536e57188fSKeyon Jie * @reg: the stream sync register address 65414752412STakashi Iwai */ 65514752412STakashi Iwai void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set, 65614752412STakashi Iwai unsigned int streams, unsigned int reg) 65714752412STakashi Iwai { 65814752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 65914752412STakashi Iwai unsigned int val; 66014752412STakashi Iwai 66114752412STakashi Iwai if (!reg) 66214752412STakashi Iwai reg = AZX_REG_SSYNC; 6632c1f8138STakashi Iwai val = _snd_hdac_chip_readl(bus, reg); 66414752412STakashi Iwai if (set) 66514752412STakashi Iwai val |= streams; 66614752412STakashi Iwai else 66714752412STakashi Iwai val &= ~streams; 6682c1f8138STakashi Iwai _snd_hdac_chip_writel(bus, reg, val); 66914752412STakashi Iwai } 67014752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_sync_trigger); 67114752412STakashi Iwai 67214752412STakashi Iwai /** 6738518c648Shuangjianghui * snd_hdac_stream_sync - sync with start/stop trigger operation 67414752412STakashi Iwai * @azx_dev: HD-audio core stream (master stream) 67514752412STakashi Iwai * @start: true = start, false = stop 67614752412STakashi Iwai * @streams: bit flags of streams to sync 67714752412STakashi Iwai * 67814752412STakashi Iwai * For @start = true, wait until all FIFOs get ready. 67914752412STakashi Iwai * For @start = false, wait until all RUN bits are cleared. 68014752412STakashi Iwai */ 68114752412STakashi Iwai void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start, 68214752412STakashi Iwai unsigned int streams) 68314752412STakashi Iwai { 68414752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 68514752412STakashi Iwai int i, nwait, timeout; 68614752412STakashi Iwai struct hdac_stream *s; 68714752412STakashi Iwai 68814752412STakashi Iwai for (timeout = 5000; timeout; timeout--) { 68914752412STakashi Iwai nwait = 0; 69014752412STakashi Iwai i = 0; 69114752412STakashi Iwai list_for_each_entry(s, &bus->stream_list, list) { 6927faa26c1SMohan Kumar if (!(streams & (1 << i++))) 6937faa26c1SMohan Kumar continue; 6947faa26c1SMohan Kumar 69514752412STakashi Iwai if (start) { 69614752412STakashi Iwai /* check FIFO gets ready */ 69714752412STakashi Iwai if (!(snd_hdac_stream_readb(s, SD_STS) & 69814752412STakashi Iwai SD_STS_FIFO_READY)) 69914752412STakashi Iwai nwait++; 70014752412STakashi Iwai } else { 70114752412STakashi Iwai /* check RUN bit is cleared */ 70214752412STakashi Iwai if (snd_hdac_stream_readb(s, SD_CTL) & 7037faa26c1SMohan Kumar SD_CTL_DMA_START) { 70414752412STakashi Iwai nwait++; 7057faa26c1SMohan Kumar /* 7067faa26c1SMohan Kumar * Perform stream reset if DMA RUN 7077faa26c1SMohan Kumar * bit not cleared within given timeout 7087faa26c1SMohan Kumar */ 7097faa26c1SMohan Kumar if (timeout == 1) 7107faa26c1SMohan Kumar snd_hdac_stream_reset(s); 71114752412STakashi Iwai } 71214752412STakashi Iwai } 71314752412STakashi Iwai } 71414752412STakashi Iwai if (!nwait) 71514752412STakashi Iwai break; 71614752412STakashi Iwai cpu_relax(); 71714752412STakashi Iwai } 71814752412STakashi Iwai } 71914752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_sync); 7208f3f600bSTakashi Iwai 7218f3f600bSTakashi Iwai #ifdef CONFIG_SND_HDA_DSP_LOADER 7228f3f600bSTakashi Iwai /** 7238f3f600bSTakashi Iwai * snd_hdac_dsp_prepare - prepare for DSP loading 7248f3f600bSTakashi Iwai * @azx_dev: HD-audio core stream used for DSP loading 7258f3f600bSTakashi Iwai * @format: HD-audio stream format 7268f3f600bSTakashi Iwai * @byte_size: data chunk byte size 7278f3f600bSTakashi Iwai * @bufp: allocated buffer 7288f3f600bSTakashi Iwai * 7298f3f600bSTakashi Iwai * Allocate the buffer for the given size and set up the given stream for 7308f3f600bSTakashi Iwai * DSP loading. Returns the stream tag (>= 0), or a negative error code. 7318f3f600bSTakashi Iwai */ 7328f3f600bSTakashi Iwai int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format, 7338f3f600bSTakashi Iwai unsigned int byte_size, struct snd_dma_buffer *bufp) 7348f3f600bSTakashi Iwai { 7358f3f600bSTakashi Iwai struct hdac_bus *bus = azx_dev->bus; 7367362b0fcSTakashi Iwai __le32 *bdl; 7378f3f600bSTakashi Iwai int err; 7388f3f600bSTakashi Iwai 7398f3f600bSTakashi Iwai snd_hdac_dsp_lock(azx_dev); 7408f3f600bSTakashi Iwai spin_lock_irq(&bus->reg_lock); 7418f3f600bSTakashi Iwai if (azx_dev->running || azx_dev->locked) { 7428f3f600bSTakashi Iwai spin_unlock_irq(&bus->reg_lock); 7438f3f600bSTakashi Iwai err = -EBUSY; 7448f3f600bSTakashi Iwai goto unlock; 7458f3f600bSTakashi Iwai } 7468f3f600bSTakashi Iwai azx_dev->locked = true; 7478f3f600bSTakashi Iwai spin_unlock_irq(&bus->reg_lock); 7488f3f600bSTakashi Iwai 749619a1f19STakashi Iwai err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, bus->dev, 7508f3f600bSTakashi Iwai byte_size, bufp); 7518f3f600bSTakashi Iwai if (err < 0) 7528f3f600bSTakashi Iwai goto err_alloc; 7538f3f600bSTakashi Iwai 7544214c534STakashi Iwai azx_dev->substream = NULL; 7558f3f600bSTakashi Iwai azx_dev->bufsize = byte_size; 7568f3f600bSTakashi Iwai azx_dev->period_bytes = byte_size; 7578f3f600bSTakashi Iwai azx_dev->format_val = format; 7588f3f600bSTakashi Iwai 7598f3f600bSTakashi Iwai snd_hdac_stream_reset(azx_dev); 7608f3f600bSTakashi Iwai 7618f3f600bSTakashi Iwai /* reset BDL address */ 7628f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0); 7638f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0); 7648f3f600bSTakashi Iwai 7658f3f600bSTakashi Iwai azx_dev->frags = 0; 7667362b0fcSTakashi Iwai bdl = (__le32 *)azx_dev->bdl.area; 7678f3f600bSTakashi Iwai err = setup_bdle(bus, bufp, azx_dev, &bdl, 0, byte_size, 0); 7688f3f600bSTakashi Iwai if (err < 0) 7698f3f600bSTakashi Iwai goto error; 7708f3f600bSTakashi Iwai 7718f3f600bSTakashi Iwai snd_hdac_stream_setup(azx_dev); 7728f3f600bSTakashi Iwai snd_hdac_dsp_unlock(azx_dev); 7738f3f600bSTakashi Iwai return azx_dev->stream_tag; 7748f3f600bSTakashi Iwai 7758f3f600bSTakashi Iwai error: 776619a1f19STakashi Iwai snd_dma_free_pages(bufp); 7778f3f600bSTakashi Iwai err_alloc: 7788f3f600bSTakashi Iwai spin_lock_irq(&bus->reg_lock); 7798f3f600bSTakashi Iwai azx_dev->locked = false; 7808f3f600bSTakashi Iwai spin_unlock_irq(&bus->reg_lock); 7818f3f600bSTakashi Iwai unlock: 7828f3f600bSTakashi Iwai snd_hdac_dsp_unlock(azx_dev); 7838f3f600bSTakashi Iwai return err; 7848f3f600bSTakashi Iwai } 7858f3f600bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare); 7868f3f600bSTakashi Iwai 7878f3f600bSTakashi Iwai /** 7888f3f600bSTakashi Iwai * snd_hdac_dsp_trigger - start / stop DSP loading 7898f3f600bSTakashi Iwai * @azx_dev: HD-audio core stream used for DSP loading 7908f3f600bSTakashi Iwai * @start: trigger start or stop 7918f3f600bSTakashi Iwai */ 7928f3f600bSTakashi Iwai void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start) 7938f3f600bSTakashi Iwai { 7948f3f600bSTakashi Iwai if (start) 7958f3f600bSTakashi Iwai snd_hdac_stream_start(azx_dev, true); 7968f3f600bSTakashi Iwai else 7978f3f600bSTakashi Iwai snd_hdac_stream_stop(azx_dev); 7988f3f600bSTakashi Iwai } 7998f3f600bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_dsp_trigger); 8008f3f600bSTakashi Iwai 8018f3f600bSTakashi Iwai /** 8028f3f600bSTakashi Iwai * snd_hdac_dsp_cleanup - clean up the stream from DSP loading to normal 8038f3f600bSTakashi Iwai * @azx_dev: HD-audio core stream used for DSP loading 8048f3f600bSTakashi Iwai * @dmab: buffer used by DSP loading 8058f3f600bSTakashi Iwai */ 8068f3f600bSTakashi Iwai void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev, 8078f3f600bSTakashi Iwai struct snd_dma_buffer *dmab) 8088f3f600bSTakashi Iwai { 8098f3f600bSTakashi Iwai struct hdac_bus *bus = azx_dev->bus; 8108f3f600bSTakashi Iwai 8118f3f600bSTakashi Iwai if (!dmab->area || !azx_dev->locked) 8128f3f600bSTakashi Iwai return; 8138f3f600bSTakashi Iwai 8148f3f600bSTakashi Iwai snd_hdac_dsp_lock(azx_dev); 8158f3f600bSTakashi Iwai /* reset BDL address */ 8168f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0); 8178f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0); 8188f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_CTL, 0); 8198f3f600bSTakashi Iwai azx_dev->bufsize = 0; 8208f3f600bSTakashi Iwai azx_dev->period_bytes = 0; 8218f3f600bSTakashi Iwai azx_dev->format_val = 0; 8228f3f600bSTakashi Iwai 823619a1f19STakashi Iwai snd_dma_free_pages(dmab); 8248f3f600bSTakashi Iwai dmab->area = NULL; 8258f3f600bSTakashi Iwai 8268f3f600bSTakashi Iwai spin_lock_irq(&bus->reg_lock); 8278f3f600bSTakashi Iwai azx_dev->locked = false; 8288f3f600bSTakashi Iwai spin_unlock_irq(&bus->reg_lock); 8298f3f600bSTakashi Iwai snd_hdac_dsp_unlock(azx_dev); 8308f3f600bSTakashi Iwai } 8318f3f600bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup); 8328f3f600bSTakashi Iwai #endif /* CONFIG_SND_HDA_DSP_LOADER */ 833