blob: 07782ba9c74d1fbce3838bae0c91c483886bddec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA driver for Intel ICH (i8x0) chipsets
3 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/io.h>
30#include <linux/delay.h>
31#include <linux/interrupt.h>
32#include <linux/init.h>
33#include <linux/pci.h>
34#include <linux/slab.h>
35#include <linux/moduleparam.h>
36#include <sound/core.h>
37#include <sound/pcm.h>
38#include <sound/ac97_codec.h>
39#include <sound/info.h>
40#include <sound/initval.h>
41/* for 440MX workaround */
42#include <asm/pgtable.h>
43#include <asm/cacheflush.h>
44
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020045MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046MODULE_DESCRIPTION("Intel 82801AA,82901AB,i810,i820,i830,i840,i845,MX440; SiS 7012; Ali 5455");
47MODULE_LICENSE("GPL");
48MODULE_SUPPORTED_DEVICE("{{Intel,82801AA-ICH},"
49 "{Intel,82901AB-ICH0},"
50 "{Intel,82801BA-ICH2},"
51 "{Intel,82801CA-ICH3},"
52 "{Intel,82801DB-ICH4},"
53 "{Intel,ICH5},"
54 "{Intel,ICH6},"
55 "{Intel,ICH7},"
56 "{Intel,6300ESB},"
Jason Gastonc4c8ea92005-04-16 15:24:43 -070057 "{Intel,ESB2},"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 "{Intel,MX440},"
59 "{SiS,SI7012},"
60 "{NVidia,nForce Audio},"
61 "{NVidia,nForce2 Audio},"
62 "{AMD,AMD768},"
63 "{AMD,AMD8111},"
64 "{ALI,M5455}}");
65
Clemens Ladischb7fe4622005-10-04 08:46:51 +020066static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
67static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
Takashi Iwai6581f4e2006-05-17 17:14:51 +020068static int ac97_clock;
Clemens Ladischb7fe4622005-10-04 08:46:51 +020069static char *ac97_quirk;
70static int buggy_semaphore;
Takashi Iwaibeef08a2005-10-27 20:55:38 +020071static int buggy_irq = -1; /* auto-check */
Clemens Ladischb7fe4622005-10-04 08:46:51 +020072static int xbox;
Takashi Iwaia9e99662006-11-24 15:42:07 +010073static int spdif_aclink = -1;
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.");
Takashi Iwaia9e99662006-11-24 15:42:07 +010089module_param(spdif_aclink, int, 0444);
90MODULE_PARM_DESC(spdif_aclink, "S/PDIF over AC-link.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Takashi Iwai2b3e5842005-10-06 13:47:23 +020092/* just for backward compatibility */
93static int enable;
Takashi Iwai698444f2005-10-20 16:53:49 +020094module_param(enable, bool, 0444);
Takashi Iwai2b3e5842005-10-06 13:47:23 +020095static int joystick;
96module_param(joystick, int, 0444);
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
99 * Direct registers
100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101enum { DEVICE_INTEL, DEVICE_INTEL_ICH4, DEVICE_SIS, DEVICE_ALI, DEVICE_NFORCE };
102
103#define ICHREG(x) ICH_REG_##x
104
105#define DEFINE_REGSET(name,base) \
106enum { \
107 ICH_REG_##name##_BDBAR = base + 0x0, /* dword - buffer descriptor list base address */ \
108 ICH_REG_##name##_CIV = base + 0x04, /* byte - current index value */ \
109 ICH_REG_##name##_LVI = base + 0x05, /* byte - last valid index */ \
110 ICH_REG_##name##_SR = base + 0x06, /* byte - status register */ \
111 ICH_REG_##name##_PICB = base + 0x08, /* word - position in current buffer */ \
112 ICH_REG_##name##_PIV = base + 0x0a, /* byte - prefetched index value */ \
113 ICH_REG_##name##_CR = base + 0x0b, /* byte - control register */ \
114};
115
116/* busmaster blocks */
117DEFINE_REGSET(OFF, 0); /* offset */
118DEFINE_REGSET(PI, 0x00); /* PCM in */
119DEFINE_REGSET(PO, 0x10); /* PCM out */
120DEFINE_REGSET(MC, 0x20); /* Mic in */
121
122/* ICH4 busmaster blocks */
123DEFINE_REGSET(MC2, 0x40); /* Mic in 2 */
124DEFINE_REGSET(PI2, 0x50); /* PCM in 2 */
125DEFINE_REGSET(SP, 0x60); /* SPDIF out */
126
127/* values for each busmaster block */
128
129/* LVI */
130#define ICH_REG_LVI_MASK 0x1f
131
132/* SR */
133#define ICH_FIFOE 0x10 /* FIFO error */
134#define ICH_BCIS 0x08 /* buffer completion interrupt status */
135#define ICH_LVBCI 0x04 /* last valid buffer completion interrupt */
136#define ICH_CELV 0x02 /* current equals last valid */
137#define ICH_DCH 0x01 /* DMA controller halted */
138
139/* PIV */
140#define ICH_REG_PIV_MASK 0x1f /* mask */
141
142/* CR */
143#define ICH_IOCE 0x10 /* interrupt on completion enable */
144#define ICH_FEIE 0x08 /* fifo error interrupt enable */
145#define ICH_LVBIE 0x04 /* last valid buffer interrupt enable */
146#define ICH_RESETREGS 0x02 /* reset busmaster registers */
147#define ICH_STARTBM 0x01 /* start busmaster operation */
148
149
150/* global block */
151#define ICH_REG_GLOB_CNT 0x2c /* dword - global control */
152#define ICH_PCM_SPDIF_MASK 0xc0000000 /* s/pdif pcm slot mask (ICH4) */
153#define ICH_PCM_SPDIF_NONE 0x00000000 /* reserved - undefined */
154#define ICH_PCM_SPDIF_78 0x40000000 /* s/pdif pcm on slots 7&8 */
155#define ICH_PCM_SPDIF_69 0x80000000 /* s/pdif pcm on slots 6&9 */
156#define ICH_PCM_SPDIF_1011 0xc0000000 /* s/pdif pcm on slots 10&11 */
157#define ICH_PCM_20BIT 0x00400000 /* 20-bit samples (ICH4) */
Takashi Iwai4235a312008-02-18 12:23:13 +0100158#define ICH_PCM_246_MASK 0x00300000 /* chan mask (not all chips) */
159#define ICH_PCM_8 0x00300000 /* 8 channels (not all chips) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define ICH_PCM_6 0x00200000 /* 6 channels (not all chips) */
161#define ICH_PCM_4 0x00100000 /* 4 channels (not all chips) */
162#define ICH_PCM_2 0x00000000 /* 2 channels (stereo) */
163#define ICH_SIS_PCM_246_MASK 0x000000c0 /* 6 channels (SIS7012) */
164#define ICH_SIS_PCM_6 0x00000080 /* 6 channels (SIS7012) */
165#define ICH_SIS_PCM_4 0x00000040 /* 4 channels (SIS7012) */
166#define ICH_SIS_PCM_2 0x00000000 /* 2 channels (SIS7012) */
167#define ICH_TRIE 0x00000040 /* tertiary resume interrupt enable */
168#define ICH_SRIE 0x00000020 /* secondary resume interrupt enable */
169#define ICH_PRIE 0x00000010 /* primary resume interrupt enable */
170#define ICH_ACLINK 0x00000008 /* AClink shut off */
171#define ICH_AC97WARM 0x00000004 /* AC'97 warm reset */
172#define ICH_AC97COLD 0x00000002 /* AC'97 cold reset */
173#define ICH_GIE 0x00000001 /* GPI interrupt enable */
174#define ICH_REG_GLOB_STA 0x30 /* dword - global status */
175#define ICH_TRI 0x20000000 /* ICH4: tertiary (AC_SDIN2) resume interrupt */
176#define ICH_TCR 0x10000000 /* ICH4: tertiary (AC_SDIN2) codec ready */
177#define ICH_BCS 0x08000000 /* ICH4: bit clock stopped */
178#define ICH_SPINT 0x04000000 /* ICH4: S/PDIF interrupt */
179#define ICH_P2INT 0x02000000 /* ICH4: PCM2-In interrupt */
180#define ICH_M2INT 0x01000000 /* ICH4: Mic2-In interrupt */
181#define ICH_SAMPLE_CAP 0x00c00000 /* ICH4: sample capability bits (RO) */
182#define ICH_SAMPLE_16_20 0x00400000 /* ICH4: 16- and 20-bit samples */
183#define ICH_MULTICHAN_CAP 0x00300000 /* ICH4: multi-channel capability bits (RO) */
Takashi Iwai84a43bd2006-01-12 11:47:32 +0100184#define ICH_SIS_TRI 0x00080000 /* SIS: tertiary resume irq */
185#define ICH_SIS_TCR 0x00040000 /* SIS: tertiary codec ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#define ICH_MD3 0x00020000 /* modem power down semaphore */
187#define ICH_AD3 0x00010000 /* audio power down semaphore */
188#define ICH_RCS 0x00008000 /* read completion status */
189#define ICH_BIT3 0x00004000 /* bit 3 slot 12 */
190#define ICH_BIT2 0x00002000 /* bit 2 slot 12 */
191#define ICH_BIT1 0x00001000 /* bit 1 slot 12 */
192#define ICH_SRI 0x00000800 /* secondary (AC_SDIN1) resume interrupt */
193#define ICH_PRI 0x00000400 /* primary (AC_SDIN0) resume interrupt */
194#define ICH_SCR 0x00000200 /* secondary (AC_SDIN1) codec ready */
195#define ICH_PCR 0x00000100 /* primary (AC_SDIN0) codec ready */
196#define ICH_MCINT 0x00000080 /* MIC capture interrupt */
197#define ICH_POINT 0x00000040 /* playback interrupt */
198#define ICH_PIINT 0x00000020 /* capture interrupt */
199#define ICH_NVSPINT 0x00000010 /* nforce spdif interrupt */
200#define ICH_MOINT 0x00000004 /* modem playback interrupt */
201#define ICH_MIINT 0x00000002 /* modem capture interrupt */
202#define ICH_GSCI 0x00000001 /* GPI status change interrupt */
203#define ICH_REG_ACC_SEMA 0x34 /* byte - codec write semaphore */
204#define ICH_CAS 0x01 /* codec access semaphore */
205#define ICH_REG_SDM 0x80
206#define ICH_DI2L_MASK 0x000000c0 /* PCM In 2, Mic In 2 data in line */
207#define ICH_DI2L_SHIFT 6
208#define ICH_DI1L_MASK 0x00000030 /* PCM In 1, Mic In 1 data in line */
209#define ICH_DI1L_SHIFT 4
210#define ICH_SE 0x00000008 /* steer enable */
211#define ICH_LDI_MASK 0x00000003 /* last codec read data input */
212
213#define ICH_MAX_FRAGS 32 /* max hw frags */
214
215
216/*
217 * registers for Ali5455
218 */
219
220/* ALi 5455 busmaster blocks */
221DEFINE_REGSET(AL_PI, 0x40); /* ALi PCM in */
222DEFINE_REGSET(AL_PO, 0x50); /* Ali PCM out */
223DEFINE_REGSET(AL_MC, 0x60); /* Ali Mic in */
224DEFINE_REGSET(AL_CDC_SPO, 0x70); /* Ali Codec SPDIF out */
225DEFINE_REGSET(AL_CENTER, 0x80); /* Ali center out */
226DEFINE_REGSET(AL_LFE, 0x90); /* Ali center out */
227DEFINE_REGSET(AL_CLR_SPI, 0xa0); /* Ali Controller SPDIF in */
228DEFINE_REGSET(AL_CLR_SPO, 0xb0); /* Ali Controller SPDIF out */
229DEFINE_REGSET(AL_I2S, 0xc0); /* Ali I2S in */
230DEFINE_REGSET(AL_PI2, 0xd0); /* Ali PCM2 in */
231DEFINE_REGSET(AL_MC2, 0xe0); /* Ali Mic2 in */
232
233enum {
234 ICH_REG_ALI_SCR = 0x00, /* System Control Register */
235 ICH_REG_ALI_SSR = 0x04, /* System Status Register */
236 ICH_REG_ALI_DMACR = 0x08, /* DMA Control Register */
237 ICH_REG_ALI_FIFOCR1 = 0x0c, /* FIFO Control Register 1 */
238 ICH_REG_ALI_INTERFACECR = 0x10, /* Interface Control Register */
239 ICH_REG_ALI_INTERRUPTCR = 0x14, /* Interrupt control Register */
240 ICH_REG_ALI_INTERRUPTSR = 0x18, /* Interrupt Status Register */
241 ICH_REG_ALI_FIFOCR2 = 0x1c, /* FIFO Control Register 2 */
242 ICH_REG_ALI_CPR = 0x20, /* Command Port Register */
243 ICH_REG_ALI_CPR_ADDR = 0x22, /* ac97 addr write */
244 ICH_REG_ALI_SPR = 0x24, /* Status Port Register */
245 ICH_REG_ALI_SPR_ADDR = 0x26, /* ac97 addr read */
246 ICH_REG_ALI_FIFOCR3 = 0x2c, /* FIFO Control Register 3 */
247 ICH_REG_ALI_TTSR = 0x30, /* Transmit Tag Slot Register */
248 ICH_REG_ALI_RTSR = 0x34, /* Receive Tag Slot Register */
249 ICH_REG_ALI_CSPSR = 0x38, /* Command/Status Port Status Register */
250 ICH_REG_ALI_CAS = 0x3c, /* Codec Write Semaphore Register */
251 ICH_REG_ALI_HWVOL = 0xf0, /* hardware volume control/status */
252 ICH_REG_ALI_I2SCR = 0xf4, /* I2S control/status */
253 ICH_REG_ALI_SPDIFCSR = 0xf8, /* spdif channel status register */
254 ICH_REG_ALI_SPDIFICS = 0xfc, /* spdif interface control/status */
255};
256
257#define ALI_CAS_SEM_BUSY 0x80000000
258#define ALI_CPR_ADDR_SECONDARY 0x100
259#define ALI_CPR_ADDR_READ 0x80
260#define ALI_CSPSR_CODEC_READY 0x08
261#define ALI_CSPSR_READ_OK 0x02
262#define ALI_CSPSR_WRITE_OK 0x01
263
264/* interrupts for the whole chip by interrupt status register finish */
265
266#define ALI_INT_MICIN2 (1<<26)
267#define ALI_INT_PCMIN2 (1<<25)
268#define ALI_INT_I2SIN (1<<24)
269#define ALI_INT_SPDIFOUT (1<<23) /* controller spdif out INTERRUPT */
270#define ALI_INT_SPDIFIN (1<<22)
271#define ALI_INT_LFEOUT (1<<21)
272#define ALI_INT_CENTEROUT (1<<20)
273#define ALI_INT_CODECSPDIFOUT (1<<19)
274#define ALI_INT_MICIN (1<<18)
275#define ALI_INT_PCMOUT (1<<17)
276#define ALI_INT_PCMIN (1<<16)
277#define ALI_INT_CPRAIS (1<<7) /* command port available */
278#define ALI_INT_SPRAIS (1<<5) /* status port available */
279#define ALI_INT_GPIO (1<<1)
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100280#define ALI_INT_MASK (ALI_INT_SPDIFOUT|ALI_INT_CODECSPDIFOUT|\
281 ALI_INT_MICIN|ALI_INT_PCMOUT|ALI_INT_PCMIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283#define ICH_ALI_SC_RESET (1<<31) /* master reset */
284#define ICH_ALI_SC_AC97_DBL (1<<30)
285#define ICH_ALI_SC_CODEC_SPDF (3<<20) /* 1=7/8, 2=6/9, 3=10/11 */
286#define ICH_ALI_SC_IN_BITS (3<<18)
287#define ICH_ALI_SC_OUT_BITS (3<<16)
288#define ICH_ALI_SC_6CH_CFG (3<<14)
289#define ICH_ALI_SC_PCM_4 (1<<8)
290#define ICH_ALI_SC_PCM_6 (2<<8)
291#define ICH_ALI_SC_PCM_246_MASK (3<<8)
292
293#define ICH_ALI_SS_SEC_ID (3<<5)
294#define ICH_ALI_SS_PRI_ID (3<<3)
295
296#define ICH_ALI_IF_AC97SP (1<<21)
297#define ICH_ALI_IF_MC (1<<20)
298#define ICH_ALI_IF_PI (1<<19)
299#define ICH_ALI_IF_MC2 (1<<18)
300#define ICH_ALI_IF_PI2 (1<<17)
301#define ICH_ALI_IF_LINE_SRC (1<<15) /* 0/1 = slot 3/6 */
302#define ICH_ALI_IF_MIC_SRC (1<<14) /* 0/1 = slot 3/6 */
303#define ICH_ALI_IF_SPDF_SRC (3<<12) /* 00 = PCM, 01 = AC97-in, 10 = spdif-in, 11 = i2s */
304#define ICH_ALI_IF_AC97_OUT (3<<8) /* 00 = PCM, 10 = spdif-in, 11 = i2s */
305#define ICH_ALI_IF_PO_SPDF (1<<3)
306#define ICH_ALI_IF_PO (1<<1)
307
308/*
309 *
310 */
311
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100312enum {
313 ICHD_PCMIN,
314 ICHD_PCMOUT,
315 ICHD_MIC,
316 ICHD_MIC2,
317 ICHD_PCM2IN,
318 ICHD_SPBAR,
319 ICHD_LAST = ICHD_SPBAR
320};
321enum {
322 NVD_PCMIN,
323 NVD_PCMOUT,
324 NVD_MIC,
325 NVD_SPBAR,
326 NVD_LAST = NVD_SPBAR
327};
328enum {
329 ALID_PCMIN,
330 ALID_PCMOUT,
331 ALID_MIC,
332 ALID_AC97SPDIFOUT,
333 ALID_SPDIFIN,
334 ALID_SPDIFOUT,
335 ALID_LAST = ALID_SPDIFOUT
336};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100338#define get_ichdev(substream) (substream->runtime->private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100340struct ichdev {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 unsigned int ichd; /* ich device number */
342 unsigned long reg_offset; /* offset to bmaddr */
343 u32 *bdbar; /* CPU address (32bit) */
344 unsigned int bdbar_addr; /* PCI bus address (32bit) */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100345 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 unsigned int physbuf; /* physical address (32bit) */
347 unsigned int size;
348 unsigned int fragsize;
349 unsigned int fragsize1;
350 unsigned int position;
351 unsigned int pos_shift;
352 int frags;
353 int lvi;
354 int lvi_frag;
355 int civ;
356 int ack;
357 int ack_reload;
358 unsigned int ack_bit;
359 unsigned int roff_sr;
360 unsigned int roff_picb;
361 unsigned int int_sta_mask; /* interrupt status mask */
362 unsigned int ali_slot; /* ALI DMA slot */
363 struct ac97_pcm *pcm;
364 int pcm_open_flag;
365 unsigned int page_attr_changed: 1;
Takashi Iwai1cfe43d2005-08-16 16:52:24 +0200366 unsigned int suspended: 1;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100367};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100369struct intel8x0 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned int device_type;
371
372 int irq;
373
Takashi Iwai3388c372006-10-06 17:06:39 +0200374 void __iomem *addr;
375 void __iomem *bmaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 struct pci_dev *pci;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100378 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 int pcm_devs;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100381 struct snd_pcm *pcm[6];
382 struct ichdev ichd[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 unsigned multi4: 1,
385 multi6: 1,
Takashi Iwai4235a312008-02-18 12:23:13 +0100386 multi8 :1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 dra: 1,
388 smp20bit: 1;
389 unsigned in_ac97_init: 1,
390 in_sdin_init: 1;
391 unsigned in_measurement: 1; /* during ac97 clock measurement */
392 unsigned fix_nocache: 1; /* workaround for 440MX */
393 unsigned buggy_irq: 1; /* workaround for buggy mobos */
394 unsigned xbox: 1; /* workaround for Xbox AC'97 detection */
Takashi Iwaia06147d2005-09-08 19:54:17 +0200395 unsigned buggy_semaphore: 1; /* workaround for buggy codec semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 int spdif_idx; /* SPDIF BAR index; *_SPBAR or -1 if use PCMOUT */
Takashi Iwai52b72382005-06-30 13:47:06 +0200398 unsigned int sdm_saved; /* SDM reg value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100400 struct snd_ac97_bus *ac97_bus;
401 struct snd_ac97 *ac97[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 unsigned int ac97_sdin[3];
Takashi Iwai84a43bd2006-01-12 11:47:32 +0100403 unsigned int max_codecs, ncodecs;
404 unsigned int *codec_bit;
405 unsigned int codec_isr_bits;
406 unsigned int codec_ready_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 spinlock_t reg_lock;
409
410 u32 bdbars_count;
411 struct snd_dma_buffer bdbars;
412 u32 int_sta_reg; /* interrupt status register */
413 u32 int_sta_mask; /* interrupt status mask */
414};
415
Takashi Iwaif40b6892006-07-05 16:51:05 +0200416static struct pci_device_id snd_intel8x0_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 { 0x8086, 0x2415, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801AA */
418 { 0x8086, 0x2425, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82901AB */
419 { 0x8086, 0x2445, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 82801BA */
420 { 0x8086, 0x2485, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* ICH3 */
421 { 0x8086, 0x24c5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH4 */
422 { 0x8086, 0x24d5, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH5 */
423 { 0x8086, 0x25a6, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ESB */
424 { 0x8086, 0x266e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH6 */
425 { 0x8086, 0x27de, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ICH7 */
Jason Gastonc4c8ea92005-04-16 15:24:43 -0700426 { 0x8086, 0x2698, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL_ICH4 }, /* ESB2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 { 0x8086, 0x7195, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* 440MX */
428 { 0x1039, 0x7012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_SIS }, /* SI7012 */
429 { 0x10de, 0x01b1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE */
430 { 0x10de, 0x003a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* MCP04 */
431 { 0x10de, 0x006a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE2 */
432 { 0x10de, 0x0059, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* CK804 */
433 { 0x10de, 0x008a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* CK8 */
434 { 0x10de, 0x00da, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* NFORCE3 */
435 { 0x10de, 0x00ea, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* CK8S */
Takashi Iwai89ac9c22006-01-24 10:36:57 +0100436 { 0x10de, 0x026b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_NFORCE }, /* MCP51 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 { 0x1022, 0x746d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD8111 */
438 { 0x1022, 0x7445, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_INTEL }, /* AMD768 */
439 { 0x10b9, 0x5455, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALI }, /* Ali5455 */
440 { 0, }
441};
442
443MODULE_DEVICE_TABLE(pci, snd_intel8x0_ids);
444
445/*
446 * Lowlevel I/O - busmaster
447 */
448
Takashi Iwai3388c372006-10-06 17:06:39 +0200449static inline u8 igetbyte(struct intel8x0 *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Takashi Iwai3388c372006-10-06 17:06:39 +0200451 return ioread8(chip->bmaddr + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Takashi Iwai3388c372006-10-06 17:06:39 +0200454static inline u16 igetword(struct intel8x0 *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Takashi Iwai3388c372006-10-06 17:06:39 +0200456 return ioread16(chip->bmaddr + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Takashi Iwai3388c372006-10-06 17:06:39 +0200459static inline u32 igetdword(struct intel8x0 *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Takashi Iwai3388c372006-10-06 17:06:39 +0200461 return ioread32(chip->bmaddr + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Takashi Iwai3388c372006-10-06 17:06:39 +0200464static inline void iputbyte(struct intel8x0 *chip, u32 offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Takashi Iwai3388c372006-10-06 17:06:39 +0200466 iowrite8(val, chip->bmaddr + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Takashi Iwai3388c372006-10-06 17:06:39 +0200469static inline void iputword(struct intel8x0 *chip, u32 offset, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Takashi Iwai3388c372006-10-06 17:06:39 +0200471 iowrite16(val, chip->bmaddr + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Takashi Iwai3388c372006-10-06 17:06:39 +0200474static inline void iputdword(struct intel8x0 *chip, u32 offset, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Takashi Iwai3388c372006-10-06 17:06:39 +0200476 iowrite32(val, chip->bmaddr + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479/*
480 * Lowlevel I/O - AC'97 registers
481 */
482
Takashi Iwai3388c372006-10-06 17:06:39 +0200483static inline u16 iagetword(struct intel8x0 *chip, u32 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Takashi Iwai3388c372006-10-06 17:06:39 +0200485 return ioread16(chip->addr + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Takashi Iwai3388c372006-10-06 17:06:39 +0200488static inline void iaputword(struct intel8x0 *chip, u32 offset, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Takashi Iwai3388c372006-10-06 17:06:39 +0200490 iowrite16(val, chip->addr + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493/*
494 * Basic I/O
495 */
496
497/*
498 * access to AC97 codec via normal i/o (for ICH and SIS7012)
499 */
500
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100501static int snd_intel8x0_codec_semaphore(struct intel8x0 *chip, unsigned int codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 int time;
504
505 if (codec > 2)
506 return -EIO;
507 if (chip->in_sdin_init) {
508 /* we don't know the ready bit assignment at the moment */
509 /* so we check any */
Takashi Iwai84a43bd2006-01-12 11:47:32 +0100510 codec = chip->codec_isr_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 } else {
Takashi Iwai84a43bd2006-01-12 11:47:32 +0100512 codec = chip->codec_bit[chip->ac97_sdin[codec]];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
515 /* codec ready ? */
516 if ((igetdword(chip, ICHREG(GLOB_STA)) & codec) == 0)
517 return -EIO;
518
Takashi Iwaia06147d2005-09-08 19:54:17 +0200519 if (chip->buggy_semaphore)
520 return 0; /* just ignore ... */
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Anyone holding a semaphore for 1 msec should be shot... */
523 time = 100;
524 do {
525 if (!(igetbyte(chip, ICHREG(ACC_SEMA)) & ICH_CAS))
526 return 0;
527 udelay(10);
528 } while (time--);
529
530 /* access to some forbidden (non existant) ac97 registers will not
531 * reset the semaphore. So even if you don't get the semaphore, still
532 * continue the access. We don't need the semaphore anyway. */
Takashi Iwai99b359b2005-10-20 18:26:44 +0200533 snd_printk(KERN_ERR "codec_semaphore: semaphore is not ready [0x%x][0x%x]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 igetbyte(chip, ICHREG(ACC_SEMA)), igetdword(chip, ICHREG(GLOB_STA)));
535 iagetword(chip, 0); /* clear semaphore flag */
536 /* I don't care about the semaphore */
537 return -EBUSY;
538}
539
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100540static void snd_intel8x0_codec_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 unsigned short reg,
542 unsigned short val)
543{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100544 struct intel8x0 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 if (snd_intel8x0_codec_semaphore(chip, ac97->num) < 0) {
547 if (! chip->in_ac97_init)
Takashi Iwai99b359b2005-10-20 18:26:44 +0200548 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 -0700549 }
550 iaputword(chip, reg + ac97->num * 0x80, val);
551}
552
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100553static unsigned short snd_intel8x0_codec_read(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 unsigned short reg)
555{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100556 struct intel8x0 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 unsigned short res;
558 unsigned int tmp;
559
560 if (snd_intel8x0_codec_semaphore(chip, ac97->num) < 0) {
561 if (! chip->in_ac97_init)
Takashi Iwai99b359b2005-10-20 18:26:44 +0200562 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 -0700563 res = 0xffff;
564 } else {
565 res = iagetword(chip, reg + ac97->num * 0x80);
566 if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
567 /* reset RCS and preserve other R/WC bits */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100568 iputdword(chip, ICHREG(GLOB_STA), tmp &
Takashi Iwai84a43bd2006-01-12 11:47:32 +0100569 ~(chip->codec_ready_bits | ICH_GSCI));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (! chip->in_ac97_init)
Takashi Iwai99b359b2005-10-20 18:26:44 +0200571 snd_printk(KERN_ERR "codec_read %d: read timeout for register 0x%x\n", ac97->num, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 res = 0xffff;
573 }
574 }
575 return res;
576}
577
Takashi Iwai84a43bd2006-01-12 11:47:32 +0100578static void __devinit snd_intel8x0_codec_read_test(struct intel8x0 *chip,
579 unsigned int codec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 unsigned int tmp;
582
583 if (snd_intel8x0_codec_semaphore(chip, codec) >= 0) {
584 iagetword(chip, codec * 0x80);
585 if ((tmp = igetdword(chip, ICHREG(GLOB_STA))) & ICH_RCS) {
586 /* reset RCS and preserve other R/WC bits */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100587 iputdword(chip, ICHREG(GLOB_STA), tmp &
Takashi Iwai84a43bd2006-01-12 11:47:32 +0100588 ~(chip->codec_ready_bits | ICH_GSCI));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590 }
591}
592
593/*
594 * access to AC97 for Ali5455
595 */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100596static int snd_intel8x0_ali_codec_ready(struct intel8x0 *chip, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 int count = 0;
599 for (count = 0; count < 0x7f; count++) {
600 int val = igetbyte(chip, ICHREG(ALI_CSPSR));
601 if (val & mask)
602 return 0;
603 }
Takashi Iwai79ba34b2005-10-05 19:47:41 +0200604 if (! chip->in_ac97_init)
605 snd_printd(KERN_WARNING "intel8x0: AC97 codec ready timeout.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return -EBUSY;
607}
608
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100609static int snd_intel8x0_ali_codec_semaphore(struct intel8x0 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 int time = 100;
Takashi Iwai79ba34b2005-10-05 19:47:41 +0200612 if (chip->buggy_semaphore)
613 return 0; /* just ignore ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 while (time-- && (igetdword(chip, ICHREG(ALI_CAS)) & ALI_CAS_SEM_BUSY))
615 udelay(1);
Takashi Iwai79ba34b2005-10-05 19:47:41 +0200616 if (! time && ! chip->in_ac97_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 snd_printk(KERN_WARNING "ali_codec_semaphore timeout\n");
618 return snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_CODEC_READY);
619}
620
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100621static unsigned short snd_intel8x0_ali_codec_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100623 struct intel8x0 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 unsigned short data = 0xffff;
625
626 if (snd_intel8x0_ali_codec_semaphore(chip))
627 goto __err;
628 reg |= ALI_CPR_ADDR_READ;
629 if (ac97->num)
630 reg |= ALI_CPR_ADDR_SECONDARY;
631 iputword(chip, ICHREG(ALI_CPR_ADDR), reg);
632 if (snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_READ_OK))
633 goto __err;
634 data = igetword(chip, ICHREG(ALI_SPR));
635 __err:
636 return data;
637}
638
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100639static void snd_intel8x0_ali_codec_write(struct snd_ac97 *ac97, unsigned short reg,
640 unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100642 struct intel8x0 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 if (snd_intel8x0_ali_codec_semaphore(chip))
645 return;
646 iputword(chip, ICHREG(ALI_CPR), val);
647 if (ac97->num)
648 reg |= ALI_CPR_ADDR_SECONDARY;
649 iputword(chip, ICHREG(ALI_CPR_ADDR), reg);
650 snd_intel8x0_ali_codec_ready(chip, ALI_CSPSR_WRITE_OK);
651}
652
653
654/*
655 * DMA I/O
656 */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100657static void snd_intel8x0_setup_periods(struct intel8x0 *chip, struct ichdev *ichdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 int idx;
660 u32 *bdbar = ichdev->bdbar;
661 unsigned long port = ichdev->reg_offset;
662
663 iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
664 if (ichdev->size == ichdev->fragsize) {
665 ichdev->ack_reload = ichdev->ack = 2;
666 ichdev->fragsize1 = ichdev->fragsize >> 1;
667 for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 4) {
668 bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf);
669 bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
670 ichdev->fragsize1 >> ichdev->pos_shift);
671 bdbar[idx + 2] = cpu_to_le32(ichdev->physbuf + (ichdev->size >> 1));
672 bdbar[idx + 3] = cpu_to_le32(0x80000000 | /* interrupt on completion */
673 ichdev->fragsize1 >> ichdev->pos_shift);
674 }
675 ichdev->frags = 2;
676 } else {
677 ichdev->ack_reload = ichdev->ack = 1;
678 ichdev->fragsize1 = ichdev->fragsize;
679 for (idx = 0; idx < (ICH_REG_LVI_MASK + 1) * 2; idx += 2) {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100680 bdbar[idx + 0] = cpu_to_le32(ichdev->physbuf +
681 (((idx >> 1) * ichdev->fragsize) %
682 ichdev->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 bdbar[idx + 1] = cpu_to_le32(0x80000000 | /* interrupt on completion */
684 ichdev->fragsize >> ichdev->pos_shift);
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100685#if 0
686 printk("bdbar[%i] = 0x%x [0x%x]\n",
687 idx + 0, bdbar[idx + 0], bdbar[idx + 1]);
688#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690 ichdev->frags = ichdev->size / ichdev->fragsize;
691 }
692 iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi = ICH_REG_LVI_MASK);
693 ichdev->civ = 0;
694 iputbyte(chip, port + ICH_REG_OFF_CIV, 0);
695 ichdev->lvi_frag = ICH_REG_LVI_MASK % ichdev->frags;
696 ichdev->position = 0;
697#if 0
698 printk("lvi_frag = %i, frags = %i, period_size = 0x%x, period_size1 = 0x%x\n",
699 ichdev->lvi_frag, ichdev->frags, ichdev->fragsize, ichdev->fragsize1);
700#endif
701 /* clear interrupts */
702 iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
703}
704
705#ifdef __i386__
706/*
707 * Intel 82443MX running a 100MHz processor system bus has a hardware bug,
708 * which aborts PCI busmaster for audio transfer. A workaround is to set
709 * the pages as non-cached. For details, see the errata in
710 * http://www.intel.com/design/chipsets/specupdt/245051.htm
711 */
712static void fill_nocache(void *buf, int size, int nocache)
713{
714 size = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100715 if (nocache)
716 set_pages_uc(virt_to_page(buf), size);
717 else
718 set_pages_wb(virt_to_page(buf), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720#else
Arjan van de Ven6d238cc2008-01-30 13:34:06 +0100721#define fill_nocache(buf, size, nocache) do { ; } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722#endif
723
724/*
725 * Interrupt handler
726 */
727
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100728static inline void snd_intel8x0_update(struct intel8x0 *chip, struct ichdev *ichdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 unsigned long port = ichdev->reg_offset;
Takashi Iwai883be792007-03-13 21:52:36 +0100731 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 int status, civ, i, step;
733 int ack = 0;
734
Takashi Iwai883be792007-03-13 21:52:36 +0100735 spin_lock_irqsave(&chip->reg_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 status = igetbyte(chip, port + ichdev->roff_sr);
737 civ = igetbyte(chip, port + ICH_REG_OFF_CIV);
738 if (!(status & ICH_BCIS)) {
739 step = 0;
740 } else if (civ == ichdev->civ) {
741 // snd_printd("civ same %d\n", civ);
742 step = 1;
743 ichdev->civ++;
744 ichdev->civ &= ICH_REG_LVI_MASK;
745 } else {
746 step = civ - ichdev->civ;
747 if (step < 0)
748 step += ICH_REG_LVI_MASK + 1;
749 // if (step != 1)
750 // snd_printd("step = %d, %d -> %d\n", step, ichdev->civ, civ);
751 ichdev->civ = civ;
752 }
753
754 ichdev->position += step * ichdev->fragsize1;
755 if (! chip->in_measurement)
756 ichdev->position %= ichdev->size;
757 ichdev->lvi += step;
758 ichdev->lvi &= ICH_REG_LVI_MASK;
759 iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
760 for (i = 0; i < step; i++) {
761 ichdev->lvi_frag++;
762 ichdev->lvi_frag %= ichdev->frags;
763 ichdev->bdbar[ichdev->lvi * 2] = cpu_to_le32(ichdev->physbuf + ichdev->lvi_frag * ichdev->fragsize1);
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100764#if 0
765 printk("new: bdbar[%i] = 0x%x [0x%x], prefetch = %i, all = 0x%x, 0x%x\n",
766 ichdev->lvi * 2, ichdev->bdbar[ichdev->lvi * 2],
767 ichdev->bdbar[ichdev->lvi * 2 + 1], inb(ICH_REG_OFF_PIV + port),
768 inl(port + 4), inb(port + ICH_REG_OFF_CR));
769#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (--ichdev->ack == 0) {
771 ichdev->ack = ichdev->ack_reload;
772 ack = 1;
773 }
774 }
Takashi Iwai883be792007-03-13 21:52:36 +0100775 spin_unlock_irqrestore(&chip->reg_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (ack && ichdev->substream) {
777 snd_pcm_period_elapsed(ichdev->substream);
778 }
779 iputbyte(chip, port + ichdev->roff_sr,
780 status & (ICH_FIFOE | ICH_BCIS | ICH_LVBCI));
781}
782
David Howells7d12e782006-10-05 14:55:46 +0100783static irqreturn_t snd_intel8x0_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100785 struct intel8x0 *chip = dev_id;
786 struct ichdev *ichdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 unsigned int status;
788 unsigned int i;
789
790 status = igetdword(chip, chip->int_sta_reg);
791 if (status == 0xffffffff) /* we are not yet resumed */
792 return IRQ_NONE;
793
794 if ((status & chip->int_sta_mask) == 0) {
795 if (status) {
796 /* ack */
797 iputdword(chip, chip->int_sta_reg, status);
798 if (! chip->buggy_irq)
799 status = 0;
800 }
801 return IRQ_RETVAL(status);
802 }
803
804 for (i = 0; i < chip->bdbars_count; i++) {
805 ichdev = &chip->ichd[i];
806 if (status & ichdev->int_sta_mask)
807 snd_intel8x0_update(chip, ichdev);
808 }
809
810 /* ack them */
811 iputdword(chip, chip->int_sta_reg, status & chip->int_sta_mask);
812
813 return IRQ_HANDLED;
814}
815
816/*
817 * PCM part
818 */
819
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100820static int snd_intel8x0_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100822 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
823 struct ichdev *ichdev = get_ichdev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 unsigned char val = 0;
825 unsigned long port = ichdev->reg_offset;
826
827 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwai1cfe43d2005-08-16 16:52:24 +0200829 ichdev->suspended = 0;
830 /* fallthru */
831 case SNDRV_PCM_TRIGGER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 val = ICH_IOCE | ICH_STARTBM;
833 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwai1cfe43d2005-08-16 16:52:24 +0200835 ichdev->suspended = 1;
836 /* fallthru */
837 case SNDRV_PCM_TRIGGER_STOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 val = 0;
839 break;
840 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
841 val = ICH_IOCE;
842 break;
843 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
844 val = ICH_IOCE | ICH_STARTBM;
845 break;
846 default:
847 return -EINVAL;
848 }
849 iputbyte(chip, port + ICH_REG_OFF_CR, val);
850 if (cmd == SNDRV_PCM_TRIGGER_STOP) {
851 /* wait until DMA stopped */
852 while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH)) ;
853 /* reset whole DMA things */
854 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
855 }
856 return 0;
857}
858
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100859static int snd_intel8x0_ali_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100861 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
862 struct ichdev *ichdev = get_ichdev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 unsigned long port = ichdev->reg_offset;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100864 static int fiforeg[] = {
865 ICHREG(ALI_FIFOCR1), ICHREG(ALI_FIFOCR2), ICHREG(ALI_FIFOCR3)
866 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 unsigned int val, fifo;
868
869 val = igetdword(chip, ICHREG(ALI_DMACR));
870 switch (cmd) {
Takashi Iwai1cfe43d2005-08-16 16:52:24 +0200871 case SNDRV_PCM_TRIGGER_RESUME:
872 ichdev->suspended = 0;
873 /* fallthru */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 case SNDRV_PCM_TRIGGER_START:
875 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
877 /* clear FIFO for synchronization of channels */
878 fifo = igetdword(chip, fiforeg[ichdev->ali_slot / 4]);
879 fifo &= ~(0xff << (ichdev->ali_slot % 4));
880 fifo |= 0x83 << (ichdev->ali_slot % 4);
881 iputdword(chip, fiforeg[ichdev->ali_slot / 4], fifo);
882 }
883 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE);
884 val &= ~(1 << (ichdev->ali_slot + 16)); /* clear PAUSE flag */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100885 /* start DMA */
886 iputdword(chip, ICHREG(ALI_DMACR), val | (1 << ichdev->ali_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 break;
Takashi Iwai1cfe43d2005-08-16 16:52:24 +0200888 case SNDRV_PCM_TRIGGER_SUSPEND:
889 ichdev->suspended = 1;
890 /* fallthru */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 case SNDRV_PCM_TRIGGER_STOP:
892 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100893 /* pause */
894 iputdword(chip, ICHREG(ALI_DMACR), val | (1 << (ichdev->ali_slot + 16)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 iputbyte(chip, port + ICH_REG_OFF_CR, 0);
896 while (igetbyte(chip, port + ICH_REG_OFF_CR))
897 ;
898 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
899 break;
900 /* reset whole DMA things */
901 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
902 /* clear interrupts */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100903 iputbyte(chip, port + ICH_REG_OFF_SR,
904 igetbyte(chip, port + ICH_REG_OFF_SR) | 0x1e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 iputdword(chip, ICHREG(ALI_INTERRUPTSR),
906 igetdword(chip, ICHREG(ALI_INTERRUPTSR)) & ichdev->int_sta_mask);
907 break;
908 default:
909 return -EINVAL;
910 }
911 return 0;
912}
913
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100914static int snd_intel8x0_hw_params(struct snd_pcm_substream *substream,
915 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100917 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
918 struct ichdev *ichdev = get_ichdev(substream);
919 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 int dbl = params_rate(hw_params) > 48000;
921 int err;
922
923 if (chip->fix_nocache && ichdev->page_attr_changed) {
924 fill_nocache(runtime->dma_area, runtime->dma_bytes, 0); /* clear */
925 ichdev->page_attr_changed = 0;
926 }
927 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
928 if (err < 0)
929 return err;
930 if (chip->fix_nocache) {
931 if (runtime->dma_area && ! ichdev->page_attr_changed) {
932 fill_nocache(runtime->dma_area, runtime->dma_bytes, 1);
933 ichdev->page_attr_changed = 1;
934 }
935 }
936 if (ichdev->pcm_open_flag) {
937 snd_ac97_pcm_close(ichdev->pcm);
938 ichdev->pcm_open_flag = 0;
939 }
940 err = snd_ac97_pcm_open(ichdev->pcm, params_rate(hw_params),
941 params_channels(hw_params),
942 ichdev->pcm->r[dbl].slots);
943 if (err >= 0) {
944 ichdev->pcm_open_flag = 1;
945 /* Force SPDIF setting */
946 if (ichdev->ichd == ICHD_PCMOUT && chip->spdif_idx < 0)
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100947 snd_ac97_set_rate(ichdev->pcm->r[0].codec[0], AC97_SPDIF,
948 params_rate(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950 return err;
951}
952
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100953static int snd_intel8x0_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100955 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
956 struct ichdev *ichdev = get_ichdev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 if (ichdev->pcm_open_flag) {
959 snd_ac97_pcm_close(ichdev->pcm);
960 ichdev->pcm_open_flag = 0;
961 }
962 if (chip->fix_nocache && ichdev->page_attr_changed) {
963 fill_nocache(substream->runtime->dma_area, substream->runtime->dma_bytes, 0);
964 ichdev->page_attr_changed = 0;
965 }
966 return snd_pcm_lib_free_pages(substream);
967}
968
Takashi Iwai6b75a9d2005-11-17 15:04:53 +0100969static void snd_intel8x0_setup_pcm_out(struct intel8x0 *chip,
970 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 unsigned int cnt;
973 int dbl = runtime->rate > 48000;
Takashi Iwai1cfe43d2005-08-16 16:52:24 +0200974
975 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 switch (chip->device_type) {
977 case DEVICE_ALI:
978 cnt = igetdword(chip, ICHREG(ALI_SCR));
979 cnt &= ~ICH_ALI_SC_PCM_246_MASK;
980 if (runtime->channels == 4 || dbl)
981 cnt |= ICH_ALI_SC_PCM_4;
982 else if (runtime->channels == 6)
983 cnt |= ICH_ALI_SC_PCM_6;
984 iputdword(chip, ICHREG(ALI_SCR), cnt);
985 break;
986 case DEVICE_SIS:
987 cnt = igetdword(chip, ICHREG(GLOB_CNT));
988 cnt &= ~ICH_SIS_PCM_246_MASK;
989 if (runtime->channels == 4 || dbl)
990 cnt |= ICH_SIS_PCM_4;
991 else if (runtime->channels == 6)
992 cnt |= ICH_SIS_PCM_6;
993 iputdword(chip, ICHREG(GLOB_CNT), cnt);
994 break;
995 default:
996 cnt = igetdword(chip, ICHREG(GLOB_CNT));
997 cnt &= ~(ICH_PCM_246_MASK | ICH_PCM_20BIT);
998 if (runtime->channels == 4 || dbl)
999 cnt |= ICH_PCM_4;
1000 else if (runtime->channels == 6)
1001 cnt |= ICH_PCM_6;
Takashi Iwai4235a312008-02-18 12:23:13 +01001002 else if (runtime->channels == 8)
1003 cnt |= ICH_PCM_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (chip->device_type == DEVICE_NFORCE) {
1005 /* reset to 2ch once to keep the 6 channel data in alignment,
1006 * to start from Front Left always
1007 */
1008 if (cnt & ICH_PCM_246_MASK) {
1009 iputdword(chip, ICHREG(GLOB_CNT), cnt & ~ICH_PCM_246_MASK);
1010 spin_unlock_irq(&chip->reg_lock);
1011 msleep(50); /* grrr... */
1012 spin_lock_irq(&chip->reg_lock);
1013 }
1014 } else if (chip->device_type == DEVICE_INTEL_ICH4) {
1015 if (runtime->sample_bits > 16)
1016 cnt |= ICH_PCM_20BIT;
1017 }
1018 iputdword(chip, ICHREG(GLOB_CNT), cnt);
1019 break;
1020 }
Takashi Iwai1cfe43d2005-08-16 16:52:24 +02001021 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001024static int snd_intel8x0_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001026 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
1027 struct snd_pcm_runtime *runtime = substream->runtime;
1028 struct ichdev *ichdev = get_ichdev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 ichdev->physbuf = runtime->dma_addr;
1031 ichdev->size = snd_pcm_lib_buffer_bytes(substream);
1032 ichdev->fragsize = snd_pcm_lib_period_bytes(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (ichdev->ichd == ICHD_PCMOUT) {
1034 snd_intel8x0_setup_pcm_out(chip, runtime);
Takashi Iwai1cfe43d2005-08-16 16:52:24 +02001035 if (chip->device_type == DEVICE_INTEL_ICH4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 ichdev->pos_shift = (runtime->sample_bits > 16) ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038 snd_intel8x0_setup_periods(chip, ichdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return 0;
1040}
1041
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001042static snd_pcm_uframes_t snd_intel8x0_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001044 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
1045 struct ichdev *ichdev = get_ichdev(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 size_t ptr1, ptr;
1047 int civ, timeout = 100;
1048 unsigned int position;
1049
1050 spin_lock(&chip->reg_lock);
1051 do {
1052 civ = igetbyte(chip, ichdev->reg_offset + ICH_REG_OFF_CIV);
1053 ptr1 = igetword(chip, ichdev->reg_offset + ichdev->roff_picb);
1054 position = ichdev->position;
1055 if (ptr1 == 0) {
1056 udelay(10);
1057 continue;
1058 }
1059 if (civ == igetbyte(chip, ichdev->reg_offset + ICH_REG_OFF_CIV) &&
1060 ptr1 == igetword(chip, ichdev->reg_offset + ichdev->roff_picb))
1061 break;
1062 } while (timeout--);
1063 ptr1 <<= ichdev->pos_shift;
1064 ptr = ichdev->fragsize1 - ptr1;
1065 ptr += position;
1066 spin_unlock(&chip->reg_lock);
1067 if (ptr >= ichdev->size)
1068 return 0;
1069 return bytes_to_frames(substream->runtime, ptr);
1070}
1071
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001072static struct snd_pcm_hardware snd_intel8x0_stream =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1075 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1076 SNDRV_PCM_INFO_MMAP_VALID |
1077 SNDRV_PCM_INFO_PAUSE |
1078 SNDRV_PCM_INFO_RESUME),
1079 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1080 .rates = SNDRV_PCM_RATE_48000,
1081 .rate_min = 48000,
1082 .rate_max = 48000,
1083 .channels_min = 2,
1084 .channels_max = 2,
1085 .buffer_bytes_max = 128 * 1024,
1086 .period_bytes_min = 32,
1087 .period_bytes_max = 128 * 1024,
1088 .periods_min = 1,
1089 .periods_max = 1024,
1090 .fifo_size = 0,
1091};
1092
1093static unsigned int channels4[] = {
1094 2, 4,
1095};
1096
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001097static struct snd_pcm_hw_constraint_list hw_constraints_channels4 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 .count = ARRAY_SIZE(channels4),
1099 .list = channels4,
1100 .mask = 0,
1101};
1102
1103static unsigned int channels6[] = {
1104 2, 4, 6,
1105};
1106
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001107static struct snd_pcm_hw_constraint_list hw_constraints_channels6 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 .count = ARRAY_SIZE(channels6),
1109 .list = channels6,
1110 .mask = 0,
1111};
1112
Takashi Iwai4235a312008-02-18 12:23:13 +01001113static unsigned int channels8[] = {
1114 2, 4, 6, 8,
1115};
1116
1117static struct snd_pcm_hw_constraint_list hw_constraints_channels8 = {
1118 .count = ARRAY_SIZE(channels8),
1119 .list = channels8,
1120 .mask = 0,
1121};
1122
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001123static int snd_intel8x0_pcm_open(struct snd_pcm_substream *substream, struct ichdev *ichdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001125 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
1126 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 int err;
1128
1129 ichdev->substream = substream;
1130 runtime->hw = snd_intel8x0_stream;
1131 runtime->hw.rates = ichdev->pcm->rates;
1132 snd_pcm_limit_hw_rates(runtime);
1133 if (chip->device_type == DEVICE_SIS) {
1134 runtime->hw.buffer_bytes_max = 64*1024;
1135 runtime->hw.period_bytes_max = 64*1024;
1136 }
1137 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
1138 return err;
1139 runtime->private_data = ichdev;
1140 return 0;
1141}
1142
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001143static int snd_intel8x0_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001145 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
1146 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 int err;
1148
1149 err = snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCMOUT]);
1150 if (err < 0)
1151 return err;
1152
Takashi Iwai4235a312008-02-18 12:23:13 +01001153 if (chip->multi8) {
1154 runtime->hw.channels_max = 8;
1155 snd_pcm_hw_constraint_list(runtime, 0,
1156 SNDRV_PCM_HW_PARAM_CHANNELS,
1157 &hw_constraints_channels8);
1158 } else if (chip->multi6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 runtime->hw.channels_max = 6;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001160 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1161 &hw_constraints_channels6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 } else if (chip->multi4) {
1163 runtime->hw.channels_max = 4;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001164 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1165 &hw_constraints_channels4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167 if (chip->dra) {
1168 snd_ac97_pcm_double_rate_rules(runtime);
1169 }
1170 if (chip->smp20bit) {
1171 runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
1172 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 20);
1173 }
1174 return 0;
1175}
1176
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001177static int snd_intel8x0_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001179 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 chip->ichd[ICHD_PCMOUT].substream = NULL;
1182 return 0;
1183}
1184
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001185static int snd_intel8x0_capture_open(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 return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCMIN]);
1190}
1191
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001192static int snd_intel8x0_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001194 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 chip->ichd[ICHD_PCMIN].substream = NULL;
1197 return 0;
1198}
1199
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001200static int snd_intel8x0_mic_open(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 return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_MIC]);
1205}
1206
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001207static int snd_intel8x0_mic_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001209 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 chip->ichd[ICHD_MIC].substream = NULL;
1212 return 0;
1213}
1214
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001215static int snd_intel8x0_mic2_open(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 return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_MIC2]);
1220}
1221
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001222static int snd_intel8x0_mic2_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001224 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 chip->ichd[ICHD_MIC2].substream = NULL;
1227 return 0;
1228}
1229
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001230static int snd_intel8x0_capture2_open(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 return snd_intel8x0_pcm_open(substream, &chip->ichd[ICHD_PCM2IN]);
1235}
1236
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001237static int snd_intel8x0_capture2_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001239 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 chip->ichd[ICHD_PCM2IN].substream = NULL;
1242 return 0;
1243}
1244
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001245static int snd_intel8x0_spdif_open(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 int idx = chip->device_type == DEVICE_NFORCE ? NVD_SPBAR : ICHD_SPBAR;
1249
1250 return snd_intel8x0_pcm_open(substream, &chip->ichd[idx]);
1251}
1252
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001253static int snd_intel8x0_spdif_close(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 chip->ichd[idx].substream = NULL;
1259 return 0;
1260}
1261
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001262static int snd_intel8x0_ali_ac97spdifout_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001264 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 unsigned int val;
1266
1267 spin_lock_irq(&chip->reg_lock);
1268 val = igetdword(chip, ICHREG(ALI_INTERFACECR));
1269 val |= ICH_ALI_IF_AC97SP;
1270 iputdword(chip, ICHREG(ALI_INTERFACECR), val);
1271 /* also needs to set ALI_SC_CODEC_SPDF correctly */
1272 spin_unlock_irq(&chip->reg_lock);
1273
1274 return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_AC97SPDIFOUT]);
1275}
1276
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001277static int snd_intel8x0_ali_ac97spdifout_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001279 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 unsigned int val;
1281
1282 chip->ichd[ALID_AC97SPDIFOUT].substream = NULL;
1283 spin_lock_irq(&chip->reg_lock);
1284 val = igetdword(chip, ICHREG(ALI_INTERFACECR));
1285 val &= ~ICH_ALI_IF_AC97SP;
1286 iputdword(chip, ICHREG(ALI_INTERFACECR), val);
1287 spin_unlock_irq(&chip->reg_lock);
1288
1289 return 0;
1290}
1291
Takashi Iwai1a183132006-04-18 21:23:47 +02001292#if 0 // NYI
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001293static int snd_intel8x0_ali_spdifin_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001295 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_SPDIFIN]);
1298}
1299
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001300static int snd_intel8x0_ali_spdifin_close(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 chip->ichd[ALID_SPDIFIN].substream = NULL;
1305 return 0;
1306}
1307
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001308static int snd_intel8x0_ali_spdifout_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001310 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 return snd_intel8x0_pcm_open(substream, &chip->ichd[ALID_SPDIFOUT]);
1313}
1314
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001315static int snd_intel8x0_ali_spdifout_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001317 struct intel8x0 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 chip->ichd[ALID_SPDIFOUT].substream = NULL;
1320 return 0;
1321}
1322#endif
1323
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001324static struct snd_pcm_ops snd_intel8x0_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 .open = snd_intel8x0_playback_open,
1326 .close = snd_intel8x0_playback_close,
1327 .ioctl = snd_pcm_lib_ioctl,
1328 .hw_params = snd_intel8x0_hw_params,
1329 .hw_free = snd_intel8x0_hw_free,
1330 .prepare = snd_intel8x0_pcm_prepare,
1331 .trigger = snd_intel8x0_pcm_trigger,
1332 .pointer = snd_intel8x0_pcm_pointer,
1333};
1334
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001335static struct snd_pcm_ops snd_intel8x0_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 .open = snd_intel8x0_capture_open,
1337 .close = snd_intel8x0_capture_close,
1338 .ioctl = snd_pcm_lib_ioctl,
1339 .hw_params = snd_intel8x0_hw_params,
1340 .hw_free = snd_intel8x0_hw_free,
1341 .prepare = snd_intel8x0_pcm_prepare,
1342 .trigger = snd_intel8x0_pcm_trigger,
1343 .pointer = snd_intel8x0_pcm_pointer,
1344};
1345
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001346static struct snd_pcm_ops snd_intel8x0_capture_mic_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 .open = snd_intel8x0_mic_open,
1348 .close = snd_intel8x0_mic_close,
1349 .ioctl = snd_pcm_lib_ioctl,
1350 .hw_params = snd_intel8x0_hw_params,
1351 .hw_free = snd_intel8x0_hw_free,
1352 .prepare = snd_intel8x0_pcm_prepare,
1353 .trigger = snd_intel8x0_pcm_trigger,
1354 .pointer = snd_intel8x0_pcm_pointer,
1355};
1356
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001357static struct snd_pcm_ops snd_intel8x0_capture_mic2_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 .open = snd_intel8x0_mic2_open,
1359 .close = snd_intel8x0_mic2_close,
1360 .ioctl = snd_pcm_lib_ioctl,
1361 .hw_params = snd_intel8x0_hw_params,
1362 .hw_free = snd_intel8x0_hw_free,
1363 .prepare = snd_intel8x0_pcm_prepare,
1364 .trigger = snd_intel8x0_pcm_trigger,
1365 .pointer = snd_intel8x0_pcm_pointer,
1366};
1367
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001368static struct snd_pcm_ops snd_intel8x0_capture2_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 .open = snd_intel8x0_capture2_open,
1370 .close = snd_intel8x0_capture2_close,
1371 .ioctl = snd_pcm_lib_ioctl,
1372 .hw_params = snd_intel8x0_hw_params,
1373 .hw_free = snd_intel8x0_hw_free,
1374 .prepare = snd_intel8x0_pcm_prepare,
1375 .trigger = snd_intel8x0_pcm_trigger,
1376 .pointer = snd_intel8x0_pcm_pointer,
1377};
1378
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001379static struct snd_pcm_ops snd_intel8x0_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 .open = snd_intel8x0_spdif_open,
1381 .close = snd_intel8x0_spdif_close,
1382 .ioctl = snd_pcm_lib_ioctl,
1383 .hw_params = snd_intel8x0_hw_params,
1384 .hw_free = snd_intel8x0_hw_free,
1385 .prepare = snd_intel8x0_pcm_prepare,
1386 .trigger = snd_intel8x0_pcm_trigger,
1387 .pointer = snd_intel8x0_pcm_pointer,
1388};
1389
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001390static struct snd_pcm_ops snd_intel8x0_ali_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 .open = snd_intel8x0_playback_open,
1392 .close = snd_intel8x0_playback_close,
1393 .ioctl = snd_pcm_lib_ioctl,
1394 .hw_params = snd_intel8x0_hw_params,
1395 .hw_free = snd_intel8x0_hw_free,
1396 .prepare = snd_intel8x0_pcm_prepare,
1397 .trigger = snd_intel8x0_ali_trigger,
1398 .pointer = snd_intel8x0_pcm_pointer,
1399};
1400
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001401static struct snd_pcm_ops snd_intel8x0_ali_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 .open = snd_intel8x0_capture_open,
1403 .close = snd_intel8x0_capture_close,
1404 .ioctl = snd_pcm_lib_ioctl,
1405 .hw_params = snd_intel8x0_hw_params,
1406 .hw_free = snd_intel8x0_hw_free,
1407 .prepare = snd_intel8x0_pcm_prepare,
1408 .trigger = snd_intel8x0_ali_trigger,
1409 .pointer = snd_intel8x0_pcm_pointer,
1410};
1411
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001412static struct snd_pcm_ops snd_intel8x0_ali_capture_mic_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 .open = snd_intel8x0_mic_open,
1414 .close = snd_intel8x0_mic_close,
1415 .ioctl = snd_pcm_lib_ioctl,
1416 .hw_params = snd_intel8x0_hw_params,
1417 .hw_free = snd_intel8x0_hw_free,
1418 .prepare = snd_intel8x0_pcm_prepare,
1419 .trigger = snd_intel8x0_ali_trigger,
1420 .pointer = snd_intel8x0_pcm_pointer,
1421};
1422
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001423static struct snd_pcm_ops snd_intel8x0_ali_ac97spdifout_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 .open = snd_intel8x0_ali_ac97spdifout_open,
1425 .close = snd_intel8x0_ali_ac97spdifout_close,
1426 .ioctl = snd_pcm_lib_ioctl,
1427 .hw_params = snd_intel8x0_hw_params,
1428 .hw_free = snd_intel8x0_hw_free,
1429 .prepare = snd_intel8x0_pcm_prepare,
1430 .trigger = snd_intel8x0_ali_trigger,
1431 .pointer = snd_intel8x0_pcm_pointer,
1432};
1433
Takashi Iwai1a183132006-04-18 21:23:47 +02001434#if 0 // NYI
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001435static struct snd_pcm_ops snd_intel8x0_ali_spdifin_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 .open = snd_intel8x0_ali_spdifin_open,
1437 .close = snd_intel8x0_ali_spdifin_close,
1438 .ioctl = snd_pcm_lib_ioctl,
1439 .hw_params = snd_intel8x0_hw_params,
1440 .hw_free = snd_intel8x0_hw_free,
1441 .prepare = snd_intel8x0_pcm_prepare,
1442 .trigger = snd_intel8x0_pcm_trigger,
1443 .pointer = snd_intel8x0_pcm_pointer,
1444};
1445
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001446static struct snd_pcm_ops snd_intel8x0_ali_spdifout_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 .open = snd_intel8x0_ali_spdifout_open,
1448 .close = snd_intel8x0_ali_spdifout_close,
1449 .ioctl = snd_pcm_lib_ioctl,
1450 .hw_params = snd_intel8x0_hw_params,
1451 .hw_free = snd_intel8x0_hw_free,
1452 .prepare = snd_intel8x0_pcm_prepare,
1453 .trigger = snd_intel8x0_pcm_trigger,
1454 .pointer = snd_intel8x0_pcm_pointer,
1455};
1456#endif // NYI
1457
1458struct ich_pcm_table {
1459 char *suffix;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001460 struct snd_pcm_ops *playback_ops;
1461 struct snd_pcm_ops *capture_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 size_t prealloc_size;
1463 size_t prealloc_max_size;
1464 int ac97_idx;
1465};
1466
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001467static int __devinit snd_intel8x0_pcm1(struct intel8x0 *chip, int device,
1468 struct ich_pcm_table *rec)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001470 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 int err;
1472 char name[32];
1473
1474 if (rec->suffix)
1475 sprintf(name, "Intel ICH - %s", rec->suffix);
1476 else
1477 strcpy(name, "Intel ICH");
1478 err = snd_pcm_new(chip->card, name, device,
1479 rec->playback_ops ? 1 : 0,
1480 rec->capture_ops ? 1 : 0, &pcm);
1481 if (err < 0)
1482 return err;
1483
1484 if (rec->playback_ops)
1485 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, rec->playback_ops);
1486 if (rec->capture_ops)
1487 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, rec->capture_ops);
1488
1489 pcm->private_data = chip;
1490 pcm->info_flags = 0;
1491 if (rec->suffix)
1492 sprintf(pcm->name, "%s - %s", chip->card->shortname, rec->suffix);
1493 else
1494 strcpy(pcm->name, chip->card->shortname);
1495 chip->pcm[device] = pcm;
1496
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001497 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1498 snd_dma_pci_data(chip->pci),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 rec->prealloc_size, rec->prealloc_max_size);
1500
1501 return 0;
1502}
1503
1504static struct ich_pcm_table intel_pcms[] __devinitdata = {
1505 {
1506 .playback_ops = &snd_intel8x0_playback_ops,
1507 .capture_ops = &snd_intel8x0_capture_ops,
1508 .prealloc_size = 64 * 1024,
1509 .prealloc_max_size = 128 * 1024,
1510 },
1511 {
1512 .suffix = "MIC ADC",
1513 .capture_ops = &snd_intel8x0_capture_mic_ops,
1514 .prealloc_size = 0,
1515 .prealloc_max_size = 128 * 1024,
1516 .ac97_idx = ICHD_MIC,
1517 },
1518 {
1519 .suffix = "MIC2 ADC",
1520 .capture_ops = &snd_intel8x0_capture_mic2_ops,
1521 .prealloc_size = 0,
1522 .prealloc_max_size = 128 * 1024,
1523 .ac97_idx = ICHD_MIC2,
1524 },
1525 {
1526 .suffix = "ADC2",
1527 .capture_ops = &snd_intel8x0_capture2_ops,
1528 .prealloc_size = 0,
1529 .prealloc_max_size = 128 * 1024,
1530 .ac97_idx = ICHD_PCM2IN,
1531 },
1532 {
1533 .suffix = "IEC958",
1534 .playback_ops = &snd_intel8x0_spdif_ops,
1535 .prealloc_size = 64 * 1024,
1536 .prealloc_max_size = 128 * 1024,
1537 .ac97_idx = ICHD_SPBAR,
1538 },
1539};
1540
1541static struct ich_pcm_table nforce_pcms[] __devinitdata = {
1542 {
1543 .playback_ops = &snd_intel8x0_playback_ops,
1544 .capture_ops = &snd_intel8x0_capture_ops,
1545 .prealloc_size = 64 * 1024,
1546 .prealloc_max_size = 128 * 1024,
1547 },
1548 {
1549 .suffix = "MIC ADC",
1550 .capture_ops = &snd_intel8x0_capture_mic_ops,
1551 .prealloc_size = 0,
1552 .prealloc_max_size = 128 * 1024,
1553 .ac97_idx = NVD_MIC,
1554 },
1555 {
1556 .suffix = "IEC958",
1557 .playback_ops = &snd_intel8x0_spdif_ops,
1558 .prealloc_size = 64 * 1024,
1559 .prealloc_max_size = 128 * 1024,
1560 .ac97_idx = NVD_SPBAR,
1561 },
1562};
1563
1564static struct ich_pcm_table ali_pcms[] __devinitdata = {
1565 {
1566 .playback_ops = &snd_intel8x0_ali_playback_ops,
1567 .capture_ops = &snd_intel8x0_ali_capture_ops,
1568 .prealloc_size = 64 * 1024,
1569 .prealloc_max_size = 128 * 1024,
1570 },
1571 {
1572 .suffix = "MIC ADC",
1573 .capture_ops = &snd_intel8x0_ali_capture_mic_ops,
1574 .prealloc_size = 0,
1575 .prealloc_max_size = 128 * 1024,
1576 .ac97_idx = ALID_MIC,
1577 },
1578 {
1579 .suffix = "IEC958",
1580 .playback_ops = &snd_intel8x0_ali_ac97spdifout_ops,
Takashi Iwai1a183132006-04-18 21:23:47 +02001581 /* .capture_ops = &snd_intel8x0_ali_spdifin_ops, */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 .prealloc_size = 64 * 1024,
1583 .prealloc_max_size = 128 * 1024,
1584 .ac97_idx = ALID_AC97SPDIFOUT,
1585 },
1586#if 0 // NYI
1587 {
1588 .suffix = "HW IEC958",
1589 .playback_ops = &snd_intel8x0_ali_spdifout_ops,
1590 .prealloc_size = 64 * 1024,
1591 .prealloc_max_size = 128 * 1024,
1592 },
1593#endif
1594};
1595
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001596static int __devinit snd_intel8x0_pcm(struct intel8x0 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 int i, tblsize, device, err;
1599 struct ich_pcm_table *tbl, *rec;
1600
1601 switch (chip->device_type) {
1602 case DEVICE_INTEL_ICH4:
1603 tbl = intel_pcms;
1604 tblsize = ARRAY_SIZE(intel_pcms);
Takashi Iwaia9e99662006-11-24 15:42:07 +01001605 if (spdif_aclink)
1606 tblsize--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 break;
1608 case DEVICE_NFORCE:
1609 tbl = nforce_pcms;
1610 tblsize = ARRAY_SIZE(nforce_pcms);
Takashi Iwaia9e99662006-11-24 15:42:07 +01001611 if (spdif_aclink)
1612 tblsize--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 break;
1614 case DEVICE_ALI:
1615 tbl = ali_pcms;
1616 tblsize = ARRAY_SIZE(ali_pcms);
1617 break;
1618 default:
1619 tbl = intel_pcms;
1620 tblsize = 2;
1621 break;
1622 }
1623
1624 device = 0;
1625 for (i = 0; i < tblsize; i++) {
1626 rec = tbl + i;
1627 if (i > 0 && rec->ac97_idx) {
1628 /* activate PCM only when associated AC'97 codec */
1629 if (! chip->ichd[rec->ac97_idx].pcm)
1630 continue;
1631 }
1632 err = snd_intel8x0_pcm1(chip, device, rec);
1633 if (err < 0)
1634 return err;
1635 device++;
1636 }
1637
1638 chip->pcm_devs = device;
1639 return 0;
1640}
1641
1642
1643/*
1644 * Mixer part
1645 */
1646
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001647static void snd_intel8x0_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001649 struct intel8x0 *chip = bus->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 chip->ac97_bus = NULL;
1651}
1652
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001653static void snd_intel8x0_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01001655 struct intel8x0 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 chip->ac97[ac97->num] = NULL;
1657}
1658
1659static struct ac97_pcm ac97_pcm_defs[] __devinitdata = {
1660 /* front PCM */
1661 {
1662 .exclusive = 1,
1663 .r = { {
1664 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1665 (1 << AC97_SLOT_PCM_RIGHT) |
1666 (1 << AC97_SLOT_PCM_CENTER) |
1667 (1 << AC97_SLOT_PCM_SLEFT) |
1668 (1 << AC97_SLOT_PCM_SRIGHT) |
1669 (1 << AC97_SLOT_LFE)
1670 },
1671 {
1672 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1673 (1 << AC97_SLOT_PCM_RIGHT) |
1674 (1 << AC97_SLOT_PCM_LEFT_0) |
1675 (1 << AC97_SLOT_PCM_RIGHT_0)
1676 }
1677 }
1678 },
1679 /* PCM IN #1 */
1680 {
1681 .stream = 1,
1682 .exclusive = 1,
1683 .r = { {
1684 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1685 (1 << AC97_SLOT_PCM_RIGHT)
1686 }
1687 }
1688 },
1689 /* MIC IN #1 */
1690 {
1691 .stream = 1,
1692 .exclusive = 1,
1693 .r = { {
1694 .slots = (1 << AC97_SLOT_MIC)
1695 }
1696 }
1697 },
1698 /* S/PDIF PCM */
1699 {
1700 .exclusive = 1,
1701 .spdif = 1,
1702 .r = { {
1703 .slots = (1 << AC97_SLOT_SPDIF_LEFT2) |
1704 (1 << AC97_SLOT_SPDIF_RIGHT2)
1705 }
1706 }
1707 },
1708 /* PCM IN #2 */
1709 {
1710 .stream = 1,
1711 .exclusive = 1,
1712 .r = { {
1713 .slots = (1 << AC97_SLOT_PCM_LEFT) |
1714 (1 << AC97_SLOT_PCM_RIGHT)
1715 }
1716 }
1717 },
1718 /* MIC IN #2 */
1719 {
1720 .stream = 1,
1721 .exclusive = 1,
1722 .r = { {
1723 .slots = (1 << AC97_SLOT_MIC)
1724 }
1725 }
1726 },
1727};
1728
1729static struct ac97_quirk ac97_quirks[] __devinitdata = {
Takashi Iwai0d9ac272008-02-27 16:40:18 +01001730 {
1731 .subvendor = 0x0e11,
1732 .subdevice = 0x000e,
1733 .name = "Compaq Deskpro EN", /* AD1885 */
1734 .type = AC97_TUNE_HP_ONLY
1735 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001737 .subvendor = 0x0e11,
1738 .subdevice = 0x008a,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 .name = "Compaq Evo W4000", /* AD1885 */
1740 .type = AC97_TUNE_HP_ONLY
1741 },
1742 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001743 .subvendor = 0x0e11,
1744 .subdevice = 0x00b8,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 .name = "Compaq Evo D510C",
1746 .type = AC97_TUNE_HP_ONLY
1747 },
1748 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001749 .subvendor = 0x0e11,
1750 .subdevice = 0x0860,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 .name = "HP/Compaq nx7010",
1752 .type = AC97_TUNE_MUTE_LED
1753 },
1754 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001755 .subvendor = 0x1014,
1756 .subdevice = 0x1f00,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 .name = "MS-9128",
1758 .type = AC97_TUNE_ALC_JACK
1759 },
1760 {
Takashi Iwai5d529392005-09-01 13:56:23 +02001761 .subvendor = 0x1014,
1762 .subdevice = 0x0267,
1763 .name = "IBM NetVista A30p", /* AD1981B */
1764 .type = AC97_TUNE_HP_ONLY
1765 },
1766 {
Dick Streefland72c89862005-10-07 12:02:23 +02001767 .subvendor = 0x1025,
Takashi Iwaib6a370b62008-02-04 14:00:53 +01001768 .subdevice = 0x0082,
1769 .name = "Acer Travelmate 2310",
1770 .type = AC97_TUNE_HP_ONLY
1771 },
1772 {
1773 .subvendor = 0x1025,
Dick Streefland72c89862005-10-07 12:02:23 +02001774 .subdevice = 0x0083,
1775 .name = "Acer Aspire 3003LCi",
1776 .type = AC97_TUNE_HP_ONLY
1777 },
1778 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001779 .subvendor = 0x1028,
1780 .subdevice = 0x00d8,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 .name = "Dell Precision 530", /* AD1885 */
1782 .type = AC97_TUNE_HP_ONLY
1783 },
1784 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001785 .subvendor = 0x1028,
1786 .subdevice = 0x010d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 .name = "Dell", /* which model? AD1885 */
1788 .type = AC97_TUNE_HP_ONLY
1789 },
1790 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001791 .subvendor = 0x1028,
1792 .subdevice = 0x0126,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 .name = "Dell Optiplex GX260", /* AD1981A */
1794 .type = AC97_TUNE_HP_ONLY
1795 },
1796 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001797 .subvendor = 0x1028,
1798 .subdevice = 0x012c,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 .name = "Dell Precision 650", /* AD1981A */
1800 .type = AC97_TUNE_HP_ONLY
1801 },
1802 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001803 .subvendor = 0x1028,
1804 .subdevice = 0x012d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 .name = "Dell Precision 450", /* AD1981B*/
1806 .type = AC97_TUNE_HP_ONLY
1807 },
1808 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001809 .subvendor = 0x1028,
1810 .subdevice = 0x0147,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 .name = "Dell", /* which model? AD1981B*/
1812 .type = AC97_TUNE_HP_ONLY
1813 },
1814 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001815 .subvendor = 0x1028,
Chris Ballc9fe51c2006-01-13 13:26:42 +01001816 .subdevice = 0x0151,
1817 .name = "Dell Optiplex GX270", /* AD1981B */
1818 .type = AC97_TUNE_HP_ONLY
1819 },
1820 {
1821 .subvendor = 0x1028,
Daniel T Chen1781a9a2006-06-16 12:13:00 +02001822 .subdevice = 0x014e,
1823 .name = "Dell D800", /* STAC9750/51 */
1824 .type = AC97_TUNE_HP_ONLY
1825 },
1826 {
1827 .subvendor = 0x1028,
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001828 .subdevice = 0x0163,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 .name = "Dell Unknown", /* STAC9750/51 */
1830 .type = AC97_TUNE_HP_ONLY
1831 },
1832 {
Karoly Lorentey6d6f9152005-10-25 11:50:25 +02001833 .subvendor = 0x1028,
Daniel T Chen8286c532007-05-15 11:46:23 +02001834 .subdevice = 0x0186,
1835 .name = "Dell Latitude D810", /* cf. Malone #41015 */
1836 .type = AC97_TUNE_HP_MUTE_LED
1837 },
1838 {
1839 .subvendor = 0x1028,
1840 .subdevice = 0x0188,
1841 .name = "Dell Inspiron 6000",
1842 .type = AC97_TUNE_HP_MUTE_LED /* cf. Malone #41015 */
1843 },
1844 {
1845 .subvendor = 0x1028,
Karoly Lorentey6d6f9152005-10-25 11:50:25 +02001846 .subdevice = 0x0191,
1847 .name = "Dell Inspiron 8600",
1848 .type = AC97_TUNE_HP_ONLY
1849 },
1850 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001851 .subvendor = 0x103c,
1852 .subdevice = 0x006d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 .name = "HP zv5000",
1854 .type = AC97_TUNE_MUTE_LED /*AD1981B*/
1855 },
1856 { /* FIXME: which codec? */
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001857 .subvendor = 0x103c,
1858 .subdevice = 0x00c3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 .name = "HP xw6000",
1860 .type = AC97_TUNE_HP_ONLY
1861 },
1862 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001863 .subvendor = 0x103c,
1864 .subdevice = 0x088c,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 .name = "HP nc8000",
Daniel T Chen8286c532007-05-15 11:46:23 +02001866 .type = AC97_TUNE_HP_MUTE_LED
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 },
1868 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001869 .subvendor = 0x103c,
1870 .subdevice = 0x0890,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 .name = "HP nc6000",
1872 .type = AC97_TUNE_MUTE_LED
1873 },
1874 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001875 .subvendor = 0x103c,
Daniel Horchnere0c93cf2005-08-25 13:02:24 +02001876 .subdevice = 0x0934,
1877 .name = "HP nx8220",
1878 .type = AC97_TUNE_MUTE_LED
1879 },
1880 {
1881 .subvendor = 0x103c,
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001882 .subdevice = 0x129d,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 .name = "HP xw8000",
1884 .type = AC97_TUNE_HP_ONLY
1885 },
1886 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001887 .subvendor = 0x103c,
Matthew Garretta0faefe2005-12-06 13:59:12 +01001888 .subdevice = 0x0938,
1889 .name = "HP nc4200",
1890 .type = AC97_TUNE_HP_MUTE_LED
1891 },
1892 {
1893 .subvendor = 0x103c,
1894 .subdevice = 0x099c,
Ulrich Muellerd82ed2f2006-01-04 12:21:11 +01001895 .name = "HP nx6110/nc6120",
Matthew Garretta0faefe2005-12-06 13:59:12 +01001896 .type = AC97_TUNE_HP_MUTE_LED
1897 },
1898 {
1899 .subvendor = 0x103c,
1900 .subdevice = 0x0944,
1901 .name = "HP nc6220",
1902 .type = AC97_TUNE_HP_MUTE_LED
1903 },
1904 {
1905 .subvendor = 0x103c,
1906 .subdevice = 0x0934,
1907 .name = "HP nc8220",
1908 .type = AC97_TUNE_HP_MUTE_LED
1909 },
1910 {
1911 .subvendor = 0x103c,
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001912 .subdevice = 0x12f1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 .name = "HP xw8200", /* AD1981B*/
1914 .type = AC97_TUNE_HP_ONLY
1915 },
1916 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001917 .subvendor = 0x103c,
1918 .subdevice = 0x12f2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 .name = "HP xw6200",
1920 .type = AC97_TUNE_HP_ONLY
1921 },
1922 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001923 .subvendor = 0x103c,
1924 .subdevice = 0x3008,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 .name = "HP xw4200", /* AD1981B*/
1926 .type = AC97_TUNE_HP_ONLY
1927 },
1928 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001929 .subvendor = 0x104d,
1930 .subdevice = 0x8197,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 .name = "Sony S1XP",
1932 .type = AC97_TUNE_INV_EAPD
1933 },
1934 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001935 .subvendor = 0x1043,
1936 .subdevice = 0x80f3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 .name = "ASUS ICH5/AD1985",
1938 .type = AC97_TUNE_AD_SHARING
1939 },
1940 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001941 .subvendor = 0x10cf,
1942 .subdevice = 0x11c3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 .name = "Fujitsu-Siemens E4010",
1944 .type = AC97_TUNE_HP_ONLY
1945 },
1946 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001947 .subvendor = 0x10cf,
Takashi Iwai98c7f212005-05-31 16:52:58 +02001948 .subdevice = 0x1225,
1949 .name = "Fujitsu-Siemens T3010",
1950 .type = AC97_TUNE_HP_ONLY
1951 },
1952 {
1953 .subvendor = 0x10cf,
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001954 .subdevice = 0x1253,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 .name = "Fujitsu S6210", /* STAC9750/51 */
1956 .type = AC97_TUNE_HP_ONLY
1957 },
1958 {
Takashi Iwai9970dce2005-08-25 20:30:27 +02001959 .subvendor = 0x10cf,
Daniel T Chen8286c532007-05-15 11:46:23 +02001960 .subdevice = 0x127e,
1961 .name = "Fujitsu Lifebook C1211D",
1962 .type = AC97_TUNE_HP_ONLY
1963 },
1964 {
1965 .subvendor = 0x10cf,
Takashi Iwai9970dce2005-08-25 20:30:27 +02001966 .subdevice = 0x12ec,
1967 .name = "Fujitsu-Siemens 4010",
1968 .type = AC97_TUNE_HP_ONLY
1969 },
1970 {
Jaroslav Kysela2eb061f2005-11-18 07:44:13 +01001971 .subvendor = 0x10cf,
1972 .subdevice = 0x12f2,
1973 .name = "Fujitsu-Siemens Celsius H320",
1974 .type = AC97_TUNE_SWAP_HP
1975 },
1976 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001977 .subvendor = 0x10f1,
1978 .subdevice = 0x2665,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 .name = "Fujitsu-Siemens Celsius", /* AD1981? */
1980 .type = AC97_TUNE_HP_ONLY
1981 },
1982 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02001983 .subvendor = 0x10f1,
1984 .subdevice = 0x2885,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 .name = "AMD64 Mobo", /* ALC650 */
1986 .type = AC97_TUNE_HP_ONLY
1987 },
1988 {
Takashi Iwai4f42bcc2006-06-29 17:05:31 +02001989 .subvendor = 0x10f1,
1990 .subdevice = 0x2895,
1991 .name = "Tyan Thunder K8WE",
1992 .type = AC97_TUNE_HP_ONLY
1993 },
1994 {
Keith Packard6c504442006-10-24 13:34:11 -07001995 .subvendor = 0x10f7,
1996 .subdevice = 0x834c,
1997 .name = "Panasonic CF-R4",
1998 .type = AC97_TUNE_HP_ONLY,
1999 },
2000 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02002001 .subvendor = 0x110a,
2002 .subdevice = 0x0056,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 .name = "Fujitsu-Siemens Scenic", /* AD1981? */
2004 .type = AC97_TUNE_HP_ONLY
2005 },
2006 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02002007 .subvendor = 0x11d4,
2008 .subdevice = 0x5375,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 .name = "ADI AD1985 (discrete)",
2010 .type = AC97_TUNE_HP_ONLY
2011 },
2012 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02002013 .subvendor = 0x1462,
2014 .subdevice = 0x5470,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 .name = "MSI P4 ATX 645 Ultra",
2016 .type = AC97_TUNE_HP_ONLY
2017 },
2018 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02002019 .subvendor = 0x1734,
2020 .subdevice = 0x0088,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 .name = "Fujitsu-Siemens D1522", /* AD1981 */
2022 .type = AC97_TUNE_HP_ONLY
2023 },
2024 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02002025 .subvendor = 0x8086,
2026 .subdevice = 0x2000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 .mask = 0xfff0,
2028 .name = "Intel ICH5/AD1985",
2029 .type = AC97_TUNE_AD_SHARING
2030 },
2031 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02002032 .subvendor = 0x8086,
2033 .subdevice = 0x4000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 .mask = 0xfff0,
2035 .name = "Intel ICH5/AD1985",
2036 .type = AC97_TUNE_AD_SHARING
2037 },
2038 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02002039 .subvendor = 0x8086,
2040 .subdevice = 0x4856,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 .name = "Intel D845WN (82801BA)",
2042 .type = AC97_TUNE_SWAP_HP
2043 },
2044 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02002045 .subvendor = 0x8086,
2046 .subdevice = 0x4d44,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 .name = "Intel D850EMV2", /* AD1885 */
2048 .type = AC97_TUNE_HP_ONLY
2049 },
2050 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02002051 .subvendor = 0x8086,
2052 .subdevice = 0x4d56,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 .name = "Intel ICH/AD1885",
2054 .type = AC97_TUNE_HP_ONLY
2055 },
2056 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02002057 .subvendor = 0x8086,
2058 .subdevice = 0x6000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 .mask = 0xfff0,
2060 .name = "Intel ICH5/AD1985",
2061 .type = AC97_TUNE_AD_SHARING
2062 },
2063 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02002064 .subvendor = 0x8086,
2065 .subdevice = 0xe000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 .mask = 0xfff0,
2067 .name = "Intel ICH5/AD1985",
2068 .type = AC97_TUNE_AD_SHARING
2069 },
2070#if 0 /* FIXME: this seems wrong on most boards */
2071 {
James Courtier-Dutton6fd8b872005-05-30 17:20:19 +02002072 .subvendor = 0x8086,
2073 .subdevice = 0xa000,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 .mask = 0xfff0,
2075 .name = "Intel ICH5/AD1985",
2076 .type = AC97_TUNE_HP_ONLY
2077 },
2078#endif
2079 { } /* terminator */
2080};
2081
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002082static int __devinit snd_intel8x0_mixer(struct intel8x0 *chip, int ac97_clock,
2083 const char *quirk_override)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002085 struct snd_ac97_bus *pbus;
2086 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 int err;
2088 unsigned int i, codecs;
2089 unsigned int glob_sta = 0;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002090 struct snd_ac97_bus_ops *ops;
2091 static struct snd_ac97_bus_ops standard_bus_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 .write = snd_intel8x0_codec_write,
2093 .read = snd_intel8x0_codec_read,
2094 };
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002095 static struct snd_ac97_bus_ops ali_bus_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 .write = snd_intel8x0_ali_codec_write,
2097 .read = snd_intel8x0_ali_codec_read,
2098 };
2099
2100 chip->spdif_idx = -1; /* use PCMOUT (or disabled) */
Takashi Iwaia9e99662006-11-24 15:42:07 +01002101 if (!spdif_aclink) {
2102 switch (chip->device_type) {
2103 case DEVICE_NFORCE:
2104 chip->spdif_idx = NVD_SPBAR;
2105 break;
2106 case DEVICE_ALI:
2107 chip->spdif_idx = ALID_AC97SPDIFOUT;
2108 break;
2109 case DEVICE_INTEL_ICH4:
2110 chip->spdif_idx = ICHD_SPBAR;
2111 break;
2112 };
2113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 chip->in_ac97_init = 1;
2116
2117 memset(&ac97, 0, sizeof(ac97));
2118 ac97.private_data = chip;
2119 ac97.private_free = snd_intel8x0_mixer_free_ac97;
Takashi Iwaif1a63a32006-10-24 18:25:29 +02002120 ac97.scaps = AC97_SCAP_SKIP_MODEM | AC97_SCAP_POWER_SAVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (chip->xbox)
2122 ac97.scaps |= AC97_SCAP_DETECT_BY_VENDOR;
2123 if (chip->device_type != DEVICE_ALI) {
2124 glob_sta = igetdword(chip, ICHREG(GLOB_STA));
2125 ops = &standard_bus_ops;
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002126 chip->in_sdin_init = 1;
2127 codecs = 0;
2128 for (i = 0; i < chip->max_codecs; i++) {
2129 if (! (glob_sta & chip->codec_bit[i]))
2130 continue;
2131 if (chip->device_type == DEVICE_INTEL_ICH4) {
2132 snd_intel8x0_codec_read_test(chip, codecs);
2133 chip->ac97_sdin[codecs] =
2134 igetbyte(chip, ICHREG(SDM)) & ICH_LDI_MASK;
2135 snd_assert(chip->ac97_sdin[codecs] < 3,
2136 chip->ac97_sdin[codecs] = 0);
2137 } else
2138 chip->ac97_sdin[codecs] = i;
2139 codecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002141 chip->in_sdin_init = 0;
2142 if (! codecs)
2143 codecs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 } else {
2145 ops = &ali_bus_ops;
2146 codecs = 1;
2147 /* detect the secondary codec */
2148 for (i = 0; i < 100; i++) {
2149 unsigned int reg = igetdword(chip, ICHREG(ALI_RTSR));
2150 if (reg & 0x40) {
2151 codecs = 2;
2152 break;
2153 }
2154 iputdword(chip, ICHREG(ALI_RTSR), reg | 0x40);
2155 udelay(1);
2156 }
2157 }
2158 if ((err = snd_ac97_bus(chip->card, 0, ops, chip, &pbus)) < 0)
2159 goto __err;
2160 pbus->private_free = snd_intel8x0_mixer_free_ac97_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (ac97_clock >= 8000 && ac97_clock <= 48000)
2162 pbus->clock = ac97_clock;
2163 /* FIXME: my test board doesn't work well with VRA... */
2164 if (chip->device_type == DEVICE_ALI)
2165 pbus->no_vra = 1;
2166 else
2167 pbus->dra = 1;
2168 chip->ac97_bus = pbus;
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002169 chip->ncodecs = codecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 ac97.pci = chip->pci;
2172 for (i = 0; i < codecs; i++) {
2173 ac97.num = i;
2174 if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i])) < 0) {
2175 if (err != -EACCES)
2176 snd_printk(KERN_ERR "Unable to initialize codec #%d\n", i);
2177 if (i == 0)
2178 goto __err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
2180 }
2181 /* tune up the primary codec */
2182 snd_ac97_tune_hardware(chip->ac97[0], ac97_quirks, quirk_override);
2183 /* enable separate SDINs for ICH4 */
2184 if (chip->device_type == DEVICE_INTEL_ICH4)
2185 pbus->isdin = 1;
2186 /* find the available PCM streams */
2187 i = ARRAY_SIZE(ac97_pcm_defs);
2188 if (chip->device_type != DEVICE_INTEL_ICH4)
2189 i -= 2; /* do not allocate PCM2IN and MIC2 */
2190 if (chip->spdif_idx < 0)
2191 i--; /* do not allocate S/PDIF */
2192 err = snd_ac97_pcm_assign(pbus, i, ac97_pcm_defs);
2193 if (err < 0)
2194 goto __err;
2195 chip->ichd[ICHD_PCMOUT].pcm = &pbus->pcms[0];
2196 chip->ichd[ICHD_PCMIN].pcm = &pbus->pcms[1];
2197 chip->ichd[ICHD_MIC].pcm = &pbus->pcms[2];
2198 if (chip->spdif_idx >= 0)
2199 chip->ichd[chip->spdif_idx].pcm = &pbus->pcms[3];
2200 if (chip->device_type == DEVICE_INTEL_ICH4) {
2201 chip->ichd[ICHD_PCM2IN].pcm = &pbus->pcms[4];
2202 chip->ichd[ICHD_MIC2].pcm = &pbus->pcms[5];
2203 }
2204 /* enable separate SDINs for ICH4 */
2205 if (chip->device_type == DEVICE_INTEL_ICH4) {
2206 struct ac97_pcm *pcm = chip->ichd[ICHD_PCM2IN].pcm;
2207 u8 tmp = igetbyte(chip, ICHREG(SDM));
2208 tmp &= ~(ICH_DI2L_MASK|ICH_DI1L_MASK);
2209 if (pcm) {
2210 tmp |= ICH_SE; /* steer enable for multiple SDINs */
2211 tmp |= chip->ac97_sdin[0] << ICH_DI1L_SHIFT;
2212 for (i = 1; i < 4; i++) {
2213 if (pcm->r[0].codec[i]) {
2214 tmp |= chip->ac97_sdin[pcm->r[0].codec[1]->num] << ICH_DI2L_SHIFT;
2215 break;
2216 }
2217 }
2218 } else {
2219 tmp &= ~ICH_SE; /* steer disable */
2220 }
2221 iputbyte(chip, ICHREG(SDM), tmp);
2222 }
2223 if (pbus->pcms[0].r[0].slots & (1 << AC97_SLOT_PCM_SLEFT)) {
2224 chip->multi4 = 1;
Takashi Iwai4235a312008-02-18 12:23:13 +01002225 if (pbus->pcms[0].r[0].slots & (1 << AC97_SLOT_LFE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 chip->multi6 = 1;
Takashi Iwai4235a312008-02-18 12:23:13 +01002227 if (chip->ac97[0]->flags & AC97_HAS_8CH)
2228 chip->multi8 = 1;
2229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
2231 if (pbus->pcms[0].r[1].rslots[0]) {
2232 chip->dra = 1;
2233 }
2234 if (chip->device_type == DEVICE_INTEL_ICH4) {
2235 if ((igetdword(chip, ICHREG(GLOB_STA)) & ICH_SAMPLE_CAP) == ICH_SAMPLE_16_20)
2236 chip->smp20bit = 1;
2237 }
Takashi Iwaia9e99662006-11-24 15:42:07 +01002238 if (chip->device_type == DEVICE_NFORCE && !spdif_aclink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 /* 48kHz only */
2240 chip->ichd[chip->spdif_idx].pcm->rates = SNDRV_PCM_RATE_48000;
2241 }
Takashi Iwaia9e99662006-11-24 15:42:07 +01002242 if (chip->device_type == DEVICE_INTEL_ICH4 && !spdif_aclink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 /* use slot 10/11 for SPDIF */
2244 u32 val;
2245 val = igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_PCM_SPDIF_MASK;
2246 val |= ICH_PCM_SPDIF_1011;
2247 iputdword(chip, ICHREG(GLOB_CNT), val);
2248 snd_ac97_update_bits(chip->ac97[0], AC97_EXTENDED_STATUS, 0x03 << 4, 0x03 << 4);
2249 }
2250 chip->in_ac97_init = 0;
2251 return 0;
2252
2253 __err:
2254 /* clear the cold-reset bit for the next chance */
2255 if (chip->device_type != DEVICE_ALI)
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002256 iputdword(chip, ICHREG(GLOB_CNT),
2257 igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_AC97COLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return err;
2259}
2260
2261
2262/*
2263 *
2264 */
2265
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002266static void do_ali_reset(struct intel8x0 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
2268 iputdword(chip, ICHREG(ALI_SCR), ICH_ALI_SC_RESET);
2269 iputdword(chip, ICHREG(ALI_FIFOCR1), 0x83838383);
2270 iputdword(chip, ICHREG(ALI_FIFOCR2), 0x83838383);
2271 iputdword(chip, ICHREG(ALI_FIFOCR3), 0x83838383);
2272 iputdword(chip, ICHREG(ALI_INTERFACECR),
Wei Nid78bec22005-10-24 11:04:51 +02002273 ICH_ALI_IF_PI|ICH_ALI_IF_PO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 iputdword(chip, ICHREG(ALI_INTERRUPTCR), 0x00000000);
2275 iputdword(chip, ICHREG(ALI_INTERRUPTSR), 0x00000000);
2276}
2277
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002278static int snd_intel8x0_ich_chip_init(struct intel8x0 *chip, int probing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
2280 unsigned long end_time;
2281 unsigned int cnt, status, nstatus;
2282
2283 /* put logic to right state */
2284 /* first clear status bits */
2285 status = ICH_RCS | ICH_MCINT | ICH_POINT | ICH_PIINT;
2286 if (chip->device_type == DEVICE_NFORCE)
2287 status |= ICH_NVSPINT;
2288 cnt = igetdword(chip, ICHREG(GLOB_STA));
2289 iputdword(chip, ICHREG(GLOB_STA), cnt & status);
2290
2291 /* ACLink on, 2 channels */
2292 cnt = igetdword(chip, ICHREG(GLOB_CNT));
2293 cnt &= ~(ICH_ACLINK | ICH_PCM_246_MASK);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002294#ifdef CONFIG_SND_AC97_POWER_SAVE
2295 /* do cold reset - the full ac97 powerdown may leave the controller
2296 * in a warm state but actually it cannot communicate with the codec.
2297 */
2298 iputdword(chip, ICHREG(GLOB_CNT), cnt & ~ICH_AC97COLD);
2299 cnt = igetdword(chip, ICHREG(GLOB_CNT));
2300 udelay(10);
2301 iputdword(chip, ICHREG(GLOB_CNT), cnt | ICH_AC97COLD);
2302 msleep(1);
2303#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 /* finish cold or do warm reset */
2305 cnt |= (cnt & ICH_AC97COLD) == 0 ? ICH_AC97COLD : ICH_AC97WARM;
2306 iputdword(chip, ICHREG(GLOB_CNT), cnt);
2307 end_time = (jiffies + (HZ / 4)) + 1;
2308 do {
2309 if ((igetdword(chip, ICHREG(GLOB_CNT)) & ICH_AC97WARM) == 0)
2310 goto __ok;
Takashi Iwai954bea32005-11-17 10:37:40 +01002311 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 } while (time_after_eq(end_time, jiffies));
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002313 snd_printk(KERN_ERR "AC'97 warm reset still in progress? [0x%x]\n",
2314 igetdword(chip, ICHREG(GLOB_CNT)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return -EIO;
2316
2317 __ok:
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002318#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (probing) {
2320 /* wait for any codec ready status.
2321 * Once it becomes ready it should remain ready
2322 * as long as we do not disable the ac97 link.
2323 */
2324 end_time = jiffies + HZ;
2325 do {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002326 status = igetdword(chip, ICHREG(GLOB_STA)) &
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002327 chip->codec_isr_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (status)
2329 break;
Takashi Iwai954bea32005-11-17 10:37:40 +01002330 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 } while (time_after_eq(end_time, jiffies));
2332 if (! status) {
2333 /* no codec is found */
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002334 snd_printk(KERN_ERR "codec_ready: codec is not ready [0x%x]\n",
2335 igetdword(chip, ICHREG(GLOB_STA)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return -EIO;
2337 }
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /* wait for other codecs ready status. */
2340 end_time = jiffies + HZ / 4;
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002341 while (status != chip->codec_isr_bits &&
2342 time_after_eq(end_time, jiffies)) {
Takashi Iwai954bea32005-11-17 10:37:40 +01002343 schedule_timeout_uninterruptible(1);
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002344 status |= igetdword(chip, ICHREG(GLOB_STA)) &
2345 chip->codec_isr_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347
2348 } else {
2349 /* resume phase */
2350 int i;
2351 status = 0;
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002352 for (i = 0; i < chip->ncodecs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 if (chip->ac97[i])
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002354 status |= chip->codec_bit[chip->ac97_sdin[i]];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /* wait until all the probed codecs are ready */
2356 end_time = jiffies + HZ;
2357 do {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002358 nstatus = igetdword(chip, ICHREG(GLOB_STA)) &
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002359 chip->codec_isr_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (status == nstatus)
2361 break;
Takashi Iwai954bea32005-11-17 10:37:40 +01002362 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 } while (time_after_eq(end_time, jiffies));
2364 }
2365
2366 if (chip->device_type == DEVICE_SIS) {
2367 /* unmute the output on SIS7012 */
2368 iputword(chip, 0x4c, igetword(chip, 0x4c) | 1);
2369 }
Takashi Iwaia9e99662006-11-24 15:42:07 +01002370 if (chip->device_type == DEVICE_NFORCE && !spdif_aclink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 /* enable SPDIF interrupt */
2372 unsigned int val;
2373 pci_read_config_dword(chip->pci, 0x4c, &val);
2374 val |= 0x1000000;
2375 pci_write_config_dword(chip->pci, 0x4c, val);
2376 }
2377 return 0;
2378}
2379
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002380static int snd_intel8x0_ali_chip_init(struct intel8x0 *chip, int probing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 u32 reg;
2383 int i = 0;
2384
2385 reg = igetdword(chip, ICHREG(ALI_SCR));
2386 if ((reg & 2) == 0) /* Cold required */
2387 reg |= 2;
2388 else
2389 reg |= 1; /* Warm */
2390 reg &= ~0x80000000; /* ACLink on */
2391 iputdword(chip, ICHREG(ALI_SCR), reg);
2392
2393 for (i = 0; i < HZ / 2; i++) {
2394 if (! (igetdword(chip, ICHREG(ALI_INTERRUPTSR)) & ALI_INT_GPIO))
2395 goto __ok;
Takashi Iwai954bea32005-11-17 10:37:40 +01002396 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398 snd_printk(KERN_ERR "AC'97 reset failed.\n");
2399 if (probing)
2400 return -EIO;
2401
2402 __ok:
2403 for (i = 0; i < HZ / 2; i++) {
2404 reg = igetdword(chip, ICHREG(ALI_RTSR));
2405 if (reg & 0x80) /* primary codec */
2406 break;
2407 iputdword(chip, ICHREG(ALI_RTSR), reg | 0x80);
Takashi Iwai954bea32005-11-17 10:37:40 +01002408 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
2410
2411 do_ali_reset(chip);
2412 return 0;
2413}
2414
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002415static int snd_intel8x0_chip_init(struct intel8x0 *chip, int probing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Jaroslav Kysela253b9992006-02-15 13:31:23 +01002417 unsigned int i, timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 int err;
2419
2420 if (chip->device_type != DEVICE_ALI) {
2421 if ((err = snd_intel8x0_ich_chip_init(chip, probing)) < 0)
2422 return err;
2423 iagetword(chip, 0); /* clear semaphore flag */
2424 } else {
2425 if ((err = snd_intel8x0_ali_chip_init(chip, probing)) < 0)
2426 return err;
2427 }
2428
2429 /* disable interrupts */
2430 for (i = 0; i < chip->bdbars_count; i++)
2431 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
2432 /* reset channels */
2433 for (i = 0; i < chip->bdbars_count; i++)
2434 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
Jaroslav Kysela253b9992006-02-15 13:31:23 +01002435 for (i = 0; i < chip->bdbars_count; i++) {
2436 timeout = 100000;
2437 while (--timeout != 0) {
2438 if ((igetbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset) & ICH_RESETREGS) == 0)
2439 break;
2440 }
2441 if (timeout == 0)
2442 printk(KERN_ERR "intel8x0: reset of registers failed?\n");
2443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 /* initialize Buffer Descriptor Lists */
2445 for (i = 0; i < chip->bdbars_count; i++)
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002446 iputdword(chip, ICH_REG_OFF_BDBAR + chip->ichd[i].reg_offset,
2447 chip->ichd[i].bdbar_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return 0;
2449}
2450
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002451static int snd_intel8x0_free(struct intel8x0 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
2453 unsigned int i;
2454
2455 if (chip->irq < 0)
2456 goto __hw_end;
2457 /* disable interrupts */
2458 for (i = 0; i < chip->bdbars_count; i++)
2459 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, 0x00);
2460 /* reset channels */
2461 for (i = 0; i < chip->bdbars_count; i++)
2462 iputbyte(chip, ICH_REG_OFF_CR + chip->ichd[i].reg_offset, ICH_RESETREGS);
Takashi Iwaia9e99662006-11-24 15:42:07 +01002463 if (chip->device_type == DEVICE_NFORCE && !spdif_aclink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 /* stop the spdif interrupt */
2465 unsigned int val;
2466 pci_read_config_dword(chip->pci, 0x4c, &val);
2467 val &= ~0x1000000;
2468 pci_write_config_dword(chip->pci, 0x4c, val);
2469 }
2470 /* --- */
2471 synchronize_irq(chip->irq);
2472 __hw_end:
2473 if (chip->irq >= 0)
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002474 free_irq(chip->irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (chip->bdbars.area) {
2476 if (chip->fix_nocache)
2477 fill_nocache(chip->bdbars.area, chip->bdbars.bytes, 0);
2478 snd_dma_free_pages(&chip->bdbars);
2479 }
Takashi Iwai3388c372006-10-06 17:06:39 +02002480 if (chip->addr)
2481 pci_iounmap(chip->pci, chip->addr);
2482 if (chip->bmaddr)
2483 pci_iounmap(chip->pci, chip->bmaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 pci_release_regions(chip->pci);
2485 pci_disable_device(chip->pci);
2486 kfree(chip);
2487 return 0;
2488}
2489
2490#ifdef CONFIG_PM
2491/*
2492 * power management
2493 */
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002494static int intel8x0_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002496 struct snd_card *card = pci_get_drvdata(pci);
2497 struct intel8x0 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 int i;
2499
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002500 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 for (i = 0; i < chip->pcm_devs; i++)
2502 snd_pcm_suspend_all(chip->pcm[i]);
2503 /* clear nocache */
2504 if (chip->fix_nocache) {
2505 for (i = 0; i < chip->bdbars_count; i++) {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002506 struct ichdev *ichdev = &chip->ichd[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if (ichdev->substream && ichdev->page_attr_changed) {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002508 struct snd_pcm_runtime *runtime = ichdev->substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (runtime->dma_area)
2510 fill_nocache(runtime->dma_area, runtime->dma_bytes, 0);
2511 }
2512 }
2513 }
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002514 for (i = 0; i < chip->ncodecs; i++)
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002515 snd_ac97_suspend(chip->ac97[i]);
Takashi Iwai52b72382005-06-30 13:47:06 +02002516 if (chip->device_type == DEVICE_INTEL_ICH4)
2517 chip->sdm_saved = igetbyte(chip, ICHREG(SDM));
Len Brownadbedd32005-07-30 01:55:32 -04002518
Takashi Iwai30b35392006-10-11 18:52:53 +02002519 if (chip->irq >= 0) {
2520 synchronize_irq(chip->irq);
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002521 free_irq(chip->irq, chip);
Takashi Iwai30b35392006-10-11 18:52:53 +02002522 chip->irq = -1;
2523 }
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002524 pci_disable_device(pci);
2525 pci_save_state(pci);
Tommi Kyntola19bfafb2007-03-09 16:15:06 +01002526 /* The call below may disable built-in speaker on some laptops
2527 * after S2RAM. So, don't touch it.
2528 */
2529 /* pci_set_power_state(pci, pci_choose_state(pci, state)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return 0;
2531}
2532
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002533static int intel8x0_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002535 struct snd_card *card = pci_get_drvdata(pci);
2536 struct intel8x0 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 int i;
2538
Takashi Iwai30b35392006-10-11 18:52:53 +02002539 pci_set_power_state(pci, PCI_D0);
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002540 pci_restore_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002541 if (pci_enable_device(pci) < 0) {
2542 printk(KERN_ERR "intel8x0: pci_enable_device failed, "
2543 "disabling device\n");
2544 snd_card_disconnect(card);
2545 return -EIO;
2546 }
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002547 pci_set_master(pci);
Takashi Iwai2078f382007-04-20 12:30:28 +02002548 snd_intel8x0_chip_init(chip, 0);
Takashi Iwai30b35392006-10-11 18:52:53 +02002549 if (request_irq(pci->irq, snd_intel8x0_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002550 IRQF_SHARED, card->shortname, chip)) {
Takashi Iwai30b35392006-10-11 18:52:53 +02002551 printk(KERN_ERR "intel8x0: unable to grab IRQ %d, "
2552 "disabling device\n", pci->irq);
2553 snd_card_disconnect(card);
2554 return -EIO;
2555 }
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002556 chip->irq = pci->irq;
Rafael J. Wysocki90158b82005-07-24 14:22:00 -04002557 synchronize_irq(chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Takashi Iwai52b72382005-06-30 13:47:06 +02002559 /* re-initialize mixer stuff */
Takashi Iwaia9e99662006-11-24 15:42:07 +01002560 if (chip->device_type == DEVICE_INTEL_ICH4 && !spdif_aclink) {
Takashi Iwai52b72382005-06-30 13:47:06 +02002561 /* enable separate SDINs for ICH4 */
2562 iputbyte(chip, ICHREG(SDM), chip->sdm_saved);
2563 /* use slot 10/11 for SPDIF */
2564 iputdword(chip, ICHREG(GLOB_CNT),
2565 (igetdword(chip, ICHREG(GLOB_CNT)) & ~ICH_PCM_SPDIF_MASK) |
2566 ICH_PCM_SPDIF_1011);
2567 }
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 /* refill nocache */
2570 if (chip->fix_nocache)
2571 fill_nocache(chip->bdbars.area, chip->bdbars.bytes, 1);
2572
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002573 for (i = 0; i < chip->ncodecs; i++)
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002574 snd_ac97_resume(chip->ac97[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 /* refill nocache */
2577 if (chip->fix_nocache) {
2578 for (i = 0; i < chip->bdbars_count; i++) {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002579 struct ichdev *ichdev = &chip->ichd[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 if (ichdev->substream && ichdev->page_attr_changed) {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002581 struct snd_pcm_runtime *runtime = ichdev->substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (runtime->dma_area)
2583 fill_nocache(runtime->dma_area, runtime->dma_bytes, 1);
2584 }
2585 }
2586 }
2587
Takashi Iwai1cfe43d2005-08-16 16:52:24 +02002588 /* resume status */
2589 for (i = 0; i < chip->bdbars_count; i++) {
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002590 struct ichdev *ichdev = &chip->ichd[i];
Takashi Iwai1cfe43d2005-08-16 16:52:24 +02002591 unsigned long port = ichdev->reg_offset;
2592 if (! ichdev->substream || ! ichdev->suspended)
2593 continue;
2594 if (ichdev->ichd == ICHD_PCMOUT)
2595 snd_intel8x0_setup_pcm_out(chip, ichdev->substream->runtime);
2596 iputdword(chip, port + ICH_REG_OFF_BDBAR, ichdev->bdbar_addr);
2597 iputbyte(chip, port + ICH_REG_OFF_LVI, ichdev->lvi);
2598 iputbyte(chip, port + ICH_REG_OFF_CIV, ichdev->civ);
2599 iputbyte(chip, port + ichdev->roff_sr, ICH_FIFOE | ICH_BCIS | ICH_LVBCI);
2600 }
2601
Takashi Iwai5809c6c42005-11-17 16:10:01 +01002602 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 return 0;
2604}
2605#endif /* CONFIG_PM */
2606
2607#define INTEL8X0_TESTBUF_SIZE 32768 /* enough large for one shot */
2608
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002609static void __devinit intel8x0_measure_ac97_clock(struct intel8x0 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002611 struct snd_pcm_substream *subs;
2612 struct ichdev *ichdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 unsigned long port;
2614 unsigned long pos, t;
2615 struct timeval start_time, stop_time;
2616
2617 if (chip->ac97_bus->clock != 48000)
2618 return; /* specified in module option */
2619
2620 subs = chip->pcm[0]->streams[0].substream;
2621 if (! subs || subs->dma_buffer.bytes < INTEL8X0_TESTBUF_SIZE) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002622 snd_printk(KERN_WARNING "no playback buffer allocated - aborting measure ac97 clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 return;
2624 }
2625 ichdev = &chip->ichd[ICHD_PCMOUT];
2626 ichdev->physbuf = subs->dma_buffer.addr;
2627 ichdev->size = chip->ichd[ICHD_PCMOUT].fragsize = INTEL8X0_TESTBUF_SIZE;
2628 ichdev->substream = NULL; /* don't process interrupts */
2629
2630 /* set rate */
2631 if (snd_ac97_set_rate(chip->ac97[0], AC97_PCM_FRONT_DAC_RATE, 48000) < 0) {
2632 snd_printk(KERN_ERR "cannot set ac97 rate: clock = %d\n", chip->ac97_bus->clock);
2633 return;
2634 }
2635 snd_intel8x0_setup_periods(chip, ichdev);
2636 port = ichdev->reg_offset;
2637 spin_lock_irq(&chip->reg_lock);
2638 chip->in_measurement = 1;
2639 /* trigger */
2640 if (chip->device_type != DEVICE_ALI)
2641 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE | ICH_STARTBM);
2642 else {
2643 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_IOCE);
2644 iputdword(chip, ICHREG(ALI_DMACR), 1 << ichdev->ali_slot);
2645 }
2646 do_gettimeofday(&start_time);
2647 spin_unlock_irq(&chip->reg_lock);
Nishanth Aravamudanef21ca22005-07-09 10:13:22 +02002648 msleep(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 spin_lock_irq(&chip->reg_lock);
2650 /* check the position */
2651 pos = ichdev->fragsize1;
2652 pos -= igetword(chip, ichdev->reg_offset + ichdev->roff_picb) << ichdev->pos_shift;
2653 pos += ichdev->position;
2654 chip->in_measurement = 0;
2655 do_gettimeofday(&stop_time);
2656 /* stop */
2657 if (chip->device_type == DEVICE_ALI) {
Wei Nid78bec22005-10-24 11:04:51 +02002658 iputdword(chip, ICHREG(ALI_DMACR), 1 << (ichdev->ali_slot + 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 iputbyte(chip, port + ICH_REG_OFF_CR, 0);
2660 while (igetbyte(chip, port + ICH_REG_OFF_CR))
2661 ;
2662 } else {
2663 iputbyte(chip, port + ICH_REG_OFF_CR, 0);
2664 while (!(igetbyte(chip, port + ichdev->roff_sr) & ICH_DCH))
2665 ;
2666 }
2667 iputbyte(chip, port + ICH_REG_OFF_CR, ICH_RESETREGS);
2668 spin_unlock_irq(&chip->reg_lock);
2669
2670 t = stop_time.tv_sec - start_time.tv_sec;
2671 t *= 1000000;
2672 t += stop_time.tv_usec - start_time.tv_usec;
2673 printk(KERN_INFO "%s: measured %lu usecs\n", __FUNCTION__, t);
2674 if (t == 0) {
2675 snd_printk(KERN_ERR "?? calculation error..\n");
2676 return;
2677 }
2678 pos = (pos / 4) * 1000;
2679 pos = (pos / t) * 1000 + ((pos % t) * 1000) / t;
2680 if (pos < 40000 || pos >= 60000)
2681 /* abnormal value. hw problem? */
2682 printk(KERN_INFO "intel8x0: measured clock %ld rejected\n", pos);
2683 else if (pos < 47500 || pos > 48500)
2684 /* not 48000Hz, tuning the clock.. */
2685 chip->ac97_bus->clock = (chip->ac97_bus->clock * 48000) / pos;
2686 printk(KERN_INFO "intel8x0: clocking to %d\n", chip->ac97_bus->clock);
Takashi Iwai6dbe6622006-06-27 18:28:53 +02002687 snd_ac97_update_power(chip->ac97[0], AC97_PCM_FRONT_DAC_RATE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688}
2689
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +01002690#ifdef CONFIG_PROC_FS
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002691static void snd_intel8x0_proc_read(struct snd_info_entry * entry,
2692 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002694 struct intel8x0 *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 unsigned int tmp;
2696
2697 snd_iprintf(buffer, "Intel8x0\n\n");
2698 if (chip->device_type == DEVICE_ALI)
2699 return;
2700 tmp = igetdword(chip, ICHREG(GLOB_STA));
2701 snd_iprintf(buffer, "Global control : 0x%08x\n", igetdword(chip, ICHREG(GLOB_CNT)));
2702 snd_iprintf(buffer, "Global status : 0x%08x\n", tmp);
2703 if (chip->device_type == DEVICE_INTEL_ICH4)
2704 snd_iprintf(buffer, "SDM : 0x%08x\n", igetdword(chip, ICHREG(SDM)));
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002705 snd_iprintf(buffer, "AC'97 codecs ready :");
2706 if (tmp & chip->codec_isr_bits) {
2707 int i;
2708 static const char *codecs[3] = {
2709 "primary", "secondary", "tertiary"
2710 };
2711 for (i = 0; i < chip->max_codecs; i++)
2712 if (tmp & chip->codec_bit[i])
2713 snd_iprintf(buffer, " %s", codecs[i]);
2714 } else
2715 snd_iprintf(buffer, " none");
2716 snd_iprintf(buffer, "\n");
2717 if (chip->device_type == DEVICE_INTEL_ICH4 ||
2718 chip->device_type == DEVICE_SIS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 snd_iprintf(buffer, "AC'97 codecs SDIN : %i %i %i\n",
2720 chip->ac97_sdin[0],
2721 chip->ac97_sdin[1],
2722 chip->ac97_sdin[2]);
2723}
2724
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002725static void __devinit snd_intel8x0_proc_init(struct intel8x0 * chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002727 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729 if (! snd_card_proc_new(chip->card, "intel8x0", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02002730 snd_info_set_text_ops(entry, chip, snd_intel8x0_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +01002732#else
2733#define snd_intel8x0_proc_init(x)
2734#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002736static int snd_intel8x0_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002738 struct intel8x0 *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return snd_intel8x0_free(chip);
2740}
2741
2742struct ich_reg_info {
2743 unsigned int int_sta_mask;
2744 unsigned int offset;
2745};
2746
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002747static unsigned int ich_codec_bits[3] = {
2748 ICH_PCR, ICH_SCR, ICH_TCR
2749};
2750static unsigned int sis_codec_bits[3] = {
2751 ICH_PCR, ICH_SCR, ICH_SIS_TCR
2752};
2753
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002754static int __devinit snd_intel8x0_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 struct pci_dev *pci,
2756 unsigned long device_type,
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002757 struct intel8x0 ** r_intel8x0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002759 struct intel8x0 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 int err;
2761 unsigned int i;
2762 unsigned int int_sta_masks;
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01002763 struct ichdev *ichdev;
2764 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 .dev_free = snd_intel8x0_dev_free,
2766 };
2767
2768 static unsigned int bdbars[] = {
2769 3, /* DEVICE_INTEL */
2770 6, /* DEVICE_INTEL_ICH4 */
2771 3, /* DEVICE_SIS */
2772 6, /* DEVICE_ALI */
2773 4, /* DEVICE_NFORCE */
2774 };
2775 static struct ich_reg_info intel_regs[6] = {
2776 { ICH_PIINT, 0 },
2777 { ICH_POINT, 0x10 },
2778 { ICH_MCINT, 0x20 },
2779 { ICH_M2INT, 0x40 },
2780 { ICH_P2INT, 0x50 },
2781 { ICH_SPINT, 0x60 },
2782 };
2783 static struct ich_reg_info nforce_regs[4] = {
2784 { ICH_PIINT, 0 },
2785 { ICH_POINT, 0x10 },
2786 { ICH_MCINT, 0x20 },
2787 { ICH_NVSPINT, 0x70 },
2788 };
2789 static struct ich_reg_info ali_regs[6] = {
2790 { ALI_INT_PCMIN, 0x40 },
2791 { ALI_INT_PCMOUT, 0x50 },
2792 { ALI_INT_MICIN, 0x60 },
2793 { ALI_INT_CODECSPDIFOUT, 0x70 },
2794 { ALI_INT_SPDIFIN, 0xa0 },
2795 { ALI_INT_SPDIFOUT, 0xb0 },
2796 };
2797 struct ich_reg_info *tbl;
2798
2799 *r_intel8x0 = NULL;
2800
2801 if ((err = pci_enable_device(pci)) < 0)
2802 return err;
2803
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002804 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (chip == NULL) {
2806 pci_disable_device(pci);
2807 return -ENOMEM;
2808 }
2809 spin_lock_init(&chip->reg_lock);
2810 chip->device_type = device_type;
2811 chip->card = card;
2812 chip->pci = pci;
2813 chip->irq = -1;
Takashi Iwaic829b052005-10-18 18:03:35 +02002814
2815 /* module parameters */
2816 chip->buggy_irq = buggy_irq;
2817 chip->buggy_semaphore = buggy_semaphore;
2818 if (xbox)
2819 chip->xbox = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 if (pci->vendor == PCI_VENDOR_ID_INTEL &&
2822 pci->device == PCI_DEVICE_ID_INTEL_440MX)
2823 chip->fix_nocache = 1; /* enable workaround */
2824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if ((err = pci_request_regions(pci, card->shortname)) < 0) {
2826 kfree(chip);
2827 pci_disable_device(pci);
2828 return err;
2829 }
2830
2831 if (device_type == DEVICE_ALI) {
2832 /* ALI5455 has no ac97 region */
Takashi Iwai3388c372006-10-06 17:06:39 +02002833 chip->bmaddr = pci_iomap(pci, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 goto port_inited;
2835 }
2836
Takashi Iwai3388c372006-10-06 17:06:39 +02002837 if (pci_resource_flags(pci, 2) & IORESOURCE_MEM) /* ICH4 and Nforce */
2838 chip->addr = pci_iomap(pci, 2, 0);
2839 else
2840 chip->addr = pci_iomap(pci, 0, 0);
2841 if (!chip->addr) {
2842 snd_printk(KERN_ERR "AC'97 space ioremap problem\n");
2843 snd_intel8x0_free(chip);
2844 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
Takashi Iwai3388c372006-10-06 17:06:39 +02002846 if (pci_resource_flags(pci, 3) & IORESOURCE_MEM) /* ICH4 */
2847 chip->bmaddr = pci_iomap(pci, 3, 0);
2848 else
2849 chip->bmaddr = pci_iomap(pci, 1, 0);
2850 if (!chip->bmaddr) {
2851 snd_printk(KERN_ERR "Controller space ioremap problem\n");
2852 snd_intel8x0_free(chip);
2853 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 }
2855
2856 port_inited:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 chip->bdbars_count = bdbars[device_type];
2858
2859 /* initialize offsets */
2860 switch (device_type) {
2861 case DEVICE_NFORCE:
2862 tbl = nforce_regs;
2863 break;
2864 case DEVICE_ALI:
2865 tbl = ali_regs;
2866 break;
2867 default:
2868 tbl = intel_regs;
2869 break;
2870 }
2871 for (i = 0; i < chip->bdbars_count; i++) {
2872 ichdev = &chip->ichd[i];
2873 ichdev->ichd = i;
2874 ichdev->reg_offset = tbl[i].offset;
2875 ichdev->int_sta_mask = tbl[i].int_sta_mask;
2876 if (device_type == DEVICE_SIS) {
2877 /* SiS 7012 swaps the registers */
2878 ichdev->roff_sr = ICH_REG_OFF_PICB;
2879 ichdev->roff_picb = ICH_REG_OFF_SR;
2880 } else {
2881 ichdev->roff_sr = ICH_REG_OFF_SR;
2882 ichdev->roff_picb = ICH_REG_OFF_PICB;
2883 }
2884 if (device_type == DEVICE_ALI)
2885 ichdev->ali_slot = (ichdev->reg_offset - 0x40) / 0x10;
2886 /* SIS7012 handles the pcm data in bytes, others are in samples */
2887 ichdev->pos_shift = (device_type == DEVICE_SIS) ? 0 : 1;
2888 }
2889
2890 /* allocate buffer descriptor lists */
2891 /* the start of each lists must be aligned to 8 bytes */
2892 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
2893 chip->bdbars_count * sizeof(u32) * ICH_MAX_FRAGS * 2,
2894 &chip->bdbars) < 0) {
2895 snd_intel8x0_free(chip);
2896 snd_printk(KERN_ERR "intel8x0: cannot allocate buffer descriptors\n");
2897 return -ENOMEM;
2898 }
2899 /* tables must be aligned to 8 bytes here, but the kernel pages
2900 are much bigger, so we don't care (on i386) */
2901 /* workaround for 440MX */
2902 if (chip->fix_nocache)
2903 fill_nocache(chip->bdbars.area, chip->bdbars.bytes, 1);
2904 int_sta_masks = 0;
2905 for (i = 0; i < chip->bdbars_count; i++) {
2906 ichdev = &chip->ichd[i];
Takashi Iwaibeef08a2005-10-27 20:55:38 +02002907 ichdev->bdbar = ((u32 *)chip->bdbars.area) +
2908 (i * ICH_MAX_FRAGS * 2);
2909 ichdev->bdbar_addr = chip->bdbars.addr +
2910 (i * sizeof(u32) * ICH_MAX_FRAGS * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 int_sta_masks |= ichdev->int_sta_mask;
2912 }
Takashi Iwaibeef08a2005-10-27 20:55:38 +02002913 chip->int_sta_reg = device_type == DEVICE_ALI ?
2914 ICH_REG_ALI_INTERRUPTSR : ICH_REG_GLOB_STA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 chip->int_sta_mask = int_sta_masks;
2916
Takashi Iwaibeef08a2005-10-27 20:55:38 +02002917 pci_set_master(pci);
Takashi Iwaibeef08a2005-10-27 20:55:38 +02002918
Takashi Iwai84a43bd2006-01-12 11:47:32 +01002919 switch(chip->device_type) {
2920 case DEVICE_INTEL_ICH4:
2921 /* ICH4 can have three codecs */
2922 chip->max_codecs = 3;
2923 chip->codec_bit = ich_codec_bits;
2924 chip->codec_ready_bits = ICH_PRI | ICH_SRI | ICH_TRI;
2925 break;
2926 case DEVICE_SIS:
2927 /* recent SIS7012 can have three codecs */
2928 chip->max_codecs = 3;
2929 chip->codec_bit = sis_codec_bits;
2930 chip->codec_ready_bits = ICH_PRI | ICH_SRI | ICH_SIS_TRI;
2931 break;
2932 default:
2933 /* others up to two codecs */
2934 chip->max_codecs = 2;
2935 chip->codec_bit = ich_codec_bits;
2936 chip->codec_ready_bits = ICH_PRI | ICH_SRI;
2937 break;
2938 }
2939 for (i = 0; i < chip->max_codecs; i++)
2940 chip->codec_isr_bits |= chip->codec_bit[i];
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 if ((err = snd_intel8x0_chip_init(chip, 1)) < 0) {
2943 snd_intel8x0_free(chip);
2944 return err;
2945 }
2946
Takashi Iwai2078f382007-04-20 12:30:28 +02002947 /* request irq after initializaing int_sta_mask, etc */
2948 if (request_irq(pci->irq, snd_intel8x0_interrupt,
2949 IRQF_SHARED, card->shortname, chip)) {
2950 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
2951 snd_intel8x0_free(chip);
2952 return -EBUSY;
2953 }
2954 chip->irq = pci->irq;
2955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2957 snd_intel8x0_free(chip);
2958 return err;
2959 }
2960
2961 snd_card_set_dev(card, &pci->dev);
2962
2963 *r_intel8x0 = chip;
2964 return 0;
2965}
2966
2967static struct shortname_table {
2968 unsigned int id;
2969 const char *s;
2970} shortnames[] __devinitdata = {
Takashi Iwai8cdfd252005-09-07 14:08:11 +02002971 { PCI_DEVICE_ID_INTEL_82801AA_5, "Intel 82801AA-ICH" },
2972 { PCI_DEVICE_ID_INTEL_82801AB_5, "Intel 82901AB-ICH0" },
2973 { PCI_DEVICE_ID_INTEL_82801BA_4, "Intel 82801BA-ICH2" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 { PCI_DEVICE_ID_INTEL_440MX, "Intel 440MX" },
Takashi Iwai8cdfd252005-09-07 14:08:11 +02002975 { PCI_DEVICE_ID_INTEL_82801CA_5, "Intel 82801CA-ICH3" },
2976 { PCI_DEVICE_ID_INTEL_82801DB_5, "Intel 82801DB-ICH4" },
2977 { PCI_DEVICE_ID_INTEL_82801EB_5, "Intel ICH5" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 { PCI_DEVICE_ID_INTEL_ESB_5, "Intel 6300ESB" },
2979 { PCI_DEVICE_ID_INTEL_ICH6_18, "Intel ICH6" },
2980 { PCI_DEVICE_ID_INTEL_ICH7_20, "Intel ICH7" },
Jason Gaston3437c5d2005-05-05 16:15:05 -07002981 { PCI_DEVICE_ID_INTEL_ESB2_14, "Intel ESB2" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 { PCI_DEVICE_ID_SI_7012, "SiS SI7012" },
Takashi Iwai8cdfd252005-09-07 14:08:11 +02002983 { PCI_DEVICE_ID_NVIDIA_MCP1_AUDIO, "NVidia nForce" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 { PCI_DEVICE_ID_NVIDIA_MCP2_AUDIO, "NVidia nForce2" },
2985 { PCI_DEVICE_ID_NVIDIA_MCP3_AUDIO, "NVidia nForce3" },
2986 { PCI_DEVICE_ID_NVIDIA_CK8S_AUDIO, "NVidia CK8S" },
2987 { PCI_DEVICE_ID_NVIDIA_CK804_AUDIO, "NVidia CK804" },
2988 { PCI_DEVICE_ID_NVIDIA_CK8_AUDIO, "NVidia CK8" },
2989 { 0x003a, "NVidia MCP04" },
2990 { 0x746d, "AMD AMD8111" },
2991 { 0x7445, "AMD AMD768" },
2992 { 0x5455, "ALi M5455" },
2993 { 0, NULL },
2994};
2995
Takashi Iwaia9e99662006-11-24 15:42:07 +01002996static struct snd_pci_quirk spdif_aclink_defaults[] __devinitdata = {
2997 SND_PCI_QUIRK(0x147b, 0x1c1a, "ASUS KN8", 1),
2998 { } /* end */
2999};
3000
3001/* look up white/black list for SPDIF over ac-link */
3002static int __devinit check_default_spdif_aclink(struct pci_dev *pci)
3003{
3004 const struct snd_pci_quirk *w;
3005
3006 w = snd_pci_quirk_lookup(pci, spdif_aclink_defaults);
3007 if (w) {
3008 if (w->value)
3009 snd_printdd(KERN_INFO "intel8x0: Using SPDIF over "
3010 "AC-Link for %s\n", w->name);
3011 else
3012 snd_printdd(KERN_INFO "intel8x0: Using integrated "
3013 "SPDIF DMA for %s\n", w->name);
3014 return w->value;
3015 }
3016 return 0;
3017}
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019static int __devinit snd_intel8x0_probe(struct pci_dev *pci,
3020 const struct pci_device_id *pci_id)
3021{
Takashi Iwai6b75a9d2005-11-17 15:04:53 +01003022 struct snd_card *card;
3023 struct intel8x0 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 int err;
3025 struct shortname_table *name;
3026
Clemens Ladischb7fe4622005-10-04 08:46:51 +02003027 card = snd_card_new(index, id, THIS_MODULE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 if (card == NULL)
3029 return -ENOMEM;
3030
Takashi Iwaia9e99662006-11-24 15:42:07 +01003031 if (spdif_aclink < 0)
3032 spdif_aclink = check_default_spdif_aclink(pci);
3033
3034 strcpy(card->driver, "ICH");
3035 if (!spdif_aclink) {
3036 switch (pci_id->driver_data) {
3037 case DEVICE_NFORCE:
3038 strcpy(card->driver, "NFORCE");
3039 break;
3040 case DEVICE_INTEL_ICH4:
3041 strcpy(card->driver, "ICH4");
3042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 }
3044
3045 strcpy(card->shortname, "Intel ICH");
3046 for (name = shortnames; name->id; name++) {
3047 if (pci->device == name->id) {
3048 strcpy(card->shortname, name->s);
3049 break;
3050 }
3051 }
3052
Takashi Iwaibeef08a2005-10-27 20:55:38 +02003053 if (buggy_irq < 0) {
3054 /* some Nforce[2] and ICH boards have problems with IRQ handling.
3055 * Needs to return IRQ_HANDLED for unknown irqs.
3056 */
3057 if (pci_id->driver_data == DEVICE_NFORCE)
3058 buggy_irq = 1;
3059 else
3060 buggy_irq = 0;
3061 }
3062
Takashi Iwaia06147d2005-09-08 19:54:17 +02003063 if ((err = snd_intel8x0_create(card, pci, pci_id->driver_data,
Takashi Iwaic829b052005-10-18 18:03:35 +02003064 &chip)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 snd_card_free(card);
3066 return err;
3067 }
Takashi Iwai5809c6c42005-11-17 16:10:01 +01003068 card->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Clemens Ladischb7fe4622005-10-04 08:46:51 +02003070 if ((err = snd_intel8x0_mixer(chip, ac97_clock, ac97_quirk)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 snd_card_free(card);
3072 return err;
3073 }
3074 if ((err = snd_intel8x0_pcm(chip)) < 0) {
3075 snd_card_free(card);
3076 return err;
3077 }
3078
3079 snd_intel8x0_proc_init(chip);
3080
3081 snprintf(card->longname, sizeof(card->longname),
Takashi Iwai3388c372006-10-06 17:06:39 +02003082 "%s with %s at irq %i", card->shortname,
3083 snd_ac97_get_short_name(chip->ac97[0]), chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Clemens Ladischb7fe4622005-10-04 08:46:51 +02003085 if (! ac97_clock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 intel8x0_measure_ac97_clock(chip);
3087
3088 if ((err = snd_card_register(card)) < 0) {
3089 snd_card_free(card);
3090 return err;
3091 }
3092 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return 0;
3094}
3095
3096static void __devexit snd_intel8x0_remove(struct pci_dev *pci)
3097{
3098 snd_card_free(pci_get_drvdata(pci));
3099 pci_set_drvdata(pci, NULL);
3100}
3101
3102static struct pci_driver driver = {
3103 .name = "Intel ICH",
3104 .id_table = snd_intel8x0_ids,
3105 .probe = snd_intel8x0_probe,
3106 .remove = __devexit_p(snd_intel8x0_remove),
Takashi Iwai5809c6c42005-11-17 16:10:01 +01003107#ifdef CONFIG_PM
3108 .suspend = intel8x0_suspend,
3109 .resume = intel8x0_resume,
3110#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111};
3112
3113
3114static int __init alsa_card_intel8x0_init(void)
3115{
Takashi Iwai01d25d42005-04-11 16:58:24 +02003116 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117}
3118
3119static void __exit alsa_card_intel8x0_exit(void)
3120{
3121 pci_unregister_driver(&driver);
3122}
3123
3124module_init(alsa_card_intel8x0_init)
3125module_exit(alsa_card_intel8x0_exit)