xref: /linux/sound/pci/fm801.c (revision e97e98c63b43040732ad5d1f0b38ad4a8371c73a)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  The driver for the ForteMedia FM801 based soundcards
3c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
61da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
71da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
81da177e4SLinus Torvalds  *   (at your option) any later version.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
131da177e4SLinus Torvalds  *   GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/delay.h>
181da177e4SLinus Torvalds #include <linux/init.h>
191da177e4SLinus Torvalds #include <linux/interrupt.h>
20215dacc2SAndy Shevchenko #include <linux/io.h>
211da177e4SLinus Torvalds #include <linux/pci.h>
221da177e4SLinus Torvalds #include <linux/slab.h>
2365a77217SPaul Gortmaker #include <linux/module.h>
241da177e4SLinus Torvalds #include <sound/core.h>
251da177e4SLinus Torvalds #include <sound/pcm.h>
26666c70ffSTakashi Iwai #include <sound/tlv.h>
271da177e4SLinus Torvalds #include <sound/ac97_codec.h>
281da177e4SLinus Torvalds #include <sound/mpu401.h>
291da177e4SLinus Torvalds #include <sound/opl3.h>
301da177e4SLinus Torvalds #include <sound/initval.h>
311da177e4SLinus Torvalds 
32efce4bb9SAdrian Bunk #ifdef CONFIG_SND_FM801_TEA575X_BOOL
3359b56459SOndrej Zary #include <media/tea575x.h>
341da177e4SLinus Torvalds #endif
351da177e4SLinus Torvalds 
36c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
371da177e4SLinus Torvalds MODULE_DESCRIPTION("ForteMedia FM801");
381da177e4SLinus Torvalds MODULE_LICENSE("GPL");
391da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{ForteMedia,FM801},"
401da177e4SLinus Torvalds 		"{Genius,SoundMaker Live 5.1}}");
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
431da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
44a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
451da177e4SLinus Torvalds /*
461da177e4SLinus Torvalds  *  Enable TEA575x tuner
471da177e4SLinus Torvalds  *    1 = MediaForte 256-PCS
48d7ba858aSOndrej Zary  *    2 = MediaForte 256-PCP
491da177e4SLinus Torvalds  *    3 = MediaForte 64-PCR
50fb716c0bSOndrej Zary  *   16 = setup tuner only (this is additional bit), i.e. SF64-PCR FM card
511da177e4SLinus Torvalds  *  High 16-bits are video (radio) device number + 1
521da177e4SLinus Torvalds  */
536581f4e7STakashi Iwai static int tea575x_tuner[SNDRV_CARDS];
54d4ecc83bSHans Verkuil static int radio_nr[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
571da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
581da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
591da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
601da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
611da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
621da177e4SLinus Torvalds module_param_array(tea575x_tuner, int, NULL, 0444);
63d7ba858aSOndrej 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).");
64d4ecc83bSHans Verkuil module_param_array(radio_nr, int, NULL, 0444);
65d4ecc83bSHans Verkuil MODULE_PARM_DESC(radio_nr, "Radio device numbers");
66d4ecc83bSHans Verkuil 
67fb716c0bSOndrej Zary 
68c37279b9SBen Hutchings #define TUNER_DISABLED		(1<<3)
69fb716c0bSOndrej Zary #define TUNER_ONLY		(1<<4)
70fb716c0bSOndrej Zary #define TUNER_TYPE_MASK		(~TUNER_ONLY & 0xFFFF)
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /*
731da177e4SLinus Torvalds  *  Direct registers
741da177e4SLinus Torvalds  */
751da177e4SLinus Torvalds 
76215dacc2SAndy Shevchenko #define fm801_writew(chip,reg,value)	outw((value), chip->port + FM801_##reg)
77215dacc2SAndy Shevchenko #define fm801_readw(chip,reg)		inw(chip->port + FM801_##reg)
78215dacc2SAndy Shevchenko 
79215dacc2SAndy Shevchenko #define fm801_writel(chip,reg,value)	outl((value), chip->port + FM801_##reg)
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds #define FM801_PCM_VOL		0x00	/* PCM Output Volume */
821da177e4SLinus Torvalds #define FM801_FM_VOL		0x02	/* FM Output Volume */
831da177e4SLinus Torvalds #define FM801_I2S_VOL		0x04	/* I2S Volume */
841da177e4SLinus Torvalds #define FM801_REC_SRC		0x06	/* Record Source */
851da177e4SLinus Torvalds #define FM801_PLY_CTRL		0x08	/* Playback Control */
861da177e4SLinus Torvalds #define FM801_PLY_COUNT		0x0a	/* Playback Count */
871da177e4SLinus Torvalds #define FM801_PLY_BUF1		0x0c	/* Playback Bufer I */
881da177e4SLinus Torvalds #define FM801_PLY_BUF2		0x10	/* Playback Buffer II */
891da177e4SLinus Torvalds #define FM801_CAP_CTRL		0x14	/* Capture Control */
901da177e4SLinus Torvalds #define FM801_CAP_COUNT		0x16	/* Capture Count */
911da177e4SLinus Torvalds #define FM801_CAP_BUF1		0x18	/* Capture Buffer I */
921da177e4SLinus Torvalds #define FM801_CAP_BUF2		0x1c	/* Capture Buffer II */
931da177e4SLinus Torvalds #define FM801_CODEC_CTRL	0x22	/* Codec Control */
941da177e4SLinus Torvalds #define FM801_I2S_MODE		0x24	/* I2S Mode Control */
951da177e4SLinus Torvalds #define FM801_VOLUME		0x26	/* Volume Up/Down/Mute Status */
961da177e4SLinus Torvalds #define FM801_I2C_CTRL		0x29	/* I2C Control */
971da177e4SLinus Torvalds #define FM801_AC97_CMD		0x2a	/* AC'97 Command */
981da177e4SLinus Torvalds #define FM801_AC97_DATA		0x2c	/* AC'97 Data */
991da177e4SLinus Torvalds #define FM801_MPU401_DATA	0x30	/* MPU401 Data */
1001da177e4SLinus Torvalds #define FM801_MPU401_CMD	0x31	/* MPU401 Command */
1011da177e4SLinus Torvalds #define FM801_GPIO_CTRL		0x52	/* General Purpose I/O Control */
1021da177e4SLinus Torvalds #define FM801_GEN_CTRL		0x54	/* General Control */
1031da177e4SLinus Torvalds #define FM801_IRQ_MASK		0x56	/* Interrupt Mask */
1041da177e4SLinus Torvalds #define FM801_IRQ_STATUS	0x5a	/* Interrupt Status */
1051da177e4SLinus Torvalds #define FM801_OPL3_BANK0	0x68	/* OPL3 Status Read / Bank 0 Write */
1061da177e4SLinus Torvalds #define FM801_OPL3_DATA0	0x69	/* OPL3 Data 0 Write */
1071da177e4SLinus Torvalds #define FM801_OPL3_BANK1	0x6a	/* OPL3 Bank 1 Write */
1081da177e4SLinus Torvalds #define FM801_OPL3_DATA1	0x6b	/* OPL3 Bank 1 Write */
1091da177e4SLinus Torvalds #define FM801_POWERDOWN		0x70	/* Blocks Power Down Control */
1101da177e4SLinus Torvalds 
111b1e9ed26STakashi Iwai /* codec access */
112b1e9ed26STakashi Iwai #define FM801_AC97_READ		(1<<7)	/* read=1, write=0 */
113b1e9ed26STakashi Iwai #define FM801_AC97_VALID	(1<<8)	/* port valid=1 */
114b1e9ed26STakashi Iwai #define FM801_AC97_BUSY		(1<<9)	/* busy=1 */
115b1e9ed26STakashi Iwai #define FM801_AC97_ADDR_SHIFT	10	/* codec id (2bit) */
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* playback and record control register bits */
1181da177e4SLinus Torvalds #define FM801_BUF1_LAST		(1<<1)
1191da177e4SLinus Torvalds #define FM801_BUF2_LAST		(1<<2)
1201da177e4SLinus Torvalds #define FM801_START		(1<<5)
1211da177e4SLinus Torvalds #define FM801_PAUSE		(1<<6)
1221da177e4SLinus Torvalds #define FM801_IMMED_STOP	(1<<7)
1231da177e4SLinus Torvalds #define FM801_RATE_SHIFT	8
1241da177e4SLinus Torvalds #define FM801_RATE_MASK		(15 << FM801_RATE_SHIFT)
1251da177e4SLinus Torvalds #define FM801_CHANNELS_4	(1<<12)	/* playback only */
1261da177e4SLinus Torvalds #define FM801_CHANNELS_6	(2<<12)	/* playback only */
1271da177e4SLinus Torvalds #define FM801_CHANNELS_6MS	(3<<12)	/* playback only */
1281da177e4SLinus Torvalds #define FM801_CHANNELS_MASK	(3<<12)
1291da177e4SLinus Torvalds #define FM801_16BIT		(1<<14)
1301da177e4SLinus Torvalds #define FM801_STEREO		(1<<15)
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /* IRQ status bits */
1331da177e4SLinus Torvalds #define FM801_IRQ_PLAYBACK	(1<<8)
1341da177e4SLinus Torvalds #define FM801_IRQ_CAPTURE	(1<<9)
1351da177e4SLinus Torvalds #define FM801_IRQ_VOLUME	(1<<14)
1361da177e4SLinus Torvalds #define FM801_IRQ_MPU		(1<<15)
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /* GPIO control register */
1391da177e4SLinus Torvalds #define FM801_GPIO_GP0		(1<<0)	/* read/write */
1401da177e4SLinus Torvalds #define FM801_GPIO_GP1		(1<<1)
1411da177e4SLinus Torvalds #define FM801_GPIO_GP2		(1<<2)
1421da177e4SLinus Torvalds #define FM801_GPIO_GP3		(1<<3)
1431da177e4SLinus Torvalds #define FM801_GPIO_GP(x)	(1<<(0+(x)))
1441da177e4SLinus Torvalds #define FM801_GPIO_GD0		(1<<8)	/* directions: 1 = input, 0 = output*/
1451da177e4SLinus Torvalds #define FM801_GPIO_GD1		(1<<9)
1461da177e4SLinus Torvalds #define FM801_GPIO_GD2		(1<<10)
1471da177e4SLinus Torvalds #define FM801_GPIO_GD3		(1<<11)
1481da177e4SLinus Torvalds #define FM801_GPIO_GD(x)	(1<<(8+(x)))
1491da177e4SLinus Torvalds #define FM801_GPIO_GS0		(1<<12)	/* function select: */
1501da177e4SLinus Torvalds #define FM801_GPIO_GS1		(1<<13)	/*    1 = GPIO */
1511da177e4SLinus Torvalds #define FM801_GPIO_GS2		(1<<14)	/*    0 = other (S/PDIF, VOL) */
1521da177e4SLinus Torvalds #define FM801_GPIO_GS3		(1<<15)
1531da177e4SLinus Torvalds #define FM801_GPIO_GS(x)	(1<<(12+(x)))
1541da177e4SLinus Torvalds 
155052c233eSAndy Shevchenko /**
156052c233eSAndy Shevchenko  * struct fm801 - describes FM801 chip
157052c233eSAndy Shevchenko  * @port:		I/O port number
158052c233eSAndy Shevchenko  * @multichannel:	multichannel support
159052c233eSAndy Shevchenko  * @secondary:		secondary codec
160052c233eSAndy Shevchenko  * @secondary_addr:	address of the secondary codec
161052c233eSAndy Shevchenko  * @tea575x_tuner:	tuner access method & flags
162052c233eSAndy Shevchenko  * @ply_ctrl:		playback control
163052c233eSAndy Shevchenko  * @cap_ctrl:		capture control
1641da177e4SLinus Torvalds  */
165a5f22156STakashi Iwai struct fm801 {
1661da177e4SLinus Torvalds 	int irq;
1671da177e4SLinus Torvalds 
168052c233eSAndy Shevchenko 	unsigned long port;
169052c233eSAndy Shevchenko 	unsigned int multichannel: 1,
170052c233eSAndy Shevchenko 		     secondary: 1;
171052c233eSAndy Shevchenko 	unsigned char secondary_addr;
172052c233eSAndy Shevchenko 	unsigned int tea575x_tuner;
1731da177e4SLinus Torvalds 
174052c233eSAndy Shevchenko 	unsigned short ply_ctrl;
175052c233eSAndy Shevchenko 	unsigned short cap_ctrl;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	unsigned long ply_buffer;
1781da177e4SLinus Torvalds 	unsigned int ply_buf;
1791da177e4SLinus Torvalds 	unsigned int ply_count;
1801da177e4SLinus Torvalds 	unsigned int ply_size;
1811da177e4SLinus Torvalds 	unsigned int ply_pos;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	unsigned long cap_buffer;
1841da177e4SLinus Torvalds 	unsigned int cap_buf;
1851da177e4SLinus Torvalds 	unsigned int cap_count;
1861da177e4SLinus Torvalds 	unsigned int cap_size;
1871da177e4SLinus Torvalds 	unsigned int cap_pos;
1881da177e4SLinus Torvalds 
189a5f22156STakashi Iwai 	struct snd_ac97_bus *ac97_bus;
190a5f22156STakashi Iwai 	struct snd_ac97 *ac97;
191a5f22156STakashi Iwai 	struct snd_ac97 *ac97_sec;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	struct pci_dev *pci;
194a5f22156STakashi Iwai 	struct snd_card *card;
195a5f22156STakashi Iwai 	struct snd_pcm *pcm;
196a5f22156STakashi Iwai 	struct snd_rawmidi *rmidi;
197a5f22156STakashi Iwai 	struct snd_pcm_substream *playback_substream;
198a5f22156STakashi Iwai 	struct snd_pcm_substream *capture_substream;
1991da177e4SLinus Torvalds 	unsigned int p_dma_size;
2001da177e4SLinus Torvalds 	unsigned int c_dma_size;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	spinlock_t reg_lock;
203a5f22156STakashi Iwai 	struct snd_info_entry *proc_entry;
2041da177e4SLinus Torvalds 
205fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
206d4ecc83bSHans Verkuil 	struct v4l2_device v4l2_dev;
207a5f22156STakashi Iwai 	struct snd_tea575x tea;
2081da177e4SLinus Torvalds #endif
209b1e9ed26STakashi Iwai 
210c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
211b1e9ed26STakashi Iwai 	u16 saved_regs[0x20];
212b1e9ed26STakashi Iwai #endif
2131da177e4SLinus Torvalds };
2141da177e4SLinus Torvalds 
2159baa3c34SBenoit Taine static const struct pci_device_id snd_fm801_ids[] = {
2161da177e4SLinus Torvalds 	{ 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* FM801 */
21726be8659SSergey Vlasov 	{ 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* Gallant Odyssey Sound 4 */
2181da177e4SLinus Torvalds 	{ 0, }
2191da177e4SLinus Torvalds };
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds /*
2241da177e4SLinus Torvalds  *  common I/O routines
2251da177e4SLinus Torvalds  */
2261da177e4SLinus Torvalds 
22702fd1a76SAndy Shevchenko static bool fm801_ac97_is_ready(struct fm801 *chip, unsigned int iterations)
22802fd1a76SAndy Shevchenko {
22902fd1a76SAndy Shevchenko 	unsigned int idx;
23002fd1a76SAndy Shevchenko 
23102fd1a76SAndy Shevchenko 	for (idx = 0; idx < iterations; idx++) {
23202fd1a76SAndy Shevchenko 		if (!(fm801_readw(chip, AC97_CMD) & FM801_AC97_BUSY))
23302fd1a76SAndy Shevchenko 			return true;
23402fd1a76SAndy Shevchenko 		udelay(10);
23502fd1a76SAndy Shevchenko 	}
23602fd1a76SAndy Shevchenko 	return false;
23702fd1a76SAndy Shevchenko }
23802fd1a76SAndy Shevchenko 
23902fd1a76SAndy Shevchenko static bool fm801_ac97_is_valid(struct fm801 *chip, unsigned int iterations)
24002fd1a76SAndy Shevchenko {
24102fd1a76SAndy Shevchenko 	unsigned int idx;
24202fd1a76SAndy Shevchenko 
24302fd1a76SAndy Shevchenko 	for (idx = 0; idx < iterations; idx++) {
24402fd1a76SAndy Shevchenko 		if (fm801_readw(chip, AC97_CMD) & FM801_AC97_VALID)
24502fd1a76SAndy Shevchenko 			return true;
24602fd1a76SAndy Shevchenko 		udelay(10);
24702fd1a76SAndy Shevchenko 	}
24802fd1a76SAndy Shevchenko 	return false;
24902fd1a76SAndy Shevchenko }
25002fd1a76SAndy Shevchenko 
251a5f22156STakashi Iwai static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
2521da177e4SLinus Torvalds 				 unsigned short mask, unsigned short value)
2531da177e4SLinus Torvalds {
2541da177e4SLinus Torvalds 	int change;
2551da177e4SLinus Torvalds 	unsigned long flags;
2561da177e4SLinus Torvalds 	unsigned short old, new;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
2591da177e4SLinus Torvalds 	old = inw(chip->port + reg);
2601da177e4SLinus Torvalds 	new = (old & ~mask) | value;
2611da177e4SLinus Torvalds 	change = old != new;
2621da177e4SLinus Torvalds 	if (change)
2631da177e4SLinus Torvalds 		outw(new, chip->port + reg);
2641da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
2651da177e4SLinus Torvalds 	return change;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
268a5f22156STakashi Iwai static void snd_fm801_codec_write(struct snd_ac97 *ac97,
2691da177e4SLinus Torvalds 				  unsigned short reg,
2701da177e4SLinus Torvalds 				  unsigned short val)
2711da177e4SLinus Torvalds {
272a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	/*
2751da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
2761da177e4SLinus Torvalds 	 */
27702fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 100)) {
2789c7f9abfSTakashi Iwai 		dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
2791da177e4SLinus Torvalds 		return;
28002fd1a76SAndy Shevchenko 	}
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	/* write data and address */
283215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_DATA, val);
284215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_CMD, reg | (ac97->addr << FM801_AC97_ADDR_SHIFT));
2851da177e4SLinus Torvalds 	/*
2861da177e4SLinus Torvalds 	 *  Wait until the write command is not completed..
2871da177e4SLinus Torvalds 	 */
28802fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 1000))
28902fd1a76SAndy Shevchenko 		dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
29002fd1a76SAndy Shevchenko 		ac97->num);
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
293a5f22156STakashi Iwai static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
2941da177e4SLinus Torvalds {
295a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	/*
2981da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
2991da177e4SLinus Torvalds 	 */
30002fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 100)) {
3019c7f9abfSTakashi Iwai 		dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
3021da177e4SLinus Torvalds 		return 0;
30302fd1a76SAndy Shevchenko 	}
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	/* read command */
306215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_CMD,
307215dacc2SAndy Shevchenko 		     reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
30802fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 100)) {
30902fd1a76SAndy Shevchenko 		dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
31002fd1a76SAndy Shevchenko 			ac97->num);
3111da177e4SLinus Torvalds 		return 0;
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 
31402fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_valid(chip, 1000)) {
31502fd1a76SAndy Shevchenko 		dev_err(chip->card->dev,
31602fd1a76SAndy Shevchenko 			"AC'97 interface #%d is not valid (2)\n", ac97->num);
31702fd1a76SAndy Shevchenko 		return 0;
31802fd1a76SAndy Shevchenko 	}
31902fd1a76SAndy Shevchenko 
320215dacc2SAndy Shevchenko 	return fm801_readw(chip, AC97_DATA);
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds static unsigned int rates[] = {
3241da177e4SLinus Torvalds   5500,  8000,  9600, 11025,
3251da177e4SLinus Torvalds   16000, 19200, 22050, 32000,
3261da177e4SLinus Torvalds   38400, 44100, 48000
3271da177e4SLinus Torvalds };
3281da177e4SLinus Torvalds 
329a5f22156STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
3301da177e4SLinus Torvalds 	.count = ARRAY_SIZE(rates),
3311da177e4SLinus Torvalds 	.list = rates,
3321da177e4SLinus Torvalds 	.mask = 0,
3331da177e4SLinus Torvalds };
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds static unsigned int channels[] = {
3361da177e4SLinus Torvalds   2, 4, 6
3371da177e4SLinus Torvalds };
3381da177e4SLinus Torvalds 
339a5f22156STakashi Iwai static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
3405e4968e2STobias Klauser 	.count = ARRAY_SIZE(channels),
3411da177e4SLinus Torvalds 	.list = channels,
3421da177e4SLinus Torvalds 	.mask = 0,
3431da177e4SLinus Torvalds };
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds /*
3461da177e4SLinus Torvalds  *  Sample rate routines
3471da177e4SLinus Torvalds  */
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds static unsigned short snd_fm801_rate_bits(unsigned int rate)
3501da177e4SLinus Torvalds {
3511da177e4SLinus Torvalds 	unsigned int idx;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
3541da177e4SLinus Torvalds 		if (rates[idx] == rate)
3551da177e4SLinus Torvalds 			return idx;
3561da177e4SLinus Torvalds 	snd_BUG();
3571da177e4SLinus Torvalds 	return ARRAY_SIZE(rates) - 1;
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds /*
3611da177e4SLinus Torvalds  *  PCM part
3621da177e4SLinus Torvalds  */
3631da177e4SLinus Torvalds 
364a5f22156STakashi Iwai static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
3651da177e4SLinus Torvalds 				      int cmd)
3661da177e4SLinus Torvalds {
367a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3701da177e4SLinus Torvalds 	switch (cmd) {
3711da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
3721da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_BUF1_LAST |
3731da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
3741da177e4SLinus Torvalds 				     FM801_PAUSE);
3751da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_START |
3761da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
3771da177e4SLinus Torvalds 		break;
3781da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
3791da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
3801da177e4SLinus Torvalds 		break;
3811da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
382b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
3831da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_PAUSE;
3841da177e4SLinus Torvalds 		break;
3851da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
386b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
3871da177e4SLinus Torvalds 		chip->ply_ctrl &= ~FM801_PAUSE;
3881da177e4SLinus Torvalds 		break;
3891da177e4SLinus Torvalds 	default:
3901da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
3911da177e4SLinus Torvalds 		snd_BUG();
3921da177e4SLinus Torvalds 		return -EINVAL;
3931da177e4SLinus Torvalds 	}
394215dacc2SAndy Shevchenko 	fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
3951da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3961da177e4SLinus Torvalds 	return 0;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
399a5f22156STakashi Iwai static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
4001da177e4SLinus Torvalds 				     int cmd)
4011da177e4SLinus Torvalds {
402a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
4051da177e4SLinus Torvalds 	switch (cmd) {
4061da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4071da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_BUF1_LAST |
4081da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
4091da177e4SLinus Torvalds 				     FM801_PAUSE);
4101da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_START |
4111da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
4121da177e4SLinus Torvalds 		break;
4131da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4141da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
4151da177e4SLinus Torvalds 		break;
4161da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
417b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
4181da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_PAUSE;
4191da177e4SLinus Torvalds 		break;
4201da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
421b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
4221da177e4SLinus Torvalds 		chip->cap_ctrl &= ~FM801_PAUSE;
4231da177e4SLinus Torvalds 		break;
4241da177e4SLinus Torvalds 	default:
4251da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
4261da177e4SLinus Torvalds 		snd_BUG();
4271da177e4SLinus Torvalds 		return -EINVAL;
4281da177e4SLinus Torvalds 	}
429215dacc2SAndy Shevchenko 	fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
4301da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4311da177e4SLinus Torvalds 	return 0;
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
434a5f22156STakashi Iwai static int snd_fm801_hw_params(struct snd_pcm_substream *substream,
435a5f22156STakashi Iwai 			       struct snd_pcm_hw_params *hw_params)
4361da177e4SLinus Torvalds {
4371da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
440a5f22156STakashi Iwai static int snd_fm801_hw_free(struct snd_pcm_substream *substream)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
445a5f22156STakashi Iwai static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
4461da177e4SLinus Torvalds {
447a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
448a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
4511da177e4SLinus Torvalds 	chip->ply_count = snd_pcm_lib_period_bytes(substream);
4521da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
4531da177e4SLinus Torvalds 	chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
4541da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK |
4551da177e4SLinus Torvalds 			     FM801_CHANNELS_MASK);
4561da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
4571da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_16BIT;
4581da177e4SLinus Torvalds 	if (runtime->channels > 1) {
4591da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_STEREO;
4601da177e4SLinus Torvalds 		if (runtime->channels == 4)
4611da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_4;
4621da177e4SLinus Torvalds 		else if (runtime->channels == 6)
4631da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_6;
4641da177e4SLinus Torvalds 	}
4651da177e4SLinus Torvalds 	chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
4661da177e4SLinus Torvalds 	chip->ply_buf = 0;
467215dacc2SAndy Shevchenko 	fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
468215dacc2SAndy Shevchenko 	fm801_writew(chip, PLY_COUNT, chip->ply_count - 1);
4691da177e4SLinus Torvalds 	chip->ply_buffer = runtime->dma_addr;
4701da177e4SLinus Torvalds 	chip->ply_pos = 0;
471215dacc2SAndy Shevchenko 	fm801_writel(chip, PLY_BUF1, chip->ply_buffer);
472215dacc2SAndy Shevchenko 	fm801_writel(chip, PLY_BUF2,
473215dacc2SAndy Shevchenko 		     chip->ply_buffer + (chip->ply_count % chip->ply_size));
4741da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
4751da177e4SLinus Torvalds 	return 0;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
478a5f22156STakashi Iwai static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
4791da177e4SLinus Torvalds {
480a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
481a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
4841da177e4SLinus Torvalds 	chip->cap_count = snd_pcm_lib_period_bytes(substream);
4851da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
4861da177e4SLinus Torvalds 	chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
4871da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK);
4881da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
4891da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_16BIT;
4901da177e4SLinus Torvalds 	if (runtime->channels > 1)
4911da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_STEREO;
4921da177e4SLinus Torvalds 	chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
4931da177e4SLinus Torvalds 	chip->cap_buf = 0;
494215dacc2SAndy Shevchenko 	fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
495215dacc2SAndy Shevchenko 	fm801_writew(chip, CAP_COUNT, chip->cap_count - 1);
4961da177e4SLinus Torvalds 	chip->cap_buffer = runtime->dma_addr;
4971da177e4SLinus Torvalds 	chip->cap_pos = 0;
498215dacc2SAndy Shevchenko 	fm801_writel(chip, CAP_BUF1, chip->cap_buffer);
499215dacc2SAndy Shevchenko 	fm801_writel(chip, CAP_BUF2,
500215dacc2SAndy Shevchenko 		     chip->cap_buffer + (chip->cap_count % chip->cap_size));
5011da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
5021da177e4SLinus Torvalds 	return 0;
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
505a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
5061da177e4SLinus Torvalds {
507a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
5081da177e4SLinus Torvalds 	size_t ptr;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	if (!(chip->ply_ctrl & FM801_START))
5111da177e4SLinus Torvalds 		return 0;
5121da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
513215dacc2SAndy Shevchenko 	ptr = chip->ply_pos + (chip->ply_count - 1) - fm801_readw(chip, PLY_COUNT);
514215dacc2SAndy Shevchenko 	if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_PLAYBACK) {
5151da177e4SLinus Torvalds 		ptr += chip->ply_count;
5161da177e4SLinus Torvalds 		ptr %= chip->ply_size;
5171da177e4SLinus Torvalds 	}
5181da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5191da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
522a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
5231da177e4SLinus Torvalds {
524a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
5251da177e4SLinus Torvalds 	size_t ptr;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	if (!(chip->cap_ctrl & FM801_START))
5281da177e4SLinus Torvalds 		return 0;
5291da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
530215dacc2SAndy Shevchenko 	ptr = chip->cap_pos + (chip->cap_count - 1) - fm801_readw(chip, CAP_COUNT);
531215dacc2SAndy Shevchenko 	if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_CAPTURE) {
5321da177e4SLinus Torvalds 		ptr += chip->cap_count;
5331da177e4SLinus Torvalds 		ptr %= chip->cap_size;
5341da177e4SLinus Torvalds 	}
5351da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5361da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
5397d12e780SDavid Howells static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
5401da177e4SLinus Torvalds {
541a5f22156STakashi Iwai 	struct fm801 *chip = dev_id;
5421da177e4SLinus Torvalds 	unsigned short status;
5431da177e4SLinus Torvalds 	unsigned int tmp;
5441da177e4SLinus Torvalds 
545215dacc2SAndy Shevchenko 	status = fm801_readw(chip, IRQ_STATUS);
5461da177e4SLinus Torvalds 	status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
5471da177e4SLinus Torvalds 	if (! status)
5481da177e4SLinus Torvalds 		return IRQ_NONE;
5491da177e4SLinus Torvalds 	/* ack first */
550215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_STATUS, status);
5511da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
5521da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5531da177e4SLinus Torvalds 		chip->ply_buf++;
5541da177e4SLinus Torvalds 		chip->ply_pos += chip->ply_count;
5551da177e4SLinus Torvalds 		chip->ply_pos %= chip->ply_size;
5561da177e4SLinus Torvalds 		tmp = chip->ply_pos + chip->ply_count;
5571da177e4SLinus Torvalds 		tmp %= chip->ply_size;
558215dacc2SAndy Shevchenko 		if (chip->ply_buf & 1)
559215dacc2SAndy Shevchenko 			fm801_writel(chip, PLY_BUF1, chip->ply_buffer + tmp);
560215dacc2SAndy Shevchenko 		else
561215dacc2SAndy Shevchenko 			fm801_writel(chip, PLY_BUF2, chip->ply_buffer + tmp);
5621da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5631da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->playback_substream);
5641da177e4SLinus Torvalds 	}
5651da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
5661da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5671da177e4SLinus Torvalds 		chip->cap_buf++;
5681da177e4SLinus Torvalds 		chip->cap_pos += chip->cap_count;
5691da177e4SLinus Torvalds 		chip->cap_pos %= chip->cap_size;
5701da177e4SLinus Torvalds 		tmp = chip->cap_pos + chip->cap_count;
5711da177e4SLinus Torvalds 		tmp %= chip->cap_size;
572215dacc2SAndy Shevchenko 		if (chip->cap_buf & 1)
573215dacc2SAndy Shevchenko 			fm801_writel(chip, CAP_BUF1, chip->cap_buffer + tmp);
574215dacc2SAndy Shevchenko 		else
575215dacc2SAndy Shevchenko 			fm801_writel(chip, CAP_BUF2, chip->cap_buffer + tmp);
5761da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5771da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->capture_substream);
5781da177e4SLinus Torvalds 	}
5791da177e4SLinus Torvalds 	if (chip->rmidi && (status & FM801_IRQ_MPU))
5807d12e780SDavid Howells 		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
5811da177e4SLinus Torvalds 	if (status & FM801_IRQ_VOLUME)
5821da177e4SLinus Torvalds 		;/* TODO */
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	return IRQ_HANDLED;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
587a5f22156STakashi Iwai static struct snd_pcm_hardware snd_fm801_playback =
5881da177e4SLinus Torvalds {
5891da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
5901da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
591b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
5921da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
5931da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
5941da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
5951da177e4SLinus Torvalds 	.rate_min =		5500,
5961da177e4SLinus Torvalds 	.rate_max =		48000,
5971da177e4SLinus Torvalds 	.channels_min =		1,
5981da177e4SLinus Torvalds 	.channels_max =		2,
5991da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
6001da177e4SLinus Torvalds 	.period_bytes_min =	64,
6011da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
6021da177e4SLinus Torvalds 	.periods_min =		1,
6031da177e4SLinus Torvalds 	.periods_max =		1024,
6041da177e4SLinus Torvalds 	.fifo_size =		0,
6051da177e4SLinus Torvalds };
6061da177e4SLinus Torvalds 
607a5f22156STakashi Iwai static struct snd_pcm_hardware snd_fm801_capture =
6081da177e4SLinus Torvalds {
6091da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
6101da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
611b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
6121da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
6131da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
6141da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
6151da177e4SLinus Torvalds 	.rate_min =		5500,
6161da177e4SLinus Torvalds 	.rate_max =		48000,
6171da177e4SLinus Torvalds 	.channels_min =		1,
6181da177e4SLinus Torvalds 	.channels_max =		2,
6191da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
6201da177e4SLinus Torvalds 	.period_bytes_min =	64,
6211da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
6221da177e4SLinus Torvalds 	.periods_min =		1,
6231da177e4SLinus Torvalds 	.periods_max =		1024,
6241da177e4SLinus Torvalds 	.fifo_size =		0,
6251da177e4SLinus Torvalds };
6261da177e4SLinus Torvalds 
627a5f22156STakashi Iwai static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
6281da177e4SLinus Torvalds {
629a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
630a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6311da177e4SLinus Torvalds 	int err;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	chip->playback_substream = substream;
6341da177e4SLinus Torvalds 	runtime->hw = snd_fm801_playback;
635a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
636a5f22156STakashi Iwai 				   &hw_constraints_rates);
6371da177e4SLinus Torvalds 	if (chip->multichannel) {
6381da177e4SLinus Torvalds 		runtime->hw.channels_max = 6;
639a5f22156STakashi Iwai 		snd_pcm_hw_constraint_list(runtime, 0,
640a5f22156STakashi Iwai 					   SNDRV_PCM_HW_PARAM_CHANNELS,
641a5f22156STakashi Iwai 					   &hw_constraints_channels);
6421da177e4SLinus Torvalds 	}
6431da177e4SLinus Torvalds 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
6441da177e4SLinus Torvalds 		return err;
6451da177e4SLinus Torvalds 	return 0;
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds 
648a5f22156STakashi Iwai static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
6491da177e4SLinus Torvalds {
650a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
651a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6521da177e4SLinus Torvalds 	int err;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	chip->capture_substream = substream;
6551da177e4SLinus Torvalds 	runtime->hw = snd_fm801_capture;
656a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
657a5f22156STakashi Iwai 				   &hw_constraints_rates);
6581da177e4SLinus Torvalds 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
6591da177e4SLinus Torvalds 		return err;
6601da177e4SLinus Torvalds 	return 0;
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds 
663a5f22156STakashi Iwai static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
6641da177e4SLinus Torvalds {
665a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	chip->playback_substream = NULL;
6681da177e4SLinus Torvalds 	return 0;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
671a5f22156STakashi Iwai static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
6721da177e4SLinus Torvalds {
673a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	chip->capture_substream = NULL;
6761da177e4SLinus Torvalds 	return 0;
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
679a5f22156STakashi Iwai static struct snd_pcm_ops snd_fm801_playback_ops = {
6801da177e4SLinus Torvalds 	.open =		snd_fm801_playback_open,
6811da177e4SLinus Torvalds 	.close =	snd_fm801_playback_close,
6821da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
6831da177e4SLinus Torvalds 	.hw_params =	snd_fm801_hw_params,
6841da177e4SLinus Torvalds 	.hw_free =	snd_fm801_hw_free,
6851da177e4SLinus Torvalds 	.prepare =	snd_fm801_playback_prepare,
6861da177e4SLinus Torvalds 	.trigger =	snd_fm801_playback_trigger,
6871da177e4SLinus Torvalds 	.pointer =	snd_fm801_playback_pointer,
6881da177e4SLinus Torvalds };
6891da177e4SLinus Torvalds 
690a5f22156STakashi Iwai static struct snd_pcm_ops snd_fm801_capture_ops = {
6911da177e4SLinus Torvalds 	.open =		snd_fm801_capture_open,
6921da177e4SLinus Torvalds 	.close =	snd_fm801_capture_close,
6931da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
6941da177e4SLinus Torvalds 	.hw_params =	snd_fm801_hw_params,
6951da177e4SLinus Torvalds 	.hw_free =	snd_fm801_hw_free,
6961da177e4SLinus Torvalds 	.prepare =	snd_fm801_capture_prepare,
6971da177e4SLinus Torvalds 	.trigger =	snd_fm801_capture_trigger,
6981da177e4SLinus Torvalds 	.pointer =	snd_fm801_capture_pointer,
6991da177e4SLinus Torvalds };
7001da177e4SLinus Torvalds 
701483337f9SLars-Peter Clausen static int snd_fm801_pcm(struct fm801 *chip, int device)
7021da177e4SLinus Torvalds {
703a5f22156STakashi Iwai 	struct snd_pcm *pcm;
7041da177e4SLinus Torvalds 	int err;
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm)) < 0)
7071da177e4SLinus Torvalds 		return err;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
7101da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	pcm->private_data = chip;
7131da177e4SLinus Torvalds 	pcm->info_flags = 0;
7141da177e4SLinus Torvalds 	strcpy(pcm->name, "FM801");
7151da177e4SLinus Torvalds 	chip->pcm = pcm;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
7181da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci),
7191da177e4SLinus Torvalds 					      chip->multichannel ? 128*1024 : 64*1024, 128*1024);
7201da177e4SLinus Torvalds 
721483337f9SLars-Peter Clausen 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
722e36e3b86STakashi Iwai 				     snd_pcm_alt_chmaps,
723e36e3b86STakashi Iwai 				     chip->multichannel ? 6 : 2, 0,
724e36e3b86STakashi Iwai 				     NULL);
7251da177e4SLinus Torvalds }
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds /*
7281da177e4SLinus Torvalds  *  TEA5757 radio
7291da177e4SLinus Torvalds  */
7301da177e4SLinus Torvalds 
731fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
7321da177e4SLinus Torvalds 
733938a1566SOndrej Zary /* GPIO to TEA575x maps */
734938a1566SOndrej Zary struct snd_fm801_tea575x_gpio {
735938a1566SOndrej Zary 	u8 data, clk, wren, most;
736d7ba858aSOndrej Zary 	char *name;
737938a1566SOndrej Zary };
7381da177e4SLinus Torvalds 
739938a1566SOndrej Zary static struct snd_fm801_tea575x_gpio snd_fm801_tea575x_gpios[] = {
740d7ba858aSOndrej Zary 	{ .data = 1, .clk = 3, .wren = 2, .most = 0, .name = "SF256-PCS" },
741d7ba858aSOndrej Zary 	{ .data = 1, .clk = 0, .wren = 2, .most = 3, .name = "SF256-PCP" },
742d7ba858aSOndrej Zary 	{ .data = 2, .clk = 0, .wren = 1, .most = 3, .name = "SF64-PCR" },
743938a1566SOndrej Zary };
744938a1566SOndrej Zary 
7458e699d2cSTakashi Iwai #define get_tea575x_gpio(chip) \
7468e699d2cSTakashi Iwai 	(&snd_fm801_tea575x_gpios[((chip)->tea575x_tuner & TUNER_TYPE_MASK) - 1])
7478e699d2cSTakashi Iwai 
748938a1566SOndrej Zary static void snd_fm801_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
7491da177e4SLinus Torvalds {
750a5f22156STakashi Iwai 	struct fm801 *chip = tea->private_data;
751215dacc2SAndy Shevchenko 	unsigned short reg = fm801_readw(chip, GPIO_CTRL);
7528e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
7531da177e4SLinus Torvalds 
754938a1566SOndrej Zary 	reg &= ~(FM801_GPIO_GP(gpio.data) |
755938a1566SOndrej Zary 		 FM801_GPIO_GP(gpio.clk) |
756938a1566SOndrej Zary 		 FM801_GPIO_GP(gpio.wren));
757938a1566SOndrej Zary 
758938a1566SOndrej Zary 	reg |= (pins & TEA575X_DATA) ? FM801_GPIO_GP(gpio.data) : 0;
759938a1566SOndrej Zary 	reg |= (pins & TEA575X_CLK)  ? FM801_GPIO_GP(gpio.clk) : 0;
760938a1566SOndrej Zary 	/* WRITE_ENABLE is inverted */
761938a1566SOndrej Zary 	reg |= (pins & TEA575X_WREN) ? 0 : FM801_GPIO_GP(gpio.wren);
762938a1566SOndrej Zary 
763215dacc2SAndy Shevchenko 	fm801_writew(chip, GPIO_CTRL, reg);
764938a1566SOndrej Zary }
765938a1566SOndrej Zary 
766938a1566SOndrej Zary static u8 snd_fm801_tea575x_get_pins(struct snd_tea575x *tea)
767938a1566SOndrej Zary {
768938a1566SOndrej Zary 	struct fm801 *chip = tea->private_data;
769215dacc2SAndy Shevchenko 	unsigned short reg = fm801_readw(chip, GPIO_CTRL);
7708e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
771effded75SDan Carpenter 	u8 ret;
772938a1566SOndrej Zary 
773effded75SDan Carpenter 	ret = 0;
774effded75SDan Carpenter 	if (reg & FM801_GPIO_GP(gpio.data))
775effded75SDan Carpenter 		ret |= TEA575X_DATA;
776effded75SDan Carpenter 	if (reg & FM801_GPIO_GP(gpio.most))
777effded75SDan Carpenter 		ret |= TEA575X_MOST;
778effded75SDan Carpenter 	return ret;
779938a1566SOndrej Zary }
780938a1566SOndrej Zary 
781938a1566SOndrej Zary static void snd_fm801_tea575x_set_direction(struct snd_tea575x *tea, bool output)
782938a1566SOndrej Zary {
783938a1566SOndrej Zary 	struct fm801 *chip = tea->private_data;
784215dacc2SAndy Shevchenko 	unsigned short reg = fm801_readw(chip, GPIO_CTRL);
7858e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
786938a1566SOndrej Zary 
7871da177e4SLinus Torvalds 	/* use GPIO lines and set write enable bit */
788938a1566SOndrej Zary 	reg |= FM801_GPIO_GS(gpio.data) |
789938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.wren) |
790938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.clk) |
791938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.most);
792938a1566SOndrej Zary 	if (output) {
7931da177e4SLinus Torvalds 		/* all of lines are in the write direction */
7941da177e4SLinus Torvalds 		/* clear data and clock lines */
795938a1566SOndrej Zary 		reg &= ~(FM801_GPIO_GD(gpio.data) |
796938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.wren) |
797938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.clk) |
798938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.data) |
799938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.clk) |
800938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.wren));
801938a1566SOndrej Zary 	} else {
8021da177e4SLinus Torvalds 		/* use GPIO lines, set data direction to input */
803938a1566SOndrej Zary 		reg |= FM801_GPIO_GD(gpio.data) |
804938a1566SOndrej Zary 		       FM801_GPIO_GD(gpio.most) |
805938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.data) |
806938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.most) |
807938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.wren);
8081da177e4SLinus Torvalds 		/* all of lines are in the write direction, except data */
8091da177e4SLinus Torvalds 		/* clear data, write enable and clock lines */
810938a1566SOndrej Zary 		reg &= ~(FM801_GPIO_GD(gpio.wren) |
811938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.clk) |
812938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.clk));
8131da177e4SLinus Torvalds 	}
8141da177e4SLinus Torvalds 
815215dacc2SAndy Shevchenko 	fm801_writew(chip, GPIO_CTRL, reg);
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
818938a1566SOndrej Zary static struct snd_tea575x_ops snd_fm801_tea_ops = {
819938a1566SOndrej Zary 	.set_pins = snd_fm801_tea575x_set_pins,
820938a1566SOndrej Zary 	.get_pins = snd_fm801_tea575x_get_pins,
821938a1566SOndrej Zary 	.set_direction = snd_fm801_tea575x_set_direction,
8221da177e4SLinus Torvalds };
8231da177e4SLinus Torvalds #endif
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds /*
8261da177e4SLinus Torvalds  *  Mixer routines
8271da177e4SLinus Torvalds  */
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds #define FM801_SINGLE(xname, reg, shift, mask, invert) \
8301da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
8311da177e4SLinus Torvalds   .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
8321da177e4SLinus Torvalds   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
8331da177e4SLinus Torvalds 
834a5f22156STakashi Iwai static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
835a5f22156STakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
8361da177e4SLinus Torvalds {
8371da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
8401da177e4SLinus Torvalds 	uinfo->count = 1;
8411da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
8421da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
8431da177e4SLinus Torvalds 	return 0;
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
846a5f22156STakashi Iwai static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
847a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
8481da177e4SLinus Torvalds {
849a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
8501da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
8511da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
8521da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8531da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift) & mask;
8561da177e4SLinus Torvalds 	if (invert)
8571da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
8581da177e4SLinus Torvalds 	return 0;
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
861a5f22156STakashi Iwai static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
862a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
8631da177e4SLinus Torvalds {
864a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
8651da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
8661da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
8671da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8681da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
8691da177e4SLinus Torvalds 	unsigned short val;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
8721da177e4SLinus Torvalds 	if (invert)
8731da177e4SLinus Torvalds 		val = mask - val;
8741da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
8751da177e4SLinus Torvalds }
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds #define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
8781da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
8791da177e4SLinus Torvalds   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
8801da177e4SLinus Torvalds   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
881666c70ffSTakashi Iwai #define FM801_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
882666c70ffSTakashi Iwai { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
883666c70ffSTakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
884666c70ffSTakashi Iwai   .name = xname, .info = snd_fm801_info_double, \
885666c70ffSTakashi Iwai   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
886666c70ffSTakashi Iwai   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
887666c70ffSTakashi Iwai   .tlv = { .p = (xtlv) } }
8881da177e4SLinus Torvalds 
889a5f22156STakashi Iwai static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
890a5f22156STakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
8911da177e4SLinus Torvalds {
8921da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
8951da177e4SLinus Torvalds 	uinfo->count = 2;
8961da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
8971da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
8981da177e4SLinus Torvalds 	return 0;
8991da177e4SLinus Torvalds }
9001da177e4SLinus Torvalds 
901a5f22156STakashi Iwai static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
902a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
9031da177e4SLinus Torvalds {
904a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9051da177e4SLinus Torvalds         int reg = kcontrol->private_value & 0xff;
9061da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
9071da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
9081da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
9091da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9121da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift_left) & mask;
9131da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = (inw(chip->port + reg) >> shift_right) & mask;
9141da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9151da177e4SLinus Torvalds 	if (invert) {
9161da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
9171da177e4SLinus Torvalds 		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
9181da177e4SLinus Torvalds 	}
9191da177e4SLinus Torvalds 	return 0;
9201da177e4SLinus Torvalds }
9211da177e4SLinus Torvalds 
922a5f22156STakashi Iwai static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
923a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
9241da177e4SLinus Torvalds {
925a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9261da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
9271da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
9281da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
9291da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
9301da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
9311da177e4SLinus Torvalds 	unsigned short val1, val2;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
9341da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
9351da177e4SLinus Torvalds 	if (invert) {
9361da177e4SLinus Torvalds 		val1 = mask - val1;
9371da177e4SLinus Torvalds 		val2 = mask - val2;
9381da177e4SLinus Torvalds 	}
9391da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg,
9401da177e4SLinus Torvalds 				     (mask << shift_left) | (mask << shift_right),
9411da177e4SLinus Torvalds 				     (val1 << shift_left ) | (val2 << shift_right));
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
944a5f22156STakashi Iwai static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
945a5f22156STakashi Iwai 			      struct snd_ctl_elem_info *uinfo)
9461da177e4SLinus Torvalds {
947ca776a28STakashi Iwai 	static const char * const texts[5] = {
9481da177e4SLinus Torvalds 		"AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
9491da177e4SLinus Torvalds 	};
9501da177e4SLinus Torvalds 
951ca776a28STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 5, texts);
9521da177e4SLinus Torvalds }
9531da177e4SLinus Torvalds 
954a5f22156STakashi Iwai static int snd_fm801_get_mux(struct snd_kcontrol *kcontrol,
955a5f22156STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
9561da177e4SLinus Torvalds {
957a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9581da177e4SLinus Torvalds         unsigned short val;
9591da177e4SLinus Torvalds 
960215dacc2SAndy Shevchenko 	val = fm801_readw(chip, REC_SRC) & 7;
9611da177e4SLinus Torvalds 	if (val > 4)
9621da177e4SLinus Torvalds 		val = 4;
9631da177e4SLinus Torvalds         ucontrol->value.enumerated.item[0] = val;
9641da177e4SLinus Torvalds         return 0;
9651da177e4SLinus Torvalds }
9661da177e4SLinus Torvalds 
967a5f22156STakashi Iwai static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
968a5f22156STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
9691da177e4SLinus Torvalds {
970a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9711da177e4SLinus Torvalds         unsigned short val;
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds         if ((val = ucontrol->value.enumerated.item[0]) > 4)
9741da177e4SLinus Torvalds                 return -EINVAL;
9751da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9780cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
979666c70ffSTakashi Iwai 
980a5f22156STakashi Iwai #define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
9811da177e4SLinus Torvalds 
982e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_fm801_controls[] = {
983666c70ffSTakashi Iwai FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
984666c70ffSTakashi Iwai 		 db_scale_dsp),
9851da177e4SLinus Torvalds FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
986666c70ffSTakashi Iwai FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
987666c70ffSTakashi Iwai 		 db_scale_dsp),
9881da177e4SLinus Torvalds FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
989666c70ffSTakashi Iwai FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
990666c70ffSTakashi Iwai 		 db_scale_dsp),
9911da177e4SLinus Torvalds FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
9921da177e4SLinus Torvalds {
9931da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
9941da177e4SLinus Torvalds 	.name = "Digital Capture Source",
9951da177e4SLinus Torvalds 	.info = snd_fm801_info_mux,
9961da177e4SLinus Torvalds 	.get = snd_fm801_get_mux,
9971da177e4SLinus Torvalds 	.put = snd_fm801_put_mux,
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds };
10001da177e4SLinus Torvalds 
1001a5f22156STakashi Iwai #define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
10021da177e4SLinus Torvalds 
1003e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_fm801_controls_multi[] = {
10041da177e4SLinus Torvalds FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
10051da177e4SLinus Torvalds FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
100610e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
100710e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
100810e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
100910e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
10101da177e4SLinus Torvalds };
10111da177e4SLinus Torvalds 
1012a5f22156STakashi Iwai static void snd_fm801_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
10131da177e4SLinus Torvalds {
1014a5f22156STakashi Iwai 	struct fm801 *chip = bus->private_data;
10151da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
10161da177e4SLinus Torvalds }
10171da177e4SLinus Torvalds 
1018a5f22156STakashi Iwai static void snd_fm801_mixer_free_ac97(struct snd_ac97 *ac97)
10191da177e4SLinus Torvalds {
1020a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
10211da177e4SLinus Torvalds 	if (ac97->num == 0) {
10221da177e4SLinus Torvalds 		chip->ac97 = NULL;
10231da177e4SLinus Torvalds 	} else {
10241da177e4SLinus Torvalds 		chip->ac97_sec = NULL;
10251da177e4SLinus Torvalds 	}
10261da177e4SLinus Torvalds }
10271da177e4SLinus Torvalds 
1028e23e7a14SBill Pemberton static int snd_fm801_mixer(struct fm801 *chip)
10291da177e4SLinus Torvalds {
1030a5f22156STakashi Iwai 	struct snd_ac97_template ac97;
10311da177e4SLinus Torvalds 	unsigned int i;
10321da177e4SLinus Torvalds 	int err;
1033a5f22156STakashi Iwai 	static struct snd_ac97_bus_ops ops = {
10341da177e4SLinus Torvalds 		.write = snd_fm801_codec_write,
10351da177e4SLinus Torvalds 		.read = snd_fm801_codec_read,
10361da177e4SLinus Torvalds 	};
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
10391da177e4SLinus Torvalds 		return err;
10401da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_fm801_mixer_free_ac97_bus;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
10431da177e4SLinus Torvalds 	ac97.private_data = chip;
10441da177e4SLinus Torvalds 	ac97.private_free = snd_fm801_mixer_free_ac97;
10451da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
10461da177e4SLinus Torvalds 		return err;
10471da177e4SLinus Torvalds 	if (chip->secondary) {
10481da177e4SLinus Torvalds 		ac97.num = 1;
10491da177e4SLinus Torvalds 		ac97.addr = chip->secondary_addr;
10501da177e4SLinus Torvalds 		if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec)) < 0)
10511da177e4SLinus Torvalds 			return err;
10521da177e4SLinus Torvalds 	}
10531da177e4SLinus Torvalds 	for (i = 0; i < FM801_CONTROLS; i++)
10541da177e4SLinus Torvalds 		snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls[i], chip));
10551da177e4SLinus Torvalds 	if (chip->multichannel) {
10561da177e4SLinus Torvalds 		for (i = 0; i < FM801_CONTROLS_MULTI; i++)
10571da177e4SLinus Torvalds 			snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 	return 0;
10601da177e4SLinus Torvalds }
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds /*
10631da177e4SLinus Torvalds  *  initialization routines
10641da177e4SLinus Torvalds  */
10651da177e4SLinus Torvalds 
1066b1e9ed26STakashi Iwai static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
1067b1e9ed26STakashi Iwai 			  unsigned short reg, unsigned long waits)
1068b1e9ed26STakashi Iwai {
1069b1e9ed26STakashi Iwai 	unsigned long timeout = jiffies + waits;
1070b1e9ed26STakashi Iwai 
1071215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_CMD,
1072215dacc2SAndy Shevchenko 		     reg | (codec_id << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
1073b1e9ed26STakashi Iwai 	udelay(5);
1074b1e9ed26STakashi Iwai 	do {
1075215dacc2SAndy Shevchenko 		if ((fm801_readw(chip, AC97_CMD) &
1076215dacc2SAndy Shevchenko 		     (FM801_AC97_VALID | FM801_AC97_BUSY)) == FM801_AC97_VALID)
1077b1e9ed26STakashi Iwai 			return 0;
1078b1e9ed26STakashi Iwai 		schedule_timeout_uninterruptible(1);
1079b1e9ed26STakashi Iwai 	} while (time_after(timeout, jiffies));
1080b1e9ed26STakashi Iwai 	return -EIO;
1081b1e9ed26STakashi Iwai }
1082b1e9ed26STakashi Iwai 
1083b1e9ed26STakashi Iwai static int snd_fm801_chip_init(struct fm801 *chip, int resume)
1084b1e9ed26STakashi Iwai {
1085b1e9ed26STakashi Iwai 	unsigned short cmdw;
1086b1e9ed26STakashi Iwai 
1087fb716c0bSOndrej Zary 	if (chip->tea575x_tuner & TUNER_ONLY)
1088e0a5d82aSAndy Shevchenko 		goto __ac97_ok;
1089e0a5d82aSAndy Shevchenko 
1090b1e9ed26STakashi Iwai 	/* codec cold reset + AC'97 warm reset */
1091215dacc2SAndy Shevchenko 	fm801_writew(chip, CODEC_CTRL, (1 << 5) | (1 << 6));
1092215dacc2SAndy Shevchenko 	fm801_readw(chip, CODEC_CTRL); /* flush posting data */
1093b1e9ed26STakashi Iwai 	udelay(100);
1094215dacc2SAndy Shevchenko 	fm801_writew(chip, CODEC_CTRL, 0);
1095b1e9ed26STakashi Iwai 
1096fb716c0bSOndrej Zary 	if (wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750)) < 0)
1097fb716c0bSOndrej Zary 		if (!resume) {
10989c7f9abfSTakashi Iwai 			dev_info(chip->card->dev,
10999c7f9abfSTakashi Iwai 				 "Primary AC'97 codec not found, assume SF64-PCR (tuner-only)\n");
1100fb716c0bSOndrej Zary 			chip->tea575x_tuner = 3 | TUNER_ONLY;
1101fb716c0bSOndrej Zary 			goto __ac97_ok;
1102b1e9ed26STakashi Iwai 		}
1103b1e9ed26STakashi Iwai 
1104b1e9ed26STakashi Iwai 	if (chip->multichannel) {
1105b1e9ed26STakashi Iwai 		if (chip->secondary_addr) {
1106b1e9ed26STakashi Iwai 			wait_for_codec(chip, chip->secondary_addr,
1107b1e9ed26STakashi Iwai 				       AC97_VENDOR_ID1, msecs_to_jiffies(50));
1108b1e9ed26STakashi Iwai 		} else {
1109b1e9ed26STakashi Iwai 			/* my card has the secondary codec */
1110b1e9ed26STakashi Iwai 			/* at address #3, so the loop is inverted */
111158e4334eSHarvey Harrison 			int i;
111258e4334eSHarvey Harrison 			for (i = 3; i > 0; i--) {
111358e4334eSHarvey Harrison 				if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
1114b1e9ed26STakashi Iwai 						     msecs_to_jiffies(50))) {
1115215dacc2SAndy Shevchenko 					cmdw = fm801_readw(chip, AC97_DATA);
1116b1e9ed26STakashi Iwai 					if (cmdw != 0xffff && cmdw != 0) {
1117b1e9ed26STakashi Iwai 						chip->secondary = 1;
111858e4334eSHarvey Harrison 						chip->secondary_addr = i;
1119b1e9ed26STakashi Iwai 						break;
1120b1e9ed26STakashi Iwai 					}
1121b1e9ed26STakashi Iwai 				}
1122b1e9ed26STakashi Iwai 			}
1123b1e9ed26STakashi Iwai 		}
1124b1e9ed26STakashi Iwai 
1125b1e9ed26STakashi Iwai 		/* the recovery phase, it seems that probing for non-existing codec might */
1126b1e9ed26STakashi Iwai 		/* cause timeout problems */
1127b1e9ed26STakashi Iwai 		wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
1128b1e9ed26STakashi Iwai 	}
1129b1e9ed26STakashi Iwai 
11306bbe13ecSJaroslav Kysela       __ac97_ok:
11316bbe13ecSJaroslav Kysela 
1132b1e9ed26STakashi Iwai 	/* init volume */
1133215dacc2SAndy Shevchenko 	fm801_writew(chip, PCM_VOL, 0x0808);
1134215dacc2SAndy Shevchenko 	fm801_writew(chip, FM_VOL, 0x9f1f);
1135215dacc2SAndy Shevchenko 	fm801_writew(chip, I2S_VOL, 0x8808);
1136b1e9ed26STakashi Iwai 
1137b1e9ed26STakashi Iwai 	/* I2S control - I2S mode */
1138215dacc2SAndy Shevchenko 	fm801_writew(chip, I2S_MODE, 0x0003);
1139b1e9ed26STakashi Iwai 
11406bbe13ecSJaroslav Kysela 	/* interrupt setup */
1141215dacc2SAndy Shevchenko 	cmdw = fm801_readw(chip, IRQ_MASK);
11426bbe13ecSJaroslav Kysela 	if (chip->irq < 0)
11436bbe13ecSJaroslav Kysela 		cmdw |= 0x00c3;		/* mask everything, no PCM nor MPU */
11446bbe13ecSJaroslav Kysela 	else
11456bbe13ecSJaroslav Kysela 		cmdw &= ~0x0083;	/* unmask MPU, PLAYBACK & CAPTURE */
1146215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_MASK, cmdw);
1147b1e9ed26STakashi Iwai 
1148b1e9ed26STakashi Iwai 	/* interrupt clear */
1149215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_STATUS,
1150215dacc2SAndy Shevchenko 		     FM801_IRQ_PLAYBACK | FM801_IRQ_CAPTURE | FM801_IRQ_MPU);
1151b1e9ed26STakashi Iwai 
1152b1e9ed26STakashi Iwai 	return 0;
1153b1e9ed26STakashi Iwai }
1154b1e9ed26STakashi Iwai 
1155b1e9ed26STakashi Iwai 
1156a5f22156STakashi Iwai static int snd_fm801_free(struct fm801 *chip)
11571da177e4SLinus Torvalds {
11581da177e4SLinus Torvalds 	unsigned short cmdw;
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	if (chip->irq < 0)
11611da177e4SLinus Torvalds 		goto __end_hw;
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	/* interrupt setup - mask everything */
1164215dacc2SAndy Shevchenko 	cmdw = fm801_readw(chip, IRQ_MASK);
11651da177e4SLinus Torvalds 	cmdw |= 0x00c3;
1166215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_MASK, cmdw);
11671da177e4SLinus Torvalds 
1168*e97e98c6SAndy Shevchenko 	devm_free_irq(&chip->pci->dev, chip->irq, chip);
1169*e97e98c6SAndy Shevchenko 
11701da177e4SLinus Torvalds       __end_hw:
1171fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1172d4ecc83bSHans Verkuil 	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
11731da177e4SLinus Torvalds 		snd_tea575x_exit(&chip->tea);
1174d4ecc83bSHans Verkuil 		v4l2_device_unregister(&chip->v4l2_dev);
1175d4ecc83bSHans Verkuil 	}
11761da177e4SLinus Torvalds #endif
11771da177e4SLinus Torvalds 	return 0;
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds 
1180a5f22156STakashi Iwai static int snd_fm801_dev_free(struct snd_device *device)
11811da177e4SLinus Torvalds {
1182a5f22156STakashi Iwai 	struct fm801 *chip = device->device_data;
11831da177e4SLinus Torvalds 	return snd_fm801_free(chip);
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
1186e23e7a14SBill Pemberton static int snd_fm801_create(struct snd_card *card,
11871da177e4SLinus Torvalds 			    struct pci_dev *pci,
11881da177e4SLinus Torvalds 			    int tea575x_tuner,
1189d4ecc83bSHans Verkuil 			    int radio_nr,
1190a5f22156STakashi Iwai 			    struct fm801 **rchip)
11911da177e4SLinus Torvalds {
1192a5f22156STakashi Iwai 	struct fm801 *chip;
11931da177e4SLinus Torvalds 	int err;
1194a5f22156STakashi Iwai 	static struct snd_device_ops ops = {
11951da177e4SLinus Torvalds 		.dev_free =	snd_fm801_dev_free,
11961da177e4SLinus Torvalds 	};
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	*rchip = NULL;
11995618955cSAndy Shevchenko 	if ((err = pcim_enable_device(pci)) < 0)
12001da177e4SLinus Torvalds 		return err;
12015618955cSAndy Shevchenko 	chip = devm_kzalloc(&pci->dev, sizeof(*chip), GFP_KERNEL);
12025618955cSAndy Shevchenko 	if (chip == NULL)
12031da177e4SLinus Torvalds 		return -ENOMEM;
12041da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
12051da177e4SLinus Torvalds 	chip->card = card;
12061da177e4SLinus Torvalds 	chip->pci = pci;
12071da177e4SLinus Torvalds 	chip->irq = -1;
12086bbe13ecSJaroslav Kysela 	chip->tea575x_tuner = tea575x_tuner;
12095618955cSAndy Shevchenko 	if ((err = pci_request_regions(pci, "FM801")) < 0)
12101da177e4SLinus Torvalds 		return err;
12111da177e4SLinus Torvalds 	chip->port = pci_resource_start(pci, 0);
1212fb716c0bSOndrej Zary 	if ((tea575x_tuner & TUNER_ONLY) == 0) {
12135618955cSAndy Shevchenko 		if (devm_request_irq(&pci->dev, pci->irq, snd_fm801_interrupt,
12145618955cSAndy Shevchenko 				IRQF_SHARED, KBUILD_MODNAME, chip)) {
12155618955cSAndy Shevchenko 			dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
12161da177e4SLinus Torvalds 			snd_fm801_free(chip);
12171da177e4SLinus Torvalds 			return -EBUSY;
12181da177e4SLinus Torvalds 		}
12191da177e4SLinus Torvalds 		chip->irq = pci->irq;
12201da177e4SLinus Torvalds 		pci_set_master(pci);
12216bbe13ecSJaroslav Kysela 	}
12221da177e4SLinus Torvalds 
122344c10138SAuke Kok 	if (pci->revision >= 0xb1)	/* FM801-AU */
12241da177e4SLinus Torvalds 		chip->multichannel = 1;
12251da177e4SLinus Torvalds 
1226b1e9ed26STakashi Iwai 	snd_fm801_chip_init(chip, 0);
1227fb716c0bSOndrej Zary 	/* init might set tuner access method */
1228fb716c0bSOndrej Zary 	tea575x_tuner = chip->tea575x_tuner;
1229fb716c0bSOndrej Zary 
12301da177e4SLinus Torvalds 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
12311da177e4SLinus Torvalds 		snd_fm801_free(chip);
12321da177e4SLinus Torvalds 		return err;
12331da177e4SLinus Torvalds 	}
12341da177e4SLinus Torvalds 
1235fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1236d4ecc83bSHans Verkuil 	err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
1237d4ecc83bSHans Verkuil 	if (err < 0) {
1238d4ecc83bSHans Verkuil 		snd_fm801_free(chip);
1239d4ecc83bSHans Verkuil 		return err;
1240d4ecc83bSHans Verkuil 	}
1241d4ecc83bSHans Verkuil 	chip->tea.v4l2_dev = &chip->v4l2_dev;
1242d4ecc83bSHans Verkuil 	chip->tea.radio_nr = radio_nr;
12431da177e4SLinus Torvalds 	chip->tea.private_data = chip;
1244938a1566SOndrej Zary 	chip->tea.ops = &snd_fm801_tea_ops;
124510ca7201SOndrej Zary 	sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
1246d7ba858aSOndrej Zary 	if ((tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
1247d7ba858aSOndrej Zary 	    (tea575x_tuner & TUNER_TYPE_MASK) < 4) {
12485daf53a6SHans de Goede 		if (snd_tea575x_init(&chip->tea, THIS_MODULE)) {
12499c7f9abfSTakashi Iwai 			dev_err(card->dev, "TEA575x radio not found\n");
1250d4ecc83bSHans Verkuil 			snd_fm801_free(chip);
125196760015SDan Carpenter 			return -ENODEV;
125296760015SDan Carpenter 		}
125396760015SDan Carpenter 	} else if ((tea575x_tuner & TUNER_TYPE_MASK) == 0) {
1254d7ba858aSOndrej Zary 		/* autodetect tuner connection */
1255d7ba858aSOndrej Zary 		for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
1256d7ba858aSOndrej Zary 			chip->tea575x_tuner = tea575x_tuner;
12575daf53a6SHans de Goede 			if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
12589c7f9abfSTakashi Iwai 				dev_info(card->dev,
12599c7f9abfSTakashi Iwai 					 "detected TEA575x radio type %s\n",
12608e699d2cSTakashi Iwai 					   get_tea575x_gpio(chip)->name);
1261d7ba858aSOndrej Zary 				break;
1262d7ba858aSOndrej Zary 			}
12631da177e4SLinus Torvalds 		}
126496760015SDan Carpenter 		if (tea575x_tuner == 4) {
12659c7f9abfSTakashi Iwai 			dev_err(card->dev, "TEA575x radio not found\n");
1266c37279b9SBen Hutchings 			chip->tea575x_tuner = TUNER_DISABLED;
126796760015SDan Carpenter 		}
126896760015SDan Carpenter 	}
1269c37279b9SBen Hutchings 	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
12708e699d2cSTakashi Iwai 		strlcpy(chip->tea.card, get_tea575x_gpio(chip)->name,
1271c37279b9SBen Hutchings 			sizeof(chip->tea.card));
1272c37279b9SBen Hutchings 	}
12731da177e4SLinus Torvalds #endif
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	*rchip = chip;
12761da177e4SLinus Torvalds 	return 0;
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
1279e23e7a14SBill Pemberton static int snd_card_fm801_probe(struct pci_dev *pci,
12801da177e4SLinus Torvalds 				const struct pci_device_id *pci_id)
12811da177e4SLinus Torvalds {
12821da177e4SLinus Torvalds 	static int dev;
1283a5f22156STakashi Iwai 	struct snd_card *card;
1284a5f22156STakashi Iwai 	struct fm801 *chip;
1285a5f22156STakashi Iwai 	struct snd_opl3 *opl3;
12861da177e4SLinus Torvalds 	int err;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds         if (dev >= SNDRV_CARDS)
12891da177e4SLinus Torvalds                 return -ENODEV;
12901da177e4SLinus Torvalds 	if (!enable[dev]) {
12911da177e4SLinus Torvalds 		dev++;
12921da177e4SLinus Torvalds 		return -ENOENT;
12931da177e4SLinus Torvalds 	}
12941da177e4SLinus Torvalds 
129560c5772bSTakashi Iwai 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
129660c5772bSTakashi Iwai 			   0, &card);
1297e58de7baSTakashi Iwai 	if (err < 0)
1298e58de7baSTakashi Iwai 		return err;
1299d4ecc83bSHans Verkuil 	if ((err = snd_fm801_create(card, pci, tea575x_tuner[dev], radio_nr[dev], &chip)) < 0) {
13001da177e4SLinus Torvalds 		snd_card_free(card);
13011da177e4SLinus Torvalds 		return err;
13021da177e4SLinus Torvalds 	}
1303b1e9ed26STakashi Iwai 	card->private_data = chip;
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 	strcpy(card->driver, "FM801");
13061da177e4SLinus Torvalds 	strcpy(card->shortname, "ForteMedia FM801-");
13071da177e4SLinus Torvalds 	strcat(card->shortname, chip->multichannel ? "AU" : "AS");
13081da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %i",
13091da177e4SLinus Torvalds 		card->shortname, chip->port, chip->irq);
13101da177e4SLinus Torvalds 
1311fb716c0bSOndrej Zary 	if (chip->tea575x_tuner & TUNER_ONLY)
1312e0a5d82aSAndy Shevchenko 		goto __fm801_tuner_only;
1313e0a5d82aSAndy Shevchenko 
1314483337f9SLars-Peter Clausen 	if ((err = snd_fm801_pcm(chip, 0)) < 0) {
13151da177e4SLinus Torvalds 		snd_card_free(card);
13161da177e4SLinus Torvalds 		return err;
13171da177e4SLinus Torvalds 	}
13181da177e4SLinus Torvalds 	if ((err = snd_fm801_mixer(chip)) < 0) {
13191da177e4SLinus Torvalds 		snd_card_free(card);
13201da177e4SLinus Torvalds 		return err;
13211da177e4SLinus Torvalds 	}
13221da177e4SLinus Torvalds 	if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1323215dacc2SAndy Shevchenko 				       chip->port + FM801_MPU401_DATA,
1324dba8b469SClemens Ladisch 				       MPU401_INFO_INTEGRATED |
1325dba8b469SClemens Ladisch 				       MPU401_INFO_IRQ_HOOK,
1326dba8b469SClemens Ladisch 				       -1, &chip->rmidi)) < 0) {
13271da177e4SLinus Torvalds 		snd_card_free(card);
13281da177e4SLinus Torvalds 		return err;
13291da177e4SLinus Torvalds 	}
1330215dacc2SAndy Shevchenko 	if ((err = snd_opl3_create(card, chip->port + FM801_OPL3_BANK0,
1331215dacc2SAndy Shevchenko 				   chip->port + FM801_OPL3_BANK1,
13321da177e4SLinus Torvalds 				   OPL3_HW_OPL3_FM801, 1, &opl3)) < 0) {
13331da177e4SLinus Torvalds 		snd_card_free(card);
13341da177e4SLinus Torvalds 		return err;
13351da177e4SLinus Torvalds 	}
13361da177e4SLinus Torvalds 	if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
13371da177e4SLinus Torvalds 		snd_card_free(card);
13381da177e4SLinus Torvalds 		return err;
13391da177e4SLinus Torvalds 	}
13401da177e4SLinus Torvalds 
1341e0a5d82aSAndy Shevchenko       __fm801_tuner_only:
13421da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
13431da177e4SLinus Torvalds 		snd_card_free(card);
13441da177e4SLinus Torvalds 		return err;
13451da177e4SLinus Torvalds 	}
13461da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
13471da177e4SLinus Torvalds 	dev++;
13481da177e4SLinus Torvalds 	return 0;
13491da177e4SLinus Torvalds }
13501da177e4SLinus Torvalds 
1351e23e7a14SBill Pemberton static void snd_card_fm801_remove(struct pci_dev *pci)
13521da177e4SLinus Torvalds {
13531da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
13541da177e4SLinus Torvalds }
13551da177e4SLinus Torvalds 
1356c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
1357b1e9ed26STakashi Iwai static unsigned char saved_regs[] = {
1358b1e9ed26STakashi Iwai 	FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
1359b1e9ed26STakashi Iwai 	FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
1360b1e9ed26STakashi Iwai 	FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
1361b1e9ed26STakashi Iwai 	FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
1362b1e9ed26STakashi Iwai };
1363b1e9ed26STakashi Iwai 
136468cb2b55STakashi Iwai static int snd_fm801_suspend(struct device *dev)
1365b1e9ed26STakashi Iwai {
136668cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1367b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1368b1e9ed26STakashi Iwai 	int i;
1369b1e9ed26STakashi Iwai 
1370b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1371b1e9ed26STakashi Iwai 	snd_pcm_suspend_all(chip->pcm);
1372b1e9ed26STakashi Iwai 	snd_ac97_suspend(chip->ac97);
1373b1e9ed26STakashi Iwai 	snd_ac97_suspend(chip->ac97_sec);
1374b1e9ed26STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1375b1e9ed26STakashi Iwai 		chip->saved_regs[i] = inw(chip->port + saved_regs[i]);
1376b1e9ed26STakashi Iwai 	/* FIXME: tea575x suspend */
1377b1e9ed26STakashi Iwai 	return 0;
1378b1e9ed26STakashi Iwai }
1379b1e9ed26STakashi Iwai 
138068cb2b55STakashi Iwai static int snd_fm801_resume(struct device *dev)
1381b1e9ed26STakashi Iwai {
138268cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1383b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1384b1e9ed26STakashi Iwai 	int i;
1385b1e9ed26STakashi Iwai 
1386b1e9ed26STakashi Iwai 	snd_fm801_chip_init(chip, 1);
1387b1e9ed26STakashi Iwai 	snd_ac97_resume(chip->ac97);
1388b1e9ed26STakashi Iwai 	snd_ac97_resume(chip->ac97_sec);
1389b1e9ed26STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
1390b1e9ed26STakashi Iwai 		outw(chip->saved_regs[i], chip->port + saved_regs[i]);
1391b1e9ed26STakashi Iwai 
1392b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1393b1e9ed26STakashi Iwai 	return 0;
1394b1e9ed26STakashi Iwai }
139568cb2b55STakashi Iwai 
139668cb2b55STakashi Iwai static SIMPLE_DEV_PM_OPS(snd_fm801_pm, snd_fm801_suspend, snd_fm801_resume);
139768cb2b55STakashi Iwai #define SND_FM801_PM_OPS	&snd_fm801_pm
139868cb2b55STakashi Iwai #else
139968cb2b55STakashi Iwai #define SND_FM801_PM_OPS	NULL
1400c7561cd8STakashi Iwai #endif /* CONFIG_PM_SLEEP */
1401b1e9ed26STakashi Iwai 
1402e9f66d9bSTakashi Iwai static struct pci_driver fm801_driver = {
14033733e424STakashi Iwai 	.name = KBUILD_MODNAME,
14041da177e4SLinus Torvalds 	.id_table = snd_fm801_ids,
14051da177e4SLinus Torvalds 	.probe = snd_card_fm801_probe,
1406e23e7a14SBill Pemberton 	.remove = snd_card_fm801_remove,
140768cb2b55STakashi Iwai 	.driver = {
140868cb2b55STakashi Iwai 		.pm = SND_FM801_PM_OPS,
140968cb2b55STakashi Iwai 	},
14101da177e4SLinus Torvalds };
14111da177e4SLinus Torvalds 
1412e9f66d9bSTakashi Iwai module_pci_driver(fm801_driver);
1413