xref: /linux/sound/pci/fm801.c (revision 01d25d460a3b28aab537fab9a0038d1b5832ce28)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  The driver for the ForteMedia FM801 based soundcards
31da177e4SLinus Torvalds  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
71da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
81da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
91da177e4SLinus Torvalds  *   (at your option) any later version.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
121da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
131da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141da177e4SLinus Torvalds  *   GNU General Public License for more details.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
171da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
181da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include <sound/driver.h>
231da177e4SLinus Torvalds #include <linux/delay.h>
241da177e4SLinus Torvalds #include <linux/init.h>
251da177e4SLinus Torvalds #include <linux/interrupt.h>
261da177e4SLinus Torvalds #include <linux/pci.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/moduleparam.h>
291da177e4SLinus Torvalds #include <sound/core.h>
301da177e4SLinus Torvalds #include <sound/pcm.h>
311da177e4SLinus Torvalds #include <sound/ac97_codec.h>
321da177e4SLinus Torvalds #include <sound/mpu401.h>
331da177e4SLinus Torvalds #include <sound/opl3.h>
341da177e4SLinus Torvalds #include <sound/initval.h>
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #include <asm/io.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #if (defined(CONFIG_SND_FM801_TEA575X) || defined(CONFIG_SND_FM801_TEA575X_MODULE)) && (defined(CONFIG_VIDEO_DEV) || defined(CONFIG_VIDEO_DEV_MODULE))
391da177e4SLinus Torvalds #include <sound/tea575x-tuner.h>
401da177e4SLinus Torvalds #define TEA575X_RADIO 1
411da177e4SLinus Torvalds #endif
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
441da177e4SLinus Torvalds MODULE_DESCRIPTION("ForteMedia FM801");
451da177e4SLinus Torvalds MODULE_LICENSE("GPL");
461da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{ForteMedia,FM801},"
471da177e4SLinus Torvalds 		"{Genius,SoundMaker Live 5.1}}");
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
501da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
511da177e4SLinus Torvalds static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  *  Enable TEA575x tuner
541da177e4SLinus Torvalds  *    1 = MediaForte 256-PCS
551da177e4SLinus Torvalds  *    2 = MediaForte 256-PCPR
561da177e4SLinus Torvalds  *    3 = MediaForte 64-PCR
571da177e4SLinus Torvalds  *  High 16-bits are video (radio) device number + 1
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds static int tea575x_tuner[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 0 };
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
621da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
631da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
641da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
651da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
661da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
671da177e4SLinus Torvalds module_param_array(tea575x_tuner, int, NULL, 0444);
681da177e4SLinus Torvalds MODULE_PARM_DESC(tea575x_tuner, "Enable TEA575x tuner.");
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /*
711da177e4SLinus Torvalds  *  Direct registers
721da177e4SLinus Torvalds  */
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds #define FM801_REG(chip, reg)	(chip->port + FM801_##reg)
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds #define FM801_PCM_VOL		0x00	/* PCM Output Volume */
771da177e4SLinus Torvalds #define FM801_FM_VOL		0x02	/* FM Output Volume */
781da177e4SLinus Torvalds #define FM801_I2S_VOL		0x04	/* I2S Volume */
791da177e4SLinus Torvalds #define FM801_REC_SRC		0x06	/* Record Source */
801da177e4SLinus Torvalds #define FM801_PLY_CTRL		0x08	/* Playback Control */
811da177e4SLinus Torvalds #define FM801_PLY_COUNT		0x0a	/* Playback Count */
821da177e4SLinus Torvalds #define FM801_PLY_BUF1		0x0c	/* Playback Bufer I */
831da177e4SLinus Torvalds #define FM801_PLY_BUF2		0x10	/* Playback Buffer II */
841da177e4SLinus Torvalds #define FM801_CAP_CTRL		0x14	/* Capture Control */
851da177e4SLinus Torvalds #define FM801_CAP_COUNT		0x16	/* Capture Count */
861da177e4SLinus Torvalds #define FM801_CAP_BUF1		0x18	/* Capture Buffer I */
871da177e4SLinus Torvalds #define FM801_CAP_BUF2		0x1c	/* Capture Buffer II */
881da177e4SLinus Torvalds #define FM801_CODEC_CTRL	0x22	/* Codec Control */
891da177e4SLinus Torvalds #define FM801_I2S_MODE		0x24	/* I2S Mode Control */
901da177e4SLinus Torvalds #define FM801_VOLUME		0x26	/* Volume Up/Down/Mute Status */
911da177e4SLinus Torvalds #define FM801_I2C_CTRL		0x29	/* I2C Control */
921da177e4SLinus Torvalds #define FM801_AC97_CMD		0x2a	/* AC'97 Command */
931da177e4SLinus Torvalds #define FM801_AC97_DATA		0x2c	/* AC'97 Data */
941da177e4SLinus Torvalds #define FM801_MPU401_DATA	0x30	/* MPU401 Data */
951da177e4SLinus Torvalds #define FM801_MPU401_CMD	0x31	/* MPU401 Command */
961da177e4SLinus Torvalds #define FM801_GPIO_CTRL		0x52	/* General Purpose I/O Control */
971da177e4SLinus Torvalds #define FM801_GEN_CTRL		0x54	/* General Control */
981da177e4SLinus Torvalds #define FM801_IRQ_MASK		0x56	/* Interrupt Mask */
991da177e4SLinus Torvalds #define FM801_IRQ_STATUS	0x5a	/* Interrupt Status */
1001da177e4SLinus Torvalds #define FM801_OPL3_BANK0	0x68	/* OPL3 Status Read / Bank 0 Write */
1011da177e4SLinus Torvalds #define FM801_OPL3_DATA0	0x69	/* OPL3 Data 0 Write */
1021da177e4SLinus Torvalds #define FM801_OPL3_BANK1	0x6a	/* OPL3 Bank 1 Write */
1031da177e4SLinus Torvalds #define FM801_OPL3_DATA1	0x6b	/* OPL3 Bank 1 Write */
1041da177e4SLinus Torvalds #define FM801_POWERDOWN		0x70	/* Blocks Power Down Control */
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds #define FM801_AC97_ADDR_SHIFT	10
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds /* playback and record control register bits */
1091da177e4SLinus Torvalds #define FM801_BUF1_LAST		(1<<1)
1101da177e4SLinus Torvalds #define FM801_BUF2_LAST		(1<<2)
1111da177e4SLinus Torvalds #define FM801_START		(1<<5)
1121da177e4SLinus Torvalds #define FM801_PAUSE		(1<<6)
1131da177e4SLinus Torvalds #define FM801_IMMED_STOP	(1<<7)
1141da177e4SLinus Torvalds #define FM801_RATE_SHIFT	8
1151da177e4SLinus Torvalds #define FM801_RATE_MASK		(15 << FM801_RATE_SHIFT)
1161da177e4SLinus Torvalds #define FM801_CHANNELS_4	(1<<12)	/* playback only */
1171da177e4SLinus Torvalds #define FM801_CHANNELS_6	(2<<12)	/* playback only */
1181da177e4SLinus Torvalds #define FM801_CHANNELS_6MS	(3<<12)	/* playback only */
1191da177e4SLinus Torvalds #define FM801_CHANNELS_MASK	(3<<12)
1201da177e4SLinus Torvalds #define FM801_16BIT		(1<<14)
1211da177e4SLinus Torvalds #define FM801_STEREO		(1<<15)
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /* IRQ status bits */
1241da177e4SLinus Torvalds #define FM801_IRQ_PLAYBACK	(1<<8)
1251da177e4SLinus Torvalds #define FM801_IRQ_CAPTURE	(1<<9)
1261da177e4SLinus Torvalds #define FM801_IRQ_VOLUME	(1<<14)
1271da177e4SLinus Torvalds #define FM801_IRQ_MPU		(1<<15)
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /* GPIO control register */
1301da177e4SLinus Torvalds #define FM801_GPIO_GP0		(1<<0)	/* read/write */
1311da177e4SLinus Torvalds #define FM801_GPIO_GP1		(1<<1)
1321da177e4SLinus Torvalds #define FM801_GPIO_GP2		(1<<2)
1331da177e4SLinus Torvalds #define FM801_GPIO_GP3		(1<<3)
1341da177e4SLinus Torvalds #define FM801_GPIO_GP(x)	(1<<(0+(x)))
1351da177e4SLinus Torvalds #define FM801_GPIO_GD0		(1<<8)	/* directions: 1 = input, 0 = output*/
1361da177e4SLinus Torvalds #define FM801_GPIO_GD1		(1<<9)
1371da177e4SLinus Torvalds #define FM801_GPIO_GD2		(1<<10)
1381da177e4SLinus Torvalds #define FM801_GPIO_GD3		(1<<11)
1391da177e4SLinus Torvalds #define FM801_GPIO_GD(x)	(1<<(8+(x)))
1401da177e4SLinus Torvalds #define FM801_GPIO_GS0		(1<<12)	/* function select: */
1411da177e4SLinus Torvalds #define FM801_GPIO_GS1		(1<<13)	/*    1 = GPIO */
1421da177e4SLinus Torvalds #define FM801_GPIO_GS2		(1<<14)	/*    0 = other (S/PDIF, VOL) */
1431da177e4SLinus Torvalds #define FM801_GPIO_GS3		(1<<15)
1441da177e4SLinus Torvalds #define FM801_GPIO_GS(x)	(1<<(12+(x)))
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds /*
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds  */
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds typedef struct _snd_fm801 fm801_t;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds struct _snd_fm801 {
1531da177e4SLinus Torvalds 	int irq;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	unsigned long port;	/* I/O port number */
1561da177e4SLinus Torvalds 	unsigned int multichannel: 1,	/* multichannel support */
1571da177e4SLinus Torvalds 		     secondary: 1;	/* secondary codec */
1581da177e4SLinus Torvalds 	unsigned char secondary_addr;	/* address of the secondary codec */
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	unsigned short ply_ctrl; /* playback control */
1611da177e4SLinus Torvalds 	unsigned short cap_ctrl; /* capture control */
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	unsigned long ply_buffer;
1641da177e4SLinus Torvalds 	unsigned int ply_buf;
1651da177e4SLinus Torvalds 	unsigned int ply_count;
1661da177e4SLinus Torvalds 	unsigned int ply_size;
1671da177e4SLinus Torvalds 	unsigned int ply_pos;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	unsigned long cap_buffer;
1701da177e4SLinus Torvalds 	unsigned int cap_buf;
1711da177e4SLinus Torvalds 	unsigned int cap_count;
1721da177e4SLinus Torvalds 	unsigned int cap_size;
1731da177e4SLinus Torvalds 	unsigned int cap_pos;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	ac97_bus_t *ac97_bus;
1761da177e4SLinus Torvalds 	ac97_t *ac97;
1771da177e4SLinus Torvalds 	ac97_t *ac97_sec;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	struct pci_dev *pci;
1801da177e4SLinus Torvalds 	snd_card_t *card;
1811da177e4SLinus Torvalds 	snd_pcm_t *pcm;
1821da177e4SLinus Torvalds 	snd_rawmidi_t *rmidi;
1831da177e4SLinus Torvalds 	snd_pcm_substream_t *playback_substream;
1841da177e4SLinus Torvalds 	snd_pcm_substream_t *capture_substream;
1851da177e4SLinus Torvalds 	unsigned int p_dma_size;
1861da177e4SLinus Torvalds 	unsigned int c_dma_size;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	spinlock_t reg_lock;
1891da177e4SLinus Torvalds 	snd_info_entry_t *proc_entry;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds #ifdef TEA575X_RADIO
1921da177e4SLinus Torvalds 	tea575x_t tea;
1931da177e4SLinus Torvalds #endif
1941da177e4SLinus Torvalds };
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds static struct pci_device_id snd_fm801_ids[] = {
1971da177e4SLinus Torvalds 	{ 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* FM801 */
19826be8659SSergey Vlasov 	{ 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* Gallant Odyssey Sound 4 */
1991da177e4SLinus Torvalds 	{ 0, }
2001da177e4SLinus Torvalds };
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds /*
2051da177e4SLinus Torvalds  *  common I/O routines
2061da177e4SLinus Torvalds  */
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds static int snd_fm801_update_bits(fm801_t *chip, unsigned short reg,
2091da177e4SLinus Torvalds 				 unsigned short mask, unsigned short value)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	int change;
2121da177e4SLinus Torvalds 	unsigned long flags;
2131da177e4SLinus Torvalds 	unsigned short old, new;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
2161da177e4SLinus Torvalds 	old = inw(chip->port + reg);
2171da177e4SLinus Torvalds 	new = (old & ~mask) | value;
2181da177e4SLinus Torvalds 	change = old != new;
2191da177e4SLinus Torvalds 	if (change)
2201da177e4SLinus Torvalds 		outw(new, chip->port + reg);
2211da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
2221da177e4SLinus Torvalds 	return change;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds static void snd_fm801_codec_write(ac97_t *ac97,
2261da177e4SLinus Torvalds 				  unsigned short reg,
2271da177e4SLinus Torvalds 				  unsigned short val)
2281da177e4SLinus Torvalds {
2291da177e4SLinus Torvalds 	fm801_t *chip = ac97->private_data;
2301da177e4SLinus Torvalds 	int idx;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	/*
2331da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
2341da177e4SLinus Torvalds 	 */
2351da177e4SLinus Torvalds 	for (idx = 0; idx < 100; idx++) {
2361da177e4SLinus Torvalds 		if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
2371da177e4SLinus Torvalds 			goto ok1;
2381da177e4SLinus Torvalds 		udelay(10);
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 	snd_printk("AC'97 interface is busy (1)\n");
2411da177e4SLinus Torvalds 	return;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds  ok1:
2441da177e4SLinus Torvalds 	/* write data and address */
2451da177e4SLinus Torvalds 	outw(val, FM801_REG(chip, AC97_DATA));
2461da177e4SLinus Torvalds 	outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
2471da177e4SLinus Torvalds 	/*
2481da177e4SLinus Torvalds 	 *  Wait until the write command is not completed..
2491da177e4SLinus Torvalds          */
2501da177e4SLinus Torvalds 	for (idx = 0; idx < 1000; idx++) {
2511da177e4SLinus Torvalds 		if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
2521da177e4SLinus Torvalds 			return;
2531da177e4SLinus Torvalds 		udelay(10);
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds 	snd_printk("AC'97 interface #%d is busy (2)\n", ac97->num);
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds static unsigned short snd_fm801_codec_read(ac97_t *ac97, unsigned short reg)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	fm801_t *chip = ac97->private_data;
2611da177e4SLinus Torvalds 	int idx;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	/*
2641da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
2651da177e4SLinus Torvalds 	 */
2661da177e4SLinus Torvalds 	for (idx = 0; idx < 100; idx++) {
2671da177e4SLinus Torvalds 		if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
2681da177e4SLinus Torvalds 			goto ok1;
2691da177e4SLinus Torvalds 		udelay(10);
2701da177e4SLinus Torvalds 	}
2711da177e4SLinus Torvalds 	snd_printk("AC'97 interface is busy (1)\n");
2721da177e4SLinus Torvalds 	return 0;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds  ok1:
2751da177e4SLinus Torvalds 	/* read command */
2761da177e4SLinus Torvalds 	outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | (1<<7), FM801_REG(chip, AC97_CMD));
2771da177e4SLinus Torvalds 	for (idx = 0; idx < 100; idx++) {
2781da177e4SLinus Torvalds 		if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
2791da177e4SLinus Torvalds 			goto ok2;
2801da177e4SLinus Torvalds 		udelay(10);
2811da177e4SLinus Torvalds 	}
2821da177e4SLinus Torvalds 	snd_printk("AC'97 interface #%d is busy (2)\n", ac97->num);
2831da177e4SLinus Torvalds 	return 0;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds  ok2:
2861da177e4SLinus Torvalds 	for (idx = 0; idx < 1000; idx++) {
2871da177e4SLinus Torvalds 		if (inw(FM801_REG(chip, AC97_CMD)) & (1<<8))
2881da177e4SLinus Torvalds 			goto ok3;
2891da177e4SLinus Torvalds 		udelay(10);
2901da177e4SLinus Torvalds 	}
2911da177e4SLinus Torvalds 	snd_printk("AC'97 interface #%d is not valid (2)\n", ac97->num);
2921da177e4SLinus Torvalds 	return 0;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds  ok3:
2951da177e4SLinus Torvalds 	return inw(FM801_REG(chip, AC97_DATA));
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds static unsigned int rates[] = {
2991da177e4SLinus Torvalds   5500,  8000,  9600, 11025,
3001da177e4SLinus Torvalds   16000, 19200, 22050, 32000,
3011da177e4SLinus Torvalds   38400, 44100, 48000
3021da177e4SLinus Torvalds };
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
3051da177e4SLinus Torvalds 	.count = ARRAY_SIZE(rates),
3061da177e4SLinus Torvalds 	.list = rates,
3071da177e4SLinus Torvalds 	.mask = 0,
3081da177e4SLinus Torvalds };
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds static unsigned int channels[] = {
3111da177e4SLinus Torvalds   2, 4, 6
3121da177e4SLinus Torvalds };
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds #define CHANNELS sizeof(channels) / sizeof(channels[0])
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds static snd_pcm_hw_constraint_list_t hw_constraints_channels = {
3171da177e4SLinus Torvalds 	.count = CHANNELS,
3181da177e4SLinus Torvalds 	.list = channels,
3191da177e4SLinus Torvalds 	.mask = 0,
3201da177e4SLinus Torvalds };
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds /*
3231da177e4SLinus Torvalds  *  Sample rate routines
3241da177e4SLinus Torvalds  */
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds static unsigned short snd_fm801_rate_bits(unsigned int rate)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	unsigned int idx;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
3311da177e4SLinus Torvalds 		if (rates[idx] == rate)
3321da177e4SLinus Torvalds 			return idx;
3331da177e4SLinus Torvalds 	snd_BUG();
3341da177e4SLinus Torvalds 	return ARRAY_SIZE(rates) - 1;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds /*
3381da177e4SLinus Torvalds  *  PCM part
3391da177e4SLinus Torvalds  */
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds static int snd_fm801_playback_trigger(snd_pcm_substream_t * substream,
3421da177e4SLinus Torvalds 				      int cmd)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	fm801_t *chip = snd_pcm_substream_chip(substream);
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3471da177e4SLinus Torvalds 	switch (cmd) {
3481da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
3491da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_BUF1_LAST |
3501da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
3511da177e4SLinus Torvalds 				     FM801_PAUSE);
3521da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_START |
3531da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
3541da177e4SLinus Torvalds 		break;
3551da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
3561da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
3571da177e4SLinus Torvalds 		break;
3581da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
3591da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_PAUSE;
3601da177e4SLinus Torvalds 		break;
3611da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
3621da177e4SLinus Torvalds 		chip->ply_ctrl &= ~FM801_PAUSE;
3631da177e4SLinus Torvalds 		break;
3641da177e4SLinus Torvalds 	default:
3651da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
3661da177e4SLinus Torvalds 		snd_BUG();
3671da177e4SLinus Torvalds 		return -EINVAL;
3681da177e4SLinus Torvalds 	}
3691da177e4SLinus Torvalds 	outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
3701da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3711da177e4SLinus Torvalds 	return 0;
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds static int snd_fm801_capture_trigger(snd_pcm_substream_t * substream,
3751da177e4SLinus Torvalds 				     int cmd)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	fm801_t *chip = snd_pcm_substream_chip(substream);
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3801da177e4SLinus Torvalds 	switch (cmd) {
3811da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
3821da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_BUF1_LAST |
3831da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
3841da177e4SLinus Torvalds 				     FM801_PAUSE);
3851da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_START |
3861da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
3871da177e4SLinus Torvalds 		break;
3881da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
3891da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
3901da177e4SLinus Torvalds 		break;
3911da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
3921da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_PAUSE;
3931da177e4SLinus Torvalds 		break;
3941da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
3951da177e4SLinus Torvalds 		chip->cap_ctrl &= ~FM801_PAUSE;
3961da177e4SLinus Torvalds 		break;
3971da177e4SLinus Torvalds 	default:
3981da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
3991da177e4SLinus Torvalds 		snd_BUG();
4001da177e4SLinus Torvalds 		return -EINVAL;
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 	outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
4031da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4041da177e4SLinus Torvalds 	return 0;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds static int snd_fm801_hw_params(snd_pcm_substream_t * substream,
4081da177e4SLinus Torvalds 			       snd_pcm_hw_params_t * hw_params)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds static int snd_fm801_hw_free(snd_pcm_substream_t * substream)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds static int snd_fm801_playback_prepare(snd_pcm_substream_t * substream)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds 	fm801_t *chip = snd_pcm_substream_chip(substream);
4211da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
4241da177e4SLinus Torvalds 	chip->ply_count = snd_pcm_lib_period_bytes(substream);
4251da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
4261da177e4SLinus Torvalds 	chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
4271da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK |
4281da177e4SLinus Torvalds 			     FM801_CHANNELS_MASK);
4291da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
4301da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_16BIT;
4311da177e4SLinus Torvalds 	if (runtime->channels > 1) {
4321da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_STEREO;
4331da177e4SLinus Torvalds 		if (runtime->channels == 4)
4341da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_4;
4351da177e4SLinus Torvalds 		else if (runtime->channels == 6)
4361da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_6;
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 	chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
4391da177e4SLinus Torvalds 	chip->ply_buf = 0;
4401da177e4SLinus Torvalds 	outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
4411da177e4SLinus Torvalds 	outw(chip->ply_count - 1, FM801_REG(chip, PLY_COUNT));
4421da177e4SLinus Torvalds 	chip->ply_buffer = runtime->dma_addr;
4431da177e4SLinus Torvalds 	chip->ply_pos = 0;
4441da177e4SLinus Torvalds 	outl(chip->ply_buffer, FM801_REG(chip, PLY_BUF1));
4451da177e4SLinus Torvalds 	outl(chip->ply_buffer + (chip->ply_count % chip->ply_size), FM801_REG(chip, PLY_BUF2));
4461da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
4471da177e4SLinus Torvalds 	return 0;
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds static int snd_fm801_capture_prepare(snd_pcm_substream_t * substream)
4511da177e4SLinus Torvalds {
4521da177e4SLinus Torvalds 	fm801_t *chip = snd_pcm_substream_chip(substream);
4531da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
4561da177e4SLinus Torvalds 	chip->cap_count = snd_pcm_lib_period_bytes(substream);
4571da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
4581da177e4SLinus Torvalds 	chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
4591da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK);
4601da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
4611da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_16BIT;
4621da177e4SLinus Torvalds 	if (runtime->channels > 1)
4631da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_STEREO;
4641da177e4SLinus Torvalds 	chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
4651da177e4SLinus Torvalds 	chip->cap_buf = 0;
4661da177e4SLinus Torvalds 	outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
4671da177e4SLinus Torvalds 	outw(chip->cap_count - 1, FM801_REG(chip, CAP_COUNT));
4681da177e4SLinus Torvalds 	chip->cap_buffer = runtime->dma_addr;
4691da177e4SLinus Torvalds 	chip->cap_pos = 0;
4701da177e4SLinus Torvalds 	outl(chip->cap_buffer, FM801_REG(chip, CAP_BUF1));
4711da177e4SLinus Torvalds 	outl(chip->cap_buffer + (chip->cap_count % chip->cap_size), FM801_REG(chip, CAP_BUF2));
4721da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
4731da177e4SLinus Torvalds 	return 0;
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds static snd_pcm_uframes_t snd_fm801_playback_pointer(snd_pcm_substream_t * substream)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	fm801_t *chip = snd_pcm_substream_chip(substream);
4791da177e4SLinus Torvalds 	size_t ptr;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	if (!(chip->ply_ctrl & FM801_START))
4821da177e4SLinus Torvalds 		return 0;
4831da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
4841da177e4SLinus Torvalds 	ptr = chip->ply_pos + (chip->ply_count - 1) - inw(FM801_REG(chip, PLY_COUNT));
4851da177e4SLinus Torvalds 	if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_PLAYBACK) {
4861da177e4SLinus Torvalds 		ptr += chip->ply_count;
4871da177e4SLinus Torvalds 		ptr %= chip->ply_size;
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4901da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds static snd_pcm_uframes_t snd_fm801_capture_pointer(snd_pcm_substream_t * substream)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	fm801_t *chip = snd_pcm_substream_chip(substream);
4961da177e4SLinus Torvalds 	size_t ptr;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	if (!(chip->cap_ctrl & FM801_START))
4991da177e4SLinus Torvalds 		return 0;
5001da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
5011da177e4SLinus Torvalds 	ptr = chip->cap_pos + (chip->cap_count - 1) - inw(FM801_REG(chip, CAP_COUNT));
5021da177e4SLinus Torvalds 	if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_CAPTURE) {
5031da177e4SLinus Torvalds 		ptr += chip->cap_count;
5041da177e4SLinus Torvalds 		ptr %= chip->cap_size;
5051da177e4SLinus Torvalds 	}
5061da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5071da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id, struct pt_regs *regs)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds 	fm801_t *chip = dev_id;
5131da177e4SLinus Torvalds 	unsigned short status;
5141da177e4SLinus Torvalds 	unsigned int tmp;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	status = inw(FM801_REG(chip, IRQ_STATUS));
5171da177e4SLinus Torvalds 	status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
5181da177e4SLinus Torvalds 	if (! status)
5191da177e4SLinus Torvalds 		return IRQ_NONE;
5201da177e4SLinus Torvalds 	/* ack first */
5211da177e4SLinus Torvalds 	outw(status, FM801_REG(chip, IRQ_STATUS));
5221da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
5231da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5241da177e4SLinus Torvalds 		chip->ply_buf++;
5251da177e4SLinus Torvalds 		chip->ply_pos += chip->ply_count;
5261da177e4SLinus Torvalds 		chip->ply_pos %= chip->ply_size;
5271da177e4SLinus Torvalds 		tmp = chip->ply_pos + chip->ply_count;
5281da177e4SLinus Torvalds 		tmp %= chip->ply_size;
5291da177e4SLinus Torvalds 		outl(chip->ply_buffer + tmp,
5301da177e4SLinus Torvalds 				(chip->ply_buf & 1) ?
5311da177e4SLinus Torvalds 					FM801_REG(chip, PLY_BUF1) :
5321da177e4SLinus Torvalds 					FM801_REG(chip, PLY_BUF2));
5331da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5341da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->playback_substream);
5351da177e4SLinus Torvalds 	}
5361da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
5371da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5381da177e4SLinus Torvalds 		chip->cap_buf++;
5391da177e4SLinus Torvalds 		chip->cap_pos += chip->cap_count;
5401da177e4SLinus Torvalds 		chip->cap_pos %= chip->cap_size;
5411da177e4SLinus Torvalds 		tmp = chip->cap_pos + chip->cap_count;
5421da177e4SLinus Torvalds 		tmp %= chip->cap_size;
5431da177e4SLinus Torvalds 		outl(chip->cap_buffer + tmp,
5441da177e4SLinus Torvalds 				(chip->cap_buf & 1) ?
5451da177e4SLinus Torvalds 					FM801_REG(chip, CAP_BUF1) :
5461da177e4SLinus Torvalds 					FM801_REG(chip, CAP_BUF2));
5471da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5481da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->capture_substream);
5491da177e4SLinus Torvalds 	}
5501da177e4SLinus Torvalds 	if (chip->rmidi && (status & FM801_IRQ_MPU))
5511da177e4SLinus Torvalds 		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
5521da177e4SLinus Torvalds 	if (status & FM801_IRQ_VOLUME)
5531da177e4SLinus Torvalds 		;/* TODO */
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	return IRQ_HANDLED;
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds static snd_pcm_hardware_t snd_fm801_playback =
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
5611da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
5621da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE |
5631da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
5641da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
5651da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
5661da177e4SLinus Torvalds 	.rate_min =		5500,
5671da177e4SLinus Torvalds 	.rate_max =		48000,
5681da177e4SLinus Torvalds 	.channels_min =		1,
5691da177e4SLinus Torvalds 	.channels_max =		2,
5701da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
5711da177e4SLinus Torvalds 	.period_bytes_min =	64,
5721da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
5731da177e4SLinus Torvalds 	.periods_min =		1,
5741da177e4SLinus Torvalds 	.periods_max =		1024,
5751da177e4SLinus Torvalds 	.fifo_size =		0,
5761da177e4SLinus Torvalds };
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds static snd_pcm_hardware_t snd_fm801_capture =
5791da177e4SLinus Torvalds {
5801da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
5811da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
5821da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE |
5831da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
5841da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
5851da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
5861da177e4SLinus Torvalds 	.rate_min =		5500,
5871da177e4SLinus Torvalds 	.rate_max =		48000,
5881da177e4SLinus Torvalds 	.channels_min =		1,
5891da177e4SLinus Torvalds 	.channels_max =		2,
5901da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
5911da177e4SLinus Torvalds 	.period_bytes_min =	64,
5921da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
5931da177e4SLinus Torvalds 	.periods_min =		1,
5941da177e4SLinus Torvalds 	.periods_max =		1024,
5951da177e4SLinus Torvalds 	.fifo_size =		0,
5961da177e4SLinus Torvalds };
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds static int snd_fm801_playback_open(snd_pcm_substream_t * substream)
5991da177e4SLinus Torvalds {
6001da177e4SLinus Torvalds 	fm801_t *chip = snd_pcm_substream_chip(substream);
6011da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
6021da177e4SLinus Torvalds 	int err;
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	chip->playback_substream = substream;
6051da177e4SLinus Torvalds 	runtime->hw = snd_fm801_playback;
6061da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
6071da177e4SLinus Torvalds 	if (chip->multichannel) {
6081da177e4SLinus Torvalds 		runtime->hw.channels_max = 6;
6091da177e4SLinus Torvalds 		snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels);
6101da177e4SLinus Torvalds 	}
6111da177e4SLinus Torvalds 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
6121da177e4SLinus Torvalds 		return err;
6131da177e4SLinus Torvalds 	return 0;
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds static int snd_fm801_capture_open(snd_pcm_substream_t * substream)
6171da177e4SLinus Torvalds {
6181da177e4SLinus Torvalds 	fm801_t *chip = snd_pcm_substream_chip(substream);
6191da177e4SLinus Torvalds 	snd_pcm_runtime_t *runtime = substream->runtime;
6201da177e4SLinus Torvalds 	int err;
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	chip->capture_substream = substream;
6231da177e4SLinus Torvalds 	runtime->hw = snd_fm801_capture;
6241da177e4SLinus Torvalds 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
6251da177e4SLinus Torvalds 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
6261da177e4SLinus Torvalds 		return err;
6271da177e4SLinus Torvalds 	return 0;
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds static int snd_fm801_playback_close(snd_pcm_substream_t * substream)
6311da177e4SLinus Torvalds {
6321da177e4SLinus Torvalds 	fm801_t *chip = snd_pcm_substream_chip(substream);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	chip->playback_substream = NULL;
6351da177e4SLinus Torvalds 	return 0;
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds static int snd_fm801_capture_close(snd_pcm_substream_t * substream)
6391da177e4SLinus Torvalds {
6401da177e4SLinus Torvalds 	fm801_t *chip = snd_pcm_substream_chip(substream);
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	chip->capture_substream = NULL;
6431da177e4SLinus Torvalds 	return 0;
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds static snd_pcm_ops_t snd_fm801_playback_ops = {
6471da177e4SLinus Torvalds 	.open =		snd_fm801_playback_open,
6481da177e4SLinus Torvalds 	.close =	snd_fm801_playback_close,
6491da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
6501da177e4SLinus Torvalds 	.hw_params =	snd_fm801_hw_params,
6511da177e4SLinus Torvalds 	.hw_free =	snd_fm801_hw_free,
6521da177e4SLinus Torvalds 	.prepare =	snd_fm801_playback_prepare,
6531da177e4SLinus Torvalds 	.trigger =	snd_fm801_playback_trigger,
6541da177e4SLinus Torvalds 	.pointer =	snd_fm801_playback_pointer,
6551da177e4SLinus Torvalds };
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds static snd_pcm_ops_t snd_fm801_capture_ops = {
6581da177e4SLinus Torvalds 	.open =		snd_fm801_capture_open,
6591da177e4SLinus Torvalds 	.close =	snd_fm801_capture_close,
6601da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
6611da177e4SLinus Torvalds 	.hw_params =	snd_fm801_hw_params,
6621da177e4SLinus Torvalds 	.hw_free =	snd_fm801_hw_free,
6631da177e4SLinus Torvalds 	.prepare =	snd_fm801_capture_prepare,
6641da177e4SLinus Torvalds 	.trigger =	snd_fm801_capture_trigger,
6651da177e4SLinus Torvalds 	.pointer =	snd_fm801_capture_pointer,
6661da177e4SLinus Torvalds };
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds static void snd_fm801_pcm_free(snd_pcm_t *pcm)
6691da177e4SLinus Torvalds {
6701da177e4SLinus Torvalds 	fm801_t *chip = pcm->private_data;
6711da177e4SLinus Torvalds 	chip->pcm = NULL;
6721da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_free_for_all(pcm);
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds static int __devinit snd_fm801_pcm(fm801_t *chip, int device, snd_pcm_t ** rpcm)
6761da177e4SLinus Torvalds {
6771da177e4SLinus Torvalds 	snd_pcm_t *pcm;
6781da177e4SLinus Torvalds 	int err;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	if (rpcm)
6811da177e4SLinus Torvalds 		*rpcm = NULL;
6821da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm)) < 0)
6831da177e4SLinus Torvalds 		return err;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
6861da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	pcm->private_data = chip;
6891da177e4SLinus Torvalds 	pcm->private_free = snd_fm801_pcm_free;
6901da177e4SLinus Torvalds 	pcm->info_flags = 0;
6911da177e4SLinus Torvalds 	strcpy(pcm->name, "FM801");
6921da177e4SLinus Torvalds 	chip->pcm = pcm;
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
6951da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci),
6961da177e4SLinus Torvalds 					      chip->multichannel ? 128*1024 : 64*1024, 128*1024);
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	if (rpcm)
6991da177e4SLinus Torvalds 		*rpcm = pcm;
7001da177e4SLinus Torvalds 	return 0;
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds /*
7041da177e4SLinus Torvalds  *  TEA5757 radio
7051da177e4SLinus Torvalds  */
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds #ifdef TEA575X_RADIO
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds /* 256PCS GPIO numbers */
7101da177e4SLinus Torvalds #define TEA_256PCS_DATA			1
7111da177e4SLinus Torvalds #define TEA_256PCS_WRITE_ENABLE		2	/* inverted */
7121da177e4SLinus Torvalds #define TEA_256PCS_BUS_CLOCK		3
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds static void snd_fm801_tea575x_256pcs_write(tea575x_t *tea, unsigned int val)
7151da177e4SLinus Torvalds {
7161da177e4SLinus Torvalds 	fm801_t *chip = tea->private_data;
7171da177e4SLinus Torvalds 	unsigned short reg;
7181da177e4SLinus Torvalds 	int i = 25;
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
7211da177e4SLinus Torvalds 	reg = inw(FM801_REG(chip, GPIO_CTRL));
7221da177e4SLinus Torvalds 	/* use GPIO lines and set write enable bit */
7231da177e4SLinus Torvalds 	reg |= FM801_GPIO_GS(TEA_256PCS_DATA) |
7241da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCS_WRITE_ENABLE) |
7251da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCS_BUS_CLOCK);
7261da177e4SLinus Torvalds 	/* all of lines are in the write direction */
7271da177e4SLinus Torvalds 	/* clear data and clock lines */
7281da177e4SLinus Torvalds 	reg &= ~(FM801_GPIO_GD(TEA_256PCS_DATA) |
7291da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_256PCS_WRITE_ENABLE) |
7301da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_256PCS_BUS_CLOCK) |
7311da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_256PCS_DATA) |
7321da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK) |
7331da177e4SLinus Torvalds 		 FM801_GPIO_GP(TEA_256PCS_WRITE_ENABLE));
7341da177e4SLinus Torvalds 	outw(reg, FM801_REG(chip, GPIO_CTRL));
7351da177e4SLinus Torvalds 	udelay(1);
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	while (i--) {
7381da177e4SLinus Torvalds 		if (val & (1 << i))
7391da177e4SLinus Torvalds 			reg |= FM801_GPIO_GP(TEA_256PCS_DATA);
7401da177e4SLinus Torvalds 		else
7411da177e4SLinus Torvalds 			reg &= ~FM801_GPIO_GP(TEA_256PCS_DATA);
7421da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
7431da177e4SLinus Torvalds 		udelay(1);
7441da177e4SLinus Torvalds 		reg |= FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK);
7451da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
7461da177e4SLinus Torvalds 		reg &= ~FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK);
7471da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
7481da177e4SLinus Torvalds 		udelay(1);
7491da177e4SLinus Torvalds 	}
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	/* and reset the write enable bit */
7521da177e4SLinus Torvalds 	reg |= FM801_GPIO_GP(TEA_256PCS_WRITE_ENABLE) |
7531da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_256PCS_DATA);
7541da177e4SLinus Torvalds 	outw(reg, FM801_REG(chip, GPIO_CTRL));
7551da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
7561da177e4SLinus Torvalds }
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds static unsigned int snd_fm801_tea575x_256pcs_read(tea575x_t *tea)
7591da177e4SLinus Torvalds {
7601da177e4SLinus Torvalds 	fm801_t *chip = tea->private_data;
7611da177e4SLinus Torvalds 	unsigned short reg;
7621da177e4SLinus Torvalds 	unsigned int val = 0;
7631da177e4SLinus Torvalds 	int i;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
7661da177e4SLinus Torvalds 	reg = inw(FM801_REG(chip, GPIO_CTRL));
7671da177e4SLinus Torvalds 	/* use GPIO lines, set data direction to input */
7681da177e4SLinus Torvalds 	reg |= FM801_GPIO_GS(TEA_256PCS_DATA) |
7691da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCS_WRITE_ENABLE) |
7701da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCS_BUS_CLOCK) |
7711da177e4SLinus Torvalds 	       FM801_GPIO_GD(TEA_256PCS_DATA) |
7721da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_256PCS_DATA) |
7731da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_256PCS_WRITE_ENABLE);
7741da177e4SLinus Torvalds 	/* all of lines are in the write direction, except data */
7751da177e4SLinus Torvalds 	/* clear data, write enable and clock lines */
7761da177e4SLinus Torvalds 	reg &= ~(FM801_GPIO_GD(TEA_256PCS_WRITE_ENABLE) |
7771da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_256PCS_BUS_CLOCK) |
7781da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK));
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	for (i = 0; i < 24; i++) {
7811da177e4SLinus Torvalds 		reg &= ~FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK);
7821da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
7831da177e4SLinus Torvalds 		udelay(1);
7841da177e4SLinus Torvalds 		reg |= FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK);
7851da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
7861da177e4SLinus Torvalds 		udelay(1);
7871da177e4SLinus Torvalds 		val <<= 1;
7881da177e4SLinus Torvalds 		if (inw(FM801_REG(chip, GPIO_CTRL)) & FM801_GPIO_GP(TEA_256PCS_DATA))
7891da177e4SLinus Torvalds 			val |= 1;
7901da177e4SLinus Torvalds 	}
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	return val;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds /* 256PCPR GPIO numbers */
7981da177e4SLinus Torvalds #define TEA_256PCPR_BUS_CLOCK		0
7991da177e4SLinus Torvalds #define TEA_256PCPR_DATA		1
8001da177e4SLinus Torvalds #define TEA_256PCPR_WRITE_ENABLE	2	/* inverted */
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds static void snd_fm801_tea575x_256pcpr_write(tea575x_t *tea, unsigned int val)
8031da177e4SLinus Torvalds {
8041da177e4SLinus Torvalds 	fm801_t *chip = tea->private_data;
8051da177e4SLinus Torvalds 	unsigned short reg;
8061da177e4SLinus Torvalds 	int i = 25;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
8091da177e4SLinus Torvalds 	reg = inw(FM801_REG(chip, GPIO_CTRL));
8101da177e4SLinus Torvalds 	/* use GPIO lines and set write enable bit */
8111da177e4SLinus Torvalds 	reg |= FM801_GPIO_GS(TEA_256PCPR_DATA) |
8121da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCPR_WRITE_ENABLE) |
8131da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCPR_BUS_CLOCK);
8141da177e4SLinus Torvalds 	/* all of lines are in the write direction */
8151da177e4SLinus Torvalds 	/* clear data and clock lines */
8161da177e4SLinus Torvalds 	reg &= ~(FM801_GPIO_GD(TEA_256PCPR_DATA) |
8171da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_256PCPR_WRITE_ENABLE) |
8181da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_256PCPR_BUS_CLOCK) |
8191da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_256PCPR_DATA) |
8201da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK) |
8211da177e4SLinus Torvalds 		 FM801_GPIO_GP(TEA_256PCPR_WRITE_ENABLE));
8221da177e4SLinus Torvalds 	outw(reg, FM801_REG(chip, GPIO_CTRL));
8231da177e4SLinus Torvalds 	udelay(1);
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	while (i--) {
8261da177e4SLinus Torvalds 		if (val & (1 << i))
8271da177e4SLinus Torvalds 			reg |= FM801_GPIO_GP(TEA_256PCPR_DATA);
8281da177e4SLinus Torvalds 		else
8291da177e4SLinus Torvalds 			reg &= ~FM801_GPIO_GP(TEA_256PCPR_DATA);
8301da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
8311da177e4SLinus Torvalds 		udelay(1);
8321da177e4SLinus Torvalds 		reg |= FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK);
8331da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
8341da177e4SLinus Torvalds 		reg &= ~FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK);
8351da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
8361da177e4SLinus Torvalds 		udelay(1);
8371da177e4SLinus Torvalds 	}
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	/* and reset the write enable bit */
8401da177e4SLinus Torvalds 	reg |= FM801_GPIO_GP(TEA_256PCPR_WRITE_ENABLE) |
8411da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_256PCPR_DATA);
8421da177e4SLinus Torvalds 	outw(reg, FM801_REG(chip, GPIO_CTRL));
8431da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds static unsigned int snd_fm801_tea575x_256pcpr_read(tea575x_t *tea)
8471da177e4SLinus Torvalds {
8481da177e4SLinus Torvalds 	fm801_t *chip = tea->private_data;
8491da177e4SLinus Torvalds 	unsigned short reg;
8501da177e4SLinus Torvalds 	unsigned int val = 0;
8511da177e4SLinus Torvalds 	int i;
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
8541da177e4SLinus Torvalds 	reg = inw(FM801_REG(chip, GPIO_CTRL));
8551da177e4SLinus Torvalds 	/* use GPIO lines, set data direction to input */
8561da177e4SLinus Torvalds 	reg |= FM801_GPIO_GS(TEA_256PCPR_DATA) |
8571da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCPR_WRITE_ENABLE) |
8581da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCPR_BUS_CLOCK) |
8591da177e4SLinus Torvalds 	       FM801_GPIO_GD(TEA_256PCPR_DATA) |
8601da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_256PCPR_DATA) |
8611da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_256PCPR_WRITE_ENABLE);
8621da177e4SLinus Torvalds 	/* all of lines are in the write direction, except data */
8631da177e4SLinus Torvalds 	/* clear data, write enable and clock lines */
8641da177e4SLinus Torvalds 	reg &= ~(FM801_GPIO_GD(TEA_256PCPR_WRITE_ENABLE) |
8651da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_256PCPR_BUS_CLOCK) |
8661da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK));
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	for (i = 0; i < 24; i++) {
8691da177e4SLinus Torvalds 		reg &= ~FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK);
8701da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
8711da177e4SLinus Torvalds 		udelay(1);
8721da177e4SLinus Torvalds 		reg |= FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK);
8731da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
8741da177e4SLinus Torvalds 		udelay(1);
8751da177e4SLinus Torvalds 		val <<= 1;
8761da177e4SLinus Torvalds 		if (inw(FM801_REG(chip, GPIO_CTRL)) & FM801_GPIO_GP(TEA_256PCPR_DATA))
8771da177e4SLinus Torvalds 			val |= 1;
8781da177e4SLinus Torvalds 	}
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	return val;
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds /* 64PCR GPIO numbers */
8861da177e4SLinus Torvalds #define TEA_64PCR_BUS_CLOCK		0
8871da177e4SLinus Torvalds #define TEA_64PCR_WRITE_ENABLE		1	/* inverted */
8881da177e4SLinus Torvalds #define TEA_64PCR_DATA			2
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds static void snd_fm801_tea575x_64pcr_write(tea575x_t *tea, unsigned int val)
8911da177e4SLinus Torvalds {
8921da177e4SLinus Torvalds 	fm801_t *chip = tea->private_data;
8931da177e4SLinus Torvalds 	unsigned short reg;
8941da177e4SLinus Torvalds 	int i = 25;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
8971da177e4SLinus Torvalds 	reg = inw(FM801_REG(chip, GPIO_CTRL));
8981da177e4SLinus Torvalds 	/* use GPIO lines and set write enable bit */
8991da177e4SLinus Torvalds 	reg |= FM801_GPIO_GS(TEA_64PCR_DATA) |
9001da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_64PCR_WRITE_ENABLE) |
9011da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_64PCR_BUS_CLOCK);
9021da177e4SLinus Torvalds 	/* all of lines are in the write direction */
9031da177e4SLinus Torvalds 	/* clear data and clock lines */
9041da177e4SLinus Torvalds 	reg &= ~(FM801_GPIO_GD(TEA_64PCR_DATA) |
9051da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_64PCR_WRITE_ENABLE) |
9061da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_64PCR_BUS_CLOCK) |
9071da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_64PCR_DATA) |
9081da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK) |
9091da177e4SLinus Torvalds 		 FM801_GPIO_GP(TEA_64PCR_WRITE_ENABLE));
9101da177e4SLinus Torvalds 	outw(reg, FM801_REG(chip, GPIO_CTRL));
9111da177e4SLinus Torvalds 	udelay(1);
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	while (i--) {
9141da177e4SLinus Torvalds 		if (val & (1 << i))
9151da177e4SLinus Torvalds 			reg |= FM801_GPIO_GP(TEA_64PCR_DATA);
9161da177e4SLinus Torvalds 		else
9171da177e4SLinus Torvalds 			reg &= ~FM801_GPIO_GP(TEA_64PCR_DATA);
9181da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
9191da177e4SLinus Torvalds 		udelay(1);
9201da177e4SLinus Torvalds 		reg |= FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK);
9211da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
9221da177e4SLinus Torvalds 		reg &= ~FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK);
9231da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
9241da177e4SLinus Torvalds 		udelay(1);
9251da177e4SLinus Torvalds 	}
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	/* and reset the write enable bit */
9281da177e4SLinus Torvalds 	reg |= FM801_GPIO_GP(TEA_64PCR_WRITE_ENABLE) |
9291da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_64PCR_DATA);
9301da177e4SLinus Torvalds 	outw(reg, FM801_REG(chip, GPIO_CTRL));
9311da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9321da177e4SLinus Torvalds }
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds static unsigned int snd_fm801_tea575x_64pcr_read(tea575x_t *tea)
9351da177e4SLinus Torvalds {
9361da177e4SLinus Torvalds 	fm801_t *chip = tea->private_data;
9371da177e4SLinus Torvalds 	unsigned short reg;
9381da177e4SLinus Torvalds 	unsigned int val = 0;
9391da177e4SLinus Torvalds 	int i;
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9421da177e4SLinus Torvalds 	reg = inw(FM801_REG(chip, GPIO_CTRL));
9431da177e4SLinus Torvalds 	/* use GPIO lines, set data direction to input */
9441da177e4SLinus Torvalds 	reg |= FM801_GPIO_GS(TEA_64PCR_DATA) |
9451da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_64PCR_WRITE_ENABLE) |
9461da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_64PCR_BUS_CLOCK) |
9471da177e4SLinus Torvalds 	       FM801_GPIO_GD(TEA_64PCR_DATA) |
9481da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_64PCR_DATA) |
9491da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_64PCR_WRITE_ENABLE);
9501da177e4SLinus Torvalds 	/* all of lines are in the write direction, except data */
9511da177e4SLinus Torvalds 	/* clear data, write enable and clock lines */
9521da177e4SLinus Torvalds 	reg &= ~(FM801_GPIO_GD(TEA_64PCR_WRITE_ENABLE) |
9531da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_64PCR_BUS_CLOCK) |
9541da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK));
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	for (i = 0; i < 24; i++) {
9571da177e4SLinus Torvalds 		reg &= ~FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK);
9581da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
9591da177e4SLinus Torvalds 		udelay(1);
9601da177e4SLinus Torvalds 		reg |= FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK);
9611da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
9621da177e4SLinus Torvalds 		udelay(1);
9631da177e4SLinus Torvalds 		val <<= 1;
9641da177e4SLinus Torvalds 		if (inw(FM801_REG(chip, GPIO_CTRL)) & FM801_GPIO_GP(TEA_64PCR_DATA))
9651da177e4SLinus Torvalds 			val |= 1;
9661da177e4SLinus Torvalds 	}
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	return val;
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds static struct snd_tea575x_ops snd_fm801_tea_ops[3] = {
9741da177e4SLinus Torvalds 	{
9751da177e4SLinus Torvalds 		/* 1 = MediaForte 256-PCS */
9761da177e4SLinus Torvalds 		.write = snd_fm801_tea575x_256pcs_write,
9771da177e4SLinus Torvalds 		.read = snd_fm801_tea575x_256pcs_read,
9781da177e4SLinus Torvalds 	},
9791da177e4SLinus Torvalds 	{
9801da177e4SLinus Torvalds 		/* 2 = MediaForte 256-PCPR */
9811da177e4SLinus Torvalds 		.write = snd_fm801_tea575x_256pcpr_write,
9821da177e4SLinus Torvalds 		.read = snd_fm801_tea575x_256pcpr_read,
9831da177e4SLinus Torvalds 	},
9841da177e4SLinus Torvalds 	{
9851da177e4SLinus Torvalds 		/* 3 = MediaForte 64-PCR */
9861da177e4SLinus Torvalds 		.write = snd_fm801_tea575x_64pcr_write,
9871da177e4SLinus Torvalds 		.read = snd_fm801_tea575x_64pcr_read,
9881da177e4SLinus Torvalds 	}
9891da177e4SLinus Torvalds };
9901da177e4SLinus Torvalds #endif
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds /*
9931da177e4SLinus Torvalds  *  Mixer routines
9941da177e4SLinus Torvalds  */
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds #define FM801_SINGLE(xname, reg, shift, mask, invert) \
9971da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
9981da177e4SLinus Torvalds   .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
9991da177e4SLinus Torvalds   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds static int snd_fm801_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
10021da177e4SLinus Torvalds {
10031da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
10061da177e4SLinus Torvalds 	uinfo->count = 1;
10071da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
10081da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
10091da177e4SLinus Torvalds 	return 0;
10101da177e4SLinus Torvalds }
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds static int snd_fm801_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
10131da177e4SLinus Torvalds {
10141da177e4SLinus Torvalds 	fm801_t *chip = snd_kcontrol_chip(kcontrol);
10151da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
10161da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
10171da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
10181da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift) & mask;
10211da177e4SLinus Torvalds 	if (invert)
10221da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
10231da177e4SLinus Torvalds 	return 0;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds static int snd_fm801_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
10271da177e4SLinus Torvalds {
10281da177e4SLinus Torvalds 	fm801_t *chip = snd_kcontrol_chip(kcontrol);
10291da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
10301da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
10311da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
10321da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
10331da177e4SLinus Torvalds 	unsigned short val;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
10361da177e4SLinus Torvalds 	if (invert)
10371da177e4SLinus Torvalds 		val = mask - val;
10381da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds #define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
10421da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
10431da177e4SLinus Torvalds   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
10441da177e4SLinus Torvalds   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds static int snd_fm801_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
10471da177e4SLinus Torvalds {
10481da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
10511da177e4SLinus Torvalds 	uinfo->count = 2;
10521da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
10531da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
10541da177e4SLinus Torvalds 	return 0;
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds static int snd_fm801_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
10581da177e4SLinus Torvalds {
10591da177e4SLinus Torvalds 	fm801_t *chip = snd_kcontrol_chip(kcontrol);
10601da177e4SLinus Torvalds         int reg = kcontrol->private_value & 0xff;
10611da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
10621da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
10631da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
10641da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10671da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift_left) & mask;
10681da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = (inw(chip->port + reg) >> shift_right) & mask;
10691da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10701da177e4SLinus Torvalds 	if (invert) {
10711da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
10721da177e4SLinus Torvalds 		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
10731da177e4SLinus Torvalds 	}
10741da177e4SLinus Torvalds 	return 0;
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds static int snd_fm801_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
10781da177e4SLinus Torvalds {
10791da177e4SLinus Torvalds 	fm801_t *chip = snd_kcontrol_chip(kcontrol);
10801da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
10811da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
10821da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
10831da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
10841da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
10851da177e4SLinus Torvalds 	unsigned short val1, val2;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
10881da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
10891da177e4SLinus Torvalds 	if (invert) {
10901da177e4SLinus Torvalds 		val1 = mask - val1;
10911da177e4SLinus Torvalds 		val2 = mask - val2;
10921da177e4SLinus Torvalds 	}
10931da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg,
10941da177e4SLinus Torvalds 				     (mask << shift_left) | (mask << shift_right),
10951da177e4SLinus Torvalds 				     (val1 << shift_left ) | (val2 << shift_right));
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds static int snd_fm801_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
10991da177e4SLinus Torvalds {
11001da177e4SLinus Torvalds 	static char *texts[5] = {
11011da177e4SLinus Torvalds 		"AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
11021da177e4SLinus Torvalds 	};
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
11051da177e4SLinus Torvalds 	uinfo->count = 1;
11061da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 5;
11071da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item > 4)
11081da177e4SLinus Torvalds 		uinfo->value.enumerated.item = 4;
11091da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
11101da177e4SLinus Torvalds 	return 0;
11111da177e4SLinus Torvalds }
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds static int snd_fm801_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
11141da177e4SLinus Torvalds {
11151da177e4SLinus Torvalds 	fm801_t *chip = snd_kcontrol_chip(kcontrol);
11161da177e4SLinus Torvalds         unsigned short val;
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	val = inw(FM801_REG(chip, REC_SRC)) & 7;
11191da177e4SLinus Torvalds 	if (val > 4)
11201da177e4SLinus Torvalds 		val = 4;
11211da177e4SLinus Torvalds         ucontrol->value.enumerated.item[0] = val;
11221da177e4SLinus Torvalds         return 0;
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds static int snd_fm801_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
11261da177e4SLinus Torvalds {
11271da177e4SLinus Torvalds 	fm801_t *chip = snd_kcontrol_chip(kcontrol);
11281da177e4SLinus Torvalds         unsigned short val;
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds         if ((val = ucontrol->value.enumerated.item[0]) > 4)
11311da177e4SLinus Torvalds                 return -EINVAL;
11321da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
11331da177e4SLinus Torvalds }
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds #define FM801_CONTROLS (sizeof(snd_fm801_controls)/sizeof(snd_kcontrol_new_t))
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds static snd_kcontrol_new_t snd_fm801_controls[] __devinitdata = {
11381da177e4SLinus Torvalds FM801_DOUBLE("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1),
11391da177e4SLinus Torvalds FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
11401da177e4SLinus Torvalds FM801_DOUBLE("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1),
11411da177e4SLinus Torvalds FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
11421da177e4SLinus Torvalds FM801_DOUBLE("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1),
11431da177e4SLinus Torvalds FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
11441da177e4SLinus Torvalds {
11451da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
11461da177e4SLinus Torvalds 	.name = "Digital Capture Source",
11471da177e4SLinus Torvalds 	.info = snd_fm801_info_mux,
11481da177e4SLinus Torvalds 	.get = snd_fm801_get_mux,
11491da177e4SLinus Torvalds 	.put = snd_fm801_put_mux,
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds };
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds #define FM801_CONTROLS_MULTI (sizeof(snd_fm801_controls_multi)/sizeof(snd_kcontrol_new_t))
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds static snd_kcontrol_new_t snd_fm801_controls_multi[] __devinitdata = {
11561da177e4SLinus Torvalds FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
11571da177e4SLinus Torvalds FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
11581da177e4SLinus Torvalds FM801_SINGLE("IEC958 Capture Switch", FM801_I2S_MODE, 8, 1, 0),
11591da177e4SLinus Torvalds FM801_SINGLE("IEC958 Raw Data Playback Switch", FM801_I2S_MODE, 9, 1, 0),
11601da177e4SLinus Torvalds FM801_SINGLE("IEC958 Raw Data Capture Switch", FM801_I2S_MODE, 10, 1, 0),
11611da177e4SLinus Torvalds FM801_SINGLE("IEC958 Playback Switch", FM801_GEN_CTRL, 2, 1, 0),
11621da177e4SLinus Torvalds };
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds static void snd_fm801_mixer_free_ac97_bus(ac97_bus_t *bus)
11651da177e4SLinus Torvalds {
11661da177e4SLinus Torvalds 	fm801_t *chip = bus->private_data;
11671da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
11681da177e4SLinus Torvalds }
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds static void snd_fm801_mixer_free_ac97(ac97_t *ac97)
11711da177e4SLinus Torvalds {
11721da177e4SLinus Torvalds 	fm801_t *chip = ac97->private_data;
11731da177e4SLinus Torvalds 	if (ac97->num == 0) {
11741da177e4SLinus Torvalds 		chip->ac97 = NULL;
11751da177e4SLinus Torvalds 	} else {
11761da177e4SLinus Torvalds 		chip->ac97_sec = NULL;
11771da177e4SLinus Torvalds 	}
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds static int __devinit snd_fm801_mixer(fm801_t *chip)
11811da177e4SLinus Torvalds {
11821da177e4SLinus Torvalds 	ac97_template_t ac97;
11831da177e4SLinus Torvalds 	unsigned int i;
11841da177e4SLinus Torvalds 	int err;
11851da177e4SLinus Torvalds 	static ac97_bus_ops_t ops = {
11861da177e4SLinus Torvalds 		.write = snd_fm801_codec_write,
11871da177e4SLinus Torvalds 		.read = snd_fm801_codec_read,
11881da177e4SLinus Torvalds 	};
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
11911da177e4SLinus Torvalds 		return err;
11921da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_fm801_mixer_free_ac97_bus;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
11951da177e4SLinus Torvalds 	ac97.private_data = chip;
11961da177e4SLinus Torvalds 	ac97.private_free = snd_fm801_mixer_free_ac97;
11971da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
11981da177e4SLinus Torvalds 		return err;
11991da177e4SLinus Torvalds 	if (chip->secondary) {
12001da177e4SLinus Torvalds 		ac97.num = 1;
12011da177e4SLinus Torvalds 		ac97.addr = chip->secondary_addr;
12021da177e4SLinus Torvalds 		if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec)) < 0)
12031da177e4SLinus Torvalds 			return err;
12041da177e4SLinus Torvalds 	}
12051da177e4SLinus Torvalds 	for (i = 0; i < FM801_CONTROLS; i++)
12061da177e4SLinus Torvalds 		snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls[i], chip));
12071da177e4SLinus Torvalds 	if (chip->multichannel) {
12081da177e4SLinus Torvalds 		for (i = 0; i < FM801_CONTROLS_MULTI; i++)
12091da177e4SLinus Torvalds 			snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
12101da177e4SLinus Torvalds 	}
12111da177e4SLinus Torvalds 	return 0;
12121da177e4SLinus Torvalds }
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds /*
12151da177e4SLinus Torvalds  *  initialization routines
12161da177e4SLinus Torvalds  */
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds static int snd_fm801_free(fm801_t *chip)
12191da177e4SLinus Torvalds {
12201da177e4SLinus Torvalds 	unsigned short cmdw;
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	if (chip->irq < 0)
12231da177e4SLinus Torvalds 		goto __end_hw;
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	/* interrupt setup - mask everything */
12261da177e4SLinus Torvalds 	cmdw = inw(FM801_REG(chip, IRQ_MASK));
12271da177e4SLinus Torvalds 	cmdw |= 0x00c3;
12281da177e4SLinus Torvalds 	outw(cmdw, FM801_REG(chip, IRQ_MASK));
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds       __end_hw:
12311da177e4SLinus Torvalds #ifdef TEA575X_RADIO
12321da177e4SLinus Torvalds 	snd_tea575x_exit(&chip->tea);
12331da177e4SLinus Torvalds #endif
12341da177e4SLinus Torvalds 	if (chip->irq >= 0)
12351da177e4SLinus Torvalds 		free_irq(chip->irq, (void *)chip);
12361da177e4SLinus Torvalds 	pci_release_regions(chip->pci);
12371da177e4SLinus Torvalds 	pci_disable_device(chip->pci);
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	kfree(chip);
12401da177e4SLinus Torvalds 	return 0;
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds static int snd_fm801_dev_free(snd_device_t *device)
12441da177e4SLinus Torvalds {
12451da177e4SLinus Torvalds 	fm801_t *chip = device->device_data;
12461da177e4SLinus Torvalds 	return snd_fm801_free(chip);
12471da177e4SLinus Torvalds }
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds static int __devinit snd_fm801_create(snd_card_t * card,
12501da177e4SLinus Torvalds 				      struct pci_dev * pci,
12511da177e4SLinus Torvalds 				      int tea575x_tuner,
12521da177e4SLinus Torvalds 				      fm801_t ** rchip)
12531da177e4SLinus Torvalds {
12541da177e4SLinus Torvalds 	fm801_t *chip;
12551da177e4SLinus Torvalds 	unsigned char rev, id;
12561da177e4SLinus Torvalds 	unsigned short cmdw;
12571da177e4SLinus Torvalds 	unsigned long timeout;
12581da177e4SLinus Torvalds 	int err;
12591da177e4SLinus Torvalds 	static snd_device_ops_t ops = {
12601da177e4SLinus Torvalds 		.dev_free =	snd_fm801_dev_free,
12611da177e4SLinus Torvalds 	};
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	*rchip = NULL;
12641da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
12651da177e4SLinus Torvalds 		return err;
12661da177e4SLinus Torvalds 	chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
12671da177e4SLinus Torvalds 	if (chip == NULL) {
12681da177e4SLinus Torvalds 		pci_disable_device(pci);
12691da177e4SLinus Torvalds 		return -ENOMEM;
12701da177e4SLinus Torvalds 	}
12711da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
12721da177e4SLinus Torvalds 	chip->card = card;
12731da177e4SLinus Torvalds 	chip->pci = pci;
12741da177e4SLinus Torvalds 	chip->irq = -1;
12751da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, "FM801")) < 0) {
12761da177e4SLinus Torvalds 		kfree(chip);
12771da177e4SLinus Torvalds 		pci_disable_device(pci);
12781da177e4SLinus Torvalds 		return err;
12791da177e4SLinus Torvalds 	}
12801da177e4SLinus Torvalds 	chip->port = pci_resource_start(pci, 0);
12811da177e4SLinus Torvalds 	if (request_irq(pci->irq, snd_fm801_interrupt, SA_INTERRUPT|SA_SHIRQ, "FM801", (void *)chip)) {
12821da177e4SLinus Torvalds 		snd_printk("unable to grab IRQ %d\n", chip->irq);
12831da177e4SLinus Torvalds 		snd_fm801_free(chip);
12841da177e4SLinus Torvalds 		return -EBUSY;
12851da177e4SLinus Torvalds 	}
12861da177e4SLinus Torvalds 	chip->irq = pci->irq;
12871da177e4SLinus Torvalds 	pci_set_master(pci);
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 	pci_read_config_byte(pci, PCI_REVISION_ID, &rev);
12901da177e4SLinus Torvalds 	if (rev >= 0xb1)	/* FM801-AU */
12911da177e4SLinus Torvalds 		chip->multichannel = 1;
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	/* codec cold reset + AC'97 warm reset */
12941da177e4SLinus Torvalds 	outw((1<<5)|(1<<6), FM801_REG(chip, CODEC_CTRL));
12951da177e4SLinus Torvalds 	inw(FM801_REG(chip, CODEC_CTRL)); /* flush posting data */
12961da177e4SLinus Torvalds 	udelay(100);
12971da177e4SLinus Torvalds 	outw(0, FM801_REG(chip, CODEC_CTRL));
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 	timeout = (jiffies + (3 * HZ) / 4) + 1;		/* min 750ms */
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	outw((1<<7) | (0 << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
13021da177e4SLinus Torvalds 	udelay(5);
13031da177e4SLinus Torvalds 	do {
13041da177e4SLinus Torvalds 		if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8))
13051da177e4SLinus Torvalds 			goto __ac97_secondary;
13061da177e4SLinus Torvalds 		set_current_state(TASK_UNINTERRUPTIBLE);
13071da177e4SLinus Torvalds 		schedule_timeout(1);
13081da177e4SLinus Torvalds 	} while (time_after(timeout, jiffies));
13091da177e4SLinus Torvalds 	snd_printk("Primary AC'97 codec not found\n");
13101da177e4SLinus Torvalds 	snd_fm801_free(chip);
13111da177e4SLinus Torvalds 	return -EIO;
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds       __ac97_secondary:
13141da177e4SLinus Torvalds       	if (!chip->multichannel)	/* lookup is not required */
13151da177e4SLinus Torvalds       		goto __ac97_ok;
13161da177e4SLinus Torvalds 	for (id = 3; id > 0; id--) {	/* my card has the secondary codec */
13171da177e4SLinus Torvalds 					/* at address #3, so the loop is inverted */
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 		timeout = jiffies + HZ / 20;
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 		outw((1<<7) | (id << FM801_AC97_ADDR_SHIFT) | AC97_VENDOR_ID1, FM801_REG(chip, AC97_CMD));
13221da177e4SLinus Torvalds 		udelay(5);
13231da177e4SLinus Torvalds 		do {
13241da177e4SLinus Torvalds 			if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8)) {
13251da177e4SLinus Torvalds 				cmdw = inw(FM801_REG(chip, AC97_DATA));
13261da177e4SLinus Torvalds 				if (cmdw != 0xffff && cmdw != 0) {
13271da177e4SLinus Torvalds 					chip->secondary = 1;
13281da177e4SLinus Torvalds 					chip->secondary_addr = id;
13291da177e4SLinus Torvalds 					goto __ac97_ok;
13301da177e4SLinus Torvalds 				}
13311da177e4SLinus Torvalds 			}
13321da177e4SLinus Torvalds 			set_current_state(TASK_UNINTERRUPTIBLE);
13331da177e4SLinus Torvalds 			schedule_timeout(1);
13341da177e4SLinus Torvalds 		} while (time_after(timeout, jiffies));
13351da177e4SLinus Torvalds 	}
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 	/* the recovery phase, it seems that probing for non-existing codec might */
13381da177e4SLinus Torvalds 	/* cause timeout problems */
13391da177e4SLinus Torvalds 	timeout = (jiffies + (3 * HZ) / 4) + 1;		/* min 750ms */
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	outw((1<<7) | (0 << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
13421da177e4SLinus Torvalds 	udelay(5);
13431da177e4SLinus Torvalds 	do {
13441da177e4SLinus Torvalds 		if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8))
13451da177e4SLinus Torvalds 			goto __ac97_ok;
13461da177e4SLinus Torvalds 		set_current_state(TASK_UNINTERRUPTIBLE);
13471da177e4SLinus Torvalds 		schedule_timeout(1);
13481da177e4SLinus Torvalds 	} while (time_after(timeout, jiffies));
13491da177e4SLinus Torvalds 	snd_printk("Primary AC'97 codec not responding\n");
13501da177e4SLinus Torvalds 	snd_fm801_free(chip);
13511da177e4SLinus Torvalds 	return -EIO;
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds       __ac97_ok:
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	/* init volume */
13561da177e4SLinus Torvalds 	outw(0x0808, FM801_REG(chip, PCM_VOL));
13571da177e4SLinus Torvalds 	outw(0x9f1f, FM801_REG(chip, FM_VOL));
13581da177e4SLinus Torvalds 	outw(0x8808, FM801_REG(chip, I2S_VOL));
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	/* I2S control - I2S mode */
13611da177e4SLinus Torvalds 	outw(0x0003, FM801_REG(chip, I2S_MODE));
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	/* interrupt setup - unmask MPU, PLAYBACK & CAPTURE */
13641da177e4SLinus Torvalds 	cmdw = inw(FM801_REG(chip, IRQ_MASK));
13651da177e4SLinus Torvalds 	cmdw &= ~0x0083;
13661da177e4SLinus Torvalds 	outw(cmdw, FM801_REG(chip, IRQ_MASK));
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	/* interrupt clear */
13691da177e4SLinus Torvalds 	outw(FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU, FM801_REG(chip, IRQ_STATUS));
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
13721da177e4SLinus Torvalds 		snd_fm801_free(chip);
13731da177e4SLinus Torvalds 		return err;
13741da177e4SLinus Torvalds 	}
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	snd_card_set_dev(card, &pci->dev);
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds #ifdef TEA575X_RADIO
13791da177e4SLinus Torvalds 	if (tea575x_tuner > 0 && (tea575x_tuner & 0xffff) < 4) {
13801da177e4SLinus Torvalds 		chip->tea.dev_nr = tea575x_tuner >> 16;
13811da177e4SLinus Torvalds 		chip->tea.card = card;
13821da177e4SLinus Torvalds 		chip->tea.freq_fixup = 10700;
13831da177e4SLinus Torvalds 		chip->tea.private_data = chip;
13841da177e4SLinus Torvalds 		chip->tea.ops = &snd_fm801_tea_ops[(tea575x_tuner & 0xffff) - 1];
13851da177e4SLinus Torvalds 		snd_tea575x_init(&chip->tea);
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds #endif
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 	*rchip = chip;
13901da177e4SLinus Torvalds 	return 0;
13911da177e4SLinus Torvalds }
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds static int __devinit snd_card_fm801_probe(struct pci_dev *pci,
13941da177e4SLinus Torvalds 					  const struct pci_device_id *pci_id)
13951da177e4SLinus Torvalds {
13961da177e4SLinus Torvalds 	static int dev;
13971da177e4SLinus Torvalds 	snd_card_t *card;
13981da177e4SLinus Torvalds 	fm801_t *chip;
13991da177e4SLinus Torvalds 	opl3_t *opl3;
14001da177e4SLinus Torvalds 	int err;
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds         if (dev >= SNDRV_CARDS)
14031da177e4SLinus Torvalds                 return -ENODEV;
14041da177e4SLinus Torvalds 	if (!enable[dev]) {
14051da177e4SLinus Torvalds 		dev++;
14061da177e4SLinus Torvalds 		return -ENOENT;
14071da177e4SLinus Torvalds 	}
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 	card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
14101da177e4SLinus Torvalds 	if (card == NULL)
14111da177e4SLinus Torvalds 		return -ENOMEM;
14121da177e4SLinus Torvalds 	if ((err = snd_fm801_create(card, pci, tea575x_tuner[dev], &chip)) < 0) {
14131da177e4SLinus Torvalds 		snd_card_free(card);
14141da177e4SLinus Torvalds 		return err;
14151da177e4SLinus Torvalds 	}
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds 	strcpy(card->driver, "FM801");
14181da177e4SLinus Torvalds 	strcpy(card->shortname, "ForteMedia FM801-");
14191da177e4SLinus Torvalds 	strcat(card->shortname, chip->multichannel ? "AU" : "AS");
14201da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %i",
14211da177e4SLinus Torvalds 		card->shortname, chip->port, chip->irq);
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 	if ((err = snd_fm801_pcm(chip, 0, NULL)) < 0) {
14241da177e4SLinus Torvalds 		snd_card_free(card);
14251da177e4SLinus Torvalds 		return err;
14261da177e4SLinus Torvalds 	}
14271da177e4SLinus Torvalds 	if ((err = snd_fm801_mixer(chip)) < 0) {
14281da177e4SLinus Torvalds 		snd_card_free(card);
14291da177e4SLinus Torvalds 		return err;
14301da177e4SLinus Torvalds 	}
14311da177e4SLinus Torvalds 	if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
14321da177e4SLinus Torvalds 				       FM801_REG(chip, MPU401_DATA), 1,
14331da177e4SLinus Torvalds 				       chip->irq, 0, &chip->rmidi)) < 0) {
14341da177e4SLinus Torvalds 		snd_card_free(card);
14351da177e4SLinus Torvalds 		return err;
14361da177e4SLinus Torvalds 	}
14371da177e4SLinus Torvalds 	if ((err = snd_opl3_create(card, FM801_REG(chip, OPL3_BANK0),
14381da177e4SLinus Torvalds 				   FM801_REG(chip, OPL3_BANK1),
14391da177e4SLinus Torvalds 				   OPL3_HW_OPL3_FM801, 1, &opl3)) < 0) {
14401da177e4SLinus Torvalds 		snd_card_free(card);
14411da177e4SLinus Torvalds 		return err;
14421da177e4SLinus Torvalds 	}
14431da177e4SLinus Torvalds 	if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
14441da177e4SLinus Torvalds 		snd_card_free(card);
14451da177e4SLinus Torvalds 		return err;
14461da177e4SLinus Torvalds 	}
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
14491da177e4SLinus Torvalds 		snd_card_free(card);
14501da177e4SLinus Torvalds 		return err;
14511da177e4SLinus Torvalds 	}
14521da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
14531da177e4SLinus Torvalds 	dev++;
14541da177e4SLinus Torvalds 	return 0;
14551da177e4SLinus Torvalds }
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds static void __devexit snd_card_fm801_remove(struct pci_dev *pci)
14581da177e4SLinus Torvalds {
14591da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
14601da177e4SLinus Torvalds 	pci_set_drvdata(pci, NULL);
14611da177e4SLinus Torvalds }
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds static struct pci_driver driver = {
14641da177e4SLinus Torvalds 	.name = "FM801",
14651da177e4SLinus Torvalds 	.id_table = snd_fm801_ids,
14661da177e4SLinus Torvalds 	.probe = snd_card_fm801_probe,
14671da177e4SLinus Torvalds 	.remove = __devexit_p(snd_card_fm801_remove),
14681da177e4SLinus Torvalds };
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds static int __init alsa_card_fm801_init(void)
14711da177e4SLinus Torvalds {
1472*01d25d46STakashi Iwai 	return pci_register_driver(&driver);
14731da177e4SLinus Torvalds }
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds static void __exit alsa_card_fm801_exit(void)
14761da177e4SLinus Torvalds {
14771da177e4SLinus Torvalds 	pci_unregister_driver(&driver);
14781da177e4SLinus Torvalds }
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds module_init(alsa_card_fm801_init)
14811da177e4SLinus Torvalds module_exit(alsa_card_fm801_exit)
1482