xref: /openbmc/linux/sound/pci/ymfpci/ymfpci_main.c (revision e23e7a1436207217000c2854214bc908936af3cb)
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>
28b82a82d0SDavid Woodhouse #include <linux/mutex.h>
29da155d5bSPaul Gortmaker #include <linux/module.h>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <sound/core.h>
321da177e4SLinus Torvalds #include <sound/control.h>
331da177e4SLinus Torvalds #include <sound/info.h>
3433925186STakashi Iwai #include <sound/tlv.h>
3581fcb170STakashi Iwai #include "ymfpci.h"
361da177e4SLinus Torvalds #include <sound/asoundef.h>
371da177e4SLinus Torvalds #include <sound/mpu401.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <asm/io.h>
40102fa906SClemens Ladisch #include <asm/byteorder.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /*
431da177e4SLinus Torvalds  *  common I/O routines
441da177e4SLinus Torvalds  */
451da177e4SLinus Torvalds 
46208a1b4cSTakashi Iwai static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
471da177e4SLinus Torvalds 
48208a1b4cSTakashi Iwai static inline u8 snd_ymfpci_readb(struct snd_ymfpci *chip, u32 offset)
491da177e4SLinus Torvalds {
501da177e4SLinus Torvalds 	return readb(chip->reg_area_virt + offset);
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds 
53208a1b4cSTakashi Iwai static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
541da177e4SLinus Torvalds {
551da177e4SLinus Torvalds 	writeb(val, chip->reg_area_virt + offset);
561da177e4SLinus Torvalds }
571da177e4SLinus Torvalds 
58208a1b4cSTakashi Iwai static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	return readw(chip->reg_area_virt + offset);
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds 
63208a1b4cSTakashi Iwai static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
641da177e4SLinus Torvalds {
651da177e4SLinus Torvalds 	writew(val, chip->reg_area_virt + offset);
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
68208a1b4cSTakashi Iwai static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds 	return readl(chip->reg_area_virt + offset);
711da177e4SLinus Torvalds }
721da177e4SLinus Torvalds 
73208a1b4cSTakashi Iwai static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	writel(val, chip->reg_area_virt + offset);
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
78208a1b4cSTakashi Iwai static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
791da177e4SLinus Torvalds {
80ef21ca24SNishanth Aravamudan 	unsigned long end_time;
811da177e4SLinus Torvalds 	u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
821da177e4SLinus Torvalds 
83ef21ca24SNishanth Aravamudan 	end_time = jiffies + msecs_to_jiffies(750);
841da177e4SLinus Torvalds 	do {
851da177e4SLinus Torvalds 		if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
861da177e4SLinus Torvalds 			return 0;
878433a509SNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
88ef21ca24SNishanth Aravamudan 	} while (time_before(jiffies, end_time));
8999b359baSTakashi Iwai 	snd_printk(KERN_ERR "codec_ready: codec %i is not ready [0x%x]\n", secondary, snd_ymfpci_readw(chip, reg));
901da177e4SLinus Torvalds 	return -EBUSY;
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
93208a1b4cSTakashi Iwai static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
941da177e4SLinus Torvalds {
95208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
961da177e4SLinus Torvalds 	u32 cmd;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	snd_ymfpci_codec_ready(chip, 0);
991da177e4SLinus Torvalds 	cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
1001da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds 
103208a1b4cSTakashi Iwai static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
1041da177e4SLinus Torvalds {
105208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0))
1081da177e4SLinus Torvalds 		return ~0;
1091da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
1101da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0))
1111da177e4SLinus Torvalds 		return ~0;
1121da177e4SLinus Torvalds 	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
1131da177e4SLinus Torvalds 		int i;
1141da177e4SLinus Torvalds 		for (i = 0; i < 600; i++)
1151da177e4SLinus Torvalds 			snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
1161da177e4SLinus Torvalds 	}
1171da177e4SLinus Torvalds 	return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds /*
1211da177e4SLinus Torvalds  *  Misc routines
1221da177e4SLinus Torvalds  */
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds static u32 snd_ymfpci_calc_delta(u32 rate)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	switch (rate) {
1271da177e4SLinus Torvalds 	case 8000:	return 0x02aaab00;
1281da177e4SLinus Torvalds 	case 11025:	return 0x03accd00;
1291da177e4SLinus Torvalds 	case 16000:	return 0x05555500;
1301da177e4SLinus Torvalds 	case 22050:	return 0x07599a00;
1311da177e4SLinus Torvalds 	case 32000:	return 0x0aaaab00;
1321da177e4SLinus Torvalds 	case 44100:	return 0x0eb33300;
1331da177e4SLinus Torvalds 	default:	return ((rate << 16) / 375) << 5;
1341da177e4SLinus Torvalds 	}
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds static u32 def_rate[8] = {
1381da177e4SLinus Torvalds 	100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
1391da177e4SLinus Torvalds };
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds static u32 snd_ymfpci_calc_lpfK(u32 rate)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	u32 i;
1441da177e4SLinus Torvalds 	static u32 val[8] = {
1451da177e4SLinus Torvalds 		0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
1461da177e4SLinus Torvalds 		0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
1471da177e4SLinus Torvalds 	};
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	if (rate == 44100)
1501da177e4SLinus Torvalds 		return 0x40000000;	/* FIXME: What's the right value? */
1511da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
1521da177e4SLinus Torvalds 		if (rate <= def_rate[i])
1531da177e4SLinus Torvalds 			return val[i];
1541da177e4SLinus Torvalds 	return val[0];
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds static u32 snd_ymfpci_calc_lpfQ(u32 rate)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	u32 i;
1601da177e4SLinus Torvalds 	static u32 val[8] = {
1611da177e4SLinus Torvalds 		0x35280000, 0x34A70000, 0x32020000, 0x31770000,
1621da177e4SLinus Torvalds 		0x31390000, 0x31C90000, 0x33D00000, 0x40000000
1631da177e4SLinus Torvalds 	};
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	if (rate == 44100)
1661da177e4SLinus Torvalds 		return 0x370A0000;
1671da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
1681da177e4SLinus Torvalds 		if (rate <= def_rate[i])
1691da177e4SLinus Torvalds 			return val[i];
1701da177e4SLinus Torvalds 	return val[0];
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds /*
1741da177e4SLinus Torvalds  *  Hardware start management
1751da177e4SLinus Torvalds  */
1761da177e4SLinus Torvalds 
177208a1b4cSTakashi Iwai static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	unsigned long flags;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
1821da177e4SLinus Torvalds 	if (chip->start_count++ > 0)
1831da177e4SLinus Torvalds 		goto __end;
1841da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE,
1851da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
1861da177e4SLinus Torvalds 	chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
1871da177e4SLinus Torvalds       __end:
1881da177e4SLinus Torvalds       	spin_unlock_irqrestore(&chip->reg_lock, flags);
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
191208a1b4cSTakashi Iwai static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	unsigned long flags;
1941da177e4SLinus Torvalds 	long timeout = 1000;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
1971da177e4SLinus Torvalds 	if (--chip->start_count > 0)
1981da177e4SLinus Torvalds 		goto __end;
1991da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE,
2001da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
2011da177e4SLinus Torvalds 	while (timeout-- > 0) {
2021da177e4SLinus Torvalds 		if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
2031da177e4SLinus Torvalds 			break;
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 	if (atomic_read(&chip->interrupt_sleep_count)) {
2061da177e4SLinus Torvalds 		atomic_set(&chip->interrupt_sleep_count, 0);
2071da177e4SLinus Torvalds 		wake_up(&chip->interrupt_sleep);
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds       __end:
2101da177e4SLinus Torvalds       	spin_unlock_irqrestore(&chip->reg_lock, flags);
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds /*
2141da177e4SLinus Torvalds  *  Playback voice management
2151da177e4SLinus Torvalds  */
2161da177e4SLinus Torvalds 
217208a1b4cSTakashi Iwai static int voice_alloc(struct snd_ymfpci *chip,
218208a1b4cSTakashi Iwai 		       enum snd_ymfpci_voice_type type, int pair,
219208a1b4cSTakashi Iwai 		       struct snd_ymfpci_voice **rvoice)
2201da177e4SLinus Torvalds {
221208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice, *voice2;
2221da177e4SLinus Torvalds 	int idx;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	*rvoice = NULL;
2251da177e4SLinus Torvalds 	for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
2261da177e4SLinus Torvalds 		voice = &chip->voices[idx];
2271da177e4SLinus Torvalds 		voice2 = pair ? &chip->voices[idx+1] : NULL;
2281da177e4SLinus Torvalds 		if (voice->use || (voice2 && voice2->use))
2291da177e4SLinus Torvalds 			continue;
2301da177e4SLinus Torvalds 		voice->use = 1;
2311da177e4SLinus Torvalds 		if (voice2)
2321da177e4SLinus Torvalds 			voice2->use = 1;
2331da177e4SLinus Torvalds 		switch (type) {
2341da177e4SLinus Torvalds 		case YMFPCI_PCM:
2351da177e4SLinus Torvalds 			voice->pcm = 1;
2361da177e4SLinus Torvalds 			if (voice2)
2371da177e4SLinus Torvalds 				voice2->pcm = 1;
2381da177e4SLinus Torvalds 			break;
2391da177e4SLinus Torvalds 		case YMFPCI_SYNTH:
2401da177e4SLinus Torvalds 			voice->synth = 1;
2411da177e4SLinus Torvalds 			break;
2421da177e4SLinus Torvalds 		case YMFPCI_MIDI:
2431da177e4SLinus Torvalds 			voice->midi = 1;
2441da177e4SLinus Torvalds 			break;
2451da177e4SLinus Torvalds 		}
2461da177e4SLinus Torvalds 		snd_ymfpci_hw_start(chip);
2471da177e4SLinus Torvalds 		if (voice2)
2481da177e4SLinus Torvalds 			snd_ymfpci_hw_start(chip);
2491da177e4SLinus Torvalds 		*rvoice = voice;
2501da177e4SLinus Torvalds 		return 0;
2511da177e4SLinus Torvalds 	}
2521da177e4SLinus Torvalds 	return -ENOMEM;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
255208a1b4cSTakashi Iwai static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
256208a1b4cSTakashi Iwai 				  enum snd_ymfpci_voice_type type, int pair,
257208a1b4cSTakashi Iwai 				  struct snd_ymfpci_voice **rvoice)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	unsigned long flags;
2601da177e4SLinus Torvalds 	int result;
2611da177e4SLinus Torvalds 
262da3cec35STakashi Iwai 	if (snd_BUG_ON(!rvoice))
263da3cec35STakashi Iwai 		return -EINVAL;
264da3cec35STakashi Iwai 	if (snd_BUG_ON(pair && type != YMFPCI_PCM))
265da3cec35STakashi Iwai 		return -EINVAL;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->voice_lock, flags);
2681da177e4SLinus Torvalds 	for (;;) {
2691da177e4SLinus Torvalds 		result = voice_alloc(chip, type, pair, rvoice);
2701da177e4SLinus Torvalds 		if (result == 0 || type != YMFPCI_PCM)
2711da177e4SLinus Torvalds 			break;
2721da177e4SLinus Torvalds 		/* TODO: synth/midi voice deallocation */
2731da177e4SLinus Torvalds 		break;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->voice_lock, flags);
2761da177e4SLinus Torvalds 	return result;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
279208a1b4cSTakashi Iwai static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	unsigned long flags;
2821da177e4SLinus Torvalds 
283da3cec35STakashi Iwai 	if (snd_BUG_ON(!pvoice))
284da3cec35STakashi Iwai 		return -EINVAL;
2851da177e4SLinus Torvalds 	snd_ymfpci_hw_stop(chip);
2861da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->voice_lock, flags);
2879ed1261eSTeru KAMOGASHIRA 	if (pvoice->number == chip->src441_used) {
2889ed1261eSTeru KAMOGASHIRA 		chip->src441_used = -1;
2899ed1261eSTeru KAMOGASHIRA 		pvoice->ypcm->use_441_slot = 0;
2909ed1261eSTeru KAMOGASHIRA 	}
2911da177e4SLinus Torvalds 	pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
2921da177e4SLinus Torvalds 	pvoice->ypcm = NULL;
2931da177e4SLinus Torvalds 	pvoice->interrupt = NULL;
2941da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->voice_lock, flags);
2951da177e4SLinus Torvalds 	return 0;
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds /*
2991da177e4SLinus Torvalds  *  PCM part
3001da177e4SLinus Torvalds  */
3011da177e4SLinus Torvalds 
302208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
3031da177e4SLinus Torvalds {
304208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
3051da177e4SLinus Torvalds 	u32 pos, delta;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	if ((ypcm = voice->ypcm) == NULL)
3081da177e4SLinus Torvalds 		return;
3091da177e4SLinus Torvalds 	if (ypcm->substream == NULL)
3101da177e4SLinus Torvalds 		return;
3111da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3121da177e4SLinus Torvalds 	if (ypcm->running) {
3131da177e4SLinus Torvalds 		pos = le32_to_cpu(voice->bank[chip->active_bank].start);
3141da177e4SLinus Torvalds 		if (pos < ypcm->last_pos)
3151da177e4SLinus Torvalds 			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
3161da177e4SLinus Torvalds 		else
3171da177e4SLinus Torvalds 			delta = pos - ypcm->last_pos;
3181da177e4SLinus Torvalds 		ypcm->period_pos += delta;
3191da177e4SLinus Torvalds 		ypcm->last_pos = pos;
3201da177e4SLinus Torvalds 		if (ypcm->period_pos >= ypcm->period_size) {
321ee419653STakashi Iwai 			/*
322ee419653STakashi Iwai 			printk(KERN_DEBUG
323ee419653STakashi Iwai 			       "done - active_bank = 0x%x, start = 0x%x\n",
324ee419653STakashi Iwai 			       chip->active_bank,
325ee419653STakashi Iwai 			       voice->bank[chip->active_bank].start);
326ee419653STakashi Iwai 			*/
3271da177e4SLinus Torvalds 			ypcm->period_pos %= ypcm->period_size;
3281da177e4SLinus Torvalds 			spin_unlock(&chip->reg_lock);
3291da177e4SLinus Torvalds 			snd_pcm_period_elapsed(ypcm->substream);
3301da177e4SLinus Torvalds 			spin_lock(&chip->reg_lock);
3311da177e4SLinus Torvalds 		}
3329bcf6551SClemens Ladisch 
3339bcf6551SClemens Ladisch 		if (unlikely(ypcm->update_pcm_vol)) {
3349bcf6551SClemens Ladisch 			unsigned int subs = ypcm->substream->number;
3359bcf6551SClemens Ladisch 			unsigned int next_bank = 1 - chip->active_bank;
336208a1b4cSTakashi Iwai 			struct snd_ymfpci_playback_bank *bank;
3379bcf6551SClemens Ladisch 			u32 volume;
3389bcf6551SClemens Ladisch 
3399bcf6551SClemens Ladisch 			bank = &voice->bank[next_bank];
3409bcf6551SClemens Ladisch 			volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
3419bcf6551SClemens Ladisch 			bank->left_gain_end = volume;
3429bcf6551SClemens Ladisch 			if (ypcm->output_rear)
3439bcf6551SClemens Ladisch 				bank->eff2_gain_end = volume;
3449bcf6551SClemens Ladisch 			if (ypcm->voices[1])
3459bcf6551SClemens Ladisch 				bank = &ypcm->voices[1]->bank[next_bank];
3469bcf6551SClemens Ladisch 			volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
3479bcf6551SClemens Ladisch 			bank->right_gain_end = volume;
3489bcf6551SClemens Ladisch 			if (ypcm->output_rear)
3499bcf6551SClemens Ladisch 				bank->eff3_gain_end = volume;
3509bcf6551SClemens Ladisch 			ypcm->update_pcm_vol--;
3519bcf6551SClemens Ladisch 		}
3521da177e4SLinus Torvalds 	}
3531da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
356208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
3571da177e4SLinus Torvalds {
358208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
359208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
360208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ypcm->chip;
3611da177e4SLinus Torvalds 	u32 pos, delta;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3641da177e4SLinus Torvalds 	if (ypcm->running) {
3651da177e4SLinus Torvalds 		pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
3661da177e4SLinus Torvalds 		if (pos < ypcm->last_pos)
3671da177e4SLinus Torvalds 			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
3681da177e4SLinus Torvalds 		else
3691da177e4SLinus Torvalds 			delta = pos - ypcm->last_pos;
3701da177e4SLinus Torvalds 		ypcm->period_pos += delta;
3711da177e4SLinus Torvalds 		ypcm->last_pos = pos;
3721da177e4SLinus Torvalds 		if (ypcm->period_pos >= ypcm->period_size) {
3731da177e4SLinus Torvalds 			ypcm->period_pos %= ypcm->period_size;
374ee419653STakashi Iwai 			/*
375ee419653STakashi Iwai 			printk(KERN_DEBUG
376ee419653STakashi Iwai 			       "done - active_bank = 0x%x, start = 0x%x\n",
377ee419653STakashi Iwai 			       chip->active_bank,
378ee419653STakashi Iwai 			       voice->bank[chip->active_bank].start);
379ee419653STakashi Iwai 			*/
3801da177e4SLinus Torvalds 			spin_unlock(&chip->reg_lock);
3811da177e4SLinus Torvalds 			snd_pcm_period_elapsed(substream);
3821da177e4SLinus Torvalds 			spin_lock(&chip->reg_lock);
3831da177e4SLinus Torvalds 		}
3841da177e4SLinus Torvalds 	}
3851da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
388208a1b4cSTakashi Iwai static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
3891da177e4SLinus Torvalds 				       int cmd)
3901da177e4SLinus Torvalds {
391208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
392208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
393177a7cdbSClemens Ladisch 	struct snd_kcontrol *kctl = NULL;
3941da177e4SLinus Torvalds 	int result = 0;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3971da177e4SLinus Torvalds 	if (ypcm->voices[0] == NULL) {
3981da177e4SLinus Torvalds 		result = -EINVAL;
3991da177e4SLinus Torvalds 		goto __unlock;
4001da177e4SLinus Torvalds 	}
4011da177e4SLinus Torvalds 	switch (cmd) {
4021da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4031da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
4041da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_RESUME:
4051da177e4SLinus Torvalds 		chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
4069ed1261eSTeru KAMOGASHIRA 		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
4071da177e4SLinus Torvalds 			chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
4081da177e4SLinus Torvalds 		ypcm->running = 1;
4091da177e4SLinus Torvalds 		break;
4101da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
411177a7cdbSClemens Ladisch 		if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
412177a7cdbSClemens Ladisch 			kctl = chip->pcm_mixer[substream->number].ctl;
413177a7cdbSClemens Ladisch 			kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
414177a7cdbSClemens Ladisch 		}
415177a7cdbSClemens Ladisch 		/* fall through */
4161da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
4171da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_SUSPEND:
4181da177e4SLinus Torvalds 		chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
4199ed1261eSTeru KAMOGASHIRA 		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
4201da177e4SLinus Torvalds 			chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
4211da177e4SLinus Torvalds 		ypcm->running = 0;
4221da177e4SLinus Torvalds 		break;
4231da177e4SLinus Torvalds 	default:
4241da177e4SLinus Torvalds 		result = -EINVAL;
4251da177e4SLinus Torvalds 		break;
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds       __unlock:
4281da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
429177a7cdbSClemens Ladisch 	if (kctl)
430177a7cdbSClemens Ladisch 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
4311da177e4SLinus Torvalds 	return result;
4321da177e4SLinus Torvalds }
433208a1b4cSTakashi Iwai static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
4341da177e4SLinus Torvalds 				      int cmd)
4351da177e4SLinus Torvalds {
436208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
437208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
4381da177e4SLinus Torvalds 	int result = 0;
4391da177e4SLinus Torvalds 	u32 tmp;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
4421da177e4SLinus Torvalds 	switch (cmd) {
4431da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4441da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
4451da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_RESUME:
4461da177e4SLinus Torvalds 		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
4471da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
4481da177e4SLinus Torvalds 		ypcm->running = 1;
4491da177e4SLinus Torvalds 		break;
4501da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4511da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
4521da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_SUSPEND:
4531da177e4SLinus Torvalds 		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
4541da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
4551da177e4SLinus Torvalds 		ypcm->running = 0;
4561da177e4SLinus Torvalds 		break;
4571da177e4SLinus Torvalds 	default:
4581da177e4SLinus Torvalds 		result = -EINVAL;
4591da177e4SLinus Torvalds 		break;
4601da177e4SLinus Torvalds 	}
4611da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4621da177e4SLinus Torvalds 	return result;
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
465208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
4661da177e4SLinus Torvalds {
4671da177e4SLinus Torvalds 	int err;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	if (ypcm->voices[1] != NULL && voices < 2) {
4701da177e4SLinus Torvalds 		snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
4711da177e4SLinus Torvalds 		ypcm->voices[1] = NULL;
4721da177e4SLinus Torvalds 	}
4731da177e4SLinus Torvalds 	if (voices == 1 && ypcm->voices[0] != NULL)
4741da177e4SLinus Torvalds 		return 0;		/* already allocated */
4751da177e4SLinus Torvalds 	if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
4761da177e4SLinus Torvalds 		return 0;		/* already allocated */
4771da177e4SLinus Torvalds 	if (voices > 1) {
4781da177e4SLinus Torvalds 		if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
4791da177e4SLinus Torvalds 			snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
4801da177e4SLinus Torvalds 			ypcm->voices[0] = NULL;
4811da177e4SLinus Torvalds 		}
4821da177e4SLinus Torvalds 	}
4831da177e4SLinus Torvalds 	err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
4841da177e4SLinus Torvalds 	if (err < 0)
4851da177e4SLinus Torvalds 		return err;
4861da177e4SLinus Torvalds 	ypcm->voices[0]->ypcm = ypcm;
4871da177e4SLinus Torvalds 	ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
4881da177e4SLinus Torvalds 	if (voices > 1) {
4891da177e4SLinus Torvalds 		ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
4901da177e4SLinus Torvalds 		ypcm->voices[1]->ypcm = ypcm;
4911da177e4SLinus Torvalds 	}
4921da177e4SLinus Torvalds 	return 0;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
495208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
496208a1b4cSTakashi Iwai 				      struct snd_pcm_runtime *runtime,
4979bcf6551SClemens Ladisch 				      int has_pcm_volume)
4981da177e4SLinus Torvalds {
499208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
5001da177e4SLinus Torvalds 	u32 format;
5019bcf6551SClemens Ladisch 	u32 delta = snd_ymfpci_calc_delta(runtime->rate);
5029bcf6551SClemens Ladisch 	u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
5039bcf6551SClemens Ladisch 	u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
504208a1b4cSTakashi Iwai 	struct snd_ymfpci_playback_bank *bank;
5051da177e4SLinus Torvalds 	unsigned int nbank;
5069bcf6551SClemens Ladisch 	u32 vol_left, vol_right;
5079bcf6551SClemens Ladisch 	u8 use_left, use_right;
5089ed1261eSTeru KAMOGASHIRA 	unsigned long flags;
5091da177e4SLinus Torvalds 
510da3cec35STakashi Iwai 	if (snd_BUG_ON(!voice))
511da3cec35STakashi Iwai 		return;
5129bcf6551SClemens Ladisch 	if (runtime->channels == 1) {
5139bcf6551SClemens Ladisch 		use_left = 1;
5149bcf6551SClemens Ladisch 		use_right = 1;
5159bcf6551SClemens Ladisch 	} else {
5169bcf6551SClemens Ladisch 		use_left = (voiceidx & 1) == 0;
5179bcf6551SClemens Ladisch 		use_right = !use_left;
5189bcf6551SClemens Ladisch 	}
5199bcf6551SClemens Ladisch 	if (has_pcm_volume) {
5209bcf6551SClemens Ladisch 		vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
5219bcf6551SClemens Ladisch 				       [ypcm->substream->number].left << 15);
5229bcf6551SClemens Ladisch 		vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
5239bcf6551SClemens Ladisch 					[ypcm->substream->number].right << 15);
5249bcf6551SClemens Ladisch 	} else {
5259bcf6551SClemens Ladisch 		vol_left = cpu_to_le32(0x40000000);
5269bcf6551SClemens Ladisch 		vol_right = cpu_to_le32(0x40000000);
5279bcf6551SClemens Ladisch 	}
5289ed1261eSTeru KAMOGASHIRA 	spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
5299bcf6551SClemens Ladisch 	format = runtime->channels == 2 ? 0x00010000 : 0;
5309bcf6551SClemens Ladisch 	if (snd_pcm_format_width(runtime->format) == 8)
5319bcf6551SClemens Ladisch 		format |= 0x80000000;
5329ed1261eSTeru KAMOGASHIRA 	else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
5339ed1261eSTeru KAMOGASHIRA 		 runtime->rate == 44100 && runtime->channels == 2 &&
5349ed1261eSTeru KAMOGASHIRA 		 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
5359ed1261eSTeru KAMOGASHIRA 				   ypcm->chip->src441_used == voice->number)) {
5369ed1261eSTeru KAMOGASHIRA 		ypcm->chip->src441_used = voice->number;
5379ed1261eSTeru KAMOGASHIRA 		ypcm->use_441_slot = 1;
5389ed1261eSTeru KAMOGASHIRA 		format |= 0x10000000;
5399ed1261eSTeru KAMOGASHIRA 	}
5409ed1261eSTeru KAMOGASHIRA 	if (ypcm->chip->src441_used == voice->number &&
5419ed1261eSTeru KAMOGASHIRA 	    (format & 0x10000000) == 0) {
5429ed1261eSTeru KAMOGASHIRA 		ypcm->chip->src441_used = -1;
5439ed1261eSTeru KAMOGASHIRA 		ypcm->use_441_slot = 0;
5449ed1261eSTeru KAMOGASHIRA 	}
5459bcf6551SClemens Ladisch 	if (runtime->channels == 2 && (voiceidx & 1) != 0)
5469bcf6551SClemens Ladisch 		format |= 1;
5479ed1261eSTeru KAMOGASHIRA 	spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
5481da177e4SLinus Torvalds 	for (nbank = 0; nbank < 2; nbank++) {
5491da177e4SLinus Torvalds 		bank = &voice->bank[nbank];
5509bcf6551SClemens Ladisch 		memset(bank, 0, sizeof(*bank));
5511da177e4SLinus Torvalds 		bank->format = cpu_to_le32(format);
5529bcf6551SClemens Ladisch 		bank->base = cpu_to_le32(runtime->dma_addr);
5539bcf6551SClemens Ladisch 		bank->loop_end = cpu_to_le32(ypcm->buffer_size);
5541da177e4SLinus Torvalds 		bank->lpfQ = cpu_to_le32(lpfQ);
5551da177e4SLinus Torvalds 		bank->delta =
5561da177e4SLinus Torvalds 		bank->delta_end = cpu_to_le32(delta);
5571da177e4SLinus Torvalds 		bank->lpfK =
5581da177e4SLinus Torvalds 		bank->lpfK_end = cpu_to_le32(lpfK);
5599bcf6551SClemens Ladisch 		bank->eg_gain =
5609bcf6551SClemens Ladisch 		bank->eg_gain_end = cpu_to_le32(0x40000000);
5611da177e4SLinus Torvalds 
5629bcf6551SClemens Ladisch 		if (ypcm->output_front) {
5639bcf6551SClemens Ladisch 			if (use_left) {
5641da177e4SLinus Torvalds 				bank->left_gain =
5659bcf6551SClemens Ladisch 				bank->left_gain_end = vol_left;
5669bcf6551SClemens Ladisch 			}
5679bcf6551SClemens Ladisch 			if (use_right) {
5681da177e4SLinus Torvalds 				bank->right_gain =
5699bcf6551SClemens Ladisch 				bank->right_gain_end = vol_right;
5709bcf6551SClemens Ladisch 			}
5719bcf6551SClemens Ladisch 		}
5729bcf6551SClemens Ladisch 		if (ypcm->output_rear) {
5735a25c5cfSJaroslav Kysela 		        if (!ypcm->swap_rear) {
5745a25c5cfSJaroslav Kysela         			if (use_left) {
5755a25c5cfSJaroslav Kysela         				bank->eff2_gain =
5765a25c5cfSJaroslav Kysela         				bank->eff2_gain_end = vol_left;
5775a25c5cfSJaroslav Kysela         			}
5785a25c5cfSJaroslav Kysela         			if (use_right) {
5795a25c5cfSJaroslav Kysela         				bank->eff3_gain =
5805a25c5cfSJaroslav Kysela         				bank->eff3_gain_end = vol_right;
5815a25c5cfSJaroslav Kysela         			}
5825a25c5cfSJaroslav Kysela 		        } else {
583153abaa4SClemens Ladisch         			/* The SPDIF out channels seem to be swapped, so we have
584153abaa4SClemens Ladisch         			 * to swap them here, too.  The rear analog out channels
585153abaa4SClemens Ladisch         			 * will be wrong, but otherwise AC3 would not work.
586153abaa4SClemens Ladisch         			 */
5879bcf6551SClemens Ladisch         			if (use_left) {
588153abaa4SClemens Ladisch         				bank->eff3_gain =
589153abaa4SClemens Ladisch         				bank->eff3_gain_end = vol_left;
5909bcf6551SClemens Ladisch         			}
5919bcf6551SClemens Ladisch         			if (use_right) {
592153abaa4SClemens Ladisch         				bank->eff2_gain =
593153abaa4SClemens Ladisch         				bank->eff2_gain_end = vol_right;
5941da177e4SLinus Torvalds         			}
5951da177e4SLinus Torvalds         		}
5961da177e4SLinus Torvalds                 }
5971da177e4SLinus Torvalds 	}
5985a25c5cfSJaroslav Kysela }
5991da177e4SLinus Torvalds 
600*e23e7a14SBill Pemberton static int snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
6011da177e4SLinus Torvalds {
6021da177e4SLinus Torvalds 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
6031da177e4SLinus Torvalds 				4096, &chip->ac3_tmp_base) < 0)
6041da177e4SLinus Torvalds 		return -ENOMEM;
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	chip->bank_effect[3][0]->base =
6071da177e4SLinus Torvalds 	chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
6081da177e4SLinus Torvalds 	chip->bank_effect[3][0]->loop_end =
6091da177e4SLinus Torvalds 	chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
6101da177e4SLinus Torvalds 	chip->bank_effect[4][0]->base =
6111da177e4SLinus Torvalds 	chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
6121da177e4SLinus Torvalds 	chip->bank_effect[4][0]->loop_end =
6131da177e4SLinus Torvalds 	chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
6161da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
6171da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
6181da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
6191da177e4SLinus Torvalds 	return 0;
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
622208a1b4cSTakashi Iwai static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
6231da177e4SLinus Torvalds {
6241da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
6251da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
6261da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
6271da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
6281da177e4SLinus Torvalds 	// snd_ymfpci_irq_wait(chip);
6291da177e4SLinus Torvalds 	if (chip->ac3_tmp_base.area) {
6301da177e4SLinus Torvalds 		snd_dma_free_pages(&chip->ac3_tmp_base);
6311da177e4SLinus Torvalds 		chip->ac3_tmp_base.area = NULL;
6321da177e4SLinus Torvalds 	}
6331da177e4SLinus Torvalds 	return 0;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
636208a1b4cSTakashi Iwai static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
637208a1b4cSTakashi Iwai 					 struct snd_pcm_hw_params *hw_params)
6381da177e4SLinus Torvalds {
639208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
640208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
6411da177e4SLinus Torvalds 	int err;
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
6441da177e4SLinus Torvalds 		return err;
6451da177e4SLinus Torvalds 	if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
6461da177e4SLinus Torvalds 		return err;
6471da177e4SLinus Torvalds 	return 0;
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds 
650208a1b4cSTakashi Iwai static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
6511da177e4SLinus Torvalds {
652208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
653208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
654208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	if (runtime->private_data == NULL)
6571da177e4SLinus Torvalds 		return 0;
6581da177e4SLinus Torvalds 	ypcm = runtime->private_data;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	/* wait, until the PCI operations are not finished */
6611da177e4SLinus Torvalds 	snd_ymfpci_irq_wait(chip);
6621da177e4SLinus Torvalds 	snd_pcm_lib_free_pages(substream);
6631da177e4SLinus Torvalds 	if (ypcm->voices[1]) {
6641da177e4SLinus Torvalds 		snd_ymfpci_voice_free(chip, ypcm->voices[1]);
6651da177e4SLinus Torvalds 		ypcm->voices[1] = NULL;
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 	if (ypcm->voices[0]) {
6681da177e4SLinus Torvalds 		snd_ymfpci_voice_free(chip, ypcm->voices[0]);
6691da177e4SLinus Torvalds 		ypcm->voices[0] = NULL;
6701da177e4SLinus Torvalds 	}
6711da177e4SLinus Torvalds 	return 0;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
674208a1b4cSTakashi Iwai static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
6751da177e4SLinus Torvalds {
676208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
677208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
678208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
679177a7cdbSClemens Ladisch 	struct snd_kcontrol *kctl;
6801da177e4SLinus Torvalds 	unsigned int nvoice;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	ypcm->period_size = runtime->period_size;
6831da177e4SLinus Torvalds 	ypcm->buffer_size = runtime->buffer_size;
6841da177e4SLinus Torvalds 	ypcm->period_pos = 0;
6851da177e4SLinus Torvalds 	ypcm->last_pos = 0;
6861da177e4SLinus Torvalds 	for (nvoice = 0; nvoice < runtime->channels; nvoice++)
6879bcf6551SClemens Ladisch 		snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
6889bcf6551SClemens Ladisch 					  substream->pcm == chip->pcm);
689177a7cdbSClemens Ladisch 
690177a7cdbSClemens Ladisch 	if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
691177a7cdbSClemens Ladisch 		kctl = chip->pcm_mixer[substream->number].ctl;
692177a7cdbSClemens Ladisch 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
693177a7cdbSClemens Ladisch 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
694177a7cdbSClemens Ladisch 	}
6951da177e4SLinus Torvalds 	return 0;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
698208a1b4cSTakashi Iwai static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
699208a1b4cSTakashi Iwai 					struct snd_pcm_hw_params *hw_params)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
704208a1b4cSTakashi Iwai static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
7051da177e4SLinus Torvalds {
706208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	/* wait, until the PCI operations are not finished */
7091da177e4SLinus Torvalds 	snd_ymfpci_irq_wait(chip);
7101da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
713208a1b4cSTakashi Iwai static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
7141da177e4SLinus Torvalds {
715208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
716208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
717208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
718208a1b4cSTakashi Iwai 	struct snd_ymfpci_capture_bank * bank;
7191da177e4SLinus Torvalds 	int nbank;
7201da177e4SLinus Torvalds 	u32 rate, format;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	ypcm->period_size = runtime->period_size;
7231da177e4SLinus Torvalds 	ypcm->buffer_size = runtime->buffer_size;
7241da177e4SLinus Torvalds 	ypcm->period_pos = 0;
7251da177e4SLinus Torvalds 	ypcm->last_pos = 0;
7261da177e4SLinus Torvalds 	ypcm->shift = 0;
7271da177e4SLinus Torvalds 	rate = ((48000 * 4096) / runtime->rate) - 1;
7281da177e4SLinus Torvalds 	format = 0;
7291da177e4SLinus Torvalds 	if (runtime->channels == 2) {
7301da177e4SLinus Torvalds 		format |= 2;
7311da177e4SLinus Torvalds 		ypcm->shift++;
7321da177e4SLinus Torvalds 	}
7331da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 8)
7341da177e4SLinus Torvalds 		format |= 1;
7351da177e4SLinus Torvalds 	else
7361da177e4SLinus Torvalds 		ypcm->shift++;
7371da177e4SLinus Torvalds 	switch (ypcm->capture_bank_number) {
7381da177e4SLinus Torvalds 	case 0:
7391da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
7401da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
7411da177e4SLinus Torvalds 		break;
7421da177e4SLinus Torvalds 	case 1:
7431da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
7441da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
7451da177e4SLinus Torvalds 		break;
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 	for (nbank = 0; nbank < 2; nbank++) {
7481da177e4SLinus Torvalds 		bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
7491da177e4SLinus Torvalds 		bank->base = cpu_to_le32(runtime->dma_addr);
7501da177e4SLinus Torvalds 		bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
7511da177e4SLinus Torvalds 		bank->start = 0;
7521da177e4SLinus Torvalds 		bank->num_of_loops = 0;
7531da177e4SLinus Torvalds 	}
7541da177e4SLinus Torvalds 	return 0;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
757208a1b4cSTakashi Iwai static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
7581da177e4SLinus Torvalds {
759208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
760208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
761208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
762208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice = ypcm->voices[0];
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	if (!(ypcm->running && voice))
7651da177e4SLinus Torvalds 		return 0;
7661da177e4SLinus Torvalds 	return le32_to_cpu(voice->bank[chip->active_bank].start);
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
769208a1b4cSTakashi Iwai static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
7701da177e4SLinus Torvalds {
771208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
772208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
773208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	if (!ypcm->running)
7761da177e4SLinus Torvalds 		return 0;
7771da177e4SLinus Torvalds 	return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
780208a1b4cSTakashi Iwai static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
7811da177e4SLinus Torvalds {
7821da177e4SLinus Torvalds 	wait_queue_t wait;
7831da177e4SLinus Torvalds 	int loops = 4;
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	while (loops-- > 0) {
7861da177e4SLinus Torvalds 		if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
7871da177e4SLinus Torvalds 		 	continue;
7881da177e4SLinus Torvalds 		init_waitqueue_entry(&wait, current);
7891da177e4SLinus Torvalds 		add_wait_queue(&chip->interrupt_sleep, &wait);
7901da177e4SLinus Torvalds 		atomic_inc(&chip->interrupt_sleep_count);
7918433a509SNishanth Aravamudan 		schedule_timeout_uninterruptible(msecs_to_jiffies(50));
7921da177e4SLinus Torvalds 		remove_wait_queue(&chip->interrupt_sleep, &wait);
7931da177e4SLinus Torvalds 	}
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7967d12e780SDavid Howells static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
7971da177e4SLinus Torvalds {
798208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = dev_id;
7991da177e4SLinus Torvalds 	u32 status, nvoice, mode;
800208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
8031da177e4SLinus Torvalds 	if (status & 0x80000000) {
8041da177e4SLinus Torvalds 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
8051da177e4SLinus Torvalds 		spin_lock(&chip->voice_lock);
8061da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
8071da177e4SLinus Torvalds 			voice = &chip->voices[nvoice];
8081da177e4SLinus Torvalds 			if (voice->interrupt)
8091da177e4SLinus Torvalds 				voice->interrupt(chip, voice);
8101da177e4SLinus Torvalds 		}
8111da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
8121da177e4SLinus Torvalds 			if (chip->capture_substream[nvoice])
8131da177e4SLinus Torvalds 				snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
8141da177e4SLinus Torvalds 		}
8151da177e4SLinus Torvalds #if 0
8161da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
8171da177e4SLinus Torvalds 			if (chip->effect_substream[nvoice])
8181da177e4SLinus Torvalds 				snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
8191da177e4SLinus Torvalds 		}
8201da177e4SLinus Torvalds #endif
8211da177e4SLinus Torvalds 		spin_unlock(&chip->voice_lock);
8221da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
8231da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
8241da177e4SLinus Torvalds 		mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
8251da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
8261da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 		if (atomic_read(&chip->interrupt_sleep_count)) {
8291da177e4SLinus Torvalds 			atomic_set(&chip->interrupt_sleep_count, 0);
8301da177e4SLinus Torvalds 			wake_up(&chip->interrupt_sleep);
8311da177e4SLinus Torvalds 		}
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
8351da177e4SLinus Torvalds 	if (status & 1) {
8361da177e4SLinus Torvalds 		if (chip->timer)
8376e2efaacSClemens Ladisch 			snd_timer_interrupt(chip->timer, chip->timer_ticks);
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	if (chip->rawmidi)
8427d12e780SDavid Howells 		snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
8431da177e4SLinus Torvalds 	return IRQ_HANDLED;
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
846208a1b4cSTakashi Iwai static struct snd_pcm_hardware snd_ymfpci_playback =
8471da177e4SLinus Torvalds {
8481da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
8491da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
8501da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_INTERLEAVED |
8511da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
8521da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE |
8531da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_RESUME),
8541da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
8551da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8561da177e4SLinus Torvalds 	.rate_min =		8000,
8571da177e4SLinus Torvalds 	.rate_max =		48000,
8581da177e4SLinus Torvalds 	.channels_min =		1,
8591da177e4SLinus Torvalds 	.channels_max =		2,
8601da177e4SLinus Torvalds 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
8611da177e4SLinus Torvalds 	.period_bytes_min =	64,
8621da177e4SLinus Torvalds 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
8631da177e4SLinus Torvalds 	.periods_min =		3,
8641da177e4SLinus Torvalds 	.periods_max =		1024,
8651da177e4SLinus Torvalds 	.fifo_size =		0,
8661da177e4SLinus Torvalds };
8671da177e4SLinus Torvalds 
868208a1b4cSTakashi Iwai static struct snd_pcm_hardware snd_ymfpci_capture =
8691da177e4SLinus Torvalds {
8701da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
8711da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
8721da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_INTERLEAVED |
8731da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
8741da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE |
8751da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_RESUME),
8761da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
8771da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8781da177e4SLinus Torvalds 	.rate_min =		8000,
8791da177e4SLinus Torvalds 	.rate_max =		48000,
8801da177e4SLinus Torvalds 	.channels_min =		1,
8811da177e4SLinus Torvalds 	.channels_max =		2,
8821da177e4SLinus Torvalds 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
8831da177e4SLinus Torvalds 	.period_bytes_min =	64,
8841da177e4SLinus Torvalds 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
8851da177e4SLinus Torvalds 	.periods_min =		3,
8861da177e4SLinus Torvalds 	.periods_max =		1024,
8871da177e4SLinus Torvalds 	.fifo_size =		0,
8881da177e4SLinus Torvalds };
8891da177e4SLinus Torvalds 
890208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
8911da177e4SLinus Torvalds {
8924d572776SJesper Juhl 	kfree(runtime->private_data);
8931da177e4SLinus Torvalds }
8941da177e4SLinus Torvalds 
895208a1b4cSTakashi Iwai static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
8961da177e4SLinus Torvalds {
897208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
898208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
899208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
90084f9df15SClemens Ladisch 	int err;
90184f9df15SClemens Ladisch 
90284f9df15SClemens Ladisch 	runtime->hw = snd_ymfpci_playback;
90384f9df15SClemens Ladisch 	/* FIXME? True value is 256/48 = 5.33333 ms */
90484f9df15SClemens Ladisch 	err = snd_pcm_hw_constraint_minmax(runtime,
90584f9df15SClemens Ladisch 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
90684f9df15SClemens Ladisch 					   5334, UINT_MAX);
90784f9df15SClemens Ladisch 	if (err < 0)
90884f9df15SClemens Ladisch 		return err;
9095b0416a3SClemens Ladisch 	err = snd_pcm_hw_rule_noresample(runtime, 48000);
9105b0416a3SClemens Ladisch 	if (err < 0)
9115b0416a3SClemens Ladisch 		return err;
9121da177e4SLinus Torvalds 
913e560d8d8STakashi Iwai 	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
9141da177e4SLinus Torvalds 	if (ypcm == NULL)
9151da177e4SLinus Torvalds 		return -ENOMEM;
9161da177e4SLinus Torvalds 	ypcm->chip = chip;
9171da177e4SLinus Torvalds 	ypcm->type = PLAYBACK_VOICE;
9181da177e4SLinus Torvalds 	ypcm->substream = substream;
9191da177e4SLinus Torvalds 	runtime->private_data = ypcm;
9201da177e4SLinus Torvalds 	runtime->private_free = snd_ymfpci_pcm_free_substream;
9211da177e4SLinus Torvalds 	return 0;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds /* call with spinlock held */
925208a1b4cSTakashi Iwai static void ymfpci_open_extension(struct snd_ymfpci *chip)
9261da177e4SLinus Torvalds {
9271da177e4SLinus Torvalds 	if (! chip->rear_opened) {
9281da177e4SLinus Torvalds 		if (! chip->spdif_opened) /* set AC3 */
9291da177e4SLinus Torvalds 			snd_ymfpci_writel(chip, YDSXGR_MODE,
9301da177e4SLinus Torvalds 					  snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
9311da177e4SLinus Torvalds 		/* enable second codec (4CHEN) */
9321da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
9331da177e4SLinus Torvalds 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
9341da177e4SLinus Torvalds 	}
9351da177e4SLinus Torvalds }
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds /* call with spinlock held */
938208a1b4cSTakashi Iwai static void ymfpci_close_extension(struct snd_ymfpci *chip)
9391da177e4SLinus Torvalds {
9401da177e4SLinus Torvalds 	if (! chip->rear_opened) {
9411da177e4SLinus Torvalds 		if (! chip->spdif_opened)
9421da177e4SLinus Torvalds 			snd_ymfpci_writel(chip, YDSXGR_MODE,
9431da177e4SLinus Torvalds 					  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
9441da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
9451da177e4SLinus Torvalds 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
9461da177e4SLinus Torvalds 	}
9471da177e4SLinus Torvalds }
9481da177e4SLinus Torvalds 
949208a1b4cSTakashi Iwai static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
9501da177e4SLinus Torvalds {
951208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
952208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
953208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
9541da177e4SLinus Torvalds 	int err;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
9571da177e4SLinus Torvalds 		return err;
9581da177e4SLinus Torvalds 	ypcm = runtime->private_data;
9591da177e4SLinus Torvalds 	ypcm->output_front = 1;
9601da177e4SLinus Torvalds 	ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
961d9301263SGlen Masgai 	ypcm->swap_rear = 0;
9621da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9631da177e4SLinus Torvalds 	if (ypcm->output_rear) {
9641da177e4SLinus Torvalds 		ymfpci_open_extension(chip);
9651da177e4SLinus Torvalds 		chip->rear_opened++;
9661da177e4SLinus Torvalds 	}
9671da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9681da177e4SLinus Torvalds 	return 0;
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds 
971208a1b4cSTakashi Iwai static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
9721da177e4SLinus Torvalds {
973208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
974208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
975208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
9761da177e4SLinus Torvalds 	int err;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
9791da177e4SLinus Torvalds 		return err;
9801da177e4SLinus Torvalds 	ypcm = runtime->private_data;
9811da177e4SLinus Torvalds 	ypcm->output_front = 0;
9821da177e4SLinus Torvalds 	ypcm->output_rear = 1;
983d9301263SGlen Masgai 	ypcm->swap_rear = 1;
9841da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9851da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
9861da177e4SLinus Torvalds 			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
9871da177e4SLinus Torvalds 	ymfpci_open_extension(chip);
9881da177e4SLinus Torvalds 	chip->spdif_pcm_bits = chip->spdif_bits;
9891da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
9901da177e4SLinus Torvalds 	chip->spdif_opened++;
9911da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
9941da177e4SLinus Torvalds 	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
9951da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
9961da177e4SLinus Torvalds 	return 0;
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
999208a1b4cSTakashi Iwai static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
10001da177e4SLinus Torvalds {
1001208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1002208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
1003208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
10041da177e4SLinus Torvalds 	int err;
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
10071da177e4SLinus Torvalds 		return err;
10081da177e4SLinus Torvalds 	ypcm = runtime->private_data;
10091da177e4SLinus Torvalds 	ypcm->output_front = 0;
10101da177e4SLinus Torvalds 	ypcm->output_rear = 1;
1011d9301263SGlen Masgai 	ypcm->swap_rear = 0;
10121da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10131da177e4SLinus Torvalds 	ymfpci_open_extension(chip);
10141da177e4SLinus Torvalds 	chip->rear_opened++;
10151da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10161da177e4SLinus Torvalds 	return 0;
10171da177e4SLinus Torvalds }
10181da177e4SLinus Torvalds 
1019208a1b4cSTakashi Iwai static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
10201da177e4SLinus Torvalds 				   u32 capture_bank_number)
10211da177e4SLinus Torvalds {
1022208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1023208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
1024208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
102584f9df15SClemens Ladisch 	int err;
102684f9df15SClemens Ladisch 
102784f9df15SClemens Ladisch 	runtime->hw = snd_ymfpci_capture;
102884f9df15SClemens Ladisch 	/* FIXME? True value is 256/48 = 5.33333 ms */
102984f9df15SClemens Ladisch 	err = snd_pcm_hw_constraint_minmax(runtime,
103084f9df15SClemens Ladisch 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
103184f9df15SClemens Ladisch 					   5334, UINT_MAX);
103284f9df15SClemens Ladisch 	if (err < 0)
103384f9df15SClemens Ladisch 		return err;
10345b0416a3SClemens Ladisch 	err = snd_pcm_hw_rule_noresample(runtime, 48000);
10355b0416a3SClemens Ladisch 	if (err < 0)
10365b0416a3SClemens Ladisch 		return err;
10371da177e4SLinus Torvalds 
1038e560d8d8STakashi Iwai 	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
10391da177e4SLinus Torvalds 	if (ypcm == NULL)
10401da177e4SLinus Torvalds 		return -ENOMEM;
10411da177e4SLinus Torvalds 	ypcm->chip = chip;
10421da177e4SLinus Torvalds 	ypcm->type = capture_bank_number + CAPTURE_REC;
10431da177e4SLinus Torvalds 	ypcm->substream = substream;
10441da177e4SLinus Torvalds 	ypcm->capture_bank_number = capture_bank_number;
10451da177e4SLinus Torvalds 	chip->capture_substream[capture_bank_number] = substream;
10461da177e4SLinus Torvalds 	runtime->private_data = ypcm;
10471da177e4SLinus Torvalds 	runtime->private_free = snd_ymfpci_pcm_free_substream;
10481da177e4SLinus Torvalds 	snd_ymfpci_hw_start(chip);
10491da177e4SLinus Torvalds 	return 0;
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds 
1052208a1b4cSTakashi Iwai static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
10531da177e4SLinus Torvalds {
10541da177e4SLinus Torvalds 	return snd_ymfpci_capture_open(substream, 0);
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
1057208a1b4cSTakashi Iwai static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
10581da177e4SLinus Torvalds {
10591da177e4SLinus Torvalds 	return snd_ymfpci_capture_open(substream, 1);
10601da177e4SLinus Torvalds }
10611da177e4SLinus Torvalds 
1062208a1b4cSTakashi Iwai static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
10631da177e4SLinus Torvalds {
10641da177e4SLinus Torvalds 	return 0;
10651da177e4SLinus Torvalds }
10661da177e4SLinus Torvalds 
1067208a1b4cSTakashi Iwai static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
10681da177e4SLinus Torvalds {
1069208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1070208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10731da177e4SLinus Torvalds 	if (ypcm->output_rear && chip->rear_opened > 0) {
10741da177e4SLinus Torvalds 		chip->rear_opened--;
10751da177e4SLinus Torvalds 		ymfpci_close_extension(chip);
10761da177e4SLinus Torvalds 	}
10771da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10781da177e4SLinus Torvalds 	return snd_ymfpci_playback_close_1(substream);
10791da177e4SLinus Torvalds }
10801da177e4SLinus Torvalds 
1081208a1b4cSTakashi Iwai static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
10821da177e4SLinus Torvalds {
1083208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10861da177e4SLinus Torvalds 	chip->spdif_opened = 0;
10871da177e4SLinus Torvalds 	ymfpci_close_extension(chip);
10881da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
10891da177e4SLinus Torvalds 			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
10901da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
10911da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10921da177e4SLinus Torvalds 	chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
10931da177e4SLinus Torvalds 	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
10941da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
10951da177e4SLinus Torvalds 	return snd_ymfpci_playback_close_1(substream);
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds 
1098208a1b4cSTakashi Iwai static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
10991da177e4SLinus Torvalds {
1100208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
11031da177e4SLinus Torvalds 	if (chip->rear_opened > 0) {
11041da177e4SLinus Torvalds 		chip->rear_opened--;
11051da177e4SLinus Torvalds 		ymfpci_close_extension(chip);
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
11081da177e4SLinus Torvalds 	return snd_ymfpci_playback_close_1(substream);
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
1111208a1b4cSTakashi Iwai static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
11121da177e4SLinus Torvalds {
1113208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1114208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
1115208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	if (ypcm != NULL) {
11181da177e4SLinus Torvalds 		chip->capture_substream[ypcm->capture_bank_number] = NULL;
11191da177e4SLinus Torvalds 		snd_ymfpci_hw_stop(chip);
11201da177e4SLinus Torvalds 	}
11211da177e4SLinus Torvalds 	return 0;
11221da177e4SLinus Torvalds }
11231da177e4SLinus Torvalds 
1124208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_playback_ops = {
11251da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_open,
11261da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_close,
11271da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
11281da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
11291da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
11301da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
11311da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
11321da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
11331da177e4SLinus Torvalds };
11341da177e4SLinus Torvalds 
1135208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
11361da177e4SLinus Torvalds 	.open =			snd_ymfpci_capture_rec_open,
11371da177e4SLinus Torvalds 	.close =		snd_ymfpci_capture_close,
11381da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
11391da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_capture_hw_params,
11401da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_capture_hw_free,
11411da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_capture_prepare,
11421da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_capture_trigger,
11431da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_capture_pointer,
11441da177e4SLinus Torvalds };
11451da177e4SLinus Torvalds 
1146*e23e7a14SBill Pemberton int snd_ymfpci_pcm(struct snd_ymfpci *chip, int device, struct snd_pcm **rpcm)
11471da177e4SLinus Torvalds {
1148208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
11491da177e4SLinus Torvalds 	int err;
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	if (rpcm)
11521da177e4SLinus Torvalds 		*rpcm = NULL;
11531da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm)) < 0)
11541da177e4SLinus Torvalds 		return err;
11551da177e4SLinus Torvalds 	pcm->private_data = chip;
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
11581da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	/* global setup */
11611da177e4SLinus Torvalds 	pcm->info_flags = 0;
11621da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI");
11631da177e4SLinus Torvalds 	chip->pcm = pcm;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
11661da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
11671da177e4SLinus Torvalds 
11680afdb8f2STakashi Iwai 	err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
11690afdb8f2STakashi Iwai 				     snd_pcm_std_chmaps, 2, 0, NULL);
11700afdb8f2STakashi Iwai 	if (err < 0)
11710afdb8f2STakashi Iwai 		return err;
11720afdb8f2STakashi Iwai 
11731da177e4SLinus Torvalds 	if (rpcm)
11741da177e4SLinus Torvalds 		*rpcm = pcm;
11751da177e4SLinus Torvalds 	return 0;
11761da177e4SLinus Torvalds }
11771da177e4SLinus Torvalds 
1178208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
11791da177e4SLinus Torvalds 	.open =			snd_ymfpci_capture_ac97_open,
11801da177e4SLinus Torvalds 	.close =		snd_ymfpci_capture_close,
11811da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
11821da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_capture_hw_params,
11831da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_capture_hw_free,
11841da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_capture_prepare,
11851da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_capture_trigger,
11861da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_capture_pointer,
11871da177e4SLinus Torvalds };
11881da177e4SLinus Torvalds 
1189*e23e7a14SBill Pemberton int snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device, struct snd_pcm **rpcm)
11901da177e4SLinus Torvalds {
1191208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
11921da177e4SLinus Torvalds 	int err;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	if (rpcm)
11951da177e4SLinus Torvalds 		*rpcm = NULL;
11961da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
11971da177e4SLinus Torvalds 		return err;
11981da177e4SLinus Torvalds 	pcm->private_data = chip;
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	/* global setup */
12031da177e4SLinus Torvalds 	pcm->info_flags = 0;
12041da177e4SLinus Torvalds 	sprintf(pcm->name, "YMFPCI - %s",
12051da177e4SLinus Torvalds 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
12061da177e4SLinus Torvalds 	chip->pcm2 = pcm;
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
12091da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	if (rpcm)
12121da177e4SLinus Torvalds 		*rpcm = pcm;
12131da177e4SLinus Torvalds 	return 0;
12141da177e4SLinus Torvalds }
12151da177e4SLinus Torvalds 
1216208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
12171da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_spdif_open,
12181da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_spdif_close,
12191da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
12201da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
12211da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
12221da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
12231da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
12241da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
12251da177e4SLinus Torvalds };
12261da177e4SLinus Torvalds 
1227*e23e7a14SBill Pemberton int snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device,
1228*e23e7a14SBill Pemberton 			 struct snd_pcm **rpcm)
12291da177e4SLinus Torvalds {
1230208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
12311da177e4SLinus Torvalds 	int err;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	if (rpcm)
12341da177e4SLinus Torvalds 		*rpcm = NULL;
12351da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
12361da177e4SLinus Torvalds 		return err;
12371da177e4SLinus Torvalds 	pcm->private_data = chip;
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	/* global setup */
12421da177e4SLinus Torvalds 	pcm->info_flags = 0;
12431da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI - IEC958");
12441da177e4SLinus Torvalds 	chip->pcm_spdif = pcm;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
12471da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	if (rpcm)
12501da177e4SLinus Torvalds 		*rpcm = pcm;
12511da177e4SLinus Torvalds 	return 0;
12521da177e4SLinus Torvalds }
12531da177e4SLinus Torvalds 
1254208a1b4cSTakashi Iwai static struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
12551da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_4ch_open,
12561da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_4ch_close,
12571da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
12581da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
12591da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
12601da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
12611da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
12621da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
12631da177e4SLinus Torvalds };
12641da177e4SLinus Torvalds 
12650afdb8f2STakashi Iwai static const struct snd_pcm_chmap_elem surround_map[] = {
12660afdb8f2STakashi Iwai 	{ .channels = 1,
12675efbc261STakashi Iwai 	  .map = { SNDRV_CHMAP_MONO } },
12680afdb8f2STakashi Iwai 	{ .channels = 2,
12690afdb8f2STakashi Iwai 	  .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
12700afdb8f2STakashi Iwai 	{ }
12710afdb8f2STakashi Iwai };
12720afdb8f2STakashi Iwai 
1273*e23e7a14SBill Pemberton int snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device,
1274*e23e7a14SBill Pemberton 		       struct snd_pcm **rpcm)
12751da177e4SLinus Torvalds {
1276208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
12771da177e4SLinus Torvalds 	int err;
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	if (rpcm)
12801da177e4SLinus Torvalds 		*rpcm = NULL;
12811da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
12821da177e4SLinus Torvalds 		return err;
12831da177e4SLinus Torvalds 	pcm->private_data = chip;
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	/* global setup */
12881da177e4SLinus Torvalds 	pcm->info_flags = 0;
12891da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI - Rear PCM");
12901da177e4SLinus Torvalds 	chip->pcm_4ch = pcm;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
12931da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
12941da177e4SLinus Torvalds 
12950afdb8f2STakashi Iwai 	err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
12960afdb8f2STakashi Iwai 				     surround_map, 2, 0, NULL);
12970afdb8f2STakashi Iwai 	if (err < 0)
12980afdb8f2STakashi Iwai 		return err;
12990afdb8f2STakashi Iwai 
13001da177e4SLinus Torvalds 	if (rpcm)
13011da177e4SLinus Torvalds 		*rpcm = pcm;
13021da177e4SLinus Torvalds 	return 0;
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
1305208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
13061da177e4SLinus Torvalds {
13071da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
13081da177e4SLinus Torvalds 	uinfo->count = 1;
13091da177e4SLinus Torvalds 	return 0;
13101da177e4SLinus Torvalds }
13111da177e4SLinus Torvalds 
1312208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1313208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
13141da177e4SLinus Torvalds {
1315208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13181da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
13191da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1320fc80a202SClemens Ladisch 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
13211da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13221da177e4SLinus Torvalds 	return 0;
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds 
1325208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1326208a1b4cSTakashi Iwai 					 struct snd_ctl_elem_value *ucontrol)
13271da177e4SLinus Torvalds {
1328208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13291da177e4SLinus Torvalds 	unsigned int val;
13301da177e4SLinus Torvalds 	int change;
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
13331da177e4SLinus Torvalds 	      (ucontrol->value.iec958.status[1] << 8);
13341da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13351da177e4SLinus Torvalds 	change = chip->spdif_bits != val;
13361da177e4SLinus Torvalds 	chip->spdif_bits = val;
13371da177e4SLinus Torvalds 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
13381da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
13391da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13401da177e4SLinus Torvalds 	return change;
13411da177e4SLinus Torvalds }
13421da177e4SLinus Torvalds 
1343*e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_ymfpci_spdif_default =
13441da177e4SLinus Torvalds {
13451da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
13461da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
13471da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_default_info,
13481da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_default_get,
13491da177e4SLinus Torvalds 	.put =		snd_ymfpci_spdif_default_put
13501da177e4SLinus Torvalds };
13511da177e4SLinus Torvalds 
1352208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
13531da177e4SLinus Torvalds {
13541da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
13551da177e4SLinus Torvalds 	uinfo->count = 1;
13561da177e4SLinus Torvalds 	return 0;
13571da177e4SLinus Torvalds }
13581da177e4SLinus Torvalds 
1359208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1360208a1b4cSTakashi Iwai 				      struct snd_ctl_elem_value *ucontrol)
13611da177e4SLinus Torvalds {
1362208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13651da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = 0x3e;
13661da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = 0xff;
13671da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13681da177e4SLinus Torvalds 	return 0;
13691da177e4SLinus Torvalds }
13701da177e4SLinus Torvalds 
1371*e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_ymfpci_spdif_mask =
13721da177e4SLinus Torvalds {
13731da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
13741da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
13751da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
13761da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_mask_info,
13771da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_mask_get,
13781da177e4SLinus Torvalds };
13791da177e4SLinus Torvalds 
1380208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
13811da177e4SLinus Torvalds {
13821da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
13831da177e4SLinus Torvalds 	uinfo->count = 1;
13841da177e4SLinus Torvalds 	return 0;
13851da177e4SLinus Torvalds }
13861da177e4SLinus Torvalds 
1387208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1388208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
13891da177e4SLinus Torvalds {
1390208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13931da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
13941da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1395fc80a202SClemens Ladisch 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
13961da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13971da177e4SLinus Torvalds 	return 0;
13981da177e4SLinus Torvalds }
13991da177e4SLinus Torvalds 
1400208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1401208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
14021da177e4SLinus Torvalds {
1403208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
14041da177e4SLinus Torvalds 	unsigned int val;
14051da177e4SLinus Torvalds 	int change;
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
14081da177e4SLinus Torvalds 	      (ucontrol->value.iec958.status[1] << 8);
14091da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
14101da177e4SLinus Torvalds 	change = chip->spdif_pcm_bits != val;
14111da177e4SLinus Torvalds 	chip->spdif_pcm_bits = val;
14121da177e4SLinus Torvalds 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
14131da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
14141da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
14151da177e4SLinus Torvalds 	return change;
14161da177e4SLinus Torvalds }
14171da177e4SLinus Torvalds 
1418*e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_ymfpci_spdif_stream =
14191da177e4SLinus Torvalds {
14201da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
14211da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
14221da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
14231da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_stream_info,
14241da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_stream_get,
14251da177e4SLinus Torvalds 	.put =		snd_ymfpci_spdif_stream_put
14261da177e4SLinus Torvalds };
14271da177e4SLinus Torvalds 
1428208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
14291da177e4SLinus Torvalds {
1430bed6896dSClemens Ladisch 	static const char *const texts[3] = {"AC'97", "IEC958", "ZV Port"};
14311da177e4SLinus Torvalds 
1432bed6896dSClemens Ladisch 	return snd_ctl_enum_info(info, 1, 3, texts);
14331da177e4SLinus Torvalds }
14341da177e4SLinus Torvalds 
1435208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
14361da177e4SLinus Torvalds {
1437208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
14381da177e4SLinus Torvalds 	u16 reg;
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
14411da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
14421da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
14431da177e4SLinus Torvalds 	if (!(reg & 0x100))
14441da177e4SLinus Torvalds 		value->value.enumerated.item[0] = 0;
14451da177e4SLinus Torvalds 	else
14461da177e4SLinus Torvalds 		value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
14471da177e4SLinus Torvalds 	return 0;
14481da177e4SLinus Torvalds }
14491da177e4SLinus Torvalds 
1450208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
14511da177e4SLinus Torvalds {
1452208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
14531da177e4SLinus Torvalds 	u16 reg, old_reg;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
14561da177e4SLinus Torvalds 	old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
14571da177e4SLinus Torvalds 	if (value->value.enumerated.item[0] == 0)
14581da177e4SLinus Torvalds 		reg = old_reg & ~0x100;
14591da177e4SLinus Torvalds 	else
14601da177e4SLinus Torvalds 		reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
14611da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
14621da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
14631da177e4SLinus Torvalds 	return reg != old_reg;
14641da177e4SLinus Torvalds }
14651da177e4SLinus Torvalds 
1466*e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_ymfpci_drec_source = {
14671da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
14681da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
14691da177e4SLinus Torvalds 	.name =		"Direct Recording Source",
14701da177e4SLinus Torvalds 	.info =		snd_ymfpci_drec_source_info,
14711da177e4SLinus Torvalds 	.get =		snd_ymfpci_drec_source_get,
14721da177e4SLinus Torvalds 	.put =		snd_ymfpci_drec_source_put
14731da177e4SLinus Torvalds };
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds /*
14761da177e4SLinus Torvalds  *  Mixer controls
14771da177e4SLinus Torvalds  */
14781da177e4SLinus Torvalds 
1479d602c885SGlen Masgai #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
14801da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
14811da177e4SLinus Torvalds   .info = snd_ymfpci_info_single, \
14821da177e4SLinus Torvalds   .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1483d602c885SGlen Masgai   .private_value = ((reg) | ((shift) << 16)) }
14841da177e4SLinus Torvalds 
1485a5ce8890STakashi Iwai #define snd_ymfpci_info_single		snd_ctl_boolean_mono_info
14861da177e4SLinus Torvalds 
1487208a1b4cSTakashi Iwai static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1488208a1b4cSTakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
14891da177e4SLinus Torvalds {
1490208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1491d602c885SGlen Masgai 	int reg = kcontrol->private_value & 0xffff;
1492d602c885SGlen Masgai 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1493d602c885SGlen Masgai 	unsigned int mask = 1;
14941da177e4SLinus Torvalds 
1495d602c885SGlen Masgai 	switch (reg) {
14961da177e4SLinus Torvalds 	case YDSXGR_SPDIFOUTCTRL: break;
14971da177e4SLinus Torvalds 	case YDSXGR_SPDIFINCTRL: break;
14981da177e4SLinus Torvalds 	default: return -EINVAL;
14991da177e4SLinus Torvalds 	}
1500d602c885SGlen Masgai 	ucontrol->value.integer.value[0] =
1501d602c885SGlen Masgai 		(snd_ymfpci_readl(chip, reg) >> shift) & mask;
15021da177e4SLinus Torvalds 	return 0;
15031da177e4SLinus Torvalds }
15041da177e4SLinus Torvalds 
1505208a1b4cSTakashi Iwai static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1506208a1b4cSTakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
15071da177e4SLinus Torvalds {
1508208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1509d602c885SGlen Masgai 	int reg = kcontrol->private_value & 0xffff;
1510d602c885SGlen Masgai 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1511d602c885SGlen Masgai  	unsigned int mask = 1;
15121da177e4SLinus Torvalds 	int change;
15131da177e4SLinus Torvalds 	unsigned int val, oval;
15141da177e4SLinus Torvalds 
1515d602c885SGlen Masgai 	switch (reg) {
15161da177e4SLinus Torvalds 	case YDSXGR_SPDIFOUTCTRL: break;
15171da177e4SLinus Torvalds 	case YDSXGR_SPDIFINCTRL: break;
15181da177e4SLinus Torvalds 	default: return -EINVAL;
15191da177e4SLinus Torvalds 	}
15201da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
15211da177e4SLinus Torvalds 	val <<= shift;
15221da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15231da177e4SLinus Torvalds 	oval = snd_ymfpci_readl(chip, reg);
15241da177e4SLinus Torvalds 	val = (oval & ~(mask << shift)) | val;
15251da177e4SLinus Torvalds 	change = val != oval;
15261da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, reg, val);
15271da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15281da177e4SLinus Torvalds 	return change;
15291da177e4SLinus Torvalds }
15301da177e4SLinus Torvalds 
15310cb29ea0STakashi Iwai static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
153233925186STakashi Iwai 
15331da177e4SLinus Torvalds #define YMFPCI_DOUBLE(xname, xindex, reg) \
15341da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
153533925186STakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
15361da177e4SLinus Torvalds   .info = snd_ymfpci_info_double, \
15371da177e4SLinus Torvalds   .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
153833925186STakashi Iwai   .private_value = reg, \
153933925186STakashi Iwai   .tlv = { .p = db_scale_native } }
15401da177e4SLinus Torvalds 
1541208a1b4cSTakashi Iwai static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15421da177e4SLinus Torvalds {
15431da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
15461da177e4SLinus Torvalds 		return -EINVAL;
1547467a8c2fSAdrian Bunk 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
15481da177e4SLinus Torvalds 	uinfo->count = 2;
15491da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
1550467a8c2fSAdrian Bunk 	uinfo->value.integer.max = 16383;
15511da177e4SLinus Torvalds 	return 0;
15521da177e4SLinus Torvalds }
15531da177e4SLinus Torvalds 
1554208a1b4cSTakashi Iwai static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15551da177e4SLinus Torvalds {
1556208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15571da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
1558467a8c2fSAdrian Bunk 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
15591da177e4SLinus Torvalds 	unsigned int val;
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
15621da177e4SLinus Torvalds 		return -EINVAL;
15631da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15641da177e4SLinus Torvalds 	val = snd_ymfpci_readl(chip, reg);
15651da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15661da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
15671da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
15681da177e4SLinus Torvalds 	return 0;
15691da177e4SLinus Torvalds }
15701da177e4SLinus Torvalds 
1571208a1b4cSTakashi Iwai static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15721da177e4SLinus Torvalds {
1573208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15741da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
1575467a8c2fSAdrian Bunk 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
15761da177e4SLinus Torvalds 	int change;
15771da177e4SLinus Torvalds 	unsigned int val1, val2, oval;
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
15801da177e4SLinus Torvalds 		return -EINVAL;
15811da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
15821da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
15831da177e4SLinus Torvalds 	val1 <<= shift_left;
15841da177e4SLinus Torvalds 	val2 <<= shift_right;
15851da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15861da177e4SLinus Torvalds 	oval = snd_ymfpci_readl(chip, reg);
15871da177e4SLinus Torvalds 	val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
15881da177e4SLinus Torvalds 	change = val1 != oval;
15891da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, reg, val1);
15901da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15911da177e4SLinus Torvalds 	return change;
15921da177e4SLinus Torvalds }
15931da177e4SLinus Torvalds 
1594177a7cdbSClemens Ladisch static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1595177a7cdbSClemens Ladisch 				       struct snd_ctl_elem_value *ucontrol)
1596177a7cdbSClemens Ladisch {
1597177a7cdbSClemens Ladisch 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1598177a7cdbSClemens Ladisch 	unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1599177a7cdbSClemens Ladisch 	unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1600177a7cdbSClemens Ladisch 	int change;
1601177a7cdbSClemens Ladisch 	unsigned int value, oval;
1602177a7cdbSClemens Ladisch 
1603177a7cdbSClemens Ladisch 	value = ucontrol->value.integer.value[0] & 0x3fff;
1604177a7cdbSClemens Ladisch 	value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1605177a7cdbSClemens Ladisch 	spin_lock_irq(&chip->reg_lock);
1606177a7cdbSClemens Ladisch 	oval = snd_ymfpci_readl(chip, reg);
1607177a7cdbSClemens Ladisch 	change = value != oval;
1608177a7cdbSClemens Ladisch 	snd_ymfpci_writel(chip, reg, value);
1609177a7cdbSClemens Ladisch 	snd_ymfpci_writel(chip, reg2, value);
1610177a7cdbSClemens Ladisch 	spin_unlock_irq(&chip->reg_lock);
1611177a7cdbSClemens Ladisch 	return change;
1612177a7cdbSClemens Ladisch }
1613177a7cdbSClemens Ladisch 
16141da177e4SLinus Torvalds /*
16151da177e4SLinus Torvalds  * 4ch duplication
16161da177e4SLinus Torvalds  */
1617a5ce8890STakashi Iwai #define snd_ymfpci_info_dup4ch		snd_ctl_boolean_mono_info
16181da177e4SLinus Torvalds 
1619208a1b4cSTakashi Iwai static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16201da177e4SLinus Torvalds {
1621208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
16221da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = chip->mode_dup4ch;
16231da177e4SLinus Torvalds 	return 0;
16241da177e4SLinus Torvalds }
16251da177e4SLinus Torvalds 
1626208a1b4cSTakashi Iwai static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16271da177e4SLinus Torvalds {
1628208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
16291da177e4SLinus Torvalds 	int change;
16301da177e4SLinus Torvalds 	change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
16311da177e4SLinus Torvalds 	if (change)
16321da177e4SLinus Torvalds 		chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
16331da177e4SLinus Torvalds 	return change;
16341da177e4SLinus Torvalds }
16351da177e4SLinus Torvalds 
1636*e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_ymfpci_dup4ch = {
16374d20bb1dSRaymond Yau 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
16384d20bb1dSRaymond Yau 	.name = "4ch Duplication",
16394d20bb1dSRaymond Yau 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
16404d20bb1dSRaymond Yau 	.info = snd_ymfpci_info_dup4ch,
16414d20bb1dSRaymond Yau 	.get = snd_ymfpci_get_dup4ch,
16424d20bb1dSRaymond Yau 	.put = snd_ymfpci_put_dup4ch,
16434d20bb1dSRaymond Yau };
16441da177e4SLinus Torvalds 
1645*e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_ymfpci_controls[] = {
1646177a7cdbSClemens Ladisch {
1647177a7cdbSClemens Ladisch 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1648177a7cdbSClemens Ladisch 	.name = "Wave Playback Volume",
1649177a7cdbSClemens Ladisch 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1650177a7cdbSClemens Ladisch 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1651177a7cdbSClemens Ladisch 	.info = snd_ymfpci_info_double,
1652177a7cdbSClemens Ladisch 	.get = snd_ymfpci_get_double,
1653177a7cdbSClemens Ladisch 	.put = snd_ymfpci_put_nativedacvol,
1654177a7cdbSClemens Ladisch 	.private_value = YDSXGR_NATIVEDACOUTVOL,
1655177a7cdbSClemens Ladisch 	.tlv = { .p = db_scale_native },
1656177a7cdbSClemens Ladisch },
16571da177e4SLinus Torvalds YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
16581da177e4SLinus Torvalds YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
16591da177e4SLinus Torvalds YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
16601da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
16611da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
16621da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
16631da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
166489f3325aSRaymond Yau YMFPCI_DOUBLE("FM Legacy Playback Volume", 0, YDSXGR_LEGACYOUTVOL),
16651da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
16661da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
16671da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
16681da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1669d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1670d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1671d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
16721da177e4SLinus Torvalds };
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds /*
16761da177e4SLinus Torvalds  * GPIO
16771da177e4SLinus Torvalds  */
16781da177e4SLinus Torvalds 
1679208a1b4cSTakashi Iwai static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
16801da177e4SLinus Torvalds {
16811da177e4SLinus Torvalds 	u16 reg, mode;
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 + 8));
16871da177e4SLinus Torvalds 	reg |= (1 << pin);
16881da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
16891da177e4SLinus Torvalds 	/* set the level mode for input line */
16901da177e4SLinus Torvalds 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
16911da177e4SLinus Torvalds 	mode &= ~(3 << (pin * 2));
16921da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
16931da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
16941da177e4SLinus Torvalds 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
16951da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
16961da177e4SLinus Torvalds 	return (mode >> pin) & 1;
16971da177e4SLinus Torvalds }
16981da177e4SLinus Torvalds 
1699208a1b4cSTakashi Iwai static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
17001da177e4SLinus Torvalds {
17011da177e4SLinus Torvalds 	u16 reg;
17021da177e4SLinus Torvalds 	unsigned long flags;
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
17051da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
17061da177e4SLinus Torvalds 	reg &= ~(1 << pin);
17071da177e4SLinus Torvalds 	reg &= ~(1 << (pin + 8));
17081da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
17091da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
17101da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
17111da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
17121da177e4SLinus Torvalds 
17131da177e4SLinus Torvalds 	return 0;
17141da177e4SLinus Torvalds }
17151da177e4SLinus Torvalds 
1716a5ce8890STakashi Iwai #define snd_ymfpci_gpio_sw_info		snd_ctl_boolean_mono_info
17171da177e4SLinus Torvalds 
1718208a1b4cSTakashi Iwai static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17191da177e4SLinus Torvalds {
1720208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17211da177e4SLinus Torvalds 	int pin = (int)kcontrol->private_value;
17221da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
17231da177e4SLinus Torvalds 	return 0;
17241da177e4SLinus Torvalds }
17251da177e4SLinus Torvalds 
1726208a1b4cSTakashi Iwai static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17271da177e4SLinus Torvalds {
1728208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17291da177e4SLinus Torvalds 	int pin = (int)kcontrol->private_value;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 	if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
17321da177e4SLinus Torvalds 		snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
17331da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
17341da177e4SLinus Torvalds 		return 1;
17351da177e4SLinus Torvalds 	}
17361da177e4SLinus Torvalds 	return 0;
17371da177e4SLinus Torvalds }
17381da177e4SLinus Torvalds 
1739*e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_ymfpci_rear_shared = {
17401da177e4SLinus Torvalds 	.name = "Shared Rear/Line-In Switch",
17411da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
17421da177e4SLinus Torvalds 	.info = snd_ymfpci_gpio_sw_info,
17431da177e4SLinus Torvalds 	.get = snd_ymfpci_gpio_sw_get,
17441da177e4SLinus Torvalds 	.put = snd_ymfpci_gpio_sw_put,
17451da177e4SLinus Torvalds 	.private_value = 2,
17461da177e4SLinus Torvalds };
17471da177e4SLinus Torvalds 
17489bcf6551SClemens Ladisch /*
17499bcf6551SClemens Ladisch  * PCM voice volume
17509bcf6551SClemens Ladisch  */
17519bcf6551SClemens Ladisch 
1752208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1753208a1b4cSTakashi Iwai 				   struct snd_ctl_elem_info *uinfo)
17549bcf6551SClemens Ladisch {
17559bcf6551SClemens Ladisch 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
17569bcf6551SClemens Ladisch 	uinfo->count = 2;
17579bcf6551SClemens Ladisch 	uinfo->value.integer.min = 0;
17589bcf6551SClemens Ladisch 	uinfo->value.integer.max = 0x8000;
17599bcf6551SClemens Ladisch 	return 0;
17609bcf6551SClemens Ladisch }
17619bcf6551SClemens Ladisch 
1762208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1763208a1b4cSTakashi Iwai 				  struct snd_ctl_elem_value *ucontrol)
17649bcf6551SClemens Ladisch {
1765208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17669bcf6551SClemens Ladisch 	unsigned int subs = kcontrol->id.subdevice;
17679bcf6551SClemens Ladisch 
17689bcf6551SClemens Ladisch 	ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
17699bcf6551SClemens Ladisch 	ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
17709bcf6551SClemens Ladisch 	return 0;
17719bcf6551SClemens Ladisch }
17729bcf6551SClemens Ladisch 
1773208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1774208a1b4cSTakashi Iwai 				  struct snd_ctl_elem_value *ucontrol)
17759bcf6551SClemens Ladisch {
1776208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17779bcf6551SClemens Ladisch 	unsigned int subs = kcontrol->id.subdevice;
1778208a1b4cSTakashi Iwai 	struct snd_pcm_substream *substream;
17799bcf6551SClemens Ladisch 	unsigned long flags;
17809bcf6551SClemens Ladisch 
17819bcf6551SClemens Ladisch 	if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
17829bcf6551SClemens Ladisch 	    ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
17839bcf6551SClemens Ladisch 		chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
17849bcf6551SClemens Ladisch 		chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
17854e98d6a7STakashi Iwai 		if (chip->pcm_mixer[subs].left > 0x8000)
17864e98d6a7STakashi Iwai 			chip->pcm_mixer[subs].left = 0x8000;
17874e98d6a7STakashi Iwai 		if (chip->pcm_mixer[subs].right > 0x8000)
17884e98d6a7STakashi Iwai 			chip->pcm_mixer[subs].right = 0x8000;
17899bcf6551SClemens Ladisch 
1790208a1b4cSTakashi Iwai 		substream = (struct snd_pcm_substream *)kcontrol->private_value;
17919bcf6551SClemens Ladisch 		spin_lock_irqsave(&chip->voice_lock, flags);
17929bcf6551SClemens Ladisch 		if (substream->runtime && substream->runtime->private_data) {
1793208a1b4cSTakashi Iwai 			struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
17949ed1261eSTeru KAMOGASHIRA 			if (!ypcm->use_441_slot)
17959bcf6551SClemens Ladisch 				ypcm->update_pcm_vol = 2;
17969bcf6551SClemens Ladisch 		}
17979bcf6551SClemens Ladisch 		spin_unlock_irqrestore(&chip->voice_lock, flags);
17989bcf6551SClemens Ladisch 		return 1;
17999bcf6551SClemens Ladisch 	}
18009bcf6551SClemens Ladisch 	return 0;
18019bcf6551SClemens Ladisch }
18029bcf6551SClemens Ladisch 
1803*e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_ymfpci_pcm_volume = {
18049bcf6551SClemens Ladisch 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
18059bcf6551SClemens Ladisch 	.name = "PCM Playback Volume",
18069bcf6551SClemens Ladisch 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
18079bcf6551SClemens Ladisch 		SNDRV_CTL_ELEM_ACCESS_INACTIVE,
18089bcf6551SClemens Ladisch 	.info = snd_ymfpci_pcm_vol_info,
18099bcf6551SClemens Ladisch 	.get = snd_ymfpci_pcm_vol_get,
18109bcf6551SClemens Ladisch 	.put = snd_ymfpci_pcm_vol_put,
18119bcf6551SClemens Ladisch };
18129bcf6551SClemens Ladisch 
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds /*
18151da177e4SLinus Torvalds  *  Mixer routines
18161da177e4SLinus Torvalds  */
18171da177e4SLinus Torvalds 
1818208a1b4cSTakashi Iwai static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
18191da177e4SLinus Torvalds {
1820208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = bus->private_data;
18211da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds 
1824208a1b4cSTakashi Iwai static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
18251da177e4SLinus Torvalds {
1826208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
18271da177e4SLinus Torvalds 	chip->ac97 = NULL;
18281da177e4SLinus Torvalds }
18291da177e4SLinus Torvalds 
1830*e23e7a14SBill Pemberton int snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
18311da177e4SLinus Torvalds {
1832208a1b4cSTakashi Iwai 	struct snd_ac97_template ac97;
1833208a1b4cSTakashi Iwai 	struct snd_kcontrol *kctl;
1834208a1b4cSTakashi Iwai 	struct snd_pcm_substream *substream;
18351da177e4SLinus Torvalds 	unsigned int idx;
18361da177e4SLinus Torvalds 	int err;
1837208a1b4cSTakashi Iwai 	static struct snd_ac97_bus_ops ops = {
18381da177e4SLinus Torvalds 		.write = snd_ymfpci_codec_write,
18391da177e4SLinus Torvalds 		.read = snd_ymfpci_codec_read,
18401da177e4SLinus Torvalds 	};
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
18431da177e4SLinus Torvalds 		return err;
18441da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
18451da177e4SLinus Torvalds 	chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
18481da177e4SLinus Torvalds 	ac97.private_data = chip;
18491da177e4SLinus Torvalds 	ac97.private_free = snd_ymfpci_mixer_free_ac97;
18501da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
18511da177e4SLinus Torvalds 		return err;
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 	/* to be sure */
18541da177e4SLinus Torvalds 	snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
18551da177e4SLinus Torvalds 			     AC97_EA_VRA|AC97_EA_VRM, 0);
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
18581da177e4SLinus Torvalds 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
18591da177e4SLinus Torvalds 			return err;
18601da177e4SLinus Torvalds 	}
18614d20bb1dSRaymond Yau 	if (chip->ac97->ext_id & AC97_EI_SDAC) {
18624d20bb1dSRaymond Yau 		kctl = snd_ctl_new1(&snd_ymfpci_dup4ch, chip);
18634d20bb1dSRaymond Yau 		err = snd_ctl_add(chip->card, kctl);
18644d20bb1dSRaymond Yau 		if (err < 0)
18654d20bb1dSRaymond Yau 			return err;
18664d20bb1dSRaymond Yau 	}
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	/* add S/PDIF control */
1869da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip->pcm_spdif))
1870da3cec35STakashi Iwai 		return -ENXIO;
18711da177e4SLinus Torvalds 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
18721da177e4SLinus Torvalds 		return err;
18731da177e4SLinus Torvalds 	kctl->id.device = chip->pcm_spdif->device;
18741da177e4SLinus Torvalds 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
18751da177e4SLinus Torvalds 		return err;
18761da177e4SLinus Torvalds 	kctl->id.device = chip->pcm_spdif->device;
18771da177e4SLinus Torvalds 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
18781da177e4SLinus Torvalds 		return err;
18791da177e4SLinus Torvalds 	kctl->id.device = chip->pcm_spdif->device;
18801da177e4SLinus Torvalds 	chip->spdif_pcm_ctl = kctl;
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 	/* direct recording source */
18831da177e4SLinus Torvalds 	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
18841da177e4SLinus Torvalds 	    (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
18851da177e4SLinus Torvalds 		return err;
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds 	/*
18881da177e4SLinus Torvalds 	 * shared rear/line-in
18891da177e4SLinus Torvalds 	 */
18901da177e4SLinus Torvalds 	if (rear_switch) {
18911da177e4SLinus Torvalds 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
18921da177e4SLinus Torvalds 			return err;
18931da177e4SLinus Torvalds 	}
18941da177e4SLinus Torvalds 
18959bcf6551SClemens Ladisch 	/* per-voice volume */
18969bcf6551SClemens Ladisch 	substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
18979bcf6551SClemens Ladisch 	for (idx = 0; idx < 32; ++idx) {
18989bcf6551SClemens Ladisch 		kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
18999bcf6551SClemens Ladisch 		if (!kctl)
19009bcf6551SClemens Ladisch 			return -ENOMEM;
19019bcf6551SClemens Ladisch 		kctl->id.device = chip->pcm->device;
19029bcf6551SClemens Ladisch 		kctl->id.subdevice = idx;
19039bcf6551SClemens Ladisch 		kctl->private_value = (unsigned long)substream;
19049bcf6551SClemens Ladisch 		if ((err = snd_ctl_add(chip->card, kctl)) < 0)
19059bcf6551SClemens Ladisch 			return err;
19069bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].left = 0x8000;
19079bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].right = 0x8000;
19089bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].ctl = kctl;
19099bcf6551SClemens Ladisch 		substream = substream->next;
19109bcf6551SClemens Ladisch 	}
19119bcf6551SClemens Ladisch 
19121da177e4SLinus Torvalds 	return 0;
19131da177e4SLinus Torvalds }
19141da177e4SLinus Torvalds 
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds /*
19171da177e4SLinus Torvalds  * timer
19181da177e4SLinus Torvalds  */
19191da177e4SLinus Torvalds 
1920208a1b4cSTakashi Iwai static int snd_ymfpci_timer_start(struct snd_timer *timer)
19211da177e4SLinus Torvalds {
1922208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
19231da177e4SLinus Torvalds 	unsigned long flags;
19241da177e4SLinus Torvalds 	unsigned int count;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	chip = snd_timer_chip(timer);
19271da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
19286e2efaacSClemens Ladisch 	if (timer->sticks > 1) {
19296e2efaacSClemens Ladisch 		chip->timer_ticks = timer->sticks;
19306e2efaacSClemens Ladisch 		count = timer->sticks - 1;
19316e2efaacSClemens Ladisch 	} else {
19326e2efaacSClemens Ladisch 		/*
19336e2efaacSClemens Ladisch 		 * Divisor 1 is not allowed; fake it by using divisor 2 and
19346e2efaacSClemens Ladisch 		 * counting two ticks for each interrupt.
19356e2efaacSClemens Ladisch 		 */
19366e2efaacSClemens Ladisch 		chip->timer_ticks = 2;
19376e2efaacSClemens Ladisch 		count = 2 - 1;
19386e2efaacSClemens Ladisch 	}
19391da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
19401da177e4SLinus Torvalds 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
19411da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
19421da177e4SLinus Torvalds 	return 0;
19431da177e4SLinus Torvalds }
19441da177e4SLinus Torvalds 
1945208a1b4cSTakashi Iwai static int snd_ymfpci_timer_stop(struct snd_timer *timer)
19461da177e4SLinus Torvalds {
1947208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
19481da177e4SLinus Torvalds 	unsigned long flags;
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	chip = snd_timer_chip(timer);
19511da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
19521da177e4SLinus Torvalds 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
19531da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
19541da177e4SLinus Torvalds 	return 0;
19551da177e4SLinus Torvalds }
19561da177e4SLinus Torvalds 
1957208a1b4cSTakashi Iwai static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
19581da177e4SLinus Torvalds 					       unsigned long *num, unsigned long *den)
19591da177e4SLinus Torvalds {
19601da177e4SLinus Torvalds 	*num = 1;
19616e2efaacSClemens Ladisch 	*den = 96000;
19621da177e4SLinus Torvalds 	return 0;
19631da177e4SLinus Torvalds }
19641da177e4SLinus Torvalds 
1965208a1b4cSTakashi Iwai static struct snd_timer_hardware snd_ymfpci_timer_hw = {
19661da177e4SLinus Torvalds 	.flags = SNDRV_TIMER_HW_AUTO,
19676e2efaacSClemens Ladisch 	.resolution = 10417, /* 1 / 96 kHz = 10.41666...us */
19686e2efaacSClemens Ladisch 	.ticks = 0x10000,
19691da177e4SLinus Torvalds 	.start = snd_ymfpci_timer_start,
19701da177e4SLinus Torvalds 	.stop = snd_ymfpci_timer_stop,
19711da177e4SLinus Torvalds 	.precise_resolution = snd_ymfpci_timer_precise_resolution,
19721da177e4SLinus Torvalds };
19731da177e4SLinus Torvalds 
1974*e23e7a14SBill Pemberton int snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
19751da177e4SLinus Torvalds {
1976208a1b4cSTakashi Iwai 	struct snd_timer *timer = NULL;
1977208a1b4cSTakashi Iwai 	struct snd_timer_id tid;
19781da177e4SLinus Torvalds 	int err;
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds 	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
19811da177e4SLinus Torvalds 	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
19821da177e4SLinus Torvalds 	tid.card = chip->card->number;
19831da177e4SLinus Torvalds 	tid.device = device;
19841da177e4SLinus Torvalds 	tid.subdevice = 0;
19851da177e4SLinus Torvalds 	if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
19861da177e4SLinus Torvalds 		strcpy(timer->name, "YMFPCI timer");
19871da177e4SLinus Torvalds 		timer->private_data = chip;
19881da177e4SLinus Torvalds 		timer->hw = snd_ymfpci_timer_hw;
19891da177e4SLinus Torvalds 	}
19901da177e4SLinus Torvalds 	chip->timer = timer;
19911da177e4SLinus Torvalds 	return err;
19921da177e4SLinus Torvalds }
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 
19951da177e4SLinus Torvalds /*
19961da177e4SLinus Torvalds  *  proc interface
19971da177e4SLinus Torvalds  */
19981da177e4SLinus Torvalds 
1999208a1b4cSTakashi Iwai static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
2000208a1b4cSTakashi Iwai 				 struct snd_info_buffer *buffer)
20011da177e4SLinus Torvalds {
2002208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = entry->private_data;
20031da177e4SLinus Torvalds 	int i;
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds 	snd_iprintf(buffer, "YMFPCI\n\n");
20061da177e4SLinus Torvalds 	for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
20071da177e4SLinus Torvalds 		snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
20081da177e4SLinus Torvalds }
20091da177e4SLinus Torvalds 
2010*e23e7a14SBill Pemberton static int snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
20111da177e4SLinus Torvalds {
2012208a1b4cSTakashi Iwai 	struct snd_info_entry *entry;
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds 	if (! snd_card_proc_new(card, "ymfpci", &entry))
2015bf850204STakashi Iwai 		snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
20161da177e4SLinus Torvalds 	return 0;
20171da177e4SLinus Torvalds }
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds /*
20201da177e4SLinus Torvalds  *  initialization routines
20211da177e4SLinus Torvalds  */
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
20241da177e4SLinus Torvalds {
20251da177e4SLinus Torvalds 	u8 cmd;
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds 	pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
20281da177e4SLinus Torvalds #if 0 // force to reset
20291da177e4SLinus Torvalds 	if (cmd & 0x03) {
20301da177e4SLinus Torvalds #endif
20311da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
20321da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
20331da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
20341da177e4SLinus Torvalds 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
20351da177e4SLinus Torvalds 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
20361da177e4SLinus Torvalds #if 0
20371da177e4SLinus Torvalds 	}
20381da177e4SLinus Torvalds #endif
20391da177e4SLinus Torvalds }
20401da177e4SLinus Torvalds 
2041208a1b4cSTakashi Iwai static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
20421da177e4SLinus Torvalds {
20431da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
20441da177e4SLinus Torvalds }
20451da177e4SLinus Torvalds 
2046208a1b4cSTakashi Iwai static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
20471da177e4SLinus Torvalds {
20481da177e4SLinus Torvalds 	u32 val;
20491da177e4SLinus Torvalds 	int timeout = 1000;
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 	val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
20521da177e4SLinus Torvalds 	if (val)
20531da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
20541da177e4SLinus Torvalds 	while (timeout-- > 0) {
20551da177e4SLinus Torvalds 		val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
20561da177e4SLinus Torvalds 		if ((val & 0x00000002) == 0)
20571da177e4SLinus Torvalds 			break;
20581da177e4SLinus Torvalds 	}
20591da177e4SLinus Torvalds }
20601da177e4SLinus Torvalds 
2061102fa906SClemens Ladisch static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2062102fa906SClemens Ladisch {
2063102fa906SClemens Ladisch 	int err, is_1e;
2064102fa906SClemens Ladisch 	const char *name;
2065102fa906SClemens Ladisch 
2066102fa906SClemens Ladisch 	err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2067102fa906SClemens Ladisch 			       &chip->pci->dev);
2068102fa906SClemens Ladisch 	if (err >= 0) {
2069b82a82d0SDavid Woodhouse 		if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
2070102fa906SClemens Ladisch 			snd_printk(KERN_ERR "DSP microcode has wrong size\n");
2071102fa906SClemens Ladisch 			err = -EINVAL;
2072102fa906SClemens Ladisch 		}
2073102fa906SClemens Ladisch 	}
2074b7dd2b34STakashi Iwai 	if (err < 0)
2075102fa906SClemens Ladisch 		return err;
2076102fa906SClemens Ladisch 	is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2077102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2078102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2079102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2080102fa906SClemens Ladisch 	name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2081102fa906SClemens Ladisch 	err = request_firmware(&chip->controller_microcode, name,
2082102fa906SClemens Ladisch 			       &chip->pci->dev);
2083102fa906SClemens Ladisch 	if (err >= 0) {
2084b82a82d0SDavid Woodhouse 		if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
2085102fa906SClemens Ladisch 			snd_printk(KERN_ERR "controller microcode"
2086102fa906SClemens Ladisch 				   " has wrong size\n");
2087102fa906SClemens Ladisch 			err = -EINVAL;
2088102fa906SClemens Ladisch 		}
2089102fa906SClemens Ladisch 	}
2090b7dd2b34STakashi Iwai 	if (err < 0)
2091102fa906SClemens Ladisch 		return err;
2092102fa906SClemens Ladisch 	return 0;
2093102fa906SClemens Ladisch }
20947e0af29dSClemens Ladisch 
20957e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
20967e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
20977e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
20987e0af29dSClemens Ladisch 
2099208a1b4cSTakashi Iwai static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
21001da177e4SLinus Torvalds {
21011da177e4SLinus Torvalds 	int i;
21021da177e4SLinus Torvalds 	u16 ctrl;
2103b82a82d0SDavid Woodhouse 	const __le32 *inst;
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
21061da177e4SLinus Torvalds 	snd_ymfpci_disable_dsp(chip);
21071da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
21081da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
21091da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
21101da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
21111da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
21121da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
21131da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
21141da177e4SLinus Torvalds 	ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
21151da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds 	/* setup DSP instruction code */
2118b82a82d0SDavid Woodhouse 	inst = (const __le32 *)chip->dsp_microcode->data;
21191da177e4SLinus Torvalds 	for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2120b82a82d0SDavid Woodhouse 		snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2121b82a82d0SDavid Woodhouse 				  le32_to_cpu(inst[i]));
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 	/* setup control instruction code */
2124b82a82d0SDavid Woodhouse 	inst = (const __le32 *)chip->controller_microcode->data;
21251da177e4SLinus Torvalds 	for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2126b82a82d0SDavid Woodhouse 		snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2127b82a82d0SDavid Woodhouse 				  le32_to_cpu(inst[i]));
21281da177e4SLinus Torvalds 
21291da177e4SLinus Torvalds 	snd_ymfpci_enable_dsp(chip);
21301da177e4SLinus Torvalds }
21311da177e4SLinus Torvalds 
2132*e23e7a14SBill Pemberton static int snd_ymfpci_memalloc(struct snd_ymfpci *chip)
21331da177e4SLinus Torvalds {
21341da177e4SLinus Torvalds 	long size, playback_ctrl_size;
21351da177e4SLinus Torvalds 	int voice, bank, reg;
21361da177e4SLinus Torvalds 	u8 *ptr;
21371da177e4SLinus Torvalds 	dma_addr_t ptr_addr;
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds 	playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
21401da177e4SLinus Torvalds 	chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
21411da177e4SLinus Torvalds 	chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
21421da177e4SLinus Torvalds 	chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
21431da177e4SLinus Torvalds 	chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
21441da177e4SLinus Torvalds 
21457ab39926SClemens Ladisch 	size = ALIGN(playback_ctrl_size, 0x100) +
21467ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
21477ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
21487ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
21491da177e4SLinus Torvalds 	       chip->work_size;
21501da177e4SLinus Torvalds 	/* work_ptr must be aligned to 256 bytes, but it's already
21511da177e4SLinus Torvalds 	   covered with the kernel page allocation mechanism */
21521da177e4SLinus Torvalds 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
21531da177e4SLinus Torvalds 				size, &chip->work_ptr) < 0)
21541da177e4SLinus Torvalds 		return -ENOMEM;
21551da177e4SLinus Torvalds 	ptr = chip->work_ptr.area;
21561da177e4SLinus Torvalds 	ptr_addr = chip->work_ptr.addr;
21571da177e4SLinus Torvalds 	memset(ptr, 0, size);	/* for sure */
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 	chip->bank_base_playback = ptr;
21601da177e4SLinus Torvalds 	chip->bank_base_playback_addr = ptr_addr;
21611da177e4SLinus Torvalds 	chip->ctrl_playback = (u32 *)ptr;
21621da177e4SLinus Torvalds 	chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
21637ab39926SClemens Ladisch 	ptr += ALIGN(playback_ctrl_size, 0x100);
21647ab39926SClemens Ladisch 	ptr_addr += ALIGN(playback_ctrl_size, 0x100);
21651da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
21661da177e4SLinus Torvalds 		chip->voices[voice].number = voice;
2167208a1b4cSTakashi Iwai 		chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
21681da177e4SLinus Torvalds 		chip->voices[voice].bank_addr = ptr_addr;
21691da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2170208a1b4cSTakashi Iwai 			chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
21711da177e4SLinus Torvalds 			ptr += chip->bank_size_playback;
21721da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_playback;
21731da177e4SLinus Torvalds 		}
21741da177e4SLinus Torvalds 	}
21757ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21767ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21771da177e4SLinus Torvalds 	chip->bank_base_capture = ptr;
21781da177e4SLinus Torvalds 	chip->bank_base_capture_addr = ptr_addr;
21791da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
21801da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2181208a1b4cSTakashi Iwai 			chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
21821da177e4SLinus Torvalds 			ptr += chip->bank_size_capture;
21831da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_capture;
21841da177e4SLinus Torvalds 		}
21857ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21867ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21871da177e4SLinus Torvalds 	chip->bank_base_effect = ptr;
21881da177e4SLinus Torvalds 	chip->bank_base_effect_addr = ptr_addr;
21891da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
21901da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2191208a1b4cSTakashi Iwai 			chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
21921da177e4SLinus Torvalds 			ptr += chip->bank_size_effect;
21931da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_effect;
21941da177e4SLinus Torvalds 		}
21957ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21967ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21971da177e4SLinus Torvalds 	chip->work_base = ptr;
21981da177e4SLinus Torvalds 	chip->work_base_addr = ptr_addr;
21991da177e4SLinus Torvalds 
2200da3cec35STakashi Iwai 	snd_BUG_ON(ptr + chip->work_size !=
2201da3cec35STakashi Iwai 		   chip->work_ptr.area + chip->work_ptr.bytes);
22021da177e4SLinus Torvalds 
22031da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
22041da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
22051da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
22061da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
22071da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 	/* S/PDIF output initialization */
22101da177e4SLinus Torvalds 	chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
22111da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
22121da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds 	/* S/PDIF input initialization */
22151da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds 	/* digital mixer setup */
22181da177e4SLinus Torvalds 	for (reg = 0x80; reg < 0xc0; reg += 4)
22191da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, reg, 0);
22201da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
22214a3b6983STakashi Iwai 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
22221da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
22231da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
22241da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
22251da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
22261da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
22271da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds 	return 0;
22301da177e4SLinus Torvalds }
22311da177e4SLinus Torvalds 
2232208a1b4cSTakashi Iwai static int snd_ymfpci_free(struct snd_ymfpci *chip)
22331da177e4SLinus Torvalds {
22341da177e4SLinus Torvalds 	u16 ctrl;
22351da177e4SLinus Torvalds 
2236da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
2237da3cec35STakashi Iwai 		return -EINVAL;
22381da177e4SLinus Torvalds 
22391da177e4SLinus Torvalds 	if (chip->res_reg_area) {	/* don't touch busy hardware */
22401da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
22411da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
22421da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
22431da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
22441da177e4SLinus Torvalds 		snd_ymfpci_disable_dsp(chip);
22451da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
22461da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
22471da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
22481da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
22491da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
22501da177e4SLinus Torvalds 		ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
22511da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
22521da177e4SLinus Torvalds 	}
22531da177e4SLinus Torvalds 
22541da177e4SLinus Torvalds 	snd_ymfpci_ac3_done(chip);
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds 	/* Set PCI device to D3 state */
22571da177e4SLinus Torvalds #if 0
22581da177e4SLinus Torvalds 	/* FIXME: temporarily disabled, otherwise we cannot fire up
22591da177e4SLinus Torvalds 	 * the chip again unless reboot.  ACPI bug?
22601da177e4SLinus Torvalds 	 */
22611da177e4SLinus Torvalds 	pci_set_power_state(chip->pci, 3);
22621da177e4SLinus Torvalds #endif
22631da177e4SLinus Torvalds 
2264c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
22657009fa56STakashi Iwai 	kfree(chip->saved_regs);
22661da177e4SLinus Torvalds #endif
226795866d38STakashi Iwai 	if (chip->irq >= 0)
226895866d38STakashi Iwai 		free_irq(chip->irq, chip);
2269b1d5776dSTakashi Iwai 	release_and_free_resource(chip->mpu_res);
2270b1d5776dSTakashi Iwai 	release_and_free_resource(chip->fm_res);
22711da177e4SLinus Torvalds 	snd_ymfpci_free_gameport(chip);
22721da177e4SLinus Torvalds 	if (chip->reg_area_virt)
22731da177e4SLinus Torvalds 		iounmap(chip->reg_area_virt);
22741da177e4SLinus Torvalds 	if (chip->work_ptr.area)
22751da177e4SLinus Torvalds 		snd_dma_free_pages(&chip->work_ptr);
22761da177e4SLinus Torvalds 
2277b1d5776dSTakashi Iwai 	release_and_free_resource(chip->res_reg_area);
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds 	pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
22801da177e4SLinus Torvalds 
22811da177e4SLinus Torvalds 	pci_disable_device(chip->pci);
2282102fa906SClemens Ladisch 	release_firmware(chip->dsp_microcode);
2283102fa906SClemens Ladisch 	release_firmware(chip->controller_microcode);
22841da177e4SLinus Torvalds 	kfree(chip);
22851da177e4SLinus Torvalds 	return 0;
22861da177e4SLinus Torvalds }
22871da177e4SLinus Torvalds 
2288208a1b4cSTakashi Iwai static int snd_ymfpci_dev_free(struct snd_device *device)
22891da177e4SLinus Torvalds {
2290208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = device->device_data;
22911da177e4SLinus Torvalds 	return snd_ymfpci_free(chip);
22921da177e4SLinus Torvalds }
22931da177e4SLinus Torvalds 
2294c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
22951da177e4SLinus Torvalds static int saved_regs_index[] = {
22961da177e4SLinus Torvalds 	/* spdif */
22971da177e4SLinus Torvalds 	YDSXGR_SPDIFOUTCTRL,
22981da177e4SLinus Torvalds 	YDSXGR_SPDIFOUTSTATUS,
22991da177e4SLinus Torvalds 	YDSXGR_SPDIFINCTRL,
23001da177e4SLinus Torvalds 	/* volumes */
23011da177e4SLinus Torvalds 	YDSXGR_PRIADCLOOPVOL,
23021da177e4SLinus Torvalds 	YDSXGR_NATIVEDACINVOL,
23031da177e4SLinus Torvalds 	YDSXGR_NATIVEDACOUTVOL,
23049ed1261eSTeru KAMOGASHIRA 	YDSXGR_BUF441OUTVOL,
23051da177e4SLinus Torvalds 	YDSXGR_NATIVEADCINVOL,
23061da177e4SLinus Torvalds 	YDSXGR_SPDIFLOOPVOL,
23071da177e4SLinus Torvalds 	YDSXGR_SPDIFOUTVOL,
23081da177e4SLinus Torvalds 	YDSXGR_ZVOUTVOL,
23091da177e4SLinus Torvalds 	YDSXGR_LEGACYOUTVOL,
23101da177e4SLinus Torvalds 	/* address bases */
23111da177e4SLinus Torvalds 	YDSXGR_PLAYCTRLBASE,
23121da177e4SLinus Torvalds 	YDSXGR_RECCTRLBASE,
23131da177e4SLinus Torvalds 	YDSXGR_EFFCTRLBASE,
23141da177e4SLinus Torvalds 	YDSXGR_WORKBASE,
23151da177e4SLinus Torvalds 	/* capture set up */
23161da177e4SLinus Torvalds 	YDSXGR_MAPOFREC,
23171da177e4SLinus Torvalds 	YDSXGR_RECFORMAT,
23181da177e4SLinus Torvalds 	YDSXGR_RECSLOTSR,
23191da177e4SLinus Torvalds 	YDSXGR_ADCFORMAT,
23201da177e4SLinus Torvalds 	YDSXGR_ADCSLOTSR,
23211da177e4SLinus Torvalds };
23221da177e4SLinus Torvalds #define YDSXGR_NUM_SAVED_REGS	ARRAY_SIZE(saved_regs_index)
23231da177e4SLinus Torvalds 
232468cb2b55STakashi Iwai static int snd_ymfpci_suspend(struct device *dev)
23251da177e4SLinus Torvalds {
232668cb2b55STakashi Iwai 	struct pci_dev *pci = to_pci_dev(dev);
232768cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
2328ded46235STakashi Iwai 	struct snd_ymfpci *chip = card->private_data;
23291da177e4SLinus Torvalds 	unsigned int i;
23301da177e4SLinus Torvalds 
2331ded46235STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
23321da177e4SLinus Torvalds 	snd_pcm_suspend_all(chip->pcm);
23331da177e4SLinus Torvalds 	snd_pcm_suspend_all(chip->pcm2);
23341da177e4SLinus Torvalds 	snd_pcm_suspend_all(chip->pcm_spdif);
23351da177e4SLinus Torvalds 	snd_pcm_suspend_all(chip->pcm_4ch);
23361da177e4SLinus Torvalds 	snd_ac97_suspend(chip->ac97);
23371da177e4SLinus Torvalds 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
23381da177e4SLinus Torvalds 		chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
23391da177e4SLinus Torvalds 	chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
234028aa165cSTakashi Iwai 	pci_read_config_word(chip->pci, PCIR_DSXG_LEGACY,
234128aa165cSTakashi Iwai 			     &chip->saved_dsxg_legacy);
234228aa165cSTakashi Iwai 	pci_read_config_word(chip->pci, PCIR_DSXG_ELEGACY,
234328aa165cSTakashi Iwai 			     &chip->saved_dsxg_elegacy);
23441da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
23454a3b6983STakashi Iwai 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
23461da177e4SLinus Torvalds 	snd_ymfpci_disable_dsp(chip);
2347ded46235STakashi Iwai 	pci_disable_device(pci);
2348ded46235STakashi Iwai 	pci_save_state(pci);
234968cb2b55STakashi Iwai 	pci_set_power_state(pci, PCI_D3hot);
23501da177e4SLinus Torvalds 	return 0;
23511da177e4SLinus Torvalds }
23521da177e4SLinus Torvalds 
235368cb2b55STakashi Iwai static int snd_ymfpci_resume(struct device *dev)
23541da177e4SLinus Torvalds {
235568cb2b55STakashi Iwai 	struct pci_dev *pci = to_pci_dev(dev);
235668cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
2357ded46235STakashi Iwai 	struct snd_ymfpci *chip = card->private_data;
23581da177e4SLinus Torvalds 	unsigned int i;
23591da177e4SLinus Torvalds 
236030b35399STakashi Iwai 	pci_set_power_state(pci, PCI_D0);
2361ded46235STakashi Iwai 	pci_restore_state(pci);
236230b35399STakashi Iwai 	if (pci_enable_device(pci) < 0) {
236330b35399STakashi Iwai 		printk(KERN_ERR "ymfpci: pci_enable_device failed, "
236430b35399STakashi Iwai 		       "disabling device\n");
236530b35399STakashi Iwai 		snd_card_disconnect(card);
236630b35399STakashi Iwai 		return -EIO;
236730b35399STakashi Iwai 	}
2368ded46235STakashi Iwai 	pci_set_master(pci);
2369ded46235STakashi Iwai 	snd_ymfpci_aclink_reset(pci);
23701da177e4SLinus Torvalds 	snd_ymfpci_codec_ready(chip, 0);
23711da177e4SLinus Torvalds 	snd_ymfpci_download_image(chip);
23721da177e4SLinus Torvalds 	udelay(100);
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
23751da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
23761da177e4SLinus Torvalds 
23771da177e4SLinus Torvalds 	snd_ac97_resume(chip->ac97);
23781da177e4SLinus Torvalds 
237928aa165cSTakashi Iwai 	pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY,
238028aa165cSTakashi Iwai 			      chip->saved_dsxg_legacy);
238128aa165cSTakashi Iwai 	pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY,
238228aa165cSTakashi Iwai 			      chip->saved_dsxg_elegacy);
238328aa165cSTakashi Iwai 
23841da177e4SLinus Torvalds 	/* start hw again */
23851da177e4SLinus Torvalds 	if (chip->start_count > 0) {
23861da177e4SLinus Torvalds 		spin_lock_irq(&chip->reg_lock);
23871da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
23881da177e4SLinus Torvalds 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
23891da177e4SLinus Torvalds 		spin_unlock_irq(&chip->reg_lock);
23901da177e4SLinus Torvalds 	}
2391ded46235STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
23921da177e4SLinus Torvalds 	return 0;
23931da177e4SLinus Torvalds }
239468cb2b55STakashi Iwai 
239568cb2b55STakashi Iwai SIMPLE_DEV_PM_OPS(snd_ymfpci_pm, snd_ymfpci_suspend, snd_ymfpci_resume);
2396c7561cd8STakashi Iwai #endif /* CONFIG_PM_SLEEP */
23971da177e4SLinus Torvalds 
2398*e23e7a14SBill Pemberton int snd_ymfpci_create(struct snd_card *card,
23991da177e4SLinus Torvalds 		      struct pci_dev *pci,
24001da177e4SLinus Torvalds 		      unsigned short old_legacy_ctrl,
2401208a1b4cSTakashi Iwai 		      struct snd_ymfpci **rchip)
24021da177e4SLinus Torvalds {
2403208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
24041da177e4SLinus Torvalds 	int err;
2405208a1b4cSTakashi Iwai 	static struct snd_device_ops ops = {
24061da177e4SLinus Torvalds 		.dev_free =	snd_ymfpci_dev_free,
24071da177e4SLinus Torvalds 	};
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds 	*rchip = NULL;
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds 	/* enable PCI device */
24121da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
24131da177e4SLinus Torvalds 		return err;
24141da177e4SLinus Torvalds 
2415e560d8d8STakashi Iwai 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
24161da177e4SLinus Torvalds 	if (chip == NULL) {
24171da177e4SLinus Torvalds 		pci_disable_device(pci);
24181da177e4SLinus Torvalds 		return -ENOMEM;
24191da177e4SLinus Torvalds 	}
24201da177e4SLinus Torvalds 	chip->old_legacy_ctrl = old_legacy_ctrl;
24211da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
24221da177e4SLinus Torvalds 	spin_lock_init(&chip->voice_lock);
24231da177e4SLinus Torvalds 	init_waitqueue_head(&chip->interrupt_sleep);
24241da177e4SLinus Torvalds 	atomic_set(&chip->interrupt_sleep_count, 0);
24251da177e4SLinus Torvalds 	chip->card = card;
24261da177e4SLinus Torvalds 	chip->pci = pci;
24271da177e4SLinus Torvalds 	chip->irq = -1;
24281da177e4SLinus Torvalds 	chip->device_id = pci->device;
242944c10138SAuke Kok 	chip->rev = pci->revision;
24301da177e4SLinus Torvalds 	chip->reg_area_phys = pci_resource_start(pci, 0);
24311da177e4SLinus Torvalds 	chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
24321da177e4SLinus Torvalds 	pci_set_master(pci);
24339ed1261eSTeru KAMOGASHIRA 	chip->src441_used = -1;
24341da177e4SLinus Torvalds 
24351da177e4SLinus Torvalds 	if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
243699b359baSTakashi 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);
24371da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24381da177e4SLinus Torvalds 		return -EBUSY;
24391da177e4SLinus Torvalds 	}
2440437a5a46STakashi Iwai 	if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2441934c2b6dSTakashi Iwai 			KBUILD_MODNAME, chip)) {
244299b359baSTakashi Iwai 		snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
24431da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24441da177e4SLinus Torvalds 		return -EBUSY;
24451da177e4SLinus Torvalds 	}
24461da177e4SLinus Torvalds 	chip->irq = pci->irq;
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds 	snd_ymfpci_aclink_reset(pci);
24491da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0) < 0) {
24501da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24511da177e4SLinus Torvalds 		return -EIO;
24521da177e4SLinus Torvalds 	}
24531da177e4SLinus Torvalds 
2454102fa906SClemens Ladisch 	err = snd_ymfpci_request_firmware(chip);
2455102fa906SClemens Ladisch 	if (err < 0) {
2456102fa906SClemens Ladisch 		snd_printk(KERN_ERR "firmware request failed: %d\n", err);
2457102fa906SClemens Ladisch 		snd_ymfpci_free(chip);
2458102fa906SClemens Ladisch 		return err;
2459102fa906SClemens Ladisch 	}
24601da177e4SLinus Torvalds 	snd_ymfpci_download_image(chip);
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds 	udelay(100); /* seems we need a delay after downloading image.. */
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds 	if (snd_ymfpci_memalloc(chip) < 0) {
24651da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24661da177e4SLinus Torvalds 		return -EIO;
24671da177e4SLinus Torvalds 	}
24681da177e4SLinus Torvalds 
24691da177e4SLinus Torvalds 	if ((err = snd_ymfpci_ac3_init(chip)) < 0) {
24701da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24711da177e4SLinus Torvalds 		return err;
24721da177e4SLinus Torvalds 	}
24731da177e4SLinus Torvalds 
2474c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
24757009fa56STakashi Iwai 	chip->saved_regs = kmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32),
24767009fa56STakashi Iwai 				   GFP_KERNEL);
24771da177e4SLinus Torvalds 	if (chip->saved_regs == NULL) {
24781da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24791da177e4SLinus Torvalds 		return -ENOMEM;
24801da177e4SLinus Torvalds 	}
24811da177e4SLinus Torvalds #endif
24821da177e4SLinus Torvalds 
24831da177e4SLinus Torvalds 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
24841da177e4SLinus Torvalds 		snd_ymfpci_free(chip);
24851da177e4SLinus Torvalds 		return err;
24861da177e4SLinus Torvalds 	}
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 	snd_ymfpci_proc_init(card, chip);
24891da177e4SLinus Torvalds 
24901da177e4SLinus Torvalds 	snd_card_set_dev(card, &pci->dev);
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds 	*rchip = chip;
24931da177e4SLinus Torvalds 	return 0;
24941da177e4SLinus Torvalds }
2495