xref: /linux/sound/pci/cs4281.c (revision e23e7a1436207217000c2854214bc908936af3cb)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Driver for Cirrus Logic CS4281 based PCI soundcard
3c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
71da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
81da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
91da177e4SLinus Torvalds  *   (at your option) any later version.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
121da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
131da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141da177e4SLinus Torvalds  *   GNU General Public License for more details.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
171da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
181da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include <asm/io.h>
231da177e4SLinus Torvalds #include <linux/delay.h>
241da177e4SLinus Torvalds #include <linux/interrupt.h>
251da177e4SLinus Torvalds #include <linux/init.h>
261da177e4SLinus Torvalds #include <linux/pci.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/gameport.h>
2965a77217SPaul Gortmaker #include <linux/module.h>
301da177e4SLinus Torvalds #include <sound/core.h>
311da177e4SLinus Torvalds #include <sound/control.h>
321da177e4SLinus Torvalds #include <sound/pcm.h>
331da177e4SLinus Torvalds #include <sound/rawmidi.h>
341da177e4SLinus Torvalds #include <sound/ac97_codec.h>
359f6ab250STakashi Iwai #include <sound/tlv.h>
361da177e4SLinus Torvalds #include <sound/opl3.h>
371da177e4SLinus Torvalds #include <sound/initval.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds 
40c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
411da177e4SLinus Torvalds MODULE_DESCRIPTION("Cirrus Logic CS4281");
421da177e4SLinus Torvalds MODULE_LICENSE("GPL");
431da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{Cirrus Logic,CS4281}}");
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
461da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
47a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable switches */
48a67ff6a5SRusty Russell static bool dual_codec[SNDRV_CARDS];	/* dual codec */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
511da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for CS4281 soundcard.");
521da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
531da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for CS4281 soundcard.");
541da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
551da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable CS4281 soundcard.");
561da177e4SLinus Torvalds module_param_array(dual_codec, bool, NULL, 0444);
571da177e4SLinus Torvalds MODULE_PARM_DESC(dual_codec, "Secondary Codec ID (0 = disabled).");
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /*
601da177e4SLinus Torvalds  *  Direct registers
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #define CS4281_BA0_SIZE		0x1000
641da177e4SLinus Torvalds #define CS4281_BA1_SIZE		0x10000
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /*
671da177e4SLinus Torvalds  *  BA0 registers
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds #define BA0_HISR		0x0000	/* Host Interrupt Status Register */
701da177e4SLinus Torvalds #define BA0_HISR_INTENA		(1<<31)	/* Internal Interrupt Enable Bit */
711da177e4SLinus Torvalds #define BA0_HISR_MIDI		(1<<22)	/* MIDI port interrupt */
721da177e4SLinus Torvalds #define BA0_HISR_FIFOI		(1<<20)	/* FIFO polled interrupt */
731da177e4SLinus Torvalds #define BA0_HISR_DMAI		(1<<18)	/* DMA interrupt (half or end) */
741da177e4SLinus Torvalds #define BA0_HISR_FIFO(c)	(1<<(12+(c))) /* FIFO channel interrupt */
751da177e4SLinus Torvalds #define BA0_HISR_DMA(c)		(1<<(8+(c)))  /* DMA channel interrupt */
761da177e4SLinus Torvalds #define BA0_HISR_GPPI		(1<<5)	/* General Purpose Input (Primary chip) */
771da177e4SLinus Torvalds #define BA0_HISR_GPSI		(1<<4)	/* General Purpose Input (Secondary chip) */
781da177e4SLinus Torvalds #define BA0_HISR_GP3I		(1<<3)	/* GPIO3 pin Interrupt */
791da177e4SLinus Torvalds #define BA0_HISR_GP1I		(1<<2)	/* GPIO1 pin Interrupt */
801da177e4SLinus Torvalds #define BA0_HISR_VUPI		(1<<1)	/* VOLUP pin Interrupt */
811da177e4SLinus Torvalds #define BA0_HISR_VDNI		(1<<0)	/* VOLDN pin Interrupt */
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds #define BA0_HICR		0x0008	/* Host Interrupt Control Register */
841da177e4SLinus Torvalds #define BA0_HICR_CHGM		(1<<1)	/* INTENA Change Mask */
851da177e4SLinus Torvalds #define BA0_HICR_IEV		(1<<0)	/* INTENA Value */
861da177e4SLinus Torvalds #define BA0_HICR_EOI		(3<<0)	/* End of Interrupt command */
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds #define BA0_HIMR		0x000c	/* Host Interrupt Mask Register */
891da177e4SLinus Torvalds 					/* Use same contants as for BA0_HISR */
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds #define BA0_IIER		0x0010	/* ISA Interrupt Enable Register */
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #define BA0_HDSR0		0x00f0	/* Host DMA Engine 0 Status Register */
941da177e4SLinus Torvalds #define BA0_HDSR1		0x00f4	/* Host DMA Engine 1 Status Register */
951da177e4SLinus Torvalds #define BA0_HDSR2		0x00f8	/* Host DMA Engine 2 Status Register */
961da177e4SLinus Torvalds #define BA0_HDSR3		0x00fc	/* Host DMA Engine 3 Status Register */
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds #define BA0_HDSR_CH1P		(1<<25)	/* Channel 1 Pending */
991da177e4SLinus Torvalds #define BA0_HDSR_CH2P		(1<<24)	/* Channel 2 Pending */
1001da177e4SLinus Torvalds #define BA0_HDSR_DHTC		(1<<17)	/* DMA Half Terminal Count */
1011da177e4SLinus Torvalds #define BA0_HDSR_DTC		(1<<16)	/* DMA Terminal Count */
1021da177e4SLinus Torvalds #define BA0_HDSR_DRUN		(1<<15)	/* DMA Running */
1031da177e4SLinus Torvalds #define BA0_HDSR_RQ		(1<<7)	/* Pending Request */
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds #define BA0_DCA0		0x0110	/* Host DMA Engine 0 Current Address */
1061da177e4SLinus Torvalds #define BA0_DCC0		0x0114	/* Host DMA Engine 0 Current Count */
1071da177e4SLinus Torvalds #define BA0_DBA0		0x0118	/* Host DMA Engine 0 Base Address */
1081da177e4SLinus Torvalds #define BA0_DBC0		0x011c	/* Host DMA Engine 0 Base Count */
1091da177e4SLinus Torvalds #define BA0_DCA1		0x0120	/* Host DMA Engine 1 Current Address */
1101da177e4SLinus Torvalds #define BA0_DCC1		0x0124	/* Host DMA Engine 1 Current Count */
1111da177e4SLinus Torvalds #define BA0_DBA1		0x0128	/* Host DMA Engine 1 Base Address */
1121da177e4SLinus Torvalds #define BA0_DBC1		0x012c	/* Host DMA Engine 1 Base Count */
1131da177e4SLinus Torvalds #define BA0_DCA2		0x0130	/* Host DMA Engine 2 Current Address */
1141da177e4SLinus Torvalds #define BA0_DCC2		0x0134	/* Host DMA Engine 2 Current Count */
1151da177e4SLinus Torvalds #define BA0_DBA2		0x0138	/* Host DMA Engine 2 Base Address */
1161da177e4SLinus Torvalds #define BA0_DBC2		0x013c	/* Host DMA Engine 2 Base Count */
1171da177e4SLinus Torvalds #define BA0_DCA3		0x0140	/* Host DMA Engine 3 Current Address */
1181da177e4SLinus Torvalds #define BA0_DCC3		0x0144	/* Host DMA Engine 3 Current Count */
1191da177e4SLinus Torvalds #define BA0_DBA3		0x0148	/* Host DMA Engine 3 Base Address */
1201da177e4SLinus Torvalds #define BA0_DBC3		0x014c	/* Host DMA Engine 3 Base Count */
1211da177e4SLinus Torvalds #define BA0_DMR0		0x0150	/* Host DMA Engine 0 Mode */
1221da177e4SLinus Torvalds #define BA0_DCR0		0x0154	/* Host DMA Engine 0 Command */
1231da177e4SLinus Torvalds #define BA0_DMR1		0x0158	/* Host DMA Engine 1 Mode */
1241da177e4SLinus Torvalds #define BA0_DCR1		0x015c	/* Host DMA Engine 1 Command */
1251da177e4SLinus Torvalds #define BA0_DMR2		0x0160	/* Host DMA Engine 2 Mode */
1261da177e4SLinus Torvalds #define BA0_DCR2		0x0164	/* Host DMA Engine 2 Command */
1271da177e4SLinus Torvalds #define BA0_DMR3		0x0168	/* Host DMA Engine 3 Mode */
1281da177e4SLinus Torvalds #define BA0_DCR3		0x016c	/* Host DMA Engine 3 Command */
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds #define BA0_DMR_DMA		(1<<29)	/* Enable DMA mode */
1311da177e4SLinus Torvalds #define BA0_DMR_POLL		(1<<28)	/* Enable poll mode */
1321da177e4SLinus Torvalds #define BA0_DMR_TBC		(1<<25)	/* Transfer By Channel */
1331da177e4SLinus Torvalds #define BA0_DMR_CBC		(1<<24)	/* Count By Channel (0 = frame resolution) */
1341da177e4SLinus Torvalds #define BA0_DMR_SWAPC		(1<<22)	/* Swap Left/Right Channels */
1351da177e4SLinus Torvalds #define BA0_DMR_SIZE20		(1<<20)	/* Sample is 20-bit */
1361da177e4SLinus Torvalds #define BA0_DMR_USIGN		(1<<19)	/* Unsigned */
1371da177e4SLinus Torvalds #define BA0_DMR_BEND		(1<<18)	/* Big Endian */
1381da177e4SLinus Torvalds #define BA0_DMR_MONO		(1<<17)	/* Mono */
1391da177e4SLinus Torvalds #define BA0_DMR_SIZE8		(1<<16)	/* Sample is 8-bit */
1401da177e4SLinus Torvalds #define BA0_DMR_TYPE_DEMAND	(0<<6)
1411da177e4SLinus Torvalds #define BA0_DMR_TYPE_SINGLE	(1<<6)
1421da177e4SLinus Torvalds #define BA0_DMR_TYPE_BLOCK	(2<<6)
1431da177e4SLinus Torvalds #define BA0_DMR_TYPE_CASCADE	(3<<6)	/* Not supported */
1441da177e4SLinus Torvalds #define BA0_DMR_DEC		(1<<5)	/* Access Increment (0) or Decrement (1) */
1451da177e4SLinus Torvalds #define BA0_DMR_AUTO		(1<<4)	/* Auto-Initialize */
1461da177e4SLinus Torvalds #define BA0_DMR_TR_VERIFY	(0<<2)	/* Verify Transfer */
1471da177e4SLinus Torvalds #define BA0_DMR_TR_WRITE	(1<<2)	/* Write Transfer */
1481da177e4SLinus Torvalds #define BA0_DMR_TR_READ		(2<<2)	/* Read Transfer */
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds #define BA0_DCR_HTCIE		(1<<17)	/* Half Terminal Count Interrupt */
1511da177e4SLinus Torvalds #define BA0_DCR_TCIE		(1<<16)	/* Terminal Count Interrupt */
1521da177e4SLinus Torvalds #define BA0_DCR_MSK		(1<<0)	/* DMA Mask bit */
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds #define BA0_FCR0		0x0180	/* FIFO Control 0 */
1551da177e4SLinus Torvalds #define BA0_FCR1		0x0184	/* FIFO Control 1 */
1561da177e4SLinus Torvalds #define BA0_FCR2		0x0188	/* FIFO Control 2 */
1571da177e4SLinus Torvalds #define BA0_FCR3		0x018c	/* FIFO Control 3 */
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds #define BA0_FCR_FEN		(1<<31)	/* FIFO Enable bit */
1601da177e4SLinus Torvalds #define BA0_FCR_DACZ		(1<<30)	/* DAC Zero */
1611da177e4SLinus Torvalds #define BA0_FCR_PSH		(1<<29)	/* Previous Sample Hold */
1621da177e4SLinus Torvalds #define BA0_FCR_RS(x)		(((x)&0x1f)<<24) /* Right Slot Mapping */
1631da177e4SLinus Torvalds #define BA0_FCR_LS(x)		(((x)&0x1f)<<16) /* Left Slot Mapping */
1641da177e4SLinus Torvalds #define BA0_FCR_SZ(x)		(((x)&0x7f)<<8)	/* FIFO buffer size (in samples) */
1651da177e4SLinus Torvalds #define BA0_FCR_OF(x)		(((x)&0x7f)<<0)	/* FIFO starting offset (in samples) */
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds #define BA0_FPDR0		0x0190	/* FIFO Polled Data 0 */
1681da177e4SLinus Torvalds #define BA0_FPDR1		0x0194	/* FIFO Polled Data 1 */
1691da177e4SLinus Torvalds #define BA0_FPDR2		0x0198	/* FIFO Polled Data 2 */
1701da177e4SLinus Torvalds #define BA0_FPDR3		0x019c	/* FIFO Polled Data 3 */
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds #define BA0_FCHS		0x020c	/* FIFO Channel Status */
1731da177e4SLinus Torvalds #define BA0_FCHS_RCO(x)		(1<<(7+(((x)&3)<<3))) /* Right Channel Out */
1741da177e4SLinus Torvalds #define BA0_FCHS_LCO(x)		(1<<(6+(((x)&3)<<3))) /* Left Channel Out */
1751da177e4SLinus Torvalds #define BA0_FCHS_MRP(x)		(1<<(5+(((x)&3)<<3))) /* Move Read Pointer */
1761da177e4SLinus Torvalds #define BA0_FCHS_FE(x)		(1<<(4+(((x)&3)<<3))) /* FIFO Empty */
1771da177e4SLinus Torvalds #define BA0_FCHS_FF(x)		(1<<(3+(((x)&3)<<3))) /* FIFO Full */
1781da177e4SLinus Torvalds #define BA0_FCHS_IOR(x)		(1<<(2+(((x)&3)<<3))) /* Internal Overrun Flag */
1791da177e4SLinus Torvalds #define BA0_FCHS_RCI(x)		(1<<(1+(((x)&3)<<3))) /* Right Channel In */
1801da177e4SLinus Torvalds #define BA0_FCHS_LCI(x)		(1<<(0+(((x)&3)<<3))) /* Left Channel In */
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds #define BA0_FSIC0		0x0210	/* FIFO Status and Interrupt Control 0 */
1831da177e4SLinus Torvalds #define BA0_FSIC1		0x0214	/* FIFO Status and Interrupt Control 1 */
1841da177e4SLinus Torvalds #define BA0_FSIC2		0x0218	/* FIFO Status and Interrupt Control 2 */
1851da177e4SLinus Torvalds #define BA0_FSIC3		0x021c	/* FIFO Status and Interrupt Control 3 */
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds #define BA0_FSIC_FIC(x)		(((x)&0x7f)<<24) /* FIFO Interrupt Count */
1881da177e4SLinus Torvalds #define BA0_FSIC_FORIE		(1<<23) /* FIFO OverRun Interrupt Enable */
1891da177e4SLinus Torvalds #define BA0_FSIC_FURIE		(1<<22) /* FIFO UnderRun Interrupt Enable */
1901da177e4SLinus Torvalds #define BA0_FSIC_FSCIE		(1<<16)	/* FIFO Sample Count Interrupt Enable */
1911da177e4SLinus Torvalds #define BA0_FSIC_FSC(x)		(((x)&0x7f)<<8) /* FIFO Sample Count */
1921da177e4SLinus Torvalds #define BA0_FSIC_FOR		(1<<7)	/* FIFO OverRun */
1931da177e4SLinus Torvalds #define BA0_FSIC_FUR		(1<<6)	/* FIFO UnderRun */
1941da177e4SLinus Torvalds #define BA0_FSIC_FSCR		(1<<0)	/* FIFO Sample Count Reached */
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds #define BA0_PMCS		0x0344	/* Power Management Control/Status */
1971da177e4SLinus Torvalds #define BA0_CWPR		0x03e0	/* Configuration Write Protect */
198a488e033SArnaud Patard 
1991da177e4SLinus Torvalds #define BA0_EPPMC		0x03e4	/* Extended PCI Power Management Control */
200a488e033SArnaud Patard #define BA0_EPPMC_FPDN		(1<<14) /* Full Power DowN */
201a488e033SArnaud Patard 
2021da177e4SLinus Torvalds #define BA0_GPIOR		0x03e8	/* GPIO Pin Interface Register */
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds #define BA0_SPMC		0x03ec	/* Serial Port Power Management Control (& ASDIN2 enable) */
2051da177e4SLinus Torvalds #define BA0_SPMC_GIPPEN		(1<<15)	/* GP INT Primary PME# Enable */
2061da177e4SLinus Torvalds #define BA0_SPMC_GISPEN		(1<<14)	/* GP INT Secondary PME# Enable */
2071da177e4SLinus Torvalds #define BA0_SPMC_EESPD		(1<<9)	/* EEPROM Serial Port Disable */
2081da177e4SLinus Torvalds #define BA0_SPMC_ASDI2E		(1<<8)	/* ASDIN2 Enable */
2091da177e4SLinus Torvalds #define BA0_SPMC_ASDO		(1<<7)	/* Asynchronous ASDOUT Assertion */
2101da177e4SLinus Torvalds #define BA0_SPMC_WUP2		(1<<3)	/* Wakeup for Secondary Input */
2111da177e4SLinus Torvalds #define BA0_SPMC_WUP1		(1<<2)	/* Wakeup for Primary Input */
2121da177e4SLinus Torvalds #define BA0_SPMC_ASYNC		(1<<1)	/* Asynchronous ASYNC Assertion */
2131da177e4SLinus Torvalds #define BA0_SPMC_RSTN		(1<<0)	/* Reset Not! */
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds #define BA0_CFLR		0x03f0	/* Configuration Load Register (EEPROM or BIOS) */
2161da177e4SLinus Torvalds #define BA0_CFLR_DEFAULT	0x00000001 /* CFLR must be in AC97 link mode */
2171da177e4SLinus Torvalds #define BA0_IISR		0x03f4	/* ISA Interrupt Select */
2181da177e4SLinus Torvalds #define BA0_TMS			0x03f8	/* Test Register */
2191da177e4SLinus Torvalds #define BA0_SSVID		0x03fc	/* Subsystem ID register */
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds #define BA0_CLKCR1		0x0400	/* Clock Control Register 1 */
2221da177e4SLinus Torvalds #define BA0_CLKCR1_CLKON	(1<<25)	/* Read Only */
2231da177e4SLinus Torvalds #define BA0_CLKCR1_DLLRDY	(1<<24)	/* DLL Ready */
2241da177e4SLinus Torvalds #define BA0_CLKCR1_DLLOS	(1<<6)	/* DLL Output Select */
2251da177e4SLinus Torvalds #define BA0_CLKCR1_SWCE		(1<<5)	/* Clock Enable */
2261da177e4SLinus Torvalds #define BA0_CLKCR1_DLLP		(1<<4)	/* DLL PowerUp */
2271da177e4SLinus Torvalds #define BA0_CLKCR1_DLLSS	(((x)&3)<<3) /* DLL Source Select */
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds #define BA0_FRR			0x0410	/* Feature Reporting Register */
2301da177e4SLinus Torvalds #define BA0_SLT12O		0x041c	/* Slot 12 GPIO Output Register for AC-Link */
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds #define BA0_SERMC		0x0420	/* Serial Port Master Control */
2331da177e4SLinus Torvalds #define BA0_SERMC_FCRN		(1<<27)	/* Force Codec Ready Not */
2341da177e4SLinus Torvalds #define BA0_SERMC_ODSEN2	(1<<25)	/* On-Demand Support Enable ASDIN2 */
2351da177e4SLinus Torvalds #define BA0_SERMC_ODSEN1	(1<<24)	/* On-Demand Support Enable ASDIN1 */
2361da177e4SLinus Torvalds #define BA0_SERMC_SXLB		(1<<21)	/* ASDIN2 to ASDOUT Loopback */
2371da177e4SLinus Torvalds #define BA0_SERMC_SLB		(1<<20)	/* ASDOUT to ASDIN2 Loopback */
2381da177e4SLinus Torvalds #define BA0_SERMC_LOVF		(1<<19)	/* Loopback Output Valid Frame bit */
2391da177e4SLinus Torvalds #define BA0_SERMC_TCID(x)	(((x)&3)<<16) /* Target Secondary Codec ID */
2401da177e4SLinus Torvalds #define BA0_SERMC_PXLB		(5<<1)	/* Primary Port External Loopback */
2411da177e4SLinus Torvalds #define BA0_SERMC_PLB		(4<<1)	/* Primary Port Internal Loopback */
2421da177e4SLinus Torvalds #define BA0_SERMC_PTC		(7<<1)	/* Port Timing Configuration */
2431da177e4SLinus Torvalds #define BA0_SERMC_PTC_AC97	(1<<1)	/* AC97 mode */
2441da177e4SLinus Torvalds #define BA0_SERMC_MSPE		(1<<0)	/* Master Serial Port Enable */
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds #define BA0_SERC1		0x0428	/* Serial Port Configuration 1 */
2471da177e4SLinus Torvalds #define BA0_SERC1_SO1F(x)	(((x)&7)>>1) /* Primary Output Port Format */
2481da177e4SLinus Torvalds #define BA0_SERC1_AC97		(1<<1)
2491da177e4SLinus Torvalds #define BA0_SERC1_SO1EN		(1<<0)	/* Primary Output Port Enable */
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds #define BA0_SERC2		0x042c	/* Serial Port Configuration 2 */
2521da177e4SLinus Torvalds #define BA0_SERC2_SI1F(x)	(((x)&7)>>1) /* Primary Input Port Format */
2531da177e4SLinus Torvalds #define BA0_SERC2_AC97		(1<<1)
2541da177e4SLinus Torvalds #define BA0_SERC2_SI1EN		(1<<0)	/* Primary Input Port Enable */
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds #define BA0_SLT12M		0x045c	/* Slot 12 Monitor Register for Primary AC-Link */
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds #define BA0_ACCTL		0x0460	/* AC'97 Control */
2591da177e4SLinus Torvalds #define BA0_ACCTL_TC		(1<<6)	/* Target Codec */
2601da177e4SLinus Torvalds #define BA0_ACCTL_CRW		(1<<4)	/* 0=Write, 1=Read Command */
2611da177e4SLinus Torvalds #define BA0_ACCTL_DCV		(1<<3)	/* Dynamic Command Valid */
2621da177e4SLinus Torvalds #define BA0_ACCTL_VFRM		(1<<2)	/* Valid Frame */
2631da177e4SLinus Torvalds #define BA0_ACCTL_ESYN		(1<<1)	/* Enable Sync */
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds #define BA0_ACSTS		0x0464	/* AC'97 Status */
2661da177e4SLinus Torvalds #define BA0_ACSTS_VSTS		(1<<1)	/* Valid Status */
2671da177e4SLinus Torvalds #define BA0_ACSTS_CRDY		(1<<0)	/* Codec Ready */
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds #define BA0_ACOSV		0x0468	/* AC'97 Output Slot Valid */
2701da177e4SLinus Torvalds #define BA0_ACOSV_SLV(x)	(1<<((x)-3))
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds #define BA0_ACCAD		0x046c	/* AC'97 Command Address */
2731da177e4SLinus Torvalds #define BA0_ACCDA		0x0470	/* AC'97 Command Data */
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds #define BA0_ACISV		0x0474	/* AC'97 Input Slot Valid */
2761da177e4SLinus Torvalds #define BA0_ACISV_SLV(x)	(1<<((x)-3))
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds #define BA0_ACSAD		0x0478	/* AC'97 Status Address */
2791da177e4SLinus Torvalds #define BA0_ACSDA		0x047c	/* AC'97 Status Data */
2801da177e4SLinus Torvalds #define BA0_JSPT		0x0480	/* Joystick poll/trigger */
2811da177e4SLinus Torvalds #define BA0_JSCTL		0x0484	/* Joystick control */
2821da177e4SLinus Torvalds #define BA0_JSC1		0x0488	/* Joystick control */
2831da177e4SLinus Torvalds #define BA0_JSC2		0x048c	/* Joystick control */
2841da177e4SLinus Torvalds #define BA0_JSIO		0x04a0
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds #define BA0_MIDCR		0x0490	/* MIDI Control */
2871da177e4SLinus Torvalds #define BA0_MIDCR_MRST		(1<<5)	/* Reset MIDI Interface */
2881da177e4SLinus Torvalds #define BA0_MIDCR_MLB		(1<<4)	/* MIDI Loop Back Enable */
2891da177e4SLinus Torvalds #define BA0_MIDCR_TIE		(1<<3)	/* MIDI Transmuit Interrupt Enable */
2901da177e4SLinus Torvalds #define BA0_MIDCR_RIE		(1<<2)	/* MIDI Receive Interrupt Enable */
2911da177e4SLinus Torvalds #define BA0_MIDCR_RXE		(1<<1)	/* MIDI Receive Enable */
2921da177e4SLinus Torvalds #define BA0_MIDCR_TXE		(1<<0)	/* MIDI Transmit Enable */
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds #define BA0_MIDCMD		0x0494	/* MIDI Command (wo) */
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds #define BA0_MIDSR		0x0494	/* MIDI Status (ro) */
2971da177e4SLinus Torvalds #define BA0_MIDSR_RDA		(1<<15)	/* Sticky bit (RBE 1->0) */
2981da177e4SLinus Torvalds #define BA0_MIDSR_TBE		(1<<14) /* Sticky bit (TBF 0->1) */
2991da177e4SLinus Torvalds #define BA0_MIDSR_RBE		(1<<7)	/* Receive Buffer Empty */
3001da177e4SLinus Torvalds #define BA0_MIDSR_TBF		(1<<6)	/* Transmit Buffer Full */
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds #define BA0_MIDWP		0x0498	/* MIDI Write */
3031da177e4SLinus Torvalds #define BA0_MIDRP		0x049c	/* MIDI Read (ro) */
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds #define BA0_AODSD1		0x04a8	/* AC'97 On-Demand Slot Disable for primary link (ro) */
3061da177e4SLinus Torvalds #define BA0_AODSD1_NDS(x)	(1<<((x)-3))
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds #define BA0_AODSD2		0x04ac	/* AC'97 On-Demand Slot Disable for secondary link (ro) */
3091da177e4SLinus Torvalds #define BA0_AODSD2_NDS(x)	(1<<((x)-3))
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds #define BA0_CFGI		0x04b0	/* Configure Interface (EEPROM interface) */
3121da177e4SLinus Torvalds #define BA0_SLT12M2		0x04dc	/* Slot 12 Monitor Register 2 for secondary AC-link */
3131da177e4SLinus Torvalds #define BA0_ACSTS2		0x04e4	/* AC'97 Status Register 2 */
3141da177e4SLinus Torvalds #define BA0_ACISV2		0x04f4	/* AC'97 Input Slot Valid Register 2 */
3151da177e4SLinus Torvalds #define BA0_ACSAD2		0x04f8	/* AC'97 Status Address Register 2 */
3161da177e4SLinus Torvalds #define BA0_ACSDA2		0x04fc	/* AC'97 Status Data Register 2 */
3171da177e4SLinus Torvalds #define BA0_FMSR		0x0730	/* FM Synthesis Status (ro) */
3181da177e4SLinus Torvalds #define BA0_B0AP		0x0730	/* FM Bank 0 Address Port (wo) */
3191da177e4SLinus Torvalds #define BA0_FMDP		0x0734	/* FM Data Port */
3201da177e4SLinus Torvalds #define BA0_B1AP		0x0738	/* FM Bank 1 Address Port */
3211da177e4SLinus Torvalds #define BA0_B1DP		0x073c	/* FM Bank 1 Data Port */
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds #define BA0_SSPM		0x0740	/* Sound System Power Management */
3241da177e4SLinus Torvalds #define BA0_SSPM_MIXEN		(1<<6)	/* Playback SRC + FM/Wavetable MIX */
3251da177e4SLinus Torvalds #define BA0_SSPM_CSRCEN		(1<<5)	/* Capture Sample Rate Converter Enable */
3261da177e4SLinus Torvalds #define BA0_SSPM_PSRCEN		(1<<4)	/* Playback Sample Rate Converter Enable */
3271da177e4SLinus Torvalds #define BA0_SSPM_JSEN		(1<<3)	/* Joystick Enable */
3281da177e4SLinus Torvalds #define BA0_SSPM_ACLEN		(1<<2)	/* Serial Port Engine and AC-Link Enable */
3291da177e4SLinus Torvalds #define BA0_SSPM_FMEN		(1<<1)	/* FM Synthesis Block Enable */
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds #define BA0_DACSR		0x0744	/* DAC Sample Rate - Playback SRC */
3321da177e4SLinus Torvalds #define BA0_ADCSR		0x0748	/* ADC Sample Rate - Capture SRC */
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds #define BA0_SSCR		0x074c	/* Sound System Control Register */
3351da177e4SLinus Torvalds #define BA0_SSCR_HVS1		(1<<23)	/* Hardwave Volume Step (0=1,1=2) */
3361da177e4SLinus Torvalds #define BA0_SSCR_MVCS		(1<<19)	/* Master Volume Codec Select */
3371da177e4SLinus Torvalds #define BA0_SSCR_MVLD		(1<<18)	/* Master Volume Line Out Disable */
3381da177e4SLinus Torvalds #define BA0_SSCR_MVAD		(1<<17)	/* Master Volume Alternate Out Disable */
3391da177e4SLinus Torvalds #define BA0_SSCR_MVMD		(1<<16)	/* Master Volume Mono Out Disable */
3401da177e4SLinus Torvalds #define BA0_SSCR_XLPSRC		(1<<8)	/* External SRC Loopback Mode */
3411da177e4SLinus Torvalds #define BA0_SSCR_LPSRC		(1<<7)	/* SRC Loopback Mode */
3421da177e4SLinus Torvalds #define BA0_SSCR_CDTX		(1<<5)	/* CD Transfer Data */
3431da177e4SLinus Torvalds #define BA0_SSCR_HVC		(1<<3)	/* Harware Volume Control Enable */
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds #define BA0_FMLVC		0x0754	/* FM Synthesis Left Volume Control */
3461da177e4SLinus Torvalds #define BA0_FMRVC		0x0758	/* FM Synthesis Right Volume Control */
3471da177e4SLinus Torvalds #define BA0_SRCSA		0x075c	/* SRC Slot Assignments */
3481da177e4SLinus Torvalds #define BA0_PPLVC		0x0760	/* PCM Playback Left Volume Control */
3491da177e4SLinus Torvalds #define BA0_PPRVC		0x0764	/* PCM Playback Right Volume Control */
3501da177e4SLinus Torvalds #define BA0_PASR		0x0768	/* playback sample rate */
3511da177e4SLinus Torvalds #define BA0_CASR		0x076C	/* capture sample rate */
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds /* Source Slot Numbers - Playback */
3541da177e4SLinus Torvalds #define SRCSLOT_LEFT_PCM_PLAYBACK		0
3551da177e4SLinus Torvalds #define SRCSLOT_RIGHT_PCM_PLAYBACK		1
3561da177e4SLinus Torvalds #define SRCSLOT_PHONE_LINE_1_DAC		2
3571da177e4SLinus Torvalds #define SRCSLOT_CENTER_PCM_PLAYBACK		3
3581da177e4SLinus Torvalds #define SRCSLOT_LEFT_SURROUND_PCM_PLAYBACK	4
3591da177e4SLinus Torvalds #define SRCSLOT_RIGHT_SURROUND_PCM_PLAYBACK	5
3601da177e4SLinus Torvalds #define SRCSLOT_LFE_PCM_PLAYBACK		6
3611da177e4SLinus Torvalds #define SRCSLOT_PHONE_LINE_2_DAC		7
3621da177e4SLinus Torvalds #define SRCSLOT_HEADSET_DAC			8
3631da177e4SLinus Torvalds #define SRCSLOT_LEFT_WT				29  /* invalid for BA0_SRCSA */
3641da177e4SLinus Torvalds #define SRCSLOT_RIGHT_WT			30  /* invalid for BA0_SRCSA */
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds /* Source Slot Numbers - Capture */
3671da177e4SLinus Torvalds #define SRCSLOT_LEFT_PCM_RECORD			10
3681da177e4SLinus Torvalds #define SRCSLOT_RIGHT_PCM_RECORD		11
3691da177e4SLinus Torvalds #define SRCSLOT_PHONE_LINE_1_ADC		12
3701da177e4SLinus Torvalds #define SRCSLOT_MIC_ADC				13
3711da177e4SLinus Torvalds #define SRCSLOT_PHONE_LINE_2_ADC		17
3721da177e4SLinus Torvalds #define SRCSLOT_HEADSET_ADC			18
3731da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_LEFT_PCM_RECORD	20
3741da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_RIGHT_PCM_RECORD	21
3751da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_PHONE_LINE_1_ADC	22
3761da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_MIC_ADC		23
3771da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_PHONE_LINE_2_ADC	27
3781da177e4SLinus Torvalds #define SRCSLOT_SECONDARY_HEADSET_ADC		28
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds /* Source Slot Numbers - Others */
3811da177e4SLinus Torvalds #define SRCSLOT_POWER_DOWN			31
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds /* MIDI modes */
3841da177e4SLinus Torvalds #define CS4281_MODE_OUTPUT		(1<<0)
3851da177e4SLinus Torvalds #define CS4281_MODE_INPUT		(1<<1)
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds /* joystick bits */
3881da177e4SLinus Torvalds /* Bits for JSPT */
3891da177e4SLinus Torvalds #define JSPT_CAX                                0x00000001
3901da177e4SLinus Torvalds #define JSPT_CAY                                0x00000002
3911da177e4SLinus Torvalds #define JSPT_CBX                                0x00000004
3921da177e4SLinus Torvalds #define JSPT_CBY                                0x00000008
3931da177e4SLinus Torvalds #define JSPT_BA1                                0x00000010
3941da177e4SLinus Torvalds #define JSPT_BA2                                0x00000020
3951da177e4SLinus Torvalds #define JSPT_BB1                                0x00000040
3961da177e4SLinus Torvalds #define JSPT_BB2                                0x00000080
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds /* Bits for JSCTL */
3991da177e4SLinus Torvalds #define JSCTL_SP_MASK                           0x00000003
4001da177e4SLinus Torvalds #define JSCTL_SP_SLOW                           0x00000000
4011da177e4SLinus Torvalds #define JSCTL_SP_MEDIUM_SLOW                    0x00000001
4021da177e4SLinus Torvalds #define JSCTL_SP_MEDIUM_FAST                    0x00000002
4031da177e4SLinus Torvalds #define JSCTL_SP_FAST                           0x00000003
4041da177e4SLinus Torvalds #define JSCTL_ARE                               0x00000004
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds /* Data register pairs masks */
4071da177e4SLinus Torvalds #define JSC1_Y1V_MASK                           0x0000FFFF
4081da177e4SLinus Torvalds #define JSC1_X1V_MASK                           0xFFFF0000
4091da177e4SLinus Torvalds #define JSC1_Y1V_SHIFT                          0
4101da177e4SLinus Torvalds #define JSC1_X1V_SHIFT                          16
4111da177e4SLinus Torvalds #define JSC2_Y2V_MASK                           0x0000FFFF
4121da177e4SLinus Torvalds #define JSC2_X2V_MASK                           0xFFFF0000
4131da177e4SLinus Torvalds #define JSC2_Y2V_SHIFT                          0
4141da177e4SLinus Torvalds #define JSC2_X2V_SHIFT                          16
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds /* JS GPIO */
4171da177e4SLinus Torvalds #define JSIO_DAX                                0x00000001
4181da177e4SLinus Torvalds #define JSIO_DAY                                0x00000002
4191da177e4SLinus Torvalds #define JSIO_DBX                                0x00000004
4201da177e4SLinus Torvalds #define JSIO_DBY                                0x00000008
4211da177e4SLinus Torvalds #define JSIO_AXOE                               0x00000010
4221da177e4SLinus Torvalds #define JSIO_AYOE                               0x00000020
4231da177e4SLinus Torvalds #define JSIO_BXOE                               0x00000040
4241da177e4SLinus Torvalds #define JSIO_BYOE                               0x00000080
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds /*
4271da177e4SLinus Torvalds  *
4281da177e4SLinus Torvalds  */
4291da177e4SLinus Torvalds 
43093e35f95STakashi Iwai struct cs4281_dma {
43193e35f95STakashi Iwai 	struct snd_pcm_substream *substream;
4321da177e4SLinus Torvalds 	unsigned int regDBA;		/* offset to DBA register */
4331da177e4SLinus Torvalds 	unsigned int regDCA;		/* offset to DCA register */
4341da177e4SLinus Torvalds 	unsigned int regDBC;		/* offset to DBC register */
4351da177e4SLinus Torvalds 	unsigned int regDCC;		/* offset to DCC register */
4361da177e4SLinus Torvalds 	unsigned int regDMR;		/* offset to DMR register */
4371da177e4SLinus Torvalds 	unsigned int regDCR;		/* offset to DCR register */
4381da177e4SLinus Torvalds 	unsigned int regHDSR;		/* offset to HDSR register */
4391da177e4SLinus Torvalds 	unsigned int regFCR;		/* offset to FCR register */
4401da177e4SLinus Torvalds 	unsigned int regFSIC;		/* offset to FSIC register */
4411da177e4SLinus Torvalds 	unsigned int valDMR;		/* DMA mode */
4421da177e4SLinus Torvalds 	unsigned int valDCR;		/* DMA command */
4431da177e4SLinus Torvalds 	unsigned int valFCR;		/* FIFO control */
4441da177e4SLinus Torvalds 	unsigned int fifo_offset;	/* FIFO offset within BA1 */
4451da177e4SLinus Torvalds 	unsigned char left_slot;	/* FIFO left slot */
4461da177e4SLinus Torvalds 	unsigned char right_slot;	/* FIFO right slot */
4471da177e4SLinus Torvalds 	int frag;			/* period number */
4481da177e4SLinus Torvalds };
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds #define SUSPEND_REGISTERS	20
4511da177e4SLinus Torvalds 
45293e35f95STakashi Iwai struct cs4281 {
4531da177e4SLinus Torvalds 	int irq;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	void __iomem *ba0;		/* virtual (accessible) address */
4561da177e4SLinus Torvalds 	void __iomem *ba1;		/* virtual (accessible) address */
4571da177e4SLinus Torvalds 	unsigned long ba0_addr;
4581da177e4SLinus Torvalds 	unsigned long ba1_addr;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	int dual_codec;
4611da177e4SLinus Torvalds 
46293e35f95STakashi Iwai 	struct snd_ac97_bus *ac97_bus;
46393e35f95STakashi Iwai 	struct snd_ac97 *ac97;
46493e35f95STakashi Iwai 	struct snd_ac97 *ac97_secondary;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	struct pci_dev *pci;
46793e35f95STakashi Iwai 	struct snd_card *card;
46893e35f95STakashi Iwai 	struct snd_pcm *pcm;
46993e35f95STakashi Iwai 	struct snd_rawmidi *rmidi;
47093e35f95STakashi Iwai 	struct snd_rawmidi_substream *midi_input;
47193e35f95STakashi Iwai 	struct snd_rawmidi_substream *midi_output;
4721da177e4SLinus Torvalds 
47393e35f95STakashi Iwai 	struct cs4281_dma dma[4];
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	unsigned char src_left_play_slot;
4761da177e4SLinus Torvalds 	unsigned char src_right_play_slot;
4771da177e4SLinus Torvalds 	unsigned char src_left_rec_slot;
4781da177e4SLinus Torvalds 	unsigned char src_right_rec_slot;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	unsigned int spurious_dhtc_irq;
4811da177e4SLinus Torvalds 	unsigned int spurious_dtc_irq;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	spinlock_t reg_lock;
4841da177e4SLinus Torvalds 	unsigned int midcr;
4851da177e4SLinus Torvalds 	unsigned int uartm;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	struct gameport *gameport;
4881da177e4SLinus Torvalds 
489c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
4901da177e4SLinus Torvalds 	u32 suspend_regs[SUSPEND_REGISTERS];
4911da177e4SLinus Torvalds #endif
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds };
4941da177e4SLinus Torvalds 
4957d12e780SDavid Howells static irqreturn_t snd_cs4281_interrupt(int irq, void *dev_id);
4961da177e4SLinus Torvalds 
497cebe41d4SAlexey Dobriyan static DEFINE_PCI_DEVICE_TABLE(snd_cs4281_ids) = {
49828d27aaeSJoe Perches 	{ PCI_VDEVICE(CIRRUS, 0x6005), 0, },	/* CS4281 */
4991da177e4SLinus Torvalds 	{ 0, }
5001da177e4SLinus Torvalds };
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_cs4281_ids);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds /*
5051da177e4SLinus Torvalds  *  constants
5061da177e4SLinus Torvalds  */
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds #define CS4281_FIFO_SIZE	32
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds /*
5111da177e4SLinus Torvalds  *  common I/O routines
5121da177e4SLinus Torvalds  */
5131da177e4SLinus Torvalds 
51493e35f95STakashi Iwai static inline void snd_cs4281_pokeBA0(struct cs4281 *chip, unsigned long offset,
51593e35f95STakashi Iwai 				      unsigned int val)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds         writel(val, chip->ba0 + offset);
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds 
52093e35f95STakashi Iwai static inline unsigned int snd_cs4281_peekBA0(struct cs4281 *chip, unsigned long offset)
5211da177e4SLinus Torvalds {
5221da177e4SLinus Torvalds         return readl(chip->ba0 + offset);
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
52593e35f95STakashi Iwai static void snd_cs4281_ac97_write(struct snd_ac97 *ac97,
5261da177e4SLinus Torvalds 				  unsigned short reg, unsigned short val)
5271da177e4SLinus Torvalds {
5281da177e4SLinus Torvalds 	/*
5291da177e4SLinus Torvalds 	 *  1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
5301da177e4SLinus Torvalds 	 *  2. Write ACCDA = Command Data Register = 470h    for data to write to AC97
5311da177e4SLinus Torvalds 	 *  3. Write ACCTL = Control Register = 460h for initiating the write
5321da177e4SLinus Torvalds 	 *  4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
5331da177e4SLinus Torvalds 	 *  5. if DCV not cleared, break and return error
5341da177e4SLinus Torvalds 	 */
53593e35f95STakashi Iwai 	struct cs4281 *chip = ac97->private_data;
5361da177e4SLinus Torvalds 	int count;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	/*
5391da177e4SLinus Torvalds 	 *  Setup the AC97 control registers on the CS461x to send the
5401da177e4SLinus Torvalds 	 *  appropriate command to the AC97 to perform the read.
5411da177e4SLinus Torvalds 	 *  ACCAD = Command Address Register = 46Ch
5421da177e4SLinus Torvalds 	 *  ACCDA = Command Data Register = 470h
5431da177e4SLinus Torvalds 	 *  ACCTL = Control Register = 460h
5441da177e4SLinus Torvalds 	 *  set DCV - will clear when process completed
5451da177e4SLinus Torvalds 	 *  reset CRW - Write command
5461da177e4SLinus Torvalds 	 *  set VFRM - valid frame enabled
5471da177e4SLinus Torvalds 	 *  set ESYN - ASYNC generation enabled
5481da177e4SLinus Torvalds 	 *  set RSTN - ARST# inactive, AC97 codec not reset
5491da177e4SLinus Torvalds          */
5501da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCAD, reg);
5511da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCDA, val);
5521da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, BA0_ACCTL_DCV | BA0_ACCTL_VFRM |
5531da177e4SLinus Torvalds 				            BA0_ACCTL_ESYN | (ac97->num ? BA0_ACCTL_TC : 0));
5541da177e4SLinus Torvalds 	for (count = 0; count < 2000; count++) {
5551da177e4SLinus Torvalds 		/*
5561da177e4SLinus Torvalds 		 *  First, we want to wait for a short time.
5571da177e4SLinus Torvalds 		 */
5581da177e4SLinus Torvalds 		udelay(10);
5591da177e4SLinus Torvalds 		/*
5601da177e4SLinus Torvalds 		 *  Now, check to see if the write has completed.
5611da177e4SLinus Torvalds 		 *  ACCTL = 460h, DCV should be reset by now and 460h = 07h
5621da177e4SLinus Torvalds 		 */
5631da177e4SLinus Torvalds 		if (!(snd_cs4281_peekBA0(chip, BA0_ACCTL) & BA0_ACCTL_DCV)) {
5641da177e4SLinus Torvalds 			return;
5651da177e4SLinus Torvalds 		}
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds 	snd_printk(KERN_ERR "AC'97 write problem, reg = 0x%x, val = 0x%x\n", reg, val);
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
57093e35f95STakashi Iwai static unsigned short snd_cs4281_ac97_read(struct snd_ac97 *ac97,
5711da177e4SLinus Torvalds 					   unsigned short reg)
5721da177e4SLinus Torvalds {
57393e35f95STakashi Iwai 	struct cs4281 *chip = ac97->private_data;
5741da177e4SLinus Torvalds 	int count;
5751da177e4SLinus Torvalds 	unsigned short result;
5761da177e4SLinus Torvalds 	// FIXME: volatile is necessary in the following due to a bug of
5771da177e4SLinus Torvalds 	// some gcc versions
57893e35f95STakashi Iwai 	volatile int ac97_num = ((volatile struct snd_ac97 *)ac97)->num;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	/*
5811da177e4SLinus Torvalds 	 *  1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
5821da177e4SLinus Torvalds 	 *  2. Write ACCDA = Command Data Register = 470h    for data to write to AC97
5831da177e4SLinus Torvalds 	 *  3. Write ACCTL = Control Register = 460h for initiating the write
5841da177e4SLinus Torvalds 	 *  4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
5851da177e4SLinus Torvalds 	 *  5. if DCV not cleared, break and return error
5861da177e4SLinus Torvalds 	 *  6. Read ACSTS = Status Register = 464h, check VSTS bit
5871da177e4SLinus Torvalds 	 */
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	snd_cs4281_peekBA0(chip, ac97_num ? BA0_ACSDA2 : BA0_ACSDA);
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	/*
5921da177e4SLinus Torvalds 	 *  Setup the AC97 control registers on the CS461x to send the
5931da177e4SLinus Torvalds 	 *  appropriate command to the AC97 to perform the read.
5941da177e4SLinus Torvalds 	 *  ACCAD = Command Address Register = 46Ch
5951da177e4SLinus Torvalds 	 *  ACCDA = Command Data Register = 470h
5961da177e4SLinus Torvalds 	 *  ACCTL = Control Register = 460h
5971da177e4SLinus Torvalds 	 *  set DCV - will clear when process completed
5981da177e4SLinus Torvalds 	 *  set CRW - Read command
5991da177e4SLinus Torvalds 	 *  set VFRM - valid frame enabled
6001da177e4SLinus Torvalds 	 *  set ESYN - ASYNC generation enabled
6011da177e4SLinus Torvalds 	 *  set RSTN - ARST# inactive, AC97 codec not reset
6021da177e4SLinus Torvalds 	 */
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCAD, reg);
6051da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCDA, 0);
6061da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, BA0_ACCTL_DCV | BA0_ACCTL_CRW |
6071da177e4SLinus Torvalds 					    BA0_ACCTL_VFRM | BA0_ACCTL_ESYN |
6081da177e4SLinus Torvalds 			   (ac97_num ? BA0_ACCTL_TC : 0));
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	/*
6121da177e4SLinus Torvalds 	 *  Wait for the read to occur.
6131da177e4SLinus Torvalds 	 */
6141da177e4SLinus Torvalds 	for (count = 0; count < 500; count++) {
6151da177e4SLinus Torvalds 		/*
6161da177e4SLinus Torvalds 		 *  First, we want to wait for a short time.
6171da177e4SLinus Torvalds 	 	 */
6181da177e4SLinus Torvalds 		udelay(10);
6191da177e4SLinus Torvalds 		/*
6201da177e4SLinus Torvalds 		 *  Now, check to see if the read has completed.
6211da177e4SLinus Torvalds 		 *  ACCTL = 460h, DCV should be reset by now and 460h = 17h
6221da177e4SLinus Torvalds 		 */
6231da177e4SLinus Torvalds 		if (!(snd_cs4281_peekBA0(chip, BA0_ACCTL) & BA0_ACCTL_DCV))
6241da177e4SLinus Torvalds 			goto __ok1;
6251da177e4SLinus Torvalds 	}
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	snd_printk(KERN_ERR "AC'97 read problem (ACCTL_DCV), reg = 0x%x\n", reg);
6281da177e4SLinus Torvalds 	result = 0xffff;
6291da177e4SLinus Torvalds 	goto __end;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds       __ok1:
6321da177e4SLinus Torvalds 	/*
6331da177e4SLinus Torvalds 	 *  Wait for the valid status bit to go active.
6341da177e4SLinus Torvalds 	 */
6351da177e4SLinus Torvalds 	for (count = 0; count < 100; count++) {
6361da177e4SLinus Torvalds 		/*
6371da177e4SLinus Torvalds 		 *  Read the AC97 status register.
6381da177e4SLinus Torvalds 		 *  ACSTS = Status Register = 464h
6391da177e4SLinus Torvalds 		 *  VSTS - Valid Status
6401da177e4SLinus Torvalds 		 */
6411da177e4SLinus Torvalds 		if (snd_cs4281_peekBA0(chip, ac97_num ? BA0_ACSTS2 : BA0_ACSTS) & BA0_ACSTS_VSTS)
6421da177e4SLinus Torvalds 			goto __ok2;
6431da177e4SLinus Torvalds 		udelay(10);
6441da177e4SLinus Torvalds 	}
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	snd_printk(KERN_ERR "AC'97 read problem (ACSTS_VSTS), reg = 0x%x\n", reg);
6471da177e4SLinus Torvalds 	result = 0xffff;
6481da177e4SLinus Torvalds 	goto __end;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds       __ok2:
6511da177e4SLinus Torvalds 	/*
6521da177e4SLinus Torvalds 	 *  Read the data returned from the AC97 register.
6531da177e4SLinus Torvalds 	 *  ACSDA = Status Data Register = 474h
6541da177e4SLinus Torvalds 	 */
6551da177e4SLinus Torvalds 	result = snd_cs4281_peekBA0(chip, ac97_num ? BA0_ACSDA2 : BA0_ACSDA);
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds       __end:
6581da177e4SLinus Torvalds 	return result;
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds /*
6621da177e4SLinus Torvalds  *  PCM part
6631da177e4SLinus Torvalds  */
6641da177e4SLinus Torvalds 
66593e35f95STakashi Iwai static int snd_cs4281_trigger(struct snd_pcm_substream *substream, int cmd)
6661da177e4SLinus Torvalds {
66793e35f95STakashi Iwai 	struct cs4281_dma *dma = substream->runtime->private_data;
66893e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
6711da177e4SLinus Torvalds 	switch (cmd) {
6721da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
6731da177e4SLinus Torvalds 		dma->valDCR |= BA0_DCR_MSK;
6741da177e4SLinus Torvalds 		dma->valFCR |= BA0_FCR_FEN;
6751da177e4SLinus Torvalds 		break;
6761da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
6771da177e4SLinus Torvalds 		dma->valDCR &= ~BA0_DCR_MSK;
6781da177e4SLinus Torvalds 		dma->valFCR &= ~BA0_FCR_FEN;
6791da177e4SLinus Torvalds 		break;
6801da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
6811da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_RESUME:
6821da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, dma->regDMR, dma->valDMR & ~BA0_DMR_DMA);
6831da177e4SLinus Torvalds 		dma->valDMR |= BA0_DMR_DMA;
6841da177e4SLinus Torvalds 		dma->valDCR &= ~BA0_DCR_MSK;
6851da177e4SLinus Torvalds 		dma->valFCR |= BA0_FCR_FEN;
6861da177e4SLinus Torvalds 		break;
6871da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
6881da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_SUSPEND:
6891da177e4SLinus Torvalds 		dma->valDMR &= ~(BA0_DMR_DMA|BA0_DMR_POLL);
6901da177e4SLinus Torvalds 		dma->valDCR |= BA0_DCR_MSK;
6911da177e4SLinus Torvalds 		dma->valFCR &= ~BA0_FCR_FEN;
6921da177e4SLinus Torvalds 		/* Leave wave playback FIFO enabled for FM */
6931da177e4SLinus Torvalds 		if (dma->regFCR != BA0_FCR0)
6941da177e4SLinus Torvalds 			dma->valFCR &= ~BA0_FCR_FEN;
6951da177e4SLinus Torvalds 		break;
6961da177e4SLinus Torvalds 	default:
6971da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
6981da177e4SLinus Torvalds 		return -EINVAL;
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regDMR, dma->valDMR);
7011da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regFCR, dma->valFCR);
7021da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regDCR, dma->valDCR);
7031da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
7041da177e4SLinus Torvalds 	return 0;
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds static unsigned int snd_cs4281_rate(unsigned int rate, unsigned int *real_rate)
7081da177e4SLinus Torvalds {
7091da177e4SLinus Torvalds 	unsigned int val = ~0;
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	if (real_rate)
7121da177e4SLinus Torvalds 		*real_rate = rate;
7131da177e4SLinus Torvalds 	/* special "hardcoded" rates */
7141da177e4SLinus Torvalds 	switch (rate) {
7151da177e4SLinus Torvalds 	case 8000:	return 5;
7161da177e4SLinus Torvalds 	case 11025:	return 4;
7171da177e4SLinus Torvalds 	case 16000:	return 3;
7181da177e4SLinus Torvalds 	case 22050:	return 2;
7191da177e4SLinus Torvalds 	case 44100:	return 1;
7201da177e4SLinus Torvalds 	case 48000:	return 0;
7211da177e4SLinus Torvalds 	default:
7221da177e4SLinus Torvalds 		goto __variable;
7231da177e4SLinus Torvalds 	}
7241da177e4SLinus Torvalds       __variable:
7251da177e4SLinus Torvalds 	val = 1536000 / rate;
7261da177e4SLinus Torvalds 	if (real_rate)
7271da177e4SLinus Torvalds 		*real_rate = 1536000 / val;
7281da177e4SLinus Torvalds 	return val;
7291da177e4SLinus Torvalds }
7301da177e4SLinus Torvalds 
73193e35f95STakashi Iwai static void snd_cs4281_mode(struct cs4281 *chip, struct cs4281_dma *dma,
73293e35f95STakashi Iwai 			    struct snd_pcm_runtime *runtime,
73393e35f95STakashi Iwai 			    int capture, int src)
7341da177e4SLinus Torvalds {
7351da177e4SLinus Torvalds 	int rec_mono;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	dma->valDMR = BA0_DMR_TYPE_SINGLE | BA0_DMR_AUTO |
7381da177e4SLinus Torvalds 		      (capture ? BA0_DMR_TR_WRITE : BA0_DMR_TR_READ);
7391da177e4SLinus Torvalds 	if (runtime->channels == 1)
7401da177e4SLinus Torvalds 		dma->valDMR |= BA0_DMR_MONO;
7411da177e4SLinus Torvalds 	if (snd_pcm_format_unsigned(runtime->format) > 0)
7421da177e4SLinus Torvalds 		dma->valDMR |= BA0_DMR_USIGN;
7431da177e4SLinus Torvalds 	if (snd_pcm_format_big_endian(runtime->format) > 0)
7441da177e4SLinus Torvalds 		dma->valDMR |= BA0_DMR_BEND;
7451da177e4SLinus Torvalds 	switch (snd_pcm_format_width(runtime->format)) {
7461da177e4SLinus Torvalds 	case 8: dma->valDMR |= BA0_DMR_SIZE8;
7471da177e4SLinus Torvalds 		if (runtime->channels == 1)
7481da177e4SLinus Torvalds 			dma->valDMR |= BA0_DMR_SWAPC;
7491da177e4SLinus Torvalds 		break;
7501da177e4SLinus Torvalds 	case 32: dma->valDMR |= BA0_DMR_SIZE20; break;
7511da177e4SLinus Torvalds 	}
7521da177e4SLinus Torvalds 	dma->frag = 0;	/* for workaround */
7531da177e4SLinus Torvalds 	dma->valDCR = BA0_DCR_TCIE | BA0_DCR_MSK;
7541da177e4SLinus Torvalds 	if (runtime->buffer_size != runtime->period_size)
7551da177e4SLinus Torvalds 		dma->valDCR |= BA0_DCR_HTCIE;
7561da177e4SLinus Torvalds 	/* Initialize DMA */
7571da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regDBA, runtime->dma_addr);
7581da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regDBC, runtime->buffer_size - 1);
7591da177e4SLinus Torvalds 	rec_mono = (chip->dma[1].valDMR & BA0_DMR_MONO) == BA0_DMR_MONO;
7601da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SRCSA, (chip->src_left_play_slot << 0) |
7611da177e4SLinus Torvalds 					    (chip->src_right_play_slot << 8) |
7621da177e4SLinus Torvalds 					    (chip->src_left_rec_slot << 16) |
7631da177e4SLinus Torvalds 					    ((rec_mono ? 31 : chip->src_right_rec_slot) << 24));
7641da177e4SLinus Torvalds 	if (!src)
7651da177e4SLinus Torvalds 		goto __skip_src;
7661da177e4SLinus Torvalds 	if (!capture) {
7671da177e4SLinus Torvalds 		if (dma->left_slot == chip->src_left_play_slot) {
7681da177e4SLinus Torvalds 			unsigned int val = snd_cs4281_rate(runtime->rate, NULL);
769da3cec35STakashi Iwai 			snd_BUG_ON(dma->right_slot != chip->src_right_play_slot);
7701da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_DACSR, val);
7711da177e4SLinus Torvalds 		}
7721da177e4SLinus Torvalds 	} else {
7731da177e4SLinus Torvalds 		if (dma->left_slot == chip->src_left_rec_slot) {
7741da177e4SLinus Torvalds 			unsigned int val = snd_cs4281_rate(runtime->rate, NULL);
775da3cec35STakashi Iwai 			snd_BUG_ON(dma->right_slot != chip->src_right_rec_slot);
7761da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_ADCSR, val);
7771da177e4SLinus Torvalds 		}
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds       __skip_src:
7801da177e4SLinus Torvalds 	/* Deactivate wave playback FIFO before changing slot assignments */
7811da177e4SLinus Torvalds 	if (dma->regFCR == BA0_FCR0)
7821da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, dma->regFCR, snd_cs4281_peekBA0(chip, dma->regFCR) & ~BA0_FCR_FEN);
7831da177e4SLinus Torvalds 	/* Initialize FIFO */
7841da177e4SLinus Torvalds 	dma->valFCR = BA0_FCR_LS(dma->left_slot) |
7851da177e4SLinus Torvalds 		      BA0_FCR_RS(capture && (dma->valDMR & BA0_DMR_MONO) ? 31 : dma->right_slot) |
7861da177e4SLinus Torvalds 		      BA0_FCR_SZ(CS4281_FIFO_SIZE) |
7871da177e4SLinus Torvalds 		      BA0_FCR_OF(dma->fifo_offset);
7881da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regFCR, dma->valFCR | (capture ? BA0_FCR_PSH : 0));
7891da177e4SLinus Torvalds 	/* Activate FIFO again for FM playback */
7901da177e4SLinus Torvalds 	if (dma->regFCR == BA0_FCR0)
7911da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, dma->regFCR, dma->valFCR | BA0_FCR_FEN);
7921da177e4SLinus Torvalds 	/* Clear FIFO Status and Interrupt Control Register */
7931da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, dma->regFSIC, 0);
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
79693e35f95STakashi Iwai static int snd_cs4281_hw_params(struct snd_pcm_substream *substream,
79793e35f95STakashi Iwai 				struct snd_pcm_hw_params *hw_params)
7981da177e4SLinus Torvalds {
7991da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
8001da177e4SLinus Torvalds }
8011da177e4SLinus Torvalds 
80293e35f95STakashi Iwai static int snd_cs4281_hw_free(struct snd_pcm_substream *substream)
8031da177e4SLinus Torvalds {
8041da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
8051da177e4SLinus Torvalds }
8061da177e4SLinus Torvalds 
80793e35f95STakashi Iwai static int snd_cs4281_playback_prepare(struct snd_pcm_substream *substream)
8081da177e4SLinus Torvalds {
80993e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
81093e35f95STakashi Iwai 	struct cs4281_dma *dma = runtime->private_data;
81193e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
8141da177e4SLinus Torvalds 	snd_cs4281_mode(chip, dma, runtime, 0, 1);
8151da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
8161da177e4SLinus Torvalds 	return 0;
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
81993e35f95STakashi Iwai static int snd_cs4281_capture_prepare(struct snd_pcm_substream *substream)
8201da177e4SLinus Torvalds {
82193e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
82293e35f95STakashi Iwai 	struct cs4281_dma *dma = runtime->private_data;
82393e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
8261da177e4SLinus Torvalds 	snd_cs4281_mode(chip, dma, runtime, 1, 1);
8271da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
8281da177e4SLinus Torvalds 	return 0;
8291da177e4SLinus Torvalds }
8301da177e4SLinus Torvalds 
83193e35f95STakashi Iwai static snd_pcm_uframes_t snd_cs4281_pointer(struct snd_pcm_substream *substream)
8321da177e4SLinus Torvalds {
83393e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
83493e35f95STakashi Iwai 	struct cs4281_dma *dma = runtime->private_data;
83593e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
8361da177e4SLinus Torvalds 
837ee419653STakashi Iwai 	/*
838ee419653STakashi Iwai 	printk(KERN_DEBUG "DCC = 0x%x, buffer_size = 0x%x, jiffies = %li\n",
839ee419653STakashi Iwai 	       snd_cs4281_peekBA0(chip, dma->regDCC), runtime->buffer_size,
840ee419653STakashi Iwai 	       jiffies);
841ee419653STakashi Iwai 	*/
8421da177e4SLinus Torvalds 	return runtime->buffer_size -
8431da177e4SLinus Torvalds 	       snd_cs4281_peekBA0(chip, dma->regDCC) - 1;
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
84693e35f95STakashi Iwai static struct snd_pcm_hardware snd_cs4281_playback =
8471da177e4SLinus Torvalds {
848b83f346bSClemens Ladisch 	.info =			SNDRV_PCM_INFO_MMAP |
8491da177e4SLinus Torvalds 				SNDRV_PCM_INFO_INTERLEAVED |
8501da177e4SLinus Torvalds 				SNDRV_PCM_INFO_MMAP_VALID |
8511da177e4SLinus Torvalds 				SNDRV_PCM_INFO_PAUSE |
852b83f346bSClemens Ladisch 				SNDRV_PCM_INFO_RESUME,
8531da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
8541da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
8551da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
8561da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
8571da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE,
8581da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8591da177e4SLinus Torvalds 	.rate_min =		4000,
8601da177e4SLinus Torvalds 	.rate_max =		48000,
8611da177e4SLinus Torvalds 	.channels_min =		1,
8621da177e4SLinus Torvalds 	.channels_max =		2,
8631da177e4SLinus Torvalds 	.buffer_bytes_max =	(512*1024),
8641da177e4SLinus Torvalds 	.period_bytes_min =	64,
8651da177e4SLinus Torvalds 	.period_bytes_max =	(512*1024),
8661da177e4SLinus Torvalds 	.periods_min =		1,
8671da177e4SLinus Torvalds 	.periods_max =		2,
8681da177e4SLinus Torvalds 	.fifo_size =		CS4281_FIFO_SIZE,
8691da177e4SLinus Torvalds };
8701da177e4SLinus Torvalds 
87193e35f95STakashi Iwai static struct snd_pcm_hardware snd_cs4281_capture =
8721da177e4SLinus Torvalds {
873b83f346bSClemens Ladisch 	.info =			SNDRV_PCM_INFO_MMAP |
8741da177e4SLinus Torvalds 				SNDRV_PCM_INFO_INTERLEAVED |
8751da177e4SLinus Torvalds 				SNDRV_PCM_INFO_MMAP_VALID |
8761da177e4SLinus Torvalds 				SNDRV_PCM_INFO_PAUSE |
877b83f346bSClemens Ladisch 				SNDRV_PCM_INFO_RESUME,
8781da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
8791da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
8801da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
8811da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
8821da177e4SLinus Torvalds 				SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE,
8831da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8841da177e4SLinus Torvalds 	.rate_min =		4000,
8851da177e4SLinus Torvalds 	.rate_max =		48000,
8861da177e4SLinus Torvalds 	.channels_min =		1,
8871da177e4SLinus Torvalds 	.channels_max =		2,
8881da177e4SLinus Torvalds 	.buffer_bytes_max =	(512*1024),
8891da177e4SLinus Torvalds 	.period_bytes_min =	64,
8901da177e4SLinus Torvalds 	.period_bytes_max =	(512*1024),
8911da177e4SLinus Torvalds 	.periods_min =		1,
8921da177e4SLinus Torvalds 	.periods_max =		2,
8931da177e4SLinus Torvalds 	.fifo_size =		CS4281_FIFO_SIZE,
8941da177e4SLinus Torvalds };
8951da177e4SLinus Torvalds 
89693e35f95STakashi Iwai static int snd_cs4281_playback_open(struct snd_pcm_substream *substream)
8971da177e4SLinus Torvalds {
89893e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
89993e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
90093e35f95STakashi Iwai 	struct cs4281_dma *dma;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	dma = &chip->dma[0];
9031da177e4SLinus Torvalds 	dma->substream = substream;
9041da177e4SLinus Torvalds 	dma->left_slot = 0;
9051da177e4SLinus Torvalds 	dma->right_slot = 1;
9061da177e4SLinus Torvalds 	runtime->private_data = dma;
9071da177e4SLinus Torvalds 	runtime->hw = snd_cs4281_playback;
9081da177e4SLinus Torvalds 	/* should be detected from the AC'97 layer, but it seems
9091da177e4SLinus Torvalds 	   that although CS4297A rev B reports 18-bit ADC resolution,
9101da177e4SLinus Torvalds 	   samples are 20-bit */
9111da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 20);
9121da177e4SLinus Torvalds 	return 0;
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
91593e35f95STakashi Iwai static int snd_cs4281_capture_open(struct snd_pcm_substream *substream)
9161da177e4SLinus Torvalds {
91793e35f95STakashi Iwai 	struct cs4281 *chip = snd_pcm_substream_chip(substream);
91893e35f95STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
91993e35f95STakashi Iwai 	struct cs4281_dma *dma;
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	dma = &chip->dma[1];
9221da177e4SLinus Torvalds 	dma->substream = substream;
9231da177e4SLinus Torvalds 	dma->left_slot = 10;
9241da177e4SLinus Torvalds 	dma->right_slot = 11;
9251da177e4SLinus Torvalds 	runtime->private_data = dma;
9261da177e4SLinus Torvalds 	runtime->hw = snd_cs4281_capture;
9271da177e4SLinus Torvalds 	/* should be detected from the AC'97 layer, but it seems
9281da177e4SLinus Torvalds 	   that although CS4297A rev B reports 18-bit ADC resolution,
9291da177e4SLinus Torvalds 	   samples are 20-bit */
9301da177e4SLinus Torvalds 	snd_pcm_hw_constraint_msbits(runtime, 0, 32, 20);
9311da177e4SLinus Torvalds 	return 0;
9321da177e4SLinus Torvalds }
9331da177e4SLinus Torvalds 
93493e35f95STakashi Iwai static int snd_cs4281_playback_close(struct snd_pcm_substream *substream)
9351da177e4SLinus Torvalds {
93693e35f95STakashi Iwai 	struct cs4281_dma *dma = substream->runtime->private_data;
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	dma->substream = NULL;
9391da177e4SLinus Torvalds 	return 0;
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
94293e35f95STakashi Iwai static int snd_cs4281_capture_close(struct snd_pcm_substream *substream)
9431da177e4SLinus Torvalds {
94493e35f95STakashi Iwai 	struct cs4281_dma *dma = substream->runtime->private_data;
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	dma->substream = NULL;
9471da177e4SLinus Torvalds 	return 0;
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
95093e35f95STakashi Iwai static struct snd_pcm_ops snd_cs4281_playback_ops = {
9511da177e4SLinus Torvalds 	.open =		snd_cs4281_playback_open,
9521da177e4SLinus Torvalds 	.close =	snd_cs4281_playback_close,
9531da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
9541da177e4SLinus Torvalds 	.hw_params =	snd_cs4281_hw_params,
9551da177e4SLinus Torvalds 	.hw_free =	snd_cs4281_hw_free,
9561da177e4SLinus Torvalds 	.prepare =	snd_cs4281_playback_prepare,
9571da177e4SLinus Torvalds 	.trigger =	snd_cs4281_trigger,
9581da177e4SLinus Torvalds 	.pointer =	snd_cs4281_pointer,
9591da177e4SLinus Torvalds };
9601da177e4SLinus Torvalds 
96193e35f95STakashi Iwai static struct snd_pcm_ops snd_cs4281_capture_ops = {
9621da177e4SLinus Torvalds 	.open =		snd_cs4281_capture_open,
9631da177e4SLinus Torvalds 	.close =	snd_cs4281_capture_close,
9641da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
9651da177e4SLinus Torvalds 	.hw_params =	snd_cs4281_hw_params,
9661da177e4SLinus Torvalds 	.hw_free =	snd_cs4281_hw_free,
9671da177e4SLinus Torvalds 	.prepare =	snd_cs4281_capture_prepare,
9681da177e4SLinus Torvalds 	.trigger =	snd_cs4281_trigger,
9691da177e4SLinus Torvalds 	.pointer =	snd_cs4281_pointer,
9701da177e4SLinus Torvalds };
9711da177e4SLinus Torvalds 
972*e23e7a14SBill Pemberton static int snd_cs4281_pcm(struct cs4281 *chip, int device,
97393e35f95STakashi Iwai 			  struct snd_pcm **rpcm)
9741da177e4SLinus Torvalds {
97593e35f95STakashi Iwai 	struct snd_pcm *pcm;
9761da177e4SLinus Torvalds 	int err;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	if (rpcm)
9791da177e4SLinus Torvalds 		*rpcm = NULL;
9801da177e4SLinus Torvalds 	err = snd_pcm_new(chip->card, "CS4281", device, 1, 1, &pcm);
9811da177e4SLinus Torvalds 	if (err < 0)
9821da177e4SLinus Torvalds 		return err;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs4281_playback_ops);
9851da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs4281_capture_ops);
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	pcm->private_data = chip;
9881da177e4SLinus Torvalds 	pcm->info_flags = 0;
9891da177e4SLinus Torvalds 	strcpy(pcm->name, "CS4281");
9901da177e4SLinus Torvalds 	chip->pcm = pcm;
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
9931da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci), 64*1024, 512*1024);
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	if (rpcm)
9961da177e4SLinus Torvalds 		*rpcm = pcm;
9971da177e4SLinus Torvalds 	return 0;
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds /*
10011da177e4SLinus Torvalds  *  Mixer section
10021da177e4SLinus Torvalds  */
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds #define CS_VOL_MASK	0x1f
10051da177e4SLinus Torvalds 
100693e35f95STakashi Iwai static int snd_cs4281_info_volume(struct snd_kcontrol *kcontrol,
100793e35f95STakashi Iwai 				  struct snd_ctl_elem_info *uinfo)
10081da177e4SLinus Torvalds {
10091da177e4SLinus Torvalds 	uinfo->type              = SNDRV_CTL_ELEM_TYPE_INTEGER;
10101da177e4SLinus Torvalds 	uinfo->count             = 2;
10111da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
10121da177e4SLinus Torvalds 	uinfo->value.integer.max = CS_VOL_MASK;
10131da177e4SLinus Torvalds 	return 0;
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
101693e35f95STakashi Iwai static int snd_cs4281_get_volume(struct snd_kcontrol *kcontrol,
101793e35f95STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
10181da177e4SLinus Torvalds {
101993e35f95STakashi Iwai 	struct cs4281 *chip = snd_kcontrol_chip(kcontrol);
10201da177e4SLinus Torvalds 	int regL = (kcontrol->private_value >> 16) & 0xffff;
10211da177e4SLinus Torvalds 	int regR = kcontrol->private_value & 0xffff;
10221da177e4SLinus Torvalds 	int volL, volR;
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	volL = CS_VOL_MASK - (snd_cs4281_peekBA0(chip, regL) & CS_VOL_MASK);
10251da177e4SLinus Torvalds 	volR = CS_VOL_MASK - (snd_cs4281_peekBA0(chip, regR) & CS_VOL_MASK);
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = volL;
10281da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = volR;
10291da177e4SLinus Torvalds 	return 0;
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds 
103293e35f95STakashi Iwai static int snd_cs4281_put_volume(struct snd_kcontrol *kcontrol,
103393e35f95STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
10341da177e4SLinus Torvalds {
103593e35f95STakashi Iwai 	struct cs4281 *chip = snd_kcontrol_chip(kcontrol);
10361da177e4SLinus Torvalds 	int change = 0;
10371da177e4SLinus Torvalds 	int regL = (kcontrol->private_value >> 16) & 0xffff;
10381da177e4SLinus Torvalds 	int regR = kcontrol->private_value & 0xffff;
10391da177e4SLinus Torvalds 	int volL, volR;
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	volL = CS_VOL_MASK - (snd_cs4281_peekBA0(chip, regL) & CS_VOL_MASK);
10421da177e4SLinus Torvalds 	volR = CS_VOL_MASK - (snd_cs4281_peekBA0(chip, regR) & CS_VOL_MASK);
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	if (ucontrol->value.integer.value[0] != volL) {
10451da177e4SLinus Torvalds 		volL = CS_VOL_MASK - (ucontrol->value.integer.value[0] & CS_VOL_MASK);
10461da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, regL, volL);
10471da177e4SLinus Torvalds 		change = 1;
10481da177e4SLinus Torvalds 	}
1049e860f000STakashi Iwai 	if (ucontrol->value.integer.value[1] != volR) {
10501da177e4SLinus Torvalds 		volR = CS_VOL_MASK - (ucontrol->value.integer.value[1] & CS_VOL_MASK);
10511da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, regR, volR);
10521da177e4SLinus Torvalds 		change = 1;
10531da177e4SLinus Torvalds 	}
10541da177e4SLinus Torvalds 	return change;
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
10570cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -4650, 150, 0);
10589f6ab250STakashi Iwai 
105993e35f95STakashi Iwai static struct snd_kcontrol_new snd_cs4281_fm_vol =
10601da177e4SLinus Torvalds {
10611da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
10621da177e4SLinus Torvalds 	.name = "Synth Playback Volume",
10631da177e4SLinus Torvalds 	.info = snd_cs4281_info_volume,
10641da177e4SLinus Torvalds 	.get = snd_cs4281_get_volume,
10651da177e4SLinus Torvalds 	.put = snd_cs4281_put_volume,
10661da177e4SLinus Torvalds 	.private_value = ((BA0_FMLVC << 16) | BA0_FMRVC),
10679f6ab250STakashi Iwai 	.tlv = { .p = db_scale_dsp },
10681da177e4SLinus Torvalds };
10691da177e4SLinus Torvalds 
107093e35f95STakashi Iwai static struct snd_kcontrol_new snd_cs4281_pcm_vol =
10711da177e4SLinus Torvalds {
10721da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
10731da177e4SLinus Torvalds 	.name = "PCM Stream Playback Volume",
10741da177e4SLinus Torvalds 	.info = snd_cs4281_info_volume,
10751da177e4SLinus Torvalds 	.get = snd_cs4281_get_volume,
10761da177e4SLinus Torvalds 	.put = snd_cs4281_put_volume,
10771da177e4SLinus Torvalds 	.private_value = ((BA0_PPLVC << 16) | BA0_PPRVC),
10789f6ab250STakashi Iwai 	.tlv = { .p = db_scale_dsp },
10791da177e4SLinus Torvalds };
10801da177e4SLinus Torvalds 
108193e35f95STakashi Iwai static void snd_cs4281_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
10821da177e4SLinus Torvalds {
108393e35f95STakashi Iwai 	struct cs4281 *chip = bus->private_data;
10841da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
108793e35f95STakashi Iwai static void snd_cs4281_mixer_free_ac97(struct snd_ac97 *ac97)
10881da177e4SLinus Torvalds {
108993e35f95STakashi Iwai 	struct cs4281 *chip = ac97->private_data;
10901da177e4SLinus Torvalds 	if (ac97->num)
10911da177e4SLinus Torvalds 		chip->ac97_secondary = NULL;
10921da177e4SLinus Torvalds 	else
10931da177e4SLinus Torvalds 		chip->ac97 = NULL;
10941da177e4SLinus Torvalds }
10951da177e4SLinus Torvalds 
1096*e23e7a14SBill Pemberton static int snd_cs4281_mixer(struct cs4281 *chip)
10971da177e4SLinus Torvalds {
109893e35f95STakashi Iwai 	struct snd_card *card = chip->card;
109993e35f95STakashi Iwai 	struct snd_ac97_template ac97;
11001da177e4SLinus Torvalds 	int err;
110193e35f95STakashi Iwai 	static struct snd_ac97_bus_ops ops = {
11021da177e4SLinus Torvalds 		.write = snd_cs4281_ac97_write,
11031da177e4SLinus Torvalds 		.read = snd_cs4281_ac97_read,
11041da177e4SLinus Torvalds 	};
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus)) < 0)
11071da177e4SLinus Torvalds 		return err;
11081da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_cs4281_mixer_free_ac97_bus;
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
11111da177e4SLinus Torvalds 	ac97.private_data = chip;
11121da177e4SLinus Torvalds 	ac97.private_free = snd_cs4281_mixer_free_ac97;
11131da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
11141da177e4SLinus Torvalds 		return err;
11151da177e4SLinus Torvalds 	if (chip->dual_codec) {
11161da177e4SLinus Torvalds 		ac97.num = 1;
11171da177e4SLinus Torvalds 		if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_secondary)) < 0)
11181da177e4SLinus Torvalds 			return err;
11191da177e4SLinus Torvalds 	}
11201da177e4SLinus Torvalds 	if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4281_fm_vol, chip))) < 0)
11211da177e4SLinus Torvalds 		return err;
11221da177e4SLinus Torvalds 	if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4281_pcm_vol, chip))) < 0)
11231da177e4SLinus Torvalds 		return err;
11241da177e4SLinus Torvalds 	return 0;
11251da177e4SLinus Torvalds }
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds /*
11291da177e4SLinus Torvalds  * proc interface
11301da177e4SLinus Torvalds  */
11311da177e4SLinus Torvalds 
113293e35f95STakashi Iwai static void snd_cs4281_proc_read(struct snd_info_entry *entry,
113393e35f95STakashi Iwai 				  struct snd_info_buffer *buffer)
11341da177e4SLinus Torvalds {
113593e35f95STakashi Iwai 	struct cs4281 *chip = entry->private_data;
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	snd_iprintf(buffer, "Cirrus Logic CS4281\n\n");
11381da177e4SLinus Torvalds 	snd_iprintf(buffer, "Spurious half IRQs   : %u\n", chip->spurious_dhtc_irq);
11391da177e4SLinus Torvalds 	snd_iprintf(buffer, "Spurious end IRQs    : %u\n", chip->spurious_dtc_irq);
11401da177e4SLinus Torvalds }
11411da177e4SLinus Torvalds 
114224e4a121STakashi Iwai static ssize_t snd_cs4281_BA0_read(struct snd_info_entry *entry,
114393e35f95STakashi Iwai 				   void *file_private_data,
11441da177e4SLinus Torvalds 				   struct file *file, char __user *buf,
114524e4a121STakashi Iwai 				   size_t count, loff_t pos)
11461da177e4SLinus Torvalds {
114793e35f95STakashi Iwai 	struct cs4281 *chip = entry->private_data;
11481da177e4SLinus Torvalds 
1149d97e1b78STakashi Iwai 	if (copy_to_user_fromio(buf, chip->ba0 + pos, count))
11501da177e4SLinus Torvalds 		return -EFAULT;
1151d97e1b78STakashi Iwai 	return count;
11521da177e4SLinus Torvalds }
11531da177e4SLinus Torvalds 
115424e4a121STakashi Iwai static ssize_t snd_cs4281_BA1_read(struct snd_info_entry *entry,
115593e35f95STakashi Iwai 				   void *file_private_data,
11561da177e4SLinus Torvalds 				   struct file *file, char __user *buf,
115724e4a121STakashi Iwai 				   size_t count, loff_t pos)
11581da177e4SLinus Torvalds {
115993e35f95STakashi Iwai 	struct cs4281 *chip = entry->private_data;
11601da177e4SLinus Torvalds 
1161d97e1b78STakashi Iwai 	if (copy_to_user_fromio(buf, chip->ba1 + pos, count))
11621da177e4SLinus Torvalds 		return -EFAULT;
1163d97e1b78STakashi Iwai 	return count;
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds static struct snd_info_entry_ops snd_cs4281_proc_ops_BA0 = {
11671da177e4SLinus Torvalds 	.read = snd_cs4281_BA0_read,
11681da177e4SLinus Torvalds };
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds static struct snd_info_entry_ops snd_cs4281_proc_ops_BA1 = {
11711da177e4SLinus Torvalds 	.read = snd_cs4281_BA1_read,
11721da177e4SLinus Torvalds };
11731da177e4SLinus Torvalds 
1174*e23e7a14SBill Pemberton static void snd_cs4281_proc_init(struct cs4281 *chip)
11751da177e4SLinus Torvalds {
117693e35f95STakashi Iwai 	struct snd_info_entry *entry;
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	if (! snd_card_proc_new(chip->card, "cs4281", &entry))
1179bf850204STakashi Iwai 		snd_info_set_text_ops(entry, chip, snd_cs4281_proc_read);
11801da177e4SLinus Torvalds 	if (! snd_card_proc_new(chip->card, "cs4281_BA0", &entry)) {
11811da177e4SLinus Torvalds 		entry->content = SNDRV_INFO_CONTENT_DATA;
11821da177e4SLinus Torvalds 		entry->private_data = chip;
11831da177e4SLinus Torvalds 		entry->c.ops = &snd_cs4281_proc_ops_BA0;
11841da177e4SLinus Torvalds 		entry->size = CS4281_BA0_SIZE;
11851da177e4SLinus Torvalds 	}
11861da177e4SLinus Torvalds 	if (! snd_card_proc_new(chip->card, "cs4281_BA1", &entry)) {
11871da177e4SLinus Torvalds 		entry->content = SNDRV_INFO_CONTENT_DATA;
11881da177e4SLinus Torvalds 		entry->private_data = chip;
11891da177e4SLinus Torvalds 		entry->c.ops = &snd_cs4281_proc_ops_BA1;
11901da177e4SLinus Torvalds 		entry->size = CS4281_BA1_SIZE;
11911da177e4SLinus Torvalds 	}
11921da177e4SLinus Torvalds }
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds /*
11951da177e4SLinus Torvalds  * joystick support
11961da177e4SLinus Torvalds  */
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds static void snd_cs4281_gameport_trigger(struct gameport *gameport)
12011da177e4SLinus Torvalds {
120293e35f95STakashi Iwai 	struct cs4281 *chip = gameport_get_port_data(gameport);
12031da177e4SLinus Torvalds 
1204da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
1205da3cec35STakashi Iwai 		return;
12061da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_JSPT, 0xff);
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds static unsigned char snd_cs4281_gameport_read(struct gameport *gameport)
12101da177e4SLinus Torvalds {
121193e35f95STakashi Iwai 	struct cs4281 *chip = gameport_get_port_data(gameport);
12121da177e4SLinus Torvalds 
1213da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
1214da3cec35STakashi Iwai 		return 0;
12151da177e4SLinus Torvalds 	return snd_cs4281_peekBA0(chip, BA0_JSPT);
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds #ifdef COOKED_MODE
121993e35f95STakashi Iwai static int snd_cs4281_gameport_cooked_read(struct gameport *gameport,
122093e35f95STakashi Iwai 					   int *axes, int *buttons)
12211da177e4SLinus Torvalds {
122293e35f95STakashi Iwai 	struct cs4281 *chip = gameport_get_port_data(gameport);
12231da177e4SLinus Torvalds 	unsigned js1, js2, jst;
12241da177e4SLinus Torvalds 
1225da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
1226da3cec35STakashi Iwai 		return 0;
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	js1 = snd_cs4281_peekBA0(chip, BA0_JSC1);
12291da177e4SLinus Torvalds 	js2 = snd_cs4281_peekBA0(chip, BA0_JSC2);
12301da177e4SLinus Torvalds 	jst = snd_cs4281_peekBA0(chip, BA0_JSPT);
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	*buttons = (~jst >> 4) & 0x0F;
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds 	axes[0] = ((js1 & JSC1_Y1V_MASK) >> JSC1_Y1V_SHIFT) & 0xFFFF;
12351da177e4SLinus Torvalds 	axes[1] = ((js1 & JSC1_X1V_MASK) >> JSC1_X1V_SHIFT) & 0xFFFF;
12361da177e4SLinus Torvalds 	axes[2] = ((js2 & JSC2_Y2V_MASK) >> JSC2_Y2V_SHIFT) & 0xFFFF;
12371da177e4SLinus Torvalds 	axes[3] = ((js2 & JSC2_X2V_MASK) >> JSC2_X2V_SHIFT) & 0xFFFF;
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	for (jst = 0; jst < 4; ++jst)
12401da177e4SLinus Torvalds 		if (axes[jst] == 0xFFFF) axes[jst] = -1;
12411da177e4SLinus Torvalds 	return 0;
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds #else
12441da177e4SLinus Torvalds #define snd_cs4281_gameport_cooked_read	NULL
12451da177e4SLinus Torvalds #endif
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds static int snd_cs4281_gameport_open(struct gameport *gameport, int mode)
12481da177e4SLinus Torvalds {
12491da177e4SLinus Torvalds 	switch (mode) {
12501da177e4SLinus Torvalds #ifdef COOKED_MODE
12511da177e4SLinus Torvalds 	case GAMEPORT_MODE_COOKED:
12521da177e4SLinus Torvalds 		return 0;
12531da177e4SLinus Torvalds #endif
12541da177e4SLinus Torvalds 	case GAMEPORT_MODE_RAW:
12551da177e4SLinus Torvalds 		return 0;
12561da177e4SLinus Torvalds 	default:
12571da177e4SLinus Torvalds 		return -1;
12581da177e4SLinus Torvalds 	}
12591da177e4SLinus Torvalds 	return 0;
12601da177e4SLinus Torvalds }
12611da177e4SLinus Torvalds 
1262*e23e7a14SBill Pemberton static int snd_cs4281_create_gameport(struct cs4281 *chip)
12631da177e4SLinus Torvalds {
12641da177e4SLinus Torvalds 	struct gameport *gp;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	chip->gameport = gp = gameport_allocate_port();
12671da177e4SLinus Torvalds 	if (!gp) {
12681da177e4SLinus Torvalds 		printk(KERN_ERR "cs4281: cannot allocate memory for gameport\n");
12691da177e4SLinus Torvalds 		return -ENOMEM;
12701da177e4SLinus Torvalds 	}
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	gameport_set_name(gp, "CS4281 Gameport");
12731da177e4SLinus Torvalds 	gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
12741da177e4SLinus Torvalds 	gameport_set_dev_parent(gp, &chip->pci->dev);
12751da177e4SLinus Torvalds 	gp->open = snd_cs4281_gameport_open;
12761da177e4SLinus Torvalds 	gp->read = snd_cs4281_gameport_read;
12771da177e4SLinus Torvalds 	gp->trigger = snd_cs4281_gameport_trigger;
12781da177e4SLinus Torvalds 	gp->cooked_read = snd_cs4281_gameport_cooked_read;
12791da177e4SLinus Torvalds 	gameport_set_port_data(gp, chip);
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_JSIO, 0xFF); // ?
12821da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_JSCTL, JSCTL_SP_MEDIUM_SLOW);
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	gameport_register_port(gp);
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	return 0;
12871da177e4SLinus Torvalds }
12881da177e4SLinus Torvalds 
128993e35f95STakashi Iwai static void snd_cs4281_free_gameport(struct cs4281 *chip)
12901da177e4SLinus Torvalds {
12911da177e4SLinus Torvalds 	if (chip->gameport) {
12921da177e4SLinus Torvalds 		gameport_unregister_port(chip->gameport);
12931da177e4SLinus Torvalds 		chip->gameport = NULL;
12941da177e4SLinus Torvalds 	}
12951da177e4SLinus Torvalds }
12961da177e4SLinus Torvalds #else
129793e35f95STakashi Iwai static inline int snd_cs4281_create_gameport(struct cs4281 *chip) { return -ENOSYS; }
129893e35f95STakashi Iwai static inline void snd_cs4281_free_gameport(struct cs4281 *chip) { }
12991da177e4SLinus Torvalds #endif /* CONFIG_GAMEPORT || (MODULE && CONFIG_GAMEPORT_MODULE) */
13001da177e4SLinus Torvalds 
130193e35f95STakashi Iwai static int snd_cs4281_free(struct cs4281 *chip)
13021da177e4SLinus Torvalds {
13031da177e4SLinus Torvalds 	snd_cs4281_free_gameport(chip);
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 	if (chip->irq >= 0)
13061da177e4SLinus Torvalds 		synchronize_irq(chip->irq);
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	/* Mask interrupts */
13091da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HIMR, 0x7fffffff);
13101da177e4SLinus Torvalds 	/* Stop the DLL Clock logic. */
13111da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, 0);
13121da177e4SLinus Torvalds 	/* Sound System Power Management - Turn Everything OFF */
13131da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SSPM, 0);
13141da177e4SLinus Torvalds 	/* PCI interface - D3 state */
13151da177e4SLinus Torvalds 	pci_set_power_state(chip->pci, 3);
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	if (chip->irq >= 0)
131893e35f95STakashi Iwai 		free_irq(chip->irq, chip);
13191da177e4SLinus Torvalds 	if (chip->ba0)
13201da177e4SLinus Torvalds 		iounmap(chip->ba0);
13211da177e4SLinus Torvalds 	if (chip->ba1)
13221da177e4SLinus Torvalds 		iounmap(chip->ba1);
13231da177e4SLinus Torvalds 	pci_release_regions(chip->pci);
13241da177e4SLinus Torvalds 	pci_disable_device(chip->pci);
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	kfree(chip);
13271da177e4SLinus Torvalds 	return 0;
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds 
133093e35f95STakashi Iwai static int snd_cs4281_dev_free(struct snd_device *device)
13311da177e4SLinus Torvalds {
133293e35f95STakashi Iwai 	struct cs4281 *chip = device->device_data;
13331da177e4SLinus Torvalds 	return snd_cs4281_free(chip);
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds 
133693e35f95STakashi Iwai static int snd_cs4281_chip_init(struct cs4281 *chip); /* defined below */
13371da177e4SLinus Torvalds 
1338*e23e7a14SBill Pemberton static int snd_cs4281_create(struct snd_card *card,
13391da177e4SLinus Torvalds 			     struct pci_dev *pci,
134093e35f95STakashi Iwai 			     struct cs4281 **rchip,
13411da177e4SLinus Torvalds 			     int dual_codec)
13421da177e4SLinus Torvalds {
134393e35f95STakashi Iwai 	struct cs4281 *chip;
13441da177e4SLinus Torvalds 	unsigned int tmp;
13451da177e4SLinus Torvalds 	int err;
134693e35f95STakashi Iwai 	static struct snd_device_ops ops = {
13471da177e4SLinus Torvalds 		.dev_free =	snd_cs4281_dev_free,
13481da177e4SLinus Torvalds 	};
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 	*rchip = NULL;
13511da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
13521da177e4SLinus Torvalds 		return err;
1353e560d8d8STakashi Iwai 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
13541da177e4SLinus Torvalds 	if (chip == NULL) {
13551da177e4SLinus Torvalds 		pci_disable_device(pci);
13561da177e4SLinus Torvalds 		return -ENOMEM;
13571da177e4SLinus Torvalds 	}
13581da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
13591da177e4SLinus Torvalds 	chip->card = card;
13601da177e4SLinus Torvalds 	chip->pci = pci;
13611da177e4SLinus Torvalds 	chip->irq = -1;
13621da177e4SLinus Torvalds 	pci_set_master(pci);
13631da177e4SLinus Torvalds 	if (dual_codec < 0 || dual_codec > 3) {
13641da177e4SLinus Torvalds 		snd_printk(KERN_ERR "invalid dual_codec option %d\n", dual_codec);
13651da177e4SLinus Torvalds 		dual_codec = 0;
13661da177e4SLinus Torvalds 	}
13671da177e4SLinus Torvalds 	chip->dual_codec = dual_codec;
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, "CS4281")) < 0) {
13701da177e4SLinus Torvalds 		kfree(chip);
13711da177e4SLinus Torvalds 		pci_disable_device(pci);
13721da177e4SLinus Torvalds 		return err;
13731da177e4SLinus Torvalds 	}
13741da177e4SLinus Torvalds 	chip->ba0_addr = pci_resource_start(pci, 0);
13751da177e4SLinus Torvalds 	chip->ba1_addr = pci_resource_start(pci, 1);
13761da177e4SLinus Torvalds 
13772f5ad54eSArjan van de Ven 	chip->ba0 = pci_ioremap_bar(pci, 0);
13782f5ad54eSArjan van de Ven 	chip->ba1 = pci_ioremap_bar(pci, 1);
1379688956f2STakashi Iwai 	if (!chip->ba0 || !chip->ba1) {
1380688956f2STakashi Iwai 		snd_cs4281_free(chip);
1381688956f2STakashi Iwai 		return -ENOMEM;
1382688956f2STakashi Iwai 	}
1383688956f2STakashi Iwai 
1384437a5a46STakashi Iwai 	if (request_irq(pci->irq, snd_cs4281_interrupt, IRQF_SHARED,
1385934c2b6dSTakashi Iwai 			KBUILD_MODNAME, chip)) {
13861da177e4SLinus Torvalds 		snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
13871da177e4SLinus Torvalds 		snd_cs4281_free(chip);
13881da177e4SLinus Torvalds 		return -ENOMEM;
13891da177e4SLinus Torvalds 	}
13901da177e4SLinus Torvalds 	chip->irq = pci->irq;
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 	tmp = snd_cs4281_chip_init(chip);
13931da177e4SLinus Torvalds 	if (tmp) {
13941da177e4SLinus Torvalds 		snd_cs4281_free(chip);
13951da177e4SLinus Torvalds 		return tmp;
13961da177e4SLinus Torvalds 	}
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
13991da177e4SLinus Torvalds 		snd_cs4281_free(chip);
14001da177e4SLinus Torvalds 		return err;
14011da177e4SLinus Torvalds 	}
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	snd_cs4281_proc_init(chip);
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	snd_card_set_dev(card, &pci->dev);
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	*rchip = chip;
14081da177e4SLinus Torvalds 	return 0;
14091da177e4SLinus Torvalds }
14101da177e4SLinus Torvalds 
141193e35f95STakashi Iwai static int snd_cs4281_chip_init(struct cs4281 *chip)
14121da177e4SLinus Torvalds {
14131da177e4SLinus Torvalds 	unsigned int tmp;
141438223daaSTakashi Iwai 	unsigned long end_time;
14151da177e4SLinus Torvalds 	int retry_count = 2;
14161da177e4SLinus Torvalds 
1417a488e033SArnaud Patard 	/* Having EPPMC.FPDN=1 prevent proper chip initialisation */
1418a488e033SArnaud Patard 	tmp = snd_cs4281_peekBA0(chip, BA0_EPPMC);
1419a488e033SArnaud Patard 	if (tmp & BA0_EPPMC_FPDN)
1420a488e033SArnaud Patard 		snd_cs4281_pokeBA0(chip, BA0_EPPMC, tmp & ~BA0_EPPMC_FPDN);
1421a488e033SArnaud Patard 
14221da177e4SLinus Torvalds       __retry:
14231da177e4SLinus Torvalds 	tmp = snd_cs4281_peekBA0(chip, BA0_CFLR);
14241da177e4SLinus Torvalds 	if (tmp != BA0_CFLR_DEFAULT) {
14251da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_CFLR, BA0_CFLR_DEFAULT);
14261da177e4SLinus Torvalds 		tmp = snd_cs4281_peekBA0(chip, BA0_CFLR);
14271da177e4SLinus Torvalds 		if (tmp != BA0_CFLR_DEFAULT) {
14281da177e4SLinus Torvalds 			snd_printk(KERN_ERR "CFLR setup failed (0x%x)\n", tmp);
14291da177e4SLinus Torvalds 			return -EIO;
14301da177e4SLinus Torvalds 		}
14311da177e4SLinus Torvalds 	}
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	/* Set the 'Configuration Write Protect' register
14341da177e4SLinus Torvalds 	 * to 4281h.  Allows vendor-defined configuration
14351da177e4SLinus Torvalds          * space between 0e4h and 0ffh to be written. */
14361da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CWPR, 0x4281);
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	if ((tmp = snd_cs4281_peekBA0(chip, BA0_SERC1)) != (BA0_SERC1_SO1EN | BA0_SERC1_AC97)) {
14391da177e4SLinus Torvalds 		snd_printk(KERN_ERR "SERC1 AC'97 check failed (0x%x)\n", tmp);
14401da177e4SLinus Torvalds 		return -EIO;
14411da177e4SLinus Torvalds 	}
14421da177e4SLinus Torvalds 	if ((tmp = snd_cs4281_peekBA0(chip, BA0_SERC2)) != (BA0_SERC2_SI1EN | BA0_SERC2_AC97)) {
14431da177e4SLinus Torvalds 		snd_printk(KERN_ERR "SERC2 AC'97 check failed (0x%x)\n", tmp);
14441da177e4SLinus Torvalds 		return -EIO;
14451da177e4SLinus Torvalds 	}
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 	/* Sound System Power Management */
14481da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SSPM, BA0_SSPM_MIXEN | BA0_SSPM_CSRCEN |
14491da177e4SLinus Torvalds 				           BA0_SSPM_PSRCEN | BA0_SSPM_JSEN |
14501da177e4SLinus Torvalds 				           BA0_SSPM_ACLEN | BA0_SSPM_FMEN);
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds 	/* Serial Port Power Management */
14531da177e4SLinus Torvalds  	/* Blast the clock control register to zero so that the
14541da177e4SLinus Torvalds          * PLL starts out in a known state, and blast the master serial
14551da177e4SLinus Torvalds          * port control register to zero so that the serial ports also
14561da177e4SLinus Torvalds          * start out in a known state. */
14571da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, 0);
14581da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SERMC, 0);
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds         /* Make ESYN go to zero to turn off
14611da177e4SLinus Torvalds          * the Sync pulse on the AC97 link. */
14621da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, 0);
14631da177e4SLinus Torvalds 	udelay(50);
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	/*  Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
14661da177e4SLinus Torvalds 	 *  spec) and then drive it high.  This is done for non AC97 modes since
14671da177e4SLinus Torvalds 	 *  there might be logic external to the CS4281 that uses the ARST# line
14681da177e4SLinus Torvalds 	 *  for a reset. */
14691da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SPMC, 0);
14701da177e4SLinus Torvalds 	udelay(50);
14711da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SPMC, BA0_SPMC_RSTN);
1472c9a49bb1STakashi Iwai 	msleep(50);
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 	if (chip->dual_codec)
14751da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_SPMC, BA0_SPMC_RSTN | BA0_SPMC_ASDI2E);
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	/*
14781da177e4SLinus Torvalds 	 *  Set the serial port timing configuration.
14791da177e4SLinus Torvalds 	 */
14801da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SERMC,
14811da177e4SLinus Torvalds 			   (chip->dual_codec ? BA0_SERMC_TCID(chip->dual_codec) : BA0_SERMC_TCID(1)) |
14821da177e4SLinus Torvalds 			   BA0_SERMC_PTC_AC97 | BA0_SERMC_MSPE);
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 	/*
14851da177e4SLinus Torvalds 	 *  Start the DLL Clock logic.
14861da177e4SLinus Torvalds 	 */
14871da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, BA0_CLKCR1_DLLP);
1488c9a49bb1STakashi Iwai 	msleep(50);
14891da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, BA0_CLKCR1_SWCE | BA0_CLKCR1_DLLP);
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	/*
14921da177e4SLinus Torvalds 	 * Wait for the DLL ready signal from the clock logic.
14931da177e4SLinus Torvalds 	 */
149438223daaSTakashi Iwai 	end_time = jiffies + HZ;
14951da177e4SLinus Torvalds 	do {
14961da177e4SLinus Torvalds 		/*
14971da177e4SLinus Torvalds 		 *  Read the AC97 status register to see if we've seen a CODEC
14981da177e4SLinus Torvalds 		 *  signal from the AC97 codec.
14991da177e4SLinus Torvalds 		 */
15001da177e4SLinus Torvalds 		if (snd_cs4281_peekBA0(chip, BA0_CLKCR1) & BA0_CLKCR1_DLLRDY)
15011da177e4SLinus Torvalds 			goto __ok0;
150238223daaSTakashi Iwai 		schedule_timeout_uninterruptible(1);
150338223daaSTakashi Iwai 	} while (time_after_eq(end_time, jiffies));
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 	snd_printk(KERN_ERR "DLLRDY not seen\n");
15061da177e4SLinus Torvalds 	return -EIO;
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds       __ok0:
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	/*
15111da177e4SLinus Torvalds 	 *  The first thing we do here is to enable sync generation.  As soon
15121da177e4SLinus Torvalds 	 *  as we start receiving bit clock, we'll start producing the SYNC
15131da177e4SLinus Torvalds 	 *  signal.
15141da177e4SLinus Torvalds 	 */
15151da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, BA0_ACCTL_ESYN);
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	/*
15181da177e4SLinus Torvalds 	 * Wait for the codec ready signal from the AC97 codec.
15191da177e4SLinus Torvalds 	 */
152038223daaSTakashi Iwai 	end_time = jiffies + HZ;
15211da177e4SLinus Torvalds 	do {
15221da177e4SLinus Torvalds 		/*
15231da177e4SLinus Torvalds 		 *  Read the AC97 status register to see if we've seen a CODEC
15241da177e4SLinus Torvalds 		 *  signal from the AC97 codec.
15251da177e4SLinus Torvalds 		 */
15261da177e4SLinus Torvalds 		if (snd_cs4281_peekBA0(chip, BA0_ACSTS) & BA0_ACSTS_CRDY)
15271da177e4SLinus Torvalds 			goto __ok1;
152838223daaSTakashi Iwai 		schedule_timeout_uninterruptible(1);
152938223daaSTakashi Iwai 	} while (time_after_eq(end_time, jiffies));
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	snd_printk(KERN_ERR "never read codec ready from AC'97 (0x%x)\n", snd_cs4281_peekBA0(chip, BA0_ACSTS));
15321da177e4SLinus Torvalds 	return -EIO;
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds       __ok1:
15351da177e4SLinus Torvalds 	if (chip->dual_codec) {
153638223daaSTakashi Iwai 		end_time = jiffies + HZ;
15371da177e4SLinus Torvalds 		do {
15381da177e4SLinus Torvalds 			if (snd_cs4281_peekBA0(chip, BA0_ACSTS2) & BA0_ACSTS_CRDY)
15391da177e4SLinus Torvalds 				goto __codec2_ok;
154038223daaSTakashi Iwai 			schedule_timeout_uninterruptible(1);
154138223daaSTakashi Iwai 		} while (time_after_eq(end_time, jiffies));
15421da177e4SLinus Torvalds 		snd_printk(KERN_INFO "secondary codec doesn't respond. disable it...\n");
15431da177e4SLinus Torvalds 		chip->dual_codec = 0;
15441da177e4SLinus Torvalds 	__codec2_ok: ;
15451da177e4SLinus Torvalds 	}
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	/*
15481da177e4SLinus Torvalds 	 *  Assert the valid frame signal so that we can start sending commands
15491da177e4SLinus Torvalds 	 *  to the AC97 codec.
15501da177e4SLinus Torvalds 	 */
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACCTL, BA0_ACCTL_VFRM | BA0_ACCTL_ESYN);
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds 	/*
15551da177e4SLinus Torvalds 	 *  Wait until we've sampled input slots 3 and 4 as valid, meaning that
15561da177e4SLinus Torvalds 	 *  the codec is pumping ADC data across the AC-link.
15571da177e4SLinus Torvalds 	 */
15581da177e4SLinus Torvalds 
155938223daaSTakashi Iwai 	end_time = jiffies + HZ;
15601da177e4SLinus Torvalds 	do {
15611da177e4SLinus Torvalds 		/*
15621da177e4SLinus Torvalds 		 *  Read the input slot valid register and see if input slots 3
15631da177e4SLinus Torvalds 		 *  4 are valid yet.
15641da177e4SLinus Torvalds 		 */
15651da177e4SLinus Torvalds                 if ((snd_cs4281_peekBA0(chip, BA0_ACISV) & (BA0_ACISV_SLV(3) | BA0_ACISV_SLV(4))) == (BA0_ACISV_SLV(3) | BA0_ACISV_SLV(4)))
15661da177e4SLinus Torvalds                         goto __ok2;
156738223daaSTakashi Iwai 		schedule_timeout_uninterruptible(1);
156838223daaSTakashi Iwai 	} while (time_after_eq(end_time, jiffies));
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	if (--retry_count > 0)
15711da177e4SLinus Torvalds 		goto __retry;
15721da177e4SLinus Torvalds 	snd_printk(KERN_ERR "never read ISV3 and ISV4 from AC'97\n");
15731da177e4SLinus Torvalds 	return -EIO;
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds       __ok2:
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	/*
15781da177e4SLinus Torvalds 	 *  Now, assert valid frame and the slot 3 and 4 valid bits.  This will
15791da177e4SLinus Torvalds 	 *  commense the transfer of digital audio data to the AC97 codec.
15801da177e4SLinus Torvalds 	 */
15811da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_ACOSV, BA0_ACOSV_SLV(3) | BA0_ACOSV_SLV(4));
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	/*
15841da177e4SLinus Torvalds 	 *  Initialize DMA structures
15851da177e4SLinus Torvalds 	 */
15861da177e4SLinus Torvalds 	for (tmp = 0; tmp < 4; tmp++) {
158793e35f95STakashi Iwai 		struct cs4281_dma *dma = &chip->dma[tmp];
15881da177e4SLinus Torvalds 		dma->regDBA = BA0_DBA0 + (tmp * 0x10);
15891da177e4SLinus Torvalds 		dma->regDCA = BA0_DCA0 + (tmp * 0x10);
15901da177e4SLinus Torvalds 		dma->regDBC = BA0_DBC0 + (tmp * 0x10);
15911da177e4SLinus Torvalds 		dma->regDCC = BA0_DCC0 + (tmp * 0x10);
15921da177e4SLinus Torvalds 		dma->regDMR = BA0_DMR0 + (tmp * 8);
15931da177e4SLinus Torvalds 		dma->regDCR = BA0_DCR0 + (tmp * 8);
15941da177e4SLinus Torvalds 		dma->regHDSR = BA0_HDSR0 + (tmp * 4);
15951da177e4SLinus Torvalds 		dma->regFCR = BA0_FCR0 + (tmp * 4);
15961da177e4SLinus Torvalds 		dma->regFSIC = BA0_FSIC0 + (tmp * 4);
15971da177e4SLinus Torvalds 		dma->fifo_offset = tmp * CS4281_FIFO_SIZE;
15981da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, dma->regFCR,
15991da177e4SLinus Torvalds 				   BA0_FCR_LS(31) |
16001da177e4SLinus Torvalds 				   BA0_FCR_RS(31) |
16011da177e4SLinus Torvalds 				   BA0_FCR_SZ(CS4281_FIFO_SIZE) |
16021da177e4SLinus Torvalds 				   BA0_FCR_OF(dma->fifo_offset));
16031da177e4SLinus Torvalds 	}
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	chip->src_left_play_slot = 0;	/* AC'97 left PCM playback (3) */
16061da177e4SLinus Torvalds 	chip->src_right_play_slot = 1;	/* AC'97 right PCM playback (4) */
16071da177e4SLinus Torvalds 	chip->src_left_rec_slot = 10;	/* AC'97 left PCM record (3) */
16081da177e4SLinus Torvalds 	chip->src_right_rec_slot = 11;	/* AC'97 right PCM record (4) */
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	/* Activate wave playback FIFO for FM playback */
16111da177e4SLinus Torvalds 	chip->dma[0].valFCR = BA0_FCR_FEN | BA0_FCR_LS(0) |
16121da177e4SLinus Torvalds 		              BA0_FCR_RS(1) |
16131da177e4SLinus Torvalds  	  	              BA0_FCR_SZ(CS4281_FIFO_SIZE) |
16141da177e4SLinus Torvalds 		              BA0_FCR_OF(chip->dma[0].fifo_offset);
16151da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, chip->dma[0].regFCR, chip->dma[0].valFCR);
16161da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SRCSA, (chip->src_left_play_slot << 0) |
16171da177e4SLinus Torvalds 					    (chip->src_right_play_slot << 8) |
16181da177e4SLinus Torvalds 					    (chip->src_left_rec_slot << 16) |
16191da177e4SLinus Torvalds 					    (chip->src_right_rec_slot << 24));
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds 	/* Initialize digital volume */
16221da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_PPLVC, 0);
16231da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_PPRVC, 0);
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds 	/* Enable IRQs */
16261da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HICR, BA0_HICR_EOI);
16271da177e4SLinus Torvalds 	/* Unmask interrupts */
16281da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HIMR, 0x7fffffff & ~(
16291da177e4SLinus Torvalds 					BA0_HISR_MIDI |
16301da177e4SLinus Torvalds 					BA0_HISR_DMAI |
16311da177e4SLinus Torvalds 					BA0_HISR_DMA(0) |
16321da177e4SLinus Torvalds 					BA0_HISR_DMA(1) |
16331da177e4SLinus Torvalds 					BA0_HISR_DMA(2) |
16341da177e4SLinus Torvalds 					BA0_HISR_DMA(3)));
16351da177e4SLinus Torvalds 	synchronize_irq(chip->irq);
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds 	return 0;
16381da177e4SLinus Torvalds }
16391da177e4SLinus Torvalds 
16401da177e4SLinus Torvalds /*
16411da177e4SLinus Torvalds  *  MIDI section
16421da177e4SLinus Torvalds  */
16431da177e4SLinus Torvalds 
164493e35f95STakashi Iwai static void snd_cs4281_midi_reset(struct cs4281 *chip)
16451da177e4SLinus Torvalds {
16461da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr | BA0_MIDCR_MRST);
16471da177e4SLinus Torvalds 	udelay(100);
16481da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16491da177e4SLinus Torvalds }
16501da177e4SLinus Torvalds 
165193e35f95STakashi Iwai static int snd_cs4281_midi_input_open(struct snd_rawmidi_substream *substream)
16521da177e4SLinus Torvalds {
165393e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
16561da177e4SLinus Torvalds  	chip->midcr |= BA0_MIDCR_RXE;
16571da177e4SLinus Torvalds 	chip->midi_input = substream;
16581da177e4SLinus Torvalds 	if (!(chip->uartm & CS4281_MODE_OUTPUT)) {
16591da177e4SLinus Torvalds 		snd_cs4281_midi_reset(chip);
16601da177e4SLinus Torvalds 	} else {
16611da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16621da177e4SLinus Torvalds 	}
16631da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
16641da177e4SLinus Torvalds 	return 0;
16651da177e4SLinus Torvalds }
16661da177e4SLinus Torvalds 
166793e35f95STakashi Iwai static int snd_cs4281_midi_input_close(struct snd_rawmidi_substream *substream)
16681da177e4SLinus Torvalds {
166993e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
16721da177e4SLinus Torvalds 	chip->midcr &= ~(BA0_MIDCR_RXE | BA0_MIDCR_RIE);
16731da177e4SLinus Torvalds 	chip->midi_input = NULL;
16741da177e4SLinus Torvalds 	if (!(chip->uartm & CS4281_MODE_OUTPUT)) {
16751da177e4SLinus Torvalds 		snd_cs4281_midi_reset(chip);
16761da177e4SLinus Torvalds 	} else {
16771da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16781da177e4SLinus Torvalds 	}
16791da177e4SLinus Torvalds 	chip->uartm &= ~CS4281_MODE_INPUT;
16801da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
16811da177e4SLinus Torvalds 	return 0;
16821da177e4SLinus Torvalds }
16831da177e4SLinus Torvalds 
168493e35f95STakashi Iwai static int snd_cs4281_midi_output_open(struct snd_rawmidi_substream *substream)
16851da177e4SLinus Torvalds {
168693e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
16891da177e4SLinus Torvalds 	chip->uartm |= CS4281_MODE_OUTPUT;
16901da177e4SLinus Torvalds 	chip->midcr |= BA0_MIDCR_TXE;
16911da177e4SLinus Torvalds 	chip->midi_output = substream;
16921da177e4SLinus Torvalds 	if (!(chip->uartm & CS4281_MODE_INPUT)) {
16931da177e4SLinus Torvalds 		snd_cs4281_midi_reset(chip);
16941da177e4SLinus Torvalds 	} else {
16951da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
16961da177e4SLinus Torvalds 	}
16971da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
16981da177e4SLinus Torvalds 	return 0;
16991da177e4SLinus Torvalds }
17001da177e4SLinus Torvalds 
170193e35f95STakashi Iwai static int snd_cs4281_midi_output_close(struct snd_rawmidi_substream *substream)
17021da177e4SLinus Torvalds {
170393e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
17061da177e4SLinus Torvalds 	chip->midcr &= ~(BA0_MIDCR_TXE | BA0_MIDCR_TIE);
17071da177e4SLinus Torvalds 	chip->midi_output = NULL;
17081da177e4SLinus Torvalds 	if (!(chip->uartm & CS4281_MODE_INPUT)) {
17091da177e4SLinus Torvalds 		snd_cs4281_midi_reset(chip);
17101da177e4SLinus Torvalds 	} else {
17111da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
17121da177e4SLinus Torvalds 	}
17131da177e4SLinus Torvalds 	chip->uartm &= ~CS4281_MODE_OUTPUT;
17141da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
17151da177e4SLinus Torvalds 	return 0;
17161da177e4SLinus Torvalds }
17171da177e4SLinus Torvalds 
171893e35f95STakashi Iwai static void snd_cs4281_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
17191da177e4SLinus Torvalds {
17201da177e4SLinus Torvalds 	unsigned long flags;
172193e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
17241da177e4SLinus Torvalds 	if (up) {
17251da177e4SLinus Torvalds 		if ((chip->midcr & BA0_MIDCR_RIE) == 0) {
17261da177e4SLinus Torvalds 			chip->midcr |= BA0_MIDCR_RIE;
17271da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
17281da177e4SLinus Torvalds 		}
17291da177e4SLinus Torvalds 	} else {
17301da177e4SLinus Torvalds 		if (chip->midcr & BA0_MIDCR_RIE) {
17311da177e4SLinus Torvalds 			chip->midcr &= ~BA0_MIDCR_RIE;
17321da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
17331da177e4SLinus Torvalds 		}
17341da177e4SLinus Torvalds 	}
17351da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
17361da177e4SLinus Torvalds }
17371da177e4SLinus Torvalds 
173893e35f95STakashi Iwai static void snd_cs4281_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
17391da177e4SLinus Torvalds {
17401da177e4SLinus Torvalds 	unsigned long flags;
174193e35f95STakashi Iwai 	struct cs4281 *chip = substream->rmidi->private_data;
17421da177e4SLinus Torvalds 	unsigned char byte;
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
17451da177e4SLinus Torvalds 	if (up) {
17461da177e4SLinus Torvalds 		if ((chip->midcr & BA0_MIDCR_TIE) == 0) {
17471da177e4SLinus Torvalds 			chip->midcr |= BA0_MIDCR_TIE;
17481da177e4SLinus Torvalds 			/* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
17491da177e4SLinus Torvalds 			while ((chip->midcr & BA0_MIDCR_TIE) &&
17501da177e4SLinus Torvalds 			       (snd_cs4281_peekBA0(chip, BA0_MIDSR) & BA0_MIDSR_TBF) == 0) {
17511da177e4SLinus Torvalds 				if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
17521da177e4SLinus Torvalds 					chip->midcr &= ~BA0_MIDCR_TIE;
17531da177e4SLinus Torvalds 				} else {
17541da177e4SLinus Torvalds 					snd_cs4281_pokeBA0(chip, BA0_MIDWP, byte);
17551da177e4SLinus Torvalds 				}
17561da177e4SLinus Torvalds 			}
17571da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
17581da177e4SLinus Torvalds 		}
17591da177e4SLinus Torvalds 	} else {
17601da177e4SLinus Torvalds 		if (chip->midcr & BA0_MIDCR_TIE) {
17611da177e4SLinus Torvalds 			chip->midcr &= ~BA0_MIDCR_TIE;
17621da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
17631da177e4SLinus Torvalds 		}
17641da177e4SLinus Torvalds 	}
17651da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds 
176893e35f95STakashi Iwai static struct snd_rawmidi_ops snd_cs4281_midi_output =
17691da177e4SLinus Torvalds {
17701da177e4SLinus Torvalds 	.open =		snd_cs4281_midi_output_open,
17711da177e4SLinus Torvalds 	.close =	snd_cs4281_midi_output_close,
17721da177e4SLinus Torvalds 	.trigger =	snd_cs4281_midi_output_trigger,
17731da177e4SLinus Torvalds };
17741da177e4SLinus Torvalds 
177593e35f95STakashi Iwai static struct snd_rawmidi_ops snd_cs4281_midi_input =
17761da177e4SLinus Torvalds {
17771da177e4SLinus Torvalds 	.open = 	snd_cs4281_midi_input_open,
17781da177e4SLinus Torvalds 	.close =	snd_cs4281_midi_input_close,
17791da177e4SLinus Torvalds 	.trigger =	snd_cs4281_midi_input_trigger,
17801da177e4SLinus Torvalds };
17811da177e4SLinus Torvalds 
1782*e23e7a14SBill Pemberton static int snd_cs4281_midi(struct cs4281 *chip, int device,
178393e35f95STakashi Iwai 			   struct snd_rawmidi **rrawmidi)
17841da177e4SLinus Torvalds {
178593e35f95STakashi Iwai 	struct snd_rawmidi *rmidi;
17861da177e4SLinus Torvalds 	int err;
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 	if (rrawmidi)
17891da177e4SLinus Torvalds 		*rrawmidi = NULL;
17901da177e4SLinus Torvalds 	if ((err = snd_rawmidi_new(chip->card, "CS4281", device, 1, 1, &rmidi)) < 0)
17911da177e4SLinus Torvalds 		return err;
17921da177e4SLinus Torvalds 	strcpy(rmidi->name, "CS4281");
17931da177e4SLinus Torvalds 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_cs4281_midi_output);
17941da177e4SLinus Torvalds 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_cs4281_midi_input);
17951da177e4SLinus Torvalds 	rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
17961da177e4SLinus Torvalds 	rmidi->private_data = chip;
17971da177e4SLinus Torvalds 	chip->rmidi = rmidi;
17981da177e4SLinus Torvalds 	if (rrawmidi)
17991da177e4SLinus Torvalds 		*rrawmidi = rmidi;
18001da177e4SLinus Torvalds 	return 0;
18011da177e4SLinus Torvalds }
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds /*
18041da177e4SLinus Torvalds  *  Interrupt handler
18051da177e4SLinus Torvalds  */
18061da177e4SLinus Torvalds 
18077d12e780SDavid Howells static irqreturn_t snd_cs4281_interrupt(int irq, void *dev_id)
18081da177e4SLinus Torvalds {
180993e35f95STakashi Iwai 	struct cs4281 *chip = dev_id;
18101da177e4SLinus Torvalds 	unsigned int status, dma, val;
181193e35f95STakashi Iwai 	struct cs4281_dma *cdma;
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds 	if (chip == NULL)
18141da177e4SLinus Torvalds 		return IRQ_NONE;
18151da177e4SLinus Torvalds 	status = snd_cs4281_peekBA0(chip, BA0_HISR);
18161da177e4SLinus Torvalds 	if ((status & 0x7fffffff) == 0) {
18171da177e4SLinus Torvalds 		snd_cs4281_pokeBA0(chip, BA0_HICR, BA0_HICR_EOI);
18181da177e4SLinus Torvalds 		return IRQ_NONE;
18191da177e4SLinus Torvalds 	}
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 	if (status & (BA0_HISR_DMA(0)|BA0_HISR_DMA(1)|BA0_HISR_DMA(2)|BA0_HISR_DMA(3))) {
18221da177e4SLinus Torvalds 		for (dma = 0; dma < 4; dma++)
18231da177e4SLinus Torvalds 			if (status & BA0_HISR_DMA(dma)) {
18241da177e4SLinus Torvalds 				cdma = &chip->dma[dma];
18251da177e4SLinus Torvalds 				spin_lock(&chip->reg_lock);
18261da177e4SLinus Torvalds 				/* ack DMA IRQ */
18271da177e4SLinus Torvalds 				val = snd_cs4281_peekBA0(chip, cdma->regHDSR);
18281da177e4SLinus Torvalds 				/* workaround, sometimes CS4281 acknowledges */
18291da177e4SLinus Torvalds 				/* end or middle transfer position twice */
18301da177e4SLinus Torvalds 				cdma->frag++;
18311da177e4SLinus Torvalds 				if ((val & BA0_HDSR_DHTC) && !(cdma->frag & 1)) {
18321da177e4SLinus Torvalds 					cdma->frag--;
18331da177e4SLinus Torvalds 					chip->spurious_dhtc_irq++;
18341da177e4SLinus Torvalds 					spin_unlock(&chip->reg_lock);
18351da177e4SLinus Torvalds 					continue;
18361da177e4SLinus Torvalds 				}
18371da177e4SLinus Torvalds 				if ((val & BA0_HDSR_DTC) && (cdma->frag & 1)) {
18381da177e4SLinus Torvalds 					cdma->frag--;
18391da177e4SLinus Torvalds 					chip->spurious_dtc_irq++;
18401da177e4SLinus Torvalds 					spin_unlock(&chip->reg_lock);
18411da177e4SLinus Torvalds 					continue;
18421da177e4SLinus Torvalds 				}
18431da177e4SLinus Torvalds 				spin_unlock(&chip->reg_lock);
18441da177e4SLinus Torvalds 				snd_pcm_period_elapsed(cdma->substream);
18451da177e4SLinus Torvalds 			}
18461da177e4SLinus Torvalds 	}
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds 	if ((status & BA0_HISR_MIDI) && chip->rmidi) {
18491da177e4SLinus Torvalds 		unsigned char c;
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
18521da177e4SLinus Torvalds 		while ((snd_cs4281_peekBA0(chip, BA0_MIDSR) & BA0_MIDSR_RBE) == 0) {
18531da177e4SLinus Torvalds 			c = snd_cs4281_peekBA0(chip, BA0_MIDRP);
18541da177e4SLinus Torvalds 			if ((chip->midcr & BA0_MIDCR_RIE) == 0)
18551da177e4SLinus Torvalds 				continue;
18561da177e4SLinus Torvalds 			snd_rawmidi_receive(chip->midi_input, &c, 1);
18571da177e4SLinus Torvalds 		}
18581da177e4SLinus Torvalds 		while ((snd_cs4281_peekBA0(chip, BA0_MIDSR) & BA0_MIDSR_TBF) == 0) {
18591da177e4SLinus Torvalds 			if ((chip->midcr & BA0_MIDCR_TIE) == 0)
18601da177e4SLinus Torvalds 				break;
18611da177e4SLinus Torvalds 			if (snd_rawmidi_transmit(chip->midi_output, &c, 1) != 1) {
18621da177e4SLinus Torvalds 				chip->midcr &= ~BA0_MIDCR_TIE;
18631da177e4SLinus Torvalds 				snd_cs4281_pokeBA0(chip, BA0_MIDCR, chip->midcr);
18641da177e4SLinus Torvalds 				break;
18651da177e4SLinus Torvalds 			}
18661da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, BA0_MIDWP, c);
18671da177e4SLinus Torvalds 		}
18681da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
18691da177e4SLinus Torvalds 	}
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 	/* EOI to the PCI part... reenables interrupts */
18721da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HICR, BA0_HICR_EOI);
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 	return IRQ_HANDLED;
18751da177e4SLinus Torvalds }
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds /*
18791da177e4SLinus Torvalds  * OPL3 command
18801da177e4SLinus Torvalds  */
188193e35f95STakashi Iwai static void snd_cs4281_opl3_command(struct snd_opl3 *opl3, unsigned short cmd,
188293e35f95STakashi Iwai 				    unsigned char val)
18831da177e4SLinus Torvalds {
18841da177e4SLinus Torvalds 	unsigned long flags;
188593e35f95STakashi Iwai 	struct cs4281 *chip = opl3->private_data;
18861da177e4SLinus Torvalds 	void __iomem *port;
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds 	if (cmd & OPL3_RIGHT)
18891da177e4SLinus Torvalds 		port = chip->ba0 + BA0_B1AP; /* right port */
18901da177e4SLinus Torvalds 	else
18911da177e4SLinus Torvalds 		port = chip->ba0 + BA0_B0AP; /* left port */
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 	spin_lock_irqsave(&opl3->reg_lock, flags);
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	writel((unsigned int)cmd, port);
18961da177e4SLinus Torvalds 	udelay(10);
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds 	writel((unsigned int)val, port + 4);
18991da177e4SLinus Torvalds 	udelay(30);
19001da177e4SLinus Torvalds 
19011da177e4SLinus Torvalds 	spin_unlock_irqrestore(&opl3->reg_lock, flags);
19021da177e4SLinus Torvalds }
19031da177e4SLinus Torvalds 
1904*e23e7a14SBill Pemberton static int snd_cs4281_probe(struct pci_dev *pci,
19051da177e4SLinus Torvalds 			    const struct pci_device_id *pci_id)
19061da177e4SLinus Torvalds {
19071da177e4SLinus Torvalds 	static int dev;
190893e35f95STakashi Iwai 	struct snd_card *card;
190993e35f95STakashi Iwai 	struct cs4281 *chip;
191093e35f95STakashi Iwai 	struct snd_opl3 *opl3;
19111da177e4SLinus Torvalds 	int err;
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds         if (dev >= SNDRV_CARDS)
19141da177e4SLinus Torvalds                 return -ENODEV;
19151da177e4SLinus Torvalds 	if (!enable[dev]) {
19161da177e4SLinus Torvalds 		dev++;
19171da177e4SLinus Torvalds 		return -ENOENT;
19181da177e4SLinus Torvalds 	}
19191da177e4SLinus Torvalds 
1920e58de7baSTakashi Iwai 	err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
1921e58de7baSTakashi Iwai 	if (err < 0)
1922e58de7baSTakashi Iwai 		return err;
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds 	if ((err = snd_cs4281_create(card, pci, &chip, dual_codec[dev])) < 0) {
19251da177e4SLinus Torvalds 		snd_card_free(card);
19261da177e4SLinus Torvalds 		return err;
19271da177e4SLinus Torvalds 	}
192838c0a158STakashi Iwai 	card->private_data = chip;
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 	if ((err = snd_cs4281_mixer(chip)) < 0) {
19311da177e4SLinus Torvalds 		snd_card_free(card);
19321da177e4SLinus Torvalds 		return err;
19331da177e4SLinus Torvalds 	}
19341da177e4SLinus Torvalds 	if ((err = snd_cs4281_pcm(chip, 0, NULL)) < 0) {
19351da177e4SLinus Torvalds 		snd_card_free(card);
19361da177e4SLinus Torvalds 		return err;
19371da177e4SLinus Torvalds 	}
19381da177e4SLinus Torvalds 	if ((err = snd_cs4281_midi(chip, 0, NULL)) < 0) {
19391da177e4SLinus Torvalds 		snd_card_free(card);
19401da177e4SLinus Torvalds 		return err;
19411da177e4SLinus Torvalds 	}
19421da177e4SLinus Torvalds 	if ((err = snd_opl3_new(card, OPL3_HW_OPL3_CS4281, &opl3)) < 0) {
19431da177e4SLinus Torvalds 		snd_card_free(card);
19441da177e4SLinus Torvalds 		return err;
19451da177e4SLinus Torvalds 	}
19461da177e4SLinus Torvalds 	opl3->private_data = chip;
19471da177e4SLinus Torvalds 	opl3->command = snd_cs4281_opl3_command;
19481da177e4SLinus Torvalds 	snd_opl3_init(opl3);
19491da177e4SLinus Torvalds 	if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
19501da177e4SLinus Torvalds 		snd_card_free(card);
19511da177e4SLinus Torvalds 		return err;
19521da177e4SLinus Torvalds 	}
19531da177e4SLinus Torvalds 	snd_cs4281_create_gameport(chip);
19541da177e4SLinus Torvalds 	strcpy(card->driver, "CS4281");
19551da177e4SLinus Torvalds 	strcpy(card->shortname, "Cirrus Logic CS4281");
19561da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %d",
19571da177e4SLinus Torvalds 		card->shortname,
19581da177e4SLinus Torvalds 		chip->ba0_addr,
19591da177e4SLinus Torvalds 		chip->irq);
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
19621da177e4SLinus Torvalds 		snd_card_free(card);
19631da177e4SLinus Torvalds 		return err;
19641da177e4SLinus Torvalds 	}
19651da177e4SLinus Torvalds 
19661da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
19671da177e4SLinus Torvalds 	dev++;
19681da177e4SLinus Torvalds 	return 0;
19691da177e4SLinus Torvalds }
19701da177e4SLinus Torvalds 
1971*e23e7a14SBill Pemberton static void snd_cs4281_remove(struct pci_dev *pci)
19721da177e4SLinus Torvalds {
19731da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
19741da177e4SLinus Torvalds 	pci_set_drvdata(pci, NULL);
19751da177e4SLinus Torvalds }
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds /*
19781da177e4SLinus Torvalds  * Power Management
19791da177e4SLinus Torvalds  */
1980c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds static int saved_regs[SUSPEND_REGISTERS] = {
19831da177e4SLinus Torvalds 	BA0_JSCTL,
19841da177e4SLinus Torvalds 	BA0_GPIOR,
19851da177e4SLinus Torvalds 	BA0_SSCR,
19861da177e4SLinus Torvalds 	BA0_MIDCR,
19871da177e4SLinus Torvalds 	BA0_SRCSA,
19881da177e4SLinus Torvalds 	BA0_PASR,
19891da177e4SLinus Torvalds 	BA0_CASR,
19901da177e4SLinus Torvalds 	BA0_DACSR,
19911da177e4SLinus Torvalds 	BA0_ADCSR,
19921da177e4SLinus Torvalds 	BA0_FMLVC,
19931da177e4SLinus Torvalds 	BA0_FMRVC,
19941da177e4SLinus Torvalds 	BA0_PPLVC,
19951da177e4SLinus Torvalds 	BA0_PPRVC,
19961da177e4SLinus Torvalds };
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds #define CLKCR1_CKRA                             0x00010000L
19991da177e4SLinus Torvalds 
200068cb2b55STakashi Iwai static int cs4281_suspend(struct device *dev)
20011da177e4SLinus Torvalds {
200268cb2b55STakashi Iwai 	struct pci_dev *pci = to_pci_dev(dev);
200368cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
200438c0a158STakashi Iwai 	struct cs4281 *chip = card->private_data;
20051da177e4SLinus Torvalds 	u32 ulCLK;
20061da177e4SLinus Torvalds 	unsigned int i;
20071da177e4SLinus Torvalds 
200838c0a158STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
20091da177e4SLinus Torvalds 	snd_pcm_suspend_all(chip->pcm);
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	snd_ac97_suspend(chip->ac97);
20121da177e4SLinus Torvalds 	snd_ac97_suspend(chip->ac97_secondary);
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds 	ulCLK = snd_cs4281_peekBA0(chip, BA0_CLKCR1);
20151da177e4SLinus Torvalds 	ulCLK |= CLKCR1_CKRA;
20161da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, ulCLK);
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds 	/* Disable interrupts. */
20191da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_HICR, BA0_HICR_CHGM);
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	/* remember the status registers */
20221da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
20231da177e4SLinus Torvalds 		if (saved_regs[i])
20241da177e4SLinus Torvalds 			chip->suspend_regs[i] = snd_cs4281_peekBA0(chip, saved_regs[i]);
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds 	/* Turn off the serial ports. */
20271da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SERMC, 0);
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds 	/* Power off FM, Joystick, AC link, */
20301da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SSPM, 0);
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds 	/* DLL off. */
20331da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, 0);
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds 	/* AC link off. */
20361da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_SPMC, 0);
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 	ulCLK = snd_cs4281_peekBA0(chip, BA0_CLKCR1);
20391da177e4SLinus Torvalds 	ulCLK &= ~CLKCR1_CKRA;
20401da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, ulCLK);
20411da177e4SLinus Torvalds 
204238c0a158STakashi Iwai 	pci_disable_device(pci);
204338c0a158STakashi Iwai 	pci_save_state(pci);
204468cb2b55STakashi Iwai 	pci_set_power_state(pci, PCI_D3hot);
20451da177e4SLinus Torvalds 	return 0;
20461da177e4SLinus Torvalds }
20471da177e4SLinus Torvalds 
204868cb2b55STakashi Iwai static int cs4281_resume(struct device *dev)
20491da177e4SLinus Torvalds {
205068cb2b55STakashi Iwai 	struct pci_dev *pci = to_pci_dev(dev);
205168cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
205238c0a158STakashi Iwai 	struct cs4281 *chip = card->private_data;
20531da177e4SLinus Torvalds 	unsigned int i;
20541da177e4SLinus Torvalds 	u32 ulCLK;
20551da177e4SLinus Torvalds 
205630b35399STakashi Iwai 	pci_set_power_state(pci, PCI_D0);
205738c0a158STakashi Iwai 	pci_restore_state(pci);
205830b35399STakashi Iwai 	if (pci_enable_device(pci) < 0) {
205930b35399STakashi Iwai 		printk(KERN_ERR "cs4281: pci_enable_device failed, "
206030b35399STakashi Iwai 		       "disabling device\n");
206130b35399STakashi Iwai 		snd_card_disconnect(card);
206230b35399STakashi Iwai 		return -EIO;
206330b35399STakashi Iwai 	}
206438c0a158STakashi Iwai 	pci_set_master(pci);
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	ulCLK = snd_cs4281_peekBA0(chip, BA0_CLKCR1);
20671da177e4SLinus Torvalds 	ulCLK |= CLKCR1_CKRA;
20681da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, ulCLK);
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds 	snd_cs4281_chip_init(chip);
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 	/* restore the status registers */
20731da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
20741da177e4SLinus Torvalds 		if (saved_regs[i])
20751da177e4SLinus Torvalds 			snd_cs4281_pokeBA0(chip, saved_regs[i], chip->suspend_regs[i]);
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 	snd_ac97_resume(chip->ac97);
20781da177e4SLinus Torvalds 	snd_ac97_resume(chip->ac97_secondary);
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 	ulCLK = snd_cs4281_peekBA0(chip, BA0_CLKCR1);
20811da177e4SLinus Torvalds 	ulCLK &= ~CLKCR1_CKRA;
20821da177e4SLinus Torvalds 	snd_cs4281_pokeBA0(chip, BA0_CLKCR1, ulCLK);
20831da177e4SLinus Torvalds 
208438c0a158STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
20851da177e4SLinus Torvalds 	return 0;
20861da177e4SLinus Torvalds }
208768cb2b55STakashi Iwai 
208868cb2b55STakashi Iwai static SIMPLE_DEV_PM_OPS(cs4281_pm, cs4281_suspend, cs4281_resume);
208968cb2b55STakashi Iwai #define CS4281_PM_OPS	&cs4281_pm
209068cb2b55STakashi Iwai #else
209168cb2b55STakashi Iwai #define CS4281_PM_OPS	NULL
2092c7561cd8STakashi Iwai #endif /* CONFIG_PM_SLEEP */
20931da177e4SLinus Torvalds 
2094e9f66d9bSTakashi Iwai static struct pci_driver cs4281_driver = {
20953733e424STakashi Iwai 	.name = KBUILD_MODNAME,
20961da177e4SLinus Torvalds 	.id_table = snd_cs4281_ids,
20971da177e4SLinus Torvalds 	.probe = snd_cs4281_probe,
2098*e23e7a14SBill Pemberton 	.remove = snd_cs4281_remove,
209968cb2b55STakashi Iwai 	.driver = {
210068cb2b55STakashi Iwai 		.pm = CS4281_PM_OPS,
210168cb2b55STakashi Iwai 	},
21021da177e4SLinus Torvalds };
21031da177e4SLinus Torvalds 
2104e9f66d9bSTakashi Iwai module_pci_driver(cs4281_driver);
2105