ymfpci_main.c 71.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *  Routines for control of YMF724/740/744/754 chips
 *
 *   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>
22
#include <linux/firmware.h>
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>

#include <sound/core.h>
#include <sound/control.h>
#include <sound/info.h>
33
#include <sound/tlv.h>
L
Linus Torvalds 已提交
34 35 36 37 38
#include <sound/ymfpci.h>
#include <sound/asoundef.h>
#include <sound/mpu401.h>

#include <asm/io.h>
39
#include <asm/byteorder.h>
L
Linus Torvalds 已提交
40 41 42 43 44

/*
 *  common I/O routines
 */

45
static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
L
Linus Torvalds 已提交
46

47
static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
L
Linus Torvalds 已提交
48 49 50 51
{
	return readb(chip->reg_area_virt + offset);
}

52
static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
L
Linus Torvalds 已提交
53 54 55 56
{
	writeb(val, chip->reg_area_virt + offset);
}

57
static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
L
Linus Torvalds 已提交
58 59 60 61
{
	return readw(chip->reg_area_virt + offset);
}

62
static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
L
Linus Torvalds 已提交
63 64 65 66
{
	writew(val, chip->reg_area_virt + offset);
}

67
static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
L
Linus Torvalds 已提交
68 69 70 71
{
	return readl(chip->reg_area_virt + offset);
}

72
static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
L
Linus Torvalds 已提交
73 74 75 76
{
	writel(val, chip->reg_area_virt + offset);
}

77
static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
L
Linus Torvalds 已提交
78
{
79
	unsigned long end_time;
L
Linus Torvalds 已提交
80 81
	u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
	
82
	end_time = jiffies + msecs_to_jiffies(750);
L
Linus Torvalds 已提交
83 84 85
	do {
		if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
			return 0;
86
		schedule_timeout_uninterruptible(1);
87
	} while (time_before(jiffies, end_time));
88
	snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg));
L
Linus Torvalds 已提交
89 90 91
	return -EBUSY;
}

92
static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
L
Linus Torvalds 已提交
93
{
94
	struct snd_ymfpci *chip = ac97->private_data;
L
Linus Torvalds 已提交
95 96 97 98 99 100 101
	u32 cmd;
	
	snd_ymfpci_codec_ready(chip, 0);
	cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
	snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
}

102
static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
L
Linus Torvalds 已提交
103
{
104
	struct snd_ymfpci *chip = ac97->private_data;
L
Linus Torvalds 已提交
105 106 107 108 109 110 111 112 113 114 115 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

	if (snd_ymfpci_codec_ready(chip, 0))
		return ~0;
	snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
	if (snd_ymfpci_codec_ready(chip, 0))
		return ~0;
	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
		int i;
		for (i = 0; i < 600; i++)
			snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
	}
	return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
}

/*
 *  Misc routines
 */

static u32 snd_ymfpci_calc_delta(u32 rate)
{
	switch (rate) {
	case 8000:	return 0x02aaab00;
	case 11025:	return 0x03accd00;
	case 16000:	return 0x05555500;
	case 22050:	return 0x07599a00;
	case 32000:	return 0x0aaaab00;
	case 44100:	return 0x0eb33300;
	default:	return ((rate << 16) / 375) << 5;
	}
}

static u32 def_rate[8] = {
	100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
};

static u32 snd_ymfpci_calc_lpfK(u32 rate)
{
	u32 i;
	static u32 val[8] = {
		0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
		0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
	};
	
	if (rate == 44100)
		return 0x40000000;	/* FIXME: What's the right value? */
	for (i = 0; i < 8; i++)
		if (rate <= def_rate[i])
			return val[i];
	return val[0];
}

static u32 snd_ymfpci_calc_lpfQ(u32 rate)
{
	u32 i;
	static u32 val[8] = {
		0x35280000, 0x34A70000, 0x32020000, 0x31770000,
		0x31390000, 0x31C90000, 0x33D00000, 0x40000000
	};
	
	if (rate == 44100)
		return 0x370A0000;
	for (i = 0; i < 8; i++)
		if (rate <= def_rate[i])
			return val[i];
	return val[0];
}

/*
 *  Hardware start management
 */

176
static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189
{
	unsigned long flags;

	spin_lock_irqsave(&chip->reg_lock, flags);
	if (chip->start_count++ > 0)
		goto __end;
	snd_ymfpci_writel(chip, YDSXGR_MODE,
			  snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
	chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
      __end:
      	spin_unlock_irqrestore(&chip->reg_lock, flags);
}

190
static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
L
Linus Torvalds 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
{
	unsigned long flags;
	long timeout = 1000;

	spin_lock_irqsave(&chip->reg_lock, flags);
	if (--chip->start_count > 0)
		goto __end;
	snd_ymfpci_writel(chip, YDSXGR_MODE,
			  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
	while (timeout-- > 0) {
		if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
			break;
	}
	if (atomic_read(&chip->interrupt_sleep_count)) {
		atomic_set(&chip->interrupt_sleep_count, 0);
		wake_up(&chip->interrupt_sleep);
	}
      __end:
      	spin_unlock_irqrestore(&chip->reg_lock, flags);
}

/*
 *  Playback voice management
 */

216 217 218
static int voice_alloc(struct snd_ymfpci *chip,
		       enum snd_ymfpci_voice_type type, int pair,
		       struct snd_ymfpci_voice **rvoice)
L
Linus Torvalds 已提交
219
{
220
	struct snd_ymfpci_voice *voice, *voice2;
L
Linus Torvalds 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	int idx;
	
	*rvoice = NULL;
	for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
		voice = &chip->voices[idx];
		voice2 = pair ? &chip->voices[idx+1] : NULL;
		if (voice->use || (voice2 && voice2->use))
			continue;
		voice->use = 1;
		if (voice2)
			voice2->use = 1;
		switch (type) {
		case YMFPCI_PCM:
			voice->pcm = 1;
			if (voice2)
				voice2->pcm = 1;
			break;
		case YMFPCI_SYNTH:
			voice->synth = 1;
			break;
		case YMFPCI_MIDI:
			voice->midi = 1;
			break;
		}
		snd_ymfpci_hw_start(chip);
		if (voice2)
			snd_ymfpci_hw_start(chip);
		*rvoice = voice;
		return 0;
	}
	return -ENOMEM;
}

254 255 256
static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
				  enum snd_ymfpci_voice_type type, int pair,
				  struct snd_ymfpci_voice **rvoice)
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
{
	unsigned long flags;
	int result;
	
	snd_assert(rvoice != NULL, return -EINVAL);
	snd_assert(!pair || type == YMFPCI_PCM, return -EINVAL);
	
	spin_lock_irqsave(&chip->voice_lock, flags);
	for (;;) {
		result = voice_alloc(chip, type, pair, rvoice);
		if (result == 0 || type != YMFPCI_PCM)
			break;
		/* TODO: synth/midi voice deallocation */
		break;
	}
	spin_unlock_irqrestore(&chip->voice_lock, flags);	
	return result;		
}

276
static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
L
Linus Torvalds 已提交
277 278 279 280 281 282
{
	unsigned long flags;
	
	snd_assert(pvoice != NULL, return -EINVAL);
	snd_ymfpci_hw_stop(chip);
	spin_lock_irqsave(&chip->voice_lock, flags);
283 284 285 286
	if (pvoice->number == chip->src441_used) {
		chip->src441_used = -1;
		pvoice->ypcm->use_441_slot = 0;
	}
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297
	pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
	pvoice->ypcm = NULL;
	pvoice->interrupt = NULL;
	spin_unlock_irqrestore(&chip->voice_lock, flags);
	return 0;
}

/*
 *  PCM part
 */

298
static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
L
Linus Torvalds 已提交
299
{
300
	struct snd_ymfpci_pcm *ypcm;
L
Linus Torvalds 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	u32 pos, delta;
	
	if ((ypcm = voice->ypcm) == NULL)
		return;
	if (ypcm->substream == NULL)
		return;
	spin_lock(&chip->reg_lock);
	if (ypcm->running) {
		pos = le32_to_cpu(voice->bank[chip->active_bank].start);
		if (pos < ypcm->last_pos)
			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
		else
			delta = pos - ypcm->last_pos;
		ypcm->period_pos += delta;
		ypcm->last_pos = pos;
		if (ypcm->period_pos >= ypcm->period_size) {
			// printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
			ypcm->period_pos %= ypcm->period_size;
			spin_unlock(&chip->reg_lock);
			snd_pcm_period_elapsed(ypcm->substream);
			spin_lock(&chip->reg_lock);
		}
323 324 325 326

		if (unlikely(ypcm->update_pcm_vol)) {
			unsigned int subs = ypcm->substream->number;
			unsigned int next_bank = 1 - chip->active_bank;
327
			struct snd_ymfpci_playback_bank *bank;
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
			u32 volume;
			
			bank = &voice->bank[next_bank];
			volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
			bank->left_gain_end = volume;
			if (ypcm->output_rear)
				bank->eff2_gain_end = volume;
			if (ypcm->voices[1])
				bank = &ypcm->voices[1]->bank[next_bank];
			volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
			bank->right_gain_end = volume;
			if (ypcm->output_rear)
				bank->eff3_gain_end = volume;
			ypcm->update_pcm_vol--;
		}
L
Linus Torvalds 已提交
343 344 345 346
	}
	spin_unlock(&chip->reg_lock);
}

