xref: /linux/sound/isa/opti9xx/opti92x-ad1848.c (revision 3ae5f36a368b143cfd8a6f7a988a012c1b46eda0)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds     card-opti92x-ad1848.c - driver for OPTi 82c92x based soundcards.
31da177e4SLinus Torvalds     Copyright (C) 1998-2000 by Massimo Piccioni <dafastidio@libero.it>
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds     Part of this code was developed at the Italian Ministry of Air Defence,
61da177e4SLinus Torvalds     Sixth Division (oh, che pace ...), Rome.
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds     Thanks to Maria Grazia Pollarini, Salvatore Vassallo.
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds     This program is free software; you can redistribute it and/or modify
111da177e4SLinus Torvalds     it under the terms of the GNU General Public License as published by
121da177e4SLinus Torvalds     the Free Software Foundation; either version 2 of the License, or
131da177e4SLinus Torvalds     (at your option) any later version.
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds     This program is distributed in the hope that it will be useful,
161da177e4SLinus Torvalds     but WITHOUT ANY WARRANTY; without even the implied warranty of
171da177e4SLinus Torvalds     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
181da177e4SLinus Torvalds     GNU General Public License for more details.
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds     You should have received a copy of the GNU General Public License
211da177e4SLinus Torvalds     along with this program; if not, write to the Free Software
221da177e4SLinus Torvalds     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
231da177e4SLinus Torvalds */
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <linux/init.h>
2799a0b768STakashi Iwai #include <linux/err.h>
285e24c1c1STakashi Iwai #include <linux/isa.h>
2999a0b768STakashi Iwai #include <linux/delay.h>
301da177e4SLinus Torvalds #include <linux/slab.h>
311da177e4SLinus Torvalds #include <linux/pnp.h>
321da177e4SLinus Torvalds #include <linux/moduleparam.h>
3399a0b768STakashi Iwai #include <asm/io.h>
3499a0b768STakashi Iwai #include <asm/dma.h>
351da177e4SLinus Torvalds #include <sound/core.h>
361da177e4SLinus Torvalds #ifdef CS4231
371da177e4SLinus Torvalds #include <sound/cs4231.h>
381da177e4SLinus Torvalds #else
391da177e4SLinus Torvalds #ifndef OPTi93X
401da177e4SLinus Torvalds #include <sound/ad1848.h>
411da177e4SLinus Torvalds #else
421da177e4SLinus Torvalds #include <sound/control.h>
431da177e4SLinus Torvalds #include <sound/pcm.h>
441da177e4SLinus Torvalds #endif	/* OPTi93X */
451da177e4SLinus Torvalds #endif	/* CS4231 */
461da177e4SLinus Torvalds #include <sound/mpu401.h>
471da177e4SLinus Torvalds #include <sound/opl3.h>
481da177e4SLinus Torvalds #ifndef OPTi93X
491da177e4SLinus Torvalds #include <sound/opl4.h>
501da177e4SLinus Torvalds #endif
511da177e4SLinus Torvalds #define SNDRV_LEGACY_FIND_FREE_IRQ
521da177e4SLinus Torvalds #define SNDRV_LEGACY_FIND_FREE_DMA
531da177e4SLinus Torvalds #include <sound/initval.h>
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
561da177e4SLinus Torvalds MODULE_LICENSE("GPL");
571da177e4SLinus Torvalds #ifdef OPTi93X
581da177e4SLinus Torvalds MODULE_DESCRIPTION("OPTi93X");
591da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{OPTi,82C931/3}}");
601da177e4SLinus Torvalds #else	/* OPTi93X */
611da177e4SLinus Torvalds #ifdef CS4231
621da177e4SLinus Torvalds MODULE_DESCRIPTION("OPTi92X - CS4231");
631da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (CS4231)},"
641da177e4SLinus Torvalds 		"{OPTi,82C925 (CS4231)}}");
651da177e4SLinus Torvalds #else	/* CS4231 */
661da177e4SLinus Torvalds MODULE_DESCRIPTION("OPTi92X - AD1848");
671da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (AD1848)},"
681da177e4SLinus Torvalds 		"{OPTi,82C925 (AD1848)},"
691da177e4SLinus Torvalds 	        "{OAK,Mozart}}");
701da177e4SLinus Torvalds #endif	/* CS4231 */
711da177e4SLinus Torvalds #endif	/* OPTi93X */
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds static int index = SNDRV_DEFAULT_IDX1;	/* Index 0-MAX */
741da177e4SLinus Torvalds static char *id = SNDRV_DEFAULT_STR1;		/* ID for this card */
751da177e4SLinus Torvalds //static int enable = SNDRV_DEFAULT_ENABLE1;	/* Enable this card */
761da177e4SLinus Torvalds static int isapnp = 1;			/* Enable ISA PnP detection */
771da177e4SLinus Torvalds static long port = SNDRV_DEFAULT_PORT1; 	/* 0x530,0xe80,0xf40,0x604 */
781da177e4SLinus Torvalds static long mpu_port = SNDRV_DEFAULT_PORT1;	/* 0x300,0x310,0x320,0x330 */
791da177e4SLinus Torvalds static long fm_port = SNDRV_DEFAULT_PORT1;	/* 0x388 */
801da177e4SLinus Torvalds static int irq = SNDRV_DEFAULT_IRQ1;		/* 5,7,9,10,11 */
811da177e4SLinus Torvalds static int mpu_irq = SNDRV_DEFAULT_IRQ1;	/* 5,7,9,10 */
821da177e4SLinus Torvalds static int dma1 = SNDRV_DEFAULT_DMA1;		/* 0,1,3 */
831da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
841da177e4SLinus Torvalds static int dma2 = SNDRV_DEFAULT_DMA1;		/* 0,1,3 */
851da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds module_param(index, int, 0444);
881da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard.");
891da177e4SLinus Torvalds module_param(id, charp, 0444);
901da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard.");
911da177e4SLinus Torvalds //module_param(enable, bool, 0444);
921da177e4SLinus Torvalds //MODULE_PARM_DESC(enable, "Enable opti9xx soundcard.");
931da177e4SLinus Torvalds module_param(isapnp, bool, 0444);
941da177e4SLinus Torvalds MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
951da177e4SLinus Torvalds module_param(port, long, 0444);
961da177e4SLinus Torvalds MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
971da177e4SLinus Torvalds module_param(mpu_port, long, 0444);
981da177e4SLinus Torvalds MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
991da177e4SLinus Torvalds module_param(fm_port, long, 0444);
1001da177e4SLinus Torvalds MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
1011da177e4SLinus Torvalds module_param(irq, int, 0444);
1021da177e4SLinus Torvalds MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
1031da177e4SLinus Torvalds module_param(mpu_irq, int, 0444);
1041da177e4SLinus Torvalds MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
1051da177e4SLinus Torvalds module_param(dma1, int, 0444);
1061da177e4SLinus Torvalds MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver.");
1071da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
1081da177e4SLinus Torvalds module_param(dma2, int, 0444);
1091da177e4SLinus Torvalds MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver.");
1101da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds #define OPTi9XX_HW_DETECT	0
1131da177e4SLinus Torvalds #define OPTi9XX_HW_82C928	1
1141da177e4SLinus Torvalds #define OPTi9XX_HW_82C929	2
1151da177e4SLinus Torvalds #define OPTi9XX_HW_82C924	3
1161da177e4SLinus Torvalds #define OPTi9XX_HW_82C925	4
1171da177e4SLinus Torvalds #define OPTi9XX_HW_82C930	5
1181da177e4SLinus Torvalds #define OPTi9XX_HW_82C931	6
1191da177e4SLinus Torvalds #define OPTi9XX_HW_82C933	7
1201da177e4SLinus Torvalds #define OPTi9XX_HW_LAST		OPTi9XX_HW_82C933
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds #define OPTi9XX_MC_REG(n)	n
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds #ifdef OPTi93X
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds #define OPTi93X_INDEX			0x00
1271da177e4SLinus Torvalds #define OPTi93X_DATA			0x01
1281da177e4SLinus Torvalds #define OPTi93X_STATUS			0x02
1291da177e4SLinus Torvalds #define OPTi93X_DDATA			0x03
1301da177e4SLinus Torvalds #define OPTi93X_PORT(chip, r)		((chip)->port + OPTi93X_##r)
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds #define OPTi93X_MIXOUT_LEFT		0x00
1331da177e4SLinus Torvalds #define OPTi93X_MIXOUT_RIGHT		0x01
1341da177e4SLinus Torvalds #define OPTi93X_CD_LEFT_INPUT		0x02
1351da177e4SLinus Torvalds #define OPTi93X_CD_RIGHT_INPUT		0x03
1361da177e4SLinus Torvalds #define OPTi930_AUX_LEFT_INPUT		0x04
1371da177e4SLinus Torvalds #define OPTi930_AUX_RIGHT_INPUT		0x05
1381da177e4SLinus Torvalds #define OPTi931_FM_LEFT_INPUT		0x04
1391da177e4SLinus Torvalds #define OPTi931_FM_RIGHT_INPUT		0x05
1401da177e4SLinus Torvalds #define OPTi93X_DAC_LEFT		0x06
1411da177e4SLinus Torvalds #define OPTi93X_DAC_RIGHT		0x07
1421da177e4SLinus Torvalds #define OPTi93X_PLAY_FORMAT		0x08
1431da177e4SLinus Torvalds #define OPTi93X_IFACE_CONF		0x09
1441da177e4SLinus Torvalds #define OPTi93X_PIN_CTRL		0x0a
1451da177e4SLinus Torvalds #define OPTi93X_ERR_INIT		0x0b
1461da177e4SLinus Torvalds #define OPTi93X_ID			0x0c
1471da177e4SLinus Torvalds #define OPTi93X_PLAY_UPR_CNT		0x0e
1481da177e4SLinus Torvalds #define OPTi93X_PLAY_LWR_CNT		0x0f
1491da177e4SLinus Torvalds #define OPTi931_AUX_LEFT_INPUT		0x10
1501da177e4SLinus Torvalds #define OPTi931_AUX_RIGHT_INPUT		0x11
1511da177e4SLinus Torvalds #define OPTi93X_LINE_LEFT_INPUT		0x12
1521da177e4SLinus Torvalds #define OPTi93X_LINE_RIGHT_INPUT	0x13
1531da177e4SLinus Torvalds #define OPTi93X_MIC_LEFT_INPUT		0x14
1541da177e4SLinus Torvalds #define OPTi93X_MIC_RIGHT_INPUT		0x15
1551da177e4SLinus Torvalds #define OPTi93X_OUT_LEFT		0x16
1561da177e4SLinus Torvalds #define OPTi93X_OUT_RIGHT		0x17
1571da177e4SLinus Torvalds #define OPTi93X_CAPT_FORMAT		0x1c
1581da177e4SLinus Torvalds #define OPTi93X_CAPT_UPR_CNT		0x1e
1591da177e4SLinus Torvalds #define OPTi93X_CAPT_LWR_CNT		0x1f
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds #define OPTi93X_TRD			0x20
1621da177e4SLinus Torvalds #define OPTi93X_MCE			0x40
1631da177e4SLinus Torvalds #define OPTi93X_INIT			0x80
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds #define OPTi93X_MIXOUT_MIC_GAIN		0x20
1661da177e4SLinus Torvalds #define OPTi93X_MIXOUT_LINE		0x00
1671da177e4SLinus Torvalds #define OPTi93X_MIXOUT_CD		0x40
1681da177e4SLinus Torvalds #define OPTi93X_MIXOUT_MIC		0x80
1691da177e4SLinus Torvalds #define OPTi93X_MIXOUT_MIXER		0xc0
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds #define OPTi93X_STEREO			0x10
1721da177e4SLinus Torvalds #define OPTi93X_LINEAR_8		0x00
1731da177e4SLinus Torvalds #define OPTi93X_ULAW_8			0x20
1741da177e4SLinus Torvalds #define OPTi93X_LINEAR_16_LIT		0x40
1751da177e4SLinus Torvalds #define OPTi93X_ALAW_8			0x60
1761da177e4SLinus Torvalds #define OPTi93X_ADPCM_16		0xa0
1771da177e4SLinus Torvalds #define OPTi93X_LINEAR_16_BIG		0xc0
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds #define OPTi93X_CAPTURE_PIO		0x80
1801da177e4SLinus Torvalds #define OPTi93X_PLAYBACK_PIO		0x40
1811da177e4SLinus Torvalds #define OPTi93X_AUTOCALIB		0x08
1821da177e4SLinus Torvalds #define OPTi93X_SINGLE_DMA		0x04
1831da177e4SLinus Torvalds #define OPTi93X_CAPTURE_ENABLE		0x02
1841da177e4SLinus Torvalds #define OPTi93X_PLAYBACK_ENABLE		0x01
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds #define OPTi93X_IRQ_ENABLE		0x02
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds #define OPTi93X_DMA_REQUEST		0x10
1891da177e4SLinus Torvalds #define OPTi93X_CALIB_IN_PROGRESS	0x20
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds #define OPTi93X_IRQ_PLAYBACK		0x04
1921da177e4SLinus Torvalds #define OPTi93X_IRQ_CAPTURE		0x08
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 
195346c7a68STakashi Iwai struct snd_opti93x {
1961da177e4SLinus Torvalds 	unsigned long port;
1971da177e4SLinus Torvalds 	struct resource *res_port;
1981da177e4SLinus Torvalds 	int irq;
1991da177e4SLinus Torvalds 	int dma1;
2001da177e4SLinus Torvalds 	int dma2;
2011da177e4SLinus Torvalds 
202346c7a68STakashi Iwai 	struct snd_opti9xx *chip;
2031da177e4SLinus Torvalds 	unsigned short hardware;
2041da177e4SLinus Torvalds 	unsigned char image[32];
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	unsigned char mce_bit;
2071da177e4SLinus Torvalds 	unsigned short mode;
2081da177e4SLinus Torvalds 	int mute;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	spinlock_t lock;
2111da177e4SLinus Torvalds 
212346c7a68STakashi Iwai 	struct snd_card *card;
213346c7a68STakashi Iwai 	struct snd_pcm *pcm;
214346c7a68STakashi Iwai 	struct snd_pcm_substream *playback_substream;
215346c7a68STakashi Iwai 	struct snd_pcm_substream *capture_substream;
2161da177e4SLinus Torvalds 	unsigned int p_dma_size;
2171da177e4SLinus Torvalds 	unsigned int c_dma_size;
2181da177e4SLinus Torvalds };
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds #define OPTi93X_MODE_NONE	0x00
2211da177e4SLinus Torvalds #define OPTi93X_MODE_PLAY	0x01
2221da177e4SLinus Torvalds #define OPTi93X_MODE_CAPTURE	0x02
2231da177e4SLinus Torvalds #define OPTi93X_MODE_OPEN	(OPTi93X_MODE_PLAY | OPTi93X_MODE_CAPTURE)
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds #endif /* OPTi93X */
2261da177e4SLinus Torvalds 
227346c7a68STakashi Iwai struct snd_opti9xx {
2281da177e4SLinus Torvalds 	unsigned short hardware;
2291da177e4SLinus Torvalds 	unsigned char password;
2301da177e4SLinus Torvalds 	char name[7];
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	unsigned long mc_base;
2331da177e4SLinus Torvalds 	struct resource *res_mc_base;
2341da177e4SLinus Torvalds 	unsigned long mc_base_size;
2351da177e4SLinus Torvalds #ifdef OPTi93X
2361da177e4SLinus Torvalds 	unsigned long mc_indir_index;
2371da177e4SLinus Torvalds #endif	/* OPTi93X */
2381da177e4SLinus Torvalds 	unsigned long pwd_reg;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	spinlock_t lock;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	long wss_base;
2431da177e4SLinus Torvalds 	int irq;
2441da177e4SLinus Torvalds 	int dma1;
2451da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
2461da177e4SLinus Torvalds 	int dma2;
2471da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	long fm_port;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	long mpu_port;
2521da177e4SLinus Torvalds 	int mpu_irq;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds #ifdef CONFIG_PNP
2551da177e4SLinus Torvalds 	struct pnp_dev *dev;
2561da177e4SLinus Torvalds 	struct pnp_dev *devmpu;
2571da177e4SLinus Torvalds #endif	/* CONFIG_PNP */
2581da177e4SLinus Torvalds };
2591da177e4SLinus Torvalds 
26099a0b768STakashi Iwai static int snd_opti9xx_pnp_is_probed;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds #ifdef CONFIG_PNP
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds static struct pnp_card_device_id snd_opti9xx_pnpids[] = {
2651da177e4SLinus Torvalds #ifndef OPTi93X
2661da177e4SLinus Torvalds 	/* OPTi 82C924 */
2671da177e4SLinus Torvalds 	{ .id = "OPT0924", .devs = { { "OPT0000" }, { "OPT0002" } }, .driver_data = 0x0924 },
2681da177e4SLinus Torvalds 	/* OPTi 82C925 */
2691da177e4SLinus Torvalds 	{ .id = "OPT0925", .devs = { { "OPT9250" }, { "OPT0002" } }, .driver_data = 0x0925 },
2701da177e4SLinus Torvalds #else
2711da177e4SLinus Torvalds 	/* OPTi 82C931/3 */
2721da177e4SLinus Torvalds 	{ .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } }, .driver_data = 0x0931 },
2731da177e4SLinus Torvalds #endif	/* OPTi93X */
2741da177e4SLinus Torvalds 	{ .id = "" }
2751da177e4SLinus Torvalds };
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds #endif	/* CONFIG_PNP */
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds #ifdef OPTi93X
28283c51c0aSRene Herman #define DEV_NAME "opti93x"
2831da177e4SLinus Torvalds #else
28483c51c0aSRene Herman #define DEV_NAME "opti92x"
28583c51c0aSRene Herman #endif
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds static char * snd_opti9xx_names[] = {
2881da177e4SLinus Torvalds 	"unkown",
2891da177e4SLinus Torvalds 	"82C928",	"82C929",
2901da177e4SLinus Torvalds 	"82C924",	"82C925",
2911da177e4SLinus Torvalds 	"82C930",	"82C931",	"82C933"
2921da177e4SLinus Torvalds };
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 
2955e24c1c1STakashi Iwai static long __devinit snd_legacy_find_free_ioport(long *port_table, long size)
2961da177e4SLinus Torvalds {
2971da177e4SLinus Torvalds 	while (*port_table != -1) {
298b1d5776dSTakashi Iwai 		if (request_region(*port_table, size, "ALSA test")) {
299b1d5776dSTakashi Iwai 			release_region(*port_table, size);
3001da177e4SLinus Torvalds 			return *port_table;
3011da177e4SLinus Torvalds 		}
3021da177e4SLinus Torvalds 		port_table++;
3031da177e4SLinus Torvalds 	}
3041da177e4SLinus Torvalds 	return -1;
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
3075e24c1c1STakashi Iwai static int __devinit snd_opti9xx_init(struct snd_opti9xx *chip,
3085e24c1c1STakashi Iwai 				      unsigned short hardware)
3091da177e4SLinus Torvalds {
3101da177e4SLinus Torvalds 	static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	chip->hardware = hardware;
3131da177e4SLinus Torvalds 	strcpy(chip->name, snd_opti9xx_names[hardware]);
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	chip->mc_base_size = opti9xx_mc_size[hardware];
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	spin_lock_init(&chip->lock);
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	chip->wss_base = -1;
3201da177e4SLinus Torvalds 	chip->irq = -1;
3211da177e4SLinus Torvalds 	chip->dma1 = -1;
3221da177e4SLinus Torvalds #if defined(CS4231) || defined (OPTi93X)
3231da177e4SLinus Torvalds 	chip->dma2 = -1;
3241da177e4SLinus Torvalds #endif 	/* CS4231 || OPTi93X */
3251da177e4SLinus Torvalds 	chip->fm_port = -1;
3261da177e4SLinus Torvalds 	chip->mpu_port = -1;
3271da177e4SLinus Torvalds 	chip->mpu_irq = -1;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	switch (hardware) {
3301da177e4SLinus Torvalds #ifndef OPTi93X
3311da177e4SLinus Torvalds 	case OPTi9XX_HW_82C928:
3321da177e4SLinus Torvalds 	case OPTi9XX_HW_82C929:
3331da177e4SLinus Torvalds 		chip->mc_base = 0xf8c;
3341da177e4SLinus Torvalds 		chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
3351da177e4SLinus Torvalds 		chip->pwd_reg = 3;
3361da177e4SLinus Torvalds 		break;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	case OPTi9XX_HW_82C924:
3391da177e4SLinus Torvalds 	case OPTi9XX_HW_82C925:
3401da177e4SLinus Torvalds 		chip->mc_base = 0xf8c;
3411da177e4SLinus Torvalds 		chip->password = 0xe5;
3421da177e4SLinus Torvalds 		chip->pwd_reg = 3;
3431da177e4SLinus Torvalds 		break;
3441da177e4SLinus Torvalds #else	/* OPTi93X */
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	case OPTi9XX_HW_82C930:
3471da177e4SLinus Torvalds 	case OPTi9XX_HW_82C931:
3481da177e4SLinus Torvalds 	case OPTi9XX_HW_82C933:
3491da177e4SLinus Torvalds 		chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
3501da177e4SLinus Torvalds 		chip->mc_indir_index = 0xe0e;
3511da177e4SLinus Torvalds 		chip->password = 0xe4;
3521da177e4SLinus Torvalds 		chip->pwd_reg = 0;
3531da177e4SLinus Torvalds 		break;
3541da177e4SLinus Torvalds #endif	/* OPTi93X */
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	default:
3571da177e4SLinus Torvalds 		snd_printk("chip %d not supported\n", hardware);
3581da177e4SLinus Torvalds 		return -ENODEV;
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 	return 0;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
363346c7a68STakashi Iwai static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip,
3641da177e4SLinus Torvalds 				      unsigned char reg)
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds 	unsigned long flags;
3671da177e4SLinus Torvalds 	unsigned char retval = 0xff;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
3701da177e4SLinus Torvalds 	outb(chip->password, chip->mc_base + chip->pwd_reg);
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	switch (chip->hardware) {
3731da177e4SLinus Torvalds #ifndef OPTi93X
3741da177e4SLinus Torvalds 	case OPTi9XX_HW_82C924:
3751da177e4SLinus Torvalds 	case OPTi9XX_HW_82C925:
3761da177e4SLinus Torvalds 		if (reg > 7) {
3771da177e4SLinus Torvalds 			outb(reg, chip->mc_base + 8);
3781da177e4SLinus Torvalds 			outb(chip->password, chip->mc_base + chip->pwd_reg);
3791da177e4SLinus Torvalds 			retval = inb(chip->mc_base + 9);
3801da177e4SLinus Torvalds 			break;
3811da177e4SLinus Torvalds 		}
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	case OPTi9XX_HW_82C928:
3841da177e4SLinus Torvalds 	case OPTi9XX_HW_82C929:
3851da177e4SLinus Torvalds 		retval = inb(chip->mc_base + reg);
3861da177e4SLinus Torvalds 		break;
3871da177e4SLinus Torvalds #else	/* OPTi93X */
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	case OPTi9XX_HW_82C930:
3901da177e4SLinus Torvalds 	case OPTi9XX_HW_82C931:
3911da177e4SLinus Torvalds 	case OPTi9XX_HW_82C933:
3921da177e4SLinus Torvalds 		outb(reg, chip->mc_indir_index);
3931da177e4SLinus Torvalds 		outb(chip->password, chip->mc_base + chip->pwd_reg);
3941da177e4SLinus Torvalds 		retval = inb(chip->mc_indir_index + 1);
3951da177e4SLinus Torvalds 		break;
3961da177e4SLinus Torvalds #endif	/* OPTi93X */
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	default:
3991da177e4SLinus Torvalds 		snd_printk("chip %d not supported\n", chip->hardware);
4001da177e4SLinus Torvalds 	}
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
4031da177e4SLinus Torvalds 	return retval;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
406346c7a68STakashi Iwai static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg,
4071da177e4SLinus Torvalds 			      unsigned char value)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	unsigned long flags;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
4121da177e4SLinus Torvalds 	outb(chip->password, chip->mc_base + chip->pwd_reg);
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	switch (chip->hardware) {
4151da177e4SLinus Torvalds #ifndef OPTi93X
4161da177e4SLinus Torvalds 	case OPTi9XX_HW_82C924:
4171da177e4SLinus Torvalds 	case OPTi9XX_HW_82C925:
4181da177e4SLinus Torvalds 		if (reg > 7) {
4191da177e4SLinus Torvalds 			outb(reg, chip->mc_base + 8);
4201da177e4SLinus Torvalds 			outb(chip->password, chip->mc_base + chip->pwd_reg);
4211da177e4SLinus Torvalds 			outb(value, chip->mc_base + 9);
4221da177e4SLinus Torvalds 			break;
4231da177e4SLinus Torvalds 		}
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	case OPTi9XX_HW_82C928:
4261da177e4SLinus Torvalds 	case OPTi9XX_HW_82C929:
4271da177e4SLinus Torvalds 		outb(value, chip->mc_base + reg);
4281da177e4SLinus Torvalds 		break;
4291da177e4SLinus Torvalds #else	/* OPTi93X */
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	case OPTi9XX_HW_82C930:
4321da177e4SLinus Torvalds 	case OPTi9XX_HW_82C931:
4331da177e4SLinus Torvalds 	case OPTi9XX_HW_82C933:
4341da177e4SLinus Torvalds 		outb(reg, chip->mc_indir_index);
4351da177e4SLinus Torvalds 		outb(chip->password, chip->mc_base + chip->pwd_reg);
4361da177e4SLinus Torvalds 		outb(value, chip->mc_indir_index + 1);
4371da177e4SLinus Torvalds 		break;
4381da177e4SLinus Torvalds #endif	/* OPTi93X */
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	default:
4411da177e4SLinus Torvalds 		snd_printk("chip %d not supported\n", chip->hardware);
4421da177e4SLinus Torvalds 	}
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds #define snd_opti9xx_write_mask(chip, reg, value, mask)	\
4491da177e4SLinus Torvalds 	snd_opti9xx_write(chip, reg,			\
4501da177e4SLinus Torvalds 		(snd_opti9xx_read(chip, reg) & ~(mask)) | ((value) & (mask)))
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 
4535e24c1c1STakashi Iwai static int __devinit snd_opti9xx_configure(struct snd_opti9xx *chip)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	unsigned char wss_base_bits;
4561da177e4SLinus Torvalds 	unsigned char irq_bits;
4571da177e4SLinus Torvalds 	unsigned char dma_bits;
4581da177e4SLinus Torvalds 	unsigned char mpu_port_bits = 0;
4591da177e4SLinus Torvalds 	unsigned char mpu_irq_bits;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	switch (chip->hardware) {
4621da177e4SLinus Torvalds #ifndef OPTi93X
4631da177e4SLinus Torvalds 	case OPTi9XX_HW_82C924:
4641da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
4651da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	case OPTi9XX_HW_82C925:
4681da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
4691da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
4701da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
4711da177e4SLinus Torvalds #ifdef CS4231
4721da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
4731da177e4SLinus Torvalds #else
4741da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
4751da177e4SLinus Torvalds #endif	/* CS4231 */
4761da177e4SLinus Torvalds 		break;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	case OPTi9XX_HW_82C928:
4791da177e4SLinus Torvalds 	case OPTi9XX_HW_82C929:
4801da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
4811da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
4821da177e4SLinus Torvalds 		/*
4831da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
4841da177e4SLinus Torvalds 		*/
4851da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
4861da177e4SLinus Torvalds #ifdef CS4231
4871da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
4881da177e4SLinus Torvalds #else
4891da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
4901da177e4SLinus Torvalds #endif	/* CS4231 */
4911da177e4SLinus Torvalds 		break;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds #else	/* OPTi93X */
4941da177e4SLinus Torvalds 	case OPTi9XX_HW_82C931:
4951da177e4SLinus Torvalds 	case OPTi9XX_HW_82C933:
496f81b953dSKrzysztof Helt 		/*
497f81b953dSKrzysztof Helt 		 * The BTC 1817DW has QS1000 wavetable which is connected
498f81b953dSKrzysztof Helt 		 * to the serial digital input of the OPTI931.
499f81b953dSKrzysztof Helt 		 */
500f81b953dSKrzysztof Helt 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
501f81b953dSKrzysztof Helt 		/*
502f81b953dSKrzysztof Helt 		 * This bit sets OPTI931 to automaticaly select FM
503f81b953dSKrzysztof Helt 		 * or digital input signal.
504f81b953dSKrzysztof Helt 		 */
505f81b953dSKrzysztof Helt 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
506*3ae5f36aSKrzysztof Helt 	case OPTi9XX_HW_82C930: /* FALL THROUGH */
507*3ae5f36aSKrzysztof Helt 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
508*3ae5f36aSKrzysztof Helt 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
509*3ae5f36aSKrzysztof Helt 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
510*3ae5f36aSKrzysztof Helt 			(chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
511*3ae5f36aSKrzysztof Helt 			0x34);
512*3ae5f36aSKrzysztof Helt 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
5131da177e4SLinus Torvalds 		break;
5141da177e4SLinus Torvalds #endif	/* OPTi93X */
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	default:
5171da177e4SLinus Torvalds 		snd_printk("chip %d not supported\n", chip->hardware);
5181da177e4SLinus Torvalds 		return -EINVAL;
5191da177e4SLinus Torvalds 	}
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	switch (chip->wss_base) {
5221da177e4SLinus Torvalds 	case 0x530:
5231da177e4SLinus Torvalds 		wss_base_bits = 0x00;
5241da177e4SLinus Torvalds 		break;
5251da177e4SLinus Torvalds 	case 0x604:
5261da177e4SLinus Torvalds 		wss_base_bits = 0x03;
5271da177e4SLinus Torvalds 		break;
5281da177e4SLinus Torvalds 	case 0xe80:
5291da177e4SLinus Torvalds 		wss_base_bits = 0x01;
5301da177e4SLinus Torvalds 		break;
5311da177e4SLinus Torvalds 	case 0xf40:
5321da177e4SLinus Torvalds 		wss_base_bits = 0x02;
5331da177e4SLinus Torvalds 		break;
5341da177e4SLinus Torvalds 	default:
5351da177e4SLinus Torvalds 		snd_printk("WSS port 0x%lx not valid\n", chip->wss_base);
5361da177e4SLinus Torvalds 		goto __skip_base;
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 	snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds __skip_base:
5411da177e4SLinus Torvalds 	switch (chip->irq) {
5421da177e4SLinus Torvalds //#ifdef OPTi93X
5431da177e4SLinus Torvalds 	case 5:
5441da177e4SLinus Torvalds 		irq_bits = 0x05;
5451da177e4SLinus Torvalds 		break;
5461da177e4SLinus Torvalds //#endif	/* OPTi93X */
5471da177e4SLinus Torvalds 	case 7:
5481da177e4SLinus Torvalds 		irq_bits = 0x01;
5491da177e4SLinus Torvalds 		break;
5501da177e4SLinus Torvalds 	case 9:
5511da177e4SLinus Torvalds 		irq_bits = 0x02;
5521da177e4SLinus Torvalds 		break;
5531da177e4SLinus Torvalds 	case 10:
5541da177e4SLinus Torvalds 		irq_bits = 0x03;
5551da177e4SLinus Torvalds 		break;
5561da177e4SLinus Torvalds 	case 11:
5571da177e4SLinus Torvalds 		irq_bits = 0x04;
5581da177e4SLinus Torvalds 		break;
5591da177e4SLinus Torvalds 	default:
5601da177e4SLinus Torvalds 		snd_printk("WSS irq # %d not valid\n", chip->irq);
5611da177e4SLinus Torvalds 		goto __skip_resources;
5621da177e4SLinus Torvalds 	}
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	switch (chip->dma1) {
5651da177e4SLinus Torvalds 	case 0:
5661da177e4SLinus Torvalds 		dma_bits = 0x01;
5671da177e4SLinus Torvalds 		break;
5681da177e4SLinus Torvalds 	case 1:
5691da177e4SLinus Torvalds 		dma_bits = 0x02;
5701da177e4SLinus Torvalds 		break;
5711da177e4SLinus Torvalds 	case 3:
5721da177e4SLinus Torvalds 		dma_bits = 0x03;
5731da177e4SLinus Torvalds 		break;
5741da177e4SLinus Torvalds 	default:
5751da177e4SLinus Torvalds 		snd_printk("WSS dma1 # %d not valid\n", chip->dma1);
5761da177e4SLinus Torvalds 		goto __skip_resources;
5771da177e4SLinus Torvalds 	}
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
5801da177e4SLinus Torvalds 	if (chip->dma1 == chip->dma2) {
5811da177e4SLinus Torvalds 		snd_printk("don't want to share dmas\n");
5821da177e4SLinus Torvalds 		return -EBUSY;
5831da177e4SLinus Torvalds 	}
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	switch (chip->dma2) {
5861da177e4SLinus Torvalds 	case 0:
5871da177e4SLinus Torvalds 	case 1:
5881da177e4SLinus Torvalds 		break;
5891da177e4SLinus Torvalds 	default:
5901da177e4SLinus Torvalds 		snd_printk("WSS dma2 # %d not valid\n", chip->dma2);
5911da177e4SLinus Torvalds 		goto __skip_resources;
5921da177e4SLinus Torvalds 	}
5931da177e4SLinus Torvalds 	dma_bits |= 0x04;
5941da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds #ifndef OPTi93X
5971da177e4SLinus Torvalds 	 outb(irq_bits << 3 | dma_bits, chip->wss_base);
5981da177e4SLinus Torvalds #else /* OPTi93X */
5991da177e4SLinus Torvalds 	snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
6001da177e4SLinus Torvalds #endif /* OPTi93X */
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds __skip_resources:
6031da177e4SLinus Torvalds 	if (chip->hardware > OPTi9XX_HW_82C928) {
6041da177e4SLinus Torvalds 		switch (chip->mpu_port) {
6051da177e4SLinus Torvalds 		case 0:
6061da177e4SLinus Torvalds 		case -1:
6071da177e4SLinus Torvalds 			break;
6081da177e4SLinus Torvalds 		case 0x300:
6091da177e4SLinus Torvalds 			mpu_port_bits = 0x03;
6101da177e4SLinus Torvalds 			break;
6111da177e4SLinus Torvalds 		case 0x310:
6121da177e4SLinus Torvalds 			mpu_port_bits = 0x02;
6131da177e4SLinus Torvalds 			break;
6141da177e4SLinus Torvalds 		case 0x320:
6151da177e4SLinus Torvalds 			mpu_port_bits = 0x01;
6161da177e4SLinus Torvalds 			break;
6171da177e4SLinus Torvalds 		case 0x330:
6181da177e4SLinus Torvalds 			mpu_port_bits = 0x00;
6191da177e4SLinus Torvalds 			break;
6201da177e4SLinus Torvalds 		default:
6211da177e4SLinus Torvalds 			snd_printk("MPU-401 port 0x%lx not valid\n",
6221da177e4SLinus Torvalds 				chip->mpu_port);
6231da177e4SLinus Torvalds 			goto __skip_mpu;
6241da177e4SLinus Torvalds 		}
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 		switch (chip->mpu_irq) {
6271da177e4SLinus Torvalds 		case 5:
6281da177e4SLinus Torvalds 			mpu_irq_bits = 0x02;
6291da177e4SLinus Torvalds 			break;
6301da177e4SLinus Torvalds 		case 7:
6311da177e4SLinus Torvalds 			mpu_irq_bits = 0x03;
6321da177e4SLinus Torvalds 			break;
6331da177e4SLinus Torvalds 		case 9:
6341da177e4SLinus Torvalds 			mpu_irq_bits = 0x00;
6351da177e4SLinus Torvalds 			break;
6361da177e4SLinus Torvalds 		case 10:
6371da177e4SLinus Torvalds 			mpu_irq_bits = 0x01;
6381da177e4SLinus Torvalds 			break;
6391da177e4SLinus Torvalds 		default:
6401da177e4SLinus Torvalds 			snd_printk("MPU-401 irq # %d not valid\n",
6411da177e4SLinus Torvalds 				chip->mpu_irq);
6421da177e4SLinus Torvalds 			goto __skip_mpu;
6431da177e4SLinus Torvalds 		}
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
6461da177e4SLinus Torvalds 			(chip->mpu_port <= 0) ? 0x00 :
6471da177e4SLinus Torvalds 				0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
6481da177e4SLinus Torvalds 			0xf8);
6491da177e4SLinus Torvalds 	}
6501da177e4SLinus Torvalds __skip_mpu:
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	return 0;
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds #ifdef OPTi93X
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds static unsigned char snd_opti93x_default_image[32] =
6581da177e4SLinus Torvalds {
6591da177e4SLinus Torvalds 	0x00,		/* 00/00 - l_mixout_outctrl */
6601da177e4SLinus Torvalds 	0x00,		/* 01/01 - r_mixout_outctrl */
6611da177e4SLinus Torvalds 	0x88,		/* 02/02 - l_cd_inctrl */
6621da177e4SLinus Torvalds 	0x88,		/* 03/03 - r_cd_inctrl */
6631da177e4SLinus Torvalds 	0x88,		/* 04/04 - l_a1/fm_inctrl */
6641da177e4SLinus Torvalds 	0x88,		/* 05/05 - r_a1/fm_inctrl */
6651da177e4SLinus Torvalds 	0x80,		/* 06/06 - l_dac_inctrl */
6661da177e4SLinus Torvalds 	0x80,		/* 07/07 - r_dac_inctrl */
6671da177e4SLinus Torvalds 	0x00,		/* 08/08 - ply_dataform_reg */
6681da177e4SLinus Torvalds 	0x00,		/* 09/09 - if_conf */
6691da177e4SLinus Torvalds 	0x00,		/* 0a/10 - pin_ctrl */
6701da177e4SLinus Torvalds 	0x00,		/* 0b/11 - err_init_reg */
6711da177e4SLinus Torvalds 	0x0a,		/* 0c/12 - id_reg */
6721da177e4SLinus Torvalds 	0x00,		/* 0d/13 - reserved */
6731da177e4SLinus Torvalds 	0x00,		/* 0e/14 - ply_upcount_reg */
6741da177e4SLinus Torvalds 	0x00,		/* 0f/15 - ply_lowcount_reg */
6751da177e4SLinus Torvalds 	0x88,		/* 10/16 - reserved/l_a1_inctrl */
6761da177e4SLinus Torvalds 	0x88,		/* 11/17 - reserved/r_a1_inctrl */
6771da177e4SLinus Torvalds 	0x88,		/* 12/18 - l_line_inctrl */
6781da177e4SLinus Torvalds 	0x88,		/* 13/19 - r_line_inctrl */
6791da177e4SLinus Torvalds 	0x88,		/* 14/20 - l_mic_inctrl */
6801da177e4SLinus Torvalds 	0x88,		/* 15/21 - r_mic_inctrl */
6811da177e4SLinus Torvalds 	0x80,		/* 16/22 - l_out_outctrl */
6821da177e4SLinus Torvalds 	0x80,		/* 17/23 - r_out_outctrl */
6831da177e4SLinus Torvalds 	0x00,		/* 18/24 - reserved */
6841da177e4SLinus Torvalds 	0x00,		/* 19/25 - reserved */
6851da177e4SLinus Torvalds 	0x00,		/* 1a/26 - reserved */
6861da177e4SLinus Torvalds 	0x00,		/* 1b/27 - reserved */
6871da177e4SLinus Torvalds 	0x00,		/* 1c/28 - cap_dataform_reg */
6881da177e4SLinus Torvalds 	0x00,		/* 1d/29 - reserved */
6891da177e4SLinus Torvalds 	0x00,		/* 1e/30 - cap_upcount_reg */
6901da177e4SLinus Torvalds 	0x00		/* 1f/31 - cap_lowcount_reg */
6911da177e4SLinus Torvalds };
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 
694346c7a68STakashi Iwai static int snd_opti93x_busy_wait(struct snd_opti93x *chip)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	int timeout;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	for (timeout = 250; timeout-- > 0; udelay(10))
6991da177e4SLinus Torvalds 		if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_INIT))
7001da177e4SLinus Torvalds 			return 0;
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	snd_printk("chip still busy.\n");
7031da177e4SLinus Torvalds 	return -EBUSY;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
706346c7a68STakashi Iwai static unsigned char snd_opti93x_in(struct snd_opti93x *chip, unsigned char reg)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	snd_opti93x_busy_wait(chip);
7091da177e4SLinus Torvalds 	outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
7101da177e4SLinus Torvalds 	return inb(OPTi93X_PORT(chip, DATA));
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
713346c7a68STakashi Iwai static void snd_opti93x_out(struct snd_opti93x *chip, unsigned char reg,
7141da177e4SLinus Torvalds 			    unsigned char value)
7151da177e4SLinus Torvalds {
7161da177e4SLinus Torvalds 	snd_opti93x_busy_wait(chip);
7171da177e4SLinus Torvalds 	outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
7181da177e4SLinus Torvalds 	outb(value, OPTi93X_PORT(chip, DATA));
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
721346c7a68STakashi Iwai static void snd_opti93x_out_image(struct snd_opti93x *chip, unsigned char reg,
7221da177e4SLinus Torvalds 				  unsigned char value)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	snd_opti93x_out(chip, reg, chip->image[reg] = value);
7251da177e4SLinus Torvalds }
7261da177e4SLinus Torvalds 
727346c7a68STakashi Iwai static void snd_opti93x_out_mask(struct snd_opti93x *chip, unsigned char reg,
7281da177e4SLinus Torvalds 				 unsigned char mask, unsigned char value)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, reg,
7311da177e4SLinus Torvalds 		(chip->image[reg] & ~mask) | (value & mask));
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 
735346c7a68STakashi Iwai static void snd_opti93x_mce_up(struct snd_opti93x *chip)
7361da177e4SLinus Torvalds {
7371da177e4SLinus Torvalds 	snd_opti93x_busy_wait(chip);
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	chip->mce_bit = OPTi93X_MCE;
7401da177e4SLinus Torvalds 	if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE))
7411da177e4SLinus Torvalds 		outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
744346c7a68STakashi Iwai static void snd_opti93x_mce_down(struct snd_opti93x *chip)
7451da177e4SLinus Torvalds {
7461da177e4SLinus Torvalds 	snd_opti93x_busy_wait(chip);
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	chip->mce_bit = 0;
7491da177e4SLinus Torvalds 	if (inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE)
7501da177e4SLinus Torvalds 		outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds #define snd_opti93x_mute_reg(chip, reg, mute)	\
7541da177e4SLinus Torvalds 	snd_opti93x_out(chip, reg, mute ? 0x80 : chip->image[reg]);
7551da177e4SLinus Torvalds 
756346c7a68STakashi Iwai static void snd_opti93x_mute(struct snd_opti93x *chip, int mute)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	mute = mute ? 1 : 0;
7591da177e4SLinus Torvalds 	if (chip->mute == mute)
7601da177e4SLinus Torvalds 		return;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	chip->mute = mute;
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_CD_LEFT_INPUT, mute);
7651da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_CD_RIGHT_INPUT, mute);
7661da177e4SLinus Torvalds 	switch (chip->hardware) {
7671da177e4SLinus Torvalds 	case OPTi9XX_HW_82C930:
7681da177e4SLinus Torvalds 		snd_opti93x_mute_reg(chip, OPTi930_AUX_LEFT_INPUT, mute);
7691da177e4SLinus Torvalds 		snd_opti93x_mute_reg(chip, OPTi930_AUX_RIGHT_INPUT, mute);
7701da177e4SLinus Torvalds 		break;
7711da177e4SLinus Torvalds 	case OPTi9XX_HW_82C931:
7721da177e4SLinus Torvalds 	case OPTi9XX_HW_82C933:
7731da177e4SLinus Torvalds 		snd_opti93x_mute_reg(chip, OPTi931_FM_LEFT_INPUT, mute);
7741da177e4SLinus Torvalds 		snd_opti93x_mute_reg(chip, OPTi931_FM_RIGHT_INPUT, mute);
7751da177e4SLinus Torvalds 		snd_opti93x_mute_reg(chip, OPTi931_AUX_LEFT_INPUT, mute);
7761da177e4SLinus Torvalds 		snd_opti93x_mute_reg(chip, OPTi931_AUX_RIGHT_INPUT, mute);
7771da177e4SLinus Torvalds 	}
7781da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_DAC_LEFT, mute);
7791da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_DAC_RIGHT, mute);
7801da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_LINE_LEFT_INPUT, mute);
7811da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_LINE_RIGHT_INPUT, mute);
7821da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_MIC_LEFT_INPUT, mute);
7831da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_MIC_RIGHT_INPUT, mute);
7841da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_OUT_LEFT, mute);
7851da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_OUT_RIGHT, mute);
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds static unsigned int snd_opti93x_get_count(unsigned char format,
7901da177e4SLinus Torvalds 					  unsigned int size)
7911da177e4SLinus Torvalds {
7921da177e4SLinus Torvalds 	switch (format & 0xe0) {
7931da177e4SLinus Torvalds 	case OPTi93X_LINEAR_16_LIT:
7941da177e4SLinus Torvalds 	case OPTi93X_LINEAR_16_BIG:
7951da177e4SLinus Torvalds 		size >>= 1;
7961da177e4SLinus Torvalds 		break;
7971da177e4SLinus Torvalds 	case OPTi93X_ADPCM_16:
7981da177e4SLinus Torvalds 		return size >> 2;
7991da177e4SLinus Torvalds 	}
8001da177e4SLinus Torvalds 	return (format & OPTi93X_STEREO) ? (size >> 1) : size;
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds static unsigned int rates[] = {  5512,  6615,  8000,  9600, 11025, 16000,
8041da177e4SLinus Torvalds 				18900, 22050, 27428, 32000, 33075, 37800,
8051da177e4SLinus Torvalds 				44100, 48000 };
8061da177e4SLinus Torvalds #define RATES ARRAY_SIZE(rates)
8071da177e4SLinus Torvalds 
808346c7a68STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
8091da177e4SLinus Torvalds 	.count = RATES,
8101da177e4SLinus Torvalds 	.list = rates,
8111da177e4SLinus Torvalds 	.mask = 0,
8121da177e4SLinus Torvalds };
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds static unsigned char bits[] = {  0x01,  0x0f,  0x00,  0x0e,  0x03,  0x02,
8151da177e4SLinus Torvalds 				 0x05,  0x07,  0x04,  0x06,  0x0d,  0x09,
8161da177e4SLinus Torvalds 				 0x0b,  0x0c};
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds static unsigned char snd_opti93x_get_freq(unsigned int rate)
8191da177e4SLinus Torvalds {
8201da177e4SLinus Torvalds 	unsigned int i;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	for (i = 0; i < RATES; i++) {
8231da177e4SLinus Torvalds 		if (rate == rates[i])
8241da177e4SLinus Torvalds 			return bits[i];
8251da177e4SLinus Torvalds 	}
8261da177e4SLinus Torvalds 	snd_BUG();
8271da177e4SLinus Torvalds 	return bits[RATES-1];
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
830346c7a68STakashi Iwai static unsigned char snd_opti93x_get_format(struct snd_opti93x *chip,
8311da177e4SLinus Torvalds 					    unsigned int format, int channels)
8321da177e4SLinus Torvalds {
8331da177e4SLinus Torvalds 	unsigned char retval = OPTi93X_LINEAR_8;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	switch (format) {
8361da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_MU_LAW:
8371da177e4SLinus Torvalds 		retval = OPTi93X_ULAW_8;
8381da177e4SLinus Torvalds 		break;
8391da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_A_LAW:
8401da177e4SLinus Torvalds 		retval = OPTi93X_ALAW_8;
8411da177e4SLinus Torvalds 		break;
8421da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S16_LE:
8431da177e4SLinus Torvalds 		retval = OPTi93X_LINEAR_16_LIT;
8441da177e4SLinus Torvalds 		break;
8451da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S16_BE:
8461da177e4SLinus Torvalds 		retval = OPTi93X_LINEAR_16_BIG;
8471da177e4SLinus Torvalds 		break;
8481da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_IMA_ADPCM:
8491da177e4SLinus Torvalds 		retval = OPTi93X_ADPCM_16;
8501da177e4SLinus Torvalds 	}
8511da177e4SLinus Torvalds 	return (channels > 1) ? (retval | OPTi93X_STEREO) : retval;
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 
855346c7a68STakashi Iwai static void snd_opti93x_playback_format(struct snd_opti93x *chip, unsigned char fmt)
8561da177e4SLinus Torvalds {
8571da177e4SLinus Torvalds 	unsigned char mask;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	snd_opti93x_mute(chip, 1);
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	snd_opti93x_mce_up(chip);
8621da177e4SLinus Torvalds 	mask = (chip->mode & OPTi93X_MODE_CAPTURE) ? 0xf0 : 0xff;
8631da177e4SLinus Torvalds 	snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, mask, fmt);
8641da177e4SLinus Torvalds 	snd_opti93x_mce_down(chip);
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	snd_opti93x_mute(chip, 0);
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
869346c7a68STakashi Iwai static void snd_opti93x_capture_format(struct snd_opti93x *chip, unsigned char fmt)
8701da177e4SLinus Torvalds {
8711da177e4SLinus Torvalds 	snd_opti93x_mute(chip, 1);
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	snd_opti93x_mce_up(chip);
8741da177e4SLinus Torvalds 	if (!(chip->mode & OPTi93X_MODE_PLAY))
8751da177e4SLinus Torvalds 		snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, 0x0f, fmt);
8761da177e4SLinus Torvalds 	else
8771da177e4SLinus Torvalds 		fmt = chip->image[OPTi93X_PLAY_FORMAT] & 0xf0;
8781da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_CAPT_FORMAT, fmt);
8791da177e4SLinus Torvalds 	snd_opti93x_mce_down(chip);
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	snd_opti93x_mute(chip, 0);
8821da177e4SLinus Torvalds }
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 
885346c7a68STakashi Iwai static int snd_opti93x_open(struct snd_opti93x *chip, unsigned int mode)
8861da177e4SLinus Torvalds {
8871da177e4SLinus Torvalds 	unsigned long flags;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	if (chip->mode & mode) {
8921da177e4SLinus Torvalds 		spin_unlock_irqrestore(&chip->lock, flags);
8931da177e4SLinus Torvalds 		return -EAGAIN;
8941da177e4SLinus Torvalds 	}
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	if (!(chip->mode & OPTi93X_MODE_OPEN)) {
8971da177e4SLinus Torvalds 		outb(0x00, OPTi93X_PORT(chip, STATUS));
8981da177e4SLinus Torvalds 		snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL,
8991da177e4SLinus Torvalds 			OPTi93X_IRQ_ENABLE, OPTi93X_IRQ_ENABLE);
9001da177e4SLinus Torvalds 		chip->mode = mode;
9011da177e4SLinus Torvalds 	}
9021da177e4SLinus Torvalds 	else
9031da177e4SLinus Torvalds 		chip->mode |= mode;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
9061da177e4SLinus Torvalds 	return 0;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
909346c7a68STakashi Iwai static void snd_opti93x_close(struct snd_opti93x *chip, unsigned int mode)
9101da177e4SLinus Torvalds {
9111da177e4SLinus Torvalds 	unsigned long flags;
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	chip->mode &= ~mode;
9161da177e4SLinus Torvalds 	if (chip->mode & OPTi93X_MODE_OPEN) {
9171da177e4SLinus Torvalds 		spin_unlock_irqrestore(&chip->lock, flags);
9181da177e4SLinus Torvalds 		return;
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	snd_opti93x_mute(chip, 1);
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	outb(0, OPTi93X_PORT(chip, STATUS));
9241da177e4SLinus Torvalds 	snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL, OPTi93X_IRQ_ENABLE,
9251da177e4SLinus Torvalds 		~OPTi93X_IRQ_ENABLE);
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	snd_opti93x_mce_up(chip);
9281da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_IFACE_CONF, 0x00);
9291da177e4SLinus Torvalds 	snd_opti93x_mce_down(chip);
9301da177e4SLinus Torvalds 	chip->mode = 0;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	snd_opti93x_mute(chip, 0);
9331da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
9341da177e4SLinus Torvalds }
9351da177e4SLinus Torvalds 
936346c7a68STakashi Iwai static int snd_opti93x_trigger(struct snd_pcm_substream *substream,
9371da177e4SLinus Torvalds 			       unsigned char what, int cmd)
9381da177e4SLinus Torvalds {
939346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	switch (cmd) {
9421da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
9431da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
9441da177e4SLinus Torvalds 	{
9451da177e4SLinus Torvalds 		unsigned int what = 0;
946346c7a68STakashi Iwai 		struct snd_pcm_substream *s;
947ef991b95STakashi Iwai 		snd_pcm_group_for_each_entry(s, substream) {
9481da177e4SLinus Torvalds 			if (s == chip->playback_substream) {
9491da177e4SLinus Torvalds 				what |= OPTi93X_PLAYBACK_ENABLE;
9501da177e4SLinus Torvalds 				snd_pcm_trigger_done(s, substream);
9511da177e4SLinus Torvalds 			} else if (s == chip->capture_substream) {
9521da177e4SLinus Torvalds 				what |= OPTi93X_CAPTURE_ENABLE;
9531da177e4SLinus Torvalds 				snd_pcm_trigger_done(s, substream);
9541da177e4SLinus Torvalds 			}
9551da177e4SLinus Torvalds 		}
9561da177e4SLinus Torvalds 		spin_lock(&chip->lock);
9571da177e4SLinus Torvalds 		if (cmd == SNDRV_PCM_TRIGGER_START) {
9581da177e4SLinus Torvalds 			snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, what);
9591da177e4SLinus Torvalds 			if (what & OPTi93X_CAPTURE_ENABLE)
9601da177e4SLinus Torvalds 				udelay(50);
9611da177e4SLinus Torvalds 		} else
9621da177e4SLinus Torvalds 			snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, 0x00);
9631da177e4SLinus Torvalds 		spin_unlock(&chip->lock);
9641da177e4SLinus Torvalds 		break;
9651da177e4SLinus Torvalds 	}
9661da177e4SLinus Torvalds 	default:
9671da177e4SLinus Torvalds 		return -EINVAL;
9681da177e4SLinus Torvalds 	}
9691da177e4SLinus Torvalds 	return 0;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
972346c7a68STakashi Iwai static int snd_opti93x_playback_trigger(struct snd_pcm_substream *substream, int cmd)
9731da177e4SLinus Torvalds {
9741da177e4SLinus Torvalds 	return snd_opti93x_trigger(substream,
9751da177e4SLinus Torvalds 				   OPTi93X_PLAYBACK_ENABLE, cmd);
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
978346c7a68STakashi Iwai static int snd_opti93x_capture_trigger(struct snd_pcm_substream *substream, int cmd)
9791da177e4SLinus Torvalds {
9801da177e4SLinus Torvalds 	return snd_opti93x_trigger(substream,
9811da177e4SLinus Torvalds 				   OPTi93X_CAPTURE_ENABLE, cmd);
9821da177e4SLinus Torvalds }
9831da177e4SLinus Torvalds 
984346c7a68STakashi Iwai static int snd_opti93x_hw_params(struct snd_pcm_substream *substream,
985346c7a68STakashi Iwai 				 struct snd_pcm_hw_params *hw_params)
9861da177e4SLinus Torvalds {
9871da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 
991346c7a68STakashi Iwai static int snd_opti93x_hw_free(struct snd_pcm_substream *substream)
9921da177e4SLinus Torvalds {
9931da177e4SLinus Torvalds 	snd_pcm_lib_free_pages(substream);
9941da177e4SLinus Torvalds 	return 0;
9951da177e4SLinus Torvalds }
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 
998346c7a68STakashi Iwai static int snd_opti93x_playback_prepare(struct snd_pcm_substream *substream)
9991da177e4SLinus Torvalds {
1000346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
1001346c7a68STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
10021da177e4SLinus Torvalds 	unsigned long flags;
10031da177e4SLinus Torvalds 	unsigned char format;
10041da177e4SLinus Torvalds 	unsigned int count = snd_pcm_lib_period_bytes(substream);
10051da177e4SLinus Torvalds 	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	chip->p_dma_size = size;
10101da177e4SLinus Torvalds 	snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
10111da177e4SLinus Torvalds 		OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO,
10121da177e4SLinus Torvalds 		~(OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO));
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	snd_dma_program(chip->dma1, runtime->dma_addr, size,
10151da177e4SLinus Torvalds 		DMA_MODE_WRITE | DMA_AUTOINIT);
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	format = snd_opti93x_get_freq(runtime->rate);
10181da177e4SLinus Torvalds 	format |= snd_opti93x_get_format(chip, runtime->format,
10191da177e4SLinus Torvalds 		runtime->channels);
10201da177e4SLinus Torvalds 	snd_opti93x_playback_format(chip, format);
10211da177e4SLinus Torvalds 	format = chip->image[OPTi93X_PLAY_FORMAT];
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	count = snd_opti93x_get_count(format, count) - 1;
10241da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_PLAY_LWR_CNT, count);
10251da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_PLAY_UPR_CNT, count >> 8);
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
10281da177e4SLinus Torvalds 	return 0;
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds 
1031346c7a68STakashi Iwai static int snd_opti93x_capture_prepare(struct snd_pcm_substream *substream)
10321da177e4SLinus Torvalds {
1033346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
1034346c7a68STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
10351da177e4SLinus Torvalds 	unsigned long flags;
10361da177e4SLinus Torvalds 	unsigned char format;
10371da177e4SLinus Torvalds 	unsigned int count = snd_pcm_lib_period_bytes(substream);
10381da177e4SLinus Torvalds 	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	chip->c_dma_size = size;
10431da177e4SLinus Torvalds 	snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
1044db67319aSClemens Ladisch 		OPTi93X_CAPTURE_ENABLE | OPTi93X_CAPTURE_PIO, 0);
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	snd_dma_program(chip->dma2, runtime->dma_addr, size,
10471da177e4SLinus Torvalds 		DMA_MODE_READ | DMA_AUTOINIT);
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	format = snd_opti93x_get_freq(runtime->rate);
10501da177e4SLinus Torvalds 	format |= snd_opti93x_get_format(chip, runtime->format,
10511da177e4SLinus Torvalds 		runtime->channels);
10521da177e4SLinus Torvalds 	snd_opti93x_capture_format(chip, format);
10531da177e4SLinus Torvalds 	format = chip->image[OPTi93X_CAPT_FORMAT];
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	count = snd_opti93x_get_count(format, count) - 1;
10561da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_CAPT_LWR_CNT, count);
10571da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_CAPT_UPR_CNT, count >> 8);
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
10601da177e4SLinus Torvalds 	return 0;
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds 
1063346c7a68STakashi Iwai static snd_pcm_uframes_t snd_opti93x_playback_pointer(struct snd_pcm_substream *substream)
10641da177e4SLinus Torvalds {
1065346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
10661da177e4SLinus Torvalds 	size_t ptr;
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_PLAYBACK_ENABLE))
10691da177e4SLinus Torvalds 		return 0;
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
10721da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
10731da177e4SLinus Torvalds }
10741da177e4SLinus Torvalds 
1075346c7a68STakashi Iwai static snd_pcm_uframes_t snd_opti93x_capture_pointer(struct snd_pcm_substream *substream)
10761da177e4SLinus Torvalds {
1077346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
10781da177e4SLinus Torvalds 	size_t ptr;
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_CAPTURE_ENABLE))
10811da177e4SLinus Torvalds 		return 0;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
10841da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 
1088346c7a68STakashi Iwai static void snd_opti93x_overrange(struct snd_opti93x *chip)
10891da177e4SLinus Torvalds {
10901da177e4SLinus Torvalds 	unsigned long flags;
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	if (snd_opti93x_in(chip, OPTi93X_ERR_INIT) & (0x08 | 0x02))
10951da177e4SLinus Torvalds 		chip->capture_substream->runtime->overrange++;
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
11007d12e780SDavid Howells static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
11011da177e4SLinus Torvalds {
1102346c7a68STakashi Iwai 	struct snd_opti93x *codec = dev_id;
11031da177e4SLinus Torvalds 	unsigned char status;
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 	status = snd_opti9xx_read(codec->chip, OPTi9XX_MC_REG(11));
11061da177e4SLinus Torvalds 	if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
11071da177e4SLinus Torvalds 		snd_pcm_period_elapsed(codec->playback_substream);
11081da177e4SLinus Torvalds 	if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
11091da177e4SLinus Torvalds 		snd_opti93x_overrange(codec);
11101da177e4SLinus Torvalds 		snd_pcm_period_elapsed(codec->capture_substream);
11111da177e4SLinus Torvalds 	}
11121da177e4SLinus Torvalds 	outb(0x00, OPTi93X_PORT(codec, STATUS));
11131da177e4SLinus Torvalds 	return IRQ_HANDLED;
11141da177e4SLinus Torvalds }
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 
1117346c7a68STakashi Iwai static struct snd_pcm_hardware snd_opti93x_playback = {
11181da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
11191da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
11201da177e4SLinus Torvalds 	.formats =		(SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
11211da177e4SLinus Torvalds 				 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
11221da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
11231da177e4SLinus Torvalds 	.rate_min =		5512,
11241da177e4SLinus Torvalds 	.rate_max =		48000,
11251da177e4SLinus Torvalds 	.channels_min =		1,
11261da177e4SLinus Torvalds 	.channels_max =		2,
11271da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
11281da177e4SLinus Torvalds 	.period_bytes_min =	64,
11291da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
11301da177e4SLinus Torvalds 	.periods_min =		1,
11311da177e4SLinus Torvalds 	.periods_max =		1024,
11321da177e4SLinus Torvalds 	.fifo_size =		0,
11331da177e4SLinus Torvalds };
11341da177e4SLinus Torvalds 
1135346c7a68STakashi Iwai static struct snd_pcm_hardware snd_opti93x_capture = {
11361da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
11371da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
11381da177e4SLinus Torvalds 	.formats =		(SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
11391da177e4SLinus Torvalds 				 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
11401da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_8000_48000,
11411da177e4SLinus Torvalds 	.rate_min =		5512,
11421da177e4SLinus Torvalds 	.rate_max =		48000,
11431da177e4SLinus Torvalds 	.channels_min =		1,
11441da177e4SLinus Torvalds 	.channels_max =		2,
11451da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
11461da177e4SLinus Torvalds 	.period_bytes_min =	64,
11471da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
11481da177e4SLinus Torvalds 	.periods_min =		1,
11491da177e4SLinus Torvalds 	.periods_max =		1024,
11501da177e4SLinus Torvalds 	.fifo_size =		0,
11511da177e4SLinus Torvalds };
11521da177e4SLinus Torvalds 
1153346c7a68STakashi Iwai static int snd_opti93x_playback_open(struct snd_pcm_substream *substream)
11541da177e4SLinus Torvalds {
11551da177e4SLinus Torvalds 	int error;
1156346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
1157346c7a68STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	if ((error = snd_opti93x_open(chip, OPTi93X_MODE_PLAY)) < 0)
11601da177e4SLinus Torvalds 		return error;
11611da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
11621da177e4SLinus Torvalds 	chip->playback_substream = substream;
11631da177e4SLinus Torvalds 	runtime->hw = snd_opti93x_playback;
11641da177e4SLinus Torvalds 	snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
11651da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
11661da177e4SLinus Torvalds 	return error;
11671da177e4SLinus Torvalds }
11681da177e4SLinus Torvalds 
1169346c7a68STakashi Iwai static int snd_opti93x_capture_open(struct snd_pcm_substream *substream)
11701da177e4SLinus Torvalds {
11711da177e4SLinus Torvalds 	int error;
1172346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
1173346c7a68STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	if ((error = snd_opti93x_open(chip, OPTi93X_MODE_CAPTURE)) < 0)
11761da177e4SLinus Torvalds 		return error;
11771da177e4SLinus Torvalds 	runtime->hw = snd_opti93x_capture;
11781da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
11791da177e4SLinus Torvalds 	chip->capture_substream = substream;
11801da177e4SLinus Torvalds 	snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
11811da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
11821da177e4SLinus Torvalds 	return error;
11831da177e4SLinus Torvalds }
11841da177e4SLinus Torvalds 
1185346c7a68STakashi Iwai static int snd_opti93x_playback_close(struct snd_pcm_substream *substream)
11861da177e4SLinus Torvalds {
1187346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	chip->playback_substream = NULL;
11901da177e4SLinus Torvalds 	snd_opti93x_close(chip, OPTi93X_MODE_PLAY);
11911da177e4SLinus Torvalds 	return 0;
11921da177e4SLinus Torvalds }
11931da177e4SLinus Torvalds 
1194346c7a68STakashi Iwai static int snd_opti93x_capture_close(struct snd_pcm_substream *substream)
11951da177e4SLinus Torvalds {
1196346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_pcm_substream_chip(substream);
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	chip->capture_substream = NULL;
11991da177e4SLinus Torvalds 	snd_opti93x_close(chip, OPTi93X_MODE_CAPTURE);
12001da177e4SLinus Torvalds 	return 0;
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 
1204346c7a68STakashi Iwai static void snd_opti93x_init(struct snd_opti93x *chip)
12051da177e4SLinus Torvalds {
12061da177e4SLinus Torvalds 	unsigned long flags;
12071da177e4SLinus Torvalds 	int i;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
12101da177e4SLinus Torvalds 	snd_opti93x_mce_up(chip);
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	for (i = 0; i < 32; i++)
12131da177e4SLinus Torvalds 		snd_opti93x_out_image(chip, i, snd_opti93x_default_image[i]);
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	snd_opti93x_mce_down(chip);
12161da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
12171da177e4SLinus Torvalds }
12181da177e4SLinus Torvalds 
1219346c7a68STakashi Iwai static int snd_opti93x_probe(struct snd_opti93x *chip)
12201da177e4SLinus Torvalds {
12211da177e4SLinus Torvalds 	unsigned long flags;
12221da177e4SLinus Torvalds 	unsigned char val;
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
12251da177e4SLinus Torvalds 	val = snd_opti93x_in(chip, OPTi93X_ID) & 0x0f;
12261da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	return (val == 0x0a) ? 0 : -ENODEV;
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
1231346c7a68STakashi Iwai static int snd_opti93x_free(struct snd_opti93x *chip)
12321da177e4SLinus Torvalds {
1233b1d5776dSTakashi Iwai 	release_and_free_resource(chip->res_port);
12341da177e4SLinus Torvalds 	if (chip->dma1 >= 0) {
12351da177e4SLinus Torvalds 		disable_dma(chip->dma1);
12361da177e4SLinus Torvalds 		free_dma(chip->dma1);
12371da177e4SLinus Torvalds 	}
12381da177e4SLinus Torvalds 	if (chip->dma2 >= 0) {
12391da177e4SLinus Torvalds 		disable_dma(chip->dma2);
12401da177e4SLinus Torvalds 		free_dma(chip->dma2);
12411da177e4SLinus Torvalds 	}
12421da177e4SLinus Torvalds 	if (chip->irq >= 0) {
12431da177e4SLinus Torvalds 	  free_irq(chip->irq, chip);
12441da177e4SLinus Torvalds 	}
12451da177e4SLinus Torvalds 	kfree(chip);
12461da177e4SLinus Torvalds 	return 0;
12471da177e4SLinus Torvalds }
12481da177e4SLinus Torvalds 
1249346c7a68STakashi Iwai static int snd_opti93x_dev_free(struct snd_device *device)
12501da177e4SLinus Torvalds {
1251346c7a68STakashi Iwai 	struct snd_opti93x *chip = device->device_data;
12521da177e4SLinus Torvalds 	return snd_opti93x_free(chip);
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
1255346c7a68STakashi Iwai static const char *snd_opti93x_chip_id(struct snd_opti93x *codec)
12561da177e4SLinus Torvalds {
12571da177e4SLinus Torvalds 	switch (codec->hardware) {
12581da177e4SLinus Torvalds 	case OPTi9XX_HW_82C930: return "82C930";
12591da177e4SLinus Torvalds 	case OPTi9XX_HW_82C931: return "82C931";
12601da177e4SLinus Torvalds 	case OPTi9XX_HW_82C933: return "82C933";
12611da177e4SLinus Torvalds 	default:		return "???";
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
1265346c7a68STakashi Iwai static int snd_opti93x_create(struct snd_card *card, struct snd_opti9xx *chip,
12661da177e4SLinus Torvalds 			      int dma1, int dma2,
1267346c7a68STakashi Iwai 			      struct snd_opti93x **rcodec)
12681da177e4SLinus Torvalds {
1269346c7a68STakashi Iwai 	static struct snd_device_ops ops = {
12701da177e4SLinus Torvalds 		.dev_free =	snd_opti93x_dev_free,
12711da177e4SLinus Torvalds 	};
12721da177e4SLinus Torvalds 	int error;
1273346c7a68STakashi Iwai 	struct snd_opti93x *codec;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	*rcodec = NULL;
12769e76a76eSTakashi Iwai 	codec = kzalloc(sizeof(*codec), GFP_KERNEL);
12771da177e4SLinus Torvalds 	if (codec == NULL)
12781da177e4SLinus Torvalds 		return -ENOMEM;
12791da177e4SLinus Torvalds 	codec->irq = -1;
12801da177e4SLinus Torvalds 	codec->dma1 = -1;
12811da177e4SLinus Torvalds 	codec->dma2 = -1;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	if ((codec->res_port = request_region(chip->wss_base + 4, 4, "OPTI93x CODEC")) == NULL) {
12841da177e4SLinus Torvalds 		snd_printk(KERN_ERR "opti9xx: can't grab port 0x%lx\n", chip->wss_base + 4);
12851da177e4SLinus Torvalds 		snd_opti93x_free(codec);
12861da177e4SLinus Torvalds 		return -EBUSY;
12871da177e4SLinus Torvalds 	}
12881da177e4SLinus Torvalds 	if (request_dma(dma1, "OPTI93x - 1")) {
12891da177e4SLinus Torvalds 		snd_printk(KERN_ERR "opti9xx: can't grab DMA1 %d\n", dma1);
12901da177e4SLinus Torvalds 		snd_opti93x_free(codec);
12911da177e4SLinus Torvalds 		return -EBUSY;
12921da177e4SLinus Torvalds 	}
12931da177e4SLinus Torvalds 	codec->dma1 = chip->dma1;
12941da177e4SLinus Torvalds 	if (request_dma(dma2, "OPTI93x - 2")) {
12951da177e4SLinus Torvalds 		snd_printk(KERN_ERR "opti9xx: can't grab DMA2 %d\n", dma2);
12961da177e4SLinus Torvalds 		snd_opti93x_free(codec);
12971da177e4SLinus Torvalds 		return -EBUSY;
12981da177e4SLinus Torvalds 	}
12991da177e4SLinus Torvalds 	codec->dma2 = chip->dma2;
13001da177e4SLinus Torvalds 
130183c51c0aSRene Herman 	if (request_irq(chip->irq, snd_opti93x_interrupt, IRQF_DISABLED, DEV_NAME" - WSS", codec)) {
13021da177e4SLinus Torvalds 		snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", chip->irq);
13031da177e4SLinus Torvalds 		snd_opti93x_free(codec);
13041da177e4SLinus Torvalds 		return -EBUSY;
13051da177e4SLinus Torvalds 	}
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	codec->card = card;
13081da177e4SLinus Torvalds 	codec->port = chip->wss_base + 4;
13091da177e4SLinus Torvalds 	codec->irq = chip->irq;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	spin_lock_init(&codec->lock);
13121da177e4SLinus Torvalds 	codec->hardware = chip->hardware;
13131da177e4SLinus Torvalds 	codec->chip = chip;
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	if ((error = snd_opti93x_probe(codec))) {
13161da177e4SLinus Torvalds 		snd_opti93x_free(codec);
13171da177e4SLinus Torvalds 		return error;
13181da177e4SLinus Torvalds 	}
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	snd_opti93x_init(codec);
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	/* Register device */
13231da177e4SLinus Torvalds 	if ((error = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
13241da177e4SLinus Torvalds 		snd_opti93x_free(codec);
13251da177e4SLinus Torvalds 		return error;
13261da177e4SLinus Torvalds 	}
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	*rcodec = codec;
13291da177e4SLinus Torvalds 	return 0;
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds 
1332346c7a68STakashi Iwai static struct snd_pcm_ops snd_opti93x_playback_ops = {
13331da177e4SLinus Torvalds 	.open =		snd_opti93x_playback_open,
13341da177e4SLinus Torvalds 	.close =	snd_opti93x_playback_close,
13351da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
13361da177e4SLinus Torvalds 	.hw_params =	snd_opti93x_hw_params,
13371da177e4SLinus Torvalds 	.hw_free =	snd_opti93x_hw_free,
13381da177e4SLinus Torvalds 	.prepare =	snd_opti93x_playback_prepare,
13391da177e4SLinus Torvalds 	.trigger =	snd_opti93x_playback_trigger,
13401da177e4SLinus Torvalds 	.pointer =	snd_opti93x_playback_pointer,
13411da177e4SLinus Torvalds };
13421da177e4SLinus Torvalds 
1343346c7a68STakashi Iwai static struct snd_pcm_ops snd_opti93x_capture_ops = {
13441da177e4SLinus Torvalds 	.open =		snd_opti93x_capture_open,
13451da177e4SLinus Torvalds 	.close =	snd_opti93x_capture_close,
13461da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
13471da177e4SLinus Torvalds 	.hw_params =	snd_opti93x_hw_params,
13481da177e4SLinus Torvalds 	.hw_free =	snd_opti93x_hw_free,
13491da177e4SLinus Torvalds 	.prepare =	snd_opti93x_capture_prepare,
13501da177e4SLinus Torvalds 	.trigger =	snd_opti93x_capture_trigger,
13511da177e4SLinus Torvalds 	.pointer =	snd_opti93x_capture_pointer,
13521da177e4SLinus Torvalds };
13531da177e4SLinus Torvalds 
1354346c7a68STakashi Iwai static int snd_opti93x_pcm(struct snd_opti93x *codec, int device, struct snd_pcm **rpcm)
13551da177e4SLinus Torvalds {
13561da177e4SLinus Torvalds 	int error;
1357346c7a68STakashi Iwai 	struct snd_pcm *pcm;
13581da177e4SLinus Torvalds 
135991134859STakashi Iwai 	if ((error = snd_pcm_new(codec->card, "OPTi 82C93X", device, 1, 1, &pcm)) < 0)
13601da177e4SLinus Torvalds 		return error;
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_opti93x_playback_ops);
13631da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_opti93x_capture_ops);
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	pcm->private_data = codec;
13661da177e4SLinus Torvalds 	pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	strcpy(pcm->name, snd_opti93x_chip_id(codec));
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
13711da177e4SLinus Torvalds 					      snd_dma_isa_data(),
13721da177e4SLinus Torvalds 					      64*1024, codec->dma1 > 3 || codec->dma2 > 3 ? 128*1024 : 64*1024);
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	codec->pcm = pcm;
13751da177e4SLinus Torvalds 	if (rpcm)
13761da177e4SLinus Torvalds 		*rpcm = pcm;
13771da177e4SLinus Torvalds 	return 0;
13781da177e4SLinus Torvalds }
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds /*
13811da177e4SLinus Torvalds  *  MIXER part
13821da177e4SLinus Torvalds  */
13831da177e4SLinus Torvalds 
1384346c7a68STakashi Iwai static int snd_opti93x_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
13851da177e4SLinus Torvalds {
13861da177e4SLinus Torvalds 	static char *texts[4] = {
13871da177e4SLinus Torvalds 		"Line1", "Aux", "Mic", "Mix"
13881da177e4SLinus Torvalds 	};
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
13911da177e4SLinus Torvalds 	uinfo->count = 2;
13921da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 4;
13931da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item > 3)
13941da177e4SLinus Torvalds 		uinfo->value.enumerated.item = 3;
13951da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
13961da177e4SLinus Torvalds 	return 0;
13971da177e4SLinus Torvalds }
13981da177e4SLinus Torvalds 
1399346c7a68STakashi Iwai static int snd_opti93x_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
14001da177e4SLinus Torvalds {
1401346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
14021da177e4SLinus Torvalds 	unsigned long flags;
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
14051da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = (chip->image[OPTi93X_MIXOUT_LEFT] & OPTi93X_MIXOUT_MIXER) >> 6;
14061da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[1] = (chip->image[OPTi93X_MIXOUT_RIGHT] & OPTi93X_MIXOUT_MIXER) >> 6;
14071da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
14081da177e4SLinus Torvalds 	return 0;
14091da177e4SLinus Torvalds }
14101da177e4SLinus Torvalds 
1411346c7a68STakashi Iwai static int snd_opti93x_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
14121da177e4SLinus Torvalds {
1413346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
14141da177e4SLinus Torvalds 	unsigned long flags;
14151da177e4SLinus Torvalds 	unsigned short left, right;
14161da177e4SLinus Torvalds 	int change;
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	if (ucontrol->value.enumerated.item[0] > 3 ||
14191da177e4SLinus Torvalds 	    ucontrol->value.enumerated.item[1] > 3)
14201da177e4SLinus Torvalds 		return -EINVAL;
14211da177e4SLinus Torvalds 	left = ucontrol->value.enumerated.item[0] << 6;
14221da177e4SLinus Torvalds 	right = ucontrol->value.enumerated.item[1] << 6;
14231da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
14241da177e4SLinus Torvalds 	left = (chip->image[OPTi93X_MIXOUT_LEFT] & ~OPTi93X_MIXOUT_MIXER) | left;
14251da177e4SLinus Torvalds 	right = (chip->image[OPTi93X_MIXOUT_RIGHT] & ~OPTi93X_MIXOUT_MIXER) | right;
14261da177e4SLinus Torvalds 	change = left != chip->image[OPTi93X_MIXOUT_LEFT] ||
14271da177e4SLinus Torvalds 	         right != chip->image[OPTi93X_MIXOUT_RIGHT];
14281da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_MIXOUT_LEFT, left);
14291da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_MIXOUT_RIGHT, right);
14301da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
14311da177e4SLinus Torvalds 	return change;
14321da177e4SLinus Torvalds }
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds #if 0
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds #define OPTi93X_SINGLE(xname, xindex, reg, shift, mask, invert) \
14371da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
14381da177e4SLinus Torvalds   .info = snd_opti93x_info_single, \
14391da177e4SLinus Torvalds   .get = snd_opti93x_get_single, .put = snd_opti93x_put_single, \
14401da177e4SLinus Torvalds   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
14411da177e4SLinus Torvalds 
1442346c7a68STakashi Iwai static int snd_opti93x_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
14431da177e4SLinus Torvalds {
14441da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
14471da177e4SLinus Torvalds 	uinfo->count = 1;
14481da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
14491da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
14501da177e4SLinus Torvalds 	return 0;
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
1453346c7a68STakashi Iwai static int snd_opti93x_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
14541da177e4SLinus Torvalds {
1455346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
14561da177e4SLinus Torvalds 	unsigned long flags;
14571da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
14581da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
14591da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
14601da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
14631da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
14641da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
14651da177e4SLinus Torvalds 	if (invert)
14661da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
14671da177e4SLinus Torvalds 	return 0;
14681da177e4SLinus Torvalds }
14691da177e4SLinus Torvalds 
1470346c7a68STakashi Iwai static int snd_opti93x_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
14711da177e4SLinus Torvalds {
1472346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
14731da177e4SLinus Torvalds 	unsigned long flags;
14741da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
14751da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
14761da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
14771da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
14781da177e4SLinus Torvalds 	int change;
14791da177e4SLinus Torvalds 	unsigned short val;
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
14821da177e4SLinus Torvalds 	if (invert)
14831da177e4SLinus Torvalds 		val = mask - val;
14841da177e4SLinus Torvalds 	val <<= shift;
14851da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
14861da177e4SLinus Torvalds 	val = (chip->image[reg] & ~(mask << shift)) | val;
14871da177e4SLinus Torvalds 	change = val != chip->image[reg];
14881da177e4SLinus Torvalds 	snd_opti93x_out(chip, reg, val);
14891da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
14901da177e4SLinus Torvalds 	return change;
14911da177e4SLinus Torvalds }
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds #endif /* single */
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds #define OPTi93X_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
14961da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
14971da177e4SLinus Torvalds   .info = snd_opti93x_info_double, \
14981da177e4SLinus Torvalds   .get = snd_opti93x_get_double, .put = snd_opti93x_put_double, \
14991da177e4SLinus Torvalds   .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds #define OPTi93X_DOUBLE_INVERT_INVERT(xctl) \
15021da177e4SLinus Torvalds 	do { xctl.private_value ^= 22; } while (0)
15031da177e4SLinus Torvalds #define OPTi93X_DOUBLE_CHANGE_REGS(xctl, left_reg, right_reg) \
15041da177e4SLinus Torvalds 	do { xctl.private_value &= ~0x0000ffff; \
15051da177e4SLinus Torvalds 	     xctl.private_value |= left_reg | (right_reg << 8); } while (0)
15061da177e4SLinus Torvalds 
1507346c7a68STakashi Iwai static int snd_opti93x_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15081da177e4SLinus Torvalds {
15091da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 24) & 0xff;
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
15121da177e4SLinus Torvalds 	uinfo->count = 2;
15131da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
15141da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
15151da177e4SLinus Torvalds 	return 0;
15161da177e4SLinus Torvalds }
15171da177e4SLinus Torvalds 
1518346c7a68STakashi Iwai static int snd_opti93x_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15191da177e4SLinus Torvalds {
1520346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
15211da177e4SLinus Torvalds 	unsigned long flags;
15221da177e4SLinus Torvalds 	int left_reg = kcontrol->private_value & 0xff;
15231da177e4SLinus Torvalds 	int right_reg = (kcontrol->private_value >> 8) & 0xff;
15241da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 16) & 0x07;
15251da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 19) & 0x07;
15261da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 24) & 0xff;
15271da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 22) & 1;
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
15301da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
15311da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
15321da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
15331da177e4SLinus Torvalds 	if (invert) {
15341da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
15351da177e4SLinus Torvalds 		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
15361da177e4SLinus Torvalds 	}
15371da177e4SLinus Torvalds 	return 0;
15381da177e4SLinus Torvalds }
15391da177e4SLinus Torvalds 
1540346c7a68STakashi Iwai static int snd_opti93x_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15411da177e4SLinus Torvalds {
1542346c7a68STakashi Iwai 	struct snd_opti93x *chip = snd_kcontrol_chip(kcontrol);
15431da177e4SLinus Torvalds 	unsigned long flags;
15441da177e4SLinus Torvalds 	int left_reg = kcontrol->private_value & 0xff;
15451da177e4SLinus Torvalds 	int right_reg = (kcontrol->private_value >> 8) & 0xff;
15461da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 16) & 0x07;
15471da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 19) & 0x07;
15481da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 24) & 0xff;
15491da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 22) & 1;
15501da177e4SLinus Torvalds 	int change;
15511da177e4SLinus Torvalds 	unsigned short val1, val2;
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
15541da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
15551da177e4SLinus Torvalds 	if (invert) {
15561da177e4SLinus Torvalds 		val1 = mask - val1;
15571da177e4SLinus Torvalds 		val2 = mask - val2;
15581da177e4SLinus Torvalds 	}
15591da177e4SLinus Torvalds 	val1 <<= shift_left;
15601da177e4SLinus Torvalds 	val2 <<= shift_right;
15611da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
15621da177e4SLinus Torvalds 	val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
15631da177e4SLinus Torvalds 	val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
15641da177e4SLinus Torvalds 	change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
15651da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, left_reg, val1);
15661da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, right_reg, val2);
15671da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
15681da177e4SLinus Torvalds 	return change;
15691da177e4SLinus Torvalds }
15701da177e4SLinus Torvalds 
15715e24c1c1STakashi Iwai static struct snd_kcontrol_new snd_opti93x_controls[] __devinitdata = {
15721da177e4SLinus Torvalds OPTi93X_DOUBLE("Master Playback Switch", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
15731da177e4SLinus Torvalds OPTi93X_DOUBLE("Master Playback Volume", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1),
15741da177e4SLinus Torvalds OPTi93X_DOUBLE("PCM Playback Switch", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 7, 7, 1, 1),
15751da177e4SLinus Torvalds OPTi93X_DOUBLE("PCM Playback Volume", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 0, 0, 31, 1),
15761da177e4SLinus Torvalds OPTi93X_DOUBLE("FM Playback Switch", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 7, 7, 1, 1),
15771da177e4SLinus Torvalds OPTi93X_DOUBLE("FM Playback Volume", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 1, 1, 15, 1),
15781da177e4SLinus Torvalds OPTi93X_DOUBLE("Line Playback Switch", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 7, 7, 1, 1),
15791da177e4SLinus Torvalds OPTi93X_DOUBLE("Line Playback Volume", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 1, 1, 15, 1),
15801da177e4SLinus Torvalds OPTi93X_DOUBLE("Mic Playback Switch", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
15811da177e4SLinus Torvalds OPTi93X_DOUBLE("Mic Playback Volume", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1),
15821da177e4SLinus Torvalds OPTi93X_DOUBLE("Mic Boost", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 5, 5, 1, 1),
15831da177e4SLinus Torvalds OPTi93X_DOUBLE("CD Playback Switch", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 7, 7, 1, 1),
15841da177e4SLinus Torvalds OPTi93X_DOUBLE("CD Playback Volume", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 1, 1, 15, 1),
15851da177e4SLinus Torvalds OPTi93X_DOUBLE("Aux Playback Switch", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
15861da177e4SLinus Torvalds OPTi93X_DOUBLE("Aux Playback Volume", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1),
15871da177e4SLinus Torvalds OPTi93X_DOUBLE("Capture Volume", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 0, 0, 15, 0),
15881da177e4SLinus Torvalds {
15891da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
15901da177e4SLinus Torvalds 	.name = "Capture Source",
15911da177e4SLinus Torvalds 	.info = snd_opti93x_info_mux,
15921da177e4SLinus Torvalds 	.get = snd_opti93x_get_mux,
15931da177e4SLinus Torvalds 	.put = snd_opti93x_put_mux,
15941da177e4SLinus Torvalds }
15951da177e4SLinus Torvalds };
15961da177e4SLinus Torvalds 
15979478bc3bSRandy Dunlap static int __devinit snd_opti93x_mixer(struct snd_opti93x *chip)
15981da177e4SLinus Torvalds {
1599346c7a68STakashi Iwai 	struct snd_card *card;
1600346c7a68STakashi Iwai 	struct snd_kcontrol_new knew;
16011da177e4SLinus Torvalds 	int err;
16021da177e4SLinus Torvalds 	unsigned int idx;
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	card = chip->card;
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds 	strcpy(card->mixername, snd_opti93x_chip_id(chip));
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
16111da177e4SLinus Torvalds 		knew = snd_opti93x_controls[idx];
16121da177e4SLinus Torvalds 		if (chip->hardware == OPTi9XX_HW_82C930) {
16131da177e4SLinus Torvalds 			if (strstr(knew.name, "FM"))	/* skip FM controls */
16141da177e4SLinus Torvalds 				continue;
16151da177e4SLinus Torvalds 			else if (strcmp(knew.name, "Mic Playback Volume"))
16161da177e4SLinus Torvalds 				OPTi93X_DOUBLE_INVERT_INVERT(knew);
16171da177e4SLinus Torvalds 			else if (strstr(knew.name, "Aux"))
16181da177e4SLinus Torvalds 				OPTi93X_DOUBLE_CHANGE_REGS(knew, OPTi930_AUX_LEFT_INPUT, OPTi930_AUX_RIGHT_INPUT);
16191da177e4SLinus Torvalds 			else if (strcmp(knew.name, "PCM Playback Volume"))
16201da177e4SLinus Torvalds 				OPTi93X_DOUBLE_INVERT_INVERT(knew);
16211da177e4SLinus Torvalds 			else if (strcmp(knew.name, "Master Playback Volume"))
16221da177e4SLinus Torvalds 				OPTi93X_DOUBLE_INVERT_INVERT(knew);
16231da177e4SLinus Torvalds 		}
16241da177e4SLinus Torvalds 		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_opti93x_controls[idx], chip))) < 0)
16251da177e4SLinus Torvalds 			return err;
16261da177e4SLinus Torvalds 	}
16271da177e4SLinus Torvalds 	return 0;
16281da177e4SLinus Torvalds }
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds #endif /* OPTi93X */
16311da177e4SLinus Torvalds 
16325e24c1c1STakashi Iwai static int __devinit snd_card_opti9xx_detect(struct snd_card *card,
16335e24c1c1STakashi Iwai 					     struct snd_opti9xx *chip)
16341da177e4SLinus Torvalds {
16351da177e4SLinus Torvalds 	int i, err;
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds #ifndef OPTi93X
16381da177e4SLinus Torvalds 	for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
16391da177e4SLinus Torvalds 		unsigned char value;
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 		if ((err = snd_opti9xx_init(chip, i)) < 0)
16421da177e4SLinus Torvalds 			return err;
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 		if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
16451da177e4SLinus Torvalds 			continue;
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 		value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
16481da177e4SLinus Torvalds 		if ((value != 0xff) && (value != inb(chip->mc_base + 1)))
16491da177e4SLinus Torvalds 			if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
16501da177e4SLinus Torvalds 				return 1;
16511da177e4SLinus Torvalds 
1652b1d5776dSTakashi Iwai 		release_and_free_resource(chip->res_mc_base);
16531da177e4SLinus Torvalds 		chip->res_mc_base = NULL;
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 	}
16561da177e4SLinus Torvalds #else	/* OPTi93X */
16571da177e4SLinus Torvalds 	for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
16581da177e4SLinus Torvalds 		unsigned long flags;
16591da177e4SLinus Torvalds 		unsigned char value;
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 		if ((err = snd_opti9xx_init(chip, i)) < 0)
16621da177e4SLinus Torvalds 			return err;
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 		if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
16651da177e4SLinus Torvalds 			continue;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 		spin_lock_irqsave(&chip->lock, flags);
16681da177e4SLinus Torvalds 		outb(chip->password, chip->mc_base + chip->pwd_reg);
16691da177e4SLinus Torvalds 		outb(((chip->mc_indir_index & (1 << 8)) >> 4) |
16701da177e4SLinus Torvalds 			((chip->mc_indir_index & 0xf0) >> 4), chip->mc_base);
16711da177e4SLinus Torvalds 		spin_unlock_irqrestore(&chip->lock, flags);
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 		value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
16741da177e4SLinus Torvalds 		snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
16751da177e4SLinus Torvalds 		if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
16761da177e4SLinus Torvalds 			return 1;
16771da177e4SLinus Torvalds 
1678b1d5776dSTakashi Iwai 		release_and_free_resource(chip->res_mc_base);
16791da177e4SLinus Torvalds 		chip->res_mc_base = NULL;
16801da177e4SLinus Torvalds 	}
16811da177e4SLinus Torvalds #endif	/* OPTi93X */
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	return -ENODEV;
16841da177e4SLinus Torvalds }
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds #ifdef CONFIG_PNP
16875e24c1c1STakashi Iwai static int __devinit snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
16885e24c1c1STakashi Iwai 					  struct pnp_card_link *card,
16891da177e4SLinus Torvalds 					  const struct pnp_card_device_id *pid)
16901da177e4SLinus Torvalds {
16911da177e4SLinus Torvalds 	struct pnp_dev *pdev;
16921da177e4SLinus Torvalds 	int err;
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	chip->dev = pnp_request_card_device(card, pid->devs[0].id, NULL);
1695109c53f8SRene Herman 	if (chip->dev == NULL)
16961da177e4SLinus Torvalds 		return -EBUSY;
1697109c53f8SRene Herman 
16981da177e4SLinus Torvalds 	chip->devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 	pdev = chip->dev;
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	err = pnp_activate_dev(pdev);
17031da177e4SLinus Torvalds 	if (err < 0) {
17041da177e4SLinus Torvalds 		snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
17051da177e4SLinus Torvalds 		return err;
17061da177e4SLinus Torvalds 	}
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds #ifdef OPTi93X
17091da177e4SLinus Torvalds 	port = pnp_port_start(pdev, 0) - 4;
17101ea73412STakashi Iwai 	fm_port = pnp_port_start(pdev, 1) + 8;
17111da177e4SLinus Torvalds #else
17121da177e4SLinus Torvalds 	if (pid->driver_data != 0x0924)
17131da177e4SLinus Torvalds 		port = pnp_port_start(pdev, 1);
17141ea73412STakashi Iwai 	fm_port = pnp_port_start(pdev, 2) + 8;
17151da177e4SLinus Torvalds #endif	/* OPTi93X */
17161da177e4SLinus Torvalds 	irq = pnp_irq(pdev, 0);
17171da177e4SLinus Torvalds 	dma1 = pnp_dma(pdev, 0);
17181da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
17191da177e4SLinus Torvalds 	dma2 = pnp_dma(pdev, 1);
17201da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	pdev = chip->devmpu;
17231da177e4SLinus Torvalds 	if (pdev && mpu_port > 0) {
17241da177e4SLinus Torvalds 		err = pnp_activate_dev(pdev);
17251da177e4SLinus Torvalds 		if (err < 0) {
17261da177e4SLinus Torvalds 			snd_printk(KERN_ERR "AUDIO pnp configure failure\n");
17271da177e4SLinus Torvalds 			mpu_port = -1;
17281da177e4SLinus Torvalds 			chip->devmpu = NULL;
17291da177e4SLinus Torvalds 		} else {
17301da177e4SLinus Torvalds 			mpu_port = pnp_port_start(pdev, 0);
17311da177e4SLinus Torvalds 			mpu_irq = pnp_irq(pdev, 0);
17321da177e4SLinus Torvalds 		}
17331da177e4SLinus Torvalds 	}
17341da177e4SLinus Torvalds 	return pid->driver_data;
17351da177e4SLinus Torvalds }
17361da177e4SLinus Torvalds #endif	/* CONFIG_PNP */
17371da177e4SLinus Torvalds 
1738346c7a68STakashi Iwai static void snd_card_opti9xx_free(struct snd_card *card)
17391da177e4SLinus Torvalds {
174099a0b768STakashi Iwai 	struct snd_opti9xx *chip = card->private_data;
17411da177e4SLinus Torvalds 
1742b1d5776dSTakashi Iwai 	if (chip)
1743b1d5776dSTakashi Iwai 		release_and_free_resource(chip->res_mc_base);
17441da177e4SLinus Torvalds }
17451da177e4SLinus Torvalds 
17465e24c1c1STakashi Iwai static int __devinit snd_opti9xx_probe(struct snd_card *card)
17471da177e4SLinus Torvalds {
17481da177e4SLinus Torvalds 	static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
17491da177e4SLinus Torvalds 	int error;
175099a0b768STakashi Iwai 	struct snd_opti9xx *chip = card->private_data;
17511da177e4SLinus Torvalds #if defined(OPTi93X)
1752346c7a68STakashi Iwai 	struct snd_opti93x *codec;
17531da177e4SLinus Torvalds #elif defined(CS4231)
1754346c7a68STakashi Iwai 	struct snd_cs4231 *codec;
1755346c7a68STakashi Iwai 	struct snd_timer *timer;
17561da177e4SLinus Torvalds #else
1757346c7a68STakashi Iwai 	struct snd_ad1848 *codec;
17581da177e4SLinus Torvalds #endif
1759346c7a68STakashi Iwai 	struct snd_pcm *pcm;
1760346c7a68STakashi Iwai 	struct snd_rawmidi *rmidi;
1761346c7a68STakashi Iwai 	struct snd_hwdep *synth;
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds 	if (! chip->res_mc_base &&
176499a0b768STakashi Iwai 	    (chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
176599a0b768STakashi Iwai 						"OPTi9xx MC")) == NULL)
17661da177e4SLinus Torvalds 		return -ENOMEM;
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	chip->wss_base = port;
17691da177e4SLinus Torvalds 	chip->fm_port = fm_port;
17701da177e4SLinus Torvalds 	chip->mpu_port = mpu_port;
17711da177e4SLinus Torvalds 	chip->irq = irq;
17721da177e4SLinus Torvalds 	chip->mpu_irq = mpu_irq;
17731da177e4SLinus Torvalds 	chip->dma1 = dma1;
17741da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
17751da177e4SLinus Torvalds 	chip->dma2 = dma2;
17761da177e4SLinus Torvalds #endif
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 	if (chip->wss_base == SNDRV_AUTO_PORT) {
17791da177e4SLinus Torvalds 		if ((chip->wss_base = snd_legacy_find_free_ioport(possible_ports, 4)) < 0) {
17801da177e4SLinus Torvalds 			snd_printk("unable to find a free WSS port\n");
17811da177e4SLinus Torvalds 			return -EBUSY;
17821da177e4SLinus Torvalds 		}
17831da177e4SLinus Torvalds 	}
178499a0b768STakashi Iwai 	if ((error = snd_opti9xx_configure(chip)))
17851da177e4SLinus Torvalds 		return error;
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds #if defined(OPTi93X)
178899a0b768STakashi Iwai 	if ((error = snd_opti93x_create(card, chip, chip->dma1, chip->dma2, &codec)))
17891da177e4SLinus Torvalds 		return error;
179099a0b768STakashi Iwai 	if ((error = snd_opti93x_pcm(codec, 0, &pcm)) < 0)
17911da177e4SLinus Torvalds 		return error;
179299a0b768STakashi Iwai 	if ((error = snd_opti93x_mixer(codec)) < 0)
17931da177e4SLinus Torvalds 		return error;
17941da177e4SLinus Torvalds #elif defined(CS4231)
17951da177e4SLinus Torvalds 	if ((error = snd_cs4231_create(card, chip->wss_base + 4, -1,
17961da177e4SLinus Torvalds 				       chip->irq, chip->dma1, chip->dma2,
17971da177e4SLinus Torvalds 				       CS4231_HW_DETECT,
17981da177e4SLinus Torvalds 				       0,
179999a0b768STakashi Iwai 				       &codec)) < 0)
18001da177e4SLinus Torvalds 		return error;
180199a0b768STakashi Iwai 	if ((error = snd_cs4231_pcm(codec, 0, &pcm)) < 0)
18021da177e4SLinus Torvalds 		return error;
180399a0b768STakashi Iwai 	if ((error = snd_cs4231_mixer(codec)) < 0)
18041da177e4SLinus Torvalds 		return error;
180599a0b768STakashi Iwai 	if ((error = snd_cs4231_timer(codec, 0, &timer)) < 0)
18061da177e4SLinus Torvalds 		return error;
18071da177e4SLinus Torvalds #else
18081da177e4SLinus Torvalds 	if ((error = snd_ad1848_create(card, chip->wss_base + 4,
18091da177e4SLinus Torvalds 				       chip->irq, chip->dma1,
181099a0b768STakashi Iwai 				       AD1848_HW_DETECT, &codec)) < 0)
18111da177e4SLinus Torvalds 		return error;
181299a0b768STakashi Iwai 	if ((error = snd_ad1848_pcm(codec, 0, &pcm)) < 0)
18131da177e4SLinus Torvalds 		return error;
181499a0b768STakashi Iwai 	if ((error = snd_ad1848_mixer(codec)) < 0)
18151da177e4SLinus Torvalds 		return error;
18161da177e4SLinus Torvalds #endif
18171da177e4SLinus Torvalds 	strcpy(card->driver, chip->name);
18181da177e4SLinus Torvalds 	sprintf(card->shortname, "OPTi %s", card->driver);
18191da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
18201da177e4SLinus Torvalds 	sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
18211da177e4SLinus Torvalds 		card->shortname, pcm->name, chip->wss_base + 4,
18221da177e4SLinus Torvalds 		chip->irq, chip->dma1, chip->dma2);
18231da177e4SLinus Torvalds #else
18241da177e4SLinus Torvalds 	sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
18251da177e4SLinus Torvalds 		card->shortname, pcm->name, chip->wss_base + 4,
18261da177e4SLinus Torvalds 		chip->irq, chip->dma1);
18271da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	if (chip->mpu_port <= 0 || chip->mpu_port == SNDRV_AUTO_PORT)
18301da177e4SLinus Torvalds 		rmidi = NULL;
18311da177e4SLinus Torvalds 	else
18321da177e4SLinus Torvalds 		if ((error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
183365ca68b3SThomas Gleixner 				chip->mpu_port, 0, chip->mpu_irq, IRQF_DISABLED,
18341da177e4SLinus Torvalds 				&rmidi)))
183599a0b768STakashi Iwai 			snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
183699a0b768STakashi Iwai 				   chip->mpu_port);
18371da177e4SLinus Torvalds 
18381da177e4SLinus Torvalds 	if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
1839346c7a68STakashi Iwai 		struct snd_opl3 *opl3 = NULL;
18401da177e4SLinus Torvalds #ifndef OPTi93X
18411da177e4SLinus Torvalds 		if (chip->hardware == OPTi9XX_HW_82C928 ||
18421da177e4SLinus Torvalds 		    chip->hardware == OPTi9XX_HW_82C929 ||
18431da177e4SLinus Torvalds 		    chip->hardware == OPTi9XX_HW_82C924) {
1844346c7a68STakashi Iwai 			struct snd_opl4 *opl4;
18451da177e4SLinus Torvalds 			/* assume we have an OPL4 */
18461da177e4SLinus Torvalds 			snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
18471da177e4SLinus Torvalds 					       0x20, 0x20);
18481da177e4SLinus Torvalds 			if (snd_opl4_create(card,
18491da177e4SLinus Torvalds 					    chip->fm_port,
18501da177e4SLinus Torvalds 					    chip->fm_port - 8,
18511da177e4SLinus Torvalds 					    2, &opl3, &opl4) < 0) {
18521da177e4SLinus Torvalds 				/* no luck, use OPL3 instead */
18531da177e4SLinus Torvalds 				snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
18541da177e4SLinus Torvalds 						       0x00, 0x20);
18551da177e4SLinus Torvalds 			}
18561da177e4SLinus Torvalds 		}
18571da177e4SLinus Torvalds #endif	/* !OPTi93X */
18581da177e4SLinus Torvalds 		if (!opl3 && snd_opl3_create(card,
18591da177e4SLinus Torvalds 					     chip->fm_port,
18601da177e4SLinus Torvalds 					     chip->fm_port + 2,
18611da177e4SLinus Torvalds 					     OPL3_HW_AUTO, 0, &opl3) < 0) {
186299a0b768STakashi Iwai 			snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
18631da177e4SLinus Torvalds 				   chip->fm_port, chip->fm_port + 4 - 1);
18641da177e4SLinus Torvalds 		}
18651da177e4SLinus Torvalds 		if (opl3) {
18661da177e4SLinus Torvalds #ifdef CS4231
186799a0b768STakashi Iwai 			const int t1dev = 1;
18681da177e4SLinus Torvalds #else
186999a0b768STakashi Iwai 			const int t1dev = 0;
187099a0b768STakashi Iwai #endif
187199a0b768STakashi Iwai 			if ((error = snd_opl3_timer_new(opl3, t1dev, t1dev+1)) < 0)
18721da177e4SLinus Torvalds 				return error;
187399a0b768STakashi Iwai 			if ((error = snd_opl3_hwdep_new(opl3, 0, 1, &synth)) < 0)
18741da177e4SLinus Torvalds 				return error;
18751da177e4SLinus Torvalds 		}
18761da177e4SLinus Torvalds 	}
18771da177e4SLinus Torvalds 
187899a0b768STakashi Iwai 	return snd_card_register(card);
187999a0b768STakashi Iwai }
188099a0b768STakashi Iwai 
188199a0b768STakashi Iwai static struct snd_card *snd_opti9xx_card_new(void)
188299a0b768STakashi Iwai {
188399a0b768STakashi Iwai 	struct snd_card *card;
188499a0b768STakashi Iwai 
188599a0b768STakashi Iwai 	card = snd_card_new(index, id, THIS_MODULE, sizeof(struct snd_opti9xx));
188699a0b768STakashi Iwai 	if (! card)
188799a0b768STakashi Iwai 		return NULL;
188899a0b768STakashi Iwai 	card->private_free = snd_card_opti9xx_free;
188999a0b768STakashi Iwai 	return card;
189099a0b768STakashi Iwai }
189199a0b768STakashi Iwai 
18925e24c1c1STakashi Iwai static int __devinit snd_opti9xx_isa_match(struct device *devptr,
18935e24c1c1STakashi Iwai 					   unsigned int dev)
18945e24c1c1STakashi Iwai {
1895101f6f4bSTakashi Iwai #ifdef CONFIG_PNP
18965e24c1c1STakashi Iwai 	if (snd_opti9xx_pnp_is_probed)
18975e24c1c1STakashi Iwai 		return 0;
18985e24c1c1STakashi Iwai 	if (isapnp)
18995e24c1c1STakashi Iwai 		return 0;
1900101f6f4bSTakashi Iwai #endif
19015e24c1c1STakashi Iwai 	return 1;
19025e24c1c1STakashi Iwai }
19035e24c1c1STakashi Iwai 
19045e24c1c1STakashi Iwai static int __devinit snd_opti9xx_isa_probe(struct device *devptr,
19055e24c1c1STakashi Iwai 					   unsigned int dev)
190699a0b768STakashi Iwai {
190799a0b768STakashi Iwai 	struct snd_card *card;
190899a0b768STakashi Iwai 	int error;
190999a0b768STakashi Iwai 	static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
191099a0b768STakashi Iwai #ifdef OPTi93X
191199a0b768STakashi Iwai 	static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
191299a0b768STakashi Iwai #else
191399a0b768STakashi Iwai 	static int possible_irqs[] = {9, 10, 11, 7, -1};
191499a0b768STakashi Iwai #endif	/* OPTi93X */
191599a0b768STakashi Iwai 	static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
191699a0b768STakashi Iwai 	static int possible_dma1s[] = {3, 1, 0, -1};
191799a0b768STakashi Iwai #if defined(CS4231) || defined(OPTi93X)
191899a0b768STakashi Iwai 	static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
191999a0b768STakashi Iwai #endif	/* CS4231 || OPTi93X */
192099a0b768STakashi Iwai 
192199a0b768STakashi Iwai 	if (mpu_port == SNDRV_AUTO_PORT) {
192299a0b768STakashi Iwai 		if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
192399a0b768STakashi Iwai 			snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
192499a0b768STakashi Iwai 			return -EBUSY;
192599a0b768STakashi Iwai 		}
192699a0b768STakashi Iwai 	}
192799a0b768STakashi Iwai 	if (irq == SNDRV_AUTO_IRQ) {
192899a0b768STakashi Iwai 		if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
192999a0b768STakashi Iwai 			snd_printk(KERN_ERR "unable to find a free IRQ\n");
193099a0b768STakashi Iwai 			return -EBUSY;
193199a0b768STakashi Iwai 		}
193299a0b768STakashi Iwai 	}
193399a0b768STakashi Iwai 	if (mpu_irq == SNDRV_AUTO_IRQ) {
193499a0b768STakashi Iwai 		if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
193599a0b768STakashi Iwai 			snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
193699a0b768STakashi Iwai 			return -EBUSY;
193799a0b768STakashi Iwai 		}
193899a0b768STakashi Iwai 	}
193999a0b768STakashi Iwai 	if (dma1 == SNDRV_AUTO_DMA) {
194099a0b768STakashi Iwai 		if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
194199a0b768STakashi Iwai 			snd_printk(KERN_ERR "unable to find a free DMA1\n");
194299a0b768STakashi Iwai 			return -EBUSY;
194399a0b768STakashi Iwai 		}
194499a0b768STakashi Iwai 	}
194599a0b768STakashi Iwai #if defined(CS4231) || defined(OPTi93X)
194699a0b768STakashi Iwai 	if (dma2 == SNDRV_AUTO_DMA) {
194799a0b768STakashi Iwai 		if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
194899a0b768STakashi Iwai 			snd_printk("unable to find a free DMA2\n");
194999a0b768STakashi Iwai 			return -EBUSY;
195099a0b768STakashi Iwai 		}
195199a0b768STakashi Iwai 	}
195299a0b768STakashi Iwai #endif
195399a0b768STakashi Iwai 
195499a0b768STakashi Iwai 	card = snd_opti9xx_card_new();
195599a0b768STakashi Iwai 	if (! card)
195699a0b768STakashi Iwai 		return -ENOMEM;
195799a0b768STakashi Iwai 
195899a0b768STakashi Iwai 	if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
19591da177e4SLinus Torvalds 		snd_card_free(card);
19601da177e4SLinus Torvalds 		return error;
19611da177e4SLinus Torvalds 	}
19625e24c1c1STakashi Iwai 	snd_card_set_dev(card, devptr);
196399a0b768STakashi Iwai 	if ((error = snd_opti9xx_probe(card)) < 0) {
196499a0b768STakashi Iwai 		snd_card_free(card);
196599a0b768STakashi Iwai 		return error;
196699a0b768STakashi Iwai 	}
19675e24c1c1STakashi Iwai 	dev_set_drvdata(devptr, card);
19681da177e4SLinus Torvalds 	return 0;
19691da177e4SLinus Torvalds }
19701da177e4SLinus Torvalds 
19715e24c1c1STakashi Iwai static int __devexit snd_opti9xx_isa_remove(struct device *devptr,
19725e24c1c1STakashi Iwai 					    unsigned int dev)
197399a0b768STakashi Iwai {
19745e24c1c1STakashi Iwai 	snd_card_free(dev_get_drvdata(devptr));
19755e24c1c1STakashi Iwai 	dev_set_drvdata(devptr, NULL);
197699a0b768STakashi Iwai 	return 0;
197799a0b768STakashi Iwai }
197899a0b768STakashi Iwai 
19795e24c1c1STakashi Iwai static struct isa_driver snd_opti9xx_driver = {
19805e24c1c1STakashi Iwai 	.match		= snd_opti9xx_isa_match,
19815e24c1c1STakashi Iwai 	.probe		= snd_opti9xx_isa_probe,
19825e24c1c1STakashi Iwai 	.remove		= __devexit_p(snd_opti9xx_isa_remove),
198399a0b768STakashi Iwai 	/* FIXME: suspend/resume */
198499a0b768STakashi Iwai 	.driver		= {
198583c51c0aSRene Herman 		.name	= DEV_NAME
198699a0b768STakashi Iwai 	},
198799a0b768STakashi Iwai };
198899a0b768STakashi Iwai 
19891da177e4SLinus Torvalds #ifdef CONFIG_PNP
19905e24c1c1STakashi Iwai static int __devinit snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
199199a0b768STakashi Iwai 					   const struct pnp_card_device_id *pid)
199299a0b768STakashi Iwai {
199399a0b768STakashi Iwai 	struct snd_card *card;
199499a0b768STakashi Iwai 	int error, hw;
199599a0b768STakashi Iwai 	struct snd_opti9xx *chip;
199699a0b768STakashi Iwai 
199799a0b768STakashi Iwai 	if (snd_opti9xx_pnp_is_probed)
199899a0b768STakashi Iwai 		return -EBUSY;
199999a0b768STakashi Iwai 	if (! isapnp)
200099a0b768STakashi Iwai 		return -ENODEV;
200199a0b768STakashi Iwai 	card = snd_opti9xx_card_new();
200299a0b768STakashi Iwai 	if (! card)
200399a0b768STakashi Iwai 		return -ENOMEM;
200499a0b768STakashi Iwai 	chip = card->private_data;
200599a0b768STakashi Iwai 
200699a0b768STakashi Iwai 	hw = snd_card_opti9xx_pnp(chip, pcard, pid);
200799a0b768STakashi Iwai 	switch (hw) {
200899a0b768STakashi Iwai 	case 0x0924:
200999a0b768STakashi Iwai 		hw = OPTi9XX_HW_82C924;
201099a0b768STakashi Iwai 		break;
201199a0b768STakashi Iwai 	case 0x0925:
201299a0b768STakashi Iwai 		hw = OPTi9XX_HW_82C925;
201399a0b768STakashi Iwai 		break;
201499a0b768STakashi Iwai 	case 0x0931:
201599a0b768STakashi Iwai 		hw = OPTi9XX_HW_82C931;
201699a0b768STakashi Iwai 		break;
201799a0b768STakashi Iwai 	default:
201899a0b768STakashi Iwai 		snd_card_free(card);
201999a0b768STakashi Iwai 		return -ENODEV;
202099a0b768STakashi Iwai 	}
202199a0b768STakashi Iwai 
202299a0b768STakashi Iwai 	if ((error = snd_opti9xx_init(chip, hw))) {
202399a0b768STakashi Iwai 		snd_card_free(card);
202499a0b768STakashi Iwai 		return error;
202599a0b768STakashi Iwai 	}
202699a0b768STakashi Iwai 	if (hw <= OPTi9XX_HW_82C930)
202799a0b768STakashi Iwai 		chip->mc_base -= 0x80;
202899a0b768STakashi Iwai 	snd_card_set_dev(card, &pcard->card->dev);
202999a0b768STakashi Iwai 	if ((error = snd_opti9xx_probe(card)) < 0) {
203099a0b768STakashi Iwai 		snd_card_free(card);
203199a0b768STakashi Iwai 		return error;
203299a0b768STakashi Iwai 	}
203399a0b768STakashi Iwai 	pnp_set_card_drvdata(pcard, card);
203499a0b768STakashi Iwai 	snd_opti9xx_pnp_is_probed = 1;
203599a0b768STakashi Iwai 	return 0;
203699a0b768STakashi Iwai }
203799a0b768STakashi Iwai 
20381da177e4SLinus Torvalds static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
20391da177e4SLinus Torvalds {
204099a0b768STakashi Iwai 	snd_card_free(pnp_get_card_drvdata(pcard));
204199a0b768STakashi Iwai 	pnp_set_card_drvdata(pcard, NULL);
204299a0b768STakashi Iwai 	snd_opti9xx_pnp_is_probed = 0;
20431da177e4SLinus Torvalds }
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds static struct pnp_card_driver opti9xx_pnpc_driver = {
20461da177e4SLinus Torvalds 	.flags		= PNP_DRIVER_RES_DISABLE,
20471da177e4SLinus Torvalds 	.name		= "opti9xx",
20481da177e4SLinus Torvalds 	.id_table	= snd_opti9xx_pnpids,
204999a0b768STakashi Iwai 	.probe		= snd_opti9xx_pnp_probe,
20501da177e4SLinus Torvalds 	.remove		= __devexit_p(snd_opti9xx_pnp_remove),
20511da177e4SLinus Torvalds };
20521da177e4SLinus Torvalds #endif
20531da177e4SLinus Torvalds 
205499a0b768STakashi Iwai #ifdef OPTi93X
205599a0b768STakashi Iwai #define CHIP_NAME	"82C93x"
205699a0b768STakashi Iwai #else
205799a0b768STakashi Iwai #define CHIP_NAME	"82C92x"
205899a0b768STakashi Iwai #endif
205999a0b768STakashi Iwai 
20601da177e4SLinus Torvalds static int __init alsa_card_opti9xx_init(void)
20611da177e4SLinus Torvalds {
20620bbbc4caSTakashi Iwai #ifdef CONFIG_PNP
206399a0b768STakashi Iwai 	pnp_register_card_driver(&opti9xx_pnpc_driver);
206499a0b768STakashi Iwai 	if (snd_opti9xx_pnp_is_probed)
20651da177e4SLinus Torvalds 		return 0;
2066101f6f4bSTakashi Iwai 	pnp_unregister_card_driver(&opti9xx_pnpc_driver);
20670bbbc4caSTakashi Iwai #endif
20685e24c1c1STakashi Iwai 	return isa_register_driver(&snd_opti9xx_driver, 1);
20691da177e4SLinus Torvalds }
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds static void __exit alsa_card_opti9xx_exit(void)
20721da177e4SLinus Torvalds {
2073f7a9275dSClemens Ladisch 	if (!snd_opti9xx_pnp_is_probed) {
20745e24c1c1STakashi Iwai 		isa_unregister_driver(&snd_opti9xx_driver);
20755e24c1c1STakashi Iwai 		return;
2076f7a9275dSClemens Ladisch 	}
20770bbbc4caSTakashi Iwai #ifdef CONFIG_PNP
20781da177e4SLinus Torvalds 	pnp_unregister_card_driver(&opti9xx_pnpc_driver);
20790bbbc4caSTakashi Iwai #endif
20801da177e4SLinus Torvalds }
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds module_init(alsa_card_opti9xx_init)
20831da177e4SLinus Torvalds module_exit(alsa_card_opti9xx_exit)
2084