xref: /openbmc/linux/sound/pci/ymfpci/ymfpci_main.c (revision 28aa165cc52fa686a55a2a2052fdddad0fbde5eb)
11da177e4SLinus Torvalds /*
2c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
31da177e4SLinus Torvalds  *  Routines for control of YMF724/740/744/754 chips
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
61da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
71da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
81da177e4SLinus Torvalds  *   (at your option) any later version.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
131da177e4SLinus Torvalds  *   GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
161da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
171da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <linux/delay.h>
22102fa906SClemens Ladisch #include <linux/firmware.h>
231da177e4SLinus Torvalds #include <linux/init.h>
241da177e4SLinus Torvalds #include <linux/interrupt.h>
251da177e4SLinus Torvalds #include <linux/pci.h>
261da177e4SLinus Torvalds #include <linux/sched.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/vmalloc.h>
29b82a82d0SDavid Woodhouse #include <linux/mutex.h>
30da155d5bSPaul Gortmaker #include <linux/module.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <sound/core.h>
331da177e4SLinus Torvalds #include <sound/control.h>
341da177e4SLinus Torvalds #include <sound/info.h>
3533925186STakashi Iwai #include <sound/tlv.h>
361da177e4SLinus Torvalds #include <sound/ymfpci.h>
371da177e4SLinus Torvalds #include <sound/asoundef.h>
381da177e4SLinus Torvalds #include <sound/mpu401.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #include <asm/io.h>
41102fa906SClemens Ladisch #include <asm/byteorder.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /*
441da177e4SLinus Torvalds  *  common I/O routines
451da177e4SLinus Torvalds  */
461da177e4SLinus Torvalds 
47208a1b4cSTakashi Iwai static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
481da177e4SLinus Torvalds 
49208a1b4cSTakashi Iwai static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	return readb(chip->reg_area_virt + offset);
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds 
54208a1b4cSTakashi Iwai static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
551da177e4SLinus Torvalds {
561da177e4SLinus Torvalds 	writeb(val, chip->reg_area_virt + offset);
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
59208a1b4cSTakashi Iwai static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds 	return readw(chip->reg_area_virt + offset);
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
64208a1b4cSTakashi Iwai static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
651da177e4SLinus Torvalds {
661da177e4SLinus Torvalds 	writew(val, chip->reg_area_virt + offset);
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
69208a1b4cSTakashi Iwai static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds 	return readl(chip->reg_area_virt + offset);
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds 
74208a1b4cSTakashi Iwai static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds 	writel(val, chip->reg_area_virt + offset);
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
79208a1b4cSTakashi Iwai static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
801da177e4SLinus Torvalds {
81ef21ca24SNishanth Aravamudan 	unsigned long end_time;
821da177e4SLinus Torvalds 	u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
831da177e4SLinus Torvalds 
84ef21ca24SNishanth Aravamudan 	end_time = jiffies + msecs_to_jiffies(750);
851da177e4SLinus Torvalds 	do {
861da177e4SLinus Torvalds 		if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
871da177e4SLinus Torvalds 			return 0;
888433a509SNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
89ef21ca24SNishanth Aravamudan 	} while (time_before(jiffies, end_time));
9099b359baSTakashi Iwai 	snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg));
911da177e4SLinus Torvalds 	return -EBUSY;
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
94208a1b4cSTakashi Iwai static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
951da177e4SLinus Torvalds {
96208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
971da177e4SLinus Torvalds 	u32 cmd;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	snd_ymfpci_codec_ready(chip, 0);
1001da177e4SLinus Torvalds 	cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
1011da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
104208a1b4cSTakashi Iwai static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
1051da177e4SLinus Torvalds {
106208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0))
1091da177e4SLinus Torvalds 		return ~0;
1101da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
1111da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0))
1121da177e4SLinus Torvalds 		return ~0;
1131da177e4SLinus Torvalds 	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
1141da177e4SLinus Torvalds 		int i;
1151da177e4SLinus Torvalds 		for (i = 0; i < 600; i++)
1161da177e4SLinus Torvalds 			snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
1171da177e4SLinus Torvalds 	}
1181da177e4SLinus Torvalds 	return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds /*
1221da177e4SLinus Torvalds  *  Misc routines
1231da177e4SLinus Torvalds  */
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds static u32 snd_ymfpci_calc_delta(u32 rate)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	switch (rate) {
1281da177e4SLinus Torvalds 	case 8000:	return 0x02aaab00;
1291da177e4SLinus Torvalds 	case 11025:	return 0x03accd00;
1301da177e4SLinus Torvalds 	case 16000:	return 0x05555500;
1311da177e4SLinus Torvalds 	case 22050:	return 0x07599a00;
1321da177e4SLinus Torvalds 	case 32000:	return 0x0aaaab00;
1331da177e4SLinus Torvalds 	case 44100:	return 0x0eb33300;
1341da177e4SLinus Torvalds 	default:	return ((rate << 16) / 375) << 5;
1351da177e4SLinus Torvalds 	}
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds static u32 def_rate[8] = {
1391da177e4SLinus Torvalds 	100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
1401da177e4SLinus Torvalds };
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds static u32 snd_ymfpci_calc_lpfK(u32 rate)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	u32 i;
1451da177e4SLinus Torvalds 	static u32 val[8] = {
1461da177e4SLinus Torvalds 		0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
1471da177e4SLinus Torvalds 		0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
1481da177e4SLinus Torvalds 	};
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	if (rate == 44100)
1511da177e4SLinus Torvalds 		return 0x40000000;	/* FIXME: What's the right value? */
1521da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
1531da177e4SLinus Torvalds 		if (rate <= def_rate[i])
1541da177e4SLinus Torvalds 			return val[i];
1551da177e4SLinus Torvalds 	return val[0];
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds static u32 snd_ymfpci_calc_lpfQ(u32 rate)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	u32 i;
1611da177e4SLinus Torvalds 	static u32 val[8] = {
1621da177e4SLinus Torvalds 		0x35280000, 0x34A70000, 0x32020000, 0x31770000,
1631da177e4SLinus Torvalds 		0x31390000, 0x31C90000, 0x33D00000, 0x40000000
1641da177e4SLinus Torvalds 	};
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	if (rate == 44100)
1671da177e4SLinus Torvalds 		return 0x370A0000;
1681da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
1691da177e4SLinus Torvalds 		if (rate <= def_rate[i])
1701da177e4SLinus Torvalds 			return val[i];
1711da177e4SLinus Torvalds 	return val[0];
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds /*
1751da177e4SLinus Torvalds  *  Hardware start management
1761da177e4SLinus Torvalds  */
1771da177e4SLinus Torvalds 
178208a1b4cSTakashi Iwai static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds 	unsigned long flags;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
1831da177e4SLinus Torvalds 	if (chip->start_count++ > 0)
1841da177e4SLinus Torvalds 		goto __end;
1851da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE,
1861da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
1871da177e4SLinus Torvalds 	chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
1881da177e4SLinus Torvalds       __end:
1891da177e4SLinus Torvalds       	spin_unlock_irqrestore(&chip->reg_lock, flags);
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
192208a1b4cSTakashi Iwai static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	unsigned long flags;
1951da177e4SLinus Torvalds 	long timeout = 1000;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
1981da177e4SLinus Torvalds 	if (--chip->start_count > 0)
1991da177e4SLinus Torvalds 		goto __end;
2001da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE,
2011da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
2021da177e4SLinus Torvalds 	while (timeout-- > 0) {
2031da177e4SLinus Torvalds 		if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
2041da177e4SLinus Torvalds 			break;
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds 	if (atomic_read(&chip->interrupt_sleep_count)) {
2071da177e4SLinus Torvalds 		atomic_set(&chip->interrupt_sleep_count, 0);
2081da177e4SLinus Torvalds 		wake_up(&chip->interrupt_sleep);
2091da177e4SLinus Torvalds 	}
2101da177e4SLinus Torvalds       __end:
2111da177e4SLinus Torvalds       	spin_unlock_irqrestore(&chip->reg_lock, flags);
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds /*
2151da177e4SLinus Torvalds  *  Playback voice management
2161da177e4SLinus Torvalds  */
2171da177e4SLinus Torvalds 
218208a1b4cSTakashi Iwai static int voice_alloc(struct snd_ymfpci *chip,
219208a1b4cSTakashi Iwai 		       enum snd_ymfpci_voice_type type, int pair,
220208a1b4cSTakashi Iwai 		       struct snd_ymfpci_voice **rvoice)
2211da177e4SLinus Torvalds {
222208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice, *voice2;
2231da177e4SLinus Torvalds 	int idx;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	*rvoice = NULL;
2261da177e4SLinus Torvalds 	for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
2271da177e4SLinus Torvalds 		voice = &chip->voices[idx];
2281da177e4SLinus Torvalds 		voice2 = pair ? &chip->voices[idx+1] : NULL;
2291da177e4SLinus Torvalds 		if (voice->use || (voice2 && voice2->use))
2301da177e4SLinus Torvalds 			continue;
2311da177e4SLinus Torvalds 		voice->use = 1;
2321da177e4SLinus Torvalds 		if (voice2)
2331da177e4SLinus Torvalds 			voice2->use = 1;
2341da177e4SLinus Torvalds 		switch (type) {
2351da177e4SLinus Torvalds 		case YMFPCI_PCM:
2361da177e4SLinus Torvalds 			voice->pcm = 1;
2371da177e4SLinus Torvalds 			if (voice2)
2381da177e4SLinus Torvalds 				voice2->pcm = 1;
2391da177e4SLinus Torvalds 			break;
2401da177e4SLinus Torvalds 		case YMFPCI_SYNTH:
2411da177e4SLinus Torvalds 			voice->synth = 1;
2421da177e4SLinus Torvalds 			break;
2431da177e4SLinus Torvalds 		case YMFPCI_MIDI:
2441da177e4SLinus Torvalds 			voice->midi = 1;
2451da177e4SLinus Torvalds 			break;
2461da177e4SLinus Torvalds 		}
2471da177e4SLinus Torvalds 		snd_ymfpci_hw_start(chip);
2481da177e4SLinus Torvalds 		if (voice2)
2491da177e4SLinus Torvalds 			snd_ymfpci_hw_start(chip);
2501da177e4SLinus Torvalds 		*rvoice = voice;
2511da177e4SLinus Torvalds 		return 0;
2521da177e4SLinus Torvalds 	}
2531da177e4SLinus Torvalds 	return -ENOMEM;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
256208a1b4cSTakashi Iwai static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
257208a1b4cSTakashi Iwai 				  enum snd_ymfpci_voice_type type, int pair,
258208a1b4cSTakashi Iwai 				  struct snd_ymfpci_voice **rvoice)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	unsigned long flags;
2611da177e4SLinus Torvalds 	int result;
2621da177e4SLinus Torvalds 
263da3cec35STakashi Iwai 	if (snd_BUG_ON(!rvoice))
264da3cec35STakashi Iwai 		return -EINVAL;
265da3cec35STakashi Iwai 	if (snd_BUG_ON(pair && type != YMFPCI_PCM))
266da3cec35STakashi Iwai 		return -EINVAL;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->voice_lock, flags);
2691da177e4SLinus Torvalds 	for (;;) {
2701da177e4SLinus Torvalds 		result = voice_alloc(chip, type, pair, rvoice);
2711da177e4SLinus Torvalds 		if (result == 0 || type != YMFPCI_PCM)
2721da177e4SLinus Torvalds 			break;
2731da177e4SLinus Torvalds 		/* TODO: synth/midi voice deallocation */
2741da177e4SLinus Torvalds 		break;
2751da177e4SLinus Torvalds 	}
2761da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->voice_lock, flags);
2771da177e4SLinus Torvalds 	return result;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
280208a1b4cSTakashi Iwai static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	unsigned long flags;
2831da177e4SLinus Torvalds 
284da3cec35STakashi Iwai 	if (snd_BUG_ON(!pvoice))
285da3cec35STakashi Iwai 		return -EINVAL;
2861da177e4SLinus Torvalds 	snd_ymfpci_hw_stop(chip);
2871da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->voice_lock, flags);
2889ed1261eSTeru KAMOGASHIRA 	if (pvoice->number == chip->src441_used) {
2899ed1261eSTeru KAMOGASHIRA 		chip->src441_used = -1;
2909ed1261eSTeru KAMOGASHIRA 		pvoice->ypcm->use_441_slot = 0;
2919ed1261eSTeru KAMOGASHIRA 	}
2921da177e4SLinus Torvalds 	pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
2931da177e4SLinus Torvalds 	pvoice->ypcm = NULL;
2941da177e4SLinus Torvalds 	pvoice->interrupt = NULL;
2951da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->voice_lock, flags);
2961da177e4SLinus Torvalds 	return 0;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /*
3001da177e4SLinus Torvalds  *  PCM part
3011da177e4SLinus Torvalds  */
3021da177e4SLinus Torvalds 
303208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
3041da177e4SLinus Torvalds {
305208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
3061da177e4SLinus Torvalds 	u32 pos, delta;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	if ((ypcm = voice->ypcm) == NULL)
3091da177e4SLinus Torvalds 		return;
3101da177e4SLinus Torvalds 	if (ypcm->substream == NULL)
3111da177e4SLinus Torvalds 		return;
3121da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3131da177e4SLinus Torvalds 	if (ypcm->running) {
3141da177e4SLinus Torvalds 		pos = le32_to_cpu(voice->bank[chip->active_bank].start);
3151da177e4SLinus Torvalds 		if (pos < ypcm->last_pos)
3161da177e4SLinus Torvalds 			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
3171da177e4SLinus Torvalds 		else
3181da177e4SLinus Torvalds 			delta = pos - ypcm->last_pos;
3191da177e4SLinus Torvalds 		ypcm->period_pos += delta;
3201da177e4SLinus Torvalds 		ypcm->last_pos = pos;
3211da177e4SLinus Torvalds 		if (ypcm->period_pos >= ypcm->period_size) {
322ee419653STakashi Iwai 			/*
323ee419653STakashi Iwai 			printk(KERN_DEBUG
324ee419653STakashi Iwai 			       "done - active_bank = 0x%x, start = 0x%x\n",
325ee419653STakashi Iwai 			       chip->active_bank,
326ee419653STakashi Iwai 			       voice->bank[chip->active_bank].start);
327ee419653STakashi Iwai 			*/
3281da177e4SLinus Torvalds 			ypcm->period_pos %= ypcm->period_size;
3291da177e4SLinus Torvalds 			spin_unlock(&chip->reg_lock);
3301da177e4SLinus Torvalds 			snd_pcm_period_elapsed(ypcm->substream);
3311da177e4SLinus Torvalds 			spin_lock(&chip->reg_lock);
3321da177e4SLinus Torvalds 		}
3339bcf6551SClemens Ladisch 
3349bcf6551SClemens Ladisch 		if (unlikely(ypcm->update_pcm_vol)) {
3359bcf6551SClemens Ladisch 			unsigned int subs = ypcm->substream->number;
3369bcf6551SClemens Ladisch 			unsigned int next_bank = 1 - chip->active_bank;
337208a1b4cSTakashi Iwai 			struct snd_ymfpci_playback_bank *bank;
3389bcf6551SClemens Ladisch 			u32 volume;
3399bcf6551SClemens Ladisch 
3409bcf6551SClemens Ladisch 			bank = &voice->bank[next_bank];
3419bcf6551SClemens Ladisch 			volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
3429bcf6551SClemens Ladisch 			bank->left_gain_end = volume;
3439bcf6551SClemens Ladisch 			if (ypcm->output_rear)
3449bcf6551SClemens Ladisch 				bank->eff2_gain_end = volume;
3459bcf6551SClemens Ladisch 			if (ypcm->voices[1])
3469bcf6551SClemens Ladisch 				bank = &ypcm->voices[1]->bank[next_bank];
3479bcf6551SClemens Ladisch 			volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
3489bcf6551SClemens Ladisch 			bank->right_gain_end = volume;
3499bcf6551SClemens Ladisch 			if (ypcm->output_rear)
3509bcf6551SClemens Ladisch 				bank->eff3_gain_end = volume;
3519bcf6551SClemens Ladisch 			ypcm->update_pcm_vol--;
3529bcf6551SClemens Ladisch 		}
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
357208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
3581da177e4SLinus Torvalds {
359208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
360208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
361208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ypcm->chip;
3621da177e4SLinus Torvalds 	u32 pos, delta;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3651da177e4SLinus Torvalds 	if (ypcm->running) {
3661da177e4SLinus Torvalds 		pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
3671da177e4SLinus Torvalds 		if (pos < ypcm->last_pos)
3681da177e4SLinus Torvalds 			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
3691da177e4SLinus Torvalds 		else
3701da177e4SLinus Torvalds 			delta = pos - ypcm->last_pos;
3711da177e4SLinus Torvalds 		ypcm->period_pos += delta;
3721da177e4SLinus Torvalds 		ypcm->last_pos = pos;
3731da177e4SLinus Torvalds 		if (ypcm->period_pos >= ypcm->period_size) {
3741da177e4SLinus Torvalds 			ypcm->period_pos %= ypcm->period_size;
375ee419653STakashi Iwai 			/*
376ee419653STakashi Iwai 			printk(KERN_DEBUG
377ee419653STakashi Iwai 			       "done - active_bank = 0x%x, start = 0x%x\n",
378ee419653STakashi Iwai 			       chip->active_bank,
379ee419653STakashi Iwai 			       voice->bank[chip->active_bank].start);
380ee419653STakashi Iwai 			*/
3811da177e4SLinus Torvalds 			spin_unlock(&chip->reg_lock);
3821da177e4SLinus Torvalds 			snd_pcm_period_elapsed(substream);
3831da177e4SLinus Torvalds 			spin_lock(&chip->reg_lock);
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
389208a1b4cSTakashi Iwai static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
3901da177e4SLinus Torvalds 				       int cmd)
3911da177e4SLinus Torvalds {
392208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
393208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
394177a7cdbSClemens Ladisch 	struct snd_kcontrol *kctl = NULL;
3951da177e4SLinus Torvalds 	int result = 0;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3981da177e4SLinus Torvalds 	if (ypcm->voices[0] == NULL) {
3991da177e4SLinus Torvalds 		result = -EINVAL;
4001da177e4SLinus Torvalds 		goto __unlock;
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 	switch (cmd) {
4031da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4041da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
4051da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_RESUME:
4061da177e4SLinus Torvalds 		chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
4079ed1261eSTeru KAMOGASHIRA 		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
4081da177e4SLinus Torvalds 			chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
4091da177e4SLinus Torvalds 		ypcm->running = 1;
4101da177e4SLinus Torvalds 		break;
4111da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
412177a7cdbSClemens Ladisch 		if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
413177a7cdbSClemens Ladisch 			kctl = chip->pcm_mixer[substream->number].ctl;
414177a7cdbSClemens Ladisch 			kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
415177a7cdbSClemens Ladisch 		}
416177a7cdbSClemens Ladisch 		/* fall through */
4171da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
4181da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_SUSPEND:
4191da177e4SLinus Torvalds 		chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
4209ed1261eSTeru KAMOGASHIRA 		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
4211da177e4SLinus Torvalds 			chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
4221da177e4SLinus Torvalds 		ypcm->running = 0;
4231da177e4SLinus Torvalds 		break;
4241da177e4SLinus Torvalds 	default:
4251da177e4SLinus Torvalds 		result = -EINVAL;
4261da177e4SLinus Torvalds 		break;
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds       __unlock:
4291da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
430177a7cdbSClemens Ladisch 	if (kctl)
431177a7cdbSClemens Ladisch 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
4321da177e4SLinus Torvalds 	return result;
4331da177e4SLinus Torvalds }
434208a1b4cSTakashi Iwai static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
4351da177e4SLinus Torvalds 				      int cmd)
4361da177e4SLinus Torvalds {
437208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
438208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
4391da177e4SLinus Torvalds 	int result = 0;
4401da177e4SLinus Torvalds 	u32 tmp;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
4431da177e4SLinus Torvalds 	switch (cmd) {
4441da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4451da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
4461da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_RESUME:
4471da177e4SLinus Torvalds 		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
4481da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
4491da177e4SLinus Torvalds 		ypcm->running = 1;
4501da177e4SLinus Torvalds 		break;
4511da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4521da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
4531da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_SUSPEND:
4541da177e4SLinus Torvalds 		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
4551da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
4561da177e4SLinus Torvalds 		ypcm->running = 0;
4571da177e4SLinus Torvalds 		break;
4581da177e4SLinus Torvalds 	default:
4591da177e4SLinus Torvalds 		result = -EINVAL;
4601da177e4SLinus Torvalds 		break;
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4631da177e4SLinus Torvalds 	return result;
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
466208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
4671da177e4SLinus Torvalds {
4681da177e4SLinus Torvalds 	int err;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	if (ypcm->voices[1] != NULL && voices < 2) {
4711da177e4SLinus Torvalds 		snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
4721da177e4SLinus Torvalds 		ypcm->voices[1] = NULL;
4731da177e4SLinus Torvalds 	}
4741da177e4SLinus Torvalds 	if (voices == 1 && ypcm->voices[0] != NULL)
4751da177e4SLinus Torvalds 		return 0;		/* already allocated */
4761da177e4SLinus Torvalds 	if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
4771da177e4SLinus Torvalds 		return 0;		/* already allocated */
4781da177e4SLinus Torvalds 	if (voices > 1) {
4791da177e4SLinus Torvalds 		if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
4801da177e4SLinus Torvalds 			snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
4811da177e4SLinus Torvalds 			ypcm->voices[0] = NULL;
4821da177e4SLinus Torvalds 		}
4831da177e4SLinus Torvalds 	}
4841da177e4SLinus Torvalds 	err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
4851da177e4SLinus Torvalds 	if (err < 0)
4861da177e4SLinus Torvalds 		return err;
4871da177e4SLinus Torvalds 	ypcm->voices[0]->ypcm = ypcm;
4881da177e4SLinus Torvalds 	ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
4891da177e4SLinus Torvalds 	if (voices > 1) {
4901da177e4SLinus Torvalds 		ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
4911da177e4SLinus Torvalds 		ypcm->voices[1]->ypcm = ypcm;
4921da177e4SLinus Torvalds 	}
4931da177e4SLinus Torvalds 	return 0;
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
496208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
497208a1b4cSTakashi Iwai 				      struct snd_pcm_runtime *runtime,
4989bcf6551SClemens Ladisch 				      int has_pcm_volume)
4991da177e4SLinus Torvalds {
500208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
5011da177e4SLinus Torvalds 	u32 format;
5029bcf6551SClemens Ladisch 	u32 delta = snd_ymfpci_calc_delta(runtime->rate);
5039bcf6551SClemens Ladisch 	u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
5049bcf6551SClemens Ladisch 	u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
505208a1b4cSTakashi Iwai 	struct snd_ymfpci_playback_bank *bank;
5061da177e4SLinus Torvalds 	unsigned int nbank;
5079bcf6551SClemens Ladisch 	u32 vol_left, vol_right;
5089bcf6551SClemens Ladisch 	u8 use_left, use_right;
5099ed1261eSTeru KAMOGASHIRA 	unsigned long flags;
5101da177e4SLinus Torvalds 
511da3cec35STakashi Iwai 	if (snd_BUG_ON(!voice))
512da3cec35STakashi Iwai 		return;
5139bcf6551SClemens Ladisch 	if (runtime->channels == 1) {
5149bcf6551SClemens Ladisch 		use_left = 1;
5159bcf6551SClemens Ladisch 		use_right = 1;
5169bcf6551SClemens Ladisch 	} else {
5179bcf6551SClemens Ladisch 		use_left = (voiceidx & 1) == 0;
5189bcf6551SClemens Ladisch 		use_right = !use_left;
5199bcf6551SClemens Ladisch 	}
5209bcf6551SClemens Ladisch 	if (has_pcm_volume) {
5219bcf6551SClemens Ladisch 		vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
5229bcf6551SClemens Ladisch 				       [ypcm->substream->number].left << 15);
5239bcf6551SClemens Ladisch 		vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
5249bcf6551SClemens Ladisch 					[ypcm->substream->number].right << 15);
5259bcf6551SClemens Ladisch 	} else {
5269bcf6551SClemens Ladisch 		vol_left = cpu_to_le32(0x40000000);
5279bcf6551SClemens Ladisch 		vol_right = cpu_to_le32(0x40000000);
5289bcf6551SClemens Ladisch 	}
5299ed1261eSTeru KAMOGASHIRA 	spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
5309bcf6551SClemens Ladisch 	format = runtime->channels == 2 ? 0x00010000 : 0;
5319bcf6551SClemens Ladisch 	if (snd_pcm_format_width(runtime->format) == 8)
5329bcf6551SClemens Ladisch 		format |= 0x80000000;
5339ed1261eSTeru KAMOGASHIRA 	else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
5349ed1261eSTeru KAMOGASHIRA 		 runtime->rate == 44100 && runtime->channels == 2 &&
5359ed1261eSTeru KAMOGASHIRA 		 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
5369ed1261eSTeru KAMOGASHIRA 				   ypcm->chip->src441_used == voice->number)) {
5379ed1261eSTeru KAMOGASHIRA 		ypcm->chip->src441_used = voice->number;
5389ed1261eSTeru KAMOGASHIRA 		ypcm->use_441_slot = 1;
5399ed1261eSTeru KAMOGASHIRA 		format |= 0x10000000;
5409ed1261eSTeru KAMOGASHIRA 	}
5419ed1261eSTeru KAMOGASHIRA 	if (ypcm->chip->src441_used == voice->number &&
5429ed1261eSTeru KAMOGASHIRA 	    (format & 0x10000000) == 0) {
5439ed1261eSTeru KAMOGASHIRA 		ypcm->chip->src441_used = -1;
5449ed1261eSTeru KAMOGASHIRA 		ypcm->use_441_slot = 0;
5459ed1261eSTeru KAMOGASHIRA 	}
5469bcf6551SClemens Ladisch 	if (runtime->channels == 2 && (voiceidx & 1) != 0)
5479bcf6551SClemens Ladisch 		format |= 1;
5489ed1261eSTeru KAMOGASHIRA 	spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
5491da177e4SLinus Torvalds 	for (nbank = 0; nbank < 2; nbank++) {
5501da177e4SLinus Torvalds 		bank = &voice->bank[nbank];
5519bcf6551SClemens Ladisch 		memset(bank, 0, sizeof(*bank));
5521da177e4SLinus Torvalds 		bank->format = cpu_to_le32(format);
5539bcf6551SClemens Ladisch 		bank->base = cpu_to_le32(runtime->dma_addr);
5549bcf6551SClemens Ladisch 		bank->loop_end = cpu_to_le32(ypcm->buffer_size);
5551da177e4SLinus Torvalds 		bank->lpfQ = cpu_to_le32(lpfQ);
5561da177e4SLinus Torvalds 		bank->delta =
5571da177e4SLinus Torvalds 		bank->delta_end = cpu_to_le32(delta);
5581da177e4SLinus Torvalds 		bank->lpfK =
5591da177e4SLinus Torvalds 		bank->lpfK_end = cpu_to_le32(lpfK);
5609bcf6551SClemens Ladisch 		bank->eg_gain =
5619bcf6551SClemens Ladisch 		bank->eg_gain_end = cpu_to_le32(0x40000000);
5621da177e4SLinus Torvalds 
5639bcf6551SClemens Ladisch 		if (ypcm->output_front) {
5649bcf6551SClemens Ladisch 			if (use_left) {
5651da177e4SLinus Torvalds 				bank->left_gain =
5669bcf6551SClemens Ladisch 				bank->left_gain_end = vol_left;
5679bcf6551SClemens Ladisch 			}
5689bcf6551SClemens Ladisch 			if (use_right) {
5691da177e4SLinus Torvalds 				bank->right_gain =
5709bcf6551SClemens Ladisch 				bank->right_gain_end = vol_right;
5719bcf6551SClemens Ladisch 			}
5729bcf6551SClemens Ladisch 		}
5739bcf6551SClemens Ladisch 		if (ypcm->output_rear) {
5745a25c5cfSJaroslav Kysela 		        if (!ypcm->swap_rear) {
5755a25c5cfSJaroslav Kysela         			if (use_left) {
5765a25c5cfSJaroslav Kysela         				bank->eff2_gain =
5775a25c5cfSJaroslav Kysela         				bank->eff2_gain_end = vol_left;
5785a25c5cfSJaroslav Kysela         			}
5795a25c5cfSJaroslav Kysela         			if (use_right) {
5805a25c5cfSJaroslav Kysela         				bank->eff3_gain =
5815a25c5cfSJaroslav Kysela         				bank->eff3_gain_end = vol_right;
5825a25c5cfSJaroslav Kysela         			}
5835a25c5cfSJaroslav Kysela 		        } else {
584153abaa4SClemens Ladisch         			/* The SPDIF out channels seem to be swapped, so we have
585153abaa4SClemens Ladisch         			 * to swap them here, too.  The rear analog out channels
586153abaa4SClemens Ladisch         			 * will be wrong, but otherwise AC3 would not work.
587153abaa4SClemens Ladisch         			 */
5889bcf6551SClemens Ladisch         			if (use_left) {
589153abaa4SClemens Ladisch         				bank->eff3_gain =
590153abaa4SClemens Ladisch         				bank->eff3_gain_end = vol_left;
5919bcf6551SClemens Ladisch         			}
5929bcf6551SClemens Ladisch         			if (use_right) {
593153abaa4SClemens Ladisch         				bank->eff2_gain =
594153abaa4SClemens Ladisch         				bank->eff2_gain_end = vol_right;
5951da177e4SLinus Torvalds         			}
5961da177e4SLinus Torvalds         		}
5971da177e4SLinus Torvalds                 }
5981da177e4SLinus Torvalds 	}
5995a25c5cfSJaroslav Kysela }
6001da177e4SLinus Torvalds 
601208a1b4cSTakashi Iwai static int __devinit snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
6021da177e4SLinus Torvalds {
6031da177e4SLinus Torvalds 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
6041da177e4SLinus Torvalds 				4096, &chip->ac3_tmp_base) < 0)
6051da177e4SLinus Torvalds 		return -ENOMEM;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	chip->bank_effect[3][0]->base =
6081da177e4SLinus Torvalds 	chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
6091da177e4SLinus Torvalds 	chip->bank_effect[3][0]->loop_end =
6101da177e4SLinus Torvalds 	chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
6111da177e4SLinus Torvalds 	chip->bank_effect[4][0]->base =
6121da177e4SLinus Torvalds 	chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
6131da177e4SLinus Torvalds 	chip->bank_effect[4][0]->loop_end =
6141da177e4SLinus Torvalds 	chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
6171da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
6181da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
6191da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
6201da177e4SLinus Torvalds 	return 0;
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
623208a1b4cSTakashi Iwai static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
6241da177e4SLinus Torvalds {
6251da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
6261da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
6271da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
6281da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
6291da177e4SLinus Torvalds 	// snd_ymfpci_irq_wait(chip);
6301da177e4SLinus Torvalds 	if (chip->ac3_tmp_base.area) {
6311da177e4SLinus Torvalds 		snd_dma_free_pages(&chip->ac3_tmp_base);
6321da177e4SLinus Torvalds 		chip->ac3_tmp_base.area = NULL;
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds 	return 0;
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
637208a1b4cSTakashi Iwai static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
638208a1b4cSTakashi Iwai 					 struct snd_pcm_hw_params *hw_params)
6391da177e4SLinus Torvalds {
640208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
641208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
6421da177e4SLinus Torvalds 	int err;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
6451da177e4SLinus Torvalds 		return err;
6461da177e4SLinus Torvalds 	if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
6471da177e4SLinus Torvalds 		return err;
6481da177e4SLinus Torvalds 	return 0;
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
651208a1b4cSTakashi Iwai static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
6521da177e4SLinus Torvalds {
653208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
654208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
655208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	if (runtime->private_data == NULL)
6581da177e4SLinus Torvalds 		return 0;
6591da177e4SLinus Torvalds 	ypcm = runtime->private_data;
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	/* wait, until the PCI operations are not finished */
6621da177e4SLinus Torvalds 	snd_ymfpci_irq_wait(chip);
6631da177e4SLinus Torvalds 	snd_pcm_lib_free_pages(substream);
6641da177e4SLinus Torvalds 	if (ypcm->voices[1]) {
6651da177e4SLinus Torvalds 		snd_ymfpci_voice_free(chip, ypcm->voices[1]);
6661da177e4SLinus Torvalds 		ypcm->voices[1] = NULL;
6671da177e4SLinus Torvalds 	}
6681da177e4SLinus Torvalds 	if (ypcm->voices[0]) {
6691da177e4SLinus Torvalds 		snd_ymfpci_voice_free(chip, ypcm->voices[0]);
6701da177e4SLinus Torvalds 		ypcm->voices[0] = NULL;
6711da177e4SLinus Torvalds 	}
6721da177e4SLinus Torvalds 	return 0;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
675208a1b4cSTakashi Iwai static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
6761da177e4SLinus Torvalds {
677208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
678208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
679208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
680177a7cdbSClemens Ladisch 	struct snd_kcontrol *kctl;
6811da177e4SLinus Torvalds 	unsigned int nvoice;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	ypcm->period_size = runtime->period_size;
6841da177e4SLinus Torvalds 	ypcm->buffer_size = runtime->buffer_size;
6851da177e4SLinus Torvalds 	ypcm->period_pos = 0;
6861da177e4SLinus Torvalds 	ypcm->last_pos = 0;
6871da177e4SLinus Torvalds 	for (nvoice = 0; nvoice < runtime->channels; nvoice++)
6889bcf6551SClemens Ladisch 		snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
6899bcf6551SClemens Ladisch 					  substream->pcm == chip->pcm);
690177a7cdbSClemens Ladisch 
691177a7cdbSClemens Ladisch 	if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
692177a7cdbSClemens Ladisch 		kctl = chip->pcm_mixer[substream->number].ctl;
693177a7cdbSClemens Ladisch 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
694177a7cdbSClemens Ladisch 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
695177a7cdbSClemens Ladisch 	}
6961da177e4SLinus Torvalds 	return 0;
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds 
699208a1b4cSTakashi Iwai static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
700208a1b4cSTakashi Iwai 					struct snd_pcm_hw_params *hw_params)
7011da177e4SLinus Torvalds {
7021da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
705208a1b4cSTakashi Iwai static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
7061da177e4SLinus Torvalds {
707208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	/* wait, until the PCI operations are not finished */
7101da177e4SLinus Torvalds 	snd_ymfpci_irq_wait(chip);
7111da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
714208a1b4cSTakashi Iwai static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
7151da177e4SLinus Torvalds {
716208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
717208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
718208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
719208a1b4cSTakashi Iwai 	struct snd_ymfpci_capture_bank * bank;
7201da177e4SLinus Torvalds 	int nbank;
7211da177e4SLinus Torvalds 	u32 rate, format;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	ypcm->period_size = runtime->period_size;
7241da177e4SLinus Torvalds 	ypcm->buffer_size = runtime->buffer_size;
7251da177e4SLinus Torvalds 	ypcm->period_pos = 0;
7261da177e4SLinus Torvalds 	ypcm->last_pos = 0;
7271da177e4SLinus Torvalds 	ypcm->shift = 0;
7281da177e4SLinus Torvalds 	rate = ((48000 * 4096) / runtime->rate) - 1;
7291da177e4SLinus Torvalds 	format = 0;
7301da177e4SLinus Torvalds 	if (runtime->channels == 2) {
7311da177e4SLinus Torvalds 		format |= 2;
7321da177e4SLinus Torvalds 		ypcm->shift++;
7331da177e4SLinus Torvalds 	}
7341da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 8)
7351da177e4SLinus Torvalds 		format |= 1;
7361da177e4SLinus Torvalds 	else
7371da177e4SLinus Torvalds 		ypcm->shift++;
7381da177e4SLinus Torvalds 	switch (ypcm->capture_bank_number) {
7391da177e4SLinus Torvalds 	case 0:
7401da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
7411da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
7421da177e4SLinus Torvalds 		break;
7431da177e4SLinus Torvalds 	case 1:
7441da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
7451da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
7461da177e4SLinus Torvalds 		break;
7471da177e4SLinus Torvalds 	}
7481da177e4SLinus Torvalds 	for (nbank = 0; nbank < 2; nbank++) {
7491da177e4SLinus Torvalds 		bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
7501da177e4SLinus Torvalds 		bank->base = cpu_to_le32(runtime->dma_addr);
7511da177e4SLinus Torvalds 		bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
7521da177e4SLinus Torvalds 		bank->start = 0;
7531da177e4SLinus Torvalds 		bank->num_of_loops = 0;
7541da177e4SLinus Torvalds 	}
7551da177e4SLinus Torvalds 	return 0;
7561da177e4SLinus Torvalds }
7571da177e4SLinus Torvalds 
758208a1b4cSTakashi Iwai static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
7591da177e4SLinus Torvalds {
760208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
761208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
762208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
763208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice = ypcm->voices[0];
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	if (!(ypcm->running && voice))
7661da177e4SLinus Torvalds 		return 0;
7671da177e4SLinus Torvalds 	return le32_to_cpu(voice->bank[chip->active_bank].start);
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
770208a1b4cSTakashi Iwai static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
7711da177e4SLinus Torvalds {
772208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
773208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
774208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	if (!ypcm->running)
7771da177e4SLinus Torvalds 		return 0;
7781da177e4SLinus Torvalds 	return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
781208a1b4cSTakashi Iwai static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
7821da177e4SLinus Torvalds {
7831da177e4SLinus Torvalds 	wait_queue_t wait;
7841da177e4SLinus Torvalds 	int loops = 4;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	while (loops-- > 0) {
7871da177e4SLinus Torvalds 		if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
7881da177e4SLinus Torvalds 		 	continue;
7891da177e4SLinus Torvalds 		init_waitqueue_entry(&wait, current);
7901da177e4SLinus Torvalds 		add_wait_queue(&chip->interrupt_sleep, &wait);
7911da177e4SLinus Torvalds 		atomic_inc(&chip->interrupt_sleep_count);
7928433a509SNishanth Aravamudan 		schedule_timeout_uninterruptible(msecs_to_jiffies(50));
7931da177e4SLinus Torvalds 		remove_wait_queue(&chip->interrupt_sleep, &wait);
7941da177e4SLinus Torvalds 	}
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
7977d12e780SDavid Howells static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
7981da177e4SLinus Torvalds {
799208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = dev_id;
8001da177e4SLinus Torvalds 	u32 status, nvoice, mode;
801208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
8041da177e4SLinus Torvalds 	if (status & 0x80000000) {
8051da177e4SLinus Torvalds 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
8061da177e4SLinus Torvalds 		spin_lock(&chip->voice_lock);
8071da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
8081da177e4SLinus Torvalds 			voice = &chip->voices[nvoice];
8091da177e4SLinus Torvalds 			if (voice->interrupt)
8101da177e4SLinus Torvalds 				voice->interrupt(chip, voice);
8111da177e4SLinus Torvalds 		}
8121da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
8131da177e4SLinus Torvalds 			if (chip->capture_substream[nvoice])
8141da177e4SLinus Torvalds 				snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
8151da177e4SLinus Torvalds 		}
8161da177e4SLinus Torvalds #if 0
8171da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
8181da177e4SLinus Torvalds 			if (chip->effect_substream[nvoice])
8191da177e4SLinus Torvalds 				snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
8201da177e4SLinus Torvalds 		}
8211da177e4SLinus Torvalds #endif
8221da177e4SLinus Torvalds 		spin_unlock(&chip->voice_lock);
8231da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
8241da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
8251da177e4SLinus Torvalds 		mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
8261da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
8271da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 		if (atomic_read(&chip->interrupt_sleep_count)) {
8301da177e4SLinus Torvalds 			atomic_set(&chip->interrupt_sleep_count, 0);
8311da177e4SLinus Torvalds 			wake_up(&chip->interrupt_sleep);
8321da177e4SLinus Torvalds 		}
8331da177e4SLinus Torvalds 	}
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
8361da177e4SLinus Torvalds 	if (status & 1) {
8371da177e4SLinus Torvalds 		if (chip->timer)
8386e2efaacSClemens Ladisch 			snd_timer_interrupt(chip->timer, chip->timer_ticks);
8391da177e4SLinus Torvalds 	}
8401da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	if (chip->rawmidi)
8437d12e780SDavid Howells 		snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
8441da177e4SLinus Torvalds 	return IRQ_HANDLED;
8451da177e4SLinus Torvalds }
8461da177e4SLinus Torvalds 
847208a1b4cSTakashi Iwai static struct snd_pcm_hardware snd_ymfpci_playback =
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
8501da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
8511da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_INTERLEAVED |
8521da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
8531da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE |
8541da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_RESUME),
8551da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
8561da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8571da177e4SLinus Torvalds 	.rate_min =		8000,
8581da177e4SLinus Torvalds 	.rate_max =		48000,
8591da177e4SLinus Torvalds 	.channels_min =		1,
8601da177e4SLinus Torvalds 	.channels_max =		2,
8611da177e4SLinus Torvalds 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
8621da177e4SLinus Torvalds 	.period_bytes_min =	64,
8631da177e4SLinus Torvalds 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
8641da177e4SLinus Torvalds 	.periods_min =		3,
8651da177e4SLinus Torvalds 	.periods_max =		1024,
8661da177e4SLinus Torvalds 	.fifo_size =		0,
8671da177e4SLinus Torvalds };
8681da177e4SLinus Torvalds 
869208a1b4cSTakashi Iwai static struct snd_pcm_hardware snd_ymfpci_capture =
8701da177e4SLinus Torvalds {
8711da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
8721da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
8731da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_INTERLEAVED |
8741da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
8751da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE |
8761da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_RESUME),
8771da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
8781da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8791da177e4SLinus Torvalds 	.rate_min =		8000,
8801da177e4SLinus Torvalds 	.rate_max =		48000,
8811da177e4SLinus Torvalds 	.channels_min =		1,
8821da177e4SLinus Torvalds 	.channels_max =		2,
8831da177e4SLinus Torvalds 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
8841da177e4SLinus Torvalds 	.period_bytes_min =	64,
8851da177e4SLinus Torvalds 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
8861da177e4SLinus Torvalds 	.periods_min =		3,
8871da177e4SLinus Torvalds 	.periods_max =		1024,
8881da177e4SLinus Torvalds 	.fifo_size =		0,
8891da177e4SLinus Torvalds };
8901da177e4SLinus Torvalds 
891208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
8921da177e4SLinus Torvalds {
8934d572776SJesper Juhl 	kfree(runtime->private_data);
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
896208a1b4cSTakashi Iwai static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
8971da177e4SLinus Torvalds {
898208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
899208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
900208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
90184f9df15SClemens Ladisch 	int err;
90284f9df15SClemens Ladisch 
90384f9df15SClemens Ladisch 	runtime->hw = snd_ymfpci_playback;
90484f9df15SClemens Ladisch 	/* FIXME? True value is 256/48 = 5.33333 ms */
90584f9df15SClemens Ladisch 	err = snd_pcm_hw_constraint_minmax(runtime,
90684f9df15SClemens Ladisch 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
90784f9df15SClemens Ladisch 					   5334, UINT_MAX);
90884f9df15SClemens Ladisch 	if (err < 0)
90984f9df15SClemens Ladisch 		return err;
9105b0416a3SClemens Ladisch 	err = snd_pcm_hw_rule_noresample(runtime, 48000);
9115b0416a3SClemens Ladisch 	if (err < 0)
9125b0416a3SClemens Ladisch 		return err;
9131da177e4SLinus Torvalds 
914e560d8d8STakashi Iwai 	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
9151da177e4SLinus Torvalds 	if (ypcm == NULL)
9161da177e4SLinus Torvalds 		return -ENOMEM;
9171da177e4SLinus Torvalds 	ypcm->chip = chip;
9181da177e4SLinus Torvalds 	ypcm->type = PLAYBACK_VOICE;
9191da177e4SLinus Torvalds 	ypcm->substream = substream;
9201da177e4SLinus Torvalds 	runtime->private_data = ypcm;
9211da177e4SLinus Torvalds 	runtime->private_free = snd_ymfpci_pcm_free_substream;
9221da177e4SLinus Torvalds 	return 0;
9231da177e4SLinus Torvalds }
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds /* call with spinlock held */
926208a1b4cSTakashi Iwai static void ymfpci_open_extension(struct snd_ymfpci *chip)
9271da177e4SLinus Torvalds {
9281da177e4SLinus Torvalds 	if (! chip->rear_opened) {
9291da177e4SLinus Torvalds 		if (! chip->spdif_opened) /* set AC3 */
9301da177e4SLinus Torvalds 			snd_ymfpci_writel(chip, YDSXGR_MODE,
9311da177e4SLinus Torvalds 					  snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
9321da177e4SLinus Torvalds 		/* enable second codec (4CHEN) */
9331da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
9341da177e4SLinus Torvalds 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
9351da177e4SLinus Torvalds 	}
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds /* call with spinlock held */
939208a1b4cSTakashi Iwai static void ymfpci_close_extension(struct snd_ymfpci *chip)
9401da177e4SLinus Torvalds {
9411da177e4SLinus Torvalds 	if (! chip->rear_opened) {
9421da177e4SLinus Torvalds 		if (! chip->spdif_opened)
9431da177e4SLinus Torvalds 			snd_ymfpci_writel(chip, YDSXGR_MODE,
9441da177e4SLinus Torvalds 					  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
9451da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
9461da177e4SLinus Torvalds 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
9471da177e4SLinus Torvalds 	}
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
950208a1b4cSTakashi Iwai static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
9511da177e4SLinus Torvalds {
952208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
953208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
954208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
9551da177e4SLinus Torvalds 	int err;
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
9581da177e4SLinus Torvalds 		return err;
9591da177e4SLinus Torvalds 	ypcm = runtime->private_data;
9601da177e4SLinus Torvalds 	ypcm->output_front = 1;
9611da177e4SLinus Torvalds 	ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
962d9301263SGlen Masgai 	ypcm->swap_rear = 0;
9631da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9641da177e4SLinus Torvalds 	if (ypcm->output_rear) {
9651da177e4SLinus Torvalds 		ymfpci_open_extension(chip);
9661da177e4SLinus Torvalds 		chip->rear_opened++;
9671da177e4SLinus Torvalds 	}
9681da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9691da177e4SLinus Torvalds 	return 0;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
972208a1b4cSTakashi Iwai static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
9731da177e4SLinus Torvalds {
974208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
975208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
976208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
9771da177e4SLinus Torvalds 	int err;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
9801da177e4SLinus Torvalds 		return err;
9811da177e4SLinus Torvalds 	ypcm = runtime->private_data;
9821da177e4SLinus Torvalds 	ypcm->output_front = 0;
9831da177e4SLinus Torvalds 	ypcm->output_rear = 1;
984d9301263SGlen Masgai 	ypcm->swap_rear = 1;
9851da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9861da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
9871da177e4SLinus Torvalds 			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
9881da177e4SLinus Torvalds 	ymfpci_open_extension(chip);
9891da177e4SLinus Torvalds 	chip->spdif_pcm_bits = chip->spdif_bits;
9901da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
9911da177e4SLinus Torvalds 	chip->spdif_opened++;
9921da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
9951da177e4SLinus Torvalds 	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
9961da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
9971da177e4SLinus Torvalds 	return 0;
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
1000208a1b4cSTakashi Iwai static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
10011da177e4SLinus Torvalds {
1002208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1003208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
1004208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
10051da177e4SLinus Torvalds 	int err;
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
10081da177e4SLinus Torvalds 		return err;
10091da177e4SLinus Torvalds 	ypcm = runtime->private_data;
10101da177e4SLinus Torvalds 	ypcm->output_front = 0;
10111da177e4SLinus Torvalds 	ypcm->output_rear = 1;
1012d9301263SGlen Masgai 	ypcm->swap_rear = 0;
10131da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10141da177e4SLinus Torvalds 	ymfpci_open_extension(chip);
10151da177e4SLinus Torvalds 	chip->rear_opened++;
10161da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10171da177e4SLinus Torvalds 	return 0;
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
1020208a1b4cSTakashi Iwai static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
10211da177e4SLinus Torvalds 				   u32 capture_bank_number)
10221da177e4SLinus Torvalds {
1023208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1024208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
1025208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
102684f9df15SClemens Ladisch 	int err;
102784f9df15SClemens Ladisch 
102884f9df15SClemens Ladisch 	runtime->hw = snd_ymfpci_capture;
102984f9df15SClemens Ladisch 	/* FIXME? True value is 256/48 = 5.33333 ms */
103084f9df15SClemens Ladisch 	err = snd_pcm_hw_constraint_minmax(runtime,
103184f9df15SClemens Ladisch 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
103284f9df15SClemens Ladisch 					   5334, UINT_MAX);
103384f9df15SClemens Ladisch 	if (err < 0)
103484f9df15SClemens Ladisch 		return err;
10355b0416a3SClemens Ladisch 	err = snd_pcm_hw_rule_noresample(runtime, 48000);
10365b0416a3SClemens Ladisch 	if (err < 0)
10375b0416a3SClemens Ladisch 		return err;
10381da177e4SLinus Torvalds 
1039e560d8d8STakashi Iwai 	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
10401da177e4SLinus Torvalds 	if (ypcm == NULL)
10411da177e4SLinus Torvalds 		return -ENOMEM;
10421da177e4SLinus Torvalds 	ypcm->chip = chip;
10431da177e4SLinus Torvalds 	ypcm->type = capture_bank_number + CAPTURE_REC;
10441da177e4SLinus Torvalds 	ypcm->substream = substream;
10451da177e4SLinus Torvalds 	ypcm->capture_bank_number = capture_bank_number;
10461da177e4SLinus Torvalds 	chip->capture_substream[capture_bank_number] = substream;
10471da177e4SLinus Torvalds 	runtime->private_data = ypcm;
10481da177e4SLinus Torvalds 	runtime->private_free = snd_ymfpci_pcm_free_substream;
10491da177e4SLinus Torvalds 	snd_ymfpci_hw_start(chip);
10501da177e4SLinus Torvalds 	return 0;
10511da177e4SLinus Torvalds }
10521da177e4SLinus Torvalds 
1053208a1b4cSTakashi Iwai static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
10541da177e4SLinus Torvalds {
10551da177e4SLinus Torvalds 	return snd_ymfpci_capture_open(substream, 0);
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
1058208a1b4cSTakashi Iwai static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
10591da177e4SLinus Torvalds {
10601da177e4SLinus Torvalds 	return snd_ymfpci_capture_open(substream, 1);
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds 
1063208a1b4cSTakashi Iwai static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
10641da177e4SLinus Torvalds {
10651da177e4SLinus Torvalds 	return 0;
10661da177e4SLinus Torvalds }
10671da177e4SLinus Torvalds 
1068208a1b4cSTakashi Iwai static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
10691da177e4SLinus Torvalds {
1070208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1071208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10741da177e4SLinus Torvalds 	if (ypcm->output_rear && chip->rear_opened > 0) {
10751da177e4SLinus Torvalds 		chip->rear_opened--;
10761da177e4SLinus Torvalds 		ymfpci_close_extension(chip);
10771da177e4SLinus Torvalds 	}
10781da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10791da177e4SLinus Torvalds 	return snd_ymfpci_playback_close_1(substream);
10801da177e4SLinus Torvalds }
10811da177e4SLinus Torvalds 
1082208a1b4cSTakashi Iwai static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
10831da177e4SLinus Torvalds {
1084208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10871da177e4SLinus Torvalds 	chip->spdif_opened = 0;
10881da177e4SLinus Torvalds 	ymfpci_close_extension(chip);
10891da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
10901da177e4SLinus Torvalds 			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
10911da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
10921da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10931da177e4SLinus Torvalds 	chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
10941da177e4SLinus Torvalds 	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
10951da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
10961da177e4SLinus Torvalds 	return snd_ymfpci_playback_close_1(substream);
10971da177e4SLinus Torvalds }
10981da177e4SLinus Torvalds 
1099208a1b4cSTakashi Iwai static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
11001da177e4SLinus Torvalds {
1101208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
11041da177e4SLinus Torvalds 	if (chip->rear_opened > 0) {
11051da177e4SLinus Torvalds 		chip->rear_opened--;
11061da177e4SLinus Torvalds 		ymfpci_close_extension(chip);
11071da177e4SLinus Torvalds 	}
11081da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
11091da177e4SLinus Torvalds 	return snd_ymfpci_playback_close_1(substream);
11101da177e4SLinus Torvalds }
11111da177e4SLinus Torvalds 
1112208a1b4cSTakashi Iwai static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
11131da177e4SLinus Torvalds {
1114208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1115208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
1116208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	if (ypcm != NULL) {
11191da177e4SLinus Torvalds 		chip->capture_substream[ypcm->capture_bank_number] = NULL;
11201da177e4SLinus Torvalds 		snd_ymfpci_hw_stop(chip);
11211da177e4SLinus Torvalds 	}
11221da177e4SLinus Torvalds 	return 0;
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
1125208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_playback_ops = {
11261da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_open,
11271da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_close,
11281da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
11291da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
11301da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
11311da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
11321da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
11331da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
11341da177e4SLinus Torvalds };
11351da177e4SLinus Torvalds 
1136208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
11371da177e4SLinus Torvalds 	.open =			snd_ymfpci_capture_rec_open,
11381da177e4SLinus Torvalds 	.close =		snd_ymfpci_capture_close,
11391da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
11401da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_capture_hw_params,
11411da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_capture_hw_free,
11421da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_capture_prepare,
11431da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_capture_trigger,
11441da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_capture_pointer,
11451da177e4SLinus Torvalds };
11461da177e4SLinus Torvalds 
1147208a1b4cSTakashi Iwai int __devinit snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
11481da177e4SLinus Torvalds {
1149208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
11501da177e4SLinus Torvalds 	int err;
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	if (rpcm)
11531da177e4SLinus Torvalds 		*rpcm = NULL;
11541da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
11551da177e4SLinus Torvalds 		return err;
11561da177e4SLinus Torvalds 	pcm->private_data = chip;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
11591da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	/* global setup */
11621da177e4SLinus Torvalds 	pcm->info_flags = 0;
11631da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI");
11641da177e4SLinus Torvalds 	chip->pcm = pcm;
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
11671da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	if (rpcm)
11701da177e4SLinus Torvalds 		*rpcm = pcm;
11711da177e4SLinus Torvalds 	return 0;
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds 
1174208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
11751da177e4SLinus Torvalds 	.open =			snd_ymfpci_capture_ac97_open,
11761da177e4SLinus Torvalds 	.close =		snd_ymfpci_capture_close,
11771da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
11781da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_capture_hw_params,
11791da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_capture_hw_free,
11801da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_capture_prepare,
11811da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_capture_trigger,
11821da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_capture_pointer,
11831da177e4SLinus Torvalds };
11841da177e4SLinus Torvalds 
1185208a1b4cSTakashi Iwai int __devinit snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
11861da177e4SLinus Torvalds {
1187208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
11881da177e4SLinus Torvalds 	int err;
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 	if (rpcm)
11911da177e4SLinus Torvalds 		*rpcm = NULL;
11921da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
11931da177e4SLinus Torvalds 		return err;
11941da177e4SLinus Torvalds 	pcm->private_data = chip;
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	/* global setup */
11991da177e4SLinus Torvalds 	pcm->info_flags = 0;
12001da177e4SLinus Torvalds 	sprintf(pcm->name, "YMFPCI - %s",
12011da177e4SLinus Torvalds 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
12021da177e4SLinus Torvalds 	chip->pcm2 = pcm;
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
12051da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	if (rpcm)
12081da177e4SLinus Torvalds 		*rpcm = pcm;
12091da177e4SLinus Torvalds 	return 0;
12101da177e4SLinus Torvalds }
12111da177e4SLinus Torvalds 
1212208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
12131da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_spdif_open,
12141da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_spdif_close,
12151da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
12161da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
12171da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
12181da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
12191da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
12201da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
12211da177e4SLinus Torvalds };
12221da177e4SLinus Torvalds 
1223208a1b4cSTakashi Iwai int __devinit snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
12241da177e4SLinus Torvalds {
1225208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
12261da177e4SLinus Torvalds 	int err;
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	if (rpcm)
12291da177e4SLinus Torvalds 		*rpcm = NULL;
12301da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
12311da177e4SLinus Torvalds 		return err;
12321da177e4SLinus Torvalds 	pcm->private_data = chip;
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	/* global setup */
12371da177e4SLinus Torvalds 	pcm->info_flags = 0;
12381da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI - IEC958");
12391da177e4SLinus Torvalds 	chip->pcm_spdif = pcm;
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
12421da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	if (rpcm)
12451da177e4SLinus Torvalds 		*rpcm = pcm;
12461da177e4SLinus Torvalds 	return 0;
12471da177e4SLinus Torvalds }
12481da177e4SLinus Torvalds 
1249208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
12501da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_4ch_open,
12511da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_4ch_close,
12521da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
12531da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
12541da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
12551da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
12561da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
12571da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
12581da177e4SLinus Torvalds };
12591da177e4SLinus Torvalds 
1260208a1b4cSTakashi Iwai int __devinit snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device, struct snd_pcm ** rpcm)
12611da177e4SLinus Torvalds {
1262208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
12631da177e4SLinus Torvalds 	int err;
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	if (rpcm)
12661da177e4SLinus Torvalds 		*rpcm = NULL;
12671da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
12681da177e4SLinus Torvalds 		return err;
12691da177e4SLinus Torvalds 	pcm->private_data = chip;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 	/* global setup */
12741da177e4SLinus Torvalds 	pcm->info_flags = 0;
12751da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI - Rear PCM");
12761da177e4SLinus Torvalds 	chip->pcm_4ch = pcm;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
12791da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	if (rpcm)
12821da177e4SLinus Torvalds 		*rpcm = pcm;
12831da177e4SLinus Torvalds 	return 0;
12841da177e4SLinus Torvalds }
12851da177e4SLinus Torvalds 
1286208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
12871da177e4SLinus Torvalds {
12881da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
12891da177e4SLinus Torvalds 	uinfo->count = 1;
12901da177e4SLinus Torvalds 	return 0;
12911da177e4SLinus Torvalds }
12921da177e4SLinus Torvalds 
1293208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1294208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
12951da177e4SLinus Torvalds {
1296208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
12991da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
13001da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1301fc80a202SClemens Ladisch 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
13021da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13031da177e4SLinus Torvalds 	return 0;
13041da177e4SLinus Torvalds }
13051da177e4SLinus Torvalds 
1306208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1307208a1b4cSTakashi Iwai 					 struct snd_ctl_elem_value *ucontrol)
13081da177e4SLinus Torvalds {
1309208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13101da177e4SLinus Torvalds 	unsigned int val;
13111da177e4SLinus Torvalds 	int change;
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
13141da177e4SLinus Torvalds 	      (ucontrol->value.iec958.status[1] << 8);
13151da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13161da177e4SLinus Torvalds 	change = chip->spdif_bits != val;
13171da177e4SLinus Torvalds 	chip->spdif_bits = val;
13181da177e4SLinus Torvalds 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
13191da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
13201da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13211da177e4SLinus Torvalds 	return change;
13221da177e4SLinus Torvalds }
13231da177e4SLinus Torvalds 
1324208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_spdif_default __devinitdata =
13251da177e4SLinus Torvalds {
13261da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
13271da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
13281da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_default_info,
13291da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_default_get,
13301da177e4SLinus Torvalds 	.put =		snd_ymfpci_spdif_default_put
13311da177e4SLinus Torvalds };
13321da177e4SLinus Torvalds 
1333208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
13341da177e4SLinus Torvalds {
13351da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
13361da177e4SLinus Torvalds 	uinfo->count = 1;
13371da177e4SLinus Torvalds 	return 0;
13381da177e4SLinus Torvalds }
13391da177e4SLinus Torvalds 
1340208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1341208a1b4cSTakashi Iwai 				      struct snd_ctl_elem_value *ucontrol)
13421da177e4SLinus Torvalds {
1343208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13461da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = 0x3e;
13471da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = 0xff;
13481da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13491da177e4SLinus Torvalds 	return 0;
13501da177e4SLinus Torvalds }
13511da177e4SLinus Torvalds 
1352208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_spdif_mask __devinitdata =
13531da177e4SLinus Torvalds {
13541da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
13551da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
13561da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
13571da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_mask_info,
13581da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_mask_get,
13591da177e4SLinus Torvalds };
13601da177e4SLinus Torvalds 
1361208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
13621da177e4SLinus Torvalds {
13631da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
13641da177e4SLinus Torvalds 	uinfo->count = 1;
13651da177e4SLinus Torvalds 	return 0;
13661da177e4SLinus Torvalds }
13671da177e4SLinus Torvalds 
1368208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1369208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
13701da177e4SLinus Torvalds {
1371208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13741da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
13751da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1376fc80a202SClemens Ladisch 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
13771da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13781da177e4SLinus Torvalds 	return 0;
13791da177e4SLinus Torvalds }
13801da177e4SLinus Torvalds 
1381208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1382208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
13831da177e4SLinus Torvalds {
1384208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13851da177e4SLinus Torvalds 	unsigned int val;
13861da177e4SLinus Torvalds 	int change;
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
13891da177e4SLinus Torvalds 	      (ucontrol->value.iec958.status[1] << 8);
13901da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13911da177e4SLinus Torvalds 	change = chip->spdif_pcm_bits != val;
13921da177e4SLinus Torvalds 	chip->spdif_pcm_bits = val;
13931da177e4SLinus Torvalds 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
13941da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
13951da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13961da177e4SLinus Torvalds 	return change;
13971da177e4SLinus Torvalds }
13981da177e4SLinus Torvalds 
1399208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_spdif_stream __devinitdata =
14001da177e4SLinus Torvalds {
14011da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
14021da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
14031da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
14041da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_stream_info,
14051da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_stream_get,
14061da177e4SLinus Torvalds 	.put =		snd_ymfpci_spdif_stream_put
14071da177e4SLinus Torvalds };
14081da177e4SLinus Torvalds 
1409208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
14101da177e4SLinus Torvalds {
1411bed6896dSClemens Ladisch 	static const char *const texts[3] = {"AC'97", "IEC958", "ZV Port"};
14121da177e4SLinus Torvalds 
1413bed6896dSClemens Ladisch 	return snd_ctl_enum_info(info, 1, 3, texts);
14141da177e4SLinus Torvalds }
14151da177e4SLinus Torvalds 
1416208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
14171da177e4SLinus Torvalds {
1418208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
14191da177e4SLinus Torvalds 	u16 reg;
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
14221da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
14231da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
14241da177e4SLinus Torvalds 	if (!(reg & 0x100))
14251da177e4SLinus Torvalds 		value->value.enumerated.item[0] = 0;
14261da177e4SLinus Torvalds 	else
14271da177e4SLinus Torvalds 		value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
14281da177e4SLinus Torvalds 	return 0;
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds 
1431208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
14321da177e4SLinus Torvalds {
1433208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
14341da177e4SLinus Torvalds 	u16 reg, old_reg;
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
14371da177e4SLinus Torvalds 	old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
14381da177e4SLinus Torvalds 	if (value->value.enumerated.item[0] == 0)
14391da177e4SLinus Torvalds 		reg = old_reg & ~0x100;
14401da177e4SLinus Torvalds 	else
14411da177e4SLinus Torvalds 		reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
14421da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
14431da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
14441da177e4SLinus Torvalds 	return reg != old_reg;
14451da177e4SLinus Torvalds }
14461da177e4SLinus Torvalds 
1447208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_drec_source __devinitdata = {
14481da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
14491da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
14501da177e4SLinus Torvalds 	.name =		"Direct Recording Source",
14511da177e4SLinus Torvalds 	.info =		snd_ymfpci_drec_source_info,
14521da177e4SLinus Torvalds 	.get =		snd_ymfpci_drec_source_get,
14531da177e4SLinus Torvalds 	.put =		snd_ymfpci_drec_source_put
14541da177e4SLinus Torvalds };
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds /*
14571da177e4SLinus Torvalds  *  Mixer controls
14581da177e4SLinus Torvalds  */
14591da177e4SLinus Torvalds 
1460d602c885SGlen Masgai #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
14611da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
14621da177e4SLinus Torvalds   .info = snd_ymfpci_info_single, \
14631da177e4SLinus Torvalds   .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1464d602c885SGlen Masgai   .private_value = ((reg) | ((shift) << 16)) }
14651da177e4SLinus Torvalds 
1466a5ce8890STakashi Iwai #define snd_ymfpci_info_single		snd_ctl_boolean_mono_info
14671da177e4SLinus Torvalds 
1468208a1b4cSTakashi Iwai static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1469208a1b4cSTakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
14701da177e4SLinus Torvalds {
1471208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1472d602c885SGlen Masgai 	int reg = kcontrol->private_value & 0xffff;
1473d602c885SGlen Masgai 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1474d602c885SGlen Masgai 	unsigned int mask = 1;
14751da177e4SLinus Torvalds 
1476d602c885SGlen Masgai 	switch (reg) {
14771da177e4SLinus Torvalds 	case YDSXGR_SPDIFOUTCTRL: break;
14781da177e4SLinus Torvalds 	case YDSXGR_SPDIFINCTRL: break;
14791da177e4SLinus Torvalds 	default: return -EINVAL;
14801da177e4SLinus Torvalds 	}
1481d602c885SGlen Masgai 	ucontrol->value.integer.value[0] =
1482d602c885SGlen Masgai 		(snd_ymfpci_readl(chip, reg) >> shift) & mask;
14831da177e4SLinus Torvalds 	return 0;
14841da177e4SLinus Torvalds }
14851da177e4SLinus Torvalds 
1486208a1b4cSTakashi Iwai static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1487208a1b4cSTakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
14881da177e4SLinus Torvalds {
1489208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1490d602c885SGlen Masgai 	int reg = kcontrol->private_value & 0xffff;
1491d602c885SGlen Masgai 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1492d602c885SGlen Masgai  	unsigned int mask = 1;
14931da177e4SLinus Torvalds 	int change;
14941da177e4SLinus Torvalds 	unsigned int val, oval;
14951da177e4SLinus Torvalds 
1496d602c885SGlen Masgai 	switch (reg) {
14971da177e4SLinus Torvalds 	case YDSXGR_SPDIFOUTCTRL: break;
14981da177e4SLinus Torvalds 	case YDSXGR_SPDIFINCTRL: break;
14991da177e4SLinus Torvalds 	default: return -EINVAL;
15001da177e4SLinus Torvalds 	}
15011da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
15021da177e4SLinus Torvalds 	val <<= shift;
15031da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15041da177e4SLinus Torvalds 	oval = snd_ymfpci_readl(chip, reg);
15051da177e4SLinus Torvalds 	val = (oval & ~(mask << shift)) | val;
15061da177e4SLinus Torvalds 	change = val != oval;
15071da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, reg, val);
15081da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15091da177e4SLinus Torvalds 	return change;
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds 
15120cb29ea0STakashi Iwai static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
151333925186STakashi Iwai 
15141da177e4SLinus Torvalds #define YMFPCI_DOUBLE(xname, xindex, reg) \
15151da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
151633925186STakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
15171da177e4SLinus Torvalds   .info = snd_ymfpci_info_double, \
15181da177e4SLinus Torvalds   .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
151933925186STakashi Iwai   .private_value = reg, \
152033925186STakashi Iwai   .tlv = { .p = db_scale_native } }
15211da177e4SLinus Torvalds 
1522208a1b4cSTakashi Iwai static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15231da177e4SLinus Torvalds {
15241da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
15271da177e4SLinus Torvalds 		return -EINVAL;
1528467a8c2fSAdrian Bunk 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
15291da177e4SLinus Torvalds 	uinfo->count = 2;
15301da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
1531467a8c2fSAdrian Bunk 	uinfo->value.integer.max = 16383;
15321da177e4SLinus Torvalds 	return 0;
15331da177e4SLinus Torvalds }
15341da177e4SLinus Torvalds 
1535208a1b4cSTakashi Iwai static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15361da177e4SLinus Torvalds {
1537208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15381da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
1539467a8c2fSAdrian Bunk 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
15401da177e4SLinus Torvalds 	unsigned int val;
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
15431da177e4SLinus Torvalds 		return -EINVAL;
15441da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15451da177e4SLinus Torvalds 	val = snd_ymfpci_readl(chip, reg);
15461da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15471da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
15481da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
15491da177e4SLinus Torvalds 	return 0;
15501da177e4SLinus Torvalds }
15511da177e4SLinus Torvalds 
1552208a1b4cSTakashi Iwai static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15531da177e4SLinus Torvalds {
1554208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15551da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
1556467a8c2fSAdrian Bunk 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
15571da177e4SLinus Torvalds 	int change;
15581da177e4SLinus Torvalds 	unsigned int val1, val2, oval;
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
15611da177e4SLinus Torvalds 		return -EINVAL;
15621da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
15631da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
15641da177e4SLinus Torvalds 	val1 <<= shift_left;
15651da177e4SLinus Torvalds 	val2 <<= shift_right;
15661da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15671da177e4SLinus Torvalds 	oval = snd_ymfpci_readl(chip, reg);
15681da177e4SLinus Torvalds 	val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
15691da177e4SLinus Torvalds 	change = val1 != oval;
15701da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, reg, val1);
15711da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15721da177e4SLinus Torvalds 	return change;
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
1575177a7cdbSClemens Ladisch static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1576177a7cdbSClemens Ladisch 				       struct snd_ctl_elem_value *ucontrol)
1577177a7cdbSClemens Ladisch {
1578177a7cdbSClemens Ladisch 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1579177a7cdbSClemens Ladisch 	unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1580177a7cdbSClemens Ladisch 	unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1581177a7cdbSClemens Ladisch 	int change;
1582177a7cdbSClemens Ladisch 	unsigned int value, oval;
1583177a7cdbSClemens Ladisch 
1584177a7cdbSClemens Ladisch 	value = ucontrol->value.integer.value[0] & 0x3fff;
1585177a7cdbSClemens Ladisch 	value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1586177a7cdbSClemens Ladisch 	spin_lock_irq(&chip->reg_lock);
1587177a7cdbSClemens Ladisch 	oval = snd_ymfpci_readl(chip, reg);
1588177a7cdbSClemens Ladisch 	change = value != oval;
1589177a7cdbSClemens Ladisch 	snd_ymfpci_writel(chip, reg, value);
1590177a7cdbSClemens Ladisch 	snd_ymfpci_writel(chip, reg2, value);
1591177a7cdbSClemens Ladisch 	spin_unlock_irq(&chip->reg_lock);
1592177a7cdbSClemens Ladisch 	return change;
1593177a7cdbSClemens Ladisch }
1594177a7cdbSClemens Ladisch 
15951da177e4SLinus Torvalds /*
15961da177e4SLinus Torvalds  * 4ch duplication
15971da177e4SLinus Torvalds  */
1598a5ce8890STakashi Iwai #define snd_ymfpci_info_dup4ch		snd_ctl_boolean_mono_info
15991da177e4SLinus Torvalds 
1600208a1b4cSTakashi Iwai static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16011da177e4SLinus Torvalds {
1602208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
16031da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = chip->mode_dup4ch;
16041da177e4SLinus Torvalds 	return 0;
16051da177e4SLinus Torvalds }
16061da177e4SLinus Torvalds 
1607208a1b4cSTakashi Iwai static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16081da177e4SLinus Torvalds {
1609208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
16101da177e4SLinus Torvalds 	int change;
16111da177e4SLinus Torvalds 	change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
16121da177e4SLinus Torvalds 	if (change)
16131da177e4SLinus Torvalds 		chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
16141da177e4SLinus Torvalds 	return change;
16151da177e4SLinus Torvalds }
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 
1618208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_controls[] __devinitdata = {
1619177a7cdbSClemens Ladisch {
1620177a7cdbSClemens Ladisch 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1621177a7cdbSClemens Ladisch 	.name = "Wave Playback Volume",
1622177a7cdbSClemens Ladisch 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1623177a7cdbSClemens Ladisch 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1624177a7cdbSClemens Ladisch 	.info = snd_ymfpci_info_double,
1625177a7cdbSClemens Ladisch 	.get = snd_ymfpci_get_double,
1626177a7cdbSClemens Ladisch 	.put = snd_ymfpci_put_nativedacvol,
1627177a7cdbSClemens Ladisch 	.private_value = YDSXGR_NATIVEDACOUTVOL,
1628177a7cdbSClemens Ladisch 	.tlv = { .p = db_scale_native },
1629177a7cdbSClemens Ladisch },
16301da177e4SLinus Torvalds YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
16311da177e4SLinus Torvalds YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
16321da177e4SLinus Torvalds YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
16331da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
16341da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
16351da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
16361da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
163789f3325aSRaymond Yau YMFPCI_DOUBLE("FM Legacy Playback Volume", 0, YDSXGR_LEGACYOUTVOL),
16381da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
16391da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
16401da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
16411da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1642d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1643d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1644d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
16451da177e4SLinus Torvalds {
16461da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
16471da177e4SLinus Torvalds 	.name = "4ch Duplication",
16481da177e4SLinus Torvalds 	.info = snd_ymfpci_info_dup4ch,
16491da177e4SLinus Torvalds 	.get = snd_ymfpci_get_dup4ch,
16501da177e4SLinus Torvalds 	.put = snd_ymfpci_put_dup4ch,
16511da177e4SLinus Torvalds },
16521da177e4SLinus Torvalds };
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds /*
16561da177e4SLinus Torvalds  * GPIO
16571da177e4SLinus Torvalds  */
16581da177e4SLinus Torvalds 
1659208a1b4cSTakashi Iwai static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
16601da177e4SLinus Torvalds {
16611da177e4SLinus Torvalds 	u16 reg, mode;
16621da177e4SLinus Torvalds 	unsigned long flags;
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
16651da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
16661da177e4SLinus Torvalds 	reg &= ~(1 << (pin + 8));
16671da177e4SLinus Torvalds 	reg |= (1 << pin);
16681da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
16691da177e4SLinus Torvalds 	/* set the level mode for input line */
16701da177e4SLinus Torvalds 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
16711da177e4SLinus Torvalds 	mode &= ~(3 << (pin * 2));
16721da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
16731da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
16741da177e4SLinus Torvalds 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
16751da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
16761da177e4SLinus Torvalds 	return (mode >> pin) & 1;
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds 
1679208a1b4cSTakashi Iwai static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
16801da177e4SLinus Torvalds {
16811da177e4SLinus Torvalds 	u16 reg;
16821da177e4SLinus Torvalds 	unsigned long flags;
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
16851da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
16861da177e4SLinus Torvalds 	reg &= ~(1 << pin);
16871da177e4SLinus Torvalds 	reg &= ~(1 << (pin + 8));
16881da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
16891da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
16901da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
16911da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds 	return 0;
16941da177e4SLinus Torvalds }
16951da177e4SLinus Torvalds 
1696a5ce8890STakashi Iwai #define snd_ymfpci_gpio_sw_info		snd_ctl_boolean_mono_info
16971da177e4SLinus Torvalds 
1698208a1b4cSTakashi Iwai static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16991da177e4SLinus Torvalds {
1700208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17011da177e4SLinus Torvalds 	int pin = (int)kcontrol->private_value;
17021da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
17031da177e4SLinus Torvalds 	return 0;
17041da177e4SLinus Torvalds }
17051da177e4SLinus Torvalds 
1706208a1b4cSTakashi Iwai static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17071da177e4SLinus Torvalds {
1708208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17091da177e4SLinus Torvalds 	int pin = (int)kcontrol->private_value;
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
17121da177e4SLinus Torvalds 		snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
17131da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
17141da177e4SLinus Torvalds 		return 1;
17151da177e4SLinus Torvalds 	}
17161da177e4SLinus Torvalds 	return 0;
17171da177e4SLinus Torvalds }
17181da177e4SLinus Torvalds 
1719208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_rear_shared __devinitdata = {
17201da177e4SLinus Torvalds 	.name = "Shared Rear/Line-In Switch",
17211da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
17221da177e4SLinus Torvalds 	.info = snd_ymfpci_gpio_sw_info,
17231da177e4SLinus Torvalds 	.get = snd_ymfpci_gpio_sw_get,
17241da177e4SLinus Torvalds 	.put = snd_ymfpci_gpio_sw_put,
17251da177e4SLinus Torvalds 	.private_value = 2,
17261da177e4SLinus Torvalds };
17271da177e4SLinus Torvalds 
17289bcf6551SClemens Ladisch /*
17299bcf6551SClemens Ladisch  * PCM voice volume
17309bcf6551SClemens Ladisch  */
17319bcf6551SClemens Ladisch 
1732208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1733208a1b4cSTakashi Iwai 				   struct snd_ctl_elem_info *uinfo)
17349bcf6551SClemens Ladisch {
17359bcf6551SClemens Ladisch 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
17369bcf6551SClemens Ladisch 	uinfo->count = 2;
17379bcf6551SClemens Ladisch 	uinfo->value.integer.min = 0;
17389bcf6551SClemens Ladisch 	uinfo->value.integer.max = 0x8000;
17399bcf6551SClemens Ladisch 	return 0;
17409bcf6551SClemens Ladisch }
17419bcf6551SClemens Ladisch 
1742208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1743208a1b4cSTakashi Iwai 				  struct snd_ctl_elem_value *ucontrol)
17449bcf6551SClemens Ladisch {
1745208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17469bcf6551SClemens Ladisch 	unsigned int subs = kcontrol->id.subdevice;
17479bcf6551SClemens Ladisch 
17489bcf6551SClemens Ladisch 	ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
17499bcf6551SClemens Ladisch 	ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
17509bcf6551SClemens Ladisch 	return 0;
17519bcf6551SClemens Ladisch }
17529bcf6551SClemens Ladisch 
1753208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1754208a1b4cSTakashi Iwai 				  struct snd_ctl_elem_value *ucontrol)
17559bcf6551SClemens Ladisch {
1756208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17579bcf6551SClemens Ladisch 	unsigned int subs = kcontrol->id.subdevice;
1758208a1b4cSTakashi Iwai 	struct snd_pcm_substream *substream;
17599bcf6551SClemens Ladisch 	unsigned long flags;
17609bcf6551SClemens Ladisch 
17619bcf6551SClemens Ladisch 	if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
17629bcf6551SClemens Ladisch 	    ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
17639bcf6551SClemens Ladisch 		chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
17649bcf6551SClemens Ladisch 		chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
17654e98d6a7STakashi Iwai 		if (chip->pcm_mixer[subs].left > 0x8000)
17664e98d6a7STakashi Iwai 			chip->pcm_mixer[subs].left = 0x8000;
17674e98d6a7STakashi Iwai 		if (chip->pcm_mixer[subs].right > 0x8000)
17684e98d6a7STakashi Iwai 			chip->pcm_mixer[subs].right = 0x8000;
17699bcf6551SClemens Ladisch 
1770208a1b4cSTakashi Iwai 		substream = (struct snd_pcm_substream *)kcontrol->private_value;
17719bcf6551SClemens Ladisch 		spin_lock_irqsave(&chip->voice_lock, flags);
17729bcf6551SClemens Ladisch 		if (substream->runtime && substream->runtime->private_data) {
1773208a1b4cSTakashi Iwai 			struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
17749ed1261eSTeru KAMOGASHIRA 			if (!ypcm->use_441_slot)
17759bcf6551SClemens Ladisch 				ypcm->update_pcm_vol = 2;
17769bcf6551SClemens Ladisch 		}
17779bcf6551SClemens Ladisch 		spin_unlock_irqrestore(&chip->voice_lock, flags);
17789bcf6551SClemens Ladisch 		return 1;
17799bcf6551SClemens Ladisch 	}
17809bcf6551SClemens Ladisch 	return 0;
17819bcf6551SClemens Ladisch }
17829bcf6551SClemens Ladisch 
1783208a1b4cSTakashi Iwai static struct snd_kcontrol_new snd_ymfpci_pcm_volume __devinitdata = {
17849bcf6551SClemens Ladisch 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
17859bcf6551SClemens Ladisch 	.name = "PCM Playback Volume",
17869bcf6551SClemens Ladisch 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
17879bcf6551SClemens Ladisch 		SNDRV_CTL_ELEM_ACCESS_INACTIVE,
17889bcf6551SClemens Ladisch 	.info = snd_ymfpci_pcm_vol_info,
17899bcf6551SClemens Ladisch 	.get = snd_ymfpci_pcm_vol_get,
17909bcf6551SClemens Ladisch 	.put = snd_ymfpci_pcm_vol_put,
17919bcf6551SClemens Ladisch };
17929bcf6551SClemens Ladisch 
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds /*
17951da177e4SLinus Torvalds  *  Mixer routines
17961da177e4SLinus Torvalds  */
17971da177e4SLinus Torvalds 
1798208a1b4cSTakashi Iwai static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
17991da177e4SLinus Torvalds {
1800208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = bus->private_data;
18011da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
18021da177e4SLinus Torvalds }
18031da177e4SLinus Torvalds 
1804208a1b4cSTakashi Iwai static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
18051da177e4SLinus Torvalds {
1806208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
18071da177e4SLinus Torvalds 	chip->ac97 = NULL;
18081da177e4SLinus Torvalds }
18091da177e4SLinus Torvalds 
1810d9301263SGlen Masgai int __devinit snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
18111da177e4SLinus Torvalds {
1812208a1b4cSTakashi Iwai 	struct snd_ac97_template ac97;
1813208a1b4cSTakashi Iwai 	struct snd_kcontrol *kctl;
1814208a1b4cSTakashi Iwai 	struct snd_pcm_substream *substream;
18151da177e4SLinus Torvalds 	unsigned int idx;
18161da177e4SLinus Torvalds 	int err;
1817208a1b4cSTakashi Iwai 	static struct snd_ac97_bus_ops ops = {
18181da177e4SLinus Torvalds 		.write = snd_ymfpci_codec_write,
18191da177e4SLinus Torvalds 		.read = snd_ymfpci_codec_read,
18201da177e4SLinus Torvalds 	};
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
18231da177e4SLinus Torvalds 		return err;
18241da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
18251da177e4SLinus Torvalds 	chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
18281da177e4SLinus Torvalds 	ac97.private_data = chip;
18291da177e4SLinus Torvalds 	ac97.private_free = snd_ymfpci_mixer_free_ac97;
18301da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
18311da177e4SLinus Torvalds 		return err;
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 	/* to be sure */
18341da177e4SLinus Torvalds 	snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
18351da177e4SLinus Torvalds 			     AC97_EA_VRA|AC97_EA_VRM, 0);
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
18381da177e4SLinus Torvalds 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
18391da177e4SLinus Torvalds 			return err;
18401da177e4SLinus Torvalds 	}
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds 	/* add S/PDIF control */
1843da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip->pcm_spdif))
1844da3cec35STakashi Iwai 		return -ENXIO;
18451da177e4SLinus Torvalds 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
18461da177e4SLinus Torvalds 		return err;
18471da177e4SLinus Torvalds 	kctl->id.device = chip->pcm_spdif->device;
18481da177e4SLinus Torvalds 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
18491da177e4SLinus Torvalds 		return err;
18501da177e4SLinus Torvalds 	kctl->id.device = chip->pcm_spdif->device;
18511da177e4SLinus Torvalds 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
18521da177e4SLinus Torvalds 		return err;
18531da177e4SLinus Torvalds 	kctl->id.device = chip->pcm_spdif->device;
18541da177e4SLinus Torvalds 	chip->spdif_pcm_ctl = kctl;
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds 	/* direct recording source */
18571da177e4SLinus Torvalds 	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
18581da177e4SLinus Torvalds 	    (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
18591da177e4SLinus Torvalds 		return err;
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	/*
18621da177e4SLinus Torvalds 	 * shared rear/line-in
18631da177e4SLinus Torvalds 	 */
18641da177e4SLinus Torvalds 	if (rear_switch) {
18651da177e4SLinus Torvalds 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
18661da177e4SLinus Torvalds 			return err;
18671da177e4SLinus Torvalds 	}
18681da177e4SLinus Torvalds 
18699bcf6551SClemens Ladisch 	/* per-voice volume */
18709bcf6551SClemens Ladisch 	substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
18719bcf6551SClemens Ladisch 	for (idx = 0; idx < 32; ++idx) {
18729bcf6551SClemens Ladisch 		kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
18739bcf6551SClemens Ladisch 		if (!kctl)
18749bcf6551SClemens Ladisch 			return -ENOMEM;
18759bcf6551SClemens Ladisch 		kctl->id.device = chip->pcm->device;
18769bcf6551SClemens Ladisch 		kctl->id.subdevice = idx;
18779bcf6551SClemens Ladisch 		kctl->private_value = (unsigned long)substream;
18789bcf6551SClemens Ladisch 		if ((err = snd_ctl_add(chip->card, kctl)) < 0)
18799bcf6551SClemens Ladisch 			return err;
18809bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].left = 0x8000;
18819bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].right = 0x8000;
18829bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].ctl = kctl;
18839bcf6551SClemens Ladisch 		substream = substream->next;
18849bcf6551SClemens Ladisch 	}
18859bcf6551SClemens Ladisch 
18861da177e4SLinus Torvalds 	return 0;
18871da177e4SLinus Torvalds }
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds /*
18911da177e4SLinus Torvalds  * timer
18921da177e4SLinus Torvalds  */
18931da177e4SLinus Torvalds 
1894208a1b4cSTakashi Iwai static int snd_ymfpci_timer_start(struct snd_timer *timer)
18951da177e4SLinus Torvalds {
1896208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
18971da177e4SLinus Torvalds 	unsigned long flags;
18981da177e4SLinus Torvalds 	unsigned int count;
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 	chip = snd_timer_chip(timer);
19011da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
19026e2efaacSClemens Ladisch 	if (timer->sticks > 1) {
19036e2efaacSClemens Ladisch 		chip->timer_ticks = timer->sticks;
19046e2efaacSClemens Ladisch 		count = timer->sticks - 1;
19056e2efaacSClemens Ladisch 	} else {
19066e2efaacSClemens Ladisch 		/*
19076e2efaacSClemens Ladisch 		 * Divisor 1 is not allowed; fake it by using divisor 2 and
19086e2efaacSClemens Ladisch 		 * counting two ticks for each interrupt.
19096e2efaacSClemens Ladisch 		 */
19106e2efaacSClemens Ladisch 		chip->timer_ticks = 2;
19116e2efaacSClemens Ladisch 		count = 2 - 1;
19126e2efaacSClemens Ladisch 	}
19131da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
19141da177e4SLinus Torvalds 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
19151da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
19161da177e4SLinus Torvalds 	return 0;
19171da177e4SLinus Torvalds }
19181da177e4SLinus Torvalds 
1919208a1b4cSTakashi Iwai static int snd_ymfpci_timer_stop(struct snd_timer *timer)
19201da177e4SLinus Torvalds {
1921208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
19221da177e4SLinus Torvalds 	unsigned long flags;
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds 	chip = snd_timer_chip(timer);
19251da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
19261da177e4SLinus Torvalds 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
19271da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
19281da177e4SLinus Torvalds 	return 0;
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds 
1931208a1b4cSTakashi Iwai static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
19321da177e4SLinus Torvalds 					       unsigned long *num, unsigned long *den)
19331da177e4SLinus Torvalds {
19341da177e4SLinus Torvalds 	*num = 1;
19356e2efaacSClemens Ladisch 	*den = 96000;
19361da177e4SLinus Torvalds 	return 0;
19371da177e4SLinus Torvalds }
19381da177e4SLinus Torvalds 
1939208a1b4cSTakashi Iwai static struct snd_timer_hardware snd_ymfpci_timer_hw = {
19401da177e4SLinus Torvalds 	.flags = SNDRV_TIMER_HW_AUTO,
19416e2efaacSClemens Ladisch 	.resolution = 10417, /* 1 / 96 kHz = 10.41666...us */
19426e2efaacSClemens Ladisch 	.ticks = 0x10000,
19431da177e4SLinus Torvalds 	.start = snd_ymfpci_timer_start,
19441da177e4SLinus Torvalds 	.stop = snd_ymfpci_timer_stop,
19451da177e4SLinus Torvalds 	.precise_resolution = snd_ymfpci_timer_precise_resolution,
19461da177e4SLinus Torvalds };
19471da177e4SLinus Torvalds 
1948208a1b4cSTakashi Iwai int __devinit snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
19491da177e4SLinus Torvalds {
1950208a1b4cSTakashi Iwai 	struct snd_timer *timer = NULL;
1951208a1b4cSTakashi Iwai 	struct snd_timer_id tid;
19521da177e4SLinus Torvalds 	int err;
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
19551da177e4SLinus Torvalds 	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
19561da177e4SLinus Torvalds 	tid.card = chip->card->number;
19571da177e4SLinus Torvalds 	tid.device = device;
19581da177e4SLinus Torvalds 	tid.subdevice = 0;
19591da177e4SLinus Torvalds 	if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
19601da177e4SLinus Torvalds 		strcpy(timer->name, "YMFPCI timer");
19611da177e4SLinus Torvalds 		timer->private_data = chip;
19621da177e4SLinus Torvalds 		timer->hw = snd_ymfpci_timer_hw;
19631da177e4SLinus Torvalds 	}
19641da177e4SLinus Torvalds 	chip->timer = timer;
19651da177e4SLinus Torvalds 	return err;
19661da177e4SLinus Torvalds }
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds /*
19701da177e4SLinus Torvalds  *  proc interface
19711da177e4SLinus Torvalds  */
19721da177e4SLinus Torvalds 
1973208a1b4cSTakashi Iwai static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1974208a1b4cSTakashi Iwai 				 struct snd_info_buffer *buffer)
19751da177e4SLinus Torvalds {
1976208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = entry->private_data;
19771da177e4SLinus Torvalds 	int i;
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 	snd_iprintf(buffer, "YMFPCI\n\n");
19801da177e4SLinus Torvalds 	for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
19811da177e4SLinus Torvalds 		snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
19821da177e4SLinus Torvalds }
19831da177e4SLinus Torvalds 
1984208a1b4cSTakashi Iwai static int __devinit snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
19851da177e4SLinus Torvalds {
1986208a1b4cSTakashi Iwai 	struct snd_info_entry *entry;
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds 	if (! snd_card_proc_new(card, "ymfpci", &entry))
1989bf850204STakashi Iwai 		snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
19901da177e4SLinus Torvalds 	return 0;
19911da177e4SLinus Torvalds }
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds /*
19941da177e4SLinus Torvalds  *  initialization routines
19951da177e4SLinus Torvalds  */
19961da177e4SLinus Torvalds 
19971da177e4SLinus Torvalds static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
19981da177e4SLinus Torvalds {
19991da177e4SLinus Torvalds 	u8 cmd;
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
20021da177e4SLinus Torvalds #if 0 // force to reset
20031da177e4SLinus Torvalds 	if (cmd & 0x03) {
20041da177e4SLinus Torvalds #endif
20051da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
20061da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
20071da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
20081da177e4SLinus Torvalds 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
20091da177e4SLinus Torvalds 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
20101da177e4SLinus Torvalds #if 0
20111da177e4SLinus Torvalds 	}
20121da177e4SLinus Torvalds #endif
20131da177e4SLinus Torvalds }
20141da177e4SLinus Torvalds 
2015208a1b4cSTakashi Iwai static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
20161da177e4SLinus Torvalds {
20171da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
20181da177e4SLinus Torvalds }
20191da177e4SLinus Torvalds 
2020208a1b4cSTakashi Iwai static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
20211da177e4SLinus Torvalds {
20221da177e4SLinus Torvalds 	u32 val;
20231da177e4SLinus Torvalds 	int timeout = 1000;
20241da177e4SLinus Torvalds 
20251da177e4SLinus Torvalds 	val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
20261da177e4SLinus Torvalds 	if (val)
20271da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
20281da177e4SLinus Torvalds 	while (timeout-- > 0) {
20291da177e4SLinus Torvalds 		val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
20301da177e4SLinus Torvalds 		if ((val & 0x00000002) == 0)
20311da177e4SLinus Torvalds 			break;
20321da177e4SLinus Torvalds 	}
20331da177e4SLinus Torvalds }
20341da177e4SLinus Torvalds 
2035102fa906SClemens Ladisch static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2036102fa906SClemens Ladisch {
2037102fa906SClemens Ladisch 	int err, is_1e;
2038102fa906SClemens Ladisch 	const char *name;
2039102fa906SClemens Ladisch 
2040102fa906SClemens Ladisch 	err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2041102fa906SClemens Ladisch 			       &chip->pci->dev);
2042102fa906SClemens Ladisch 	if (err >= 0) {
2043b82a82d0SDavid Woodhouse 		if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
2044102fa906SClemens Ladisch 			snd_printk(KERN_ERR "DSP microcode has wrong size\n");
2045102fa906SClemens Ladisch 			err = -EINVAL;
2046102fa906SClemens Ladisch 		}
2047102fa906SClemens Ladisch 	}
2048b7dd2b34STakashi Iwai 	if (err < 0)
2049102fa906SClemens Ladisch 		return err;
2050102fa906SClemens Ladisch 	is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2051102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2052102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2053102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2054102fa906SClemens Ladisch 	name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2055102fa906SClemens Ladisch 	err = request_firmware(&chip->controller_microcode, name,
2056102fa906SClemens Ladisch 			       &chip->pci->dev);
2057102fa906SClemens Ladisch 	if (err >= 0) {
2058b82a82d0SDavid Woodhouse 		if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
2059102fa906SClemens Ladisch 			snd_printk(KERN_ERR "controller microcode"
2060102fa906SClemens Ladisch 				   " has wrong size\n");
2061102fa906SClemens Ladisch 			err = -EINVAL;
2062102fa906SClemens Ladisch 		}
2063102fa906SClemens Ladisch 	}
2064b7dd2b34STakashi Iwai 	if (err < 0)
2065102fa906SClemens Ladisch 		return err;
2066102fa906SClemens Ladisch 	return 0;
2067102fa906SClemens Ladisch }
20687e0af29dSClemens Ladisch 
20697e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
20707e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
20717e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
20727e0af29dSClemens Ladisch 
2073208a1b4cSTakashi Iwai static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
20741da177e4SLinus Torvalds {
20751da177e4SLinus Torvalds 	int i;
20761da177e4SLinus Torvalds 	u16 ctrl;
2077b82a82d0SDavid Woodhouse 	const __le32 *inst;
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
20801da177e4SLinus Torvalds 	snd_ymfpci_disable_dsp(chip);
20811da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
20821da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
20831da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
20841da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
20851da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
20861da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
20871da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
20881da177e4SLinus Torvalds 	ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
20891da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds 	/* setup DSP instruction code */
2092b82a82d0SDavid Woodhouse 	inst = (const __le32 *)chip->dsp_microcode->data;
20931da177e4SLinus Torvalds 	for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2094b82a82d0SDavid Woodhouse 		snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2095b82a82d0SDavid Woodhouse 				  le32_to_cpu(inst[i]));
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds 	/* setup control instruction code */
2098b82a82d0SDavid Woodhouse 	inst = (const __le32 *)chip->controller_microcode->data;
20991da177e4SLinus Torvalds 	for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2100b82a82d0SDavid Woodhouse 		snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2101b82a82d0SDavid Woodhouse 				  le32_to_cpu(inst[i]));
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds 	snd_ymfpci_enable_dsp(chip);
21041da177e4SLinus Torvalds }
21051da177e4SLinus Torvalds 
2106208a1b4cSTakashi Iwai static int __devinit snd_ymfpci_memalloc(struct snd_ymfpci *chip)
21071da177e4SLinus Torvalds {
21081da177e4SLinus Torvalds 	long size, playback_ctrl_size;
21091da177e4SLinus Torvalds 	int voice, bank, reg;
21101da177e4SLinus Torvalds 	u8 *ptr;
21111da177e4SLinus Torvalds 	dma_addr_t ptr_addr;
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds 	playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
21141da177e4SLinus Torvalds 	chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
21151da177e4SLinus Torvalds 	chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
21161da177e4SLinus Torvalds 	chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
21171da177e4SLinus Torvalds 	chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
21181da177e4SLinus Torvalds 
21197ab39926SClemens Ladisch 	size = ALIGN(playback_ctrl_size, 0x100) +
21207ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
21217ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
21227ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
21231da177e4SLinus Torvalds 	       chip->work_size;
21241da177e4SLinus Torvalds 	/* work_ptr must be aligned to 256 bytes, but it's already
21251da177e4SLinus Torvalds 	   covered with the kernel page allocation mechanism */
21261da177e4SLinus Torvalds 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
21271da177e4SLinus Torvalds 				size, &chip->work_ptr) < 0)
21281da177e4SLinus Torvalds 		return -ENOMEM;
21291da177e4SLinus Torvalds 	ptr = chip->work_ptr.area;
21301da177e4SLinus Torvalds 	ptr_addr = chip->work_ptr.addr;
21311da177e4SLinus Torvalds 	memset(ptr, 0, size);	/* for sure */
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	chip->bank_base_playback = ptr;
21341da177e4SLinus Torvalds 	chip->bank_base_playback_addr = ptr_addr;
21351da177e4SLinus Torvalds 	chip->ctrl_playback = (u32 *)ptr;
21361da177e4SLinus Torvalds 	chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
21377ab39926SClemens Ladisch 	ptr += ALIGN(playback_ctrl_size, 0x100);
21387ab39926SClemens Ladisch 	ptr_addr += ALIGN(playback_ctrl_size, 0x100);
21391da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
21401da177e4SLinus Torvalds 		chip->voices[voice].number = voice;
2141208a1b4cSTakashi Iwai 		chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
21421da177e4SLinus Torvalds 		chip->voices[voice].bank_addr = ptr_addr;
21431da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2144208a1b4cSTakashi Iwai 			chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
21451da177e4SLinus Torvalds 			ptr += chip->bank_size_playback;
21461da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_playback;
21471da177e4SLinus Torvalds 		}
21481da177e4SLinus Torvalds 	}
21497ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21507ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21511da177e4SLinus Torvalds 	chip->bank_base_capture = ptr;
21521da177e4SLinus Torvalds 	chip->bank_base_capture_addr = ptr_addr;
21531da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
21541da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2155208a1b4cSTakashi Iwai 			chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
21561da177e4SLinus Torvalds 			ptr += chip->bank_size_capture;
21571da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_capture;
21581da177e4SLinus Torvalds 		}
21597ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21607ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21611da177e4SLinus Torvalds 	chip->bank_base_effect = ptr;
21621da177e4SLinus Torvalds 	chip->bank_base_effect_addr = ptr_addr;
21631da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
21641da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2165208a1b4cSTakashi Iwai 			chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
21661da177e4SLinus Torvalds 			ptr += chip->bank_size_effect;
21671da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_effect;
21681da177e4SLinus Torvalds 		}
21697ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21707ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21711da177e4SLinus Torvalds 	chip->work_base = ptr;
21721da177e4SLinus Torvalds 	chip->work_base_addr = ptr_addr;
21731da177e4SLinus Torvalds 
2174da3cec35STakashi Iwai 	snd_BUG_ON(ptr + chip->work_size !=
2175da3cec35STakashi Iwai 		   chip->work_ptr.area + chip->work_ptr.bytes);
21761da177e4SLinus Torvalds 
21771da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
21781da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
21791da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
21801da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
21811da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
21821da177e4SLinus Torvalds 
21831da177e4SLinus Torvalds 	/* S/PDIF output initialization */
21841da177e4SLinus Torvalds 	chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
21851da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
21861da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds 	/* S/PDIF input initialization */
21891da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 	/* digital mixer setup */
21921da177e4SLinus Torvalds 	for (reg = 0x80; reg < 0xc0; reg += 4)
21931da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, reg, 0);
21941da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
21954a3b6983STakashi Iwai 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
21961da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
21971da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
21981da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
21991da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
22001da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
22011da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
22021da177e4SLinus Torvalds 
22031da177e4SLinus Torvalds 	return 0;
22041da177e4SLinus Torvalds }
22051da177e4SLinus Torvalds 
2206208a1b4cSTakashi Iwai static int snd_ymfpci_free(struct snd_ymfpci *chip)
22071da177e4SLinus Torvalds {
22081da177e4SLinus Torvalds 	u16 ctrl;
22091da177e4SLinus Torvalds 
2210da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
2211da3cec35STakashi Iwai 		return -EINVAL;
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 	if (chip->res_reg_area) {	/* don't touch busy hardware */
22141da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
22151da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
22161da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
22171da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
22181da177e4SLinus Torvalds 		snd_ymfpci_disable_dsp(chip);
22191da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
22201da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
22211da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
22221da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
22231da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
22241da177e4SLinus Torvalds 		ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
22251da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
22261da177e4SLinus Torvalds 	}
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds 	snd_ymfpci_ac3_done(chip);
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds 	/* Set PCI device to D3 state */
22311da177e4SLinus Torvalds #if 0
22321da177e4SLinus Torvalds 	/* FIXME: temporarily disabled, otherwise we cannot fire up
22331da177e4SLinus Torvalds 	 * the chip again unless reboot.  ACPI bug?
22341da177e4SLinus Torvalds 	 */
22351da177e4SLinus Torvalds 	pci_set_power_state(chip->pci, 3);
22361da177e4SLinus Torvalds #endif
22371da177e4SLinus Torvalds 
22381da177e4SLinus Torvalds #ifdef CONFIG_PM
22391da177e4SLinus Torvalds 	vfree(chip->saved_regs);
22401da177e4SLinus Torvalds #endif
224195866d38STakashi Iwai 	if (chip->irq >= 0)
224295866d38STakashi Iwai 		free_irq(chip->irq, chip);
2243b1d5776dSTakashi Iwai 	release_and_free_resource(chip->mpu_res);
2244b1d5776dSTakashi Iwai 	release_and_free_resource(chip->fm_res);
22451da177e4SLinus Torvalds 	snd_ymfpci_free_gameport(chip);
22461da177e4SLinus Torvalds 	if (chip->reg_area_virt)
22471da177e4SLinus Torvalds 		iounmap(chip->reg_area_virt);
22481da177e4SLinus Torvalds 	if (chip->work_ptr.area)
22491da177e4SLinus Torvalds 		snd_dma_free_pages(&chip->work_ptr);
22501da177e4SLinus Torvalds 
2251b1d5776dSTakashi Iwai 	release_and_free_resource(chip->res_reg_area);
22521da177e4SLinus Torvalds 
22531da177e4SLinus Torvalds 	pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds 	pci_disable_device(chip->pci);
2256102fa906SClemens Ladisch 	release_firmware(chip->dsp_microcode);
2257102fa906SClemens Ladisch 	release_firmware(chip->controller_microcode);
22581da177e4SLinus Torvalds 	kfree(chip);
22591da177e4SLinus Torvalds 	return 0;
22601da177e4SLinus Torvalds }
22611da177e4SLinus Torvalds 
2262208a1b4cSTakashi Iwai static int snd_ymfpci_dev_free(struct snd_device *device)
22631da177e4SLinus Torvalds {
2264208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = device->device_data;
22651da177e4SLinus Torvalds 	return snd_ymfpci_free(chip);
22661da177e4SLinus Torvalds }
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds #ifdef CONFIG_PM
22691da177e4SLinus Torvalds static int saved_regs_index[] = {
22701da177e4SLinus Torvalds 	/* spdif */
22711da177e4SLinus Torvalds 	YDSXGR_SPDIFOUTCTRL,
22721da177e4SLinus Torvalds 	YDSXGR_SPDIFOUTSTATUS,
22731da177e4SLinus Torvalds 	YDSXGR_SPDIFINCTRL,
22741da177e4SLinus Torvalds 	/* volumes */
22751da177e4SLinus Torvalds 	YDSXGR_PRIADCLOOPVOL,
22761da177e4SLinus Torvalds 	YDSXGR_NATIVEDACINVOL,
22771da177e4SLinus Torvalds 	YDSXGR_NATIVEDACOUTVOL,
22789ed1261eSTeru KAMOGASHIRA 	YDSXGR_BUF441OUTVOL,
22791da177e4SLinus Torvalds 	YDSXGR_NATIVEADCINVOL,
22801da177e4SLinus Torvalds 	YDSXGR_SPDIFLOOPVOL,
22811da177e4SLinus Torvalds 	YDSXGR_SPDIFOUTVOL,
22821da177e4SLinus Torvalds 	YDSXGR_ZVOUTVOL,
22831da177e4SLinus Torvalds 	YDSXGR_LEGACYOUTVOL,
22841da177e4SLinus Torvalds 	/* address bases */
22851da177e4SLinus Torvalds 	YDSXGR_PLAYCTRLBASE,
22861da177e4SLinus Torvalds 	YDSXGR_RECCTRLBASE,
22871da177e4SLinus Torvalds 	YDSXGR_EFFCTRLBASE,
22881da177e4SLinus Torvalds 	YDSXGR_WORKBASE,
22891da177e4SLinus Torvalds 	/* capture set up */
22901da177e4SLinus Torvalds 	YDSXGR_MAPOFREC,
22911da177e4SLinus Torvalds 	YDSXGR_RECFORMAT,
22921da177e4SLinus Torvalds 	YDSXGR_RECSLOTSR,
22931da177e4SLinus Torvalds 	YDSXGR_ADCFORMAT,
22941da177e4SLinus Torvalds 	YDSXGR_ADCSLOTSR,
22951da177e4SLinus Torvalds };
22961da177e4SLinus Torvalds #define YDSXGR_NUM_SAVED_REGS	ARRAY_SIZE(saved_regs_index)
22971da177e4SLinus Torvalds 
2298ded46235STakashi Iwai int snd_ymfpci_suspend(struct pci_dev *pci, pm_message_t state)
22991da177e4SLinus Torvalds {
2300ded46235STakashi Iwai 	struct snd_card *card = pci_get_drvdata(pci);
2301ded46235STakashi Iwai 	struct snd_ymfpci *chip = card->private_data;
23021da177e4SLinus Torvalds 	unsigned int i;
23031da177e4SLinus Torvalds 
2304ded46235STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
23051da177e4SLinus Torvalds 	snd_pcm_suspend_all(chip->pcm);
23061da177e4SLinus Torvalds 	snd_pcm_suspend_all(chip->pcm2);
23071da177e4SLinus Torvalds 	snd_pcm_suspend_all(chip->pcm_spdif);
23081da177e4SLinus Torvalds 	snd_pcm_suspend_all(chip->pcm_4ch);
23091da177e4SLinus Torvalds 	snd_ac97_suspend(chip->ac97);
23101da177e4SLinus Torvalds 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
23111da177e4SLinus Torvalds 		chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
23121da177e4SLinus Torvalds 	chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
2313*28aa165cSTakashi Iwai 	pci_read_config_word(chip->pci, PCIR_DSXG_LEGACY,
2314*28aa165cSTakashi Iwai 			     &chip->saved_dsxg_legacy);
2315*28aa165cSTakashi Iwai 	pci_read_config_word(chip->pci, PCIR_DSXG_ELEGACY,
2316*28aa165cSTakashi Iwai 			     &chip->saved_dsxg_elegacy);
23171da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
23184a3b6983STakashi Iwai 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
23191da177e4SLinus Torvalds 	snd_ymfpci_disable_dsp(chip);
2320ded46235STakashi Iwai 	pci_disable_device(pci);
2321ded46235STakashi Iwai 	pci_save_state(pci);
232230b35399STakashi Iwai 	pci_set_power_state(pci, pci_choose_state(pci, state));
23231da177e4SLinus Torvalds 	return 0;
23241da177e4SLinus Torvalds }
23251da177e4SLinus Torvalds 
2326ded46235STakashi Iwai int snd_ymfpci_resume(struct pci_dev *pci)
23271da177e4SLinus Torvalds {
2328ded46235STakashi Iwai 	struct snd_card *card = pci_get_drvdata(pci);
2329ded46235STakashi Iwai 	struct snd_ymfpci *chip = card->private_data;
23301da177e4SLinus Torvalds 	unsigned int i;
23311da177e4SLinus Torvalds 
233230b35399STakashi Iwai 	pci_set_power_state(pci, PCI_D0);
2333ded46235STakashi Iwai 	pci_restore_state(pci);
233430b35399STakashi Iwai 	if (pci_enable_device(pci) < 0) {
233530b35399STakashi Iwai 		printk(KERN_ERR "ymfpci: pci_enable_device failed, "
233630b35399STakashi Iwai 		       "disabling device\n");
233730b35399STakashi Iwai 		snd_card_disconnect(card);
233830b35399STakashi Iwai 		return -EIO;
233930b35399STakashi Iwai 	}
2340ded46235STakashi Iwai 	pci_set_master(pci);
2341ded46235STakashi Iwai 	snd_ymfpci_aclink_reset(pci);
23421da177e4SLinus Torvalds 	snd_ymfpci_codec_ready(chip, 0);
23431da177e4SLinus Torvalds 	snd_ymfpci_download_image(chip);
23441da177e4SLinus Torvalds 	udelay(100);
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
23471da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds 	snd_ac97_resume(chip->ac97);
23501da177e4SLinus Torvalds 
2351*28aa165cSTakashi Iwai 	pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY,
2352*28aa165cSTakashi Iwai 			      chip->saved_dsxg_legacy);
2353*28aa165cSTakashi Iwai 	pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY,
2354*28aa165cSTakashi Iwai 			      chip->saved_dsxg_elegacy);
2355*28aa165cSTakashi Iwai 
23561da177e4SLinus Torvalds 	/* start hw again */
23571da177e4SLinus Torvalds 	if (chip->start_count > 0) {
23581da177e4SLinus Torvalds 		spin_lock_irq(&chip->reg_lock);
23591da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
23601da177e4SLinus Torvalds 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
23611da177e4SLinus Torvalds 		spin_unlock_irq(&chip->reg_lock);
23621da177e4SLinus Torvalds 	}
2363ded46235STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
23641da177e4SLinus Torvalds 	return 0;
23651da177e4SLinus Torvalds }
23661da177e4SLinus Torvalds #endif /* CONFIG_PM */
23671da177e4SLinus Torvalds 
2368208a1b4cSTakashi Iwai int __devinit snd_ymfpci_create(struct snd_card *card,
23691da177e4SLinus Torvalds 				struct pci_dev * pci,
23701da177e4SLinus Torvalds 				unsigned short old_legacy_ctrl,
2371208a1b4cSTakashi Iwai 				struct snd_ymfpci ** rchip)
23721da177e4SLinus Torvalds {
2373208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
23741da177e4SLinus Torvalds 	int err;
2375208a1b4cSTakashi Iwai 	static struct snd_device_ops ops = {
23761da177e4SLinus Torvalds 		.dev_free =	snd_ymfpci_dev_free,
23771da177e4SLinus Torvalds 	};
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds 	*rchip = NULL;
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds 	/* enable PCI device */
23821da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
23831da177e4SLinus Torvalds 		return err;
23841da177e4SLinus Torvalds 
2385e560d8d8STakashi Iwai 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
23861da177e4SLinus Torvalds 	if (chip == NULL) {
23871da177e4SLinus Torvalds 		pci_disable_device(pci);
23881da177e4SLinus Torvalds 		return -ENOMEM;
23891da177e4SLinus Torvalds 	}
23901da177e4SLinus Torvalds 	chip->old_legacy_ctrl = old_legacy_ctrl;
23911da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
23921da177e4SLinus Torvalds 	spin_lock_init(&chip->voice_lock);
23931da177e4SLinus Torvalds 	init_waitqueue_head(&chip->interrupt_sleep);
23941da177e4SLinus Torvalds 	atomic_set(&chip->interrupt_sleep_count, 0);
23951da177e4SLinus Torvalds 	chip->card = card;
23961da177e4SLinus Torvalds 	chip->pci = pci;
23971da177e4SLinus Torvalds 	chip->irq = -1;
23981da177e4SLinus Torvalds 	chip->device_id = pci->device;
239944c10138SAuke Kok 	chip->rev = pci->revision;
24001da177e4SLinus Torvalds 	chip->reg_area_phys = pci_resource_start(pci, 0);
24011da177e4SLinus Torvalds 	chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
24021da177e4SLinus Torvalds 	pci_set_master(pci);
24039ed1261eSTeru KAMOGASHIRA 	chip->src441_used = -1;
24041da177e4SLinus Torvalds 
24051da177e4SLinus Torvalds 	if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
240699b359baSTakashi Iwai 		snd_printk(KERN_ERR "unable to grab memory region 0x%lx-0x%lx\n", chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
24071da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24081da177e4SLinus Torvalds 		return -EBUSY;
24091da177e4SLinus Torvalds 	}
2410437a5a46STakashi Iwai 	if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2411934c2b6dSTakashi Iwai 			KBUILD_MODNAME, chip)) {
241299b359baSTakashi Iwai 		snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
24131da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24141da177e4SLinus Torvalds 		return -EBUSY;
24151da177e4SLinus Torvalds 	}
24161da177e4SLinus Torvalds 	chip->irq = pci->irq;
24171da177e4SLinus Torvalds 
24181da177e4SLinus Torvalds 	snd_ymfpci_aclink_reset(pci);
24191da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0) < 0) {
24201da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24211da177e4SLinus Torvalds 		return -EIO;
24221da177e4SLinus Torvalds 	}
24231da177e4SLinus Torvalds 
2424102fa906SClemens Ladisch 	err = snd_ymfpci_request_firmware(chip);
2425102fa906SClemens Ladisch 	if (err < 0) {
2426102fa906SClemens Ladisch 		snd_printk(KERN_ERR "firmware request failed: %d\n", err);
2427102fa906SClemens Ladisch 		snd_ymfpci_free(chip);
2428102fa906SClemens Ladisch 		return err;
2429102fa906SClemens Ladisch 	}
24301da177e4SLinus Torvalds 	snd_ymfpci_download_image(chip);
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds 	udelay(100); /* seems we need a delay after downloading image.. */
24331da177e4SLinus Torvalds 
24341da177e4SLinus Torvalds 	if (snd_ymfpci_memalloc(chip) < 0) {
24351da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24361da177e4SLinus Torvalds 		return -EIO;
24371da177e4SLinus Torvalds 	}
24381da177e4SLinus Torvalds 
24391da177e4SLinus Torvalds 	if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
24401da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24411da177e4SLinus Torvalds 		return err;
24421da177e4SLinus Torvalds 	}
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds #ifdef CONFIG_PM
24451da177e4SLinus Torvalds 	chip->saved_regs = vmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32));
24461da177e4SLinus Torvalds 	if (chip->saved_regs == NULL) {
24471da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24481da177e4SLinus Torvalds 		return -ENOMEM;
24491da177e4SLinus Torvalds 	}
24501da177e4SLinus Torvalds #endif
24511da177e4SLinus Torvalds 
24521da177e4SLinus Torvalds 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
24531da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24541da177e4SLinus Torvalds 		return err;
24551da177e4SLinus Torvalds 	}
24561da177e4SLinus Torvalds 
24571da177e4SLinus Torvalds 	snd_ymfpci_proc_init(card, chip);
24581da177e4SLinus Torvalds 
24591da177e4SLinus Torvalds 	snd_card_set_dev(card, &pci->dev);
24601da177e4SLinus Torvalds 
24611da177e4SLinus Torvalds 	*rchip = chip;
24621da177e4SLinus Torvalds 	return 0;
24631da177e4SLinus Torvalds }
2464