347
static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
348
{
349 350 351
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
	struct snd_ymfpci *chip = ypcm->chip;
L
Linus Torvalds 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
	u32 pos, delta;
	
	spin_lock(&chip->reg_lock);
	if (ypcm->running) {
		pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
		if (pos < ypcm->last_pos)
			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
		else
			delta = pos - ypcm->last_pos;
		ypcm->period_pos += delta;
		ypcm->last_pos = pos;
		if (ypcm->period_pos >= ypcm->period_size) {
			ypcm->period_pos %= ypcm->period_size;
			// printk("done - active_bank = 0x%x, start = 0x%x\n", chip->active_bank, voice->bank[chip->active_bank].start);
			spin_unlock(&chip->reg_lock);
			snd_pcm_period_elapsed(substream);
			spin_lock(&chip->reg_lock);
		}
	}
	spin_unlock(&chip->reg_lock);
}

374
static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
375 376
				       int cmd)
{
377 378
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
379
	struct snd_kcontrol *kctl = NULL;
L
Linus Torvalds 已提交
380 381 382 383 384 385 386 387 388 389 390 391
	int result = 0;

	spin_lock(&chip->reg_lock);
	if (ypcm->voices[0] == NULL) {
		result = -EINVAL;
		goto __unlock;
	}
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
	case SNDRV_PCM_TRIGGER_RESUME:
		chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
392
		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
L
Linus Torvalds 已提交
393 394 395 396
			chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
		ypcm->running = 1;
		break;
	case SNDRV_PCM_TRIGGER_STOP:
397 398 399 400 401
		if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
			kctl = chip->pcm_mixer[substream->number].ctl;
			kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
		}
		/* fall through */
L
Linus Torvalds 已提交
402 403 404
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
	case SNDRV_PCM_TRIGGER_SUSPEND:
		chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
405
		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
L
Linus Torvalds 已提交
406 407 408 409 410 411 412 413 414
			chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
		ypcm->running = 0;
		break;
	default:
		result = -EINVAL;
		break;
	}
      __unlock:
	spin_unlock(&chip->reg_lock);
415 416
	if (kctl)
		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
L
Linus Torvalds 已提交
417 418
	return result;
}
419
static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
420 421
				      int cmd)
{
422 423
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
L
Linus Torvalds 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
	int result = 0;
	u32 tmp;

	spin_lock(&chip->reg_lock);
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
	case SNDRV_PCM_TRIGGER_RESUME:
		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
		ypcm->running = 1;
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
	case SNDRV_PCM_TRIGGER_SUSPEND:
		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
		ypcm->running = 0;
		break;
	default:
		result = -EINVAL;
		break;
	}
	spin_unlock(&chip->reg_lock);
	return result;
}

451
static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
{
	int err;

	if (ypcm->voices[1] != NULL && voices < 2) {
		snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
		ypcm->voices[1] = NULL;
	}
	if (voices == 1 && ypcm->voices[0] != NULL)
		return 0;		/* already allocated */
	if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
		return 0;		/* already allocated */
	if (voices > 1) {
		if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
			snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
			ypcm->voices[0] = NULL;
		}		
	}
	err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
	if (err < 0)
		return err;
	ypcm->voices[0]->ypcm = ypcm;
	ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
	if (voices > 1) {
		ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
		ypcm->voices[1]->ypcm = ypcm;
	}
	return 0;
}

481 482
static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
				      struct snd_pcm_runtime *runtime,
483
				      int has_pcm_volume)
L
Linus Torvalds 已提交
484
{
485
	struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
L
Linus Torvalds 已提交
486
	u32 format;
487 488 489
	u32 delta = snd_ymfpci_calc_delta(runtime->rate);
	u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
	u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
490
	struct snd_ymfpci_playback_bank *bank;
L
Linus Torvalds 已提交
491
	unsigned int nbank;
492 493
	u32 vol_left, vol_right;
	u8 use_left, use_right;
494
	unsigned long flags;
L
Linus Torvalds 已提交
495 496

	snd_assert(voice != NULL, return);
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	if (runtime->channels == 1) {
		use_left = 1;
		use_right = 1;
	} else {
		use_left = (voiceidx & 1) == 0;
		use_right = !use_left;
	}
	if (has_pcm_volume) {
		vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
				       [ypcm->substream->number].left << 15);
		vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
					[ypcm->substream->number].right << 15);
	} else {
		vol_left = cpu_to_le32(0x40000000);
		vol_right = cpu_to_le32(0x40000000);
	}
513
	spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
514 515 516
	format = runtime->channels == 2 ? 0x00010000 : 0;
	if (snd_pcm_format_width(runtime->format) == 8)
		format |= 0x80000000;
517 518 519 520 521 522 523 524 525 526 527 528 529
	else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
		 runtime->rate == 44100 && runtime->channels == 2 &&
		 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
				   ypcm->chip->src441_used == voice->number)) {
		ypcm->chip->src441_used = voice->number;
		ypcm->use_441_slot = 1;
		format |= 0x10000000;
	}
	if (ypcm->chip->src441_used == voice->number &&
	    (format & 0x10000000) == 0) {
		ypcm->chip->src441_used = -1;
		ypcm->use_441_slot = 0;
	}
530 531
	if (runtime->channels == 2 && (voiceidx & 1) != 0)
		format |= 1;
532
	spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
L
Linus Torvalds 已提交
533 534
	for (nbank = 0; nbank < 2; nbank++) {
		bank = &voice->bank[nbank];
535
		memset(bank, 0, sizeof(*bank));
L
Linus Torvalds 已提交
536
		bank->format = cpu_to_le32(format);
537 538
		bank->base = cpu_to_le32(runtime->dma_addr);
		bank->loop_end = cpu_to_le32(ypcm->buffer_size);
L
Linus Torvalds 已提交
539 540 541 542 543
		bank->lpfQ = cpu_to_le32(lpfQ);
		bank->delta =
		bank->delta_end = cpu_to_le32(delta);
		bank->lpfK =
		bank->lpfK_end = cpu_to_le32(lpfK);
544 545 546 547 548 549 550 551 552
		bank->eg_gain =
		bank->eg_gain_end = cpu_to_le32(0x40000000);

		if (ypcm->output_front) {
			if (use_left) {
				bank->left_gain =
				bank->left_gain_end = vol_left;
			}
			if (use_right) {
L
Linus Torvalds 已提交
553
				bank->right_gain =
554
				bank->right_gain_end = vol_right;
L
Linus Torvalds 已提交
555
			}
556 557
		}
		if (ypcm->output_rear) {
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
		        if (!ypcm->swap_rear) {
        			if (use_left) {
        				bank->eff2_gain =
        				bank->eff2_gain_end = vol_left;
        			}
        			if (use_right) {
        				bank->eff3_gain =
        				bank->eff3_gain_end = vol_right;
        			}
		        } else {
        			/* The SPDIF out channels seem to be swapped, so we have
        			 * to swap them here, too.  The rear analog out channels
        			 * will be wrong, but otherwise AC3 would not work.
        			 */
        			if (use_left) {
        				bank->eff3_gain =
        				bank->eff3_gain_end = vol_left;
        			}
        			if (use_right) {
        				bank->eff2_gain =
        				bank->eff2_gain_end = vol_right;
        			}
        		}
                }
L
Linus Torvalds 已提交
582 583 584
	}
}

585
static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
L
Linus Torvalds 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
{
	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
				4096, &chip->ac3_tmp_base) < 0)
		return -ENOMEM;

	chip->bank_effect[3][0]->base =
	chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
	chip->bank_effect[3][0]->loop_end =
	chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
	chip->bank_effect[4][0]->base =
	chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
	chip->bank_effect[4][0]->loop_end =
	chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);

	spin_lock_irq(&chip->reg_lock);
	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
	spin_unlock_irq(&chip->reg_lock);
	return 0;
}

607
static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620
{
	spin_lock_irq(&chip->reg_lock);
	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
	spin_unlock_irq(&chip->reg_lock);
	// snd_ymfpci_irq_wait(chip);
	if (chip->ac3_tmp_base.area) {
		snd_dma_free_pages(&chip->ac3_tmp_base);
		chip->ac3_tmp_base.area = NULL;
	}
	return 0;
}

