xref: /linux/sound/pci/cs4281.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
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 
291da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
301da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
31a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable switches */
32a67ff6a5SRusty Russell static bool dual_codec[SNDRV_CARDS];	/* dual codec */
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
351da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for CS4281 soundcard.");
361da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
371da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for CS4281 soundcard.");
381da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
391da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable CS4281 soundcard.");
401da177e4SLinus Torvalds module_param_array(dual_codec, bool, NULL, 0444);
411da177e4SLinus Torvalds MODULE_PARM_DESC(dual_codec, "Secondary Codec ID (0 = disabled).");
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /*
441da177e4SLinus Torvalds  *  Direct registers
451da177e4SLinus Torvalds  */
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #define CS4281_BA0_SIZE		0x1000
481da177e4SLinus Torvalds #define CS4281_BA1_SIZE		0x10000
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /*
511da177e4SLinus Torvalds  *  BA0 registers
521da177e4SLinus Torvalds  */
531da177e4SLinus Torvalds #define BA0_HISR		0x0000	/* Host Interrupt Status Register */
541da177e4SLinus Torvalds #define BA0_HISR_INTENA		(1<<31)	/* Internal Interrupt Enable Bit */
551da177e4SLinus Torvalds #define BA0_HISR_MIDI		(1<<22)	/* MIDI port interrupt */
561da177e4SLinus Torvalds #define BA0_HISR_FIFOI		(1<<20)	/* FIFO polled interrupt */
571da177e4SLinus Torvalds #define BA0_HISR_DMAI		(1<<18)	/* DMA interrupt (half or end) */
581da177e4SLinus Torvalds #define BA0_HISR_FIFO(c)	(1<<(12+(c))) /* FIFO channel interrupt */
591da177e4SLinus Torvalds #define BA0_HISR_DMA(c)		(1<<(8+(c)))  /* DMA channel interrupt */
601da177e4SLinus Torvalds #define BA0_HISR_GPPI		(1<<5)	/* General Purpose Input (Primary chip) */
611da177e4SLinus Torvalds #define BA0_HISR_GPSI		(1<<4)	/* General Purpose Input (Secondary chip) */
621da177e4SLinus Torvalds #define BA0_HISR_GP3I		(1<<3)	/* GPIO3 pin Interrupt */
631da177e4SLinus Torvalds #define BA0_HISR_GP1I		(1<<2)	/* GPIO1 pin Interrupt */
641da177e4SLinus Torvalds #define BA0_HISR_VUPI		(1<<1)	/* VOLUP pin Interrupt */
651da177e4SLinus Torvalds #define BA0_HISR_VDNI		(1<<0)	/* VOLDN pin Interrupt */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #define BA0_HICR		0x0008	/* Host Interrupt Control Register */
681da177e4SLinus Torvalds #define BA0_HICR_CHGM		(1<<1)	/* INTENA Change Mask */
691da177e4SLinus Torvalds #define BA0_HICR_IEV		(1<<0)	/* INTENA Value */
701da177e4SLinus Torvalds #define BA0_HICR_EOI		(3<<0)	/* End of Interrupt command */
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #define BA0_HIMR		0x000c	/* Host Interrupt Mask Register */
731da177e4SLinus Torvalds 					/* Use same contants as for BA0_HISR */
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds #define BA0_IIER		0x0010	/* ISA Interrupt Enable Register */
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds #define BA0_HDSR0		0x00f0	/* Host DMA Engine 0 Status Register */
781da177e4SLinus Torvalds #define BA0_HDSR1		0x00f4	/* Host DMA Engine 1 Status Register */
791da177e4SLinus Torvalds #define BA0_HDSR2		0x00f8	/* Host DMA Engine 2 Status Register */
801da177e4SLinus Torvalds #define BA0_HDSR3		0x00fc	/* Host DMA Engine 3 Status Register */
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds #define BA0_HDSR_CH1P		(1<<25)	/* Channel 1 Pending */
831da177e4SLinus Torvalds #define BA0_HDSR_CH2P		(1<<24)	/* Channel 2 Pending */
841da177e4SLinus Torvalds #define BA0_HDSR_DHTC		(1<<17)	/* DMA Half Terminal Count */
851da177e4SLinus Torvalds #define BA0_HDSR_DTC		(1<<16)	/* DMA Terminal Count */
861da177e4SLinus Torvalds #define BA0_HDSR_DRUN		(1<<15)	/* DMA Running */
871da177e4SLinus Torvalds #define BA0_HDSR_RQ		(1<<7)	/* Pending Request */
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds #define BA0_DCA0		0x0110	/* Host DMA Engine 0 Current Address */
901da177e4SLinus Torvalds #define BA0_DCC0		0x0114	/* Host DMA Engine 0 Current Count */
911da177e4SLinus Torvalds #define BA0_DBA0		0x0118	/* Host DMA Engine 0 Base Address */
921da177e4SLinus Torvalds #define BA0_DBC0		0x011c	/* Host DMA Engine 0 Base Count */
931da177e4SLinus Torvalds #define BA0_DCA1		0x0120	/* Host DMA Engine 1 Current Address */
941da177e4SLinus Torvalds #define BA0_DCC1		0x0124	/* Host DMA Engine 1 Current Count */
951da177e4SLinus Torvalds #define BA0_DBA1		0x0128	/* Host DMA Engine 1 Base Address */
961da177e4SLinus Torvalds #define BA0_DBC1		0x012c	/* Host DMA Engine 1 Base Count */
971da177e4SLinus Torvalds #define BA0_DCA2		0x0130	/* Host DMA Engine 2 Current Address */
981da177e4SLinus Torvalds #define BA0_DCC2		0x0134	/* Host DMA Engine 2 Current Count */
991da177e4SLinus Torvalds #define BA0_DBA2		0x0138	/* Host DMA Engine 2 Base Address */
1001da177e4SLinus Torvalds #define BA0_DBC2		0x013c	/* Host DMA Engine 2 Base Count */
1011da177e4SLinus Torvalds #define BA0_DCA3		0x0140	/* Host DMA Engine 3 Current Address */
1021da177e4SLinus Torvalds #define BA0_DCC3		0x0144	/* Host DMA Engine 3 Current Count */
1031da177e4SLinus Torvalds #define BA0_DBA3		0x0148	/* Host DMA Engine 3 Base Address */
1041da177e4SLinus Torvalds #define BA0_DBC3		0x014c	/* Host DMA Engine 3 Base Count */
1051da177e4SLinus Torvalds #define BA0_DMR0		0x0150	/* Host DMA Engine 0 Mode */
1061da177e4SLinus Torvalds #define BA0_DCR0		0x0154	/* Host DMA Engine 0 Command */
1071da177e4SLinus Torvalds #define BA0_DMR1		0x0158	/* Host DMA Engine 1 Mode */
1081da177e4SLinus Torvalds #define BA0_DCR1		0x015c	/* Host DMA Engine 1 Command */
1091da177e4SLinus Torvalds #define BA0_DMR2		0x0160	/* Host DMA Engine 2 Mode */
1101da177e4SLinus Torvalds #define BA0_DCR2		0x0164	/* Host DMA Engine 2 Command */
1111da177e4SLinus Torvalds #define BA0_DMR3		0x0168	/* Host DMA Engine 3 Mode */
1121da177e4SLinus Torvalds #define BA0_DCR3		0x016c	/* Host DMA Engine 3 Command */
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds #define BA0_DMR_DMA		(1<<29)	/* Enable DMA mode */
1151da177e4SLinus Torvalds #define BA0_DMR_POLL		(1<<28)	/* Enable poll mode */
1161da177e4SLinus Torvalds #define BA0_DMR_TBC		(1<<25)	/* Transfer By Channel */
1171da177e4SLinus Torvalds #define BA0_DMR_CBC		(1<<24)	/* Count By Channel (0 = frame resolution) */
1181da177e4SLinus Torvalds #define BA0_DMR_SWAPC		(1<<22)	/* Swap Left/Right Channels */
1191da177e4SLinus Torvalds #define BA0_DMR_SIZE20		(1<<20)	/* Sample is 20-bit */
1201da177e4SLinus Torvalds #define BA0_DMR_USIGN		(1<<19)	/* Unsigned */
1211da177e4SLinus Torvalds #define BA0_DMR_BEND		(1<<18)	/* Big Endian */
1221da177e4SLinus Torvalds #define BA0_DMR_MONO		(1<<17)	/* Mono */
1231da177e4SLinus Torvalds #define BA0_DMR_SIZE8		(1<<16)	/* Sample is 8-bit */
1241da177e4SLinus Torvalds #define BA0_DMR_TYPE_DEMAND	(0<<6)
1251da177e4SLinus Torvalds #define BA0_DMR_TYPE_SINGLE	(1<<6)
1261da177e4SLinus Torvalds #define BA0_DMR_TYPE_BLOCK	(2<<6)
1271da177e4SLinus Torvalds #define BA0_DMR_TYPE_CASCADE	(3<<6)	/* Not supported */
1281da177e4SLinus Torvalds #define BA0_DMR_DEC		(1<<5)	/* Access Increment (0) or Decrement (1) */
1291da177e4SLinus Torvalds #define BA0_DMR_AUTO		(1<<4)	/* Auto-Initialize */
1301da177e4SLinus Torvalds #define BA0_DMR_TR_VERIFY	(0<<2)	/* Verify Transfer */
1311da177e4SLinus Torvalds #define BA0_DMR_TR_WRITE	(1<<2)	/* Write Transfer */
1321da177e4SLinus Torvalds #define BA0_DMR_TR_READ		(2<<2)	/* Read Transfer */
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds #define BA0_DCR_HTCIE		(1<<17)	/* Half Terminal Count Interrupt */
1351da177e4SLinus Torvalds #define BA0_DCR_TCIE		(1<<16)	/* Terminal Count Interrupt */
1361da177e4SLinus Torvalds #define BA0_DCR_MSK		(1<<0)	/* DMA Mask bit */
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds #define BA0_FCR0		0x0180	/* FIFO Control 0 */
1391da177e4SLinus Torvalds #define BA0_FCR1		0x0184	/* FIFO Control 1 */
1401da177e4SLinus Torvalds #define BA0_FCR2		0x0188	/* FIFO Control 2 */
1411da177e4SLinus Torvalds #define BA0_FCR3		0x018c	/* FIFO Control 3 */
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds #define BA0_FCR_FEN		(1<<31)	/* FIFO Enable bit */
1441da177e4SLinus Torvalds #define BA0_FCR_DACZ		(1<<30)	/* DAC Zero */
1451da177e4SLinus Torvalds #define BA0_FCR_PSH		(1<<29)	/* Previous Sample Hold */
1461da177e4SLinus Torvalds #define BA0_FCR_RS(x)		(((x)&0x1f)<<24) /* Right Slot Mapping */
1471da177e4SLinus Torvalds #define BA0_FCR_LS(x)		(((x)&0x1f)<<16) /* Left Slot Mapping */
1481da177e4SLinus Torvalds #define BA0_FCR_SZ(x)		(((x)&0x7f)<<8)	/* FIFO buffer size (in samples) */
1491da177e4SLinus Torvalds #define BA0_FCR_OF(x)		(((x)&0x7f)<<0)	/* FIFO starting offset (in samples) */
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds #define BA0_FPDR0		0x0190	/* FIFO Polled Data 0 */
1521da177e4SLinus Torvalds #define BA0_FPDR1		0x0194	/* FIFO Polled Data 1 */
1531da177e4SLinus Torvalds #define BA0_FPDR2		0x0198	/* FIFO Polled Data 2 */
1541da177e4SLinus Torvalds #define BA0_FPDR3		0x019c	/* FIFO Polled Data 3 */
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds #define BA0_FCHS		0x020c	/* FIFO Channel Status */
1571da177e4SLinus Torvalds #define BA0_FCHS_RCO(x)		(1<<(7+(((x)&3)<<3))) /* Right Channel Out */
1581da177e4SLinus Torvalds #define BA0_FCHS_LCO(x)		(1<<(6+(((x)&3)<<3))) /* Left Channel Out */
1591da177e4SLinus Torvalds #define BA0_FCHS_MRP(x)		(1<<(5+(((x)&3)<<3))) /* Move Read Pointer */
1601da177e4SLinus Torvalds #define BA0_FCHS_FE(x)		(1<<(4+(((x)&3)<<3))) /* FIFO Empty */
1611da177e4SLinus Torvalds #define BA0_FCHS_FF(x)		(1<<(3+(((x)&3)<<3))) /* FIFO Full */
1621da177e4SLinus Torvalds #define BA0_FCHS_IOR(x)		(1<<(2+(((x)&3)<<3))) /* Internal Overrun Flag */
1631da177e4SLinus Torvalds #define BA0_FCHS_RCI(x)		(1<<(1+(((x)&3)<<3))) /* Right Channel In */
1641da177e4SLinus Torvalds #define BA0_FCHS_LCI(x)		(1<<(0+(((x)&3)<<3))) /* Left Channel In */
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds #define BA0_FSIC0		0x0210	/* FIFO Status and Interrupt Control 0 */
1671da177e4SLinus Torvalds #define BA0_FSIC1		0x0214	/* FIFO Status and Interrupt Control 1 */
1681da177e4SLinus Torvalds #define BA0_FSIC2		0x0218	/* FIFO Status and Interrupt Control 2 */
1691da177e4SLinus Torvalds #define BA0_FSIC3		0x021c	/* FIFO Status and Interrupt Control 3 */
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds #define BA0_FSIC_FIC(x)		(((x)&0x7f)<<24) /* FIFO Interrupt Count */
1721da177e4SLinus Torvalds #define BA0_FSIC_FORIE		(1<<23) /* FIFO OverRun Interrupt Enable */
1731da177e4SLinus Torvalds #define BA0_FSIC_FURIE		(1<<22) /* FIFO UnderRun Interrupt Enable */
1741da177e4SLinus Torvalds #define BA0_FSIC_FSCIE		(1<<16)	/* FIFO Sample Count Interrupt Enable */
1751da177e4SLinus Torvalds #define BA0_FSIC_FSC(x)		(((x)&0x7f)<<8) /* FIFO Sample Count */
1761da177e4SLinus Torvalds #define BA0_FSIC_FOR		(1<<7)	/* FIFO OverRun */
1771da177e4SLinus Torvalds #define BA0_FSIC_FUR		(1<<6)	/* FIFO UnderRun */
1781da177e4SLinus Torvalds #define BA0_FSIC_FSCR		(1<<0)	/* FIFO Sample Count Reached */
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds #define BA0_PMCS		0x0344	/* Power Management Control/Status */
1811da177e4SLinus Torvalds #define BA0_CWPR		0x03e0	/* Configuration Write Protect */
182a488e033SArnaud Patard 
1831da177e4SLinus Torvalds #define BA0_EPPMC		0x03e4	/* Extended PCI Power Management Control */
184a488e033SArnaud Patard #define BA0_EPPMC_FPDN		(1<<14) /* Full Power DowN */
185a488e033SArnaud Patard 
1861da177e4SLinus Torvalds #define BA0_GPIOR		0x03e8	/* GPIO Pin Interface Register */
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds #define BA0_SPMC		0x03ec	/* Serial Port Power Management Control (& ASDIN2 enable) */
1891da177e4SLinus Torvalds #define BA0_SPMC_GIPPEN		(1<<15)	/* GP INT Primary PME# Enable */
1901da177e4SLinus Torvalds #define BA0_SPMC_GISPEN		(1<<14)	/* GP INT Secondary PME# Enable */
1911da177e4SLinus Torvalds #define BA0_SPMC_EESPD		(1<<9)	/* EEPROM Serial Port Disable */
1921da177e4SLinus Torvalds #define BA0_SPMC_ASDI2E		(1<<8)	/* ASDIN2 Enable */
1931da177e4SLinus Torvalds #define BA0_SPMC_ASDO		(1<<7)	/* Asynchronous ASDOUT Assertion */
1941da177e4SLinus Torvalds #define BA0_SPMC_WUP2		(1<<3)	/* Wakeup for Secondary Input */
1951da177e4SLinus Torvalds #define BA0_SPMC_WUP1		(1<<2)	/* Wakeup for Primary Input */
1961da177e4SLinus Torvalds #define BA0_SPMC_ASYNC		(1<<1)	/* Asynchronous ASYNC Assertion */
1971da177e4SLinus Torvalds #define BA0_SPMC_RSTN		(1<<0)	/* Reset Not! */
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds #define BA0_CFLR		0x03f0	/* Configuration Load Register (EEPROM or BIOS) */
2001da177e4SLinus Torvalds #define BA0_CFLR_DEFAULT	0x00000001 /* CFLR must be in AC97 link mode */
2011da177e4SLinus Torvalds #define BA0_IISR		0x03f4	/* ISA Interrupt Select */
2021da177e4SLinus Torvalds #define BA0_TMS			0x03f8	/* Test Register */
2031da177e4SLinus Torvalds #define BA0_SSVID		0x03fc	/* Subsystem ID register */
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds #define BA0_CLKCR1		0x0400	/* Clock Control Register 1 */
2061da177e4SLinus Torvalds #define BA0_CLKCR1_CLKON	(1<<25)	/* Read Only */
2071da177e4SLinus Torvalds #define BA0_CLKCR1_DLLRDY	(1<<24)	/* DLL Ready */
2081da177e4SLinus Torvalds #define BA0_CLKCR1_DLLOS	(1<<6)	/* DLL Output Select */
2091da177e4SLinus Torvalds #define BA0_CLKCR1_SWCE		(1<<5)	/* Clock Enable */
2101da177e4SLinus Torvalds #define BA0_CLKCR1_DLLP		(1<<4)	/* DLL PowerUp */
2111da177e4SLinus Torvalds #define BA0_CLKCR1_DLLSS	(((x)&3)<<3) /* DLL Source Select */
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds #define BA0_FRR			0x0410	/* Feature Reporting Register */
2141da177e4SLinus Torvalds #define BA0_SLT12O		0x041c	/* Slot 12 GPIO Output Register for AC-Link */
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds #define BA0_SERMC		0x0420	/* Serial Port Master Control */
2171da177e4SLinus Torvalds #define BA0_SERMC_FCRN		(1<<27)	/* Force Codec Ready Not */
2181da177e4SLinus Torvalds #define BA0_SERMC_ODSEN2	(1<<25)	/* On-Demand Support Enable ASDIN2 */
2191da177e4SLinus Torvalds #define BA0_SERMC_ODSEN1	(1<<24)	/* On-Demand Support Enable ASDIN1 */
2201da177e4SLinus Torvalds #define BA0_SERMC_SXLB		(1<<21)	/* ASDIN2 to ASDOUT Loopback */
2211da177e4SLinus Torvalds #define BA0_SERMC_SLB		(1<<20)	/* ASDOUT to ASDIN2 Loopback */
2221da177e4SLinus Torvalds #define BA0_SERMC_LOVF		(1<<19)	/* Loopback Output Valid Frame bit */
2231da177e4SLinus Torvalds #define BA0_SERMC_TCID(x)	(((x)&3)<<16) /* Target Secondary Codec ID */
2241da177e4SLinus Torvalds #define BA0_SERMC_PXLB		(5<<1)	/* Primary Port External Loopback */
2251da177e4SLinus Torvalds #define BA0_SERMC_PLB		(4<<1)	/* Primary Port Internal Loopback */
2261da177e4SLinus Torvalds #define BA0_SERMC_PTC		(7<<1)	/* Port Timing Configuration */
2271da177e4SLinus Torvalds #define BA0_SERMC_PTC_AC97	(1<<1)	/* AC97 mode */
2281da177e4SLinus Torvalds #define BA0_SERMC_MSPE		(1<<0)	/* Master Serial Port Enable */
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds #define BA0_SERC1		0x0428	/* Serial Port Configuration 1 */
2311da177e4SLinus Torvalds #define BA0_SERC1_SO1F(x)	(((x)&7)>>1) /* Primary Output Port Format */
2321da177e4SLinus Torvalds #define BA0_SERC1_AC97		(1<<1)
2331da177e4SLinus Torvalds #define BA0_SERC1_SO1EN		(1<<0)	/* Primary Output Port Enable */
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds #define BA0_SERC2		0x042c	/* Serial Port Configuration 2 */
2361da177e4SLinus Torvalds #define BA0_SERC2_SI1F(x)	(((x)&7)>>1) /* Primary Input Port Format */
2371da177e4SLinus Torvalds #define BA0_SERC2_AC97		(1<<1)
2381da177e4SLinus Torvalds #define BA0_SERC2_SI1EN		(1<<0)	/* Primary Input Port Enable */
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds #define BA0_SLT12M		0x045c	/* Slot 12 Monitor Register for Primary AC-Link */
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds #define BA0_ACCTL		0x0460	/* AC'97 Control */
2431da177e4SLinus Torvalds #define BA0_ACCTL_TC		(1<<6)	/* Target Codec */
2441da177e4SLinus Torvalds #define BA0_ACCTL_CRW		(1<<4)	/* 0=Write, 1=Read Command */
2451da177e4SLinus Torvalds #define BA0_ACCTL_DCV		(1<<3)	/* Dynamic Command Valid */
2461da177e4SLinus Torvalds #define BA0_ACCTL_VFRM		(1<<2)	/* Valid Frame */
2471da177e4SLinus Torvalds #define BA0_ACCTL_ESYN		(1<<1)	/* Enable Sync */
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds #define BA0_ACSTS		0x0464	/* AC'97 Status */
2501da177e4SLinus Torvalds #define BA0_ACSTS_VSTS		(1<<1)	/* Valid Status */
2511da177e4SLinus Torvalds #define BA0_ACSTS_CRDY		(1<<0)	/* Codec Ready */
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds #define BA0_ACOSV		0x0468	/* AC'97 Output Slot Valid */
2541da177e4SLinus Torvalds #define BA0_ACOSV_SLV(x)	(1<<((x)-3))
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds #define BA0_ACCAD		0x046c	/* AC'97 Command Address */
2571da177e4SLinus Torvalds #define BA0_ACCDA		0x0470	/* AC'97 Command Data */
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds #define BA0_ACISV		0x0474	/* AC'97 Input Slot Valid */
2601da177e4SLinus Torvalds #define BA0_ACISV_SLV(x)	(1<<((x)-3))
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds #define BA0_ACSAD		0x0478	/* AC'97 Status Address */
2631da177e4SLinus Torvalds #define BA0_ACSDA		0x047c	/* AC'97 Status Data */
2641da177e4SLinus Torvalds #define BA0_JSPT		0x0480	/* Joystick poll/trigger */
2651da177e4SLinus Torvalds #define BA0_JSCTL		0x0484	/* Joystick control */
2661da177e4SLinus Torvalds #define BA0_JSC1		0x0488	/* Joystick control */
2671da177e4SLinus Torvalds #define BA0_JSC2		0x048c	/* Joystick control */
2681da177e4SLinus Torvalds #define BA0_JSIO		0x04a0
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds #define BA0_MIDCR		0x0490	/* MIDI Control */
2711da177e4SLinus Torvalds #define BA0_MIDCR_MRST		(1<<5)	/* Reset MIDI Interface */
2721da177e4SLinus Torvalds #define BA0_MIDCR_MLB		(1<<4)	/* MIDI Loop Back Enable */
2731da177e4SLinus Torvalds #define BA0_MIDCR_TIE		(1<<3)	/* MIDI Transmuit Interrupt Enable */
2741da177e4SLinus Torvalds #define BA0_MIDCR_RIE		(1<<2)	/* MIDI Receive Interrupt Enable */
2751da177e4SLinus Torvalds #define BA0_MIDCR_RXE		(1<<1)	/* MIDI Receive Enable */
2761da177e4SLinus Torvalds #define BA0_MIDCR_TXE		(1<<0)	/* MIDI Transmit Enable */
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds #define BA0_MIDCMD		0x0494	/* MIDI Command (wo) */
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds #define BA0_MIDSR		0x0494	/* MIDI Status (ro) */
2811da177e4SLinus Torvalds #define BA0_MIDSR_RDA		(1<<15)	/* Sticky bit (RBE 1->0) */
2821da177e4SLinus Torvalds #define BA0_MIDSR_TBE		(1<<14) /* Sticky bit (TBF 0->1) */
2831da177e4SLinus Torvalds #define BA0_MIDSR_RBE		(1<<7)	/* Receive Buffer Empty */
2841da177e4SLinus Torvalds #define BA0_MIDSR_TBF		(1<<6)	/* Transmit Buffer Full */
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds #define BA0_MIDWP		0x0498	/* MIDI Write */
2871da177e4SLinus Torvalds #define BA0_MIDRP		0x049c	/* MIDI Read (ro) */
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds #define BA0_AODSD1		0x04a8	/* AC'97 On-Demand Slot Disable for primary link (ro) */
2901da177e4SLinus Torvalds #define BA0_AODSD1_NDS(x)	(1<<((x)-3))
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds #define BA0_AODSD2		0x04ac	/* AC'97 On-Demand Slot Disable for secondary link (ro) */
2931da177e4SLinus Torvalds #define BA0_AODSD2_NDS(x)	(1<<((x)-3))
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds #define BA0_CFGI		0x04b0	/* Configure Interface (EEPROM interface) */
2961da177e4SLinus Torvalds #define BA0_SLT12M2		0x04dc	/* Slot 12 Monitor Register 2 for secondary AC-link */
2971da177e4SLinus Torvalds #define BA0_ACSTS2		0x04e4	/* AC'97 Status Register 2 */
2981da177e4SLinus Torvalds #define BA0_ACISV2		0x04f4	/* AC'97 Input Slot Valid Register 2 */
2991da177e4SLinus Torvalds #define BA0_ACSAD2		0x04f8	/* AC'97 Status Address Register 2 */
3001da177e4SLinus Torvalds #define BA0_ACSDA2		0x04fc	/* AC'97 Status Data Register 2 */
3011da177e4SLinus Torvalds #define BA0_FMSR		0x0730	/* FM Synthesis Status (ro) */
3021da177e4SLinus Torvalds #define BA0_B0AP		0x0730	/* FM Bank 0 Address Port (wo) */
3031da177e4SLinus Torvalds #define BA0_FMDP		0x0734	/* FM Data Port */
3041da177e4SLinus Torvalds #define BA0_B1AP		0x0738	/* FM Bank 1 Address Port */
3051da177e4SLinus Torvalds #define BA0_B1DP		0x073c	/* FM Bank 1 Data Port */
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds #define BA0_SSPM		0x0740	/* Sound System Power Management */
3081da177e4SLinus Torvalds #define BA0_SSPM_MIXEN		(1<<6)	/* Playback SRC + FM/Wavetable MIX */
3091da177e4SLinus Torvalds #define BA0_SSPM_CSRCEN		(1<<5)	/* Capture Sample Rate Converter Enable */
3101da177e4SLinus Torvalds #define BA0_SSPM_PSRCEN		(1<<4)	/* Playback Sample Rate Converter Enable */
3111da177e4SLinus Torvalds #define BA0_SSPM_JSEN		(1<<3)	/* Joystick Enable */
3121da177e4SLinus Torvalds #define BA0_SSPM_ACLEN		(1<<2)	/* Serial Port Engine and AC-Link Enable */
3131da177e4SLinus Torvalds #define BA0_SSPM_FMEN		(1<<1)	/* FM Synthesis Block Enable */
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds #define BA0_DACSR		0x0744	/* DAC Sample Rate - Playback SRC */
3161da177e4SLinus Torvalds #define BA0_ADCSR		0x0748	/* ADC Sample Rate - Capture SRC */
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds #define BA0_SSCR		0x074c	/* Sound System Control Register */
3191da177e4SLinus Torvalds #define BA0_SSCR_HVS1		(1<<23)	/* Hardwave Volume Step (0=1,1=2) */
3201da177e4SLinus Torvalds #define BA0_SSCR_MVCS		(1<<19)	/* Master Volume Codec Select */
3211da177e4SLinus Torvalds #define BA0_SSCR_MVLD		(1<<18)	/* Master Volume Line Out Disable */
3221da177e4SLinus Torvalds #define BA0_SSCR_MVAD		(1<<17)	/* Master Volume Alternate Out Disable */
3231da177e4SLinus Torvalds #define BA0_SSCR_MVMD		(1<<16)	/* Master Volume Mono Out Disable */
3241da177e4SLinus Torvalds #define BA0_SSCR_XLPSRC		(1<<8)	/* External SRC Loopback Mode */
3251da177e4SLinus Torvalds #define BA0_SSCR_LPSRC		(1<<7)	/* SRC Loopback Mode */
3261da177e4SLinus Torvalds #define BA0_SSCR_CDTX		(1<<5)	/* CD Transfer Data */
3271da177e4SLinus Torvalds #define BA0_SSCR_HVC		(1<<3)	/* Harware Volume Control Enable */
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds #define BA0_FMLVC		0x0754	/* FM Synthesis Left Volume Control */
3301da177e4SLinus Torvalds #define BA0_FMRVC		0x0758	/* FM Synthesis Right Volume Control */
3311da177e4SLinus Torvalds #define BA0_SRCSA		0x075c	/* SRC Slot Assignments */
3321da177e4SLinus Torvalds #define BA0_PPLVC		0x0760	/* PCM Playback Left Volume Control */
3331da177e4SLinus Torvalds #define BA0_PPRVC		0x0764	/* PCM Playback Right Volume Control */
3341da177e4SLinus Torvalds #define BA0_PASR		0x0768	/* playback sample rate */
3351da177e4SLinus Torvalds #define BA0_CASR		0x076C	/* capture sample rate */
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds /* Source Slot Numbers - Playback */
3381da177e4SLinus Torvalds #define SRCSLOT_LEFT_PCM_PLAYBACK		0
3391da177e4SLinus Torvalds #define SRCSLOT_RIGHT_PCM_PLAYBACK		1
3401da177e4SLinus Torvalds #define SRCSLOT_PHONE_LINE_1_DAC		2
3411da177e4SLinus Torvalds #define SRCSLOT_CENTER_PCM_PLAYBACK		3
3421da177e4SLinus Torvalds #define SRCSLOT_LEFT_SURROUND_PCM_PLAYBACK	4
3431da177e4SLinus Torvalds #define SRCSLOT_RIGHT_SURROUND_PCM_PLAYBACK	5
3441da177e4SLinus Torvalds #define SRCSLOT_LFE_PCM_PLAYBACK		6
3451da177e4SLinus Torvalds #define SRCSLOT_PHONE_LINE_2_DAC		7
3461da177e4SLinus Torvalds #define SRCSLOT_HEADSET_DAC			8
3471da177e4SLinus Torvalds #define SRCSLOT_LEFT_WT				29  /* invalid for BA0_SRCSA */
3481da177e4SLinus Torvalds #define SRCSLOT_RIGHT_WT			30  /* invalid for BA0_SRCSA */
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds /* Source Slot Numbers - Capture */
3511da177e4SLinus Torvalds #define SRCSLOT_LEFT_PCM_RECORD			10
3521da177e4SLinus Torvalds #define SRCSLOT_RIGHT_PCM_RECORD		11
3531da177e4SLinus Torvalds #define SRCSLOT_PHONE_LINE_1_ADC		12
3541da177e4SLinus Torvalds #define SRCSLOT_MIC_ADC				13
3551da177e4SLinus Torvalds #define SRCSLOT_PHONE_LINE_2_ADC		17
3561da177e4SLinus Torvalds #define SRCSLOT_HEADSET_ADC			18
3571da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_LEFT_PCM_RECORD	20
3581da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_RIGHT_PCM_RECORD	21
3591da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_PHONE_LINE_1_ADC	22
3601da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_MIC_ADC		23
3611da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_PHONE_LINE_2_ADC	27
3621da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_HEADSET_ADC		28
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds /* Source Slot Numbers - Others */
3651da177e4SLinus Torvalds #define SRCSLOT_POWER_DOWN			31
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds /* MIDI modes */
3681da177e4SLinus Torvalds #define CS4281_MODE_OUTPUT		(1<<0)
3691da177e4SLinus Torvalds #define CS4281_MODE_INPUT		(1<<1)
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds /* joystick bits */
3721da177e4SLinus Torvalds /* Bits for JSPT */
3731da177e4SLinus Torvalds #define JSPT_CAX                                0x00000001
3741da177e4SLinus Torvalds #define JSPT_CAY                                0x00000002
3751da177e4SLinus Torvalds #define JSPT_CBX                                0x00000004
3761da177e4SLinus Torvalds #define JSPT_CBY                                0x00000008
3771da177e4SLinus Torvalds #define JSPT_BA1                                0x00000010
3781da177e4SLinus Torvalds #define JSPT_BA2                                0x00000020
3791da177e4SLinus Torvalds #define JSPT_BB1                                0x00000040
3801da177e4SLinus Torvalds #define JSPT_BB2                                0x00000080
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds /* Bits for JSCTL */
3831da177e4SLinus Torvalds #define JSCTL_SP_MASK                           0x00000003
3841da177e4SLinus Torvalds #define JSCTL_SP_SLOW                           0x00000000
3851da177e4SLinus Torvalds #define JSCTL_SP_MEDIUM_SLOW                    0x00000001
3861da177e4SLinus Torvalds #define JSCTL_SP_MEDIUM_FAST                    0x00000002
3871da177e4SLinus Torvalds #define JSCTL_SP_FAST                           0x00000003
3881da177e4SLinus Torvalds #define JSCTL_ARE                               0x00000004
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /* Data register pairs masks */
3911da177e4SLinus Torvalds #define JSC1_Y1V_MASK                           0x0000FFFF
3921da177e4SLinus Torvalds #define JSC1_X1V_MASK                           0xFFFF0000
3931da177e4SLinus Torvalds #define JSC1_Y1V_SHIFT                          0
3941da177e4SLinus Torvalds #define JSC1_X1V_SHIFT                          16
3951da177e4SLinus Torvalds #define JSC2_Y2V_MASK                           0x0000FFFF
3961da177e4SLinus Torvalds #define JSC2_X2V_MASK                           0xFFFF0000
3971da177e4SLinus Torvalds #define JSC2_Y2V_SHIFT                          0
3981da177e4SLinus Torvalds #define JSC2_X2V_SHIFT                          16
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds /* JS GPIO */
4011da177e4SLinus Torvalds #define JSIO_DAX                                0x00000001
4021da177e4SLinus Torvalds #define JSIO_DAY                                0x00000002
4031da177e4SLinus Torvalds #define JSIO_DBX                                0x00000004
4041da177e4SLinus Torvalds #define JSIO_DBY                                0x00000008
4051da177e4SLinus Torvalds #define JSIO_AXOE                               0x00000010
4061da177e4SLinus Torvalds #define JSIO_AYOE                               0x00000020
4071da177e4SLinus Torvalds #define JSIO_BXOE                               0x00000040
4081da177e4SLinus Torvalds #define JSIO_BYOE                               0x00000080
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /*
4111da177e4SLinus Torvalds  *
4121da177e4SLinus Torvalds  */
4131da177e4SLinus Torvalds 
41493e35f95STakashi Iwai struct cs4281_dma {
41593e35f95STakashi Iwai 	struct snd_pcm_substream *substream;
4161da177e4SLinus Torvalds 	unsigned int regDBA;		/* offset to DBA register */
4171da177e4SLinus Torvalds 	unsigned int regDCA;		/* offset to DCA register */
4181da177e4SLinus Torvalds 	unsigned int regDBC;		/* offset to DBC register */
4191da177e4SLinus Torvalds 	unsigned int regDCC;		/* offset to DCC register */
4201da177e4SLinus Torvalds 	unsigned int regDMR;		/* offset to DMR register */
4211da177e4SLinus Torvalds 	unsigned int regDCR;		/* offset to DCR register */
4221da177e4SLinus Torvalds 	unsigned int regHDSR;		/* offset to HDSR register */
4231da177e4SLinus Torvalds 	unsigned int regFCR;		/* offset to FCR register */
4241da177e4SLinus Torvalds 	unsigned int regFSIC;		/* offset to FSIC register */
4251da177e4SLinus Torvalds 	unsigned int valDMR;		/* DMA mode */
4261da177e4SLinus Torvalds 	unsigned int valDCR;		/* DMA command */
4271da177e4SLinus Torvalds 	unsigned int valFCR;		/* FIFO control */
4281da177e4SLinus Torvalds 	unsigned int fifo_offset;	/* FIFO offset within BA1 */
4291da177e4SLinus Torvalds 	unsigned char left_slot;	/* FIFO left slot */
4301da177e4SLinus Torvalds 	unsigned char right_slot;	/* FIFO right slot */
4311da177e4SLinus Torvalds 	int frag;			/* period number */
4321da177e4SLinus Torvalds };
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds #define SUSPEND_REGISTERS	20
4351da177e4SLinus Torvalds 
43693e35f95STakashi Iwai struct cs4281 {
4371da177e4SLinus Torvalds 	int irq;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	void __iomem *ba0;		/* virtual (accessible) address */
4401da177e4SLinus Torvalds 	void __iomem *ba1;		/* virtual (accessible) address */
4411da177e4SLinus Torvalds 	unsigned long ba0_addr;
4421da177e4SLinus Torvalds 	unsigned long ba1_addr;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	int dual_codec;
4451da177e4SLinus Torvalds 
44693e35f95STakashi Iwai 	struct snd_ac97_bus *ac97_bus;
44793e35f95STakashi Iwai 	struct snd_ac97 *ac97;
44893e35f95STakashi Iwai 	struct snd_ac97 *ac97_secondary;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	struct pci_dev *pci;
45193e35f95STakashi Iwai 	struct snd_card *card;
45293e35f95STakashi Iwai 	struct snd_pcm *pcm;
45393e35f95STakashi Iwai 	struct snd_rawmidi *rmidi;
45493e35f95STakashi Iwai 	struct snd_rawmidi_substream *midi_input;
45593e35f95STakashi Iwai 	struct snd_rawmidi_substream *midi_output;
4561da177e4SLinus Torvalds 
45793e35f95STakashi Iwai 	struct cs4281_dma dma[4];
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	unsigned char src_left_play_slot;
4601da177e4SLinus Torvalds 	unsigned char src_right_play_slot;
4611da177e4SLinus Torvalds 	unsigned char src_left_rec_slot;
4621da177e4SLinus Torvalds 	unsigned char src_right_rec_slot;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	unsigned int spurious_dhtc_irq;
4651da177e4SLinus Torvalds 	unsigned int spurious_dtc_irq;
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	spinlock_t reg_lock;
4681da177e4SLinus Torvalds 	unsigned int midcr;
4691da177e4SLinus Torvalds 	unsigned int uartm;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	struct gameport *gameport;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	u32 suspend_regs[SUSPEND_REGISTERS];
4741da177e4SLinus Torvalds };
4751da177e4SLinus Torvalds 
4767d12e780SDavid Howells static irqreturn_t snd_cs4281_interrupt(int irq, void *dev_id);
4771da177e4SLinus Torvalds 
4789baa3c34SBenoit Taine static const struct pci_device_id snd_cs4281_ids[] = {
47928d27aaeSJoe Perches 	{ PCI_VDEVICE(CIRRUS, 0x6005), 0, },	/* CS4281 */
4801da177e4SLinus Torvalds 	{ 0, }
4811da177e4SLinus Torvalds };
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_cs4281_ids);
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /*
4861da177e4SLinus Torvalds  *  constants
4871da177e4SLinus Torvalds  */
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds #define CS4281_FIFO_SIZE	32
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds /*
4921da177e4SLinus Torvalds  *  common I/O routines
4931da177e4SLinus Torvalds  */
4941da177e4SLinus Torvalds 
snd_cs4281_pokeBA0(struct cs4281 * chip,unsigned long offset,unsigned int val)49593e35f95STakashi Iwai static inline void snd_cs4281_pokeBA0(struct cs4281 *chip, unsigned long offset,
49693e35f95STakashi Iwai 				      unsigned int val)
4971da177e4SLinus Torvalds {
4981da177e4SLinus Torvalds         writel(val, chip->ba0 + offset);
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
snd_cs4281_peekBA0(struct cs4281 * chip,unsigned long offset)50193e35f95STakashi Iwai static inline unsigned int snd_cs4281_peekBA0(struct cs4281 *chip, unsigned long offset)
5021da177e4SLinus Torvalds {
5031da177e4SLinus Torvalds         return readl(chip->ba0 + offset);
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
snd_cs4281_ac97_write(struct snd_ac97 * ac97,unsigned short reg,unsigned short val)50693e35f95STakashi Iwai static void snd_cs4281_ac97_write(struct snd_ac97 *ac97,
5071da177e4SLinus Torvalds 				  unsigned short reg, unsigned short val)
5081da177e4SLinus Torvalds {
5091da177e4SLinus Torvalds 	/*
5101da177e4SLinus Torvalds 	 *  1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
5111da177e4SLinus Torvalds 	 *  2. Write ACCDA = Command Data Register = 470h    for data to write to AC97
5121da177e4SLinus Torvalds 	 *  3. Write ACCTL = Control Register = 460h for initiating the write
5131da177e4SLinus Torvalds 	 *  4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
5141da177e4SLinus Torvalds 	 *  5. if DCV not cleared, break and return error
5151da177e4SLinus Torvalds 	 */
51693e35f95STakashi Iwai 	struct cs4281 *chip = ac97->private_data;
5171da177e4SLinus Torvalds 	int count;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	/*
5201da177e4SLinus Torvalds 	 *  Setup the AC97 control registers on the CS461x to send the
5211da177e4SLinus Torvalds 	 *  appropriate command to the AC97 to perform the read.
5221da177e4SLinus Torvalds 	 *  ACCAD = Command Address Register = 46Ch
5231da177e4SLinus Torvalds 	 *  ACCDA = Command Data Register = 470h
5241da177e4SLinus Torvalds 	 *  ACCTL = Control Register = 460h
5251da177e4SLinus Torvalds 	 *  set DCV - will clear when process completed
5261da177e4SLinus Torvalds 	 *  reset CRW - Write command
5271da177e4SLinus Torvalds 	 *  set VFRM - valid frame enabled
5281da177e4SLinus Torvalds 	 *  set ESYN - ASYNC generation enabled
5291da177e4SLinus Torvalds 	 *  set RSTN - ARST# inactive, AC97 codec not reset
5301da177e4SLinus Torvalds          */
5311da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCAD, reg);
5321da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCDA, val);
5331da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, BA0_ACCTL_DCV | BA0_ACCTL_VFRM |
5341da177e4SLinus Torvalds 				            BA0_ACCTL_ESYN | (ac97->num ? BA0_ACCTL_TC : 0));
5351da177e4SLinus Torvalds 	for (count = 0; count < 2000; count++) {
5361da177e4SLinus Torvalds 		/*
5371da177e4SLinus Torvalds 		 *  First, we want to wait for a short time.
5381da177e4SLinus Torvalds 		 */
5391da177e4SLinus Torvalds 		udelay(10);
5401da177e4SLinus Torvalds 		/*
5411da177e4SLinus Torvalds 		 *  Now, check to see if the write has completed.
5421da177e4SLinus Torvalds 		 *  ACCTL = 460h, DCV should be reset by now and 460h = 07h
5431da177e4SLinus Torvalds 		 */
5441da177e4SLinus Torvalds 		if (!(snd_cs4281_peekBA0(chip, BA0_ACCTL) & BA0_ACCTL_DCV)) {
5451da177e4SLinus Torvalds 			return;
5461da177e4SLinus Torvalds 		}
5471da177e4SLinus Torvalds 	}
548b055e7b4STakashi Iwai 	dev_err(chip->card->dev,
549b055e7b4STakashi Iwai 		"AC'97 write problem, reg = 0x%x, val = 0x%x\n", reg, val);
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
snd_cs4281_ac97_read(struct snd_ac97 * ac97,unsigned short reg)55293e35f95STakashi Iwai static unsigned short snd_cs4281_ac97_read(struct snd_ac97 *ac97,
5531da177e4SLinus Torvalds 					   unsigned short reg)
5541da177e4SLinus Torvalds {
55593e35f95STakashi Iwai 	struct cs4281 *chip = ac97->private_data;
5561da177e4SLinus Torvalds 	int count;
5571da177e4SLinus Torvalds 	unsigned short result;
5581da177e4SLinus Torvalds 	// FIXME: volatile is necessary in the following due to a bug of
5591da177e4SLinus Torvalds 	// some gcc versions
56093e35f95STakashi Iwai 	volatile int ac97_num = ((volatile struct snd_ac97 *)ac97)->num;
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	/*
5631da177e4SLinus Torvalds 	 *  1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
5641da177e4SLinus Torvalds 	 *  2. Write ACCDA = Command Data Register = 470h    for data to write to AC97
5651da177e4SLinus Torvalds 	 *  3. Write ACCTL = Control Register = 460h for initiating the write
5661da177e4SLinus Torvalds 	 *  4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
5671da177e4SLinus Torvalds 	 *  5. if DCV not cleared, break and return error
5681da177e4SLinus Torvalds 	 *  6. Read ACSTS = Status Register = 464h, check VSTS bit
5691da177e4SLinus Torvalds 	 */
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	snd_cs4281_peekBA0(chip, ac97_num ? BA0_ACSDA2 : BA0_ACSDA);
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	/*
5741da177e4SLinus Torvalds 	 *  Setup the AC97 control registers on the CS461x to send the
5751da177e4SLinus Torvalds 	 *  appropriate command to the AC97 to perform the read.
5761da177e4SLinus Torvalds 	 *  ACCAD = Command Address Register = 46Ch
5771da177e4SLinus Torvalds 	 *  ACCDA = Command Data Register = 470h
5781da177e4SLinus Torvalds 	 *  ACCTL = Control Register = 460h
5791da177e4SLinus Torvalds 	 *  set DCV - will clear when process completed
5801da177e4SLinus Torvalds 	 *  set CRW - Read command
5811da177e4SLinus Torvalds 	 *  set VFRM - valid frame enabled
5821da177e4SLinus Torvalds 	 *  set ESYN - ASYNC generation enabled
5831da177e4SLinus Torvalds 	 *  set RSTN - ARST# inactive, AC97 codec not reset
5841da177e4SLinus Torvalds 	 */
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCAD, reg);
5871da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCDA, 0);
5881da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, BA0_ACCTL_DCV | BA0_ACCTL_CRW |
5891da177e4SLinus Torvalds 					    BA0_ACCTL_VFRM | BA0_ACCTL_ESYN |
5901da177e4SLinus Torvalds 			   (ac97_num ? BA0_ACCTL_TC : 0));
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	/*
5941da177e4SLinus Torvalds 	 *  Wait for the read to occur.
5951da177e4SLinus Torvalds 	 */
5961da177e4SLinus Torvalds 	for (count = 0; count < 500; count++) {
5971da177e4SLinus Torvalds 		/*
5981da177e4SLinus Torvalds 		 *  First, we want to wait for a short time.
5991da177e4SLinus Torvalds 	 	 */
6001da177e4SLinus Torvalds 		udelay(10);
6011da177e4SLinus Torvalds 		/*
6021da177e4SLinus Torvalds 		 *  Now, check to see if the read has completed.
6031da177e4SLinus Torvalds 		 *  ACCTL = 460h, DCV should be reset by now and 460h = 17h
6041da177e4SLinus Torvalds 		 */
6051da177e4SLinus Torvalds 		if (!(snd_cs4281_peekBA0(chip, BA0_ACCTL) & BA0_ACCTL_DCV))
6061da177e4SLinus Torvalds 			goto __ok1;
6071da177e4SLinus Torvalds 	}
6081da177e4SLinus Torvalds 
609b055e7b4STakashi Iwai 	dev_err(chip->card->dev,
610b055e7b4STakashi Iwai 		"AC'97 read problem (ACCTL_DCV), reg = 0x%x\n", reg);
6111da177e4SLinus Torvalds 	result = 0xffff;
6121da177e4SLinus Torvalds 	goto __end;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds       __ok1:
6151da177e4SLinus Torvalds 	/*
6161da177e4SLinus Torvalds 	 *  Wait for the valid status bit to go active.
6171da177e4SLinus Torvalds 	 */
6181da177e4SLinus Torvalds 	for (count = 0; count < 100; count++) {
6191da177e4SLinus Torvalds 		/*
6201da177e4SLinus Torvalds 		 *  Read the AC97 status register.
6211da177e4SLinus Torvalds 		 *  ACSTS = Status Register = 464h
6221da177e4SLinus Torvalds 		 *  VSTS - Valid Status
6231da177e4SLinus Torvalds 		 */
6241da177e4SLinus Torvalds 		if (snd_cs4281_peekBA0(chip, ac97_num ? BA0_ACSTS2 : BA0_ACSTS) & BA0_ACSTS_VSTS)
6251da177e4SLinus Torvalds 			goto __ok2;
6261da177e4SLinus Torvalds 		udelay(10);
6271da177e4SLinus Torvalds 	}
6281da177e4SLinus Torvalds 
629b055e7b4STakashi Iwai 	dev_err(chip->card->dev,
630b055e7b4STakashi Iwai 		"AC'97 read problem (ACSTS_VSTS), reg = 0x%x\n", reg);
6311da177e4SLinus Torvalds 	result = 0xffff;
6321da177e4SLinus Torvalds 	goto __end;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds       __ok2:
6351da177e4SLinus Torvalds 	/*
6361da177e4SLinus Torvalds 	 *  Read the data returned from the AC97 register.
6371da177e4SLinus Torvalds 	 *  ACSDA = Status Data Register = 474h
6381da177e4SLinus Torvalds 	 */
6391da177e4SLinus Torvalds 	result = snd_cs4281_peekBA0(chip, ac97_num ? BA0_ACSDA2 : BA0_ACSDA);
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds       __end:
6421da177e4SLinus Torvalds 	return result;
6431da177e4SLinus Torvalds }
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds /*
6461da177e4SLinus Torvalds  *  PCM part
6471da177e4SLinus Torvalds  */
6481da177e4SLinus Torvalds 
snd_cs4281_trigger(struct snd_pcm_substream * substream,int cmd)64993e35f95STakashi Iwai static int snd_cs4281_trigger(struct snd_pcm_substream *substream, int cmd)
6501da177e4SLinus Torvalds {
65193e35f95STakashi Iwai 	struct cs4281_dma *dma = substream->runtime->private_data;
65293e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
6551da177e4SLinus Torvalds 	switch (cmd) {
6561da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
6571da177e4SLinus Torvalds 		dma->valDCR |= BA0_DCR_MSK;
6581da177e4SLinus Torvalds 		dma->valFCR |= BA0_FCR_FEN;
6591da177e4SLinus Torvalds 		break;
6601da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
6611da177e4SLinus Torvalds 		dma->valDCR &= ~BA0_DCR_MSK;
6621da177e4SLinus Torvalds 		dma->valFCR &= ~BA0_FCR_FEN;
6631da177e4SLinus Torvalds 		break;
6641da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
6651da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_RESUME:
6661da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, dma->regDMR, dma->valDMR & ~BA0_DMR_DMA);
6671da177e4SLinus Torvalds 		dma->valDMR |= BA0_DMR_DMA;
6681da177e4SLinus Torvalds 		dma->valDCR &= ~BA0_DCR_MSK;
6691da177e4SLinus Torvalds 		dma->valFCR |= BA0_FCR_FEN;
6701da177e4SLinus Torvalds 		break;
6711da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
6721da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_SUSPEND:
6731da177e4SLinus Torvalds 		dma->valDMR &= ~(BA0_DMR_DMA|BA0_DMR_POLL);
6741da177e4SLinus Torvalds 		dma->valDCR |= BA0_DCR_MSK;
6751da177e4SLinus Torvalds 		dma->valFCR &= ~BA0_FCR_FEN;
6761da177e4SLinus Torvalds 		/* Leave wave playback FIFO enabled for FM */
6771da177e4SLinus Torvalds 		if (dma->regFCR != BA0_FCR0)
6781da177e4SLinus Torvalds 			dma->valFCR &= ~BA0_FCR_FEN;
6791da177e4SLinus Torvalds 		break;
6801da177e4SLinus Torvalds 	default:
6811da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
6821da177e4SLinus Torvalds 		return -EINVAL;
6831da177e4SLinus Torvalds 	}
6841da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regDMR, dma->valDMR);
6851da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regFCR, dma->valFCR);
6861da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regDCR, dma->valDCR);
6871da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
6881da177e4SLinus Torvalds 	return 0;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
snd_cs4281_rate(unsigned int rate,unsigned int * real_rate)6911da177e4SLinus Torvalds static unsigned int snd_cs4281_rate(unsigned int rate, unsigned int *real_rate)
6921da177e4SLinus Torvalds {
693388b00f0SColin Ian King 	unsigned int val;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	if (real_rate)
6961da177e4SLinus Torvalds 		*real_rate = rate;
6971da177e4SLinus Torvalds 	/* special "hardcoded" rates */
6981da177e4SLinus Torvalds 	switch (rate) {
6991da177e4SLinus Torvalds 	case 8000:	return 5;
7001da177e4SLinus Torvalds 	case 11025:	return 4;
7011da177e4SLinus Torvalds 	case 16000:	return 3;
7021da177e4SLinus Torvalds 	case 22050:	return 2;
7031da177e4SLinus Torvalds 	case 44100:	return 1;
7041da177e4SLinus Torvalds 	case 48000:	return 0;
7051da177e4SLinus Torvalds 	default:
706388b00f0SColin Ian King 		break;
7071da177e4SLinus Torvalds 	}
7081da177e4SLinus Torvalds 	val = 1536000 / rate;
7091da177e4SLinus Torvalds 	if (real_rate)
7101da177e4SLinus Torvalds 		*real_rate = 1536000 / val;
7111da177e4SLinus Torvalds 	return val;
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
snd_cs4281_mode(struct cs4281 * chip,struct cs4281_dma * dma,struct snd_pcm_runtime * runtime,int capture,int src)71493e35f95STakashi Iwai static void snd_cs4281_mode(struct cs4281 *chip, struct cs4281_dma *dma,
71593e35f95STakashi Iwai 			    struct snd_pcm_runtime *runtime,
71693e35f95STakashi Iwai 			    int capture, int src)
7171da177e4SLinus Torvalds {
7181da177e4SLinus Torvalds 	int rec_mono;
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	dma->valDMR = BA0_DMR_TYPE_SINGLE | BA0_DMR_AUTO |
7211da177e4SLinus Torvalds 		      (capture ? BA0_DMR_TR_WRITE : BA0_DMR_TR_READ);
7221da177e4SLinus Torvalds 	if (runtime->channels == 1)
7231da177e4SLinus Torvalds 		dma->valDMR |= BA0_DMR_MONO;
7241da177e4SLinus Torvalds 	if (snd_pcm_format_unsigned(runtime->format) > 0)
7251da177e4SLinus Torvalds 		dma->valDMR |= BA0_DMR_USIGN;
7261da177e4SLinus Torvalds 	if (snd_pcm_format_big_endian(runtime->format) > 0)
7271da177e4SLinus Torvalds 		dma->valDMR |= BA0_DMR_BEND;
7281da177e4SLinus Torvalds 	switch (snd_pcm_format_width(runtime->format)) {
7291da177e4SLinus Torvalds 	case 8: dma->valDMR |= BA0_DMR_SIZE8;
7301da177e4SLinus Torvalds 		if (runtime->channels == 1)
7311da177e4SLinus Torvalds 			dma->valDMR |= BA0_DMR_SWAPC;
7321da177e4SLinus Torvalds 		break;
7331da177e4SLinus Torvalds 	case 32: dma->valDMR |= BA0_DMR_SIZE20; break;
7341da177e4SLinus Torvalds 	}
7351da177e4SLinus Torvalds 	dma->frag = 0;	/* for workaround */
7361da177e4SLinus Torvalds 	dma->valDCR = BA0_DCR_TCIE | BA0_DCR_MSK;
7371da177e4SLinus Torvalds 	if (runtime->buffer_size != runtime->period_size)
7381da177e4SLinus Torvalds 		dma->valDCR |= BA0_DCR_HTCIE;
7391da177e4SLinus Torvalds 	/* Initialize DMA */
7401da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regDBA, runtime->dma_addr);
7411da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regDBC, runtime->buffer_size - 1);
7421da177e4SLinus Torvalds 	rec_mono = (chip->dma[1].valDMR & BA0_DMR_MONO) == BA0_DMR_MONO;
7431da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SRCSA, (chip->src_left_play_slot << 0) |
7441da177e4SLinus Torvalds 					    (chip->src_right_play_slot << 8) |
7451da177e4SLinus Torvalds 					    (chip->src_left_rec_slot << 16) |
7461da177e4SLinus Torvalds 					    ((rec_mono ? 31 : chip->src_right_rec_slot) << 24));
7471da177e4SLinus Torvalds 	if (!src)
7481da177e4SLinus Torvalds 		goto __skip_src;
7491da177e4SLinus Torvalds 	if (!capture) {
7501da177e4SLinus Torvalds 		if (dma->left_slot == chip->src_left_play_slot) {
7511da177e4SLinus Torvalds 			unsigned int val = snd_cs4281_rate(runtime->rate, NULL);
752da3cec35STakashi Iwai 			snd_BUG_ON(dma->right_slot != chip->src_right_play_slot);
7531da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_DACSR, val);
7541da177e4SLinus Torvalds 		}
7551da177e4SLinus Torvalds 	} else {
7561da177e4SLinus Torvalds 		if (dma->left_slot == chip->src_left_rec_slot) {
7571da177e4SLinus Torvalds 			unsigned int val = snd_cs4281_rate(runtime->rate, NULL);
758da3cec35STakashi Iwai 			snd_BUG_ON(dma->right_slot != chip->src_right_rec_slot);
7591da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_ADCSR, val);
7601da177e4SLinus Torvalds 		}
7611da177e4SLinus Torvalds 	}
7621da177e4SLinus Torvalds       __skip_src:
7631da177e4SLinus Torvalds 	/* Deactivate wave playback FIFO before changing slot assignments */
7641da177e4SLinus Torvalds 	if (dma->regFCR == BA0_FCR0)
7651da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, dma->regFCR, snd_cs4281_peekBA0(chip, dma->regFCR) & ~BA0_FCR_FEN);
7661da177e4SLinus Torvalds 	/* Initialize FIFO */
7671da177e4SLinus Torvalds 	dma->valFCR = BA0_FCR_LS(dma->left_slot) |
7681da177e4SLinus Torvalds 		      BA0_FCR_RS(capture && (dma->valDMR & BA0_DMR_MONO) ? 31 : dma->right_slot) |
7691da177e4SLinus Torvalds 		      BA0_FCR_SZ(CS4281_FIFO_SIZE) |
7701da177e4SLinus Torvalds 		      BA0_FCR_OF(dma->fifo_offset);
7711da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regFCR, dma->valFCR | (capture ? BA0_FCR_PSH : 0));
7721da177e4SLinus Torvalds 	/* Activate FIFO again for FM playback */
7731da177e4SLinus Torvalds 	if (dma->regFCR == BA0_FCR0)
7741da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, dma->regFCR, dma->valFCR | BA0_FCR_FEN);
7751da177e4SLinus Torvalds 	/* Clear FIFO Status and Interrupt Control Register */
7761da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regFSIC, 0);
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds 
snd_cs4281_playback_prepare(struct snd_pcm_substream * substream)77993e35f95STakashi Iwai static int snd_cs4281_playback_prepare(struct snd_pcm_substream *substream)
7801da177e4SLinus Torvalds {
78193e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
78293e35f95STakashi Iwai 	struct cs4281_dma *dma = runtime->private_data;
78393e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
7861da177e4SLinus Torvalds 	snd_cs4281_mode(chip, dma, runtime, 0, 1);
7871da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
7881da177e4SLinus Torvalds 	return 0;
7891da177e4SLinus Torvalds }
7901da177e4SLinus Torvalds 
snd_cs4281_capture_prepare(struct snd_pcm_substream * substream)79193e35f95STakashi Iwai static int snd_cs4281_capture_prepare(struct snd_pcm_substream *substream)
7921da177e4SLinus Torvalds {
79393e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
79493e35f95STakashi Iwai 	struct cs4281_dma *dma = runtime->private_data;
79593e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
7981da177e4SLinus Torvalds 	snd_cs4281_mode(chip, dma, runtime, 1, 1);
7991da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
8001da177e4SLinus Torvalds 	return 0;
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
snd_cs4281_pointer(struct snd_pcm_substream * substream)80393e35f95STakashi Iwai static snd_pcm_uframes_t snd_cs4281_pointer(struct snd_pcm_substream *substream)
8041da177e4SLinus Torvalds {
80593e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
80693e35f95STakashi Iwai 	struct cs4281_dma *dma = runtime->private_data;
80793e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
8081da177e4SLinus Torvalds 
809ee419653STakashi Iwai 	/*
810b055e7b4STakashi Iwai 	dev_dbg(chip->card->dev,
811b055e7b4STakashi Iwai 		"DCC = 0x%x, buffer_size = 0x%x, jiffies = %li\n",
812ee419653STakashi Iwai 		snd_cs4281_peekBA0(chip, dma->regDCC), runtime->buffer_size,
813ee419653STakashi Iwai 	       jiffies);
814ee419653STakashi Iwai 	*/
8151da177e4SLinus Torvalds 	return runtime->buffer_size -
8161da177e4SLinus Torvalds 	       snd_cs4281_peekBA0(chip, dma->regDCC) - 1;
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
819dee49895SBhumika Goyal static const struct snd_pcm_hardware snd_cs4281_playback =
8201da177e4SLinus Torvalds {
821b83f346bSClemens Ladisch 	.info =			SNDRV_PCM_INFO_MMAP |
8221da177e4SLinus Torvalds 				SNDRV_PCM_INFO_INTERLEAVED |
8231da177e4SLinus Torvalds 				SNDRV_PCM_INFO_MMAP_VALID |
8241da177e4SLinus Torvalds 				SNDRV_PCM_INFO_PAUSE |
825b83f346bSClemens Ladisch 				SNDRV_PCM_INFO_RESUME,
8261da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
8271da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
8281da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
8291da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
8301da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE,
8311da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8321da177e4SLinus Torvalds 	.rate_min =		4000,
8331da177e4SLinus Torvalds 	.rate_max =		48000,
8341da177e4SLinus Torvalds 	.channels_min =		1,
8351da177e4SLinus Torvalds 	.channels_max =		2,
8361da177e4SLinus Torvalds 	.buffer_bytes_max =	(512*1024),
8371da177e4SLinus Torvalds 	.period_bytes_min =	64,
8381da177e4SLinus Torvalds 	.period_bytes_max =	(512*1024),
8391da177e4SLinus Torvalds 	.periods_min =		1,
8401da177e4SLinus Torvalds 	.periods_max =		2,
8411da177e4SLinus Torvalds 	.fifo_size =		CS4281_FIFO_SIZE,
8421da177e4SLinus Torvalds };
8431da177e4SLinus Torvalds 
844dee49895SBhumika Goyal static const struct snd_pcm_hardware snd_cs4281_capture =
8451da177e4SLinus Torvalds {
846b83f346bSClemens Ladisch 	.info =			SNDRV_PCM_INFO_MMAP |
8471da177e4SLinus Torvalds 				SNDRV_PCM_INFO_INTERLEAVED |
8481da177e4SLinus Torvalds 				SNDRV_PCM_INFO_MMAP_VALID |
8491da177e4SLinus Torvalds 				SNDRV_PCM_INFO_PAUSE |
850b83f346bSClemens Ladisch 				SNDRV_PCM_INFO_RESUME,
8511da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
8521da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
8531da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
8541da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
8551da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE,
8561da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8571da177e4SLinus Torvalds 	.rate_min =		4000,
8581da177e4SLinus Torvalds 	.rate_max =		48000,
8591da177e4SLinus Torvalds 	.channels_min =		1,
8601da177e4SLinus Torvalds 	.channels_max =		2,
8611da177e4SLinus Torvalds 	.buffer_bytes_max =	(512*1024),
8621da177e4SLinus Torvalds 	.period_bytes_min =	64,
8631da177e4SLinus Torvalds 	.period_bytes_max =	(512*1024),
8641da177e4SLinus Torvalds 	.periods_min =		1,
8651da177e4SLinus Torvalds 	.periods_max =		2,
8661da177e4SLinus Torvalds 	.fifo_size =		CS4281_FIFO_SIZE,
8671da177e4SLinus Torvalds };
8681da177e4SLinus Torvalds 
snd_cs4281_playback_open(struct snd_pcm_substream * substream)86993e35f95STakashi Iwai static int snd_cs4281_playback_open(struct snd_pcm_substream *substream)
8701da177e4SLinus Torvalds {
87193e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
87293e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
87393e35f95STakashi Iwai 	struct cs4281_dma *dma;
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 	dma = &chip->dma[0];
8761da177e4SLinus Torvalds 	dma->substream = substream;
8771da177e4SLinus Torvalds 	dma->left_slot = 0;
8781da177e4SLinus Torvalds 	dma->right_slot = 1;
8791da177e4SLinus Torvalds 	runtime->private_data = dma;
8801da177e4SLinus Torvalds 	runtime->hw = snd_cs4281_playback;
8811da177e4SLinus Torvalds 	/* should be detected from the AC'97 layer, but it seems
8821da177e4SLinus Torvalds 	   that although CS4297A rev B reports 18-bit ADC resolution,
8831da177e4SLinus Torvalds 	   samples are 20-bit */
8841da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 20);
8851da177e4SLinus Torvalds 	return 0;
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
snd_cs4281_capture_open(struct snd_pcm_substream * substream)88893e35f95STakashi Iwai static int snd_cs4281_capture_open(struct snd_pcm_substream *substream)
8891da177e4SLinus Torvalds {
89093e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
89193e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
89293e35f95STakashi Iwai 	struct cs4281_dma *dma;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	dma = &chip->dma[1];
8951da177e4SLinus Torvalds 	dma->substream = substream;
8961da177e4SLinus Torvalds 	dma->left_slot = 10;
8971da177e4SLinus Torvalds 	dma->right_slot = 11;
8981da177e4SLinus Torvalds 	runtime->private_data = dma;
8991da177e4SLinus Torvalds 	runtime->hw = snd_cs4281_capture;
9001da177e4SLinus Torvalds 	/* should be detected from the AC'97 layer, but it seems
9011da177e4SLinus Torvalds 	   that although CS4297A rev B reports 18-bit ADC resolution,
9021da177e4SLinus Torvalds 	   samples are 20-bit */
9031da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 20);
9041da177e4SLinus Torvalds 	return 0;
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
snd_cs4281_playback_close(struct snd_pcm_substream * substream)90793e35f95STakashi Iwai static int snd_cs4281_playback_close(struct snd_pcm_substream *substream)
9081da177e4SLinus Torvalds {
90993e35f95STakashi Iwai 	struct cs4281_dma *dma = substream->runtime->private_data;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	dma->substream = NULL;
9121da177e4SLinus Torvalds 	return 0;
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
snd_cs4281_capture_close(struct snd_pcm_substream * substream)91593e35f95STakashi Iwai static int snd_cs4281_capture_close(struct snd_pcm_substream *substream)
9161da177e4SLinus Torvalds {
91793e35f95STakashi Iwai 	struct cs4281_dma *dma = substream->runtime->private_data;
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	dma->substream = NULL;
9201da177e4SLinus Torvalds 	return 0;
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
9236769e988SJulia Lawall static const struct snd_pcm_ops snd_cs4281_playback_ops = {
9241da177e4SLinus Torvalds 	.open =		snd_cs4281_playback_open,
9251da177e4SLinus Torvalds 	.close =	snd_cs4281_playback_close,
9261da177e4SLinus Torvalds 	.prepare =	snd_cs4281_playback_prepare,
9271da177e4SLinus Torvalds 	.trigger =	snd_cs4281_trigger,
9281da177e4SLinus Torvalds 	.pointer =	snd_cs4281_pointer,
9291da177e4SLinus Torvalds };
9301da177e4SLinus Torvalds 
9316769e988SJulia Lawall static const struct snd_pcm_ops snd_cs4281_capture_ops = {
9321da177e4SLinus Torvalds 	.open =		snd_cs4281_capture_open,
9331da177e4SLinus Torvalds 	.close =	snd_cs4281_capture_close,
9341da177e4SLinus Torvalds 	.prepare =	snd_cs4281_capture_prepare,
9351da177e4SLinus Torvalds 	.trigger =	snd_cs4281_trigger,
9361da177e4SLinus Torvalds 	.pointer =	snd_cs4281_pointer,
9371da177e4SLinus Torvalds };
9381da177e4SLinus Torvalds 
snd_cs4281_pcm(struct cs4281 * chip,int device)9393e4f4776SLars-Peter Clausen static int snd_cs4281_pcm(struct cs4281 *chip, int device)
9401da177e4SLinus Torvalds {
94193e35f95STakashi Iwai 	struct snd_pcm *pcm;
9421da177e4SLinus Torvalds 	int err;
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	err = snd_pcm_new(chip->card, "CS4281", device, 1, 1, &pcm);
9451da177e4SLinus Torvalds 	if (err < 0)
9461da177e4SLinus Torvalds 		return err;
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs4281_playback_ops);
9491da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs4281_capture_ops);
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	pcm->private_data = chip;
9521da177e4SLinus Torvalds 	pcm->info_flags = 0;
9531da177e4SLinus Torvalds 	strcpy(pcm->name, "CS4281");
9541da177e4SLinus Torvalds 	chip->pcm = pcm;
9551da177e4SLinus Torvalds 
95644e78a1aSTakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
9576974f8adSTakashi Iwai 				       64*1024, 512*1024);
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	return 0;
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds /*
9631da177e4SLinus Torvalds  *  Mixer section
9641da177e4SLinus Torvalds  */
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds #define CS_VOL_MASK	0x1f
9671da177e4SLinus Torvalds 
snd_cs4281_info_volume(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)96893e35f95STakashi Iwai static int snd_cs4281_info_volume(struct snd_kcontrol *kcontrol,
96993e35f95STakashi Iwai 				  struct snd_ctl_elem_info *uinfo)
9701da177e4SLinus Torvalds {
9711da177e4SLinus Torvalds 	uinfo->type              = SNDRV_CTL_ELEM_TYPE_INTEGER;
9721da177e4SLinus Torvalds 	uinfo->count             = 2;
9731da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
9741da177e4SLinus Torvalds 	uinfo->value.integer.max = CS_VOL_MASK;
9751da177e4SLinus Torvalds 	return 0;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
snd_cs4281_get_volume(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)97893e35f95STakashi Iwai static int snd_cs4281_get_volume(struct snd_kcontrol *kcontrol,
97993e35f95STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
9801da177e4SLinus Torvalds {
98193e35f95STakashi Iwai 	struct cs4281 *chip = snd_kcontrol_chip(kcontrol);
9821da177e4SLinus Torvalds 	int regL = (kcontrol->private_value >> 16) & 0xffff;
9831da177e4SLinus Torvalds 	int regR = kcontrol->private_value & 0xffff;
9841da177e4SLinus Torvalds 	int volL, volR;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	volL = CS_VOL_MASK - (snd_cs4281_peekBA0(chip, regL) & CS_VOL_MASK);
9871da177e4SLinus Torvalds 	volR = CS_VOL_MASK - (snd_cs4281_peekBA0(chip, regR) & CS_VOL_MASK);
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = volL;
9901da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = volR;
9911da177e4SLinus Torvalds 	return 0;
9921da177e4SLinus Torvalds }
9931da177e4SLinus Torvalds 
snd_cs4281_put_volume(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)99493e35f95STakashi Iwai static int snd_cs4281_put_volume(struct snd_kcontrol *kcontrol,
99593e35f95STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
9961da177e4SLinus Torvalds {
99793e35f95STakashi Iwai 	struct cs4281 *chip = snd_kcontrol_chip(kcontrol);
9981da177e4SLinus Torvalds 	int change = 0;
9991da177e4SLinus Torvalds 	int regL = (kcontrol->private_value >> 16) & 0xffff;
10001da177e4SLinus Torvalds 	int regR = kcontrol->private_value & 0xffff;
10011da177e4SLinus Torvalds 	int volL, volR;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	volL = CS_VOL_MASK - (snd_cs4281_peekBA0(chip, regL) & CS_VOL_MASK);
10041da177e4SLinus Torvalds 	volR = CS_VOL_MASK - (snd_cs4281_peekBA0(chip, regR) & CS_VOL_MASK);
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	if (ucontrol->value.integer.value[0] != volL) {
10071da177e4SLinus Torvalds 		volL = CS_VOL_MASK - (ucontrol->value.integer.value[0] & CS_VOL_MASK);
10081da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, regL, volL);
10091da177e4SLinus Torvalds 		change = 1;
10101da177e4SLinus Torvalds 	}
1011e860f000STakashi Iwai 	if (ucontrol->value.integer.value[1] != volR) {
10121da177e4SLinus Torvalds 		volR = CS_VOL_MASK - (ucontrol->value.integer.value[1] & CS_VOL_MASK);
10131da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, regR, volR);
10141da177e4SLinus Torvalds 		change = 1;
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 	return change;
10171da177e4SLinus Torvalds }
10181da177e4SLinus Torvalds 
10190cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -4650, 150, 0);
10209f6ab250STakashi Iwai 
1021f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_cs4281_fm_vol =
10221da177e4SLinus Torvalds {
10231da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
10241da177e4SLinus Torvalds 	.name = "Synth Playback Volume",
10251da177e4SLinus Torvalds 	.info = snd_cs4281_info_volume,
10261da177e4SLinus Torvalds 	.get = snd_cs4281_get_volume,
10271da177e4SLinus Torvalds 	.put = snd_cs4281_put_volume,
10281da177e4SLinus Torvalds 	.private_value = ((BA0_FMLVC << 16) | BA0_FMRVC),
10299f6ab250STakashi Iwai 	.tlv = { .p = db_scale_dsp },
10301da177e4SLinus Torvalds };
10311da177e4SLinus Torvalds 
1032f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_cs4281_pcm_vol =
10331da177e4SLinus Torvalds {
10341da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
10351da177e4SLinus Torvalds 	.name = "PCM Stream Playback Volume",
10361da177e4SLinus Torvalds 	.info = snd_cs4281_info_volume,
10371da177e4SLinus Torvalds 	.get = snd_cs4281_get_volume,
10381da177e4SLinus Torvalds 	.put = snd_cs4281_put_volume,
10391da177e4SLinus Torvalds 	.private_value = ((BA0_PPLVC << 16) | BA0_PPRVC),
10409f6ab250STakashi Iwai 	.tlv = { .p = db_scale_dsp },
10411da177e4SLinus Torvalds };
10421da177e4SLinus Torvalds 
snd_cs4281_mixer_free_ac97_bus(struct snd_ac97_bus * bus)104393e35f95STakashi Iwai static void snd_cs4281_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
10441da177e4SLinus Torvalds {
104593e35f95STakashi Iwai 	struct cs4281 *chip = bus->private_data;
10461da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
snd_cs4281_mixer_free_ac97(struct snd_ac97 * ac97)104993e35f95STakashi Iwai static void snd_cs4281_mixer_free_ac97(struct snd_ac97 *ac97)
10501da177e4SLinus Torvalds {
105193e35f95STakashi Iwai 	struct cs4281 *chip = ac97->private_data;
10521da177e4SLinus Torvalds 	if (ac97->num)
10531da177e4SLinus Torvalds 		chip->ac97_secondary = NULL;
10541da177e4SLinus Torvalds 	else
10551da177e4SLinus Torvalds 		chip->ac97 = NULL;
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
snd_cs4281_mixer(struct cs4281 * chip)1058e23e7a14SBill Pemberton static int snd_cs4281_mixer(struct cs4281 *chip)
10591da177e4SLinus Torvalds {
106093e35f95STakashi Iwai 	struct snd_card *card = chip->card;
106193e35f95STakashi Iwai 	struct snd_ac97_template ac97;
10621da177e4SLinus Torvalds 	int err;
106351055da5STakashi Iwai 	static const struct snd_ac97_bus_ops ops = {
10641da177e4SLinus Torvalds 		.write = snd_cs4281_ac97_write,
10651da177e4SLinus Torvalds 		.read = snd_cs4281_ac97_read,
10661da177e4SLinus Torvalds 	};
10671da177e4SLinus Torvalds 
1068393e95cdSTakashi Iwai 	err = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
1069393e95cdSTakashi Iwai 	if (err < 0)
10701da177e4SLinus Torvalds 		return err;
10711da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_cs4281_mixer_free_ac97_bus;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
10741da177e4SLinus Torvalds 	ac97.private_data = chip;
10751da177e4SLinus Torvalds 	ac97.private_free = snd_cs4281_mixer_free_ac97;
1076393e95cdSTakashi Iwai 	err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
1077393e95cdSTakashi Iwai 	if (err < 0)
10781da177e4SLinus Torvalds 		return err;
10791da177e4SLinus Torvalds 	if (chip->dual_codec) {
10801da177e4SLinus Torvalds 		ac97.num = 1;
1081393e95cdSTakashi Iwai 		err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_secondary);
1082393e95cdSTakashi Iwai 		if (err < 0)
10831da177e4SLinus Torvalds 			return err;
10841da177e4SLinus Torvalds 	}
1085393e95cdSTakashi Iwai 	err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4281_fm_vol, chip));
1086393e95cdSTakashi Iwai 	if (err < 0)
10871da177e4SLinus Torvalds 		return err;
1088393e95cdSTakashi Iwai 	err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4281_pcm_vol, chip));
1089393e95cdSTakashi Iwai 	if (err < 0)
10901da177e4SLinus Torvalds 		return err;
10911da177e4SLinus Torvalds 	return 0;
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds /*
10961da177e4SLinus Torvalds  * proc interface
10971da177e4SLinus Torvalds  */
10981da177e4SLinus Torvalds 
snd_cs4281_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)109993e35f95STakashi Iwai static void snd_cs4281_proc_read(struct snd_info_entry *entry,
110093e35f95STakashi Iwai 				  struct snd_info_buffer *buffer)
11011da177e4SLinus Torvalds {
110293e35f95STakashi Iwai 	struct cs4281 *chip = entry->private_data;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	snd_iprintf(buffer, "Cirrus Logic CS4281\n\n");
11051da177e4SLinus Torvalds 	snd_iprintf(buffer, "Spurious half IRQs   : %u\n", chip->spurious_dhtc_irq);
11061da177e4SLinus Torvalds 	snd_iprintf(buffer, "Spurious end IRQs    : %u\n", chip->spurious_dtc_irq);
11071da177e4SLinus Torvalds }
11081da177e4SLinus Torvalds 
snd_cs4281_BA0_read(struct snd_info_entry * entry,void * file_private_data,struct file * file,char __user * buf,size_t count,loff_t pos)110924e4a121STakashi Iwai static ssize_t snd_cs4281_BA0_read(struct snd_info_entry *entry,
111093e35f95STakashi Iwai 				   void *file_private_data,
11111da177e4SLinus Torvalds 				   struct file *file, char __user *buf,
111224e4a121STakashi Iwai 				   size_t count, loff_t pos)
11131da177e4SLinus Torvalds {
111493e35f95STakashi Iwai 	struct cs4281 *chip = entry->private_data;
11151da177e4SLinus Torvalds 
1116d97e1b78STakashi Iwai 	if (copy_to_user_fromio(buf, chip->ba0 + pos, count))
11171da177e4SLinus Torvalds 		return -EFAULT;
1118d97e1b78STakashi Iwai 	return count;
11191da177e4SLinus Torvalds }
11201da177e4SLinus Torvalds 
snd_cs4281_BA1_read(struct snd_info_entry * entry,void * file_private_data,struct file * file,char __user * buf,size_t count,loff_t pos)112124e4a121STakashi Iwai static ssize_t snd_cs4281_BA1_read(struct snd_info_entry *entry,
112293e35f95STakashi Iwai 				   void *file_private_data,
11231da177e4SLinus Torvalds 				   struct file *file, char __user *buf,
112424e4a121STakashi Iwai 				   size_t count, loff_t pos)
11251da177e4SLinus Torvalds {
112693e35f95STakashi Iwai 	struct cs4281 *chip = entry->private_data;
11271da177e4SLinus Torvalds 
1128d97e1b78STakashi Iwai 	if (copy_to_user_fromio(buf, chip->ba1 + pos, count))
11291da177e4SLinus Torvalds 		return -EFAULT;
1130d97e1b78STakashi Iwai 	return count;
11311da177e4SLinus Torvalds }
11321da177e4SLinus Torvalds 
1133d25ff268STakashi Iwai static const struct snd_info_entry_ops snd_cs4281_proc_ops_BA0 = {
11341da177e4SLinus Torvalds 	.read = snd_cs4281_BA0_read,
11351da177e4SLinus Torvalds };
11361da177e4SLinus Torvalds 
1137d25ff268STakashi Iwai static const struct snd_info_entry_ops snd_cs4281_proc_ops_BA1 = {
11381da177e4SLinus Torvalds 	.read = snd_cs4281_BA1_read,
11391da177e4SLinus Torvalds };
11401da177e4SLinus Torvalds 
snd_cs4281_proc_init(struct cs4281 * chip)1141e23e7a14SBill Pemberton static void snd_cs4281_proc_init(struct cs4281 *chip)
11421da177e4SLinus Torvalds {
114393e35f95STakashi Iwai 	struct snd_info_entry *entry;
11441da177e4SLinus Torvalds 
114547f2769bSTakashi Iwai 	snd_card_ro_proc_new(chip->card, "cs4281", chip, snd_cs4281_proc_read);
11461da177e4SLinus Torvalds 	if (! snd_card_proc_new(chip->card, "cs4281_BA0", &entry)) {
11471da177e4SLinus Torvalds 		entry->content = SNDRV_INFO_CONTENT_DATA;
11481da177e4SLinus Torvalds 		entry->private_data = chip;
11491da177e4SLinus Torvalds 		entry->c.ops = &snd_cs4281_proc_ops_BA0;
11501da177e4SLinus Torvalds 		entry->size = CS4281_BA0_SIZE;
11511da177e4SLinus Torvalds 	}
11521da177e4SLinus Torvalds 	if (! snd_card_proc_new(chip->card, "cs4281_BA1", &entry)) {
11531da177e4SLinus Torvalds 		entry->content = SNDRV_INFO_CONTENT_DATA;
11541da177e4SLinus Torvalds 		entry->private_data = chip;
11551da177e4SLinus Torvalds 		entry->c.ops = &snd_cs4281_proc_ops_BA1;
11561da177e4SLinus Torvalds 		entry->size = CS4281_BA1_SIZE;
11571da177e4SLinus Torvalds 	}
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds /*
11611da177e4SLinus Torvalds  * joystick support
11621da177e4SLinus Torvalds  */
11631da177e4SLinus Torvalds 
1164b2fac073SFabian Frederick #if IS_REACHABLE(CONFIG_GAMEPORT)
11651da177e4SLinus Torvalds 
snd_cs4281_gameport_trigger(struct gameport * gameport)11661da177e4SLinus Torvalds static void snd_cs4281_gameport_trigger(struct gameport *gameport)
11671da177e4SLinus Torvalds {
116893e35f95STakashi Iwai 	struct cs4281 *chip = gameport_get_port_data(gameport);
11691da177e4SLinus Torvalds 
1170da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
1171da3cec35STakashi Iwai 		return;
11721da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_JSPT, 0xff);
11731da177e4SLinus Torvalds }
11741da177e4SLinus Torvalds 
snd_cs4281_gameport_read(struct gameport * gameport)11751da177e4SLinus Torvalds static unsigned char snd_cs4281_gameport_read(struct gameport *gameport)
11761da177e4SLinus Torvalds {
117793e35f95STakashi Iwai 	struct cs4281 *chip = gameport_get_port_data(gameport);
11781da177e4SLinus Torvalds 
1179da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
1180da3cec35STakashi Iwai 		return 0;
11811da177e4SLinus Torvalds 	return snd_cs4281_peekBA0(chip, BA0_JSPT);
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds #ifdef COOKED_MODE
snd_cs4281_gameport_cooked_read(struct gameport * gameport,int * axes,int * buttons)118593e35f95STakashi Iwai static int snd_cs4281_gameport_cooked_read(struct gameport *gameport,
118693e35f95STakashi Iwai 					   int *axes, int *buttons)
11871da177e4SLinus Torvalds {
118893e35f95STakashi Iwai 	struct cs4281 *chip = gameport_get_port_data(gameport);
11891da177e4SLinus Torvalds 	unsigned js1, js2, jst;
11901da177e4SLinus Torvalds 
1191da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
1192da3cec35STakashi Iwai 		return 0;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	js1 = snd_cs4281_peekBA0(chip, BA0_JSC1);
11951da177e4SLinus Torvalds 	js2 = snd_cs4281_peekBA0(chip, BA0_JSC2);
11961da177e4SLinus Torvalds 	jst = snd_cs4281_peekBA0(chip, BA0_JSPT);
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	*buttons = (~jst >> 4) & 0x0F;
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	axes[0] = ((js1 & JSC1_Y1V_MASK) >> JSC1_Y1V_SHIFT) & 0xFFFF;
12011da177e4SLinus Torvalds 	axes[1] = ((js1 & JSC1_X1V_MASK) >> JSC1_X1V_SHIFT) & 0xFFFF;
12021da177e4SLinus Torvalds 	axes[2] = ((js2 & JSC2_Y2V_MASK) >> JSC2_Y2V_SHIFT) & 0xFFFF;
12031da177e4SLinus Torvalds 	axes[3] = ((js2 & JSC2_X2V_MASK) >> JSC2_X2V_SHIFT) & 0xFFFF;
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	for (jst = 0; jst < 4; ++jst)
12061da177e4SLinus Torvalds 		if (axes[jst] == 0xFFFF) axes[jst] = -1;
12071da177e4SLinus Torvalds 	return 0;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds #else
12101da177e4SLinus Torvalds #define snd_cs4281_gameport_cooked_read	NULL
12111da177e4SLinus Torvalds #endif
12121da177e4SLinus Torvalds 
snd_cs4281_gameport_open(struct gameport * gameport,int mode)12131da177e4SLinus Torvalds static int snd_cs4281_gameport_open(struct gameport *gameport, int mode)
12141da177e4SLinus Torvalds {
12151da177e4SLinus Torvalds 	switch (mode) {
12161da177e4SLinus Torvalds #ifdef COOKED_MODE
12171da177e4SLinus Torvalds 	case GAMEPORT_MODE_COOKED:
12181da177e4SLinus Torvalds 		return 0;
12191da177e4SLinus Torvalds #endif
12201da177e4SLinus Torvalds 	case GAMEPORT_MODE_RAW:
12211da177e4SLinus Torvalds 		return 0;
12221da177e4SLinus Torvalds 	default:
12231da177e4SLinus Torvalds 		return -1;
12241da177e4SLinus Torvalds 	}
12251da177e4SLinus Torvalds 	return 0;
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds 
snd_cs4281_create_gameport(struct cs4281 * chip)1228e23e7a14SBill Pemberton static int snd_cs4281_create_gameport(struct cs4281 *chip)
12291da177e4SLinus Torvalds {
12301da177e4SLinus Torvalds 	struct gameport *gp;
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	chip->gameport = gp = gameport_allocate_port();
12331da177e4SLinus Torvalds 	if (!gp) {
1234b055e7b4STakashi Iwai 		dev_err(chip->card->dev,
1235b055e7b4STakashi Iwai 			"cannot allocate memory for gameport\n");
12361da177e4SLinus Torvalds 		return -ENOMEM;
12371da177e4SLinus Torvalds 	}
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	gameport_set_name(gp, "CS4281 Gameport");
12401da177e4SLinus Torvalds 	gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
12411da177e4SLinus Torvalds 	gameport_set_dev_parent(gp, &chip->pci->dev);
12421da177e4SLinus Torvalds 	gp->open = snd_cs4281_gameport_open;
12431da177e4SLinus Torvalds 	gp->read = snd_cs4281_gameport_read;
12441da177e4SLinus Torvalds 	gp->trigger = snd_cs4281_gameport_trigger;
12451da177e4SLinus Torvalds 	gp->cooked_read = snd_cs4281_gameport_cooked_read;
12461da177e4SLinus Torvalds 	gameport_set_port_data(gp, chip);
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_JSIO, 0xFF); // ?
12491da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_JSCTL, JSCTL_SP_MEDIUM_SLOW);
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	gameport_register_port(gp);
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	return 0;
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
snd_cs4281_free_gameport(struct cs4281 * chip)125693e35f95STakashi Iwai static void snd_cs4281_free_gameport(struct cs4281 *chip)
12571da177e4SLinus Torvalds {
12581da177e4SLinus Torvalds 	if (chip->gameport) {
12591da177e4SLinus Torvalds 		gameport_unregister_port(chip->gameport);
12601da177e4SLinus Torvalds 		chip->gameport = NULL;
12611da177e4SLinus Torvalds 	}
12621da177e4SLinus Torvalds }
12631da177e4SLinus Torvalds #else
snd_cs4281_create_gameport(struct cs4281 * chip)126493e35f95STakashi Iwai static inline int snd_cs4281_create_gameport(struct cs4281 *chip) { return -ENOSYS; }
snd_cs4281_free_gameport(struct cs4281 * chip)126593e35f95STakashi Iwai static inline void snd_cs4281_free_gameport(struct cs4281 *chip) { }
126666701170STakashi Iwai #endif /* IS_REACHABLE(CONFIG_GAMEPORT) */
12671da177e4SLinus Torvalds 
snd_cs4281_free(struct snd_card * card)126899041feaSTakashi Iwai static void snd_cs4281_free(struct snd_card *card)
12691da177e4SLinus Torvalds {
127099041feaSTakashi Iwai 	struct cs4281 *chip = card->private_data;
127199041feaSTakashi Iwai 
12721da177e4SLinus Torvalds 	snd_cs4281_free_gameport(chip);
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	/* Mask interrupts */
12751da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HIMR, 0x7fffffff);
12761da177e4SLinus Torvalds 	/* Stop the DLL Clock logic. */
12771da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, 0);
12781da177e4SLinus Torvalds 	/* Sound System Power Management - Turn Everything OFF */
12791da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SSPM, 0);
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
128293e35f95STakashi Iwai static int snd_cs4281_chip_init(struct cs4281 *chip); /* defined below */
12831da177e4SLinus Torvalds 
snd_cs4281_create(struct snd_card * card,struct pci_dev * pci,int dual_codec)1284e23e7a14SBill Pemberton static int snd_cs4281_create(struct snd_card *card,
12851da177e4SLinus Torvalds 			     struct pci_dev *pci,
12861da177e4SLinus Torvalds 			     int dual_codec)
12871da177e4SLinus Torvalds {
128835fe7901STakashi Iwai 	struct cs4281 *chip = card->private_data;
12891da177e4SLinus Torvalds 	int err;
12901da177e4SLinus Torvalds 
129199041feaSTakashi Iwai 	err = pcim_enable_device(pci);
1292393e95cdSTakashi Iwai 	if (err < 0)
12931da177e4SLinus Torvalds 		return err;
12941da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
12951da177e4SLinus Torvalds 	chip->card = card;
12961da177e4SLinus Torvalds 	chip->pci = pci;
12971da177e4SLinus Torvalds 	chip->irq = -1;
12981da177e4SLinus Torvalds 	pci_set_master(pci);
12991da177e4SLinus Torvalds 	if (dual_codec < 0 || dual_codec > 3) {
1300b055e7b4STakashi Iwai 		dev_err(card->dev, "invalid dual_codec option %d\n", dual_codec);
13011da177e4SLinus Torvalds 		dual_codec = 0;
13021da177e4SLinus Torvalds 	}
13031da177e4SLinus Torvalds 	chip->dual_codec = dual_codec;
13041da177e4SLinus Torvalds 
130599041feaSTakashi Iwai 	err = pcim_iomap_regions(pci, 0x03, "CS4281"); /* 2 BARs */
130699041feaSTakashi Iwai 	if (err < 0)
13071da177e4SLinus Torvalds 		return err;
13081da177e4SLinus Torvalds 	chip->ba0_addr = pci_resource_start(pci, 0);
13091da177e4SLinus Torvalds 	chip->ba1_addr = pci_resource_start(pci, 1);
13101da177e4SLinus Torvalds 
131199041feaSTakashi Iwai 	chip->ba0 = pcim_iomap_table(pci)[0];
131299041feaSTakashi Iwai 	chip->ba1 = pcim_iomap_table(pci)[1];
1313688956f2STakashi Iwai 
131499041feaSTakashi Iwai 	if (devm_request_irq(&pci->dev, pci->irq, snd_cs4281_interrupt,
131599041feaSTakashi Iwai 			     IRQF_SHARED, KBUILD_MODNAME, chip)) {
1316b055e7b4STakashi Iwai 		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
13171da177e4SLinus Torvalds 		return -ENOMEM;
13181da177e4SLinus Torvalds 	}
13191da177e4SLinus Torvalds 	chip->irq = pci->irq;
13205afff8c1STakashi Iwai 	card->sync_irq = chip->irq;
132199041feaSTakashi Iwai 	card->private_free = snd_cs4281_free;
13221da177e4SLinus Torvalds 
132399041feaSTakashi Iwai 	err = snd_cs4281_chip_init(chip);
132499041feaSTakashi Iwai 	if (err)
13251da177e4SLinus Torvalds 		return err;
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	snd_cs4281_proc_init(chip);
13281da177e4SLinus Torvalds 	return 0;
13291da177e4SLinus Torvalds }
13301da177e4SLinus Torvalds 
snd_cs4281_chip_init(struct cs4281 * chip)133193e35f95STakashi Iwai static int snd_cs4281_chip_init(struct cs4281 *chip)
13321da177e4SLinus Torvalds {
13331da177e4SLinus Torvalds 	unsigned int tmp;
133438223daaSTakashi Iwai 	unsigned long end_time;
13351da177e4SLinus Torvalds 	int retry_count = 2;
13361da177e4SLinus Torvalds 
1337a488e033SArnaud Patard 	/* Having EPPMC.FPDN=1 prevent proper chip initialisation */
1338a488e033SArnaud Patard 	tmp = snd_cs4281_peekBA0(chip, BA0_EPPMC);
1339a488e033SArnaud Patard 	if (tmp & BA0_EPPMC_FPDN)
1340a488e033SArnaud Patard 		snd_cs4281_pokeBA0(chip, BA0_EPPMC, tmp & ~BA0_EPPMC_FPDN);
1341a488e033SArnaud Patard 
13421da177e4SLinus Torvalds       __retry:
13431da177e4SLinus Torvalds 	tmp = snd_cs4281_peekBA0(chip, BA0_CFLR);
13441da177e4SLinus Torvalds 	if (tmp != BA0_CFLR_DEFAULT) {
13451da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_CFLR, BA0_CFLR_DEFAULT);
13461da177e4SLinus Torvalds 		tmp = snd_cs4281_peekBA0(chip, BA0_CFLR);
13471da177e4SLinus Torvalds 		if (tmp != BA0_CFLR_DEFAULT) {
1348b055e7b4STakashi Iwai 			dev_err(chip->card->dev,
1349b055e7b4STakashi Iwai 				"CFLR setup failed (0x%x)\n", tmp);
13501da177e4SLinus Torvalds 			return -EIO;
13511da177e4SLinus Torvalds 		}
13521da177e4SLinus Torvalds 	}
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	/* Set the 'Configuration Write Protect' register
13551da177e4SLinus Torvalds 	 * to 4281h.  Allows vendor-defined configuration
13561da177e4SLinus Torvalds          * space between 0e4h and 0ffh to be written. */
13571da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CWPR, 0x4281);
13581da177e4SLinus Torvalds 
1359393e95cdSTakashi Iwai 	tmp = snd_cs4281_peekBA0(chip, BA0_SERC1);
1360393e95cdSTakashi Iwai 	if (tmp != (BA0_SERC1_SO1EN | BA0_SERC1_AC97)) {
1361b055e7b4STakashi Iwai 		dev_err(chip->card->dev,
1362b055e7b4STakashi Iwai 			"SERC1 AC'97 check failed (0x%x)\n", tmp);
13631da177e4SLinus Torvalds 		return -EIO;
13641da177e4SLinus Torvalds 	}
1365393e95cdSTakashi Iwai 	tmp = snd_cs4281_peekBA0(chip, BA0_SERC2);
1366393e95cdSTakashi Iwai 	if (tmp != (BA0_SERC2_SI1EN | BA0_SERC2_AC97)) {
1367b055e7b4STakashi Iwai 		dev_err(chip->card->dev,
1368b055e7b4STakashi Iwai 			"SERC2 AC'97 check failed (0x%x)\n", tmp);
13691da177e4SLinus Torvalds 		return -EIO;
13701da177e4SLinus Torvalds 	}
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 	/* Sound System Power Management */
13731da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SSPM, BA0_SSPM_MIXEN | BA0_SSPM_CSRCEN |
13741da177e4SLinus Torvalds 				           BA0_SSPM_PSRCEN | BA0_SSPM_JSEN |
13751da177e4SLinus Torvalds 				           BA0_SSPM_ACLEN | BA0_SSPM_FMEN);
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 	/* Serial Port Power Management */
13781da177e4SLinus Torvalds  	/* Blast the clock control register to zero so that the
13791da177e4SLinus Torvalds          * PLL starts out in a known state, and blast the master serial
13801da177e4SLinus Torvalds          * port control register to zero so that the serial ports also
13811da177e4SLinus Torvalds          * start out in a known state. */
13821da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, 0);
13831da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SERMC, 0);
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds         /* Make ESYN go to zero to turn off
13861da177e4SLinus Torvalds          * the Sync pulse on the AC97 link. */
13871da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, 0);
13881da177e4SLinus Torvalds 	udelay(50);
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	/*  Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
13911da177e4SLinus Torvalds 	 *  spec) and then drive it high.  This is done for non AC97 modes since
13921da177e4SLinus Torvalds 	 *  there might be logic external to the CS4281 that uses the ARST# line
13931da177e4SLinus Torvalds 	 *  for a reset. */
13941da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SPMC, 0);
13951da177e4SLinus Torvalds 	udelay(50);
13961da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SPMC, BA0_SPMC_RSTN);
1397c9a49bb1STakashi Iwai 	msleep(50);
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	if (chip->dual_codec)
14001da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_SPMC, BA0_SPMC_RSTN | BA0_SPMC_ASDI2E);
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	/*
14031da177e4SLinus Torvalds 	 *  Set the serial port timing configuration.
14041da177e4SLinus Torvalds 	 */
14051da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SERMC,
14061da177e4SLinus Torvalds 			   (chip->dual_codec ? BA0_SERMC_TCID(chip->dual_codec) : BA0_SERMC_TCID(1)) |
14071da177e4SLinus Torvalds 			   BA0_SERMC_PTC_AC97 | BA0_SERMC_MSPE);
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 	/*
14101da177e4SLinus Torvalds 	 *  Start the DLL Clock logic.
14111da177e4SLinus Torvalds 	 */
14121da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, BA0_CLKCR1_DLLP);
1413c9a49bb1STakashi Iwai 	msleep(50);
14141da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, BA0_CLKCR1_SWCE | BA0_CLKCR1_DLLP);
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 	/*
14171da177e4SLinus Torvalds 	 * Wait for the DLL ready signal from the clock logic.
14181da177e4SLinus Torvalds 	 */
141938223daaSTakashi Iwai 	end_time = jiffies + HZ;
14201da177e4SLinus Torvalds 	do {
14211da177e4SLinus Torvalds 		/*
14221da177e4SLinus Torvalds 		 *  Read the AC97 status register to see if we've seen a CODEC
14231da177e4SLinus Torvalds 		 *  signal from the AC97 codec.
14241da177e4SLinus Torvalds 		 */
14251da177e4SLinus Torvalds 		if (snd_cs4281_peekBA0(chip, BA0_CLKCR1) & BA0_CLKCR1_DLLRDY)
14261da177e4SLinus Torvalds 			goto __ok0;
142738223daaSTakashi Iwai 		schedule_timeout_uninterruptible(1);
142838223daaSTakashi Iwai 	} while (time_after_eq(end_time, jiffies));
14291da177e4SLinus Torvalds 
1430b055e7b4STakashi Iwai 	dev_err(chip->card->dev, "DLLRDY not seen\n");
14311da177e4SLinus Torvalds 	return -EIO;
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds       __ok0:
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	/*
14361da177e4SLinus Torvalds 	 *  The first thing we do here is to enable sync generation.  As soon
14371da177e4SLinus Torvalds 	 *  as we start receiving bit clock, we'll start producing the SYNC
14381da177e4SLinus Torvalds 	 *  signal.
14391da177e4SLinus Torvalds 	 */
14401da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, BA0_ACCTL_ESYN);
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 	/*
14431da177e4SLinus Torvalds 	 * Wait for the codec ready signal from the AC97 codec.
14441da177e4SLinus Torvalds 	 */
144538223daaSTakashi Iwai 	end_time = jiffies + HZ;
14461da177e4SLinus Torvalds 	do {
14471da177e4SLinus Torvalds 		/*
14481da177e4SLinus Torvalds 		 *  Read the AC97 status register to see if we've seen a CODEC
14491da177e4SLinus Torvalds 		 *  signal from the AC97 codec.
14501da177e4SLinus Torvalds 		 */
14511da177e4SLinus Torvalds 		if (snd_cs4281_peekBA0(chip, BA0_ACSTS) & BA0_ACSTS_CRDY)
14521da177e4SLinus Torvalds 			goto __ok1;
145338223daaSTakashi Iwai 		schedule_timeout_uninterruptible(1);
145438223daaSTakashi Iwai 	} while (time_after_eq(end_time, jiffies));
14551da177e4SLinus Torvalds 
1456b055e7b4STakashi Iwai 	dev_err(chip->card->dev,
1457b055e7b4STakashi Iwai 		"never read codec ready from AC'97 (0x%x)\n",
1458b055e7b4STakashi Iwai 		snd_cs4281_peekBA0(chip, BA0_ACSTS));
14591da177e4SLinus Torvalds 	return -EIO;
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds       __ok1:
14621da177e4SLinus Torvalds 	if (chip->dual_codec) {
146338223daaSTakashi Iwai 		end_time = jiffies + HZ;
14641da177e4SLinus Torvalds 		do {
14651da177e4SLinus Torvalds 			if (snd_cs4281_peekBA0(chip, BA0_ACSTS2) & BA0_ACSTS_CRDY)
14661da177e4SLinus Torvalds 				goto __codec2_ok;
146738223daaSTakashi Iwai 			schedule_timeout_uninterruptible(1);
146838223daaSTakashi Iwai 		} while (time_after_eq(end_time, jiffies));
1469b055e7b4STakashi Iwai 		dev_info(chip->card->dev,
1470b055e7b4STakashi Iwai 			 "secondary codec doesn't respond. disable it...\n");
14711da177e4SLinus Torvalds 		chip->dual_codec = 0;
14721da177e4SLinus Torvalds 	__codec2_ok: ;
14731da177e4SLinus Torvalds 	}
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 	/*
14761da177e4SLinus Torvalds 	 *  Assert the valid frame signal so that we can start sending commands
14771da177e4SLinus Torvalds 	 *  to the AC97 codec.
14781da177e4SLinus Torvalds 	 */
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, BA0_ACCTL_VFRM | BA0_ACCTL_ESYN);
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 	/*
14831da177e4SLinus Torvalds 	 *  Wait until we've sampled input slots 3 and 4 as valid, meaning that
14841da177e4SLinus Torvalds 	 *  the codec is pumping ADC data across the AC-link.
14851da177e4SLinus Torvalds 	 */
14861da177e4SLinus Torvalds 
148738223daaSTakashi Iwai 	end_time = jiffies + HZ;
14881da177e4SLinus Torvalds 	do {
14891da177e4SLinus Torvalds 		/*
14901da177e4SLinus Torvalds 		 *  Read the input slot valid register and see if input slots 3
14911da177e4SLinus Torvalds 		 *  4 are valid yet.
14921da177e4SLinus Torvalds 		 */
14931da177e4SLinus Torvalds                 if ((snd_cs4281_peekBA0(chip, BA0_ACISV) & (BA0_ACISV_SLV(3) | BA0_ACISV_SLV(4))) == (BA0_ACISV_SLV(3) | BA0_ACISV_SLV(4)))
14941da177e4SLinus Torvalds                         goto __ok2;
149538223daaSTakashi Iwai 		schedule_timeout_uninterruptible(1);
149638223daaSTakashi Iwai 	} while (time_after_eq(end_time, jiffies));
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	if (--retry_count > 0)
14991da177e4SLinus Torvalds 		goto __retry;
1500b055e7b4STakashi Iwai 	dev_err(chip->card->dev, "never read ISV3 and ISV4 from AC'97\n");
15011da177e4SLinus Torvalds 	return -EIO;
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds       __ok2:
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	/*
15061da177e4SLinus Torvalds 	 *  Now, assert valid frame and the slot 3 and 4 valid bits.  This will
15071da177e4SLinus Torvalds 	 *  commense the transfer of digital audio data to the AC97 codec.
15081da177e4SLinus Torvalds 	 */
15091da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACOSV, BA0_ACOSV_SLV(3) | BA0_ACOSV_SLV(4));
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 	/*
15121da177e4SLinus Torvalds 	 *  Initialize DMA structures
15131da177e4SLinus Torvalds 	 */
15141da177e4SLinus Torvalds 	for (tmp = 0; tmp < 4; tmp++) {
151593e35f95STakashi Iwai 		struct cs4281_dma *dma = &chip->dma[tmp];
15161da177e4SLinus Torvalds 		dma->regDBA = BA0_DBA0 + (tmp * 0x10);
15171da177e4SLinus Torvalds 		dma->regDCA = BA0_DCA0 + (tmp * 0x10);
15181da177e4SLinus Torvalds 		dma->regDBC = BA0_DBC0 + (tmp * 0x10);
15191da177e4SLinus Torvalds 		dma->regDCC = BA0_DCC0 + (tmp * 0x10);
15201da177e4SLinus Torvalds 		dma->regDMR = BA0_DMR0 + (tmp * 8);
15211da177e4SLinus Torvalds 		dma->regDCR = BA0_DCR0 + (tmp * 8);
15221da177e4SLinus Torvalds 		dma->regHDSR = BA0_HDSR0 + (tmp * 4);
15231da177e4SLinus Torvalds 		dma->regFCR = BA0_FCR0 + (tmp * 4);
15241da177e4SLinus Torvalds 		dma->regFSIC = BA0_FSIC0 + (tmp * 4);
15251da177e4SLinus Torvalds 		dma->fifo_offset = tmp * CS4281_FIFO_SIZE;
15261da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, dma->regFCR,
15271da177e4SLinus Torvalds 				   BA0_FCR_LS(31) |
15281da177e4SLinus Torvalds 				   BA0_FCR_RS(31) |
15291da177e4SLinus Torvalds 				   BA0_FCR_SZ(CS4281_FIFO_SIZE) |
15301da177e4SLinus Torvalds 				   BA0_FCR_OF(dma->fifo_offset));
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 	chip->src_left_play_slot = 0;	/* AC'97 left PCM playback (3) */
15341da177e4SLinus Torvalds 	chip->src_right_play_slot = 1;	/* AC'97 right PCM playback (4) */
15351da177e4SLinus Torvalds 	chip->src_left_rec_slot = 10;	/* AC'97 left PCM record (3) */
15361da177e4SLinus Torvalds 	chip->src_right_rec_slot = 11;	/* AC'97 right PCM record (4) */
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 	/* Activate wave playback FIFO for FM playback */
15391da177e4SLinus Torvalds 	chip->dma[0].valFCR = BA0_FCR_FEN | BA0_FCR_LS(0) |
15401da177e4SLinus Torvalds 		              BA0_FCR_RS(1) |
15411da177e4SLinus Torvalds  	  	              BA0_FCR_SZ(CS4281_FIFO_SIZE) |
15421da177e4SLinus Torvalds 		              BA0_FCR_OF(chip->dma[0].fifo_offset);
15431da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, chip->dma[0].regFCR, chip->dma[0].valFCR);
15441da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SRCSA, (chip->src_left_play_slot << 0) |
15451da177e4SLinus Torvalds 					    (chip->src_right_play_slot << 8) |
15461da177e4SLinus Torvalds 					    (chip->src_left_rec_slot << 16) |
15471da177e4SLinus Torvalds 					    (chip->src_right_rec_slot << 24));
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	/* Initialize digital volume */
15501da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_PPLVC, 0);
15511da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_PPRVC, 0);
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds 	/* Enable IRQs */
15541da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HICR, BA0_HICR_EOI);
15551da177e4SLinus Torvalds 	/* Unmask interrupts */
15561da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HIMR, 0x7fffffff & ~(
15571da177e4SLinus Torvalds 					BA0_HISR_MIDI |
15581da177e4SLinus Torvalds 					BA0_HISR_DMAI |
15591da177e4SLinus Torvalds 					BA0_HISR_DMA(0) |
15601da177e4SLinus Torvalds 					BA0_HISR_DMA(1) |
15611da177e4SLinus Torvalds 					BA0_HISR_DMA(2) |
15621da177e4SLinus Torvalds 					BA0_HISR_DMA(3)));
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds 	return 0;
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds /*
15681da177e4SLinus Torvalds  *  MIDI section
15691da177e4SLinus Torvalds  */
15701da177e4SLinus Torvalds 
snd_cs4281_midi_reset(struct cs4281 * chip)157193e35f95STakashi Iwai static void snd_cs4281_midi_reset(struct cs4281 *chip)
15721da177e4SLinus Torvalds {
15731da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr | BA0_MIDCR_MRST);
15741da177e4SLinus Torvalds 	udelay(100);
15751da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
15761da177e4SLinus Torvalds }
15771da177e4SLinus Torvalds 
snd_cs4281_midi_input_open(struct snd_rawmidi_substream * substream)157893e35f95STakashi Iwai static int snd_cs4281_midi_input_open(struct snd_rawmidi_substream *substream)
15791da177e4SLinus Torvalds {
158093e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15831da177e4SLinus Torvalds  	chip->midcr |= BA0_MIDCR_RXE;
15841da177e4SLinus Torvalds 	chip->midi_input = substream;
15851da177e4SLinus Torvalds 	if (!(chip->uartm & CS4281_MODE_OUTPUT)) {
15861da177e4SLinus Torvalds 		snd_cs4281_midi_reset(chip);
15871da177e4SLinus Torvalds 	} else {
15881da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
15891da177e4SLinus Torvalds 	}
15901da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15911da177e4SLinus Torvalds 	return 0;
15921da177e4SLinus Torvalds }
15931da177e4SLinus Torvalds 
snd_cs4281_midi_input_close(struct snd_rawmidi_substream * substream)159493e35f95STakashi Iwai static int snd_cs4281_midi_input_close(struct snd_rawmidi_substream *substream)
15951da177e4SLinus Torvalds {
159693e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15991da177e4SLinus Torvalds 	chip->midcr &= ~(BA0_MIDCR_RXE | BA0_MIDCR_RIE);
16001da177e4SLinus Torvalds 	chip->midi_input = NULL;
16011da177e4SLinus Torvalds 	if (!(chip->uartm & CS4281_MODE_OUTPUT)) {
16021da177e4SLinus Torvalds 		snd_cs4281_midi_reset(chip);
16031da177e4SLinus Torvalds 	} else {
16041da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16051da177e4SLinus Torvalds 	}
16061da177e4SLinus Torvalds 	chip->uartm &= ~CS4281_MODE_INPUT;
16071da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
16081da177e4SLinus Torvalds 	return 0;
16091da177e4SLinus Torvalds }
16101da177e4SLinus Torvalds 
snd_cs4281_midi_output_open(struct snd_rawmidi_substream * substream)161193e35f95STakashi Iwai static int snd_cs4281_midi_output_open(struct snd_rawmidi_substream *substream)
16121da177e4SLinus Torvalds {
161393e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
16161da177e4SLinus Torvalds 	chip->uartm |= CS4281_MODE_OUTPUT;
16171da177e4SLinus Torvalds 	chip->midcr |= BA0_MIDCR_TXE;
16181da177e4SLinus Torvalds 	chip->midi_output = substream;
16191da177e4SLinus Torvalds 	if (!(chip->uartm & CS4281_MODE_INPUT)) {
16201da177e4SLinus Torvalds 		snd_cs4281_midi_reset(chip);
16211da177e4SLinus Torvalds 	} else {
16221da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16231da177e4SLinus Torvalds 	}
16241da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
16251da177e4SLinus Torvalds 	return 0;
16261da177e4SLinus Torvalds }
16271da177e4SLinus Torvalds 
snd_cs4281_midi_output_close(struct snd_rawmidi_substream * substream)162893e35f95STakashi Iwai static int snd_cs4281_midi_output_close(struct snd_rawmidi_substream *substream)
16291da177e4SLinus Torvalds {
163093e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
16331da177e4SLinus Torvalds 	chip->midcr &= ~(BA0_MIDCR_TXE | BA0_MIDCR_TIE);
16341da177e4SLinus Torvalds 	chip->midi_output = NULL;
16351da177e4SLinus Torvalds 	if (!(chip->uartm & CS4281_MODE_INPUT)) {
16361da177e4SLinus Torvalds 		snd_cs4281_midi_reset(chip);
16371da177e4SLinus Torvalds 	} else {
16381da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16391da177e4SLinus Torvalds 	}
16401da177e4SLinus Torvalds 	chip->uartm &= ~CS4281_MODE_OUTPUT;
16411da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
16421da177e4SLinus Torvalds 	return 0;
16431da177e4SLinus Torvalds }
16441da177e4SLinus Torvalds 
snd_cs4281_midi_input_trigger(struct snd_rawmidi_substream * substream,int up)164593e35f95STakashi Iwai static void snd_cs4281_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
16461da177e4SLinus Torvalds {
16471da177e4SLinus Torvalds 	unsigned long flags;
164893e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
16511da177e4SLinus Torvalds 	if (up) {
16521da177e4SLinus Torvalds 		if ((chip->midcr & BA0_MIDCR_RIE) == 0) {
16531da177e4SLinus Torvalds 			chip->midcr |= BA0_MIDCR_RIE;
16541da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16551da177e4SLinus Torvalds 		}
16561da177e4SLinus Torvalds 	} else {
16571da177e4SLinus Torvalds 		if (chip->midcr & BA0_MIDCR_RIE) {
16581da177e4SLinus Torvalds 			chip->midcr &= ~BA0_MIDCR_RIE;
16591da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16601da177e4SLinus Torvalds 		}
16611da177e4SLinus Torvalds 	}
16621da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
16631da177e4SLinus Torvalds }
16641da177e4SLinus Torvalds 
snd_cs4281_midi_output_trigger(struct snd_rawmidi_substream * substream,int up)166593e35f95STakashi Iwai static void snd_cs4281_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
16661da177e4SLinus Torvalds {
16671da177e4SLinus Torvalds 	unsigned long flags;
166893e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
16691da177e4SLinus Torvalds 	unsigned char byte;
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
16721da177e4SLinus Torvalds 	if (up) {
16731da177e4SLinus Torvalds 		if ((chip->midcr & BA0_MIDCR_TIE) == 0) {
16741da177e4SLinus Torvalds 			chip->midcr |= BA0_MIDCR_TIE;
16751da177e4SLinus Torvalds 			/* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
16761da177e4SLinus Torvalds 			while ((chip->midcr & BA0_MIDCR_TIE) &&
16771da177e4SLinus Torvalds 			       (snd_cs4281_peekBA0(chip, BA0_MIDSR) & BA0_MIDSR_TBF) == 0) {
16781da177e4SLinus Torvalds 				if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
16791da177e4SLinus Torvalds 					chip->midcr &= ~BA0_MIDCR_TIE;
16801da177e4SLinus Torvalds 				} else {
16811da177e4SLinus Torvalds 					snd_cs4281_pokeBA0(chip, BA0_MIDWP, byte);
16821da177e4SLinus Torvalds 				}
16831da177e4SLinus Torvalds 			}
16841da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16851da177e4SLinus Torvalds 		}
16861da177e4SLinus Torvalds 	} else {
16871da177e4SLinus Torvalds 		if (chip->midcr & BA0_MIDCR_TIE) {
16881da177e4SLinus Torvalds 			chip->midcr &= ~BA0_MIDCR_TIE;
16891da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16901da177e4SLinus Torvalds 		}
16911da177e4SLinus Torvalds 	}
16921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
16931da177e4SLinus Torvalds }
16941da177e4SLinus Torvalds 
1695485885b9STakashi Iwai static const struct snd_rawmidi_ops snd_cs4281_midi_output =
16961da177e4SLinus Torvalds {
16971da177e4SLinus Torvalds 	.open =		snd_cs4281_midi_output_open,
16981da177e4SLinus Torvalds 	.close =	snd_cs4281_midi_output_close,
16991da177e4SLinus Torvalds 	.trigger =	snd_cs4281_midi_output_trigger,
17001da177e4SLinus Torvalds };
17011da177e4SLinus Torvalds 
1702485885b9STakashi Iwai static const struct snd_rawmidi_ops snd_cs4281_midi_input =
17031da177e4SLinus Torvalds {
17041da177e4SLinus Torvalds 	.open = 	snd_cs4281_midi_input_open,
17051da177e4SLinus Torvalds 	.close =	snd_cs4281_midi_input_close,
17061da177e4SLinus Torvalds 	.trigger =	snd_cs4281_midi_input_trigger,
17071da177e4SLinus Torvalds };
17081da177e4SLinus Torvalds 
snd_cs4281_midi(struct cs4281 * chip,int device)17093e4f4776SLars-Peter Clausen static int snd_cs4281_midi(struct cs4281 *chip, int device)
17101da177e4SLinus Torvalds {
171193e35f95STakashi Iwai 	struct snd_rawmidi *rmidi;
17121da177e4SLinus Torvalds 	int err;
17131da177e4SLinus Torvalds 
1714393e95cdSTakashi Iwai 	err = snd_rawmidi_new(chip->card, "CS4281", device, 1, 1, &rmidi);
1715393e95cdSTakashi Iwai 	if (err < 0)
17161da177e4SLinus Torvalds 		return err;
17171da177e4SLinus Torvalds 	strcpy(rmidi->name, "CS4281");
17181da177e4SLinus Torvalds 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_cs4281_midi_output);
17191da177e4SLinus Torvalds 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_cs4281_midi_input);
17201da177e4SLinus Torvalds 	rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
17211da177e4SLinus Torvalds 	rmidi->private_data = chip;
17221da177e4SLinus Torvalds 	chip->rmidi = rmidi;
17231da177e4SLinus Torvalds 	return 0;
17241da177e4SLinus Torvalds }
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds /*
17271da177e4SLinus Torvalds  *  Interrupt handler
17281da177e4SLinus Torvalds  */
17291da177e4SLinus Torvalds 
snd_cs4281_interrupt(int irq,void * dev_id)17307d12e780SDavid Howells static irqreturn_t snd_cs4281_interrupt(int irq, void *dev_id)
17311da177e4SLinus Torvalds {
173293e35f95STakashi Iwai 	struct cs4281 *chip = dev_id;
17331da177e4SLinus Torvalds 	unsigned int status, dma, val;
173493e35f95STakashi Iwai 	struct cs4281_dma *cdma;
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 	if (chip == NULL)
17371da177e4SLinus Torvalds 		return IRQ_NONE;
17381da177e4SLinus Torvalds 	status = snd_cs4281_peekBA0(chip, BA0_HISR);
17391da177e4SLinus Torvalds 	if ((status & 0x7fffffff) == 0) {
17401da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_HICR, BA0_HICR_EOI);
17411da177e4SLinus Torvalds 		return IRQ_NONE;
17421da177e4SLinus Torvalds 	}
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	if (status & (BA0_HISR_DMA(0)|BA0_HISR_DMA(1)|BA0_HISR_DMA(2)|BA0_HISR_DMA(3))) {
17451da177e4SLinus Torvalds 		for (dma = 0; dma < 4; dma++)
17461da177e4SLinus Torvalds 			if (status & BA0_HISR_DMA(dma)) {
17471da177e4SLinus Torvalds 				cdma = &chip->dma[dma];
17481da177e4SLinus Torvalds 				spin_lock(&chip->reg_lock);
17491da177e4SLinus Torvalds 				/* ack DMA IRQ */
17501da177e4SLinus Torvalds 				val = snd_cs4281_peekBA0(chip, cdma->regHDSR);
17511da177e4SLinus Torvalds 				/* workaround, sometimes CS4281 acknowledges */
17521da177e4SLinus Torvalds 				/* end or middle transfer position twice */
17531da177e4SLinus Torvalds 				cdma->frag++;
17541da177e4SLinus Torvalds 				if ((val & BA0_HDSR_DHTC) && !(cdma->frag & 1)) {
17551da177e4SLinus Torvalds 					cdma->frag--;
17561da177e4SLinus Torvalds 					chip->spurious_dhtc_irq++;
17571da177e4SLinus Torvalds 					spin_unlock(&chip->reg_lock);
17581da177e4SLinus Torvalds 					continue;
17591da177e4SLinus Torvalds 				}
17601da177e4SLinus Torvalds 				if ((val & BA0_HDSR_DTC) && (cdma->frag & 1)) {
17611da177e4SLinus Torvalds 					cdma->frag--;
17621da177e4SLinus Torvalds 					chip->spurious_dtc_irq++;
17631da177e4SLinus Torvalds 					spin_unlock(&chip->reg_lock);
17641da177e4SLinus Torvalds 					continue;
17651da177e4SLinus Torvalds 				}
17661da177e4SLinus Torvalds 				spin_unlock(&chip->reg_lock);
17671da177e4SLinus Torvalds 				snd_pcm_period_elapsed(cdma->substream);
17681da177e4SLinus Torvalds 			}
17691da177e4SLinus Torvalds 	}
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	if ((status & BA0_HISR_MIDI) && chip->rmidi) {
17721da177e4SLinus Torvalds 		unsigned char c;
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
17751da177e4SLinus Torvalds 		while ((snd_cs4281_peekBA0(chip, BA0_MIDSR) & BA0_MIDSR_RBE) == 0) {
17761da177e4SLinus Torvalds 			c = snd_cs4281_peekBA0(chip, BA0_MIDRP);
17771da177e4SLinus Torvalds 			if ((chip->midcr & BA0_MIDCR_RIE) == 0)
17781da177e4SLinus Torvalds 				continue;
17791da177e4SLinus Torvalds 			snd_rawmidi_receive(chip->midi_input, &c, 1);
17801da177e4SLinus Torvalds 		}
17811da177e4SLinus Torvalds 		while ((snd_cs4281_peekBA0(chip, BA0_MIDSR) & BA0_MIDSR_TBF) == 0) {
17821da177e4SLinus Torvalds 			if ((chip->midcr & BA0_MIDCR_TIE) == 0)
17831da177e4SLinus Torvalds 				break;
17841da177e4SLinus Torvalds 			if (snd_rawmidi_transmit(chip->midi_output, &c, 1) != 1) {
17851da177e4SLinus Torvalds 				chip->midcr &= ~BA0_MIDCR_TIE;
17861da177e4SLinus Torvalds 				snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
17871da177e4SLinus Torvalds 				break;
17881da177e4SLinus Torvalds 			}
17891da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDWP, c);
17901da177e4SLinus Torvalds 		}
17911da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
17921da177e4SLinus Torvalds 	}
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 	/* EOI to the PCI part... reenables interrupts */
17951da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HICR, BA0_HICR_EOI);
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds 	return IRQ_HANDLED;
17981da177e4SLinus Torvalds }
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds /*
18021da177e4SLinus Torvalds  * OPL3 command
18031da177e4SLinus Torvalds  */
snd_cs4281_opl3_command(struct snd_opl3 * opl3,unsigned short cmd,unsigned char val)180493e35f95STakashi Iwai static void snd_cs4281_opl3_command(struct snd_opl3 *opl3, unsigned short cmd,
180593e35f95STakashi Iwai 				    unsigned char val)
18061da177e4SLinus Torvalds {
18071da177e4SLinus Torvalds 	unsigned long flags;
180893e35f95STakashi Iwai 	struct cs4281 *chip = opl3->private_data;
18091da177e4SLinus Torvalds 	void __iomem *port;
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds 	if (cmd & OPL3_RIGHT)
18121da177e4SLinus Torvalds 		port = chip->ba0 + BA0_B1AP; /* right port */
18131da177e4SLinus Torvalds 	else
18141da177e4SLinus Torvalds 		port = chip->ba0 + BA0_B0AP; /* left port */
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds 	spin_lock_irqsave(&opl3->reg_lock, flags);
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds 	writel((unsigned int)cmd, port);
18191da177e4SLinus Torvalds 	udelay(10);
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 	writel((unsigned int)val, port + 4);
18221da177e4SLinus Torvalds 	udelay(30);
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds 	spin_unlock_irqrestore(&opl3->reg_lock, flags);
18251da177e4SLinus Torvalds }
18261da177e4SLinus Torvalds 
__snd_cs4281_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)18279bf5ed9aSTakashi Iwai static int __snd_cs4281_probe(struct pci_dev *pci,
18281da177e4SLinus Torvalds 			      const struct pci_device_id *pci_id)
18291da177e4SLinus Torvalds {
18301da177e4SLinus Torvalds 	static int dev;
183193e35f95STakashi Iwai 	struct snd_card *card;
183293e35f95STakashi Iwai 	struct cs4281 *chip;
183393e35f95STakashi Iwai 	struct snd_opl3 *opl3;
18341da177e4SLinus Torvalds 	int err;
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds         if (dev >= SNDRV_CARDS)
18371da177e4SLinus Torvalds                 return -ENODEV;
18381da177e4SLinus Torvalds 	if (!enable[dev]) {
18391da177e4SLinus Torvalds 		dev++;
18401da177e4SLinus Torvalds 		return -ENOENT;
18411da177e4SLinus Torvalds 	}
18421da177e4SLinus Torvalds 
184399041feaSTakashi Iwai 	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
184499041feaSTakashi Iwai 				sizeof(*chip), &card);
184599041feaSTakashi Iwai 	if (err < 0)
184699041feaSTakashi Iwai 		return err;
184799041feaSTakashi Iwai 	chip = card->private_data;
184899041feaSTakashi Iwai 
184999041feaSTakashi Iwai 	err = snd_cs4281_create(card, pci, dual_codec[dev]);
1850e58de7baSTakashi Iwai 	if (err < 0)
1851e58de7baSTakashi Iwai 		return err;
18521da177e4SLinus Torvalds 
1853393e95cdSTakashi Iwai 	err = snd_cs4281_mixer(chip);
185499041feaSTakashi Iwai 	if (err < 0)
18551da177e4SLinus Torvalds 		return err;
1856393e95cdSTakashi Iwai 	err = snd_cs4281_pcm(chip, 0);
185799041feaSTakashi Iwai 	if (err < 0)
18581da177e4SLinus Torvalds 		return err;
1859393e95cdSTakashi Iwai 	err = snd_cs4281_midi(chip, 0);
186099041feaSTakashi Iwai 	if (err < 0)
18611da177e4SLinus Torvalds 		return err;
1862393e95cdSTakashi Iwai 	err = snd_opl3_new(card, OPL3_HW_OPL3_CS4281, &opl3);
186399041feaSTakashi Iwai 	if (err < 0)
18641da177e4SLinus Torvalds 		return err;
18651da177e4SLinus Torvalds 	opl3->private_data = chip;
18661da177e4SLinus Torvalds 	opl3->command = snd_cs4281_opl3_command;
18671da177e4SLinus Torvalds 	snd_opl3_init(opl3);
1868393e95cdSTakashi Iwai 	err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
186999041feaSTakashi Iwai 	if (err < 0)
18701da177e4SLinus Torvalds 		return err;
18711da177e4SLinus Torvalds 	snd_cs4281_create_gameport(chip);
18721da177e4SLinus Torvalds 	strcpy(card->driver, "CS4281");
18731da177e4SLinus Torvalds 	strcpy(card->shortname, "Cirrus Logic CS4281");
18741da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %d",
18751da177e4SLinus Torvalds 		card->shortname,
18761da177e4SLinus Torvalds 		chip->ba0_addr,
18771da177e4SLinus Torvalds 		chip->irq);
18781da177e4SLinus Torvalds 
1879393e95cdSTakashi Iwai 	err = snd_card_register(card);
188099041feaSTakashi Iwai 	if (err < 0)
18811da177e4SLinus Torvalds 		return err;
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
18841da177e4SLinus Torvalds 	dev++;
18851da177e4SLinus Torvalds 	return 0;
18861da177e4SLinus Torvalds }
18871da177e4SLinus Torvalds 
snd_cs4281_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)18889bf5ed9aSTakashi Iwai static int snd_cs4281_probe(struct pci_dev *pci,
18899bf5ed9aSTakashi Iwai 			    const struct pci_device_id *pci_id)
18909bf5ed9aSTakashi Iwai {
18919bf5ed9aSTakashi Iwai 	return snd_card_free_on_error(&pci->dev, __snd_cs4281_probe(pci, pci_id));
18929bf5ed9aSTakashi Iwai }
18939bf5ed9aSTakashi Iwai 
18941da177e4SLinus Torvalds /*
18951da177e4SLinus Torvalds  * Power Management
18961da177e4SLinus Torvalds  */
1897e157f0cdSTakashi Iwai static const int saved_regs[SUSPEND_REGISTERS] = {
18981da177e4SLinus Torvalds 	BA0_JSCTL,
18991da177e4SLinus Torvalds 	BA0_GPIOR,
19001da177e4SLinus Torvalds 	BA0_SSCR,
19011da177e4SLinus Torvalds 	BA0_MIDCR,
19021da177e4SLinus Torvalds 	BA0_SRCSA,
19031da177e4SLinus Torvalds 	BA0_PASR,
19041da177e4SLinus Torvalds 	BA0_CASR,
19051da177e4SLinus Torvalds 	BA0_DACSR,
19061da177e4SLinus Torvalds 	BA0_ADCSR,
19071da177e4SLinus Torvalds 	BA0_FMLVC,
19081da177e4SLinus Torvalds 	BA0_FMRVC,
19091da177e4SLinus Torvalds 	BA0_PPLVC,
19101da177e4SLinus Torvalds 	BA0_PPRVC,
19111da177e4SLinus Torvalds };
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds #define CLKCR1_CKRA                             0x00010000L
19141da177e4SLinus Torvalds 
cs4281_suspend(struct device * dev)191568cb2b55STakashi Iwai static int cs4281_suspend(struct device *dev)
19161da177e4SLinus Torvalds {
191768cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
191838c0a158STakashi Iwai 	struct cs4281 *chip = card->private_data;
19191da177e4SLinus Torvalds 	u32 ulCLK;
19201da177e4SLinus Torvalds 	unsigned int i;
19211da177e4SLinus Torvalds 
192238c0a158STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
19231da177e4SLinus Torvalds 	snd_ac97_suspend(chip->ac97);
19241da177e4SLinus Torvalds 	snd_ac97_suspend(chip->ac97_secondary);
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	ulCLK = snd_cs4281_peekBA0(chip, BA0_CLKCR1);
19271da177e4SLinus Torvalds 	ulCLK |= CLKCR1_CKRA;
19281da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, ulCLK);
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 	/* Disable interrupts. */
19311da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HICR, BA0_HICR_CHGM);
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 	/* remember the status registers */
19341da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
19351da177e4SLinus Torvalds 		if (saved_regs[i])
19361da177e4SLinus Torvalds 			chip->suspend_regs[i] = snd_cs4281_peekBA0(chip, saved_regs[i]);
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	/* Turn off the serial ports. */
19391da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SERMC, 0);
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds 	/* Power off FM, Joystick, AC link, */
19421da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SSPM, 0);
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 	/* DLL off. */
19451da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, 0);
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	/* AC link off. */
19481da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SPMC, 0);
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	ulCLK = snd_cs4281_peekBA0(chip, BA0_CLKCR1);
19511da177e4SLinus Torvalds 	ulCLK &= ~CLKCR1_CKRA;
19521da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, ulCLK);
19531da177e4SLinus Torvalds 	return 0;
19541da177e4SLinus Torvalds }
19551da177e4SLinus Torvalds 
cs4281_resume(struct device * dev)195668cb2b55STakashi Iwai static int cs4281_resume(struct device *dev)
19571da177e4SLinus Torvalds {
195868cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
195938c0a158STakashi Iwai 	struct cs4281 *chip = card->private_data;
19601da177e4SLinus Torvalds 	unsigned int i;
19611da177e4SLinus Torvalds 	u32 ulCLK;
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds 	ulCLK = snd_cs4281_peekBA0(chip, BA0_CLKCR1);
19641da177e4SLinus Torvalds 	ulCLK |= CLKCR1_CKRA;
19651da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, ulCLK);
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds 	snd_cs4281_chip_init(chip);
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds 	/* restore the status registers */
19701da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
19711da177e4SLinus Torvalds 		if (saved_regs[i])
19721da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, saved_regs[i], chip->suspend_regs[i]);
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 	snd_ac97_resume(chip->ac97);
19751da177e4SLinus Torvalds 	snd_ac97_resume(chip->ac97_secondary);
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds 	ulCLK = snd_cs4281_peekBA0(chip, BA0_CLKCR1);
19781da177e4SLinus Torvalds 	ulCLK &= ~CLKCR1_CKRA;
19791da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, ulCLK);
19801da177e4SLinus Torvalds 
198138c0a158STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
19821da177e4SLinus Torvalds 	return 0;
19831da177e4SLinus Torvalds }
198468cb2b55STakashi Iwai 
1985*cbdcefbdSTakashi Iwai static DEFINE_SIMPLE_DEV_PM_OPS(cs4281_pm, cs4281_suspend, cs4281_resume);
19861da177e4SLinus Torvalds 
1987e9f66d9bSTakashi Iwai static struct pci_driver cs4281_driver = {
19883733e424STakashi Iwai 	.name = KBUILD_MODNAME,
19891da177e4SLinus Torvalds 	.id_table = snd_cs4281_ids,
19901da177e4SLinus Torvalds 	.probe = snd_cs4281_probe,
199168cb2b55STakashi Iwai 	.driver = {
1992*cbdcefbdSTakashi Iwai 		.pm = &cs4281_pm,
199368cb2b55STakashi Iwai 	},
19941da177e4SLinus Torvalds };
19951da177e4SLinus Torvalds 
1996e9f66d9bSTakashi Iwai module_pci_driver(cs4281_driver);
1997