xref: /linux/sound/pci/fm801.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  The driver for the ForteMedia FM801 based soundcards
4c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/delay.h>
81da177e4SLinus Torvalds #include <linux/init.h>
91da177e4SLinus Torvalds #include <linux/interrupt.h>
10215dacc2SAndy Shevchenko #include <linux/io.h>
111da177e4SLinus Torvalds #include <linux/pci.h>
121da177e4SLinus Torvalds #include <linux/slab.h>
1365a77217SPaul Gortmaker #include <linux/module.h>
141da177e4SLinus Torvalds #include <sound/core.h>
151da177e4SLinus Torvalds #include <sound/pcm.h>
16666c70ffSTakashi Iwai #include <sound/tlv.h>
171da177e4SLinus Torvalds #include <sound/ac97_codec.h>
181da177e4SLinus Torvalds #include <sound/mpu401.h>
191da177e4SLinus Torvalds #include <sound/opl3.h>
201da177e4SLinus Torvalds #include <sound/initval.h>
211da177e4SLinus Torvalds 
22efce4bb9SAdrian Bunk #ifdef CONFIG_SND_FM801_TEA575X_BOOL
23d647f0b7SMauro Carvalho Chehab #include <media/drv-intf/tea575x.h>
241da177e4SLinus Torvalds #endif
251da177e4SLinus Torvalds 
26c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
271da177e4SLinus Torvalds MODULE_DESCRIPTION("ForteMedia FM801");
281da177e4SLinus Torvalds MODULE_LICENSE("GPL");
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
311da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
32a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
331da177e4SLinus Torvalds /*
341da177e4SLinus Torvalds  *  Enable TEA575x tuner
351da177e4SLinus Torvalds  *    1 = MediaForte 256-PCS
36d7ba858aSOndrej Zary  *    2 = MediaForte 256-PCP
371da177e4SLinus Torvalds  *    3 = MediaForte 64-PCR
38fb716c0bSOndrej Zary  *   16 = setup tuner only (this is additional bit), i.e. SF64-PCR FM card
391da177e4SLinus Torvalds  *  High 16-bits are video (radio) device number + 1
401da177e4SLinus Torvalds  */
416581f4e7STakashi Iwai static int tea575x_tuner[SNDRV_CARDS];
42d4ecc83bSHans Verkuil static int radio_nr[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444);
451da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
461da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444);
471da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
481da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444);
491da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
501da177e4SLinus Torvalds module_param_array(tea575x_tuner, int, NULL, 0444);
51d7ba858aSOndrej 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).");
52d4ecc83bSHans Verkuil module_param_array(radio_nr, int, NULL, 0444);
53d4ecc83bSHans Verkuil MODULE_PARM_DESC(radio_nr, "Radio device numbers");
54d4ecc83bSHans Verkuil 
55fb716c0bSOndrej Zary 
56c37279b9SBen Hutchings #define TUNER_DISABLED		(1<<3)
57fb716c0bSOndrej Zary #define TUNER_ONLY		(1<<4)
58fb716c0bSOndrej Zary #define TUNER_TYPE_MASK		(~TUNER_ONLY & 0xFFFF)
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /*
611da177e4SLinus Torvalds  *  Direct registers
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds 
64215dacc2SAndy Shevchenko #define fm801_writew(chip,reg,value)	outw((value), chip->port + FM801_##reg)
65215dacc2SAndy Shevchenko #define fm801_readw(chip,reg)		inw(chip->port + FM801_##reg)
66215dacc2SAndy Shevchenko 
67215dacc2SAndy Shevchenko #define fm801_writel(chip,reg,value)	outl((value), chip->port + FM801_##reg)
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #define FM801_PCM_VOL		0x00	/* PCM Output Volume */
701da177e4SLinus Torvalds #define FM801_FM_VOL		0x02	/* FM Output Volume */
711da177e4SLinus Torvalds #define FM801_I2S_VOL		0x04	/* I2S Volume */
721da177e4SLinus Torvalds #define FM801_REC_SRC		0x06	/* Record Source */
731da177e4SLinus Torvalds #define FM801_PLY_CTRL		0x08	/* Playback Control */
741da177e4SLinus Torvalds #define FM801_PLY_COUNT		0x0a	/* Playback Count */
751da177e4SLinus Torvalds #define FM801_PLY_BUF1		0x0c	/* Playback Bufer I */
761da177e4SLinus Torvalds #define FM801_PLY_BUF2		0x10	/* Playback Buffer II */
771da177e4SLinus Torvalds #define FM801_CAP_CTRL		0x14	/* Capture Control */
781da177e4SLinus Torvalds #define FM801_CAP_COUNT		0x16	/* Capture Count */
791da177e4SLinus Torvalds #define FM801_CAP_BUF1		0x18	/* Capture Buffer I */
801da177e4SLinus Torvalds #define FM801_CAP_BUF2		0x1c	/* Capture Buffer II */
811da177e4SLinus Torvalds #define FM801_CODEC_CTRL	0x22	/* Codec Control */
821da177e4SLinus Torvalds #define FM801_I2S_MODE		0x24	/* I2S Mode Control */
831da177e4SLinus Torvalds #define FM801_VOLUME		0x26	/* Volume Up/Down/Mute Status */
841da177e4SLinus Torvalds #define FM801_I2C_CTRL		0x29	/* I2C Control */
851da177e4SLinus Torvalds #define FM801_AC97_CMD		0x2a	/* AC'97 Command */
861da177e4SLinus Torvalds #define FM801_AC97_DATA		0x2c	/* AC'97 Data */
871da177e4SLinus Torvalds #define FM801_MPU401_DATA	0x30	/* MPU401 Data */
881da177e4SLinus Torvalds #define FM801_MPU401_CMD	0x31	/* MPU401 Command */
891da177e4SLinus Torvalds #define FM801_GPIO_CTRL		0x52	/* General Purpose I/O Control */
901da177e4SLinus Torvalds #define FM801_GEN_CTRL		0x54	/* General Control */
911da177e4SLinus Torvalds #define FM801_IRQ_MASK		0x56	/* Interrupt Mask */
921da177e4SLinus Torvalds #define FM801_IRQ_STATUS	0x5a	/* Interrupt Status */
931da177e4SLinus Torvalds #define FM801_OPL3_BANK0	0x68	/* OPL3 Status Read / Bank 0 Write */
941da177e4SLinus Torvalds #define FM801_OPL3_DATA0	0x69	/* OPL3 Data 0 Write */
951da177e4SLinus Torvalds #define FM801_OPL3_BANK1	0x6a	/* OPL3 Bank 1 Write */
961da177e4SLinus Torvalds #define FM801_OPL3_DATA1	0x6b	/* OPL3 Bank 1 Write */
971da177e4SLinus Torvalds #define FM801_POWERDOWN		0x70	/* Blocks Power Down Control */
981da177e4SLinus Torvalds 
99b1e9ed26STakashi Iwai /* codec access */
100b1e9ed26STakashi Iwai #define FM801_AC97_READ		(1<<7)	/* read=1, write=0 */
101b1e9ed26STakashi Iwai #define FM801_AC97_VALID	(1<<8)	/* port valid=1 */
102b1e9ed26STakashi Iwai #define FM801_AC97_BUSY		(1<<9)	/* busy=1 */
103b1e9ed26STakashi Iwai #define FM801_AC97_ADDR_SHIFT	10	/* codec id (2bit) */
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /* playback and record control register bits */
1061da177e4SLinus Torvalds #define FM801_BUF1_LAST		(1<<1)
1071da177e4SLinus Torvalds #define FM801_BUF2_LAST		(1<<2)
1081da177e4SLinus Torvalds #define FM801_START		(1<<5)
1091da177e4SLinus Torvalds #define FM801_PAUSE		(1<<6)
1101da177e4SLinus Torvalds #define FM801_IMMED_STOP	(1<<7)
1111da177e4SLinus Torvalds #define FM801_RATE_SHIFT	8
1121da177e4SLinus Torvalds #define FM801_RATE_MASK		(15 << FM801_RATE_SHIFT)
1131da177e4SLinus Torvalds #define FM801_CHANNELS_4	(1<<12)	/* playback only */
1141da177e4SLinus Torvalds #define FM801_CHANNELS_6	(2<<12)	/* playback only */
1151da177e4SLinus Torvalds #define FM801_CHANNELS_6MS	(3<<12)	/* playback only */
1161da177e4SLinus Torvalds #define FM801_CHANNELS_MASK	(3<<12)
1171da177e4SLinus Torvalds #define FM801_16BIT		(1<<14)
1181da177e4SLinus Torvalds #define FM801_STEREO		(1<<15)
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds /* IRQ status bits */
1211da177e4SLinus Torvalds #define FM801_IRQ_PLAYBACK	(1<<8)
1221da177e4SLinus Torvalds #define FM801_IRQ_CAPTURE	(1<<9)
1231da177e4SLinus Torvalds #define FM801_IRQ_VOLUME	(1<<14)
1241da177e4SLinus Torvalds #define FM801_IRQ_MPU		(1<<15)
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /* GPIO control register */
1271da177e4SLinus Torvalds #define FM801_GPIO_GP0		(1<<0)	/* read/write */
1281da177e4SLinus Torvalds #define FM801_GPIO_GP1		(1<<1)
1291da177e4SLinus Torvalds #define FM801_GPIO_GP2		(1<<2)
1301da177e4SLinus Torvalds #define FM801_GPIO_GP3		(1<<3)
1311da177e4SLinus Torvalds #define FM801_GPIO_GP(x)	(1<<(0+(x)))
1321da177e4SLinus Torvalds #define FM801_GPIO_GD0		(1<<8)	/* directions: 1 = input, 0 = output*/
1331da177e4SLinus Torvalds #define FM801_GPIO_GD1		(1<<9)
1341da177e4SLinus Torvalds #define FM801_GPIO_GD2		(1<<10)
1351da177e4SLinus Torvalds #define FM801_GPIO_GD3		(1<<11)
1361da177e4SLinus Torvalds #define FM801_GPIO_GD(x)	(1<<(8+(x)))
1371da177e4SLinus Torvalds #define FM801_GPIO_GS0		(1<<12)	/* function select: */
1381da177e4SLinus Torvalds #define FM801_GPIO_GS1		(1<<13)	/*    1 = GPIO */
1391da177e4SLinus Torvalds #define FM801_GPIO_GS2		(1<<14)	/*    0 = other (S/PDIF, VOL) */
1401da177e4SLinus Torvalds #define FM801_GPIO_GS3		(1<<15)
1411da177e4SLinus Torvalds #define FM801_GPIO_GS(x)	(1<<(12+(x)))
1421da177e4SLinus Torvalds 
143052c233eSAndy Shevchenko /**
144052c233eSAndy Shevchenko  * struct fm801 - describes FM801 chip
145af8c5dffSPierre-Louis Bossart  * @dev:		device for this chio
146af8c5dffSPierre-Louis Bossart  * @irq:		irq number
147052c233eSAndy Shevchenko  * @port:		I/O port number
148052c233eSAndy Shevchenko  * @multichannel:	multichannel support
149052c233eSAndy Shevchenko  * @secondary:		secondary codec
150052c233eSAndy Shevchenko  * @secondary_addr:	address of the secondary codec
151052c233eSAndy Shevchenko  * @tea575x_tuner:	tuner access method & flags
152052c233eSAndy Shevchenko  * @ply_ctrl:		playback control
153052c233eSAndy Shevchenko  * @cap_ctrl:		capture control
154af8c5dffSPierre-Louis Bossart  * @ply_buffer:		playback buffer
155af8c5dffSPierre-Louis Bossart  * @ply_buf:		playback buffer index
156af8c5dffSPierre-Louis Bossart  * @ply_count:		playback buffer count
157af8c5dffSPierre-Louis Bossart  * @ply_size:		playback buffer size
158af8c5dffSPierre-Louis Bossart  * @ply_pos:		playback position
159af8c5dffSPierre-Louis Bossart  * @cap_buffer:		capture buffer
160af8c5dffSPierre-Louis Bossart  * @cap_buf:		capture buffer index
161af8c5dffSPierre-Louis Bossart  * @cap_count:		capture buffer count
162af8c5dffSPierre-Louis Bossart  * @cap_size:		capture buffer size
163af8c5dffSPierre-Louis Bossart  * @cap_pos:		capture position
164af8c5dffSPierre-Louis Bossart  * @ac97_bus:		ac97 bus handle
165af8c5dffSPierre-Louis Bossart  * @ac97:		ac97 handle
166af8c5dffSPierre-Louis Bossart  * @ac97_sec:		ac97 secondary handle
167af8c5dffSPierre-Louis Bossart  * @card:		ALSA card
168af8c5dffSPierre-Louis Bossart  * @pcm:		PCM devices
169af8c5dffSPierre-Louis Bossart  * @rmidi:		rmidi device
170af8c5dffSPierre-Louis Bossart  * @playback_substream:	substream for playback
171af8c5dffSPierre-Louis Bossart  * @capture_substream:	substream for capture
172af8c5dffSPierre-Louis Bossart  * @p_dma_size:		playback DMA size
173af8c5dffSPierre-Louis Bossart  * @c_dma_size:		capture DMA size
174af8c5dffSPierre-Louis Bossart  * @reg_lock:		lock
175af8c5dffSPierre-Louis Bossart  * @proc_entry:		/proc entry
176af8c5dffSPierre-Louis Bossart  * @v4l2_dev:		v4l2 device
177af8c5dffSPierre-Louis Bossart  * @tea:		tea575a structure
178af8c5dffSPierre-Louis Bossart  * @saved_regs:		context saved during suspend
1791da177e4SLinus Torvalds  */
180a5f22156STakashi Iwai struct fm801 {
181d3d33aabSAndy Shevchenko 	struct device *dev;
1821da177e4SLinus Torvalds 	int irq;
1831da177e4SLinus Torvalds 
184052c233eSAndy Shevchenko 	unsigned long port;
185052c233eSAndy Shevchenko 	unsigned int multichannel: 1,
186052c233eSAndy Shevchenko 		     secondary: 1;
187052c233eSAndy Shevchenko 	unsigned char secondary_addr;
188052c233eSAndy Shevchenko 	unsigned int tea575x_tuner;
1891da177e4SLinus Torvalds 
190052c233eSAndy Shevchenko 	unsigned short ply_ctrl;
191052c233eSAndy Shevchenko 	unsigned short cap_ctrl;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	unsigned long ply_buffer;
1941da177e4SLinus Torvalds 	unsigned int ply_buf;
1951da177e4SLinus Torvalds 	unsigned int ply_count;
1961da177e4SLinus Torvalds 	unsigned int ply_size;
1971da177e4SLinus Torvalds 	unsigned int ply_pos;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	unsigned long cap_buffer;
2001da177e4SLinus Torvalds 	unsigned int cap_buf;
2011da177e4SLinus Torvalds 	unsigned int cap_count;
2021da177e4SLinus Torvalds 	unsigned int cap_size;
2031da177e4SLinus Torvalds 	unsigned int cap_pos;
2041da177e4SLinus Torvalds 
205a5f22156STakashi Iwai 	struct snd_ac97_bus *ac97_bus;
206a5f22156STakashi Iwai 	struct snd_ac97 *ac97;
207a5f22156STakashi Iwai 	struct snd_ac97 *ac97_sec;
2081da177e4SLinus Torvalds 
209a5f22156STakashi Iwai 	struct snd_card *card;
210a5f22156STakashi Iwai 	struct snd_pcm *pcm;
211a5f22156STakashi Iwai 	struct snd_rawmidi *rmidi;
212a5f22156STakashi Iwai 	struct snd_pcm_substream *playback_substream;
213a5f22156STakashi Iwai 	struct snd_pcm_substream *capture_substream;
2141da177e4SLinus Torvalds 	unsigned int p_dma_size;
2151da177e4SLinus Torvalds 	unsigned int c_dma_size;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	spinlock_t reg_lock;
218a5f22156STakashi Iwai 	struct snd_info_entry *proc_entry;
2191da177e4SLinus Torvalds 
220fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
221d4ecc83bSHans Verkuil 	struct v4l2_device v4l2_dev;
222a5f22156STakashi Iwai 	struct snd_tea575x tea;
2231da177e4SLinus Torvalds #endif
224b1e9ed26STakashi Iwai 
225b1e9ed26STakashi Iwai 	u16 saved_regs[0x20];
2261da177e4SLinus Torvalds };
2271da177e4SLinus Torvalds 
2284b5c15f7SAndy Shevchenko /*
2294b5c15f7SAndy Shevchenko  * IO accessors
2304b5c15f7SAndy Shevchenko  */
2314b5c15f7SAndy Shevchenko 
fm801_iowrite16(struct fm801 * chip,unsigned short offset,u16 value)2324b5c15f7SAndy Shevchenko static inline void fm801_iowrite16(struct fm801 *chip, unsigned short offset, u16 value)
2334b5c15f7SAndy Shevchenko {
2344b5c15f7SAndy Shevchenko 	outw(value, chip->port + offset);
2354b5c15f7SAndy Shevchenko }
2364b5c15f7SAndy Shevchenko 
fm801_ioread16(struct fm801 * chip,unsigned short offset)2374b5c15f7SAndy Shevchenko static inline u16 fm801_ioread16(struct fm801 *chip, unsigned short offset)
2384b5c15f7SAndy Shevchenko {
2394b5c15f7SAndy Shevchenko 	return inw(chip->port + offset);
2404b5c15f7SAndy Shevchenko }
2414b5c15f7SAndy Shevchenko 
2429baa3c34SBenoit Taine static const struct pci_device_id snd_fm801_ids[] = {
2431da177e4SLinus Torvalds 	{ 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* FM801 */
24426be8659SSergey Vlasov 	{ 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* Gallant Odyssey Sound 4 */
2451da177e4SLinus Torvalds 	{ 0, }
2461da177e4SLinus Torvalds };
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds /*
2511da177e4SLinus Torvalds  *  common I/O routines
2521da177e4SLinus Torvalds  */
2531da177e4SLinus Torvalds 
fm801_ac97_is_ready(struct fm801 * chip,unsigned int iterations)25402fd1a76SAndy Shevchenko static bool fm801_ac97_is_ready(struct fm801 *chip, unsigned int iterations)
25502fd1a76SAndy Shevchenko {
25602fd1a76SAndy Shevchenko 	unsigned int idx;
25702fd1a76SAndy Shevchenko 
25802fd1a76SAndy Shevchenko 	for (idx = 0; idx < iterations; idx++) {
25902fd1a76SAndy Shevchenko 		if (!(fm801_readw(chip, AC97_CMD) & FM801_AC97_BUSY))
26002fd1a76SAndy Shevchenko 			return true;
26102fd1a76SAndy Shevchenko 		udelay(10);
26202fd1a76SAndy Shevchenko 	}
26302fd1a76SAndy Shevchenko 	return false;
26402fd1a76SAndy Shevchenko }
26502fd1a76SAndy Shevchenko 
fm801_ac97_is_valid(struct fm801 * chip,unsigned int iterations)26602fd1a76SAndy Shevchenko static bool fm801_ac97_is_valid(struct fm801 *chip, unsigned int iterations)
26702fd1a76SAndy Shevchenko {
26802fd1a76SAndy Shevchenko 	unsigned int idx;
26902fd1a76SAndy Shevchenko 
27002fd1a76SAndy Shevchenko 	for (idx = 0; idx < iterations; idx++) {
27102fd1a76SAndy Shevchenko 		if (fm801_readw(chip, AC97_CMD) & FM801_AC97_VALID)
27202fd1a76SAndy Shevchenko 			return true;
27302fd1a76SAndy Shevchenko 		udelay(10);
27402fd1a76SAndy Shevchenko 	}
27502fd1a76SAndy Shevchenko 	return false;
27602fd1a76SAndy Shevchenko }
27702fd1a76SAndy Shevchenko 
snd_fm801_update_bits(struct fm801 * chip,unsigned short reg,unsigned short mask,unsigned short value)278a5f22156STakashi Iwai static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
2791da177e4SLinus Torvalds 				 unsigned short mask, unsigned short value)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	int change;
2821da177e4SLinus Torvalds 	unsigned long flags;
2831da177e4SLinus Torvalds 	unsigned short old, new;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
2864b5c15f7SAndy Shevchenko 	old = fm801_ioread16(chip, reg);
2871da177e4SLinus Torvalds 	new = (old & ~mask) | value;
2881da177e4SLinus Torvalds 	change = old != new;
2891da177e4SLinus Torvalds 	if (change)
2904b5c15f7SAndy Shevchenko 		fm801_iowrite16(chip, reg, new);
2911da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
2921da177e4SLinus Torvalds 	return change;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
snd_fm801_codec_write(struct snd_ac97 * ac97,unsigned short reg,unsigned short val)295a5f22156STakashi Iwai static void snd_fm801_codec_write(struct snd_ac97 *ac97,
2961da177e4SLinus Torvalds 				  unsigned short reg,
2971da177e4SLinus Torvalds 				  unsigned short val)
2981da177e4SLinus Torvalds {
299a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	/*
3021da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
3031da177e4SLinus Torvalds 	 */
30402fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 100)) {
3059c7f9abfSTakashi Iwai 		dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
3061da177e4SLinus Torvalds 		return;
30702fd1a76SAndy Shevchenko 	}
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	/* write data and address */
310215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_DATA, val);
311215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_CMD, reg | (ac97->addr << FM801_AC97_ADDR_SHIFT));
3121da177e4SLinus Torvalds 	/*
3131da177e4SLinus Torvalds 	 *  Wait until the write command is not completed..
3141da177e4SLinus Torvalds 	 */
31502fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 1000))
31602fd1a76SAndy Shevchenko 		dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
31702fd1a76SAndy Shevchenko 		ac97->num);
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
snd_fm801_codec_read(struct snd_ac97 * ac97,unsigned short reg)320a5f22156STakashi Iwai static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
3211da177e4SLinus Torvalds {
322a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	/*
3251da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
3261da177e4SLinus Torvalds 	 */
32702fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 100)) {
3289c7f9abfSTakashi Iwai 		dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
3291da177e4SLinus Torvalds 		return 0;
33002fd1a76SAndy Shevchenko 	}
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	/* read command */
333215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_CMD,
334215dacc2SAndy Shevchenko 		     reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
33502fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 100)) {
33602fd1a76SAndy Shevchenko 		dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
33702fd1a76SAndy Shevchenko 			ac97->num);
3381da177e4SLinus Torvalds 		return 0;
3391da177e4SLinus Torvalds 	}
3401da177e4SLinus Torvalds 
34102fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_valid(chip, 1000)) {
34202fd1a76SAndy Shevchenko 		dev_err(chip->card->dev,
34302fd1a76SAndy Shevchenko 			"AC'97 interface #%d is not valid (2)\n", ac97->num);
34402fd1a76SAndy Shevchenko 		return 0;
34502fd1a76SAndy Shevchenko 	}
34602fd1a76SAndy Shevchenko 
347215dacc2SAndy Shevchenko 	return fm801_readw(chip, AC97_DATA);
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
350d71a13f4STakashi Iwai static const unsigned int rates[] = {
3511da177e4SLinus Torvalds   5500,  8000,  9600, 11025,
3521da177e4SLinus Torvalds   16000, 19200, 22050, 32000,
3531da177e4SLinus Torvalds   38400, 44100, 48000
3541da177e4SLinus Torvalds };
3551da177e4SLinus Torvalds 
356d71a13f4STakashi Iwai static const struct snd_pcm_hw_constraint_list hw_constraints_rates = {
3571da177e4SLinus Torvalds 	.count = ARRAY_SIZE(rates),
3581da177e4SLinus Torvalds 	.list = rates,
3591da177e4SLinus Torvalds 	.mask = 0,
3601da177e4SLinus Torvalds };
3611da177e4SLinus Torvalds 
362d71a13f4STakashi Iwai static const unsigned int channels[] = {
3631da177e4SLinus Torvalds   2, 4, 6
3641da177e4SLinus Torvalds };
3651da177e4SLinus Torvalds 
366d71a13f4STakashi Iwai static const struct snd_pcm_hw_constraint_list hw_constraints_channels = {
3675e4968e2STobias Klauser 	.count = ARRAY_SIZE(channels),
3681da177e4SLinus Torvalds 	.list = channels,
3691da177e4SLinus Torvalds 	.mask = 0,
3701da177e4SLinus Torvalds };
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds /*
3731da177e4SLinus Torvalds  *  Sample rate routines
3741da177e4SLinus Torvalds  */
3751da177e4SLinus Torvalds 
snd_fm801_rate_bits(unsigned int rate)3761da177e4SLinus Torvalds static unsigned short snd_fm801_rate_bits(unsigned int rate)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	unsigned int idx;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
3811da177e4SLinus Torvalds 		if (rates[idx] == rate)
3821da177e4SLinus Torvalds 			return idx;
3831da177e4SLinus Torvalds 	snd_BUG();
3841da177e4SLinus Torvalds 	return ARRAY_SIZE(rates) - 1;
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds /*
3881da177e4SLinus Torvalds  *  PCM part
3891da177e4SLinus Torvalds  */
3901da177e4SLinus Torvalds 
snd_fm801_playback_trigger(struct snd_pcm_substream * substream,int cmd)391a5f22156STakashi Iwai static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
3921da177e4SLinus Torvalds 				      int cmd)
3931da177e4SLinus Torvalds {
394a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3971da177e4SLinus Torvalds 	switch (cmd) {
3981da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
3991da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_BUF1_LAST |
4001da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
4011da177e4SLinus Torvalds 				     FM801_PAUSE);
4021da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_START |
4031da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
4041da177e4SLinus Torvalds 		break;
4051da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4061da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
4071da177e4SLinus Torvalds 		break;
4081da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
409b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
4101da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_PAUSE;
4111da177e4SLinus Torvalds 		break;
4121da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
413b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
4141da177e4SLinus Torvalds 		chip->ply_ctrl &= ~FM801_PAUSE;
4151da177e4SLinus Torvalds 		break;
4161da177e4SLinus Torvalds 	default:
4171da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
4181da177e4SLinus Torvalds 		snd_BUG();
4191da177e4SLinus Torvalds 		return -EINVAL;
4201da177e4SLinus Torvalds 	}
421215dacc2SAndy Shevchenko 	fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
4221da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4231da177e4SLinus Torvalds 	return 0;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
snd_fm801_capture_trigger(struct snd_pcm_substream * substream,int cmd)426a5f22156STakashi Iwai static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
4271da177e4SLinus Torvalds 				     int cmd)
4281da177e4SLinus Torvalds {
429a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
4321da177e4SLinus Torvalds 	switch (cmd) {
4331da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4341da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_BUF1_LAST |
4351da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
4361da177e4SLinus Torvalds 				     FM801_PAUSE);
4371da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_START |
4381da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
4391da177e4SLinus Torvalds 		break;
4401da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4411da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
4421da177e4SLinus Torvalds 		break;
4431da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
444b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
4451da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_PAUSE;
4461da177e4SLinus Torvalds 		break;
4471da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
448b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
4491da177e4SLinus Torvalds 		chip->cap_ctrl &= ~FM801_PAUSE;
4501da177e4SLinus Torvalds 		break;
4511da177e4SLinus Torvalds 	default:
4521da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
4531da177e4SLinus Torvalds 		snd_BUG();
4541da177e4SLinus Torvalds 		return -EINVAL;
4551da177e4SLinus Torvalds 	}
456215dacc2SAndy Shevchenko 	fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
4571da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4581da177e4SLinus Torvalds 	return 0;
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
snd_fm801_playback_prepare(struct snd_pcm_substream * substream)461a5f22156STakashi Iwai static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
4621da177e4SLinus Torvalds {
463a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
464a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
4671da177e4SLinus Torvalds 	chip->ply_count = snd_pcm_lib_period_bytes(substream);
4681da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
4691da177e4SLinus Torvalds 	chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
4701da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK |
4711da177e4SLinus Torvalds 			     FM801_CHANNELS_MASK);
4721da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
4731da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_16BIT;
4741da177e4SLinus Torvalds 	if (runtime->channels > 1) {
4751da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_STEREO;
4761da177e4SLinus Torvalds 		if (runtime->channels == 4)
4771da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_4;
4781da177e4SLinus Torvalds 		else if (runtime->channels == 6)
4791da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_6;
4801da177e4SLinus Torvalds 	}
4811da177e4SLinus Torvalds 	chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
4821da177e4SLinus Torvalds 	chip->ply_buf = 0;
483215dacc2SAndy Shevchenko 	fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
484215dacc2SAndy Shevchenko 	fm801_writew(chip, PLY_COUNT, chip->ply_count - 1);
4851da177e4SLinus Torvalds 	chip->ply_buffer = runtime->dma_addr;
4861da177e4SLinus Torvalds 	chip->ply_pos = 0;
487215dacc2SAndy Shevchenko 	fm801_writel(chip, PLY_BUF1, chip->ply_buffer);
488215dacc2SAndy Shevchenko 	fm801_writel(chip, PLY_BUF2,
489215dacc2SAndy Shevchenko 		     chip->ply_buffer + (chip->ply_count % chip->ply_size));
4901da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
4911da177e4SLinus Torvalds 	return 0;
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
snd_fm801_capture_prepare(struct snd_pcm_substream * substream)494a5f22156STakashi Iwai static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
4951da177e4SLinus Torvalds {
496a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
497a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
5001da177e4SLinus Torvalds 	chip->cap_count = snd_pcm_lib_period_bytes(substream);
5011da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
5021da177e4SLinus Torvalds 	chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
5031da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK);
5041da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
5051da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_16BIT;
5061da177e4SLinus Torvalds 	if (runtime->channels > 1)
5071da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_STEREO;
5081da177e4SLinus Torvalds 	chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
5091da177e4SLinus Torvalds 	chip->cap_buf = 0;
510215dacc2SAndy Shevchenko 	fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
511215dacc2SAndy Shevchenko 	fm801_writew(chip, CAP_COUNT, chip->cap_count - 1);
5121da177e4SLinus Torvalds 	chip->cap_buffer = runtime->dma_addr;
5131da177e4SLinus Torvalds 	chip->cap_pos = 0;
514215dacc2SAndy Shevchenko 	fm801_writel(chip, CAP_BUF1, chip->cap_buffer);
515215dacc2SAndy Shevchenko 	fm801_writel(chip, CAP_BUF2,
516215dacc2SAndy Shevchenko 		     chip->cap_buffer + (chip->cap_count % chip->cap_size));
5171da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
5181da177e4SLinus Torvalds 	return 0;
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
snd_fm801_playback_pointer(struct snd_pcm_substream * substream)521a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
5221da177e4SLinus Torvalds {
523a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
5241da177e4SLinus Torvalds 	size_t ptr;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	if (!(chip->ply_ctrl & FM801_START))
5271da177e4SLinus Torvalds 		return 0;
5281da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
529215dacc2SAndy Shevchenko 	ptr = chip->ply_pos + (chip->ply_count - 1) - fm801_readw(chip, PLY_COUNT);
530215dacc2SAndy Shevchenko 	if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_PLAYBACK) {
5311da177e4SLinus Torvalds 		ptr += chip->ply_count;
5321da177e4SLinus Torvalds 		ptr %= chip->ply_size;
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5351da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
snd_fm801_capture_pointer(struct snd_pcm_substream * substream)538a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
5391da177e4SLinus Torvalds {
540a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
5411da177e4SLinus Torvalds 	size_t ptr;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	if (!(chip->cap_ctrl & FM801_START))
5441da177e4SLinus Torvalds 		return 0;
5451da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
546215dacc2SAndy Shevchenko 	ptr = chip->cap_pos + (chip->cap_count - 1) - fm801_readw(chip, CAP_COUNT);
547215dacc2SAndy Shevchenko 	if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_CAPTURE) {
5481da177e4SLinus Torvalds 		ptr += chip->cap_count;
5491da177e4SLinus Torvalds 		ptr %= chip->cap_size;
5501da177e4SLinus Torvalds 	}
5511da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5521da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
snd_fm801_interrupt(int irq,void * dev_id)5557d12e780SDavid Howells static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
5561da177e4SLinus Torvalds {
557a5f22156STakashi Iwai 	struct fm801 *chip = dev_id;
5581da177e4SLinus Torvalds 	unsigned short status;
5591da177e4SLinus Torvalds 	unsigned int tmp;
5601da177e4SLinus Torvalds 
561215dacc2SAndy Shevchenko 	status = fm801_readw(chip, IRQ_STATUS);
5621da177e4SLinus Torvalds 	status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
5631da177e4SLinus Torvalds 	if (! status)
5641da177e4SLinus Torvalds 		return IRQ_NONE;
5651da177e4SLinus Torvalds 	/* ack first */
566215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_STATUS, status);
5671da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
5681da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5691da177e4SLinus Torvalds 		chip->ply_buf++;
5701da177e4SLinus Torvalds 		chip->ply_pos += chip->ply_count;
5711da177e4SLinus Torvalds 		chip->ply_pos %= chip->ply_size;
5721da177e4SLinus Torvalds 		tmp = chip->ply_pos + chip->ply_count;
5731da177e4SLinus Torvalds 		tmp %= chip->ply_size;
574215dacc2SAndy Shevchenko 		if (chip->ply_buf & 1)
575215dacc2SAndy Shevchenko 			fm801_writel(chip, PLY_BUF1, chip->ply_buffer + tmp);
576215dacc2SAndy Shevchenko 		else
577215dacc2SAndy Shevchenko 			fm801_writel(chip, PLY_BUF2, chip->ply_buffer + tmp);
5781da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5791da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->playback_substream);
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
5821da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5831da177e4SLinus Torvalds 		chip->cap_buf++;
5841da177e4SLinus Torvalds 		chip->cap_pos += chip->cap_count;
5851da177e4SLinus Torvalds 		chip->cap_pos %= chip->cap_size;
5861da177e4SLinus Torvalds 		tmp = chip->cap_pos + chip->cap_count;
5871da177e4SLinus Torvalds 		tmp %= chip->cap_size;
588215dacc2SAndy Shevchenko 		if (chip->cap_buf & 1)
589215dacc2SAndy Shevchenko 			fm801_writel(chip, CAP_BUF1, chip->cap_buffer + tmp);
590215dacc2SAndy Shevchenko 		else
591215dacc2SAndy Shevchenko 			fm801_writel(chip, CAP_BUF2, chip->cap_buffer + tmp);
5921da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5931da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->capture_substream);
5941da177e4SLinus Torvalds 	}
5951da177e4SLinus Torvalds 	if (chip->rmidi && (status & FM801_IRQ_MPU))
5967d12e780SDavid Howells 		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
597997c87daSAndy Shevchenko 	if (status & FM801_IRQ_VOLUME) {
598997c87daSAndy Shevchenko 		/* TODO */
599997c87daSAndy Shevchenko 	}
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	return IRQ_HANDLED;
6021da177e4SLinus Torvalds }
6031da177e4SLinus Torvalds 
604dee49895SBhumika Goyal static const struct snd_pcm_hardware snd_fm801_playback =
6051da177e4SLinus Torvalds {
6061da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
6071da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
608b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
6091da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
6101da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
6111da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
6121da177e4SLinus Torvalds 	.rate_min =		5500,
6131da177e4SLinus Torvalds 	.rate_max =		48000,
6141da177e4SLinus Torvalds 	.channels_min =		1,
6151da177e4SLinus Torvalds 	.channels_max =		2,
6161da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
6171da177e4SLinus Torvalds 	.period_bytes_min =	64,
6181da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
6191da177e4SLinus Torvalds 	.periods_min =		1,
6201da177e4SLinus Torvalds 	.periods_max =		1024,
6211da177e4SLinus Torvalds 	.fifo_size =		0,
6221da177e4SLinus Torvalds };
6231da177e4SLinus Torvalds 
624dee49895SBhumika Goyal static const struct snd_pcm_hardware snd_fm801_capture =
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
6271da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
628b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
6291da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
6301da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
6311da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
6321da177e4SLinus Torvalds 	.rate_min =		5500,
6331da177e4SLinus Torvalds 	.rate_max =		48000,
6341da177e4SLinus Torvalds 	.channels_min =		1,
6351da177e4SLinus Torvalds 	.channels_max =		2,
6361da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
6371da177e4SLinus Torvalds 	.period_bytes_min =	64,
6381da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
6391da177e4SLinus Torvalds 	.periods_min =		1,
6401da177e4SLinus Torvalds 	.periods_max =		1024,
6411da177e4SLinus Torvalds 	.fifo_size =		0,
6421da177e4SLinus Torvalds };
6431da177e4SLinus Torvalds 
snd_fm801_playback_open(struct snd_pcm_substream * substream)644a5f22156STakashi Iwai static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
6451da177e4SLinus Torvalds {
646a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
647a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6481da177e4SLinus Torvalds 	int err;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	chip->playback_substream = substream;
6511da177e4SLinus Torvalds 	runtime->hw = snd_fm801_playback;
652a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
653a5f22156STakashi Iwai 				   &hw_constraints_rates);
6541da177e4SLinus Torvalds 	if (chip->multichannel) {
6551da177e4SLinus Torvalds 		runtime->hw.channels_max = 6;
656a5f22156STakashi Iwai 		snd_pcm_hw_constraint_list(runtime, 0,
657a5f22156STakashi Iwai 					   SNDRV_PCM_HW_PARAM_CHANNELS,
658a5f22156STakashi Iwai 					   &hw_constraints_channels);
6591da177e4SLinus Torvalds 	}
66068f441abSTakashi Iwai 	err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
66168f441abSTakashi Iwai 	if (err < 0)
6621da177e4SLinus Torvalds 		return err;
6631da177e4SLinus Torvalds 	return 0;
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
snd_fm801_capture_open(struct snd_pcm_substream * substream)666a5f22156STakashi Iwai static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
6671da177e4SLinus Torvalds {
668a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
669a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6701da177e4SLinus Torvalds 	int err;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	chip->capture_substream = substream;
6731da177e4SLinus Torvalds 	runtime->hw = snd_fm801_capture;
674a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
675a5f22156STakashi Iwai 				   &hw_constraints_rates);
67668f441abSTakashi Iwai 	err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
67768f441abSTakashi Iwai 	if (err < 0)
6781da177e4SLinus Torvalds 		return err;
6791da177e4SLinus Torvalds 	return 0;
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
snd_fm801_playback_close(struct snd_pcm_substream * substream)682a5f22156STakashi Iwai static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
6831da177e4SLinus Torvalds {
684a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	chip->playback_substream = NULL;
6871da177e4SLinus Torvalds 	return 0;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
snd_fm801_capture_close(struct snd_pcm_substream * substream)690a5f22156STakashi Iwai static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
6911da177e4SLinus Torvalds {
692a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	chip->capture_substream = NULL;
6951da177e4SLinus Torvalds 	return 0;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6986769e988SJulia Lawall static const struct snd_pcm_ops snd_fm801_playback_ops = {
6991da177e4SLinus Torvalds 	.open =		snd_fm801_playback_open,
7001da177e4SLinus Torvalds 	.close =	snd_fm801_playback_close,
7011da177e4SLinus Torvalds 	.prepare =	snd_fm801_playback_prepare,
7021da177e4SLinus Torvalds 	.trigger =	snd_fm801_playback_trigger,
7031da177e4SLinus Torvalds 	.pointer =	snd_fm801_playback_pointer,
7041da177e4SLinus Torvalds };
7051da177e4SLinus Torvalds 
7066769e988SJulia Lawall static const struct snd_pcm_ops snd_fm801_capture_ops = {
7071da177e4SLinus Torvalds 	.open =		snd_fm801_capture_open,
7081da177e4SLinus Torvalds 	.close =	snd_fm801_capture_close,
7091da177e4SLinus Torvalds 	.prepare =	snd_fm801_capture_prepare,
7101da177e4SLinus Torvalds 	.trigger =	snd_fm801_capture_trigger,
7111da177e4SLinus Torvalds 	.pointer =	snd_fm801_capture_pointer,
7121da177e4SLinus Torvalds };
7131da177e4SLinus Torvalds 
snd_fm801_pcm(struct fm801 * chip,int device)714483337f9SLars-Peter Clausen static int snd_fm801_pcm(struct fm801 *chip, int device)
7151da177e4SLinus Torvalds {
716d3d33aabSAndy Shevchenko 	struct pci_dev *pdev = to_pci_dev(chip->dev);
717a5f22156STakashi Iwai 	struct snd_pcm *pcm;
7181da177e4SLinus Torvalds 	int err;
7191da177e4SLinus Torvalds 
72068f441abSTakashi Iwai 	err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm);
72168f441abSTakashi Iwai 	if (err < 0)
7221da177e4SLinus Torvalds 		return err;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
7251da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	pcm->private_data = chip;
7281da177e4SLinus Torvalds 	pcm->info_flags = 0;
7291da177e4SLinus Torvalds 	strcpy(pcm->name, "FM801");
7301da177e4SLinus Torvalds 	chip->pcm = pcm;
7311da177e4SLinus Torvalds 
732247ed102STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &pdev->dev,
7331da177e4SLinus Torvalds 				       chip->multichannel ? 128*1024 : 64*1024, 128*1024);
7341da177e4SLinus Torvalds 
735483337f9SLars-Peter Clausen 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
736e36e3b86STakashi Iwai 				     snd_pcm_alt_chmaps,
737e36e3b86STakashi Iwai 				     chip->multichannel ? 6 : 2, 0,
738e36e3b86STakashi Iwai 				     NULL);
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds /*
7421da177e4SLinus Torvalds  *  TEA5757 radio
7431da177e4SLinus Torvalds  */
7441da177e4SLinus Torvalds 
745fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
7461da177e4SLinus Torvalds 
747938a1566SOndrej Zary /* GPIO to TEA575x maps */
748938a1566SOndrej Zary struct snd_fm801_tea575x_gpio {
749938a1566SOndrej Zary 	u8 data, clk, wren, most;
750d7ba858aSOndrej Zary 	char *name;
751938a1566SOndrej Zary };
7521da177e4SLinus Torvalds 
753fb537cd0STakashi Iwai static const struct snd_fm801_tea575x_gpio snd_fm801_tea575x_gpios[] = {
754d7ba858aSOndrej Zary 	{ .data = 1, .clk = 3, .wren = 2, .most = 0, .name = "SF256-PCS" },
755d7ba858aSOndrej Zary 	{ .data = 1, .clk = 0, .wren = 2, .most = 3, .name = "SF256-PCP" },
756d7ba858aSOndrej Zary 	{ .data = 2, .clk = 0, .wren = 1, .most = 3, .name = "SF64-PCR" },
757938a1566SOndrej Zary };
758938a1566SOndrej Zary 
7598e699d2cSTakashi Iwai #define get_tea575x_gpio(chip) \
7608e699d2cSTakashi Iwai 	(&snd_fm801_tea575x_gpios[((chip)->tea575x_tuner & TUNER_TYPE_MASK) - 1])
7618e699d2cSTakashi Iwai 
snd_fm801_tea575x_set_pins(struct snd_tea575x * tea,u8 pins)762938a1566SOndrej Zary static void snd_fm801_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
7631da177e4SLinus Torvalds {
764a5f22156STakashi Iwai 	struct fm801 *chip = tea->private_data;
765215dacc2SAndy Shevchenko 	unsigned short reg = fm801_readw(chip, GPIO_CTRL);
7668e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
7671da177e4SLinus Torvalds 
768938a1566SOndrej Zary 	reg &= ~(FM801_GPIO_GP(gpio.data) |
769938a1566SOndrej Zary 		 FM801_GPIO_GP(gpio.clk) |
770938a1566SOndrej Zary 		 FM801_GPIO_GP(gpio.wren));
771938a1566SOndrej Zary 
772938a1566SOndrej Zary 	reg |= (pins & TEA575X_DATA) ? FM801_GPIO_GP(gpio.data) : 0;
773938a1566SOndrej Zary 	reg |= (pins & TEA575X_CLK)  ? FM801_GPIO_GP(gpio.clk) : 0;
774938a1566SOndrej Zary 	/* WRITE_ENABLE is inverted */
775938a1566SOndrej Zary 	reg |= (pins & TEA575X_WREN) ? 0 : FM801_GPIO_GP(gpio.wren);
776938a1566SOndrej Zary 
777215dacc2SAndy Shevchenko 	fm801_writew(chip, GPIO_CTRL, reg);
778938a1566SOndrej Zary }
779938a1566SOndrej Zary 
snd_fm801_tea575x_get_pins(struct snd_tea575x * tea)780938a1566SOndrej Zary static u8 snd_fm801_tea575x_get_pins(struct snd_tea575x *tea)
781938a1566SOndrej Zary {
782938a1566SOndrej Zary 	struct fm801 *chip = tea->private_data;
783215dacc2SAndy Shevchenko 	unsigned short reg = fm801_readw(chip, GPIO_CTRL);
7848e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
785effded75SDan Carpenter 	u8 ret;
786938a1566SOndrej Zary 
787effded75SDan Carpenter 	ret = 0;
788effded75SDan Carpenter 	if (reg & FM801_GPIO_GP(gpio.data))
789effded75SDan Carpenter 		ret |= TEA575X_DATA;
790effded75SDan Carpenter 	if (reg & FM801_GPIO_GP(gpio.most))
791effded75SDan Carpenter 		ret |= TEA575X_MOST;
792effded75SDan Carpenter 	return ret;
793938a1566SOndrej Zary }
794938a1566SOndrej Zary 
snd_fm801_tea575x_set_direction(struct snd_tea575x * tea,bool output)795938a1566SOndrej Zary static void snd_fm801_tea575x_set_direction(struct snd_tea575x *tea, bool output)
796938a1566SOndrej Zary {
797938a1566SOndrej Zary 	struct fm801 *chip = tea->private_data;
798215dacc2SAndy Shevchenko 	unsigned short reg = fm801_readw(chip, GPIO_CTRL);
7998e699d2cSTakashi Iwai 	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
800938a1566SOndrej Zary 
8011da177e4SLinus Torvalds 	/* use GPIO lines and set write enable bit */
802938a1566SOndrej Zary 	reg |= FM801_GPIO_GS(gpio.data) |
803938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.wren) |
804938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.clk) |
805938a1566SOndrej Zary 	       FM801_GPIO_GS(gpio.most);
806938a1566SOndrej Zary 	if (output) {
8071da177e4SLinus Torvalds 		/* all of lines are in the write direction */
8081da177e4SLinus Torvalds 		/* clear data and clock lines */
809938a1566SOndrej Zary 		reg &= ~(FM801_GPIO_GD(gpio.data) |
810938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.wren) |
811938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.clk) |
812938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.data) |
813938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.clk) |
814938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.wren));
815938a1566SOndrej Zary 	} else {
8161da177e4SLinus Torvalds 		/* use GPIO lines, set data direction to input */
817938a1566SOndrej Zary 		reg |= FM801_GPIO_GD(gpio.data) |
818938a1566SOndrej Zary 		       FM801_GPIO_GD(gpio.most) |
819938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.data) |
820938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.most) |
821938a1566SOndrej Zary 		       FM801_GPIO_GP(gpio.wren);
8221da177e4SLinus Torvalds 		/* all of lines are in the write direction, except data */
8231da177e4SLinus Torvalds 		/* clear data, write enable and clock lines */
824938a1566SOndrej Zary 		reg &= ~(FM801_GPIO_GD(gpio.wren) |
825938a1566SOndrej Zary 			 FM801_GPIO_GD(gpio.clk) |
826938a1566SOndrej Zary 			 FM801_GPIO_GP(gpio.clk));
8271da177e4SLinus Torvalds 	}
8281da177e4SLinus Torvalds 
829215dacc2SAndy Shevchenko 	fm801_writew(chip, GPIO_CTRL, reg);
8301da177e4SLinus Torvalds }
8311da177e4SLinus Torvalds 
83222dbec26SJulia Lawall static const struct snd_tea575x_ops snd_fm801_tea_ops = {
833938a1566SOndrej Zary 	.set_pins = snd_fm801_tea575x_set_pins,
834938a1566SOndrej Zary 	.get_pins = snd_fm801_tea575x_get_pins,
835938a1566SOndrej Zary 	.set_direction = snd_fm801_tea575x_set_direction,
8361da177e4SLinus Torvalds };
8371da177e4SLinus Torvalds #endif
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds /*
8401da177e4SLinus Torvalds  *  Mixer routines
8411da177e4SLinus Torvalds  */
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds #define FM801_SINGLE(xname, reg, shift, mask, invert) \
8441da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
8451da177e4SLinus Torvalds   .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
8461da177e4SLinus Torvalds   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
8471da177e4SLinus Torvalds 
snd_fm801_info_single(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)848a5f22156STakashi Iwai static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
849a5f22156STakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
8501da177e4SLinus Torvalds {
8511da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
8541da177e4SLinus Torvalds 	uinfo->count = 1;
8551da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
8561da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
8571da177e4SLinus Torvalds 	return 0;
8581da177e4SLinus Torvalds }
8591da177e4SLinus Torvalds 
snd_fm801_get_single(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)860a5f22156STakashi Iwai static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
861a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
8621da177e4SLinus Torvalds {
863a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
8641da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
8651da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
8661da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8671da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
8684b5c15f7SAndy Shevchenko 	long *value = ucontrol->value.integer.value;
8691da177e4SLinus Torvalds 
8704b5c15f7SAndy Shevchenko 	value[0] = (fm801_ioread16(chip, reg) >> shift) & mask;
8711da177e4SLinus Torvalds 	if (invert)
8724b5c15f7SAndy Shevchenko 		value[0] = mask - value[0];
8731da177e4SLinus Torvalds 	return 0;
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
snd_fm801_put_single(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)876a5f22156STakashi Iwai static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
877a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
8781da177e4SLinus Torvalds {
879a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
8801da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
8811da177e4SLinus Torvalds 	int shift = (kcontrol->private_value >> 8) & 0xff;
8821da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
8831da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
8841da177e4SLinus Torvalds 	unsigned short val;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
8871da177e4SLinus Torvalds 	if (invert)
8881da177e4SLinus Torvalds 		val = mask - val;
8891da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds #define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
8931da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
8941da177e4SLinus Torvalds   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
8951da177e4SLinus Torvalds   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
896666c70ffSTakashi Iwai #define FM801_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
897666c70ffSTakashi Iwai { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
898666c70ffSTakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
899666c70ffSTakashi Iwai   .name = xname, .info = snd_fm801_info_double, \
900666c70ffSTakashi Iwai   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
901666c70ffSTakashi Iwai   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
902666c70ffSTakashi Iwai   .tlv = { .p = (xtlv) } }
9031da177e4SLinus Torvalds 
snd_fm801_info_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)904a5f22156STakashi Iwai static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
905a5f22156STakashi Iwai 				 struct snd_ctl_elem_info *uinfo)
9061da177e4SLinus Torvalds {
9071da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
9101da177e4SLinus Torvalds 	uinfo->count = 2;
9111da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
9121da177e4SLinus Torvalds 	uinfo->value.integer.max = mask;
9131da177e4SLinus Torvalds 	return 0;
9141da177e4SLinus Torvalds }
9151da177e4SLinus Torvalds 
snd_fm801_get_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)916a5f22156STakashi Iwai static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
917a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
9181da177e4SLinus Torvalds {
919a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9201da177e4SLinus Torvalds         int reg = kcontrol->private_value & 0xff;
9211da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
9221da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
9231da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
9241da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
9254b5c15f7SAndy Shevchenko 	long *value = ucontrol->value.integer.value;
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9284b5c15f7SAndy Shevchenko 	value[0] = (fm801_ioread16(chip, reg) >> shift_left) & mask;
9294b5c15f7SAndy Shevchenko 	value[1] = (fm801_ioread16(chip, reg) >> shift_right) & mask;
9301da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9311da177e4SLinus Torvalds 	if (invert) {
9324b5c15f7SAndy Shevchenko 		value[0] = mask - value[0];
9334b5c15f7SAndy Shevchenko 		value[1] = mask - value[1];
9341da177e4SLinus Torvalds 	}
9351da177e4SLinus Torvalds 	return 0;
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
snd_fm801_put_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)938a5f22156STakashi Iwai static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
939a5f22156STakashi Iwai 				struct snd_ctl_elem_value *ucontrol)
9401da177e4SLinus Torvalds {
941a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9421da177e4SLinus Torvalds 	int reg = kcontrol->private_value & 0xff;
9431da177e4SLinus Torvalds 	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
9441da177e4SLinus Torvalds 	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
9451da177e4SLinus Torvalds 	int mask = (kcontrol->private_value >> 16) & 0xff;
9461da177e4SLinus Torvalds 	int invert = (kcontrol->private_value >> 24) & 0xff;
9471da177e4SLinus Torvalds 	unsigned short val1, val2;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
9501da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
9511da177e4SLinus Torvalds 	if (invert) {
9521da177e4SLinus Torvalds 		val1 = mask - val1;
9531da177e4SLinus Torvalds 		val2 = mask - val2;
9541da177e4SLinus Torvalds 	}
9551da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, reg,
9561da177e4SLinus Torvalds 				     (mask << shift_left) | (mask << shift_right),
9571da177e4SLinus Torvalds 				     (val1 << shift_left ) | (val2 << shift_right));
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
snd_fm801_info_mux(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)960a5f22156STakashi Iwai static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
961a5f22156STakashi Iwai 			      struct snd_ctl_elem_info *uinfo)
9621da177e4SLinus Torvalds {
963ca776a28STakashi Iwai 	static const char * const texts[5] = {
9641da177e4SLinus Torvalds 		"AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
9651da177e4SLinus Torvalds 	};
9661da177e4SLinus Torvalds 
967ca776a28STakashi Iwai 	return snd_ctl_enum_info(uinfo, 1, 5, texts);
9681da177e4SLinus Torvalds }
9691da177e4SLinus Torvalds 
snd_fm801_get_mux(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)970a5f22156STakashi Iwai static int snd_fm801_get_mux(struct snd_kcontrol *kcontrol,
971a5f22156STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
9721da177e4SLinus Torvalds {
973a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9741da177e4SLinus Torvalds         unsigned short val;
9751da177e4SLinus Torvalds 
976215dacc2SAndy Shevchenko 	val = fm801_readw(chip, REC_SRC) & 7;
9771da177e4SLinus Torvalds 	if (val > 4)
9781da177e4SLinus Torvalds 		val = 4;
9791da177e4SLinus Torvalds         ucontrol->value.enumerated.item[0] = val;
9801da177e4SLinus Torvalds         return 0;
9811da177e4SLinus Torvalds }
9821da177e4SLinus Torvalds 
snd_fm801_put_mux(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)983a5f22156STakashi Iwai static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
984a5f22156STakashi Iwai 			     struct snd_ctl_elem_value *ucontrol)
9851da177e4SLinus Torvalds {
986a5f22156STakashi Iwai 	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
9871da177e4SLinus Torvalds         unsigned short val;
9881da177e4SLinus Torvalds 
98968f441abSTakashi Iwai 	val = ucontrol->value.enumerated.item[0];
99068f441abSTakashi Iwai 	if (val > 4)
9911da177e4SLinus Torvalds                 return -EINVAL;
9921da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
9950cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
996666c70ffSTakashi Iwai 
997a5f22156STakashi Iwai #define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
9981da177e4SLinus Torvalds 
999b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_fm801_controls[] = {
1000666c70ffSTakashi Iwai FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
1001666c70ffSTakashi Iwai 		 db_scale_dsp),
10021da177e4SLinus Torvalds FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
1003666c70ffSTakashi Iwai FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
1004666c70ffSTakashi Iwai 		 db_scale_dsp),
10051da177e4SLinus Torvalds FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
1006666c70ffSTakashi Iwai FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
1007666c70ffSTakashi Iwai 		 db_scale_dsp),
10081da177e4SLinus Torvalds FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
10091da177e4SLinus Torvalds {
10101da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
10111da177e4SLinus Torvalds 	.name = "Digital Capture Source",
10121da177e4SLinus Torvalds 	.info = snd_fm801_info_mux,
10131da177e4SLinus Torvalds 	.get = snd_fm801_get_mux,
10141da177e4SLinus Torvalds 	.put = snd_fm801_put_mux,
10151da177e4SLinus Torvalds }
10161da177e4SLinus Torvalds };
10171da177e4SLinus Torvalds 
1018a5f22156STakashi Iwai #define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
10191da177e4SLinus Torvalds 
1020b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_fm801_controls_multi[] = {
10211da177e4SLinus Torvalds FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
10221da177e4SLinus Torvalds FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
102310e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
102410e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
102510e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
102610e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
10271da177e4SLinus Torvalds };
10281da177e4SLinus Torvalds 
snd_fm801_mixer(struct fm801 * chip)1029e23e7a14SBill Pemberton static int snd_fm801_mixer(struct fm801 *chip)
10301da177e4SLinus Torvalds {
1031a5f22156STakashi Iwai 	struct snd_ac97_template ac97;
10321da177e4SLinus Torvalds 	unsigned int i;
10331da177e4SLinus Torvalds 	int err;
103451055da5STakashi Iwai 	static const struct snd_ac97_bus_ops ops = {
10351da177e4SLinus Torvalds 		.write = snd_fm801_codec_write,
10361da177e4SLinus Torvalds 		.read = snd_fm801_codec_read,
10371da177e4SLinus Torvalds 	};
10381da177e4SLinus Torvalds 
103968f441abSTakashi Iwai 	err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
104068f441abSTakashi Iwai 	if (err < 0)
10411da177e4SLinus Torvalds 		return err;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
10441da177e4SLinus Torvalds 	ac97.private_data = chip;
104568f441abSTakashi Iwai 	err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
104668f441abSTakashi Iwai 	if (err < 0)
10471da177e4SLinus Torvalds 		return err;
10481da177e4SLinus Torvalds 	if (chip->secondary) {
10491da177e4SLinus Torvalds 		ac97.num = 1;
10501da177e4SLinus Torvalds 		ac97.addr = chip->secondary_addr;
105168f441abSTakashi Iwai 		err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec);
105268f441abSTakashi Iwai 		if (err < 0)
10531da177e4SLinus Torvalds 			return err;
10541da177e4SLinus Torvalds 	}
1055ef1ffbe7SZhouyang Jia 	for (i = 0; i < FM801_CONTROLS; i++) {
1056ef1ffbe7SZhouyang Jia 		err = snd_ctl_add(chip->card,
1057ef1ffbe7SZhouyang Jia 			snd_ctl_new1(&snd_fm801_controls[i], chip));
1058ef1ffbe7SZhouyang Jia 		if (err < 0)
1059ef1ffbe7SZhouyang Jia 			return err;
1060ef1ffbe7SZhouyang Jia 	}
10611da177e4SLinus Torvalds 	if (chip->multichannel) {
1062ef1ffbe7SZhouyang Jia 		for (i = 0; i < FM801_CONTROLS_MULTI; i++) {
1063ef1ffbe7SZhouyang Jia 			err = snd_ctl_add(chip->card,
1064ef1ffbe7SZhouyang Jia 				snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
1065ef1ffbe7SZhouyang Jia 			if (err < 0)
1066ef1ffbe7SZhouyang Jia 				return err;
1067ef1ffbe7SZhouyang Jia 		}
10681da177e4SLinus Torvalds 	}
10691da177e4SLinus Torvalds 	return 0;
10701da177e4SLinus Torvalds }
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds /*
10731da177e4SLinus Torvalds  *  initialization routines
10741da177e4SLinus Torvalds  */
10751da177e4SLinus Torvalds 
wait_for_codec(struct fm801 * chip,unsigned int codec_id,unsigned short reg,unsigned long waits)1076b1e9ed26STakashi Iwai static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
1077b1e9ed26STakashi Iwai 			  unsigned short reg, unsigned long waits)
1078b1e9ed26STakashi Iwai {
1079b1e9ed26STakashi Iwai 	unsigned long timeout = jiffies + waits;
1080b1e9ed26STakashi Iwai 
1081215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_CMD,
1082215dacc2SAndy Shevchenko 		     reg | (codec_id << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
1083b1e9ed26STakashi Iwai 	udelay(5);
1084b1e9ed26STakashi Iwai 	do {
1085215dacc2SAndy Shevchenko 		if ((fm801_readw(chip, AC97_CMD) &
1086215dacc2SAndy Shevchenko 		     (FM801_AC97_VALID | FM801_AC97_BUSY)) == FM801_AC97_VALID)
1087b1e9ed26STakashi Iwai 			return 0;
1088b1e9ed26STakashi Iwai 		schedule_timeout_uninterruptible(1);
1089b1e9ed26STakashi Iwai 	} while (time_after(timeout, jiffies));
1090b1e9ed26STakashi Iwai 	return -EIO;
1091b1e9ed26STakashi Iwai }
1092b1e9ed26STakashi Iwai 
reset_codec(struct fm801 * chip)1093b56fa687SAndy Shevchenko static int reset_codec(struct fm801 *chip)
1094b1e9ed26STakashi Iwai {
1095b1e9ed26STakashi Iwai 	/* codec cold reset + AC'97 warm reset */
1096215dacc2SAndy Shevchenko 	fm801_writew(chip, CODEC_CTRL, (1 << 5) | (1 << 6));
1097215dacc2SAndy Shevchenko 	fm801_readw(chip, CODEC_CTRL); /* flush posting data */
1098b1e9ed26STakashi Iwai 	udelay(100);
1099215dacc2SAndy Shevchenko 	fm801_writew(chip, CODEC_CTRL, 0);
1100b1e9ed26STakashi Iwai 
1101b56fa687SAndy Shevchenko 	return wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750));
1102b1e9ed26STakashi Iwai }
1103b1e9ed26STakashi Iwai 
snd_fm801_chip_multichannel_init(struct fm801 * chip)1104b56fa687SAndy Shevchenko static void snd_fm801_chip_multichannel_init(struct fm801 *chip)
1105b56fa687SAndy Shevchenko {
1106b56fa687SAndy Shevchenko 	unsigned short cmdw;
1107b56fa687SAndy Shevchenko 
1108b1e9ed26STakashi Iwai 	if (chip->multichannel) {
1109b1e9ed26STakashi Iwai 		if (chip->secondary_addr) {
1110b1e9ed26STakashi Iwai 			wait_for_codec(chip, chip->secondary_addr,
1111b1e9ed26STakashi Iwai 				       AC97_VENDOR_ID1, msecs_to_jiffies(50));
1112b1e9ed26STakashi Iwai 		} else {
1113b1e9ed26STakashi Iwai 			/* my card has the secondary codec */
1114b1e9ed26STakashi Iwai 			/* at address #3, so the loop is inverted */
111558e4334eSHarvey Harrison 			int i;
111658e4334eSHarvey Harrison 			for (i = 3; i > 0; i--) {
111758e4334eSHarvey Harrison 				if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
1118b1e9ed26STakashi Iwai 						     msecs_to_jiffies(50))) {
1119215dacc2SAndy Shevchenko 					cmdw = fm801_readw(chip, AC97_DATA);
1120b1e9ed26STakashi Iwai 					if (cmdw != 0xffff && cmdw != 0) {
1121b1e9ed26STakashi Iwai 						chip->secondary = 1;
112258e4334eSHarvey Harrison 						chip->secondary_addr = i;
1123b1e9ed26STakashi Iwai 						break;
1124b1e9ed26STakashi Iwai 					}
1125b1e9ed26STakashi Iwai 				}
1126b1e9ed26STakashi Iwai 			}
1127b1e9ed26STakashi Iwai 		}
1128b1e9ed26STakashi Iwai 
1129b1e9ed26STakashi Iwai 		/* the recovery phase, it seems that probing for non-existing codec might */
1130b1e9ed26STakashi Iwai 		/* cause timeout problems */
1131b1e9ed26STakashi Iwai 		wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
1132b1e9ed26STakashi Iwai 	}
1133b56fa687SAndy Shevchenko }
1134b1e9ed26STakashi Iwai 
snd_fm801_chip_init(struct fm801 * chip)1135b56fa687SAndy Shevchenko static void snd_fm801_chip_init(struct fm801 *chip)
1136b56fa687SAndy Shevchenko {
1137b56fa687SAndy Shevchenko 	unsigned short cmdw;
11386bbe13ecSJaroslav Kysela 
1139b1e9ed26STakashi Iwai 	/* init volume */
1140215dacc2SAndy Shevchenko 	fm801_writew(chip, PCM_VOL, 0x0808);
1141215dacc2SAndy Shevchenko 	fm801_writew(chip, FM_VOL, 0x9f1f);
1142215dacc2SAndy Shevchenko 	fm801_writew(chip, I2S_VOL, 0x8808);
1143b1e9ed26STakashi Iwai 
1144b1e9ed26STakashi Iwai 	/* I2S control - I2S mode */
1145215dacc2SAndy Shevchenko 	fm801_writew(chip, I2S_MODE, 0x0003);
1146b1e9ed26STakashi Iwai 
11476bbe13ecSJaroslav Kysela 	/* interrupt setup */
1148215dacc2SAndy Shevchenko 	cmdw = fm801_readw(chip, IRQ_MASK);
11496bbe13ecSJaroslav Kysela 	if (chip->irq < 0)
11506bbe13ecSJaroslav Kysela 		cmdw |= 0x00c3;		/* mask everything, no PCM nor MPU */
11516bbe13ecSJaroslav Kysela 	else
11526bbe13ecSJaroslav Kysela 		cmdw &= ~0x0083;	/* unmask MPU, PLAYBACK & CAPTURE */
1153215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_MASK, cmdw);
1154b1e9ed26STakashi Iwai 
1155b1e9ed26STakashi Iwai 	/* interrupt clear */
1156215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_STATUS,
1157215dacc2SAndy Shevchenko 		     FM801_IRQ_PLAYBACK | FM801_IRQ_CAPTURE | FM801_IRQ_MPU);
1158b1e9ed26STakashi Iwai }
1159b1e9ed26STakashi Iwai 
snd_fm801_free(struct snd_card * card)116047c41339STakashi Iwai static void snd_fm801_free(struct snd_card *card)
11611da177e4SLinus Torvalds {
116247c41339STakashi Iwai 	struct fm801 *chip = card->private_data;
11631da177e4SLinus Torvalds 	unsigned short cmdw;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	/* interrupt setup - mask everything */
1166215dacc2SAndy Shevchenko 	cmdw = fm801_readw(chip, IRQ_MASK);
11671da177e4SLinus Torvalds 	cmdw |= 0x00c3;
1168215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_MASK, cmdw);
11691da177e4SLinus Torvalds 
1170fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1171d4ecc83bSHans Verkuil 	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
11721da177e4SLinus Torvalds 		snd_tea575x_exit(&chip->tea);
1173d4ecc83bSHans Verkuil 		v4l2_device_unregister(&chip->v4l2_dev);
1174d4ecc83bSHans Verkuil 	}
11751da177e4SLinus Torvalds #endif
11761da177e4SLinus Torvalds }
11771da177e4SLinus Torvalds 
snd_fm801_create(struct snd_card * card,struct pci_dev * pci,int tea575x_tuner,int radio_nr)1178e23e7a14SBill Pemberton static int snd_fm801_create(struct snd_card *card,
11791da177e4SLinus Torvalds 			    struct pci_dev *pci,
11801da177e4SLinus Torvalds 			    int tea575x_tuner,
118147c41339STakashi Iwai 			    int radio_nr)
11821da177e4SLinus Torvalds {
118347c41339STakashi Iwai 	struct fm801 *chip = card->private_data;
11841da177e4SLinus Torvalds 	int err;
11851da177e4SLinus Torvalds 
118668f441abSTakashi Iwai 	err = pcim_enable_device(pci);
118768f441abSTakashi Iwai 	if (err < 0)
11881da177e4SLinus Torvalds 		return err;
11891da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
11901da177e4SLinus Torvalds 	chip->card = card;
1191d3d33aabSAndy Shevchenko 	chip->dev = &pci->dev;
11921da177e4SLinus Torvalds 	chip->irq = -1;
11936bbe13ecSJaroslav Kysela 	chip->tea575x_tuner = tea575x_tuner;
119468f441abSTakashi Iwai 	err = pci_request_regions(pci, "FM801");
119568f441abSTakashi Iwai 	if (err < 0)
11961da177e4SLinus Torvalds 		return err;
11971da177e4SLinus Torvalds 	chip->port = pci_resource_start(pci, 0);
1198b56fa687SAndy Shevchenko 
1199b56fa687SAndy Shevchenko 	if (pci->revision >= 0xb1)	/* FM801-AU */
1200b56fa687SAndy Shevchenko 		chip->multichannel = 1;
1201b56fa687SAndy Shevchenko 
1202b56fa687SAndy Shevchenko 	if (!(chip->tea575x_tuner & TUNER_ONLY)) {
1203b56fa687SAndy Shevchenko 		if (reset_codec(chip) < 0) {
1204b56fa687SAndy Shevchenko 			dev_info(chip->card->dev,
1205b56fa687SAndy Shevchenko 				 "Primary AC'97 codec not found, assume SF64-PCR (tuner-only)\n");
1206b56fa687SAndy Shevchenko 			chip->tea575x_tuner = 3 | TUNER_ONLY;
1207b56fa687SAndy Shevchenko 		} else {
1208b56fa687SAndy Shevchenko 			snd_fm801_chip_multichannel_init(chip);
1209b56fa687SAndy Shevchenko 		}
1210b56fa687SAndy Shevchenko 	}
1211b56fa687SAndy Shevchenko 
1212b56fa687SAndy Shevchenko 	if ((chip->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 			return -EBUSY;
12171da177e4SLinus Torvalds 		}
12181da177e4SLinus Torvalds 		chip->irq = pci->irq;
1219e41dbd20STakashi Iwai 		card->sync_irq = chip->irq;
12201da177e4SLinus Torvalds 		pci_set_master(pci);
12216bbe13ecSJaroslav Kysela 	}
12221da177e4SLinus Torvalds 
122347c41339STakashi Iwai 	card->private_free = snd_fm801_free;
1224610e1ae9SAndy Shevchenko 	snd_fm801_chip_init(chip);
1225610e1ae9SAndy Shevchenko 
1226fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1227d4ecc83bSHans Verkuil 	err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
122847c41339STakashi Iwai 	if (err < 0)
1229d4ecc83bSHans Verkuil 		return err;
1230d4ecc83bSHans Verkuil 	chip->tea.v4l2_dev = &chip->v4l2_dev;
1231d4ecc83bSHans Verkuil 	chip->tea.radio_nr = radio_nr;
12321da177e4SLinus Torvalds 	chip->tea.private_data = chip;
1233938a1566SOndrej Zary 	chip->tea.ops = &snd_fm801_tea_ops;
123410ca7201SOndrej Zary 	sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
1235b56fa687SAndy Shevchenko 	if ((chip->tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
1236b56fa687SAndy Shevchenko 	    (chip->tea575x_tuner & TUNER_TYPE_MASK) < 4) {
12375daf53a6SHans de Goede 		if (snd_tea575x_init(&chip->tea, THIS_MODULE)) {
12389c7f9abfSTakashi Iwai 			dev_err(card->dev, "TEA575x radio not found\n");
123996760015SDan Carpenter 			return -ENODEV;
124096760015SDan Carpenter 		}
1241b56fa687SAndy Shevchenko 	} else if ((chip->tea575x_tuner & TUNER_TYPE_MASK) == 0) {
1242b56fa687SAndy Shevchenko 		unsigned int tuner_only = chip->tea575x_tuner & TUNER_ONLY;
1243dbec6719SAndy Shevchenko 
1244d7ba858aSOndrej Zary 		/* autodetect tuner connection */
1245d7ba858aSOndrej Zary 		for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
1246d7ba858aSOndrej Zary 			chip->tea575x_tuner = tea575x_tuner;
12475daf53a6SHans de Goede 			if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
12489c7f9abfSTakashi Iwai 				dev_info(card->dev,
12499c7f9abfSTakashi Iwai 					 "detected TEA575x radio type %s\n",
12508e699d2cSTakashi Iwai 					   get_tea575x_gpio(chip)->name);
1251d7ba858aSOndrej Zary 				break;
1252d7ba858aSOndrej Zary 			}
12531da177e4SLinus Torvalds 		}
125496760015SDan Carpenter 		if (tea575x_tuner == 4) {
12559c7f9abfSTakashi Iwai 			dev_err(card->dev, "TEA575x radio not found\n");
1256c37279b9SBen Hutchings 			chip->tea575x_tuner = TUNER_DISABLED;
125796760015SDan Carpenter 		}
1258dbec6719SAndy Shevchenko 
1259dbec6719SAndy Shevchenko 		chip->tea575x_tuner |= tuner_only;
126096760015SDan Carpenter 	}
1261c37279b9SBen Hutchings 	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
126275b1a8f9SJoe Perches 		strscpy(chip->tea.card, get_tea575x_gpio(chip)->name,
1263c37279b9SBen Hutchings 			sizeof(chip->tea.card));
1264c37279b9SBen Hutchings 	}
12651da177e4SLinus Torvalds #endif
12661da177e4SLinus Torvalds 	return 0;
12671da177e4SLinus Torvalds }
12681da177e4SLinus Torvalds 
__snd_card_fm801_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)12697f611274STakashi Iwai static int __snd_card_fm801_probe(struct pci_dev *pci,
12701da177e4SLinus Torvalds 				  const struct pci_device_id *pci_id)
12711da177e4SLinus Torvalds {
12721da177e4SLinus Torvalds 	static int dev;
1273a5f22156STakashi Iwai 	struct snd_card *card;
1274a5f22156STakashi Iwai 	struct fm801 *chip;
1275a5f22156STakashi Iwai 	struct snd_opl3 *opl3;
12761da177e4SLinus Torvalds 	int err;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds         if (dev >= SNDRV_CARDS)
12791da177e4SLinus Torvalds                 return -ENODEV;
12801da177e4SLinus Torvalds 	if (!enable[dev]) {
12811da177e4SLinus Torvalds 		dev++;
12821da177e4SLinus Torvalds 		return -ENOENT;
12831da177e4SLinus Torvalds 	}
12841da177e4SLinus Torvalds 
128547c41339STakashi Iwai 	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
128647c41339STakashi Iwai 				sizeof(*chip), &card);
1287e58de7baSTakashi Iwai 	if (err < 0)
1288e58de7baSTakashi Iwai 		return err;
128947c41339STakashi Iwai 	chip = card->private_data;
129047c41339STakashi Iwai 	err = snd_fm801_create(card, pci, tea575x_tuner[dev], radio_nr[dev]);
129147c41339STakashi Iwai 	if (err < 0)
12921da177e4SLinus Torvalds 		return err;
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 	strcpy(card->driver, "FM801");
12951da177e4SLinus Torvalds 	strcpy(card->shortname, "ForteMedia FM801-");
12961da177e4SLinus Torvalds 	strcat(card->shortname, chip->multichannel ? "AU" : "AS");
12971da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %i",
12981da177e4SLinus Torvalds 		card->shortname, chip->port, chip->irq);
12991da177e4SLinus Torvalds 
1300fb716c0bSOndrej Zary 	if (chip->tea575x_tuner & TUNER_ONLY)
1301e0a5d82aSAndy Shevchenko 		goto __fm801_tuner_only;
1302e0a5d82aSAndy Shevchenko 
130368f441abSTakashi Iwai 	err = snd_fm801_pcm(chip, 0);
130447c41339STakashi Iwai 	if (err < 0)
13051da177e4SLinus Torvalds 		return err;
130668f441abSTakashi Iwai 	err = snd_fm801_mixer(chip);
130747c41339STakashi Iwai 	if (err < 0)
13081da177e4SLinus Torvalds 		return err;
130968f441abSTakashi Iwai 	err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1310215dacc2SAndy Shevchenko 				  chip->port + FM801_MPU401_DATA,
1311dba8b469SClemens Ladisch 				  MPU401_INFO_INTEGRATED |
1312dba8b469SClemens Ladisch 				  MPU401_INFO_IRQ_HOOK,
131368f441abSTakashi Iwai 				  -1, &chip->rmidi);
131447c41339STakashi Iwai 	if (err < 0)
13151da177e4SLinus Torvalds 		return err;
131668f441abSTakashi Iwai 	err = snd_opl3_create(card, chip->port + FM801_OPL3_BANK0,
1317215dacc2SAndy Shevchenko 			      chip->port + FM801_OPL3_BANK1,
131868f441abSTakashi Iwai 			      OPL3_HW_OPL3_FM801, 1, &opl3);
131947c41339STakashi Iwai 	if (err < 0)
13201da177e4SLinus Torvalds 		return err;
132168f441abSTakashi Iwai 	err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
132247c41339STakashi Iwai 	if (err < 0)
13231da177e4SLinus Torvalds 		return err;
13241da177e4SLinus Torvalds 
1325e0a5d82aSAndy Shevchenko       __fm801_tuner_only:
132668f441abSTakashi Iwai 	err = snd_card_register(card);
132747c41339STakashi Iwai 	if (err < 0)
13281da177e4SLinus Torvalds 		return err;
13291da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
13301da177e4SLinus Torvalds 	dev++;
13311da177e4SLinus Torvalds 	return 0;
13321da177e4SLinus Torvalds }
13331da177e4SLinus Torvalds 
snd_card_fm801_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)13347f611274STakashi Iwai static int snd_card_fm801_probe(struct pci_dev *pci,
13357f611274STakashi Iwai 				const struct pci_device_id *pci_id)
13367f611274STakashi Iwai {
13377f611274STakashi Iwai 	return snd_card_free_on_error(&pci->dev, __snd_card_fm801_probe(pci, pci_id));
13387f611274STakashi Iwai }
13397f611274STakashi Iwai 
13408045d0fcSTakashi Iwai static const unsigned char saved_regs[] = {
1341b1e9ed26STakashi Iwai 	FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
1342b1e9ed26STakashi Iwai 	FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
1343b1e9ed26STakashi Iwai 	FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
1344b1e9ed26STakashi Iwai 	FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
1345b1e9ed26STakashi Iwai };
1346b1e9ed26STakashi Iwai 
snd_fm801_suspend(struct device * dev)134768cb2b55STakashi Iwai static int snd_fm801_suspend(struct device *dev)
1348b1e9ed26STakashi Iwai {
134968cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1350b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1351b1e9ed26STakashi Iwai 	int i;
1352b1e9ed26STakashi Iwai 
1353b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
135414da04b5SAndy Shevchenko 
135537ba8fcaSAndy Shevchenko 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
135637ba8fcaSAndy Shevchenko 		chip->saved_regs[i] = fm801_ioread16(chip, saved_regs[i]);
135737ba8fcaSAndy Shevchenko 
135814da04b5SAndy Shevchenko 	if (chip->tea575x_tuner & TUNER_ONLY) {
135914da04b5SAndy Shevchenko 		/* FIXME: tea575x suspend */
136014da04b5SAndy Shevchenko 	} else {
1361b1e9ed26STakashi Iwai 		snd_ac97_suspend(chip->ac97);
1362b1e9ed26STakashi Iwai 		snd_ac97_suspend(chip->ac97_sec);
136314da04b5SAndy Shevchenko 	}
136414da04b5SAndy Shevchenko 
1365b1e9ed26STakashi Iwai 	return 0;
1366b1e9ed26STakashi Iwai }
1367b1e9ed26STakashi Iwai 
snd_fm801_resume(struct device * dev)136868cb2b55STakashi Iwai static int snd_fm801_resume(struct device *dev)
1369b1e9ed26STakashi Iwai {
137068cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1371b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1372b1e9ed26STakashi Iwai 	int i;
1373b1e9ed26STakashi Iwai 
1374b56fa687SAndy Shevchenko 	if (chip->tea575x_tuner & TUNER_ONLY) {
1375b56fa687SAndy Shevchenko 		snd_fm801_chip_init(chip);
1376b56fa687SAndy Shevchenko 	} else {
1377b56fa687SAndy Shevchenko 		reset_codec(chip);
1378b56fa687SAndy Shevchenko 		snd_fm801_chip_multichannel_init(chip);
1379b56fa687SAndy Shevchenko 		snd_fm801_chip_init(chip);
1380b1e9ed26STakashi Iwai 		snd_ac97_resume(chip->ac97);
1381b1e9ed26STakashi Iwai 		snd_ac97_resume(chip->ac97_sec);
138214da04b5SAndy Shevchenko 	}
138314da04b5SAndy Shevchenko 
1384b1e9ed26STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
13854b5c15f7SAndy Shevchenko 		fm801_iowrite16(chip, saved_regs[i], chip->saved_regs[i]);
1386b1e9ed26STakashi Iwai 
1387cb41f271SAndy Shevchenko #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1388cb41f271SAndy Shevchenko 	if (!(chip->tea575x_tuner & TUNER_DISABLED))
1389cb41f271SAndy Shevchenko 		snd_tea575x_set_freq(&chip->tea);
1390cb41f271SAndy Shevchenko #endif
1391b1e9ed26STakashi Iwai 
1392b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1393b1e9ed26STakashi Iwai 	return 0;
1394b1e9ed26STakashi Iwai }
139568cb2b55STakashi Iwai 
1396*e6c2f5ecSTakashi Iwai static DEFINE_SIMPLE_DEV_PM_OPS(snd_fm801_pm, snd_fm801_suspend, snd_fm801_resume);
1397b1e9ed26STakashi Iwai 
1398e9f66d9bSTakashi Iwai static struct pci_driver fm801_driver = {
13993733e424STakashi Iwai 	.name = KBUILD_MODNAME,
14001da177e4SLinus Torvalds 	.id_table = snd_fm801_ids,
14011da177e4SLinus Torvalds 	.probe = snd_card_fm801_probe,
140268cb2b55STakashi Iwai 	.driver = {
1403*e6c2f5ecSTakashi Iwai 		.pm = &snd_fm801_pm,
140468cb2b55STakashi Iwai 	},
14051da177e4SLinus Torvalds };
14061da177e4SLinus Torvalds 
1407e9f66d9bSTakashi Iwai module_pci_driver(fm801_driver);
1408