621 622
static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
					 struct snd_pcm_hw_params *hw_params)
L
Linus Torvalds 已提交
623
{
624 625
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
L
Linus Torvalds 已提交
626 627 628 629 630 631 632 633 634
	int err;

	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
		return err;
	if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
		return err;
	return 0;
}

635
static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
636
{
637 638 639
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ymfpci_pcm *ypcm;
L
Linus Torvalds 已提交
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
	
	if (runtime->private_data == NULL)
		return 0;
	ypcm = runtime->private_data;

	/* wait, until the PCI operations are not finished */
	snd_ymfpci_irq_wait(chip);
	snd_pcm_lib_free_pages(substream);
	if (ypcm->voices[1]) {
		snd_ymfpci_voice_free(chip, ypcm->voices[1]);
		ypcm->voices[1] = NULL;
	}
	if (ypcm->voices[0]) {
		snd_ymfpci_voice_free(chip, ypcm->voices[0]);
		ypcm->voices[0] = NULL;
	}
	return 0;
}

659
static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
660
{
661 662 663
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
664
	struct snd_kcontrol *kctl;
L
Linus Torvalds 已提交
665 666 667 668 669 670 671
	unsigned int nvoice;

	ypcm->period_size = runtime->period_size;
	ypcm->buffer_size = runtime->buffer_size;
	ypcm->period_pos = 0;
	ypcm->last_pos = 0;
	for (nvoice = 0; nvoice < runtime->channels; nvoice++)
672 673
		snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
					  substream->pcm == chip->pcm);
674 675 676 677 678 679

	if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
		kctl = chip->pcm_mixer[substream->number].ctl;
		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
	}
L
Linus Torvalds 已提交
680 681 682
	return 0;
}

683 684
static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
					struct snd_pcm_hw_params *hw_params)
L
Linus Torvalds 已提交
685 686 687 688
{
	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
}

689
static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
690
{
691
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
692 693 694 695 696 697

	/* wait, until the PCI operations are not finished */
	snd_ymfpci_irq_wait(chip);
	return snd_pcm_lib_free_pages(substream);
}

698
static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
699
{
700 701 702 703
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
	struct snd_ymfpci_capture_bank * bank;
L
Linus Torvalds 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
	int nbank;
	u32 rate, format;

	ypcm->period_size = runtime->period_size;
	ypcm->buffer_size = runtime->buffer_size;
	ypcm->period_pos = 0;
	ypcm->last_pos = 0;
	ypcm->shift = 0;
	rate = ((48000 * 4096) / runtime->rate) - 1;
	format = 0;
	if (runtime->channels == 2) {
		format |= 2;
		ypcm->shift++;
	}
	if (snd_pcm_format_width(runtime->format) == 8)
		format |= 1;
	else
		ypcm->shift++;
	switch (ypcm->capture_bank_number) {
	case 0:
		snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
		snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
		break;
	case 1:
		snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
		snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
		break;
	}
	for (nbank = 0; nbank < 2; nbank++) {
		bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
		bank->base = cpu_to_le32(runtime->dma_addr);
		bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
		bank->start = 0;
		bank->num_of_loops = 0;
	}
	return 0;
}

742
static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
743
{
744 745 746 747
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
	struct snd_ymfpci_voice *voice = ypcm->voices[0];
L
Linus Torvalds 已提交
748 749 750 751 752 753

	if (!(ypcm->running && voice))
		return 0;
	return le32_to_cpu(voice->bank[chip->active_bank].start);
}

754
static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
755
{
756 757 758
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
L
Linus Torvalds 已提交
759 760 761 762 763 764

	if (!ypcm->running)
		return 0;
	return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
}

765
static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
L
Linus Torvalds 已提交
766 767 768 769 770 771 772 773 774 775
{
	wait_queue_t wait;
	int loops = 4;

	while (loops-- > 0) {
		if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
		 	continue;
		init_waitqueue_entry(&wait, current);
		add_wait_queue(&chip->interrupt_sleep, &wait);
		atomic_inc(&chip->interrupt_sleep_count);
776
		schedule_timeout_uninterruptible(msecs_to_jiffies(50));
L
Linus Torvalds 已提交
777 778 779 780
		remove_wait_queue(&chip->interrupt_sleep, &wait);
	}
}

781
static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
782
{
783
	struct snd_ymfpci *chip = dev_id;
L
Linus Torvalds 已提交
784
	u32 status, nvoice, mode;
785
	struct snd_ymfpci_voice *voice;
L
Linus Torvalds 已提交
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826

	status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
	if (status & 0x80000000) {
		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
		spin_lock(&chip->voice_lock);
		for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
			voice = &chip->voices[nvoice];
			if (voice->interrupt)
				voice->interrupt(chip, voice);
		}
		for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
			if (chip->capture_substream[nvoice])
				snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
		}
#if 0
		for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
			if (chip->effect_substream[nvoice])
				snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
		}
#endif
		spin_unlock(&chip->voice_lock);
		spin_lock(&chip->reg_lock);
		snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
		mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
		snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
		spin_unlock(&chip->reg_lock);

		if (atomic_read(&chip->interrupt_sleep_count)) {
			atomic_set(&chip->interrupt_sleep_count, 0);
			wake_up(&chip->interrupt_sleep);
		}
	}

	status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
	if (status & 1) {
		if (chip->timer)
			snd_timer_interrupt(chip->timer, chip->timer->sticks);
	}
	snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);

	if (chip->rawmidi)
827
		snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
L
Linus Torvalds 已提交
828 829 830
	return IRQ_HANDLED;
}

831
static struct snd_pcm_hardware snd_ymfpci_playback =
L
Linus Torvalds 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
{
	.info =			(SNDRV_PCM_INFO_MMAP |
				 SNDRV_PCM_INFO_MMAP_VALID | 
				 SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
				 SNDRV_PCM_INFO_PAUSE |
				 SNDRV_PCM_INFO_RESUME),
	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
	.rate_min =		8000,
	.rate_max =		48000,
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
	.period_bytes_min =	64,
	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
	.periods_min =		3,
	.periods_max =		1024,
	.fifo_size =		0,
};

853
static struct snd_pcm_hardware snd_ymfpci_capture =
L
Linus Torvalds 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
{
	.info =			(SNDRV_PCM_INFO_MMAP |
				 SNDRV_PCM_INFO_MMAP_VALID |
				 SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
				 SNDRV_PCM_INFO_PAUSE |
				 SNDRV_PCM_INFO_RESUME),
	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
	.rate_min =		8000,
	.rate_max =		48000,
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
	.period_bytes_min =	64,
	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
	.periods_min =		3,
	.periods_max =		1024,
	.fifo_size =		0,
};

875
static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
L
Linus Torvalds 已提交
876
{
877
	kfree(runtime->private_data);
L
Linus Torvalds 已提交
878 879
}

880
static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
881
{
882 883 884
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ymfpci_pcm *ypcm;
L
Linus Torvalds 已提交
885

886
	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
L
Linus Torvalds 已提交
887 888 889 890 891 892 893 894 895 896 897 898 899 900
	if (ypcm == NULL)
		return -ENOMEM;
	ypcm->chip = chip;
	ypcm->type = PLAYBACK_VOICE;
	ypcm->substream = substream;
	runtime->hw = snd_ymfpci_playback;
	runtime->private_data = ypcm;
	runtime->private_free = snd_ymfpci_pcm_free_substream;
	/* FIXME? True value is 256/48 = 5.33333 ms */
	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
	return 0;
}

