ALSA: hda - Move the dsp loader to hda_controller
[linux-2.6/btrfs-unstable.git] / sound / pci / hda / hda_controller.c
blobb637d2c462373cb52a658692544bfba696f659d3
1 /*
3 * Implementation of primary alsa driver code base for Intel HD Audio.
5 * Copyright(c) 2004 Intel Corporation. All rights reserved.
7 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * PeiSen Hou <pshou@realtek.com.tw>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * more details.
23 #include <linux/clocksource.h>
24 #include <linux/delay.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <sound/core.h>
29 #include <sound/initval.h>
30 #include "hda_priv.h"
31 #include "hda_controller.h"
33 #define CREATE_TRACE_POINTS
34 #include "hda_intel_trace.h"
36 /* DSP lock helpers */
37 #ifdef CONFIG_SND_HDA_DSP_LOADER
38 #define dsp_lock_init(dev) mutex_init(&(dev)->dsp_mutex)
39 #define dsp_lock(dev) mutex_lock(&(dev)->dsp_mutex)
40 #define dsp_unlock(dev) mutex_unlock(&(dev)->dsp_mutex)
41 #define dsp_is_locked(dev) ((dev)->locked)
42 #else
43 #define dsp_lock_init(dev) do {} while (0)
44 #define dsp_lock(dev) do {} while (0)
45 #define dsp_unlock(dev) do {} while (0)
46 #define dsp_is_locked(dev) 0
47 #endif
50 * AZX stream operations.
53 /* start a stream */
54 static void azx_stream_start(struct azx *chip, struct azx_dev *azx_dev)
57 * Before stream start, initialize parameter
59 azx_dev->insufficient = 1;
61 /* enable SIE */
62 azx_writel(chip, INTCTL,
63 azx_readl(chip, INTCTL) | (1 << azx_dev->index));
64 /* set DMA start and interrupt mask */
65 azx_sd_writeb(chip, azx_dev, SD_CTL,
66 azx_sd_readb(chip, azx_dev, SD_CTL) |
67 SD_CTL_DMA_START | SD_INT_MASK);
70 /* stop DMA */
71 static void azx_stream_clear(struct azx *chip, struct azx_dev *azx_dev)
73 azx_sd_writeb(chip, azx_dev, SD_CTL,
74 azx_sd_readb(chip, azx_dev, SD_CTL) &
75 ~(SD_CTL_DMA_START | SD_INT_MASK));
76 azx_sd_writeb(chip, azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
79 /* stop a stream */
80 void azx_stream_stop(struct azx *chip, struct azx_dev *azx_dev)
82 azx_stream_clear(chip, azx_dev);
83 /* disable SIE */
84 azx_writel(chip, INTCTL,
85 azx_readl(chip, INTCTL) & ~(1 << azx_dev->index));
87 EXPORT_SYMBOL_GPL(azx_stream_stop);
89 /* reset stream */
90 static void azx_stream_reset(struct azx *chip, struct azx_dev *azx_dev)
92 unsigned char val;
93 int timeout;
95 azx_stream_clear(chip, azx_dev);
97 azx_sd_writeb(chip, azx_dev, SD_CTL,
98 azx_sd_readb(chip, azx_dev, SD_CTL) |
99 SD_CTL_STREAM_RESET);
100 udelay(3);
101 timeout = 300;
102 while (!((val = azx_sd_readb(chip, azx_dev, SD_CTL)) &
103 SD_CTL_STREAM_RESET) && --timeout)
105 val &= ~SD_CTL_STREAM_RESET;
106 azx_sd_writeb(chip, azx_dev, SD_CTL, val);
107 udelay(3);
109 timeout = 300;
110 /* waiting for hardware to report that the stream is out of reset */
111 while (((val = azx_sd_readb(chip, azx_dev, SD_CTL)) &
112 SD_CTL_STREAM_RESET) && --timeout)
115 /* reset first position - may not be synced with hw at this time */
116 *azx_dev->posbuf = 0;
120 * set up the SD for streaming
122 static int azx_setup_controller(struct azx *chip, struct azx_dev *azx_dev)
124 unsigned int val;
125 /* make sure the run bit is zero for SD */
126 azx_stream_clear(chip, azx_dev);
127 /* program the stream_tag */
128 val = azx_sd_readl(chip, azx_dev, SD_CTL);
129 val = (val & ~SD_CTL_STREAM_TAG_MASK) |
130 (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
131 if (!azx_snoop(chip))
132 val |= SD_CTL_TRAFFIC_PRIO;
133 azx_sd_writel(chip, azx_dev, SD_CTL, val);
135 /* program the length of samples in cyclic buffer */
136 azx_sd_writel(chip, azx_dev, SD_CBL, azx_dev->bufsize);
138 /* program the stream format */
139 /* this value needs to be the same as the one programmed */
140 azx_sd_writew(chip, azx_dev, SD_FORMAT, azx_dev->format_val);
142 /* program the stream LVI (last valid index) of the BDL */
143 azx_sd_writew(chip, azx_dev, SD_LVI, azx_dev->frags - 1);
145 /* program the BDL address */
146 /* lower BDL address */
147 azx_sd_writel(chip, azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
148 /* upper BDL address */
149 azx_sd_writel(chip, azx_dev, SD_BDLPU,
150 upper_32_bits(azx_dev->bdl.addr));
152 /* enable the position buffer */
153 if (chip->position_fix[0] != POS_FIX_LPIB ||
154 chip->position_fix[1] != POS_FIX_LPIB) {
155 if (!(azx_readl(chip, DPLBASE) & ICH6_DPLBASE_ENABLE))
156 azx_writel(chip, DPLBASE,
157 (u32)chip->posbuf.addr | ICH6_DPLBASE_ENABLE);
160 /* set the interrupt enable bits in the descriptor control register */
161 azx_sd_writel(chip, azx_dev, SD_CTL,
162 azx_sd_readl(chip, azx_dev, SD_CTL) | SD_INT_MASK);
164 return 0;
167 /* assign a stream for the PCM */
168 static inline struct azx_dev *
169 azx_assign_device(struct azx *chip, struct snd_pcm_substream *substream)
171 int dev, i, nums;
172 struct azx_dev *res = NULL;
173 /* make a non-zero unique key for the substream */
174 int key = (substream->pcm->device << 16) | (substream->number << 2) |
175 (substream->stream + 1);
177 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
178 dev = chip->playback_index_offset;
179 nums = chip->playback_streams;
180 } else {
181 dev = chip->capture_index_offset;
182 nums = chip->capture_streams;
184 for (i = 0; i < nums; i++, dev++) {
185 struct azx_dev *azx_dev = &chip->azx_dev[dev];
186 dsp_lock(azx_dev);
187 if (!azx_dev->opened && !dsp_is_locked(azx_dev)) {
188 res = azx_dev;
189 if (res->assigned_key == key) {
190 res->opened = 1;
191 res->assigned_key = key;
192 dsp_unlock(azx_dev);
193 return azx_dev;
196 dsp_unlock(azx_dev);
198 if (res) {
199 dsp_lock(res);
200 res->opened = 1;
201 res->assigned_key = key;
202 dsp_unlock(res);
204 return res;
207 /* release the assigned stream */
208 static inline void azx_release_device(struct azx_dev *azx_dev)
210 azx_dev->opened = 0;
213 static cycle_t azx_cc_read(const struct cyclecounter *cc)
215 struct azx_dev *azx_dev = container_of(cc, struct azx_dev, azx_cc);
216 struct snd_pcm_substream *substream = azx_dev->substream;
217 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
218 struct azx *chip = apcm->chip;
220 return azx_readl(chip, WALLCLK);
223 static void azx_timecounter_init(struct snd_pcm_substream *substream,
224 bool force, cycle_t last)
226 struct azx_dev *azx_dev = get_azx_dev(substream);
227 struct timecounter *tc = &azx_dev->azx_tc;
228 struct cyclecounter *cc = &azx_dev->azx_cc;
229 u64 nsec;
231 cc->read = azx_cc_read;
232 cc->mask = CLOCKSOURCE_MASK(32);
235 * Converting from 24 MHz to ns means applying a 125/3 factor.
236 * To avoid any saturation issues in intermediate operations,
237 * the 125 factor is applied first. The division is applied
238 * last after reading the timecounter value.
239 * Applying the 1/3 factor as part of the multiplication
240 * requires at least 20 bits for a decent precision, however
241 * overflows occur after about 4 hours or less, not a option.
244 cc->mult = 125; /* saturation after 195 years */
245 cc->shift = 0;
247 nsec = 0; /* audio time is elapsed time since trigger */
248 timecounter_init(tc, cc, nsec);
249 if (force)
251 * force timecounter to use predefined value,
252 * used for synchronized starts
254 tc->cycle_last = last;
257 static u64 azx_adjust_codec_delay(struct snd_pcm_substream *substream,
258 u64 nsec)
260 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
261 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
262 u64 codec_frames, codec_nsecs;
264 if (!hinfo->ops.get_delay)
265 return nsec;
267 codec_frames = hinfo->ops.get_delay(hinfo, apcm->codec, substream);
268 codec_nsecs = div_u64(codec_frames * 1000000000LL,
269 substream->runtime->rate);
271 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
272 return nsec + codec_nsecs;
274 return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
278 * set up a BDL entry
280 static int setup_bdle(struct azx *chip,
281 struct snd_dma_buffer *dmab,
282 struct azx_dev *azx_dev, u32 **bdlp,
283 int ofs, int size, int with_ioc)
285 u32 *bdl = *bdlp;
287 while (size > 0) {
288 dma_addr_t addr;
289 int chunk;
291 if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
292 return -EINVAL;
294 addr = snd_sgbuf_get_addr(dmab, ofs);
295 /* program the address field of the BDL entry */
296 bdl[0] = cpu_to_le32((u32)addr);
297 bdl[1] = cpu_to_le32(upper_32_bits(addr));
298 /* program the size field of the BDL entry */
299 chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
300 /* one BDLE cannot cross 4K boundary on CTHDA chips */
301 if (chip->driver_caps & AZX_DCAPS_4K_BDLE_BOUNDARY) {
302 u32 remain = 0x1000 - (ofs & 0xfff);
303 if (chunk > remain)
304 chunk = remain;
306 bdl[2] = cpu_to_le32(chunk);
307 /* program the IOC to enable interrupt
308 * only when the whole fragment is processed
310 size -= chunk;
311 bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
312 bdl += 4;
313 azx_dev->frags++;
314 ofs += chunk;
316 *bdlp = bdl;
317 return ofs;
321 * set up BDL entries
323 static int azx_setup_periods(struct azx *chip,
324 struct snd_pcm_substream *substream,
325 struct azx_dev *azx_dev)
327 u32 *bdl;
328 int i, ofs, periods, period_bytes;
329 int pos_adj = 0;
331 /* reset BDL address */
332 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
333 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
335 period_bytes = azx_dev->period_bytes;
336 periods = azx_dev->bufsize / period_bytes;
338 /* program the initial BDL entries */
339 bdl = (u32 *)azx_dev->bdl.area;
340 ofs = 0;
341 azx_dev->frags = 0;
343 if (chip->bdl_pos_adj)
344 pos_adj = chip->bdl_pos_adj[chip->dev_index];
345 if (!azx_dev->no_period_wakeup && pos_adj > 0) {
346 struct snd_pcm_runtime *runtime = substream->runtime;
347 int pos_align = pos_adj;
348 pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
349 if (!pos_adj)
350 pos_adj = pos_align;
351 else
352 pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
353 pos_align;
354 pos_adj = frames_to_bytes(runtime, pos_adj);
355 if (pos_adj >= period_bytes) {
356 dev_warn(chip->card->dev,"Too big adjustment %d\n",
357 pos_adj);
358 pos_adj = 0;
359 } else {
360 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
361 azx_dev,
362 &bdl, ofs, pos_adj, true);
363 if (ofs < 0)
364 goto error;
366 } else
367 pos_adj = 0;
369 for (i = 0; i < periods; i++) {
370 if (i == periods - 1 && pos_adj)
371 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
372 azx_dev, &bdl, ofs,
373 period_bytes - pos_adj, 0);
374 else
375 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
376 azx_dev, &bdl, ofs,
377 period_bytes,
378 !azx_dev->no_period_wakeup);
379 if (ofs < 0)
380 goto error;
382 return 0;
384 error:
385 dev_err(chip->card->dev, "Too many BDL entries: buffer=%d, period=%d\n",
386 azx_dev->bufsize, period_bytes);
387 return -EINVAL;
391 * PCM ops
394 static int azx_pcm_close(struct snd_pcm_substream *substream)
396 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
397 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
398 struct azx *chip = apcm->chip;
399 struct azx_dev *azx_dev = get_azx_dev(substream);
400 unsigned long flags;
402 mutex_lock(&chip->open_mutex);
403 spin_lock_irqsave(&chip->reg_lock, flags);
404 azx_dev->substream = NULL;
405 azx_dev->running = 0;
406 spin_unlock_irqrestore(&chip->reg_lock, flags);
407 azx_release_device(azx_dev);
408 hinfo->ops.close(hinfo, apcm->codec, substream);
409 snd_hda_power_down(apcm->codec);
410 mutex_unlock(&chip->open_mutex);
411 return 0;
414 static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
415 struct snd_pcm_hw_params *hw_params)
417 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
418 struct azx *chip = apcm->chip;
419 int ret;
421 dsp_lock(get_azx_dev(substream));
422 if (dsp_is_locked(get_azx_dev(substream))) {
423 ret = -EBUSY;
424 goto unlock;
427 ret = chip->ops->substream_alloc_pages(chip, substream,
428 params_buffer_bytes(hw_params));
429 unlock:
430 dsp_unlock(get_azx_dev(substream));
431 return ret;
434 static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
436 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
437 struct azx_dev *azx_dev = get_azx_dev(substream);
438 struct azx *chip = apcm->chip;
439 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
440 int err;
442 /* reset BDL address */
443 dsp_lock(azx_dev);
444 if (!dsp_is_locked(azx_dev)) {
445 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
446 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
447 azx_sd_writel(chip, azx_dev, SD_CTL, 0);
448 azx_dev->bufsize = 0;
449 azx_dev->period_bytes = 0;
450 azx_dev->format_val = 0;
453 snd_hda_codec_cleanup(apcm->codec, hinfo, substream);
455 err = chip->ops->substream_free_pages(chip, substream);
456 azx_dev->prepared = 0;
457 dsp_unlock(azx_dev);
458 return err;
461 static int azx_pcm_prepare(struct snd_pcm_substream *substream)
463 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
464 struct azx *chip = apcm->chip;
465 struct azx_dev *azx_dev = get_azx_dev(substream);
466 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
467 struct snd_pcm_runtime *runtime = substream->runtime;
468 unsigned int bufsize, period_bytes, format_val, stream_tag;
469 int err;
470 struct hda_spdif_out *spdif =
471 snd_hda_spdif_out_of_nid(apcm->codec, hinfo->nid);
472 unsigned short ctls = spdif ? spdif->ctls : 0;
474 dsp_lock(azx_dev);
475 if (dsp_is_locked(azx_dev)) {
476 err = -EBUSY;
477 goto unlock;
480 azx_stream_reset(chip, azx_dev);
481 format_val = snd_hda_calc_stream_format(runtime->rate,
482 runtime->channels,
483 runtime->format,
484 hinfo->maxbps,
485 ctls);
486 if (!format_val) {
487 dev_err(chip->card->dev,
488 "invalid format_val, rate=%d, ch=%d, format=%d\n",
489 runtime->rate, runtime->channels, runtime->format);
490 err = -EINVAL;
491 goto unlock;
494 bufsize = snd_pcm_lib_buffer_bytes(substream);
495 period_bytes = snd_pcm_lib_period_bytes(substream);
497 dev_dbg(chip->card->dev, "azx_pcm_prepare: bufsize=0x%x, format=0x%x\n",
498 bufsize, format_val);
500 if (bufsize != azx_dev->bufsize ||
501 period_bytes != azx_dev->period_bytes ||
502 format_val != azx_dev->format_val ||
503 runtime->no_period_wakeup != azx_dev->no_period_wakeup) {
504 azx_dev->bufsize = bufsize;
505 azx_dev->period_bytes = period_bytes;
506 azx_dev->format_val = format_val;
507 azx_dev->no_period_wakeup = runtime->no_period_wakeup;
508 err = azx_setup_periods(chip, substream, azx_dev);
509 if (err < 0)
510 goto unlock;
513 /* when LPIB delay correction gives a small negative value,
514 * we ignore it; currently set the threshold statically to
515 * 64 frames
517 if (runtime->period_size > 64)
518 azx_dev->delay_negative_threshold = -frames_to_bytes(runtime, 64);
519 else
520 azx_dev->delay_negative_threshold = 0;
522 /* wallclk has 24Mhz clock source */
523 azx_dev->period_wallclk = (((runtime->period_size * 24000) /
524 runtime->rate) * 1000);
525 azx_setup_controller(chip, azx_dev);
526 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
527 azx_dev->fifo_size =
528 azx_sd_readw(chip, azx_dev, SD_FIFOSIZE) + 1;
529 else
530 azx_dev->fifo_size = 0;
532 stream_tag = azx_dev->stream_tag;
533 /* CA-IBG chips need the playback stream starting from 1 */
534 if ((chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND) &&
535 stream_tag > chip->capture_streams)
536 stream_tag -= chip->capture_streams;
537 err = snd_hda_codec_prepare(apcm->codec, hinfo, stream_tag,
538 azx_dev->format_val, substream);
540 unlock:
541 if (!err)
542 azx_dev->prepared = 1;
543 dsp_unlock(azx_dev);
544 return err;
547 static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
549 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
550 struct azx *chip = apcm->chip;
551 struct azx_dev *azx_dev;
552 struct snd_pcm_substream *s;
553 int rstart = 0, start, nsync = 0, sbits = 0;
554 int nwait, timeout;
556 azx_dev = get_azx_dev(substream);
557 trace_azx_pcm_trigger(chip, azx_dev, cmd);
559 if (dsp_is_locked(azx_dev) || !azx_dev->prepared)
560 return -EPIPE;
562 switch (cmd) {
563 case SNDRV_PCM_TRIGGER_START:
564 rstart = 1;
565 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
566 case SNDRV_PCM_TRIGGER_RESUME:
567 start = 1;
568 break;
569 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
570 case SNDRV_PCM_TRIGGER_SUSPEND:
571 case SNDRV_PCM_TRIGGER_STOP:
572 start = 0;
573 break;
574 default:
575 return -EINVAL;
578 snd_pcm_group_for_each_entry(s, substream) {
579 if (s->pcm->card != substream->pcm->card)
580 continue;
581 azx_dev = get_azx_dev(s);
582 sbits |= 1 << azx_dev->index;
583 nsync++;
584 snd_pcm_trigger_done(s, substream);
587 spin_lock(&chip->reg_lock);
589 /* first, set SYNC bits of corresponding streams */
590 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
591 azx_writel(chip, OLD_SSYNC,
592 azx_readl(chip, OLD_SSYNC) | sbits);
593 else
594 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) | sbits);
596 snd_pcm_group_for_each_entry(s, substream) {
597 if (s->pcm->card != substream->pcm->card)
598 continue;
599 azx_dev = get_azx_dev(s);
600 if (start) {
601 azx_dev->start_wallclk = azx_readl(chip, WALLCLK);
602 if (!rstart)
603 azx_dev->start_wallclk -=
604 azx_dev->period_wallclk;
605 azx_stream_start(chip, azx_dev);
606 } else {
607 azx_stream_stop(chip, azx_dev);
609 azx_dev->running = start;
611 spin_unlock(&chip->reg_lock);
612 if (start) {
613 /* wait until all FIFOs get ready */
614 for (timeout = 5000; timeout; timeout--) {
615 nwait = 0;
616 snd_pcm_group_for_each_entry(s, substream) {
617 if (s->pcm->card != substream->pcm->card)
618 continue;
619 azx_dev = get_azx_dev(s);
620 if (!(azx_sd_readb(chip, azx_dev, SD_STS) &
621 SD_STS_FIFO_READY))
622 nwait++;
624 if (!nwait)
625 break;
626 cpu_relax();
628 } else {
629 /* wait until all RUN bits are cleared */
630 for (timeout = 5000; timeout; timeout--) {
631 nwait = 0;
632 snd_pcm_group_for_each_entry(s, substream) {
633 if (s->pcm->card != substream->pcm->card)
634 continue;
635 azx_dev = get_azx_dev(s);
636 if (azx_sd_readb(chip, azx_dev, SD_CTL) &
637 SD_CTL_DMA_START)
638 nwait++;
640 if (!nwait)
641 break;
642 cpu_relax();
645 spin_lock(&chip->reg_lock);
646 /* reset SYNC bits */
647 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
648 azx_writel(chip, OLD_SSYNC,
649 azx_readl(chip, OLD_SSYNC) & ~sbits);
650 else
651 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) & ~sbits);
652 if (start) {
653 azx_timecounter_init(substream, 0, 0);
654 if (nsync > 1) {
655 cycle_t cycle_last;
657 /* same start cycle for master and group */
658 azx_dev = get_azx_dev(substream);
659 cycle_last = azx_dev->azx_tc.cycle_last;
661 snd_pcm_group_for_each_entry(s, substream) {
662 if (s->pcm->card != substream->pcm->card)
663 continue;
664 azx_timecounter_init(s, 1, cycle_last);
668 spin_unlock(&chip->reg_lock);
669 return 0;
672 /* get the current DMA position with correction on VIA chips */
673 static unsigned int azx_via_get_position(struct azx *chip,
674 struct azx_dev *azx_dev)
676 unsigned int link_pos, mini_pos, bound_pos;
677 unsigned int mod_link_pos, mod_dma_pos, mod_mini_pos;
678 unsigned int fifo_size;
680 link_pos = azx_sd_readl(chip, azx_dev, SD_LPIB);
681 if (azx_dev->substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
682 /* Playback, no problem using link position */
683 return link_pos;
686 /* Capture */
687 /* For new chipset,
688 * use mod to get the DMA position just like old chipset
690 mod_dma_pos = le32_to_cpu(*azx_dev->posbuf);
691 mod_dma_pos %= azx_dev->period_bytes;
693 /* azx_dev->fifo_size can't get FIFO size of in stream.
694 * Get from base address + offset.
696 fifo_size = readw(chip->remap_addr + VIA_IN_STREAM0_FIFO_SIZE_OFFSET);
698 if (azx_dev->insufficient) {
699 /* Link position never gather than FIFO size */
700 if (link_pos <= fifo_size)
701 return 0;
703 azx_dev->insufficient = 0;
706 if (link_pos <= fifo_size)
707 mini_pos = azx_dev->bufsize + link_pos - fifo_size;
708 else
709 mini_pos = link_pos - fifo_size;
711 /* Find nearest previous boudary */
712 mod_mini_pos = mini_pos % azx_dev->period_bytes;
713 mod_link_pos = link_pos % azx_dev->period_bytes;
714 if (mod_link_pos >= fifo_size)
715 bound_pos = link_pos - mod_link_pos;
716 else if (mod_dma_pos >= mod_mini_pos)
717 bound_pos = mini_pos - mod_mini_pos;
718 else {
719 bound_pos = mini_pos - mod_mini_pos + azx_dev->period_bytes;
720 if (bound_pos >= azx_dev->bufsize)
721 bound_pos = 0;
724 /* Calculate real DMA position we want */
725 return bound_pos + mod_dma_pos;
728 unsigned int azx_get_position(struct azx *chip,
729 struct azx_dev *azx_dev,
730 bool with_check)
732 struct snd_pcm_substream *substream = azx_dev->substream;
733 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
734 unsigned int pos;
735 int stream = substream->stream;
736 struct hda_pcm_stream *hinfo = apcm->hinfo[stream];
737 int delay = 0;
739 switch (chip->position_fix[stream]) {
740 case POS_FIX_LPIB:
741 /* read LPIB */
742 pos = azx_sd_readl(chip, azx_dev, SD_LPIB);
743 break;
744 case POS_FIX_VIACOMBO:
745 pos = azx_via_get_position(chip, azx_dev);
746 break;
747 default:
748 /* use the position buffer */
749 pos = le32_to_cpu(*azx_dev->posbuf);
750 if (with_check && chip->position_fix[stream] == POS_FIX_AUTO) {
751 if (!pos || pos == (u32)-1) {
752 dev_info(chip->card->dev,
753 "Invalid position buffer, using LPIB read method instead.\n");
754 chip->position_fix[stream] = POS_FIX_LPIB;
755 pos = azx_sd_readl(chip, azx_dev, SD_LPIB);
756 } else
757 chip->position_fix[stream] = POS_FIX_POSBUF;
759 break;
762 if (pos >= azx_dev->bufsize)
763 pos = 0;
765 /* calculate runtime delay from LPIB */
766 if (substream->runtime &&
767 chip->position_fix[stream] == POS_FIX_POSBUF &&
768 (chip->driver_caps & AZX_DCAPS_COUNT_LPIB_DELAY)) {
769 unsigned int lpib_pos = azx_sd_readl(chip, azx_dev, SD_LPIB);
770 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
771 delay = pos - lpib_pos;
772 else
773 delay = lpib_pos - pos;
774 if (delay < 0) {
775 if (delay >= azx_dev->delay_negative_threshold)
776 delay = 0;
777 else
778 delay += azx_dev->bufsize;
780 if (delay >= azx_dev->period_bytes) {
781 dev_info(chip->card->dev,
782 "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n",
783 delay, azx_dev->period_bytes);
784 delay = 0;
785 chip->driver_caps &= ~AZX_DCAPS_COUNT_LPIB_DELAY;
787 delay = bytes_to_frames(substream->runtime, delay);
790 if (substream->runtime) {
791 if (hinfo->ops.get_delay)
792 delay += hinfo->ops.get_delay(hinfo, apcm->codec,
793 substream);
794 substream->runtime->delay = delay;
797 trace_azx_get_position(chip, azx_dev, pos, delay);
798 return pos;
800 EXPORT_SYMBOL_GPL(azx_get_position);
802 static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream)
804 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
805 struct azx *chip = apcm->chip;
806 struct azx_dev *azx_dev = get_azx_dev(substream);
807 return bytes_to_frames(substream->runtime,
808 azx_get_position(chip, azx_dev, false));
811 static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
812 struct timespec *ts)
814 struct azx_dev *azx_dev = get_azx_dev(substream);
815 u64 nsec;
817 nsec = timecounter_read(&azx_dev->azx_tc);
818 nsec = div_u64(nsec, 3); /* can be optimized */
819 nsec = azx_adjust_codec_delay(substream, nsec);
821 *ts = ns_to_timespec(nsec);
823 return 0;
826 static struct snd_pcm_hardware azx_pcm_hw = {
827 .info = (SNDRV_PCM_INFO_MMAP |
828 SNDRV_PCM_INFO_INTERLEAVED |
829 SNDRV_PCM_INFO_BLOCK_TRANSFER |
830 SNDRV_PCM_INFO_MMAP_VALID |
831 /* No full-resume yet implemented */
832 /* SNDRV_PCM_INFO_RESUME |*/
833 SNDRV_PCM_INFO_PAUSE |
834 SNDRV_PCM_INFO_SYNC_START |
835 SNDRV_PCM_INFO_HAS_WALL_CLOCK |
836 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
837 .formats = SNDRV_PCM_FMTBIT_S16_LE,
838 .rates = SNDRV_PCM_RATE_48000,
839 .rate_min = 48000,
840 .rate_max = 48000,
841 .channels_min = 2,
842 .channels_max = 2,
843 .buffer_bytes_max = AZX_MAX_BUF_SIZE,
844 .period_bytes_min = 128,
845 .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
846 .periods_min = 2,
847 .periods_max = AZX_MAX_FRAG,
848 .fifo_size = 0,
851 static int azx_pcm_open(struct snd_pcm_substream *substream)
853 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
854 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
855 struct azx *chip = apcm->chip;
856 struct azx_dev *azx_dev;
857 struct snd_pcm_runtime *runtime = substream->runtime;
858 unsigned long flags;
859 int err;
860 int buff_step;
862 mutex_lock(&chip->open_mutex);
863 azx_dev = azx_assign_device(chip, substream);
864 if (azx_dev == NULL) {
865 mutex_unlock(&chip->open_mutex);
866 return -EBUSY;
868 runtime->hw = azx_pcm_hw;
869 runtime->hw.channels_min = hinfo->channels_min;
870 runtime->hw.channels_max = hinfo->channels_max;
871 runtime->hw.formats = hinfo->formats;
872 runtime->hw.rates = hinfo->rates;
873 snd_pcm_limit_hw_rates(runtime);
874 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
876 /* avoid wrap-around with wall-clock */
877 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
879 178000000);
881 if (chip->align_buffer_size)
882 /* constrain buffer sizes to be multiple of 128
883 bytes. This is more efficient in terms of memory
884 access but isn't required by the HDA spec and
885 prevents users from specifying exact period/buffer
886 sizes. For example for 44.1kHz, a period size set
887 to 20ms will be rounded to 19.59ms. */
888 buff_step = 128;
889 else
890 /* Don't enforce steps on buffer sizes, still need to
891 be multiple of 4 bytes (HDA spec). Tested on Intel
892 HDA controllers, may not work on all devices where
893 option needs to be disabled */
894 buff_step = 4;
896 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
897 buff_step);
898 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
899 buff_step);
900 snd_hda_power_up_d3wait(apcm->codec);
901 err = hinfo->ops.open(hinfo, apcm->codec, substream);
902 if (err < 0) {
903 azx_release_device(azx_dev);
904 snd_hda_power_down(apcm->codec);
905 mutex_unlock(&chip->open_mutex);
906 return err;
908 snd_pcm_limit_hw_rates(runtime);
909 /* sanity check */
910 if (snd_BUG_ON(!runtime->hw.channels_min) ||
911 snd_BUG_ON(!runtime->hw.channels_max) ||
912 snd_BUG_ON(!runtime->hw.formats) ||
913 snd_BUG_ON(!runtime->hw.rates)) {
914 azx_release_device(azx_dev);
915 hinfo->ops.close(hinfo, apcm->codec, substream);
916 snd_hda_power_down(apcm->codec);
917 mutex_unlock(&chip->open_mutex);
918 return -EINVAL;
921 /* disable WALLCLOCK timestamps for capture streams
922 until we figure out how to handle digital inputs */
923 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
924 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK;
926 spin_lock_irqsave(&chip->reg_lock, flags);
927 azx_dev->substream = substream;
928 azx_dev->running = 0;
929 spin_unlock_irqrestore(&chip->reg_lock, flags);
931 runtime->private_data = azx_dev;
932 snd_pcm_set_sync(substream);
933 mutex_unlock(&chip->open_mutex);
934 return 0;
937 static int azx_pcm_mmap(struct snd_pcm_substream *substream,
938 struct vm_area_struct *area)
940 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
941 struct azx *chip = apcm->chip;
942 if (chip->ops->pcm_mmap_prepare)
943 chip->ops->pcm_mmap_prepare(substream, area);
944 return snd_pcm_lib_default_mmap(substream, area);
947 static struct snd_pcm_ops azx_pcm_ops = {
948 .open = azx_pcm_open,
949 .close = azx_pcm_close,
950 .ioctl = snd_pcm_lib_ioctl,
951 .hw_params = azx_pcm_hw_params,
952 .hw_free = azx_pcm_hw_free,
953 .prepare = azx_pcm_prepare,
954 .trigger = azx_pcm_trigger,
955 .pointer = azx_pcm_pointer,
956 .wall_clock = azx_get_wallclock_tstamp,
957 .mmap = azx_pcm_mmap,
958 .page = snd_pcm_sgbuf_ops_page,
961 static void azx_pcm_free(struct snd_pcm *pcm)
963 struct azx_pcm *apcm = pcm->private_data;
964 if (apcm) {
965 list_del(&apcm->list);
966 kfree(apcm);
970 #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
972 int azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
973 struct hda_pcm *cpcm)
975 struct azx *chip = bus->private_data;
976 struct snd_pcm *pcm;
977 struct azx_pcm *apcm;
978 int pcm_dev = cpcm->device;
979 unsigned int size;
980 int s, err;
982 list_for_each_entry(apcm, &chip->pcm_list, list) {
983 if (apcm->pcm->device == pcm_dev) {
984 dev_err(chip->card->dev, "PCM %d already exists\n",
985 pcm_dev);
986 return -EBUSY;
989 err = snd_pcm_new(chip->card, cpcm->name, pcm_dev,
990 cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
991 cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams,
992 &pcm);
993 if (err < 0)
994 return err;
995 strlcpy(pcm->name, cpcm->name, sizeof(pcm->name));
996 apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
997 if (apcm == NULL)
998 return -ENOMEM;
999 apcm->chip = chip;
1000 apcm->pcm = pcm;
1001 apcm->codec = codec;
1002 pcm->private_data = apcm;
1003 pcm->private_free = azx_pcm_free;
1004 if (cpcm->pcm_type == HDA_PCM_TYPE_MODEM)
1005 pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
1006 list_add_tail(&apcm->list, &chip->pcm_list);
1007 cpcm->pcm = pcm;
1008 for (s = 0; s < 2; s++) {
1009 apcm->hinfo[s] = &cpcm->stream[s];
1010 if (cpcm->stream[s].substreams)
1011 snd_pcm_set_ops(pcm, s, &azx_pcm_ops);
1013 /* buffer pre-allocation */
1014 size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
1015 if (size > MAX_PREALLOC_SIZE)
1016 size = MAX_PREALLOC_SIZE;
1017 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1018 chip->card->dev,
1019 size, MAX_PREALLOC_SIZE);
1020 /* link to codec */
1021 pcm->dev = &codec->dev;
1022 return 0;
1024 EXPORT_SYMBOL_GPL(azx_attach_pcm_stream);
1026 #ifdef CONFIG_SND_HDA_DSP_LOADER
1028 * DSP loading code (e.g. for CA0132)
1031 /* use the first stream for loading DSP */
1032 static struct azx_dev *
1033 azx_get_dsp_loader_dev(struct azx *chip)
1035 return &chip->azx_dev[chip->playback_index_offset];
1038 int azx_load_dsp_prepare(struct hda_bus *bus, unsigned int format,
1039 unsigned int byte_size,
1040 struct snd_dma_buffer *bufp)
1042 u32 *bdl;
1043 struct azx *chip = bus->private_data;
1044 struct azx_dev *azx_dev;
1045 int err;
1047 azx_dev = azx_get_dsp_loader_dev(chip);
1049 dsp_lock(azx_dev);
1050 spin_lock_irq(&chip->reg_lock);
1051 if (azx_dev->running || azx_dev->locked) {
1052 spin_unlock_irq(&chip->reg_lock);
1053 err = -EBUSY;
1054 goto unlock;
1056 azx_dev->prepared = 0;
1057 chip->saved_azx_dev = *azx_dev;
1058 azx_dev->locked = 1;
1059 spin_unlock_irq(&chip->reg_lock);
1061 err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV_SG,
1062 byte_size, bufp);
1063 if (err < 0)
1064 goto err_alloc;
1066 azx_dev->bufsize = byte_size;
1067 azx_dev->period_bytes = byte_size;
1068 azx_dev->format_val = format;
1070 azx_stream_reset(chip, azx_dev);
1072 /* reset BDL address */
1073 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
1074 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
1076 azx_dev->frags = 0;
1077 bdl = (u32 *)azx_dev->bdl.area;
1078 err = setup_bdle(chip, bufp, azx_dev, &bdl, 0, byte_size, 0);
1079 if (err < 0)
1080 goto error;
1082 azx_setup_controller(chip, azx_dev);
1083 dsp_unlock(azx_dev);
1084 return azx_dev->stream_tag;
1086 error:
1087 chip->ops->dma_free_pages(chip, bufp);
1088 err_alloc:
1089 spin_lock_irq(&chip->reg_lock);
1090 if (azx_dev->opened)
1091 *azx_dev = chip->saved_azx_dev;
1092 azx_dev->locked = 0;
1093 spin_unlock_irq(&chip->reg_lock);
1094 unlock:
1095 dsp_unlock(azx_dev);
1096 return err;
1098 EXPORT_SYMBOL_GPL(azx_load_dsp_prepare);
1100 void azx_load_dsp_trigger(struct hda_bus *bus, bool start)
1102 struct azx *chip = bus->private_data;
1103 struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
1105 if (start)
1106 azx_stream_start(chip, azx_dev);
1107 else
1108 azx_stream_stop(chip, azx_dev);
1109 azx_dev->running = start;
1111 EXPORT_SYMBOL_GPL(azx_load_dsp_trigger);
1113 void azx_load_dsp_cleanup(struct hda_bus *bus,
1114 struct snd_dma_buffer *dmab)
1116 struct azx *chip = bus->private_data;
1117 struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
1119 if (!dmab->area || !azx_dev->locked)
1120 return;
1122 dsp_lock(azx_dev);
1123 /* reset BDL address */
1124 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
1125 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
1126 azx_sd_writel(chip, azx_dev, SD_CTL, 0);
1127 azx_dev->bufsize = 0;
1128 azx_dev->period_bytes = 0;
1129 azx_dev->format_val = 0;
1131 chip->ops->dma_free_pages(chip, dmab);
1132 dmab->area = NULL;
1134 spin_lock_irq(&chip->reg_lock);
1135 if (azx_dev->opened)
1136 *azx_dev = chip->saved_azx_dev;
1137 azx_dev->locked = 0;
1138 spin_unlock_irq(&chip->reg_lock);
1139 dsp_unlock(azx_dev);
1141 EXPORT_SYMBOL_GPL(azx_load_dsp_cleanup);
1142 #endif /* CONFIG_SND_HDA_DSP_LOADER */
1144 int azx_alloc_stream_pages(struct azx *chip)
1146 int i, err;
1147 struct snd_card *card = chip->card;
1149 for (i = 0; i < chip->num_streams; i++) {
1150 dsp_lock_init(&chip->azx_dev[i]);
1151 /* allocate memory for the BDL for each stream */
1152 err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV,
1153 BDL_SIZE,
1154 &chip->azx_dev[i].bdl);
1155 if (err < 0) {
1156 dev_err(card->dev, "cannot allocate BDL\n");
1157 return -ENOMEM;
1160 /* allocate memory for the position buffer */
1161 err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV,
1162 chip->num_streams * 8, &chip->posbuf);
1163 if (err < 0) {
1164 dev_err(card->dev, "cannot allocate posbuf\n");
1165 return -ENOMEM;
1167 return 0;
1169 EXPORT_SYMBOL_GPL(azx_alloc_stream_pages);
1171 void azx_free_stream_pages(struct azx *chip)
1173 int i;
1174 if (chip->azx_dev) {
1175 for (i = 0; i < chip->num_streams; i++)
1176 if (chip->azx_dev[i].bdl.area)
1177 chip->ops->dma_free_pages(
1178 chip, &chip->azx_dev[i].bdl);
1180 if (chip->rb.area)
1181 chip->ops->dma_free_pages(chip, &chip->rb);
1182 if (chip->posbuf.area)
1183 chip->ops->dma_free_pages(chip, &chip->posbuf);
1185 EXPORT_SYMBOL_GPL(azx_free_stream_pages);
1187 MODULE_LICENSE("GPL");
1188 MODULE_DESCRIPTION("Common HDA driver funcitons");