fm801.c 41.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
/*
 *  The driver for the ForteMedia FM801 based soundcards
3
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds's avatar
Linus Torvalds committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 *   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.
 *
 */

#include <linux/delay.h>
#include <linux/init.h>
#include <linux/interrupt.h>
20
#include <linux/io.h>
Linus Torvalds's avatar
Linus Torvalds committed
21
22
#include <linux/pci.h>
#include <linux/slab.h>
23
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
24
25
#include <sound/core.h>
#include <sound/pcm.h>
26
#include <sound/tlv.h>
Linus Torvalds's avatar
Linus Torvalds committed
27
28
29
30
31
#include <sound/ac97_codec.h>
#include <sound/mpu401.h>
#include <sound/opl3.h>
#include <sound/initval.h>

32
#ifdef CONFIG_SND_FM801_TEA575X_BOOL
33
#include <media/drv-intf/tea575x.h>
Linus Torvalds's avatar
Linus Torvalds committed
34
35
#endif

36
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds's avatar
Linus Torvalds committed
37
38
39
40
41
42
43
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 */
44
static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
Linus Torvalds's avatar
Linus Torvalds committed
45
46
47
/*
 *  Enable TEA575x tuner
 *    1 = MediaForte 256-PCS
48
 *    2 = MediaForte 256-PCP
Linus Torvalds's avatar
Linus Torvalds committed
49
 *    3 = MediaForte 64-PCR
50
 *   16 = setup tuner only (this is additional bit), i.e. SF64-PCR FM card
Linus Torvalds's avatar
Linus Torvalds committed
51
52
 *  High 16-bits are video (radio) device number + 1
 */
53
static int tea575x_tuner[SNDRV_CARDS];
54
static int radio_nr[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
Linus Torvalds's avatar
Linus Torvalds committed
55
56
57
58
59
60
61
62

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);
63
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).");
64
65
66
module_param_array(radio_nr, int, NULL, 0444);
MODULE_PARM_DESC(radio_nr, "Radio device numbers");

67

68
#define TUNER_DISABLED		(1<<3)
69
70
#define TUNER_ONLY		(1<<4)
#define TUNER_TYPE_MASK		(~TUNER_ONLY & 0xFFFF)
Linus Torvalds's avatar
Linus Torvalds committed
71
72
73
74
75

/*
 *  Direct registers
 */

76
77
78
79
#define fm801_writew(chip,reg,value)	outw((value), chip->port + FM801_##reg)
#define fm801_readw(chip,reg)		inw(chip->port + FM801_##reg)

#define fm801_writel(chip,reg,value)	outl((value), chip->port + FM801_##reg)
Linus Torvalds's avatar
Linus Torvalds committed
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

#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 */

Takashi Iwai's avatar
Takashi Iwai committed
111
112
113
114
115
/* 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) */
Linus Torvalds's avatar
Linus Torvalds committed
116
117
118
119
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

/* 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)))
	
155
156
157
158
159
160
161
162
163
/**
 * struct fm801 - describes FM801 chip
 * @port:		I/O port number
 * @multichannel:	multichannel support
 * @secondary:		secondary codec
 * @secondary_addr:	address of the secondary codec
 * @tea575x_tuner:	tuner access method & flags
 * @ply_ctrl:		playback control
 * @cap_ctrl:		capture control
Linus Torvalds's avatar
Linus Torvalds committed
164
 */
165
struct fm801 {
166
	struct device *dev;
Linus Torvalds's avatar
Linus Torvalds committed
167
168
	int irq;

169
170
171
172
173
	unsigned long port;
	unsigned int multichannel: 1,
		     secondary: 1;
	unsigned char secondary_addr;
	unsigned int tea575x_tuner;
Linus Torvalds's avatar
Linus Torvalds committed
174

175
176
	unsigned short ply_ctrl;
	unsigned short cap_ctrl;
Linus Torvalds's avatar
Linus Torvalds committed
177
178
179
180
181
182
183
184
185
186
187
188
189

	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;

190
191
192
	struct snd_ac97_bus *ac97_bus;
	struct snd_ac97 *ac97;
	struct snd_ac97 *ac97_sec;
Linus Torvalds's avatar
Linus Torvalds committed
193

194
195
196
197
198
	struct snd_card *card;
	struct snd_pcm *pcm;
	struct snd_rawmidi *rmidi;
	struct snd_pcm_substream *playback_substream;
	struct snd_pcm_substream *capture_substream;
Linus Torvalds's avatar
Linus Torvalds committed
199
200
201
202
	unsigned int p_dma_size;
	unsigned int c_dma_size;