/* call with spinlock held */
901
static void ymfpci_open_extension(struct snd_ymfpci *chip)
L
Linus Torvalds 已提交
902 903 904 905 906 907 908 909 910 911 912 913
{
	if (! chip->rear_opened) {
		if (! chip->spdif_opened) /* set AC3 */
			snd_ymfpci_writel(chip, YDSXGR_MODE,
					  snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
		/* enable second codec (4CHEN) */
		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
	}
}

/* call with spinlock held */
914
static void ymfpci_close_extension(struct snd_ymfpci *chip)
L
Linus Torvalds 已提交
915 916 917 918 919 920 921 922 923 924
{
	if (! chip->rear_opened) {
		if (! chip->spdif_opened)
			snd_ymfpci_writel(chip, YDSXGR_MODE,
					  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
	}
}

925
static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
926
{
927 928 929
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ymfpci_pcm *ypcm;
L
Linus Torvalds 已提交
930 931 932 933 934 935 936
	int err;
	
	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
		return err;
	ypcm = runtime->private_data;
	ypcm->output_front = 1;
	ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
937
	ypcm->swap_rear = 0;
L
Linus Torvalds 已提交
938 939 940 941 942 943 944 945 946
	spin_lock_irq(&chip->reg_lock);
	if (ypcm->output_rear) {
		ymfpci_open_extension(chip);
		chip->rear_opened++;
	}
	spin_unlock_irq(&chip->reg_lock);
	return 0;
}

947
static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
948
{
949 950 951
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ymfpci_pcm *ypcm;
L
Linus Torvalds 已提交
952 953 954 955 956 957 958
	int err;
	
	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
		return err;
	ypcm = runtime->private_data;
	ypcm->output_front = 0;
	ypcm->output_rear = 1;
959
	ypcm->swap_rear = 1;
L
Linus Torvalds 已提交
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
	spin_lock_irq(&chip->reg_lock);
	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
	ymfpci_open_extension(chip);
	chip->spdif_pcm_bits = chip->spdif_bits;
	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
	chip->spdif_opened++;
	spin_unlock_irq(&chip->reg_lock);

	chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
	return 0;
}

975
static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
976
{
977 978 979
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ymfpci_pcm *ypcm;
L
Linus Torvalds 已提交
980 981 982 983 984 985 986
	int err;
	
	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
		return err;
	ypcm = runtime->private_data;
	ypcm->output_front = 0;
	ypcm->output_rear = 1;
987
	ypcm->swap_rear = 0;
L
Linus Torvalds 已提交
988 989 990 991 992 993 994
	spin_lock_irq(&chip->reg_lock);
	ymfpci_open_extension(chip);
	chip->rear_opened++;
	spin_unlock_irq(&chip->reg_lock);
	return 0;
}

995
static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
996 997
				   u32 capture_bank_number)
{
998 999 1000
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ymfpci_pcm *ypcm;
L
Linus Torvalds 已提交
1001

1002
	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
L
Linus Torvalds 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
	if (ypcm == NULL)
		return -ENOMEM;
	ypcm->chip = chip;
	ypcm->type = capture_bank_number + CAPTURE_REC;
	ypcm->substream = substream;	
	ypcm->capture_bank_number = capture_bank_number;
	chip->capture_substream[capture_bank_number] = substream;
	runtime->hw = snd_ymfpci_capture;
	/* FIXME? True value is 256/48 = 5.33333 ms */
	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 5333, UINT_MAX);
	runtime->private_data = ypcm;
	runtime->private_free = snd_ymfpci_pcm_free_substream;
	snd_ymfpci_hw_start(chip);
	return 0;
}

1019
static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1020 1021 1022 1023
{
	return snd_ymfpci_capture_open(substream, 0);
}

1024
static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1025 1026 1027 1028
{
	return snd_ymfpci_capture_open(substream, 1);
}

1029
static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1030 1031 1032 1033
{
	return 0;
}

1034
static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1035
{
1036 1037
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
L
Linus Torvalds 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

	spin_lock_irq(&chip->reg_lock);
	if (ypcm->output_rear && chip->rear_opened > 0) {
		chip->rear_opened--;
		ymfpci_close_extension(chip);
	}
	spin_unlock_irq(&chip->reg_lock);
	return snd_ymfpci_playback_close_1(substream);
}

1048
static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1049
{
1050
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

	spin_lock_irq(&chip->reg_lock);
	chip->spdif_opened = 0;
	ymfpci_close_extension(chip);
	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
	spin_unlock_irq(&chip->reg_lock);
	chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
	return snd_ymfpci_playback_close_1(substream);
}

1065
static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1066
{
1067
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

	spin_lock_irq(&chip->reg_lock);
	if (chip->rear_opened > 0) {
		chip->rear_opened--;
		ymfpci_close_extension(chip);
	}
	spin_unlock_irq(&chip->reg_lock);
	return snd_ymfpci_playback_close_1(substream);
}

1078
static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1079
{
1080 1081 1082
	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
L
Linus Torvalds 已提交
1083 1084 1085 1086 1087 1088 1089 1090

	if (ypcm != NULL) {
		chip->capture_substream[ypcm->capture_bank_number] = NULL;
		snd_ymfpci_hw_stop(chip);
	}
	return 0;
}

1091
static struct snd_pcm_ops snd_ymfpci_playback_ops = {
L
Linus Torvalds 已提交
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
	.open =			snd_ymfpci_playback_open,
	.close =		snd_ymfpci_playback_close,
	.ioctl =		snd_pcm_lib_ioctl,
	.hw_params =		snd_ymfpci_playback_hw_params,
	.hw_free =		snd_ymfpci_playback_hw_free,
	.prepare =		snd_ymfpci_playback_prepare,
	.trigger =		snd_ymfpci_playback_trigger,
	.pointer =		snd_ymfpci_playback_pointer,
};

1102
static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
L
Linus Torvalds 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
	.open =			snd_ymfpci_capture_rec_open,
	.close =		snd_ymfpci_capture_close,
	.ioctl =		snd_pcm_lib_ioctl,
	.hw_params =		snd_ymfpci_capture_hw_params,
	.hw_free =		snd_ymfpci_capture_hw_free,
	.prepare =		snd_ymfpci_capture_prepare,
	.trigger =		snd_ymfpci_capture_trigger,
	.pointer =		snd_ymfpci_capture_pointer,
};

1113
int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
L
Linus Torvalds 已提交
1114
{
1115
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
	int err;

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

	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);

	/* global setup */
	pcm->info_flags = 0;
	strcpy(pcm->name, "YMFPCI");
	chip->pcm = pcm;

	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);

	if (rpcm)
		*rpcm = pcm;
	return 0;
}

1140
static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
L
Linus Torvalds 已提交
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
	.open =			snd_ymfpci_capture_ac97_open,
	.close =		snd_ymfpci_capture_close,
	.ioctl =		snd_pcm_lib_ioctl,
	.hw_params =		snd_ymfpci_capture_hw_params,
	.hw_free =		snd_ymfpci_capture_hw_free,
	.prepare =		snd_ymfpci_capture_prepare,
	.trigger =		snd_ymfpci_capture_trigger,
	.pointer =		snd_ymfpci_capture_pointer,
};

1151
int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
L
Linus Torvalds 已提交
1152
{
1153
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
	int err;

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

	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);

	/* global setup */
	pcm->info_flags = 0;
	sprintf(pcm->name, "YMFPCI - %s",
		chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
	chip->pcm2 = pcm;

	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);

	if (rpcm)
		*rpcm = pcm;
	return 0;
}

1178
static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
L
Linus Torvalds 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	.open =			snd_ymfpci_playback_spdif_open,
	.close =		snd_ymfpci_playback_spdif_close,
	.ioctl =		snd_pcm_lib_ioctl,
	.hw_params =		snd_ymfpci_playback_hw_params,
	.hw_free =		snd_ymfpci_playback_hw_free,
	.prepare =		snd_ymfpci_playback_prepare,
	.trigger =		snd_ymfpci_playback_trigger,
	.pointer =		snd_ymfpci_playback_pointer,
};

1189
int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
L
Linus Torvalds 已提交
1190
{
1191
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
	int err;

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

	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);

	/* global setup */
	pcm->info_flags = 0;
	strcpy(pcm->name, "YMFPCI - IEC958");
	chip->pcm_spdif = pcm;

	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);

	if (rpcm)
		*rpcm = pcm;
	return 0;
}

1215
static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
L
Linus Torvalds 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
	.open =			snd_ymfpci_playback_4ch_open,
	.close =		snd_ymfpci_playback_4ch_close,
	.ioctl =		snd_pcm_lib_ioctl,
	.hw_params =		snd_ymfpci_playback_hw_params,
	.hw_free =		snd_ymfpci_playback_hw_free,
	.prepare =		snd_ymfpci_playback_prepare,
	.trigger =		snd_ymfpci_playback_trigger,
	.pointer =		snd_ymfpci_playback_pointer,
};

1226
int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
L
Linus Torvalds 已提交
1227
{
1228
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
	int err;

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

	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);

	/* global setup */
	pcm->info_flags = 0;
	strcpy(pcm->name, "YMFPCI - Rear PCM");
	chip->pcm_4ch = pcm;

	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);

	if (rpcm)
		*rpcm = pcm;
	return 0;
}

1252
static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1253 1254 1255 1256 1257 1258
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

1259 1260
static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1261
{
1262
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1263 1264 1265 1266

	spin_lock_irq(&chip->reg_lock);
	ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
	ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1267
	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
L
Linus Torvalds 已提交
1268 1269 1270 1271
	spin_unlock_irq(&chip->reg_lock);
	return 0;
}

1272 1273
static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
					 struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1274
{
1275
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
	unsigned int val;
	int change;

	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
	      (ucontrol->value.iec958.status[1] << 8);
	spin_lock_irq(&chip->reg_lock);
	change = chip->spdif_bits != val;
	chip->spdif_bits = val;
	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
	spin_unlock_irq(&chip->reg_lock);
	return change;
}

1290
static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
L
Linus Torvalds 已提交
1291 1292 1293 1294 1295 1296 1297 1298
{
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
	.info =		snd_ymfpci_spdif_default_info,
	.get =		snd_ymfpci_spdif_default_get,
	.put =		snd_ymfpci_spdif_default_put
};

