xref: /openbmc/linux/sound/pci/fm801.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
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 
225c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
226b1e9ed26STakashi Iwai 	u16 saved_regs[0x20];
227b1e9ed26STakashi Iwai #endif
2281da177e4SLinus Torvalds };
2291da177e4SLinus Torvalds 
2304b5c15f7SAndy Shevchenko /*
2314b5c15f7SAndy Shevchenko  * IO accessors
2324b5c15f7SAndy Shevchenko  */
2334b5c15f7SAndy Shevchenko 
fm801_iowrite16(struct fm801 * chip,unsigned short offset,u16 value)2344b5c15f7SAndy Shevchenko static inline void fm801_iowrite16(struct fm801 *chip, unsigned short offset, u16 value)
2354b5c15f7SAndy Shevchenko {
2364b5c15f7SAndy Shevchenko 	outw(value, chip->port + offset);
2374b5c15f7SAndy Shevchenko }
2384b5c15f7SAndy Shevchenko 
fm801_ioread16(struct fm801 * chip,unsigned short offset)2394b5c15f7SAndy Shevchenko static inline u16 fm801_ioread16(struct fm801 *chip, unsigned short offset)
2404b5c15f7SAndy Shevchenko {
2414b5c15f7SAndy Shevchenko 	return inw(chip->port + offset);
2424b5c15f7SAndy Shevchenko }
2434b5c15f7SAndy Shevchenko 
2449baa3c34SBenoit Taine static const struct pci_device_id snd_fm801_ids[] = {
2451da177e4SLinus Torvalds 	{ 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* FM801 */
24626be8659SSergey Vlasov 	{ 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* Gallant Odyssey Sound 4 */
2471da177e4SLinus Torvalds 	{ 0, }
2481da177e4SLinus Torvalds };
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds /*
2531da177e4SLinus Torvalds  *  common I/O routines
2541da177e4SLinus Torvalds  */
2551da177e4SLinus Torvalds 
fm801_ac97_is_ready(struct fm801 * chip,unsigned int iterations)25602fd1a76SAndy Shevchenko static bool fm801_ac97_is_ready(struct fm801 *chip, unsigned int iterations)
25702fd1a76SAndy Shevchenko {
25802fd1a76SAndy Shevchenko 	unsigned int idx;
25902fd1a76SAndy Shevchenko 
26002fd1a76SAndy Shevchenko 	for (idx = 0; idx < iterations; idx++) {
26102fd1a76SAndy Shevchenko 		if (!(fm801_readw(chip, AC97_CMD) & FM801_AC97_BUSY))
26202fd1a76SAndy Shevchenko 			return true;
26302fd1a76SAndy Shevchenko 		udelay(10);
26402fd1a76SAndy Shevchenko 	}
26502fd1a76SAndy Shevchenko 	return false;
26602fd1a76SAndy Shevchenko }
26702fd1a76SAndy Shevchenko 
fm801_ac97_is_valid(struct fm801 * chip,unsigned int iterations)26802fd1a76SAndy Shevchenko static bool fm801_ac97_is_valid(struct fm801 *chip, unsigned int iterations)
26902fd1a76SAndy Shevchenko {
27002fd1a76SAndy Shevchenko 	unsigned int idx;
27102fd1a76SAndy Shevchenko 
27202fd1a76SAndy Shevchenko 	for (idx = 0; idx < iterations; idx++) {
27302fd1a76SAndy Shevchenko 		if (fm801_readw(chip, AC97_CMD) & FM801_AC97_VALID)
27402fd1a76SAndy Shevchenko 			return true;
27502fd1a76SAndy Shevchenko 		udelay(10);
27602fd1a76SAndy Shevchenko 	}
27702fd1a76SAndy Shevchenko 	return false;
27802fd1a76SAndy Shevchenko }
27902fd1a76SAndy Shevchenko 
snd_fm801_update_bits(struct fm801 * chip,unsigned short reg,unsigned short mask,unsigned short value)280a5f22156STakashi Iwai static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
2811da177e4SLinus Torvalds 				 unsigned short mask, unsigned short value)
2821da177e4SLinus Torvalds {
2831da177e4SLinus Torvalds 	int change;
2841da177e4SLinus Torvalds 	unsigned long flags;
2851da177e4SLinus Torvalds 	unsigned short old, new;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
2884b5c15f7SAndy Shevchenko 	old = fm801_ioread16(chip, reg);
2891da177e4SLinus Torvalds 	new = (old & ~mask) | value;
2901da177e4SLinus Torvalds 	change = old != new;
2911da177e4SLinus Torvalds 	if (change)
2924b5c15f7SAndy Shevchenko 		fm801_iowrite16(chip, reg, new);
2931da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
2941da177e4SLinus Torvalds 	return change;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
snd_fm801_codec_write(struct snd_ac97 * ac97,unsigned short reg,unsigned short val)297a5f22156STakashi Iwai static void snd_fm801_codec_write(struct snd_ac97 *ac97,
2981da177e4SLinus Torvalds 				  unsigned short reg,
2991da177e4SLinus Torvalds 				  unsigned short val)
3001da177e4SLinus Torvalds {
301a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	/*
3041da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
3051da177e4SLinus Torvalds 	 */
30602fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 100)) {
3079c7f9abfSTakashi Iwai 		dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
3081da177e4SLinus Torvalds 		return;
30902fd1a76SAndy Shevchenko 	}
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/* write data and address */
312215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_DATA, val);
313215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_CMD, reg | (ac97->addr << FM801_AC97_ADDR_SHIFT));
3141da177e4SLinus Torvalds 	/*
3151da177e4SLinus Torvalds 	 *  Wait until the write command is not completed..
3161da177e4SLinus Torvalds 	 */
31702fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 1000))
31802fd1a76SAndy Shevchenko 		dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
31902fd1a76SAndy Shevchenko 		ac97->num);
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
snd_fm801_codec_read(struct snd_ac97 * ac97,unsigned short reg)322a5f22156STakashi Iwai static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
3231da177e4SLinus Torvalds {
324a5f22156STakashi Iwai 	struct fm801 *chip = ac97->private_data;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/*
3271da177e4SLinus Torvalds 	 *  Wait until the codec interface is not ready..
3281da177e4SLinus Torvalds 	 */
32902fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 100)) {
3309c7f9abfSTakashi Iwai 		dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
3311da177e4SLinus Torvalds 		return 0;
33202fd1a76SAndy Shevchenko 	}
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	/* read command */
335215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_CMD,
336215dacc2SAndy Shevchenko 		     reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
33702fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_ready(chip, 100)) {
33802fd1a76SAndy Shevchenko 		dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
33902fd1a76SAndy Shevchenko 			ac97->num);
3401da177e4SLinus Torvalds 		return 0;
3411da177e4SLinus Torvalds 	}
3421da177e4SLinus Torvalds 
34302fd1a76SAndy Shevchenko 	if (!fm801_ac97_is_valid(chip, 1000)) {
34402fd1a76SAndy Shevchenko 		dev_err(chip->card->dev,
34502fd1a76SAndy Shevchenko 			"AC'97 interface #%d is not valid (2)\n", ac97->num);
34602fd1a76SAndy Shevchenko 		return 0;
34702fd1a76SAndy Shevchenko 	}
34802fd1a76SAndy Shevchenko 
349215dacc2SAndy Shevchenko 	return fm801_readw(chip, AC97_DATA);
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
352d71a13f4STakashi Iwai static const unsigned int rates[] = {
3531da177e4SLinus Torvalds   5500,  8000,  9600, 11025,
3541da177e4SLinus Torvalds   16000, 19200, 22050, 32000,
3551da177e4SLinus Torvalds   38400, 44100, 48000
3561da177e4SLinus Torvalds };
3571da177e4SLinus Torvalds 
358d71a13f4STakashi Iwai static const struct snd_pcm_hw_constraint_list hw_constraints_rates = {
3591da177e4SLinus Torvalds 	.count = ARRAY_SIZE(rates),
3601da177e4SLinus Torvalds 	.list = rates,
3611da177e4SLinus Torvalds 	.mask = 0,
3621da177e4SLinus Torvalds };
3631da177e4SLinus Torvalds 
364d71a13f4STakashi Iwai static const unsigned int channels[] = {
3651da177e4SLinus Torvalds   2, 4, 6
3661da177e4SLinus Torvalds };
3671da177e4SLinus Torvalds 
368d71a13f4STakashi Iwai static const struct snd_pcm_hw_constraint_list hw_constraints_channels = {
3695e4968e2STobias Klauser 	.count = ARRAY_SIZE(channels),
3701da177e4SLinus Torvalds 	.list = channels,
3711da177e4SLinus Torvalds 	.mask = 0,
3721da177e4SLinus Torvalds };
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds /*
3751da177e4SLinus Torvalds  *  Sample rate routines
3761da177e4SLinus Torvalds  */
3771da177e4SLinus Torvalds 
snd_fm801_rate_bits(unsigned int rate)3781da177e4SLinus Torvalds static unsigned short snd_fm801_rate_bits(unsigned int rate)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	unsigned int idx;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
3831da177e4SLinus Torvalds 		if (rates[idx] == rate)
3841da177e4SLinus Torvalds 			return idx;
3851da177e4SLinus Torvalds 	snd_BUG();
3861da177e4SLinus Torvalds 	return ARRAY_SIZE(rates) - 1;
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds /*
3901da177e4SLinus Torvalds  *  PCM part
3911da177e4SLinus Torvalds  */
3921da177e4SLinus Torvalds 
snd_fm801_playback_trigger(struct snd_pcm_substream * substream,int cmd)393a5f22156STakashi Iwai static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
3941da177e4SLinus Torvalds 				      int cmd)
3951da177e4SLinus Torvalds {
396a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3991da177e4SLinus Torvalds 	switch (cmd) {
4001da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4011da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_BUF1_LAST |
4021da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
4031da177e4SLinus Torvalds 				     FM801_PAUSE);
4041da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_START |
4051da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
4061da177e4SLinus Torvalds 		break;
4071da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4081da177e4SLinus Torvalds 		chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
4091da177e4SLinus Torvalds 		break;
4101da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
411b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
4121da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_PAUSE;
4131da177e4SLinus Torvalds 		break;
4141da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
415b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
4161da177e4SLinus Torvalds 		chip->ply_ctrl &= ~FM801_PAUSE;
4171da177e4SLinus Torvalds 		break;
4181da177e4SLinus Torvalds 	default:
4191da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
4201da177e4SLinus Torvalds 		snd_BUG();
4211da177e4SLinus Torvalds 		return -EINVAL;
4221da177e4SLinus Torvalds 	}
423215dacc2SAndy Shevchenko 	fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
4241da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4251da177e4SLinus Torvalds 	return 0;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
snd_fm801_capture_trigger(struct snd_pcm_substream * substream,int cmd)428a5f22156STakashi Iwai static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
4291da177e4SLinus Torvalds 				     int cmd)
4301da177e4SLinus Torvalds {
431a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
4341da177e4SLinus Torvalds 	switch (cmd) {
4351da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4361da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_BUF1_LAST |
4371da177e4SLinus Torvalds 				     FM801_BUF2_LAST |
4381da177e4SLinus Torvalds 				     FM801_PAUSE);
4391da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_START |
4401da177e4SLinus Torvalds 				   FM801_IMMED_STOP;
4411da177e4SLinus Torvalds 		break;
4421da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4431da177e4SLinus Torvalds 		chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
4441da177e4SLinus Torvalds 		break;
4451da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
446b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_SUSPEND:
4471da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_PAUSE;
4481da177e4SLinus Torvalds 		break;
4491da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
450b1e9ed26STakashi Iwai 	case SNDRV_PCM_TRIGGER_RESUME:
4511da177e4SLinus Torvalds 		chip->cap_ctrl &= ~FM801_PAUSE;
4521da177e4SLinus Torvalds 		break;
4531da177e4SLinus Torvalds 	default:
4541da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
4551da177e4SLinus Torvalds 		snd_BUG();
4561da177e4SLinus Torvalds 		return -EINVAL;
4571da177e4SLinus Torvalds 	}
458215dacc2SAndy Shevchenko 	fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
4591da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4601da177e4SLinus Torvalds 	return 0;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
snd_fm801_playback_prepare(struct snd_pcm_substream * substream)463a5f22156STakashi Iwai static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
4641da177e4SLinus Torvalds {
465a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
466a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
4691da177e4SLinus Torvalds 	chip->ply_count = snd_pcm_lib_period_bytes(substream);
4701da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
4711da177e4SLinus Torvalds 	chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
4721da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK |
4731da177e4SLinus Torvalds 			     FM801_CHANNELS_MASK);
4741da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
4751da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_16BIT;
4761da177e4SLinus Torvalds 	if (runtime->channels > 1) {
4771da177e4SLinus Torvalds 		chip->ply_ctrl |= FM801_STEREO;
4781da177e4SLinus Torvalds 		if (runtime->channels == 4)
4791da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_4;
4801da177e4SLinus Torvalds 		else if (runtime->channels == 6)
4811da177e4SLinus Torvalds 			chip->ply_ctrl |= FM801_CHANNELS_6;
4821da177e4SLinus Torvalds 	}
4831da177e4SLinus Torvalds 	chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
4841da177e4SLinus Torvalds 	chip->ply_buf = 0;
485215dacc2SAndy Shevchenko 	fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
486215dacc2SAndy Shevchenko 	fm801_writew(chip, PLY_COUNT, chip->ply_count - 1);
4871da177e4SLinus Torvalds 	chip->ply_buffer = runtime->dma_addr;
4881da177e4SLinus Torvalds 	chip->ply_pos = 0;
489215dacc2SAndy Shevchenko 	fm801_writel(chip, PLY_BUF1, chip->ply_buffer);
490215dacc2SAndy Shevchenko 	fm801_writel(chip, PLY_BUF2,
491215dacc2SAndy Shevchenko 		     chip->ply_buffer + (chip->ply_count % chip->ply_size));
4921da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
4931da177e4SLinus Torvalds 	return 0;
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
snd_fm801_capture_prepare(struct snd_pcm_substream * substream)496a5f22156STakashi Iwai static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
4971da177e4SLinus Torvalds {
498a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
499a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
5021da177e4SLinus Torvalds 	chip->cap_count = snd_pcm_lib_period_bytes(substream);
5031da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
5041da177e4SLinus Torvalds 	chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
5051da177e4SLinus Torvalds 			     FM801_STEREO | FM801_RATE_MASK);
5061da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 16)
5071da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_16BIT;
5081da177e4SLinus Torvalds 	if (runtime->channels > 1)
5091da177e4SLinus Torvalds 		chip->cap_ctrl |= FM801_STEREO;
5101da177e4SLinus Torvalds 	chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
5111da177e4SLinus Torvalds 	chip->cap_buf = 0;
512215dacc2SAndy Shevchenko 	fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
513215dacc2SAndy Shevchenko 	fm801_writew(chip, CAP_COUNT, chip->cap_count - 1);
5141da177e4SLinus Torvalds 	chip->cap_buffer = runtime->dma_addr;
5151da177e4SLinus Torvalds 	chip->cap_pos = 0;
516215dacc2SAndy Shevchenko 	fm801_writel(chip, CAP_BUF1, chip->cap_buffer);
517215dacc2SAndy Shevchenko 	fm801_writel(chip, CAP_BUF2,
518215dacc2SAndy Shevchenko 		     chip->cap_buffer + (chip->cap_count % chip->cap_size));
5191da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
5201da177e4SLinus Torvalds 	return 0;
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
snd_fm801_playback_pointer(struct snd_pcm_substream * substream)523a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
5241da177e4SLinus Torvalds {
525a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
5261da177e4SLinus Torvalds 	size_t ptr;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	if (!(chip->ply_ctrl & FM801_START))
5291da177e4SLinus Torvalds 		return 0;
5301da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
531215dacc2SAndy Shevchenko 	ptr = chip->ply_pos + (chip->ply_count - 1) - fm801_readw(chip, PLY_COUNT);
532215dacc2SAndy Shevchenko 	if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_PLAYBACK) {
5331da177e4SLinus Torvalds 		ptr += chip->ply_count;
5341da177e4SLinus Torvalds 		ptr %= chip->ply_size;
5351da177e4SLinus Torvalds 	}
5361da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5371da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
snd_fm801_capture_pointer(struct snd_pcm_substream * substream)540a5f22156STakashi Iwai static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
5411da177e4SLinus Torvalds {
542a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
5431da177e4SLinus Torvalds 	size_t ptr;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	if (!(chip->cap_ctrl & FM801_START))
5461da177e4SLinus Torvalds 		return 0;
5471da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
548215dacc2SAndy Shevchenko 	ptr = chip->cap_pos + (chip->cap_count - 1) - fm801_readw(chip, CAP_COUNT);
549215dacc2SAndy Shevchenko 	if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_CAPTURE) {
5501da177e4SLinus Torvalds 		ptr += chip->cap_count;
5511da177e4SLinus Torvalds 		ptr %= chip->cap_size;
5521da177e4SLinus Torvalds 	}
5531da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
5541da177e4SLinus Torvalds 	return bytes_to_frames(substream->runtime, ptr);
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
snd_fm801_interrupt(int irq,void * dev_id)5577d12e780SDavid Howells static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
5581da177e4SLinus Torvalds {
559a5f22156STakashi Iwai 	struct fm801 *chip = dev_id;
5601da177e4SLinus Torvalds 	unsigned short status;
5611da177e4SLinus Torvalds 	unsigned int tmp;
5621da177e4SLinus Torvalds 
563215dacc2SAndy Shevchenko 	status = fm801_readw(chip, IRQ_STATUS);
5641da177e4SLinus Torvalds 	status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
5651da177e4SLinus Torvalds 	if (! status)
5661da177e4SLinus Torvalds 		return IRQ_NONE;
5671da177e4SLinus Torvalds 	/* ack first */
568215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_STATUS, status);
5691da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
5701da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5711da177e4SLinus Torvalds 		chip->ply_buf++;
5721da177e4SLinus Torvalds 		chip->ply_pos += chip->ply_count;
5731da177e4SLinus Torvalds 		chip->ply_pos %= chip->ply_size;
5741da177e4SLinus Torvalds 		tmp = chip->ply_pos + chip->ply_count;
5751da177e4SLinus Torvalds 		tmp %= chip->ply_size;
576215dacc2SAndy Shevchenko 		if (chip->ply_buf & 1)
577215dacc2SAndy Shevchenko 			fm801_writel(chip, PLY_BUF1, chip->ply_buffer + tmp);
578215dacc2SAndy Shevchenko 		else
579215dacc2SAndy Shevchenko 			fm801_writel(chip, PLY_BUF2, chip->ply_buffer + tmp);
5801da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5811da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->playback_substream);
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds 	if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
5841da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
5851da177e4SLinus Torvalds 		chip->cap_buf++;
5861da177e4SLinus Torvalds 		chip->cap_pos += chip->cap_count;
5871da177e4SLinus Torvalds 		chip->cap_pos %= chip->cap_size;
5881da177e4SLinus Torvalds 		tmp = chip->cap_pos + chip->cap_count;
5891da177e4SLinus Torvalds 		tmp %= chip->cap_size;
590215dacc2SAndy Shevchenko 		if (chip->cap_buf & 1)
591215dacc2SAndy Shevchenko 			fm801_writel(chip, CAP_BUF1, chip->cap_buffer + tmp);
592215dacc2SAndy Shevchenko 		else
593215dacc2SAndy Shevchenko 			fm801_writel(chip, CAP_BUF2, chip->cap_buffer + tmp);
5941da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
5951da177e4SLinus Torvalds 		snd_pcm_period_elapsed(chip->capture_substream);
5961da177e4SLinus Torvalds 	}
5971da177e4SLinus Torvalds 	if (chip->rmidi && (status & FM801_IRQ_MPU))
5987d12e780SDavid Howells 		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
599997c87daSAndy Shevchenko 	if (status & FM801_IRQ_VOLUME) {
600997c87daSAndy Shevchenko 		/* TODO */
601997c87daSAndy Shevchenko 	}
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	return IRQ_HANDLED;
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
606dee49895SBhumika Goyal static const struct snd_pcm_hardware snd_fm801_playback =
6071da177e4SLinus Torvalds {
6081da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
6091da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
610b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
6111da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
6121da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
6131da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
6141da177e4SLinus Torvalds 	.rate_min =		5500,
6151da177e4SLinus Torvalds 	.rate_max =		48000,
6161da177e4SLinus Torvalds 	.channels_min =		1,
6171da177e4SLinus Torvalds 	.channels_max =		2,
6181da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
6191da177e4SLinus Torvalds 	.period_bytes_min =	64,
6201da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
6211da177e4SLinus Torvalds 	.periods_min =		1,
6221da177e4SLinus Torvalds 	.periods_max =		1024,
6231da177e4SLinus Torvalds 	.fifo_size =		0,
6241da177e4SLinus Torvalds };
6251da177e4SLinus Torvalds 
626dee49895SBhumika Goyal static const struct snd_pcm_hardware snd_fm801_capture =
6271da177e4SLinus Torvalds {
6281da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
6291da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
630b1e9ed26STakashi Iwai 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
6311da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID),
6321da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
6331da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
6341da177e4SLinus Torvalds 	.rate_min =		5500,
6351da177e4SLinus Torvalds 	.rate_max =		48000,
6361da177e4SLinus Torvalds 	.channels_min =		1,
6371da177e4SLinus Torvalds 	.channels_max =		2,
6381da177e4SLinus Torvalds 	.buffer_bytes_max =	(128*1024),
6391da177e4SLinus Torvalds 	.period_bytes_min =	64,
6401da177e4SLinus Torvalds 	.period_bytes_max =	(128*1024),
6411da177e4SLinus Torvalds 	.periods_min =		1,
6421da177e4SLinus Torvalds 	.periods_max =		1024,
6431da177e4SLinus Torvalds 	.fifo_size =		0,
6441da177e4SLinus Torvalds };
6451da177e4SLinus Torvalds 
snd_fm801_playback_open(struct snd_pcm_substream * substream)646a5f22156STakashi Iwai static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
6471da177e4SLinus Torvalds {
648a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
649a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6501da177e4SLinus Torvalds 	int err;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	chip->playback_substream = substream;
6531da177e4SLinus Torvalds 	runtime->hw = snd_fm801_playback;
654a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
655a5f22156STakashi Iwai 				   &hw_constraints_rates);
6561da177e4SLinus Torvalds 	if (chip->multichannel) {
6571da177e4SLinus Torvalds 		runtime->hw.channels_max = 6;
658a5f22156STakashi Iwai 		snd_pcm_hw_constraint_list(runtime, 0,
659a5f22156STakashi Iwai 					   SNDRV_PCM_HW_PARAM_CHANNELS,
660a5f22156STakashi Iwai 					   &hw_constraints_channels);
6611da177e4SLinus Torvalds 	}
66268f441abSTakashi Iwai 	err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
66368f441abSTakashi Iwai 	if (err < 0)
6641da177e4SLinus Torvalds 		return err;
6651da177e4SLinus Torvalds 	return 0;
6661da177e4SLinus Torvalds }
6671da177e4SLinus Torvalds 
snd_fm801_capture_open(struct snd_pcm_substream * substream)668a5f22156STakashi Iwai static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
6691da177e4SLinus Torvalds {
670a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
671a5f22156STakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
6721da177e4SLinus Torvalds 	int err;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	chip->capture_substream = substream;
6751da177e4SLinus Torvalds 	runtime->hw = snd_fm801_capture;
676a5f22156STakashi Iwai 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
677a5f22156STakashi Iwai 				   &hw_constraints_rates);
67868f441abSTakashi Iwai 	err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
67968f441abSTakashi Iwai 	if (err < 0)
6801da177e4SLinus Torvalds 		return err;
6811da177e4SLinus Torvalds 	return 0;
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds 
snd_fm801_playback_close(struct snd_pcm_substream * substream)684a5f22156STakashi Iwai static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
6851da177e4SLinus Torvalds {
686a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	chip->playback_substream = NULL;
6891da177e4SLinus Torvalds 	return 0;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
snd_fm801_capture_close(struct snd_pcm_substream * substream)692a5f22156STakashi Iwai static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
6931da177e4SLinus Torvalds {
694a5f22156STakashi Iwai 	struct fm801 *chip = snd_pcm_substream_chip(substream);
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	chip->capture_substream = NULL;
6971da177e4SLinus Torvalds 	return 0;
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
7006769e988SJulia Lawall static const struct snd_pcm_ops snd_fm801_playback_ops = {
7011da177e4SLinus Torvalds 	.open =		snd_fm801_playback_open,
7021da177e4SLinus Torvalds 	.close =	snd_fm801_playback_close,
7031da177e4SLinus Torvalds 	.prepare =	snd_fm801_playback_prepare,
7041da177e4SLinus Torvalds 	.trigger =	snd_fm801_playback_trigger,
7051da177e4SLinus Torvalds 	.pointer =	snd_fm801_playback_pointer,
7061da177e4SLinus Torvalds };
7071da177e4SLinus Torvalds 
7086769e988SJulia Lawall static const struct snd_pcm_ops snd_fm801_capture_ops = {
7091da177e4SLinus Torvalds 	.open =		snd_fm801_capture_open,
7101da177e4SLinus Torvalds 	.close =	snd_fm801_capture_close,
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 
snd_fm801_pcm(struct fm801 * chip,int device)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 
72268f441abSTakashi Iwai 	err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm);
72368f441abSTakashi Iwai 	if (err < 0)
7241da177e4SLinus Torvalds 		return err;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
7271da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	pcm->private_data = chip;
7301da177e4SLinus Torvalds 	pcm->info_flags = 0;
7311da177e4SLinus Torvalds 	strcpy(pcm->name, "FM801");
7321da177e4SLinus Torvalds 	chip->pcm = pcm;
7331da177e4SLinus Torvalds 
734247ed102STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &pdev->dev,
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 
755fb537cd0STakashi Iwai static const 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 
snd_fm801_tea575x_set_pins(struct snd_tea575x * tea,u8 pins)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 
snd_fm801_tea575x_get_pins(struct snd_tea575x * tea)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 
snd_fm801_tea575x_set_direction(struct snd_tea575x * tea,bool output)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 
snd_fm801_info_single(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)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 
snd_fm801_get_single(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
snd_fm801_put_single(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
snd_fm801_info_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)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 
snd_fm801_get_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
snd_fm801_put_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
snd_fm801_info_mux(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)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 
snd_fm801_get_mux(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
snd_fm801_put_mux(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
99168f441abSTakashi Iwai 	val = ucontrol->value.enumerated.item[0];
99268f441abSTakashi Iwai 	if (val > 4)
9931da177e4SLinus Torvalds                 return -EINVAL;
9941da177e4SLinus Torvalds 	return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
9951da177e4SLinus Torvalds }
9961da177e4SLinus Torvalds 
9970cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
998666c70ffSTakashi Iwai 
999a5f22156STakashi Iwai #define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
10001da177e4SLinus Torvalds 
1001b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_fm801_controls[] = {
1002666c70ffSTakashi Iwai FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
1003666c70ffSTakashi Iwai 		 db_scale_dsp),
10041da177e4SLinus Torvalds FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
1005666c70ffSTakashi Iwai FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
1006666c70ffSTakashi Iwai 		 db_scale_dsp),
10071da177e4SLinus Torvalds FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
1008666c70ffSTakashi Iwai FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
1009666c70ffSTakashi Iwai 		 db_scale_dsp),
10101da177e4SLinus Torvalds FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
10111da177e4SLinus Torvalds {
10121da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
10131da177e4SLinus Torvalds 	.name = "Digital Capture Source",
10141da177e4SLinus Torvalds 	.info = snd_fm801_info_mux,
10151da177e4SLinus Torvalds 	.get = snd_fm801_get_mux,
10161da177e4SLinus Torvalds 	.put = snd_fm801_put_mux,
10171da177e4SLinus Torvalds }
10181da177e4SLinus Torvalds };
10191da177e4SLinus Torvalds 
1020a5f22156STakashi Iwai #define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
10211da177e4SLinus Torvalds 
1022b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_fm801_controls_multi[] = {
10231da177e4SLinus Torvalds FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
10241da177e4SLinus Torvalds FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
102510e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
102610e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
102710e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
102810e8d78aSClemens Ladisch FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
10291da177e4SLinus Torvalds };
10301da177e4SLinus Torvalds 
snd_fm801_mixer(struct fm801 * chip)1031e23e7a14SBill Pemberton static int snd_fm801_mixer(struct fm801 *chip)
10321da177e4SLinus Torvalds {
1033a5f22156STakashi Iwai 	struct snd_ac97_template ac97;
10341da177e4SLinus Torvalds 	unsigned int i;
10351da177e4SLinus Torvalds 	int err;
103651055da5STakashi Iwai 	static const struct snd_ac97_bus_ops ops = {
10371da177e4SLinus Torvalds 		.write = snd_fm801_codec_write,
10381da177e4SLinus Torvalds 		.read = snd_fm801_codec_read,
10391da177e4SLinus Torvalds 	};
10401da177e4SLinus Torvalds 
104168f441abSTakashi Iwai 	err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
104268f441abSTakashi Iwai 	if (err < 0)
10431da177e4SLinus Torvalds 		return err;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
10461da177e4SLinus Torvalds 	ac97.private_data = chip;
104768f441abSTakashi Iwai 	err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
104868f441abSTakashi Iwai 	if (err < 0)
10491da177e4SLinus Torvalds 		return err;
10501da177e4SLinus Torvalds 	if (chip->secondary) {
10511da177e4SLinus Torvalds 		ac97.num = 1;
10521da177e4SLinus Torvalds 		ac97.addr = chip->secondary_addr;
105368f441abSTakashi Iwai 		err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec);
105468f441abSTakashi Iwai 		if (err < 0)
10551da177e4SLinus Torvalds 			return err;
10561da177e4SLinus Torvalds 	}
1057ef1ffbe7SZhouyang Jia 	for (i = 0; i < FM801_CONTROLS; i++) {
1058ef1ffbe7SZhouyang Jia 		err = snd_ctl_add(chip->card,
1059ef1ffbe7SZhouyang Jia 			snd_ctl_new1(&snd_fm801_controls[i], chip));
1060ef1ffbe7SZhouyang Jia 		if (err < 0)
1061ef1ffbe7SZhouyang Jia 			return err;
1062ef1ffbe7SZhouyang Jia 	}
10631da177e4SLinus Torvalds 	if (chip->multichannel) {
1064ef1ffbe7SZhouyang Jia 		for (i = 0; i < FM801_CONTROLS_MULTI; i++) {
1065ef1ffbe7SZhouyang Jia 			err = snd_ctl_add(chip->card,
1066ef1ffbe7SZhouyang Jia 				snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
1067ef1ffbe7SZhouyang Jia 			if (err < 0)
1068ef1ffbe7SZhouyang Jia 				return err;
1069ef1ffbe7SZhouyang Jia 		}
10701da177e4SLinus Torvalds 	}
10711da177e4SLinus Torvalds 	return 0;
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds /*
10751da177e4SLinus Torvalds  *  initialization routines
10761da177e4SLinus Torvalds  */
10771da177e4SLinus Torvalds 
wait_for_codec(struct fm801 * chip,unsigned int codec_id,unsigned short reg,unsigned long waits)1078b1e9ed26STakashi Iwai static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
1079b1e9ed26STakashi Iwai 			  unsigned short reg, unsigned long waits)
1080b1e9ed26STakashi Iwai {
1081b1e9ed26STakashi Iwai 	unsigned long timeout = jiffies + waits;
1082b1e9ed26STakashi Iwai 
1083215dacc2SAndy Shevchenko 	fm801_writew(chip, AC97_CMD,
1084215dacc2SAndy Shevchenko 		     reg | (codec_id << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
1085b1e9ed26STakashi Iwai 	udelay(5);
1086b1e9ed26STakashi Iwai 	do {
1087215dacc2SAndy Shevchenko 		if ((fm801_readw(chip, AC97_CMD) &
1088215dacc2SAndy Shevchenko 		     (FM801_AC97_VALID | FM801_AC97_BUSY)) == FM801_AC97_VALID)
1089b1e9ed26STakashi Iwai 			return 0;
1090b1e9ed26STakashi Iwai 		schedule_timeout_uninterruptible(1);
1091b1e9ed26STakashi Iwai 	} while (time_after(timeout, jiffies));
1092b1e9ed26STakashi Iwai 	return -EIO;
1093b1e9ed26STakashi Iwai }
1094b1e9ed26STakashi Iwai 
reset_codec(struct fm801 * chip)1095b56fa687SAndy Shevchenko static int reset_codec(struct fm801 *chip)
1096b1e9ed26STakashi Iwai {
1097b1e9ed26STakashi Iwai 	/* codec cold reset + AC'97 warm reset */
1098215dacc2SAndy Shevchenko 	fm801_writew(chip, CODEC_CTRL, (1 << 5) | (1 << 6));
1099215dacc2SAndy Shevchenko 	fm801_readw(chip, CODEC_CTRL); /* flush posting data */
1100b1e9ed26STakashi Iwai 	udelay(100);
1101215dacc2SAndy Shevchenko 	fm801_writew(chip, CODEC_CTRL, 0);
1102b1e9ed26STakashi Iwai 
1103b56fa687SAndy Shevchenko 	return wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750));
1104b1e9ed26STakashi Iwai }
1105b1e9ed26STakashi Iwai 
snd_fm801_chip_multichannel_init(struct fm801 * chip)1106b56fa687SAndy Shevchenko static void snd_fm801_chip_multichannel_init(struct fm801 *chip)
1107b56fa687SAndy Shevchenko {
1108b56fa687SAndy Shevchenko 	unsigned short cmdw;
1109b56fa687SAndy Shevchenko 
1110b1e9ed26STakashi Iwai 	if (chip->multichannel) {
1111b1e9ed26STakashi Iwai 		if (chip->secondary_addr) {
1112b1e9ed26STakashi Iwai 			wait_for_codec(chip, chip->secondary_addr,
1113b1e9ed26STakashi Iwai 				       AC97_VENDOR_ID1, msecs_to_jiffies(50));
1114b1e9ed26STakashi Iwai 		} else {
1115b1e9ed26STakashi Iwai 			/* my card has the secondary codec */
1116b1e9ed26STakashi Iwai 			/* at address #3, so the loop is inverted */
111758e4334eSHarvey Harrison 			int i;
111858e4334eSHarvey Harrison 			for (i = 3; i > 0; i--) {
111958e4334eSHarvey Harrison 				if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
1120b1e9ed26STakashi Iwai 						     msecs_to_jiffies(50))) {
1121215dacc2SAndy Shevchenko 					cmdw = fm801_readw(chip, AC97_DATA);
1122b1e9ed26STakashi Iwai 					if (cmdw != 0xffff && cmdw != 0) {
1123b1e9ed26STakashi Iwai 						chip->secondary = 1;
112458e4334eSHarvey Harrison 						chip->secondary_addr = i;
1125b1e9ed26STakashi Iwai 						break;
1126b1e9ed26STakashi Iwai 					}
1127b1e9ed26STakashi Iwai 				}
1128b1e9ed26STakashi Iwai 			}
1129b1e9ed26STakashi Iwai 		}
1130b1e9ed26STakashi Iwai 
1131b1e9ed26STakashi Iwai 		/* the recovery phase, it seems that probing for non-existing codec might */
1132b1e9ed26STakashi Iwai 		/* cause timeout problems */
1133b1e9ed26STakashi Iwai 		wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
1134b1e9ed26STakashi Iwai 	}
1135b56fa687SAndy Shevchenko }
1136b1e9ed26STakashi Iwai 
snd_fm801_chip_init(struct fm801 * chip)1137b56fa687SAndy Shevchenko static void snd_fm801_chip_init(struct fm801 *chip)
1138b56fa687SAndy Shevchenko {
1139b56fa687SAndy Shevchenko 	unsigned short cmdw;
11406bbe13ecSJaroslav Kysela 
1141b1e9ed26STakashi Iwai 	/* init volume */
1142215dacc2SAndy Shevchenko 	fm801_writew(chip, PCM_VOL, 0x0808);
1143215dacc2SAndy Shevchenko 	fm801_writew(chip, FM_VOL, 0x9f1f);
1144215dacc2SAndy Shevchenko 	fm801_writew(chip, I2S_VOL, 0x8808);
1145b1e9ed26STakashi Iwai 
1146b1e9ed26STakashi Iwai 	/* I2S control - I2S mode */
1147215dacc2SAndy Shevchenko 	fm801_writew(chip, I2S_MODE, 0x0003);
1148b1e9ed26STakashi Iwai 
11496bbe13ecSJaroslav Kysela 	/* interrupt setup */
1150215dacc2SAndy Shevchenko 	cmdw = fm801_readw(chip, IRQ_MASK);
11516bbe13ecSJaroslav Kysela 	if (chip->irq < 0)
11526bbe13ecSJaroslav Kysela 		cmdw |= 0x00c3;		/* mask everything, no PCM nor MPU */
11536bbe13ecSJaroslav Kysela 	else
11546bbe13ecSJaroslav Kysela 		cmdw &= ~0x0083;	/* unmask MPU, PLAYBACK & CAPTURE */
1155215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_MASK, cmdw);
1156b1e9ed26STakashi Iwai 
1157b1e9ed26STakashi Iwai 	/* interrupt clear */
1158215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_STATUS,
1159215dacc2SAndy Shevchenko 		     FM801_IRQ_PLAYBACK | FM801_IRQ_CAPTURE | FM801_IRQ_MPU);
1160b1e9ed26STakashi Iwai }
1161b1e9ed26STakashi Iwai 
snd_fm801_free(struct snd_card * card)116247c41339STakashi Iwai static void snd_fm801_free(struct snd_card *card)
11631da177e4SLinus Torvalds {
116447c41339STakashi Iwai 	struct fm801 *chip = card->private_data;
11651da177e4SLinus Torvalds 	unsigned short cmdw;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	/* interrupt setup - mask everything */
1168215dacc2SAndy Shevchenko 	cmdw = fm801_readw(chip, IRQ_MASK);
11691da177e4SLinus Torvalds 	cmdw |= 0x00c3;
1170215dacc2SAndy Shevchenko 	fm801_writew(chip, IRQ_MASK, cmdw);
11711da177e4SLinus Torvalds 
1172fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1173d4ecc83bSHans Verkuil 	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
11741da177e4SLinus Torvalds 		snd_tea575x_exit(&chip->tea);
1175d4ecc83bSHans Verkuil 		v4l2_device_unregister(&chip->v4l2_dev);
1176d4ecc83bSHans Verkuil 	}
11771da177e4SLinus Torvalds #endif
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds 
snd_fm801_create(struct snd_card * card,struct pci_dev * pci,int tea575x_tuner,int radio_nr)1180e23e7a14SBill Pemberton static int snd_fm801_create(struct snd_card *card,
11811da177e4SLinus Torvalds 			    struct pci_dev *pci,
11821da177e4SLinus Torvalds 			    int tea575x_tuner,
118347c41339STakashi Iwai 			    int radio_nr)
11841da177e4SLinus Torvalds {
118547c41339STakashi Iwai 	struct fm801 *chip = card->private_data;
11861da177e4SLinus Torvalds 	int err;
11871da177e4SLinus Torvalds 
118868f441abSTakashi Iwai 	err = pcim_enable_device(pci);
118968f441abSTakashi Iwai 	if (err < 0)
11901da177e4SLinus Torvalds 		return err;
11911da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
11921da177e4SLinus Torvalds 	chip->card = card;
1193d3d33aabSAndy Shevchenko 	chip->dev = &pci->dev;
11941da177e4SLinus Torvalds 	chip->irq = -1;
11956bbe13ecSJaroslav Kysela 	chip->tea575x_tuner = tea575x_tuner;
119668f441abSTakashi Iwai 	err = pci_request_regions(pci, "FM801");
119768f441abSTakashi Iwai 	if (err < 0)
11981da177e4SLinus Torvalds 		return err;
11991da177e4SLinus Torvalds 	chip->port = pci_resource_start(pci, 0);
1200b56fa687SAndy Shevchenko 
1201b56fa687SAndy Shevchenko 	if (pci->revision >= 0xb1)	/* FM801-AU */
1202b56fa687SAndy Shevchenko 		chip->multichannel = 1;
1203b56fa687SAndy Shevchenko 
1204b56fa687SAndy Shevchenko 	if (!(chip->tea575x_tuner & TUNER_ONLY)) {
1205b56fa687SAndy Shevchenko 		if (reset_codec(chip) < 0) {
1206b56fa687SAndy Shevchenko 			dev_info(chip->card->dev,
1207b56fa687SAndy Shevchenko 				 "Primary AC'97 codec not found, assume SF64-PCR (tuner-only)\n");
1208b56fa687SAndy Shevchenko 			chip->tea575x_tuner = 3 | TUNER_ONLY;
1209b56fa687SAndy Shevchenko 		} else {
1210b56fa687SAndy Shevchenko 			snd_fm801_chip_multichannel_init(chip);
1211b56fa687SAndy Shevchenko 		}
1212b56fa687SAndy Shevchenko 	}
1213b56fa687SAndy Shevchenko 
1214b56fa687SAndy Shevchenko 	if ((chip->tea575x_tuner & TUNER_ONLY) == 0) {
12155618955cSAndy Shevchenko 		if (devm_request_irq(&pci->dev, pci->irq, snd_fm801_interrupt,
12165618955cSAndy Shevchenko 				IRQF_SHARED, KBUILD_MODNAME, chip)) {
12175618955cSAndy Shevchenko 			dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
12181da177e4SLinus Torvalds 			return -EBUSY;
12191da177e4SLinus Torvalds 		}
12201da177e4SLinus Torvalds 		chip->irq = pci->irq;
1221e41dbd20STakashi Iwai 		card->sync_irq = chip->irq;
12221da177e4SLinus Torvalds 		pci_set_master(pci);
12236bbe13ecSJaroslav Kysela 	}
12241da177e4SLinus Torvalds 
122547c41339STakashi Iwai 	card->private_free = snd_fm801_free;
1226610e1ae9SAndy Shevchenko 	snd_fm801_chip_init(chip);
1227610e1ae9SAndy Shevchenko 
1228fdb62b50SOndrej Zary #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1229d4ecc83bSHans Verkuil 	err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
123047c41339STakashi Iwai 	if (err < 0)
1231d4ecc83bSHans Verkuil 		return err;
1232d4ecc83bSHans Verkuil 	chip->tea.v4l2_dev = &chip->v4l2_dev;
1233d4ecc83bSHans Verkuil 	chip->tea.radio_nr = radio_nr;
12341da177e4SLinus Torvalds 	chip->tea.private_data = chip;
1235938a1566SOndrej Zary 	chip->tea.ops = &snd_fm801_tea_ops;
123610ca7201SOndrej Zary 	sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
1237b56fa687SAndy Shevchenko 	if ((chip->tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
1238b56fa687SAndy Shevchenko 	    (chip->tea575x_tuner & TUNER_TYPE_MASK) < 4) {
12395daf53a6SHans de Goede 		if (snd_tea575x_init(&chip->tea, THIS_MODULE)) {
12409c7f9abfSTakashi Iwai 			dev_err(card->dev, "TEA575x radio not found\n");
124196760015SDan Carpenter 			return -ENODEV;
124296760015SDan Carpenter 		}
1243b56fa687SAndy Shevchenko 	} else if ((chip->tea575x_tuner & TUNER_TYPE_MASK) == 0) {
1244b56fa687SAndy Shevchenko 		unsigned int tuner_only = chip->tea575x_tuner & TUNER_ONLY;
1245dbec6719SAndy Shevchenko 
1246d7ba858aSOndrej Zary 		/* autodetect tuner connection */
1247d7ba858aSOndrej Zary 		for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
1248d7ba858aSOndrej Zary 			chip->tea575x_tuner = tea575x_tuner;
12495daf53a6SHans de Goede 			if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
12509c7f9abfSTakashi Iwai 				dev_info(card->dev,
12519c7f9abfSTakashi Iwai 					 "detected TEA575x radio type %s\n",
12528e699d2cSTakashi Iwai 					   get_tea575x_gpio(chip)->name);
1253d7ba858aSOndrej Zary 				break;
1254d7ba858aSOndrej Zary 			}
12551da177e4SLinus Torvalds 		}
125696760015SDan Carpenter 		if (tea575x_tuner == 4) {
12579c7f9abfSTakashi Iwai 			dev_err(card->dev, "TEA575x radio not found\n");
1258c37279b9SBen Hutchings 			chip->tea575x_tuner = TUNER_DISABLED;
125996760015SDan Carpenter 		}
1260dbec6719SAndy Shevchenko 
1261dbec6719SAndy Shevchenko 		chip->tea575x_tuner |= tuner_only;
126296760015SDan Carpenter 	}
1263c37279b9SBen Hutchings 	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
126475b1a8f9SJoe Perches 		strscpy(chip->tea.card, get_tea575x_gpio(chip)->name,
1265c37279b9SBen Hutchings 			sizeof(chip->tea.card));
1266c37279b9SBen Hutchings 	}
12671da177e4SLinus Torvalds #endif
12681da177e4SLinus Torvalds 	return 0;
12691da177e4SLinus Torvalds }
12701da177e4SLinus Torvalds 
__snd_card_fm801_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)1271*7f611274STakashi Iwai static int __snd_card_fm801_probe(struct pci_dev *pci,
12721da177e4SLinus Torvalds 				  const struct pci_device_id *pci_id)
12731da177e4SLinus Torvalds {
12741da177e4SLinus Torvalds 	static int dev;
1275a5f22156STakashi Iwai 	struct snd_card *card;
1276a5f22156STakashi Iwai 	struct fm801 *chip;
1277a5f22156STakashi Iwai 	struct snd_opl3 *opl3;
12781da177e4SLinus Torvalds 	int err;
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds         if (dev >= SNDRV_CARDS)
12811da177e4SLinus Torvalds                 return -ENODEV;
12821da177e4SLinus Torvalds 	if (!enable[dev]) {
12831da177e4SLinus Torvalds 		dev++;
12841da177e4SLinus Torvalds 		return -ENOENT;
12851da177e4SLinus Torvalds 	}
12861da177e4SLinus Torvalds 
128747c41339STakashi Iwai 	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
128847c41339STakashi Iwai 				sizeof(*chip), &card);
1289e58de7baSTakashi Iwai 	if (err < 0)
1290e58de7baSTakashi Iwai 		return err;
129147c41339STakashi Iwai 	chip = card->private_data;
129247c41339STakashi Iwai 	err = snd_fm801_create(card, pci, tea575x_tuner[dev], radio_nr[dev]);
129347c41339STakashi Iwai 	if (err < 0)
12941da177e4SLinus Torvalds 		return err;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	strcpy(card->driver, "FM801");
12971da177e4SLinus Torvalds 	strcpy(card->shortname, "ForteMedia FM801-");
12981da177e4SLinus Torvalds 	strcat(card->shortname, chip->multichannel ? "AU" : "AS");
12991da177e4SLinus Torvalds 	sprintf(card->longname, "%s at 0x%lx, irq %i",
13001da177e4SLinus Torvalds 		card->shortname, chip->port, chip->irq);
13011da177e4SLinus Torvalds 
1302fb716c0bSOndrej Zary 	if (chip->tea575x_tuner & TUNER_ONLY)
1303e0a5d82aSAndy Shevchenko 		goto __fm801_tuner_only;
1304e0a5d82aSAndy Shevchenko 
130568f441abSTakashi Iwai 	err = snd_fm801_pcm(chip, 0);
130647c41339STakashi Iwai 	if (err < 0)
13071da177e4SLinus Torvalds 		return err;
130868f441abSTakashi Iwai 	err = snd_fm801_mixer(chip);
130947c41339STakashi Iwai 	if (err < 0)
13101da177e4SLinus Torvalds 		return err;
131168f441abSTakashi Iwai 	err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1312215dacc2SAndy Shevchenko 				  chip->port + FM801_MPU401_DATA,
1313dba8b469SClemens Ladisch 				  MPU401_INFO_INTEGRATED |
1314dba8b469SClemens Ladisch 				  MPU401_INFO_IRQ_HOOK,
131568f441abSTakashi Iwai 				  -1, &chip->rmidi);
131647c41339STakashi Iwai 	if (err < 0)
13171da177e4SLinus Torvalds 		return err;
131868f441abSTakashi Iwai 	err = snd_opl3_create(card, chip->port + FM801_OPL3_BANK0,
1319215dacc2SAndy Shevchenko 			      chip->port + FM801_OPL3_BANK1,
132068f441abSTakashi Iwai 			      OPL3_HW_OPL3_FM801, 1, &opl3);
132147c41339STakashi Iwai 	if (err < 0)
13221da177e4SLinus Torvalds 		return err;
132368f441abSTakashi Iwai 	err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
132447c41339STakashi Iwai 	if (err < 0)
13251da177e4SLinus Torvalds 		return err;
13261da177e4SLinus Torvalds 
1327e0a5d82aSAndy Shevchenko       __fm801_tuner_only:
132868f441abSTakashi Iwai 	err = snd_card_register(card);
132947c41339STakashi Iwai 	if (err < 0)
13301da177e4SLinus Torvalds 		return err;
13311da177e4SLinus Torvalds 	pci_set_drvdata(pci, card);
13321da177e4SLinus Torvalds 	dev++;
13331da177e4SLinus Torvalds 	return 0;
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds 
snd_card_fm801_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)1336*7f611274STakashi Iwai static int snd_card_fm801_probe(struct pci_dev *pci,
1337*7f611274STakashi Iwai 				const struct pci_device_id *pci_id)
1338*7f611274STakashi Iwai {
1339*7f611274STakashi Iwai 	return snd_card_free_on_error(&pci->dev, __snd_card_fm801_probe(pci, pci_id));
1340*7f611274STakashi Iwai }
1341*7f611274STakashi Iwai 
1342c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
13438045d0fcSTakashi Iwai static const unsigned char saved_regs[] = {
1344b1e9ed26STakashi Iwai 	FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
1345b1e9ed26STakashi Iwai 	FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
1346b1e9ed26STakashi Iwai 	FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
1347b1e9ed26STakashi Iwai 	FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
1348b1e9ed26STakashi Iwai };
1349b1e9ed26STakashi Iwai 
snd_fm801_suspend(struct device * dev)135068cb2b55STakashi Iwai static int snd_fm801_suspend(struct device *dev)
1351b1e9ed26STakashi Iwai {
135268cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1353b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1354b1e9ed26STakashi Iwai 	int i;
1355b1e9ed26STakashi Iwai 
1356b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
135714da04b5SAndy Shevchenko 
135837ba8fcaSAndy Shevchenko 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
135937ba8fcaSAndy Shevchenko 		chip->saved_regs[i] = fm801_ioread16(chip, saved_regs[i]);
136037ba8fcaSAndy Shevchenko 
136114da04b5SAndy Shevchenko 	if (chip->tea575x_tuner & TUNER_ONLY) {
136214da04b5SAndy Shevchenko 		/* FIXME: tea575x suspend */
136314da04b5SAndy Shevchenko 	} else {
1364b1e9ed26STakashi Iwai 		snd_ac97_suspend(chip->ac97);
1365b1e9ed26STakashi Iwai 		snd_ac97_suspend(chip->ac97_sec);
136614da04b5SAndy Shevchenko 	}
136714da04b5SAndy Shevchenko 
1368b1e9ed26STakashi Iwai 	return 0;
1369b1e9ed26STakashi Iwai }
1370b1e9ed26STakashi Iwai 
snd_fm801_resume(struct device * dev)137168cb2b55STakashi Iwai static int snd_fm801_resume(struct device *dev)
1372b1e9ed26STakashi Iwai {
137368cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
1374b1e9ed26STakashi Iwai 	struct fm801 *chip = card->private_data;
1375b1e9ed26STakashi Iwai 	int i;
1376b1e9ed26STakashi Iwai 
1377b56fa687SAndy Shevchenko 	if (chip->tea575x_tuner & TUNER_ONLY) {
1378b56fa687SAndy Shevchenko 		snd_fm801_chip_init(chip);
1379b56fa687SAndy Shevchenko 	} else {
1380b56fa687SAndy Shevchenko 		reset_codec(chip);
1381b56fa687SAndy Shevchenko 		snd_fm801_chip_multichannel_init(chip);
1382b56fa687SAndy Shevchenko 		snd_fm801_chip_init(chip);
1383b1e9ed26STakashi Iwai 		snd_ac97_resume(chip->ac97);
1384b1e9ed26STakashi Iwai 		snd_ac97_resume(chip->ac97_sec);
138514da04b5SAndy Shevchenko 	}
138614da04b5SAndy Shevchenko 
1387b1e9ed26STakashi Iwai 	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
13884b5c15f7SAndy Shevchenko 		fm801_iowrite16(chip, saved_regs[i], chip->saved_regs[i]);
1389b1e9ed26STakashi Iwai 
1390cb41f271SAndy Shevchenko #ifdef CONFIG_SND_FM801_TEA575X_BOOL
1391cb41f271SAndy Shevchenko 	if (!(chip->tea575x_tuner & TUNER_DISABLED))
1392cb41f271SAndy Shevchenko 		snd_tea575x_set_freq(&chip->tea);
1393cb41f271SAndy Shevchenko #endif
1394b1e9ed26STakashi Iwai 
1395b1e9ed26STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1396b1e9ed26STakashi Iwai 	return 0;
1397b1e9ed26STakashi Iwai }
139868cb2b55STakashi Iwai 
139968cb2b55STakashi Iwai static SIMPLE_DEV_PM_OPS(snd_fm801_pm, snd_fm801_suspend, snd_fm801_resume);
140068cb2b55STakashi Iwai #define SND_FM801_PM_OPS	&snd_fm801_pm
140168cb2b55STakashi Iwai #else
140268cb2b55STakashi Iwai #define SND_FM801_PM_OPS	NULL
1403c7561cd8STakashi Iwai #endif /* CONFIG_PM_SLEEP */
1404b1e9ed26STakashi Iwai 
1405e9f66d9bSTakashi Iwai static struct pci_driver fm801_driver = {
14063733e424STakashi Iwai 	.name = KBUILD_MODNAME,
14071da177e4SLinus Torvalds 	.id_table = snd_fm801_ids,
14081da177e4SLinus Torvalds 	.probe = snd_card_fm801_probe,
140968cb2b55STakashi Iwai 	.driver = {
141068cb2b55STakashi Iwai 		.pm = SND_FM801_PM_OPS,
141168cb2b55STakashi Iwai 	},
14121da177e4SLinus Torvalds };
14131da177e4SLinus Torvalds 
1414e9f66d9bSTakashi Iwai module_pci_driver(fm801_driver);
1415