114752412STakashi Iwai /* 214752412STakashi Iwai * HD-audio stream operations 314752412STakashi Iwai */ 414752412STakashi Iwai 514752412STakashi Iwai #include <linux/kernel.h> 614752412STakashi Iwai #include <linux/delay.h> 714752412STakashi Iwai #include <linux/export.h> 8*5f26faceSTakashi Iwai #include <linux/clocksource.h> 914752412STakashi Iwai #include <sound/core.h> 1014752412STakashi Iwai #include <sound/pcm.h> 1114752412STakashi Iwai #include <sound/hdaudio.h> 1214752412STakashi Iwai #include <sound/hda_register.h> 1314752412STakashi Iwai 1414752412STakashi Iwai /** 1514752412STakashi Iwai * snd_hdac_stream_init - initialize each stream (aka device) 1614752412STakashi Iwai * @bus: HD-audio core bus 1714752412STakashi Iwai * @azx_dev: HD-audio core stream object to initialize 1814752412STakashi Iwai * @idx: stream index number 1914752412STakashi Iwai * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE) 2014752412STakashi Iwai * @tag: the tag id to assign 2114752412STakashi Iwai * 2214752412STakashi Iwai * Assign the starting bdl address to each stream (device) and initialize. 2314752412STakashi Iwai */ 2414752412STakashi Iwai void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev, 2514752412STakashi Iwai int idx, int direction, int tag) 2614752412STakashi Iwai { 2714752412STakashi Iwai azx_dev->bus = bus; 2814752412STakashi Iwai /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */ 2914752412STakashi Iwai azx_dev->sd_addr = bus->remap_addr + (0x20 * idx + 0x80); 3014752412STakashi Iwai /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */ 3114752412STakashi Iwai azx_dev->sd_int_sta_mask = 1 << idx; 3214752412STakashi Iwai azx_dev->index = idx; 3314752412STakashi Iwai azx_dev->direction = direction; 3414752412STakashi Iwai azx_dev->stream_tag = tag; 358f3f600bSTakashi Iwai snd_hdac_dsp_lock_init(azx_dev); 3614752412STakashi Iwai list_add_tail(&azx_dev->list, &bus->stream_list); 3714752412STakashi Iwai } 3814752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_init); 3914752412STakashi Iwai 4014752412STakashi Iwai /** 4114752412STakashi Iwai * snd_hdac_stream_start - start a stream 4214752412STakashi Iwai * @azx_dev: HD-audio core stream to start 4314752412STakashi Iwai * @fresh_start: false = wallclock timestamp relative to period wallclock 4414752412STakashi Iwai * 4514752412STakashi Iwai * Start a stream, set start_wallclk and set the running flag. 4614752412STakashi Iwai */ 4714752412STakashi Iwai void snd_hdac_stream_start(struct hdac_stream *azx_dev, bool fresh_start) 4814752412STakashi Iwai { 4914752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 5014752412STakashi Iwai 5114752412STakashi Iwai azx_dev->start_wallclk = snd_hdac_chip_readl(bus, WALLCLK); 5214752412STakashi Iwai if (!fresh_start) 5314752412STakashi Iwai azx_dev->start_wallclk -= azx_dev->period_wallclk; 5414752412STakashi Iwai 5514752412STakashi Iwai /* enable SIE */ 5614752412STakashi Iwai snd_hdac_chip_updatel(bus, INTCTL, 0, 1 << azx_dev->index); 5714752412STakashi Iwai /* set DMA start and interrupt mask */ 5814752412STakashi Iwai snd_hdac_stream_updateb(azx_dev, SD_CTL, 5914752412STakashi Iwai 0, SD_CTL_DMA_START | SD_INT_MASK); 6014752412STakashi Iwai azx_dev->running = true; 6114752412STakashi Iwai } 6214752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_start); 6314752412STakashi Iwai 6414752412STakashi Iwai /** 6514752412STakashi Iwai * snd_hdac_stream_clear - stop a stream DMA 6614752412STakashi Iwai * @azx_dev: HD-audio core stream to stop 6714752412STakashi Iwai */ 6814752412STakashi Iwai void snd_hdac_stream_clear(struct hdac_stream *azx_dev) 6914752412STakashi Iwai { 7014752412STakashi Iwai snd_hdac_stream_updateb(azx_dev, SD_CTL, 7114752412STakashi Iwai SD_CTL_DMA_START | SD_INT_MASK, 0); 7214752412STakashi Iwai snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */ 7314752412STakashi Iwai azx_dev->running = false; 7414752412STakashi Iwai } 7514752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_clear); 7614752412STakashi Iwai 7714752412STakashi Iwai /** 7814752412STakashi Iwai * snd_hdac_stream_stop - stop a stream 7914752412STakashi Iwai * @azx_dev: HD-audio core stream to stop 8014752412STakashi Iwai * 8114752412STakashi Iwai * Stop a stream DMA and disable stream interrupt 8214752412STakashi Iwai */ 8314752412STakashi Iwai void snd_hdac_stream_stop(struct hdac_stream *azx_dev) 8414752412STakashi Iwai { 8514752412STakashi Iwai snd_hdac_stream_clear(azx_dev); 8614752412STakashi Iwai /* disable SIE */ 8714752412STakashi Iwai snd_hdac_chip_updatel(azx_dev->bus, INTCTL, 1 << azx_dev->index, 0); 8814752412STakashi Iwai } 8914752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_stop); 9014752412STakashi Iwai 9114752412STakashi Iwai /** 9214752412STakashi Iwai * snd_hdac_stream_reset - reset a stream 9314752412STakashi Iwai * @azx_dev: HD-audio core stream to reset 9414752412STakashi Iwai */ 9514752412STakashi Iwai void snd_hdac_stream_reset(struct hdac_stream *azx_dev) 9614752412STakashi Iwai { 9714752412STakashi Iwai unsigned char val; 9814752412STakashi Iwai int timeout; 9914752412STakashi Iwai 10014752412STakashi Iwai snd_hdac_stream_clear(azx_dev); 10114752412STakashi Iwai 10214752412STakashi Iwai snd_hdac_stream_updateb(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET); 10314752412STakashi Iwai udelay(3); 10414752412STakashi Iwai timeout = 300; 10514752412STakashi Iwai do { 10614752412STakashi Iwai val = snd_hdac_stream_readb(azx_dev, SD_CTL) & 10714752412STakashi Iwai SD_CTL_STREAM_RESET; 10814752412STakashi Iwai if (val) 10914752412STakashi Iwai break; 11014752412STakashi Iwai } while (--timeout); 11114752412STakashi Iwai val &= ~SD_CTL_STREAM_RESET; 11214752412STakashi Iwai snd_hdac_stream_writeb(azx_dev, SD_CTL, val); 11314752412STakashi Iwai udelay(3); 11414752412STakashi Iwai 11514752412STakashi Iwai timeout = 300; 11614752412STakashi Iwai /* waiting for hardware to report that the stream is out of reset */ 11714752412STakashi Iwai do { 11814752412STakashi Iwai val = snd_hdac_stream_readb(azx_dev, SD_CTL) & 11914752412STakashi Iwai SD_CTL_STREAM_RESET; 12014752412STakashi Iwai if (!val) 12114752412STakashi Iwai break; 12214752412STakashi Iwai } while (--timeout); 12314752412STakashi Iwai 12414752412STakashi Iwai /* reset first position - may not be synced with hw at this time */ 12514752412STakashi Iwai if (azx_dev->posbuf) 12614752412STakashi Iwai *azx_dev->posbuf = 0; 12714752412STakashi Iwai } 12814752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_reset); 12914752412STakashi Iwai 13014752412STakashi Iwai /** 13114752412STakashi Iwai * snd_hdac_stream_setup - set up the SD for streaming 13214752412STakashi Iwai * @azx_dev: HD-audio core stream to set up 13314752412STakashi Iwai */ 13414752412STakashi Iwai int snd_hdac_stream_setup(struct hdac_stream *azx_dev) 13514752412STakashi Iwai { 13614752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 13714752412STakashi Iwai struct snd_pcm_runtime *runtime = azx_dev->substream->runtime; 13814752412STakashi Iwai unsigned int val; 13914752412STakashi Iwai 14014752412STakashi Iwai /* make sure the run bit is zero for SD */ 14114752412STakashi Iwai snd_hdac_stream_clear(azx_dev); 14214752412STakashi Iwai /* program the stream_tag */ 14314752412STakashi Iwai val = snd_hdac_stream_readl(azx_dev, SD_CTL); 14414752412STakashi Iwai val = (val & ~SD_CTL_STREAM_TAG_MASK) | 14514752412STakashi Iwai (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT); 14614752412STakashi Iwai if (!bus->snoop) 14714752412STakashi Iwai val |= SD_CTL_TRAFFIC_PRIO; 14814752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_CTL, val); 14914752412STakashi Iwai 15014752412STakashi Iwai /* program the length of samples in cyclic buffer */ 15114752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_CBL, azx_dev->bufsize); 15214752412STakashi Iwai 15314752412STakashi Iwai /* program the stream format */ 15414752412STakashi Iwai /* this value needs to be the same as the one programmed */ 15514752412STakashi Iwai snd_hdac_stream_writew(azx_dev, SD_FORMAT, azx_dev->format_val); 15614752412STakashi Iwai 15714752412STakashi Iwai /* program the stream LVI (last valid index) of the BDL */ 15814752412STakashi Iwai snd_hdac_stream_writew(azx_dev, SD_LVI, azx_dev->frags - 1); 15914752412STakashi Iwai 16014752412STakashi Iwai /* program the BDL address */ 16114752412STakashi Iwai /* lower BDL address */ 16214752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr); 16314752412STakashi Iwai /* upper BDL address */ 16414752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 16514752412STakashi Iwai upper_32_bits(azx_dev->bdl.addr)); 16614752412STakashi Iwai 16714752412STakashi Iwai /* enable the position buffer */ 16814752412STakashi Iwai if (bus->use_posbuf && bus->posbuf.addr) { 16914752412STakashi Iwai if (!(snd_hdac_chip_readl(bus, DPLBASE) & AZX_DPLBASE_ENABLE)) 17014752412STakashi Iwai snd_hdac_chip_writel(bus, DPLBASE, 17114752412STakashi Iwai (u32)bus->posbuf.addr | AZX_DPLBASE_ENABLE); 17214752412STakashi Iwai } 17314752412STakashi Iwai 17414752412STakashi Iwai /* set the interrupt enable bits in the descriptor control register */ 17514752412STakashi Iwai snd_hdac_stream_updatel(azx_dev, SD_CTL, 0, SD_INT_MASK); 17614752412STakashi Iwai 17714752412STakashi Iwai if (azx_dev->direction == SNDRV_PCM_STREAM_PLAYBACK) 17814752412STakashi Iwai azx_dev->fifo_size = 17914752412STakashi Iwai snd_hdac_stream_readw(azx_dev, SD_FIFOSIZE) + 1; 18014752412STakashi Iwai else 18114752412STakashi Iwai azx_dev->fifo_size = 0; 18214752412STakashi Iwai 18314752412STakashi Iwai /* when LPIB delay correction gives a small negative value, 18414752412STakashi Iwai * we ignore it; currently set the threshold statically to 18514752412STakashi Iwai * 64 frames 18614752412STakashi Iwai */ 18714752412STakashi Iwai if (runtime->period_size > 64) 18814752412STakashi Iwai azx_dev->delay_negative_threshold = 18914752412STakashi Iwai -frames_to_bytes(runtime, 64); 19014752412STakashi Iwai else 19114752412STakashi Iwai azx_dev->delay_negative_threshold = 0; 19214752412STakashi Iwai 19314752412STakashi Iwai /* wallclk has 24Mhz clock source */ 19414752412STakashi Iwai azx_dev->period_wallclk = (((runtime->period_size * 24000) / 19514752412STakashi Iwai runtime->rate) * 1000); 19614752412STakashi Iwai 19714752412STakashi Iwai return 0; 19814752412STakashi Iwai } 19914752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_setup); 20014752412STakashi Iwai 20114752412STakashi Iwai /** 20214752412STakashi Iwai * snd_hdac_stream_cleanup - cleanup a stream 20314752412STakashi Iwai * @azx_dev: HD-audio core stream to clean up 20414752412STakashi Iwai */ 20514752412STakashi Iwai void snd_hdac_stream_cleanup(struct hdac_stream *azx_dev) 20614752412STakashi Iwai { 20714752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0); 20814752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0); 20914752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_CTL, 0); 21014752412STakashi Iwai azx_dev->bufsize = 0; 21114752412STakashi Iwai azx_dev->period_bytes = 0; 21214752412STakashi Iwai azx_dev->format_val = 0; 21314752412STakashi Iwai } 21414752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_cleanup); 21514752412STakashi Iwai 21614752412STakashi Iwai /** 21714752412STakashi Iwai * snd_hdac_stream_assign - assign a stream for the PCM 21814752412STakashi Iwai * @bus: HD-audio core bus 21914752412STakashi Iwai * @substream: PCM substream to assign 22014752412STakashi Iwai * 22114752412STakashi Iwai * Look for an unused stream for the given PCM substream, assign it 22214752412STakashi Iwai * and return the stream object. If no stream is free, returns NULL. 22314752412STakashi Iwai * The function tries to keep using the same stream object when it's used 22414752412STakashi Iwai * beforehand. Also, when bus->reverse_assign flag is set, the last free 22514752412STakashi Iwai * or matching entry is returned. This is needed for some strange codecs. 22614752412STakashi Iwai */ 22714752412STakashi Iwai struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus, 22814752412STakashi Iwai struct snd_pcm_substream *substream) 22914752412STakashi Iwai { 23014752412STakashi Iwai struct hdac_stream *azx_dev; 23114752412STakashi Iwai struct hdac_stream *res = NULL; 23214752412STakashi Iwai 23314752412STakashi Iwai /* make a non-zero unique key for the substream */ 23414752412STakashi Iwai int key = (substream->pcm->device << 16) | (substream->number << 2) | 23514752412STakashi Iwai (substream->stream + 1); 23614752412STakashi Iwai 23714752412STakashi Iwai list_for_each_entry(azx_dev, &bus->stream_list, list) { 23814752412STakashi Iwai if (azx_dev->direction != substream->stream) 23914752412STakashi Iwai continue; 24014752412STakashi Iwai if (azx_dev->opened) 24114752412STakashi Iwai continue; 24214752412STakashi Iwai if (azx_dev->assigned_key == key) { 24314752412STakashi Iwai res = azx_dev; 24414752412STakashi Iwai break; 24514752412STakashi Iwai } 24614752412STakashi Iwai if (!res || bus->reverse_assign) 24714752412STakashi Iwai res = azx_dev; 24814752412STakashi Iwai } 24914752412STakashi Iwai if (res) { 25014752412STakashi Iwai spin_lock_irq(&bus->reg_lock); 25114752412STakashi Iwai res->opened = 1; 25214752412STakashi Iwai res->running = 0; 25314752412STakashi Iwai res->assigned_key = key; 25414752412STakashi Iwai res->substream = substream; 25514752412STakashi Iwai spin_unlock_irq(&bus->reg_lock); 25614752412STakashi Iwai } 25714752412STakashi Iwai return res; 25814752412STakashi Iwai } 25914752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_assign); 26014752412STakashi Iwai 26114752412STakashi Iwai /** 26214752412STakashi Iwai * snd_hdac_stream_release - release the assigned stream 26314752412STakashi Iwai * @azx_dev: HD-audio core stream to release 26414752412STakashi Iwai * 26514752412STakashi Iwai * Release the stream that has been assigned by snd_hdac_stream_assign(). 26614752412STakashi Iwai */ 26714752412STakashi Iwai void snd_hdac_stream_release(struct hdac_stream *azx_dev) 26814752412STakashi Iwai { 26914752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 27014752412STakashi Iwai 27114752412STakashi Iwai spin_lock_irq(&bus->reg_lock); 27214752412STakashi Iwai azx_dev->opened = 0; 27314752412STakashi Iwai azx_dev->running = 0; 27414752412STakashi Iwai azx_dev->substream = NULL; 27514752412STakashi Iwai spin_unlock_irq(&bus->reg_lock); 27614752412STakashi Iwai } 27714752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_release); 27814752412STakashi Iwai 27914752412STakashi Iwai /* 28014752412STakashi Iwai * set up a BDL entry 28114752412STakashi Iwai */ 28214752412STakashi Iwai static int setup_bdle(struct hdac_bus *bus, 28314752412STakashi Iwai struct snd_dma_buffer *dmab, 28414752412STakashi Iwai struct hdac_stream *azx_dev, __le32 **bdlp, 28514752412STakashi Iwai int ofs, int size, int with_ioc) 28614752412STakashi Iwai { 28714752412STakashi Iwai __le32 *bdl = *bdlp; 28814752412STakashi Iwai 28914752412STakashi Iwai while (size > 0) { 29014752412STakashi Iwai dma_addr_t addr; 29114752412STakashi Iwai int chunk; 29214752412STakashi Iwai 29314752412STakashi Iwai if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES) 29414752412STakashi Iwai return -EINVAL; 29514752412STakashi Iwai 29614752412STakashi Iwai addr = snd_sgbuf_get_addr(dmab, ofs); 29714752412STakashi Iwai /* program the address field of the BDL entry */ 29814752412STakashi Iwai bdl[0] = cpu_to_le32((u32)addr); 29914752412STakashi Iwai bdl[1] = cpu_to_le32(upper_32_bits(addr)); 30014752412STakashi Iwai /* program the size field of the BDL entry */ 30114752412STakashi Iwai chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size); 30214752412STakashi Iwai /* one BDLE cannot cross 4K boundary on CTHDA chips */ 30314752412STakashi Iwai if (bus->align_bdle_4k) { 30414752412STakashi Iwai u32 remain = 0x1000 - (ofs & 0xfff); 30514752412STakashi Iwai 30614752412STakashi Iwai if (chunk > remain) 30714752412STakashi Iwai chunk = remain; 30814752412STakashi Iwai } 30914752412STakashi Iwai bdl[2] = cpu_to_le32(chunk); 31014752412STakashi Iwai /* program the IOC to enable interrupt 31114752412STakashi Iwai * only when the whole fragment is processed 31214752412STakashi Iwai */ 31314752412STakashi Iwai size -= chunk; 31414752412STakashi Iwai bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01); 31514752412STakashi Iwai bdl += 4; 31614752412STakashi Iwai azx_dev->frags++; 31714752412STakashi Iwai ofs += chunk; 31814752412STakashi Iwai } 31914752412STakashi Iwai *bdlp = bdl; 32014752412STakashi Iwai return ofs; 32114752412STakashi Iwai } 32214752412STakashi Iwai 32314752412STakashi Iwai /** 32414752412STakashi Iwai * snd_hdac_stream_setup_periods - set up BDL entries 32514752412STakashi Iwai * @azx_dev: HD-audio core stream to set up 32614752412STakashi Iwai * 32714752412STakashi Iwai * Set up the buffer descriptor table of the given stream based on the 32814752412STakashi Iwai * period and buffer sizes of the assigned PCM substream. 32914752412STakashi Iwai */ 33014752412STakashi Iwai int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev) 33114752412STakashi Iwai { 33214752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 33314752412STakashi Iwai struct snd_pcm_substream *substream = azx_dev->substream; 33414752412STakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 33514752412STakashi Iwai __le32 *bdl; 33614752412STakashi Iwai int i, ofs, periods, period_bytes; 33714752412STakashi Iwai int pos_adj, pos_align; 33814752412STakashi Iwai 33914752412STakashi Iwai /* reset BDL address */ 34014752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0); 34114752412STakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0); 34214752412STakashi Iwai 34314752412STakashi Iwai period_bytes = azx_dev->period_bytes; 34414752412STakashi Iwai periods = azx_dev->bufsize / period_bytes; 34514752412STakashi Iwai 34614752412STakashi Iwai /* program the initial BDL entries */ 34714752412STakashi Iwai bdl = (__le32 *)azx_dev->bdl.area; 34814752412STakashi Iwai ofs = 0; 34914752412STakashi Iwai azx_dev->frags = 0; 35014752412STakashi Iwai 35114752412STakashi Iwai pos_adj = bus->bdl_pos_adj; 35214752412STakashi Iwai if (!azx_dev->no_period_wakeup && pos_adj > 0) { 35314752412STakashi Iwai pos_align = pos_adj; 35414752412STakashi Iwai pos_adj = (pos_adj * runtime->rate + 47999) / 48000; 35514752412STakashi Iwai if (!pos_adj) 35614752412STakashi Iwai pos_adj = pos_align; 35714752412STakashi Iwai else 35814752412STakashi Iwai pos_adj = ((pos_adj + pos_align - 1) / pos_align) * 35914752412STakashi Iwai pos_align; 36014752412STakashi Iwai pos_adj = frames_to_bytes(runtime, pos_adj); 36114752412STakashi Iwai if (pos_adj >= period_bytes) { 36214752412STakashi Iwai dev_warn(bus->dev, "Too big adjustment %d\n", 36314752412STakashi Iwai pos_adj); 36414752412STakashi Iwai pos_adj = 0; 36514752412STakashi Iwai } else { 36614752412STakashi Iwai ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream), 36714752412STakashi Iwai azx_dev, 36814752412STakashi Iwai &bdl, ofs, pos_adj, true); 36914752412STakashi Iwai if (ofs < 0) 37014752412STakashi Iwai goto error; 37114752412STakashi Iwai } 37214752412STakashi Iwai } else 37314752412STakashi Iwai pos_adj = 0; 37414752412STakashi Iwai 37514752412STakashi Iwai for (i = 0; i < periods; i++) { 37614752412STakashi Iwai if (i == periods - 1 && pos_adj) 37714752412STakashi Iwai ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream), 37814752412STakashi Iwai azx_dev, &bdl, ofs, 37914752412STakashi Iwai period_bytes - pos_adj, 0); 38014752412STakashi Iwai else 38114752412STakashi Iwai ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream), 38214752412STakashi Iwai azx_dev, &bdl, ofs, 38314752412STakashi Iwai period_bytes, 38414752412STakashi Iwai !azx_dev->no_period_wakeup); 38514752412STakashi Iwai if (ofs < 0) 38614752412STakashi Iwai goto error; 38714752412STakashi Iwai } 38814752412STakashi Iwai return 0; 38914752412STakashi Iwai 39014752412STakashi Iwai error: 39114752412STakashi Iwai dev_err(bus->dev, "Too many BDL entries: buffer=%d, period=%d\n", 39214752412STakashi Iwai azx_dev->bufsize, period_bytes); 39314752412STakashi Iwai return -EINVAL; 39414752412STakashi Iwai } 39514752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_setup_periods); 39614752412STakashi Iwai 39714752412STakashi Iwai static cycle_t azx_cc_read(const struct cyclecounter *cc) 39814752412STakashi Iwai { 39914752412STakashi Iwai struct hdac_stream *azx_dev = container_of(cc, struct hdac_stream, cc); 40014752412STakashi Iwai 40114752412STakashi Iwai return snd_hdac_chip_readl(azx_dev->bus, WALLCLK); 40214752412STakashi Iwai } 40314752412STakashi Iwai 40414752412STakashi Iwai static void azx_timecounter_init(struct hdac_stream *azx_dev, 40514752412STakashi Iwai bool force, cycle_t last) 40614752412STakashi Iwai { 40714752412STakashi Iwai struct timecounter *tc = &azx_dev->tc; 40814752412STakashi Iwai struct cyclecounter *cc = &azx_dev->cc; 40914752412STakashi Iwai u64 nsec; 41014752412STakashi Iwai 41114752412STakashi Iwai cc->read = azx_cc_read; 41214752412STakashi Iwai cc->mask = CLOCKSOURCE_MASK(32); 41314752412STakashi Iwai 41414752412STakashi Iwai /* 41514752412STakashi Iwai * Converting from 24 MHz to ns means applying a 125/3 factor. 41614752412STakashi Iwai * To avoid any saturation issues in intermediate operations, 41714752412STakashi Iwai * the 125 factor is applied first. The division is applied 41814752412STakashi Iwai * last after reading the timecounter value. 41914752412STakashi Iwai * Applying the 1/3 factor as part of the multiplication 42014752412STakashi Iwai * requires at least 20 bits for a decent precision, however 42114752412STakashi Iwai * overflows occur after about 4 hours or less, not a option. 42214752412STakashi Iwai */ 42314752412STakashi Iwai 42414752412STakashi Iwai cc->mult = 125; /* saturation after 195 years */ 42514752412STakashi Iwai cc->shift = 0; 42614752412STakashi Iwai 42714752412STakashi Iwai nsec = 0; /* audio time is elapsed time since trigger */ 42814752412STakashi Iwai timecounter_init(tc, cc, nsec); 42914752412STakashi Iwai if (force) { 43014752412STakashi Iwai /* 43114752412STakashi Iwai * force timecounter to use predefined value, 43214752412STakashi Iwai * used for synchronized starts 43314752412STakashi Iwai */ 43414752412STakashi Iwai tc->cycle_last = last; 43514752412STakashi Iwai } 43614752412STakashi Iwai } 43714752412STakashi Iwai 43814752412STakashi Iwai /** 43914752412STakashi Iwai * snd_hdac_stream_timecounter_init - initialize time counter 44014752412STakashi Iwai * @azx_dev: HD-audio core stream (master stream) 44114752412STakashi Iwai * @streams: bit flags of streams to set up 44214752412STakashi Iwai * 44314752412STakashi Iwai * Initializes the time counter of streams marked by the bit flags (each 44414752412STakashi Iwai * bit corresponds to the stream index). 44514752412STakashi Iwai * The trigger timestamp of PCM substream assigned to the given stream is 44614752412STakashi Iwai * updated accordingly, too. 44714752412STakashi Iwai */ 44814752412STakashi Iwai void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev, 44914752412STakashi Iwai unsigned int streams) 45014752412STakashi Iwai { 45114752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 45214752412STakashi Iwai struct snd_pcm_runtime *runtime = azx_dev->substream->runtime; 45314752412STakashi Iwai struct hdac_stream *s; 45414752412STakashi Iwai bool inited = false; 45514752412STakashi Iwai cycle_t cycle_last = 0; 45614752412STakashi Iwai int i = 0; 45714752412STakashi Iwai 45814752412STakashi Iwai list_for_each_entry(s, &bus->stream_list, list) { 45914752412STakashi Iwai if (streams & (1 << i)) { 46014752412STakashi Iwai azx_timecounter_init(s, inited, cycle_last); 46114752412STakashi Iwai if (!inited) { 46214752412STakashi Iwai inited = true; 46314752412STakashi Iwai cycle_last = s->tc.cycle_last; 46414752412STakashi Iwai } 46514752412STakashi Iwai } 46614752412STakashi Iwai i++; 46714752412STakashi Iwai } 46814752412STakashi Iwai 46914752412STakashi Iwai snd_pcm_gettime(runtime, &runtime->trigger_tstamp); 47014752412STakashi Iwai runtime->trigger_tstamp_latched = true; 47114752412STakashi Iwai } 47214752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_timecounter_init); 47314752412STakashi Iwai 47414752412STakashi Iwai /** 47514752412STakashi Iwai * snd_hdac_stream_sync_trigger - turn on/off stream sync register 47614752412STakashi Iwai * @azx_dev: HD-audio core stream (master stream) 47714752412STakashi Iwai * @streams: bit flags of streams to sync 47814752412STakashi Iwai */ 47914752412STakashi Iwai void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set, 48014752412STakashi Iwai unsigned int streams, unsigned int reg) 48114752412STakashi Iwai { 48214752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 48314752412STakashi Iwai unsigned int val; 48414752412STakashi Iwai 48514752412STakashi Iwai if (!reg) 48614752412STakashi Iwai reg = AZX_REG_SSYNC; 48714752412STakashi Iwai val = _snd_hdac_chip_read(l, bus, reg); 48814752412STakashi Iwai if (set) 48914752412STakashi Iwai val |= streams; 49014752412STakashi Iwai else 49114752412STakashi Iwai val &= ~streams; 49214752412STakashi Iwai _snd_hdac_chip_write(l, bus, reg, val); 49314752412STakashi Iwai } 49414752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_sync_trigger); 49514752412STakashi Iwai 49614752412STakashi Iwai /** 49714752412STakashi Iwai * snd_hdac_stream_sync - sync with start/strop trigger operation 49814752412STakashi Iwai * @azx_dev: HD-audio core stream (master stream) 49914752412STakashi Iwai * @start: true = start, false = stop 50014752412STakashi Iwai * @streams: bit flags of streams to sync 50114752412STakashi Iwai * 50214752412STakashi Iwai * For @start = true, wait until all FIFOs get ready. 50314752412STakashi Iwai * For @start = false, wait until all RUN bits are cleared. 50414752412STakashi Iwai */ 50514752412STakashi Iwai void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start, 50614752412STakashi Iwai unsigned int streams) 50714752412STakashi Iwai { 50814752412STakashi Iwai struct hdac_bus *bus = azx_dev->bus; 50914752412STakashi Iwai int i, nwait, timeout; 51014752412STakashi Iwai struct hdac_stream *s; 51114752412STakashi Iwai 51214752412STakashi Iwai for (timeout = 5000; timeout; timeout--) { 51314752412STakashi Iwai nwait = 0; 51414752412STakashi Iwai i = 0; 51514752412STakashi Iwai list_for_each_entry(s, &bus->stream_list, list) { 51614752412STakashi Iwai if (streams & (1 << i)) { 51714752412STakashi Iwai if (start) { 51814752412STakashi Iwai /* check FIFO gets ready */ 51914752412STakashi Iwai if (!(snd_hdac_stream_readb(s, SD_STS) & 52014752412STakashi Iwai SD_STS_FIFO_READY)) 52114752412STakashi Iwai nwait++; 52214752412STakashi Iwai } else { 52314752412STakashi Iwai /* check RUN bit is cleared */ 52414752412STakashi Iwai if (snd_hdac_stream_readb(s, SD_CTL) & 52514752412STakashi Iwai SD_CTL_DMA_START) 52614752412STakashi Iwai nwait++; 52714752412STakashi Iwai } 52814752412STakashi Iwai } 52914752412STakashi Iwai i++; 53014752412STakashi Iwai } 53114752412STakashi Iwai if (!nwait) 53214752412STakashi Iwai break; 53314752412STakashi Iwai cpu_relax(); 53414752412STakashi Iwai } 53514752412STakashi Iwai } 53614752412STakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_stream_sync); 5378f3f600bSTakashi Iwai 5388f3f600bSTakashi Iwai #ifdef CONFIG_SND_HDA_DSP_LOADER 5398f3f600bSTakashi Iwai /** 5408f3f600bSTakashi Iwai * snd_hdac_dsp_prepare - prepare for DSP loading 5418f3f600bSTakashi Iwai * @azx_dev: HD-audio core stream used for DSP loading 5428f3f600bSTakashi Iwai * @format: HD-audio stream format 5438f3f600bSTakashi Iwai * @byte_size: data chunk byte size 5448f3f600bSTakashi Iwai * @bufp: allocated buffer 5458f3f600bSTakashi Iwai * 5468f3f600bSTakashi Iwai * Allocate the buffer for the given size and set up the given stream for 5478f3f600bSTakashi Iwai * DSP loading. Returns the stream tag (>= 0), or a negative error code. 5488f3f600bSTakashi Iwai */ 5498f3f600bSTakashi Iwai int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format, 5508f3f600bSTakashi Iwai unsigned int byte_size, struct snd_dma_buffer *bufp) 5518f3f600bSTakashi Iwai { 5528f3f600bSTakashi Iwai struct hdac_bus *bus = azx_dev->bus; 5538f3f600bSTakashi Iwai u32 *bdl; 5548f3f600bSTakashi Iwai int err; 5558f3f600bSTakashi Iwai 5568f3f600bSTakashi Iwai snd_hdac_dsp_lock(azx_dev); 5578f3f600bSTakashi Iwai spin_lock_irq(&bus->reg_lock); 5588f3f600bSTakashi Iwai if (azx_dev->running || azx_dev->locked) { 5598f3f600bSTakashi Iwai spin_unlock_irq(&bus->reg_lock); 5608f3f600bSTakashi Iwai err = -EBUSY; 5618f3f600bSTakashi Iwai goto unlock; 5628f3f600bSTakashi Iwai } 5638f3f600bSTakashi Iwai azx_dev->locked = true; 5648f3f600bSTakashi Iwai spin_unlock_irq(&bus->reg_lock); 5658f3f600bSTakashi Iwai 5668f3f600bSTakashi Iwai err = bus->io_ops->dma_alloc_pages(bus, SNDRV_DMA_TYPE_DEV_SG, 5678f3f600bSTakashi Iwai byte_size, bufp); 5688f3f600bSTakashi Iwai if (err < 0) 5698f3f600bSTakashi Iwai goto err_alloc; 5708f3f600bSTakashi Iwai 5718f3f600bSTakashi Iwai azx_dev->bufsize = byte_size; 5728f3f600bSTakashi Iwai azx_dev->period_bytes = byte_size; 5738f3f600bSTakashi Iwai azx_dev->format_val = format; 5748f3f600bSTakashi Iwai 5758f3f600bSTakashi Iwai snd_hdac_stream_reset(azx_dev); 5768f3f600bSTakashi Iwai 5778f3f600bSTakashi Iwai /* reset BDL address */ 5788f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0); 5798f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0); 5808f3f600bSTakashi Iwai 5818f3f600bSTakashi Iwai azx_dev->frags = 0; 5828f3f600bSTakashi Iwai bdl = (u32 *)azx_dev->bdl.area; 5838f3f600bSTakashi Iwai err = setup_bdle(bus, bufp, azx_dev, &bdl, 0, byte_size, 0); 5848f3f600bSTakashi Iwai if (err < 0) 5858f3f600bSTakashi Iwai goto error; 5868f3f600bSTakashi Iwai 5878f3f600bSTakashi Iwai snd_hdac_stream_setup(azx_dev); 5888f3f600bSTakashi Iwai snd_hdac_dsp_unlock(azx_dev); 5898f3f600bSTakashi Iwai return azx_dev->stream_tag; 5908f3f600bSTakashi Iwai 5918f3f600bSTakashi Iwai error: 5928f3f600bSTakashi Iwai bus->io_ops->dma_free_pages(bus, bufp); 5938f3f600bSTakashi Iwai err_alloc: 5948f3f600bSTakashi Iwai spin_lock_irq(&bus->reg_lock); 5958f3f600bSTakashi Iwai azx_dev->locked = false; 5968f3f600bSTakashi Iwai spin_unlock_irq(&bus->reg_lock); 5978f3f600bSTakashi Iwai unlock: 5988f3f600bSTakashi Iwai snd_hdac_dsp_unlock(azx_dev); 5998f3f600bSTakashi Iwai return err; 6008f3f600bSTakashi Iwai } 6018f3f600bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare); 6028f3f600bSTakashi Iwai 6038f3f600bSTakashi Iwai /** 6048f3f600bSTakashi Iwai * snd_hdac_dsp_trigger - start / stop DSP loading 6058f3f600bSTakashi Iwai * @azx_dev: HD-audio core stream used for DSP loading 6068f3f600bSTakashi Iwai * @start: trigger start or stop 6078f3f600bSTakashi Iwai */ 6088f3f600bSTakashi Iwai void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start) 6098f3f600bSTakashi Iwai { 6108f3f600bSTakashi Iwai if (start) 6118f3f600bSTakashi Iwai snd_hdac_stream_start(azx_dev, true); 6128f3f600bSTakashi Iwai else 6138f3f600bSTakashi Iwai snd_hdac_stream_stop(azx_dev); 6148f3f600bSTakashi Iwai } 6158f3f600bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_dsp_trigger); 6168f3f600bSTakashi Iwai 6178f3f600bSTakashi Iwai /** 6188f3f600bSTakashi Iwai * snd_hdac_dsp_cleanup - clean up the stream from DSP loading to normal 6198f3f600bSTakashi Iwai * @azx_dev: HD-audio core stream used for DSP loading 6208f3f600bSTakashi Iwai * @dmab: buffer used by DSP loading 6218f3f600bSTakashi Iwai */ 6228f3f600bSTakashi Iwai void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev, 6238f3f600bSTakashi Iwai struct snd_dma_buffer *dmab) 6248f3f600bSTakashi Iwai { 6258f3f600bSTakashi Iwai struct hdac_bus *bus = azx_dev->bus; 6268f3f600bSTakashi Iwai 6278f3f600bSTakashi Iwai if (!dmab->area || !azx_dev->locked) 6288f3f600bSTakashi Iwai return; 6298f3f600bSTakashi Iwai 6308f3f600bSTakashi Iwai snd_hdac_dsp_lock(azx_dev); 6318f3f600bSTakashi Iwai /* reset BDL address */ 6328f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0); 6338f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0); 6348f3f600bSTakashi Iwai snd_hdac_stream_writel(azx_dev, SD_CTL, 0); 6358f3f600bSTakashi Iwai azx_dev->bufsize = 0; 6368f3f600bSTakashi Iwai azx_dev->period_bytes = 0; 6378f3f600bSTakashi Iwai azx_dev->format_val = 0; 6388f3f600bSTakashi Iwai 6398f3f600bSTakashi Iwai bus->io_ops->dma_free_pages(bus, dmab); 6408f3f600bSTakashi Iwai dmab->area = NULL; 6418f3f600bSTakashi Iwai 6428f3f600bSTakashi Iwai spin_lock_irq(&bus->reg_lock); 6438f3f600bSTakashi Iwai azx_dev->locked = false; 6448f3f600bSTakashi Iwai spin_unlock_irq(&bus->reg_lock); 6458f3f600bSTakashi Iwai snd_hdac_dsp_unlock(azx_dev); 6468f3f600bSTakashi Iwai } 6478f3f600bSTakashi Iwai EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup); 6488f3f600bSTakashi Iwai #endif /* CONFIG_SND_HDA_DSP_LOADER */ 649