blob: b513a15c777f8e184f06705a79935f3f1475acec [file] [log] [blame]
Takashi Iwai14752412015-04-14 12:15:47 +02001/*
2 * HD-audio stream operations
3 */
4
5#include <linux/kernel.h>
6#include <linux/delay.h>
7#include <linux/export.h>
8#include <sound/core.h>
9#include <sound/pcm.h>
10#include <sound/hdaudio.h>
11#include <sound/hda_register.h>
12
13/**
14 * snd_hdac_stream_init - initialize each stream (aka device)
15 * @bus: HD-audio core bus
16 * @azx_dev: HD-audio core stream object to initialize
17 * @idx: stream index number
18 * @direction: stream direction (SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE)
19 * @tag: the tag id to assign
20 *
21 * Assign the starting bdl address to each stream (device) and initialize.
22 */
23void snd_hdac_stream_init(struct hdac_bus *bus, struct hdac_stream *azx_dev,
24 int idx, int direction, int tag)
25{
26 azx_dev->bus = bus;
27 if (bus->posbuf.area)
28 azx_dev->posbuf = (__le32 *)(bus->posbuf.area + idx * 8);
29 /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
30 azx_dev->sd_addr = bus->remap_addr + (0x20 * idx + 0x80);
31 /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
32 azx_dev->sd_int_sta_mask = 1 << idx;
33 azx_dev->index = idx;
34 azx_dev->direction = direction;
35 azx_dev->stream_tag = tag;
36 list_add_tail(&azx_dev->list, &bus->stream_list);
37}
38EXPORT_SYMBOL_GPL(snd_hdac_stream_init);
39
40/**
41 * snd_hdac_stream_start - start a stream
42 * @azx_dev: HD-audio core stream to start
43 * @fresh_start: false = wallclock timestamp relative to period wallclock
44 *
45 * Start a stream, set start_wallclk and set the running flag.
46 */
47void snd_hdac_stream_start(struct hdac_stream *azx_dev, bool fresh_start)
48{
49 struct hdac_bus *bus = azx_dev->bus;
50
51 azx_dev->start_wallclk = snd_hdac_chip_readl(bus, WALLCLK);
52 if (!fresh_start)
53 azx_dev->start_wallclk -= azx_dev->period_wallclk;
54
55 /* enable SIE */
56 snd_hdac_chip_updatel(bus, INTCTL, 0, 1 << azx_dev->index);
57 /* set DMA start and interrupt mask */
58 snd_hdac_stream_updateb(azx_dev, SD_CTL,
59 0, SD_CTL_DMA_START | SD_INT_MASK);
60 azx_dev->running = true;
61}
62EXPORT_SYMBOL_GPL(snd_hdac_stream_start);
63
64/**
65 * snd_hdac_stream_clear - stop a stream DMA
66 * @azx_dev: HD-audio core stream to stop
67 */
68void snd_hdac_stream_clear(struct hdac_stream *azx_dev)
69{
70 snd_hdac_stream_updateb(azx_dev, SD_CTL,
71 SD_CTL_DMA_START | SD_INT_MASK, 0);
72 snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
73 azx_dev->running = false;
74}
75EXPORT_SYMBOL_GPL(snd_hdac_stream_clear);
76
77/**
78 * snd_hdac_stream_stop - stop a stream
79 * @azx_dev: HD-audio core stream to stop
80 *
81 * Stop a stream DMA and disable stream interrupt
82 */
83void snd_hdac_stream_stop(struct hdac_stream *azx_dev)
84{
85 snd_hdac_stream_clear(azx_dev);
86 /* disable SIE */
87 snd_hdac_chip_updatel(azx_dev->bus, INTCTL, 1 << azx_dev->index, 0);
88}
89EXPORT_SYMBOL_GPL(snd_hdac_stream_stop);
90
91/**
92 * snd_hdac_stream_reset - reset a stream
93 * @azx_dev: HD-audio core stream to reset
94 */
95void snd_hdac_stream_reset(struct hdac_stream *azx_dev)
96{
97 unsigned char val;
98 int timeout;
99
100 snd_hdac_stream_clear(azx_dev);
101
102 snd_hdac_stream_updateb(azx_dev, SD_CTL, 0, SD_CTL_STREAM_RESET);
103 udelay(3);
104 timeout = 300;
105 do {
106 val = snd_hdac_stream_readb(azx_dev, SD_CTL) &
107 SD_CTL_STREAM_RESET;
108 if (val)
109 break;
110 } while (--timeout);
111 val &= ~SD_CTL_STREAM_RESET;
112 snd_hdac_stream_writeb(azx_dev, SD_CTL, val);
113 udelay(3);
114
115 timeout = 300;
116 /* waiting for hardware to report that the stream is out of reset */
117 do {
118 val = snd_hdac_stream_readb(azx_dev, SD_CTL) &
119 SD_CTL_STREAM_RESET;
120 if (!val)
121 break;
122 } while (--timeout);
123
124 /* reset first position - may not be synced with hw at this time */
125 if (azx_dev->posbuf)
126 *azx_dev->posbuf = 0;
127}
128EXPORT_SYMBOL_GPL(snd_hdac_stream_reset);
129
130/**
131 * snd_hdac_stream_setup - set up the SD for streaming
132 * @azx_dev: HD-audio core stream to set up
133 */
134int snd_hdac_stream_setup(struct hdac_stream *azx_dev)
135{
136 struct hdac_bus *bus = azx_dev->bus;
137 struct snd_pcm_runtime *runtime = azx_dev->substream->runtime;
138 unsigned int val;
139
140 /* make sure the run bit is zero for SD */
141 snd_hdac_stream_clear(azx_dev);
142 /* program the stream_tag */
143 val = snd_hdac_stream_readl(azx_dev, SD_CTL);
144 val = (val & ~SD_CTL_STREAM_TAG_MASK) |
145 (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
146 if (!bus->snoop)
147 val |= SD_CTL_TRAFFIC_PRIO;
148 snd_hdac_stream_writel(azx_dev, SD_CTL, val);
149
150 /* program the length of samples in cyclic buffer */
151 snd_hdac_stream_writel(azx_dev, SD_CBL, azx_dev->bufsize);
152
153 /* program the stream format */
154 /* this value needs to be the same as the one programmed */
155 snd_hdac_stream_writew(azx_dev, SD_FORMAT, azx_dev->format_val);
156
157 /* program the stream LVI (last valid index) of the BDL */
158 snd_hdac_stream_writew(azx_dev, SD_LVI, azx_dev->frags - 1);
159
160 /* program the BDL address */
161 /* lower BDL address */
162 snd_hdac_stream_writel(azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
163 /* upper BDL address */
164 snd_hdac_stream_writel(azx_dev, SD_BDLPU,
165 upper_32_bits(azx_dev->bdl.addr));
166
167 /* enable the position buffer */
168 if (bus->use_posbuf && bus->posbuf.addr) {
169 if (!(snd_hdac_chip_readl(bus, DPLBASE) & AZX_DPLBASE_ENABLE))
170 snd_hdac_chip_writel(bus, DPLBASE,
171 (u32)bus->posbuf.addr | AZX_DPLBASE_ENABLE);
172 }
173
174 /* set the interrupt enable bits in the descriptor control register */
175 snd_hdac_stream_updatel(azx_dev, SD_CTL, 0, SD_INT_MASK);
176
177 if (azx_dev->direction == SNDRV_PCM_STREAM_PLAYBACK)
178 azx_dev->fifo_size =
179 snd_hdac_stream_readw(azx_dev, SD_FIFOSIZE) + 1;
180 else
181 azx_dev->fifo_size = 0;
182
183 /* when LPIB delay correction gives a small negative value,
184 * we ignore it; currently set the threshold statically to
185 * 64 frames
186 */
187 if (runtime->period_size > 64)
188 azx_dev->delay_negative_threshold =
189 -frames_to_bytes(runtime, 64);
190 else
191 azx_dev->delay_negative_threshold = 0;
192
193 /* wallclk has 24Mhz clock source */
194 azx_dev->period_wallclk = (((runtime->period_size * 24000) /
195 runtime->rate) * 1000);
196
197 return 0;
198}
199EXPORT_SYMBOL_GPL(snd_hdac_stream_setup);
200
201/**
202 * snd_hdac_stream_cleanup - cleanup a stream
203 * @azx_dev: HD-audio core stream to clean up
204 */
205void snd_hdac_stream_cleanup(struct hdac_stream *azx_dev)
206{
207 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
208 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
209 snd_hdac_stream_writel(azx_dev, SD_CTL, 0);
210 azx_dev->bufsize = 0;
211 azx_dev->period_bytes = 0;
212 azx_dev->format_val = 0;
213}
214EXPORT_SYMBOL_GPL(snd_hdac_stream_cleanup);
215
216/**
217 * snd_hdac_stream_assign - assign a stream for the PCM
218 * @bus: HD-audio core bus
219 * @substream: PCM substream to assign
220 *
221 * Look for an unused stream for the given PCM substream, assign it
222 * and return the stream object. If no stream is free, returns NULL.
223 * The function tries to keep using the same stream object when it's used
224 * beforehand. Also, when bus->reverse_assign flag is set, the last free
225 * or matching entry is returned. This is needed for some strange codecs.
226 */
227struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus,
228 struct snd_pcm_substream *substream)
229{
230 struct hdac_stream *azx_dev;
231 struct hdac_stream *res = NULL;
232
233 /* make a non-zero unique key for the substream */
234 int key = (substream->pcm->device << 16) | (substream->number << 2) |
235 (substream->stream + 1);
236
237 list_for_each_entry(azx_dev, &bus->stream_list, list) {
238 if (azx_dev->direction != substream->stream)
239 continue;
240 if (azx_dev->opened)
241 continue;
242 if (azx_dev->assigned_key == key) {
243 res = azx_dev;
244 break;
245 }
246 if (!res || bus->reverse_assign)
247 res = azx_dev;
248 }
249 if (res) {
250 spin_lock_irq(&bus->reg_lock);
251 res->opened = 1;
252 res->running = 0;
253 res->assigned_key = key;
254 res->substream = substream;
255 spin_unlock_irq(&bus->reg_lock);
256 }
257 return res;
258}
259EXPORT_SYMBOL_GPL(snd_hdac_stream_assign);
260
261/**
262 * snd_hdac_stream_release - release the assigned stream
263 * @azx_dev: HD-audio core stream to release
264 *
265 * Release the stream that has been assigned by snd_hdac_stream_assign().
266 */
267void snd_hdac_stream_release(struct hdac_stream *azx_dev)
268{
269 struct hdac_bus *bus = azx_dev->bus;
270
271 spin_lock_irq(&bus->reg_lock);
272 azx_dev->opened = 0;
273 azx_dev->running = 0;
274 azx_dev->substream = NULL;
275 spin_unlock_irq(&bus->reg_lock);
276}
277EXPORT_SYMBOL_GPL(snd_hdac_stream_release);
278
279/*
280 * set up a BDL entry
281 */
282static int setup_bdle(struct hdac_bus *bus,
283 struct snd_dma_buffer *dmab,
284 struct hdac_stream *azx_dev, __le32 **bdlp,
285 int ofs, int size, int with_ioc)
286{
287 __le32 *bdl = *bdlp;
288
289 while (size > 0) {
290 dma_addr_t addr;
291 int chunk;
292
293 if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
294 return -EINVAL;
295
296 addr = snd_sgbuf_get_addr(dmab, ofs);
297 /* program the address field of the BDL entry */
298 bdl[0] = cpu_to_le32((u32)addr);
299 bdl[1] = cpu_to_le32(upper_32_bits(addr));
300 /* program the size field of the BDL entry */
301 chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
302 /* one BDLE cannot cross 4K boundary on CTHDA chips */
303 if (bus->align_bdle_4k) {
304 u32 remain = 0x1000 - (ofs & 0xfff);
305
306 if (chunk > remain)
307 chunk = remain;
308 }
309 bdl[2] = cpu_to_le32(chunk);
310 /* program the IOC to enable interrupt
311 * only when the whole fragment is processed
312 */
313 size -= chunk;
314 bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
315 bdl += 4;
316 azx_dev->frags++;
317 ofs += chunk;
318 }
319 *bdlp = bdl;
320 return ofs;
321}
322
323/**
324 * snd_hdac_stream_setup_periods - set up BDL entries
325 * @azx_dev: HD-audio core stream to set up
326 *
327 * Set up the buffer descriptor table of the given stream based on the
328 * period and buffer sizes of the assigned PCM substream.
329 */
330int snd_hdac_stream_setup_periods(struct hdac_stream *azx_dev)
331{
332 struct hdac_bus *bus = azx_dev->bus;
333 struct snd_pcm_substream *substream = azx_dev->substream;
334 struct snd_pcm_runtime *runtime = substream->runtime;
335 __le32 *bdl;
336 int i, ofs, periods, period_bytes;
337 int pos_adj, pos_align;
338
339 /* reset BDL address */
340 snd_hdac_stream_writel(azx_dev, SD_BDLPL, 0);
341 snd_hdac_stream_writel(azx_dev, SD_BDLPU, 0);
342
343 period_bytes = azx_dev->period_bytes;
344 periods = azx_dev->bufsize / period_bytes;
345
346 /* program the initial BDL entries */
347 bdl = (__le32 *)azx_dev->bdl.area;
348 ofs = 0;
349 azx_dev->frags = 0;
350
351 pos_adj = bus->bdl_pos_adj;
352 if (!azx_dev->no_period_wakeup && pos_adj > 0) {
353 pos_align = pos_adj;
354 pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
355 if (!pos_adj)
356 pos_adj = pos_align;
357 else
358 pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
359 pos_align;
360 pos_adj = frames_to_bytes(runtime, pos_adj);
361 if (pos_adj >= period_bytes) {
362 dev_warn(bus->dev, "Too big adjustment %d\n",
363 pos_adj);
364 pos_adj = 0;
365 } else {
366 ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
367 azx_dev,
368 &bdl, ofs, pos_adj, true);
369 if (ofs < 0)
370 goto error;
371 }
372 } else
373 pos_adj = 0;
374
375 for (i = 0; i < periods; i++) {
376 if (i == periods - 1 && pos_adj)
377 ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
378 azx_dev, &bdl, ofs,
379 period_bytes - pos_adj, 0);
380 else
381 ofs = setup_bdle(bus, snd_pcm_get_dma_buf(substream),
382 azx_dev, &bdl, ofs,
383 period_bytes,
384 !azx_dev->no_period_wakeup);
385 if (ofs < 0)
386 goto error;
387 }
388 return 0;
389
390 error:
391 dev_err(bus->dev, "Too many BDL entries: buffer=%d, period=%d\n",
392 azx_dev->bufsize, period_bytes);
393 return -EINVAL;
394}
395EXPORT_SYMBOL_GPL(snd_hdac_stream_setup_periods);
396
397static cycle_t azx_cc_read(const struct cyclecounter *cc)
398{
399 struct hdac_stream *azx_dev = container_of(cc, struct hdac_stream, cc);
400
401 return snd_hdac_chip_readl(azx_dev->bus, WALLCLK);
402}
403
404static void azx_timecounter_init(struct hdac_stream *azx_dev,
405 bool force, cycle_t last)
406{
407 struct timecounter *tc = &azx_dev->tc;
408 struct cyclecounter *cc = &azx_dev->cc;
409 u64 nsec;
410
411 cc->read = azx_cc_read;
412 cc->mask = CLOCKSOURCE_MASK(32);
413
414 /*
415 * Converting from 24 MHz to ns means applying a 125/3 factor.
416 * To avoid any saturation issues in intermediate operations,
417 * the 125 factor is applied first. The division is applied
418 * last after reading the timecounter value.
419 * Applying the 1/3 factor as part of the multiplication
420 * requires at least 20 bits for a decent precision, however
421 * overflows occur after about 4 hours or less, not a option.
422 */
423
424 cc->mult = 125; /* saturation after 195 years */
425 cc->shift = 0;
426
427 nsec = 0; /* audio time is elapsed time since trigger */
428 timecounter_init(tc, cc, nsec);
429 if (force) {
430 /*
431 * force timecounter to use predefined value,
432 * used for synchronized starts
433 */
434 tc->cycle_last = last;
435 }
436}
437
438/**
439 * snd_hdac_stream_timecounter_init - initialize time counter
440 * @azx_dev: HD-audio core stream (master stream)
441 * @streams: bit flags of streams to set up
442 *
443 * Initializes the time counter of streams marked by the bit flags (each
444 * bit corresponds to the stream index).
445 * The trigger timestamp of PCM substream assigned to the given stream is
446 * updated accordingly, too.
447 */
448void snd_hdac_stream_timecounter_init(struct hdac_stream *azx_dev,
449 unsigned int streams)
450{
451 struct hdac_bus *bus = azx_dev->bus;
452 struct snd_pcm_runtime *runtime = azx_dev->substream->runtime;
453 struct hdac_stream *s;
454 bool inited = false;
455 cycle_t cycle_last = 0;
456 int i = 0;
457
458 list_for_each_entry(s, &bus->stream_list, list) {
459 if (streams & (1 << i)) {
460 azx_timecounter_init(s, inited, cycle_last);
461 if (!inited) {
462 inited = true;
463 cycle_last = s->tc.cycle_last;
464 }
465 }
466 i++;
467 }
468
469 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
470 runtime->trigger_tstamp_latched = true;
471}
472EXPORT_SYMBOL_GPL(snd_hdac_stream_timecounter_init);
473
474/**
475 * snd_hdac_stream_sync_trigger - turn on/off stream sync register
476 * @azx_dev: HD-audio core stream (master stream)
477 * @streams: bit flags of streams to sync
478 */
479void snd_hdac_stream_sync_trigger(struct hdac_stream *azx_dev, bool set,
480 unsigned int streams, unsigned int reg)
481{
482 struct hdac_bus *bus = azx_dev->bus;
483 unsigned int val;
484
485 if (!reg)
486 reg = AZX_REG_SSYNC;
487 val = _snd_hdac_chip_read(l, bus, reg);
488 if (set)
489 val |= streams;
490 else
491 val &= ~streams;
492 _snd_hdac_chip_write(l, bus, reg, val);
493}
494EXPORT_SYMBOL_GPL(snd_hdac_stream_sync_trigger);
495
496/**
497 * snd_hdac_stream_sync - sync with start/strop trigger operation
498 * @azx_dev: HD-audio core stream (master stream)
499 * @start: true = start, false = stop
500 * @streams: bit flags of streams to sync
501 *
502 * For @start = true, wait until all FIFOs get ready.
503 * For @start = false, wait until all RUN bits are cleared.
504 */
505void snd_hdac_stream_sync(struct hdac_stream *azx_dev, bool start,
506 unsigned int streams)
507{
508 struct hdac_bus *bus = azx_dev->bus;
509 int i, nwait, timeout;
510 struct hdac_stream *s;
511
512 for (timeout = 5000; timeout; timeout--) {
513 nwait = 0;
514 i = 0;
515 list_for_each_entry(s, &bus->stream_list, list) {
516 if (streams & (1 << i)) {
517 if (start) {
518 /* check FIFO gets ready */
519 if (!(snd_hdac_stream_readb(s, SD_STS) &
520 SD_STS_FIFO_READY))
521 nwait++;
522 } else {
523 /* check RUN bit is cleared */
524 if (snd_hdac_stream_readb(s, SD_CTL) &
525 SD_CTL_DMA_START)
526 nwait++;
527 }
528 }
529 i++;
530 }
531 if (!nwait)
532 break;
533 cpu_relax();
534 }
535}
536EXPORT_SYMBOL_GPL(snd_hdac_stream_sync);