fm801.c 41.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  The driver for the ForteMedia FM801 based soundcards
3
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
4
 *
5
 *  Support FM only card by Andy Shevchenko <andy@smile.org.ua>
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/delay.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/slab.h>
28
#include <linux/module.h>
L
Linus Torvalds 已提交
29 30
#include <sound/core.h>
#include <sound/pcm.h>
31
#include <sound/tlv.h>
L
Linus Torvalds 已提交
32 33 34 35 36 37 38
#include <sound/ac97_codec.h>
#include <sound/mpu401.h>
#include <sound/opl3.h>
#include <sound/initval.h>

#include <asm/io.h>

39
#ifdef CONFIG_SND_FM801_TEA575X_BOOL
L
Linus Torvalds 已提交
40 41 42
#include <sound/tea575x-tuner.h>
#endif

43
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
L
Linus Torvalds 已提交
44 45 46 47 48 49 50
MODULE_DESCRIPTION("ForteMedia FM801");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{ForteMedia,FM801},"
		"{Genius,SoundMaker Live 5.1}}");

static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
51
static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
L
Linus Torvalds 已提交
52 53 54
/*
 *  Enable TEA575x tuner
 *    1 = MediaForte 256-PCS
55
 *    2 = MediaForte 256-PCP
L
Linus Torvalds 已提交
56
 *    3 = MediaForte 64-PCR
57
 *   16 = setup tuner only (this is additional bit), i.e. SF64-PCR FM card
L
Linus Torvalds 已提交
58 59
 *  High 16-bits are video (radio) device number + 1
 */
60
static int tea575x_tuner[SNDRV_CARDS];
61
static int radio_nr[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
L
Linus Torvalds 已提交
62 63 64 65 66 67 68 69

module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
module_param_array(tea575x_tuner, int, NULL, 0444);
70
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).");
71 72 73
module_param_array(radio_nr, int, NULL, 0444);
MODULE_PARM_DESC(radio_nr, "Radio device numbers");

74

75
#define TUNER_DISABLED		(1<<3)
76 77
#define TUNER_ONLY		(1<<4)
#define TUNER_TYPE_MASK		(~TUNER_ONLY & 0xFFFF)
L
Linus Torvalds 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

/*
 *  Direct registers
 */

#define FM801_REG(chip, reg)	(chip->port + FM801_##reg)

#define FM801_PCM_VOL		0x00	/* PCM Output Volume */
#define FM801_FM_VOL		0x02	/* FM Output Volume */
#define FM801_I2S_VOL		0x04	/* I2S Volume */
#define FM801_REC_SRC		0x06	/* Record Source */
#define FM801_PLY_CTRL		0x08	/* Playback Control */
#define FM801_PLY_COUNT		0x0a	/* Playback Count */
#define FM801_PLY_BUF1		0x0c	/* Playback Bufer I */
#define FM801_PLY_BUF2		0x10	/* Playback Buffer II */
#define FM801_CAP_CTRL		0x14	/* Capture Control */
#define FM801_CAP_COUNT		0x16	/* Capture Count */
#define FM801_CAP_BUF1		0x18	/* Capture Buffer I */
#define FM801_CAP_BUF2		0x1c	/* Capture Buffer II */
#define FM801_CODEC_CTRL	0x22	/* Codec Control */
#define FM801_I2S_MODE		0x24	/* I2S Mode Control */
#define FM801_VOLUME		0x26	/* Volume Up/Down/Mute Status */
#define FM801_I2C_CTRL		0x29	/* I2C Control */
#define FM801_AC97_CMD		0x2a	/* AC'97 Command */
#define FM801_AC97_DATA		0x2c	/* AC'97 Data */
#define FM801_MPU401_DATA	0x30	/* MPU401 Data */
#define FM801_MPU401_CMD	0x31	/* MPU401 Command */
#define FM801_GPIO_CTRL		0x52	/* General Purpose I/O Control */
#define FM801_GEN_CTRL		0x54	/* General Control */
#define FM801_IRQ_MASK		0x56	/* Interrupt Mask */
#define FM801_IRQ_STATUS	0x5a	/* Interrupt Status */
#define FM801_OPL3_BANK0	0x68	/* OPL3 Status Read / Bank 0 Write */
#define FM801_OPL3_DATA0	0x69	/* OPL3 Data 0 Write */
#define FM801_OPL3_BANK1	0x6a	/* OPL3 Bank 1 Write */
#define FM801_OPL3_DATA1	0x6b	/* OPL3 Bank 1 Write */
#define FM801_POWERDOWN		0x70	/* Blocks Power Down Control */

T
Takashi Iwai 已提交
115 116 117 118 119
/* codec access */
#define FM801_AC97_READ		(1<<7)	/* read=1, write=0 */
#define FM801_AC97_VALID	(1<<8)	/* port valid=1 */
#define FM801_AC97_BUSY		(1<<9)	/* busy=1 */
#define FM801_AC97_ADDR_SHIFT	10	/* codec id (2bit) */
L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

/* playback and record control register bits */
#define FM801_BUF1_LAST		(1<<1)
#define FM801_BUF2_LAST		(1<<2)
#define FM801_START		(1<<5)
#define FM801_PAUSE		(1<<6)
#define FM801_IMMED_STOP	(1<<7)
#define FM801_RATE_SHIFT	8
#define FM801_RATE_MASK		(15 << FM801_RATE_SHIFT)
#define FM801_CHANNELS_4	(1<<12)	/* playback only */
#define FM801_CHANNELS_6	(2<<12)	/* playback only */
#define FM801_CHANNELS_6MS	(3<<12)	/* playback only */
#define FM801_CHANNELS_MASK	(3<<12)
#define FM801_16BIT		(1<<14)
#define FM801_STEREO		(1<<15)

/* IRQ status bits */
#define FM801_IRQ_PLAYBACK	(1<<8)
#define FM801_IRQ_CAPTURE	(1<<9)
#define FM801_IRQ_VOLUME	(1<<14)
#define FM801_IRQ_MPU		(1<<15)

/* GPIO control register */
#define FM801_GPIO_GP0		(1<<0)	/* read/write */
#define FM801_GPIO_GP1		(1<<1)
#define FM801_GPIO_GP2		(1<<2)
#define FM801_GPIO_GP3		(1<<3)
#define FM801_GPIO_GP(x)	(1<<(0+(x)))
#define FM801_GPIO_GD0		(1<<8)	/* directions: 1 = input, 0 = output*/
#define FM801_GPIO_GD1		(1<<9)
#define FM801_GPIO_GD2		(1<<10)
#define FM801_GPIO_GD3		(1<<11)
#define FM801_GPIO_GD(x)	(1<<(8+(x)))
#define FM801_GPIO_GS0		(1<<12)	/* function select: */
#define FM801_GPIO_GS1		(1<<13)	/*    1 = GPIO */
#define FM801_GPIO_GS2		(1<<14)	/*    0 = other (S/PDIF, VOL) */
#define FM801_GPIO_GS3		(1<<15)
#define FM801_GPIO_GS(x)	(1<<(12+(x)))
	
