xref: /openbmc/linux/sound/pci/ymfpci/ymfpci_main.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
3c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
41da177e4SLinus Torvalds  *  Routines for control of YMF724/740/744/754 chips
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/delay.h>
8102fa906SClemens Ladisch #include <linux/firmware.h>
91da177e4SLinus Torvalds #include <linux/init.h>
101da177e4SLinus Torvalds #include <linux/interrupt.h>
111da177e4SLinus Torvalds #include <linux/pci.h>
121da177e4SLinus Torvalds #include <linux/sched.h>
131da177e4SLinus Torvalds #include <linux/slab.h>
14b82a82d0SDavid Woodhouse #include <linux/mutex.h>
15da155d5bSPaul Gortmaker #include <linux/module.h>
166cbbfe1cSTakashi Iwai #include <linux/io.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <sound/core.h>
191da177e4SLinus Torvalds #include <sound/control.h>
201da177e4SLinus Torvalds #include <sound/info.h>
2133925186STakashi Iwai #include <sound/tlv.h>
2281fcb170STakashi Iwai #include "ymfpci.h"
231da177e4SLinus Torvalds #include <sound/asoundef.h>
241da177e4SLinus Torvalds #include <sound/mpu401.h>
251da177e4SLinus Torvalds 
26102fa906SClemens Ladisch #include <asm/byteorder.h>
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /*
291da177e4SLinus Torvalds  *  common I/O routines
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
32208a1b4cSTakashi Iwai static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip);
331da177e4SLinus Torvalds 
snd_ymfpci_writeb(struct snd_ymfpci * chip,u32 offset,u8 val)34208a1b4cSTakashi Iwai static inline void snd_ymfpci_writeb(struct snd_ymfpci *chip, u32 offset, u8 val)
351da177e4SLinus Torvalds {
361da177e4SLinus Torvalds 	writeb(val, chip->reg_area_virt + offset);
371da177e4SLinus Torvalds }
381da177e4SLinus Torvalds 
snd_ymfpci_readw(struct snd_ymfpci * chip,u32 offset)39208a1b4cSTakashi Iwai static inline u16 snd_ymfpci_readw(struct snd_ymfpci *chip, u32 offset)
401da177e4SLinus Torvalds {
411da177e4SLinus Torvalds 	return readw(chip->reg_area_virt + offset);
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds 
snd_ymfpci_writew(struct snd_ymfpci * chip,u32 offset,u16 val)44208a1b4cSTakashi Iwai static inline void snd_ymfpci_writew(struct snd_ymfpci *chip, u32 offset, u16 val)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	writew(val, chip->reg_area_virt + offset);
471da177e4SLinus Torvalds }
481da177e4SLinus Torvalds 
snd_ymfpci_readl(struct snd_ymfpci * chip,u32 offset)49208a1b4cSTakashi Iwai static inline u32 snd_ymfpci_readl(struct snd_ymfpci *chip, u32 offset)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	return readl(chip->reg_area_virt + offset);
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds 
snd_ymfpci_writel(struct snd_ymfpci * chip,u32 offset,u32 val)54208a1b4cSTakashi Iwai static inline void snd_ymfpci_writel(struct snd_ymfpci *chip, u32 offset, u32 val)
551da177e4SLinus Torvalds {
561da177e4SLinus Torvalds 	writel(val, chip->reg_area_virt + offset);
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
snd_ymfpci_codec_ready(struct snd_ymfpci * chip,int secondary)59208a1b4cSTakashi Iwai static int snd_ymfpci_codec_ready(struct snd_ymfpci *chip, int secondary)
601da177e4SLinus Torvalds {
61ef21ca24SNishanth Aravamudan 	unsigned long end_time;
621da177e4SLinus Torvalds 	u32 reg = secondary ? YDSXGR_SECSTATUSADR : YDSXGR_PRISTATUSADR;
631da177e4SLinus Torvalds 
64ef21ca24SNishanth Aravamudan 	end_time = jiffies + msecs_to_jiffies(750);
651da177e4SLinus Torvalds 	do {
661da177e4SLinus Torvalds 		if ((snd_ymfpci_readw(chip, reg) & 0x8000) == 0)
671da177e4SLinus Torvalds 			return 0;
688433a509SNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
69ef21ca24SNishanth Aravamudan 	} while (time_before(jiffies, end_time));
706436bcf6STakashi Iwai 	dev_err(chip->card->dev,
716436bcf6STakashi Iwai 		"codec_ready: codec %i is not ready [0x%x]\n",
726436bcf6STakashi Iwai 		secondary, snd_ymfpci_readw(chip, reg));
731da177e4SLinus Torvalds 	return -EBUSY;
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
snd_ymfpci_codec_write(struct snd_ac97 * ac97,u16 reg,u16 val)76208a1b4cSTakashi Iwai static void snd_ymfpci_codec_write(struct snd_ac97 *ac97, u16 reg, u16 val)
771da177e4SLinus Torvalds {
78208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
791da177e4SLinus Torvalds 	u32 cmd;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	snd_ymfpci_codec_ready(chip, 0);
821da177e4SLinus Torvalds 	cmd = ((YDSXG_AC97WRITECMD | reg) << 16) | val;
831da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_AC97CMDDATA, cmd);
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
snd_ymfpci_codec_read(struct snd_ac97 * ac97,u16 reg)86208a1b4cSTakashi Iwai static u16 snd_ymfpci_codec_read(struct snd_ac97 *ac97, u16 reg)
871da177e4SLinus Torvalds {
88208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0))
911da177e4SLinus Torvalds 		return ~0;
921da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_AC97CMDADR, YDSXG_AC97READCMD | reg);
931da177e4SLinus Torvalds 	if (snd_ymfpci_codec_ready(chip, 0))
941da177e4SLinus Torvalds 		return ~0;
951da177e4SLinus Torvalds 	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_744 && chip->rev < 2) {
961da177e4SLinus Torvalds 		int i;
971da177e4SLinus Torvalds 		for (i = 0; i < 600; i++)
981da177e4SLinus Torvalds 			snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
991da177e4SLinus Torvalds 	}
1001da177e4SLinus Torvalds 	return snd_ymfpci_readw(chip, YDSXGR_PRISTATUSDATA);
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds /*
1041da177e4SLinus Torvalds  *  Misc routines
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds 
snd_ymfpci_calc_delta(u32 rate)1071da177e4SLinus Torvalds static u32 snd_ymfpci_calc_delta(u32 rate)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	switch (rate) {
1101da177e4SLinus Torvalds 	case 8000:	return 0x02aaab00;
1111da177e4SLinus Torvalds 	case 11025:	return 0x03accd00;
1121da177e4SLinus Torvalds 	case 16000:	return 0x05555500;
1131da177e4SLinus Torvalds 	case 22050:	return 0x07599a00;
1141da177e4SLinus Torvalds 	case 32000:	return 0x0aaaab00;
1151da177e4SLinus Torvalds 	case 44100:	return 0x0eb33300;
1161da177e4SLinus Torvalds 	default:	return ((rate << 16) / 375) << 5;
1171da177e4SLinus Torvalds 	}
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
12010aab1a2STakashi Iwai static const u32 def_rate[8] = {
1211da177e4SLinus Torvalds 	100, 2000, 8000, 11025, 16000, 22050, 32000, 48000
1221da177e4SLinus Torvalds };
1231da177e4SLinus Torvalds 
snd_ymfpci_calc_lpfK(u32 rate)1241da177e4SLinus Torvalds static u32 snd_ymfpci_calc_lpfK(u32 rate)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	u32 i;
12710aab1a2STakashi Iwai 	static const u32 val[8] = {
1281da177e4SLinus Torvalds 		0x00570000, 0x06AA0000, 0x18B20000, 0x20930000,
1291da177e4SLinus Torvalds 		0x2B9A0000, 0x35A10000, 0x3EAA0000, 0x40000000
1301da177e4SLinus Torvalds 	};
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	if (rate == 44100)
1331da177e4SLinus Torvalds 		return 0x40000000;	/* FIXME: What's the right value? */
1341da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
1351da177e4SLinus Torvalds 		if (rate <= def_rate[i])
1361da177e4SLinus Torvalds 			return val[i];
1371da177e4SLinus Torvalds 	return val[0];
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
snd_ymfpci_calc_lpfQ(u32 rate)1401da177e4SLinus Torvalds static u32 snd_ymfpci_calc_lpfQ(u32 rate)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	u32 i;
14310aab1a2STakashi Iwai 	static const u32 val[8] = {
1441da177e4SLinus Torvalds 		0x35280000, 0x34A70000, 0x32020000, 0x31770000,
1451da177e4SLinus Torvalds 		0x31390000, 0x31C90000, 0x33D00000, 0x40000000
1461da177e4SLinus Torvalds 	};
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	if (rate == 44100)
1491da177e4SLinus Torvalds 		return 0x370A0000;
1501da177e4SLinus Torvalds 	for (i = 0; i < 8; i++)
1511da177e4SLinus Torvalds 		if (rate <= def_rate[i])
1521da177e4SLinus Torvalds 			return val[i];
1531da177e4SLinus Torvalds 	return val[0];
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds /*
1571da177e4SLinus Torvalds  *  Hardware start management
1581da177e4SLinus Torvalds  */
1591da177e4SLinus Torvalds 
snd_ymfpci_hw_start(struct snd_ymfpci * chip)160208a1b4cSTakashi Iwai static void snd_ymfpci_hw_start(struct snd_ymfpci *chip)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	unsigned long flags;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
1651da177e4SLinus Torvalds 	if (chip->start_count++ > 0)
1661da177e4SLinus Torvalds 		goto __end;
1671da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE,
1681da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MODE) | 3);
1691da177e4SLinus Torvalds 	chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
1701da177e4SLinus Torvalds       __end:
1711da177e4SLinus Torvalds       	spin_unlock_irqrestore(&chip->reg_lock, flags);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
snd_ymfpci_hw_stop(struct snd_ymfpci * chip)174208a1b4cSTakashi Iwai static void snd_ymfpci_hw_stop(struct snd_ymfpci *chip)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	unsigned long flags;
1771da177e4SLinus Torvalds 	long timeout = 1000;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
1801da177e4SLinus Torvalds 	if (--chip->start_count > 0)
1811da177e4SLinus Torvalds 		goto __end;
1821da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE,
1831da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~3);
1841da177e4SLinus Torvalds 	while (timeout-- > 0) {
1851da177e4SLinus Torvalds 		if ((snd_ymfpci_readl(chip, YDSXGR_STATUS) & 2) == 0)
1861da177e4SLinus Torvalds 			break;
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 	if (atomic_read(&chip->interrupt_sleep_count)) {
1891da177e4SLinus Torvalds 		atomic_set(&chip->interrupt_sleep_count, 0);
1901da177e4SLinus Torvalds 		wake_up(&chip->interrupt_sleep);
1911da177e4SLinus Torvalds 	}
1921da177e4SLinus Torvalds       __end:
1931da177e4SLinus Torvalds       	spin_unlock_irqrestore(&chip->reg_lock, flags);
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds /*
1971da177e4SLinus Torvalds  *  Playback voice management
1981da177e4SLinus Torvalds  */
1991da177e4SLinus Torvalds 
voice_alloc(struct snd_ymfpci * chip,enum snd_ymfpci_voice_type type,int pair,struct snd_ymfpci_voice ** rvoice)200208a1b4cSTakashi Iwai static int voice_alloc(struct snd_ymfpci *chip,
201208a1b4cSTakashi Iwai 		       enum snd_ymfpci_voice_type type, int pair,
202208a1b4cSTakashi Iwai 		       struct snd_ymfpci_voice **rvoice)
2031da177e4SLinus Torvalds {
204208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice, *voice2;
2051da177e4SLinus Torvalds 	int idx;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	*rvoice = NULL;
2081da177e4SLinus Torvalds 	for (idx = 0; idx < YDSXG_PLAYBACK_VOICES; idx += pair ? 2 : 1) {
2091da177e4SLinus Torvalds 		voice = &chip->voices[idx];
2101da177e4SLinus Torvalds 		voice2 = pair ? &chip->voices[idx+1] : NULL;
2111da177e4SLinus Torvalds 		if (voice->use || (voice2 && voice2->use))
2121da177e4SLinus Torvalds 			continue;
2131da177e4SLinus Torvalds 		voice->use = 1;
2141da177e4SLinus Torvalds 		if (voice2)
2151da177e4SLinus Torvalds 			voice2->use = 1;
2161da177e4SLinus Torvalds 		switch (type) {
2171da177e4SLinus Torvalds 		case YMFPCI_PCM:
2181da177e4SLinus Torvalds 			voice->pcm = 1;
2191da177e4SLinus Torvalds 			if (voice2)
2201da177e4SLinus Torvalds 				voice2->pcm = 1;
2211da177e4SLinus Torvalds 			break;
2221da177e4SLinus Torvalds 		case YMFPCI_SYNTH:
2231da177e4SLinus Torvalds 			voice->synth = 1;
2241da177e4SLinus Torvalds 			break;
2251da177e4SLinus Torvalds 		case YMFPCI_MIDI:
2261da177e4SLinus Torvalds 			voice->midi = 1;
2271da177e4SLinus Torvalds 			break;
2281da177e4SLinus Torvalds 		}
2291da177e4SLinus Torvalds 		snd_ymfpci_hw_start(chip);
2301da177e4SLinus Torvalds 		if (voice2)
2311da177e4SLinus Torvalds 			snd_ymfpci_hw_start(chip);
2321da177e4SLinus Torvalds 		*rvoice = voice;
2331da177e4SLinus Torvalds 		return 0;
2341da177e4SLinus Torvalds 	}
2351da177e4SLinus Torvalds 	return -ENOMEM;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
snd_ymfpci_voice_alloc(struct snd_ymfpci * chip,enum snd_ymfpci_voice_type type,int pair,struct snd_ymfpci_voice ** rvoice)238208a1b4cSTakashi Iwai static int snd_ymfpci_voice_alloc(struct snd_ymfpci *chip,
239208a1b4cSTakashi Iwai 				  enum snd_ymfpci_voice_type type, int pair,
240208a1b4cSTakashi Iwai 				  struct snd_ymfpci_voice **rvoice)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	unsigned long flags;
2431da177e4SLinus Torvalds 	int result;
2441da177e4SLinus Torvalds 
245da3cec35STakashi Iwai 	if (snd_BUG_ON(!rvoice))
246da3cec35STakashi Iwai 		return -EINVAL;
247da3cec35STakashi Iwai 	if (snd_BUG_ON(pair && type != YMFPCI_PCM))
248da3cec35STakashi Iwai 		return -EINVAL;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->voice_lock, flags);
2511da177e4SLinus Torvalds 	for (;;) {
2521da177e4SLinus Torvalds 		result = voice_alloc(chip, type, pair, rvoice);
2531da177e4SLinus Torvalds 		if (result == 0 || type != YMFPCI_PCM)
2541da177e4SLinus Torvalds 			break;
2551da177e4SLinus Torvalds 		/* TODO: synth/midi voice deallocation */
2561da177e4SLinus Torvalds 		break;
2571da177e4SLinus Torvalds 	}
2581da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->voice_lock, flags);
2591da177e4SLinus Torvalds 	return result;
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
snd_ymfpci_voice_free(struct snd_ymfpci * chip,struct snd_ymfpci_voice * pvoice)262208a1b4cSTakashi Iwai static int snd_ymfpci_voice_free(struct snd_ymfpci *chip, struct snd_ymfpci_voice *pvoice)
2631da177e4SLinus Torvalds {
2641da177e4SLinus Torvalds 	unsigned long flags;
2651da177e4SLinus Torvalds 
266da3cec35STakashi Iwai 	if (snd_BUG_ON(!pvoice))
267da3cec35STakashi Iwai 		return -EINVAL;
2681da177e4SLinus Torvalds 	snd_ymfpci_hw_stop(chip);
2691da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->voice_lock, flags);
2709ed1261eSTeru KAMOGASHIRA 	if (pvoice->number == chip->src441_used) {
2719ed1261eSTeru KAMOGASHIRA 		chip->src441_used = -1;
2729ed1261eSTeru KAMOGASHIRA 		pvoice->ypcm->use_441_slot = 0;
2739ed1261eSTeru KAMOGASHIRA 	}
2741da177e4SLinus Torvalds 	pvoice->use = pvoice->pcm = pvoice->synth = pvoice->midi = 0;
2751da177e4SLinus Torvalds 	pvoice->ypcm = NULL;
2761da177e4SLinus Torvalds 	pvoice->interrupt = NULL;
2771da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->voice_lock, flags);
2781da177e4SLinus Torvalds 	return 0;
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds /*
2821da177e4SLinus Torvalds  *  PCM part
2831da177e4SLinus Torvalds  */
2841da177e4SLinus Torvalds 
snd_ymfpci_pcm_interrupt(struct snd_ymfpci * chip,struct snd_ymfpci_voice * voice)285208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_interrupt(struct snd_ymfpci *chip, struct snd_ymfpci_voice *voice)
2861da177e4SLinus Torvalds {
287208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
2881da177e4SLinus Torvalds 	u32 pos, delta;
2891da177e4SLinus Torvalds 
290e7daaeedSTakashi Iwai 	ypcm = voice->ypcm;
291e7daaeedSTakashi Iwai 	if (!ypcm)
2921da177e4SLinus Torvalds 		return;
2931da177e4SLinus Torvalds 	if (ypcm->substream == NULL)
2941da177e4SLinus Torvalds 		return;
2951da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
2961da177e4SLinus Torvalds 	if (ypcm->running) {
2971da177e4SLinus Torvalds 		pos = le32_to_cpu(voice->bank[chip->active_bank].start);
2981da177e4SLinus Torvalds 		if (pos < ypcm->last_pos)
2991da177e4SLinus Torvalds 			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
3001da177e4SLinus Torvalds 		else
3011da177e4SLinus Torvalds 			delta = pos - ypcm->last_pos;
3021da177e4SLinus Torvalds 		ypcm->period_pos += delta;
3031da177e4SLinus Torvalds 		ypcm->last_pos = pos;
3041da177e4SLinus Torvalds 		if (ypcm->period_pos >= ypcm->period_size) {
305ee419653STakashi Iwai 			/*
3066436bcf6STakashi Iwai 			dev_dbg(chip->card->dev,
307ee419653STakashi Iwai 			       "done - active_bank = 0x%x, start = 0x%x\n",
308ee419653STakashi Iwai 			       chip->active_bank,
309ee419653STakashi Iwai 			       voice->bank[chip->active_bank].start);
310ee419653STakashi Iwai 			*/
3111da177e4SLinus Torvalds 			ypcm->period_pos %= ypcm->period_size;
3121da177e4SLinus Torvalds 			spin_unlock(&chip->reg_lock);
3131da177e4SLinus Torvalds 			snd_pcm_period_elapsed(ypcm->substream);
3141da177e4SLinus Torvalds 			spin_lock(&chip->reg_lock);
3151da177e4SLinus Torvalds 		}
3169bcf6551SClemens Ladisch 
3179bcf6551SClemens Ladisch 		if (unlikely(ypcm->update_pcm_vol)) {
3189bcf6551SClemens Ladisch 			unsigned int subs = ypcm->substream->number;
3199bcf6551SClemens Ladisch 			unsigned int next_bank = 1 - chip->active_bank;
320208a1b4cSTakashi Iwai 			struct snd_ymfpci_playback_bank *bank;
321d3c63763STakashi Iwai 			__le32 volume;
3229bcf6551SClemens Ladisch 
3239bcf6551SClemens Ladisch 			bank = &voice->bank[next_bank];
3249bcf6551SClemens Ladisch 			volume = cpu_to_le32(chip->pcm_mixer[subs].left << 15);
3259bcf6551SClemens Ladisch 			bank->left_gain_end = volume;
3269bcf6551SClemens Ladisch 			if (ypcm->output_rear)
3279bcf6551SClemens Ladisch 				bank->eff2_gain_end = volume;
3289bcf6551SClemens Ladisch 			if (ypcm->voices[1])
3299bcf6551SClemens Ladisch 				bank = &ypcm->voices[1]->bank[next_bank];
3309bcf6551SClemens Ladisch 			volume = cpu_to_le32(chip->pcm_mixer[subs].right << 15);
3319bcf6551SClemens Ladisch 			bank->right_gain_end = volume;
3329bcf6551SClemens Ladisch 			if (ypcm->output_rear)
3339bcf6551SClemens Ladisch 				bank->eff3_gain_end = volume;
3349bcf6551SClemens Ladisch 			ypcm->update_pcm_vol--;
3359bcf6551SClemens Ladisch 		}
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream * substream)340208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_capture_interrupt(struct snd_pcm_substream *substream)
3411da177e4SLinus Torvalds {
342208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
343208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
344208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ypcm->chip;
3451da177e4SLinus Torvalds 	u32 pos, delta;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3481da177e4SLinus Torvalds 	if (ypcm->running) {
3491da177e4SLinus Torvalds 		pos = le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
3501da177e4SLinus Torvalds 		if (pos < ypcm->last_pos)
3511da177e4SLinus Torvalds 			delta = pos + (ypcm->buffer_size - ypcm->last_pos);
3521da177e4SLinus Torvalds 		else
3531da177e4SLinus Torvalds 			delta = pos - ypcm->last_pos;
3541da177e4SLinus Torvalds 		ypcm->period_pos += delta;
3551da177e4SLinus Torvalds 		ypcm->last_pos = pos;
3561da177e4SLinus Torvalds 		if (ypcm->period_pos >= ypcm->period_size) {
3571da177e4SLinus Torvalds 			ypcm->period_pos %= ypcm->period_size;
358ee419653STakashi Iwai 			/*
3596436bcf6STakashi Iwai 			dev_dbg(chip->card->dev,
360ee419653STakashi Iwai 			       "done - active_bank = 0x%x, start = 0x%x\n",
361ee419653STakashi Iwai 			       chip->active_bank,
362ee419653STakashi Iwai 			       voice->bank[chip->active_bank].start);
363ee419653STakashi Iwai 			*/
3641da177e4SLinus Torvalds 			spin_unlock(&chip->reg_lock);
3651da177e4SLinus Torvalds 			snd_pcm_period_elapsed(substream);
3661da177e4SLinus Torvalds 			spin_lock(&chip->reg_lock);
3671da177e4SLinus Torvalds 		}
3681da177e4SLinus Torvalds 	}
3691da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
snd_ymfpci_playback_trigger(struct snd_pcm_substream * substream,int cmd)372208a1b4cSTakashi Iwai static int snd_ymfpci_playback_trigger(struct snd_pcm_substream *substream,
3731da177e4SLinus Torvalds 				       int cmd)
3741da177e4SLinus Torvalds {
375208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
376208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
377177a7cdbSClemens Ladisch 	struct snd_kcontrol *kctl = NULL;
3781da177e4SLinus Torvalds 	int result = 0;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
3811da177e4SLinus Torvalds 	if (ypcm->voices[0] == NULL) {
3821da177e4SLinus Torvalds 		result = -EINVAL;
3831da177e4SLinus Torvalds 		goto __unlock;
3841da177e4SLinus Torvalds 	}
3851da177e4SLinus Torvalds 	switch (cmd) {
3861da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
3871da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
3881da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_RESUME:
3891da177e4SLinus Torvalds 		chip->ctrl_playback[ypcm->voices[0]->number + 1] = cpu_to_le32(ypcm->voices[0]->bank_addr);
3909ed1261eSTeru KAMOGASHIRA 		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
3911da177e4SLinus Torvalds 			chip->ctrl_playback[ypcm->voices[1]->number + 1] = cpu_to_le32(ypcm->voices[1]->bank_addr);
3921da177e4SLinus Torvalds 		ypcm->running = 1;
3931da177e4SLinus Torvalds 		break;
3941da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
395177a7cdbSClemens Ladisch 		if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
396177a7cdbSClemens Ladisch 			kctl = chip->pcm_mixer[substream->number].ctl;
397177a7cdbSClemens Ladisch 			kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
398177a7cdbSClemens Ladisch 		}
399c0dbbdadSGustavo A. R. Silva 		fallthrough;
4001da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
4011da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_SUSPEND:
4021da177e4SLinus Torvalds 		chip->ctrl_playback[ypcm->voices[0]->number + 1] = 0;
4039ed1261eSTeru KAMOGASHIRA 		if (ypcm->voices[1] != NULL && !ypcm->use_441_slot)
4041da177e4SLinus Torvalds 			chip->ctrl_playback[ypcm->voices[1]->number + 1] = 0;
4051da177e4SLinus Torvalds 		ypcm->running = 0;
4061da177e4SLinus Torvalds 		break;
4071da177e4SLinus Torvalds 	default:
4081da177e4SLinus Torvalds 		result = -EINVAL;
4091da177e4SLinus Torvalds 		break;
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds       __unlock:
4121da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
413177a7cdbSClemens Ladisch 	if (kctl)
414177a7cdbSClemens Ladisch 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
4151da177e4SLinus Torvalds 	return result;
4161da177e4SLinus Torvalds }
snd_ymfpci_capture_trigger(struct snd_pcm_substream * substream,int cmd)417208a1b4cSTakashi Iwai static int snd_ymfpci_capture_trigger(struct snd_pcm_substream *substream,
4181da177e4SLinus Torvalds 				      int cmd)
4191da177e4SLinus Torvalds {
420208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
421208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
4221da177e4SLinus Torvalds 	int result = 0;
4231da177e4SLinus Torvalds 	u32 tmp;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	spin_lock(&chip->reg_lock);
4261da177e4SLinus Torvalds 	switch (cmd) {
4271da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_START:
4281da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
4291da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_RESUME:
4301da177e4SLinus Torvalds 		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) | (1 << ypcm->capture_bank_number);
4311da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
4321da177e4SLinus Torvalds 		ypcm->running = 1;
4331da177e4SLinus Torvalds 		break;
4341da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_STOP:
4351da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
4361da177e4SLinus Torvalds 	case SNDRV_PCM_TRIGGER_SUSPEND:
4371da177e4SLinus Torvalds 		tmp = snd_ymfpci_readl(chip, YDSXGR_MAPOFREC) & ~(1 << ypcm->capture_bank_number);
4381da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, tmp);
4391da177e4SLinus Torvalds 		ypcm->running = 0;
4401da177e4SLinus Torvalds 		break;
4411da177e4SLinus Torvalds 	default:
4421da177e4SLinus Torvalds 		result = -EINVAL;
4431da177e4SLinus Torvalds 		break;
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds 	spin_unlock(&chip->reg_lock);
4461da177e4SLinus Torvalds 	return result;
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm * ypcm,int voices)449208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_voice_alloc(struct snd_ymfpci_pcm *ypcm, int voices)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	int err;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	if (ypcm->voices[1] != NULL && voices < 2) {
4541da177e4SLinus Torvalds 		snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[1]);
4551da177e4SLinus Torvalds 		ypcm->voices[1] = NULL;
4561da177e4SLinus Torvalds 	}
4571da177e4SLinus Torvalds 	if (voices == 1 && ypcm->voices[0] != NULL)
4581da177e4SLinus Torvalds 		return 0;		/* already allocated */
4591da177e4SLinus Torvalds 	if (voices == 2 && ypcm->voices[0] != NULL && ypcm->voices[1] != NULL)
4601da177e4SLinus Torvalds 		return 0;		/* already allocated */
4611da177e4SLinus Torvalds 	if (voices > 1) {
4621da177e4SLinus Torvalds 		if (ypcm->voices[0] != NULL && ypcm->voices[1] == NULL) {
4631da177e4SLinus Torvalds 			snd_ymfpci_voice_free(ypcm->chip, ypcm->voices[0]);
4641da177e4SLinus Torvalds 			ypcm->voices[0] = NULL;
4651da177e4SLinus Torvalds 		}
4661da177e4SLinus Torvalds 	}
4671da177e4SLinus Torvalds 	err = snd_ymfpci_voice_alloc(ypcm->chip, YMFPCI_PCM, voices > 1, &ypcm->voices[0]);
4681da177e4SLinus Torvalds 	if (err < 0)
4691da177e4SLinus Torvalds 		return err;
4701da177e4SLinus Torvalds 	ypcm->voices[0]->ypcm = ypcm;
4711da177e4SLinus Torvalds 	ypcm->voices[0]->interrupt = snd_ymfpci_pcm_interrupt;
4721da177e4SLinus Torvalds 	if (voices > 1) {
4731da177e4SLinus Torvalds 		ypcm->voices[1] = &ypcm->chip->voices[ypcm->voices[0]->number + 1];
4741da177e4SLinus Torvalds 		ypcm->voices[1]->ypcm = ypcm;
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 	return 0;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm * ypcm,unsigned int voiceidx,struct snd_pcm_runtime * runtime,int has_pcm_volume)479208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_init_voice(struct snd_ymfpci_pcm *ypcm, unsigned int voiceidx,
480208a1b4cSTakashi Iwai 				      struct snd_pcm_runtime *runtime,
4819bcf6551SClemens Ladisch 				      int has_pcm_volume)
4821da177e4SLinus Torvalds {
483208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice = ypcm->voices[voiceidx];
4841da177e4SLinus Torvalds 	u32 format;
4859bcf6551SClemens Ladisch 	u32 delta = snd_ymfpci_calc_delta(runtime->rate);
4869bcf6551SClemens Ladisch 	u32 lpfQ = snd_ymfpci_calc_lpfQ(runtime->rate);
4879bcf6551SClemens Ladisch 	u32 lpfK = snd_ymfpci_calc_lpfK(runtime->rate);
488208a1b4cSTakashi Iwai 	struct snd_ymfpci_playback_bank *bank;
4891da177e4SLinus Torvalds 	unsigned int nbank;
490d3c63763STakashi Iwai 	__le32 vol_left, vol_right;
4919bcf6551SClemens Ladisch 	u8 use_left, use_right;
4929ed1261eSTeru KAMOGASHIRA 	unsigned long flags;
4931da177e4SLinus Torvalds 
494da3cec35STakashi Iwai 	if (snd_BUG_ON(!voice))
495da3cec35STakashi Iwai 		return;
4969bcf6551SClemens Ladisch 	if (runtime->channels == 1) {
4979bcf6551SClemens Ladisch 		use_left = 1;
4989bcf6551SClemens Ladisch 		use_right = 1;
4999bcf6551SClemens Ladisch 	} else {
5009bcf6551SClemens Ladisch 		use_left = (voiceidx & 1) == 0;
5019bcf6551SClemens Ladisch 		use_right = !use_left;
5029bcf6551SClemens Ladisch 	}
5039bcf6551SClemens Ladisch 	if (has_pcm_volume) {
5049bcf6551SClemens Ladisch 		vol_left = cpu_to_le32(ypcm->chip->pcm_mixer
5059bcf6551SClemens Ladisch 				       [ypcm->substream->number].left << 15);
5069bcf6551SClemens Ladisch 		vol_right = cpu_to_le32(ypcm->chip->pcm_mixer
5079bcf6551SClemens Ladisch 					[ypcm->substream->number].right << 15);
5089bcf6551SClemens Ladisch 	} else {
5099bcf6551SClemens Ladisch 		vol_left = cpu_to_le32(0x40000000);
5109bcf6551SClemens Ladisch 		vol_right = cpu_to_le32(0x40000000);
5119bcf6551SClemens Ladisch 	}
5129ed1261eSTeru KAMOGASHIRA 	spin_lock_irqsave(&ypcm->chip->voice_lock, flags);
5139bcf6551SClemens Ladisch 	format = runtime->channels == 2 ? 0x00010000 : 0;
5149bcf6551SClemens Ladisch 	if (snd_pcm_format_width(runtime->format) == 8)
5159bcf6551SClemens Ladisch 		format |= 0x80000000;
5169ed1261eSTeru KAMOGASHIRA 	else if (ypcm->chip->device_id == PCI_DEVICE_ID_YAMAHA_754 &&
5179ed1261eSTeru KAMOGASHIRA 		 runtime->rate == 44100 && runtime->channels == 2 &&
5189ed1261eSTeru KAMOGASHIRA 		 voiceidx == 0 && (ypcm->chip->src441_used == -1 ||
5199ed1261eSTeru KAMOGASHIRA 				   ypcm->chip->src441_used == voice->number)) {
5209ed1261eSTeru KAMOGASHIRA 		ypcm->chip->src441_used = voice->number;
5219ed1261eSTeru KAMOGASHIRA 		ypcm->use_441_slot = 1;
5229ed1261eSTeru KAMOGASHIRA 		format |= 0x10000000;
5239ed1261eSTeru KAMOGASHIRA 	}
5249ed1261eSTeru KAMOGASHIRA 	if (ypcm->chip->src441_used == voice->number &&
5259ed1261eSTeru KAMOGASHIRA 	    (format & 0x10000000) == 0) {
5269ed1261eSTeru KAMOGASHIRA 		ypcm->chip->src441_used = -1;
5279ed1261eSTeru KAMOGASHIRA 		ypcm->use_441_slot = 0;
5289ed1261eSTeru KAMOGASHIRA 	}
5299bcf6551SClemens Ladisch 	if (runtime->channels == 2 && (voiceidx & 1) != 0)
5309bcf6551SClemens Ladisch 		format |= 1;
5319ed1261eSTeru KAMOGASHIRA 	spin_unlock_irqrestore(&ypcm->chip->voice_lock, flags);
5321da177e4SLinus Torvalds 	for (nbank = 0; nbank < 2; nbank++) {
5331da177e4SLinus Torvalds 		bank = &voice->bank[nbank];
5349bcf6551SClemens Ladisch 		memset(bank, 0, sizeof(*bank));
5351da177e4SLinus Torvalds 		bank->format = cpu_to_le32(format);
5369bcf6551SClemens Ladisch 		bank->base = cpu_to_le32(runtime->dma_addr);
5379bcf6551SClemens Ladisch 		bank->loop_end = cpu_to_le32(ypcm->buffer_size);
5381da177e4SLinus Torvalds 		bank->lpfQ = cpu_to_le32(lpfQ);
5391da177e4SLinus Torvalds 		bank->delta =
5401da177e4SLinus Torvalds 		bank->delta_end = cpu_to_le32(delta);
5411da177e4SLinus Torvalds 		bank->lpfK =
5421da177e4SLinus Torvalds 		bank->lpfK_end = cpu_to_le32(lpfK);
5439bcf6551SClemens Ladisch 		bank->eg_gain =
5449bcf6551SClemens Ladisch 		bank->eg_gain_end = cpu_to_le32(0x40000000);
5451da177e4SLinus Torvalds 
5469bcf6551SClemens Ladisch 		if (ypcm->output_front) {
5479bcf6551SClemens Ladisch 			if (use_left) {
5481da177e4SLinus Torvalds 				bank->left_gain =
5499bcf6551SClemens Ladisch 				bank->left_gain_end = vol_left;
5509bcf6551SClemens Ladisch 			}
5519bcf6551SClemens Ladisch 			if (use_right) {
5521da177e4SLinus Torvalds 				bank->right_gain =
5539bcf6551SClemens Ladisch 				bank->right_gain_end = vol_right;
5549bcf6551SClemens Ladisch 			}
5559bcf6551SClemens Ladisch 		}
5569bcf6551SClemens Ladisch 		if (ypcm->output_rear) {
5575a25c5cfSJaroslav Kysela 		        if (!ypcm->swap_rear) {
5585a25c5cfSJaroslav Kysela         			if (use_left) {
5595a25c5cfSJaroslav Kysela         				bank->eff2_gain =
5605a25c5cfSJaroslav Kysela         				bank->eff2_gain_end = vol_left;
5615a25c5cfSJaroslav Kysela         			}
5625a25c5cfSJaroslav Kysela         			if (use_right) {
5635a25c5cfSJaroslav Kysela         				bank->eff3_gain =
5645a25c5cfSJaroslav Kysela         				bank->eff3_gain_end = vol_right;
5655a25c5cfSJaroslav Kysela         			}
5665a25c5cfSJaroslav Kysela 		        } else {
567153abaa4SClemens Ladisch         			/* The SPDIF out channels seem to be swapped, so we have
568153abaa4SClemens Ladisch         			 * to swap them here, too.  The rear analog out channels
569153abaa4SClemens Ladisch         			 * will be wrong, but otherwise AC3 would not work.
570153abaa4SClemens Ladisch         			 */
5719bcf6551SClemens Ladisch         			if (use_left) {
572153abaa4SClemens Ladisch         				bank->eff3_gain =
573153abaa4SClemens Ladisch         				bank->eff3_gain_end = vol_left;
5749bcf6551SClemens Ladisch         			}
5759bcf6551SClemens Ladisch         			if (use_right) {
576153abaa4SClemens Ladisch         				bank->eff2_gain =
577153abaa4SClemens Ladisch         				bank->eff2_gain_end = vol_right;
5781da177e4SLinus Torvalds         			}
5791da177e4SLinus Torvalds         		}
5801da177e4SLinus Torvalds                 }
5811da177e4SLinus Torvalds 	}
5825a25c5cfSJaroslav Kysela }
5831da177e4SLinus Torvalds 
snd_ymfpci_ac3_init(struct snd_ymfpci * chip)584e23e7a14SBill Pemberton static int snd_ymfpci_ac3_init(struct snd_ymfpci *chip)
5851da177e4SLinus Torvalds {
5866974f8adSTakashi Iwai 	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
5871da177e4SLinus Torvalds 				4096, &chip->ac3_tmp_base) < 0)
5881da177e4SLinus Torvalds 		return -ENOMEM;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	chip->bank_effect[3][0]->base =
5911da177e4SLinus Torvalds 	chip->bank_effect[3][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr);
5921da177e4SLinus Torvalds 	chip->bank_effect[3][0]->loop_end =
5931da177e4SLinus Torvalds 	chip->bank_effect[3][1]->loop_end = cpu_to_le32(1024);
5941da177e4SLinus Torvalds 	chip->bank_effect[4][0]->base =
5951da177e4SLinus Torvalds 	chip->bank_effect[4][1]->base = cpu_to_le32(chip->ac3_tmp_base.addr + 2048);
5961da177e4SLinus Torvalds 	chip->bank_effect[4][0]->loop_end =
5971da177e4SLinus Torvalds 	chip->bank_effect[4][1]->loop_end = cpu_to_le32(1024);
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
6001da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
6011da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) | 3 << 3);
6021da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
6031da177e4SLinus Torvalds 	return 0;
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
snd_ymfpci_ac3_done(struct snd_ymfpci * chip)606208a1b4cSTakashi Iwai static int snd_ymfpci_ac3_done(struct snd_ymfpci *chip)
6071da177e4SLinus Torvalds {
6081da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
6091da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT,
6101da177e4SLinus Torvalds 			  snd_ymfpci_readl(chip, YDSXGR_MAPOFEFFECT) & ~(3 << 3));
6111da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
6121da177e4SLinus Torvalds 	// snd_ymfpci_irq_wait(chip);
6131da177e4SLinus Torvalds 	if (chip->ac3_tmp_base.area) {
6141da177e4SLinus Torvalds 		snd_dma_free_pages(&chip->ac3_tmp_base);
6151da177e4SLinus Torvalds 		chip->ac3_tmp_base.area = NULL;
6161da177e4SLinus Torvalds 	}
6171da177e4SLinus Torvalds 	return 0;
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
snd_ymfpci_playback_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)620208a1b4cSTakashi Iwai static int snd_ymfpci_playback_hw_params(struct snd_pcm_substream *substream,
621208a1b4cSTakashi Iwai 					 struct snd_pcm_hw_params *hw_params)
6221da177e4SLinus Torvalds {
623208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
624208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
6251da177e4SLinus Torvalds 	int err;
6261da177e4SLinus Torvalds 
627e7daaeedSTakashi Iwai 	err = snd_ymfpci_pcm_voice_alloc(ypcm, params_channels(hw_params));
628e7daaeedSTakashi Iwai 	if (err < 0)
6291da177e4SLinus Torvalds 		return err;
6301da177e4SLinus Torvalds 	return 0;
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds 
snd_ymfpci_playback_hw_free(struct snd_pcm_substream * substream)633208a1b4cSTakashi Iwai static int snd_ymfpci_playback_hw_free(struct snd_pcm_substream *substream)
6341da177e4SLinus Torvalds {
635208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
636208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
637208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	if (runtime->private_data == NULL)
6401da177e4SLinus Torvalds 		return 0;
6411da177e4SLinus Torvalds 	ypcm = runtime->private_data;
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	/* wait, until the PCI operations are not finished */
6441da177e4SLinus Torvalds 	snd_ymfpci_irq_wait(chip);
6451da177e4SLinus Torvalds 	if (ypcm->voices[1]) {
6461da177e4SLinus Torvalds 		snd_ymfpci_voice_free(chip, ypcm->voices[1]);
6471da177e4SLinus Torvalds 		ypcm->voices[1] = NULL;
6481da177e4SLinus Torvalds 	}
6491da177e4SLinus Torvalds 	if (ypcm->voices[0]) {
6501da177e4SLinus Torvalds 		snd_ymfpci_voice_free(chip, ypcm->voices[0]);
6511da177e4SLinus Torvalds 		ypcm->voices[0] = NULL;
6521da177e4SLinus Torvalds 	}
6531da177e4SLinus Torvalds 	return 0;
6541da177e4SLinus Torvalds }
6551da177e4SLinus Torvalds 
snd_ymfpci_playback_prepare(struct snd_pcm_substream * substream)656208a1b4cSTakashi Iwai static int snd_ymfpci_playback_prepare(struct snd_pcm_substream *substream)
6571da177e4SLinus Torvalds {
658208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
659208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
660208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
661177a7cdbSClemens Ladisch 	struct snd_kcontrol *kctl;
6621da177e4SLinus Torvalds 	unsigned int nvoice;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	ypcm->period_size = runtime->period_size;
6651da177e4SLinus Torvalds 	ypcm->buffer_size = runtime->buffer_size;
6661da177e4SLinus Torvalds 	ypcm->period_pos = 0;
6671da177e4SLinus Torvalds 	ypcm->last_pos = 0;
6681da177e4SLinus Torvalds 	for (nvoice = 0; nvoice < runtime->channels; nvoice++)
6699bcf6551SClemens Ladisch 		snd_ymfpci_pcm_init_voice(ypcm, nvoice, runtime,
6709bcf6551SClemens Ladisch 					  substream->pcm == chip->pcm);
671177a7cdbSClemens Ladisch 
672177a7cdbSClemens Ladisch 	if (substream->pcm == chip->pcm && !ypcm->use_441_slot) {
673177a7cdbSClemens Ladisch 		kctl = chip->pcm_mixer[substream->number].ctl;
674177a7cdbSClemens Ladisch 		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
675177a7cdbSClemens Ladisch 		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
676177a7cdbSClemens Ladisch 	}
6771da177e4SLinus Torvalds 	return 0;
6781da177e4SLinus Torvalds }
6791da177e4SLinus Torvalds 
snd_ymfpci_capture_hw_free(struct snd_pcm_substream * substream)680208a1b4cSTakashi Iwai static int snd_ymfpci_capture_hw_free(struct snd_pcm_substream *substream)
6811da177e4SLinus Torvalds {
682208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	/* wait, until the PCI operations are not finished */
6851da177e4SLinus Torvalds 	snd_ymfpci_irq_wait(chip);
686b6ed90c0STakashi Iwai 	return 0;
6871da177e4SLinus Torvalds }
6881da177e4SLinus Torvalds 
snd_ymfpci_capture_prepare(struct snd_pcm_substream * substream)689208a1b4cSTakashi Iwai static int snd_ymfpci_capture_prepare(struct snd_pcm_substream *substream)
6901da177e4SLinus Torvalds {
691208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
692208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
693208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
694208a1b4cSTakashi Iwai 	struct snd_ymfpci_capture_bank * bank;
6951da177e4SLinus Torvalds 	int nbank;
6961da177e4SLinus Torvalds 	u32 rate, format;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	ypcm->period_size = runtime->period_size;
6991da177e4SLinus Torvalds 	ypcm->buffer_size = runtime->buffer_size;
7001da177e4SLinus Torvalds 	ypcm->period_pos = 0;
7011da177e4SLinus Torvalds 	ypcm->last_pos = 0;
7021da177e4SLinus Torvalds 	ypcm->shift = 0;
7031da177e4SLinus Torvalds 	rate = ((48000 * 4096) / runtime->rate) - 1;
7041da177e4SLinus Torvalds 	format = 0;
7051da177e4SLinus Torvalds 	if (runtime->channels == 2) {
7061da177e4SLinus Torvalds 		format |= 2;
7071da177e4SLinus Torvalds 		ypcm->shift++;
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 	if (snd_pcm_format_width(runtime->format) == 8)
7101da177e4SLinus Torvalds 		format |= 1;
7111da177e4SLinus Torvalds 	else
7121da177e4SLinus Torvalds 		ypcm->shift++;
7131da177e4SLinus Torvalds 	switch (ypcm->capture_bank_number) {
7141da177e4SLinus Torvalds 	case 0:
7151da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECFORMAT, format);
7161da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_RECSLOTSR, rate);
7171da177e4SLinus Torvalds 		break;
7181da177e4SLinus Torvalds 	case 1:
7191da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_ADCFORMAT, format);
7201da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_ADCSLOTSR, rate);
7211da177e4SLinus Torvalds 		break;
7221da177e4SLinus Torvalds 	}
7231da177e4SLinus Torvalds 	for (nbank = 0; nbank < 2; nbank++) {
7241da177e4SLinus Torvalds 		bank = chip->bank_capture[ypcm->capture_bank_number][nbank];
7251da177e4SLinus Torvalds 		bank->base = cpu_to_le32(runtime->dma_addr);
7261da177e4SLinus Torvalds 		bank->loop_end = cpu_to_le32(ypcm->buffer_size << ypcm->shift);
7271da177e4SLinus Torvalds 		bank->start = 0;
7281da177e4SLinus Torvalds 		bank->num_of_loops = 0;
7291da177e4SLinus Torvalds 	}
7301da177e4SLinus Torvalds 	return 0;
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
snd_ymfpci_playback_pointer(struct snd_pcm_substream * substream)733208a1b4cSTakashi Iwai static snd_pcm_uframes_t snd_ymfpci_playback_pointer(struct snd_pcm_substream *substream)
7341da177e4SLinus Torvalds {
735208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
736208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
737208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
738208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice = ypcm->voices[0];
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	if (!(ypcm->running && voice))
7411da177e4SLinus Torvalds 		return 0;
7421da177e4SLinus Torvalds 	return le32_to_cpu(voice->bank[chip->active_bank].start);
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
snd_ymfpci_capture_pointer(struct snd_pcm_substream * substream)745208a1b4cSTakashi Iwai static snd_pcm_uframes_t snd_ymfpci_capture_pointer(struct snd_pcm_substream *substream)
7461da177e4SLinus Torvalds {
747208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
748208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
749208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	if (!ypcm->running)
7521da177e4SLinus Torvalds 		return 0;
7531da177e4SLinus Torvalds 	return le32_to_cpu(chip->bank_capture[ypcm->capture_bank_number][chip->active_bank]->start) >> ypcm->shift;
7541da177e4SLinus Torvalds }
7551da177e4SLinus Torvalds 
snd_ymfpci_irq_wait(struct snd_ymfpci * chip)756208a1b4cSTakashi Iwai static void snd_ymfpci_irq_wait(struct snd_ymfpci *chip)
7571da177e4SLinus Torvalds {
758ac6424b9SIngo Molnar 	wait_queue_entry_t wait;
7591da177e4SLinus Torvalds 	int loops = 4;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	while (loops-- > 0) {
7621da177e4SLinus Torvalds 		if ((snd_ymfpci_readl(chip, YDSXGR_MODE) & 3) == 0)
7631da177e4SLinus Torvalds 		 	continue;
7641da177e4SLinus Torvalds 		init_waitqueue_entry(&wait, current);
7651da177e4SLinus Torvalds 		add_wait_queue(&chip->interrupt_sleep, &wait);
7661da177e4SLinus Torvalds 		atomic_inc(&chip->interrupt_sleep_count);
7678433a509SNishanth Aravamudan 		schedule_timeout_uninterruptible(msecs_to_jiffies(50));
7681da177e4SLinus Torvalds 		remove_wait_queue(&chip->interrupt_sleep, &wait);
7691da177e4SLinus Torvalds 	}
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
snd_ymfpci_interrupt(int irq,void * dev_id)7727d12e780SDavid Howells static irqreturn_t snd_ymfpci_interrupt(int irq, void *dev_id)
7731da177e4SLinus Torvalds {
774208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = dev_id;
7751da177e4SLinus Torvalds 	u32 status, nvoice, mode;
776208a1b4cSTakashi Iwai 	struct snd_ymfpci_voice *voice;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	status = snd_ymfpci_readl(chip, YDSXGR_STATUS);
7791da177e4SLinus Torvalds 	if (status & 0x80000000) {
7801da177e4SLinus Torvalds 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT) & 1;
7811da177e4SLinus Torvalds 		spin_lock(&chip->voice_lock);
7821da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_PLAYBACK_VOICES; nvoice++) {
7831da177e4SLinus Torvalds 			voice = &chip->voices[nvoice];
7841da177e4SLinus Torvalds 			if (voice->interrupt)
7851da177e4SLinus Torvalds 				voice->interrupt(chip, voice);
7861da177e4SLinus Torvalds 		}
7871da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_CAPTURE_VOICES; nvoice++) {
7881da177e4SLinus Torvalds 			if (chip->capture_substream[nvoice])
7891da177e4SLinus Torvalds 				snd_ymfpci_pcm_capture_interrupt(chip->capture_substream[nvoice]);
7901da177e4SLinus Torvalds 		}
7911da177e4SLinus Torvalds #if 0
7921da177e4SLinus Torvalds 		for (nvoice = 0; nvoice < YDSXG_EFFECT_VOICES; nvoice++) {
7931da177e4SLinus Torvalds 			if (chip->effect_substream[nvoice])
7941da177e4SLinus Torvalds 				snd_ymfpci_pcm_effect_interrupt(chip->effect_substream[nvoice]);
7951da177e4SLinus Torvalds 		}
7961da177e4SLinus Torvalds #endif
7971da177e4SLinus Torvalds 		spin_unlock(&chip->voice_lock);
7981da177e4SLinus Torvalds 		spin_lock(&chip->reg_lock);
7991da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_STATUS, 0x80000000);
8001da177e4SLinus Torvalds 		mode = snd_ymfpci_readl(chip, YDSXGR_MODE) | 2;
8011da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MODE, mode);
8021da177e4SLinus Torvalds 		spin_unlock(&chip->reg_lock);
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 		if (atomic_read(&chip->interrupt_sleep_count)) {
8051da177e4SLinus Torvalds 			atomic_set(&chip->interrupt_sleep_count, 0);
8061da177e4SLinus Torvalds 			wake_up(&chip->interrupt_sleep);
8071da177e4SLinus Torvalds 		}
8081da177e4SLinus Torvalds 	}
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	status = snd_ymfpci_readw(chip, YDSXGR_INTFLAG);
8111da177e4SLinus Torvalds 	if (status & 1) {
8121da177e4SLinus Torvalds 		if (chip->timer)
8136e2efaacSClemens Ladisch 			snd_timer_interrupt(chip->timer, chip->timer_ticks);
8141da177e4SLinus Torvalds 	}
8151da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_INTFLAG, status);
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	if (chip->rawmidi)
8187d12e780SDavid Howells 		snd_mpu401_uart_interrupt(irq, chip->rawmidi->private_data);
8191da177e4SLinus Torvalds 	return IRQ_HANDLED;
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
822420b0c1bSBhumika Goyal static const struct snd_pcm_hardware snd_ymfpci_playback =
8231da177e4SLinus Torvalds {
8241da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
8251da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
8261da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_INTERLEAVED |
8271da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
8281da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE |
8291da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_RESUME),
8301da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
8311da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8321da177e4SLinus Torvalds 	.rate_min =		8000,
8331da177e4SLinus Torvalds 	.rate_max =		48000,
8341da177e4SLinus Torvalds 	.channels_min =		1,
8351da177e4SLinus Torvalds 	.channels_max =		2,
8361da177e4SLinus Torvalds 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
8371da177e4SLinus Torvalds 	.period_bytes_min =	64,
8381da177e4SLinus Torvalds 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
8391da177e4SLinus Torvalds 	.periods_min =		3,
8401da177e4SLinus Torvalds 	.periods_max =		1024,
8411da177e4SLinus Torvalds 	.fifo_size =		0,
8421da177e4SLinus Torvalds };
8431da177e4SLinus Torvalds 
844420b0c1bSBhumika Goyal static const struct snd_pcm_hardware snd_ymfpci_capture =
8451da177e4SLinus Torvalds {
8461da177e4SLinus Torvalds 	.info =			(SNDRV_PCM_INFO_MMAP |
8471da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_MMAP_VALID |
8481da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_INTERLEAVED |
8491da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
8501da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_PAUSE |
8511da177e4SLinus Torvalds 				 SNDRV_PCM_INFO_RESUME),
8521da177e4SLinus Torvalds 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
8531da177e4SLinus Torvalds 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
8541da177e4SLinus Torvalds 	.rate_min =		8000,
8551da177e4SLinus Torvalds 	.rate_max =		48000,
8561da177e4SLinus Torvalds 	.channels_min =		1,
8571da177e4SLinus Torvalds 	.channels_max =		2,
8581da177e4SLinus Torvalds 	.buffer_bytes_max =	256 * 1024, /* FIXME: enough? */
8591da177e4SLinus Torvalds 	.period_bytes_min =	64,
8601da177e4SLinus Torvalds 	.period_bytes_max =	256 * 1024, /* FIXME: enough? */
8611da177e4SLinus Torvalds 	.periods_min =		3,
8621da177e4SLinus Torvalds 	.periods_max =		1024,
8631da177e4SLinus Torvalds 	.fifo_size =		0,
8641da177e4SLinus Torvalds };
8651da177e4SLinus Torvalds 
snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime * runtime)866208a1b4cSTakashi Iwai static void snd_ymfpci_pcm_free_substream(struct snd_pcm_runtime *runtime)
8671da177e4SLinus Torvalds {
8684d572776SJesper Juhl 	kfree(runtime->private_data);
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds 
snd_ymfpci_playback_open_1(struct snd_pcm_substream * substream)871208a1b4cSTakashi Iwai static int snd_ymfpci_playback_open_1(struct snd_pcm_substream *substream)
8721da177e4SLinus Torvalds {
873208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
874208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
875208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
87684f9df15SClemens Ladisch 	int err;
87784f9df15SClemens Ladisch 
87884f9df15SClemens Ladisch 	runtime->hw = snd_ymfpci_playback;
87984f9df15SClemens Ladisch 	/* FIXME? True value is 256/48 = 5.33333 ms */
88084f9df15SClemens Ladisch 	err = snd_pcm_hw_constraint_minmax(runtime,
88184f9df15SClemens Ladisch 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
88284f9df15SClemens Ladisch 					   5334, UINT_MAX);
88384f9df15SClemens Ladisch 	if (err < 0)
88484f9df15SClemens Ladisch 		return err;
8855b0416a3SClemens Ladisch 	err = snd_pcm_hw_rule_noresample(runtime, 48000);
8865b0416a3SClemens Ladisch 	if (err < 0)
8875b0416a3SClemens Ladisch 		return err;
8881da177e4SLinus Torvalds 
889e560d8d8STakashi Iwai 	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
8901da177e4SLinus Torvalds 	if (ypcm == NULL)
8911da177e4SLinus Torvalds 		return -ENOMEM;
8921da177e4SLinus Torvalds 	ypcm->chip = chip;
8931da177e4SLinus Torvalds 	ypcm->type = PLAYBACK_VOICE;
8941da177e4SLinus Torvalds 	ypcm->substream = substream;
8951da177e4SLinus Torvalds 	runtime->private_data = ypcm;
8961da177e4SLinus Torvalds 	runtime->private_free = snd_ymfpci_pcm_free_substream;
8971da177e4SLinus Torvalds 	return 0;
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds /* call with spinlock held */
ymfpci_open_extension(struct snd_ymfpci * chip)901208a1b4cSTakashi Iwai static void ymfpci_open_extension(struct snd_ymfpci *chip)
9021da177e4SLinus Torvalds {
9031da177e4SLinus Torvalds 	if (! chip->rear_opened) {
9041da177e4SLinus Torvalds 		if (! chip->spdif_opened) /* set AC3 */
9051da177e4SLinus Torvalds 			snd_ymfpci_writel(chip, YDSXGR_MODE,
9061da177e4SLinus Torvalds 					  snd_ymfpci_readl(chip, YDSXGR_MODE) | (1 << 30));
9071da177e4SLinus Torvalds 		/* enable second codec (4CHEN) */
9081da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
9091da177e4SLinus Torvalds 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) | 0x0010);
9101da177e4SLinus Torvalds 	}
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds /* call with spinlock held */
ymfpci_close_extension(struct snd_ymfpci * chip)914208a1b4cSTakashi Iwai static void ymfpci_close_extension(struct snd_ymfpci *chip)
9151da177e4SLinus Torvalds {
9161da177e4SLinus Torvalds 	if (! chip->rear_opened) {
9171da177e4SLinus Torvalds 		if (! chip->spdif_opened)
9181da177e4SLinus Torvalds 			snd_ymfpci_writel(chip, YDSXGR_MODE,
9191da177e4SLinus Torvalds 					  snd_ymfpci_readl(chip, YDSXGR_MODE) & ~(1 << 30));
9201da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SECCONFIG,
9211da177e4SLinus Torvalds 				  (snd_ymfpci_readw(chip, YDSXGR_SECCONFIG) & ~0x0330) & ~0x0010);
9221da177e4SLinus Torvalds 	}
9231da177e4SLinus Torvalds }
9241da177e4SLinus Torvalds 
snd_ymfpci_playback_open(struct snd_pcm_substream * substream)925208a1b4cSTakashi Iwai static int snd_ymfpci_playback_open(struct snd_pcm_substream *substream)
9261da177e4SLinus Torvalds {
927208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
928208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
929208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
9301da177e4SLinus Torvalds 	int err;
9311da177e4SLinus Torvalds 
932e7daaeedSTakashi Iwai 	err = snd_ymfpci_playback_open_1(substream);
933e7daaeedSTakashi Iwai 	if (err < 0)
9341da177e4SLinus Torvalds 		return err;
9351da177e4SLinus Torvalds 	ypcm = runtime->private_data;
9361da177e4SLinus Torvalds 	ypcm->output_front = 1;
9371da177e4SLinus Torvalds 	ypcm->output_rear = chip->mode_dup4ch ? 1 : 0;
938d9301263SGlen Masgai 	ypcm->swap_rear = 0;
9391da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9401da177e4SLinus Torvalds 	if (ypcm->output_rear) {
9411da177e4SLinus Torvalds 		ymfpci_open_extension(chip);
9421da177e4SLinus Torvalds 		chip->rear_opened++;
9431da177e4SLinus Torvalds 	}
9441da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9451da177e4SLinus Torvalds 	return 0;
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
snd_ymfpci_playback_spdif_open(struct snd_pcm_substream * substream)948208a1b4cSTakashi Iwai static int snd_ymfpci_playback_spdif_open(struct snd_pcm_substream *substream)
9491da177e4SLinus Torvalds {
950208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
951208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
952208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
9531da177e4SLinus Torvalds 	int err;
9541da177e4SLinus Torvalds 
955e7daaeedSTakashi Iwai 	err = snd_ymfpci_playback_open_1(substream);
956e7daaeedSTakashi Iwai 	if (err < 0)
9571da177e4SLinus Torvalds 		return err;
9581da177e4SLinus Torvalds 	ypcm = runtime->private_data;
9591da177e4SLinus Torvalds 	ypcm->output_front = 0;
9601da177e4SLinus Torvalds 	ypcm->output_rear = 1;
961d9301263SGlen Masgai 	ypcm->swap_rear = 1;
9621da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9631da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
9641da177e4SLinus Torvalds 			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) | 2);
9651da177e4SLinus Torvalds 	ymfpci_open_extension(chip);
9661da177e4SLinus Torvalds 	chip->spdif_pcm_bits = chip->spdif_bits;
9671da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
9681da177e4SLinus Torvalds 	chip->spdif_opened++;
9691da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	chip->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
9721da177e4SLinus Torvalds 	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
9731da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
9741da177e4SLinus Torvalds 	return 0;
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
snd_ymfpci_playback_4ch_open(struct snd_pcm_substream * substream)977208a1b4cSTakashi Iwai static int snd_ymfpci_playback_4ch_open(struct snd_pcm_substream *substream)
9781da177e4SLinus Torvalds {
979208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
980208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
981208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
9821da177e4SLinus Torvalds 	int err;
9831da177e4SLinus Torvalds 
984e7daaeedSTakashi Iwai 	err = snd_ymfpci_playback_open_1(substream);
985e7daaeedSTakashi Iwai 	if (err < 0)
9861da177e4SLinus Torvalds 		return err;
9871da177e4SLinus Torvalds 	ypcm = runtime->private_data;
9881da177e4SLinus Torvalds 	ypcm->output_front = 0;
9891da177e4SLinus Torvalds 	ypcm->output_rear = 1;
990d9301263SGlen Masgai 	ypcm->swap_rear = 0;
9911da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
9921da177e4SLinus Torvalds 	ymfpci_open_extension(chip);
9931da177e4SLinus Torvalds 	chip->rear_opened++;
9941da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
9951da177e4SLinus Torvalds 	return 0;
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
snd_ymfpci_capture_open(struct snd_pcm_substream * substream,u32 capture_bank_number)998208a1b4cSTakashi Iwai static int snd_ymfpci_capture_open(struct snd_pcm_substream *substream,
9991da177e4SLinus Torvalds 				   u32 capture_bank_number)
10001da177e4SLinus Torvalds {
1001208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1002208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
1003208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm;
100484f9df15SClemens Ladisch 	int err;
100584f9df15SClemens Ladisch 
100684f9df15SClemens Ladisch 	runtime->hw = snd_ymfpci_capture;
100784f9df15SClemens Ladisch 	/* FIXME? True value is 256/48 = 5.33333 ms */
100884f9df15SClemens Ladisch 	err = snd_pcm_hw_constraint_minmax(runtime,
100984f9df15SClemens Ladisch 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
101084f9df15SClemens Ladisch 					   5334, UINT_MAX);
101184f9df15SClemens Ladisch 	if (err < 0)
101284f9df15SClemens Ladisch 		return err;
10135b0416a3SClemens Ladisch 	err = snd_pcm_hw_rule_noresample(runtime, 48000);
10145b0416a3SClemens Ladisch 	if (err < 0)
10155b0416a3SClemens Ladisch 		return err;
10161da177e4SLinus Torvalds 
1017e560d8d8STakashi Iwai 	ypcm = kzalloc(sizeof(*ypcm), GFP_KERNEL);
10181da177e4SLinus Torvalds 	if (ypcm == NULL)
10191da177e4SLinus Torvalds 		return -ENOMEM;
10201da177e4SLinus Torvalds 	ypcm->chip = chip;
10211da177e4SLinus Torvalds 	ypcm->type = capture_bank_number + CAPTURE_REC;
10221da177e4SLinus Torvalds 	ypcm->substream = substream;
10231da177e4SLinus Torvalds 	ypcm->capture_bank_number = capture_bank_number;
10241da177e4SLinus Torvalds 	chip->capture_substream[capture_bank_number] = substream;
10251da177e4SLinus Torvalds 	runtime->private_data = ypcm;
10261da177e4SLinus Torvalds 	runtime->private_free = snd_ymfpci_pcm_free_substream;
10271da177e4SLinus Torvalds 	snd_ymfpci_hw_start(chip);
10281da177e4SLinus Torvalds 	return 0;
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds 
snd_ymfpci_capture_rec_open(struct snd_pcm_substream * substream)1031208a1b4cSTakashi Iwai static int snd_ymfpci_capture_rec_open(struct snd_pcm_substream *substream)
10321da177e4SLinus Torvalds {
10331da177e4SLinus Torvalds 	return snd_ymfpci_capture_open(substream, 0);
10341da177e4SLinus Torvalds }
10351da177e4SLinus Torvalds 
snd_ymfpci_capture_ac97_open(struct snd_pcm_substream * substream)1036208a1b4cSTakashi Iwai static int snd_ymfpci_capture_ac97_open(struct snd_pcm_substream *substream)
10371da177e4SLinus Torvalds {
10381da177e4SLinus Torvalds 	return snd_ymfpci_capture_open(substream, 1);
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds 
snd_ymfpci_playback_close_1(struct snd_pcm_substream * substream)1041208a1b4cSTakashi Iwai static int snd_ymfpci_playback_close_1(struct snd_pcm_substream *substream)
10421da177e4SLinus Torvalds {
10431da177e4SLinus Torvalds 	return 0;
10441da177e4SLinus Torvalds }
10451da177e4SLinus Torvalds 
snd_ymfpci_playback_close(struct snd_pcm_substream * substream)1046208a1b4cSTakashi Iwai static int snd_ymfpci_playback_close(struct snd_pcm_substream *substream)
10471da177e4SLinus Torvalds {
1048208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1049208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10521da177e4SLinus Torvalds 	if (ypcm->output_rear && chip->rear_opened > 0) {
10531da177e4SLinus Torvalds 		chip->rear_opened--;
10541da177e4SLinus Torvalds 		ymfpci_close_extension(chip);
10551da177e4SLinus Torvalds 	}
10561da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10571da177e4SLinus Torvalds 	return snd_ymfpci_playback_close_1(substream);
10581da177e4SLinus Torvalds }
10591da177e4SLinus Torvalds 
snd_ymfpci_playback_spdif_close(struct snd_pcm_substream * substream)1060208a1b4cSTakashi Iwai static int snd_ymfpci_playback_spdif_close(struct snd_pcm_substream *substream)
10611da177e4SLinus Torvalds {
1062208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10651da177e4SLinus Torvalds 	chip->spdif_opened = 0;
10661da177e4SLinus Torvalds 	ymfpci_close_extension(chip);
10671da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL,
10681da177e4SLinus Torvalds 			  snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & ~2);
10691da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
10701da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10711da177e4SLinus Torvalds 	chip->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
10721da177e4SLinus Torvalds 	snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE |
10731da177e4SLinus Torvalds 		       SNDRV_CTL_EVENT_MASK_INFO, &chip->spdif_pcm_ctl->id);
10741da177e4SLinus Torvalds 	return snd_ymfpci_playback_close_1(substream);
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds 
snd_ymfpci_playback_4ch_close(struct snd_pcm_substream * substream)1077208a1b4cSTakashi Iwai static int snd_ymfpci_playback_4ch_close(struct snd_pcm_substream *substream)
10781da177e4SLinus Torvalds {
1079208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
10821da177e4SLinus Torvalds 	if (chip->rear_opened > 0) {
10831da177e4SLinus Torvalds 		chip->rear_opened--;
10841da177e4SLinus Torvalds 		ymfpci_close_extension(chip);
10851da177e4SLinus Torvalds 	}
10861da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
10871da177e4SLinus Torvalds 	return snd_ymfpci_playback_close_1(substream);
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
snd_ymfpci_capture_close(struct snd_pcm_substream * substream)1090208a1b4cSTakashi Iwai static int snd_ymfpci_capture_close(struct snd_pcm_substream *substream)
10911da177e4SLinus Torvalds {
1092208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_pcm_substream_chip(substream);
1093208a1b4cSTakashi Iwai 	struct snd_pcm_runtime *runtime = substream->runtime;
1094208a1b4cSTakashi Iwai 	struct snd_ymfpci_pcm *ypcm = runtime->private_data;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	if (ypcm != NULL) {
10971da177e4SLinus Torvalds 		chip->capture_substream[ypcm->capture_bank_number] = NULL;
10981da177e4SLinus Torvalds 		snd_ymfpci_hw_stop(chip);
10991da177e4SLinus Torvalds 	}
11001da177e4SLinus Torvalds 	return 0;
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds 
11036769e988SJulia Lawall static const struct snd_pcm_ops snd_ymfpci_playback_ops = {
11041da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_open,
11051da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_close,
11061da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
11071da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
11081da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
11091da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
11101da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
11111da177e4SLinus Torvalds };
11121da177e4SLinus Torvalds 
11136769e988SJulia Lawall static const struct snd_pcm_ops snd_ymfpci_capture_rec_ops = {
11141da177e4SLinus Torvalds 	.open =			snd_ymfpci_capture_rec_open,
11151da177e4SLinus Torvalds 	.close =		snd_ymfpci_capture_close,
11161da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_capture_hw_free,
11171da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_capture_prepare,
11181da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_capture_trigger,
11191da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_capture_pointer,
11201da177e4SLinus Torvalds };
11211da177e4SLinus Torvalds 
snd_ymfpci_pcm(struct snd_ymfpci * chip,int device)112238c47181SLars-Peter Clausen int snd_ymfpci_pcm(struct snd_ymfpci *chip, int device)
11231da177e4SLinus Torvalds {
1124208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
11251da177e4SLinus Torvalds 	int err;
11261da177e4SLinus Torvalds 
1127e7daaeedSTakashi Iwai 	err = snd_pcm_new(chip->card, "YMFPCI", device, 32, 1, &pcm);
1128e7daaeedSTakashi Iwai 	if (err < 0)
11291da177e4SLinus Torvalds 		return err;
11301da177e4SLinus Torvalds 	pcm->private_data = chip;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_ops);
11331da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_rec_ops);
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	/* global setup */
11361da177e4SLinus Torvalds 	pcm->info_flags = 0;
11371da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI");
11381da177e4SLinus Torvalds 	chip->pcm = pcm;
11391da177e4SLinus Torvalds 
1140b6ed90c0STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1141b6ed90c0STakashi Iwai 				       &chip->pci->dev, 64*1024, 256*1024);
11421da177e4SLinus Torvalds 
114338c47181SLars-Peter Clausen 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
11440afdb8f2STakashi Iwai 				     snd_pcm_std_chmaps, 2, 0, NULL);
11451da177e4SLinus Torvalds }
11461da177e4SLinus Torvalds 
11476769e988SJulia Lawall static const struct snd_pcm_ops snd_ymfpci_capture_ac97_ops = {
11481da177e4SLinus Torvalds 	.open =			snd_ymfpci_capture_ac97_open,
11491da177e4SLinus Torvalds 	.close =		snd_ymfpci_capture_close,
11501da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_capture_hw_free,
11511da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_capture_prepare,
11521da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_capture_trigger,
11531da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_capture_pointer,
11541da177e4SLinus Torvalds };
11551da177e4SLinus Torvalds 
snd_ymfpci_pcm2(struct snd_ymfpci * chip,int device)115638c47181SLars-Peter Clausen int snd_ymfpci_pcm2(struct snd_ymfpci *chip, int device)
11571da177e4SLinus Torvalds {
1158208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
11591da177e4SLinus Torvalds 	int err;
11601da177e4SLinus Torvalds 
1161e7daaeedSTakashi Iwai 	err = snd_pcm_new(chip->card, "YMFPCI - PCM2", device, 0, 1, &pcm);
1162e7daaeedSTakashi Iwai 	if (err < 0)
11631da177e4SLinus Torvalds 		return err;
11641da177e4SLinus Torvalds 	pcm->private_data = chip;
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ymfpci_capture_ac97_ops);
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	/* global setup */
11691da177e4SLinus Torvalds 	pcm->info_flags = 0;
11701da177e4SLinus Torvalds 	sprintf(pcm->name, "YMFPCI - %s",
11711da177e4SLinus Torvalds 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754 ? "Direct Recording" : "AC'97");
11721da177e4SLinus Torvalds 	chip->pcm2 = pcm;
11731da177e4SLinus Torvalds 
1174b6ed90c0STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1175b6ed90c0STakashi Iwai 				       &chip->pci->dev, 64*1024, 256*1024);
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	return 0;
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds 
11806769e988SJulia Lawall static const struct snd_pcm_ops snd_ymfpci_playback_spdif_ops = {
11811da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_spdif_open,
11821da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_spdif_close,
11831da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
11841da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
11851da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
11861da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
11871da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
11881da177e4SLinus Torvalds };
11891da177e4SLinus Torvalds 
snd_ymfpci_pcm_spdif(struct snd_ymfpci * chip,int device)119038c47181SLars-Peter Clausen int snd_ymfpci_pcm_spdif(struct snd_ymfpci *chip, int device)
11911da177e4SLinus Torvalds {
1192208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
11931da177e4SLinus Torvalds 	int err;
11941da177e4SLinus Torvalds 
1195e7daaeedSTakashi Iwai 	err = snd_pcm_new(chip->card, "YMFPCI - IEC958", device, 1, 0, &pcm);
1196e7daaeedSTakashi Iwai 	if (err < 0)
11971da177e4SLinus Torvalds 		return err;
11981da177e4SLinus Torvalds 	pcm->private_data = chip;
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_spdif_ops);
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	/* global setup */
12031da177e4SLinus Torvalds 	pcm->info_flags = 0;
12041da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI - IEC958");
12051da177e4SLinus Torvalds 	chip->pcm_spdif = pcm;
12061da177e4SLinus Torvalds 
1207b6ed90c0STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1208b6ed90c0STakashi Iwai 				       &chip->pci->dev, 64*1024, 256*1024);
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	return 0;
12111da177e4SLinus Torvalds }
12121da177e4SLinus Torvalds 
12136769e988SJulia Lawall static const struct snd_pcm_ops snd_ymfpci_playback_4ch_ops = {
12141da177e4SLinus Torvalds 	.open =			snd_ymfpci_playback_4ch_open,
12151da177e4SLinus Torvalds 	.close =		snd_ymfpci_playback_4ch_close,
12161da177e4SLinus Torvalds 	.hw_params =		snd_ymfpci_playback_hw_params,
12171da177e4SLinus Torvalds 	.hw_free =		snd_ymfpci_playback_hw_free,
12181da177e4SLinus Torvalds 	.prepare =		snd_ymfpci_playback_prepare,
12191da177e4SLinus Torvalds 	.trigger =		snd_ymfpci_playback_trigger,
12201da177e4SLinus Torvalds 	.pointer =		snd_ymfpci_playback_pointer,
12211da177e4SLinus Torvalds };
12221da177e4SLinus Torvalds 
12230afdb8f2STakashi Iwai static const struct snd_pcm_chmap_elem surround_map[] = {
12240afdb8f2STakashi Iwai 	{ .channels = 1,
12255efbc261STakashi Iwai 	  .map = { SNDRV_CHMAP_MONO } },
12260afdb8f2STakashi Iwai 	{ .channels = 2,
12270afdb8f2STakashi Iwai 	  .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
12280afdb8f2STakashi Iwai 	{ }
12290afdb8f2STakashi Iwai };
12300afdb8f2STakashi Iwai 
snd_ymfpci_pcm_4ch(struct snd_ymfpci * chip,int device)123138c47181SLars-Peter Clausen int snd_ymfpci_pcm_4ch(struct snd_ymfpci *chip, int device)
12321da177e4SLinus Torvalds {
1233208a1b4cSTakashi Iwai 	struct snd_pcm *pcm;
12341da177e4SLinus Torvalds 	int err;
12351da177e4SLinus Torvalds 
1236e7daaeedSTakashi Iwai 	err = snd_pcm_new(chip->card, "YMFPCI - Rear", device, 1, 0, &pcm);
1237e7daaeedSTakashi Iwai 	if (err < 0)
12381da177e4SLinus Torvalds 		return err;
12391da177e4SLinus Torvalds 	pcm->private_data = chip;
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ymfpci_playback_4ch_ops);
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	/* global setup */
12441da177e4SLinus Torvalds 	pcm->info_flags = 0;
12451da177e4SLinus Torvalds 	strcpy(pcm->name, "YMFPCI - Rear PCM");
12461da177e4SLinus Torvalds 	chip->pcm_4ch = pcm;
12471da177e4SLinus Torvalds 
1248b6ed90c0STakashi Iwai 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1249b6ed90c0STakashi Iwai 				       &chip->pci->dev, 64*1024, 256*1024);
12501da177e4SLinus Torvalds 
125138c47181SLars-Peter Clausen 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
12520afdb8f2STakashi Iwai 				     surround_map, 2, 0, NULL);
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
snd_ymfpci_spdif_default_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1255208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
12561da177e4SLinus Torvalds {
12571da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
12581da177e4SLinus Torvalds 	uinfo->count = 1;
12591da177e4SLinus Torvalds 	return 0;
12601da177e4SLinus Torvalds }
12611da177e4SLinus Torvalds 
snd_ymfpci_spdif_default_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1262208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_get(struct snd_kcontrol *kcontrol,
1263208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
12641da177e4SLinus Torvalds {
1265208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
12681da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = (chip->spdif_bits >> 0) & 0xff;
12691da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = (chip->spdif_bits >> 8) & 0xff;
1270fc80a202SClemens Ladisch 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
12711da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
12721da177e4SLinus Torvalds 	return 0;
12731da177e4SLinus Torvalds }
12741da177e4SLinus Torvalds 
snd_ymfpci_spdif_default_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1275208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_default_put(struct snd_kcontrol *kcontrol,
1276208a1b4cSTakashi Iwai 					 struct snd_ctl_elem_value *ucontrol)
12771da177e4SLinus Torvalds {
1278208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
12791da177e4SLinus Torvalds 	unsigned int val;
12801da177e4SLinus Torvalds 	int change;
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
12831da177e4SLinus Torvalds 	      (ucontrol->value.iec958.status[1] << 8);
12841da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
12851da177e4SLinus Torvalds 	change = chip->spdif_bits != val;
12861da177e4SLinus Torvalds 	chip->spdif_bits = val;
12871da177e4SLinus Torvalds 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 1) && chip->pcm_spdif == NULL)
12881da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
12891da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
12901da177e4SLinus Torvalds 	return change;
12911da177e4SLinus Torvalds }
12921da177e4SLinus Torvalds 
1293f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_spdif_default =
12941da177e4SLinus Torvalds {
12951da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
12961da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
12971da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_default_info,
12981da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_default_get,
12991da177e4SLinus Torvalds 	.put =		snd_ymfpci_spdif_default_put
13001da177e4SLinus Torvalds };
13011da177e4SLinus Torvalds 
snd_ymfpci_spdif_mask_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1302208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
13031da177e4SLinus Torvalds {
13041da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
13051da177e4SLinus Torvalds 	uinfo->count = 1;
13061da177e4SLinus Torvalds 	return 0;
13071da177e4SLinus Torvalds }
13081da177e4SLinus Torvalds 
snd_ymfpci_spdif_mask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1309208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_mask_get(struct snd_kcontrol *kcontrol,
1310208a1b4cSTakashi Iwai 				      struct snd_ctl_elem_value *ucontrol)
13111da177e4SLinus Torvalds {
1312208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13151da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = 0x3e;
13161da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = 0xff;
13171da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13181da177e4SLinus Torvalds 	return 0;
13191da177e4SLinus Torvalds }
13201da177e4SLinus Torvalds 
1321f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_spdif_mask =
13221da177e4SLinus Torvalds {
13231da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
13241da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
13251da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
13261da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_mask_info,
13271da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_mask_get,
13281da177e4SLinus Torvalds };
13291da177e4SLinus Torvalds 
snd_ymfpci_spdif_stream_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1330208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
13311da177e4SLinus Torvalds {
13321da177e4SLinus Torvalds 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
13331da177e4SLinus Torvalds 	uinfo->count = 1;
13341da177e4SLinus Torvalds 	return 0;
13351da177e4SLinus Torvalds }
13361da177e4SLinus Torvalds 
snd_ymfpci_spdif_stream_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1337208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_get(struct snd_kcontrol *kcontrol,
1338208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
13391da177e4SLinus Torvalds {
1340208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13431da177e4SLinus Torvalds 	ucontrol->value.iec958.status[0] = (chip->spdif_pcm_bits >> 0) & 0xff;
13441da177e4SLinus Torvalds 	ucontrol->value.iec958.status[1] = (chip->spdif_pcm_bits >> 8) & 0xff;
1345fc80a202SClemens Ladisch 	ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000;
13461da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13471da177e4SLinus Torvalds 	return 0;
13481da177e4SLinus Torvalds }
13491da177e4SLinus Torvalds 
snd_ymfpci_spdif_stream_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1350208a1b4cSTakashi Iwai static int snd_ymfpci_spdif_stream_put(struct snd_kcontrol *kcontrol,
1351208a1b4cSTakashi Iwai 					struct snd_ctl_elem_value *ucontrol)
13521da177e4SLinus Torvalds {
1353208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13541da177e4SLinus Torvalds 	unsigned int val;
13551da177e4SLinus Torvalds 	int change;
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	val = ((ucontrol->value.iec958.status[0] & 0x3e) << 0) |
13581da177e4SLinus Torvalds 	      (ucontrol->value.iec958.status[1] << 8);
13591da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13601da177e4SLinus Torvalds 	change = chip->spdif_pcm_bits != val;
13611da177e4SLinus Torvalds 	chip->spdif_pcm_bits = val;
13621da177e4SLinus Torvalds 	if ((snd_ymfpci_readw(chip, YDSXGR_SPDIFOUTCTRL) & 2))
13631da177e4SLinus Torvalds 		snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_pcm_bits);
13641da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13651da177e4SLinus Torvalds 	return change;
13661da177e4SLinus Torvalds }
13671da177e4SLinus Torvalds 
1368f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_spdif_stream =
13691da177e4SLinus Torvalds {
13701da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
13711da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
13721da177e4SLinus Torvalds 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
13731da177e4SLinus Torvalds 	.info =		snd_ymfpci_spdif_stream_info,
13741da177e4SLinus Torvalds 	.get =		snd_ymfpci_spdif_stream_get,
13751da177e4SLinus Torvalds 	.put =		snd_ymfpci_spdif_stream_put
13761da177e4SLinus Torvalds };
13771da177e4SLinus Torvalds 
snd_ymfpci_drec_source_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * info)1378208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *info)
13791da177e4SLinus Torvalds {
1380bed6896dSClemens Ladisch 	static const char *const texts[3] = {"AC'97", "IEC958", "ZV Port"};
13811da177e4SLinus Torvalds 
1382bed6896dSClemens Ladisch 	return snd_ctl_enum_info(info, 1, 3, texts);
13831da177e4SLinus Torvalds }
13841da177e4SLinus Torvalds 
snd_ymfpci_drec_source_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * value)1385208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
13861da177e4SLinus Torvalds {
1387208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
13881da177e4SLinus Torvalds 	u16 reg;
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
13911da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
13921da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
13931da177e4SLinus Torvalds 	if (!(reg & 0x100))
13941da177e4SLinus Torvalds 		value->value.enumerated.item[0] = 0;
13951da177e4SLinus Torvalds 	else
13961da177e4SLinus Torvalds 		value->value.enumerated.item[0] = 1 + ((reg & 0x200) != 0);
13971da177e4SLinus Torvalds 	return 0;
13981da177e4SLinus Torvalds }
13991da177e4SLinus Torvalds 
snd_ymfpci_drec_source_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * value)1400208a1b4cSTakashi Iwai static int snd_ymfpci_drec_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *value)
14011da177e4SLinus Torvalds {
1402208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
14031da177e4SLinus Torvalds 	u16 reg, old_reg;
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
14061da177e4SLinus Torvalds 	old_reg = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
14071da177e4SLinus Torvalds 	if (value->value.enumerated.item[0] == 0)
14081da177e4SLinus Torvalds 		reg = old_reg & ~0x100;
14091da177e4SLinus Torvalds 	else
14101da177e4SLinus Torvalds 		reg = (old_reg & ~0x300) | 0x100 | ((value->value.enumerated.item[0] == 2) << 9);
14111da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, reg);
14121da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
14131da177e4SLinus Torvalds 	return reg != old_reg;
14141da177e4SLinus Torvalds }
14151da177e4SLinus Torvalds 
1416f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_drec_source = {
14171da177e4SLinus Torvalds 	.access =	SNDRV_CTL_ELEM_ACCESS_READWRITE,
14181da177e4SLinus Torvalds 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
14191da177e4SLinus Torvalds 	.name =		"Direct Recording Source",
14201da177e4SLinus Torvalds 	.info =		snd_ymfpci_drec_source_info,
14211da177e4SLinus Torvalds 	.get =		snd_ymfpci_drec_source_get,
14221da177e4SLinus Torvalds 	.put =		snd_ymfpci_drec_source_put
14231da177e4SLinus Torvalds };
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds /*
14261da177e4SLinus Torvalds  *  Mixer controls
14271da177e4SLinus Torvalds  */
14281da177e4SLinus Torvalds 
1429d602c885SGlen Masgai #define YMFPCI_SINGLE(xname, xindex, reg, shift) \
14301da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
14311da177e4SLinus Torvalds   .info = snd_ymfpci_info_single, \
14321da177e4SLinus Torvalds   .get = snd_ymfpci_get_single, .put = snd_ymfpci_put_single, \
1433d602c885SGlen Masgai   .private_value = ((reg) | ((shift) << 16)) }
14341da177e4SLinus Torvalds 
1435a5ce8890STakashi Iwai #define snd_ymfpci_info_single		snd_ctl_boolean_mono_info
14361da177e4SLinus Torvalds 
snd_ymfpci_get_single(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1437208a1b4cSTakashi Iwai static int snd_ymfpci_get_single(struct snd_kcontrol *kcontrol,
1438208a1b4cSTakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
14391da177e4SLinus Torvalds {
1440208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1441d602c885SGlen Masgai 	int reg = kcontrol->private_value & 0xffff;
1442d602c885SGlen Masgai 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1443d602c885SGlen Masgai 	unsigned int mask = 1;
14441da177e4SLinus Torvalds 
1445d602c885SGlen Masgai 	switch (reg) {
14461da177e4SLinus Torvalds 	case YDSXGR_SPDIFOUTCTRL: break;
14471da177e4SLinus Torvalds 	case YDSXGR_SPDIFINCTRL: break;
14481da177e4SLinus Torvalds 	default: return -EINVAL;
14491da177e4SLinus Torvalds 	}
1450d602c885SGlen Masgai 	ucontrol->value.integer.value[0] =
1451d602c885SGlen Masgai 		(snd_ymfpci_readl(chip, reg) >> shift) & mask;
14521da177e4SLinus Torvalds 	return 0;
14531da177e4SLinus Torvalds }
14541da177e4SLinus Torvalds 
snd_ymfpci_put_single(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1455208a1b4cSTakashi Iwai static int snd_ymfpci_put_single(struct snd_kcontrol *kcontrol,
1456208a1b4cSTakashi Iwai 				 struct snd_ctl_elem_value *ucontrol)
14571da177e4SLinus Torvalds {
1458208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1459d602c885SGlen Masgai 	int reg = kcontrol->private_value & 0xffff;
1460d602c885SGlen Masgai 	unsigned int shift = (kcontrol->private_value >> 16) & 0xff;
1461d602c885SGlen Masgai  	unsigned int mask = 1;
14621da177e4SLinus Torvalds 	int change;
14631da177e4SLinus Torvalds 	unsigned int val, oval;
14641da177e4SLinus Torvalds 
1465d602c885SGlen Masgai 	switch (reg) {
14661da177e4SLinus Torvalds 	case YDSXGR_SPDIFOUTCTRL: break;
14671da177e4SLinus Torvalds 	case YDSXGR_SPDIFINCTRL: break;
14681da177e4SLinus Torvalds 	default: return -EINVAL;
14691da177e4SLinus Torvalds 	}
14701da177e4SLinus Torvalds 	val = (ucontrol->value.integer.value[0] & mask);
14711da177e4SLinus Torvalds 	val <<= shift;
14721da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
14731da177e4SLinus Torvalds 	oval = snd_ymfpci_readl(chip, reg);
14741da177e4SLinus Torvalds 	val = (oval & ~(mask << shift)) | val;
14751da177e4SLinus Torvalds 	change = val != oval;
14761da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, reg, val);
14771da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
14781da177e4SLinus Torvalds 	return change;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
14810cb29ea0STakashi Iwai static const DECLARE_TLV_DB_LINEAR(db_scale_native, TLV_DB_GAIN_MUTE, 0);
148233925186STakashi Iwai 
14831da177e4SLinus Torvalds #define YMFPCI_DOUBLE(xname, xindex, reg) \
14841da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
148533925186STakashi Iwai   .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
14861da177e4SLinus Torvalds   .info = snd_ymfpci_info_double, \
14871da177e4SLinus Torvalds   .get = snd_ymfpci_get_double, .put = snd_ymfpci_put_double, \
148833925186STakashi Iwai   .private_value = reg, \
148933925186STakashi Iwai   .tlv = { .p = db_scale_native } }
14901da177e4SLinus Torvalds 
snd_ymfpci_info_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1491208a1b4cSTakashi Iwai static int snd_ymfpci_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
14921da177e4SLinus Torvalds {
14931da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
14961da177e4SLinus Torvalds 		return -EINVAL;
1497467a8c2fSAdrian Bunk 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
14981da177e4SLinus Torvalds 	uinfo->count = 2;
14991da177e4SLinus Torvalds 	uinfo->value.integer.min = 0;
1500467a8c2fSAdrian Bunk 	uinfo->value.integer.max = 16383;
15011da177e4SLinus Torvalds 	return 0;
15021da177e4SLinus Torvalds }
15031da177e4SLinus Torvalds 
snd_ymfpci_get_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1504208a1b4cSTakashi Iwai static int snd_ymfpci_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15051da177e4SLinus Torvalds {
1506208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15071da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
1508467a8c2fSAdrian Bunk 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
15091da177e4SLinus Torvalds 	unsigned int val;
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
15121da177e4SLinus Torvalds 		return -EINVAL;
15131da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15141da177e4SLinus Torvalds 	val = snd_ymfpci_readl(chip, reg);
15151da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15161da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
15171da177e4SLinus Torvalds 	ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
15181da177e4SLinus Torvalds 	return 0;
15191da177e4SLinus Torvalds }
15201da177e4SLinus Torvalds 
snd_ymfpci_put_double(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1521208a1b4cSTakashi Iwai static int snd_ymfpci_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15221da177e4SLinus Torvalds {
1523208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15241da177e4SLinus Torvalds 	unsigned int reg = kcontrol->private_value;
1525467a8c2fSAdrian Bunk 	unsigned int shift_left = 0, shift_right = 16, mask = 16383;
15261da177e4SLinus Torvalds 	int change;
15271da177e4SLinus Torvalds 	unsigned int val1, val2, oval;
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds 	if (reg < 0x80 || reg >= 0xc0)
15301da177e4SLinus Torvalds 		return -EINVAL;
15311da177e4SLinus Torvalds 	val1 = ucontrol->value.integer.value[0] & mask;
15321da177e4SLinus Torvalds 	val2 = ucontrol->value.integer.value[1] & mask;
15331da177e4SLinus Torvalds 	val1 <<= shift_left;
15341da177e4SLinus Torvalds 	val2 <<= shift_right;
15351da177e4SLinus Torvalds 	spin_lock_irq(&chip->reg_lock);
15361da177e4SLinus Torvalds 	oval = snd_ymfpci_readl(chip, reg);
15371da177e4SLinus Torvalds 	val1 = (oval & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
15381da177e4SLinus Torvalds 	change = val1 != oval;
15391da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, reg, val1);
15401da177e4SLinus Torvalds 	spin_unlock_irq(&chip->reg_lock);
15411da177e4SLinus Torvalds 	return change;
15421da177e4SLinus Torvalds }
15431da177e4SLinus Torvalds 
snd_ymfpci_put_nativedacvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1544177a7cdbSClemens Ladisch static int snd_ymfpci_put_nativedacvol(struct snd_kcontrol *kcontrol,
1545177a7cdbSClemens Ladisch 				       struct snd_ctl_elem_value *ucontrol)
1546177a7cdbSClemens Ladisch {
1547177a7cdbSClemens Ladisch 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
1548177a7cdbSClemens Ladisch 	unsigned int reg = YDSXGR_NATIVEDACOUTVOL;
1549177a7cdbSClemens Ladisch 	unsigned int reg2 = YDSXGR_BUF441OUTVOL;
1550177a7cdbSClemens Ladisch 	int change;
1551177a7cdbSClemens Ladisch 	unsigned int value, oval;
1552177a7cdbSClemens Ladisch 
1553177a7cdbSClemens Ladisch 	value = ucontrol->value.integer.value[0] & 0x3fff;
1554177a7cdbSClemens Ladisch 	value |= (ucontrol->value.integer.value[1] & 0x3fff) << 16;
1555177a7cdbSClemens Ladisch 	spin_lock_irq(&chip->reg_lock);
1556177a7cdbSClemens Ladisch 	oval = snd_ymfpci_readl(chip, reg);
1557177a7cdbSClemens Ladisch 	change = value != oval;
1558177a7cdbSClemens Ladisch 	snd_ymfpci_writel(chip, reg, value);
1559177a7cdbSClemens Ladisch 	snd_ymfpci_writel(chip, reg2, value);
1560177a7cdbSClemens Ladisch 	spin_unlock_irq(&chip->reg_lock);
1561177a7cdbSClemens Ladisch 	return change;
1562177a7cdbSClemens Ladisch }
1563177a7cdbSClemens Ladisch 
15641da177e4SLinus Torvalds /*
15651da177e4SLinus Torvalds  * 4ch duplication
15661da177e4SLinus Torvalds  */
1567a5ce8890STakashi Iwai #define snd_ymfpci_info_dup4ch		snd_ctl_boolean_mono_info
15681da177e4SLinus Torvalds 
snd_ymfpci_get_dup4ch(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1569208a1b4cSTakashi Iwai static int snd_ymfpci_get_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15701da177e4SLinus Torvalds {
1571208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15721da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = chip->mode_dup4ch;
15731da177e4SLinus Torvalds 	return 0;
15741da177e4SLinus Torvalds }
15751da177e4SLinus Torvalds 
snd_ymfpci_put_dup4ch(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1576208a1b4cSTakashi Iwai static int snd_ymfpci_put_dup4ch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
15771da177e4SLinus Torvalds {
1578208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
15791da177e4SLinus Torvalds 	int change;
15801da177e4SLinus Torvalds 	change = (ucontrol->value.integer.value[0] != chip->mode_dup4ch);
15811da177e4SLinus Torvalds 	if (change)
15821da177e4SLinus Torvalds 		chip->mode_dup4ch = !!ucontrol->value.integer.value[0];
15831da177e4SLinus Torvalds 	return change;
15841da177e4SLinus Torvalds }
15851da177e4SLinus Torvalds 
1586f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_dup4ch = {
15874d20bb1dSRaymond Yau 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
15884d20bb1dSRaymond Yau 	.name = "4ch Duplication",
15894d20bb1dSRaymond Yau 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
15904d20bb1dSRaymond Yau 	.info = snd_ymfpci_info_dup4ch,
15914d20bb1dSRaymond Yau 	.get = snd_ymfpci_get_dup4ch,
15924d20bb1dSRaymond Yau 	.put = snd_ymfpci_put_dup4ch,
15934d20bb1dSRaymond Yau };
15941da177e4SLinus Torvalds 
1595b4e5e707STakashi Iwai static const struct snd_kcontrol_new snd_ymfpci_controls[] = {
1596177a7cdbSClemens Ladisch {
1597177a7cdbSClemens Ladisch 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1598177a7cdbSClemens Ladisch 	.name = "Wave Playback Volume",
1599177a7cdbSClemens Ladisch 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1600177a7cdbSClemens Ladisch 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1601177a7cdbSClemens Ladisch 	.info = snd_ymfpci_info_double,
1602177a7cdbSClemens Ladisch 	.get = snd_ymfpci_get_double,
1603177a7cdbSClemens Ladisch 	.put = snd_ymfpci_put_nativedacvol,
1604177a7cdbSClemens Ladisch 	.private_value = YDSXGR_NATIVEDACOUTVOL,
1605177a7cdbSClemens Ladisch 	.tlv = { .p = db_scale_native },
1606177a7cdbSClemens Ladisch },
16071da177e4SLinus Torvalds YMFPCI_DOUBLE("Wave Capture Volume", 0, YDSXGR_NATIVEDACLOOPVOL),
16081da177e4SLinus Torvalds YMFPCI_DOUBLE("Digital Capture Volume", 0, YDSXGR_NATIVEDACINVOL),
16091da177e4SLinus Torvalds YMFPCI_DOUBLE("Digital Capture Volume", 1, YDSXGR_NATIVEADCINVOL),
16101da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Playback Volume", 0, YDSXGR_PRIADCOUTVOL),
16111da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Capture Volume", 0, YDSXGR_PRIADCLOOPVOL),
16121da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Playback Volume", 1, YDSXGR_SECADCOUTVOL),
16131da177e4SLinus Torvalds YMFPCI_DOUBLE("ADC Capture Volume", 1, YDSXGR_SECADCLOOPVOL),
161489f3325aSRaymond Yau YMFPCI_DOUBLE("FM Legacy Playback Volume", 0, YDSXGR_LEGACYOUTVOL),
16151da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ", PLAYBACK,VOLUME), 0, YDSXGR_ZVOUTVOL),
16161da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("", CAPTURE,VOLUME), 0, YDSXGR_ZVLOOPVOL),
16171da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("AC97 ",PLAYBACK,VOLUME), 1, YDSXGR_SPDIFOUTVOL),
16181da177e4SLinus Torvalds YMFPCI_DOUBLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,VOLUME), 1, YDSXGR_SPDIFLOOPVOL),
1619d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), 0, YDSXGR_SPDIFOUTCTRL, 0),
1620d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, YDSXGR_SPDIFINCTRL, 0),
1621d602c885SGlen Masgai YMFPCI_SINGLE(SNDRV_CTL_NAME_IEC958("Loop",NONE,NONE), 0, YDSXGR_SPDIFINCTRL, 4),
16221da177e4SLinus Torvalds };
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds /*
16261da177e4SLinus Torvalds  * GPIO
16271da177e4SLinus Torvalds  */
16281da177e4SLinus Torvalds 
snd_ymfpci_get_gpio_out(struct snd_ymfpci * chip,int pin)1629208a1b4cSTakashi Iwai static int snd_ymfpci_get_gpio_out(struct snd_ymfpci *chip, int pin)
16301da177e4SLinus Torvalds {
16311da177e4SLinus Torvalds 	u16 reg, mode;
16321da177e4SLinus Torvalds 	unsigned long flags;
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
16351da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
16361da177e4SLinus Torvalds 	reg &= ~(1 << (pin + 8));
16371da177e4SLinus Torvalds 	reg |= (1 << pin);
16381da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
16391da177e4SLinus Torvalds 	/* set the level mode for input line */
16401da177e4SLinus Torvalds 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOTYPECONFIG);
16411da177e4SLinus Torvalds 	mode &= ~(3 << (pin * 2));
16421da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOTYPECONFIG, mode);
16431da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
16441da177e4SLinus Torvalds 	mode = snd_ymfpci_readw(chip, YDSXGR_GPIOINSTATUS);
16451da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
16461da177e4SLinus Torvalds 	return (mode >> pin) & 1;
16471da177e4SLinus Torvalds }
16481da177e4SLinus Torvalds 
snd_ymfpci_set_gpio_out(struct snd_ymfpci * chip,int pin,int enable)1649208a1b4cSTakashi Iwai static int snd_ymfpci_set_gpio_out(struct snd_ymfpci *chip, int pin, int enable)
16501da177e4SLinus Torvalds {
16511da177e4SLinus Torvalds 	u16 reg;
16521da177e4SLinus Torvalds 	unsigned long flags;
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
16551da177e4SLinus Torvalds 	reg = snd_ymfpci_readw(chip, YDSXGR_GPIOFUNCENABLE);
16561da177e4SLinus Torvalds 	reg &= ~(1 << pin);
16571da177e4SLinus Torvalds 	reg &= ~(1 << (pin + 8));
16581da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg);
16591da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOOUTCTRL, enable << pin);
16601da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GPIOFUNCENABLE, reg | (1 << (pin + 8)));
16611da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	return 0;
16641da177e4SLinus Torvalds }
16651da177e4SLinus Torvalds 
1666a5ce8890STakashi Iwai #define snd_ymfpci_gpio_sw_info		snd_ctl_boolean_mono_info
16671da177e4SLinus Torvalds 
snd_ymfpci_gpio_sw_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1668208a1b4cSTakashi Iwai static int snd_ymfpci_gpio_sw_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16691da177e4SLinus Torvalds {
1670208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
16711da177e4SLinus Torvalds 	int pin = (int)kcontrol->private_value;
16721da177e4SLinus Torvalds 	ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
16731da177e4SLinus Torvalds 	return 0;
16741da177e4SLinus Torvalds }
16751da177e4SLinus Torvalds 
snd_ymfpci_gpio_sw_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1676208a1b4cSTakashi Iwai static int snd_ymfpci_gpio_sw_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
16771da177e4SLinus Torvalds {
1678208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
16791da177e4SLinus Torvalds 	int pin = (int)kcontrol->private_value;
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds 	if (snd_ymfpci_get_gpio_out(chip, pin) != ucontrol->value.integer.value[0]) {
16821da177e4SLinus Torvalds 		snd_ymfpci_set_gpio_out(chip, pin, !!ucontrol->value.integer.value[0]);
16831da177e4SLinus Torvalds 		ucontrol->value.integer.value[0] = snd_ymfpci_get_gpio_out(chip, pin);
16841da177e4SLinus Torvalds 		return 1;
16851da177e4SLinus Torvalds 	}
16861da177e4SLinus Torvalds 	return 0;
16871da177e4SLinus Torvalds }
16881da177e4SLinus Torvalds 
1689f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_rear_shared = {
16901da177e4SLinus Torvalds 	.name = "Shared Rear/Line-In Switch",
16911da177e4SLinus Torvalds 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
16921da177e4SLinus Torvalds 	.info = snd_ymfpci_gpio_sw_info,
16931da177e4SLinus Torvalds 	.get = snd_ymfpci_gpio_sw_get,
16941da177e4SLinus Torvalds 	.put = snd_ymfpci_gpio_sw_put,
16951da177e4SLinus Torvalds 	.private_value = 2,
16961da177e4SLinus Torvalds };
16971da177e4SLinus Torvalds 
16989bcf6551SClemens Ladisch /*
16999bcf6551SClemens Ladisch  * PCM voice volume
17009bcf6551SClemens Ladisch  */
17019bcf6551SClemens Ladisch 
snd_ymfpci_pcm_vol_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1702208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_info(struct snd_kcontrol *kcontrol,
1703208a1b4cSTakashi Iwai 				   struct snd_ctl_elem_info *uinfo)
17049bcf6551SClemens Ladisch {
17059bcf6551SClemens Ladisch 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
17069bcf6551SClemens Ladisch 	uinfo->count = 2;
17079bcf6551SClemens Ladisch 	uinfo->value.integer.min = 0;
17089bcf6551SClemens Ladisch 	uinfo->value.integer.max = 0x8000;
17099bcf6551SClemens Ladisch 	return 0;
17109bcf6551SClemens Ladisch }
17119bcf6551SClemens Ladisch 
snd_ymfpci_pcm_vol_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1712208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_get(struct snd_kcontrol *kcontrol,
1713208a1b4cSTakashi Iwai 				  struct snd_ctl_elem_value *ucontrol)
17149bcf6551SClemens Ladisch {
1715208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17169bcf6551SClemens Ladisch 	unsigned int subs = kcontrol->id.subdevice;
17179bcf6551SClemens Ladisch 
17189bcf6551SClemens Ladisch 	ucontrol->value.integer.value[0] = chip->pcm_mixer[subs].left;
17199bcf6551SClemens Ladisch 	ucontrol->value.integer.value[1] = chip->pcm_mixer[subs].right;
17209bcf6551SClemens Ladisch 	return 0;
17219bcf6551SClemens Ladisch }
17229bcf6551SClemens Ladisch 
snd_ymfpci_pcm_vol_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1723208a1b4cSTakashi Iwai static int snd_ymfpci_pcm_vol_put(struct snd_kcontrol *kcontrol,
1724208a1b4cSTakashi Iwai 				  struct snd_ctl_elem_value *ucontrol)
17259bcf6551SClemens Ladisch {
1726208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = snd_kcontrol_chip(kcontrol);
17279bcf6551SClemens Ladisch 	unsigned int subs = kcontrol->id.subdevice;
1728208a1b4cSTakashi Iwai 	struct snd_pcm_substream *substream;
17299bcf6551SClemens Ladisch 	unsigned long flags;
17309bcf6551SClemens Ladisch 
17319bcf6551SClemens Ladisch 	if (ucontrol->value.integer.value[0] != chip->pcm_mixer[subs].left ||
17329bcf6551SClemens Ladisch 	    ucontrol->value.integer.value[1] != chip->pcm_mixer[subs].right) {
17339bcf6551SClemens Ladisch 		chip->pcm_mixer[subs].left = ucontrol->value.integer.value[0];
17349bcf6551SClemens Ladisch 		chip->pcm_mixer[subs].right = ucontrol->value.integer.value[1];
17354e98d6a7STakashi Iwai 		if (chip->pcm_mixer[subs].left > 0x8000)
17364e98d6a7STakashi Iwai 			chip->pcm_mixer[subs].left = 0x8000;
17374e98d6a7STakashi Iwai 		if (chip->pcm_mixer[subs].right > 0x8000)
17384e98d6a7STakashi Iwai 			chip->pcm_mixer[subs].right = 0x8000;
17399bcf6551SClemens Ladisch 
1740208a1b4cSTakashi Iwai 		substream = (struct snd_pcm_substream *)kcontrol->private_value;
17419bcf6551SClemens Ladisch 		spin_lock_irqsave(&chip->voice_lock, flags);
17429bcf6551SClemens Ladisch 		if (substream->runtime && substream->runtime->private_data) {
1743208a1b4cSTakashi Iwai 			struct snd_ymfpci_pcm *ypcm = substream->runtime->private_data;
17449ed1261eSTeru KAMOGASHIRA 			if (!ypcm->use_441_slot)
17459bcf6551SClemens Ladisch 				ypcm->update_pcm_vol = 2;
17469bcf6551SClemens Ladisch 		}
17479bcf6551SClemens Ladisch 		spin_unlock_irqrestore(&chip->voice_lock, flags);
17489bcf6551SClemens Ladisch 		return 1;
17499bcf6551SClemens Ladisch 	}
17509bcf6551SClemens Ladisch 	return 0;
17519bcf6551SClemens Ladisch }
17529bcf6551SClemens Ladisch 
1753f3b827e0SBhumika Goyal static const struct snd_kcontrol_new snd_ymfpci_pcm_volume = {
17549bcf6551SClemens Ladisch 	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
17559bcf6551SClemens Ladisch 	.name = "PCM Playback Volume",
17569bcf6551SClemens Ladisch 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
17579bcf6551SClemens Ladisch 		SNDRV_CTL_ELEM_ACCESS_INACTIVE,
17589bcf6551SClemens Ladisch 	.info = snd_ymfpci_pcm_vol_info,
17599bcf6551SClemens Ladisch 	.get = snd_ymfpci_pcm_vol_get,
17609bcf6551SClemens Ladisch 	.put = snd_ymfpci_pcm_vol_put,
17619bcf6551SClemens Ladisch };
17629bcf6551SClemens Ladisch 
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds /*
17651da177e4SLinus Torvalds  *  Mixer routines
17661da177e4SLinus Torvalds  */
17671da177e4SLinus Torvalds 
snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus * bus)1768208a1b4cSTakashi Iwai static void snd_ymfpci_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
17691da177e4SLinus Torvalds {
1770208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = bus->private_data;
17711da177e4SLinus Torvalds 	chip->ac97_bus = NULL;
17721da177e4SLinus Torvalds }
17731da177e4SLinus Torvalds 
snd_ymfpci_mixer_free_ac97(struct snd_ac97 * ac97)1774208a1b4cSTakashi Iwai static void snd_ymfpci_mixer_free_ac97(struct snd_ac97 *ac97)
17751da177e4SLinus Torvalds {
1776208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = ac97->private_data;
17771da177e4SLinus Torvalds 	chip->ac97 = NULL;
17781da177e4SLinus Torvalds }
17791da177e4SLinus Torvalds 
snd_ymfpci_mixer(struct snd_ymfpci * chip,int rear_switch)1780e23e7a14SBill Pemberton int snd_ymfpci_mixer(struct snd_ymfpci *chip, int rear_switch)
17811da177e4SLinus Torvalds {
1782208a1b4cSTakashi Iwai 	struct snd_ac97_template ac97;
1783208a1b4cSTakashi Iwai 	struct snd_kcontrol *kctl;
1784208a1b4cSTakashi Iwai 	struct snd_pcm_substream *substream;
17851da177e4SLinus Torvalds 	unsigned int idx;
17861da177e4SLinus Torvalds 	int err;
178751055da5STakashi Iwai 	static const struct snd_ac97_bus_ops ops = {
17881da177e4SLinus Torvalds 		.write = snd_ymfpci_codec_write,
17891da177e4SLinus Torvalds 		.read = snd_ymfpci_codec_read,
17901da177e4SLinus Torvalds 	};
17911da177e4SLinus Torvalds 
1792e7daaeedSTakashi Iwai 	err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus);
1793e7daaeedSTakashi Iwai 	if (err < 0)
17941da177e4SLinus Torvalds 		return err;
17951da177e4SLinus Torvalds 	chip->ac97_bus->private_free = snd_ymfpci_mixer_free_ac97_bus;
17961da177e4SLinus Torvalds 	chip->ac97_bus->no_vra = 1; /* YMFPCI doesn't need VRA */
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	memset(&ac97, 0, sizeof(ac97));
17991da177e4SLinus Torvalds 	ac97.private_data = chip;
18001da177e4SLinus Torvalds 	ac97.private_free = snd_ymfpci_mixer_free_ac97;
1801e7daaeedSTakashi Iwai 	err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97);
1802e7daaeedSTakashi Iwai 	if (err < 0)
18031da177e4SLinus Torvalds 		return err;
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 	/* to be sure */
18061da177e4SLinus Torvalds 	snd_ac97_update_bits(chip->ac97, AC97_EXTENDED_STATUS,
18071da177e4SLinus Torvalds 			     AC97_EA_VRA|AC97_EA_VRM, 0);
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 	for (idx = 0; idx < ARRAY_SIZE(snd_ymfpci_controls); idx++) {
1810e7daaeedSTakashi Iwai 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_controls[idx], chip));
1811e7daaeedSTakashi Iwai 		if (err < 0)
18121da177e4SLinus Torvalds 			return err;
18131da177e4SLinus Torvalds 	}
18144d20bb1dSRaymond Yau 	if (chip->ac97->ext_id & AC97_EI_SDAC) {
18154d20bb1dSRaymond Yau 		kctl = snd_ctl_new1(&snd_ymfpci_dup4ch, chip);
18164d20bb1dSRaymond Yau 		err = snd_ctl_add(chip->card, kctl);
18174d20bb1dSRaymond Yau 		if (err < 0)
18184d20bb1dSRaymond Yau 			return err;
18194d20bb1dSRaymond Yau 	}
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 	/* add S/PDIF control */
1822da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip->pcm_spdif))
1823da3cec35STakashi Iwai 		return -ENXIO;
1824e7daaeedSTakashi Iwai 	kctl = snd_ctl_new1(&snd_ymfpci_spdif_default, chip);
1825*c9b83ae4STakashi Iwai 	kctl->id.device = chip->pcm_spdif->device;
1826e7daaeedSTakashi Iwai 	err = snd_ctl_add(chip->card, kctl);
1827e7daaeedSTakashi Iwai 	if (err < 0)
18281da177e4SLinus Torvalds 		return err;
1829e7daaeedSTakashi Iwai 	kctl = snd_ctl_new1(&snd_ymfpci_spdif_mask, chip);
1830*c9b83ae4STakashi Iwai 	kctl->id.device = chip->pcm_spdif->device;
1831e7daaeedSTakashi Iwai 	err = snd_ctl_add(chip->card, kctl);
1832e7daaeedSTakashi Iwai 	if (err < 0)
18331da177e4SLinus Torvalds 		return err;
1834e7daaeedSTakashi Iwai 	kctl = snd_ctl_new1(&snd_ymfpci_spdif_stream, chip);
1835*c9b83ae4STakashi Iwai 	kctl->id.device = chip->pcm_spdif->device;
1836e7daaeedSTakashi Iwai 	err = snd_ctl_add(chip->card, kctl);
1837e7daaeedSTakashi Iwai 	if (err < 0)
18381da177e4SLinus Torvalds 		return err;
18391da177e4SLinus Torvalds 	chip->spdif_pcm_ctl = kctl;
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	/* direct recording source */
1842e7daaeedSTakashi Iwai 	if (chip->device_id == PCI_DEVICE_ID_YAMAHA_754) {
1843e7daaeedSTakashi Iwai 		kctl = snd_ctl_new1(&snd_ymfpci_drec_source, chip);
1844e7daaeedSTakashi Iwai 		err = snd_ctl_add(chip->card, kctl);
1845e7daaeedSTakashi Iwai 		if (err < 0)
18461da177e4SLinus Torvalds 			return err;
1847e7daaeedSTakashi Iwai 	}
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds 	/*
18501da177e4SLinus Torvalds 	 * shared rear/line-in
18511da177e4SLinus Torvalds 	 */
18521da177e4SLinus Torvalds 	if (rear_switch) {
1853e7daaeedSTakashi Iwai 		err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_ymfpci_rear_shared, chip));
1854e7daaeedSTakashi Iwai 		if (err < 0)
18551da177e4SLinus Torvalds 			return err;
18561da177e4SLinus Torvalds 	}
18571da177e4SLinus Torvalds 
18589bcf6551SClemens Ladisch 	/* per-voice volume */
18599bcf6551SClemens Ladisch 	substream = chip->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
18609bcf6551SClemens Ladisch 	for (idx = 0; idx < 32; ++idx) {
18619bcf6551SClemens Ladisch 		kctl = snd_ctl_new1(&snd_ymfpci_pcm_volume, chip);
18629bcf6551SClemens Ladisch 		if (!kctl)
18639bcf6551SClemens Ladisch 			return -ENOMEM;
18649bcf6551SClemens Ladisch 		kctl->id.device = chip->pcm->device;
18659bcf6551SClemens Ladisch 		kctl->id.subdevice = idx;
18669bcf6551SClemens Ladisch 		kctl->private_value = (unsigned long)substream;
1867e7daaeedSTakashi Iwai 		err = snd_ctl_add(chip->card, kctl);
1868e7daaeedSTakashi Iwai 		if (err < 0)
18699bcf6551SClemens Ladisch 			return err;
18709bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].left = 0x8000;
18719bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].right = 0x8000;
18729bcf6551SClemens Ladisch 		chip->pcm_mixer[idx].ctl = kctl;
18739bcf6551SClemens Ladisch 		substream = substream->next;
18749bcf6551SClemens Ladisch 	}
18759bcf6551SClemens Ladisch 
18761da177e4SLinus Torvalds 	return 0;
18771da177e4SLinus Torvalds }
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds /*
18811da177e4SLinus Torvalds  * timer
18821da177e4SLinus Torvalds  */
18831da177e4SLinus Torvalds 
snd_ymfpci_timer_start(struct snd_timer * timer)1884208a1b4cSTakashi Iwai static int snd_ymfpci_timer_start(struct snd_timer *timer)
18851da177e4SLinus Torvalds {
1886208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
18871da177e4SLinus Torvalds 	unsigned long flags;
18881da177e4SLinus Torvalds 	unsigned int count;
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds 	chip = snd_timer_chip(timer);
18911da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
18926e2efaacSClemens Ladisch 	if (timer->sticks > 1) {
18936e2efaacSClemens Ladisch 		chip->timer_ticks = timer->sticks;
18946e2efaacSClemens Ladisch 		count = timer->sticks - 1;
18956e2efaacSClemens Ladisch 	} else {
18966e2efaacSClemens Ladisch 		/*
18976e2efaacSClemens Ladisch 		 * Divisor 1 is not allowed; fake it by using divisor 2 and
18986e2efaacSClemens Ladisch 		 * counting two ticks for each interrupt.
18996e2efaacSClemens Ladisch 		 */
19006e2efaacSClemens Ladisch 		chip->timer_ticks = 2;
19016e2efaacSClemens Ladisch 		count = 2 - 1;
19026e2efaacSClemens Ladisch 	}
19031da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_TIMERCOUNT, count);
19041da177e4SLinus Torvalds 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x03);
19051da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
19061da177e4SLinus Torvalds 	return 0;
19071da177e4SLinus Torvalds }
19081da177e4SLinus Torvalds 
snd_ymfpci_timer_stop(struct snd_timer * timer)1909208a1b4cSTakashi Iwai static int snd_ymfpci_timer_stop(struct snd_timer *timer)
19101da177e4SLinus Torvalds {
1911208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip;
19121da177e4SLinus Torvalds 	unsigned long flags;
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 	chip = snd_timer_chip(timer);
19151da177e4SLinus Torvalds 	spin_lock_irqsave(&chip->reg_lock, flags);
19161da177e4SLinus Torvalds 	snd_ymfpci_writeb(chip, YDSXGR_TIMERCTRL, 0x00);
19171da177e4SLinus Torvalds 	spin_unlock_irqrestore(&chip->reg_lock, flags);
19181da177e4SLinus Torvalds 	return 0;
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
snd_ymfpci_timer_precise_resolution(struct snd_timer * timer,unsigned long * num,unsigned long * den)1921208a1b4cSTakashi Iwai static int snd_ymfpci_timer_precise_resolution(struct snd_timer *timer,
19221da177e4SLinus Torvalds 					       unsigned long *num, unsigned long *den)
19231da177e4SLinus Torvalds {
19241da177e4SLinus Torvalds 	*num = 1;
19256e2efaacSClemens Ladisch 	*den = 96000;
19261da177e4SLinus Torvalds 	return 0;
19271da177e4SLinus Torvalds }
19281da177e4SLinus Torvalds 
19295ff16a3dSTakashi Iwai static const struct snd_timer_hardware snd_ymfpci_timer_hw = {
19301da177e4SLinus Torvalds 	.flags = SNDRV_TIMER_HW_AUTO,
19316e2efaacSClemens Ladisch 	.resolution = 10417, /* 1 / 96 kHz = 10.41666...us */
19326e2efaacSClemens Ladisch 	.ticks = 0x10000,
19331da177e4SLinus Torvalds 	.start = snd_ymfpci_timer_start,
19341da177e4SLinus Torvalds 	.stop = snd_ymfpci_timer_stop,
19351da177e4SLinus Torvalds 	.precise_resolution = snd_ymfpci_timer_precise_resolution,
19361da177e4SLinus Torvalds };
19371da177e4SLinus Torvalds 
snd_ymfpci_timer(struct snd_ymfpci * chip,int device)1938e23e7a14SBill Pemberton int snd_ymfpci_timer(struct snd_ymfpci *chip, int device)
19391da177e4SLinus Torvalds {
1940208a1b4cSTakashi Iwai 	struct snd_timer *timer = NULL;
1941208a1b4cSTakashi Iwai 	struct snd_timer_id tid;
19421da177e4SLinus Torvalds 	int err;
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
19451da177e4SLinus Torvalds 	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
19461da177e4SLinus Torvalds 	tid.card = chip->card->number;
19471da177e4SLinus Torvalds 	tid.device = device;
19481da177e4SLinus Torvalds 	tid.subdevice = 0;
1949e7daaeedSTakashi Iwai 	err = snd_timer_new(chip->card, "YMFPCI", &tid, &timer);
1950e7daaeedSTakashi Iwai 	if (err >= 0) {
19511da177e4SLinus Torvalds 		strcpy(timer->name, "YMFPCI timer");
19521da177e4SLinus Torvalds 		timer->private_data = chip;
19531da177e4SLinus Torvalds 		timer->hw = snd_ymfpci_timer_hw;
19541da177e4SLinus Torvalds 	}
19551da177e4SLinus Torvalds 	chip->timer = timer;
19561da177e4SLinus Torvalds 	return err;
19571da177e4SLinus Torvalds }
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds /*
19611da177e4SLinus Torvalds  *  proc interface
19621da177e4SLinus Torvalds  */
19631da177e4SLinus Torvalds 
snd_ymfpci_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)1964208a1b4cSTakashi Iwai static void snd_ymfpci_proc_read(struct snd_info_entry *entry,
1965208a1b4cSTakashi Iwai 				 struct snd_info_buffer *buffer)
19661da177e4SLinus Torvalds {
1967208a1b4cSTakashi Iwai 	struct snd_ymfpci *chip = entry->private_data;
19681da177e4SLinus Torvalds 	int i;
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 	snd_iprintf(buffer, "YMFPCI\n\n");
19711da177e4SLinus Torvalds 	for (i = 0; i <= YDSXGR_WORKBASE; i += 4)
19721da177e4SLinus Torvalds 		snd_iprintf(buffer, "%04x: %04x\n", i, snd_ymfpci_readl(chip, i));
19731da177e4SLinus Torvalds }
19741da177e4SLinus Torvalds 
snd_ymfpci_proc_init(struct snd_card * card,struct snd_ymfpci * chip)1975e23e7a14SBill Pemberton static int snd_ymfpci_proc_init(struct snd_card *card, struct snd_ymfpci *chip)
19761da177e4SLinus Torvalds {
197747f2769bSTakashi Iwai 	return snd_card_ro_proc_new(card, "ymfpci", chip, snd_ymfpci_proc_read);
19781da177e4SLinus Torvalds }
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds /*
19811da177e4SLinus Torvalds  *  initialization routines
19821da177e4SLinus Torvalds  */
19831da177e4SLinus Torvalds 
snd_ymfpci_aclink_reset(struct pci_dev * pci)19841da177e4SLinus Torvalds static void snd_ymfpci_aclink_reset(struct pci_dev * pci)
19851da177e4SLinus Torvalds {
19861da177e4SLinus Torvalds 	u8 cmd;
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds 	pci_read_config_byte(pci, PCIR_DSXG_CTRL, &cmd);
19891da177e4SLinus Torvalds #if 0 // force to reset
19901da177e4SLinus Torvalds 	if (cmd & 0x03) {
19911da177e4SLinus Torvalds #endif
19921da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
19931da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd | 0x03);
19941da177e4SLinus Torvalds 		pci_write_config_byte(pci, PCIR_DSXG_CTRL, cmd & 0xfc);
19951da177e4SLinus Torvalds 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL1, 0);
19961da177e4SLinus Torvalds 		pci_write_config_word(pci, PCIR_DSXG_PWRCTRL2, 0);
19971da177e4SLinus Torvalds #if 0
19981da177e4SLinus Torvalds 	}
19991da177e4SLinus Torvalds #endif
20001da177e4SLinus Torvalds }
20011da177e4SLinus Torvalds 
snd_ymfpci_enable_dsp(struct snd_ymfpci * chip)2002208a1b4cSTakashi Iwai static void snd_ymfpci_enable_dsp(struct snd_ymfpci *chip)
20031da177e4SLinus Torvalds {
20041da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000001);
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds 
snd_ymfpci_disable_dsp(struct snd_ymfpci * chip)2007208a1b4cSTakashi Iwai static void snd_ymfpci_disable_dsp(struct snd_ymfpci *chip)
20081da177e4SLinus Torvalds {
20091da177e4SLinus Torvalds 	u32 val;
20101da177e4SLinus Torvalds 	int timeout = 1000;
20111da177e4SLinus Torvalds 
20121da177e4SLinus Torvalds 	val = snd_ymfpci_readl(chip, YDSXGR_CONFIG);
20131da177e4SLinus Torvalds 	if (val)
20141da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_CONFIG, 0x00000000);
20151da177e4SLinus Torvalds 	while (timeout-- > 0) {
20161da177e4SLinus Torvalds 		val = snd_ymfpci_readl(chip, YDSXGR_STATUS);
20171da177e4SLinus Torvalds 		if ((val & 0x00000002) == 0)
20181da177e4SLinus Torvalds 			break;
20191da177e4SLinus Torvalds 	}
20201da177e4SLinus Torvalds }
20211da177e4SLinus Torvalds 
snd_ymfpci_request_firmware(struct snd_ymfpci * chip)2022102fa906SClemens Ladisch static int snd_ymfpci_request_firmware(struct snd_ymfpci *chip)
2023102fa906SClemens Ladisch {
2024102fa906SClemens Ladisch 	int err, is_1e;
2025102fa906SClemens Ladisch 	const char *name;
2026102fa906SClemens Ladisch 
2027102fa906SClemens Ladisch 	err = request_firmware(&chip->dsp_microcode, "yamaha/ds1_dsp.fw",
2028102fa906SClemens Ladisch 			       &chip->pci->dev);
2029102fa906SClemens Ladisch 	if (err >= 0) {
2030b82a82d0SDavid Woodhouse 		if (chip->dsp_microcode->size != YDSXG_DSPLENGTH) {
20316436bcf6STakashi Iwai 			dev_err(chip->card->dev,
20326436bcf6STakashi Iwai 				"DSP microcode has wrong size\n");
2033102fa906SClemens Ladisch 			err = -EINVAL;
2034102fa906SClemens Ladisch 		}
2035102fa906SClemens Ladisch 	}
2036b7dd2b34STakashi Iwai 	if (err < 0)
2037102fa906SClemens Ladisch 		return err;
2038102fa906SClemens Ladisch 	is_1e = chip->device_id == PCI_DEVICE_ID_YAMAHA_724F ||
2039102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_740C ||
2040102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_744 ||
2041102fa906SClemens Ladisch 		chip->device_id == PCI_DEVICE_ID_YAMAHA_754;
2042102fa906SClemens Ladisch 	name = is_1e ? "yamaha/ds1e_ctrl.fw" : "yamaha/ds1_ctrl.fw";
2043102fa906SClemens Ladisch 	err = request_firmware(&chip->controller_microcode, name,
2044102fa906SClemens Ladisch 			       &chip->pci->dev);
2045102fa906SClemens Ladisch 	if (err >= 0) {
2046b82a82d0SDavid Woodhouse 		if (chip->controller_microcode->size != YDSXG_CTRLLENGTH) {
20476436bcf6STakashi Iwai 			dev_err(chip->card->dev,
20486436bcf6STakashi Iwai 				"controller microcode has wrong size\n");
2049102fa906SClemens Ladisch 			err = -EINVAL;
2050102fa906SClemens Ladisch 		}
2051102fa906SClemens Ladisch 	}
2052b7dd2b34STakashi Iwai 	if (err < 0)
2053102fa906SClemens Ladisch 		return err;
2054102fa906SClemens Ladisch 	return 0;
2055102fa906SClemens Ladisch }
20567e0af29dSClemens Ladisch 
20577e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1_dsp.fw");
20587e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1_ctrl.fw");
20597e0af29dSClemens Ladisch MODULE_FIRMWARE("yamaha/ds1e_ctrl.fw");
20607e0af29dSClemens Ladisch 
snd_ymfpci_download_image(struct snd_ymfpci * chip)2061208a1b4cSTakashi Iwai static void snd_ymfpci_download_image(struct snd_ymfpci *chip)
20621da177e4SLinus Torvalds {
20631da177e4SLinus Torvalds 	int i;
20641da177e4SLinus Torvalds 	u16 ctrl;
2065b82a82d0SDavid Woodhouse 	const __le32 *inst;
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x00000000);
20681da177e4SLinus Torvalds 	snd_ymfpci_disable_dsp(chip);
20691da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00010000);
20701da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MODE, 0x00000000);
20711da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFREC, 0x00000000);
20721da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_MAPOFEFFECT, 0x00000000);
20731da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0x00000000);
20741da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0x00000000);
20751da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0x00000000);
20761da177e4SLinus Torvalds 	ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
20771da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds 	/* setup DSP instruction code */
2080b82a82d0SDavid Woodhouse 	inst = (const __le32 *)chip->dsp_microcode->data;
20811da177e4SLinus Torvalds 	for (i = 0; i < YDSXG_DSPLENGTH / 4; i++)
2082b82a82d0SDavid Woodhouse 		snd_ymfpci_writel(chip, YDSXGR_DSPINSTRAM + (i << 2),
2083b82a82d0SDavid Woodhouse 				  le32_to_cpu(inst[i]));
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	/* setup control instruction code */
2086b82a82d0SDavid Woodhouse 	inst = (const __le32 *)chip->controller_microcode->data;
20871da177e4SLinus Torvalds 	for (i = 0; i < YDSXG_CTRLLENGTH / 4; i++)
2088b82a82d0SDavid Woodhouse 		snd_ymfpci_writel(chip, YDSXGR_CTRLINSTRAM + (i << 2),
2089b82a82d0SDavid Woodhouse 				  le32_to_cpu(inst[i]));
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds 	snd_ymfpci_enable_dsp(chip);
20921da177e4SLinus Torvalds }
20931da177e4SLinus Torvalds 
snd_ymfpci_memalloc(struct snd_ymfpci * chip)2094e23e7a14SBill Pemberton static int snd_ymfpci_memalloc(struct snd_ymfpci *chip)
20951da177e4SLinus Torvalds {
20961da177e4SLinus Torvalds 	long size, playback_ctrl_size;
20971da177e4SLinus Torvalds 	int voice, bank, reg;
20981da177e4SLinus Torvalds 	u8 *ptr;
20991da177e4SLinus Torvalds 	dma_addr_t ptr_addr;
21001da177e4SLinus Torvalds 
21011da177e4SLinus Torvalds 	playback_ctrl_size = 4 + 4 * YDSXG_PLAYBACK_VOICES;
21021da177e4SLinus Torvalds 	chip->bank_size_playback = snd_ymfpci_readl(chip, YDSXGR_PLAYCTRLSIZE) << 2;
21031da177e4SLinus Torvalds 	chip->bank_size_capture = snd_ymfpci_readl(chip, YDSXGR_RECCTRLSIZE) << 2;
21041da177e4SLinus Torvalds 	chip->bank_size_effect = snd_ymfpci_readl(chip, YDSXGR_EFFCTRLSIZE) << 2;
21051da177e4SLinus Torvalds 	chip->work_size = YDSXG_DEFAULT_WORK_SIZE;
21061da177e4SLinus Torvalds 
21077ab39926SClemens Ladisch 	size = ALIGN(playback_ctrl_size, 0x100) +
21087ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_playback * 2 * YDSXG_PLAYBACK_VOICES, 0x100) +
21097ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_capture * 2 * YDSXG_CAPTURE_VOICES, 0x100) +
21107ab39926SClemens Ladisch 	       ALIGN(chip->bank_size_effect * 2 * YDSXG_EFFECT_VOICES, 0x100) +
21111da177e4SLinus Torvalds 	       chip->work_size;
21121da177e4SLinus Torvalds 	/* work_ptr must be aligned to 256 bytes, but it's already
21131da177e4SLinus Torvalds 	   covered with the kernel page allocation mechanism */
2114c6e6bb5eSTakashi Iwai 	chip->work_ptr = snd_devm_alloc_pages(&chip->pci->dev,
2115c6e6bb5eSTakashi Iwai 					      SNDRV_DMA_TYPE_DEV, size);
2116c6e6bb5eSTakashi Iwai 	if (!chip->work_ptr)
21171da177e4SLinus Torvalds 		return -ENOMEM;
2118c6e6bb5eSTakashi Iwai 	ptr = chip->work_ptr->area;
2119c6e6bb5eSTakashi Iwai 	ptr_addr = chip->work_ptr->addr;
21201da177e4SLinus Torvalds 	memset(ptr, 0, size);	/* for sure */
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds 	chip->bank_base_playback = ptr;
21231da177e4SLinus Torvalds 	chip->bank_base_playback_addr = ptr_addr;
2124d3c63763STakashi Iwai 	chip->ctrl_playback = (__le32 *)ptr;
21251da177e4SLinus Torvalds 	chip->ctrl_playback[0] = cpu_to_le32(YDSXG_PLAYBACK_VOICES);
21267ab39926SClemens Ladisch 	ptr += ALIGN(playback_ctrl_size, 0x100);
21277ab39926SClemens Ladisch 	ptr_addr += ALIGN(playback_ctrl_size, 0x100);
21281da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_PLAYBACK_VOICES; voice++) {
21291da177e4SLinus Torvalds 		chip->voices[voice].number = voice;
2130208a1b4cSTakashi Iwai 		chip->voices[voice].bank = (struct snd_ymfpci_playback_bank *)ptr;
21311da177e4SLinus Torvalds 		chip->voices[voice].bank_addr = ptr_addr;
21321da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2133208a1b4cSTakashi Iwai 			chip->bank_playback[voice][bank] = (struct snd_ymfpci_playback_bank *)ptr;
21341da177e4SLinus Torvalds 			ptr += chip->bank_size_playback;
21351da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_playback;
21361da177e4SLinus Torvalds 		}
21371da177e4SLinus Torvalds 	}
21387ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21397ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21401da177e4SLinus Torvalds 	chip->bank_base_capture = ptr;
21411da177e4SLinus Torvalds 	chip->bank_base_capture_addr = ptr_addr;
21421da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_CAPTURE_VOICES; voice++)
21431da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2144208a1b4cSTakashi Iwai 			chip->bank_capture[voice][bank] = (struct snd_ymfpci_capture_bank *)ptr;
21451da177e4SLinus Torvalds 			ptr += chip->bank_size_capture;
21461da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_capture;
21471da177e4SLinus Torvalds 		}
21487ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21497ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21501da177e4SLinus Torvalds 	chip->bank_base_effect = ptr;
21511da177e4SLinus Torvalds 	chip->bank_base_effect_addr = ptr_addr;
21521da177e4SLinus Torvalds 	for (voice = 0; voice < YDSXG_EFFECT_VOICES; voice++)
21531da177e4SLinus Torvalds 		for (bank = 0; bank < 2; bank++) {
2154208a1b4cSTakashi Iwai 			chip->bank_effect[voice][bank] = (struct snd_ymfpci_effect_bank *)ptr;
21551da177e4SLinus Torvalds 			ptr += chip->bank_size_effect;
21561da177e4SLinus Torvalds 			ptr_addr += chip->bank_size_effect;
21571da177e4SLinus Torvalds 		}
21587ab39926SClemens Ladisch 	ptr = (char *)ALIGN((unsigned long)ptr, 0x100);
21597ab39926SClemens Ladisch 	ptr_addr = ALIGN(ptr_addr, 0x100);
21601da177e4SLinus Torvalds 	chip->work_base = ptr;
21611da177e4SLinus Torvalds 	chip->work_base_addr = ptr_addr;
21621da177e4SLinus Torvalds 
21636be2e752STasos Sahanidis 	snd_BUG_ON(ptr + PAGE_ALIGN(chip->work_size) !=
2164c6e6bb5eSTakashi Iwai 		   chip->work_ptr->area + chip->work_ptr->bytes);
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, chip->bank_base_playback_addr);
21671da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, chip->bank_base_capture_addr);
21681da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, chip->bank_base_effect_addr);
21691da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_WORKBASE, chip->work_base_addr);
21701da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, chip->work_size >> 2);
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds 	/* S/PDIF output initialization */
21731da177e4SLinus Torvalds 	chip->spdif_bits = chip->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF & 0xffff;
21741da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTCTRL, 0);
21751da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFOUTSTATUS, chip->spdif_bits);
21761da177e4SLinus Torvalds 
21771da177e4SLinus Torvalds 	/* S/PDIF input initialization */
21781da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_SPDIFINCTRL, 0);
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 	/* digital mixer setup */
21811da177e4SLinus Torvalds 	for (reg = 0x80; reg < 0xc0; reg += 4)
21821da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, reg, 0);
21831da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0x3fff3fff);
21844a3b6983STakashi Iwai 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0x3fff3fff);
21851da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_ZVOUTVOL, 0x3fff3fff);
21861da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_SPDIFOUTVOL, 0x3fff3fff);
21871da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEADCINVOL, 0x3fff3fff);
21881da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACINVOL, 0x3fff3fff);
21891da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PRIADCLOOPVOL, 0x3fff3fff);
21901da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0x3fff3fff);
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds 	return 0;
21931da177e4SLinus Torvalds }
21941da177e4SLinus Torvalds 
snd_ymfpci_free(struct snd_card * card)2195c6e6bb5eSTakashi Iwai static void snd_ymfpci_free(struct snd_card *card)
21961da177e4SLinus Torvalds {
2197c6e6bb5eSTakashi Iwai 	struct snd_ymfpci *chip = card->private_data;
21981da177e4SLinus Torvalds 	u16 ctrl;
21991da177e4SLinus Torvalds 
22001da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
22011da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
22021da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_LEGACYOUTVOL, 0);
22031da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_STATUS, ~0);
22041da177e4SLinus Torvalds 	snd_ymfpci_disable_dsp(chip);
22051da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_PLAYCTRLBASE, 0);
22061da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_RECCTRLBASE, 0);
22071da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_EFFCTRLBASE, 0);
22081da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_WORKBASE, 0);
22091da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_WORKSIZE, 0);
22101da177e4SLinus Torvalds 	ctrl = snd_ymfpci_readw(chip, YDSXGR_GLOBALCTRL);
22111da177e4SLinus Torvalds 	snd_ymfpci_writew(chip, YDSXGR_GLOBALCTRL, ctrl & ~0x0007);
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 	snd_ymfpci_ac3_done(chip);
22141da177e4SLinus Torvalds 
22151da177e4SLinus Torvalds 	snd_ymfpci_free_gameport(chip);
22161da177e4SLinus Torvalds 
22172fa98a42STasos Sahanidis 	pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, chip->old_legacy_ctrl);
22181da177e4SLinus Torvalds 
2219102fa906SClemens Ladisch 	release_firmware(chip->dsp_microcode);
2220102fa906SClemens Ladisch 	release_firmware(chip->controller_microcode);
22211da177e4SLinus Torvalds }
22221da177e4SLinus Torvalds 
snd_ymfpci_suspend(struct device * dev)222368cb2b55STakashi Iwai static int snd_ymfpci_suspend(struct device *dev)
22241da177e4SLinus Torvalds {
222568cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
2226ded46235STakashi Iwai 	struct snd_ymfpci *chip = card->private_data;
22274fa4a147STasos Sahanidis 	unsigned int i, legacy_reg_count = DSXG_PCI_NUM_SAVED_LEGACY_REGS;
22284fa4a147STasos Sahanidis 
22294fa4a147STasos Sahanidis 	if (chip->pci->device >= 0x0010) /* YMF 744/754 */
22304fa4a147STasos Sahanidis 		legacy_reg_count = DSXG_PCI_NUM_SAVED_REGS;
22311da177e4SLinus Torvalds 
2232ded46235STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
22331da177e4SLinus Torvalds 	snd_ac97_suspend(chip->ac97);
223439fef76cSTasos Sahanidis 
22351da177e4SLinus Torvalds 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
22361da177e4SLinus Torvalds 		chip->saved_regs[i] = snd_ymfpci_readl(chip, saved_regs_index[i]);
223739fef76cSTasos Sahanidis 
22381da177e4SLinus Torvalds 	chip->saved_ydsxgr_mode = snd_ymfpci_readl(chip, YDSXGR_MODE);
223939fef76cSTasos Sahanidis 
22404fa4a147STasos Sahanidis 	for (i = 0; i < legacy_reg_count; i++)
224139fef76cSTasos Sahanidis 		pci_read_config_word(chip->pci, pci_saved_regs_index[i],
224239fef76cSTasos Sahanidis 				      chip->saved_dsxg_pci_regs + i);
224339fef76cSTasos Sahanidis 
22441da177e4SLinus Torvalds 	snd_ymfpci_writel(chip, YDSXGR_NATIVEDACOUTVOL, 0);
22454a3b6983STakashi Iwai 	snd_ymfpci_writel(chip, YDSXGR_BUF441OUTVOL, 0);
22461da177e4SLinus Torvalds 	snd_ymfpci_disable_dsp(chip);
22471da177e4SLinus Torvalds 	return 0;
22481da177e4SLinus Torvalds }
22491da177e4SLinus Torvalds 
snd_ymfpci_resume(struct device * dev)225068cb2b55STakashi Iwai static int snd_ymfpci_resume(struct device *dev)
22511da177e4SLinus Torvalds {
225268cb2b55STakashi Iwai 	struct pci_dev *pci = to_pci_dev(dev);
225368cb2b55STakashi Iwai 	struct snd_card *card = dev_get_drvdata(dev);
2254ded46235STakashi Iwai 	struct snd_ymfpci *chip = card->private_data;
22554fa4a147STasos Sahanidis 	unsigned int i, legacy_reg_count = DSXG_PCI_NUM_SAVED_LEGACY_REGS;
22564fa4a147STasos Sahanidis 
22574fa4a147STasos Sahanidis 	if (chip->pci->device >= 0x0010) /* YMF 744/754 */
22584fa4a147STasos Sahanidis 		legacy_reg_count = DSXG_PCI_NUM_SAVED_REGS;
22591da177e4SLinus Torvalds 
2260ded46235STakashi Iwai 	snd_ymfpci_aclink_reset(pci);
22611da177e4SLinus Torvalds 	snd_ymfpci_codec_ready(chip, 0);
22621da177e4SLinus Torvalds 	snd_ymfpci_download_image(chip);
22631da177e4SLinus Torvalds 	udelay(100);
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	for (i = 0; i < YDSXGR_NUM_SAVED_REGS; i++)
22661da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, saved_regs_index[i], chip->saved_regs[i]);
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds 	snd_ac97_resume(chip->ac97);
22691da177e4SLinus Torvalds 
22704fa4a147STasos Sahanidis 	for (i = 0; i < legacy_reg_count; i++)
227139fef76cSTasos Sahanidis 		pci_write_config_word(chip->pci, pci_saved_regs_index[i],
227239fef76cSTasos Sahanidis 				      chip->saved_dsxg_pci_regs[i]);
227328aa165cSTakashi Iwai 
22741da177e4SLinus Torvalds 	/* start hw again */
22751da177e4SLinus Torvalds 	if (chip->start_count > 0) {
22761da177e4SLinus Torvalds 		spin_lock_irq(&chip->reg_lock);
22771da177e4SLinus Torvalds 		snd_ymfpci_writel(chip, YDSXGR_MODE, chip->saved_ydsxgr_mode);
22781da177e4SLinus Torvalds 		chip->active_bank = snd_ymfpci_readl(chip, YDSXGR_CTRLSELECT);
22791da177e4SLinus Torvalds 		spin_unlock_irq(&chip->reg_lock);
22801da177e4SLinus Torvalds 	}
2281ded46235STakashi Iwai 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
22821da177e4SLinus Torvalds 	return 0;
22831da177e4SLinus Torvalds }
228468cb2b55STakashi Iwai 
2285081364d7STasos Sahanidis DEFINE_SIMPLE_DEV_PM_OPS(snd_ymfpci_pm, snd_ymfpci_suspend, snd_ymfpci_resume);
22861da177e4SLinus Torvalds 
snd_ymfpci_create(struct snd_card * card,struct pci_dev * pci,u16 old_legacy_ctrl)2287e23e7a14SBill Pemberton int snd_ymfpci_create(struct snd_card *card,
22881da177e4SLinus Torvalds 		      struct pci_dev *pci,
2289a8752868STasos Sahanidis 		      u16 old_legacy_ctrl)
22901da177e4SLinus Torvalds {
2291c6e6bb5eSTakashi Iwai 	struct snd_ymfpci *chip = card->private_data;
22921da177e4SLinus Torvalds 	int err;
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds 	/* enable PCI device */
2295c6e6bb5eSTakashi Iwai 	err = pcim_enable_device(pci);
2296e7daaeedSTakashi Iwai 	if (err < 0)
22971da177e4SLinus Torvalds 		return err;
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds 	chip->old_legacy_ctrl = old_legacy_ctrl;
23001da177e4SLinus Torvalds 	spin_lock_init(&chip->reg_lock);
23011da177e4SLinus Torvalds 	spin_lock_init(&chip->voice_lock);
23021da177e4SLinus Torvalds 	init_waitqueue_head(&chip->interrupt_sleep);
23031da177e4SLinus Torvalds 	atomic_set(&chip->interrupt_sleep_count, 0);
23041da177e4SLinus Torvalds 	chip->card = card;
23051da177e4SLinus Torvalds 	chip->pci = pci;
23061da177e4SLinus Torvalds 	chip->irq = -1;
23071da177e4SLinus Torvalds 	chip->device_id = pci->device;
230844c10138SAuke Kok 	chip->rev = pci->revision;
23091da177e4SLinus Torvalds 
2310c6e6bb5eSTakashi Iwai 	err = pci_request_regions(pci, "YMFPCI");
2311c6e6bb5eSTakashi Iwai 	if (err < 0)
2312c6e6bb5eSTakashi Iwai 		return err;
2313c6e6bb5eSTakashi Iwai 
2314c6e6bb5eSTakashi Iwai 	chip->reg_area_phys = pci_resource_start(pci, 0);
2315c6e6bb5eSTakashi Iwai 	chip->reg_area_virt = devm_ioremap(&pci->dev, chip->reg_area_phys, 0x8000);
2316c6e6bb5eSTakashi Iwai 	if (!chip->reg_area_virt) {
23176436bcf6STakashi Iwai 		dev_err(chip->card->dev,
23186436bcf6STakashi Iwai 			"unable to grab memory region 0x%lx-0x%lx\n",
23196436bcf6STakashi Iwai 			chip->reg_area_phys, chip->reg_area_phys + 0x8000 - 1);
2320c6e6bb5eSTakashi Iwai 		return -EBUSY;
23211da177e4SLinus Torvalds 	}
2322c6e6bb5eSTakashi Iwai 	pci_set_master(pci);
2323c6e6bb5eSTakashi Iwai 	chip->src441_used = -1;
2324c6e6bb5eSTakashi Iwai 
2325c6e6bb5eSTakashi Iwai 	if (devm_request_irq(&pci->dev, pci->irq, snd_ymfpci_interrupt, IRQF_SHARED,
2326934c2b6dSTakashi Iwai 			KBUILD_MODNAME, chip)) {
23276436bcf6STakashi Iwai 		dev_err(chip->card->dev, "unable to grab IRQ %d\n", pci->irq);
2328c6e6bb5eSTakashi Iwai 		return -EBUSY;
23291da177e4SLinus Torvalds 	}
23301da177e4SLinus Torvalds 	chip->irq = pci->irq;
2331b7a03a1eSTakashi Iwai 	card->sync_irq = chip->irq;
2332c6e6bb5eSTakashi Iwai 	card->private_free = snd_ymfpci_free;
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 	snd_ymfpci_aclink_reset(pci);
2335c6e6bb5eSTakashi Iwai 	if (snd_ymfpci_codec_ready(chip, 0) < 0)
2336c6e6bb5eSTakashi Iwai 		return -EIO;
23371da177e4SLinus Torvalds 
2338102fa906SClemens Ladisch 	err = snd_ymfpci_request_firmware(chip);
2339102fa906SClemens Ladisch 	if (err < 0) {
23406436bcf6STakashi Iwai 		dev_err(chip->card->dev, "firmware request failed: %d\n", err);
2341c6e6bb5eSTakashi Iwai 		return err;
2342102fa906SClemens Ladisch 	}
23431da177e4SLinus Torvalds 	snd_ymfpci_download_image(chip);
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds 	udelay(100); /* seems we need a delay after downloading image.. */
23461da177e4SLinus Torvalds 
2347c6e6bb5eSTakashi Iwai 	if (snd_ymfpci_memalloc(chip) < 0)
2348c6e6bb5eSTakashi Iwai 		return -EIO;
23491da177e4SLinus Torvalds 
2350ba2186e4SMarkus Elfring 	err = snd_ymfpci_ac3_init(chip);
2351ba2186e4SMarkus Elfring 	if (err < 0)
2352c6e6bb5eSTakashi Iwai 		return err;
23531da177e4SLinus Torvalds 
23541da177e4SLinus Torvalds 	snd_ymfpci_proc_init(card, chip);
23551da177e4SLinus Torvalds 
23561da177e4SLinus Torvalds 	return 0;
23571da177e4SLinus Torvalds }
2358