1299
static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1300 1301 1302 1303 1304 1305
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

1306 1307
static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1308
{
1309
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1310 1311 1312 1313 1314 1315 1316 1317

	spin_lock_irq(&chip->reg_lock);
	ucontrol->value.iec958.status[0] = 0x3e;
	ucontrol->value.iec958.status[1] = 0xff;
	spin_unlock_irq(&chip->reg_lock);
	return 0;
}

1318
static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
L
Linus Torvalds 已提交
1319 1320 1321 1322 1323 1324 1325 1326
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
	.info =		snd_ymfpci_spdif_mask_info,
	.get =		snd_ymfpci_spdif_mask_get,
};

1327
static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1328 1329 1330 1331 1332 1333
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
	uinfo->count = 1;
	return 0;
}

1334 1335
static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1336
{
1337
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1338 1339 1340 1341

	spin_lock_irq(&chip->reg_lock);
	ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
	ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1342
	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
L
Linus Torvalds 已提交
1343 1344 1345 1346
	spin_unlock_irq(&chip->reg_lock);
	return 0;
}

1347 1348
static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1349
{
1350
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
	unsigned int val;
	int change;

	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
	      (ucontrol->value.iec958.status[1] << 8);
	spin_lock_irq(&chip->reg_lock);
	change = chip->spdif_pcm_bits != val;
	chip->spdif_pcm_bits = val;
	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
	spin_unlock_irq(&chip->reg_lock);
	return change;
}

1365
static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
L
Linus Torvalds 已提交
1366 1367 1368 1369 1370 1371 1372 1373 1374
{
	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
	.info =		snd_ymfpci_spdif_stream_info,
	.get =		snd_ymfpci_spdif_stream_get,
	.put =		snd_ymfpci_spdif_stream_put
};

1375
static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
L
Linus Torvalds 已提交
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
{
	static char *texts[3] = {"AC'97", "IEC958", "ZV Port"};

	info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	info->count = 1;
	info->value.enumerated.items = 3;
	if (info->value.enumerated.item > 2)
		info->value.enumerated.item = 2;
	strcpy(info->value.enumerated.name, texts[info->value.enumerated.item]);
	return 0;
}

1388
static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
L
Linus Torvalds 已提交
1389
{
1390
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
	u16 reg;

	spin_lock_irq(&chip->reg_lock);
	reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
	spin_unlock_irq(&chip->reg_lock);
	if (!(reg & 0x100))
		value->value.enumerated.item[0] = 0;
	else
		value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
	return 0;
}

1403
static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
L
Linus Torvalds 已提交
1404
{
1405
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
	u16 reg, old_reg;

	spin_lock_irq(&chip->reg_lock);
	old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
	if (value->value.enumerated.item[0] == 0)
		reg = old_reg & ~0x100;
	else
		reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
	spin_unlock_irq(&chip->reg_lock);
	return reg != old_reg;
}

1419
static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
L
Linus Torvalds 已提交
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
	.name =		"Direct Recording Source",
	.info =		snd_ymfpci_drec_source_info,
	.get =		snd_ymfpci_drec_source_get,
	.put =		snd_ymfpci_drec_source_put
};

/*
 *  Mixer controls
 */

1432
#define YMFPCI_SINGLE(xname, xindex, reg, shift) \
L
Linus Torvalds 已提交
1433 1434 1435
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  .info = snd_ymfpci_info_single, \
  .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1436
  .private_value = ((reg) | ((shift) << 16)) }
L
Linus Torvalds 已提交
1437

1438
#define snd_ymfpci_info_single		snd_ctl_boolean_mono_info
L
Linus Torvalds 已提交
1439

1440 1441
static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1442
{
1443
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1444 1445 1446
	int reg = kcontrol->private_value & 0xffff;
	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
	unsigned int mask = 1;
L
Linus Torvalds 已提交
1447
	
1448
	switch (reg) {
L
Linus Torvalds 已提交
1449 1450 1451 1452
	case YDSXGR_SPDIFOUTCTRL: break;
	case YDSXGR_SPDIFINCTRL: break;
	default: return -EINVAL;
	}
1453 1454
	ucontrol->value.integer.value[0] =
		(snd_ymfpci_readl(chip, reg) >> shift) & mask;
L
Linus Torvalds 已提交
1455 1456 1457
	return 0;
}

1458 1459
static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1460
{
1461
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1462 1463 1464
	int reg = kcontrol->private_value & 0xffff;
	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
 	unsigned int mask = 1;
L
Linus Torvalds 已提交
1465 1466 1467
	int change;
	unsigned int val, oval;
	
1468
	switch (reg) {
L
Linus Torvalds 已提交
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
	case YDSXGR_SPDIFOUTCTRL: break;
	case YDSXGR_SPDIFINCTRL: break;
	default: return -EINVAL;
	}
	val = (ucontrol->value.integer.value[0] & mask);
	val <<= shift;
	spin_lock_irq(&chip->reg_lock);
	oval = snd_ymfpci_readl(chip, reg);
	val = (oval & ~(mask << shift)) | val;
	change = val != oval;
	snd_ymfpci_writel(chip, reg, val);
	spin_unlock_irq(&chip->reg_lock);
	return change;
}

1484
static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
1485

L
Linus Torvalds 已提交
1486 1487
#define YMFPCI_DOUBLE(xname, xindex, reg) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1488
  .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
L
Linus Torvalds 已提交
1489 1490
  .info = snd_ymfpci_info_double, \
  .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
1491 1492
  .private_value = reg, \
  .tlv = { .p = db_scale_native } }
L
Linus Torvalds 已提交
1493

1494
static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1495 1496 1497 1498 1499
{
	unsigned int reg = kcontrol->private_value;

	if (reg < 0x80 || reg >= 0xc0)
		return -EINVAL;
A
Adrian Bunk 已提交
1500
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
L
Linus Torvalds 已提交
1501 1502
	uinfo->count = 2;
	uinfo->value.integer.min = 0;
A
Adrian Bunk 已提交
1503
	uinfo->value.integer.max = 16383;
L
Linus Torvalds 已提交
1504 1505 1506
	return 0;
}

1507
static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1508
{
1509
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1510
	unsigned int reg = kcontrol->private_value;
A
Adrian Bunk 已提交
1511
	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
L
Linus Torvalds 已提交
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
	unsigned int val;
	
	if (reg < 0x80 || reg >= 0xc0)
		return -EINVAL;
	spin_lock_irq(&chip->reg_lock);
	val = snd_ymfpci_readl(chip, reg);
	spin_unlock_irq(&chip->reg_lock);
	ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
	ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
	return 0;
}

1524
static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1525
{
1526
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1527
	unsigned int reg = kcontrol->private_value;
A
Adrian Bunk 已提交
1528
	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
L
Linus Torvalds 已提交
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	int change;
	unsigned int val1, val2, oval;
	
	if (reg < 0x80 || reg >= 0xc0)
		return -EINVAL;
	val1 = ucontrol->value.integer.value[0] & mask;
	val2 = ucontrol->value.integer.value[1] & mask;
	val1 <<= shift_left;
	val2 <<= shift_right;
	spin_lock_irq(&chip->reg_lock);
	oval = snd_ymfpci_readl(chip, reg);
	val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
	change = val1 != oval;
	snd_ymfpci_writel(chip, reg, val1);
	spin_unlock_irq(&chip->reg_lock);
	return change;
}

1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_value *ucontrol)
{
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
	unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
	unsigned int reg2 = YDSXGR_BUF441OUTVOL;
	int change;
	unsigned int value, oval;
	
	value = ucontrol->value.integer.value[0] & 0x3fff;
	value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
	spin_lock_irq(&chip->reg_lock);
	oval = snd_ymfpci_readl(chip, reg);
	change = value != oval;
	snd_ymfpci_writel(chip, reg, value);
	snd_ymfpci_writel(chip, reg2, value);
	spin_unlock_irq(&chip->reg_lock);
	return change;
}

L
Linus Torvalds 已提交
1567 1568 1569
/*
 * 4ch duplication
 */
1570
#define snd_ymfpci_info_dup4ch		snd_ctl_boolean_mono_info
L
Linus Torvalds 已提交
1571

1572
static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1573
{
1574
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1575 1576 1577 1578
	ucontrol->value.integer.value[0] = chip->mode_dup4ch;
	return 0;
}

1579
static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1580
{
1581
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1582 1583 1584 1585 1586 1587 1588 1589
	int change;
	change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
	if (change)
		chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
	return change;
}