/*

 */

163
struct fm801 {
L
Linus Torvalds 已提交
164 165 166 167 168 169
	int irq;

	unsigned long port;	/* I/O port number */
	unsigned int multichannel: 1,	/* multichannel support */
		     secondary: 1;	/* secondary codec */
	unsigned char secondary_addr;	/* address of the secondary codec */
170
	unsigned int tea575x_tuner;	/* tuner access method & flags */
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

	unsigned short ply_ctrl; /* playback control */
	unsigned short cap_ctrl; /* capture control */

	unsigned long ply_buffer;
	unsigned int ply_buf;
	unsigned int ply_count;
	unsigned int ply_size;
	unsigned int ply_pos;

	unsigned long cap_buffer;
	unsigned int cap_buf;
	unsigned int cap_count;
	unsigned int cap_size;
	unsigned int cap_pos;

187 188 189
	struct snd_ac97_bus *ac97_bus;
	struct snd_ac97 *ac97;
	struct snd_ac97 *ac97_sec;
L
Linus Torvalds 已提交
190 191

	struct pci_dev *pci;
192 193 194 195 196
	struct snd_card *card;
	struct snd_pcm *pcm;
	struct snd_rawmidi *rmidi;
	struct snd_pcm_substream *playback_substream;
	struct snd_pcm_substream *capture_substream;
L
Linus Torvalds 已提交
197 198 199 200
	unsigned int p_dma_size;
	unsigned int c_dma_size;

	spinlock_t reg_lock;
201
	struct snd_info_entry *proc_entry;
L
Linus Torvalds 已提交
202

203
#ifdef CONFIG_SND_FM801_TEA575X_BOOL
204
	struct v4l2_device v4l2_dev;
205
	struct snd_tea575x tea;
L
Linus Torvalds 已提交
206
#endif
T
Takashi Iwai 已提交
207

208
#ifdef CONFIG_PM_SLEEP
T
Takashi Iwai 已提交
209 210
	u16 saved_regs[0x20];
#endif
L
Linus Torvalds 已提交
211 212
};

213
static DEFINE_PCI_DEVICE_TABLE(snd_fm801_ids) = {
L
Linus Torvalds 已提交
214
	{ 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* FM801 */
215
	{ 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* Gallant Odyssey Sound 4 */
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224
	{ 0, }
};

MODULE_DEVICE_TABLE(pci, snd_fm801_ids);

/*
 *  common I/O routines
 */

225
static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
				 unsigned short mask, unsigned short value)
{
	int change;
	unsigned long flags;
	unsigned short old, new;

	spin_lock_irqsave(&chip->reg_lock, flags);
	old = inw(chip->port + reg);
	new = (old & ~mask) | value;
	change = old != new;
	if (change)
		outw(new, chip->port + reg);
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	return change;
}

242
static void snd_fm801_codec_write(struct snd_ac97 *ac97,
L
Linus Torvalds 已提交
243 244 245
				  unsigned short reg,
				  unsigned short val)
{
246
	struct fm801 *chip = ac97->private_data;
L
Linus Torvalds 已提交
247 248 249 250 251 252
	int idx;

	/*
	 *  Wait until the codec interface is not ready..
	 */
	for (idx = 0; idx < 100; idx++) {
T
Takashi Iwai 已提交
253
		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
L
Linus Torvalds 已提交
254 255 256
			goto ok1;
		udelay(10);
	}
257
	snd_printk(KERN_ERR "AC'97 interface is busy (1)\n");
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266 267
	return;

 ok1:
	/* write data and address */
	outw(val, FM801_REG(chip, AC97_DATA));
	outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
	/*
	 *  Wait until the write command is not completed..
         */
	for (idx = 0; idx < 1000; idx++) {
T
Takashi Iwai 已提交
268
		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
L
Linus Torvalds 已提交
269 270 271
			return;
		udelay(10);
	}
272
	snd_printk(KERN_ERR "AC'97 interface #%d is busy (2)\n", ac97->num);
L
Linus Torvalds 已提交
273 274
}

275
static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
L
Linus Torvalds 已提交
276
{
277
	struct fm801 *chip = ac97->private_data;
L
Linus Torvalds 已提交
278 279 280 281 282 283
	int idx;

	/*
	 *  Wait until the codec interface is not ready..
	 */
	for (idx = 0; idx < 100; idx++) {
T
Takashi Iwai 已提交
284
		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
L
Linus Torvalds 已提交
285 286 287
			goto ok1;
		udelay(10);
	}
288
	snd_printk(KERN_ERR "AC'97 interface is busy (1)\n");
L
Linus Torvalds 已提交
289 290 291 292
	return 0;

 ok1:
	/* read command */
T
Takashi Iwai 已提交
293 294
	outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ,
	     FM801_REG(chip, AC97_CMD));
L
Linus Torvalds 已提交
295
	for (idx = 0; idx < 100; idx++) {
T
Takashi Iwai 已提交
296
		if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
L
Linus Torvalds 已提交
297 298 299
			goto ok2;
		udelay(10);
	}
300
	snd_printk(KERN_ERR "AC'97 interface #%d is busy (2)\n", ac97->num);
L
Linus Torvalds 已提交
301 302 303 304
	return 0;

 ok2:
	for (idx = 0; idx < 1000; idx++) {
T
Takashi Iwai 已提交
305
		if (inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_VALID)
L
Linus Torvalds 已提交
306 307 308
			goto ok3;
		udelay(10);
	}
309
	snd_printk(KERN_ERR "AC'97 interface #%d is not valid (2)\n", ac97->num);
L
Linus Torvalds 已提交
310 311 312 313 314 315 316 317 318 319 320 321
	return 0;

 ok3:
	return inw(FM801_REG(chip, AC97_DATA));
}

static unsigned int rates[] = {
  5500,  8000,  9600, 11025,
  16000, 19200, 22050, 32000,
  38400, 44100, 48000
};

322
static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
L
Linus Torvalds 已提交
323 324 325 326 327 328 329 330 331
	.count = ARRAY_SIZE(rates),
	.list = rates,
	.mask = 0,
};

static unsigned int channels[] = {
  2, 4, 6
};