	spinlock_t reg_lock;
203
	struct snd_info_entry *proc_entry;
Linus Torvalds's avatar
Linus Torvalds committed
204

205
#ifdef CONFIG_SND_FM801_TEA575X_BOOL
206
	struct v4l2_device v4l2_dev;
207
	struct snd_tea575x tea;
Linus Torvalds's avatar
Linus Torvalds committed
208
#endif
Takashi Iwai's avatar
Takashi Iwai committed
209

210
#ifdef CONFIG_PM_SLEEP
Takashi Iwai's avatar
Takashi Iwai committed
211
212
	u16 saved_regs[0x20];
#endif
Linus Torvalds's avatar
Linus Torvalds committed
213
214
};

215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * IO accessors
 */

static inline void fm801_iowrite16(struct fm801 *chip, unsigned short offset, u16 value)
{
	outw(value, chip->port + offset);
}

static inline u16 fm801_ioread16(struct fm801 *chip, unsigned short offset)
{
	return inw(chip->port + offset);
}

229
static const struct pci_device_id snd_fm801_ids[] = {
Linus Torvalds's avatar
Linus Torvalds committed
230
	{ 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* FM801 */
231
	{ 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* Gallant Odyssey Sound 4 */
Linus Torvalds's avatar
Linus Torvalds committed
232
233
234
235
236
237
238
239
240
	{ 0, }
};

MODULE_DEVICE_TABLE(pci, snd_fm801_ids);

/*
 *  common I/O routines
 */

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
static bool fm801_ac97_is_ready(struct fm801 *chip, unsigned int iterations)
{
	unsigned int idx;

	for (idx = 0; idx < iterations; idx++) {
		if (!(fm801_readw(chip, AC97_CMD) & FM801_AC97_BUSY))
			return true;
		udelay(10);
	}
	return false;
}

static bool fm801_ac97_is_valid(struct fm801 *chip, unsigned int iterations)
{
	unsigned int idx;

	for (idx = 0; idx < iterations; idx++) {
		if (fm801_readw(chip, AC97_CMD) & FM801_AC97_VALID)
			return true;
		udelay(10);
	}
	return false;
}

265
static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
Linus Torvalds's avatar
Linus Torvalds committed
266
267
268
269
270
271
272
				 unsigned short mask, unsigned short value)
{
	int change;
	unsigned long flags;
	unsigned short old, new;

	spin_lock_irqsave(&chip->reg_lock, flags);
273
	old = fm801_ioread16(chip, reg);
Linus Torvalds's avatar
Linus Torvalds committed
274
275
276
	new = (old & ~mask) | value;
	change = old != new;
	if (change)
277
		fm801_iowrite16(chip, reg, new);
Linus Torvalds's avatar
Linus Torvalds committed
278
279
280
281
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	return change;
}

282
static void snd_fm801_codec_write(struct snd_ac97 *ac97,
Linus Torvalds's avatar
Linus Torvalds committed
283
284
285
				  unsigned short reg,
				  unsigned short val)
{
286
	struct fm801 *chip = ac97->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
287
288
289
290

	/*
	 *  Wait until the codec interface is not ready..
	 */
291
292
293
	if (!fm801_ac97_is_ready(chip, 100)) {
		dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
		return;
Linus Torvalds's avatar
Linus Torvalds committed
294
295
296
	}

	/* write data and address */
297
298
	fm801_writew(chip, AC97_DATA, val);
	fm801_writew(chip, AC97_CMD, reg | (ac97->addr << FM801_AC97_ADDR_SHIFT));
Linus Torvalds's avatar
Linus Torvalds committed
299
300
	/*
	 *  Wait until the write command is not completed..
301
302
303
304
	 */
	if (!fm801_ac97_is_ready(chip, 1000))
		dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
		ac97->num);
Linus Torvalds's avatar
Linus Torvalds committed
305
306
}

