xref: /linux/sound/pci/fm801.c (revision a67ff6a54095e27093ea501fb143fefe51a536c2)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  The driver for the ForteMedia FM801 based soundcards
3c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
41da177e4SLinus Torvalds  *
5e0a5d82aSAndy Shevchenko  *  Support FM only card by Andy Shevchenko <andy@smile.org.ua>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
81da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
91da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
101da177e4SLinus Torvalds  *   (at your option) any later version.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
131da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
141da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
151da177e4SLinus Torvalds  *   GNU General Public License for more details.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
181da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
191da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
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>
2865a77217SPaul Gortmaker #include <linux/module.h>
291da177e4SLinus Torvalds #include <sound/core.h>
301da177e4SLinus Torvalds #include <sound/pcm.h>
31666c70ffSTakashi Iwai #include <sound/tlv.h>
321da177e4SLinus Torvalds #include <sound/ac97_codec.h>
331da177e4SLinus Torvalds #include <sound/mpu401.h>
341da177e4SLinus Torvalds #include <sound/opl3.h>
351da177e4SLinus Torvalds #include <sound/initval.h>
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #include <asm/io.h>
381da177e4SLinus Torvalds 
39efce4bb9SAdrian Bunk #ifdef CONFIG_SND_FM801_TEA575X_BOOL
401da177e4SLinus Torvalds #include <sound/tea575x-tuner.h>
411da177e4SLinus Torvalds #endif
421da177e4SLinus Torvalds 
43c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.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 */
51*a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  *  Enable TEA575x tuner
541da177e4SLinus Torvalds  *    1 = MediaForte 256-PCS
55d7ba858aSOndrej Zary  *    2 = MediaForte 256-PCP
561da177e4SLinus Torvalds  *    3 = MediaForte 64-PCR
57fb716c0bSOndrej Zary  *   16 = setup tuner only (this is additional bit), i.e. SF64-PCR FM card
581da177e4SLinus Torvalds  *  High 16-bits are video (radio) device number + 1
591da177e4SLinus Torvalds  */
606581f4e7STakashi Iwai static int tea575x_tuner[SNDRV_CARDS];
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
631da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
641da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
651da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
661da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
671da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
681da177e4SLinus Torvalds module_param_array(tea575x_tuner, int, NULL, 0444);
69d7ba858aSOndrej Zary MODULE_PARM_DESC(tea575x_tuner, "TEA575x tuner access method (0 = auto, 1 = SF256-PCS, 2=SF256-PCP, 3=SF64-PCR, 8=disable, +16=tuner-only).");
70fb716c0bSOndrej Zary 
71c37279b9SBen Hutchings #define TUNER_DISABLED		(1<<3)
72fb716c0bSOndrej Zary #define TUNER_ONLY		(1<<4)
73fb716c0bSOndrej Zary #define TUNER_TYPE_MASK		(~TUNER_ONLY & 0xFFFF)
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds /*
761da177e4SLinus Torvalds  *  Direct registers
771da177e4SLinus Torvalds  */
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds #define FM801_REG(chip, reg)	(chip->port + FM801_##reg)
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds #define FM801_PCM_VOL		0x00	/* PCM Output Volume */
821da177e4SLinus Torvalds #define FM801_FM_VOL		0x02	/* FM Output Volume */
831da177e4SLinus Torvalds #define FM801_I2S_VOL		0x04	/* I2S Volume */
841da177e4SLinus Torvalds #define FM801_REC_SRC		0x06	/* Record Source */
851da177e4SLinus Torvalds #define FM801_PLY_CTRL		0x08	/* Playback Control */
861da177e4SLinus Torvalds #define FM801_PLY_COUNT		0x0a	/* Playback Count */
871da177e4SLinus Torvalds #define FM801_PLY_BUF1		0x0c	/* Playback Bufer I */
881da177e4SLinus Torvalds #define FM801_PLY_BUF2		0x10	/* Playback Buffer II */
891da177e4SLinus Torvalds #define FM801_CAP_CTRL		0x14	/* Capture Control */
901da177e4SLinus Torvalds #define FM801_CAP_COUNT		0x16	/* Capture Count */
911da177e4SLinus Torvalds #define FM801_CAP_BUF1		0x18	/* Capture Buffer I */
921da177e4SLinus Torvalds #define FM801_CAP_BUF2		0x1c	/* Capture Buffer II */
931da177e4SLinus Torvalds #define FM801_CODEC_CTRL	0x22	/* Codec Control */
941da177e4SLinus Torvalds #define FM801_I2S_MODE		0x24	/* I2S Mode Control */
951da177e4SLinus Torvalds #define FM801_VOLUME		0x26	/* Volume Up/Down/Mute Status */
961da177e4SLinus Torvalds #define FM801_I2C_CTRL		0x29	/* I2C Control */
971da177e4SLinus Torvalds #define FM801_AC97_CMD		0x2a	/* AC'97 Command */
981da177e4SLinus Torvalds #define FM801_AC97_DATA		0x2c	/* AC'97 Data */
991da177e4SLinus Torvalds #define FM801_MPU401_DATA	0x30	/* MPU401 Data */
1001da177e4SLinus Torvalds #define FM801_MPU401_CMD	0x31	/* MPU401 Command */
1011da177e4SLinus Torvalds #define FM801_GPIO_CTRL		0x52	/* General Purpose I/O Control */
1021da177e4SLinus Torvalds #define FM801_GEN_CTRL		0x54	/* General Control */
1031da177e4SLinus Torvalds #define FM801_IRQ_MASK		0x56	/* Interrupt Mask */
1041da177e4SLinus Torvalds #define FM801_IRQ_STATUS	0x5a	/* Interrupt Status */
1051da177e4SLinus Torvalds #define FM801_OPL3_BANK0	0x68	/* OPL3 Status Read / Bank 0 Write */
1061da177e4SLinus Torvalds #define FM801_OPL3_DATA0	0x69	/* OPL3 Data 0 Write */
1071da177e4SLinus Torvalds #define FM801_OPL3_BANK1	0x6a	/* OPL3 Bank 1 Write */
1081da177e4SLinus Torvalds #define FM801_OPL3_DATA1	0x6b	/* OPL3 Bank 1 Write */
1091da177e4SLinus Torvalds #define FM801_POWERDOWN		0x70	/* Blocks Power Down Control */
1101da177e4SLinus Torvalds 
111b1e9ed26STakashi Iwai /* codec access */
112b1e9ed26STakashi Iwai #define FM801_AC97_READ		(1<<7)	/* read=1, write=0 */
113b1e9ed26STakashi Iwai #define FM801_AC97_VALID	(1<<8)	/* port valid=1 */
114b1e9ed26STakashi Iwai #define FM801_AC97_BUSY		(1<<9)	/* busy=1 */
115b1e9ed26STakashi Iwai #define FM801_AC97_ADDR_SHIFT	10	/* codec id (2bit) */
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* playback and record control register bits */
1181da177e4SLinus Torvalds #define FM801_BUF1_LAST		(1<<1)
1191da177e4SLinus Torvalds #define FM801_BUF2_LAST		(1<<2)
1201da177e4SLinus Torvalds #define FM801_START		(1<<5)
1211da177e4SLinus Torvalds #define FM801_PAUSE		(1<<6)
1221da177e4SLinus Torvalds #define FM801_IMMED_STOP	(1<<7)
1231da177e4SLinus Torvalds #define FM801_RATE_SHIFT	8
1241da177e4SLinus Torvalds #define FM801_RATE_MASK		(15 << FM801_RATE_SHIFT)
1251da177e4SLinus Torvalds #define FM801_CHANNELS_4	(1<<12)	/* playback only */
1261da177e4SLinus Torvalds #define FM801_CHANNELS_6	(2<<12)	/* playback only */
1271da177e4SLinus Torvalds #define FM801_CHANNELS_6MS	(3<<12)	/* playback only */
1281da177e4SLinus Torvalds #define FM801_CHANNELS_MASK	(3<<12)
1291da177e4SLinus Torvalds #define FM801_16BIT		(1<<14)
1301da177e4SLinus Torvalds #define FM801_STEREO		(1<<15)
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /* IRQ status bits */
1331da177e4SLinus Torvalds #define FM801_IRQ_PLAYBACK	(1<<8)
1341da177e4SLinus Torvalds #define FM801_IRQ_CAPTURE	(1<<9)
1351da177e4SLinus Torvalds #define FM801_IRQ_VOLUME	(1<<14)
1361da177e4SLinus Torvalds #define FM801_IRQ_MPU		(1<<15)
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /* GPIO control register */
1391da177e4SLinus Torvalds #define FM801_GPIO_GP0		(1<<0)	/* read/write */
1401da177e4SLinus Torvalds #define FM801_GPIO_GP1		(1<<1)
1411da177e4SLinus Torvalds #define FM801_GPIO_GP2		(1<<2)
1421da177e4SLinus Torvalds #define FM801_GPIO_GP3		(1<<3)
1431da177e4SLinus Torvalds #define FM801_GPIO_GP(x)	(1<<(0+(x)))
1441da177e4SLinus Torvalds #define FM801_GPIO_GD0		(1<<8)	/* directions: 1 = input, 0 = output*/
1451da177e4SLinus Torvalds #define FM801_GPIO_GD1		(1<<9)
1461da177e4SLinus Torvalds #define FM801_GPIO_GD2		(1<<10)
1471da177e4SLinus Torvalds #define FM801_GPIO_GD3		(1<<11)
1481da177e4SLinus Torvalds #define FM801_GPIO_GD(x)	(1<<(8+(x)))
1491da177e4SLinus Torvalds #define FM801_GPIO_GS0		(1<<12)	/* function select: */
1501da177e4SLinus Torvalds #define FM801_GPIO_GS1		(1<<13)	/*    1 = GPIO */
1511da177e4SLinus Torvalds #define FM801_GPIO_GS2		(1<<14)	/*    0 = other (S/PDIF, VOL) */
1521da177e4SLinus Torvalds #define FM801_GPIO_GS3		(1<<15)
1531da177e4SLinus Torvalds #define FM801_GPIO_GS(x)	(1<<(12+(x)))
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds /*
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds  */
1581da177e4SLinus Torvalds 
159a5f22156STakashi Iwai struct fm801 {
1601da177e4SLinus Torvalds 	int irq;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	unsigned long port;	/* I/O port number */
1631da177e4SLinus Torvalds 	unsigned int multichannel: 1,	/* multichannel support */
1641da177e4SLinus Torvalds 		     secondary: 1;	/* secondary codec */
1651da177e4SLinus Torvalds 	unsigned char secondary_addr;	/* address of the secondary codec */
166fb716c0bSOndrej Zary 	unsigned int tea575x_tuner;	/* tuner access method & flags */
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	unsigned short ply_ctrl; /* playback control */
1691da177e4SLinus Torvalds 	unsigned short cap_ctrl; /* capture control */
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	unsigned long ply_buffer;
1721da177e4SLinus Torvalds 	unsigned int ply_buf;
1731da177e4SLinus Torvalds 	unsigned int ply_count;
1741da177e4SLinus Torvalds 	unsigned int ply_size;
1751da177e4SLinus Torvalds 	unsigned int ply_pos;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	unsigned long cap_buffer;
1781da177e4SLinus Torvalds 	unsigned int cap_buf;
1791da177e4SLinus Torvalds 	unsigned int cap_count;
1801da177e4SLinus Torvalds 	unsigned int cap_size;
1811da177e4SLinus Torvalds 	unsigned int cap_pos;
1821da177e4SLinus Torvalds 
183a5f22156STakashi Iwai 	struct snd_ac97_bus *ac97_bus;
184a5f22156STakashi Iwai 	struct snd_ac97 *ac97;
185a5f22156STakashi Iwai 	struct snd_ac97 *ac97_sec;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	struct pci_dev *pci;
188a5f22156STakashi Iwai 	struct snd_card *card;
189a5f22156STakashi Iwai 	struct snd_pcm *pcm;
190a5f22156STakashi Iwai 	struct snd_rawmidi *rmidi;
191a5f22156STakashi Iwai 	struct snd_pcm_substream *playback_substream;
192a5f22156STakashi Iwai 	struct snd_pcm_substream *capture_substream;
1931da177e4SLinus Torvalds 	unsigned int p_dma_size;
1941da177e4SLinus Torvalds 	unsigned int c_dma_size;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	spinlock_t reg_lock;
197a5f22156STakashi Iwai 	struct snd_info_entry *proc_entry;
1981da177e4SLinus Torvalds 
199fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
200a5f22156STakashi Iwai 	struct snd_tea575x tea;
2011da177e4SLinus Torvalds #endif
202b1e9ed26STakashi Iwai 
203b1e9ed26STakashi Iwai #ifdef CONFIG_PM
204b1e9ed26STakashi Iwai 	u16 saved_regs[0x20];
205b1e9ed26STakashi Iwai #endif
2061da177e4SLinus Torvalds };
2071da177e4SLinus Torvalds 
208cebe41d4SAlexey Dobriyan static DEFINE_PCI_DEVICE_TABLE(snd_fm801_ids) = {
2091da177e4SLinus Torvalds 	{ 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* FM801 */
21026be8659SSergey Vlasov 	{ 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* Gallant Odyssey Sound 4 */
2111da177e4SLinus Torvalds 	{ 0, }
2121da177e4SLinus Torvalds };
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds /*
2171da177e4SLinus Torvalds  *  common I/O routines
2181da177e4SLinus Torvalds  */
2191da177e4SLinus Torvalds 
220a5f22156STakashi Iwai static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
2211da177e4SLinus Torvalds 				 unsigned short mask, unsigned short value)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	int change;
2241da177e4SLinus Torvalds 	unsigned long flags;
2251da177e4SLinus Torvalds 	unsigned short old, new;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
2281da177e4SLinus Torvalds 	old = inw(chip->port + reg);
2291da177e4SLinus Torvalds 	new = (old & ~mask) | value;
2301da177e4SLinus Torvalds 	change = old != new;
2311da177e4SLinus Torvalds 	if (change)
2321da177e4SLinus Torvalds 		outw(new, chip->port + reg);
2331da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
2341da177e4SLinus Torvalds 	return change;
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds 
237a5f22156STakashi Iwai static void snd_fm801_codec_write(struct snd_ac97 *ac97,
2381da177e4SLinus Torvalds 				  unsigned short reg,
2391da177e4SLinus Torvalds 				  unsigned short val)
2401da177e4SLinus Torvalds {
241a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
2421da177e4SLinus Torvalds 	int idx;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	/*
2451da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
2461da177e4SLinus Torvalds 	 */
2471da177e4SLinus Torvalds 	for (idx = 0; idx < 100; idx++) {
248b1e9ed26STakashi Iwai 		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
2491da177e4SLinus Torvalds 			goto ok1;
2501da177e4SLinus Torvalds 		udelay(10);
2511da177e4SLinus Torvalds 	}
25299b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface is busy (1)\n");
2531da177e4SLinus Torvalds 	return;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds  ok1:
2561da177e4SLinus Torvalds 	/* write data and address */
2571da177e4SLinus Torvalds 	outw(val, FM801_REG(chip, AC97_DATA));
2581da177e4SLinus Torvalds 	outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
2591da177e4SLinus Torvalds 	/*
2601da177e4SLinus Torvalds 	 *  Wait until the write command is not completed..
2611da177e4SLinus Torvalds          */
2621da177e4SLinus Torvalds 	for (idx = 0; idx < 1000; idx++) {
263b1e9ed26STakashi Iwai 		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
2641da177e4SLinus Torvalds 			return;
2651da177e4SLinus Torvalds 		udelay(10);
2661da177e4SLinus Torvalds 	}
26799b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface #%d is busy (2)\n", ac97->num);
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
270a5f22156STakashi Iwai static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
2711da177e4SLinus Torvalds {
272a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
2731da177e4SLinus Torvalds 	int idx;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	/*
2761da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
2771da177e4SLinus Torvalds 	 */
2781da177e4SLinus Torvalds 	for (idx = 0; idx < 100; idx++) {
279b1e9ed26STakashi Iwai 		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
2801da177e4SLinus Torvalds 			goto ok1;
2811da177e4SLinus Torvalds 		udelay(10);
2821da177e4SLinus Torvalds 	}
28399b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface is busy (1)\n");
2841da177e4SLinus Torvalds 	return 0;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds  ok1:
2871da177e4SLinus Torvalds 	/* read command */
288b1e9ed26STakashi Iwai 	outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ,
289b1e9ed26STakashi Iwai 	     FM801_REG(chip, AC97_CMD));
2901da177e4SLinus Torvalds 	for (idx = 0; idx < 100; idx++) {
291b1e9ed26STakashi Iwai 		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
2921da177e4SLinus Torvalds 			goto ok2;
2931da177e4SLinus Torvalds 		udelay(10);
2941da177e4SLinus Torvalds 	}
29599b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface #%d is busy (2)\n", ac97->num);
2961da177e4SLinus Torvalds 	return 0;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds  ok2:
2991da177e4SLinus Torvalds 	for (idx = 0; idx < 1000; idx++) {
300b1e9ed26STakashi Iwai 		if (inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_VALID)
3011da177e4SLinus Torvalds 			goto ok3;
3021da177e4SLinus Torvalds 		udelay(10);
3031da177e4SLinus Torvalds 	}
30499b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface #%d is not valid (2)\n", ac97->num);
3051da177e4SLinus Torvalds 	return 0;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds  ok3:
3081da177e4SLinus Torvalds 	return inw(FM801_REG(chip, AC97_DATA));
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds static unsigned int rates[] = {
3121da177e4SLinus Torvalds   5500,  8000,  9600, 11025,
3131da177e4SLinus Torvalds   16000, 19200, 22050, 32000,
3141da177e4SLinus Torvalds   38400, 44100, 48000
3151da177e4SLinus Torvalds };
3161da177e4SLinus Torvalds 
317a5f22156STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
3181da177e4SLinus Torvalds 	.count = ARRAY_SIZE(rates),
3191da177e4SLinus Torvalds 	.list = rates,
3201da177e4SLinus Torvalds 	.mask = 0,
3211da177e4SLinus Torvalds };
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds static unsigned int channels[] = {
3241da177e4SLinus Torvalds   2, 4, 6
3251da177e4SLinus Torvalds };
3261da177e4SLinus Torvalds 
327a5f22156STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
3285e4968e2STobias Klauser 	.count = ARRAY_SIZE(channels),
3291da177e4SLinus Torvalds 	.list = channels,
3301da177e4SLinus Torvalds 	.mask = 0,
3311da177e4SLinus Torvalds };
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds /*
3341da177e4SLinus Torvalds  *  Sample rate routines
3351da177e4SLinus Torvalds  */
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds static unsigned short snd_fm801_rate_bits(unsigned int rate)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	unsigned int idx;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
3421da177e4SLinus Torvalds 		if (rates[idx] == rate)
3431da177e4SLinus Torvalds 			return idx;
3441da177e4SLinus Torvalds 	snd_BUG();
3451da177e4SLinus Torvalds 	return ARRAY_SIZE(rates) - 1;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds /*
3491da177e4SLinus Torvalds  *  PCM part
3501da177e4SLinus Torvalds  */
3511da177e4SLinus Torvalds 
352a5f22156STakashi Iwai static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
3531da177e4SLinus Torvalds 				      int cmd)
3541da177e4SLinus Torvalds {
355a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3581da177e4SLinus Torvalds 	switch (cmd) {
3591da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
3601da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_BUF1_LAST |
3611da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
3621da177e4SLinus Torvalds 				     FM801_PAUSE);
3631da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_START |
3641da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
3651da177e4SLinus Torvalds 		break;
3661da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
3671da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
3681da177e4SLinus Torvalds 		break;
3691da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
370b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
3711da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_PAUSE;
3721da177e4SLinus Torvalds 		break;
3731da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
374b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
3751da177e4SLinus Torvalds 		chip->ply_ctrl &= ~FM801_PAUSE;
3761da177e4SLinus Torvalds 		break;
3771da177e4SLinus Torvalds 	default:
3781da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
3791da177e4SLinus Torvalds 		snd_BUG();
3801da177e4SLinus Torvalds 		return -EINVAL;
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds 	outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
3831da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3841da177e4SLinus Torvalds 	return 0;
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
387a5f22156STakashi Iwai static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
3881da177e4SLinus Torvalds 				     int cmd)
3891da177e4SLinus Torvalds {
390a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3931da177e4SLinus Torvalds 	switch (cmd) {
3941da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
3951da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_BUF1_LAST |
3961da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
3971da177e4SLinus Torvalds 				     FM801_PAUSE);
3981da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_START |
3991da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
4001da177e4SLinus Torvalds 		break;
4011da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4021da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
4031da177e4SLinus Torvalds 		break;
4041da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
405b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
4061da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_PAUSE;
4071da177e4SLinus Torvalds 		break;
4081da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
409b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
4101da177e4SLinus Torvalds 		chip->cap_ctrl &= ~FM801_PAUSE;
4111da177e4SLinus Torvalds 		break;
4121da177e4SLinus Torvalds 	default:
4131da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
4141da177e4SLinus Torvalds 		snd_BUG();
4151da177e4SLinus Torvalds 		return -EINVAL;
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 	outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
4181da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4191da177e4SLinus Torvalds 	return 0;
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
422a5f22156STakashi Iwai static int snd_fm801_hw_params(struct snd_pcm_substream *substream,
423a5f22156STakashi Iwai 			       struct snd_pcm_hw_params *hw_params)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
428a5f22156STakashi Iwai static int snd_fm801_hw_free(struct snd_pcm_substream *substream)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
433a5f22156STakashi Iwai static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
4341da177e4SLinus Torvalds {
435a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
436a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
4391da177e4SLinus Torvalds 	chip->ply_count = snd_pcm_lib_period_bytes(substream);
4401da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
4411da177e4SLinus Torvalds 	chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
4421da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK |
4431da177e4SLinus Torvalds 			     FM801_CHANNELS_MASK);
4441da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
4451da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_16BIT;
4461da177e4SLinus Torvalds 	if (runtime->channels > 1) {
4471da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_STEREO;
4481da177e4SLinus Torvalds 		if (runtime->channels == 4)
4491da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_4;
4501da177e4SLinus Torvalds 		else if (runtime->channels == 6)
4511da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_6;
4521da177e4SLinus Torvalds 	}
4531da177e4SLinus Torvalds 	chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
4541da177e4SLinus Torvalds 	chip->ply_buf = 0;
4551da177e4SLinus Torvalds 	outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
4561da177e4SLinus Torvalds 	outw(chip->ply_count - 1, FM801_REG(chip, PLY_COUNT));
4571da177e4SLinus Torvalds 	chip->ply_buffer = runtime->dma_addr;
4581da177e4SLinus Torvalds 	chip->ply_pos = 0;
4591da177e4SLinus Torvalds 	outl(chip->ply_buffer, FM801_REG(chip, PLY_BUF1));
4601da177e4SLinus Torvalds 	outl(chip->ply_buffer + (chip->ply_count % chip->ply_size), FM801_REG(chip, PLY_BUF2));
4611da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
4621da177e4SLinus Torvalds 	return 0;
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
465a5f22156STakashi Iwai static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
4661da177e4SLinus Torvalds {
467a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
468a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
4711da177e4SLinus Torvalds 	chip->cap_count = snd_pcm_lib_period_bytes(substream);
4721da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
4731da177e4SLinus Torvalds 	chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
4741da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK);
4751da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
4761da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_16BIT;
4771da177e4SLinus Torvalds 	if (runtime->channels > 1)
4781da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_STEREO;
4791da177e4SLinus Torvalds 	chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
4801da177e4SLinus Torvalds 	chip->cap_buf = 0;
4811da177e4SLinus Torvalds 	outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
4821da177e4SLinus Torvalds 	outw(chip->cap_count - 1, FM801_REG(chip, CAP_COUNT));
4831da177e4SLinus Torvalds 	chip->cap_buffer = runtime->dma_addr;
4841da177e4SLinus Torvalds 	chip->cap_pos = 0;
4851da177e4SLinus Torvalds 	outl(chip->cap_buffer, FM801_REG(chip, CAP_BUF1));
4861da177e4SLinus Torvalds 	outl(chip->cap_buffer + (chip->cap_count % chip->cap_size), FM801_REG(chip, CAP_BUF2));
4871da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
4881da177e4SLinus Torvalds 	return 0;
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds 
491a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
4921da177e4SLinus Torvalds {
493a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
4941da177e4SLinus Torvalds 	size_t ptr;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	if (!(chip->ply_ctrl & FM801_START))
4971da177e4SLinus Torvalds 		return 0;
4981da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
4991da177e4SLinus Torvalds 	ptr = chip->ply_pos + (chip->ply_count - 1) - inw(FM801_REG(chip, PLY_COUNT));
5001da177e4SLinus Torvalds 	if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_PLAYBACK) {
5011da177e4SLinus Torvalds 		ptr += chip->ply_count;
5021da177e4SLinus Torvalds 		ptr %= chip->ply_size;
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5051da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
508a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
5091da177e4SLinus Torvalds {
510a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
5111da177e4SLinus Torvalds 	size_t ptr;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	if (!(chip->cap_ctrl & FM801_START))
5141da177e4SLinus Torvalds 		return 0;
5151da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
5161da177e4SLinus Torvalds 	ptr = chip->cap_pos + (chip->cap_count - 1) - inw(FM801_REG(chip, CAP_COUNT));
5171da177e4SLinus Torvalds 	if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_CAPTURE) {
5181da177e4SLinus Torvalds 		ptr += chip->cap_count;
5191da177e4SLinus Torvalds 		ptr %= chip->cap_size;
5201da177e4SLinus Torvalds 	}
5211da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5221da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5257d12e780SDavid Howells static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
5261da177e4SLinus Torvalds {
527a5f22156STakashi Iwai 	struct fm801 *chip = dev_id;
5281da177e4SLinus Torvalds 	unsigned short status;
5291da177e4SLinus Torvalds 	unsigned int tmp;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	status = inw(FM801_REG(chip, IRQ_STATUS));
5321da177e4SLinus Torvalds 	status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
5331da177e4SLinus Torvalds 	if (! status)
5341da177e4SLinus Torvalds 		return IRQ_NONE;
5351da177e4SLinus Torvalds 	/* ack first */
5361da177e4SLinus Torvalds 	outw(status, FM801_REG(chip, IRQ_STATUS));
5371da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
5381da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5391da177e4SLinus Torvalds 		chip->ply_buf++;
5401da177e4SLinus Torvalds 		chip->ply_pos += chip->ply_count;
5411da177e4SLinus Torvalds 		chip->ply_pos %= chip->ply_size;
5421da177e4SLinus Torvalds 		tmp = chip->ply_pos + chip->ply_count;
5431da177e4SLinus Torvalds 		tmp %= chip->ply_size;
5441da177e4SLinus Torvalds 		outl(chip->ply_buffer + tmp,
5451da177e4SLinus Torvalds 				(chip->ply_buf & 1) ?
5461da177e4SLinus Torvalds 					FM801_REG(chip, PLY_BUF1) :
5471da177e4SLinus Torvalds 					FM801_REG(chip, PLY_BUF2));
5481da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5491da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->playback_substream);
5501da177e4SLinus Torvalds 	}
5511da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
5521da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5531da177e4SLinus Torvalds 		chip->cap_buf++;
5541da177e4SLinus Torvalds 		chip->cap_pos += chip->cap_count;
5551da177e4SLinus Torvalds 		chip->cap_pos %= chip->cap_size;
5561da177e4SLinus Torvalds 		tmp = chip->cap_pos + chip->cap_count;
5571da177e4SLinus Torvalds 		tmp %= chip->cap_size;
5581da177e4SLinus Torvalds 		outl(chip->cap_buffer + tmp,
5591da177e4SLinus Torvalds 				(chip->cap_buf & 1) ?
5601da177e4SLinus Torvalds 					FM801_REG(chip, CAP_BUF1) :
5611da177e4SLinus Torvalds 					FM801_REG(chip, CAP_BUF2));
5621da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5631da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->capture_substream);
5641da177e4SLinus Torvalds 	}
5651da177e4SLinus Torvalds 	if (chip->rmidi && (status & FM801_IRQ_MPU))
5667d12e780SDavid Howells 		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
5671da177e4SLinus Torvalds 	if (status & FM801_IRQ_VOLUME)
5681da177e4SLinus Torvalds 		;/* TODO */
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	return IRQ_HANDLED;
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
573a5f22156STakashi Iwai static struct snd_pcm_hardware snd_fm801_playback =
5741da177e4SLinus Torvalds {
5751da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
5761da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
577b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
5781da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
5791da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
5801da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
5811da177e4SLinus Torvalds 	.rate_min =		5500,
5821da177e4SLinus Torvalds 	.rate_max =		48000,
5831da177e4SLinus Torvalds 	.channels_min =		1,
5841da177e4SLinus Torvalds 	.channels_max =		2,
5851da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
5861da177e4SLinus Torvalds 	.period_bytes_min =	64,
5871da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
5881da177e4SLinus Torvalds 	.periods_min =		1,
5891da177e4SLinus Torvalds 	.periods_max =		1024,
5901da177e4SLinus Torvalds 	.fifo_size =		0,
5911da177e4SLinus Torvalds };
5921da177e4SLinus Torvalds 
593a5f22156STakashi Iwai static struct snd_pcm_hardware snd_fm801_capture =
5941da177e4SLinus Torvalds {
5951da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
5961da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
597b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
5981da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
5991da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
6001da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
6011da177e4SLinus Torvalds 	.rate_min =		5500,
6021da177e4SLinus Torvalds 	.rate_max =		48000,
6031da177e4SLinus Torvalds 	.channels_min =		1,
6041da177e4SLinus Torvalds 	.channels_max =		2,
6051da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
6061da177e4SLinus Torvalds 	.period_bytes_min =	64,
6071da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
6081da177e4SLinus Torvalds 	.periods_min =		1,
6091da177e4SLinus Torvalds 	.periods_max =		1024,
6101da177e4SLinus Torvalds 	.fifo_size =		0,
6111da177e4SLinus Torvalds };
6121da177e4SLinus Torvalds 
613a5f22156STakashi Iwai static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
6141da177e4SLinus Torvalds {
615a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
616a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6171da177e4SLinus Torvalds 	int err;
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	chip->playback_substream = substream;
6201da177e4SLinus Torvalds 	runtime->hw = snd_fm801_playback;
621a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
622a5f22156STakashi Iwai 				   &hw_constraints_rates);
6231da177e4SLinus Torvalds 	if (chip->multichannel) {
6241da177e4SLinus Torvalds 		runtime->hw.channels_max = 6;
625a5f22156STakashi Iwai 		snd_pcm_hw_constraint_list(runtime, 0,
626a5f22156STakashi Iwai 					   SNDRV_PCM_HW_PARAM_CHANNELS,
627a5f22156STakashi Iwai 					   &hw_constraints_channels);
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
6301da177e4SLinus Torvalds 		return err;
6311da177e4SLinus Torvalds 	return 0;
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
634a5f22156STakashi Iwai static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
6351da177e4SLinus Torvalds {
636a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
637a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6381da177e4SLinus Torvalds 	int err;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	chip->capture_substream = substream;
6411da177e4SLinus Torvalds 	runtime->hw = snd_fm801_capture;
642a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
643a5f22156STakashi Iwai 				   &hw_constraints_rates);
6441da177e4SLinus Torvalds 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
6451da177e4SLinus Torvalds 		return err;
6461da177e4SLinus Torvalds 	return 0;
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
649a5f22156STakashi Iwai static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
6501da177e4SLinus Torvalds {
651a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	chip->playback_substream = NULL;
6541da177e4SLinus Torvalds 	return 0;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
657a5f22156STakashi Iwai static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
6581da177e4SLinus Torvalds {
659a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	chip->capture_substream = NULL;
6621da177e4SLinus Torvalds 	return 0;
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
665a5f22156STakashi Iwai static struct snd_pcm_ops snd_fm801_playback_ops = {
6661da177e4SLinus Torvalds 	.open =		snd_fm801_playback_open,
6671da177e4SLinus Torvalds 	.close =	snd_fm801_playback_close,
6681da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
6691da177e4SLinus Torvalds 	.hw_params =	snd_fm801_hw_params,
6701da177e4SLinus Torvalds 	.hw_free =	snd_fm801_hw_free,
6711da177e4SLinus Torvalds 	.prepare =	snd_fm801_playback_prepare,
6721da177e4SLinus Torvalds 	.trigger =	snd_fm801_playback_trigger,
6731da177e4SLinus Torvalds 	.pointer =	snd_fm801_playback_pointer,
6741da177e4SLinus Torvalds };
6751da177e4SLinus Torvalds 
676a5f22156STakashi Iwai static struct snd_pcm_ops snd_fm801_capture_ops = {
6771da177e4SLinus Torvalds 	.open =		snd_fm801_capture_open,
6781da177e4SLinus Torvalds 	.close =	snd_fm801_capture_close,
6791da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
6801da177e4SLinus Torvalds 	.hw_params =	snd_fm801_hw_params,
6811da177e4SLinus Torvalds 	.hw_free =	snd_fm801_hw_free,
6821da177e4SLinus Torvalds 	.prepare =	snd_fm801_capture_prepare,
6831da177e4SLinus Torvalds 	.trigger =	snd_fm801_capture_trigger,
6841da177e4SLinus Torvalds 	.pointer =	snd_fm801_capture_pointer,
6851da177e4SLinus Torvalds };
6861da177e4SLinus Torvalds 
687a5f22156STakashi Iwai static int __devinit snd_fm801_pcm(struct fm801 *chip, int device, struct snd_pcm ** rpcm)
6881da177e4SLinus Torvalds {
689a5f22156STakashi Iwai 	struct snd_pcm *pcm;
6901da177e4SLinus Torvalds 	int err;
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	if (rpcm)
6931da177e4SLinus Torvalds 		*rpcm = NULL;
6941da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm)) < 0)
6951da177e4SLinus Torvalds 		return err;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
6981da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	pcm->private_data = chip;
7011da177e4SLinus Torvalds 	pcm->info_flags = 0;
7021da177e4SLinus Torvalds 	strcpy(pcm->name, "FM801");
7031da177e4SLinus Torvalds 	chip->pcm = pcm;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
7061da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci),
7071da177e4SLinus Torvalds 					      chip->multichannel ? 128*1024 : 64*1024, 128*1024);
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	if (rpcm)
7101da177e4SLinus Torvalds 		*rpcm = pcm;
7111da177e4SLinus Torvalds 	return 0;
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds /*
7151da177e4SLinus Torvalds  *  TEA5757 radio
7161da177e4SLinus Torvalds  */
7171da177e4SLinus Torvalds 
718fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
7191da177e4SLinus Torvalds 
720938a1566SOndrej Zary /* GPIO to TEA575x maps */
721938a1566SOndrej Zary struct snd_fm801_tea575x_gpio {
722938a1566SOndrej Zary 	u8 data, clk, wren, most;
723d7ba858aSOndrej Zary 	char *name;
724938a1566SOndrej Zary };
7251da177e4SLinus Torvalds 
726938a1566SOndrej Zary static struct snd_fm801_tea575x_gpio snd_fm801_tea575x_gpios[] = {
727d7ba858aSOndrej Zary 	{ .data = 1, .clk = 3, .wren = 2, .most = 0, .name = "SF256-PCS" },
728d7ba858aSOndrej Zary 	{ .data = 1, .clk = 0, .wren = 2, .most = 3, .name = "SF256-PCP" },
729d7ba858aSOndrej Zary 	{ .data = 2, .clk = 0, .wren = 1, .most = 3, .name = "SF64-PCR" },
730938a1566SOndrej Zary };
731938a1566SOndrej Zary 
7328e699d2cSTakashi Iwai #define get_tea575x_gpio(chip) \
7338e699d2cSTakashi Iwai 	(&snd_fm801_tea575x_gpios[((chip)->tea575x_tuner & TUNER_TYPE_MASK) - 1])
7348e699d2cSTakashi Iwai 
735938a1566SOndrej Zary static void snd_fm801_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
7361da177e4SLinus Torvalds {
737a5f22156STakashi Iwai 	struct fm801 *chip = tea->private_data;
738938a1566SOndrej Zary 	unsigned short reg = inw(FM801_REG(chip, GPIO_CTRL));
7398e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
7401da177e4SLinus Torvalds 
741938a1566SOndrej Zary 	reg &= ~(FM801_GPIO_GP(gpio.data) |
742938a1566SOndrej Zary 		 FM801_GPIO_GP(gpio.clk) |
743938a1566SOndrej Zary 		 FM801_GPIO_GP(gpio.wren));
744938a1566SOndrej Zary 
745938a1566SOndrej Zary 	reg |= (pins & TEA575X_DATA) ? FM801_GPIO_GP(gpio.data) : 0;
746938a1566SOndrej Zary 	reg |= (pins & TEA575X_CLK)  ? FM801_GPIO_GP(gpio.clk) : 0;
747938a1566SOndrej Zary 	/* WRITE_ENABLE is inverted */
748938a1566SOndrej Zary 	reg |= (pins & TEA575X_WREN) ? 0 : FM801_GPIO_GP(gpio.wren);
749938a1566SOndrej Zary 
750938a1566SOndrej Zary 	outw(reg, FM801_REG(chip, GPIO_CTRL));
751938a1566SOndrej Zary }
752938a1566SOndrej Zary 
753938a1566SOndrej Zary static u8 snd_fm801_tea575x_get_pins(struct snd_tea575x *tea)
754938a1566SOndrej Zary {
755938a1566SOndrej Zary 	struct fm801 *chip = tea->private_data;
756938a1566SOndrej Zary 	unsigned short reg = inw(FM801_REG(chip, GPIO_CTRL));
7578e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
758938a1566SOndrej Zary 
759938a1566SOndrej Zary 	return  (reg & FM801_GPIO_GP(gpio.data)) ? TEA575X_DATA : 0 |
760938a1566SOndrej Zary 		(reg & FM801_GPIO_GP(gpio.most)) ? TEA575X_MOST : 0;
761938a1566SOndrej Zary }
762938a1566SOndrej Zary 
763938a1566SOndrej Zary static void snd_fm801_tea575x_set_direction(struct snd_tea575x *tea, bool output)
764938a1566SOndrej Zary {
765938a1566SOndrej Zary 	struct fm801 *chip = tea->private_data;
766938a1566SOndrej Zary 	unsigned short reg = inw(FM801_REG(chip, GPIO_CTRL));
7678e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
768938a1566SOndrej Zary 
7691da177e4SLinus Torvalds 	/* use GPIO lines and set write enable bit */
770938a1566SOndrej Zary 	reg |= FM801_GPIO_GS(gpio.data) |
771938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.wren) |
772938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.clk) |
773938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.most);
774938a1566SOndrej Zary 	if (output) {
7751da177e4SLinus Torvalds 		/* all of lines are in the write direction */
7761da177e4SLinus Torvalds 		/* clear data and clock lines */
777938a1566SOndrej Zary 		reg &= ~(FM801_GPIO_GD(gpio.data) |
778938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.wren) |
779938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.clk) |
780938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.data) |
781938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.clk) |
782938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.wren));
783938a1566SOndrej Zary 	} else {
7841da177e4SLinus Torvalds 		/* use GPIO lines, set data direction to input */
785938a1566SOndrej Zary 		reg |= FM801_GPIO_GD(gpio.data) |
786938a1566SOndrej Zary 		       FM801_GPIO_GD(gpio.most) |
787938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.data) |
788938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.most) |
789938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.wren);
7901da177e4SLinus Torvalds 		/* all of lines are in the write direction, except data */
7911da177e4SLinus Torvalds 		/* clear data, write enable and clock lines */
792938a1566SOndrej Zary 		reg &= ~(FM801_GPIO_GD(gpio.wren) |
793938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.clk) |
794938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.clk));
7951da177e4SLinus Torvalds 	}
7961da177e4SLinus Torvalds 
797938a1566SOndrej Zary 	outw(reg, FM801_REG(chip, GPIO_CTRL));
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds 
800938a1566SOndrej Zary static struct snd_tea575x_ops snd_fm801_tea_ops = {
801938a1566SOndrej Zary 	.set_pins = snd_fm801_tea575x_set_pins,
802938a1566SOndrej Zary 	.get_pins = snd_fm801_tea575x_get_pins,
803938a1566SOndrej Zary 	.set_direction = snd_fm801_tea575x_set_direction,
8041da177e4SLinus Torvalds };
8051da177e4SLinus Torvalds #endif
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds /*
8081da177e4SLinus Torvalds  *  Mixer routines
8091da177e4SLinus Torvalds  */
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds #define FM801_SINGLE(xname, reg, shift, mask, invert) \
8121da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
8131da177e4SLinus Torvalds   .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
8141da177e4SLinus Torvalds   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
8151da177e4SLinus Torvalds 
816a5f22156STakashi Iwai static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
817a5f22156STakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
8221da177e4SLinus Torvalds 	uinfo->count = 1;
8231da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
8241da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
8251da177e4SLinus Torvalds 	return 0;
8261da177e4SLinus Torvalds }
8271da177e4SLinus Torvalds 
828a5f22156STakashi Iwai static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
829a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
8301da177e4SLinus Torvalds {
831a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
8321da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
8331da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
8341da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8351da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift) & mask;
8381da177e4SLinus Torvalds 	if (invert)
8391da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
8401da177e4SLinus Torvalds 	return 0;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
843a5f22156STakashi Iwai static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
844a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
8451da177e4SLinus Torvalds {
846a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
8471da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
8481da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
8491da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8501da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
8511da177e4SLinus Torvalds 	unsigned short val;
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
8541da177e4SLinus Torvalds 	if (invert)
8551da177e4SLinus Torvalds 		val = mask - val;
8561da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds #define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
8601da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
8611da177e4SLinus Torvalds   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
8621da177e4SLinus Torvalds   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
863666c70ffSTakashi Iwai #define FM801_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
864666c70ffSTakashi Iwai { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
865666c70ffSTakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
866666c70ffSTakashi Iwai   .name = xname, .info = snd_fm801_info_double, \
867666c70ffSTakashi Iwai   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
868666c70ffSTakashi Iwai   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
869666c70ffSTakashi Iwai   .tlv = { .p = (xtlv) } }
8701da177e4SLinus Torvalds 
871a5f22156STakashi Iwai static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
872a5f22156STakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
8731da177e4SLinus Torvalds {
8741da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
8771da177e4SLinus Torvalds 	uinfo->count = 2;
8781da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
8791da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
8801da177e4SLinus Torvalds 	return 0;
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds 
883a5f22156STakashi Iwai static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
884a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
8851da177e4SLinus Torvalds {
886a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
8871da177e4SLinus Torvalds         int reg = kcontrol->private_value & 0xff;
8881da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
8891da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
8901da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8911da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
8941da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift_left) & mask;
8951da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = (inw(chip->port + reg) >> shift_right) & mask;
8961da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
8971da177e4SLinus Torvalds 	if (invert) {
8981da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
8991da177e4SLinus Torvalds 		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
9001da177e4SLinus Torvalds 	}
9011da177e4SLinus Torvalds 	return 0;
9021da177e4SLinus Torvalds }
9031da177e4SLinus Torvalds 
904a5f22156STakashi Iwai static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
905a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
9061da177e4SLinus Torvalds {
907a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9081da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
9091da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
9101da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
9111da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
9121da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
9131da177e4SLinus Torvalds 	unsigned short val1, val2;
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
9161da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
9171da177e4SLinus Torvalds 	if (invert) {
9181da177e4SLinus Torvalds 		val1 = mask - val1;
9191da177e4SLinus Torvalds 		val2 = mask - val2;
9201da177e4SLinus Torvalds 	}
9211da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg,
9221da177e4SLinus Torvalds 				     (mask << shift_left) | (mask << shift_right),
9231da177e4SLinus Torvalds 				     (val1 << shift_left ) | (val2 << shift_right));
9241da177e4SLinus Torvalds }
9251da177e4SLinus Torvalds 
926a5f22156STakashi Iwai static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
927a5f22156STakashi Iwai 			      struct snd_ctl_elem_info *uinfo)
9281da177e4SLinus Torvalds {
9291da177e4SLinus Torvalds 	static char *texts[5] = {
9301da177e4SLinus Torvalds 		"AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
9311da177e4SLinus Torvalds 	};
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
9341da177e4SLinus Torvalds 	uinfo->count = 1;
9351da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 5;
9361da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item > 4)
9371da177e4SLinus Torvalds 		uinfo->value.enumerated.item = 4;
9381da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
9391da177e4SLinus Torvalds 	return 0;
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
942a5f22156STakashi Iwai static int snd_fm801_get_mux(struct snd_kcontrol *kcontrol,
943a5f22156STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
9441da177e4SLinus Torvalds {
945a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9461da177e4SLinus Torvalds         unsigned short val;
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	val = inw(FM801_REG(chip, REC_SRC)) & 7;
9491da177e4SLinus Torvalds 	if (val > 4)
9501da177e4SLinus Torvalds 		val = 4;
9511da177e4SLinus Torvalds         ucontrol->value.enumerated.item[0] = val;
9521da177e4SLinus Torvalds         return 0;
9531da177e4SLinus Torvalds }
9541da177e4SLinus Torvalds 
955a5f22156STakashi Iwai static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
956a5f22156STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
9571da177e4SLinus Torvalds {
958a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9591da177e4SLinus Torvalds         unsigned short val;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds         if ((val = ucontrol->value.enumerated.item[0]) > 4)
9621da177e4SLinus Torvalds                 return -EINVAL;
9631da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
9641da177e4SLinus Torvalds }
9651da177e4SLinus Torvalds 
9660cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
967666c70ffSTakashi Iwai 
968a5f22156STakashi Iwai #define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
9691da177e4SLinus Torvalds 
970a5f22156STakashi Iwai static struct snd_kcontrol_new snd_fm801_controls[] __devinitdata = {
971666c70ffSTakashi Iwai FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
972666c70ffSTakashi Iwai 		 db_scale_dsp),
9731da177e4SLinus Torvalds FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
974666c70ffSTakashi Iwai FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
975666c70ffSTakashi Iwai 		 db_scale_dsp),
9761da177e4SLinus Torvalds FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
977666c70ffSTakashi Iwai FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
978666c70ffSTakashi Iwai 		 db_scale_dsp),
9791da177e4SLinus Torvalds FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
9821da177e4SLinus Torvalds 	.name = "Digital Capture Source",
9831da177e4SLinus Torvalds 	.info = snd_fm801_info_mux,
9841da177e4SLinus Torvalds 	.get = snd_fm801_get_mux,
9851da177e4SLinus Torvalds 	.put = snd_fm801_put_mux,
9861da177e4SLinus Torvalds }
9871da177e4SLinus Torvalds };
9881da177e4SLinus Torvalds 
989a5f22156STakashi Iwai #define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
9901da177e4SLinus Torvalds 
991a5f22156STakashi Iwai static struct snd_kcontrol_new snd_fm801_controls_multi[] __devinitdata = {
9921da177e4SLinus Torvalds FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
9931da177e4SLinus Torvalds FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
99410e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
99510e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
99610e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
99710e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
9981da177e4SLinus Torvalds };
9991da177e4SLinus Torvalds 
1000a5f22156STakashi Iwai static void snd_fm801_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
10011da177e4SLinus Torvalds {
1002a5f22156STakashi Iwai 	struct fm801 *chip = bus->private_data;
10031da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds 
1006a5f22156STakashi Iwai static void snd_fm801_mixer_free_ac97(struct snd_ac97 *ac97)
10071da177e4SLinus Torvalds {
1008a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
10091da177e4SLinus Torvalds 	if (ac97->num == 0) {
10101da177e4SLinus Torvalds 		chip->ac97 = NULL;
10111da177e4SLinus Torvalds 	} else {
10121da177e4SLinus Torvalds 		chip->ac97_sec = NULL;
10131da177e4SLinus Torvalds 	}
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
1016a5f22156STakashi Iwai static int __devinit snd_fm801_mixer(struct fm801 *chip)
10171da177e4SLinus Torvalds {
1018a5f22156STakashi Iwai 	struct snd_ac97_template ac97;
10191da177e4SLinus Torvalds 	unsigned int i;
10201da177e4SLinus Torvalds 	int err;
1021a5f22156STakashi Iwai 	static struct snd_ac97_bus_ops ops = {
10221da177e4SLinus Torvalds 		.write = snd_fm801_codec_write,
10231da177e4SLinus Torvalds 		.read = snd_fm801_codec_read,
10241da177e4SLinus Torvalds 	};
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
10271da177e4SLinus Torvalds 		return err;
10281da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_fm801_mixer_free_ac97_bus;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
10311da177e4SLinus Torvalds 	ac97.private_data = chip;
10321da177e4SLinus Torvalds 	ac97.private_free = snd_fm801_mixer_free_ac97;
10331da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
10341da177e4SLinus Torvalds 		return err;
10351da177e4SLinus Torvalds 	if (chip->secondary) {
10361da177e4SLinus Torvalds 		ac97.num = 1;
10371da177e4SLinus Torvalds 		ac97.addr = chip->secondary_addr;
10381da177e4SLinus Torvalds 		if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec)) < 0)
10391da177e4SLinus Torvalds 			return err;
10401da177e4SLinus Torvalds 	}
10411da177e4SLinus Torvalds 	for (i = 0; i < FM801_CONTROLS; i++)
10421da177e4SLinus Torvalds 		snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls[i], chip));
10431da177e4SLinus Torvalds 	if (chip->multichannel) {
10441da177e4SLinus Torvalds 		for (i = 0; i < FM801_CONTROLS_MULTI; i++)
10451da177e4SLinus Torvalds 			snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
10461da177e4SLinus Torvalds 	}
10471da177e4SLinus Torvalds 	return 0;
10481da177e4SLinus Torvalds }
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds /*
10511da177e4SLinus Torvalds  *  initialization routines
10521da177e4SLinus Torvalds  */
10531da177e4SLinus Torvalds 
1054b1e9ed26STakashi Iwai static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
1055b1e9ed26STakashi Iwai 			  unsigned short reg, unsigned long waits)
1056b1e9ed26STakashi Iwai {
1057b1e9ed26STakashi Iwai 	unsigned long timeout = jiffies + waits;
1058b1e9ed26STakashi Iwai 
1059b1e9ed26STakashi Iwai 	outw(FM801_AC97_READ | (codec_id << FM801_AC97_ADDR_SHIFT) | reg,
1060b1e9ed26STakashi Iwai 	     FM801_REG(chip, AC97_CMD));
1061b1e9ed26STakashi Iwai 	udelay(5);
1062b1e9ed26STakashi Iwai 	do {
1063b1e9ed26STakashi Iwai 		if ((inw(FM801_REG(chip, AC97_CMD)) & (FM801_AC97_VALID|FM801_AC97_BUSY))
1064b1e9ed26STakashi Iwai 		    == FM801_AC97_VALID)
1065b1e9ed26STakashi Iwai 			return 0;
1066b1e9ed26STakashi Iwai 		schedule_timeout_uninterruptible(1);
1067b1e9ed26STakashi Iwai 	} while (time_after(timeout, jiffies));
1068b1e9ed26STakashi Iwai 	return -EIO;
1069b1e9ed26STakashi Iwai }
1070b1e9ed26STakashi Iwai 
1071b1e9ed26STakashi Iwai static int snd_fm801_chip_init(struct fm801 *chip, int resume)
1072b1e9ed26STakashi Iwai {
1073b1e9ed26STakashi Iwai 	unsigned short cmdw;
1074b1e9ed26STakashi Iwai 
1075fb716c0bSOndrej Zary 	if (chip->tea575x_tuner & TUNER_ONLY)
1076e0a5d82aSAndy Shevchenko 		goto __ac97_ok;
1077e0a5d82aSAndy Shevchenko 
1078b1e9ed26STakashi Iwai 	/* codec cold reset + AC'97 warm reset */
1079b1e9ed26STakashi Iwai 	outw((1<<5) | (1<<6), FM801_REG(chip, CODEC_CTRL));
1080b1e9ed26STakashi Iwai 	inw(FM801_REG(chip, CODEC_CTRL)); /* flush posting data */
1081b1e9ed26STakashi Iwai 	udelay(100);
1082b1e9ed26STakashi Iwai 	outw(0, FM801_REG(chip, CODEC_CTRL));
1083b1e9ed26STakashi Iwai 
1084fb716c0bSOndrej Zary 	if (wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750)) < 0)
1085fb716c0bSOndrej Zary 		if (!resume) {
1086fb716c0bSOndrej Zary 			snd_printk(KERN_INFO "Primary AC'97 codec not found, "
1087fb716c0bSOndrej Zary 					    "assume SF64-PCR (tuner-only)\n");
1088fb716c0bSOndrej Zary 			chip->tea575x_tuner = 3 | TUNER_ONLY;
1089fb716c0bSOndrej Zary 			goto __ac97_ok;
1090b1e9ed26STakashi Iwai 		}
1091b1e9ed26STakashi Iwai 
1092b1e9ed26STakashi Iwai 	if (chip->multichannel) {
1093b1e9ed26STakashi Iwai 		if (chip->secondary_addr) {
1094b1e9ed26STakashi Iwai 			wait_for_codec(chip, chip->secondary_addr,
1095b1e9ed26STakashi Iwai 				       AC97_VENDOR_ID1, msecs_to_jiffies(50));
1096b1e9ed26STakashi Iwai 		} else {
1097b1e9ed26STakashi Iwai 			/* my card has the secondary codec */
1098b1e9ed26STakashi Iwai 			/* at address #3, so the loop is inverted */
109958e4334eSHarvey Harrison 			int i;
110058e4334eSHarvey Harrison 			for (i = 3; i > 0; i--) {
110158e4334eSHarvey Harrison 				if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
1102b1e9ed26STakashi Iwai 						     msecs_to_jiffies(50))) {
1103b1e9ed26STakashi Iwai 					cmdw = inw(FM801_REG(chip, AC97_DATA));
1104b1e9ed26STakashi Iwai 					if (cmdw != 0xffff && cmdw != 0) {
1105b1e9ed26STakashi Iwai 						chip->secondary = 1;
110658e4334eSHarvey Harrison 						chip->secondary_addr = i;
1107b1e9ed26STakashi Iwai 						break;
1108b1e9ed26STakashi Iwai 					}
1109b1e9ed26STakashi Iwai 				}
1110b1e9ed26STakashi Iwai 			}
1111b1e9ed26STakashi Iwai 		}
1112b1e9ed26STakashi Iwai 
1113b1e9ed26STakashi Iwai 		/* the recovery phase, it seems that probing for non-existing codec might */
1114b1e9ed26STakashi Iwai 		/* cause timeout problems */
1115b1e9ed26STakashi Iwai 		wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
1116b1e9ed26STakashi Iwai 	}
1117b1e9ed26STakashi Iwai 
11186bbe13ecSJaroslav Kysela       __ac97_ok:
11196bbe13ecSJaroslav Kysela 
1120b1e9ed26STakashi Iwai 	/* init volume */
1121b1e9ed26STakashi Iwai 	outw(0x0808, FM801_REG(chip, PCM_VOL));
1122b1e9ed26STakashi Iwai 	outw(0x9f1f, FM801_REG(chip, FM_VOL));
1123b1e9ed26STakashi Iwai 	outw(0x8808, FM801_REG(chip, I2S_VOL));
1124b1e9ed26STakashi Iwai 
1125b1e9ed26STakashi Iwai 	/* I2S control - I2S mode */
1126b1e9ed26STakashi Iwai 	outw(0x0003, FM801_REG(chip, I2S_MODE));
1127b1e9ed26STakashi Iwai 
11286bbe13ecSJaroslav Kysela 	/* interrupt setup */
1129b1e9ed26STakashi Iwai 	cmdw = inw(FM801_REG(chip, IRQ_MASK));
11306bbe13ecSJaroslav Kysela 	if (chip->irq < 0)
11316bbe13ecSJaroslav Kysela 		cmdw |= 0x00c3;		/* mask everything, no PCM nor MPU */
11326bbe13ecSJaroslav Kysela 	else
11336bbe13ecSJaroslav Kysela 		cmdw &= ~0x0083;	/* unmask MPU, PLAYBACK & CAPTURE */
1134b1e9ed26STakashi Iwai 	outw(cmdw, FM801_REG(chip, IRQ_MASK));
1135b1e9ed26STakashi Iwai 
1136b1e9ed26STakashi Iwai 	/* interrupt clear */
1137b1e9ed26STakashi Iwai 	outw(FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU, FM801_REG(chip, IRQ_STATUS));
1138b1e9ed26STakashi Iwai 
1139b1e9ed26STakashi Iwai 	return 0;
1140b1e9ed26STakashi Iwai }
1141b1e9ed26STakashi Iwai 
1142b1e9ed26STakashi Iwai 
1143a5f22156STakashi Iwai static int snd_fm801_free(struct fm801 *chip)
11441da177e4SLinus Torvalds {
11451da177e4SLinus Torvalds 	unsigned short cmdw;
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	if (chip->irq < 0)
11481da177e4SLinus Torvalds 		goto __end_hw;
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	/* interrupt setup - mask everything */
11511da177e4SLinus Torvalds 	cmdw = inw(FM801_REG(chip, IRQ_MASK));
11521da177e4SLinus Torvalds 	cmdw |= 0x00c3;
11531da177e4SLinus Torvalds 	outw(cmdw, FM801_REG(chip, IRQ_MASK));
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds       __end_hw:
1156fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1157c37279b9SBen Hutchings 	if (!(chip->tea575x_tuner & TUNER_DISABLED))
11581da177e4SLinus Torvalds 		snd_tea575x_exit(&chip->tea);
11591da177e4SLinus Torvalds #endif
11601da177e4SLinus Torvalds 	if (chip->irq >= 0)
1161a5f22156STakashi Iwai 		free_irq(chip->irq, chip);
11621da177e4SLinus Torvalds 	pci_release_regions(chip->pci);
11631da177e4SLinus Torvalds 	pci_disable_device(chip->pci);
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	kfree(chip);
11661da177e4SLinus Torvalds 	return 0;
11671da177e4SLinus Torvalds }
11681da177e4SLinus Torvalds 
1169a5f22156STakashi Iwai static int snd_fm801_dev_free(struct snd_device *device)
11701da177e4SLinus Torvalds {
1171a5f22156STakashi Iwai 	struct fm801 *chip = device->device_data;
11721da177e4SLinus Torvalds 	return snd_fm801_free(chip);
11731da177e4SLinus Torvalds }
11741da177e4SLinus Torvalds 
1175a5f22156STakashi Iwai static int __devinit snd_fm801_create(struct snd_card *card,
11761da177e4SLinus Torvalds 				      struct pci_dev * pci,
11771da177e4SLinus Torvalds 				      int tea575x_tuner,
1178a5f22156STakashi Iwai 				      struct fm801 ** rchip)
11791da177e4SLinus Torvalds {
1180a5f22156STakashi Iwai 	struct fm801 *chip;
11811da177e4SLinus Torvalds 	int err;
1182a5f22156STakashi Iwai 	static struct snd_device_ops ops = {
11831da177e4SLinus Torvalds 		.dev_free =	snd_fm801_dev_free,
11841da177e4SLinus Torvalds 	};
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	*rchip = NULL;
11871da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
11881da177e4SLinus Torvalds 		return err;
1189e560d8d8STakashi Iwai 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
11901da177e4SLinus Torvalds 	if (chip == NULL) {
11911da177e4SLinus Torvalds 		pci_disable_device(pci);
11921da177e4SLinus Torvalds 		return -ENOMEM;
11931da177e4SLinus Torvalds 	}
11941da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
11951da177e4SLinus Torvalds 	chip->card = card;
11961da177e4SLinus Torvalds 	chip->pci = pci;
11971da177e4SLinus Torvalds 	chip->irq = -1;
11986bbe13ecSJaroslav Kysela 	chip->tea575x_tuner = tea575x_tuner;
11991da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, "FM801")) < 0) {
12001da177e4SLinus Torvalds 		kfree(chip);
12011da177e4SLinus Torvalds 		pci_disable_device(pci);
12021da177e4SLinus Torvalds 		return err;
12031da177e4SLinus Torvalds 	}
12041da177e4SLinus Torvalds 	chip->port = pci_resource_start(pci, 0);
1205fb716c0bSOndrej Zary 	if ((tea575x_tuner & TUNER_ONLY) == 0) {
1206437a5a46STakashi Iwai 		if (request_irq(pci->irq, snd_fm801_interrupt, IRQF_SHARED,
1207934c2b6dSTakashi Iwai 				KBUILD_MODNAME, chip)) {
120899b359baSTakashi Iwai 			snd_printk(KERN_ERR "unable to grab IRQ %d\n", chip->irq);
12091da177e4SLinus Torvalds 			snd_fm801_free(chip);
12101da177e4SLinus Torvalds 			return -EBUSY;
12111da177e4SLinus Torvalds 		}
12121da177e4SLinus Torvalds 		chip->irq = pci->irq;
12131da177e4SLinus Torvalds 		pci_set_master(pci);
12146bbe13ecSJaroslav Kysela 	}
12151da177e4SLinus Torvalds 
121644c10138SAuke Kok 	if (pci->revision >= 0xb1)	/* FM801-AU */
12171da177e4SLinus Torvalds 		chip->multichannel = 1;
12181da177e4SLinus Torvalds 
1219b1e9ed26STakashi Iwai 	snd_fm801_chip_init(chip, 0);
1220fb716c0bSOndrej Zary 	/* init might set tuner access method */
1221fb716c0bSOndrej Zary 	tea575x_tuner = chip->tea575x_tuner;
1222fb716c0bSOndrej Zary 
1223fb716c0bSOndrej Zary 	if (chip->irq >= 0 && (tea575x_tuner & TUNER_ONLY)) {
1224fb716c0bSOndrej Zary 		pci_clear_master(pci);
1225fb716c0bSOndrej Zary 		free_irq(chip->irq, chip);
1226fb716c0bSOndrej Zary 		chip->irq = -1;
1227fb716c0bSOndrej Zary 	}
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
12301da177e4SLinus Torvalds 		snd_fm801_free(chip);
12311da177e4SLinus Torvalds 		return err;
12321da177e4SLinus Torvalds 	}
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds 	snd_card_set_dev(card, &pci->dev);
12351da177e4SLinus Torvalds 
1236fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
12371da177e4SLinus Torvalds 	chip->tea.private_data = chip;
1238938a1566SOndrej Zary 	chip->tea.ops = &snd_fm801_tea_ops;
123910ca7201SOndrej Zary 	sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
1240d7ba858aSOndrej Zary 	if ((tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
1241d7ba858aSOndrej Zary 	    (tea575x_tuner & TUNER_TYPE_MASK) < 4) {
124296760015SDan Carpenter 		if (snd_tea575x_init(&chip->tea)) {
1243d7ba858aSOndrej Zary 			snd_printk(KERN_ERR "TEA575x radio not found\n");
124496760015SDan Carpenter 			return -ENODEV;
124596760015SDan Carpenter 		}
124696760015SDan Carpenter 	} else if ((tea575x_tuner & TUNER_TYPE_MASK) == 0) {
1247d7ba858aSOndrej Zary 		/* autodetect tuner connection */
1248d7ba858aSOndrej Zary 		for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
1249d7ba858aSOndrej Zary 			chip->tea575x_tuner = tea575x_tuner;
1250d7ba858aSOndrej Zary 			if (!snd_tea575x_init(&chip->tea)) {
1251d7ba858aSOndrej Zary 				snd_printk(KERN_INFO "detected TEA575x radio type %s\n",
12528e699d2cSTakashi Iwai 					   get_tea575x_gpio(chip)->name);
1253d7ba858aSOndrej Zary 				break;
1254d7ba858aSOndrej Zary 			}
12551da177e4SLinus Torvalds 		}
125696760015SDan Carpenter 		if (tea575x_tuner == 4) {
125796760015SDan Carpenter 			snd_printk(KERN_ERR "TEA575x radio not found\n");
1258c37279b9SBen Hutchings 			chip->tea575x_tuner = TUNER_DISABLED;
125996760015SDan Carpenter 		}
126096760015SDan Carpenter 	}
1261c37279b9SBen Hutchings 	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
12628e699d2cSTakashi Iwai 		strlcpy(chip->tea.card, get_tea575x_gpio(chip)->name,
1263c37279b9SBen Hutchings 			sizeof(chip->tea.card));
1264c37279b9SBen Hutchings 	}
12651da177e4SLinus Torvalds #endif
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	*rchip = chip;
12681da177e4SLinus Torvalds 	return 0;
12691da177e4SLinus Torvalds }
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds static int __devinit snd_card_fm801_probe(struct pci_dev *pci,
12721da177e4SLinus Torvalds 					  const struct pci_device_id *pci_id)
12731da177e4SLinus Torvalds {
12741da177e4SLinus Torvalds 	static int dev;
1275a5f22156STakashi Iwai 	struct snd_card *card;
1276a5f22156STakashi Iwai 	struct fm801 *chip;
1277a5f22156STakashi Iwai 	struct snd_opl3 *opl3;
12781da177e4SLinus Torvalds 	int err;
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds         if (dev >= SNDRV_CARDS)
12811da177e4SLinus Torvalds                 return -ENODEV;
12821da177e4SLinus Torvalds 	if (!enable[dev]) {
12831da177e4SLinus Torvalds 		dev++;
12841da177e4SLinus Torvalds 		return -ENOENT;
12851da177e4SLinus Torvalds 	}
12861da177e4SLinus Torvalds 
1287e58de7baSTakashi Iwai 	err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
1288e58de7baSTakashi Iwai 	if (err < 0)
1289e58de7baSTakashi Iwai 		return err;
12901da177e4SLinus Torvalds 	if ((err = snd_fm801_create(card, pci, tea575x_tuner[dev], &chip)) < 0) {
12911da177e4SLinus Torvalds 		snd_card_free(card);
12921da177e4SLinus Torvalds 		return err;
12931da177e4SLinus Torvalds 	}
1294b1e9ed26STakashi Iwai 	card->private_data = chip;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	strcpy(card->driver, "FM801");
12971da177e4SLinus Torvalds 	strcpy(card->shortname, "ForteMedia FM801-");
12981da177e4SLinus Torvalds 	strcat(card->shortname, chip->multichannel ? "AU" : "AS");
12991da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %i",
13001da177e4SLinus Torvalds 		card->shortname, chip->port, chip->irq);
13011da177e4SLinus Torvalds 
1302fb716c0bSOndrej Zary 	if (chip->tea575x_tuner & TUNER_ONLY)
1303e0a5d82aSAndy Shevchenko 		goto __fm801_tuner_only;
1304e0a5d82aSAndy Shevchenko 
13051da177e4SLinus Torvalds 	if ((err = snd_fm801_pcm(chip, 0, NULL)) < 0) {
13061da177e4SLinus Torvalds 		snd_card_free(card);
13071da177e4SLinus Torvalds 		return err;
13081da177e4SLinus Torvalds 	}
13091da177e4SLinus Torvalds 	if ((err = snd_fm801_mixer(chip)) < 0) {
13101da177e4SLinus Torvalds 		snd_card_free(card);
13111da177e4SLinus Torvalds 		return err;
13121da177e4SLinus Torvalds 	}
13131da177e4SLinus Torvalds 	if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1314302e4c2fSTakashi Iwai 				       FM801_REG(chip, MPU401_DATA),
1315dba8b469SClemens Ladisch 				       MPU401_INFO_INTEGRATED |
1316dba8b469SClemens Ladisch 				       MPU401_INFO_IRQ_HOOK,
1317dba8b469SClemens Ladisch 				       -1, &chip->rmidi)) < 0) {
13181da177e4SLinus Torvalds 		snd_card_free(card);
13191da177e4SLinus Torvalds 		return err;
13201da177e4SLinus Torvalds 	}
13211da177e4SLinus Torvalds 	if ((err = snd_opl3_create(card, FM801_REG(chip, OPL3_BANK0),
13221da177e4SLinus Torvalds 				   FM801_REG(chip, OPL3_BANK1),
13231da177e4SLinus Torvalds 				   OPL3_HW_OPL3_FM801, 1, &opl3)) < 0) {
13241da177e4SLinus Torvalds 		snd_card_free(card);
13251da177e4SLinus Torvalds 		return err;
13261da177e4SLinus Torvalds 	}
13271da177e4SLinus Torvalds 	if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
13281da177e4SLinus Torvalds 		snd_card_free(card);
13291da177e4SLinus Torvalds 		return err;
13301da177e4SLinus Torvalds 	}
13311da177e4SLinus Torvalds 
1332e0a5d82aSAndy Shevchenko       __fm801_tuner_only:
13331da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
13341da177e4SLinus Torvalds 		snd_card_free(card);
13351da177e4SLinus Torvalds 		return err;
13361da177e4SLinus Torvalds 	}
13371da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
13381da177e4SLinus Torvalds 	dev++;
13391da177e4SLinus Torvalds 	return 0;
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds static void __devexit snd_card_fm801_remove(struct pci_dev *pci)
13431da177e4SLinus Torvalds {
13441da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
13451da177e4SLinus Torvalds 	pci_set_drvdata(pci, NULL);
13461da177e4SLinus Torvalds }
13471da177e4SLinus Torvalds 
1348b1e9ed26STakashi Iwai #ifdef CONFIG_PM
1349b1e9ed26STakashi Iwai static unsigned char saved_regs[] = {
1350b1e9ed26STakashi Iwai 	FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
1351b1e9ed26STakashi Iwai 	FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
1352b1e9ed26STakashi Iwai 	FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
1353b1e9ed26STakashi Iwai 	FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
1354b1e9ed26STakashi Iwai };
1355b1e9ed26STakashi Iwai 
1356b1e9ed26STakashi Iwai static int snd_fm801_suspend(struct pci_dev *pci, pm_message_t state)
1357b1e9ed26STakashi Iwai {
1358b1e9ed26STakashi Iwai 	struct snd_card *card = pci_get_drvdata(pci);
1359b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1360b1e9ed26STakashi Iwai 	int i;
1361b1e9ed26STakashi Iwai 
1362b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1363b1e9ed26STakashi Iwai 	snd_pcm_suspend_all(chip->pcm);
1364b1e9ed26STakashi Iwai 	snd_ac97_suspend(chip->ac97);
1365b1e9ed26STakashi Iwai 	snd_ac97_suspend(chip->ac97_sec);
1366b1e9ed26STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1367b1e9ed26STakashi Iwai 		chip->saved_regs[i] = inw(chip->port + saved_regs[i]);
1368b1e9ed26STakashi Iwai 	/* FIXME: tea575x suspend */
1369b1e9ed26STakashi Iwai 
1370b1e9ed26STakashi Iwai 	pci_disable_device(pci);
1371b1e9ed26STakashi Iwai 	pci_save_state(pci);
137230b35399STakashi Iwai 	pci_set_power_state(pci, pci_choose_state(pci, state));
1373b1e9ed26STakashi Iwai 	return 0;
1374b1e9ed26STakashi Iwai }
1375b1e9ed26STakashi Iwai 
1376b1e9ed26STakashi Iwai static int snd_fm801_resume(struct pci_dev *pci)
1377b1e9ed26STakashi Iwai {
1378b1e9ed26STakashi Iwai 	struct snd_card *card = pci_get_drvdata(pci);
1379b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1380b1e9ed26STakashi Iwai 	int i;
1381b1e9ed26STakashi Iwai 
1382b1e9ed26STakashi Iwai 	pci_set_power_state(pci, PCI_D0);
138330b35399STakashi Iwai 	pci_restore_state(pci);
138430b35399STakashi Iwai 	if (pci_enable_device(pci) < 0) {
138530b35399STakashi Iwai 		printk(KERN_ERR "fm801: pci_enable_device failed, "
138630b35399STakashi Iwai 		       "disabling device\n");
138730b35399STakashi Iwai 		snd_card_disconnect(card);
138830b35399STakashi Iwai 		return -EIO;
138930b35399STakashi Iwai 	}
1390b1e9ed26STakashi Iwai 	pci_set_master(pci);
1391b1e9ed26STakashi Iwai 
1392b1e9ed26STakashi Iwai 	snd_fm801_chip_init(chip, 1);
1393b1e9ed26STakashi Iwai 	snd_ac97_resume(chip->ac97);
1394b1e9ed26STakashi Iwai 	snd_ac97_resume(chip->ac97_sec);
1395b1e9ed26STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1396b1e9ed26STakashi Iwai 		outw(chip->saved_regs[i], chip->port + saved_regs[i]);
1397b1e9ed26STakashi Iwai 
1398b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1399b1e9ed26STakashi Iwai 	return 0;
1400b1e9ed26STakashi Iwai }
1401b1e9ed26STakashi Iwai #endif
1402b1e9ed26STakashi Iwai 
14031da177e4SLinus Torvalds static struct pci_driver driver = {
14043733e424STakashi Iwai 	.name = KBUILD_MODNAME,
14051da177e4SLinus Torvalds 	.id_table = snd_fm801_ids,
14061da177e4SLinus Torvalds 	.probe = snd_card_fm801_probe,
14071da177e4SLinus Torvalds 	.remove = __devexit_p(snd_card_fm801_remove),
1408b1e9ed26STakashi Iwai #ifdef CONFIG_PM
1409b1e9ed26STakashi Iwai 	.suspend = snd_fm801_suspend,
1410b1e9ed26STakashi Iwai 	.resume = snd_fm801_resume,
1411b1e9ed26STakashi Iwai #endif
14121da177e4SLinus Torvalds };
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds static int __init alsa_card_fm801_init(void)
14151da177e4SLinus Torvalds {
141601d25d46STakashi Iwai 	return pci_register_driver(&driver);
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds static void __exit alsa_card_fm801_exit(void)
14201da177e4SLinus Torvalds {
14211da177e4SLinus Torvalds 	pci_unregister_driver(&driver);
14221da177e4SLinus Torvalds }
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds module_init(alsa_card_fm801_init)
14251da177e4SLinus Torvalds module_exit(alsa_card_fm801_exit)
1426