332
static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
333
	.count = ARRAY_SIZE(channels),
L
Linus Torvalds 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	.list = channels,
	.mask = 0,
};

/*
 *  Sample rate routines
 */

static unsigned short snd_fm801_rate_bits(unsigned int rate)
{
	unsigned int idx;

	for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
		if (rates[idx] == rate)
			return idx;
	snd_BUG();
	return ARRAY_SIZE(rates) - 1;
}

/*
 *  PCM part
 */

357
static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
358 359
				      int cmd)
{
360
	struct fm801 *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374

	spin_lock(&chip->reg_lock);
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		chip->ply_ctrl &= ~(FM801_BUF1_LAST |
				     FM801_BUF2_LAST |
				     FM801_PAUSE);
		chip->ply_ctrl |= FM801_START |
				   FM801_IMMED_STOP;
		break;
	case SNDRV_PCM_TRIGGER_STOP:
		chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
T
Takashi Iwai 已提交
375
	case SNDRV_PCM_TRIGGER_SUSPEND:
L
Linus Torvalds 已提交
376 377 378
		chip->ply_ctrl |= FM801_PAUSE;
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
T
Takashi Iwai 已提交
379
	case SNDRV_PCM_TRIGGER_RESUME:
L
Linus Torvalds 已提交
380 381 382 383 384 385 386 387 388 389 390 391
		chip->ply_ctrl &= ~FM801_PAUSE;
		break;
	default:
		spin_unlock(&chip->reg_lock);
		snd_BUG();
		return -EINVAL;
	}
	outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
	spin_unlock(&chip->reg_lock);
	return 0;
}

392
static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
393 394
				     int cmd)
{
395
	struct fm801 *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409

	spin_lock(&chip->reg_lock);
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		chip->cap_ctrl &= ~(FM801_BUF1_LAST |
				     FM801_BUF2_LAST |
				     FM801_PAUSE);
		chip->cap_ctrl |= FM801_START |
				   FM801_IMMED_STOP;
		break;
	case SNDRV_PCM_TRIGGER_STOP:
		chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
T
Takashi Iwai 已提交
410
	case SNDRV_PCM_TRIGGER_SUSPEND:
L
Linus Torvalds 已提交
411 412 413
		chip->cap_ctrl |= FM801_PAUSE;
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
T
Takashi Iwai 已提交
414
	case SNDRV_PCM_TRIGGER_RESUME:
L
Linus Torvalds 已提交
415 416 417 418 419 420 421 422 423 424 425 426
		chip->cap_ctrl &= ~FM801_PAUSE;
		break;
	default:
		spin_unlock(&chip->reg_lock);
		snd_BUG();
		return -EINVAL;
	}
	outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
	spin_unlock(&chip->reg_lock);
	return 0;
}

427 428
static int snd_fm801_hw_params(struct snd_pcm_substream *substream,
			       struct snd_pcm_hw_params *hw_params)
L
Linus Torvalds 已提交
429 430 431 432
{
	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
}

433
static int snd_fm801_hw_free(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
434 435 436 437
{
	return snd_pcm_lib_free_pages(substream);
}

438
static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
439
{
440 441
	struct fm801 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

	chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
	chip->ply_count = snd_pcm_lib_period_bytes(substream);
	spin_lock_irq(&chip->reg_lock);
	chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
			     FM801_STEREO | FM801_RATE_MASK |
			     FM801_CHANNELS_MASK);
	if (snd_pcm_format_width(runtime->format) == 16)
		chip->ply_ctrl |= FM801_16BIT;
	if (runtime->channels > 1) {
		chip->ply_ctrl |= FM801_STEREO;
		if (runtime->channels == 4)
			chip->ply_ctrl |= FM801_CHANNELS_4;
		else if (runtime->channels == 6)
			chip->ply_ctrl |= FM801_CHANNELS_6;
	}
	chip->ply_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
	chip->ply_buf = 0;
	outw(chip->ply_ctrl, FM801_REG(chip, PLY_CTRL));
	outw(chip->ply_count - 1, FM801_REG(chip, PLY_COUNT));
	chip->ply_buffer = runtime->dma_addr;
	chip->ply_pos = 0;
	outl(chip->ply_buffer, FM801_REG(chip, PLY_BUF1));
	outl(chip->ply_buffer + (chip->ply_count % chip->ply_size), FM801_REG(chip, PLY_BUF2));
	spin_unlock_irq(&chip->reg_lock);
	return 0;
}

470
static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
471
{
472 473
	struct fm801 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

	chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
	chip->cap_count = snd_pcm_lib_period_bytes(substream);
	spin_lock_irq(&chip->reg_lock);
	chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
			     FM801_STEREO | FM801_RATE_MASK);
	if (snd_pcm_format_width(runtime->format) == 16)
		chip->cap_ctrl |= FM801_16BIT;
	if (runtime->channels > 1)
		chip->cap_ctrl |= FM801_STEREO;
	chip->cap_ctrl |= snd_fm801_rate_bits(runtime->rate) << FM801_RATE_SHIFT;
	chip->cap_buf = 0;
	outw(chip->cap_ctrl, FM801_REG(chip, CAP_CTRL));
	outw(chip->cap_count - 1, FM801_REG(chip, CAP_COUNT));
	chip->cap_buffer = runtime->dma_addr;
	chip->cap_pos = 0;
	outl(chip->cap_buffer, FM801_REG(chip, CAP_BUF1));
	outl(chip->cap_buffer + (chip->cap_count % chip->cap_size), FM801_REG(chip, CAP_BUF2));
	spin_unlock_irq(&chip->reg_lock);
	return 0;
}

496
static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
497
{
498
	struct fm801 *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511 512
	size_t ptr;

	if (!(chip->ply_ctrl & FM801_START))
		return 0;
	spin_lock(&chip->reg_lock);
	ptr = chip->ply_pos + (chip->ply_count - 1) - inw(FM801_REG(chip, PLY_COUNT));
	if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_PLAYBACK) {
		ptr += chip->ply_count;
		ptr %= chip->ply_size;
	}
	spin_unlock(&chip->reg_lock);
	return bytes_to_frames(substream->runtime, ptr);
}

513
static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
514
{
515
	struct fm801 *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529
	size_t ptr;

	if (!(chip->cap_ctrl & FM801_START))
		return 0;
	spin_lock(&chip->reg_lock);
	ptr = chip->cap_pos + (chip->cap_count - 1) - inw(FM801_REG(chip, CAP_COUNT));
	if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_CAPTURE) {
		ptr += chip->cap_count;
		ptr %= chip->cap_size;
	}
	spin_unlock(&chip->reg_lock);
	return bytes_to_frames(substream->runtime, ptr);
}