1590
static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
{
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Wave Playback Volume",
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
	.info = snd_ymfpci_info_double,
	.get = snd_ymfpci_get_double,
	.put = snd_ymfpci_put_nativedacvol,
	.private_value = YDSXGR_NATIVEDACOUTVOL,
	.tlv = { .p = db_scale_native },
},
L
Linus Torvalds 已提交
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
YMFPCI_DOUBLE("FM Legacy Volume", 0, YDSXGR_LEGACYOUTVOL),
YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1614 1615 1616
YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
L
Linus Torvalds 已提交
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
{
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "4ch Duplication",
	.info = snd_ymfpci_info_dup4ch,
	.get = snd_ymfpci_get_dup4ch,
	.put = snd_ymfpci_put_dup4ch,
},
};


/*
 * GPIO
 */

1631
static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
L
Linus Torvalds 已提交
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
{
	u16 reg, mode;
	unsigned long flags;

	spin_lock_irqsave(&chip->reg_lock, flags);
	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
	reg &= ~(1 << (pin + 8));
	reg |= (1 << pin);
	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
	/* set the level mode for input line */
	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
	mode &= ~(3 << (pin * 2));
	snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	return (mode >> pin) & 1;
}

1651
static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
L
Linus Torvalds 已提交
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
{
	u16 reg;
	unsigned long flags;

	spin_lock_irqsave(&chip->reg_lock, flags);
	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
	reg &= ~(1 << pin);
	reg &= ~(1 << (pin + 8));
	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
	snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
	spin_unlock_irqrestore(&chip->reg_lock, flags);

	return 0;
}

1668
#define snd_ymfpci_gpio_sw_info		snd_ctl_boolean_mono_info
L
Linus Torvalds 已提交
1669

1670
static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1671
{
1672
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1673 1674 1675 1676 1677
	int pin = (int)kcontrol->private_value;
	ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
	return 0;
}

1678
static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1679
{
1680
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
	int pin = (int)kcontrol->private_value;

	if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
		snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
		ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
		return 1;
	}
	return 0;
}

1691
static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
L
Linus Torvalds 已提交
1692 1693 1694 1695 1696 1697 1698 1699
	.name = "Shared Rear/Line-In Switch",
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.info = snd_ymfpci_gpio_sw_info,
	.get = snd_ymfpci_gpio_sw_get,
	.put = snd_ymfpci_gpio_sw_put,
	.private_value = 2,
};

1700 1701 1702 1703
/*
 * PCM voice volume
 */

1704 1705
static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
				   struct snd_ctl_elem_info *uinfo)
1706 1707 1708 1709 1710 1711 1712 1713
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 0x8000;
	return 0;
}

1714 1715
static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_value *ucontrol)
1716
{
1717
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1718 1719 1720 1721 1722 1723 1724
	unsigned int subs = kcontrol->id.subdevice;

	ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
	ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
	return 0;
}

1725 1726
static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_value *ucontrol)
1727
{
1728
	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1729
	unsigned int subs = kcontrol->id.subdevice;
1730
	struct snd_pcm_substream *substream;
1731 1732 1733 1734 1735 1736
	unsigned long flags;

	if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
	    ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
		chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
		chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1737 1738 1739 1740
		if (chip->pcm_mixer[subs].left > 0x8000)
			chip->pcm_mixer[subs].left = 0x8000;
		if (chip->pcm_mixer[subs].right > 0x8000)
			chip->pcm_mixer[subs].right = 0x8000;
1741

1742
		substream = (struct snd_pcm_substream *)kcontrol->private_value;
1743 1744
		spin_lock_irqsave(&chip->voice_lock, flags);
		if (substream->runtime && substream->runtime->private_data) {
1745
			struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1746 1747
			if (!ypcm->use_441_slot)
				ypcm->update_pcm_vol = 2;
1748 1749 1750 1751 1752 1753 1754
		}
		spin_unlock_irqrestore(&chip->voice_lock, flags);
		return 1;
	}
	return 0;
}

1755
static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
1756 1757 1758 1759 1760 1761 1762 1763 1764
	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
	.name = "PCM Playback Volume",
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
		SNDRV_CTL_ELEM_ACCESS_INACTIVE,
	.info = snd_ymfpci_pcm_vol_info,
	.get = snd_ymfpci_pcm_vol_get,
	.put = snd_ymfpci_pcm_vol_put,
};

L
Linus Torvalds 已提交
1765 1766 1767 1768 1769

/*
 *  Mixer routines
 */

1770
static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
L
Linus Torvalds 已提交
1771
{
1772
	struct snd_ymfpci *chip = bus->private_data;
L
Linus Torvalds 已提交
1773 1774 1775
	chip->ac97_bus = NULL;
}

1776
static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
L
Linus Torvalds 已提交
1777
{
1778
	struct snd_ymfpci *chip = ac97->private_data;
L
Linus Torvalds 已提交
1779 1780 1781
	chip->ac97 = NULL;
}

1782
int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
L
Linus Torvalds 已提交
1783
{
1784 1785 1786
	struct snd_ac97_template ac97;
	struct snd_kcontrol *kctl;
	struct snd_pcm_substream *substream;
L
Linus Torvalds 已提交
1787 1788
	unsigned int idx;
	int err;
1789
	static struct snd_ac97_bus_ops ops = {
L
Linus Torvalds 已提交
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
		.write = snd_ymfpci_codec_write,
		.read = snd_ymfpci_codec_read,
	};

	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
		return err;
	chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
	chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */

	memset(&ac97, 0, sizeof(ac97));
	ac97.private_data = chip;
	ac97.private_free = snd_ymfpci_mixer_free_ac97;
	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
		return err;

	/* to be sure */
	snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
			     AC97_EA_VRA|AC97_EA_VRM, 0);

	for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
			return err;
	}

	/* add S/PDIF control */
	snd_assert(chip->pcm_spdif != NULL, return -EIO);
	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
		return err;
	kctl->id.device = chip->pcm_spdif->device;
	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
		return err;
	kctl->id.device = chip->pcm_spdif->device;
	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
		return err;
	kctl->id.device = chip->pcm_spdif->device;
	chip->spdif_pcm_ctl = kctl;

	/* direct recording source */
	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
	    (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
		return err;

	/*
	 * shared rear/line-in
	 */
	if (rear_switch) {
		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
			return err;
	}

1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
	/* per-voice volume */
	substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
	for (idx = 0; idx < 32; ++idx) {
		kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
		if (!kctl)
			return -ENOMEM;
		kctl->id.device = chip->pcm->device;
		kctl->id.subdevice = idx;
		kctl->private_value = (unsigned long)substream;
		if ((err = snd_ctl_add(chip->card, kctl)) < 0)
			return err;
		chip->pcm_mixer[idx].left = 0x8000;
		chip->pcm_mixer[idx].right = 0x8000;
		chip->pcm_mixer[idx].ctl = kctl;
		substream = substream->next;
	}

L
Linus Torvalds 已提交
1857 1858 1859 1860 1861 1862 1863 1864
	return 0;
}


/*
 * timer
 */

1865
static int snd_ymfpci_timer_start(struct snd_timer *timer)
L
Linus Torvalds 已提交
1866
{
1867
	struct snd_ymfpci *chip;
L
Linus Torvalds 已提交
1868 1869 1870 1871
	unsigned long flags;
	unsigned int count;

	chip = snd_timer_chip(timer);
1872
	count = (timer->sticks << 1) - 1;
L
Linus Torvalds 已提交
1873 1874 1875 1876 1877 1878 1879
	spin_lock_irqsave(&chip->reg_lock, flags);
	snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	return 0;
}

1880
static int snd_ymfpci_timer_stop(struct snd_timer *timer)
L
Linus Torvalds 已提交
1881
{
1882
	struct snd_ymfpci *chip;
L
Linus Torvalds 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891
	unsigned long flags;

	chip = snd_timer_chip(timer);
	spin_lock_irqsave(&chip->reg_lock, flags);
	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	return 0;
}

1892
static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
L
Linus Torvalds 已提交
1893 1894 1895
					       unsigned long *num, unsigned long *den)
{
	*num = 1;
1896
	*den = 48000;
L
Linus Torvalds 已提交
1897 1898 1899
	return 0;
}

1900
static struct snd_timer_hardware snd_ymfpci_timer_hw = {
L
Linus Torvalds 已提交
1901
	.flags = SNDRV_TIMER_HW_AUTO,
1902 1903
	.resolution = 20833, /* 1/fs = 20.8333...us */
	.ticks = 0x8000,
L
Linus Torvalds 已提交
1904 1905 1906 1907 1908
	.start = snd_ymfpci_timer_start,
	.stop = snd_ymfpci_timer_stop,
	.precise_resolution = snd_ymfpci_timer_precise_resolution,
};

1909
int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
L
Linus Torvalds 已提交
1910
{
1911 1912
	struct snd_timer *timer = NULL;
	struct snd_timer_id tid;
L
Linus Torvalds 已提交
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
	int err;

	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
	tid.card = chip->card->number;
	tid.device = device;
	tid.subdevice = 0;
	if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
		strcpy(timer->name, "YMFPCI timer");
		timer->private_data = chip;
		timer->hw = snd_ymfpci_timer_hw;
	}
	chip->timer = timer;
	return err;
}


