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