530
static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
531
{
532
	struct fm801 *chip = dev_id;
L
Linus Torvalds 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
	unsigned short status;
	unsigned int tmp;

	status = inw(FM801_REG(chip, IRQ_STATUS));
	status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
	if (! status)
		return IRQ_NONE;
	/* ack first */
	outw(status, FM801_REG(chip, IRQ_STATUS));
	if (chip->pcm && (status & FM801_IRQ_PLAYBACK) && chip->playback_substream) {
		spin_lock(&chip->reg_lock);
		chip->ply_buf++;
		chip->ply_pos += chip->ply_count;
		chip->ply_pos %= chip->ply_size;
		tmp = chip->ply_pos + chip->ply_count;
		tmp %= chip->ply_size;
		outl(chip->ply_buffer + tmp,
				(chip->ply_buf & 1) ?
					FM801_REG(chip, PLY_BUF1) :
					FM801_REG(chip, PLY_BUF2));
		spin_unlock(&chip->reg_lock);
		snd_pcm_period_elapsed(chip->playback_substream);
	}
	if (chip->pcm && (status & FM801_IRQ_CAPTURE) && chip->capture_substream) {
		spin_lock(&chip->reg_lock);
		chip->cap_buf++;
		chip->cap_pos += chip->cap_count;
		chip->cap_pos %= chip->cap_size;
		tmp = chip->cap_pos + chip->cap_count;
		tmp %= chip->cap_size;
		outl(chip->cap_buffer + tmp,
				(chip->cap_buf & 1) ?
					FM801_REG(chip, CAP_BUF1) :
					FM801_REG(chip, CAP_BUF2));
		spin_unlock(&chip->reg_lock);
		snd_pcm_period_elapsed(chip->capture_substream);
	}
	if (chip->rmidi && (status & FM801_IRQ_MPU))
571
		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
L
Linus Torvalds 已提交
572 573 574 575 576 577
	if (status & FM801_IRQ_VOLUME)
		;/* TODO */

	return IRQ_HANDLED;
}

578
static struct snd_pcm_hardware snd_fm801_playback =
L
Linus Torvalds 已提交
579 580 581
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
T
Takashi Iwai 已提交
582
				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
L
Linus Torvalds 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
				 SNDRV_PCM_INFO_MMAP_VALID),
	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
	.rate_min =		5500,
	.rate_max =		48000,
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	(128*1024),
	.period_bytes_min =	64,
	.period_bytes_max =	(128*1024),
	.periods_min =		1,
	.periods_max =		1024,
	.fifo_size =		0,
};

598
static struct snd_pcm_hardware snd_fm801_capture =
L
Linus Torvalds 已提交
599 600 601
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
T
Takashi Iwai 已提交
602
				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
L
Linus Torvalds 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
				 SNDRV_PCM_INFO_MMAP_VALID),
	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
	.rate_min =		5500,
	.rate_max =		48000,
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	(128*1024),
	.period_bytes_min =	64,
	.period_bytes_max =	(128*1024),
	.periods_min =		1,
	.periods_max =		1024,
	.fifo_size =		0,
};

618
static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
619
{
620 621
	struct fm801 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
622 623 624 625
	int err;

	chip->playback_substream = substream;
	runtime->hw = snd_fm801_playback;
626 627
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				   &hw_constraints_rates);
L
Linus Torvalds 已提交
628 629
	if (chip->multichannel) {
		runtime->hw.channels_max = 6;
630 631 632
		snd_pcm_hw_constraint_list(runtime, 0,
					   SNDRV_PCM_HW_PARAM_CHANNELS,
					   &hw_constraints_channels);
L
Linus Torvalds 已提交
633 634 635 636 637 638
	}
	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
		return err;
	return 0;
}

639
static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
640
{
641 642
	struct fm801 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
643 644 645 646
	int err;

	chip->capture_substream = substream;
	runtime->hw = snd_fm801_capture;
647 648
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				   &hw_constraints_rates);
L
Linus Torvalds 已提交
649 650 651 652 653
	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
		return err;
	return 0;
}

654
static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
655
{
656
	struct fm801 *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
657 658 659 660 661

	chip->playback_substream = NULL;
	return 0;
}

662
static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
663
{
664
	struct fm801 *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
665 666 667 668 669

	chip->capture_substream = NULL;
	return 0;
}

670
static struct snd_pcm_ops snd_fm801_playback_ops = {
L
Linus Torvalds 已提交
671 672 673 674 675 676 677 678 679 680
	.open =		snd_fm801_playback_open,
	.close =	snd_fm801_playback_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_fm801_hw_params,
	.hw_free =	snd_fm801_hw_free,
	.prepare =	snd_fm801_playback_prepare,
	.trigger =	snd_fm801_playback_trigger,
	.pointer =	snd_fm801_playback_pointer,
};

681
static struct snd_pcm_ops snd_fm801_capture_ops = {
L
Linus Torvalds 已提交
682 683 684 685 686 687 688 689 690 691
	.open =		snd_fm801_capture_open,
	.close =	snd_fm801_capture_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_fm801_hw_params,
	.hw_free =	snd_fm801_hw_free,
	.prepare =	snd_fm801_capture_prepare,
	.trigger =	snd_fm801_capture_trigger,
	.pointer =	snd_fm801_capture_pointer,
};

692
static int __devinit snd_fm801_pcm(struct fm801 *chip, int device, struct snd_pcm ** rpcm)
L
Linus Torvalds 已提交
693
{
694
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
	int err;

	if (rpcm)
		*rpcm = NULL;
	if ((err = snd_pcm_new(chip->card, "FM801", device, 1, 1, &pcm)) < 0)
		return err;

	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_fm801_playback_ops);
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);

	pcm->private_data = chip;
	pcm->info_flags = 0;
	strcpy(pcm->name, "FM801");
	chip->pcm = pcm;

	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
					      snd_dma_pci_data(chip->pci),
					      chip->multichannel ? 128*1024 : 64*1024, 128*1024);

714 715 716 717 718 719 720
	err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
				     snd_pcm_alt_chmaps,
				     chip->multichannel ? 6 : 2, 0,
				     NULL);
	if (err < 0)
		return err;

L
Linus Torvalds 已提交
721 722 723 724 725 726 727 728 729
	if (rpcm)
		*rpcm = pcm;
	return 0;
}

/*
 *  TEA5757 radio
 */

730
#ifdef CONFIG_SND_FM801_TEA575X_BOOL
L
Linus Torvalds 已提交
731