/*
 *  proc interface
 */

1934 1935
static void snd_ymfpci_proc_read(struct snd_info_entry *entry, 
				 struct snd_info_buffer *buffer)
L
Linus Torvalds 已提交
1936
{
1937
	struct snd_ymfpci *chip = entry->private_data;
L
Linus Torvalds 已提交
1938 1939 1940 1941 1942 1943 1944
	int i;
	
	snd_iprintf(buffer, "YMFPCI\n\n");
	for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
		snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
}

1945
static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
L
Linus Torvalds 已提交
1946
{
1947
	struct snd_info_entry *entry;
L
Linus Torvalds 已提交
1948 1949
	
	if (! snd_card_proc_new(card, "ymfpci", &entry))
1950
		snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
L
Linus Torvalds 已提交
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
	return 0;
}

/*
 *  initialization routines
 */

static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
{
	u8 cmd;

	pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
#if 0 // force to reset
	if (cmd & 0x03) {
#endif
		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
#if 0
	}
#endif
}

1976
static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
L
Linus Torvalds 已提交
1977 1978 1979 1980
{
	snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
}

1981
static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
L
Linus Torvalds 已提交
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
{
	u32 val;
	int timeout = 1000;

	val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
	if (val)
		snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
	while (timeout-- > 0) {
		val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
		if ((val & 0x00000002) == 0)
			break;
	}
}

1996
#ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
1997

L
Linus Torvalds 已提交
1998 1999
#include "ymfpci_image.h"

2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
static struct firmware snd_ymfpci_dsp_microcode = {
	.size = YDSXG_DSPLENGTH,
	.data = (u8 *)DspInst,
};
static struct firmware snd_ymfpci_controller_microcode = {
	.size = YDSXG_CTRLLENGTH,
	.data = (u8 *)CntrlInst,
};
static struct firmware snd_ymfpci_controller_1e_microcode = {
	.size = YDSXG_CTRLLENGTH,
	.data = (u8 *)CntrlInst1E,
};
#endif

2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
#ifdef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
{
	chip->dsp_microcode = &snd_ymfpci_dsp_microcode;
	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
	    chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
	    chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
	    chip->device_id == PCI_DEVICE_ID_YAMAHA_754)
		chip->controller_microcode =
			&snd_ymfpci_controller_1e_microcode;
	else
		chip->controller_microcode =
			&snd_ymfpci_controller_microcode;
	return 0;
}

#else /* use fw_loader */

2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
#ifdef __LITTLE_ENDIAN
static inline void snd_ymfpci_convert_from_le(const struct firmware *fw) { }
#else
static void snd_ymfpci_convert_from_le(const struct firmware *fw)
{
	int i;
	u32 *data = (u32 *)fw->data;

	for (i = 0; i < fw->size / 4; ++i)
		le32_to_cpus(&data[i]);
}
#endif

static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
{
	int err, is_1e;
	const char *name;

	err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
			       &chip->pci->dev);
	if (err >= 0) {
		if (chip->dsp_microcode->size == YDSXG_DSPLENGTH)
			snd_ymfpci_convert_from_le(chip->dsp_microcode);
		else {
			snd_printk(KERN_ERR "DSP microcode has wrong size\n");
			err = -EINVAL;
		}
	}
2060
	if (err < 0)
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
		return err;
	is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
		chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
		chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
		chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
	name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
	err = request_firmware(&chip->controller_microcode, name,
			       &chip->pci->dev);
	if (err >= 0) {
		if (chip->controller_microcode->size == YDSXG_CTRLLENGTH)
			snd_ymfpci_convert_from_le(chip->controller_microcode);
		else {
			snd_printk(KERN_ERR "controller microcode"
				   " has wrong size\n");
			err = -EINVAL;
		}
	}
2078
	if (err < 0)
2079 2080 2081
		return err;
	return 0;
}
2082 2083 2084 2085 2086

MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");

2087
#endif
2088

2089
static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
L
Linus Torvalds 已提交
2090 2091 2092
{
	int i;
	u16 ctrl;
2093
	u32 *inst;
L
Linus Torvalds 已提交
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107

	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
	snd_ymfpci_disable_dsp(chip);
	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
	snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
	ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);

	/* setup DSP instruction code */
2108
	inst = (u32 *)chip->dsp_microcode->data;
L
Linus Torvalds 已提交
2109
	for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2110
		snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2), inst[i]);
L
Linus Torvalds 已提交
2111 2112

	/* setup control instruction code */
2113
	inst = (u32 *)chip->controller_microcode->data;
L
Linus Torvalds 已提交
2114 2115 2116 2117 2118 2119
	for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
		snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2), inst[i]);

	snd_ymfpci_enable_dsp(chip);
}

2120
static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
L
Linus Torvalds 已提交
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
{
	long size, playback_ctrl_size;
	int voice, bank, reg;
	u8 *ptr;
	dma_addr_t ptr_addr;

	playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
	chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
	chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
	chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
	chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
	
C
Clemens Ladisch 已提交
2133 2134 2135 2136
	size = ALIGN(playback_ctrl_size, 0x100) +
	       ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
	       ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
	       ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
L
Linus Torvalds 已提交
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
	       chip->work_size;
	/* work_ptr must be aligned to 256 bytes, but it's already
	   covered with the kernel page allocation mechanism */
	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
				size, &chip->work_ptr) < 0) 
		return -ENOMEM;
	ptr = chip->work_ptr.area;
	ptr_addr = chip->work_ptr.addr;
	memset(ptr, 0, size);	/* for sure */

	chip->bank_base_playback = ptr;
	chip->bank_base_playback_addr = ptr_addr;
	chip->ctrl_playback = (u32 *)ptr;
	chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
C
Clemens Ladisch 已提交
2151 2152
	ptr += ALIGN(playback_ctrl_size, 0x100);
	ptr_addr += ALIGN(playback_ctrl_size, 0x100);
L
Linus Torvalds 已提交
2153 2154
	for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
		chip->voices[voice].number = voice;
2155
		chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
L
Linus Torvalds 已提交
2156 2157
		chip->voices[voice].bank_addr = ptr_addr;
		for (bank = 0; bank < 2; bank++) {
2158
			chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
L
Linus Torvalds 已提交
2159 2160 2161 2162
			ptr += chip->bank_size_playback;
			ptr_addr += chip->bank_size_playback;
		}
	}
C
Clemens Ladisch 已提交
2163 2164
	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
	ptr_addr = ALIGN(ptr_addr, 0x100);
L
Linus Torvalds 已提交
2165 2166 2167 2168
	chip->bank_base_capture = ptr;
	chip->bank_base_capture_addr = ptr_addr;
	for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
		for (bank = 0; bank < 2; bank++) {
2169
			chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
L
Linus Torvalds 已提交
2170 2171 2172
			ptr += chip->bank_size_capture;
			ptr_addr += chip->bank_size_capture;
		}
C
Clemens Ladisch 已提交
2173 2174
	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
	ptr_addr = ALIGN(ptr_addr, 0x100);
L
Linus Torvalds 已提交
2175 2176 2177 2178
	chip->bank_base_effect = ptr;
	chip->bank_base_effect_addr = ptr_addr;
	for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
		for (bank = 0; bank < 2; bank++) {
2179
			chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
L
Linus Torvalds 已提交
2180 2181 2182
			ptr += chip->bank_size_effect;
			ptr_addr += chip->bank_size_effect;
		}
C
Clemens Ladisch 已提交
2183 2184
	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
	ptr_addr = ALIGN(ptr_addr, 0x100);
L
Linus Torvalds 已提交
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
	chip->work_base = ptr;
	chip->work_base_addr = ptr_addr;
	
	snd_assert(ptr + chip->work_size == chip->work_ptr.area + chip->work_ptr.bytes, );

	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
	snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
	snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);

	/* S/PDIF output initialization */
	chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);

	/* S/PDIF input initialization */
	snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);

	/* digital mixer setup */
	for (reg = 0x80; reg < 0xc0; reg += 4)
		snd_ymfpci_writel(chip, reg, 0);
	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
	snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
	snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
	snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
	snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
	snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
	
	return 0;
}

