blob: c5c4ec64a156bc3790a584acfc9c05a17510327b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
46MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
47MODULE_DESCRIPTION("Intel 82801AA,82901AB,i810,i820,i830,i840,i845,MX440; SiS 7012; Ali 5455");
48MODULE_LICENSE("GPL");
49MODULE_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},"
Jason Gastonc4c8ea92005-04-16 15:24:43 -070058 "{Intel,ESB2},"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 "{Intel,MX440},"
60 "{SiS,SI7012},"
61 "{NVidia,nForce Audio},"
62 "{NVidia,nForce2 Audio},"
63 "{AMD,AMD768},"
64 "{AMD,AMD8111},"
65 "{ALI,M5455}}");
66
Clemens Ladischb7fe4622005-10-04 08:46:51 +020067static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
68static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
69static int ac97_clock = 0;
70static char *ac97_quirk;
71static int buggy_semaphore;
Takashi Iwaibeef08a2005-10-27 20:55:38 +020072static int buggy_irq = -1; /* auto-check */
Clemens Ladischb7fe4622005-10-04 08:46:51 +020073static int xbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Clemens Ladischb7fe4622005-10-04 08:46:51 +020075module_param(index, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076MODULE_PARM_DESC(index, "Index value for Intel i8x0 soundcard.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020077module_param(id, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078MODULE_PARM_DESC(id, "ID string for Intel i8x0 soundcard.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020079module_param(ac97_clock, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (0 = auto-detect).");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020081module_param(ac97_quirk, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020083module_param(buggy_semaphore, bool, 0444);
Takashi Iwaia06147d2005-09-08 19:54:17 +020084MODULE_PARM_DESC(buggy_semaphore, "Enable workaround for hardwares with problematic codec semaphores.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020085module_param(buggy_irq, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086MODULE_PARM_DESC(buggy_irq, "Enable workaround for buggy interrupts on some motherboards.");
Clemens Ladischb7fe4622005-10-04 08:46:51 +020087module_param(xbox, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088MODULE_PARM_DESC(xbox, "Set to 1 for Xbox, if you have problems with the AC'97 codec detection.");
89
Takashi Iwai2b3e5842005-10-06 13:47:23 +020090/* just for backward compatibility */
91static int enable;
Takashi Iwai698444f2005-10-20 16:53:49 +020092module_param(enable, bool, 0444);
Takashi Iwai2b3e5842005-10-06 13:47:23 +020093static int joystick;
94module_param(joystick, int, 0444);
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
97 * Direct registers
98 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099enum { 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) \
104enum { \
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 */
115DEFINE_REGSET(OFF, 0); /* offset */
116DEFINE_REGSET(PI, 0x00); /* PCM in */
117DEFINE_REGSET(PO, 0x10); /* PCM out */
118DEFINE_REGSET(MC, 0x20); /* Mic in */
119
120/* ICH4 busmaster blocks */
121DEFINE_REGSET(MC2, 0x40); /* Mic in 2 */
122DEFINE_REGSET(PI2, 0x50); /* PCM in 2 */
123DEFINE_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 */
216DEFINE_REGSET(AL_PI, 0x40); /* ALi PCM in */
217DEFINE_REGSET(AL_PO, 0x50); /* Ali PCM out */
218DEFINE_REGSET(AL_MC, 0x60); /* Ali Mic in */
219DEFINE_REGSET(AL_CDC_SPO, 0x70); /* Ali Codec SPDIF out */
220DEFINE_REGSET(AL_CENTER, 0x80); /* Ali center out */
221DEFINE_REGSET(AL_LFE, 0x90); /* Ali center out */
222DEFINE_REGSET(AL_CLR_SPI, 0xa0); /* Ali Controller SPDIF in */
223DEFINE_REGSET(AL_CLR_SPO, 0xb0); /* Ali Controller SPDIF out */
224DEFINE_REGSET(AL_I2S, 0xc0); /* Ali I2S in */
225DEFINE_REGSET(AL_PI2, 0xd0); /* Ali PCM2 in */
226DEFINE_REGSET(AL_MC2, 0xe0); /* Ali Mic2 in */
227
228enum {
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)
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100275#define ALI_INT_MASK (ALI_INT_SPDIFOUT|ALI_INT_CODECSPDIFOUT|\
276 ALI_INT_MICIN|ALI_INT_PCMOUT|ALI_INT_PCMIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278#define ICH_ALI_SC_RESET (1<<31) /* master reset */
279#define ICH_ALI_SC_AC97_DBL (1<<30)
280#define ICH_ALI_SC_CODEC_SPDF (3<<20) /* 1=7/8, 2=6/9, 3=10/11 */
281#define ICH_ALI_SC_IN_BITS (3<<18)
282#define ICH_ALI_SC_OUT_BITS (3<<16)
283#define ICH_ALI_SC_6CH_CFG (3<<14)
284#define ICH_ALI_SC_PCM_4 (1<<8)
285#define ICH_ALI_SC_PCM_6 (2<<8)
286#define ICH_ALI_SC_PCM_246_MASK (3<<8)
287
288#define ICH_ALI_SS_SEC_ID (3<<5)
289#define ICH_ALI_SS_PRI_ID (3<<3)
290
291#define ICH_ALI_IF_AC97SP (1<<21)
292#define ICH_ALI_IF_MC (1<<20)
293#define ICH_ALI_IF_PI (1<<19)
294#define ICH_ALI_IF_MC2 (1<<18)
295#define ICH_ALI_IF_PI2 (1<<17)
296#define ICH_ALI_IF_LINE_SRC (1<<15) /* 0/1 = slot 3/6 */
297#define ICH_ALI_IF_MIC_SRC (1<<14) /* 0/1 = slot 3/6 */
298#define ICH_ALI_IF_SPDF_SRC (3<<12) /* 00 = PCM, 01 = AC97-in, 10 = spdif-in, 11 = i2s */
299#define ICH_ALI_IF_AC97_OUT (3<<8) /* 00 = PCM, 10 = spdif-in, 11 = i2s */
300#define ICH_ALI_IF_PO_SPDF (1<<3)
301#define ICH_ALI_IF_PO (1<<1)
302
303/*
304 *
305 */
306
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100307enum {
308 ICHD_PCMIN,
309 ICHD_PCMOUT,
310 ICHD_MIC,
311 ICHD_MIC2,
312 ICHD_PCM2IN,
313 ICHD_SPBAR,
314 ICHD_LAST = ICHD_SPBAR
315};
316enum {
317 NVD_PCMIN,
318 NVD_PCMOUT,
319 NVD_MIC,
320 NVD_SPBAR,
321 NVD_LAST = NVD_SPBAR
322};
323enum {
324 ALID_PCMIN,
325 ALID_PCMOUT,
326 ALID_MIC,
327 ALID_AC97SPDIFOUT,
328 ALID_SPDIFIN,
329 ALID_SPDIFOUT,
330 ALID_LAST = ALID_SPDIFOUT
331};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100333#define get_ichdev(substream) (substream->runtime->private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100335struct ichdev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 unsigned int ichd; /* ich device number */
337 unsigned long reg_offset; /* offset to bmaddr */
338 u32 *bdbar; /* CPU address (32bit) */
339 unsigned int bdbar_addr; /* PCI bus address (32bit) */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100340 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 unsigned int physbuf; /* physical address (32bit) */
342 unsigned int size;
343 unsigned int fragsize;
344 unsigned int fragsize1;
345 unsigned int position;
346 unsigned int pos_shift;
347 int frags;
348 int lvi;
349 int lvi_frag;
350 int civ;
351 int ack;
352 int ack_reload;
353 unsigned int ack_bit;
354 unsigned int roff_sr;
355 unsigned int roff_picb;
356 unsigned int int_sta_mask; /* interrupt status mask */
357 unsigned int ali_slot; /* ALI DMA slot */
358 struct ac97_pcm *pcm;
359 int pcm_open_flag;
360 unsigned int page_attr_changed: 1;
Takashi Iwai1cfe43d2005-08-16 16:52:24 +0200361 unsigned int suspended: 1;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100362};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100364struct intel8x0 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 unsigned int device_type;
366
367 int irq;
368
369 unsigned int mmio;
370 unsigned long addr;
371 void __iomem *remap_addr;
372 unsigned int bm_mmio;
373 unsigned long bmaddr;
374 void __iomem *remap_bmaddr;
375
376 struct pci_dev *pci;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100377 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 int pcm_devs;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100380 struct snd_pcm *pcm[6];
381 struct ichdev ichd[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 unsigned multi4: 1,
384 multi6: 1,
385 dra: 1,
386 smp20bit: 1;
387 unsigned in_ac97_init: 1,
388 in_sdin_init: 1;
389 unsigned in_measurement: 1; /* during ac97 clock measurement */
390 unsigned fix_nocache: 1; /* workaround for 440MX */
391 unsigned buggy_irq: 1; /* workaround for buggy mobos */
392 unsigned xbox: 1; /* workaround for Xbox AC'97 detection */
Takashi Iwaia06147d2005-09-08 19:54:17 +0200393 unsigned buggy_semaphore: 1; /* workaround for buggy codec semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 int spdif_idx; /* SPDIF BAR index; *_SPBAR or -1 if use PCMOUT */
Takashi Iwai52b72382005-06-30 13:47:06 +0200396 unsigned int sdm_saved; /* SDM reg value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100398 struct snd_ac97_bus *ac97_bus;
399 struct snd_ac97 *ac97[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 unsigned int ac97_sdin[3];
401
402 spinlock_t reg_lock;
403
404 u32 bdbars_count;
405 struct snd_dma_buffer bdbars;
406 u32 int_sta_reg; /* interrupt status register */
407 u32 int_sta_mask; /* interrupt status mask */
408};
409
410static struct pci_device_id snd_intel8x0_ids[] = {
411 { 0x8086, 0x2415, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801AA */
412 { 0x8086, 0x2425, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82901AB */
413 { 0x8086, 0x2445, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801BA */
414 { 0x8086, 0x2485, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH3 */
415 { 0x8086, 0x24c5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH4 */
416 { 0x8086, 0x24d5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH5 */
417 { 0x8086, 0x25a6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ESB */
418 { 0x8086, 0x266e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH6 */
419 { 0x8086, 0x27de, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH7 */
Jason Gastonc4c8ea92005-04-16 15:24:43 -0700420 { 0x8086, 0x2698, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ESB2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 { 0x8086, 0x7195, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 440MX */
422 { 0x1039, 0x7012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_SIS }, /* SI7012 */
423 { 0x10de, 0x01b1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE */
424 { 0x10de, 0x003a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* MCP04 */
425 { 0x10de, 0x006a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE2 */
426 { 0x10de, 0x0059, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* CK804 */
427 { 0x10de, 0x008a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* CK8 */
428 { 0x10de, 0x00da, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE3 */
429 { 0x10de, 0x00ea, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* CK8S */
430 { 0x1022, 0x746d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD8111 */
431 { 0x1022, 0x7445, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD768 */
432 { 0x10b9, 0x5455, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALI }, /* Ali5455 */
433 { 0, }
434};
435
436MODULE_DEVICE_TABLE(pci, snd_intel8x0_ids);
437
438/*
439 * Lowlevel I/O - busmaster
440 */
441
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100442static u8 igetbyte(struct intel8x0 *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 if (chip->bm_mmio)
445 return readb(chip->remap_bmaddr + offset);
446 else
447 return inb(chip->bmaddr + offset);
448}
449
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100450static u16 igetword(struct intel8x0 *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 if (chip->bm_mmio)
453 return readw(chip->remap_bmaddr + offset);
454 else
455 return inw(chip->bmaddr + offset);
456}
457
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100458static u32 igetdword(struct intel8x0 *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 if (chip->bm_mmio)
461 return readl(chip->remap_bmaddr + offset);
462 else
463 return inl(chip->bmaddr + offset);
464}
465
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100466static void iputbyte(struct intel8x0 *chip, u32 offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 if (chip->bm_mmio)
469 writeb(val, chip->remap_bmaddr + offset);
470 else
471 outb(val, chip->bmaddr + offset);
472}
473
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100474static void iputword(struct intel8x0 *chip, u32 offset, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 if (chip->bm_mmio)
477 writew(val, chip->remap_bmaddr + offset);
478 else
479 outw(val, chip->bmaddr + offset);
480}
481
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100482static void iputdword(struct intel8x0 *chip, u32 offset, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 if (chip->bm_mmio)
485 writel(val, chip->remap_bmaddr + offset);
486 else
487 outl(val, chip->bmaddr + offset);
488}
489
490/*
491 * Lowlevel I/O - AC'97 registers
492 */
493
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100494static u16 iagetword(struct intel8x0 *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 if (chip->mmio)
497 return readw(chip->remap_addr + offset);
498 else
499 return inw(chip->addr + offset);
500}
501
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100502static void iaputword(struct intel8x0 *chip, u32 offset, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 if (chip->mmio)
505 writew(val, chip->remap_addr + offset);
506 else
507 outw(val, chip->addr + offset);
508}
509
510/*
511 * Basic I/O
512 */
513
514/*
515 * access to AC97 codec via normal i/o (for ICH and SIS7012)
516 */
517
518/* return the GLOB_STA bit for the corresponding codec */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100519static unsigned int get_ich_codec_bit(struct intel8x0 *chip, unsigned int codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 static unsigned int codec_bit[3] = {
522 ICH_PCR, ICH_SCR, ICH_TCR
523 };
524 snd_assert(codec < 3, return ICH_PCR);
525 if (chip->device_type == DEVICE_INTEL_ICH4)
526 codec = chip->ac97_sdin[codec];
527 return codec_bit[codec];
528}
529
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100530static int snd_intel8x0_codec_semaphore(struct intel8x0 *chip, unsigned int codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 int time;
533
534 if (codec > 2)
535 return -EIO;
536 if (chip->in_sdin_init) {
537 /* we don't know the ready bit assignment at the moment */
538 /* so we check any */
539 codec = ICH_PCR | ICH_SCR | ICH_TCR;
540 } else {
541 codec = get_ich_codec_bit(chip, codec);
542 }
543
544 /* codec ready ? */
545 if ((igetdword(chip, ICHREG(GLOB_STA)) & codec) == 0)
546 return -EIO;
547
Takashi Iwaia06147d2005-09-08 19:54:17 +0200548 if (chip->buggy_semaphore)
549 return 0; /* just ignore ... */
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* Anyone holding a semaphore for 1 msec should be shot... */
552 time = 100;
553 do {
554 if (!(igetbyte(chip, ICHREG(ACC_SEMA)) & ICH_CAS))
555 return 0;
556 udelay(10);
557 } while (time--);
558
559 /* access to some forbidden (non existant) ac97 registers will not
560 * reset the semaphore. So even if you don't get the semaphore, still
561 * continue the access. We don't need the semaphore anyway. */
Takashi Iwai99b359b2005-10-20 18:26:44 +0200562 snd_printk(KERN_ERR "codec_semaphore: semaphore is not ready [0x%x][0x%x]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 igetbyte(chip, ICHREG(ACC_SEMA)), igetdword(chip, ICHREG(GLOB_STA)));
564 iagetword(chip, 0); /* clear semaphore flag */
565 /* I don't care about the semaphore */
566 return -EBUSY;
567}
568
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100569static void snd_intel8x0_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 unsigned short reg,
571 unsigned short val)
572{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100573 struct intel8x0 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (snd_intel8x0_codec_semaphore(chip, ac97->num) < 0) {
576 if (! chip->in_ac97_init)
Takashi Iwai99b359b2005-10-20 18:26:44 +0200577 snd_printk(KERN_ERR "codec_write %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579 iaputword(chip, reg + ac97->num * 0x80, val);
580}
581
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100582static unsigned short snd_intel8x0_codec_read(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 unsigned short reg)
584{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100585 struct intel8x0 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 unsigned short res;
587 unsigned int tmp;
588
589 if (snd_intel8x0_codec_semaphore(chip, ac97->num) < 0) {
590 if (! chip->in_ac97_init)
Takashi Iwai99b359b2005-10-20 18:26:44 +0200591 snd_printk(KERN_ERR "codec_read %d: semaphore is not ready for register 0x%x\n", ac97->num, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 res = 0xffff;
593 } else {
594 res = iagetword(chip, reg + ac97->num * 0x80);
595 if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
596 /* reset RCS and preserve other R/WC bits */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100597 iputdword(chip, ICHREG(GLOB_STA), tmp &
598 ~(ICH_SRI|ICH_PRI|ICH_TRI|ICH_GSCI));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (! chip->in_ac97_init)
Takashi Iwai99b359b2005-10-20 18:26:44 +0200600 snd_printk(KERN_ERR "codec_read %d: read timeout for register 0x%x\n", ac97->num, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 res = 0xffff;
602 }
603 }
604 return res;
605}
606
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100607static void snd_intel8x0_codec_read_test(struct intel8x0 *chip, unsigned int codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 unsigned int tmp;
610
611 if (snd_intel8x0_codec_semaphore(chip, codec) >= 0) {
612 iagetword(chip, codec * 0x80);
613 if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
614 /* reset RCS and preserve other R/WC bits */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100615 iputdword(chip, ICHREG(GLOB_STA), tmp &
616 ~(ICH_SRI|ICH_PRI|ICH_TRI|ICH_GSCI));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 }
619}
620
621/*
622 * access to AC97 for Ali5455
623 */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100624static int snd_intel8x0_ali_codec_ready(struct intel8x0 *chip, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 int count = 0;
627 for (count = 0; count < 0x7f; count++) {
628 int val = igetbyte(chip, ICHREG(ALI_CSPSR));
629 if (val & mask)
630 return 0;
631 }
Takashi Iwai79ba34b2005-10-05 19:47:41 +0200632 if (! chip->in_ac97_init)
633 snd_printd(KERN_WARNING "intel8x0: AC97 codec ready timeout.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return -EBUSY;
635}
636
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100637static int snd_intel8x0_ali_codec_semaphore(struct intel8x0 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 int time = 100;
Takashi Iwai79ba34b2005-10-05 19:47:41 +0200640 if (chip->buggy_semaphore)
641 return 0; /* just ignore ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 while (time-- && (igetdword(chip, ICHREG(ALI_CAS)) & ALI_CAS_SEM_BUSY))
643 udelay(1);
Takashi Iwai79ba34b2005-10-05 19:47:41 +0200644 if (! time && ! chip->in_ac97_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 snd_printk(KERN_WARNING "ali_codec_semaphore timeout\n");
646 return snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_CODEC_READY);
647}
648
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100649static unsigned short snd_intel8x0_ali_codec_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100651 struct intel8x0 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 unsigned short data = 0xffff;
653
654 if (snd_intel8x0_ali_codec_semaphore(chip))
655 goto __err;
656 reg |= ALI_CPR_ADDR_READ;
657 if (ac97->num)
658 reg |= ALI_CPR_ADDR_SECONDARY;
659 iputword(chip, ICHREG(ALI_CPR_ADDR), reg);
660 if (snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_READ_OK))
661 goto __err;
662 data = igetword(chip, ICHREG(ALI_SPR));
663 __err:
664 return data;
665}
666
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100667static void snd_intel8x0_ali_codec_write(struct snd_ac97 *ac97, unsigned short reg,
668 unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100670 struct intel8x0 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 if (snd_intel8x0_ali_codec_semaphore(chip))
673 return;
674 iputword(chip, ICHREG(ALI_CPR), val);
675 if (ac97->num)
676 reg |= ALI_CPR_ADDR_SECONDARY;
677 iputword(chip, ICHREG(ALI_CPR_ADDR), reg);
678 snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_WRITE_OK);
679}
680
681
682/*
683 * DMA I/O
684 */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100685static void snd_intel8x0_setup_periods(struct intel8x0 *chip, struct ichdev *ichdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 int idx;
688 u32 *bdbar = ichdev->bdbar;
689 unsigned long port = ichdev->reg_offset;
690
691 iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
692 if (ichdev->size == ichdev->fragsize) {
693 ichdev->ack_reload = ichdev->ack = 2;
694 ichdev->fragsize1 = ichdev->fragsize >> 1;
695 for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 4) {
696 bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf);
697 bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
698 ichdev->fragsize1 >> ichdev->pos_shift);
699 bdbar[idx + 2] = cpu_to_le32(ichdev->physbuf + (ichdev->size >> 1));
700 bdbar[idx + 3] = cpu_to_le32(0x80000000 | /* interrupt on completion */
701 ichdev->fragsize1 >> ichdev->pos_shift);
702 }
703 ichdev->frags = 2;
704 } else {
705 ichdev->ack_reload = ichdev->ack = 1;
706 ichdev->fragsize1 = ichdev->fragsize;
707 for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 2) {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100708 bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf +
709 (((idx >> 1) * ichdev->fragsize) %
710 ichdev->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
712 ichdev->fragsize >> ichdev->pos_shift);
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100713#if 0
714 printk("bdbar[%i] = 0x%x [0x%x]\n",
715 idx + 0, bdbar[idx + 0], bdbar[idx + 1]);
716#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 ichdev->frags = ichdev->size / ichdev->fragsize;
719 }
720 iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi = ICH_REG_LVI_MASK);
721 ichdev->civ = 0;
722 iputbyte(chip, port + ICH_REG_OFF_CIV, 0);
723 ichdev->lvi_frag = ICH_REG_LVI_MASK % ichdev->frags;
724 ichdev->position = 0;
725#if 0
726 printk("lvi_frag = %i, frags = %i, period_size = 0x%x, period_size1 = 0x%x\n",
727 ichdev->lvi_frag, ichdev->frags, ichdev->fragsize, ichdev->fragsize1);
728#endif
729 /* clear interrupts */
730 iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
731}
732
733#ifdef __i386__
734/*
735 * Intel 82443MX running a 100MHz processor system bus has a hardware bug,
736 * which aborts PCI busmaster for audio transfer. A workaround is to set
737 * the pages as non-cached. For details, see the errata in
738 * http://www.intel.com/design/chipsets/specupdt/245051.htm
739 */
740static void fill_nocache(void *buf, int size, int nocache)
741{
742 size = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
743 change_page_attr(virt_to_page(buf), size, nocache ? PAGE_KERNEL_NOCACHE : PAGE_KERNEL);
744 global_flush_tlb();
745}
746#else
747#define fill_nocache(buf,size,nocache)
748#endif
749
750/*
751 * Interrupt handler
752 */
753
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100754static inline void snd_intel8x0_update(struct intel8x0 *chip, struct ichdev *ichdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 unsigned long port = ichdev->reg_offset;
757 int status, civ, i, step;
758 int ack = 0;
759
760 spin_lock(&chip->reg_lock);
761 status = igetbyte(chip, port + ichdev->roff_sr);
762 civ = igetbyte(chip, port + ICH_REG_OFF_CIV);
763 if (!(status & ICH_BCIS)) {
764 step = 0;
765 } else if (civ == ichdev->civ) {
766 // snd_printd("civ same %d\n", civ);
767 step = 1;
768 ichdev->civ++;
769 ichdev->civ &= ICH_REG_LVI_MASK;
770 } else {
771 step = civ - ichdev->civ;
772 if (step < 0)
773 step += ICH_REG_LVI_MASK + 1;
774 // if (step != 1)
775 // snd_printd("step = %d, %d -> %d\n", step, ichdev->civ, civ);
776 ichdev->civ = civ;
777 }
778
779 ichdev->position += step * ichdev->fragsize1;
780 if (! chip->in_measurement)
781 ichdev->position %= ichdev->size;
782 ichdev->lvi += step;
783 ichdev->lvi &= ICH_REG_LVI_MASK;
784 iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
785 for (i = 0; i < step; i++) {
786 ichdev->lvi_frag++;
787 ichdev->lvi_frag %= ichdev->frags;
788 ichdev->bdbar[ichdev->lvi * 2] = cpu_to_le32(ichdev->physbuf + ichdev->lvi_frag * ichdev->fragsize1);
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100789#if 0
790 printk("new: bdbar[%i] = 0x%x [0x%x], prefetch = %i, all = 0x%x, 0x%x\n",
791 ichdev->lvi * 2, ichdev->bdbar[ichdev->lvi * 2],
792 ichdev->bdbar[ichdev->lvi * 2 + 1], inb(ICH_REG_OFF_PIV + port),
793 inl(port + 4), inb(port + ICH_REG_OFF_CR));
794#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (--ichdev->ack == 0) {
796 ichdev->ack = ichdev->ack_reload;
797 ack = 1;
798 }
799 }
800 spin_unlock(&chip->reg_lock);
801 if (ack && ichdev->substream) {
802 snd_pcm_period_elapsed(ichdev->substream);
803 }
804 iputbyte(chip, port + ichdev->roff_sr,
805 status & (ICH_FIFOE | ICH_BCIS | ICH_LVBCI));
806}
807
808static irqreturn_t snd_intel8x0_interrupt(int irq, void *dev_id, struct pt_regs *regs)
809{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100810 struct intel8x0 *chip = dev_id;
811 struct ichdev *ichdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unsigned int status;
813 unsigned int i;
814
815 status = igetdword(chip, chip->int_sta_reg);
816 if (status == 0xffffffff) /* we are not yet resumed */
817 return IRQ_NONE;
818
819 if ((status & chip->int_sta_mask) == 0) {
820 if (status) {
821 /* ack */
822 iputdword(chip, chip->int_sta_reg, status);
823 if (! chip->buggy_irq)
824 status = 0;
825 }
826 return IRQ_RETVAL(status);
827 }
828
829 for (i = 0; i < chip->bdbars_count; i++) {
830 ichdev = &chip->ichd[i];
831 if (status & ichdev->int_sta_mask)
832 snd_intel8x0_update(chip, ichdev);
833 }
834
835 /* ack them */
836 iputdword(chip, chip->int_sta_reg, status & chip->int_sta_mask);
837
838 return IRQ_HANDLED;
839}
840
841/*
842 * PCM part
843 */
844
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100845static int snd_intel8x0_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100847 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
848 struct ichdev *ichdev = get_ichdev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 unsigned char val = 0;
850 unsigned long port = ichdev->reg_offset;
851
852 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwai1cfe43d2005-08-16 16:52:24 +0200854 ichdev->suspended = 0;
855 /* fallthru */
856 case SNDRV_PCM_TRIGGER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 val = ICH_IOCE | ICH_STARTBM;
858 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwai1cfe43d2005-08-16 16:52:24 +0200860 ichdev->suspended = 1;
861 /* fallthru */
862 case SNDRV_PCM_TRIGGER_STOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 val = 0;
864 break;
865 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
866 val = ICH_IOCE;
867 break;
868 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
869 val = ICH_IOCE | ICH_STARTBM;
870 break;
871 default:
872 return -EINVAL;
873 }
874 iputbyte(chip, port + ICH_REG_OFF_CR, val);
875 if (cmd == SNDRV_PCM_TRIGGER_STOP) {
876 /* wait until DMA stopped */
877 while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH)) ;
878 /* reset whole DMA things */
879 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
880 }
881 return 0;
882}
883
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100884static int snd_intel8x0_ali_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100886 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
887 struct ichdev *ichdev = get_ichdev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 unsigned long port = ichdev->reg_offset;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100889 static int fiforeg[] = {
890 ICHREG(ALI_FIFOCR1), ICHREG(ALI_FIFOCR2), ICHREG(ALI_FIFOCR3)
891 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 unsigned int val, fifo;
893
894 val = igetdword(chip, ICHREG(ALI_DMACR));
895 switch (cmd) {
Takashi Iwai1cfe43d2005-08-16 16:52:24 +0200896 case SNDRV_PCM_TRIGGER_RESUME:
897 ichdev->suspended = 0;
898 /* fallthru */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 case SNDRV_PCM_TRIGGER_START:
900 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
902 /* clear FIFO for synchronization of channels */
903 fifo = igetdword(chip, fiforeg[ichdev->ali_slot / 4]);
904 fifo &= ~(0xff << (ichdev->ali_slot % 4));
905 fifo |= 0x83 << (ichdev->ali_slot % 4);
906 iputdword(chip, fiforeg[ichdev->ali_slot / 4], fifo);
907 }
908 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE);
909 val &= ~(1 << (ichdev->ali_slot + 16)); /* clear PAUSE flag */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100910 /* start DMA */
911 iputdword(chip, ICHREG(ALI_DMACR), val | (1 << ichdev->ali_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 break;
Takashi Iwai1cfe43d2005-08-16 16:52:24 +0200913 case SNDRV_PCM_TRIGGER_SUSPEND:
914 ichdev->suspended = 1;
915 /* fallthru */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 case SNDRV_PCM_TRIGGER_STOP:
917 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100918 /* pause */
919 iputdword(chip, ICHREG(ALI_DMACR), val | (1 << (ichdev->ali_slot + 16)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 iputbyte(chip, port + ICH_REG_OFF_CR, 0);
921 while (igetbyte(chip, port + ICH_REG_OFF_CR))
922 ;
923 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
924 break;
925 /* reset whole DMA things */
926 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
927 /* clear interrupts */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100928 iputbyte(chip, port + ICH_REG_OFF_SR,
929 igetbyte(chip, port + ICH_REG_OFF_SR) | 0x1e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 iputdword(chip, ICHREG(ALI_INTERRUPTSR),
931 igetdword(chip, ICHREG(ALI_INTERRUPTSR)) & ichdev->int_sta_mask);
932 break;
933 default:
934 return -EINVAL;
935 }
936 return 0;
937}
938
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100939static int snd_intel8x0_hw_params(struct snd_pcm_substream *substream,
940 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100942 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
943 struct ichdev *ichdev = get_ichdev(substream);
944 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int dbl = params_rate(hw_params) > 48000;
946 int err;
947
948 if (chip->fix_nocache && ichdev->page_attr_changed) {
949 fill_nocache(runtime->dma_area, runtime->dma_bytes, 0); /* clear */
950 ichdev->page_attr_changed = 0;
951 }
952 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
953 if (err < 0)
954 return err;
955 if (chip->fix_nocache) {
956 if (runtime->dma_area && ! ichdev->page_attr_changed) {
957 fill_nocache(runtime->dma_area, runtime->dma_bytes, 1);
958 ichdev->page_attr_changed = 1;
959 }
960 }
961 if (ichdev->pcm_open_flag) {
962 snd_ac97_pcm_close(ichdev->pcm);
963 ichdev->pcm_open_flag = 0;
964 }
965 err = snd_ac97_pcm_open(ichdev->pcm, params_rate(hw_params),
966 params_channels(hw_params),
967 ichdev->pcm->r[dbl].slots);
968 if (err >= 0) {
969 ichdev->pcm_open_flag = 1;
970 /* Force SPDIF setting */
971 if (ichdev->ichd == ICHD_PCMOUT && chip->spdif_idx < 0)
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100972 snd_ac97_set_rate(ichdev->pcm->r[0].codec[0], AC97_SPDIF,
973 params_rate(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975 return err;
976}
977
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100978static int snd_intel8x0_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100980 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
981 struct ichdev *ichdev = get_ichdev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 if (ichdev->pcm_open_flag) {
984 snd_ac97_pcm_close(ichdev->pcm);
985 ichdev->pcm_open_flag = 0;
986 }
987 if (chip->fix_nocache && ichdev->page_attr_changed) {
988 fill_nocache(substream->runtime->dma_area, substream->runtime->dma_bytes, 0);
989 ichdev->page_attr_changed = 0;
990 }
991 return snd_pcm_lib_free_pages(substream);
992}
993
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100994static void snd_intel8x0_setup_pcm_out(struct intel8x0 *chip,
995 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 unsigned int cnt;
998 int dbl = runtime->rate > 48000;
Takashi Iwai1cfe43d2005-08-16 16:52:24 +0200999
1000 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 switch (chip->device_type) {
1002 case DEVICE_ALI:
1003 cnt = igetdword(chip, ICHREG(ALI_SCR));
1004 cnt &= ~ICH_ALI_SC_PCM_246_MASK;
1005 if (runtime->channels == 4 || dbl)
1006 cnt |= ICH_ALI_SC_PCM_4;
1007 else if (runtime->channels == 6)
1008 cnt |= ICH_ALI_SC_PCM_6;
1009 iputdword(chip, ICHREG(ALI_SCR), cnt);
1010 break;
1011 case DEVICE_SIS:
1012 cnt = igetdword(chip, ICHREG(GLOB_CNT));
1013 cnt &= ~ICH_SIS_PCM_246_MASK;
1014 if (runtime->channels == 4 || dbl)
1015 cnt |= ICH_SIS_PCM_4;
1016 else if (runtime->channels == 6)
1017 cnt |= ICH_SIS_PCM_6;
1018 iputdword(chip, ICHREG(GLOB_CNT), cnt);
1019 break;
1020 default:
1021 cnt = igetdword(chip, ICHREG(GLOB_CNT));
1022 cnt &= ~(ICH_PCM_246_MASK | ICH_PCM_20BIT);
1023 if (runtime->channels == 4 || dbl)
1024 cnt |= ICH_PCM_4;
1025 else if (runtime->channels == 6)
1026 cnt |= ICH_PCM_6;
1027 if (chip->device_type == DEVICE_NFORCE) {
1028 /* reset to 2ch once to keep the 6 channel data in alignment,
1029 * to start from Front Left always
1030 */
1031 if (cnt & ICH_PCM_246_MASK) {
1032 iputdword(chip, ICHREG(GLOB_CNT), cnt & ~ICH_PCM_246_MASK);
1033 spin_unlock_irq(&chip->reg_lock);
1034 msleep(50); /* grrr... */
1035 spin_lock_irq(&chip->reg_lock);
1036 }
1037 } else if (chip->device_type == DEVICE_INTEL_ICH4) {
1038 if (runtime->sample_bits > 16)
1039 cnt |= ICH_PCM_20BIT;
1040 }
1041 iputdword(chip, ICHREG(GLOB_CNT), cnt);
1042 break;
1043 }
Takashi Iwai1cfe43d2005-08-16 16:52:24 +02001044 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001047static int snd_intel8x0_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001049 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
1050 struct snd_pcm_runtime *runtime = substream->runtime;
1051 struct ichdev *ichdev = get_ichdev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 ichdev->physbuf = runtime->dma_addr;
1054 ichdev->size = snd_pcm_lib_buffer_bytes(substream);
1055 ichdev->fragsize = snd_pcm_lib_period_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (ichdev->ichd == ICHD_PCMOUT) {
1057 snd_intel8x0_setup_pcm_out(chip, runtime);
Takashi Iwai1cfe43d2005-08-16 16:52:24 +02001058 if (chip->device_type == DEVICE_INTEL_ICH4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ichdev->pos_shift = (runtime->sample_bits > 16) ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061 snd_intel8x0_setup_periods(chip, ichdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return 0;
1063}
1064
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001065static snd_pcm_uframes_t snd_intel8x0_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001067 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
1068 struct ichdev *ichdev = get_ichdev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 size_t ptr1, ptr;
1070 int civ, timeout = 100;
1071 unsigned int position;
1072
1073 spin_lock(&chip->reg_lock);
1074 do {
1075 civ = igetbyte(chip, ichdev->reg_offset + ICH_REG_OFF_CIV);
1076 ptr1 = igetword(chip, ichdev->reg_offset + ichdev->roff_picb);
1077 position = ichdev->position;
1078 if (ptr1 == 0) {
1079 udelay(10);
1080 continue;
1081 }
1082 if (civ == igetbyte(chip, ichdev->reg_offset + ICH_REG_OFF_CIV) &&
1083 ptr1 == igetword(chip, ichdev->reg_offset + ichdev->roff_picb))
1084 break;
1085 } while (timeout--);
1086 ptr1 <<= ichdev->pos_shift;
1087 ptr = ichdev->fragsize1 - ptr1;
1088 ptr += position;
1089 spin_unlock(&chip->reg_lock);
1090 if (ptr >= ichdev->size)
1091 return 0;
1092 return bytes_to_frames(substream->runtime, ptr);
1093}
1094
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001095static struct snd_pcm_hardware snd_intel8x0_stream =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1098 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1099 SNDRV_PCM_INFO_MMAP_VALID |
1100 SNDRV_PCM_INFO_PAUSE |
1101 SNDRV_PCM_INFO_RESUME),
1102 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1103 .rates = SNDRV_PCM_RATE_48000,
1104 .rate_min = 48000,
1105 .rate_max = 48000,
1106 .channels_min = 2,
1107 .channels_max = 2,
1108 .buffer_bytes_max = 128 * 1024,
1109 .period_bytes_min = 32,
1110 .period_bytes_max = 128 * 1024,
1111 .periods_min = 1,
1112 .periods_max = 1024,
1113 .fifo_size = 0,
1114};
1115
1116static unsigned int channels4[] = {
1117 2, 4,
1118};
1119
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001120static struct snd_pcm_hw_constraint_list hw_constraints_channels4 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 .count = ARRAY_SIZE(channels4),
1122 .list = channels4,
1123 .mask = 0,
1124};
1125
1126static unsigned int channels6[] = {
1127 2, 4, 6,
1128};
1129
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001130static struct snd_pcm_hw_constraint_list hw_constraints_channels6 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 .count = ARRAY_SIZE(channels6),
1132 .list = channels6,
1133 .mask = 0,
1134};
1135
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001136static int snd_intel8x0_pcm_open(struct snd_pcm_substream *substream, struct ichdev *ichdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001138 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
1139 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 int err;
1141
1142 ichdev->substream = substream;
1143 runtime->hw = snd_intel8x0_stream;
1144 runtime->hw.rates = ichdev->pcm->rates;
1145 snd_pcm_limit_hw_rates(runtime);
1146 if (chip->device_type == DEVICE_SIS) {
1147 runtime->hw.buffer_bytes_max = 64*1024;
1148 runtime->hw.period_bytes_max = 64*1024;
1149 }
1150 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1151 return err;
1152 runtime->private_data = ichdev;
1153 return 0;
1154}
1155
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001156static int snd_intel8x0_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001158 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
1159 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 int err;
1161
1162 err = snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCMOUT]);
1163 if (err < 0)
1164 return err;
1165
1166 if (chip->multi6) {
1167 runtime->hw.channels_max = 6;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001168 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1169 &hw_constraints_channels6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 } else if (chip->multi4) {
1171 runtime->hw.channels_max = 4;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001172 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1173 &hw_constraints_channels4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175 if (chip->dra) {
1176 snd_ac97_pcm_double_rate_rules(runtime);
1177 }
1178 if (chip->smp20bit) {
1179 runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
1180 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 20);
1181 }
1182 return 0;
1183}
1184
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001185static int snd_intel8x0_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001187 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 chip->ichd[ICHD_PCMOUT].substream = NULL;
1190 return 0;
1191}
1192
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001193static int snd_intel8x0_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001195 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCMIN]);
1198}
1199
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001200static int snd_intel8x0_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001202 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 chip->ichd[ICHD_PCMIN].substream = NULL;
1205 return 0;
1206}
1207
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001208static int snd_intel8x0_mic_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001210 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_MIC]);
1213}
1214
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001215static int snd_intel8x0_mic_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001217 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 chip->ichd[ICHD_MIC].substream = NULL;
1220 return 0;
1221}
1222
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001223static int snd_intel8x0_mic2_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001225 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_MIC2]);
1228}
1229
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001230static int snd_intel8x0_mic2_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001232 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 chip->ichd[ICHD_MIC2].substream = NULL;
1235 return 0;
1236}
1237
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001238static int snd_intel8x0_capture2_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001240 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCM2IN]);
1243}
1244
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001245static int snd_intel8x0_capture2_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001247 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 chip->ichd[ICHD_PCM2IN].substream = NULL;
1250 return 0;
1251}
1252
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001253static int snd_intel8x0_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001255 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 int idx = chip->device_type == DEVICE_NFORCE ? NVD_SPBAR : ICHD_SPBAR;
1257
1258 return snd_intel8x0_pcm_open(substream, &chip->ichd[idx]);
1259}
1260
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001261static int snd_intel8x0_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001263 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int idx = chip->device_type == DEVICE_NFORCE ? NVD_SPBAR : ICHD_SPBAR;
1265
1266 chip->ichd[idx].substream = NULL;
1267 return 0;
1268}
1269
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001270static int snd_intel8x0_ali_ac97spdifout_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001272 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 unsigned int val;
1274
1275 spin_lock_irq(&chip->reg_lock);
1276 val = igetdword(chip, ICHREG(ALI_INTERFACECR));
1277 val |= ICH_ALI_IF_AC97SP;
1278 iputdword(chip, ICHREG(ALI_INTERFACECR), val);
1279 /* also needs to set ALI_SC_CODEC_SPDF correctly */
1280 spin_unlock_irq(&chip->reg_lock);
1281
1282 return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_AC97SPDIFOUT]);
1283}
1284
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001285static int snd_intel8x0_ali_ac97spdifout_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001287 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 unsigned int val;
1289
1290 chip->ichd[ALID_AC97SPDIFOUT].substream = NULL;
1291 spin_lock_irq(&chip->reg_lock);
1292 val = igetdword(chip, ICHREG(ALI_INTERFACECR));
1293 val &= ~ICH_ALI_IF_AC97SP;
1294 iputdword(chip, ICHREG(ALI_INTERFACECR), val);
1295 spin_unlock_irq(&chip->reg_lock);
1296
1297 return 0;
1298}
1299
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001300static int snd_intel8x0_ali_spdifin_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001302 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_SPDIFIN]);
1305}
1306
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001307static int snd_intel8x0_ali_spdifin_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001309 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 chip->ichd[ALID_SPDIFIN].substream = NULL;
1312 return 0;
1313}
1314
1315#if 0 // NYI
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001316static int snd_intel8x0_ali_spdifout_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001318 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_SPDIFOUT]);
1321}
1322
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001323static int snd_intel8x0_ali_spdifout_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001325 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 chip->ichd[ALID_SPDIFOUT].substream = NULL;
1328 return 0;
1329}
1330#endif
1331
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001332static struct snd_pcm_ops snd_intel8x0_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 .open = snd_intel8x0_playback_open,
1334 .close = snd_intel8x0_playback_close,
1335 .ioctl = snd_pcm_lib_ioctl,
1336 .hw_params = snd_intel8x0_hw_params,
1337 .hw_free = snd_intel8x0_hw_free,
1338 .prepare = snd_intel8x0_pcm_prepare,
1339 .trigger = snd_intel8x0_pcm_trigger,
1340 .pointer = snd_intel8x0_pcm_pointer,
1341};
1342
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001343static struct snd_pcm_ops snd_intel8x0_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 .open = snd_intel8x0_capture_open,
1345 .close = snd_intel8x0_capture_close,
1346 .ioctl = snd_pcm_lib_ioctl,
1347 .hw_params = snd_intel8x0_hw_params,
1348 .hw_free = snd_intel8x0_hw_free,
1349 .prepare = snd_intel8x0_pcm_prepare,
1350 .trigger = snd_intel8x0_pcm_trigger,
1351 .pointer = snd_intel8x0_pcm_pointer,
1352};
1353
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001354static struct snd_pcm_ops snd_intel8x0_capture_mic_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 .open = snd_intel8x0_mic_open,
1356 .close = snd_intel8x0_mic_close,
1357 .ioctl = snd_pcm_lib_ioctl,
1358 .hw_params = snd_intel8x0_hw_params,
1359 .hw_free = snd_intel8x0_hw_free,
1360 .prepare = snd_intel8x0_pcm_prepare,
1361 .trigger = snd_intel8x0_pcm_trigger,
1362 .pointer = snd_intel8x0_pcm_pointer,
1363};
1364
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001365static struct snd_pcm_ops snd_intel8x0_capture_mic2_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 .open = snd_intel8x0_mic2_open,
1367 .close = snd_intel8x0_mic2_close,
1368 .ioctl = snd_pcm_lib_ioctl,
1369 .hw_params = snd_intel8x0_hw_params,
1370 .hw_free = snd_intel8x0_hw_free,
1371 .prepare = snd_intel8x0_pcm_prepare,
1372 .trigger = snd_intel8x0_pcm_trigger,
1373 .pointer = snd_intel8x0_pcm_pointer,
1374};
1375
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001376static struct snd_pcm_ops snd_intel8x0_capture2_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 .open = snd_intel8x0_capture2_open,
1378 .close = snd_intel8x0_capture2_close,
1379 .ioctl = snd_pcm_lib_ioctl,
1380 .hw_params = snd_intel8x0_hw_params,
1381 .hw_free = snd_intel8x0_hw_free,
1382 .prepare = snd_intel8x0_pcm_prepare,
1383 .trigger = snd_intel8x0_pcm_trigger,
1384 .pointer = snd_intel8x0_pcm_pointer,
1385};
1386
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001387static struct snd_pcm_ops snd_intel8x0_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 .open = snd_intel8x0_spdif_open,
1389 .close = snd_intel8x0_spdif_close,
1390 .ioctl = snd_pcm_lib_ioctl,
1391 .hw_params = snd_intel8x0_hw_params,
1392 .hw_free = snd_intel8x0_hw_free,
1393 .prepare = snd_intel8x0_pcm_prepare,
1394 .trigger = snd_intel8x0_pcm_trigger,
1395 .pointer = snd_intel8x0_pcm_pointer,
1396};
1397
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001398static struct snd_pcm_ops snd_intel8x0_ali_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 .open = snd_intel8x0_playback_open,
1400 .close = snd_intel8x0_playback_close,
1401 .ioctl = snd_pcm_lib_ioctl,
1402 .hw_params = snd_intel8x0_hw_params,
1403 .hw_free = snd_intel8x0_hw_free,
1404 .prepare = snd_intel8x0_pcm_prepare,
1405 .trigger = snd_intel8x0_ali_trigger,
1406 .pointer = snd_intel8x0_pcm_pointer,
1407};
1408
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001409static struct snd_pcm_ops snd_intel8x0_ali_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 .open = snd_intel8x0_capture_open,
1411 .close = snd_intel8x0_capture_close,
1412 .ioctl = snd_pcm_lib_ioctl,
1413 .hw_params = snd_intel8x0_hw_params,
1414 .hw_free = snd_intel8x0_hw_free,
1415 .prepare = snd_intel8x0_pcm_prepare,
1416 .trigger = snd_intel8x0_ali_trigger,
1417 .pointer = snd_intel8x0_pcm_pointer,
1418};
1419
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001420static struct snd_pcm_ops snd_intel8x0_ali_capture_mic_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 .open = snd_intel8x0_mic_open,
1422 .close = snd_intel8x0_mic_close,
1423 .ioctl = snd_pcm_lib_ioctl,
1424 .hw_params = snd_intel8x0_hw_params,
1425 .hw_free = snd_intel8x0_hw_free,
1426 .prepare = snd_intel8x0_pcm_prepare,
1427 .trigger = snd_intel8x0_ali_trigger,
1428 .pointer = snd_intel8x0_pcm_pointer,
1429};
1430
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001431static struct snd_pcm_ops snd_intel8x0_ali_ac97spdifout_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 .open = snd_intel8x0_ali_ac97spdifout_open,
1433 .close = snd_intel8x0_ali_ac97spdifout_close,
1434 .ioctl = snd_pcm_lib_ioctl,
1435 .hw_params = snd_intel8x0_hw_params,
1436 .hw_free = snd_intel8x0_hw_free,
1437 .prepare = snd_intel8x0_pcm_prepare,
1438 .trigger = snd_intel8x0_ali_trigger,
1439 .pointer = snd_intel8x0_pcm_pointer,
1440};
1441
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001442static struct snd_pcm_ops snd_intel8x0_ali_spdifin_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 .open = snd_intel8x0_ali_spdifin_open,
1444 .close = snd_intel8x0_ali_spdifin_close,
1445 .ioctl = snd_pcm_lib_ioctl,
1446 .hw_params = snd_intel8x0_hw_params,
1447 .hw_free = snd_intel8x0_hw_free,
1448 .prepare = snd_intel8x0_pcm_prepare,
1449 .trigger = snd_intel8x0_pcm_trigger,
1450 .pointer = snd_intel8x0_pcm_pointer,
1451};
1452
1453#if 0 // NYI
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001454static struct snd_pcm_ops snd_intel8x0_ali_spdifout_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 .open = snd_intel8x0_ali_spdifout_open,
1456 .close = snd_intel8x0_ali_spdifout_close,
1457 .ioctl = snd_pcm_lib_ioctl,
1458 .hw_params = snd_intel8x0_hw_params,
1459 .hw_free = snd_intel8x0_hw_free,
1460 .prepare = snd_intel8x0_pcm_prepare,
1461 .trigger = snd_intel8x0_pcm_trigger,
1462 .pointer = snd_intel8x0_pcm_pointer,
1463};
1464#endif // NYI
1465
1466struct ich_pcm_table {
1467 char *suffix;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001468 struct snd_pcm_ops *playback_ops;
1469 struct snd_pcm_ops *capture_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 size_t prealloc_size;
1471 size_t prealloc_max_size;
1472 int ac97_idx;
1473};
1474
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001475static int __devinit snd_intel8x0_pcm1(struct intel8x0 *chip, int device,
1476 struct ich_pcm_table *rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001478 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 int err;
1480 char name[32];
1481
1482 if (rec->suffix)
1483 sprintf(name, "Intel ICH - %s", rec->suffix);
1484 else
1485 strcpy(name, "Intel ICH");
1486 err = snd_pcm_new(chip->card, name, device,
1487 rec->playback_ops ? 1 : 0,
1488 rec->capture_ops ? 1 : 0, &pcm);
1489 if (err < 0)
1490 return err;
1491
1492 if (rec->playback_ops)
1493 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, rec->playback_ops);
1494 if (rec->capture_ops)
1495 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, rec->capture_ops);
1496
1497 pcm->private_data = chip;
1498 pcm->info_flags = 0;
1499 if (rec->suffix)
1500 sprintf(pcm->name, "%s - %s", chip->card->shortname, rec->suffix);
1501 else
1502 strcpy(pcm->name, chip->card->shortname);
1503 chip->pcm[device] = pcm;
1504
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001505 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1506 snd_dma_pci_data(chip->pci),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 rec->prealloc_size, rec->prealloc_max_size);
1508
1509 return 0;
1510}
1511
1512static struct ich_pcm_table intel_pcms[] __devinitdata = {
1513 {
1514 .playback_ops = &snd_intel8x0_playback_ops,
1515 .capture_ops = &snd_intel8x0_capture_ops,
1516 .prealloc_size = 64 * 1024,
1517 .prealloc_max_size = 128 * 1024,
1518 },
1519 {
1520 .suffix = "MIC ADC",
1521 .capture_ops = &snd_intel8x0_capture_mic_ops,
1522 .prealloc_size = 0,
1523 .prealloc_max_size = 128 * 1024,
1524 .ac97_idx = ICHD_MIC,
1525 },
1526 {
1527 .suffix = "MIC2 ADC",
1528 .capture_ops = &snd_intel8x0_capture_mic2_ops,
1529 .prealloc_size = 0,
1530 .prealloc_max_size = 128 * 1024,
1531 .ac97_idx = ICHD_MIC2,
1532 },
1533 {
1534 .suffix = "ADC2",
1535 .capture_ops = &snd_intel8x0_capture2_ops,
1536 .prealloc_size = 0,
1537 .prealloc_max_size = 128 * 1024,
1538 .ac97_idx = ICHD_PCM2IN,
1539 },
1540 {
1541 .suffix = "IEC958",
1542 .playback_ops = &snd_intel8x0_spdif_ops,
1543 .prealloc_size = 64 * 1024,
1544 .prealloc_max_size = 128 * 1024,
1545 .ac97_idx = ICHD_SPBAR,
1546 },
1547};
1548
1549static struct ich_pcm_table nforce_pcms[] __devinitdata = {
1550 {
1551 .playback_ops = &snd_intel8x0_playback_ops,
1552 .capture_ops = &snd_intel8x0_capture_ops,
1553 .prealloc_size = 64 * 1024,
1554 .prealloc_max_size = 128 * 1024,
1555 },
1556 {
1557 .suffix = "MIC ADC",
1558 .capture_ops = &snd_intel8x0_capture_mic_ops,
1559 .prealloc_size = 0,
1560 .prealloc_max_size = 128 * 1024,
1561 .ac97_idx = NVD_MIC,
1562 },
1563 {
1564 .suffix = "IEC958",
1565 .playback_ops = &snd_intel8x0_spdif_ops,
1566 .prealloc_size = 64 * 1024,
1567 .prealloc_max_size = 128 * 1024,
1568 .ac97_idx = NVD_SPBAR,
1569 },
1570};
1571
1572static struct ich_pcm_table ali_pcms[] __devinitdata = {
1573 {
1574 .playback_ops = &snd_intel8x0_ali_playback_ops,
1575 .capture_ops = &snd_intel8x0_ali_capture_ops,
1576 .prealloc_size = 64 * 1024,
1577 .prealloc_max_size = 128 * 1024,
1578 },
1579 {
1580 .suffix = "MIC ADC",
1581 .capture_ops = &snd_intel8x0_ali_capture_mic_ops,
1582 .prealloc_size = 0,
1583 .prealloc_max_size = 128 * 1024,
1584 .ac97_idx = ALID_MIC,
1585 },
1586 {
1587 .suffix = "IEC958",
1588 .playback_ops = &snd_intel8x0_ali_ac97spdifout_ops,
1589 .capture_ops = &snd_intel8x0_ali_spdifin_ops,
1590 .prealloc_size = 64 * 1024,
1591 .prealloc_max_size = 128 * 1024,
1592 .ac97_idx = ALID_AC97SPDIFOUT,
1593 },
1594#if 0 // NYI
1595 {
1596 .suffix = "HW IEC958",
1597 .playback_ops = &snd_intel8x0_ali_spdifout_ops,
1598 .prealloc_size = 64 * 1024,
1599 .prealloc_max_size = 128 * 1024,
1600 },
1601#endif
1602};
1603
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001604static int __devinit snd_intel8x0_pcm(struct intel8x0 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 int i, tblsize, device, err;
1607 struct ich_pcm_table *tbl, *rec;
1608
1609 switch (chip->device_type) {
1610 case DEVICE_INTEL_ICH4:
1611 tbl = intel_pcms;
1612 tblsize = ARRAY_SIZE(intel_pcms);
1613 break;
1614 case DEVICE_NFORCE:
1615 tbl = nforce_pcms;
1616 tblsize = ARRAY_SIZE(nforce_pcms);
1617 break;
1618 case DEVICE_ALI:
1619 tbl = ali_pcms;
1620 tblsize = ARRAY_SIZE(ali_pcms);
1621 break;
1622 default:
1623 tbl = intel_pcms;
1624 tblsize = 2;
1625 break;
1626 }
1627
1628 device = 0;
1629 for (i = 0; i < tblsize; i++) {
1630 rec = tbl + i;
1631 if (i > 0 && rec->ac97_idx) {
1632 /* activate PCM only when associated AC'97 codec */
1633 if (! chip->ichd[rec->ac97_idx].pcm)
1634 continue;
1635 }
1636 err = snd_intel8x0_pcm1(chip, device, rec);
1637 if (err < 0)
1638 return err;
1639 device++;
1640 }
1641
1642 chip->pcm_devs = device;
1643 return 0;
1644}
1645
1646
1647/*
1648 * Mixer part
1649 */
1650
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001651static void snd_intel8x0_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001653 struct intel8x0 *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 chip->ac97_bus = NULL;
1655}
1656
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001657static void snd_intel8x0_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001659 struct intel8x0 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 chip->ac97[ac97->num] = NULL;
1661}
1662
1663static struct ac97_pcm ac97_pcm_defs[] __devinitdata = {
1664 /* front PCM */
1665 {
1666 .exclusive = 1,
1667 .r = { {
1668 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1669 (1 << AC97_SLOT_PCM_RIGHT) |
1670 (1 << AC97_SLOT_PCM_CENTER) |
1671 (1 << AC97_SLOT_PCM_SLEFT) |
1672 (1 << AC97_SLOT_PCM_SRIGHT) |
1673 (1 << AC97_SLOT_LFE)
1674 },
1675 {
1676 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1677 (1 << AC97_SLOT_PCM_RIGHT) |
1678 (1 << AC97_SLOT_PCM_LEFT_0) |
1679 (1 << AC97_SLOT_PCM_RIGHT_0)
1680 }
1681 }
1682 },
1683 /* PCM IN #1 */
1684 {
1685 .stream = 1,
1686 .exclusive = 1,
1687 .r = { {
1688 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1689 (1 << AC97_SLOT_PCM_RIGHT)
1690 }
1691 }
1692 },
1693 /* MIC IN #1 */
1694 {
1695 .stream = 1,
1696 .exclusive = 1,
1697 .r = { {
1698 .slots = (1 << AC97_SLOT_MIC)
1699 }
1700 }
1701 },
1702 /* S/PDIF PCM */
1703 {
1704 .exclusive = 1,
1705 .spdif = 1,
1706 .r = { {
1707 .slots = (1 << AC97_SLOT_SPDIF_LEFT2) |
1708 (1 << AC97_SLOT_SPDIF_RIGHT2)
1709 }
1710 }
1711 },
1712 /* PCM IN #2 */
1713 {
1714 .stream = 1,
1715 .exclusive = 1,
1716 .r = { {
1717 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1718 (1 << AC97_SLOT_PCM_RIGHT)
1719 }
1720 }
1721 },
1722 /* MIC IN #2 */
1723 {
1724 .stream = 1,
1725 .exclusive = 1,
1726 .r = { {
1727 .slots = (1 << AC97_SLOT_MIC)
1728 }
1729 }
1730 },
1731};
1732
1733static struct ac97_quirk ac97_quirks[] __devinitdata = {
1734 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001735 .subvendor = 0x0e11,
1736 .subdevice = 0x008a,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 .name = "Compaq Evo W4000", /* AD1885 */
1738 .type = AC97_TUNE_HP_ONLY
1739 },
1740 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001741 .subvendor = 0x0e11,
1742 .subdevice = 0x00b8,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 .name = "Compaq Evo D510C",
1744 .type = AC97_TUNE_HP_ONLY
1745 },
1746 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001747 .subvendor = 0x0e11,
1748 .subdevice = 0x0860,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 .name = "HP/Compaq nx7010",
1750 .type = AC97_TUNE_MUTE_LED
1751 },
1752 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001753 .subvendor = 0x1014,
1754 .subdevice = 0x1f00,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 .name = "MS-9128",
1756 .type = AC97_TUNE_ALC_JACK
1757 },
1758 {
Takashi Iwai5d529392005-09-01 13:56:23 +02001759 .subvendor = 0x1014,
1760 .subdevice = 0x0267,
1761 .name = "IBM NetVista A30p", /* AD1981B */
1762 .type = AC97_TUNE_HP_ONLY
1763 },
1764 {
Dick Streefland72c89862005-10-07 12:02:23 +02001765 .subvendor = 0x1025,
1766 .subdevice = 0x0083,
1767 .name = "Acer Aspire 3003LCi",
1768 .type = AC97_TUNE_HP_ONLY
1769 },
1770 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001771 .subvendor = 0x1028,
1772 .subdevice = 0x00d8,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 .name = "Dell Precision 530", /* AD1885 */
1774 .type = AC97_TUNE_HP_ONLY
1775 },
1776 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001777 .subvendor = 0x1028,
1778 .subdevice = 0x010d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 .name = "Dell", /* which model? AD1885 */
1780 .type = AC97_TUNE_HP_ONLY
1781 },
1782 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001783 .subvendor = 0x1028,
1784 .subdevice = 0x0126,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 .name = "Dell Optiplex GX260", /* AD1981A */
1786 .type = AC97_TUNE_HP_ONLY
1787 },
1788 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001789 .subvendor = 0x1028,
1790 .subdevice = 0x012c,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 .name = "Dell Precision 650", /* AD1981A */
1792 .type = AC97_TUNE_HP_ONLY
1793 },
1794 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001795 .subvendor = 0x1028,
1796 .subdevice = 0x012d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 .name = "Dell Precision 450", /* AD1981B*/
1798 .type = AC97_TUNE_HP_ONLY
1799 },
1800 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001801 .subvendor = 0x1028,
1802 .subdevice = 0x0147,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 .name = "Dell", /* which model? AD1981B*/
1804 .type = AC97_TUNE_HP_ONLY
1805 },
1806 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001807 .subvendor = 0x1028,
1808 .subdevice = 0x0163,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 .name = "Dell Unknown", /* STAC9750/51 */
1810 .type = AC97_TUNE_HP_ONLY
1811 },
1812 {
Karoly Lorentey6d6f9152005-10-25 11:50:25 +02001813 .subvendor = 0x1028,
1814 .subdevice = 0x0191,
1815 .name = "Dell Inspiron 8600",
1816 .type = AC97_TUNE_HP_ONLY
1817 },
1818 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001819 .subvendor = 0x103c,
1820 .subdevice = 0x006d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 .name = "HP zv5000",
1822 .type = AC97_TUNE_MUTE_LED /*AD1981B*/
1823 },
1824 { /* FIXME: which codec? */
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001825 .subvendor = 0x103c,
1826 .subdevice = 0x00c3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 .name = "HP xw6000",
1828 .type = AC97_TUNE_HP_ONLY
1829 },
1830 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001831 .subvendor = 0x103c,
1832 .subdevice = 0x088c,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 .name = "HP nc8000",
1834 .type = AC97_TUNE_MUTE_LED
1835 },
1836 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001837 .subvendor = 0x103c,
1838 .subdevice = 0x0890,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 .name = "HP nc6000",
1840 .type = AC97_TUNE_MUTE_LED
1841 },
1842 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001843 .subvendor = 0x103c,
Daniel Horchnere0c93cf2005-08-25 13:02:24 +02001844 .subdevice = 0x0934,
1845 .name = "HP nx8220",
1846 .type = AC97_TUNE_MUTE_LED
1847 },
1848 {
1849 .subvendor = 0x103c,
Sergey Vlasov66d10642005-08-22 13:43:39 +02001850 .subdevice = 0x099c,
1851 .name = "HP nx6110", /* AD1981B */
1852 .type = AC97_TUNE_HP_ONLY
1853 },
1854 {
1855 .subvendor = 0x103c,
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001856 .subdevice = 0x129d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 .name = "HP xw8000",
1858 .type = AC97_TUNE_HP_ONLY
1859 },
1860 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001861 .subvendor = 0x103c,
1862 .subdevice = 0x12f1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 .name = "HP xw8200", /* AD1981B*/
1864 .type = AC97_TUNE_HP_ONLY
1865 },
1866 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001867 .subvendor = 0x103c,
1868 .subdevice = 0x12f2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 .name = "HP xw6200",
1870 .type = AC97_TUNE_HP_ONLY
1871 },
1872 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001873 .subvendor = 0x103c,
1874 .subdevice = 0x3008,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 .name = "HP xw4200", /* AD1981B*/
1876 .type = AC97_TUNE_HP_ONLY
1877 },
1878 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001879 .subvendor = 0x104d,
1880 .subdevice = 0x8197,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 .name = "Sony S1XP",
1882 .type = AC97_TUNE_INV_EAPD
1883 },
1884 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001885 .subvendor = 0x1043,
1886 .subdevice = 0x80f3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 .name = "ASUS ICH5/AD1985",
1888 .type = AC97_TUNE_AD_SHARING
1889 },
1890 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001891 .subvendor = 0x10cf,
1892 .subdevice = 0x11c3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 .name = "Fujitsu-Siemens E4010",
1894 .type = AC97_TUNE_HP_ONLY
1895 },
1896 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001897 .subvendor = 0x10cf,
Takashi Iwai98c7f212005-05-31 16:52:58 +02001898 .subdevice = 0x1225,
1899 .name = "Fujitsu-Siemens T3010",
1900 .type = AC97_TUNE_HP_ONLY
1901 },
1902 {
1903 .subvendor = 0x10cf,
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001904 .subdevice = 0x1253,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 .name = "Fujitsu S6210", /* STAC9750/51 */
1906 .type = AC97_TUNE_HP_ONLY
1907 },
1908 {
Takashi Iwai9970dce2005-08-25 20:30:27 +02001909 .subvendor = 0x10cf,
1910 .subdevice = 0x12ec,
1911 .name = "Fujitsu-Siemens 4010",
1912 .type = AC97_TUNE_HP_ONLY
1913 },
1914 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001915 .subvendor = 0x10f1,
1916 .subdevice = 0x2665,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 .name = "Fujitsu-Siemens Celsius", /* AD1981? */
1918 .type = AC97_TUNE_HP_ONLY
1919 },
1920 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001921 .subvendor = 0x10f1,
1922 .subdevice = 0x2885,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 .name = "AMD64 Mobo", /* ALC650 */
1924 .type = AC97_TUNE_HP_ONLY
1925 },
1926 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001927 .subvendor = 0x110a,
1928 .subdevice = 0x0056,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 .name = "Fujitsu-Siemens Scenic", /* AD1981? */
1930 .type = AC97_TUNE_HP_ONLY
1931 },
1932 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001933 .subvendor = 0x11d4,
1934 .subdevice = 0x5375,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 .name = "ADI AD1985 (discrete)",
1936 .type = AC97_TUNE_HP_ONLY
1937 },
1938 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001939 .subvendor = 0x1462,
1940 .subdevice = 0x5470,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 .name = "MSI P4 ATX 645 Ultra",
1942 .type = AC97_TUNE_HP_ONLY
1943 },
1944 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001945 .subvendor = 0x1734,
1946 .subdevice = 0x0088,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 .name = "Fujitsu-Siemens D1522", /* AD1981 */
1948 .type = AC97_TUNE_HP_ONLY
1949 },
1950 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001951 .subvendor = 0x8086,
1952 .subdevice = 0x2000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 .mask = 0xfff0,
1954 .name = "Intel ICH5/AD1985",
1955 .type = AC97_TUNE_AD_SHARING
1956 },
1957 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001958 .subvendor = 0x8086,
1959 .subdevice = 0x4000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 .mask = 0xfff0,
1961 .name = "Intel ICH5/AD1985",
1962 .type = AC97_TUNE_AD_SHARING
1963 },
1964 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001965 .subvendor = 0x8086,
1966 .subdevice = 0x4856,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 .name = "Intel D845WN (82801BA)",
1968 .type = AC97_TUNE_SWAP_HP
1969 },
1970 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001971 .subvendor = 0x8086,
1972 .subdevice = 0x4d44,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 .name = "Intel D850EMV2", /* AD1885 */
1974 .type = AC97_TUNE_HP_ONLY
1975 },
1976 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001977 .subvendor = 0x8086,
1978 .subdevice = 0x4d56,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 .name = "Intel ICH/AD1885",
1980 .type = AC97_TUNE_HP_ONLY
1981 },
1982 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001983 .subvendor = 0x8086,
1984 .subdevice = 0x6000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 .mask = 0xfff0,
1986 .name = "Intel ICH5/AD1985",
1987 .type = AC97_TUNE_AD_SHARING
1988 },
1989 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001990 .subvendor = 0x8086,
1991 .subdevice = 0xe000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 .mask = 0xfff0,
1993 .name = "Intel ICH5/AD1985",
1994 .type = AC97_TUNE_AD_SHARING
1995 },
1996#if 0 /* FIXME: this seems wrong on most boards */
1997 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001998 .subvendor = 0x8086,
1999 .subdevice = 0xa000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 .mask = 0xfff0,
2001 .name = "Intel ICH5/AD1985",
2002 .type = AC97_TUNE_HP_ONLY
2003 },
2004#endif
2005 { } /* terminator */
2006};
2007
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002008static int __devinit snd_intel8x0_mixer(struct intel8x0 *chip, int ac97_clock,
2009 const char *quirk_override)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002011 struct snd_ac97_bus *pbus;
2012 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 int err;
2014 unsigned int i, codecs;
2015 unsigned int glob_sta = 0;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002016 struct snd_ac97_bus_ops *ops;
2017 static struct snd_ac97_bus_ops standard_bus_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 .write = snd_intel8x0_codec_write,
2019 .read = snd_intel8x0_codec_read,
2020 };
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002021 static struct snd_ac97_bus_ops ali_bus_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 .write = snd_intel8x0_ali_codec_write,
2023 .read = snd_intel8x0_ali_codec_read,
2024 };
2025
2026 chip->spdif_idx = -1; /* use PCMOUT (or disabled) */
2027 switch (chip->device_type) {
2028 case DEVICE_NFORCE:
2029 chip->spdif_idx = NVD_SPBAR;
2030 break;
2031 case DEVICE_ALI:
2032 chip->spdif_idx = ALID_AC97SPDIFOUT;
2033 break;
2034 case DEVICE_INTEL_ICH4:
2035 chip->spdif_idx = ICHD_SPBAR;
2036 break;
2037 };
2038
2039 chip->in_ac97_init = 1;
2040
2041 memset(&ac97, 0, sizeof(ac97));
2042 ac97.private_data = chip;
2043 ac97.private_free = snd_intel8x0_mixer_free_ac97;
2044 ac97.scaps = AC97_SCAP_SKIP_MODEM;
2045 if (chip->xbox)
2046 ac97.scaps |= AC97_SCAP_DETECT_BY_VENDOR;
2047 if (chip->device_type != DEVICE_ALI) {
2048 glob_sta = igetdword(chip, ICHREG(GLOB_STA));
2049 ops = &standard_bus_ops;
2050 if (chip->device_type == DEVICE_INTEL_ICH4) {
2051 codecs = 0;
2052 if (glob_sta & ICH_PCR)
2053 codecs++;
2054 if (glob_sta & ICH_SCR)
2055 codecs++;
2056 if (glob_sta & ICH_TCR)
2057 codecs++;
2058 chip->in_sdin_init = 1;
2059 for (i = 0; i < codecs; i++) {
2060 snd_intel8x0_codec_read_test(chip, i);
2061 chip->ac97_sdin[i] = igetbyte(chip, ICHREG(SDM)) & ICH_LDI_MASK;
2062 }
2063 chip->in_sdin_init = 0;
2064 } else {
2065 codecs = glob_sta & ICH_SCR ? 2 : 1;
2066 }
2067 } else {
2068 ops = &ali_bus_ops;
2069 codecs = 1;
2070 /* detect the secondary codec */
2071 for (i = 0; i < 100; i++) {
2072 unsigned int reg = igetdword(chip, ICHREG(ALI_RTSR));
2073 if (reg & 0x40) {
2074 codecs = 2;
2075 break;
2076 }
2077 iputdword(chip, ICHREG(ALI_RTSR), reg | 0x40);
2078 udelay(1);
2079 }
2080 }
2081 if ((err = snd_ac97_bus(chip->card, 0, ops, chip, &pbus)) < 0)
2082 goto __err;
2083 pbus->private_free = snd_intel8x0_mixer_free_ac97_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (ac97_clock >= 8000 && ac97_clock <= 48000)
2085 pbus->clock = ac97_clock;
2086 /* FIXME: my test board doesn't work well with VRA... */
2087 if (chip->device_type == DEVICE_ALI)
2088 pbus->no_vra = 1;
2089 else
2090 pbus->dra = 1;
2091 chip->ac97_bus = pbus;
2092
2093 ac97.pci = chip->pci;
2094 for (i = 0; i < codecs; i++) {
2095 ac97.num = i;
2096 if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
2097 if (err != -EACCES)
2098 snd_printk(KERN_ERR "Unable to initialize codec #%d\n", i);
2099 if (i == 0)
2100 goto __err;
2101 continue;
2102 }
2103 }
2104 /* tune up the primary codec */
2105 snd_ac97_tune_hardware(chip->ac97[0], ac97_quirks, quirk_override);
2106 /* enable separate SDINs for ICH4 */
2107 if (chip->device_type == DEVICE_INTEL_ICH4)
2108 pbus->isdin = 1;
2109 /* find the available PCM streams */
2110 i = ARRAY_SIZE(ac97_pcm_defs);
2111 if (chip->device_type != DEVICE_INTEL_ICH4)
2112 i -= 2; /* do not allocate PCM2IN and MIC2 */
2113 if (chip->spdif_idx < 0)
2114 i--; /* do not allocate S/PDIF */
2115 err = snd_ac97_pcm_assign(pbus, i, ac97_pcm_defs);
2116 if (err < 0)
2117 goto __err;
2118 chip->ichd[ICHD_PCMOUT].pcm = &pbus->pcms[0];
2119 chip->ichd[ICHD_PCMIN].pcm = &pbus->pcms[1];
2120 chip->ichd[ICHD_MIC].pcm = &pbus->pcms[2];
2121 if (chip->spdif_idx >= 0)
2122 chip->ichd[chip->spdif_idx].pcm = &pbus->pcms[3];
2123 if (chip->device_type == DEVICE_INTEL_ICH4) {
2124 chip->ichd[ICHD_PCM2IN].pcm = &pbus->pcms[4];
2125 chip->ichd[ICHD_MIC2].pcm = &pbus->pcms[5];
2126 }
2127 /* enable separate SDINs for ICH4 */
2128 if (chip->device_type == DEVICE_INTEL_ICH4) {
2129 struct ac97_pcm *pcm = chip->ichd[ICHD_PCM2IN].pcm;
2130 u8 tmp = igetbyte(chip, ICHREG(SDM));
2131 tmp &= ~(ICH_DI2L_MASK|ICH_DI1L_MASK);
2132 if (pcm) {
2133 tmp |= ICH_SE; /* steer enable for multiple SDINs */
2134 tmp |= chip->ac97_sdin[0] << ICH_DI1L_SHIFT;
2135 for (i = 1; i < 4; i++) {
2136 if (pcm->r[0].codec[i]) {
2137 tmp |= chip->ac97_sdin[pcm->r[0].codec[1]->num] << ICH_DI2L_SHIFT;
2138 break;
2139 }
2140 }
2141 } else {
2142 tmp &= ~ICH_SE; /* steer disable */
2143 }
2144 iputbyte(chip, ICHREG(SDM), tmp);
2145 }
2146 if (pbus->pcms[0].r[0].slots & (1 << AC97_SLOT_PCM_SLEFT)) {
2147 chip->multi4 = 1;
2148 if (pbus->pcms[0].r[0].slots & (1 << AC97_SLOT_LFE))
2149 chip->multi6 = 1;
2150 }
2151 if (pbus->pcms[0].r[1].rslots[0]) {
2152 chip->dra = 1;
2153 }
2154 if (chip->device_type == DEVICE_INTEL_ICH4) {
2155 if ((igetdword(chip, ICHREG(GLOB_STA)) & ICH_SAMPLE_CAP) == ICH_SAMPLE_16_20)
2156 chip->smp20bit = 1;
2157 }
2158 if (chip->device_type == DEVICE_NFORCE) {
2159 /* 48kHz only */
2160 chip->ichd[chip->spdif_idx].pcm->rates = SNDRV_PCM_RATE_48000;
2161 }
2162 if (chip->device_type == DEVICE_INTEL_ICH4) {
2163 /* use slot 10/11 for SPDIF */
2164 u32 val;
2165 val = igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_PCM_SPDIF_MASK;
2166 val |= ICH_PCM_SPDIF_1011;
2167 iputdword(chip, ICHREG(GLOB_CNT), val);
2168 snd_ac97_update_bits(chip->ac97[0], AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
2169 }
2170 chip->in_ac97_init = 0;
2171 return 0;
2172
2173 __err:
2174 /* clear the cold-reset bit for the next chance */
2175 if (chip->device_type != DEVICE_ALI)
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002176 iputdword(chip, ICHREG(GLOB_CNT),
2177 igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_AC97COLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return err;
2179}
2180
2181
2182/*
2183 *
2184 */
2185
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002186static void do_ali_reset(struct intel8x0 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 iputdword(chip, ICHREG(ALI_SCR), ICH_ALI_SC_RESET);
2189 iputdword(chip, ICHREG(ALI_FIFOCR1), 0x83838383);
2190 iputdword(chip, ICHREG(ALI_FIFOCR2), 0x83838383);
2191 iputdword(chip, ICHREG(ALI_FIFOCR3), 0x83838383);
2192 iputdword(chip, ICHREG(ALI_INTERFACECR),
Wei Nid78bec22005-10-24 11:04:51 +02002193 ICH_ALI_IF_PI|ICH_ALI_IF_PO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 iputdword(chip, ICHREG(ALI_INTERRUPTCR), 0x00000000);
2195 iputdword(chip, ICHREG(ALI_INTERRUPTSR), 0x00000000);
2196}
2197
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002198static int snd_intel8x0_ich_chip_init(struct intel8x0 *chip, int probing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 unsigned long end_time;
2201 unsigned int cnt, status, nstatus;
2202
2203 /* put logic to right state */
2204 /* first clear status bits */
2205 status = ICH_RCS | ICH_MCINT | ICH_POINT | ICH_PIINT;
2206 if (chip->device_type == DEVICE_NFORCE)
2207 status |= ICH_NVSPINT;
2208 cnt = igetdword(chip, ICHREG(GLOB_STA));
2209 iputdword(chip, ICHREG(GLOB_STA), cnt & status);
2210
2211 /* ACLink on, 2 channels */
2212 cnt = igetdword(chip, ICHREG(GLOB_CNT));
2213 cnt &= ~(ICH_ACLINK | ICH_PCM_246_MASK);
2214 /* finish cold or do warm reset */
2215 cnt |= (cnt & ICH_AC97COLD) == 0 ? ICH_AC97COLD : ICH_AC97WARM;
2216 iputdword(chip, ICHREG(GLOB_CNT), cnt);
2217 end_time = (jiffies + (HZ / 4)) + 1;
2218 do {
2219 if ((igetdword(chip, ICHREG(GLOB_CNT)) & ICH_AC97WARM) == 0)
2220 goto __ok;
Takashi Iwai954bea32005-11-17 10:37:40 +01002221 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 } while (time_after_eq(end_time, jiffies));
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002223 snd_printk(KERN_ERR "AC'97 warm reset still in progress? [0x%x]\n",
2224 igetdword(chip, ICHREG(GLOB_CNT)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return -EIO;
2226
2227 __ok:
2228 if (probing) {
2229 /* wait for any codec ready status.
2230 * Once it becomes ready it should remain ready
2231 * as long as we do not disable the ac97 link.
2232 */
2233 end_time = jiffies + HZ;
2234 do {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002235 status = igetdword(chip, ICHREG(GLOB_STA)) &
2236 (ICH_PCR | ICH_SCR | ICH_TCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (status)
2238 break;
Takashi Iwai954bea32005-11-17 10:37:40 +01002239 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 } while (time_after_eq(end_time, jiffies));
2241 if (! status) {
2242 /* no codec is found */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002243 snd_printk(KERN_ERR "codec_ready: codec is not ready [0x%x]\n",
2244 igetdword(chip, ICHREG(GLOB_STA)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return -EIO;
2246 }
2247
2248 if (chip->device_type == DEVICE_INTEL_ICH4)
2249 /* ICH4 can have three codecs */
2250 nstatus = ICH_PCR | ICH_SCR | ICH_TCR;
2251 else
2252 /* others up to two codecs */
2253 nstatus = ICH_PCR | ICH_SCR;
2254
2255 /* wait for other codecs ready status. */
2256 end_time = jiffies + HZ / 4;
2257 while (status != nstatus && time_after_eq(end_time, jiffies)) {
Takashi Iwai954bea32005-11-17 10:37:40 +01002258 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 status |= igetdword(chip, ICHREG(GLOB_STA)) & nstatus;
2260 }
2261
2262 } else {
2263 /* resume phase */
2264 int i;
2265 status = 0;
2266 for (i = 0; i < 3; i++)
2267 if (chip->ac97[i])
2268 status |= get_ich_codec_bit(chip, i);
2269 /* wait until all the probed codecs are ready */
2270 end_time = jiffies + HZ;
2271 do {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002272 nstatus = igetdword(chip, ICHREG(GLOB_STA)) &
2273 (ICH_PCR | ICH_SCR | ICH_TCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (status == nstatus)
2275 break;
Takashi Iwai954bea32005-11-17 10:37:40 +01002276 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 } while (time_after_eq(end_time, jiffies));
2278 }
2279
2280 if (chip->device_type == DEVICE_SIS) {
2281 /* unmute the output on SIS7012 */
2282 iputword(chip, 0x4c, igetword(chip, 0x4c) | 1);
2283 }
2284 if (chip->device_type == DEVICE_NFORCE) {
2285 /* enable SPDIF interrupt */
2286 unsigned int val;
2287 pci_read_config_dword(chip->pci, 0x4c, &val);
2288 val |= 0x1000000;
2289 pci_write_config_dword(chip->pci, 0x4c, val);
2290 }
2291 return 0;
2292}
2293
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002294static int snd_intel8x0_ali_chip_init(struct intel8x0 *chip, int probing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 u32 reg;
2297 int i = 0;
2298
2299 reg = igetdword(chip, ICHREG(ALI_SCR));
2300 if ((reg & 2) == 0) /* Cold required */
2301 reg |= 2;
2302 else
2303 reg |= 1; /* Warm */
2304 reg &= ~0x80000000; /* ACLink on */
2305 iputdword(chip, ICHREG(ALI_SCR), reg);
2306
2307 for (i = 0; i < HZ / 2; i++) {
2308 if (! (igetdword(chip, ICHREG(ALI_INTERRUPTSR)) & ALI_INT_GPIO))
2309 goto __ok;
Takashi Iwai954bea32005-11-17 10:37:40 +01002310 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312 snd_printk(KERN_ERR "AC'97 reset failed.\n");
2313 if (probing)
2314 return -EIO;
2315
2316 __ok:
2317 for (i = 0; i < HZ / 2; i++) {
2318 reg = igetdword(chip, ICHREG(ALI_RTSR));
2319 if (reg & 0x80) /* primary codec */
2320 break;
2321 iputdword(chip, ICHREG(ALI_RTSR), reg | 0x80);
Takashi Iwai954bea32005-11-17 10:37:40 +01002322 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
2324
2325 do_ali_reset(chip);
2326 return 0;
2327}
2328
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002329static int snd_intel8x0_chip_init(struct intel8x0 *chip, int probing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
2331 unsigned int i;
2332 int err;
2333
2334 if (chip->device_type != DEVICE_ALI) {
2335 if ((err = snd_intel8x0_ich_chip_init(chip, probing)) < 0)
2336 return err;
2337 iagetword(chip, 0); /* clear semaphore flag */
2338 } else {
2339 if ((err = snd_intel8x0_ali_chip_init(chip, probing)) < 0)
2340 return err;
2341 }
2342
2343 /* disable interrupts */
2344 for (i = 0; i < chip->bdbars_count; i++)
2345 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
2346 /* reset channels */
2347 for (i = 0; i < chip->bdbars_count; i++)
2348 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
2349 /* initialize Buffer Descriptor Lists */
2350 for (i = 0; i < chip->bdbars_count; i++)
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002351 iputdword(chip, ICH_REG_OFF_BDBAR + chip->ichd[i].reg_offset,
2352 chip->ichd[i].bdbar_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return 0;
2354}
2355
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002356static int snd_intel8x0_free(struct intel8x0 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
2358 unsigned int i;
2359
2360 if (chip->irq < 0)
2361 goto __hw_end;
2362 /* disable interrupts */
2363 for (i = 0; i < chip->bdbars_count; i++)
2364 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
2365 /* reset channels */
2366 for (i = 0; i < chip->bdbars_count; i++)
2367 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
2368 if (chip->device_type == DEVICE_NFORCE) {
2369 /* stop the spdif interrupt */
2370 unsigned int val;
2371 pci_read_config_dword(chip->pci, 0x4c, &val);
2372 val &= ~0x1000000;
2373 pci_write_config_dword(chip->pci, 0x4c, val);
2374 }
2375 /* --- */
2376 synchronize_irq(chip->irq);
2377 __hw_end:
2378 if (chip->irq >= 0)
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002379 free_irq(chip->irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (chip->bdbars.area) {
2381 if (chip->fix_nocache)
2382 fill_nocache(chip->bdbars.area, chip->bdbars.bytes, 0);
2383 snd_dma_free_pages(&chip->bdbars);
2384 }
2385 if (chip->remap_addr)
2386 iounmap(chip->remap_addr);
2387 if (chip->remap_bmaddr)
2388 iounmap(chip->remap_bmaddr);
2389 pci_release_regions(chip->pci);
2390 pci_disable_device(chip->pci);
2391 kfree(chip);
2392 return 0;
2393}
2394
2395#ifdef CONFIG_PM
2396/*
2397 * power management
2398 */
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002399static int intel8x0_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002401 struct snd_card *card = pci_get_drvdata(pci);
2402 struct intel8x0 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 int i;
2404
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002405 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 for (i = 0; i < chip->pcm_devs; i++)
2407 snd_pcm_suspend_all(chip->pcm[i]);
2408 /* clear nocache */
2409 if (chip->fix_nocache) {
2410 for (i = 0; i < chip->bdbars_count; i++) {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002411 struct ichdev *ichdev = &chip->ichd[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (ichdev->substream && ichdev->page_attr_changed) {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002413 struct snd_pcm_runtime *runtime = ichdev->substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if (runtime->dma_area)
2415 fill_nocache(runtime->dma_area, runtime->dma_bytes, 0);
2416 }
2417 }
2418 }
2419 for (i = 0; i < 3; i++)
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002420 snd_ac97_suspend(chip->ac97[i]);
Takashi Iwai52b72382005-06-30 13:47:06 +02002421 if (chip->device_type == DEVICE_INTEL_ICH4)
2422 chip->sdm_saved = igetbyte(chip, ICHREG(SDM));
Len Brownadbedd32005-07-30 01:55:32 -04002423
Rafael J. Wysocki90158b82005-07-24 14:22:00 -04002424 if (chip->irq >= 0)
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002425 free_irq(chip->irq, chip);
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002426 pci_disable_device(pci);
2427 pci_save_state(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return 0;
2429}
2430
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002431static int intel8x0_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002433 struct snd_card *card = pci_get_drvdata(pci);
2434 struct intel8x0 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 int i;
2436
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002437 pci_restore_state(pci);
2438 pci_enable_device(pci);
2439 pci_set_master(pci);
2440 request_irq(pci->irq, snd_intel8x0_interrupt, SA_INTERRUPT|SA_SHIRQ,
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002441 card->shortname, chip);
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002442 chip->irq = pci->irq;
Rafael J. Wysocki90158b82005-07-24 14:22:00 -04002443 synchronize_irq(chip->irq);
2444 snd_intel8x0_chip_init(chip, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Takashi Iwai52b72382005-06-30 13:47:06 +02002446 /* re-initialize mixer stuff */
2447 if (chip->device_type == DEVICE_INTEL_ICH4) {
2448 /* enable separate SDINs for ICH4 */
2449 iputbyte(chip, ICHREG(SDM), chip->sdm_saved);
2450 /* use slot 10/11 for SPDIF */
2451 iputdword(chip, ICHREG(GLOB_CNT),
2452 (igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_PCM_SPDIF_MASK) |
2453 ICH_PCM_SPDIF_1011);
2454 }
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 /* refill nocache */
2457 if (chip->fix_nocache)
2458 fill_nocache(chip->bdbars.area, chip->bdbars.bytes, 1);
2459
2460 for (i = 0; i < 3; i++)
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002461 snd_ac97_resume(chip->ac97[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 /* refill nocache */
2464 if (chip->fix_nocache) {
2465 for (i = 0; i < chip->bdbars_count; i++) {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002466 struct ichdev *ichdev = &chip->ichd[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (ichdev->substream && ichdev->page_attr_changed) {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002468 struct snd_pcm_runtime *runtime = ichdev->substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (runtime->dma_area)
2470 fill_nocache(runtime->dma_area, runtime->dma_bytes, 1);
2471 }
2472 }
2473 }
2474
Takashi Iwai1cfe43d2005-08-16 16:52:24 +02002475 /* resume status */
2476 for (i = 0; i < chip->bdbars_count; i++) {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002477 struct ichdev *ichdev = &chip->ichd[i];
Takashi Iwai1cfe43d2005-08-16 16:52:24 +02002478 unsigned long port = ichdev->reg_offset;
2479 if (! ichdev->substream || ! ichdev->suspended)
2480 continue;
2481 if (ichdev->ichd == ICHD_PCMOUT)
2482 snd_intel8x0_setup_pcm_out(chip, ichdev->substream->runtime);
2483 iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
2484 iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
2485 iputbyte(chip, port + ICH_REG_OFF_CIV, ichdev->civ);
2486 iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
2487 }
2488
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002489 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return 0;
2491}
2492#endif /* CONFIG_PM */
2493
2494#define INTEL8X0_TESTBUF_SIZE 32768 /* enough large for one shot */
2495
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002496static void __devinit intel8x0_measure_ac97_clock(struct intel8x0 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002498 struct snd_pcm_substream *subs;
2499 struct ichdev *ichdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 unsigned long port;
2501 unsigned long pos, t;
2502 struct timeval start_time, stop_time;
2503
2504 if (chip->ac97_bus->clock != 48000)
2505 return; /* specified in module option */
2506
2507 subs = chip->pcm[0]->streams[0].substream;
2508 if (! subs || subs->dma_buffer.bytes < INTEL8X0_TESTBUF_SIZE) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002509 snd_printk(KERN_WARNING "no playback buffer allocated - aborting measure ac97 clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return;
2511 }
2512 ichdev = &chip->ichd[ICHD_PCMOUT];
2513 ichdev->physbuf = subs->dma_buffer.addr;
2514 ichdev->size = chip->ichd[ICHD_PCMOUT].fragsize = INTEL8X0_TESTBUF_SIZE;
2515 ichdev->substream = NULL; /* don't process interrupts */
2516
2517 /* set rate */
2518 if (snd_ac97_set_rate(chip->ac97[0], AC97_PCM_FRONT_DAC_RATE, 48000) < 0) {
2519 snd_printk(KERN_ERR "cannot set ac97 rate: clock = %d\n", chip->ac97_bus->clock);
2520 return;
2521 }
2522 snd_intel8x0_setup_periods(chip, ichdev);
2523 port = ichdev->reg_offset;
2524 spin_lock_irq(&chip->reg_lock);
2525 chip->in_measurement = 1;
2526 /* trigger */
2527 if (chip->device_type != DEVICE_ALI)
2528 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE | ICH_STARTBM);
2529 else {
2530 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE);
2531 iputdword(chip, ICHREG(ALI_DMACR), 1 << ichdev->ali_slot);
2532 }
2533 do_gettimeofday(&start_time);
2534 spin_unlock_irq(&chip->reg_lock);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002535 msleep(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 spin_lock_irq(&chip->reg_lock);
2537 /* check the position */
2538 pos = ichdev->fragsize1;
2539 pos -= igetword(chip, ichdev->reg_offset + ichdev->roff_picb) << ichdev->pos_shift;
2540 pos += ichdev->position;
2541 chip->in_measurement = 0;
2542 do_gettimeofday(&stop_time);
2543 /* stop */
2544 if (chip->device_type == DEVICE_ALI) {
Wei Nid78bec22005-10-24 11:04:51 +02002545 iputdword(chip, ICHREG(ALI_DMACR), 1 << (ichdev->ali_slot + 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 iputbyte(chip, port + ICH_REG_OFF_CR, 0);
2547 while (igetbyte(chip, port + ICH_REG_OFF_CR))
2548 ;
2549 } else {
2550 iputbyte(chip, port + ICH_REG_OFF_CR, 0);
2551 while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH))
2552 ;
2553 }
2554 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
2555 spin_unlock_irq(&chip->reg_lock);
2556
2557 t = stop_time.tv_sec - start_time.tv_sec;
2558 t *= 1000000;
2559 t += stop_time.tv_usec - start_time.tv_usec;
2560 printk(KERN_INFO "%s: measured %lu usecs\n", __FUNCTION__, t);
2561 if (t == 0) {
2562 snd_printk(KERN_ERR "?? calculation error..\n");
2563 return;
2564 }
2565 pos = (pos / 4) * 1000;
2566 pos = (pos / t) * 1000 + ((pos % t) * 1000) / t;
2567 if (pos < 40000 || pos >= 60000)
2568 /* abnormal value. hw problem? */
2569 printk(KERN_INFO "intel8x0: measured clock %ld rejected\n", pos);
2570 else if (pos < 47500 || pos > 48500)
2571 /* not 48000Hz, tuning the clock.. */
2572 chip->ac97_bus->clock = (chip->ac97_bus->clock * 48000) / pos;
2573 printk(KERN_INFO "intel8x0: clocking to %d\n", chip->ac97_bus->clock);
2574}
2575
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002576static void snd_intel8x0_proc_read(struct snd_info_entry * entry,
2577 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002579 struct intel8x0 *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 unsigned int tmp;
2581
2582 snd_iprintf(buffer, "Intel8x0\n\n");
2583 if (chip->device_type == DEVICE_ALI)
2584 return;
2585 tmp = igetdword(chip, ICHREG(GLOB_STA));
2586 snd_iprintf(buffer, "Global control : 0x%08x\n", igetdword(chip, ICHREG(GLOB_CNT)));
2587 snd_iprintf(buffer, "Global status : 0x%08x\n", tmp);
2588 if (chip->device_type == DEVICE_INTEL_ICH4)
2589 snd_iprintf(buffer, "SDM : 0x%08x\n", igetdword(chip, ICHREG(SDM)));
2590 snd_iprintf(buffer, "AC'97 codecs ready :%s%s%s%s\n",
2591 tmp & ICH_PCR ? " primary" : "",
2592 tmp & ICH_SCR ? " secondary" : "",
2593 tmp & ICH_TCR ? " tertiary" : "",
2594 (tmp & (ICH_PCR | ICH_SCR | ICH_TCR)) == 0 ? " none" : "");
2595 if (chip->device_type == DEVICE_INTEL_ICH4)
2596 snd_iprintf(buffer, "AC'97 codecs SDIN : %i %i %i\n",
2597 chip->ac97_sdin[0],
2598 chip->ac97_sdin[1],
2599 chip->ac97_sdin[2]);
2600}
2601
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002602static void __devinit snd_intel8x0_proc_init(struct intel8x0 * chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002604 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 if (! snd_card_proc_new(chip->card, "intel8x0", &entry))
2607 snd_info_set_text_ops(entry, chip, 1024, snd_intel8x0_proc_read);
2608}
2609
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002610static int snd_intel8x0_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002612 struct intel8x0 *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return snd_intel8x0_free(chip);
2614}
2615
2616struct ich_reg_info {
2617 unsigned int int_sta_mask;
2618 unsigned int offset;
2619};
2620
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002621static int __devinit snd_intel8x0_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 struct pci_dev *pci,
2623 unsigned long device_type,
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002624 struct intel8x0 ** r_intel8x0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002626 struct intel8x0 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 int err;
2628 unsigned int i;
2629 unsigned int int_sta_masks;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002630 struct ichdev *ichdev;
2631 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 .dev_free = snd_intel8x0_dev_free,
2633 };
2634
2635 static unsigned int bdbars[] = {
2636 3, /* DEVICE_INTEL */
2637 6, /* DEVICE_INTEL_ICH4 */
2638 3, /* DEVICE_SIS */
2639 6, /* DEVICE_ALI */
2640 4, /* DEVICE_NFORCE */
2641 };
2642 static struct ich_reg_info intel_regs[6] = {
2643 { ICH_PIINT, 0 },
2644 { ICH_POINT, 0x10 },
2645 { ICH_MCINT, 0x20 },
2646 { ICH_M2INT, 0x40 },
2647 { ICH_P2INT, 0x50 },
2648 { ICH_SPINT, 0x60 },
2649 };
2650 static struct ich_reg_info nforce_regs[4] = {
2651 { ICH_PIINT, 0 },
2652 { ICH_POINT, 0x10 },
2653 { ICH_MCINT, 0x20 },
2654 { ICH_NVSPINT, 0x70 },
2655 };
2656 static struct ich_reg_info ali_regs[6] = {
2657 { ALI_INT_PCMIN, 0x40 },
2658 { ALI_INT_PCMOUT, 0x50 },
2659 { ALI_INT_MICIN, 0x60 },
2660 { ALI_INT_CODECSPDIFOUT, 0x70 },
2661 { ALI_INT_SPDIFIN, 0xa0 },
2662 { ALI_INT_SPDIFOUT, 0xb0 },
2663 };
2664 struct ich_reg_info *tbl;
2665
2666 *r_intel8x0 = NULL;
2667
2668 if ((err = pci_enable_device(pci)) < 0)
2669 return err;
2670
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002671 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if (chip == NULL) {
2673 pci_disable_device(pci);
2674 return -ENOMEM;
2675 }
2676 spin_lock_init(&chip->reg_lock);
2677 chip->device_type = device_type;
2678 chip->card = card;
2679 chip->pci = pci;
2680 chip->irq = -1;
Takashi Iwaic829b052005-10-18 18:03:35 +02002681
2682 /* module parameters */
2683 chip->buggy_irq = buggy_irq;
2684 chip->buggy_semaphore = buggy_semaphore;
2685 if (xbox)
2686 chip->xbox = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 if (pci->vendor == PCI_VENDOR_ID_INTEL &&
2689 pci->device == PCI_DEVICE_ID_INTEL_440MX)
2690 chip->fix_nocache = 1; /* enable workaround */
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if ((err = pci_request_regions(pci, card->shortname)) < 0) {
2693 kfree(chip);
2694 pci_disable_device(pci);
2695 return err;
2696 }
2697
2698 if (device_type == DEVICE_ALI) {
2699 /* ALI5455 has no ac97 region */
2700 chip->bmaddr = pci_resource_start(pci, 0);
2701 goto port_inited;
2702 }
2703
2704 if (pci_resource_flags(pci, 2) & IORESOURCE_MEM) { /* ICH4 and Nforce */
2705 chip->mmio = 1;
2706 chip->addr = pci_resource_start(pci, 2);
2707 chip->remap_addr = ioremap_nocache(chip->addr,
2708 pci_resource_len(pci, 2));
2709 if (chip->remap_addr == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002710 snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 snd_intel8x0_free(chip);
2712 return -EIO;
2713 }
2714 } else {
2715 chip->addr = pci_resource_start(pci, 0);
2716 }
2717 if (pci_resource_flags(pci, 3) & IORESOURCE_MEM) { /* ICH4 */
2718 chip->bm_mmio = 1;
2719 chip->bmaddr = pci_resource_start(pci, 3);
2720 chip->remap_bmaddr = ioremap_nocache(chip->bmaddr,
2721 pci_resource_len(pci, 3));
2722 if (chip->remap_bmaddr == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002723 snd_printk(KERN_ERR "Controller space ioremap problem\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 snd_intel8x0_free(chip);
2725 return -EIO;
2726 }
2727 } else {
2728 chip->bmaddr = pci_resource_start(pci, 1);
2729 }
2730
2731 port_inited:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 chip->bdbars_count = bdbars[device_type];
2733
2734 /* initialize offsets */
2735 switch (device_type) {
2736 case DEVICE_NFORCE:
2737 tbl = nforce_regs;
2738 break;
2739 case DEVICE_ALI:
2740 tbl = ali_regs;
2741 break;
2742 default:
2743 tbl = intel_regs;
2744 break;
2745 }
2746 for (i = 0; i < chip->bdbars_count; i++) {
2747 ichdev = &chip->ichd[i];
2748 ichdev->ichd = i;
2749 ichdev->reg_offset = tbl[i].offset;
2750 ichdev->int_sta_mask = tbl[i].int_sta_mask;
2751 if (device_type == DEVICE_SIS) {
2752 /* SiS 7012 swaps the registers */
2753 ichdev->roff_sr = ICH_REG_OFF_PICB;
2754 ichdev->roff_picb = ICH_REG_OFF_SR;
2755 } else {
2756 ichdev->roff_sr = ICH_REG_OFF_SR;
2757 ichdev->roff_picb = ICH_REG_OFF_PICB;
2758 }
2759 if (device_type == DEVICE_ALI)
2760 ichdev->ali_slot = (ichdev->reg_offset - 0x40) / 0x10;
2761 /* SIS7012 handles the pcm data in bytes, others are in samples */
2762 ichdev->pos_shift = (device_type == DEVICE_SIS) ? 0 : 1;
2763 }
2764
2765 /* allocate buffer descriptor lists */
2766 /* the start of each lists must be aligned to 8 bytes */
2767 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2768 chip->bdbars_count * sizeof(u32) * ICH_MAX_FRAGS * 2,
2769 &chip->bdbars) < 0) {
2770 snd_intel8x0_free(chip);
2771 snd_printk(KERN_ERR "intel8x0: cannot allocate buffer descriptors\n");
2772 return -ENOMEM;
2773 }
2774 /* tables must be aligned to 8 bytes here, but the kernel pages
2775 are much bigger, so we don't care (on i386) */
2776 /* workaround for 440MX */
2777 if (chip->fix_nocache)
2778 fill_nocache(chip->bdbars.area, chip->bdbars.bytes, 1);
2779 int_sta_masks = 0;
2780 for (i = 0; i < chip->bdbars_count; i++) {
2781 ichdev = &chip->ichd[i];
Takashi Iwaibeef08a2005-10-27 20:55:38 +02002782 ichdev->bdbar = ((u32 *)chip->bdbars.area) +
2783 (i * ICH_MAX_FRAGS * 2);
2784 ichdev->bdbar_addr = chip->bdbars.addr +
2785 (i * sizeof(u32) * ICH_MAX_FRAGS * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 int_sta_masks |= ichdev->int_sta_mask;
2787 }
Takashi Iwaibeef08a2005-10-27 20:55:38 +02002788 chip->int_sta_reg = device_type == DEVICE_ALI ?
2789 ICH_REG_ALI_INTERRUPTSR : ICH_REG_GLOB_STA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 chip->int_sta_mask = int_sta_masks;
2791
Takashi Iwaibeef08a2005-10-27 20:55:38 +02002792 /* request irq after initializaing int_sta_mask, etc */
2793 if (request_irq(pci->irq, snd_intel8x0_interrupt,
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002794 SA_INTERRUPT|SA_SHIRQ, card->shortname, chip)) {
Takashi Iwaibeef08a2005-10-27 20:55:38 +02002795 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
2796 snd_intel8x0_free(chip);
2797 return -EBUSY;
2798 }
2799 chip->irq = pci->irq;
2800 pci_set_master(pci);
2801 synchronize_irq(chip->irq);
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 if ((err = snd_intel8x0_chip_init(chip, 1)) < 0) {
2804 snd_intel8x0_free(chip);
2805 return err;
2806 }
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2809 snd_intel8x0_free(chip);
2810 return err;
2811 }
2812
2813 snd_card_set_dev(card, &pci->dev);
2814
2815 *r_intel8x0 = chip;
2816 return 0;
2817}
2818
2819static struct shortname_table {
2820 unsigned int id;
2821 const char *s;
2822} shortnames[] __devinitdata = {
Takashi Iwai8cdfd252005-09-07 14:08:11 +02002823 { PCI_DEVICE_ID_INTEL_82801AA_5, "Intel 82801AA-ICH" },
2824 { PCI_DEVICE_ID_INTEL_82801AB_5, "Intel 82901AB-ICH0" },
2825 { PCI_DEVICE_ID_INTEL_82801BA_4, "Intel 82801BA-ICH2" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 { PCI_DEVICE_ID_INTEL_440MX, "Intel 440MX" },
Takashi Iwai8cdfd252005-09-07 14:08:11 +02002827 { PCI_DEVICE_ID_INTEL_82801CA_5, "Intel 82801CA-ICH3" },
2828 { PCI_DEVICE_ID_INTEL_82801DB_5, "Intel 82801DB-ICH4" },
2829 { PCI_DEVICE_ID_INTEL_82801EB_5, "Intel ICH5" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 { PCI_DEVICE_ID_INTEL_ESB_5, "Intel 6300ESB" },
2831 { PCI_DEVICE_ID_INTEL_ICH6_18, "Intel ICH6" },
2832 { PCI_DEVICE_ID_INTEL_ICH7_20, "Intel ICH7" },
Jason Gaston3437c5d2005-05-05 16:15:05 -07002833 { PCI_DEVICE_ID_INTEL_ESB2_14, "Intel ESB2" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 { PCI_DEVICE_ID_SI_7012, "SiS SI7012" },
Takashi Iwai8cdfd252005-09-07 14:08:11 +02002835 { PCI_DEVICE_ID_NVIDIA_MCP1_AUDIO, "NVidia nForce" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 { PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO, "NVidia nForce2" },
2837 { PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO, "NVidia nForce3" },
2838 { PCI_DEVICE_ID_NVIDIA_CK8S_AUDIO, "NVidia CK8S" },
2839 { PCI_DEVICE_ID_NVIDIA_CK804_AUDIO, "NVidia CK804" },
2840 { PCI_DEVICE_ID_NVIDIA_CK8_AUDIO, "NVidia CK8" },
2841 { 0x003a, "NVidia MCP04" },
2842 { 0x746d, "AMD AMD8111" },
2843 { 0x7445, "AMD AMD768" },
2844 { 0x5455, "ALi M5455" },
2845 { 0, NULL },
2846};
2847
2848static int __devinit snd_intel8x0_probe(struct pci_dev *pci,
2849 const struct pci_device_id *pci_id)
2850{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002851 struct snd_card *card;
2852 struct intel8x0 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 int err;
2854 struct shortname_table *name;
2855
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002856 card = snd_card_new(index, id, THIS_MODULE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (card == NULL)
2858 return -ENOMEM;
2859
2860 switch (pci_id->driver_data) {
2861 case DEVICE_NFORCE:
2862 strcpy(card->driver, "NFORCE");
2863 break;
2864 case DEVICE_INTEL_ICH4:
2865 strcpy(card->driver, "ICH4");
2866 break;
2867 default:
2868 strcpy(card->driver, "ICH");
2869 break;
2870 }
2871
2872 strcpy(card->shortname, "Intel ICH");
2873 for (name = shortnames; name->id; name++) {
2874 if (pci->device == name->id) {
2875 strcpy(card->shortname, name->s);
2876 break;
2877 }
2878 }
2879
Takashi Iwaibeef08a2005-10-27 20:55:38 +02002880 if (buggy_irq < 0) {
2881 /* some Nforce[2] and ICH boards have problems with IRQ handling.
2882 * Needs to return IRQ_HANDLED for unknown irqs.
2883 */
2884 if (pci_id->driver_data == DEVICE_NFORCE)
2885 buggy_irq = 1;
2886 else
2887 buggy_irq = 0;
2888 }
2889
Takashi Iwaia06147d2005-09-08 19:54:17 +02002890 if ((err = snd_intel8x0_create(card, pci, pci_id->driver_data,
Takashi Iwaic829b052005-10-18 18:03:35 +02002891 &chip)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 snd_card_free(card);
2893 return err;
2894 }
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002895 card->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002897 if ((err = snd_intel8x0_mixer(chip, ac97_clock, ac97_quirk)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 snd_card_free(card);
2899 return err;
2900 }
2901 if ((err = snd_intel8x0_pcm(chip)) < 0) {
2902 snd_card_free(card);
2903 return err;
2904 }
2905
2906 snd_intel8x0_proc_init(chip);
2907
2908 snprintf(card->longname, sizeof(card->longname),
2909 "%s with %s at %#lx, irq %i", card->shortname,
2910 snd_ac97_get_short_name(chip->ac97[0]), chip->addr, chip->irq);
2911
Clemens Ladischb7fe4622005-10-04 08:46:51 +02002912 if (! ac97_clock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 intel8x0_measure_ac97_clock(chip);
2914
2915 if ((err = snd_card_register(card)) < 0) {
2916 snd_card_free(card);
2917 return err;
2918 }
2919 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return 0;
2921}
2922
2923static void __devexit snd_intel8x0_remove(struct pci_dev *pci)
2924{
2925 snd_card_free(pci_get_drvdata(pci));
2926 pci_set_drvdata(pci, NULL);
2927}
2928
2929static struct pci_driver driver = {
2930 .name = "Intel ICH",
2931 .id_table = snd_intel8x0_ids,
2932 .probe = snd_intel8x0_probe,
2933 .remove = __devexit_p(snd_intel8x0_remove),
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002934#ifdef CONFIG_PM
2935 .suspend = intel8x0_suspend,
2936 .resume = intel8x0_resume,
2937#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938};
2939
2940
2941static int __init alsa_card_intel8x0_init(void)
2942{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002943 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944}
2945
2946static void __exit alsa_card_intel8x0_exit(void)
2947{
2948 pci_unregister_driver(&driver);
2949}
2950
2951module_init(alsa_card_intel8x0_init)
2952module_exit(alsa_card_intel8x0_exit)