732 733 734
/* GPIO to TEA575x maps */
struct snd_fm801_tea575x_gpio {
	u8 data, clk, wren, most;
735
	char *name;
736
};
L
Linus Torvalds 已提交
737

738
static struct snd_fm801_tea575x_gpio snd_fm801_tea575x_gpios[] = {
739 740 741
	{ .data = 1, .clk = 3, .wren = 2, .most = 0, .name = "SF256-PCS" },
	{ .data = 1, .clk = 0, .wren = 2, .most = 3, .name = "SF256-PCP" },
	{ .data = 2, .clk = 0, .wren = 1, .most = 3, .name = "SF64-PCR" },
742
};
L
Linus Torvalds 已提交
743

744 745 746
#define get_tea575x_gpio(chip) \
	(&snd_fm801_tea575x_gpios[((chip)->tea575x_tuner & TUNER_TYPE_MASK) - 1])

747
static void snd_fm801_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
L
Linus Torvalds 已提交
748
{
749
	struct fm801 *chip = tea->private_data;
750
	unsigned short reg = inw(FM801_REG(chip, GPIO_CTRL));
751
	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
L
Linus Torvalds 已提交
752

753 754 755
	reg &= ~(FM801_GPIO_GP(gpio.data) |
		 FM801_GPIO_GP(gpio.clk) |
		 FM801_GPIO_GP(gpio.wren));
L
Linus Torvalds 已提交
756

757 758 759 760
	reg |= (pins & TEA575X_DATA) ? FM801_GPIO_GP(gpio.data) : 0;
	reg |= (pins & TEA575X_CLK)  ? FM801_GPIO_GP(gpio.clk) : 0;
	/* WRITE_ENABLE is inverted */
	reg |= (pins & TEA575X_WREN) ? 0 : FM801_GPIO_GP(gpio.wren);
L
Linus Torvalds 已提交
761 762 763 764

	outw(reg, FM801_REG(chip, GPIO_CTRL));
}

765
static u8 snd_fm801_tea575x_get_pins(struct snd_tea575x *tea)
L
Linus Torvalds 已提交
766
{
767
	struct fm801 *chip = tea->private_data;
768
	unsigned short reg = inw(FM801_REG(chip, GPIO_CTRL));
769
	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
L
Linus Torvalds 已提交
770

771 772
	return  (reg & FM801_GPIO_GP(gpio.data)) ? TEA575X_DATA : 0 |
		(reg & FM801_GPIO_GP(gpio.most)) ? TEA575X_MOST : 0;
L
Linus Torvalds 已提交
773 774
}

775
static void snd_fm801_tea575x_set_direction(struct snd_tea575x *tea, bool output)
L
Linus Torvalds 已提交
776
{
777
	struct fm801 *chip = tea->private_data;
778
	unsigned short reg = inw(FM801_REG(chip, GPIO_CTRL));
779
	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
L
Linus Torvalds 已提交
780 781

	/* use GPIO lines and set write enable bit */
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
	reg |= FM801_GPIO_GS(gpio.data) |
	       FM801_GPIO_GS(gpio.wren) |
	       FM801_GPIO_GS(gpio.clk) |
	       FM801_GPIO_GS(gpio.most);
	if (output) {
		/* all of lines are in the write direction */
		/* clear data and clock lines */
		reg &= ~(FM801_GPIO_GD(gpio.data) |
			 FM801_GPIO_GD(gpio.wren) |
			 FM801_GPIO_GD(gpio.clk) |
			 FM801_GPIO_GP(gpio.data) |
			 FM801_GPIO_GP(gpio.clk) |
			 FM801_GPIO_GP(gpio.wren));
	} else {
		/* use GPIO lines, set data direction to input */
		reg |= FM801_GPIO_GD(gpio.data) |
		       FM801_GPIO_GD(gpio.most) |
		       FM801_GPIO_GP(gpio.data) |
		       FM801_GPIO_GP(gpio.most) |
		       FM801_GPIO_GP(gpio.wren);
		/* all of lines are in the write direction, except data */
		/* clear data, write enable and clock lines */
		reg &= ~(FM801_GPIO_GD(gpio.wren) |
			 FM801_GPIO_GD(gpio.clk) |
			 FM801_GPIO_GP(gpio.clk));
L
Linus Torvalds 已提交
807 808
	}

809 810 811
	outw(reg, FM801_REG(chip, GPIO_CTRL));
}

812 813 814 815
static struct snd_tea575x_ops snd_fm801_tea_ops = {
	.set_pins = snd_fm801_tea575x_set_pins,
	.get_pins = snd_fm801_tea575x_get_pins,
	.set_direction = snd_fm801_tea575x_set_direction,
L
Linus Torvalds 已提交
816 817 818 819 820 821 822 823 824 825 826 827
};
#endif

/*
 *  Mixer routines
 */

#define FM801_SINGLE(xname, reg, shift, mask, invert) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_single, \
  .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
  .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }

828 829
static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
830 831 832 833 834 835 836 837 838 839
{
	int mask = (kcontrol->private_value >> 16) & 0xff;

	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = mask;
	return 0;
}

840 841
static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
842
{
843
	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
844 845 846 847 848 849 850 851 852 853 854
	int reg = kcontrol->private_value & 0xff;
	int shift = (kcontrol->private_value >> 8) & 0xff;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	int invert = (kcontrol->private_value >> 24) & 0xff;

	ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift) & mask;
	if (invert)
		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
	return 0;
}

855 856
static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
857
{
858
	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
	int reg = kcontrol->private_value & 0xff;
	int shift = (kcontrol->private_value >> 8) & 0xff;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	int invert = (kcontrol->private_value >> 24) & 0xff;
	unsigned short val;

	val = (ucontrol->value.integer.value[0] & mask);
	if (invert)
		val = mask - val;
	return snd_fm801_update_bits(chip, reg, mask << shift, val << shift);
}

#define FM801_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_fm801_info_double, \
  .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
  .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
875 876 877 878 879 880 881
#define FM801_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
  .name = xname, .info = snd_fm801_info_double, \
  .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
  .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
  .tlv = { .p = (xtlv) } }
L
Linus Torvalds 已提交
882

883 884
static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
885 886 887 888 889 890 891 892 893 894
{
	int mask = (kcontrol->private_value >> 16) & 0xff;

	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = mask;
	return 0;
}

895 896
static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
897
{
898
	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
        int reg = kcontrol->private_value & 0xff;
	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	int invert = (kcontrol->private_value >> 24) & 0xff;

	spin_lock_irq(&chip->reg_lock);
	ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift_left) & mask;
	ucontrol->value.integer.value[1] = (inw(chip->port + reg) >> shift_right) & mask;
	spin_unlock_irq(&chip->reg_lock);
	if (invert) {
		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
	}
	return 0;
}