307
static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds's avatar
Linus Torvalds committed
308
{
309
	struct fm801 *chip = ac97->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
310
311
312
313

	/*
	 *  Wait until the codec interface is not ready..
	 */
314
315
316
	if (!fm801_ac97_is_ready(chip, 100)) {
		dev_err(chip->card->dev, "AC'97 interface is busy (1)\n");
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
317
318
319
	}

	/* read command */
320
321
	fm801_writew(chip, AC97_CMD,
		     reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
322
323
324
325
	if (!fm801_ac97_is_ready(chip, 100)) {
		dev_err(chip->card->dev, "AC'97 interface #%d is busy (2)\n",
			ac97->num);
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
326
327
	}

328
329
330
331
	if (!fm801_ac97_is_valid(chip, 1000)) {
		dev_err(chip->card->dev,
			"AC'97 interface #%d is not valid (2)\n", ac97->num);
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
332
333
	}

334
	return fm801_readw(chip, AC97_DATA);
Linus Torvalds's avatar
Linus Torvalds committed
335
336
}

337
static const unsigned int rates[] = {
Linus Torvalds's avatar
Linus Torvalds committed
338
339
340
341
342
  5500,  8000,  9600, 11025,
  16000, 19200, 22050, 32000,
  38400, 44100, 48000
};

343
static const struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Linus Torvalds's avatar
Linus Torvalds committed
344
345
346
347
348
	.count = ARRAY_SIZE(rates),
	.list = rates,
	.mask = 0,
};

349
static const unsigned int channels[] = {
Linus Torvalds's avatar
Linus Torvalds committed
350
351
352
  2, 4, 6
};

353
static const struct snd_pcm_hw_constraint_list hw_constraints_channels = {
354
	.count = ARRAY_SIZE(channels),
Linus Torvalds's avatar
Linus Torvalds committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
	.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
 */

378
static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
379
380
				      int cmd)
{
381
	struct fm801 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
382
383
384
385
386
387
388
389
390
391
392
393
394
395

	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:
Takashi Iwai's avatar
Takashi Iwai committed
396
	case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds's avatar
Linus Torvalds committed
397
398
399
		chip->ply_ctrl |= FM801_PAUSE;
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai's avatar
Takashi Iwai committed
400
	case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds's avatar
Linus Torvalds committed
401
402
403
404
405
406
407
		chip->ply_ctrl &= ~FM801_PAUSE;
		break;
	default:
		spin_unlock(&chip->reg_lock);
		snd_BUG();
		return -EINVAL;
	}
408
	fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
Linus Torvalds's avatar
Linus Torvalds committed
409
410
411
412
	spin_unlock(&chip->reg_lock);
	return 0;
}

413
static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
414
415
				     int cmd)
{
416
	struct fm801 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430

	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:
Takashi Iwai's avatar
Takashi Iwai committed
431
	case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds's avatar
Linus Torvalds committed
432
433
434
		chip->cap_ctrl |= FM801_PAUSE;
		break;
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai's avatar
Takashi Iwai committed
435
	case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds's avatar
Linus Torvalds committed
436
437
438
439
440
441
442
		chip->cap_ctrl &= ~FM801_PAUSE;
		break;
	default:
		spin_unlock(&chip->reg_lock);
		snd_BUG();
		return -EINVAL;
	}
443
	fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
Linus Torvalds's avatar
Linus Torvalds committed
444
445
446
447
	spin_unlock(&chip->reg_lock);
	return 0;
}

448
449
static int snd_fm801_hw_params(struct snd_pcm_substream *substream,
			       struct snd_pcm_hw_params *hw_params)
Linus Torvalds's avatar
Linus Torvalds committed
450
451
452
453
{
	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
}

454
static int snd_fm801_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
455
456
457
458
{
	return snd_pcm_lib_free_pages(substream);
}

459
static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
460
{
461
462
	struct fm801 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480

	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;
481
482
	fm801_writew(chip, PLY_CTRL, chip->ply_ctrl);
	fm801_writew(chip, PLY_COUNT, chip->ply_count - 1);
Linus Torvalds's avatar
Linus Torvalds committed
483
484
	chip->ply_buffer = runtime->dma_addr;
	chip->ply_pos = 0;
485
486
487
	fm801_writel(chip, PLY_BUF1, chip->ply_buffer);
	fm801_writel(chip, PLY_BUF2,
		     chip->ply_buffer + (chip->ply_count % chip->ply_size));
Linus Torvalds's avatar
Linus Torvalds committed
488
489
490
491
	spin_unlock_irq(&chip->reg_lock);
	return 0;
}

