[ALSA] intel8x0 - enable ac97_quirk hp_only for Acer Aspire 3003LCi
[linux-2.6] / sound / pci / intel8x0.c
1 /*
2  *   ALSA driver for Intel ICH (i8x0) chipsets
3  *
4  *      Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
5  *
6  *
7  *   This code also contains alpha support for SiS 735 chipsets provided
8  *   by Mike Pieper <mptei@users.sourceforge.net>. We have no datasheet
9  *   for SiS735, so the code is not fully functional.
10  *
11  *
12  *   This program is free software; you can redistribute it and/or modify
13  *   it under the terms of the GNU General Public License as published by
14  *   the Free Software Foundation; either version 2 of the License, or
15  *   (at your option) any later version.
16  *
17  *   This program is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with this program; if not, write to the Free Software
24  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25
26  *
27  */      
28
29 #include <sound/driver.h>
30 #include <asm/io.h>
31 #include <linux/delay.h>
32 #include <linux/interrupt.h>
33 #include <linux/init.h>
34 #include <linux/pci.h>
35 #include <linux/slab.h>
36 #include <linux/moduleparam.h>
37 #include <sound/core.h>
38 #include <sound/pcm.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/info.h>
41 #include <sound/initval.h>
42 /* for 440MX workaround */
43 #include <asm/pgtable.h>
44 #include <asm/cacheflush.h>
45
46 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
47 MODULE_DESCRIPTION("Intel 82801AA,82901AB,i810,i820,i830,i840,i845,MX440; SiS 7012; Ali 5455");
48 MODULE_LICENSE("GPL");
49 MODULE_SUPPORTED_DEVICE("{{Intel,82801AA-ICH},"
50                 "{Intel,82901AB-ICH0},"
51                 "{Intel,82801BA-ICH2},"
52                 "{Intel,82801CA-ICH3},"
53                 "{Intel,82801DB-ICH4},"
54                 "{Intel,ICH5},"
55                 "{Intel,ICH6},"
56                 "{Intel,ICH7},"
57                 "{Intel,6300ESB},"
58                 "{Intel,ESB2},"
59                 "{Intel,MX440},"
60                 "{SiS,SI7012},"
61                 "{NVidia,nForce Audio},"
62                 "{NVidia,nForce2 Audio},"
63                 "{AMD,AMD768},"
64                 "{AMD,AMD8111},"
65                 "{ALI,M5455}}");
66
67 static int index = SNDRV_DEFAULT_IDX1;  /* Index 0-MAX */
68 static char *id = SNDRV_DEFAULT_STR1;   /* ID for this card */
69 static int ac97_clock = 0;
70 static char *ac97_quirk;
71 static int buggy_semaphore;
72 static int buggy_irq;
73 static int xbox;
74
75 module_param(index, int, 0444);
76 MODULE_PARM_DESC(index, "Index value for Intel i8x0 soundcard.");
77 module_param(id, charp, 0444);
78 MODULE_PARM_DESC(id, "ID string for Intel i8x0 soundcard.");
79 module_param(ac97_clock, int, 0444);
80 MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (0 = auto-detect).");
81 module_param(ac97_quirk, charp, 0444);
82 MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
83 module_param(buggy_semaphore, bool, 0444);
84 MODULE_PARM_DESC(buggy_semaphore, "Enable workaround for hardwares with problematic codec semaphores.");
85 module_param(buggy_irq, bool, 0444);
86 MODULE_PARM_DESC(buggy_irq, "Enable workaround for buggy interrupts on some motherboards.");
87 module_param(xbox, bool, 0444);
88 MODULE_PARM_DESC(xbox, "Set to 1 for Xbox, if you have problems with the AC'97 codec detection.");
89
90 /* just for backward compatibility */
91 static int enable;
92 module_param(enable, int, 0444);
93 static int joystick;
94 module_param(joystick, int, 0444);
95
96 /*
97  *  Direct registers
98  */
99 enum { DEVICE_INTEL, DEVICE_INTEL_ICH4, DEVICE_SIS, DEVICE_ALI, DEVICE_NFORCE };
100
101 #define ICHREG(x) ICH_REG_##x
102
103 #define DEFINE_REGSET(name,base) \
104 enum { \
105         ICH_REG_##name##_BDBAR  = base + 0x0,   /* dword - buffer descriptor list base address */ \
106         ICH_REG_##name##_CIV    = base + 0x04,  /* byte - current index value */ \
107         ICH_REG_##name##_LVI    = base + 0x05,  /* byte - last valid index */ \
108         ICH_REG_##name##_SR     = base + 0x06,  /* byte - status register */ \
109         ICH_REG_##name##_PICB   = base + 0x08,  /* word - position in current buffer */ \
110         ICH_REG_##name##_PIV    = base + 0x0a,  /* byte - prefetched index value */ \
111         ICH_REG_##name##_CR     = base + 0x0b,  /* byte - control register */ \
112 };
113
114 /* busmaster blocks */
115 DEFINE_REGSET(OFF, 0);          /* offset */
116 DEFINE_REGSET(PI, 0x00);        /* PCM in */
117 DEFINE_REGSET(PO, 0x10);        /* PCM out */
118 DEFINE_REGSET(MC, 0x20);        /* Mic in */
119
120 /* ICH4 busmaster blocks */
121 DEFINE_REGSET(MC2, 0x40);       /* Mic in 2 */
122 DEFINE_REGSET(PI2, 0x50);       /* PCM in 2 */
123 DEFINE_REGSET(SP, 0x60);        /* SPDIF out */
124
125 /* values for each busmaster block */
126
127 /* LVI */
128 #define ICH_REG_LVI_MASK                0x1f
129
130 /* SR */
131 #define ICH_FIFOE                       0x10    /* FIFO error */
132 #define ICH_BCIS                        0x08    /* buffer completion interrupt status */
133 #define ICH_LVBCI                       0x04    /* last valid buffer completion interrupt */
134 #define ICH_CELV                        0x02    /* current equals last valid */
135 #define ICH_DCH                         0x01    /* DMA controller halted */
136
137 /* PIV */
138 #define ICH_REG_PIV_MASK                0x1f    /* mask */
139
140 /* CR */
141 #define ICH_IOCE                        0x10    /* interrupt on completion enable */
142 #define ICH_FEIE                        0x08    /* fifo error interrupt enable */
143 #define ICH_LVBIE                       0x04    /* last valid buffer interrupt enable */
144 #define ICH_RESETREGS                   0x02    /* reset busmaster registers */
145 #define ICH_STARTBM                     0x01    /* start busmaster operation */
146
147
148 /* global block */
149 #define ICH_REG_GLOB_CNT                0x2c    /* dword - global control */
150 #define   ICH_PCM_SPDIF_MASK    0xc0000000      /* s/pdif pcm slot mask (ICH4) */
151 #define   ICH_PCM_SPDIF_NONE    0x00000000      /* reserved - undefined */
152 #define   ICH_PCM_SPDIF_78      0x40000000      /* s/pdif pcm on slots 7&8 */
153 #define   ICH_PCM_SPDIF_69      0x80000000      /* s/pdif pcm on slots 6&9 */
154 #define   ICH_PCM_SPDIF_1011    0xc0000000      /* s/pdif pcm on slots 10&11 */
155 #define   ICH_PCM_20BIT         0x00400000      /* 20-bit samples (ICH4) */
156 #define   ICH_PCM_246_MASK      0x00300000      /* 6 channels (not all chips) */
157 #define   ICH_PCM_6             0x00200000      /* 6 channels (not all chips) */
158 #define   ICH_PCM_4             0x00100000      /* 4 channels (not all chips) */
159 #define   ICH_PCM_2             0x00000000      /* 2 channels (stereo) */
160 #define   ICH_SIS_PCM_246_MASK  0x000000c0      /* 6 channels (SIS7012) */
161 #define   ICH_SIS_PCM_6         0x00000080      /* 6 channels (SIS7012) */
162 #define   ICH_SIS_PCM_4         0x00000040      /* 4 channels (SIS7012) */
163 #define   ICH_SIS_PCM_2         0x00000000      /* 2 channels (SIS7012) */
164 #define   ICH_TRIE              0x00000040      /* tertiary resume interrupt enable */
165 #define   ICH_SRIE              0x00000020      /* secondary resume interrupt enable */
166 #define   ICH_PRIE              0x00000010      /* primary resume interrupt enable */
167 #define   ICH_ACLINK            0x00000008      /* AClink shut off */
168 #define   ICH_AC97WARM          0x00000004      /* AC'97 warm reset */
169 #define   ICH_AC97COLD          0x00000002      /* AC'97 cold reset */
170 #define   ICH_GIE               0x00000001      /* GPI interrupt enable */
171 #define ICH_REG_GLOB_STA                0x30    /* dword - global status */
172 #define   ICH_TRI               0x20000000      /* ICH4: tertiary (AC_SDIN2) resume interrupt */
173 #define   ICH_TCR               0x10000000      /* ICH4: tertiary (AC_SDIN2) codec ready */
174 #define   ICH_BCS               0x08000000      /* ICH4: bit clock stopped */
175 #define   ICH_SPINT             0x04000000      /* ICH4: S/PDIF interrupt */
176 #define   ICH_P2INT             0x02000000      /* ICH4: PCM2-In interrupt */
177 #define   ICH_M2INT             0x01000000      /* ICH4: Mic2-In interrupt */
178 #define   ICH_SAMPLE_CAP        0x00c00000      /* ICH4: sample capability bits (RO) */
179 #define   ICH_SAMPLE_16_20      0x00400000      /* ICH4: 16- and 20-bit samples */
180 #define   ICH_MULTICHAN_CAP     0x00300000      /* ICH4: multi-channel capability bits (RO) */
181 #define   ICH_MD3               0x00020000      /* modem power down semaphore */
182 #define   ICH_AD3               0x00010000      /* audio power down semaphore */
183 #define   ICH_RCS               0x00008000      /* read completion status */
184 #define   ICH_BIT3              0x00004000      /* bit 3 slot 12 */
185 #define   ICH_BIT2              0x00002000      /* bit 2 slot 12 */
186 #define   ICH_BIT1              0x00001000      /* bit 1 slot 12 */
187 #define   ICH_SRI               0x00000800      /* secondary (AC_SDIN1) resume interrupt */
188 #define   ICH_PRI               0x00000400      /* primary (AC_SDIN0) resume interrupt */
189 #define   ICH_SCR               0x00000200      /* secondary (AC_SDIN1) codec ready */
190 #define   ICH_PCR               0x00000100      /* primary (AC_SDIN0) codec ready */
191 #define   ICH_MCINT             0x00000080      /* MIC capture interrupt */
192 #define   ICH_POINT             0x00000040      /* playback interrupt */
193 #define   ICH_PIINT             0x00000020      /* capture interrupt */
194 #define   ICH_NVSPINT           0x00000010      /* nforce spdif interrupt */
195 #define   ICH_MOINT             0x00000004      /* modem playback interrupt */
196 #define   ICH_MIINT             0x00000002      /* modem capture interrupt */
197 #define   ICH_GSCI              0x00000001      /* GPI status change interrupt */
198 #define ICH_REG_ACC_SEMA                0x34    /* byte - codec write semaphore */
199 #define   ICH_CAS               0x01            /* codec access semaphore */
200 #define ICH_REG_SDM             0x80
201 #define   ICH_DI2L_MASK         0x000000c0      /* PCM In 2, Mic In 2 data in line */
202 #define   ICH_DI2L_SHIFT        6
203 #define   ICH_DI1L_MASK         0x00000030      /* PCM In 1, Mic In 1 data in line */
204 #define   ICH_DI1L_SHIFT        4
205 #define   ICH_SE                0x00000008      /* steer enable */
206 #define   ICH_LDI_MASK          0x00000003      /* last codec read data input */
207
208 #define ICH_MAX_FRAGS           32              /* max hw frags */
209
210
211 /*
212  * registers for Ali5455
213  */
214
215 /* ALi 5455 busmaster blocks */
216 DEFINE_REGSET(AL_PI, 0x40);     /* ALi PCM in */
217 DEFINE_REGSET(AL_PO, 0x50);     /* Ali PCM out */
218 DEFINE_REGSET(AL_MC, 0x60);     /* Ali Mic in */
219 DEFINE_REGSET(AL_CDC_SPO, 0x70);        /* Ali Codec SPDIF out */
220 DEFINE_REGSET(AL_CENTER, 0x80);         /* Ali center out */
221 DEFINE_REGSET(AL_LFE, 0x90);            /* Ali center out */
222 DEFINE_REGSET(AL_CLR_SPI, 0xa0);        /* Ali Controller SPDIF in */
223 DEFINE_REGSET(AL_CLR_SPO, 0xb0);        /* Ali Controller SPDIF out */
224 DEFINE_REGSET(AL_I2S, 0xc0);    /* Ali I2S in */
225 DEFINE_REGSET(AL_PI2, 0xd0);    /* Ali PCM2 in */
226 DEFINE_REGSET(AL_MC2, 0xe0);    /* Ali Mic2 in */
227
228 enum {
229         ICH_REG_ALI_SCR = 0x00,         /* System Control Register */
230         ICH_REG_ALI_SSR = 0x04,         /* System Status Register  */
231         ICH_REG_ALI_DMACR = 0x08,       /* DMA Control Register    */
232         ICH_REG_ALI_FIFOCR1 = 0x0c,     /* FIFO Control Register 1  */
233         ICH_REG_ALI_INTERFACECR = 0x10, /* Interface Control Register */
234         ICH_REG_ALI_INTERRUPTCR = 0x14, /* Interrupt control Register */
235         ICH_REG_ALI_INTERRUPTSR = 0x18, /* Interrupt  Status Register */
236         ICH_REG_ALI_FIFOCR2 = 0x1c,     /* FIFO Control Register 2   */
237         ICH_REG_ALI_CPR = 0x20,         /* Command Port Register     */
238         ICH_REG_ALI_CPR_ADDR = 0x22,    /* ac97 addr write */
239         ICH_REG_ALI_SPR = 0x24,         /* Status Port Register      */
240         ICH_REG_ALI_SPR_ADDR = 0x26,    /* ac97 addr read */
241         ICH_REG_ALI_FIFOCR3 = 0x2c,     /* FIFO Control Register 3  */
242         ICH_REG_ALI_TTSR = 0x30,        /* Transmit Tag Slot Register */
243         ICH_REG_ALI_RTSR = 0x34,        /* Receive Tag Slot  Register */
244         ICH_REG_ALI_CSPSR = 0x38,       /* Command/Status Port Status Register */
245         ICH_REG_ALI_CAS = 0x3c,         /* Codec Write Semaphore Register */
246         ICH_REG_ALI_HWVOL = 0xf0,       /* hardware volume control/status */
247         ICH_REG_ALI_I2SCR = 0xf4,       /* I2S control/status */
248         ICH_REG_ALI_SPDIFCSR = 0xf8,    /* spdif channel status register  */
249         ICH_REG_ALI_SPDIFICS = 0xfc,    /* spdif interface control/status  */
250 };
251
252 #define ALI_CAS_SEM_BUSY        0x80000000
253 #define ALI_CPR_ADDR_SECONDARY  0x100
254 #define ALI_CPR_ADDR_READ       0x80
255 #define ALI_CSPSR_CODEC_READY   0x08
256 #define ALI_CSPSR_READ_OK       0x02
257 #define ALI_CSPSR_WRITE_OK      0x01
258
259 /* interrupts for the whole chip by interrupt status register finish */
260  
261 #define ALI_INT_MICIN2          (1<<26)
262 #define ALI_INT_PCMIN2          (1<<25)
263 #define ALI_INT_I2SIN           (1<<24)
264 #define ALI_INT_SPDIFOUT        (1<<23) /* controller spdif out INTERRUPT */
265 #define ALI_INT_SPDIFIN         (1<<22)
266 #define ALI_INT_LFEOUT          (1<<21)
267 #define ALI_INT_CENTEROUT       (1<<20)
268 #define ALI_INT_CODECSPDIFOUT   (1<<19)
269 #define ALI_INT_MICIN           (1<<18)
270 #define ALI_INT_PCMOUT          (1<<17)
271 #define ALI_INT_PCMIN           (1<<16)
272 #define ALI_INT_CPRAIS          (1<<7)  /* command port available */
273 #define ALI_INT_SPRAIS          (1<<5)  /* status port available */
274 #define ALI_INT_GPIO            (1<<1)
275 #define ALI_INT_MASK            (ALI_INT_SPDIFOUT|ALI_INT_CODECSPDIFOUT|ALI_INT_MICIN|ALI_INT_PCMOUT|ALI_INT_PCMIN)
276
277 #define ICH_ALI_SC_RESET        (1<<31) /* master reset */
278 #define ICH_ALI_SC_AC97_DBL     (1<<30)
279 #define ICH_ALI_SC_CODEC_SPDF   (3<<20) /* 1=7/8, 2=6/9, 3=10/11 */
280 #define ICH_ALI_SC_IN_BITS      (3<<18)
281 #define ICH_ALI_SC_OUT_BITS     (3<<16)
282 #define ICH_ALI_SC_6CH_CFG      (3<<14)
283 #define ICH_ALI_SC_PCM_4        (1<<8)
284 #define ICH_ALI_SC_PCM_6        (2<<8)
285 #define ICH_ALI_SC_PCM_246_MASK (3<<8)
286
287 #define ICH_ALI_SS_SEC_ID       (3<<5)
288 #define ICH_ALI_SS_PRI_ID       (3<<3)
289
290 #define ICH_ALI_IF_AC97SP       (1<<21)
291 #define ICH_ALI_IF_MC           (1<<20)
292 #define ICH_ALI_IF_PI           (1<<19)
293 #define ICH_ALI_IF_MC2          (1<<18)
294 #define ICH_ALI_IF_PI2          (1<<17)
295 #define ICH_ALI_IF_LINE_SRC     (1<<15) /* 0/1 = slot 3/6 */
296 #define ICH_ALI_IF_MIC_SRC      (1<<14) /* 0/1 = slot 3/6 */
297 #define ICH_ALI_IF_SPDF_SRC     (3<<12) /* 00 = PCM, 01 = AC97-in, 10 = spdif-in, 11 = i2s */
298 #define ICH_ALI_IF_AC97_OUT     (3<<8)  /* 00 = PCM, 10 = spdif-in, 11 = i2s */
299 #define ICH_ALI_IF_PO_SPDF      (1<<3)
300 #define ICH_ALI_IF_PO           (1<<1)
301
302 /*
303  *  
304  */
305
306 enum { ICHD_PCMIN, ICHD_PCMOUT, ICHD_MIC, ICHD_MIC2, ICHD_PCM2IN, ICHD_SPBAR, ICHD_LAST = ICHD_SPBAR };
307 enum { NVD_PCMIN, NVD_PCMOUT, NVD_MIC, NVD_SPBAR, NVD_LAST = NVD_SPBAR };
308 enum { ALID_PCMIN, ALID_PCMOUT, ALID_MIC, ALID_AC97SPDIFOUT, ALID_SPDIFIN, ALID_SPDIFOUT, ALID_LAST = ALID_SPDIFOUT };
309
310 #define get_ichdev(substream) (ichdev_t *)(substream->runtime->private_data)
311
312 typedef struct {
313         unsigned int ichd;                      /* ich device number */
314         unsigned long reg_offset;               /* offset to bmaddr */
315         u32 *bdbar;                             /* CPU address (32bit) */
316         unsigned int bdbar_addr;                /* PCI bus address (32bit) */
317         snd_pcm_substream_t *substream;
318         unsigned int physbuf;                   /* physical address (32bit) */
319         unsigned int size;
320         unsigned int fragsize;
321         unsigned int fragsize1;
322         unsigned int position;
323         unsigned int pos_shift;
324         int frags;
325         int lvi;
326         int lvi_frag;
327         int civ;
328         int ack;
329         int ack_reload;
330         unsigned int ack_bit;
331         unsigned int roff_sr;
332         unsigned int roff_picb;
333         unsigned int int_sta_mask;              /* interrupt status mask */
334         unsigned int ali_slot;                  /* ALI DMA slot */
335         struct ac97_pcm *pcm;
336         int pcm_open_flag;
337         unsigned int page_attr_changed: 1;
338         unsigned int suspended: 1;
339 } ichdev_t;
340
341 typedef struct _snd_intel8x0 intel8x0_t;
342
343 struct _snd_intel8x0 {
344         unsigned int device_type;
345
346         int irq;
347
348         unsigned int mmio;
349         unsigned long addr;
350         void __iomem *remap_addr;
351         unsigned int bm_mmio;
352         unsigned long bmaddr;
353         void __iomem *remap_bmaddr;
354
355         struct pci_dev *pci;
356         snd_card_t *card;
357
358         int pcm_devs;
359         snd_pcm_t *pcm[6];
360         ichdev_t ichd[6];
361
362         unsigned multi4: 1,
363                  multi6: 1,
364                  dra: 1,
365                  smp20bit: 1;
366         unsigned in_ac97_init: 1,
367                  in_sdin_init: 1;
368         unsigned in_measurement: 1;     /* during ac97 clock measurement */
369         unsigned fix_nocache: 1;        /* workaround for 440MX */
370         unsigned buggy_irq: 1;          /* workaround for buggy mobos */
371         unsigned xbox: 1;               /* workaround for Xbox AC'97 detection */
372         unsigned buggy_semaphore: 1;    /* workaround for buggy codec semaphore */
373
374         int spdif_idx;  /* SPDIF BAR index; *_SPBAR or -1 if use PCMOUT */
375         unsigned int sdm_saved; /* SDM reg value */
376
377         ac97_bus_t *ac97_bus;
378         ac97_t *ac97[3];
379         unsigned int ac97_sdin[3];
380
381         spinlock_t reg_lock;
382         
383         u32 bdbars_count;
384         struct snd_dma_buffer bdbars;
385         u32 int_sta_reg;                /* interrupt status register */
386         u32 int_sta_mask;               /* interrupt status mask */
387 };
388
389 static struct pci_device_id snd_intel8x0_ids[] = {
390         { 0x8086, 0x2415, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801AA */
391         { 0x8086, 0x2425, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82901AB */
392         { 0x8086, 0x2445, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801BA */
393         { 0x8086, 0x2485, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH3 */
394         { 0x8086, 0x24c5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH4 */
395         { 0x8086, 0x24d5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH5 */
396         { 0x8086, 0x25a6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ESB */
397         { 0x8086, 0x266e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH6 */
398         { 0x8086, 0x27de, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH7 */
399         { 0x8086, 0x2698, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ESB2 */
400         { 0x8086, 0x7195, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 440MX */
401         { 0x1039, 0x7012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_SIS },   /* SI7012 */
402         { 0x10de, 0x01b1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* NFORCE */
403         { 0x10de, 0x003a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* MCP04 */
404         { 0x10de, 0x006a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* NFORCE2 */
405         { 0x10de, 0x0059, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* CK804 */
406         { 0x10de, 0x008a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* CK8 */
407         { 0x10de, 0x00da, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* NFORCE3 */
408         { 0x10de, 0x00ea, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE },        /* CK8S */
409         { 0x1022, 0x746d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD8111 */
410         { 0x1022, 0x7445, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD768 */
411         { 0x10b9, 0x5455, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALI },   /* Ali5455 */
412         { 0, }
413 };
414
415 MODULE_DEVICE_TABLE(pci, snd_intel8x0_ids);
416
417 /*
418  *  Lowlevel I/O - busmaster
419  */
420
421 static u8 igetbyte(intel8x0_t *chip, u32 offset)
422 {
423         if (chip->bm_mmio)
424                 return readb(chip->remap_bmaddr + offset);
425         else
426                 return inb(chip->bmaddr + offset);
427 }
428
429 static u16 igetword(intel8x0_t *chip, u32 offset)
430 {
431         if (chip->bm_mmio)
432                 return readw(chip->remap_bmaddr + offset);
433         else
434                 return inw(chip->bmaddr + offset);
435 }
436
437 static u32 igetdword(intel8x0_t *chip, u32 offset)
438 {
439         if (chip->bm_mmio)
440                 return readl(chip->remap_bmaddr + offset);
441         else
442                 return inl(chip->bmaddr + offset);
443 }
444
445 static void iputbyte(intel8x0_t *chip, u32 offset, u8 val)
446 {
447         if (chip->bm_mmio)
448                 writeb(val, chip->remap_bmaddr + offset);
449         else
450                 outb(val, chip->bmaddr + offset);
451 }
452
453 static void iputword(intel8x0_t *chip, u32 offset, u16 val)
454 {
455         if (chip->bm_mmio)
456                 writew(val, chip->remap_bmaddr + offset);
457         else
458                 outw(val, chip->bmaddr + offset);
459 }
460
461 static void iputdword(intel8x0_t *chip, u32 offset, u32 val)
462 {
463         if (chip->bm_mmio)
464                 writel(val, chip->remap_bmaddr + offset);
465         else
466                 outl(val, chip->bmaddr + offset);
467 }
468
469 /*
470  *  Lowlevel I/O - AC'97 registers
471  */
472
473 static u16 iagetword(intel8x0_t *chip, u32 offset)
474 {
475         if (chip->mmio)
476                 return readw(chip->remap_addr + offset);
477         else
478                 return inw(chip->addr + offset);
479 }
480
481 static void iaputword(intel8x0_t *chip, u32 offset, u16 val)
482 {
483         if (chip->mmio)
484                 writew(val, chip->remap_addr + offset);
485         else
486                 outw(val, chip->addr + offset);
487 }
488
489 /*
490  *  Basic I/O
491  */
492
493 /*
494  * access to AC97 codec via normal i/o (for ICH and SIS7012)
495  */
496
497 /* return the GLOB_STA bit for the corresponding codec */
498 static unsigned int get_ich_codec_bit(intel8x0_t *chip, unsigned int codec)
499 {
500         static unsigned int codec_bit[3] = {
501                 ICH_PCR, ICH_SCR, ICH_TCR
502         };
503         snd_assert(codec < 3, return ICH_PCR);
504         if (chip->device_type == DEVICE_INTEL_ICH4)
505                 codec = chip->ac97_sdin[codec];
506         return codec_bit[codec];
507 }
508
509 static int snd_intel8x0_codec_semaphore(intel8x0_t *chip, unsigned int codec)
510 {
511         int time;
512         
513         if (codec > 2)
514                 return -EIO;
515         if (chip->in_sdin_init) {
516                 /* we don't know the ready bit assignment at the moment */
517                 /* so we check any */
518                 codec = ICH_PCR | ICH_SCR | ICH_TCR;
519         } else {
520                 codec = get_ich_codec_bit(chip, codec);
521         }
522
523         /* codec ready ? */
524         if ((igetdword(chip, ICHREG(GLOB_STA)) & codec) == 0)
525                 return -EIO;
526
527         if (chip->buggy_semaphore)
528                 return 0; /* just ignore ... */
529
530         /* Anyone holding a semaphore for 1 msec should be shot... */
531         time = 100;
532         do {
533                 if (!(igetbyte(chip, ICHREG(ACC_SEMA)) & ICH_CAS))
534                         return 0;
535                 udelay(10);
536         } while (time--);
537
538         /* access to some forbidden (non existant) ac97 registers will not
539          * reset the semaphore. So even if you don't get the semaphore, still
540          * continue the access. We don't need the semaphore anyway. */
541         snd_printk("codec_semaphore: semaphore is not ready [0x%x][0x%x]\n",
542                         igetbyte(chip, ICHREG(ACC_SEMA)), igetdword(chip, ICHREG(GLOB_STA)));
543         iagetword(chip, 0);     /* clear semaphore flag */
544         /* I don't care about the semaphore */
545         return -EBUSY;
546 }
547  
548 static void snd_intel8x0_codec_write(ac97_t *ac97,
549                                      unsigned short reg,
550                                      unsigned short val)
551 {
552         intel8x0_t *chip = ac97->private_data;
553         
554         if (snd_intel8x0_codec_semaphore(chip, ac97->num) < 0) {
555                 if (! chip->in_ac97_init)
556                         snd_printk("codec_write %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
557         }
558         iaputword(chip, reg + ac97->num * 0x80, val);
559 }
560
561 static unsigned short snd_intel8x0_codec_read(ac97_t *ac97,
562                                               unsigned short reg)
563 {
564         intel8x0_t *chip = ac97->private_data;
565         unsigned short res;
566         unsigned int tmp;
567
568         if (snd_intel8x0_codec_semaphore(chip, ac97->num) < 0) {
569                 if (! chip->in_ac97_init)
570                         snd_printk("codec_read %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
571                 res = 0xffff;
572         } else {
573                 res = iagetword(chip, reg + ac97->num * 0x80);
574                 if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
575                         /* reset RCS and preserve other R/WC bits */
576                         iputdword(chip, ICHREG(GLOB_STA), tmp & ~(ICH_SRI|ICH_PRI|ICH_TRI|ICH_GSCI));
577                         if (! chip->in_ac97_init)
578                                 snd_printk("codec_read %d: read timeout for register 0x%x\n", ac97->num, reg);
579                         res = 0xffff;
580                 }
581         }
582         return res;
583 }
584
585 static void snd_intel8x0_codec_read_test(intel8x0_t *chip, unsigned int codec)
586 {
587         unsigned int tmp;
588
589         if (snd_intel8x0_codec_semaphore(chip, codec) >= 0) {
590                 iagetword(chip, codec * 0x80);
591                 if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
592                         /* reset RCS and preserve other R/WC bits */
593                         iputdword(chip, ICHREG(GLOB_STA), tmp & ~(ICH_SRI|ICH_PRI|ICH_TRI|ICH_GSCI));
594                 }
595         }
596 }
597
598 /*
599  * access to AC97 for Ali5455
600  */
601 static int snd_intel8x0_ali_codec_ready(intel8x0_t *chip, int mask)
602 {
603         int count = 0;
604         for (count = 0; count < 0x7f; count++) {
605                 int val = igetbyte(chip, ICHREG(ALI_CSPSR));
606                 if (val & mask)
607                         return 0;
608         }
609         if (! chip->in_ac97_init)
610                 snd_printd(KERN_WARNING "intel8x0: AC97 codec ready timeout.\n");
611         return -EBUSY;
612 }
613
614 static int snd_intel8x0_ali_codec_semaphore(intel8x0_t *chip)
615 {
616         int time = 100;
617         if (chip->buggy_semaphore)
618                 return 0; /* just ignore ... */
619         while (time-- && (igetdword(chip, ICHREG(ALI_CAS)) & ALI_CAS_SEM_BUSY))
620                 udelay(1);
621         if (! time && ! chip->in_ac97_init)
622                 snd_printk(KERN_WARNING "ali_codec_semaphore timeout\n");
623         return snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_CODEC_READY);
624 }
625
626 static unsigned short snd_intel8x0_ali_codec_read(ac97_t *ac97, unsigned short reg)
627 {
628         intel8x0_t *chip = ac97->private_data;
629         unsigned short data = 0xffff;
630
631         if (snd_intel8x0_ali_codec_semaphore(chip))
632                 goto __err;
633         reg |= ALI_CPR_ADDR_READ;
634         if (ac97->num)
635                 reg |= ALI_CPR_ADDR_SECONDARY;
636         iputword(chip, ICHREG(ALI_CPR_ADDR), reg);
637         if (snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_READ_OK))
638                 goto __err;
639         data = igetword(chip, ICHREG(ALI_SPR));
640  __err:
641         return data;
642 }
643
644 static void snd_intel8x0_ali_codec_write(ac97_t *ac97, unsigned short reg, unsigned short val)
645 {
646         intel8x0_t *chip = ac97->private_data;
647
648         if (snd_intel8x0_ali_codec_semaphore(chip))
649                 return;
650         iputword(chip, ICHREG(ALI_CPR), val);
651         if (ac97->num)
652                 reg |= ALI_CPR_ADDR_SECONDARY;
653         iputword(chip, ICHREG(ALI_CPR_ADDR), reg);
654         snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_WRITE_OK);
655 }
656
657
658 /*
659  * DMA I/O
660  */
661 static void snd_intel8x0_setup_periods(intel8x0_t *chip, ichdev_t *ichdev) 
662 {
663         int idx;
664         u32 *bdbar = ichdev->bdbar;
665         unsigned long port = ichdev->reg_offset;
666
667         iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
668         if (ichdev->size == ichdev->fragsize) {
669                 ichdev->ack_reload = ichdev->ack = 2;
670                 ichdev->fragsize1 = ichdev->fragsize >> 1;
671                 for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 4) {
672                         bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf);
673                         bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
674                                                      ichdev->fragsize1 >> ichdev->pos_shift);
675                         bdbar[idx + 2] = cpu_to_le32(ichdev->physbuf + (ichdev->size >> 1));
676                         bdbar[idx + 3] = cpu_to_le32(0x80000000 | /* interrupt on completion */
677                                                      ichdev->fragsize1 >> ichdev->pos_shift);
678                 }
679                 ichdev->frags = 2;
680         } else {
681                 ichdev->ack_reload = ichdev->ack = 1;
682                 ichdev->fragsize1 = ichdev->fragsize;
683                 for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 2) {
684                         bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf + (((idx >> 1) * ichdev->fragsize) % ichdev->size));
685                         bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
686                                                      ichdev->fragsize >> ichdev->pos_shift);
687                         // printk("bdbar[%i] = 0x%x [0x%x]\n", idx + 0, bdbar[idx + 0], bdbar[idx + 1]);
688                 }
689                 ichdev->frags = ichdev->size / ichdev->fragsize;
690         }
691         iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi = ICH_REG_LVI_MASK);
692         ichdev->civ = 0;
693         iputbyte(chip, port + ICH_REG_OFF_CIV, 0);
694         ichdev->lvi_frag = ICH_REG_LVI_MASK % ichdev->frags;
695         ichdev->position = 0;
696 #if 0
697         printk("lvi_frag = %i, frags = %i, period_size = 0x%x, period_size1 = 0x%x\n",
698                         ichdev->lvi_frag, ichdev->frags, ichdev->fragsize, ichdev->fragsize1);
699 #endif
700         /* clear interrupts */
701         iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
702 }
703
704 #ifdef __i386__
705 /*
706  * Intel 82443MX running a 100MHz processor system bus has a hardware bug,
707  * which aborts PCI busmaster for audio transfer.  A workaround is to set
708  * the pages as non-cached.  For details, see the errata in
709  *      http://www.intel.com/design/chipsets/specupdt/245051.htm
710  */
711 static void fill_nocache(void *buf, int size, int nocache)
712 {
713         size = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
714         change_page_attr(virt_to_page(buf), size, nocache ? PAGE_KERNEL_NOCACHE : PAGE_KERNEL);
715         global_flush_tlb();
716 }
717 #else
718 #define fill_nocache(buf,size,nocache)
719 #endif
720
721 /*
722  *  Interrupt handler
723  */
724
725 static inline void snd_intel8x0_update(intel8x0_t *chip, ichdev_t *ichdev)
726 {
727         unsigned long port = ichdev->reg_offset;
728         int status, civ, i, step;
729         int ack = 0;
730
731         spin_lock(&chip->reg_lock);
732         status = igetbyte(chip, port + ichdev->roff_sr);
733         civ = igetbyte(chip, port + ICH_REG_OFF_CIV);
734         if (!(status & ICH_BCIS)) {
735                 step = 0;
736         } else if (civ == ichdev->civ) {
737                 // snd_printd("civ same %d\n", civ);
738                 step = 1;
739                 ichdev->civ++;
740                 ichdev->civ &= ICH_REG_LVI_MASK;
741         } else {
742                 step = civ - ichdev->civ;
743                 if (step < 0)
744                         step += ICH_REG_LVI_MASK + 1;
745                 // if (step != 1)
746                 //      snd_printd("step = %d, %d -> %d\n", step, ichdev->civ, civ);
747                 ichdev->civ = civ;
748         }
749
750         ichdev->position += step * ichdev->fragsize1;
751         if (! chip->in_measurement)
752                 ichdev->position %= ichdev->size;
753         ichdev->lvi += step;
754         ichdev->lvi &= ICH_REG_LVI_MASK;
755         iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
756         for (i = 0; i < step; i++) {
757                 ichdev->lvi_frag++;
758                 ichdev->lvi_frag %= ichdev->frags;
759                 ichdev->bdbar[ichdev->lvi * 2] = cpu_to_le32(ichdev->physbuf + ichdev->lvi_frag * ichdev->fragsize1);
760         // printk("new: bdbar[%i] = 0x%x [0x%x], prefetch = %i, all = 0x%x, 0x%x\n", ichdev->lvi * 2, ichdev->bdbar[ichdev->lvi * 2], ichdev->bdbar[ichdev->lvi * 2 + 1], inb(ICH_REG_OFF_PIV + port), inl(port + 4), inb(port + ICH_REG_OFF_CR));
761                 if (--ichdev->ack == 0) {
762                         ichdev->ack = ichdev->ack_reload;
763                         ack = 1;
764                 }
765         }
766         spin_unlock(&chip->reg_lock);
767         if (ack && ichdev->substream) {
768                 snd_pcm_period_elapsed(ichdev->substream);
769         }
770         iputbyte(chip, port + ichdev->roff_sr,
771                  status & (ICH_FIFOE | ICH_BCIS | ICH_LVBCI));
772 }
773
774 static irqreturn_t snd_intel8x0_interrupt(int irq, void *dev_id, struct pt_regs *regs)
775 {
776         intel8x0_t *chip = dev_id;
777         ichdev_t *ichdev;
778         unsigned int status;
779         unsigned int i;
780
781         status = igetdword(chip, chip->int_sta_reg);
782         if (status == 0xffffffff)       /* we are not yet resumed */
783                 return IRQ_NONE;
784
785         if ((status & chip->int_sta_mask) == 0) {
786                 if (status) {
787                         /* ack */
788                         iputdword(chip, chip->int_sta_reg, status);
789                         if (! chip->buggy_irq)
790                                 status = 0;
791                 }
792                 return IRQ_RETVAL(status);
793         }
794
795         for (i = 0; i < chip->bdbars_count; i++) {
796                 ichdev = &chip->ichd[i];
797                 if (status & ichdev->int_sta_mask)
798                         snd_intel8x0_update(chip, ichdev);
799         }
800
801         /* ack them */
802         iputdword(chip, chip->int_sta_reg, status & chip->int_sta_mask);
803         
804         return IRQ_HANDLED;
805 }
806
807 /*
808  *  PCM part
809  */
810
811 static int snd_intel8x0_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
812 {
813         intel8x0_t *chip = snd_pcm_substream_chip(substream);
814         ichdev_t *ichdev = get_ichdev(substream);
815         unsigned char val = 0;
816         unsigned long port = ichdev->reg_offset;
817
818         switch (cmd) {
819         case SNDRV_PCM_TRIGGER_RESUME:
820                 ichdev->suspended = 0;
821                 /* fallthru */
822         case SNDRV_PCM_TRIGGER_START:
823                 val = ICH_IOCE | ICH_STARTBM;
824                 break;
825         case SNDRV_PCM_TRIGGER_SUSPEND:
826                 ichdev->suspended = 1;
827                 /* fallthru */
828         case SNDRV_PCM_TRIGGER_STOP:
829                 val = 0;
830                 break;
831         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
832                 val = ICH_IOCE;
833                 break;
834         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
835                 val = ICH_IOCE | ICH_STARTBM;
836                 break;
837         default:
838                 return -EINVAL;
839         }
840         iputbyte(chip, port + ICH_REG_OFF_CR, val);
841         if (cmd == SNDRV_PCM_TRIGGER_STOP) {
842                 /* wait until DMA stopped */
843                 while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH)) ;
844                 /* reset whole DMA things */
845                 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
846         }
847         return 0;
848 }
849
850 static int snd_intel8x0_ali_trigger(snd_pcm_substream_t *substream, int cmd)
851 {
852         intel8x0_t *chip = snd_pcm_substream_chip(substream);
853         ichdev_t *ichdev = get_ichdev(substream);
854         unsigned long port = ichdev->reg_offset;
855         static int fiforeg[] = { ICHREG(ALI_FIFOCR1), ICHREG(ALI_FIFOCR2), ICHREG(ALI_FIFOCR3) };
856         unsigned int val, fifo;
857
858         val = igetdword(chip, ICHREG(ALI_DMACR));
859         switch (cmd) {
860         case SNDRV_PCM_TRIGGER_RESUME:
861                 ichdev->suspended = 0;
862                 /* fallthru */
863         case SNDRV_PCM_TRIGGER_START:
864         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
865                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
866                         /* clear FIFO for synchronization of channels */
867                         fifo = igetdword(chip, fiforeg[ichdev->ali_slot / 4]);
868                         fifo &= ~(0xff << (ichdev->ali_slot % 4));  
869                         fifo |= 0x83 << (ichdev->ali_slot % 4); 
870                         iputdword(chip, fiforeg[ichdev->ali_slot / 4], fifo);
871                 }
872                 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE);
873                 val &= ~(1 << (ichdev->ali_slot + 16)); /* clear PAUSE flag */
874                 iputdword(chip, ICHREG(ALI_DMACR), val | (1 << ichdev->ali_slot)); /* start DMA */
875                 break;
876         case SNDRV_PCM_TRIGGER_SUSPEND:
877                 ichdev->suspended = 1;
878                 /* fallthru */
879         case SNDRV_PCM_TRIGGER_STOP:
880         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
881                 iputdword(chip, ICHREG(ALI_DMACR), val | (1 << (ichdev->ali_slot + 16))); /* pause */
882                 iputbyte(chip, port + ICH_REG_OFF_CR, 0);
883                 while (igetbyte(chip, port + ICH_REG_OFF_CR))
884                         ;
885                 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
886                         break;
887                 /* reset whole DMA things */
888                 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
889                 /* clear interrupts */
890                 iputbyte(chip, port + ICH_REG_OFF_SR, igetbyte(chip, port + ICH_REG_OFF_SR) | 0x1e);
891                 iputdword(chip, ICHREG(ALI_INTERRUPTSR),
892                           igetdword(chip, ICHREG(ALI_INTERRUPTSR)) & ichdev->int_sta_mask);
893                 break;
894         default:
895                 return -EINVAL;
896         }
897         return 0;
898 }
899
900 static int snd_intel8x0_hw_params(snd_pcm_substream_t * substream,
901                                   snd_pcm_hw_params_t * hw_params)
902 {
903         intel8x0_t *chip = snd_pcm_substream_chip(substream);
904         ichdev_t *ichdev = get_ichdev(substream);
905         snd_pcm_runtime_t *runtime = substream->runtime;
906         int dbl = params_rate(hw_params) > 48000;
907         int err;
908
909         if (chip->fix_nocache && ichdev->page_attr_changed) {
910                 fill_nocache(runtime->dma_area, runtime->dma_bytes, 0); /* clear */
911                 ichdev->page_attr_changed = 0;
912         }
913         err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
914         if (err < 0)
915                 return err;
916         if (chip->fix_nocache) {
917                 if (runtime->dma_area && ! ichdev->page_attr_changed) {
918                         fill_nocache(runtime->dma_area, runtime->dma_bytes, 1);
919                         ichdev->page_attr_changed = 1;
920                 }
921         }
922         if (ichdev->pcm_open_flag) {
923                 snd_ac97_pcm_close(ichdev->pcm);
924                 ichdev->pcm_open_flag = 0;
925         }
926         err = snd_ac97_pcm_open(ichdev->pcm, params_rate(hw_params),
927                                 params_channels(hw_params),
928                                 ichdev->pcm->r[dbl].slots);
929         if (err >= 0) {
930                 ichdev->pcm_open_flag = 1;
931                 /* Force SPDIF setting */
932                 if (ichdev->ichd == ICHD_PCMOUT && chip->spdif_idx < 0)
933                         snd_ac97_set_rate(ichdev->pcm->r[0].codec[0], AC97_SPDIF, params_rate(hw_params));
934         }
935         return err;
936 }
937
938 static int snd_intel8x0_hw_free(snd_pcm_substream_t * substream)
939 {
940         intel8x0_t *chip = snd_pcm_substream_chip(substream);
941         ichdev_t *ichdev = get_ichdev(substream);
942
943         if (ichdev->pcm_open_flag) {
944                 snd_ac97_pcm_close(ichdev->pcm);
945                 ichdev->pcm_open_flag = 0;
946         }
947         if (chip->fix_nocache && ichdev->page_attr_changed) {
948                 fill_nocache(substream->runtime->dma_area, substream->runtime->dma_bytes, 0);
949                 ichdev->page_attr_changed = 0;
950         }
951         return snd_pcm_lib_free_pages(substream);
952 }
953
954 static void snd_intel8x0_setup_pcm_out(intel8x0_t *chip,
955                                        snd_pcm_runtime_t *runtime)
956 {
957         unsigned int cnt;
958         int dbl = runtime->rate > 48000;
959
960         spin_lock_irq(&chip->reg_lock);
961         switch (chip->device_type) {
962         case DEVICE_ALI:
963                 cnt = igetdword(chip, ICHREG(ALI_SCR));
964                 cnt &= ~ICH_ALI_SC_PCM_246_MASK;
965                 if (runtime->channels == 4 || dbl)
966                         cnt |= ICH_ALI_SC_PCM_4;
967                 else if (runtime->channels == 6)
968                         cnt |= ICH_ALI_SC_PCM_6;
969                 iputdword(chip, ICHREG(ALI_SCR), cnt);
970                 break;
971         case DEVICE_SIS:
972                 cnt = igetdword(chip, ICHREG(GLOB_CNT));
973                 cnt &= ~ICH_SIS_PCM_246_MASK;
974                 if (runtime->channels == 4 || dbl)
975                         cnt |= ICH_SIS_PCM_4;
976                 else if (runtime->channels == 6)
977                         cnt |= ICH_SIS_PCM_6;
978                 iputdword(chip, ICHREG(GLOB_CNT), cnt);
979                 break;
980         default:
981                 cnt = igetdword(chip, ICHREG(GLOB_CNT));
982                 cnt &= ~(ICH_PCM_246_MASK | ICH_PCM_20BIT);
983                 if (runtime->channels == 4 || dbl)
984                         cnt |= ICH_PCM_4;
985                 else if (runtime->channels == 6)
986                         cnt |= ICH_PCM_6;
987                 if (chip->device_type == DEVICE_NFORCE) {
988                         /* reset to 2ch once to keep the 6 channel data in alignment,
989                          * to start from Front Left always
990                          */
991                         if (cnt & ICH_PCM_246_MASK) {
992                                 iputdword(chip, ICHREG(GLOB_CNT), cnt & ~ICH_PCM_246_MASK);
993                                 spin_unlock_irq(&chip->reg_lock);
994                                 msleep(50); /* grrr... */
995                                 spin_lock_irq(&chip->reg_lock);
996                         }
997                 } else if (chip->device_type == DEVICE_INTEL_ICH4) {
998                         if (runtime->sample_bits > 16)
999                                 cnt |= ICH_PCM_20BIT;
1000                 }
1001                 iputdword(chip, ICHREG(GLOB_CNT), cnt);
1002                 break;
1003         }
1004         spin_unlock_irq(&chip->reg_lock);
1005 }
1006
1007 static int snd_intel8x0_pcm_prepare(snd_pcm_substream_t * substream)
1008 {
1009         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1010         snd_pcm_runtime_t *runtime = substream->runtime;
1011         ichdev_t *ichdev = get_ichdev(substream);
1012
1013         ichdev->physbuf = runtime->dma_addr;
1014         ichdev->size = snd_pcm_lib_buffer_bytes(substream);
1015         ichdev->fragsize = snd_pcm_lib_period_bytes(substream);
1016         if (ichdev->ichd == ICHD_PCMOUT) {
1017                 snd_intel8x0_setup_pcm_out(chip, runtime);
1018                 if (chip->device_type == DEVICE_INTEL_ICH4)
1019                         ichdev->pos_shift = (runtime->sample_bits > 16) ? 2 : 1;
1020         }
1021         snd_intel8x0_setup_periods(chip, ichdev);
1022         return 0;
1023 }
1024
1025 static snd_pcm_uframes_t snd_intel8x0_pcm_pointer(snd_pcm_substream_t * substream)
1026 {
1027         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1028         ichdev_t *ichdev = get_ichdev(substream);
1029         size_t ptr1, ptr;
1030         int civ, timeout = 100;
1031         unsigned int position;
1032
1033         spin_lock(&chip->reg_lock);
1034         do {
1035                 civ = igetbyte(chip, ichdev->reg_offset + ICH_REG_OFF_CIV);
1036                 ptr1 = igetword(chip, ichdev->reg_offset + ichdev->roff_picb);
1037                 position = ichdev->position;
1038                 if (ptr1 == 0) {
1039                         udelay(10);
1040                         continue;
1041                 }
1042                 if (civ == igetbyte(chip, ichdev->reg_offset + ICH_REG_OFF_CIV) &&
1043                     ptr1 == igetword(chip, ichdev->reg_offset + ichdev->roff_picb))
1044                         break;
1045         } while (timeout--);
1046         ptr1 <<= ichdev->pos_shift;
1047         ptr = ichdev->fragsize1 - ptr1;
1048         ptr += position;
1049         spin_unlock(&chip->reg_lock);
1050         if (ptr >= ichdev->size)
1051                 return 0;
1052         return bytes_to_frames(substream->runtime, ptr);
1053 }
1054
1055 static snd_pcm_hardware_t snd_intel8x0_stream =
1056 {
1057         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1058                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1059                                  SNDRV_PCM_INFO_MMAP_VALID |
1060                                  SNDRV_PCM_INFO_PAUSE |
1061                                  SNDRV_PCM_INFO_RESUME),
1062         .formats =              SNDRV_PCM_FMTBIT_S16_LE,
1063         .rates =                SNDRV_PCM_RATE_48000,
1064         .rate_min =             48000,
1065         .rate_max =             48000,
1066         .channels_min =         2,
1067         .channels_max =         2,
1068         .buffer_bytes_max =     128 * 1024,
1069         .period_bytes_min =     32,
1070         .period_bytes_max =     128 * 1024,
1071         .periods_min =          1,
1072         .periods_max =          1024,
1073         .fifo_size =            0,
1074 };
1075
1076 static unsigned int channels4[] = {
1077         2, 4,
1078 };
1079
1080 static snd_pcm_hw_constraint_list_t hw_constraints_channels4 = {
1081         .count = ARRAY_SIZE(channels4),
1082         .list = channels4,
1083         .mask = 0,
1084 };
1085
1086 static unsigned int channels6[] = {
1087         2, 4, 6,
1088 };
1089
1090 static snd_pcm_hw_constraint_list_t hw_constraints_channels6 = {
1091         .count = ARRAY_SIZE(channels6),
1092         .list = channels6,
1093         .mask = 0,
1094 };
1095
1096 static int snd_intel8x0_pcm_open(snd_pcm_substream_t * substream, ichdev_t *ichdev)
1097 {
1098         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1099         snd_pcm_runtime_t *runtime = substream->runtime;
1100         int err;
1101
1102         ichdev->substream = substream;
1103         runtime->hw = snd_intel8x0_stream;
1104         runtime->hw.rates = ichdev->pcm->rates;
1105         snd_pcm_limit_hw_rates(runtime);
1106         if (chip->device_type == DEVICE_SIS) {
1107                 runtime->hw.buffer_bytes_max = 64*1024;
1108                 runtime->hw.period_bytes_max = 64*1024;
1109         }
1110         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1111                 return err;
1112         runtime->private_data = ichdev;
1113         return 0;
1114 }
1115
1116 static int snd_intel8x0_playback_open(snd_pcm_substream_t * substream)
1117 {
1118         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1119         snd_pcm_runtime_t *runtime = substream->runtime;
1120         int err;
1121
1122         err = snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCMOUT]);
1123         if (err < 0)
1124                 return err;
1125
1126         if (chip->multi6) {
1127                 runtime->hw.channels_max = 6;
1128                 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels6);
1129         } else if (chip->multi4) {
1130                 runtime->hw.channels_max = 4;
1131                 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels4);
1132         }
1133         if (chip->dra) {
1134                 snd_ac97_pcm_double_rate_rules(runtime);
1135         }
1136         if (chip->smp20bit) {
1137                 runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
1138                 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 20);
1139         }
1140         return 0;
1141 }
1142
1143 static int snd_intel8x0_playback_close(snd_pcm_substream_t * substream)
1144 {
1145         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1146
1147         chip->ichd[ICHD_PCMOUT].substream = NULL;
1148         return 0;
1149 }
1150
1151 static int snd_intel8x0_capture_open(snd_pcm_substream_t * substream)
1152 {
1153         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1154
1155         return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCMIN]);
1156 }
1157
1158 static int snd_intel8x0_capture_close(snd_pcm_substream_t * substream)
1159 {
1160         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1161
1162         chip->ichd[ICHD_PCMIN].substream = NULL;
1163         return 0;
1164 }
1165
1166 static int snd_intel8x0_mic_open(snd_pcm_substream_t * substream)
1167 {
1168         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1169
1170         return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_MIC]);
1171 }
1172
1173 static int snd_intel8x0_mic_close(snd_pcm_substream_t * substream)
1174 {
1175         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1176
1177         chip->ichd[ICHD_MIC].substream = NULL;
1178         return 0;
1179 }
1180
1181 static int snd_intel8x0_mic2_open(snd_pcm_substream_t * substream)
1182 {
1183         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1184
1185         return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_MIC2]);
1186 }
1187
1188 static int snd_intel8x0_mic2_close(snd_pcm_substream_t * substream)
1189 {
1190         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1191
1192         chip->ichd[ICHD_MIC2].substream = NULL;
1193         return 0;
1194 }
1195
1196 static int snd_intel8x0_capture2_open(snd_pcm_substream_t * substream)
1197 {
1198         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1199
1200         return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCM2IN]);
1201 }
1202
1203 static int snd_intel8x0_capture2_close(snd_pcm_substream_t * substream)
1204 {
1205         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1206
1207         chip->ichd[ICHD_PCM2IN].substream = NULL;
1208         return 0;
1209 }
1210
1211 static int snd_intel8x0_spdif_open(snd_pcm_substream_t * substream)
1212 {
1213         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1214         int idx = chip->device_type == DEVICE_NFORCE ? NVD_SPBAR : ICHD_SPBAR;
1215
1216         return snd_intel8x0_pcm_open(substream, &chip->ichd[idx]);
1217 }
1218
1219 static int snd_intel8x0_spdif_close(snd_pcm_substream_t * substream)
1220 {
1221         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1222         int idx = chip->device_type == DEVICE_NFORCE ? NVD_SPBAR : ICHD_SPBAR;
1223
1224         chip->ichd[idx].substream = NULL;
1225         return 0;
1226 }
1227
1228 static int snd_intel8x0_ali_ac97spdifout_open(snd_pcm_substream_t * substream)
1229 {
1230         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1231         unsigned int val;
1232
1233         spin_lock_irq(&chip->reg_lock);
1234         val = igetdword(chip, ICHREG(ALI_INTERFACECR));
1235         val |= ICH_ALI_IF_AC97SP;
1236         iputdword(chip, ICHREG(ALI_INTERFACECR), val);
1237         /* also needs to set ALI_SC_CODEC_SPDF correctly */
1238         spin_unlock_irq(&chip->reg_lock);
1239
1240         return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_AC97SPDIFOUT]);
1241 }
1242
1243 static int snd_intel8x0_ali_ac97spdifout_close(snd_pcm_substream_t * substream)
1244 {
1245         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1246         unsigned int val;
1247
1248         chip->ichd[ALID_AC97SPDIFOUT].substream = NULL;
1249         spin_lock_irq(&chip->reg_lock);
1250         val = igetdword(chip, ICHREG(ALI_INTERFACECR));
1251         val &= ~ICH_ALI_IF_AC97SP;
1252         iputdword(chip, ICHREG(ALI_INTERFACECR), val);
1253         spin_unlock_irq(&chip->reg_lock);
1254
1255         return 0;
1256 }
1257
1258 static int snd_intel8x0_ali_spdifin_open(snd_pcm_substream_t * substream)
1259 {
1260         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1261
1262         return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_SPDIFIN]);
1263 }
1264
1265 static int snd_intel8x0_ali_spdifin_close(snd_pcm_substream_t * substream)
1266 {
1267         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1268
1269         chip->ichd[ALID_SPDIFIN].substream = NULL;
1270         return 0;
1271 }
1272
1273 #if 0 // NYI
1274 static int snd_intel8x0_ali_spdifout_open(snd_pcm_substream_t * substream)
1275 {
1276         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1277
1278         return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_SPDIFOUT]);
1279 }
1280
1281 static int snd_intel8x0_ali_spdifout_close(snd_pcm_substream_t * substream)
1282 {
1283         intel8x0_t *chip = snd_pcm_substream_chip(substream);
1284
1285         chip->ichd[ALID_SPDIFOUT].substream = NULL;
1286         return 0;
1287 }
1288 #endif
1289
1290 static snd_pcm_ops_t snd_intel8x0_playback_ops = {
1291         .open =         snd_intel8x0_playback_open,
1292         .close =        snd_intel8x0_playback_close,
1293         .ioctl =        snd_pcm_lib_ioctl,
1294         .hw_params =    snd_intel8x0_hw_params,
1295         .hw_free =      snd_intel8x0_hw_free,
1296         .prepare =      snd_intel8x0_pcm_prepare,
1297         .trigger =      snd_intel8x0_pcm_trigger,
1298         .pointer =      snd_intel8x0_pcm_pointer,
1299 };
1300
1301 static snd_pcm_ops_t snd_intel8x0_capture_ops = {
1302         .open =         snd_intel8x0_capture_open,
1303         .close =        snd_intel8x0_capture_close,
1304         .ioctl =        snd_pcm_lib_ioctl,
1305         .hw_params =    snd_intel8x0_hw_params,
1306         .hw_free =      snd_intel8x0_hw_free,
1307         .prepare =      snd_intel8x0_pcm_prepare,
1308         .trigger =      snd_intel8x0_pcm_trigger,
1309         .pointer =      snd_intel8x0_pcm_pointer,
1310 };
1311
1312 static snd_pcm_ops_t snd_intel8x0_capture_mic_ops = {
1313         .open =         snd_intel8x0_mic_open,
1314         .close =        snd_intel8x0_mic_close,
1315         .ioctl =        snd_pcm_lib_ioctl,
1316         .hw_params =    snd_intel8x0_hw_params,
1317         .hw_free =      snd_intel8x0_hw_free,
1318         .prepare =      snd_intel8x0_pcm_prepare,
1319         .trigger =      snd_intel8x0_pcm_trigger,
1320         .pointer =      snd_intel8x0_pcm_pointer,
1321 };
1322
1323 static snd_pcm_ops_t snd_intel8x0_capture_mic2_ops = {
1324         .open =         snd_intel8x0_mic2_open,
1325         .close =        snd_intel8x0_mic2_close,
1326         .ioctl =        snd_pcm_lib_ioctl,
1327         .hw_params =    snd_intel8x0_hw_params,
1328         .hw_free =      snd_intel8x0_hw_free,
1329         .prepare =      snd_intel8x0_pcm_prepare,
1330         .trigger =      snd_intel8x0_pcm_trigger,
1331         .pointer =      snd_intel8x0_pcm_pointer,
1332 };
1333
1334 static snd_pcm_ops_t snd_intel8x0_capture2_ops = {
1335         .open =         snd_intel8x0_capture2_open,
1336         .close =        snd_intel8x0_capture2_close,
1337         .ioctl =        snd_pcm_lib_ioctl,
1338         .hw_params =    snd_intel8x0_hw_params,
1339         .hw_free =      snd_intel8x0_hw_free,
1340         .prepare =      snd_intel8x0_pcm_prepare,
1341         .trigger =      snd_intel8x0_pcm_trigger,
1342         .pointer =      snd_intel8x0_pcm_pointer,
1343 };
1344
1345 static snd_pcm_ops_t snd_intel8x0_spdif_ops = {
1346         .open =         snd_intel8x0_spdif_open,
1347         .close =        snd_intel8x0_spdif_close,
1348         .ioctl =        snd_pcm_lib_ioctl,
1349         .hw_params =    snd_intel8x0_hw_params,
1350         .hw_free =      snd_intel8x0_hw_free,
1351         .prepare =      snd_intel8x0_pcm_prepare,
1352         .trigger =      snd_intel8x0_pcm_trigger,
1353         .pointer =      snd_intel8x0_pcm_pointer,
1354 };
1355
1356 static snd_pcm_ops_t snd_intel8x0_ali_playback_ops = {
1357         .open =         snd_intel8x0_playback_open,
1358         .close =        snd_intel8x0_playback_close,
1359         .ioctl =        snd_pcm_lib_ioctl,
1360         .hw_params =    snd_intel8x0_hw_params,
1361         .hw_free =      snd_intel8x0_hw_free,
1362         .prepare =      snd_intel8x0_pcm_prepare,
1363         .trigger =      snd_intel8x0_ali_trigger,
1364         .pointer =      snd_intel8x0_pcm_pointer,
1365 };
1366
1367 static snd_pcm_ops_t snd_intel8x0_ali_capture_ops = {
1368         .open =         snd_intel8x0_capture_open,
1369         .close =        snd_intel8x0_capture_close,
1370         .ioctl =        snd_pcm_lib_ioctl,
1371         .hw_params =    snd_intel8x0_hw_params,
1372         .hw_free =      snd_intel8x0_hw_free,
1373         .prepare =      snd_intel8x0_pcm_prepare,
1374         .trigger =      snd_intel8x0_ali_trigger,
1375         .pointer =      snd_intel8x0_pcm_pointer,
1376 };
1377
1378 static snd_pcm_ops_t snd_intel8x0_ali_capture_mic_ops = {
1379         .open =         snd_intel8x0_mic_open,
1380         .close =        snd_intel8x0_mic_close,
1381         .ioctl =        snd_pcm_lib_ioctl,
1382         .hw_params =    snd_intel8x0_hw_params,
1383         .hw_free =      snd_intel8x0_hw_free,
1384         .prepare =      snd_intel8x0_pcm_prepare,
1385         .trigger =      snd_intel8x0_ali_trigger,
1386         .pointer =      snd_intel8x0_pcm_pointer,
1387 };
1388
1389 static snd_pcm_ops_t snd_intel8x0_ali_ac97spdifout_ops = {
1390         .open =         snd_intel8x0_ali_ac97spdifout_open,
1391         .close =        snd_intel8x0_ali_ac97spdifout_close,
1392         .ioctl =        snd_pcm_lib_ioctl,
1393         .hw_params =    snd_intel8x0_hw_params,
1394         .hw_free =      snd_intel8x0_hw_free,
1395         .prepare =      snd_intel8x0_pcm_prepare,
1396         .trigger =      snd_intel8x0_ali_trigger,
1397         .pointer =      snd_intel8x0_pcm_pointer,
1398 };
1399
1400 static snd_pcm_ops_t snd_intel8x0_ali_spdifin_ops = {
1401         .open =         snd_intel8x0_ali_spdifin_open,
1402         .close =        snd_intel8x0_ali_spdifin_close,
1403         .ioctl =        snd_pcm_lib_ioctl,
1404         .hw_params =    snd_intel8x0_hw_params,
1405         .hw_free =      snd_intel8x0_hw_free,
1406         .prepare =      snd_intel8x0_pcm_prepare,
1407         .trigger =      snd_intel8x0_pcm_trigger,
1408         .pointer =      snd_intel8x0_pcm_pointer,
1409 };
1410
1411 #if 0 // NYI
1412 static snd_pcm_ops_t snd_intel8x0_ali_spdifout_ops = {
1413         .open =         snd_intel8x0_ali_spdifout_open,
1414         .close =        snd_intel8x0_ali_spdifout_close,
1415         .ioctl =        snd_pcm_lib_ioctl,
1416         .hw_params =    snd_intel8x0_hw_params,
1417         .hw_free =      snd_intel8x0_hw_free,
1418         .prepare =      snd_intel8x0_pcm_prepare,
1419         .trigger =      snd_intel8x0_pcm_trigger,
1420         .pointer =      snd_intel8x0_pcm_pointer,
1421 };
1422 #endif // NYI
1423
1424 struct ich_pcm_table {
1425         char *suffix;
1426         snd_pcm_ops_t *playback_ops;
1427         snd_pcm_ops_t *capture_ops;
1428         size_t prealloc_size;
1429         size_t prealloc_max_size;
1430         int ac97_idx;
1431 };
1432
1433 static int __devinit snd_intel8x0_pcm1(intel8x0_t *chip, int device, struct ich_pcm_table *rec)
1434 {
1435         snd_pcm_t *pcm;
1436         int err;
1437         char name[32];
1438
1439         if (rec->suffix)
1440                 sprintf(name, "Intel ICH - %s", rec->suffix);
1441         else
1442                 strcpy(name, "Intel ICH");
1443         err = snd_pcm_new(chip->card, name, device,
1444                           rec->playback_ops ? 1 : 0,
1445                           rec->capture_ops ? 1 : 0, &pcm);
1446         if (err < 0)
1447                 return err;
1448
1449         if (rec->playback_ops)
1450                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, rec->playback_ops);
1451         if (rec->capture_ops)
1452                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, rec->capture_ops);
1453
1454         pcm->private_data = chip;
1455         pcm->info_flags = 0;
1456         if (rec->suffix)
1457                 sprintf(pcm->name, "%s - %s", chip->card->shortname, rec->suffix);
1458         else
1459                 strcpy(pcm->name, chip->card->shortname);
1460         chip->pcm[device] = pcm;
1461
1462         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
1463                                               rec->prealloc_size, rec->prealloc_max_size);
1464
1465         return 0;
1466 }
1467
1468 static struct ich_pcm_table intel_pcms[] __devinitdata = {
1469         {
1470                 .playback_ops = &snd_intel8x0_playback_ops,
1471                 .capture_ops = &snd_intel8x0_capture_ops,
1472                 .prealloc_size = 64 * 1024,
1473                 .prealloc_max_size = 128 * 1024,
1474         },
1475         {
1476                 .suffix = "MIC ADC",
1477                 .capture_ops = &snd_intel8x0_capture_mic_ops,
1478                 .prealloc_size = 0,
1479                 .prealloc_max_size = 128 * 1024,
1480                 .ac97_idx = ICHD_MIC,
1481         },
1482         {
1483                 .suffix = "MIC2 ADC",
1484                 .capture_ops = &snd_intel8x0_capture_mic2_ops,
1485                 .prealloc_size = 0,
1486                 .prealloc_max_size = 128 * 1024,
1487                 .ac97_idx = ICHD_MIC2,
1488         },
1489         {
1490                 .suffix = "ADC2",
1491                 .capture_ops = &snd_intel8x0_capture2_ops,
1492                 .prealloc_size = 0,
1493                 .prealloc_max_size = 128 * 1024,
1494                 .ac97_idx = ICHD_PCM2IN,
1495         },
1496         {
1497                 .suffix = "IEC958",
1498                 .playback_ops = &snd_intel8x0_spdif_ops,
1499                 .prealloc_size = 64 * 1024,
1500                 .prealloc_max_size = 128 * 1024,
1501                 .ac97_idx = ICHD_SPBAR,
1502         },
1503 };
1504
1505 static struct ich_pcm_table nforce_pcms[] __devinitdata = {
1506         {
1507                 .playback_ops = &snd_intel8x0_playback_ops,
1508                 .capture_ops = &snd_intel8x0_capture_ops,
1509                 .prealloc_size = 64 * 1024,
1510                 .prealloc_max_size = 128 * 1024,
1511         },
1512         {
1513                 .suffix = "MIC ADC",
1514                 .capture_ops = &snd_intel8x0_capture_mic_ops,
1515                 .prealloc_size = 0,
1516                 .prealloc_max_size = 128 * 1024,
1517                 .ac97_idx = NVD_MIC,
1518         },
1519         {
1520                 .suffix = "IEC958",
1521                 .playback_ops = &snd_intel8x0_spdif_ops,
1522                 .prealloc_size = 64 * 1024,
1523                 .prealloc_max_size = 128 * 1024,
1524                 .ac97_idx = NVD_SPBAR,
1525         },
1526 };
1527
1528 static struct ich_pcm_table ali_pcms[] __devinitdata = {
1529         {
1530                 .playback_ops = &snd_intel8x0_ali_playback_ops,
1531                 .capture_ops = &snd_intel8x0_ali_capture_ops,
1532                 .prealloc_size = 64 * 1024,
1533                 .prealloc_max_size = 128 * 1024,
1534         },
1535         {
1536                 .suffix = "MIC ADC",
1537                 .capture_ops = &snd_intel8x0_ali_capture_mic_ops,
1538                 .prealloc_size = 0,
1539                 .prealloc_max_size = 128 * 1024,
1540                 .ac97_idx = ALID_MIC,
1541         },
1542         {
1543                 .suffix = "IEC958",
1544                 .playback_ops = &snd_intel8x0_ali_ac97spdifout_ops,
1545                 .capture_ops = &snd_intel8x0_ali_spdifin_ops,
1546                 .prealloc_size = 64 * 1024,
1547                 .prealloc_max_size = 128 * 1024,
1548                 .ac97_idx = ALID_AC97SPDIFOUT,
1549         },
1550 #if 0 // NYI
1551         {
1552                 .suffix = "HW IEC958",
1553                 .playback_ops = &snd_intel8x0_ali_spdifout_ops,
1554                 .prealloc_size = 64 * 1024,
1555                 .prealloc_max_size = 128 * 1024,
1556         },
1557 #endif
1558 };
1559
1560 static int __devinit snd_intel8x0_pcm(intel8x0_t *chip)
1561 {
1562         int i, tblsize, device, err;
1563         struct ich_pcm_table *tbl, *rec;
1564
1565         switch (chip->device_type) {
1566         case DEVICE_INTEL_ICH4:
1567                 tbl = intel_pcms;
1568                 tblsize = ARRAY_SIZE(intel_pcms);
1569                 break;
1570         case DEVICE_NFORCE:
1571                 tbl = nforce_pcms;
1572                 tblsize = ARRAY_SIZE(nforce_pcms);
1573                 break;
1574         case DEVICE_ALI:
1575                 tbl = ali_pcms;
1576                 tblsize = ARRAY_SIZE(ali_pcms);
1577                 break;
1578         default:
1579                 tbl = intel_pcms;
1580                 tblsize = 2;
1581                 break;
1582         }
1583
1584         device = 0;
1585         for (i = 0; i < tblsize; i++) {
1586                 rec = tbl + i;
1587                 if (i > 0 && rec->ac97_idx) {
1588                         /* activate PCM only when associated AC'97 codec */
1589                         if (! chip->ichd[rec->ac97_idx].pcm)
1590                                 continue;
1591                 }
1592                 err = snd_intel8x0_pcm1(chip, device, rec);
1593                 if (err < 0)
1594                         return err;
1595                 device++;
1596         }
1597
1598         chip->pcm_devs = device;
1599         return 0;
1600 }
1601         
1602
1603 /*
1604  *  Mixer part
1605  */
1606
1607 static void snd_intel8x0_mixer_free_ac97_bus(ac97_bus_t *bus)
1608 {
1609         intel8x0_t *chip = bus->private_data;
1610         chip->ac97_bus = NULL;
1611 }
1612
1613 static void snd_intel8x0_mixer_free_ac97(ac97_t *ac97)
1614 {
1615         intel8x0_t *chip = ac97->private_data;
1616         chip->ac97[ac97->num] = NULL;
1617 }
1618
1619 static struct ac97_pcm ac97_pcm_defs[] __devinitdata = {
1620         /* front PCM */
1621         {
1622                 .exclusive = 1,
1623                 .r = {  {
1624                                 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1625                                          (1 << AC97_SLOT_PCM_RIGHT) |
1626                                          (1 << AC97_SLOT_PCM_CENTER) |
1627                                          (1 << AC97_SLOT_PCM_SLEFT) |
1628                                          (1 << AC97_SLOT_PCM_SRIGHT) |
1629                                          (1 << AC97_SLOT_LFE)
1630                         },
1631                         {
1632                                 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1633                                          (1 << AC97_SLOT_PCM_RIGHT) |
1634                                          (1 << AC97_SLOT_PCM_LEFT_0) |
1635                                          (1 << AC97_SLOT_PCM_RIGHT_0)
1636                         }
1637                 }
1638         },
1639         /* PCM IN #1 */
1640         {
1641                 .stream = 1,
1642                 .exclusive = 1,
1643                 .r = {  {
1644                                 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1645                                          (1 << AC97_SLOT_PCM_RIGHT)
1646                         }
1647                 }
1648         },
1649         /* MIC IN #1 */
1650         {
1651                 .stream = 1,
1652                 .exclusive = 1,
1653                 .r = {  {
1654                                 .slots = (1 << AC97_SLOT_MIC)
1655                         }
1656                 }
1657         },
1658         /* S/PDIF PCM */
1659         {
1660                 .exclusive = 1,
1661                 .spdif = 1,
1662                 .r = {  {
1663                                 .slots = (1 << AC97_SLOT_SPDIF_LEFT2) |
1664                                          (1 << AC97_SLOT_SPDIF_RIGHT2)
1665                         }
1666                 }
1667         },
1668         /* PCM IN #2 */
1669         {
1670                 .stream = 1,
1671                 .exclusive = 1,
1672                 .r = {  {
1673                                 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1674                                          (1 << AC97_SLOT_PCM_RIGHT)
1675                         }
1676                 }
1677         },
1678         /* MIC IN #2 */
1679         {
1680                 .stream = 1,
1681                 .exclusive = 1,
1682                 .r = {  {
1683                                 .slots = (1 << AC97_SLOT_MIC)
1684                         }
1685                 }
1686         },
1687 };
1688
1689 static struct ac97_quirk ac97_quirks[] __devinitdata = {
1690         {
1691                 .subvendor = 0x0e11,
1692                 .subdevice = 0x008a,
1693                 .name = "Compaq Evo W4000",     /* AD1885 */
1694                 .type = AC97_TUNE_HP_ONLY
1695         },
1696         {
1697                 .subvendor = 0x0e11,
1698                 .subdevice = 0x00b8,
1699                 .name = "Compaq Evo D510C",
1700                 .type = AC97_TUNE_HP_ONLY
1701         },
1702         {
1703                 .subvendor = 0x0e11,
1704                 .subdevice = 0x0860,
1705                 .name = "HP/Compaq nx7010",
1706                 .type = AC97_TUNE_MUTE_LED
1707         },
1708         {
1709                 .subvendor = 0x1014,
1710                 .subdevice = 0x1f00,
1711                 .name = "MS-9128",
1712                 .type = AC97_TUNE_ALC_JACK
1713         },
1714         {
1715                 .subvendor = 0x1014,
1716                 .subdevice = 0x0267,
1717                 .name = "IBM NetVista A30p",    /* AD1981B */
1718                 .type = AC97_TUNE_HP_ONLY
1719         },
1720         {
1721                 .subvendor = 0x1025,
1722                 .subdevice = 0x0083,
1723                 .name = "Acer Aspire 3003LCi",
1724                 .type = AC97_TUNE_HP_ONLY
1725         },
1726         {
1727                 .subvendor = 0x1028,
1728                 .subdevice = 0x00d8,
1729                 .name = "Dell Precision 530",   /* AD1885 */
1730                 .type = AC97_TUNE_HP_ONLY
1731         },
1732         {
1733                 .subvendor = 0x1028,
1734                 .subdevice = 0x010d,
1735                 .name = "Dell", /* which model?  AD1885 */
1736                 .type = AC97_TUNE_HP_ONLY
1737         },
1738         {
1739                 .subvendor = 0x1028,
1740                 .subdevice = 0x0126,
1741                 .name = "Dell Optiplex GX260",  /* AD1981A */
1742                 .type = AC97_TUNE_HP_ONLY
1743         },
1744         {
1745                 .subvendor = 0x1028,
1746                 .subdevice = 0x012c,
1747                 .name = "Dell Precision 650",   /* AD1981A */
1748                 .type = AC97_TUNE_HP_ONLY
1749         },
1750         {
1751                 .subvendor = 0x1028,
1752                 .subdevice = 0x012d,
1753                 .name = "Dell Precision 450",   /* AD1981B*/
1754                 .type = AC97_TUNE_HP_ONLY
1755         },
1756         {
1757                 .subvendor = 0x1028,
1758                 .subdevice = 0x0147,
1759                 .name = "Dell", /* which model?  AD1981B*/
1760                 .type = AC97_TUNE_HP_ONLY
1761         },
1762         {
1763                 .subvendor = 0x1028,
1764                 .subdevice = 0x0163,
1765                 .name = "Dell Unknown", /* STAC9750/51 */
1766                 .type = AC97_TUNE_HP_ONLY
1767         },
1768         {
1769                 .subvendor = 0x103c,
1770                 .subdevice = 0x006d,
1771                 .name = "HP zv5000",
1772                 .type = AC97_TUNE_MUTE_LED      /*AD1981B*/
1773         },
1774         {       /* FIXME: which codec? */
1775                 .subvendor = 0x103c,
1776                 .subdevice = 0x00c3,
1777                 .name = "HP xw6000",
1778                 .type = AC97_TUNE_HP_ONLY
1779         },
1780         {
1781                 .subvendor = 0x103c,
1782                 .subdevice = 0x088c,
1783                 .name = "HP nc8000",
1784                 .type = AC97_TUNE_MUTE_LED
1785         },
1786         {
1787                 .subvendor = 0x103c,
1788                 .subdevice = 0x0890,
1789                 .name = "HP nc6000",
1790                 .type = AC97_TUNE_MUTE_LED
1791         },
1792         {
1793                 .subvendor = 0x103c,
1794                 .subdevice = 0x0934,
1795                 .name = "HP nx8220",
1796                 .type = AC97_TUNE_MUTE_LED
1797         },
1798         {
1799                 .subvendor = 0x103c,
1800                 .subdevice = 0x099c,
1801                 .name = "HP nx6110",    /* AD1981B */
1802                 .type = AC97_TUNE_HP_ONLY
1803         },
1804         {
1805                 .subvendor = 0x103c,
1806                 .subdevice = 0x129d,
1807                 .name = "HP xw8000",
1808                 .type = AC97_TUNE_HP_ONLY
1809         },
1810         {
1811                 .subvendor = 0x103c,
1812                 .subdevice = 0x12f1,
1813                 .name = "HP xw8200",    /* AD1981B*/
1814                 .type = AC97_TUNE_HP_ONLY
1815         },
1816         {
1817                 .subvendor = 0x103c,
1818                 .subdevice = 0x12f2,
1819                 .name = "HP xw6200",
1820                 .type = AC97_TUNE_HP_ONLY
1821         },
1822         {
1823                 .subvendor = 0x103c,
1824                 .subdevice = 0x3008,
1825                 .name = "HP xw4200",    /* AD1981B*/
1826                 .type = AC97_TUNE_HP_ONLY
1827         },
1828         {
1829                 .subvendor = 0x104d,
1830                 .subdevice = 0x8197,
1831                 .name = "Sony S1XP",
1832                 .type = AC97_TUNE_INV_EAPD
1833         },
1834         {
1835                 .subvendor = 0x1043,
1836                 .subdevice = 0x80f3,
1837                 .name = "ASUS ICH5/AD1985",
1838                 .type = AC97_TUNE_AD_SHARING
1839         },
1840         {
1841                 .subvendor = 0x10cf,
1842                 .subdevice = 0x11c3,
1843                 .name = "Fujitsu-Siemens E4010",
1844                 .type = AC97_TUNE_HP_ONLY
1845         },
1846         {
1847                 .subvendor = 0x10cf,
1848                 .subdevice = 0x1225,
1849                 .name = "Fujitsu-Siemens T3010",
1850                 .type = AC97_TUNE_HP_ONLY
1851         },
1852         {
1853                 .subvendor = 0x10cf,
1854                 .subdevice = 0x1253,
1855                 .name = "Fujitsu S6210",        /* STAC9750/51 */
1856                 .type = AC97_TUNE_HP_ONLY
1857         },
1858         {
1859                 .subvendor = 0x10cf,
1860                 .subdevice = 0x12ec,
1861                 .name = "Fujitsu-Siemens 4010",
1862                 .type = AC97_TUNE_HP_ONLY
1863         },
1864         {
1865                 .subvendor = 0x10f1,
1866                 .subdevice = 0x2665,
1867                 .name = "Fujitsu-Siemens Celsius",      /* AD1981? */
1868                 .type = AC97_TUNE_HP_ONLY
1869         },
1870         {
1871                 .subvendor = 0x10f1,
1872                 .subdevice = 0x2885,
1873                 .name = "AMD64 Mobo",   /* ALC650 */
1874                 .type = AC97_TUNE_HP_ONLY
1875         },
1876         {
1877                 .subvendor = 0x110a,
1878                 .subdevice = 0x0056,
1879                 .name = "Fujitsu-Siemens Scenic",       /* AD1981? */
1880                 .type = AC97_TUNE_HP_ONLY
1881         },
1882         {
1883                 .subvendor = 0x11d4,
1884                 .subdevice = 0x5375,
1885                 .name = "ADI AD1985 (discrete)",
1886                 .type = AC97_TUNE_HP_ONLY
1887         },
1888         {
1889                 .subvendor = 0x1462,
1890                 .subdevice = 0x5470,
1891                 .name = "MSI P4 ATX 645 Ultra",
1892                 .type = AC97_TUNE_HP_ONLY
1893         },
1894         {
1895                 .subvendor = 0x1734,
1896                 .subdevice = 0x0088,
1897                 .name = "Fujitsu-Siemens D1522",        /* AD1981 */
1898                 .type = AC97_TUNE_HP_ONLY
1899         },
1900         {
1901                 .subvendor = 0x8086,
1902                 .subdevice = 0x2000,
1903                 .mask = 0xfff0,
1904                 .name = "Intel ICH5/AD1985",
1905                 .type = AC97_TUNE_AD_SHARING
1906         },
1907         {
1908                 .subvendor = 0x8086,
1909                 .subdevice = 0x4000,
1910                 .mask = 0xfff0,
1911                 .name = "Intel ICH5/AD1985",
1912                 .type = AC97_TUNE_AD_SHARING
1913         },
1914         {
1915                 .subvendor = 0x8086,
1916                 .subdevice = 0x4856,
1917                 .name = "Intel D845WN (82801BA)",
1918                 .type = AC97_TUNE_SWAP_HP
1919         },
1920         {
1921                 .subvendor = 0x8086,
1922                 .subdevice = 0x4d44,
1923                 .name = "Intel D850EMV2",       /* AD1885 */
1924                 .type = AC97_TUNE_HP_ONLY
1925         },
1926         {
1927                 .subvendor = 0x8086,
1928                 .subdevice = 0x4d56,
1929                 .name = "Intel ICH/AD1885",
1930                 .type = AC97_TUNE_HP_ONLY
1931         },
1932         {
1933                 .subvendor = 0x8086,
1934                 .subdevice = 0x6000,
1935                 .mask = 0xfff0,
1936                 .name = "Intel ICH5/AD1985",
1937                 .type = AC97_TUNE_AD_SHARING
1938         },
1939         {
1940                 .subvendor = 0x8086,
1941                 .subdevice = 0xe000,
1942                 .mask = 0xfff0,
1943                 .name = "Intel ICH5/AD1985",
1944                 .type = AC97_TUNE_AD_SHARING
1945         },
1946 #if 0 /* FIXME: this seems wrong on most boards */
1947         {
1948                 .subvendor = 0x8086,
1949                 .subdevice = 0xa000,
1950                 .mask = 0xfff0,
1951                 .name = "Intel ICH5/AD1985",
1952                 .type = AC97_TUNE_HP_ONLY
1953         },
1954 #endif
1955         { } /* terminator */
1956 };
1957
1958 static int __devinit snd_intel8x0_mixer(intel8x0_t *chip, int ac97_clock, const char *quirk_override)
1959 {
1960         ac97_bus_t *pbus;
1961         ac97_template_t ac97;
1962         int err;
1963         unsigned int i, codecs;
1964         unsigned int glob_sta = 0;
1965         ac97_bus_ops_t *ops;
1966         static ac97_bus_ops_t standard_bus_ops = {
1967                 .write = snd_intel8x0_codec_write,
1968                 .read = snd_intel8x0_codec_read,
1969         };
1970         static ac97_bus_ops_t ali_bus_ops = {
1971                 .write = snd_intel8x0_ali_codec_write,
1972                 .read = snd_intel8x0_ali_codec_read,
1973         };
1974
1975         chip->spdif_idx = -1; /* use PCMOUT (or disabled) */
1976         switch (chip->device_type) {
1977         case DEVICE_NFORCE:
1978                 chip->spdif_idx = NVD_SPBAR;
1979                 break;
1980         case DEVICE_ALI:
1981                 chip->spdif_idx = ALID_AC97SPDIFOUT;
1982                 break;
1983         case DEVICE_INTEL_ICH4:
1984                 chip->spdif_idx = ICHD_SPBAR;
1985                 break;
1986         };
1987
1988         chip->in_ac97_init = 1;
1989         
1990         memset(&ac97, 0, sizeof(ac97));
1991         ac97.private_data = chip;
1992         ac97.private_free = snd_intel8x0_mixer_free_ac97;
1993         ac97.scaps = AC97_SCAP_SKIP_MODEM;
1994         if (chip->xbox)
1995                 ac97.scaps |= AC97_SCAP_DETECT_BY_VENDOR;
1996         if (chip->device_type != DEVICE_ALI) {
1997                 glob_sta = igetdword(chip, ICHREG(GLOB_STA));
1998                 ops = &standard_bus_ops;
1999                 if (chip->device_type == DEVICE_INTEL_ICH4) {
2000                         codecs = 0;
2001                         if (glob_sta & ICH_PCR)
2002                                 codecs++;
2003                         if (glob_sta & ICH_SCR)
2004                                 codecs++;
2005                         if (glob_sta & ICH_TCR)
2006                                 codecs++;
2007                         chip->in_sdin_init = 1;
2008                         for (i = 0; i < codecs; i++) {
2009                                 snd_intel8x0_codec_read_test(chip, i);
2010                                 chip->ac97_sdin[i] = igetbyte(chip, ICHREG(SDM)) & ICH_LDI_MASK;
2011                         }
2012                         chip->in_sdin_init = 0;
2013                 } else {
2014                         codecs = glob_sta & ICH_SCR ? 2 : 1;
2015                 }
2016         } else {
2017                 ops = &ali_bus_ops;
2018                 codecs = 1;
2019                 /* detect the secondary codec */
2020                 for (i = 0; i < 100; i++) {
2021                         unsigned int reg = igetdword(chip, ICHREG(ALI_RTSR));
2022                         if (reg & 0x40) {
2023                                 codecs = 2;
2024                                 break;
2025                         }
2026                         iputdword(chip, ICHREG(ALI_RTSR), reg | 0x40);
2027                         udelay(1);
2028                 }
2029         }
2030         if ((err = snd_ac97_bus(chip->card, 0, ops, chip, &pbus)) < 0)
2031                 goto __err;
2032         pbus->private_free = snd_intel8x0_mixer_free_ac97_bus;
2033         if (ac97_clock >= 8000 && ac97_clock <= 48000)
2034                 pbus->clock = ac97_clock;
2035         /* FIXME: my test board doesn't work well with VRA... */
2036         if (chip->device_type == DEVICE_ALI)
2037                 pbus->no_vra = 1;
2038         else
2039                 pbus->dra = 1;
2040         chip->ac97_bus = pbus;
2041
2042         ac97.pci = chip->pci;
2043         for (i = 0; i < codecs; i++) {
2044                 ac97.num = i;
2045                 if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
2046                         if (err != -EACCES)
2047                                 snd_printk(KERN_ERR "Unable to initialize codec #%d\n", i);
2048                         if (i == 0)
2049                                 goto __err;
2050                         continue;
2051                 }
2052         }
2053         /* tune up the primary codec */
2054         snd_ac97_tune_hardware(chip->ac97[0], ac97_quirks, quirk_override);
2055         /* enable separate SDINs for ICH4 */
2056         if (chip->device_type == DEVICE_INTEL_ICH4)
2057                 pbus->isdin = 1;
2058         /* find the available PCM streams */
2059         i = ARRAY_SIZE(ac97_pcm_defs);
2060         if (chip->device_type != DEVICE_INTEL_ICH4)
2061                 i -= 2;         /* do not allocate PCM2IN and MIC2 */
2062         if (chip->spdif_idx < 0)
2063                 i--;            /* do not allocate S/PDIF */
2064         err = snd_ac97_pcm_assign(pbus, i, ac97_pcm_defs);
2065         if (err < 0)
2066                 goto __err;
2067         chip->ichd[ICHD_PCMOUT].pcm = &pbus->pcms[0];
2068         chip->ichd[ICHD_PCMIN].pcm = &pbus->pcms[1];
2069         chip->ichd[ICHD_MIC].pcm = &pbus->pcms[2];
2070         if (chip->spdif_idx >= 0)
2071                 chip->ichd[chip->spdif_idx].pcm = &pbus->pcms[3];
2072         if (chip->device_type == DEVICE_INTEL_ICH4) {
2073                 chip->ichd[ICHD_PCM2IN].pcm = &pbus->pcms[4];
2074                 chip->ichd[ICHD_MIC2].pcm = &pbus->pcms[5];
2075         }
2076         /* enable separate SDINs for ICH4 */
2077         if (chip->device_type == DEVICE_INTEL_ICH4) {
2078                 struct ac97_pcm *pcm = chip->ichd[ICHD_PCM2IN].pcm;
2079                 u8 tmp = igetbyte(chip, ICHREG(SDM));
2080                 tmp &= ~(ICH_DI2L_MASK|ICH_DI1L_MASK);
2081                 if (pcm) {
2082                         tmp |= ICH_SE;  /* steer enable for multiple SDINs */
2083                         tmp |= chip->ac97_sdin[0] << ICH_DI1L_SHIFT;
2084                         for (i = 1; i < 4; i++) {
2085                                 if (pcm->r[0].codec[i]) {
2086                                         tmp |= chip->ac97_sdin[pcm->r[0].codec[1]->num] << ICH_DI2L_SHIFT;
2087                                         break;
2088                                 }
2089                         }
2090                 } else {
2091                         tmp &= ~ICH_SE; /* steer disable */
2092                 }
2093                 iputbyte(chip, ICHREG(SDM), tmp);
2094         }
2095         if (pbus->pcms[0].r[0].slots & (1 << AC97_SLOT_PCM_SLEFT)) {
2096                 chip->multi4 = 1;
2097                 if (pbus->pcms[0].r[0].slots & (1 << AC97_SLOT_LFE))
2098                         chip->multi6 = 1;
2099         }
2100         if (pbus->pcms[0].r[1].rslots[0]) {
2101                 chip->dra = 1;
2102         }
2103         if (chip->device_type == DEVICE_INTEL_ICH4) {
2104                 if ((igetdword(chip, ICHREG(GLOB_STA)) & ICH_SAMPLE_CAP) == ICH_SAMPLE_16_20)
2105                         chip->smp20bit = 1;
2106         }
2107         if (chip->device_type == DEVICE_NFORCE) {
2108                 /* 48kHz only */
2109                 chip->ichd[chip->spdif_idx].pcm->rates = SNDRV_PCM_RATE_48000;
2110         }
2111         if (chip->device_type == DEVICE_INTEL_ICH4) {
2112                 /* use slot 10/11 for SPDIF */
2113                 u32 val;
2114                 val = igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_PCM_SPDIF_MASK;
2115                 val |= ICH_PCM_SPDIF_1011;
2116                 iputdword(chip, ICHREG(GLOB_CNT), val);
2117                 snd_ac97_update_bits(chip->ac97[0], AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
2118         }
2119         chip->in_ac97_init = 0;
2120         return 0;
2121
2122  __err:
2123         /* clear the cold-reset bit for the next chance */
2124         if (chip->device_type != DEVICE_ALI)
2125                 iputdword(chip, ICHREG(GLOB_CNT), igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_AC97COLD);
2126         return err;
2127 }
2128
2129
2130 /*
2131  *
2132  */
2133
2134 static void do_ali_reset(intel8x0_t *chip)
2135 {
2136         iputdword(chip, ICHREG(ALI_SCR), ICH_ALI_SC_RESET);
2137         iputdword(chip, ICHREG(ALI_FIFOCR1), 0x83838383);
2138         iputdword(chip, ICHREG(ALI_FIFOCR2), 0x83838383);
2139         iputdword(chip, ICHREG(ALI_FIFOCR3), 0x83838383);
2140         iputdword(chip, ICHREG(ALI_INTERFACECR),
2141                   ICH_ALI_IF_MC|ICH_ALI_IF_PI|ICH_ALI_IF_PO);
2142         iputdword(chip, ICHREG(ALI_INTERRUPTCR), 0x00000000);
2143         iputdword(chip, ICHREG(ALI_INTERRUPTSR), 0x00000000);
2144 }
2145
2146 #define do_delay(chip) do {\
2147         set_current_state(TASK_UNINTERRUPTIBLE);\
2148         schedule_timeout(1);\
2149 } while (0)
2150
2151 static int snd_intel8x0_ich_chip_init(intel8x0_t *chip, int probing)
2152 {
2153         unsigned long end_time;
2154         unsigned int cnt, status, nstatus;
2155         
2156         /* put logic to right state */
2157         /* first clear status bits */
2158         status = ICH_RCS | ICH_MCINT | ICH_POINT | ICH_PIINT;
2159         if (chip->device_type == DEVICE_NFORCE)
2160                 status |= ICH_NVSPINT;
2161         cnt = igetdword(chip, ICHREG(GLOB_STA));
2162         iputdword(chip, ICHREG(GLOB_STA), cnt & status);
2163
2164         /* ACLink on, 2 channels */
2165         cnt = igetdword(chip, ICHREG(GLOB_CNT));
2166         cnt &= ~(ICH_ACLINK | ICH_PCM_246_MASK);
2167         /* finish cold or do warm reset */
2168         cnt |= (cnt & ICH_AC97COLD) == 0 ? ICH_AC97COLD : ICH_AC97WARM;
2169         iputdword(chip, ICHREG(GLOB_CNT), cnt);
2170         end_time = (jiffies + (HZ / 4)) + 1;
2171         do {
2172                 if ((igetdword(chip, ICHREG(GLOB_CNT)) & ICH_AC97WARM) == 0)
2173                         goto __ok;
2174                 do_delay(chip);
2175         } while (time_after_eq(end_time, jiffies));
2176         snd_printk("AC'97 warm reset still in progress? [0x%x]\n", igetdword(chip, ICHREG(GLOB_CNT)));
2177         return -EIO;
2178
2179       __ok:
2180         if (probing) {
2181                 /* wait for any codec ready status.
2182                  * Once it becomes ready it should remain ready
2183                  * as long as we do not disable the ac97 link.
2184                  */
2185                 end_time = jiffies + HZ;
2186                 do {
2187                         status = igetdword(chip, ICHREG(GLOB_STA)) & (ICH_PCR | ICH_SCR | ICH_TCR);
2188                         if (status)
2189                                 break;
2190                         do_delay(chip);
2191                 } while (time_after_eq(end_time, jiffies));
2192                 if (! status) {
2193                         /* no codec is found */
2194                         snd_printk(KERN_ERR "codec_ready: codec is not ready [0x%x]\n", igetdword(chip, ICHREG(GLOB_STA)));
2195                         return -EIO;
2196                 }
2197
2198                 if (chip->device_type == DEVICE_INTEL_ICH4)
2199                         /* ICH4 can have three codecs */
2200                         nstatus = ICH_PCR | ICH_SCR | ICH_TCR;
2201                 else
2202                         /* others up to two codecs */
2203                         nstatus = ICH_PCR | ICH_SCR;
2204
2205                 /* wait for other codecs ready status. */
2206                 end_time = jiffies + HZ / 4;
2207                 while (status != nstatus && time_after_eq(end_time, jiffies)) {
2208                         do_delay(chip);
2209                         status |= igetdword(chip, ICHREG(GLOB_STA)) & nstatus;
2210                 }
2211
2212         } else {
2213                 /* resume phase */
2214                 int i;
2215                 status = 0;
2216                 for (i = 0; i < 3; i++)
2217                         if (chip->ac97[i])
2218                                 status |= get_ich_codec_bit(chip, i);
2219                 /* wait until all the probed codecs are ready */
2220                 end_time = jiffies + HZ;
2221                 do {
2222                         nstatus = igetdword(chip, ICHREG(GLOB_STA)) & (ICH_PCR | ICH_SCR | ICH_TCR);
2223                         if (status == nstatus)
2224                                 break;
2225                         do_delay(chip);
2226                 } while (time_after_eq(end_time, jiffies));
2227         }
2228
2229         if (chip->device_type == DEVICE_SIS) {
2230                 /* unmute the output on SIS7012 */
2231                 iputword(chip, 0x4c, igetword(chip, 0x4c) | 1);
2232         }
2233         if (chip->device_type == DEVICE_NFORCE) {
2234                 /* enable SPDIF interrupt */
2235                 unsigned int val;
2236                 pci_read_config_dword(chip->pci, 0x4c, &val);
2237                 val |= 0x1000000;
2238                 pci_write_config_dword(chip->pci, 0x4c, val);
2239         }
2240         return 0;
2241 }
2242
2243 static int snd_intel8x0_ali_chip_init(intel8x0_t *chip, int probing)
2244 {
2245         u32 reg;
2246         int i = 0;
2247
2248         reg = igetdword(chip, ICHREG(ALI_SCR));
2249         if ((reg & 2) == 0)     /* Cold required */
2250                 reg |= 2;
2251         else
2252                 reg |= 1;       /* Warm */
2253         reg &= ~0x80000000;     /* ACLink on */
2254         iputdword(chip, ICHREG(ALI_SCR), reg);
2255
2256         for (i = 0; i < HZ / 2; i++) {
2257                 if (! (igetdword(chip, ICHREG(ALI_INTERRUPTSR)) & ALI_INT_GPIO))
2258                         goto __ok;
2259                 do_delay(chip);
2260         }
2261         snd_printk(KERN_ERR "AC'97 reset failed.\n");
2262         if (probing)
2263                 return -EIO;
2264
2265  __ok:
2266         for (i = 0; i < HZ / 2; i++) {
2267                 reg = igetdword(chip, ICHREG(ALI_RTSR));
2268                 if (reg & 0x80) /* primary codec */
2269                         break;
2270                 iputdword(chip, ICHREG(ALI_RTSR), reg | 0x80);
2271                 do_delay(chip);
2272         }
2273
2274         do_ali_reset(chip);
2275         return 0;
2276 }
2277
2278 static int snd_intel8x0_chip_init(intel8x0_t *chip, int probing)
2279 {
2280         unsigned int i;
2281         int err;
2282         
2283         if (chip->device_type != DEVICE_ALI) {
2284                 if ((err = snd_intel8x0_ich_chip_init(chip, probing)) < 0)
2285                         return err;
2286                 iagetword(chip, 0);     /* clear semaphore flag */
2287         } else {
2288                 if ((err = snd_intel8x0_ali_chip_init(chip, probing)) < 0)
2289                         return err;
2290         }
2291
2292         /* disable interrupts */
2293         for (i = 0; i < chip->bdbars_count; i++)
2294                 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
2295         /* reset channels */
2296         for (i = 0; i < chip->bdbars_count; i++)
2297                 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
2298         /* initialize Buffer Descriptor Lists */
2299         for (i = 0; i < chip->bdbars_count; i++)
2300                 iputdword(chip, ICH_REG_OFF_BDBAR + chip->ichd[i].reg_offset, chip->ichd[i].bdbar_addr);
2301         return 0;
2302 }
2303
2304 static int snd_intel8x0_free(intel8x0_t *chip)
2305 {
2306         unsigned int i;
2307
2308         if (chip->irq < 0)
2309                 goto __hw_end;
2310         /* disable interrupts */
2311         for (i = 0; i < chip->bdbars_count; i++)
2312                 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
2313         /* reset channels */
2314         for (i = 0; i < chip->bdbars_count; i++)
2315                 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
2316         if (chip->device_type == DEVICE_NFORCE) {
2317                 /* stop the spdif interrupt */
2318                 unsigned int val;
2319                 pci_read_config_dword(chip->pci, 0x4c, &val);
2320                 val &= ~0x1000000;
2321                 pci_write_config_dword(chip->pci, 0x4c, val);
2322         }
2323         /* --- */
2324         synchronize_irq(chip->irq);
2325       __hw_end:
2326         if (chip->irq >= 0)
2327                 free_irq(chip->irq, (void *)chip);
2328         if (chip->bdbars.area) {
2329                 if (chip->fix_nocache)
2330                         fill_nocache(chip->bdbars.area, chip->bdbars.bytes, 0);
2331                 snd_dma_free_pages(&chip->bdbars);
2332         }
2333         if (chip->remap_addr)
2334                 iounmap(chip->remap_addr);
2335         if (chip->remap_bmaddr)
2336                 iounmap(chip->remap_bmaddr);
2337         pci_release_regions(chip->pci);
2338         pci_disable_device(chip->pci);
2339         kfree(chip);
2340         return 0;
2341 }
2342
2343 #ifdef CONFIG_PM
2344 /*
2345  * power management
2346  */
2347 static int intel8x0_suspend(snd_card_t *card, pm_message_t state)
2348 {
2349         intel8x0_t *chip = card->pm_private_data;
2350         int i;
2351
2352         for (i = 0; i < chip->pcm_devs; i++)
2353                 snd_pcm_suspend_all(chip->pcm[i]);
2354         /* clear nocache */
2355         if (chip->fix_nocache) {
2356                 for (i = 0; i < chip->bdbars_count; i++) {
2357                         ichdev_t *ichdev = &chip->ichd[i];
2358                         if (ichdev->substream && ichdev->page_attr_changed) {
2359                                 snd_pcm_runtime_t *runtime = ichdev->substream->runtime;
2360                                 if (runtime->dma_area)
2361                                         fill_nocache(runtime->dma_area, runtime->dma_bytes, 0);
2362                         }
2363                 }
2364         }
2365         for (i = 0; i < 3; i++)
2366                 if (chip->ac97[i])
2367                         snd_ac97_suspend(chip->ac97[i]);
2368         if (chip->device_type == DEVICE_INTEL_ICH4)
2369                 chip->sdm_saved = igetbyte(chip, ICHREG(SDM));
2370
2371         if (chip->irq >= 0)
2372                 free_irq(chip->irq, (void *)chip);
2373         pci_disable_device(chip->pci);
2374         return 0;
2375 }
2376
2377 static int intel8x0_resume(snd_card_t *card)
2378 {
2379         intel8x0_t *chip = card->pm_private_data;
2380         int i;
2381
2382         pci_enable_device(chip->pci);
2383         pci_set_master(chip->pci);
2384         request_irq(chip->irq, snd_intel8x0_interrupt, SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip);
2385         synchronize_irq(chip->irq);
2386         snd_intel8x0_chip_init(chip, 1);
2387
2388         /* re-initialize mixer stuff */
2389         if (chip->device_type == DEVICE_INTEL_ICH4) {
2390                 /* enable separate SDINs for ICH4 */
2391                 iputbyte(chip, ICHREG(SDM), chip->sdm_saved);
2392                 /* use slot 10/11 for SPDIF */
2393                 iputdword(chip, ICHREG(GLOB_CNT),
2394                           (igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_PCM_SPDIF_MASK) |
2395                           ICH_PCM_SPDIF_1011);
2396         }
2397
2398         /* refill nocache */
2399         if (chip->fix_nocache)
2400                 fill_nocache(chip->bdbars.area, chip->bdbars.bytes, 1);
2401
2402         for (i = 0; i < 3; i++)
2403                 if (chip->ac97[i])
2404                         snd_ac97_resume(chip->ac97[i]);
2405
2406         /* refill nocache */
2407         if (chip->fix_nocache) {
2408                 for (i = 0; i < chip->bdbars_count; i++) {
2409                         ichdev_t *ichdev = &chip->ichd[i];
2410                         if (ichdev->substream && ichdev->page_attr_changed) {
2411                                 snd_pcm_runtime_t *runtime = ichdev->substream->runtime;
2412                                 if (runtime->dma_area)
2413                                         fill_nocache(runtime->dma_area, runtime->dma_bytes, 1);
2414                         }
2415                 }
2416         }
2417
2418         /* resume status */
2419         for (i = 0; i < chip->bdbars_count; i++) {
2420                 ichdev_t *ichdev = &chip->ichd[i];
2421                 unsigned long port = ichdev->reg_offset;
2422                 if (! ichdev->substream || ! ichdev->suspended)
2423                         continue;
2424                 if (ichdev->ichd == ICHD_PCMOUT)
2425                         snd_intel8x0_setup_pcm_out(chip, ichdev->substream->runtime);
2426                 iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
2427                 iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
2428                 iputbyte(chip, port + ICH_REG_OFF_CIV, ichdev->civ);
2429                 iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
2430         }
2431
2432         return 0;
2433 }
2434 #endif /* CONFIG_PM */
2435
2436 #define INTEL8X0_TESTBUF_SIZE   32768   /* enough large for one shot */
2437
2438 static void __devinit intel8x0_measure_ac97_clock(intel8x0_t *chip)
2439 {
2440         snd_pcm_substream_t *subs;
2441         ichdev_t *ichdev;
2442         unsigned long port;
2443         unsigned long pos, t;
2444         struct timeval start_time, stop_time;
2445
2446         if (chip->ac97_bus->clock != 48000)
2447                 return; /* specified in module option */
2448
2449         subs = chip->pcm[0]->streams[0].substream;
2450         if (! subs || subs->dma_buffer.bytes < INTEL8X0_TESTBUF_SIZE) {
2451                 snd_printk("no playback buffer allocated - aborting measure ac97 clock\n");
2452                 return;
2453         }
2454         ichdev = &chip->ichd[ICHD_PCMOUT];
2455         ichdev->physbuf = subs->dma_buffer.addr;
2456         ichdev->size = chip->ichd[ICHD_PCMOUT].fragsize = INTEL8X0_TESTBUF_SIZE;
2457         ichdev->substream = NULL; /* don't process interrupts */
2458
2459         /* set rate */
2460         if (snd_ac97_set_rate(chip->ac97[0], AC97_PCM_FRONT_DAC_RATE, 48000) < 0) {
2461                 snd_printk(KERN_ERR "cannot set ac97 rate: clock = %d\n", chip->ac97_bus->clock);
2462                 return;
2463         }
2464         snd_intel8x0_setup_periods(chip, ichdev);
2465         port = ichdev->reg_offset;
2466         spin_lock_irq(&chip->reg_lock);
2467         chip->in_measurement = 1;
2468         /* trigger */
2469         if (chip->device_type != DEVICE_ALI)
2470                 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE | ICH_STARTBM);
2471         else {
2472                 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE);
2473                 iputdword(chip, ICHREG(ALI_DMACR), 1 << ichdev->ali_slot);
2474         }
2475         do_gettimeofday(&start_time);
2476         spin_unlock_irq(&chip->reg_lock);
2477         msleep(50);
2478         spin_lock_irq(&chip->reg_lock);
2479         /* check the position */
2480         pos = ichdev->fragsize1;
2481         pos -= igetword(chip, ichdev->reg_offset + ichdev->roff_picb) << ichdev->pos_shift;
2482         pos += ichdev->position;
2483         chip->in_measurement = 0;
2484         do_gettimeofday(&stop_time);
2485         /* stop */
2486         if (chip->device_type == DEVICE_ALI) {
2487                 iputdword(chip, ICHREG(ALI_DMACR), 1 << (ichdev->ali_slot + 8));
2488                 iputbyte(chip, port + ICH_REG_OFF_CR, 0);
2489                 while (igetbyte(chip, port + ICH_REG_OFF_CR))
2490                         ;
2491         } else {
2492                 iputbyte(chip, port + ICH_REG_OFF_CR, 0);
2493                 while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH))
2494                         ;
2495         }
2496         iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
2497         spin_unlock_irq(&chip->reg_lock);
2498
2499         t = stop_time.tv_sec - start_time.tv_sec;
2500         t *= 1000000;
2501         t += stop_time.tv_usec - start_time.tv_usec;
2502         printk(KERN_INFO "%s: measured %lu usecs\n", __FUNCTION__, t);
2503         if (t == 0) {
2504                 snd_printk(KERN_ERR "?? calculation error..\n");
2505                 return;
2506         }
2507         pos = (pos / 4) * 1000;
2508         pos = (pos / t) * 1000 + ((pos % t) * 1000) / t;
2509         if (pos < 40000 || pos >= 60000) 
2510                 /* abnormal value. hw problem? */
2511                 printk(KERN_INFO "intel8x0: measured clock %ld rejected\n", pos);
2512         else if (pos < 47500 || pos > 48500)
2513                 /* not 48000Hz, tuning the clock.. */
2514                 chip->ac97_bus->clock = (chip->ac97_bus->clock * 48000) / pos;
2515         printk(KERN_INFO "intel8x0: clocking to %d\n", chip->ac97_bus->clock);
2516 }
2517
2518 static void snd_intel8x0_proc_read(snd_info_entry_t * entry,
2519                                    snd_info_buffer_t * buffer)
2520 {
2521         intel8x0_t *chip = entry->private_data;
2522         unsigned int tmp;
2523
2524         snd_iprintf(buffer, "Intel8x0\n\n");
2525         if (chip->device_type == DEVICE_ALI)
2526                 return;
2527         tmp = igetdword(chip, ICHREG(GLOB_STA));
2528         snd_iprintf(buffer, "Global control        : 0x%08x\n", igetdword(chip, ICHREG(GLOB_CNT)));
2529         snd_iprintf(buffer, "Global status         : 0x%08x\n", tmp);
2530         if (chip->device_type == DEVICE_INTEL_ICH4)
2531                 snd_iprintf(buffer, "SDM                   : 0x%08x\n", igetdword(chip, ICHREG(SDM)));
2532         snd_iprintf(buffer, "AC'97 codecs ready    :%s%s%s%s\n",
2533                         tmp & ICH_PCR ? " primary" : "",
2534                         tmp & ICH_SCR ? " secondary" : "",
2535                         tmp & ICH_TCR ? " tertiary" : "",
2536                         (tmp & (ICH_PCR | ICH_SCR | ICH_TCR)) == 0 ? " none" : "");
2537         if (chip->device_type == DEVICE_INTEL_ICH4)
2538                 snd_iprintf(buffer, "AC'97 codecs SDIN     : %i %i %i\n",
2539                         chip->ac97_sdin[0],
2540                         chip->ac97_sdin[1],
2541                         chip->ac97_sdin[2]);
2542 }
2543
2544 static void __devinit snd_intel8x0_proc_init(intel8x0_t * chip)
2545 {
2546         snd_info_entry_t *entry;
2547
2548         if (! snd_card_proc_new(chip->card, "intel8x0", &entry))
2549                 snd_info_set_text_ops(entry, chip, 1024, snd_intel8x0_proc_read);
2550 }
2551
2552 static int snd_intel8x0_dev_free(snd_device_t *device)
2553 {
2554         intel8x0_t *chip = device->device_data;
2555         return snd_intel8x0_free(chip);
2556 }
2557
2558 struct ich_reg_info {
2559         unsigned int int_sta_mask;
2560         unsigned int offset;
2561 };
2562
2563 static int __devinit snd_intel8x0_create(snd_card_t * card,
2564                                          struct pci_dev *pci,
2565                                          unsigned long device_type,
2566                                          int buggy_sem,
2567                                          intel8x0_t ** r_intel8x0)
2568 {
2569         intel8x0_t *chip;
2570         int err;
2571         unsigned int i;
2572         unsigned int int_sta_masks;
2573         ichdev_t *ichdev;
2574         static snd_device_ops_t ops = {
2575                 .dev_free =     snd_intel8x0_dev_free,
2576         };
2577
2578         static unsigned int bdbars[] = {
2579                 3, /* DEVICE_INTEL */
2580                 6, /* DEVICE_INTEL_ICH4 */
2581                 3, /* DEVICE_SIS */
2582                 6, /* DEVICE_ALI */
2583                 4, /* DEVICE_NFORCE */
2584         };
2585         static struct ich_reg_info intel_regs[6] = {
2586                 { ICH_PIINT, 0 },
2587                 { ICH_POINT, 0x10 },
2588                 { ICH_MCINT, 0x20 },
2589                 { ICH_M2INT, 0x40 },
2590                 { ICH_P2INT, 0x50 },
2591                 { ICH_SPINT, 0x60 },
2592         };
2593         static struct ich_reg_info nforce_regs[4] = {
2594                 { ICH_PIINT, 0 },
2595                 { ICH_POINT, 0x10 },
2596                 { ICH_MCINT, 0x20 },
2597                 { ICH_NVSPINT, 0x70 },
2598         };
2599         static struct ich_reg_info ali_regs[6] = {
2600                 { ALI_INT_PCMIN, 0x40 },
2601                 { ALI_INT_PCMOUT, 0x50 },
2602                 { ALI_INT_MICIN, 0x60 },
2603                 { ALI_INT_CODECSPDIFOUT, 0x70 },
2604                 { ALI_INT_SPDIFIN, 0xa0 },
2605                 { ALI_INT_SPDIFOUT, 0xb0 },
2606         };
2607         struct ich_reg_info *tbl;
2608
2609         *r_intel8x0 = NULL;
2610
2611         if ((err = pci_enable_device(pci)) < 0)
2612                 return err;
2613
2614         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
2615         if (chip == NULL) {
2616                 pci_disable_device(pci);
2617                 return -ENOMEM;
2618         }
2619         spin_lock_init(&chip->reg_lock);
2620         chip->device_type = device_type;
2621         chip->card = card;
2622         chip->pci = pci;
2623         chip->irq = -1;
2624         chip->buggy_semaphore = buggy_sem;
2625
2626         if (pci->vendor == PCI_VENDOR_ID_INTEL &&
2627             pci->device == PCI_DEVICE_ID_INTEL_440MX)
2628                 chip->fix_nocache = 1; /* enable workaround */
2629
2630         /* some Nforce[2] and ICH boards have problems with IRQ handling.
2631          * Needs to return IRQ_HANDLED for unknown irqs.
2632          */
2633         if (device_type == DEVICE_NFORCE)
2634                 chip->buggy_irq = 1;
2635
2636         if ((err = pci_request_regions(pci, card->shortname)) < 0) {
2637                 kfree(chip);
2638                 pci_disable_device(pci);
2639                 return err;
2640         }
2641
2642         if (device_type == DEVICE_ALI) {
2643                 /* ALI5455 has no ac97 region */
2644                 chip->bmaddr = pci_resource_start(pci, 0);
2645                 goto port_inited;
2646         }
2647
2648         if (pci_resource_flags(pci, 2) & IORESOURCE_MEM) {      /* ICH4 and Nforce */
2649                 chip->mmio = 1;
2650                 chip->addr = pci_resource_start(pci, 2);
2651                 chip->remap_addr = ioremap_nocache(chip->addr,
2652                                                    pci_resource_len(pci, 2));
2653                 if (chip->remap_addr == NULL) {
2654                         snd_printk("AC'97 space ioremap problem\n");
2655                         snd_intel8x0_free(chip);
2656                         return -EIO;
2657                 }
2658         } else {
2659                 chip->addr = pci_resource_start(pci, 0);
2660         }
2661         if (pci_resource_flags(pci, 3) & IORESOURCE_MEM) {      /* ICH4 */
2662                 chip->bm_mmio = 1;
2663                 chip->bmaddr = pci_resource_start(pci, 3);
2664                 chip->remap_bmaddr = ioremap_nocache(chip->bmaddr,
2665                                                      pci_resource_len(pci, 3));
2666                 if (chip->remap_bmaddr == NULL) {
2667                         snd_printk("Controller space ioremap problem\n");
2668                         snd_intel8x0_free(chip);
2669                         return -EIO;
2670                 }
2671         } else {
2672                 chip->bmaddr = pci_resource_start(pci, 1);
2673         }
2674
2675  port_inited:
2676         if (request_irq(pci->irq, snd_intel8x0_interrupt, SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip)) {
2677                 snd_printk("unable to grab IRQ %d\n", pci->irq);
2678                 snd_intel8x0_free(chip);
2679                 return -EBUSY;
2680         }
2681         chip->irq = pci->irq;
2682         pci_set_master(pci);
2683         synchronize_irq(chip->irq);
2684
2685         chip->bdbars_count = bdbars[device_type];
2686
2687         /* initialize offsets */
2688         switch (device_type) {
2689         case DEVICE_NFORCE:
2690                 tbl = nforce_regs;
2691                 break;
2692         case DEVICE_ALI:
2693                 tbl = ali_regs;
2694                 break;
2695         default:
2696                 tbl = intel_regs;
2697                 break;
2698         }
2699         for (i = 0; i < chip->bdbars_count; i++) {
2700                 ichdev = &chip->ichd[i];
2701                 ichdev->ichd = i;
2702                 ichdev->reg_offset = tbl[i].offset;
2703                 ichdev->int_sta_mask = tbl[i].int_sta_mask;
2704                 if (device_type == DEVICE_SIS) {
2705                         /* SiS 7012 swaps the registers */
2706                         ichdev->roff_sr = ICH_REG_OFF_PICB;
2707                         ichdev->roff_picb = ICH_REG_OFF_SR;
2708                 } else {
2709                         ichdev->roff_sr = ICH_REG_OFF_SR;
2710                         ichdev->roff_picb = ICH_REG_OFF_PICB;
2711                 }
2712                 if (device_type == DEVICE_ALI)
2713                         ichdev->ali_slot = (ichdev->reg_offset - 0x40) / 0x10;
2714                 /* SIS7012 handles the pcm data in bytes, others are in samples */
2715                 ichdev->pos_shift = (device_type == DEVICE_SIS) ? 0 : 1;
2716         }
2717
2718         /* allocate buffer descriptor lists */
2719         /* the start of each lists must be aligned to 8 bytes */
2720         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2721                                 chip->bdbars_count * sizeof(u32) * ICH_MAX_FRAGS * 2,
2722                                 &chip->bdbars) < 0) {
2723                 snd_intel8x0_free(chip);
2724                 snd_printk(KERN_ERR "intel8x0: cannot allocate buffer descriptors\n");
2725                 return -ENOMEM;
2726         }
2727         /* tables must be aligned to 8 bytes here, but the kernel pages
2728            are much bigger, so we don't care (on i386) */
2729         /* workaround for 440MX */
2730         if (chip->fix_nocache)
2731                 fill_nocache(chip->bdbars.area, chip->bdbars.bytes, 1);
2732         int_sta_masks = 0;
2733         for (i = 0; i < chip->bdbars_count; i++) {
2734                 ichdev = &chip->ichd[i];
2735                 ichdev->bdbar = ((u32 *)chip->bdbars.area) + (i * ICH_MAX_FRAGS * 2);
2736                 ichdev->bdbar_addr = chip->bdbars.addr + (i * sizeof(u32) * ICH_MAX_FRAGS * 2);
2737                 int_sta_masks |= ichdev->int_sta_mask;
2738         }
2739         chip->int_sta_reg = device_type == DEVICE_ALI ? ICH_REG_ALI_INTERRUPTSR : ICH_REG_GLOB_STA;
2740         chip->int_sta_mask = int_sta_masks;
2741
2742         if ((err = snd_intel8x0_chip_init(chip, 1)) < 0) {
2743                 snd_intel8x0_free(chip);
2744                 return err;
2745         }
2746
2747         snd_card_set_pm_callback(card, intel8x0_suspend, intel8x0_resume, chip);
2748
2749         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2750                 snd_intel8x0_free(chip);
2751                 return err;
2752         }
2753
2754         snd_card_set_dev(card, &pci->dev);
2755
2756         *r_intel8x0 = chip;
2757         return 0;
2758 }
2759
2760 static struct shortname_table {
2761         unsigned int id;
2762         const char *s;
2763 } shortnames[] __devinitdata = {
2764         { PCI_DEVICE_ID_INTEL_82801AA_5, "Intel 82801AA-ICH" },
2765         { PCI_DEVICE_ID_INTEL_82801AB_5, "Intel 82901AB-ICH0" },
2766         { PCI_DEVICE_ID_INTEL_82801BA_4, "Intel 82801BA-ICH2" },
2767         { PCI_DEVICE_ID_INTEL_440MX, "Intel 440MX" },
2768         { PCI_DEVICE_ID_INTEL_82801CA_5, "Intel 82801CA-ICH3" },
2769         { PCI_DEVICE_ID_INTEL_82801DB_5, "Intel 82801DB-ICH4" },
2770         { PCI_DEVICE_ID_INTEL_82801EB_5, "Intel ICH5" },
2771         { PCI_DEVICE_ID_INTEL_ESB_5, "Intel 6300ESB" },
2772         { PCI_DEVICE_ID_INTEL_ICH6_18, "Intel ICH6" },
2773         { PCI_DEVICE_ID_INTEL_ICH7_20, "Intel ICH7" },
2774         { PCI_DEVICE_ID_INTEL_ESB2_14, "Intel ESB2" },
2775         { PCI_DEVICE_ID_SI_7012, "SiS SI7012" },
2776         { PCI_DEVICE_ID_NVIDIA_MCP1_AUDIO, "NVidia nForce" },
2777         { PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO, "NVidia nForce2" },
2778         { PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO, "NVidia nForce3" },
2779         { PCI_DEVICE_ID_NVIDIA_CK8S_AUDIO, "NVidia CK8S" },
2780         { PCI_DEVICE_ID_NVIDIA_CK804_AUDIO, "NVidia CK804" },
2781         { PCI_DEVICE_ID_NVIDIA_CK8_AUDIO, "NVidia CK8" },
2782         { 0x003a, "NVidia MCP04" },
2783         { 0x746d, "AMD AMD8111" },
2784         { 0x7445, "AMD AMD768" },
2785         { 0x5455, "ALi M5455" },
2786         { 0, NULL },
2787 };
2788
2789 static int __devinit snd_intel8x0_probe(struct pci_dev *pci,
2790                                         const struct pci_device_id *pci_id)
2791 {
2792         snd_card_t *card;
2793         intel8x0_t *chip;
2794         int err;
2795         struct shortname_table *name;
2796
2797         card = snd_card_new(index, id, THIS_MODULE, 0);
2798         if (card == NULL)
2799                 return -ENOMEM;
2800
2801         switch (pci_id->driver_data) {
2802         case DEVICE_NFORCE:
2803                 strcpy(card->driver, "NFORCE");
2804                 break;
2805         case DEVICE_INTEL_ICH4:
2806                 strcpy(card->driver, "ICH4");
2807                 break;
2808         default:
2809                 strcpy(card->driver, "ICH");
2810                 break;
2811         }
2812
2813         strcpy(card->shortname, "Intel ICH");
2814         for (name = shortnames; name->id; name++) {
2815                 if (pci->device == name->id) {
2816                         strcpy(card->shortname, name->s);
2817                         break;
2818                 }
2819         }
2820
2821         if ((err = snd_intel8x0_create(card, pci, pci_id->driver_data,
2822                                        buggy_semaphore, &chip)) < 0) {
2823                 snd_card_free(card);
2824                 return err;
2825         }
2826         if (buggy_irq)
2827                 chip->buggy_irq = 1;
2828         if (xbox)
2829                 chip->xbox = 1;
2830
2831         if ((err = snd_intel8x0_mixer(chip, ac97_clock, ac97_quirk)) < 0) {
2832                 snd_card_free(card);
2833                 return err;
2834         }
2835         if ((err = snd_intel8x0_pcm(chip)) < 0) {
2836                 snd_card_free(card);
2837                 return err;
2838         }
2839         
2840         snd_intel8x0_proc_init(chip);
2841
2842         snprintf(card->longname, sizeof(card->longname),
2843                  "%s with %s at %#lx, irq %i", card->shortname,
2844                  snd_ac97_get_short_name(chip->ac97[0]), chip->addr, chip->irq);
2845
2846         if (! ac97_clock)
2847                 intel8x0_measure_ac97_clock(chip);
2848
2849         if ((err = snd_card_register(card)) < 0) {
2850                 snd_card_free(card);
2851                 return err;
2852         }
2853         pci_set_drvdata(pci, card);
2854         return 0;
2855 }
2856
2857 static void __devexit snd_intel8x0_remove(struct pci_dev *pci)
2858 {
2859         snd_card_free(pci_get_drvdata(pci));
2860         pci_set_drvdata(pci, NULL);
2861 }
2862
2863 static struct pci_driver driver = {
2864         .name = "Intel ICH",
2865         .owner = THIS_MODULE,
2866         .id_table = snd_intel8x0_ids,
2867         .probe = snd_intel8x0_probe,
2868         .remove = __devexit_p(snd_intel8x0_remove),
2869         SND_PCI_PM_CALLBACKS
2870 };
2871
2872
2873 static int __init alsa_card_intel8x0_init(void)
2874 {
2875         return pci_register_driver(&driver);
2876 }
2877
2878 static void __exit alsa_card_intel8x0_exit(void)
2879 {
2880         pci_unregister_driver(&driver);
2881 }
2882
2883 module_init(alsa_card_intel8x0_init)
2884 module_exit(alsa_card_intel8x0_exit)