916 917
static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
918
{
919
	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
	int reg = kcontrol->private_value & 0xff;
	int shift_left = (kcontrol->private_value >> 8) & 0x0f;
	int shift_right = (kcontrol->private_value >> 12) & 0x0f;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	int invert = (kcontrol->private_value >> 24) & 0xff;
	unsigned short val1, val2;
 
	val1 = ucontrol->value.integer.value[0] & mask;
	val2 = ucontrol->value.integer.value[1] & mask;
	if (invert) {
		val1 = mask - val1;
		val2 = mask - val2;
	}
	return snd_fm801_update_bits(chip, reg,
				     (mask << shift_left) | (mask << shift_right),
				     (val1 << shift_left ) | (val2 << shift_right));
}

938 939
static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952 953
{
	static char *texts[5] = {
		"AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
	};
 
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 5;
	if (uinfo->value.enumerated.item > 4)
		uinfo->value.enumerated.item = 4;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

954 955
static int snd_fm801_get_mux(struct snd_kcontrol *kcontrol,
			     struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
956
{
957
	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
958 959 960 961 962 963 964 965 966
        unsigned short val;
 
	val = inw(FM801_REG(chip, REC_SRC)) & 7;
	if (val > 4)
		val = 4;
        ucontrol->value.enumerated.item[0] = val;
        return 0;
}

967 968
static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
			     struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
969
{
970
	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
971 972 973 974 975 976 977
        unsigned short val;
 
        if ((val = ucontrol->value.enumerated.item[0]) > 4)
                return -EINVAL;
	return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
}

978
static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
979

980
#define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
L
Linus Torvalds 已提交
981

982
static struct snd_kcontrol_new snd_fm801_controls[] __devinitdata = {
983 984
FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
		 db_scale_dsp),
L
Linus Torvalds 已提交
985
FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
986 987
FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
		 db_scale_dsp),
L
Linus Torvalds 已提交
988
FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
989 990
FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
		 db_scale_dsp),
L
Linus Torvalds 已提交
991 992 993 994 995 996 997 998 999 1000
FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
{
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Digital Capture Source",
	.info = snd_fm801_info_mux,
	.get = snd_fm801_get_mux,
	.put = snd_fm801_put_mux,
}
};

1001
#define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
L
Linus Torvalds 已提交
1002

1003
static struct snd_kcontrol_new snd_fm801_controls_multi[] __devinitdata = {
L
Linus Torvalds 已提交
1004 1005
FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
1006 1007 1008 1009
FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
L
Linus Torvalds 已提交
1010 1011
};

1012
static void snd_fm801_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
L
Linus Torvalds 已提交
1013
{
1014
	struct fm801 *chip = bus->private_data;
L
Linus Torvalds 已提交
1015 1016 1017
	chip->ac97_bus = NULL;
}

1018
static void snd_fm801_mixer_free_ac97(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
1019
{
1020
	struct fm801 *chip = ac97->private_data;
L
Linus Torvalds 已提交
1021 1022 1023 1024 1025 1026 1027
	if (ac97->num == 0) {
		chip->ac97 = NULL;
	} else {
		chip->ac97_sec = NULL;
	}
}

1028
static int __devinit snd_fm801_mixer(struct fm801 *chip)
L
Linus Torvalds 已提交
1029
{
1030
	struct snd_ac97_template ac97;
L
Linus Torvalds 已提交
1031 1032
	unsigned int i;
	int err;
1033
	static struct snd_ac97_bus_ops ops = {
L
Linus Torvalds 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
		.write = snd_fm801_codec_write,
		.read = snd_fm801_codec_read,
	};

	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
		return err;
	chip->ac97_bus->private_free = snd_fm801_mixer_free_ac97_bus;

	memset(&ac97, 0, sizeof(ac97));
	ac97.private_data = chip;
	ac97.private_free = snd_fm801_mixer_free_ac97;
	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
		return err;
	if (chip->secondary) {
		ac97.num = 1;
		ac97.addr = chip->secondary_addr;
		if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97_sec)) < 0)
			return err;
	}
	for (i = 0; i < FM801_CONTROLS; i++)
		snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls[i], chip));
	if (chip->multichannel) {
		for (i = 0; i < FM801_CONTROLS_MULTI; i++)
			snd_ctl_add(chip->card, snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
	}
	return 0;
}

/*
 *  initialization routines
 */

T
Takashi Iwai 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
			  unsigned short reg, unsigned long waits)
{
	unsigned long timeout = jiffies + waits;

	outw(FM801_AC97_READ | (codec_id << FM801_AC97_ADDR_SHIFT) | reg,
	     FM801_REG(chip, AC97_CMD));
	udelay(5);
	do {
		if ((inw(FM801_REG(chip, AC97_CMD)) & (FM801_AC97_VALID|FM801_AC97_BUSY))
		    == FM801_AC97_VALID)
			return 0;
		schedule_timeout_uninterruptible(1);
	} while (time_after(timeout, jiffies));
	return -EIO;
}

static int snd_fm801_chip_init(struct fm801 *chip, int resume)
{
	unsigned short cmdw;

1087
	if (chip->tea575x_tuner & TUNER_ONLY)
1088 1089
		goto __ac97_ok;

T
Takashi Iwai 已提交
1090 1091 1092 1093 1094 1095
	/* codec cold reset + AC'97 warm reset */
	outw((1<<5) | (1<<6), FM801_REG(chip, CODEC_CTRL));
	inw(FM801_REG(chip, CODEC_CTRL)); /* flush posting data */
	udelay(100);
	outw(0, FM801_REG(chip, CODEC_CTRL));

1096 1097 1098 1099 1100 1101 1102
	if (wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750)) < 0)
		if (!resume) {
			snd_printk(KERN_INFO "Primary AC'97 codec not found, "
					    "assume SF64-PCR (tuner-only)\n");
			chip->tea575x_tuner = 3 | TUNER_ONLY;
			goto __ac97_ok;
		}
T
Takashi Iwai 已提交
1103 1104 1105 1106 1107 1108 1109 1110

	if (chip->multichannel) {
		if (chip->secondary_addr) {
			wait_for_codec(chip, chip->secondary_addr,
				       AC97_VENDOR_ID1, msecs_to_jiffies(50));
		} else {
			/* my card has the secondary codec */
			/* at address #3, so the loop is inverted */
1111 1112 1113
			int i;
			for (i = 3; i > 0; i--) {
				if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
T
Takashi Iwai 已提交
1114 1115 1116 1117
						     msecs_to_jiffies(50))) {
					cmdw = inw(FM801_REG(chip, AC97_DATA));
					if (cmdw != 0xffff && cmdw != 0) {
						chip->secondary = 1;
1118
						chip->secondary_addr = i;
T
Takashi Iwai 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
						break;
					}
				}
			}
		}

		/* the recovery phase, it seems that probing for non-existing codec might */
		/* cause timeout problems */
		wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
	}

