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)
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
23 #include <linux/clocksource.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/slab.h>
30 #include <linux/reboot.h>
31 #include <sound/core.h>
32 #include <sound/initval.h>
33 #include "hda_controller.h"
35 #define CREATE_TRACE_POINTS
36 #include "hda_intel_trace.h"
38 /* DSP lock helpers */
39 #ifdef CONFIG_SND_HDA_DSP_LOADER
40 #define dsp_lock_init(dev) mutex_init(&(dev)->dsp_mutex)
41 #define dsp_lock(dev) mutex_lock(&(dev)->dsp_mutex)
42 #define dsp_unlock(dev) mutex_unlock(&(dev)->dsp_mutex)
43 #define dsp_is_locked(dev) ((dev)->locked)
45 #define dsp_lock_init(dev) do {} while (0)
46 #define dsp_lock(dev) do {} while (0)
47 #define dsp_unlock(dev) do {} while (0)
48 #define dsp_is_locked(dev) 0
52 * AZX stream operations.
56 static void azx_stream_start(struct azx *chip, struct azx_dev *azx_dev)
59 * Before stream start, initialize parameter
61 azx_dev->insufficient = 1;
64 azx_writel(chip, INTCTL,
65 azx_readl(chip, INTCTL) | (1 << azx_dev->index));
66 /* set DMA start and interrupt mask */
67 azx_sd_writeb(chip, azx_dev, SD_CTL,
68 azx_sd_readb(chip, azx_dev, SD_CTL) |
69 SD_CTL_DMA_START | SD_INT_MASK);
73 static void azx_stream_clear(struct azx *chip, struct azx_dev *azx_dev)
75 azx_sd_writeb(chip, azx_dev, SD_CTL,
76 azx_sd_readb(chip, azx_dev, SD_CTL) &
77 ~(SD_CTL_DMA_START | SD_INT_MASK));
78 azx_sd_writeb(chip, azx_dev, SD_STS, SD_INT_MASK); /* to be sure */
82 void azx_stream_stop(struct azx *chip, struct azx_dev *azx_dev)
84 azx_stream_clear(chip, azx_dev);
86 azx_writel(chip, INTCTL,
87 azx_readl(chip, INTCTL) & ~(1 << azx_dev->index));
89 EXPORT_SYMBOL_GPL(azx_stream_stop);
92 static void azx_stream_reset(struct azx *chip, struct azx_dev *azx_dev)
97 azx_stream_clear(chip, azx_dev);
99 azx_sd_writeb(chip, azx_dev, SD_CTL,
100 azx_sd_readb(chip, azx_dev, SD_CTL) |
101 SD_CTL_STREAM_RESET);
104 while (!((val = azx_sd_readb(chip, azx_dev, SD_CTL)) &
105 SD_CTL_STREAM_RESET) && --timeout)
107 val &= ~SD_CTL_STREAM_RESET;
108 azx_sd_writeb(chip, azx_dev, SD_CTL, val);
112 /* waiting for hardware to report that the stream is out of reset */
113 while (((val = azx_sd_readb(chip, azx_dev, SD_CTL)) &
114 SD_CTL_STREAM_RESET) && --timeout)
117 /* reset first position - may not be synced with hw at this time */
118 *azx_dev->posbuf = 0;
122 * set up the SD for streaming
124 static int azx_setup_controller(struct azx *chip, struct azx_dev *azx_dev)
127 /* make sure the run bit is zero for SD */
128 azx_stream_clear(chip, azx_dev);
129 /* program the stream_tag */
130 val = azx_sd_readl(chip, azx_dev, SD_CTL);
131 val = (val & ~SD_CTL_STREAM_TAG_MASK) |
132 (azx_dev->stream_tag << SD_CTL_STREAM_TAG_SHIFT);
133 if (!azx_snoop(chip))
134 val |= SD_CTL_TRAFFIC_PRIO;
135 azx_sd_writel(chip, azx_dev, SD_CTL, val);
137 /* program the length of samples in cyclic buffer */
138 azx_sd_writel(chip, azx_dev, SD_CBL, azx_dev->bufsize);
140 /* program the stream format */
141 /* this value needs to be the same as the one programmed */
142 azx_sd_writew(chip, azx_dev, SD_FORMAT, azx_dev->format_val);
144 /* program the stream LVI (last valid index) of the BDL */
145 azx_sd_writew(chip, azx_dev, SD_LVI, azx_dev->frags - 1);
147 /* program the BDL address */
148 /* lower BDL address */
149 azx_sd_writel(chip, azx_dev, SD_BDLPL, (u32)azx_dev->bdl.addr);
150 /* upper BDL address */
151 azx_sd_writel(chip, azx_dev, SD_BDLPU,
152 upper_32_bits(azx_dev->bdl.addr));
154 /* enable the position buffer */
155 if (chip->get_position[0] != azx_get_pos_lpib ||
156 chip->get_position[1] != azx_get_pos_lpib) {
157 if (!(azx_readl(chip, DPLBASE) & AZX_DPLBASE_ENABLE))
158 azx_writel(chip, DPLBASE,
159 (u32)chip->posbuf.addr | AZX_DPLBASE_ENABLE);
162 /* set the interrupt enable bits in the descriptor control register */
163 azx_sd_writel(chip, azx_dev, SD_CTL,
164 azx_sd_readl(chip, azx_dev, SD_CTL) | SD_INT_MASK);
169 /* assign a stream for the PCM */
170 static inline struct azx_dev *
171 azx_assign_device(struct azx *chip, struct snd_pcm_substream *substream)
174 struct azx_dev *res = NULL;
175 /* make a non-zero unique key for the substream */
176 int key = (substream->pcm->device << 16) | (substream->number << 2) |
177 (substream->stream + 1);
179 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
180 dev = chip->playback_index_offset;
181 nums = chip->playback_streams;
183 dev = chip->capture_index_offset;
184 nums = chip->capture_streams;
186 for (i = 0; i < nums; i++, dev++) {
187 struct azx_dev *azx_dev = &chip->azx_dev[dev];
189 if (!azx_dev->opened && !dsp_is_locked(azx_dev)) {
190 if (azx_dev->assigned_key == key) {
192 azx_dev->assigned_key = key;
197 (chip->driver_caps & AZX_DCAPS_REVERSE_ASSIGN))
205 res->assigned_key = key;
211 /* release the assigned stream */
212 static inline void azx_release_device(struct azx_dev *azx_dev)
217 static cycle_t azx_cc_read(const struct cyclecounter *cc)
219 struct azx_dev *azx_dev = container_of(cc, struct azx_dev, azx_cc);
220 struct snd_pcm_substream *substream = azx_dev->substream;
221 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
222 struct azx *chip = apcm->chip;
224 return azx_readl(chip, WALLCLK);
227 static void azx_timecounter_init(struct snd_pcm_substream *substream,
228 bool force, cycle_t last)
230 struct azx_dev *azx_dev = get_azx_dev(substream);
231 struct timecounter *tc = &azx_dev->azx_tc;
232 struct cyclecounter *cc = &azx_dev->azx_cc;
235 cc->read = azx_cc_read;
236 cc->mask = CLOCKSOURCE_MASK(32);
239 * Converting from 24 MHz to ns means applying a 125/3 factor.
240 * To avoid any saturation issues in intermediate operations,
241 * the 125 factor is applied first. The division is applied
242 * last after reading the timecounter value.
243 * Applying the 1/3 factor as part of the multiplication
244 * requires at least 20 bits for a decent precision, however
245 * overflows occur after about 4 hours or less, not a option.
248 cc->mult = 125; /* saturation after 195 years */
251 nsec = 0; /* audio time is elapsed time since trigger */
252 timecounter_init(tc, cc, nsec);
255 * force timecounter to use predefined value,
256 * used for synchronized starts
258 tc->cycle_last = last;
261 static u64 azx_adjust_codec_delay(struct snd_pcm_substream *substream,
264 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
265 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
266 u64 codec_frames, codec_nsecs;
268 if (!hinfo->ops.get_delay)
271 codec_frames = hinfo->ops.get_delay(hinfo, apcm->codec, substream);
272 codec_nsecs = div_u64(codec_frames * 1000000000LL,
273 substream->runtime->rate);
275 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
276 return nsec + codec_nsecs;
278 return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
284 static int setup_bdle(struct azx *chip,
285 struct snd_dma_buffer *dmab,
286 struct azx_dev *azx_dev, u32 **bdlp,
287 int ofs, int size, int with_ioc)
295 if (azx_dev->frags >= AZX_MAX_BDL_ENTRIES)
298 addr = snd_sgbuf_get_addr(dmab, ofs);
299 /* program the address field of the BDL entry */
300 bdl[0] = cpu_to_le32((u32)addr);
301 bdl[1] = cpu_to_le32(upper_32_bits(addr));
302 /* program the size field of the BDL entry */
303 chunk = snd_sgbuf_get_chunk_size(dmab, ofs, size);
304 /* one BDLE cannot cross 4K boundary on CTHDA chips */
305 if (chip->driver_caps & AZX_DCAPS_4K_BDLE_BOUNDARY) {
306 u32 remain = 0x1000 - (ofs & 0xfff);
310 bdl[2] = cpu_to_le32(chunk);
311 /* program the IOC to enable interrupt
312 * only when the whole fragment is processed
315 bdl[3] = (size || !with_ioc) ? 0 : cpu_to_le32(0x01);
327 static int azx_setup_periods(struct azx *chip,
328 struct snd_pcm_substream *substream,
329 struct azx_dev *azx_dev)
332 int i, ofs, periods, period_bytes;
335 /* reset BDL address */
336 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
337 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
339 period_bytes = azx_dev->period_bytes;
340 periods = azx_dev->bufsize / period_bytes;
342 /* program the initial BDL entries */
343 bdl = (u32 *)azx_dev->bdl.area;
347 if (chip->bdl_pos_adj)
348 pos_adj = chip->bdl_pos_adj[chip->dev_index];
349 if (!azx_dev->no_period_wakeup && pos_adj > 0) {
350 struct snd_pcm_runtime *runtime = substream->runtime;
351 int pos_align = pos_adj;
352 pos_adj = (pos_adj * runtime->rate + 47999) / 48000;
356 pos_adj = ((pos_adj + pos_align - 1) / pos_align) *
358 pos_adj = frames_to_bytes(runtime, pos_adj);
359 if (pos_adj >= period_bytes) {
360 dev_warn(chip->card->dev,"Too big adjustment %d\n",
364 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
366 &bdl, ofs, pos_adj, true);
373 for (i = 0; i < periods; i++) {
374 if (i == periods - 1 && pos_adj)
375 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
377 period_bytes - pos_adj, 0);
379 ofs = setup_bdle(chip, snd_pcm_get_dma_buf(substream),
382 !azx_dev->no_period_wakeup);
389 dev_err(chip->card->dev, "Too many BDL entries: buffer=%d, period=%d\n",
390 azx_dev->bufsize, period_bytes);
398 static int azx_pcm_close(struct snd_pcm_substream *substream)
400 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
401 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
402 struct azx *chip = apcm->chip;
403 struct azx_dev *azx_dev = get_azx_dev(substream);
406 mutex_lock(&chip->open_mutex);
407 spin_lock_irqsave(&chip->reg_lock, flags);
408 azx_dev->substream = NULL;
409 azx_dev->running = 0;
410 spin_unlock_irqrestore(&chip->reg_lock, flags);
411 azx_release_device(azx_dev);
412 hinfo->ops.close(hinfo, apcm->codec, substream);
413 snd_hda_power_down(apcm->codec);
414 mutex_unlock(&chip->open_mutex);
418 static int azx_pcm_hw_params(struct snd_pcm_substream *substream,
419 struct snd_pcm_hw_params *hw_params)
421 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
422 struct azx *chip = apcm->chip;
425 dsp_lock(get_azx_dev(substream));
426 if (dsp_is_locked(get_azx_dev(substream))) {
431 ret = chip->ops->substream_alloc_pages(chip, substream,
432 params_buffer_bytes(hw_params));
434 dsp_unlock(get_azx_dev(substream));
438 static int azx_pcm_hw_free(struct snd_pcm_substream *substream)
440 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
441 struct azx_dev *azx_dev = get_azx_dev(substream);
442 struct azx *chip = apcm->chip;
443 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
446 /* reset BDL address */
448 if (!dsp_is_locked(azx_dev)) {
449 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
450 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
451 azx_sd_writel(chip, azx_dev, SD_CTL, 0);
452 azx_dev->bufsize = 0;
453 azx_dev->period_bytes = 0;
454 azx_dev->format_val = 0;
457 snd_hda_codec_cleanup(apcm->codec, hinfo, substream);
459 err = chip->ops->substream_free_pages(chip, substream);
460 azx_dev->prepared = 0;
465 static int azx_pcm_prepare(struct snd_pcm_substream *substream)
467 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
468 struct azx *chip = apcm->chip;
469 struct azx_dev *azx_dev = get_azx_dev(substream);
470 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
471 struct snd_pcm_runtime *runtime = substream->runtime;
472 unsigned int bufsize, period_bytes, format_val, stream_tag;
474 struct hda_spdif_out *spdif =
475 snd_hda_spdif_out_of_nid(apcm->codec, hinfo->nid);
476 unsigned short ctls = spdif ? spdif->ctls : 0;
479 if (dsp_is_locked(azx_dev)) {
484 azx_stream_reset(chip, azx_dev);
485 format_val = snd_hda_calc_stream_format(apcm->codec,
492 dev_err(chip->card->dev,
493 "invalid format_val, rate=%d, ch=%d, format=%d\n",
494 runtime->rate, runtime->channels, runtime->format);
499 bufsize = snd_pcm_lib_buffer_bytes(substream);
500 period_bytes = snd_pcm_lib_period_bytes(substream);
502 dev_dbg(chip->card->dev, "azx_pcm_prepare: bufsize=0x%x, format=0x%x\n",
503 bufsize, format_val);
505 if (bufsize != azx_dev->bufsize ||
506 period_bytes != azx_dev->period_bytes ||
507 format_val != azx_dev->format_val ||
508 runtime->no_period_wakeup != azx_dev->no_period_wakeup) {
509 azx_dev->bufsize = bufsize;
510 azx_dev->period_bytes = period_bytes;
511 azx_dev->format_val = format_val;
512 azx_dev->no_period_wakeup = runtime->no_period_wakeup;
513 err = azx_setup_periods(chip, substream, azx_dev);
518 /* when LPIB delay correction gives a small negative value,
519 * we ignore it; currently set the threshold statically to
522 if (runtime->period_size > 64)
523 azx_dev->delay_negative_threshold = -frames_to_bytes(runtime, 64);
525 azx_dev->delay_negative_threshold = 0;
527 /* wallclk has 24Mhz clock source */
528 azx_dev->period_wallclk = (((runtime->period_size * 24000) /
529 runtime->rate) * 1000);
530 azx_setup_controller(chip, azx_dev);
531 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
533 azx_sd_readw(chip, azx_dev, SD_FIFOSIZE) + 1;
535 azx_dev->fifo_size = 0;
537 stream_tag = azx_dev->stream_tag;
538 /* CA-IBG chips need the playback stream starting from 1 */
539 if ((chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND) &&
540 stream_tag > chip->capture_streams)
541 stream_tag -= chip->capture_streams;
542 err = snd_hda_codec_prepare(apcm->codec, hinfo, stream_tag,
543 azx_dev->format_val, substream);
547 azx_dev->prepared = 1;
552 static int azx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
554 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
555 struct azx *chip = apcm->chip;
556 struct azx_dev *azx_dev;
557 struct snd_pcm_substream *s;
558 int rstart = 0, start, nsync = 0, sbits = 0;
561 azx_dev = get_azx_dev(substream);
562 trace_azx_pcm_trigger(chip, azx_dev, cmd);
564 if (dsp_is_locked(azx_dev) || !azx_dev->prepared)
568 case SNDRV_PCM_TRIGGER_START:
570 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
571 case SNDRV_PCM_TRIGGER_RESUME:
574 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
575 case SNDRV_PCM_TRIGGER_SUSPEND:
576 case SNDRV_PCM_TRIGGER_STOP:
583 snd_pcm_group_for_each_entry(s, substream) {
584 if (s->pcm->card != substream->pcm->card)
586 azx_dev = get_azx_dev(s);
587 sbits |= 1 << azx_dev->index;
589 snd_pcm_trigger_done(s, substream);
592 spin_lock(&chip->reg_lock);
594 /* first, set SYNC bits of corresponding streams */
595 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
596 azx_writel(chip, OLD_SSYNC,
597 azx_readl(chip, OLD_SSYNC) | sbits);
599 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) | sbits);
601 snd_pcm_group_for_each_entry(s, substream) {
602 if (s->pcm->card != substream->pcm->card)
604 azx_dev = get_azx_dev(s);
606 azx_dev->start_wallclk = azx_readl(chip, WALLCLK);
608 azx_dev->start_wallclk -=
609 azx_dev->period_wallclk;
610 azx_stream_start(chip, azx_dev);
612 azx_stream_stop(chip, azx_dev);
614 azx_dev->running = start;
616 spin_unlock(&chip->reg_lock);
618 /* wait until all FIFOs get ready */
619 for (timeout = 5000; timeout; timeout--) {
621 snd_pcm_group_for_each_entry(s, substream) {
622 if (s->pcm->card != substream->pcm->card)
624 azx_dev = get_azx_dev(s);
625 if (!(azx_sd_readb(chip, azx_dev, SD_STS) &
634 /* wait until all RUN bits are cleared */
635 for (timeout = 5000; timeout; timeout--) {
637 snd_pcm_group_for_each_entry(s, substream) {
638 if (s->pcm->card != substream->pcm->card)
640 azx_dev = get_azx_dev(s);
641 if (azx_sd_readb(chip, azx_dev, SD_CTL) &
650 spin_lock(&chip->reg_lock);
651 /* reset SYNC bits */
652 if (chip->driver_caps & AZX_DCAPS_OLD_SSYNC)
653 azx_writel(chip, OLD_SSYNC,
654 azx_readl(chip, OLD_SSYNC) & ~sbits);
656 azx_writel(chip, SSYNC, azx_readl(chip, SSYNC) & ~sbits);
658 azx_timecounter_init(substream, 0, 0);
659 snd_pcm_gettime(substream->runtime, &substream->runtime->trigger_tstamp);
660 substream->runtime->trigger_tstamp_latched = true;
665 /* same start cycle for master and group */
666 azx_dev = get_azx_dev(substream);
667 cycle_last = azx_dev->azx_tc.cycle_last;
669 snd_pcm_group_for_each_entry(s, substream) {
670 if (s->pcm->card != substream->pcm->card)
672 azx_timecounter_init(s, 1, cycle_last);
676 spin_unlock(&chip->reg_lock);
680 unsigned int azx_get_pos_lpib(struct azx *chip, struct azx_dev *azx_dev)
682 return azx_sd_readl(chip, azx_dev, SD_LPIB);
684 EXPORT_SYMBOL_GPL(azx_get_pos_lpib);
686 unsigned int azx_get_pos_posbuf(struct azx *chip, struct azx_dev *azx_dev)
688 return le32_to_cpu(*azx_dev->posbuf);
690 EXPORT_SYMBOL_GPL(azx_get_pos_posbuf);
692 unsigned int azx_get_position(struct azx *chip,
693 struct azx_dev *azx_dev)
695 struct snd_pcm_substream *substream = azx_dev->substream;
697 int stream = substream->stream;
700 if (chip->get_position[stream])
701 pos = chip->get_position[stream](chip, azx_dev);
702 else /* use the position buffer as default */
703 pos = azx_get_pos_posbuf(chip, azx_dev);
705 if (pos >= azx_dev->bufsize)
708 if (substream->runtime) {
709 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
710 struct hda_pcm_stream *hinfo = apcm->hinfo[stream];
712 if (chip->get_delay[stream])
713 delay += chip->get_delay[stream](chip, azx_dev, pos);
714 if (hinfo->ops.get_delay)
715 delay += hinfo->ops.get_delay(hinfo, apcm->codec,
717 substream->runtime->delay = delay;
720 trace_azx_get_position(chip, azx_dev, pos, delay);
723 EXPORT_SYMBOL_GPL(azx_get_position);
725 static snd_pcm_uframes_t azx_pcm_pointer(struct snd_pcm_substream *substream)
727 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
728 struct azx *chip = apcm->chip;
729 struct azx_dev *azx_dev = get_azx_dev(substream);
730 return bytes_to_frames(substream->runtime,
731 azx_get_position(chip, azx_dev));
734 static int azx_get_wallclock_tstamp(struct snd_pcm_substream *substream,
737 struct azx_dev *azx_dev = get_azx_dev(substream);
740 nsec = timecounter_read(&azx_dev->azx_tc);
741 nsec = div_u64(nsec, 3); /* can be optimized */
742 nsec = azx_adjust_codec_delay(substream, nsec);
744 *ts = ns_to_timespec(nsec);
749 static struct snd_pcm_hardware azx_pcm_hw = {
750 .info = (SNDRV_PCM_INFO_MMAP |
751 SNDRV_PCM_INFO_INTERLEAVED |
752 SNDRV_PCM_INFO_BLOCK_TRANSFER |
753 SNDRV_PCM_INFO_MMAP_VALID |
754 /* No full-resume yet implemented */
755 /* SNDRV_PCM_INFO_RESUME |*/
756 SNDRV_PCM_INFO_PAUSE |
757 SNDRV_PCM_INFO_SYNC_START |
758 SNDRV_PCM_INFO_HAS_WALL_CLOCK |
759 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
760 .formats = SNDRV_PCM_FMTBIT_S16_LE,
761 .rates = SNDRV_PCM_RATE_48000,
766 .buffer_bytes_max = AZX_MAX_BUF_SIZE,
767 .period_bytes_min = 128,
768 .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
770 .periods_max = AZX_MAX_FRAG,
774 static int azx_pcm_open(struct snd_pcm_substream *substream)
776 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
777 struct hda_pcm_stream *hinfo = apcm->hinfo[substream->stream];
778 struct azx *chip = apcm->chip;
779 struct azx_dev *azx_dev;
780 struct snd_pcm_runtime *runtime = substream->runtime;
785 mutex_lock(&chip->open_mutex);
786 azx_dev = azx_assign_device(chip, substream);
787 if (azx_dev == NULL) {
788 mutex_unlock(&chip->open_mutex);
791 runtime->hw = azx_pcm_hw;
792 runtime->hw.channels_min = hinfo->channels_min;
793 runtime->hw.channels_max = hinfo->channels_max;
794 runtime->hw.formats = hinfo->formats;
795 runtime->hw.rates = hinfo->rates;
796 snd_pcm_limit_hw_rates(runtime);
797 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
799 /* avoid wrap-around with wall-clock */
800 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
804 if (chip->align_buffer_size)
805 /* constrain buffer sizes to be multiple of 128
806 bytes. This is more efficient in terms of memory
807 access but isn't required by the HDA spec and
808 prevents users from specifying exact period/buffer
809 sizes. For example for 44.1kHz, a period size set
810 to 20ms will be rounded to 19.59ms. */
813 /* Don't enforce steps on buffer sizes, still need to
814 be multiple of 4 bytes (HDA spec). Tested on Intel
815 HDA controllers, may not work on all devices where
816 option needs to be disabled */
819 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
821 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
823 snd_hda_power_up_d3wait(apcm->codec);
824 err = hinfo->ops.open(hinfo, apcm->codec, substream);
826 azx_release_device(azx_dev);
827 snd_hda_power_down(apcm->codec);
828 mutex_unlock(&chip->open_mutex);
831 snd_pcm_limit_hw_rates(runtime);
833 if (snd_BUG_ON(!runtime->hw.channels_min) ||
834 snd_BUG_ON(!runtime->hw.channels_max) ||
835 snd_BUG_ON(!runtime->hw.formats) ||
836 snd_BUG_ON(!runtime->hw.rates)) {
837 azx_release_device(azx_dev);
838 hinfo->ops.close(hinfo, apcm->codec, substream);
839 snd_hda_power_down(apcm->codec);
840 mutex_unlock(&chip->open_mutex);
844 /* disable WALLCLOCK timestamps for capture streams
845 until we figure out how to handle digital inputs */
846 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
847 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK;
849 spin_lock_irqsave(&chip->reg_lock, flags);
850 azx_dev->substream = substream;
851 azx_dev->running = 0;
852 spin_unlock_irqrestore(&chip->reg_lock, flags);
854 runtime->private_data = azx_dev;
855 snd_pcm_set_sync(substream);
856 mutex_unlock(&chip->open_mutex);
860 static int azx_pcm_mmap(struct snd_pcm_substream *substream,
861 struct vm_area_struct *area)
863 struct azx_pcm *apcm = snd_pcm_substream_chip(substream);
864 struct azx *chip = apcm->chip;
865 if (chip->ops->pcm_mmap_prepare)
866 chip->ops->pcm_mmap_prepare(substream, area);
867 return snd_pcm_lib_default_mmap(substream, area);
870 static struct snd_pcm_ops azx_pcm_ops = {
871 .open = azx_pcm_open,
872 .close = azx_pcm_close,
873 .ioctl = snd_pcm_lib_ioctl,
874 .hw_params = azx_pcm_hw_params,
875 .hw_free = azx_pcm_hw_free,
876 .prepare = azx_pcm_prepare,
877 .trigger = azx_pcm_trigger,
878 .pointer = azx_pcm_pointer,
879 .wall_clock = azx_get_wallclock_tstamp,
880 .mmap = azx_pcm_mmap,
881 .page = snd_pcm_sgbuf_ops_page,
884 static void azx_pcm_free(struct snd_pcm *pcm)
886 struct azx_pcm *apcm = pcm->private_data;
888 list_del(&apcm->list);
893 #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
895 static int azx_attach_pcm_stream(struct hda_bus *bus, struct hda_codec *codec,
896 struct hda_pcm *cpcm)
898 struct azx *chip = bus->private_data;
900 struct azx_pcm *apcm;
901 int pcm_dev = cpcm->device;
905 list_for_each_entry(apcm, &chip->pcm_list, list) {
906 if (apcm->pcm->device == pcm_dev) {
907 dev_err(chip->card->dev, "PCM %d already exists\n",
912 err = snd_pcm_new(chip->card, cpcm->name, pcm_dev,
913 cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
914 cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams,
918 strlcpy(pcm->name, cpcm->name, sizeof(pcm->name));
919 apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
925 pcm->private_data = apcm;
926 pcm->private_free = azx_pcm_free;
927 if (cpcm->pcm_type == HDA_PCM_TYPE_MODEM)
928 pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
929 list_add_tail(&apcm->list, &chip->pcm_list);
931 for (s = 0; s < 2; s++) {
932 apcm->hinfo[s] = &cpcm->stream[s];
933 if (cpcm->stream[s].substreams)
934 snd_pcm_set_ops(pcm, s, &azx_pcm_ops);
936 /* buffer pre-allocation */
937 size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
938 if (size > MAX_PREALLOC_SIZE)
939 size = MAX_PREALLOC_SIZE;
940 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
942 size, MAX_PREALLOC_SIZE);
944 for (s = 0; s < 2; s++)
945 pcm->streams[s].dev.parent = &codec->dev;
950 * CORB / RIRB interface
952 static int azx_alloc_cmd_io(struct azx *chip)
956 /* single page (at least 4096 bytes) must suffice for both ringbuffes */
957 err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV,
958 PAGE_SIZE, &chip->rb);
960 dev_err(chip->card->dev, "cannot allocate CORB/RIRB\n");
963 EXPORT_SYMBOL_GPL(azx_alloc_cmd_io);
965 static void azx_init_cmd_io(struct azx *chip)
969 spin_lock_irq(&chip->reg_lock);
971 chip->corb.addr = chip->rb.addr;
972 chip->corb.buf = (u32 *)chip->rb.area;
973 azx_writel(chip, CORBLBASE, (u32)chip->corb.addr);
974 azx_writel(chip, CORBUBASE, upper_32_bits(chip->corb.addr));
976 /* set the corb size to 256 entries (ULI requires explicitly) */
977 azx_writeb(chip, CORBSIZE, 0x02);
978 /* set the corb write pointer to 0 */
979 azx_writew(chip, CORBWP, 0);
981 /* reset the corb hw read pointer */
982 azx_writew(chip, CORBRP, AZX_CORBRP_RST);
983 if (!(chip->driver_caps & AZX_DCAPS_CORBRP_SELF_CLEAR)) {
984 for (timeout = 1000; timeout > 0; timeout--) {
985 if ((azx_readw(chip, CORBRP) & AZX_CORBRP_RST) == AZX_CORBRP_RST)
990 dev_err(chip->card->dev, "CORB reset timeout#1, CORBRP = %d\n",
991 azx_readw(chip, CORBRP));
993 azx_writew(chip, CORBRP, 0);
994 for (timeout = 1000; timeout > 0; timeout--) {
995 if (azx_readw(chip, CORBRP) == 0)
1000 dev_err(chip->card->dev, "CORB reset timeout#2, CORBRP = %d\n",
1001 azx_readw(chip, CORBRP));
1004 /* enable corb dma */
1005 azx_writeb(chip, CORBCTL, AZX_CORBCTL_RUN);
1008 chip->rirb.addr = chip->rb.addr + 2048;
1009 chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
1010 chip->rirb.wp = chip->rirb.rp = 0;
1011 memset(chip->rirb.cmds, 0, sizeof(chip->rirb.cmds));
1012 azx_writel(chip, RIRBLBASE, (u32)chip->rirb.addr);
1013 azx_writel(chip, RIRBUBASE, upper_32_bits(chip->rirb.addr));
1015 /* set the rirb size to 256 entries (ULI requires explicitly) */
1016 azx_writeb(chip, RIRBSIZE, 0x02);
1017 /* reset the rirb hw write pointer */
1018 azx_writew(chip, RIRBWP, AZX_RIRBWP_RST);
1019 /* set N=1, get RIRB response interrupt for new entry */
1020 if (chip->driver_caps & AZX_DCAPS_CTX_WORKAROUND)
1021 azx_writew(chip, RINTCNT, 0xc0);
1023 azx_writew(chip, RINTCNT, 1);
1024 /* enable rirb dma and response irq */
1025 azx_writeb(chip, RIRBCTL, AZX_RBCTL_DMA_EN | AZX_RBCTL_IRQ_EN);
1026 spin_unlock_irq(&chip->reg_lock);
1028 EXPORT_SYMBOL_GPL(azx_init_cmd_io);
1030 static void azx_free_cmd_io(struct azx *chip)
1032 spin_lock_irq(&chip->reg_lock);
1033 /* disable ringbuffer DMAs */
1034 azx_writeb(chip, RIRBCTL, 0);
1035 azx_writeb(chip, CORBCTL, 0);
1036 spin_unlock_irq(&chip->reg_lock);
1038 EXPORT_SYMBOL_GPL(azx_free_cmd_io);
1040 static unsigned int azx_command_addr(u32 cmd)
1042 unsigned int addr = cmd >> 28;
1044 if (addr >= AZX_MAX_CODECS) {
1052 /* send a command */
1053 static int azx_corb_send_cmd(struct hda_bus *bus, u32 val)
1055 struct azx *chip = bus->private_data;
1056 unsigned int addr = azx_command_addr(val);
1057 unsigned int wp, rp;
1059 spin_lock_irq(&chip->reg_lock);
1061 /* add command to corb */
1062 wp = azx_readw(chip, CORBWP);
1064 /* something wrong, controller likely turned to D3 */
1065 spin_unlock_irq(&chip->reg_lock);
1069 wp %= AZX_MAX_CORB_ENTRIES;
1071 rp = azx_readw(chip, CORBRP);
1073 /* oops, it's full */
1074 spin_unlock_irq(&chip->reg_lock);
1078 chip->rirb.cmds[addr]++;
1079 chip->corb.buf[wp] = cpu_to_le32(val);
1080 azx_writew(chip, CORBWP, wp);
1082 spin_unlock_irq(&chip->reg_lock);
1087 #define AZX_RIRB_EX_UNSOL_EV (1<<4)
1089 /* retrieve RIRB entry - called from interrupt handler */
1090 static void azx_update_rirb(struct azx *chip)
1092 unsigned int rp, wp;
1096 wp = azx_readw(chip, RIRBWP);
1098 /* something wrong, controller likely turned to D3 */
1102 if (wp == chip->rirb.wp)
1106 while (chip->rirb.rp != wp) {
1108 chip->rirb.rp %= AZX_MAX_RIRB_ENTRIES;
1110 rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
1111 res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
1112 res = le32_to_cpu(chip->rirb.buf[rp]);
1113 addr = res_ex & 0xf;
1114 if ((addr >= AZX_MAX_CODECS) || !(chip->codec_mask & (1 << addr))) {
1115 dev_err(chip->card->dev, "spurious response %#x:%#x, rp = %d, wp = %d",
1119 } else if (res_ex & AZX_RIRB_EX_UNSOL_EV)
1120 snd_hda_queue_unsol_event(chip->bus, res, res_ex);
1121 else if (chip->rirb.cmds[addr]) {
1122 chip->rirb.res[addr] = res;
1124 chip->rirb.cmds[addr]--;
1125 } else if (printk_ratelimit()) {
1126 dev_err(chip->card->dev, "spurious response %#x:%#x, last cmd=%#08x\n",
1128 chip->last_cmd[addr]);
1133 /* receive a response */
1134 static unsigned int azx_rirb_get_response(struct hda_bus *bus,
1137 struct azx *chip = bus->private_data;
1138 unsigned long timeout;
1139 unsigned long loopcounter;
1143 timeout = jiffies + msecs_to_jiffies(1000);
1145 for (loopcounter = 0;; loopcounter++) {
1146 if (chip->polling_mode || do_poll) {
1147 spin_lock_irq(&chip->reg_lock);
1148 azx_update_rirb(chip);
1149 spin_unlock_irq(&chip->reg_lock);
1151 if (!chip->rirb.cmds[addr]) {
1153 bus->rirb_error = 0;
1156 chip->poll_count = 0;
1157 return chip->rirb.res[addr]; /* the last value */
1159 if (time_after(jiffies, timeout))
1161 if (bus->needs_damn_long_delay || loopcounter > 3000)
1162 msleep(2); /* temporary workaround */
1169 if (!bus->no_response_fallback)
1172 if (!chip->polling_mode && chip->poll_count < 2) {
1173 dev_dbg(chip->card->dev,
1174 "azx_get_response timeout, polling the codec once: last cmd=0x%08x\n",
1175 chip->last_cmd[addr]);
1182 if (!chip->polling_mode) {
1183 dev_warn(chip->card->dev,
1184 "azx_get_response timeout, switching to polling mode: last cmd=0x%08x\n",
1185 chip->last_cmd[addr]);
1186 chip->polling_mode = 1;
1191 dev_warn(chip->card->dev,
1192 "No response from codec, disabling MSI: last cmd=0x%08x\n",
1193 chip->last_cmd[addr]);
1194 if (chip->ops->disable_msi_reset_irq(chip) &&
1195 chip->ops->disable_msi_reset_irq(chip) < 0) {
1196 bus->rirb_error = 1;
1202 if (chip->probing) {
1203 /* If this critical timeout happens during the codec probing
1204 * phase, this is likely an access to a non-existing codec
1205 * slot. Better to return an error and reset the system.
1210 /* a fatal communication error; need either to reset or to fallback
1211 * to the single_cmd mode
1213 bus->rirb_error = 1;
1214 if (bus->allow_bus_reset && !bus->response_reset && !bus->in_reset) {
1215 bus->response_reset = 1;
1216 return -1; /* give a chance to retry */
1219 dev_err(chip->card->dev,
1220 "azx_get_response timeout, switching to single_cmd mode: last cmd=0x%08x\n",
1221 chip->last_cmd[addr]);
1222 chip->single_cmd = 1;
1223 bus->response_reset = 0;
1224 /* release CORB/RIRB */
1225 azx_free_cmd_io(chip);
1226 /* disable unsolicited responses */
1227 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~AZX_GCTL_UNSOL);
1232 * Use the single immediate command instead of CORB/RIRB for simplicity
1234 * Note: according to Intel, this is not preferred use. The command was
1235 * intended for the BIOS only, and may get confused with unsolicited
1236 * responses. So, we shouldn't use it for normal operation from the
1238 * I left the codes, however, for debugging/testing purposes.
1241 /* receive a response */
1242 static int azx_single_wait_for_response(struct azx *chip, unsigned int addr)
1247 /* check IRV busy bit */
1248 if (azx_readw(chip, IRS) & AZX_IRS_VALID) {
1249 /* reuse rirb.res as the response return value */
1250 chip->rirb.res[addr] = azx_readl(chip, IR);
1255 if (printk_ratelimit())
1256 dev_dbg(chip->card->dev, "get_response timeout: IRS=0x%x\n",
1257 azx_readw(chip, IRS));
1258 chip->rirb.res[addr] = -1;
1262 /* send a command */
1263 static int azx_single_send_cmd(struct hda_bus *bus, u32 val)
1265 struct azx *chip = bus->private_data;
1266 unsigned int addr = azx_command_addr(val);
1269 bus->rirb_error = 0;
1271 /* check ICB busy bit */
1272 if (!((azx_readw(chip, IRS) & AZX_IRS_BUSY))) {
1273 /* Clear IRV valid bit */
1274 azx_writew(chip, IRS, azx_readw(chip, IRS) |
1276 azx_writel(chip, IC, val);
1277 azx_writew(chip, IRS, azx_readw(chip, IRS) |
1279 return azx_single_wait_for_response(chip, addr);
1283 if (printk_ratelimit())
1284 dev_dbg(chip->card->dev,
1285 "send_cmd timeout: IRS=0x%x, val=0x%x\n",
1286 azx_readw(chip, IRS), val);
1290 /* receive a response */
1291 static unsigned int azx_single_get_response(struct hda_bus *bus,
1294 struct azx *chip = bus->private_data;
1295 return chip->rirb.res[addr];
1299 * The below are the main callbacks from hda_codec.
1301 * They are just the skeleton to call sub-callbacks according to the
1302 * current setting of chip->single_cmd.
1305 /* send a command */
1306 static int azx_send_cmd(struct hda_bus *bus, unsigned int val)
1308 struct azx *chip = bus->private_data;
1312 chip->last_cmd[azx_command_addr(val)] = val;
1313 if (chip->single_cmd)
1314 return azx_single_send_cmd(bus, val);
1316 return azx_corb_send_cmd(bus, val);
1318 EXPORT_SYMBOL_GPL(azx_send_cmd);
1320 /* get a response */
1321 static unsigned int azx_get_response(struct hda_bus *bus,
1324 struct azx *chip = bus->private_data;
1327 if (chip->single_cmd)
1328 return azx_single_get_response(bus, addr);
1330 return azx_rirb_get_response(bus, addr);
1332 EXPORT_SYMBOL_GPL(azx_get_response);
1334 #ifdef CONFIG_SND_HDA_DSP_LOADER
1336 * DSP loading code (e.g. for CA0132)
1339 /* use the first stream for loading DSP */
1340 static struct azx_dev *
1341 azx_get_dsp_loader_dev(struct azx *chip)
1343 return &chip->azx_dev[chip->playback_index_offset];
1346 static int azx_load_dsp_prepare(struct hda_bus *bus, unsigned int format,
1347 unsigned int byte_size,
1348 struct snd_dma_buffer *bufp)
1351 struct azx *chip = bus->private_data;
1352 struct azx_dev *azx_dev;
1355 azx_dev = azx_get_dsp_loader_dev(chip);
1358 spin_lock_irq(&chip->reg_lock);
1359 if (azx_dev->running || azx_dev->locked) {
1360 spin_unlock_irq(&chip->reg_lock);
1364 azx_dev->prepared = 0;
1365 chip->saved_azx_dev = *azx_dev;
1366 azx_dev->locked = 1;
1367 spin_unlock_irq(&chip->reg_lock);
1369 err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV_SG,
1374 azx_dev->bufsize = byte_size;
1375 azx_dev->period_bytes = byte_size;
1376 azx_dev->format_val = format;
1378 azx_stream_reset(chip, azx_dev);
1380 /* reset BDL address */
1381 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
1382 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
1385 bdl = (u32 *)azx_dev->bdl.area;
1386 err = setup_bdle(chip, bufp, azx_dev, &bdl, 0, byte_size, 0);
1390 azx_setup_controller(chip, azx_dev);
1391 dsp_unlock(azx_dev);
1392 return azx_dev->stream_tag;
1395 chip->ops->dma_free_pages(chip, bufp);
1397 spin_lock_irq(&chip->reg_lock);
1398 if (azx_dev->opened)
1399 *azx_dev = chip->saved_azx_dev;
1400 azx_dev->locked = 0;
1401 spin_unlock_irq(&chip->reg_lock);
1403 dsp_unlock(azx_dev);
1407 static void azx_load_dsp_trigger(struct hda_bus *bus, bool start)
1409 struct azx *chip = bus->private_data;
1410 struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
1413 azx_stream_start(chip, azx_dev);
1415 azx_stream_stop(chip, azx_dev);
1416 azx_dev->running = start;
1419 static void azx_load_dsp_cleanup(struct hda_bus *bus,
1420 struct snd_dma_buffer *dmab)
1422 struct azx *chip = bus->private_data;
1423 struct azx_dev *azx_dev = azx_get_dsp_loader_dev(chip);
1425 if (!dmab->area || !azx_dev->locked)
1429 /* reset BDL address */
1430 azx_sd_writel(chip, azx_dev, SD_BDLPL, 0);
1431 azx_sd_writel(chip, azx_dev, SD_BDLPU, 0);
1432 azx_sd_writel(chip, azx_dev, SD_CTL, 0);
1433 azx_dev->bufsize = 0;
1434 azx_dev->period_bytes = 0;
1435 azx_dev->format_val = 0;
1437 chip->ops->dma_free_pages(chip, dmab);
1440 spin_lock_irq(&chip->reg_lock);
1441 if (azx_dev->opened)
1442 *azx_dev = chip->saved_azx_dev;
1443 azx_dev->locked = 0;
1444 spin_unlock_irq(&chip->reg_lock);
1445 dsp_unlock(azx_dev);
1447 #endif /* CONFIG_SND_HDA_DSP_LOADER */
1449 int azx_alloc_stream_pages(struct azx *chip)
1452 struct snd_card *card = chip->card;
1454 for (i = 0; i < chip->num_streams; i++) {
1455 dsp_lock_init(&chip->azx_dev[i]);
1456 /* allocate memory for the BDL for each stream */
1457 err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV,
1459 &chip->azx_dev[i].bdl);
1461 dev_err(card->dev, "cannot allocate BDL\n");
1465 /* allocate memory for the position buffer */
1466 err = chip->ops->dma_alloc_pages(chip, SNDRV_DMA_TYPE_DEV,
1467 chip->num_streams * 8, &chip->posbuf);
1469 dev_err(card->dev, "cannot allocate posbuf\n");
1473 /* allocate CORB/RIRB */
1474 err = azx_alloc_cmd_io(chip);
1479 EXPORT_SYMBOL_GPL(azx_alloc_stream_pages);
1481 void azx_free_stream_pages(struct azx *chip)
1484 if (chip->azx_dev) {
1485 for (i = 0; i < chip->num_streams; i++)
1486 if (chip->azx_dev[i].bdl.area)
1487 chip->ops->dma_free_pages(
1488 chip, &chip->azx_dev[i].bdl);
1491 chip->ops->dma_free_pages(chip, &chip->rb);
1492 if (chip->posbuf.area)
1493 chip->ops->dma_free_pages(chip, &chip->posbuf);
1495 EXPORT_SYMBOL_GPL(azx_free_stream_pages);
1498 * Lowlevel interface
1501 /* enter link reset */
1502 void azx_enter_link_reset(struct azx *chip)
1504 unsigned long timeout;
1506 /* reset controller */
1507 azx_writel(chip, GCTL, azx_readl(chip, GCTL) & ~AZX_GCTL_RESET);
1509 timeout = jiffies + msecs_to_jiffies(100);
1510 while ((azx_readb(chip, GCTL) & AZX_GCTL_RESET) &&
1511 time_before(jiffies, timeout))
1512 usleep_range(500, 1000);
1514 EXPORT_SYMBOL_GPL(azx_enter_link_reset);
1516 /* exit link reset */
1517 static void azx_exit_link_reset(struct azx *chip)
1519 unsigned long timeout;
1521 azx_writeb(chip, GCTL, azx_readb(chip, GCTL) | AZX_GCTL_RESET);
1523 timeout = jiffies + msecs_to_jiffies(100);
1524 while (!azx_readb(chip, GCTL) &&
1525 time_before(jiffies, timeout))
1526 usleep_range(500, 1000);
1529 /* reset codec link */
1530 static int azx_reset(struct azx *chip, bool full_reset)
1535 /* clear STATESTS */
1536 azx_writew(chip, STATESTS, STATESTS_INT_MASK);
1538 /* reset controller */
1539 azx_enter_link_reset(chip);
1541 /* delay for >= 100us for codec PLL to settle per spec
1542 * Rev 0.9 section 5.5.1
1544 usleep_range(500, 1000);
1546 /* Bring controller out of reset */
1547 azx_exit_link_reset(chip);
1549 /* Brent Chartrand said to wait >= 540us for codecs to initialize */
1550 usleep_range(1000, 1200);
1553 /* check to see if controller is ready */
1554 if (!azx_readb(chip, GCTL)) {
1555 dev_dbg(chip->card->dev, "azx_reset: controller not ready!\n");
1559 /* Accept unsolicited responses */
1560 if (!chip->single_cmd)
1561 azx_writel(chip, GCTL, azx_readl(chip, GCTL) |
1565 if (!chip->codec_mask) {
1566 chip->codec_mask = azx_readw(chip, STATESTS);
1567 dev_dbg(chip->card->dev, "codec_mask = 0x%x\n",
1574 /* enable interrupts */
1575 static void azx_int_enable(struct azx *chip)
1577 /* enable controller CIE and GIE */
1578 azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) |
1579 AZX_INT_CTRL_EN | AZX_INT_GLOBAL_EN);
1582 /* disable interrupts */
1583 static void azx_int_disable(struct azx *chip)
1587 /* disable interrupts in stream descriptor */
1588 for (i = 0; i < chip->num_streams; i++) {
1589 struct azx_dev *azx_dev = &chip->azx_dev[i];
1590 azx_sd_writeb(chip, azx_dev, SD_CTL,
1591 azx_sd_readb(chip, azx_dev, SD_CTL) &
1595 /* disable SIE for all streams */
1596 azx_writeb(chip, INTCTL, 0);
1598 /* disable controller CIE and GIE */
1599 azx_writel(chip, INTCTL, azx_readl(chip, INTCTL) &
1600 ~(AZX_INT_CTRL_EN | AZX_INT_GLOBAL_EN));
1603 /* clear interrupts */
1604 static void azx_int_clear(struct azx *chip)
1608 /* clear stream status */
1609 for (i = 0; i < chip->num_streams; i++) {
1610 struct azx_dev *azx_dev = &chip->azx_dev[i];
1611 azx_sd_writeb(chip, azx_dev, SD_STS, SD_INT_MASK);
1614 /* clear STATESTS */
1615 azx_writew(chip, STATESTS, STATESTS_INT_MASK);
1617 /* clear rirb status */
1618 azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
1620 /* clear int status */
1621 azx_writel(chip, INTSTS, AZX_INT_CTRL_EN | AZX_INT_ALL_STREAM);
1625 * reset and start the controller registers
1627 void azx_init_chip(struct azx *chip, bool full_reset)
1629 if (chip->initialized)
1632 /* reset controller */
1633 azx_reset(chip, full_reset);
1635 /* initialize interrupts */
1636 azx_int_clear(chip);
1637 azx_int_enable(chip);
1639 /* initialize the codec command I/O */
1640 if (!chip->single_cmd)
1641 azx_init_cmd_io(chip);
1643 /* program the position buffer */
1644 azx_writel(chip, DPLBASE, (u32)chip->posbuf.addr);
1645 azx_writel(chip, DPUBASE, upper_32_bits(chip->posbuf.addr));
1647 chip->initialized = 1;
1649 EXPORT_SYMBOL_GPL(azx_init_chip);
1651 void azx_stop_chip(struct azx *chip)
1653 if (!chip->initialized)
1656 /* disable interrupts */
1657 azx_int_disable(chip);
1658 azx_int_clear(chip);
1660 /* disable CORB/RIRB */
1661 azx_free_cmd_io(chip);
1663 /* disable position buffer */
1664 azx_writel(chip, DPLBASE, 0);
1665 azx_writel(chip, DPUBASE, 0);
1667 chip->initialized = 0;
1669 EXPORT_SYMBOL_GPL(azx_stop_chip);
1674 irqreturn_t azx_interrupt(int irq, void *dev_id)
1676 struct azx *chip = dev_id;
1677 struct azx_dev *azx_dev;
1683 if (azx_has_pm_runtime(chip))
1684 if (!pm_runtime_active(chip->card->dev))
1688 spin_lock(&chip->reg_lock);
1690 if (chip->disabled) {
1691 spin_unlock(&chip->reg_lock);
1695 status = azx_readl(chip, INTSTS);
1696 if (status == 0 || status == 0xffffffff) {
1697 spin_unlock(&chip->reg_lock);
1701 for (i = 0; i < chip->num_streams; i++) {
1702 azx_dev = &chip->azx_dev[i];
1703 if (status & azx_dev->sd_int_sta_mask) {
1704 sd_status = azx_sd_readb(chip, azx_dev, SD_STS);
1705 azx_sd_writeb(chip, azx_dev, SD_STS, SD_INT_MASK);
1706 if (!azx_dev->substream || !azx_dev->running ||
1707 !(sd_status & SD_INT_COMPLETE))
1709 /* check whether this IRQ is really acceptable */
1710 if (!chip->ops->position_check ||
1711 chip->ops->position_check(chip, azx_dev)) {
1712 spin_unlock(&chip->reg_lock);
1713 snd_pcm_period_elapsed(azx_dev->substream);
1714 spin_lock(&chip->reg_lock);
1719 /* clear rirb int */
1720 status = azx_readb(chip, RIRBSTS);
1721 if (status & RIRB_INT_MASK) {
1722 if (status & RIRB_INT_RESPONSE) {
1723 if (chip->driver_caps & AZX_DCAPS_RIRB_PRE_DELAY)
1725 azx_update_rirb(chip);
1727 azx_writeb(chip, RIRBSTS, RIRB_INT_MASK);
1730 spin_unlock(&chip->reg_lock);
1734 EXPORT_SYMBOL_GPL(azx_interrupt);
1741 * Probe the given codec address
1743 static int probe_codec(struct azx *chip, int addr)
1745 unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
1746 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
1749 mutex_lock(&chip->bus->cmd_mutex);
1751 azx_send_cmd(chip->bus, cmd);
1752 res = azx_get_response(chip->bus, addr);
1754 mutex_unlock(&chip->bus->cmd_mutex);
1757 dev_dbg(chip->card->dev, "codec #%d probed OK\n", addr);
1761 static void azx_bus_reset(struct hda_bus *bus)
1763 struct azx *chip = bus->private_data;
1766 azx_stop_chip(chip);
1767 azx_init_chip(chip, true);
1769 if (chip->initialized) {
1771 list_for_each_entry(p, &chip->pcm_list, list)
1772 snd_pcm_suspend_all(p->pcm);
1773 snd_hda_suspend(chip->bus);
1774 snd_hda_resume(chip->bus);
1781 /* power-up/down the controller */
1782 static void azx_power_notify(struct hda_bus *bus, bool power_up)
1784 struct azx *chip = bus->private_data;
1786 if (!azx_has_pm_runtime(chip))
1790 pm_runtime_get_sync(chip->card->dev);
1792 pm_runtime_put_sync(chip->card->dev);
1796 static int get_jackpoll_interval(struct azx *chip)
1801 if (!chip->jackpoll_ms)
1804 i = chip->jackpoll_ms[chip->dev_index];
1807 if (i < 50 || i > 60000)
1810 j = msecs_to_jiffies(i);
1812 dev_warn(chip->card->dev,
1813 "jackpoll_ms value out of range: %d\n", i);
1817 static struct hda_bus_ops bus_ops = {
1818 .command = azx_send_cmd,
1819 .get_response = azx_get_response,
1820 .attach_pcm = azx_attach_pcm_stream,
1821 .bus_reset = azx_bus_reset,
1823 .pm_notify = azx_power_notify,
1825 #ifdef CONFIG_SND_HDA_DSP_LOADER
1826 .load_dsp_prepare = azx_load_dsp_prepare,
1827 .load_dsp_trigger = azx_load_dsp_trigger,
1828 .load_dsp_cleanup = azx_load_dsp_cleanup,
1832 /* HD-audio bus initialization */
1833 int azx_bus_create(struct azx *chip, const char *model, int *power_save_to)
1835 struct hda_bus *bus;
1838 err = snd_hda_bus_new(chip->card, &bus);
1843 bus->private_data = chip;
1844 bus->pci = chip->pci;
1845 bus->modelname = model;
1848 bus->power_save = power_save_to;
1851 if (chip->driver_caps & AZX_DCAPS_RIRB_DELAY) {
1852 dev_dbg(chip->card->dev, "Enable delay in RIRB handling\n");
1853 bus->needs_damn_long_delay = 1;
1856 /* AMD chipsets often cause the communication stalls upon certain
1857 * sequence like the pin-detection. It seems that forcing the synced
1858 * access works around the stall. Grrr...
1860 if (chip->driver_caps & AZX_DCAPS_SYNC_WRITE) {
1861 dev_dbg(chip->card->dev, "Enable sync_write for stable communication\n");
1862 bus->sync_write = 1;
1863 bus->allow_bus_reset = 1;
1868 EXPORT_SYMBOL_GPL(azx_bus_create);
1871 int azx_probe_codecs(struct azx *chip, unsigned int max_slots)
1873 struct hda_bus *bus = chip->bus;
1878 max_slots = AZX_DEFAULT_CODECS;
1880 /* First try to probe all given codec slots */
1881 for (c = 0; c < max_slots; c++) {
1882 if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
1883 if (probe_codec(chip, c) < 0) {
1884 /* Some BIOSen give you wrong codec addresses
1887 dev_warn(chip->card->dev,
1888 "Codec #%d probe error; disabling it...\n", c);
1889 chip->codec_mask &= ~(1 << c);
1890 /* More badly, accessing to a non-existing
1891 * codec often screws up the controller chip,
1892 * and disturbs the further communications.
1893 * Thus if an error occurs during probing,
1894 * better to reset the controller chip to
1895 * get back to the sanity state.
1897 azx_stop_chip(chip);
1898 azx_init_chip(chip, true);
1903 /* Then create codec instances */
1904 for (c = 0; c < max_slots; c++) {
1905 if ((chip->codec_mask & (1 << c)) & chip->codec_probe_mask) {
1906 struct hda_codec *codec;
1907 err = snd_hda_codec_new(bus, c, &codec);
1910 codec->jackpoll_interval = get_jackpoll_interval(chip);
1911 codec->beep_mode = chip->beep_mode;
1916 dev_err(chip->card->dev, "no codecs initialized\n");
1921 EXPORT_SYMBOL_GPL(azx_probe_codecs);
1923 /* configure each codec instance */
1924 int azx_codec_configure(struct azx *chip)
1926 struct hda_codec *codec;
1927 list_for_each_entry(codec, &chip->bus->codec_list, list) {
1928 snd_hda_codec_configure(codec);
1932 EXPORT_SYMBOL_GPL(azx_codec_configure);
1935 static bool is_input_stream(struct azx *chip, unsigned char index)
1937 return (index >= chip->capture_index_offset &&
1938 index < chip->capture_index_offset + chip->capture_streams);
1941 /* initialize SD streams */
1942 int azx_init_stream(struct azx *chip)
1945 int in_stream_tag = 0;
1946 int out_stream_tag = 0;
1948 /* initialize each stream (aka device)
1949 * assign the starting bdl address to each stream (device)
1952 for (i = 0; i < chip->num_streams; i++) {
1953 struct azx_dev *azx_dev = &chip->azx_dev[i];
1954 azx_dev->posbuf = (u32 __iomem *)(chip->posbuf.area + i * 8);
1955 /* offset: SDI0=0x80, SDI1=0xa0, ... SDO3=0x160 */
1956 azx_dev->sd_addr = chip->remap_addr + (0x20 * i + 0x80);
1957 /* int mask: SDI0=0x01, SDI1=0x02, ... SDO3=0x80 */
1958 azx_dev->sd_int_sta_mask = 1 << i;
1961 /* stream tag must be unique throughout
1962 * the stream direction group,
1963 * valid values 1...15
1964 * use separate stream tag if the flag
1965 * AZX_DCAPS_SEPARATE_STREAM_TAG is used
1967 if (chip->driver_caps & AZX_DCAPS_SEPARATE_STREAM_TAG)
1968 azx_dev->stream_tag =
1969 is_input_stream(chip, i) ?
1973 azx_dev->stream_tag = i + 1;
1978 EXPORT_SYMBOL_GPL(azx_init_stream);
1981 * reboot notifier for hang-up problem at power-down
1983 static int azx_halt(struct notifier_block *nb, unsigned long event, void *buf)
1985 struct azx *chip = container_of(nb, struct azx, reboot_notifier);
1986 snd_hda_bus_reboot_notify(chip->bus);
1987 azx_stop_chip(chip);
1991 void azx_notifier_register(struct azx *chip)
1993 chip->reboot_notifier.notifier_call = azx_halt;
1994 register_reboot_notifier(&chip->reboot_notifier);
1996 EXPORT_SYMBOL_GPL(azx_notifier_register);
1998 void azx_notifier_unregister(struct azx *chip)
2000 if (chip->reboot_notifier.notifier_call)
2001 unregister_reboot_notifier(&chip->reboot_notifier);
2003 EXPORT_SYMBOL_GPL(azx_notifier_unregister);
2005 MODULE_LICENSE("GPL");
2006 MODULE_DESCRIPTION("Common HDA driver functions");