xref: /openbmc/linux/sound/pci/ymfpci/ymfpci_main.c (revision ba2186e40c9565b0a3ecd9e51ac1195cc501c182)
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>
306cbbfe1cSTakashi Iwai #include <linux/io.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <sound/core.h>
331da177e4SLinus Torvalds #include <sound/control.h>
341da177e4SLinus Torvalds #include <sound/info.h>
3533925186STakashi Iwai #include <sound/tlv.h>
3681fcb170STakashi Iwai #include "ymfpci.h"
371da177e4SLinus Torvalds #include <sound/asoundef.h>
381da177e4SLinus Torvalds #include <sound/mpu401.h>
391da177e4SLinus Torvalds 
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));
896436bcf6STakashi Iwai 	dev_err(chip->card->dev,
906436bcf6STakashi Iwai 		"codec_ready: codec %i is not ready [0x%x]\n",
916436bcf6STakashi Iwai 		secondary, snd_ymfpci_readw(chip, reg));
921da177e4SLinus Torvalds 	return -EBUSY;
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
95208a1b4cSTakashi Iwai static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
961da177e4SLinus Torvalds {
97208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
981da177e4SLinus Torvalds 	u32 cmd;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	snd_ymfpci_codec_ready(chip, 0);
1011da177e4SLinus Torvalds 	cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
1021da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
105208a1b4cSTakashi Iwai static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
1061da177e4SLinus Torvalds {
107208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0))
1101da177e4SLinus Torvalds 		return ~0;
1111da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
1121da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0))
1131da177e4SLinus Torvalds 		return ~0;
1141da177e4SLinus Torvalds 	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
1151da177e4SLinus Torvalds 		int i;
1161da177e4SLinus Torvalds 		for (i = 0; i < 600; i++)
1171da177e4SLinus Torvalds 			snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
1181da177e4SLinus Torvalds 	}
1191da177e4SLinus Torvalds 	return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /*
1231da177e4SLinus Torvalds  *  Misc routines
1241da177e4SLinus Torvalds  */
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds static u32 snd_ymfpci_calc_delta(u32 rate)
1271da177e4SLinus Torvalds {
1281da177e4SLinus Torvalds 	switch (rate) {
1291da177e4SLinus Torvalds 	case 8000:	return 0x02aaab00;
1301da177e4SLinus Torvalds 	case 11025:	return 0x03accd00;
1311da177e4SLinus Torvalds 	case 16000:	return 0x05555500;
1321da177e4SLinus Torvalds 	case 22050:	return 0x07599a00;
1331da177e4SLinus Torvalds 	case 32000:	return 0x0aaaab00;
1341da177e4SLinus Torvalds 	case 44100:	return 0x0eb33300;
1351da177e4SLinus Torvalds 	default:	return ((rate << 16) / 375) << 5;
1361da177e4SLinus Torvalds 	}
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds static u32 def_rate[8] = {
1401da177e4SLinus Torvalds 	100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
1411da177e4SLinus Torvalds };
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds static u32 snd_ymfpci_calc_lpfK(u32 rate)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	u32 i;
1461da177e4SLinus Torvalds 	static u32 val[8] = {
1471da177e4SLinus Torvalds 		0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
1481da177e4SLinus Torvalds 		0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
1491da177e4SLinus Torvalds 	};
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	if (rate == 44100)
1521da177e4SLinus Torvalds 		return 0x40000000;	/* FIXME: What's the right value? */
1531da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
1541da177e4SLinus Torvalds 		if (rate <= def_rate[i])
1551da177e4SLinus Torvalds 			return val[i];
1561da177e4SLinus Torvalds 	return val[0];
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds static u32 snd_ymfpci_calc_lpfQ(u32 rate)
1601da177e4SLinus Torvalds {
1611da177e4SLinus Torvalds 	u32 i;
1621da177e4SLinus Torvalds 	static u32 val[8] = {
1631da177e4SLinus Torvalds 		0x35280000, 0x34A70000, 0x32020000, 0x31770000,
1641da177e4SLinus Torvalds 		0x31390000, 0x31C90000, 0x33D00000, 0x40000000
1651da177e4SLinus Torvalds 	};
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	if (rate == 44100)
1681da177e4SLinus Torvalds 		return 0x370A0000;
1691da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
1701da177e4SLinus Torvalds 		if (rate <= def_rate[i])
1711da177e4SLinus Torvalds 			return val[i];
1721da177e4SLinus Torvalds 	return val[0];
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /*
1761da177e4SLinus Torvalds  *  Hardware start management
1771da177e4SLinus Torvalds  */
1781da177e4SLinus Torvalds 
179208a1b4cSTakashi Iwai static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	unsigned long flags;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
1841da177e4SLinus Torvalds 	if (chip->start_count++ > 0)
1851da177e4SLinus Torvalds 		goto __end;
1861da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE,
1871da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
1881da177e4SLinus Torvalds 	chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
1891da177e4SLinus Torvalds       __end:
1901da177e4SLinus Torvalds       	spin_unlock_irqrestore(&chip->reg_lock, flags);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
193208a1b4cSTakashi Iwai static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	unsigned long flags;
1961da177e4SLinus Torvalds 	long timeout = 1000;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
1991da177e4SLinus Torvalds 	if (--chip->start_count > 0)
2001da177e4SLinus Torvalds 		goto __end;
2011da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE,
2021da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
2031da177e4SLinus Torvalds 	while (timeout-- > 0) {
2041da177e4SLinus Torvalds 		if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
2051da177e4SLinus Torvalds 			break;
2061da177e4SLinus Torvalds 	}
2071da177e4SLinus Torvalds 	if (atomic_read(&chip->interrupt_sleep_count)) {
2081da177e4SLinus Torvalds 		atomic_set(&chip->interrupt_sleep_count, 0);
2091da177e4SLinus Torvalds 		wake_up(&chip->interrupt_sleep);
2101da177e4SLinus Torvalds 	}
2111da177e4SLinus Torvalds       __end:
2121da177e4SLinus Torvalds       	spin_unlock_irqrestore(&chip->reg_lock, flags);
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds /*
2161da177e4SLinus Torvalds  *  Playback voice management
2171da177e4SLinus Torvalds  */
2181da177e4SLinus Torvalds 
219208a1b4cSTakashi Iwai static int voice_alloc(struct snd_ymfpci *chip,
220208a1b4cSTakashi Iwai 		       enum snd_ymfpci_voice_type type, int pair,
221208a1b4cSTakashi Iwai 		       struct snd_ymfpci_voice **rvoice)
2221da177e4SLinus Torvalds {
223208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice, *voice2;
2241da177e4SLinus Torvalds 	int idx;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	*rvoice = NULL;
2271da177e4SLinus Torvalds 	for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
2281da177e4SLinus Torvalds 		voice = &chip->voices[idx];
2291da177e4SLinus Torvalds 		voice2 = pair ? &chip->voices[idx+1] : NULL;
2301da177e4SLinus Torvalds 		if (voice->use || (voice2 && voice2->use))
2311da177e4SLinus Torvalds 			continue;
2321da177e4SLinus Torvalds 		voice->use = 1;
2331da177e4SLinus Torvalds 		if (voice2)
2341da177e4SLinus Torvalds 			voice2->use = 1;
2351da177e4SLinus Torvalds 		switch (type) {
2361da177e4SLinus Torvalds 		case YMFPCI_PCM:
2371da177e4SLinus Torvalds 			voice->pcm = 1;
2381da177e4SLinus Torvalds 			if (voice2)
2391da177e4SLinus Torvalds 				voice2->pcm = 1;
2401da177e4SLinus Torvalds 			break;
2411da177e4SLinus Torvalds 		case YMFPCI_SYNTH:
2421da177e4SLinus Torvalds 			voice->synth = 1;
2431da177e4SLinus Torvalds 			break;
2441da177e4SLinus Torvalds 		case YMFPCI_MIDI:
2451da177e4SLinus Torvalds 			voice->midi = 1;
2461da177e4SLinus Torvalds 			break;
2471da177e4SLinus Torvalds 		}
2481da177e4SLinus Torvalds 		snd_ymfpci_hw_start(chip);
2491da177e4SLinus Torvalds 		if (voice2)
2501da177e4SLinus Torvalds 			snd_ymfpci_hw_start(chip);
2511da177e4SLinus Torvalds 		*rvoice = voice;
2521da177e4SLinus Torvalds 		return 0;
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 	return -ENOMEM;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
257208a1b4cSTakashi Iwai static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
258208a1b4cSTakashi Iwai 				  enum snd_ymfpci_voice_type type, int pair,
259208a1b4cSTakashi Iwai 				  struct snd_ymfpci_voice **rvoice)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds 	unsigned long flags;
2621da177e4SLinus Torvalds 	int result;
2631da177e4SLinus Torvalds 
264da3cec35STakashi Iwai 	if (snd_BUG_ON(!rvoice))
265da3cec35STakashi Iwai 		return -EINVAL;
266da3cec35STakashi Iwai 	if (snd_BUG_ON(pair && type != YMFPCI_PCM))
267da3cec35STakashi Iwai 		return -EINVAL;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->voice_lock, flags);
2701da177e4SLinus Torvalds 	for (;;) {
2711da177e4SLinus Torvalds 		result = voice_alloc(chip, type, pair, rvoice);
2721da177e4SLinus Torvalds 		if (result == 0 || type != YMFPCI_PCM)
2731da177e4SLinus Torvalds 			break;
2741da177e4SLinus Torvalds 		/* TODO: synth/midi voice deallocation */
2751da177e4SLinus Torvalds 		break;
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->voice_lock, flags);
2781da177e4SLinus Torvalds 	return result;
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
281208a1b4cSTakashi Iwai static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
2821da177e4SLinus Torvalds {
2831da177e4SLinus Torvalds 	unsigned long flags;
2841da177e4SLinus Torvalds 
285da3cec35STakashi Iwai 	if (snd_BUG_ON(!pvoice))
286da3cec35STakashi Iwai 		return -EINVAL;
2871da177e4SLinus Torvalds 	snd_ymfpci_hw_stop(chip);
2881da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->voice_lock, flags);
2899ed1261eSTeru KAMOGASHIRA 	if (pvoice->number == chip->src441_used) {
2909ed1261eSTeru KAMOGASHIRA 		chip->src441_used = -1;
2919ed1261eSTeru KAMOGASHIRA 		pvoice->ypcm->use_441_slot = 0;
2929ed1261eSTeru KAMOGASHIRA 	}
2931da177e4SLinus Torvalds 	pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
2941da177e4SLinus Torvalds 	pvoice->ypcm = NULL;
2951da177e4SLinus Torvalds 	pvoice->interrupt = NULL;
2961da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->voice_lock, flags);
2971da177e4SLinus Torvalds 	return 0;
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds /*
3011da177e4SLinus Torvalds  *  PCM part
3021da177e4SLinus Torvalds  */
3031da177e4SLinus Torvalds 
304208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
3051da177e4SLinus Torvalds {
306208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
3071da177e4SLinus Torvalds 	u32 pos, delta;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	if ((ypcm = voice->ypcm) == NULL)
3101da177e4SLinus Torvalds 		return;
3111da177e4SLinus Torvalds 	if (ypcm->substream == NULL)
3121da177e4SLinus Torvalds 		return;
3131da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3141da177e4SLinus Torvalds 	if (ypcm->running) {
3151da177e4SLinus Torvalds 		pos = le32_to_cpu(voice->bank[chip->active_bank].start);
3161da177e4SLinus Torvalds 		if (pos < ypcm->last_pos)
3171da177e4SLinus Torvalds 			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
3181da177e4SLinus Torvalds 		else
3191da177e4SLinus Torvalds 			delta = pos - ypcm->last_pos;
3201da177e4SLinus Torvalds 		ypcm->period_pos += delta;
3211da177e4SLinus Torvalds 		ypcm->last_pos = pos;
3221da177e4SLinus Torvalds 		if (ypcm->period_pos >= ypcm->period_size) {
323ee419653STakashi Iwai 			/*
3246436bcf6STakashi Iwai 			dev_dbg(chip->card->dev,
325ee419653STakashi Iwai 			       "done - active_bank = 0x%x, start = 0x%x\n",
326ee419653STakashi Iwai 			       chip->active_bank,
327ee419653STakashi Iwai 			       voice->bank[chip->active_bank].start);
328ee419653STakashi Iwai 			*/
3291da177e4SLinus Torvalds 			ypcm->period_pos %= ypcm->period_size;
3301da177e4SLinus Torvalds 			spin_unlock(&chip->reg_lock);
3311da177e4SLinus Torvalds 			snd_pcm_period_elapsed(ypcm->substream);
3321da177e4SLinus Torvalds 			spin_lock(&chip->reg_lock);
3331da177e4SLinus Torvalds 		}
3349bcf6551SClemens Ladisch 
3359bcf6551SClemens Ladisch 		if (unlikely(ypcm->update_pcm_vol)) {
3369bcf6551SClemens Ladisch 			unsigned int subs = ypcm->substream->number;
3379bcf6551SClemens Ladisch 			unsigned int next_bank = 1 - chip->active_bank;
338208a1b4cSTakashi Iwai 			struct snd_ymfpci_playback_bank *bank;
3399bcf6551SClemens Ladisch 			u32 volume;
3409bcf6551SClemens Ladisch 
3419bcf6551SClemens Ladisch 			bank = &voice->bank[next_bank];
3429bcf6551SClemens Ladisch 			volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
3439bcf6551SClemens Ladisch 			bank->left_gain_end = volume;
3449bcf6551SClemens Ladisch 			if (ypcm->output_rear)
3459bcf6551SClemens Ladisch 				bank->eff2_gain_end = volume;
3469bcf6551SClemens Ladisch 			if (ypcm->voices[1])
3479bcf6551SClemens Ladisch 				bank = &ypcm->voices[1]->bank[next_bank];
3489bcf6551SClemens Ladisch 			volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
3499bcf6551SClemens Ladisch 			bank->right_gain_end = volume;
3509bcf6551SClemens Ladisch 			if (ypcm->output_rear)
3519bcf6551SClemens Ladisch 				bank->eff3_gain_end = volume;
3529bcf6551SClemens Ladisch 			ypcm->update_pcm_vol--;
3539bcf6551SClemens Ladisch 		}
3541da177e4SLinus Torvalds 	}
3551da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
358208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
3591da177e4SLinus Torvalds {
360208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
361208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
362208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ypcm->chip;
3631da177e4SLinus Torvalds 	u32 pos, delta;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3661da177e4SLinus Torvalds 	if (ypcm->running) {
3671da177e4SLinus Torvalds 		pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
3681da177e4SLinus Torvalds 		if (pos < ypcm->last_pos)
3691da177e4SLinus Torvalds 			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
3701da177e4SLinus Torvalds 		else
3711da177e4SLinus Torvalds 			delta = pos - ypcm->last_pos;
3721da177e4SLinus Torvalds 		ypcm->period_pos += delta;
3731da177e4SLinus Torvalds 		ypcm->last_pos = pos;
3741da177e4SLinus Torvalds 		if (ypcm->period_pos >= ypcm->period_size) {
3751da177e4SLinus Torvalds 			ypcm->period_pos %= ypcm->period_size;
376ee419653STakashi Iwai 			/*
3776436bcf6STakashi Iwai 			dev_dbg(chip->card->dev,
378ee419653STakashi Iwai 			       "done - active_bank = 0x%x, start = 0x%x\n",
379ee419653STakashi Iwai 			       chip->active_bank,
380ee419653STakashi Iwai 			       voice->bank[chip->active_bank].start);
381ee419653STakashi Iwai 			*/
3821da177e4SLinus Torvalds 			spin_unlock(&chip->reg_lock);
3831da177e4SLinus Torvalds 			snd_pcm_period_elapsed(substream);
3841da177e4SLinus Torvalds 			spin_lock(&chip->reg_lock);
3851da177e4SLinus Torvalds 		}
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
390208a1b4cSTakashi Iwai static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
3911da177e4SLinus Torvalds 				       int cmd)
3921da177e4SLinus Torvalds {
393208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
394208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
395177a7cdbSClemens Ladisch 	struct snd_kcontrol *kctl = NULL;
3961da177e4SLinus Torvalds 	int result = 0;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3991da177e4SLinus Torvalds 	if (ypcm->voices[0] == NULL) {
4001da177e4SLinus Torvalds 		result = -EINVAL;
4011da177e4SLinus Torvalds 		goto __unlock;
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 	switch (cmd) {
4041da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4051da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
4061da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_RESUME:
4071da177e4SLinus Torvalds 		chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
4089ed1261eSTeru KAMOGASHIRA 		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
4091da177e4SLinus Torvalds 			chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
4101da177e4SLinus Torvalds 		ypcm->running = 1;
4111da177e4SLinus Torvalds 		break;
4121da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
413177a7cdbSClemens Ladisch 		if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
414177a7cdbSClemens Ladisch 			kctl = chip->pcm_mixer[substream->number].ctl;
415177a7cdbSClemens Ladisch 			kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
416177a7cdbSClemens Ladisch 		}
417177a7cdbSClemens Ladisch 		/* fall through */
4181da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
4191da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_SUSPEND:
4201da177e4SLinus Torvalds 		chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
4219ed1261eSTeru KAMOGASHIRA 		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
4221da177e4SLinus Torvalds 			chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
4231da177e4SLinus Torvalds 		ypcm->running = 0;
4241da177e4SLinus Torvalds 		break;
4251da177e4SLinus Torvalds 	default:
4261da177e4SLinus Torvalds 		result = -EINVAL;
4271da177e4SLinus Torvalds 		break;
4281da177e4SLinus Torvalds 	}
4291da177e4SLinus Torvalds       __unlock:
4301da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
431177a7cdbSClemens Ladisch 	if (kctl)
432177a7cdbSClemens Ladisch 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
4331da177e4SLinus Torvalds 	return result;
4341da177e4SLinus Torvalds }
435208a1b4cSTakashi Iwai static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
4361da177e4SLinus Torvalds 				      int cmd)
4371da177e4SLinus Torvalds {
438208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
439208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
4401da177e4SLinus Torvalds 	int result = 0;
4411da177e4SLinus Torvalds 	u32 tmp;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
4441da177e4SLinus Torvalds 	switch (cmd) {
4451da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4461da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
4471da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_RESUME:
4481da177e4SLinus Torvalds 		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
4491da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
4501da177e4SLinus Torvalds 		ypcm->running = 1;
4511da177e4SLinus Torvalds 		break;
4521da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4531da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
4541da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_SUSPEND:
4551da177e4SLinus Torvalds 		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
4561da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
4571da177e4SLinus Torvalds 		ypcm->running = 0;
4581da177e4SLinus Torvalds 		break;
4591da177e4SLinus Torvalds 	default:
4601da177e4SLinus Torvalds 		result = -EINVAL;
4611da177e4SLinus Torvalds 		break;
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4641da177e4SLinus Torvalds 	return result;
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
467208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds 	int err;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	if (ypcm->voices[1] != NULL && voices < 2) {
4721da177e4SLinus Torvalds 		snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
4731da177e4SLinus Torvalds 		ypcm->voices[1] = NULL;
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 	if (voices == 1 && ypcm->voices[0] != NULL)
4761da177e4SLinus Torvalds 		return 0;		/* already allocated */
4771da177e4SLinus Torvalds 	if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
4781da177e4SLinus Torvalds 		return 0;		/* already allocated */
4791da177e4SLinus Torvalds 	if (voices > 1) {
4801da177e4SLinus Torvalds 		if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
4811da177e4SLinus Torvalds 			snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
4821da177e4SLinus Torvalds 			ypcm->voices[0] = NULL;
4831da177e4SLinus Torvalds 		}
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds 	err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
4861da177e4SLinus Torvalds 	if (err < 0)
4871da177e4SLinus Torvalds 		return err;
4881da177e4SLinus Torvalds 	ypcm->voices[0]->ypcm = ypcm;
4891da177e4SLinus Torvalds 	ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
4901da177e4SLinus Torvalds 	if (voices > 1) {
4911da177e4SLinus Torvalds 		ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
4921da177e4SLinus Torvalds 		ypcm->voices[1]->ypcm = ypcm;
4931da177e4SLinus Torvalds 	}
4941da177e4SLinus Torvalds 	return 0;
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds 
497208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
498208a1b4cSTakashi Iwai 				      struct snd_pcm_runtime *runtime,
4999bcf6551SClemens Ladisch 				      int has_pcm_volume)
5001da177e4SLinus Torvalds {
501208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
5021da177e4SLinus Torvalds 	u32 format;
5039bcf6551SClemens Ladisch 	u32 delta = snd_ymfpci_calc_delta(runtime->rate);
5049bcf6551SClemens Ladisch 	u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
5059bcf6551SClemens Ladisch 	u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
506208a1b4cSTakashi Iwai 	struct snd_ymfpci_playback_bank *bank;
5071da177e4SLinus Torvalds 	unsigned int nbank;
5089bcf6551SClemens Ladisch 	u32 vol_left, vol_right;
5099bcf6551SClemens Ladisch 	u8 use_left, use_right;
5109ed1261eSTeru KAMOGASHIRA 	unsigned long flags;
5111da177e4SLinus Torvalds 
512da3cec35STakashi Iwai 	if (snd_BUG_ON(!voice))
513da3cec35STakashi Iwai 		return;
5149bcf6551SClemens Ladisch 	if (runtime->channels == 1) {
5159bcf6551SClemens Ladisch 		use_left = 1;
5169bcf6551SClemens Ladisch 		use_right = 1;
5179bcf6551SClemens Ladisch 	} else {
5189bcf6551SClemens Ladisch 		use_left = (voiceidx & 1) == 0;
5199bcf6551SClemens Ladisch 		use_right = !use_left;
5209bcf6551SClemens Ladisch 	}
5219bcf6551SClemens Ladisch 	if (has_pcm_volume) {
5229bcf6551SClemens Ladisch 		vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
5239bcf6551SClemens Ladisch 				       [ypcm->substream->number].left << 15);
5249bcf6551SClemens Ladisch 		vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
5259bcf6551SClemens Ladisch 					[ypcm->substream->number].right << 15);
5269bcf6551SClemens Ladisch 	} else {
5279bcf6551SClemens Ladisch 		vol_left = cpu_to_le32(0x40000000);
5289bcf6551SClemens Ladisch 		vol_right = cpu_to_le32(0x40000000);
5299bcf6551SClemens Ladisch 	}
5309ed1261eSTeru KAMOGASHIRA 	spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
5319bcf6551SClemens Ladisch 	format = runtime->channels == 2 ? 0x00010000 : 0;
5329bcf6551SClemens Ladisch 	if (snd_pcm_format_width(runtime->format) == 8)
5339bcf6551SClemens Ladisch 		format |= 0x80000000;
5349ed1261eSTeru KAMOGASHIRA 	else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
5359ed1261eSTeru KAMOGASHIRA 		 runtime->rate == 44100 && runtime->channels == 2 &&
5369ed1261eSTeru KAMOGASHIRA 		 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
5379ed1261eSTeru KAMOGASHIRA 				   ypcm->chip->src441_used == voice->number)) {
5389ed1261eSTeru KAMOGASHIRA 		ypcm->chip->src441_used = voice->number;
5399ed1261eSTeru KAMOGASHIRA 		ypcm->use_441_slot = 1;
5409ed1261eSTeru KAMOGASHIRA 		format |= 0x10000000;
5419ed1261eSTeru KAMOGASHIRA 	}
5429ed1261eSTeru KAMOGASHIRA 	if (ypcm->chip->src441_used == voice->number &&
5439ed1261eSTeru KAMOGASHIRA 	    (format & 0x10000000) == 0) {
5449ed1261eSTeru KAMOGASHIRA 		ypcm->chip->src441_used = -1;
5459ed1261eSTeru KAMOGASHIRA 		ypcm->use_441_slot = 0;
5469ed1261eSTeru KAMOGASHIRA 	}
5479bcf6551SClemens Ladisch 	if (runtime->channels == 2 && (voiceidx & 1) != 0)
5489bcf6551SClemens Ladisch 		format |= 1;
5499ed1261eSTeru KAMOGASHIRA 	spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
5501da177e4SLinus Torvalds 	for (nbank = 0; nbank < 2; nbank++) {
5511da177e4SLinus Torvalds 		bank = &voice->bank[nbank];
5529bcf6551SClemens Ladisch 		memset(bank, 0, sizeof(*bank));
5531da177e4SLinus Torvalds 		bank->format = cpu_to_le32(format);
5549bcf6551SClemens Ladisch 		bank->base = cpu_to_le32(runtime->dma_addr);
5559bcf6551SClemens Ladisch 		bank->loop_end = cpu_to_le32(ypcm->buffer_size);
5561da177e4SLinus Torvalds 		bank->lpfQ = cpu_to_le32(lpfQ);
5571da177e4SLinus Torvalds 		bank->delta =
5581da177e4SLinus Torvalds 		bank->delta_end = cpu_to_le32(delta);
5591da177e4SLinus Torvalds 		bank->lpfK =
5601da177e4SLinus Torvalds 		bank->lpfK_end = cpu_to_le32(lpfK);
5619bcf6551SClemens Ladisch 		bank->eg_gain =
5629bcf6551SClemens Ladisch 		bank->eg_gain_end = cpu_to_le32(0x40000000);
5631da177e4SLinus Torvalds 
5649bcf6551SClemens Ladisch 		if (ypcm->output_front) {
5659bcf6551SClemens Ladisch 			if (use_left) {
5661da177e4SLinus Torvalds 				bank->left_gain =
5679bcf6551SClemens Ladisch 				bank->left_gain_end = vol_left;
5689bcf6551SClemens Ladisch 			}
5699bcf6551SClemens Ladisch 			if (use_right) {
5701da177e4SLinus Torvalds 				bank->right_gain =
5719bcf6551SClemens Ladisch 				bank->right_gain_end = vol_right;
5729bcf6551SClemens Ladisch 			}
5739bcf6551SClemens Ladisch 		}
5749bcf6551SClemens Ladisch 		if (ypcm->output_rear) {
5755a25c5cfSJaroslav Kysela 		        if (!ypcm->swap_rear) {
5765a25c5cfSJaroslav Kysela         			if (use_left) {
5775a25c5cfSJaroslav Kysela         				bank->eff2_gain =
5785a25c5cfSJaroslav Kysela         				bank->eff2_gain_end = vol_left;
5795a25c5cfSJaroslav Kysela         			}
5805a25c5cfSJaroslav Kysela         			if (use_right) {
5815a25c5cfSJaroslav Kysela         				bank->eff3_gain =
5825a25c5cfSJaroslav Kysela         				bank->eff3_gain_end = vol_right;
5835a25c5cfSJaroslav Kysela         			}
5845a25c5cfSJaroslav Kysela 		        } else {
585153abaa4SClemens Ladisch         			/* The SPDIF out channels seem to be swapped, so we have
586153abaa4SClemens Ladisch         			 * to swap them here, too.  The rear analog out channels
587153abaa4SClemens Ladisch         			 * will be wrong, but otherwise AC3 would not work.
588153abaa4SClemens Ladisch         			 */
5899bcf6551SClemens Ladisch         			if (use_left) {
590153abaa4SClemens Ladisch         				bank->eff3_gain =
591153abaa4SClemens Ladisch         				bank->eff3_gain_end = vol_left;
5929bcf6551SClemens Ladisch         			}
5939bcf6551SClemens Ladisch         			if (use_right) {
594153abaa4SClemens Ladisch         				bank->eff2_gain =
595153abaa4SClemens Ladisch         				bank->eff2_gain_end = vol_right;
5961da177e4SLinus Torvalds         			}
5971da177e4SLinus Torvalds         		}
5981da177e4SLinus Torvalds                 }
5991da177e4SLinus Torvalds 	}
6005a25c5cfSJaroslav Kysela }
6011da177e4SLinus Torvalds 
602e23e7a14SBill Pemberton static int snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
6031da177e4SLinus Torvalds {
6041da177e4SLinus Torvalds 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
6051da177e4SLinus Torvalds 				4096, &chip->ac3_tmp_base) < 0)
6061da177e4SLinus Torvalds 		return -ENOMEM;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	chip->bank_effect[3][0]->base =
6091da177e4SLinus Torvalds 	chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
6101da177e4SLinus Torvalds 	chip->bank_effect[3][0]->loop_end =
6111da177e4SLinus Torvalds 	chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
6121da177e4SLinus Torvalds 	chip->bank_effect[4][0]->base =
6131da177e4SLinus Torvalds 	chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
6141da177e4SLinus Torvalds 	chip->bank_effect[4][0]->loop_end =
6151da177e4SLinus Torvalds 	chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
6181da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
6191da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
6201da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
6211da177e4SLinus Torvalds 	return 0;
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
624208a1b4cSTakashi Iwai static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
6271da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
6281da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
6291da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
6301da177e4SLinus Torvalds 	// snd_ymfpci_irq_wait(chip);
6311da177e4SLinus Torvalds 	if (chip->ac3_tmp_base.area) {
6321da177e4SLinus Torvalds 		snd_dma_free_pages(&chip->ac3_tmp_base);
6331da177e4SLinus Torvalds 		chip->ac3_tmp_base.area = NULL;
6341da177e4SLinus Torvalds 	}
6351da177e4SLinus Torvalds 	return 0;
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds 
638208a1b4cSTakashi Iwai static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
639208a1b4cSTakashi Iwai 					 struct snd_pcm_hw_params *hw_params)
6401da177e4SLinus Torvalds {
641208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
642208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
6431da177e4SLinus Torvalds 	int err;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
6461da177e4SLinus Torvalds 		return err;
6471da177e4SLinus Torvalds 	if ((err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params))) < 0)
6481da177e4SLinus Torvalds 		return err;
6491da177e4SLinus Torvalds 	return 0;
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
652208a1b4cSTakashi Iwai static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
6531da177e4SLinus Torvalds {
654208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
655208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
656208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	if (runtime->private_data == NULL)
6591da177e4SLinus Torvalds 		return 0;
6601da177e4SLinus Torvalds 	ypcm = runtime->private_data;
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	/* wait, until the PCI operations are not finished */
6631da177e4SLinus Torvalds 	snd_ymfpci_irq_wait(chip);
6641da177e4SLinus Torvalds 	snd_pcm_lib_free_pages(substream);
6651da177e4SLinus Torvalds 	if (ypcm->voices[1]) {
6661da177e4SLinus Torvalds 		snd_ymfpci_voice_free(chip, ypcm->voices[1]);
6671da177e4SLinus Torvalds 		ypcm->voices[1] = NULL;
6681da177e4SLinus Torvalds 	}
6691da177e4SLinus Torvalds 	if (ypcm->voices[0]) {
6701da177e4SLinus Torvalds 		snd_ymfpci_voice_free(chip, ypcm->voices[0]);
6711da177e4SLinus Torvalds 		ypcm->voices[0] = NULL;
6721da177e4SLinus Torvalds 	}
6731da177e4SLinus Torvalds 	return 0;
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
676208a1b4cSTakashi Iwai static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
6771da177e4SLinus Torvalds {
678208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
679208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
680208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
681177a7cdbSClemens Ladisch 	struct snd_kcontrol *kctl;
6821da177e4SLinus Torvalds 	unsigned int nvoice;
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	ypcm->period_size = runtime->period_size;
6851da177e4SLinus Torvalds 	ypcm->buffer_size = runtime->buffer_size;
6861da177e4SLinus Torvalds 	ypcm->period_pos = 0;
6871da177e4SLinus Torvalds 	ypcm->last_pos = 0;
6881da177e4SLinus Torvalds 	for (nvoice = 0; nvoice < runtime->channels; nvoice++)
6899bcf6551SClemens Ladisch 		snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
6909bcf6551SClemens Ladisch 					  substream->pcm == chip->pcm);
691177a7cdbSClemens Ladisch 
692177a7cdbSClemens Ladisch 	if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
693177a7cdbSClemens Ladisch 		kctl = chip->pcm_mixer[substream->number].ctl;
694177a7cdbSClemens Ladisch 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
695177a7cdbSClemens Ladisch 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
696177a7cdbSClemens Ladisch 	}
6971da177e4SLinus Torvalds 	return 0;
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
700208a1b4cSTakashi Iwai static int snd_ymfpci_capture_hw_params(struct snd_pcm_substream *substream,
701208a1b4cSTakashi Iwai 					struct snd_pcm_hw_params *hw_params)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
706208a1b4cSTakashi Iwai static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
7071da177e4SLinus Torvalds {
708208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	/* wait, until the PCI operations are not finished */
7111da177e4SLinus Torvalds 	snd_ymfpci_irq_wait(chip);
7121da177e4SLinus Torvalds 	return snd_pcm_lib_free_pages(substream);
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
715208a1b4cSTakashi Iwai static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
7161da177e4SLinus Torvalds {
717208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
718208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
719208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
720208a1b4cSTakashi Iwai 	struct snd_ymfpci_capture_bank * bank;
7211da177e4SLinus Torvalds 	int nbank;
7221da177e4SLinus Torvalds 	u32 rate, format;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	ypcm->period_size = runtime->period_size;
7251da177e4SLinus Torvalds 	ypcm->buffer_size = runtime->buffer_size;
7261da177e4SLinus Torvalds 	ypcm->period_pos = 0;
7271da177e4SLinus Torvalds 	ypcm->last_pos = 0;
7281da177e4SLinus Torvalds 	ypcm->shift = 0;
7291da177e4SLinus Torvalds 	rate = ((48000 * 4096) / runtime->rate) - 1;
7301da177e4SLinus Torvalds 	format = 0;
7311da177e4SLinus Torvalds 	if (runtime->channels == 2) {
7321da177e4SLinus Torvalds 		format |= 2;
7331da177e4SLinus Torvalds 		ypcm->shift++;
7341da177e4SLinus Torvalds 	}
7351da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 8)
7361da177e4SLinus Torvalds 		format |= 1;
7371da177e4SLinus Torvalds 	else
7381da177e4SLinus Torvalds 		ypcm->shift++;
7391da177e4SLinus Torvalds 	switch (ypcm->capture_bank_number) {
7401da177e4SLinus Torvalds 	case 0:
7411da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
7421da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
7431da177e4SLinus Torvalds 		break;
7441da177e4SLinus Torvalds 	case 1:
7451da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
7461da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
7471da177e4SLinus Torvalds 		break;
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds 	for (nbank = 0; nbank < 2; nbank++) {
7501da177e4SLinus Torvalds 		bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
7511da177e4SLinus Torvalds 		bank->base = cpu_to_le32(runtime->dma_addr);
7521da177e4SLinus Torvalds 		bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
7531da177e4SLinus Torvalds 		bank->start = 0;
7541da177e4SLinus Torvalds 		bank->num_of_loops = 0;
7551da177e4SLinus Torvalds 	}
7561da177e4SLinus Torvalds 	return 0;
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
759208a1b4cSTakashi Iwai static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
7601da177e4SLinus Torvalds {
761208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
762208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
763208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
764208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice = ypcm->voices[0];
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	if (!(ypcm->running && voice))
7671da177e4SLinus Torvalds 		return 0;
7681da177e4SLinus Torvalds 	return le32_to_cpu(voice->bank[chip->active_bank].start);
7691da177e4SLinus Torvalds }
7701da177e4SLinus Torvalds 
771208a1b4cSTakashi Iwai static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
7721da177e4SLinus Torvalds {
773208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
774208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
775208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	if (!ypcm->running)
7781da177e4SLinus Torvalds 		return 0;
7791da177e4SLinus Torvalds 	return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
7801da177e4SLinus Torvalds }
7811da177e4SLinus Torvalds 
782208a1b4cSTakashi Iwai static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
7831da177e4SLinus Torvalds {
784ac6424b9SIngo Molnar 	wait_queue_entry_t wait;
7851da177e4SLinus Torvalds 	int loops = 4;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	while (loops-- > 0) {
7881da177e4SLinus Torvalds 		if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
7891da177e4SLinus Torvalds 		 	continue;
7901da177e4SLinus Torvalds 		init_waitqueue_entry(&wait, current);
7911da177e4SLinus Torvalds 		add_wait_queue(&chip->interrupt_sleep, &wait);
7921da177e4SLinus Torvalds 		atomic_inc(&chip->interrupt_sleep_count);
7938433a509SNishanth Aravamudan 		schedule_timeout_uninterruptible(msecs_to_jiffies(50));
7941da177e4SLinus Torvalds 		remove_wait_queue(&chip->interrupt_sleep, &wait);
7951da177e4SLinus Torvalds 	}
7961da177e4SLinus Torvalds }
7971da177e4SLinus Torvalds 
7987d12e780SDavid Howells static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
7991da177e4SLinus Torvalds {
800208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = dev_id;
8011da177e4SLinus Torvalds 	u32 status, nvoice, mode;
802208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
8051da177e4SLinus Torvalds 	if (status & 0x80000000) {
8061da177e4SLinus Torvalds 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
8071da177e4SLinus Torvalds 		spin_lock(&chip->voice_lock);
8081da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
8091da177e4SLinus Torvalds 			voice = &chip->voices[nvoice];
8101da177e4SLinus Torvalds 			if (voice->interrupt)
8111da177e4SLinus Torvalds 				voice->interrupt(chip, voice);
8121da177e4SLinus Torvalds 		}
8131da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
8141da177e4SLinus Torvalds 			if (chip->capture_substream[nvoice])
8151da177e4SLinus Torvalds 				snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
8161da177e4SLinus Torvalds 		}
8171da177e4SLinus Torvalds #if 0
8181da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
8191da177e4SLinus Torvalds 			if (chip->effect_substream[nvoice])
8201da177e4SLinus Torvalds 				snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
8211da177e4SLinus Torvalds 		}
8221da177e4SLinus Torvalds #endif
8231da177e4SLinus Torvalds 		spin_unlock(&chip->voice_lock);
8241da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
8251da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
8261da177e4SLinus Torvalds 		mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
8271da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
8281da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 		if (atomic_read(&chip->interrupt_sleep_count)) {
8311da177e4SLinus Torvalds 			atomic_set(&chip->interrupt_sleep_count, 0);
8321da177e4SLinus Torvalds 			wake_up(&chip->interrupt_sleep);
8331da177e4SLinus Torvalds 		}
8341da177e4SLinus Torvalds 	}
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
8371da177e4SLinus Torvalds 	if (status & 1) {
8381da177e4SLinus Torvalds 		if (chip->timer)
8396e2efaacSClemens Ladisch 			snd_timer_interrupt(chip->timer, chip->timer_ticks);
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	if (chip->rawmidi)
8447d12e780SDavid Howells 		snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
8451da177e4SLinus Torvalds 	return IRQ_HANDLED;
8461da177e4SLinus Torvalds }
8471da177e4SLinus Torvalds 
848420b0c1bSBhumika Goyal static const struct snd_pcm_hardware snd_ymfpci_playback =
8491da177e4SLinus Torvalds {
8501da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
8511da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
8521da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_INTERLEAVED |
8531da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
8541da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE |
8551da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_RESUME),
8561da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
8571da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8581da177e4SLinus Torvalds 	.rate_min =		8000,
8591da177e4SLinus Torvalds 	.rate_max =		48000,
8601da177e4SLinus Torvalds 	.channels_min =		1,
8611da177e4SLinus Torvalds 	.channels_max =		2,
8621da177e4SLinus Torvalds 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
8631da177e4SLinus Torvalds 	.period_bytes_min =	64,
8641da177e4SLinus Torvalds 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
8651da177e4SLinus Torvalds 	.periods_min =		3,
8661da177e4SLinus Torvalds 	.periods_max =		1024,
8671da177e4SLinus Torvalds 	.fifo_size =		0,
8681da177e4SLinus Torvalds };
8691da177e4SLinus Torvalds 
870420b0c1bSBhumika Goyal static const struct snd_pcm_hardware snd_ymfpci_capture =
8711da177e4SLinus Torvalds {
8721da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
8731da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
8741da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_INTERLEAVED |
8751da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
8761da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE |
8771da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_RESUME),
8781da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
8791da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8801da177e4SLinus Torvalds 	.rate_min =		8000,
8811da177e4SLinus Torvalds 	.rate_max =		48000,
8821da177e4SLinus Torvalds 	.channels_min =		1,
8831da177e4SLinus Torvalds 	.channels_max =		2,
8841da177e4SLinus Torvalds 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
8851da177e4SLinus Torvalds 	.period_bytes_min =	64,
8861da177e4SLinus Torvalds 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
8871da177e4SLinus Torvalds 	.periods_min =		3,
8881da177e4SLinus Torvalds 	.periods_max =		1024,
8891da177e4SLinus Torvalds 	.fifo_size =		0,
8901da177e4SLinus Torvalds };
8911da177e4SLinus Torvalds 
892208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
8931da177e4SLinus Torvalds {
8944d572776SJesper Juhl 	kfree(runtime->private_data);
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
897208a1b4cSTakashi Iwai static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
8981da177e4SLinus Torvalds {
899208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
900208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
901208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
90284f9df15SClemens Ladisch 	int err;
90384f9df15SClemens Ladisch 
90484f9df15SClemens Ladisch 	runtime->hw = snd_ymfpci_playback;
90584f9df15SClemens Ladisch 	/* FIXME? True value is 256/48 = 5.33333 ms */
90684f9df15SClemens Ladisch 	err = snd_pcm_hw_constraint_minmax(runtime,
90784f9df15SClemens Ladisch 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
90884f9df15SClemens Ladisch 					   5334, UINT_MAX);
90984f9df15SClemens Ladisch 	if (err < 0)
91084f9df15SClemens Ladisch 		return err;
9115b0416a3SClemens Ladisch 	err = snd_pcm_hw_rule_noresample(runtime, 48000);
9125b0416a3SClemens Ladisch 	if (err < 0)
9135b0416a3SClemens Ladisch 		return err;
9141da177e4SLinus Torvalds 
915e560d8d8STakashi Iwai 	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
9161da177e4SLinus Torvalds 	if (ypcm == NULL)
9171da177e4SLinus Torvalds 		return -ENOMEM;
9181da177e4SLinus Torvalds 	ypcm->chip = chip;
9191da177e4SLinus Torvalds 	ypcm->type = PLAYBACK_VOICE;
9201da177e4SLinus Torvalds 	ypcm->substream = substream;
9211da177e4SLinus Torvalds 	runtime->private_data = ypcm;
9221da177e4SLinus Torvalds 	runtime->private_free = snd_ymfpci_pcm_free_substream;
9231da177e4SLinus Torvalds 	return 0;
9241da177e4SLinus Torvalds }
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds /* call with spinlock held */
927208a1b4cSTakashi Iwai static void ymfpci_open_extension(struct snd_ymfpci *chip)
9281da177e4SLinus Torvalds {
9291da177e4SLinus Torvalds 	if (! chip->rear_opened) {
9301da177e4SLinus Torvalds 		if (! chip->spdif_opened) /* set AC3 */
9311da177e4SLinus Torvalds 			snd_ymfpci_writel(chip, YDSXGR_MODE,
9321da177e4SLinus Torvalds 					  snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
9331da177e4SLinus Torvalds 		/* enable second codec (4CHEN) */
9341da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
9351da177e4SLinus Torvalds 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
9361da177e4SLinus Torvalds 	}
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds /* call with spinlock held */
940208a1b4cSTakashi Iwai static void ymfpci_close_extension(struct snd_ymfpci *chip)
9411da177e4SLinus Torvalds {
9421da177e4SLinus Torvalds 	if (! chip->rear_opened) {
9431da177e4SLinus Torvalds 		if (! chip->spdif_opened)
9441da177e4SLinus Torvalds 			snd_ymfpci_writel(chip, YDSXGR_MODE,
9451da177e4SLinus Torvalds 					  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
9461da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
9471da177e4SLinus Torvalds 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
9481da177e4SLinus Torvalds 	}
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
951208a1b4cSTakashi Iwai static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
9521da177e4SLinus Torvalds {
953208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
954208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
955208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
9561da177e4SLinus Torvalds 	int err;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
9591da177e4SLinus Torvalds 		return err;
9601da177e4SLinus Torvalds 	ypcm = runtime->private_data;
9611da177e4SLinus Torvalds 	ypcm->output_front = 1;
9621da177e4SLinus Torvalds 	ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
963d9301263SGlen Masgai 	ypcm->swap_rear = 0;
9641da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9651da177e4SLinus Torvalds 	if (ypcm->output_rear) {
9661da177e4SLinus Torvalds 		ymfpci_open_extension(chip);
9671da177e4SLinus Torvalds 		chip->rear_opened++;
9681da177e4SLinus Torvalds 	}
9691da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9701da177e4SLinus Torvalds 	return 0;
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
973208a1b4cSTakashi Iwai static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
9741da177e4SLinus Torvalds {
975208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
976208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
977208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
9781da177e4SLinus Torvalds 	int err;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
9811da177e4SLinus Torvalds 		return err;
9821da177e4SLinus Torvalds 	ypcm = runtime->private_data;
9831da177e4SLinus Torvalds 	ypcm->output_front = 0;
9841da177e4SLinus Torvalds 	ypcm->output_rear = 1;
985d9301263SGlen Masgai 	ypcm->swap_rear = 1;
9861da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9871da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
9881da177e4SLinus Torvalds 			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
9891da177e4SLinus Torvalds 	ymfpci_open_extension(chip);
9901da177e4SLinus Torvalds 	chip->spdif_pcm_bits = chip->spdif_bits;
9911da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
9921da177e4SLinus Torvalds 	chip->spdif_opened++;
9931da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
9961da177e4SLinus Torvalds 	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
9971da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
9981da177e4SLinus Torvalds 	return 0;
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds 
1001208a1b4cSTakashi Iwai static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
10021da177e4SLinus Torvalds {
1003208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1004208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
1005208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
10061da177e4SLinus Torvalds 	int err;
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	if ((err = snd_ymfpci_playback_open_1(substream)) < 0)
10091da177e4SLinus Torvalds 		return err;
10101da177e4SLinus Torvalds 	ypcm = runtime->private_data;
10111da177e4SLinus Torvalds 	ypcm->output_front = 0;
10121da177e4SLinus Torvalds 	ypcm->output_rear = 1;
1013d9301263SGlen Masgai 	ypcm->swap_rear = 0;
10141da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10151da177e4SLinus Torvalds 	ymfpci_open_extension(chip);
10161da177e4SLinus Torvalds 	chip->rear_opened++;
10171da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10181da177e4SLinus Torvalds 	return 0;
10191da177e4SLinus Torvalds }
10201da177e4SLinus Torvalds 
1021208a1b4cSTakashi Iwai static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
10221da177e4SLinus Torvalds 				   u32 capture_bank_number)
10231da177e4SLinus Torvalds {
1024208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1025208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
1026208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
102784f9df15SClemens Ladisch 	int err;
102884f9df15SClemens Ladisch 
102984f9df15SClemens Ladisch 	runtime->hw = snd_ymfpci_capture;
103084f9df15SClemens Ladisch 	/* FIXME? True value is 256/48 = 5.33333 ms */
103184f9df15SClemens Ladisch 	err = snd_pcm_hw_constraint_minmax(runtime,
103284f9df15SClemens Ladisch 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
103384f9df15SClemens Ladisch 					   5334, UINT_MAX);
103484f9df15SClemens Ladisch 	if (err < 0)
103584f9df15SClemens Ladisch 		return err;
10365b0416a3SClemens Ladisch 	err = snd_pcm_hw_rule_noresample(runtime, 48000);
10375b0416a3SClemens Ladisch 	if (err < 0)
10385b0416a3SClemens Ladisch 		return err;
10391da177e4SLinus Torvalds 
1040e560d8d8STakashi Iwai 	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
10411da177e4SLinus Torvalds 	if (ypcm == NULL)
10421da177e4SLinus Torvalds 		return -ENOMEM;
10431da177e4SLinus Torvalds 	ypcm->chip = chip;
10441da177e4SLinus Torvalds 	ypcm->type = capture_bank_number + CAPTURE_REC;
10451da177e4SLinus Torvalds 	ypcm->substream = substream;
10461da177e4SLinus Torvalds 	ypcm->capture_bank_number = capture_bank_number;
10471da177e4SLinus Torvalds 	chip->capture_substream[capture_bank_number] = substream;
10481da177e4SLinus Torvalds 	runtime->private_data = ypcm;
10491da177e4SLinus Torvalds 	runtime->private_free = snd_ymfpci_pcm_free_substream;
10501da177e4SLinus Torvalds 	snd_ymfpci_hw_start(chip);
10511da177e4SLinus Torvalds 	return 0;
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
1054208a1b4cSTakashi Iwai static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
10551da177e4SLinus Torvalds {
10561da177e4SLinus Torvalds 	return snd_ymfpci_capture_open(substream, 0);
10571da177e4SLinus Torvalds }
10581da177e4SLinus Torvalds 
1059208a1b4cSTakashi Iwai static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
10601da177e4SLinus Torvalds {
10611da177e4SLinus Torvalds 	return snd_ymfpci_capture_open(substream, 1);
10621da177e4SLinus Torvalds }
10631da177e4SLinus Torvalds 
1064208a1b4cSTakashi Iwai static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
10651da177e4SLinus Torvalds {
10661da177e4SLinus Torvalds 	return 0;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
1069208a1b4cSTakashi Iwai static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
10701da177e4SLinus Torvalds {
1071208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1072208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10751da177e4SLinus Torvalds 	if (ypcm->output_rear && chip->rear_opened > 0) {
10761da177e4SLinus Torvalds 		chip->rear_opened--;
10771da177e4SLinus Torvalds 		ymfpci_close_extension(chip);
10781da177e4SLinus Torvalds 	}
10791da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10801da177e4SLinus Torvalds 	return snd_ymfpci_playback_close_1(substream);
10811da177e4SLinus Torvalds }
10821da177e4SLinus Torvalds 
1083208a1b4cSTakashi Iwai static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
10841da177e4SLinus Torvalds {
1085208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10881da177e4SLinus Torvalds 	chip->spdif_opened = 0;
10891da177e4SLinus Torvalds 	ymfpci_close_extension(chip);
10901da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
10911da177e4SLinus Torvalds 			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
10921da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
10931da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10941da177e4SLinus Torvalds 	chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
10951da177e4SLinus Torvalds 	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
10961da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
10971da177e4SLinus Torvalds 	return snd_ymfpci_playback_close_1(substream);
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
1100208a1b4cSTakashi Iwai static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
11011da177e4SLinus Torvalds {
1102208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
11051da177e4SLinus Torvalds 	if (chip->rear_opened > 0) {
11061da177e4SLinus Torvalds 		chip->rear_opened--;
11071da177e4SLinus Torvalds 		ymfpci_close_extension(chip);
11081da177e4SLinus Torvalds 	}
11091da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
11101da177e4SLinus Torvalds 	return snd_ymfpci_playback_close_1(substream);
11111da177e4SLinus Torvalds }
11121da177e4SLinus Torvalds 
1113208a1b4cSTakashi Iwai static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
11141da177e4SLinus Torvalds {
1115208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1116208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
1117208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	if (ypcm != NULL) {
11201da177e4SLinus Torvalds 		chip->capture_substream[ypcm->capture_bank_number] = NULL;
11211da177e4SLinus Torvalds 		snd_ymfpci_hw_stop(chip);
11221da177e4SLinus Torvalds 	}
11231da177e4SLinus Torvalds 	return 0;
11241da177e4SLinus Torvalds }
11251da177e4SLinus Torvalds 
11266769e988SJulia Lawall static const struct snd_pcm_ops snd_ymfpci_playback_ops = {
11271da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_open,
11281da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_close,
11291da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
11301da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
11311da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
11321da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
11331da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
11341da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
11351da177e4SLinus Torvalds };
11361da177e4SLinus Torvalds 
11376769e988SJulia Lawall static const struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
11381da177e4SLinus Torvalds 	.open =			snd_ymfpci_capture_rec_open,
11391da177e4SLinus Torvalds 	.close =		snd_ymfpci_capture_close,
11401da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
11411da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_capture_hw_params,
11421da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_capture_hw_free,
11431da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_capture_prepare,
11441da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_capture_trigger,
11451da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_capture_pointer,
11461da177e4SLinus Torvalds };
11471da177e4SLinus Torvalds 
114838c47181SLars-Peter Clausen int snd_ymfpci_pcm(struct snd_ymfpci *chip, int device)
11491da177e4SLinus Torvalds {
1150208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
11511da177e4SLinus Torvalds 	int err;
11521da177e4SLinus Torvalds 
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 
116838c47181SLars-Peter Clausen 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
11690afdb8f2STakashi Iwai 				     snd_pcm_std_chmaps, 2, 0, NULL);
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds 
11726769e988SJulia Lawall static const struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
11731da177e4SLinus Torvalds 	.open =			snd_ymfpci_capture_ac97_open,
11741da177e4SLinus Torvalds 	.close =		snd_ymfpci_capture_close,
11751da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
11761da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_capture_hw_params,
11771da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_capture_hw_free,
11781da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_capture_prepare,
11791da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_capture_trigger,
11801da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_capture_pointer,
11811da177e4SLinus Torvalds };
11821da177e4SLinus Torvalds 
118338c47181SLars-Peter Clausen int snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device)
11841da177e4SLinus Torvalds {
1185208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
11861da177e4SLinus Torvalds 	int err;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm)) < 0)
11891da177e4SLinus Torvalds 		return err;
11901da177e4SLinus Torvalds 	pcm->private_data = chip;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	/* global setup */
11951da177e4SLinus Torvalds 	pcm->info_flags = 0;
11961da177e4SLinus Torvalds 	sprintf(pcm->name, "YMFPCI - %s",
11971da177e4SLinus Torvalds 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
11981da177e4SLinus Torvalds 	chip->pcm2 = pcm;
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
12011da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 	return 0;
12041da177e4SLinus Torvalds }
12051da177e4SLinus Torvalds 
12066769e988SJulia Lawall static const struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
12071da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_spdif_open,
12081da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_spdif_close,
12091da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
12101da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
12111da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
12121da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
12131da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
12141da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
12151da177e4SLinus Torvalds };
12161da177e4SLinus Torvalds 
121738c47181SLars-Peter Clausen int snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device)
12181da177e4SLinus Torvalds {
1219208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
12201da177e4SLinus Torvalds 	int err;
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm)) < 0)
12231da177e4SLinus Torvalds 		return err;
12241da177e4SLinus Torvalds 	pcm->private_data = chip;
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	/* global setup */
12291da177e4SLinus Torvalds 	pcm->info_flags = 0;
12301da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI - IEC958");
12311da177e4SLinus Torvalds 	chip->pcm_spdif = pcm;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
12341da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	return 0;
12371da177e4SLinus Torvalds }
12381da177e4SLinus Torvalds 
12396769e988SJulia Lawall static const struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
12401da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_4ch_open,
12411da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_4ch_close,
12421da177e4SLinus Torvalds 	.ioctl =		snd_pcm_lib_ioctl,
12431da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
12441da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
12451da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
12461da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
12471da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
12481da177e4SLinus Torvalds };
12491da177e4SLinus Torvalds 
12500afdb8f2STakashi Iwai static const struct snd_pcm_chmap_elem surround_map[] = {
12510afdb8f2STakashi Iwai 	{ .channels = 1,
12525efbc261STakashi Iwai 	  .map = { SNDRV_CHMAP_MONO } },
12530afdb8f2STakashi Iwai 	{ .channels = 2,
12540afdb8f2STakashi Iwai 	  .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
12550afdb8f2STakashi Iwai 	{ }
12560afdb8f2STakashi Iwai };
12570afdb8f2STakashi Iwai 
125838c47181SLars-Peter Clausen int snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device)
12591da177e4SLinus Torvalds {
1260208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
12611da177e4SLinus Torvalds 	int err;
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	if ((err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm)) < 0)
12641da177e4SLinus Torvalds 		return err;
12651da177e4SLinus Torvalds 	pcm->private_data = chip;
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 	/* global setup */
12701da177e4SLinus Torvalds 	pcm->info_flags = 0;
12711da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI - Rear PCM");
12721da177e4SLinus Torvalds 	chip->pcm_4ch = pcm;
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
12751da177e4SLinus Torvalds 					      snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
12761da177e4SLinus Torvalds 
127738c47181SLars-Peter Clausen 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
12780afdb8f2STakashi Iwai 				     surround_map, 2, 0, NULL);
12791da177e4SLinus Torvalds }
12801da177e4SLinus Torvalds 
1281208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
12821da177e4SLinus Torvalds {
12831da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
12841da177e4SLinus Torvalds 	uinfo->count = 1;
12851da177e4SLinus Torvalds 	return 0;
12861da177e4SLinus Torvalds }
12871da177e4SLinus Torvalds 
1288208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1289208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
12901da177e4SLinus Torvalds {
1291208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
12941da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
12951da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1296fc80a202SClemens Ladisch 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
12971da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
12981da177e4SLinus Torvalds 	return 0;
12991da177e4SLinus Torvalds }
13001da177e4SLinus Torvalds 
1301208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1302208a1b4cSTakashi Iwai 					 struct snd_ctl_elem_value *ucontrol)
13031da177e4SLinus Torvalds {
1304208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13051da177e4SLinus Torvalds 	unsigned int val;
13061da177e4SLinus Torvalds 	int change;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
13091da177e4SLinus Torvalds 	      (ucontrol->value.iec958.status[1] << 8);
13101da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13111da177e4SLinus Torvalds 	change = chip->spdif_bits != val;
13121da177e4SLinus Torvalds 	chip->spdif_bits = val;
13131da177e4SLinus Torvalds 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
13141da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
13151da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13161da177e4SLinus Torvalds 	return change;
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
1319f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_spdif_default =
13201da177e4SLinus Torvalds {
13211da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
13221da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
13231da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_default_info,
13241da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_default_get,
13251da177e4SLinus Torvalds 	.put =		snd_ymfpci_spdif_default_put
13261da177e4SLinus Torvalds };
13271da177e4SLinus Torvalds 
1328208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
13291da177e4SLinus Torvalds {
13301da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
13311da177e4SLinus Torvalds 	uinfo->count = 1;
13321da177e4SLinus Torvalds 	return 0;
13331da177e4SLinus Torvalds }
13341da177e4SLinus Torvalds 
1335208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1336208a1b4cSTakashi Iwai 				      struct snd_ctl_elem_value *ucontrol)
13371da177e4SLinus Torvalds {
1338208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13411da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = 0x3e;
13421da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = 0xff;
13431da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13441da177e4SLinus Torvalds 	return 0;
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds 
1347f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_spdif_mask =
13481da177e4SLinus Torvalds {
13491da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
13501da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
13511da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
13521da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_mask_info,
13531da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_mask_get,
13541da177e4SLinus Torvalds };
13551da177e4SLinus Torvalds 
1356208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
13571da177e4SLinus Torvalds {
13581da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
13591da177e4SLinus Torvalds 	uinfo->count = 1;
13601da177e4SLinus Torvalds 	return 0;
13611da177e4SLinus Torvalds }
13621da177e4SLinus Torvalds 
1363208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1364208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
13651da177e4SLinus Torvalds {
1366208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13691da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
13701da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1371fc80a202SClemens Ladisch 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
13721da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13731da177e4SLinus Torvalds 	return 0;
13741da177e4SLinus Torvalds }
13751da177e4SLinus Torvalds 
1376208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1377208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
13781da177e4SLinus Torvalds {
1379208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13801da177e4SLinus Torvalds 	unsigned int val;
13811da177e4SLinus Torvalds 	int change;
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
13841da177e4SLinus Torvalds 	      (ucontrol->value.iec958.status[1] << 8);
13851da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13861da177e4SLinus Torvalds 	change = chip->spdif_pcm_bits != val;
13871da177e4SLinus Torvalds 	chip->spdif_pcm_bits = val;
13881da177e4SLinus Torvalds 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
13891da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
13901da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13911da177e4SLinus Torvalds 	return change;
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds 
1394f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_spdif_stream =
13951da177e4SLinus Torvalds {
13961da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
13971da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
13981da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
13991da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_stream_info,
14001da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_stream_get,
14011da177e4SLinus Torvalds 	.put =		snd_ymfpci_spdif_stream_put
14021da177e4SLinus Torvalds };
14031da177e4SLinus Torvalds 
1404208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
14051da177e4SLinus Torvalds {
1406bed6896dSClemens Ladisch 	static const char *const texts[3] = {"AC'97", "IEC958", "ZV Port"};
14071da177e4SLinus Torvalds 
1408bed6896dSClemens Ladisch 	return snd_ctl_enum_info(info, 1, 3, texts);
14091da177e4SLinus Torvalds }
14101da177e4SLinus Torvalds 
1411208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
14121da177e4SLinus Torvalds {
1413208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
14141da177e4SLinus Torvalds 	u16 reg;
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
14171da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
14181da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
14191da177e4SLinus Torvalds 	if (!(reg & 0x100))
14201da177e4SLinus Torvalds 		value->value.enumerated.item[0] = 0;
14211da177e4SLinus Torvalds 	else
14221da177e4SLinus Torvalds 		value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
14231da177e4SLinus Torvalds 	return 0;
14241da177e4SLinus Torvalds }
14251da177e4SLinus Torvalds 
1426208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
14271da177e4SLinus Torvalds {
1428208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
14291da177e4SLinus Torvalds 	u16 reg, old_reg;
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
14321da177e4SLinus Torvalds 	old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
14331da177e4SLinus Torvalds 	if (value->value.enumerated.item[0] == 0)
14341da177e4SLinus Torvalds 		reg = old_reg & ~0x100;
14351da177e4SLinus Torvalds 	else
14361da177e4SLinus Torvalds 		reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
14371da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
14381da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
14391da177e4SLinus Torvalds 	return reg != old_reg;
14401da177e4SLinus Torvalds }
14411da177e4SLinus Torvalds 
1442f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_drec_source = {
14431da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
14441da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
14451da177e4SLinus Torvalds 	.name =		"Direct Recording Source",
14461da177e4SLinus Torvalds 	.info =		snd_ymfpci_drec_source_info,
14471da177e4SLinus Torvalds 	.get =		snd_ymfpci_drec_source_get,
14481da177e4SLinus Torvalds 	.put =		snd_ymfpci_drec_source_put
14491da177e4SLinus Torvalds };
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds /*
14521da177e4SLinus Torvalds  *  Mixer controls
14531da177e4SLinus Torvalds  */
14541da177e4SLinus Torvalds 
1455d602c885SGlen Masgai #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
14561da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
14571da177e4SLinus Torvalds   .info = snd_ymfpci_info_single, \
14581da177e4SLinus Torvalds   .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1459d602c885SGlen Masgai   .private_value = ((reg) | ((shift) << 16)) }
14601da177e4SLinus Torvalds 
1461a5ce8890STakashi Iwai #define snd_ymfpci_info_single		snd_ctl_boolean_mono_info
14621da177e4SLinus Torvalds 
1463208a1b4cSTakashi Iwai static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1464208a1b4cSTakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
14651da177e4SLinus Torvalds {
1466208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1467d602c885SGlen Masgai 	int reg = kcontrol->private_value & 0xffff;
1468d602c885SGlen Masgai 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1469d602c885SGlen Masgai 	unsigned int mask = 1;
14701da177e4SLinus Torvalds 
1471d602c885SGlen Masgai 	switch (reg) {
14721da177e4SLinus Torvalds 	case YDSXGR_SPDIFOUTCTRL: break;
14731da177e4SLinus Torvalds 	case YDSXGR_SPDIFINCTRL: break;
14741da177e4SLinus Torvalds 	default: return -EINVAL;
14751da177e4SLinus Torvalds 	}
1476d602c885SGlen Masgai 	ucontrol->value.integer.value[0] =
1477d602c885SGlen Masgai 		(snd_ymfpci_readl(chip, reg) >> shift) & mask;
14781da177e4SLinus Torvalds 	return 0;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
1481208a1b4cSTakashi Iwai static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1482208a1b4cSTakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
14831da177e4SLinus Torvalds {
1484208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1485d602c885SGlen Masgai 	int reg = kcontrol->private_value & 0xffff;
1486d602c885SGlen Masgai 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1487d602c885SGlen Masgai  	unsigned int mask = 1;
14881da177e4SLinus Torvalds 	int change;
14891da177e4SLinus Torvalds 	unsigned int val, oval;
14901da177e4SLinus Torvalds 
1491d602c885SGlen Masgai 	switch (reg) {
14921da177e4SLinus Torvalds 	case YDSXGR_SPDIFOUTCTRL: break;
14931da177e4SLinus Torvalds 	case YDSXGR_SPDIFINCTRL: break;
14941da177e4SLinus Torvalds 	default: return -EINVAL;
14951da177e4SLinus Torvalds 	}
14961da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
14971da177e4SLinus Torvalds 	val <<= shift;
14981da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
14991da177e4SLinus Torvalds 	oval = snd_ymfpci_readl(chip, reg);
15001da177e4SLinus Torvalds 	val = (oval & ~(mask << shift)) | val;
15011da177e4SLinus Torvalds 	change = val != oval;
15021da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, reg, val);
15031da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15041da177e4SLinus Torvalds 	return change;
15051da177e4SLinus Torvalds }
15061da177e4SLinus Torvalds 
15070cb29ea0STakashi Iwai static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
150833925186STakashi Iwai 
15091da177e4SLinus Torvalds #define YMFPCI_DOUBLE(xname, xindex, reg) \
15101da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
151133925186STakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
15121da177e4SLinus Torvalds   .info = snd_ymfpci_info_double, \
15131da177e4SLinus Torvalds   .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
151433925186STakashi Iwai   .private_value = reg, \
151533925186STakashi Iwai   .tlv = { .p = db_scale_native } }
15161da177e4SLinus Torvalds 
1517208a1b4cSTakashi Iwai static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
15181da177e4SLinus Torvalds {
15191da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
15221da177e4SLinus Torvalds 		return -EINVAL;
1523467a8c2fSAdrian Bunk 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
15241da177e4SLinus Torvalds 	uinfo->count = 2;
15251da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
1526467a8c2fSAdrian Bunk 	uinfo->value.integer.max = 16383;
15271da177e4SLinus Torvalds 	return 0;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
1530208a1b4cSTakashi Iwai static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15311da177e4SLinus Torvalds {
1532208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15331da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
1534467a8c2fSAdrian Bunk 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
15351da177e4SLinus Torvalds 	unsigned int val;
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
15381da177e4SLinus Torvalds 		return -EINVAL;
15391da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15401da177e4SLinus Torvalds 	val = snd_ymfpci_readl(chip, reg);
15411da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15421da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
15431da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
15441da177e4SLinus Torvalds 	return 0;
15451da177e4SLinus Torvalds }
15461da177e4SLinus Torvalds 
1547208a1b4cSTakashi Iwai static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15481da177e4SLinus Torvalds {
1549208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15501da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
1551467a8c2fSAdrian Bunk 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
15521da177e4SLinus Torvalds 	int change;
15531da177e4SLinus Torvalds 	unsigned int val1, val2, oval;
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
15561da177e4SLinus Torvalds 		return -EINVAL;
15571da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
15581da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
15591da177e4SLinus Torvalds 	val1 <<= shift_left;
15601da177e4SLinus Torvalds 	val2 <<= shift_right;
15611da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15621da177e4SLinus Torvalds 	oval = snd_ymfpci_readl(chip, reg);
15631da177e4SLinus Torvalds 	val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
15641da177e4SLinus Torvalds 	change = val1 != oval;
15651da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, reg, val1);
15661da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15671da177e4SLinus Torvalds 	return change;
15681da177e4SLinus Torvalds }
15691da177e4SLinus Torvalds 
1570177a7cdbSClemens Ladisch static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1571177a7cdbSClemens Ladisch 				       struct snd_ctl_elem_value *ucontrol)
1572177a7cdbSClemens Ladisch {
1573177a7cdbSClemens Ladisch 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1574177a7cdbSClemens Ladisch 	unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1575177a7cdbSClemens Ladisch 	unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1576177a7cdbSClemens Ladisch 	int change;
1577177a7cdbSClemens Ladisch 	unsigned int value, oval;
1578177a7cdbSClemens Ladisch 
1579177a7cdbSClemens Ladisch 	value = ucontrol->value.integer.value[0] & 0x3fff;
1580177a7cdbSClemens Ladisch 	value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1581177a7cdbSClemens Ladisch 	spin_lock_irq(&chip->reg_lock);
1582177a7cdbSClemens Ladisch 	oval = snd_ymfpci_readl(chip, reg);
1583177a7cdbSClemens Ladisch 	change = value != oval;
1584177a7cdbSClemens Ladisch 	snd_ymfpci_writel(chip, reg, value);
1585177a7cdbSClemens Ladisch 	snd_ymfpci_writel(chip, reg2, value);
1586177a7cdbSClemens Ladisch 	spin_unlock_irq(&chip->reg_lock);
1587177a7cdbSClemens Ladisch 	return change;
1588177a7cdbSClemens Ladisch }
1589177a7cdbSClemens Ladisch 
15901da177e4SLinus Torvalds /*
15911da177e4SLinus Torvalds  * 4ch duplication
15921da177e4SLinus Torvalds  */
1593a5ce8890STakashi Iwai #define snd_ymfpci_info_dup4ch		snd_ctl_boolean_mono_info
15941da177e4SLinus Torvalds 
1595208a1b4cSTakashi Iwai static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15961da177e4SLinus Torvalds {
1597208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15981da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = chip->mode_dup4ch;
15991da177e4SLinus Torvalds 	return 0;
16001da177e4SLinus Torvalds }
16011da177e4SLinus Torvalds 
1602208a1b4cSTakashi Iwai static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16031da177e4SLinus Torvalds {
1604208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
16051da177e4SLinus Torvalds 	int change;
16061da177e4SLinus Torvalds 	change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
16071da177e4SLinus Torvalds 	if (change)
16081da177e4SLinus Torvalds 		chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
16091da177e4SLinus Torvalds 	return change;
16101da177e4SLinus Torvalds }
16111da177e4SLinus Torvalds 
1612f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_dup4ch = {
16134d20bb1dSRaymond Yau 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
16144d20bb1dSRaymond Yau 	.name = "4ch Duplication",
16154d20bb1dSRaymond Yau 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
16164d20bb1dSRaymond Yau 	.info = snd_ymfpci_info_dup4ch,
16174d20bb1dSRaymond Yau 	.get = snd_ymfpci_get_dup4ch,
16184d20bb1dSRaymond Yau 	.put = snd_ymfpci_put_dup4ch,
16194d20bb1dSRaymond Yau };
16201da177e4SLinus Torvalds 
1621e23e7a14SBill Pemberton static struct snd_kcontrol_new snd_ymfpci_controls[] = {
1622177a7cdbSClemens Ladisch {
1623177a7cdbSClemens Ladisch 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1624177a7cdbSClemens Ladisch 	.name = "Wave Playback Volume",
1625177a7cdbSClemens Ladisch 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1626177a7cdbSClemens Ladisch 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1627177a7cdbSClemens Ladisch 	.info = snd_ymfpci_info_double,
1628177a7cdbSClemens Ladisch 	.get = snd_ymfpci_get_double,
1629177a7cdbSClemens Ladisch 	.put = snd_ymfpci_put_nativedacvol,
1630177a7cdbSClemens Ladisch 	.private_value = YDSXGR_NATIVEDACOUTVOL,
1631177a7cdbSClemens Ladisch 	.tlv = { .p = db_scale_native },
1632177a7cdbSClemens Ladisch },
16331da177e4SLinus Torvalds YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
16341da177e4SLinus Torvalds YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
16351da177e4SLinus Torvalds YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
16361da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
16371da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
16381da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
16391da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
164089f3325aSRaymond Yau YMFPCI_DOUBLE("FM Legacy Playback Volume", 0, YDSXGR_LEGACYOUTVOL),
16411da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
16421da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
16431da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
16441da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1645d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1646d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1647d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
16481da177e4SLinus Torvalds };
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds /*
16521da177e4SLinus Torvalds  * GPIO
16531da177e4SLinus Torvalds  */
16541da177e4SLinus Torvalds 
1655208a1b4cSTakashi Iwai static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
16561da177e4SLinus Torvalds {
16571da177e4SLinus Torvalds 	u16 reg, mode;
16581da177e4SLinus Torvalds 	unsigned long flags;
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
16611da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
16621da177e4SLinus Torvalds 	reg &= ~(1 << (pin + 8));
16631da177e4SLinus Torvalds 	reg |= (1 << pin);
16641da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
16651da177e4SLinus Torvalds 	/* set the level mode for input line */
16661da177e4SLinus Torvalds 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
16671da177e4SLinus Torvalds 	mode &= ~(3 << (pin * 2));
16681da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
16691da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
16701da177e4SLinus Torvalds 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
16711da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
16721da177e4SLinus Torvalds 	return (mode >> pin) & 1;
16731da177e4SLinus Torvalds }
16741da177e4SLinus Torvalds 
1675208a1b4cSTakashi Iwai static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
16761da177e4SLinus Torvalds {
16771da177e4SLinus Torvalds 	u16 reg;
16781da177e4SLinus Torvalds 	unsigned long flags;
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
16811da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
16821da177e4SLinus Torvalds 	reg &= ~(1 << pin);
16831da177e4SLinus Torvalds 	reg &= ~(1 << (pin + 8));
16841da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
16851da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
16861da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
16871da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	return 0;
16901da177e4SLinus Torvalds }
16911da177e4SLinus Torvalds 
1692a5ce8890STakashi Iwai #define snd_ymfpci_gpio_sw_info		snd_ctl_boolean_mono_info
16931da177e4SLinus Torvalds 
1694208a1b4cSTakashi Iwai static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16951da177e4SLinus Torvalds {
1696208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
16971da177e4SLinus Torvalds 	int pin = (int)kcontrol->private_value;
16981da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
16991da177e4SLinus Torvalds 	return 0;
17001da177e4SLinus Torvalds }
17011da177e4SLinus Torvalds 
1702208a1b4cSTakashi Iwai static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
17031da177e4SLinus Torvalds {
1704208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17051da177e4SLinus Torvalds 	int pin = (int)kcontrol->private_value;
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 	if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
17081da177e4SLinus Torvalds 		snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
17091da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
17101da177e4SLinus Torvalds 		return 1;
17111da177e4SLinus Torvalds 	}
17121da177e4SLinus Torvalds 	return 0;
17131da177e4SLinus Torvalds }
17141da177e4SLinus Torvalds 
1715f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_rear_shared = {
17161da177e4SLinus Torvalds 	.name = "Shared Rear/Line-In Switch",
17171da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
17181da177e4SLinus Torvalds 	.info = snd_ymfpci_gpio_sw_info,
17191da177e4SLinus Torvalds 	.get = snd_ymfpci_gpio_sw_get,
17201da177e4SLinus Torvalds 	.put = snd_ymfpci_gpio_sw_put,
17211da177e4SLinus Torvalds 	.private_value = 2,
17221da177e4SLinus Torvalds };
17231da177e4SLinus Torvalds 
17249bcf6551SClemens Ladisch /*
17259bcf6551SClemens Ladisch  * PCM voice volume
17269bcf6551SClemens Ladisch  */
17279bcf6551SClemens Ladisch 
1728208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1729208a1b4cSTakashi Iwai 				   struct snd_ctl_elem_info *uinfo)
17309bcf6551SClemens Ladisch {
17319bcf6551SClemens Ladisch 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
17329bcf6551SClemens Ladisch 	uinfo->count = 2;
17339bcf6551SClemens Ladisch 	uinfo->value.integer.min = 0;
17349bcf6551SClemens Ladisch 	uinfo->value.integer.max = 0x8000;
17359bcf6551SClemens Ladisch 	return 0;
17369bcf6551SClemens Ladisch }
17379bcf6551SClemens Ladisch 
1738208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1739208a1b4cSTakashi Iwai 				  struct snd_ctl_elem_value *ucontrol)
17409bcf6551SClemens Ladisch {
1741208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17429bcf6551SClemens Ladisch 	unsigned int subs = kcontrol->id.subdevice;
17439bcf6551SClemens Ladisch 
17449bcf6551SClemens Ladisch 	ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
17459bcf6551SClemens Ladisch 	ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
17469bcf6551SClemens Ladisch 	return 0;
17479bcf6551SClemens Ladisch }
17489bcf6551SClemens Ladisch 
1749208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1750208a1b4cSTakashi Iwai 				  struct snd_ctl_elem_value *ucontrol)
17519bcf6551SClemens Ladisch {
1752208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17539bcf6551SClemens Ladisch 	unsigned int subs = kcontrol->id.subdevice;
1754208a1b4cSTakashi Iwai 	struct snd_pcm_substream *substream;
17559bcf6551SClemens Ladisch 	unsigned long flags;
17569bcf6551SClemens Ladisch 
17579bcf6551SClemens Ladisch 	if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
17589bcf6551SClemens Ladisch 	    ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
17599bcf6551SClemens Ladisch 		chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
17609bcf6551SClemens Ladisch 		chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
17614e98d6a7STakashi Iwai 		if (chip->pcm_mixer[subs].left > 0x8000)
17624e98d6a7STakashi Iwai 			chip->pcm_mixer[subs].left = 0x8000;
17634e98d6a7STakashi Iwai 		if (chip->pcm_mixer[subs].right > 0x8000)
17644e98d6a7STakashi Iwai 			chip->pcm_mixer[subs].right = 0x8000;
17659bcf6551SClemens Ladisch 
1766208a1b4cSTakashi Iwai 		substream = (struct snd_pcm_substream *)kcontrol->private_value;
17679bcf6551SClemens Ladisch 		spin_lock_irqsave(&chip->voice_lock, flags);
17689bcf6551SClemens Ladisch 		if (substream->runtime && substream->runtime->private_data) {
1769208a1b4cSTakashi Iwai 			struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
17709ed1261eSTeru KAMOGASHIRA 			if (!ypcm->use_441_slot)
17719bcf6551SClemens Ladisch 				ypcm->update_pcm_vol = 2;
17729bcf6551SClemens Ladisch 		}
17739bcf6551SClemens Ladisch 		spin_unlock_irqrestore(&chip->voice_lock, flags);
17749bcf6551SClemens Ladisch 		return 1;
17759bcf6551SClemens Ladisch 	}
17769bcf6551SClemens Ladisch 	return 0;
17779bcf6551SClemens Ladisch }
17789bcf6551SClemens Ladisch 
1779f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_pcm_volume = {
17809bcf6551SClemens Ladisch 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
17819bcf6551SClemens Ladisch 	.name = "PCM Playback Volume",
17829bcf6551SClemens Ladisch 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
17839bcf6551SClemens Ladisch 		SNDRV_CTL_ELEM_ACCESS_INACTIVE,
17849bcf6551SClemens Ladisch 	.info = snd_ymfpci_pcm_vol_info,
17859bcf6551SClemens Ladisch 	.get = snd_ymfpci_pcm_vol_get,
17869bcf6551SClemens Ladisch 	.put = snd_ymfpci_pcm_vol_put,
17879bcf6551SClemens Ladisch };
17889bcf6551SClemens Ladisch 
17891da177e4SLinus Torvalds 
17901da177e4SLinus Torvalds /*
17911da177e4SLinus Torvalds  *  Mixer routines
17921da177e4SLinus Torvalds  */
17931da177e4SLinus Torvalds 
1794208a1b4cSTakashi Iwai static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
17951da177e4SLinus Torvalds {
1796208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = bus->private_data;
17971da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
17981da177e4SLinus Torvalds }
17991da177e4SLinus Torvalds 
1800208a1b4cSTakashi Iwai static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
18011da177e4SLinus Torvalds {
1802208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
18031da177e4SLinus Torvalds 	chip->ac97 = NULL;
18041da177e4SLinus Torvalds }
18051da177e4SLinus Torvalds 
1806e23e7a14SBill Pemberton int snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
18071da177e4SLinus Torvalds {
1808208a1b4cSTakashi Iwai 	struct snd_ac97_template ac97;
1809208a1b4cSTakashi Iwai 	struct snd_kcontrol *kctl;
1810208a1b4cSTakashi Iwai 	struct snd_pcm_substream *substream;
18111da177e4SLinus Torvalds 	unsigned int idx;
18121da177e4SLinus Torvalds 	int err;
1813208a1b4cSTakashi Iwai 	static struct snd_ac97_bus_ops ops = {
18141da177e4SLinus Torvalds 		.write = snd_ymfpci_codec_write,
18151da177e4SLinus Torvalds 		.read = snd_ymfpci_codec_read,
18161da177e4SLinus Torvalds 	};
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
18191da177e4SLinus Torvalds 		return err;
18201da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
18211da177e4SLinus Torvalds 	chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
18241da177e4SLinus Torvalds 	ac97.private_data = chip;
18251da177e4SLinus Torvalds 	ac97.private_free = snd_ymfpci_mixer_free_ac97;
18261da177e4SLinus Torvalds 	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
18271da177e4SLinus Torvalds 		return err;
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	/* to be sure */
18301da177e4SLinus Torvalds 	snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
18311da177e4SLinus Torvalds 			     AC97_EA_VRA|AC97_EA_VRM, 0);
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
18341da177e4SLinus Torvalds 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip))) < 0)
18351da177e4SLinus Torvalds 			return err;
18361da177e4SLinus Torvalds 	}
18374d20bb1dSRaymond Yau 	if (chip->ac97->ext_id & AC97_EI_SDAC) {
18384d20bb1dSRaymond Yau 		kctl = snd_ctl_new1(&snd_ymfpci_dup4ch, chip);
18394d20bb1dSRaymond Yau 		err = snd_ctl_add(chip->card, kctl);
18404d20bb1dSRaymond Yau 		if (err < 0)
18414d20bb1dSRaymond Yau 			return err;
18424d20bb1dSRaymond Yau 	}
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	/* add S/PDIF control */
1845da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip->pcm_spdif))
1846da3cec35STakashi Iwai 		return -ENXIO;
18471da177e4SLinus Torvalds 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip))) < 0)
18481da177e4SLinus Torvalds 		return err;
18491da177e4SLinus Torvalds 	kctl->id.device = chip->pcm_spdif->device;
18501da177e4SLinus Torvalds 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip))) < 0)
18511da177e4SLinus Torvalds 		return err;
18521da177e4SLinus Torvalds 	kctl->id.device = chip->pcm_spdif->device;
18531da177e4SLinus Torvalds 	if ((err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip))) < 0)
18541da177e4SLinus Torvalds 		return err;
18551da177e4SLinus Torvalds 	kctl->id.device = chip->pcm_spdif->device;
18561da177e4SLinus Torvalds 	chip->spdif_pcm_ctl = kctl;
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 	/* direct recording source */
18591da177e4SLinus Torvalds 	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
18601da177e4SLinus Torvalds 	    (err = snd_ctl_add(chip->card, kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip))) < 0)
18611da177e4SLinus Torvalds 		return err;
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	/*
18641da177e4SLinus Torvalds 	 * shared rear/line-in
18651da177e4SLinus Torvalds 	 */
18661da177e4SLinus Torvalds 	if (rear_switch) {
18671da177e4SLinus Torvalds 		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip))) < 0)
18681da177e4SLinus Torvalds 			return err;
18691da177e4SLinus Torvalds 	}
18701da177e4SLinus Torvalds 
18719bcf6551SClemens Ladisch 	/* per-voice volume */
18729bcf6551SClemens Ladisch 	substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
18739bcf6551SClemens Ladisch 	for (idx = 0; idx < 32; ++idx) {
18749bcf6551SClemens Ladisch 		kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
18759bcf6551SClemens Ladisch 		if (!kctl)
18769bcf6551SClemens Ladisch 			return -ENOMEM;
18779bcf6551SClemens Ladisch 		kctl->id.device = chip->pcm->device;
18789bcf6551SClemens Ladisch 		kctl->id.subdevice = idx;
18799bcf6551SClemens Ladisch 		kctl->private_value = (unsigned long)substream;
18809bcf6551SClemens Ladisch 		if ((err = snd_ctl_add(chip->card, kctl)) < 0)
18819bcf6551SClemens Ladisch 			return err;
18829bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].left = 0x8000;
18839bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].right = 0x8000;
18849bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].ctl = kctl;
18859bcf6551SClemens Ladisch 		substream = substream->next;
18869bcf6551SClemens Ladisch 	}
18879bcf6551SClemens Ladisch 
18881da177e4SLinus Torvalds 	return 0;
18891da177e4SLinus Torvalds }
18901da177e4SLinus Torvalds 
18911da177e4SLinus Torvalds 
18921da177e4SLinus Torvalds /*
18931da177e4SLinus Torvalds  * timer
18941da177e4SLinus Torvalds  */
18951da177e4SLinus Torvalds 
1896208a1b4cSTakashi Iwai static int snd_ymfpci_timer_start(struct snd_timer *timer)
18971da177e4SLinus Torvalds {
1898208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
18991da177e4SLinus Torvalds 	unsigned long flags;
19001da177e4SLinus Torvalds 	unsigned int count;
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds 	chip = snd_timer_chip(timer);
19031da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
19046e2efaacSClemens Ladisch 	if (timer->sticks > 1) {
19056e2efaacSClemens Ladisch 		chip->timer_ticks = timer->sticks;
19066e2efaacSClemens Ladisch 		count = timer->sticks - 1;
19076e2efaacSClemens Ladisch 	} else {
19086e2efaacSClemens Ladisch 		/*
19096e2efaacSClemens Ladisch 		 * Divisor 1 is not allowed; fake it by using divisor 2 and
19106e2efaacSClemens Ladisch 		 * counting two ticks for each interrupt.
19116e2efaacSClemens Ladisch 		 */
19126e2efaacSClemens Ladisch 		chip->timer_ticks = 2;
19136e2efaacSClemens Ladisch 		count = 2 - 1;
19146e2efaacSClemens Ladisch 	}
19151da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
19161da177e4SLinus Torvalds 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
19171da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
19181da177e4SLinus Torvalds 	return 0;
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
1921208a1b4cSTakashi Iwai static int snd_ymfpci_timer_stop(struct snd_timer *timer)
19221da177e4SLinus Torvalds {
1923208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
19241da177e4SLinus Torvalds 	unsigned long flags;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	chip = snd_timer_chip(timer);
19271da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
19281da177e4SLinus Torvalds 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
19291da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
19301da177e4SLinus Torvalds 	return 0;
19311da177e4SLinus Torvalds }
19321da177e4SLinus Torvalds 
1933208a1b4cSTakashi Iwai static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
19341da177e4SLinus Torvalds 					       unsigned long *num, unsigned long *den)
19351da177e4SLinus Torvalds {
19361da177e4SLinus Torvalds 	*num = 1;
19376e2efaacSClemens Ladisch 	*den = 96000;
19381da177e4SLinus Torvalds 	return 0;
19391da177e4SLinus Torvalds }
19401da177e4SLinus Torvalds 
1941208a1b4cSTakashi Iwai static struct snd_timer_hardware snd_ymfpci_timer_hw = {
19421da177e4SLinus Torvalds 	.flags = SNDRV_TIMER_HW_AUTO,
19436e2efaacSClemens Ladisch 	.resolution = 10417, /* 1 / 96 kHz = 10.41666...us */
19446e2efaacSClemens Ladisch 	.ticks = 0x10000,
19451da177e4SLinus Torvalds 	.start = snd_ymfpci_timer_start,
19461da177e4SLinus Torvalds 	.stop = snd_ymfpci_timer_stop,
19471da177e4SLinus Torvalds 	.precise_resolution = snd_ymfpci_timer_precise_resolution,
19481da177e4SLinus Torvalds };
19491da177e4SLinus Torvalds 
1950e23e7a14SBill Pemberton int snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
19511da177e4SLinus Torvalds {
1952208a1b4cSTakashi Iwai 	struct snd_timer *timer = NULL;
1953208a1b4cSTakashi Iwai 	struct snd_timer_id tid;
19541da177e4SLinus Torvalds 	int err;
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
19571da177e4SLinus Torvalds 	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
19581da177e4SLinus Torvalds 	tid.card = chip->card->number;
19591da177e4SLinus Torvalds 	tid.device = device;
19601da177e4SLinus Torvalds 	tid.subdevice = 0;
19611da177e4SLinus Torvalds 	if ((err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer)) >= 0) {
19621da177e4SLinus Torvalds 		strcpy(timer->name, "YMFPCI timer");
19631da177e4SLinus Torvalds 		timer->private_data = chip;
19641da177e4SLinus Torvalds 		timer->hw = snd_ymfpci_timer_hw;
19651da177e4SLinus Torvalds 	}
19661da177e4SLinus Torvalds 	chip->timer = timer;
19671da177e4SLinus Torvalds 	return err;
19681da177e4SLinus Torvalds }
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds /*
19721da177e4SLinus Torvalds  *  proc interface
19731da177e4SLinus Torvalds  */
19741da177e4SLinus Torvalds 
1975208a1b4cSTakashi Iwai static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1976208a1b4cSTakashi Iwai 				 struct snd_info_buffer *buffer)
19771da177e4SLinus Torvalds {
1978208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = entry->private_data;
19791da177e4SLinus Torvalds 	int i;
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 	snd_iprintf(buffer, "YMFPCI\n\n");
19821da177e4SLinus Torvalds 	for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
19831da177e4SLinus Torvalds 		snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
19841da177e4SLinus Torvalds }
19851da177e4SLinus Torvalds 
1986e23e7a14SBill Pemberton static int snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
19871da177e4SLinus Torvalds {
1988208a1b4cSTakashi Iwai 	struct snd_info_entry *entry;
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 	if (! snd_card_proc_new(card, "ymfpci", &entry))
1991bf850204STakashi Iwai 		snd_info_set_text_ops(entry, chip, snd_ymfpci_proc_read);
19921da177e4SLinus Torvalds 	return 0;
19931da177e4SLinus Torvalds }
19941da177e4SLinus Torvalds 
19951da177e4SLinus Torvalds /*
19961da177e4SLinus Torvalds  *  initialization routines
19971da177e4SLinus Torvalds  */
19981da177e4SLinus Torvalds 
19991da177e4SLinus Torvalds static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
20001da177e4SLinus Torvalds {
20011da177e4SLinus Torvalds 	u8 cmd;
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 	pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
20041da177e4SLinus Torvalds #if 0 // force to reset
20051da177e4SLinus Torvalds 	if (cmd & 0x03) {
20061da177e4SLinus Torvalds #endif
20071da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
20081da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
20091da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
20101da177e4SLinus Torvalds 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
20111da177e4SLinus Torvalds 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
20121da177e4SLinus Torvalds #if 0
20131da177e4SLinus Torvalds 	}
20141da177e4SLinus Torvalds #endif
20151da177e4SLinus Torvalds }
20161da177e4SLinus Torvalds 
2017208a1b4cSTakashi Iwai static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
20181da177e4SLinus Torvalds {
20191da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
20201da177e4SLinus Torvalds }
20211da177e4SLinus Torvalds 
2022208a1b4cSTakashi Iwai static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
20231da177e4SLinus Torvalds {
20241da177e4SLinus Torvalds 	u32 val;
20251da177e4SLinus Torvalds 	int timeout = 1000;
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds 	val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
20281da177e4SLinus Torvalds 	if (val)
20291da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
20301da177e4SLinus Torvalds 	while (timeout-- > 0) {
20311da177e4SLinus Torvalds 		val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
20321da177e4SLinus Torvalds 		if ((val & 0x00000002) == 0)
20331da177e4SLinus Torvalds 			break;
20341da177e4SLinus Torvalds 	}
20351da177e4SLinus Torvalds }
20361da177e4SLinus Torvalds 
2037102fa906SClemens Ladisch static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2038102fa906SClemens Ladisch {
2039102fa906SClemens Ladisch 	int err, is_1e;
2040102fa906SClemens Ladisch 	const char *name;
2041102fa906SClemens Ladisch 
2042102fa906SClemens Ladisch 	err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2043102fa906SClemens Ladisch 			       &chip->pci->dev);
2044102fa906SClemens Ladisch 	if (err >= 0) {
2045b82a82d0SDavid Woodhouse 		if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
20466436bcf6STakashi Iwai 			dev_err(chip->card->dev,
20476436bcf6STakashi Iwai 				"DSP microcode has wrong size\n");
2048102fa906SClemens Ladisch 			err = -EINVAL;
2049102fa906SClemens Ladisch 		}
2050102fa906SClemens Ladisch 	}
2051b7dd2b34STakashi Iwai 	if (err < 0)
2052102fa906SClemens Ladisch 		return err;
2053102fa906SClemens Ladisch 	is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2054102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2055102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2056102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2057102fa906SClemens Ladisch 	name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2058102fa906SClemens Ladisch 	err = request_firmware(&chip->controller_microcode, name,
2059102fa906SClemens Ladisch 			       &chip->pci->dev);
2060102fa906SClemens Ladisch 	if (err >= 0) {
2061b82a82d0SDavid Woodhouse 		if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
20626436bcf6STakashi Iwai 			dev_err(chip->card->dev,
20636436bcf6STakashi Iwai 				"controller microcode has wrong size\n");
2064102fa906SClemens Ladisch 			err = -EINVAL;
2065102fa906SClemens Ladisch 		}
2066102fa906SClemens Ladisch 	}
2067b7dd2b34STakashi Iwai 	if (err < 0)
2068102fa906SClemens Ladisch 		return err;
2069102fa906SClemens Ladisch 	return 0;
2070102fa906SClemens Ladisch }
20717e0af29dSClemens Ladisch 
20727e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
20737e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
20747e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
20757e0af29dSClemens Ladisch 
2076208a1b4cSTakashi Iwai static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
20771da177e4SLinus Torvalds {
20781da177e4SLinus Torvalds 	int i;
20791da177e4SLinus Torvalds 	u16 ctrl;
2080b82a82d0SDavid Woodhouse 	const __le32 *inst;
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
20831da177e4SLinus Torvalds 	snd_ymfpci_disable_dsp(chip);
20841da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
20851da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
20861da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
20871da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
20881da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
20891da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
20901da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
20911da177e4SLinus Torvalds 	ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
20921da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds 	/* setup DSP instruction code */
2095b82a82d0SDavid Woodhouse 	inst = (const __le32 *)chip->dsp_microcode->data;
20961da177e4SLinus Torvalds 	for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2097b82a82d0SDavid Woodhouse 		snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2098b82a82d0SDavid Woodhouse 				  le32_to_cpu(inst[i]));
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 	/* setup control instruction code */
2101b82a82d0SDavid Woodhouse 	inst = (const __le32 *)chip->controller_microcode->data;
21021da177e4SLinus Torvalds 	for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2103b82a82d0SDavid Woodhouse 		snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2104b82a82d0SDavid Woodhouse 				  le32_to_cpu(inst[i]));
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds 	snd_ymfpci_enable_dsp(chip);
21071da177e4SLinus Torvalds }
21081da177e4SLinus Torvalds 
2109e23e7a14SBill Pemberton static int snd_ymfpci_memalloc(struct snd_ymfpci *chip)
21101da177e4SLinus Torvalds {
21111da177e4SLinus Torvalds 	long size, playback_ctrl_size;
21121da177e4SLinus Torvalds 	int voice, bank, reg;
21131da177e4SLinus Torvalds 	u8 *ptr;
21141da177e4SLinus Torvalds 	dma_addr_t ptr_addr;
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
21171da177e4SLinus Torvalds 	chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
21181da177e4SLinus Torvalds 	chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
21191da177e4SLinus Torvalds 	chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
21201da177e4SLinus Torvalds 	chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
21211da177e4SLinus Torvalds 
21227ab39926SClemens Ladisch 	size = ALIGN(playback_ctrl_size, 0x100) +
21237ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
21247ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
21257ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
21261da177e4SLinus Torvalds 	       chip->work_size;
21271da177e4SLinus Torvalds 	/* work_ptr must be aligned to 256 bytes, but it's already
21281da177e4SLinus Torvalds 	   covered with the kernel page allocation mechanism */
21291da177e4SLinus Torvalds 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
21301da177e4SLinus Torvalds 				size, &chip->work_ptr) < 0)
21311da177e4SLinus Torvalds 		return -ENOMEM;
21321da177e4SLinus Torvalds 	ptr = chip->work_ptr.area;
21331da177e4SLinus Torvalds 	ptr_addr = chip->work_ptr.addr;
21341da177e4SLinus Torvalds 	memset(ptr, 0, size);	/* for sure */
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds 	chip->bank_base_playback = ptr;
21371da177e4SLinus Torvalds 	chip->bank_base_playback_addr = ptr_addr;
21381da177e4SLinus Torvalds 	chip->ctrl_playback = (u32 *)ptr;
21391da177e4SLinus Torvalds 	chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
21407ab39926SClemens Ladisch 	ptr += ALIGN(playback_ctrl_size, 0x100);
21417ab39926SClemens Ladisch 	ptr_addr += ALIGN(playback_ctrl_size, 0x100);
21421da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
21431da177e4SLinus Torvalds 		chip->voices[voice].number = voice;
2144208a1b4cSTakashi Iwai 		chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
21451da177e4SLinus Torvalds 		chip->voices[voice].bank_addr = ptr_addr;
21461da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2147208a1b4cSTakashi Iwai 			chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
21481da177e4SLinus Torvalds 			ptr += chip->bank_size_playback;
21491da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_playback;
21501da177e4SLinus Torvalds 		}
21511da177e4SLinus Torvalds 	}
21527ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21537ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21541da177e4SLinus Torvalds 	chip->bank_base_capture = ptr;
21551da177e4SLinus Torvalds 	chip->bank_base_capture_addr = ptr_addr;
21561da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
21571da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2158208a1b4cSTakashi Iwai 			chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
21591da177e4SLinus Torvalds 			ptr += chip->bank_size_capture;
21601da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_capture;
21611da177e4SLinus Torvalds 		}
21627ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21637ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21641da177e4SLinus Torvalds 	chip->bank_base_effect = ptr;
21651da177e4SLinus Torvalds 	chip->bank_base_effect_addr = ptr_addr;
21661da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
21671da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2168208a1b4cSTakashi Iwai 			chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
21691da177e4SLinus Torvalds 			ptr += chip->bank_size_effect;
21701da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_effect;
21711da177e4SLinus Torvalds 		}
21727ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21737ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21741da177e4SLinus Torvalds 	chip->work_base = ptr;
21751da177e4SLinus Torvalds 	chip->work_base_addr = ptr_addr;
21761da177e4SLinus Torvalds 
2177da3cec35STakashi Iwai 	snd_BUG_ON(ptr + chip->work_size !=
2178da3cec35STakashi Iwai 		   chip->work_ptr.area + chip->work_ptr.bytes);
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
21811da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
21821da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
21831da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
21841da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds 	/* S/PDIF output initialization */
21871da177e4SLinus Torvalds 	chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
21881da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
21891da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 	/* S/PDIF input initialization */
21921da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds 	/* digital mixer setup */
21951da177e4SLinus Torvalds 	for (reg = 0x80; reg < 0xc0; reg += 4)
21961da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, reg, 0);
21971da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
21984a3b6983STakashi Iwai 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
21991da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
22001da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
22011da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
22021da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
22031da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
22041da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds 	return 0;
22071da177e4SLinus Torvalds }
22081da177e4SLinus Torvalds 
2209208a1b4cSTakashi Iwai static int snd_ymfpci_free(struct snd_ymfpci *chip)
22101da177e4SLinus Torvalds {
22111da177e4SLinus Torvalds 	u16 ctrl;
22121da177e4SLinus Torvalds 
2213da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
2214da3cec35STakashi Iwai 		return -EINVAL;
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 	if (chip->res_reg_area) {	/* don't touch busy hardware */
22171da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
22181da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
22191da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
22201da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
22211da177e4SLinus Torvalds 		snd_ymfpci_disable_dsp(chip);
22221da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
22231da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
22241da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
22251da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
22261da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
22271da177e4SLinus Torvalds 		ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
22281da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
22291da177e4SLinus Torvalds 	}
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds 	snd_ymfpci_ac3_done(chip);
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds 	/* Set PCI device to D3 state */
22341da177e4SLinus Torvalds #if 0
22351da177e4SLinus Torvalds 	/* FIXME: temporarily disabled, otherwise we cannot fire up
22361da177e4SLinus Torvalds 	 * the chip again unless reboot.  ACPI bug?
22371da177e4SLinus Torvalds 	 */
2238db10e7fbSYijing Wang 	pci_set_power_state(chip->pci, PCI_D3hot);
22391da177e4SLinus Torvalds #endif
22401da177e4SLinus Torvalds 
2241c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
22427009fa56STakashi Iwai 	kfree(chip->saved_regs);
22431da177e4SLinus Torvalds #endif
224495866d38STakashi Iwai 	if (chip->irq >= 0)
224595866d38STakashi Iwai 		free_irq(chip->irq, chip);
2246b1d5776dSTakashi Iwai 	release_and_free_resource(chip->mpu_res);
2247b1d5776dSTakashi Iwai 	release_and_free_resource(chip->fm_res);
22481da177e4SLinus Torvalds 	snd_ymfpci_free_gameport(chip);
22491da177e4SLinus Torvalds 	iounmap(chip->reg_area_virt);
22501da177e4SLinus Torvalds 	if (chip->work_ptr.area)
22511da177e4SLinus Torvalds 		snd_dma_free_pages(&chip->work_ptr);
22521da177e4SLinus Torvalds 
2253b1d5776dSTakashi Iwai 	release_and_free_resource(chip->res_reg_area);
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds 	pci_write_config_word(chip->pci, 0x40, chip->old_legacy_ctrl);
22561da177e4SLinus Torvalds 
22571da177e4SLinus Torvalds 	pci_disable_device(chip->pci);
2258102fa906SClemens Ladisch 	release_firmware(chip->dsp_microcode);
2259102fa906SClemens Ladisch 	release_firmware(chip->controller_microcode);
22601da177e4SLinus Torvalds 	kfree(chip);
22611da177e4SLinus Torvalds 	return 0;
22621da177e4SLinus Torvalds }
22631da177e4SLinus Torvalds 
2264208a1b4cSTakashi Iwai static int snd_ymfpci_dev_free(struct snd_device *device)
22651da177e4SLinus Torvalds {
2266208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = device->device_data;
22671da177e4SLinus Torvalds 	return snd_ymfpci_free(chip);
22681da177e4SLinus Torvalds }
22691da177e4SLinus Torvalds 
2270c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
22711da177e4SLinus Torvalds static int saved_regs_index[] = {
22721da177e4SLinus Torvalds 	/* spdif */
22731da177e4SLinus Torvalds 	YDSXGR_SPDIFOUTCTRL,
22741da177e4SLinus Torvalds 	YDSXGR_SPDIFOUTSTATUS,
22751da177e4SLinus Torvalds 	YDSXGR_SPDIFINCTRL,
22761da177e4SLinus Torvalds 	/* volumes */
22771da177e4SLinus Torvalds 	YDSXGR_PRIADCLOOPVOL,
22781da177e4SLinus Torvalds 	YDSXGR_NATIVEDACINVOL,
22791da177e4SLinus Torvalds 	YDSXGR_NATIVEDACOUTVOL,
22809ed1261eSTeru KAMOGASHIRA 	YDSXGR_BUF441OUTVOL,
22811da177e4SLinus Torvalds 	YDSXGR_NATIVEADCINVOL,
22821da177e4SLinus Torvalds 	YDSXGR_SPDIFLOOPVOL,
22831da177e4SLinus Torvalds 	YDSXGR_SPDIFOUTVOL,
22841da177e4SLinus Torvalds 	YDSXGR_ZVOUTVOL,
22851da177e4SLinus Torvalds 	YDSXGR_LEGACYOUTVOL,
22861da177e4SLinus Torvalds 	/* address bases */
22871da177e4SLinus Torvalds 	YDSXGR_PLAYCTRLBASE,
22881da177e4SLinus Torvalds 	YDSXGR_RECCTRLBASE,
22891da177e4SLinus Torvalds 	YDSXGR_EFFCTRLBASE,
22901da177e4SLinus Torvalds 	YDSXGR_WORKBASE,
22911da177e4SLinus Torvalds 	/* capture set up */
22921da177e4SLinus Torvalds 	YDSXGR_MAPOFREC,
22931da177e4SLinus Torvalds 	YDSXGR_RECFORMAT,
22941da177e4SLinus Torvalds 	YDSXGR_RECSLOTSR,
22951da177e4SLinus Torvalds 	YDSXGR_ADCFORMAT,
22961da177e4SLinus Torvalds 	YDSXGR_ADCSLOTSR,
22971da177e4SLinus Torvalds };
22981da177e4SLinus Torvalds #define YDSXGR_NUM_SAVED_REGS	ARRAY_SIZE(saved_regs_index)
22991da177e4SLinus Torvalds 
230068cb2b55STakashi Iwai static int snd_ymfpci_suspend(struct device *dev)
23011da177e4SLinus Torvalds {
230268cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
2303ded46235STakashi Iwai 	struct snd_ymfpci *chip = card->private_data;
23041da177e4SLinus Torvalds 	unsigned int i;
23051da177e4SLinus Torvalds 
2306ded46235STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
23071da177e4SLinus Torvalds 	snd_pcm_suspend_all(chip->pcm);
23081da177e4SLinus Torvalds 	snd_pcm_suspend_all(chip->pcm2);
23091da177e4SLinus Torvalds 	snd_pcm_suspend_all(chip->pcm_spdif);
23101da177e4SLinus Torvalds 	snd_pcm_suspend_all(chip->pcm_4ch);
23111da177e4SLinus Torvalds 	snd_ac97_suspend(chip->ac97);
23121da177e4SLinus Torvalds 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
23131da177e4SLinus Torvalds 		chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
23141da177e4SLinus Torvalds 	chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
231528aa165cSTakashi Iwai 	pci_read_config_word(chip->pci, PCIR_DSXG_LEGACY,
231628aa165cSTakashi Iwai 			     &chip->saved_dsxg_legacy);
231728aa165cSTakashi Iwai 	pci_read_config_word(chip->pci, PCIR_DSXG_ELEGACY,
231828aa165cSTakashi Iwai 			     &chip->saved_dsxg_elegacy);
23191da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
23204a3b6983STakashi Iwai 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
23211da177e4SLinus Torvalds 	snd_ymfpci_disable_dsp(chip);
23221da177e4SLinus Torvalds 	return 0;
23231da177e4SLinus Torvalds }
23241da177e4SLinus Torvalds 
232568cb2b55STakashi Iwai static int snd_ymfpci_resume(struct device *dev)
23261da177e4SLinus Torvalds {
232768cb2b55STakashi Iwai 	struct pci_dev *pci = to_pci_dev(dev);
232868cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
2329ded46235STakashi Iwai 	struct snd_ymfpci *chip = card->private_data;
23301da177e4SLinus Torvalds 	unsigned int i;
23311da177e4SLinus Torvalds 
2332ded46235STakashi Iwai 	snd_ymfpci_aclink_reset(pci);
23331da177e4SLinus Torvalds 	snd_ymfpci_codec_ready(chip, 0);
23341da177e4SLinus Torvalds 	snd_ymfpci_download_image(chip);
23351da177e4SLinus Torvalds 	udelay(100);
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
23381da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds 	snd_ac97_resume(chip->ac97);
23411da177e4SLinus Torvalds 
234228aa165cSTakashi Iwai 	pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY,
234328aa165cSTakashi Iwai 			      chip->saved_dsxg_legacy);
234428aa165cSTakashi Iwai 	pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY,
234528aa165cSTakashi Iwai 			      chip->saved_dsxg_elegacy);
234628aa165cSTakashi Iwai 
23471da177e4SLinus Torvalds 	/* start hw again */
23481da177e4SLinus Torvalds 	if (chip->start_count > 0) {
23491da177e4SLinus Torvalds 		spin_lock_irq(&chip->reg_lock);
23501da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
23511da177e4SLinus Torvalds 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
23521da177e4SLinus Torvalds 		spin_unlock_irq(&chip->reg_lock);
23531da177e4SLinus Torvalds 	}
2354ded46235STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
23551da177e4SLinus Torvalds 	return 0;
23561da177e4SLinus Torvalds }
235768cb2b55STakashi Iwai 
235868cb2b55STakashi Iwai SIMPLE_DEV_PM_OPS(snd_ymfpci_pm, snd_ymfpci_suspend, snd_ymfpci_resume);
2359c7561cd8STakashi Iwai #endif /* CONFIG_PM_SLEEP */
23601da177e4SLinus Torvalds 
2361e23e7a14SBill Pemberton int snd_ymfpci_create(struct snd_card *card,
23621da177e4SLinus Torvalds 		      struct pci_dev *pci,
23631da177e4SLinus Torvalds 		      unsigned short old_legacy_ctrl,
2364208a1b4cSTakashi Iwai 		      struct snd_ymfpci **rchip)
23651da177e4SLinus Torvalds {
2366208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
23671da177e4SLinus Torvalds 	int err;
2368208a1b4cSTakashi Iwai 	static struct snd_device_ops ops = {
23691da177e4SLinus Torvalds 		.dev_free =	snd_ymfpci_dev_free,
23701da177e4SLinus Torvalds 	};
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 	*rchip = NULL;
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds 	/* enable PCI device */
23751da177e4SLinus Torvalds 	if ((err = pci_enable_device(pci)) < 0)
23761da177e4SLinus Torvalds 		return err;
23771da177e4SLinus Torvalds 
2378e560d8d8STakashi Iwai 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
23791da177e4SLinus Torvalds 	if (chip == NULL) {
23801da177e4SLinus Torvalds 		pci_disable_device(pci);
23811da177e4SLinus Torvalds 		return -ENOMEM;
23821da177e4SLinus Torvalds 	}
23831da177e4SLinus Torvalds 	chip->old_legacy_ctrl = old_legacy_ctrl;
23841da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
23851da177e4SLinus Torvalds 	spin_lock_init(&chip->voice_lock);
23861da177e4SLinus Torvalds 	init_waitqueue_head(&chip->interrupt_sleep);
23871da177e4SLinus Torvalds 	atomic_set(&chip->interrupt_sleep_count, 0);
23881da177e4SLinus Torvalds 	chip->card = card;
23891da177e4SLinus Torvalds 	chip->pci = pci;
23901da177e4SLinus Torvalds 	chip->irq = -1;
23911da177e4SLinus Torvalds 	chip->device_id = pci->device;
239244c10138SAuke Kok 	chip->rev = pci->revision;
23931da177e4SLinus Torvalds 	chip->reg_area_phys = pci_resource_start(pci, 0);
23941da177e4SLinus Torvalds 	chip->reg_area_virt = ioremap_nocache(chip->reg_area_phys, 0x8000);
23951da177e4SLinus Torvalds 	pci_set_master(pci);
23969ed1261eSTeru KAMOGASHIRA 	chip->src441_used = -1;
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 	if ((chip->res_reg_area = request_mem_region(chip->reg_area_phys, 0x8000, "YMFPCI")) == NULL) {
23996436bcf6STakashi Iwai 		dev_err(chip->card->dev,
24006436bcf6STakashi Iwai 			"unable to grab memory region 0x%lx-0x%lx\n",
24016436bcf6STakashi Iwai 			chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
2402*ba2186e4SMarkus Elfring 		err = -EBUSY;
2403*ba2186e4SMarkus Elfring 		goto free_chip;
24041da177e4SLinus Torvalds 	}
2405437a5a46STakashi Iwai 	if (request_irq(pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2406934c2b6dSTakashi Iwai 			KBUILD_MODNAME, chip)) {
24076436bcf6STakashi Iwai 		dev_err(chip->card->dev, "unable to grab IRQ %d\n", pci->irq);
2408*ba2186e4SMarkus Elfring 		err = -EBUSY;
2409*ba2186e4SMarkus Elfring 		goto free_chip;
24101da177e4SLinus Torvalds 	}
24111da177e4SLinus Torvalds 	chip->irq = pci->irq;
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds 	snd_ymfpci_aclink_reset(pci);
24141da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0) < 0) {
2415*ba2186e4SMarkus Elfring 		err = -EIO;
2416*ba2186e4SMarkus Elfring 		goto free_chip;
24171da177e4SLinus Torvalds 	}
24181da177e4SLinus Torvalds 
2419102fa906SClemens Ladisch 	err = snd_ymfpci_request_firmware(chip);
2420102fa906SClemens Ladisch 	if (err < 0) {
24216436bcf6STakashi Iwai 		dev_err(chip->card->dev, "firmware request failed: %d\n", err);
2422*ba2186e4SMarkus Elfring 		goto free_chip;
2423102fa906SClemens Ladisch 	}
24241da177e4SLinus Torvalds 	snd_ymfpci_download_image(chip);
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds 	udelay(100); /* seems we need a delay after downloading image.. */
24271da177e4SLinus Torvalds 
24281da177e4SLinus Torvalds 	if (snd_ymfpci_memalloc(chip) < 0) {
2429*ba2186e4SMarkus Elfring 		err = -EIO;
2430*ba2186e4SMarkus Elfring 		goto free_chip;
24311da177e4SLinus Torvalds 	}
24321da177e4SLinus Torvalds 
2433*ba2186e4SMarkus Elfring 	err = snd_ymfpci_ac3_init(chip);
2434*ba2186e4SMarkus Elfring 	if (err < 0)
2435*ba2186e4SMarkus Elfring 		goto free_chip;
24361da177e4SLinus Torvalds 
2437c7561cd8STakashi Iwai #ifdef CONFIG_PM_SLEEP
24387009fa56STakashi Iwai 	chip->saved_regs = kmalloc(YDSXGR_NUM_SAVED_REGS * sizeof(u32),
24397009fa56STakashi Iwai 				   GFP_KERNEL);
24401da177e4SLinus Torvalds 	if (chip->saved_regs == NULL) {
2441*ba2186e4SMarkus Elfring 		err = -ENOMEM;
2442*ba2186e4SMarkus Elfring 		goto free_chip;
24431da177e4SLinus Torvalds 	}
24441da177e4SLinus Torvalds #endif
24451da177e4SLinus Torvalds 
2446*ba2186e4SMarkus Elfring 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
2447*ba2186e4SMarkus Elfring 	if (err < 0)
2448*ba2186e4SMarkus Elfring 		goto free_chip;
24491da177e4SLinus Torvalds 
24501da177e4SLinus Torvalds 	snd_ymfpci_proc_init(card, chip);
24511da177e4SLinus Torvalds 
24521da177e4SLinus Torvalds 	*rchip = chip;
24531da177e4SLinus Torvalds 	return 0;
2454*ba2186e4SMarkus Elfring 
2455*ba2186e4SMarkus Elfring free_chip:
2456*ba2186e4SMarkus Elfring 	snd_ymfpci_free(chip);
2457*ba2186e4SMarkus Elfring 	return err;
24581da177e4SLinus Torvalds }
2459