xref: /linux/sound/pci/cmipci.c (revision feb77712b0e780f04507cdda0529088ff0f3286f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Driver for C-Media CMI8338 and 8738 PCI soundcards.
31da177e4SLinus Torvalds  * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
61da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
71da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
81da177e4SLinus Torvalds  *   (at your option) any later version.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
131da177e4SLinus Torvalds  *   GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
161da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
171da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds /* Does not work. Warning may block system in capture mode */
211da177e4SLinus Torvalds /* #define USE_VAR48KRATE */
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <sound/driver.h>
241da177e4SLinus Torvalds #include <asm/io.h>
251da177e4SLinus Torvalds #include <linux/delay.h>
261da177e4SLinus Torvalds #include <linux/interrupt.h>
271da177e4SLinus Torvalds #include <linux/init.h>
281da177e4SLinus Torvalds #include <linux/pci.h>
291da177e4SLinus Torvalds #include <linux/slab.h>
301da177e4SLinus Torvalds #include <linux/gameport.h>
311da177e4SLinus Torvalds #include <linux/moduleparam.h>
3262932df8SIngo Molnar #include <linux/mutex.h>
331da177e4SLinus Torvalds #include <sound/core.h>
341da177e4SLinus Torvalds #include <sound/info.h>
351da177e4SLinus Torvalds #include <sound/control.h>
361da177e4SLinus Torvalds #include <sound/pcm.h>
371da177e4SLinus Torvalds #include <sound/rawmidi.h>
381da177e4SLinus Torvalds #include <sound/mpu401.h>
391da177e4SLinus Torvalds #include <sound/opl3.h>
401da177e4SLinus Torvalds #include <sound/sb.h>
411da177e4SLinus Torvalds #include <sound/asoundef.h>
421da177e4SLinus Torvalds #include <sound/initval.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
451da177e4SLinus Torvalds MODULE_DESCRIPTION("C-Media CMI8x38 PCI");
461da177e4SLinus Torvalds MODULE_LICENSE("GPL");
471da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8738},"
481da177e4SLinus Torvalds 		"{C-Media,CMI8738B},"
491da177e4SLinus Torvalds 		"{C-Media,CMI8338A},"
501da177e4SLinus Torvalds 		"{C-Media,CMI8338B}}");
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
531da177e4SLinus Torvalds #define SUPPORT_JOYSTICK 1
541da177e4SLinus Torvalds #endif
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
571da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
581da177e4SLinus Torvalds static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable switches */
591da177e4SLinus Torvalds static long mpu_port[SNDRV_CARDS];
602f24d159STakashi Iwai static long fm_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
611da177e4SLinus Torvalds static int soft_ac3[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
621da177e4SLinus Torvalds #ifdef SUPPORT_JOYSTICK
631da177e4SLinus Torvalds static int joystick_port[SNDRV_CARDS];
641da177e4SLinus Torvalds #endif
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
671da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for C-Media PCI soundcard.");
681da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
691da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for C-Media PCI soundcard.");
701da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
711da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable C-Media PCI soundcard.");
721da177e4SLinus Torvalds module_param_array(mpu_port, long, NULL, 0444);
731da177e4SLinus Torvalds MODULE_PARM_DESC(mpu_port, "MPU-401 port.");
741da177e4SLinus Torvalds module_param_array(fm_port, long, NULL, 0444);
751da177e4SLinus Torvalds MODULE_PARM_DESC(fm_port, "FM port.");
761da177e4SLinus Torvalds module_param_array(soft_ac3, bool, NULL, 0444);
771da177e4SLinus Torvalds MODULE_PARM_DESC(soft_ac3, "Sofware-conversion of raw SPDIF packets (model 033 only).");
781da177e4SLinus Torvalds #ifdef SUPPORT_JOYSTICK
791da177e4SLinus Torvalds module_param_array(joystick_port, int, NULL, 0444);
801da177e4SLinus Torvalds MODULE_PARM_DESC(joystick_port, "Joystick port address.");
811da177e4SLinus Torvalds #endif
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /*
841da177e4SLinus Torvalds  * CM8x38 registers definition
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #define CM_REG_FUNCTRL0		0x00
881da177e4SLinus Torvalds #define CM_RST_CH1		0x00080000
891da177e4SLinus Torvalds #define CM_RST_CH0		0x00040000
901da177e4SLinus Torvalds #define CM_CHEN1		0x00020000	/* ch1: enable */
911da177e4SLinus Torvalds #define CM_CHEN0		0x00010000	/* ch0: enable */
921da177e4SLinus Torvalds #define CM_PAUSE1		0x00000008	/* ch1: pause */
931da177e4SLinus Torvalds #define CM_PAUSE0		0x00000004	/* ch0: pause */
941da177e4SLinus Torvalds #define CM_CHADC1		0x00000002	/* ch1, 0:playback, 1:record */
951da177e4SLinus Torvalds #define CM_CHADC0		0x00000001	/* ch0, 0:playback, 1:record */
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds #define CM_REG_FUNCTRL1		0x04
98a839a33dSClemens Ladisch #define CM_DSFC_MASK		0x0000E000	/* channel 1 (DAC?) sampling frequency */
99a839a33dSClemens Ladisch #define CM_DSFC_SHIFT		13
100a839a33dSClemens Ladisch #define CM_ASFC_MASK		0x00001C00	/* channel 0 (ADC?) sampling frequency */
101a839a33dSClemens Ladisch #define CM_ASFC_SHIFT		10
1021da177e4SLinus Torvalds #define CM_SPDF_1		0x00000200	/* SPDIF IN/OUT at channel B */
1031da177e4SLinus Torvalds #define CM_SPDF_0		0x00000100	/* SPDIF OUT only channel A */
104a839a33dSClemens Ladisch #define CM_SPDFLOOP		0x00000080	/* ext. SPDIIF/IN -> OUT loopback */
1051da177e4SLinus Torvalds #define CM_SPDO2DAC		0x00000040	/* SPDIF/OUT can be heard from internal DAC */
1061da177e4SLinus Torvalds #define CM_INTRM		0x00000020	/* master control block (MCB) interrupt enabled */
1071da177e4SLinus Torvalds #define CM_BREQ			0x00000010	/* bus master enabled */
1081da177e4SLinus Torvalds #define CM_VOICE_EN		0x00000008	/* legacy voice (SB16,FM) */
109a839a33dSClemens Ladisch #define CM_UART_EN		0x00000004	/* legacy UART */
110a839a33dSClemens Ladisch #define CM_JYSTK_EN		0x00000002	/* legacy joystick */
111a839a33dSClemens Ladisch #define CM_ZVPORT		0x00000001	/* ZVPORT */
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds #define CM_REG_CHFORMAT		0x08
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds #define CM_CHB3D5C		0x80000000	/* 5,6 channels */
116a839a33dSClemens Ladisch #define CM_FMOFFSET2		0x40000000	/* initial FM PCM offset 2 when Fmute=1 */
1171da177e4SLinus Torvalds #define CM_CHB3D		0x20000000	/* 4 channels */
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds #define CM_CHIP_MASK1		0x1f000000
1201da177e4SLinus Torvalds #define CM_CHIP_037		0x01000000
121a839a33dSClemens Ladisch #define CM_SETLAT48		0x00800000	/* set latency timer 48h */
122a839a33dSClemens Ladisch #define CM_EDGEIRQ		0x00400000	/* emulated edge trigger legacy IRQ */
123a839a33dSClemens Ladisch #define CM_SPD24SEL39		0x00200000	/* 24-bit spdif: model 039 */
1241da177e4SLinus Torvalds #define CM_AC3EN1		0x00100000	/* enable AC3: model 037 */
125a839a33dSClemens Ladisch #define CM_SPDIF_SELECT1	0x00080000	/* for model <= 037 ? */
1261da177e4SLinus Torvalds #define CM_SPD24SEL		0x00020000	/* 24bit spdif: model 037 */
1271da177e4SLinus Torvalds /* #define CM_SPDIF_INVERSE	0x00010000 */ /* ??? */
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds #define CM_ADCBITLEN_MASK	0x0000C000
1301da177e4SLinus Torvalds #define CM_ADCBITLEN_16		0x00000000
1311da177e4SLinus Torvalds #define CM_ADCBITLEN_15		0x00004000
1321da177e4SLinus Torvalds #define CM_ADCBITLEN_14		0x00008000
1331da177e4SLinus Torvalds #define CM_ADCBITLEN_13		0x0000C000
1341da177e4SLinus Torvalds 
135a839a33dSClemens Ladisch #define CM_ADCDACLEN_MASK	0x00003000	/* model 037 */
1361da177e4SLinus Torvalds #define CM_ADCDACLEN_060	0x00000000
1371da177e4SLinus Torvalds #define CM_ADCDACLEN_066	0x00001000
1381da177e4SLinus Torvalds #define CM_ADCDACLEN_130	0x00002000
1391da177e4SLinus Torvalds #define CM_ADCDACLEN_280	0x00003000
1401da177e4SLinus Torvalds 
141a839a33dSClemens Ladisch #define CM_ADCDLEN_MASK		0x00003000	/* model 039 */
142a839a33dSClemens Ladisch #define CM_ADCDLEN_ORIGINAL	0x00000000
143a839a33dSClemens Ladisch #define CM_ADCDLEN_EXTRA	0x00001000
144a839a33dSClemens Ladisch #define CM_ADCDLEN_24K		0x00002000
145a839a33dSClemens Ladisch #define CM_ADCDLEN_WEIGHT	0x00003000
146a839a33dSClemens Ladisch 
1471da177e4SLinus Torvalds #define CM_CH1_SRATE_176K	0x00000800
1488992e18dSClemens Ladisch #define CM_CH1_SRATE_96K	0x00000800	/* model 055? */
1491da177e4SLinus Torvalds #define CM_CH1_SRATE_88K	0x00000400
1501da177e4SLinus Torvalds #define CM_CH0_SRATE_176K	0x00000200
1518992e18dSClemens Ladisch #define CM_CH0_SRATE_96K	0x00000200	/* model 055? */
1521da177e4SLinus Torvalds #define CM_CH0_SRATE_88K	0x00000100
153755c48abSTimofei Bondarenko #define CM_CH0_SRATE_128K	0x00000300
154755c48abSTimofei Bondarenko #define CM_CH0_SRATE_MASK	0x00000300
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds #define CM_SPDIF_INVERSE2	0x00000080	/* model 055? */
157a839a33dSClemens Ladisch #define CM_DBLSPDS		0x00000040	/* double SPDIF sample rate 88.2/96 */
158a839a33dSClemens Ladisch #define CM_POLVALID		0x00000020	/* inverse SPDIF/IN valid bit */
159a839a33dSClemens Ladisch #define CM_SPDLOCKED		0x00000010
1601da177e4SLinus Torvalds 
161a839a33dSClemens Ladisch #define CM_CH1FMT_MASK		0x0000000C	/* bit 3: 16 bits, bit 2: stereo */
1621da177e4SLinus Torvalds #define CM_CH1FMT_SHIFT		2
163a839a33dSClemens Ladisch #define CM_CH0FMT_MASK		0x00000003	/* bit 1: 16 bits, bit 0: stereo */
1641da177e4SLinus Torvalds #define CM_CH0FMT_SHIFT		0
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds #define CM_REG_INT_HLDCLR	0x0C
1671da177e4SLinus Torvalds #define CM_CHIP_MASK2		0xff000000
168a839a33dSClemens Ladisch #define CM_CHIP_8768		0x20000000
169a839a33dSClemens Ladisch #define CM_CHIP_055		0x08000000
1701da177e4SLinus Torvalds #define CM_CHIP_039		0x04000000
1711da177e4SLinus Torvalds #define CM_CHIP_039_6CH		0x01000000
172a839a33dSClemens Ladisch #define CM_UNKNOWN_INT_EN	0x00080000	/* ? */
1731da177e4SLinus Torvalds #define CM_TDMA_INT_EN		0x00040000
1741da177e4SLinus Torvalds #define CM_CH1_INT_EN		0x00020000
1751da177e4SLinus Torvalds #define CM_CH0_INT_EN		0x00010000
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds #define CM_REG_INT_STATUS	0x10
1781da177e4SLinus Torvalds #define CM_INTR			0x80000000
1791da177e4SLinus Torvalds #define CM_VCO			0x08000000	/* Voice Control? CMI8738 */
1801da177e4SLinus Torvalds #define CM_MCBINT		0x04000000	/* Master Control Block abort cond.? */
1811da177e4SLinus Torvalds #define CM_UARTINT		0x00010000
1821da177e4SLinus Torvalds #define CM_LTDMAINT		0x00008000
1831da177e4SLinus Torvalds #define CM_HTDMAINT		0x00004000
1841da177e4SLinus Torvalds #define CM_XDO46		0x00000080	/* Modell 033? Direct programming EEPROM (read data register) */
1851da177e4SLinus Torvalds #define CM_LHBTOG		0x00000040	/* High/Low status from DMA ctrl register */
1861da177e4SLinus Torvalds #define CM_LEG_HDMA		0x00000020	/* Legacy is in High DMA channel */
1871da177e4SLinus Torvalds #define CM_LEG_STEREO		0x00000010	/* Legacy is in Stereo mode */
1881da177e4SLinus Torvalds #define CM_CH1BUSY		0x00000008
1891da177e4SLinus Torvalds #define CM_CH0BUSY		0x00000004
1901da177e4SLinus Torvalds #define CM_CHINT1		0x00000002
1911da177e4SLinus Torvalds #define CM_CHINT0		0x00000001
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds #define CM_REG_LEGACY_CTRL	0x14
194a839a33dSClemens Ladisch #define CM_NXCHG		0x80000000	/* don't map base reg dword->sample */
1951da177e4SLinus Torvalds #define CM_VMPU_MASK		0x60000000	/* MPU401 i/o port address */
1961da177e4SLinus Torvalds #define CM_VMPU_330		0x00000000
1971da177e4SLinus Torvalds #define CM_VMPU_320		0x20000000
1981da177e4SLinus Torvalds #define CM_VMPU_310		0x40000000
1991da177e4SLinus Torvalds #define CM_VMPU_300		0x60000000
200a839a33dSClemens Ladisch #define CM_ENWR8237		0x10000000	/* enable bus master to write 8237 base reg */
2011da177e4SLinus Torvalds #define CM_VSBSEL_MASK		0x0C000000	/* SB16 base address */
2021da177e4SLinus Torvalds #define CM_VSBSEL_220		0x00000000
2031da177e4SLinus Torvalds #define CM_VSBSEL_240		0x04000000
2041da177e4SLinus Torvalds #define CM_VSBSEL_260		0x08000000
2051da177e4SLinus Torvalds #define CM_VSBSEL_280		0x0C000000
2061da177e4SLinus Torvalds #define CM_FMSEL_MASK		0x03000000	/* FM OPL3 base address */
2071da177e4SLinus Torvalds #define CM_FMSEL_388		0x00000000
2081da177e4SLinus Torvalds #define CM_FMSEL_3C8		0x01000000
2091da177e4SLinus Torvalds #define CM_FMSEL_3E0		0x02000000
2101da177e4SLinus Torvalds #define CM_FMSEL_3E8		0x03000000
211a839a33dSClemens Ladisch #define CM_ENSPDOUT		0x00800000	/* enable XSPDIF/OUT to I/O interface */
212a839a33dSClemens Ladisch #define CM_SPDCOPYRHT		0x00400000	/* spdif in/out copyright bit */
2131da177e4SLinus Torvalds #define CM_DAC2SPDO		0x00200000	/* enable wave+fm_midi -> SPDIF/OUT */
214a839a33dSClemens Ladisch #define CM_INVIDWEN		0x00100000	/* internal vendor ID write enable, model 039? */
215a839a33dSClemens Ladisch #define CM_SETRETRY		0x00100000	/* 0: legacy i/o wait (default), 1: legacy i/o bus retry */
216a839a33dSClemens Ladisch #define CM_C_EEACCESS		0x00080000	/* direct programming eeprom regs */
217a839a33dSClemens Ladisch #define CM_C_EECS		0x00040000
218a839a33dSClemens Ladisch #define CM_C_EEDI46		0x00020000
219a839a33dSClemens Ladisch #define CM_C_EECK46		0x00010000
2201da177e4SLinus Torvalds #define CM_CHB3D6C		0x00008000	/* 5.1 channels support */
221a839a33dSClemens Ladisch #define CM_CENTR2LIN		0x00004000	/* line-in as center out */
222a839a33dSClemens Ladisch #define CM_BASE2LIN		0x00002000	/* line-in as bass out */
223a839a33dSClemens Ladisch #define CM_EXBASEN		0x00001000	/* external bass input enable */
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds #define CM_REG_MISC_CTRL	0x18
226a839a33dSClemens Ladisch #define CM_PWD			0x80000000	/* power down */
2271da177e4SLinus Torvalds #define CM_RESET		0x40000000
228a839a33dSClemens Ladisch #define CM_SFIL_MASK		0x30000000	/* filter control at front end DAC, model 037? */
229a839a33dSClemens Ladisch #define CM_VMGAIN		0x10000000	/* analog master amp +6dB, model 039? */
230a839a33dSClemens Ladisch #define CM_TXVX			0x08000000	/* model 037? */
231a839a33dSClemens Ladisch #define CM_N4SPK3D		0x04000000	/* copy front to rear */
2321da177e4SLinus Torvalds #define CM_SPDO5V		0x02000000	/* 5V spdif output (1 = 0.5v (coax)) */
2331da177e4SLinus Torvalds #define CM_SPDIF48K		0x01000000	/* write */
2341da177e4SLinus Torvalds #define CM_SPATUS48K		0x01000000	/* read */
235a839a33dSClemens Ladisch #define CM_ENDBDAC		0x00800000	/* enable double dac */
2361da177e4SLinus Torvalds #define CM_XCHGDAC		0x00400000	/* 0: front=ch0, 1: front=ch1 */
2371da177e4SLinus Torvalds #define CM_SPD32SEL		0x00200000	/* 0: 16bit SPDIF, 1: 32bit */
238a839a33dSClemens Ladisch #define CM_SPDFLOOPI		0x00100000	/* int. SPDIF-OUT -> int. IN */
239a839a33dSClemens Ladisch #define CM_FM_EN		0x00080000	/* enable legacy FM */
2401da177e4SLinus Torvalds #define CM_AC3EN2		0x00040000	/* enable AC3: model 039 */
241a839a33dSClemens Ladisch #define CM_ENWRASID		0x00010000	/* choose writable internal SUBID (audio) */
242a839a33dSClemens Ladisch #define CM_VIDWPDSB		0x00010000	/* model 037? */
2431da177e4SLinus Torvalds #define CM_SPDF_AC97		0x00008000	/* 0: SPDIF/OUT 44.1K, 1: 48K */
244a839a33dSClemens Ladisch #define CM_MASK_EN		0x00004000	/* activate channel mask on legacy DMA */
245a839a33dSClemens Ladisch #define CM_ENWRMSID		0x00002000	/* choose writable internal SUBID (modem) */
246a839a33dSClemens Ladisch #define CM_VIDWPPRT		0x00002000	/* model 037? */
247a839a33dSClemens Ladisch #define CM_SFILENB		0x00001000	/* filter stepping at front end DAC, model 037? */
248a839a33dSClemens Ladisch #define CM_MMODE_MASK		0x00000E00	/* model DAA interface mode */
2491da177e4SLinus Torvalds #define CM_SPDIF_SELECT2	0x00000100	/* for model > 039 ? */
2501da177e4SLinus Torvalds #define CM_ENCENTER		0x00000080
25156c36ca3SClemens Ladisch #define CM_FLINKON		0x00000040	/* force modem link detection on, model 037 */
252a839a33dSClemens Ladisch #define CM_MUTECH1		0x00000040	/* mute PCI ch1 to DAC */
25356c36ca3SClemens Ladisch #define CM_FLINKOFF		0x00000020	/* force modem link detection off, model 037 */
254a839a33dSClemens Ladisch #define CM_MIDSMP		0x00000010	/* 1/2 interpolation at front end DAC */
255a839a33dSClemens Ladisch #define CM_UPDDMA_MASK		0x0000000C	/* TDMA position update notification */
256a839a33dSClemens Ladisch #define CM_UPDDMA_2048		0x00000000
257a839a33dSClemens Ladisch #define CM_UPDDMA_1024		0x00000004
258a839a33dSClemens Ladisch #define CM_UPDDMA_512		0x00000008
259a839a33dSClemens Ladisch #define CM_UPDDMA_256		0x0000000C
260a839a33dSClemens Ladisch #define CM_TWAIT_MASK		0x00000003	/* model 037 */
261a839a33dSClemens Ladisch #define CM_TWAIT1		0x00000002	/* FM i/o cycle, 0: 48, 1: 64 PCICLKs */
262a839a33dSClemens Ladisch #define CM_TWAIT0		0x00000001	/* i/o cycle, 0: 4, 1: 6 PCICLKs */
263a839a33dSClemens Ladisch 
264a839a33dSClemens Ladisch #define CM_REG_TDMA_POSITION	0x1C
265a839a33dSClemens Ladisch #define CM_TDMA_CNT_MASK	0xFFFF0000	/* current byte/word count */
266a839a33dSClemens Ladisch #define CM_TDMA_ADR_MASK	0x0000FFFF	/* current address */
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	/* byte */
2691da177e4SLinus Torvalds #define CM_REG_MIXER0		0x20
270a839a33dSClemens Ladisch #define CM_REG_SBVR		0x20		/* write: sb16 version */
271a839a33dSClemens Ladisch #define CM_REG_DEV		0x20		/* read: hardware device version */
272a839a33dSClemens Ladisch 
273a839a33dSClemens Ladisch #define CM_REG_MIXER21		0x21
274a839a33dSClemens Ladisch #define CM_UNKNOWN_21_MASK	0x78		/* ? */
275a839a33dSClemens Ladisch #define CM_X_ADPCM		0x04		/* SB16 ADPCM enable */
276a839a33dSClemens Ladisch #define CM_PROINV		0x02		/* SBPro left/right channel switching */
277a839a33dSClemens Ladisch #define CM_X_SB16		0x01		/* SB16 compatible */
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds #define CM_REG_SB16_DATA	0x22
2801da177e4SLinus Torvalds #define CM_REG_SB16_ADDR	0x23
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds #define CM_REFFREQ_XIN		(315*1000*1000)/22	/* 14.31818 Mhz reference clock frequency pin XIN */
2831da177e4SLinus Torvalds #define CM_ADCMULT_XIN		512			/* Guessed (487 best for 44.1kHz, not for 88/176kHz) */
2841da177e4SLinus Torvalds #define CM_TOLERANCE_RATE	0.001			/* Tolerance sample rate pitch (1000ppm) */
2851da177e4SLinus Torvalds #define CM_MAXIMUM_RATE		80000000		/* Note more than 80MHz */
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds #define CM_REG_MIXER1		0x24
2881da177e4SLinus Torvalds #define CM_FMMUTE		0x80	/* mute FM */
2891da177e4SLinus Torvalds #define CM_FMMUTE_SHIFT		7
2901da177e4SLinus Torvalds #define CM_WSMUTE		0x40	/* mute PCM */
2911da177e4SLinus Torvalds #define CM_WSMUTE_SHIFT		6
292a839a33dSClemens Ladisch #define CM_REAR2LIN		0x20	/* lin-in -> rear line out */
293a839a33dSClemens Ladisch #define CM_REAR2LIN_SHIFT	5
2941da177e4SLinus Torvalds #define CM_REAR2FRONT		0x10	/* exchange rear/front */
2951da177e4SLinus Torvalds #define CM_REAR2FRONT_SHIFT	4
2961da177e4SLinus Torvalds #define CM_WAVEINL		0x08	/* digital wave rec. left chan */
2971da177e4SLinus Torvalds #define CM_WAVEINL_SHIFT	3
2981da177e4SLinus Torvalds #define CM_WAVEINR		0x04	/* digical wave rec. right */
2991da177e4SLinus Torvalds #define CM_WAVEINR_SHIFT	2
3001da177e4SLinus Torvalds #define CM_X3DEN		0x02	/* 3D surround enable */
3011da177e4SLinus Torvalds #define CM_X3DEN_SHIFT		1
3021da177e4SLinus Torvalds #define CM_CDPLAY		0x01	/* enable SPDIF/IN PCM -> DAC */
3031da177e4SLinus Torvalds #define CM_CDPLAY_SHIFT		0
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds #define CM_REG_MIXER2		0x25
3061da177e4SLinus Torvalds #define CM_RAUXREN		0x80	/* AUX right capture */
3071da177e4SLinus Torvalds #define CM_RAUXREN_SHIFT	7
3081da177e4SLinus Torvalds #define CM_RAUXLEN		0x40	/* AUX left capture */
3091da177e4SLinus Torvalds #define CM_RAUXLEN_SHIFT	6
3101da177e4SLinus Torvalds #define CM_VAUXRM		0x20	/* AUX right mute */
3111da177e4SLinus Torvalds #define CM_VAUXRM_SHIFT		5
3121da177e4SLinus Torvalds #define CM_VAUXLM		0x10	/* AUX left mute */
3131da177e4SLinus Torvalds #define CM_VAUXLM_SHIFT		4
3141da177e4SLinus Torvalds #define CM_VADMIC_MASK		0x0e	/* mic gain level (0-3) << 1 */
3151da177e4SLinus Torvalds #define CM_VADMIC_SHIFT		1
3161da177e4SLinus Torvalds #define CM_MICGAINZ		0x01	/* mic boost */
3171da177e4SLinus Torvalds #define CM_MICGAINZ_SHIFT	0
3181da177e4SLinus Torvalds 
319cb60e5f5STakashi Iwai #define CM_REG_MIXER3		0x24
3201da177e4SLinus Torvalds #define CM_REG_AUX_VOL		0x26
3211da177e4SLinus Torvalds #define CM_VAUXL_MASK		0xf0
3221da177e4SLinus Torvalds #define CM_VAUXR_MASK		0x0f
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds #define CM_REG_MISC		0x27
325a839a33dSClemens Ladisch #define CM_UNKNOWN_27_MASK	0xd8	/* ? */
3261da177e4SLinus Torvalds #define CM_XGPO1		0x20
3271da177e4SLinus Torvalds // #define CM_XGPBIO		0x04
3281da177e4SLinus Torvalds #define CM_MIC_CENTER_LFE	0x04	/* mic as center/lfe out? (model 039 or later?) */
3291da177e4SLinus Torvalds #define CM_SPDIF_INVERSE	0x04	/* spdif input phase inverse (model 037) */
3301da177e4SLinus Torvalds #define CM_SPDVALID		0x02	/* spdif input valid check */
331a839a33dSClemens Ladisch #define CM_DMAUTO		0x01	/* SB16 DMA auto detect */
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds #define CM_REG_AC97		0x28	/* hmmm.. do we have ac97 link? */
3341da177e4SLinus Torvalds /*
3351da177e4SLinus Torvalds  * For CMI-8338 (0x28 - 0x2b) .. is this valid for CMI-8738
3361da177e4SLinus Torvalds  * or identical with AC97 codec?
3371da177e4SLinus Torvalds  */
3381da177e4SLinus Torvalds #define CM_REG_EXTERN_CODEC	CM_REG_AC97
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds /*
3411da177e4SLinus Torvalds  * MPU401 pci port index address 0x40 - 0x4f (CMI-8738 spec ver. 0.6)
3421da177e4SLinus Torvalds  */
3431da177e4SLinus Torvalds #define CM_REG_MPU_PCI		0x40
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds /*
3461da177e4SLinus Torvalds  * FM pci port index address 0x50 - 0x5f (CMI-8738 spec ver. 0.6)
3471da177e4SLinus Torvalds  */
3481da177e4SLinus Torvalds #define CM_REG_FM_PCI		0x50
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds /*
3512eff7ec8STakashi Iwai  * access from SB-mixer port
3521da177e4SLinus Torvalds  */
3531da177e4SLinus Torvalds #define CM_REG_EXTENT_IND	0xf0
3541da177e4SLinus Torvalds #define CM_VPHONE_MASK		0xe0	/* Phone volume control (0-3) << 5 */
3551da177e4SLinus Torvalds #define CM_VPHONE_SHIFT		5
3561da177e4SLinus Torvalds #define CM_VPHOM		0x10	/* Phone mute control */
3571da177e4SLinus Torvalds #define CM_VSPKM		0x08	/* Speaker mute control, default high */
3581da177e4SLinus Torvalds #define CM_RLOOPREN		0x04    /* Rec. R-channel enable */
3591da177e4SLinus Torvalds #define CM_RLOOPLEN		0x02	/* Rec. L-channel enable */
3602eff7ec8STakashi Iwai #define CM_VADMIC3		0x01	/* Mic record boost */
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds /*
3631da177e4SLinus Torvalds  * CMI-8338 spec ver 0.5 (this is not valid for CMI-8738):
3641da177e4SLinus Torvalds  * the 8 registers 0xf8 - 0xff are used for programming m/n counter by the PLL
3651da177e4SLinus Torvalds  * unit (readonly?).
3661da177e4SLinus Torvalds  */
3671da177e4SLinus Torvalds #define CM_REG_PLL		0xf8
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds /*
3701da177e4SLinus Torvalds  * extended registers
3711da177e4SLinus Torvalds  */
372a839a33dSClemens Ladisch #define CM_REG_CH0_FRAME1	0x80	/* write: base address */
373a839a33dSClemens Ladisch #define CM_REG_CH0_FRAME2	0x84	/* read: current address */
3741da177e4SLinus Torvalds #define CM_REG_CH1_FRAME1	0x88	/* 0-15: count of samples at bus master; buffer size */
3751da177e4SLinus Torvalds #define CM_REG_CH1_FRAME2	0x8C	/* 16-31: count of samples at codec; fragment size */
376a839a33dSClemens Ladisch 
377cb60e5f5STakashi Iwai #define CM_REG_EXT_MISC		0x90
378a839a33dSClemens Ladisch #define CM_ADC48K44K		0x10000000	/* ADC parameters group, 0: 44k, 1: 48k */
379a839a33dSClemens Ladisch #define CM_CHB3D8C		0x00200000	/* 7.1 channels support */
380a839a33dSClemens Ladisch #define CM_SPD32FMT		0x00100000	/* SPDIF/IN 32k sample rate */
381a839a33dSClemens Ladisch #define CM_ADC2SPDIF		0x00080000	/* ADC output to SPDIF/OUT */
382a839a33dSClemens Ladisch #define CM_SHAREADC		0x00040000	/* DAC in ADC as Center/LFE */
383a839a33dSClemens Ladisch #define CM_REALTCMP		0x00020000	/* monitor the CMPL/CMPR of ADC */
384a839a33dSClemens Ladisch #define CM_INVLRCK		0x00010000	/* invert ZVPORT's LRCK */
385a839a33dSClemens Ladisch #define CM_UNKNOWN_90_MASK	0x0000FFFF	/* ? */
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds /*
3881da177e4SLinus Torvalds  * size of i/o region
3891da177e4SLinus Torvalds  */
3901da177e4SLinus Torvalds #define CM_EXTENT_CODEC	  0x100
3911da177e4SLinus Torvalds #define CM_EXTENT_MIDI	  0x2
3921da177e4SLinus Torvalds #define CM_EXTENT_SYNTH	  0x4
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds /*
3961da177e4SLinus Torvalds  * channels for playback / capture
3971da177e4SLinus Torvalds  */
3981da177e4SLinus Torvalds #define CM_CH_PLAY	0
3991da177e4SLinus Torvalds #define CM_CH_CAPT	1
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds /*
4021da177e4SLinus Torvalds  * flags to check device open/close
4031da177e4SLinus Torvalds  */
4041da177e4SLinus Torvalds #define CM_OPEN_NONE	0
4051da177e4SLinus Torvalds #define CM_OPEN_CH_MASK	0x01
4061da177e4SLinus Torvalds #define CM_OPEN_DAC	0x10
4071da177e4SLinus Torvalds #define CM_OPEN_ADC	0x20
4081da177e4SLinus Torvalds #define CM_OPEN_SPDIF	0x40
4091da177e4SLinus Torvalds #define CM_OPEN_MCHAN	0x80
4101da177e4SLinus Torvalds #define CM_OPEN_PLAYBACK	(CM_CH_PLAY | CM_OPEN_DAC)
4111da177e4SLinus Torvalds #define CM_OPEN_PLAYBACK2	(CM_CH_CAPT | CM_OPEN_DAC)
4121da177e4SLinus Torvalds #define CM_OPEN_PLAYBACK_MULTI	(CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_MCHAN)
4131da177e4SLinus Torvalds #define CM_OPEN_CAPTURE		(CM_CH_CAPT | CM_OPEN_ADC)
4141da177e4SLinus Torvalds #define CM_OPEN_SPDIF_PLAYBACK	(CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_SPDIF)
4151da177e4SLinus Torvalds #define CM_OPEN_SPDIF_CAPTURE	(CM_CH_CAPT | CM_OPEN_ADC | CM_OPEN_SPDIF)
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds #if CM_CH_PLAY == 1
4191da177e4SLinus Torvalds #define CM_PLAYBACK_SRATE_176K	CM_CH1_SRATE_176K
4201da177e4SLinus Torvalds #define CM_PLAYBACK_SPDF	CM_SPDF_1
4211da177e4SLinus Torvalds #define CM_CAPTURE_SPDF		CM_SPDF_0
4221da177e4SLinus Torvalds #else
4231da177e4SLinus Torvalds #define CM_PLAYBACK_SRATE_176K CM_CH0_SRATE_176K
4241da177e4SLinus Torvalds #define CM_PLAYBACK_SPDF	CM_SPDF_0
4251da177e4SLinus Torvalds #define CM_CAPTURE_SPDF		CM_SPDF_1
4261da177e4SLinus Torvalds #endif
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds /*
4301da177e4SLinus Torvalds  * driver data
4311da177e4SLinus Torvalds  */
4321da177e4SLinus Torvalds 
4332cbdb686STakashi Iwai struct cmipci_pcm {
4342cbdb686STakashi Iwai 	struct snd_pcm_substream *substream;
435ebe9e289SClemens Ladisch 	u8 running;		/* dac/adc running? */
436ebe9e289SClemens Ladisch 	u8 fmt;			/* format bits */
437ebe9e289SClemens Ladisch 	u8 is_dac;
438c36fd8c3SClemens Ladisch 	u8 needs_silencing;
4391da177e4SLinus Torvalds 	unsigned int dma_size;	/* in frames */
440ebe9e289SClemens Ladisch 	unsigned int shift;
441ebe9e289SClemens Ladisch 	unsigned int ch;	/* channel (0/1) */
4421da177e4SLinus Torvalds 	unsigned int offset;	/* physical address of the buffer */
4431da177e4SLinus Torvalds };
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds /* mixer elements toggled/resumed during ac3 playback */
4461da177e4SLinus Torvalds struct cmipci_mixer_auto_switches {
4471da177e4SLinus Torvalds 	const char *name;	/* switch to toggle */
4481da177e4SLinus Torvalds 	int toggle_on;		/* value to change when ac3 mode */
4491da177e4SLinus Torvalds };
4501da177e4SLinus Torvalds static const struct cmipci_mixer_auto_switches cm_saved_mixer[] = {
4511da177e4SLinus Torvalds 	{"PCM Playback Switch", 0},
4521da177e4SLinus Torvalds 	{"IEC958 Output Switch", 1},
4531da177e4SLinus Torvalds 	{"IEC958 Mix Analog", 0},
4541da177e4SLinus Torvalds 	// {"IEC958 Out To DAC", 1}, // no longer used
4551da177e4SLinus Torvalds 	{"IEC958 Loop", 0},
4561da177e4SLinus Torvalds };
4571da177e4SLinus Torvalds #define CM_SAVED_MIXERS		ARRAY_SIZE(cm_saved_mixer)
4581da177e4SLinus Torvalds 
4592cbdb686STakashi Iwai struct cmipci {
4602cbdb686STakashi Iwai 	struct snd_card *card;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	struct pci_dev *pci;
4631da177e4SLinus Torvalds 	unsigned int device;	/* device ID */
4641da177e4SLinus Torvalds 	int irq;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	unsigned long iobase;
4671da177e4SLinus Torvalds 	unsigned int ctrl;	/* FUNCTRL0 current value */
4681da177e4SLinus Torvalds 
4692cbdb686STakashi Iwai 	struct snd_pcm *pcm;		/* DAC/ADC PCM */
4702cbdb686STakashi Iwai 	struct snd_pcm *pcm2;	/* 2nd DAC */
4712cbdb686STakashi Iwai 	struct snd_pcm *pcm_spdif;	/* SPDIF */
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	int chip_version;
4741da177e4SLinus Torvalds 	int max_channels;
4751da177e4SLinus Torvalds 	unsigned int can_ac3_sw: 1;
4761da177e4SLinus Torvalds 	unsigned int can_ac3_hw: 1;
4771da177e4SLinus Torvalds 	unsigned int can_multi_ch: 1;
478755c48abSTimofei Bondarenko 	unsigned int can_96k: 1;	/* samplerate above 48k */
4791da177e4SLinus Torvalds 	unsigned int do_soft_ac3: 1;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	unsigned int spdif_playback_avail: 1;	/* spdif ready? */
4821da177e4SLinus Torvalds 	unsigned int spdif_playback_enabled: 1;	/* spdif switch enabled? */
4831da177e4SLinus Torvalds 	int spdif_counter;	/* for software AC3 */
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	unsigned int dig_status;
4861da177e4SLinus Torvalds 	unsigned int dig_pcm_status;
4871da177e4SLinus Torvalds 
4882cbdb686STakashi Iwai 	struct snd_pcm_hardware *hw_info[3]; /* for playbacks */
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	int opened[2];	/* open mode */
49162932df8SIngo Molnar 	struct mutex open_mutex;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	unsigned int mixer_insensitive: 1;
4942cbdb686STakashi Iwai 	struct snd_kcontrol *mixer_res_ctl[CM_SAVED_MIXERS];
4951da177e4SLinus Torvalds 	int mixer_res_status[CM_SAVED_MIXERS];
4961da177e4SLinus Torvalds 
4972cbdb686STakashi Iwai 	struct cmipci_pcm channel[2];	/* ch0 - DAC, ch1 - ADC or 2nd DAC */
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	/* external MIDI */
5002cbdb686STakashi Iwai 	struct snd_rawmidi *rmidi;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds #ifdef SUPPORT_JOYSTICK
5031da177e4SLinus Torvalds 	struct gameport *gameport;
5041da177e4SLinus Torvalds #endif
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	spinlock_t reg_lock;
507cb60e5f5STakashi Iwai 
508cb60e5f5STakashi Iwai #ifdef CONFIG_PM
509cb60e5f5STakashi Iwai 	unsigned int saved_regs[0x20];
510cb60e5f5STakashi Iwai 	unsigned char saved_mixers[0x20];
511cb60e5f5STakashi Iwai #endif
5121da177e4SLinus Torvalds };
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds /* read/write operations for dword register */
5162cbdb686STakashi Iwai static inline void snd_cmipci_write(struct cmipci *cm, unsigned int cmd, unsigned int data)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	outl(data, cm->iobase + cmd);
5191da177e4SLinus Torvalds }
52077933d72SJesper Juhl 
5212cbdb686STakashi Iwai static inline unsigned int snd_cmipci_read(struct cmipci *cm, unsigned int cmd)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	return inl(cm->iobase + cmd);
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds /* read/write operations for word register */
5272cbdb686STakashi Iwai static inline void snd_cmipci_write_w(struct cmipci *cm, unsigned int cmd, unsigned short data)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	outw(data, cm->iobase + cmd);
5301da177e4SLinus Torvalds }
53177933d72SJesper Juhl 
5322cbdb686STakashi Iwai static inline unsigned short snd_cmipci_read_w(struct cmipci *cm, unsigned int cmd)
5331da177e4SLinus Torvalds {
5341da177e4SLinus Torvalds 	return inw(cm->iobase + cmd);
5351da177e4SLinus Torvalds }
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds /* read/write operations for byte register */
5382cbdb686STakashi Iwai static inline void snd_cmipci_write_b(struct cmipci *cm, unsigned int cmd, unsigned char data)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds 	outb(data, cm->iobase + cmd);
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
5432cbdb686STakashi Iwai static inline unsigned char snd_cmipci_read_b(struct cmipci *cm, unsigned int cmd)
5441da177e4SLinus Torvalds {
5451da177e4SLinus Torvalds 	return inb(cm->iobase + cmd);
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds /* bit operations for dword register */
5492cbdb686STakashi Iwai static int snd_cmipci_set_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
5501da177e4SLinus Torvalds {
55101d25d46STakashi Iwai 	unsigned int val, oval;
55201d25d46STakashi Iwai 	val = oval = inl(cm->iobase + cmd);
5531da177e4SLinus Torvalds 	val |= flag;
55401d25d46STakashi Iwai 	if (val == oval)
55501d25d46STakashi Iwai 		return 0;
5561da177e4SLinus Torvalds 	outl(val, cm->iobase + cmd);
55701d25d46STakashi Iwai 	return 1;
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5602cbdb686STakashi Iwai static int snd_cmipci_clear_bit(struct cmipci *cm, unsigned int cmd, unsigned int flag)
5611da177e4SLinus Torvalds {
56201d25d46STakashi Iwai 	unsigned int val, oval;
56301d25d46STakashi Iwai 	val = oval = inl(cm->iobase + cmd);
5641da177e4SLinus Torvalds 	val &= ~flag;
56501d25d46STakashi Iwai 	if (val == oval)
56601d25d46STakashi Iwai 		return 0;
5671da177e4SLinus Torvalds 	outl(val, cm->iobase + cmd);
56801d25d46STakashi Iwai 	return 1;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds /* bit operations for byte register */
5722cbdb686STakashi Iwai static int snd_cmipci_set_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
5731da177e4SLinus Torvalds {
57401d25d46STakashi Iwai 	unsigned char val, oval;
57501d25d46STakashi Iwai 	val = oval = inb(cm->iobase + cmd);
5761da177e4SLinus Torvalds 	val |= flag;
57701d25d46STakashi Iwai 	if (val == oval)
57801d25d46STakashi Iwai 		return 0;
5791da177e4SLinus Torvalds 	outb(val, cm->iobase + cmd);
58001d25d46STakashi Iwai 	return 1;
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
5832cbdb686STakashi Iwai static int snd_cmipci_clear_bit_b(struct cmipci *cm, unsigned int cmd, unsigned char flag)
5841da177e4SLinus Torvalds {
58501d25d46STakashi Iwai 	unsigned char val, oval;
58601d25d46STakashi Iwai 	val = oval = inb(cm->iobase + cmd);
5871da177e4SLinus Torvalds 	val &= ~flag;
58801d25d46STakashi Iwai 	if (val == oval)
58901d25d46STakashi Iwai 		return 0;
5901da177e4SLinus Torvalds 	outb(val, cm->iobase + cmd);
59101d25d46STakashi Iwai 	return 1;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds /*
5961da177e4SLinus Torvalds  * PCM interface
5971da177e4SLinus Torvalds  */
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds /*
6001da177e4SLinus Torvalds  * calculate frequency
6011da177e4SLinus Torvalds  */
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds static unsigned int rates[] = { 5512, 11025, 22050, 44100, 8000, 16000, 32000, 48000 };
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds static unsigned int snd_cmipci_rate_freq(unsigned int rate)
6061da177e4SLinus Torvalds {
6071da177e4SLinus Torvalds 	unsigned int i;
6080f28eca3SClemens Ladisch 
6091da177e4SLinus Torvalds 	for (i = 0; i < ARRAY_SIZE(rates); i++) {
6101da177e4SLinus Torvalds 		if (rates[i] == rate)
6111da177e4SLinus Torvalds 			return i;
6121da177e4SLinus Torvalds 	}
6131da177e4SLinus Torvalds 	snd_BUG();
6141da177e4SLinus Torvalds 	return 0;
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds #ifdef USE_VAR48KRATE
6181da177e4SLinus Torvalds /*
6191da177e4SLinus Torvalds  * Determine PLL values for frequency setup, maybe the CMI8338 (CMI8738???)
6201da177e4SLinus Torvalds  * does it this way .. maybe not.  Never get any information from C-Media about
6211da177e4SLinus Torvalds  * that <werner@suse.de>.
6221da177e4SLinus Torvalds  */
6231da177e4SLinus Torvalds static int snd_cmipci_pll_rmn(unsigned int rate, unsigned int adcmult, int *r, int *m, int *n)
6241da177e4SLinus Torvalds {
6251da177e4SLinus Torvalds 	unsigned int delta, tolerance;
6261da177e4SLinus Torvalds 	int xm, xn, xr;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	for (*r = 0; rate < CM_MAXIMUM_RATE/adcmult; *r += (1<<5))
6291da177e4SLinus Torvalds 		rate <<= 1;
6301da177e4SLinus Torvalds 	*n = -1;
6311da177e4SLinus Torvalds 	if (*r > 0xff)
6321da177e4SLinus Torvalds 		goto out;
6331da177e4SLinus Torvalds 	tolerance = rate*CM_TOLERANCE_RATE;
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	for (xn = (1+2); xn < (0x1f+2); xn++) {
6361da177e4SLinus Torvalds 		for (xm = (1+2); xm < (0xff+2); xm++) {
6371da177e4SLinus Torvalds 			xr = ((CM_REFFREQ_XIN/adcmult) * xm) / xn;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 			if (xr < rate)
6401da177e4SLinus Torvalds 				delta = rate - xr;
6411da177e4SLinus Torvalds 			else
6421da177e4SLinus Torvalds 				delta = xr - rate;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 			/*
6451da177e4SLinus Torvalds 			 * If we found one, remember this,
6461da177e4SLinus Torvalds 			 * and try to find a closer one
6471da177e4SLinus Torvalds 			 */
6481da177e4SLinus Torvalds 			if (delta < tolerance) {
6491da177e4SLinus Torvalds 				tolerance = delta;
6501da177e4SLinus Torvalds 				*m = xm - 2;
6511da177e4SLinus Torvalds 				*n = xn - 2;
6521da177e4SLinus Torvalds 			}
6531da177e4SLinus Torvalds 		}
6541da177e4SLinus Torvalds 	}
6551da177e4SLinus Torvalds out:
6561da177e4SLinus Torvalds 	return (*n > -1);
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds /*
6601da177e4SLinus Torvalds  * Program pll register bits, I assume that the 8 registers 0xf8 upto 0xff
6611da177e4SLinus Torvalds  * are mapped onto the 8 ADC/DAC sampling frequency which can be choosen
6621da177e4SLinus Torvalds  * at the register CM_REG_FUNCTRL1 (0x04).
6631da177e4SLinus Torvalds  * Problem: other ways are also possible (any information about that?)
6641da177e4SLinus Torvalds  */
6652cbdb686STakashi Iwai static void snd_cmipci_set_pll(struct cmipci *cm, unsigned int rate, unsigned int slot)
6661da177e4SLinus Torvalds {
6671da177e4SLinus Torvalds 	unsigned int reg = CM_REG_PLL + slot;
6681da177e4SLinus Torvalds 	/*
6691da177e4SLinus Torvalds 	 * Guess that this programs at reg. 0x04 the pos 15:13/12:10
6701da177e4SLinus Torvalds 	 * for DSFC/ASFC (000 upto 111).
6711da177e4SLinus Torvalds 	 */
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	/* FIXME: Init (Do we've to set an other register first before programming?) */
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	/* FIXME: Is this correct? Or shouldn't the m/n/r values be used for that? */
6761da177e4SLinus Torvalds 	snd_cmipci_write_b(cm, reg, rate>>8);
6771da177e4SLinus Torvalds 	snd_cmipci_write_b(cm, reg, rate&0xff);
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	/* FIXME: Setup (Do we've to set an other register first to enable this?) */
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds #endif /* USE_VAR48KRATE */
6821da177e4SLinus Torvalds 
6832cbdb686STakashi Iwai static int snd_cmipci_hw_params(struct snd_pcm_substream *substream,
6842cbdb686STakashi Iwai 				struct snd_pcm_hw_params *hw_params)
6851da177e4SLinus Torvalds {
6861da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
6871da177e4SLinus Torvalds }
6881da177e4SLinus Torvalds 
6892cbdb686STakashi Iwai static int snd_cmipci_playback2_hw_params(struct snd_pcm_substream *substream,
6902cbdb686STakashi Iwai 					  struct snd_pcm_hw_params *hw_params)
6911da177e4SLinus Torvalds {
6922cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
6931da177e4SLinus Torvalds 	if (params_channels(hw_params) > 2) {
69462932df8SIngo Molnar 		mutex_lock(&cm->open_mutex);
6951da177e4SLinus Torvalds 		if (cm->opened[CM_CH_PLAY]) {
69662932df8SIngo Molnar 			mutex_unlock(&cm->open_mutex);
6971da177e4SLinus Torvalds 			return -EBUSY;
6981da177e4SLinus Torvalds 		}
6991da177e4SLinus Torvalds 		/* reserve the channel A */
7001da177e4SLinus Torvalds 		cm->opened[CM_CH_PLAY] = CM_OPEN_PLAYBACK_MULTI;
70162932df8SIngo Molnar 		mutex_unlock(&cm->open_mutex);
7021da177e4SLinus Torvalds 	}
7031da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7062cbdb686STakashi Iwai static void snd_cmipci_ch_reset(struct cmipci *cm, int ch)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	int reset = CM_RST_CH0 << (cm->channel[ch].ch);
7091da177e4SLinus Torvalds 	snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
7101da177e4SLinus Torvalds 	snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
7111da177e4SLinus Torvalds 	udelay(10);
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7142cbdb686STakashi Iwai static int snd_cmipci_hw_free(struct snd_pcm_substream *substream)
7151da177e4SLinus Torvalds {
7161da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
7171da177e4SLinus Torvalds }
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds /*
7211da177e4SLinus Torvalds  */
7221da177e4SLinus Torvalds 
72335add1c2SClemens Ladisch static unsigned int hw_channels[] = {1, 2, 4, 6, 8};
7242cbdb686STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_channels_4 = {
7251da177e4SLinus Torvalds 	.count = 3,
7261da177e4SLinus Torvalds 	.list = hw_channels,
7271da177e4SLinus Torvalds 	.mask = 0,
7281da177e4SLinus Torvalds };
7292cbdb686STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_channels_6 = {
73035add1c2SClemens Ladisch 	.count = 4,
7311da177e4SLinus Torvalds 	.list = hw_channels,
7321da177e4SLinus Torvalds 	.mask = 0,
7331da177e4SLinus Torvalds };
7342cbdb686STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_channels_8 = {
73535add1c2SClemens Ladisch 	.count = 5,
7361da177e4SLinus Torvalds 	.list = hw_channels,
7371da177e4SLinus Torvalds 	.mask = 0,
7381da177e4SLinus Torvalds };
7391da177e4SLinus Torvalds 
7402cbdb686STakashi Iwai static int set_dac_channels(struct cmipci *cm, struct cmipci_pcm *rec, int channels)
7411da177e4SLinus Torvalds {
7421da177e4SLinus Torvalds 	if (channels > 2) {
7438ffbc01eSClemens Ladisch 		if (!cm->can_multi_ch || !rec->ch)
7441da177e4SLinus Torvalds 			return -EINVAL;
7451da177e4SLinus Torvalds 		if (rec->fmt != 0x03) /* stereo 16bit only */
7461da177e4SLinus Torvalds 			return -EINVAL;
7478ffbc01eSClemens Ladisch 	}
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	if (cm->can_multi_ch) {
7501da177e4SLinus Torvalds 		spin_lock_irq(&cm->reg_lock);
7518ffbc01eSClemens Ladisch 		if (channels > 2) {
7528ffbc01eSClemens Ladisch 			snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
7538ffbc01eSClemens Ladisch 			snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
7548ffbc01eSClemens Ladisch 		} else {
7551da177e4SLinus Torvalds 			snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
7568ffbc01eSClemens Ladisch 			snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
7578ffbc01eSClemens Ladisch 		}
7588ffbc01eSClemens Ladisch 		if (channels == 8)
7598ffbc01eSClemens Ladisch 			snd_cmipci_set_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
7608ffbc01eSClemens Ladisch 		else
7618ffbc01eSClemens Ladisch 			snd_cmipci_clear_bit(cm, CM_REG_EXT_MISC, CM_CHB3D8C);
7628ffbc01eSClemens Ladisch 		if (channels == 6) {
7638ffbc01eSClemens Ladisch 			snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
7648ffbc01eSClemens Ladisch 			snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
7658ffbc01eSClemens Ladisch 		} else {
7661da177e4SLinus Torvalds 			snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
7671da177e4SLinus Torvalds 			snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
7681da177e4SLinus Torvalds 		}
7698ffbc01eSClemens Ladisch 		if (channels == 4)
7708ffbc01eSClemens Ladisch 			snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
7718ffbc01eSClemens Ladisch 		else
7728ffbc01eSClemens Ladisch 			snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
7738ffbc01eSClemens Ladisch 		spin_unlock_irq(&cm->reg_lock);
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 	return 0;
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds /*
7801da177e4SLinus Torvalds  * prepare playback/capture channel
7811da177e4SLinus Torvalds  * channel to be used must have been set in rec->ch.
7821da177e4SLinus Torvalds  */
7832cbdb686STakashi Iwai static int snd_cmipci_pcm_prepare(struct cmipci *cm, struct cmipci_pcm *rec,
7842cbdb686STakashi Iwai 				 struct snd_pcm_substream *substream)
7851da177e4SLinus Torvalds {
786755c48abSTimofei Bondarenko 	unsigned int reg, freq, freq_ext, val;
787ebe9e289SClemens Ladisch 	unsigned int period_size;
7882cbdb686STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	rec->fmt = 0;
7911da177e4SLinus Torvalds 	rec->shift = 0;
7921da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) >= 16) {
7931da177e4SLinus Torvalds 		rec->fmt |= 0x02;
7941da177e4SLinus Torvalds 		if (snd_pcm_format_width(runtime->format) > 16)
7951da177e4SLinus Torvalds 			rec->shift++; /* 24/32bit */
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds 	if (runtime->channels > 1)
7981da177e4SLinus Torvalds 		rec->fmt |= 0x01;
7991da177e4SLinus Torvalds 	if (rec->is_dac && set_dac_channels(cm, rec, runtime->channels) < 0) {
8001da177e4SLinus Torvalds 		snd_printd("cannot set dac channels\n");
8011da177e4SLinus Torvalds 		return -EINVAL;
8021da177e4SLinus Torvalds 	}
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	rec->offset = runtime->dma_addr;
8051da177e4SLinus Torvalds 	/* buffer and period sizes in frame */
8061da177e4SLinus Torvalds 	rec->dma_size = runtime->buffer_size << rec->shift;
807ebe9e289SClemens Ladisch 	period_size = runtime->period_size << rec->shift;
8081da177e4SLinus Torvalds 	if (runtime->channels > 2) {
8091da177e4SLinus Torvalds 		/* multi-channels */
8101da177e4SLinus Torvalds 		rec->dma_size = (rec->dma_size * runtime->channels) / 2;
811ebe9e289SClemens Ladisch 		period_size = (period_size * runtime->channels) / 2;
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	spin_lock_irq(&cm->reg_lock);
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	/* set buffer address */
8171da177e4SLinus Torvalds 	reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
8181da177e4SLinus Torvalds 	snd_cmipci_write(cm, reg, rec->offset);
8191da177e4SLinus Torvalds 	/* program sample counts */
8201da177e4SLinus Torvalds 	reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
8211da177e4SLinus Torvalds 	snd_cmipci_write_w(cm, reg, rec->dma_size - 1);
822ebe9e289SClemens Ladisch 	snd_cmipci_write_w(cm, reg + 2, period_size - 1);
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	/* set adc/dac flag */
8251da177e4SLinus Torvalds 	val = rec->ch ? CM_CHADC1 : CM_CHADC0;
8261da177e4SLinus Torvalds 	if (rec->is_dac)
8271da177e4SLinus Torvalds 		cm->ctrl &= ~val;
8281da177e4SLinus Torvalds 	else
8291da177e4SLinus Torvalds 		cm->ctrl |= val;
8301da177e4SLinus Torvalds 	snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
8311da177e4SLinus Torvalds 	//snd_printd("cmipci: functrl0 = %08x\n", cm->ctrl);
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	/* set sample rate */
834755c48abSTimofei Bondarenko 	freq = 0;
835755c48abSTimofei Bondarenko 	freq_ext = 0;
836755c48abSTimofei Bondarenko 	if (runtime->rate > 48000)
837755c48abSTimofei Bondarenko 		switch (runtime->rate) {
838755c48abSTimofei Bondarenko 		case 88200:  freq_ext = CM_CH0_SRATE_88K; break;
839755c48abSTimofei Bondarenko 		case 96000:  freq_ext = CM_CH0_SRATE_96K; break;
840755c48abSTimofei Bondarenko 		case 128000: freq_ext = CM_CH0_SRATE_128K; break;
841755c48abSTimofei Bondarenko 		default:     snd_BUG(); break;
842755c48abSTimofei Bondarenko 		}
843755c48abSTimofei Bondarenko 	else
8441da177e4SLinus Torvalds 		freq = snd_cmipci_rate_freq(runtime->rate);
8451da177e4SLinus Torvalds 	val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
8461da177e4SLinus Torvalds 	if (rec->ch) {
8471da177e4SLinus Torvalds 		val &= ~CM_DSFC_MASK;
8481da177e4SLinus Torvalds 		val |= (freq << CM_DSFC_SHIFT) & CM_DSFC_MASK;
849a839a33dSClemens Ladisch 	} else {
850a839a33dSClemens Ladisch 		val &= ~CM_ASFC_MASK;
851a839a33dSClemens Ladisch 		val |= (freq << CM_ASFC_SHIFT) & CM_ASFC_MASK;
8521da177e4SLinus Torvalds 	}
8531da177e4SLinus Torvalds 	snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
8541da177e4SLinus Torvalds 	//snd_printd("cmipci: functrl1 = %08x\n", val);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	/* set format */
8571da177e4SLinus Torvalds 	val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
8581da177e4SLinus Torvalds 	if (rec->ch) {
8591da177e4SLinus Torvalds 		val &= ~CM_CH1FMT_MASK;
8601da177e4SLinus Torvalds 		val |= rec->fmt << CM_CH1FMT_SHIFT;
8611da177e4SLinus Torvalds 	} else {
8621da177e4SLinus Torvalds 		val &= ~CM_CH0FMT_MASK;
8631da177e4SLinus Torvalds 		val |= rec->fmt << CM_CH0FMT_SHIFT;
8641da177e4SLinus Torvalds 	}
865755c48abSTimofei Bondarenko 	if (cm->can_96k) {
866755c48abSTimofei Bondarenko 		val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
867755c48abSTimofei Bondarenko 		val |= freq_ext << (rec->ch * 2);
8688992e18dSClemens Ladisch 	}
8691da177e4SLinus Torvalds 	snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
8701da177e4SLinus Torvalds 	//snd_printd("cmipci: chformat = %08x\n", val);
8711da177e4SLinus Torvalds 
872*feb77712STimofei Bondarenko 	if (!rec->is_dac && cm->chip_version) {
873*feb77712STimofei Bondarenko 		if (runtime->rate > 44100)
874*feb77712STimofei Bondarenko 			snd_cmipci_set_bit(cm, CM_REG_EXT_MISC, CM_ADC48K44K);
875*feb77712STimofei Bondarenko 		else
876*feb77712STimofei Bondarenko 			snd_cmipci_clear_bit(cm, CM_REG_EXT_MISC, CM_ADC48K44K);
877*feb77712STimofei Bondarenko 	}
878*feb77712STimofei Bondarenko 
8791da177e4SLinus Torvalds 	rec->running = 0;
8801da177e4SLinus Torvalds 	spin_unlock_irq(&cm->reg_lock);
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	return 0;
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds /*
8861da177e4SLinus Torvalds  * PCM trigger/stop
8871da177e4SLinus Torvalds  */
8882cbdb686STakashi Iwai static int snd_cmipci_pcm_trigger(struct cmipci *cm, struct cmipci_pcm *rec,
889ebe9e289SClemens Ladisch 				  int cmd)
8901da177e4SLinus Torvalds {
8911da177e4SLinus Torvalds 	unsigned int inthld, chen, reset, pause;
8921da177e4SLinus Torvalds 	int result = 0;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	inthld = CM_CH0_INT_EN << rec->ch;
8951da177e4SLinus Torvalds 	chen = CM_CHEN0 << rec->ch;
8961da177e4SLinus Torvalds 	reset = CM_RST_CH0 << rec->ch;
8971da177e4SLinus Torvalds 	pause = CM_PAUSE0 << rec->ch;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	spin_lock(&cm->reg_lock);
9001da177e4SLinus Torvalds 	switch (cmd) {
9011da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
9021da177e4SLinus Torvalds 		rec->running = 1;
9031da177e4SLinus Torvalds 		/* set interrupt */
9041da177e4SLinus Torvalds 		snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, inthld);
9051da177e4SLinus Torvalds 		cm->ctrl |= chen;
9061da177e4SLinus Torvalds 		/* enable channel */
9071da177e4SLinus Torvalds 		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
9081da177e4SLinus Torvalds 		//snd_printd("cmipci: functrl0 = %08x\n", cm->ctrl);
9091da177e4SLinus Torvalds 		break;
9101da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
9111da177e4SLinus Torvalds 		rec->running = 0;
9121da177e4SLinus Torvalds 		/* disable interrupt */
9131da177e4SLinus Torvalds 		snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, inthld);
9141da177e4SLinus Torvalds 		/* reset */
9151da177e4SLinus Torvalds 		cm->ctrl &= ~chen;
9161da177e4SLinus Torvalds 		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
9171da177e4SLinus Torvalds 		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
918c36fd8c3SClemens Ladisch 		rec->needs_silencing = rec->is_dac;
9191da177e4SLinus Torvalds 		break;
9201da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
921cb60e5f5STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
9221da177e4SLinus Torvalds 		cm->ctrl |= pause;
9231da177e4SLinus Torvalds 		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
9241da177e4SLinus Torvalds 		break;
9251da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
926cb60e5f5STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
9271da177e4SLinus Torvalds 		cm->ctrl &= ~pause;
9281da177e4SLinus Torvalds 		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
9291da177e4SLinus Torvalds 		break;
9301da177e4SLinus Torvalds 	default:
9311da177e4SLinus Torvalds 		result = -EINVAL;
9321da177e4SLinus Torvalds 		break;
9331da177e4SLinus Torvalds 	}
9341da177e4SLinus Torvalds 	spin_unlock(&cm->reg_lock);
9351da177e4SLinus Torvalds 	return result;
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds /*
9391da177e4SLinus Torvalds  * return the current pointer
9401da177e4SLinus Torvalds  */
9412cbdb686STakashi Iwai static snd_pcm_uframes_t snd_cmipci_pcm_pointer(struct cmipci *cm, struct cmipci_pcm *rec,
9422cbdb686STakashi Iwai 						struct snd_pcm_substream *substream)
9431da177e4SLinus Torvalds {
9441da177e4SLinus Torvalds 	size_t ptr;
9451da177e4SLinus Torvalds 	unsigned int reg;
9461da177e4SLinus Torvalds 	if (!rec->running)
9471da177e4SLinus Torvalds 		return 0;
9481da177e4SLinus Torvalds #if 1 // this seems better..
9491da177e4SLinus Torvalds 	reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
9501da177e4SLinus Torvalds 	ptr = rec->dma_size - (snd_cmipci_read_w(cm, reg) + 1);
9511da177e4SLinus Torvalds 	ptr >>= rec->shift;
9521da177e4SLinus Torvalds #else
9531da177e4SLinus Torvalds 	reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
9541da177e4SLinus Torvalds 	ptr = snd_cmipci_read(cm, reg) - rec->offset;
9551da177e4SLinus Torvalds 	ptr = bytes_to_frames(substream->runtime, ptr);
9561da177e4SLinus Torvalds #endif
9571da177e4SLinus Torvalds 	if (substream->runtime->channels > 2)
9581da177e4SLinus Torvalds 		ptr = (ptr * 2) / substream->runtime->channels;
9591da177e4SLinus Torvalds 	return ptr;
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds /*
9631da177e4SLinus Torvalds  * playback
9641da177e4SLinus Torvalds  */
9651da177e4SLinus Torvalds 
9662cbdb686STakashi Iwai static int snd_cmipci_playback_trigger(struct snd_pcm_substream *substream,
9671da177e4SLinus Torvalds 				       int cmd)
9681da177e4SLinus Torvalds {
9692cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
970ebe9e289SClemens Ladisch 	return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], cmd);
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
9732cbdb686STakashi Iwai static snd_pcm_uframes_t snd_cmipci_playback_pointer(struct snd_pcm_substream *substream)
9741da177e4SLinus Torvalds {
9752cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
9761da177e4SLinus Torvalds 	return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_PLAY], substream);
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds /*
9821da177e4SLinus Torvalds  * capture
9831da177e4SLinus Torvalds  */
9841da177e4SLinus Torvalds 
9852cbdb686STakashi Iwai static int snd_cmipci_capture_trigger(struct snd_pcm_substream *substream,
9861da177e4SLinus Torvalds 				     int cmd)
9871da177e4SLinus Torvalds {
9882cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
989ebe9e289SClemens Ladisch 	return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], cmd);
9901da177e4SLinus Torvalds }
9911da177e4SLinus Torvalds 
9922cbdb686STakashi Iwai static snd_pcm_uframes_t snd_cmipci_capture_pointer(struct snd_pcm_substream *substream)
9931da177e4SLinus Torvalds {
9942cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
9951da177e4SLinus Torvalds 	return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_CAPT], substream);
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds /*
10001da177e4SLinus Torvalds  * hw preparation for spdif
10011da177e4SLinus Torvalds  */
10021da177e4SLinus Torvalds 
10032cbdb686STakashi Iwai static int snd_cmipci_spdif_default_info(struct snd_kcontrol *kcontrol,
10042cbdb686STakashi Iwai 					 struct snd_ctl_elem_info *uinfo)
10051da177e4SLinus Torvalds {
10061da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
10071da177e4SLinus Torvalds 	uinfo->count = 1;
10081da177e4SLinus Torvalds 	return 0;
10091da177e4SLinus Torvalds }
10101da177e4SLinus Torvalds 
10112cbdb686STakashi Iwai static int snd_cmipci_spdif_default_get(struct snd_kcontrol *kcontrol,
10122cbdb686STakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
10131da177e4SLinus Torvalds {
10142cbdb686STakashi Iwai 	struct cmipci *chip = snd_kcontrol_chip(kcontrol);
10151da177e4SLinus Torvalds 	int i;
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10181da177e4SLinus Torvalds 	for (i = 0; i < 4; i++)
10191da177e4SLinus Torvalds 		ucontrol->value.iec958.status[i] = (chip->dig_status >> (i * 8)) & 0xff;
10201da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10211da177e4SLinus Torvalds 	return 0;
10221da177e4SLinus Torvalds }
10231da177e4SLinus Torvalds 
10242cbdb686STakashi Iwai static int snd_cmipci_spdif_default_put(struct snd_kcontrol *kcontrol,
10252cbdb686STakashi Iwai 					 struct snd_ctl_elem_value *ucontrol)
10261da177e4SLinus Torvalds {
10272cbdb686STakashi Iwai 	struct cmipci *chip = snd_kcontrol_chip(kcontrol);
10281da177e4SLinus Torvalds 	int i, change;
10291da177e4SLinus Torvalds 	unsigned int val;
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	val = 0;
10321da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10331da177e4SLinus Torvalds 	for (i = 0; i < 4; i++)
10341da177e4SLinus Torvalds 		val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
10351da177e4SLinus Torvalds 	change = val != chip->dig_status;
10361da177e4SLinus Torvalds 	chip->dig_status = val;
10371da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10381da177e4SLinus Torvalds 	return change;
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds 
10412cbdb686STakashi Iwai static struct snd_kcontrol_new snd_cmipci_spdif_default __devinitdata =
10421da177e4SLinus Torvalds {
10431da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
10441da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
10451da177e4SLinus Torvalds 	.info =		snd_cmipci_spdif_default_info,
10461da177e4SLinus Torvalds 	.get =		snd_cmipci_spdif_default_get,
10471da177e4SLinus Torvalds 	.put =		snd_cmipci_spdif_default_put
10481da177e4SLinus Torvalds };
10491da177e4SLinus Torvalds 
10502cbdb686STakashi Iwai static int snd_cmipci_spdif_mask_info(struct snd_kcontrol *kcontrol,
10512cbdb686STakashi Iwai 				      struct snd_ctl_elem_info *uinfo)
10521da177e4SLinus Torvalds {
10531da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
10541da177e4SLinus Torvalds 	uinfo->count = 1;
10551da177e4SLinus Torvalds 	return 0;
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
10582cbdb686STakashi Iwai static int snd_cmipci_spdif_mask_get(struct snd_kcontrol *kcontrol,
10592cbdb686STakashi Iwai 				     struct snd_ctl_elem_value *ucontrol)
10601da177e4SLinus Torvalds {
10611da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = 0xff;
10621da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = 0xff;
10631da177e4SLinus Torvalds 	ucontrol->value.iec958.status[2] = 0xff;
10641da177e4SLinus Torvalds 	ucontrol->value.iec958.status[3] = 0xff;
10651da177e4SLinus Torvalds 	return 0;
10661da177e4SLinus Torvalds }
10671da177e4SLinus Torvalds 
10682cbdb686STakashi Iwai static struct snd_kcontrol_new snd_cmipci_spdif_mask __devinitdata =
10691da177e4SLinus Torvalds {
10701da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
107167ed4161SClemens Ladisch 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
10721da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
10731da177e4SLinus Torvalds 	.info =		snd_cmipci_spdif_mask_info,
10741da177e4SLinus Torvalds 	.get =		snd_cmipci_spdif_mask_get,
10751da177e4SLinus Torvalds };
10761da177e4SLinus Torvalds 
10772cbdb686STakashi Iwai static int snd_cmipci_spdif_stream_info(struct snd_kcontrol *kcontrol,
10782cbdb686STakashi Iwai 					struct snd_ctl_elem_info *uinfo)
10791da177e4SLinus Torvalds {
10801da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
10811da177e4SLinus Torvalds 	uinfo->count = 1;
10821da177e4SLinus Torvalds 	return 0;
10831da177e4SLinus Torvalds }
10841da177e4SLinus Torvalds 
10852cbdb686STakashi Iwai static int snd_cmipci_spdif_stream_get(struct snd_kcontrol *kcontrol,
10862cbdb686STakashi Iwai 				       struct snd_ctl_elem_value *ucontrol)
10871da177e4SLinus Torvalds {
10882cbdb686STakashi Iwai 	struct cmipci *chip = snd_kcontrol_chip(kcontrol);
10891da177e4SLinus Torvalds 	int i;
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10921da177e4SLinus Torvalds 	for (i = 0; i < 4; i++)
10931da177e4SLinus Torvalds 		ucontrol->value.iec958.status[i] = (chip->dig_pcm_status >> (i * 8)) & 0xff;
10941da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10951da177e4SLinus Torvalds 	return 0;
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds 
10982cbdb686STakashi Iwai static int snd_cmipci_spdif_stream_put(struct snd_kcontrol *kcontrol,
10992cbdb686STakashi Iwai 				       struct snd_ctl_elem_value *ucontrol)
11001da177e4SLinus Torvalds {
11012cbdb686STakashi Iwai 	struct cmipci *chip = snd_kcontrol_chip(kcontrol);
11021da177e4SLinus Torvalds 	int i, change;
11031da177e4SLinus Torvalds 	unsigned int val;
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 	val = 0;
11061da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
11071da177e4SLinus Torvalds 	for (i = 0; i < 4; i++)
11081da177e4SLinus Torvalds 		val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
11091da177e4SLinus Torvalds 	change = val != chip->dig_pcm_status;
11101da177e4SLinus Torvalds 	chip->dig_pcm_status = val;
11111da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
11121da177e4SLinus Torvalds 	return change;
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds 
11152cbdb686STakashi Iwai static struct snd_kcontrol_new snd_cmipci_spdif_stream __devinitdata =
11161da177e4SLinus Torvalds {
11171da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
11181da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
11191da177e4SLinus Torvalds 	.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
11201da177e4SLinus Torvalds 	.info =		snd_cmipci_spdif_stream_info,
11211da177e4SLinus Torvalds 	.get =		snd_cmipci_spdif_stream_get,
11221da177e4SLinus Torvalds 	.put =		snd_cmipci_spdif_stream_put
11231da177e4SLinus Torvalds };
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds /*
11261da177e4SLinus Torvalds  */
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds /* save mixer setting and mute for AC3 playback */
11292cbdb686STakashi Iwai static int save_mixer_state(struct cmipci *cm)
11301da177e4SLinus Torvalds {
11311da177e4SLinus Torvalds 	if (! cm->mixer_insensitive) {
11322cbdb686STakashi Iwai 		struct snd_ctl_elem_value *val;
11331da177e4SLinus Torvalds 		unsigned int i;
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 		val = kmalloc(sizeof(*val), GFP_ATOMIC);
11361da177e4SLinus Torvalds 		if (!val)
11371da177e4SLinus Torvalds 			return -ENOMEM;
11381da177e4SLinus Torvalds 		for (i = 0; i < CM_SAVED_MIXERS; i++) {
11392cbdb686STakashi Iwai 			struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
11401da177e4SLinus Torvalds 			if (ctl) {
11411da177e4SLinus Torvalds 				int event;
11421da177e4SLinus Torvalds 				memset(val, 0, sizeof(*val));
11431da177e4SLinus Torvalds 				ctl->get(ctl, val);
11441da177e4SLinus Torvalds 				cm->mixer_res_status[i] = val->value.integer.value[0];
11451da177e4SLinus Torvalds 				val->value.integer.value[0] = cm_saved_mixer[i].toggle_on;
11461da177e4SLinus Torvalds 				event = SNDRV_CTL_EVENT_MASK_INFO;
11471da177e4SLinus Torvalds 				if (cm->mixer_res_status[i] != val->value.integer.value[0]) {
11481da177e4SLinus Torvalds 					ctl->put(ctl, val); /* toggle */
11491da177e4SLinus Torvalds 					event |= SNDRV_CTL_EVENT_MASK_VALUE;
11501da177e4SLinus Torvalds 				}
11511da177e4SLinus Torvalds 				ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
11521da177e4SLinus Torvalds 				snd_ctl_notify(cm->card, event, &ctl->id);
11531da177e4SLinus Torvalds 			}
11541da177e4SLinus Torvalds 		}
11551da177e4SLinus Torvalds 		kfree(val);
11561da177e4SLinus Torvalds 		cm->mixer_insensitive = 1;
11571da177e4SLinus Torvalds 	}
11581da177e4SLinus Torvalds 	return 0;
11591da177e4SLinus Torvalds }
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds /* restore the previously saved mixer status */
11632cbdb686STakashi Iwai static void restore_mixer_state(struct cmipci *cm)
11641da177e4SLinus Torvalds {
11651da177e4SLinus Torvalds 	if (cm->mixer_insensitive) {
11662cbdb686STakashi Iwai 		struct snd_ctl_elem_value *val;
11671da177e4SLinus Torvalds 		unsigned int i;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 		val = kmalloc(sizeof(*val), GFP_KERNEL);
11701da177e4SLinus Torvalds 		if (!val)
11711da177e4SLinus Torvalds 			return;
11721da177e4SLinus Torvalds 		cm->mixer_insensitive = 0; /* at first clear this;
11731da177e4SLinus Torvalds 					      otherwise the changes will be ignored */
11741da177e4SLinus Torvalds 		for (i = 0; i < CM_SAVED_MIXERS; i++) {
11752cbdb686STakashi Iwai 			struct snd_kcontrol *ctl = cm->mixer_res_ctl[i];
11761da177e4SLinus Torvalds 			if (ctl) {
11771da177e4SLinus Torvalds 				int event;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 				memset(val, 0, sizeof(*val));
11801da177e4SLinus Torvalds 				ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
11811da177e4SLinus Torvalds 				ctl->get(ctl, val);
11821da177e4SLinus Torvalds 				event = SNDRV_CTL_EVENT_MASK_INFO;
11831da177e4SLinus Torvalds 				if (val->value.integer.value[0] != cm->mixer_res_status[i]) {
11841da177e4SLinus Torvalds 					val->value.integer.value[0] = cm->mixer_res_status[i];
11851da177e4SLinus Torvalds 					ctl->put(ctl, val);
11861da177e4SLinus Torvalds 					event |= SNDRV_CTL_EVENT_MASK_VALUE;
11871da177e4SLinus Torvalds 				}
11881da177e4SLinus Torvalds 				snd_ctl_notify(cm->card, event, &ctl->id);
11891da177e4SLinus Torvalds 			}
11901da177e4SLinus Torvalds 		}
11911da177e4SLinus Torvalds 		kfree(val);
11921da177e4SLinus Torvalds 	}
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds /* spinlock held! */
11962cbdb686STakashi Iwai static void setup_ac3(struct cmipci *cm, struct snd_pcm_substream *subs, int do_ac3, int rate)
11971da177e4SLinus Torvalds {
11981da177e4SLinus Torvalds 	if (do_ac3) {
11991da177e4SLinus Torvalds 		/* AC3EN for 037 */
12001da177e4SLinus Torvalds 		snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
12011da177e4SLinus Torvalds 		/* AC3EN for 039 */
12021da177e4SLinus Torvalds 		snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 		if (cm->can_ac3_hw) {
12051da177e4SLinus Torvalds 			/* SPD24SEL for 037, 0x02 */
12061da177e4SLinus Torvalds 			/* SPD24SEL for 039, 0x20, but cannot be set */
12071da177e4SLinus Torvalds 			snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
12081da177e4SLinus Torvalds 			snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
12091da177e4SLinus Torvalds 		} else { /* can_ac3_sw */
12101da177e4SLinus Torvalds 			/* SPD32SEL for 037 & 039, 0x20 */
12111da177e4SLinus Torvalds 			snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
12121da177e4SLinus Torvalds 			/* set 176K sample rate to fix 033 HW bug */
12131da177e4SLinus Torvalds 			if (cm->chip_version == 33) {
12141da177e4SLinus Torvalds 				if (rate >= 48000) {
12151da177e4SLinus Torvalds 					snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
12161da177e4SLinus Torvalds 				} else {
12171da177e4SLinus Torvalds 					snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
12181da177e4SLinus Torvalds 				}
12191da177e4SLinus Torvalds 			}
12201da177e4SLinus Torvalds 		}
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	} else {
12231da177e4SLinus Torvalds 		snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
12241da177e4SLinus Torvalds 		snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 		if (cm->can_ac3_hw) {
12271da177e4SLinus Torvalds 			/* chip model >= 37 */
12281da177e4SLinus Torvalds 			if (snd_pcm_format_width(subs->runtime->format) > 16) {
12291da177e4SLinus Torvalds 				snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
12301da177e4SLinus Torvalds 				snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
12311da177e4SLinus Torvalds 			} else {
12321da177e4SLinus Torvalds 				snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
12331da177e4SLinus Torvalds 				snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
12341da177e4SLinus Torvalds 			}
12351da177e4SLinus Torvalds 		} else {
12361da177e4SLinus Torvalds 			snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
12371da177e4SLinus Torvalds 			snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
12381da177e4SLinus Torvalds 			snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
12391da177e4SLinus Torvalds 		}
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
12432cbdb686STakashi Iwai static int setup_spdif_playback(struct cmipci *cm, struct snd_pcm_substream *subs, int up, int do_ac3)
12441da177e4SLinus Torvalds {
12451da177e4SLinus Torvalds 	int rate, err;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	rate = subs->runtime->rate;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	if (up && do_ac3)
12501da177e4SLinus Torvalds 		if ((err = save_mixer_state(cm)) < 0)
12511da177e4SLinus Torvalds 			return err;
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	spin_lock_irq(&cm->reg_lock);
12541da177e4SLinus Torvalds 	cm->spdif_playback_avail = up;
12551da177e4SLinus Torvalds 	if (up) {
12561da177e4SLinus Torvalds 		/* they are controlled via "IEC958 Output Switch" */
12571da177e4SLinus Torvalds 		/* snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
12581da177e4SLinus Torvalds 		/* snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
12591da177e4SLinus Torvalds 		if (cm->spdif_playback_enabled)
12601da177e4SLinus Torvalds 			snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
12611da177e4SLinus Torvalds 		setup_ac3(cm, subs, do_ac3, rate);
12621da177e4SLinus Torvalds 
12638992e18dSClemens Ladisch 		if (rate == 48000 || rate == 96000)
12641da177e4SLinus Torvalds 			snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
12651da177e4SLinus Torvalds 		else
12661da177e4SLinus Torvalds 			snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
12678992e18dSClemens Ladisch 		if (rate > 48000)
12688992e18dSClemens Ladisch 			snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
12698992e18dSClemens Ladisch 		else
12708992e18dSClemens Ladisch 			snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
12711da177e4SLinus Torvalds 	} else {
12721da177e4SLinus Torvalds 		/* they are controlled via "IEC958 Output Switch" */
12731da177e4SLinus Torvalds 		/* snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
12741da177e4SLinus Torvalds 		/* snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
12758992e18dSClemens Ladisch 		snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
12761da177e4SLinus Torvalds 		snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
12771da177e4SLinus Torvalds 		setup_ac3(cm, subs, 0, 0);
12781da177e4SLinus Torvalds 	}
12791da177e4SLinus Torvalds 	spin_unlock_irq(&cm->reg_lock);
12801da177e4SLinus Torvalds 	return 0;
12811da177e4SLinus Torvalds }
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds /*
12851da177e4SLinus Torvalds  * preparation
12861da177e4SLinus Torvalds  */
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds /* playback - enable spdif only on the certain condition */
12892cbdb686STakashi Iwai static int snd_cmipci_playback_prepare(struct snd_pcm_substream *substream)
12901da177e4SLinus Torvalds {
12912cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
12921da177e4SLinus Torvalds 	int rate = substream->runtime->rate;
12931da177e4SLinus Torvalds 	int err, do_spdif, do_ac3 = 0;
12941da177e4SLinus Torvalds 
1295755c48abSTimofei Bondarenko 	do_spdif = (rate >= 44100 && rate <= 96000 &&
12961da177e4SLinus Torvalds 		    substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE &&
12971da177e4SLinus Torvalds 		    substream->runtime->channels == 2);
12981da177e4SLinus Torvalds 	if (do_spdif && cm->can_ac3_hw)
12991da177e4SLinus Torvalds 		do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
13001da177e4SLinus Torvalds 	if ((err = setup_spdif_playback(cm, substream, do_spdif, do_ac3)) < 0)
13011da177e4SLinus Torvalds 		return err;
13021da177e4SLinus Torvalds 	return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds /* playback  (via device #2) - enable spdif always */
13062cbdb686STakashi Iwai static int snd_cmipci_playback_spdif_prepare(struct snd_pcm_substream *substream)
13071da177e4SLinus Torvalds {
13082cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
13091da177e4SLinus Torvalds 	int err, do_ac3;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	if (cm->can_ac3_hw)
13121da177e4SLinus Torvalds 		do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
13131da177e4SLinus Torvalds 	else
13141da177e4SLinus Torvalds 		do_ac3 = 1; /* doesn't matter */
13151da177e4SLinus Torvalds 	if ((err = setup_spdif_playback(cm, substream, 1, do_ac3)) < 0)
13161da177e4SLinus Torvalds 		return err;
13171da177e4SLinus Torvalds 	return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
1320c36fd8c3SClemens Ladisch /*
1321c36fd8c3SClemens Ladisch  * Apparently, the samples last played on channel A stay in some buffer, even
1322c36fd8c3SClemens Ladisch  * after the channel is reset, and get added to the data for the rear DACs when
1323c36fd8c3SClemens Ladisch  * playing a multichannel stream on channel B.  This is likely to generate
1324c36fd8c3SClemens Ladisch  * wraparounds and thus distortions.
1325c36fd8c3SClemens Ladisch  * To avoid this, we play at least one zero sample after the actual stream has
1326c36fd8c3SClemens Ladisch  * stopped.
1327c36fd8c3SClemens Ladisch  */
1328c36fd8c3SClemens Ladisch static void snd_cmipci_silence_hack(struct cmipci *cm, struct cmipci_pcm *rec)
1329c36fd8c3SClemens Ladisch {
1330c36fd8c3SClemens Ladisch 	struct snd_pcm_runtime *runtime = rec->substream->runtime;
1331c36fd8c3SClemens Ladisch 	unsigned int reg, val;
1332c36fd8c3SClemens Ladisch 
1333c36fd8c3SClemens Ladisch 	if (rec->needs_silencing && runtime && runtime->dma_area) {
1334c36fd8c3SClemens Ladisch 		/* set up a small silence buffer */
1335c36fd8c3SClemens Ladisch 		memset(runtime->dma_area, 0, PAGE_SIZE);
1336c36fd8c3SClemens Ladisch 		reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
1337c36fd8c3SClemens Ladisch 		val = ((PAGE_SIZE / 4) - 1) | (((PAGE_SIZE / 4) / 2 - 1) << 16);
1338c36fd8c3SClemens Ladisch 		snd_cmipci_write(cm, reg, val);
1339c36fd8c3SClemens Ladisch 
1340c36fd8c3SClemens Ladisch 		/* configure for 16 bits, 2 channels, 8 kHz */
1341c36fd8c3SClemens Ladisch 		if (runtime->channels > 2)
1342c36fd8c3SClemens Ladisch 			set_dac_channels(cm, rec, 2);
1343c36fd8c3SClemens Ladisch 		spin_lock_irq(&cm->reg_lock);
1344c36fd8c3SClemens Ladisch 		val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
1345c36fd8c3SClemens Ladisch 		val &= ~(CM_ASFC_MASK << (rec->ch * 3));
1346c36fd8c3SClemens Ladisch 		val |= (4 << CM_ASFC_SHIFT) << (rec->ch * 3);
1347c36fd8c3SClemens Ladisch 		snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
1348c36fd8c3SClemens Ladisch 		val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
1349c36fd8c3SClemens Ladisch 		val &= ~(CM_CH0FMT_MASK << (rec->ch * 2));
1350c36fd8c3SClemens Ladisch 		val |= (3 << CM_CH0FMT_SHIFT) << (rec->ch * 2);
1351755c48abSTimofei Bondarenko 		if (cm->can_96k)
1352755c48abSTimofei Bondarenko 			val &= ~(CM_CH0_SRATE_MASK << (rec->ch * 2));
1353c36fd8c3SClemens Ladisch 		snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
1354c36fd8c3SClemens Ladisch 
1355c36fd8c3SClemens Ladisch 		/* start stream (we don't need interrupts) */
1356c36fd8c3SClemens Ladisch 		cm->ctrl |= CM_CHEN0 << rec->ch;
1357c36fd8c3SClemens Ladisch 		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
1358c36fd8c3SClemens Ladisch 		spin_unlock_irq(&cm->reg_lock);
1359c36fd8c3SClemens Ladisch 
1360c36fd8c3SClemens Ladisch 		msleep(1);
1361c36fd8c3SClemens Ladisch 
1362c36fd8c3SClemens Ladisch 		/* stop and reset stream */
1363c36fd8c3SClemens Ladisch 		spin_lock_irq(&cm->reg_lock);
1364c36fd8c3SClemens Ladisch 		cm->ctrl &= ~(CM_CHEN0 << rec->ch);
1365c36fd8c3SClemens Ladisch 		val = CM_RST_CH0 << rec->ch;
1366c36fd8c3SClemens Ladisch 		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | val);
1367c36fd8c3SClemens Ladisch 		snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~val);
1368c36fd8c3SClemens Ladisch 		spin_unlock_irq(&cm->reg_lock);
1369c36fd8c3SClemens Ladisch 
1370c36fd8c3SClemens Ladisch 		rec->needs_silencing = 0;
1371c36fd8c3SClemens Ladisch 	}
1372c36fd8c3SClemens Ladisch }
1373c36fd8c3SClemens Ladisch 
13742cbdb686STakashi Iwai static int snd_cmipci_playback_hw_free(struct snd_pcm_substream *substream)
13751da177e4SLinus Torvalds {
13762cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
13771da177e4SLinus Torvalds 	setup_spdif_playback(cm, substream, 0, 0);
13781da177e4SLinus Torvalds 	restore_mixer_state(cm);
1379c36fd8c3SClemens Ladisch 	snd_cmipci_silence_hack(cm, &cm->channel[0]);
1380c36fd8c3SClemens Ladisch 	return snd_cmipci_hw_free(substream);
1381c36fd8c3SClemens Ladisch }
1382c36fd8c3SClemens Ladisch 
1383c36fd8c3SClemens Ladisch static int snd_cmipci_playback2_hw_free(struct snd_pcm_substream *substream)
1384c36fd8c3SClemens Ladisch {
1385c36fd8c3SClemens Ladisch 	struct cmipci *cm = snd_pcm_substream_chip(substream);
1386c36fd8c3SClemens Ladisch 	snd_cmipci_silence_hack(cm, &cm->channel[1]);
13871da177e4SLinus Torvalds 	return snd_cmipci_hw_free(substream);
13881da177e4SLinus Torvalds }
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds /* capture */
13912cbdb686STakashi Iwai static int snd_cmipci_capture_prepare(struct snd_pcm_substream *substream)
13921da177e4SLinus Torvalds {
13932cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
13941da177e4SLinus Torvalds 	return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
13951da177e4SLinus Torvalds }
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds /* capture with spdif (via device #2) */
13982cbdb686STakashi Iwai static int snd_cmipci_capture_spdif_prepare(struct snd_pcm_substream *substream)
13991da177e4SLinus Torvalds {
14002cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	spin_lock_irq(&cm->reg_lock);
14031da177e4SLinus Torvalds 	snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
1404755c48abSTimofei Bondarenko 	if (cm->can_96k) {
1405755c48abSTimofei Bondarenko 		if (substream->runtime->rate > 48000)
1406755c48abSTimofei Bondarenko 			snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1407755c48abSTimofei Bondarenko 		else
1408755c48abSTimofei Bondarenko 			snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_DBLSPDS);
1409755c48abSTimofei Bondarenko 	}
14101da177e4SLinus Torvalds 	spin_unlock_irq(&cm->reg_lock);
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
14152cbdb686STakashi Iwai static int snd_cmipci_capture_spdif_hw_free(struct snd_pcm_substream *subs)
14161da177e4SLinus Torvalds {
14172cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(subs);
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 	spin_lock_irq(&cm->reg_lock);
14201da177e4SLinus Torvalds 	snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
14211da177e4SLinus Torvalds 	spin_unlock_irq(&cm->reg_lock);
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 	return snd_cmipci_hw_free(subs);
14241da177e4SLinus Torvalds }
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds /*
14281da177e4SLinus Torvalds  * interrupt handler
14291da177e4SLinus Torvalds  */
14307d12e780SDavid Howells static irqreturn_t snd_cmipci_interrupt(int irq, void *dev_id)
14311da177e4SLinus Torvalds {
14322cbdb686STakashi Iwai 	struct cmipci *cm = dev_id;
14331da177e4SLinus Torvalds 	unsigned int status, mask = 0;
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	/* fastpath out, to ease interrupt sharing */
14361da177e4SLinus Torvalds 	status = snd_cmipci_read(cm, CM_REG_INT_STATUS);
14371da177e4SLinus Torvalds 	if (!(status & CM_INTR))
14381da177e4SLinus Torvalds 		return IRQ_NONE;
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	/* acknowledge interrupt */
14411da177e4SLinus Torvalds 	spin_lock(&cm->reg_lock);
14421da177e4SLinus Torvalds 	if (status & CM_CHINT0)
14431da177e4SLinus Torvalds 		mask |= CM_CH0_INT_EN;
14441da177e4SLinus Torvalds 	if (status & CM_CHINT1)
14451da177e4SLinus Torvalds 		mask |= CM_CH1_INT_EN;
14461da177e4SLinus Torvalds 	snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, mask);
14471da177e4SLinus Torvalds 	snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, mask);
14481da177e4SLinus Torvalds 	spin_unlock(&cm->reg_lock);
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 	if (cm->rmidi && (status & CM_UARTINT))
14517d12e780SDavid Howells 		snd_mpu401_uart_interrupt(irq, cm->rmidi->private_data);
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds 	if (cm->pcm) {
14541da177e4SLinus Torvalds 		if ((status & CM_CHINT0) && cm->channel[0].running)
14551da177e4SLinus Torvalds 			snd_pcm_period_elapsed(cm->channel[0].substream);
14561da177e4SLinus Torvalds 		if ((status & CM_CHINT1) && cm->channel[1].running)
14571da177e4SLinus Torvalds 			snd_pcm_period_elapsed(cm->channel[1].substream);
14581da177e4SLinus Torvalds 	}
14591da177e4SLinus Torvalds 	return IRQ_HANDLED;
14601da177e4SLinus Torvalds }
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds /*
14631da177e4SLinus Torvalds  * h/w infos
14641da177e4SLinus Torvalds  */
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds /* playback on channel A */
14672cbdb686STakashi Iwai static struct snd_pcm_hardware snd_cmipci_playback =
14681da177e4SLinus Torvalds {
14691da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
14701da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1471cb60e5f5STakashi Iwai 				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
14721da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
14731da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
14741da177e4SLinus Torvalds 	.rate_min =		5512,
14751da177e4SLinus Torvalds 	.rate_max =		48000,
14761da177e4SLinus Torvalds 	.channels_min =		1,
14771da177e4SLinus Torvalds 	.channels_max =		2,
14781da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
14791da177e4SLinus Torvalds 	.period_bytes_min =	64,
14801da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
14811da177e4SLinus Torvalds 	.periods_min =		2,
14821da177e4SLinus Torvalds 	.periods_max =		1024,
14831da177e4SLinus Torvalds 	.fifo_size =		0,
14841da177e4SLinus Torvalds };
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds /* capture on channel B */
14872cbdb686STakashi Iwai static struct snd_pcm_hardware snd_cmipci_capture =
14881da177e4SLinus Torvalds {
14891da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
14901da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1491cb60e5f5STakashi Iwai 				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
14921da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
14931da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
14941da177e4SLinus Torvalds 	.rate_min =		5512,
14951da177e4SLinus Torvalds 	.rate_max =		48000,
14961da177e4SLinus Torvalds 	.channels_min =		1,
14971da177e4SLinus Torvalds 	.channels_max =		2,
14981da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
14991da177e4SLinus Torvalds 	.period_bytes_min =	64,
15001da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
15011da177e4SLinus Torvalds 	.periods_min =		2,
15021da177e4SLinus Torvalds 	.periods_max =		1024,
15031da177e4SLinus Torvalds 	.fifo_size =		0,
15041da177e4SLinus Torvalds };
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds /* playback on channel B - stereo 16bit only? */
15072cbdb686STakashi Iwai static struct snd_pcm_hardware snd_cmipci_playback2 =
15081da177e4SLinus Torvalds {
15091da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
15101da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1511cb60e5f5STakashi Iwai 				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
15121da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
15131da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
15141da177e4SLinus Torvalds 	.rate_min =		5512,
15151da177e4SLinus Torvalds 	.rate_max =		48000,
15161da177e4SLinus Torvalds 	.channels_min =		2,
15171da177e4SLinus Torvalds 	.channels_max =		2,
15181da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
15191da177e4SLinus Torvalds 	.period_bytes_min =	64,
15201da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
15211da177e4SLinus Torvalds 	.periods_min =		2,
15221da177e4SLinus Torvalds 	.periods_max =		1024,
15231da177e4SLinus Torvalds 	.fifo_size =		0,
15241da177e4SLinus Torvalds };
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds /* spdif playback on channel A */
15272cbdb686STakashi Iwai static struct snd_pcm_hardware snd_cmipci_playback_spdif =
15281da177e4SLinus Torvalds {
15291da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
15301da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1531cb60e5f5STakashi Iwai 				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
15321da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
15331da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
15341da177e4SLinus Torvalds 	.rate_min =		44100,
15351da177e4SLinus Torvalds 	.rate_max =		48000,
15361da177e4SLinus Torvalds 	.channels_min =		2,
15371da177e4SLinus Torvalds 	.channels_max =		2,
15381da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
15391da177e4SLinus Torvalds 	.period_bytes_min =	64,
15401da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
15411da177e4SLinus Torvalds 	.periods_min =		2,
15421da177e4SLinus Torvalds 	.periods_max =		1024,
15431da177e4SLinus Torvalds 	.fifo_size =		0,
15441da177e4SLinus Torvalds };
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds /* spdif playback on channel A (32bit, IEC958 subframes) */
15472cbdb686STakashi Iwai static struct snd_pcm_hardware snd_cmipci_playback_iec958_subframe =
15481da177e4SLinus Torvalds {
15491da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
15501da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1551cb60e5f5STakashi Iwai 				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
15521da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
15531da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
15541da177e4SLinus Torvalds 	.rate_min =		44100,
15551da177e4SLinus Torvalds 	.rate_max =		48000,
15561da177e4SLinus Torvalds 	.channels_min =		2,
15571da177e4SLinus Torvalds 	.channels_max =		2,
15581da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
15591da177e4SLinus Torvalds 	.period_bytes_min =	64,
15601da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
15611da177e4SLinus Torvalds 	.periods_min =		2,
15621da177e4SLinus Torvalds 	.periods_max =		1024,
15631da177e4SLinus Torvalds 	.fifo_size =		0,
15641da177e4SLinus Torvalds };
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds /* spdif capture on channel B */
15672cbdb686STakashi Iwai static struct snd_pcm_hardware snd_cmipci_capture_spdif =
15681da177e4SLinus Torvalds {
15691da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
15701da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
1571cb60e5f5STakashi Iwai 				 SNDRV_PCM_INFO_RESUME | SNDRV_PCM_INFO_MMAP_VALID),
15721da177e4SLinus Torvalds 	.formats =	        SNDRV_PCM_FMTBIT_S16_LE,
15731da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
15741da177e4SLinus Torvalds 	.rate_min =		44100,
15751da177e4SLinus Torvalds 	.rate_max =		48000,
15761da177e4SLinus Torvalds 	.channels_min =		2,
15771da177e4SLinus Torvalds 	.channels_max =		2,
15781da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
15791da177e4SLinus Torvalds 	.period_bytes_min =	64,
15801da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
15811da177e4SLinus Torvalds 	.periods_min =		2,
15821da177e4SLinus Torvalds 	.periods_max =		1024,
15831da177e4SLinus Torvalds 	.fifo_size =		0,
15841da177e4SLinus Torvalds };
15851da177e4SLinus Torvalds 
1586755c48abSTimofei Bondarenko static unsigned int rate_constraints[] = { 5512, 8000, 11025, 16000, 22050,
1587755c48abSTimofei Bondarenko 			32000, 44100, 48000, 88200, 96000, 128000 };
1588755c48abSTimofei Bondarenko static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
1589755c48abSTimofei Bondarenko 		.count = ARRAY_SIZE(rate_constraints),
1590755c48abSTimofei Bondarenko 		.list = rate_constraints,
1591755c48abSTimofei Bondarenko 		.mask = 0,
1592755c48abSTimofei Bondarenko };
1593755c48abSTimofei Bondarenko 
15941da177e4SLinus Torvalds /*
15951da177e4SLinus Torvalds  * check device open/close
15961da177e4SLinus Torvalds  */
15972cbdb686STakashi Iwai static int open_device_check(struct cmipci *cm, int mode, struct snd_pcm_substream *subs)
15981da177e4SLinus Torvalds {
15991da177e4SLinus Torvalds 	int ch = mode & CM_OPEN_CH_MASK;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	/* FIXME: a file should wait until the device becomes free
16021da177e4SLinus Torvalds 	 * when it's opened on blocking mode.  however, since the current
16031da177e4SLinus Torvalds 	 * pcm framework doesn't pass file pointer before actually opened,
16041da177e4SLinus Torvalds 	 * we can't know whether blocking mode or not in open callback..
16051da177e4SLinus Torvalds 	 */
160662932df8SIngo Molnar 	mutex_lock(&cm->open_mutex);
16071da177e4SLinus Torvalds 	if (cm->opened[ch]) {
160862932df8SIngo Molnar 		mutex_unlock(&cm->open_mutex);
16091da177e4SLinus Torvalds 		return -EBUSY;
16101da177e4SLinus Torvalds 	}
16111da177e4SLinus Torvalds 	cm->opened[ch] = mode;
16121da177e4SLinus Torvalds 	cm->channel[ch].substream = subs;
16131da177e4SLinus Torvalds 	if (! (mode & CM_OPEN_DAC)) {
16141da177e4SLinus Torvalds 		/* disable dual DAC mode */
16151da177e4SLinus Torvalds 		cm->channel[ch].is_dac = 0;
16161da177e4SLinus Torvalds 		spin_lock_irq(&cm->reg_lock);
16171da177e4SLinus Torvalds 		snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
16181da177e4SLinus Torvalds 		spin_unlock_irq(&cm->reg_lock);
16191da177e4SLinus Torvalds 	}
162062932df8SIngo Molnar 	mutex_unlock(&cm->open_mutex);
16211da177e4SLinus Torvalds 	return 0;
16221da177e4SLinus Torvalds }
16231da177e4SLinus Torvalds 
16242cbdb686STakashi Iwai static void close_device_check(struct cmipci *cm, int mode)
16251da177e4SLinus Torvalds {
16261da177e4SLinus Torvalds 	int ch = mode & CM_OPEN_CH_MASK;
16271da177e4SLinus Torvalds 
162862932df8SIngo Molnar 	mutex_lock(&cm->open_mutex);
16291da177e4SLinus Torvalds 	if (cm->opened[ch] == mode) {
16301da177e4SLinus Torvalds 		if (cm->channel[ch].substream) {
16311da177e4SLinus Torvalds 			snd_cmipci_ch_reset(cm, ch);
16321da177e4SLinus Torvalds 			cm->channel[ch].running = 0;
16331da177e4SLinus Torvalds 			cm->channel[ch].substream = NULL;
16341da177e4SLinus Torvalds 		}
16351da177e4SLinus Torvalds 		cm->opened[ch] = 0;
16361da177e4SLinus Torvalds 		if (! cm->channel[ch].is_dac) {
16371da177e4SLinus Torvalds 			/* enable dual DAC mode again */
16381da177e4SLinus Torvalds 			cm->channel[ch].is_dac = 1;
16391da177e4SLinus Torvalds 			spin_lock_irq(&cm->reg_lock);
16401da177e4SLinus Torvalds 			snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
16411da177e4SLinus Torvalds 			spin_unlock_irq(&cm->reg_lock);
16421da177e4SLinus Torvalds 		}
16431da177e4SLinus Torvalds 	}
164462932df8SIngo Molnar 	mutex_unlock(&cm->open_mutex);
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds /*
16481da177e4SLinus Torvalds  */
16491da177e4SLinus Torvalds 
16502cbdb686STakashi Iwai static int snd_cmipci_playback_open(struct snd_pcm_substream *substream)
16511da177e4SLinus Torvalds {
16522cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
16532cbdb686STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
16541da177e4SLinus Torvalds 	int err;
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 	if ((err = open_device_check(cm, CM_OPEN_PLAYBACK, substream)) < 0)
16571da177e4SLinus Torvalds 		return err;
16581da177e4SLinus Torvalds 	runtime->hw = snd_cmipci_playback;
16598992e18dSClemens Ladisch 	if (cm->chip_version == 68) {
16608992e18dSClemens Ladisch 		runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
16618992e18dSClemens Ladisch 				     SNDRV_PCM_RATE_96000;
16628992e18dSClemens Ladisch 		runtime->hw.rate_max = 96000;
1663755c48abSTimofei Bondarenko 	} else if (cm->chip_version == 55) {
1664755c48abSTimofei Bondarenko 		err = snd_pcm_hw_constraint_list(runtime, 0,
1665755c48abSTimofei Bondarenko 			SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1666755c48abSTimofei Bondarenko 		if (err < 0)
1667755c48abSTimofei Bondarenko 			return err;
1668755c48abSTimofei Bondarenko 		runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
1669755c48abSTimofei Bondarenko 		runtime->hw.rate_max = 128000;
16708992e18dSClemens Ladisch 	}
16711da177e4SLinus Torvalds 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
16721da177e4SLinus Torvalds 	cm->dig_pcm_status = cm->dig_status;
16731da177e4SLinus Torvalds 	return 0;
16741da177e4SLinus Torvalds }
16751da177e4SLinus Torvalds 
16762cbdb686STakashi Iwai static int snd_cmipci_capture_open(struct snd_pcm_substream *substream)
16771da177e4SLinus Torvalds {
16782cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
16792cbdb686STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
16801da177e4SLinus Torvalds 	int err;
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 	if ((err = open_device_check(cm, CM_OPEN_CAPTURE, substream)) < 0)
16831da177e4SLinus Torvalds 		return err;
16841da177e4SLinus Torvalds 	runtime->hw = snd_cmipci_capture;
16851da177e4SLinus Torvalds 	if (cm->chip_version == 68) {	// 8768 only supports 44k/48k recording
16861da177e4SLinus Torvalds 		runtime->hw.rate_min = 41000;
16871da177e4SLinus Torvalds 		runtime->hw.rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
1688755c48abSTimofei Bondarenko 	} else if (cm->chip_version == 55) {
1689755c48abSTimofei Bondarenko 		err = snd_pcm_hw_constraint_list(runtime, 0,
1690755c48abSTimofei Bondarenko 			SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1691755c48abSTimofei Bondarenko 		if (err < 0)
1692755c48abSTimofei Bondarenko 			return err;
1693755c48abSTimofei Bondarenko 		runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
1694755c48abSTimofei Bondarenko 		runtime->hw.rate_max = 128000;
16951da177e4SLinus Torvalds 	}
16961da177e4SLinus Torvalds 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
16971da177e4SLinus Torvalds 	return 0;
16981da177e4SLinus Torvalds }
16991da177e4SLinus Torvalds 
17002cbdb686STakashi Iwai static int snd_cmipci_playback2_open(struct snd_pcm_substream *substream)
17011da177e4SLinus Torvalds {
17022cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
17032cbdb686STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
17041da177e4SLinus Torvalds 	int err;
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds 	if ((err = open_device_check(cm, CM_OPEN_PLAYBACK2, substream)) < 0) /* use channel B */
17071da177e4SLinus Torvalds 		return err;
17081da177e4SLinus Torvalds 	runtime->hw = snd_cmipci_playback2;
170962932df8SIngo Molnar 	mutex_lock(&cm->open_mutex);
17101da177e4SLinus Torvalds 	if (! cm->opened[CM_CH_PLAY]) {
17111da177e4SLinus Torvalds 		if (cm->can_multi_ch) {
17121da177e4SLinus Torvalds 			runtime->hw.channels_max = cm->max_channels;
17131da177e4SLinus Torvalds 			if (cm->max_channels == 4)
17141da177e4SLinus Torvalds 				snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_4);
17151da177e4SLinus Torvalds 			else if (cm->max_channels == 6)
17161da177e4SLinus Torvalds 				snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_6);
17171da177e4SLinus Torvalds 			else if (cm->max_channels == 8)
17181da177e4SLinus Torvalds 				snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_8);
17191da177e4SLinus Torvalds 		}
172022a22f5aSClemens Ladisch 	}
172122a22f5aSClemens Ladisch 	mutex_unlock(&cm->open_mutex);
17228992e18dSClemens Ladisch 	if (cm->chip_version == 68) {
17238992e18dSClemens Ladisch 		runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
17248992e18dSClemens Ladisch 				     SNDRV_PCM_RATE_96000;
17258992e18dSClemens Ladisch 		runtime->hw.rate_max = 96000;
1726755c48abSTimofei Bondarenko 	} else if (cm->chip_version == 55) {
1727755c48abSTimofei Bondarenko 		err = snd_pcm_hw_constraint_list(runtime, 0,
1728755c48abSTimofei Bondarenko 			SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1729755c48abSTimofei Bondarenko 		if (err < 0)
1730755c48abSTimofei Bondarenko 			return err;
1731755c48abSTimofei Bondarenko 		runtime->hw.rates |= SNDRV_PCM_RATE_KNOT;
1732755c48abSTimofei Bondarenko 		runtime->hw.rate_max = 128000;
17338992e18dSClemens Ladisch 	}
17341da177e4SLinus Torvalds 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
17351da177e4SLinus Torvalds 	return 0;
17361da177e4SLinus Torvalds }
17371da177e4SLinus Torvalds 
17382cbdb686STakashi Iwai static int snd_cmipci_playback_spdif_open(struct snd_pcm_substream *substream)
17391da177e4SLinus Torvalds {
17402cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
17412cbdb686STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
17421da177e4SLinus Torvalds 	int err;
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	if ((err = open_device_check(cm, CM_OPEN_SPDIF_PLAYBACK, substream)) < 0) /* use channel A */
17451da177e4SLinus Torvalds 		return err;
17461da177e4SLinus Torvalds 	if (cm->can_ac3_hw) {
17471da177e4SLinus Torvalds 		runtime->hw = snd_cmipci_playback_spdif;
174857bd68b8SClemens Ladisch 		if (cm->chip_version >= 37) {
17491da177e4SLinus Torvalds 			runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
175057bd68b8SClemens Ladisch 			snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
175157bd68b8SClemens Ladisch 		}
1752755c48abSTimofei Bondarenko 		if (cm->can_96k) {
17538992e18dSClemens Ladisch 			runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
17548992e18dSClemens Ladisch 					     SNDRV_PCM_RATE_96000;
17558992e18dSClemens Ladisch 			runtime->hw.rate_max = 96000;
17568992e18dSClemens Ladisch 		}
17571da177e4SLinus Torvalds 	} else {
17581da177e4SLinus Torvalds 		runtime->hw = snd_cmipci_playback_iec958_subframe;
17591da177e4SLinus Torvalds 	}
17601da177e4SLinus Torvalds 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
17611da177e4SLinus Torvalds 	cm->dig_pcm_status = cm->dig_status;
17621da177e4SLinus Torvalds 	return 0;
17631da177e4SLinus Torvalds }
17641da177e4SLinus Torvalds 
17652cbdb686STakashi Iwai static int snd_cmipci_capture_spdif_open(struct snd_pcm_substream *substream)
17661da177e4SLinus Torvalds {
17672cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
17682cbdb686STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
17691da177e4SLinus Torvalds 	int err;
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	if ((err = open_device_check(cm, CM_OPEN_SPDIF_CAPTURE, substream)) < 0) /* use channel B */
17721da177e4SLinus Torvalds 		return err;
17731da177e4SLinus Torvalds 	runtime->hw = snd_cmipci_capture_spdif;
1774755c48abSTimofei Bondarenko 	if (cm->can_96k && !(cm->chip_version == 68)) {
1775755c48abSTimofei Bondarenko 		runtime->hw.rates |= SNDRV_PCM_RATE_88200 |
1776755c48abSTimofei Bondarenko 				     SNDRV_PCM_RATE_96000;
1777755c48abSTimofei Bondarenko 		runtime->hw.rate_max = 96000;
1778755c48abSTimofei Bondarenko 	}
17791da177e4SLinus Torvalds 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
17801da177e4SLinus Torvalds 	return 0;
17811da177e4SLinus Torvalds }
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds /*
17851da177e4SLinus Torvalds  */
17861da177e4SLinus Torvalds 
17872cbdb686STakashi Iwai static int snd_cmipci_playback_close(struct snd_pcm_substream *substream)
17881da177e4SLinus Torvalds {
17892cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
17901da177e4SLinus Torvalds 	close_device_check(cm, CM_OPEN_PLAYBACK);
17911da177e4SLinus Torvalds 	return 0;
17921da177e4SLinus Torvalds }
17931da177e4SLinus Torvalds 
17942cbdb686STakashi Iwai static int snd_cmipci_capture_close(struct snd_pcm_substream *substream)
17951da177e4SLinus Torvalds {
17962cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
17971da177e4SLinus Torvalds 	close_device_check(cm, CM_OPEN_CAPTURE);
17981da177e4SLinus Torvalds 	return 0;
17991da177e4SLinus Torvalds }
18001da177e4SLinus Torvalds 
18012cbdb686STakashi Iwai static int snd_cmipci_playback2_close(struct snd_pcm_substream *substream)
18021da177e4SLinus Torvalds {
18032cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
18041da177e4SLinus Torvalds 	close_device_check(cm, CM_OPEN_PLAYBACK2);
18051da177e4SLinus Torvalds 	close_device_check(cm, CM_OPEN_PLAYBACK_MULTI);
18061da177e4SLinus Torvalds 	return 0;
18071da177e4SLinus Torvalds }
18081da177e4SLinus Torvalds 
18092cbdb686STakashi Iwai static int snd_cmipci_playback_spdif_close(struct snd_pcm_substream *substream)
18101da177e4SLinus Torvalds {
18112cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
18121da177e4SLinus Torvalds 	close_device_check(cm, CM_OPEN_SPDIF_PLAYBACK);
18131da177e4SLinus Torvalds 	return 0;
18141da177e4SLinus Torvalds }
18151da177e4SLinus Torvalds 
18162cbdb686STakashi Iwai static int snd_cmipci_capture_spdif_close(struct snd_pcm_substream *substream)
18171da177e4SLinus Torvalds {
18182cbdb686STakashi Iwai 	struct cmipci *cm = snd_pcm_substream_chip(substream);
18191da177e4SLinus Torvalds 	close_device_check(cm, CM_OPEN_SPDIF_CAPTURE);
18201da177e4SLinus Torvalds 	return 0;
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds /*
18251da177e4SLinus Torvalds  */
18261da177e4SLinus Torvalds 
18272cbdb686STakashi Iwai static struct snd_pcm_ops snd_cmipci_playback_ops = {
18281da177e4SLinus Torvalds 	.open =		snd_cmipci_playback_open,
18291da177e4SLinus Torvalds 	.close =	snd_cmipci_playback_close,
18301da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
18311da177e4SLinus Torvalds 	.hw_params =	snd_cmipci_hw_params,
18321da177e4SLinus Torvalds 	.hw_free =	snd_cmipci_playback_hw_free,
18331da177e4SLinus Torvalds 	.prepare =	snd_cmipci_playback_prepare,
18341da177e4SLinus Torvalds 	.trigger =	snd_cmipci_playback_trigger,
18351da177e4SLinus Torvalds 	.pointer =	snd_cmipci_playback_pointer,
18361da177e4SLinus Torvalds };
18371da177e4SLinus Torvalds 
18382cbdb686STakashi Iwai static struct snd_pcm_ops snd_cmipci_capture_ops = {
18391da177e4SLinus Torvalds 	.open =		snd_cmipci_capture_open,
18401da177e4SLinus Torvalds 	.close =	snd_cmipci_capture_close,
18411da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
18421da177e4SLinus Torvalds 	.hw_params =	snd_cmipci_hw_params,
18431da177e4SLinus Torvalds 	.hw_free =	snd_cmipci_hw_free,
18441da177e4SLinus Torvalds 	.prepare =	snd_cmipci_capture_prepare,
18451da177e4SLinus Torvalds 	.trigger =	snd_cmipci_capture_trigger,
18461da177e4SLinus Torvalds 	.pointer =	snd_cmipci_capture_pointer,
18471da177e4SLinus Torvalds };
18481da177e4SLinus Torvalds 
18492cbdb686STakashi Iwai static struct snd_pcm_ops snd_cmipci_playback2_ops = {
18501da177e4SLinus Torvalds 	.open =		snd_cmipci_playback2_open,
18511da177e4SLinus Torvalds 	.close =	snd_cmipci_playback2_close,
18521da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
18531da177e4SLinus Torvalds 	.hw_params =	snd_cmipci_playback2_hw_params,
1854c36fd8c3SClemens Ladisch 	.hw_free =	snd_cmipci_playback2_hw_free,
18551da177e4SLinus Torvalds 	.prepare =	snd_cmipci_capture_prepare,	/* channel B */
18561da177e4SLinus Torvalds 	.trigger =	snd_cmipci_capture_trigger,	/* channel B */
18571da177e4SLinus Torvalds 	.pointer =	snd_cmipci_capture_pointer,	/* channel B */
18581da177e4SLinus Torvalds };
18591da177e4SLinus Torvalds 
18602cbdb686STakashi Iwai static struct snd_pcm_ops snd_cmipci_playback_spdif_ops = {
18611da177e4SLinus Torvalds 	.open =		snd_cmipci_playback_spdif_open,
18621da177e4SLinus Torvalds 	.close =	snd_cmipci_playback_spdif_close,
18631da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
18641da177e4SLinus Torvalds 	.hw_params =	snd_cmipci_hw_params,
18651da177e4SLinus Torvalds 	.hw_free =	snd_cmipci_playback_hw_free,
18661da177e4SLinus Torvalds 	.prepare =	snd_cmipci_playback_spdif_prepare,	/* set up rate */
18671da177e4SLinus Torvalds 	.trigger =	snd_cmipci_playback_trigger,
18681da177e4SLinus Torvalds 	.pointer =	snd_cmipci_playback_pointer,
18691da177e4SLinus Torvalds };
18701da177e4SLinus Torvalds 
18712cbdb686STakashi Iwai static struct snd_pcm_ops snd_cmipci_capture_spdif_ops = {
18721da177e4SLinus Torvalds 	.open =		snd_cmipci_capture_spdif_open,
18731da177e4SLinus Torvalds 	.close =	snd_cmipci_capture_spdif_close,
18741da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
18751da177e4SLinus Torvalds 	.hw_params =	snd_cmipci_hw_params,
18761da177e4SLinus Torvalds 	.hw_free =	snd_cmipci_capture_spdif_hw_free,
18771da177e4SLinus Torvalds 	.prepare =	snd_cmipci_capture_spdif_prepare,
18781da177e4SLinus Torvalds 	.trigger =	snd_cmipci_capture_trigger,
18791da177e4SLinus Torvalds 	.pointer =	snd_cmipci_capture_pointer,
18801da177e4SLinus Torvalds };
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds /*
18841da177e4SLinus Torvalds  */
18851da177e4SLinus Torvalds 
18862cbdb686STakashi Iwai static int __devinit snd_cmipci_pcm_new(struct cmipci *cm, int device)
18871da177e4SLinus Torvalds {
18882cbdb686STakashi Iwai 	struct snd_pcm *pcm;
18891da177e4SLinus Torvalds 	int err;
18901da177e4SLinus Torvalds 
18911da177e4SLinus Torvalds 	err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
18921da177e4SLinus Torvalds 	if (err < 0)
18931da177e4SLinus Torvalds 		return err;
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_ops);
18961da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_ops);
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds 	pcm->private_data = cm;
18991da177e4SLinus Torvalds 	pcm->info_flags = 0;
19001da177e4SLinus Torvalds 	strcpy(pcm->name, "C-Media PCI DAC/ADC");
19011da177e4SLinus Torvalds 	cm->pcm = pcm;
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
19041da177e4SLinus Torvalds 					      snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	return 0;
19071da177e4SLinus Torvalds }
19081da177e4SLinus Torvalds 
19092cbdb686STakashi Iwai static int __devinit snd_cmipci_pcm2_new(struct cmipci *cm, int device)
19101da177e4SLinus Torvalds {
19112cbdb686STakashi Iwai 	struct snd_pcm *pcm;
19121da177e4SLinus Torvalds 	int err;
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 	err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 0, &pcm);
19151da177e4SLinus Torvalds 	if (err < 0)
19161da177e4SLinus Torvalds 		return err;
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback2_ops);
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds 	pcm->private_data = cm;
19211da177e4SLinus Torvalds 	pcm->info_flags = 0;
19221da177e4SLinus Torvalds 	strcpy(pcm->name, "C-Media PCI 2nd DAC");
19231da177e4SLinus Torvalds 	cm->pcm2 = pcm;
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
19261da177e4SLinus Torvalds 					      snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds 	return 0;
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds 
19312cbdb686STakashi Iwai static int __devinit snd_cmipci_pcm_spdif_new(struct cmipci *cm, int device)
19321da177e4SLinus Torvalds {
19332cbdb686STakashi Iwai 	struct snd_pcm *pcm;
19341da177e4SLinus Torvalds 	int err;
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds 	err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
19371da177e4SLinus Torvalds 	if (err < 0)
19381da177e4SLinus Torvalds 		return err;
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_spdif_ops);
19411da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_spdif_ops);
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds 	pcm->private_data = cm;
19441da177e4SLinus Torvalds 	pcm->info_flags = 0;
19451da177e4SLinus Torvalds 	strcpy(pcm->name, "C-Media PCI IEC958");
19461da177e4SLinus Torvalds 	cm->pcm_spdif = pcm;
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
19491da177e4SLinus Torvalds 					      snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	return 0;
19521da177e4SLinus Torvalds }
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds /*
19551da177e4SLinus Torvalds  * mixer interface:
19561da177e4SLinus Torvalds  * - CM8338/8738 has a compatible mixer interface with SB16, but
19571da177e4SLinus Torvalds  *   lack of some elements like tone control, i/o gain and AGC.
19581da177e4SLinus Torvalds  * - Access to native registers:
19591da177e4SLinus Torvalds  *   - A 3D switch
19601da177e4SLinus Torvalds  *   - Output mute switches
19611da177e4SLinus Torvalds  */
19621da177e4SLinus Torvalds 
19632cbdb686STakashi Iwai static void snd_cmipci_mixer_write(struct cmipci *s, unsigned char idx, unsigned char data)
19641da177e4SLinus Torvalds {
19651da177e4SLinus Torvalds 	outb(idx, s->iobase + CM_REG_SB16_ADDR);
19661da177e4SLinus Torvalds 	outb(data, s->iobase + CM_REG_SB16_DATA);
19671da177e4SLinus Torvalds }
19681da177e4SLinus Torvalds 
19692cbdb686STakashi Iwai static unsigned char snd_cmipci_mixer_read(struct cmipci *s, unsigned char idx)
19701da177e4SLinus Torvalds {
19711da177e4SLinus Torvalds 	unsigned char v;
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 	outb(idx, s->iobase + CM_REG_SB16_ADDR);
19741da177e4SLinus Torvalds 	v = inb(s->iobase + CM_REG_SB16_DATA);
19751da177e4SLinus Torvalds 	return v;
19761da177e4SLinus Torvalds }
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds /*
19791da177e4SLinus Torvalds  * general mixer element
19801da177e4SLinus Torvalds  */
19812cbdb686STakashi Iwai struct cmipci_sb_reg {
19821da177e4SLinus Torvalds 	unsigned int left_reg, right_reg;
19831da177e4SLinus Torvalds 	unsigned int left_shift, right_shift;
19841da177e4SLinus Torvalds 	unsigned int mask;
19851da177e4SLinus Torvalds 	unsigned int invert: 1;
19861da177e4SLinus Torvalds 	unsigned int stereo: 1;
19872cbdb686STakashi Iwai };
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds #define COMPOSE_SB_REG(lreg,rreg,lshift,rshift,mask,invert,stereo) \
19901da177e4SLinus Torvalds  ((lreg) | ((rreg) << 8) | (lshift << 16) | (rshift << 19) | (mask << 24) | (invert << 22) | (stereo << 23))
19911da177e4SLinus Torvalds 
19921da177e4SLinus Torvalds #define CMIPCI_DOUBLE(xname, left_reg, right_reg, left_shift, right_shift, mask, invert, stereo) \
19931da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
19941da177e4SLinus Torvalds   .info = snd_cmipci_info_volume, \
19951da177e4SLinus Torvalds   .get = snd_cmipci_get_volume, .put = snd_cmipci_put_volume, \
19961da177e4SLinus Torvalds   .private_value = COMPOSE_SB_REG(left_reg, right_reg, left_shift, right_shift, mask, invert, stereo), \
19971da177e4SLinus Torvalds }
19981da177e4SLinus Torvalds 
19991da177e4SLinus Torvalds #define CMIPCI_SB_VOL_STEREO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg+1, shift, shift, mask, 0, 1)
20001da177e4SLinus Torvalds #define CMIPCI_SB_VOL_MONO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg, shift, shift, mask, 0, 0)
20011da177e4SLinus Torvalds #define CMIPCI_SB_SW_STEREO(xname,lshift,rshift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, lshift, rshift, 1, 0, 1)
20021da177e4SLinus Torvalds #define CMIPCI_SB_SW_MONO(xname,shift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, shift, shift, 1, 0, 0)
20031da177e4SLinus Torvalds 
20042cbdb686STakashi Iwai static void cmipci_sb_reg_decode(struct cmipci_sb_reg *r, unsigned long val)
20051da177e4SLinus Torvalds {
20061da177e4SLinus Torvalds 	r->left_reg = val & 0xff;
20071da177e4SLinus Torvalds 	r->right_reg = (val >> 8) & 0xff;
20081da177e4SLinus Torvalds 	r->left_shift = (val >> 16) & 0x07;
20091da177e4SLinus Torvalds 	r->right_shift = (val >> 19) & 0x07;
20101da177e4SLinus Torvalds 	r->invert = (val >> 22) & 1;
20111da177e4SLinus Torvalds 	r->stereo = (val >> 23) & 1;
20121da177e4SLinus Torvalds 	r->mask = (val >> 24) & 0xff;
20131da177e4SLinus Torvalds }
20141da177e4SLinus Torvalds 
20152cbdb686STakashi Iwai static int snd_cmipci_info_volume(struct snd_kcontrol *kcontrol,
20162cbdb686STakashi Iwai 				  struct snd_ctl_elem_info *uinfo)
20171da177e4SLinus Torvalds {
20182cbdb686STakashi Iwai 	struct cmipci_sb_reg reg;
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
20211da177e4SLinus Torvalds 	uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
20221da177e4SLinus Torvalds 	uinfo->count = reg.stereo + 1;
20231da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
20241da177e4SLinus Torvalds 	uinfo->value.integer.max = reg.mask;
20251da177e4SLinus Torvalds 	return 0;
20261da177e4SLinus Torvalds }
20271da177e4SLinus Torvalds 
20282cbdb686STakashi Iwai static int snd_cmipci_get_volume(struct snd_kcontrol *kcontrol,
20292cbdb686STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
20301da177e4SLinus Torvalds {
20312cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
20322cbdb686STakashi Iwai 	struct cmipci_sb_reg reg;
20331da177e4SLinus Torvalds 	int val;
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds 	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
20361da177e4SLinus Torvalds 	spin_lock_irq(&cm->reg_lock);
20371da177e4SLinus Torvalds 	val = (snd_cmipci_mixer_read(cm, reg.left_reg) >> reg.left_shift) & reg.mask;
20381da177e4SLinus Torvalds 	if (reg.invert)
20391da177e4SLinus Torvalds 		val = reg.mask - val;
20401da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = val;
20411da177e4SLinus Torvalds 	if (reg.stereo) {
20421da177e4SLinus Torvalds 		val = (snd_cmipci_mixer_read(cm, reg.right_reg) >> reg.right_shift) & reg.mask;
20431da177e4SLinus Torvalds 		if (reg.invert)
20441da177e4SLinus Torvalds 			val = reg.mask - val;
20451da177e4SLinus Torvalds 		 ucontrol->value.integer.value[1] = val;
20461da177e4SLinus Torvalds 	}
20471da177e4SLinus Torvalds 	spin_unlock_irq(&cm->reg_lock);
20481da177e4SLinus Torvalds 	return 0;
20491da177e4SLinus Torvalds }
20501da177e4SLinus Torvalds 
20512cbdb686STakashi Iwai static int snd_cmipci_put_volume(struct snd_kcontrol *kcontrol,
20522cbdb686STakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
20531da177e4SLinus Torvalds {
20542cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
20552cbdb686STakashi Iwai 	struct cmipci_sb_reg reg;
20561da177e4SLinus Torvalds 	int change;
20571da177e4SLinus Torvalds 	int left, right, oleft, oright;
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
20601da177e4SLinus Torvalds 	left = ucontrol->value.integer.value[0] & reg.mask;
20611da177e4SLinus Torvalds 	if (reg.invert)
20621da177e4SLinus Torvalds 		left = reg.mask - left;
20631da177e4SLinus Torvalds 	left <<= reg.left_shift;
20641da177e4SLinus Torvalds 	if (reg.stereo) {
20651da177e4SLinus Torvalds 		right = ucontrol->value.integer.value[1] & reg.mask;
20661da177e4SLinus Torvalds 		if (reg.invert)
20671da177e4SLinus Torvalds 			right = reg.mask - right;
20681da177e4SLinus Torvalds 		right <<= reg.right_shift;
20691da177e4SLinus Torvalds 	} else
20701da177e4SLinus Torvalds 		right = 0;
20711da177e4SLinus Torvalds 	spin_lock_irq(&cm->reg_lock);
20721da177e4SLinus Torvalds 	oleft = snd_cmipci_mixer_read(cm, reg.left_reg);
20731da177e4SLinus Torvalds 	left |= oleft & ~(reg.mask << reg.left_shift);
20741da177e4SLinus Torvalds 	change = left != oleft;
20751da177e4SLinus Torvalds 	if (reg.stereo) {
20761da177e4SLinus Torvalds 		if (reg.left_reg != reg.right_reg) {
20771da177e4SLinus Torvalds 			snd_cmipci_mixer_write(cm, reg.left_reg, left);
20781da177e4SLinus Torvalds 			oright = snd_cmipci_mixer_read(cm, reg.right_reg);
20791da177e4SLinus Torvalds 		} else
20801da177e4SLinus Torvalds 			oright = left;
20811da177e4SLinus Torvalds 		right |= oright & ~(reg.mask << reg.right_shift);
20821da177e4SLinus Torvalds 		change |= right != oright;
20831da177e4SLinus Torvalds 		snd_cmipci_mixer_write(cm, reg.right_reg, right);
20841da177e4SLinus Torvalds 	} else
20851da177e4SLinus Torvalds 		snd_cmipci_mixer_write(cm, reg.left_reg, left);
20861da177e4SLinus Torvalds 	spin_unlock_irq(&cm->reg_lock);
20871da177e4SLinus Torvalds 	return change;
20881da177e4SLinus Torvalds }
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds /*
20911da177e4SLinus Torvalds  * input route (left,right) -> (left,right)
20921da177e4SLinus Torvalds  */
20931da177e4SLinus Torvalds #define CMIPCI_SB_INPUT_SW(xname, left_shift, right_shift) \
20941da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
20951da177e4SLinus Torvalds   .info = snd_cmipci_info_input_sw, \
20961da177e4SLinus Torvalds   .get = snd_cmipci_get_input_sw, .put = snd_cmipci_put_input_sw, \
20971da177e4SLinus Torvalds   .private_value = COMPOSE_SB_REG(SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, left_shift, right_shift, 1, 0, 1), \
20981da177e4SLinus Torvalds }
20991da177e4SLinus Torvalds 
21002cbdb686STakashi Iwai static int snd_cmipci_info_input_sw(struct snd_kcontrol *kcontrol,
21012cbdb686STakashi Iwai 				    struct snd_ctl_elem_info *uinfo)
21021da177e4SLinus Torvalds {
21031da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
21041da177e4SLinus Torvalds 	uinfo->count = 4;
21051da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
21061da177e4SLinus Torvalds 	uinfo->value.integer.max = 1;
21071da177e4SLinus Torvalds 	return 0;
21081da177e4SLinus Torvalds }
21091da177e4SLinus Torvalds 
21102cbdb686STakashi Iwai static int snd_cmipci_get_input_sw(struct snd_kcontrol *kcontrol,
21112cbdb686STakashi Iwai 				   struct snd_ctl_elem_value *ucontrol)
21121da177e4SLinus Torvalds {
21132cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
21142cbdb686STakashi Iwai 	struct cmipci_sb_reg reg;
21151da177e4SLinus Torvalds 	int val1, val2;
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds 	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
21181da177e4SLinus Torvalds 	spin_lock_irq(&cm->reg_lock);
21191da177e4SLinus Torvalds 	val1 = snd_cmipci_mixer_read(cm, reg.left_reg);
21201da177e4SLinus Torvalds 	val2 = snd_cmipci_mixer_read(cm, reg.right_reg);
21211da177e4SLinus Torvalds 	spin_unlock_irq(&cm->reg_lock);
21221da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (val1 >> reg.left_shift) & 1;
21231da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = (val2 >> reg.left_shift) & 1;
21241da177e4SLinus Torvalds 	ucontrol->value.integer.value[2] = (val1 >> reg.right_shift) & 1;
21251da177e4SLinus Torvalds 	ucontrol->value.integer.value[3] = (val2 >> reg.right_shift) & 1;
21261da177e4SLinus Torvalds 	return 0;
21271da177e4SLinus Torvalds }
21281da177e4SLinus Torvalds 
21292cbdb686STakashi Iwai static int snd_cmipci_put_input_sw(struct snd_kcontrol *kcontrol,
21302cbdb686STakashi Iwai 				   struct snd_ctl_elem_value *ucontrol)
21311da177e4SLinus Torvalds {
21322cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
21332cbdb686STakashi Iwai 	struct cmipci_sb_reg reg;
21341da177e4SLinus Torvalds 	int change;
21351da177e4SLinus Torvalds 	int val1, val2, oval1, oval2;
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
21381da177e4SLinus Torvalds 	spin_lock_irq(&cm->reg_lock);
21391da177e4SLinus Torvalds 	oval1 = snd_cmipci_mixer_read(cm, reg.left_reg);
21401da177e4SLinus Torvalds 	oval2 = snd_cmipci_mixer_read(cm, reg.right_reg);
21411da177e4SLinus Torvalds 	val1 = oval1 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
21421da177e4SLinus Torvalds 	val2 = oval2 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
21431da177e4SLinus Torvalds 	val1 |= (ucontrol->value.integer.value[0] & 1) << reg.left_shift;
21441da177e4SLinus Torvalds 	val2 |= (ucontrol->value.integer.value[1] & 1) << reg.left_shift;
21451da177e4SLinus Torvalds 	val1 |= (ucontrol->value.integer.value[2] & 1) << reg.right_shift;
21461da177e4SLinus Torvalds 	val2 |= (ucontrol->value.integer.value[3] & 1) << reg.right_shift;
21471da177e4SLinus Torvalds 	change = val1 != oval1 || val2 != oval2;
21481da177e4SLinus Torvalds 	snd_cmipci_mixer_write(cm, reg.left_reg, val1);
21491da177e4SLinus Torvalds 	snd_cmipci_mixer_write(cm, reg.right_reg, val2);
21501da177e4SLinus Torvalds 	spin_unlock_irq(&cm->reg_lock);
21511da177e4SLinus Torvalds 	return change;
21521da177e4SLinus Torvalds }
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds /*
21551da177e4SLinus Torvalds  * native mixer switches/volumes
21561da177e4SLinus Torvalds  */
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds #define CMIPCI_MIXER_SW_STEREO(xname, reg, lshift, rshift, invert) \
21591da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
21601da177e4SLinus Torvalds   .info = snd_cmipci_info_native_mixer, \
21611da177e4SLinus Torvalds   .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
21621da177e4SLinus Torvalds   .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, 1, invert, 1), \
21631da177e4SLinus Torvalds }
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds #define CMIPCI_MIXER_SW_MONO(xname, reg, shift, invert) \
21661da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
21671da177e4SLinus Torvalds   .info = snd_cmipci_info_native_mixer, \
21681da177e4SLinus Torvalds   .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
21691da177e4SLinus Torvalds   .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, 1, invert, 0), \
21701da177e4SLinus Torvalds }
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds #define CMIPCI_MIXER_VOL_STEREO(xname, reg, lshift, rshift, mask) \
21731da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
21741da177e4SLinus Torvalds   .info = snd_cmipci_info_native_mixer, \
21751da177e4SLinus Torvalds   .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
21761da177e4SLinus Torvalds   .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, mask, 0, 1), \
21771da177e4SLinus Torvalds }
21781da177e4SLinus Torvalds 
21791da177e4SLinus Torvalds #define CMIPCI_MIXER_VOL_MONO(xname, reg, shift, mask) \
21801da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
21811da177e4SLinus Torvalds   .info = snd_cmipci_info_native_mixer, \
21821da177e4SLinus Torvalds   .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
21831da177e4SLinus Torvalds   .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, mask, 0, 0), \
21841da177e4SLinus Torvalds }
21851da177e4SLinus Torvalds 
21862cbdb686STakashi Iwai static int snd_cmipci_info_native_mixer(struct snd_kcontrol *kcontrol,
21872cbdb686STakashi Iwai 					struct snd_ctl_elem_info *uinfo)
21881da177e4SLinus Torvalds {
21892cbdb686STakashi Iwai 	struct cmipci_sb_reg reg;
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
21921da177e4SLinus Torvalds 	uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
21931da177e4SLinus Torvalds 	uinfo->count = reg.stereo + 1;
21941da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
21951da177e4SLinus Torvalds 	uinfo->value.integer.max = reg.mask;
21961da177e4SLinus Torvalds 	return 0;
21971da177e4SLinus Torvalds 
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds 
22002cbdb686STakashi Iwai static int snd_cmipci_get_native_mixer(struct snd_kcontrol *kcontrol,
22012cbdb686STakashi Iwai 				       struct snd_ctl_elem_value *ucontrol)
22021da177e4SLinus Torvalds {
22032cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
22042cbdb686STakashi Iwai 	struct cmipci_sb_reg reg;
22051da177e4SLinus Torvalds 	unsigned char oreg, val;
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds 	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
22081da177e4SLinus Torvalds 	spin_lock_irq(&cm->reg_lock);
22091da177e4SLinus Torvalds 	oreg = inb(cm->iobase + reg.left_reg);
22101da177e4SLinus Torvalds 	val = (oreg >> reg.left_shift) & reg.mask;
22111da177e4SLinus Torvalds 	if (reg.invert)
22121da177e4SLinus Torvalds 		val = reg.mask - val;
22131da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = val;
22141da177e4SLinus Torvalds 	if (reg.stereo) {
22151da177e4SLinus Torvalds 		val = (oreg >> reg.right_shift) & reg.mask;
22161da177e4SLinus Torvalds 		if (reg.invert)
22171da177e4SLinus Torvalds 			val = reg.mask - val;
22181da177e4SLinus Torvalds 		ucontrol->value.integer.value[1] = val;
22191da177e4SLinus Torvalds 	}
22201da177e4SLinus Torvalds 	spin_unlock_irq(&cm->reg_lock);
22211da177e4SLinus Torvalds 	return 0;
22221da177e4SLinus Torvalds }
22231da177e4SLinus Torvalds 
22242cbdb686STakashi Iwai static int snd_cmipci_put_native_mixer(struct snd_kcontrol *kcontrol,
22252cbdb686STakashi Iwai 				       struct snd_ctl_elem_value *ucontrol)
22261da177e4SLinus Torvalds {
22272cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
22282cbdb686STakashi Iwai 	struct cmipci_sb_reg reg;
22291da177e4SLinus Torvalds 	unsigned char oreg, nreg, val;
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds 	cmipci_sb_reg_decode(&reg, kcontrol->private_value);
22321da177e4SLinus Torvalds 	spin_lock_irq(&cm->reg_lock);
22331da177e4SLinus Torvalds 	oreg = inb(cm->iobase + reg.left_reg);
22341da177e4SLinus Torvalds 	val = ucontrol->value.integer.value[0] & reg.mask;
22351da177e4SLinus Torvalds 	if (reg.invert)
22361da177e4SLinus Torvalds 		val = reg.mask - val;
22371da177e4SLinus Torvalds 	nreg = oreg & ~(reg.mask << reg.left_shift);
22381da177e4SLinus Torvalds 	nreg |= (val << reg.left_shift);
22391da177e4SLinus Torvalds 	if (reg.stereo) {
22401da177e4SLinus Torvalds 		val = ucontrol->value.integer.value[1] & reg.mask;
22411da177e4SLinus Torvalds 		if (reg.invert)
22421da177e4SLinus Torvalds 			val = reg.mask - val;
22431da177e4SLinus Torvalds 		nreg &= ~(reg.mask << reg.right_shift);
22441da177e4SLinus Torvalds 		nreg |= (val << reg.right_shift);
22451da177e4SLinus Torvalds 	}
22461da177e4SLinus Torvalds 	outb(nreg, cm->iobase + reg.left_reg);
22471da177e4SLinus Torvalds 	spin_unlock_irq(&cm->reg_lock);
22481da177e4SLinus Torvalds 	return (nreg != oreg);
22491da177e4SLinus Torvalds }
22501da177e4SLinus Torvalds 
22511da177e4SLinus Torvalds /*
22521da177e4SLinus Torvalds  * special case - check mixer sensitivity
22531da177e4SLinus Torvalds  */
22542cbdb686STakashi Iwai static int snd_cmipci_get_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
22552cbdb686STakashi Iwai 						 struct snd_ctl_elem_value *ucontrol)
22561da177e4SLinus Torvalds {
22572cbdb686STakashi Iwai 	//struct cmipci *cm = snd_kcontrol_chip(kcontrol);
22581da177e4SLinus Torvalds 	return snd_cmipci_get_native_mixer(kcontrol, ucontrol);
22591da177e4SLinus Torvalds }
22601da177e4SLinus Torvalds 
22612cbdb686STakashi Iwai static int snd_cmipci_put_native_mixer_sensitive(struct snd_kcontrol *kcontrol,
22622cbdb686STakashi Iwai 						 struct snd_ctl_elem_value *ucontrol)
22631da177e4SLinus Torvalds {
22642cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
22651da177e4SLinus Torvalds 	if (cm->mixer_insensitive) {
22661da177e4SLinus Torvalds 		/* ignored */
22671da177e4SLinus Torvalds 		return 0;
22681da177e4SLinus Torvalds 	}
22691da177e4SLinus Torvalds 	return snd_cmipci_put_native_mixer(kcontrol, ucontrol);
22701da177e4SLinus Torvalds }
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds 
22732cbdb686STakashi Iwai static struct snd_kcontrol_new snd_cmipci_mixers[] __devinitdata = {
22741da177e4SLinus Torvalds 	CMIPCI_SB_VOL_STEREO("Master Playback Volume", SB_DSP4_MASTER_DEV, 3, 31),
22751da177e4SLinus Torvalds 	CMIPCI_MIXER_SW_MONO("3D Control - Switch", CM_REG_MIXER1, CM_X3DEN_SHIFT, 0),
22761da177e4SLinus Torvalds 	CMIPCI_SB_VOL_STEREO("PCM Playback Volume", SB_DSP4_PCM_DEV, 3, 31),
22771da177e4SLinus Torvalds 	//CMIPCI_MIXER_SW_MONO("PCM Playback Switch", CM_REG_MIXER1, CM_WSMUTE_SHIFT, 1),
22781da177e4SLinus Torvalds 	{ /* switch with sensitivity */
22791da177e4SLinus Torvalds 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
22801da177e4SLinus Torvalds 		.name = "PCM Playback Switch",
22811da177e4SLinus Torvalds 		.info = snd_cmipci_info_native_mixer,
22821da177e4SLinus Torvalds 		.get = snd_cmipci_get_native_mixer_sensitive,
22831da177e4SLinus Torvalds 		.put = snd_cmipci_put_native_mixer_sensitive,
22841da177e4SLinus Torvalds 		.private_value = COMPOSE_SB_REG(CM_REG_MIXER1, CM_REG_MIXER1, CM_WSMUTE_SHIFT, CM_WSMUTE_SHIFT, 1, 1, 0),
22851da177e4SLinus Torvalds 	},
22861da177e4SLinus Torvalds 	CMIPCI_MIXER_SW_STEREO("PCM Capture Switch", CM_REG_MIXER1, CM_WAVEINL_SHIFT, CM_WAVEINR_SHIFT, 0),
22871da177e4SLinus Torvalds 	CMIPCI_SB_VOL_STEREO("Synth Playback Volume", SB_DSP4_SYNTH_DEV, 3, 31),
22881da177e4SLinus Torvalds 	CMIPCI_MIXER_SW_MONO("Synth Playback Switch", CM_REG_MIXER1, CM_FMMUTE_SHIFT, 1),
22891da177e4SLinus Torvalds 	CMIPCI_SB_INPUT_SW("Synth Capture Route", 6, 5),
22901da177e4SLinus Torvalds 	CMIPCI_SB_VOL_STEREO("CD Playback Volume", SB_DSP4_CD_DEV, 3, 31),
22911da177e4SLinus Torvalds 	CMIPCI_SB_SW_STEREO("CD Playback Switch", 2, 1),
22921da177e4SLinus Torvalds 	CMIPCI_SB_INPUT_SW("CD Capture Route", 2, 1),
22931da177e4SLinus Torvalds 	CMIPCI_SB_VOL_STEREO("Line Playback Volume", SB_DSP4_LINE_DEV, 3, 31),
22941da177e4SLinus Torvalds 	CMIPCI_SB_SW_STEREO("Line Playback Switch", 4, 3),
22951da177e4SLinus Torvalds 	CMIPCI_SB_INPUT_SW("Line Capture Route", 4, 3),
22961da177e4SLinus Torvalds 	CMIPCI_SB_VOL_MONO("Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
22971da177e4SLinus Torvalds 	CMIPCI_SB_SW_MONO("Mic Playback Switch", 0),
22981da177e4SLinus Torvalds 	CMIPCI_DOUBLE("Mic Capture Switch", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 0, 0, 1, 0, 0),
22991da177e4SLinus Torvalds 	CMIPCI_SB_VOL_MONO("PC Speaker Playback Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
23001da177e4SLinus Torvalds 	CMIPCI_MIXER_VOL_STEREO("Aux Playback Volume", CM_REG_AUX_VOL, 4, 0, 15),
23011da177e4SLinus Torvalds 	CMIPCI_MIXER_SW_STEREO("Aux Playback Switch", CM_REG_MIXER2, CM_VAUXLM_SHIFT, CM_VAUXRM_SHIFT, 0),
23021da177e4SLinus Torvalds 	CMIPCI_MIXER_SW_STEREO("Aux Capture Switch", CM_REG_MIXER2, CM_RAUXLEN_SHIFT, CM_RAUXREN_SHIFT, 0),
23032eff7ec8STakashi Iwai 	CMIPCI_MIXER_SW_MONO("Mic Boost Playback Switch", CM_REG_MIXER2, CM_MICGAINZ_SHIFT, 1),
23041da177e4SLinus Torvalds 	CMIPCI_MIXER_VOL_MONO("Mic Capture Volume", CM_REG_MIXER2, CM_VADMIC_SHIFT, 7),
23052eff7ec8STakashi Iwai 	CMIPCI_SB_VOL_MONO("Phone Playback Volume", CM_REG_EXTENT_IND, 5, 7),
23062eff7ec8STakashi Iwai 	CMIPCI_DOUBLE("Phone Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 4, 4, 1, 0, 0),
2307f26eb78fSTakashi Iwai 	CMIPCI_DOUBLE("PC Speaker Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 3, 3, 1, 0, 0),
23082eff7ec8STakashi Iwai 	CMIPCI_DOUBLE("Mic Boost Capture Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 0, 0, 1, 0, 0),
23091da177e4SLinus Torvalds };
23101da177e4SLinus Torvalds 
23111da177e4SLinus Torvalds /*
23121da177e4SLinus Torvalds  * other switches
23131da177e4SLinus Torvalds  */
23141da177e4SLinus Torvalds 
23152cbdb686STakashi Iwai struct cmipci_switch_args {
23161da177e4SLinus Torvalds 	int reg;		/* register index */
23171da177e4SLinus Torvalds 	unsigned int mask;	/* mask bits */
23181da177e4SLinus Torvalds 	unsigned int mask_on;	/* mask bits to turn on */
23191da177e4SLinus Torvalds 	unsigned int is_byte: 1;		/* byte access? */
23202cbdb686STakashi Iwai 	unsigned int ac3_sensitive: 1;	/* access forbidden during
23212cbdb686STakashi Iwai 					 * non-audio operation?
23222cbdb686STakashi Iwai 					 */
23232cbdb686STakashi Iwai };
23241da177e4SLinus Torvalds 
2325a5ce8890STakashi Iwai #define snd_cmipci_uswitch_info		snd_ctl_boolean_mono_info
23261da177e4SLinus Torvalds 
23272cbdb686STakashi Iwai static int _snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
23282cbdb686STakashi Iwai 				   struct snd_ctl_elem_value *ucontrol,
23292cbdb686STakashi Iwai 				   struct cmipci_switch_args *args)
23301da177e4SLinus Torvalds {
23311da177e4SLinus Torvalds 	unsigned int val;
23322cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 	spin_lock_irq(&cm->reg_lock);
23351da177e4SLinus Torvalds 	if (args->ac3_sensitive && cm->mixer_insensitive) {
23361da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = 0;
23371da177e4SLinus Torvalds 		spin_unlock_irq(&cm->reg_lock);
23381da177e4SLinus Torvalds 		return 0;
23391da177e4SLinus Torvalds 	}
23401da177e4SLinus Torvalds 	if (args->is_byte)
23411da177e4SLinus Torvalds 		val = inb(cm->iobase + args->reg);
23421da177e4SLinus Torvalds 	else
23431da177e4SLinus Torvalds 		val = snd_cmipci_read(cm, args->reg);
23441da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = ((val & args->mask) == args->mask_on) ? 1 : 0;
23451da177e4SLinus Torvalds 	spin_unlock_irq(&cm->reg_lock);
23461da177e4SLinus Torvalds 	return 0;
23471da177e4SLinus Torvalds }
23481da177e4SLinus Torvalds 
23492cbdb686STakashi Iwai static int snd_cmipci_uswitch_get(struct snd_kcontrol *kcontrol,
23502cbdb686STakashi Iwai 				  struct snd_ctl_elem_value *ucontrol)
23511da177e4SLinus Torvalds {
23522cbdb686STakashi Iwai 	struct cmipci_switch_args *args;
23532cbdb686STakashi Iwai 	args = (struct cmipci_switch_args *)kcontrol->private_value;
23541da177e4SLinus Torvalds 	snd_assert(args != NULL, return -EINVAL);
23551da177e4SLinus Torvalds 	return _snd_cmipci_uswitch_get(kcontrol, ucontrol, args);
23561da177e4SLinus Torvalds }
23571da177e4SLinus Torvalds 
23582cbdb686STakashi Iwai static int _snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
23592cbdb686STakashi Iwai 				   struct snd_ctl_elem_value *ucontrol,
23602cbdb686STakashi Iwai 				   struct cmipci_switch_args *args)
23611da177e4SLinus Torvalds {
23621da177e4SLinus Torvalds 	unsigned int val;
23631da177e4SLinus Torvalds 	int change;
23642cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds 	spin_lock_irq(&cm->reg_lock);
23671da177e4SLinus Torvalds 	if (args->ac3_sensitive && cm->mixer_insensitive) {
23681da177e4SLinus Torvalds 		/* ignored */
23691da177e4SLinus Torvalds 		spin_unlock_irq(&cm->reg_lock);
23701da177e4SLinus Torvalds 		return 0;
23711da177e4SLinus Torvalds 	}
23721da177e4SLinus Torvalds 	if (args->is_byte)
23731da177e4SLinus Torvalds 		val = inb(cm->iobase + args->reg);
23741da177e4SLinus Torvalds 	else
23751da177e4SLinus Torvalds 		val = snd_cmipci_read(cm, args->reg);
23768c670714STimofei V. Bondarenko 	change = (val & args->mask) != (ucontrol->value.integer.value[0] ?
23778c670714STimofei V. Bondarenko 			args->mask_on : (args->mask & ~args->mask_on));
23781da177e4SLinus Torvalds 	if (change) {
23791da177e4SLinus Torvalds 		val &= ~args->mask;
23801da177e4SLinus Torvalds 		if (ucontrol->value.integer.value[0])
23811da177e4SLinus Torvalds 			val |= args->mask_on;
23821da177e4SLinus Torvalds 		else
23831da177e4SLinus Torvalds 			val |= (args->mask & ~args->mask_on);
23841da177e4SLinus Torvalds 		if (args->is_byte)
23851da177e4SLinus Torvalds 			outb((unsigned char)val, cm->iobase + args->reg);
23861da177e4SLinus Torvalds 		else
23871da177e4SLinus Torvalds 			snd_cmipci_write(cm, args->reg, val);
23881da177e4SLinus Torvalds 	}
23891da177e4SLinus Torvalds 	spin_unlock_irq(&cm->reg_lock);
23901da177e4SLinus Torvalds 	return change;
23911da177e4SLinus Torvalds }
23921da177e4SLinus Torvalds 
23932cbdb686STakashi Iwai static int snd_cmipci_uswitch_put(struct snd_kcontrol *kcontrol,
23942cbdb686STakashi Iwai 				  struct snd_ctl_elem_value *ucontrol)
23951da177e4SLinus Torvalds {
23962cbdb686STakashi Iwai 	struct cmipci_switch_args *args;
23972cbdb686STakashi Iwai 	args = (struct cmipci_switch_args *)kcontrol->private_value;
23981da177e4SLinus Torvalds 	snd_assert(args != NULL, return -EINVAL);
23991da177e4SLinus Torvalds 	return _snd_cmipci_uswitch_put(kcontrol, ucontrol, args);
24001da177e4SLinus Torvalds }
24011da177e4SLinus Torvalds 
24021da177e4SLinus Torvalds #define DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask_on, xis_byte, xac3) \
24032cbdb686STakashi Iwai static struct cmipci_switch_args cmipci_switch_arg_##sname = { \
24041da177e4SLinus Torvalds   .reg = xreg, \
24051da177e4SLinus Torvalds   .mask = xmask, \
24061da177e4SLinus Torvalds   .mask_on = xmask_on, \
24071da177e4SLinus Torvalds   .is_byte = xis_byte, \
24081da177e4SLinus Torvalds   .ac3_sensitive = xac3, \
24091da177e4SLinus Torvalds }
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds #define DEFINE_BIT_SWITCH_ARG(sname, xreg, xmask, xis_byte, xac3) \
24121da177e4SLinus Torvalds 	DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask, xis_byte, xac3)
24131da177e4SLinus Torvalds 
24141da177e4SLinus Torvalds #if 0 /* these will be controlled in pcm device */
24151da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(spdif_in, CM_REG_FUNCTRL1, CM_SPDF_1, 0, 0);
24161da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(spdif_out, CM_REG_FUNCTRL1, CM_SPDF_0, 0, 0);
24171da177e4SLinus Torvalds #endif
24181da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(spdif_in_sel1, CM_REG_CHFORMAT, CM_SPDIF_SELECT1, 0, 0);
24191da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(spdif_in_sel2, CM_REG_MISC_CTRL, CM_SPDIF_SELECT2, 0, 0);
24201da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(spdif_enable, CM_REG_LEGACY_CTRL, CM_ENSPDOUT, 0, 0);
24211da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(spdo2dac, CM_REG_FUNCTRL1, CM_SPDO2DAC, 0, 1);
24221da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(spdi_valid, CM_REG_MISC, CM_SPDVALID, 1, 0);
24231da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(spdif_copyright, CM_REG_LEGACY_CTRL, CM_SPDCOPYRHT, 0, 0);
24241da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(spdif_dac_out, CM_REG_LEGACY_CTRL, CM_DAC2SPDO, 0, 1);
24251da177e4SLinus Torvalds DEFINE_SWITCH_ARG(spdo_5v, CM_REG_MISC_CTRL, CM_SPDO5V, 0, 0, 0); /* inverse: 0 = 5V */
24261da177e4SLinus Torvalds // DEFINE_BIT_SWITCH_ARG(spdo_48k, CM_REG_MISC_CTRL, CM_SPDF_AC97|CM_SPDIF48K, 0, 1);
24271da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(spdif_loop, CM_REG_FUNCTRL1, CM_SPDFLOOP, 0, 1);
24281da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(spdi_monitor, CM_REG_MIXER1, CM_CDPLAY, 1, 0);
24291da177e4SLinus Torvalds /* DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_CHFORMAT, CM_SPDIF_INVERSE, 0, 0); */
24301da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_MISC, CM_SPDIF_INVERSE, 1, 0);
24311da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(spdi_phase2, CM_REG_CHFORMAT, CM_SPDIF_INVERSE2, 0, 0);
24321da177e4SLinus Torvalds #if CM_CH_PLAY == 1
24331da177e4SLinus Torvalds DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, 0, 0, 0); /* reversed */
24341da177e4SLinus Torvalds #else
24351da177e4SLinus Torvalds DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, CM_XCHGDAC, 0, 0);
24361da177e4SLinus Torvalds #endif
24371da177e4SLinus Torvalds DEFINE_BIT_SWITCH_ARG(fourch, CM_REG_MISC_CTRL, CM_N4SPK3D, 0, 0);
2438a839a33dSClemens Ladisch // DEFINE_BIT_SWITCH_ARG(line_rear, CM_REG_MIXER1, CM_REAR2LIN, 1, 0);
2439a839a33dSClemens Ladisch // DEFINE_BIT_SWITCH_ARG(line_bass, CM_REG_LEGACY_CTRL, CM_CENTR2LIN|CM_BASE2LIN, 0, 0);
24401da177e4SLinus Torvalds // DEFINE_BIT_SWITCH_ARG(joystick, CM_REG_FUNCTRL1, CM_JYSTK_EN, 0, 0); /* now module option */
24411da177e4SLinus Torvalds DEFINE_SWITCH_ARG(modem, CM_REG_MISC_CTRL, CM_FLINKON|CM_FLINKOFF, CM_FLINKON, 0, 0);
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds #define DEFINE_SWITCH(sname, stype, sarg) \
24441da177e4SLinus Torvalds { .name = sname, \
24451da177e4SLinus Torvalds   .iface = stype, \
24461da177e4SLinus Torvalds   .info = snd_cmipci_uswitch_info, \
24471da177e4SLinus Torvalds   .get = snd_cmipci_uswitch_get, \
24481da177e4SLinus Torvalds   .put = snd_cmipci_uswitch_put, \
24491da177e4SLinus Torvalds   .private_value = (unsigned long)&cmipci_switch_arg_##sarg,\
24501da177e4SLinus Torvalds }
24511da177e4SLinus Torvalds 
24521da177e4SLinus Torvalds #define DEFINE_CARD_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_CARD, sarg)
24531da177e4SLinus Torvalds #define DEFINE_MIXER_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_MIXER, sarg)
24541da177e4SLinus Torvalds 
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds /*
24571da177e4SLinus Torvalds  * callbacks for spdif output switch
24581da177e4SLinus Torvalds  * needs toggle two registers..
24591da177e4SLinus Torvalds  */
24602cbdb686STakashi Iwai static int snd_cmipci_spdout_enable_get(struct snd_kcontrol *kcontrol,
24612cbdb686STakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
24621da177e4SLinus Torvalds {
24631da177e4SLinus Torvalds 	int changed;
24641da177e4SLinus Torvalds 	changed = _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
24651da177e4SLinus Torvalds 	changed |= _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
24661da177e4SLinus Torvalds 	return changed;
24671da177e4SLinus Torvalds }
24681da177e4SLinus Torvalds 
24692cbdb686STakashi Iwai static int snd_cmipci_spdout_enable_put(struct snd_kcontrol *kcontrol,
24702cbdb686STakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
24711da177e4SLinus Torvalds {
24722cbdb686STakashi Iwai 	struct cmipci *chip = snd_kcontrol_chip(kcontrol);
24731da177e4SLinus Torvalds 	int changed;
24741da177e4SLinus Torvalds 	changed = _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
24751da177e4SLinus Torvalds 	changed |= _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
24761da177e4SLinus Torvalds 	if (changed) {
24771da177e4SLinus Torvalds 		if (ucontrol->value.integer.value[0]) {
24781da177e4SLinus Torvalds 			if (chip->spdif_playback_avail)
24791da177e4SLinus Torvalds 				snd_cmipci_set_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
24801da177e4SLinus Torvalds 		} else {
24811da177e4SLinus Torvalds 			if (chip->spdif_playback_avail)
24821da177e4SLinus Torvalds 				snd_cmipci_clear_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
24831da177e4SLinus Torvalds 		}
24841da177e4SLinus Torvalds 	}
24851da177e4SLinus Torvalds 	chip->spdif_playback_enabled = ucontrol->value.integer.value[0];
24861da177e4SLinus Torvalds 	return changed;
24871da177e4SLinus Torvalds }
24881da177e4SLinus Torvalds 
24891da177e4SLinus Torvalds 
24902cbdb686STakashi Iwai static int snd_cmipci_line_in_mode_info(struct snd_kcontrol *kcontrol,
24912cbdb686STakashi Iwai 					struct snd_ctl_elem_info *uinfo)
249201d25d46STakashi Iwai {
24932cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
249401d25d46STakashi Iwai 	static char *texts[3] = { "Line-In", "Rear Output", "Bass Output" };
249501d25d46STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
249601d25d46STakashi Iwai 	uinfo->count = 1;
249701d25d46STakashi Iwai 	uinfo->value.enumerated.items = cm->chip_version >= 39 ? 3 : 2;
249801d25d46STakashi Iwai 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
249901d25d46STakashi Iwai 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
250001d25d46STakashi Iwai 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
250101d25d46STakashi Iwai 	return 0;
250201d25d46STakashi Iwai }
250301d25d46STakashi Iwai 
25042cbdb686STakashi Iwai static inline unsigned int get_line_in_mode(struct cmipci *cm)
250501d25d46STakashi Iwai {
250601d25d46STakashi Iwai 	unsigned int val;
250701d25d46STakashi Iwai 	if (cm->chip_version >= 39) {
250801d25d46STakashi Iwai 		val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL);
2509a839a33dSClemens Ladisch 		if (val & (CM_CENTR2LIN | CM_BASE2LIN))
251001d25d46STakashi Iwai 			return 2;
251101d25d46STakashi Iwai 	}
251201d25d46STakashi Iwai 	val = snd_cmipci_read_b(cm, CM_REG_MIXER1);
2513a839a33dSClemens Ladisch 	if (val & CM_REAR2LIN)
251401d25d46STakashi Iwai 		return 1;
251501d25d46STakashi Iwai 	return 0;
251601d25d46STakashi Iwai }
251701d25d46STakashi Iwai 
25182cbdb686STakashi Iwai static int snd_cmipci_line_in_mode_get(struct snd_kcontrol *kcontrol,
25192cbdb686STakashi Iwai 				       struct snd_ctl_elem_value *ucontrol)
252001d25d46STakashi Iwai {
25212cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
252201d25d46STakashi Iwai 
252301d25d46STakashi Iwai 	spin_lock_irq(&cm->reg_lock);
252401d25d46STakashi Iwai 	ucontrol->value.enumerated.item[0] = get_line_in_mode(cm);
252501d25d46STakashi Iwai 	spin_unlock_irq(&cm->reg_lock);
252601d25d46STakashi Iwai 	return 0;
252701d25d46STakashi Iwai }
252801d25d46STakashi Iwai 
25292cbdb686STakashi Iwai static int snd_cmipci_line_in_mode_put(struct snd_kcontrol *kcontrol,
25302cbdb686STakashi Iwai 				       struct snd_ctl_elem_value *ucontrol)
253101d25d46STakashi Iwai {
25322cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
253301d25d46STakashi Iwai 	int change;
253401d25d46STakashi Iwai 
253501d25d46STakashi Iwai 	spin_lock_irq(&cm->reg_lock);
253601d25d46STakashi Iwai 	if (ucontrol->value.enumerated.item[0] == 2)
2537a839a33dSClemens Ladisch 		change = snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
253801d25d46STakashi Iwai 	else
2539a839a33dSClemens Ladisch 		change = snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CENTR2LIN | CM_BASE2LIN);
254001d25d46STakashi Iwai 	if (ucontrol->value.enumerated.item[0] == 1)
2541a839a33dSClemens Ladisch 		change |= snd_cmipci_set_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
254201d25d46STakashi Iwai 	else
2543a839a33dSClemens Ladisch 		change |= snd_cmipci_clear_bit_b(cm, CM_REG_MIXER1, CM_REAR2LIN);
254401d25d46STakashi Iwai 	spin_unlock_irq(&cm->reg_lock);
254501d25d46STakashi Iwai 	return change;
254601d25d46STakashi Iwai }
254701d25d46STakashi Iwai 
25482cbdb686STakashi Iwai static int snd_cmipci_mic_in_mode_info(struct snd_kcontrol *kcontrol,
25492cbdb686STakashi Iwai 				       struct snd_ctl_elem_info *uinfo)
255001d25d46STakashi Iwai {
255101d25d46STakashi Iwai 	static char *texts[2] = { "Mic-In", "Center/LFE Output" };
255201d25d46STakashi Iwai 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
255301d25d46STakashi Iwai 	uinfo->count = 1;
255401d25d46STakashi Iwai 	uinfo->value.enumerated.items = 2;
255501d25d46STakashi Iwai 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
255601d25d46STakashi Iwai 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
255701d25d46STakashi Iwai 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
255801d25d46STakashi Iwai 	return 0;
255901d25d46STakashi Iwai }
256001d25d46STakashi Iwai 
25612cbdb686STakashi Iwai static int snd_cmipci_mic_in_mode_get(struct snd_kcontrol *kcontrol,
25622cbdb686STakashi Iwai 				      struct snd_ctl_elem_value *ucontrol)
256301d25d46STakashi Iwai {
25642cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
256501d25d46STakashi Iwai 	/* same bit as spdi_phase */
256601d25d46STakashi Iwai 	spin_lock_irq(&cm->reg_lock);
256701d25d46STakashi Iwai 	ucontrol->value.enumerated.item[0] =
256801d25d46STakashi Iwai 		(snd_cmipci_read_b(cm, CM_REG_MISC) & CM_SPDIF_INVERSE) ? 1 : 0;
256901d25d46STakashi Iwai 	spin_unlock_irq(&cm->reg_lock);
257001d25d46STakashi Iwai 	return 0;
257101d25d46STakashi Iwai }
257201d25d46STakashi Iwai 
25732cbdb686STakashi Iwai static int snd_cmipci_mic_in_mode_put(struct snd_kcontrol *kcontrol,
25742cbdb686STakashi Iwai 				      struct snd_ctl_elem_value *ucontrol)
257501d25d46STakashi Iwai {
25762cbdb686STakashi Iwai 	struct cmipci *cm = snd_kcontrol_chip(kcontrol);
257701d25d46STakashi Iwai 	int change;
257801d25d46STakashi Iwai 
257901d25d46STakashi Iwai 	spin_lock_irq(&cm->reg_lock);
258001d25d46STakashi Iwai 	if (ucontrol->value.enumerated.item[0])
258101d25d46STakashi Iwai 		change = snd_cmipci_set_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
258201d25d46STakashi Iwai 	else
258301d25d46STakashi Iwai 		change = snd_cmipci_clear_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
258401d25d46STakashi Iwai 	spin_unlock_irq(&cm->reg_lock);
258501d25d46STakashi Iwai 	return change;
258601d25d46STakashi Iwai }
258701d25d46STakashi Iwai 
25881da177e4SLinus Torvalds /* both for CM8338/8738 */
25892cbdb686STakashi Iwai static struct snd_kcontrol_new snd_cmipci_mixer_switches[] __devinitdata = {
25901da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("Four Channel Mode", fourch),
259101d25d46STakashi Iwai 	{
259201d25d46STakashi Iwai 		.name = "Line-In Mode",
259301d25d46STakashi Iwai 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
259401d25d46STakashi Iwai 		.info = snd_cmipci_line_in_mode_info,
259501d25d46STakashi Iwai 		.get = snd_cmipci_line_in_mode_get,
259601d25d46STakashi Iwai 		.put = snd_cmipci_line_in_mode_put,
259701d25d46STakashi Iwai 	},
25981da177e4SLinus Torvalds };
25991da177e4SLinus Torvalds 
26001da177e4SLinus Torvalds /* for non-multichannel chips */
26012cbdb686STakashi Iwai static struct snd_kcontrol_new snd_cmipci_nomulti_switch __devinitdata =
26021da177e4SLinus Torvalds DEFINE_MIXER_SWITCH("Exchange DAC", exchange_dac);
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds /* only for CM8738 */
26052cbdb686STakashi Iwai static struct snd_kcontrol_new snd_cmipci_8738_mixer_switches[] __devinitdata = {
26061da177e4SLinus Torvalds #if 0 /* controlled in pcm device */
26071da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("IEC958 In Record", spdif_in),
26081da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("IEC958 Out", spdif_out),
26091da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("IEC958 Out To DAC", spdo2dac),
26101da177e4SLinus Torvalds #endif
26111da177e4SLinus Torvalds 	// DEFINE_MIXER_SWITCH("IEC958 Output Switch", spdif_enable),
26121da177e4SLinus Torvalds 	{ .name = "IEC958 Output Switch",
26131da177e4SLinus Torvalds 	  .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
26141da177e4SLinus Torvalds 	  .info = snd_cmipci_uswitch_info,
26151da177e4SLinus Torvalds 	  .get = snd_cmipci_spdout_enable_get,
26161da177e4SLinus Torvalds 	  .put = snd_cmipci_spdout_enable_put,
26171da177e4SLinus Torvalds 	},
26181da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("IEC958 In Valid", spdi_valid),
26191da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("IEC958 Copyright", spdif_copyright),
26201da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("IEC958 5V", spdo_5v),
26211da177e4SLinus Torvalds //	DEFINE_MIXER_SWITCH("IEC958 In/Out 48KHz", spdo_48k),
26221da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("IEC958 Loop", spdif_loop),
26231da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("IEC958 In Monitor", spdi_monitor),
26241da177e4SLinus Torvalds };
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds /* only for model 033/037 */
26272cbdb686STakashi Iwai static struct snd_kcontrol_new snd_cmipci_old_mixer_switches[] __devinitdata = {
26281da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("IEC958 Mix Analog", spdif_dac_out),
26291da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase),
26301da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel1),
26311da177e4SLinus Torvalds };
26321da177e4SLinus Torvalds 
26331da177e4SLinus Torvalds /* only for model 039 or later */
26342cbdb686STakashi Iwai static struct snd_kcontrol_new snd_cmipci_extra_mixer_switches[] __devinitdata = {
26351da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel2),
26361da177e4SLinus Torvalds 	DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase2),
263701d25d46STakashi Iwai 	{
263801d25d46STakashi Iwai 		.name = "Mic-In Mode",
263901d25d46STakashi Iwai 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
264001d25d46STakashi Iwai 		.info = snd_cmipci_mic_in_mode_info,
264101d25d46STakashi Iwai 		.get = snd_cmipci_mic_in_mode_get,
264201d25d46STakashi Iwai 		.put = snd_cmipci_mic_in_mode_put,
264301d25d46STakashi Iwai 	}
26441da177e4SLinus Torvalds };
26451da177e4SLinus Torvalds 
26461da177e4SLinus Torvalds /* card control switches */
26472cbdb686STakashi Iwai static struct snd_kcontrol_new snd_cmipci_control_switches[] __devinitdata = {
26481da177e4SLinus Torvalds 	// DEFINE_CARD_SWITCH("Joystick", joystick), /* now module option */
26491da177e4SLinus Torvalds 	DEFINE_CARD_SWITCH("Modem", modem),
26501da177e4SLinus Torvalds };
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds 
26532cbdb686STakashi Iwai static int __devinit snd_cmipci_mixer_new(struct cmipci *cm, int pcm_spdif_device)
26541da177e4SLinus Torvalds {
26552cbdb686STakashi Iwai 	struct snd_card *card;
26562cbdb686STakashi Iwai 	struct snd_kcontrol_new *sw;
26572cbdb686STakashi Iwai 	struct snd_kcontrol *kctl;
26581da177e4SLinus Torvalds 	unsigned int idx;
26591da177e4SLinus Torvalds 	int err;
26601da177e4SLinus Torvalds 
26611da177e4SLinus Torvalds 	snd_assert(cm != NULL && cm->card != NULL, return -EINVAL);
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 	card = cm->card;
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds 	strcpy(card->mixername, "CMedia PCI");
26661da177e4SLinus Torvalds 
26671da177e4SLinus Torvalds 	spin_lock_irq(&cm->reg_lock);
26681da177e4SLinus Torvalds 	snd_cmipci_mixer_write(cm, 0x00, 0x00);		/* mixer reset */
26691da177e4SLinus Torvalds 	spin_unlock_irq(&cm->reg_lock);
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixers); idx++) {
26721da177e4SLinus Torvalds 		if (cm->chip_version == 68) {	// 8768 has no PCM volume
26731da177e4SLinus Torvalds 			if (!strcmp(snd_cmipci_mixers[idx].name,
26741da177e4SLinus Torvalds 				"PCM Playback Volume"))
26751da177e4SLinus Torvalds 				continue;
26761da177e4SLinus Torvalds 		}
26771da177e4SLinus Torvalds 		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cmipci_mixers[idx], cm))) < 0)
26781da177e4SLinus Torvalds 			return err;
26791da177e4SLinus Torvalds 	}
26801da177e4SLinus Torvalds 
26811da177e4SLinus Torvalds 	/* mixer switches */
26821da177e4SLinus Torvalds 	sw = snd_cmipci_mixer_switches;
26831da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixer_switches); idx++, sw++) {
26841da177e4SLinus Torvalds 		err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
26851da177e4SLinus Torvalds 		if (err < 0)
26861da177e4SLinus Torvalds 			return err;
26871da177e4SLinus Torvalds 	}
26881da177e4SLinus Torvalds 	if (! cm->can_multi_ch) {
26891da177e4SLinus Torvalds 		err = snd_ctl_add(cm->card, snd_ctl_new1(&snd_cmipci_nomulti_switch, cm));
26901da177e4SLinus Torvalds 		if (err < 0)
26911da177e4SLinus Torvalds 			return err;
26921da177e4SLinus Torvalds 	}
26931da177e4SLinus Torvalds 	if (cm->device == PCI_DEVICE_ID_CMEDIA_CM8738 ||
26941da177e4SLinus Torvalds 	    cm->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
26951da177e4SLinus Torvalds 		sw = snd_cmipci_8738_mixer_switches;
26961da177e4SLinus Torvalds 		for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_8738_mixer_switches); idx++, sw++) {
26971da177e4SLinus Torvalds 			err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
26981da177e4SLinus Torvalds 			if (err < 0)
26991da177e4SLinus Torvalds 				return err;
27001da177e4SLinus Torvalds 		}
27011da177e4SLinus Torvalds 		if (cm->can_ac3_hw) {
27021da177e4SLinus Torvalds 			if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_default, cm))) < 0)
27031da177e4SLinus Torvalds 				return err;
27041da177e4SLinus Torvalds 			kctl->id.device = pcm_spdif_device;
27051da177e4SLinus Torvalds 			if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_mask, cm))) < 0)
27061da177e4SLinus Torvalds 				return err;
27071da177e4SLinus Torvalds 			kctl->id.device = pcm_spdif_device;
27081da177e4SLinus Torvalds 			if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_stream, cm))) < 0)
27091da177e4SLinus Torvalds 				return err;
27101da177e4SLinus Torvalds 			kctl->id.device = pcm_spdif_device;
27111da177e4SLinus Torvalds 		}
27121da177e4SLinus Torvalds 		if (cm->chip_version <= 37) {
27131da177e4SLinus Torvalds 			sw = snd_cmipci_old_mixer_switches;
27141da177e4SLinus Torvalds 			for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_old_mixer_switches); idx++, sw++) {
27151da177e4SLinus Torvalds 				err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
27161da177e4SLinus Torvalds 				if (err < 0)
27171da177e4SLinus Torvalds 					return err;
27181da177e4SLinus Torvalds 			}
27191da177e4SLinus Torvalds 		}
27201da177e4SLinus Torvalds 	}
27211da177e4SLinus Torvalds 	if (cm->chip_version >= 39) {
27221da177e4SLinus Torvalds 		sw = snd_cmipci_extra_mixer_switches;
27231da177e4SLinus Torvalds 		for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_extra_mixer_switches); idx++, sw++) {
27241da177e4SLinus Torvalds 			err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
27251da177e4SLinus Torvalds 			if (err < 0)
27261da177e4SLinus Torvalds 				return err;
27271da177e4SLinus Torvalds 		}
27281da177e4SLinus Torvalds 	}
27291da177e4SLinus Torvalds 
27301da177e4SLinus Torvalds 	/* card switches */
27311da177e4SLinus Torvalds 	sw = snd_cmipci_control_switches;
27321da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_control_switches); idx++, sw++) {
27331da177e4SLinus Torvalds 		err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
27341da177e4SLinus Torvalds 		if (err < 0)
27351da177e4SLinus Torvalds 			return err;
27361da177e4SLinus Torvalds 	}
27371da177e4SLinus Torvalds 
27381da177e4SLinus Torvalds 	for (idx = 0; idx < CM_SAVED_MIXERS; idx++) {
27392cbdb686STakashi Iwai 		struct snd_ctl_elem_id id;
27402cbdb686STakashi Iwai 		struct snd_kcontrol *ctl;
27411da177e4SLinus Torvalds 		memset(&id, 0, sizeof(id));
27421da177e4SLinus Torvalds 		id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
27431da177e4SLinus Torvalds 		strcpy(id.name, cm_saved_mixer[idx].name);
27441da177e4SLinus Torvalds 		if ((ctl = snd_ctl_find_id(cm->card, &id)) != NULL)
27451da177e4SLinus Torvalds 			cm->mixer_res_ctl[idx] = ctl;
27461da177e4SLinus Torvalds 	}
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds 	return 0;
27491da177e4SLinus Torvalds }
27501da177e4SLinus Torvalds 
27511da177e4SLinus Torvalds 
27521da177e4SLinus Torvalds /*
27531da177e4SLinus Torvalds  * proc interface
27541da177e4SLinus Torvalds  */
27551da177e4SLinus Torvalds 
27561da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
27572cbdb686STakashi Iwai static void snd_cmipci_proc_read(struct snd_info_entry *entry,
27582cbdb686STakashi Iwai 				 struct snd_info_buffer *buffer)
27591da177e4SLinus Torvalds {
27602cbdb686STakashi Iwai 	struct cmipci *cm = entry->private_data;
276154d030ccSClemens Ladisch 	int i, v;
27621da177e4SLinus Torvalds 
276354d030ccSClemens Ladisch 	snd_iprintf(buffer, "%s\n", cm->card->longname);
276454d030ccSClemens Ladisch 	for (i = 0; i < 0x94; i++) {
276554d030ccSClemens Ladisch 		if (i == 0x28)
276654d030ccSClemens Ladisch 			i = 0x90;
276754d030ccSClemens Ladisch 		v = inb(cm->iobase + i);
27681da177e4SLinus Torvalds 		if (i % 4 == 0)
276954d030ccSClemens Ladisch 			snd_iprintf(buffer, "\n%02x:", i);
27701da177e4SLinus Torvalds 		snd_iprintf(buffer, " %02x", v);
27711da177e4SLinus Torvalds 	}
277254d030ccSClemens Ladisch 	snd_iprintf(buffer, "\n");
27731da177e4SLinus Torvalds }
27741da177e4SLinus Torvalds 
27752cbdb686STakashi Iwai static void __devinit snd_cmipci_proc_init(struct cmipci *cm)
27761da177e4SLinus Torvalds {
27772cbdb686STakashi Iwai 	struct snd_info_entry *entry;
27781da177e4SLinus Torvalds 
27791da177e4SLinus Torvalds 	if (! snd_card_proc_new(cm->card, "cmipci", &entry))
2780bf850204STakashi Iwai 		snd_info_set_text_ops(entry, cm, snd_cmipci_proc_read);
27811da177e4SLinus Torvalds }
27821da177e4SLinus Torvalds #else /* !CONFIG_PROC_FS */
27832cbdb686STakashi Iwai static inline void snd_cmipci_proc_init(struct cmipci *cm) {}
27841da177e4SLinus Torvalds #endif
27851da177e4SLinus Torvalds 
27861da177e4SLinus Torvalds 
2787f40b6890STakashi Iwai static struct pci_device_id snd_cmipci_ids[] = {
27881da177e4SLinus Torvalds 	{PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
27891da177e4SLinus Torvalds 	{PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
27901da177e4SLinus Torvalds 	{PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
27911da177e4SLinus Torvalds 	{PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
27921da177e4SLinus Torvalds 	{PCI_VENDOR_ID_AL, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
27931da177e4SLinus Torvalds 	{0,},
27941da177e4SLinus Torvalds };
27951da177e4SLinus Torvalds 
27961da177e4SLinus Torvalds 
27971da177e4SLinus Torvalds /*
27981da177e4SLinus Torvalds  * check chip version and capabilities
27991da177e4SLinus Torvalds  * driver name is modified according to the chip model
28001da177e4SLinus Torvalds  */
28012cbdb686STakashi Iwai static void __devinit query_chip(struct cmipci *cm)
28021da177e4SLinus Torvalds {
28031da177e4SLinus Torvalds 	unsigned int detect;
28041da177e4SLinus Torvalds 
28051da177e4SLinus Torvalds 	/* check reg 0Ch, bit 24-31 */
28061da177e4SLinus Torvalds 	detect = snd_cmipci_read(cm, CM_REG_INT_HLDCLR) & CM_CHIP_MASK2;
28071da177e4SLinus Torvalds 	if (! detect) {
28081da177e4SLinus Torvalds 		/* check reg 08h, bit 24-28 */
28091da177e4SLinus Torvalds 		detect = snd_cmipci_read(cm, CM_REG_CHFORMAT) & CM_CHIP_MASK1;
2810133271feSClemens Ladisch 		switch (detect) {
2811133271feSClemens Ladisch 		case 0:
28121da177e4SLinus Torvalds 			cm->chip_version = 33;
28131da177e4SLinus Torvalds 			if (cm->do_soft_ac3)
28141da177e4SLinus Torvalds 				cm->can_ac3_sw = 1;
28151da177e4SLinus Torvalds 			else
28161da177e4SLinus Torvalds 				cm->can_ac3_hw = 1;
2817133271feSClemens Ladisch 			break;
28186935e688SClemens Ladisch 		case CM_CHIP_037:
28191da177e4SLinus Torvalds 			cm->chip_version = 37;
28201da177e4SLinus Torvalds 			cm->can_ac3_hw = 1;
2821133271feSClemens Ladisch 			break;
2822133271feSClemens Ladisch 		default:
2823133271feSClemens Ladisch 			cm->chip_version = 39;
2824133271feSClemens Ladisch 			cm->can_ac3_hw = 1;
2825133271feSClemens Ladisch 			break;
28261da177e4SLinus Torvalds 		}
2827133271feSClemens Ladisch 		cm->max_channels = 2;
28281da177e4SLinus Torvalds 	} else {
2829133271feSClemens Ladisch 		if (detect & CM_CHIP_039) {
28301da177e4SLinus Torvalds 			cm->chip_version = 39;
28311da177e4SLinus Torvalds 			if (detect & CM_CHIP_039_6CH) /* 4 or 6 channels */
28321da177e4SLinus Torvalds 				cm->max_channels = 6;
28331da177e4SLinus Torvalds 			else
28341da177e4SLinus Torvalds 				cm->max_channels = 4;
2835133271feSClemens Ladisch 		} else if (detect & CM_CHIP_8768) {
2836133271feSClemens Ladisch 			cm->chip_version = 68;
2837133271feSClemens Ladisch 			cm->max_channels = 8;
2838755c48abSTimofei Bondarenko 			cm->can_96k = 1;
2839133271feSClemens Ladisch 		} else {
2840133271feSClemens Ladisch 			cm->chip_version = 55;
2841133271feSClemens Ladisch 			cm->max_channels = 6;
2842755c48abSTimofei Bondarenko 			cm->can_96k = 1;
2843133271feSClemens Ladisch 		}
28441da177e4SLinus Torvalds 		cm->can_ac3_hw = 1;
28451da177e4SLinus Torvalds 		cm->can_multi_ch = 1;
28461da177e4SLinus Torvalds 	}
28471da177e4SLinus Torvalds }
28481da177e4SLinus Torvalds 
28491da177e4SLinus Torvalds #ifdef SUPPORT_JOYSTICK
28502cbdb686STakashi Iwai static int __devinit snd_cmipci_create_gameport(struct cmipci *cm, int dev)
28511da177e4SLinus Torvalds {
28521da177e4SLinus Torvalds 	static int ports[] = { 0x201, 0x200, 0 }; /* FIXME: majority is 0x201? */
28531da177e4SLinus Torvalds 	struct gameport *gp;
28541da177e4SLinus Torvalds 	struct resource *r = NULL;
28551da177e4SLinus Torvalds 	int i, io_port = 0;
28561da177e4SLinus Torvalds 
28571da177e4SLinus Torvalds 	if (joystick_port[dev] == 0)
28581da177e4SLinus Torvalds 		return -ENODEV;
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds 	if (joystick_port[dev] == 1) { /* auto-detect */
28611da177e4SLinus Torvalds 		for (i = 0; ports[i]; i++) {
28621da177e4SLinus Torvalds 			io_port = ports[i];
28631da177e4SLinus Torvalds 			r = request_region(io_port, 1, "CMIPCI gameport");
28641da177e4SLinus Torvalds 			if (r)
28651da177e4SLinus Torvalds 				break;
28661da177e4SLinus Torvalds 		}
28671da177e4SLinus Torvalds 	} else {
28681da177e4SLinus Torvalds 		io_port = joystick_port[dev];
28691da177e4SLinus Torvalds 		r = request_region(io_port, 1, "CMIPCI gameport");
28701da177e4SLinus Torvalds 	}
28711da177e4SLinus Torvalds 
28721da177e4SLinus Torvalds 	if (!r) {
28731da177e4SLinus Torvalds 		printk(KERN_WARNING "cmipci: cannot reserve joystick ports\n");
28741da177e4SLinus Torvalds 		return -EBUSY;
28751da177e4SLinus Torvalds 	}
28761da177e4SLinus Torvalds 
28771da177e4SLinus Torvalds 	cm->gameport = gp = gameport_allocate_port();
28781da177e4SLinus Torvalds 	if (!gp) {
28791da177e4SLinus Torvalds 		printk(KERN_ERR "cmipci: cannot allocate memory for gameport\n");
2880b1d5776dSTakashi Iwai 		release_and_free_resource(r);
28811da177e4SLinus Torvalds 		return -ENOMEM;
28821da177e4SLinus Torvalds 	}
28831da177e4SLinus Torvalds 	gameport_set_name(gp, "C-Media Gameport");
28841da177e4SLinus Torvalds 	gameport_set_phys(gp, "pci%s/gameport0", pci_name(cm->pci));
28851da177e4SLinus Torvalds 	gameport_set_dev_parent(gp, &cm->pci->dev);
28861da177e4SLinus Torvalds 	gp->io = io_port;
28871da177e4SLinus Torvalds 	gameport_set_port_data(gp, r);
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 	snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
28901da177e4SLinus Torvalds 
28911da177e4SLinus Torvalds 	gameport_register_port(cm->gameport);
28921da177e4SLinus Torvalds 
28931da177e4SLinus Torvalds 	return 0;
28941da177e4SLinus Torvalds }
28951da177e4SLinus Torvalds 
28962cbdb686STakashi Iwai static void snd_cmipci_free_gameport(struct cmipci *cm)
28971da177e4SLinus Torvalds {
28981da177e4SLinus Torvalds 	if (cm->gameport) {
28991da177e4SLinus Torvalds 		struct resource *r = gameport_get_port_data(cm->gameport);
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds 		gameport_unregister_port(cm->gameport);
29021da177e4SLinus Torvalds 		cm->gameport = NULL;
29031da177e4SLinus Torvalds 
29041da177e4SLinus Torvalds 		snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
2905b1d5776dSTakashi Iwai 		release_and_free_resource(r);
29061da177e4SLinus Torvalds 	}
29071da177e4SLinus Torvalds }
29081da177e4SLinus Torvalds #else
29092cbdb686STakashi Iwai static inline int snd_cmipci_create_gameport(struct cmipci *cm, int dev) { return -ENOSYS; }
29102cbdb686STakashi Iwai static inline void snd_cmipci_free_gameport(struct cmipci *cm) { }
29111da177e4SLinus Torvalds #endif
29121da177e4SLinus Torvalds 
29132cbdb686STakashi Iwai static int snd_cmipci_free(struct cmipci *cm)
29141da177e4SLinus Torvalds {
29151da177e4SLinus Torvalds 	if (cm->irq >= 0) {
29161da177e4SLinus Torvalds 		snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
29171da177e4SLinus Torvalds 		snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT);
29181da177e4SLinus Torvalds 		snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);  /* disable ints */
29191da177e4SLinus Torvalds 		snd_cmipci_ch_reset(cm, CM_CH_PLAY);
29201da177e4SLinus Torvalds 		snd_cmipci_ch_reset(cm, CM_CH_CAPT);
29211da177e4SLinus Torvalds 		snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
29221da177e4SLinus Torvalds 		snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
29231da177e4SLinus Torvalds 
29241da177e4SLinus Torvalds 		/* reset mixer */
29251da177e4SLinus Torvalds 		snd_cmipci_mixer_write(cm, 0, 0);
29261da177e4SLinus Torvalds 
29271da177e4SLinus Torvalds 		synchronize_irq(cm->irq);
29281da177e4SLinus Torvalds 
29292cbdb686STakashi Iwai 		free_irq(cm->irq, cm);
29301da177e4SLinus Torvalds 	}
29311da177e4SLinus Torvalds 
29321da177e4SLinus Torvalds 	snd_cmipci_free_gameport(cm);
29331da177e4SLinus Torvalds 	pci_release_regions(cm->pci);
29341da177e4SLinus Torvalds 	pci_disable_device(cm->pci);
29351da177e4SLinus Torvalds 	kfree(cm);
29361da177e4SLinus Torvalds 	return 0;
29371da177e4SLinus Torvalds }
29381da177e4SLinus Torvalds 
29392cbdb686STakashi Iwai static int snd_cmipci_dev_free(struct snd_device *device)
29401da177e4SLinus Torvalds {
29412cbdb686STakashi Iwai 	struct cmipci *cm = device->device_data;
29421da177e4SLinus Torvalds 	return snd_cmipci_free(cm);
29431da177e4SLinus Torvalds }
29441da177e4SLinus Torvalds 
29452cbdb686STakashi Iwai static int __devinit snd_cmipci_create_fm(struct cmipci *cm, long fm_port)
29465747e540SClemens Ladisch {
29475747e540SClemens Ladisch 	long iosynth;
29485747e540SClemens Ladisch 	unsigned int val;
29492cbdb686STakashi Iwai 	struct snd_opl3 *opl3;
29505747e540SClemens Ladisch 	int err;
29515747e540SClemens Ladisch 
29522f24d159STakashi Iwai 	if (!fm_port)
29532f24d159STakashi Iwai 		goto disable_fm;
29542f24d159STakashi Iwai 
2955c78c950dSClemens Ladisch 	if (cm->chip_version >= 39) {
29565747e540SClemens Ladisch 		/* first try FM regs in PCI port range */
29575747e540SClemens Ladisch 		iosynth = cm->iobase + CM_REG_FM_PCI;
29585747e540SClemens Ladisch 		err = snd_opl3_create(cm->card, iosynth, iosynth + 2,
29595747e540SClemens Ladisch 				      OPL3_HW_OPL3, 1, &opl3);
296045c41b48SClemens Ladisch 	} else {
296145c41b48SClemens Ladisch 		err = -EIO;
296245c41b48SClemens Ladisch 	}
29635747e540SClemens Ladisch 	if (err < 0) {
29645747e540SClemens Ladisch 		/* then try legacy ports */
29655747e540SClemens Ladisch 		val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL) & ~CM_FMSEL_MASK;
29665747e540SClemens Ladisch 		iosynth = fm_port;
29675747e540SClemens Ladisch 		switch (iosynth) {
29685747e540SClemens Ladisch 		case 0x3E8: val |= CM_FMSEL_3E8; break;
29695747e540SClemens Ladisch 		case 0x3E0: val |= CM_FMSEL_3E0; break;
29705747e540SClemens Ladisch 		case 0x3C8: val |= CM_FMSEL_3C8; break;
29715747e540SClemens Ladisch 		case 0x388: val |= CM_FMSEL_388; break;
29725747e540SClemens Ladisch 		default:
29732f24d159STakashi Iwai 			goto disable_fm;
29745747e540SClemens Ladisch 		}
29755747e540SClemens Ladisch 		snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
29765747e540SClemens Ladisch 		/* enable FM */
29775747e540SClemens Ladisch 		snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
29785747e540SClemens Ladisch 
29795747e540SClemens Ladisch 		if (snd_opl3_create(cm->card, iosynth, iosynth + 2,
29805747e540SClemens Ladisch 				    OPL3_HW_OPL3, 0, &opl3) < 0) {
29815747e540SClemens Ladisch 			printk(KERN_ERR "cmipci: no OPL device at %#lx, "
29825747e540SClemens Ladisch 			       "skipping...\n", iosynth);
29832f24d159STakashi Iwai 			goto disable_fm;
29845747e540SClemens Ladisch 		}
29855747e540SClemens Ladisch 	}
29865747e540SClemens Ladisch 	if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
29875747e540SClemens Ladisch 		printk(KERN_ERR "cmipci: cannot create OPL3 hwdep\n");
29885747e540SClemens Ladisch 		return err;
29895747e540SClemens Ladisch 	}
29905747e540SClemens Ladisch 	return 0;
29912f24d159STakashi Iwai 
29922f24d159STakashi Iwai  disable_fm:
29932f24d159STakashi Iwai 	snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_FMSEL_MASK);
29942f24d159STakashi Iwai 	snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
29952f24d159STakashi Iwai 	return 0;
29965747e540SClemens Ladisch }
29975747e540SClemens Ladisch 
29982cbdb686STakashi Iwai static int __devinit snd_cmipci_create(struct snd_card *card, struct pci_dev *pci,
29992cbdb686STakashi Iwai 				       int dev, struct cmipci **rcmipci)
30001da177e4SLinus Torvalds {
30012cbdb686STakashi Iwai 	struct cmipci *cm;
30021da177e4SLinus Torvalds 	int err;
30032cbdb686STakashi Iwai 	static struct snd_device_ops ops = {
30041da177e4SLinus Torvalds 		.dev_free =	snd_cmipci_dev_free,
30051da177e4SLinus Torvalds 	};
3006d6426257SClemens Ladisch 	unsigned int val;
30075747e540SClemens Ladisch 	long iomidi;
3008c9116ae4SClemens Ladisch 	int integrated_midi = 0;
3009b7e054a7SClemens Ladisch 	char modelstr[16];
30101da177e4SLinus Torvalds 	int pcm_index, pcm_spdif_index;
30111da177e4SLinus Torvalds 	static struct pci_device_id intel_82437vx[] = {
30121da177e4SLinus Torvalds 		{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437VX) },
30131da177e4SLinus Torvalds 		{ },
30141da177e4SLinus Torvalds 	};
30151da177e4SLinus Torvalds 
30161da177e4SLinus Torvalds 	*rcmipci = NULL;
30171da177e4SLinus Torvalds 
30181da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
30191da177e4SLinus Torvalds 		return err;
30201da177e4SLinus Torvalds 
3021e560d8d8STakashi Iwai 	cm = kzalloc(sizeof(*cm), GFP_KERNEL);
30221da177e4SLinus Torvalds 	if (cm == NULL) {
30231da177e4SLinus Torvalds 		pci_disable_device(pci);
30241da177e4SLinus Torvalds 		return -ENOMEM;
30251da177e4SLinus Torvalds 	}
30261da177e4SLinus Torvalds 
30271da177e4SLinus Torvalds 	spin_lock_init(&cm->reg_lock);
302862932df8SIngo Molnar 	mutex_init(&cm->open_mutex);
30291da177e4SLinus Torvalds 	cm->device = pci->device;
30301da177e4SLinus Torvalds 	cm->card = card;
30311da177e4SLinus Torvalds 	cm->pci = pci;
30321da177e4SLinus Torvalds 	cm->irq = -1;
30331da177e4SLinus Torvalds 	cm->channel[0].ch = 0;
30341da177e4SLinus Torvalds 	cm->channel[1].ch = 1;
30351da177e4SLinus Torvalds 	cm->channel[0].is_dac = cm->channel[1].is_dac = 1; /* dual DAC mode */
30361da177e4SLinus Torvalds 
30371da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, card->driver)) < 0) {
30381da177e4SLinus Torvalds 		kfree(cm);
30391da177e4SLinus Torvalds 		pci_disable_device(pci);
30401da177e4SLinus Torvalds 		return err;
30411da177e4SLinus Torvalds 	}
30421da177e4SLinus Torvalds 	cm->iobase = pci_resource_start(pci, 0);
30431da177e4SLinus Torvalds 
30442cbdb686STakashi Iwai 	if (request_irq(pci->irq, snd_cmipci_interrupt,
3045437a5a46STakashi Iwai 			IRQF_SHARED, card->driver, cm)) {
304699b359baSTakashi Iwai 		snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
30471da177e4SLinus Torvalds 		snd_cmipci_free(cm);
30481da177e4SLinus Torvalds 		return -EBUSY;
30491da177e4SLinus Torvalds 	}
30501da177e4SLinus Torvalds 	cm->irq = pci->irq;
30511da177e4SLinus Torvalds 
30521da177e4SLinus Torvalds 	pci_set_master(cm->pci);
30531da177e4SLinus Torvalds 
30541da177e4SLinus Torvalds 	/*
30551da177e4SLinus Torvalds 	 * check chip version, max channels and capabilities
30561da177e4SLinus Torvalds 	 */
30571da177e4SLinus Torvalds 
30581da177e4SLinus Torvalds 	cm->chip_version = 0;
30591da177e4SLinus Torvalds 	cm->max_channels = 2;
30601da177e4SLinus Torvalds 	cm->do_soft_ac3 = soft_ac3[dev];
30611da177e4SLinus Torvalds 
30621da177e4SLinus Torvalds 	if (pci->device != PCI_DEVICE_ID_CMEDIA_CM8338A &&
30631da177e4SLinus Torvalds 	    pci->device != PCI_DEVICE_ID_CMEDIA_CM8338B)
30641da177e4SLinus Torvalds 		query_chip(cm);
30651da177e4SLinus Torvalds 	/* added -MCx suffix for chip supporting multi-channels */
30661da177e4SLinus Torvalds 	if (cm->can_multi_ch)
30671da177e4SLinus Torvalds 		sprintf(cm->card->driver + strlen(cm->card->driver),
30681da177e4SLinus Torvalds 			"-MC%d", cm->max_channels);
30691da177e4SLinus Torvalds 	else if (cm->can_ac3_sw)
30701da177e4SLinus Torvalds 		strcpy(cm->card->driver + strlen(cm->card->driver), "-SWIEC");
30711da177e4SLinus Torvalds 
30721da177e4SLinus Torvalds 	cm->dig_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
30731da177e4SLinus Torvalds 	cm->dig_pcm_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
30741da177e4SLinus Torvalds 
30751da177e4SLinus Torvalds #if CM_CH_PLAY == 1
30761da177e4SLinus Torvalds 	cm->ctrl = CM_CHADC0;	/* default FUNCNTRL0 */
30771da177e4SLinus Torvalds #else
30781da177e4SLinus Torvalds 	cm->ctrl = CM_CHADC1;	/* default FUNCNTRL0 */
30791da177e4SLinus Torvalds #endif
30801da177e4SLinus Torvalds 
30811da177e4SLinus Torvalds 	/* initialize codec registers */
30823042ef75SClemens Ladisch 	snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
30833042ef75SClemens Ladisch 	snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_RESET);
30841da177e4SLinus Torvalds 	snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);	/* disable ints */
30851da177e4SLinus Torvalds 	snd_cmipci_ch_reset(cm, CM_CH_PLAY);
30861da177e4SLinus Torvalds 	snd_cmipci_ch_reset(cm, CM_CH_CAPT);
30871da177e4SLinus Torvalds 	snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0);	/* disable channels */
30881da177e4SLinus Torvalds 	snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
30891da177e4SLinus Torvalds 
30901da177e4SLinus Torvalds 	snd_cmipci_write(cm, CM_REG_CHFORMAT, 0);
30911da177e4SLinus Torvalds 	snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC|CM_N4SPK3D);
30921da177e4SLinus Torvalds #if CM_CH_PLAY == 1
30931da177e4SLinus Torvalds 	snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
30941da177e4SLinus Torvalds #else
30951da177e4SLinus Torvalds 	snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
30961da177e4SLinus Torvalds #endif
30974ee72717SClemens Ladisch 	if (cm->chip_version) {
30984ee72717SClemens Ladisch 		snd_cmipci_write_b(cm, CM_REG_EXT_MISC, 0x20); /* magic */
30994ee72717SClemens Ladisch 		snd_cmipci_write_b(cm, CM_REG_EXT_MISC + 1, 0x09); /* more magic */
31004ee72717SClemens Ladisch 	}
31011da177e4SLinus Torvalds 	/* Set Bus Master Request */
31021da177e4SLinus Torvalds 	snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_BREQ);
31031da177e4SLinus Torvalds 
31041da177e4SLinus Torvalds 	/* Assume TX and compatible chip set (Autodetection required for VX chip sets) */
31051da177e4SLinus Torvalds 	switch (pci->device) {
31061da177e4SLinus Torvalds 	case PCI_DEVICE_ID_CMEDIA_CM8738:
31071da177e4SLinus Torvalds 	case PCI_DEVICE_ID_CMEDIA_CM8738B:
31081da177e4SLinus Torvalds 		if (!pci_dev_present(intel_82437vx))
31091da177e4SLinus Torvalds 			snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_TXVX);
31101da177e4SLinus Torvalds 		break;
31111da177e4SLinus Torvalds 	default:
31121da177e4SLinus Torvalds 		break;
31131da177e4SLinus Torvalds 	}
31141da177e4SLinus Torvalds 
3115d6426257SClemens Ladisch 	if (cm->chip_version < 68) {
3116d6426257SClemens Ladisch 		val = pci->device < 0x110 ? 8338 : 8738;
3117d6426257SClemens Ladisch 	} else {
3118d6426257SClemens Ladisch 		switch (snd_cmipci_read_b(cm, CM_REG_INT_HLDCLR + 3) & 0x03) {
3119d6426257SClemens Ladisch 		case 0:
3120d6426257SClemens Ladisch 			val = 8769;
3121d6426257SClemens Ladisch 			break;
3122d6426257SClemens Ladisch 		case 2:
3123d6426257SClemens Ladisch 			val = 8762;
3124d6426257SClemens Ladisch 			break;
3125d6426257SClemens Ladisch 		default:
3126d6426257SClemens Ladisch 			switch ((pci->subsystem_vendor << 16) |
3127d6426257SClemens Ladisch 				pci->subsystem_device) {
3128d6426257SClemens Ladisch 			case 0x13f69761:
3129d6426257SClemens Ladisch 			case 0x584d3741:
3130d6426257SClemens Ladisch 			case 0x584d3751:
3131d6426257SClemens Ladisch 			case 0x584d3761:
3132d6426257SClemens Ladisch 			case 0x584d3771:
3133d6426257SClemens Ladisch 			case 0x72848384:
3134d6426257SClemens Ladisch 				val = 8770;
3135d6426257SClemens Ladisch 				break;
3136d6426257SClemens Ladisch 			default:
3137d6426257SClemens Ladisch 				val = 8768;
3138d6426257SClemens Ladisch 				break;
3139d6426257SClemens Ladisch 			}
3140d6426257SClemens Ladisch 		}
3141d6426257SClemens Ladisch 	}
3142b7e054a7SClemens Ladisch 	sprintf(card->shortname, "C-Media CMI%d", val);
3143b7e054a7SClemens Ladisch 	if (cm->chip_version < 68)
3144b7e054a7SClemens Ladisch 		sprintf(modelstr, " (model %d)", cm->chip_version);
3145b7e054a7SClemens Ladisch 	else
3146b7e054a7SClemens Ladisch 		modelstr[0] = '\0';
3147b7e054a7SClemens Ladisch 	sprintf(card->longname, "%s%s at %#lx, irq %i",
3148b7e054a7SClemens Ladisch 		card->shortname, modelstr, cm->iobase, cm->irq);
31491e02d6eaSClemens Ladisch 
31501da177e4SLinus Torvalds 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, cm, &ops)) < 0) {
31511da177e4SLinus Torvalds 		snd_cmipci_free(cm);
31521da177e4SLinus Torvalds 		return err;
31531da177e4SLinus Torvalds 	}
31541da177e4SLinus Torvalds 
3155c78c950dSClemens Ladisch 	if (cm->chip_version >= 39) {
3156c9116ae4SClemens Ladisch 		val = snd_cmipci_read_b(cm, CM_REG_MPU_PCI + 1);
3157c9116ae4SClemens Ladisch 		if (val != 0x00 && val != 0xff) {
31585747e540SClemens Ladisch 			iomidi = cm->iobase + CM_REG_MPU_PCI;
3159c9116ae4SClemens Ladisch 			integrated_midi = 1;
3160c9116ae4SClemens Ladisch 		}
3161c9116ae4SClemens Ladisch 	}
3162c9116ae4SClemens Ladisch 	if (!integrated_midi) {
3163c78c950dSClemens Ladisch 		val = 0;
31645747e540SClemens Ladisch 		iomidi = mpu_port[dev];
31651da177e4SLinus Torvalds 		switch (iomidi) {
31661da177e4SLinus Torvalds 		case 0x320: val = CM_VMPU_320; break;
31671da177e4SLinus Torvalds 		case 0x310: val = CM_VMPU_310; break;
31681da177e4SLinus Torvalds 		case 0x300: val = CM_VMPU_300; break;
31691da177e4SLinus Torvalds 		case 0x330: val = CM_VMPU_330; break;
31701da177e4SLinus Torvalds 		default:
31711da177e4SLinus Torvalds 			    iomidi = 0; break;
31721da177e4SLinus Torvalds 		}
31731da177e4SLinus Torvalds 		if (iomidi > 0) {
31741da177e4SLinus Torvalds 			snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
31751da177e4SLinus Torvalds 			/* enable UART */
31761da177e4SLinus Torvalds 			snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_UART_EN);
317788039815SClemens Ladisch 			if (inb(iomidi + 1) == 0xff) {
317888039815SClemens Ladisch 				snd_printk(KERN_ERR "cannot enable MPU-401 port"
317988039815SClemens Ladisch 					   " at %#lx\n", iomidi);
318088039815SClemens Ladisch 				snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1,
318188039815SClemens Ladisch 						     CM_UART_EN);
318288039815SClemens Ladisch 				iomidi = 0;
318388039815SClemens Ladisch 			}
31841da177e4SLinus Torvalds 		}
31851da177e4SLinus Torvalds 	}
31861da177e4SLinus Torvalds 
318745c41b48SClemens Ladisch 	if (cm->chip_version < 68) {
318845c41b48SClemens Ladisch 		err = snd_cmipci_create_fm(cm, fm_port[dev]);
318945c41b48SClemens Ladisch 		if (err < 0)
31901da177e4SLinus Torvalds 			return err;
319145c41b48SClemens Ladisch 	}
31921da177e4SLinus Torvalds 
31931da177e4SLinus Torvalds 	/* reset mixer */
31941da177e4SLinus Torvalds 	snd_cmipci_mixer_write(cm, 0, 0);
31951da177e4SLinus Torvalds 
31961da177e4SLinus Torvalds 	snd_cmipci_proc_init(cm);
31971da177e4SLinus Torvalds 
31981da177e4SLinus Torvalds 	/* create pcm devices */
31991da177e4SLinus Torvalds 	pcm_index = pcm_spdif_index = 0;
32001da177e4SLinus Torvalds 	if ((err = snd_cmipci_pcm_new(cm, pcm_index)) < 0)
32011da177e4SLinus Torvalds 		return err;
32021da177e4SLinus Torvalds 	pcm_index++;
32031da177e4SLinus Torvalds 	if ((err = snd_cmipci_pcm2_new(cm, pcm_index)) < 0)
32041da177e4SLinus Torvalds 		return err;
32051da177e4SLinus Torvalds 	pcm_index++;
32061da177e4SLinus Torvalds 	if (cm->can_ac3_hw || cm->can_ac3_sw) {
32071da177e4SLinus Torvalds 		pcm_spdif_index = pcm_index;
32081da177e4SLinus Torvalds 		if ((err = snd_cmipci_pcm_spdif_new(cm, pcm_index)) < 0)
32091da177e4SLinus Torvalds 			return err;
32101da177e4SLinus Torvalds 	}
32111da177e4SLinus Torvalds 
32121da177e4SLinus Torvalds 	/* create mixer interface & switches */
32131da177e4SLinus Torvalds 	if ((err = snd_cmipci_mixer_new(cm, pcm_spdif_index)) < 0)
32141da177e4SLinus Torvalds 		return err;
32151da177e4SLinus Torvalds 
32161da177e4SLinus Torvalds 	if (iomidi > 0) {
32171da177e4SLinus Torvalds 		if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
3218302e4c2fSTakashi Iwai 					       iomidi,
3219302e4c2fSTakashi Iwai 					       (integrated_midi ?
3220302e4c2fSTakashi Iwai 						MPU401_INFO_INTEGRATED : 0),
32211da177e4SLinus Torvalds 					       cm->irq, 0, &cm->rmidi)) < 0) {
32221da177e4SLinus Torvalds 			printk(KERN_ERR "cmipci: no UART401 device at 0x%lx\n", iomidi);
32231da177e4SLinus Torvalds 		}
32241da177e4SLinus Torvalds 	}
32251da177e4SLinus Torvalds 
32261da177e4SLinus Torvalds #ifdef USE_VAR48KRATE
32271da177e4SLinus Torvalds 	for (val = 0; val < ARRAY_SIZE(rates); val++)
32281da177e4SLinus Torvalds 		snd_cmipci_set_pll(cm, rates[val], val);
32291da177e4SLinus Torvalds 
32301da177e4SLinus Torvalds 	/*
32311da177e4SLinus Torvalds 	 * (Re-)Enable external switch spdo_48k
32321da177e4SLinus Torvalds 	 */
32331da177e4SLinus Torvalds 	snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K|CM_SPDF_AC97);
32341da177e4SLinus Torvalds #endif /* USE_VAR48KRATE */
32351da177e4SLinus Torvalds 
32361da177e4SLinus Torvalds 	if (snd_cmipci_create_gameport(cm, dev) < 0)
32371da177e4SLinus Torvalds 		snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
32381da177e4SLinus Torvalds 
32391da177e4SLinus Torvalds 	snd_card_set_dev(card, &pci->dev);
32401da177e4SLinus Torvalds 
32411da177e4SLinus Torvalds 	*rcmipci = cm;
32421da177e4SLinus Torvalds 	return 0;
32431da177e4SLinus Torvalds }
32441da177e4SLinus Torvalds 
32451da177e4SLinus Torvalds /*
32461da177e4SLinus Torvalds  */
32471da177e4SLinus Torvalds 
32481da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_cmipci_ids);
32491da177e4SLinus Torvalds 
32501da177e4SLinus Torvalds static int __devinit snd_cmipci_probe(struct pci_dev *pci,
32511da177e4SLinus Torvalds 				      const struct pci_device_id *pci_id)
32521da177e4SLinus Torvalds {
32531da177e4SLinus Torvalds 	static int dev;
32542cbdb686STakashi Iwai 	struct snd_card *card;
32552cbdb686STakashi Iwai 	struct cmipci *cm;
32561da177e4SLinus Torvalds 	int err;
32571da177e4SLinus Torvalds 
32581da177e4SLinus Torvalds 	if (dev >= SNDRV_CARDS)
32591da177e4SLinus Torvalds 		return -ENODEV;
32601da177e4SLinus Torvalds 	if (! enable[dev]) {
32611da177e4SLinus Torvalds 		dev++;
32621da177e4SLinus Torvalds 		return -ENOENT;
32631da177e4SLinus Torvalds 	}
32641da177e4SLinus Torvalds 
32651da177e4SLinus Torvalds 	card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
32661da177e4SLinus Torvalds 	if (card == NULL)
32671da177e4SLinus Torvalds 		return -ENOMEM;
32681da177e4SLinus Torvalds 
32691da177e4SLinus Torvalds 	switch (pci->device) {
32701da177e4SLinus Torvalds 	case PCI_DEVICE_ID_CMEDIA_CM8738:
32711da177e4SLinus Torvalds 	case PCI_DEVICE_ID_CMEDIA_CM8738B:
32721da177e4SLinus Torvalds 		strcpy(card->driver, "CMI8738");
32731da177e4SLinus Torvalds 		break;
32741da177e4SLinus Torvalds 	case PCI_DEVICE_ID_CMEDIA_CM8338A:
32751da177e4SLinus Torvalds 	case PCI_DEVICE_ID_CMEDIA_CM8338B:
32761da177e4SLinus Torvalds 		strcpy(card->driver, "CMI8338");
32771da177e4SLinus Torvalds 		break;
32781da177e4SLinus Torvalds 	default:
32791da177e4SLinus Torvalds 		strcpy(card->driver, "CMIPCI");
32801da177e4SLinus Torvalds 		break;
32811da177e4SLinus Torvalds 	}
32821da177e4SLinus Torvalds 
32831da177e4SLinus Torvalds 	if ((err = snd_cmipci_create(card, pci, dev, &cm)) < 0) {
32841da177e4SLinus Torvalds 		snd_card_free(card);
32851da177e4SLinus Torvalds 		return err;
32861da177e4SLinus Torvalds 	}
3287cb60e5f5STakashi Iwai 	card->private_data = cm;
32881da177e4SLinus Torvalds 
32891da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
32901da177e4SLinus Torvalds 		snd_card_free(card);
32911da177e4SLinus Torvalds 		return err;
32921da177e4SLinus Torvalds 	}
32931da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
32941da177e4SLinus Torvalds 	dev++;
32951da177e4SLinus Torvalds 	return 0;
32961da177e4SLinus Torvalds 
32971da177e4SLinus Torvalds }
32981da177e4SLinus Torvalds 
32991da177e4SLinus Torvalds static void __devexit snd_cmipci_remove(struct pci_dev *pci)
33001da177e4SLinus Torvalds {
33011da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
33021da177e4SLinus Torvalds 	pci_set_drvdata(pci, NULL);
33031da177e4SLinus Torvalds }
33041da177e4SLinus Torvalds 
33051da177e4SLinus Torvalds 
3306cb60e5f5STakashi Iwai #ifdef CONFIG_PM
3307cb60e5f5STakashi Iwai /*
3308cb60e5f5STakashi Iwai  * power management
3309cb60e5f5STakashi Iwai  */
3310cb60e5f5STakashi Iwai static unsigned char saved_regs[] = {
3311cb60e5f5STakashi Iwai 	CM_REG_FUNCTRL1, CM_REG_CHFORMAT, CM_REG_LEGACY_CTRL, CM_REG_MISC_CTRL,
3312cb60e5f5STakashi Iwai 	CM_REG_MIXER0, CM_REG_MIXER1, CM_REG_MIXER2, CM_REG_MIXER3, CM_REG_PLL,
3313cb60e5f5STakashi Iwai 	CM_REG_CH0_FRAME1, CM_REG_CH0_FRAME2,
3314cb60e5f5STakashi Iwai 	CM_REG_CH1_FRAME1, CM_REG_CH1_FRAME2, CM_REG_EXT_MISC,
3315cb60e5f5STakashi Iwai 	CM_REG_INT_STATUS, CM_REG_INT_HLDCLR, CM_REG_FUNCTRL0,
3316cb60e5f5STakashi Iwai };
3317cb60e5f5STakashi Iwai 
3318cb60e5f5STakashi Iwai static unsigned char saved_mixers[] = {
3319cb60e5f5STakashi Iwai 	SB_DSP4_MASTER_DEV, SB_DSP4_MASTER_DEV + 1,
3320cb60e5f5STakashi Iwai 	SB_DSP4_PCM_DEV, SB_DSP4_PCM_DEV + 1,
3321cb60e5f5STakashi Iwai 	SB_DSP4_SYNTH_DEV, SB_DSP4_SYNTH_DEV + 1,
3322cb60e5f5STakashi Iwai 	SB_DSP4_CD_DEV, SB_DSP4_CD_DEV + 1,
3323cb60e5f5STakashi Iwai 	SB_DSP4_LINE_DEV, SB_DSP4_LINE_DEV + 1,
3324cb60e5f5STakashi Iwai 	SB_DSP4_MIC_DEV, SB_DSP4_SPEAKER_DEV,
3325cb60e5f5STakashi Iwai 	CM_REG_EXTENT_IND, SB_DSP4_OUTPUT_SW,
3326cb60e5f5STakashi Iwai 	SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT,
3327cb60e5f5STakashi Iwai };
3328cb60e5f5STakashi Iwai 
3329cb60e5f5STakashi Iwai static int snd_cmipci_suspend(struct pci_dev *pci, pm_message_t state)
3330cb60e5f5STakashi Iwai {
3331cb60e5f5STakashi Iwai 	struct snd_card *card = pci_get_drvdata(pci);
3332cb60e5f5STakashi Iwai 	struct cmipci *cm = card->private_data;
3333cb60e5f5STakashi Iwai 	int i;
3334cb60e5f5STakashi Iwai 
3335cb60e5f5STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
3336cb60e5f5STakashi Iwai 
3337cb60e5f5STakashi Iwai 	snd_pcm_suspend_all(cm->pcm);
3338cb60e5f5STakashi Iwai 	snd_pcm_suspend_all(cm->pcm2);
3339cb60e5f5STakashi Iwai 	snd_pcm_suspend_all(cm->pcm_spdif);
3340cb60e5f5STakashi Iwai 
3341cb60e5f5STakashi Iwai 	/* save registers */
3342cb60e5f5STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
3343cb60e5f5STakashi Iwai 		cm->saved_regs[i] = snd_cmipci_read(cm, saved_regs[i]);
3344cb60e5f5STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
3345cb60e5f5STakashi Iwai 		cm->saved_mixers[i] = snd_cmipci_mixer_read(cm, saved_mixers[i]);
3346cb60e5f5STakashi Iwai 
3347cb60e5f5STakashi Iwai 	/* disable ints */
3348cb60e5f5STakashi Iwai 	snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
3349cb60e5f5STakashi Iwai 
3350cb60e5f5STakashi Iwai 	pci_disable_device(pci);
3351cb60e5f5STakashi Iwai 	pci_save_state(pci);
335230b35399STakashi Iwai 	pci_set_power_state(pci, pci_choose_state(pci, state));
3353cb60e5f5STakashi Iwai 	return 0;
3354cb60e5f5STakashi Iwai }
3355cb60e5f5STakashi Iwai 
3356cb60e5f5STakashi Iwai static int snd_cmipci_resume(struct pci_dev *pci)
3357cb60e5f5STakashi Iwai {
3358cb60e5f5STakashi Iwai 	struct snd_card *card = pci_get_drvdata(pci);
3359cb60e5f5STakashi Iwai 	struct cmipci *cm = card->private_data;
3360cb60e5f5STakashi Iwai 	int i;
3361cb60e5f5STakashi Iwai 
3362cb60e5f5STakashi Iwai 	pci_set_power_state(pci, PCI_D0);
336330b35399STakashi Iwai 	pci_restore_state(pci);
336430b35399STakashi Iwai 	if (pci_enable_device(pci) < 0) {
336530b35399STakashi Iwai 		printk(KERN_ERR "cmipci: pci_enable_device failed, "
336630b35399STakashi Iwai 		       "disabling device\n");
336730b35399STakashi Iwai 		snd_card_disconnect(card);
336830b35399STakashi Iwai 		return -EIO;
336930b35399STakashi Iwai 	}
3370cb60e5f5STakashi Iwai 	pci_set_master(pci);
3371cb60e5f5STakashi Iwai 
3372cb60e5f5STakashi Iwai 	/* reset / initialize to a sane state */
3373cb60e5f5STakashi Iwai 	snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0);
3374cb60e5f5STakashi Iwai 	snd_cmipci_ch_reset(cm, CM_CH_PLAY);
3375cb60e5f5STakashi Iwai 	snd_cmipci_ch_reset(cm, CM_CH_CAPT);
3376cb60e5f5STakashi Iwai 	snd_cmipci_mixer_write(cm, 0, 0);
3377cb60e5f5STakashi Iwai 
3378cb60e5f5STakashi Iwai 	/* restore registers */
3379cb60e5f5STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
3380cb60e5f5STakashi Iwai 		snd_cmipci_write(cm, saved_regs[i], cm->saved_regs[i]);
3381cb60e5f5STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_mixers); i++)
3382cb60e5f5STakashi Iwai 		snd_cmipci_mixer_write(cm, saved_mixers[i], cm->saved_mixers[i]);
3383cb60e5f5STakashi Iwai 
3384cb60e5f5STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
3385cb60e5f5STakashi Iwai 	return 0;
3386cb60e5f5STakashi Iwai }
3387cb60e5f5STakashi Iwai #endif /* CONFIG_PM */
3388cb60e5f5STakashi Iwai 
33891da177e4SLinus Torvalds static struct pci_driver driver = {
33901da177e4SLinus Torvalds 	.name = "C-Media PCI",
33911da177e4SLinus Torvalds 	.id_table = snd_cmipci_ids,
33921da177e4SLinus Torvalds 	.probe = snd_cmipci_probe,
33931da177e4SLinus Torvalds 	.remove = __devexit_p(snd_cmipci_remove),
3394cb60e5f5STakashi Iwai #ifdef CONFIG_PM
3395cb60e5f5STakashi Iwai 	.suspend = snd_cmipci_suspend,
3396cb60e5f5STakashi Iwai 	.resume = snd_cmipci_resume,
3397cb60e5f5STakashi Iwai #endif
33981da177e4SLinus Torvalds };
33991da177e4SLinus Torvalds 
34001da177e4SLinus Torvalds static int __init alsa_card_cmipci_init(void)
34011da177e4SLinus Torvalds {
340201d25d46STakashi Iwai 	return pci_register_driver(&driver);
34031da177e4SLinus Torvalds }
34041da177e4SLinus Torvalds 
34051da177e4SLinus Torvalds static void __exit alsa_card_cmipci_exit(void)
34061da177e4SLinus Torvalds {
34071da177e4SLinus Torvalds 	pci_unregister_driver(&driver);
34081da177e4SLinus Torvalds }
34091da177e4SLinus Torvalds 
34101da177e4SLinus Torvalds module_init(alsa_card_cmipci_init)
34111da177e4SLinus Torvalds module_exit(alsa_card_cmipci_exit)
3412