2 * ALSA driver for ATI IXP 150/200/250/300 AC97 controllers
4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/pci.h>
28 #include <linux/slab.h>
29 #include <linux/moduleparam.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/info.h>
34 #include <sound/ac97_codec.h>
35 #include <sound/initval.h>
37 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
38 MODULE_DESCRIPTION("ATI IXP AC97 controller");
39 MODULE_LICENSE("GPL");
40 MODULE_SUPPORTED_DEVICE("{{ATI,IXP150/200/250/300/400}}");
42 static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
43 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
44 static int ac97_clock = 48000;
45 static char *ac97_quirk;
46 static int spdif_aclink = 1;
48 module_param(index, int, 0444);
49 MODULE_PARM_DESC(index, "Index value for ATI IXP controller.");
50 module_param(id, charp, 0444);
51 MODULE_PARM_DESC(id, "ID string for ATI IXP controller.");
52 module_param(ac97_clock, int, 0444);
53 MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
54 module_param(ac97_quirk, charp, 0444);
55 MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
56 module_param(spdif_aclink, bool, 0444);
57 MODULE_PARM_DESC(spdif_aclink, "S/PDIF over AC-link.");
59 /* just for backward compatibility */
61 module_param(enable, bool, 0444);
67 #define ATI_REG_ISR 0x00 /* interrupt source */
68 #define ATI_REG_ISR_IN_XRUN (1U<<0)
69 #define ATI_REG_ISR_IN_STATUS (1U<<1)
70 #define ATI_REG_ISR_OUT_XRUN (1U<<2)
71 #define ATI_REG_ISR_OUT_STATUS (1U<<3)
72 #define ATI_REG_ISR_SPDF_XRUN (1U<<4)
73 #define ATI_REG_ISR_SPDF_STATUS (1U<<5)
74 #define ATI_REG_ISR_PHYS_INTR (1U<<8)
75 #define ATI_REG_ISR_PHYS_MISMATCH (1U<<9)
76 #define ATI_REG_ISR_CODEC0_NOT_READY (1U<<10)
77 #define ATI_REG_ISR_CODEC1_NOT_READY (1U<<11)
78 #define ATI_REG_ISR_CODEC2_NOT_READY (1U<<12)
79 #define ATI_REG_ISR_NEW_FRAME (1U<<13)
81 #define ATI_REG_IER 0x04 /* interrupt enable */
82 #define ATI_REG_IER_IN_XRUN_EN (1U<<0)
83 #define ATI_REG_IER_IO_STATUS_EN (1U<<1)
84 #define ATI_REG_IER_OUT_XRUN_EN (1U<<2)
85 #define ATI_REG_IER_OUT_XRUN_COND (1U<<3)
86 #define ATI_REG_IER_SPDF_XRUN_EN (1U<<4)
87 #define ATI_REG_IER_SPDF_STATUS_EN (1U<<5)
88 #define ATI_REG_IER_PHYS_INTR_EN (1U<<8)
89 #define ATI_REG_IER_PHYS_MISMATCH_EN (1U<<9)
90 #define ATI_REG_IER_CODEC0_INTR_EN (1U<<10)
91 #define ATI_REG_IER_CODEC1_INTR_EN (1U<<11)
92 #define ATI_REG_IER_CODEC2_INTR_EN (1U<<12)
93 #define ATI_REG_IER_NEW_FRAME_EN (1U<<13) /* (RO */
94 #define ATI_REG_IER_SET_BUS_BUSY (1U<<14) /* (WO) audio is running */
96 #define ATI_REG_CMD 0x08 /* command */
97 #define ATI_REG_CMD_POWERDOWN (1U<<0)
98 #define ATI_REG_CMD_RECEIVE_EN (1U<<1)
99 #define ATI_REG_CMD_SEND_EN (1U<<2)
100 #define ATI_REG_CMD_STATUS_MEM (1U<<3)
101 #define ATI_REG_CMD_SPDF_OUT_EN (1U<<4)
102 #define ATI_REG_CMD_SPDF_STATUS_MEM (1U<<5)
103 #define ATI_REG_CMD_SPDF_THRESHOLD (3U<<6)
104 #define ATI_REG_CMD_SPDF_THRESHOLD_SHIFT 6
105 #define ATI_REG_CMD_IN_DMA_EN (1U<<8)
106 #define ATI_REG_CMD_OUT_DMA_EN (1U<<9)
107 #define ATI_REG_CMD_SPDF_DMA_EN (1U<<10)
108 #define ATI_REG_CMD_SPDF_OUT_STOPPED (1U<<11)
109 #define ATI_REG_CMD_SPDF_CONFIG_MASK (7U<<12)
110 #define ATI_REG_CMD_SPDF_CONFIG_34 (1U<<12)
111 #define ATI_REG_CMD_SPDF_CONFIG_78 (2U<<12)
112 #define ATI_REG_CMD_SPDF_CONFIG_69 (3U<<12)
113 #define ATI_REG_CMD_SPDF_CONFIG_01 (4U<<12)
114 #define ATI_REG_CMD_INTERLEAVE_SPDF (1U<<16)
115 #define ATI_REG_CMD_AUDIO_PRESENT (1U<<20)
116 #define ATI_REG_CMD_INTERLEAVE_IN (1U<<21)
117 #define ATI_REG_CMD_INTERLEAVE_OUT (1U<<22)
118 #define ATI_REG_CMD_LOOPBACK_EN (1U<<23)
119 #define ATI_REG_CMD_PACKED_DIS (1U<<24)
120 #define ATI_REG_CMD_BURST_EN (1U<<25)
121 #define ATI_REG_CMD_PANIC_EN (1U<<26)
122 #define ATI_REG_CMD_MODEM_PRESENT (1U<<27)
123 #define ATI_REG_CMD_ACLINK_ACTIVE (1U<<28)
124 #define ATI_REG_CMD_AC_SOFT_RESET (1U<<29)
125 #define ATI_REG_CMD_AC_SYNC (1U<<30)
126 #define ATI_REG_CMD_AC_RESET (1U<<31)
128 #define ATI_REG_PHYS_OUT_ADDR 0x0c
129 #define ATI_REG_PHYS_OUT_CODEC_MASK (3U<<0)
130 #define ATI_REG_PHYS_OUT_RW (1U<<2)
131 #define ATI_REG_PHYS_OUT_ADDR_EN (1U<<8)
132 #define ATI_REG_PHYS_OUT_ADDR_SHIFT 9
133 #define ATI_REG_PHYS_OUT_DATA_SHIFT 16
135 #define ATI_REG_PHYS_IN_ADDR 0x10
136 #define ATI_REG_PHYS_IN_READ_FLAG (1U<<8)
137 #define ATI_REG_PHYS_IN_ADDR_SHIFT 9
138 #define ATI_REG_PHYS_IN_DATA_SHIFT 16
140 #define ATI_REG_SLOTREQ 0x14
142 #define ATI_REG_COUNTER 0x18
143 #define ATI_REG_COUNTER_SLOT (3U<<0) /* slot # */
144 #define ATI_REG_COUNTER_BITCLOCK (31U<<8)
146 #define ATI_REG_IN_FIFO_THRESHOLD 0x1c
148 #define ATI_REG_IN_DMA_LINKPTR 0x20
149 #define ATI_REG_IN_DMA_DT_START 0x24 /* RO */
150 #define ATI_REG_IN_DMA_DT_NEXT 0x28 /* RO */
151 #define ATI_REG_IN_DMA_DT_CUR 0x2c /* RO */
152 #define ATI_REG_IN_DMA_DT_SIZE 0x30
154 #define ATI_REG_OUT_DMA_SLOT 0x34
155 #define ATI_REG_OUT_DMA_SLOT_BIT(x) (1U << ((x) - 3))
156 #define ATI_REG_OUT_DMA_SLOT_MASK 0x1ff
157 #define ATI_REG_OUT_DMA_THRESHOLD_MASK 0xf800
158 #define ATI_REG_OUT_DMA_THRESHOLD_SHIFT 11
160 #define ATI_REG_OUT_DMA_LINKPTR 0x38
161 #define ATI_REG_OUT_DMA_DT_START 0x3c /* RO */
162 #define ATI_REG_OUT_DMA_DT_NEXT 0x40 /* RO */
163 #define ATI_REG_OUT_DMA_DT_CUR 0x44 /* RO */
164 #define ATI_REG_OUT_DMA_DT_SIZE 0x48
166 #define ATI_REG_SPDF_CMD 0x4c
167 #define ATI_REG_SPDF_CMD_LFSR (1U<<4)
168 #define ATI_REG_SPDF_CMD_SINGLE_CH (1U<<5)
169 #define ATI_REG_SPDF_CMD_LFSR_ACC (0xff<<8) /* RO */
171 #define ATI_REG_SPDF_DMA_LINKPTR 0x50
172 #define ATI_REG_SPDF_DMA_DT_START 0x54 /* RO */
173 #define ATI_REG_SPDF_DMA_DT_NEXT 0x58 /* RO */
174 #define ATI_REG_SPDF_DMA_DT_CUR 0x5c /* RO */
175 #define ATI_REG_SPDF_DMA_DT_SIZE 0x60
177 #define ATI_REG_MODEM_MIRROR 0x7c
178 #define ATI_REG_AUDIO_MIRROR 0x80
180 #define ATI_REG_6CH_REORDER 0x84 /* reorder slots for 6ch */
181 #define ATI_REG_6CH_REORDER_EN (1U<<0) /* 3,4,7,8,6,9 -> 3,4,6,9,7,8 */
183 #define ATI_REG_FIFO_FLUSH 0x88
184 #define ATI_REG_FIFO_OUT_FLUSH (1U<<0)
185 #define ATI_REG_FIFO_IN_FLUSH (1U<<1)
188 #define ATI_REG_LINKPTR_EN (1U<<0)
190 /* [INT|OUT|SPDIF]_DMA_DT_SIZE */
191 #define ATI_REG_DMA_DT_SIZE (0xffffU<<0)
192 #define ATI_REG_DMA_FIFO_USED (0x1fU<<16)
193 #define ATI_REG_DMA_FIFO_FREE (0x1fU<<21)
194 #define ATI_REG_DMA_STATE (7U<<26)
197 #define ATI_MAX_DESCRIPTORS 256 /* max number of descriptor packets */
203 typedef struct snd_atiixp atiixp_t;
204 typedef struct snd_atiixp_dma atiixp_dma_t;
205 typedef struct snd_atiixp_dma_ops atiixp_dma_ops_t;
209 * DMA packate descriptor
212 typedef struct atiixp_dma_desc {
213 u32 addr; /* DMA buffer address */
214 u16 status; /* status bits */
215 u16 size; /* size of the packet in dwords */
216 u32 next; /* address of the next packet descriptor */
222 enum { ATI_DMA_PLAYBACK, ATI_DMA_CAPTURE, ATI_DMA_SPDIF, NUM_ATI_DMAS }; /* DMAs */
223 enum { ATI_PCM_OUT, ATI_PCM_IN, ATI_PCM_SPDIF, NUM_ATI_PCMS }; /* AC97 pcm slots */
224 enum { ATI_PCMDEV_ANALOG, ATI_PCMDEV_DIGITAL, NUM_ATI_PCMDEVS }; /* pcm devices */
226 #define NUM_ATI_CODECS 3
230 * constants and callbacks for each DMA type
232 struct snd_atiixp_dma_ops {
233 int type; /* ATI_DMA_XXX */
234 unsigned int llp_offset; /* LINKPTR offset */
235 unsigned int dt_cur; /* DT_CUR offset */
236 void (*enable_dma)(atiixp_t *chip, int on); /* called from open callback */
237 void (*enable_transfer)(atiixp_t *chip, int on); /* called from trigger (START/STOP) */
238 void (*flush_dma)(atiixp_t *chip); /* called from trigger (STOP only) */
244 struct snd_atiixp_dma {
245 const atiixp_dma_ops_t *ops;
246 struct snd_dma_buffer desc_buf;
247 snd_pcm_substream_t *substream; /* assigned PCM substream */
248 unsigned int buf_addr, buf_bytes; /* DMA buffer address, bytes */
249 unsigned int period_bytes, periods;
254 int ac97_pcm_type; /* index # of ac97_pcm to access, -1 = not used */
255 unsigned int saved_curptr;
266 void __iomem *remap_addr;
269 ac97_bus_t *ac97_bus;
270 ac97_t *ac97[NUM_ATI_CODECS];
274 atiixp_dma_t dmas[NUM_ATI_DMAS];
275 struct ac97_pcm *pcms[NUM_ATI_PCMS];
276 snd_pcm_t *pcmdevs[NUM_ATI_PCMDEVS];
278 int max_channels; /* max. channels for PCM out */
280 unsigned int codec_not_ready_bits; /* for codec detection */
282 int spdif_over_aclink; /* passed from the module option */
283 struct semaphore open_mutex; /* playback open mutex */
289 static struct pci_device_id snd_atiixp_ids[] = {
290 { 0x1002, 0x4341, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB200 */
291 { 0x1002, 0x4361, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB300 */
292 { 0x1002, 0x4370, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* SB400 */
296 MODULE_DEVICE_TABLE(pci, snd_atiixp_ids);
304 * update the bits of the given register.
305 * return 1 if the bits changed.
307 static int snd_atiixp_update_bits(atiixp_t *chip, unsigned int reg,
308 unsigned int mask, unsigned int value)
310 void __iomem *addr = chip->remap_addr + reg;
311 unsigned int data, old_data;
312 old_data = data = readl(addr);
315 if (old_data == data)
322 * macros for easy use
324 #define atiixp_write(chip,reg,value) \
325 writel(value, chip->remap_addr + ATI_REG_##reg)
326 #define atiixp_read(chip,reg) \
327 readl(chip->remap_addr + ATI_REG_##reg)
328 #define atiixp_update(chip,reg,mask,val) \
329 snd_atiixp_update_bits(chip, ATI_REG_##reg, mask, val)
331 /* delay for one tick */
332 #define do_delay() do { \
333 set_current_state(TASK_UNINTERRUPTIBLE); \
334 schedule_timeout(1); \
339 * handling DMA packets
341 * we allocate a linear buffer for the DMA, and split it to each packet.
342 * in a future version, a scatter-gather buffer should be implemented.
345 #define ATI_DESC_LIST_SIZE \
346 PAGE_ALIGN(ATI_MAX_DESCRIPTORS * sizeof(atiixp_dma_desc_t))
349 * build packets ring for the given buffer size.
351 * IXP handles the buffer descriptors, which are connected as a linked
352 * list. although we can change the list dynamically, in this version,
353 * a static RING of buffer descriptors is used.
355 * the ring is built in this function, and is set up to the hardware.
357 static int atiixp_build_dma_packets(atiixp_t *chip, atiixp_dma_t *dma,
358 snd_pcm_substream_t *substream,
359 unsigned int periods,
360 unsigned int period_bytes)
366 if (periods > ATI_MAX_DESCRIPTORS)
369 if (dma->desc_buf.area == NULL) {
370 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
371 ATI_DESC_LIST_SIZE, &dma->desc_buf) < 0)
373 dma->period_bytes = dma->periods = 0; /* clear */
376 if (dma->periods == periods && dma->period_bytes == period_bytes)
379 /* reset DMA before changing the descriptor table */
380 spin_lock_irqsave(&chip->reg_lock, flags);
381 writel(0, chip->remap_addr + dma->ops->llp_offset);
382 dma->ops->enable_dma(chip, 0);
383 dma->ops->enable_dma(chip, 1);
384 spin_unlock_irqrestore(&chip->reg_lock, flags);
386 /* fill the entries */
387 addr = (u32)substream->runtime->dma_addr;
388 desc_addr = (u32)dma->desc_buf.addr;
389 for (i = 0; i < periods; i++) {
390 atiixp_dma_desc_t *desc = &((atiixp_dma_desc_t *)dma->desc_buf.area)[i];
391 desc->addr = cpu_to_le32(addr);
393 desc->size = period_bytes >> 2; /* in dwords */
394 desc_addr += sizeof(atiixp_dma_desc_t);
395 if (i == periods - 1)
396 desc->next = cpu_to_le32((u32)dma->desc_buf.addr);
398 desc->next = cpu_to_le32(desc_addr);
399 addr += period_bytes;
402 writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
403 chip->remap_addr + dma->ops->llp_offset);
405 dma->period_bytes = period_bytes;
406 dma->periods = periods;
412 * remove the ring buffer and release it if assigned
414 static void atiixp_clear_dma_packets(atiixp_t *chip, atiixp_dma_t *dma, snd_pcm_substream_t *substream)
416 if (dma->desc_buf.area) {
417 writel(0, chip->remap_addr + dma->ops->llp_offset);
418 snd_dma_free_pages(&dma->desc_buf);
419 dma->desc_buf.area = NULL;
426 static int snd_atiixp_acquire_codec(atiixp_t *chip)
430 while (atiixp_read(chip, PHYS_OUT_ADDR) & ATI_REG_PHYS_OUT_ADDR_EN) {
432 snd_printk(KERN_WARNING "atiixp: codec acquire timeout\n");
440 static unsigned short snd_atiixp_codec_read(atiixp_t *chip, unsigned short codec, unsigned short reg)
445 if (snd_atiixp_acquire_codec(chip) < 0)
447 data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
448 ATI_REG_PHYS_OUT_ADDR_EN |
449 ATI_REG_PHYS_OUT_RW |
451 atiixp_write(chip, PHYS_OUT_ADDR, data);
452 if (snd_atiixp_acquire_codec(chip) < 0)
456 data = atiixp_read(chip, PHYS_IN_ADDR);
457 if (data & ATI_REG_PHYS_IN_READ_FLAG)
458 return data >> ATI_REG_PHYS_IN_DATA_SHIFT;
461 /* time out may happen during reset */
463 snd_printk(KERN_WARNING "atiixp: codec read timeout (reg %x)\n", reg);
468 static void snd_atiixp_codec_write(atiixp_t *chip, unsigned short codec, unsigned short reg, unsigned short val)
472 if (snd_atiixp_acquire_codec(chip) < 0)
474 data = ((unsigned int)val << ATI_REG_PHYS_OUT_DATA_SHIFT) |
475 ((unsigned int)reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
476 ATI_REG_PHYS_OUT_ADDR_EN | codec;
477 atiixp_write(chip, PHYS_OUT_ADDR, data);
481 static unsigned short snd_atiixp_ac97_read(ac97_t *ac97, unsigned short reg)
483 atiixp_t *chip = ac97->private_data;
484 return snd_atiixp_codec_read(chip, ac97->num, reg);
488 static void snd_atiixp_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short val)
490 atiixp_t *chip = ac97->private_data;
491 snd_atiixp_codec_write(chip, ac97->num, reg, val);
497 static int snd_atiixp_aclink_reset(atiixp_t *chip)
501 /* reset powerdoewn */
502 if (atiixp_update(chip, CMD, ATI_REG_CMD_POWERDOWN, 0))
505 /* perform a software reset */
506 atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, ATI_REG_CMD_AC_SOFT_RESET);
507 atiixp_read(chip, CMD);
509 atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, 0);
512 while (! (atiixp_read(chip, CMD) & ATI_REG_CMD_ACLINK_ACTIVE)) {
513 /* do a hard reset */
514 atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
515 ATI_REG_CMD_AC_SYNC);
516 atiixp_read(chip, CMD);
518 atiixp_update(chip, CMD, ATI_REG_CMD_AC_RESET, ATI_REG_CMD_AC_RESET);
520 snd_printk(KERN_ERR "atiixp: codec reset timeout\n");
525 /* deassert RESET and assert SYNC to make sure */
526 atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
527 ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET);
533 static int snd_atiixp_aclink_down(atiixp_t *chip)
535 // if (atiixp_read(chip, MODEM_MIRROR) & 0x1) /* modem running, too? */
537 atiixp_update(chip, CMD,
538 ATI_REG_CMD_POWERDOWN | ATI_REG_CMD_AC_RESET,
539 ATI_REG_CMD_POWERDOWN);
545 * auto-detection of codecs
547 * the IXP chip can generate interrupts for the non-existing codecs.
548 * NEW_FRAME interrupt is used to make sure that the interrupt is generated
549 * even if all three codecs are connected.
552 #define ALL_CODEC_NOT_READY \
553 (ATI_REG_ISR_CODEC0_NOT_READY |\
554 ATI_REG_ISR_CODEC1_NOT_READY |\
555 ATI_REG_ISR_CODEC2_NOT_READY)
556 #define CODEC_CHECK_BITS (ALL_CODEC_NOT_READY|ATI_REG_ISR_NEW_FRAME)
558 static int snd_atiixp_codec_detect(atiixp_t *chip)
562 chip->codec_not_ready_bits = 0;
563 atiixp_write(chip, IER, CODEC_CHECK_BITS);
564 /* wait for the interrupts */
566 while (timeout-- > 0) {
568 if (chip->codec_not_ready_bits)
571 atiixp_write(chip, IER, 0); /* disable irqs */
573 if ((chip->codec_not_ready_bits & ALL_CODEC_NOT_READY) == ALL_CODEC_NOT_READY) {
574 snd_printk(KERN_ERR "atiixp: no codec detected!\n");
582 * enable DMA and irqs
584 static int snd_atiixp_chip_start(atiixp_t *chip)
588 /* set up spdif, enable burst mode */
589 reg = atiixp_read(chip, CMD);
590 reg |= 0x02 << ATI_REG_CMD_SPDF_THRESHOLD_SHIFT;
591 reg |= ATI_REG_CMD_BURST_EN;
592 atiixp_write(chip, CMD, reg);
594 reg = atiixp_read(chip, SPDF_CMD);
595 reg &= ~(ATI_REG_SPDF_CMD_LFSR|ATI_REG_SPDF_CMD_SINGLE_CH);
596 atiixp_write(chip, SPDF_CMD, reg);
598 /* clear all interrupt source */
599 atiixp_write(chip, ISR, 0xffffffff);
601 atiixp_write(chip, IER,
602 ATI_REG_IER_IO_STATUS_EN |
603 ATI_REG_IER_IN_XRUN_EN |
604 ATI_REG_IER_OUT_XRUN_EN |
605 ATI_REG_IER_SPDF_XRUN_EN |
606 ATI_REG_IER_SPDF_STATUS_EN);
612 * disable DMA and IRQs
614 static int snd_atiixp_chip_stop(atiixp_t *chip)
616 /* clear interrupt source */
617 atiixp_write(chip, ISR, atiixp_read(chip, ISR));
619 atiixp_write(chip, IER, 0);
629 * pointer callback simplly reads XXX_DMA_DT_CUR register as the current
630 * position. when SG-buffer is implemented, the offset must be calculated
633 static snd_pcm_uframes_t snd_atiixp_pcm_pointer(snd_pcm_substream_t *substream)
635 atiixp_t *chip = snd_pcm_substream_chip(substream);
636 snd_pcm_runtime_t *runtime = substream->runtime;
637 atiixp_dma_t *dma = (atiixp_dma_t *)runtime->private_data;
642 curptr = readl(chip->remap_addr + dma->ops->dt_cur);
643 if (curptr < dma->buf_addr)
645 curptr -= dma->buf_addr;
646 if (curptr >= dma->buf_bytes)
648 return bytes_to_frames(runtime, curptr);
650 snd_printd("atiixp: invalid DMA pointer read 0x%x (buf=%x)\n",
651 readl(chip->remap_addr + dma->ops->dt_cur), dma->buf_addr);
656 * XRUN detected, and stop the PCM substream
658 static void snd_atiixp_xrun_dma(atiixp_t *chip, atiixp_dma_t *dma)
660 if (! dma->substream || ! dma->running)
662 snd_printdd("atiixp: XRUN detected (DMA %d)\n", dma->ops->type);
663 snd_pcm_stop(dma->substream, SNDRV_PCM_STATE_XRUN);
667 * the period ack. update the substream.
669 static void snd_atiixp_update_dma(atiixp_t *chip, atiixp_dma_t *dma)
671 if (! dma->substream || ! dma->running)
673 snd_pcm_period_elapsed(dma->substream);
676 /* set BUS_BUSY interrupt bit if any DMA is running */
677 /* call with spinlock held */
678 static void snd_atiixp_check_bus_busy(atiixp_t *chip)
680 unsigned int bus_busy;
681 if (atiixp_read(chip, CMD) & (ATI_REG_CMD_SEND_EN |
682 ATI_REG_CMD_RECEIVE_EN |
683 ATI_REG_CMD_SPDF_OUT_EN))
684 bus_busy = ATI_REG_IER_SET_BUS_BUSY;
687 atiixp_update(chip, IER, ATI_REG_IER_SET_BUS_BUSY, bus_busy);
690 /* common trigger callback
691 * calling the lowlevel callbacks in it
693 static int snd_atiixp_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
695 atiixp_t *chip = snd_pcm_substream_chip(substream);
696 atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
699 snd_assert(dma->ops->enable_transfer && dma->ops->flush_dma, return -EINVAL);
701 spin_lock(&chip->reg_lock);
703 case SNDRV_PCM_TRIGGER_START:
704 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
705 case SNDRV_PCM_TRIGGER_RESUME:
706 dma->ops->enable_transfer(chip, 1);
710 case SNDRV_PCM_TRIGGER_STOP:
711 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
712 case SNDRV_PCM_TRIGGER_SUSPEND:
713 dma->ops->enable_transfer(chip, 0);
715 dma->suspended = cmd == SNDRV_PCM_TRIGGER_SUSPEND;
722 snd_atiixp_check_bus_busy(chip);
723 if (cmd == SNDRV_PCM_TRIGGER_STOP) {
724 dma->ops->flush_dma(chip);
725 snd_atiixp_check_bus_busy(chip);
728 spin_unlock(&chip->reg_lock);
734 * lowlevel callbacks for each DMA type
736 * every callback is supposed to be called in chip->reg_lock spinlock
739 /* flush FIFO of analog OUT DMA */
740 static void atiixp_out_flush_dma(atiixp_t *chip)
742 atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_OUT_FLUSH);
745 /* enable/disable analog OUT DMA */
746 static void atiixp_out_enable_dma(atiixp_t *chip, int on)
749 data = atiixp_read(chip, CMD);
751 if (data & ATI_REG_CMD_OUT_DMA_EN)
753 atiixp_out_flush_dma(chip);
754 data |= ATI_REG_CMD_OUT_DMA_EN;
756 data &= ~ATI_REG_CMD_OUT_DMA_EN;
757 atiixp_write(chip, CMD, data);
760 /* start/stop transfer over OUT DMA */
761 static void atiixp_out_enable_transfer(atiixp_t *chip, int on)
763 atiixp_update(chip, CMD, ATI_REG_CMD_SEND_EN,
764 on ? ATI_REG_CMD_SEND_EN : 0);
767 /* enable/disable analog IN DMA */
768 static void atiixp_in_enable_dma(atiixp_t *chip, int on)
770 atiixp_update(chip, CMD, ATI_REG_CMD_IN_DMA_EN,
771 on ? ATI_REG_CMD_IN_DMA_EN : 0);
774 /* start/stop analog IN DMA */
775 static void atiixp_in_enable_transfer(atiixp_t *chip, int on)
778 unsigned int data = atiixp_read(chip, CMD);
779 if (! (data & ATI_REG_CMD_RECEIVE_EN)) {
780 data |= ATI_REG_CMD_RECEIVE_EN;
781 #if 0 /* FIXME: this causes the endless loop */
782 /* wait until slot 3/4 are finished */
783 while ((atiixp_read(chip, COUNTER) &
784 ATI_REG_COUNTER_SLOT) != 5)
787 atiixp_write(chip, CMD, data);
790 atiixp_update(chip, CMD, ATI_REG_CMD_RECEIVE_EN, 0);
793 /* flush FIFO of analog IN DMA */
794 static void atiixp_in_flush_dma(atiixp_t *chip)
796 atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_IN_FLUSH);
799 /* enable/disable SPDIF OUT DMA */
800 static void atiixp_spdif_enable_dma(atiixp_t *chip, int on)
802 atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_DMA_EN,
803 on ? ATI_REG_CMD_SPDF_DMA_EN : 0);
806 /* start/stop SPDIF OUT DMA */
807 static void atiixp_spdif_enable_transfer(atiixp_t *chip, int on)
810 data = atiixp_read(chip, CMD);
812 data |= ATI_REG_CMD_SPDF_OUT_EN;
814 data &= ~ATI_REG_CMD_SPDF_OUT_EN;
815 atiixp_write(chip, CMD, data);
818 /* flush FIFO of SPDIF OUT DMA */
819 static void atiixp_spdif_flush_dma(atiixp_t *chip)
823 /* DMA off, transfer on */
824 atiixp_spdif_enable_dma(chip, 0);
825 atiixp_spdif_enable_transfer(chip, 1);
829 if (! (atiixp_read(chip, SPDF_DMA_DT_SIZE) & ATI_REG_DMA_FIFO_USED))
832 } while (timeout-- > 0);
834 atiixp_spdif_enable_transfer(chip, 0);
837 /* set up slots and formats for SPDIF OUT */
838 static int snd_atiixp_spdif_prepare(snd_pcm_substream_t *substream)
840 atiixp_t *chip = snd_pcm_substream_chip(substream);
842 spin_lock_irq(&chip->reg_lock);
843 if (chip->spdif_over_aclink) {
845 /* enable slots 10/11 */
846 atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK,
847 ATI_REG_CMD_SPDF_CONFIG_01);
848 data = atiixp_read(chip, OUT_DMA_SLOT) & ~ATI_REG_OUT_DMA_SLOT_MASK;
849 data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
850 ATI_REG_OUT_DMA_SLOT_BIT(11);
851 data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
852 atiixp_write(chip, OUT_DMA_SLOT, data);
853 atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_OUT,
854 substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
855 ATI_REG_CMD_INTERLEAVE_OUT : 0);
857 atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK, 0);
858 atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_SPDF, 0);
860 spin_unlock_irq(&chip->reg_lock);
864 /* set up slots and formats for analog OUT */
865 static int snd_atiixp_playback_prepare(snd_pcm_substream_t *substream)
867 atiixp_t *chip = snd_pcm_substream_chip(substream);
870 spin_lock_irq(&chip->reg_lock);
871 data = atiixp_read(chip, OUT_DMA_SLOT) & ~ATI_REG_OUT_DMA_SLOT_MASK;
872 switch (substream->runtime->channels) {
874 data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
875 ATI_REG_OUT_DMA_SLOT_BIT(11);
878 data |= ATI_REG_OUT_DMA_SLOT_BIT(7) |
879 ATI_REG_OUT_DMA_SLOT_BIT(8);
882 data |= ATI_REG_OUT_DMA_SLOT_BIT(6) |
883 ATI_REG_OUT_DMA_SLOT_BIT(9);
886 data |= ATI_REG_OUT_DMA_SLOT_BIT(3) |
887 ATI_REG_OUT_DMA_SLOT_BIT(4);
891 /* set output threshold */
892 data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
893 atiixp_write(chip, OUT_DMA_SLOT, data);
895 atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_OUT,
896 substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
897 ATI_REG_CMD_INTERLEAVE_OUT : 0);
900 * enable 6 channel re-ordering bit if needed
902 atiixp_update(chip, 6CH_REORDER, ATI_REG_6CH_REORDER_EN,
903 substream->runtime->channels >= 6 ? ATI_REG_6CH_REORDER_EN: 0);
905 spin_unlock_irq(&chip->reg_lock);
909 /* set up slots and formats for analog IN */
910 static int snd_atiixp_capture_prepare(snd_pcm_substream_t *substream)
912 atiixp_t *chip = snd_pcm_substream_chip(substream);
914 spin_lock_irq(&chip->reg_lock);
915 atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_IN,
916 substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
917 ATI_REG_CMD_INTERLEAVE_IN : 0);
918 spin_unlock_irq(&chip->reg_lock);
923 * hw_params - allocate the buffer and set up buffer descriptors
925 static int snd_atiixp_pcm_hw_params(snd_pcm_substream_t *substream,
926 snd_pcm_hw_params_t *hw_params)
928 atiixp_t *chip = snd_pcm_substream_chip(substream);
929 atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
932 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
935 dma->buf_addr = substream->runtime->dma_addr;
936 dma->buf_bytes = params_buffer_bytes(hw_params);
938 err = atiixp_build_dma_packets(chip, dma, substream,
939 params_periods(hw_params),
940 params_period_bytes(hw_params));
944 if (dma->ac97_pcm_type >= 0) {
945 struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
946 /* PCM is bound to AC97 codec(s)
947 * set up the AC97 codecs
949 if (dma->pcm_open_flag) {
950 snd_ac97_pcm_close(pcm);
951 dma->pcm_open_flag = 0;
953 err = snd_ac97_pcm_open(pcm, params_rate(hw_params),
954 params_channels(hw_params),
957 dma->pcm_open_flag = 1;
963 static int snd_atiixp_pcm_hw_free(snd_pcm_substream_t * substream)
965 atiixp_t *chip = snd_pcm_substream_chip(substream);
966 atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
968 if (dma->pcm_open_flag) {
969 struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
970 snd_ac97_pcm_close(pcm);
971 dma->pcm_open_flag = 0;
973 atiixp_clear_dma_packets(chip, dma, substream);
974 snd_pcm_lib_free_pages(substream);
980 * pcm hardware definition, identical for all DMA types
982 static snd_pcm_hardware_t snd_atiixp_pcm_hw =
984 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
985 SNDRV_PCM_INFO_BLOCK_TRANSFER |
986 SNDRV_PCM_INFO_PAUSE |
987 SNDRV_PCM_INFO_RESUME |
988 SNDRV_PCM_INFO_MMAP_VALID),
989 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
990 .rates = SNDRV_PCM_RATE_48000,
995 .buffer_bytes_max = 256 * 1024,
996 .period_bytes_min = 32,
997 .period_bytes_max = 128 * 1024,
999 .periods_max = ATI_MAX_DESCRIPTORS,
1002 static int snd_atiixp_pcm_open(snd_pcm_substream_t *substream, atiixp_dma_t *dma, int pcm_type)
1004 atiixp_t *chip = snd_pcm_substream_chip(substream);
1005 snd_pcm_runtime_t *runtime = substream->runtime;
1008 snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
1012 dma->substream = substream;
1013 runtime->hw = snd_atiixp_pcm_hw;
1014 dma->ac97_pcm_type = pcm_type;
1015 if (pcm_type >= 0) {
1016 runtime->hw.rates = chip->pcms[pcm_type]->rates;
1017 snd_pcm_limit_hw_rates(runtime);
1020 runtime->hw.formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1022 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1024 runtime->private_data = dma;
1026 /* enable DMA bits */
1027 spin_lock_irq(&chip->reg_lock);
1028 dma->ops->enable_dma(chip, 1);
1029 spin_unlock_irq(&chip->reg_lock);
1035 static int snd_atiixp_pcm_close(snd_pcm_substream_t *substream, atiixp_dma_t *dma)
1037 atiixp_t *chip = snd_pcm_substream_chip(substream);
1038 /* disable DMA bits */
1039 snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
1040 spin_lock_irq(&chip->reg_lock);
1041 dma->ops->enable_dma(chip, 0);
1042 spin_unlock_irq(&chip->reg_lock);
1043 dma->substream = NULL;
1050 static int snd_atiixp_playback_open(snd_pcm_substream_t *substream)
1052 atiixp_t *chip = snd_pcm_substream_chip(substream);
1055 down(&chip->open_mutex);
1056 err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 0);
1057 up(&chip->open_mutex);
1060 substream->runtime->hw.channels_max = chip->max_channels;
1061 if (chip->max_channels > 2)
1062 /* channels must be even */
1063 snd_pcm_hw_constraint_step(substream->runtime, 0,
1064 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1068 static int snd_atiixp_playback_close(snd_pcm_substream_t *substream)
1070 atiixp_t *chip = snd_pcm_substream_chip(substream);
1072 down(&chip->open_mutex);
1073 err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
1074 up(&chip->open_mutex);
1078 static int snd_atiixp_capture_open(snd_pcm_substream_t *substream)
1080 atiixp_t *chip = snd_pcm_substream_chip(substream);
1081 return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE], 1);
1084 static int snd_atiixp_capture_close(snd_pcm_substream_t *substream)
1086 atiixp_t *chip = snd_pcm_substream_chip(substream);
1087 return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_CAPTURE]);
1090 static int snd_atiixp_spdif_open(snd_pcm_substream_t *substream)
1092 atiixp_t *chip = snd_pcm_substream_chip(substream);
1094 down(&chip->open_mutex);
1095 if (chip->spdif_over_aclink) /* share DMA_PLAYBACK */
1096 err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 2);
1098 err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_SPDIF], -1);
1099 up(&chip->open_mutex);
1103 static int snd_atiixp_spdif_close(snd_pcm_substream_t *substream)
1105 atiixp_t *chip = snd_pcm_substream_chip(substream);
1107 down(&chip->open_mutex);
1108 if (chip->spdif_over_aclink)
1109 err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
1111 err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_SPDIF]);
1112 up(&chip->open_mutex);
1117 static snd_pcm_ops_t snd_atiixp_playback_ops = {
1118 .open = snd_atiixp_playback_open,
1119 .close = snd_atiixp_playback_close,
1120 .ioctl = snd_pcm_lib_ioctl,
1121 .hw_params = snd_atiixp_pcm_hw_params,
1122 .hw_free = snd_atiixp_pcm_hw_free,
1123 .prepare = snd_atiixp_playback_prepare,
1124 .trigger = snd_atiixp_pcm_trigger,
1125 .pointer = snd_atiixp_pcm_pointer,
1129 static snd_pcm_ops_t snd_atiixp_capture_ops = {
1130 .open = snd_atiixp_capture_open,
1131 .close = snd_atiixp_capture_close,
1132 .ioctl = snd_pcm_lib_ioctl,
1133 .hw_params = snd_atiixp_pcm_hw_params,
1134 .hw_free = snd_atiixp_pcm_hw_free,
1135 .prepare = snd_atiixp_capture_prepare,
1136 .trigger = snd_atiixp_pcm_trigger,
1137 .pointer = snd_atiixp_pcm_pointer,
1140 /* SPDIF playback */
1141 static snd_pcm_ops_t snd_atiixp_spdif_ops = {
1142 .open = snd_atiixp_spdif_open,
1143 .close = snd_atiixp_spdif_close,
1144 .ioctl = snd_pcm_lib_ioctl,
1145 .hw_params = snd_atiixp_pcm_hw_params,
1146 .hw_free = snd_atiixp_pcm_hw_free,
1147 .prepare = snd_atiixp_spdif_prepare,
1148 .trigger = snd_atiixp_pcm_trigger,
1149 .pointer = snd_atiixp_pcm_pointer,
1152 static struct ac97_pcm atiixp_pcm_defs[] __devinitdata = {
1157 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1158 (1 << AC97_SLOT_PCM_RIGHT) |
1159 (1 << AC97_SLOT_PCM_CENTER) |
1160 (1 << AC97_SLOT_PCM_SLEFT) |
1161 (1 << AC97_SLOT_PCM_SRIGHT) |
1162 (1 << AC97_SLOT_LFE)
1171 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1172 (1 << AC97_SLOT_PCM_RIGHT)
1176 /* S/PDIF OUT (optional) */
1181 .slots = (1 << AC97_SLOT_SPDIF_LEFT2) |
1182 (1 << AC97_SLOT_SPDIF_RIGHT2)
1188 static atiixp_dma_ops_t snd_atiixp_playback_dma_ops = {
1189 .type = ATI_DMA_PLAYBACK,
1190 .llp_offset = ATI_REG_OUT_DMA_LINKPTR,
1191 .dt_cur = ATI_REG_OUT_DMA_DT_CUR,
1192 .enable_dma = atiixp_out_enable_dma,
1193 .enable_transfer = atiixp_out_enable_transfer,
1194 .flush_dma = atiixp_out_flush_dma,
1197 static atiixp_dma_ops_t snd_atiixp_capture_dma_ops = {
1198 .type = ATI_DMA_CAPTURE,
1199 .llp_offset = ATI_REG_IN_DMA_LINKPTR,
1200 .dt_cur = ATI_REG_IN_DMA_DT_CUR,
1201 .enable_dma = atiixp_in_enable_dma,
1202 .enable_transfer = atiixp_in_enable_transfer,
1203 .flush_dma = atiixp_in_flush_dma,
1206 static atiixp_dma_ops_t snd_atiixp_spdif_dma_ops = {
1207 .type = ATI_DMA_SPDIF,
1208 .llp_offset = ATI_REG_SPDF_DMA_LINKPTR,
1209 .dt_cur = ATI_REG_SPDF_DMA_DT_CUR,
1210 .enable_dma = atiixp_spdif_enable_dma,
1211 .enable_transfer = atiixp_spdif_enable_transfer,
1212 .flush_dma = atiixp_spdif_flush_dma,
1216 static int __devinit snd_atiixp_pcm_new(atiixp_t *chip)
1219 ac97_bus_t *pbus = chip->ac97_bus;
1220 int err, i, num_pcms;
1222 /* initialize constants */
1223 chip->dmas[ATI_DMA_PLAYBACK].ops = &snd_atiixp_playback_dma_ops;
1224 chip->dmas[ATI_DMA_CAPTURE].ops = &snd_atiixp_capture_dma_ops;
1225 if (! chip->spdif_over_aclink)
1226 chip->dmas[ATI_DMA_SPDIF].ops = &snd_atiixp_spdif_dma_ops;
1228 /* assign AC97 pcm */
1229 if (chip->spdif_over_aclink)
1233 err = snd_ac97_pcm_assign(pbus, num_pcms, atiixp_pcm_defs);
1236 for (i = 0; i < num_pcms; i++)
1237 chip->pcms[i] = &pbus->pcms[i];
1239 chip->max_channels = 2;
1240 if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_PCM_SLEFT)) {
1241 if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_LFE))
1242 chip->max_channels = 6;
1244 chip->max_channels = 4;
1247 /* PCM #0: analog I/O */
1248 err = snd_pcm_new(chip->card, "ATI IXP AC97", ATI_PCMDEV_ANALOG, 1, 1, &pcm);
1251 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_playback_ops);
1252 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_atiixp_capture_ops);
1253 pcm->private_data = chip;
1254 strcpy(pcm->name, "ATI IXP AC97");
1255 chip->pcmdevs[ATI_PCMDEV_ANALOG] = pcm;
1257 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1258 snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
1260 /* no SPDIF support on codec? */
1261 if (chip->pcms[ATI_PCM_SPDIF] && ! chip->pcms[ATI_PCM_SPDIF]->rates)
1264 /* FIXME: non-48k sample rate doesn't work on my test machine with AD1888 */
1265 if (chip->pcms[ATI_PCM_SPDIF])
1266 chip->pcms[ATI_PCM_SPDIF]->rates = SNDRV_PCM_RATE_48000;
1268 /* PCM #1: spdif playback */
1269 err = snd_pcm_new(chip->card, "ATI IXP IEC958", ATI_PCMDEV_DIGITAL, 1, 0, &pcm);
1272 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_spdif_ops);
1273 pcm->private_data = chip;
1274 if (chip->spdif_over_aclink)
1275 strcpy(pcm->name, "ATI IXP IEC958 (AC97)");
1277 strcpy(pcm->name, "ATI IXP IEC958 (Direct)");
1278 chip->pcmdevs[ATI_PCMDEV_DIGITAL] = pcm;
1280 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1281 snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
1283 /* pre-select AC97 SPDIF slots 10/11 */
1284 for (i = 0; i < NUM_ATI_CODECS; i++) {
1286 snd_ac97_update_bits(chip->ac97[i], AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1297 static irqreturn_t snd_atiixp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1299 atiixp_t *chip = dev_id;
1300 unsigned int status;
1302 status = atiixp_read(chip, ISR);
1307 /* process audio DMA */
1308 if (status & ATI_REG_ISR_OUT_XRUN)
1309 snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
1310 else if (status & ATI_REG_ISR_OUT_STATUS)
1311 snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
1312 if (status & ATI_REG_ISR_IN_XRUN)
1313 snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
1314 else if (status & ATI_REG_ISR_IN_STATUS)
1315 snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
1316 if (! chip->spdif_over_aclink) {
1317 if (status & ATI_REG_ISR_SPDF_XRUN)
1318 snd_atiixp_xrun_dma(chip, &chip->dmas[ATI_DMA_SPDIF]);
1319 else if (status & ATI_REG_ISR_SPDF_STATUS)
1320 snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_SPDIF]);
1323 /* for codec detection */
1324 if (status & CODEC_CHECK_BITS) {
1325 unsigned int detected;
1326 detected = status & CODEC_CHECK_BITS;
1327 spin_lock(&chip->reg_lock);
1328 chip->codec_not_ready_bits |= detected;
1329 atiixp_update(chip, IER, detected, 0); /* disable the detected irqs */
1330 spin_unlock(&chip->reg_lock);
1334 atiixp_write(chip, ISR, status);
1341 * ac97 mixer section
1344 static struct ac97_quirk ac97_quirks[] __devinitdata = {
1346 .subvendor = 0x103c,
1347 .subdevice = 0x006b,
1348 .name = "HP Pavilion ZV5030US",
1349 .type = AC97_TUNE_MUTE_LED
1351 { } /* terminator */
1354 static int __devinit snd_atiixp_mixer_new(atiixp_t *chip, int clock, const char *quirk_override)
1357 ac97_template_t ac97;
1360 static ac97_bus_ops_t ops = {
1361 .write = snd_atiixp_ac97_write,
1362 .read = snd_atiixp_ac97_read,
1364 static unsigned int codec_skip[NUM_ATI_CODECS] = {
1365 ATI_REG_ISR_CODEC0_NOT_READY,
1366 ATI_REG_ISR_CODEC1_NOT_READY,
1367 ATI_REG_ISR_CODEC2_NOT_READY,
1370 if (snd_atiixp_codec_detect(chip) < 0)
1373 if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
1375 pbus->clock = clock;
1376 chip->ac97_bus = pbus;
1379 for (i = 0; i < NUM_ATI_CODECS; i++) {
1380 if (chip->codec_not_ready_bits & codec_skip[i])
1382 memset(&ac97, 0, sizeof(ac97));
1383 ac97.private_data = chip;
1384 ac97.pci = chip->pci;
1386 ac97.scaps = AC97_SCAP_SKIP_MODEM;
1387 if (! chip->spdif_over_aclink)
1388 ac97.scaps |= AC97_SCAP_NO_SPDIF;
1389 if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
1390 chip->ac97[i] = NULL; /* to be sure */
1391 snd_printdd("atiixp: codec %d not available for audio\n", i);
1397 if (! codec_count) {
1398 snd_printk(KERN_ERR "atiixp: no codec available\n");
1402 snd_ac97_tune_hardware(chip->ac97[0], ac97_quirks, quirk_override);
1412 static int snd_atiixp_suspend(snd_card_t *card, pm_message_t state)
1414 atiixp_t *chip = card->pm_private_data;
1417 for (i = 0; i < NUM_ATI_PCMDEVS; i++)
1418 if (chip->pcmdevs[i]) {
1419 atiixp_dma_t *dma = &chip->dmas[i];
1420 if (dma->substream && dma->running)
1421 dma->saved_curptr = readl(chip->remap_addr + dma->ops->dt_cur);
1422 snd_pcm_suspend_all(chip->pcmdevs[i]);
1424 for (i = 0; i < NUM_ATI_CODECS; i++)
1426 snd_ac97_suspend(chip->ac97[i]);
1427 snd_atiixp_aclink_down(chip);
1428 snd_atiixp_chip_stop(chip);
1430 pci_set_power_state(chip->pci, PCI_D3hot);
1431 pci_disable_device(chip->pci);
1435 static int snd_atiixp_resume(snd_card_t *card)
1437 atiixp_t *chip = card->pm_private_data;
1440 pci_enable_device(chip->pci);
1441 pci_set_power_state(chip->pci, PCI_D0);
1442 pci_set_master(chip->pci);
1444 snd_atiixp_aclink_reset(chip);
1445 snd_atiixp_chip_start(chip);
1447 for (i = 0; i < NUM_ATI_CODECS; i++)
1449 snd_ac97_resume(chip->ac97[i]);
1451 for (i = 0; i < NUM_ATI_PCMDEVS; i++)
1452 if (chip->pcmdevs[i]) {
1453 atiixp_dma_t *dma = &chip->dmas[i];
1454 if (dma->substream && dma->suspended) {
1455 dma->ops->enable_dma(chip, 1);
1456 dma->substream->ops->prepare(dma->substream);
1457 writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
1458 chip->remap_addr + dma->ops->llp_offset);
1459 writel(dma->saved_curptr, chip->remap_addr + dma->ops->dt_cur);
1465 #endif /* CONFIG_PM */
1469 * proc interface for register dump
1472 static void snd_atiixp_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
1474 atiixp_t *chip = entry->private_data;
1477 for (i = 0; i < 256; i += 4)
1478 snd_iprintf(buffer, "%02x: %08x\n", i, readl(chip->remap_addr + i));
1481 static void __devinit snd_atiixp_proc_init(atiixp_t *chip)
1483 snd_info_entry_t *entry;
1485 if (! snd_card_proc_new(chip->card, "atiixp", &entry))
1486 snd_info_set_text_ops(entry, chip, 1024, snd_atiixp_proc_read);
1495 static int snd_atiixp_free(atiixp_t *chip)
1499 snd_atiixp_chip_stop(chip);
1500 synchronize_irq(chip->irq);
1503 free_irq(chip->irq, (void *)chip);
1504 if (chip->remap_addr)
1505 iounmap(chip->remap_addr);
1506 pci_release_regions(chip->pci);
1507 pci_disable_device(chip->pci);
1512 static int snd_atiixp_dev_free(snd_device_t *device)
1514 atiixp_t *chip = device->device_data;
1515 return snd_atiixp_free(chip);
1519 * constructor for chip instance
1521 static int __devinit snd_atiixp_create(snd_card_t *card,
1522 struct pci_dev *pci,
1525 static snd_device_ops_t ops = {
1526 .dev_free = snd_atiixp_dev_free,
1531 if ((err = pci_enable_device(pci)) < 0)
1534 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1536 pci_disable_device(pci);
1540 spin_lock_init(&chip->reg_lock);
1541 init_MUTEX(&chip->open_mutex);
1545 if ((err = pci_request_regions(pci, "ATI IXP AC97")) < 0) {
1546 pci_disable_device(pci);
1550 chip->addr = pci_resource_start(pci, 0);
1551 chip->remap_addr = ioremap_nocache(chip->addr, pci_resource_len(pci, 0));
1552 if (chip->remap_addr == NULL) {
1553 snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
1554 snd_atiixp_free(chip);
1558 if (request_irq(pci->irq, snd_atiixp_interrupt, SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip)) {
1559 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
1560 snd_atiixp_free(chip);
1563 chip->irq = pci->irq;
1564 pci_set_master(pci);
1565 synchronize_irq(chip->irq);
1567 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1568 snd_atiixp_free(chip);
1572 snd_card_set_dev(card, &pci->dev);
1579 static int __devinit snd_atiixp_probe(struct pci_dev *pci,
1580 const struct pci_device_id *pci_id)
1584 unsigned char revision;
1587 card = snd_card_new(index, id, THIS_MODULE, 0);
1591 pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
1593 strcpy(card->driver, spdif_aclink ? "ATIIXP" : "ATIIXP-SPDMA");
1594 strcpy(card->shortname, "ATI IXP");
1595 if ((err = snd_atiixp_create(card, pci, &chip)) < 0)
1598 if ((err = snd_atiixp_aclink_reset(chip)) < 0)
1601 chip->spdif_over_aclink = spdif_aclink;
1603 if ((err = snd_atiixp_mixer_new(chip, ac97_clock, ac97_quirk)) < 0)
1606 if ((err = snd_atiixp_pcm_new(chip)) < 0)
1609 snd_atiixp_proc_init(chip);
1611 snd_atiixp_chip_start(chip);
1613 snprintf(card->longname, sizeof(card->longname),
1614 "%s rev %x with %s at %#lx, irq %i", card->shortname, revision,
1615 chip->ac97[0] ? snd_ac97_get_short_name(chip->ac97[0]) : "?",
1616 chip->addr, chip->irq);
1618 snd_card_set_pm_callback(card, snd_atiixp_suspend, snd_atiixp_resume, chip);
1620 if ((err = snd_card_register(card)) < 0)
1623 pci_set_drvdata(pci, card);
1627 snd_card_free(card);
1631 static void __devexit snd_atiixp_remove(struct pci_dev *pci)
1633 snd_card_free(pci_get_drvdata(pci));
1634 pci_set_drvdata(pci, NULL);
1637 static struct pci_driver driver = {
1638 .name = "ATI IXP AC97 controller",
1639 .owner = THIS_MODULE,
1640 .id_table = snd_atiixp_ids,
1641 .probe = snd_atiixp_probe,
1642 .remove = __devexit_p(snd_atiixp_remove),
1643 SND_PCI_PM_CALLBACKS
1647 static int __init alsa_card_atiixp_init(void)
1649 return pci_register_driver(&driver);
1652 static void __exit alsa_card_atiixp_exit(void)
1654 pci_unregister_driver(&driver);
1657 module_init(alsa_card_atiixp_init)
1658 module_exit(alsa_card_atiixp_exit)