2218
static int snd_ymfpci_free(struct snd_ymfpci *chip)
L
Linus Torvalds 已提交
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
{
	u16 ctrl;

	snd_assert(chip != NULL, return -EINVAL);

	if (chip->res_reg_area) {	/* don't touch busy hardware */
		snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
		snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
		snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
		snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
		snd_ymfpci_disable_dsp(chip);
		snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
		snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
		snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
		snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
		snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
		ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
		snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
	}

	snd_ymfpci_ac3_done(chip);

	/* Set PCI device to D3 state */
#if 0
	/* FIXME: temporarily disabled, otherwise we cannot fire up
	 * the chip again unless reboot.  ACPI bug?
	 */
	pci_set_power_state(chip->pci, 3);
#endif

#ifdef CONFIG_PM
	vfree(chip->saved_regs);
#endif
2252 2253
	release_and_free_resource(chip->mpu_res);
	release_and_free_resource(chip->fm_res);
L
Linus Torvalds 已提交
2254 2255 2256 2257 2258 2259 2260
	snd_ymfpci_free_gameport(chip);
	if (chip->reg_area_virt)
		iounmap(chip->reg_area_virt);
	if (chip->work_ptr.area)
		snd_dma_free_pages(&chip->work_ptr);
	
	if (chip->irq >= 0)
2261
		free_irq(chip->irq, chip);
2262
	release_and_free_resource(chip->res_reg_area);
L
Linus Torvalds 已提交
2263 2264 2265 2266

	pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
	
	pci_disable_device(chip->pci);
2267 2268 2269
#ifndef CONFIG_SND_YMFPCI_FIRMWARE_IN_KERNEL
	release_firmware(chip->dsp_microcode);
	release_firmware(chip->controller_microcode);
2270
#endif
L
Linus Torvalds 已提交
2271 2272 2273 2274
	kfree(chip);
	return 0;
}

2275
static int snd_ymfpci_dev_free(struct snd_device *device)
L
Linus Torvalds 已提交
2276
{
2277
	struct snd_ymfpci *chip = device->device_data;
L
Linus Torvalds 已提交
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290
	return snd_ymfpci_free(chip);
}

#ifdef CONFIG_PM
static int saved_regs_index[] = {
	/* spdif */
	YDSXGR_SPDIFOUTCTRL,
	YDSXGR_SPDIFOUTSTATUS,
	YDSXGR_SPDIFINCTRL,
	/* volumes */
	YDSXGR_PRIADCLOOPVOL,
	YDSXGR_NATIVEDACINVOL,
	YDSXGR_NATIVEDACOUTVOL,
2291
	YDSXGR_BUF441OUTVOL,
L
Linus Torvalds 已提交
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
	YDSXGR_NATIVEADCINVOL,
	YDSXGR_SPDIFLOOPVOL,
	YDSXGR_SPDIFOUTVOL,
	YDSXGR_ZVOUTVOL,
	YDSXGR_LEGACYOUTVOL,
	/* address bases */
	YDSXGR_PLAYCTRLBASE,
	YDSXGR_RECCTRLBASE,
	YDSXGR_EFFCTRLBASE,
	YDSXGR_WORKBASE,
	/* capture set up */
	YDSXGR_MAPOFREC,
	YDSXGR_RECFORMAT,
	YDSXGR_RECSLOTSR,
	YDSXGR_ADCFORMAT,
	YDSXGR_ADCSLOTSR,
};
#define YDSXGR_NUM_SAVED_REGS	ARRAY_SIZE(saved_regs_index)

T
Takashi Iwai 已提交
2311
int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
L
Linus Torvalds 已提交
2312
{
T
Takashi Iwai 已提交
2313 2314
	struct snd_card *card = pci_get_drvdata(pci);
	struct snd_ymfpci *chip = card->private_data;
L
Linus Torvalds 已提交
2315 2316
	unsigned int i;
	
T
Takashi Iwai 已提交
2317
	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
L
Linus Torvalds 已提交
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
	snd_pcm_suspend_all(chip->pcm);
	snd_pcm_suspend_all(chip->pcm2);
	snd_pcm_suspend_all(chip->pcm_spdif);
	snd_pcm_suspend_all(chip->pcm_4ch);
	snd_ac97_suspend(chip->ac97);
	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
		chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
	chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
	snd_ymfpci_disable_dsp(chip);
T
Takashi Iwai 已提交
2328 2329
	pci_disable_device(pci);
	pci_save_state(pci);
2330
	pci_set_power_state(pci, pci_choose_state(pci, state));
L
Linus Torvalds 已提交
2331 2332 2333
	return 0;
}

T
Takashi Iwai 已提交
2334
int snd_ymfpci_resume(struct pci_dev *pci)
L
Linus Torvalds 已提交
2335
{
T
Takashi Iwai 已提交
2336 2337
	struct snd_card *card = pci_get_drvdata(pci);
	struct snd_ymfpci *chip = card->private_data;
L
Linus Torvalds 已提交
2338 2339
	unsigned int i;

2340
	pci_set_power_state(pci, PCI_D0);
T
Takashi Iwai 已提交
2341
	pci_restore_state(pci);
2342 2343 2344 2345 2346 2347
	if (pci_enable_device(pci) < 0) {
		printk(KERN_ERR "ymfpci: pci_enable_device failed, "
		       "disabling device\n");
		snd_card_disconnect(card);
		return -EIO;
	}
T
Takashi Iwai 已提交
2348 2349
	pci_set_master(pci);
	snd_ymfpci_aclink_reset(pci);
L
Linus Torvalds 已提交
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
	snd_ymfpci_codec_ready(chip, 0);
	snd_ymfpci_download_image(chip);
	udelay(100);

	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
		snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);

	snd_ac97_resume(chip->ac97);

	/* start hw again */
	if (chip->start_count > 0) {
		spin_lock_irq(&chip->reg_lock);
		snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
		spin_unlock_irq(&chip->reg_lock);
	}
T
Takashi Iwai 已提交
2366
	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
L
Linus Torvalds 已提交
2367 2368 2369 2370
	return 0;
}
#endif /* CONFIG_PM */

2371
int __devinit snd_ymfpci_create(struct snd_card *card,
L
Linus Torvalds 已提交
2372 2373
				struct pci_dev * pci,
				unsigned short old_legacy_ctrl,
2374
				struct snd_ymfpci ** rchip)
L
Linus Torvalds 已提交
2375
{
2376
	struct snd_ymfpci *chip;
L
Linus Torvalds 已提交
2377
	int err;
2378
	static struct snd_device_ops ops = {
L
Linus Torvalds 已提交
2379 2380 2381 2382 2383 2384 2385 2386 2387
		.dev_free =	snd_ymfpci_dev_free,
	};
	
	*rchip = NULL;

	/* enable PCI device */
	if ((err = pci_enable_device(pci)) < 0)
		return err;

2388
	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
L
Linus Torvalds 已提交
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401
	if (chip == NULL) {
		pci_disable_device(pci);
		return -ENOMEM;
	}
	chip->old_legacy_ctrl = old_legacy_ctrl;
	spin_lock_init(&chip->reg_lock);
	spin_lock_init(&chip->voice_lock);
	init_waitqueue_head(&chip->interrupt_sleep);
	atomic_set(&chip->interrupt_sleep_count, 0);
	chip->card = card;
	chip->pci = pci;
	chip->irq = -1;
	chip->device_id = pci->device;
2402
	chip->rev = pci->revision;
L
Linus Torvalds 已提交
2403 2404 2405
	chip->reg_area_phys = pci_resource_start(pci, 0);
	chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
	pci_set_master(pci);
2406
	chip->src441_used = -1;
L
Linus Torvalds 已提交
2407 2408

	if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
2409
		snd_printk(KERN_ERR "unable to grab memory region 0x%lx-0x%lx\n", chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
L
Linus Torvalds 已提交
2410 2411 2412
		snd_ymfpci_free(chip);
		return -EBUSY;
	}
2413 2414
	if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
			"YMFPCI", chip)) {
2415
		snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
L
Linus Torvalds 已提交
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426
		snd_ymfpci_free(chip);
		return -EBUSY;
	}
	chip->irq = pci->irq;

	snd_ymfpci_aclink_reset(pci);
	if (snd_ymfpci_codec_ready(chip, 0) < 0) {
		snd_ymfpci_free(chip);
		return -EIO;
	}

2427 2428 2429 2430 2431 2432
	err = snd_ymfpci_request_firmware(chip);
	if (err < 0) {
		snd_printk(KERN_ERR "firmware request failed: %d\n", err);
		snd_ymfpci_free(chip);
		return err;
	}
L
Linus Torvalds 已提交
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
	snd_ymfpci_download_image(chip);

	udelay(100); /* seems we need a delay after downloading image.. */

	if (snd_ymfpci_memalloc(chip) < 0) {
		snd_ymfpci_free(chip);
		return -EIO;
	}

	if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
		snd_ymfpci_free(chip);
		return err;
	}

#ifdef CONFIG_PM
	chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
	if (chip->saved_regs == NULL) {
		snd_ymfpci_free(chip);
		return -ENOMEM;
	}
#endif

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

	snd_ymfpci_proc_init(card, chip);

	snd_card_set_dev(card, &pci->dev);

	*rchip = chip;
	return 0;
}