492
static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
493
{
494
495
	struct fm801 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
496
497
498
499
500
501
502
503
504
505
506
507

	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;
508
509
	fm801_writew(chip, CAP_CTRL, chip->cap_ctrl);
	fm801_writew(chip, CAP_COUNT, chip->cap_count - 1);
Linus Torvalds's avatar
Linus Torvalds committed
510
511
	chip->cap_buffer = runtime->dma_addr;
	chip->cap_pos = 0;
512
513
514
	fm801_writel(chip, CAP_BUF1, chip->cap_buffer);
	fm801_writel(chip, CAP_BUF2,
		     chip->cap_buffer + (chip->cap_count % chip->cap_size));
Linus Torvalds's avatar
Linus Torvalds committed
515
516
517
518
	spin_unlock_irq(&chip->reg_lock);
	return 0;
}

519
static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
520
{
521
	struct fm801 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
522
523
524
525
526
	size_t ptr;

	if (!(chip->ply_ctrl & FM801_START))
		return 0;
	spin_lock(&chip->reg_lock);
527
528
	ptr = chip->ply_pos + (chip->ply_count - 1) - fm801_readw(chip, PLY_COUNT);
	if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_PLAYBACK) {
Linus Torvalds's avatar
Linus Torvalds committed
529
530
531
532
533
534
535
		ptr += chip->ply_count;
		ptr %= chip->ply_size;
	}
	spin_unlock(&chip->reg_lock);
	return bytes_to_frames(substream->runtime, ptr);
}

536
static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
537
{
538
	struct fm801 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
539
540
541
542
543
	size_t ptr;

	if (!(chip->cap_ctrl & FM801_START))
		return 0;
	spin_lock(&chip->reg_lock);
544
545
	ptr = chip->cap_pos + (chip->cap_count - 1) - fm801_readw(chip, CAP_COUNT);
	if (fm801_readw(chip, IRQ_STATUS) & FM801_IRQ_CAPTURE) {
Linus Torvalds's avatar
Linus Torvalds committed
546
547
548
549
550
551
552
		ptr += chip->cap_count;
		ptr %= chip->cap_size;
	}
	spin_unlock(&chip->reg_lock);
	return bytes_to_frames(substream->runtime, ptr);
}

553
static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
554
{
555
	struct fm801 *chip = dev_id;
Linus Torvalds's avatar
Linus Torvalds committed
556
557
558
	unsigned short status;
	unsigned int tmp;

559
	status = fm801_readw(chip, IRQ_STATUS);
Linus Torvalds's avatar
Linus Torvalds committed
560
561
562
563
	status &= FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU|FM801_IRQ_VOLUME;
	if (! status)
		return IRQ_NONE;
	/* ack first */
564
	fm801_writew(chip, IRQ_STATUS, status);
Linus Torvalds's avatar
Linus Torvalds committed
565
566
567
568
569
570
571
	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;
572
573
574
575
		if (chip->ply_buf & 1)
			fm801_writel(chip, PLY_BUF1, chip->ply_buffer + tmp);
		else
			fm801_writel(chip, PLY_BUF2, chip->ply_buffer + tmp);
Linus Torvalds's avatar
Linus Torvalds committed
576
577
578
579
580
581
582
583
584
585
		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;
586
587
588
589
		if (chip->cap_buf & 1)
			fm801_writel(chip, CAP_BUF1, chip->cap_buffer + tmp);
		else
			fm801_writel(chip, CAP_BUF2, chip->cap_buffer + tmp);
Linus Torvalds's avatar
Linus Torvalds committed
590
591
592
593
		spin_unlock(&chip->reg_lock);
		snd_pcm_period_elapsed(chip->capture_substream);
	}
	if (chip->rmidi && (status & FM801_IRQ_MPU))
594
		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
595
596
597
	if (status & FM801_IRQ_VOLUME) {
		/* TODO */
	}
Linus Torvalds's avatar
Linus Torvalds committed
598
599
600
601

	return IRQ_HANDLED;
}

602
static const struct snd_pcm_hardware snd_fm801_playback =
Linus Torvalds's avatar
Linus Torvalds committed
603
604
605
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai's avatar
Takashi Iwai committed
606
				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
Linus Torvalds's avatar
Linus Torvalds committed
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
				 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,
};

622
static const struct snd_pcm_hardware snd_fm801_capture =
Linus Torvalds's avatar
Linus Torvalds committed
623
624
625
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Takashi Iwai's avatar
Takashi Iwai committed
626
				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
Linus Torvalds's avatar
Linus Torvalds committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
				 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,
};

