xref: /linux/sound/pci/atiixp.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *   ALSA driver for ATI IXP 150/200/250/300 AC97 controllers
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
86cbbfe1cSTakashi Iwai #include <linux/io.h>
91da177e4SLinus Torvalds #include <linux/delay.h>
101da177e4SLinus Torvalds #include <linux/interrupt.h>
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/pci.h>
131da177e4SLinus Torvalds #include <linux/slab.h>
1465a77217SPaul Gortmaker #include <linux/module.h>
1562932df8SIngo Molnar #include <linux/mutex.h>
161da177e4SLinus Torvalds #include <sound/core.h>
171da177e4SLinus Torvalds #include <sound/pcm.h>
181da177e4SLinus Torvalds #include <sound/pcm_params.h>
191da177e4SLinus Torvalds #include <sound/info.h>
201da177e4SLinus Torvalds #include <sound/ac97_codec.h>
211da177e4SLinus Torvalds #include <sound/initval.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
241da177e4SLinus Torvalds MODULE_DESCRIPTION("ATI IXP AC97 controller");
251da177e4SLinus Torvalds MODULE_LICENSE("GPL");
261da177e4SLinus Torvalds 
27b7fe4622SClemens Ladisch static int index = SNDRV_DEFAULT_IDX1;	/* Index 0-MAX */
28b7fe4622SClemens Ladisch static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
29b7fe4622SClemens Ladisch static int ac97_clock = 48000;
30b7fe4622SClemens Ladisch static char *ac97_quirk;
31a67ff6a5SRusty Russell static bool spdif_aclink = 1;
3214e1d357SDan Carpenter static int ac97_codec = -1;
331da177e4SLinus Torvalds 
34b7fe4622SClemens Ladisch module_param(index, int, 0444);
351da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for ATI IXP controller.");
36b7fe4622SClemens Ladisch module_param(id, charp, 0444);
371da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for ATI IXP controller.");
38b7fe4622SClemens Ladisch module_param(ac97_clock, int, 0444);
391da177e4SLinus Torvalds MODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
40b7fe4622SClemens Ladisch module_param(ac97_quirk, charp, 0444);
411da177e4SLinus Torvalds MODULE_PARM_DESC(ac97_quirk, "AC'97 workaround for strange hardware.");
4214e1d357SDan Carpenter module_param(ac97_codec, int, 0444);
4314e1d357SDan Carpenter MODULE_PARM_DESC(ac97_codec, "Specify codec instead of probing.");
44b7fe4622SClemens Ladisch module_param(spdif_aclink, bool, 0444);
451da177e4SLinus Torvalds MODULE_PARM_DESC(spdif_aclink, "S/PDIF over AC-link.");
461da177e4SLinus Torvalds 
472b3e584bSTakashi Iwai /* just for backward compatibility */
48a67ff6a5SRusty Russell static bool enable;
49698444f3STakashi Iwai module_param(enable, bool, 0444);
502b3e584bSTakashi Iwai 
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  */
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds #define ATI_REG_ISR			0x00	/* interrupt source */
561da177e4SLinus Torvalds #define  ATI_REG_ISR_IN_XRUN		(1U<<0)
571da177e4SLinus Torvalds #define  ATI_REG_ISR_IN_STATUS		(1U<<1)
581da177e4SLinus Torvalds #define  ATI_REG_ISR_OUT_XRUN		(1U<<2)
591da177e4SLinus Torvalds #define  ATI_REG_ISR_OUT_STATUS		(1U<<3)
601da177e4SLinus Torvalds #define  ATI_REG_ISR_SPDF_XRUN		(1U<<4)
611da177e4SLinus Torvalds #define  ATI_REG_ISR_SPDF_STATUS	(1U<<5)
621da177e4SLinus Torvalds #define  ATI_REG_ISR_PHYS_INTR		(1U<<8)
631da177e4SLinus Torvalds #define  ATI_REG_ISR_PHYS_MISMATCH	(1U<<9)
641da177e4SLinus Torvalds #define  ATI_REG_ISR_CODEC0_NOT_READY	(1U<<10)
651da177e4SLinus Torvalds #define  ATI_REG_ISR_CODEC1_NOT_READY	(1U<<11)
661da177e4SLinus Torvalds #define  ATI_REG_ISR_CODEC2_NOT_READY	(1U<<12)
671da177e4SLinus Torvalds #define  ATI_REG_ISR_NEW_FRAME		(1U<<13)
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #define ATI_REG_IER			0x04	/* interrupt enable */
701da177e4SLinus Torvalds #define  ATI_REG_IER_IN_XRUN_EN		(1U<<0)
711da177e4SLinus Torvalds #define  ATI_REG_IER_IO_STATUS_EN	(1U<<1)
721da177e4SLinus Torvalds #define  ATI_REG_IER_OUT_XRUN_EN	(1U<<2)
731da177e4SLinus Torvalds #define  ATI_REG_IER_OUT_XRUN_COND	(1U<<3)
741da177e4SLinus Torvalds #define  ATI_REG_IER_SPDF_XRUN_EN	(1U<<4)
751da177e4SLinus Torvalds #define  ATI_REG_IER_SPDF_STATUS_EN	(1U<<5)
761da177e4SLinus Torvalds #define  ATI_REG_IER_PHYS_INTR_EN	(1U<<8)
771da177e4SLinus Torvalds #define  ATI_REG_IER_PHYS_MISMATCH_EN	(1U<<9)
781da177e4SLinus Torvalds #define  ATI_REG_IER_CODEC0_INTR_EN	(1U<<10)
791da177e4SLinus Torvalds #define  ATI_REG_IER_CODEC1_INTR_EN	(1U<<11)
801da177e4SLinus Torvalds #define  ATI_REG_IER_CODEC2_INTR_EN	(1U<<12)
811da177e4SLinus Torvalds #define  ATI_REG_IER_NEW_FRAME_EN	(1U<<13)	/* (RO */
821da177e4SLinus Torvalds #define  ATI_REG_IER_SET_BUS_BUSY	(1U<<14)	/* (WO) audio is running */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds #define ATI_REG_CMD			0x08	/* command */
851da177e4SLinus Torvalds #define  ATI_REG_CMD_POWERDOWN		(1U<<0)
861da177e4SLinus Torvalds #define  ATI_REG_CMD_RECEIVE_EN		(1U<<1)
871da177e4SLinus Torvalds #define  ATI_REG_CMD_SEND_EN		(1U<<2)
881da177e4SLinus Torvalds #define  ATI_REG_CMD_STATUS_MEM		(1U<<3)
891da177e4SLinus Torvalds #define  ATI_REG_CMD_SPDF_OUT_EN	(1U<<4)
901da177e4SLinus Torvalds #define  ATI_REG_CMD_SPDF_STATUS_MEM	(1U<<5)
911da177e4SLinus Torvalds #define  ATI_REG_CMD_SPDF_THRESHOLD	(3U<<6)
921da177e4SLinus Torvalds #define  ATI_REG_CMD_SPDF_THRESHOLD_SHIFT	6
931da177e4SLinus Torvalds #define  ATI_REG_CMD_IN_DMA_EN		(1U<<8)
941da177e4SLinus Torvalds #define  ATI_REG_CMD_OUT_DMA_EN		(1U<<9)
951da177e4SLinus Torvalds #define  ATI_REG_CMD_SPDF_DMA_EN	(1U<<10)
961da177e4SLinus Torvalds #define  ATI_REG_CMD_SPDF_OUT_STOPPED	(1U<<11)
971da177e4SLinus Torvalds #define  ATI_REG_CMD_SPDF_CONFIG_MASK	(7U<<12)
981da177e4SLinus Torvalds #define   ATI_REG_CMD_SPDF_CONFIG_34	(1U<<12)
991da177e4SLinus Torvalds #define   ATI_REG_CMD_SPDF_CONFIG_78	(2U<<12)
1001da177e4SLinus Torvalds #define   ATI_REG_CMD_SPDF_CONFIG_69	(3U<<12)
1011da177e4SLinus Torvalds #define   ATI_REG_CMD_SPDF_CONFIG_01	(4U<<12)
1021da177e4SLinus Torvalds #define  ATI_REG_CMD_INTERLEAVE_SPDF	(1U<<16)
1031da177e4SLinus Torvalds #define  ATI_REG_CMD_AUDIO_PRESENT	(1U<<20)
1041da177e4SLinus Torvalds #define  ATI_REG_CMD_INTERLEAVE_IN	(1U<<21)
1051da177e4SLinus Torvalds #define  ATI_REG_CMD_INTERLEAVE_OUT	(1U<<22)
1061da177e4SLinus Torvalds #define  ATI_REG_CMD_LOOPBACK_EN	(1U<<23)
1071da177e4SLinus Torvalds #define  ATI_REG_CMD_PACKED_DIS		(1U<<24)
1081da177e4SLinus Torvalds #define  ATI_REG_CMD_BURST_EN		(1U<<25)
1091da177e4SLinus Torvalds #define  ATI_REG_CMD_PANIC_EN		(1U<<26)
1101da177e4SLinus Torvalds #define  ATI_REG_CMD_MODEM_PRESENT	(1U<<27)
1111da177e4SLinus Torvalds #define  ATI_REG_CMD_ACLINK_ACTIVE	(1U<<28)
1121da177e4SLinus Torvalds #define  ATI_REG_CMD_AC_SOFT_RESET	(1U<<29)
1131da177e4SLinus Torvalds #define  ATI_REG_CMD_AC_SYNC		(1U<<30)
1141da177e4SLinus Torvalds #define  ATI_REG_CMD_AC_RESET		(1U<<31)
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #define ATI_REG_PHYS_OUT_ADDR		0x0c
1171da177e4SLinus Torvalds #define  ATI_REG_PHYS_OUT_CODEC_MASK	(3U<<0)
1181da177e4SLinus Torvalds #define  ATI_REG_PHYS_OUT_RW		(1U<<2)
1191da177e4SLinus Torvalds #define  ATI_REG_PHYS_OUT_ADDR_EN	(1U<<8)
1201da177e4SLinus Torvalds #define  ATI_REG_PHYS_OUT_ADDR_SHIFT	9
1211da177e4SLinus Torvalds #define  ATI_REG_PHYS_OUT_DATA_SHIFT	16
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds #define ATI_REG_PHYS_IN_ADDR		0x10
1241da177e4SLinus Torvalds #define  ATI_REG_PHYS_IN_READ_FLAG	(1U<<8)
1251da177e4SLinus Torvalds #define  ATI_REG_PHYS_IN_ADDR_SHIFT	9
1261da177e4SLinus Torvalds #define  ATI_REG_PHYS_IN_DATA_SHIFT	16
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds #define ATI_REG_SLOTREQ			0x14
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds #define ATI_REG_COUNTER			0x18
1311da177e4SLinus Torvalds #define  ATI_REG_COUNTER_SLOT		(3U<<0)	/* slot # */
1321da177e4SLinus Torvalds #define  ATI_REG_COUNTER_BITCLOCK	(31U<<8)
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds #define ATI_REG_IN_FIFO_THRESHOLD	0x1c
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds #define ATI_REG_IN_DMA_LINKPTR		0x20
1371da177e4SLinus Torvalds #define ATI_REG_IN_DMA_DT_START		0x24	/* RO */
1381da177e4SLinus Torvalds #define ATI_REG_IN_DMA_DT_NEXT		0x28	/* RO */
1391da177e4SLinus Torvalds #define ATI_REG_IN_DMA_DT_CUR		0x2c	/* RO */
1401da177e4SLinus Torvalds #define ATI_REG_IN_DMA_DT_SIZE		0x30
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds #define ATI_REG_OUT_DMA_SLOT		0x34
1431da177e4SLinus Torvalds #define  ATI_REG_OUT_DMA_SLOT_BIT(x)	(1U << ((x) - 3))
1441da177e4SLinus Torvalds #define  ATI_REG_OUT_DMA_SLOT_MASK	0x1ff
1451da177e4SLinus Torvalds #define  ATI_REG_OUT_DMA_THRESHOLD_MASK	0xf800
1461da177e4SLinus Torvalds #define  ATI_REG_OUT_DMA_THRESHOLD_SHIFT	11
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds #define ATI_REG_OUT_DMA_LINKPTR		0x38
1491da177e4SLinus Torvalds #define ATI_REG_OUT_DMA_DT_START	0x3c	/* RO */
1501da177e4SLinus Torvalds #define ATI_REG_OUT_DMA_DT_NEXT		0x40	/* RO */
1511da177e4SLinus Torvalds #define ATI_REG_OUT_DMA_DT_CUR		0x44	/* RO */
1521da177e4SLinus Torvalds #define ATI_REG_OUT_DMA_DT_SIZE		0x48
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds #define ATI_REG_SPDF_CMD		0x4c
1551da177e4SLinus Torvalds #define  ATI_REG_SPDF_CMD_LFSR		(1U<<4)
1561da177e4SLinus Torvalds #define  ATI_REG_SPDF_CMD_SINGLE_CH	(1U<<5)
1571da177e4SLinus Torvalds #define  ATI_REG_SPDF_CMD_LFSR_ACC	(0xff<<8)	/* RO */
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds #define ATI_REG_SPDF_DMA_LINKPTR	0x50
1601da177e4SLinus Torvalds #define ATI_REG_SPDF_DMA_DT_START	0x54	/* RO */
1611da177e4SLinus Torvalds #define ATI_REG_SPDF_DMA_DT_NEXT	0x58	/* RO */
1621da177e4SLinus Torvalds #define ATI_REG_SPDF_DMA_DT_CUR		0x5c	/* RO */
1631da177e4SLinus Torvalds #define ATI_REG_SPDF_DMA_DT_SIZE	0x60
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds #define ATI_REG_MODEM_MIRROR		0x7c
1661da177e4SLinus Torvalds #define ATI_REG_AUDIO_MIRROR		0x80
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds #define ATI_REG_6CH_REORDER		0x84	/* reorder slots for 6ch */
1691da177e4SLinus Torvalds #define  ATI_REG_6CH_REORDER_EN		(1U<<0)	/* 3,4,7,8,6,9 -> 3,4,6,9,7,8 */
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds #define ATI_REG_FIFO_FLUSH		0x88
1721da177e4SLinus Torvalds #define  ATI_REG_FIFO_OUT_FLUSH		(1U<<0)
1731da177e4SLinus Torvalds #define  ATI_REG_FIFO_IN_FLUSH		(1U<<1)
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /* LINKPTR */
1761da177e4SLinus Torvalds #define  ATI_REG_LINKPTR_EN		(1U<<0)
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* [INT|OUT|SPDIF]_DMA_DT_SIZE */
1791da177e4SLinus Torvalds #define  ATI_REG_DMA_DT_SIZE		(0xffffU<<0)
1801da177e4SLinus Torvalds #define  ATI_REG_DMA_FIFO_USED		(0x1fU<<16)
1811da177e4SLinus Torvalds #define  ATI_REG_DMA_FIFO_FREE		(0x1fU<<21)
1821da177e4SLinus Torvalds #define  ATI_REG_DMA_STATE		(7U<<26)
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds #define ATI_MAX_DESCRIPTORS	256	/* max number of descriptor packets */
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 
18874ee4ff1STakashi Iwai struct atiixp;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds /*
1911da177e4SLinus Torvalds  * DMA packate descriptor
1921da177e4SLinus Torvalds  */
1931da177e4SLinus Torvalds 
19474ee4ff1STakashi Iwai struct atiixp_dma_desc {
195c44a81a4STakashi Iwai 	__le32 addr;	/* DMA buffer address */
1961da177e4SLinus Torvalds 	u16 status;	/* status bits */
1971da177e4SLinus Torvalds 	u16 size;	/* size of the packet in dwords */
198c44a81a4STakashi Iwai 	__le32 next;	/* address of the next packet descriptor */
19974ee4ff1STakashi Iwai };
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /*
2021da177e4SLinus Torvalds  * stream enum
2031da177e4SLinus Torvalds  */
2041da177e4SLinus Torvalds enum { ATI_DMA_PLAYBACK, ATI_DMA_CAPTURE, ATI_DMA_SPDIF, NUM_ATI_DMAS }; /* DMAs */
2051da177e4SLinus Torvalds enum { ATI_PCM_OUT, ATI_PCM_IN, ATI_PCM_SPDIF, NUM_ATI_PCMS }; /* AC97 pcm slots */
2061da177e4SLinus Torvalds enum { ATI_PCMDEV_ANALOG, ATI_PCMDEV_DIGITAL, NUM_ATI_PCMDEVS }; /* pcm devices */
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds #define NUM_ATI_CODECS	3
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /*
2121da177e4SLinus Torvalds  * constants and callbacks for each DMA type
2131da177e4SLinus Torvalds  */
21474ee4ff1STakashi Iwai struct atiixp_dma_ops {
2151da177e4SLinus Torvalds 	int type;			/* ATI_DMA_XXX */
2161da177e4SLinus Torvalds 	unsigned int llp_offset;	/* LINKPTR offset */
2171da177e4SLinus Torvalds 	unsigned int dt_cur;		/* DT_CUR offset */
21874ee4ff1STakashi Iwai 	/* called from open callback */
21974ee4ff1STakashi Iwai 	void (*enable_dma)(struct atiixp *chip, int on);
22074ee4ff1STakashi Iwai 	/* called from trigger (START/STOP) */
22174ee4ff1STakashi Iwai 	void (*enable_transfer)(struct atiixp *chip, int on);
22274ee4ff1STakashi Iwai  	/* called from trigger (STOP only) */
22374ee4ff1STakashi Iwai 	void (*flush_dma)(struct atiixp *chip);
2241da177e4SLinus Torvalds };
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds /*
2271da177e4SLinus Torvalds  * DMA stream
2281da177e4SLinus Torvalds  */
22974ee4ff1STakashi Iwai struct atiixp_dma {
23074ee4ff1STakashi Iwai 	const struct atiixp_dma_ops *ops;
2311da177e4SLinus Torvalds 	struct snd_dma_buffer desc_buf;
23274ee4ff1STakashi Iwai 	struct snd_pcm_substream *substream;	/* assigned PCM substream */
2331da177e4SLinus Torvalds 	unsigned int buf_addr, buf_bytes;	/* DMA buffer address, bytes */
2341da177e4SLinus Torvalds 	unsigned int period_bytes, periods;
2351da177e4SLinus Torvalds 	int opened;
2361da177e4SLinus Torvalds 	int running;
23741e4845cSJaroslav Kysela 	int suspended;
2381da177e4SLinus Torvalds 	int pcm_open_flag;
2391da177e4SLinus Torvalds 	int ac97_pcm_type;	/* index # of ac97_pcm to access, -1 = not used */
2401da177e4SLinus Torvalds 	unsigned int saved_curptr;
2411da177e4SLinus Torvalds };
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds /*
2441da177e4SLinus Torvalds  * ATI IXP chip
2451da177e4SLinus Torvalds  */
24674ee4ff1STakashi Iwai struct atiixp {
24774ee4ff1STakashi Iwai 	struct snd_card *card;
2481da177e4SLinus Torvalds 	struct pci_dev *pci;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	unsigned long addr;
2511da177e4SLinus Torvalds 	void __iomem *remap_addr;
2521da177e4SLinus Torvalds 	int irq;
2531da177e4SLinus Torvalds 
25474ee4ff1STakashi Iwai 	struct snd_ac97_bus *ac97_bus;
25574ee4ff1STakashi Iwai 	struct snd_ac97 *ac97[NUM_ATI_CODECS];
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	spinlock_t reg_lock;
2581da177e4SLinus Torvalds 
25974ee4ff1STakashi Iwai 	struct atiixp_dma dmas[NUM_ATI_DMAS];
2601da177e4SLinus Torvalds 	struct ac97_pcm *pcms[NUM_ATI_PCMS];
26174ee4ff1STakashi Iwai 	struct snd_pcm *pcmdevs[NUM_ATI_PCMDEVS];
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	int max_channels;		/* max. channels for PCM out */
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	unsigned int codec_not_ready_bits;	/* for codec detection */
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	int spdif_over_aclink;		/* passed from the module option */
26862932df8SIngo Molnar 	struct mutex open_mutex;	/* playback open mutex */
2691da177e4SLinus Torvalds };
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds /*
2731da177e4SLinus Torvalds  */
2749baa3c34SBenoit Taine static const struct pci_device_id snd_atiixp_ids[] = {
27528d27aaeSJoe Perches 	{ PCI_VDEVICE(ATI, 0x4341), 0 }, /* SB200 */
27628d27aaeSJoe Perches 	{ PCI_VDEVICE(ATI, 0x4361), 0 }, /* SB300 */
27728d27aaeSJoe Perches 	{ PCI_VDEVICE(ATI, 0x4370), 0 }, /* SB400 */
27828d27aaeSJoe Perches 	{ PCI_VDEVICE(ATI, 0x4382), 0 }, /* SB600 */
2791da177e4SLinus Torvalds 	{ 0, }
2801da177e4SLinus Torvalds };
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_atiixp_ids);
2831da177e4SLinus Torvalds 
28488e540a8STakashi Iwai static const struct snd_pci_quirk atiixp_quirks[] = {
285dfb12eebSDaniel T Chen 	SND_PCI_QUIRK(0x105b, 0x0c81, "Foxconn RC4107MA-RS2", 0),
286f41bea84STakashi Iwai 	SND_PCI_QUIRK(0x15bd, 0x3100, "DFI RS482", 0),
287f41bea84STakashi Iwai 	{ } /* terminator */
28814e1d357SDan Carpenter };
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds /*
2911da177e4SLinus Torvalds  * lowlevel functions
2921da177e4SLinus Torvalds  */
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds /*
2951da177e4SLinus Torvalds  * update the bits of the given register.
2961da177e4SLinus Torvalds  * return 1 if the bits changed.
2971da177e4SLinus Torvalds  */
snd_atiixp_update_bits(struct atiixp * chip,unsigned int reg,unsigned int mask,unsigned int value)29874ee4ff1STakashi Iwai static int snd_atiixp_update_bits(struct atiixp *chip, unsigned int reg,
2991da177e4SLinus Torvalds 				 unsigned int mask, unsigned int value)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	void __iomem *addr = chip->remap_addr + reg;
3021da177e4SLinus Torvalds 	unsigned int data, old_data;
3031da177e4SLinus Torvalds 	old_data = data = readl(addr);
3041da177e4SLinus Torvalds 	data &= ~mask;
3051da177e4SLinus Torvalds 	data |= value;
3061da177e4SLinus Torvalds 	if (old_data == data)
3071da177e4SLinus Torvalds 		return 0;
3081da177e4SLinus Torvalds 	writel(data, addr);
3091da177e4SLinus Torvalds 	return 1;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds /*
3131da177e4SLinus Torvalds  * macros for easy use
3141da177e4SLinus Torvalds  */
3151da177e4SLinus Torvalds #define atiixp_write(chip,reg,value) \
3161da177e4SLinus Torvalds 	writel(value, chip->remap_addr + ATI_REG_##reg)
3171da177e4SLinus Torvalds #define atiixp_read(chip,reg) \
3181da177e4SLinus Torvalds 	readl(chip->remap_addr + ATI_REG_##reg)
3191da177e4SLinus Torvalds #define atiixp_update(chip,reg,mask,val) \
3201da177e4SLinus Torvalds 	snd_atiixp_update_bits(chip, ATI_REG_##reg, mask, val)
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds /*
3231da177e4SLinus Torvalds  * handling DMA packets
3241da177e4SLinus Torvalds  *
3251da177e4SLinus Torvalds  * we allocate a linear buffer for the DMA, and split it to  each packet.
3261da177e4SLinus Torvalds  * in a future version, a scatter-gather buffer should be implemented.
3271da177e4SLinus Torvalds  */
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds #define ATI_DESC_LIST_SIZE \
33074ee4ff1STakashi Iwai 	PAGE_ALIGN(ATI_MAX_DESCRIPTORS * sizeof(struct atiixp_dma_desc))
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds /*
3331da177e4SLinus Torvalds  * build packets ring for the given buffer size.
3341da177e4SLinus Torvalds  *
3351da177e4SLinus Torvalds  * IXP handles the buffer descriptors, which are connected as a linked
3361da177e4SLinus Torvalds  * list.  although we can change the list dynamically, in this version,
3371da177e4SLinus Torvalds  * a static RING of buffer descriptors is used.
3381da177e4SLinus Torvalds  *
3391da177e4SLinus Torvalds  * the ring is built in this function, and is set up to the hardware.
3401da177e4SLinus Torvalds  */
atiixp_build_dma_packets(struct atiixp * chip,struct atiixp_dma * dma,struct snd_pcm_substream * substream,unsigned int periods,unsigned int period_bytes)34174ee4ff1STakashi Iwai static int atiixp_build_dma_packets(struct atiixp *chip, struct atiixp_dma *dma,
34274ee4ff1STakashi Iwai 				    struct snd_pcm_substream *substream,
3431da177e4SLinus Torvalds 				    unsigned int periods,
3441da177e4SLinus Torvalds 				    unsigned int period_bytes)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds 	unsigned int i;
3471da177e4SLinus Torvalds 	u32 addr, desc_addr;
3481da177e4SLinus Torvalds 	unsigned long flags;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	if (periods > ATI_MAX_DESCRIPTORS)
3511da177e4SLinus Torvalds 		return -ENOMEM;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	if (dma->desc_buf.area == NULL) {
35474ee4ff1STakashi Iwai 		if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
3556974f8adSTakashi Iwai 					&chip->pci->dev,
35674ee4ff1STakashi Iwai 					ATI_DESC_LIST_SIZE,
35774ee4ff1STakashi Iwai 					&dma->desc_buf) < 0)
3581da177e4SLinus Torvalds 			return -ENOMEM;
3591da177e4SLinus Torvalds 		dma->period_bytes = dma->periods = 0; /* clear */
3601da177e4SLinus Torvalds 	}
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	if (dma->periods == periods && dma->period_bytes == period_bytes)
3631da177e4SLinus Torvalds 		return 0;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	/* reset DMA before changing the descriptor table */
3661da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
3671da177e4SLinus Torvalds 	writel(0, chip->remap_addr + dma->ops->llp_offset);
3681da177e4SLinus Torvalds 	dma->ops->enable_dma(chip, 0);
3691da177e4SLinus Torvalds 	dma->ops->enable_dma(chip, 1);
3701da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	/* fill the entries */
3731da177e4SLinus Torvalds 	addr = (u32)substream->runtime->dma_addr;
3741da177e4SLinus Torvalds 	desc_addr = (u32)dma->desc_buf.addr;
3751da177e4SLinus Torvalds 	for (i = 0; i < periods; i++) {
37674ee4ff1STakashi Iwai 		struct atiixp_dma_desc *desc;
37774ee4ff1STakashi Iwai 		desc = &((struct atiixp_dma_desc *)dma->desc_buf.area)[i];
3781da177e4SLinus Torvalds 		desc->addr = cpu_to_le32(addr);
3791da177e4SLinus Torvalds 		desc->status = 0;
3801da177e4SLinus Torvalds 		desc->size = period_bytes >> 2; /* in dwords */
38174ee4ff1STakashi Iwai 		desc_addr += sizeof(struct atiixp_dma_desc);
3821da177e4SLinus Torvalds 		if (i == periods - 1)
3831da177e4SLinus Torvalds 			desc->next = cpu_to_le32((u32)dma->desc_buf.addr);
3841da177e4SLinus Torvalds 		else
3851da177e4SLinus Torvalds 			desc->next = cpu_to_le32(desc_addr);
3861da177e4SLinus Torvalds 		addr += period_bytes;
3871da177e4SLinus Torvalds 	}
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
3901da177e4SLinus Torvalds 	       chip->remap_addr + dma->ops->llp_offset);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	dma->period_bytes = period_bytes;
3931da177e4SLinus Torvalds 	dma->periods = periods;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	return 0;
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds /*
3991da177e4SLinus Torvalds  * remove the ring buffer and release it if assigned
4001da177e4SLinus Torvalds  */
atiixp_clear_dma_packets(struct atiixp * chip,struct atiixp_dma * dma,struct snd_pcm_substream * substream)40174ee4ff1STakashi Iwai static void atiixp_clear_dma_packets(struct atiixp *chip, struct atiixp_dma *dma,
40274ee4ff1STakashi Iwai 				     struct snd_pcm_substream *substream)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	if (dma->desc_buf.area) {
4051da177e4SLinus Torvalds 		writel(0, chip->remap_addr + dma->ops->llp_offset);
4061da177e4SLinus Torvalds 		snd_dma_free_pages(&dma->desc_buf);
4071da177e4SLinus Torvalds 		dma->desc_buf.area = NULL;
4081da177e4SLinus Torvalds 	}
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds /*
4121da177e4SLinus Torvalds  * AC97 interface
4131da177e4SLinus Torvalds  */
snd_atiixp_acquire_codec(struct atiixp * chip)41474ee4ff1STakashi Iwai static int snd_atiixp_acquire_codec(struct atiixp *chip)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	int timeout = 1000;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	while (atiixp_read(chip, PHYS_OUT_ADDR) & ATI_REG_PHYS_OUT_ADDR_EN) {
4191da177e4SLinus Torvalds 		if (! timeout--) {
42025135fdcSTakashi Iwai 			dev_warn(chip->card->dev, "codec acquire timeout\n");
4211da177e4SLinus Torvalds 			return -EBUSY;
4221da177e4SLinus Torvalds 		}
4231da177e4SLinus Torvalds 		udelay(1);
4241da177e4SLinus Torvalds 	}
4251da177e4SLinus Torvalds 	return 0;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
snd_atiixp_codec_read(struct atiixp * chip,unsigned short codec,unsigned short reg)42874ee4ff1STakashi Iwai static unsigned short snd_atiixp_codec_read(struct atiixp *chip, unsigned short codec, unsigned short reg)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	unsigned int data;
4311da177e4SLinus Torvalds 	int timeout;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	if (snd_atiixp_acquire_codec(chip) < 0)
4341da177e4SLinus Torvalds 		return 0xffff;
4351da177e4SLinus Torvalds 	data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
4361da177e4SLinus Torvalds 		ATI_REG_PHYS_OUT_ADDR_EN |
4371da177e4SLinus Torvalds 		ATI_REG_PHYS_OUT_RW |
4381da177e4SLinus Torvalds 		codec;
4391da177e4SLinus Torvalds 	atiixp_write(chip, PHYS_OUT_ADDR, data);
4401da177e4SLinus Torvalds 	if (snd_atiixp_acquire_codec(chip) < 0)
4411da177e4SLinus Torvalds 		return 0xffff;
4421da177e4SLinus Torvalds 	timeout = 1000;
4431da177e4SLinus Torvalds 	do {
4441da177e4SLinus Torvalds 		data = atiixp_read(chip, PHYS_IN_ADDR);
4451da177e4SLinus Torvalds 		if (data & ATI_REG_PHYS_IN_READ_FLAG)
4461da177e4SLinus Torvalds 			return data >> ATI_REG_PHYS_IN_DATA_SHIFT;
4471da177e4SLinus Torvalds 		udelay(1);
4481da177e4SLinus Torvalds 	} while (--timeout);
4491da177e4SLinus Torvalds 	/* time out may happen during reset */
4501da177e4SLinus Torvalds 	if (reg < 0x7c)
45125135fdcSTakashi Iwai 		dev_warn(chip->card->dev, "codec read timeout (reg %x)\n", reg);
4521da177e4SLinus Torvalds 	return 0xffff;
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 
snd_atiixp_codec_write(struct atiixp * chip,unsigned short codec,unsigned short reg,unsigned short val)45674ee4ff1STakashi Iwai static void snd_atiixp_codec_write(struct atiixp *chip, unsigned short codec,
45774ee4ff1STakashi Iwai 				   unsigned short reg, unsigned short val)
4581da177e4SLinus Torvalds {
4591da177e4SLinus Torvalds 	unsigned int data;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	if (snd_atiixp_acquire_codec(chip) < 0)
4621da177e4SLinus Torvalds 		return;
4631da177e4SLinus Torvalds 	data = ((unsigned int)val << ATI_REG_PHYS_OUT_DATA_SHIFT) |
4641da177e4SLinus Torvalds 		((unsigned int)reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
4651da177e4SLinus Torvalds 		ATI_REG_PHYS_OUT_ADDR_EN | codec;
4661da177e4SLinus Torvalds 	atiixp_write(chip, PHYS_OUT_ADDR, data);
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 
snd_atiixp_ac97_read(struct snd_ac97 * ac97,unsigned short reg)47074ee4ff1STakashi Iwai static unsigned short snd_atiixp_ac97_read(struct snd_ac97 *ac97,
47174ee4ff1STakashi Iwai 					   unsigned short reg)
4721da177e4SLinus Torvalds {
47374ee4ff1STakashi Iwai 	struct atiixp *chip = ac97->private_data;
4741da177e4SLinus Torvalds 	return snd_atiixp_codec_read(chip, ac97->num, reg);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
snd_atiixp_ac97_write(struct snd_ac97 * ac97,unsigned short reg,unsigned short val)47874ee4ff1STakashi Iwai static void snd_atiixp_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
47974ee4ff1STakashi Iwai 				  unsigned short val)
4801da177e4SLinus Torvalds {
48174ee4ff1STakashi Iwai 	struct atiixp *chip = ac97->private_data;
4821da177e4SLinus Torvalds 	snd_atiixp_codec_write(chip, ac97->num, reg, val);
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /*
4861da177e4SLinus Torvalds  * reset AC link
4871da177e4SLinus Torvalds  */
snd_atiixp_aclink_reset(struct atiixp * chip)48874ee4ff1STakashi Iwai static int snd_atiixp_aclink_reset(struct atiixp *chip)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	int timeout;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	/* reset powerdoewn */
4931da177e4SLinus Torvalds 	if (atiixp_update(chip, CMD, ATI_REG_CMD_POWERDOWN, 0))
4941da177e4SLinus Torvalds 		udelay(10);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	/* perform a software reset */
4971da177e4SLinus Torvalds 	atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, ATI_REG_CMD_AC_SOFT_RESET);
4981da177e4SLinus Torvalds 	atiixp_read(chip, CMD);
4991da177e4SLinus Torvalds 	udelay(10);
5001da177e4SLinus Torvalds 	atiixp_update(chip, CMD, ATI_REG_CMD_AC_SOFT_RESET, 0);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	timeout = 10;
5031da177e4SLinus Torvalds 	while (! (atiixp_read(chip, CMD) & ATI_REG_CMD_ACLINK_ACTIVE)) {
5041da177e4SLinus Torvalds 		/* do a hard reset */
5051da177e4SLinus Torvalds 		atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
5061da177e4SLinus Torvalds 			      ATI_REG_CMD_AC_SYNC);
5071da177e4SLinus Torvalds 		atiixp_read(chip, CMD);
50874ee4ff1STakashi Iwai 		mdelay(1);
5091da177e4SLinus Torvalds 		atiixp_update(chip, CMD, ATI_REG_CMD_AC_RESET, ATI_REG_CMD_AC_RESET);
5107e79443cSTakashi Iwai 		if (!--timeout) {
51125135fdcSTakashi Iwai 			dev_err(chip->card->dev, "codec reset timeout\n");
5121da177e4SLinus Torvalds 			break;
5131da177e4SLinus Torvalds 		}
5141da177e4SLinus Torvalds 	}
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	/* deassert RESET and assert SYNC to make sure */
5171da177e4SLinus Torvalds 	atiixp_update(chip, CMD, ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET,
5181da177e4SLinus Torvalds 		      ATI_REG_CMD_AC_SYNC|ATI_REG_CMD_AC_RESET);
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	return 0;
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
snd_atiixp_aclink_down(struct atiixp * chip)52374ee4ff1STakashi Iwai static int snd_atiixp_aclink_down(struct atiixp *chip)
5241da177e4SLinus Torvalds {
5251da177e4SLinus Torvalds 	// if (atiixp_read(chip, MODEM_MIRROR) & 0x1) /* modem running, too? */
5261da177e4SLinus Torvalds 	//	return -EBUSY;
5271da177e4SLinus Torvalds 	atiixp_update(chip, CMD,
5281da177e4SLinus Torvalds 		     ATI_REG_CMD_POWERDOWN | ATI_REG_CMD_AC_RESET,
5291da177e4SLinus Torvalds 		     ATI_REG_CMD_POWERDOWN);
5301da177e4SLinus Torvalds 	return 0;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds /*
5341da177e4SLinus Torvalds  * auto-detection of codecs
5351da177e4SLinus Torvalds  *
5361da177e4SLinus Torvalds  * the IXP chip can generate interrupts for the non-existing codecs.
5371da177e4SLinus Torvalds  * NEW_FRAME interrupt is used to make sure that the interrupt is generated
5381da177e4SLinus Torvalds  * even if all three codecs are connected.
5391da177e4SLinus Torvalds  */
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds #define ALL_CODEC_NOT_READY \
5421da177e4SLinus Torvalds 	    (ATI_REG_ISR_CODEC0_NOT_READY |\
5431da177e4SLinus Torvalds 	     ATI_REG_ISR_CODEC1_NOT_READY |\
5441da177e4SLinus Torvalds 	     ATI_REG_ISR_CODEC2_NOT_READY)
5451da177e4SLinus Torvalds #define CODEC_CHECK_BITS (ALL_CODEC_NOT_READY|ATI_REG_ISR_NEW_FRAME)
5461da177e4SLinus Torvalds 
ac97_probing_bugs(struct pci_dev * pci)547e23e7a14SBill Pemberton static int ac97_probing_bugs(struct pci_dev *pci)
54814e1d357SDan Carpenter {
549f41bea84STakashi Iwai 	const struct snd_pci_quirk *q;
55014e1d357SDan Carpenter 
551f41bea84STakashi Iwai 	q = snd_pci_quirk_lookup(pci, atiixp_quirks);
552f41bea84STakashi Iwai 	if (q) {
55325135fdcSTakashi Iwai 		dev_dbg(&pci->dev, "atiixp quirk for %s.  Forcing codec %d\n",
55486b27237STakashi Iwai 			snd_pci_quirk_name(q), q->value);
555f41bea84STakashi Iwai 		return q->value;
55614e1d357SDan Carpenter 	}
55714e1d357SDan Carpenter 	/* this hardware doesn't need workarounds.  Probe for codec */
55814e1d357SDan Carpenter 	return -1;
55914e1d357SDan Carpenter }
56014e1d357SDan Carpenter 
snd_atiixp_codec_detect(struct atiixp * chip)561e23e7a14SBill Pemberton static int snd_atiixp_codec_detect(struct atiixp *chip)
5621da177e4SLinus Torvalds {
5631da177e4SLinus Torvalds 	int timeout;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	chip->codec_not_ready_bits = 0;
56614e1d357SDan Carpenter 	if (ac97_codec == -1)
56714e1d357SDan Carpenter 		ac97_codec = ac97_probing_bugs(chip->pci);
56814e1d357SDan Carpenter 	if (ac97_codec >= 0) {
56914e1d357SDan Carpenter 		chip->codec_not_ready_bits |=
57014e1d357SDan Carpenter 			CODEC_CHECK_BITS ^ (1 << (ac97_codec + 10));
57114e1d357SDan Carpenter 		return 0;
57214e1d357SDan Carpenter 	}
57314e1d357SDan Carpenter 
5741da177e4SLinus Torvalds 	atiixp_write(chip, IER, CODEC_CHECK_BITS);
5751da177e4SLinus Torvalds 	/* wait for the interrupts */
576bfdcbaceSTakashi Iwai 	timeout = 50;
5771da177e4SLinus Torvalds 	while (timeout-- > 0) {
57874ee4ff1STakashi Iwai 		mdelay(1);
5791da177e4SLinus Torvalds 		if (chip->codec_not_ready_bits)
5801da177e4SLinus Torvalds 			break;
5811da177e4SLinus Torvalds 	}
5821da177e4SLinus Torvalds 	atiixp_write(chip, IER, 0); /* disable irqs */
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	if ((chip->codec_not_ready_bits & ALL_CODEC_NOT_READY) == ALL_CODEC_NOT_READY) {
58525135fdcSTakashi Iwai 		dev_err(chip->card->dev, "no codec detected!\n");
5861da177e4SLinus Torvalds 		return -ENXIO;
5871da177e4SLinus Torvalds 	}
5881da177e4SLinus Torvalds 	return 0;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds /*
5931da177e4SLinus Torvalds  * enable DMA and irqs
5941da177e4SLinus Torvalds  */
snd_atiixp_chip_start(struct atiixp * chip)59574ee4ff1STakashi Iwai static int snd_atiixp_chip_start(struct atiixp *chip)
5961da177e4SLinus Torvalds {
5971da177e4SLinus Torvalds 	unsigned int reg;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	/* set up spdif, enable burst mode */
6001da177e4SLinus Torvalds 	reg = atiixp_read(chip, CMD);
6011da177e4SLinus Torvalds 	reg |= 0x02 << ATI_REG_CMD_SPDF_THRESHOLD_SHIFT;
6021da177e4SLinus Torvalds 	reg |= ATI_REG_CMD_BURST_EN;
6031da177e4SLinus Torvalds 	atiixp_write(chip, CMD, reg);
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	reg = atiixp_read(chip, SPDF_CMD);
6061da177e4SLinus Torvalds 	reg &= ~(ATI_REG_SPDF_CMD_LFSR|ATI_REG_SPDF_CMD_SINGLE_CH);
6071da177e4SLinus Torvalds 	atiixp_write(chip, SPDF_CMD, reg);
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	/* clear all interrupt source */
6101da177e4SLinus Torvalds 	atiixp_write(chip, ISR, 0xffffffff);
6111da177e4SLinus Torvalds 	/* enable irqs */
6121da177e4SLinus Torvalds 	atiixp_write(chip, IER,
6131da177e4SLinus Torvalds 		     ATI_REG_IER_IO_STATUS_EN |
6141da177e4SLinus Torvalds 		     ATI_REG_IER_IN_XRUN_EN |
6151da177e4SLinus Torvalds 		     ATI_REG_IER_OUT_XRUN_EN |
6161da177e4SLinus Torvalds 		     ATI_REG_IER_SPDF_XRUN_EN |
6171da177e4SLinus Torvalds 		     ATI_REG_IER_SPDF_STATUS_EN);
6181da177e4SLinus Torvalds 	return 0;
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds /*
6231da177e4SLinus Torvalds  * disable DMA and IRQs
6241da177e4SLinus Torvalds  */
snd_atiixp_chip_stop(struct atiixp * chip)62574ee4ff1STakashi Iwai static int snd_atiixp_chip_stop(struct atiixp *chip)
6261da177e4SLinus Torvalds {
6271da177e4SLinus Torvalds 	/* clear interrupt source */
6281da177e4SLinus Torvalds 	atiixp_write(chip, ISR, atiixp_read(chip, ISR));
6291da177e4SLinus Torvalds 	/* disable irqs */
6301da177e4SLinus Torvalds 	atiixp_write(chip, IER, 0);
6311da177e4SLinus Torvalds 	return 0;
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds /*
6361da177e4SLinus Torvalds  * PCM section
6371da177e4SLinus Torvalds  */
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds /*
6401da177e4SLinus Torvalds  * pointer callback simplly reads XXX_DMA_DT_CUR register as the current
6411da177e4SLinus Torvalds  * position.  when SG-buffer is implemented, the offset must be calculated
6421da177e4SLinus Torvalds  * correctly...
6431da177e4SLinus Torvalds  */
snd_atiixp_pcm_pointer(struct snd_pcm_substream * substream)64474ee4ff1STakashi Iwai static snd_pcm_uframes_t snd_atiixp_pcm_pointer(struct snd_pcm_substream *substream)
6451da177e4SLinus Torvalds {
64674ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
64774ee4ff1STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
64874ee4ff1STakashi Iwai 	struct atiixp_dma *dma = runtime->private_data;
6491da177e4SLinus Torvalds 	unsigned int curptr;
6501da177e4SLinus Torvalds 	int timeout = 1000;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	while (timeout--) {
6531da177e4SLinus Torvalds 		curptr = readl(chip->remap_addr + dma->ops->dt_cur);
6541da177e4SLinus Torvalds 		if (curptr < dma->buf_addr)
6551da177e4SLinus Torvalds 			continue;
6561da177e4SLinus Torvalds 		curptr -= dma->buf_addr;
6571da177e4SLinus Torvalds 		if (curptr >= dma->buf_bytes)
6581da177e4SLinus Torvalds 			continue;
6591da177e4SLinus Torvalds 		return bytes_to_frames(runtime, curptr);
6601da177e4SLinus Torvalds 	}
66125135fdcSTakashi Iwai 	dev_dbg(chip->card->dev, "invalid DMA pointer read 0x%x (buf=%x)\n",
6621da177e4SLinus Torvalds 		   readl(chip->remap_addr + dma->ops->dt_cur), dma->buf_addr);
6631da177e4SLinus Torvalds 	return 0;
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds /*
6671da177e4SLinus Torvalds  * XRUN detected, and stop the PCM substream
6681da177e4SLinus Torvalds  */
snd_atiixp_xrun_dma(struct atiixp * chip,struct atiixp_dma * dma)66974ee4ff1STakashi Iwai static void snd_atiixp_xrun_dma(struct atiixp *chip, struct atiixp_dma *dma)
6701da177e4SLinus Torvalds {
6711da177e4SLinus Torvalds 	if (! dma->substream || ! dma->running)
6721da177e4SLinus Torvalds 		return;
67325135fdcSTakashi Iwai 	dev_dbg(chip->card->dev, "XRUN detected (DMA %d)\n", dma->ops->type);
6741fb8510cSTakashi Iwai 	snd_pcm_stop_xrun(dma->substream);
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds /*
6781da177e4SLinus Torvalds  * the period ack.  update the substream.
6791da177e4SLinus Torvalds  */
snd_atiixp_update_dma(struct atiixp * chip,struct atiixp_dma * dma)68074ee4ff1STakashi Iwai static void snd_atiixp_update_dma(struct atiixp *chip, struct atiixp_dma *dma)
6811da177e4SLinus Torvalds {
6821da177e4SLinus Torvalds 	if (! dma->substream || ! dma->running)
6831da177e4SLinus Torvalds 		return;
6841da177e4SLinus Torvalds 	snd_pcm_period_elapsed(dma->substream);
6851da177e4SLinus Torvalds }
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds /* set BUS_BUSY interrupt bit if any DMA is running */
6881da177e4SLinus Torvalds /* call with spinlock held */
snd_atiixp_check_bus_busy(struct atiixp * chip)68974ee4ff1STakashi Iwai static void snd_atiixp_check_bus_busy(struct atiixp *chip)
6901da177e4SLinus Torvalds {
6911da177e4SLinus Torvalds 	unsigned int bus_busy;
6921da177e4SLinus Torvalds 	if (atiixp_read(chip, CMD) & (ATI_REG_CMD_SEND_EN |
6931da177e4SLinus Torvalds 				      ATI_REG_CMD_RECEIVE_EN |
6941da177e4SLinus Torvalds 				      ATI_REG_CMD_SPDF_OUT_EN))
6951da177e4SLinus Torvalds 		bus_busy = ATI_REG_IER_SET_BUS_BUSY;
6961da177e4SLinus Torvalds 	else
6971da177e4SLinus Torvalds 		bus_busy = 0;
6981da177e4SLinus Torvalds 	atiixp_update(chip, IER, ATI_REG_IER_SET_BUS_BUSY, bus_busy);
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds /* common trigger callback
7021da177e4SLinus Torvalds  * calling the lowlevel callbacks in it
7031da177e4SLinus Torvalds  */
snd_atiixp_pcm_trigger(struct snd_pcm_substream * substream,int cmd)70474ee4ff1STakashi Iwai static int snd_atiixp_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
7051da177e4SLinus Torvalds {
70674ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
70774ee4ff1STakashi Iwai 	struct atiixp_dma *dma = substream->runtime->private_data;
7081da177e4SLinus Torvalds 	int err = 0;
7091da177e4SLinus Torvalds 
710da3cec35STakashi Iwai 	if (snd_BUG_ON(!dma->ops->enable_transfer ||
711da3cec35STakashi Iwai 		       !dma->ops->flush_dma))
712da3cec35STakashi Iwai 		return -EINVAL;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
7151da177e4SLinus Torvalds 	switch (cmd) {
7161da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
71741e4845cSJaroslav Kysela 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
71841e4845cSJaroslav Kysela 	case SNDRV_PCM_TRIGGER_RESUME:
719435e25c6STakashi Iwai 		if (dma->running && dma->suspended &&
720435e25c6STakashi Iwai 		    cmd == SNDRV_PCM_TRIGGER_RESUME)
721435e25c6STakashi Iwai 			writel(dma->saved_curptr, chip->remap_addr +
722435e25c6STakashi Iwai 			       dma->ops->dt_cur);
7231da177e4SLinus Torvalds 		dma->ops->enable_transfer(chip, 1);
7241da177e4SLinus Torvalds 		dma->running = 1;
72541e4845cSJaroslav Kysela 		dma->suspended = 0;
7261da177e4SLinus Torvalds 		break;
7271da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
72841e4845cSJaroslav Kysela 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
72941e4845cSJaroslav Kysela 	case SNDRV_PCM_TRIGGER_SUSPEND:
730435e25c6STakashi Iwai 		dma->suspended = cmd == SNDRV_PCM_TRIGGER_SUSPEND;
731435e25c6STakashi Iwai 		if (dma->running && dma->suspended)
732435e25c6STakashi Iwai 			dma->saved_curptr = readl(chip->remap_addr +
733435e25c6STakashi Iwai 						  dma->ops->dt_cur);
7341da177e4SLinus Torvalds 		dma->ops->enable_transfer(chip, 0);
7351da177e4SLinus Torvalds 		dma->running = 0;
7361da177e4SLinus Torvalds 		break;
7371da177e4SLinus Torvalds 	default:
7381da177e4SLinus Torvalds 		err = -EINVAL;
7391da177e4SLinus Torvalds 		break;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds 	if (! err) {
7421da177e4SLinus Torvalds 		snd_atiixp_check_bus_busy(chip);
7431da177e4SLinus Torvalds 		if (cmd == SNDRV_PCM_TRIGGER_STOP) {
7441da177e4SLinus Torvalds 			dma->ops->flush_dma(chip);
7451da177e4SLinus Torvalds 			snd_atiixp_check_bus_busy(chip);
7461da177e4SLinus Torvalds 		}
7471da177e4SLinus Torvalds 	}
7481da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
7491da177e4SLinus Torvalds 	return err;
7501da177e4SLinus Torvalds }
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds /*
7541da177e4SLinus Torvalds  * lowlevel callbacks for each DMA type
7551da177e4SLinus Torvalds  *
7561da177e4SLinus Torvalds  * every callback is supposed to be called in chip->reg_lock spinlock
7571da177e4SLinus Torvalds  */
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds /* flush FIFO of analog OUT DMA */
atiixp_out_flush_dma(struct atiixp * chip)76074ee4ff1STakashi Iwai static void atiixp_out_flush_dma(struct atiixp *chip)
7611da177e4SLinus Torvalds {
7621da177e4SLinus Torvalds 	atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_OUT_FLUSH);
7631da177e4SLinus Torvalds }
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds /* enable/disable analog OUT DMA */
atiixp_out_enable_dma(struct atiixp * chip,int on)76674ee4ff1STakashi Iwai static void atiixp_out_enable_dma(struct atiixp *chip, int on)
7671da177e4SLinus Torvalds {
7681da177e4SLinus Torvalds 	unsigned int data;
7691da177e4SLinus Torvalds 	data = atiixp_read(chip, CMD);
7701da177e4SLinus Torvalds 	if (on) {
7711da177e4SLinus Torvalds 		if (data & ATI_REG_CMD_OUT_DMA_EN)
7721da177e4SLinus Torvalds 			return;
7731da177e4SLinus Torvalds 		atiixp_out_flush_dma(chip);
7741da177e4SLinus Torvalds 		data |= ATI_REG_CMD_OUT_DMA_EN;
7751da177e4SLinus Torvalds 	} else
7761da177e4SLinus Torvalds 		data &= ~ATI_REG_CMD_OUT_DMA_EN;
7771da177e4SLinus Torvalds 	atiixp_write(chip, CMD, data);
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds /* start/stop transfer over OUT DMA */
atiixp_out_enable_transfer(struct atiixp * chip,int on)78174ee4ff1STakashi Iwai static void atiixp_out_enable_transfer(struct atiixp *chip, int on)
7821da177e4SLinus Torvalds {
7831da177e4SLinus Torvalds 	atiixp_update(chip, CMD, ATI_REG_CMD_SEND_EN,
7841da177e4SLinus Torvalds 		      on ? ATI_REG_CMD_SEND_EN : 0);
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds /* enable/disable analog IN DMA */
atiixp_in_enable_dma(struct atiixp * chip,int on)78874ee4ff1STakashi Iwai static void atiixp_in_enable_dma(struct atiixp *chip, int on)
7891da177e4SLinus Torvalds {
7901da177e4SLinus Torvalds 	atiixp_update(chip, CMD, ATI_REG_CMD_IN_DMA_EN,
7911da177e4SLinus Torvalds 		      on ? ATI_REG_CMD_IN_DMA_EN : 0);
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds /* start/stop analog IN DMA */
atiixp_in_enable_transfer(struct atiixp * chip,int on)79574ee4ff1STakashi Iwai static void atiixp_in_enable_transfer(struct atiixp *chip, int on)
7961da177e4SLinus Torvalds {
7971da177e4SLinus Torvalds 	if (on) {
7981da177e4SLinus Torvalds 		unsigned int data = atiixp_read(chip, CMD);
7991da177e4SLinus Torvalds 		if (! (data & ATI_REG_CMD_RECEIVE_EN)) {
8001da177e4SLinus Torvalds 			data |= ATI_REG_CMD_RECEIVE_EN;
8011da177e4SLinus Torvalds #if 0 /* FIXME: this causes the endless loop */
8021da177e4SLinus Torvalds 			/* wait until slot 3/4 are finished */
8031da177e4SLinus Torvalds 			while ((atiixp_read(chip, COUNTER) &
8041da177e4SLinus Torvalds 				ATI_REG_COUNTER_SLOT) != 5)
8051da177e4SLinus Torvalds 				;
8061da177e4SLinus Torvalds #endif
8071da177e4SLinus Torvalds 			atiixp_write(chip, CMD, data);
8081da177e4SLinus Torvalds 		}
8091da177e4SLinus Torvalds 	} else
8101da177e4SLinus Torvalds 		atiixp_update(chip, CMD, ATI_REG_CMD_RECEIVE_EN, 0);
8111da177e4SLinus Torvalds }
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds /* flush FIFO of analog IN DMA */
atiixp_in_flush_dma(struct atiixp * chip)81474ee4ff1STakashi Iwai static void atiixp_in_flush_dma(struct atiixp *chip)
8151da177e4SLinus Torvalds {
8161da177e4SLinus Torvalds 	atiixp_write(chip, FIFO_FLUSH, ATI_REG_FIFO_IN_FLUSH);
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds /* enable/disable SPDIF OUT DMA */
atiixp_spdif_enable_dma(struct atiixp * chip,int on)82074ee4ff1STakashi Iwai static void atiixp_spdif_enable_dma(struct atiixp *chip, int on)
8211da177e4SLinus Torvalds {
8221da177e4SLinus Torvalds 	atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_DMA_EN,
8231da177e4SLinus Torvalds 		      on ? ATI_REG_CMD_SPDF_DMA_EN : 0);
8241da177e4SLinus Torvalds }
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds /* start/stop SPDIF OUT DMA */
atiixp_spdif_enable_transfer(struct atiixp * chip,int on)82774ee4ff1STakashi Iwai static void atiixp_spdif_enable_transfer(struct atiixp *chip, int on)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	unsigned int data;
8301da177e4SLinus Torvalds 	data = atiixp_read(chip, CMD);
8311da177e4SLinus Torvalds 	if (on)
8321da177e4SLinus Torvalds 		data |= ATI_REG_CMD_SPDF_OUT_EN;
8331da177e4SLinus Torvalds 	else
8341da177e4SLinus Torvalds 		data &= ~ATI_REG_CMD_SPDF_OUT_EN;
8351da177e4SLinus Torvalds 	atiixp_write(chip, CMD, data);
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds /* flush FIFO of SPDIF OUT DMA */
atiixp_spdif_flush_dma(struct atiixp * chip)83974ee4ff1STakashi Iwai static void atiixp_spdif_flush_dma(struct atiixp *chip)
8401da177e4SLinus Torvalds {
8411da177e4SLinus Torvalds 	int timeout;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	/* DMA off, transfer on */
8441da177e4SLinus Torvalds 	atiixp_spdif_enable_dma(chip, 0);
8451da177e4SLinus Torvalds 	atiixp_spdif_enable_transfer(chip, 1);
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	timeout = 100;
8481da177e4SLinus Torvalds 	do {
8491da177e4SLinus Torvalds 		if (! (atiixp_read(chip, SPDF_DMA_DT_SIZE) & ATI_REG_DMA_FIFO_USED))
8501da177e4SLinus Torvalds 			break;
8511da177e4SLinus Torvalds 		udelay(1);
8521da177e4SLinus Torvalds 	} while (timeout-- > 0);
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	atiixp_spdif_enable_transfer(chip, 0);
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds /* set up slots and formats for SPDIF OUT */
snd_atiixp_spdif_prepare(struct snd_pcm_substream * substream)85874ee4ff1STakashi Iwai static int snd_atiixp_spdif_prepare(struct snd_pcm_substream *substream)
8591da177e4SLinus Torvalds {
86074ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
8631da177e4SLinus Torvalds 	if (chip->spdif_over_aclink) {
8641da177e4SLinus Torvalds 		unsigned int data;
8651da177e4SLinus Torvalds 		/* enable slots 10/11 */
8661da177e4SLinus Torvalds 		atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK,
8671da177e4SLinus Torvalds 			      ATI_REG_CMD_SPDF_CONFIG_01);
8681da177e4SLinus Torvalds 		data = atiixp_read(chip, OUT_DMA_SLOT) & ~ATI_REG_OUT_DMA_SLOT_MASK;
8691da177e4SLinus Torvalds 		data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
8701da177e4SLinus Torvalds 			ATI_REG_OUT_DMA_SLOT_BIT(11);
8711da177e4SLinus Torvalds 		data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
8721da177e4SLinus Torvalds 		atiixp_write(chip, OUT_DMA_SLOT, data);
8731da177e4SLinus Torvalds 		atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_OUT,
8741da177e4SLinus Torvalds 			      substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
8751da177e4SLinus Torvalds 			      ATI_REG_CMD_INTERLEAVE_OUT : 0);
8761da177e4SLinus Torvalds 	} else {
8771da177e4SLinus Torvalds 		atiixp_update(chip, CMD, ATI_REG_CMD_SPDF_CONFIG_MASK, 0);
8781da177e4SLinus Torvalds 		atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_SPDF, 0);
8791da177e4SLinus Torvalds 	}
8801da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
8811da177e4SLinus Torvalds 	return 0;
8821da177e4SLinus Torvalds }
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds /* set up slots and formats for analog OUT */
snd_atiixp_playback_prepare(struct snd_pcm_substream * substream)88574ee4ff1STakashi Iwai static int snd_atiixp_playback_prepare(struct snd_pcm_substream *substream)
8861da177e4SLinus Torvalds {
88774ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
8881da177e4SLinus Torvalds 	unsigned int data;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
8911da177e4SLinus Torvalds 	data = atiixp_read(chip, OUT_DMA_SLOT) & ~ATI_REG_OUT_DMA_SLOT_MASK;
8921da177e4SLinus Torvalds 	switch (substream->runtime->channels) {
8931da177e4SLinus Torvalds 	case 8:
8941da177e4SLinus Torvalds 		data |= ATI_REG_OUT_DMA_SLOT_BIT(10) |
8951da177e4SLinus Torvalds 			ATI_REG_OUT_DMA_SLOT_BIT(11);
896c0dbbdadSGustavo A. R. Silva 		fallthrough;
8971da177e4SLinus Torvalds 	case 6:
8981da177e4SLinus Torvalds 		data |= ATI_REG_OUT_DMA_SLOT_BIT(7) |
8991da177e4SLinus Torvalds 			ATI_REG_OUT_DMA_SLOT_BIT(8);
900c0dbbdadSGustavo A. R. Silva 		fallthrough;
9011da177e4SLinus Torvalds 	case 4:
9021da177e4SLinus Torvalds 		data |= ATI_REG_OUT_DMA_SLOT_BIT(6) |
9031da177e4SLinus Torvalds 			ATI_REG_OUT_DMA_SLOT_BIT(9);
904c0dbbdadSGustavo A. R. Silva 		fallthrough;
9051da177e4SLinus Torvalds 	default:
9061da177e4SLinus Torvalds 		data |= ATI_REG_OUT_DMA_SLOT_BIT(3) |
9071da177e4SLinus Torvalds 			ATI_REG_OUT_DMA_SLOT_BIT(4);
9081da177e4SLinus Torvalds 		break;
9091da177e4SLinus Torvalds 	}
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	/* set output threshold */
9121da177e4SLinus Torvalds 	data |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
9131da177e4SLinus Torvalds 	atiixp_write(chip, OUT_DMA_SLOT, data);
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_OUT,
9161da177e4SLinus Torvalds 		      substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
9171da177e4SLinus Torvalds 		      ATI_REG_CMD_INTERLEAVE_OUT : 0);
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	/*
9201da177e4SLinus Torvalds 	 * enable 6 channel re-ordering bit if needed
9211da177e4SLinus Torvalds 	 */
9221da177e4SLinus Torvalds 	atiixp_update(chip, 6CH_REORDER, ATI_REG_6CH_REORDER_EN,
9231da177e4SLinus Torvalds 		      substream->runtime->channels >= 6 ? ATI_REG_6CH_REORDER_EN: 0);
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9261da177e4SLinus Torvalds 	return 0;
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds /* set up slots and formats for analog IN */
snd_atiixp_capture_prepare(struct snd_pcm_substream * substream)93074ee4ff1STakashi Iwai static int snd_atiixp_capture_prepare(struct snd_pcm_substream *substream)
9311da177e4SLinus Torvalds {
93274ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9351da177e4SLinus Torvalds 	atiixp_update(chip, CMD, ATI_REG_CMD_INTERLEAVE_IN,
9361da177e4SLinus Torvalds 		      substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE ?
9371da177e4SLinus Torvalds 		      ATI_REG_CMD_INTERLEAVE_IN : 0);
9381da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9391da177e4SLinus Torvalds 	return 0;
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds /*
9431da177e4SLinus Torvalds  * hw_params - allocate the buffer and set up buffer descriptors
9441da177e4SLinus Torvalds  */
snd_atiixp_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)94574ee4ff1STakashi Iwai static int snd_atiixp_pcm_hw_params(struct snd_pcm_substream *substream,
94674ee4ff1STakashi Iwai 				    struct snd_pcm_hw_params *hw_params)
9471da177e4SLinus Torvalds {
94874ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
94974ee4ff1STakashi Iwai 	struct atiixp_dma *dma = substream->runtime->private_data;
9501da177e4SLinus Torvalds 	int err;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	dma->buf_addr = substream->runtime->dma_addr;
9531da177e4SLinus Torvalds 	dma->buf_bytes = params_buffer_bytes(hw_params);
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	err = atiixp_build_dma_packets(chip, dma, substream,
9561da177e4SLinus Torvalds 				       params_periods(hw_params),
9571da177e4SLinus Torvalds 				       params_period_bytes(hw_params));
9581da177e4SLinus Torvalds 	if (err < 0)
9591da177e4SLinus Torvalds 		return err;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	if (dma->ac97_pcm_type >= 0) {
9621da177e4SLinus Torvalds 		struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
9631da177e4SLinus Torvalds 		/* PCM is bound to AC97 codec(s)
9641da177e4SLinus Torvalds 		 * set up the AC97 codecs
9651da177e4SLinus Torvalds 		 */
9661da177e4SLinus Torvalds 		if (dma->pcm_open_flag) {
9671da177e4SLinus Torvalds 			snd_ac97_pcm_close(pcm);
9681da177e4SLinus Torvalds 			dma->pcm_open_flag = 0;
9691da177e4SLinus Torvalds 		}
9701da177e4SLinus Torvalds 		err = snd_ac97_pcm_open(pcm, params_rate(hw_params),
9711da177e4SLinus Torvalds 					params_channels(hw_params),
9721da177e4SLinus Torvalds 					pcm->r[0].slots);
9731da177e4SLinus Torvalds 		if (err >= 0)
9741da177e4SLinus Torvalds 			dma->pcm_open_flag = 1;
9751da177e4SLinus Torvalds 	}
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	return err;
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
snd_atiixp_pcm_hw_free(struct snd_pcm_substream * substream)98074ee4ff1STakashi Iwai static int snd_atiixp_pcm_hw_free(struct snd_pcm_substream *substream)
9811da177e4SLinus Torvalds {
98274ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
98374ee4ff1STakashi Iwai 	struct atiixp_dma *dma = substream->runtime->private_data;
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	if (dma->pcm_open_flag) {
9861da177e4SLinus Torvalds 		struct ac97_pcm *pcm = chip->pcms[dma->ac97_pcm_type];
9871da177e4SLinus Torvalds 		snd_ac97_pcm_close(pcm);
9881da177e4SLinus Torvalds 		dma->pcm_open_flag = 0;
9891da177e4SLinus Torvalds 	}
9901da177e4SLinus Torvalds 	atiixp_clear_dma_packets(chip, dma, substream);
9911da177e4SLinus Torvalds 	return 0;
9921da177e4SLinus Torvalds }
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds /*
9961da177e4SLinus Torvalds  * pcm hardware definition, identical for all DMA types
9971da177e4SLinus Torvalds  */
998dee49895SBhumika Goyal static const struct snd_pcm_hardware snd_atiixp_pcm_hw =
9991da177e4SLinus Torvalds {
10001da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
10011da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
100241e4845cSJaroslav Kysela 				 SNDRV_PCM_INFO_PAUSE |
10031da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_RESUME |
10041da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
10051da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
10061da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_48000,
10071da177e4SLinus Torvalds 	.rate_min =		48000,
10081da177e4SLinus Torvalds 	.rate_max =		48000,
10091da177e4SLinus Torvalds 	.channels_min =		2,
10101da177e4SLinus Torvalds 	.channels_max =		2,
10111da177e4SLinus Torvalds 	.buffer_bytes_max =	256 * 1024,
10121da177e4SLinus Torvalds 	.period_bytes_min =	32,
10131da177e4SLinus Torvalds 	.period_bytes_max =	128 * 1024,
10141da177e4SLinus Torvalds 	.periods_min =		2,
10151da177e4SLinus Torvalds 	.periods_max =		ATI_MAX_DESCRIPTORS,
10161da177e4SLinus Torvalds };
10171da177e4SLinus Torvalds 
snd_atiixp_pcm_open(struct snd_pcm_substream * substream,struct atiixp_dma * dma,int pcm_type)101874ee4ff1STakashi Iwai static int snd_atiixp_pcm_open(struct snd_pcm_substream *substream,
101974ee4ff1STakashi Iwai 			       struct atiixp_dma *dma, int pcm_type)
10201da177e4SLinus Torvalds {
102174ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
102274ee4ff1STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
10231da177e4SLinus Torvalds 	int err;
10241da177e4SLinus Torvalds 
1025da3cec35STakashi Iwai 	if (snd_BUG_ON(!dma->ops || !dma->ops->enable_dma))
1026da3cec35STakashi Iwai 		return -EINVAL;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	if (dma->opened)
10291da177e4SLinus Torvalds 		return -EBUSY;
10301da177e4SLinus Torvalds 	dma->substream = substream;
10311da177e4SLinus Torvalds 	runtime->hw = snd_atiixp_pcm_hw;
10321da177e4SLinus Torvalds 	dma->ac97_pcm_type = pcm_type;
10331da177e4SLinus Torvalds 	if (pcm_type >= 0) {
10341da177e4SLinus Torvalds 		runtime->hw.rates = chip->pcms[pcm_type]->rates;
10351da177e4SLinus Torvalds 		snd_pcm_limit_hw_rates(runtime);
10361da177e4SLinus Torvalds 	} else {
10371da177e4SLinus Torvalds 		/* direct SPDIF */
10381da177e4SLinus Torvalds 		runtime->hw.formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
10391da177e4SLinus Torvalds 	}
1040b83bea38STakashi Iwai 	err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
1041b83bea38STakashi Iwai 	if (err < 0)
10421da177e4SLinus Torvalds 		return err;
10431da177e4SLinus Torvalds 	runtime->private_data = dma;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	/* enable DMA bits */
10461da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10471da177e4SLinus Torvalds 	dma->ops->enable_dma(chip, 1);
10481da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10491da177e4SLinus Torvalds 	dma->opened = 1;
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	return 0;
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
snd_atiixp_pcm_close(struct snd_pcm_substream * substream,struct atiixp_dma * dma)105474ee4ff1STakashi Iwai static int snd_atiixp_pcm_close(struct snd_pcm_substream *substream,
105574ee4ff1STakashi Iwai 				struct atiixp_dma *dma)
10561da177e4SLinus Torvalds {
105774ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
10581da177e4SLinus Torvalds 	/* disable DMA bits */
1059da3cec35STakashi Iwai 	if (snd_BUG_ON(!dma->ops || !dma->ops->enable_dma))
1060da3cec35STakashi Iwai 		return -EINVAL;
10611da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10621da177e4SLinus Torvalds 	dma->ops->enable_dma(chip, 0);
10631da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10641da177e4SLinus Torvalds 	dma->substream = NULL;
10651da177e4SLinus Torvalds 	dma->opened = 0;
10661da177e4SLinus Torvalds 	return 0;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds /*
10701da177e4SLinus Torvalds  */
snd_atiixp_playback_open(struct snd_pcm_substream * substream)107174ee4ff1STakashi Iwai static int snd_atiixp_playback_open(struct snd_pcm_substream *substream)
10721da177e4SLinus Torvalds {
107374ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
10741da177e4SLinus Torvalds 	int err;
10751da177e4SLinus Torvalds 
107662932df8SIngo Molnar 	mutex_lock(&chip->open_mutex);
10771da177e4SLinus Torvalds 	err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 0);
107862932df8SIngo Molnar 	mutex_unlock(&chip->open_mutex);
10791da177e4SLinus Torvalds 	if (err < 0)
10801da177e4SLinus Torvalds 		return err;
10811da177e4SLinus Torvalds 	substream->runtime->hw.channels_max = chip->max_channels;
10821da177e4SLinus Torvalds 	if (chip->max_channels > 2)
10831da177e4SLinus Torvalds 		/* channels must be even */
10841da177e4SLinus Torvalds 		snd_pcm_hw_constraint_step(substream->runtime, 0,
10851da177e4SLinus Torvalds 					   SNDRV_PCM_HW_PARAM_CHANNELS, 2);
10861da177e4SLinus Torvalds 	return 0;
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
snd_atiixp_playback_close(struct snd_pcm_substream * substream)108974ee4ff1STakashi Iwai static int snd_atiixp_playback_close(struct snd_pcm_substream *substream)
10901da177e4SLinus Torvalds {
109174ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
10921da177e4SLinus Torvalds 	int err;
109362932df8SIngo Molnar 	mutex_lock(&chip->open_mutex);
10941da177e4SLinus Torvalds 	err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
109562932df8SIngo Molnar 	mutex_unlock(&chip->open_mutex);
10961da177e4SLinus Torvalds 	return err;
10971da177e4SLinus Torvalds }
10981da177e4SLinus Torvalds 
snd_atiixp_capture_open(struct snd_pcm_substream * substream)109974ee4ff1STakashi Iwai static int snd_atiixp_capture_open(struct snd_pcm_substream *substream)
11001da177e4SLinus Torvalds {
110174ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
11021da177e4SLinus Torvalds 	return snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_CAPTURE], 1);
11031da177e4SLinus Torvalds }
11041da177e4SLinus Torvalds 
snd_atiixp_capture_close(struct snd_pcm_substream * substream)110574ee4ff1STakashi Iwai static int snd_atiixp_capture_close(struct snd_pcm_substream *substream)
11061da177e4SLinus Torvalds {
110774ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
11081da177e4SLinus Torvalds 	return snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_CAPTURE]);
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
snd_atiixp_spdif_open(struct snd_pcm_substream * substream)111174ee4ff1STakashi Iwai static int snd_atiixp_spdif_open(struct snd_pcm_substream *substream)
11121da177e4SLinus Torvalds {
111374ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
11141da177e4SLinus Torvalds 	int err;
111562932df8SIngo Molnar 	mutex_lock(&chip->open_mutex);
11161da177e4SLinus Torvalds 	if (chip->spdif_over_aclink) /* share DMA_PLAYBACK */
11171da177e4SLinus Torvalds 		err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_PLAYBACK], 2);
11181da177e4SLinus Torvalds 	else
11191da177e4SLinus Torvalds 		err = snd_atiixp_pcm_open(substream, &chip->dmas[ATI_DMA_SPDIF], -1);
112062932df8SIngo Molnar 	mutex_unlock(&chip->open_mutex);
11211da177e4SLinus Torvalds 	return err;
11221da177e4SLinus Torvalds }
11231da177e4SLinus Torvalds 
snd_atiixp_spdif_close(struct snd_pcm_substream * substream)112474ee4ff1STakashi Iwai static int snd_atiixp_spdif_close(struct snd_pcm_substream *substream)
11251da177e4SLinus Torvalds {
112674ee4ff1STakashi Iwai 	struct atiixp *chip = snd_pcm_substream_chip(substream);
11271da177e4SLinus Torvalds 	int err;
112862932df8SIngo Molnar 	mutex_lock(&chip->open_mutex);
11291da177e4SLinus Torvalds 	if (chip->spdif_over_aclink)
11301da177e4SLinus Torvalds 		err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_PLAYBACK]);
11311da177e4SLinus Torvalds 	else
11321da177e4SLinus Torvalds 		err = snd_atiixp_pcm_close(substream, &chip->dmas[ATI_DMA_SPDIF]);
113362932df8SIngo Molnar 	mutex_unlock(&chip->open_mutex);
11341da177e4SLinus Torvalds 	return err;
11351da177e4SLinus Torvalds }
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds /* AC97 playback */
11386769e988SJulia Lawall static const struct snd_pcm_ops snd_atiixp_playback_ops = {
11391da177e4SLinus Torvalds 	.open =		snd_atiixp_playback_open,
11401da177e4SLinus Torvalds 	.close =	snd_atiixp_playback_close,
11411da177e4SLinus Torvalds 	.hw_params =	snd_atiixp_pcm_hw_params,
11421da177e4SLinus Torvalds 	.hw_free =	snd_atiixp_pcm_hw_free,
11431da177e4SLinus Torvalds 	.prepare =	snd_atiixp_playback_prepare,
11441da177e4SLinus Torvalds 	.trigger =	snd_atiixp_pcm_trigger,
11451da177e4SLinus Torvalds 	.pointer =	snd_atiixp_pcm_pointer,
11461da177e4SLinus Torvalds };
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds /* AC97 capture */
11496769e988SJulia Lawall static const struct snd_pcm_ops snd_atiixp_capture_ops = {
11501da177e4SLinus Torvalds 	.open =		snd_atiixp_capture_open,
11511da177e4SLinus Torvalds 	.close =	snd_atiixp_capture_close,
11521da177e4SLinus Torvalds 	.hw_params =	snd_atiixp_pcm_hw_params,
11531da177e4SLinus Torvalds 	.hw_free =	snd_atiixp_pcm_hw_free,
11541da177e4SLinus Torvalds 	.prepare =	snd_atiixp_capture_prepare,
11551da177e4SLinus Torvalds 	.trigger =	snd_atiixp_pcm_trigger,
11561da177e4SLinus Torvalds 	.pointer =	snd_atiixp_pcm_pointer,
11571da177e4SLinus Torvalds };
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds /* SPDIF playback */
11606769e988SJulia Lawall static const struct snd_pcm_ops snd_atiixp_spdif_ops = {
11611da177e4SLinus Torvalds 	.open =		snd_atiixp_spdif_open,
11621da177e4SLinus Torvalds 	.close =	snd_atiixp_spdif_close,
11631da177e4SLinus Torvalds 	.hw_params =	snd_atiixp_pcm_hw_params,
11641da177e4SLinus Torvalds 	.hw_free =	snd_atiixp_pcm_hw_free,
11651da177e4SLinus Torvalds 	.prepare =	snd_atiixp_spdif_prepare,
11661da177e4SLinus Torvalds 	.trigger =	snd_atiixp_pcm_trigger,
11671da177e4SLinus Torvalds 	.pointer =	snd_atiixp_pcm_pointer,
11681da177e4SLinus Torvalds };
11691da177e4SLinus Torvalds 
11709715f0bdSArvind Yadav static const struct ac97_pcm atiixp_pcm_defs[] = {
11711da177e4SLinus Torvalds 	/* front PCM */
11721da177e4SLinus Torvalds 	{
11731da177e4SLinus Torvalds 		.exclusive = 1,
11741da177e4SLinus Torvalds 		.r = {	{
11751da177e4SLinus Torvalds 				.slots = (1 << AC97_SLOT_PCM_LEFT) |
11761da177e4SLinus Torvalds 					 (1 << AC97_SLOT_PCM_RIGHT) |
11771da177e4SLinus Torvalds 					 (1 << AC97_SLOT_PCM_CENTER) |
11781da177e4SLinus Torvalds 					 (1 << AC97_SLOT_PCM_SLEFT) |
11791da177e4SLinus Torvalds 					 (1 << AC97_SLOT_PCM_SRIGHT) |
11801da177e4SLinus Torvalds 					 (1 << AC97_SLOT_LFE)
11811da177e4SLinus Torvalds 			}
11821da177e4SLinus Torvalds 		}
11831da177e4SLinus Torvalds 	},
11841da177e4SLinus Torvalds 	/* PCM IN #1 */
11851da177e4SLinus Torvalds 	{
11861da177e4SLinus Torvalds 		.stream = 1,
11871da177e4SLinus Torvalds 		.exclusive = 1,
11881da177e4SLinus Torvalds 		.r = {	{
11891da177e4SLinus Torvalds 				.slots = (1 << AC97_SLOT_PCM_LEFT) |
11901da177e4SLinus Torvalds 					 (1 << AC97_SLOT_PCM_RIGHT)
11911da177e4SLinus Torvalds 			}
11921da177e4SLinus Torvalds 		}
11931da177e4SLinus Torvalds 	},
11941da177e4SLinus Torvalds 	/* S/PDIF OUT (optional) */
11951da177e4SLinus Torvalds 	{
11961da177e4SLinus Torvalds 		.exclusive = 1,
11971da177e4SLinus Torvalds 		.spdif = 1,
11981da177e4SLinus Torvalds 		.r = {	{
11991da177e4SLinus Torvalds 				.slots = (1 << AC97_SLOT_SPDIF_LEFT2) |
12001da177e4SLinus Torvalds 					 (1 << AC97_SLOT_SPDIF_RIGHT2)
12011da177e4SLinus Torvalds 			}
12021da177e4SLinus Torvalds 		}
12031da177e4SLinus Torvalds 	},
12041da177e4SLinus Torvalds };
12051da177e4SLinus Torvalds 
1206064e186fSJulia Lawall static const struct atiixp_dma_ops snd_atiixp_playback_dma_ops = {
12071da177e4SLinus Torvalds 	.type = ATI_DMA_PLAYBACK,
12081da177e4SLinus Torvalds 	.llp_offset = ATI_REG_OUT_DMA_LINKPTR,
12091da177e4SLinus Torvalds 	.dt_cur = ATI_REG_OUT_DMA_DT_CUR,
12101da177e4SLinus Torvalds 	.enable_dma = atiixp_out_enable_dma,
12111da177e4SLinus Torvalds 	.enable_transfer = atiixp_out_enable_transfer,
12121da177e4SLinus Torvalds 	.flush_dma = atiixp_out_flush_dma,
12131da177e4SLinus Torvalds };
12141da177e4SLinus Torvalds 
1215064e186fSJulia Lawall static const struct atiixp_dma_ops snd_atiixp_capture_dma_ops = {
12161da177e4SLinus Torvalds 	.type = ATI_DMA_CAPTURE,
12171da177e4SLinus Torvalds 	.llp_offset = ATI_REG_IN_DMA_LINKPTR,
12181da177e4SLinus Torvalds 	.dt_cur = ATI_REG_IN_DMA_DT_CUR,
12191da177e4SLinus Torvalds 	.enable_dma = atiixp_in_enable_dma,
12201da177e4SLinus Torvalds 	.enable_transfer = atiixp_in_enable_transfer,
12211da177e4SLinus Torvalds 	.flush_dma = atiixp_in_flush_dma,
12221da177e4SLinus Torvalds };
12231da177e4SLinus Torvalds 
1224064e186fSJulia Lawall static const struct atiixp_dma_ops snd_atiixp_spdif_dma_ops = {
12251da177e4SLinus Torvalds 	.type = ATI_DMA_SPDIF,
12261da177e4SLinus Torvalds 	.llp_offset = ATI_REG_SPDF_DMA_LINKPTR,
12271da177e4SLinus Torvalds 	.dt_cur = ATI_REG_SPDF_DMA_DT_CUR,
12281da177e4SLinus Torvalds 	.enable_dma = atiixp_spdif_enable_dma,
12291da177e4SLinus Torvalds 	.enable_transfer = atiixp_spdif_enable_transfer,
12301da177e4SLinus Torvalds 	.flush_dma = atiixp_spdif_flush_dma,
12311da177e4SLinus Torvalds };
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 
snd_atiixp_pcm_new(struct atiixp * chip)1234e23e7a14SBill Pemberton static int snd_atiixp_pcm_new(struct atiixp *chip)
12351da177e4SLinus Torvalds {
123674ee4ff1STakashi Iwai 	struct snd_pcm *pcm;
1237e36e3b86STakashi Iwai 	struct snd_pcm_chmap *chmap;
123874ee4ff1STakashi Iwai 	struct snd_ac97_bus *pbus = chip->ac97_bus;
12391da177e4SLinus Torvalds 	int err, i, num_pcms;
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	/* initialize constants */
12421da177e4SLinus Torvalds 	chip->dmas[ATI_DMA_PLAYBACK].ops = &snd_atiixp_playback_dma_ops;
12431da177e4SLinus Torvalds 	chip->dmas[ATI_DMA_CAPTURE].ops = &snd_atiixp_capture_dma_ops;
12441da177e4SLinus Torvalds 	if (! chip->spdif_over_aclink)
12451da177e4SLinus Torvalds 		chip->dmas[ATI_DMA_SPDIF].ops = &snd_atiixp_spdif_dma_ops;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	/* assign AC97 pcm */
12481da177e4SLinus Torvalds 	if (chip->spdif_over_aclink)
12491da177e4SLinus Torvalds 		num_pcms = 3;
12501da177e4SLinus Torvalds 	else
12511da177e4SLinus Torvalds 		num_pcms = 2;
12521da177e4SLinus Torvalds 	err = snd_ac97_pcm_assign(pbus, num_pcms, atiixp_pcm_defs);
12531da177e4SLinus Torvalds 	if (err < 0)
12541da177e4SLinus Torvalds 		return err;
12551da177e4SLinus Torvalds 	for (i = 0; i < num_pcms; i++)
12561da177e4SLinus Torvalds 		chip->pcms[i] = &pbus->pcms[i];
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	chip->max_channels = 2;
12591da177e4SLinus Torvalds 	if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_PCM_SLEFT)) {
12601da177e4SLinus Torvalds 		if (pbus->pcms[ATI_PCM_OUT].r[0].slots & (1 << AC97_SLOT_LFE))
12611da177e4SLinus Torvalds 			chip->max_channels = 6;
12621da177e4SLinus Torvalds 		else
12631da177e4SLinus Torvalds 			chip->max_channels = 4;
12641da177e4SLinus Torvalds 	}
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	/* PCM #0: analog I/O */
126774ee4ff1STakashi Iwai 	err = snd_pcm_new(chip->card, "ATI IXP AC97",
126874ee4ff1STakashi Iwai 			  ATI_PCMDEV_ANALOG, 1, 1, &pcm);
12691da177e4SLinus Torvalds 	if (err < 0)
12701da177e4SLinus Torvalds 		return err;
12711da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_playback_ops);
12721da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_atiixp_capture_ops);
12731da177e4SLinus Torvalds 	pcm->private_data = chip;
12741da177e4SLinus Torvalds 	strcpy(pcm->name, "ATI IXP AC97");
12751da177e4SLinus Torvalds 	chip->pcmdevs[ATI_PCMDEV_ANALOG] = pcm;
12761da177e4SLinus Torvalds 
1277801bf057STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1278801bf057STakashi Iwai 				       &chip->pci->dev, 64*1024, 128*1024);
12791da177e4SLinus Torvalds 
1280e36e3b86STakashi Iwai 	err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1281e36e3b86STakashi Iwai 				     snd_pcm_alt_chmaps, chip->max_channels, 0,
1282e36e3b86STakashi Iwai 				     &chmap);
1283e36e3b86STakashi Iwai 	if (err < 0)
1284e36e3b86STakashi Iwai 		return err;
1285e36e3b86STakashi Iwai 	chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
1286e36e3b86STakashi Iwai 	chip->ac97[0]->chmaps[SNDRV_PCM_STREAM_PLAYBACK] = chmap;
1287e36e3b86STakashi Iwai 
12881da177e4SLinus Torvalds 	/* no SPDIF support on codec? */
12891da177e4SLinus Torvalds 	if (chip->pcms[ATI_PCM_SPDIF] && ! chip->pcms[ATI_PCM_SPDIF]->rates)
12901da177e4SLinus Torvalds 		return 0;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	/* FIXME: non-48k sample rate doesn't work on my test machine with AD1888 */
12931da177e4SLinus Torvalds 	if (chip->pcms[ATI_PCM_SPDIF])
12941da177e4SLinus Torvalds 		chip->pcms[ATI_PCM_SPDIF]->rates = SNDRV_PCM_RATE_48000;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	/* PCM #1: spdif playback */
129774ee4ff1STakashi Iwai 	err = snd_pcm_new(chip->card, "ATI IXP IEC958",
129874ee4ff1STakashi Iwai 			  ATI_PCMDEV_DIGITAL, 1, 0, &pcm);
12991da177e4SLinus Torvalds 	if (err < 0)
13001da177e4SLinus Torvalds 		return err;
13011da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_atiixp_spdif_ops);
13021da177e4SLinus Torvalds 	pcm->private_data = chip;
13031da177e4SLinus Torvalds 	if (chip->spdif_over_aclink)
13041da177e4SLinus Torvalds 		strcpy(pcm->name, "ATI IXP IEC958 (AC97)");
13051da177e4SLinus Torvalds 	else
13061da177e4SLinus Torvalds 		strcpy(pcm->name, "ATI IXP IEC958 (Direct)");
13071da177e4SLinus Torvalds 	chip->pcmdevs[ATI_PCMDEV_DIGITAL] = pcm;
13081da177e4SLinus Torvalds 
1309801bf057STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1310801bf057STakashi Iwai 				       &chip->pci->dev, 64*1024, 128*1024);
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 	/* pre-select AC97 SPDIF slots 10/11 */
13131da177e4SLinus Torvalds 	for (i = 0; i < NUM_ATI_CODECS; i++) {
13141da177e4SLinus Torvalds 		if (chip->ac97[i])
131574ee4ff1STakashi Iwai 			snd_ac97_update_bits(chip->ac97[i],
131674ee4ff1STakashi Iwai 					     AC97_EXTENDED_STATUS,
131774ee4ff1STakashi Iwai 					     0x03 << 4, 0x03 << 4);
13181da177e4SLinus Torvalds 	}
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	return 0;
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds /*
13261da177e4SLinus Torvalds  * interrupt handler
13271da177e4SLinus Torvalds  */
snd_atiixp_interrupt(int irq,void * dev_id)13287d12e780SDavid Howells static irqreturn_t snd_atiixp_interrupt(int irq, void *dev_id)
13291da177e4SLinus Torvalds {
133074ee4ff1STakashi Iwai 	struct atiixp *chip = dev_id;
13311da177e4SLinus Torvalds 	unsigned int status;
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 	status = atiixp_read(chip, ISR);
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	if (! status)
13361da177e4SLinus Torvalds 		return IRQ_NONE;
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	/* process audio DMA */
13391da177e4SLinus Torvalds 	if (status & ATI_REG_ISR_OUT_XRUN)
13401da177e4SLinus Torvalds 		snd_atiixp_xrun_dma(chip,  &chip->dmas[ATI_DMA_PLAYBACK]);
13411da177e4SLinus Torvalds 	else if (status & ATI_REG_ISR_OUT_STATUS)
13421da177e4SLinus Torvalds 		snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_PLAYBACK]);
13431da177e4SLinus Torvalds 	if (status & ATI_REG_ISR_IN_XRUN)
13441da177e4SLinus Torvalds 		snd_atiixp_xrun_dma(chip,  &chip->dmas[ATI_DMA_CAPTURE]);
13451da177e4SLinus Torvalds 	else if (status & ATI_REG_ISR_IN_STATUS)
13461da177e4SLinus Torvalds 		snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_CAPTURE]);
13471da177e4SLinus Torvalds 	if (! chip->spdif_over_aclink) {
13481da177e4SLinus Torvalds 		if (status & ATI_REG_ISR_SPDF_XRUN)
13491da177e4SLinus Torvalds 			snd_atiixp_xrun_dma(chip,  &chip->dmas[ATI_DMA_SPDIF]);
13501da177e4SLinus Torvalds 		else if (status & ATI_REG_ISR_SPDF_STATUS)
13511da177e4SLinus Torvalds 			snd_atiixp_update_dma(chip, &chip->dmas[ATI_DMA_SPDIF]);
13521da177e4SLinus Torvalds 	}
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	/* for codec detection */
13551da177e4SLinus Torvalds 	if (status & CODEC_CHECK_BITS) {
13561da177e4SLinus Torvalds 		unsigned int detected;
13571da177e4SLinus Torvalds 		detected = status & CODEC_CHECK_BITS;
13581da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
13591da177e4SLinus Torvalds 		chip->codec_not_ready_bits |= detected;
13601da177e4SLinus Torvalds 		atiixp_update(chip, IER, detected, 0); /* disable the detected irqs */
13611da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
13621da177e4SLinus Torvalds 	}
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	/* ack */
13651da177e4SLinus Torvalds 	atiixp_write(chip, ISR, status);
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	return IRQ_HANDLED;
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds /*
13721da177e4SLinus Torvalds  * ac97 mixer section
13731da177e4SLinus Torvalds  */
13741da177e4SLinus Torvalds 
1375eab0fbfaSJoe Perches static const struct ac97_quirk ac97_quirks[] = {
13761da177e4SLinus Torvalds 	{
137769ad07cfSJaroslav Kysela 		.subvendor = 0x103c,
137869ad07cfSJaroslav Kysela 		.subdevice = 0x006b,
13791da177e4SLinus Torvalds 		.name = "HP Pavilion ZV5030US",
13801da177e4SLinus Torvalds 		.type = AC97_TUNE_MUTE_LED
13811da177e4SLinus Torvalds 	},
1382a0faefedSMatthew Garrett 	{
1383a0faefedSMatthew Garrett 		.subvendor = 0x103c,
1384a0faefedSMatthew Garrett 		.subdevice = 0x308b,
1385a0faefedSMatthew Garrett 		.name = "HP nx6125",
1386a0faefedSMatthew Garrett 		.type = AC97_TUNE_MUTE_LED
1387a0faefedSMatthew Garrett 	},
1388e3ba906aSDaniel T Chen 	{
1389e3ba906aSDaniel T Chen 		.subvendor = 0x103c,
1390e3ba906aSDaniel T Chen 		.subdevice = 0x3091,
1391e3ba906aSDaniel T Chen 		.name = "unknown HP",
1392e3ba906aSDaniel T Chen 		.type = AC97_TUNE_MUTE_LED
1393e3ba906aSDaniel T Chen 	},
13941da177e4SLinus Torvalds 	{ } /* terminator */
13951da177e4SLinus Torvalds };
13961da177e4SLinus Torvalds 
snd_atiixp_mixer_new(struct atiixp * chip,int clock,const char * quirk_override)1397e23e7a14SBill Pemberton static int snd_atiixp_mixer_new(struct atiixp *chip, int clock,
139874ee4ff1STakashi Iwai 				const char *quirk_override)
13991da177e4SLinus Torvalds {
140074ee4ff1STakashi Iwai 	struct snd_ac97_bus *pbus;
140174ee4ff1STakashi Iwai 	struct snd_ac97_template ac97;
14021da177e4SLinus Torvalds 	int i, err;
14031da177e4SLinus Torvalds 	int codec_count;
140451055da5STakashi Iwai 	static const struct snd_ac97_bus_ops ops = {
14051da177e4SLinus Torvalds 		.write = snd_atiixp_ac97_write,
14061da177e4SLinus Torvalds 		.read = snd_atiixp_ac97_read,
14071da177e4SLinus Torvalds 	};
1408066c044bSTakashi Iwai 	static const unsigned int codec_skip[NUM_ATI_CODECS] = {
14091da177e4SLinus Torvalds 		ATI_REG_ISR_CODEC0_NOT_READY,
14101da177e4SLinus Torvalds 		ATI_REG_ISR_CODEC1_NOT_READY,
14111da177e4SLinus Torvalds 		ATI_REG_ISR_CODEC2_NOT_READY,
14121da177e4SLinus Torvalds 	};
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	if (snd_atiixp_codec_detect(chip) < 0)
14151da177e4SLinus Torvalds 		return -ENXIO;
14161da177e4SLinus Torvalds 
1417b83bea38STakashi Iwai 	err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus);
1418b83bea38STakashi Iwai 	if (err < 0)
14191da177e4SLinus Torvalds 		return err;
14201da177e4SLinus Torvalds 	pbus->clock = clock;
14211da177e4SLinus Torvalds 	chip->ac97_bus = pbus;
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 	codec_count = 0;
14241da177e4SLinus Torvalds 	for (i = 0; i < NUM_ATI_CODECS; i++) {
14251da177e4SLinus Torvalds 		if (chip->codec_not_ready_bits & codec_skip[i])
14261da177e4SLinus Torvalds 			continue;
14271da177e4SLinus Torvalds 		memset(&ac97, 0, sizeof(ac97));
14281da177e4SLinus Torvalds 		ac97.private_data = chip;
14291da177e4SLinus Torvalds 		ac97.pci = chip->pci;
14301da177e4SLinus Torvalds 		ac97.num = i;
1431f1a63a38STakashi Iwai 		ac97.scaps = AC97_SCAP_SKIP_MODEM | AC97_SCAP_POWER_SAVE;
14321da177e4SLinus Torvalds 		if (! chip->spdif_over_aclink)
14331da177e4SLinus Torvalds 			ac97.scaps |= AC97_SCAP_NO_SPDIF;
1434b83bea38STakashi Iwai 		err = snd_ac97_mixer(pbus, &ac97, &chip->ac97[i]);
1435b83bea38STakashi Iwai 		if (err < 0) {
14361da177e4SLinus Torvalds 			chip->ac97[i] = NULL; /* to be sure */
143725135fdcSTakashi Iwai 			dev_dbg(chip->card->dev,
143825135fdcSTakashi Iwai 				"codec %d not available for audio\n", i);
14391da177e4SLinus Torvalds 			continue;
14401da177e4SLinus Torvalds 		}
14411da177e4SLinus Torvalds 		codec_count++;
14421da177e4SLinus Torvalds 	}
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	if (! codec_count) {
144525135fdcSTakashi Iwai 		dev_err(chip->card->dev, "no codec available\n");
14461da177e4SLinus Torvalds 		return -ENODEV;
14471da177e4SLinus Torvalds 	}
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 	snd_ac97_tune_hardware(chip->ac97[0], ac97_quirks, quirk_override);
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	return 0;
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds /*
14561da177e4SLinus Torvalds  * power management
14571da177e4SLinus Torvalds  */
snd_atiixp_suspend(struct device * dev)145868cb2b55STakashi Iwai static int snd_atiixp_suspend(struct device *dev)
14591da177e4SLinus Torvalds {
146068cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
146192304cc7STakashi Iwai 	struct atiixp *chip = card->private_data;
14621da177e4SLinus Torvalds 	int i;
14631da177e4SLinus Torvalds 
146492304cc7STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
14651da177e4SLinus Torvalds 	for (i = 0; i < NUM_ATI_CODECS; i++)
14661da177e4SLinus Torvalds 		snd_ac97_suspend(chip->ac97[i]);
14671da177e4SLinus Torvalds 	snd_atiixp_aclink_down(chip);
14681da177e4SLinus Torvalds 	snd_atiixp_chip_stop(chip);
14691da177e4SLinus Torvalds 	return 0;
14701da177e4SLinus Torvalds }
14711da177e4SLinus Torvalds 
snd_atiixp_resume(struct device * dev)147268cb2b55STakashi Iwai static int snd_atiixp_resume(struct device *dev)
14731da177e4SLinus Torvalds {
147468cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
147592304cc7STakashi Iwai 	struct atiixp *chip = card->private_data;
14761da177e4SLinus Torvalds 	int i;
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 	snd_atiixp_aclink_reset(chip);
14791da177e4SLinus Torvalds 	snd_atiixp_chip_start(chip);
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	for (i = 0; i < NUM_ATI_CODECS; i++)
14821da177e4SLinus Torvalds 		snd_ac97_resume(chip->ac97[i]);
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 	for (i = 0; i < NUM_ATI_PCMDEVS; i++)
14851da177e4SLinus Torvalds 		if (chip->pcmdevs[i]) {
148674ee4ff1STakashi Iwai 			struct atiixp_dma *dma = &chip->dmas[i];
148741e4845cSJaroslav Kysela 			if (dma->substream && dma->suspended) {
14881da177e4SLinus Torvalds 				dma->ops->enable_dma(chip, 1);
14898e3d759dSTakashi Iwai 				dma->substream->ops->prepare(dma->substream);
14901da177e4SLinus Torvalds 				writel((u32)dma->desc_buf.addr | ATI_REG_LINKPTR_EN,
14911da177e4SLinus Torvalds 				       chip->remap_addr + dma->ops->llp_offset);
14921da177e4SLinus Torvalds 			}
14931da177e4SLinus Torvalds 		}
14941da177e4SLinus Torvalds 
149592304cc7STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
14961da177e4SLinus Torvalds 	return 0;
14971da177e4SLinus Torvalds }
149868cb2b55STakashi Iwai 
1499*b462d0b9STakashi Iwai static DEFINE_SIMPLE_DEV_PM_OPS(snd_atiixp_pm, snd_atiixp_suspend, snd_atiixp_resume);
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds /*
15021da177e4SLinus Torvalds  * proc interface for register dump
15031da177e4SLinus Torvalds  */
15041da177e4SLinus Torvalds 
snd_atiixp_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)150574ee4ff1STakashi Iwai static void snd_atiixp_proc_read(struct snd_info_entry *entry,
150674ee4ff1STakashi Iwai 				 struct snd_info_buffer *buffer)
15071da177e4SLinus Torvalds {
150874ee4ff1STakashi Iwai 	struct atiixp *chip = entry->private_data;
15091da177e4SLinus Torvalds 	int i;
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 	for (i = 0; i < 256; i += 4)
15121da177e4SLinus Torvalds 		snd_iprintf(buffer, "%02x: %08x\n", i, readl(chip->remap_addr + i));
15131da177e4SLinus Torvalds }
15141da177e4SLinus Torvalds 
snd_atiixp_proc_init(struct atiixp * chip)1515e23e7a14SBill Pemberton static void snd_atiixp_proc_init(struct atiixp *chip)
15161da177e4SLinus Torvalds {
151747f2769bSTakashi Iwai 	snd_card_ro_proc_new(chip->card, "atiixp", chip, snd_atiixp_proc_read);
15181da177e4SLinus Torvalds }
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds /*
15221da177e4SLinus Torvalds  * destructor
15231da177e4SLinus Torvalds  */
15241da177e4SLinus Torvalds 
snd_atiixp_free(struct snd_card * card)152586bde74dSTakashi Iwai static void snd_atiixp_free(struct snd_card *card)
15261da177e4SLinus Torvalds {
152786bde74dSTakashi Iwai 	snd_atiixp_chip_stop(card->private_data);
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds /*
15311da177e4SLinus Torvalds  * constructor for chip instance
15321da177e4SLinus Torvalds  */
snd_atiixp_init(struct snd_card * card,struct pci_dev * pci)153386bde74dSTakashi Iwai static int snd_atiixp_init(struct snd_card *card, struct pci_dev *pci)
15341da177e4SLinus Torvalds {
153586bde74dSTakashi Iwai 	struct atiixp *chip = card->private_data;
15361da177e4SLinus Torvalds 	int err;
15371da177e4SLinus Torvalds 
153886bde74dSTakashi Iwai 	err = pcim_enable_device(pci);
1539b83bea38STakashi Iwai 	if (err < 0)
15401da177e4SLinus Torvalds 		return err;
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
154362932df8SIngo Molnar 	mutex_init(&chip->open_mutex);
15441da177e4SLinus Torvalds 	chip->card = card;
15451da177e4SLinus Torvalds 	chip->pci = pci;
15461da177e4SLinus Torvalds 	chip->irq = -1;
154786bde74dSTakashi Iwai 	err = pcim_iomap_regions(pci, 1 << 0, "ATI IXP AC97");
154886bde74dSTakashi Iwai 	if (err < 0)
15491da177e4SLinus Torvalds 		return err;
15501da177e4SLinus Torvalds 	chip->addr = pci_resource_start(pci, 0);
155186bde74dSTakashi Iwai 	chip->remap_addr = pcim_iomap_table(pci)[0];
15521da177e4SLinus Torvalds 
155386bde74dSTakashi Iwai 	if (devm_request_irq(&pci->dev, pci->irq, snd_atiixp_interrupt,
155486bde74dSTakashi Iwai 			     IRQF_SHARED, KBUILD_MODNAME, chip)) {
155525135fdcSTakashi Iwai 		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
15561da177e4SLinus Torvalds 		return -EBUSY;
15571da177e4SLinus Torvalds 	}
15581da177e4SLinus Torvalds 	chip->irq = pci->irq;
15594504487dSTakashi Iwai 	card->sync_irq = chip->irq;
156086bde74dSTakashi Iwai 	card->private_free = snd_atiixp_free;
15611da177e4SLinus Torvalds 	pci_set_master(pci);
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 	return 0;
15641da177e4SLinus Torvalds }
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 
__snd_atiixp_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)156748e8addeSTakashi Iwai static int __snd_atiixp_probe(struct pci_dev *pci,
15681da177e4SLinus Torvalds 			      const struct pci_device_id *pci_id)
15691da177e4SLinus Torvalds {
157074ee4ff1STakashi Iwai 	struct snd_card *card;
157174ee4ff1STakashi Iwai 	struct atiixp *chip;
15721da177e4SLinus Torvalds 	int err;
15731da177e4SLinus Torvalds 
157486bde74dSTakashi Iwai 	err = snd_devm_card_new(&pci->dev, index, id, THIS_MODULE,
157586bde74dSTakashi Iwai 				sizeof(*chip), &card);
1576e58de7baSTakashi Iwai 	if (err < 0)
1577e58de7baSTakashi Iwai 		return err;
157886bde74dSTakashi Iwai 	chip = card->private_data;
15791da177e4SLinus Torvalds 
1580b7fe4622SClemens Ladisch 	strcpy(card->driver, spdif_aclink ? "ATIIXP" : "ATIIXP-SPDMA");
15811da177e4SLinus Torvalds 	strcpy(card->shortname, "ATI IXP");
158286bde74dSTakashi Iwai 	err = snd_atiixp_init(card, pci);
1583b83bea38STakashi Iwai 	if (err < 0)
158486bde74dSTakashi Iwai 		return err;
15851da177e4SLinus Torvalds 
1586b83bea38STakashi Iwai 	err = snd_atiixp_aclink_reset(chip);
1587b83bea38STakashi Iwai 	if (err < 0)
158886bde74dSTakashi Iwai 		return err;
15891da177e4SLinus Torvalds 
1590b7fe4622SClemens Ladisch 	chip->spdif_over_aclink = spdif_aclink;
15911da177e4SLinus Torvalds 
1592b83bea38STakashi Iwai 	err = snd_atiixp_mixer_new(chip, ac97_clock, ac97_quirk);
1593b83bea38STakashi Iwai 	if (err < 0)
159486bde74dSTakashi Iwai 		return err;
15951da177e4SLinus Torvalds 
1596b83bea38STakashi Iwai 	err = snd_atiixp_pcm_new(chip);
1597b83bea38STakashi Iwai 	if (err < 0)
159886bde74dSTakashi Iwai 		return err;
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	snd_atiixp_proc_init(chip);
16011da177e4SLinus Torvalds 
16021da177e4SLinus Torvalds 	snd_atiixp_chip_start(chip);
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	snprintf(card->longname, sizeof(card->longname),
160544c10138SAuke Kok 		 "%s rev %x with %s at %#lx, irq %i", card->shortname,
160644c10138SAuke Kok 		 pci->revision,
16071da177e4SLinus Torvalds 		 chip->ac97[0] ? snd_ac97_get_short_name(chip->ac97[0]) : "?",
16081da177e4SLinus Torvalds 		 chip->addr, chip->irq);
16091da177e4SLinus Torvalds 
1610b83bea38STakashi Iwai 	err = snd_card_register(card);
1611b83bea38STakashi Iwai 	if (err < 0)
161286bde74dSTakashi Iwai 		return err;
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
16151da177e4SLinus Torvalds 	return 0;
16161da177e4SLinus Torvalds }
16171da177e4SLinus Torvalds 
snd_atiixp_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)161848e8addeSTakashi Iwai static int snd_atiixp_probe(struct pci_dev *pci,
161948e8addeSTakashi Iwai 			    const struct pci_device_id *pci_id)
162048e8addeSTakashi Iwai {
162148e8addeSTakashi Iwai 	return snd_card_free_on_error(&pci->dev, __snd_atiixp_probe(pci, pci_id));
162248e8addeSTakashi Iwai }
162348e8addeSTakashi Iwai 
1624e9f66d9bSTakashi Iwai static struct pci_driver atiixp_driver = {
16253733e424STakashi Iwai 	.name = KBUILD_MODNAME,
16261da177e4SLinus Torvalds 	.id_table = snd_atiixp_ids,
16271da177e4SLinus Torvalds 	.probe = snd_atiixp_probe,
162868cb2b55STakashi Iwai 	.driver = {
1629*b462d0b9STakashi Iwai 		.pm = &snd_atiixp_pm,
163068cb2b55STakashi Iwai 	},
16311da177e4SLinus Torvalds };
16321da177e4SLinus Torvalds 
1633e9f66d9bSTakashi Iwai module_pci_driver(atiixp_driver);
1634