source: GPL/trunk/alsa-kernel/pci/intel8x0.c@ 695

Last change on this file since 695 was 695, checked in by David Azarewicz, 4 years ago

Merge changes from next branch.

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