642
static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
643
{
644
645
	struct fm801 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
646
647
648
649
	int err;

	chip->playback_substream = substream;
	runtime->hw = snd_fm801_playback;
650
651
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				   &hw_constraints_rates);
Linus Torvalds's avatar
Linus Torvalds committed
652
653
	if (chip->multichannel) {
		runtime->hw.channels_max = 6;
654
655
656
		snd_pcm_hw_constraint_list(runtime, 0,
					   SNDRV_PCM_HW_PARAM_CHANNELS,
					   &hw_constraints_channels);
Linus Torvalds's avatar
Linus Torvalds committed
657
658
659
660
661
662
	}
	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
		return err;
	return 0;
}

663
static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
664
{
665
666
	struct fm801 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
667
668
669
670
	int err;

	chip->capture_substream = substream;
	runtime->hw = snd_fm801_capture;
671
672
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				   &hw_constraints_rates);
Linus Torvalds's avatar
Linus Torvalds committed
673
674
675
676
677
	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
		return err;
	return 0;
}

678
static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
679
{
680
	struct fm801 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
681
682
683
684
685

	chip->playback_substream = NULL;
	return 0;
}

686
static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
687
{
688
	struct fm801 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds's avatar
Linus Torvalds committed
689
690
691
692
693

	chip->capture_substream = NULL;
	return 0;
}

694
static const struct snd_pcm_ops snd_fm801_playback_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
695
696
697
698
699
700
701
702
703
704
	.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,
};

705
static const struct snd_pcm_ops snd_fm801_capture_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
706
707
708
709
710
711
712
713
714
715
	.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,
};

716
static int snd_fm801_pcm(struct fm801 *chip, int device)
Linus Torvalds's avatar
Linus Torvalds committed
717
{
718
	struct pci_dev *pdev = to_pci_dev(chip->dev);
719
	struct snd_pcm *pcm;
Linus Torvalds's avatar
Linus Torvalds committed
720
721
722
723
724
725
726
727
728
729
730
731
732
733
	int err;

	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,
734
					      snd_dma_pci_data(pdev),
Linus Torvalds's avatar
Linus Torvalds committed
735
736
					      chip->multichannel ? 128*1024 : 64*1024, 128*1024);

737
	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
738
739
740
				     snd_pcm_alt_chmaps,
				     chip->multichannel ? 6 : 2, 0,
				     NULL);
Linus Torvalds's avatar
Linus Torvalds committed
741
742
743
744
745
746
}

/*
 *  TEA5757 radio
 */

747
#ifdef CONFIG_SND_FM801_TEA575X_BOOL
Linus Torvalds's avatar
Linus Torvalds committed
748

749
750
751
/* GPIO to TEA575x maps */
struct snd_fm801_tea575x_gpio {
	u8 data, clk, wren, most;
752
	char *name;
753
};
Linus Torvalds's avatar
Linus Torvalds committed
754

755
static struct snd_fm801_tea575x_gpio snd_fm801_tea575x_gpios[] = {
756
757
758
	{ .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" },
759
};
Linus Torvalds's avatar
Linus Torvalds committed
760

761
762
763
#define get_tea575x_gpio(chip) \
	(&snd_fm801_tea575x_gpios[((chip)->tea575x_tuner & TUNER_TYPE_MASK) - 1])

764
static void snd_fm801_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
Linus Torvalds's avatar
Linus Torvalds committed
765
{
766
	struct fm801 *chip = tea->private_data;
767
	unsigned short reg = fm801_readw(chip, GPIO_CTRL);
768
	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
Linus Torvalds's avatar
Linus Torvalds committed
769

770
771
772
	reg &= ~(FM801_GPIO_GP(gpio.data) |
		 FM801_GPIO_GP(gpio.clk) |
		 FM801_GPIO_GP(gpio.wren));
Linus Torvalds's avatar
Linus Torvalds committed
773

774
775
776
777
	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);
Linus Torvalds's avatar
Linus Torvalds committed
778

779
	fm801_writew(chip, GPIO_CTRL, reg);
Linus Torvalds's avatar
Linus Torvalds committed
780
781
}

782
static u8 snd_fm801_tea575x_get_pins(struct snd_tea575x *tea)
Linus Torvalds's avatar
Linus Torvalds committed
783
{
784
	struct fm801 *chip = tea->private_data;
785
	unsigned short reg = fm801_readw(chip, GPIO_CTRL);
786
	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
787
788
789
790
791
792
793
794
	u8 ret;

	ret = 0;
	if (reg & FM801_GPIO_GP(gpio.data))
		ret |= TEA575X_DATA;
	if (reg & FM801_GPIO_GP(gpio.most))
		ret |= TEA575X_MOST;
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
795
796
}

