xref: /linux/sound/pci/fm801.c (revision d71a13f4c0e659c7ce7bef872c2e6a2287868e0e)
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
33d647f0b7SMauro Carvalho Chehab #include <media/drv-intf/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 {
166d3d33aabSAndy Shevchenko 	struct device *dev;
1671da177e4SLinus Torvalds 	int irq;
1681da177e4SLinus Torvalds 
169052c233eSAndy Shevchenko 	unsigned long port;
170052c233eSAndy Shevchenko 	unsigned int multichannel: 1,
171052c233eSAndy Shevchenko 		     secondary: 1;
172052c233eSAndy Shevchenko 	unsigned char secondary_addr;
173052c233eSAndy Shevchenko 	unsigned int tea575x_tuner;
1741da177e4SLinus Torvalds 
175052c233eSAndy Shevchenko 	unsigned short ply_ctrl;
176052c233eSAndy Shevchenko 	unsigned short cap_ctrl;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	unsigned long ply_buffer;
1791da177e4SLinus Torvalds 	unsigned int ply_buf;
1801da177e4SLinus Torvalds 	unsigned int ply_count;
1811da177e4SLinus Torvalds 	unsigned int ply_size;
1821da177e4SLinus Torvalds 	unsigned int ply_pos;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	unsigned long cap_buffer;
1851da177e4SLinus Torvalds 	unsigned int cap_buf;
1861da177e4SLinus Torvalds 	unsigned int cap_count;
1871da177e4SLinus Torvalds 	unsigned int cap_size;
1881da177e4SLinus Torvalds 	unsigned int cap_pos;
1891da177e4SLinus Torvalds 
190a5f22156STakashi Iwai 	struct snd_ac97_bus *ac97_bus;
191a5f22156STakashi Iwai 	struct snd_ac97 *ac97;
192a5f22156STakashi Iwai 	struct snd_ac97 *ac97_sec;
1931da177e4SLinus Torvalds 
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 
2154b5c15f7SAndy Shevchenko /*
2164b5c15f7SAndy Shevchenko  * IO accessors
2174b5c15f7SAndy Shevchenko  */
2184b5c15f7SAndy Shevchenko 
2194b5c15f7SAndy Shevchenko static inline void fm801_iowrite16(struct fm801 *chip, unsigned short offset, u16 value)
2204b5c15f7SAndy Shevchenko {
2214b5c15f7SAndy Shevchenko 	outw(value, chip->port + offset);
2224b5c15f7SAndy Shevchenko }
2234b5c15f7SAndy Shevchenko 
2244b5c15f7SAndy Shevchenko static inline u16 fm801_ioread16(struct fm801 *chip, unsigned short offset)
2254b5c15f7SAndy Shevchenko {
2264b5c15f7SAndy Shevchenko 	return inw(chip->port + offset);
2274b5c15f7SAndy Shevchenko }
2284b5c15f7SAndy Shevchenko 
2299baa3c34SBenoit Taine static const struct pci_device_id snd_fm801_ids[] = {
2301da177e4SLinus Torvalds 	{ 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* FM801 */
23126be8659SSergey Vlasov 	{ 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* Gallant Odyssey Sound 4 */
2321da177e4SLinus Torvalds 	{ 0, }
2331da177e4SLinus Torvalds };
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds /*
2381da177e4SLinus Torvalds  *  common I/O routines
2391da177e4SLinus Torvalds  */
2401da177e4SLinus Torvalds 
24102fd1a76SAndy Shevchenko static bool fm801_ac97_is_ready(struct fm801 *chip, unsigned int iterations)
24202fd1a76SAndy Shevchenko {
24302fd1a76SAndy Shevchenko 	unsigned int idx;
24402fd1a76SAndy Shevchenko 
24502fd1a76SAndy Shevchenko 	for (idx = 0; idx < iterations; idx++) {
24602fd1a76SAndy Shevchenko 		if (!(fm801_readw(chip, AC97_CMD) & FM801_AC97_BUSY))
24702fd1a76SAndy Shevchenko 			return true;
24802fd1a76SAndy Shevchenko 		udelay(10);
24902fd1a76SAndy Shevchenko 	}
25002fd1a76SAndy Shevchenko 	return false;
25102fd1a76SAndy Shevchenko }
25202fd1a76SAndy Shevchenko 
25302fd1a76SAndy Shevchenko static bool fm801_ac97_is_valid(struct fm801 *chip, unsigned int iterations)
25402fd1a76SAndy Shevchenko {
25502fd1a76SAndy Shevchenko 	unsigned int idx;
25602fd1a76SAndy Shevchenko 
25702fd1a76SAndy Shevchenko 	for (idx = 0; idx < iterations; idx++) {
25802fd1a76SAndy Shevchenko 		if (fm801_readw(chip, AC97_CMD) & FM801_AC97_VALID)
25902fd1a76SAndy Shevchenko 			return true;
26002fd1a76SAndy Shevchenko 		udelay(10);
26102fd1a76SAndy Shevchenko 	}
26202fd1a76SAndy Shevchenko 	return false;
26302fd1a76SAndy Shevchenko }
26402fd1a76SAndy Shevchenko 
265a5f22156STakashi Iwai static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
2661da177e4SLinus Torvalds 				 unsigned short mask, unsigned short value)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	int change;
2691da177e4SLinus Torvalds 	unsigned long flags;
2701da177e4SLinus Torvalds 	unsigned short old, new;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
2734b5c15f7SAndy Shevchenko 	old = fm801_ioread16(chip, reg);
2741da177e4SLinus Torvalds 	new = (old & ~mask) | value;
2751da177e4SLinus Torvalds 	change = old != new;
2761da177e4SLinus Torvalds 	if (change)
2774b5c15f7SAndy Shevchenko 		fm801_iowrite16(chip, reg, new);
2781da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
2791da177e4SLinus Torvalds 	return change;
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
282a5f22156STakashi Iwai static void snd_fm801_codec_write(struct snd_ac97 *ac97,
2831da177e4SLinus Torvalds 				  unsigned short reg,
2841da177e4SLinus Torvalds 				  unsigned short val)
2851da177e4SLinus Torvalds {
286a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	/*
2891da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
2901da177e4SLinus Torvalds 	 */
29102fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 100)) {
2929c7f9abfSTakashi Iwai 		dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
2931da177e4SLinus Torvalds 		return;
29402fd1a76SAndy Shevchenko 	}
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/* write data and address */
297215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_DATA, val);
298215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_CMD, reg | (ac97->addr << FM801_AC97_ADDR_SHIFT));
2991da177e4SLinus Torvalds 	/*
3001da177e4SLinus Torvalds 	 *  Wait until the write command is not completed..
3011da177e4SLinus Torvalds 	 */
30202fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 1000))
30302fd1a76SAndy Shevchenko 		dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
30402fd1a76SAndy Shevchenko 		ac97->num);
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
307a5f22156STakashi Iwai static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
3081da177e4SLinus Torvalds {
309a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/*
3121da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
3131da177e4SLinus Torvalds 	 */
31402fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 100)) {
3159c7f9abfSTakashi Iwai 		dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
3161da177e4SLinus Torvalds 		return 0;
31702fd1a76SAndy Shevchenko 	}
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	/* read command */
320215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_CMD,
321215dacc2SAndy Shevchenko 		     reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
32202fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 100)) {
32302fd1a76SAndy Shevchenko 		dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
32402fd1a76SAndy Shevchenko 			ac97->num);
3251da177e4SLinus Torvalds 		return 0;
3261da177e4SLinus Torvalds 	}
3271da177e4SLinus Torvalds 
32802fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_valid(chip, 1000)) {
32902fd1a76SAndy Shevchenko 		dev_err(chip->card->dev,
33002fd1a76SAndy Shevchenko 			"AC'97 interface #%d is not valid (2)\n", ac97->num);
33102fd1a76SAndy Shevchenko 		return 0;
33202fd1a76SAndy Shevchenko 	}
33302fd1a76SAndy Shevchenko 
334215dacc2SAndy Shevchenko 	return fm801_readw(chip, AC97_DATA);
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
337*d71a13f4STakashi Iwai static const unsigned int rates[] = {
3381da177e4SLinus Torvalds   5500,  8000,  9600, 11025,
3391da177e4SLinus Torvalds   16000, 19200, 22050, 32000,
3401da177e4SLinus Torvalds   38400, 44100, 48000
3411da177e4SLinus Torvalds };
3421da177e4SLinus Torvalds 
343*d71a13f4STakashi Iwai static const struct snd_pcm_hw_constraint_list hw_constraints_rates = {
3441da177e4SLinus Torvalds 	.count = ARRAY_SIZE(rates),
3451da177e4SLinus Torvalds 	.list = rates,
3461da177e4SLinus Torvalds 	.mask = 0,
3471da177e4SLinus Torvalds };
3481da177e4SLinus Torvalds 
349*d71a13f4STakashi Iwai static const unsigned int channels[] = {
3501da177e4SLinus Torvalds   2, 4, 6
3511da177e4SLinus Torvalds };
3521da177e4SLinus Torvalds 
353*d71a13f4STakashi Iwai static const struct snd_pcm_hw_constraint_list hw_constraints_channels = {
3545e4968e2STobias Klauser 	.count = ARRAY_SIZE(channels),
3551da177e4SLinus Torvalds 	.list = channels,
3561da177e4SLinus Torvalds 	.mask = 0,
3571da177e4SLinus Torvalds };
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds /*
3601da177e4SLinus Torvalds  *  Sample rate routines
3611da177e4SLinus Torvalds  */
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds static unsigned short snd_fm801_rate_bits(unsigned int rate)
3641da177e4SLinus Torvalds {
3651da177e4SLinus Torvalds 	unsigned int idx;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
3681da177e4SLinus Torvalds 		if (rates[idx] == rate)
3691da177e4SLinus Torvalds 			return idx;
3701da177e4SLinus Torvalds 	snd_BUG();
3711da177e4SLinus Torvalds 	return ARRAY_SIZE(rates) - 1;
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds /*
3751da177e4SLinus Torvalds  *  PCM part
3761da177e4SLinus Torvalds  */
3771da177e4SLinus Torvalds 
378a5f22156STakashi Iwai static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
3791da177e4SLinus Torvalds 				      int cmd)
3801da177e4SLinus Torvalds {
381a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3841da177e4SLinus Torvalds 	switch (cmd) {
3851da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
3861da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_BUF1_LAST |
3871da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
3881da177e4SLinus Torvalds 				     FM801_PAUSE);
3891da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_START |
3901da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
3911da177e4SLinus Torvalds 		break;
3921da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
3931da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
3941da177e4SLinus Torvalds 		break;
3951da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
396b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
3971da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_PAUSE;
3981da177e4SLinus Torvalds 		break;
3991da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
400b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
4011da177e4SLinus Torvalds 		chip->ply_ctrl &= ~FM801_PAUSE;
4021da177e4SLinus Torvalds 		break;
4031da177e4SLinus Torvalds 	default:
4041da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
4051da177e4SLinus Torvalds 		snd_BUG();
4061da177e4SLinus Torvalds 		return -EINVAL;
4071da177e4SLinus Torvalds 	}
408215dacc2SAndy Shevchenko 	fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
4091da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4101da177e4SLinus Torvalds 	return 0;
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
413a5f22156STakashi Iwai static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
4141da177e4SLinus Torvalds 				     int cmd)
4151da177e4SLinus Torvalds {
416a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
4191da177e4SLinus Torvalds 	switch (cmd) {
4201da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4211da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_BUF1_LAST |
4221da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
4231da177e4SLinus Torvalds 				     FM801_PAUSE);
4241da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_START |
4251da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
4261da177e4SLinus Torvalds 		break;
4271da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4281da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
4291da177e4SLinus Torvalds 		break;
4301da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
431b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
4321da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_PAUSE;
4331da177e4SLinus Torvalds 		break;
4341da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
435b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
4361da177e4SLinus Torvalds 		chip->cap_ctrl &= ~FM801_PAUSE;
4371da177e4SLinus Torvalds 		break;
4381da177e4SLinus Torvalds 	default:
4391da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
4401da177e4SLinus Torvalds 		snd_BUG();
4411da177e4SLinus Torvalds 		return -EINVAL;
4421da177e4SLinus Torvalds 	}
443215dacc2SAndy Shevchenko 	fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
4441da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4451da177e4SLinus Torvalds 	return 0;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
448a5f22156STakashi Iwai static int snd_fm801_hw_params(struct snd_pcm_substream *substream,
449a5f22156STakashi Iwai 			       struct snd_pcm_hw_params *hw_params)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
454a5f22156STakashi Iwai static int snd_fm801_hw_free(struct snd_pcm_substream *substream)
4551da177e4SLinus Torvalds {
4561da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
459a5f22156STakashi Iwai static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
4601da177e4SLinus Torvalds {
461a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
462a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
4651da177e4SLinus Torvalds 	chip->ply_count = snd_pcm_lib_period_bytes(substream);
4661da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
4671da177e4SLinus Torvalds 	chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
4681da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK |
4691da177e4SLinus Torvalds 			     FM801_CHANNELS_MASK);
4701da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
4711da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_16BIT;
4721da177e4SLinus Torvalds 	if (runtime->channels > 1) {
4731da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_STEREO;
4741da177e4SLinus Torvalds 		if (runtime->channels == 4)
4751da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_4;
4761da177e4SLinus Torvalds 		else if (runtime->channels == 6)
4771da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_6;
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds 	chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
4801da177e4SLinus Torvalds 	chip->ply_buf = 0;
481215dacc2SAndy Shevchenko 	fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
482215dacc2SAndy Shevchenko 	fm801_writew(chip, PLY_COUNT, chip->ply_count - 1);
4831da177e4SLinus Torvalds 	chip->ply_buffer = runtime->dma_addr;
4841da177e4SLinus Torvalds 	chip->ply_pos = 0;
485215dacc2SAndy Shevchenko 	fm801_writel(chip, PLY_BUF1, chip->ply_buffer);
486215dacc2SAndy Shevchenko 	fm801_writel(chip, PLY_BUF2,
487215dacc2SAndy Shevchenko 		     chip->ply_buffer + (chip->ply_count % chip->ply_size));
4881da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
4891da177e4SLinus Torvalds 	return 0;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
492a5f22156STakashi Iwai static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
4931da177e4SLinus Torvalds {
494a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
495a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
4981da177e4SLinus Torvalds 	chip->cap_count = snd_pcm_lib_period_bytes(substream);
4991da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
5001da177e4SLinus Torvalds 	chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
5011da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK);
5021da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
5031da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_16BIT;
5041da177e4SLinus Torvalds 	if (runtime->channels > 1)
5051da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_STEREO;
5061da177e4SLinus Torvalds 	chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
5071da177e4SLinus Torvalds 	chip->cap_buf = 0;
508215dacc2SAndy Shevchenko 	fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
509215dacc2SAndy Shevchenko 	fm801_writew(chip, CAP_COUNT, chip->cap_count - 1);
5101da177e4SLinus Torvalds 	chip->cap_buffer = runtime->dma_addr;
5111da177e4SLinus Torvalds 	chip->cap_pos = 0;
512215dacc2SAndy Shevchenko 	fm801_writel(chip, CAP_BUF1, chip->cap_buffer);
513215dacc2SAndy Shevchenko 	fm801_writel(chip, CAP_BUF2,
514215dacc2SAndy Shevchenko 		     chip->cap_buffer + (chip->cap_count % chip->cap_size));
5151da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
5161da177e4SLinus Torvalds 	return 0;
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
519a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
5201da177e4SLinus Torvalds {
521a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
5221da177e4SLinus Torvalds 	size_t ptr;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	if (!(chip->ply_ctrl & FM801_START))
5251da177e4SLinus Torvalds 		return 0;
5261da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
527215dacc2SAndy Shevchenko 	ptr = chip->ply_pos + (chip->ply_count - 1) - fm801_readw(chip, PLY_COUNT);
528215dacc2SAndy Shevchenko 	if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_PLAYBACK) {
5291da177e4SLinus Torvalds 		ptr += chip->ply_count;
5301da177e4SLinus Torvalds 		ptr %= chip->ply_size;
5311da177e4SLinus Torvalds 	}
5321da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5331da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5341da177e4SLinus Torvalds }
5351da177e4SLinus Torvalds 
536a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
5371da177e4SLinus Torvalds {
538a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
5391da177e4SLinus Torvalds 	size_t ptr;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	if (!(chip->cap_ctrl & FM801_START))
5421da177e4SLinus Torvalds 		return 0;
5431da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
544215dacc2SAndy Shevchenko 	ptr = chip->cap_pos + (chip->cap_count - 1) - fm801_readw(chip, CAP_COUNT);
545215dacc2SAndy Shevchenko 	if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_CAPTURE) {
5461da177e4SLinus Torvalds 		ptr += chip->cap_count;
5471da177e4SLinus Torvalds 		ptr %= chip->cap_size;
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5501da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5537d12e780SDavid Howells static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
5541da177e4SLinus Torvalds {
555a5f22156STakashi Iwai 	struct fm801 *chip = dev_id;
5561da177e4SLinus Torvalds 	unsigned short status;
5571da177e4SLinus Torvalds 	unsigned int tmp;
5581da177e4SLinus Torvalds 
559215dacc2SAndy Shevchenko 	status = fm801_readw(chip, IRQ_STATUS);
5601da177e4SLinus Torvalds 	status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
5611da177e4SLinus Torvalds 	if (! status)
5621da177e4SLinus Torvalds 		return IRQ_NONE;
5631da177e4SLinus Torvalds 	/* ack first */
564215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_STATUS, status);
5651da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
5661da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5671da177e4SLinus Torvalds 		chip->ply_buf++;
5681da177e4SLinus Torvalds 		chip->ply_pos += chip->ply_count;
5691da177e4SLinus Torvalds 		chip->ply_pos %= chip->ply_size;
5701da177e4SLinus Torvalds 		tmp = chip->ply_pos + chip->ply_count;
5711da177e4SLinus Torvalds 		tmp %= chip->ply_size;
572215dacc2SAndy Shevchenko 		if (chip->ply_buf & 1)
573215dacc2SAndy Shevchenko 			fm801_writel(chip, PLY_BUF1, chip->ply_buffer + tmp);
574215dacc2SAndy Shevchenko 		else
575215dacc2SAndy Shevchenko 			fm801_writel(chip, PLY_BUF2, chip->ply_buffer + tmp);
5761da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5771da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->playback_substream);
5781da177e4SLinus Torvalds 	}
5791da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
5801da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5811da177e4SLinus Torvalds 		chip->cap_buf++;
5821da177e4SLinus Torvalds 		chip->cap_pos += chip->cap_count;
5831da177e4SLinus Torvalds 		chip->cap_pos %= chip->cap_size;
5841da177e4SLinus Torvalds 		tmp = chip->cap_pos + chip->cap_count;
5851da177e4SLinus Torvalds 		tmp %= chip->cap_size;
586215dacc2SAndy Shevchenko 		if (chip->cap_buf & 1)
587215dacc2SAndy Shevchenko 			fm801_writel(chip, CAP_BUF1, chip->cap_buffer + tmp);
588215dacc2SAndy Shevchenko 		else
589215dacc2SAndy Shevchenko 			fm801_writel(chip, CAP_BUF2, chip->cap_buffer + tmp);
5901da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5911da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->capture_substream);
5921da177e4SLinus Torvalds 	}
5931da177e4SLinus Torvalds 	if (chip->rmidi && (status & FM801_IRQ_MPU))
5947d12e780SDavid Howells 		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
595997c87daSAndy Shevchenko 	if (status & FM801_IRQ_VOLUME) {
596997c87daSAndy Shevchenko 		/* TODO */
597997c87daSAndy Shevchenko 	}
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	return IRQ_HANDLED;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
602a5f22156STakashi Iwai static struct snd_pcm_hardware snd_fm801_playback =
6031da177e4SLinus Torvalds {
6041da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
6051da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
606b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
6071da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
6081da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
6091da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
6101da177e4SLinus Torvalds 	.rate_min =		5500,
6111da177e4SLinus Torvalds 	.rate_max =		48000,
6121da177e4SLinus Torvalds 	.channels_min =		1,
6131da177e4SLinus Torvalds 	.channels_max =		2,
6141da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
6151da177e4SLinus Torvalds 	.period_bytes_min =	64,
6161da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
6171da177e4SLinus Torvalds 	.periods_min =		1,
6181da177e4SLinus Torvalds 	.periods_max =		1024,
6191da177e4SLinus Torvalds 	.fifo_size =		0,
6201da177e4SLinus Torvalds };
6211da177e4SLinus Torvalds 
622a5f22156STakashi Iwai static struct snd_pcm_hardware snd_fm801_capture =
6231da177e4SLinus Torvalds {
6241da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
6251da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
626b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
6271da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
6281da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
6291da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
6301da177e4SLinus Torvalds 	.rate_min =		5500,
6311da177e4SLinus Torvalds 	.rate_max =		48000,
6321da177e4SLinus Torvalds 	.channels_min =		1,
6331da177e4SLinus Torvalds 	.channels_max =		2,
6341da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
6351da177e4SLinus Torvalds 	.period_bytes_min =	64,
6361da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
6371da177e4SLinus Torvalds 	.periods_min =		1,
6381da177e4SLinus Torvalds 	.periods_max =		1024,
6391da177e4SLinus Torvalds 	.fifo_size =		0,
6401da177e4SLinus Torvalds };
6411da177e4SLinus Torvalds 
642a5f22156STakashi Iwai static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
6431da177e4SLinus Torvalds {
644a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
645a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6461da177e4SLinus Torvalds 	int err;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	chip->playback_substream = substream;
6491da177e4SLinus Torvalds 	runtime->hw = snd_fm801_playback;
650a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
651a5f22156STakashi Iwai 				   &hw_constraints_rates);
6521da177e4SLinus Torvalds 	if (chip->multichannel) {
6531da177e4SLinus Torvalds 		runtime->hw.channels_max = 6;
654a5f22156STakashi Iwai 		snd_pcm_hw_constraint_list(runtime, 0,
655a5f22156STakashi Iwai 					   SNDRV_PCM_HW_PARAM_CHANNELS,
656a5f22156STakashi Iwai 					   &hw_constraints_channels);
6571da177e4SLinus Torvalds 	}
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_capture_open(struct snd_pcm_substream *substream)
6641da177e4SLinus Torvalds {
665a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
666a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6671da177e4SLinus Torvalds 	int err;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	chip->capture_substream = substream;
6701da177e4SLinus Torvalds 	runtime->hw = snd_fm801_capture;
671a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
672a5f22156STakashi Iwai 				   &hw_constraints_rates);
6731da177e4SLinus Torvalds 	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
6741da177e4SLinus Torvalds 		return err;
6751da177e4SLinus Torvalds 	return 0;
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
678a5f22156STakashi Iwai static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
6791da177e4SLinus Torvalds {
680a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	chip->playback_substream = NULL;
6831da177e4SLinus Torvalds 	return 0;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
686a5f22156STakashi Iwai static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
6871da177e4SLinus Torvalds {
688a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	chip->capture_substream = NULL;
6911da177e4SLinus Torvalds 	return 0;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6946769e988SJulia Lawall static const struct snd_pcm_ops snd_fm801_playback_ops = {
6951da177e4SLinus Torvalds 	.open =		snd_fm801_playback_open,
6961da177e4SLinus Torvalds 	.close =	snd_fm801_playback_close,
6971da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
6981da177e4SLinus Torvalds 	.hw_params =	snd_fm801_hw_params,
6991da177e4SLinus Torvalds 	.hw_free =	snd_fm801_hw_free,
7001da177e4SLinus Torvalds 	.prepare =	snd_fm801_playback_prepare,
7011da177e4SLinus Torvalds 	.trigger =	snd_fm801_playback_trigger,
7021da177e4SLinus Torvalds 	.pointer =	snd_fm801_playback_pointer,
7031da177e4SLinus Torvalds };
7041da177e4SLinus Torvalds 
7056769e988SJulia Lawall static const struct snd_pcm_ops snd_fm801_capture_ops = {
7061da177e4SLinus Torvalds 	.open =		snd_fm801_capture_open,
7071da177e4SLinus Torvalds 	.close =	snd_fm801_capture_close,
7081da177e4SLinus Torvalds 	.ioctl =	snd_pcm_lib_ioctl,
7091da177e4SLinus Torvalds 	.hw_params =	snd_fm801_hw_params,
7101da177e4SLinus Torvalds 	.hw_free =	snd_fm801_hw_free,
7111da177e4SLinus Torvalds 	.prepare =	snd_fm801_capture_prepare,
7121da177e4SLinus Torvalds 	.trigger =	snd_fm801_capture_trigger,
7131da177e4SLinus Torvalds 	.pointer =	snd_fm801_capture_pointer,
7141da177e4SLinus Torvalds };
7151da177e4SLinus Torvalds 
716483337f9SLars-Peter Clausen static int snd_fm801_pcm(struct fm801 *chip, int device)
7171da177e4SLinus Torvalds {
718d3d33aabSAndy Shevchenko 	struct pci_dev *pdev = to_pci_dev(chip->dev);
719a5f22156STakashi Iwai 	struct snd_pcm *pcm;
7201da177e4SLinus Torvalds 	int err;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm)) < 0)
7231da177e4SLinus Torvalds 		return err;
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
7261da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	pcm->private_data = chip;
7291da177e4SLinus Torvalds 	pcm->info_flags = 0;
7301da177e4SLinus Torvalds 	strcpy(pcm->name, "FM801");
7311da177e4SLinus Torvalds 	chip->pcm = pcm;
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
734d3d33aabSAndy Shevchenko 					      snd_dma_pci_data(pdev),
7351da177e4SLinus Torvalds 					      chip->multichannel ? 128*1024 : 64*1024, 128*1024);
7361da177e4SLinus Torvalds 
737483337f9SLars-Peter Clausen 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
738e36e3b86STakashi Iwai 				     snd_pcm_alt_chmaps,
739e36e3b86STakashi Iwai 				     chip->multichannel ? 6 : 2, 0,
740e36e3b86STakashi Iwai 				     NULL);
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds /*
7441da177e4SLinus Torvalds  *  TEA5757 radio
7451da177e4SLinus Torvalds  */
7461da177e4SLinus Torvalds 
747fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
7481da177e4SLinus Torvalds 
749938a1566SOndrej Zary /* GPIO to TEA575x maps */
750938a1566SOndrej Zary struct snd_fm801_tea575x_gpio {
751938a1566SOndrej Zary 	u8 data, clk, wren, most;
752d7ba858aSOndrej Zary 	char *name;
753938a1566SOndrej Zary };
7541da177e4SLinus Torvalds 
755938a1566SOndrej Zary static struct snd_fm801_tea575x_gpio snd_fm801_tea575x_gpios[] = {
756d7ba858aSOndrej Zary 	{ .data = 1, .clk = 3, .wren = 2, .most = 0, .name = "SF256-PCS" },
757d7ba858aSOndrej Zary 	{ .data = 1, .clk = 0, .wren = 2, .most = 3, .name = "SF256-PCP" },
758d7ba858aSOndrej Zary 	{ .data = 2, .clk = 0, .wren = 1, .most = 3, .name = "SF64-PCR" },
759938a1566SOndrej Zary };
760938a1566SOndrej Zary 
7618e699d2cSTakashi Iwai #define get_tea575x_gpio(chip) \
7628e699d2cSTakashi Iwai 	(&snd_fm801_tea575x_gpios[((chip)->tea575x_tuner & TUNER_TYPE_MASK) - 1])
7638e699d2cSTakashi Iwai 
764938a1566SOndrej Zary static void snd_fm801_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
7651da177e4SLinus Torvalds {
766a5f22156STakashi Iwai 	struct fm801 *chip = tea->private_data;
767215dacc2SAndy Shevchenko 	unsigned short reg = fm801_readw(chip, GPIO_CTRL);
7688e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
7691da177e4SLinus Torvalds 
770938a1566SOndrej Zary 	reg &= ~(FM801_GPIO_GP(gpio.data) |
771938a1566SOndrej Zary 		 FM801_GPIO_GP(gpio.clk) |
772938a1566SOndrej Zary 		 FM801_GPIO_GP(gpio.wren));
773938a1566SOndrej Zary 
774938a1566SOndrej Zary 	reg |= (pins & TEA575X_DATA) ? FM801_GPIO_GP(gpio.data) : 0;
775938a1566SOndrej Zary 	reg |= (pins & TEA575X_CLK)  ? FM801_GPIO_GP(gpio.clk) : 0;
776938a1566SOndrej Zary 	/* WRITE_ENABLE is inverted */
777938a1566SOndrej Zary 	reg |= (pins & TEA575X_WREN) ? 0 : FM801_GPIO_GP(gpio.wren);
778938a1566SOndrej Zary 
779215dacc2SAndy Shevchenko 	fm801_writew(chip, GPIO_CTRL, reg);
780938a1566SOndrej Zary }
781938a1566SOndrej Zary 
782938a1566SOndrej Zary static u8 snd_fm801_tea575x_get_pins(struct snd_tea575x *tea)
783938a1566SOndrej Zary {
784938a1566SOndrej Zary 	struct fm801 *chip = tea->private_data;
785215dacc2SAndy Shevchenko 	unsigned short reg = fm801_readw(chip, GPIO_CTRL);
7868e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
787effded75SDan Carpenter 	u8 ret;
788938a1566SOndrej Zary 
789effded75SDan Carpenter 	ret = 0;
790effded75SDan Carpenter 	if (reg & FM801_GPIO_GP(gpio.data))
791effded75SDan Carpenter 		ret |= TEA575X_DATA;
792effded75SDan Carpenter 	if (reg & FM801_GPIO_GP(gpio.most))
793effded75SDan Carpenter 		ret |= TEA575X_MOST;
794effded75SDan Carpenter 	return ret;
795938a1566SOndrej Zary }
796938a1566SOndrej Zary 
797938a1566SOndrej Zary static void snd_fm801_tea575x_set_direction(struct snd_tea575x *tea, bool output)
798938a1566SOndrej Zary {
799938a1566SOndrej Zary 	struct fm801 *chip = tea->private_data;
800215dacc2SAndy Shevchenko 	unsigned short reg = fm801_readw(chip, GPIO_CTRL);
8018e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
802938a1566SOndrej Zary 
8031da177e4SLinus Torvalds 	/* use GPIO lines and set write enable bit */
804938a1566SOndrej Zary 	reg |= FM801_GPIO_GS(gpio.data) |
805938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.wren) |
806938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.clk) |
807938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.most);
808938a1566SOndrej Zary 	if (output) {
8091da177e4SLinus Torvalds 		/* all of lines are in the write direction */
8101da177e4SLinus Torvalds 		/* clear data and clock lines */
811938a1566SOndrej Zary 		reg &= ~(FM801_GPIO_GD(gpio.data) |
812938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.wren) |
813938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.clk) |
814938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.data) |
815938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.clk) |
816938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.wren));
817938a1566SOndrej Zary 	} else {
8181da177e4SLinus Torvalds 		/* use GPIO lines, set data direction to input */
819938a1566SOndrej Zary 		reg |= FM801_GPIO_GD(gpio.data) |
820938a1566SOndrej Zary 		       FM801_GPIO_GD(gpio.most) |
821938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.data) |
822938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.most) |
823938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.wren);
8241da177e4SLinus Torvalds 		/* all of lines are in the write direction, except data */
8251da177e4SLinus Torvalds 		/* clear data, write enable and clock lines */
826938a1566SOndrej Zary 		reg &= ~(FM801_GPIO_GD(gpio.wren) |
827938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.clk) |
828938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.clk));
8291da177e4SLinus Torvalds 	}
8301da177e4SLinus Torvalds 
831215dacc2SAndy Shevchenko 	fm801_writew(chip, GPIO_CTRL, reg);
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds 
83422dbec26SJulia Lawall static const struct snd_tea575x_ops snd_fm801_tea_ops = {
835938a1566SOndrej Zary 	.set_pins = snd_fm801_tea575x_set_pins,
836938a1566SOndrej Zary 	.get_pins = snd_fm801_tea575x_get_pins,
837938a1566SOndrej Zary 	.set_direction = snd_fm801_tea575x_set_direction,
8381da177e4SLinus Torvalds };
8391da177e4SLinus Torvalds #endif
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds /*
8421da177e4SLinus Torvalds  *  Mixer routines
8431da177e4SLinus Torvalds  */
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds #define FM801_SINGLE(xname, reg, shift, mask, invert) \
8461da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
8471da177e4SLinus Torvalds   .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
8481da177e4SLinus Torvalds   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
8491da177e4SLinus Torvalds 
850a5f22156STakashi Iwai static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
851a5f22156STakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
8521da177e4SLinus Torvalds {
8531da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
8561da177e4SLinus Torvalds 	uinfo->count = 1;
8571da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
8581da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
8591da177e4SLinus Torvalds 	return 0;
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
862a5f22156STakashi Iwai static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
863a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
8641da177e4SLinus Torvalds {
865a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
8661da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
8671da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
8681da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8691da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
8704b5c15f7SAndy Shevchenko 	long *value = ucontrol->value.integer.value;
8711da177e4SLinus Torvalds 
8724b5c15f7SAndy Shevchenko 	value[0] = (fm801_ioread16(chip, reg) >> shift) & mask;
8731da177e4SLinus Torvalds 	if (invert)
8744b5c15f7SAndy Shevchenko 		value[0] = mask - value[0];
8751da177e4SLinus Torvalds 	return 0;
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
878a5f22156STakashi Iwai static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
879a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
8801da177e4SLinus Torvalds {
881a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
8821da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
8831da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
8841da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8851da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
8861da177e4SLinus Torvalds 	unsigned short val;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
8891da177e4SLinus Torvalds 	if (invert)
8901da177e4SLinus Torvalds 		val = mask - val;
8911da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds #define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
8951da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
8961da177e4SLinus Torvalds   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
8971da177e4SLinus Torvalds   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
898666c70ffSTakashi Iwai #define FM801_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
899666c70ffSTakashi Iwai { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
900666c70ffSTakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
901666c70ffSTakashi Iwai   .name = xname, .info = snd_fm801_info_double, \
902666c70ffSTakashi Iwai   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
903666c70ffSTakashi Iwai   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
904666c70ffSTakashi Iwai   .tlv = { .p = (xtlv) } }
9051da177e4SLinus Torvalds 
906a5f22156STakashi Iwai static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
907a5f22156STakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
9121da177e4SLinus Torvalds 	uinfo->count = 2;
9131da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
9141da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
9151da177e4SLinus Torvalds 	return 0;
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
918a5f22156STakashi Iwai static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
919a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
9201da177e4SLinus Torvalds {
921a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9221da177e4SLinus Torvalds         int reg = kcontrol->private_value & 0xff;
9231da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
9241da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
9251da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
9261da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
9274b5c15f7SAndy Shevchenko 	long *value = ucontrol->value.integer.value;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9304b5c15f7SAndy Shevchenko 	value[0] = (fm801_ioread16(chip, reg) >> shift_left) & mask;
9314b5c15f7SAndy Shevchenko 	value[1] = (fm801_ioread16(chip, reg) >> shift_right) & mask;
9321da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9331da177e4SLinus Torvalds 	if (invert) {
9344b5c15f7SAndy Shevchenko 		value[0] = mask - value[0];
9354b5c15f7SAndy Shevchenko 		value[1] = mask - value[1];
9361da177e4SLinus Torvalds 	}
9371da177e4SLinus Torvalds 	return 0;
9381da177e4SLinus Torvalds }
9391da177e4SLinus Torvalds 
940a5f22156STakashi Iwai static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
941a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
9421da177e4SLinus Torvalds {
943a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9441da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
9451da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
9461da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
9471da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
9481da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
9491da177e4SLinus Torvalds 	unsigned short val1, val2;
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
9521da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
9531da177e4SLinus Torvalds 	if (invert) {
9541da177e4SLinus Torvalds 		val1 = mask - val1;
9551da177e4SLinus Torvalds 		val2 = mask - val2;
9561da177e4SLinus Torvalds 	}
9571da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg,
9581da177e4SLinus Torvalds 				     (mask << shift_left) | (mask << shift_right),
9591da177e4SLinus Torvalds 				     (val1 << shift_left ) | (val2 << shift_right));
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds 
962a5f22156STakashi Iwai static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
963a5f22156STakashi Iwai 			      struct snd_ctl_elem_info *uinfo)
9641da177e4SLinus Torvalds {
965ca776a28STakashi Iwai 	static const char * const texts[5] = {
9661da177e4SLinus Torvalds 		"AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
9671da177e4SLinus Torvalds 	};
9681da177e4SLinus Torvalds 
969ca776a28STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 5, texts);
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
972a5f22156STakashi Iwai static int snd_fm801_get_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 
978215dacc2SAndy Shevchenko 	val = fm801_readw(chip, REC_SRC) & 7;
9791da177e4SLinus Torvalds 	if (val > 4)
9801da177e4SLinus Torvalds 		val = 4;
9811da177e4SLinus Torvalds         ucontrol->value.enumerated.item[0] = val;
9821da177e4SLinus Torvalds         return 0;
9831da177e4SLinus Torvalds }
9841da177e4SLinus Torvalds 
985a5f22156STakashi Iwai static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
986a5f22156STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
9871da177e4SLinus Torvalds {
988a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9891da177e4SLinus Torvalds         unsigned short val;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds         if ((val = ucontrol->value.enumerated.item[0]) > 4)
9921da177e4SLinus Torvalds                 return -EINVAL;
9931da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
9960cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
997666c70ffSTakashi Iwai 
998a5f22156STakashi Iwai #define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
9991da177e4SLinus Torvalds 
1000e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_fm801_controls[] = {
1001666c70ffSTakashi Iwai FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
1002666c70ffSTakashi Iwai 		 db_scale_dsp),
10031da177e4SLinus Torvalds FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
1004666c70ffSTakashi Iwai FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
1005666c70ffSTakashi Iwai 		 db_scale_dsp),
10061da177e4SLinus Torvalds FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
1007666c70ffSTakashi Iwai FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
1008666c70ffSTakashi Iwai 		 db_scale_dsp),
10091da177e4SLinus Torvalds FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
10101da177e4SLinus Torvalds {
10111da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
10121da177e4SLinus Torvalds 	.name = "Digital Capture Source",
10131da177e4SLinus Torvalds 	.info = snd_fm801_info_mux,
10141da177e4SLinus Torvalds 	.get = snd_fm801_get_mux,
10151da177e4SLinus Torvalds 	.put = snd_fm801_put_mux,
10161da177e4SLinus Torvalds }
10171da177e4SLinus Torvalds };
10181da177e4SLinus Torvalds 
1019a5f22156STakashi Iwai #define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
10201da177e4SLinus Torvalds 
1021e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_fm801_controls_multi[] = {
10221da177e4SLinus Torvalds FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
10231da177e4SLinus Torvalds FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
102410e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
102510e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
102610e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
102710e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
10281da177e4SLinus Torvalds };
10291da177e4SLinus Torvalds 
1030a5f22156STakashi Iwai static void snd_fm801_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
10311da177e4SLinus Torvalds {
1032a5f22156STakashi Iwai 	struct fm801 *chip = bus->private_data;
10331da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
10341da177e4SLinus Torvalds }
10351da177e4SLinus Torvalds 
1036a5f22156STakashi Iwai static void snd_fm801_mixer_free_ac97(struct snd_ac97 *ac97)
10371da177e4SLinus Torvalds {
1038a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
10391da177e4SLinus Torvalds 	if (ac97->num == 0) {
10401da177e4SLinus Torvalds 		chip->ac97 = NULL;
10411da177e4SLinus Torvalds 	} else {
10421da177e4SLinus Torvalds 		chip->ac97_sec = NULL;
10431da177e4SLinus Torvalds 	}
10441da177e4SLinus Torvalds }
10451da177e4SLinus Torvalds 
1046e23e7a14SBill Pemberton static int snd_fm801_mixer(struct fm801 *chip)
10471da177e4SLinus Torvalds {
1048a5f22156STakashi Iwai 	struct snd_ac97_template ac97;
10491da177e4SLinus Torvalds 	unsigned int i;
10501da177e4SLinus Torvalds 	int err;
1051a5f22156STakashi Iwai 	static struct snd_ac97_bus_ops ops = {
10521da177e4SLinus Torvalds 		.write = snd_fm801_codec_write,
10531da177e4SLinus Torvalds 		.read = snd_fm801_codec_read,
10541da177e4SLinus Torvalds 	};
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
10571da177e4SLinus Torvalds 		return err;
10581da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_fm801_mixer_free_ac97_bus;
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
10611da177e4SLinus Torvalds 	ac97.private_data = chip;
10621da177e4SLinus Torvalds 	ac97.private_free = snd_fm801_mixer_free_ac97;
10631da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
10641da177e4SLinus Torvalds 		return err;
10651da177e4SLinus Torvalds 	if (chip->secondary) {
10661da177e4SLinus Torvalds 		ac97.num = 1;
10671da177e4SLinus Torvalds 		ac97.addr = chip->secondary_addr;
10681da177e4SLinus Torvalds 		if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec)) < 0)
10691da177e4SLinus Torvalds 			return err;
10701da177e4SLinus Torvalds 	}
10711da177e4SLinus Torvalds 	for (i = 0; i < FM801_CONTROLS; i++)
10721da177e4SLinus Torvalds 		snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls[i], chip));
10731da177e4SLinus Torvalds 	if (chip->multichannel) {
10741da177e4SLinus Torvalds 		for (i = 0; i < FM801_CONTROLS_MULTI; i++)
10751da177e4SLinus Torvalds 			snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
10761da177e4SLinus Torvalds 	}
10771da177e4SLinus Torvalds 	return 0;
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds /*
10811da177e4SLinus Torvalds  *  initialization routines
10821da177e4SLinus Torvalds  */
10831da177e4SLinus Torvalds 
1084b1e9ed26STakashi Iwai static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
1085b1e9ed26STakashi Iwai 			  unsigned short reg, unsigned long waits)
1086b1e9ed26STakashi Iwai {
1087b1e9ed26STakashi Iwai 	unsigned long timeout = jiffies + waits;
1088b1e9ed26STakashi Iwai 
1089215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_CMD,
1090215dacc2SAndy Shevchenko 		     reg | (codec_id << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
1091b1e9ed26STakashi Iwai 	udelay(5);
1092b1e9ed26STakashi Iwai 	do {
1093215dacc2SAndy Shevchenko 		if ((fm801_readw(chip, AC97_CMD) &
1094215dacc2SAndy Shevchenko 		     (FM801_AC97_VALID | FM801_AC97_BUSY)) == FM801_AC97_VALID)
1095b1e9ed26STakashi Iwai 			return 0;
1096b1e9ed26STakashi Iwai 		schedule_timeout_uninterruptible(1);
1097b1e9ed26STakashi Iwai 	} while (time_after(timeout, jiffies));
1098b1e9ed26STakashi Iwai 	return -EIO;
1099b1e9ed26STakashi Iwai }
1100b1e9ed26STakashi Iwai 
1101b56fa687SAndy Shevchenko static int reset_codec(struct fm801 *chip)
1102b1e9ed26STakashi Iwai {
1103b1e9ed26STakashi Iwai 	/* codec cold reset + AC'97 warm reset */
1104215dacc2SAndy Shevchenko 	fm801_writew(chip, CODEC_CTRL, (1 << 5) | (1 << 6));
1105215dacc2SAndy Shevchenko 	fm801_readw(chip, CODEC_CTRL); /* flush posting data */
1106b1e9ed26STakashi Iwai 	udelay(100);
1107215dacc2SAndy Shevchenko 	fm801_writew(chip, CODEC_CTRL, 0);
1108b1e9ed26STakashi Iwai 
1109b56fa687SAndy Shevchenko 	return wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750));
1110b1e9ed26STakashi Iwai }
1111b1e9ed26STakashi Iwai 
1112b56fa687SAndy Shevchenko static void snd_fm801_chip_multichannel_init(struct fm801 *chip)
1113b56fa687SAndy Shevchenko {
1114b56fa687SAndy Shevchenko 	unsigned short cmdw;
1115b56fa687SAndy Shevchenko 
1116b1e9ed26STakashi Iwai 	if (chip->multichannel) {
1117b1e9ed26STakashi Iwai 		if (chip->secondary_addr) {
1118b1e9ed26STakashi Iwai 			wait_for_codec(chip, chip->secondary_addr,
1119b1e9ed26STakashi Iwai 				       AC97_VENDOR_ID1, msecs_to_jiffies(50));
1120b1e9ed26STakashi Iwai 		} else {
1121b1e9ed26STakashi Iwai 			/* my card has the secondary codec */
1122b1e9ed26STakashi Iwai 			/* at address #3, so the loop is inverted */
112358e4334eSHarvey Harrison 			int i;
112458e4334eSHarvey Harrison 			for (i = 3; i > 0; i--) {
112558e4334eSHarvey Harrison 				if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
1126b1e9ed26STakashi Iwai 						     msecs_to_jiffies(50))) {
1127215dacc2SAndy Shevchenko 					cmdw = fm801_readw(chip, AC97_DATA);
1128b1e9ed26STakashi Iwai 					if (cmdw != 0xffff && cmdw != 0) {
1129b1e9ed26STakashi Iwai 						chip->secondary = 1;
113058e4334eSHarvey Harrison 						chip->secondary_addr = i;
1131b1e9ed26STakashi Iwai 						break;
1132b1e9ed26STakashi Iwai 					}
1133b1e9ed26STakashi Iwai 				}
1134b1e9ed26STakashi Iwai 			}
1135b1e9ed26STakashi Iwai 		}
1136b1e9ed26STakashi Iwai 
1137b1e9ed26STakashi Iwai 		/* the recovery phase, it seems that probing for non-existing codec might */
1138b1e9ed26STakashi Iwai 		/* cause timeout problems */
1139b1e9ed26STakashi Iwai 		wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
1140b1e9ed26STakashi Iwai 	}
1141b56fa687SAndy Shevchenko }
1142b1e9ed26STakashi Iwai 
1143b56fa687SAndy Shevchenko static void snd_fm801_chip_init(struct fm801 *chip)
1144b56fa687SAndy Shevchenko {
1145b56fa687SAndy Shevchenko 	unsigned short cmdw;
11466bbe13ecSJaroslav Kysela 
1147b1e9ed26STakashi Iwai 	/* init volume */
1148215dacc2SAndy Shevchenko 	fm801_writew(chip, PCM_VOL, 0x0808);
1149215dacc2SAndy Shevchenko 	fm801_writew(chip, FM_VOL, 0x9f1f);
1150215dacc2SAndy Shevchenko 	fm801_writew(chip, I2S_VOL, 0x8808);
1151b1e9ed26STakashi Iwai 
1152b1e9ed26STakashi Iwai 	/* I2S control - I2S mode */
1153215dacc2SAndy Shevchenko 	fm801_writew(chip, I2S_MODE, 0x0003);
1154b1e9ed26STakashi Iwai 
11556bbe13ecSJaroslav Kysela 	/* interrupt setup */
1156215dacc2SAndy Shevchenko 	cmdw = fm801_readw(chip, IRQ_MASK);
11576bbe13ecSJaroslav Kysela 	if (chip->irq < 0)
11586bbe13ecSJaroslav Kysela 		cmdw |= 0x00c3;		/* mask everything, no PCM nor MPU */
11596bbe13ecSJaroslav Kysela 	else
11606bbe13ecSJaroslav Kysela 		cmdw &= ~0x0083;	/* unmask MPU, PLAYBACK & CAPTURE */
1161215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_MASK, cmdw);
1162b1e9ed26STakashi Iwai 
1163b1e9ed26STakashi Iwai 	/* interrupt clear */
1164215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_STATUS,
1165215dacc2SAndy Shevchenko 		     FM801_IRQ_PLAYBACK | FM801_IRQ_CAPTURE | FM801_IRQ_MPU);
1166b1e9ed26STakashi Iwai }
1167b1e9ed26STakashi Iwai 
1168a5f22156STakashi Iwai static int snd_fm801_free(struct fm801 *chip)
11691da177e4SLinus Torvalds {
11701da177e4SLinus Torvalds 	unsigned short cmdw;
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	if (chip->irq < 0)
11731da177e4SLinus Torvalds 		goto __end_hw;
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	/* interrupt setup - mask everything */
1176215dacc2SAndy Shevchenko 	cmdw = fm801_readw(chip, IRQ_MASK);
11771da177e4SLinus Torvalds 	cmdw |= 0x00c3;
1178215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_MASK, cmdw);
11791da177e4SLinus Torvalds 
1180d3d33aabSAndy Shevchenko 	devm_free_irq(chip->dev, chip->irq, chip);
1181e97e98c6SAndy Shevchenko 
11821da177e4SLinus Torvalds       __end_hw:
1183fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1184d4ecc83bSHans Verkuil 	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
11851da177e4SLinus Torvalds 		snd_tea575x_exit(&chip->tea);
1186d4ecc83bSHans Verkuil 		v4l2_device_unregister(&chip->v4l2_dev);
1187d4ecc83bSHans Verkuil 	}
11881da177e4SLinus Torvalds #endif
11891da177e4SLinus Torvalds 	return 0;
11901da177e4SLinus Torvalds }
11911da177e4SLinus Torvalds 
1192a5f22156STakashi Iwai static int snd_fm801_dev_free(struct snd_device *device)
11931da177e4SLinus Torvalds {
1194a5f22156STakashi Iwai 	struct fm801 *chip = device->device_data;
11951da177e4SLinus Torvalds 	return snd_fm801_free(chip);
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds 
1198e23e7a14SBill Pemberton static int snd_fm801_create(struct snd_card *card,
11991da177e4SLinus Torvalds 			    struct pci_dev *pci,
12001da177e4SLinus Torvalds 			    int tea575x_tuner,
1201d4ecc83bSHans Verkuil 			    int radio_nr,
1202a5f22156STakashi Iwai 			    struct fm801 **rchip)
12031da177e4SLinus Torvalds {
1204a5f22156STakashi Iwai 	struct fm801 *chip;
12051da177e4SLinus Torvalds 	int err;
1206a5f22156STakashi Iwai 	static struct snd_device_ops ops = {
12071da177e4SLinus Torvalds 		.dev_free =	snd_fm801_dev_free,
12081da177e4SLinus Torvalds 	};
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	*rchip = NULL;
12115618955cSAndy Shevchenko 	if ((err = pcim_enable_device(pci)) < 0)
12121da177e4SLinus Torvalds 		return err;
12135618955cSAndy Shevchenko 	chip = devm_kzalloc(&pci->dev, sizeof(*chip), GFP_KERNEL);
12145618955cSAndy Shevchenko 	if (chip == NULL)
12151da177e4SLinus Torvalds 		return -ENOMEM;
12161da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
12171da177e4SLinus Torvalds 	chip->card = card;
1218d3d33aabSAndy Shevchenko 	chip->dev = &pci->dev;
12191da177e4SLinus Torvalds 	chip->irq = -1;
12206bbe13ecSJaroslav Kysela 	chip->tea575x_tuner = tea575x_tuner;
12215618955cSAndy Shevchenko 	if ((err = pci_request_regions(pci, "FM801")) < 0)
12221da177e4SLinus Torvalds 		return err;
12231da177e4SLinus Torvalds 	chip->port = pci_resource_start(pci, 0);
1224b56fa687SAndy Shevchenko 
1225b56fa687SAndy Shevchenko 	if (pci->revision >= 0xb1)	/* FM801-AU */
1226b56fa687SAndy Shevchenko 		chip->multichannel = 1;
1227b56fa687SAndy Shevchenko 
1228b56fa687SAndy Shevchenko 	if (!(chip->tea575x_tuner & TUNER_ONLY)) {
1229b56fa687SAndy Shevchenko 		if (reset_codec(chip) < 0) {
1230b56fa687SAndy Shevchenko 			dev_info(chip->card->dev,
1231b56fa687SAndy Shevchenko 				 "Primary AC'97 codec not found, assume SF64-PCR (tuner-only)\n");
1232b56fa687SAndy Shevchenko 			chip->tea575x_tuner = 3 | TUNER_ONLY;
1233b56fa687SAndy Shevchenko 		} else {
1234b56fa687SAndy Shevchenko 			snd_fm801_chip_multichannel_init(chip);
1235b56fa687SAndy Shevchenko 		}
1236b56fa687SAndy Shevchenko 	}
1237b56fa687SAndy Shevchenko 
1238b56fa687SAndy Shevchenko 	snd_fm801_chip_init(chip);
1239b56fa687SAndy Shevchenko 
1240b56fa687SAndy Shevchenko 	if ((chip->tea575x_tuner & TUNER_ONLY) == 0) {
12415618955cSAndy Shevchenko 		if (devm_request_irq(&pci->dev, pci->irq, snd_fm801_interrupt,
12425618955cSAndy Shevchenko 				IRQF_SHARED, KBUILD_MODNAME, chip)) {
12435618955cSAndy Shevchenko 			dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
12441da177e4SLinus Torvalds 			snd_fm801_free(chip);
12451da177e4SLinus Torvalds 			return -EBUSY;
12461da177e4SLinus Torvalds 		}
12471da177e4SLinus Torvalds 		chip->irq = pci->irq;
12481da177e4SLinus Torvalds 		pci_set_master(pci);
12496bbe13ecSJaroslav Kysela 	}
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
12521da177e4SLinus Torvalds 		snd_fm801_free(chip);
12531da177e4SLinus Torvalds 		return err;
12541da177e4SLinus Torvalds 	}
12551da177e4SLinus Torvalds 
1256fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1257d4ecc83bSHans Verkuil 	err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
1258d4ecc83bSHans Verkuil 	if (err < 0) {
1259d4ecc83bSHans Verkuil 		snd_fm801_free(chip);
1260d4ecc83bSHans Verkuil 		return err;
1261d4ecc83bSHans Verkuil 	}
1262d4ecc83bSHans Verkuil 	chip->tea.v4l2_dev = &chip->v4l2_dev;
1263d4ecc83bSHans Verkuil 	chip->tea.radio_nr = radio_nr;
12641da177e4SLinus Torvalds 	chip->tea.private_data = chip;
1265938a1566SOndrej Zary 	chip->tea.ops = &snd_fm801_tea_ops;
126610ca7201SOndrej Zary 	sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
1267b56fa687SAndy Shevchenko 	if ((chip->tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
1268b56fa687SAndy Shevchenko 	    (chip->tea575x_tuner & TUNER_TYPE_MASK) < 4) {
12695daf53a6SHans de Goede 		if (snd_tea575x_init(&chip->tea, THIS_MODULE)) {
12709c7f9abfSTakashi Iwai 			dev_err(card->dev, "TEA575x radio not found\n");
1271d4ecc83bSHans Verkuil 			snd_fm801_free(chip);
127296760015SDan Carpenter 			return -ENODEV;
127396760015SDan Carpenter 		}
1274b56fa687SAndy Shevchenko 	} else if ((chip->tea575x_tuner & TUNER_TYPE_MASK) == 0) {
1275b56fa687SAndy Shevchenko 		unsigned int tuner_only = chip->tea575x_tuner & TUNER_ONLY;
1276dbec6719SAndy Shevchenko 
1277d7ba858aSOndrej Zary 		/* autodetect tuner connection */
1278d7ba858aSOndrej Zary 		for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
1279d7ba858aSOndrej Zary 			chip->tea575x_tuner = tea575x_tuner;
12805daf53a6SHans de Goede 			if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
12819c7f9abfSTakashi Iwai 				dev_info(card->dev,
12829c7f9abfSTakashi Iwai 					 "detected TEA575x radio type %s\n",
12838e699d2cSTakashi Iwai 					   get_tea575x_gpio(chip)->name);
1284d7ba858aSOndrej Zary 				break;
1285d7ba858aSOndrej Zary 			}
12861da177e4SLinus Torvalds 		}
128796760015SDan Carpenter 		if (tea575x_tuner == 4) {
12889c7f9abfSTakashi Iwai 			dev_err(card->dev, "TEA575x radio not found\n");
1289c37279b9SBen Hutchings 			chip->tea575x_tuner = TUNER_DISABLED;
129096760015SDan Carpenter 		}
1291dbec6719SAndy Shevchenko 
1292dbec6719SAndy Shevchenko 		chip->tea575x_tuner |= tuner_only;
129396760015SDan Carpenter 	}
1294c37279b9SBen Hutchings 	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
12958e699d2cSTakashi Iwai 		strlcpy(chip->tea.card, get_tea575x_gpio(chip)->name,
1296c37279b9SBen Hutchings 			sizeof(chip->tea.card));
1297c37279b9SBen Hutchings 	}
12981da177e4SLinus Torvalds #endif
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	*rchip = chip;
13011da177e4SLinus Torvalds 	return 0;
13021da177e4SLinus Torvalds }
13031da177e4SLinus Torvalds 
1304e23e7a14SBill Pemberton static int snd_card_fm801_probe(struct pci_dev *pci,
13051da177e4SLinus Torvalds 				const struct pci_device_id *pci_id)
13061da177e4SLinus Torvalds {
13071da177e4SLinus Torvalds 	static int dev;
1308a5f22156STakashi Iwai 	struct snd_card *card;
1309a5f22156STakashi Iwai 	struct fm801 *chip;
1310a5f22156STakashi Iwai 	struct snd_opl3 *opl3;
13111da177e4SLinus Torvalds 	int err;
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds         if (dev >= SNDRV_CARDS)
13141da177e4SLinus Torvalds                 return -ENODEV;
13151da177e4SLinus Torvalds 	if (!enable[dev]) {
13161da177e4SLinus Torvalds 		dev++;
13171da177e4SLinus Torvalds 		return -ENOENT;
13181da177e4SLinus Torvalds 	}
13191da177e4SLinus Torvalds 
132060c5772bSTakashi Iwai 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
132160c5772bSTakashi Iwai 			   0, &card);
1322e58de7baSTakashi Iwai 	if (err < 0)
1323e58de7baSTakashi Iwai 		return err;
1324d4ecc83bSHans Verkuil 	if ((err = snd_fm801_create(card, pci, tea575x_tuner[dev], radio_nr[dev], &chip)) < 0) {
13251da177e4SLinus Torvalds 		snd_card_free(card);
13261da177e4SLinus Torvalds 		return err;
13271da177e4SLinus Torvalds 	}
1328b1e9ed26STakashi Iwai 	card->private_data = chip;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 	strcpy(card->driver, "FM801");
13311da177e4SLinus Torvalds 	strcpy(card->shortname, "ForteMedia FM801-");
13321da177e4SLinus Torvalds 	strcat(card->shortname, chip->multichannel ? "AU" : "AS");
13331da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %i",
13341da177e4SLinus Torvalds 		card->shortname, chip->port, chip->irq);
13351da177e4SLinus Torvalds 
1336fb716c0bSOndrej Zary 	if (chip->tea575x_tuner & TUNER_ONLY)
1337e0a5d82aSAndy Shevchenko 		goto __fm801_tuner_only;
1338e0a5d82aSAndy Shevchenko 
1339483337f9SLars-Peter Clausen 	if ((err = snd_fm801_pcm(chip, 0)) < 0) {
13401da177e4SLinus Torvalds 		snd_card_free(card);
13411da177e4SLinus Torvalds 		return err;
13421da177e4SLinus Torvalds 	}
13431da177e4SLinus Torvalds 	if ((err = snd_fm801_mixer(chip)) < 0) {
13441da177e4SLinus Torvalds 		snd_card_free(card);
13451da177e4SLinus Torvalds 		return err;
13461da177e4SLinus Torvalds 	}
13471da177e4SLinus Torvalds 	if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1348215dacc2SAndy Shevchenko 				       chip->port + FM801_MPU401_DATA,
1349dba8b469SClemens Ladisch 				       MPU401_INFO_INTEGRATED |
1350dba8b469SClemens Ladisch 				       MPU401_INFO_IRQ_HOOK,
1351dba8b469SClemens Ladisch 				       -1, &chip->rmidi)) < 0) {
13521da177e4SLinus Torvalds 		snd_card_free(card);
13531da177e4SLinus Torvalds 		return err;
13541da177e4SLinus Torvalds 	}
1355215dacc2SAndy Shevchenko 	if ((err = snd_opl3_create(card, chip->port + FM801_OPL3_BANK0,
1356215dacc2SAndy Shevchenko 				   chip->port + FM801_OPL3_BANK1,
13571da177e4SLinus Torvalds 				   OPL3_HW_OPL3_FM801, 1, &opl3)) < 0) {
13581da177e4SLinus Torvalds 		snd_card_free(card);
13591da177e4SLinus Torvalds 		return err;
13601da177e4SLinus Torvalds 	}
13611da177e4SLinus Torvalds 	if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
13621da177e4SLinus Torvalds 		snd_card_free(card);
13631da177e4SLinus Torvalds 		return err;
13641da177e4SLinus Torvalds 	}
13651da177e4SLinus Torvalds 
1366e0a5d82aSAndy Shevchenko       __fm801_tuner_only:
13671da177e4SLinus Torvalds 	if ((err = snd_card_register(card)) < 0) {
13681da177e4SLinus Torvalds 		snd_card_free(card);
13691da177e4SLinus Torvalds 		return err;
13701da177e4SLinus Torvalds 	}
13711da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
13721da177e4SLinus Torvalds 	dev++;
13731da177e4SLinus Torvalds 	return 0;
13741da177e4SLinus Torvalds }
13751da177e4SLinus Torvalds 
1376e23e7a14SBill Pemberton static void snd_card_fm801_remove(struct pci_dev *pci)
13771da177e4SLinus Torvalds {
13781da177e4SLinus Torvalds 	snd_card_free(pci_get_drvdata(pci));
13791da177e4SLinus Torvalds }
13801da177e4SLinus Torvalds 
1381c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
1382b1e9ed26STakashi Iwai static unsigned char saved_regs[] = {
1383b1e9ed26STakashi Iwai 	FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
1384b1e9ed26STakashi Iwai 	FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
1385b1e9ed26STakashi Iwai 	FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
1386b1e9ed26STakashi Iwai 	FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
1387b1e9ed26STakashi Iwai };
1388b1e9ed26STakashi Iwai 
138968cb2b55STakashi Iwai static int snd_fm801_suspend(struct device *dev)
1390b1e9ed26STakashi Iwai {
139168cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1392b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1393b1e9ed26STakashi Iwai 	int i;
1394b1e9ed26STakashi Iwai 
1395b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
139614da04b5SAndy Shevchenko 
139737ba8fcaSAndy Shevchenko 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
139837ba8fcaSAndy Shevchenko 		chip->saved_regs[i] = fm801_ioread16(chip, saved_regs[i]);
139937ba8fcaSAndy Shevchenko 
140014da04b5SAndy Shevchenko 	if (chip->tea575x_tuner & TUNER_ONLY) {
140114da04b5SAndy Shevchenko 		/* FIXME: tea575x suspend */
140214da04b5SAndy Shevchenko 	} else {
1403b1e9ed26STakashi Iwai 		snd_pcm_suspend_all(chip->pcm);
1404b1e9ed26STakashi Iwai 		snd_ac97_suspend(chip->ac97);
1405b1e9ed26STakashi Iwai 		snd_ac97_suspend(chip->ac97_sec);
140614da04b5SAndy Shevchenko 	}
140714da04b5SAndy Shevchenko 
1408b1e9ed26STakashi Iwai 	return 0;
1409b1e9ed26STakashi Iwai }
1410b1e9ed26STakashi Iwai 
141168cb2b55STakashi Iwai static int snd_fm801_resume(struct device *dev)
1412b1e9ed26STakashi Iwai {
141368cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1414b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1415b1e9ed26STakashi Iwai 	int i;
1416b1e9ed26STakashi Iwai 
1417b56fa687SAndy Shevchenko 	if (chip->tea575x_tuner & TUNER_ONLY) {
1418b56fa687SAndy Shevchenko 		snd_fm801_chip_init(chip);
1419b56fa687SAndy Shevchenko 	} else {
1420b56fa687SAndy Shevchenko 		reset_codec(chip);
1421b56fa687SAndy Shevchenko 		snd_fm801_chip_multichannel_init(chip);
1422b56fa687SAndy Shevchenko 		snd_fm801_chip_init(chip);
1423b1e9ed26STakashi Iwai 		snd_ac97_resume(chip->ac97);
1424b1e9ed26STakashi Iwai 		snd_ac97_resume(chip->ac97_sec);
142514da04b5SAndy Shevchenko 	}
142614da04b5SAndy Shevchenko 
1427b1e9ed26STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
14284b5c15f7SAndy Shevchenko 		fm801_iowrite16(chip, saved_regs[i], chip->saved_regs[i]);
1429b1e9ed26STakashi Iwai 
1430cb41f271SAndy Shevchenko #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1431cb41f271SAndy Shevchenko 	if (!(chip->tea575x_tuner & TUNER_DISABLED))
1432cb41f271SAndy Shevchenko 		snd_tea575x_set_freq(&chip->tea);
1433cb41f271SAndy Shevchenko #endif
1434b1e9ed26STakashi Iwai 
1435b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1436b1e9ed26STakashi Iwai 	return 0;
1437b1e9ed26STakashi Iwai }
143868cb2b55STakashi Iwai 
143968cb2b55STakashi Iwai static SIMPLE_DEV_PM_OPS(snd_fm801_pm, snd_fm801_suspend, snd_fm801_resume);
144068cb2b55STakashi Iwai #define SND_FM801_PM_OPS	&snd_fm801_pm
144168cb2b55STakashi Iwai #else
144268cb2b55STakashi Iwai #define SND_FM801_PM_OPS	NULL
1443c7561cd8STakashi Iwai #endif /* CONFIG_PM_SLEEP */
1444b1e9ed26STakashi Iwai 
1445e9f66d9bSTakashi Iwai static struct pci_driver fm801_driver = {
14463733e424STakashi Iwai 	.name = KBUILD_MODNAME,
14471da177e4SLinus Torvalds 	.id_table = snd_fm801_ids,
14481da177e4SLinus Torvalds 	.probe = snd_card_fm801_probe,
1449e23e7a14SBill Pemberton 	.remove = snd_card_fm801_remove,
145068cb2b55STakashi Iwai 	.driver = {
145168cb2b55STakashi Iwai 		.pm = SND_FM801_PM_OPS,
145268cb2b55STakashi Iwai 	},
14531da177e4SLinus Torvalds };
14541da177e4SLinus Torvalds 
1455e9f66d9bSTakashi Iwai module_pci_driver(fm801_driver);
1456