xref: /linux/sound/isa/opti9xx/opti92x-ad1848.c (revision b1d5776d865951c213a1caaab5d8bf5de7615dbd)
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 <sound/driver.h>
271da177e4SLinus Torvalds #include <asm/io.h>
281da177e4SLinus Torvalds #include <asm/dma.h>
291da177e4SLinus Torvalds #include <linux/delay.h>
301da177e4SLinus Torvalds #include <linux/init.h>
311da177e4SLinus Torvalds #include <linux/slab.h>
321da177e4SLinus Torvalds #include <linux/pnp.h>
331da177e4SLinus Torvalds #include <linux/moduleparam.h>
341da177e4SLinus Torvalds #include <sound/core.h>
351da177e4SLinus Torvalds #ifdef CS4231
361da177e4SLinus Torvalds #include <sound/cs4231.h>
371da177e4SLinus Torvalds #else
381da177e4SLinus Torvalds #ifndef OPTi93X
391da177e4SLinus Torvalds #include <sound/ad1848.h>
401da177e4SLinus Torvalds #else
411da177e4SLinus Torvalds #include <sound/control.h>
421da177e4SLinus Torvalds #include <sound/pcm.h>
431da177e4SLinus Torvalds #endif	/* OPTi93X */
441da177e4SLinus Torvalds #endif	/* CS4231 */
451da177e4SLinus Torvalds #include <sound/mpu401.h>
461da177e4SLinus Torvalds #include <sound/opl3.h>
471da177e4SLinus Torvalds #ifndef OPTi93X
481da177e4SLinus Torvalds #include <sound/opl4.h>
491da177e4SLinus Torvalds #endif
501da177e4SLinus Torvalds #define SNDRV_LEGACY_FIND_FREE_IRQ
511da177e4SLinus Torvalds #define SNDRV_LEGACY_FIND_FREE_DMA
521da177e4SLinus Torvalds #include <sound/initval.h>
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
551da177e4SLinus Torvalds MODULE_LICENSE("GPL");
561da177e4SLinus Torvalds #ifdef OPTi93X
571da177e4SLinus Torvalds MODULE_DESCRIPTION("OPTi93X");
581da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{OPTi,82C931/3}}");
591da177e4SLinus Torvalds #else	/* OPTi93X */
601da177e4SLinus Torvalds #ifdef CS4231
611da177e4SLinus Torvalds MODULE_DESCRIPTION("OPTi92X - CS4231");
621da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (CS4231)},"
631da177e4SLinus Torvalds 		"{OPTi,82C925 (CS4231)}}");
641da177e4SLinus Torvalds #else	/* CS4231 */
651da177e4SLinus Torvalds MODULE_DESCRIPTION("OPTi92X - AD1848");
661da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (AD1848)},"
671da177e4SLinus Torvalds 		"{OPTi,82C925 (AD1848)},"
681da177e4SLinus Torvalds 	        "{OAK,Mozart}}");
691da177e4SLinus Torvalds #endif	/* CS4231 */
701da177e4SLinus Torvalds #endif	/* OPTi93X */
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds static int index = SNDRV_DEFAULT_IDX1;	/* Index 0-MAX */
731da177e4SLinus Torvalds static char *id = SNDRV_DEFAULT_STR1;		/* ID for this card */
741da177e4SLinus Torvalds //static int enable = SNDRV_DEFAULT_ENABLE1;	/* Enable this card */
751da177e4SLinus Torvalds static int isapnp = 1;			/* Enable ISA PnP detection */
761da177e4SLinus Torvalds static long port = SNDRV_DEFAULT_PORT1; 	/* 0x530,0xe80,0xf40,0x604 */
771da177e4SLinus Torvalds static long mpu_port = SNDRV_DEFAULT_PORT1;	/* 0x300,0x310,0x320,0x330 */
781da177e4SLinus Torvalds static long fm_port = SNDRV_DEFAULT_PORT1;	/* 0x388 */
791da177e4SLinus Torvalds static int irq = SNDRV_DEFAULT_IRQ1;		/* 5,7,9,10,11 */
801da177e4SLinus Torvalds static int mpu_irq = SNDRV_DEFAULT_IRQ1;	/* 5,7,9,10 */
811da177e4SLinus Torvalds static int dma1 = SNDRV_DEFAULT_DMA1;		/* 0,1,3 */
821da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
831da177e4SLinus Torvalds static int dma2 = SNDRV_DEFAULT_DMA1;		/* 0,1,3 */
841da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds module_param(index, int, 0444);
871da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard.");
881da177e4SLinus Torvalds module_param(id, charp, 0444);
891da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard.");
901da177e4SLinus Torvalds //module_param(enable, bool, 0444);
911da177e4SLinus Torvalds //MODULE_PARM_DESC(enable, "Enable opti9xx soundcard.");
921da177e4SLinus Torvalds module_param(isapnp, bool, 0444);
931da177e4SLinus Torvalds MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
941da177e4SLinus Torvalds module_param(port, long, 0444);
951da177e4SLinus Torvalds MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
961da177e4SLinus Torvalds module_param(mpu_port, long, 0444);
971da177e4SLinus Torvalds MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
981da177e4SLinus Torvalds module_param(fm_port, long, 0444);
991da177e4SLinus Torvalds MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
1001da177e4SLinus Torvalds module_param(irq, int, 0444);
1011da177e4SLinus Torvalds MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
1021da177e4SLinus Torvalds module_param(mpu_irq, int, 0444);
1031da177e4SLinus Torvalds MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
1041da177e4SLinus Torvalds module_param(dma1, int, 0444);
1051da177e4SLinus Torvalds MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver.");
1061da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
1071da177e4SLinus Torvalds module_param(dma2, int, 0444);
1081da177e4SLinus Torvalds MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver.");
1091da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds #define OPTi9XX_HW_DETECT	0
1121da177e4SLinus Torvalds #define OPTi9XX_HW_82C928	1
1131da177e4SLinus Torvalds #define OPTi9XX_HW_82C929	2
1141da177e4SLinus Torvalds #define OPTi9XX_HW_82C924	3
1151da177e4SLinus Torvalds #define OPTi9XX_HW_82C925	4
1161da177e4SLinus Torvalds #define OPTi9XX_HW_82C930	5
1171da177e4SLinus Torvalds #define OPTi9XX_HW_82C931	6
1181da177e4SLinus Torvalds #define OPTi9XX_HW_82C933	7
1191da177e4SLinus Torvalds #define OPTi9XX_HW_LAST		OPTi9XX_HW_82C933
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds #define OPTi9XX_MC_REG(n)	n
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds typedef struct _snd_opti9xx opti9xx_t;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds #ifdef OPTi93X
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds #define OPTi93X_INDEX			0x00
1281da177e4SLinus Torvalds #define OPTi93X_DATA			0x01
1291da177e4SLinus Torvalds #define OPTi93X_STATUS			0x02
1301da177e4SLinus Torvalds #define OPTi93X_DDATA			0x03
1311da177e4SLinus Torvalds #define OPTi93X_PORT(chip, r)		((chip)->port + OPTi93X_##r)
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds #define OPTi93X_MIXOUT_LEFT		0x00
1341da177e4SLinus Torvalds #define OPTi93X_MIXOUT_RIGHT		0x01
1351da177e4SLinus Torvalds #define OPTi93X_CD_LEFT_INPUT		0x02
1361da177e4SLinus Torvalds #define OPTi93X_CD_RIGHT_INPUT		0x03
1371da177e4SLinus Torvalds #define OPTi930_AUX_LEFT_INPUT		0x04
1381da177e4SLinus Torvalds #define OPTi930_AUX_RIGHT_INPUT		0x05
1391da177e4SLinus Torvalds #define OPTi931_FM_LEFT_INPUT		0x04
1401da177e4SLinus Torvalds #define OPTi931_FM_RIGHT_INPUT		0x05
1411da177e4SLinus Torvalds #define OPTi93X_DAC_LEFT		0x06
1421da177e4SLinus Torvalds #define OPTi93X_DAC_RIGHT		0x07
1431da177e4SLinus Torvalds #define OPTi93X_PLAY_FORMAT		0x08
1441da177e4SLinus Torvalds #define OPTi93X_IFACE_CONF		0x09
1451da177e4SLinus Torvalds #define OPTi93X_PIN_CTRL		0x0a
1461da177e4SLinus Torvalds #define OPTi93X_ERR_INIT		0x0b
1471da177e4SLinus Torvalds #define OPTi93X_ID			0x0c
1481da177e4SLinus Torvalds #define OPTi93X_PLAY_UPR_CNT		0x0e
1491da177e4SLinus Torvalds #define OPTi93X_PLAY_LWR_CNT		0x0f
1501da177e4SLinus Torvalds #define OPTi931_AUX_LEFT_INPUT		0x10
1511da177e4SLinus Torvalds #define OPTi931_AUX_RIGHT_INPUT		0x11
1521da177e4SLinus Torvalds #define OPTi93X_LINE_LEFT_INPUT		0x12
1531da177e4SLinus Torvalds #define OPTi93X_LINE_RIGHT_INPUT	0x13
1541da177e4SLinus Torvalds #define OPTi93X_MIC_LEFT_INPUT		0x14
1551da177e4SLinus Torvalds #define OPTi93X_MIC_RIGHT_INPUT		0x15
1561da177e4SLinus Torvalds #define OPTi93X_OUT_LEFT		0x16
1571da177e4SLinus Torvalds #define OPTi93X_OUT_RIGHT		0x17
1581da177e4SLinus Torvalds #define OPTi93X_CAPT_FORMAT		0x1c
1591da177e4SLinus Torvalds #define OPTi93X_CAPT_UPR_CNT		0x1e
1601da177e4SLinus Torvalds #define OPTi93X_CAPT_LWR_CNT		0x1f
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds #define OPTi93X_TRD			0x20
1631da177e4SLinus Torvalds #define OPTi93X_MCE			0x40
1641da177e4SLinus Torvalds #define OPTi93X_INIT			0x80
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds #define OPTi93X_MIXOUT_MIC_GAIN		0x20
1671da177e4SLinus Torvalds #define OPTi93X_MIXOUT_LINE		0x00
1681da177e4SLinus Torvalds #define OPTi93X_MIXOUT_CD		0x40
1691da177e4SLinus Torvalds #define OPTi93X_MIXOUT_MIC		0x80
1701da177e4SLinus Torvalds #define OPTi93X_MIXOUT_MIXER		0xc0
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds #define OPTi93X_STEREO			0x10
1731da177e4SLinus Torvalds #define OPTi93X_LINEAR_8		0x00
1741da177e4SLinus Torvalds #define OPTi93X_ULAW_8			0x20
1751da177e4SLinus Torvalds #define OPTi93X_LINEAR_16_LIT		0x40
1761da177e4SLinus Torvalds #define OPTi93X_ALAW_8			0x60
1771da177e4SLinus Torvalds #define OPTi93X_ADPCM_16		0xa0
1781da177e4SLinus Torvalds #define OPTi93X_LINEAR_16_BIG		0xc0
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds #define OPTi93X_CAPTURE_PIO		0x80
1811da177e4SLinus Torvalds #define OPTi93X_PLAYBACK_PIO		0x40
1821da177e4SLinus Torvalds #define OPTi93X_AUTOCALIB		0x08
1831da177e4SLinus Torvalds #define OPTi93X_SINGLE_DMA		0x04
1841da177e4SLinus Torvalds #define OPTi93X_CAPTURE_ENABLE		0x02
1851da177e4SLinus Torvalds #define OPTi93X_PLAYBACK_ENABLE		0x01
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds #define OPTi93X_IRQ_ENABLE		0x02
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds #define OPTi93X_DMA_REQUEST		0x10
1901da177e4SLinus Torvalds #define OPTi93X_CALIB_IN_PROGRESS	0x20
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds #define OPTi93X_IRQ_PLAYBACK		0x04
1931da177e4SLinus Torvalds #define OPTi93X_IRQ_CAPTURE		0x08
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds typedef struct _snd_opti93x opti93x_t;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds struct _snd_opti93x {
1991da177e4SLinus Torvalds 	unsigned long port;
2001da177e4SLinus Torvalds 	struct resource *res_port;
2011da177e4SLinus Torvalds 	int irq;
2021da177e4SLinus Torvalds 	int dma1;
2031da177e4SLinus Torvalds 	int dma2;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	opti9xx_t *chip;
2061da177e4SLinus Torvalds 	unsigned short hardware;
2071da177e4SLinus Torvalds 	unsigned char image[32];
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	unsigned char mce_bit;
2101da177e4SLinus Torvalds 	unsigned short mode;
2111da177e4SLinus Torvalds 	int mute;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	spinlock_t lock;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	snd_card_t *card;
2161da177e4SLinus Torvalds 	snd_pcm_t *pcm;
2171da177e4SLinus Torvalds 	snd_pcm_substream_t *playback_substream;
2181da177e4SLinus Torvalds 	snd_pcm_substream_t *capture_substream;
2191da177e4SLinus Torvalds 	unsigned int p_dma_size;
2201da177e4SLinus Torvalds 	unsigned int c_dma_size;
2211da177e4SLinus Torvalds };
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds #define OPTi93X_MODE_NONE	0x00
2241da177e4SLinus Torvalds #define OPTi93X_MODE_PLAY	0x01
2251da177e4SLinus Torvalds #define OPTi93X_MODE_CAPTURE	0x02
2261da177e4SLinus Torvalds #define OPTi93X_MODE_OPEN	(OPTi93X_MODE_PLAY | OPTi93X_MODE_CAPTURE)
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds #endif /* OPTi93X */
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds struct _snd_opti9xx {
2311da177e4SLinus Torvalds 	unsigned short hardware;
2321da177e4SLinus Torvalds 	unsigned char password;
2331da177e4SLinus Torvalds 	char name[7];
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	unsigned long mc_base;
2361da177e4SLinus Torvalds 	struct resource *res_mc_base;
2371da177e4SLinus Torvalds 	unsigned long mc_base_size;
2381da177e4SLinus Torvalds #ifdef OPTi93X
2391da177e4SLinus Torvalds 	unsigned long mc_indir_index;
2401da177e4SLinus Torvalds #endif	/* OPTi93X */
2411da177e4SLinus Torvalds 	unsigned long pwd_reg;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	spinlock_t lock;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	long wss_base;
2461da177e4SLinus Torvalds 	int irq;
2471da177e4SLinus Torvalds 	int dma1;
2481da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
2491da177e4SLinus Torvalds 	int dma2;
2501da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	long fm_port;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	long mpu_port;
2551da177e4SLinus Torvalds 	int mpu_irq;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds #ifdef CONFIG_PNP
2581da177e4SLinus Torvalds 	struct pnp_dev *dev;
2591da177e4SLinus Torvalds 	struct pnp_dev *devmpu;
2601da177e4SLinus Torvalds #endif	/* CONFIG_PNP */
2611da177e4SLinus Torvalds };
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds static int snd_opti9xx_first_hit = 1;
2641da177e4SLinus Torvalds static snd_card_t *snd_opti9xx_legacy = SNDRV_DEFAULT_PTR1;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds #ifdef CONFIG_PNP
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds static struct pnp_card_device_id snd_opti9xx_pnpids[] = {
2691da177e4SLinus Torvalds #ifndef OPTi93X
2701da177e4SLinus Torvalds 	/* OPTi 82C924 */
2711da177e4SLinus Torvalds 	{ .id = "OPT0924", .devs = { { "OPT0000" }, { "OPT0002" } }, .driver_data = 0x0924 },
2721da177e4SLinus Torvalds 	/* OPTi 82C925 */
2731da177e4SLinus Torvalds 	{ .id = "OPT0925", .devs = { { "OPT9250" }, { "OPT0002" } }, .driver_data = 0x0925 },
2741da177e4SLinus Torvalds #else
2751da177e4SLinus Torvalds 	/* OPTi 82C931/3 */
2761da177e4SLinus Torvalds 	{ .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } }, .driver_data = 0x0931 },
2771da177e4SLinus Torvalds #endif	/* OPTi93X */
2781da177e4SLinus Torvalds 	{ .id = "" }
2791da177e4SLinus Torvalds };
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds #endif	/* CONFIG_PNP */
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds #ifdef OPTi93X
2861da177e4SLinus Torvalds #define DRIVER_NAME	"snd-card-opti93x"
2871da177e4SLinus Torvalds #else
2881da177e4SLinus Torvalds #define DRIVER_NAME	"snd-card-opti92x"
2891da177e4SLinus Torvalds #endif	/* OPTi93X */
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static char * snd_opti9xx_names[] = {
2921da177e4SLinus Torvalds 	"unkown",
2931da177e4SLinus Torvalds 	"82C928",	"82C929",
2941da177e4SLinus Torvalds 	"82C924",	"82C925",
2951da177e4SLinus Torvalds 	"82C930",	"82C931",	"82C933"
2961da177e4SLinus Torvalds };
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds static long snd_legacy_find_free_ioport(long *port_table, long size)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	while (*port_table != -1) {
302*b1d5776dSTakashi Iwai 		if (request_region(*port_table, size, "ALSA test")) {
303*b1d5776dSTakashi Iwai 			release_region(*port_table, size);
3041da177e4SLinus Torvalds 			return *port_table;
3051da177e4SLinus Torvalds 		}
3061da177e4SLinus Torvalds 		port_table++;
3071da177e4SLinus Torvalds 	}
3081da177e4SLinus Torvalds 	return -1;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds static int __devinit snd_opti9xx_init(opti9xx_t *chip, unsigned short hardware)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	chip->hardware = hardware;
3161da177e4SLinus Torvalds 	strcpy(chip->name, snd_opti9xx_names[hardware]);
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	chip->mc_base_size = opti9xx_mc_size[hardware];
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	spin_lock_init(&chip->lock);
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	chip->wss_base = -1;
3231da177e4SLinus Torvalds 	chip->irq = -1;
3241da177e4SLinus Torvalds 	chip->dma1 = -1;
3251da177e4SLinus Torvalds #if defined(CS4231) || defined (OPTi93X)
3261da177e4SLinus Torvalds 	chip->dma2 = -1;
3271da177e4SLinus Torvalds #endif 	/* CS4231 || OPTi93X */
3281da177e4SLinus Torvalds 	chip->fm_port = -1;
3291da177e4SLinus Torvalds 	chip->mpu_port = -1;
3301da177e4SLinus Torvalds 	chip->mpu_irq = -1;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	switch (hardware) {
3331da177e4SLinus Torvalds #ifndef OPTi93X
3341da177e4SLinus Torvalds 	case OPTi9XX_HW_82C928:
3351da177e4SLinus Torvalds 	case OPTi9XX_HW_82C929:
3361da177e4SLinus Torvalds 		chip->mc_base = 0xf8c;
3371da177e4SLinus Torvalds 		chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
3381da177e4SLinus Torvalds 		chip->pwd_reg = 3;
3391da177e4SLinus Torvalds 		break;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	case OPTi9XX_HW_82C924:
3421da177e4SLinus Torvalds 	case OPTi9XX_HW_82C925:
3431da177e4SLinus Torvalds 		chip->mc_base = 0xf8c;
3441da177e4SLinus Torvalds 		chip->password = 0xe5;
3451da177e4SLinus Torvalds 		chip->pwd_reg = 3;
3461da177e4SLinus Torvalds 		break;
3471da177e4SLinus Torvalds #else	/* OPTi93X */
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	case OPTi9XX_HW_82C930:
3501da177e4SLinus Torvalds 	case OPTi9XX_HW_82C931:
3511da177e4SLinus Torvalds 	case OPTi9XX_HW_82C933:
3521da177e4SLinus Torvalds 		chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
3531da177e4SLinus Torvalds 		chip->mc_indir_index = 0xe0e;
3541da177e4SLinus Torvalds 		chip->password = 0xe4;
3551da177e4SLinus Torvalds 		chip->pwd_reg = 0;
3561da177e4SLinus Torvalds 		break;
3571da177e4SLinus Torvalds #endif	/* OPTi93X */
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	default:
3601da177e4SLinus Torvalds 		snd_printk("chip %d not supported\n", hardware);
3611da177e4SLinus Torvalds 		return -ENODEV;
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds 	return 0;
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds static unsigned char snd_opti9xx_read(opti9xx_t *chip,
3671da177e4SLinus Torvalds 				      unsigned char reg)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	unsigned long flags;
3701da177e4SLinus Torvalds 	unsigned char retval = 0xff;
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
3731da177e4SLinus Torvalds 	outb(chip->password, chip->mc_base + chip->pwd_reg);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	switch (chip->hardware) {
3761da177e4SLinus Torvalds #ifndef OPTi93X
3771da177e4SLinus Torvalds 	case OPTi9XX_HW_82C924:
3781da177e4SLinus Torvalds 	case OPTi9XX_HW_82C925:
3791da177e4SLinus Torvalds 		if (reg > 7) {
3801da177e4SLinus Torvalds 			outb(reg, chip->mc_base + 8);
3811da177e4SLinus Torvalds 			outb(chip->password, chip->mc_base + chip->pwd_reg);
3821da177e4SLinus Torvalds 			retval = inb(chip->mc_base + 9);
3831da177e4SLinus Torvalds 			break;
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	case OPTi9XX_HW_82C928:
3871da177e4SLinus Torvalds 	case OPTi9XX_HW_82C929:
3881da177e4SLinus Torvalds 		retval = inb(chip->mc_base + reg);
3891da177e4SLinus Torvalds 		break;
3901da177e4SLinus Torvalds #else	/* OPTi93X */
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	case OPTi9XX_HW_82C930:
3931da177e4SLinus Torvalds 	case OPTi9XX_HW_82C931:
3941da177e4SLinus Torvalds 	case OPTi9XX_HW_82C933:
3951da177e4SLinus Torvalds 		outb(reg, chip->mc_indir_index);
3961da177e4SLinus Torvalds 		outb(chip->password, chip->mc_base + chip->pwd_reg);
3971da177e4SLinus Torvalds 		retval = inb(chip->mc_indir_index + 1);
3981da177e4SLinus Torvalds 		break;
3991da177e4SLinus Torvalds #endif	/* OPTi93X */
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	default:
4021da177e4SLinus Torvalds 		snd_printk("chip %d not supported\n", chip->hardware);
4031da177e4SLinus Torvalds 	}
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
4061da177e4SLinus Torvalds 	return retval;
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds static void snd_opti9xx_write(opti9xx_t *chip, unsigned char reg,
4101da177e4SLinus Torvalds 			      unsigned char value)
4111da177e4SLinus Torvalds {
4121da177e4SLinus Torvalds 	unsigned long flags;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
4151da177e4SLinus Torvalds 	outb(chip->password, chip->mc_base + chip->pwd_reg);
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	switch (chip->hardware) {
4181da177e4SLinus Torvalds #ifndef OPTi93X
4191da177e4SLinus Torvalds 	case OPTi9XX_HW_82C924:
4201da177e4SLinus Torvalds 	case OPTi9XX_HW_82C925:
4211da177e4SLinus Torvalds 		if (reg > 7) {
4221da177e4SLinus Torvalds 			outb(reg, chip->mc_base + 8);
4231da177e4SLinus Torvalds 			outb(chip->password, chip->mc_base + chip->pwd_reg);
4241da177e4SLinus Torvalds 			outb(value, chip->mc_base + 9);
4251da177e4SLinus Torvalds 			break;
4261da177e4SLinus Torvalds 		}
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	case OPTi9XX_HW_82C928:
4291da177e4SLinus Torvalds 	case OPTi9XX_HW_82C929:
4301da177e4SLinus Torvalds 		outb(value, chip->mc_base + reg);
4311da177e4SLinus Torvalds 		break;
4321da177e4SLinus Torvalds #else	/* OPTi93X */
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	case OPTi9XX_HW_82C930:
4351da177e4SLinus Torvalds 	case OPTi9XX_HW_82C931:
4361da177e4SLinus Torvalds 	case OPTi9XX_HW_82C933:
4371da177e4SLinus Torvalds 		outb(reg, chip->mc_indir_index);
4381da177e4SLinus Torvalds 		outb(chip->password, chip->mc_base + chip->pwd_reg);
4391da177e4SLinus Torvalds 		outb(value, chip->mc_indir_index + 1);
4401da177e4SLinus Torvalds 		break;
4411da177e4SLinus Torvalds #endif	/* OPTi93X */
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	default:
4441da177e4SLinus Torvalds 		snd_printk("chip %d not supported\n", chip->hardware);
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds #define snd_opti9xx_write_mask(chip, reg, value, mask)	\
4521da177e4SLinus Torvalds 	snd_opti9xx_write(chip, reg,			\
4531da177e4SLinus Torvalds 		(snd_opti9xx_read(chip, reg) & ~(mask)) | ((value) & (mask)))
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds static int __devinit snd_opti9xx_configure(opti9xx_t *chip)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds 	unsigned char wss_base_bits;
4591da177e4SLinus Torvalds 	unsigned char irq_bits;
4601da177e4SLinus Torvalds 	unsigned char dma_bits;
4611da177e4SLinus Torvalds 	unsigned char mpu_port_bits = 0;
4621da177e4SLinus Torvalds 	unsigned char mpu_irq_bits;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	switch (chip->hardware) {
4651da177e4SLinus Torvalds #ifndef OPTi93X
4661da177e4SLinus Torvalds 	case OPTi9XX_HW_82C924:
4671da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
4681da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	case OPTi9XX_HW_82C925:
4711da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
4721da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
4731da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
4741da177e4SLinus Torvalds #ifdef CS4231
4751da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
4761da177e4SLinus Torvalds #else
4771da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
4781da177e4SLinus Torvalds #endif	/* CS4231 */
4791da177e4SLinus Torvalds 		break;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	case OPTi9XX_HW_82C928:
4821da177e4SLinus Torvalds 	case OPTi9XX_HW_82C929:
4831da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
4841da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
4851da177e4SLinus Torvalds 		/*
4861da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
4871da177e4SLinus Torvalds 		*/
4881da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
4891da177e4SLinus Torvalds #ifdef CS4231
4901da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
4911da177e4SLinus Torvalds #else
4921da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
4931da177e4SLinus Torvalds #endif	/* CS4231 */
4941da177e4SLinus Torvalds 		break;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds #else	/* OPTi93X */
4971da177e4SLinus Torvalds 	case OPTi9XX_HW_82C930:
4981da177e4SLinus Torvalds 	case OPTi9XX_HW_82C931:
4991da177e4SLinus Torvalds 	case OPTi9XX_HW_82C933:
5001da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
5011da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
5021da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
5031da177e4SLinus Torvalds 			(chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
5041da177e4SLinus Torvalds 			0x34);
5051da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
5061da177e4SLinus Torvalds 		break;
5071da177e4SLinus Torvalds #endif	/* OPTi93X */
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	default:
5101da177e4SLinus Torvalds 		snd_printk("chip %d not supported\n", chip->hardware);
5111da177e4SLinus Torvalds 		return -EINVAL;
5121da177e4SLinus Torvalds 	}
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	switch (chip->wss_base) {
5151da177e4SLinus Torvalds 	case 0x530:
5161da177e4SLinus Torvalds 		wss_base_bits = 0x00;
5171da177e4SLinus Torvalds 		break;
5181da177e4SLinus Torvalds 	case 0x604:
5191da177e4SLinus Torvalds 		wss_base_bits = 0x03;
5201da177e4SLinus Torvalds 		break;
5211da177e4SLinus Torvalds 	case 0xe80:
5221da177e4SLinus Torvalds 		wss_base_bits = 0x01;
5231da177e4SLinus Torvalds 		break;
5241da177e4SLinus Torvalds 	case 0xf40:
5251da177e4SLinus Torvalds 		wss_base_bits = 0x02;
5261da177e4SLinus Torvalds 		break;
5271da177e4SLinus Torvalds 	default:
5281da177e4SLinus Torvalds 		snd_printk("WSS port 0x%lx not valid\n", chip->wss_base);
5291da177e4SLinus Torvalds 		goto __skip_base;
5301da177e4SLinus Torvalds 	}
5311da177e4SLinus Torvalds 	snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds __skip_base:
5341da177e4SLinus Torvalds 	switch (chip->irq) {
5351da177e4SLinus Torvalds //#ifdef OPTi93X
5361da177e4SLinus Torvalds 	case 5:
5371da177e4SLinus Torvalds 		irq_bits = 0x05;
5381da177e4SLinus Torvalds 		break;
5391da177e4SLinus Torvalds //#endif	/* OPTi93X */
5401da177e4SLinus Torvalds 	case 7:
5411da177e4SLinus Torvalds 		irq_bits = 0x01;
5421da177e4SLinus Torvalds 		break;
5431da177e4SLinus Torvalds 	case 9:
5441da177e4SLinus Torvalds 		irq_bits = 0x02;
5451da177e4SLinus Torvalds 		break;
5461da177e4SLinus Torvalds 	case 10:
5471da177e4SLinus Torvalds 		irq_bits = 0x03;
5481da177e4SLinus Torvalds 		break;
5491da177e4SLinus Torvalds 	case 11:
5501da177e4SLinus Torvalds 		irq_bits = 0x04;
5511da177e4SLinus Torvalds 		break;
5521da177e4SLinus Torvalds 	default:
5531da177e4SLinus Torvalds 		snd_printk("WSS irq # %d not valid\n", chip->irq);
5541da177e4SLinus Torvalds 		goto __skip_resources;
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	switch (chip->dma1) {
5581da177e4SLinus Torvalds 	case 0:
5591da177e4SLinus Torvalds 		dma_bits = 0x01;
5601da177e4SLinus Torvalds 		break;
5611da177e4SLinus Torvalds 	case 1:
5621da177e4SLinus Torvalds 		dma_bits = 0x02;
5631da177e4SLinus Torvalds 		break;
5641da177e4SLinus Torvalds 	case 3:
5651da177e4SLinus Torvalds 		dma_bits = 0x03;
5661da177e4SLinus Torvalds 		break;
5671da177e4SLinus Torvalds 	default:
5681da177e4SLinus Torvalds 		snd_printk("WSS dma1 # %d not valid\n", chip->dma1);
5691da177e4SLinus Torvalds 		goto __skip_resources;
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
5731da177e4SLinus Torvalds 	if (chip->dma1 == chip->dma2) {
5741da177e4SLinus Torvalds 		snd_printk("don't want to share dmas\n");
5751da177e4SLinus Torvalds 		return -EBUSY;
5761da177e4SLinus Torvalds 	}
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	switch (chip->dma2) {
5791da177e4SLinus Torvalds 	case 0:
5801da177e4SLinus Torvalds 	case 1:
5811da177e4SLinus Torvalds 		break;
5821da177e4SLinus Torvalds 	default:
5831da177e4SLinus Torvalds 		snd_printk("WSS dma2 # %d not valid\n", chip->dma2);
5841da177e4SLinus Torvalds 		goto __skip_resources;
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 	dma_bits |= 0x04;
5871da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds #ifndef OPTi93X
5901da177e4SLinus Torvalds 	 outb(irq_bits << 3 | dma_bits, chip->wss_base);
5911da177e4SLinus Torvalds #else /* OPTi93X */
5921da177e4SLinus Torvalds 	snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
5931da177e4SLinus Torvalds #endif /* OPTi93X */
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds __skip_resources:
5961da177e4SLinus Torvalds 	if (chip->hardware > OPTi9XX_HW_82C928) {
5971da177e4SLinus Torvalds 		switch (chip->mpu_port) {
5981da177e4SLinus Torvalds 		case 0:
5991da177e4SLinus Torvalds 		case -1:
6001da177e4SLinus Torvalds 			break;
6011da177e4SLinus Torvalds 		case 0x300:
6021da177e4SLinus Torvalds 			mpu_port_bits = 0x03;
6031da177e4SLinus Torvalds 			break;
6041da177e4SLinus Torvalds 		case 0x310:
6051da177e4SLinus Torvalds 			mpu_port_bits = 0x02;
6061da177e4SLinus Torvalds 			break;
6071da177e4SLinus Torvalds 		case 0x320:
6081da177e4SLinus Torvalds 			mpu_port_bits = 0x01;
6091da177e4SLinus Torvalds 			break;
6101da177e4SLinus Torvalds 		case 0x330:
6111da177e4SLinus Torvalds 			mpu_port_bits = 0x00;
6121da177e4SLinus Torvalds 			break;
6131da177e4SLinus Torvalds 		default:
6141da177e4SLinus Torvalds 			snd_printk("MPU-401 port 0x%lx not valid\n",
6151da177e4SLinus Torvalds 				chip->mpu_port);
6161da177e4SLinus Torvalds 			goto __skip_mpu;
6171da177e4SLinus Torvalds 		}
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 		switch (chip->mpu_irq) {
6201da177e4SLinus Torvalds 		case 5:
6211da177e4SLinus Torvalds 			mpu_irq_bits = 0x02;
6221da177e4SLinus Torvalds 			break;
6231da177e4SLinus Torvalds 		case 7:
6241da177e4SLinus Torvalds 			mpu_irq_bits = 0x03;
6251da177e4SLinus Torvalds 			break;
6261da177e4SLinus Torvalds 		case 9:
6271da177e4SLinus Torvalds 			mpu_irq_bits = 0x00;
6281da177e4SLinus Torvalds 			break;
6291da177e4SLinus Torvalds 		case 10:
6301da177e4SLinus Torvalds 			mpu_irq_bits = 0x01;
6311da177e4SLinus Torvalds 			break;
6321da177e4SLinus Torvalds 		default:
6331da177e4SLinus Torvalds 			snd_printk("MPU-401 irq # %d not valid\n",
6341da177e4SLinus Torvalds 				chip->mpu_irq);
6351da177e4SLinus Torvalds 			goto __skip_mpu;
6361da177e4SLinus Torvalds 		}
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 		snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
6391da177e4SLinus Torvalds 			(chip->mpu_port <= 0) ? 0x00 :
6401da177e4SLinus Torvalds 				0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
6411da177e4SLinus Torvalds 			0xf8);
6421da177e4SLinus Torvalds 	}
6431da177e4SLinus Torvalds __skip_mpu:
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	return 0;
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds #ifdef OPTi93X
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds static unsigned char snd_opti93x_default_image[32] =
6511da177e4SLinus Torvalds {
6521da177e4SLinus Torvalds 	0x00,		/* 00/00 - l_mixout_outctrl */
6531da177e4SLinus Torvalds 	0x00,		/* 01/01 - r_mixout_outctrl */
6541da177e4SLinus Torvalds 	0x88,		/* 02/02 - l_cd_inctrl */
6551da177e4SLinus Torvalds 	0x88,		/* 03/03 - r_cd_inctrl */
6561da177e4SLinus Torvalds 	0x88,		/* 04/04 - l_a1/fm_inctrl */
6571da177e4SLinus Torvalds 	0x88,		/* 05/05 - r_a1/fm_inctrl */
6581da177e4SLinus Torvalds 	0x80,		/* 06/06 - l_dac_inctrl */
6591da177e4SLinus Torvalds 	0x80,		/* 07/07 - r_dac_inctrl */
6601da177e4SLinus Torvalds 	0x00,		/* 08/08 - ply_dataform_reg */
6611da177e4SLinus Torvalds 	0x00,		/* 09/09 - if_conf */
6621da177e4SLinus Torvalds 	0x00,		/* 0a/10 - pin_ctrl */
6631da177e4SLinus Torvalds 	0x00,		/* 0b/11 - err_init_reg */
6641da177e4SLinus Torvalds 	0x0a,		/* 0c/12 - id_reg */
6651da177e4SLinus Torvalds 	0x00,		/* 0d/13 - reserved */
6661da177e4SLinus Torvalds 	0x00,		/* 0e/14 - ply_upcount_reg */
6671da177e4SLinus Torvalds 	0x00,		/* 0f/15 - ply_lowcount_reg */
6681da177e4SLinus Torvalds 	0x88,		/* 10/16 - reserved/l_a1_inctrl */
6691da177e4SLinus Torvalds 	0x88,		/* 11/17 - reserved/r_a1_inctrl */
6701da177e4SLinus Torvalds 	0x88,		/* 12/18 - l_line_inctrl */
6711da177e4SLinus Torvalds 	0x88,		/* 13/19 - r_line_inctrl */
6721da177e4SLinus Torvalds 	0x88,		/* 14/20 - l_mic_inctrl */
6731da177e4SLinus Torvalds 	0x88,		/* 15/21 - r_mic_inctrl */
6741da177e4SLinus Torvalds 	0x80,		/* 16/22 - l_out_outctrl */
6751da177e4SLinus Torvalds 	0x80,		/* 17/23 - r_out_outctrl */
6761da177e4SLinus Torvalds 	0x00,		/* 18/24 - reserved */
6771da177e4SLinus Torvalds 	0x00,		/* 19/25 - reserved */
6781da177e4SLinus Torvalds 	0x00,		/* 1a/26 - reserved */
6791da177e4SLinus Torvalds 	0x00,		/* 1b/27 - reserved */
6801da177e4SLinus Torvalds 	0x00,		/* 1c/28 - cap_dataform_reg */
6811da177e4SLinus Torvalds 	0x00,		/* 1d/29 - reserved */
6821da177e4SLinus Torvalds 	0x00,		/* 1e/30 - cap_upcount_reg */
6831da177e4SLinus Torvalds 	0x00		/* 1f/31 - cap_lowcount_reg */
6841da177e4SLinus Torvalds };
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds static int snd_opti93x_busy_wait(opti93x_t *chip)
6881da177e4SLinus Torvalds {
6891da177e4SLinus Torvalds 	int timeout;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	for (timeout = 250; timeout-- > 0; udelay(10))
6921da177e4SLinus Torvalds 		if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_INIT))
6931da177e4SLinus Torvalds 			return 0;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	snd_printk("chip still busy.\n");
6961da177e4SLinus Torvalds 	return -EBUSY;
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds static unsigned char snd_opti93x_in(opti93x_t *chip, unsigned char reg)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	snd_opti93x_busy_wait(chip);
7021da177e4SLinus Torvalds 	outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
7031da177e4SLinus Torvalds 	return inb(OPTi93X_PORT(chip, DATA));
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds static void snd_opti93x_out(opti93x_t *chip, unsigned char reg,
7071da177e4SLinus Torvalds 			    unsigned char value)
7081da177e4SLinus Torvalds {
7091da177e4SLinus Torvalds 	snd_opti93x_busy_wait(chip);
7101da177e4SLinus Torvalds 	outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
7111da177e4SLinus Torvalds 	outb(value, OPTi93X_PORT(chip, DATA));
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds static void snd_opti93x_out_image(opti93x_t *chip, unsigned char reg,
7151da177e4SLinus Torvalds 				  unsigned char value)
7161da177e4SLinus Torvalds {
7171da177e4SLinus Torvalds 	snd_opti93x_out(chip, reg, chip->image[reg] = value);
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds static void snd_opti93x_out_mask(opti93x_t *chip, unsigned char reg,
7211da177e4SLinus Torvalds 				 unsigned char mask, unsigned char value)
7221da177e4SLinus Torvalds {
7231da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, reg,
7241da177e4SLinus Torvalds 		(chip->image[reg] & ~mask) | (value & mask));
7251da177e4SLinus Torvalds }
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds static void snd_opti93x_mce_up(opti93x_t *chip)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	snd_opti93x_busy_wait(chip);
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	chip->mce_bit = OPTi93X_MCE;
7331da177e4SLinus Torvalds 	if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE))
7341da177e4SLinus Torvalds 		outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds static void snd_opti93x_mce_down(opti93x_t *chip)
7381da177e4SLinus Torvalds {
7391da177e4SLinus Torvalds 	snd_opti93x_busy_wait(chip);
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	chip->mce_bit = 0;
7421da177e4SLinus Torvalds 	if (inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE)
7431da177e4SLinus Torvalds 		outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds #define snd_opti93x_mute_reg(chip, reg, mute)	\
7471da177e4SLinus Torvalds 	snd_opti93x_out(chip, reg, mute ? 0x80 : chip->image[reg]);
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds static void snd_opti93x_mute(opti93x_t *chip, int mute)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	mute = mute ? 1 : 0;
7521da177e4SLinus Torvalds 	if (chip->mute == mute)
7531da177e4SLinus Torvalds 		return;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	chip->mute = mute;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_CD_LEFT_INPUT, mute);
7581da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_CD_RIGHT_INPUT, mute);
7591da177e4SLinus Torvalds 	switch (chip->hardware) {
7601da177e4SLinus Torvalds 	case OPTi9XX_HW_82C930:
7611da177e4SLinus Torvalds 		snd_opti93x_mute_reg(chip, OPTi930_AUX_LEFT_INPUT, mute);
7621da177e4SLinus Torvalds 		snd_opti93x_mute_reg(chip, OPTi930_AUX_RIGHT_INPUT, mute);
7631da177e4SLinus Torvalds 		break;
7641da177e4SLinus Torvalds 	case OPTi9XX_HW_82C931:
7651da177e4SLinus Torvalds 	case OPTi9XX_HW_82C933:
7661da177e4SLinus Torvalds 		snd_opti93x_mute_reg(chip, OPTi931_FM_LEFT_INPUT, mute);
7671da177e4SLinus Torvalds 		snd_opti93x_mute_reg(chip, OPTi931_FM_RIGHT_INPUT, mute);
7681da177e4SLinus Torvalds 		snd_opti93x_mute_reg(chip, OPTi931_AUX_LEFT_INPUT, mute);
7691da177e4SLinus Torvalds 		snd_opti93x_mute_reg(chip, OPTi931_AUX_RIGHT_INPUT, mute);
7701da177e4SLinus Torvalds 	}
7711da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_DAC_LEFT, mute);
7721da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_DAC_RIGHT, mute);
7731da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_LINE_LEFT_INPUT, mute);
7741da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_LINE_RIGHT_INPUT, mute);
7751da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_MIC_LEFT_INPUT, mute);
7761da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_MIC_RIGHT_INPUT, mute);
7771da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_OUT_LEFT, mute);
7781da177e4SLinus Torvalds 	snd_opti93x_mute_reg(chip, OPTi93X_OUT_RIGHT, mute);
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds static unsigned int snd_opti93x_get_count(unsigned char format,
7831da177e4SLinus Torvalds 					  unsigned int size)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	switch (format & 0xe0) {
7861da177e4SLinus Torvalds 	case OPTi93X_LINEAR_16_LIT:
7871da177e4SLinus Torvalds 	case OPTi93X_LINEAR_16_BIG:
7881da177e4SLinus Torvalds 		size >>= 1;
7891da177e4SLinus Torvalds 		break;
7901da177e4SLinus Torvalds 	case OPTi93X_ADPCM_16:
7911da177e4SLinus Torvalds 		return size >> 2;
7921da177e4SLinus Torvalds 	}
7931da177e4SLinus Torvalds 	return (format & OPTi93X_STEREO) ? (size >> 1) : size;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds static unsigned int rates[] = {  5512,  6615,  8000,  9600, 11025, 16000,
7971da177e4SLinus Torvalds 				18900, 22050, 27428, 32000, 33075, 37800,
7981da177e4SLinus Torvalds 				44100, 48000 };
7991da177e4SLinus Torvalds #define RATES ARRAY_SIZE(rates)
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
8021da177e4SLinus Torvalds 	.count = RATES,
8031da177e4SLinus Torvalds 	.list = rates,
8041da177e4SLinus Torvalds 	.mask = 0,
8051da177e4SLinus Torvalds };
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds static unsigned char bits[] = {  0x01,  0x0f,  0x00,  0x0e,  0x03,  0x02,
8081da177e4SLinus Torvalds 				 0x05,  0x07,  0x04,  0x06,  0x0d,  0x09,
8091da177e4SLinus Torvalds 				 0x0b,  0x0c};
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds static unsigned char snd_opti93x_get_freq(unsigned int rate)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	unsigned int i;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	for (i = 0; i < RATES; i++) {
8161da177e4SLinus Torvalds 		if (rate == rates[i])
8171da177e4SLinus Torvalds 			return bits[i];
8181da177e4SLinus Torvalds 	}
8191da177e4SLinus Torvalds 	snd_BUG();
8201da177e4SLinus Torvalds 	return bits[RATES-1];
8211da177e4SLinus Torvalds }
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds static unsigned char snd_opti93x_get_format(opti93x_t *chip,
8241da177e4SLinus Torvalds 					    unsigned int format, int channels)
8251da177e4SLinus Torvalds {
8261da177e4SLinus Torvalds 	unsigned char retval = OPTi93X_LINEAR_8;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	switch (format) {
8291da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_MU_LAW:
8301da177e4SLinus Torvalds 		retval = OPTi93X_ULAW_8;
8311da177e4SLinus Torvalds 		break;
8321da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_A_LAW:
8331da177e4SLinus Torvalds 		retval = OPTi93X_ALAW_8;
8341da177e4SLinus Torvalds 		break;
8351da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S16_LE:
8361da177e4SLinus Torvalds 		retval = OPTi93X_LINEAR_16_LIT;
8371da177e4SLinus Torvalds 		break;
8381da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_S16_BE:
8391da177e4SLinus Torvalds 		retval = OPTi93X_LINEAR_16_BIG;
8401da177e4SLinus Torvalds 		break;
8411da177e4SLinus Torvalds 	case SNDRV_PCM_FORMAT_IMA_ADPCM:
8421da177e4SLinus Torvalds 		retval = OPTi93X_ADPCM_16;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 	return (channels > 1) ? (retval | OPTi93X_STEREO) : retval;
8451da177e4SLinus Torvalds }
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds static void snd_opti93x_playback_format(opti93x_t *chip, unsigned char fmt)
8491da177e4SLinus Torvalds {
8501da177e4SLinus Torvalds 	unsigned char mask;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	snd_opti93x_mute(chip, 1);
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	snd_opti93x_mce_up(chip);
8551da177e4SLinus Torvalds 	mask = (chip->mode & OPTi93X_MODE_CAPTURE) ? 0xf0 : 0xff;
8561da177e4SLinus Torvalds 	snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, mask, fmt);
8571da177e4SLinus Torvalds 	snd_opti93x_mce_down(chip);
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	snd_opti93x_mute(chip, 0);
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds static void snd_opti93x_capture_format(opti93x_t *chip, unsigned char fmt)
8631da177e4SLinus Torvalds {
8641da177e4SLinus Torvalds 	snd_opti93x_mute(chip, 1);
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	snd_opti93x_mce_up(chip);
8671da177e4SLinus Torvalds 	if (!(chip->mode & OPTi93X_MODE_PLAY))
8681da177e4SLinus Torvalds 		snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, 0x0f, fmt);
8691da177e4SLinus Torvalds 	else
8701da177e4SLinus Torvalds 		fmt = chip->image[OPTi93X_PLAY_FORMAT] & 0xf0;
8711da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_CAPT_FORMAT, fmt);
8721da177e4SLinus Torvalds 	snd_opti93x_mce_down(chip);
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	snd_opti93x_mute(chip, 0);
8751da177e4SLinus Torvalds }
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds static int snd_opti93x_open(opti93x_t *chip, unsigned int mode)
8791da177e4SLinus Torvalds {
8801da177e4SLinus Torvalds 	unsigned long flags;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	if (chip->mode & mode) {
8851da177e4SLinus Torvalds 		spin_unlock_irqrestore(&chip->lock, flags);
8861da177e4SLinus Torvalds 		return -EAGAIN;
8871da177e4SLinus Torvalds 	}
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	if (!(chip->mode & OPTi93X_MODE_OPEN)) {
8901da177e4SLinus Torvalds 		outb(0x00, OPTi93X_PORT(chip, STATUS));
8911da177e4SLinus Torvalds 		snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL,
8921da177e4SLinus Torvalds 			OPTi93X_IRQ_ENABLE, OPTi93X_IRQ_ENABLE);
8931da177e4SLinus Torvalds 		chip->mode = mode;
8941da177e4SLinus Torvalds 	}
8951da177e4SLinus Torvalds 	else
8961da177e4SLinus Torvalds 		chip->mode |= mode;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
8991da177e4SLinus Torvalds 	return 0;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds static void snd_opti93x_close(opti93x_t *chip, unsigned int mode)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	unsigned long flags;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	chip->mode &= ~mode;
9091da177e4SLinus Torvalds 	if (chip->mode & OPTi93X_MODE_OPEN) {
9101da177e4SLinus Torvalds 		spin_unlock_irqrestore(&chip->lock, flags);
9111da177e4SLinus Torvalds 		return;
9121da177e4SLinus Torvalds 	}
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	snd_opti93x_mute(chip, 1);
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	outb(0, OPTi93X_PORT(chip, STATUS));
9171da177e4SLinus Torvalds 	snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL, OPTi93X_IRQ_ENABLE,
9181da177e4SLinus Torvalds 		~OPTi93X_IRQ_ENABLE);
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	snd_opti93x_mce_up(chip);
9211da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_IFACE_CONF, 0x00);
9221da177e4SLinus Torvalds 	snd_opti93x_mce_down(chip);
9231da177e4SLinus Torvalds 	chip->mode = 0;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	snd_opti93x_mute(chip, 0);
9261da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds static int snd_opti93x_trigger(snd_pcm_substream_t *substream,
9301da177e4SLinus Torvalds 			       unsigned char what, int cmd)
9311da177e4SLinus Torvalds {
9321da177e4SLinus Torvalds 	opti93x_t *chip = snd_pcm_substream_chip(substream);
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	switch (cmd) {
9351da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
9361da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
9371da177e4SLinus Torvalds 	{
9381da177e4SLinus Torvalds 		unsigned int what = 0;
9391da177e4SLinus Torvalds 		struct list_head *pos;
9401da177e4SLinus Torvalds 		snd_pcm_substream_t *s;
9411da177e4SLinus Torvalds 		snd_pcm_group_for_each(pos, substream) {
9421da177e4SLinus Torvalds 			s = snd_pcm_group_substream_entry(pos);
9431da177e4SLinus Torvalds 			if (s == chip->playback_substream) {
9441da177e4SLinus Torvalds 				what |= OPTi93X_PLAYBACK_ENABLE;
9451da177e4SLinus Torvalds 				snd_pcm_trigger_done(s, substream);
9461da177e4SLinus Torvalds 			} else if (s == chip->capture_substream) {
9471da177e4SLinus Torvalds 				what |= OPTi93X_CAPTURE_ENABLE;
9481da177e4SLinus Torvalds 				snd_pcm_trigger_done(s, substream);
9491da177e4SLinus Torvalds 			}
9501da177e4SLinus Torvalds 		}
9511da177e4SLinus Torvalds 		spin_lock(&chip->lock);
9521da177e4SLinus Torvalds 		if (cmd == SNDRV_PCM_TRIGGER_START) {
9531da177e4SLinus Torvalds 			snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, what);
9541da177e4SLinus Torvalds 			if (what & OPTi93X_CAPTURE_ENABLE)
9551da177e4SLinus Torvalds 				udelay(50);
9561da177e4SLinus Torvalds 		} else
9571da177e4SLinus Torvalds 			snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, 0x00);
9581da177e4SLinus Torvalds 		spin_unlock(&chip->lock);
9591da177e4SLinus Torvalds 		break;
9601da177e4SLinus Torvalds 	}
9611da177e4SLinus Torvalds 	default:
9621da177e4SLinus Torvalds 		return -EINVAL;
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 	return 0;
9651da177e4SLinus Torvalds }
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds static int snd_opti93x_playback_trigger(snd_pcm_substream_t *substream, int cmd)
9681da177e4SLinus Torvalds {
9691da177e4SLinus Torvalds 	return snd_opti93x_trigger(substream,
9701da177e4SLinus Torvalds 				   OPTi93X_PLAYBACK_ENABLE, cmd);
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds static int snd_opti93x_capture_trigger(snd_pcm_substream_t * substream, int cmd)
9741da177e4SLinus Torvalds {
9751da177e4SLinus Torvalds 	return snd_opti93x_trigger(substream,
9761da177e4SLinus Torvalds 				   OPTi93X_CAPTURE_ENABLE, cmd);
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds static int snd_opti93x_hw_params(snd_pcm_substream_t * substream,
9801da177e4SLinus Torvalds 				 snd_pcm_hw_params_t * hw_params)
9811da177e4SLinus Torvalds {
9821da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
9831da177e4SLinus Torvalds }
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds static int snd_opti93x_hw_free(snd_pcm_substream_t * substream)
9871da177e4SLinus Torvalds {
9881da177e4SLinus Torvalds 	snd_pcm_lib_free_pages(substream);
9891da177e4SLinus Torvalds 	return 0;
9901da177e4SLinus Torvalds }
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds static int snd_opti93x_playback_prepare(snd_pcm_substream_t * substream)
9941da177e4SLinus Torvalds {
9951da177e4SLinus Torvalds 	opti93x_t *chip = snd_pcm_substream_chip(substream);
9961da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
9971da177e4SLinus Torvalds 	unsigned long flags;
9981da177e4SLinus Torvalds 	unsigned char format;
9991da177e4SLinus Torvalds 	unsigned int count = snd_pcm_lib_period_bytes(substream);
10001da177e4SLinus Torvalds 	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	chip->p_dma_size = size;
10051da177e4SLinus Torvalds 	snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
10061da177e4SLinus Torvalds 		OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO,
10071da177e4SLinus Torvalds 		~(OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO));
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	snd_dma_program(chip->dma1, runtime->dma_addr, size,
10101da177e4SLinus Torvalds 		DMA_MODE_WRITE | DMA_AUTOINIT);
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	format = snd_opti93x_get_freq(runtime->rate);
10131da177e4SLinus Torvalds 	format |= snd_opti93x_get_format(chip, runtime->format,
10141da177e4SLinus Torvalds 		runtime->channels);
10151da177e4SLinus Torvalds 	snd_opti93x_playback_format(chip, format);
10161da177e4SLinus Torvalds 	format = chip->image[OPTi93X_PLAY_FORMAT];
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	count = snd_opti93x_get_count(format, count) - 1;
10191da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_PLAY_LWR_CNT, count);
10201da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_PLAY_UPR_CNT, count >> 8);
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
10231da177e4SLinus Torvalds 	return 0;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds static int snd_opti93x_capture_prepare(snd_pcm_substream_t *substream)
10271da177e4SLinus Torvalds {
10281da177e4SLinus Torvalds 	opti93x_t *chip = snd_pcm_substream_chip(substream);
10291da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
10301da177e4SLinus Torvalds 	unsigned long flags;
10311da177e4SLinus Torvalds 	unsigned char format;
10321da177e4SLinus Torvalds 	unsigned int count = snd_pcm_lib_period_bytes(substream);
10331da177e4SLinus Torvalds 	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	chip->c_dma_size = size;
10381da177e4SLinus Torvalds 	snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
1039db67319aSClemens Ladisch 		OPTi93X_CAPTURE_ENABLE | OPTi93X_CAPTURE_PIO, 0);
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	snd_dma_program(chip->dma2, runtime->dma_addr, size,
10421da177e4SLinus Torvalds 		DMA_MODE_READ | DMA_AUTOINIT);
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	format = snd_opti93x_get_freq(runtime->rate);
10451da177e4SLinus Torvalds 	format |= snd_opti93x_get_format(chip, runtime->format,
10461da177e4SLinus Torvalds 		runtime->channels);
10471da177e4SLinus Torvalds 	snd_opti93x_capture_format(chip, format);
10481da177e4SLinus Torvalds 	format = chip->image[OPTi93X_CAPT_FORMAT];
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	count = snd_opti93x_get_count(format, count) - 1;
10511da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_CAPT_LWR_CNT, count);
10521da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_CAPT_UPR_CNT, count >> 8);
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
10551da177e4SLinus Torvalds 	return 0;
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds static snd_pcm_uframes_t snd_opti93x_playback_pointer(snd_pcm_substream_t *substream)
10591da177e4SLinus Torvalds {
10601da177e4SLinus Torvalds 	opti93x_t *chip = snd_pcm_substream_chip(substream);
10611da177e4SLinus Torvalds 	size_t ptr;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_PLAYBACK_ENABLE))
10641da177e4SLinus Torvalds 		return 0;
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
10671da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
10681da177e4SLinus Torvalds }
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds static snd_pcm_uframes_t snd_opti93x_capture_pointer(snd_pcm_substream_t *substream)
10711da177e4SLinus Torvalds {
10721da177e4SLinus Torvalds 	opti93x_t *chip = snd_pcm_substream_chip(substream);
10731da177e4SLinus Torvalds 	size_t ptr;
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_CAPTURE_ENABLE))
10761da177e4SLinus Torvalds 		return 0;
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
10791da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
10801da177e4SLinus Torvalds }
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds static void snd_opti93x_overrange(opti93x_t *chip)
10841da177e4SLinus Torvalds {
10851da177e4SLinus Torvalds 	unsigned long flags;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	if (snd_opti93x_in(chip, OPTi93X_ERR_INIT) & (0x08 | 0x02))
10901da177e4SLinus Torvalds 		chip->capture_substream->runtime->overrange++;
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
10931da177e4SLinus Torvalds }
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id, struct pt_regs *regs)
10961da177e4SLinus Torvalds {
10971da177e4SLinus Torvalds 	opti93x_t *codec = dev_id;
10981da177e4SLinus Torvalds 	unsigned char status;
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	status = snd_opti9xx_read(codec->chip, OPTi9XX_MC_REG(11));
11011da177e4SLinus Torvalds 	if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
11021da177e4SLinus Torvalds 		snd_pcm_period_elapsed(codec->playback_substream);
11031da177e4SLinus Torvalds 	if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
11041da177e4SLinus Torvalds 		snd_opti93x_overrange(codec);
11051da177e4SLinus Torvalds 		snd_pcm_period_elapsed(codec->capture_substream);
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds 	outb(0x00, OPTi93X_PORT(codec, STATUS));
11081da177e4SLinus Torvalds 	return IRQ_HANDLED;
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds static snd_pcm_hardware_t snd_opti93x_playback = {
11131da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
11141da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
11151da177e4SLinus Torvalds 	.formats =		(SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
11161da177e4SLinus Torvalds 				 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
11171da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
11181da177e4SLinus Torvalds 	.rate_min =		5512,
11191da177e4SLinus Torvalds 	.rate_max =		48000,
11201da177e4SLinus Torvalds 	.channels_min =		1,
11211da177e4SLinus Torvalds 	.channels_max =		2,
11221da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
11231da177e4SLinus Torvalds 	.period_bytes_min =	64,
11241da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
11251da177e4SLinus Torvalds 	.periods_min =		1,
11261da177e4SLinus Torvalds 	.periods_max =		1024,
11271da177e4SLinus Torvalds 	.fifo_size =		0,
11281da177e4SLinus Torvalds };
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds static snd_pcm_hardware_t snd_opti93x_capture = {
11311da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
11321da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
11331da177e4SLinus Torvalds 	.formats =		(SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
11341da177e4SLinus Torvalds 				 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
11351da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_8000_48000,
11361da177e4SLinus Torvalds 	.rate_min =		5512,
11371da177e4SLinus Torvalds 	.rate_max =		48000,
11381da177e4SLinus Torvalds 	.channels_min =		1,
11391da177e4SLinus Torvalds 	.channels_max =		2,
11401da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
11411da177e4SLinus Torvalds 	.period_bytes_min =	64,
11421da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
11431da177e4SLinus Torvalds 	.periods_min =		1,
11441da177e4SLinus Torvalds 	.periods_max =		1024,
11451da177e4SLinus Torvalds 	.fifo_size =		0,
11461da177e4SLinus Torvalds };
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds static int snd_opti93x_playback_open(snd_pcm_substream_t *substream)
11491da177e4SLinus Torvalds {
11501da177e4SLinus Torvalds 	int error;
11511da177e4SLinus Torvalds 	opti93x_t *chip = snd_pcm_substream_chip(substream);
11521da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	if ((error = snd_opti93x_open(chip, OPTi93X_MODE_PLAY)) < 0)
11551da177e4SLinus Torvalds 		return error;
11561da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
11571da177e4SLinus Torvalds 	chip->playback_substream = substream;
11581da177e4SLinus Torvalds 	runtime->hw = snd_opti93x_playback;
11591da177e4SLinus Torvalds 	snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
11601da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
11611da177e4SLinus Torvalds 	return error;
11621da177e4SLinus Torvalds }
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds static int snd_opti93x_capture_open(snd_pcm_substream_t *substream)
11651da177e4SLinus Torvalds {
11661da177e4SLinus Torvalds 	int error;
11671da177e4SLinus Torvalds 	opti93x_t *chip = snd_pcm_substream_chip(substream);
11681da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	if ((error = snd_opti93x_open(chip, OPTi93X_MODE_CAPTURE)) < 0)
11711da177e4SLinus Torvalds 		return error;
11721da177e4SLinus Torvalds 	runtime->hw = snd_opti93x_capture;
11731da177e4SLinus Torvalds 	snd_pcm_set_sync(substream);
11741da177e4SLinus Torvalds 	chip->capture_substream = substream;
11751da177e4SLinus Torvalds 	snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
11761da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
11771da177e4SLinus Torvalds 	return error;
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds static int snd_opti93x_playback_close(snd_pcm_substream_t *substream)
11811da177e4SLinus Torvalds {
11821da177e4SLinus Torvalds 	opti93x_t *chip = snd_pcm_substream_chip(substream);
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	chip->playback_substream = NULL;
11851da177e4SLinus Torvalds 	snd_opti93x_close(chip, OPTi93X_MODE_PLAY);
11861da177e4SLinus Torvalds 	return 0;
11871da177e4SLinus Torvalds }
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds static int snd_opti93x_capture_close(snd_pcm_substream_t *substream)
11901da177e4SLinus Torvalds {
11911da177e4SLinus Torvalds 	opti93x_t *chip = snd_pcm_substream_chip(substream);
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	chip->capture_substream = NULL;
11941da177e4SLinus Torvalds 	snd_opti93x_close(chip, OPTi93X_MODE_CAPTURE);
11951da177e4SLinus Torvalds 	return 0;
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds static void snd_opti93x_init(opti93x_t *chip)
12001da177e4SLinus Torvalds {
12011da177e4SLinus Torvalds 	unsigned long flags;
12021da177e4SLinus Torvalds 	int i;
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
12051da177e4SLinus Torvalds 	snd_opti93x_mce_up(chip);
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	for (i = 0; i < 32; i++)
12081da177e4SLinus Torvalds 		snd_opti93x_out_image(chip, i, snd_opti93x_default_image[i]);
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	snd_opti93x_mce_down(chip);
12111da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
12121da177e4SLinus Torvalds }
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds static int snd_opti93x_probe(opti93x_t *chip)
12151da177e4SLinus Torvalds {
12161da177e4SLinus Torvalds 	unsigned long flags;
12171da177e4SLinus Torvalds 	unsigned char val;
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
12201da177e4SLinus Torvalds 	val = snd_opti93x_in(chip, OPTi93X_ID) & 0x0f;
12211da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 	return (val == 0x0a) ? 0 : -ENODEV;
12241da177e4SLinus Torvalds }
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds static int snd_opti93x_free(opti93x_t *chip)
12271da177e4SLinus Torvalds {
1228*b1d5776dSTakashi Iwai 	release_and_free_resource(chip->res_port);
12291da177e4SLinus Torvalds 	if (chip->dma1 >= 0) {
12301da177e4SLinus Torvalds 		disable_dma(chip->dma1);
12311da177e4SLinus Torvalds 		free_dma(chip->dma1);
12321da177e4SLinus Torvalds 	}
12331da177e4SLinus Torvalds 	if (chip->dma2 >= 0) {
12341da177e4SLinus Torvalds 		disable_dma(chip->dma2);
12351da177e4SLinus Torvalds 		free_dma(chip->dma2);
12361da177e4SLinus Torvalds 	}
12371da177e4SLinus Torvalds 	if (chip->irq >= 0) {
12381da177e4SLinus Torvalds 	  free_irq(chip->irq, chip);
12391da177e4SLinus Torvalds 	}
12401da177e4SLinus Torvalds 	kfree(chip);
12411da177e4SLinus Torvalds 	return 0;
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds static int snd_opti93x_dev_free(snd_device_t *device)
12451da177e4SLinus Torvalds {
12461da177e4SLinus Torvalds 	opti93x_t *chip = device->device_data;
12471da177e4SLinus Torvalds 	return snd_opti93x_free(chip);
12481da177e4SLinus Torvalds }
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds static const char *snd_opti93x_chip_id(opti93x_t *codec)
12511da177e4SLinus Torvalds {
12521da177e4SLinus Torvalds 	switch (codec->hardware) {
12531da177e4SLinus Torvalds 	case OPTi9XX_HW_82C930: return "82C930";
12541da177e4SLinus Torvalds 	case OPTi9XX_HW_82C931: return "82C931";
12551da177e4SLinus Torvalds 	case OPTi9XX_HW_82C933: return "82C933";
12561da177e4SLinus Torvalds 	default:		return "???";
12571da177e4SLinus Torvalds 	}
12581da177e4SLinus Torvalds }
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds static int snd_opti93x_create(snd_card_t *card, opti9xx_t *chip,
12611da177e4SLinus Torvalds 			      int dma1, int dma2,
12621da177e4SLinus Torvalds 			      opti93x_t **rcodec)
12631da177e4SLinus Torvalds {
12641da177e4SLinus Torvalds 	static snd_device_ops_t ops = {
12651da177e4SLinus Torvalds 		.dev_free =	snd_opti93x_dev_free,
12661da177e4SLinus Torvalds 	};
12671da177e4SLinus Torvalds 	int error;
12681da177e4SLinus Torvalds 	opti93x_t *codec;
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	*rcodec = NULL;
12719e76a76eSTakashi Iwai 	codec = kzalloc(sizeof(*codec), GFP_KERNEL);
12721da177e4SLinus Torvalds 	if (codec == NULL)
12731da177e4SLinus Torvalds 		return -ENOMEM;
12741da177e4SLinus Torvalds 	codec->irq = -1;
12751da177e4SLinus Torvalds 	codec->dma1 = -1;
12761da177e4SLinus Torvalds 	codec->dma2 = -1;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	if ((codec->res_port = request_region(chip->wss_base + 4, 4, "OPTI93x CODEC")) == NULL) {
12791da177e4SLinus Torvalds 		snd_printk(KERN_ERR "opti9xx: can't grab port 0x%lx\n", chip->wss_base + 4);
12801da177e4SLinus Torvalds 		snd_opti93x_free(codec);
12811da177e4SLinus Torvalds 		return -EBUSY;
12821da177e4SLinus Torvalds 	}
12831da177e4SLinus Torvalds 	if (request_dma(dma1, "OPTI93x - 1")) {
12841da177e4SLinus Torvalds 		snd_printk(KERN_ERR "opti9xx: can't grab DMA1 %d\n", dma1);
12851da177e4SLinus Torvalds 		snd_opti93x_free(codec);
12861da177e4SLinus Torvalds 		return -EBUSY;
12871da177e4SLinus Torvalds 	}
12881da177e4SLinus Torvalds 	codec->dma1 = chip->dma1;
12891da177e4SLinus Torvalds 	if (request_dma(dma2, "OPTI93x - 2")) {
12901da177e4SLinus Torvalds 		snd_printk(KERN_ERR "opti9xx: can't grab DMA2 %d\n", dma2);
12911da177e4SLinus Torvalds 		snd_opti93x_free(codec);
12921da177e4SLinus Torvalds 		return -EBUSY;
12931da177e4SLinus Torvalds 	}
12941da177e4SLinus Torvalds 	codec->dma2 = chip->dma2;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	if (request_irq(chip->irq, snd_opti93x_interrupt, SA_INTERRUPT, DRIVER_NAME" - WSS", codec)) {
12971da177e4SLinus Torvalds 		snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", chip->irq);
12981da177e4SLinus Torvalds 		snd_opti93x_free(codec);
12991da177e4SLinus Torvalds 		return -EBUSY;
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	codec->card = card;
13031da177e4SLinus Torvalds 	codec->port = chip->wss_base + 4;
13041da177e4SLinus Torvalds 	codec->irq = chip->irq;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	spin_lock_init(&codec->lock);
13071da177e4SLinus Torvalds 	codec->hardware = chip->hardware;
13081da177e4SLinus Torvalds 	codec->chip = chip;
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	if ((error = snd_opti93x_probe(codec))) {
13111da177e4SLinus Torvalds 		snd_opti93x_free(codec);
13121da177e4SLinus Torvalds 		return error;
13131da177e4SLinus Torvalds 	}
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	snd_opti93x_init(codec);
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	/* Register device */
13181da177e4SLinus Torvalds 	if ((error = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
13191da177e4SLinus Torvalds 		snd_opti93x_free(codec);
13201da177e4SLinus Torvalds 		return error;
13211da177e4SLinus Torvalds 	}
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 	*rcodec = codec;
13241da177e4SLinus Torvalds 	return 0;
13251da177e4SLinus Torvalds }
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds static snd_pcm_ops_t snd_opti93x_playback_ops = {
13281da177e4SLinus Torvalds 	.open =		snd_opti93x_playback_open,
13291da177e4SLinus Torvalds 	.close =	snd_opti93x_playback_close,
13301da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
13311da177e4SLinus Torvalds 	.hw_params =	snd_opti93x_hw_params,
13321da177e4SLinus Torvalds 	.hw_free =	snd_opti93x_hw_free,
13331da177e4SLinus Torvalds 	.prepare =	snd_opti93x_playback_prepare,
13341da177e4SLinus Torvalds 	.trigger =	snd_opti93x_playback_trigger,
13351da177e4SLinus Torvalds 	.pointer =	snd_opti93x_playback_pointer,
13361da177e4SLinus Torvalds };
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds static snd_pcm_ops_t snd_opti93x_capture_ops = {
13391da177e4SLinus Torvalds 	.open =		snd_opti93x_capture_open,
13401da177e4SLinus Torvalds 	.close =	snd_opti93x_capture_close,
13411da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
13421da177e4SLinus Torvalds 	.hw_params =	snd_opti93x_hw_params,
13431da177e4SLinus Torvalds 	.hw_free =	snd_opti93x_hw_free,
13441da177e4SLinus Torvalds 	.prepare =	snd_opti93x_capture_prepare,
13451da177e4SLinus Torvalds 	.trigger =	snd_opti93x_capture_trigger,
13461da177e4SLinus Torvalds 	.pointer =	snd_opti93x_capture_pointer,
13471da177e4SLinus Torvalds };
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds static void snd_opti93x_pcm_free(snd_pcm_t *pcm)
13501da177e4SLinus Torvalds {
13511da177e4SLinus Torvalds 	opti93x_t *codec = pcm->private_data;
13521da177e4SLinus Torvalds 	codec->pcm = NULL;
13531da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_free_for_all(pcm);
13541da177e4SLinus Torvalds }
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds static int snd_opti93x_pcm(opti93x_t *codec, int device, snd_pcm_t **rpcm)
13571da177e4SLinus Torvalds {
13581da177e4SLinus Torvalds 	int error;
13591da177e4SLinus Torvalds 	snd_pcm_t *pcm;
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	if ((error = snd_pcm_new(codec->card, "OPTi 82C93X", device, 1, 1, &pcm)))
13621da177e4SLinus Torvalds 		return error;
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_opti93x_playback_ops);
13651da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_opti93x_capture_ops);
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	pcm->private_data = codec;
13681da177e4SLinus Torvalds 	pcm->private_free = snd_opti93x_pcm_free;
13691da177e4SLinus Torvalds 	pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 	strcpy(pcm->name, snd_opti93x_chip_id(codec));
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
13741da177e4SLinus Torvalds 					      snd_dma_isa_data(),
13751da177e4SLinus Torvalds 					      64*1024, codec->dma1 > 3 || codec->dma2 > 3 ? 128*1024 : 64*1024);
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 	codec->pcm = pcm;
13781da177e4SLinus Torvalds 	if (rpcm)
13791da177e4SLinus Torvalds 		*rpcm = pcm;
13801da177e4SLinus Torvalds 	return 0;
13811da177e4SLinus Torvalds }
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds /*
13841da177e4SLinus Torvalds  *  MIXER part
13851da177e4SLinus Torvalds  */
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds static int snd_opti93x_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
13881da177e4SLinus Torvalds {
13891da177e4SLinus Torvalds 	static char *texts[4] = {
13901da177e4SLinus Torvalds 		"Line1", "Aux", "Mic", "Mix"
13911da177e4SLinus Torvalds 	};
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
13941da177e4SLinus Torvalds 	uinfo->count = 2;
13951da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 4;
13961da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item > 3)
13971da177e4SLinus Torvalds 		uinfo->value.enumerated.item = 3;
13981da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
13991da177e4SLinus Torvalds 	return 0;
14001da177e4SLinus Torvalds }
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds static int snd_opti93x_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
14031da177e4SLinus Torvalds {
14041da177e4SLinus Torvalds 	opti93x_t *chip = snd_kcontrol_chip(kcontrol);
14051da177e4SLinus Torvalds 	unsigned long flags;
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
14081da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[0] = (chip->image[OPTi93X_MIXOUT_LEFT] & OPTi93X_MIXOUT_MIXER) >> 6;
14091da177e4SLinus Torvalds 	ucontrol->value.enumerated.item[1] = (chip->image[OPTi93X_MIXOUT_RIGHT] & OPTi93X_MIXOUT_MIXER) >> 6;
14101da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
14111da177e4SLinus Torvalds 	return 0;
14121da177e4SLinus Torvalds }
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds static int snd_opti93x_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
14151da177e4SLinus Torvalds {
14161da177e4SLinus Torvalds 	opti93x_t *chip = snd_kcontrol_chip(kcontrol);
14171da177e4SLinus Torvalds 	unsigned long flags;
14181da177e4SLinus Torvalds 	unsigned short left, right;
14191da177e4SLinus Torvalds 	int change;
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	if (ucontrol->value.enumerated.item[0] > 3 ||
14221da177e4SLinus Torvalds 	    ucontrol->value.enumerated.item[1] > 3)
14231da177e4SLinus Torvalds 		return -EINVAL;
14241da177e4SLinus Torvalds 	left = ucontrol->value.enumerated.item[0] << 6;
14251da177e4SLinus Torvalds 	right = ucontrol->value.enumerated.item[1] << 6;
14261da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
14271da177e4SLinus Torvalds 	left = (chip->image[OPTi93X_MIXOUT_LEFT] & ~OPTi93X_MIXOUT_MIXER) | left;
14281da177e4SLinus Torvalds 	right = (chip->image[OPTi93X_MIXOUT_RIGHT] & ~OPTi93X_MIXOUT_MIXER) | right;
14291da177e4SLinus Torvalds 	change = left != chip->image[OPTi93X_MIXOUT_LEFT] ||
14301da177e4SLinus Torvalds 	         right != chip->image[OPTi93X_MIXOUT_RIGHT];
14311da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_MIXOUT_LEFT, left);
14321da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, OPTi93X_MIXOUT_RIGHT, right);
14331da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
14341da177e4SLinus Torvalds 	return change;
14351da177e4SLinus Torvalds }
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds #if 0
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds #define OPTi93X_SINGLE(xname, xindex, reg, shift, mask, invert) \
14401da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
14411da177e4SLinus Torvalds   .info = snd_opti93x_info_single, \
14421da177e4SLinus Torvalds   .get = snd_opti93x_get_single, .put = snd_opti93x_put_single, \
14431da177e4SLinus Torvalds   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds static int snd_opti93x_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
14461da177e4SLinus Torvalds {
14471da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
14501da177e4SLinus Torvalds 	uinfo->count = 1;
14511da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
14521da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
14531da177e4SLinus Torvalds 	return 0;
14541da177e4SLinus Torvalds }
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds static int snd_opti93x_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
14571da177e4SLinus Torvalds {
14581da177e4SLinus Torvalds 	opti93x_t *chip = snd_kcontrol_chip(kcontrol);
14591da177e4SLinus Torvalds 	unsigned long flags;
14601da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
14611da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
14621da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
14631da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
14661da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
14671da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
14681da177e4SLinus Torvalds 	if (invert)
14691da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
14701da177e4SLinus Torvalds 	return 0;
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds static int snd_opti93x_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
14741da177e4SLinus Torvalds {
14751da177e4SLinus Torvalds 	opti93x_t *chip = snd_kcontrol_chip(kcontrol);
14761da177e4SLinus Torvalds 	unsigned long flags;
14771da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
14781da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
14791da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
14801da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
14811da177e4SLinus Torvalds 	int change;
14821da177e4SLinus Torvalds 	unsigned short val;
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
14851da177e4SLinus Torvalds 	if (invert)
14861da177e4SLinus Torvalds 		val = mask - val;
14871da177e4SLinus Torvalds 	val <<= shift;
14881da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
14891da177e4SLinus Torvalds 	val = (chip->image[reg] & ~(mask << shift)) | val;
14901da177e4SLinus Torvalds 	change = val != chip->image[reg];
14911da177e4SLinus Torvalds 	snd_opti93x_out(chip, reg, val);
14921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
14931da177e4SLinus Torvalds 	return change;
14941da177e4SLinus Torvalds }
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds #endif /* single */
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds #define OPTi93X_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
14991da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
15001da177e4SLinus Torvalds   .info = snd_opti93x_info_double, \
15011da177e4SLinus Torvalds   .get = snd_opti93x_get_double, .put = snd_opti93x_put_double, \
15021da177e4SLinus Torvalds   .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds #define OPTi93X_DOUBLE_INVERT_INVERT(xctl) \
15051da177e4SLinus Torvalds 	do { xctl.private_value ^= 22; } while (0)
15061da177e4SLinus Torvalds #define OPTi93X_DOUBLE_CHANGE_REGS(xctl, left_reg, right_reg) \
15071da177e4SLinus Torvalds 	do { xctl.private_value &= ~0x0000ffff; \
15081da177e4SLinus Torvalds 	     xctl.private_value |= left_reg | (right_reg << 8); } while (0)
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds static int snd_opti93x_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
15111da177e4SLinus Torvalds {
15121da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 24) & 0xff;
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
15151da177e4SLinus Torvalds 	uinfo->count = 2;
15161da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
15171da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
15181da177e4SLinus Torvalds 	return 0;
15191da177e4SLinus Torvalds }
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds static int snd_opti93x_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
15221da177e4SLinus Torvalds {
15231da177e4SLinus Torvalds 	opti93x_t *chip = snd_kcontrol_chip(kcontrol);
15241da177e4SLinus Torvalds 	unsigned long flags;
15251da177e4SLinus Torvalds 	int left_reg = kcontrol->private_value & 0xff;
15261da177e4SLinus Torvalds 	int right_reg = (kcontrol->private_value >> 8) & 0xff;
15271da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 16) & 0x07;
15281da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 19) & 0x07;
15291da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 24) & 0xff;
15301da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 22) & 1;
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
15331da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
15341da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
15351da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
15361da177e4SLinus Torvalds 	if (invert) {
15371da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
15381da177e4SLinus Torvalds 		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
15391da177e4SLinus Torvalds 	}
15401da177e4SLinus Torvalds 	return 0;
15411da177e4SLinus Torvalds }
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds static int snd_opti93x_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
15441da177e4SLinus Torvalds {
15451da177e4SLinus Torvalds 	opti93x_t *chip = snd_kcontrol_chip(kcontrol);
15461da177e4SLinus Torvalds 	unsigned long flags;
15471da177e4SLinus Torvalds 	int left_reg = kcontrol->private_value & 0xff;
15481da177e4SLinus Torvalds 	int right_reg = (kcontrol->private_value >> 8) & 0xff;
15491da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 16) & 0x07;
15501da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 19) & 0x07;
15511da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 24) & 0xff;
15521da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 22) & 1;
15531da177e4SLinus Torvalds 	int change;
15541da177e4SLinus Torvalds 	unsigned short val1, val2;
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
15571da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
15581da177e4SLinus Torvalds 	if (invert) {
15591da177e4SLinus Torvalds 		val1 = mask - val1;
15601da177e4SLinus Torvalds 		val2 = mask - val2;
15611da177e4SLinus Torvalds 	}
15621da177e4SLinus Torvalds 	val1 <<= shift_left;
15631da177e4SLinus Torvalds 	val2 <<= shift_right;
15641da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->lock, flags);
15651da177e4SLinus Torvalds 	val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
15661da177e4SLinus Torvalds 	val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
15671da177e4SLinus Torvalds 	change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
15681da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, left_reg, val1);
15691da177e4SLinus Torvalds 	snd_opti93x_out_image(chip, right_reg, val2);
15701da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->lock, flags);
15711da177e4SLinus Torvalds 	return change;
15721da177e4SLinus Torvalds }
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds static snd_kcontrol_new_t snd_opti93x_controls[] = {
15751da177e4SLinus Torvalds OPTi93X_DOUBLE("Master Playback Switch", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
15761da177e4SLinus Torvalds OPTi93X_DOUBLE("Master Playback Volume", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1),
15771da177e4SLinus Torvalds OPTi93X_DOUBLE("PCM Playback Switch", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 7, 7, 1, 1),
15781da177e4SLinus Torvalds OPTi93X_DOUBLE("PCM Playback Volume", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 0, 0, 31, 1),
15791da177e4SLinus Torvalds OPTi93X_DOUBLE("FM Playback Switch", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 7, 7, 1, 1),
15801da177e4SLinus Torvalds OPTi93X_DOUBLE("FM Playback Volume", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 1, 1, 15, 1),
15811da177e4SLinus Torvalds OPTi93X_DOUBLE("Line Playback Switch", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 7, 7, 1, 1),
15821da177e4SLinus Torvalds OPTi93X_DOUBLE("Line Playback Volume", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 1, 1, 15, 1),
15831da177e4SLinus Torvalds OPTi93X_DOUBLE("Mic Playback Switch", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
15841da177e4SLinus Torvalds OPTi93X_DOUBLE("Mic Playback Volume", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1),
15851da177e4SLinus Torvalds OPTi93X_DOUBLE("Mic Boost", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 5, 5, 1, 1),
15861da177e4SLinus Torvalds OPTi93X_DOUBLE("CD Playback Switch", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 7, 7, 1, 1),
15871da177e4SLinus Torvalds OPTi93X_DOUBLE("CD Playback Volume", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 1, 1, 15, 1),
15881da177e4SLinus Torvalds OPTi93X_DOUBLE("Aux Playback Switch", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
15891da177e4SLinus Torvalds OPTi93X_DOUBLE("Aux Playback Volume", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1),
15901da177e4SLinus Torvalds OPTi93X_DOUBLE("Capture Volume", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 0, 0, 15, 0),
15911da177e4SLinus Torvalds {
15921da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
15931da177e4SLinus Torvalds 	.name = "Capture Source",
15941da177e4SLinus Torvalds 	.info = snd_opti93x_info_mux,
15951da177e4SLinus Torvalds 	.get = snd_opti93x_get_mux,
15961da177e4SLinus Torvalds 	.put = snd_opti93x_put_mux,
15971da177e4SLinus Torvalds }
15981da177e4SLinus Torvalds };
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds static int snd_opti93x_mixer(opti93x_t *chip)
16011da177e4SLinus Torvalds {
16021da177e4SLinus Torvalds 	snd_card_t *card;
16031da177e4SLinus Torvalds 	snd_kcontrol_new_t knew;
16041da177e4SLinus Torvalds 	int err;
16051da177e4SLinus Torvalds 	unsigned int idx;
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 	snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 	card = chip->card;
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 	strcpy(card->mixername, snd_opti93x_chip_id(chip));
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
16141da177e4SLinus Torvalds 		knew = snd_opti93x_controls[idx];
16151da177e4SLinus Torvalds 		if (chip->hardware == OPTi9XX_HW_82C930) {
16161da177e4SLinus Torvalds 			if (strstr(knew.name, "FM"))	/* skip FM controls */
16171da177e4SLinus Torvalds 				continue;
16181da177e4SLinus Torvalds 			else if (strcmp(knew.name, "Mic Playback Volume"))
16191da177e4SLinus Torvalds 				OPTi93X_DOUBLE_INVERT_INVERT(knew);
16201da177e4SLinus Torvalds 			else if (strstr(knew.name, "Aux"))
16211da177e4SLinus Torvalds 				OPTi93X_DOUBLE_CHANGE_REGS(knew, OPTi930_AUX_LEFT_INPUT, OPTi930_AUX_RIGHT_INPUT);
16221da177e4SLinus Torvalds 			else if (strcmp(knew.name, "PCM Playback Volume"))
16231da177e4SLinus Torvalds 				OPTi93X_DOUBLE_INVERT_INVERT(knew);
16241da177e4SLinus Torvalds 			else if (strcmp(knew.name, "Master Playback Volume"))
16251da177e4SLinus Torvalds 				OPTi93X_DOUBLE_INVERT_INVERT(knew);
16261da177e4SLinus Torvalds 		}
16271da177e4SLinus Torvalds 		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_opti93x_controls[idx], chip))) < 0)
16281da177e4SLinus Torvalds 			return err;
16291da177e4SLinus Torvalds 	}
16301da177e4SLinus Torvalds 	return 0;
16311da177e4SLinus Torvalds }
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds #endif /* OPTi93X */
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds static int __devinit snd_card_opti9xx_detect(snd_card_t *card, opti9xx_t *chip)
16361da177e4SLinus Torvalds {
16371da177e4SLinus Torvalds 	int i, err;
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds #ifndef OPTi93X
16401da177e4SLinus Torvalds 	for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
16411da177e4SLinus Torvalds 		unsigned char value;
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 		if ((err = snd_opti9xx_init(chip, i)) < 0)
16441da177e4SLinus Torvalds 			return err;
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds 		if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
16471da177e4SLinus Torvalds 			continue;
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds 		value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
16501da177e4SLinus Torvalds 		if ((value != 0xff) && (value != inb(chip->mc_base + 1)))
16511da177e4SLinus Torvalds 			if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
16521da177e4SLinus Torvalds 				return 1;
16531da177e4SLinus Torvalds 
1654*b1d5776dSTakashi Iwai 		release_and_free_resource(chip->res_mc_base);
16551da177e4SLinus Torvalds 		chip->res_mc_base = NULL;
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 	}
16581da177e4SLinus Torvalds #else	/* OPTi93X */
16591da177e4SLinus Torvalds 	for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
16601da177e4SLinus Torvalds 		unsigned long flags;
16611da177e4SLinus Torvalds 		unsigned char value;
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 		if ((err = snd_opti9xx_init(chip, i)) < 0)
16641da177e4SLinus Torvalds 			return err;
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 		if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
16671da177e4SLinus Torvalds 			continue;
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds 		spin_lock_irqsave(&chip->lock, flags);
16701da177e4SLinus Torvalds 		outb(chip->password, chip->mc_base + chip->pwd_reg);
16711da177e4SLinus Torvalds 		outb(((chip->mc_indir_index & (1 << 8)) >> 4) |
16721da177e4SLinus Torvalds 			((chip->mc_indir_index & 0xf0) >> 4), chip->mc_base);
16731da177e4SLinus Torvalds 		spin_unlock_irqrestore(&chip->lock, flags);
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds 		value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
16761da177e4SLinus Torvalds 		snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
16771da177e4SLinus Torvalds 		if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
16781da177e4SLinus Torvalds 			return 1;
16791da177e4SLinus Torvalds 
1680*b1d5776dSTakashi Iwai 		release_and_free_resource(chip->res_mc_base);
16811da177e4SLinus Torvalds 		chip->res_mc_base = NULL;
16821da177e4SLinus Torvalds 	}
16831da177e4SLinus Torvalds #endif	/* OPTi93X */
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds 	return -ENODEV;
16861da177e4SLinus Torvalds }
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds #ifdef CONFIG_PNP
16891da177e4SLinus Torvalds static int __devinit snd_card_opti9xx_pnp(opti9xx_t *chip, struct pnp_card_link *card,
16901da177e4SLinus Torvalds 					  const struct pnp_card_device_id *pid)
16911da177e4SLinus Torvalds {
16921da177e4SLinus Torvalds 	struct pnp_dev *pdev;
16931da177e4SLinus Torvalds 	struct pnp_resource_table *cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
16941da177e4SLinus Torvalds 	int err;
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds 	chip->dev = pnp_request_card_device(card, pid->devs[0].id, NULL);
16971da177e4SLinus Torvalds 	if (chip->dev == NULL) {
16981da177e4SLinus Torvalds 		kfree(cfg);
16991da177e4SLinus Torvalds 		return -EBUSY;
17001da177e4SLinus Torvalds 	}
17011da177e4SLinus Torvalds 	chip->devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	pdev = chip->dev;
17041da177e4SLinus Torvalds 	pnp_init_resource_table(cfg);
17051da177e4SLinus Torvalds 
17061da177e4SLinus Torvalds #ifdef OPTi93X
17071da177e4SLinus Torvalds 	if (port != SNDRV_AUTO_PORT)
17081da177e4SLinus Torvalds 		pnp_resource_change(&cfg->port_resource[0], port + 4, 4);
17091da177e4SLinus Torvalds #else
17101da177e4SLinus Torvalds 	if (pid->driver_data != 0x0924 && port != SNDRV_AUTO_PORT)
17111da177e4SLinus Torvalds 		pnp_resource_change(&cfg->port_resource[1], port, 4);
17121da177e4SLinus Torvalds #endif	/* OPTi93X */
17131da177e4SLinus Torvalds 	if (irq != SNDRV_AUTO_IRQ)
17141da177e4SLinus Torvalds 		pnp_resource_change(&cfg->irq_resource[0], irq, 1);
17151da177e4SLinus Torvalds 	if (dma1 != SNDRV_AUTO_DMA)
17161da177e4SLinus Torvalds 		pnp_resource_change(&cfg->dma_resource[0], dma1, 1);
17171da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
17181da177e4SLinus Torvalds 	if (dma2 != SNDRV_AUTO_DMA)
17191da177e4SLinus Torvalds 		pnp_resource_change(&cfg->dma_resource[1], dma2, 1);
17201da177e4SLinus Torvalds #else
17211da177e4SLinus Torvalds #ifdef snd_opti9xx_fixup_dma2
17221da177e4SLinus Torvalds 	snd_opti9xx_fixup_dma2(pdev);
17231da177e4SLinus Torvalds #endif
17241da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
17251da177e4SLinus Torvalds #ifdef OPTi93X
17261da177e4SLinus Torvalds 	if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT)
17271da177e4SLinus Torvalds 		pnp_resource_change(&cfg->port_resource[1], fm_port, 4);
17281da177e4SLinus Torvalds #else
17291da177e4SLinus Torvalds 	if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT)
17301da177e4SLinus Torvalds 		pnp_resource_change(&cfg->port_resource[2], fm_port, 4);
17311da177e4SLinus Torvalds #endif
17321da177e4SLinus Torvalds 	if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
17331da177e4SLinus Torvalds 		snd_printk(KERN_ERR "AUDIO the requested resources are invalid, using auto config\n");
17341da177e4SLinus Torvalds 	err = pnp_activate_dev(pdev);
17351da177e4SLinus Torvalds 	if (err < 0) {
17361da177e4SLinus Torvalds 		snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
17371da177e4SLinus Torvalds 		kfree(cfg);
17381da177e4SLinus Torvalds 		return err;
17391da177e4SLinus Torvalds 	}
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds #ifdef OPTi93X
17421da177e4SLinus Torvalds 	port = pnp_port_start(pdev, 0) - 4;
17431da177e4SLinus Torvalds 	fm_port = pnp_port_start(pdev, 1);
17441da177e4SLinus Torvalds #else
17451da177e4SLinus Torvalds 	if (pid->driver_data != 0x0924)
17461da177e4SLinus Torvalds 		port = pnp_port_start(pdev, 1);
17471da177e4SLinus Torvalds 	fm_port = pnp_port_start(pdev, 2);
17481da177e4SLinus Torvalds #endif	/* OPTi93X */
17491da177e4SLinus Torvalds 	irq = pnp_irq(pdev, 0);
17501da177e4SLinus Torvalds 	dma1 = pnp_dma(pdev, 0);
17511da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
17521da177e4SLinus Torvalds 	dma2 = pnp_dma(pdev, 1);
17531da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 	pdev = chip->devmpu;
17561da177e4SLinus Torvalds 	if (pdev && mpu_port > 0) {
17571da177e4SLinus Torvalds 		pnp_init_resource_table(cfg);
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds 		if (mpu_port != SNDRV_AUTO_PORT)
17601da177e4SLinus Torvalds 			pnp_resource_change(&cfg->port_resource[0], mpu_port, 2);
17611da177e4SLinus Torvalds 		if (mpu_irq != SNDRV_AUTO_IRQ)
17621da177e4SLinus Torvalds 			pnp_resource_change(&cfg->irq_resource[0], mpu_irq, 1);
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 		if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
17651da177e4SLinus Torvalds 			snd_printk(KERN_ERR "AUDIO the requested resources are invalid, using auto config\n");
17661da177e4SLinus Torvalds 		err = pnp_activate_dev(pdev);
17671da177e4SLinus Torvalds 		if (err < 0) {
17681da177e4SLinus Torvalds 			snd_printk(KERN_ERR "AUDIO pnp configure failure\n");
17691da177e4SLinus Torvalds 			mpu_port = -1;
17701da177e4SLinus Torvalds 			chip->devmpu = NULL;
17711da177e4SLinus Torvalds 		} else {
17721da177e4SLinus Torvalds 			mpu_port = pnp_port_start(pdev, 0);
17731da177e4SLinus Torvalds 			mpu_irq = pnp_irq(pdev, 0);
17741da177e4SLinus Torvalds 		}
17751da177e4SLinus Torvalds 	}
17761da177e4SLinus Torvalds 	kfree(cfg);
17771da177e4SLinus Torvalds 	return pid->driver_data;
17781da177e4SLinus Torvalds }
17791da177e4SLinus Torvalds #endif	/* CONFIG_PNP */
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds #if 0
17821da177e4SLinus Torvalds static int __devinit snd_card_opti9xx_resources(struct snd_card_opti9xx *chip,
17831da177e4SLinus Torvalds 						snd_card_t *card)
17841da177e4SLinus Torvalds {
17851da177e4SLinus Torvalds 	int error, i, pnp = 0;
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds #ifdef CONFIG_PNP
17881da177e4SLinus Torvalds 	pnp = chip->dev != NULL;
17891da177e4SLinus Torvalds #endif	/* CONFIG_PNP */
17901da177e4SLinus Torvalds 
17911da177e4SLinus Torvalds #ifndef OPTi93X
17921da177e4SLinus Torvalds 	if (chip->chip->hardware == OPTi9XX_HW_82C928)
17931da177e4SLinus Torvalds 		mpu_port = -1;
17941da177e4SLinus Torvalds #endif	/* OPTi93X */
17951da177e4SLinus Torvalds 	error = 0;
17961da177e4SLinus Torvalds 	if (!pnp && (mpu_port == SNDRV_DEFAULT_PORT1)) {
17971da177e4SLinus Torvalds 		for (i = 0; possible_mpu_ports[i] != -1; i++)
17981da177e4SLinus Torvalds 			if (!snd_register_ioport(card, possible_mpu_ports[i], 2,
17991da177e4SLinus Torvalds 					DRIVER_NAME" - MPU-401", NULL)) {
18001da177e4SLinus Torvalds 				mpu_port = possible_mpu_ports[i];
18011da177e4SLinus Torvalds 				break;
18021da177e4SLinus Torvalds 			}
18031da177e4SLinus Torvalds 		if (mpu_port == SNDRV_DEFAULT_PORT1)
18041da177e4SLinus Torvalds 			error = -EBUSY;
18051da177e4SLinus Torvalds 	}
18061da177e4SLinus Torvalds 	else
18071da177e4SLinus Torvalds 		error = (mpu_port == -1) ? -ENODEV :
18081da177e4SLinus Torvalds 			snd_register_ioport(card, mpu_port, 2,
18091da177e4SLinus Torvalds 			DRIVER_NAME" - MPU-401", NULL);
18101da177e4SLinus Torvalds 	if (error)
18111da177e4SLinus Torvalds 		chip->chip->mpu_port = -1;
18121da177e4SLinus Torvalds 	else if (pnp && (irq == mpu_irq))
18131da177e4SLinus Torvalds 		chip->chip->mpu_irq = mpu_irq;
18141da177e4SLinus Torvalds 	else if (!snd_register_interrupt(card,
18151da177e4SLinus Torvalds 			DRIVER_NAME" - MPU-401",
18161da177e4SLinus Torvalds 			mpu_irq, SNDRV_IRQ_TYPE_ISA,
18171da177e4SLinus Torvalds 			snd_card_opti9xx_mpu_interrupt, chip,
18181da177e4SLinus Torvalds 			pnp ? no_alternatives : possible_mpu_irqs,
18191da177e4SLinus Torvalds 			&chip->mpuirqptr)) {
18201da177e4SLinus Torvalds 		chip->chip->mpu_port = mpu_port;
18211da177e4SLinus Torvalds 		chip->chip->mpu_irq = chip->mpuirqptr->irq;
18221da177e4SLinus Torvalds 	}
18231da177e4SLinus Torvalds 	else
18241da177e4SLinus Torvalds 		chip->chip->mpu_port = -1;
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds 	if (!pnp && (port == SNDRV_DEFAULT_PORT1)) {
18271da177e4SLinus Torvalds 		for (i = 0; possible_ports[i] != -1; i++)
18281da177e4SLinus Torvalds 			if (!snd_register_ioport(card, possible_ports[i], 8,
18291da177e4SLinus Torvalds 					DRIVER_NAME" - WSS", NULL)) {
18301da177e4SLinus Torvalds 				port = possible_ports[i];
18311da177e4SLinus Torvalds 				break;
18321da177e4SLinus Torvalds 			}
18331da177e4SLinus Torvalds 		if (port == SNDRV_DEFAULT_PORT1)
18341da177e4SLinus Torvalds 			return -EBUSY;
18351da177e4SLinus Torvalds 	}
18361da177e4SLinus Torvalds 	else if ((error = snd_register_ioport(card, port, 8,
18371da177e4SLinus Torvalds 			DRIVER_NAME" - WSS", NULL)) < 0)
18381da177e4SLinus Torvalds 		return error;
18391da177e4SLinus Torvalds 	chip->chip->wss_base = port;
18401da177e4SLinus Torvalds 	if ((error = snd_register_interrupt(card, DRIVER_NAME" - WSS",
18411da177e4SLinus Torvalds 			irq, SNDRV_IRQ_TYPE_ISA,
18421da177e4SLinus Torvalds 			snd_card_opti9xx_interrupt, chip,
18431da177e4SLinus Torvalds 			pnp ? no_alternatives : possible_irqs,
18441da177e4SLinus Torvalds 			&chip->irqptr)) < 0)
18451da177e4SLinus Torvalds 		return error;
18461da177e4SLinus Torvalds 	chip->chip->irq = chip->irqptr->irq;
18471da177e4SLinus Torvalds 	if ((error = snd_register_dma_channel(card,
18481da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
18491da177e4SLinus Torvalds 			DRIVER_NAME" - WSS playback",
18501da177e4SLinus Torvalds #else
18511da177e4SLinus Torvalds 			DRIVER_NAME" - WSS",
18521da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
18531da177e4SLinus Torvalds 			dma1, SNDRV_DMA_TYPE_ISA, dma1_size,
18541da177e4SLinus Torvalds 			pnp ? no_alternatives : possible_dma1s,
18551da177e4SLinus Torvalds 			&chip->dma1ptr)) < 0)
18561da177e4SLinus Torvalds 		return error;
18571da177e4SLinus Torvalds 	chip->chip->dma1 = chip->dma1ptr->dma;
18581da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
18591da177e4SLinus Torvalds 	if ((error = snd_register_dma_channel(card, DRIVER_NAME" - WSS capture",
18601da177e4SLinus Torvalds 			dma2, SNDRV_DMA_TYPE_ISA, dma2_size,
18611da177e4SLinus Torvalds 			pnp ? no_alternatives :
18621da177e4SLinus Torvalds 				possible_dma2s[chip->dma1ptr->dma],
18631da177e4SLinus Torvalds 			&chip->dma2ptr)) < 0)
18641da177e4SLinus Torvalds 		return error;
18651da177e4SLinus Torvalds 	chip->chip->dma2 = chip->dma2ptr->dma;
18661da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	if (snd_register_ioport(card,
18691da177e4SLinus Torvalds 			pnp ? fm_port : fm_port = 0x388, 4,
18701da177e4SLinus Torvalds 			DRIVER_NAME" - OPL", NULL) < 0)
18711da177e4SLinus Torvalds 		fm_port = -1;
18721da177e4SLinus Torvalds 	chip->chip->fm_port = fm_port;
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 	return 0;
18751da177e4SLinus Torvalds }
18761da177e4SLinus Torvalds #endif
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds static void snd_card_opti9xx_free(snd_card_t *card)
18791da177e4SLinus Torvalds {
18801da177e4SLinus Torvalds 	opti9xx_t *chip = (opti9xx_t *)card->private_data;
18811da177e4SLinus Torvalds 
1882*b1d5776dSTakashi Iwai 	if (chip)
1883*b1d5776dSTakashi Iwai 		release_and_free_resource(chip->res_mc_base);
18841da177e4SLinus Torvalds }
18851da177e4SLinus Torvalds 
188643bcd973STakashi Iwai static int snd_card_opti9xx_probe(struct pnp_card_link *pcard,
18871da177e4SLinus Torvalds 				  const struct pnp_card_device_id *pid)
18881da177e4SLinus Torvalds {
18891da177e4SLinus Torvalds 	static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
18901da177e4SLinus Torvalds 	static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
18911da177e4SLinus Torvalds #ifdef OPTi93X
18921da177e4SLinus Torvalds 	static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
18931da177e4SLinus Torvalds #else
18941da177e4SLinus Torvalds 	static int possible_irqs[] = {9, 10, 11, 7, -1};
18951da177e4SLinus Torvalds #endif	/* OPTi93X */
18961da177e4SLinus Torvalds 	static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
18971da177e4SLinus Torvalds 	static int possible_dma1s[] = {3, 1, 0, -1};
18981da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
18991da177e4SLinus Torvalds 	static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
19001da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
19011da177e4SLinus Torvalds 	int error;
19021da177e4SLinus Torvalds 	opti9xx_t *chip;
19031da177e4SLinus Torvalds #if defined(OPTi93X)
19041da177e4SLinus Torvalds 	opti93x_t *codec;
19051da177e4SLinus Torvalds #elif defined(CS4231)
19061da177e4SLinus Torvalds 	cs4231_t *codec;
19071da177e4SLinus Torvalds 	snd_timer_t *timer;
19081da177e4SLinus Torvalds #else
19091da177e4SLinus Torvalds 	ad1848_t *codec;
19101da177e4SLinus Torvalds #endif
19111da177e4SLinus Torvalds 	snd_card_t *card;
19121da177e4SLinus Torvalds 	snd_pcm_t *pcm;
19131da177e4SLinus Torvalds 	snd_rawmidi_t *rmidi;
19141da177e4SLinus Torvalds 	snd_hwdep_t *synth;
19151da177e4SLinus Torvalds #ifdef CONFIG_PNP
19161da177e4SLinus Torvalds 	int hw;
19171da177e4SLinus Torvalds #endif	/* CONFIG_PNP */
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds 	if (pcard && !snd_opti9xx_first_hit)
19201da177e4SLinus Torvalds 		return -EBUSY;
19211da177e4SLinus Torvalds 	if (!(card = snd_card_new(index, id, THIS_MODULE,
19221da177e4SLinus Torvalds 				  sizeof(opti9xx_t))))
19231da177e4SLinus Torvalds 		return -ENOMEM;
19241da177e4SLinus Torvalds 	card->private_free = snd_card_opti9xx_free;
19251da177e4SLinus Torvalds 	chip = (opti9xx_t *)card->private_data;
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds #ifdef CONFIG_PNP
19281da177e4SLinus Torvalds 	if (isapnp && pcard && (hw = snd_card_opti9xx_pnp(chip, pcard, pid)) > 0) {
19291da177e4SLinus Torvalds 		switch (hw) {
19301da177e4SLinus Torvalds 		case 0x0924:
19311da177e4SLinus Torvalds 			hw = OPTi9XX_HW_82C924;
19321da177e4SLinus Torvalds 			break;
19331da177e4SLinus Torvalds 		case 0x0925:
19341da177e4SLinus Torvalds 			hw = OPTi9XX_HW_82C925;
19351da177e4SLinus Torvalds 			break;
19361da177e4SLinus Torvalds 		case 0x0931:
19371da177e4SLinus Torvalds 			hw = OPTi9XX_HW_82C931;
19381da177e4SLinus Torvalds 			break;
19391da177e4SLinus Torvalds 		default:
19401da177e4SLinus Torvalds 			snd_card_free(card);
19411da177e4SLinus Torvalds 			return -ENODEV;
19421da177e4SLinus Torvalds 		}
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 		if ((error = snd_opti9xx_init(chip, hw))) {
19451da177e4SLinus Torvalds 			snd_card_free(card);
19461da177e4SLinus Torvalds 			return error;
19471da177e4SLinus Torvalds 		}
19481da177e4SLinus Torvalds 		if (hw <= OPTi9XX_HW_82C930)
19491da177e4SLinus Torvalds 			chip->mc_base -= 0x80;
19501da177e4SLinus Torvalds 		snd_card_set_dev(card, &pcard->card->dev);
19511da177e4SLinus Torvalds 	} else {
19521da177e4SLinus Torvalds #endif	/* CONFIG_PNP */
19531da177e4SLinus Torvalds 		if ((error = snd_card_opti9xx_detect(card, chip)) < 0) {
19541da177e4SLinus Torvalds 			snd_card_free(card);
19551da177e4SLinus Torvalds 			return error;
19561da177e4SLinus Torvalds 		}
195743bcd973STakashi Iwai 		if ((error = snd_card_set_generic_dev(card)) < 0) {
195843bcd973STakashi Iwai 			snd_card_free(card);
195943bcd973STakashi Iwai 			return error;
196043bcd973STakashi Iwai 		}
19611da177e4SLinus Torvalds #ifdef CONFIG_PNP
19621da177e4SLinus Torvalds 	}
19631da177e4SLinus Torvalds #endif	/* CONFIG_PNP */
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	if (! chip->res_mc_base &&
19661da177e4SLinus Torvalds 	    (chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL) {
19671da177e4SLinus Torvalds 		snd_card_free(card);
19681da177e4SLinus Torvalds 		return -ENOMEM;
19691da177e4SLinus Torvalds 	}
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	chip->wss_base = port;
19721da177e4SLinus Torvalds 	chip->fm_port = fm_port;
19731da177e4SLinus Torvalds 	chip->mpu_port = mpu_port;
19741da177e4SLinus Torvalds 	chip->irq = irq;
19751da177e4SLinus Torvalds 	chip->mpu_irq = mpu_irq;
19761da177e4SLinus Torvalds 	chip->dma1 = dma1;
19771da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
19781da177e4SLinus Torvalds 	chip->dma2 = dma2;
19791da177e4SLinus Torvalds #endif
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 	if (chip->wss_base == SNDRV_AUTO_PORT) {
19821da177e4SLinus Torvalds 		if ((chip->wss_base = snd_legacy_find_free_ioport(possible_ports, 4)) < 0) {
19831da177e4SLinus Torvalds 			snd_card_free(card);
19841da177e4SLinus Torvalds 			snd_printk("unable to find a free WSS port\n");
19851da177e4SLinus Torvalds 			return -EBUSY;
19861da177e4SLinus Torvalds 		}
19871da177e4SLinus Torvalds 	}
19881da177e4SLinus Torvalds #ifdef CONFIG_PNP
19891da177e4SLinus Torvalds 	if (!isapnp) {
19901da177e4SLinus Torvalds #endif
19911da177e4SLinus Torvalds 	if (chip->mpu_port == SNDRV_AUTO_PORT) {
19921da177e4SLinus Torvalds 		if ((chip->mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
19931da177e4SLinus Torvalds 			snd_card_free(card);
19941da177e4SLinus Torvalds 			snd_printk("unable to find a free MPU401 port\n");
19951da177e4SLinus Torvalds 			return -EBUSY;
19961da177e4SLinus Torvalds 		}
19971da177e4SLinus Torvalds 	}
19981da177e4SLinus Torvalds 	if (chip->irq == SNDRV_AUTO_IRQ) {
19991da177e4SLinus Torvalds 		if ((chip->irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
20001da177e4SLinus Torvalds 			snd_card_free(card);
20011da177e4SLinus Torvalds 			snd_printk("unable to find a free IRQ\n");
20021da177e4SLinus Torvalds 			return -EBUSY;
20031da177e4SLinus Torvalds 		}
20041da177e4SLinus Torvalds 	}
20051da177e4SLinus Torvalds 	if (chip->mpu_irq == SNDRV_AUTO_IRQ) {
20061da177e4SLinus Torvalds 		if ((chip->mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
20071da177e4SLinus Torvalds 			snd_card_free(card);
20081da177e4SLinus Torvalds 			snd_printk("unable to find a free MPU401 IRQ\n");
20091da177e4SLinus Torvalds 			return -EBUSY;
20101da177e4SLinus Torvalds 		}
20111da177e4SLinus Torvalds 	}
20121da177e4SLinus Torvalds 	if (chip->dma1 == SNDRV_AUTO_DMA) {
20131da177e4SLinus Torvalds                 if ((chip->dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
20141da177e4SLinus Torvalds                         snd_card_free(card);
20151da177e4SLinus Torvalds 			snd_printk("unable to find a free DMA1\n");
20161da177e4SLinus Torvalds 			return -EBUSY;
20171da177e4SLinus Torvalds 		}
20181da177e4SLinus Torvalds         }
20191da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
20201da177e4SLinus Torvalds 	if (chip->dma2 == SNDRV_AUTO_DMA) {
20211da177e4SLinus Torvalds                 if ((chip->dma2 = snd_legacy_find_free_dma(possible_dma2s[chip->dma1 % 4])) < 0) {
20221da177e4SLinus Torvalds                         snd_card_free(card);
20231da177e4SLinus Torvalds 			snd_printk("unable to find a free DMA2\n");
20241da177e4SLinus Torvalds 			return -EBUSY;
20251da177e4SLinus Torvalds 		}
20261da177e4SLinus Torvalds         }
20271da177e4SLinus Torvalds #endif
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds #ifdef CONFIG_PNP
20301da177e4SLinus Torvalds 	}
20311da177e4SLinus Torvalds #endif
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds 	if ((error = snd_opti9xx_configure(chip))) {
20341da177e4SLinus Torvalds 		snd_card_free(card);
20351da177e4SLinus Torvalds 		return error;
20361da177e4SLinus Torvalds 	}
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds #if defined(OPTi93X)
20391da177e4SLinus Torvalds 	if ((error = snd_opti93x_create(card, chip, chip->dma1, chip->dma2, &codec))) {
20401da177e4SLinus Torvalds 		snd_card_free(card);
20411da177e4SLinus Torvalds 		return error;
20421da177e4SLinus Torvalds 	}
20431da177e4SLinus Torvalds 	if ((error = snd_opti93x_pcm(codec, 0, &pcm)) < 0) {
20441da177e4SLinus Torvalds 		snd_card_free(card);
20451da177e4SLinus Torvalds 		return error;
20461da177e4SLinus Torvalds 	}
20471da177e4SLinus Torvalds 	if ((error = snd_opti93x_mixer(codec)) < 0) {
20481da177e4SLinus Torvalds 		snd_card_free(card);
20491da177e4SLinus Torvalds 		return error;
20501da177e4SLinus Torvalds 	}
20511da177e4SLinus Torvalds #elif defined(CS4231)
20521da177e4SLinus Torvalds 	if ((error = snd_cs4231_create(card, chip->wss_base + 4, -1,
20531da177e4SLinus Torvalds 				       chip->irq, chip->dma1, chip->dma2,
20541da177e4SLinus Torvalds 				       CS4231_HW_DETECT,
20551da177e4SLinus Torvalds 				       0,
20561da177e4SLinus Torvalds 				       &codec)) < 0) {
20571da177e4SLinus Torvalds 		snd_card_free(card);
20581da177e4SLinus Torvalds 		return error;
20591da177e4SLinus Torvalds 	}
20601da177e4SLinus Torvalds 	if ((error = snd_cs4231_pcm(codec, 0, &pcm)) < 0) {
20611da177e4SLinus Torvalds 		snd_card_free(card);
20621da177e4SLinus Torvalds 		return error;
20631da177e4SLinus Torvalds 	}
20641da177e4SLinus Torvalds 	if ((error = snd_cs4231_mixer(codec)) < 0) {
20651da177e4SLinus Torvalds 		snd_card_free(card);
20661da177e4SLinus Torvalds 		return error;
20671da177e4SLinus Torvalds 	}
20681da177e4SLinus Torvalds 	if ((error = snd_cs4231_timer(codec, 0, &timer)) < 0) {
20691da177e4SLinus Torvalds 		snd_card_free(card);
20701da177e4SLinus Torvalds 		return error;
20711da177e4SLinus Torvalds 	}
20721da177e4SLinus Torvalds #else
20731da177e4SLinus Torvalds 	if ((error = snd_ad1848_create(card, chip->wss_base + 4,
20741da177e4SLinus Torvalds 				       chip->irq, chip->dma1,
20751da177e4SLinus Torvalds 				       AD1848_HW_DETECT, &codec)) < 0) {
20761da177e4SLinus Torvalds 		snd_card_free(card);
20771da177e4SLinus Torvalds 		return error;
20781da177e4SLinus Torvalds 	}
20791da177e4SLinus Torvalds 	if ((error = snd_ad1848_pcm(codec, 0, &pcm)) < 0) {
20801da177e4SLinus Torvalds 		snd_card_free(card);
20811da177e4SLinus Torvalds 		return error;
20821da177e4SLinus Torvalds 	}
20831da177e4SLinus Torvalds 	if ((error = snd_ad1848_mixer(codec)) < 0) {
20841da177e4SLinus Torvalds 		snd_card_free(card);
20851da177e4SLinus Torvalds 		return error;
20861da177e4SLinus Torvalds 	}
20871da177e4SLinus Torvalds #endif
20881da177e4SLinus Torvalds 	strcpy(card->driver, chip->name);
20891da177e4SLinus Torvalds 	sprintf(card->shortname, "OPTi %s", card->driver);
20901da177e4SLinus Torvalds #if defined(CS4231) || defined(OPTi93X)
20911da177e4SLinus Torvalds 	sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
20921da177e4SLinus Torvalds 		card->shortname, pcm->name, chip->wss_base + 4,
20931da177e4SLinus Torvalds 		chip->irq, chip->dma1, chip->dma2);
20941da177e4SLinus Torvalds #else
20951da177e4SLinus Torvalds 	sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
20961da177e4SLinus Torvalds 		card->shortname, pcm->name, chip->wss_base + 4,
20971da177e4SLinus Torvalds 		chip->irq, chip->dma1);
20981da177e4SLinus Torvalds #endif	/* CS4231 || OPTi93X */
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 	if (chip->mpu_port <= 0 || chip->mpu_port == SNDRV_AUTO_PORT)
21011da177e4SLinus Torvalds 		rmidi = NULL;
21021da177e4SLinus Torvalds 	else
21031da177e4SLinus Torvalds 		if ((error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
21041da177e4SLinus Torvalds 				chip->mpu_port, 0, chip->mpu_irq, SA_INTERRUPT,
21051da177e4SLinus Torvalds 				&rmidi)))
21061da177e4SLinus Torvalds 			snd_printk("no MPU-401 device at 0x%lx?\n", chip->mpu_port);
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds 	if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
21091da177e4SLinus Torvalds 		opl3_t *opl3 = NULL;
21101da177e4SLinus Torvalds #ifndef OPTi93X
21111da177e4SLinus Torvalds 		if (chip->hardware == OPTi9XX_HW_82C928 ||
21121da177e4SLinus Torvalds 		    chip->hardware == OPTi9XX_HW_82C929 ||
21131da177e4SLinus Torvalds 		    chip->hardware == OPTi9XX_HW_82C924) {
21141da177e4SLinus Torvalds 			opl4_t *opl4;
21151da177e4SLinus Torvalds 			/* assume we have an OPL4 */
21161da177e4SLinus Torvalds 			snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
21171da177e4SLinus Torvalds 					       0x20, 0x20);
21181da177e4SLinus Torvalds 			if (snd_opl4_create(card,
21191da177e4SLinus Torvalds 					    chip->fm_port,
21201da177e4SLinus Torvalds 					    chip->fm_port - 8,
21211da177e4SLinus Torvalds 					    2, &opl3, &opl4) < 0) {
21221da177e4SLinus Torvalds 				/* no luck, use OPL3 instead */
21231da177e4SLinus Torvalds 				snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
21241da177e4SLinus Torvalds 						       0x00, 0x20);
21251da177e4SLinus Torvalds 			}
21261da177e4SLinus Torvalds 		}
21271da177e4SLinus Torvalds #endif	/* !OPTi93X */
21281da177e4SLinus Torvalds 		if (!opl3 && snd_opl3_create(card,
21291da177e4SLinus Torvalds 					     chip->fm_port,
21301da177e4SLinus Torvalds 					     chip->fm_port + 2,
21311da177e4SLinus Torvalds 					     OPL3_HW_AUTO, 0, &opl3) < 0) {
21321da177e4SLinus Torvalds 			snd_printk("no OPL device at 0x%lx-0x%lx\n",
21331da177e4SLinus Torvalds 				   chip->fm_port, chip->fm_port + 4 - 1);
21341da177e4SLinus Torvalds 		}
21351da177e4SLinus Torvalds 		if (opl3) {
21361da177e4SLinus Torvalds 			if ((error = snd_opl3_timer_new(opl3,
21371da177e4SLinus Torvalds #ifdef CS4231
21381da177e4SLinus Torvalds 							1, 2)) < 0) {
21391da177e4SLinus Torvalds #else
21401da177e4SLinus Torvalds 							0, 1)) < 0) {
21411da177e4SLinus Torvalds #endif	/* CS4231 */
21421da177e4SLinus Torvalds 				snd_card_free(card);
21431da177e4SLinus Torvalds 				return error;
21441da177e4SLinus Torvalds 			}
21451da177e4SLinus Torvalds 			if ((error = snd_opl3_hwdep_new(opl3, 0, 1, &synth)) < 0) {
21461da177e4SLinus Torvalds 				snd_card_free(card);
21471da177e4SLinus Torvalds 				return error;
21481da177e4SLinus Torvalds 			}
21491da177e4SLinus Torvalds 		}
21501da177e4SLinus Torvalds 	}
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds 	if ((error = snd_card_register(card))) {
21531da177e4SLinus Torvalds 		snd_card_free(card);
21541da177e4SLinus Torvalds 		return error;
21551da177e4SLinus Torvalds 	}
21561da177e4SLinus Torvalds 	snd_opti9xx_first_hit = 0;
21571da177e4SLinus Torvalds 	if (pcard)
21581da177e4SLinus Torvalds 		pnp_set_card_drvdata(pcard, card);
21591da177e4SLinus Torvalds 	else
21601da177e4SLinus Torvalds 		snd_opti9xx_legacy = card;
21611da177e4SLinus Torvalds 	return 0;
21621da177e4SLinus Torvalds }
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds #ifdef CONFIG_PNP
21651da177e4SLinus Torvalds static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
21661da177e4SLinus Torvalds {
21671da177e4SLinus Torvalds 	snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds 	snd_card_disconnect(card);
21701da177e4SLinus Torvalds 	snd_card_free_in_thread(card);
21711da177e4SLinus Torvalds 	snd_opti9xx_first_hit = 0;
21721da177e4SLinus Torvalds }
21731da177e4SLinus Torvalds 
21741da177e4SLinus Torvalds static struct pnp_card_driver opti9xx_pnpc_driver = {
21751da177e4SLinus Torvalds 	.flags		= PNP_DRIVER_RES_DISABLE,
21761da177e4SLinus Torvalds 	.name		= "opti9xx",
21771da177e4SLinus Torvalds 	.id_table	= snd_opti9xx_pnpids,
21781da177e4SLinus Torvalds 	.probe		= snd_card_opti9xx_probe,
21791da177e4SLinus Torvalds 	.remove		= __devexit_p(snd_opti9xx_pnp_remove),
21801da177e4SLinus Torvalds };
21811da177e4SLinus Torvalds #endif
21821da177e4SLinus Torvalds 
21831da177e4SLinus Torvalds static int __init alsa_card_opti9xx_init(void)
21841da177e4SLinus Torvalds {
21851da177e4SLinus Torvalds 	int cards, error;
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds #ifdef CONFIG_PNP
21881da177e4SLinus Torvalds 	cards = pnp_register_card_driver(&opti9xx_pnpc_driver);
21891da177e4SLinus Torvalds #else
21901da177e4SLinus Torvalds 	cards = 0;
21911da177e4SLinus Torvalds #endif
21921da177e4SLinus Torvalds 	if (cards == 0 && (error = snd_card_opti9xx_probe(NULL, NULL)) < 0) {
21931da177e4SLinus Torvalds #ifdef CONFIG_PNP
21941da177e4SLinus Torvalds 		pnp_unregister_card_driver(&opti9xx_pnpc_driver);
21951da177e4SLinus Torvalds #endif
21961da177e4SLinus Torvalds #ifdef MODULE
21971da177e4SLinus Torvalds #ifdef OPTi93X
21981da177e4SLinus Torvalds 		printk(KERN_ERR "no OPTi 82C93x soundcard found\n");
21991da177e4SLinus Torvalds #else
22001da177e4SLinus Torvalds 		printk(KERN_ERR "no OPTi 82C92x soundcard found\n");
22011da177e4SLinus Torvalds #endif	/* OPTi93X */
22021da177e4SLinus Torvalds #endif
22031da177e4SLinus Torvalds 		return error;
22041da177e4SLinus Torvalds 	}
22051da177e4SLinus Torvalds 	return 0;
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds static void __exit alsa_card_opti9xx_exit(void)
22091da177e4SLinus Torvalds {
22101da177e4SLinus Torvalds #ifdef CONFIG_PNP
22111da177e4SLinus Torvalds 	pnp_unregister_card_driver(&opti9xx_pnpc_driver);
22121da177e4SLinus Torvalds #endif
22131da177e4SLinus Torvalds 	if (snd_opti9xx_legacy)
22141da177e4SLinus Torvalds 		snd_card_free(snd_opti9xx_legacy);
22151da177e4SLinus Torvalds }
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds module_init(alsa_card_opti9xx_init)
22181da177e4SLinus Torvalds module_exit(alsa_card_opti9xx_exit)
2219