1130 1131
      __ac97_ok:

T
Takashi Iwai 已提交
1132 1133 1134 1135 1136 1137 1138 1139
	/* init volume */
	outw(0x0808, FM801_REG(chip, PCM_VOL));
	outw(0x9f1f, FM801_REG(chip, FM_VOL));
	outw(0x8808, FM801_REG(chip, I2S_VOL));

	/* I2S control - I2S mode */
	outw(0x0003, FM801_REG(chip, I2S_MODE));

1140
	/* interrupt setup */
T
Takashi Iwai 已提交
1141
	cmdw = inw(FM801_REG(chip, IRQ_MASK));
1142 1143 1144 1145
	if (chip->irq < 0)
		cmdw |= 0x00c3;		/* mask everything, no PCM nor MPU */
	else
		cmdw &= ~0x0083;	/* unmask MPU, PLAYBACK & CAPTURE */
T
Takashi Iwai 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154
	outw(cmdw, FM801_REG(chip, IRQ_MASK));

	/* interrupt clear */
	outw(FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU, FM801_REG(chip, IRQ_STATUS));

	return 0;
}


1155
static int snd_fm801_free(struct fm801 *chip)
L
Linus Torvalds 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
{
	unsigned short cmdw;

	if (chip->irq < 0)
		goto __end_hw;

	/* interrupt setup - mask everything */
	cmdw = inw(FM801_REG(chip, IRQ_MASK));
	cmdw |= 0x00c3;
	outw(cmdw, FM801_REG(chip, IRQ_MASK));

      __end_hw:
1168
#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1169
	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
1170
		snd_tea575x_exit(&chip->tea);
1171 1172
		v4l2_device_unregister(&chip->v4l2_dev);
	}
L
Linus Torvalds 已提交
1173 1174
#endif
	if (chip->irq >= 0)
1175
		free_irq(chip->irq, chip);
L
Linus Torvalds 已提交
1176 1177 1178 1179 1180 1181 1182
	pci_release_regions(chip->pci);
	pci_disable_device(chip->pci);

	kfree(chip);
	return 0;
}

1183
static int snd_fm801_dev_free(struct snd_device *device)
L
Linus Torvalds 已提交
1184
{
1185
	struct fm801 *chip = device->device_data;
L
Linus Torvalds 已提交
1186 1187 1188
	return snd_fm801_free(chip);
}

1189
static int __devinit snd_fm801_create(struct snd_card *card,
L
Linus Torvalds 已提交
1190 1191
				      struct pci_dev * pci,
				      int tea575x_tuner,
1192
				      int radio_nr,
1193
				      struct fm801 ** rchip)
L
Linus Torvalds 已提交
1194
{
1195
	struct fm801 *chip;
L
Linus Torvalds 已提交
1196
	int err;
1197
	static struct snd_device_ops ops = {
L
Linus Torvalds 已提交
1198 1199 1200 1201 1202 1203
		.dev_free =	snd_fm801_dev_free,
	};

	*rchip = NULL;
	if ((err = pci_enable_device(pci)) < 0)
		return err;
1204
	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
L
Linus Torvalds 已提交
1205 1206 1207 1208 1209 1210 1211 1212
	if (chip == NULL) {
		pci_disable_device(pci);
		return -ENOMEM;
	}
	spin_lock_init(&chip->reg_lock);
	chip->card = card;
	chip->pci = pci;
	chip->irq = -1;
1213
	chip->tea575x_tuner = tea575x_tuner;
L
Linus Torvalds 已提交
1214 1215 1216 1217 1218 1219
	if ((err = pci_request_regions(pci, "FM801")) < 0) {
		kfree(chip);
		pci_disable_device(pci);
		return err;
	}
	chip->port = pci_resource_start(pci, 0);
1220
	if ((tea575x_tuner & TUNER_ONLY) == 0) {
1221
		if (request_irq(pci->irq, snd_fm801_interrupt, IRQF_SHARED,
1222
				KBUILD_MODNAME, chip)) {
1223 1224 1225 1226 1227 1228
			snd_printk(KERN_ERR "unable to grab IRQ %d\n", chip->irq);
			snd_fm801_free(chip);
			return -EBUSY;
		}
		chip->irq = pci->irq;
		pci_set_master(pci);
L
Linus Torvalds 已提交
1229 1230
	}

1231
	if (pci->revision >= 0xb1)	/* FM801-AU */
L
Linus Torvalds 已提交
1232 1233
		chip->multichannel = 1;

T
Takashi Iwai 已提交
1234
	snd_fm801_chip_init(chip, 0);
1235 1236 1237 1238 1239 1240 1241 1242
	/* init might set tuner access method */
	tea575x_tuner = chip->tea575x_tuner;

	if (chip->irq >= 0 && (tea575x_tuner & TUNER_ONLY)) {
		pci_clear_master(pci);
		free_irq(chip->irq, chip);
		chip->irq = -1;
	}
L
Linus Torvalds 已提交
1243 1244 1245 1246 1247 1248 1249 1250

	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
		snd_fm801_free(chip);
		return err;
	}

	snd_card_set_dev(card, &pci->dev);

1251
#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1252 1253 1254 1255 1256 1257 1258
	err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
	if (err < 0) {
		snd_fm801_free(chip);
		return err;
	}
	chip->tea.v4l2_dev = &chip->v4l2_dev;
	chip->tea.radio_nr = radio_nr;
1259 1260
	chip->tea.private_data = chip;
	chip->tea.ops = &snd_fm801_tea_ops;
1261
	sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
1262 1263
	if ((tea575x_tuner & TUNER_TYPE_MASK) > 0 &&
	    (tea575x_tuner & TUNER_TYPE_MASK) < 4) {
1264
		if (snd_tea575x_init(&chip->tea, THIS_MODULE)) {
1265
			snd_printk(KERN_ERR "TEA575x radio not found\n");
1266
			snd_fm801_free(chip);
1267 1268 1269
			return -ENODEV;
		}
	} else if ((tea575x_tuner & TUNER_TYPE_MASK) == 0) {
1270 1271 1272
		/* autodetect tuner connection */
		for (tea575x_tuner = 1; tea575x_tuner <= 3; tea575x_tuner++) {
			chip->tea575x_tuner = tea575x_tuner;
1273
			if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
1274
				snd_printk(KERN_INFO "detected TEA575x radio type %s\n",
1275
					   get_tea575x_gpio(chip)->name);
1276 1277 1278
				break;
			}
		}
