xref: /openbmc/linux/sound/pci/ymfpci/ymfpci_main.c (revision 1eb1a950)
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_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
632 		return err;
633 	return 0;
634 }
635 
636 static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
637 {
638 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
639 	struct snd_pcm_runtime *runtime = substream->runtime;
640 	struct snd_ymfpci_pcm *ypcm;
641 
642 	if (runtime->private_data == NULL)
643 		return 0;
644 	ypcm = runtime->private_data;
645 
646 	/* wait, until the PCI operations are not finished */
647 	snd_ymfpci_irq_wait(chip);
648 	if (ypcm->voices[1]) {
649 		snd_ymfpci_voice_free(chip, ypcm->voices[1]);
650 		ypcm->voices[1] = NULL;
651 	}
652 	if (ypcm->voices[0]) {
653 		snd_ymfpci_voice_free(chip, ypcm->voices[0]);
654 		ypcm->voices[0] = NULL;
655 	}
656 	return 0;
657 }
658 
659 static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
660 {
661 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
662 	struct snd_pcm_runtime *runtime = substream->runtime;
663 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
664 	struct snd_kcontrol *kctl;
665 	unsigned int nvoice;
666 
667 	ypcm->period_size = runtime->period_size;
668 	ypcm->buffer_size = runtime->buffer_size;
669 	ypcm->period_pos = 0;
670 	ypcm->last_pos = 0;
671 	for (nvoice = 0; nvoice < runtime->channels; nvoice++)
672 		snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
673 					  substream->pcm == chip->pcm);
674 
675 	if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
676 		kctl = chip->pcm_mixer[substream->number].ctl;
677 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
678 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
679 	}
680 	return 0;
681 }
682 
683 static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
684 {
685 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
686 
687 	/* wait, until the PCI operations are not finished */
688 	snd_ymfpci_irq_wait(chip);
689 	return 0;
690 }
691 
692 static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
693 {
694 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
695 	struct snd_pcm_runtime *runtime = substream->runtime;
696 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
697 	struct snd_ymfpci_capture_bank * bank;
698 	int nbank;
699 	u32 rate, format;
700 
701 	ypcm->period_size = runtime->period_size;
702 	ypcm->buffer_size = runtime->buffer_size;
703 	ypcm->period_pos = 0;
704 	ypcm->last_pos = 0;
705 	ypcm->shift = 0;
706 	rate = ((48000 * 4096) / runtime->rate) - 1;
707 	format = 0;
708 	if (runtime->channels == 2) {
709 		format |= 2;
710 		ypcm->shift++;
711 	}
712 	if (snd_pcm_format_width(runtime->format) == 8)
713 		format |= 1;
714 	else
715 		ypcm->shift++;
716 	switch (ypcm->capture_bank_number) {
717 	case 0:
718 		snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
719 		snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
720 		break;
721 	case 1:
722 		snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
723 		snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
724 		break;
725 	}
726 	for (nbank = 0; nbank < 2; nbank++) {
727 		bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
728 		bank->base = cpu_to_le32(runtime->dma_addr);
729 		bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
730 		bank->start = 0;
731 		bank->num_of_loops = 0;
732 	}
733 	return 0;
734 }
735 
736 static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
737 {
738 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
739 	struct snd_pcm_runtime *runtime = substream->runtime;
740 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
741 	struct snd_ymfpci_voice *voice = ypcm->voices[0];
742 
743 	if (!(ypcm->running && voice))
744 		return 0;
745 	return le32_to_cpu(voice->bank[chip->active_bank].start);
746 }
747 
748 static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
749 {
750 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
751 	struct snd_pcm_runtime *runtime = substream->runtime;
752 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
753 
754 	if (!ypcm->running)
755 		return 0;
756 	return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
757 }
758 
759 static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
760 {
761 	wait_queue_entry_t wait;
762 	int loops = 4;
763 
764 	while (loops-- > 0) {
765 		if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
766 		 	continue;
767 		init_waitqueue_entry(&wait, current);
768 		add_wait_queue(&chip->interrupt_sleep, &wait);
769 		atomic_inc(&chip->interrupt_sleep_count);
770 		schedule_timeout_uninterruptible(msecs_to_jiffies(50));
771 		remove_wait_queue(&chip->interrupt_sleep, &wait);
772 	}
773 }
774 
775 static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
776 {
777 	struct snd_ymfpci *chip = dev_id;
778 	u32 status, nvoice, mode;
779 	struct snd_ymfpci_voice *voice;
780 
781 	status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
782 	if (status & 0x80000000) {
783 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
784 		spin_lock(&chip->voice_lock);
785 		for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
786 			voice = &chip->voices[nvoice];
787 			if (voice->interrupt)
788 				voice->interrupt(chip, voice);
789 		}
790 		for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
791 			if (chip->capture_substream[nvoice])
792 				snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
793 		}
794 #if 0
795 		for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
796 			if (chip->effect_substream[nvoice])
797 				snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
798 		}
799 #endif
800 		spin_unlock(&chip->voice_lock);
801 		spin_lock(&chip->reg_lock);
802 		snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
803 		mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
804 		snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
805 		spin_unlock(&chip->reg_lock);
806 
807 		if (atomic_read(&chip->interrupt_sleep_count)) {
808 			atomic_set(&chip->interrupt_sleep_count, 0);
809 			wake_up(&chip->interrupt_sleep);
810 		}
811 	}
812 
813 	status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
814 	if (status & 1) {
815 		if (chip->timer)
816 			snd_timer_interrupt(chip->timer, chip->timer_ticks);
817 	}
818 	snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
819 
820 	if (chip->rawmidi)
821 		snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
822 	return IRQ_HANDLED;
823 }
824 
825 static const struct snd_pcm_hardware snd_ymfpci_playback =
826 {
827 	.info =			(SNDRV_PCM_INFO_MMAP |
828 				 SNDRV_PCM_INFO_MMAP_VALID |
829 				 SNDRV_PCM_INFO_INTERLEAVED |
830 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
831 				 SNDRV_PCM_INFO_PAUSE |
832 				 SNDRV_PCM_INFO_RESUME),
833 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
834 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
835 	.rate_min =		8000,
836 	.rate_max =		48000,
837 	.channels_min =		1,
838 	.channels_max =		2,
839 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
840 	.period_bytes_min =	64,
841 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
842 	.periods_min =		3,
843 	.periods_max =		1024,
844 	.fifo_size =		0,
845 };
846 
847 static const struct snd_pcm_hardware snd_ymfpci_capture =
848 {
849 	.info =			(SNDRV_PCM_INFO_MMAP |
850 				 SNDRV_PCM_INFO_MMAP_VALID |
851 				 SNDRV_PCM_INFO_INTERLEAVED |
852 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
853 				 SNDRV_PCM_INFO_PAUSE |
854 				 SNDRV_PCM_INFO_RESUME),
855 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
856 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
857 	.rate_min =		8000,
858 	.rate_max =		48000,
859 	.channels_min =		1,
860 	.channels_max =		2,
861 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
862 	.period_bytes_min =	64,
863 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
864 	.periods_min =		3,
865 	.periods_max =		1024,
866 	.fifo_size =		0,
867 };
868 
869 static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
870 {
871 	kfree(runtime->private_data);
872 }
873 
874 static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
875 {
876 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
877 	struct snd_pcm_runtime *runtime = substream->runtime;
878 	struct snd_ymfpci_pcm *ypcm;
879 	int err;
880 
881 	runtime->hw = snd_ymfpci_playback;
882 	/* FIXME? True value is 256/48 = 5.33333 ms */
883 	err = snd_pcm_hw_constraint_minmax(runtime,
884 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
885 					   5334, UINT_MAX);
886 	if (err < 0)
887 		return err;
888 	err = snd_pcm_hw_rule_noresample(runtime, 48000);
889 	if (err < 0)
890 		return err;
891 
892 	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
893 	if (ypcm == NULL)
894 		return -ENOMEM;
895 	ypcm->chip = chip;
896 	ypcm->type = PLAYBACK_VOICE;
897 	ypcm->substream = substream;
898 	runtime->private_data = ypcm;
899 	runtime->private_free = snd_ymfpci_pcm_free_substream;
900 	return 0;
901 }
902 
903 /* call with spinlock held */
904 static void ymfpci_open_extension(struct snd_ymfpci *chip)
905 {
906 	if (! chip->rear_opened) {
907 		if (! chip->spdif_opened) /* set AC3 */
908 			snd_ymfpci_writel(chip, YDSXGR_MODE,
909 					  snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
910 		/* enable second codec (4CHEN) */
911 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
912 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
913 	}
914 }
915 
916 /* call with spinlock held */
917 static void ymfpci_close_extension(struct snd_ymfpci *chip)
918 {
919 	if (! chip->rear_opened) {
920 		if (! chip->spdif_opened)
921 			snd_ymfpci_writel(chip, YDSXGR_MODE,
922 					  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
923 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
924 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
925 	}
926 }
927 
928 static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
929 {
930 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
931 	struct snd_pcm_runtime *runtime = substream->runtime;
932 	struct snd_ymfpci_pcm *ypcm;
933 	int err;
934 
935 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
936 		return err;
937 	ypcm = runtime->private_data;
938 	ypcm->output_front = 1;
939 	ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
940 	ypcm->swap_rear = 0;
941 	spin_lock_irq(&chip->reg_lock);
942 	if (ypcm->output_rear) {
943 		ymfpci_open_extension(chip);
944 		chip->rear_opened++;
945 	}
946 	spin_unlock_irq(&chip->reg_lock);
947 	return 0;
948 }
949 
950 static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
951 {
952 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
953 	struct snd_pcm_runtime *runtime = substream->runtime;
954 	struct snd_ymfpci_pcm *ypcm;
955 	int err;
956 
957 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
958 		return err;
959 	ypcm = runtime->private_data;
960 	ypcm->output_front = 0;
961 	ypcm->output_rear = 1;
962 	ypcm->swap_rear = 1;
963 	spin_lock_irq(&chip->reg_lock);
964 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
965 			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
966 	ymfpci_open_extension(chip);
967 	chip->spdif_pcm_bits = chip->spdif_bits;
968 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
969 	chip->spdif_opened++;
970 	spin_unlock_irq(&chip->reg_lock);
971 
972 	chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
973 	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
974 		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
975 	return 0;
976 }
977 
978 static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
979 {
980 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
981 	struct snd_pcm_runtime *runtime = substream->runtime;
982 	struct snd_ymfpci_pcm *ypcm;
983 	int err;
984 
985 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
986 		return err;
987 	ypcm = runtime->private_data;
988 	ypcm->output_front = 0;
989 	ypcm->output_rear = 1;
990 	ypcm->swap_rear = 0;
991 	spin_lock_irq(&chip->reg_lock);
992 	ymfpci_open_extension(chip);
993 	chip->rear_opened++;
994 	spin_unlock_irq(&chip->reg_lock);
995 	return 0;
996 }
997 
998 static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
999 				   u32 capture_bank_number)
1000 {
1001 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1002 	struct snd_pcm_runtime *runtime = substream->runtime;
1003 	struct snd_ymfpci_pcm *ypcm;
1004 	int err;
1005 
1006 	runtime->hw = snd_ymfpci_capture;
1007 	/* FIXME? True value is 256/48 = 5.33333 ms */
1008 	err = snd_pcm_hw_constraint_minmax(runtime,
1009 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1010 					   5334, UINT_MAX);
1011 	if (err < 0)
1012 		return err;
1013 	err = snd_pcm_hw_rule_noresample(runtime, 48000);
1014 	if (err < 0)
1015 		return err;
1016 
1017 	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
1018 	if (ypcm == NULL)
1019 		return -ENOMEM;
1020 	ypcm->chip = chip;
1021 	ypcm->type = capture_bank_number + CAPTURE_REC;
1022 	ypcm->substream = substream;
1023 	ypcm->capture_bank_number = capture_bank_number;
1024 	chip->capture_substream[capture_bank_number] = substream;
1025 	runtime->private_data = ypcm;
1026 	runtime->private_free = snd_ymfpci_pcm_free_substream;
1027 	snd_ymfpci_hw_start(chip);
1028 	return 0;
1029 }
1030 
1031 static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
1032 {
1033 	return snd_ymfpci_capture_open(substream, 0);
1034 }
1035 
1036 static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
1037 {
1038 	return snd_ymfpci_capture_open(substream, 1);
1039 }
1040 
1041 static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
1042 {
1043 	return 0;
1044 }
1045 
1046 static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
1047 {
1048 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1049 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1050 
1051 	spin_lock_irq(&chip->reg_lock);
1052 	if (ypcm->output_rear && chip->rear_opened > 0) {
1053 		chip->rear_opened--;
1054 		ymfpci_close_extension(chip);
1055 	}
1056 	spin_unlock_irq(&chip->reg_lock);
1057 	return snd_ymfpci_playback_close_1(substream);
1058 }
1059 
1060 static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
1061 {
1062 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1063 
1064 	spin_lock_irq(&chip->reg_lock);
1065 	chip->spdif_opened = 0;
1066 	ymfpci_close_extension(chip);
1067 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
1068 			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
1069 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1070 	spin_unlock_irq(&chip->reg_lock);
1071 	chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1072 	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
1073 		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
1074 	return snd_ymfpci_playback_close_1(substream);
1075 }
1076 
1077 static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
1078 {
1079 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1080 
1081 	spin_lock_irq(&chip->reg_lock);
1082 	if (chip->rear_opened > 0) {
1083 		chip->rear_opened--;
1084 		ymfpci_close_extension(chip);
1085 	}
1086 	spin_unlock_irq(&chip->reg_lock);
1087 	return snd_ymfpci_playback_close_1(substream);
1088 }
1089 
1090 static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
1091 {
1092 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1093 	struct snd_pcm_runtime *runtime = substream->runtime;
1094 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
1095 
1096 	if (ypcm != NULL) {
1097 		chip->capture_substream[ypcm->capture_bank_number] = NULL;
1098 		snd_ymfpci_hw_stop(chip);
1099 	}
1100 	return 0;
1101 }
1102 
1103 static const struct snd_pcm_ops snd_ymfpci_playback_ops = {
1104 	.open =			snd_ymfpci_playback_open,
1105 	.close =		snd_ymfpci_playback_close,
1106 	.ioctl =		snd_pcm_lib_ioctl,
1107 	.hw_params =		snd_ymfpci_playback_hw_params,
1108 	.hw_free =		snd_ymfpci_playback_hw_free,
1109 	.prepare =		snd_ymfpci_playback_prepare,
1110 	.trigger =		snd_ymfpci_playback_trigger,
1111 	.pointer =		snd_ymfpci_playback_pointer,
1112 };
1113 
1114 static const struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
1115 	.open =			snd_ymfpci_capture_rec_open,
1116 	.close =		snd_ymfpci_capture_close,
1117 	.ioctl =		snd_pcm_lib_ioctl,
1118 	.hw_free =		snd_ymfpci_capture_hw_free,
1119 	.prepare =		snd_ymfpci_capture_prepare,
1120 	.trigger =		snd_ymfpci_capture_trigger,
1121 	.pointer =		snd_ymfpci_capture_pointer,
1122 };
1123 
1124 int snd_ymfpci_pcm(struct snd_ymfpci *chip, int device)
1125 {
1126 	struct snd_pcm *pcm;
1127 	int err;
1128 
1129 	if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
1130 		return err;
1131 	pcm->private_data = chip;
1132 
1133 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
1134 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
1135 
1136 	/* global setup */
1137 	pcm->info_flags = 0;
1138 	strcpy(pcm->name, "YMFPCI");
1139 	chip->pcm = pcm;
1140 
1141 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1142 				       &chip->pci->dev, 64*1024, 256*1024);
1143 
1144 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1145 				     snd_pcm_std_chmaps, 2, 0, NULL);
1146 }
1147 
1148 static const struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
1149 	.open =			snd_ymfpci_capture_ac97_open,
1150 	.close =		snd_ymfpci_capture_close,
1151 	.ioctl =		snd_pcm_lib_ioctl,
1152 	.hw_free =		snd_ymfpci_capture_hw_free,
1153 	.prepare =		snd_ymfpci_capture_prepare,
1154 	.trigger =		snd_ymfpci_capture_trigger,
1155 	.pointer =		snd_ymfpci_capture_pointer,
1156 };
1157 
1158 int snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device)
1159 {
1160 	struct snd_pcm *pcm;
1161 	int err;
1162 
1163 	if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
1164 		return err;
1165 	pcm->private_data = chip;
1166 
1167 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
1168 
1169 	/* global setup */
1170 	pcm->info_flags = 0;
1171 	sprintf(pcm->name, "YMFPCI - %s",
1172 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
1173 	chip->pcm2 = pcm;
1174 
1175 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1176 				       &chip->pci->dev, 64*1024, 256*1024);
1177 
1178 	return 0;
1179 }
1180 
1181 static const struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
1182 	.open =			snd_ymfpci_playback_spdif_open,
1183 	.close =		snd_ymfpci_playback_spdif_close,
1184 	.ioctl =		snd_pcm_lib_ioctl,
1185 	.hw_params =		snd_ymfpci_playback_hw_params,
1186 	.hw_free =		snd_ymfpci_playback_hw_free,
1187 	.prepare =		snd_ymfpci_playback_prepare,
1188 	.trigger =		snd_ymfpci_playback_trigger,
1189 	.pointer =		snd_ymfpci_playback_pointer,
1190 };
1191 
1192 int snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device)
1193 {
1194 	struct snd_pcm *pcm;
1195 	int err;
1196 
1197 	if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
1198 		return err;
1199 	pcm->private_data = chip;
1200 
1201 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
1202 
1203 	/* global setup */
1204 	pcm->info_flags = 0;
1205 	strcpy(pcm->name, "YMFPCI - IEC958");
1206 	chip->pcm_spdif = pcm;
1207 
1208 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1209 				       &chip->pci->dev, 64*1024, 256*1024);
1210 
1211 	return 0;
1212 }
1213 
1214 static const struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
1215 	.open =			snd_ymfpci_playback_4ch_open,
1216 	.close =		snd_ymfpci_playback_4ch_close,
1217 	.ioctl =		snd_pcm_lib_ioctl,
1218 	.hw_params =		snd_ymfpci_playback_hw_params,
1219 	.hw_free =		snd_ymfpci_playback_hw_free,
1220 	.prepare =		snd_ymfpci_playback_prepare,
1221 	.trigger =		snd_ymfpci_playback_trigger,
1222 	.pointer =		snd_ymfpci_playback_pointer,
1223 };
1224 
1225 static const struct snd_pcm_chmap_elem surround_map[] = {
1226 	{ .channels = 1,
1227 	  .map = { SNDRV_CHMAP_MONO } },
1228 	{ .channels = 2,
1229 	  .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
1230 	{ }
1231 };
1232 
1233 int snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device)
1234 {
1235 	struct snd_pcm *pcm;
1236 	int err;
1237 
1238 	if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
1239 		return err;
1240 	pcm->private_data = chip;
1241 
1242 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
1243 
1244 	/* global setup */
1245 	pcm->info_flags = 0;
1246 	strcpy(pcm->name, "YMFPCI - Rear PCM");
1247 	chip->pcm_4ch = pcm;
1248 
1249 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1250 				       &chip->pci->dev, 64*1024, 256*1024);
1251 
1252 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1253 				     surround_map, 2, 0, NULL);
1254 }
1255 
1256 static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1257 {
1258 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1259 	uinfo->count = 1;
1260 	return 0;
1261 }
1262 
1263 static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1264 					struct snd_ctl_elem_value *ucontrol)
1265 {
1266 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1267 
1268 	spin_lock_irq(&chip->reg_lock);
1269 	ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
1270 	ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1271 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1272 	spin_unlock_irq(&chip->reg_lock);
1273 	return 0;
1274 }
1275 
1276 static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1277 					 struct snd_ctl_elem_value *ucontrol)
1278 {
1279 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1280 	unsigned int val;
1281 	int change;
1282 
1283 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1284 	      (ucontrol->value.iec958.status[1] << 8);
1285 	spin_lock_irq(&chip->reg_lock);
1286 	change = chip->spdif_bits != val;
1287 	chip->spdif_bits = val;
1288 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
1289 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
1290 	spin_unlock_irq(&chip->reg_lock);
1291 	return change;
1292 }
1293 
1294 static const struct snd_kcontrol_new snd_ymfpci_spdif_default =
1295 {
1296 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1297 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1298 	.info =		snd_ymfpci_spdif_default_info,
1299 	.get =		snd_ymfpci_spdif_default_get,
1300 	.put =		snd_ymfpci_spdif_default_put
1301 };
1302 
1303 static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1304 {
1305 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1306 	uinfo->count = 1;
1307 	return 0;
1308 }
1309 
1310 static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1311 				      struct snd_ctl_elem_value *ucontrol)
1312 {
1313 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1314 
1315 	spin_lock_irq(&chip->reg_lock);
1316 	ucontrol->value.iec958.status[0] = 0x3e;
1317 	ucontrol->value.iec958.status[1] = 0xff;
1318 	spin_unlock_irq(&chip->reg_lock);
1319 	return 0;
1320 }
1321 
1322 static const struct snd_kcontrol_new snd_ymfpci_spdif_mask =
1323 {
1324 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1325 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1326 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1327 	.info =		snd_ymfpci_spdif_mask_info,
1328 	.get =		snd_ymfpci_spdif_mask_get,
1329 };
1330 
1331 static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1332 {
1333 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1334 	uinfo->count = 1;
1335 	return 0;
1336 }
1337 
1338 static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1339 					struct snd_ctl_elem_value *ucontrol)
1340 {
1341 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1342 
1343 	spin_lock_irq(&chip->reg_lock);
1344 	ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
1345 	ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1346 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
1347 	spin_unlock_irq(&chip->reg_lock);
1348 	return 0;
1349 }
1350 
1351 static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1352 					struct snd_ctl_elem_value *ucontrol)
1353 {
1354 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1355 	unsigned int val;
1356 	int change;
1357 
1358 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
1359 	      (ucontrol->value.iec958.status[1] << 8);
1360 	spin_lock_irq(&chip->reg_lock);
1361 	change = chip->spdif_pcm_bits != val;
1362 	chip->spdif_pcm_bits = val;
1363 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
1364 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
1365 	spin_unlock_irq(&chip->reg_lock);
1366 	return change;
1367 }
1368 
1369 static const struct snd_kcontrol_new snd_ymfpci_spdif_stream =
1370 {
1371 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1372 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1373 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1374 	.info =		snd_ymfpci_spdif_stream_info,
1375 	.get =		snd_ymfpci_spdif_stream_get,
1376 	.put =		snd_ymfpci_spdif_stream_put
1377 };
1378 
1379 static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
1380 {
1381 	static const char *const texts[3] = {"AC'97", "IEC958", "ZV Port"};
1382 
1383 	return snd_ctl_enum_info(info, 1, 3, texts);
1384 }
1385 
1386 static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1387 {
1388 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1389 	u16 reg;
1390 
1391 	spin_lock_irq(&chip->reg_lock);
1392 	reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1393 	spin_unlock_irq(&chip->reg_lock);
1394 	if (!(reg & 0x100))
1395 		value->value.enumerated.item[0] = 0;
1396 	else
1397 		value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
1398 	return 0;
1399 }
1400 
1401 static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
1402 {
1403 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1404 	u16 reg, old_reg;
1405 
1406 	spin_lock_irq(&chip->reg_lock);
1407 	old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
1408 	if (value->value.enumerated.item[0] == 0)
1409 		reg = old_reg & ~0x100;
1410 	else
1411 		reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
1412 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
1413 	spin_unlock_irq(&chip->reg_lock);
1414 	return reg != old_reg;
1415 }
1416 
1417 static const struct snd_kcontrol_new snd_ymfpci_drec_source = {
1418 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
1419 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
1420 	.name =		"Direct Recording Source",
1421 	.info =		snd_ymfpci_drec_source_info,
1422 	.get =		snd_ymfpci_drec_source_get,
1423 	.put =		snd_ymfpci_drec_source_put
1424 };
1425 
1426 /*
1427  *  Mixer controls
1428  */
1429 
1430 #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
1431 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1432   .info = snd_ymfpci_info_single, \
1433   .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1434   .private_value = ((reg) | ((shift) << 16)) }
1435 
1436 #define snd_ymfpci_info_single		snd_ctl_boolean_mono_info
1437 
1438 static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1439 				 struct snd_ctl_elem_value *ucontrol)
1440 {
1441 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1442 	int reg = kcontrol->private_value & 0xffff;
1443 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1444 	unsigned int mask = 1;
1445 
1446 	switch (reg) {
1447 	case YDSXGR_SPDIFOUTCTRL: break;
1448 	case YDSXGR_SPDIFINCTRL: break;
1449 	default: return -EINVAL;
1450 	}
1451 	ucontrol->value.integer.value[0] =
1452 		(snd_ymfpci_readl(chip, reg) >> shift) & mask;
1453 	return 0;
1454 }
1455 
1456 static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1457 				 struct snd_ctl_elem_value *ucontrol)
1458 {
1459 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1460 	int reg = kcontrol->private_value & 0xffff;
1461 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1462  	unsigned int mask = 1;
1463 	int change;
1464 	unsigned int val, oval;
1465 
1466 	switch (reg) {
1467 	case YDSXGR_SPDIFOUTCTRL: break;
1468 	case YDSXGR_SPDIFINCTRL: break;
1469 	default: return -EINVAL;
1470 	}
1471 	val = (ucontrol->value.integer.value[0] & mask);
1472 	val <<= shift;
1473 	spin_lock_irq(&chip->reg_lock);
1474 	oval = snd_ymfpci_readl(chip, reg);
1475 	val = (oval & ~(mask << shift)) | val;
1476 	change = val != oval;
1477 	snd_ymfpci_writel(chip, reg, val);
1478 	spin_unlock_irq(&chip->reg_lock);
1479 	return change;
1480 }
1481 
1482 static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
1483 
1484 #define YMFPCI_DOUBLE(xname, xindex, reg) \
1485 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1486   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
1487   .info = snd_ymfpci_info_double, \
1488   .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
1489   .private_value = reg, \
1490   .tlv = { .p = db_scale_native } }
1491 
1492 static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1493 {
1494 	unsigned int reg = kcontrol->private_value;
1495 
1496 	if (reg < 0x80 || reg >= 0xc0)
1497 		return -EINVAL;
1498 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1499 	uinfo->count = 2;
1500 	uinfo->value.integer.min = 0;
1501 	uinfo->value.integer.max = 16383;
1502 	return 0;
1503 }
1504 
1505 static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1506 {
1507 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1508 	unsigned int reg = kcontrol->private_value;
1509 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1510 	unsigned int val;
1511 
1512 	if (reg < 0x80 || reg >= 0xc0)
1513 		return -EINVAL;
1514 	spin_lock_irq(&chip->reg_lock);
1515 	val = snd_ymfpci_readl(chip, reg);
1516 	spin_unlock_irq(&chip->reg_lock);
1517 	ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
1518 	ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
1519 	return 0;
1520 }
1521 
1522 static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1523 {
1524 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1525 	unsigned int reg = kcontrol->private_value;
1526 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
1527 	int change;
1528 	unsigned int val1, val2, oval;
1529 
1530 	if (reg < 0x80 || reg >= 0xc0)
1531 		return -EINVAL;
1532 	val1 = ucontrol->value.integer.value[0] & mask;
1533 	val2 = ucontrol->value.integer.value[1] & mask;
1534 	val1 <<= shift_left;
1535 	val2 <<= shift_right;
1536 	spin_lock_irq(&chip->reg_lock);
1537 	oval = snd_ymfpci_readl(chip, reg);
1538 	val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
1539 	change = val1 != oval;
1540 	snd_ymfpci_writel(chip, reg, val1);
1541 	spin_unlock_irq(&chip->reg_lock);
1542 	return change;
1543 }
1544 
1545 static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1546 				       struct snd_ctl_elem_value *ucontrol)
1547 {
1548 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1549 	unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1550 	unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1551 	int change;
1552 	unsigned int value, oval;
1553 
1554 	value = ucontrol->value.integer.value[0] & 0x3fff;
1555 	value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1556 	spin_lock_irq(&chip->reg_lock);
1557 	oval = snd_ymfpci_readl(chip, reg);
1558 	change = value != oval;
1559 	snd_ymfpci_writel(chip, reg, value);
1560 	snd_ymfpci_writel(chip, reg2, value);
1561 	spin_unlock_irq(&chip->reg_lock);
1562 	return change;
1563 }
1564 
1565 /*
1566  * 4ch duplication
1567  */
1568 #define snd_ymfpci_info_dup4ch		snd_ctl_boolean_mono_info
1569 
1570 static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1571 {
1572 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1573 	ucontrol->value.integer.value[0] = chip->mode_dup4ch;
1574 	return 0;
1575 }
1576 
1577 static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1578 {
1579 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1580 	int change;
1581 	change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
1582 	if (change)
1583 		chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
1584 	return change;
1585 }
1586 
1587 static const struct snd_kcontrol_new snd_ymfpci_dup4ch = {
1588 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1589 	.name = "4ch Duplication",
1590 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
1591 	.info = snd_ymfpci_info_dup4ch,
1592 	.get = snd_ymfpci_get_dup4ch,
1593 	.put = snd_ymfpci_put_dup4ch,
1594 };
1595 
1596 static struct snd_kcontrol_new snd_ymfpci_controls[] = {
1597 {
1598 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1599 	.name = "Wave Playback Volume",
1600 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1601 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1602 	.info = snd_ymfpci_info_double,
1603 	.get = snd_ymfpci_get_double,
1604 	.put = snd_ymfpci_put_nativedacvol,
1605 	.private_value = YDSXGR_NATIVEDACOUTVOL,
1606 	.tlv = { .p = db_scale_native },
1607 },
1608 YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
1609 YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
1610 YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
1611 YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
1612 YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
1613 YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
1614 YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
1615 YMFPCI_DOUBLE("FM Legacy Playback Volume", 0, YDSXGR_LEGACYOUTVOL),
1616 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
1617 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
1618 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
1619 YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1620 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1621 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1622 YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
1623 };
1624 
1625 
1626 /*
1627  * GPIO
1628  */
1629 
1630 static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
1631 {
1632 	u16 reg, mode;
1633 	unsigned long flags;
1634 
1635 	spin_lock_irqsave(&chip->reg_lock, flags);
1636 	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1637 	reg &= ~(1 << (pin + 8));
1638 	reg |= (1 << pin);
1639 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1640 	/* set the level mode for input line */
1641 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
1642 	mode &= ~(3 << (pin * 2));
1643 	snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
1644 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1645 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
1646 	spin_unlock_irqrestore(&chip->reg_lock, flags);
1647 	return (mode >> pin) & 1;
1648 }
1649 
1650 static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
1651 {
1652 	u16 reg;
1653 	unsigned long flags;
1654 
1655 	spin_lock_irqsave(&chip->reg_lock, flags);
1656 	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
1657 	reg &= ~(1 << pin);
1658 	reg &= ~(1 << (pin + 8));
1659 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
1660 	snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
1661 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
1662 	spin_unlock_irqrestore(&chip->reg_lock, flags);
1663 
1664 	return 0;
1665 }
1666 
1667 #define snd_ymfpci_gpio_sw_info		snd_ctl_boolean_mono_info
1668 
1669 static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1670 {
1671 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1672 	int pin = (int)kcontrol->private_value;
1673 	ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1674 	return 0;
1675 }
1676 
1677 static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1678 {
1679 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1680 	int pin = (int)kcontrol->private_value;
1681 
1682 	if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
1683 		snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
1684 		ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
1685 		return 1;
1686 	}
1687 	return 0;
1688 }
1689 
1690 static const struct snd_kcontrol_new snd_ymfpci_rear_shared = {
1691 	.name = "Shared Rear/Line-In Switch",
1692 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1693 	.info = snd_ymfpci_gpio_sw_info,
1694 	.get = snd_ymfpci_gpio_sw_get,
1695 	.put = snd_ymfpci_gpio_sw_put,
1696 	.private_value = 2,
1697 };
1698 
1699 /*
1700  * PCM voice volume
1701  */
1702 
1703 static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1704 				   struct snd_ctl_elem_info *uinfo)
1705 {
1706 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1707 	uinfo->count = 2;
1708 	uinfo->value.integer.min = 0;
1709 	uinfo->value.integer.max = 0x8000;
1710 	return 0;
1711 }
1712 
1713 static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1714 				  struct snd_ctl_elem_value *ucontrol)
1715 {
1716 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1717 	unsigned int subs = kcontrol->id.subdevice;
1718 
1719 	ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
1720 	ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
1721 	return 0;
1722 }
1723 
1724 static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1725 				  struct snd_ctl_elem_value *ucontrol)
1726 {
1727 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1728 	unsigned int subs = kcontrol->id.subdevice;
1729 	struct snd_pcm_substream *substream;
1730 	unsigned long flags;
1731 
1732 	if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
1733 	    ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
1734 		chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
1735 		chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
1736 		if (chip->pcm_mixer[subs].left > 0x8000)
1737 			chip->pcm_mixer[subs].left = 0x8000;
1738 		if (chip->pcm_mixer[subs].right > 0x8000)
1739 			chip->pcm_mixer[subs].right = 0x8000;
1740 
1741 		substream = (struct snd_pcm_substream *)kcontrol->private_value;
1742 		spin_lock_irqsave(&chip->voice_lock, flags);
1743 		if (substream->runtime && substream->runtime->private_data) {
1744 			struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
1745 			if (!ypcm->use_441_slot)
1746 				ypcm->update_pcm_vol = 2;
1747 		}
1748 		spin_unlock_irqrestore(&chip->voice_lock, flags);
1749 		return 1;
1750 	}
1751 	return 0;
1752 }
1753 
1754 static const struct snd_kcontrol_new snd_ymfpci_pcm_volume = {
1755 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1756 	.name = "PCM Playback Volume",
1757 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1758 		SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1759 	.info = snd_ymfpci_pcm_vol_info,
1760 	.get = snd_ymfpci_pcm_vol_get,
1761 	.put = snd_ymfpci_pcm_vol_put,
1762 };
1763 
1764 
1765 /*
1766  *  Mixer routines
1767  */
1768 
1769 static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
1770 {
1771 	struct snd_ymfpci *chip = bus->private_data;
1772 	chip->ac97_bus = NULL;
1773 }
1774 
1775 static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
1776 {
1777 	struct snd_ymfpci *chip = ac97->private_data;
1778 	chip->ac97 = NULL;
1779 }
1780 
1781 int snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
1782 {
1783 	struct snd_ac97_template ac97;
1784 	struct snd_kcontrol *kctl;
1785 	struct snd_pcm_substream *substream;
1786 	unsigned int idx;
1787 	int err;
1788 	static struct snd_ac97_bus_ops ops = {
1789 		.write = snd_ymfpci_codec_write,
1790 		.read = snd_ymfpci_codec_read,
1791 	};
1792 
1793 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
1794 		return err;
1795 	chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
1796 	chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
1797 
1798 	memset(&ac97, 0, sizeof(ac97));
1799 	ac97.private_data = chip;
1800 	ac97.private_free = snd_ymfpci_mixer_free_ac97;
1801 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
1802 		return err;
1803 
1804 	/* to be sure */
1805 	snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
1806 			     AC97_EA_VRA|AC97_EA_VRM, 0);
1807 
1808 	for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1809 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
1810 			return err;
1811 	}
1812 	if (chip->ac97->ext_id & AC97_EI_SDAC) {
1813 		kctl = snd_ctl_new1(&snd_ymfpci_dup4ch, chip);
1814 		err = snd_ctl_add(chip->card, kctl);
1815 		if (err < 0)
1816 			return err;
1817 	}
1818 
1819 	/* add S/PDIF control */
1820 	if (snd_BUG_ON(!chip->pcm_spdif))
1821 		return -ENXIO;
1822 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
1823 		return err;
1824 	kctl->id.device = chip->pcm_spdif->device;
1825 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
1826 		return err;
1827 	kctl->id.device = chip->pcm_spdif->device;
1828 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
1829 		return err;
1830 	kctl->id.device = chip->pcm_spdif->device;
1831 	chip->spdif_pcm_ctl = kctl;
1832 
1833 	/* direct recording source */
1834 	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
1835 	    (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
1836 		return err;
1837 
1838 	/*
1839 	 * shared rear/line-in
1840 	 */
1841 	if (rear_switch) {
1842 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
1843 			return err;
1844 	}
1845 
1846 	/* per-voice volume */
1847 	substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1848 	for (idx = 0; idx < 32; ++idx) {
1849 		kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
1850 		if (!kctl)
1851 			return -ENOMEM;
1852 		kctl->id.device = chip->pcm->device;
1853 		kctl->id.subdevice = idx;
1854 		kctl->private_value = (unsigned long)substream;
1855 		if ((err = snd_ctl_add(chip->card, kctl)) < 0)
1856 			return err;
1857 		chip->pcm_mixer[idx].left = 0x8000;
1858 		chip->pcm_mixer[idx].right = 0x8000;
1859 		chip->pcm_mixer[idx].ctl = kctl;
1860 		substream = substream->next;
1861 	}
1862 
1863 	return 0;
1864 }
1865 
1866 
1867 /*
1868  * timer
1869  */
1870 
1871 static int snd_ymfpci_timer_start(struct snd_timer *timer)
1872 {
1873 	struct snd_ymfpci *chip;
1874 	unsigned long flags;
1875 	unsigned int count;
1876 
1877 	chip = snd_timer_chip(timer);
1878 	spin_lock_irqsave(&chip->reg_lock, flags);
1879 	if (timer->sticks > 1) {
1880 		chip->timer_ticks = timer->sticks;
1881 		count = timer->sticks - 1;
1882 	} else {
1883 		/*
1884 		 * Divisor 1 is not allowed; fake it by using divisor 2 and
1885 		 * counting two ticks for each interrupt.
1886 		 */
1887 		chip->timer_ticks = 2;
1888 		count = 2 - 1;
1889 	}
1890 	snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
1891 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
1892 	spin_unlock_irqrestore(&chip->reg_lock, flags);
1893 	return 0;
1894 }
1895 
1896 static int snd_ymfpci_timer_stop(struct snd_timer *timer)
1897 {
1898 	struct snd_ymfpci *chip;
1899 	unsigned long flags;
1900 
1901 	chip = snd_timer_chip(timer);
1902 	spin_lock_irqsave(&chip->reg_lock, flags);
1903 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
1904 	spin_unlock_irqrestore(&chip->reg_lock, flags);
1905 	return 0;
1906 }
1907 
1908 static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
1909 					       unsigned long *num, unsigned long *den)
1910 {
1911 	*num = 1;
1912 	*den = 96000;
1913 	return 0;
1914 }
1915 
1916 static struct snd_timer_hardware snd_ymfpci_timer_hw = {
1917 	.flags = SNDRV_TIMER_HW_AUTO,
1918 	.resolution = 10417, /* 1 / 96 kHz = 10.41666...us */
1919 	.ticks = 0x10000,
1920 	.start = snd_ymfpci_timer_start,
1921 	.stop = snd_ymfpci_timer_stop,
1922 	.precise_resolution = snd_ymfpci_timer_precise_resolution,
1923 };
1924 
1925 int snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
1926 {
1927 	struct snd_timer *timer = NULL;
1928 	struct snd_timer_id tid;
1929 	int err;
1930 
1931 	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1932 	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1933 	tid.card = chip->card->number;
1934 	tid.device = device;
1935 	tid.subdevice = 0;
1936 	if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
1937 		strcpy(timer->name, "YMFPCI timer");
1938 		timer->private_data = chip;
1939 		timer->hw = snd_ymfpci_timer_hw;
1940 	}
1941 	chip->timer = timer;
1942 	return err;
1943 }
1944 
1945 
1946 /*
1947  *  proc interface
1948  */
1949 
1950 static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1951 				 struct snd_info_buffer *buffer)
1952 {
1953 	struct snd_ymfpci *chip = entry->private_data;
1954 	int i;
1955 
1956 	snd_iprintf(buffer, "YMFPCI\n\n");
1957 	for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
1958 		snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
1959 }
1960 
1961 static int snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
1962 {
1963 	return snd_card_ro_proc_new(card, "ymfpci", chip, snd_ymfpci_proc_read);
1964 }
1965 
1966 /*
1967  *  initialization routines
1968  */
1969 
1970 static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
1971 {
1972 	u8 cmd;
1973 
1974 	pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
1975 #if 0 // force to reset
1976 	if (cmd & 0x03) {
1977 #endif
1978 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1979 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
1980 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
1981 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
1982 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
1983 #if 0
1984 	}
1985 #endif
1986 }
1987 
1988 static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
1989 {
1990 	snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
1991 }
1992 
1993 static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
1994 {
1995 	u32 val;
1996 	int timeout = 1000;
1997 
1998 	val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
1999 	if (val)
2000 		snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
2001 	while (timeout-- > 0) {
2002 		val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
2003 		if ((val & 0x00000002) == 0)
2004 			break;
2005 	}
2006 }
2007 
2008 static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2009 {
2010 	int err, is_1e;
2011 	const char *name;
2012 
2013 	err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2014 			       &chip->pci->dev);
2015 	if (err >= 0) {
2016 		if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
2017 			dev_err(chip->card->dev,
2018 				"DSP microcode has wrong size\n");
2019 			err = -EINVAL;
2020 		}
2021 	}
2022 	if (err < 0)
2023 		return err;
2024 	is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2025 		chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2026 		chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2027 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2028 	name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2029 	err = request_firmware(&chip->controller_microcode, name,
2030 			       &chip->pci->dev);
2031 	if (err >= 0) {
2032 		if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
2033 			dev_err(chip->card->dev,
2034 				"controller microcode has wrong size\n");
2035 			err = -EINVAL;
2036 		}
2037 	}
2038 	if (err < 0)
2039 		return err;
2040 	return 0;
2041 }
2042 
2043 MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
2044 MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
2045 MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
2046 
2047 static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
2048 {
2049 	int i;
2050 	u16 ctrl;
2051 	const __le32 *inst;
2052 
2053 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
2054 	snd_ymfpci_disable_dsp(chip);
2055 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
2056 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
2057 	snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
2058 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
2059 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
2060 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
2061 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
2062 	ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2063 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2064 
2065 	/* setup DSP instruction code */
2066 	inst = (const __le32 *)chip->dsp_microcode->data;
2067 	for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2068 		snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2069 				  le32_to_cpu(inst[i]));
2070 
2071 	/* setup control instruction code */
2072 	inst = (const __le32 *)chip->controller_microcode->data;
2073 	for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2074 		snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2075 				  le32_to_cpu(inst[i]));
2076 
2077 	snd_ymfpci_enable_dsp(chip);
2078 }
2079 
2080 static int snd_ymfpci_memalloc(struct snd_ymfpci *chip)
2081 {
2082 	long size, playback_ctrl_size;
2083 	int voice, bank, reg;
2084 	u8 *ptr;
2085 	dma_addr_t ptr_addr;
2086 
2087 	playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
2088 	chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
2089 	chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
2090 	chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
2091 	chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
2092 
2093 	size = ALIGN(playback_ctrl_size, 0x100) +
2094 	       ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
2095 	       ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
2096 	       ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
2097 	       chip->work_size;
2098 	/* work_ptr must be aligned to 256 bytes, but it's already
2099 	   covered with the kernel page allocation mechanism */
2100 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
2101 				size, &chip->work_ptr) < 0)
2102 		return -ENOMEM;
2103 	ptr = chip->work_ptr.area;
2104 	ptr_addr = chip->work_ptr.addr;
2105 	memset(ptr, 0, size);	/* for sure */
2106 
2107 	chip->bank_base_playback = ptr;
2108 	chip->bank_base_playback_addr = ptr_addr;
2109 	chip->ctrl_playback = (__le32 *)ptr;
2110 	chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
2111 	ptr += ALIGN(playback_ctrl_size, 0x100);
2112 	ptr_addr += ALIGN(playback_ctrl_size, 0x100);
2113 	for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
2114 		chip->voices[voice].number = voice;
2115 		chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
2116 		chip->voices[voice].bank_addr = ptr_addr;
2117 		for (bank = 0; bank < 2; bank++) {
2118 			chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
2119 			ptr += chip->bank_size_playback;
2120 			ptr_addr += chip->bank_size_playback;
2121 		}
2122 	}
2123 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2124 	ptr_addr = ALIGN(ptr_addr, 0x100);
2125 	chip->bank_base_capture = ptr;
2126 	chip->bank_base_capture_addr = ptr_addr;
2127 	for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
2128 		for (bank = 0; bank < 2; bank++) {
2129 			chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
2130 			ptr += chip->bank_size_capture;
2131 			ptr_addr += chip->bank_size_capture;
2132 		}
2133 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2134 	ptr_addr = ALIGN(ptr_addr, 0x100);
2135 	chip->bank_base_effect = ptr;
2136 	chip->bank_base_effect_addr = ptr_addr;
2137 	for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
2138 		for (bank = 0; bank < 2; bank++) {
2139 			chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
2140 			ptr += chip->bank_size_effect;
2141 			ptr_addr += chip->bank_size_effect;
2142 		}
2143 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
2144 	ptr_addr = ALIGN(ptr_addr, 0x100);
2145 	chip->work_base = ptr;
2146 	chip->work_base_addr = ptr_addr;
2147 
2148 	snd_BUG_ON(ptr + chip->work_size !=
2149 		   chip->work_ptr.area + chip->work_ptr.bytes);
2150 
2151 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
2152 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
2153 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
2154 	snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
2155 	snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
2156 
2157 	/* S/PDIF output initialization */
2158 	chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
2159 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
2160 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
2161 
2162 	/* S/PDIF input initialization */
2163 	snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
2164 
2165 	/* digital mixer setup */
2166 	for (reg = 0x80; reg < 0xc0; reg += 4)
2167 		snd_ymfpci_writel(chip, reg, 0);
2168 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
2169 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
2170 	snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
2171 	snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
2172 	snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
2173 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
2174 	snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
2175 	snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
2176 
2177 	return 0;
2178 }
2179 
2180 static int snd_ymfpci_free(struct snd_ymfpci *chip)
2181 {
2182 	u16 ctrl;
2183 
2184 	if (snd_BUG_ON(!chip))
2185 		return -EINVAL;
2186 
2187 	if (chip->res_reg_area) {	/* don't touch busy hardware */
2188 		snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2189 		snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2190 		snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
2191 		snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
2192 		snd_ymfpci_disable_dsp(chip);
2193 		snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
2194 		snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
2195 		snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
2196 		snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
2197 		snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
2198 		ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
2199 		snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
2200 	}
2201 
2202 	snd_ymfpci_ac3_done(chip);
2203 
2204 	/* Set PCI device to D3 state */
2205 #if 0
2206 	/* FIXME: temporarily disabled, otherwise we cannot fire up
2207 	 * the chip again unless reboot.  ACPI bug?
2208 	 */
2209 	pci_set_power_state(chip->pci, PCI_D3hot);
2210 #endif
2211 
2212 #ifdef CONFIG_PM_SLEEP
2213 	kfree(chip->saved_regs);
2214 #endif
2215 	if (chip->irq >= 0)
2216 		free_irq(chip->irq, chip);
2217 	release_and_free_resource(chip->mpu_res);
2218 	release_and_free_resource(chip->fm_res);
2219 	snd_ymfpci_free_gameport(chip);
2220 	iounmap(chip->reg_area_virt);
2221 	if (chip->work_ptr.area)
2222 		snd_dma_free_pages(&chip->work_ptr);
2223 
2224 	release_and_free_resource(chip->res_reg_area);
2225 
2226 	pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
2227 
2228 	pci_disable_device(chip->pci);
2229 	release_firmware(chip->dsp_microcode);
2230 	release_firmware(chip->controller_microcode);
2231 	kfree(chip);
2232 	return 0;
2233 }
2234 
2235 static int snd_ymfpci_dev_free(struct snd_device *device)
2236 {
2237 	struct snd_ymfpci *chip = device->device_data;
2238 	return snd_ymfpci_free(chip);
2239 }
2240 
2241 #ifdef CONFIG_PM_SLEEP
2242 static int saved_regs_index[] = {
2243 	/* spdif */
2244 	YDSXGR_SPDIFOUTCTRL,
2245 	YDSXGR_SPDIFOUTSTATUS,
2246 	YDSXGR_SPDIFINCTRL,
2247 	/* volumes */
2248 	YDSXGR_PRIADCLOOPVOL,
2249 	YDSXGR_NATIVEDACINVOL,
2250 	YDSXGR_NATIVEDACOUTVOL,
2251 	YDSXGR_BUF441OUTVOL,
2252 	YDSXGR_NATIVEADCINVOL,
2253 	YDSXGR_SPDIFLOOPVOL,
2254 	YDSXGR_SPDIFOUTVOL,
2255 	YDSXGR_ZVOUTVOL,
2256 	YDSXGR_LEGACYOUTVOL,
2257 	/* address bases */
2258 	YDSXGR_PLAYCTRLBASE,
2259 	YDSXGR_RECCTRLBASE,
2260 	YDSXGR_EFFCTRLBASE,
2261 	YDSXGR_WORKBASE,
2262 	/* capture set up */
2263 	YDSXGR_MAPOFREC,
2264 	YDSXGR_RECFORMAT,
2265 	YDSXGR_RECSLOTSR,
2266 	YDSXGR_ADCFORMAT,
2267 	YDSXGR_ADCSLOTSR,
2268 };
2269 #define YDSXGR_NUM_SAVED_REGS	ARRAY_SIZE(saved_regs_index)
2270 
2271 static int snd_ymfpci_suspend(struct device *dev)
2272 {
2273 	struct snd_card *card = dev_get_drvdata(dev);
2274 	struct snd_ymfpci *chip = card->private_data;
2275 	unsigned int i;
2276 
2277 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2278 	snd_ac97_suspend(chip->ac97);
2279 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2280 		chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
2281 	chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2282 	pci_read_config_word(chip->pci, PCIR_DSXG_LEGACY,
2283 			     &chip->saved_dsxg_legacy);
2284 	pci_read_config_word(chip->pci, PCIR_DSXG_ELEGACY,
2285 			     &chip->saved_dsxg_elegacy);
2286 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
2287 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
2288 	snd_ymfpci_disable_dsp(chip);
2289 	return 0;
2290 }
2291 
2292 static int snd_ymfpci_resume(struct device *dev)
2293 {
2294 	struct pci_dev *pci = to_pci_dev(dev);
2295 	struct snd_card *card = dev_get_drvdata(dev);
2296 	struct snd_ymfpci *chip = card->private_data;
2297 	unsigned int i;
2298 
2299 	snd_ymfpci_aclink_reset(pci);
2300 	snd_ymfpci_codec_ready(chip, 0);
2301 	snd_ymfpci_download_image(chip);
2302 	udelay(100);
2303 
2304 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
2305 		snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
2306 
2307 	snd_ac97_resume(chip->ac97);
2308 
2309 	pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY,
2310 			      chip->saved_dsxg_legacy);
2311 	pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY,
2312 			      chip->saved_dsxg_elegacy);
2313 
2314 	/* start hw again */
2315 	if (chip->start_count > 0) {
2316 		spin_lock_irq(&chip->reg_lock);
2317 		snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
2318 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
2319 		spin_unlock_irq(&chip->reg_lock);
2320 	}
2321 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2322 	return 0;
2323 }
2324 
2325 SIMPLE_DEV_PM_OPS(snd_ymfpci_pm, snd_ymfpci_suspend, snd_ymfpci_resume);
2326 #endif /* CONFIG_PM_SLEEP */
2327 
2328 int snd_ymfpci_create(struct snd_card *card,
2329 		      struct pci_dev *pci,
2330 		      unsigned short old_legacy_ctrl,
2331 		      struct snd_ymfpci **rchip)
2332 {
2333 	struct snd_ymfpci *chip;
2334 	int err;
2335 	static struct snd_device_ops ops = {
2336 		.dev_free =	snd_ymfpci_dev_free,
2337 	};
2338 
2339 	*rchip = NULL;
2340 
2341 	/* enable PCI device */
2342 	if ((err = pci_enable_device(pci)) < 0)
2343 		return err;
2344 
2345 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
2346 	if (chip == NULL) {
2347 		pci_disable_device(pci);
2348 		return -ENOMEM;
2349 	}
2350 	chip->old_legacy_ctrl = old_legacy_ctrl;
2351 	spin_lock_init(&chip->reg_lock);
2352 	spin_lock_init(&chip->voice_lock);
2353 	init_waitqueue_head(&chip->interrupt_sleep);
2354 	atomic_set(&chip->interrupt_sleep_count, 0);
2355 	chip->card = card;
2356 	chip->pci = pci;
2357 	chip->irq = -1;
2358 	chip->device_id = pci->device;
2359 	chip->rev = pci->revision;
2360 	chip->reg_area_phys = pci_resource_start(pci, 0);
2361 	chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
2362 	pci_set_master(pci);
2363 	chip->src441_used = -1;
2364 
2365 	if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
2366 		dev_err(chip->card->dev,
2367 			"unable to grab memory region 0x%lx-0x%lx\n",
2368 			chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
2369 		err = -EBUSY;
2370 		goto free_chip;
2371 	}
2372 	if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2373 			KBUILD_MODNAME, chip)) {
2374 		dev_err(chip->card->dev, "unable to grab IRQ %d\n", pci->irq);
2375 		err = -EBUSY;
2376 		goto free_chip;
2377 	}
2378 	chip->irq = pci->irq;
2379 
2380 	snd_ymfpci_aclink_reset(pci);
2381 	if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2382 		err = -EIO;
2383 		goto free_chip;
2384 	}
2385 
2386 	err = snd_ymfpci_request_firmware(chip);
2387 	if (err < 0) {
2388 		dev_err(chip->card->dev, "firmware request failed: %d\n", err);
2389 		goto free_chip;
2390 	}
2391 	snd_ymfpci_download_image(chip);
2392 
2393 	udelay(100); /* seems we need a delay after downloading image.. */
2394 
2395 	if (snd_ymfpci_memalloc(chip) < 0) {
2396 		err = -EIO;
2397 		goto free_chip;
2398 	}
2399 
2400 	err = snd_ymfpci_ac3_init(chip);
2401 	if (err < 0)
2402 		goto free_chip;
2403 
2404 #ifdef CONFIG_PM_SLEEP
2405 	chip->saved_regs = kmalloc_array(YDSXGR_NUM_SAVED_REGS, sizeof(u32),
2406 					 GFP_KERNEL);
2407 	if (chip->saved_regs == NULL) {
2408 		err = -ENOMEM;
2409 		goto free_chip;
2410 	}
2411 #endif
2412 
2413 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
2414 	if (err < 0)
2415 		goto free_chip;
2416 
2417 	snd_ymfpci_proc_init(card, chip);
2418 
2419 	*rchip = chip;
2420 	return 0;
2421 
2422 free_chip:
2423 	snd_ymfpci_free(chip);
2424 	return err;
2425 }
2426