xref: /linux/sound/pci/fm801.c (revision 60c5772b50fe8ce947685cfe3a80b9bf6ea92831)
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
4059b56459SOndrej Zary #include <media/tea575x.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 */
51a67ff6a5SRusty 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];
61d4ecc83bSHans Verkuil static int radio_nr[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
641da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
651da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
661da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
671da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
681da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
691da177e4SLinus Torvalds module_param_array(tea575x_tuner, int, NULL, 0444);
70d7ba858aSOndrej 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).");
71d4ecc83bSHans Verkuil module_param_array(radio_nr, int, NULL, 0444);
72d4ecc83bSHans Verkuil MODULE_PARM_DESC(radio_nr, "Radio device numbers");
73d4ecc83bSHans Verkuil 
74fb716c0bSOndrej Zary 
75c37279b9SBen Hutchings #define TUNER_DISABLED		(1<<3)
76fb716c0bSOndrej Zary #define TUNER_ONLY		(1<<4)
77fb716c0bSOndrej Zary #define TUNER_TYPE_MASK		(~TUNER_ONLY & 0xFFFF)
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /*
801da177e4SLinus Torvalds  *  Direct registers
811da177e4SLinus Torvalds  */
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds #define FM801_REG(chip, reg)	(chip->port + FM801_##reg)
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds #define FM801_PCM_VOL		0x00	/* PCM Output Volume */
861da177e4SLinus Torvalds #define FM801_FM_VOL		0x02	/* FM Output Volume */
871da177e4SLinus Torvalds #define FM801_I2S_VOL		0x04	/* I2S Volume */
881da177e4SLinus Torvalds #define FM801_REC_SRC		0x06	/* Record Source */
891da177e4SLinus Torvalds #define FM801_PLY_CTRL		0x08	/* Playback Control */
901da177e4SLinus Torvalds #define FM801_PLY_COUNT		0x0a	/* Playback Count */
911da177e4SLinus Torvalds #define FM801_PLY_BUF1		0x0c	/* Playback Bufer I */
921da177e4SLinus Torvalds #define FM801_PLY_BUF2		0x10	/* Playback Buffer II */
931da177e4SLinus Torvalds #define FM801_CAP_CTRL		0x14	/* Capture Control */
941da177e4SLinus Torvalds #define FM801_CAP_COUNT		0x16	/* Capture Count */
951da177e4SLinus Torvalds #define FM801_CAP_BUF1		0x18	/* Capture Buffer I */
961da177e4SLinus Torvalds #define FM801_CAP_BUF2		0x1c	/* Capture Buffer II */
971da177e4SLinus Torvalds #define FM801_CODEC_CTRL	0x22	/* Codec Control */
981da177e4SLinus Torvalds #define FM801_I2S_MODE		0x24	/* I2S Mode Control */
991da177e4SLinus Torvalds #define FM801_VOLUME		0x26	/* Volume Up/Down/Mute Status */
1001da177e4SLinus Torvalds #define FM801_I2C_CTRL		0x29	/* I2C Control */
1011da177e4SLinus Torvalds #define FM801_AC97_CMD		0x2a	/* AC'97 Command */
1021da177e4SLinus Torvalds #define FM801_AC97_DATA		0x2c	/* AC'97 Data */
1031da177e4SLinus Torvalds #define FM801_MPU401_DATA	0x30	/* MPU401 Data */
1041da177e4SLinus Torvalds #define FM801_MPU401_CMD	0x31	/* MPU401 Command */
1051da177e4SLinus Torvalds #define FM801_GPIO_CTRL		0x52	/* General Purpose I/O Control */
1061da177e4SLinus Torvalds #define FM801_GEN_CTRL		0x54	/* General Control */
1071da177e4SLinus Torvalds #define FM801_IRQ_MASK		0x56	/* Interrupt Mask */
1081da177e4SLinus Torvalds #define FM801_IRQ_STATUS	0x5a	/* Interrupt Status */
1091da177e4SLinus Torvalds #define FM801_OPL3_BANK0	0x68	/* OPL3 Status Read / Bank 0 Write */
1101da177e4SLinus Torvalds #define FM801_OPL3_DATA0	0x69	/* OPL3 Data 0 Write */
1111da177e4SLinus Torvalds #define FM801_OPL3_BANK1	0x6a	/* OPL3 Bank 1 Write */
1121da177e4SLinus Torvalds #define FM801_OPL3_DATA1	0x6b	/* OPL3 Bank 1 Write */
1131da177e4SLinus Torvalds #define FM801_POWERDOWN		0x70	/* Blocks Power Down Control */
1141da177e4SLinus Torvalds 
115b1e9ed26STakashi Iwai /* codec access */
116b1e9ed26STakashi Iwai #define FM801_AC97_READ		(1<<7)	/* read=1, write=0 */
117b1e9ed26STakashi Iwai #define FM801_AC97_VALID	(1<<8)	/* port valid=1 */
118b1e9ed26STakashi Iwai #define FM801_AC97_BUSY		(1<<9)	/* busy=1 */
119b1e9ed26STakashi Iwai #define FM801_AC97_ADDR_SHIFT	10	/* codec id (2bit) */
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds /* playback and record control register bits */
1221da177e4SLinus Torvalds #define FM801_BUF1_LAST		(1<<1)
1231da177e4SLinus Torvalds #define FM801_BUF2_LAST		(1<<2)
1241da177e4SLinus Torvalds #define FM801_START		(1<<5)
1251da177e4SLinus Torvalds #define FM801_PAUSE		(1<<6)
1261da177e4SLinus Torvalds #define FM801_IMMED_STOP	(1<<7)
1271da177e4SLinus Torvalds #define FM801_RATE_SHIFT	8
1281da177e4SLinus Torvalds #define FM801_RATE_MASK		(15 << FM801_RATE_SHIFT)
1291da177e4SLinus Torvalds #define FM801_CHANNELS_4	(1<<12)	/* playback only */
1301da177e4SLinus Torvalds #define FM801_CHANNELS_6	(2<<12)	/* playback only */
1311da177e4SLinus Torvalds #define FM801_CHANNELS_6MS	(3<<12)	/* playback only */
1321da177e4SLinus Torvalds #define FM801_CHANNELS_MASK	(3<<12)
1331da177e4SLinus Torvalds #define FM801_16BIT		(1<<14)
1341da177e4SLinus Torvalds #define FM801_STEREO		(1<<15)
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds /* IRQ status bits */
1371da177e4SLinus Torvalds #define FM801_IRQ_PLAYBACK	(1<<8)
1381da177e4SLinus Torvalds #define FM801_IRQ_CAPTURE	(1<<9)
1391da177e4SLinus Torvalds #define FM801_IRQ_VOLUME	(1<<14)
1401da177e4SLinus Torvalds #define FM801_IRQ_MPU		(1<<15)
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /* GPIO control register */
1431da177e4SLinus Torvalds #define FM801_GPIO_GP0		(1<<0)	/* read/write */
1441da177e4SLinus Torvalds #define FM801_GPIO_GP1		(1<<1)
1451da177e4SLinus Torvalds #define FM801_GPIO_GP2		(1<<2)
1461da177e4SLinus Torvalds #define FM801_GPIO_GP3		(1<<3)
1471da177e4SLinus Torvalds #define FM801_GPIO_GP(x)	(1<<(0+(x)))
1481da177e4SLinus Torvalds #define FM801_GPIO_GD0		(1<<8)	/* directions: 1 = input, 0 = output*/
1491da177e4SLinus Torvalds #define FM801_GPIO_GD1		(1<<9)
1501da177e4SLinus Torvalds #define FM801_GPIO_GD2		(1<<10)
1511da177e4SLinus Torvalds #define FM801_GPIO_GD3		(1<<11)
1521da177e4SLinus Torvalds #define FM801_GPIO_GD(x)	(1<<(8+(x)))
1531da177e4SLinus Torvalds #define FM801_GPIO_GS0		(1<<12)	/* function select: */
1541da177e4SLinus Torvalds #define FM801_GPIO_GS1		(1<<13)	/*    1 = GPIO */
1551da177e4SLinus Torvalds #define FM801_GPIO_GS2		(1<<14)	/*    0 = other (S/PDIF, VOL) */
1561da177e4SLinus Torvalds #define FM801_GPIO_GS3		(1<<15)
1571da177e4SLinus Torvalds #define FM801_GPIO_GS(x)	(1<<(12+(x)))
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds /*
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds  */
1621da177e4SLinus Torvalds 
163a5f22156STakashi Iwai struct fm801 {
1641da177e4SLinus Torvalds 	int irq;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	unsigned long port;	/* I/O port number */
1671da177e4SLinus Torvalds 	unsigned int multichannel: 1,	/* multichannel support */
1681da177e4SLinus Torvalds 		     secondary: 1;	/* secondary codec */
1691da177e4SLinus Torvalds 	unsigned char secondary_addr;	/* address of the secondary codec */
170fb716c0bSOndrej Zary 	unsigned int tea575x_tuner;	/* tuner access method & flags */
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	unsigned short ply_ctrl; /* playback control */
1731da177e4SLinus Torvalds 	unsigned short cap_ctrl; /* capture control */
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	unsigned long ply_buffer;
1761da177e4SLinus Torvalds 	unsigned int ply_buf;
1771da177e4SLinus Torvalds 	unsigned int ply_count;
1781da177e4SLinus Torvalds 	unsigned int ply_size;
1791da177e4SLinus Torvalds 	unsigned int ply_pos;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	unsigned long cap_buffer;
1821da177e4SLinus Torvalds 	unsigned int cap_buf;
1831da177e4SLinus Torvalds 	unsigned int cap_count;
1841da177e4SLinus Torvalds 	unsigned int cap_size;
1851da177e4SLinus Torvalds 	unsigned int cap_pos;
1861da177e4SLinus Torvalds 
187a5f22156STakashi Iwai 	struct snd_ac97_bus *ac97_bus;
188a5f22156STakashi Iwai 	struct snd_ac97 *ac97;
189a5f22156STakashi Iwai 	struct snd_ac97 *ac97_sec;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	struct pci_dev *pci;
192a5f22156STakashi Iwai 	struct snd_card *card;
193a5f22156STakashi Iwai 	struct snd_pcm *pcm;
194a5f22156STakashi Iwai 	struct snd_rawmidi *rmidi;
195a5f22156STakashi Iwai 	struct snd_pcm_substream *playback_substream;
196a5f22156STakashi Iwai 	struct snd_pcm_substream *capture_substream;
1971da177e4SLinus Torvalds 	unsigned int p_dma_size;
1981da177e4SLinus Torvalds 	unsigned int c_dma_size;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	spinlock_t reg_lock;
201a5f22156STakashi Iwai 	struct snd_info_entry *proc_entry;
2021da177e4SLinus Torvalds 
203fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
204d4ecc83bSHans Verkuil 	struct v4l2_device v4l2_dev;
205a5f22156STakashi Iwai 	struct snd_tea575x tea;
2061da177e4SLinus Torvalds #endif
207b1e9ed26STakashi Iwai 
208c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
209b1e9ed26STakashi Iwai 	u16 saved_regs[0x20];
210b1e9ed26STakashi Iwai #endif
2111da177e4SLinus Torvalds };
2121da177e4SLinus Torvalds 
213cebe41d4SAlexey Dobriyan static DEFINE_PCI_DEVICE_TABLE(snd_fm801_ids) = {
2141da177e4SLinus Torvalds 	{ 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* FM801 */
21526be8659SSergey Vlasov 	{ 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* Gallant Odyssey Sound 4 */
2161da177e4SLinus Torvalds 	{ 0, }
2171da177e4SLinus Torvalds };
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds /*
2221da177e4SLinus Torvalds  *  common I/O routines
2231da177e4SLinus Torvalds  */
2241da177e4SLinus Torvalds 
225a5f22156STakashi Iwai static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
2261da177e4SLinus Torvalds 				 unsigned short mask, unsigned short value)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	int change;
2291da177e4SLinus Torvalds 	unsigned long flags;
2301da177e4SLinus Torvalds 	unsigned short old, new;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
2331da177e4SLinus Torvalds 	old = inw(chip->port + reg);
2341da177e4SLinus Torvalds 	new = (old & ~mask) | value;
2351da177e4SLinus Torvalds 	change = old != new;
2361da177e4SLinus Torvalds 	if (change)
2371da177e4SLinus Torvalds 		outw(new, chip->port + reg);
2381da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
2391da177e4SLinus Torvalds 	return change;
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
242a5f22156STakashi Iwai static void snd_fm801_codec_write(struct snd_ac97 *ac97,
2431da177e4SLinus Torvalds 				  unsigned short reg,
2441da177e4SLinus Torvalds 				  unsigned short val)
2451da177e4SLinus Torvalds {
246a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
2471da177e4SLinus Torvalds 	int idx;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	/*
2501da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
2511da177e4SLinus Torvalds 	 */
2521da177e4SLinus Torvalds 	for (idx = 0; idx < 100; idx++) {
253b1e9ed26STakashi Iwai 		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
2541da177e4SLinus Torvalds 			goto ok1;
2551da177e4SLinus Torvalds 		udelay(10);
2561da177e4SLinus Torvalds 	}
25799b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface is busy (1)\n");
2581da177e4SLinus Torvalds 	return;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds  ok1:
2611da177e4SLinus Torvalds 	/* write data and address */
2621da177e4SLinus Torvalds 	outw(val, FM801_REG(chip, AC97_DATA));
2631da177e4SLinus Torvalds 	outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
2641da177e4SLinus Torvalds 	/*
2651da177e4SLinus Torvalds 	 *  Wait until the write command is not completed..
2661da177e4SLinus Torvalds          */
2671da177e4SLinus Torvalds 	for (idx = 0; idx < 1000; idx++) {
268b1e9ed26STakashi Iwai 		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
2691da177e4SLinus Torvalds 			return;
2701da177e4SLinus Torvalds 		udelay(10);
2711da177e4SLinus Torvalds 	}
27299b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface #%d is busy (2)\n", ac97->num);
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
275a5f22156STakashi Iwai static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
2761da177e4SLinus Torvalds {
277a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
2781da177e4SLinus Torvalds 	int idx;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	/*
2811da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
2821da177e4SLinus Torvalds 	 */
2831da177e4SLinus Torvalds 	for (idx = 0; idx < 100; idx++) {
284b1e9ed26STakashi Iwai 		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
2851da177e4SLinus Torvalds 			goto ok1;
2861da177e4SLinus Torvalds 		udelay(10);
2871da177e4SLinus Torvalds 	}
28899b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface is busy (1)\n");
2891da177e4SLinus Torvalds 	return 0;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds  ok1:
2921da177e4SLinus Torvalds 	/* read command */
293b1e9ed26STakashi Iwai 	outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ,
294b1e9ed26STakashi Iwai 	     FM801_REG(chip, AC97_CMD));
2951da177e4SLinus Torvalds 	for (idx = 0; idx < 100; idx++) {
296b1e9ed26STakashi Iwai 		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
2971da177e4SLinus Torvalds 			goto ok2;
2981da177e4SLinus Torvalds 		udelay(10);
2991da177e4SLinus Torvalds 	}
30099b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface #%d is busy (2)\n", ac97->num);
3011da177e4SLinus Torvalds 	return 0;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds  ok2:
3041da177e4SLinus Torvalds 	for (idx = 0; idx < 1000; idx++) {
305b1e9ed26STakashi Iwai 		if (inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_VALID)
3061da177e4SLinus Torvalds 			goto ok3;
3071da177e4SLinus Torvalds 		udelay(10);
3081da177e4SLinus Torvalds 	}
30999b359baSTakashi Iwai 	snd_printk(KERN_ERR "AC'97 interface #%d is not valid (2)\n", ac97->num);
3101da177e4SLinus Torvalds 	return 0;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds  ok3:
3131da177e4SLinus Torvalds 	return inw(FM801_REG(chip, AC97_DATA));
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds static unsigned int rates[] = {
3171da177e4SLinus Torvalds   5500,  8000,  9600, 11025,
3181da177e4SLinus Torvalds   16000, 19200, 22050, 32000,
3191da177e4SLinus Torvalds   38400, 44100, 48000
3201da177e4SLinus Torvalds };
3211da177e4SLinus Torvalds 
322a5f22156STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
3231da177e4SLinus Torvalds 	.count = ARRAY_SIZE(rates),
3241da177e4SLinus Torvalds 	.list = rates,
3251da177e4SLinus Torvalds 	.mask = 0,
3261da177e4SLinus Torvalds };
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds static unsigned int channels[] = {
3291da177e4SLinus Torvalds   2, 4, 6
3301da177e4SLinus Torvalds };
3311da177e4SLinus Torvalds 
332a5f22156STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
3335e4968e2STobias Klauser 	.count = ARRAY_SIZE(channels),
3341da177e4SLinus Torvalds 	.list = channels,
3351da177e4SLinus Torvalds 	.mask = 0,
3361da177e4SLinus Torvalds };
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /*
3391da177e4SLinus Torvalds  *  Sample rate routines
3401da177e4SLinus Torvalds  */
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds static unsigned short snd_fm801_rate_bits(unsigned int rate)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	unsigned int idx;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
3471da177e4SLinus Torvalds 		if (rates[idx] == rate)
3481da177e4SLinus Torvalds 			return idx;
3491da177e4SLinus Torvalds 	snd_BUG();
3501da177e4SLinus Torvalds 	return ARRAY_SIZE(rates) - 1;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds /*
3541da177e4SLinus Torvalds  *  PCM part
3551da177e4SLinus Torvalds  */
3561da177e4SLinus Torvalds 
357a5f22156STakashi Iwai static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
3581da177e4SLinus Torvalds 				      int cmd)
3591da177e4SLinus Torvalds {
360a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3631da177e4SLinus Torvalds 	switch (cmd) {
3641da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
3651da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_BUF1_LAST |
3661da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
3671da177e4SLinus Torvalds 				     FM801_PAUSE);
3681da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_START |
3691da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
3701da177e4SLinus Torvalds 		break;
3711da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
3721da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
3731da177e4SLinus Torvalds 		break;
3741da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
375b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
3761da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_PAUSE;
3771da177e4SLinus Torvalds 		break;
3781da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
379b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
3801da177e4SLinus Torvalds 		chip->ply_ctrl &= ~FM801_PAUSE;
3811da177e4SLinus Torvalds 		break;
3821da177e4SLinus Torvalds 	default:
3831da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
3841da177e4SLinus Torvalds 		snd_BUG();
3851da177e4SLinus Torvalds 		return -EINVAL;
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 	outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
3881da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3891da177e4SLinus Torvalds 	return 0;
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
392a5f22156STakashi Iwai static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
3931da177e4SLinus Torvalds 				     int cmd)
3941da177e4SLinus Torvalds {
395a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3981da177e4SLinus Torvalds 	switch (cmd) {
3991da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4001da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_BUF1_LAST |
4011da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
4021da177e4SLinus Torvalds 				     FM801_PAUSE);
4031da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_START |
4041da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
4051da177e4SLinus Torvalds 		break;
4061da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4071da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
4081da177e4SLinus Torvalds 		break;
4091da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
410b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
4111da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_PAUSE;
4121da177e4SLinus Torvalds 		break;
4131da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
414b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
4151da177e4SLinus Torvalds 		chip->cap_ctrl &= ~FM801_PAUSE;
4161da177e4SLinus Torvalds 		break;
4171da177e4SLinus Torvalds 	default:
4181da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
4191da177e4SLinus Torvalds 		snd_BUG();
4201da177e4SLinus Torvalds 		return -EINVAL;
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds 	outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
4231da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4241da177e4SLinus Torvalds 	return 0;
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
427a5f22156STakashi Iwai static int snd_fm801_hw_params(struct snd_pcm_substream *substream,
428a5f22156STakashi Iwai 			       struct snd_pcm_hw_params *hw_params)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
433a5f22156STakashi Iwai static int snd_fm801_hw_free(struct snd_pcm_substream *substream)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
438a5f22156STakashi Iwai static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
4391da177e4SLinus Torvalds {
440a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
441a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
4441da177e4SLinus Torvalds 	chip->ply_count = snd_pcm_lib_period_bytes(substream);
4451da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
4461da177e4SLinus Torvalds 	chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
4471da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK |
4481da177e4SLinus Torvalds 			     FM801_CHANNELS_MASK);
4491da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
4501da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_16BIT;
4511da177e4SLinus Torvalds 	if (runtime->channels > 1) {
4521da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_STEREO;
4531da177e4SLinus Torvalds 		if (runtime->channels == 4)
4541da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_4;
4551da177e4SLinus Torvalds 		else if (runtime->channels == 6)
4561da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_6;
4571da177e4SLinus Torvalds 	}
4581da177e4SLinus Torvalds 	chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
4591da177e4SLinus Torvalds 	chip->ply_buf = 0;
4601da177e4SLinus Torvalds 	outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
4611da177e4SLinus Torvalds 	outw(chip->ply_count - 1, FM801_REG(chip, PLY_COUNT));
4621da177e4SLinus Torvalds 	chip->ply_buffer = runtime->dma_addr;
4631da177e4SLinus Torvalds 	chip->ply_pos = 0;
4641da177e4SLinus Torvalds 	outl(chip->ply_buffer, FM801_REG(chip, PLY_BUF1));
4651da177e4SLinus Torvalds 	outl(chip->ply_buffer + (chip->ply_count % chip->ply_size), FM801_REG(chip, PLY_BUF2));
4661da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
4671da177e4SLinus Torvalds 	return 0;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
470a5f22156STakashi Iwai static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
4711da177e4SLinus Torvalds {
472a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
473a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
4761da177e4SLinus Torvalds 	chip->cap_count = snd_pcm_lib_period_bytes(substream);
4771da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
4781da177e4SLinus Torvalds 	chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
4791da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK);
4801da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
4811da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_16BIT;
4821da177e4SLinus Torvalds 	if (runtime->channels > 1)
4831da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_STEREO;
4841da177e4SLinus Torvalds 	chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
4851da177e4SLinus Torvalds 	chip->cap_buf = 0;
4861da177e4SLinus Torvalds 	outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
4871da177e4SLinus Torvalds 	outw(chip->cap_count - 1, FM801_REG(chip, CAP_COUNT));
4881da177e4SLinus Torvalds 	chip->cap_buffer = runtime->dma_addr;
4891da177e4SLinus Torvalds 	chip->cap_pos = 0;
4901da177e4SLinus Torvalds 	outl(chip->cap_buffer, FM801_REG(chip, CAP_BUF1));
4911da177e4SLinus Torvalds 	outl(chip->cap_buffer + (chip->cap_count % chip->cap_size), FM801_REG(chip, CAP_BUF2));
4921da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
4931da177e4SLinus Torvalds 	return 0;
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
496a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
4971da177e4SLinus Torvalds {
498a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
4991da177e4SLinus Torvalds 	size_t ptr;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	if (!(chip->ply_ctrl & FM801_START))
5021da177e4SLinus Torvalds 		return 0;
5031da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
5041da177e4SLinus Torvalds 	ptr = chip->ply_pos + (chip->ply_count - 1) - inw(FM801_REG(chip, PLY_COUNT));
5051da177e4SLinus Torvalds 	if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_PLAYBACK) {
5061da177e4SLinus Torvalds 		ptr += chip->ply_count;
5071da177e4SLinus Torvalds 		ptr %= chip->ply_size;
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5101da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
513a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
5141da177e4SLinus Torvalds {
515a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
5161da177e4SLinus Torvalds 	size_t ptr;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	if (!(chip->cap_ctrl & FM801_START))
5191da177e4SLinus Torvalds 		return 0;
5201da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
5211da177e4SLinus Torvalds 	ptr = chip->cap_pos + (chip->cap_count - 1) - inw(FM801_REG(chip, CAP_COUNT));
5221da177e4SLinus Torvalds 	if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_CAPTURE) {
5231da177e4SLinus Torvalds 		ptr += chip->cap_count;
5241da177e4SLinus Torvalds 		ptr %= chip->cap_size;
5251da177e4SLinus Torvalds 	}
5261da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5271da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5307d12e780SDavid Howells static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
5311da177e4SLinus Torvalds {
532a5f22156STakashi Iwai 	struct fm801 *chip = dev_id;
5331da177e4SLinus Torvalds 	unsigned short status;
5341da177e4SLinus Torvalds 	unsigned int tmp;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	status = inw(FM801_REG(chip, IRQ_STATUS));
5371da177e4SLinus Torvalds 	status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
5381da177e4SLinus Torvalds 	if (! status)
5391da177e4SLinus Torvalds 		return IRQ_NONE;
5401da177e4SLinus Torvalds 	/* ack first */
5411da177e4SLinus Torvalds 	outw(status, FM801_REG(chip, IRQ_STATUS));
5421da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
5431da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5441da177e4SLinus Torvalds 		chip->ply_buf++;
5451da177e4SLinus Torvalds 		chip->ply_pos += chip->ply_count;
5461da177e4SLinus Torvalds 		chip->ply_pos %= chip->ply_size;
5471da177e4SLinus Torvalds 		tmp = chip->ply_pos + chip->ply_count;
5481da177e4SLinus Torvalds 		tmp %= chip->ply_size;
5491da177e4SLinus Torvalds 		outl(chip->ply_buffer + tmp,
5501da177e4SLinus Torvalds 				(chip->ply_buf & 1) ?
5511da177e4SLinus Torvalds 					FM801_REG(chip, PLY_BUF1) :
5521da177e4SLinus Torvalds 					FM801_REG(chip, PLY_BUF2));
5531da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5541da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->playback_substream);
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
5571da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5581da177e4SLinus Torvalds 		chip->cap_buf++;
5591da177e4SLinus Torvalds 		chip->cap_pos += chip->cap_count;
5601da177e4SLinus Torvalds 		chip->cap_pos %= chip->cap_size;
5611da177e4SLinus Torvalds 		tmp = chip->cap_pos + chip->cap_count;
5621da177e4SLinus Torvalds 		tmp %= chip->cap_size;
5631da177e4SLinus Torvalds 		outl(chip->cap_buffer + tmp,
5641da177e4SLinus Torvalds 				(chip->cap_buf & 1) ?
5651da177e4SLinus Torvalds 					FM801_REG(chip, CAP_BUF1) :
5661da177e4SLinus Torvalds 					FM801_REG(chip, CAP_BUF2));
5671da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5681da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->capture_substream);
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds 	if (chip->rmidi && (status & FM801_IRQ_MPU))
5717d12e780SDavid Howells 		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
5721da177e4SLinus Torvalds 	if (status & FM801_IRQ_VOLUME)
5731da177e4SLinus Torvalds 		;/* TODO */
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	return IRQ_HANDLED;
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
578a5f22156STakashi Iwai static struct snd_pcm_hardware snd_fm801_playback =
5791da177e4SLinus Torvalds {
5801da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
5811da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
582b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
5831da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
5841da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
5851da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
5861da177e4SLinus Torvalds 	.rate_min =		5500,
5871da177e4SLinus Torvalds 	.rate_max =		48000,
5881da177e4SLinus Torvalds 	.channels_min =		1,
5891da177e4SLinus Torvalds 	.channels_max =		2,
5901da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
5911da177e4SLinus Torvalds 	.period_bytes_min =	64,
5921da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
5931da177e4SLinus Torvalds 	.periods_min =		1,
5941da177e4SLinus Torvalds 	.periods_max =		1024,
5951da177e4SLinus Torvalds 	.fifo_size =		0,
5961da177e4SLinus Torvalds };
5971da177e4SLinus Torvalds 
598a5f22156STakashi Iwai static struct snd_pcm_hardware snd_fm801_capture =
5991da177e4SLinus Torvalds {
6001da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
6011da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
602b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
6031da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
6041da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
6051da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
6061da177e4SLinus Torvalds 	.rate_min =		5500,
6071da177e4SLinus Torvalds 	.rate_max =		48000,
6081da177e4SLinus Torvalds 	.channels_min =		1,
6091da177e4SLinus Torvalds 	.channels_max =		2,
6101da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
6111da177e4SLinus Torvalds 	.period_bytes_min =	64,
6121da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
6131da177e4SLinus Torvalds 	.periods_min =		1,
6141da177e4SLinus Torvalds 	.periods_max =		1024,
6151da177e4SLinus Torvalds 	.fifo_size =		0,
6161da177e4SLinus Torvalds };
6171da177e4SLinus Torvalds 
618a5f22156STakashi Iwai static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
6191da177e4SLinus Torvalds {
620a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
621a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6221da177e4SLinus Torvalds 	int err;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	chip->playback_substream = substream;
6251da177e4SLinus Torvalds 	runtime->hw = snd_fm801_playback;
626a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
627a5f22156STakashi Iwai 				   &hw_constraints_rates);
6281da177e4SLinus Torvalds 	if (chip->multichannel) {
6291da177e4SLinus Torvalds 		runtime->hw.channels_max = 6;
630a5f22156STakashi Iwai 		snd_pcm_hw_constraint_list(runtime, 0,
631a5f22156STakashi Iwai 					   SNDRV_PCM_HW_PARAM_CHANNELS,
632a5f22156STakashi Iwai 					   &hw_constraints_channels);
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
6351da177e4SLinus Torvalds 		return err;
6361da177e4SLinus Torvalds 	return 0;
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
639a5f22156STakashi Iwai static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
6401da177e4SLinus Torvalds {
641a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
642a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6431da177e4SLinus Torvalds 	int err;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	chip->capture_substream = substream;
6461da177e4SLinus Torvalds 	runtime->hw = snd_fm801_capture;
647a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
648a5f22156STakashi Iwai 				   &hw_constraints_rates);
6491da177e4SLinus Torvalds 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
6501da177e4SLinus Torvalds 		return err;
6511da177e4SLinus Torvalds 	return 0;
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
654a5f22156STakashi Iwai static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
6551da177e4SLinus Torvalds {
656a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	chip->playback_substream = NULL;
6591da177e4SLinus Torvalds 	return 0;
6601da177e4SLinus Torvalds }
6611da177e4SLinus Torvalds 
662a5f22156STakashi Iwai static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
6631da177e4SLinus Torvalds {
664a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	chip->capture_substream = NULL;
6671da177e4SLinus Torvalds 	return 0;
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
670a5f22156STakashi Iwai static struct snd_pcm_ops snd_fm801_playback_ops = {
6711da177e4SLinus Torvalds 	.open =		snd_fm801_playback_open,
6721da177e4SLinus Torvalds 	.close =	snd_fm801_playback_close,
6731da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
6741da177e4SLinus Torvalds 	.hw_params =	snd_fm801_hw_params,
6751da177e4SLinus Torvalds 	.hw_free =	snd_fm801_hw_free,
6761da177e4SLinus Torvalds 	.prepare =	snd_fm801_playback_prepare,
6771da177e4SLinus Torvalds 	.trigger =	snd_fm801_playback_trigger,
6781da177e4SLinus Torvalds 	.pointer =	snd_fm801_playback_pointer,
6791da177e4SLinus Torvalds };
6801da177e4SLinus Torvalds 
681a5f22156STakashi Iwai static struct snd_pcm_ops snd_fm801_capture_ops = {
6821da177e4SLinus Torvalds 	.open =		snd_fm801_capture_open,
6831da177e4SLinus Torvalds 	.close =	snd_fm801_capture_close,
6841da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
6851da177e4SLinus Torvalds 	.hw_params =	snd_fm801_hw_params,
6861da177e4SLinus Torvalds 	.hw_free =	snd_fm801_hw_free,
6871da177e4SLinus Torvalds 	.prepare =	snd_fm801_capture_prepare,
6881da177e4SLinus Torvalds 	.trigger =	snd_fm801_capture_trigger,
6891da177e4SLinus Torvalds 	.pointer =	snd_fm801_capture_pointer,
6901da177e4SLinus Torvalds };
6911da177e4SLinus Torvalds 
692e23e7a14SBill Pemberton static int snd_fm801_pcm(struct fm801 *chip, int device, struct snd_pcm **rpcm)
6931da177e4SLinus Torvalds {
694a5f22156STakashi Iwai 	struct snd_pcm *pcm;
6951da177e4SLinus Torvalds 	int err;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	if (rpcm)
6981da177e4SLinus Torvalds 		*rpcm = NULL;
6991da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm)) < 0)
7001da177e4SLinus Torvalds 		return err;
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
7031da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	pcm->private_data = chip;
7061da177e4SLinus Torvalds 	pcm->info_flags = 0;
7071da177e4SLinus Torvalds 	strcpy(pcm->name, "FM801");
7081da177e4SLinus Torvalds 	chip->pcm = pcm;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
7111da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci),
7121da177e4SLinus Torvalds 					      chip->multichannel ? 128*1024 : 64*1024, 128*1024);
7131da177e4SLinus Torvalds 
714e36e3b86STakashi Iwai 	err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
715e36e3b86STakashi Iwai 				     snd_pcm_alt_chmaps,
716e36e3b86STakashi Iwai 				     chip->multichannel ? 6 : 2, 0,
717e36e3b86STakashi Iwai 				     NULL);
718e36e3b86STakashi Iwai 	if (err < 0)
719e36e3b86STakashi Iwai 		return err;
720e36e3b86STakashi Iwai 
7211da177e4SLinus Torvalds 	if (rpcm)
7221da177e4SLinus Torvalds 		*rpcm = pcm;
7231da177e4SLinus Torvalds 	return 0;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds /*
7271da177e4SLinus Torvalds  *  TEA5757 radio
7281da177e4SLinus Torvalds  */
7291da177e4SLinus Torvalds 
730fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
7311da177e4SLinus Torvalds 
732938a1566SOndrej Zary /* GPIO to TEA575x maps */
733938a1566SOndrej Zary struct snd_fm801_tea575x_gpio {
734938a1566SOndrej Zary 	u8 data, clk, wren, most;
735d7ba858aSOndrej Zary 	char *name;
736938a1566SOndrej Zary };
7371da177e4SLinus Torvalds 
738938a1566SOndrej Zary static struct snd_fm801_tea575x_gpio snd_fm801_tea575x_gpios[] = {
739d7ba858aSOndrej Zary 	{ .data = 1, .clk = 3, .wren = 2, .most = 0, .name = "SF256-PCS" },
740d7ba858aSOndrej Zary 	{ .data = 1, .clk = 0, .wren = 2, .most = 3, .name = "SF256-PCP" },
741d7ba858aSOndrej Zary 	{ .data = 2, .clk = 0, .wren = 1, .most = 3, .name = "SF64-PCR" },
742938a1566SOndrej Zary };
743938a1566SOndrej Zary 
7448e699d2cSTakashi Iwai #define get_tea575x_gpio(chip) \
7458e699d2cSTakashi Iwai 	(&snd_fm801_tea575x_gpios[((chip)->tea575x_tuner & TUNER_TYPE_MASK) - 1])
7468e699d2cSTakashi Iwai 
747938a1566SOndrej Zary static void snd_fm801_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
7481da177e4SLinus Torvalds {
749a5f22156STakashi Iwai 	struct fm801 *chip = tea->private_data;
750938a1566SOndrej Zary 	unsigned short reg = inw(FM801_REG(chip, GPIO_CTRL));
7518e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
7521da177e4SLinus Torvalds 
753938a1566SOndrej Zary 	reg &= ~(FM801_GPIO_GP(gpio.data) |
754938a1566SOndrej Zary 		 FM801_GPIO_GP(gpio.clk) |
755938a1566SOndrej Zary 		 FM801_GPIO_GP(gpio.wren));
756938a1566SOndrej Zary 
757938a1566SOndrej Zary 	reg |= (pins & TEA575X_DATA) ? FM801_GPIO_GP(gpio.data) : 0;
758938a1566SOndrej Zary 	reg |= (pins & TEA575X_CLK)  ? FM801_GPIO_GP(gpio.clk) : 0;
759938a1566SOndrej Zary 	/* WRITE_ENABLE is inverted */
760938a1566SOndrej Zary 	reg |= (pins & TEA575X_WREN) ? 0 : FM801_GPIO_GP(gpio.wren);
761938a1566SOndrej Zary 
762938a1566SOndrej Zary 	outw(reg, FM801_REG(chip, GPIO_CTRL));
763938a1566SOndrej Zary }
764938a1566SOndrej Zary 
765938a1566SOndrej Zary static u8 snd_fm801_tea575x_get_pins(struct snd_tea575x *tea)
766938a1566SOndrej Zary {
767938a1566SOndrej Zary 	struct fm801 *chip = tea->private_data;
768938a1566SOndrej Zary 	unsigned short reg = inw(FM801_REG(chip, GPIO_CTRL));
7698e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
770effded75SDan Carpenter 	u8 ret;
771938a1566SOndrej Zary 
772effded75SDan Carpenter 	ret = 0;
773effded75SDan Carpenter 	if (reg & FM801_GPIO_GP(gpio.data))
774effded75SDan Carpenter 		ret |= TEA575X_DATA;
775effded75SDan Carpenter 	if (reg & FM801_GPIO_GP(gpio.most))
776effded75SDan Carpenter 		ret |= TEA575X_MOST;
777effded75SDan Carpenter 	return ret;
778938a1566SOndrej Zary }
779938a1566SOndrej Zary 
780938a1566SOndrej Zary static void snd_fm801_tea575x_set_direction(struct snd_tea575x *tea, bool output)
781938a1566SOndrej Zary {
782938a1566SOndrej Zary 	struct fm801 *chip = tea->private_data;
783938a1566SOndrej Zary 	unsigned short reg = inw(FM801_REG(chip, GPIO_CTRL));
7848e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
785938a1566SOndrej Zary 
7861da177e4SLinus Torvalds 	/* use GPIO lines and set write enable bit */
787938a1566SOndrej Zary 	reg |= FM801_GPIO_GS(gpio.data) |
788938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.wren) |
789938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.clk) |
790938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.most);
791938a1566SOndrej Zary 	if (output) {
7921da177e4SLinus Torvalds 		/* all of lines are in the write direction */
7931da177e4SLinus Torvalds 		/* clear data and clock lines */
794938a1566SOndrej Zary 		reg &= ~(FM801_GPIO_GD(gpio.data) |
795938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.wren) |
796938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.clk) |
797938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.data) |
798938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.clk) |
799938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.wren));
800938a1566SOndrej Zary 	} else {
8011da177e4SLinus Torvalds 		/* use GPIO lines, set data direction to input */
802938a1566SOndrej Zary 		reg |= FM801_GPIO_GD(gpio.data) |
803938a1566SOndrej Zary 		       FM801_GPIO_GD(gpio.most) |
804938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.data) |
805938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.most) |
806938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.wren);
8071da177e4SLinus Torvalds 		/* all of lines are in the write direction, except data */
8081da177e4SLinus Torvalds 		/* clear data, write enable and clock lines */
809938a1566SOndrej Zary 		reg &= ~(FM801_GPIO_GD(gpio.wren) |
810938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.clk) |
811938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.clk));
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds 
814938a1566SOndrej Zary 	outw(reg, FM801_REG(chip, GPIO_CTRL));
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
817938a1566SOndrej Zary static struct snd_tea575x_ops snd_fm801_tea_ops = {
818938a1566SOndrej Zary 	.set_pins = snd_fm801_tea575x_set_pins,
819938a1566SOndrej Zary 	.get_pins = snd_fm801_tea575x_get_pins,
820938a1566SOndrej Zary 	.set_direction = snd_fm801_tea575x_set_direction,
8211da177e4SLinus Torvalds };
8221da177e4SLinus Torvalds #endif
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds /*
8251da177e4SLinus Torvalds  *  Mixer routines
8261da177e4SLinus Torvalds  */
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds #define FM801_SINGLE(xname, reg, shift, mask, invert) \
8291da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
8301da177e4SLinus Torvalds   .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
8311da177e4SLinus Torvalds   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
8321da177e4SLinus Torvalds 
833a5f22156STakashi Iwai static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
834a5f22156STakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
8351da177e4SLinus Torvalds {
8361da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
8391da177e4SLinus Torvalds 	uinfo->count = 1;
8401da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
8411da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
8421da177e4SLinus Torvalds 	return 0;
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
845a5f22156STakashi Iwai static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
846a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
8471da177e4SLinus Torvalds {
848a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
8491da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
8501da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
8511da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8521da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift) & mask;
8551da177e4SLinus Torvalds 	if (invert)
8561da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
8571da177e4SLinus Torvalds 	return 0;
8581da177e4SLinus Torvalds }
8591da177e4SLinus Torvalds 
860a5f22156STakashi Iwai static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
861a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
8621da177e4SLinus Torvalds {
863a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
8641da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
8651da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
8661da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8671da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
8681da177e4SLinus Torvalds 	unsigned short val;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
8711da177e4SLinus Torvalds 	if (invert)
8721da177e4SLinus Torvalds 		val = mask - val;
8731da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds #define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
8771da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
8781da177e4SLinus Torvalds   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
8791da177e4SLinus Torvalds   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
880666c70ffSTakashi Iwai #define FM801_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
881666c70ffSTakashi Iwai { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
882666c70ffSTakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
883666c70ffSTakashi Iwai   .name = xname, .info = snd_fm801_info_double, \
884666c70ffSTakashi Iwai   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
885666c70ffSTakashi Iwai   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
886666c70ffSTakashi Iwai   .tlv = { .p = (xtlv) } }
8871da177e4SLinus Torvalds 
888a5f22156STakashi Iwai static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
889a5f22156STakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
8901da177e4SLinus Torvalds {
8911da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
8941da177e4SLinus Torvalds 	uinfo->count = 2;
8951da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
8961da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
8971da177e4SLinus Torvalds 	return 0;
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
900a5f22156STakashi Iwai static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
901a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
9021da177e4SLinus Torvalds {
903a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9041da177e4SLinus Torvalds         int reg = kcontrol->private_value & 0xff;
9051da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
9061da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
9071da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
9081da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9111da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift_left) & mask;
9121da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = (inw(chip->port + reg) >> shift_right) & mask;
9131da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9141da177e4SLinus Torvalds 	if (invert) {
9151da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
9161da177e4SLinus Torvalds 		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
9171da177e4SLinus Torvalds 	}
9181da177e4SLinus Torvalds 	return 0;
9191da177e4SLinus Torvalds }
9201da177e4SLinus Torvalds 
921a5f22156STakashi Iwai static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
922a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
9231da177e4SLinus Torvalds {
924a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9251da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
9261da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
9271da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
9281da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
9291da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
9301da177e4SLinus Torvalds 	unsigned short val1, val2;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
9331da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
9341da177e4SLinus Torvalds 	if (invert) {
9351da177e4SLinus Torvalds 		val1 = mask - val1;
9361da177e4SLinus Torvalds 		val2 = mask - val2;
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg,
9391da177e4SLinus Torvalds 				     (mask << shift_left) | (mask << shift_right),
9401da177e4SLinus Torvalds 				     (val1 << shift_left ) | (val2 << shift_right));
9411da177e4SLinus Torvalds }
9421da177e4SLinus Torvalds 
943a5f22156STakashi Iwai static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
944a5f22156STakashi Iwai 			      struct snd_ctl_elem_info *uinfo)
9451da177e4SLinus Torvalds {
9461da177e4SLinus Torvalds 	static char *texts[5] = {
9471da177e4SLinus Torvalds 		"AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
9481da177e4SLinus Torvalds 	};
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
9511da177e4SLinus Torvalds 	uinfo->count = 1;
9521da177e4SLinus Torvalds 	uinfo->value.enumerated.items = 5;
9531da177e4SLinus Torvalds 	if (uinfo->value.enumerated.item > 4)
9541da177e4SLinus Torvalds 		uinfo->value.enumerated.item = 4;
9551da177e4SLinus Torvalds 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
9561da177e4SLinus Torvalds 	return 0;
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
959a5f22156STakashi Iwai static int snd_fm801_get_mux(struct snd_kcontrol *kcontrol,
960a5f22156STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
9611da177e4SLinus Torvalds {
962a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9631da177e4SLinus Torvalds         unsigned short val;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	val = inw(FM801_REG(chip, REC_SRC)) & 7;
9661da177e4SLinus Torvalds 	if (val > 4)
9671da177e4SLinus Torvalds 		val = 4;
9681da177e4SLinus Torvalds         ucontrol->value.enumerated.item[0] = val;
9691da177e4SLinus Torvalds         return 0;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
972a5f22156STakashi Iwai static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
973a5f22156STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
9741da177e4SLinus Torvalds {
975a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9761da177e4SLinus Torvalds         unsigned short val;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds         if ((val = ucontrol->value.enumerated.item[0]) > 4)
9791da177e4SLinus Torvalds                 return -EINVAL;
9801da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
9811da177e4SLinus Torvalds }
9821da177e4SLinus Torvalds 
9830cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
984666c70ffSTakashi Iwai 
985a5f22156STakashi Iwai #define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
9861da177e4SLinus Torvalds 
987e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_fm801_controls[] = {
988666c70ffSTakashi Iwai FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
989666c70ffSTakashi Iwai 		 db_scale_dsp),
9901da177e4SLinus Torvalds FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
991666c70ffSTakashi Iwai FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
992666c70ffSTakashi Iwai 		 db_scale_dsp),
9931da177e4SLinus Torvalds FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
994666c70ffSTakashi Iwai FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
995666c70ffSTakashi Iwai 		 db_scale_dsp),
9961da177e4SLinus Torvalds FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
9971da177e4SLinus Torvalds {
9981da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
9991da177e4SLinus Torvalds 	.name = "Digital Capture Source",
10001da177e4SLinus Torvalds 	.info = snd_fm801_info_mux,
10011da177e4SLinus Torvalds 	.get = snd_fm801_get_mux,
10021da177e4SLinus Torvalds 	.put = snd_fm801_put_mux,
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds };
10051da177e4SLinus Torvalds 
1006a5f22156STakashi Iwai #define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
10071da177e4SLinus Torvalds 
1008e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_fm801_controls_multi[] = {
10091da177e4SLinus Torvalds FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
10101da177e4SLinus Torvalds FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
101110e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
101210e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
101310e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
101410e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
10151da177e4SLinus Torvalds };
10161da177e4SLinus Torvalds 
1017a5f22156STakashi Iwai static void snd_fm801_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
10181da177e4SLinus Torvalds {
1019a5f22156STakashi Iwai 	struct fm801 *chip = bus->private_data;
10201da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
10211da177e4SLinus Torvalds }
10221da177e4SLinus Torvalds 
1023a5f22156STakashi Iwai static void snd_fm801_mixer_free_ac97(struct snd_ac97 *ac97)
10241da177e4SLinus Torvalds {
1025a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
10261da177e4SLinus Torvalds 	if (ac97->num == 0) {
10271da177e4SLinus Torvalds 		chip->ac97 = NULL;
10281da177e4SLinus Torvalds 	} else {
10291da177e4SLinus Torvalds 		chip->ac97_sec = NULL;
10301da177e4SLinus Torvalds 	}
10311da177e4SLinus Torvalds }
10321da177e4SLinus Torvalds 
1033e23e7a14SBill Pemberton static int snd_fm801_mixer(struct fm801 *chip)
10341da177e4SLinus Torvalds {
1035a5f22156STakashi Iwai 	struct snd_ac97_template ac97;
10361da177e4SLinus Torvalds 	unsigned int i;
10371da177e4SLinus Torvalds 	int err;
1038a5f22156STakashi Iwai 	static struct snd_ac97_bus_ops ops = {
10391da177e4SLinus Torvalds 		.write = snd_fm801_codec_write,
10401da177e4SLinus Torvalds 		.read = snd_fm801_codec_read,
10411da177e4SLinus Torvalds 	};
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
10441da177e4SLinus Torvalds 		return err;
10451da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_fm801_mixer_free_ac97_bus;
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
10481da177e4SLinus Torvalds 	ac97.private_data = chip;
10491da177e4SLinus Torvalds 	ac97.private_free = snd_fm801_mixer_free_ac97;
10501da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
10511da177e4SLinus Torvalds 		return err;
10521da177e4SLinus Torvalds 	if (chip->secondary) {
10531da177e4SLinus Torvalds 		ac97.num = 1;
10541da177e4SLinus Torvalds 		ac97.addr = chip->secondary_addr;
10551da177e4SLinus Torvalds 		if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec)) < 0)
10561da177e4SLinus Torvalds 			return err;
10571da177e4SLinus Torvalds 	}
10581da177e4SLinus Torvalds 	for (i = 0; i < FM801_CONTROLS; i++)
10591da177e4SLinus Torvalds 		snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls[i], chip));
10601da177e4SLinus Torvalds 	if (chip->multichannel) {
10611da177e4SLinus Torvalds 		for (i = 0; i < FM801_CONTROLS_MULTI; i++)
10621da177e4SLinus Torvalds 			snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
10631da177e4SLinus Torvalds 	}
10641da177e4SLinus Torvalds 	return 0;
10651da177e4SLinus Torvalds }
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds /*
10681da177e4SLinus Torvalds  *  initialization routines
10691da177e4SLinus Torvalds  */
10701da177e4SLinus Torvalds 
1071b1e9ed26STakashi Iwai static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
1072b1e9ed26STakashi Iwai 			  unsigned short reg, unsigned long waits)
1073b1e9ed26STakashi Iwai {
1074b1e9ed26STakashi Iwai 	unsigned long timeout = jiffies + waits;
1075b1e9ed26STakashi Iwai 
1076b1e9ed26STakashi Iwai 	outw(FM801_AC97_READ | (codec_id << FM801_AC97_ADDR_SHIFT) | reg,
1077b1e9ed26STakashi Iwai 	     FM801_REG(chip, AC97_CMD));
1078b1e9ed26STakashi Iwai 	udelay(5);
1079b1e9ed26STakashi Iwai 	do {
1080b1e9ed26STakashi Iwai 		if ((inw(FM801_REG(chip, AC97_CMD)) & (FM801_AC97_VALID|FM801_AC97_BUSY))
1081b1e9ed26STakashi Iwai 		    == FM801_AC97_VALID)
1082b1e9ed26STakashi Iwai 			return 0;
1083b1e9ed26STakashi Iwai 		schedule_timeout_uninterruptible(1);
1084b1e9ed26STakashi Iwai 	} while (time_after(timeout, jiffies));
1085b1e9ed26STakashi Iwai 	return -EIO;
1086b1e9ed26STakashi Iwai }
1087b1e9ed26STakashi Iwai 
1088b1e9ed26STakashi Iwai static int snd_fm801_chip_init(struct fm801 *chip, int resume)
1089b1e9ed26STakashi Iwai {
1090b1e9ed26STakashi Iwai 	unsigned short cmdw;
1091b1e9ed26STakashi Iwai 
1092fb716c0bSOndrej Zary 	if (chip->tea575x_tuner & TUNER_ONLY)
1093e0a5d82aSAndy Shevchenko 		goto __ac97_ok;
1094e0a5d82aSAndy Shevchenko 
1095b1e9ed26STakashi Iwai 	/* codec cold reset + AC'97 warm reset */
1096b1e9ed26STakashi Iwai 	outw((1<<5) | (1<<6), FM801_REG(chip, CODEC_CTRL));
1097b1e9ed26STakashi Iwai 	inw(FM801_REG(chip, CODEC_CTRL)); /* flush posting data */
1098b1e9ed26STakashi Iwai 	udelay(100);
1099b1e9ed26STakashi Iwai 	outw(0, FM801_REG(chip, CODEC_CTRL));
1100b1e9ed26STakashi Iwai 
1101fb716c0bSOndrej Zary 	if (wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750)) < 0)
1102fb716c0bSOndrej Zary 		if (!resume) {
1103fb716c0bSOndrej Zary 			snd_printk(KERN_INFO "Primary AC'97 codec not found, "
1104fb716c0bSOndrej Zary 					    "assume SF64-PCR (tuner-only)\n");
1105fb716c0bSOndrej Zary 			chip->tea575x_tuner = 3 | TUNER_ONLY;
1106fb716c0bSOndrej Zary 			goto __ac97_ok;
1107b1e9ed26STakashi Iwai 		}
1108b1e9ed26STakashi Iwai 
1109b1e9ed26STakashi Iwai 	if (chip->multichannel) {
1110b1e9ed26STakashi Iwai 		if (chip->secondary_addr) {
1111b1e9ed26STakashi Iwai 			wait_for_codec(chip, chip->secondary_addr,
1112b1e9ed26STakashi Iwai 				       AC97_VENDOR_ID1, msecs_to_jiffies(50));
1113b1e9ed26STakashi Iwai 		} else {
1114b1e9ed26STakashi Iwai 			/* my card has the secondary codec */
1115b1e9ed26STakashi Iwai 			/* at address #3, so the loop is inverted */
111658e4334eSHarvey Harrison 			int i;
111758e4334eSHarvey Harrison 			for (i = 3; i > 0; i--) {
111858e4334eSHarvey Harrison 				if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
1119b1e9ed26STakashi Iwai 						     msecs_to_jiffies(50))) {
1120b1e9ed26STakashi Iwai 					cmdw = inw(FM801_REG(chip, AC97_DATA));
1121b1e9ed26STakashi Iwai 					if (cmdw != 0xffff && cmdw != 0) {
1122b1e9ed26STakashi Iwai 						chip->secondary = 1;
112358e4334eSHarvey Harrison 						chip->secondary_addr = i;
1124b1e9ed26STakashi Iwai 						break;
1125b1e9ed26STakashi Iwai 					}
1126b1e9ed26STakashi Iwai 				}
1127b1e9ed26STakashi Iwai 			}
1128b1e9ed26STakashi Iwai 		}
1129b1e9ed26STakashi Iwai 
1130b1e9ed26STakashi Iwai 		/* the recovery phase, it seems that probing for non-existing codec might */
1131b1e9ed26STakashi Iwai 		/* cause timeout problems */
1132b1e9ed26STakashi Iwai 		wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
1133b1e9ed26STakashi Iwai 	}
1134b1e9ed26STakashi Iwai 
11356bbe13ecSJaroslav Kysela       __ac97_ok:
11366bbe13ecSJaroslav Kysela 
1137b1e9ed26STakashi Iwai 	/* init volume */
1138b1e9ed26STakashi Iwai 	outw(0x0808, FM801_REG(chip, PCM_VOL));
1139b1e9ed26STakashi Iwai 	outw(0x9f1f, FM801_REG(chip, FM_VOL));
1140b1e9ed26STakashi Iwai 	outw(0x8808, FM801_REG(chip, I2S_VOL));
1141b1e9ed26STakashi Iwai 
1142b1e9ed26STakashi Iwai 	/* I2S control - I2S mode */
1143b1e9ed26STakashi Iwai 	outw(0x0003, FM801_REG(chip, I2S_MODE));
1144b1e9ed26STakashi Iwai 
11456bbe13ecSJaroslav Kysela 	/* interrupt setup */
1146b1e9ed26STakashi Iwai 	cmdw = inw(FM801_REG(chip, IRQ_MASK));
11476bbe13ecSJaroslav Kysela 	if (chip->irq < 0)
11486bbe13ecSJaroslav Kysela 		cmdw |= 0x00c3;		/* mask everything, no PCM nor MPU */
11496bbe13ecSJaroslav Kysela 	else
11506bbe13ecSJaroslav Kysela 		cmdw &= ~0x0083;	/* unmask MPU, PLAYBACK & CAPTURE */
1151b1e9ed26STakashi Iwai 	outw(cmdw, FM801_REG(chip, IRQ_MASK));
1152b1e9ed26STakashi Iwai 
1153b1e9ed26STakashi Iwai 	/* interrupt clear */
1154b1e9ed26STakashi Iwai 	outw(FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU, FM801_REG(chip, IRQ_STATUS));
1155b1e9ed26STakashi Iwai 
1156b1e9ed26STakashi Iwai 	return 0;
1157b1e9ed26STakashi Iwai }
1158b1e9ed26STakashi Iwai 
1159b1e9ed26STakashi Iwai 
1160a5f22156STakashi Iwai static int snd_fm801_free(struct fm801 *chip)
11611da177e4SLinus Torvalds {
11621da177e4SLinus Torvalds 	unsigned short cmdw;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	if (chip->irq < 0)
11651da177e4SLinus Torvalds 		goto __end_hw;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	/* interrupt setup - mask everything */
11681da177e4SLinus Torvalds 	cmdw = inw(FM801_REG(chip, IRQ_MASK));
11691da177e4SLinus Torvalds 	cmdw |= 0x00c3;
11701da177e4SLinus Torvalds 	outw(cmdw, FM801_REG(chip, IRQ_MASK));
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds       __end_hw:
1173fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1174d4ecc83bSHans Verkuil 	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
11751da177e4SLinus Torvalds 		snd_tea575x_exit(&chip->tea);
1176d4ecc83bSHans Verkuil 		v4l2_device_unregister(&chip->v4l2_dev);
1177d4ecc83bSHans Verkuil 	}
11781da177e4SLinus Torvalds #endif
11791da177e4SLinus Torvalds 	if (chip->irq >= 0)
1180a5f22156STakashi Iwai 		free_irq(chip->irq, chip);
11811da177e4SLinus Torvalds 	pci_release_regions(chip->pci);
11821da177e4SLinus Torvalds 	pci_disable_device(chip->pci);
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	kfree(chip);
11851da177e4SLinus Torvalds 	return 0;
11861da177e4SLinus Torvalds }
11871da177e4SLinus Torvalds 
1188a5f22156STakashi Iwai static int snd_fm801_dev_free(struct snd_device *device)
11891da177e4SLinus Torvalds {
1190a5f22156STakashi Iwai 	struct fm801 *chip = device->device_data;
11911da177e4SLinus Torvalds 	return snd_fm801_free(chip);
11921da177e4SLinus Torvalds }
11931da177e4SLinus Torvalds 
1194e23e7a14SBill Pemberton static int snd_fm801_create(struct snd_card *card,
11951da177e4SLinus Torvalds 			    struct pci_dev *pci,
11961da177e4SLinus Torvalds 			    int tea575x_tuner,
1197d4ecc83bSHans Verkuil 			    int radio_nr,
1198a5f22156STakashi Iwai 			    struct fm801 **rchip)
11991da177e4SLinus Torvalds {
1200a5f22156STakashi Iwai 	struct fm801 *chip;
12011da177e4SLinus Torvalds 	int err;
1202a5f22156STakashi Iwai 	static struct snd_device_ops ops = {
12031da177e4SLinus Torvalds 		.dev_free =	snd_fm801_dev_free,
12041da177e4SLinus Torvalds 	};
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	*rchip = NULL;
12071da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
12081da177e4SLinus Torvalds 		return err;
1209e560d8d8STakashi Iwai 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
12101da177e4SLinus Torvalds 	if (chip == NULL) {
12111da177e4SLinus Torvalds 		pci_disable_device(pci);
12121da177e4SLinus Torvalds 		return -ENOMEM;
12131da177e4SLinus Torvalds 	}
12141da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
12151da177e4SLinus Torvalds 	chip->card = card;
12161da177e4SLinus Torvalds 	chip->pci = pci;
12171da177e4SLinus Torvalds 	chip->irq = -1;
12186bbe13ecSJaroslav Kysela 	chip->tea575x_tuner = tea575x_tuner;
12191da177e4SLinus Torvalds 	if ((err = pci_request_regions(pci, "FM801")) < 0) {
12201da177e4SLinus Torvalds 		kfree(chip);
12211da177e4SLinus Torvalds 		pci_disable_device(pci);
12221da177e4SLinus Torvalds 		return err;
12231da177e4SLinus Torvalds 	}
12241da177e4SLinus Torvalds 	chip->port = pci_resource_start(pci, 0);
1225fb716c0bSOndrej Zary 	if ((tea575x_tuner & TUNER_ONLY) == 0) {
1226437a5a46STakashi Iwai 		if (request_irq(pci->irq, snd_fm801_interrupt, IRQF_SHARED,
1227934c2b6dSTakashi Iwai 				KBUILD_MODNAME, chip)) {
122899b359baSTakashi Iwai 			snd_printk(KERN_ERR "unable to grab IRQ %d\n", chip->irq);
12291da177e4SLinus Torvalds 			snd_fm801_free(chip);
12301da177e4SLinus Torvalds 			return -EBUSY;
12311da177e4SLinus Torvalds 		}
12321da177e4SLinus Torvalds 		chip->irq = pci->irq;
12331da177e4SLinus Torvalds 		pci_set_master(pci);
12346bbe13ecSJaroslav Kysela 	}
12351da177e4SLinus Torvalds 
123644c10138SAuke Kok 	if (pci->revision >= 0xb1)	/* FM801-AU */
12371da177e4SLinus Torvalds 		chip->multichannel = 1;
12381da177e4SLinus Torvalds 
1239b1e9ed26STakashi Iwai 	snd_fm801_chip_init(chip, 0);
1240fb716c0bSOndrej Zary 	/* init might set tuner access method */
1241fb716c0bSOndrej Zary 	tea575x_tuner = chip->tea575x_tuner;
1242fb716c0bSOndrej Zary 
1243fb716c0bSOndrej Zary 	if (chip->irq >= 0 && (tea575x_tuner & TUNER_ONLY)) {
1244fb716c0bSOndrej Zary 		pci_clear_master(pci);
1245fb716c0bSOndrej Zary 		free_irq(chip->irq, chip);
1246fb716c0bSOndrej Zary 		chip->irq = -1;
1247fb716c0bSOndrej Zary 	}
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
12501da177e4SLinus Torvalds 		snd_fm801_free(chip);
12511da177e4SLinus Torvalds 		return err;
12521da177e4SLinus Torvalds 	}
12531da177e4SLinus Torvalds 
1254fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1255d4ecc83bSHans Verkuil 	err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
1256d4ecc83bSHans Verkuil 	if (err < 0) {
1257d4ecc83bSHans Verkuil 		snd_fm801_free(chip);
1258d4ecc83bSHans Verkuil 		return err;
1259d4ecc83bSHans Verkuil 	}
1260d4ecc83bSHans Verkuil 	chip->tea.v4l2_dev = &chip->v4l2_dev;
1261d4ecc83bSHans Verkuil 	chip->tea.radio_nr = radio_nr;
12621da177e4SLinus Torvalds 	chip->tea.private_data = chip;
1263938a1566SOndrej Zary 	chip->tea.ops = &snd_fm801_tea_ops;
126410ca7201SOndrej Zary 	sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
1265d7ba858aSOndrej Zary 	if ((tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
1266d7ba858aSOndrej Zary 	    (tea575x_tuner & TUNER_TYPE_MASK) < 4) {
12675daf53a6SHans de Goede 		if (snd_tea575x_init(&chip->tea, THIS_MODULE)) {
1268d7ba858aSOndrej Zary 			snd_printk(KERN_ERR "TEA575x radio not found\n");
1269d4ecc83bSHans Verkuil 			snd_fm801_free(chip);
127096760015SDan Carpenter 			return -ENODEV;
127196760015SDan Carpenter 		}
127296760015SDan Carpenter 	} else if ((tea575x_tuner & TUNER_TYPE_MASK) == 0) {
1273d7ba858aSOndrej Zary 		/* autodetect tuner connection */
1274d7ba858aSOndrej Zary 		for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
1275d7ba858aSOndrej Zary 			chip->tea575x_tuner = tea575x_tuner;
12765daf53a6SHans de Goede 			if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
1277d7ba858aSOndrej Zary 				snd_printk(KERN_INFO "detected TEA575x radio type %s\n",
12788e699d2cSTakashi Iwai 					   get_tea575x_gpio(chip)->name);
1279d7ba858aSOndrej Zary 				break;
1280d7ba858aSOndrej Zary 			}
12811da177e4SLinus Torvalds 		}
128296760015SDan Carpenter 		if (tea575x_tuner == 4) {
128396760015SDan Carpenter 			snd_printk(KERN_ERR "TEA575x radio not found\n");
1284c37279b9SBen Hutchings 			chip->tea575x_tuner = TUNER_DISABLED;
128596760015SDan Carpenter 		}
128696760015SDan Carpenter 	}
1287c37279b9SBen Hutchings 	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
12888e699d2cSTakashi Iwai 		strlcpy(chip->tea.card, get_tea575x_gpio(chip)->name,
1289c37279b9SBen Hutchings 			sizeof(chip->tea.card));
1290c37279b9SBen Hutchings 	}
12911da177e4SLinus Torvalds #endif
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	*rchip = chip;
12941da177e4SLinus Torvalds 	return 0;
12951da177e4SLinus Torvalds }
12961da177e4SLinus Torvalds 
1297e23e7a14SBill Pemberton static int snd_card_fm801_probe(struct pci_dev *pci,
12981da177e4SLinus Torvalds 				const struct pci_device_id *pci_id)
12991da177e4SLinus Torvalds {
13001da177e4SLinus Torvalds 	static int dev;
1301a5f22156STakashi Iwai 	struct snd_card *card;
1302a5f22156STakashi Iwai 	struct fm801 *chip;
1303a5f22156STakashi Iwai 	struct snd_opl3 *opl3;
13041da177e4SLinus Torvalds 	int err;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds         if (dev >= SNDRV_CARDS)
13071da177e4SLinus Torvalds                 return -ENODEV;
13081da177e4SLinus Torvalds 	if (!enable[dev]) {
13091da177e4SLinus Torvalds 		dev++;
13101da177e4SLinus Torvalds 		return -ENOENT;
13111da177e4SLinus Torvalds 	}
13121da177e4SLinus Torvalds 
1313*60c5772bSTakashi Iwai 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1314*60c5772bSTakashi Iwai 			   0, &card);
1315e58de7baSTakashi Iwai 	if (err < 0)
1316e58de7baSTakashi Iwai 		return err;
1317d4ecc83bSHans Verkuil 	if ((err = snd_fm801_create(card, pci, tea575x_tuner[dev], radio_nr[dev], &chip)) < 0) {
13181da177e4SLinus Torvalds 		snd_card_free(card);
13191da177e4SLinus Torvalds 		return err;
13201da177e4SLinus Torvalds 	}
1321b1e9ed26STakashi Iwai 	card->private_data = chip;
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 	strcpy(card->driver, "FM801");
13241da177e4SLinus Torvalds 	strcpy(card->shortname, "ForteMedia FM801-");
13251da177e4SLinus Torvalds 	strcat(card->shortname, chip->multichannel ? "AU" : "AS");
13261da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %i",
13271da177e4SLinus Torvalds 		card->shortname, chip->port, chip->irq);
13281da177e4SLinus Torvalds 
1329fb716c0bSOndrej Zary 	if (chip->tea575x_tuner & TUNER_ONLY)
1330e0a5d82aSAndy Shevchenko 		goto __fm801_tuner_only;
1331e0a5d82aSAndy Shevchenko 
13321da177e4SLinus Torvalds 	if ((err = snd_fm801_pcm(chip, 0, NULL)) < 0) {
13331da177e4SLinus Torvalds 		snd_card_free(card);
13341da177e4SLinus Torvalds 		return err;
13351da177e4SLinus Torvalds 	}
13361da177e4SLinus Torvalds 	if ((err = snd_fm801_mixer(chip)) < 0) {
13371da177e4SLinus Torvalds 		snd_card_free(card);
13381da177e4SLinus Torvalds 		return err;
13391da177e4SLinus Torvalds 	}
13401da177e4SLinus Torvalds 	if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1341302e4c2fSTakashi Iwai 				       FM801_REG(chip, MPU401_DATA),
1342dba8b469SClemens Ladisch 				       MPU401_INFO_INTEGRATED |
1343dba8b469SClemens Ladisch 				       MPU401_INFO_IRQ_HOOK,
1344dba8b469SClemens Ladisch 				       -1, &chip->rmidi)) < 0) {
13451da177e4SLinus Torvalds 		snd_card_free(card);
13461da177e4SLinus Torvalds 		return err;
13471da177e4SLinus Torvalds 	}
13481da177e4SLinus Torvalds 	if ((err = snd_opl3_create(card, FM801_REG(chip, OPL3_BANK0),
13491da177e4SLinus Torvalds 				   FM801_REG(chip, OPL3_BANK1),
13501da177e4SLinus Torvalds 				   OPL3_HW_OPL3_FM801, 1, &opl3)) < 0) {
13511da177e4SLinus Torvalds 		snd_card_free(card);
13521da177e4SLinus Torvalds 		return err;
13531da177e4SLinus Torvalds 	}
13541da177e4SLinus Torvalds 	if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
13551da177e4SLinus Torvalds 		snd_card_free(card);
13561da177e4SLinus Torvalds 		return err;
13571da177e4SLinus Torvalds 	}
13581da177e4SLinus Torvalds 
1359e0a5d82aSAndy Shevchenko       __fm801_tuner_only:
13601da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
13611da177e4SLinus Torvalds 		snd_card_free(card);
13621da177e4SLinus Torvalds 		return err;
13631da177e4SLinus Torvalds 	}
13641da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
13651da177e4SLinus Torvalds 	dev++;
13661da177e4SLinus Torvalds 	return 0;
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds 
1369e23e7a14SBill Pemberton static void snd_card_fm801_remove(struct pci_dev *pci)
13701da177e4SLinus Torvalds {
13711da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
13721da177e4SLinus Torvalds }
13731da177e4SLinus Torvalds 
1374c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
1375b1e9ed26STakashi Iwai static unsigned char saved_regs[] = {
1376b1e9ed26STakashi Iwai 	FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
1377b1e9ed26STakashi Iwai 	FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
1378b1e9ed26STakashi Iwai 	FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
1379b1e9ed26STakashi Iwai 	FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
1380b1e9ed26STakashi Iwai };
1381b1e9ed26STakashi Iwai 
138268cb2b55STakashi Iwai static int snd_fm801_suspend(struct device *dev)
1383b1e9ed26STakashi Iwai {
138468cb2b55STakashi Iwai 	struct pci_dev *pci = to_pci_dev(dev);
138568cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1386b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1387b1e9ed26STakashi Iwai 	int i;
1388b1e9ed26STakashi Iwai 
1389b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1390b1e9ed26STakashi Iwai 	snd_pcm_suspend_all(chip->pcm);
1391b1e9ed26STakashi Iwai 	snd_ac97_suspend(chip->ac97);
1392b1e9ed26STakashi Iwai 	snd_ac97_suspend(chip->ac97_sec);
1393b1e9ed26STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1394b1e9ed26STakashi Iwai 		chip->saved_regs[i] = inw(chip->port + saved_regs[i]);
1395b1e9ed26STakashi Iwai 	/* FIXME: tea575x suspend */
1396b1e9ed26STakashi Iwai 
1397b1e9ed26STakashi Iwai 	pci_disable_device(pci);
1398b1e9ed26STakashi Iwai 	pci_save_state(pci);
139968cb2b55STakashi Iwai 	pci_set_power_state(pci, PCI_D3hot);
1400b1e9ed26STakashi Iwai 	return 0;
1401b1e9ed26STakashi Iwai }
1402b1e9ed26STakashi Iwai 
140368cb2b55STakashi Iwai static int snd_fm801_resume(struct device *dev)
1404b1e9ed26STakashi Iwai {
140568cb2b55STakashi Iwai 	struct pci_dev *pci = to_pci_dev(dev);
140668cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1407b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1408b1e9ed26STakashi Iwai 	int i;
1409b1e9ed26STakashi Iwai 
1410b1e9ed26STakashi Iwai 	pci_set_power_state(pci, PCI_D0);
141130b35399STakashi Iwai 	pci_restore_state(pci);
141230b35399STakashi Iwai 	if (pci_enable_device(pci) < 0) {
141330b35399STakashi Iwai 		printk(KERN_ERR "fm801: pci_enable_device failed, "
141430b35399STakashi Iwai 		       "disabling device\n");
141530b35399STakashi Iwai 		snd_card_disconnect(card);
141630b35399STakashi Iwai 		return -EIO;
141730b35399STakashi Iwai 	}
1418b1e9ed26STakashi Iwai 	pci_set_master(pci);
1419b1e9ed26STakashi Iwai 
1420b1e9ed26STakashi Iwai 	snd_fm801_chip_init(chip, 1);
1421b1e9ed26STakashi Iwai 	snd_ac97_resume(chip->ac97);
1422b1e9ed26STakashi Iwai 	snd_ac97_resume(chip->ac97_sec);
1423b1e9ed26STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1424b1e9ed26STakashi Iwai 		outw(chip->saved_regs[i], chip->port + saved_regs[i]);
1425b1e9ed26STakashi Iwai 
1426b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1427b1e9ed26STakashi Iwai 	return 0;
1428b1e9ed26STakashi Iwai }
142968cb2b55STakashi Iwai 
143068cb2b55STakashi Iwai static SIMPLE_DEV_PM_OPS(snd_fm801_pm, snd_fm801_suspend, snd_fm801_resume);
143168cb2b55STakashi Iwai #define SND_FM801_PM_OPS	&snd_fm801_pm
143268cb2b55STakashi Iwai #else
143368cb2b55STakashi Iwai #define SND_FM801_PM_OPS	NULL
1434c7561cd8STakashi Iwai #endif /* CONFIG_PM_SLEEP */
1435b1e9ed26STakashi Iwai 
1436e9f66d9bSTakashi Iwai static struct pci_driver fm801_driver = {
14373733e424STakashi Iwai 	.name = KBUILD_MODNAME,
14381da177e4SLinus Torvalds 	.id_table = snd_fm801_ids,
14391da177e4SLinus Torvalds 	.probe = snd_card_fm801_probe,
1440e23e7a14SBill Pemberton 	.remove = snd_card_fm801_remove,
144168cb2b55STakashi Iwai 	.driver = {
144268cb2b55STakashi Iwai 		.pm = SND_FM801_PM_OPS,
144368cb2b55STakashi Iwai 	},
14441da177e4SLinus Torvalds };
14451da177e4SLinus Torvalds 
1446e9f66d9bSTakashi Iwai module_pci_driver(fm801_driver);
1447