797
static void snd_fm801_tea575x_set_direction(struct snd_tea575x *tea, bool output)
Linus Torvalds's avatar
Linus Torvalds committed
798
{
799
	struct fm801 *chip = tea->private_data;
800
	unsigned short reg = fm801_readw(chip, GPIO_CTRL);
801
	struct snd_fm801_tea575x_gpio gpio = *get_tea575x_gpio(chip);
Linus Torvalds's avatar
Linus Torvalds committed
802
803

	/* use GPIO lines and set write enable bit */
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
	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));
Linus Torvalds's avatar
Linus Torvalds committed
829
830
	}

831
	fm801_writew(chip, GPIO_CTRL, reg);
832
833
}

834
static const struct snd_tea575x_ops snd_fm801_tea_ops = {
835
836
837
	.set_pins = snd_fm801_tea575x_set_pins,
	.get_pins = snd_fm801_tea575x_get_pins,
	.set_direction = snd_fm801_tea575x_set_direction,
Linus Torvalds's avatar
Linus Torvalds committed
838
839
840
841
842
843
844
845
846
847
848
849
};
#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) }

850
851
static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
852
853
854
855
856
857
858
859
860
861
{
	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;
}

862
863
static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
864
{
865
	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
866
867
868
869
	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;
870
	long *value = ucontrol->value.integer.value;
Linus Torvalds's avatar
Linus Torvalds committed
871

872
	value[0] = (fm801_ioread16(chip, reg) >> shift) & mask;
Linus Torvalds's avatar
Linus Torvalds committed
873
	if (invert)
874
		value[0] = mask - value[0];
Linus Torvalds's avatar
Linus Torvalds committed
875
876
877
	return 0;
}

878
879
static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
880
{
881
	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
	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) }
898
899
900
901
902
903
904
#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) } }
Linus Torvalds's avatar
Linus Torvalds committed
905

906
907
static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
908
909
910
911
912
913
914
915
916
917
{
	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;
}

918
919
static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
920
{
921
	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
922
923
924
925
926
        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;
927
	long *value = ucontrol->value.integer.value;
Linus Torvalds's avatar
Linus Torvalds committed
928
929

	spin_lock_irq(&chip->reg_lock);
930
931
	value[0] = (fm801_ioread16(chip, reg) >> shift_left) & mask;
	value[1] = (fm801_ioread16(chip, reg) >> shift_right) & mask;
Linus Torvalds's avatar
Linus Torvalds committed
932
933
	spin_unlock_irq(&chip->reg_lock);
	if (invert) {
934
935
		value[0] = mask - value[0];
		value[1] = mask - value[1];
Linus Torvalds's avatar
Linus Torvalds committed
936
937
938
939
	}
	return 0;
}

940
941
static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
942
{
943
	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
	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));
}

962
963
static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_info *uinfo)
Linus Torvalds's avatar
Linus Torvalds committed
964
{
965
	static const char * const texts[5] = {
Linus Torvalds's avatar
Linus Torvalds committed
966
967
968
		"AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
	};
 
969
	return snd_ctl_enum_info(uinfo, 1, 5, texts);
Linus Torvalds's avatar
Linus Torvalds committed
970
971
}

972
973
static int snd_fm801_get_mux(struct snd_kcontrol *kcontrol,
			     struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
974
{
975
	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
976
977
        unsigned short val;
 
978
	val = fm801_readw(chip, REC_SRC) & 7;
Linus Torvalds's avatar
Linus Torvalds committed
979
980
981
982
983
984
	if (val > 4)
		val = 4;
        ucontrol->value.enumerated.item[0] = val;
        return 0;
}

985
986
static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
			     struct snd_ctl_elem_value *ucontrol)
Linus Torvalds's avatar
Linus Torvalds committed
987
{
988
	struct fm801 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds's avatar
Linus Torvalds committed
989
990
991
992
993
994
995
        unsigned short val;
 
        if ((val = ucontrol->value.enumerated.item[0]) > 4)
                return -EINVAL;
	return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
}

996
static const DECLARE_TLV_DB_SCALE(db_scale_dsp, -3450, 150, 0);
997

998
#define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
Linus Torvalds's avatar
Linus Torvalds committed
999

