1 // SPDX-License-Identifier: GPL-2.0-only 1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 2 /* 3 * HD-audio stream operations 3 * HD-audio stream operations 4 */ 4 */ 5 5 6 #include <linux/kernel.h> 6 #include <linux/kernel.h> 7 #include <linux/delay.h> 7 #include <linux/delay.h> 8 #include <linux/export.h> 8 #include <linux/export.h> 9 #include <linux/clocksource.h> 9 #include <linux/clocksource.h> 10 #include <sound/compress_driver.h> 10 #include <sound/compress_driver.h> 11 #include <sound/core.h> 11 #include <sound/core.h> 12 #include <sound/pcm.h> 12 #include <sound/pcm.h> 13 #include <sound/hdaudio.h> 13 #include <sound/hdaudio.h> 14 #include <sound/hda_register.h> 14 #include <sound/hda_register.h> 15 #include "trace.h" 15 #include "trace.h" 16 16 17 /* 17 /* 18 * the hdac_stream library is intended to be u 18 * the hdac_stream library is intended to be used with the following 19 * transitions. The states are not formally de 19 * transitions. The states are not formally defined in the code but loosely 20 * inspired by boolean variables. Note that th 20 * inspired by boolean variables. Note that the 'prepared' field is not used 21 * in this library but by the callers during t 21 * in this library but by the callers during the hw_params/prepare transitions 22 * 22 * 23 * | 23 * | 24 * stream_init() | 24 * stream_init() | 25 * v 25 * v 26 * +--+-------+ 26 * +--+-------+ 27 * | unused | 27 * | unused | 28 * +--+----+--+ 28 * +--+----+--+ 29 * | ^ 29 * | ^ 30 * stream_assign() | | stream_re 30 * stream_assign() | | stream_release() 31 * v | 31 * v | 32 * +--+----+--+ 32 * +--+----+--+ 33 * | opened | 33 * | opened | 34 * +--+----+--+ 34 * +--+----+--+ 35 * | ^ 35 * | ^ 36 * stream_reset() | | 36 * stream_reset() | | 37 * stream_setup() | | stream_cl 37 * stream_setup() | | stream_cleanup() 38 * v | 38 * v | 39 * +--+----+--+ 39 * +--+----+--+ 40 * | prepared | 40 * | prepared | 41 * +--+----+--+ 41 * +--+----+--+ 42 * | ^ 42 * | ^ 43 * stream_start() | | stream_st 43 * stream_start() | | stream_stop() 44 * v | 44 * v | 45 * +--+----+--+ 45 * +--+----+--+ 46 * | running | 46 * | running | 47 * +----------+ 47 * +----------+ 48 */ 48 */ 49 49 50 /** 50 /** 51 * snd_hdac_get_stream_stripe_ctl - get stripe 51 * snd_hdac_get_stream_stripe_ctl - get stripe control value 52 * @bus: HD-audio core bus 52 * @bus: HD-audio core bus 53 * @substream: PCM substream 53 * @substream: PCM substream 54 */ 54 */ 55 int snd_hdac_get_stream_stripe_ctl(struct hdac 55 int snd_hdac_get_stream_stripe_ctl(struct hdac_bus *bus, 56 struct snd_ 56 struct snd_pcm_substream *substream) 57 { 57 { 58 struct snd_pcm_runtime *runtime = subs 58 struct snd_pcm_runtime *runtime = substream->runtime; 59 unsigned int channels = runtime->chann 59 unsigned int channels = runtime->channels, 60 rate = runtime->rate, 60 rate = runtime->rate, 61 bits_per_sample = runtime 61 bits_per_sample = runtime->sample_bits, 62 max_sdo_lines, value, sdo 62 max_sdo_lines, value, sdo_line; 63 63 64 /* T_AZA_GCAP_NSDO is 1:2 bitfields in 64 /* T_AZA_GCAP_NSDO is 1:2 bitfields in GCAP */ 65 max_sdo_lines = snd_hdac_chip_readl(bu 65 max_sdo_lines = snd_hdac_chip_readl(bus, GCAP) & AZX_GCAP_NSDO; 66 66 67 /* following is from HD audio spec */ 67 /* following is from HD audio spec */ 68 for (sdo_line = max_sdo_lines; sdo_lin 68 for (sdo_line = max_sdo_lines; sdo_line > 0; sdo_line >>= 1) { 69 if (rate > 48000) 69 if (rate > 48000) 70 value = (channels * bi 70 value = (channels * bits_per_sample * 71 (rate 71 (rate / 48000)) / sdo_line; 72 else 72 else 73 value = (channels * bi 73 value = (channels * bits_per_sample) / sdo_line; 74 74 75 if (value >= bus->sdo_limit) 75 if (value >= bus->sdo_limit) 76 break; 76 break; 77 } 77 } 78 78 79 /* stripe value: 0 for 1SDO, 1 for 2SD 79 /* stripe value: 0 for 1SDO, 1 for 2SDO, 2 for 4SDO lines */ 80 return sdo_line >> 1; 80 return sdo_line >> 1; 81 } 81 } 82 EXPORT_SYMBOL_GPL(snd_hdac_get_stream_stripe_c 82 EXPORT_SYMBOL_GPL(snd_hdac_get_stream_stripe_ctl); 83 83 84 /** 84 /** 85 * snd_hdac_stream_init - initialize each stre 85 * snd_hdac_stream_init - initialize each stream (aka device) 86 * @bus: HD-audio core bus 86 * @bus: HD-audio core bus 87 * @azx_dev: HD-audio core stream object to in 87 * @azx_dev: HD-audio core stream object to initialize 88 * @idx: stream index number 88 * @idx: stream index number 89 * @direction: stream direction (SNDRV_PCM_STR 89 * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE) 90 * @tag: the tag id to assign 90 * @tag: the tag id to assign 91 * 91 * 92 * Assign the starting bdl address to each str 92 * Assign the starting bdl address to each stream (device) and initialize. 93 */ 93 */ 94 void snd_hdac_stream_init(struct hdac_bus *bus 94 void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev, 95 int idx, int directi 95 int idx, int direction, int tag) 96 { 96 { 97 azx_dev->bus = bus; 97 azx_dev->bus = bus; 98 /* offset: SDI0=0x80, SDI1=0xa0, ... S 98 /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */ 99 azx_dev->sd_addr = bus->remap_addr + ( 99 azx_dev->sd_addr = bus->remap_addr + (0x20 * idx + 0x80); 100 /* int mask: SDI0=0x01, SDI1=0x02, ... 100 /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */ 101 azx_dev->sd_int_sta_mask = 1 << idx; 101 azx_dev->sd_int_sta_mask = 1 << idx; 102 azx_dev->index = idx; 102 azx_dev->index = idx; 103 azx_dev->direction = direction; 103 azx_dev->direction = direction; 104 azx_dev->stream_tag = tag; 104 azx_dev->stream_tag = tag; 105 snd_hdac_dsp_lock_init(azx_dev); 105 snd_hdac_dsp_lock_init(azx_dev); 106 list_add_tail(&azx_dev->list, &bus->st 106 list_add_tail(&azx_dev->list, &bus->stream_list); 107 107 108 if (bus->spbcap) { 108 if (bus->spbcap) { 109 azx_dev->spib_addr = bus->spbc 109 azx_dev->spib_addr = bus->spbcap + AZX_SPB_BASE + 110 AZX_SP 110 AZX_SPB_INTERVAL * idx + 111 AZX_SP 111 AZX_SPB_SPIB; 112 112 113 azx_dev->fifo_addr = bus->spbc 113 azx_dev->fifo_addr = bus->spbcap + AZX_SPB_BASE + 114 AZX_SP 114 AZX_SPB_INTERVAL * idx + 115 AZX_SP 115 AZX_SPB_MAXFIFO; 116 } 116 } 117 117 118 if (bus->drsmcap) 118 if (bus->drsmcap) 119 azx_dev->dpibr_addr = bus->drs 119 azx_dev->dpibr_addr = bus->drsmcap + AZX_DRSM_BASE + 120 AZX_DR 120 AZX_DRSM_INTERVAL * idx; 121 } 121 } 122 EXPORT_SYMBOL_GPL(snd_hdac_stream_init); 122 EXPORT_SYMBOL_GPL(snd_hdac_stream_init); 123 123 124 /** 124 /** 125 * snd_hdac_stream_start - start a stream 125 * snd_hdac_stream_start - start a stream 126 * @azx_dev: HD-audio core stream to start 126 * @azx_dev: HD-audio core stream to start 127 * 127 * 128 * Start a stream, set start_wallclk and set t 128 * Start a stream, set start_wallclk and set the running flag. 129 */ 129 */ 130 void snd_hdac_stream_start(struct hdac_stream 130 void snd_hdac_stream_start(struct hdac_stream *azx_dev) 131 { 131 { 132 struct hdac_bus *bus = azx_dev->bus; 132 struct hdac_bus *bus = azx_dev->bus; 133 int stripe_ctl; 133 int stripe_ctl; 134 134 135 trace_snd_hdac_stream_start(bus, azx_d 135 trace_snd_hdac_stream_start(bus, azx_dev); 136 136 137 azx_dev->start_wallclk = snd_hdac_chip 137 azx_dev->start_wallclk = snd_hdac_chip_readl(bus, WALLCLK); 138 138 139 /* enable SIE */ 139 /* enable SIE */ 140 snd_hdac_chip_updatel(bus, INTCTL, 140 snd_hdac_chip_updatel(bus, INTCTL, 141 1 << azx_dev->in 141 1 << azx_dev->index, 142 1 << azx_dev->in 142 1 << azx_dev->index); 143 /* set stripe control */ 143 /* set stripe control */ 144 if (azx_dev->stripe) { 144 if (azx_dev->stripe) { 145 if (azx_dev->substream) 145 if (azx_dev->substream) 146 stripe_ctl = snd_hdac_ 146 stripe_ctl = snd_hdac_get_stream_stripe_ctl(bus, azx_dev->substream); 147 else 147 else 148 stripe_ctl = 0; 148 stripe_ctl = 0; 149 snd_hdac_stream_updateb(azx_de 149 snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK, 150 stripe 150 stripe_ctl); 151 } 151 } 152 /* set DMA start and interrupt mask */ 152 /* set DMA start and interrupt mask */ 153 if (bus->access_sdnctl_in_dword) 153 if (bus->access_sdnctl_in_dword) 154 snd_hdac_stream_updatel(azx_de 154 snd_hdac_stream_updatel(azx_dev, SD_CTL, 155 0, SD_CTL_DMA_ 155 0, SD_CTL_DMA_START | SD_INT_MASK); 156 else 156 else 157 snd_hdac_stream_updateb(azx_de 157 snd_hdac_stream_updateb(azx_dev, SD_CTL, 158 0, SD_CTL_DMA_ 158 0, SD_CTL_DMA_START | SD_INT_MASK); 159 azx_dev->running = true; 159 azx_dev->running = true; 160 } 160 } 161 EXPORT_SYMBOL_GPL(snd_hdac_stream_start); 161 EXPORT_SYMBOL_GPL(snd_hdac_stream_start); 162 162 163 /** 163 /** 164 * snd_hdac_stream_clear - helper to clear str 164 * snd_hdac_stream_clear - helper to clear stream registers and stop DMA transfers 165 * @azx_dev: HD-audio core stream to stop 165 * @azx_dev: HD-audio core stream to stop 166 */ 166 */ 167 static void snd_hdac_stream_clear(struct hdac_ 167 static void snd_hdac_stream_clear(struct hdac_stream *azx_dev) 168 { 168 { 169 snd_hdac_stream_updateb(azx_dev, SD_CT 169 snd_hdac_stream_updateb(azx_dev, SD_CTL, 170 SD_CTL_DMA_STA 170 SD_CTL_DMA_START | SD_INT_MASK, 0); 171 snd_hdac_stream_writeb(azx_dev, SD_STS 171 snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */ 172 if (azx_dev->stripe) 172 if (azx_dev->stripe) 173 snd_hdac_stream_updateb(azx_de 173 snd_hdac_stream_updateb(azx_dev, SD_CTL_3B, SD_CTL_STRIPE_MASK, 0); 174 azx_dev->running = false; 174 azx_dev->running = false; 175 } 175 } 176 176 177 /** 177 /** 178 * snd_hdac_stream_stop - stop a stream 178 * snd_hdac_stream_stop - stop a stream 179 * @azx_dev: HD-audio core stream to stop 179 * @azx_dev: HD-audio core stream to stop 180 * 180 * 181 * Stop a stream DMA and disable stream interr 181 * Stop a stream DMA and disable stream interrupt 182 */ 182 */ 183 void snd_hdac_stream_stop(struct hdac_stream * 183 void snd_hdac_stream_stop(struct hdac_stream *azx_dev) 184 { 184 { 185 trace_snd_hdac_stream_stop(azx_dev->bu 185 trace_snd_hdac_stream_stop(azx_dev->bus, azx_dev); 186 186 187 snd_hdac_stream_clear(azx_dev); 187 snd_hdac_stream_clear(azx_dev); 188 /* disable SIE */ 188 /* disable SIE */ 189 snd_hdac_chip_updatel(azx_dev->bus, IN 189 snd_hdac_chip_updatel(azx_dev->bus, INTCTL, 1 << azx_dev->index, 0); 190 } 190 } 191 EXPORT_SYMBOL_GPL(snd_hdac_stream_stop); 191 EXPORT_SYMBOL_GPL(snd_hdac_stream_stop); 192 192 193 /** 193 /** 194 * snd_hdac_stop_streams - stop all streams 194 * snd_hdac_stop_streams - stop all streams 195 * @bus: HD-audio core bus 195 * @bus: HD-audio core bus 196 */ 196 */ 197 void snd_hdac_stop_streams(struct hdac_bus *bu 197 void snd_hdac_stop_streams(struct hdac_bus *bus) 198 { 198 { 199 struct hdac_stream *stream; 199 struct hdac_stream *stream; 200 200 201 list_for_each_entry(stream, &bus->stre 201 list_for_each_entry(stream, &bus->stream_list, list) 202 snd_hdac_stream_stop(stream); 202 snd_hdac_stream_stop(stream); 203 } 203 } 204 EXPORT_SYMBOL_GPL(snd_hdac_stop_streams); 204 EXPORT_SYMBOL_GPL(snd_hdac_stop_streams); 205 205 206 /** 206 /** 207 * snd_hdac_stop_streams_and_chip - stop all s 207 * snd_hdac_stop_streams_and_chip - stop all streams and chip if running 208 * @bus: HD-audio core bus 208 * @bus: HD-audio core bus 209 */ 209 */ 210 void snd_hdac_stop_streams_and_chip(struct hda 210 void snd_hdac_stop_streams_and_chip(struct hdac_bus *bus) 211 { 211 { 212 212 213 if (bus->chip_init) { 213 if (bus->chip_init) { 214 snd_hdac_stop_streams(bus); 214 snd_hdac_stop_streams(bus); 215 snd_hdac_bus_stop_chip(bus); 215 snd_hdac_bus_stop_chip(bus); 216 } 216 } 217 } 217 } 218 EXPORT_SYMBOL_GPL(snd_hdac_stop_streams_and_ch 218 EXPORT_SYMBOL_GPL(snd_hdac_stop_streams_and_chip); 219 219 220 /** 220 /** 221 * snd_hdac_stream_reset - reset a stream 221 * snd_hdac_stream_reset - reset a stream 222 * @azx_dev: HD-audio core stream to reset 222 * @azx_dev: HD-audio core stream to reset 223 */ 223 */ 224 void snd_hdac_stream_reset(struct hdac_stream 224 void snd_hdac_stream_reset(struct hdac_stream *azx_dev) 225 { 225 { 226 unsigned char val; 226 unsigned char val; 227 int dma_run_state; 227 int dma_run_state; 228 228 229 snd_hdac_stream_clear(azx_dev); 229 snd_hdac_stream_clear(azx_dev); 230 230 231 dma_run_state = snd_hdac_stream_readb( 231 dma_run_state = snd_hdac_stream_readb(azx_dev, SD_CTL) & SD_CTL_DMA_START; 232 232 233 snd_hdac_stream_updateb(azx_dev, SD_CT 233 snd_hdac_stream_updateb(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET); 234 234 235 /* wait for hardware to report that th 235 /* wait for hardware to report that the stream entered reset */ 236 snd_hdac_stream_readb_poll(azx_dev, SD 236 snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, (val & SD_CTL_STREAM_RESET), 3, 300); 237 237 238 if (azx_dev->bus->dma_stop_delay && dm 238 if (azx_dev->bus->dma_stop_delay && dma_run_state) 239 udelay(azx_dev->bus->dma_stop_ 239 udelay(azx_dev->bus->dma_stop_delay); 240 240 241 snd_hdac_stream_updateb(azx_dev, SD_CT 241 snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_CTL_STREAM_RESET, 0); 242 242 243 /* wait for hardware to report that th 243 /* wait for hardware to report that the stream is out of reset */ 244 snd_hdac_stream_readb_poll(azx_dev, SD 244 snd_hdac_stream_readb_poll(azx_dev, SD_CTL, val, !(val & SD_CTL_STREAM_RESET), 3, 300); 245 245 246 /* reset first position - may not be s 246 /* reset first position - may not be synced with hw at this time */ 247 if (azx_dev->posbuf) 247 if (azx_dev->posbuf) 248 *azx_dev->posbuf = 0; 248 *azx_dev->posbuf = 0; 249 } 249 } 250 EXPORT_SYMBOL_GPL(snd_hdac_stream_reset); 250 EXPORT_SYMBOL_GPL(snd_hdac_stream_reset); 251 251 252 /** 252 /** 253 * snd_hdac_stream_setup - set up the SD for 253 * snd_hdac_stream_setup - set up the SD for streaming 254 * @azx_dev: HD-audio core stream to set up 254 * @azx_dev: HD-audio core stream to set up 255 * @code_loading: Whether the stream is for PC 255 * @code_loading: Whether the stream is for PCM or code-loading. 256 */ 256 */ 257 int snd_hdac_stream_setup(struct hdac_stream * 257 int snd_hdac_stream_setup(struct hdac_stream *azx_dev, bool code_loading) 258 { 258 { 259 struct hdac_bus *bus = azx_dev->bus; 259 struct hdac_bus *bus = azx_dev->bus; 260 struct snd_pcm_runtime *runtime; 260 struct snd_pcm_runtime *runtime; 261 unsigned int val; 261 unsigned int val; 262 u16 reg; 262 u16 reg; 263 int ret; 263 int ret; 264 264 265 if (azx_dev->substream) 265 if (azx_dev->substream) 266 runtime = azx_dev->substream-> 266 runtime = azx_dev->substream->runtime; 267 else 267 else 268 runtime = NULL; 268 runtime = NULL; 269 /* make sure the run bit is zero for S 269 /* make sure the run bit is zero for SD */ 270 snd_hdac_stream_clear(azx_dev); 270 snd_hdac_stream_clear(azx_dev); 271 /* program the stream_tag */ 271 /* program the stream_tag */ 272 val = snd_hdac_stream_readl(azx_dev, S 272 val = snd_hdac_stream_readl(azx_dev, SD_CTL); 273 val = (val & ~SD_CTL_STREAM_TAG_MASK) 273 val = (val & ~SD_CTL_STREAM_TAG_MASK) | 274 (azx_dev->stream_tag << SD_CTL 274 (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT); 275 if (!bus->snoop) 275 if (!bus->snoop) 276 val |= SD_CTL_TRAFFIC_PRIO; 276 val |= SD_CTL_TRAFFIC_PRIO; 277 snd_hdac_stream_writel(azx_dev, SD_CTL 277 snd_hdac_stream_writel(azx_dev, SD_CTL, val); 278 278 279 /* program the length of samples in cy 279 /* program the length of samples in cyclic buffer */ 280 snd_hdac_stream_writel(azx_dev, SD_CBL 280 snd_hdac_stream_writel(azx_dev, SD_CBL, azx_dev->bufsize); 281 281 282 /* program the stream format */ 282 /* program the stream format */ 283 /* this value needs to be the same as 283 /* this value needs to be the same as the one programmed */ 284 snd_hdac_stream_writew(azx_dev, SD_FOR 284 snd_hdac_stream_writew(azx_dev, SD_FORMAT, azx_dev->format_val); 285 285 286 /* program the stream LVI (last valid 286 /* program the stream LVI (last valid index) of the BDL */ 287 snd_hdac_stream_writew(azx_dev, SD_LVI 287 snd_hdac_stream_writew(azx_dev, SD_LVI, azx_dev->frags - 1); 288 288 289 /* program the BDL address */ 289 /* program the BDL address */ 290 /* lower BDL address */ 290 /* lower BDL address */ 291 snd_hdac_stream_writel(azx_dev, SD_BDL 291 snd_hdac_stream_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr); 292 /* upper BDL address */ 292 /* upper BDL address */ 293 snd_hdac_stream_writel(azx_dev, SD_BDL 293 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 294 upper_32_bits(a 294 upper_32_bits(azx_dev->bdl.addr)); 295 295 296 /* enable the position buffer */ 296 /* enable the position buffer */ 297 if (bus->use_posbuf && bus->posbuf.add 297 if (bus->use_posbuf && bus->posbuf.addr) { 298 if (!(snd_hdac_chip_readl(bus, 298 if (!(snd_hdac_chip_readl(bus, DPLBASE) & AZX_DPLBASE_ENABLE)) 299 snd_hdac_chip_writel(b 299 snd_hdac_chip_writel(bus, DPLBASE, 300 (u32)bus->posb 300 (u32)bus->posbuf.addr | AZX_DPLBASE_ENABLE); 301 } 301 } 302 302 303 /* set the interrupt enable bits in th 303 /* set the interrupt enable bits in the descriptor control register */ 304 snd_hdac_stream_updatel(azx_dev, SD_CT 304 snd_hdac_stream_updatel(azx_dev, SD_CTL, 0, SD_INT_MASK); 305 305 306 if (!code_loading) { 306 if (!code_loading) { 307 /* Once SDxFMT is set, the con 307 /* Once SDxFMT is set, the controller programs SDxFIFOS to non-zero value. */ 308 ret = snd_hdac_stream_readw_po 308 ret = snd_hdac_stream_readw_poll(azx_dev, SD_FIFOSIZE, reg, 309 309 reg & AZX_SD_FIFOSIZE_MASK, 3, 300); 310 if (ret) 310 if (ret) 311 dev_dbg(bus->dev, "pol 311 dev_dbg(bus->dev, "polling SD_FIFOSIZE 0x%04x failed: %d\n", 312 AZX_REG_SD_FIF 312 AZX_REG_SD_FIFOSIZE, ret); 313 azx_dev->fifo_size = reg; 313 azx_dev->fifo_size = reg; 314 } 314 } 315 315 316 /* when LPIB delay correction gives a 316 /* when LPIB delay correction gives a small negative value, 317 * we ignore it; currently set the thr 317 * we ignore it; currently set the threshold statically to 318 * 64 frames 318 * 64 frames 319 */ 319 */ 320 if (runtime && runtime->period_size > 320 if (runtime && runtime->period_size > 64) 321 azx_dev->delay_negative_thresh 321 azx_dev->delay_negative_threshold = 322 -frames_to_bytes(runti 322 -frames_to_bytes(runtime, 64); 323 else 323 else 324 azx_dev->delay_negative_thresh 324 azx_dev->delay_negative_threshold = 0; 325 325 326 /* wallclk has 24Mhz clock source */ 326 /* wallclk has 24Mhz clock source */ 327 if (runtime) 327 if (runtime) 328 azx_dev->period_wallclk = (((r 328 azx_dev->period_wallclk = (((runtime->period_size * 24000) / 329 runtime->r 329 runtime->rate) * 1000); 330 330 331 return 0; 331 return 0; 332 } 332 } 333 EXPORT_SYMBOL_GPL(snd_hdac_stream_setup); 333 EXPORT_SYMBOL_GPL(snd_hdac_stream_setup); 334 334 335 /** 335 /** 336 * snd_hdac_stream_cleanup - cleanup a stream 336 * snd_hdac_stream_cleanup - cleanup a stream 337 * @azx_dev: HD-audio core stream to clean up 337 * @azx_dev: HD-audio core stream to clean up 338 */ 338 */ 339 void snd_hdac_stream_cleanup(struct hdac_strea 339 void snd_hdac_stream_cleanup(struct hdac_stream *azx_dev) 340 { 340 { 341 snd_hdac_stream_writel(azx_dev, SD_BDL 341 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0); 342 snd_hdac_stream_writel(azx_dev, SD_BDL 342 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0); 343 snd_hdac_stream_writel(azx_dev, SD_CTL 343 snd_hdac_stream_writel(azx_dev, SD_CTL, 0); 344 azx_dev->bufsize = 0; 344 azx_dev->bufsize = 0; 345 azx_dev->period_bytes = 0; 345 azx_dev->period_bytes = 0; 346 azx_dev->format_val = 0; 346 azx_dev->format_val = 0; 347 } 347 } 348 EXPORT_SYMBOL_GPL(snd_hdac_stream_cleanup); 348 EXPORT_SYMBOL_GPL(snd_hdac_stream_cleanup); 349 349 350 /** 350 /** 351 * snd_hdac_stream_assign - assign a stream fo 351 * snd_hdac_stream_assign - assign a stream for the PCM 352 * @bus: HD-audio core bus 352 * @bus: HD-audio core bus 353 * @substream: PCM substream to assign 353 * @substream: PCM substream to assign 354 * 354 * 355 * Look for an unused stream for the given PCM 355 * Look for an unused stream for the given PCM substream, assign it 356 * and return the stream object. If no stream 356 * and return the stream object. If no stream is free, returns NULL. 357 * The function tries to keep using the same s 357 * The function tries to keep using the same stream object when it's used 358 * beforehand. Also, when bus->reverse_assign 358 * beforehand. Also, when bus->reverse_assign flag is set, the last free 359 * or matching entry is returned. This is nee 359 * or matching entry is returned. This is needed for some strange codecs. 360 */ 360 */ 361 struct hdac_stream *snd_hdac_stream_assign(str 361 struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus, 362 str 362 struct snd_pcm_substream *substream) 363 { 363 { 364 struct hdac_stream *azx_dev; 364 struct hdac_stream *azx_dev; 365 struct hdac_stream *res = NULL; 365 struct hdac_stream *res = NULL; 366 366 367 /* make a non-zero unique key for the 367 /* make a non-zero unique key for the substream */ 368 int key = (substream->number << 2) | ( 368 int key = (substream->number << 2) | (substream->stream + 1); 369 369 370 if (substream->pcm) 370 if (substream->pcm) 371 key |= (substream->pcm->device 371 key |= (substream->pcm->device << 16); 372 372 373 spin_lock_irq(&bus->reg_lock); 373 spin_lock_irq(&bus->reg_lock); 374 list_for_each_entry(azx_dev, &bus->str 374 list_for_each_entry(azx_dev, &bus->stream_list, list) { 375 if (azx_dev->direction != subs 375 if (azx_dev->direction != substream->stream) 376 continue; 376 continue; 377 if (azx_dev->opened) 377 if (azx_dev->opened) 378 continue; 378 continue; 379 if (azx_dev->assigned_key == k 379 if (azx_dev->assigned_key == key) { 380 res = azx_dev; 380 res = azx_dev; 381 break; 381 break; 382 } 382 } 383 if (!res || bus->reverse_assig 383 if (!res || bus->reverse_assign) 384 res = azx_dev; 384 res = azx_dev; 385 } 385 } 386 if (res) { 386 if (res) { 387 res->opened = 1; 387 res->opened = 1; 388 res->running = 0; 388 res->running = 0; 389 res->assigned_key = key; 389 res->assigned_key = key; 390 res->substream = substream; 390 res->substream = substream; 391 } 391 } 392 spin_unlock_irq(&bus->reg_lock); 392 spin_unlock_irq(&bus->reg_lock); 393 return res; 393 return res; 394 } 394 } 395 EXPORT_SYMBOL_GPL(snd_hdac_stream_assign); 395 EXPORT_SYMBOL_GPL(snd_hdac_stream_assign); 396 396 397 /** 397 /** 398 * snd_hdac_stream_release_locked - release th 398 * snd_hdac_stream_release_locked - release the assigned stream 399 * @azx_dev: HD-audio core stream to release 399 * @azx_dev: HD-audio core stream to release 400 * 400 * 401 * Release the stream that has been assigned b 401 * Release the stream that has been assigned by snd_hdac_stream_assign(). 402 * The bus->reg_lock needs to be taken at a hi 402 * The bus->reg_lock needs to be taken at a higher level 403 */ 403 */ 404 void snd_hdac_stream_release_locked(struct hda 404 void snd_hdac_stream_release_locked(struct hdac_stream *azx_dev) 405 { 405 { 406 azx_dev->opened = 0; 406 azx_dev->opened = 0; 407 azx_dev->running = 0; 407 azx_dev->running = 0; 408 azx_dev->substream = NULL; 408 azx_dev->substream = NULL; 409 } 409 } 410 EXPORT_SYMBOL_GPL(snd_hdac_stream_release_lock 410 EXPORT_SYMBOL_GPL(snd_hdac_stream_release_locked); 411 411 412 /** 412 /** 413 * snd_hdac_stream_release - release the assig 413 * snd_hdac_stream_release - release the assigned stream 414 * @azx_dev: HD-audio core stream to release 414 * @azx_dev: HD-audio core stream to release 415 * 415 * 416 * Release the stream that has been assigned b 416 * Release the stream that has been assigned by snd_hdac_stream_assign(). 417 */ 417 */ 418 void snd_hdac_stream_release(struct hdac_strea 418 void snd_hdac_stream_release(struct hdac_stream *azx_dev) 419 { 419 { 420 struct hdac_bus *bus = azx_dev->bus; 420 struct hdac_bus *bus = azx_dev->bus; 421 421 422 spin_lock_irq(&bus->reg_lock); 422 spin_lock_irq(&bus->reg_lock); 423 snd_hdac_stream_release_locked(azx_dev 423 snd_hdac_stream_release_locked(azx_dev); 424 spin_unlock_irq(&bus->reg_lock); 424 spin_unlock_irq(&bus->reg_lock); 425 } 425 } 426 EXPORT_SYMBOL_GPL(snd_hdac_stream_release); 426 EXPORT_SYMBOL_GPL(snd_hdac_stream_release); 427 427 428 /** 428 /** 429 * snd_hdac_get_stream - return hdac_stream ba 429 * snd_hdac_get_stream - return hdac_stream based on stream_tag and 430 * direction 430 * direction 431 * 431 * 432 * @bus: HD-audio core bus 432 * @bus: HD-audio core bus 433 * @dir: direction for the stream to be found 433 * @dir: direction for the stream to be found 434 * @stream_tag: stream tag for stream to be fo 434 * @stream_tag: stream tag for stream to be found 435 */ 435 */ 436 struct hdac_stream *snd_hdac_get_stream(struct 436 struct hdac_stream *snd_hdac_get_stream(struct hdac_bus *bus, 437 int di 437 int dir, int stream_tag) 438 { 438 { 439 struct hdac_stream *s; 439 struct hdac_stream *s; 440 440 441 list_for_each_entry(s, &bus->stream_li 441 list_for_each_entry(s, &bus->stream_list, list) { 442 if (s->direction == dir && s-> 442 if (s->direction == dir && s->stream_tag == stream_tag) 443 return s; 443 return s; 444 } 444 } 445 445 446 return NULL; 446 return NULL; 447 } 447 } 448 EXPORT_SYMBOL_GPL(snd_hdac_get_stream); 448 EXPORT_SYMBOL_GPL(snd_hdac_get_stream); 449 449 450 /* 450 /* 451 * set up a BDL entry 451 * set up a BDL entry 452 */ 452 */ 453 static int setup_bdle(struct hdac_bus *bus, 453 static int setup_bdle(struct hdac_bus *bus, 454 struct snd_dma_buffer *d 454 struct snd_dma_buffer *dmab, 455 struct hdac_stream *azx_ 455 struct hdac_stream *azx_dev, __le32 **bdlp, 456 int ofs, int size, int w 456 int ofs, int size, int with_ioc) 457 { 457 { 458 __le32 *bdl = *bdlp; 458 __le32 *bdl = *bdlp; 459 459 460 while (size > 0) { 460 while (size > 0) { 461 dma_addr_t addr; 461 dma_addr_t addr; 462 int chunk; 462 int chunk; 463 463 464 if (azx_dev->frags >= AZX_MAX_ 464 if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES) 465 return -EINVAL; 465 return -EINVAL; 466 466 467 addr = snd_sgbuf_get_addr(dmab 467 addr = snd_sgbuf_get_addr(dmab, ofs); 468 /* program the address field o 468 /* program the address field of the BDL entry */ 469 bdl[0] = cpu_to_le32((u32)addr 469 bdl[0] = cpu_to_le32((u32)addr); 470 bdl[1] = cpu_to_le32(upper_32_ 470 bdl[1] = cpu_to_le32(upper_32_bits(addr)); 471 /* program the size field of t 471 /* program the size field of the BDL entry */ 472 chunk = snd_sgbuf_get_chunk_si 472 chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size); 473 /* one BDLE cannot cross 4K bo 473 /* one BDLE cannot cross 4K boundary on CTHDA chips */ 474 if (bus->align_bdle_4k) { 474 if (bus->align_bdle_4k) { 475 u32 remain = 0x1000 - 475 u32 remain = 0x1000 - (ofs & 0xfff); 476 476 477 if (chunk > remain) 477 if (chunk > remain) 478 chunk = remain 478 chunk = remain; 479 } 479 } 480 bdl[2] = cpu_to_le32(chunk); 480 bdl[2] = cpu_to_le32(chunk); 481 /* program the IOC to enable i 481 /* program the IOC to enable interrupt 482 * only when the whole fragmen 482 * only when the whole fragment is processed 483 */ 483 */ 484 size -= chunk; 484 size -= chunk; 485 bdl[3] = (size || !with_ioc) ? 485 bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01); 486 bdl += 4; 486 bdl += 4; 487 azx_dev->frags++; 487 azx_dev->frags++; 488 ofs += chunk; 488 ofs += chunk; 489 } 489 } 490 *bdlp = bdl; 490 *bdlp = bdl; 491 return ofs; 491 return ofs; 492 } 492 } 493 493 494 /** 494 /** 495 * snd_hdac_stream_setup_periods - set up BDL 495 * snd_hdac_stream_setup_periods - set up BDL entries 496 * @azx_dev: HD-audio core stream to set up 496 * @azx_dev: HD-audio core stream to set up 497 * 497 * 498 * Set up the buffer descriptor table of the g 498 * Set up the buffer descriptor table of the given stream based on the 499 * period and buffer sizes of the assigned PCM 499 * period and buffer sizes of the assigned PCM substream. 500 */ 500 */ 501 int snd_hdac_stream_setup_periods(struct hdac_ 501 int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev) 502 { 502 { 503 struct hdac_bus *bus = azx_dev->bus; 503 struct hdac_bus *bus = azx_dev->bus; 504 struct snd_pcm_substream *substream = 504 struct snd_pcm_substream *substream = azx_dev->substream; 505 struct snd_compr_stream *cstream = azx 505 struct snd_compr_stream *cstream = azx_dev->cstream; 506 struct snd_pcm_runtime *runtime = NULL 506 struct snd_pcm_runtime *runtime = NULL; 507 struct snd_dma_buffer *dmab; 507 struct snd_dma_buffer *dmab; 508 __le32 *bdl; 508 __le32 *bdl; 509 int i, ofs, periods, period_bytes; 509 int i, ofs, periods, period_bytes; 510 int pos_adj, pos_align; 510 int pos_adj, pos_align; 511 511 512 if (substream) { 512 if (substream) { 513 runtime = substream->runtime; 513 runtime = substream->runtime; 514 dmab = snd_pcm_get_dma_buf(sub 514 dmab = snd_pcm_get_dma_buf(substream); 515 } else if (cstream) { 515 } else if (cstream) { 516 dmab = snd_pcm_get_dma_buf(cst 516 dmab = snd_pcm_get_dma_buf(cstream); 517 } else { 517 } else { 518 WARN(1, "No substream or cstre 518 WARN(1, "No substream or cstream assigned\n"); 519 return -EINVAL; 519 return -EINVAL; 520 } 520 } 521 521 522 /* reset BDL address */ 522 /* reset BDL address */ 523 snd_hdac_stream_writel(azx_dev, SD_BDL 523 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0); 524 snd_hdac_stream_writel(azx_dev, SD_BDL 524 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0); 525 525 526 period_bytes = azx_dev->period_bytes; 526 period_bytes = azx_dev->period_bytes; 527 periods = azx_dev->bufsize / period_by 527 periods = azx_dev->bufsize / period_bytes; 528 528 529 /* program the initial BDL entries */ 529 /* program the initial BDL entries */ 530 bdl = (__le32 *)azx_dev->bdl.area; 530 bdl = (__le32 *)azx_dev->bdl.area; 531 ofs = 0; 531 ofs = 0; 532 azx_dev->frags = 0; 532 azx_dev->frags = 0; 533 533 534 pos_adj = bus->bdl_pos_adj; 534 pos_adj = bus->bdl_pos_adj; 535 if (runtime && !azx_dev->no_period_wak 535 if (runtime && !azx_dev->no_period_wakeup && pos_adj > 0) { 536 pos_align = pos_adj; 536 pos_align = pos_adj; 537 pos_adj = DIV_ROUND_UP(pos_adj 537 pos_adj = DIV_ROUND_UP(pos_adj * runtime->rate, 48000); 538 if (!pos_adj) 538 if (!pos_adj) 539 pos_adj = pos_align; 539 pos_adj = pos_align; 540 else 540 else 541 pos_adj = roundup(pos_ 541 pos_adj = roundup(pos_adj, pos_align); 542 pos_adj = frames_to_bytes(runt 542 pos_adj = frames_to_bytes(runtime, pos_adj); 543 if (pos_adj >= period_bytes) { 543 if (pos_adj >= period_bytes) { 544 dev_warn(bus->dev, "To 544 dev_warn(bus->dev, "Too big adjustment %d\n", 545 pos_adj); 545 pos_adj); 546 pos_adj = 0; 546 pos_adj = 0; 547 } else { 547 } else { 548 ofs = setup_bdle(bus, 548 ofs = setup_bdle(bus, dmab, azx_dev, 549 &bdl, 549 &bdl, ofs, pos_adj, true); 550 if (ofs < 0) 550 if (ofs < 0) 551 goto error; 551 goto error; 552 } 552 } 553 } else 553 } else 554 pos_adj = 0; 554 pos_adj = 0; 555 555 556 for (i = 0; i < periods; i++) { 556 for (i = 0; i < periods; i++) { 557 if (i == periods - 1 && pos_ad 557 if (i == periods - 1 && pos_adj) 558 ofs = setup_bdle(bus, 558 ofs = setup_bdle(bus, dmab, azx_dev, 559 &bdl, 559 &bdl, ofs, period_bytes - pos_adj, 0); 560 else 560 else 561 ofs = setup_bdle(bus, 561 ofs = setup_bdle(bus, dmab, azx_dev, 562 &bdl, 562 &bdl, ofs, period_bytes, 563 !azx_ 563 !azx_dev->no_period_wakeup); 564 if (ofs < 0) 564 if (ofs < 0) 565 goto error; 565 goto error; 566 } 566 } 567 return 0; 567 return 0; 568 568 569 error: 569 error: 570 dev_dbg(bus->dev, "Too many BDL entrie 570 dev_dbg(bus->dev, "Too many BDL entries: buffer=%d, period=%d\n", 571 azx_dev->bufsize, period_bytes 571 azx_dev->bufsize, period_bytes); 572 return -EINVAL; 572 return -EINVAL; 573 } 573 } 574 EXPORT_SYMBOL_GPL(snd_hdac_stream_setup_period 574 EXPORT_SYMBOL_GPL(snd_hdac_stream_setup_periods); 575 575 576 /** 576 /** 577 * snd_hdac_stream_set_params - set stream par 577 * snd_hdac_stream_set_params - set stream parameters 578 * @azx_dev: HD-audio core stream for which pa 578 * @azx_dev: HD-audio core stream for which parameters are to be set 579 * @format_val: format value parameter 579 * @format_val: format value parameter 580 * 580 * 581 * Setup the HD-audio core stream parameters f 581 * Setup the HD-audio core stream parameters from substream of the stream 582 * and passed format value 582 * and passed format value 583 */ 583 */ 584 int snd_hdac_stream_set_params(struct hdac_str 584 int snd_hdac_stream_set_params(struct hdac_stream *azx_dev, 585 unsigned int 585 unsigned int format_val) 586 { 586 { 587 struct snd_pcm_substream *substream = 587 struct snd_pcm_substream *substream = azx_dev->substream; 588 struct snd_compr_stream *cstream = azx 588 struct snd_compr_stream *cstream = azx_dev->cstream; 589 unsigned int bufsize, period_bytes; 589 unsigned int bufsize, period_bytes; 590 unsigned int no_period_wakeup; 590 unsigned int no_period_wakeup; 591 int err; 591 int err; 592 592 593 if (substream) { 593 if (substream) { 594 bufsize = snd_pcm_lib_buffer_b 594 bufsize = snd_pcm_lib_buffer_bytes(substream); 595 period_bytes = snd_pcm_lib_per 595 period_bytes = snd_pcm_lib_period_bytes(substream); 596 no_period_wakeup = substream-> 596 no_period_wakeup = substream->runtime->no_period_wakeup; 597 } else if (cstream) { 597 } else if (cstream) { 598 bufsize = cstream->runtime->bu 598 bufsize = cstream->runtime->buffer_size; 599 period_bytes = cstream->runtim 599 period_bytes = cstream->runtime->fragment_size; 600 no_period_wakeup = 0; 600 no_period_wakeup = 0; 601 } else { 601 } else { 602 return -EINVAL; 602 return -EINVAL; 603 } 603 } 604 604 605 if (bufsize != azx_dev->bufsize || 605 if (bufsize != azx_dev->bufsize || 606 period_bytes != azx_dev->period_by 606 period_bytes != azx_dev->period_bytes || 607 format_val != azx_dev->format_val 607 format_val != azx_dev->format_val || 608 no_period_wakeup != azx_dev->no_pe 608 no_period_wakeup != azx_dev->no_period_wakeup) { 609 azx_dev->bufsize = bufsize; 609 azx_dev->bufsize = bufsize; 610 azx_dev->period_bytes = period 610 azx_dev->period_bytes = period_bytes; 611 azx_dev->format_val = format_v 611 azx_dev->format_val = format_val; 612 azx_dev->no_period_wakeup = no 612 azx_dev->no_period_wakeup = no_period_wakeup; 613 err = snd_hdac_stream_setup_pe 613 err = snd_hdac_stream_setup_periods(azx_dev); 614 if (err < 0) 614 if (err < 0) 615 return err; 615 return err; 616 } 616 } 617 return 0; 617 return 0; 618 } 618 } 619 EXPORT_SYMBOL_GPL(snd_hdac_stream_set_params); 619 EXPORT_SYMBOL_GPL(snd_hdac_stream_set_params); 620 620 621 static u64 azx_cc_read(const struct cyclecount 621 static u64 azx_cc_read(const struct cyclecounter *cc) 622 { 622 { 623 struct hdac_stream *azx_dev = containe 623 struct hdac_stream *azx_dev = container_of(cc, struct hdac_stream, cc); 624 624 625 return snd_hdac_chip_readl(azx_dev->bu 625 return snd_hdac_chip_readl(azx_dev->bus, WALLCLK); 626 } 626 } 627 627 628 static void azx_timecounter_init(struct hdac_s 628 static void azx_timecounter_init(struct hdac_stream *azx_dev, 629 bool force, u 629 bool force, u64 last) 630 { 630 { 631 struct timecounter *tc = &azx_dev->tc; 631 struct timecounter *tc = &azx_dev->tc; 632 struct cyclecounter *cc = &azx_dev->cc 632 struct cyclecounter *cc = &azx_dev->cc; 633 u64 nsec; 633 u64 nsec; 634 634 635 cc->read = azx_cc_read; 635 cc->read = azx_cc_read; 636 cc->mask = CLOCKSOURCE_MASK(32); 636 cc->mask = CLOCKSOURCE_MASK(32); 637 637 638 /* 638 /* 639 * Calculate the optimal mult/shift va 639 * Calculate the optimal mult/shift values. The counter wraps 640 * around after ~178.9 seconds. 640 * around after ~178.9 seconds. 641 */ 641 */ 642 clocks_calc_mult_shift(&cc->mult, &cc- 642 clocks_calc_mult_shift(&cc->mult, &cc->shift, 24000000, 643 NSEC_PER_SEC, 1 643 NSEC_PER_SEC, 178); 644 644 645 nsec = 0; /* audio time is elapsed tim 645 nsec = 0; /* audio time is elapsed time since trigger */ 646 timecounter_init(tc, cc, nsec); 646 timecounter_init(tc, cc, nsec); 647 if (force) { 647 if (force) { 648 /* 648 /* 649 * force timecounter to use pr 649 * force timecounter to use predefined value, 650 * used for synchronized start 650 * used for synchronized starts 651 */ 651 */ 652 tc->cycle_last = last; 652 tc->cycle_last = last; 653 } 653 } 654 } 654 } 655 655 656 /** 656 /** 657 * snd_hdac_stream_timecounter_init - initiali 657 * snd_hdac_stream_timecounter_init - initialize time counter 658 * @azx_dev: HD-audio core stream (master stre 658 * @azx_dev: HD-audio core stream (master stream) 659 * @streams: bit flags of streams to set up 659 * @streams: bit flags of streams to set up 660 * @start: true for PCM trigger start, false f 660 * @start: true for PCM trigger start, false for other cases 661 * 661 * 662 * Initializes the time counter of streams mar 662 * Initializes the time counter of streams marked by the bit flags (each 663 * bit corresponds to the stream index). 663 * bit corresponds to the stream index). 664 * The trigger timestamp of PCM substream assi 664 * The trigger timestamp of PCM substream assigned to the given stream is 665 * updated accordingly, too. 665 * updated accordingly, too. 666 */ 666 */ 667 void snd_hdac_stream_timecounter_init(struct h 667 void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev, 668 unsigned 668 unsigned int streams, bool start) 669 { 669 { 670 struct hdac_bus *bus = azx_dev->bus; 670 struct hdac_bus *bus = azx_dev->bus; 671 struct snd_pcm_runtime *runtime = azx_ 671 struct snd_pcm_runtime *runtime = azx_dev->substream->runtime; 672 struct hdac_stream *s; 672 struct hdac_stream *s; 673 bool inited = false; 673 bool inited = false; 674 u64 cycle_last = 0; 674 u64 cycle_last = 0; 675 675 676 if (!start) 676 if (!start) 677 goto skip; 677 goto skip; 678 678 679 list_for_each_entry(s, &bus->stream_li 679 list_for_each_entry(s, &bus->stream_list, list) { 680 if ((streams & (1 << s->index) 680 if ((streams & (1 << s->index))) { 681 azx_timecounter_init(s 681 azx_timecounter_init(s, inited, cycle_last); 682 if (!inited) { 682 if (!inited) { 683 inited = true; 683 inited = true; 684 cycle_last = s 684 cycle_last = s->tc.cycle_last; 685 } 685 } 686 } 686 } 687 } 687 } 688 688 689 skip: 689 skip: 690 snd_pcm_gettime(runtime, &runtime->tri 690 snd_pcm_gettime(runtime, &runtime->trigger_tstamp); 691 runtime->trigger_tstamp_latched = true 691 runtime->trigger_tstamp_latched = true; 692 } 692 } 693 EXPORT_SYMBOL_GPL(snd_hdac_stream_timecounter_ 693 EXPORT_SYMBOL_GPL(snd_hdac_stream_timecounter_init); 694 694 695 /** 695 /** 696 * snd_hdac_stream_sync_trigger - turn on/off 696 * snd_hdac_stream_sync_trigger - turn on/off stream sync register 697 * @azx_dev: HD-audio core stream (master stre 697 * @azx_dev: HD-audio core stream (master stream) 698 * @set: true = set, false = clear 698 * @set: true = set, false = clear 699 * @streams: bit flags of streams to sync 699 * @streams: bit flags of streams to sync 700 * @reg: the stream sync register address 700 * @reg: the stream sync register address 701 */ 701 */ 702 void snd_hdac_stream_sync_trigger(struct hdac_ 702 void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set, 703 unsigned int 703 unsigned int streams, unsigned int reg) 704 { 704 { 705 struct hdac_bus *bus = azx_dev->bus; 705 struct hdac_bus *bus = azx_dev->bus; 706 unsigned int val; 706 unsigned int val; 707 707 708 if (!reg) 708 if (!reg) 709 reg = AZX_REG_SSYNC; 709 reg = AZX_REG_SSYNC; 710 val = _snd_hdac_chip_readl(bus, reg); 710 val = _snd_hdac_chip_readl(bus, reg); 711 if (set) 711 if (set) 712 val |= streams; 712 val |= streams; 713 else 713 else 714 val &= ~streams; 714 val &= ~streams; 715 _snd_hdac_chip_writel(bus, reg, val); 715 _snd_hdac_chip_writel(bus, reg, val); 716 } 716 } 717 EXPORT_SYMBOL_GPL(snd_hdac_stream_sync_trigger 717 EXPORT_SYMBOL_GPL(snd_hdac_stream_sync_trigger); 718 718 719 /** 719 /** 720 * snd_hdac_stream_sync - sync with start/stop 720 * snd_hdac_stream_sync - sync with start/stop trigger operation 721 * @azx_dev: HD-audio core stream (master stre 721 * @azx_dev: HD-audio core stream (master stream) 722 * @start: true = start, false = stop 722 * @start: true = start, false = stop 723 * @streams: bit flags of streams to sync 723 * @streams: bit flags of streams to sync 724 * 724 * 725 * For @start = true, wait until all FIFOs get 725 * For @start = true, wait until all FIFOs get ready. 726 * For @start = false, wait until all RUN bits 726 * For @start = false, wait until all RUN bits are cleared. 727 */ 727 */ 728 void snd_hdac_stream_sync(struct hdac_stream * 728 void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start, 729 unsigned int streams 729 unsigned int streams) 730 { 730 { 731 struct hdac_bus *bus = azx_dev->bus; 731 struct hdac_bus *bus = azx_dev->bus; 732 int nwait, timeout; 732 int nwait, timeout; 733 struct hdac_stream *s; 733 struct hdac_stream *s; 734 734 735 for (timeout = 5000; timeout; timeout- 735 for (timeout = 5000; timeout; timeout--) { 736 nwait = 0; 736 nwait = 0; 737 list_for_each_entry(s, &bus->s 737 list_for_each_entry(s, &bus->stream_list, list) { 738 if (!(streams & (1 << 738 if (!(streams & (1 << s->index))) 739 continue; 739 continue; 740 740 741 if (start) { 741 if (start) { 742 /* check FIFO 742 /* check FIFO gets ready */ 743 if (!(snd_hdac 743 if (!(snd_hdac_stream_readb(s, SD_STS) & 744 SD_STS_F 744 SD_STS_FIFO_READY)) 745 nwait+ 745 nwait++; 746 } else { 746 } else { 747 /* check RUN b 747 /* check RUN bit is cleared */ 748 if (snd_hdac_s 748 if (snd_hdac_stream_readb(s, SD_CTL) & 749 SD_CTL_DMA 749 SD_CTL_DMA_START) { 750 nwait+ 750 nwait++; 751 /* 751 /* 752 * Per 752 * Perform stream reset if DMA RUN 753 * bit 753 * bit not cleared within given timeout 754 */ 754 */ 755 if (ti 755 if (timeout == 1) 756 756 snd_hdac_stream_reset(s); 757 } 757 } 758 } 758 } 759 } 759 } 760 if (!nwait) 760 if (!nwait) 761 break; 761 break; 762 cpu_relax(); 762 cpu_relax(); 763 } 763 } 764 } 764 } 765 EXPORT_SYMBOL_GPL(snd_hdac_stream_sync); 765 EXPORT_SYMBOL_GPL(snd_hdac_stream_sync); 766 766 767 /** 767 /** 768 * snd_hdac_stream_spbcap_enable - enable SPIB 768 * snd_hdac_stream_spbcap_enable - enable SPIB for a stream 769 * @bus: HD-audio core bus 769 * @bus: HD-audio core bus 770 * @enable: flag to enable/disable SPIB 770 * @enable: flag to enable/disable SPIB 771 * @index: stream index for which SPIB need to 771 * @index: stream index for which SPIB need to be enabled 772 */ 772 */ 773 void snd_hdac_stream_spbcap_enable(struct hdac 773 void snd_hdac_stream_spbcap_enable(struct hdac_bus *bus, 774 bool enable 774 bool enable, int index) 775 { 775 { 776 u32 mask = 0; 776 u32 mask = 0; 777 777 778 if (!bus->spbcap) { 778 if (!bus->spbcap) { 779 dev_err(bus->dev, "Address of 779 dev_err(bus->dev, "Address of SPB capability is NULL\n"); 780 return; 780 return; 781 } 781 } 782 782 783 mask |= (1 << index); 783 mask |= (1 << index); 784 784 785 if (enable) 785 if (enable) 786 snd_hdac_updatel(bus->spbcap, 786 snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, mask); 787 else 787 else 788 snd_hdac_updatel(bus->spbcap, 788 snd_hdac_updatel(bus->spbcap, AZX_REG_SPB_SPBFCCTL, mask, 0); 789 } 789 } 790 EXPORT_SYMBOL_GPL(snd_hdac_stream_spbcap_enabl 790 EXPORT_SYMBOL_GPL(snd_hdac_stream_spbcap_enable); 791 791 792 /** 792 /** 793 * snd_hdac_stream_set_spib - sets the spib va 793 * snd_hdac_stream_set_spib - sets the spib value of a stream 794 * @bus: HD-audio core bus 794 * @bus: HD-audio core bus 795 * @azx_dev: hdac_stream 795 * @azx_dev: hdac_stream 796 * @value: spib value to set 796 * @value: spib value to set 797 */ 797 */ 798 int snd_hdac_stream_set_spib(struct hdac_bus * 798 int snd_hdac_stream_set_spib(struct hdac_bus *bus, 799 struct hdac_strea 799 struct hdac_stream *azx_dev, u32 value) 800 { 800 { 801 if (!bus->spbcap) { 801 if (!bus->spbcap) { 802 dev_err(bus->dev, "Address of 802 dev_err(bus->dev, "Address of SPB capability is NULL\n"); 803 return -EINVAL; 803 return -EINVAL; 804 } 804 } 805 805 806 writel(value, azx_dev->spib_addr); 806 writel(value, azx_dev->spib_addr); 807 807 808 return 0; 808 return 0; 809 } 809 } 810 EXPORT_SYMBOL_GPL(snd_hdac_stream_set_spib); 810 EXPORT_SYMBOL_GPL(snd_hdac_stream_set_spib); 811 811 812 /** 812 /** 813 * snd_hdac_stream_get_spbmaxfifo - gets the s 813 * snd_hdac_stream_get_spbmaxfifo - gets the spib value of a stream 814 * @bus: HD-audio core bus 814 * @bus: HD-audio core bus 815 * @azx_dev: hdac_stream 815 * @azx_dev: hdac_stream 816 * 816 * 817 * Return maxfifo for the stream 817 * Return maxfifo for the stream 818 */ 818 */ 819 int snd_hdac_stream_get_spbmaxfifo(struct hdac 819 int snd_hdac_stream_get_spbmaxfifo(struct hdac_bus *bus, 820 struct hdac 820 struct hdac_stream *azx_dev) 821 { 821 { 822 if (!bus->spbcap) { 822 if (!bus->spbcap) { 823 dev_err(bus->dev, "Address of 823 dev_err(bus->dev, "Address of SPB capability is NULL\n"); 824 return -EINVAL; 824 return -EINVAL; 825 } 825 } 826 826 827 return readl(azx_dev->fifo_addr); 827 return readl(azx_dev->fifo_addr); 828 } 828 } 829 EXPORT_SYMBOL_GPL(snd_hdac_stream_get_spbmaxfi 829 EXPORT_SYMBOL_GPL(snd_hdac_stream_get_spbmaxfifo); 830 830 831 /** 831 /** 832 * snd_hdac_stream_drsm_enable - enable DMA re 832 * snd_hdac_stream_drsm_enable - enable DMA resume for a stream 833 * @bus: HD-audio core bus 833 * @bus: HD-audio core bus 834 * @enable: flag to enable/disable DRSM 834 * @enable: flag to enable/disable DRSM 835 * @index: stream index for which DRSM need to 835 * @index: stream index for which DRSM need to be enabled 836 */ 836 */ 837 void snd_hdac_stream_drsm_enable(struct hdac_b 837 void snd_hdac_stream_drsm_enable(struct hdac_bus *bus, 838 bool enable, 838 bool enable, int index) 839 { 839 { 840 u32 mask = 0; 840 u32 mask = 0; 841 841 842 if (!bus->drsmcap) { 842 if (!bus->drsmcap) { 843 dev_err(bus->dev, "Address of 843 dev_err(bus->dev, "Address of DRSM capability is NULL\n"); 844 return; 844 return; 845 } 845 } 846 846 847 mask |= (1 << index); 847 mask |= (1 << index); 848 848 849 if (enable) 849 if (enable) 850 snd_hdac_updatel(bus->drsmcap, 850 snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, mask); 851 else 851 else 852 snd_hdac_updatel(bus->drsmcap, 852 snd_hdac_updatel(bus->drsmcap, AZX_REG_DRSM_CTL, mask, 0); 853 } 853 } 854 EXPORT_SYMBOL_GPL(snd_hdac_stream_drsm_enable) 854 EXPORT_SYMBOL_GPL(snd_hdac_stream_drsm_enable); 855 855 856 /* 856 /* 857 * snd_hdac_stream_wait_drsm - wait for HW to 857 * snd_hdac_stream_wait_drsm - wait for HW to clear RSM for a stream 858 * @azx_dev: HD-audio core stream to await RSM 858 * @azx_dev: HD-audio core stream to await RSM for 859 * 859 * 860 * Returns 0 on success and -ETIMEDOUT upon a 860 * Returns 0 on success and -ETIMEDOUT upon a timeout. 861 */ 861 */ 862 int snd_hdac_stream_wait_drsm(struct hdac_stre 862 int snd_hdac_stream_wait_drsm(struct hdac_stream *azx_dev) 863 { 863 { 864 struct hdac_bus *bus = azx_dev->bus; 864 struct hdac_bus *bus = azx_dev->bus; 865 u32 mask, reg; 865 u32 mask, reg; 866 int ret; 866 int ret; 867 867 868 mask = 1 << azx_dev->index; 868 mask = 1 << azx_dev->index; 869 869 870 ret = read_poll_timeout(snd_hdac_reg_r 870 ret = read_poll_timeout(snd_hdac_reg_readl, reg, !(reg & mask), 250, 2000, false, bus, 871 bus->drsmcap + 871 bus->drsmcap + AZX_REG_DRSM_CTL); 872 if (ret) 872 if (ret) 873 dev_dbg(bus->dev, "polling RSM 873 dev_dbg(bus->dev, "polling RSM 0x%08x failed: %d\n", mask, ret); 874 return ret; 874 return ret; 875 } 875 } 876 EXPORT_SYMBOL_GPL(snd_hdac_stream_wait_drsm); 876 EXPORT_SYMBOL_GPL(snd_hdac_stream_wait_drsm); 877 877 878 /** 878 /** 879 * snd_hdac_stream_set_dpibr - sets the dpibr 879 * snd_hdac_stream_set_dpibr - sets the dpibr value of a stream 880 * @bus: HD-audio core bus 880 * @bus: HD-audio core bus 881 * @azx_dev: hdac_stream 881 * @azx_dev: hdac_stream 882 * @value: dpib value to set 882 * @value: dpib value to set 883 */ 883 */ 884 int snd_hdac_stream_set_dpibr(struct hdac_bus 884 int snd_hdac_stream_set_dpibr(struct hdac_bus *bus, 885 struct hdac_stre 885 struct hdac_stream *azx_dev, u32 value) 886 { 886 { 887 if (!bus->drsmcap) { 887 if (!bus->drsmcap) { 888 dev_err(bus->dev, "Address of 888 dev_err(bus->dev, "Address of DRSM capability is NULL\n"); 889 return -EINVAL; 889 return -EINVAL; 890 } 890 } 891 891 892 writel(value, azx_dev->dpibr_addr); 892 writel(value, azx_dev->dpibr_addr); 893 893 894 return 0; 894 return 0; 895 } 895 } 896 EXPORT_SYMBOL_GPL(snd_hdac_stream_set_dpibr); 896 EXPORT_SYMBOL_GPL(snd_hdac_stream_set_dpibr); 897 897 898 /** 898 /** 899 * snd_hdac_stream_set_lpib - sets the lpib va 899 * snd_hdac_stream_set_lpib - sets the lpib value of a stream 900 * @azx_dev: hdac_stream 900 * @azx_dev: hdac_stream 901 * @value: lpib value to set 901 * @value: lpib value to set 902 */ 902 */ 903 int snd_hdac_stream_set_lpib(struct hdac_strea 903 int snd_hdac_stream_set_lpib(struct hdac_stream *azx_dev, u32 value) 904 { 904 { 905 snd_hdac_stream_writel(azx_dev, SD_LPI 905 snd_hdac_stream_writel(azx_dev, SD_LPIB, value); 906 906 907 return 0; 907 return 0; 908 } 908 } 909 EXPORT_SYMBOL_GPL(snd_hdac_stream_set_lpib); 909 EXPORT_SYMBOL_GPL(snd_hdac_stream_set_lpib); 910 910 911 #ifdef CONFIG_SND_HDA_DSP_LOADER 911 #ifdef CONFIG_SND_HDA_DSP_LOADER 912 /** 912 /** 913 * snd_hdac_dsp_prepare - prepare for DSP load 913 * snd_hdac_dsp_prepare - prepare for DSP loading 914 * @azx_dev: HD-audio core stream used for DSP 914 * @azx_dev: HD-audio core stream used for DSP loading 915 * @format: HD-audio stream format 915 * @format: HD-audio stream format 916 * @byte_size: data chunk byte size 916 * @byte_size: data chunk byte size 917 * @bufp: allocated buffer 917 * @bufp: allocated buffer 918 * 918 * 919 * Allocate the buffer for the given size and 919 * Allocate the buffer for the given size and set up the given stream for 920 * DSP loading. Returns the stream tag (>= 0) 920 * DSP loading. Returns the stream tag (>= 0), or a negative error code. 921 */ 921 */ 922 int snd_hdac_dsp_prepare(struct hdac_stream *a 922 int snd_hdac_dsp_prepare(struct hdac_stream *azx_dev, unsigned int format, 923 unsigned int byte_siz 923 unsigned int byte_size, struct snd_dma_buffer *bufp) 924 { 924 { 925 struct hdac_bus *bus = azx_dev->bus; 925 struct hdac_bus *bus = azx_dev->bus; 926 __le32 *bdl; 926 __le32 *bdl; 927 int err; 927 int err; 928 928 929 snd_hdac_dsp_lock(azx_dev); 929 snd_hdac_dsp_lock(azx_dev); 930 spin_lock_irq(&bus->reg_lock); 930 spin_lock_irq(&bus->reg_lock); 931 if (azx_dev->running || azx_dev->locke 931 if (azx_dev->running || azx_dev->locked) { 932 spin_unlock_irq(&bus->reg_lock 932 spin_unlock_irq(&bus->reg_lock); 933 err = -EBUSY; 933 err = -EBUSY; 934 goto unlock; 934 goto unlock; 935 } 935 } 936 azx_dev->locked = true; 936 azx_dev->locked = true; 937 spin_unlock_irq(&bus->reg_lock); 937 spin_unlock_irq(&bus->reg_lock); 938 938 939 err = snd_dma_alloc_pages(SNDRV_DMA_TY 939 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, bus->dev, 940 byte_size, b 940 byte_size, bufp); 941 if (err < 0) 941 if (err < 0) 942 goto err_alloc; 942 goto err_alloc; 943 943 944 azx_dev->substream = NULL; 944 azx_dev->substream = NULL; 945 azx_dev->bufsize = byte_size; 945 azx_dev->bufsize = byte_size; 946 azx_dev->period_bytes = byte_size; 946 azx_dev->period_bytes = byte_size; 947 azx_dev->format_val = format; 947 azx_dev->format_val = format; 948 948 949 snd_hdac_stream_reset(azx_dev); 949 snd_hdac_stream_reset(azx_dev); 950 950 951 /* reset BDL address */ 951 /* reset BDL address */ 952 snd_hdac_stream_writel(azx_dev, SD_BDL 952 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0); 953 snd_hdac_stream_writel(azx_dev, SD_BDL 953 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0); 954 954 955 azx_dev->frags = 0; 955 azx_dev->frags = 0; 956 bdl = (__le32 *)azx_dev->bdl.area; 956 bdl = (__le32 *)azx_dev->bdl.area; 957 err = setup_bdle(bus, bufp, azx_dev, & 957 err = setup_bdle(bus, bufp, azx_dev, &bdl, 0, byte_size, 0); 958 if (err < 0) 958 if (err < 0) 959 goto error; 959 goto error; 960 960 961 snd_hdac_stream_setup(azx_dev, true); 961 snd_hdac_stream_setup(azx_dev, true); 962 snd_hdac_dsp_unlock(azx_dev); 962 snd_hdac_dsp_unlock(azx_dev); 963 return azx_dev->stream_tag; 963 return azx_dev->stream_tag; 964 964 965 error: 965 error: 966 snd_dma_free_pages(bufp); 966 snd_dma_free_pages(bufp); 967 err_alloc: 967 err_alloc: 968 spin_lock_irq(&bus->reg_lock); 968 spin_lock_irq(&bus->reg_lock); 969 azx_dev->locked = false; 969 azx_dev->locked = false; 970 spin_unlock_irq(&bus->reg_lock); 970 spin_unlock_irq(&bus->reg_lock); 971 unlock: 971 unlock: 972 snd_hdac_dsp_unlock(azx_dev); 972 snd_hdac_dsp_unlock(azx_dev); 973 return err; 973 return err; 974 } 974 } 975 EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare); 975 EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare); 976 976 977 /** 977 /** 978 * snd_hdac_dsp_trigger - start / stop DSP loa 978 * snd_hdac_dsp_trigger - start / stop DSP loading 979 * @azx_dev: HD-audio core stream used for DSP 979 * @azx_dev: HD-audio core stream used for DSP loading 980 * @start: trigger start or stop 980 * @start: trigger start or stop 981 */ 981 */ 982 void snd_hdac_dsp_trigger(struct hdac_stream * 982 void snd_hdac_dsp_trigger(struct hdac_stream *azx_dev, bool start) 983 { 983 { 984 if (start) 984 if (start) 985 snd_hdac_stream_start(azx_dev) 985 snd_hdac_stream_start(azx_dev); 986 else 986 else 987 snd_hdac_stream_stop(azx_dev); 987 snd_hdac_stream_stop(azx_dev); 988 } 988 } 989 EXPORT_SYMBOL_GPL(snd_hdac_dsp_trigger); 989 EXPORT_SYMBOL_GPL(snd_hdac_dsp_trigger); 990 990 991 /** 991 /** 992 * snd_hdac_dsp_cleanup - clean up the stream 992 * snd_hdac_dsp_cleanup - clean up the stream from DSP loading to normal 993 * @azx_dev: HD-audio core stream used for DSP 993 * @azx_dev: HD-audio core stream used for DSP loading 994 * @dmab: buffer used by DSP loading 994 * @dmab: buffer used by DSP loading 995 */ 995 */ 996 void snd_hdac_dsp_cleanup(struct hdac_stream * 996 void snd_hdac_dsp_cleanup(struct hdac_stream *azx_dev, 997 struct snd_dma_buffe 997 struct snd_dma_buffer *dmab) 998 { 998 { 999 struct hdac_bus *bus = azx_dev->bus; 999 struct hdac_bus *bus = azx_dev->bus; 1000 1000 1001 if (!dmab->area || !azx_dev->locked) 1001 if (!dmab->area || !azx_dev->locked) 1002 return; 1002 return; 1003 1003 1004 snd_hdac_dsp_lock(azx_dev); 1004 snd_hdac_dsp_lock(azx_dev); 1005 /* reset BDL address */ 1005 /* reset BDL address */ 1006 snd_hdac_stream_writel(azx_dev, SD_BD 1006 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0); 1007 snd_hdac_stream_writel(azx_dev, SD_BD 1007 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0); 1008 snd_hdac_stream_writel(azx_dev, SD_CT 1008 snd_hdac_stream_writel(azx_dev, SD_CTL, 0); 1009 azx_dev->bufsize = 0; 1009 azx_dev->bufsize = 0; 1010 azx_dev->period_bytes = 0; 1010 azx_dev->period_bytes = 0; 1011 azx_dev->format_val = 0; 1011 azx_dev->format_val = 0; 1012 1012 1013 snd_dma_free_pages(dmab); 1013 snd_dma_free_pages(dmab); 1014 dmab->area = NULL; 1014 dmab->area = NULL; 1015 1015 1016 spin_lock_irq(&bus->reg_lock); 1016 spin_lock_irq(&bus->reg_lock); 1017 azx_dev->locked = false; 1017 azx_dev->locked = false; 1018 spin_unlock_irq(&bus->reg_lock); 1018 spin_unlock_irq(&bus->reg_lock); 1019 snd_hdac_dsp_unlock(azx_dev); 1019 snd_hdac_dsp_unlock(azx_dev); 1020 } 1020 } 1021 EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup); 1021 EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup); 1022 #endif /* CONFIG_SND_HDA_DSP_LOADER */ 1022 #endif /* CONFIG_SND_HDA_DSP_LOADER */ 1023 1023
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.