xref: /linux/sound/pci/ymfpci/ymfpci_main.c (revision 9a6b55ac)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4  *  Routines for control of YMF724/740/744/754 chips
5  */
6 
7 #include <linux/delay.h>
8 #include <linux/firmware.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/pci.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/module.h>
16 #include <linux/io.h>
17 
18 #include <sound/core.h>
19 #include <sound/control.h>
20 #include <sound/info.h>
21 #include <sound/tlv.h>
22 #include "ymfpci.h"
23 #include <sound/asoundef.h>
24 #include <sound/mpu401.h>
25 
26 #include <asm/byteorder.h>
27 
28 /*
29  *  common I/O routines
30  */
31 
32 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
33 
34 static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
35 {
36 	return readb(chip->reg_area_virt + offset);
37 }
38 
39 static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
40 {
41 	writeb(val, chip->reg_area_virt + offset);
42 }
43 
44 static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
45 {
46 	return readw(chip->reg_area_virt + offset);
47 }
48 
49 static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
50 {
51 	writew(val, chip->reg_area_virt + offset);
52 }
53 
54 static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
55 {
56 	return readl(chip->reg_area_virt + offset);
57 }
58 
59 static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
60 {
61 	writel(val, chip->reg_area_virt + offset);
62 }
63 
64 static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
65 {
66 	unsigned long end_time;
67 	u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
68 
69 	end_time = jiffies + msecs_to_jiffies(750);
70 	do {
71 		if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
72 			return 0;
73 		schedule_timeout_uninterruptible(1);
74 	} while (time_before(jiffies, end_time));
75 	dev_err(chip->card->dev,
76 		"codec_ready: codec %i is not ready [0x%x]\n",
77 		secondary, snd_ymfpci_readw(chip, reg));
78 	return -EBUSY;
79 }
80 
81 static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
82 {
83 	struct snd_ymfpci *chip = ac97->private_data;
84 	u32 cmd;
85 
86 	snd_ymfpci_codec_ready(chip, 0);
87 	cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
88 	snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
89 }
90 
91 static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
92 {
93 	struct snd_ymfpci *chip = ac97->private_data;
94 
95 	if (snd_ymfpci_codec_ready(chip, 0))
96 		return ~0;
97 	snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
98 	if (snd_ymfpci_codec_ready(chip, 0))
99 		return ~0;
100 	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
101 		int i;
102 		for (i = 0; i < 600; i++)
103 			snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
104 	}
105 	return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
106 }
107 
108 /*
109  *  Misc routines
110  */
111 
112 static u32 snd_ymfpci_calc_delta(u32 rate)
113 {
114 	switch (rate) {
115 	case 8000:	return 0x02aaab00;
116 	case 11025:	return 0x03accd00;
117 	case 16000:	return 0x05555500;
118 	case 22050:	return 0x07599a00;
119 	case 32000:	return 0x0aaaab00;
120 	case 44100:	return 0x0eb33300;
121 	default:	return ((rate << 16) / 375) << 5;
122 	}
123 }
124 
125 static u32 def_rate[8] = {
126 	100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
127 };
128 
129 static u32 snd_ymfpci_calc_lpfK(u32 rate)
130 {
131 	u32 i;
132 	static u32 val[8] = {
133 		0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
134 		0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
135 	};
136 
137 	if (rate == 44100)
138 		return 0x40000000;	/* FIXME: What's the right value? */
139 	for (i = 0; i < 8; i++)
140 		if (rate <= def_rate[i])
141 			return val[i];
142 	return val[0];
143 }
144 
145 static u32 snd_ymfpci_calc_lpfQ(u32 rate)
146 {
147 	u32 i;
148 	static u32 val[8] = {
149 		0x35280000, 0x34A70000, 0x32020000, 0x31770000,
150 		0x31390000, 0x31C90000, 0x33D00000, 0x40000000
151 	};
152 
153 	if (rate == 44100)
154 		return 0x370A0000;
155 	for (i = 0; i < 8; i++)
156 		if (rate <= def_rate[i])
157 			return val[i];
158 	return val[0];
159 }
160 
161 /*
162  *  Hardware start management
163  */
164 
165 static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
166 {
167 	unsigned long flags;
168 
169 	spin_lock_irqsave(&chip->reg_lock, flags);
170 	if (chip->start_count++ > 0)
171 		goto __end;
172 	snd_ymfpci_writel(chip, YDSXGR_MODE,
173 			  snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
174 	chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
175       __end:
176       	spin_unlock_irqrestore(&chip->reg_lock, flags);
177 }
178 
179 static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
180 {
181 	unsigned long flags;
182 	long timeout = 1000;
183 
184 	spin_lock_irqsave(&chip->reg_lock, flags);
185 	if (--chip->start_count > 0)
186 		goto __end;
187 	snd_ymfpci_writel(chip, YDSXGR_MODE,
188 			  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
189 	while (timeout-- > 0) {
190 		if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
191 			break;
192 	}
193 	if (atomic_read(&chip->interrupt_sleep_count)) {
194 		atomic_set(&chip->interrupt_sleep_count, 0);
195 		wake_up(&chip->interrupt_sleep);
196 	}
197       __end:
198       	spin_unlock_irqrestore(&chip->reg_lock, flags);
199 }
200 
201 /*
202  *  Playback voice management
203  */
204 
205 static int voice_alloc(struct snd_ymfpci *chip,
206 		       enum snd_ymfpci_voice_type type, int pair,
207 		       struct snd_ymfpci_voice **rvoice)
208 {
209 	struct snd_ymfpci_voice *voice, *voice2;
210 	int idx;
211 
212 	*rvoice = NULL;
213 	for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
214 		voice = &chip->voices[idx];
215 		voice2 = pair ? &chip->voices[idx+1] : NULL;
216 		if (voice->use || (voice2 && voice2->use))
217 			continue;
218 		voice->use = 1;
219 		if (voice2)
220 			voice2->use = 1;
221 		switch (type) {
222 		case YMFPCI_PCM:
223 			voice->pcm = 1;
224 			if (voice2)
225 				voice2->pcm = 1;
226 			break;
227 		case YMFPCI_SYNTH:
228 			voice->synth = 1;
229 			break;
230 		case YMFPCI_MIDI:
231 			voice->midi = 1;
232 			break;
233 		}
234 		snd_ymfpci_hw_start(chip);
235 		if (voice2)
236 			snd_ymfpci_hw_start(chip);
237 		*rvoice = voice;
238 		return 0;
239 	}
240 	return -ENOMEM;
241 }
242 
243 static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
244 				  enum snd_ymfpci_voice_type type, int pair,
245 				  struct snd_ymfpci_voice **rvoice)
246 {
247 	unsigned long flags;
248 	int result;
249 
250 	if (snd_BUG_ON(!rvoice))
251 		return -EINVAL;
252 	if (snd_BUG_ON(pair && type != YMFPCI_PCM))
253 		return -EINVAL;
254 
255 	spin_lock_irqsave(&chip->voice_lock, flags);
256 	for (;;) {
257 		result = voice_alloc(chip, type, pair, rvoice);
258 		if (result == 0 || type != YMFPCI_PCM)
259 			break;
260 		/* TODO: synth/midi voice deallocation */
261 		break;
262 	}
263 	spin_unlock_irqrestore(&chip->voice_lock, flags);
264 	return result;
265 }
266 
267 static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
268 {
269 	unsigned long flags;
270 
271 	if (snd_BUG_ON(!pvoice))
272 		return -EINVAL;
273 	snd_ymfpci_hw_stop(chip);
274 	spin_lock_irqsave(&chip->voice_lock, flags);
275 	if (pvoice->number == chip->src441_used) {
276 		chip->src441_used = -1;
277 		pvoice->ypcm->use_441_slot = 0;
278 	}
279 	pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
280 	pvoice->ypcm = NULL;
281 	pvoice->interrupt = NULL;
282 	spin_unlock_irqrestore(&chip->voice_lock, flags);
283 	return 0;
284 }
285 
286 /*
287  *  PCM part
288  */
289 
290 static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
291 {
292 	struct snd_ymfpci_pcm *ypcm;
293 	u32 pos, delta;
294 
295 	if ((ypcm = voice->ypcm) == NULL)
296 		return;
297 	if (ypcm->substream == NULL)
298 		return;
299 	spin_lock(&chip->reg_lock);
300 	if (ypcm->running) {
301 		pos = le32_to_cpu(voice->bank[chip->active_bank].start);
302 		if (pos < ypcm->last_pos)
303 			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
304 		else
305 			delta = pos - ypcm->last_pos;
306 		ypcm->period_pos += delta;
307 		ypcm->last_pos = pos;
308 		if (ypcm->period_pos >= ypcm->period_size) {
309 			/*
310 			dev_dbg(chip->card->dev,
311 			       "done - active_bank = 0x%x, start = 0x%x\n",
312 			       chip->active_bank,
313 			       voice->bank[chip->active_bank].start);
314 			*/
315 			ypcm->period_pos %= ypcm->period_size;
316 			spin_unlock(&chip->reg_lock);
317 			snd_pcm_period_elapsed(ypcm->substream);
318 			spin_lock(&chip->reg_lock);
319 		}
320 
321 		if (unlikely(ypcm->update_pcm_vol)) {
322 			unsigned int subs = ypcm->substream->number;
323 			unsigned int next_bank = 1 - chip->active_bank;
324 			struct snd_ymfpci_playback_bank *bank;
325 			__le32 volume;
326 
327 			bank = &voice->bank[next_bank];
328 			volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
329 			bank->left_gain_end = volume;
330 			if (ypcm->output_rear)
331 				bank->eff2_gain_end = volume;
332 			if (ypcm->voices[1])
333 				bank = &ypcm->voices[1]->bank[next_bank];
334 			volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
335 			bank->right_gain_end = volume;
336 			if (ypcm->output_rear)
337 				bank->eff3_gain_end = volume;
338 			ypcm->update_pcm_vol--;
339 		}
340 	}
341 	spin_unlock(&chip->reg_lock);
342 }
343 
344 static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
345 {
346 	struct snd_pcm_runtime *runtime = substream->runtime;
347 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
348 	struct snd_ymfpci *chip = ypcm->chip;
349 	u32 pos, delta;
350 
351 	spin_lock(&chip->reg_lock);
352 	if (ypcm->running) {
353 		pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
354 		if (pos < ypcm->last_pos)
355 			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
356 		else
357 			delta = pos - ypcm->last_pos;
358 		ypcm->period_pos += delta;
359 		ypcm->last_pos = pos;
360 		if (ypcm->period_pos >= ypcm->period_size) {
361 			ypcm->period_pos %= ypcm->period_size;
362 			/*
363 			dev_dbg(chip->card->dev,
364 			       "done - active_bank = 0x%x, start = 0x%x\n",
365 			       chip->active_bank,
366 			       voice->bank[chip->active_bank].start);
367 			*/
368 			spin_unlock(&chip->reg_lock);
369 			snd_pcm_period_elapsed(substream);
370 			spin_lock(&chip->reg_lock);
371 		}
372 	}
373 	spin_unlock(&chip->reg_lock);
374 }
375 
376 static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
377 				       int cmd)
378 {
379 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
380 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
381 	struct snd_kcontrol *kctl = NULL;
382 	int result = 0;
383 
384 	spin_lock(&chip->reg_lock);
385 	if (ypcm->voices[0] == NULL) {
386 		result = -EINVAL;
387 		goto __unlock;
388 	}
389 	switch (cmd) {
390 	case SNDRV_PCM_TRIGGER_START:
391 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
392 	case SNDRV_PCM_TRIGGER_RESUME:
393 		chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
394 		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
395 			chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
396 		ypcm->running = 1;
397 		break;
398 	case SNDRV_PCM_TRIGGER_STOP:
399 		if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
400 			kctl = chip->pcm_mixer[substream->number].ctl;
401 			kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
402 		}
403 		/* fall through */
404 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
405 	case SNDRV_PCM_TRIGGER_SUSPEND:
406 		chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
407 		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
408 			chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
409 		ypcm->running = 0;
410 		break;
411 	default:
412 		result = -EINVAL;
413 		break;
414 	}
415       __unlock:
416 	spin_unlock(&chip->reg_lock);
417 	if (kctl)
418 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
419 	return result;
420 }
421 static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
422 				      int cmd)
423 {
424 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
425 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
426 	int result = 0;
427 	u32 tmp;
428 
429 	spin_lock(&chip->reg_lock);
430 	switch (cmd) {
431 	case SNDRV_PCM_TRIGGER_START:
432 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
433 	case SNDRV_PCM_TRIGGER_RESUME:
434 		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
435 		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
436 		ypcm->running = 1;
437 		break;
438 	case SNDRV_PCM_TRIGGER_STOP:
439 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
440 	case SNDRV_PCM_TRIGGER_SUSPEND:
441 		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
442 		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
443 		ypcm->running = 0;
444 		break;
445 	default:
446 		result = -EINVAL;
447 		break;
448 	}
449 	spin_unlock(&chip->reg_lock);
450 	return result;
451 }
452 
453 static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
454 {
455 	int err;
456 
457 	if (ypcm->voices[1] != NULL && voices < 2) {
458 		snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
459 		ypcm->voices[1] = NULL;
460 	}
461 	if (voices == 1 && ypcm->voices[0] != NULL)
462 		return 0;		/* already allocated */
463 	if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
464 		return 0;		/* already allocated */
465 	if (voices > 1) {
466 		if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
467 			snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
468 			ypcm->voices[0] = NULL;
469 		}
470 	}
471 	err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
472 	if (err < 0)
473 		return err;
474 	ypcm->voices[0]->ypcm = ypcm;
475 	ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
476 	if (voices > 1) {
477 		ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
478 		ypcm->voices[1]->ypcm = ypcm;
479 	}
480 	return 0;
481 }
482 
483 static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
484 				      struct snd_pcm_runtime *runtime,
485 				      int has_pcm_volume)
486 {
487 	struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
488 	u32 format;
489 	u32 delta = snd_ymfpci_calc_delta(runtime->rate);
490 	u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
491 	u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
492 	struct snd_ymfpci_playback_bank *bank;
493 	unsigned int nbank;
494 	__le32 vol_left, vol_right;
495 	u8 use_left, use_right;
496 	unsigned long flags;
497 
498 	if (snd_BUG_ON(!voice))
499 		return;
500 	if (runtime->channels == 1) {
501 		use_left = 1;
502 		use_right = 1;
503 	} else {
504 		use_left = (voiceidx & 1) == 0;
505 		use_right = !use_left;
506 	}
507 	if (has_pcm_volume) {
508 		vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
509 				       [ypcm->substream->number].left << 15);
510 		vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
511 					[ypcm->substream->number].right << 15);
512 	} else {
513 		vol_left = cpu_to_le32(0x40000000);
514 		vol_right = cpu_to_le32(0x40000000);
515 	}
516 	spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
517 	format = runtime->channels == 2 ? 0x00010000 : 0;
518 	if (snd_pcm_format_width(runtime->format) == 8)
519 		format |= 0x80000000;
520 	else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
521 		 runtime->rate == 44100 && runtime->channels == 2 &&
522 		 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
523 				   ypcm->chip->src441_used == voice->number)) {
524 		ypcm->chip->src441_used = voice->number;
525 		ypcm->use_441_slot = 1;
526 		format |= 0x10000000;
527 	}
528 	if (ypcm->chip->src441_used == voice->number &&
529 	    (format & 0x10000000) == 0) {
530 		ypcm->chip->src441_used = -1;
531 		ypcm->use_441_slot = 0;
532 	}
533 	if (runtime->channels == 2 && (voiceidx & 1) != 0)
534 		format |= 1;
535 	spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
536 	for (nbank = 0; nbank < 2; nbank++) {
537 		bank = &voice->bank[nbank];
538 		memset(bank, 0, sizeof(*bank));
539 		bank->format = cpu_to_le32(format);
540 		bank->base = cpu_to_le32(runtime->dma_addr);
541 		bank->loop_end = cpu_to_le32(ypcm->buffer_size);
542 		bank->lpfQ = cpu_to_le32(lpfQ);
543 		bank->delta =
544 		bank->delta_end = cpu_to_le32(delta);
545 		bank->lpfK =
546 		bank->lpfK_end = cpu_to_le32(lpfK);
547 		bank->eg_gain =
548 		bank->eg_gain_end = cpu_to_le32(0x40000000);
549 
550 		if (ypcm->output_front) {
551 			if (use_left) {
552 				bank->left_gain =
553 				bank->left_gain_end = vol_left;
554 			}
555 			if (use_right) {
556 				bank->right_gain =
557 				bank->right_gain_end = vol_right;
558 			}
559 		}
560 		if (ypcm->output_rear) {
561 		        if (!ypcm->swap_rear) {
562         			if (use_left) {
563         				bank->eff2_gain =
564         				bank->eff2_gain_end = vol_left;
565         			}
566         			if (use_right) {
567         				bank->eff3_gain =
568         				bank->eff3_gain_end = vol_right;
569         			}
570 		        } else {
571         			/* The SPDIF out channels seem to be swapped, so we have
572         			 * to swap them here, too.  The rear analog out channels
573         			 * will be wrong, but otherwise AC3 would not work.
574         			 */
575         			if (use_left) {
576         				bank->eff3_gain =
577         				bank->eff3_gain_end = vol_left;
578         			}
579         			if (use_right) {
580         				bank->eff2_gain =
581         				bank->eff2_gain_end = vol_right;
582         			}
583         		}
584                 }
585 	}
586 }
587 
588 static int snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
589 {
590 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
591 				4096, &chip->ac3_tmp_base) < 0)
592 		return -ENOMEM;
593 
594 	chip->bank_effect[3][0]->base =
595 	chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
596 	chip->bank_effect[3][0]->loop_end =
597 	chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
598 	chip->bank_effect[4][0]->base =
599 	chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
600 	chip->bank_effect[4][0]->loop_end =
601 	chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
602 
603 	spin_lock_irq(&chip->reg_lock);
604 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
605 			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
606 	spin_unlock_irq(&chip->reg_lock);
607 	return 0;
608 }
609 
610 static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
611 {
612 	spin_lock_irq(&chip->reg_lock);
613 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
614 			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
615 	spin_unlock_irq(&chip->reg_lock);
616 	// snd_ymfpci_irq_wait(chip);
617 	if (chip->ac3_tmp_base.area) {
618 		snd_dma_free_pages(&chip->ac3_tmp_base);
619 		chip->ac3_tmp_base.area = NULL;
620 	}
621 	return 0;
622 }
623 
624 static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
625 					 struct snd_pcm_hw_params *hw_params)
626 {
627 	struct snd_pcm_runtime *runtime = substream->runtime;
628 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
629 	int err;
630 
631 	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
632 		return err;
633 	if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
634 		return err;
635 	return 0;
636 }
637 
638 static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
639 {
640 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
641 	struct snd_pcm_runtime *runtime = substream->runtime;
642 	struct snd_ymfpci_pcm *ypcm;
643 
644 	if (runtime->private_data == NULL)
645 		return 0;
646 	ypcm = runtime->private_data;
647 
648 	/* wait, until the PCI operations are not finished */
649 	snd_ymfpci_irq_wait(chip);
650 	snd_pcm_lib_free_pages(substream);
651 	if (ypcm->voices[1]) {
652 		snd_ymfpci_voice_free(chip, ypcm->voices[1]);
653 		ypcm->voices[1] = NULL;
654 	}
655 	if (ypcm->voices[0]) {
656 		snd_ymfpci_voice_free(chip, ypcm->voices[0]);
657 		ypcm->voices[0] = NULL;
658 	}
659 	return 0;
660 }
661 
662 static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
663 {
664 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
665 	struct snd_pcm_runtime *runtime = substream->runtime;
666 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
667 	struct snd_kcontrol *kctl;
668 	unsigned int nvoice;
669 
670 	ypcm->period_size = runtime->period_size;
671 	ypcm->buffer_size = runtime->buffer_size;
672 	ypcm->period_pos = 0;
673 	ypcm->last_pos = 0;
674 	for (nvoice = 0; nvoice < runtime->channels; nvoice++)
675 		snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
676 					  substream->pcm == chip->pcm);
677 
678 	if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
679 		kctl = chip->pcm_mixer[substream->number].ctl;
680 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
681 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
682 	}
683 	return 0;
684 }
685 
686 static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
687 					struct snd_pcm_hw_params *hw_params)
688 {
689 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
690 }
691 
692 static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
693 {
694 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
695 
696 	/* wait, until the PCI operations are not finished */
697 	snd_ymfpci_irq_wait(chip);
698 	return snd_pcm_lib_free_pages(substream);
699 }
700 
701 static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
702 {
703 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
704 	struct snd_pcm_runtime *runtime = substream->runtime;
705 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
706 	struct snd_ymfpci_capture_bank * bank;
707 	int nbank;
708 	u32 rate, format;
709 
710 	ypcm->period_size = runtime->period_size;
711 	ypcm->buffer_size = runtime->buffer_size;
712 	ypcm->period_pos = 0;
713 	ypcm->last_pos = 0;
714 	ypcm->shift = 0;
715 	rate = ((48000 * 4096) / runtime->rate) - 1;
716 	format = 0;
717 	if (runtime->channels == 2) {
718 		format |= 2;
719 		ypcm->shift++;
720 	}
721 	if (snd_pcm_format_width(runtime->format) == 8)
722 		format |= 1;
723 	else
724 		ypcm->shift++;
725 	switch (ypcm->capture_bank_number) {
726 	case 0:
727 		snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
728 		snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
729 		break;
730 	case 1:
731 		snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
732 		snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
733 		break;
734 	}
735 	for (nbank = 0; nbank < 2; nbank++) {
736 		bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
737 		bank->base = cpu_to_le32(runtime->dma_addr);
738 		bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
739 		bank->start = 0;
740 		bank->num_of_loops = 0;
741 	}
742 	return 0;
743 }
744 
745 static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
746 {
747 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
748 	struct snd_pcm_runtime *runtime = substream->runtime;
749 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
750 	struct snd_ymfpci_voice *voice = ypcm->voices[0];
751 
752 	if (!(ypcm->running && voice))
753 		return 0;
754 	return le32_to_cpu(voice->bank[chip->active_bank].start);
755 }
756 
757 static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
758 {
759 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
760 	struct snd_pcm_runtime *runtime = substream->runtime;
761 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
762 
763 	if (!ypcm->running)
764 		return 0;
765 	return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
766 }
767 
768 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
769 {
770 	wait_queue_entry_t wait;
771 	int loops = 4;
772 
773 	while (loops-- > 0) {
774 		if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
775 		 	continue;
776 		init_waitqueue_entry(&wait, current);
777 		add_wait_queue(&chip->interrupt_sleep, &wait);
778 		atomic_inc(&chip->interrupt_sleep_count);
779 		schedule_timeout_uninterruptible(msecs_to_jiffies(50));
780 		remove_wait_queue(&chip->interrupt_sleep, &wait);
781 	}
782 }
783 
784 static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
785 {
786 	struct snd_ymfpci *chip = dev_id;
787 	u32 status, nvoice, mode;
788 	struct snd_ymfpci_voice *voice;
789 
790 	status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
791 	if (status & 0x80000000) {
792 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
793 		spin_lock(&chip->voice_lock);
794 		for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
795 			voice = &chip->voices[nvoice];
796 			if (voice->interrupt)
797 				voice->interrupt(chip, voice);
798 		}
799 		for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
800 			if (chip->capture_substream[nvoice])
801 				snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
802 		}
803 #if 0
804 		for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
805 			if (chip->effect_substream[nvoice])
806 				snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
807 		}
808 #endif
809 		spin_unlock(&chip->voice_lock);
810 		spin_lock(&chip->reg_lock);
811 		snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
812 		mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
813 		snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
814 		spin_unlock(&chip->reg_lock);
815 
816 		if (atomic_read(&chip->interrupt_sleep_count)) {
817 			atomic_set(&chip->interrupt_sleep_count, 0);
818 			wake_up(&chip->interrupt_sleep);
819 		}
820 	}
821 
822 	status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
823 	if (status & 1) {
824 		if (chip->timer)
825 			snd_timer_interrupt(chip->timer, chip->timer_ticks);
826 	}
827 	snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
828 
829 	if (chip->rawmidi)
830 		snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
831 	return IRQ_HANDLED;
832 }
833 
834 static const struct snd_pcm_hardware snd_ymfpci_playback =
835 {
836 	.info =			(SNDRV_PCM_INFO_MMAP |
837 				 SNDRV_PCM_INFO_MMAP_VALID |
838 				 SNDRV_PCM_INFO_INTERLEAVED |
839 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
840 				 SNDRV_PCM_INFO_PAUSE |
841 				 SNDRV_PCM_INFO_RESUME),
842 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
843 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
844 	.rate_min =		8000,
845 	.rate_max =		48000,
846 	.channels_min =		1,
847 	.channels_max =		2,
848 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
849 	.period_bytes_min =	64,
850 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
851 	.periods_min =		3,
852 	.periods_max =		1024,
853 	.fifo_size =		0,
854 };
855 
856 static const struct snd_pcm_hardware snd_ymfpci_capture =
857 {
858 	.info =			(SNDRV_PCM_INFO_MMAP |
859 				 SNDRV_PCM_INFO_MMAP_VALID |
860 				 SNDRV_PCM_INFO_INTERLEAVED |
861 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
862 				 SNDRV_PCM_INFO_PAUSE |
863 				 SNDRV_PCM_INFO_RESUME),
864 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
865 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
866 	.rate_min =		8000,
867 	.rate_max =		48000,
868 	.channels_min =		1,
869 	.channels_max =		2,
870 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
871 	.period_bytes_min =	64,
872 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
873 	.periods_min =		3,
874 	.periods_max =		1024,
875 	.fifo_size =		0,
876 };
877 
878 static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
879 {
880 	kfree(runtime->private_data);
881 }
882 
883 static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
884 {
885 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
886 	struct snd_pcm_runtime *runtime = substream->runtime;
887 	struct snd_ymfpci_pcm *ypcm;
888 	int err;
889 
890 	runtime->hw = snd_ymfpci_playback;
891 	/* FIXME? True value is 256/48 = 5.33333 ms */
892 	err = snd_pcm_hw_constraint_minmax(runtime,
893 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
894 					   5334, UINT_MAX);
895 	if (err < 0)
896 		return err;
897 	err = snd_pcm_hw_rule_noresample(runtime, 48000);
898 	if (err < 0)
899 		return err;
900 
901 	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
902 	if (ypcm == NULL)
903 		return -ENOMEM;
904 	ypcm->chip = chip;
905 	ypcm->type = PLAYBACK_VOICE;
906 	ypcm->substream = substream;
907 	runtime->private_data = ypcm;
908 	runtime->private_free = snd_ymfpci_pcm_free_substream;
909 	return 0;
910 }
911 
912 /* call with spinlock held */
913 static void ymfpci_open_extension(struct snd_ymfpci *chip)
914 {
915 	if (! chip->rear_opened) {
916 		if (! chip->spdif_opened) /* set AC3 */
917 			snd_ymfpci_writel(chip, YDSXGR_MODE,
918 					  snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
919 		/* enable second codec (4CHEN) */
920 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
921 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
922 	}
923 }
924 
925 /* call with spinlock held */
926 static void ymfpci_close_extension(struct snd_ymfpci *chip)
927 {
928 	if (! chip->rear_opened) {
929 		if (! chip->spdif_opened)
930 			snd_ymfpci_writel(chip, YDSXGR_MODE,
931 					  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
932 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
933 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
934 	}
935 }
936 
937 static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
938 {
939 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
940 	struct snd_pcm_runtime *runtime = substream->runtime;
941 	struct snd_ymfpci_pcm *ypcm;
942 	int err;
943 
944 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
945 		return err;
946 	ypcm = runtime->private_data;
947 	ypcm->output_front = 1;
948 	ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
949 	ypcm->swap_rear = 0;
950 	spin_lock_irq(&chip->reg_lock);
951 	if (ypcm->output_rear) {
952 		ymfpci_open_extension(chip);
953 		chip->rear_opened++;
954 	}
955 	spin_unlock_irq(&chip->reg_lock);
956 	return 0;
957 }
958 
959 static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
960 {
961 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
962 	struct snd_pcm_runtime *runtime = substream->runtime;
963 	struct snd_ymfpci_pcm *ypcm;
964 	int err;
965 
966 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
967 		return err;
968 	ypcm = runtime->private_data;
969 	ypcm->output_front = 0;
970 	ypcm->output_rear = 1;
971 	ypcm->swap_rear = 1;
972 	spin_lock_irq(&chip->reg_lock);
973 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
974 			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
975 	ymfpci_open_extension(chip);
976 	chip->spdif_pcm_bits = chip->spdif_bits;
977 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
978 	chip->spdif_opened++;
979 	spin_unlock_irq(&chip->reg_lock);
980 
981 	chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
982 	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
983 		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
984 	return 0;
985 }
986 
987 static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
988 {
989 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
990 	struct snd_pcm_runtime *runtime = substream->runtime;
991 	struct snd_ymfpci_pcm *ypcm;
992 	int err;
993 
994 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
995 		return err;
996 	ypcm = runtime->private_data;
997 	ypcm->output_front = 0;
998 	ypcm->output_rear = 1;
999 	ypcm->swap_rear = 0;
1000 	spin_lock_irq(&chip->reg_lock);
1001 	ymfpci_open_extension(chip);
1002 	chip->rear_opened++;
1003 	spin_unlock_irq(&chip->reg_lock);
1004 	return 0;
1005 }
1006 
1007 static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
1008 				   u32 capture_bank_number)
1009 {
1010 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1011 	struct snd_pcm_runtime *runtime = substream->runtime;
1012 	struct snd_ymfpci_pcm *ypcm;
1013 	int err;
1014 
1015 	runtime->hw = snd_ymfpci_capture;
1016 	/* FIXME? True value is 256/48 = 5.33333 ms */
1017 	err = snd_pcm_hw_constraint_minmax(runtime,
1018 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1019 					   5334, UINT_MAX);
1020 	if (err < 0)
1021 		return err;
1022 	err = snd_pcm_hw_rule_noresample(runtime, 48000);
1023 	if (err < 0)
1024 		return err;
1025 
1026 	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
1027 	if (ypcm == NULL)
1028 		return -ENOMEM;
1029 	ypcm->chip = chip;
1030 	ypcm->type = capture_bank_number + CAPTURE_REC;
1031 	ypcm->substream = substream;
1032 	ypcm->capture_bank_number = capture_bank_number;
1033 	chip->capture_substream[capture_bank_number] = substream;
1034 	runtime->private_data = ypcm;
1035 	runtime->private_free = snd_ymfpci_pcm_free_substream;
1036 	snd_ymfpci_hw_start(chip);
1037 	return 0;
1038 }
1039 
1040 static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
1041 {
1042 	return snd_ymfpci_capture_open(substream, 0);
1043 }
1044 
1045 static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
1046 {
1047 	return snd_ymfpci_capture_open(substream, 1);
1048 }
1049 
1050 static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
1051 {
1052 	return 0;
1053 }
1054 
1055 static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
1056 {
1057 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1058 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1059 
1060 	spin_lock_irq(&chip->reg_lock);
1061 	if (ypcm->output_rear && chip->rear_opened > 0) {
1062 		chip->rear_opened--;
1063 		ymfpci_close_extension(chip);
1064 	}
1065 	spin_unlock_irq(&chip->reg_lock);
1066 	return snd_ymfpci_playback_close_1(substream);
1067 }
1068 
1069 static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
1070 {
1071 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1072 
1073 	spin_lock_irq(&chip->reg_lock);
1074 	chip->spdif_opened = 0;
1075 	ymfpci_close_extension(chip);
1076 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1077 			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1078 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1079 	spin_unlock_irq(&chip->reg_lock);
1080 	chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1081 	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1082 		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1083 	return snd_ymfpci_playback_close_1(substream);
1084 }
1085 
1086 static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
1087 {
1088 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1089 
1090 	spin_lock_irq(&chip->reg_lock);
1091 	if (chip->rear_opened > 0) {
1092 		chip->rear_opened--;
1093 		ymfpci_close_extension(chip);
1094 	}
1095 	spin_unlock_irq(&chip->reg_lock);
1096 	return snd_ymfpci_playback_close_1(substream);
1097 }
1098 
1099 static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
1100 {
1101 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1102 	struct snd_pcm_runtime *runtime = substream->runtime;
1103 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
1104 
1105 	if (ypcm != NULL) {
1106 		chip->capture_substream[ypcm->capture_bank_number] = NULL;
1107 		snd_ymfpci_hw_stop(chip);
1108 	}
1109 	return 0;
1110 }
1111 
1112 static const struct snd_pcm_ops snd_ymfpci_playback_ops = {
1113 	.open =			snd_ymfpci_playback_open,
1114 	.close =		snd_ymfpci_playback_close,
1115 	.ioctl =		snd_pcm_lib_ioctl,
1116 	.hw_params =		snd_ymfpci_playback_hw_params,
1117 	.hw_free =		snd_ymfpci_playback_hw_free,
1118 	.prepare =		snd_ymfpci_playback_prepare,
1119 	.trigger =		snd_ymfpci_playback_trigger,
1120 	.pointer =		snd_ymfpci_playback_pointer,
1121 };
1122 
1123 static const struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
1124 	.open =			snd_ymfpci_capture_rec_open,
1125 	.close =		snd_ymfpci_capture_close,
1126 	.ioctl =		snd_pcm_lib_ioctl,
1127 	.hw_params =		snd_ymfpci_capture_hw_params,
1128 	.hw_free =		snd_ymfpci_capture_hw_free,
1129 	.prepare =		snd_ymfpci_capture_prepare,
1130 	.trigger =		snd_ymfpci_capture_trigger,
1131 	.pointer =		snd_ymfpci_capture_pointer,
1132 };
1133 
1134 int snd_ymfpci_pcm(struct snd_ymfpci *chip, int device)
1135 {
1136 	struct snd_pcm *pcm;
1137 	int err;
1138 
1139 	if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1140 		return err;
1141 	pcm->private_data = chip;
1142 
1143 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1144 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1145 
1146 	/* global setup */
1147 	pcm->info_flags = 0;
1148 	strcpy(pcm->name, "YMFPCI");
1149 	chip->pcm = pcm;
1150 
1151 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1152 					      &chip->pci->dev,
1153 					      64*1024, 256*1024);
1154 
1155 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1156 				     snd_pcm_std_chmaps, 2, 0, NULL);
1157 }
1158 
1159 static const struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
1160 	.open =			snd_ymfpci_capture_ac97_open,
1161 	.close =		snd_ymfpci_capture_close,
1162 	.ioctl =		snd_pcm_lib_ioctl,
1163 	.hw_params =		snd_ymfpci_capture_hw_params,
1164 	.hw_free =		snd_ymfpci_capture_hw_free,
1165 	.prepare =		snd_ymfpci_capture_prepare,
1166 	.trigger =		snd_ymfpci_capture_trigger,
1167 	.pointer =		snd_ymfpci_capture_pointer,
1168 };
1169 
1170 int snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device)
1171 {
1172 	struct snd_pcm *pcm;
1173 	int err;
1174 
1175 	if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1176 		return err;
1177 	pcm->private_data = chip;
1178 
1179 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1180 
1181 	/* global setup */
1182 	pcm->info_flags = 0;
1183 	sprintf(pcm->name, "YMFPCI - %s",
1184 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1185 	chip->pcm2 = pcm;
1186 
1187 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1188 					      &chip->pci->dev,
1189 					      64*1024, 256*1024);
1190 
1191 	return 0;
1192 }
1193 
1194 static const struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
1195 	.open =			snd_ymfpci_playback_spdif_open,
1196 	.close =		snd_ymfpci_playback_spdif_close,
1197 	.ioctl =		snd_pcm_lib_ioctl,
1198 	.hw_params =		snd_ymfpci_playback_hw_params,
1199 	.hw_free =		snd_ymfpci_playback_hw_free,
1200 	.prepare =		snd_ymfpci_playback_prepare,
1201 	.trigger =		snd_ymfpci_playback_trigger,
1202 	.pointer =		snd_ymfpci_playback_pointer,
1203 };
1204 
1205 int snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device)
1206 {
1207 	struct snd_pcm *pcm;
1208 	int err;
1209 
1210 	if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1211 		return err;
1212 	pcm->private_data = chip;
1213 
1214 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1215 
1216 	/* global setup */
1217 	pcm->info_flags = 0;
1218 	strcpy(pcm->name, "YMFPCI - IEC958");
1219 	chip->pcm_spdif = pcm;
1220 
1221 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1222 					      &chip->pci->dev,
1223 					      64*1024, 256*1024);
1224 
1225 	return 0;
1226 }
1227 
1228 static const struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
1229 	.open =			snd_ymfpci_playback_4ch_open,
1230 	.close =		snd_ymfpci_playback_4ch_close,
1231 	.ioctl =		snd_pcm_lib_ioctl,
1232 	.hw_params =		snd_ymfpci_playback_hw_params,
1233 	.hw_free =		snd_ymfpci_playback_hw_free,
1234 	.prepare =		snd_ymfpci_playback_prepare,
1235 	.trigger =		snd_ymfpci_playback_trigger,
1236 	.pointer =		snd_ymfpci_playback_pointer,
1237 };
1238 
1239 static const struct snd_pcm_chmap_elem surround_map[] = {
1240 	{ .channels = 1,
1241 	  .map = { SNDRV_CHMAP_MONO } },
1242 	{ .channels = 2,
1243 	  .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
1244 	{ }
1245 };
1246 
1247 int snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device)
1248 {
1249 	struct snd_pcm *pcm;
1250 	int err;
1251 
1252 	if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1253 		return err;
1254 	pcm->private_data = chip;
1255 
1256 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1257 
1258 	/* global setup */
1259 	pcm->info_flags = 0;
1260 	strcpy(pcm->name, "YMFPCI - Rear PCM");
1261 	chip->pcm_4ch = pcm;
1262 
1263 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1264 					      &chip->pci->dev,
1265 					      64*1024, 256*1024);
1266 
1267 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1268 				     surround_map, 2, 0, NULL);
1269 }
1270 
1271 static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1272 {
1273 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1274 	uinfo->count = 1;
1275 	return 0;
1276 }
1277 
1278 static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1279 					struct snd_ctl_elem_value *ucontrol)
1280 {
1281 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1282 
1283 	spin_lock_irq(&chip->reg_lock);
1284 	ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1285 	ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1286 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1287 	spin_unlock_irq(&chip->reg_lock);
1288 	return 0;
1289 }
1290 
1291 static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1292 					 struct snd_ctl_elem_value *ucontrol)
1293 {
1294 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1295 	unsigned int val;
1296 	int change;
1297 
1298 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1299 	      (ucontrol->value.iec958.status[1] << 8);
1300 	spin_lock_irq(&chip->reg_lock);
1301 	change = chip->spdif_bits != val;
1302 	chip->spdif_bits = val;
1303 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1304 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1305 	spin_unlock_irq(&chip->reg_lock);
1306 	return change;
1307 }
1308 
1309 static const struct snd_kcontrol_new snd_ymfpci_spdif_default =
1310 {
1311 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1312 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1313 	.info =		snd_ymfpci_spdif_default_info,
1314 	.get =		snd_ymfpci_spdif_default_get,
1315 	.put =		snd_ymfpci_spdif_default_put
1316 };
1317 
1318 static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1319 {
1320 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1321 	uinfo->count = 1;
1322 	return 0;
1323 }
1324 
1325 static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1326 				      struct snd_ctl_elem_value *ucontrol)
1327 {
1328 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1329 
1330 	spin_lock_irq(&chip->reg_lock);
1331 	ucontrol->value.iec958.status[0] = 0x3e;
1332 	ucontrol->value.iec958.status[1] = 0xff;
1333 	spin_unlock_irq(&chip->reg_lock);
1334 	return 0;
1335 }
1336 
1337 static const struct snd_kcontrol_new snd_ymfpci_spdif_mask =
1338 {
1339 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1340 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1341 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1342 	.info =		snd_ymfpci_spdif_mask_info,
1343 	.get =		snd_ymfpci_spdif_mask_get,
1344 };
1345 
1346 static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1347 {
1348 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1349 	uinfo->count = 1;
1350 	return 0;
1351 }
1352 
1353 static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1354 					struct snd_ctl_elem_value *ucontrol)
1355 {
1356 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1357 
1358 	spin_lock_irq(&chip->reg_lock);
1359 	ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1360 	ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1361 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1362 	spin_unlock_irq(&chip->reg_lock);
1363 	return 0;
1364 }
1365 
1366 static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1367 					struct snd_ctl_elem_value *ucontrol)
1368 {
1369 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1370 	unsigned int val;
1371 	int change;
1372 
1373 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1374 	      (ucontrol->value.iec958.status[1] << 8);
1375 	spin_lock_irq(&chip->reg_lock);
1376 	change = chip->spdif_pcm_bits != val;
1377 	chip->spdif_pcm_bits = val;
1378 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1379 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1380 	spin_unlock_irq(&chip->reg_lock);
1381 	return change;
1382 }
1383 
1384 static const struct snd_kcontrol_new snd_ymfpci_spdif_stream =
1385 {
1386 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1387 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1388 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1389 	.info =		snd_ymfpci_spdif_stream_info,
1390 	.get =		snd_ymfpci_spdif_stream_get,
1391 	.put =		snd_ymfpci_spdif_stream_put
1392 };
1393 
1394 static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
1395 {
1396 	static const char *const texts[3] = {"AC'97", "IEC958", "ZV Port"};
1397 
1398 	return snd_ctl_enum_info(info, 1, 3, texts);
1399 }
1400 
1401 static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1402 {
1403 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1404 	u16 reg;
1405 
1406 	spin_lock_irq(&chip->reg_lock);
1407 	reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1408 	spin_unlock_irq(&chip->reg_lock);
1409 	if (!(reg & 0x100))
1410 		value->value.enumerated.item[0] = 0;
1411 	else
1412 		value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1413 	return 0;
1414 }
1415 
1416 static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1417 {
1418 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1419 	u16 reg, old_reg;
1420 
1421 	spin_lock_irq(&chip->reg_lock);
1422 	old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1423 	if (value->value.enumerated.item[0] == 0)
1424 		reg = old_reg & ~0x100;
1425 	else
1426 		reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1427 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1428 	spin_unlock_irq(&chip->reg_lock);
1429 	return reg != old_reg;
1430 }
1431 
1432 static const struct snd_kcontrol_new snd_ymfpci_drec_source = {
1433 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
1434 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
1435 	.name =		"Direct Recording Source",
1436 	.info =		snd_ymfpci_drec_source_info,
1437 	.get =		snd_ymfpci_drec_source_get,
1438 	.put =		snd_ymfpci_drec_source_put
1439 };
1440 
1441 /*
1442  *  Mixer controls
1443  */
1444 
1445 #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
1446 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1447   .info = snd_ymfpci_info_single, \
1448   .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1449   .private_value = ((reg) | ((shift) << 16)) }
1450 
1451 #define snd_ymfpci_info_single		snd_ctl_boolean_mono_info
1452 
1453 static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1454 				 struct snd_ctl_elem_value *ucontrol)
1455 {
1456 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1457 	int reg = kcontrol->private_value & 0xffff;
1458 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1459 	unsigned int mask = 1;
1460 
1461 	switch (reg) {
1462 	case YDSXGR_SPDIFOUTCTRL: break;
1463 	case YDSXGR_SPDIFINCTRL: break;
1464 	default: return -EINVAL;
1465 	}
1466 	ucontrol->value.integer.value[0] =
1467 		(snd_ymfpci_readl(chip, reg) >> shift) & mask;
1468 	return 0;
1469 }
1470 
1471 static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1472 				 struct snd_ctl_elem_value *ucontrol)
1473 {
1474 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1475 	int reg = kcontrol->private_value & 0xffff;
1476 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1477  	unsigned int mask = 1;
1478 	int change;
1479 	unsigned int val, oval;
1480 
1481 	switch (reg) {
1482 	case YDSXGR_SPDIFOUTCTRL: break;
1483 	case YDSXGR_SPDIFINCTRL: break;
1484 	default: return -EINVAL;
1485 	}
1486 	val = (ucontrol->value.integer.value[0] & mask);
1487 	val <<= shift;
1488 	spin_lock_irq(&chip->reg_lock);
1489 	oval = snd_ymfpci_readl(chip, reg);
1490 	val = (oval & ~(mask << shift)) | val;
1491 	change = val != oval;
1492 	snd_ymfpci_writel(chip, reg, val);
1493 	spin_unlock_irq(&chip->reg_lock);
1494 	return change;
1495 }
1496 
1497 static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
1498 
1499 #define YMFPCI_DOUBLE(xname, xindex, reg) \
1500 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1501   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
1502   .info = snd_ymfpci_info_double, \
1503   .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
1504   .private_value = reg, \
1505   .tlv = { .p = db_scale_native } }
1506 
1507 static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1508 {
1509 	unsigned int reg = kcontrol->private_value;
1510 
1511 	if (reg < 0x80 || reg >= 0xc0)
1512 		return -EINVAL;
1513 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1514 	uinfo->count = 2;
1515 	uinfo->value.integer.min = 0;
1516 	uinfo->value.integer.max = 16383;
1517 	return 0;
1518 }
1519 
1520 static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1521 {
1522 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1523 	unsigned int reg = kcontrol->private_value;
1524 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1525 	unsigned int val;
1526 
1527 	if (reg < 0x80 || reg >= 0xc0)
1528 		return -EINVAL;
1529 	spin_lock_irq(&chip->reg_lock);
1530 	val = snd_ymfpci_readl(chip, reg);
1531 	spin_unlock_irq(&chip->reg_lock);
1532 	ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1533 	ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
1534 	return 0;
1535 }
1536 
1537 static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1538 {
1539 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1540 	unsigned int reg = kcontrol->private_value;
1541 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1542 	int change;
1543 	unsigned int val1, val2, oval;
1544 
1545 	if (reg < 0x80 || reg >= 0xc0)
1546 		return -EINVAL;
1547 	val1 = ucontrol->value.integer.value[0] & mask;
1548 	val2 = ucontrol->value.integer.value[1] & mask;
1549 	val1 <<= shift_left;
1550 	val2 <<= shift_right;
1551 	spin_lock_irq(&chip->reg_lock);
1552 	oval = snd_ymfpci_readl(chip, reg);
1553 	val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1554 	change = val1 != oval;
1555 	snd_ymfpci_writel(chip, reg, val1);
1556 	spin_unlock_irq(&chip->reg_lock);
1557 	return change;
1558 }
1559 
1560 static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1561 				       struct snd_ctl_elem_value *ucontrol)
1562 {
1563 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1564 	unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1565 	unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1566 	int change;
1567 	unsigned int value, oval;
1568 
1569 	value = ucontrol->value.integer.value[0] & 0x3fff;
1570 	value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1571 	spin_lock_irq(&chip->reg_lock);
1572 	oval = snd_ymfpci_readl(chip, reg);
1573 	change = value != oval;
1574 	snd_ymfpci_writel(chip, reg, value);
1575 	snd_ymfpci_writel(chip, reg2, value);
1576 	spin_unlock_irq(&chip->reg_lock);
1577 	return change;
1578 }
1579 
1580 /*
1581  * 4ch duplication
1582  */
1583 #define snd_ymfpci_info_dup4ch		snd_ctl_boolean_mono_info
1584 
1585 static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1586 {
1587 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1588 	ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1589 	return 0;
1590 }
1591 
1592 static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1593 {
1594 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1595 	int change;
1596 	change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1597 	if (change)
1598 		chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1599 	return change;
1600 }
1601 
1602 static const struct snd_kcontrol_new snd_ymfpci_dup4ch = {
1603 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1604 	.name = "4ch Duplication",
1605 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1606 	.info = snd_ymfpci_info_dup4ch,
1607 	.get = snd_ymfpci_get_dup4ch,
1608 	.put = snd_ymfpci_put_dup4ch,
1609 };
1610 
1611 static struct snd_kcontrol_new snd_ymfpci_controls[] = {
1612 {
1613 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1614 	.name = "Wave Playback Volume",
1615 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1616 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1617 	.info = snd_ymfpci_info_double,
1618 	.get = snd_ymfpci_get_double,
1619 	.put = snd_ymfpci_put_nativedacvol,
1620 	.private_value = YDSXGR_NATIVEDACOUTVOL,
1621 	.tlv = { .p = db_scale_native },
1622 },
1623 YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1624 YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1625 YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1626 YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1627 YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1628 YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1629 YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1630 YMFPCI_DOUBLE("FM Legacy Playback Volume", 0, YDSXGR_LEGACYOUTVOL),
1631 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1632 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1633 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1634 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1635 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1636 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1637 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
1638 };
1639 
1640 
1641 /*
1642  * GPIO
1643  */
1644 
1645 static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
1646 {
1647 	u16 reg, mode;
1648 	unsigned long flags;
1649 
1650 	spin_lock_irqsave(&chip->reg_lock, flags);
1651 	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1652 	reg &= ~(1 << (pin + 8));
1653 	reg |= (1 << pin);
1654 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1655 	/* set the level mode for input line */
1656 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1657 	mode &= ~(3 << (pin * 2));
1658 	snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1659 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1660 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1661 	spin_unlock_irqrestore(&chip->reg_lock, flags);
1662 	return (mode >> pin) & 1;
1663 }
1664 
1665 static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
1666 {
1667 	u16 reg;
1668 	unsigned long flags;
1669 
1670 	spin_lock_irqsave(&chip->reg_lock, flags);
1671 	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1672 	reg &= ~(1 << pin);
1673 	reg &= ~(1 << (pin + 8));
1674 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1675 	snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1676 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1677 	spin_unlock_irqrestore(&chip->reg_lock, flags);
1678 
1679 	return 0;
1680 }
1681 
1682 #define snd_ymfpci_gpio_sw_info		snd_ctl_boolean_mono_info
1683 
1684 static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1685 {
1686 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1687 	int pin = (int)kcontrol->private_value;
1688 	ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1689 	return 0;
1690 }
1691 
1692 static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1693 {
1694 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1695 	int pin = (int)kcontrol->private_value;
1696 
1697 	if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1698 		snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1699 		ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1700 		return 1;
1701 	}
1702 	return 0;
1703 }
1704 
1705 static const struct snd_kcontrol_new snd_ymfpci_rear_shared = {
1706 	.name = "Shared Rear/Line-In Switch",
1707 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1708 	.info = snd_ymfpci_gpio_sw_info,
1709 	.get = snd_ymfpci_gpio_sw_get,
1710 	.put = snd_ymfpci_gpio_sw_put,
1711 	.private_value = 2,
1712 };
1713 
1714 /*
1715  * PCM voice volume
1716  */
1717 
1718 static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1719 				   struct snd_ctl_elem_info *uinfo)
1720 {
1721 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1722 	uinfo->count = 2;
1723 	uinfo->value.integer.min = 0;
1724 	uinfo->value.integer.max = 0x8000;
1725 	return 0;
1726 }
1727 
1728 static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1729 				  struct snd_ctl_elem_value *ucontrol)
1730 {
1731 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1732 	unsigned int subs = kcontrol->id.subdevice;
1733 
1734 	ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1735 	ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1736 	return 0;
1737 }
1738 
1739 static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1740 				  struct snd_ctl_elem_value *ucontrol)
1741 {
1742 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1743 	unsigned int subs = kcontrol->id.subdevice;
1744 	struct snd_pcm_substream *substream;
1745 	unsigned long flags;
1746 
1747 	if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1748 	    ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1749 		chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1750 		chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1751 		if (chip->pcm_mixer[subs].left > 0x8000)
1752 			chip->pcm_mixer[subs].left = 0x8000;
1753 		if (chip->pcm_mixer[subs].right > 0x8000)
1754 			chip->pcm_mixer[subs].right = 0x8000;
1755 
1756 		substream = (struct snd_pcm_substream *)kcontrol->private_value;
1757 		spin_lock_irqsave(&chip->voice_lock, flags);
1758 		if (substream->runtime && substream->runtime->private_data) {
1759 			struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1760 			if (!ypcm->use_441_slot)
1761 				ypcm->update_pcm_vol = 2;
1762 		}
1763 		spin_unlock_irqrestore(&chip->voice_lock, flags);
1764 		return 1;
1765 	}
1766 	return 0;
1767 }
1768 
1769 static const struct snd_kcontrol_new snd_ymfpci_pcm_volume = {
1770 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1771 	.name = "PCM Playback Volume",
1772 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1773 		SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1774 	.info = snd_ymfpci_pcm_vol_info,
1775 	.get = snd_ymfpci_pcm_vol_get,
1776 	.put = snd_ymfpci_pcm_vol_put,
1777 };
1778 
1779 
1780 /*
1781  *  Mixer routines
1782  */
1783 
1784 static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1785 {
1786 	struct snd_ymfpci *chip = bus->private_data;
1787 	chip->ac97_bus = NULL;
1788 }
1789 
1790 static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
1791 {
1792 	struct snd_ymfpci *chip = ac97->private_data;
1793 	chip->ac97 = NULL;
1794 }
1795 
1796 int snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
1797 {
1798 	struct snd_ac97_template ac97;
1799 	struct snd_kcontrol *kctl;
1800 	struct snd_pcm_substream *substream;
1801 	unsigned int idx;
1802 	int err;
1803 	static struct snd_ac97_bus_ops ops = {
1804 		.write = snd_ymfpci_codec_write,
1805 		.read = snd_ymfpci_codec_read,
1806 	};
1807 
1808 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1809 		return err;
1810 	chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1811 	chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1812 
1813 	memset(&ac97, 0, sizeof(ac97));
1814 	ac97.private_data = chip;
1815 	ac97.private_free = snd_ymfpci_mixer_free_ac97;
1816 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1817 		return err;
1818 
1819 	/* to be sure */
1820 	snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1821 			     AC97_EA_VRA|AC97_EA_VRM, 0);
1822 
1823 	for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1824 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1825 			return err;
1826 	}
1827 	if (chip->ac97->ext_id & AC97_EI_SDAC) {
1828 		kctl = snd_ctl_new1(&snd_ymfpci_dup4ch, chip);
1829 		err = snd_ctl_add(chip->card, kctl);
1830 		if (err < 0)
1831 			return err;
1832 	}
1833 
1834 	/* add S/PDIF control */
1835 	if (snd_BUG_ON(!chip->pcm_spdif))
1836 		return -ENXIO;
1837 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1838 		return err;
1839 	kctl->id.device = chip->pcm_spdif->device;
1840 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1841 		return err;
1842 	kctl->id.device = chip->pcm_spdif->device;
1843 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1844 		return err;
1845 	kctl->id.device = chip->pcm_spdif->device;
1846 	chip->spdif_pcm_ctl = kctl;
1847 
1848 	/* direct recording source */
1849 	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1850 	    (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1851 		return err;
1852 
1853 	/*
1854 	 * shared rear/line-in
1855 	 */
1856 	if (rear_switch) {
1857 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1858 			return err;
1859 	}
1860 
1861 	/* per-voice volume */
1862 	substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1863 	for (idx = 0; idx < 32; ++idx) {
1864 		kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1865 		if (!kctl)
1866 			return -ENOMEM;
1867 		kctl->id.device = chip->pcm->device;
1868 		kctl->id.subdevice = idx;
1869 		kctl->private_value = (unsigned long)substream;
1870 		if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1871 			return err;
1872 		chip->pcm_mixer[idx].left = 0x8000;
1873 		chip->pcm_mixer[idx].right = 0x8000;
1874 		chip->pcm_mixer[idx].ctl = kctl;
1875 		substream = substream->next;
1876 	}
1877 
1878 	return 0;
1879 }
1880 
1881 
1882 /*
1883  * timer
1884  */
1885 
1886 static int snd_ymfpci_timer_start(struct snd_timer *timer)
1887 {
1888 	struct snd_ymfpci *chip;
1889 	unsigned long flags;
1890 	unsigned int count;
1891 
1892 	chip = snd_timer_chip(timer);
1893 	spin_lock_irqsave(&chip->reg_lock, flags);
1894 	if (timer->sticks > 1) {
1895 		chip->timer_ticks = timer->sticks;
1896 		count = timer->sticks - 1;
1897 	} else {
1898 		/*
1899 		 * Divisor 1 is not allowed; fake it by using divisor 2 and
1900 		 * counting two ticks for each interrupt.
1901 		 */
1902 		chip->timer_ticks = 2;
1903 		count = 2 - 1;
1904 	}
1905 	snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1906 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1907 	spin_unlock_irqrestore(&chip->reg_lock, flags);
1908 	return 0;
1909 }
1910 
1911 static int snd_ymfpci_timer_stop(struct snd_timer *timer)
1912 {
1913 	struct snd_ymfpci *chip;
1914 	unsigned long flags;
1915 
1916 	chip = snd_timer_chip(timer);
1917 	spin_lock_irqsave(&chip->reg_lock, flags);
1918 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1919 	spin_unlock_irqrestore(&chip->reg_lock, flags);
1920 	return 0;
1921 }
1922 
1923 static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
1924 					       unsigned long *num, unsigned long *den)
1925 {
1926 	*num = 1;
1927 	*den = 96000;
1928 	return 0;
1929 }
1930 
1931 static struct snd_timer_hardware snd_ymfpci_timer_hw = {
1932 	.flags = SNDRV_TIMER_HW_AUTO,
1933 	.resolution = 10417, /* 1 / 96 kHz = 10.41666...us */
1934 	.ticks = 0x10000,
1935 	.start = snd_ymfpci_timer_start,
1936 	.stop = snd_ymfpci_timer_stop,
1937 	.precise_resolution = snd_ymfpci_timer_precise_resolution,
1938 };
1939 
1940 int snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
1941 {
1942 	struct snd_timer *timer = NULL;
1943 	struct snd_timer_id tid;
1944 	int err;
1945 
1946 	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1947 	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1948 	tid.card = chip->card->number;
1949 	tid.device = device;
1950 	tid.subdevice = 0;
1951 	if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1952 		strcpy(timer->name, "YMFPCI timer");
1953 		timer->private_data = chip;
1954 		timer->hw = snd_ymfpci_timer_hw;
1955 	}
1956 	chip->timer = timer;
1957 	return err;
1958 }
1959 
1960 
1961 /*
1962  *  proc interface
1963  */
1964 
1965 static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1966 				 struct snd_info_buffer *buffer)
1967 {
1968 	struct snd_ymfpci *chip = entry->private_data;
1969 	int i;
1970 
1971 	snd_iprintf(buffer, "YMFPCI\n\n");
1972 	for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1973 		snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1974 }
1975 
1976 static int snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
1977 {
1978 	return snd_card_ro_proc_new(card, "ymfpci", chip, snd_ymfpci_proc_read);
1979 }
1980 
1981 /*
1982  *  initialization routines
1983  */
1984 
1985 static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1986 {
1987 	u8 cmd;
1988 
1989 	pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1990 #if 0 // force to reset
1991 	if (cmd & 0x03) {
1992 #endif
1993 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1994 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1995 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1996 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1997 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1998 #if 0
1999 	}
2000 #endif
2001 }
2002 
2003 static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
2004 {
2005 	snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
2006 }
2007 
2008 static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
2009 {
2010 	u32 val;
2011 	int timeout = 1000;
2012 
2013 	val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
2014 	if (val)
2015 		snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
2016 	while (timeout-- > 0) {
2017 		val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
2018 		if ((val & 0x00000002) == 0)
2019 			break;
2020 	}
2021 }
2022 
2023 static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2024 {
2025 	int err, is_1e;
2026 	const char *name;
2027 
2028 	err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2029 			       &chip->pci->dev);
2030 	if (err >= 0) {
2031 		if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
2032 			dev_err(chip->card->dev,
2033 				"DSP microcode has wrong size\n");
2034 			err = -EINVAL;
2035 		}
2036 	}
2037 	if (err < 0)
2038 		return err;
2039 	is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2040 		chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2041 		chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2042 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2043 	name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2044 	err = request_firmware(&chip->controller_microcode, name,
2045 			       &chip->pci->dev);
2046 	if (err >= 0) {
2047 		if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
2048 			dev_err(chip->card->dev,
2049 				"controller microcode has wrong size\n");
2050 			err = -EINVAL;
2051 		}
2052 	}
2053 	if (err < 0)
2054 		return err;
2055 	return 0;
2056 }
2057 
2058 MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
2059 MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
2060 MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
2061 
2062 static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
2063 {
2064 	int i;
2065 	u16 ctrl;
2066 	const __le32 *inst;
2067 
2068 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2069 	snd_ymfpci_disable_dsp(chip);
2070 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2071 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2072 	snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2073 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2074 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2075 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2076 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2077 	ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2078 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2079 
2080 	/* setup DSP instruction code */
2081 	inst = (const __le32 *)chip->dsp_microcode->data;
2082 	for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2083 		snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2084 				  le32_to_cpu(inst[i]));
2085 
2086 	/* setup control instruction code */
2087 	inst = (const __le32 *)chip->controller_microcode->data;
2088 	for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2089 		snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2090 				  le32_to_cpu(inst[i]));
2091 
2092 	snd_ymfpci_enable_dsp(chip);
2093 }
2094 
2095 static int snd_ymfpci_memalloc(struct snd_ymfpci *chip)
2096 {
2097 	long size, playback_ctrl_size;
2098 	int voice, bank, reg;
2099 	u8 *ptr;
2100 	dma_addr_t ptr_addr;
2101 
2102 	playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2103 	chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2104 	chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2105 	chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2106 	chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2107 
2108 	size = ALIGN(playback_ctrl_size, 0x100) +
2109 	       ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2110 	       ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2111 	       ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
2112 	       chip->work_size;
2113 	/* work_ptr must be aligned to 256 bytes, but it's already
2114 	   covered with the kernel page allocation mechanism */
2115 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
2116 				size, &chip->work_ptr) < 0)
2117 		return -ENOMEM;
2118 	ptr = chip->work_ptr.area;
2119 	ptr_addr = chip->work_ptr.addr;
2120 	memset(ptr, 0, size);	/* for sure */
2121 
2122 	chip->bank_base_playback = ptr;
2123 	chip->bank_base_playback_addr = ptr_addr;
2124 	chip->ctrl_playback = (__le32 *)ptr;
2125 	chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
2126 	ptr += ALIGN(playback_ctrl_size, 0x100);
2127 	ptr_addr += ALIGN(playback_ctrl_size, 0x100);
2128 	for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2129 		chip->voices[voice].number = voice;
2130 		chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
2131 		chip->voices[voice].bank_addr = ptr_addr;
2132 		for (bank = 0; bank < 2; bank++) {
2133 			chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
2134 			ptr += chip->bank_size_playback;
2135 			ptr_addr += chip->bank_size_playback;
2136 		}
2137 	}
2138 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2139 	ptr_addr = ALIGN(ptr_addr, 0x100);
2140 	chip->bank_base_capture = ptr;
2141 	chip->bank_base_capture_addr = ptr_addr;
2142 	for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2143 		for (bank = 0; bank < 2; bank++) {
2144 			chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
2145 			ptr += chip->bank_size_capture;
2146 			ptr_addr += chip->bank_size_capture;
2147 		}
2148 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2149 	ptr_addr = ALIGN(ptr_addr, 0x100);
2150 	chip->bank_base_effect = ptr;
2151 	chip->bank_base_effect_addr = ptr_addr;
2152 	for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2153 		for (bank = 0; bank < 2; bank++) {
2154 			chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
2155 			ptr += chip->bank_size_effect;
2156 			ptr_addr += chip->bank_size_effect;
2157 		}
2158 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2159 	ptr_addr = ALIGN(ptr_addr, 0x100);
2160 	chip->work_base = ptr;
2161 	chip->work_base_addr = ptr_addr;
2162 
2163 	snd_BUG_ON(ptr + chip->work_size !=
2164 		   chip->work_ptr.area + chip->work_ptr.bytes);
2165 
2166 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2167 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2168 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2169 	snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2170 	snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2171 
2172 	/* S/PDIF output initialization */
2173 	chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2174 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2175 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2176 
2177 	/* S/PDIF input initialization */
2178 	snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2179 
2180 	/* digital mixer setup */
2181 	for (reg = 0x80; reg < 0xc0; reg += 4)
2182 		snd_ymfpci_writel(chip, reg, 0);
2183 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2184 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
2185 	snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2186 	snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2187 	snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2188 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2189 	snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2190 	snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2191 
2192 	return 0;
2193 }
2194 
2195 static int snd_ymfpci_free(struct snd_ymfpci *chip)
2196 {
2197 	u16 ctrl;
2198 
2199 	if (snd_BUG_ON(!chip))
2200 		return -EINVAL;
2201 
2202 	if (chip->res_reg_area) {	/* don't touch busy hardware */
2203 		snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2204 		snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2205 		snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2206 		snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2207 		snd_ymfpci_disable_dsp(chip);
2208 		snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2209 		snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2210 		snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2211 		snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2212 		snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2213 		ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2214 		snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2215 	}
2216 
2217 	snd_ymfpci_ac3_done(chip);
2218 
2219 	/* Set PCI device to D3 state */
2220 #if 0
2221 	/* FIXME: temporarily disabled, otherwise we cannot fire up
2222 	 * the chip again unless reboot.  ACPI bug?
2223 	 */
2224 	pci_set_power_state(chip->pci, PCI_D3hot);
2225 #endif
2226 
2227 #ifdef CONFIG_PM_SLEEP
2228 	kfree(chip->saved_regs);
2229 #endif
2230 	if (chip->irq >= 0)
2231 		free_irq(chip->irq, chip);
2232 	release_and_free_resource(chip->mpu_res);
2233 	release_and_free_resource(chip->fm_res);
2234 	snd_ymfpci_free_gameport(chip);
2235 	iounmap(chip->reg_area_virt);
2236 	if (chip->work_ptr.area)
2237 		snd_dma_free_pages(&chip->work_ptr);
2238 
2239 	release_and_free_resource(chip->res_reg_area);
2240 
2241 	pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2242 
2243 	pci_disable_device(chip->pci);
2244 	release_firmware(chip->dsp_microcode);
2245 	release_firmware(chip->controller_microcode);
2246 	kfree(chip);
2247 	return 0;
2248 }
2249 
2250 static int snd_ymfpci_dev_free(struct snd_device *device)
2251 {
2252 	struct snd_ymfpci *chip = device->device_data;
2253 	return snd_ymfpci_free(chip);
2254 }
2255 
2256 #ifdef CONFIG_PM_SLEEP
2257 static int saved_regs_index[] = {
2258 	/* spdif */
2259 	YDSXGR_SPDIFOUTCTRL,
2260 	YDSXGR_SPDIFOUTSTATUS,
2261 	YDSXGR_SPDIFINCTRL,
2262 	/* volumes */
2263 	YDSXGR_PRIADCLOOPVOL,
2264 	YDSXGR_NATIVEDACINVOL,
2265 	YDSXGR_NATIVEDACOUTVOL,
2266 	YDSXGR_BUF441OUTVOL,
2267 	YDSXGR_NATIVEADCINVOL,
2268 	YDSXGR_SPDIFLOOPVOL,
2269 	YDSXGR_SPDIFOUTVOL,
2270 	YDSXGR_ZVOUTVOL,
2271 	YDSXGR_LEGACYOUTVOL,
2272 	/* address bases */
2273 	YDSXGR_PLAYCTRLBASE,
2274 	YDSXGR_RECCTRLBASE,
2275 	YDSXGR_EFFCTRLBASE,
2276 	YDSXGR_WORKBASE,
2277 	/* capture set up */
2278 	YDSXGR_MAPOFREC,
2279 	YDSXGR_RECFORMAT,
2280 	YDSXGR_RECSLOTSR,
2281 	YDSXGR_ADCFORMAT,
2282 	YDSXGR_ADCSLOTSR,
2283 };
2284 #define YDSXGR_NUM_SAVED_REGS	ARRAY_SIZE(saved_regs_index)
2285 
2286 static int snd_ymfpci_suspend(struct device *dev)
2287 {
2288 	struct snd_card *card = dev_get_drvdata(dev);
2289 	struct snd_ymfpci *chip = card->private_data;
2290 	unsigned int i;
2291 
2292 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2293 	snd_ac97_suspend(chip->ac97);
2294 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2295 		chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2296 	chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2297 	pci_read_config_word(chip->pci, PCIR_DSXG_LEGACY,
2298 			     &chip->saved_dsxg_legacy);
2299 	pci_read_config_word(chip->pci, PCIR_DSXG_ELEGACY,
2300 			     &chip->saved_dsxg_elegacy);
2301 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2302 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2303 	snd_ymfpci_disable_dsp(chip);
2304 	return 0;
2305 }
2306 
2307 static int snd_ymfpci_resume(struct device *dev)
2308 {
2309 	struct pci_dev *pci = to_pci_dev(dev);
2310 	struct snd_card *card = dev_get_drvdata(dev);
2311 	struct snd_ymfpci *chip = card->private_data;
2312 	unsigned int i;
2313 
2314 	snd_ymfpci_aclink_reset(pci);
2315 	snd_ymfpci_codec_ready(chip, 0);
2316 	snd_ymfpci_download_image(chip);
2317 	udelay(100);
2318 
2319 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2320 		snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2321 
2322 	snd_ac97_resume(chip->ac97);
2323 
2324 	pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY,
2325 			      chip->saved_dsxg_legacy);
2326 	pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY,
2327 			      chip->saved_dsxg_elegacy);
2328 
2329 	/* start hw again */
2330 	if (chip->start_count > 0) {
2331 		spin_lock_irq(&chip->reg_lock);
2332 		snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2333 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2334 		spin_unlock_irq(&chip->reg_lock);
2335 	}
2336 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2337 	return 0;
2338 }
2339 
2340 SIMPLE_DEV_PM_OPS(snd_ymfpci_pm, snd_ymfpci_suspend, snd_ymfpci_resume);
2341 #endif /* CONFIG_PM_SLEEP */
2342 
2343 int snd_ymfpci_create(struct snd_card *card,
2344 		      struct pci_dev *pci,
2345 		      unsigned short old_legacy_ctrl,
2346 		      struct snd_ymfpci **rchip)
2347 {
2348 	struct snd_ymfpci *chip;
2349 	int err;
2350 	static struct snd_device_ops ops = {
2351 		.dev_free =	snd_ymfpci_dev_free,
2352 	};
2353 
2354 	*rchip = NULL;
2355 
2356 	/* enable PCI device */
2357 	if ((err = pci_enable_device(pci)) < 0)
2358 		return err;
2359 
2360 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
2361 	if (chip == NULL) {
2362 		pci_disable_device(pci);
2363 		return -ENOMEM;
2364 	}
2365 	chip->old_legacy_ctrl = old_legacy_ctrl;
2366 	spin_lock_init(&chip->reg_lock);
2367 	spin_lock_init(&chip->voice_lock);
2368 	init_waitqueue_head(&chip->interrupt_sleep);
2369 	atomic_set(&chip->interrupt_sleep_count, 0);
2370 	chip->card = card;
2371 	chip->pci = pci;
2372 	chip->irq = -1;
2373 	chip->device_id = pci->device;
2374 	chip->rev = pci->revision;
2375 	chip->reg_area_phys = pci_resource_start(pci, 0);
2376 	chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2377 	pci_set_master(pci);
2378 	chip->src441_used = -1;
2379 
2380 	if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
2381 		dev_err(chip->card->dev,
2382 			"unable to grab memory region 0x%lx-0x%lx\n",
2383 			chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
2384 		err = -EBUSY;
2385 		goto free_chip;
2386 	}
2387 	if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2388 			KBUILD_MODNAME, chip)) {
2389 		dev_err(chip->card->dev, "unable to grab IRQ %d\n", pci->irq);
2390 		err = -EBUSY;
2391 		goto free_chip;
2392 	}
2393 	chip->irq = pci->irq;
2394 
2395 	snd_ymfpci_aclink_reset(pci);
2396 	if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2397 		err = -EIO;
2398 		goto free_chip;
2399 	}
2400 
2401 	err = snd_ymfpci_request_firmware(chip);
2402 	if (err < 0) {
2403 		dev_err(chip->card->dev, "firmware request failed: %d\n", err);
2404 		goto free_chip;
2405 	}
2406 	snd_ymfpci_download_image(chip);
2407 
2408 	udelay(100); /* seems we need a delay after downloading image.. */
2409 
2410 	if (snd_ymfpci_memalloc(chip) < 0) {
2411 		err = -EIO;
2412 		goto free_chip;
2413 	}
2414 
2415 	err = snd_ymfpci_ac3_init(chip);
2416 	if (err < 0)
2417 		goto free_chip;
2418 
2419 #ifdef CONFIG_PM_SLEEP
2420 	chip->saved_regs = kmalloc_array(YDSXGR_NUM_SAVED_REGS, sizeof(u32),
2421 					 GFP_KERNEL);
2422 	if (chip->saved_regs == NULL) {
2423 		err = -ENOMEM;
2424 		goto free_chip;
2425 	}
2426 #endif
2427 
2428 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
2429 	if (err < 0)
2430 		goto free_chip;
2431 
2432 	snd_ymfpci_proc_init(card, chip);
2433 
2434 	*rchip = chip;
2435 	return 0;
2436 
2437 free_chip:
2438 	snd_ymfpci_free(chip);
2439 	return err;
2440 }
2441