1000
static struct snd_kcontrol_new snd_fm801_controls[] = {
1001
1002
FM801_DOUBLE_TLV("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1,
		 db_scale_dsp),
Linus Torvalds's avatar
Linus Torvalds committed
1003
FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
1004
1005
FM801_DOUBLE_TLV("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1,
		 db_scale_dsp),
Linus Torvalds's avatar
Linus Torvalds committed
1006
FM801_SINGLE("I2S Playback Switch", FM801_I2S_VOL, 15, 1, 1),
1007
1008
FM801_DOUBLE_TLV("FM Playback Volume", FM801_FM_VOL, 0, 8, 31, 1,
		 db_scale_dsp),
Linus Torvalds's avatar
Linus Torvalds committed
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
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,
}
};

1019
#define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
Linus Torvalds's avatar
Linus Torvalds committed
1020

1021
static struct snd_kcontrol_new snd_fm801_controls_multi[] = {
Linus Torvalds's avatar
Linus Torvalds committed
1022
1023
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),
1024
1025
1026
1027
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),
Linus Torvalds's avatar
Linus Torvalds committed
1028
1029
};

1030
static void snd_fm801_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
Linus Torvalds's avatar
Linus Torvalds committed
1031
{
1032
	struct fm801 *chip = bus->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1033
1034
1035
	chip->ac97_bus = NULL;
}

1036
static void snd_fm801_mixer_free_ac97(struct snd_ac97 *ac97)
Linus Torvalds's avatar
Linus Torvalds committed
1037
{
1038
	struct fm801 *chip = ac97->private_data;
Linus Torvalds's avatar
Linus Torvalds committed
1039
1040
1041
1042
1043
1044
1045
	if (ac97->num == 0) {
		chip->ac97 = NULL;
	} else {
		chip->ac97_sec = NULL;
	}
}

1046
static int snd_fm801_mixer(struct fm801 *chip)
Linus Torvalds's avatar
Linus Torvalds committed
1047
{
1048
	struct snd_ac97_template ac97;
Linus Torvalds's avatar
Linus Torvalds committed
1049
1050
	unsigned int i;
	int err;
1051
	static struct snd_ac97_bus_ops ops = {
Linus Torvalds's avatar
Linus Torvalds committed
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
		.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;
	}
1071
1072
1073
1074
1075
1076
	for (i = 0; i < FM801_CONTROLS; i++) {
		err = snd_ctl_add(chip->card,
			snd_ctl_new1(&snd_fm801_controls[i], chip));
		if (err < 0)
			return err;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1077
	if (chip->multichannel) {
1078
1079
1080
1081
1082
1083
		for (i = 0; i < FM801_CONTROLS_MULTI; i++) {
			err = snd_ctl_add(chip->card,
				snd_ctl_new1(&snd_fm801_controls_multi[i], chip));
			if (err < 0)
				return err;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1084
1085
1086
1087
1088
1089
1090
1091
	}
	return 0;
}

/*
 *  initialization routines
 */

Takashi Iwai's avatar
Takashi Iwai committed
1092
1093
1094
1095
1096
static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
			  unsigned short reg, unsigned long waits)
{
	unsigned long timeout = jiffies + waits;

1097
1098
	fm801_writew(chip, AC97_CMD,
		     reg | (codec_id << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ);
Takashi Iwai's avatar
Takashi Iwai committed
1099
1100
	udelay(5);
	do {
1101
1102
		if ((fm801_readw(chip, AC97_CMD) &
		     (FM801_AC97_VALID | FM801_AC97_BUSY)) == FM801_AC97_VALID)
Takashi Iwai's avatar
Takashi Iwai committed
1103
1104
1105
1106
1107
1108
			return 0;
		schedule_timeout_uninterruptible(1);
	} while (time_after(timeout, jiffies));
	return -EIO;
}

1109
static int reset_codec(struct fm801 *chip)
Takashi Iwai's avatar
Takashi Iwai committed
1110
1111
{
	/* codec cold reset + AC'97 warm reset */
1112
1113
	fm801_writew(chip, CODEC_CTRL, (1 << 5) | (1 << 6));
	fm801_readw(chip, CODEC_CTRL); /* flush posting data */
Takashi Iwai's avatar
Takashi Iwai committed
1114
	udelay(100);
1115
	fm801_writew(chip, CODEC_CTRL, 0);
Takashi Iwai's avatar
Takashi Iwai committed
1116

1117
1118
1119
1120
1121
1122
	return wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750));
}