1279 1280
		if (tea575x_tuner == 4) {
			snd_printk(KERN_ERR "TEA575x radio not found\n");
1281
			chip->tea575x_tuner = TUNER_DISABLED;
1282 1283
		}
	}
1284
	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
1285
		strlcpy(chip->tea.card, get_tea575x_gpio(chip)->name,
1286 1287
			sizeof(chip->tea.card));
	}
L
Linus Torvalds 已提交
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
#endif

	*rchip = chip;
	return 0;
}

static int __devinit snd_card_fm801_probe(struct pci_dev *pci,
					  const struct pci_device_id *pci_id)
{
	static int dev;
1298 1299 1300
	struct snd_card *card;
	struct fm801 *chip;
	struct snd_opl3 *opl3;
L
Linus Torvalds 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309
	int err;

        if (dev >= SNDRV_CARDS)
                return -ENODEV;
	if (!enable[dev]) {
		dev++;
		return -ENOENT;
	}

1310 1311 1312
	err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
	if (err < 0)
		return err;
1313
	if ((err = snd_fm801_create(card, pci, tea575x_tuner[dev], radio_nr[dev], &chip)) < 0) {
L
Linus Torvalds 已提交
1314 1315 1316
		snd_card_free(card);
		return err;
	}
T
Takashi Iwai 已提交
1317
	card->private_data = chip;
L
Linus Torvalds 已提交
1318 1319 1320 1321 1322 1323 1324

	strcpy(card->driver, "FM801");
	strcpy(card->shortname, "ForteMedia FM801-");
	strcat(card->shortname, chip->multichannel ? "AU" : "AS");
	sprintf(card->longname, "%s at 0x%lx, irq %i",
		card->shortname, chip->port, chip->irq);

1325
	if (chip->tea575x_tuner & TUNER_ONLY)
1326 1327
		goto __fm801_tuner_only;

L
Linus Torvalds 已提交
1328 1329 1330 1331 1332 1333 1334 1335 1336
	if ((err = snd_fm801_pcm(chip, 0, NULL)) < 0) {
		snd_card_free(card);
		return err;
	}
	if ((err = snd_fm801_mixer(chip)) < 0) {
		snd_card_free(card);
		return err;
	}
	if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_FM801,
1337
				       FM801_REG(chip, MPU401_DATA),
1338 1339 1340
				       MPU401_INFO_INTEGRATED |
				       MPU401_INFO_IRQ_HOOK,
				       -1, &chip->rmidi)) < 0) {
L
Linus Torvalds 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
		snd_card_free(card);
		return err;
	}
	if ((err = snd_opl3_create(card, FM801_REG(chip, OPL3_BANK0),
				   FM801_REG(chip, OPL3_BANK1),
				   OPL3_HW_OPL3_FM801, 1, &opl3)) < 0) {
		snd_card_free(card);
		return err;
	}
	if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
		snd_card_free(card);
		return err;
	}

1355
      __fm801_tuner_only:
L
Linus Torvalds 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
	if ((err = snd_card_register(card)) < 0) {
		snd_card_free(card);
		return err;
	}
	pci_set_drvdata(pci, card);
	dev++;
	return 0;
}

static void __devexit snd_card_fm801_remove(struct pci_dev *pci)
{
	snd_card_free(pci_get_drvdata(pci));
	pci_set_drvdata(pci, NULL);
}

1371
#ifdef CONFIG_PM_SLEEP
T
Takashi Iwai 已提交
1372 1373 1374 1375 1376 1377 1378
static unsigned char saved_regs[] = {
	FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
	FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
	FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
	FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
};

1379
static int snd_fm801_suspend(struct device *dev)
T
Takashi Iwai 已提交
1380
{
1381 1382
	struct pci_dev *pci = to_pci_dev(dev);
	struct snd_card *card = dev_get_drvdata(dev);
T
Takashi Iwai 已提交
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
	struct fm801 *chip = card->private_data;
	int i;

	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
	snd_pcm_suspend_all(chip->pcm);
	snd_ac97_suspend(chip->ac97);
	snd_ac97_suspend(chip->ac97_sec);
	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
		chip->saved_regs[i] = inw(chip->port + saved_regs[i]);
	/* FIXME: tea575x suspend */

	pci_disable_device(pci);
	pci_save_state(pci);
1396
	pci_set_power_state(pci, PCI_D3hot);
T
Takashi Iwai 已提交
1397 1398 1399
	return 0;
}

1400
static int snd_fm801_resume(struct device *dev)
T
Takashi Iwai 已提交
1401
{
1402 1403
	struct pci_dev *pci = to_pci_dev(dev);
	struct snd_card *card = dev_get_drvdata(dev);
T
Takashi Iwai 已提交
1404 1405 1406 1407
	struct fm801 *chip = card->private_data;
	int i;

	pci_set_power_state(pci, PCI_D0);
1408 1409 1410 1411 1412 1413 1414
	pci_restore_state(pci);
	if (pci_enable_device(pci) < 0) {
		printk(KERN_ERR "fm801: pci_enable_device failed, "
		       "disabling device\n");
		snd_card_disconnect(card);
		return -EIO;
	}
T
Takashi Iwai 已提交
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
	pci_set_master(pci);

	snd_fm801_chip_init(chip, 1);
	snd_ac97_resume(chip->ac97);
	snd_ac97_resume(chip->ac97_sec);
	for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
		outw(chip->saved_regs[i], chip->port + saved_regs[i]);

	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
	return 0;
}
1426 1427 1428 1429 1430

static SIMPLE_DEV_PM_OPS(snd_fm801_pm, snd_fm801_suspend, snd_fm801_resume);
#define SND_FM801_PM_OPS	&snd_fm801_pm
#else
#define SND_FM801_PM_OPS	NULL
1431
#endif /* CONFIG_PM_SLEEP */
T
Takashi Iwai 已提交
1432

1433
static struct pci_driver fm801_driver = {
1434
	.name = KBUILD_MODNAME,
L
Linus Torvalds 已提交
1435 1436 1437
	.id_table = snd_fm801_ids,
	.probe = snd_card_fm801_probe,
	.remove = __devexit_p(snd_card_fm801_remove),
1438 1439 1440
	.driver = {
		.pm = SND_FM801_PM_OPS,
	},
L
Linus Torvalds 已提交
1441 1442
};

1443
module_pci_driver(fm801_driver);