xref: /linux/sound/pci/fm801.c (revision 437a5a4606c12ab904793a7cad5b2062fc76c04e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  The driver for the ForteMedia FM801 based soundcards
31da177e4SLinus Torvalds  *  Copyright (c) by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
241da177e4SLinus Torvalds #include <linux/delay.h>
251da177e4SLinus Torvalds #include <linux/init.h>
261da177e4SLinus Torvalds #include <linux/interrupt.h>
271da177e4SLinus Torvalds #include <linux/pci.h>
281da177e4SLinus Torvalds #include <linux/slab.h>
291da177e4SLinus Torvalds #include <linux/moduleparam.h>
301da177e4SLinus Torvalds #include <sound/core.h>
311da177e4SLinus Torvalds #include <sound/pcm.h>
32666c70ffSTakashi Iwai #include <sound/tlv.h>
331da177e4SLinus Torvalds #include <sound/ac97_codec.h>
341da177e4SLinus Torvalds #include <sound/mpu401.h>
351da177e4SLinus Torvalds #include <sound/opl3.h>
361da177e4SLinus Torvalds #include <sound/initval.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #include <asm/io.h>
391da177e4SLinus Torvalds 
40efce4bb9SAdrian Bunk #ifdef CONFIG_SND_FM801_TEA575X_BOOL
411da177e4SLinus Torvalds #include <sound/tea575x-tuner.h>
421da177e4SLinus Torvalds #define TEA575X_RADIO 1
431da177e4SLinus Torvalds #endif
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
461da177e4SLinus Torvalds MODULE_DESCRIPTION("ForteMedia FM801");
471da177e4SLinus Torvalds MODULE_LICENSE("GPL");
481da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{ForteMedia,FM801},"
491da177e4SLinus Torvalds 		"{Genius,SoundMaker Live 5.1}}");
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
521da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
531da177e4SLinus Torvalds static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
541da177e4SLinus Torvalds /*
551da177e4SLinus Torvalds  *  Enable TEA575x tuner
561da177e4SLinus Torvalds  *    1 = MediaForte 256-PCS
571da177e4SLinus Torvalds  *    2 = MediaForte 256-PCPR
581da177e4SLinus Torvalds  *    3 = MediaForte 64-PCR
59e0a5d82aSAndy Shevchenko  *   16 = setup tuner only (this is additional bit), i.e. SF-64-PCR FM card
601da177e4SLinus Torvalds  *  High 16-bits are video (radio) device number + 1
611da177e4SLinus Torvalds  */
626581f4e7STakashi Iwai static int tea575x_tuner[SNDRV_CARDS];
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
651da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
661da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
671da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
681da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
691da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
701da177e4SLinus Torvalds module_param_array(tea575x_tuner, int, NULL, 0444);
711da177e4SLinus Torvalds MODULE_PARM_DESC(tea575x_tuner, "Enable TEA575x tuner.");
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /*
741da177e4SLinus Torvalds  *  Direct registers
751da177e4SLinus Torvalds  */
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds #define FM801_REG(chip, reg)	(chip->port + FM801_##reg)
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds #define FM801_PCM_VOL		0x00	/* PCM Output Volume */
801da177e4SLinus Torvalds #define FM801_FM_VOL		0x02	/* FM Output Volume */
811da177e4SLinus Torvalds #define FM801_I2S_VOL		0x04	/* I2S Volume */
821da177e4SLinus Torvalds #define FM801_REC_SRC		0x06	/* Record Source */
831da177e4SLinus Torvalds #define FM801_PLY_CTRL		0x08	/* Playback Control */
841da177e4SLinus Torvalds #define FM801_PLY_COUNT		0x0a	/* Playback Count */
851da177e4SLinus Torvalds #define FM801_PLY_BUF1		0x0c	/* Playback Bufer I */
861da177e4SLinus Torvalds #define FM801_PLY_BUF2		0x10	/* Playback Buffer II */
871da177e4SLinus Torvalds #define FM801_CAP_CTRL		0x14	/* Capture Control */
881da177e4SLinus Torvalds #define FM801_CAP_COUNT		0x16	/* Capture Count */
891da177e4SLinus Torvalds #define FM801_CAP_BUF1		0x18	/* Capture Buffer I */
901da177e4SLinus Torvalds #define FM801_CAP_BUF2		0x1c	/* Capture Buffer II */
911da177e4SLinus Torvalds #define FM801_CODEC_CTRL	0x22	/* Codec Control */
921da177e4SLinus Torvalds #define FM801_I2S_MODE		0x24	/* I2S Mode Control */
931da177e4SLinus Torvalds #define FM801_VOLUME		0x26	/* Volume Up/Down/Mute Status */
941da177e4SLinus Torvalds #define FM801_I2C_CTRL		0x29	/* I2C Control */
951da177e4SLinus Torvalds #define FM801_AC97_CMD		0x2a	/* AC'97 Command */
961da177e4SLinus Torvalds #define FM801_AC97_DATA		0x2c	/* AC'97 Data */
971da177e4SLinus Torvalds #define FM801_MPU401_DATA	0x30	/* MPU401 Data */
981da177e4SLinus Torvalds #define FM801_MPU401_CMD	0x31	/* MPU401 Command */
991da177e4SLinus Torvalds #define FM801_GPIO_CTRL		0x52	/* General Purpose I/O Control */
1001da177e4SLinus Torvalds #define FM801_GEN_CTRL		0x54	/* General Control */
1011da177e4SLinus Torvalds #define FM801_IRQ_MASK		0x56	/* Interrupt Mask */
1021da177e4SLinus Torvalds #define FM801_IRQ_STATUS	0x5a	/* Interrupt Status */
1031da177e4SLinus Torvalds #define FM801_OPL3_BANK0	0x68	/* OPL3 Status Read / Bank 0 Write */
1041da177e4SLinus Torvalds #define FM801_OPL3_DATA0	0x69	/* OPL3 Data 0 Write */
1051da177e4SLinus Torvalds #define FM801_OPL3_BANK1	0x6a	/* OPL3 Bank 1 Write */
1061da177e4SLinus Torvalds #define FM801_OPL3_DATA1	0x6b	/* OPL3 Bank 1 Write */
1071da177e4SLinus Torvalds #define FM801_POWERDOWN		0x70	/* Blocks Power Down Control */
1081da177e4SLinus Torvalds 
109b1e9ed26STakashi Iwai /* codec access */
110b1e9ed26STakashi Iwai #define FM801_AC97_READ		(1<<7)	/* read=1, write=0 */
111b1e9ed26STakashi Iwai #define FM801_AC97_VALID	(1<<8)	/* port valid=1 */
112b1e9ed26STakashi Iwai #define FM801_AC97_BUSY		(1<<9)	/* busy=1 */
113b1e9ed26STakashi Iwai #define FM801_AC97_ADDR_SHIFT	10	/* codec id (2bit) */
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /* playback and record control register bits */
1161da177e4SLinus Torvalds #define FM801_BUF1_LAST		(1<<1)
1171da177e4SLinus Torvalds #define FM801_BUF2_LAST		(1<<2)
1181da177e4SLinus Torvalds #define FM801_START		(1<<5)
1191da177e4SLinus Torvalds #define FM801_PAUSE		(1<<6)
1201da177e4SLinus Torvalds #define FM801_IMMED_STOP	(1<<7)
1211da177e4SLinus Torvalds #define FM801_RATE_SHIFT	8
1221da177e4SLinus Torvalds #define FM801_RATE_MASK		(15 << FM801_RATE_SHIFT)
1231da177e4SLinus Torvalds #define FM801_CHANNELS_4	(1<<12)	/* playback only */
1241da177e4SLinus Torvalds #define FM801_CHANNELS_6	(2<<12)	/* playback only */
1251da177e4SLinus Torvalds #define FM801_CHANNELS_6MS	(3<<12)	/* playback only */
1261da177e4SLinus Torvalds #define FM801_CHANNELS_MASK	(3<<12)
1271da177e4SLinus Torvalds #define FM801_16BIT		(1<<14)
1281da177e4SLinus Torvalds #define FM801_STEREO		(1<<15)
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds /* IRQ status bits */
1311da177e4SLinus Torvalds #define FM801_IRQ_PLAYBACK	(1<<8)
1321da177e4SLinus Torvalds #define FM801_IRQ_CAPTURE	(1<<9)
1331da177e4SLinus Torvalds #define FM801_IRQ_VOLUME	(1<<14)
1341da177e4SLinus Torvalds #define FM801_IRQ_MPU		(1<<15)
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds /* GPIO control register */
1371da177e4SLinus Torvalds #define FM801_GPIO_GP0		(1<<0)	/* read/write */
1381da177e4SLinus Torvalds #define FM801_GPIO_GP1		(1<<1)
1391da177e4SLinus Torvalds #define FM801_GPIO_GP2		(1<<2)
1401da177e4SLinus Torvalds #define FM801_GPIO_GP3		(1<<3)
1411da177e4SLinus Torvalds #define FM801_GPIO_GP(x)	(1<<(0+(x)))
1421da177e4SLinus Torvalds #define FM801_GPIO_GD0		(1<<8)	/* directions: 1 = input, 0 = output*/
1431da177e4SLinus Torvalds #define FM801_GPIO_GD1		(1<<9)
1441da177e4SLinus Torvalds #define FM801_GPIO_GD2		(1<<10)
1451da177e4SLinus Torvalds #define FM801_GPIO_GD3		(1<<11)
1461da177e4SLinus Torvalds #define FM801_GPIO_GD(x)	(1<<(8+(x)))
1471da177e4SLinus Torvalds #define FM801_GPIO_GS0		(1<<12)	/* function select: */
1481da177e4SLinus Torvalds #define FM801_GPIO_GS1		(1<<13)	/*    1 = GPIO */
1491da177e4SLinus Torvalds #define FM801_GPIO_GS2		(1<<14)	/*    0 = other (S/PDIF, VOL) */
1501da177e4SLinus Torvalds #define FM801_GPIO_GS3		(1<<15)
1511da177e4SLinus Torvalds #define FM801_GPIO_GS(x)	(1<<(12+(x)))
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds /*
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds  */
1561da177e4SLinus Torvalds 
157a5f22156STakashi Iwai struct fm801 {
1581da177e4SLinus Torvalds 	int irq;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	unsigned long port;	/* I/O port number */
1611da177e4SLinus Torvalds 	unsigned int multichannel: 1,	/* multichannel support */
1621da177e4SLinus Torvalds 		     secondary: 1;	/* secondary codec */
1631da177e4SLinus Torvalds 	unsigned char secondary_addr;	/* address of the secondary codec */
1646bbe13ecSJaroslav Kysela 	unsigned int tea575x_tuner;	/* tuner flags */
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	unsigned short ply_ctrl; /* playback control */
1671da177e4SLinus Torvalds 	unsigned short cap_ctrl; /* capture control */
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	unsigned long ply_buffer;
1701da177e4SLinus Torvalds 	unsigned int ply_buf;
1711da177e4SLinus Torvalds 	unsigned int ply_count;
1721da177e4SLinus Torvalds 	unsigned int ply_size;
1731da177e4SLinus Torvalds 	unsigned int ply_pos;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	unsigned long cap_buffer;
1761da177e4SLinus Torvalds 	unsigned int cap_buf;
1771da177e4SLinus Torvalds 	unsigned int cap_count;
1781da177e4SLinus Torvalds 	unsigned int cap_size;
1791da177e4SLinus Torvalds 	unsigned int cap_pos;
1801da177e4SLinus Torvalds 
181a5f22156STakashi Iwai 	struct snd_ac97_bus *ac97_bus;
182a5f22156STakashi Iwai 	struct snd_ac97 *ac97;
183a5f22156STakashi Iwai 	struct snd_ac97 *ac97_sec;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	struct pci_dev *pci;
186a5f22156STakashi Iwai 	struct snd_card *card;
187a5f22156STakashi Iwai 	struct snd_pcm *pcm;
188a5f22156STakashi Iwai 	struct snd_rawmidi *rmidi;
189a5f22156STakashi Iwai 	struct snd_pcm_substream *playback_substream;
190a5f22156STakashi Iwai 	struct snd_pcm_substream *capture_substream;
1911da177e4SLinus Torvalds 	unsigned int p_dma_size;
1921da177e4SLinus Torvalds 	unsigned int c_dma_size;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	spinlock_t reg_lock;
195a5f22156STakashi Iwai 	struct snd_info_entry *proc_entry;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds #ifdef TEA575X_RADIO
198a5f22156STakashi Iwai 	struct snd_tea575x tea;
1991da177e4SLinus Torvalds #endif
200b1e9ed26STakashi Iwai 
201b1e9ed26STakashi Iwai #ifdef CONFIG_PM
202b1e9ed26STakashi Iwai 	u16 saved_regs[0x20];
203b1e9ed26STakashi Iwai #endif
2041da177e4SLinus Torvalds };
2051da177e4SLinus Torvalds 
206f40b6890STakashi Iwai static struct pci_device_id snd_fm801_ids[] = {
2071da177e4SLinus Torvalds 	{ 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* FM801 */
20826be8659SSergey Vlasov 	{ 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* Gallant Odyssey Sound 4 */
2091da177e4SLinus Torvalds 	{ 0, }
2101da177e4SLinus Torvalds };
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds /*
2151da177e4SLinus Torvalds  *  common I/O routines
2161da177e4SLinus Torvalds  */
2171da177e4SLinus Torvalds 
218a5f22156STakashi Iwai static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
2191da177e4SLinus Torvalds 				 unsigned short mask, unsigned short value)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	int change;
2221da177e4SLinus Torvalds 	unsigned long flags;
2231da177e4SLinus Torvalds 	unsigned short old, new;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
2261da177e4SLinus Torvalds 	old = inw(chip->port + reg);
2271da177e4SLinus Torvalds 	new = (old & ~mask) | value;
2281da177e4SLinus Torvalds 	change = old != new;
2291da177e4SLinus Torvalds 	if (change)
2301da177e4SLinus Torvalds 		outw(new, chip->port + reg);
2311da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
2321da177e4SLinus Torvalds 	return change;
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds 
235a5f22156STakashi Iwai static void snd_fm801_codec_write(struct snd_ac97 *ac97,
2361da177e4SLinus Torvalds 				  unsigned short reg,
2371da177e4SLinus Torvalds 				  unsigned short val)
2381da177e4SLinus Torvalds {
239a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
2401da177e4SLinus Torvalds 	int idx;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	/*
2431da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
2441da177e4SLinus Torvalds 	 */
2451da177e4SLinus Torvalds 	for (idx = 0; idx < 100; idx++) {
246b1e9ed26STakashi Iwai 		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
2471da177e4SLinus Torvalds 			goto ok1;
2481da177e4SLinus Torvalds 		udelay(10);
2491da177e4SLinus Torvalds 	}
25099b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface is busy (1)\n");
2511da177e4SLinus Torvalds 	return;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds  ok1:
2541da177e4SLinus Torvalds 	/* write data and address */
2551da177e4SLinus Torvalds 	outw(val, FM801_REG(chip, AC97_DATA));
2561da177e4SLinus Torvalds 	outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
2571da177e4SLinus Torvalds 	/*
2581da177e4SLinus Torvalds 	 *  Wait until the write command is not completed..
2591da177e4SLinus Torvalds          */
2601da177e4SLinus Torvalds 	for (idx = 0; idx < 1000; idx++) {
261b1e9ed26STakashi Iwai 		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
2621da177e4SLinus Torvalds 			return;
2631da177e4SLinus Torvalds 		udelay(10);
2641da177e4SLinus Torvalds 	}
26599b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface #%d is busy (2)\n", ac97->num);
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
268a5f22156STakashi Iwai static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
2691da177e4SLinus Torvalds {
270a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
2711da177e4SLinus Torvalds 	int idx;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	/*
2741da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
2751da177e4SLinus Torvalds 	 */
2761da177e4SLinus Torvalds 	for (idx = 0; idx < 100; idx++) {
277b1e9ed26STakashi Iwai 		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
2781da177e4SLinus Torvalds 			goto ok1;
2791da177e4SLinus Torvalds 		udelay(10);
2801da177e4SLinus Torvalds 	}
28199b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface is busy (1)\n");
2821da177e4SLinus Torvalds 	return 0;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds  ok1:
2851da177e4SLinus Torvalds 	/* read command */
286b1e9ed26STakashi Iwai 	outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ,
287b1e9ed26STakashi Iwai 	     FM801_REG(chip, AC97_CMD));
2881da177e4SLinus Torvalds 	for (idx = 0; idx < 100; idx++) {
289b1e9ed26STakashi Iwai 		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
2901da177e4SLinus Torvalds 			goto ok2;
2911da177e4SLinus Torvalds 		udelay(10);
2921da177e4SLinus Torvalds 	}
29399b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface #%d is busy (2)\n", ac97->num);
2941da177e4SLinus Torvalds 	return 0;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds  ok2:
2971da177e4SLinus Torvalds 	for (idx = 0; idx < 1000; idx++) {
298b1e9ed26STakashi Iwai 		if (inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_VALID)
2991da177e4SLinus Torvalds 			goto ok3;
3001da177e4SLinus Torvalds 		udelay(10);
3011da177e4SLinus Torvalds 	}
30299b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface #%d is not valid (2)\n", ac97->num);
3031da177e4SLinus Torvalds 	return 0;
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds  ok3:
3061da177e4SLinus Torvalds 	return inw(FM801_REG(chip, AC97_DATA));
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds static unsigned int rates[] = {
3101da177e4SLinus Torvalds   5500,  8000,  9600, 11025,
3111da177e4SLinus Torvalds   16000, 19200, 22050, 32000,
3121da177e4SLinus Torvalds   38400, 44100, 48000
3131da177e4SLinus Torvalds };
3141da177e4SLinus Torvalds 
315a5f22156STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
3161da177e4SLinus Torvalds 	.count = ARRAY_SIZE(rates),
3171da177e4SLinus Torvalds 	.list = rates,
3181da177e4SLinus Torvalds 	.mask = 0,
3191da177e4SLinus Torvalds };
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds static unsigned int channels[] = {
3221da177e4SLinus Torvalds   2, 4, 6
3231da177e4SLinus Torvalds };
3241da177e4SLinus Torvalds 
325a5f22156STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
3265e4968e2STobias Klauser 	.count = ARRAY_SIZE(channels),
3271da177e4SLinus Torvalds 	.list = channels,
3281da177e4SLinus Torvalds 	.mask = 0,
3291da177e4SLinus Torvalds };
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds /*
3321da177e4SLinus Torvalds  *  Sample rate routines
3331da177e4SLinus Torvalds  */
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds static unsigned short snd_fm801_rate_bits(unsigned int rate)
3361da177e4SLinus Torvalds {
3371da177e4SLinus Torvalds 	unsigned int idx;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
3401da177e4SLinus Torvalds 		if (rates[idx] == rate)
3411da177e4SLinus Torvalds 			return idx;
3421da177e4SLinus Torvalds 	snd_BUG();
3431da177e4SLinus Torvalds 	return ARRAY_SIZE(rates) - 1;
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds /*
3471da177e4SLinus Torvalds  *  PCM part
3481da177e4SLinus Torvalds  */
3491da177e4SLinus Torvalds 
350a5f22156STakashi Iwai static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
3511da177e4SLinus Torvalds 				      int cmd)
3521da177e4SLinus Torvalds {
353a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3561da177e4SLinus Torvalds 	switch (cmd) {
3571da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
3581da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_BUF1_LAST |
3591da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
3601da177e4SLinus Torvalds 				     FM801_PAUSE);
3611da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_START |
3621da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
3631da177e4SLinus Torvalds 		break;
3641da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
3651da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
3661da177e4SLinus Torvalds 		break;
3671da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
368b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
3691da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_PAUSE;
3701da177e4SLinus Torvalds 		break;
3711da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
372b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
3731da177e4SLinus Torvalds 		chip->ply_ctrl &= ~FM801_PAUSE;
3741da177e4SLinus Torvalds 		break;
3751da177e4SLinus Torvalds 	default:
3761da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
3771da177e4SLinus Torvalds 		snd_BUG();
3781da177e4SLinus Torvalds 		return -EINVAL;
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 	outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
3811da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3821da177e4SLinus Torvalds 	return 0;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
385a5f22156STakashi Iwai static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
3861da177e4SLinus Torvalds 				     int cmd)
3871da177e4SLinus Torvalds {
388a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3911da177e4SLinus Torvalds 	switch (cmd) {
3921da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
3931da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_BUF1_LAST |
3941da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
3951da177e4SLinus Torvalds 				     FM801_PAUSE);
3961da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_START |
3971da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
3981da177e4SLinus Torvalds 		break;
3991da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4001da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
4011da177e4SLinus Torvalds 		break;
4021da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
403b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
4041da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_PAUSE;
4051da177e4SLinus Torvalds 		break;
4061da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
407b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
4081da177e4SLinus Torvalds 		chip->cap_ctrl &= ~FM801_PAUSE;
4091da177e4SLinus Torvalds 		break;
4101da177e4SLinus Torvalds 	default:
4111da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
4121da177e4SLinus Torvalds 		snd_BUG();
4131da177e4SLinus Torvalds 		return -EINVAL;
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds 	outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
4161da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4171da177e4SLinus Torvalds 	return 0;
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
420a5f22156STakashi Iwai static int snd_fm801_hw_params(struct snd_pcm_substream *substream,
421a5f22156STakashi Iwai 			       struct snd_pcm_hw_params *hw_params)
4221da177e4SLinus Torvalds {
4231da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
426a5f22156STakashi Iwai static int snd_fm801_hw_free(struct snd_pcm_substream *substream)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
431a5f22156STakashi Iwai static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
4321da177e4SLinus Torvalds {
433a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
434a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
4371da177e4SLinus Torvalds 	chip->ply_count = snd_pcm_lib_period_bytes(substream);
4381da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
4391da177e4SLinus Torvalds 	chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
4401da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK |
4411da177e4SLinus Torvalds 			     FM801_CHANNELS_MASK);
4421da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
4431da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_16BIT;
4441da177e4SLinus Torvalds 	if (runtime->channels > 1) {
4451da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_STEREO;
4461da177e4SLinus Torvalds 		if (runtime->channels == 4)
4471da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_4;
4481da177e4SLinus Torvalds 		else if (runtime->channels == 6)
4491da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_6;
4501da177e4SLinus Torvalds 	}
4511da177e4SLinus Torvalds 	chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
4521da177e4SLinus Torvalds 	chip->ply_buf = 0;
4531da177e4SLinus Torvalds 	outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
4541da177e4SLinus Torvalds 	outw(chip->ply_count - 1, FM801_REG(chip, PLY_COUNT));
4551da177e4SLinus Torvalds 	chip->ply_buffer = runtime->dma_addr;
4561da177e4SLinus Torvalds 	chip->ply_pos = 0;
4571da177e4SLinus Torvalds 	outl(chip->ply_buffer, FM801_REG(chip, PLY_BUF1));
4581da177e4SLinus Torvalds 	outl(chip->ply_buffer + (chip->ply_count % chip->ply_size), FM801_REG(chip, PLY_BUF2));
4591da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
4601da177e4SLinus Torvalds 	return 0;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
463a5f22156STakashi Iwai static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
4641da177e4SLinus Torvalds {
465a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
466a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
4691da177e4SLinus Torvalds 	chip->cap_count = snd_pcm_lib_period_bytes(substream);
4701da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
4711da177e4SLinus Torvalds 	chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
4721da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK);
4731da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
4741da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_16BIT;
4751da177e4SLinus Torvalds 	if (runtime->channels > 1)
4761da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_STEREO;
4771da177e4SLinus Torvalds 	chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
4781da177e4SLinus Torvalds 	chip->cap_buf = 0;
4791da177e4SLinus Torvalds 	outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
4801da177e4SLinus Torvalds 	outw(chip->cap_count - 1, FM801_REG(chip, CAP_COUNT));
4811da177e4SLinus Torvalds 	chip->cap_buffer = runtime->dma_addr;
4821da177e4SLinus Torvalds 	chip->cap_pos = 0;
4831da177e4SLinus Torvalds 	outl(chip->cap_buffer, FM801_REG(chip, CAP_BUF1));
4841da177e4SLinus Torvalds 	outl(chip->cap_buffer + (chip->cap_count % chip->cap_size), FM801_REG(chip, CAP_BUF2));
4851da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
4861da177e4SLinus Torvalds 	return 0;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
489a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
4901da177e4SLinus Torvalds {
491a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
4921da177e4SLinus Torvalds 	size_t ptr;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	if (!(chip->ply_ctrl & FM801_START))
4951da177e4SLinus Torvalds 		return 0;
4961da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
4971da177e4SLinus Torvalds 	ptr = chip->ply_pos + (chip->ply_count - 1) - inw(FM801_REG(chip, PLY_COUNT));
4981da177e4SLinus Torvalds 	if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_PLAYBACK) {
4991da177e4SLinus Torvalds 		ptr += chip->ply_count;
5001da177e4SLinus Torvalds 		ptr %= chip->ply_size;
5011da177e4SLinus Torvalds 	}
5021da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5031da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
506a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
5071da177e4SLinus Torvalds {
508a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
5091da177e4SLinus Torvalds 	size_t ptr;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	if (!(chip->cap_ctrl & FM801_START))
5121da177e4SLinus Torvalds 		return 0;
5131da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
5141da177e4SLinus Torvalds 	ptr = chip->cap_pos + (chip->cap_count - 1) - inw(FM801_REG(chip, CAP_COUNT));
5151da177e4SLinus Torvalds 	if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_CAPTURE) {
5161da177e4SLinus Torvalds 		ptr += chip->cap_count;
5171da177e4SLinus Torvalds 		ptr %= chip->cap_size;
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5201da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
5237d12e780SDavid Howells static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
5241da177e4SLinus Torvalds {
525a5f22156STakashi Iwai 	struct fm801 *chip = dev_id;
5261da177e4SLinus Torvalds 	unsigned short status;
5271da177e4SLinus Torvalds 	unsigned int tmp;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	status = inw(FM801_REG(chip, IRQ_STATUS));
5301da177e4SLinus Torvalds 	status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
5311da177e4SLinus Torvalds 	if (! status)
5321da177e4SLinus Torvalds 		return IRQ_NONE;
5331da177e4SLinus Torvalds 	/* ack first */
5341da177e4SLinus Torvalds 	outw(status, FM801_REG(chip, IRQ_STATUS));
5351da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
5361da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5371da177e4SLinus Torvalds 		chip->ply_buf++;
5381da177e4SLinus Torvalds 		chip->ply_pos += chip->ply_count;
5391da177e4SLinus Torvalds 		chip->ply_pos %= chip->ply_size;
5401da177e4SLinus Torvalds 		tmp = chip->ply_pos + chip->ply_count;
5411da177e4SLinus Torvalds 		tmp %= chip->ply_size;
5421da177e4SLinus Torvalds 		outl(chip->ply_buffer + tmp,
5431da177e4SLinus Torvalds 				(chip->ply_buf & 1) ?
5441da177e4SLinus Torvalds 					FM801_REG(chip, PLY_BUF1) :
5451da177e4SLinus Torvalds 					FM801_REG(chip, PLY_BUF2));
5461da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5471da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->playback_substream);
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
5501da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5511da177e4SLinus Torvalds 		chip->cap_buf++;
5521da177e4SLinus Torvalds 		chip->cap_pos += chip->cap_count;
5531da177e4SLinus Torvalds 		chip->cap_pos %= chip->cap_size;
5541da177e4SLinus Torvalds 		tmp = chip->cap_pos + chip->cap_count;
5551da177e4SLinus Torvalds 		tmp %= chip->cap_size;
5561da177e4SLinus Torvalds 		outl(chip->cap_buffer + tmp,
5571da177e4SLinus Torvalds 				(chip->cap_buf & 1) ?
5581da177e4SLinus Torvalds 					FM801_REG(chip, CAP_BUF1) :
5591da177e4SLinus Torvalds 					FM801_REG(chip, CAP_BUF2));
5601da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5611da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->capture_substream);
5621da177e4SLinus Torvalds 	}
5631da177e4SLinus Torvalds 	if (chip->rmidi && (status & FM801_IRQ_MPU))
5647d12e780SDavid Howells 		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
5651da177e4SLinus Torvalds 	if (status & FM801_IRQ_VOLUME)
5661da177e4SLinus Torvalds 		;/* TODO */
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	return IRQ_HANDLED;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
571a5f22156STakashi Iwai static struct snd_pcm_hardware snd_fm801_playback =
5721da177e4SLinus Torvalds {
5731da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
5741da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
575b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
5761da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
5771da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
5781da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
5791da177e4SLinus Torvalds 	.rate_min =		5500,
5801da177e4SLinus Torvalds 	.rate_max =		48000,
5811da177e4SLinus Torvalds 	.channels_min =		1,
5821da177e4SLinus Torvalds 	.channels_max =		2,
5831da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
5841da177e4SLinus Torvalds 	.period_bytes_min =	64,
5851da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
5861da177e4SLinus Torvalds 	.periods_min =		1,
5871da177e4SLinus Torvalds 	.periods_max =		1024,
5881da177e4SLinus Torvalds 	.fifo_size =		0,
5891da177e4SLinus Torvalds };
5901da177e4SLinus Torvalds 
591a5f22156STakashi Iwai static struct snd_pcm_hardware snd_fm801_capture =
5921da177e4SLinus Torvalds {
5931da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
5941da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
595b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
5961da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
5971da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
5981da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
5991da177e4SLinus Torvalds 	.rate_min =		5500,
6001da177e4SLinus Torvalds 	.rate_max =		48000,
6011da177e4SLinus Torvalds 	.channels_min =		1,
6021da177e4SLinus Torvalds 	.channels_max =		2,
6031da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
6041da177e4SLinus Torvalds 	.period_bytes_min =	64,
6051da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
6061da177e4SLinus Torvalds 	.periods_min =		1,
6071da177e4SLinus Torvalds 	.periods_max =		1024,
6081da177e4SLinus Torvalds 	.fifo_size =		0,
6091da177e4SLinus Torvalds };
6101da177e4SLinus Torvalds 
611a5f22156STakashi Iwai static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
6121da177e4SLinus Torvalds {
613a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
614a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6151da177e4SLinus Torvalds 	int err;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	chip->playback_substream = substream;
6181da177e4SLinus Torvalds 	runtime->hw = snd_fm801_playback;
619a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
620a5f22156STakashi Iwai 				   &hw_constraints_rates);
6211da177e4SLinus Torvalds 	if (chip->multichannel) {
6221da177e4SLinus Torvalds 		runtime->hw.channels_max = 6;
623a5f22156STakashi Iwai 		snd_pcm_hw_constraint_list(runtime, 0,
624a5f22156STakashi Iwai 					   SNDRV_PCM_HW_PARAM_CHANNELS,
625a5f22156STakashi Iwai 					   &hw_constraints_channels);
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
6281da177e4SLinus Torvalds 		return err;
6291da177e4SLinus Torvalds 	return 0;
6301da177e4SLinus Torvalds }
6311da177e4SLinus Torvalds 
632a5f22156STakashi Iwai static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
6331da177e4SLinus Torvalds {
634a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
635a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6361da177e4SLinus Torvalds 	int err;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	chip->capture_substream = substream;
6391da177e4SLinus Torvalds 	runtime->hw = snd_fm801_capture;
640a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
641a5f22156STakashi Iwai 				   &hw_constraints_rates);
6421da177e4SLinus Torvalds 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
6431da177e4SLinus Torvalds 		return err;
6441da177e4SLinus Torvalds 	return 0;
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
647a5f22156STakashi Iwai static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
6481da177e4SLinus Torvalds {
649a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	chip->playback_substream = NULL;
6521da177e4SLinus Torvalds 	return 0;
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
655a5f22156STakashi Iwai static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
6561da177e4SLinus Torvalds {
657a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	chip->capture_substream = NULL;
6601da177e4SLinus Torvalds 	return 0;
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds 
663a5f22156STakashi Iwai static struct snd_pcm_ops snd_fm801_playback_ops = {
6641da177e4SLinus Torvalds 	.open =		snd_fm801_playback_open,
6651da177e4SLinus Torvalds 	.close =	snd_fm801_playback_close,
6661da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
6671da177e4SLinus Torvalds 	.hw_params =	snd_fm801_hw_params,
6681da177e4SLinus Torvalds 	.hw_free =	snd_fm801_hw_free,
6691da177e4SLinus Torvalds 	.prepare =	snd_fm801_playback_prepare,
6701da177e4SLinus Torvalds 	.trigger =	snd_fm801_playback_trigger,
6711da177e4SLinus Torvalds 	.pointer =	snd_fm801_playback_pointer,
6721da177e4SLinus Torvalds };
6731da177e4SLinus Torvalds 
674a5f22156STakashi Iwai static struct snd_pcm_ops snd_fm801_capture_ops = {
6751da177e4SLinus Torvalds 	.open =		snd_fm801_capture_open,
6761da177e4SLinus Torvalds 	.close =	snd_fm801_capture_close,
6771da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
6781da177e4SLinus Torvalds 	.hw_params =	snd_fm801_hw_params,
6791da177e4SLinus Torvalds 	.hw_free =	snd_fm801_hw_free,
6801da177e4SLinus Torvalds 	.prepare =	snd_fm801_capture_prepare,
6811da177e4SLinus Torvalds 	.trigger =	snd_fm801_capture_trigger,
6821da177e4SLinus Torvalds 	.pointer =	snd_fm801_capture_pointer,
6831da177e4SLinus Torvalds };
6841da177e4SLinus Torvalds 
685a5f22156STakashi Iwai static int __devinit snd_fm801_pcm(struct fm801 *chip, int device, struct snd_pcm ** rpcm)
6861da177e4SLinus Torvalds {
687a5f22156STakashi Iwai 	struct snd_pcm *pcm;
6881da177e4SLinus Torvalds 	int err;
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	if (rpcm)
6911da177e4SLinus Torvalds 		*rpcm = NULL;
6921da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm)) < 0)
6931da177e4SLinus Torvalds 		return err;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
6961da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	pcm->private_data = chip;
6991da177e4SLinus Torvalds 	pcm->info_flags = 0;
7001da177e4SLinus Torvalds 	strcpy(pcm->name, "FM801");
7011da177e4SLinus Torvalds 	chip->pcm = pcm;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
7041da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci),
7051da177e4SLinus Torvalds 					      chip->multichannel ? 128*1024 : 64*1024, 128*1024);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	if (rpcm)
7081da177e4SLinus Torvalds 		*rpcm = pcm;
7091da177e4SLinus Torvalds 	return 0;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds /*
7131da177e4SLinus Torvalds  *  TEA5757 radio
7141da177e4SLinus Torvalds  */
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds #ifdef TEA575X_RADIO
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds /* 256PCS GPIO numbers */
7191da177e4SLinus Torvalds #define TEA_256PCS_DATA			1
7201da177e4SLinus Torvalds #define TEA_256PCS_WRITE_ENABLE		2	/* inverted */
7211da177e4SLinus Torvalds #define TEA_256PCS_BUS_CLOCK		3
7221da177e4SLinus Torvalds 
723a5f22156STakashi Iwai static void snd_fm801_tea575x_256pcs_write(struct snd_tea575x *tea, unsigned int val)
7241da177e4SLinus Torvalds {
725a5f22156STakashi Iwai 	struct fm801 *chip = tea->private_data;
7261da177e4SLinus Torvalds 	unsigned short reg;
7271da177e4SLinus Torvalds 	int i = 25;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
7301da177e4SLinus Torvalds 	reg = inw(FM801_REG(chip, GPIO_CTRL));
7311da177e4SLinus Torvalds 	/* use GPIO lines and set write enable bit */
7321da177e4SLinus Torvalds 	reg |= FM801_GPIO_GS(TEA_256PCS_DATA) |
7331da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCS_WRITE_ENABLE) |
7341da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCS_BUS_CLOCK);
7351da177e4SLinus Torvalds 	/* all of lines are in the write direction */
7361da177e4SLinus Torvalds 	/* clear data and clock lines */
7371da177e4SLinus Torvalds 	reg &= ~(FM801_GPIO_GD(TEA_256PCS_DATA) |
7381da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_256PCS_WRITE_ENABLE) |
7391da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_256PCS_BUS_CLOCK) |
7401da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_256PCS_DATA) |
7411da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK) |
7421da177e4SLinus Torvalds 		 FM801_GPIO_GP(TEA_256PCS_WRITE_ENABLE));
7431da177e4SLinus Torvalds 	outw(reg, FM801_REG(chip, GPIO_CTRL));
7441da177e4SLinus Torvalds 	udelay(1);
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	while (i--) {
7471da177e4SLinus Torvalds 		if (val & (1 << i))
7481da177e4SLinus Torvalds 			reg |= FM801_GPIO_GP(TEA_256PCS_DATA);
7491da177e4SLinus Torvalds 		else
7501da177e4SLinus Torvalds 			reg &= ~FM801_GPIO_GP(TEA_256PCS_DATA);
7511da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
7521da177e4SLinus Torvalds 		udelay(1);
7531da177e4SLinus Torvalds 		reg |= FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK);
7541da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
7551da177e4SLinus Torvalds 		reg &= ~FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK);
7561da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
7571da177e4SLinus Torvalds 		udelay(1);
7581da177e4SLinus Torvalds 	}
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	/* and reset the write enable bit */
7611da177e4SLinus Torvalds 	reg |= FM801_GPIO_GP(TEA_256PCS_WRITE_ENABLE) |
7621da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_256PCS_DATA);
7631da177e4SLinus Torvalds 	outw(reg, FM801_REG(chip, GPIO_CTRL));
7641da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
767a5f22156STakashi Iwai static unsigned int snd_fm801_tea575x_256pcs_read(struct snd_tea575x *tea)
7681da177e4SLinus Torvalds {
769a5f22156STakashi Iwai 	struct fm801 *chip = tea->private_data;
7701da177e4SLinus Torvalds 	unsigned short reg;
7711da177e4SLinus Torvalds 	unsigned int val = 0;
7721da177e4SLinus Torvalds 	int i;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
7751da177e4SLinus Torvalds 	reg = inw(FM801_REG(chip, GPIO_CTRL));
7761da177e4SLinus Torvalds 	/* use GPIO lines, set data direction to input */
7771da177e4SLinus Torvalds 	reg |= FM801_GPIO_GS(TEA_256PCS_DATA) |
7781da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCS_WRITE_ENABLE) |
7791da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCS_BUS_CLOCK) |
7801da177e4SLinus Torvalds 	       FM801_GPIO_GD(TEA_256PCS_DATA) |
7811da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_256PCS_DATA) |
7821da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_256PCS_WRITE_ENABLE);
7831da177e4SLinus Torvalds 	/* all of lines are in the write direction, except data */
7841da177e4SLinus Torvalds 	/* clear data, write enable and clock lines */
7851da177e4SLinus Torvalds 	reg &= ~(FM801_GPIO_GD(TEA_256PCS_WRITE_ENABLE) |
7861da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_256PCS_BUS_CLOCK) |
7871da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK));
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	for (i = 0; i < 24; i++) {
7901da177e4SLinus Torvalds 		reg &= ~FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK);
7911da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
7921da177e4SLinus Torvalds 		udelay(1);
7931da177e4SLinus Torvalds 		reg |= FM801_GPIO_GP(TEA_256PCS_BUS_CLOCK);
7941da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
7951da177e4SLinus Torvalds 		udelay(1);
7961da177e4SLinus Torvalds 		val <<= 1;
7971da177e4SLinus Torvalds 		if (inw(FM801_REG(chip, GPIO_CTRL)) & FM801_GPIO_GP(TEA_256PCS_DATA))
7981da177e4SLinus Torvalds 			val |= 1;
7991da177e4SLinus Torvalds 	}
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	return val;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds /* 256PCPR GPIO numbers */
8071da177e4SLinus Torvalds #define TEA_256PCPR_BUS_CLOCK		0
8081da177e4SLinus Torvalds #define TEA_256PCPR_DATA		1
8091da177e4SLinus Torvalds #define TEA_256PCPR_WRITE_ENABLE	2	/* inverted */
8101da177e4SLinus Torvalds 
811a5f22156STakashi Iwai static void snd_fm801_tea575x_256pcpr_write(struct snd_tea575x *tea, unsigned int val)
8121da177e4SLinus Torvalds {
813a5f22156STakashi Iwai 	struct fm801 *chip = tea->private_data;
8141da177e4SLinus Torvalds 	unsigned short reg;
8151da177e4SLinus Torvalds 	int i = 25;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
8181da177e4SLinus Torvalds 	reg = inw(FM801_REG(chip, GPIO_CTRL));
8191da177e4SLinus Torvalds 	/* use GPIO lines and set write enable bit */
8201da177e4SLinus Torvalds 	reg |= FM801_GPIO_GS(TEA_256PCPR_DATA) |
8211da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCPR_WRITE_ENABLE) |
8221da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCPR_BUS_CLOCK);
8231da177e4SLinus Torvalds 	/* all of lines are in the write direction */
8241da177e4SLinus Torvalds 	/* clear data and clock lines */
8251da177e4SLinus Torvalds 	reg &= ~(FM801_GPIO_GD(TEA_256PCPR_DATA) |
8261da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_256PCPR_WRITE_ENABLE) |
8271da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_256PCPR_BUS_CLOCK) |
8281da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_256PCPR_DATA) |
8291da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK) |
8301da177e4SLinus Torvalds 		 FM801_GPIO_GP(TEA_256PCPR_WRITE_ENABLE));
8311da177e4SLinus Torvalds 	outw(reg, FM801_REG(chip, GPIO_CTRL));
8321da177e4SLinus Torvalds 	udelay(1);
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	while (i--) {
8351da177e4SLinus Torvalds 		if (val & (1 << i))
8361da177e4SLinus Torvalds 			reg |= FM801_GPIO_GP(TEA_256PCPR_DATA);
8371da177e4SLinus Torvalds 		else
8381da177e4SLinus Torvalds 			reg &= ~FM801_GPIO_GP(TEA_256PCPR_DATA);
8391da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
8401da177e4SLinus Torvalds 		udelay(1);
8411da177e4SLinus Torvalds 		reg |= FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK);
8421da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
8431da177e4SLinus Torvalds 		reg &= ~FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK);
8441da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
8451da177e4SLinus Torvalds 		udelay(1);
8461da177e4SLinus Torvalds 	}
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	/* and reset the write enable bit */
8491da177e4SLinus Torvalds 	reg |= FM801_GPIO_GP(TEA_256PCPR_WRITE_ENABLE) |
8501da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_256PCPR_DATA);
8511da177e4SLinus Torvalds 	outw(reg, FM801_REG(chip, GPIO_CTRL));
8521da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
855a5f22156STakashi Iwai static unsigned int snd_fm801_tea575x_256pcpr_read(struct snd_tea575x *tea)
8561da177e4SLinus Torvalds {
857a5f22156STakashi Iwai 	struct fm801 *chip = tea->private_data;
8581da177e4SLinus Torvalds 	unsigned short reg;
8591da177e4SLinus Torvalds 	unsigned int val = 0;
8601da177e4SLinus Torvalds 	int i;
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
8631da177e4SLinus Torvalds 	reg = inw(FM801_REG(chip, GPIO_CTRL));
8641da177e4SLinus Torvalds 	/* use GPIO lines, set data direction to input */
8651da177e4SLinus Torvalds 	reg |= FM801_GPIO_GS(TEA_256PCPR_DATA) |
8661da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCPR_WRITE_ENABLE) |
8671da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_256PCPR_BUS_CLOCK) |
8681da177e4SLinus Torvalds 	       FM801_GPIO_GD(TEA_256PCPR_DATA) |
8691da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_256PCPR_DATA) |
8701da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_256PCPR_WRITE_ENABLE);
8711da177e4SLinus Torvalds 	/* all of lines are in the write direction, except data */
8721da177e4SLinus Torvalds 	/* clear data, write enable and clock lines */
8731da177e4SLinus Torvalds 	reg &= ~(FM801_GPIO_GD(TEA_256PCPR_WRITE_ENABLE) |
8741da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_256PCPR_BUS_CLOCK) |
8751da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK));
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	for (i = 0; i < 24; i++) {
8781da177e4SLinus Torvalds 		reg &= ~FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK);
8791da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
8801da177e4SLinus Torvalds 		udelay(1);
8811da177e4SLinus Torvalds 		reg |= FM801_GPIO_GP(TEA_256PCPR_BUS_CLOCK);
8821da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
8831da177e4SLinus Torvalds 		udelay(1);
8841da177e4SLinus Torvalds 		val <<= 1;
8851da177e4SLinus Torvalds 		if (inw(FM801_REG(chip, GPIO_CTRL)) & FM801_GPIO_GP(TEA_256PCPR_DATA))
8861da177e4SLinus Torvalds 			val |= 1;
8871da177e4SLinus Torvalds 	}
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	return val;
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds /* 64PCR GPIO numbers */
8951da177e4SLinus Torvalds #define TEA_64PCR_BUS_CLOCK		0
8961da177e4SLinus Torvalds #define TEA_64PCR_WRITE_ENABLE		1	/* inverted */
8971da177e4SLinus Torvalds #define TEA_64PCR_DATA			2
8981da177e4SLinus Torvalds 
899a5f22156STakashi Iwai static void snd_fm801_tea575x_64pcr_write(struct snd_tea575x *tea, unsigned int val)
9001da177e4SLinus Torvalds {
901a5f22156STakashi Iwai 	struct fm801 *chip = tea->private_data;
9021da177e4SLinus Torvalds 	unsigned short reg;
9031da177e4SLinus Torvalds 	int i = 25;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9061da177e4SLinus Torvalds 	reg = inw(FM801_REG(chip, GPIO_CTRL));
9071da177e4SLinus Torvalds 	/* use GPIO lines and set write enable bit */
9081da177e4SLinus Torvalds 	reg |= FM801_GPIO_GS(TEA_64PCR_DATA) |
9091da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_64PCR_WRITE_ENABLE) |
9101da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_64PCR_BUS_CLOCK);
9111da177e4SLinus Torvalds 	/* all of lines are in the write direction */
9121da177e4SLinus Torvalds 	/* clear data and clock lines */
9131da177e4SLinus Torvalds 	reg &= ~(FM801_GPIO_GD(TEA_64PCR_DATA) |
9141da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_64PCR_WRITE_ENABLE) |
9151da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_64PCR_BUS_CLOCK) |
9161da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_64PCR_DATA) |
9171da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK) |
9181da177e4SLinus Torvalds 		 FM801_GPIO_GP(TEA_64PCR_WRITE_ENABLE));
9191da177e4SLinus Torvalds 	outw(reg, FM801_REG(chip, GPIO_CTRL));
9201da177e4SLinus Torvalds 	udelay(1);
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	while (i--) {
9231da177e4SLinus Torvalds 		if (val & (1 << i))
9241da177e4SLinus Torvalds 			reg |= FM801_GPIO_GP(TEA_64PCR_DATA);
9251da177e4SLinus Torvalds 		else
9261da177e4SLinus Torvalds 			reg &= ~FM801_GPIO_GP(TEA_64PCR_DATA);
9271da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
9281da177e4SLinus Torvalds 		udelay(1);
9291da177e4SLinus Torvalds 		reg |= FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK);
9301da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
9311da177e4SLinus Torvalds 		reg &= ~FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK);
9321da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
9331da177e4SLinus Torvalds 		udelay(1);
9341da177e4SLinus Torvalds 	}
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	/* and reset the write enable bit */
9371da177e4SLinus Torvalds 	reg |= FM801_GPIO_GP(TEA_64PCR_WRITE_ENABLE) |
9381da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_64PCR_DATA);
9391da177e4SLinus Torvalds 	outw(reg, FM801_REG(chip, GPIO_CTRL));
9401da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9411da177e4SLinus Torvalds }
9421da177e4SLinus Torvalds 
943a5f22156STakashi Iwai static unsigned int snd_fm801_tea575x_64pcr_read(struct snd_tea575x *tea)
9441da177e4SLinus Torvalds {
945a5f22156STakashi Iwai 	struct fm801 *chip = tea->private_data;
9461da177e4SLinus Torvalds 	unsigned short reg;
9471da177e4SLinus Torvalds 	unsigned int val = 0;
9481da177e4SLinus Torvalds 	int i;
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9511da177e4SLinus Torvalds 	reg = inw(FM801_REG(chip, GPIO_CTRL));
9521da177e4SLinus Torvalds 	/* use GPIO lines, set data direction to input */
9531da177e4SLinus Torvalds 	reg |= FM801_GPIO_GS(TEA_64PCR_DATA) |
9541da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_64PCR_WRITE_ENABLE) |
9551da177e4SLinus Torvalds 	       FM801_GPIO_GS(TEA_64PCR_BUS_CLOCK) |
9561da177e4SLinus Torvalds 	       FM801_GPIO_GD(TEA_64PCR_DATA) |
9571da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_64PCR_DATA) |
9581da177e4SLinus Torvalds 	       FM801_GPIO_GP(TEA_64PCR_WRITE_ENABLE);
9591da177e4SLinus Torvalds 	/* all of lines are in the write direction, except data */
9601da177e4SLinus Torvalds 	/* clear data, write enable and clock lines */
9611da177e4SLinus Torvalds 	reg &= ~(FM801_GPIO_GD(TEA_64PCR_WRITE_ENABLE) |
9621da177e4SLinus Torvalds 	         FM801_GPIO_GD(TEA_64PCR_BUS_CLOCK) |
9631da177e4SLinus Torvalds 	         FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK));
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	for (i = 0; i < 24; i++) {
9661da177e4SLinus Torvalds 		reg &= ~FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK);
9671da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
9681da177e4SLinus Torvalds 		udelay(1);
9691da177e4SLinus Torvalds 		reg |= FM801_GPIO_GP(TEA_64PCR_BUS_CLOCK);
9701da177e4SLinus Torvalds 		outw(reg, FM801_REG(chip, GPIO_CTRL));
9711da177e4SLinus Torvalds 		udelay(1);
9721da177e4SLinus Torvalds 		val <<= 1;
9731da177e4SLinus Torvalds 		if (inw(FM801_REG(chip, GPIO_CTRL)) & FM801_GPIO_GP(TEA_64PCR_DATA))
9741da177e4SLinus Torvalds 			val |= 1;
9751da177e4SLinus Torvalds 	}
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	return val;
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds static struct snd_tea575x_ops snd_fm801_tea_ops[3] = {
9831da177e4SLinus Torvalds 	{
9841da177e4SLinus Torvalds 		/* 1 = MediaForte 256-PCS */
9851da177e4SLinus Torvalds 		.write = snd_fm801_tea575x_256pcs_write,
9861da177e4SLinus Torvalds 		.read = snd_fm801_tea575x_256pcs_read,
9871da177e4SLinus Torvalds 	},
9881da177e4SLinus Torvalds 	{
9891da177e4SLinus Torvalds 		/* 2 = MediaForte 256-PCPR */
9901da177e4SLinus Torvalds 		.write = snd_fm801_tea575x_256pcpr_write,
9911da177e4SLinus Torvalds 		.read = snd_fm801_tea575x_256pcpr_read,
9921da177e4SLinus Torvalds 	},
9931da177e4SLinus Torvalds 	{
9941da177e4SLinus Torvalds 		/* 3 = MediaForte 64-PCR */
9951da177e4SLinus Torvalds 		.write = snd_fm801_tea575x_64pcr_write,
9961da177e4SLinus Torvalds 		.read = snd_fm801_tea575x_64pcr_read,
9971da177e4SLinus Torvalds 	}
9981da177e4SLinus Torvalds };
9991da177e4SLinus Torvalds #endif
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds /*
10021da177e4SLinus Torvalds  *  Mixer routines
10031da177e4SLinus Torvalds  */
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds #define FM801_SINGLE(xname, reg, shift, mask, invert) \
10061da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
10071da177e4SLinus Torvalds   .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
10081da177e4SLinus Torvalds   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
10091da177e4SLinus Torvalds 
1010a5f22156STakashi Iwai static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
1011a5f22156STakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
10121da177e4SLinus Torvalds {
10131da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
10161da177e4SLinus Torvalds 	uinfo->count = 1;
10171da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
10181da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
10191da177e4SLinus Torvalds 	return 0;
10201da177e4SLinus Torvalds }
10211da177e4SLinus Torvalds 
1022a5f22156STakashi Iwai static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
1023a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
10241da177e4SLinus Torvalds {
1025a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
10261da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
10271da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
10281da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
10291da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift) & mask;
10321da177e4SLinus Torvalds 	if (invert)
10331da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
10341da177e4SLinus Torvalds 	return 0;
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
1037a5f22156STakashi Iwai static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
1038a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
10391da177e4SLinus Torvalds {
1040a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
10411da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
10421da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
10431da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
10441da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
10451da177e4SLinus Torvalds 	unsigned short val;
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
10481da177e4SLinus Torvalds 	if (invert)
10491da177e4SLinus Torvalds 		val = mask - val;
10501da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
10511da177e4SLinus Torvalds }
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds #define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
10541da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
10551da177e4SLinus Torvalds   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
10561da177e4SLinus Torvalds   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
1057666c70ffSTakashi Iwai #define FM801_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
1058666c70ffSTakashi Iwai { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1059666c70ffSTakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
1060666c70ffSTakashi Iwai   .name = xname, .info = snd_fm801_info_double, \
1061666c70ffSTakashi Iwai   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
1062666c70ffSTakashi Iwai   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
1063666c70ffSTakashi Iwai   .tlv = { .p = (xtlv) } }
10641da177e4SLinus Torvalds 
1065a5f22156STakashi Iwai static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
1066a5f22156STakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
10671da177e4SLinus Torvalds {
10681da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
10711da177e4SLinus Torvalds 	uinfo->count = 2;
10721da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
10731da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
10741da177e4SLinus Torvalds 	return 0;
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds 
1077a5f22156STakashi Iwai static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
1078a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
10791da177e4SLinus Torvalds {
1080a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
10811da177e4SLinus Torvalds         int reg = kcontrol->private_value & 0xff;
10821da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
10831da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
10841da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
10851da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10881da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift_left) & mask;
10891da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = (inw(chip->port + reg) >> shift_right) & mask;
10901da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10911da177e4SLinus Torvalds 	if (invert) {
10921da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
10931da177e4SLinus Torvalds 		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
10941da177e4SLinus Torvalds 	}
10951da177e4SLinus Torvalds 	return 0;
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds 
1098a5f22156STakashi Iwai static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
1099a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
11001da177e4SLinus Torvalds {
1101a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
11021da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
11031da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
11041da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
11051da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
11061da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
11071da177e4SLinus Torvalds 	unsigned short val1, val2;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
11101da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
11111da177e4SLinus Torvalds 	if (invert) {
11121da177e4SLinus Torvalds 		val1 = mask - val1;
11131da177e4SLinus Torvalds 		val2 = mask - val2;
11141da177e4SLinus Torvalds 	}
11151da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg,
11161da177e4SLinus Torvalds 				     (mask << shift_left) | (mask << shift_right),
11171da177e4SLinus Torvalds 				     (val1 << shift_left ) | (val2 << shift_right));
11181da177e4SLinus Torvalds }
11191da177e4SLinus Torvalds 
1120a5f22156STakashi Iwai static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
1121a5f22156STakashi Iwai 			      struct snd_ctl_elem_info *uinfo)
11221da177e4SLinus Torvalds {
11231da177e4SLinus Torvalds 	static char *texts[5] = {
11241da177e4SLinus Torvalds 		"AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
11251da177e4SLinus Torvalds 	};
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
11281da177e4SLinus Torvalds 	uinfo->count = 1;
11291da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 5;
11301da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item > 4)
11311da177e4SLinus Torvalds 		uinfo->value.enumerated.item = 4;
11321da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
11331da177e4SLinus Torvalds 	return 0;
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds 
1136a5f22156STakashi Iwai static int snd_fm801_get_mux(struct snd_kcontrol *kcontrol,
1137a5f22156STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
11381da177e4SLinus Torvalds {
1139a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
11401da177e4SLinus Torvalds         unsigned short val;
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	val = inw(FM801_REG(chip, REC_SRC)) & 7;
11431da177e4SLinus Torvalds 	if (val > 4)
11441da177e4SLinus Torvalds 		val = 4;
11451da177e4SLinus Torvalds         ucontrol->value.enumerated.item[0] = val;
11461da177e4SLinus Torvalds         return 0;
11471da177e4SLinus Torvalds }
11481da177e4SLinus Torvalds 
1149a5f22156STakashi Iwai static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
1150a5f22156STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
11511da177e4SLinus Torvalds {
1152a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
11531da177e4SLinus Torvalds         unsigned short val;
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds         if ((val = ucontrol->value.enumerated.item[0]) > 4)
11561da177e4SLinus Torvalds                 return -EINVAL;
11571da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds 
1160666c70ffSTakashi Iwai static DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
1161666c70ffSTakashi Iwai 
1162a5f22156STakashi Iwai #define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
11631da177e4SLinus Torvalds 
1164a5f22156STakashi Iwai static struct snd_kcontrol_new snd_fm801_controls[] __devinitdata = {
1165666c70ffSTakashi Iwai FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
1166666c70ffSTakashi Iwai 		 db_scale_dsp),
11671da177e4SLinus Torvalds FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
1168666c70ffSTakashi Iwai FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
1169666c70ffSTakashi Iwai 		 db_scale_dsp),
11701da177e4SLinus Torvalds FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
1171666c70ffSTakashi Iwai FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
1172666c70ffSTakashi Iwai 		 db_scale_dsp),
11731da177e4SLinus Torvalds FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
11741da177e4SLinus Torvalds {
11751da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
11761da177e4SLinus Torvalds 	.name = "Digital Capture Source",
11771da177e4SLinus Torvalds 	.info = snd_fm801_info_mux,
11781da177e4SLinus Torvalds 	.get = snd_fm801_get_mux,
11791da177e4SLinus Torvalds 	.put = snd_fm801_put_mux,
11801da177e4SLinus Torvalds }
11811da177e4SLinus Torvalds };
11821da177e4SLinus Torvalds 
1183a5f22156STakashi Iwai #define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
11841da177e4SLinus Torvalds 
1185a5f22156STakashi Iwai static struct snd_kcontrol_new snd_fm801_controls_multi[] __devinitdata = {
11861da177e4SLinus Torvalds FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
11871da177e4SLinus Torvalds FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
118810e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
118910e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
119010e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
119110e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
11921da177e4SLinus Torvalds };
11931da177e4SLinus Torvalds 
1194a5f22156STakashi Iwai static void snd_fm801_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
11951da177e4SLinus Torvalds {
1196a5f22156STakashi Iwai 	struct fm801 *chip = bus->private_data;
11971da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
11981da177e4SLinus Torvalds }
11991da177e4SLinus Torvalds 
1200a5f22156STakashi Iwai static void snd_fm801_mixer_free_ac97(struct snd_ac97 *ac97)
12011da177e4SLinus Torvalds {
1202a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
12031da177e4SLinus Torvalds 	if (ac97->num == 0) {
12041da177e4SLinus Torvalds 		chip->ac97 = NULL;
12051da177e4SLinus Torvalds 	} else {
12061da177e4SLinus Torvalds 		chip->ac97_sec = NULL;
12071da177e4SLinus Torvalds 	}
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
1210a5f22156STakashi Iwai static int __devinit snd_fm801_mixer(struct fm801 *chip)
12111da177e4SLinus Torvalds {
1212a5f22156STakashi Iwai 	struct snd_ac97_template ac97;
12131da177e4SLinus Torvalds 	unsigned int i;
12141da177e4SLinus Torvalds 	int err;
1215a5f22156STakashi Iwai 	static struct snd_ac97_bus_ops ops = {
12161da177e4SLinus Torvalds 		.write = snd_fm801_codec_write,
12171da177e4SLinus Torvalds 		.read = snd_fm801_codec_read,
12181da177e4SLinus Torvalds 	};
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
12211da177e4SLinus Torvalds 		return err;
12221da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_fm801_mixer_free_ac97_bus;
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
12251da177e4SLinus Torvalds 	ac97.private_data = chip;
12261da177e4SLinus Torvalds 	ac97.private_free = snd_fm801_mixer_free_ac97;
12271da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
12281da177e4SLinus Torvalds 		return err;
12291da177e4SLinus Torvalds 	if (chip->secondary) {
12301da177e4SLinus Torvalds 		ac97.num = 1;
12311da177e4SLinus Torvalds 		ac97.addr = chip->secondary_addr;
12321da177e4SLinus Torvalds 		if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec)) < 0)
12331da177e4SLinus Torvalds 			return err;
12341da177e4SLinus Torvalds 	}
12351da177e4SLinus Torvalds 	for (i = 0; i < FM801_CONTROLS; i++)
12361da177e4SLinus Torvalds 		snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls[i], chip));
12371da177e4SLinus Torvalds 	if (chip->multichannel) {
12381da177e4SLinus Torvalds 		for (i = 0; i < FM801_CONTROLS_MULTI; i++)
12391da177e4SLinus Torvalds 			snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds 	return 0;
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds /*
12451da177e4SLinus Torvalds  *  initialization routines
12461da177e4SLinus Torvalds  */
12471da177e4SLinus Torvalds 
1248b1e9ed26STakashi Iwai static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
1249b1e9ed26STakashi Iwai 			  unsigned short reg, unsigned long waits)
1250b1e9ed26STakashi Iwai {
1251b1e9ed26STakashi Iwai 	unsigned long timeout = jiffies + waits;
1252b1e9ed26STakashi Iwai 
1253b1e9ed26STakashi Iwai 	outw(FM801_AC97_READ | (codec_id << FM801_AC97_ADDR_SHIFT) | reg,
1254b1e9ed26STakashi Iwai 	     FM801_REG(chip, AC97_CMD));
1255b1e9ed26STakashi Iwai 	udelay(5);
1256b1e9ed26STakashi Iwai 	do {
1257b1e9ed26STakashi Iwai 		if ((inw(FM801_REG(chip, AC97_CMD)) & (FM801_AC97_VALID|FM801_AC97_BUSY))
1258b1e9ed26STakashi Iwai 		    == FM801_AC97_VALID)
1259b1e9ed26STakashi Iwai 			return 0;
1260b1e9ed26STakashi Iwai 		schedule_timeout_uninterruptible(1);
1261b1e9ed26STakashi Iwai 	} while (time_after(timeout, jiffies));
1262b1e9ed26STakashi Iwai 	return -EIO;
1263b1e9ed26STakashi Iwai }
1264b1e9ed26STakashi Iwai 
1265b1e9ed26STakashi Iwai static int snd_fm801_chip_init(struct fm801 *chip, int resume)
1266b1e9ed26STakashi Iwai {
1267b1e9ed26STakashi Iwai 	int id;
1268b1e9ed26STakashi Iwai 	unsigned short cmdw;
1269b1e9ed26STakashi Iwai 
12706bbe13ecSJaroslav Kysela 	if (chip->tea575x_tuner & 0x0010)
1271e0a5d82aSAndy Shevchenko 		goto __ac97_ok;
1272e0a5d82aSAndy Shevchenko 
1273b1e9ed26STakashi Iwai 	/* codec cold reset + AC'97 warm reset */
1274b1e9ed26STakashi Iwai 	outw((1<<5) | (1<<6), FM801_REG(chip, CODEC_CTRL));
1275b1e9ed26STakashi Iwai 	inw(FM801_REG(chip, CODEC_CTRL)); /* flush posting data */
1276b1e9ed26STakashi Iwai 	udelay(100);
1277b1e9ed26STakashi Iwai 	outw(0, FM801_REG(chip, CODEC_CTRL));
1278b1e9ed26STakashi Iwai 
1279b1e9ed26STakashi Iwai 	if (wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750)) < 0) {
1280b1e9ed26STakashi Iwai 		snd_printk(KERN_ERR "Primary AC'97 codec not found\n");
1281b1e9ed26STakashi Iwai 		if (! resume)
1282b1e9ed26STakashi Iwai 			return -EIO;
1283b1e9ed26STakashi Iwai 	}
1284b1e9ed26STakashi Iwai 
1285b1e9ed26STakashi Iwai 	if (chip->multichannel) {
1286b1e9ed26STakashi Iwai 		if (chip->secondary_addr) {
1287b1e9ed26STakashi Iwai 			wait_for_codec(chip, chip->secondary_addr,
1288b1e9ed26STakashi Iwai 				       AC97_VENDOR_ID1, msecs_to_jiffies(50));
1289b1e9ed26STakashi Iwai 		} else {
1290b1e9ed26STakashi Iwai 			/* my card has the secondary codec */
1291b1e9ed26STakashi Iwai 			/* at address #3, so the loop is inverted */
1292b1e9ed26STakashi Iwai 			for (id = 3; id > 0; id--) {
1293b1e9ed26STakashi Iwai 				if (! wait_for_codec(chip, id, AC97_VENDOR_ID1,
1294b1e9ed26STakashi Iwai 						     msecs_to_jiffies(50))) {
1295b1e9ed26STakashi Iwai 					cmdw = inw(FM801_REG(chip, AC97_DATA));
1296b1e9ed26STakashi Iwai 					if (cmdw != 0xffff && cmdw != 0) {
1297b1e9ed26STakashi Iwai 						chip->secondary = 1;
1298b1e9ed26STakashi Iwai 						chip->secondary_addr = id;
1299b1e9ed26STakashi Iwai 						break;
1300b1e9ed26STakashi Iwai 					}
1301b1e9ed26STakashi Iwai 				}
1302b1e9ed26STakashi Iwai 			}
1303b1e9ed26STakashi Iwai 		}
1304b1e9ed26STakashi Iwai 
1305b1e9ed26STakashi Iwai 		/* the recovery phase, it seems that probing for non-existing codec might */
1306b1e9ed26STakashi Iwai 		/* cause timeout problems */
1307b1e9ed26STakashi Iwai 		wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
1308b1e9ed26STakashi Iwai 	}
1309b1e9ed26STakashi Iwai 
13106bbe13ecSJaroslav Kysela       __ac97_ok:
13116bbe13ecSJaroslav Kysela 
1312b1e9ed26STakashi Iwai 	/* init volume */
1313b1e9ed26STakashi Iwai 	outw(0x0808, FM801_REG(chip, PCM_VOL));
1314b1e9ed26STakashi Iwai 	outw(0x9f1f, FM801_REG(chip, FM_VOL));
1315b1e9ed26STakashi Iwai 	outw(0x8808, FM801_REG(chip, I2S_VOL));
1316b1e9ed26STakashi Iwai 
1317b1e9ed26STakashi Iwai 	/* I2S control - I2S mode */
1318b1e9ed26STakashi Iwai 	outw(0x0003, FM801_REG(chip, I2S_MODE));
1319b1e9ed26STakashi Iwai 
13206bbe13ecSJaroslav Kysela 	/* interrupt setup */
1321b1e9ed26STakashi Iwai 	cmdw = inw(FM801_REG(chip, IRQ_MASK));
13226bbe13ecSJaroslav Kysela 	if (chip->irq < 0)
13236bbe13ecSJaroslav Kysela 		cmdw |= 0x00c3;		/* mask everything, no PCM nor MPU */
13246bbe13ecSJaroslav Kysela 	else
13256bbe13ecSJaroslav Kysela 		cmdw &= ~0x0083;	/* unmask MPU, PLAYBACK & CAPTURE */
1326b1e9ed26STakashi Iwai 	outw(cmdw, FM801_REG(chip, IRQ_MASK));
1327b1e9ed26STakashi Iwai 
1328b1e9ed26STakashi Iwai 	/* interrupt clear */
1329b1e9ed26STakashi Iwai 	outw(FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU, FM801_REG(chip, IRQ_STATUS));
1330b1e9ed26STakashi Iwai 
1331b1e9ed26STakashi Iwai 	return 0;
1332b1e9ed26STakashi Iwai }
1333b1e9ed26STakashi Iwai 
1334b1e9ed26STakashi Iwai 
1335a5f22156STakashi Iwai static int snd_fm801_free(struct fm801 *chip)
13361da177e4SLinus Torvalds {
13371da177e4SLinus Torvalds 	unsigned short cmdw;
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 	if (chip->irq < 0)
13401da177e4SLinus Torvalds 		goto __end_hw;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	/* interrupt setup - mask everything */
13431da177e4SLinus Torvalds 	cmdw = inw(FM801_REG(chip, IRQ_MASK));
13441da177e4SLinus Torvalds 	cmdw |= 0x00c3;
13451da177e4SLinus Torvalds 	outw(cmdw, FM801_REG(chip, IRQ_MASK));
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds       __end_hw:
13481da177e4SLinus Torvalds #ifdef TEA575X_RADIO
13491da177e4SLinus Torvalds 	snd_tea575x_exit(&chip->tea);
13501da177e4SLinus Torvalds #endif
13511da177e4SLinus Torvalds 	if (chip->irq >= 0)
1352a5f22156STakashi Iwai 		free_irq(chip->irq, chip);
13531da177e4SLinus Torvalds 	pci_release_regions(chip->pci);
13541da177e4SLinus Torvalds 	pci_disable_device(chip->pci);
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	kfree(chip);
13571da177e4SLinus Torvalds 	return 0;
13581da177e4SLinus Torvalds }
13591da177e4SLinus Torvalds 
1360a5f22156STakashi Iwai static int snd_fm801_dev_free(struct snd_device *device)
13611da177e4SLinus Torvalds {
1362a5f22156STakashi Iwai 	struct fm801 *chip = device->device_data;
13631da177e4SLinus Torvalds 	return snd_fm801_free(chip);
13641da177e4SLinus Torvalds }
13651da177e4SLinus Torvalds 
1366a5f22156STakashi Iwai static int __devinit snd_fm801_create(struct snd_card *card,
13671da177e4SLinus Torvalds 				      struct pci_dev * pci,
13681da177e4SLinus Torvalds 				      int tea575x_tuner,
1369a5f22156STakashi Iwai 				      struct fm801 ** rchip)
13701da177e4SLinus Torvalds {
1371a5f22156STakashi Iwai 	struct fm801 *chip;
1372b1e9ed26STakashi Iwai 	unsigned char rev;
13731da177e4SLinus Torvalds 	int err;
1374a5f22156STakashi Iwai 	static struct snd_device_ops ops = {
13751da177e4SLinus Torvalds 		.dev_free =	snd_fm801_dev_free,
13761da177e4SLinus Torvalds 	};
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	*rchip = NULL;
13791da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
13801da177e4SLinus Torvalds 		return err;
1381e560d8d8STakashi Iwai 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
13821da177e4SLinus Torvalds 	if (chip == NULL) {
13831da177e4SLinus Torvalds 		pci_disable_device(pci);
13841da177e4SLinus Torvalds 		return -ENOMEM;
13851da177e4SLinus Torvalds 	}
13861da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
13871da177e4SLinus Torvalds 	chip->card = card;
13881da177e4SLinus Torvalds 	chip->pci = pci;
13891da177e4SLinus Torvalds 	chip->irq = -1;
13906bbe13ecSJaroslav Kysela 	chip->tea575x_tuner = tea575x_tuner;
13911da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, "FM801")) < 0) {
13921da177e4SLinus Torvalds 		kfree(chip);
13931da177e4SLinus Torvalds 		pci_disable_device(pci);
13941da177e4SLinus Torvalds 		return err;
13951da177e4SLinus Torvalds 	}
13961da177e4SLinus Torvalds 	chip->port = pci_resource_start(pci, 0);
13976bbe13ecSJaroslav Kysela 	if ((tea575x_tuner & 0x0010) == 0) {
1398*437a5a46STakashi Iwai 		if (request_irq(pci->irq, snd_fm801_interrupt, IRQF_SHARED,
1399a5f22156STakashi Iwai 				"FM801", chip)) {
140099b359baSTakashi Iwai 			snd_printk(KERN_ERR "unable to grab IRQ %d\n", chip->irq);
14011da177e4SLinus Torvalds 			snd_fm801_free(chip);
14021da177e4SLinus Torvalds 			return -EBUSY;
14031da177e4SLinus Torvalds 		}
14041da177e4SLinus Torvalds 		chip->irq = pci->irq;
14051da177e4SLinus Torvalds 		pci_set_master(pci);
14066bbe13ecSJaroslav Kysela 	}
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 	pci_read_config_byte(pci, PCI_REVISION_ID, &rev);
14091da177e4SLinus Torvalds 	if (rev >= 0xb1)	/* FM801-AU */
14101da177e4SLinus Torvalds 		chip->multichannel = 1;
14111da177e4SLinus Torvalds 
1412b1e9ed26STakashi Iwai 	snd_fm801_chip_init(chip, 0);
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
14151da177e4SLinus Torvalds 		snd_fm801_free(chip);
14161da177e4SLinus Torvalds 		return err;
14171da177e4SLinus Torvalds 	}
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 	snd_card_set_dev(card, &pci->dev);
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds #ifdef TEA575X_RADIO
1422e0a5d82aSAndy Shevchenko 	if (tea575x_tuner > 0 && (tea575x_tuner & 0x000f) < 4) {
14231da177e4SLinus Torvalds 		chip->tea.dev_nr = tea575x_tuner >> 16;
14241da177e4SLinus Torvalds 		chip->tea.card = card;
14251da177e4SLinus Torvalds 		chip->tea.freq_fixup = 10700;
14261da177e4SLinus Torvalds 		chip->tea.private_data = chip;
1427e0a5d82aSAndy Shevchenko 		chip->tea.ops = &snd_fm801_tea_ops[(tea575x_tuner & 0x000f) - 1];
14281da177e4SLinus Torvalds 		snd_tea575x_init(&chip->tea);
14291da177e4SLinus Torvalds 	}
14301da177e4SLinus Torvalds #endif
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	*rchip = chip;
14331da177e4SLinus Torvalds 	return 0;
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds static int __devinit snd_card_fm801_probe(struct pci_dev *pci,
14371da177e4SLinus Torvalds 					  const struct pci_device_id *pci_id)
14381da177e4SLinus Torvalds {
14391da177e4SLinus Torvalds 	static int dev;
1440a5f22156STakashi Iwai 	struct snd_card *card;
1441a5f22156STakashi Iwai 	struct fm801 *chip;
1442a5f22156STakashi Iwai 	struct snd_opl3 *opl3;
14431da177e4SLinus Torvalds 	int err;
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds         if (dev >= SNDRV_CARDS)
14461da177e4SLinus Torvalds                 return -ENODEV;
14471da177e4SLinus Torvalds 	if (!enable[dev]) {
14481da177e4SLinus Torvalds 		dev++;
14491da177e4SLinus Torvalds 		return -ENOENT;
14501da177e4SLinus Torvalds 	}
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds 	card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
14531da177e4SLinus Torvalds 	if (card == NULL)
14541da177e4SLinus Torvalds 		return -ENOMEM;
14551da177e4SLinus Torvalds 	if ((err = snd_fm801_create(card, pci, tea575x_tuner[dev], &chip)) < 0) {
14561da177e4SLinus Torvalds 		snd_card_free(card);
14571da177e4SLinus Torvalds 		return err;
14581da177e4SLinus Torvalds 	}
1459b1e9ed26STakashi Iwai 	card->private_data = chip;
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 	strcpy(card->driver, "FM801");
14621da177e4SLinus Torvalds 	strcpy(card->shortname, "ForteMedia FM801-");
14631da177e4SLinus Torvalds 	strcat(card->shortname, chip->multichannel ? "AU" : "AS");
14641da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %i",
14651da177e4SLinus Torvalds 		card->shortname, chip->port, chip->irq);
14661da177e4SLinus Torvalds 
1467e0a5d82aSAndy Shevchenko 	if (tea575x_tuner[dev] & 0x0010)
1468e0a5d82aSAndy Shevchenko 		goto __fm801_tuner_only;
1469e0a5d82aSAndy Shevchenko 
14701da177e4SLinus Torvalds 	if ((err = snd_fm801_pcm(chip, 0, NULL)) < 0) {
14711da177e4SLinus Torvalds 		snd_card_free(card);
14721da177e4SLinus Torvalds 		return err;
14731da177e4SLinus Torvalds 	}
14741da177e4SLinus Torvalds 	if ((err = snd_fm801_mixer(chip)) < 0) {
14751da177e4SLinus Torvalds 		snd_card_free(card);
14761da177e4SLinus Torvalds 		return err;
14771da177e4SLinus Torvalds 	}
14781da177e4SLinus Torvalds 	if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1479302e4c2fSTakashi Iwai 				       FM801_REG(chip, MPU401_DATA),
1480302e4c2fSTakashi Iwai 				       MPU401_INFO_INTEGRATED,
14811da177e4SLinus Torvalds 				       chip->irq, 0, &chip->rmidi)) < 0) {
14821da177e4SLinus Torvalds 		snd_card_free(card);
14831da177e4SLinus Torvalds 		return err;
14841da177e4SLinus Torvalds 	}
14851da177e4SLinus Torvalds 	if ((err = snd_opl3_create(card, FM801_REG(chip, OPL3_BANK0),
14861da177e4SLinus Torvalds 				   FM801_REG(chip, OPL3_BANK1),
14871da177e4SLinus Torvalds 				   OPL3_HW_OPL3_FM801, 1, &opl3)) < 0) {
14881da177e4SLinus Torvalds 		snd_card_free(card);
14891da177e4SLinus Torvalds 		return err;
14901da177e4SLinus Torvalds 	}
14911da177e4SLinus Torvalds 	if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
14921da177e4SLinus Torvalds 		snd_card_free(card);
14931da177e4SLinus Torvalds 		return err;
14941da177e4SLinus Torvalds 	}
14951da177e4SLinus Torvalds 
1496e0a5d82aSAndy Shevchenko       __fm801_tuner_only:
14971da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
14981da177e4SLinus Torvalds 		snd_card_free(card);
14991da177e4SLinus Torvalds 		return err;
15001da177e4SLinus Torvalds 	}
15011da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
15021da177e4SLinus Torvalds 	dev++;
15031da177e4SLinus Torvalds 	return 0;
15041da177e4SLinus Torvalds }
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds static void __devexit snd_card_fm801_remove(struct pci_dev *pci)
15071da177e4SLinus Torvalds {
15081da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
15091da177e4SLinus Torvalds 	pci_set_drvdata(pci, NULL);
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds 
1512b1e9ed26STakashi Iwai #ifdef CONFIG_PM
1513b1e9ed26STakashi Iwai static unsigned char saved_regs[] = {
1514b1e9ed26STakashi Iwai 	FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
1515b1e9ed26STakashi Iwai 	FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
1516b1e9ed26STakashi Iwai 	FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
1517b1e9ed26STakashi Iwai 	FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
1518b1e9ed26STakashi Iwai };
1519b1e9ed26STakashi Iwai 
1520b1e9ed26STakashi Iwai static int snd_fm801_suspend(struct pci_dev *pci, pm_message_t state)
1521b1e9ed26STakashi Iwai {
1522b1e9ed26STakashi Iwai 	struct snd_card *card = pci_get_drvdata(pci);
1523b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1524b1e9ed26STakashi Iwai 	int i;
1525b1e9ed26STakashi Iwai 
1526b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1527b1e9ed26STakashi Iwai 	snd_pcm_suspend_all(chip->pcm);
1528b1e9ed26STakashi Iwai 	snd_ac97_suspend(chip->ac97);
1529b1e9ed26STakashi Iwai 	snd_ac97_suspend(chip->ac97_sec);
1530b1e9ed26STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1531b1e9ed26STakashi Iwai 		chip->saved_regs[i] = inw(chip->port + saved_regs[i]);
1532b1e9ed26STakashi Iwai 	/* FIXME: tea575x suspend */
1533b1e9ed26STakashi Iwai 
1534b1e9ed26STakashi Iwai 	pci_disable_device(pci);
1535b1e9ed26STakashi Iwai 	pci_save_state(pci);
153630b35399STakashi Iwai 	pci_set_power_state(pci, pci_choose_state(pci, state));
1537b1e9ed26STakashi Iwai 	return 0;
1538b1e9ed26STakashi Iwai }
1539b1e9ed26STakashi Iwai 
1540b1e9ed26STakashi Iwai static int snd_fm801_resume(struct pci_dev *pci)
1541b1e9ed26STakashi Iwai {
1542b1e9ed26STakashi Iwai 	struct snd_card *card = pci_get_drvdata(pci);
1543b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1544b1e9ed26STakashi Iwai 	int i;
1545b1e9ed26STakashi Iwai 
1546b1e9ed26STakashi Iwai 	pci_set_power_state(pci, PCI_D0);
154730b35399STakashi Iwai 	pci_restore_state(pci);
154830b35399STakashi Iwai 	if (pci_enable_device(pci) < 0) {
154930b35399STakashi Iwai 		printk(KERN_ERR "fm801: pci_enable_device failed, "
155030b35399STakashi Iwai 		       "disabling device\n");
155130b35399STakashi Iwai 		snd_card_disconnect(card);
155230b35399STakashi Iwai 		return -EIO;
155330b35399STakashi Iwai 	}
1554b1e9ed26STakashi Iwai 	pci_set_master(pci);
1555b1e9ed26STakashi Iwai 
1556b1e9ed26STakashi Iwai 	snd_fm801_chip_init(chip, 1);
1557b1e9ed26STakashi Iwai 	snd_ac97_resume(chip->ac97);
1558b1e9ed26STakashi Iwai 	snd_ac97_resume(chip->ac97_sec);
1559b1e9ed26STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1560b1e9ed26STakashi Iwai 		outw(chip->saved_regs[i], chip->port + saved_regs[i]);
1561b1e9ed26STakashi Iwai 
1562b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1563b1e9ed26STakashi Iwai 	return 0;
1564b1e9ed26STakashi Iwai }
1565b1e9ed26STakashi Iwai #endif
1566b1e9ed26STakashi Iwai 
15671da177e4SLinus Torvalds static struct pci_driver driver = {
15681da177e4SLinus Torvalds 	.name = "FM801",
15691da177e4SLinus Torvalds 	.id_table = snd_fm801_ids,
15701da177e4SLinus Torvalds 	.probe = snd_card_fm801_probe,
15711da177e4SLinus Torvalds 	.remove = __devexit_p(snd_card_fm801_remove),
1572b1e9ed26STakashi Iwai #ifdef CONFIG_PM
1573b1e9ed26STakashi Iwai 	.suspend = snd_fm801_suspend,
1574b1e9ed26STakashi Iwai 	.resume = snd_fm801_resume,
1575b1e9ed26STakashi Iwai #endif
15761da177e4SLinus Torvalds };
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds static int __init alsa_card_fm801_init(void)
15791da177e4SLinus Torvalds {
158001d25d46STakashi Iwai 	return pci_register_driver(&driver);
15811da177e4SLinus Torvalds }
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds static void __exit alsa_card_fm801_exit(void)
15841da177e4SLinus Torvalds {
15851da177e4SLinus Torvalds 	pci_unregister_driver(&driver);
15861da177e4SLinus Torvalds }
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds module_init(alsa_card_fm801_init)
15891da177e4SLinus Torvalds module_exit(alsa_card_fm801_exit)
1590