static void snd_fm801_chip_multichannel_init(struct fm801 *chip)
{
	unsigned short cmdw;
Takashi Iwai's avatar
Takashi Iwai committed
1123
1124
1125
1126
1127
1128
1129
1130

	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 */
1131
1132
1133
			int i;
			for (i = 3; i > 0; i--) {
				if (!wait_for_codec(chip, i, AC97_VENDOR_ID1,
Takashi Iwai's avatar
Takashi Iwai committed
1134
						     msecs_to_jiffies(50))) {
1135
					cmdw = fm801_readw(chip, AC97_DATA);
Takashi Iwai's avatar
Takashi Iwai committed
1136
1137
					if (cmdw != 0xffff && cmdw != 0) {
						chip->secondary = 1;
1138
						chip->secondary_addr = i;
Takashi Iwai's avatar
Takashi Iwai committed
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
						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));
	}
1149
}
Takashi Iwai's avatar
Takashi Iwai committed
1150

1151
1152
1153
static void snd_fm801_chip_init(struct fm801 *chip)
{
	unsigned short cmdw;
1154

Takashi Iwai's avatar
Takashi Iwai committed
1155
	/* init volume */
1156
1157
1158
	fm801_writew(chip, PCM_VOL, 0x0808);
	fm801_writew(chip, FM_VOL, 0x9f1f);
	fm801_writew(chip, I2S_VOL, 0x8808);
Takashi Iwai's avatar
Takashi Iwai committed
1159
1160

	/* I2S control - I2S mode */
1161
	fm801_writew(chip, I2S_MODE, 0x0003);
Takashi Iwai's avatar
Takashi Iwai committed
1162

1163
	/* interrupt setup */
1164
	cmdw = fm801_readw(chip, IRQ_MASK);
1165
1166
1167
1168
	if (chip->irq < 0)
		cmdw |= 0x00c3;		/* mask everything, no PCM nor MPU */
	else
		cmdw &= ~0x0083;	/* unmask MPU, PLAYBACK & CAPTURE */
1169
	fm801_writew(chip, IRQ_MASK, cmdw);
Takashi Iwai's avatar
Takashi Iwai committed
1170
1171

	/* interrupt clear */
1172
1173
	fm801_writew(chip, IRQ_STATUS,
		     FM801_IRQ_PLAYBACK | FM801_IRQ_CAPTURE | FM801_IRQ_MPU);
Takashi Iwai's avatar
Takashi Iwai committed
1174
1175
}

1176
static int snd_fm801_free(struct fm801 *chip)
Linus Torvalds's avatar
Linus Torvalds committed
1177
1178
1179
1180
1181
1182
1183
{
	unsigned short cmdw;

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

	/* interrupt setup - mask everything */
1184
	cmdw = fm801_readw(chip, IRQ_MASK);
Linus Torvalds's avatar
Linus Torvalds committed
1185
	cmdw |= 0x00c3;
1186
	fm801_writew(chip, IRQ_MASK, cmdw);
Linus Torvalds's avatar
Linus Torvalds committed
1187

1188
	devm_free_irq(chip->dev, chip->irq, chip);
1189

Linus Torvalds's avatar
Linus Torvalds committed
1190
      __end_hw:
1191
#ifdef CONFIG_SND_FM801_TEA575X_BOOL
1192
	if (!(chip->tea575x_tuner & TUNER_DISABLED)) {
1193
		snd_tea575x_exit(&chip->tea);
1194
1195
		v4l2_device_unregister(&chip->v4l2_dev);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1196
1197
1198
1199
#endif
	return 0;
}

1200
static int snd_fm801_dev_free(struct snd_device *device)
Linus Torvalds's avatar
Linus Torvalds committed
1201
{
1202
	struct fm801 *chip = device->device_data;
Linus Torvalds's avatar
Linus Torvalds committed
1203
1204
1205
	return snd_fm801_free(chip);
}

1206
1207
1208
1209
1210
static int snd_fm801_create(struct snd_card *card,
			    struct pci_dev *pci,
			    int tea575x_tuner,
			    int radio_nr,
			    struct fm801 **rchip)
Linus Torvalds's avatar
Linus Torvalds committed
1211
{
1212
	struct fm801 *chip;
Linus Torvalds's avatar
Linus Torvalds committed
1213
	int err;
1214
	static struct snd_device_ops ops = {
Linus Torvalds's avatar
Linus Torvalds committed
1215
1216
1217
1218
		.dev_free =	snd_fm801_dev_free,
	};

	*rchip = NULL;