xref: /linux/sound/pci/cs4281.c (revision 6974f8ad44946701779209cb03cd8c6b598c3342)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Driver for Cirrus Logic CS4281 based PCI soundcard
4c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
76cbbfe1cSTakashi Iwai #include <linux/io.h>
81da177e4SLinus Torvalds #include <linux/delay.h>
91da177e4SLinus Torvalds #include <linux/interrupt.h>
101da177e4SLinus Torvalds #include <linux/init.h>
111da177e4SLinus Torvalds #include <linux/pci.h>
121da177e4SLinus Torvalds #include <linux/slab.h>
131da177e4SLinus Torvalds #include <linux/gameport.h>
1465a77217SPaul Gortmaker #include <linux/module.h>
151da177e4SLinus Torvalds #include <sound/core.h>
161da177e4SLinus Torvalds #include <sound/control.h>
171da177e4SLinus Torvalds #include <sound/pcm.h>
181da177e4SLinus Torvalds #include <sound/rawmidi.h>
191da177e4SLinus Torvalds #include <sound/ac97_codec.h>
209f6ab250STakashi Iwai #include <sound/tlv.h>
211da177e4SLinus Torvalds #include <sound/opl3.h>
221da177e4SLinus Torvalds #include <sound/initval.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds 
25c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
261da177e4SLinus Torvalds MODULE_DESCRIPTION("Cirrus Logic CS4281");
271da177e4SLinus Torvalds MODULE_LICENSE("GPL");
281da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{Cirrus Logic,CS4281}}");
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
311da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
32a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable switches */
33a67ff6a5SRusty Russell static bool dual_codec[SNDRV_CARDS];	/* dual codec */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
361da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for CS4281 soundcard.");
371da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
381da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for CS4281 soundcard.");
391da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
401da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable CS4281 soundcard.");
411da177e4SLinus Torvalds module_param_array(dual_codec, bool, NULL, 0444);
421da177e4SLinus Torvalds MODULE_PARM_DESC(dual_codec, "Secondary Codec ID (0 = disabled).");
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /*
451da177e4SLinus Torvalds  *  Direct registers
461da177e4SLinus Torvalds  */
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #define CS4281_BA0_SIZE		0x1000
491da177e4SLinus Torvalds #define CS4281_BA1_SIZE		0x10000
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds /*
521da177e4SLinus Torvalds  *  BA0 registers
531da177e4SLinus Torvalds  */
541da177e4SLinus Torvalds #define BA0_HISR		0x0000	/* Host Interrupt Status Register */
551da177e4SLinus Torvalds #define BA0_HISR_INTENA		(1<<31)	/* Internal Interrupt Enable Bit */
561da177e4SLinus Torvalds #define BA0_HISR_MIDI		(1<<22)	/* MIDI port interrupt */
571da177e4SLinus Torvalds #define BA0_HISR_FIFOI		(1<<20)	/* FIFO polled interrupt */
581da177e4SLinus Torvalds #define BA0_HISR_DMAI		(1<<18)	/* DMA interrupt (half or end) */
591da177e4SLinus Torvalds #define BA0_HISR_FIFO(c)	(1<<(12+(c))) /* FIFO channel interrupt */
601da177e4SLinus Torvalds #define BA0_HISR_DMA(c)		(1<<(8+(c)))  /* DMA channel interrupt */
611da177e4SLinus Torvalds #define BA0_HISR_GPPI		(1<<5)	/* General Purpose Input (Primary chip) */
621da177e4SLinus Torvalds #define BA0_HISR_GPSI		(1<<4)	/* General Purpose Input (Secondary chip) */
631da177e4SLinus Torvalds #define BA0_HISR_GP3I		(1<<3)	/* GPIO3 pin Interrupt */
641da177e4SLinus Torvalds #define BA0_HISR_GP1I		(1<<2)	/* GPIO1 pin Interrupt */
651da177e4SLinus Torvalds #define BA0_HISR_VUPI		(1<<1)	/* VOLUP pin Interrupt */
661da177e4SLinus Torvalds #define BA0_HISR_VDNI		(1<<0)	/* VOLDN pin Interrupt */
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #define BA0_HICR		0x0008	/* Host Interrupt Control Register */
691da177e4SLinus Torvalds #define BA0_HICR_CHGM		(1<<1)	/* INTENA Change Mask */
701da177e4SLinus Torvalds #define BA0_HICR_IEV		(1<<0)	/* INTENA Value */
711da177e4SLinus Torvalds #define BA0_HICR_EOI		(3<<0)	/* End of Interrupt command */
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds #define BA0_HIMR		0x000c	/* Host Interrupt Mask Register */
741da177e4SLinus Torvalds 					/* Use same contants as for BA0_HISR */
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds #define BA0_IIER		0x0010	/* ISA Interrupt Enable Register */
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #define BA0_HDSR0		0x00f0	/* Host DMA Engine 0 Status Register */
791da177e4SLinus Torvalds #define BA0_HDSR1		0x00f4	/* Host DMA Engine 1 Status Register */
801da177e4SLinus Torvalds #define BA0_HDSR2		0x00f8	/* Host DMA Engine 2 Status Register */
811da177e4SLinus Torvalds #define BA0_HDSR3		0x00fc	/* Host DMA Engine 3 Status Register */
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds #define BA0_HDSR_CH1P		(1<<25)	/* Channel 1 Pending */
841da177e4SLinus Torvalds #define BA0_HDSR_CH2P		(1<<24)	/* Channel 2 Pending */
851da177e4SLinus Torvalds #define BA0_HDSR_DHTC		(1<<17)	/* DMA Half Terminal Count */
861da177e4SLinus Torvalds #define BA0_HDSR_DTC		(1<<16)	/* DMA Terminal Count */
871da177e4SLinus Torvalds #define BA0_HDSR_DRUN		(1<<15)	/* DMA Running */
881da177e4SLinus Torvalds #define BA0_HDSR_RQ		(1<<7)	/* Pending Request */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #define BA0_DCA0		0x0110	/* Host DMA Engine 0 Current Address */
911da177e4SLinus Torvalds #define BA0_DCC0		0x0114	/* Host DMA Engine 0 Current Count */
921da177e4SLinus Torvalds #define BA0_DBA0		0x0118	/* Host DMA Engine 0 Base Address */
931da177e4SLinus Torvalds #define BA0_DBC0		0x011c	/* Host DMA Engine 0 Base Count */
941da177e4SLinus Torvalds #define BA0_DCA1		0x0120	/* Host DMA Engine 1 Current Address */
951da177e4SLinus Torvalds #define BA0_DCC1		0x0124	/* Host DMA Engine 1 Current Count */
961da177e4SLinus Torvalds #define BA0_DBA1		0x0128	/* Host DMA Engine 1 Base Address */
971da177e4SLinus Torvalds #define BA0_DBC1		0x012c	/* Host DMA Engine 1 Base Count */
981da177e4SLinus Torvalds #define BA0_DCA2		0x0130	/* Host DMA Engine 2 Current Address */
991da177e4SLinus Torvalds #define BA0_DCC2		0x0134	/* Host DMA Engine 2 Current Count */
1001da177e4SLinus Torvalds #define BA0_DBA2		0x0138	/* Host DMA Engine 2 Base Address */
1011da177e4SLinus Torvalds #define BA0_DBC2		0x013c	/* Host DMA Engine 2 Base Count */
1021da177e4SLinus Torvalds #define BA0_DCA3		0x0140	/* Host DMA Engine 3 Current Address */
1031da177e4SLinus Torvalds #define BA0_DCC3		0x0144	/* Host DMA Engine 3 Current Count */
1041da177e4SLinus Torvalds #define BA0_DBA3		0x0148	/* Host DMA Engine 3 Base Address */
1051da177e4SLinus Torvalds #define BA0_DBC3		0x014c	/* Host DMA Engine 3 Base Count */
1061da177e4SLinus Torvalds #define BA0_DMR0		0x0150	/* Host DMA Engine 0 Mode */
1071da177e4SLinus Torvalds #define BA0_DCR0		0x0154	/* Host DMA Engine 0 Command */
1081da177e4SLinus Torvalds #define BA0_DMR1		0x0158	/* Host DMA Engine 1 Mode */
1091da177e4SLinus Torvalds #define BA0_DCR1		0x015c	/* Host DMA Engine 1 Command */
1101da177e4SLinus Torvalds #define BA0_DMR2		0x0160	/* Host DMA Engine 2 Mode */
1111da177e4SLinus Torvalds #define BA0_DCR2		0x0164	/* Host DMA Engine 2 Command */
1121da177e4SLinus Torvalds #define BA0_DMR3		0x0168	/* Host DMA Engine 3 Mode */
1131da177e4SLinus Torvalds #define BA0_DCR3		0x016c	/* Host DMA Engine 3 Command */
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds #define BA0_DMR_DMA		(1<<29)	/* Enable DMA mode */
1161da177e4SLinus Torvalds #define BA0_DMR_POLL		(1<<28)	/* Enable poll mode */
1171da177e4SLinus Torvalds #define BA0_DMR_TBC		(1<<25)	/* Transfer By Channel */
1181da177e4SLinus Torvalds #define BA0_DMR_CBC		(1<<24)	/* Count By Channel (0 = frame resolution) */
1191da177e4SLinus Torvalds #define BA0_DMR_SWAPC		(1<<22)	/* Swap Left/Right Channels */
1201da177e4SLinus Torvalds #define BA0_DMR_SIZE20		(1<<20)	/* Sample is 20-bit */
1211da177e4SLinus Torvalds #define BA0_DMR_USIGN		(1<<19)	/* Unsigned */
1221da177e4SLinus Torvalds #define BA0_DMR_BEND		(1<<18)	/* Big Endian */
1231da177e4SLinus Torvalds #define BA0_DMR_MONO		(1<<17)	/* Mono */
1241da177e4SLinus Torvalds #define BA0_DMR_SIZE8		(1<<16)	/* Sample is 8-bit */
1251da177e4SLinus Torvalds #define BA0_DMR_TYPE_DEMAND	(0<<6)
1261da177e4SLinus Torvalds #define BA0_DMR_TYPE_SINGLE	(1<<6)
1271da177e4SLinus Torvalds #define BA0_DMR_TYPE_BLOCK	(2<<6)
1281da177e4SLinus Torvalds #define BA0_DMR_TYPE_CASCADE	(3<<6)	/* Not supported */
1291da177e4SLinus Torvalds #define BA0_DMR_DEC		(1<<5)	/* Access Increment (0) or Decrement (1) */
1301da177e4SLinus Torvalds #define BA0_DMR_AUTO		(1<<4)	/* Auto-Initialize */
1311da177e4SLinus Torvalds #define BA0_DMR_TR_VERIFY	(0<<2)	/* Verify Transfer */
1321da177e4SLinus Torvalds #define BA0_DMR_TR_WRITE	(1<<2)	/* Write Transfer */
1331da177e4SLinus Torvalds #define BA0_DMR_TR_READ		(2<<2)	/* Read Transfer */
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds #define BA0_DCR_HTCIE		(1<<17)	/* Half Terminal Count Interrupt */
1361da177e4SLinus Torvalds #define BA0_DCR_TCIE		(1<<16)	/* Terminal Count Interrupt */
1371da177e4SLinus Torvalds #define BA0_DCR_MSK		(1<<0)	/* DMA Mask bit */
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds #define BA0_FCR0		0x0180	/* FIFO Control 0 */
1401da177e4SLinus Torvalds #define BA0_FCR1		0x0184	/* FIFO Control 1 */
1411da177e4SLinus Torvalds #define BA0_FCR2		0x0188	/* FIFO Control 2 */
1421da177e4SLinus Torvalds #define BA0_FCR3		0x018c	/* FIFO Control 3 */
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds #define BA0_FCR_FEN		(1<<31)	/* FIFO Enable bit */
1451da177e4SLinus Torvalds #define BA0_FCR_DACZ		(1<<30)	/* DAC Zero */
1461da177e4SLinus Torvalds #define BA0_FCR_PSH		(1<<29)	/* Previous Sample Hold */
1471da177e4SLinus Torvalds #define BA0_FCR_RS(x)		(((x)&0x1f)<<24) /* Right Slot Mapping */
1481da177e4SLinus Torvalds #define BA0_FCR_LS(x)		(((x)&0x1f)<<16) /* Left Slot Mapping */
1491da177e4SLinus Torvalds #define BA0_FCR_SZ(x)		(((x)&0x7f)<<8)	/* FIFO buffer size (in samples) */
1501da177e4SLinus Torvalds #define BA0_FCR_OF(x)		(((x)&0x7f)<<0)	/* FIFO starting offset (in samples) */
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds #define BA0_FPDR0		0x0190	/* FIFO Polled Data 0 */
1531da177e4SLinus Torvalds #define BA0_FPDR1		0x0194	/* FIFO Polled Data 1 */
1541da177e4SLinus Torvalds #define BA0_FPDR2		0x0198	/* FIFO Polled Data 2 */
1551da177e4SLinus Torvalds #define BA0_FPDR3		0x019c	/* FIFO Polled Data 3 */
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds #define BA0_FCHS		0x020c	/* FIFO Channel Status */
1581da177e4SLinus Torvalds #define BA0_FCHS_RCO(x)		(1<<(7+(((x)&3)<<3))) /* Right Channel Out */
1591da177e4SLinus Torvalds #define BA0_FCHS_LCO(x)		(1<<(6+(((x)&3)<<3))) /* Left Channel Out */
1601da177e4SLinus Torvalds #define BA0_FCHS_MRP(x)		(1<<(5+(((x)&3)<<3))) /* Move Read Pointer */
1611da177e4SLinus Torvalds #define BA0_FCHS_FE(x)		(1<<(4+(((x)&3)<<3))) /* FIFO Empty */
1621da177e4SLinus Torvalds #define BA0_FCHS_FF(x)		(1<<(3+(((x)&3)<<3))) /* FIFO Full */
1631da177e4SLinus Torvalds #define BA0_FCHS_IOR(x)		(1<<(2+(((x)&3)<<3))) /* Internal Overrun Flag */
1641da177e4SLinus Torvalds #define BA0_FCHS_RCI(x)		(1<<(1+(((x)&3)<<3))) /* Right Channel In */
1651da177e4SLinus Torvalds #define BA0_FCHS_LCI(x)		(1<<(0+(((x)&3)<<3))) /* Left Channel In */
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds #define BA0_FSIC0		0x0210	/* FIFO Status and Interrupt Control 0 */
1681da177e4SLinus Torvalds #define BA0_FSIC1		0x0214	/* FIFO Status and Interrupt Control 1 */
1691da177e4SLinus Torvalds #define BA0_FSIC2		0x0218	/* FIFO Status and Interrupt Control 2 */
1701da177e4SLinus Torvalds #define BA0_FSIC3		0x021c	/* FIFO Status and Interrupt Control 3 */
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds #define BA0_FSIC_FIC(x)		(((x)&0x7f)<<24) /* FIFO Interrupt Count */
1731da177e4SLinus Torvalds #define BA0_FSIC_FORIE		(1<<23) /* FIFO OverRun Interrupt Enable */
1741da177e4SLinus Torvalds #define BA0_FSIC_FURIE		(1<<22) /* FIFO UnderRun Interrupt Enable */
1751da177e4SLinus Torvalds #define BA0_FSIC_FSCIE		(1<<16)	/* FIFO Sample Count Interrupt Enable */
1761da177e4SLinus Torvalds #define BA0_FSIC_FSC(x)		(((x)&0x7f)<<8) /* FIFO Sample Count */
1771da177e4SLinus Torvalds #define BA0_FSIC_FOR		(1<<7)	/* FIFO OverRun */
1781da177e4SLinus Torvalds #define BA0_FSIC_FUR		(1<<6)	/* FIFO UnderRun */
1791da177e4SLinus Torvalds #define BA0_FSIC_FSCR		(1<<0)	/* FIFO Sample Count Reached */
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds #define BA0_PMCS		0x0344	/* Power Management Control/Status */
1821da177e4SLinus Torvalds #define BA0_CWPR		0x03e0	/* Configuration Write Protect */
183a488e033SArnaud Patard 
1841da177e4SLinus Torvalds #define BA0_EPPMC		0x03e4	/* Extended PCI Power Management Control */
185a488e033SArnaud Patard #define BA0_EPPMC_FPDN		(1<<14) /* Full Power DowN */
186a488e033SArnaud Patard 
1871da177e4SLinus Torvalds #define BA0_GPIOR		0x03e8	/* GPIO Pin Interface Register */
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds #define BA0_SPMC		0x03ec	/* Serial Port Power Management Control (& ASDIN2 enable) */
1901da177e4SLinus Torvalds #define BA0_SPMC_GIPPEN		(1<<15)	/* GP INT Primary PME# Enable */
1911da177e4SLinus Torvalds #define BA0_SPMC_GISPEN		(1<<14)	/* GP INT Secondary PME# Enable */
1921da177e4SLinus Torvalds #define BA0_SPMC_EESPD		(1<<9)	/* EEPROM Serial Port Disable */
1931da177e4SLinus Torvalds #define BA0_SPMC_ASDI2E		(1<<8)	/* ASDIN2 Enable */
1941da177e4SLinus Torvalds #define BA0_SPMC_ASDO		(1<<7)	/* Asynchronous ASDOUT Assertion */
1951da177e4SLinus Torvalds #define BA0_SPMC_WUP2		(1<<3)	/* Wakeup for Secondary Input */
1961da177e4SLinus Torvalds #define BA0_SPMC_WUP1		(1<<2)	/* Wakeup for Primary Input */
1971da177e4SLinus Torvalds #define BA0_SPMC_ASYNC		(1<<1)	/* Asynchronous ASYNC Assertion */
1981da177e4SLinus Torvalds #define BA0_SPMC_RSTN		(1<<0)	/* Reset Not! */
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds #define BA0_CFLR		0x03f0	/* Configuration Load Register (EEPROM or BIOS) */
2011da177e4SLinus Torvalds #define BA0_CFLR_DEFAULT	0x00000001 /* CFLR must be in AC97 link mode */
2021da177e4SLinus Torvalds #define BA0_IISR		0x03f4	/* ISA Interrupt Select */
2031da177e4SLinus Torvalds #define BA0_TMS			0x03f8	/* Test Register */
2041da177e4SLinus Torvalds #define BA0_SSVID		0x03fc	/* Subsystem ID register */
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds #define BA0_CLKCR1		0x0400	/* Clock Control Register 1 */
2071da177e4SLinus Torvalds #define BA0_CLKCR1_CLKON	(1<<25)	/* Read Only */
2081da177e4SLinus Torvalds #define BA0_CLKCR1_DLLRDY	(1<<24)	/* DLL Ready */
2091da177e4SLinus Torvalds #define BA0_CLKCR1_DLLOS	(1<<6)	/* DLL Output Select */
2101da177e4SLinus Torvalds #define BA0_CLKCR1_SWCE		(1<<5)	/* Clock Enable */
2111da177e4SLinus Torvalds #define BA0_CLKCR1_DLLP		(1<<4)	/* DLL PowerUp */
2121da177e4SLinus Torvalds #define BA0_CLKCR1_DLLSS	(((x)&3)<<3) /* DLL Source Select */
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds #define BA0_FRR			0x0410	/* Feature Reporting Register */
2151da177e4SLinus Torvalds #define BA0_SLT12O		0x041c	/* Slot 12 GPIO Output Register for AC-Link */
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds #define BA0_SERMC		0x0420	/* Serial Port Master Control */
2181da177e4SLinus Torvalds #define BA0_SERMC_FCRN		(1<<27)	/* Force Codec Ready Not */
2191da177e4SLinus Torvalds #define BA0_SERMC_ODSEN2	(1<<25)	/* On-Demand Support Enable ASDIN2 */
2201da177e4SLinus Torvalds #define BA0_SERMC_ODSEN1	(1<<24)	/* On-Demand Support Enable ASDIN1 */
2211da177e4SLinus Torvalds #define BA0_SERMC_SXLB		(1<<21)	/* ASDIN2 to ASDOUT Loopback */
2221da177e4SLinus Torvalds #define BA0_SERMC_SLB		(1<<20)	/* ASDOUT to ASDIN2 Loopback */
2231da177e4SLinus Torvalds #define BA0_SERMC_LOVF		(1<<19)	/* Loopback Output Valid Frame bit */
2241da177e4SLinus Torvalds #define BA0_SERMC_TCID(x)	(((x)&3)<<16) /* Target Secondary Codec ID */
2251da177e4SLinus Torvalds #define BA0_SERMC_PXLB		(5<<1)	/* Primary Port External Loopback */
2261da177e4SLinus Torvalds #define BA0_SERMC_PLB		(4<<1)	/* Primary Port Internal Loopback */
2271da177e4SLinus Torvalds #define BA0_SERMC_PTC		(7<<1)	/* Port Timing Configuration */
2281da177e4SLinus Torvalds #define BA0_SERMC_PTC_AC97	(1<<1)	/* AC97 mode */
2291da177e4SLinus Torvalds #define BA0_SERMC_MSPE		(1<<0)	/* Master Serial Port Enable */
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds #define BA0_SERC1		0x0428	/* Serial Port Configuration 1 */
2321da177e4SLinus Torvalds #define BA0_SERC1_SO1F(x)	(((x)&7)>>1) /* Primary Output Port Format */
2331da177e4SLinus Torvalds #define BA0_SERC1_AC97		(1<<1)
2341da177e4SLinus Torvalds #define BA0_SERC1_SO1EN		(1<<0)	/* Primary Output Port Enable */
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds #define BA0_SERC2		0x042c	/* Serial Port Configuration 2 */
2371da177e4SLinus Torvalds #define BA0_SERC2_SI1F(x)	(((x)&7)>>1) /* Primary Input Port Format */
2381da177e4SLinus Torvalds #define BA0_SERC2_AC97		(1<<1)
2391da177e4SLinus Torvalds #define BA0_SERC2_SI1EN		(1<<0)	/* Primary Input Port Enable */
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds #define BA0_SLT12M		0x045c	/* Slot 12 Monitor Register for Primary AC-Link */
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds #define BA0_ACCTL		0x0460	/* AC'97 Control */
2441da177e4SLinus Torvalds #define BA0_ACCTL_TC		(1<<6)	/* Target Codec */
2451da177e4SLinus Torvalds #define BA0_ACCTL_CRW		(1<<4)	/* 0=Write, 1=Read Command */
2461da177e4SLinus Torvalds #define BA0_ACCTL_DCV		(1<<3)	/* Dynamic Command Valid */
2471da177e4SLinus Torvalds #define BA0_ACCTL_VFRM		(1<<2)	/* Valid Frame */
2481da177e4SLinus Torvalds #define BA0_ACCTL_ESYN		(1<<1)	/* Enable Sync */
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds #define BA0_ACSTS		0x0464	/* AC'97 Status */
2511da177e4SLinus Torvalds #define BA0_ACSTS_VSTS		(1<<1)	/* Valid Status */
2521da177e4SLinus Torvalds #define BA0_ACSTS_CRDY		(1<<0)	/* Codec Ready */
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds #define BA0_ACOSV		0x0468	/* AC'97 Output Slot Valid */
2551da177e4SLinus Torvalds #define BA0_ACOSV_SLV(x)	(1<<((x)-3))
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds #define BA0_ACCAD		0x046c	/* AC'97 Command Address */
2581da177e4SLinus Torvalds #define BA0_ACCDA		0x0470	/* AC'97 Command Data */
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds #define BA0_ACISV		0x0474	/* AC'97 Input Slot Valid */
2611da177e4SLinus Torvalds #define BA0_ACISV_SLV(x)	(1<<((x)-3))
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds #define BA0_ACSAD		0x0478	/* AC'97 Status Address */
2641da177e4SLinus Torvalds #define BA0_ACSDA		0x047c	/* AC'97 Status Data */
2651da177e4SLinus Torvalds #define BA0_JSPT		0x0480	/* Joystick poll/trigger */
2661da177e4SLinus Torvalds #define BA0_JSCTL		0x0484	/* Joystick control */
2671da177e4SLinus Torvalds #define BA0_JSC1		0x0488	/* Joystick control */
2681da177e4SLinus Torvalds #define BA0_JSC2		0x048c	/* Joystick control */
2691da177e4SLinus Torvalds #define BA0_JSIO		0x04a0
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds #define BA0_MIDCR		0x0490	/* MIDI Control */
2721da177e4SLinus Torvalds #define BA0_MIDCR_MRST		(1<<5)	/* Reset MIDI Interface */
2731da177e4SLinus Torvalds #define BA0_MIDCR_MLB		(1<<4)	/* MIDI Loop Back Enable */
2741da177e4SLinus Torvalds #define BA0_MIDCR_TIE		(1<<3)	/* MIDI Transmuit Interrupt Enable */
2751da177e4SLinus Torvalds #define BA0_MIDCR_RIE		(1<<2)	/* MIDI Receive Interrupt Enable */
2761da177e4SLinus Torvalds #define BA0_MIDCR_RXE		(1<<1)	/* MIDI Receive Enable */
2771da177e4SLinus Torvalds #define BA0_MIDCR_TXE		(1<<0)	/* MIDI Transmit Enable */
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds #define BA0_MIDCMD		0x0494	/* MIDI Command (wo) */
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds #define BA0_MIDSR		0x0494	/* MIDI Status (ro) */
2821da177e4SLinus Torvalds #define BA0_MIDSR_RDA		(1<<15)	/* Sticky bit (RBE 1->0) */
2831da177e4SLinus Torvalds #define BA0_MIDSR_TBE		(1<<14) /* Sticky bit (TBF 0->1) */
2841da177e4SLinus Torvalds #define BA0_MIDSR_RBE		(1<<7)	/* Receive Buffer Empty */
2851da177e4SLinus Torvalds #define BA0_MIDSR_TBF		(1<<6)	/* Transmit Buffer Full */
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds #define BA0_MIDWP		0x0498	/* MIDI Write */
2881da177e4SLinus Torvalds #define BA0_MIDRP		0x049c	/* MIDI Read (ro) */
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds #define BA0_AODSD1		0x04a8	/* AC'97 On-Demand Slot Disable for primary link (ro) */
2911da177e4SLinus Torvalds #define BA0_AODSD1_NDS(x)	(1<<((x)-3))
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds #define BA0_AODSD2		0x04ac	/* AC'97 On-Demand Slot Disable for secondary link (ro) */
2941da177e4SLinus Torvalds #define BA0_AODSD2_NDS(x)	(1<<((x)-3))
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds #define BA0_CFGI		0x04b0	/* Configure Interface (EEPROM interface) */
2971da177e4SLinus Torvalds #define BA0_SLT12M2		0x04dc	/* Slot 12 Monitor Register 2 for secondary AC-link */
2981da177e4SLinus Torvalds #define BA0_ACSTS2		0x04e4	/* AC'97 Status Register 2 */
2991da177e4SLinus Torvalds #define BA0_ACISV2		0x04f4	/* AC'97 Input Slot Valid Register 2 */
3001da177e4SLinus Torvalds #define BA0_ACSAD2		0x04f8	/* AC'97 Status Address Register 2 */
3011da177e4SLinus Torvalds #define BA0_ACSDA2		0x04fc	/* AC'97 Status Data Register 2 */
3021da177e4SLinus Torvalds #define BA0_FMSR		0x0730	/* FM Synthesis Status (ro) */
3031da177e4SLinus Torvalds #define BA0_B0AP		0x0730	/* FM Bank 0 Address Port (wo) */
3041da177e4SLinus Torvalds #define BA0_FMDP		0x0734	/* FM Data Port */
3051da177e4SLinus Torvalds #define BA0_B1AP		0x0738	/* FM Bank 1 Address Port */
3061da177e4SLinus Torvalds #define BA0_B1DP		0x073c	/* FM Bank 1 Data Port */
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds #define BA0_SSPM		0x0740	/* Sound System Power Management */
3091da177e4SLinus Torvalds #define BA0_SSPM_MIXEN		(1<<6)	/* Playback SRC + FM/Wavetable MIX */
3101da177e4SLinus Torvalds #define BA0_SSPM_CSRCEN		(1<<5)	/* Capture Sample Rate Converter Enable */
3111da177e4SLinus Torvalds #define BA0_SSPM_PSRCEN		(1<<4)	/* Playback Sample Rate Converter Enable */
3121da177e4SLinus Torvalds #define BA0_SSPM_JSEN		(1<<3)	/* Joystick Enable */
3131da177e4SLinus Torvalds #define BA0_SSPM_ACLEN		(1<<2)	/* Serial Port Engine and AC-Link Enable */
3141da177e4SLinus Torvalds #define BA0_SSPM_FMEN		(1<<1)	/* FM Synthesis Block Enable */
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds #define BA0_DACSR		0x0744	/* DAC Sample Rate - Playback SRC */
3171da177e4SLinus Torvalds #define BA0_ADCSR		0x0748	/* ADC Sample Rate - Capture SRC */
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds #define BA0_SSCR		0x074c	/* Sound System Control Register */
3201da177e4SLinus Torvalds #define BA0_SSCR_HVS1		(1<<23)	/* Hardwave Volume Step (0=1,1=2) */
3211da177e4SLinus Torvalds #define BA0_SSCR_MVCS		(1<<19)	/* Master Volume Codec Select */
3221da177e4SLinus Torvalds #define BA0_SSCR_MVLD		(1<<18)	/* Master Volume Line Out Disable */
3231da177e4SLinus Torvalds #define BA0_SSCR_MVAD		(1<<17)	/* Master Volume Alternate Out Disable */
3241da177e4SLinus Torvalds #define BA0_SSCR_MVMD		(1<<16)	/* Master Volume Mono Out Disable */
3251da177e4SLinus Torvalds #define BA0_SSCR_XLPSRC		(1<<8)	/* External SRC Loopback Mode */
3261da177e4SLinus Torvalds #define BA0_SSCR_LPSRC		(1<<7)	/* SRC Loopback Mode */
3271da177e4SLinus Torvalds #define BA0_SSCR_CDTX		(1<<5)	/* CD Transfer Data */
3281da177e4SLinus Torvalds #define BA0_SSCR_HVC		(1<<3)	/* Harware Volume Control Enable */
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds #define BA0_FMLVC		0x0754	/* FM Synthesis Left Volume Control */
3311da177e4SLinus Torvalds #define BA0_FMRVC		0x0758	/* FM Synthesis Right Volume Control */
3321da177e4SLinus Torvalds #define BA0_SRCSA		0x075c	/* SRC Slot Assignments */
3331da177e4SLinus Torvalds #define BA0_PPLVC		0x0760	/* PCM Playback Left Volume Control */
3341da177e4SLinus Torvalds #define BA0_PPRVC		0x0764	/* PCM Playback Right Volume Control */
3351da177e4SLinus Torvalds #define BA0_PASR		0x0768	/* playback sample rate */
3361da177e4SLinus Torvalds #define BA0_CASR		0x076C	/* capture sample rate */
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /* Source Slot Numbers - Playback */
3391da177e4SLinus Torvalds #define SRCSLOT_LEFT_PCM_PLAYBACK		0
3401da177e4SLinus Torvalds #define SRCSLOT_RIGHT_PCM_PLAYBACK		1
3411da177e4SLinus Torvalds #define SRCSLOT_PHONE_LINE_1_DAC		2
3421da177e4SLinus Torvalds #define SRCSLOT_CENTER_PCM_PLAYBACK		3
3431da177e4SLinus Torvalds #define SRCSLOT_LEFT_SURROUND_PCM_PLAYBACK	4
3441da177e4SLinus Torvalds #define SRCSLOT_RIGHT_SURROUND_PCM_PLAYBACK	5
3451da177e4SLinus Torvalds #define SRCSLOT_LFE_PCM_PLAYBACK		6
3461da177e4SLinus Torvalds #define SRCSLOT_PHONE_LINE_2_DAC		7
3471da177e4SLinus Torvalds #define SRCSLOT_HEADSET_DAC			8
3481da177e4SLinus Torvalds #define SRCSLOT_LEFT_WT				29  /* invalid for BA0_SRCSA */
3491da177e4SLinus Torvalds #define SRCSLOT_RIGHT_WT			30  /* invalid for BA0_SRCSA */
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds /* Source Slot Numbers - Capture */
3521da177e4SLinus Torvalds #define SRCSLOT_LEFT_PCM_RECORD			10
3531da177e4SLinus Torvalds #define SRCSLOT_RIGHT_PCM_RECORD		11
3541da177e4SLinus Torvalds #define SRCSLOT_PHONE_LINE_1_ADC		12
3551da177e4SLinus Torvalds #define SRCSLOT_MIC_ADC				13
3561da177e4SLinus Torvalds #define SRCSLOT_PHONE_LINE_2_ADC		17
3571da177e4SLinus Torvalds #define SRCSLOT_HEADSET_ADC			18
3581da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_LEFT_PCM_RECORD	20
3591da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_RIGHT_PCM_RECORD	21
3601da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_PHONE_LINE_1_ADC	22
3611da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_MIC_ADC		23
3621da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_PHONE_LINE_2_ADC	27
3631da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_HEADSET_ADC		28
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds /* Source Slot Numbers - Others */
3661da177e4SLinus Torvalds #define SRCSLOT_POWER_DOWN			31
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds /* MIDI modes */
3691da177e4SLinus Torvalds #define CS4281_MODE_OUTPUT		(1<<0)
3701da177e4SLinus Torvalds #define CS4281_MODE_INPUT		(1<<1)
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds /* joystick bits */
3731da177e4SLinus Torvalds /* Bits for JSPT */
3741da177e4SLinus Torvalds #define JSPT_CAX                                0x00000001
3751da177e4SLinus Torvalds #define JSPT_CAY                                0x00000002
3761da177e4SLinus Torvalds #define JSPT_CBX                                0x00000004
3771da177e4SLinus Torvalds #define JSPT_CBY                                0x00000008
3781da177e4SLinus Torvalds #define JSPT_BA1                                0x00000010
3791da177e4SLinus Torvalds #define JSPT_BA2                                0x00000020
3801da177e4SLinus Torvalds #define JSPT_BB1                                0x00000040
3811da177e4SLinus Torvalds #define JSPT_BB2                                0x00000080
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds /* Bits for JSCTL */
3841da177e4SLinus Torvalds #define JSCTL_SP_MASK                           0x00000003
3851da177e4SLinus Torvalds #define JSCTL_SP_SLOW                           0x00000000
3861da177e4SLinus Torvalds #define JSCTL_SP_MEDIUM_SLOW                    0x00000001
3871da177e4SLinus Torvalds #define JSCTL_SP_MEDIUM_FAST                    0x00000002
3881da177e4SLinus Torvalds #define JSCTL_SP_FAST                           0x00000003
3891da177e4SLinus Torvalds #define JSCTL_ARE                               0x00000004
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds /* Data register pairs masks */
3921da177e4SLinus Torvalds #define JSC1_Y1V_MASK                           0x0000FFFF
3931da177e4SLinus Torvalds #define JSC1_X1V_MASK                           0xFFFF0000
3941da177e4SLinus Torvalds #define JSC1_Y1V_SHIFT                          0
3951da177e4SLinus Torvalds #define JSC1_X1V_SHIFT                          16
3961da177e4SLinus Torvalds #define JSC2_Y2V_MASK                           0x0000FFFF
3971da177e4SLinus Torvalds #define JSC2_X2V_MASK                           0xFFFF0000
3981da177e4SLinus Torvalds #define JSC2_Y2V_SHIFT                          0
3991da177e4SLinus Torvalds #define JSC2_X2V_SHIFT                          16
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds /* JS GPIO */
4021da177e4SLinus Torvalds #define JSIO_DAX                                0x00000001
4031da177e4SLinus Torvalds #define JSIO_DAY                                0x00000002
4041da177e4SLinus Torvalds #define JSIO_DBX                                0x00000004
4051da177e4SLinus Torvalds #define JSIO_DBY                                0x00000008
4061da177e4SLinus Torvalds #define JSIO_AXOE                               0x00000010
4071da177e4SLinus Torvalds #define JSIO_AYOE                               0x00000020
4081da177e4SLinus Torvalds #define JSIO_BXOE                               0x00000040
4091da177e4SLinus Torvalds #define JSIO_BYOE                               0x00000080
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds /*
4121da177e4SLinus Torvalds  *
4131da177e4SLinus Torvalds  */
4141da177e4SLinus Torvalds 
41593e35f95STakashi Iwai struct cs4281_dma {
41693e35f95STakashi Iwai 	struct snd_pcm_substream *substream;
4171da177e4SLinus Torvalds 	unsigned int regDBA;		/* offset to DBA register */
4181da177e4SLinus Torvalds 	unsigned int regDCA;		/* offset to DCA register */
4191da177e4SLinus Torvalds 	unsigned int regDBC;		/* offset to DBC register */
4201da177e4SLinus Torvalds 	unsigned int regDCC;		/* offset to DCC register */
4211da177e4SLinus Torvalds 	unsigned int regDMR;		/* offset to DMR register */
4221da177e4SLinus Torvalds 	unsigned int regDCR;		/* offset to DCR register */
4231da177e4SLinus Torvalds 	unsigned int regHDSR;		/* offset to HDSR register */
4241da177e4SLinus Torvalds 	unsigned int regFCR;		/* offset to FCR register */
4251da177e4SLinus Torvalds 	unsigned int regFSIC;		/* offset to FSIC register */
4261da177e4SLinus Torvalds 	unsigned int valDMR;		/* DMA mode */
4271da177e4SLinus Torvalds 	unsigned int valDCR;		/* DMA command */
4281da177e4SLinus Torvalds 	unsigned int valFCR;		/* FIFO control */
4291da177e4SLinus Torvalds 	unsigned int fifo_offset;	/* FIFO offset within BA1 */
4301da177e4SLinus Torvalds 	unsigned char left_slot;	/* FIFO left slot */
4311da177e4SLinus Torvalds 	unsigned char right_slot;	/* FIFO right slot */
4321da177e4SLinus Torvalds 	int frag;			/* period number */
4331da177e4SLinus Torvalds };
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds #define SUSPEND_REGISTERS	20
4361da177e4SLinus Torvalds 
43793e35f95STakashi Iwai struct cs4281 {
4381da177e4SLinus Torvalds 	int irq;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	void __iomem *ba0;		/* virtual (accessible) address */
4411da177e4SLinus Torvalds 	void __iomem *ba1;		/* virtual (accessible) address */
4421da177e4SLinus Torvalds 	unsigned long ba0_addr;
4431da177e4SLinus Torvalds 	unsigned long ba1_addr;
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	int dual_codec;
4461da177e4SLinus Torvalds 
44793e35f95STakashi Iwai 	struct snd_ac97_bus *ac97_bus;
44893e35f95STakashi Iwai 	struct snd_ac97 *ac97;
44993e35f95STakashi Iwai 	struct snd_ac97 *ac97_secondary;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	struct pci_dev *pci;
45293e35f95STakashi Iwai 	struct snd_card *card;
45393e35f95STakashi Iwai 	struct snd_pcm *pcm;
45493e35f95STakashi Iwai 	struct snd_rawmidi *rmidi;
45593e35f95STakashi Iwai 	struct snd_rawmidi_substream *midi_input;
45693e35f95STakashi Iwai 	struct snd_rawmidi_substream *midi_output;
4571da177e4SLinus Torvalds 
45893e35f95STakashi Iwai 	struct cs4281_dma dma[4];
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	unsigned char src_left_play_slot;
4611da177e4SLinus Torvalds 	unsigned char src_right_play_slot;
4621da177e4SLinus Torvalds 	unsigned char src_left_rec_slot;
4631da177e4SLinus Torvalds 	unsigned char src_right_rec_slot;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	unsigned int spurious_dhtc_irq;
4661da177e4SLinus Torvalds 	unsigned int spurious_dtc_irq;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	spinlock_t reg_lock;
4691da177e4SLinus Torvalds 	unsigned int midcr;
4701da177e4SLinus Torvalds 	unsigned int uartm;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	struct gameport *gameport;
4731da177e4SLinus Torvalds 
474c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
4751da177e4SLinus Torvalds 	u32 suspend_regs[SUSPEND_REGISTERS];
4761da177e4SLinus Torvalds #endif
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds };
4791da177e4SLinus Torvalds 
4807d12e780SDavid Howells static irqreturn_t snd_cs4281_interrupt(int irq, void *dev_id);
4811da177e4SLinus Torvalds 
4829baa3c34SBenoit Taine static const struct pci_device_id snd_cs4281_ids[] = {
48328d27aaeSJoe Perches 	{ PCI_VDEVICE(CIRRUS, 0x6005), 0, },	/* CS4281 */
4841da177e4SLinus Torvalds 	{ 0, }
4851da177e4SLinus Torvalds };
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_cs4281_ids);
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds /*
4901da177e4SLinus Torvalds  *  constants
4911da177e4SLinus Torvalds  */
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds #define CS4281_FIFO_SIZE	32
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds /*
4961da177e4SLinus Torvalds  *  common I/O routines
4971da177e4SLinus Torvalds  */
4981da177e4SLinus Torvalds 
49993e35f95STakashi Iwai static inline void snd_cs4281_pokeBA0(struct cs4281 *chip, unsigned long offset,
50093e35f95STakashi Iwai 				      unsigned int val)
5011da177e4SLinus Torvalds {
5021da177e4SLinus Torvalds         writel(val, chip->ba0 + offset);
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
50593e35f95STakashi Iwai static inline unsigned int snd_cs4281_peekBA0(struct cs4281 *chip, unsigned long offset)
5061da177e4SLinus Torvalds {
5071da177e4SLinus Torvalds         return readl(chip->ba0 + offset);
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
51093e35f95STakashi Iwai static void snd_cs4281_ac97_write(struct snd_ac97 *ac97,
5111da177e4SLinus Torvalds 				  unsigned short reg, unsigned short val)
5121da177e4SLinus Torvalds {
5131da177e4SLinus Torvalds 	/*
5141da177e4SLinus Torvalds 	 *  1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
5151da177e4SLinus Torvalds 	 *  2. Write ACCDA = Command Data Register = 470h    for data to write to AC97
5161da177e4SLinus Torvalds 	 *  3. Write ACCTL = Control Register = 460h for initiating the write
5171da177e4SLinus Torvalds 	 *  4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
5181da177e4SLinus Torvalds 	 *  5. if DCV not cleared, break and return error
5191da177e4SLinus Torvalds 	 */
52093e35f95STakashi Iwai 	struct cs4281 *chip = ac97->private_data;
5211da177e4SLinus Torvalds 	int count;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	/*
5241da177e4SLinus Torvalds 	 *  Setup the AC97 control registers on the CS461x to send the
5251da177e4SLinus Torvalds 	 *  appropriate command to the AC97 to perform the read.
5261da177e4SLinus Torvalds 	 *  ACCAD = Command Address Register = 46Ch
5271da177e4SLinus Torvalds 	 *  ACCDA = Command Data Register = 470h
5281da177e4SLinus Torvalds 	 *  ACCTL = Control Register = 460h
5291da177e4SLinus Torvalds 	 *  set DCV - will clear when process completed
5301da177e4SLinus Torvalds 	 *  reset CRW - Write command
5311da177e4SLinus Torvalds 	 *  set VFRM - valid frame enabled
5321da177e4SLinus Torvalds 	 *  set ESYN - ASYNC generation enabled
5331da177e4SLinus Torvalds 	 *  set RSTN - ARST# inactive, AC97 codec not reset
5341da177e4SLinus Torvalds          */
5351da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCAD, reg);
5361da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCDA, val);
5371da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, BA0_ACCTL_DCV | BA0_ACCTL_VFRM |
5381da177e4SLinus Torvalds 				            BA0_ACCTL_ESYN | (ac97->num ? BA0_ACCTL_TC : 0));
5391da177e4SLinus Torvalds 	for (count = 0; count < 2000; count++) {
5401da177e4SLinus Torvalds 		/*
5411da177e4SLinus Torvalds 		 *  First, we want to wait for a short time.
5421da177e4SLinus Torvalds 		 */
5431da177e4SLinus Torvalds 		udelay(10);
5441da177e4SLinus Torvalds 		/*
5451da177e4SLinus Torvalds 		 *  Now, check to see if the write has completed.
5461da177e4SLinus Torvalds 		 *  ACCTL = 460h, DCV should be reset by now and 460h = 07h
5471da177e4SLinus Torvalds 		 */
5481da177e4SLinus Torvalds 		if (!(snd_cs4281_peekBA0(chip, BA0_ACCTL) & BA0_ACCTL_DCV)) {
5491da177e4SLinus Torvalds 			return;
5501da177e4SLinus Torvalds 		}
5511da177e4SLinus Torvalds 	}
552b055e7b4STakashi Iwai 	dev_err(chip->card->dev,
553b055e7b4STakashi Iwai 		"AC'97 write problem, reg = 0x%x, val = 0x%x\n", reg, val);
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
55693e35f95STakashi Iwai static unsigned short snd_cs4281_ac97_read(struct snd_ac97 *ac97,
5571da177e4SLinus Torvalds 					   unsigned short reg)
5581da177e4SLinus Torvalds {
55993e35f95STakashi Iwai 	struct cs4281 *chip = ac97->private_data;
5601da177e4SLinus Torvalds 	int count;
5611da177e4SLinus Torvalds 	unsigned short result;
5621da177e4SLinus Torvalds 	// FIXME: volatile is necessary in the following due to a bug of
5631da177e4SLinus Torvalds 	// some gcc versions
56493e35f95STakashi Iwai 	volatile int ac97_num = ((volatile struct snd_ac97 *)ac97)->num;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	/*
5671da177e4SLinus Torvalds 	 *  1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
5681da177e4SLinus Torvalds 	 *  2. Write ACCDA = Command Data Register = 470h    for data to write to AC97
5691da177e4SLinus Torvalds 	 *  3. Write ACCTL = Control Register = 460h for initiating the write
5701da177e4SLinus Torvalds 	 *  4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
5711da177e4SLinus Torvalds 	 *  5. if DCV not cleared, break and return error
5721da177e4SLinus Torvalds 	 *  6. Read ACSTS = Status Register = 464h, check VSTS bit
5731da177e4SLinus Torvalds 	 */
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	snd_cs4281_peekBA0(chip, ac97_num ? BA0_ACSDA2 : BA0_ACSDA);
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	/*
5781da177e4SLinus Torvalds 	 *  Setup the AC97 control registers on the CS461x to send the
5791da177e4SLinus Torvalds 	 *  appropriate command to the AC97 to perform the read.
5801da177e4SLinus Torvalds 	 *  ACCAD = Command Address Register = 46Ch
5811da177e4SLinus Torvalds 	 *  ACCDA = Command Data Register = 470h
5821da177e4SLinus Torvalds 	 *  ACCTL = Control Register = 460h
5831da177e4SLinus Torvalds 	 *  set DCV - will clear when process completed
5841da177e4SLinus Torvalds 	 *  set CRW - Read command
5851da177e4SLinus Torvalds 	 *  set VFRM - valid frame enabled
5861da177e4SLinus Torvalds 	 *  set ESYN - ASYNC generation enabled
5871da177e4SLinus Torvalds 	 *  set RSTN - ARST# inactive, AC97 codec not reset
5881da177e4SLinus Torvalds 	 */
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCAD, reg);
5911da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCDA, 0);
5921da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, BA0_ACCTL_DCV | BA0_ACCTL_CRW |
5931da177e4SLinus Torvalds 					    BA0_ACCTL_VFRM | BA0_ACCTL_ESYN |
5941da177e4SLinus Torvalds 			   (ac97_num ? BA0_ACCTL_TC : 0));
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	/*
5981da177e4SLinus Torvalds 	 *  Wait for the read to occur.
5991da177e4SLinus Torvalds 	 */
6001da177e4SLinus Torvalds 	for (count = 0; count < 500; count++) {
6011da177e4SLinus Torvalds 		/*
6021da177e4SLinus Torvalds 		 *  First, we want to wait for a short time.
6031da177e4SLinus Torvalds 	 	 */
6041da177e4SLinus Torvalds 		udelay(10);
6051da177e4SLinus Torvalds 		/*
6061da177e4SLinus Torvalds 		 *  Now, check to see if the read has completed.
6071da177e4SLinus Torvalds 		 *  ACCTL = 460h, DCV should be reset by now and 460h = 17h
6081da177e4SLinus Torvalds 		 */
6091da177e4SLinus Torvalds 		if (!(snd_cs4281_peekBA0(chip, BA0_ACCTL) & BA0_ACCTL_DCV))
6101da177e4SLinus Torvalds 			goto __ok1;
6111da177e4SLinus Torvalds 	}
6121da177e4SLinus Torvalds 
613b055e7b4STakashi Iwai 	dev_err(chip->card->dev,
614b055e7b4STakashi Iwai 		"AC'97 read problem (ACCTL_DCV), reg = 0x%x\n", reg);
6151da177e4SLinus Torvalds 	result = 0xffff;
6161da177e4SLinus Torvalds 	goto __end;
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds       __ok1:
6191da177e4SLinus Torvalds 	/*
6201da177e4SLinus Torvalds 	 *  Wait for the valid status bit to go active.
6211da177e4SLinus Torvalds 	 */
6221da177e4SLinus Torvalds 	for (count = 0; count < 100; count++) {
6231da177e4SLinus Torvalds 		/*
6241da177e4SLinus Torvalds 		 *  Read the AC97 status register.
6251da177e4SLinus Torvalds 		 *  ACSTS = Status Register = 464h
6261da177e4SLinus Torvalds 		 *  VSTS - Valid Status
6271da177e4SLinus Torvalds 		 */
6281da177e4SLinus Torvalds 		if (snd_cs4281_peekBA0(chip, ac97_num ? BA0_ACSTS2 : BA0_ACSTS) & BA0_ACSTS_VSTS)
6291da177e4SLinus Torvalds 			goto __ok2;
6301da177e4SLinus Torvalds 		udelay(10);
6311da177e4SLinus Torvalds 	}
6321da177e4SLinus Torvalds 
633b055e7b4STakashi Iwai 	dev_err(chip->card->dev,
634b055e7b4STakashi Iwai 		"AC'97 read problem (ACSTS_VSTS), reg = 0x%x\n", reg);
6351da177e4SLinus Torvalds 	result = 0xffff;
6361da177e4SLinus Torvalds 	goto __end;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds       __ok2:
6391da177e4SLinus Torvalds 	/*
6401da177e4SLinus Torvalds 	 *  Read the data returned from the AC97 register.
6411da177e4SLinus Torvalds 	 *  ACSDA = Status Data Register = 474h
6421da177e4SLinus Torvalds 	 */
6431da177e4SLinus Torvalds 	result = snd_cs4281_peekBA0(chip, ac97_num ? BA0_ACSDA2 : BA0_ACSDA);
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds       __end:
6461da177e4SLinus Torvalds 	return result;
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds /*
6501da177e4SLinus Torvalds  *  PCM part
6511da177e4SLinus Torvalds  */
6521da177e4SLinus Torvalds 
65393e35f95STakashi Iwai static int snd_cs4281_trigger(struct snd_pcm_substream *substream, int cmd)
6541da177e4SLinus Torvalds {
65593e35f95STakashi Iwai 	struct cs4281_dma *dma = substream->runtime->private_data;
65693e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
6591da177e4SLinus Torvalds 	switch (cmd) {
6601da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
6611da177e4SLinus Torvalds 		dma->valDCR |= BA0_DCR_MSK;
6621da177e4SLinus Torvalds 		dma->valFCR |= BA0_FCR_FEN;
6631da177e4SLinus Torvalds 		break;
6641da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
6651da177e4SLinus Torvalds 		dma->valDCR &= ~BA0_DCR_MSK;
6661da177e4SLinus Torvalds 		dma->valFCR &= ~BA0_FCR_FEN;
6671da177e4SLinus Torvalds 		break;
6681da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
6691da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_RESUME:
6701da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, dma->regDMR, dma->valDMR & ~BA0_DMR_DMA);
6711da177e4SLinus Torvalds 		dma->valDMR |= BA0_DMR_DMA;
6721da177e4SLinus Torvalds 		dma->valDCR &= ~BA0_DCR_MSK;
6731da177e4SLinus Torvalds 		dma->valFCR |= BA0_FCR_FEN;
6741da177e4SLinus Torvalds 		break;
6751da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
6761da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_SUSPEND:
6771da177e4SLinus Torvalds 		dma->valDMR &= ~(BA0_DMR_DMA|BA0_DMR_POLL);
6781da177e4SLinus Torvalds 		dma->valDCR |= BA0_DCR_MSK;
6791da177e4SLinus Torvalds 		dma->valFCR &= ~BA0_FCR_FEN;
6801da177e4SLinus Torvalds 		/* Leave wave playback FIFO enabled for FM */
6811da177e4SLinus Torvalds 		if (dma->regFCR != BA0_FCR0)
6821da177e4SLinus Torvalds 			dma->valFCR &= ~BA0_FCR_FEN;
6831da177e4SLinus Torvalds 		break;
6841da177e4SLinus Torvalds 	default:
6851da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
6861da177e4SLinus Torvalds 		return -EINVAL;
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regDMR, dma->valDMR);
6891da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regFCR, dma->valFCR);
6901da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regDCR, dma->valDCR);
6911da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
6921da177e4SLinus Torvalds 	return 0;
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds static unsigned int snd_cs4281_rate(unsigned int rate, unsigned int *real_rate)
6961da177e4SLinus Torvalds {
697388b00f0SColin Ian King 	unsigned int val;
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	if (real_rate)
7001da177e4SLinus Torvalds 		*real_rate = rate;
7011da177e4SLinus Torvalds 	/* special "hardcoded" rates */
7021da177e4SLinus Torvalds 	switch (rate) {
7031da177e4SLinus Torvalds 	case 8000:	return 5;
7041da177e4SLinus Torvalds 	case 11025:	return 4;
7051da177e4SLinus Torvalds 	case 16000:	return 3;
7061da177e4SLinus Torvalds 	case 22050:	return 2;
7071da177e4SLinus Torvalds 	case 44100:	return 1;
7081da177e4SLinus Torvalds 	case 48000:	return 0;
7091da177e4SLinus Torvalds 	default:
710388b00f0SColin Ian King 		break;
7111da177e4SLinus Torvalds 	}
7121da177e4SLinus Torvalds 	val = 1536000 / rate;
7131da177e4SLinus Torvalds 	if (real_rate)
7141da177e4SLinus Torvalds 		*real_rate = 1536000 / val;
7151da177e4SLinus Torvalds 	return val;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
71893e35f95STakashi Iwai static void snd_cs4281_mode(struct cs4281 *chip, struct cs4281_dma *dma,
71993e35f95STakashi Iwai 			    struct snd_pcm_runtime *runtime,
72093e35f95STakashi Iwai 			    int capture, int src)
7211da177e4SLinus Torvalds {
7221da177e4SLinus Torvalds 	int rec_mono;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	dma->valDMR = BA0_DMR_TYPE_SINGLE | BA0_DMR_AUTO |
7251da177e4SLinus Torvalds 		      (capture ? BA0_DMR_TR_WRITE : BA0_DMR_TR_READ);
7261da177e4SLinus Torvalds 	if (runtime->channels == 1)
7271da177e4SLinus Torvalds 		dma->valDMR |= BA0_DMR_MONO;
7281da177e4SLinus Torvalds 	if (snd_pcm_format_unsigned(runtime->format) > 0)
7291da177e4SLinus Torvalds 		dma->valDMR |= BA0_DMR_USIGN;
7301da177e4SLinus Torvalds 	if (snd_pcm_format_big_endian(runtime->format) > 0)
7311da177e4SLinus Torvalds 		dma->valDMR |= BA0_DMR_BEND;
7321da177e4SLinus Torvalds 	switch (snd_pcm_format_width(runtime->format)) {
7331da177e4SLinus Torvalds 	case 8: dma->valDMR |= BA0_DMR_SIZE8;
7341da177e4SLinus Torvalds 		if (runtime->channels == 1)
7351da177e4SLinus Torvalds 			dma->valDMR |= BA0_DMR_SWAPC;
7361da177e4SLinus Torvalds 		break;
7371da177e4SLinus Torvalds 	case 32: dma->valDMR |= BA0_DMR_SIZE20; break;
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 	dma->frag = 0;	/* for workaround */
7401da177e4SLinus Torvalds 	dma->valDCR = BA0_DCR_TCIE | BA0_DCR_MSK;
7411da177e4SLinus Torvalds 	if (runtime->buffer_size != runtime->period_size)
7421da177e4SLinus Torvalds 		dma->valDCR |= BA0_DCR_HTCIE;
7431da177e4SLinus Torvalds 	/* Initialize DMA */
7441da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regDBA, runtime->dma_addr);
7451da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regDBC, runtime->buffer_size - 1);
7461da177e4SLinus Torvalds 	rec_mono = (chip->dma[1].valDMR & BA0_DMR_MONO) == BA0_DMR_MONO;
7471da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SRCSA, (chip->src_left_play_slot << 0) |
7481da177e4SLinus Torvalds 					    (chip->src_right_play_slot << 8) |
7491da177e4SLinus Torvalds 					    (chip->src_left_rec_slot << 16) |
7501da177e4SLinus Torvalds 					    ((rec_mono ? 31 : chip->src_right_rec_slot) << 24));
7511da177e4SLinus Torvalds 	if (!src)
7521da177e4SLinus Torvalds 		goto __skip_src;
7531da177e4SLinus Torvalds 	if (!capture) {
7541da177e4SLinus Torvalds 		if (dma->left_slot == chip->src_left_play_slot) {
7551da177e4SLinus Torvalds 			unsigned int val = snd_cs4281_rate(runtime->rate, NULL);
756da3cec35STakashi Iwai 			snd_BUG_ON(dma->right_slot != chip->src_right_play_slot);
7571da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_DACSR, val);
7581da177e4SLinus Torvalds 		}
7591da177e4SLinus Torvalds 	} else {
7601da177e4SLinus Torvalds 		if (dma->left_slot == chip->src_left_rec_slot) {
7611da177e4SLinus Torvalds 			unsigned int val = snd_cs4281_rate(runtime->rate, NULL);
762da3cec35STakashi Iwai 			snd_BUG_ON(dma->right_slot != chip->src_right_rec_slot);
7631da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_ADCSR, val);
7641da177e4SLinus Torvalds 		}
7651da177e4SLinus Torvalds 	}
7661da177e4SLinus Torvalds       __skip_src:
7671da177e4SLinus Torvalds 	/* Deactivate wave playback FIFO before changing slot assignments */
7681da177e4SLinus Torvalds 	if (dma->regFCR == BA0_FCR0)
7691da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, dma->regFCR, snd_cs4281_peekBA0(chip, dma->regFCR) & ~BA0_FCR_FEN);
7701da177e4SLinus Torvalds 	/* Initialize FIFO */
7711da177e4SLinus Torvalds 	dma->valFCR = BA0_FCR_LS(dma->left_slot) |
7721da177e4SLinus Torvalds 		      BA0_FCR_RS(capture && (dma->valDMR & BA0_DMR_MONO) ? 31 : dma->right_slot) |
7731da177e4SLinus Torvalds 		      BA0_FCR_SZ(CS4281_FIFO_SIZE) |
7741da177e4SLinus Torvalds 		      BA0_FCR_OF(dma->fifo_offset);
7751da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regFCR, dma->valFCR | (capture ? BA0_FCR_PSH : 0));
7761da177e4SLinus Torvalds 	/* Activate FIFO again for FM playback */
7771da177e4SLinus Torvalds 	if (dma->regFCR == BA0_FCR0)
7781da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, dma->regFCR, dma->valFCR | BA0_FCR_FEN);
7791da177e4SLinus Torvalds 	/* Clear FIFO Status and Interrupt Control Register */
7801da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regFSIC, 0);
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
78393e35f95STakashi Iwai static int snd_cs4281_hw_params(struct snd_pcm_substream *substream,
78493e35f95STakashi Iwai 				struct snd_pcm_hw_params *hw_params)
7851da177e4SLinus Torvalds {
7861da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
78993e35f95STakashi Iwai static int snd_cs4281_hw_free(struct snd_pcm_substream *substream)
7901da177e4SLinus Torvalds {
7911da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
79493e35f95STakashi Iwai static int snd_cs4281_playback_prepare(struct snd_pcm_substream *substream)
7951da177e4SLinus Torvalds {
79693e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
79793e35f95STakashi Iwai 	struct cs4281_dma *dma = runtime->private_data;
79893e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
8011da177e4SLinus Torvalds 	snd_cs4281_mode(chip, dma, runtime, 0, 1);
8021da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
8031da177e4SLinus Torvalds 	return 0;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
80693e35f95STakashi Iwai static int snd_cs4281_capture_prepare(struct snd_pcm_substream *substream)
8071da177e4SLinus Torvalds {
80893e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
80993e35f95STakashi Iwai 	struct cs4281_dma *dma = runtime->private_data;
81093e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
8131da177e4SLinus Torvalds 	snd_cs4281_mode(chip, dma, runtime, 1, 1);
8141da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
8151da177e4SLinus Torvalds 	return 0;
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
81893e35f95STakashi Iwai static snd_pcm_uframes_t snd_cs4281_pointer(struct snd_pcm_substream *substream)
8191da177e4SLinus Torvalds {
82093e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
82193e35f95STakashi Iwai 	struct cs4281_dma *dma = runtime->private_data;
82293e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
8231da177e4SLinus Torvalds 
824ee419653STakashi Iwai 	/*
825b055e7b4STakashi Iwai 	dev_dbg(chip->card->dev,
826b055e7b4STakashi Iwai 		"DCC = 0x%x, buffer_size = 0x%x, jiffies = %li\n",
827ee419653STakashi Iwai 		snd_cs4281_peekBA0(chip, dma->regDCC), runtime->buffer_size,
828ee419653STakashi Iwai 	       jiffies);
829ee419653STakashi Iwai 	*/
8301da177e4SLinus Torvalds 	return runtime->buffer_size -
8311da177e4SLinus Torvalds 	       snd_cs4281_peekBA0(chip, dma->regDCC) - 1;
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds 
834dee49895SBhumika Goyal static const struct snd_pcm_hardware snd_cs4281_playback =
8351da177e4SLinus Torvalds {
836b83f346bSClemens Ladisch 	.info =			SNDRV_PCM_INFO_MMAP |
8371da177e4SLinus Torvalds 				SNDRV_PCM_INFO_INTERLEAVED |
8381da177e4SLinus Torvalds 				SNDRV_PCM_INFO_MMAP_VALID |
8391da177e4SLinus Torvalds 				SNDRV_PCM_INFO_PAUSE |
840b83f346bSClemens Ladisch 				SNDRV_PCM_INFO_RESUME,
8411da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
8421da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
8431da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
8441da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
8451da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE,
8461da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8471da177e4SLinus Torvalds 	.rate_min =		4000,
8481da177e4SLinus Torvalds 	.rate_max =		48000,
8491da177e4SLinus Torvalds 	.channels_min =		1,
8501da177e4SLinus Torvalds 	.channels_max =		2,
8511da177e4SLinus Torvalds 	.buffer_bytes_max =	(512*1024),
8521da177e4SLinus Torvalds 	.period_bytes_min =	64,
8531da177e4SLinus Torvalds 	.period_bytes_max =	(512*1024),
8541da177e4SLinus Torvalds 	.periods_min =		1,
8551da177e4SLinus Torvalds 	.periods_max =		2,
8561da177e4SLinus Torvalds 	.fifo_size =		CS4281_FIFO_SIZE,
8571da177e4SLinus Torvalds };
8581da177e4SLinus Torvalds 
859dee49895SBhumika Goyal static const struct snd_pcm_hardware snd_cs4281_capture =
8601da177e4SLinus Torvalds {
861b83f346bSClemens Ladisch 	.info =			SNDRV_PCM_INFO_MMAP |
8621da177e4SLinus Torvalds 				SNDRV_PCM_INFO_INTERLEAVED |
8631da177e4SLinus Torvalds 				SNDRV_PCM_INFO_MMAP_VALID |
8641da177e4SLinus Torvalds 				SNDRV_PCM_INFO_PAUSE |
865b83f346bSClemens Ladisch 				SNDRV_PCM_INFO_RESUME,
8661da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
8671da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
8681da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
8691da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
8701da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE,
8711da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8721da177e4SLinus Torvalds 	.rate_min =		4000,
8731da177e4SLinus Torvalds 	.rate_max =		48000,
8741da177e4SLinus Torvalds 	.channels_min =		1,
8751da177e4SLinus Torvalds 	.channels_max =		2,
8761da177e4SLinus Torvalds 	.buffer_bytes_max =	(512*1024),
8771da177e4SLinus Torvalds 	.period_bytes_min =	64,
8781da177e4SLinus Torvalds 	.period_bytes_max =	(512*1024),
8791da177e4SLinus Torvalds 	.periods_min =		1,
8801da177e4SLinus Torvalds 	.periods_max =		2,
8811da177e4SLinus Torvalds 	.fifo_size =		CS4281_FIFO_SIZE,
8821da177e4SLinus Torvalds };
8831da177e4SLinus Torvalds 
88493e35f95STakashi Iwai static int snd_cs4281_playback_open(struct snd_pcm_substream *substream)
8851da177e4SLinus Torvalds {
88693e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
88793e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
88893e35f95STakashi Iwai 	struct cs4281_dma *dma;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	dma = &chip->dma[0];
8911da177e4SLinus Torvalds 	dma->substream = substream;
8921da177e4SLinus Torvalds 	dma->left_slot = 0;
8931da177e4SLinus Torvalds 	dma->right_slot = 1;
8941da177e4SLinus Torvalds 	runtime->private_data = dma;
8951da177e4SLinus Torvalds 	runtime->hw = snd_cs4281_playback;
8961da177e4SLinus Torvalds 	/* should be detected from the AC'97 layer, but it seems
8971da177e4SLinus Torvalds 	   that although CS4297A rev B reports 18-bit ADC resolution,
8981da177e4SLinus Torvalds 	   samples are 20-bit */
8991da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 20);
9001da177e4SLinus Torvalds 	return 0;
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
90393e35f95STakashi Iwai static int snd_cs4281_capture_open(struct snd_pcm_substream *substream)
9041da177e4SLinus Torvalds {
90593e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
90693e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
90793e35f95STakashi Iwai 	struct cs4281_dma *dma;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	dma = &chip->dma[1];
9101da177e4SLinus Torvalds 	dma->substream = substream;
9111da177e4SLinus Torvalds 	dma->left_slot = 10;
9121da177e4SLinus Torvalds 	dma->right_slot = 11;
9131da177e4SLinus Torvalds 	runtime->private_data = dma;
9141da177e4SLinus Torvalds 	runtime->hw = snd_cs4281_capture;
9151da177e4SLinus Torvalds 	/* should be detected from the AC'97 layer, but it seems
9161da177e4SLinus Torvalds 	   that although CS4297A rev B reports 18-bit ADC resolution,
9171da177e4SLinus Torvalds 	   samples are 20-bit */
9181da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 20);
9191da177e4SLinus Torvalds 	return 0;
9201da177e4SLinus Torvalds }
9211da177e4SLinus Torvalds 
92293e35f95STakashi Iwai static int snd_cs4281_playback_close(struct snd_pcm_substream *substream)
9231da177e4SLinus Torvalds {
92493e35f95STakashi Iwai 	struct cs4281_dma *dma = substream->runtime->private_data;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	dma->substream = NULL;
9271da177e4SLinus Torvalds 	return 0;
9281da177e4SLinus Torvalds }
9291da177e4SLinus Torvalds 
93093e35f95STakashi Iwai static int snd_cs4281_capture_close(struct snd_pcm_substream *substream)
9311da177e4SLinus Torvalds {
93293e35f95STakashi Iwai 	struct cs4281_dma *dma = substream->runtime->private_data;
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	dma->substream = NULL;
9351da177e4SLinus Torvalds 	return 0;
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
9386769e988SJulia Lawall static const struct snd_pcm_ops snd_cs4281_playback_ops = {
9391da177e4SLinus Torvalds 	.open =		snd_cs4281_playback_open,
9401da177e4SLinus Torvalds 	.close =	snd_cs4281_playback_close,
9411da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
9421da177e4SLinus Torvalds 	.hw_params =	snd_cs4281_hw_params,
9431da177e4SLinus Torvalds 	.hw_free =	snd_cs4281_hw_free,
9441da177e4SLinus Torvalds 	.prepare =	snd_cs4281_playback_prepare,
9451da177e4SLinus Torvalds 	.trigger =	snd_cs4281_trigger,
9461da177e4SLinus Torvalds 	.pointer =	snd_cs4281_pointer,
9471da177e4SLinus Torvalds };
9481da177e4SLinus Torvalds 
9496769e988SJulia Lawall static const struct snd_pcm_ops snd_cs4281_capture_ops = {
9501da177e4SLinus Torvalds 	.open =		snd_cs4281_capture_open,
9511da177e4SLinus Torvalds 	.close =	snd_cs4281_capture_close,
9521da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
9531da177e4SLinus Torvalds 	.hw_params =	snd_cs4281_hw_params,
9541da177e4SLinus Torvalds 	.hw_free =	snd_cs4281_hw_free,
9551da177e4SLinus Torvalds 	.prepare =	snd_cs4281_capture_prepare,
9561da177e4SLinus Torvalds 	.trigger =	snd_cs4281_trigger,
9571da177e4SLinus Torvalds 	.pointer =	snd_cs4281_pointer,
9581da177e4SLinus Torvalds };
9591da177e4SLinus Torvalds 
9603e4f4776SLars-Peter Clausen static int snd_cs4281_pcm(struct cs4281 *chip, int device)
9611da177e4SLinus Torvalds {
96293e35f95STakashi Iwai 	struct snd_pcm *pcm;
9631da177e4SLinus Torvalds 	int err;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	err = snd_pcm_new(chip->card, "CS4281", device, 1, 1, &pcm);
9661da177e4SLinus Torvalds 	if (err < 0)
9671da177e4SLinus Torvalds 		return err;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs4281_playback_ops);
9701da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs4281_capture_ops);
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	pcm->private_data = chip;
9731da177e4SLinus Torvalds 	pcm->info_flags = 0;
9741da177e4SLinus Torvalds 	strcpy(pcm->name, "CS4281");
9751da177e4SLinus Torvalds 	chip->pcm = pcm;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
978*6974f8adSTakashi Iwai 					      &chip->pci->dev,
979*6974f8adSTakashi Iwai 					      64*1024, 512*1024);
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	return 0;
9821da177e4SLinus Torvalds }
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds /*
9851da177e4SLinus Torvalds  *  Mixer section
9861da177e4SLinus Torvalds  */
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds #define CS_VOL_MASK	0x1f
9891da177e4SLinus Torvalds 
99093e35f95STakashi Iwai static int snd_cs4281_info_volume(struct snd_kcontrol *kcontrol,
99193e35f95STakashi Iwai 				  struct snd_ctl_elem_info *uinfo)
9921da177e4SLinus Torvalds {
9931da177e4SLinus Torvalds 	uinfo->type              = SNDRV_CTL_ELEM_TYPE_INTEGER;
9941da177e4SLinus Torvalds 	uinfo->count             = 2;
9951da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
9961da177e4SLinus Torvalds 	uinfo->value.integer.max = CS_VOL_MASK;
9971da177e4SLinus Torvalds 	return 0;
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
100093e35f95STakashi Iwai static int snd_cs4281_get_volume(struct snd_kcontrol *kcontrol,
100193e35f95STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
10021da177e4SLinus Torvalds {
100393e35f95STakashi Iwai 	struct cs4281 *chip = snd_kcontrol_chip(kcontrol);
10041da177e4SLinus Torvalds 	int regL = (kcontrol->private_value >> 16) & 0xffff;
10051da177e4SLinus Torvalds 	int regR = kcontrol->private_value & 0xffff;
10061da177e4SLinus Torvalds 	int volL, volR;
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	volL = CS_VOL_MASK - (snd_cs4281_peekBA0(chip, regL) & CS_VOL_MASK);
10091da177e4SLinus Torvalds 	volR = CS_VOL_MASK - (snd_cs4281_peekBA0(chip, regR) & CS_VOL_MASK);
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = volL;
10121da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = volR;
10131da177e4SLinus Torvalds 	return 0;
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
101693e35f95STakashi Iwai static int snd_cs4281_put_volume(struct snd_kcontrol *kcontrol,
101793e35f95STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
10181da177e4SLinus Torvalds {
101993e35f95STakashi Iwai 	struct cs4281 *chip = snd_kcontrol_chip(kcontrol);
10201da177e4SLinus Torvalds 	int change = 0;
10211da177e4SLinus Torvalds 	int regL = (kcontrol->private_value >> 16) & 0xffff;
10221da177e4SLinus Torvalds 	int regR = kcontrol->private_value & 0xffff;
10231da177e4SLinus Torvalds 	int volL, volR;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	volL = CS_VOL_MASK - (snd_cs4281_peekBA0(chip, regL) & CS_VOL_MASK);
10261da177e4SLinus Torvalds 	volR = CS_VOL_MASK - (snd_cs4281_peekBA0(chip, regR) & CS_VOL_MASK);
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	if (ucontrol->value.integer.value[0] != volL) {
10291da177e4SLinus Torvalds 		volL = CS_VOL_MASK - (ucontrol->value.integer.value[0] & CS_VOL_MASK);
10301da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, regL, volL);
10311da177e4SLinus Torvalds 		change = 1;
10321da177e4SLinus Torvalds 	}
1033e860f000STakashi Iwai 	if (ucontrol->value.integer.value[1] != volR) {
10341da177e4SLinus Torvalds 		volR = CS_VOL_MASK - (ucontrol->value.integer.value[1] & CS_VOL_MASK);
10351da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, regR, volR);
10361da177e4SLinus Torvalds 		change = 1;
10371da177e4SLinus Torvalds 	}
10381da177e4SLinus Torvalds 	return change;
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds 
10410cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -4650, 150, 0);
10429f6ab250STakashi Iwai 
1043f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_cs4281_fm_vol =
10441da177e4SLinus Torvalds {
10451da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
10461da177e4SLinus Torvalds 	.name = "Synth Playback Volume",
10471da177e4SLinus Torvalds 	.info = snd_cs4281_info_volume,
10481da177e4SLinus Torvalds 	.get = snd_cs4281_get_volume,
10491da177e4SLinus Torvalds 	.put = snd_cs4281_put_volume,
10501da177e4SLinus Torvalds 	.private_value = ((BA0_FMLVC << 16) | BA0_FMRVC),
10519f6ab250STakashi Iwai 	.tlv = { .p = db_scale_dsp },
10521da177e4SLinus Torvalds };
10531da177e4SLinus Torvalds 
1054f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_cs4281_pcm_vol =
10551da177e4SLinus Torvalds {
10561da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
10571da177e4SLinus Torvalds 	.name = "PCM Stream Playback Volume",
10581da177e4SLinus Torvalds 	.info = snd_cs4281_info_volume,
10591da177e4SLinus Torvalds 	.get = snd_cs4281_get_volume,
10601da177e4SLinus Torvalds 	.put = snd_cs4281_put_volume,
10611da177e4SLinus Torvalds 	.private_value = ((BA0_PPLVC << 16) | BA0_PPRVC),
10629f6ab250STakashi Iwai 	.tlv = { .p = db_scale_dsp },
10631da177e4SLinus Torvalds };
10641da177e4SLinus Torvalds 
106593e35f95STakashi Iwai static void snd_cs4281_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
10661da177e4SLinus Torvalds {
106793e35f95STakashi Iwai 	struct cs4281 *chip = bus->private_data;
10681da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
10691da177e4SLinus Torvalds }
10701da177e4SLinus Torvalds 
107193e35f95STakashi Iwai static void snd_cs4281_mixer_free_ac97(struct snd_ac97 *ac97)
10721da177e4SLinus Torvalds {
107393e35f95STakashi Iwai 	struct cs4281 *chip = ac97->private_data;
10741da177e4SLinus Torvalds 	if (ac97->num)
10751da177e4SLinus Torvalds 		chip->ac97_secondary = NULL;
10761da177e4SLinus Torvalds 	else
10771da177e4SLinus Torvalds 		chip->ac97 = NULL;
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
1080e23e7a14SBill Pemberton static int snd_cs4281_mixer(struct cs4281 *chip)
10811da177e4SLinus Torvalds {
108293e35f95STakashi Iwai 	struct snd_card *card = chip->card;
108393e35f95STakashi Iwai 	struct snd_ac97_template ac97;
10841da177e4SLinus Torvalds 	int err;
108593e35f95STakashi Iwai 	static struct snd_ac97_bus_ops ops = {
10861da177e4SLinus Torvalds 		.write = snd_cs4281_ac97_write,
10871da177e4SLinus Torvalds 		.read = snd_cs4281_ac97_read,
10881da177e4SLinus Torvalds 	};
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus)) < 0)
10911da177e4SLinus Torvalds 		return err;
10921da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_cs4281_mixer_free_ac97_bus;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
10951da177e4SLinus Torvalds 	ac97.private_data = chip;
10961da177e4SLinus Torvalds 	ac97.private_free = snd_cs4281_mixer_free_ac97;
10971da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
10981da177e4SLinus Torvalds 		return err;
10991da177e4SLinus Torvalds 	if (chip->dual_codec) {
11001da177e4SLinus Torvalds 		ac97.num = 1;
11011da177e4SLinus Torvalds 		if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_secondary)) < 0)
11021da177e4SLinus Torvalds 			return err;
11031da177e4SLinus Torvalds 	}
11041da177e4SLinus Torvalds 	if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4281_fm_vol, chip))) < 0)
11051da177e4SLinus Torvalds 		return err;
11061da177e4SLinus Torvalds 	if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4281_pcm_vol, chip))) < 0)
11071da177e4SLinus Torvalds 		return err;
11081da177e4SLinus Torvalds 	return 0;
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds /*
11131da177e4SLinus Torvalds  * proc interface
11141da177e4SLinus Torvalds  */
11151da177e4SLinus Torvalds 
111693e35f95STakashi Iwai static void snd_cs4281_proc_read(struct snd_info_entry *entry,
111793e35f95STakashi Iwai 				  struct snd_info_buffer *buffer)
11181da177e4SLinus Torvalds {
111993e35f95STakashi Iwai 	struct cs4281 *chip = entry->private_data;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	snd_iprintf(buffer, "Cirrus Logic CS4281\n\n");
11221da177e4SLinus Torvalds 	snd_iprintf(buffer, "Spurious half IRQs   : %u\n", chip->spurious_dhtc_irq);
11231da177e4SLinus Torvalds 	snd_iprintf(buffer, "Spurious end IRQs    : %u\n", chip->spurious_dtc_irq);
11241da177e4SLinus Torvalds }
11251da177e4SLinus Torvalds 
112624e4a121STakashi Iwai static ssize_t snd_cs4281_BA0_read(struct snd_info_entry *entry,
112793e35f95STakashi Iwai 				   void *file_private_data,
11281da177e4SLinus Torvalds 				   struct file *file, char __user *buf,
112924e4a121STakashi Iwai 				   size_t count, loff_t pos)
11301da177e4SLinus Torvalds {
113193e35f95STakashi Iwai 	struct cs4281 *chip = entry->private_data;
11321da177e4SLinus Torvalds 
1133d97e1b78STakashi Iwai 	if (copy_to_user_fromio(buf, chip->ba0 + pos, count))
11341da177e4SLinus Torvalds 		return -EFAULT;
1135d97e1b78STakashi Iwai 	return count;
11361da177e4SLinus Torvalds }
11371da177e4SLinus Torvalds 
113824e4a121STakashi Iwai static ssize_t snd_cs4281_BA1_read(struct snd_info_entry *entry,
113993e35f95STakashi Iwai 				   void *file_private_data,
11401da177e4SLinus Torvalds 				   struct file *file, char __user *buf,
114124e4a121STakashi Iwai 				   size_t count, loff_t pos)
11421da177e4SLinus Torvalds {
114393e35f95STakashi Iwai 	struct cs4281 *chip = entry->private_data;
11441da177e4SLinus Torvalds 
1145d97e1b78STakashi Iwai 	if (copy_to_user_fromio(buf, chip->ba1 + pos, count))
11461da177e4SLinus Torvalds 		return -EFAULT;
1147d97e1b78STakashi Iwai 	return count;
11481da177e4SLinus Torvalds }
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds static struct snd_info_entry_ops snd_cs4281_proc_ops_BA0 = {
11511da177e4SLinus Torvalds 	.read = snd_cs4281_BA0_read,
11521da177e4SLinus Torvalds };
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds static struct snd_info_entry_ops snd_cs4281_proc_ops_BA1 = {
11551da177e4SLinus Torvalds 	.read = snd_cs4281_BA1_read,
11561da177e4SLinus Torvalds };
11571da177e4SLinus Torvalds 
1158e23e7a14SBill Pemberton static void snd_cs4281_proc_init(struct cs4281 *chip)
11591da177e4SLinus Torvalds {
116093e35f95STakashi Iwai 	struct snd_info_entry *entry;
11611da177e4SLinus Torvalds 
116247f2769bSTakashi Iwai 	snd_card_ro_proc_new(chip->card, "cs4281", chip, snd_cs4281_proc_read);
11631da177e4SLinus Torvalds 	if (! snd_card_proc_new(chip->card, "cs4281_BA0", &entry)) {
11641da177e4SLinus Torvalds 		entry->content = SNDRV_INFO_CONTENT_DATA;
11651da177e4SLinus Torvalds 		entry->private_data = chip;
11661da177e4SLinus Torvalds 		entry->c.ops = &snd_cs4281_proc_ops_BA0;
11671da177e4SLinus Torvalds 		entry->size = CS4281_BA0_SIZE;
11681da177e4SLinus Torvalds 	}
11691da177e4SLinus Torvalds 	if (! snd_card_proc_new(chip->card, "cs4281_BA1", &entry)) {
11701da177e4SLinus Torvalds 		entry->content = SNDRV_INFO_CONTENT_DATA;
11711da177e4SLinus Torvalds 		entry->private_data = chip;
11721da177e4SLinus Torvalds 		entry->c.ops = &snd_cs4281_proc_ops_BA1;
11731da177e4SLinus Torvalds 		entry->size = CS4281_BA1_SIZE;
11741da177e4SLinus Torvalds 	}
11751da177e4SLinus Torvalds }
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds /*
11781da177e4SLinus Torvalds  * joystick support
11791da177e4SLinus Torvalds  */
11801da177e4SLinus Torvalds 
1181b2fac073SFabian Frederick #if IS_REACHABLE(CONFIG_GAMEPORT)
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds static void snd_cs4281_gameport_trigger(struct gameport *gameport)
11841da177e4SLinus Torvalds {
118593e35f95STakashi Iwai 	struct cs4281 *chip = gameport_get_port_data(gameport);
11861da177e4SLinus Torvalds 
1187da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
1188da3cec35STakashi Iwai 		return;
11891da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_JSPT, 0xff);
11901da177e4SLinus Torvalds }
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds static unsigned char snd_cs4281_gameport_read(struct gameport *gameport)
11931da177e4SLinus Torvalds {
119493e35f95STakashi Iwai 	struct cs4281 *chip = gameport_get_port_data(gameport);
11951da177e4SLinus Torvalds 
1196da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
1197da3cec35STakashi Iwai 		return 0;
11981da177e4SLinus Torvalds 	return snd_cs4281_peekBA0(chip, BA0_JSPT);
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds #ifdef COOKED_MODE
120293e35f95STakashi Iwai static int snd_cs4281_gameport_cooked_read(struct gameport *gameport,
120393e35f95STakashi Iwai 					   int *axes, int *buttons)
12041da177e4SLinus Torvalds {
120593e35f95STakashi Iwai 	struct cs4281 *chip = gameport_get_port_data(gameport);
12061da177e4SLinus Torvalds 	unsigned js1, js2, jst;
12071da177e4SLinus Torvalds 
1208da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
1209da3cec35STakashi Iwai 		return 0;
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	js1 = snd_cs4281_peekBA0(chip, BA0_JSC1);
12121da177e4SLinus Torvalds 	js2 = snd_cs4281_peekBA0(chip, BA0_JSC2);
12131da177e4SLinus Torvalds 	jst = snd_cs4281_peekBA0(chip, BA0_JSPT);
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	*buttons = (~jst >> 4) & 0x0F;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	axes[0] = ((js1 & JSC1_Y1V_MASK) >> JSC1_Y1V_SHIFT) & 0xFFFF;
12181da177e4SLinus Torvalds 	axes[1] = ((js1 & JSC1_X1V_MASK) >> JSC1_X1V_SHIFT) & 0xFFFF;
12191da177e4SLinus Torvalds 	axes[2] = ((js2 & JSC2_Y2V_MASK) >> JSC2_Y2V_SHIFT) & 0xFFFF;
12201da177e4SLinus Torvalds 	axes[3] = ((js2 & JSC2_X2V_MASK) >> JSC2_X2V_SHIFT) & 0xFFFF;
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	for (jst = 0; jst < 4; ++jst)
12231da177e4SLinus Torvalds 		if (axes[jst] == 0xFFFF) axes[jst] = -1;
12241da177e4SLinus Torvalds 	return 0;
12251da177e4SLinus Torvalds }
12261da177e4SLinus Torvalds #else
12271da177e4SLinus Torvalds #define snd_cs4281_gameport_cooked_read	NULL
12281da177e4SLinus Torvalds #endif
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds static int snd_cs4281_gameport_open(struct gameport *gameport, int mode)
12311da177e4SLinus Torvalds {
12321da177e4SLinus Torvalds 	switch (mode) {
12331da177e4SLinus Torvalds #ifdef COOKED_MODE
12341da177e4SLinus Torvalds 	case GAMEPORT_MODE_COOKED:
12351da177e4SLinus Torvalds 		return 0;
12361da177e4SLinus Torvalds #endif
12371da177e4SLinus Torvalds 	case GAMEPORT_MODE_RAW:
12381da177e4SLinus Torvalds 		return 0;
12391da177e4SLinus Torvalds 	default:
12401da177e4SLinus Torvalds 		return -1;
12411da177e4SLinus Torvalds 	}
12421da177e4SLinus Torvalds 	return 0;
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds 
1245e23e7a14SBill Pemberton static int snd_cs4281_create_gameport(struct cs4281 *chip)
12461da177e4SLinus Torvalds {
12471da177e4SLinus Torvalds 	struct gameport *gp;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	chip->gameport = gp = gameport_allocate_port();
12501da177e4SLinus Torvalds 	if (!gp) {
1251b055e7b4STakashi Iwai 		dev_err(chip->card->dev,
1252b055e7b4STakashi Iwai 			"cannot allocate memory for gameport\n");
12531da177e4SLinus Torvalds 		return -ENOMEM;
12541da177e4SLinus Torvalds 	}
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	gameport_set_name(gp, "CS4281 Gameport");
12571da177e4SLinus Torvalds 	gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
12581da177e4SLinus Torvalds 	gameport_set_dev_parent(gp, &chip->pci->dev);
12591da177e4SLinus Torvalds 	gp->open = snd_cs4281_gameport_open;
12601da177e4SLinus Torvalds 	gp->read = snd_cs4281_gameport_read;
12611da177e4SLinus Torvalds 	gp->trigger = snd_cs4281_gameport_trigger;
12621da177e4SLinus Torvalds 	gp->cooked_read = snd_cs4281_gameport_cooked_read;
12631da177e4SLinus Torvalds 	gameport_set_port_data(gp, chip);
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_JSIO, 0xFF); // ?
12661da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_JSCTL, JSCTL_SP_MEDIUM_SLOW);
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 	gameport_register_port(gp);
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	return 0;
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
127393e35f95STakashi Iwai static void snd_cs4281_free_gameport(struct cs4281 *chip)
12741da177e4SLinus Torvalds {
12751da177e4SLinus Torvalds 	if (chip->gameport) {
12761da177e4SLinus Torvalds 		gameport_unregister_port(chip->gameport);
12771da177e4SLinus Torvalds 		chip->gameport = NULL;
12781da177e4SLinus Torvalds 	}
12791da177e4SLinus Torvalds }
12801da177e4SLinus Torvalds #else
128193e35f95STakashi Iwai static inline int snd_cs4281_create_gameport(struct cs4281 *chip) { return -ENOSYS; }
128293e35f95STakashi Iwai static inline void snd_cs4281_free_gameport(struct cs4281 *chip) { }
128366701170STakashi Iwai #endif /* IS_REACHABLE(CONFIG_GAMEPORT) */
12841da177e4SLinus Torvalds 
128593e35f95STakashi Iwai static int snd_cs4281_free(struct cs4281 *chip)
12861da177e4SLinus Torvalds {
12871da177e4SLinus Torvalds 	snd_cs4281_free_gameport(chip);
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 	if (chip->irq >= 0)
12901da177e4SLinus Torvalds 		synchronize_irq(chip->irq);
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	/* Mask interrupts */
12931da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HIMR, 0x7fffffff);
12941da177e4SLinus Torvalds 	/* Stop the DLL Clock logic. */
12951da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, 0);
12961da177e4SLinus Torvalds 	/* Sound System Power Management - Turn Everything OFF */
12971da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SSPM, 0);
12981da177e4SLinus Torvalds 	/* PCI interface - D3 state */
1299db10e7fbSYijing Wang 	pci_set_power_state(chip->pci, PCI_D3hot);
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	if (chip->irq >= 0)
130293e35f95STakashi Iwai 		free_irq(chip->irq, chip);
13031da177e4SLinus Torvalds 	iounmap(chip->ba0);
13041da177e4SLinus Torvalds 	iounmap(chip->ba1);
13051da177e4SLinus Torvalds 	pci_release_regions(chip->pci);
13061da177e4SLinus Torvalds 	pci_disable_device(chip->pci);
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	kfree(chip);
13091da177e4SLinus Torvalds 	return 0;
13101da177e4SLinus Torvalds }
13111da177e4SLinus Torvalds 
131293e35f95STakashi Iwai static int snd_cs4281_dev_free(struct snd_device *device)
13131da177e4SLinus Torvalds {
131493e35f95STakashi Iwai 	struct cs4281 *chip = device->device_data;
13151da177e4SLinus Torvalds 	return snd_cs4281_free(chip);
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
131893e35f95STakashi Iwai static int snd_cs4281_chip_init(struct cs4281 *chip); /* defined below */
13191da177e4SLinus Torvalds 
1320e23e7a14SBill Pemberton static int snd_cs4281_create(struct snd_card *card,
13211da177e4SLinus Torvalds 			     struct pci_dev *pci,
132293e35f95STakashi Iwai 			     struct cs4281 **rchip,
13231da177e4SLinus Torvalds 			     int dual_codec)
13241da177e4SLinus Torvalds {
132593e35f95STakashi Iwai 	struct cs4281 *chip;
13261da177e4SLinus Torvalds 	unsigned int tmp;
13271da177e4SLinus Torvalds 	int err;
132893e35f95STakashi Iwai 	static struct snd_device_ops ops = {
13291da177e4SLinus Torvalds 		.dev_free =	snd_cs4281_dev_free,
13301da177e4SLinus Torvalds 	};
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 	*rchip = NULL;
13331da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
13341da177e4SLinus Torvalds 		return err;
1335e560d8d8STakashi Iwai 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
13361da177e4SLinus Torvalds 	if (chip == NULL) {
13371da177e4SLinus Torvalds 		pci_disable_device(pci);
13381da177e4SLinus Torvalds 		return -ENOMEM;
13391da177e4SLinus Torvalds 	}
13401da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
13411da177e4SLinus Torvalds 	chip->card = card;
13421da177e4SLinus Torvalds 	chip->pci = pci;
13431da177e4SLinus Torvalds 	chip->irq = -1;
13441da177e4SLinus Torvalds 	pci_set_master(pci);
13451da177e4SLinus Torvalds 	if (dual_codec < 0 || dual_codec > 3) {
1346b055e7b4STakashi Iwai 		dev_err(card->dev, "invalid dual_codec option %d\n", dual_codec);
13471da177e4SLinus Torvalds 		dual_codec = 0;
13481da177e4SLinus Torvalds 	}
13491da177e4SLinus Torvalds 	chip->dual_codec = dual_codec;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, "CS4281")) < 0) {
13521da177e4SLinus Torvalds 		kfree(chip);
13531da177e4SLinus Torvalds 		pci_disable_device(pci);
13541da177e4SLinus Torvalds 		return err;
13551da177e4SLinus Torvalds 	}
13561da177e4SLinus Torvalds 	chip->ba0_addr = pci_resource_start(pci, 0);
13571da177e4SLinus Torvalds 	chip->ba1_addr = pci_resource_start(pci, 1);
13581da177e4SLinus Torvalds 
13592f5ad54eSArjan van de Ven 	chip->ba0 = pci_ioremap_bar(pci, 0);
13602f5ad54eSArjan van de Ven 	chip->ba1 = pci_ioremap_bar(pci, 1);
1361688956f2STakashi Iwai 	if (!chip->ba0 || !chip->ba1) {
1362688956f2STakashi Iwai 		snd_cs4281_free(chip);
1363688956f2STakashi Iwai 		return -ENOMEM;
1364688956f2STakashi Iwai 	}
1365688956f2STakashi Iwai 
1366437a5a46STakashi Iwai 	if (request_irq(pci->irq, snd_cs4281_interrupt, IRQF_SHARED,
1367934c2b6dSTakashi Iwai 			KBUILD_MODNAME, chip)) {
1368b055e7b4STakashi Iwai 		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
13691da177e4SLinus Torvalds 		snd_cs4281_free(chip);
13701da177e4SLinus Torvalds 		return -ENOMEM;
13711da177e4SLinus Torvalds 	}
13721da177e4SLinus Torvalds 	chip->irq = pci->irq;
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	tmp = snd_cs4281_chip_init(chip);
13751da177e4SLinus Torvalds 	if (tmp) {
13761da177e4SLinus Torvalds 		snd_cs4281_free(chip);
13771da177e4SLinus Torvalds 		return tmp;
13781da177e4SLinus Torvalds 	}
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
13811da177e4SLinus Torvalds 		snd_cs4281_free(chip);
13821da177e4SLinus Torvalds 		return err;
13831da177e4SLinus Torvalds 	}
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	snd_cs4281_proc_init(chip);
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	*rchip = chip;
13881da177e4SLinus Torvalds 	return 0;
13891da177e4SLinus Torvalds }
13901da177e4SLinus Torvalds 
139193e35f95STakashi Iwai static int snd_cs4281_chip_init(struct cs4281 *chip)
13921da177e4SLinus Torvalds {
13931da177e4SLinus Torvalds 	unsigned int tmp;
139438223daaSTakashi Iwai 	unsigned long end_time;
13951da177e4SLinus Torvalds 	int retry_count = 2;
13961da177e4SLinus Torvalds 
1397a488e033SArnaud Patard 	/* Having EPPMC.FPDN=1 prevent proper chip initialisation */
1398a488e033SArnaud Patard 	tmp = snd_cs4281_peekBA0(chip, BA0_EPPMC);
1399a488e033SArnaud Patard 	if (tmp & BA0_EPPMC_FPDN)
1400a488e033SArnaud Patard 		snd_cs4281_pokeBA0(chip, BA0_EPPMC, tmp & ~BA0_EPPMC_FPDN);
1401a488e033SArnaud Patard 
14021da177e4SLinus Torvalds       __retry:
14031da177e4SLinus Torvalds 	tmp = snd_cs4281_peekBA0(chip, BA0_CFLR);
14041da177e4SLinus Torvalds 	if (tmp != BA0_CFLR_DEFAULT) {
14051da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_CFLR, BA0_CFLR_DEFAULT);
14061da177e4SLinus Torvalds 		tmp = snd_cs4281_peekBA0(chip, BA0_CFLR);
14071da177e4SLinus Torvalds 		if (tmp != BA0_CFLR_DEFAULT) {
1408b055e7b4STakashi Iwai 			dev_err(chip->card->dev,
1409b055e7b4STakashi Iwai 				"CFLR setup failed (0x%x)\n", tmp);
14101da177e4SLinus Torvalds 			return -EIO;
14111da177e4SLinus Torvalds 		}
14121da177e4SLinus Torvalds 	}
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	/* Set the 'Configuration Write Protect' register
14151da177e4SLinus Torvalds 	 * to 4281h.  Allows vendor-defined configuration
14161da177e4SLinus Torvalds          * space between 0e4h and 0ffh to be written. */
14171da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CWPR, 0x4281);
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 	if ((tmp = snd_cs4281_peekBA0(chip, BA0_SERC1)) != (BA0_SERC1_SO1EN | BA0_SERC1_AC97)) {
1420b055e7b4STakashi Iwai 		dev_err(chip->card->dev,
1421b055e7b4STakashi Iwai 			"SERC1 AC'97 check failed (0x%x)\n", tmp);
14221da177e4SLinus Torvalds 		return -EIO;
14231da177e4SLinus Torvalds 	}
14241da177e4SLinus Torvalds 	if ((tmp = snd_cs4281_peekBA0(chip, BA0_SERC2)) != (BA0_SERC2_SI1EN | BA0_SERC2_AC97)) {
1425b055e7b4STakashi Iwai 		dev_err(chip->card->dev,
1426b055e7b4STakashi Iwai 			"SERC2 AC'97 check failed (0x%x)\n", tmp);
14271da177e4SLinus Torvalds 		return -EIO;
14281da177e4SLinus Torvalds 	}
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 	/* Sound System Power Management */
14311da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SSPM, BA0_SSPM_MIXEN | BA0_SSPM_CSRCEN |
14321da177e4SLinus Torvalds 				           BA0_SSPM_PSRCEN | BA0_SSPM_JSEN |
14331da177e4SLinus Torvalds 				           BA0_SSPM_ACLEN | BA0_SSPM_FMEN);
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	/* Serial Port Power Management */
14361da177e4SLinus Torvalds  	/* Blast the clock control register to zero so that the
14371da177e4SLinus Torvalds          * PLL starts out in a known state, and blast the master serial
14381da177e4SLinus Torvalds          * port control register to zero so that the serial ports also
14391da177e4SLinus Torvalds          * start out in a known state. */
14401da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, 0);
14411da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SERMC, 0);
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds         /* Make ESYN go to zero to turn off
14441da177e4SLinus Torvalds          * the Sync pulse on the AC97 link. */
14451da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, 0);
14461da177e4SLinus Torvalds 	udelay(50);
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	/*  Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
14491da177e4SLinus Torvalds 	 *  spec) and then drive it high.  This is done for non AC97 modes since
14501da177e4SLinus Torvalds 	 *  there might be logic external to the CS4281 that uses the ARST# line
14511da177e4SLinus Torvalds 	 *  for a reset. */
14521da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SPMC, 0);
14531da177e4SLinus Torvalds 	udelay(50);
14541da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SPMC, BA0_SPMC_RSTN);
1455c9a49bb1STakashi Iwai 	msleep(50);
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	if (chip->dual_codec)
14581da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_SPMC, BA0_SPMC_RSTN | BA0_SPMC_ASDI2E);
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds 	/*
14611da177e4SLinus Torvalds 	 *  Set the serial port timing configuration.
14621da177e4SLinus Torvalds 	 */
14631da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SERMC,
14641da177e4SLinus Torvalds 			   (chip->dual_codec ? BA0_SERMC_TCID(chip->dual_codec) : BA0_SERMC_TCID(1)) |
14651da177e4SLinus Torvalds 			   BA0_SERMC_PTC_AC97 | BA0_SERMC_MSPE);
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	/*
14681da177e4SLinus Torvalds 	 *  Start the DLL Clock logic.
14691da177e4SLinus Torvalds 	 */
14701da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, BA0_CLKCR1_DLLP);
1471c9a49bb1STakashi Iwai 	msleep(50);
14721da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, BA0_CLKCR1_SWCE | BA0_CLKCR1_DLLP);
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 	/*
14751da177e4SLinus Torvalds 	 * Wait for the DLL ready signal from the clock logic.
14761da177e4SLinus Torvalds 	 */
147738223daaSTakashi Iwai 	end_time = jiffies + HZ;
14781da177e4SLinus Torvalds 	do {
14791da177e4SLinus Torvalds 		/*
14801da177e4SLinus Torvalds 		 *  Read the AC97 status register to see if we've seen a CODEC
14811da177e4SLinus Torvalds 		 *  signal from the AC97 codec.
14821da177e4SLinus Torvalds 		 */
14831da177e4SLinus Torvalds 		if (snd_cs4281_peekBA0(chip, BA0_CLKCR1) & BA0_CLKCR1_DLLRDY)
14841da177e4SLinus Torvalds 			goto __ok0;
148538223daaSTakashi Iwai 		schedule_timeout_uninterruptible(1);
148638223daaSTakashi Iwai 	} while (time_after_eq(end_time, jiffies));
14871da177e4SLinus Torvalds 
1488b055e7b4STakashi Iwai 	dev_err(chip->card->dev, "DLLRDY not seen\n");
14891da177e4SLinus Torvalds 	return -EIO;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds       __ok0:
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 	/*
14941da177e4SLinus Torvalds 	 *  The first thing we do here is to enable sync generation.  As soon
14951da177e4SLinus Torvalds 	 *  as we start receiving bit clock, we'll start producing the SYNC
14961da177e4SLinus Torvalds 	 *  signal.
14971da177e4SLinus Torvalds 	 */
14981da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, BA0_ACCTL_ESYN);
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 	/*
15011da177e4SLinus Torvalds 	 * Wait for the codec ready signal from the AC97 codec.
15021da177e4SLinus Torvalds 	 */
150338223daaSTakashi Iwai 	end_time = jiffies + HZ;
15041da177e4SLinus Torvalds 	do {
15051da177e4SLinus Torvalds 		/*
15061da177e4SLinus Torvalds 		 *  Read the AC97 status register to see if we've seen a CODEC
15071da177e4SLinus Torvalds 		 *  signal from the AC97 codec.
15081da177e4SLinus Torvalds 		 */
15091da177e4SLinus Torvalds 		if (snd_cs4281_peekBA0(chip, BA0_ACSTS) & BA0_ACSTS_CRDY)
15101da177e4SLinus Torvalds 			goto __ok1;
151138223daaSTakashi Iwai 		schedule_timeout_uninterruptible(1);
151238223daaSTakashi Iwai 	} while (time_after_eq(end_time, jiffies));
15131da177e4SLinus Torvalds 
1514b055e7b4STakashi Iwai 	dev_err(chip->card->dev,
1515b055e7b4STakashi Iwai 		"never read codec ready from AC'97 (0x%x)\n",
1516b055e7b4STakashi Iwai 		snd_cs4281_peekBA0(chip, BA0_ACSTS));
15171da177e4SLinus Torvalds 	return -EIO;
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds       __ok1:
15201da177e4SLinus Torvalds 	if (chip->dual_codec) {
152138223daaSTakashi Iwai 		end_time = jiffies + HZ;
15221da177e4SLinus Torvalds 		do {
15231da177e4SLinus Torvalds 			if (snd_cs4281_peekBA0(chip, BA0_ACSTS2) & BA0_ACSTS_CRDY)
15241da177e4SLinus Torvalds 				goto __codec2_ok;
152538223daaSTakashi Iwai 			schedule_timeout_uninterruptible(1);
152638223daaSTakashi Iwai 		} while (time_after_eq(end_time, jiffies));
1527b055e7b4STakashi Iwai 		dev_info(chip->card->dev,
1528b055e7b4STakashi Iwai 			 "secondary codec doesn't respond. disable it...\n");
15291da177e4SLinus Torvalds 		chip->dual_codec = 0;
15301da177e4SLinus Torvalds 	__codec2_ok: ;
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 	/*
15341da177e4SLinus Torvalds 	 *  Assert the valid frame signal so that we can start sending commands
15351da177e4SLinus Torvalds 	 *  to the AC97 codec.
15361da177e4SLinus Torvalds 	 */
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, BA0_ACCTL_VFRM | BA0_ACCTL_ESYN);
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds 	/*
15411da177e4SLinus Torvalds 	 *  Wait until we've sampled input slots 3 and 4 as valid, meaning that
15421da177e4SLinus Torvalds 	 *  the codec is pumping ADC data across the AC-link.
15431da177e4SLinus Torvalds 	 */
15441da177e4SLinus Torvalds 
154538223daaSTakashi Iwai 	end_time = jiffies + HZ;
15461da177e4SLinus Torvalds 	do {
15471da177e4SLinus Torvalds 		/*
15481da177e4SLinus Torvalds 		 *  Read the input slot valid register and see if input slots 3
15491da177e4SLinus Torvalds 		 *  4 are valid yet.
15501da177e4SLinus Torvalds 		 */
15511da177e4SLinus Torvalds                 if ((snd_cs4281_peekBA0(chip, BA0_ACISV) & (BA0_ACISV_SLV(3) | BA0_ACISV_SLV(4))) == (BA0_ACISV_SLV(3) | BA0_ACISV_SLV(4)))
15521da177e4SLinus Torvalds                         goto __ok2;
155338223daaSTakashi Iwai 		schedule_timeout_uninterruptible(1);
155438223daaSTakashi Iwai 	} while (time_after_eq(end_time, jiffies));
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	if (--retry_count > 0)
15571da177e4SLinus Torvalds 		goto __retry;
1558b055e7b4STakashi Iwai 	dev_err(chip->card->dev, "never read ISV3 and ISV4 from AC'97\n");
15591da177e4SLinus Torvalds 	return -EIO;
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds       __ok2:
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 	/*
15641da177e4SLinus Torvalds 	 *  Now, assert valid frame and the slot 3 and 4 valid bits.  This will
15651da177e4SLinus Torvalds 	 *  commense the transfer of digital audio data to the AC97 codec.
15661da177e4SLinus Torvalds 	 */
15671da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACOSV, BA0_ACOSV_SLV(3) | BA0_ACOSV_SLV(4));
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	/*
15701da177e4SLinus Torvalds 	 *  Initialize DMA structures
15711da177e4SLinus Torvalds 	 */
15721da177e4SLinus Torvalds 	for (tmp = 0; tmp < 4; tmp++) {
157393e35f95STakashi Iwai 		struct cs4281_dma *dma = &chip->dma[tmp];
15741da177e4SLinus Torvalds 		dma->regDBA = BA0_DBA0 + (tmp * 0x10);
15751da177e4SLinus Torvalds 		dma->regDCA = BA0_DCA0 + (tmp * 0x10);
15761da177e4SLinus Torvalds 		dma->regDBC = BA0_DBC0 + (tmp * 0x10);
15771da177e4SLinus Torvalds 		dma->regDCC = BA0_DCC0 + (tmp * 0x10);
15781da177e4SLinus Torvalds 		dma->regDMR = BA0_DMR0 + (tmp * 8);
15791da177e4SLinus Torvalds 		dma->regDCR = BA0_DCR0 + (tmp * 8);
15801da177e4SLinus Torvalds 		dma->regHDSR = BA0_HDSR0 + (tmp * 4);
15811da177e4SLinus Torvalds 		dma->regFCR = BA0_FCR0 + (tmp * 4);
15821da177e4SLinus Torvalds 		dma->regFSIC = BA0_FSIC0 + (tmp * 4);
15831da177e4SLinus Torvalds 		dma->fifo_offset = tmp * CS4281_FIFO_SIZE;
15841da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, dma->regFCR,
15851da177e4SLinus Torvalds 				   BA0_FCR_LS(31) |
15861da177e4SLinus Torvalds 				   BA0_FCR_RS(31) |
15871da177e4SLinus Torvalds 				   BA0_FCR_SZ(CS4281_FIFO_SIZE) |
15881da177e4SLinus Torvalds 				   BA0_FCR_OF(dma->fifo_offset));
15891da177e4SLinus Torvalds 	}
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	chip->src_left_play_slot = 0;	/* AC'97 left PCM playback (3) */
15921da177e4SLinus Torvalds 	chip->src_right_play_slot = 1;	/* AC'97 right PCM playback (4) */
15931da177e4SLinus Torvalds 	chip->src_left_rec_slot = 10;	/* AC'97 left PCM record (3) */
15941da177e4SLinus Torvalds 	chip->src_right_rec_slot = 11;	/* AC'97 right PCM record (4) */
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 	/* Activate wave playback FIFO for FM playback */
15971da177e4SLinus Torvalds 	chip->dma[0].valFCR = BA0_FCR_FEN | BA0_FCR_LS(0) |
15981da177e4SLinus Torvalds 		              BA0_FCR_RS(1) |
15991da177e4SLinus Torvalds  	  	              BA0_FCR_SZ(CS4281_FIFO_SIZE) |
16001da177e4SLinus Torvalds 		              BA0_FCR_OF(chip->dma[0].fifo_offset);
16011da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, chip->dma[0].regFCR, chip->dma[0].valFCR);
16021da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SRCSA, (chip->src_left_play_slot << 0) |
16031da177e4SLinus Torvalds 					    (chip->src_right_play_slot << 8) |
16041da177e4SLinus Torvalds 					    (chip->src_left_rec_slot << 16) |
16051da177e4SLinus Torvalds 					    (chip->src_right_rec_slot << 24));
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 	/* Initialize digital volume */
16081da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_PPLVC, 0);
16091da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_PPRVC, 0);
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 	/* Enable IRQs */
16121da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HICR, BA0_HICR_EOI);
16131da177e4SLinus Torvalds 	/* Unmask interrupts */
16141da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HIMR, 0x7fffffff & ~(
16151da177e4SLinus Torvalds 					BA0_HISR_MIDI |
16161da177e4SLinus Torvalds 					BA0_HISR_DMAI |
16171da177e4SLinus Torvalds 					BA0_HISR_DMA(0) |
16181da177e4SLinus Torvalds 					BA0_HISR_DMA(1) |
16191da177e4SLinus Torvalds 					BA0_HISR_DMA(2) |
16201da177e4SLinus Torvalds 					BA0_HISR_DMA(3)));
16211da177e4SLinus Torvalds 	synchronize_irq(chip->irq);
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 	return 0;
16241da177e4SLinus Torvalds }
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds /*
16271da177e4SLinus Torvalds  *  MIDI section
16281da177e4SLinus Torvalds  */
16291da177e4SLinus Torvalds 
163093e35f95STakashi Iwai static void snd_cs4281_midi_reset(struct cs4281 *chip)
16311da177e4SLinus Torvalds {
16321da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr | BA0_MIDCR_MRST);
16331da177e4SLinus Torvalds 	udelay(100);
16341da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
163793e35f95STakashi Iwai static int snd_cs4281_midi_input_open(struct snd_rawmidi_substream *substream)
16381da177e4SLinus Torvalds {
163993e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
16421da177e4SLinus Torvalds  	chip->midcr |= BA0_MIDCR_RXE;
16431da177e4SLinus Torvalds 	chip->midi_input = substream;
16441da177e4SLinus Torvalds 	if (!(chip->uartm & CS4281_MODE_OUTPUT)) {
16451da177e4SLinus Torvalds 		snd_cs4281_midi_reset(chip);
16461da177e4SLinus Torvalds 	} else {
16471da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16481da177e4SLinus Torvalds 	}
16491da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
16501da177e4SLinus Torvalds 	return 0;
16511da177e4SLinus Torvalds }
16521da177e4SLinus Torvalds 
165393e35f95STakashi Iwai static int snd_cs4281_midi_input_close(struct snd_rawmidi_substream *substream)
16541da177e4SLinus Torvalds {
165593e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
16581da177e4SLinus Torvalds 	chip->midcr &= ~(BA0_MIDCR_RXE | BA0_MIDCR_RIE);
16591da177e4SLinus Torvalds 	chip->midi_input = NULL;
16601da177e4SLinus Torvalds 	if (!(chip->uartm & CS4281_MODE_OUTPUT)) {
16611da177e4SLinus Torvalds 		snd_cs4281_midi_reset(chip);
16621da177e4SLinus Torvalds 	} else {
16631da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16641da177e4SLinus Torvalds 	}
16651da177e4SLinus Torvalds 	chip->uartm &= ~CS4281_MODE_INPUT;
16661da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
16671da177e4SLinus Torvalds 	return 0;
16681da177e4SLinus Torvalds }
16691da177e4SLinus Torvalds 
167093e35f95STakashi Iwai static int snd_cs4281_midi_output_open(struct snd_rawmidi_substream *substream)
16711da177e4SLinus Torvalds {
167293e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
16751da177e4SLinus Torvalds 	chip->uartm |= CS4281_MODE_OUTPUT;
16761da177e4SLinus Torvalds 	chip->midcr |= BA0_MIDCR_TXE;
16771da177e4SLinus Torvalds 	chip->midi_output = substream;
16781da177e4SLinus Torvalds 	if (!(chip->uartm & CS4281_MODE_INPUT)) {
16791da177e4SLinus Torvalds 		snd_cs4281_midi_reset(chip);
16801da177e4SLinus Torvalds 	} else {
16811da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16821da177e4SLinus Torvalds 	}
16831da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
16841da177e4SLinus Torvalds 	return 0;
16851da177e4SLinus Torvalds }
16861da177e4SLinus Torvalds 
168793e35f95STakashi Iwai static int snd_cs4281_midi_output_close(struct snd_rawmidi_substream *substream)
16881da177e4SLinus Torvalds {
168993e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
16921da177e4SLinus Torvalds 	chip->midcr &= ~(BA0_MIDCR_TXE | BA0_MIDCR_TIE);
16931da177e4SLinus Torvalds 	chip->midi_output = NULL;
16941da177e4SLinus Torvalds 	if (!(chip->uartm & CS4281_MODE_INPUT)) {
16951da177e4SLinus Torvalds 		snd_cs4281_midi_reset(chip);
16961da177e4SLinus Torvalds 	} else {
16971da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16981da177e4SLinus Torvalds 	}
16991da177e4SLinus Torvalds 	chip->uartm &= ~CS4281_MODE_OUTPUT;
17001da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
17011da177e4SLinus Torvalds 	return 0;
17021da177e4SLinus Torvalds }
17031da177e4SLinus Torvalds 
170493e35f95STakashi Iwai static void snd_cs4281_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
17051da177e4SLinus Torvalds {
17061da177e4SLinus Torvalds 	unsigned long flags;
170793e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
17101da177e4SLinus Torvalds 	if (up) {
17111da177e4SLinus Torvalds 		if ((chip->midcr & BA0_MIDCR_RIE) == 0) {
17121da177e4SLinus Torvalds 			chip->midcr |= BA0_MIDCR_RIE;
17131da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
17141da177e4SLinus Torvalds 		}
17151da177e4SLinus Torvalds 	} else {
17161da177e4SLinus Torvalds 		if (chip->midcr & BA0_MIDCR_RIE) {
17171da177e4SLinus Torvalds 			chip->midcr &= ~BA0_MIDCR_RIE;
17181da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
17191da177e4SLinus Torvalds 		}
17201da177e4SLinus Torvalds 	}
17211da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
17221da177e4SLinus Torvalds }
17231da177e4SLinus Torvalds 
172493e35f95STakashi Iwai static void snd_cs4281_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
17251da177e4SLinus Torvalds {
17261da177e4SLinus Torvalds 	unsigned long flags;
172793e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
17281da177e4SLinus Torvalds 	unsigned char byte;
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
17311da177e4SLinus Torvalds 	if (up) {
17321da177e4SLinus Torvalds 		if ((chip->midcr & BA0_MIDCR_TIE) == 0) {
17331da177e4SLinus Torvalds 			chip->midcr |= BA0_MIDCR_TIE;
17341da177e4SLinus Torvalds 			/* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
17351da177e4SLinus Torvalds 			while ((chip->midcr & BA0_MIDCR_TIE) &&
17361da177e4SLinus Torvalds 			       (snd_cs4281_peekBA0(chip, BA0_MIDSR) & BA0_MIDSR_TBF) == 0) {
17371da177e4SLinus Torvalds 				if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
17381da177e4SLinus Torvalds 					chip->midcr &= ~BA0_MIDCR_TIE;
17391da177e4SLinus Torvalds 				} else {
17401da177e4SLinus Torvalds 					snd_cs4281_pokeBA0(chip, BA0_MIDWP, byte);
17411da177e4SLinus Torvalds 				}
17421da177e4SLinus Torvalds 			}
17431da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
17441da177e4SLinus Torvalds 		}
17451da177e4SLinus Torvalds 	} else {
17461da177e4SLinus Torvalds 		if (chip->midcr & BA0_MIDCR_TIE) {
17471da177e4SLinus Torvalds 			chip->midcr &= ~BA0_MIDCR_TIE;
17481da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
17491da177e4SLinus Torvalds 		}
17501da177e4SLinus Torvalds 	}
17511da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
1754485885b9STakashi Iwai static const struct snd_rawmidi_ops snd_cs4281_midi_output =
17551da177e4SLinus Torvalds {
17561da177e4SLinus Torvalds 	.open =		snd_cs4281_midi_output_open,
17571da177e4SLinus Torvalds 	.close =	snd_cs4281_midi_output_close,
17581da177e4SLinus Torvalds 	.trigger =	snd_cs4281_midi_output_trigger,
17591da177e4SLinus Torvalds };
17601da177e4SLinus Torvalds 
1761485885b9STakashi Iwai static const struct snd_rawmidi_ops snd_cs4281_midi_input =
17621da177e4SLinus Torvalds {
17631da177e4SLinus Torvalds 	.open = 	snd_cs4281_midi_input_open,
17641da177e4SLinus Torvalds 	.close =	snd_cs4281_midi_input_close,
17651da177e4SLinus Torvalds 	.trigger =	snd_cs4281_midi_input_trigger,
17661da177e4SLinus Torvalds };
17671da177e4SLinus Torvalds 
17683e4f4776SLars-Peter Clausen static int snd_cs4281_midi(struct cs4281 *chip, int device)
17691da177e4SLinus Torvalds {
177093e35f95STakashi Iwai 	struct snd_rawmidi *rmidi;
17711da177e4SLinus Torvalds 	int err;
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 	if ((err = snd_rawmidi_new(chip->card, "CS4281", device, 1, 1, &rmidi)) < 0)
17741da177e4SLinus Torvalds 		return err;
17751da177e4SLinus Torvalds 	strcpy(rmidi->name, "CS4281");
17761da177e4SLinus Torvalds 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_cs4281_midi_output);
17771da177e4SLinus Torvalds 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_cs4281_midi_input);
17781da177e4SLinus Torvalds 	rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
17791da177e4SLinus Torvalds 	rmidi->private_data = chip;
17801da177e4SLinus Torvalds 	chip->rmidi = rmidi;
17811da177e4SLinus Torvalds 	return 0;
17821da177e4SLinus Torvalds }
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds /*
17851da177e4SLinus Torvalds  *  Interrupt handler
17861da177e4SLinus Torvalds  */
17871da177e4SLinus Torvalds 
17887d12e780SDavid Howells static irqreturn_t snd_cs4281_interrupt(int irq, void *dev_id)
17891da177e4SLinus Torvalds {
179093e35f95STakashi Iwai 	struct cs4281 *chip = dev_id;
17911da177e4SLinus Torvalds 	unsigned int status, dma, val;
179293e35f95STakashi Iwai 	struct cs4281_dma *cdma;
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 	if (chip == NULL)
17951da177e4SLinus Torvalds 		return IRQ_NONE;
17961da177e4SLinus Torvalds 	status = snd_cs4281_peekBA0(chip, BA0_HISR);
17971da177e4SLinus Torvalds 	if ((status & 0x7fffffff) == 0) {
17981da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_HICR, BA0_HICR_EOI);
17991da177e4SLinus Torvalds 		return IRQ_NONE;
18001da177e4SLinus Torvalds 	}
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 	if (status & (BA0_HISR_DMA(0)|BA0_HISR_DMA(1)|BA0_HISR_DMA(2)|BA0_HISR_DMA(3))) {
18031da177e4SLinus Torvalds 		for (dma = 0; dma < 4; dma++)
18041da177e4SLinus Torvalds 			if (status & BA0_HISR_DMA(dma)) {
18051da177e4SLinus Torvalds 				cdma = &chip->dma[dma];
18061da177e4SLinus Torvalds 				spin_lock(&chip->reg_lock);
18071da177e4SLinus Torvalds 				/* ack DMA IRQ */
18081da177e4SLinus Torvalds 				val = snd_cs4281_peekBA0(chip, cdma->regHDSR);
18091da177e4SLinus Torvalds 				/* workaround, sometimes CS4281 acknowledges */
18101da177e4SLinus Torvalds 				/* end or middle transfer position twice */
18111da177e4SLinus Torvalds 				cdma->frag++;
18121da177e4SLinus Torvalds 				if ((val & BA0_HDSR_DHTC) && !(cdma->frag & 1)) {
18131da177e4SLinus Torvalds 					cdma->frag--;
18141da177e4SLinus Torvalds 					chip->spurious_dhtc_irq++;
18151da177e4SLinus Torvalds 					spin_unlock(&chip->reg_lock);
18161da177e4SLinus Torvalds 					continue;
18171da177e4SLinus Torvalds 				}
18181da177e4SLinus Torvalds 				if ((val & BA0_HDSR_DTC) && (cdma->frag & 1)) {
18191da177e4SLinus Torvalds 					cdma->frag--;
18201da177e4SLinus Torvalds 					chip->spurious_dtc_irq++;
18211da177e4SLinus Torvalds 					spin_unlock(&chip->reg_lock);
18221da177e4SLinus Torvalds 					continue;
18231da177e4SLinus Torvalds 				}
18241da177e4SLinus Torvalds 				spin_unlock(&chip->reg_lock);
18251da177e4SLinus Torvalds 				snd_pcm_period_elapsed(cdma->substream);
18261da177e4SLinus Torvalds 			}
18271da177e4SLinus Torvalds 	}
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	if ((status & BA0_HISR_MIDI) && chip->rmidi) {
18301da177e4SLinus Torvalds 		unsigned char c;
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
18331da177e4SLinus Torvalds 		while ((snd_cs4281_peekBA0(chip, BA0_MIDSR) & BA0_MIDSR_RBE) == 0) {
18341da177e4SLinus Torvalds 			c = snd_cs4281_peekBA0(chip, BA0_MIDRP);
18351da177e4SLinus Torvalds 			if ((chip->midcr & BA0_MIDCR_RIE) == 0)
18361da177e4SLinus Torvalds 				continue;
18371da177e4SLinus Torvalds 			snd_rawmidi_receive(chip->midi_input, &c, 1);
18381da177e4SLinus Torvalds 		}
18391da177e4SLinus Torvalds 		while ((snd_cs4281_peekBA0(chip, BA0_MIDSR) & BA0_MIDSR_TBF) == 0) {
18401da177e4SLinus Torvalds 			if ((chip->midcr & BA0_MIDCR_TIE) == 0)
18411da177e4SLinus Torvalds 				break;
18421da177e4SLinus Torvalds 			if (snd_rawmidi_transmit(chip->midi_output, &c, 1) != 1) {
18431da177e4SLinus Torvalds 				chip->midcr &= ~BA0_MIDCR_TIE;
18441da177e4SLinus Torvalds 				snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
18451da177e4SLinus Torvalds 				break;
18461da177e4SLinus Torvalds 			}
18471da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDWP, c);
18481da177e4SLinus Torvalds 		}
18491da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
18501da177e4SLinus Torvalds 	}
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 	/* EOI to the PCI part... reenables interrupts */
18531da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HICR, BA0_HICR_EOI);
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds 	return IRQ_HANDLED;
18561da177e4SLinus Torvalds }
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds /*
18601da177e4SLinus Torvalds  * OPL3 command
18611da177e4SLinus Torvalds  */
186293e35f95STakashi Iwai static void snd_cs4281_opl3_command(struct snd_opl3 *opl3, unsigned short cmd,
186393e35f95STakashi Iwai 				    unsigned char val)
18641da177e4SLinus Torvalds {
18651da177e4SLinus Torvalds 	unsigned long flags;
186693e35f95STakashi Iwai 	struct cs4281 *chip = opl3->private_data;
18671da177e4SLinus Torvalds 	void __iomem *port;
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 	if (cmd & OPL3_RIGHT)
18701da177e4SLinus Torvalds 		port = chip->ba0 + BA0_B1AP; /* right port */
18711da177e4SLinus Torvalds 	else
18721da177e4SLinus Torvalds 		port = chip->ba0 + BA0_B0AP; /* left port */
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 	spin_lock_irqsave(&opl3->reg_lock, flags);
18751da177e4SLinus Torvalds 
18761da177e4SLinus Torvalds 	writel((unsigned int)cmd, port);
18771da177e4SLinus Torvalds 	udelay(10);
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds 	writel((unsigned int)val, port + 4);
18801da177e4SLinus Torvalds 	udelay(30);
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 	spin_unlock_irqrestore(&opl3->reg_lock, flags);
18831da177e4SLinus Torvalds }
18841da177e4SLinus Torvalds 
1885e23e7a14SBill Pemberton static int snd_cs4281_probe(struct pci_dev *pci,
18861da177e4SLinus Torvalds 			    const struct pci_device_id *pci_id)
18871da177e4SLinus Torvalds {
18881da177e4SLinus Torvalds 	static int dev;
188993e35f95STakashi Iwai 	struct snd_card *card;
189093e35f95STakashi Iwai 	struct cs4281 *chip;
189193e35f95STakashi Iwai 	struct snd_opl3 *opl3;
18921da177e4SLinus Torvalds 	int err;
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds         if (dev >= SNDRV_CARDS)
18951da177e4SLinus Torvalds                 return -ENODEV;
18961da177e4SLinus Torvalds 	if (!enable[dev]) {
18971da177e4SLinus Torvalds 		dev++;
18981da177e4SLinus Torvalds 		return -ENOENT;
18991da177e4SLinus Torvalds 	}
19001da177e4SLinus Torvalds 
190160c5772bSTakashi Iwai 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
190260c5772bSTakashi Iwai 			   0, &card);
1903e58de7baSTakashi Iwai 	if (err < 0)
1904e58de7baSTakashi Iwai 		return err;
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	if ((err = snd_cs4281_create(card, pci, &chip, dual_codec[dev])) < 0) {
19071da177e4SLinus Torvalds 		snd_card_free(card);
19081da177e4SLinus Torvalds 		return err;
19091da177e4SLinus Torvalds 	}
191038c0a158STakashi Iwai 	card->private_data = chip;
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 	if ((err = snd_cs4281_mixer(chip)) < 0) {
19131da177e4SLinus Torvalds 		snd_card_free(card);
19141da177e4SLinus Torvalds 		return err;
19151da177e4SLinus Torvalds 	}
19163e4f4776SLars-Peter Clausen 	if ((err = snd_cs4281_pcm(chip, 0)) < 0) {
19171da177e4SLinus Torvalds 		snd_card_free(card);
19181da177e4SLinus Torvalds 		return err;
19191da177e4SLinus Torvalds 	}
19203e4f4776SLars-Peter Clausen 	if ((err = snd_cs4281_midi(chip, 0)) < 0) {
19211da177e4SLinus Torvalds 		snd_card_free(card);
19221da177e4SLinus Torvalds 		return err;
19231da177e4SLinus Torvalds 	}
19241da177e4SLinus Torvalds 	if ((err = snd_opl3_new(card, OPL3_HW_OPL3_CS4281, &opl3)) < 0) {
19251da177e4SLinus Torvalds 		snd_card_free(card);
19261da177e4SLinus Torvalds 		return err;
19271da177e4SLinus Torvalds 	}
19281da177e4SLinus Torvalds 	opl3->private_data = chip;
19291da177e4SLinus Torvalds 	opl3->command = snd_cs4281_opl3_command;
19301da177e4SLinus Torvalds 	snd_opl3_init(opl3);
19311da177e4SLinus Torvalds 	if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
19321da177e4SLinus Torvalds 		snd_card_free(card);
19331da177e4SLinus Torvalds 		return err;
19341da177e4SLinus Torvalds 	}
19351da177e4SLinus Torvalds 	snd_cs4281_create_gameport(chip);
19361da177e4SLinus Torvalds 	strcpy(card->driver, "CS4281");
19371da177e4SLinus Torvalds 	strcpy(card->shortname, "Cirrus Logic CS4281");
19381da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %d",
19391da177e4SLinus Torvalds 		card->shortname,
19401da177e4SLinus Torvalds 		chip->ba0_addr,
19411da177e4SLinus Torvalds 		chip->irq);
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
19441da177e4SLinus Torvalds 		snd_card_free(card);
19451da177e4SLinus Torvalds 		return err;
19461da177e4SLinus Torvalds 	}
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
19491da177e4SLinus Torvalds 	dev++;
19501da177e4SLinus Torvalds 	return 0;
19511da177e4SLinus Torvalds }
19521da177e4SLinus Torvalds 
1953e23e7a14SBill Pemberton static void snd_cs4281_remove(struct pci_dev *pci)
19541da177e4SLinus Torvalds {
19551da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
19561da177e4SLinus Torvalds }
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds /*
19591da177e4SLinus Torvalds  * Power Management
19601da177e4SLinus Torvalds  */
1961c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds static int saved_regs[SUSPEND_REGISTERS] = {
19641da177e4SLinus Torvalds 	BA0_JSCTL,
19651da177e4SLinus Torvalds 	BA0_GPIOR,
19661da177e4SLinus Torvalds 	BA0_SSCR,
19671da177e4SLinus Torvalds 	BA0_MIDCR,
19681da177e4SLinus Torvalds 	BA0_SRCSA,
19691da177e4SLinus Torvalds 	BA0_PASR,
19701da177e4SLinus Torvalds 	BA0_CASR,
19711da177e4SLinus Torvalds 	BA0_DACSR,
19721da177e4SLinus Torvalds 	BA0_ADCSR,
19731da177e4SLinus Torvalds 	BA0_FMLVC,
19741da177e4SLinus Torvalds 	BA0_FMRVC,
19751da177e4SLinus Torvalds 	BA0_PPLVC,
19761da177e4SLinus Torvalds 	BA0_PPRVC,
19771da177e4SLinus Torvalds };
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds #define CLKCR1_CKRA                             0x00010000L
19801da177e4SLinus Torvalds 
198168cb2b55STakashi Iwai static int cs4281_suspend(struct device *dev)
19821da177e4SLinus Torvalds {
198368cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
198438c0a158STakashi Iwai 	struct cs4281 *chip = card->private_data;
19851da177e4SLinus Torvalds 	u32 ulCLK;
19861da177e4SLinus Torvalds 	unsigned int i;
19871da177e4SLinus Torvalds 
198838c0a158STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
19891da177e4SLinus Torvalds 	snd_ac97_suspend(chip->ac97);
19901da177e4SLinus Torvalds 	snd_ac97_suspend(chip->ac97_secondary);
19911da177e4SLinus Torvalds 
19921da177e4SLinus Torvalds 	ulCLK = snd_cs4281_peekBA0(chip, BA0_CLKCR1);
19931da177e4SLinus Torvalds 	ulCLK |= CLKCR1_CKRA;
19941da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, ulCLK);
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 	/* Disable interrupts. */
19971da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HICR, BA0_HICR_CHGM);
19981da177e4SLinus Torvalds 
19991da177e4SLinus Torvalds 	/* remember the status registers */
20001da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
20011da177e4SLinus Torvalds 		if (saved_regs[i])
20021da177e4SLinus Torvalds 			chip->suspend_regs[i] = snd_cs4281_peekBA0(chip, saved_regs[i]);
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds 	/* Turn off the serial ports. */
20051da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SERMC, 0);
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds 	/* Power off FM, Joystick, AC link, */
20081da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SSPM, 0);
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds 	/* DLL off. */
20111da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, 0);
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds 	/* AC link off. */
20141da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SPMC, 0);
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds 	ulCLK = snd_cs4281_peekBA0(chip, BA0_CLKCR1);
20171da177e4SLinus Torvalds 	ulCLK &= ~CLKCR1_CKRA;
20181da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, ulCLK);
20191da177e4SLinus Torvalds 	return 0;
20201da177e4SLinus Torvalds }
20211da177e4SLinus Torvalds 
202268cb2b55STakashi Iwai static int cs4281_resume(struct device *dev)
20231da177e4SLinus Torvalds {
202468cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
202538c0a158STakashi Iwai 	struct cs4281 *chip = card->private_data;
20261da177e4SLinus Torvalds 	unsigned int i;
20271da177e4SLinus Torvalds 	u32 ulCLK;
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds 	ulCLK = snd_cs4281_peekBA0(chip, BA0_CLKCR1);
20301da177e4SLinus Torvalds 	ulCLK |= CLKCR1_CKRA;
20311da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, ulCLK);
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds 	snd_cs4281_chip_init(chip);
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds 	/* restore the status registers */
20361da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
20371da177e4SLinus Torvalds 		if (saved_regs[i])
20381da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, saved_regs[i], chip->suspend_regs[i]);
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	snd_ac97_resume(chip->ac97);
20411da177e4SLinus Torvalds 	snd_ac97_resume(chip->ac97_secondary);
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 	ulCLK = snd_cs4281_peekBA0(chip, BA0_CLKCR1);
20441da177e4SLinus Torvalds 	ulCLK &= ~CLKCR1_CKRA;
20451da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, ulCLK);
20461da177e4SLinus Torvalds 
204738c0a158STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
20481da177e4SLinus Torvalds 	return 0;
20491da177e4SLinus Torvalds }
205068cb2b55STakashi Iwai 
205168cb2b55STakashi Iwai static SIMPLE_DEV_PM_OPS(cs4281_pm, cs4281_suspend, cs4281_resume);
205268cb2b55STakashi Iwai #define CS4281_PM_OPS	&cs4281_pm
205368cb2b55STakashi Iwai #else
205468cb2b55STakashi Iwai #define CS4281_PM_OPS	NULL
2055c7561cd8STakashi Iwai #endif /* CONFIG_PM_SLEEP */
20561da177e4SLinus Torvalds 
2057e9f66d9bSTakashi Iwai static struct pci_driver cs4281_driver = {
20583733e424STakashi Iwai 	.name = KBUILD_MODNAME,
20591da177e4SLinus Torvalds 	.id_table = snd_cs4281_ids,
20601da177e4SLinus Torvalds 	.probe = snd_cs4281_probe,
2061e23e7a14SBill Pemberton 	.remove = snd_cs4281_remove,
206268cb2b55STakashi Iwai 	.driver = {
206368cb2b55STakashi Iwai 		.pm = CS4281_PM_OPS,
206468cb2b55STakashi Iwai 	},
20651da177e4SLinus Torvalds };
20661da177e4SLinus Torvalds 
2067e9f66d9bSTakashi Iwai module_pci_driver(cs4281_driver);
2068