14f912ae40dad7876b0ec61d6e924dd2b23dfb90
[safe/jmp/linux-2.6] / sound / pci / atiixp.c
1 /*
2  *   ALSA driver for ATI IXP 150/200/250/300 AC97 controllers
3  *
4  *      Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include <sound/driver.h>
23 #include <asm/io.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>
36
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}}");
41
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;
47
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.");
58
59 /* just for backward compatibility */
60 static int enable;
61 module_param(enable, int, 0444);
62
63
64 /*
65  */
66
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)
80
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 */
95
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)
127
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
134
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
139
140 #define ATI_REG_SLOTREQ                 0x14
141
142 #define ATI_REG_COUNTER                 0x18
143 #define  ATI_REG_COUNTER_SLOT           (3U<<0) /* slot # */
144 #define  ATI_REG_COUNTER_BITCLOCK       (31U<<8)
145
146 #define ATI_REG_IN_FIFO_THRESHOLD       0x1c
147
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
153
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
159
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
165
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 */
170
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
176
177 #define ATI_REG_MODEM_MIRROR            0x7c
178 #define ATI_REG_AUDIO_MIRROR            0x80
179
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 */
182
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)
186
187 /* LINKPTR */
188 #define  ATI_REG_LINKPTR_EN             (1U<<0)
189
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)
195
196
197 #define ATI_MAX_DESCRIPTORS     256     /* max number of descriptor packets */
198
199
200 /*
201  */
202
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;
206
207
208 /*
209  * DMA packate descriptor
210  */
211
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 */
217 } atiixp_dma_desc_t;
218
219 /*
220  * stream enum
221  */
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 */
225
226 #define NUM_ATI_CODECS  3
227
228
229 /*
230  * constants and callbacks for each DMA type
231  */
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) */
239 };
240
241 /*
242  * DMA stream
243  */
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;
250         int opened;
251         int running;
252         int suspended;
253         int pcm_open_flag;
254         int ac97_pcm_type;      /* index # of ac97_pcm to access, -1 = not used */
255         unsigned int saved_curptr;
256 };
257
258 /*
259  * ATI IXP chip
260  */
261 struct snd_atiixp {
262         snd_card_t *card;
263         struct pci_dev *pci;
264
265         unsigned long addr;
266         void __iomem *remap_addr;
267         int irq;
268         
269         ac97_bus_t *ac97_bus;
270         ac97_t *ac97[NUM_ATI_CODECS];
271
272         spinlock_t reg_lock;
273
274         atiixp_dma_t dmas[NUM_ATI_DMAS];
275         struct ac97_pcm *pcms[NUM_ATI_PCMS];
276         snd_pcm_t *pcmdevs[NUM_ATI_PCMDEVS];
277
278         int max_channels;               /* max. channels for PCM out */
279
280         unsigned int codec_not_ready_bits;      /* for codec detection */
281
282         int spdif_over_aclink;          /* passed from the module option */
283         struct semaphore open_mutex;    /* playback open mutex */
284 };
285
286
287 /*
288  */
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 */
293         { 0, }
294 };
295
296 MODULE_DEVICE_TABLE(pci, snd_atiixp_ids);
297
298
299 /*
300  * lowlevel functions
301  */
302
303 /*
304  * update the bits of the given register.
305  * return 1 if the bits changed.
306  */
307 static int snd_atiixp_update_bits(atiixp_t *chip, unsigned int reg,
308                                  unsigned int mask, unsigned int value)
309 {
310         void __iomem *addr = chip->remap_addr + reg;
311         unsigned int data, old_data;
312         old_data = data = readl(addr);
313         data &= ~mask;
314         data |= value;
315         if (old_data == data)
316                 return 0;
317         writel(data, addr);
318         return 1;
319 }
320
321 /*
322  * macros for easy use
323  */
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)
330
331 /* delay for one tick */
332 #define do_delay() do { \
333         set_current_state(TASK_UNINTERRUPTIBLE); \
334         schedule_timeout(1); \
335 } while (0)
336
337
338 /*
339  * handling DMA packets
340  *
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.
343  */
344
345 #define ATI_DESC_LIST_SIZE \
346         PAGE_ALIGN(ATI_MAX_DESCRIPTORS * sizeof(atiixp_dma_desc_t))
347
348 /*
349  * build packets ring for the given buffer size.
350  *
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.
354  *
355  * the ring is built in this function, and is set up to the hardware. 
356  */
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)
361 {
362         unsigned int i;
363         u32 addr, desc_addr;
364         unsigned long flags;
365
366         if (periods > ATI_MAX_DESCRIPTORS)
367                 return -ENOMEM;
368
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)
372                         return -ENOMEM;
373                 dma->period_bytes = dma->periods = 0; /* clear */
374         }
375
376         if (dma->periods == periods && dma->period_bytes == period_bytes)
377                 return 0;
378
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);
385
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);
392                 desc->status = 0;
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);
397                 else
398                         desc->next = cpu_to_le32(desc_addr);
399                 addr += period_bytes;
400         }
401
402         writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
403                chip->remap_addr + dma->ops->llp_offset);
404
405         dma->period_bytes = period_bytes;
406         dma->periods = periods;
407
408         return 0;
409 }
410
411 /*
412  * remove the ring buffer and release it if assigned
413  */
414 static void atiixp_clear_dma_packets(atiixp_t *chip, atiixp_dma_t *dma, snd_pcm_substream_t *substream)
415 {
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;
420         }
421 }
422
423 /*
424  * AC97 interface
425  */
426 static int snd_atiixp_acquire_codec(atiixp_t *chip)
427 {
428         int timeout = 1000;
429
430         while (atiixp_read(chip, PHYS_OUT_ADDR) & ATI_REG_PHYS_OUT_ADDR_EN) {
431                 if (! timeout--) {
432                         snd_printk(KERN_WARNING "atiixp: codec acquire timeout\n");
433                         return -EBUSY;
434                 }
435                 udelay(1);
436         }
437         return 0;
438 }
439
440 static unsigned short snd_atiixp_codec_read(atiixp_t *chip, unsigned short codec, unsigned short reg)
441 {
442         unsigned int data;
443         int timeout;
444
445         if (snd_atiixp_acquire_codec(chip) < 0)
446                 return 0xffff;
447         data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
448                 ATI_REG_PHYS_OUT_ADDR_EN |
449                 ATI_REG_PHYS_OUT_RW |
450                 codec;
451         atiixp_write(chip, PHYS_OUT_ADDR, data);
452         if (snd_atiixp_acquire_codec(chip) < 0)
453                 return 0xffff;
454         timeout = 1000;
455         do {
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;
459                 udelay(1);
460         } while (--timeout);
461         /* time out may happen during reset */
462         if (reg < 0x7c)
463                 snd_printk(KERN_WARNING "atiixp: codec read timeout (reg %x)\n", reg);
464         return 0xffff;
465 }
466
467
468 static void snd_atiixp_codec_write(atiixp_t *chip, unsigned short codec, unsigned short reg, unsigned short val)
469 {
470         unsigned int data;
471     
472         if (snd_atiixp_acquire_codec(chip) < 0)
473                 return;
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);
478 }
479
480
481 static unsigned short snd_atiixp_ac97_read(ac97_t *ac97, unsigned short reg)
482 {
483         atiixp_t *chip = ac97->private_data;
484         return snd_atiixp_codec_read(chip, ac97->num, reg);
485     
486 }
487
488 static void snd_atiixp_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short val)
489 {
490         atiixp_t *chip = ac97->private_data;
491         snd_atiixp_codec_write(chip, ac97->num, reg, val);
492 }
493
494 /*
495  * reset AC link
496  */
497 static int snd_atiixp_aclink_reset(atiixp_t *chip)
498 {
499         int timeout;
500
501         /* reset powerdoewn */
502         if (atiixp_update(chip, CMD, ATI_REG_CMD_POWERDOWN, 0))
503                 udelay(10);
504
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);
508         udelay(10);
509         atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, 0);
510     
511         timeout = 10;
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);
517                 do_delay();
518                 atiixp_update(chip, CMD, ATI_REG_CMD_AC_RESET, ATI_REG_CMD_AC_RESET);
519                 if (--timeout) {
520                         snd_printk(KERN_ERR "atiixp: codec reset timeout\n");
521                         break;
522                 }
523         }
524
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);
528
529         return 0;
530 }
531
532 #ifdef CONFIG_PM
533 static int snd_atiixp_aclink_down(atiixp_t *chip)
534 {
535         // if (atiixp_read(chip, MODEM_MIRROR) & 0x1) /* modem running, too? */
536         //      return -EBUSY;
537         atiixp_update(chip, CMD,
538                      ATI_REG_CMD_POWERDOWN | ATI_REG_CMD_AC_RESET,
539                      ATI_REG_CMD_POWERDOWN);
540         return 0;
541 }
542 #endif
543
544 /*
545  * auto-detection of codecs
546  *
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.
550  */
551
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)
557
558 static int snd_atiixp_codec_detect(atiixp_t *chip)
559 {
560         int timeout;
561
562         chip->codec_not_ready_bits = 0;
563         atiixp_write(chip, IER, CODEC_CHECK_BITS);
564         /* wait for the interrupts */
565         timeout = HZ / 10;
566         while (timeout-- > 0) {
567                 do_delay();
568                 if (chip->codec_not_ready_bits)
569                         break;
570         }
571         atiixp_write(chip, IER, 0); /* disable irqs */
572
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");
575                 return -ENXIO;
576         }
577         return 0;
578 }
579
580
581 /*
582  * enable DMA and irqs
583  */
584 static int snd_atiixp_chip_start(atiixp_t *chip)
585 {
586         unsigned int reg;
587
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);
593
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);
597
598         /* clear all interrupt source */
599         atiixp_write(chip, ISR, 0xffffffff);
600         /* enable irqs */
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);
607         return 0;
608 }
609
610
611 /*
612  * disable DMA and IRQs
613  */
614 static int snd_atiixp_chip_stop(atiixp_t *chip)
615 {
616         /* clear interrupt source */
617         atiixp_write(chip, ISR, atiixp_read(chip, ISR));
618         /* disable irqs */
619         atiixp_write(chip, IER, 0);
620         return 0;
621 }
622
623
624 /*
625  * PCM section
626  */
627
628 /*
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
631  * correctly...
632  */
633 static snd_pcm_uframes_t snd_atiixp_pcm_pointer(snd_pcm_substream_t *substream)
634 {
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;
638         unsigned int curptr;
639         int timeout = 1000;
640
641         while (timeout--) {
642                 curptr = readl(chip->remap_addr + dma->ops->dt_cur);
643                 if (curptr < dma->buf_addr)
644                         continue;
645                 curptr -= dma->buf_addr;
646                 if (curptr >= dma->buf_bytes)
647                         continue;
648                 return bytes_to_frames(runtime, curptr);
649         }
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);
652         return 0;
653 }
654
655 /*
656  * XRUN detected, and stop the PCM substream
657  */
658 static void snd_atiixp_xrun_dma(atiixp_t *chip, atiixp_dma_t *dma)
659 {
660         if (! dma->substream || ! dma->running)
661                 return;
662         snd_printdd("atiixp: XRUN detected (DMA %d)\n", dma->ops->type);
663         snd_pcm_stop(dma->substream, SNDRV_PCM_STATE_XRUN);
664 }
665
666 /*
667  * the period ack.  update the substream.
668  */
669 static void snd_atiixp_update_dma(atiixp_t *chip, atiixp_dma_t *dma)
670 {
671         if (! dma->substream || ! dma->running)
672                 return;
673         snd_pcm_period_elapsed(dma->substream);
674 }
675
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)
679 {
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;
685         else
686                 bus_busy = 0;
687         atiixp_update(chip, IER, ATI_REG_IER_SET_BUS_BUSY, bus_busy);
688 }
689
690 /* common trigger callback
691  * calling the lowlevel callbacks in it
692  */
693 static int snd_atiixp_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
694 {
695         atiixp_t *chip = snd_pcm_substream_chip(substream);
696         atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
697         int err = 0;
698
699         snd_assert(dma->ops->enable_transfer && dma->ops->flush_dma, return -EINVAL);
700
701         spin_lock(&chip->reg_lock);
702         switch (cmd) {
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);
707                 dma->running = 1;
708                 dma->suspended = 0;
709                 break;
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);
714                 dma->running = 0;
715                 dma->suspended = cmd == SNDRV_PCM_TRIGGER_SUSPEND;
716                 break;
717         default:
718                 err = -EINVAL;
719                 break;
720         }
721         if (! err) {
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);
726                 }
727         }
728         spin_unlock(&chip->reg_lock);
729         return err;
730 }
731
732
733 /*
734  * lowlevel callbacks for each DMA type
735  *
736  * every callback is supposed to be called in chip->reg_lock spinlock
737  */
738
739 /* flush FIFO of analog OUT DMA */
740 static void atiixp_out_flush_dma(atiixp_t *chip)
741 {
742         atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_OUT_FLUSH);
743 }
744
745 /* enable/disable analog OUT DMA */
746 static void atiixp_out_enable_dma(atiixp_t *chip, int on)
747 {
748         unsigned int data;
749         data = atiixp_read(chip, CMD);
750         if (on) {
751                 if (data & ATI_REG_CMD_OUT_DMA_EN)
752                         return;
753                 atiixp_out_flush_dma(chip);
754                 data |= ATI_REG_CMD_OUT_DMA_EN;
755         } else
756                 data &= ~ATI_REG_CMD_OUT_DMA_EN;
757         atiixp_write(chip, CMD, data);
758 }
759
760 /* start/stop transfer over OUT DMA */
761 static void atiixp_out_enable_transfer(atiixp_t *chip, int on)
762 {
763         atiixp_update(chip, CMD, ATI_REG_CMD_SEND_EN,
764                       on ? ATI_REG_CMD_SEND_EN : 0);
765 }
766
767 /* enable/disable analog IN DMA */
768 static void atiixp_in_enable_dma(atiixp_t *chip, int on)
769 {
770         atiixp_update(chip, CMD, ATI_REG_CMD_IN_DMA_EN,
771                       on ? ATI_REG_CMD_IN_DMA_EN : 0);
772 }
773
774 /* start/stop analog IN DMA */
775 static void atiixp_in_enable_transfer(atiixp_t *chip, int on)
776 {
777         if (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)
785                                 ;
786 #endif
787                         atiixp_write(chip, CMD, data);
788                 }
789         } else
790                 atiixp_update(chip, CMD, ATI_REG_CMD_RECEIVE_EN, 0);
791 }
792
793 /* flush FIFO of analog IN DMA */
794 static void atiixp_in_flush_dma(atiixp_t *chip)
795 {
796         atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_IN_FLUSH);
797 }
798
799 /* enable/disable SPDIF OUT DMA */
800 static void atiixp_spdif_enable_dma(atiixp_t *chip, int on)
801 {
802         atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_DMA_EN,
803                       on ? ATI_REG_CMD_SPDF_DMA_EN : 0);
804 }
805
806 /* start/stop SPDIF OUT DMA */
807 static void atiixp_spdif_enable_transfer(atiixp_t *chip, int on)
808 {
809         unsigned int data;
810         data = atiixp_read(chip, CMD);
811         if (on)
812                 data |= ATI_REG_CMD_SPDF_OUT_EN;
813         else
814                 data &= ~ATI_REG_CMD_SPDF_OUT_EN;
815         atiixp_write(chip, CMD, data);
816 }
817
818 /* flush FIFO of SPDIF OUT DMA */
819 static void atiixp_spdif_flush_dma(atiixp_t *chip)
820 {
821         int timeout;
822
823         /* DMA off, transfer on */
824         atiixp_spdif_enable_dma(chip, 0);
825         atiixp_spdif_enable_transfer(chip, 1);
826         
827         timeout = 100;
828         do {
829                 if (! (atiixp_read(chip, SPDF_DMA_DT_SIZE) & ATI_REG_DMA_FIFO_USED))
830                         break;
831                 udelay(1);
832         } while (timeout-- > 0);
833
834         atiixp_spdif_enable_transfer(chip, 0);
835 }
836
837 /* set up slots and formats for SPDIF OUT */
838 static int snd_atiixp_spdif_prepare(snd_pcm_substream_t *substream)
839 {
840         atiixp_t *chip = snd_pcm_substream_chip(substream);
841
842         spin_lock_irq(&chip->reg_lock);
843         if (chip->spdif_over_aclink) {
844                 unsigned int data;
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);
856         } else {
857                 atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK, 0);
858                 atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_SPDF, 0);
859         }
860         spin_unlock_irq(&chip->reg_lock);
861         return 0;
862 }
863
864 /* set up slots and formats for analog OUT */
865 static int snd_atiixp_playback_prepare(snd_pcm_substream_t *substream)
866 {
867         atiixp_t *chip = snd_pcm_substream_chip(substream);
868         unsigned int data;
869
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) {
873         case 8:
874                 data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
875                         ATI_REG_OUT_DMA_SLOT_BIT(11);
876                 /* fallthru */
877         case 6:
878                 data |= ATI_REG_OUT_DMA_SLOT_BIT(7) |
879                         ATI_REG_OUT_DMA_SLOT_BIT(8);
880                 /* fallthru */
881         case 4:
882                 data |= ATI_REG_OUT_DMA_SLOT_BIT(6) |
883                         ATI_REG_OUT_DMA_SLOT_BIT(9);
884                 /* fallthru */
885         default:
886                 data |= ATI_REG_OUT_DMA_SLOT_BIT(3) |
887                         ATI_REG_OUT_DMA_SLOT_BIT(4);
888                 break;
889         }
890
891         /* set output threshold */
892         data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
893         atiixp_write(chip, OUT_DMA_SLOT, data);
894
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);
898
899         /*
900          * enable 6 channel re-ordering bit if needed
901          */
902         atiixp_update(chip, 6CH_REORDER, ATI_REG_6CH_REORDER_EN,
903                       substream->runtime->channels >= 6 ? ATI_REG_6CH_REORDER_EN: 0);
904     
905         spin_unlock_irq(&chip->reg_lock);
906         return 0;
907 }
908
909 /* set up slots and formats for analog IN */
910 static int snd_atiixp_capture_prepare(snd_pcm_substream_t *substream)
911 {
912         atiixp_t *chip = snd_pcm_substream_chip(substream);
913
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);
919         return 0;
920 }
921
922 /*
923  * hw_params - allocate the buffer and set up buffer descriptors
924  */
925 static int snd_atiixp_pcm_hw_params(snd_pcm_substream_t *substream,
926                                    snd_pcm_hw_params_t *hw_params)
927 {
928         atiixp_t *chip = snd_pcm_substream_chip(substream);
929         atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
930         int err;
931
932         err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
933         if (err < 0)
934                 return err;
935         dma->buf_addr = substream->runtime->dma_addr;
936         dma->buf_bytes = params_buffer_bytes(hw_params);
937
938         err = atiixp_build_dma_packets(chip, dma, substream,
939                                        params_periods(hw_params),
940                                        params_period_bytes(hw_params));
941         if (err < 0)
942                 return err;
943
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
948                  */
949                 if (dma->pcm_open_flag) {
950                         snd_ac97_pcm_close(pcm);
951                         dma->pcm_open_flag = 0;
952                 }
953                 err = snd_ac97_pcm_open(pcm, params_rate(hw_params),
954                                         params_channels(hw_params),
955                                         pcm->r[0].slots);
956                 if (err >= 0)
957                         dma->pcm_open_flag = 1;
958         }
959
960         return err;
961 }
962
963 static int snd_atiixp_pcm_hw_free(snd_pcm_substream_t * substream)
964 {
965         atiixp_t *chip = snd_pcm_substream_chip(substream);
966         atiixp_dma_t *dma = (atiixp_dma_t *)substream->runtime->private_data;
967
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;
972         }
973         atiixp_clear_dma_packets(chip, dma, substream);
974         snd_pcm_lib_free_pages(substream);
975         return 0;
976 }
977
978
979 /*
980  * pcm hardware definition, identical for all DMA types
981  */
982 static snd_pcm_hardware_t snd_atiixp_pcm_hw =
983 {
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,
991         .rate_min =             48000,
992         .rate_max =             48000,
993         .channels_min =         2,
994         .channels_max =         2,
995         .buffer_bytes_max =     256 * 1024,
996         .period_bytes_min =     32,
997         .period_bytes_max =     128 * 1024,
998         .periods_min =          2,
999         .periods_max =          ATI_MAX_DESCRIPTORS,
1000 };
1001
1002 static int snd_atiixp_pcm_open(snd_pcm_substream_t *substream, atiixp_dma_t *dma, int pcm_type)
1003 {
1004         atiixp_t *chip = snd_pcm_substream_chip(substream);
1005         snd_pcm_runtime_t *runtime = substream->runtime;
1006         int err;
1007
1008         snd_assert(dma->ops && dma->ops->enable_dma, return -EINVAL);
1009
1010         if (dma->opened)
1011                 return -EBUSY;
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);
1018         } else {
1019                 /* direct SPDIF */
1020                 runtime->hw.formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1021         }
1022         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1023                 return err;
1024         runtime->private_data = dma;
1025
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);
1030         dma->opened = 1;
1031
1032         return 0;
1033 }
1034
1035 static int snd_atiixp_pcm_close(snd_pcm_substream_t *substream, atiixp_dma_t *dma)
1036 {
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;
1044         dma->opened = 0;
1045         return 0;
1046 }
1047
1048 /*
1049  */
1050 static int snd_atiixp_playback_open(snd_pcm_substream_t *substream)
1051 {
1052         atiixp_t *chip = snd_pcm_substream_chip(substream);
1053         int err;
1054
1055         down(&chip->open_mutex);
1056         err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 0);
1057         up(&chip->open_mutex);
1058         if (err < 0)
1059                 return err;
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);
1065         return 0;
1066 }
1067
1068 static int snd_atiixp_playback_close(snd_pcm_substream_t *substream)
1069 {
1070         atiixp_t *chip = snd_pcm_substream_chip(substream);
1071         int err;
1072         down(&chip->open_mutex);
1073         err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
1074         up(&chip->open_mutex);
1075         return err;
1076 }
1077
1078 static int snd_atiixp_capture_open(snd_pcm_substream_t *substream)
1079 {
1080         atiixp_t *chip = snd_pcm_substream_chip(substream);
1081         return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE], 1);
1082 }
1083
1084 static int snd_atiixp_capture_close(snd_pcm_substream_t *substream)
1085 {
1086         atiixp_t *chip = snd_pcm_substream_chip(substream);
1087         return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_CAPTURE]);
1088 }
1089
1090 static int snd_atiixp_spdif_open(snd_pcm_substream_t *substream)
1091 {
1092         atiixp_t *chip = snd_pcm_substream_chip(substream);
1093         int err;
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);
1097         else
1098                 err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_SPDIF], -1);
1099         up(&chip->open_mutex);
1100         return err;
1101 }
1102
1103 static int snd_atiixp_spdif_close(snd_pcm_substream_t *substream)
1104 {
1105         atiixp_t *chip = snd_pcm_substream_chip(substream);
1106         int err;
1107         down(&chip->open_mutex);
1108         if (chip->spdif_over_aclink)
1109                 err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
1110         else
1111                 err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_SPDIF]);
1112         up(&chip->open_mutex);
1113         return err;
1114 }
1115
1116 /* AC97 playback */
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,
1126 };
1127
1128 /* AC97 capture */
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,
1138 };
1139
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,
1150 };
1151
1152 static struct ac97_pcm atiixp_pcm_defs[] __devinitdata = {
1153         /* front PCM */
1154         {
1155                 .exclusive = 1,
1156                 .r = {  {
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)
1163                         }
1164                 }
1165         },
1166         /* PCM IN #1 */
1167         {
1168                 .stream = 1,
1169                 .exclusive = 1,
1170                 .r = {  {
1171                                 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1172                                          (1 << AC97_SLOT_PCM_RIGHT)
1173                         }
1174                 }
1175         },
1176         /* S/PDIF OUT (optional) */
1177         {
1178                 .exclusive = 1,
1179                 .spdif = 1,
1180                 .r = {  {
1181                                 .slots = (1 << AC97_SLOT_SPDIF_LEFT2) |
1182                                          (1 << AC97_SLOT_SPDIF_RIGHT2)
1183                         }
1184                 }
1185         },
1186 };
1187
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,
1195 };
1196         
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,
1204 };
1205         
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,
1213 };
1214         
1215
1216 static int __devinit snd_atiixp_pcm_new(atiixp_t *chip)
1217 {
1218         snd_pcm_t *pcm;
1219         ac97_bus_t *pbus = chip->ac97_bus;
1220         int err, i, num_pcms;
1221
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;
1227
1228         /* assign AC97 pcm */
1229         if (chip->spdif_over_aclink)
1230                 num_pcms = 3;
1231         else
1232                 num_pcms = 2;
1233         err = snd_ac97_pcm_assign(pbus, num_pcms, atiixp_pcm_defs);
1234         if (err < 0)
1235                 return err;
1236         for (i = 0; i < num_pcms; i++)
1237                 chip->pcms[i] = &pbus->pcms[i];
1238
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;
1243                 else
1244                         chip->max_channels = 4;
1245         }
1246
1247         /* PCM #0: analog I/O */
1248         err = snd_pcm_new(chip->card, "ATI IXP AC97", ATI_PCMDEV_ANALOG, 1, 1, &pcm);
1249         if (err < 0)
1250                 return err;
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;
1256
1257         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1258                                               snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
1259
1260         /* no SPDIF support on codec? */
1261         if (chip->pcms[ATI_PCM_SPDIF] && ! chip->pcms[ATI_PCM_SPDIF]->rates)
1262                 return 0;
1263                 
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;
1267
1268         /* PCM #1: spdif playback */
1269         err = snd_pcm_new(chip->card, "ATI IXP IEC958", ATI_PCMDEV_DIGITAL, 1, 0, &pcm);
1270         if (err < 0)
1271                 return err;
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)");
1276         else
1277                 strcpy(pcm->name, "ATI IXP IEC958 (Direct)");
1278         chip->pcmdevs[ATI_PCMDEV_DIGITAL] = pcm;
1279
1280         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1281                                               snd_dma_pci_data(chip->pci), 64*1024, 128*1024);
1282
1283         /* pre-select AC97 SPDIF slots 10/11 */
1284         for (i = 0; i < NUM_ATI_CODECS; i++) {
1285                 if (chip->ac97[i])
1286                         snd_ac97_update_bits(chip->ac97[i], AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
1287         }
1288
1289         return 0;
1290 }
1291
1292
1293
1294 /*
1295  * interrupt handler
1296  */
1297 static irqreturn_t snd_atiixp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1298 {
1299         atiixp_t *chip = dev_id;
1300         unsigned int status;
1301
1302         status = atiixp_read(chip, ISR);
1303
1304         if (! status)
1305                 return IRQ_NONE;
1306
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]);
1321         }
1322
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);
1331         }
1332
1333         /* ack */
1334         atiixp_write(chip, ISR, status);
1335
1336         return IRQ_HANDLED;
1337 }
1338
1339
1340 /*
1341  * ac97 mixer section
1342  */
1343
1344 static struct ac97_quirk ac97_quirks[] __devinitdata = {
1345         {
1346                 .subvendor = 0x103c,
1347                 .subdevice = 0x006b,
1348                 .name = "HP Pavilion ZV5030US",
1349                 .type = AC97_TUNE_MUTE_LED
1350         },
1351         { } /* terminator */
1352 };
1353
1354 static int __devinit snd_atiixp_mixer_new(atiixp_t *chip, int clock, const char *quirk_override)
1355 {
1356         ac97_bus_t *pbus;
1357         ac97_template_t ac97;
1358         int i, err;
1359         int codec_count;
1360         static ac97_bus_ops_t ops = {
1361                 .write = snd_atiixp_ac97_write,
1362                 .read = snd_atiixp_ac97_read,
1363         };
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,
1368         };
1369
1370         if (snd_atiixp_codec_detect(chip) < 0)
1371                 return -ENXIO;
1372
1373         if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
1374                 return err;
1375         pbus->clock = clock;
1376         chip->ac97_bus = pbus;
1377
1378         codec_count = 0;
1379         for (i = 0; i < NUM_ATI_CODECS; i++) {
1380                 if (chip->codec_not_ready_bits & codec_skip[i])
1381                         continue;
1382                 memset(&ac97, 0, sizeof(ac97));
1383                 ac97.private_data = chip;
1384                 ac97.pci = chip->pci;
1385                 ac97.num = i;
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);
1392                         continue;
1393                 }
1394                 codec_count++;
1395         }
1396
1397         if (! codec_count) {
1398                 snd_printk(KERN_ERR "atiixp: no codec available\n");
1399                 return -ENODEV;
1400         }
1401
1402         snd_ac97_tune_hardware(chip->ac97[0], ac97_quirks, quirk_override);
1403
1404         return 0;
1405 }
1406
1407
1408 #ifdef CONFIG_PM
1409 /*
1410  * power management
1411  */
1412 static int snd_atiixp_suspend(snd_card_t *card, pm_message_t state)
1413 {
1414         atiixp_t *chip = card->pm_private_data;
1415         int i;
1416
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]);
1423                 }
1424         for (i = 0; i < NUM_ATI_CODECS; i++)
1425                 if (chip->ac97[i])
1426                         snd_ac97_suspend(chip->ac97[i]);
1427         snd_atiixp_aclink_down(chip);
1428         snd_atiixp_chip_stop(chip);
1429
1430         pci_set_power_state(chip->pci, PCI_D3hot);
1431         pci_disable_device(chip->pci);
1432         return 0;
1433 }
1434
1435 static int snd_atiixp_resume(snd_card_t *card)
1436 {
1437         atiixp_t *chip = card->pm_private_data;
1438         int i;
1439
1440         pci_enable_device(chip->pci);
1441         pci_set_power_state(chip->pci, PCI_D0);
1442         pci_set_master(chip->pci);
1443
1444         snd_atiixp_aclink_reset(chip);
1445         snd_atiixp_chip_start(chip);
1446
1447         for (i = 0; i < NUM_ATI_CODECS; i++)
1448                 if (chip->ac97[i])
1449                         snd_ac97_resume(chip->ac97[i]);
1450
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);
1460                         }
1461                 }
1462
1463         return 0;
1464 }
1465 #endif /* CONFIG_PM */
1466
1467
1468 /*
1469  * proc interface for register dump
1470  */
1471
1472 static void snd_atiixp_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
1473 {
1474         atiixp_t *chip = entry->private_data;
1475         int i;
1476
1477         for (i = 0; i < 256; i += 4)
1478                 snd_iprintf(buffer, "%02x: %08x\n", i, readl(chip->remap_addr + i));
1479 }
1480
1481 static void __devinit snd_atiixp_proc_init(atiixp_t *chip)
1482 {
1483         snd_info_entry_t *entry;
1484
1485         if (! snd_card_proc_new(chip->card, "atiixp", &entry))
1486                 snd_info_set_text_ops(entry, chip, 1024, snd_atiixp_proc_read);
1487 }
1488
1489
1490
1491 /*
1492  * destructor
1493  */
1494
1495 static int snd_atiixp_free(atiixp_t *chip)
1496 {
1497         if (chip->irq < 0)
1498                 goto __hw_end;
1499         snd_atiixp_chip_stop(chip);
1500         synchronize_irq(chip->irq);
1501       __hw_end:
1502         if (chip->irq >= 0)
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);
1508         kfree(chip);
1509         return 0;
1510 }
1511
1512 static int snd_atiixp_dev_free(snd_device_t *device)
1513 {
1514         atiixp_t *chip = device->device_data;
1515         return snd_atiixp_free(chip);
1516 }
1517
1518 /*
1519  * constructor for chip instance
1520  */
1521 static int __devinit snd_atiixp_create(snd_card_t *card,
1522                                       struct pci_dev *pci,
1523                                       atiixp_t **r_chip)
1524 {
1525         static snd_device_ops_t ops = {
1526                 .dev_free =     snd_atiixp_dev_free,
1527         };
1528         atiixp_t *chip;
1529         int err;
1530
1531         if ((err = pci_enable_device(pci)) < 0)
1532                 return err;
1533
1534         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1535         if (chip == NULL) {
1536                 pci_disable_device(pci);
1537                 return -ENOMEM;
1538         }
1539
1540         spin_lock_init(&chip->reg_lock);
1541         init_MUTEX(&chip->open_mutex);
1542         chip->card = card;
1543         chip->pci = pci;
1544         chip->irq = -1;
1545         if ((err = pci_request_regions(pci, "ATI IXP AC97")) < 0) {
1546                 pci_disable_device(pci);
1547                 kfree(chip);
1548                 return err;
1549         }
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);
1555                 return -EIO;
1556         }
1557
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);
1561                 return -EBUSY;
1562         }
1563         chip->irq = pci->irq;
1564         pci_set_master(pci);
1565         synchronize_irq(chip->irq);
1566
1567         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1568                 snd_atiixp_free(chip);
1569                 return err;
1570         }
1571
1572         snd_card_set_dev(card, &pci->dev);
1573
1574         *r_chip = chip;
1575         return 0;
1576 }
1577
1578
1579 static int __devinit snd_atiixp_probe(struct pci_dev *pci,
1580                                      const struct pci_device_id *pci_id)
1581 {
1582         snd_card_t *card;
1583         atiixp_t *chip;
1584         unsigned char revision;
1585         int err;
1586
1587         card = snd_card_new(index, id, THIS_MODULE, 0);
1588         if (card == NULL)
1589                 return -ENOMEM;
1590
1591         pci_read_config_byte(pci, PCI_REVISION_ID, &revision);
1592
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)
1596                 goto __error;
1597
1598         if ((err = snd_atiixp_aclink_reset(chip)) < 0)
1599                 goto __error;
1600
1601         chip->spdif_over_aclink = spdif_aclink;
1602
1603         if ((err = snd_atiixp_mixer_new(chip, ac97_clock, ac97_quirk)) < 0)
1604                 goto __error;
1605
1606         if ((err = snd_atiixp_pcm_new(chip)) < 0)
1607                 goto __error;
1608         
1609         snd_atiixp_proc_init(chip);
1610
1611         snd_atiixp_chip_start(chip);
1612
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);
1617
1618         snd_card_set_pm_callback(card, snd_atiixp_suspend, snd_atiixp_resume, chip);
1619
1620         if ((err = snd_card_register(card)) < 0)
1621                 goto __error;
1622
1623         pci_set_drvdata(pci, card);
1624         return 0;
1625
1626  __error:
1627         snd_card_free(card);
1628         return err;
1629 }
1630
1631 static void __devexit snd_atiixp_remove(struct pci_dev *pci)
1632 {
1633         snd_card_free(pci_get_drvdata(pci));
1634         pci_set_drvdata(pci, NULL);
1635 }
1636
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
1644 };
1645
1646
1647 static int __init alsa_card_atiixp_init(void)
1648 {
1649         return pci_register_driver(&driver);
1650 }
1651
1652 static void __exit alsa_card_atiixp_exit(void)
1653 {
1654         pci_unregister_driver(&driver);
1655 }
1656
1657 module_init(alsa_card_atiixp_init)
1658 module_exit(alsa_card_atiixp_exit)